PR c++/86550
[official-gcc.git] / gcc / cp / parser.c
blob90d5d007317c679768fb6969831065b57f1a3ef9
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 expanded_location token_exploc = expand_location (token->location);
2866 /* Consume tokens until the end of the source line. */
2867 while (1)
2869 cp_lexer_consume_token (parser->lexer);
2870 cp_token *next = cp_lexer_peek_token (parser->lexer);
2871 if (next == NULL)
2872 break;
2873 expanded_location next_exploc = expand_location (next->location);
2874 if (next_exploc.file != token_exploc.file)
2875 break;
2876 if (next_exploc.line != token_exploc.line)
2877 break;
2879 return;
2883 gcc_rich_location richloc (input_location);
2885 bool added_matching_location = false;
2887 if (missing_token_desc != RT_NONE)
2889 /* Potentially supply a fix-it hint, suggesting to add the
2890 missing token immediately after the *previous* token.
2891 This may move the primary location within richloc. */
2892 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2893 location_t prev_token_loc
2894 = cp_lexer_previous_token (parser->lexer)->location;
2895 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2897 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2898 Attempt to consolidate diagnostics by printing it as a
2899 secondary range within the main diagnostic. */
2900 if (matching_location != UNKNOWN_LOCATION)
2901 added_matching_location
2902 = richloc.add_location_if_nearby (matching_location);
2905 /* Actually emit the error. */
2906 c_parse_error (gmsgid,
2907 /* Because c_parser_error does not understand
2908 CPP_KEYWORD, keywords are treated like
2909 identifiers. */
2910 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2911 token->u.value, token->flags, &richloc);
2913 if (missing_token_desc != RT_NONE)
2915 /* If we weren't able to consolidate matching_location, then
2916 print it as a secondary diagnostic. */
2917 if (matching_location != UNKNOWN_LOCATION
2918 && !added_matching_location)
2919 inform (matching_location, "to match this %qs",
2920 get_matching_symbol (missing_token_desc));
2924 /* If not parsing tentatively, issue a diagnostic of the form
2925 FILE:LINE: MESSAGE before TOKEN
2926 where TOKEN is the next token in the input stream. MESSAGE
2927 (specified by the caller) is usually of the form "expected
2928 OTHER-TOKEN". */
2930 static void
2931 cp_parser_error (cp_parser* parser, const char* gmsgid)
2933 if (!cp_parser_simulate_error (parser))
2934 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2937 /* Issue an error about name-lookup failing. NAME is the
2938 IDENTIFIER_NODE DECL is the result of
2939 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2940 the thing that we hoped to find. */
2942 static void
2943 cp_parser_name_lookup_error (cp_parser* parser,
2944 tree name,
2945 tree decl,
2946 name_lookup_error desired,
2947 location_t location)
2949 /* If name lookup completely failed, tell the user that NAME was not
2950 declared. */
2951 if (decl == error_mark_node)
2953 if (parser->scope && parser->scope != global_namespace)
2954 error_at (location, "%<%E::%E%> has not been declared",
2955 parser->scope, name);
2956 else if (parser->scope == global_namespace)
2957 error_at (location, "%<::%E%> has not been declared", name);
2958 else if (parser->object_scope
2959 && !CLASS_TYPE_P (parser->object_scope))
2960 error_at (location, "request for member %qE in non-class type %qT",
2961 name, parser->object_scope);
2962 else if (parser->object_scope)
2963 error_at (location, "%<%T::%E%> has not been declared",
2964 parser->object_scope, name);
2965 else
2966 error_at (location, "%qE has not been declared", name);
2968 else if (parser->scope && parser->scope != global_namespace)
2970 switch (desired)
2972 case NLE_TYPE:
2973 error_at (location, "%<%E::%E%> is not a type",
2974 parser->scope, name);
2975 break;
2976 case NLE_CXX98:
2977 error_at (location, "%<%E::%E%> is not a class or namespace",
2978 parser->scope, name);
2979 break;
2980 case NLE_NOT_CXX98:
2981 error_at (location,
2982 "%<%E::%E%> is not a class, namespace, or enumeration",
2983 parser->scope, name);
2984 break;
2985 default:
2986 gcc_unreachable ();
2990 else if (parser->scope == global_namespace)
2992 switch (desired)
2994 case NLE_TYPE:
2995 error_at (location, "%<::%E%> is not a type", name);
2996 break;
2997 case NLE_CXX98:
2998 error_at (location, "%<::%E%> is not a class or namespace", name);
2999 break;
3000 case NLE_NOT_CXX98:
3001 error_at (location,
3002 "%<::%E%> is not a class, namespace, or enumeration",
3003 name);
3004 break;
3005 default:
3006 gcc_unreachable ();
3009 else
3011 switch (desired)
3013 case NLE_TYPE:
3014 error_at (location, "%qE is not a type", name);
3015 break;
3016 case NLE_CXX98:
3017 error_at (location, "%qE is not a class or namespace", name);
3018 break;
3019 case NLE_NOT_CXX98:
3020 error_at (location,
3021 "%qE is not a class, namespace, or enumeration", name);
3022 break;
3023 default:
3024 gcc_unreachable ();
3029 /* If we are parsing tentatively, remember that an error has occurred
3030 during this tentative parse. Returns true if the error was
3031 simulated; false if a message should be issued by the caller. */
3033 static bool
3034 cp_parser_simulate_error (cp_parser* parser)
3036 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3038 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3039 return true;
3041 return false;
3044 /* This function is called when a type is defined. If type
3045 definitions are forbidden at this point, an error message is
3046 issued. */
3048 static bool
3049 cp_parser_check_type_definition (cp_parser* parser)
3051 /* If types are forbidden here, issue a message. */
3052 if (parser->type_definition_forbidden_message)
3054 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3055 in the message need to be interpreted. */
3056 error (parser->type_definition_forbidden_message);
3057 return false;
3059 return true;
3062 /* This function is called when the DECLARATOR is processed. The TYPE
3063 was a type defined in the decl-specifiers. If it is invalid to
3064 define a type in the decl-specifiers for DECLARATOR, an error is
3065 issued. TYPE_LOCATION is the location of TYPE and is used
3066 for error reporting. */
3068 static void
3069 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3070 tree type, location_t type_location)
3072 /* [dcl.fct] forbids type definitions in return types.
3073 Unfortunately, it's not easy to know whether or not we are
3074 processing a return type until after the fact. */
3075 while (declarator
3076 && (declarator->kind == cdk_pointer
3077 || declarator->kind == cdk_reference
3078 || declarator->kind == cdk_ptrmem))
3079 declarator = declarator->declarator;
3080 if (declarator
3081 && declarator->kind == cdk_function)
3083 error_at (type_location,
3084 "new types may not be defined in a return type");
3085 inform (type_location,
3086 "(perhaps a semicolon is missing after the definition of %qT)",
3087 type);
3091 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3092 "<" in any valid C++ program. If the next token is indeed "<",
3093 issue a message warning the user about what appears to be an
3094 invalid attempt to form a template-id. LOCATION is the location
3095 of the type-specifier (TYPE) */
3097 static void
3098 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3099 tree type,
3100 enum tag_types tag_type,
3101 location_t location)
3103 cp_token_position start = 0;
3105 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3107 if (TREE_CODE (type) == TYPE_DECL)
3108 type = TREE_TYPE (type);
3109 if (TYPE_P (type) && !template_placeholder_p (type))
3110 error_at (location, "%qT is not a template", type);
3111 else if (identifier_p (type))
3113 if (tag_type != none_type)
3114 error_at (location, "%qE is not a class template", type);
3115 else
3116 error_at (location, "%qE is not a template", type);
3118 else
3119 error_at (location, "invalid template-id");
3120 /* Remember the location of the invalid "<". */
3121 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3122 start = cp_lexer_token_position (parser->lexer, true);
3123 /* Consume the "<". */
3124 cp_lexer_consume_token (parser->lexer);
3125 /* Parse the template arguments. */
3126 cp_parser_enclosed_template_argument_list (parser);
3127 /* Permanently remove the invalid template arguments so that
3128 this error message is not issued again. */
3129 if (start)
3130 cp_lexer_purge_tokens_after (parser->lexer, start);
3134 /* If parsing an integral constant-expression, issue an error message
3135 about the fact that THING appeared and return true. Otherwise,
3136 return false. In either case, set
3137 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3139 static bool
3140 cp_parser_non_integral_constant_expression (cp_parser *parser,
3141 non_integral_constant thing)
3143 parser->non_integral_constant_expression_p = true;
3144 if (parser->integral_constant_expression_p)
3146 if (!parser->allow_non_integral_constant_expression_p)
3148 const char *msg = NULL;
3149 switch (thing)
3151 case NIC_FLOAT:
3152 pedwarn (input_location, OPT_Wpedantic,
3153 "ISO C++ forbids using a floating-point literal "
3154 "in a constant-expression");
3155 return true;
3156 case NIC_CAST:
3157 error ("a cast to a type other than an integral or "
3158 "enumeration type cannot appear in a "
3159 "constant-expression");
3160 return true;
3161 case NIC_TYPEID:
3162 error ("%<typeid%> operator "
3163 "cannot appear in a constant-expression");
3164 return true;
3165 case NIC_NCC:
3166 error ("non-constant compound literals "
3167 "cannot appear in a constant-expression");
3168 return true;
3169 case NIC_FUNC_CALL:
3170 error ("a function call "
3171 "cannot appear in a constant-expression");
3172 return true;
3173 case NIC_INC:
3174 error ("an increment "
3175 "cannot appear in a constant-expression");
3176 return true;
3177 case NIC_DEC:
3178 error ("an decrement "
3179 "cannot appear in a constant-expression");
3180 return true;
3181 case NIC_ARRAY_REF:
3182 error ("an array reference "
3183 "cannot appear in a constant-expression");
3184 return true;
3185 case NIC_ADDR_LABEL:
3186 error ("the address of a label "
3187 "cannot appear in a constant-expression");
3188 return true;
3189 case NIC_OVERLOADED:
3190 error ("calls to overloaded operators "
3191 "cannot appear in a constant-expression");
3192 return true;
3193 case NIC_ASSIGNMENT:
3194 error ("an assignment cannot appear in a constant-expression");
3195 return true;
3196 case NIC_COMMA:
3197 error ("a comma operator "
3198 "cannot appear in a constant-expression");
3199 return true;
3200 case NIC_CONSTRUCTOR:
3201 error ("a call to a constructor "
3202 "cannot appear in a constant-expression");
3203 return true;
3204 case NIC_TRANSACTION:
3205 error ("a transaction expression "
3206 "cannot appear in a constant-expression");
3207 return true;
3208 case NIC_THIS:
3209 msg = "this";
3210 break;
3211 case NIC_FUNC_NAME:
3212 msg = "__FUNCTION__";
3213 break;
3214 case NIC_PRETTY_FUNC:
3215 msg = "__PRETTY_FUNCTION__";
3216 break;
3217 case NIC_C99_FUNC:
3218 msg = "__func__";
3219 break;
3220 case NIC_VA_ARG:
3221 msg = "va_arg";
3222 break;
3223 case NIC_ARROW:
3224 msg = "->";
3225 break;
3226 case NIC_POINT:
3227 msg = ".";
3228 break;
3229 case NIC_STAR:
3230 msg = "*";
3231 break;
3232 case NIC_ADDR:
3233 msg = "&";
3234 break;
3235 case NIC_PREINCREMENT:
3236 msg = "++";
3237 break;
3238 case NIC_PREDECREMENT:
3239 msg = "--";
3240 break;
3241 case NIC_NEW:
3242 msg = "new";
3243 break;
3244 case NIC_DEL:
3245 msg = "delete";
3246 break;
3247 default:
3248 gcc_unreachable ();
3250 if (msg)
3251 error ("%qs cannot appear in a constant-expression", msg);
3252 return true;
3255 return false;
3258 /* Emit a diagnostic for an invalid type name. This function commits
3259 to the current active tentative parse, if any. (Otherwise, the
3260 problematic construct might be encountered again later, resulting
3261 in duplicate error messages.) LOCATION is the location of ID. */
3263 static void
3264 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3265 location_t location)
3267 tree decl, ambiguous_decls;
3268 cp_parser_commit_to_tentative_parse (parser);
3269 /* Try to lookup the identifier. */
3270 decl = cp_parser_lookup_name (parser, id, none_type,
3271 /*is_template=*/false,
3272 /*is_namespace=*/false,
3273 /*check_dependency=*/true,
3274 &ambiguous_decls, location);
3275 if (ambiguous_decls)
3276 /* If the lookup was ambiguous, an error will already have
3277 been issued. */
3278 return;
3279 /* If the lookup found a template-name, it means that the user forgot
3280 to specify an argument list. Emit a useful error message. */
3281 if (DECL_TYPE_TEMPLATE_P (decl))
3283 error_at (location,
3284 "invalid use of template-name %qE without an argument list",
3285 decl);
3286 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3287 inform (location, "class template argument deduction is only available "
3288 "with -std=c++17 or -std=gnu++17");
3289 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3291 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3292 error_at (location, "invalid use of destructor %qD as a type", id);
3293 else if (TREE_CODE (decl) == TYPE_DECL)
3294 /* Something like 'unsigned A a;' */
3295 error_at (location, "invalid combination of multiple type-specifiers");
3296 else if (!parser->scope)
3298 /* Issue an error message. */
3299 name_hint hint;
3300 if (TREE_CODE (id) == IDENTIFIER_NODE)
3301 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3302 if (hint)
3304 gcc_rich_location richloc (location);
3305 richloc.add_fixit_replace (hint.suggestion ());
3306 error_at (&richloc,
3307 "%qE does not name a type; did you mean %qs?",
3308 id, hint.suggestion ());
3310 else
3311 error_at (location, "%qE does not name a type", id);
3312 /* If we're in a template class, it's possible that the user was
3313 referring to a type from a base class. For example:
3315 template <typename T> struct A { typedef T X; };
3316 template <typename T> struct B : public A<T> { X x; };
3318 The user should have said "typename A<T>::X". */
3319 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3320 inform (location, "C++11 %<constexpr%> only available with "
3321 "-std=c++11 or -std=gnu++11");
3322 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3323 inform (location, "C++11 %<noexcept%> only available with "
3324 "-std=c++11 or -std=gnu++11");
3325 else if (cxx_dialect < cxx11
3326 && TREE_CODE (id) == IDENTIFIER_NODE
3327 && id_equal (id, "thread_local"))
3328 inform (location, "C++11 %<thread_local%> only available with "
3329 "-std=c++11 or -std=gnu++11");
3330 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3331 inform (location, "%<concept%> only available with -fconcepts");
3332 else if (processing_template_decl && current_class_type
3333 && TYPE_BINFO (current_class_type))
3335 tree b;
3337 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3339 b = TREE_CHAIN (b))
3341 tree base_type = BINFO_TYPE (b);
3342 if (CLASS_TYPE_P (base_type)
3343 && dependent_type_p (base_type))
3345 tree field;
3346 /* Go from a particular instantiation of the
3347 template (which will have an empty TYPE_FIELDs),
3348 to the main version. */
3349 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3350 for (field = TYPE_FIELDS (base_type);
3351 field;
3352 field = DECL_CHAIN (field))
3353 if (TREE_CODE (field) == TYPE_DECL
3354 && DECL_NAME (field) == id)
3356 inform (location,
3357 "(perhaps %<typename %T::%E%> was intended)",
3358 BINFO_TYPE (b), id);
3359 break;
3361 if (field)
3362 break;
3367 /* Here we diagnose qualified-ids where the scope is actually correct,
3368 but the identifier does not resolve to a valid type name. */
3369 else if (parser->scope != error_mark_node)
3371 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3373 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3374 error_at (location_of (id),
3375 "%qE in namespace %qE does not name a template type",
3376 id, parser->scope);
3377 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3378 error_at (location_of (id),
3379 "%qE in namespace %qE does not name a template type",
3380 TREE_OPERAND (id, 0), parser->scope);
3381 else
3382 error_at (location_of (id),
3383 "%qE in namespace %qE does not name a type",
3384 id, parser->scope);
3385 if (DECL_P (decl))
3386 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3387 else if (decl == error_mark_node)
3388 suggest_alternative_in_explicit_scope (location, id,
3389 parser->scope);
3391 else if (CLASS_TYPE_P (parser->scope)
3392 && constructor_name_p (id, parser->scope))
3394 /* A<T>::A<T>() */
3395 error_at (location, "%<%T::%E%> names the constructor, not"
3396 " the type", parser->scope, id);
3397 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3398 error_at (location, "and %qT has no template constructors",
3399 parser->scope);
3401 else if (TYPE_P (parser->scope)
3402 && dependent_scope_p (parser->scope))
3404 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3405 error_at (location,
3406 "need %<typename%> before %<%T::%D::%E%> because "
3407 "%<%T::%D%> is a dependent scope",
3408 TYPE_CONTEXT (parser->scope),
3409 TYPENAME_TYPE_FULLNAME (parser->scope),
3411 TYPE_CONTEXT (parser->scope),
3412 TYPENAME_TYPE_FULLNAME (parser->scope));
3413 else
3414 error_at (location, "need %<typename%> before %<%T::%E%> because "
3415 "%qT is a dependent scope",
3416 parser->scope, id, parser->scope);
3418 else if (TYPE_P (parser->scope))
3420 if (!COMPLETE_TYPE_P (parser->scope))
3421 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3422 parser->scope);
3423 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3424 error_at (location_of (id),
3425 "%qE in %q#T does not name a template type",
3426 id, parser->scope);
3427 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3428 error_at (location_of (id),
3429 "%qE in %q#T does not name a template type",
3430 TREE_OPERAND (id, 0), parser->scope);
3431 else
3432 error_at (location_of (id),
3433 "%qE in %q#T does not name a type",
3434 id, parser->scope);
3435 if (DECL_P (decl))
3436 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3438 else
3439 gcc_unreachable ();
3443 /* Check for a common situation where a type-name should be present,
3444 but is not, and issue a sensible error message. Returns true if an
3445 invalid type-name was detected.
3447 The situation handled by this function are variable declarations of the
3448 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3449 Usually, `ID' should name a type, but if we got here it means that it
3450 does not. We try to emit the best possible error message depending on
3451 how exactly the id-expression looks like. */
3453 static bool
3454 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3456 tree id;
3457 cp_token *token = cp_lexer_peek_token (parser->lexer);
3459 /* Avoid duplicate error about ambiguous lookup. */
3460 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3462 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3463 if (next->type == CPP_NAME && next->error_reported)
3464 goto out;
3467 cp_parser_parse_tentatively (parser);
3468 id = cp_parser_id_expression (parser,
3469 /*template_keyword_p=*/false,
3470 /*check_dependency_p=*/true,
3471 /*template_p=*/NULL,
3472 /*declarator_p=*/false,
3473 /*optional_p=*/false);
3474 /* If the next token is a (, this is a function with no explicit return
3475 type, i.e. constructor, destructor or conversion op. */
3476 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3477 || TREE_CODE (id) == TYPE_DECL)
3479 cp_parser_abort_tentative_parse (parser);
3480 return false;
3482 if (!cp_parser_parse_definitely (parser))
3483 return false;
3485 /* Emit a diagnostic for the invalid type. */
3486 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3487 out:
3488 /* If we aren't in the middle of a declarator (i.e. in a
3489 parameter-declaration-clause), skip to the end of the declaration;
3490 there's no point in trying to process it. */
3491 if (!parser->in_declarator_p)
3492 cp_parser_skip_to_end_of_block_or_statement (parser);
3493 return true;
3496 /* Consume tokens up to, and including, the next non-nested closing `)'.
3497 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3498 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3499 found an unnested token of that type. */
3501 static int
3502 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3503 bool recovering,
3504 cpp_ttype or_ttype,
3505 bool consume_paren)
3507 unsigned paren_depth = 0;
3508 unsigned brace_depth = 0;
3509 unsigned square_depth = 0;
3510 unsigned condop_depth = 0;
3512 if (recovering && or_ttype == CPP_EOF
3513 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3514 return 0;
3516 while (true)
3518 cp_token * token = cp_lexer_peek_token (parser->lexer);
3520 /* Have we found what we're looking for before the closing paren? */
3521 if (token->type == or_ttype && or_ttype != CPP_EOF
3522 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3523 return -1;
3525 switch (token->type)
3527 case CPP_EOF:
3528 case CPP_PRAGMA_EOL:
3529 /* If we've run out of tokens, then there is no closing `)'. */
3530 return 0;
3532 /* This is good for lambda expression capture-lists. */
3533 case CPP_OPEN_SQUARE:
3534 ++square_depth;
3535 break;
3536 case CPP_CLOSE_SQUARE:
3537 if (!square_depth--)
3538 return 0;
3539 break;
3541 case CPP_SEMICOLON:
3542 /* This matches the processing in skip_to_end_of_statement. */
3543 if (!brace_depth)
3544 return 0;
3545 break;
3547 case CPP_OPEN_BRACE:
3548 ++brace_depth;
3549 break;
3550 case CPP_CLOSE_BRACE:
3551 if (!brace_depth--)
3552 return 0;
3553 break;
3555 case CPP_OPEN_PAREN:
3556 if (!brace_depth)
3557 ++paren_depth;
3558 break;
3560 case CPP_CLOSE_PAREN:
3561 if (!brace_depth && !paren_depth--)
3563 if (consume_paren)
3564 cp_lexer_consume_token (parser->lexer);
3565 return 1;
3567 break;
3569 case CPP_QUERY:
3570 if (!brace_depth && !paren_depth && !square_depth)
3571 ++condop_depth;
3572 break;
3574 case CPP_COLON:
3575 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3576 condop_depth--;
3577 break;
3579 default:
3580 break;
3583 /* Consume the token. */
3584 cp_lexer_consume_token (parser->lexer);
3588 /* Consume tokens up to, and including, the next non-nested closing `)'.
3589 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3590 are doing error recovery. Returns -1 if OR_COMMA is true and we
3591 found an unnested token of that type. */
3593 static int
3594 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3595 bool recovering,
3596 bool or_comma,
3597 bool consume_paren)
3599 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3600 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3601 ttype, consume_paren);
3604 /* Consume tokens until we reach the end of the current statement.
3605 Normally, that will be just before consuming a `;'. However, if a
3606 non-nested `}' comes first, then we stop before consuming that. */
3608 static void
3609 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3611 unsigned nesting_depth = 0;
3613 /* Unwind generic function template scope if necessary. */
3614 if (parser->fully_implicit_function_template_p)
3615 abort_fully_implicit_template (parser);
3617 while (true)
3619 cp_token *token = cp_lexer_peek_token (parser->lexer);
3621 switch (token->type)
3623 case CPP_EOF:
3624 case CPP_PRAGMA_EOL:
3625 /* If we've run out of tokens, stop. */
3626 return;
3628 case CPP_SEMICOLON:
3629 /* If the next token is a `;', we have reached the end of the
3630 statement. */
3631 if (!nesting_depth)
3632 return;
3633 break;
3635 case CPP_CLOSE_BRACE:
3636 /* If this is a non-nested '}', stop before consuming it.
3637 That way, when confronted with something like:
3639 { 3 + }
3641 we stop before consuming the closing '}', even though we
3642 have not yet reached a `;'. */
3643 if (nesting_depth == 0)
3644 return;
3646 /* If it is the closing '}' for a block that we have
3647 scanned, stop -- but only after consuming the token.
3648 That way given:
3650 void f g () { ... }
3651 typedef int I;
3653 we will stop after the body of the erroneously declared
3654 function, but before consuming the following `typedef'
3655 declaration. */
3656 if (--nesting_depth == 0)
3658 cp_lexer_consume_token (parser->lexer);
3659 return;
3661 break;
3663 case CPP_OPEN_BRACE:
3664 ++nesting_depth;
3665 break;
3667 default:
3668 break;
3671 /* Consume the token. */
3672 cp_lexer_consume_token (parser->lexer);
3676 /* This function is called at the end of a statement or declaration.
3677 If the next token is a semicolon, it is consumed; otherwise, error
3678 recovery is attempted. */
3680 static void
3681 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3683 /* Look for the trailing `;'. */
3684 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3686 /* If there is additional (erroneous) input, skip to the end of
3687 the statement. */
3688 cp_parser_skip_to_end_of_statement (parser);
3689 /* If the next token is now a `;', consume it. */
3690 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3691 cp_lexer_consume_token (parser->lexer);
3695 /* Skip tokens until we have consumed an entire block, or until we
3696 have consumed a non-nested `;'. */
3698 static void
3699 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3701 int nesting_depth = 0;
3703 /* Unwind generic function template scope if necessary. */
3704 if (parser->fully_implicit_function_template_p)
3705 abort_fully_implicit_template (parser);
3707 while (nesting_depth >= 0)
3709 cp_token *token = cp_lexer_peek_token (parser->lexer);
3711 switch (token->type)
3713 case CPP_EOF:
3714 case CPP_PRAGMA_EOL:
3715 /* If we've run out of tokens, stop. */
3716 return;
3718 case CPP_SEMICOLON:
3719 /* Stop if this is an unnested ';'. */
3720 if (!nesting_depth)
3721 nesting_depth = -1;
3722 break;
3724 case CPP_CLOSE_BRACE:
3725 /* Stop if this is an unnested '}', or closes the outermost
3726 nesting level. */
3727 nesting_depth--;
3728 if (nesting_depth < 0)
3729 return;
3730 if (!nesting_depth)
3731 nesting_depth = -1;
3732 break;
3734 case CPP_OPEN_BRACE:
3735 /* Nest. */
3736 nesting_depth++;
3737 break;
3739 default:
3740 break;
3743 /* Consume the token. */
3744 cp_lexer_consume_token (parser->lexer);
3748 /* Skip tokens until a non-nested closing curly brace is the next
3749 token, or there are no more tokens. Return true in the first case,
3750 false otherwise. */
3752 static bool
3753 cp_parser_skip_to_closing_brace (cp_parser *parser)
3755 unsigned nesting_depth = 0;
3757 while (true)
3759 cp_token *token = cp_lexer_peek_token (parser->lexer);
3761 switch (token->type)
3763 case CPP_EOF:
3764 case CPP_PRAGMA_EOL:
3765 /* If we've run out of tokens, stop. */
3766 return false;
3768 case CPP_CLOSE_BRACE:
3769 /* If the next token is a non-nested `}', then we have reached
3770 the end of the current block. */
3771 if (nesting_depth-- == 0)
3772 return true;
3773 break;
3775 case CPP_OPEN_BRACE:
3776 /* If it the next token is a `{', then we are entering a new
3777 block. Consume the entire block. */
3778 ++nesting_depth;
3779 break;
3781 default:
3782 break;
3785 /* Consume the token. */
3786 cp_lexer_consume_token (parser->lexer);
3790 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3791 parameter is the PRAGMA token, allowing us to purge the entire pragma
3792 sequence. */
3794 static void
3795 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3797 cp_token *token;
3799 parser->lexer->in_pragma = false;
3802 token = cp_lexer_consume_token (parser->lexer);
3803 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3805 /* Ensure that the pragma is not parsed again. */
3806 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3809 /* Require pragma end of line, resyncing with it as necessary. The
3810 arguments are as for cp_parser_skip_to_pragma_eol. */
3812 static void
3813 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3815 parser->lexer->in_pragma = false;
3816 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3817 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3820 /* This is a simple wrapper around make_typename_type. When the id is
3821 an unresolved identifier node, we can provide a superior diagnostic
3822 using cp_parser_diagnose_invalid_type_name. */
3824 static tree
3825 cp_parser_make_typename_type (cp_parser *parser, tree id,
3826 location_t id_location)
3828 tree result;
3829 if (identifier_p (id))
3831 result = make_typename_type (parser->scope, id, typename_type,
3832 /*complain=*/tf_none);
3833 if (result == error_mark_node)
3834 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3835 return result;
3837 return make_typename_type (parser->scope, id, typename_type, tf_error);
3840 /* This is a wrapper around the
3841 make_{pointer,ptrmem,reference}_declarator functions that decides
3842 which one to call based on the CODE and CLASS_TYPE arguments. The
3843 CODE argument should be one of the values returned by
3844 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3845 appertain to the pointer or reference. */
3847 static cp_declarator *
3848 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3849 cp_cv_quals cv_qualifiers,
3850 cp_declarator *target,
3851 tree attributes)
3853 if (code == ERROR_MARK || target == cp_error_declarator)
3854 return cp_error_declarator;
3856 if (code == INDIRECT_REF)
3857 if (class_type == NULL_TREE)
3858 return make_pointer_declarator (cv_qualifiers, target, attributes);
3859 else
3860 return make_ptrmem_declarator (cv_qualifiers, class_type,
3861 target, attributes);
3862 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3863 return make_reference_declarator (cv_qualifiers, target,
3864 false, attributes);
3865 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3866 return make_reference_declarator (cv_qualifiers, target,
3867 true, attributes);
3868 gcc_unreachable ();
3871 /* Create a new C++ parser. */
3873 static cp_parser *
3874 cp_parser_new (void)
3876 cp_parser *parser;
3877 cp_lexer *lexer;
3878 unsigned i;
3880 /* cp_lexer_new_main is called before doing GC allocation because
3881 cp_lexer_new_main might load a PCH file. */
3882 lexer = cp_lexer_new_main ();
3884 /* Initialize the binops_by_token so that we can get the tree
3885 directly from the token. */
3886 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3887 binops_by_token[binops[i].token_type] = binops[i];
3889 parser = ggc_cleared_alloc<cp_parser> ();
3890 parser->lexer = lexer;
3891 parser->context = cp_parser_context_new (NULL);
3893 /* For now, we always accept GNU extensions. */
3894 parser->allow_gnu_extensions_p = 1;
3896 /* The `>' token is a greater-than operator, not the end of a
3897 template-id. */
3898 parser->greater_than_is_operator_p = true;
3900 parser->default_arg_ok_p = true;
3902 /* We are not parsing a constant-expression. */
3903 parser->integral_constant_expression_p = false;
3904 parser->allow_non_integral_constant_expression_p = false;
3905 parser->non_integral_constant_expression_p = false;
3907 /* Local variable names are not forbidden. */
3908 parser->local_variables_forbidden_p = false;
3910 /* We are not processing an `extern "C"' declaration. */
3911 parser->in_unbraced_linkage_specification_p = false;
3913 /* We are not processing a declarator. */
3914 parser->in_declarator_p = false;
3916 /* We are not processing a template-argument-list. */
3917 parser->in_template_argument_list_p = false;
3919 /* We are not in an iteration statement. */
3920 parser->in_statement = 0;
3922 /* We are not in a switch statement. */
3923 parser->in_switch_statement_p = false;
3925 /* We are not parsing a type-id inside an expression. */
3926 parser->in_type_id_in_expr_p = false;
3928 /* Declarations aren't implicitly extern "C". */
3929 parser->implicit_extern_c = false;
3931 /* String literals should be translated to the execution character set. */
3932 parser->translate_strings_p = true;
3934 /* We are not parsing a function body. */
3935 parser->in_function_body = false;
3937 /* We can correct until told otherwise. */
3938 parser->colon_corrects_to_scope_p = true;
3940 /* The unparsed function queue is empty. */
3941 push_unparsed_function_queues (parser);
3943 /* There are no classes being defined. */
3944 parser->num_classes_being_defined = 0;
3946 /* No template parameters apply. */
3947 parser->num_template_parameter_lists = 0;
3949 /* Special parsing data structures. */
3950 parser->omp_declare_simd = NULL;
3951 parser->oacc_routine = NULL;
3953 /* Not declaring an implicit function template. */
3954 parser->auto_is_implicit_function_template_parm_p = false;
3955 parser->fully_implicit_function_template_p = false;
3956 parser->implicit_template_parms = 0;
3957 parser->implicit_template_scope = 0;
3959 /* Allow constrained-type-specifiers. */
3960 parser->prevent_constrained_type_specifiers = 0;
3962 /* We haven't yet seen an 'extern "C"'. */
3963 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3965 return parser;
3968 /* Create a cp_lexer structure which will emit the tokens in CACHE
3969 and push it onto the parser's lexer stack. This is used for delayed
3970 parsing of in-class method bodies and default arguments, and should
3971 not be confused with tentative parsing. */
3972 static void
3973 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3975 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3976 lexer->next = parser->lexer;
3977 parser->lexer = lexer;
3979 /* Move the current source position to that of the first token in the
3980 new lexer. */
3981 cp_lexer_set_source_position_from_token (lexer->next_token);
3984 /* Pop the top lexer off the parser stack. This is never used for the
3985 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3986 static void
3987 cp_parser_pop_lexer (cp_parser *parser)
3989 cp_lexer *lexer = parser->lexer;
3990 parser->lexer = lexer->next;
3991 cp_lexer_destroy (lexer);
3993 /* Put the current source position back where it was before this
3994 lexer was pushed. */
3995 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3998 /* Lexical conventions [gram.lex] */
4000 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4001 identifier. */
4003 static cp_expr
4004 cp_parser_identifier (cp_parser* parser)
4006 cp_token *token;
4008 /* Look for the identifier. */
4009 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4010 /* Return the value. */
4011 if (token)
4012 return cp_expr (token->u.value, token->location);
4013 else
4014 return error_mark_node;
4017 /* Parse a sequence of adjacent string constants. Returns a
4018 TREE_STRING representing the combined, nul-terminated string
4019 constant. If TRANSLATE is true, translate the string to the
4020 execution character set. If WIDE_OK is true, a wide string is
4021 invalid here.
4023 C++98 [lex.string] says that if a narrow string literal token is
4024 adjacent to a wide string literal token, the behavior is undefined.
4025 However, C99 6.4.5p4 says that this results in a wide string literal.
4026 We follow C99 here, for consistency with the C front end.
4028 This code is largely lifted from lex_string() in c-lex.c.
4030 FUTURE: ObjC++ will need to handle @-strings here. */
4031 static cp_expr
4032 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4033 bool lookup_udlit = true)
4035 tree value;
4036 size_t count;
4037 struct obstack str_ob;
4038 cpp_string str, istr, *strs;
4039 cp_token *tok;
4040 enum cpp_ttype type, curr_type;
4041 int have_suffix_p = 0;
4042 tree string_tree;
4043 tree suffix_id = NULL_TREE;
4044 bool curr_tok_is_userdef_p = false;
4046 tok = cp_lexer_peek_token (parser->lexer);
4047 if (!cp_parser_is_string_literal (tok))
4049 cp_parser_error (parser, "expected string-literal");
4050 return error_mark_node;
4053 location_t loc = tok->location;
4055 if (cpp_userdef_string_p (tok->type))
4057 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4058 curr_type = cpp_userdef_string_remove_type (tok->type);
4059 curr_tok_is_userdef_p = true;
4061 else
4063 string_tree = tok->u.value;
4064 curr_type = tok->type;
4066 type = curr_type;
4068 /* Try to avoid the overhead of creating and destroying an obstack
4069 for the common case of just one string. */
4070 if (!cp_parser_is_string_literal
4071 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4073 cp_lexer_consume_token (parser->lexer);
4075 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4076 str.len = TREE_STRING_LENGTH (string_tree);
4077 count = 1;
4079 if (curr_tok_is_userdef_p)
4081 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4082 have_suffix_p = 1;
4083 curr_type = cpp_userdef_string_remove_type (tok->type);
4085 else
4086 curr_type = tok->type;
4088 strs = &str;
4090 else
4092 location_t last_tok_loc = tok->location;
4093 gcc_obstack_init (&str_ob);
4094 count = 0;
4098 cp_lexer_consume_token (parser->lexer);
4099 count++;
4100 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4101 str.len = TREE_STRING_LENGTH (string_tree);
4103 if (curr_tok_is_userdef_p)
4105 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4106 if (have_suffix_p == 0)
4108 suffix_id = curr_suffix_id;
4109 have_suffix_p = 1;
4111 else if (have_suffix_p == 1
4112 && curr_suffix_id != suffix_id)
4114 error ("inconsistent user-defined literal suffixes"
4115 " %qD and %qD in string literal",
4116 suffix_id, curr_suffix_id);
4117 have_suffix_p = -1;
4119 curr_type = cpp_userdef_string_remove_type (tok->type);
4121 else
4122 curr_type = tok->type;
4124 if (type != curr_type)
4126 if (type == CPP_STRING)
4127 type = curr_type;
4128 else if (curr_type != CPP_STRING)
4130 rich_location rich_loc (line_table, tok->location);
4131 rich_loc.add_range (last_tok_loc, false);
4132 error_at (&rich_loc,
4133 "unsupported non-standard concatenation "
4134 "of string literals");
4138 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4140 last_tok_loc = tok->location;
4142 tok = cp_lexer_peek_token (parser->lexer);
4143 if (cpp_userdef_string_p (tok->type))
4145 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4146 curr_type = cpp_userdef_string_remove_type (tok->type);
4147 curr_tok_is_userdef_p = true;
4149 else
4151 string_tree = tok->u.value;
4152 curr_type = tok->type;
4153 curr_tok_is_userdef_p = false;
4156 while (cp_parser_is_string_literal (tok));
4158 /* A string literal built by concatenation has its caret=start at
4159 the start of the initial string, and its finish at the finish of
4160 the final string literal. */
4161 loc = make_location (loc, loc, get_finish (last_tok_loc));
4163 strs = (cpp_string *) obstack_finish (&str_ob);
4166 if (type != CPP_STRING && !wide_ok)
4168 cp_parser_error (parser, "a wide string is invalid in this context");
4169 type = CPP_STRING;
4172 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4173 (parse_in, strs, count, &istr, type))
4175 value = build_string (istr.len, (const char *)istr.text);
4176 free (CONST_CAST (unsigned char *, istr.text));
4178 switch (type)
4180 default:
4181 case CPP_STRING:
4182 case CPP_UTF8STRING:
4183 TREE_TYPE (value) = char_array_type_node;
4184 break;
4185 case CPP_STRING16:
4186 TREE_TYPE (value) = char16_array_type_node;
4187 break;
4188 case CPP_STRING32:
4189 TREE_TYPE (value) = char32_array_type_node;
4190 break;
4191 case CPP_WSTRING:
4192 TREE_TYPE (value) = wchar_array_type_node;
4193 break;
4196 value = fix_string_type (value);
4198 if (have_suffix_p)
4200 tree literal = build_userdef_literal (suffix_id, value,
4201 OT_NONE, NULL_TREE);
4202 if (lookup_udlit)
4203 value = cp_parser_userdef_string_literal (literal);
4204 else
4205 value = literal;
4208 else
4209 /* cpp_interpret_string has issued an error. */
4210 value = error_mark_node;
4212 if (count > 1)
4213 obstack_free (&str_ob, 0);
4215 return cp_expr (value, loc);
4218 /* Look up a literal operator with the name and the exact arguments. */
4220 static tree
4221 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4223 tree decl;
4224 decl = lookup_name (name);
4225 if (!decl || !is_overloaded_fn (decl))
4226 return error_mark_node;
4228 for (lkp_iterator iter (decl); iter; ++iter)
4230 unsigned int ix;
4231 bool found = true;
4232 tree fn = *iter;
4233 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4234 if (parmtypes != NULL_TREE)
4236 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4237 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4239 tree tparm = TREE_VALUE (parmtypes);
4240 tree targ = TREE_TYPE ((*args)[ix]);
4241 bool ptr = TYPE_PTR_P (tparm);
4242 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4243 if ((ptr || arr || !same_type_p (tparm, targ))
4244 && (!ptr || !arr
4245 || !same_type_p (TREE_TYPE (tparm),
4246 TREE_TYPE (targ))))
4247 found = false;
4249 if (found
4250 && ix == vec_safe_length (args)
4251 /* May be this should be sufficient_parms_p instead,
4252 depending on how exactly should user-defined literals
4253 work in presence of default arguments on the literal
4254 operator parameters. */
4255 && parmtypes == void_list_node)
4256 return decl;
4260 return error_mark_node;
4263 /* Parse a user-defined char constant. Returns a call to a user-defined
4264 literal operator taking the character as an argument. */
4266 static cp_expr
4267 cp_parser_userdef_char_literal (cp_parser *parser)
4269 cp_token *token = cp_lexer_consume_token (parser->lexer);
4270 tree literal = token->u.value;
4271 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4272 tree value = USERDEF_LITERAL_VALUE (literal);
4273 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4274 tree decl, result;
4276 /* Build up a call to the user-defined operator */
4277 /* Lookup the name we got back from the id-expression. */
4278 vec<tree, va_gc> *args = make_tree_vector ();
4279 vec_safe_push (args, value);
4280 decl = lookup_literal_operator (name, args);
4281 if (!decl || decl == error_mark_node)
4283 error ("unable to find character literal operator %qD with %qT argument",
4284 name, TREE_TYPE (value));
4285 release_tree_vector (args);
4286 return error_mark_node;
4288 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4289 release_tree_vector (args);
4290 return result;
4293 /* A subroutine of cp_parser_userdef_numeric_literal to
4294 create a char... template parameter pack from a string node. */
4296 static tree
4297 make_char_string_pack (tree value)
4299 tree charvec;
4300 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4301 const char *str = TREE_STRING_POINTER (value);
4302 int i, len = TREE_STRING_LENGTH (value) - 1;
4303 tree argvec = make_tree_vec (1);
4305 /* Fill in CHARVEC with all of the parameters. */
4306 charvec = make_tree_vec (len);
4307 for (i = 0; i < len; ++i)
4309 unsigned char s[3] = { '\'', str[i], '\'' };
4310 cpp_string in = { 3, s };
4311 cpp_string out = { 0, 0 };
4312 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4313 return NULL_TREE;
4314 gcc_assert (out.len == 2);
4315 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4316 out.text[0]);
4319 /* Build the argument packs. */
4320 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4322 TREE_VEC_ELT (argvec, 0) = argpack;
4324 return argvec;
4327 /* A subroutine of cp_parser_userdef_numeric_literal to
4328 create a char... template parameter pack from a string node. */
4330 static tree
4331 make_string_pack (tree value)
4333 tree charvec;
4334 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4335 const unsigned char *str
4336 = (const unsigned char *) TREE_STRING_POINTER (value);
4337 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4338 int len = TREE_STRING_LENGTH (value) / sz - 1;
4339 tree argvec = make_tree_vec (2);
4341 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4342 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4344 /* First template parm is character type. */
4345 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4347 /* Fill in CHARVEC with all of the parameters. */
4348 charvec = make_tree_vec (len);
4349 for (int i = 0; i < len; ++i)
4350 TREE_VEC_ELT (charvec, i)
4351 = double_int_to_tree (str_char_type_node,
4352 double_int::from_buffer (str + i * sz, sz));
4354 /* Build the argument packs. */
4355 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4357 TREE_VEC_ELT (argvec, 1) = argpack;
4359 return argvec;
4362 /* Parse a user-defined numeric constant. returns a call to a user-defined
4363 literal operator. */
4365 static cp_expr
4366 cp_parser_userdef_numeric_literal (cp_parser *parser)
4368 cp_token *token = cp_lexer_consume_token (parser->lexer);
4369 tree literal = token->u.value;
4370 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4371 tree value = USERDEF_LITERAL_VALUE (literal);
4372 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4373 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4374 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4375 tree decl, result;
4376 vec<tree, va_gc> *args;
4378 /* Look for a literal operator taking the exact type of numeric argument
4379 as the literal value. */
4380 args = make_tree_vector ();
4381 vec_safe_push (args, value);
4382 decl = lookup_literal_operator (name, args);
4383 if (decl && decl != error_mark_node)
4385 result = finish_call_expr (decl, &args, false, true,
4386 tf_warning_or_error);
4388 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4390 warning_at (token->location, OPT_Woverflow,
4391 "integer literal exceeds range of %qT type",
4392 long_long_unsigned_type_node);
4394 else
4396 if (overflow > 0)
4397 warning_at (token->location, OPT_Woverflow,
4398 "floating literal exceeds range of %qT type",
4399 long_double_type_node);
4400 else if (overflow < 0)
4401 warning_at (token->location, OPT_Woverflow,
4402 "floating literal truncated to zero");
4405 release_tree_vector (args);
4406 return result;
4408 release_tree_vector (args);
4410 /* If the numeric argument didn't work, look for a raw literal
4411 operator taking a const char* argument consisting of the number
4412 in string format. */
4413 args = make_tree_vector ();
4414 vec_safe_push (args, num_string);
4415 decl = lookup_literal_operator (name, args);
4416 if (decl && decl != error_mark_node)
4418 result = finish_call_expr (decl, &args, false, true,
4419 tf_warning_or_error);
4420 release_tree_vector (args);
4421 return result;
4423 release_tree_vector (args);
4425 /* If the raw literal didn't work, look for a non-type template
4426 function with parameter pack char.... Call the function with
4427 template parameter characters representing the number. */
4428 args = make_tree_vector ();
4429 decl = lookup_literal_operator (name, args);
4430 if (decl && decl != error_mark_node)
4432 tree tmpl_args = make_char_string_pack (num_string);
4433 if (tmpl_args == NULL_TREE)
4435 error ("failed to translate literal to execution character set %qT",
4436 num_string);
4437 return error_mark_node;
4439 decl = lookup_template_function (decl, tmpl_args);
4440 result = finish_call_expr (decl, &args, false, true,
4441 tf_warning_or_error);
4442 release_tree_vector (args);
4443 return result;
4446 release_tree_vector (args);
4448 /* In C++14 the standard library defines complex number suffixes that
4449 conflict with GNU extensions. Prefer them if <complex> is #included. */
4450 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4451 bool i14 = (cxx_dialect > cxx11
4452 && (id_equal (suffix_id, "i")
4453 || id_equal (suffix_id, "if")
4454 || id_equal (suffix_id, "il")));
4455 diagnostic_t kind = DK_ERROR;
4456 int opt = 0;
4458 if (i14 && ext)
4460 tree cxlit = lookup_qualified_name (std_node,
4461 get_identifier ("complex_literals"),
4462 0, false, false);
4463 if (cxlit == error_mark_node)
4465 /* No <complex>, so pedwarn and use GNU semantics. */
4466 kind = DK_PEDWARN;
4467 opt = OPT_Wpedantic;
4471 bool complained
4472 = emit_diagnostic (kind, input_location, opt,
4473 "unable to find numeric literal operator %qD", name);
4475 if (!complained)
4476 /* Don't inform either. */;
4477 else if (i14)
4479 inform (token->location, "add %<using namespace std::complex_literals%> "
4480 "(from <complex>) to enable the C++14 user-defined literal "
4481 "suffixes");
4482 if (ext)
4483 inform (token->location, "or use %<j%> instead of %<i%> for the "
4484 "GNU built-in suffix");
4486 else if (!ext)
4487 inform (token->location, "use -fext-numeric-literals "
4488 "to enable more built-in suffixes");
4490 if (kind == DK_ERROR)
4491 value = error_mark_node;
4492 else
4494 /* Use the built-in semantics. */
4495 tree type;
4496 if (id_equal (suffix_id, "i"))
4498 if (TREE_CODE (value) == INTEGER_CST)
4499 type = integer_type_node;
4500 else
4501 type = double_type_node;
4503 else if (id_equal (suffix_id, "if"))
4504 type = float_type_node;
4505 else /* if (id_equal (suffix_id, "il")) */
4506 type = long_double_type_node;
4508 value = build_complex (build_complex_type (type),
4509 fold_convert (type, integer_zero_node),
4510 fold_convert (type, value));
4513 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4514 /* Avoid repeated diagnostics. */
4515 token->u.value = value;
4516 return value;
4519 /* Parse a user-defined string constant. Returns a call to a user-defined
4520 literal operator taking a character pointer and the length of the string
4521 as arguments. */
4523 static tree
4524 cp_parser_userdef_string_literal (tree literal)
4526 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4527 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4528 tree value = USERDEF_LITERAL_VALUE (literal);
4529 int len = TREE_STRING_LENGTH (value)
4530 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4531 tree decl, result;
4532 vec<tree, va_gc> *args;
4534 /* Build up a call to the user-defined operator. */
4535 /* Lookup the name we got back from the id-expression. */
4536 args = make_tree_vector ();
4537 vec_safe_push (args, value);
4538 vec_safe_push (args, build_int_cst (size_type_node, len));
4539 decl = lookup_literal_operator (name, args);
4541 if (decl && decl != error_mark_node)
4543 result = finish_call_expr (decl, &args, false, true,
4544 tf_warning_or_error);
4545 release_tree_vector (args);
4546 return result;
4548 release_tree_vector (args);
4550 /* Look for a template function with typename parameter CharT
4551 and parameter pack CharT... Call the function with
4552 template parameter characters representing the string. */
4553 args = make_tree_vector ();
4554 decl = lookup_literal_operator (name, args);
4555 if (decl && decl != error_mark_node)
4557 tree tmpl_args = make_string_pack (value);
4558 decl = lookup_template_function (decl, tmpl_args);
4559 result = finish_call_expr (decl, &args, false, true,
4560 tf_warning_or_error);
4561 release_tree_vector (args);
4562 return result;
4564 release_tree_vector (args);
4566 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4567 name, TREE_TYPE (value), size_type_node);
4568 return error_mark_node;
4572 /* Basic concepts [gram.basic] */
4574 /* Parse a translation-unit.
4576 translation-unit:
4577 declaration-seq [opt]
4579 Returns TRUE if all went well. */
4581 static bool
4582 cp_parser_translation_unit (cp_parser* parser)
4584 /* The address of the first non-permanent object on the declarator
4585 obstack. */
4586 static void *declarator_obstack_base;
4588 bool success;
4590 /* Create the declarator obstack, if necessary. */
4591 if (!cp_error_declarator)
4593 gcc_obstack_init (&declarator_obstack);
4594 /* Create the error declarator. */
4595 cp_error_declarator = make_declarator (cdk_error);
4596 /* Create the empty parameter list. */
4597 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4598 UNKNOWN_LOCATION);
4599 /* Remember where the base of the declarator obstack lies. */
4600 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4603 cp_parser_declaration_seq_opt (parser);
4605 /* If there are no tokens left then all went well. */
4606 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4608 /* Get rid of the token array; we don't need it any more. */
4609 cp_lexer_destroy (parser->lexer);
4610 parser->lexer = NULL;
4612 /* This file might have been a context that's implicitly extern
4613 "C". If so, pop the lang context. (Only relevant for PCH.) */
4614 if (parser->implicit_extern_c)
4616 pop_lang_context ();
4617 parser->implicit_extern_c = false;
4620 /* Finish up. */
4621 finish_translation_unit ();
4623 success = true;
4625 else
4627 cp_parser_error (parser, "expected declaration");
4628 success = false;
4631 /* Make sure the declarator obstack was fully cleaned up. */
4632 gcc_assert (obstack_next_free (&declarator_obstack)
4633 == declarator_obstack_base);
4635 /* All went well. */
4636 return success;
4639 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4640 decltype context. */
4642 static inline tsubst_flags_t
4643 complain_flags (bool decltype_p)
4645 tsubst_flags_t complain = tf_warning_or_error;
4646 if (decltype_p)
4647 complain |= tf_decltype;
4648 return complain;
4651 /* We're about to parse a collection of statements. If we're currently
4652 parsing tentatively, set up a firewall so that any nested
4653 cp_parser_commit_to_tentative_parse won't affect the current context. */
4655 static cp_token_position
4656 cp_parser_start_tentative_firewall (cp_parser *parser)
4658 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4659 return 0;
4661 cp_parser_parse_tentatively (parser);
4662 cp_parser_commit_to_topmost_tentative_parse (parser);
4663 return cp_lexer_token_position (parser->lexer, false);
4666 /* We've finished parsing the collection of statements. Wrap up the
4667 firewall and replace the relevant tokens with the parsed form. */
4669 static void
4670 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4671 tree expr)
4673 if (!start)
4674 return;
4676 /* Finish the firewall level. */
4677 cp_parser_parse_definitely (parser);
4678 /* And remember the result of the parse for when we try again. */
4679 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4680 token->type = CPP_PREPARSED_EXPR;
4681 token->u.value = expr;
4682 token->keyword = RID_MAX;
4683 cp_lexer_purge_tokens_after (parser->lexer, start);
4686 /* Like the above functions, but let the user modify the tokens. Used by
4687 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4688 later parses, so it makes sense to localize the effects of
4689 cp_parser_commit_to_tentative_parse. */
4691 struct tentative_firewall
4693 cp_parser *parser;
4694 bool set;
4696 tentative_firewall (cp_parser *p): parser(p)
4698 /* If we're currently parsing tentatively, start a committed level as a
4699 firewall and then an inner tentative parse. */
4700 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4702 cp_parser_parse_tentatively (parser);
4703 cp_parser_commit_to_topmost_tentative_parse (parser);
4704 cp_parser_parse_tentatively (parser);
4708 ~tentative_firewall()
4710 if (set)
4712 /* Finish the inner tentative parse and the firewall, propagating any
4713 uncommitted error state to the outer tentative parse. */
4714 bool err = cp_parser_error_occurred (parser);
4715 cp_parser_parse_definitely (parser);
4716 cp_parser_parse_definitely (parser);
4717 if (err)
4718 cp_parser_simulate_error (parser);
4723 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4724 This class is for tracking such a matching pair of symbols.
4725 In particular, it tracks the location of the first token,
4726 so that if the second token is missing, we can highlight the
4727 location of the first token when notifying the user about the
4728 problem. */
4730 template <typename traits_t>
4731 class token_pair
4733 public:
4734 /* token_pair's ctor. */
4735 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4737 /* If the next token is the opening symbol for this pair, consume it and
4738 return true.
4739 Otherwise, issue an error and return false.
4740 In either case, record the location of the opening token. */
4742 bool require_open (cp_parser *parser)
4744 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4745 return cp_parser_require (parser, traits_t::open_token_type,
4746 traits_t::required_token_open);
4749 /* Consume the next token from PARSER, recording its location as
4750 that of the opening token within the pair. */
4752 cp_token * consume_open (cp_parser *parser)
4754 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4755 gcc_assert (tok->type == traits_t::open_token_type);
4756 m_open_loc = tok->location;
4757 return tok;
4760 /* If the next token is the closing symbol for this pair, consume it
4761 and return it.
4762 Otherwise, issue an error, highlighting the location of the
4763 corresponding opening token, and return NULL. */
4765 cp_token *require_close (cp_parser *parser) const
4767 return cp_parser_require (parser, traits_t::close_token_type,
4768 traits_t::required_token_close,
4769 m_open_loc);
4772 private:
4773 location_t m_open_loc;
4776 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4778 struct matching_paren_traits
4780 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4781 static const enum required_token required_token_open = RT_OPEN_PAREN;
4782 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4783 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4786 /* "matching_parens" is a token_pair<T> class for tracking matching
4787 pairs of parentheses. */
4789 typedef token_pair<matching_paren_traits> matching_parens;
4791 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4793 struct matching_brace_traits
4795 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4796 static const enum required_token required_token_open = RT_OPEN_BRACE;
4797 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4798 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4801 /* "matching_braces" is a token_pair<T> class for tracking matching
4802 pairs of braces. */
4804 typedef token_pair<matching_brace_traits> matching_braces;
4807 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4808 enclosing parentheses. */
4810 static cp_expr
4811 cp_parser_statement_expr (cp_parser *parser)
4813 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4815 /* Consume the '('. */
4816 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4817 matching_parens parens;
4818 parens.consume_open (parser);
4819 /* Start the statement-expression. */
4820 tree expr = begin_stmt_expr ();
4821 /* Parse the compound-statement. */
4822 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4823 /* Finish up. */
4824 expr = finish_stmt_expr (expr, false);
4825 /* Consume the ')'. */
4826 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4827 if (!parens.require_close (parser))
4828 cp_parser_skip_to_end_of_statement (parser);
4830 cp_parser_end_tentative_firewall (parser, start, expr);
4831 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4832 return cp_expr (expr, combined_loc);
4835 /* Expressions [gram.expr] */
4837 /* Parse a fold-operator.
4839 fold-operator:
4840 - * / % ^ & | = < > << >>
4841 = -= *= /= %= ^= &= |= <<= >>=
4842 == != <= >= && || , .* ->*
4844 This returns the tree code corresponding to the matched operator
4845 as an int. When the current token matches a compound assignment
4846 opertor, the resulting tree code is the negative value of the
4847 non-assignment operator. */
4849 static int
4850 cp_parser_fold_operator (cp_token *token)
4852 switch (token->type)
4854 case CPP_PLUS: return PLUS_EXPR;
4855 case CPP_MINUS: return MINUS_EXPR;
4856 case CPP_MULT: return MULT_EXPR;
4857 case CPP_DIV: return TRUNC_DIV_EXPR;
4858 case CPP_MOD: return TRUNC_MOD_EXPR;
4859 case CPP_XOR: return BIT_XOR_EXPR;
4860 case CPP_AND: return BIT_AND_EXPR;
4861 case CPP_OR: return BIT_IOR_EXPR;
4862 case CPP_LSHIFT: return LSHIFT_EXPR;
4863 case CPP_RSHIFT: return RSHIFT_EXPR;
4865 case CPP_EQ: return -NOP_EXPR;
4866 case CPP_PLUS_EQ: return -PLUS_EXPR;
4867 case CPP_MINUS_EQ: return -MINUS_EXPR;
4868 case CPP_MULT_EQ: return -MULT_EXPR;
4869 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4870 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4871 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4872 case CPP_AND_EQ: return -BIT_AND_EXPR;
4873 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4874 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4875 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4877 case CPP_EQ_EQ: return EQ_EXPR;
4878 case CPP_NOT_EQ: return NE_EXPR;
4879 case CPP_LESS: return LT_EXPR;
4880 case CPP_GREATER: return GT_EXPR;
4881 case CPP_LESS_EQ: return LE_EXPR;
4882 case CPP_GREATER_EQ: return GE_EXPR;
4884 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4885 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4887 case CPP_COMMA: return COMPOUND_EXPR;
4889 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4890 case CPP_DEREF_STAR: return MEMBER_REF;
4892 default: return ERROR_MARK;
4896 /* Returns true if CODE indicates a binary expression, which is not allowed in
4897 the LHS of a fold-expression. More codes will need to be added to use this
4898 function in other contexts. */
4900 static bool
4901 is_binary_op (tree_code code)
4903 switch (code)
4905 case PLUS_EXPR:
4906 case POINTER_PLUS_EXPR:
4907 case MINUS_EXPR:
4908 case MULT_EXPR:
4909 case TRUNC_DIV_EXPR:
4910 case TRUNC_MOD_EXPR:
4911 case BIT_XOR_EXPR:
4912 case BIT_AND_EXPR:
4913 case BIT_IOR_EXPR:
4914 case LSHIFT_EXPR:
4915 case RSHIFT_EXPR:
4917 case MODOP_EXPR:
4919 case EQ_EXPR:
4920 case NE_EXPR:
4921 case LE_EXPR:
4922 case GE_EXPR:
4923 case LT_EXPR:
4924 case GT_EXPR:
4926 case TRUTH_ANDIF_EXPR:
4927 case TRUTH_ORIF_EXPR:
4929 case COMPOUND_EXPR:
4931 case DOTSTAR_EXPR:
4932 case MEMBER_REF:
4933 return true;
4935 default:
4936 return false;
4940 /* If the next token is a suitable fold operator, consume it and return as
4941 the function above. */
4943 static int
4944 cp_parser_fold_operator (cp_parser *parser)
4946 cp_token* token = cp_lexer_peek_token (parser->lexer);
4947 int code = cp_parser_fold_operator (token);
4948 if (code != ERROR_MARK)
4949 cp_lexer_consume_token (parser->lexer);
4950 return code;
4953 /* Parse a fold-expression.
4955 fold-expression:
4956 ( ... folding-operator cast-expression)
4957 ( cast-expression folding-operator ... )
4958 ( cast-expression folding operator ... folding-operator cast-expression)
4960 Note that the '(' and ')' are matched in primary expression. */
4962 static cp_expr
4963 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4965 cp_id_kind pidk;
4967 // Left fold.
4968 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4970 cp_lexer_consume_token (parser->lexer);
4971 int op = cp_parser_fold_operator (parser);
4972 if (op == ERROR_MARK)
4974 cp_parser_error (parser, "expected binary operator");
4975 return error_mark_node;
4978 tree expr = cp_parser_cast_expression (parser, false, false,
4979 false, &pidk);
4980 if (expr == error_mark_node)
4981 return error_mark_node;
4982 return finish_left_unary_fold_expr (expr, op);
4985 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4986 int op = cp_parser_fold_operator (parser);
4987 if (op == ERROR_MARK)
4989 cp_parser_error (parser, "expected binary operator");
4990 return error_mark_node;
4993 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4995 cp_parser_error (parser, "expected ...");
4996 return error_mark_node;
4998 cp_lexer_consume_token (parser->lexer);
5000 /* The operands of a fold-expression are cast-expressions, so binary or
5001 conditional expressions are not allowed. We check this here to avoid
5002 tentative parsing. */
5003 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5004 /* OK, the expression was parenthesized. */;
5005 else if (is_binary_op (TREE_CODE (expr1)))
5006 error_at (location_of (expr1),
5007 "binary expression in operand of fold-expression");
5008 else if (TREE_CODE (expr1) == COND_EXPR
5009 || (REFERENCE_REF_P (expr1)
5010 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5011 error_at (location_of (expr1),
5012 "conditional expression in operand of fold-expression");
5014 // Right fold.
5015 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5016 return finish_right_unary_fold_expr (expr1, op);
5018 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5020 cp_parser_error (parser, "mismatched operator in fold-expression");
5021 return error_mark_node;
5023 cp_lexer_consume_token (parser->lexer);
5025 // Binary left or right fold.
5026 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5027 if (expr2 == error_mark_node)
5028 return error_mark_node;
5029 return finish_binary_fold_expr (expr1, expr2, op);
5032 /* Parse a primary-expression.
5034 primary-expression:
5035 literal
5036 this
5037 ( expression )
5038 id-expression
5039 lambda-expression (C++11)
5041 GNU Extensions:
5043 primary-expression:
5044 ( compound-statement )
5045 __builtin_va_arg ( assignment-expression , type-id )
5046 __builtin_offsetof ( type-id , offsetof-expression )
5048 C++ Extensions:
5049 __has_nothrow_assign ( type-id )
5050 __has_nothrow_constructor ( type-id )
5051 __has_nothrow_copy ( type-id )
5052 __has_trivial_assign ( type-id )
5053 __has_trivial_constructor ( type-id )
5054 __has_trivial_copy ( type-id )
5055 __has_trivial_destructor ( type-id )
5056 __has_virtual_destructor ( type-id )
5057 __is_abstract ( type-id )
5058 __is_base_of ( type-id , type-id )
5059 __is_class ( type-id )
5060 __is_empty ( type-id )
5061 __is_enum ( type-id )
5062 __is_final ( type-id )
5063 __is_literal_type ( type-id )
5064 __is_pod ( type-id )
5065 __is_polymorphic ( type-id )
5066 __is_std_layout ( type-id )
5067 __is_trivial ( type-id )
5068 __is_union ( type-id )
5070 Objective-C++ Extension:
5072 primary-expression:
5073 objc-expression
5075 literal:
5076 __null
5078 ADDRESS_P is true iff this expression was immediately preceded by
5079 "&" and therefore might denote a pointer-to-member. CAST_P is true
5080 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5081 true iff this expression is a template argument.
5083 Returns a representation of the expression. Upon return, *IDK
5084 indicates what kind of id-expression (if any) was present. */
5086 static cp_expr
5087 cp_parser_primary_expression (cp_parser *parser,
5088 bool address_p,
5089 bool cast_p,
5090 bool template_arg_p,
5091 bool decltype_p,
5092 cp_id_kind *idk)
5094 cp_token *token = NULL;
5096 /* Assume the primary expression is not an id-expression. */
5097 *idk = CP_ID_KIND_NONE;
5099 /* Peek at the next token. */
5100 token = cp_lexer_peek_token (parser->lexer);
5101 switch ((int) token->type)
5103 /* literal:
5104 integer-literal
5105 character-literal
5106 floating-literal
5107 string-literal
5108 boolean-literal
5109 pointer-literal
5110 user-defined-literal */
5111 case CPP_CHAR:
5112 case CPP_CHAR16:
5113 case CPP_CHAR32:
5114 case CPP_WCHAR:
5115 case CPP_UTF8CHAR:
5116 case CPP_NUMBER:
5117 case CPP_PREPARSED_EXPR:
5118 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5119 return cp_parser_userdef_numeric_literal (parser);
5120 token = cp_lexer_consume_token (parser->lexer);
5121 if (TREE_CODE (token->u.value) == FIXED_CST)
5123 error_at (token->location,
5124 "fixed-point types not supported in C++");
5125 return error_mark_node;
5127 /* Floating-point literals are only allowed in an integral
5128 constant expression if they are cast to an integral or
5129 enumeration type. */
5130 if (TREE_CODE (token->u.value) == REAL_CST
5131 && parser->integral_constant_expression_p
5132 && pedantic)
5134 /* CAST_P will be set even in invalid code like "int(2.7 +
5135 ...)". Therefore, we have to check that the next token
5136 is sure to end the cast. */
5137 if (cast_p)
5139 cp_token *next_token;
5141 next_token = cp_lexer_peek_token (parser->lexer);
5142 if (/* The comma at the end of an
5143 enumerator-definition. */
5144 next_token->type != CPP_COMMA
5145 /* The curly brace at the end of an enum-specifier. */
5146 && next_token->type != CPP_CLOSE_BRACE
5147 /* The end of a statement. */
5148 && next_token->type != CPP_SEMICOLON
5149 /* The end of the cast-expression. */
5150 && next_token->type != CPP_CLOSE_PAREN
5151 /* The end of an array bound. */
5152 && next_token->type != CPP_CLOSE_SQUARE
5153 /* The closing ">" in a template-argument-list. */
5154 && (next_token->type != CPP_GREATER
5155 || parser->greater_than_is_operator_p)
5156 /* C++0x only: A ">>" treated like two ">" tokens,
5157 in a template-argument-list. */
5158 && (next_token->type != CPP_RSHIFT
5159 || (cxx_dialect == cxx98)
5160 || parser->greater_than_is_operator_p))
5161 cast_p = false;
5164 /* If we are within a cast, then the constraint that the
5165 cast is to an integral or enumeration type will be
5166 checked at that point. If we are not within a cast, then
5167 this code is invalid. */
5168 if (!cast_p)
5169 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5171 return cp_expr (token->u.value, token->location);
5173 case CPP_CHAR_USERDEF:
5174 case CPP_CHAR16_USERDEF:
5175 case CPP_CHAR32_USERDEF:
5176 case CPP_WCHAR_USERDEF:
5177 case CPP_UTF8CHAR_USERDEF:
5178 return cp_parser_userdef_char_literal (parser);
5180 case CPP_STRING:
5181 case CPP_STRING16:
5182 case CPP_STRING32:
5183 case CPP_WSTRING:
5184 case CPP_UTF8STRING:
5185 case CPP_STRING_USERDEF:
5186 case CPP_STRING16_USERDEF:
5187 case CPP_STRING32_USERDEF:
5188 case CPP_WSTRING_USERDEF:
5189 case CPP_UTF8STRING_USERDEF:
5190 /* ??? Should wide strings be allowed when parser->translate_strings_p
5191 is false (i.e. in attributes)? If not, we can kill the third
5192 argument to cp_parser_string_literal. */
5193 return cp_parser_string_literal (parser,
5194 parser->translate_strings_p,
5195 true);
5197 case CPP_OPEN_PAREN:
5198 /* If we see `( { ' then we are looking at the beginning of
5199 a GNU statement-expression. */
5200 if (cp_parser_allow_gnu_extensions_p (parser)
5201 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5203 /* Statement-expressions are not allowed by the standard. */
5204 pedwarn (token->location, OPT_Wpedantic,
5205 "ISO C++ forbids braced-groups within expressions");
5207 /* And they're not allowed outside of a function-body; you
5208 cannot, for example, write:
5210 int i = ({ int j = 3; j + 1; });
5212 at class or namespace scope. */
5213 if (!parser->in_function_body
5214 || parser->in_template_argument_list_p)
5216 error_at (token->location,
5217 "statement-expressions are not allowed outside "
5218 "functions nor in template-argument lists");
5219 cp_parser_skip_to_end_of_block_or_statement (parser);
5220 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5221 cp_lexer_consume_token (parser->lexer);
5222 return error_mark_node;
5224 else
5225 return cp_parser_statement_expr (parser);
5227 /* Otherwise it's a normal parenthesized expression. */
5229 cp_expr expr;
5230 bool saved_greater_than_is_operator_p;
5232 location_t open_paren_loc = token->location;
5234 /* Consume the `('. */
5235 matching_parens parens;
5236 parens.consume_open (parser);
5237 /* Within a parenthesized expression, a `>' token is always
5238 the greater-than operator. */
5239 saved_greater_than_is_operator_p
5240 = parser->greater_than_is_operator_p;
5241 parser->greater_than_is_operator_p = true;
5243 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5244 /* Left fold expression. */
5245 expr = NULL_TREE;
5246 else
5247 /* Parse the parenthesized expression. */
5248 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5250 token = cp_lexer_peek_token (parser->lexer);
5251 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5253 expr = cp_parser_fold_expression (parser, expr);
5254 if (expr != error_mark_node
5255 && cxx_dialect < cxx17
5256 && !in_system_header_at (input_location))
5257 pedwarn (input_location, 0, "fold-expressions only available "
5258 "with -std=c++17 or -std=gnu++17");
5260 else
5261 /* Let the front end know that this expression was
5262 enclosed in parentheses. This matters in case, for
5263 example, the expression is of the form `A::B', since
5264 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5265 not. */
5266 expr = finish_parenthesized_expr (expr);
5268 /* DR 705: Wrapping an unqualified name in parentheses
5269 suppresses arg-dependent lookup. We want to pass back
5270 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5271 (c++/37862), but none of the others. */
5272 if (*idk != CP_ID_KIND_QUALIFIED)
5273 *idk = CP_ID_KIND_NONE;
5275 /* The `>' token might be the end of a template-id or
5276 template-parameter-list now. */
5277 parser->greater_than_is_operator_p
5278 = saved_greater_than_is_operator_p;
5280 /* Consume the `)'. */
5281 token = cp_lexer_peek_token (parser->lexer);
5282 location_t close_paren_loc = token->location;
5283 expr.set_range (open_paren_loc, close_paren_loc);
5284 if (!parens.require_close (parser)
5285 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5286 cp_parser_skip_to_end_of_statement (parser);
5288 return expr;
5291 case CPP_OPEN_SQUARE:
5293 if (c_dialect_objc ())
5295 /* We might have an Objective-C++ message. */
5296 cp_parser_parse_tentatively (parser);
5297 tree msg = cp_parser_objc_message_expression (parser);
5298 /* If that works out, we're done ... */
5299 if (cp_parser_parse_definitely (parser))
5300 return msg;
5301 /* ... else, fall though to see if it's a lambda. */
5303 cp_expr lam = cp_parser_lambda_expression (parser);
5304 /* Don't warn about a failed tentative parse. */
5305 if (cp_parser_error_occurred (parser))
5306 return error_mark_node;
5307 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5308 return lam;
5311 case CPP_OBJC_STRING:
5312 if (c_dialect_objc ())
5313 /* We have an Objective-C++ string literal. */
5314 return cp_parser_objc_expression (parser);
5315 cp_parser_error (parser, "expected primary-expression");
5316 return error_mark_node;
5318 case CPP_KEYWORD:
5319 switch (token->keyword)
5321 /* These two are the boolean literals. */
5322 case RID_TRUE:
5323 cp_lexer_consume_token (parser->lexer);
5324 return cp_expr (boolean_true_node, token->location);
5325 case RID_FALSE:
5326 cp_lexer_consume_token (parser->lexer);
5327 return cp_expr (boolean_false_node, token->location);
5329 /* The `__null' literal. */
5330 case RID_NULL:
5331 cp_lexer_consume_token (parser->lexer);
5332 return cp_expr (null_node, token->location);
5334 /* The `nullptr' literal. */
5335 case RID_NULLPTR:
5336 cp_lexer_consume_token (parser->lexer);
5337 return cp_expr (nullptr_node, token->location);
5339 /* Recognize the `this' keyword. */
5340 case RID_THIS:
5341 cp_lexer_consume_token (parser->lexer);
5342 if (parser->local_variables_forbidden_p)
5344 error_at (token->location,
5345 "%<this%> may not be used in this context");
5346 return error_mark_node;
5348 /* Pointers cannot appear in constant-expressions. */
5349 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5350 return error_mark_node;
5351 return cp_expr (finish_this_expr (), token->location);
5353 /* The `operator' keyword can be the beginning of an
5354 id-expression. */
5355 case RID_OPERATOR:
5356 goto id_expression;
5358 case RID_FUNCTION_NAME:
5359 case RID_PRETTY_FUNCTION_NAME:
5360 case RID_C99_FUNCTION_NAME:
5362 non_integral_constant name;
5364 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5365 __func__ are the names of variables -- but they are
5366 treated specially. Therefore, they are handled here,
5367 rather than relying on the generic id-expression logic
5368 below. Grammatically, these names are id-expressions.
5370 Consume the token. */
5371 token = cp_lexer_consume_token (parser->lexer);
5373 switch (token->keyword)
5375 case RID_FUNCTION_NAME:
5376 name = NIC_FUNC_NAME;
5377 break;
5378 case RID_PRETTY_FUNCTION_NAME:
5379 name = NIC_PRETTY_FUNC;
5380 break;
5381 case RID_C99_FUNCTION_NAME:
5382 name = NIC_C99_FUNC;
5383 break;
5384 default:
5385 gcc_unreachable ();
5388 if (cp_parser_non_integral_constant_expression (parser, name))
5389 return error_mark_node;
5391 /* Look up the name. */
5392 return finish_fname (token->u.value);
5395 case RID_VA_ARG:
5397 tree expression;
5398 tree type;
5399 source_location type_location;
5400 location_t start_loc
5401 = cp_lexer_peek_token (parser->lexer)->location;
5402 /* The `__builtin_va_arg' construct is used to handle
5403 `va_arg'. Consume the `__builtin_va_arg' token. */
5404 cp_lexer_consume_token (parser->lexer);
5405 /* Look for the opening `('. */
5406 matching_parens parens;
5407 parens.require_open (parser);
5408 /* Now, parse the assignment-expression. */
5409 expression = cp_parser_assignment_expression (parser);
5410 /* Look for the `,'. */
5411 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5412 type_location = cp_lexer_peek_token (parser->lexer)->location;
5413 /* Parse the type-id. */
5415 type_id_in_expr_sentinel s (parser);
5416 type = cp_parser_type_id (parser);
5418 /* Look for the closing `)'. */
5419 location_t finish_loc
5420 = cp_lexer_peek_token (parser->lexer)->location;
5421 parens.require_close (parser);
5422 /* Using `va_arg' in a constant-expression is not
5423 allowed. */
5424 if (cp_parser_non_integral_constant_expression (parser,
5425 NIC_VA_ARG))
5426 return error_mark_node;
5427 /* Construct a location of the form:
5428 __builtin_va_arg (v, int)
5429 ~~~~~~~~~~~~~~~~~~~~~^~~~
5430 with the caret at the type, ranging from the start of the
5431 "__builtin_va_arg" token to the close paren. */
5432 location_t combined_loc
5433 = make_location (type_location, start_loc, finish_loc);
5434 return build_x_va_arg (combined_loc, expression, type);
5437 case RID_OFFSETOF:
5438 return cp_parser_builtin_offsetof (parser);
5440 case RID_HAS_NOTHROW_ASSIGN:
5441 case RID_HAS_NOTHROW_CONSTRUCTOR:
5442 case RID_HAS_NOTHROW_COPY:
5443 case RID_HAS_TRIVIAL_ASSIGN:
5444 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5445 case RID_HAS_TRIVIAL_COPY:
5446 case RID_HAS_TRIVIAL_DESTRUCTOR:
5447 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5448 case RID_HAS_VIRTUAL_DESTRUCTOR:
5449 case RID_IS_ABSTRACT:
5450 case RID_IS_AGGREGATE:
5451 case RID_IS_BASE_OF:
5452 case RID_IS_CLASS:
5453 case RID_IS_EMPTY:
5454 case RID_IS_ENUM:
5455 case RID_IS_FINAL:
5456 case RID_IS_LITERAL_TYPE:
5457 case RID_IS_POD:
5458 case RID_IS_POLYMORPHIC:
5459 case RID_IS_SAME_AS:
5460 case RID_IS_STD_LAYOUT:
5461 case RID_IS_TRIVIAL:
5462 case RID_IS_TRIVIALLY_ASSIGNABLE:
5463 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5464 case RID_IS_TRIVIALLY_COPYABLE:
5465 case RID_IS_UNION:
5466 case RID_IS_ASSIGNABLE:
5467 case RID_IS_CONSTRUCTIBLE:
5468 return cp_parser_trait_expr (parser, token->keyword);
5470 // C++ concepts
5471 case RID_REQUIRES:
5472 return cp_parser_requires_expression (parser);
5474 /* Objective-C++ expressions. */
5475 case RID_AT_ENCODE:
5476 case RID_AT_PROTOCOL:
5477 case RID_AT_SELECTOR:
5478 return cp_parser_objc_expression (parser);
5480 case RID_TEMPLATE:
5481 if (parser->in_function_body
5482 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5483 == CPP_LESS))
5485 error_at (token->location,
5486 "a template declaration cannot appear at block scope");
5487 cp_parser_skip_to_end_of_block_or_statement (parser);
5488 return error_mark_node;
5490 /* FALLTHRU */
5491 default:
5492 cp_parser_error (parser, "expected primary-expression");
5493 return error_mark_node;
5496 /* An id-expression can start with either an identifier, a
5497 `::' as the beginning of a qualified-id, or the "operator"
5498 keyword. */
5499 case CPP_NAME:
5500 case CPP_SCOPE:
5501 case CPP_TEMPLATE_ID:
5502 case CPP_NESTED_NAME_SPECIFIER:
5504 id_expression:
5505 cp_expr id_expression;
5506 cp_expr decl;
5507 const char *error_msg;
5508 bool template_p;
5509 bool done;
5510 cp_token *id_expr_token;
5512 /* Parse the id-expression. */
5513 id_expression
5514 = cp_parser_id_expression (parser,
5515 /*template_keyword_p=*/false,
5516 /*check_dependency_p=*/true,
5517 &template_p,
5518 /*declarator_p=*/false,
5519 /*optional_p=*/false);
5520 if (id_expression == error_mark_node)
5521 return error_mark_node;
5522 id_expr_token = token;
5523 token = cp_lexer_peek_token (parser->lexer);
5524 done = (token->type != CPP_OPEN_SQUARE
5525 && token->type != CPP_OPEN_PAREN
5526 && token->type != CPP_DOT
5527 && token->type != CPP_DEREF
5528 && token->type != CPP_PLUS_PLUS
5529 && token->type != CPP_MINUS_MINUS);
5530 /* If we have a template-id, then no further lookup is
5531 required. If the template-id was for a template-class, we
5532 will sometimes have a TYPE_DECL at this point. */
5533 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5534 || TREE_CODE (id_expression) == TYPE_DECL)
5535 decl = id_expression;
5536 /* Look up the name. */
5537 else
5539 tree ambiguous_decls;
5541 /* If we already know that this lookup is ambiguous, then
5542 we've already issued an error message; there's no reason
5543 to check again. */
5544 if (id_expr_token->type == CPP_NAME
5545 && id_expr_token->error_reported)
5547 cp_parser_simulate_error (parser);
5548 return error_mark_node;
5551 decl = cp_parser_lookup_name (parser, id_expression,
5552 none_type,
5553 template_p,
5554 /*is_namespace=*/false,
5555 /*check_dependency=*/true,
5556 &ambiguous_decls,
5557 id_expr_token->location);
5558 /* If the lookup was ambiguous, an error will already have
5559 been issued. */
5560 if (ambiguous_decls)
5561 return error_mark_node;
5563 /* In Objective-C++, we may have an Objective-C 2.0
5564 dot-syntax for classes here. */
5565 if (c_dialect_objc ()
5566 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5567 && TREE_CODE (decl) == TYPE_DECL
5568 && objc_is_class_name (decl))
5570 tree component;
5571 cp_lexer_consume_token (parser->lexer);
5572 component = cp_parser_identifier (parser);
5573 if (component == error_mark_node)
5574 return error_mark_node;
5576 tree result = objc_build_class_component_ref (id_expression,
5577 component);
5578 /* Build a location of the form:
5579 expr.component
5580 ~~~~~^~~~~~~~~
5581 with caret at the start of the component name (at
5582 input_location), ranging from the start of the id_expression
5583 to the end of the component name. */
5584 location_t combined_loc
5585 = make_location (input_location, id_expression.get_start (),
5586 get_finish (input_location));
5587 protected_set_expr_location (result, combined_loc);
5588 return result;
5591 /* In Objective-C++, an instance variable (ivar) may be preferred
5592 to whatever cp_parser_lookup_name() found.
5593 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5594 rest of c-family, we have to do a little extra work to preserve
5595 any location information in cp_expr "decl". Given that
5596 objc_lookup_ivar is implemented in "c-family" and "objc", we
5597 have a trip through the pure "tree" type, rather than cp_expr.
5598 Naively copying it back to "decl" would implicitly give the
5599 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5600 store an EXPR_LOCATION. Hence we only update "decl" (and
5601 hence its location_t) if we get back a different tree node. */
5602 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5603 id_expression);
5604 if (decl_tree != decl.get_value ())
5605 decl = cp_expr (decl_tree);
5607 /* If name lookup gives us a SCOPE_REF, then the
5608 qualifying scope was dependent. */
5609 if (TREE_CODE (decl) == SCOPE_REF)
5611 /* At this point, we do not know if DECL is a valid
5612 integral constant expression. We assume that it is
5613 in fact such an expression, so that code like:
5615 template <int N> struct A {
5616 int a[B<N>::i];
5619 is accepted. At template-instantiation time, we
5620 will check that B<N>::i is actually a constant. */
5621 return decl;
5623 /* Check to see if DECL is a local variable in a context
5624 where that is forbidden. */
5625 if (parser->local_variables_forbidden_p
5626 && local_variable_p (decl))
5628 error_at (id_expr_token->location,
5629 "local variable %qD may not appear in this context",
5630 decl.get_value ());
5631 return error_mark_node;
5635 if (processing_template_decl)
5636 if (tree fns = maybe_get_fns (decl))
5637 /* It's too difficult to mark ths in all the places where
5638 we know for sure we need to keep the lookup, so do it
5639 now. The cost is extra GC to recycle the lookups
5640 resolved at parse time. */
5641 lookup_keep (fns);
5643 decl = (finish_id_expression
5644 (id_expression, decl, parser->scope,
5645 idk,
5646 parser->integral_constant_expression_p,
5647 parser->allow_non_integral_constant_expression_p,
5648 &parser->non_integral_constant_expression_p,
5649 template_p, done, address_p,
5650 template_arg_p,
5651 &error_msg,
5652 id_expression.get_location ()));
5653 if (error_msg)
5654 cp_parser_error (parser, error_msg);
5655 decl.set_location (id_expr_token->location);
5656 return decl;
5659 /* Anything else is an error. */
5660 default:
5661 cp_parser_error (parser, "expected primary-expression");
5662 return error_mark_node;
5666 static inline cp_expr
5667 cp_parser_primary_expression (cp_parser *parser,
5668 bool address_p,
5669 bool cast_p,
5670 bool template_arg_p,
5671 cp_id_kind *idk)
5673 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5674 /*decltype*/false, idk);
5677 /* Parse an id-expression.
5679 id-expression:
5680 unqualified-id
5681 qualified-id
5683 qualified-id:
5684 :: [opt] nested-name-specifier template [opt] unqualified-id
5685 :: identifier
5686 :: operator-function-id
5687 :: template-id
5689 Return a representation of the unqualified portion of the
5690 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5691 a `::' or nested-name-specifier.
5693 Often, if the id-expression was a qualified-id, the caller will
5694 want to make a SCOPE_REF to represent the qualified-id. This
5695 function does not do this in order to avoid wastefully creating
5696 SCOPE_REFs when they are not required.
5698 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5699 `template' keyword.
5701 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5702 uninstantiated templates.
5704 If *TEMPLATE_P is non-NULL, it is set to true iff the
5705 `template' keyword is used to explicitly indicate that the entity
5706 named is a template.
5708 If DECLARATOR_P is true, the id-expression is appearing as part of
5709 a declarator, rather than as part of an expression. */
5711 static cp_expr
5712 cp_parser_id_expression (cp_parser *parser,
5713 bool template_keyword_p,
5714 bool check_dependency_p,
5715 bool *template_p,
5716 bool declarator_p,
5717 bool optional_p)
5719 bool global_scope_p;
5720 bool nested_name_specifier_p;
5722 /* Assume the `template' keyword was not used. */
5723 if (template_p)
5724 *template_p = template_keyword_p;
5726 /* Look for the optional `::' operator. */
5727 global_scope_p
5728 = (!template_keyword_p
5729 && (cp_parser_global_scope_opt (parser,
5730 /*current_scope_valid_p=*/false)
5731 != NULL_TREE));
5733 /* Look for the optional nested-name-specifier. */
5734 nested_name_specifier_p
5735 = (cp_parser_nested_name_specifier_opt (parser,
5736 /*typename_keyword_p=*/false,
5737 check_dependency_p,
5738 /*type_p=*/false,
5739 declarator_p,
5740 template_keyword_p)
5741 != NULL_TREE);
5743 /* If there is a nested-name-specifier, then we are looking at
5744 the first qualified-id production. */
5745 if (nested_name_specifier_p)
5747 tree saved_scope;
5748 tree saved_object_scope;
5749 tree saved_qualifying_scope;
5750 cp_expr unqualified_id;
5751 bool is_template;
5753 /* See if the next token is the `template' keyword. */
5754 if (!template_p)
5755 template_p = &is_template;
5756 *template_p = cp_parser_optional_template_keyword (parser);
5757 /* Name lookup we do during the processing of the
5758 unqualified-id might obliterate SCOPE. */
5759 saved_scope = parser->scope;
5760 saved_object_scope = parser->object_scope;
5761 saved_qualifying_scope = parser->qualifying_scope;
5762 /* Process the final unqualified-id. */
5763 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5764 check_dependency_p,
5765 declarator_p,
5766 /*optional_p=*/false);
5767 /* Restore the SAVED_SCOPE for our caller. */
5768 parser->scope = saved_scope;
5769 parser->object_scope = saved_object_scope;
5770 parser->qualifying_scope = saved_qualifying_scope;
5772 return unqualified_id;
5774 /* Otherwise, if we are in global scope, then we are looking at one
5775 of the other qualified-id productions. */
5776 else if (global_scope_p)
5778 cp_token *token;
5779 tree id;
5781 /* Peek at the next token. */
5782 token = cp_lexer_peek_token (parser->lexer);
5784 /* If it's an identifier, and the next token is not a "<", then
5785 we can avoid the template-id case. This is an optimization
5786 for this common case. */
5787 if (token->type == CPP_NAME
5788 && !cp_parser_nth_token_starts_template_argument_list_p
5789 (parser, 2))
5790 return cp_parser_identifier (parser);
5792 cp_parser_parse_tentatively (parser);
5793 /* Try a template-id. */
5794 id = cp_parser_template_id (parser,
5795 /*template_keyword_p=*/false,
5796 /*check_dependency_p=*/true,
5797 none_type,
5798 declarator_p);
5799 /* If that worked, we're done. */
5800 if (cp_parser_parse_definitely (parser))
5801 return id;
5803 /* Peek at the next token. (Changes in the token buffer may
5804 have invalidated the pointer obtained above.) */
5805 token = cp_lexer_peek_token (parser->lexer);
5807 switch (token->type)
5809 case CPP_NAME:
5810 return cp_parser_identifier (parser);
5812 case CPP_KEYWORD:
5813 if (token->keyword == RID_OPERATOR)
5814 return cp_parser_operator_function_id (parser);
5815 /* Fall through. */
5817 default:
5818 cp_parser_error (parser, "expected id-expression");
5819 return error_mark_node;
5822 else
5823 return cp_parser_unqualified_id (parser, template_keyword_p,
5824 /*check_dependency_p=*/true,
5825 declarator_p,
5826 optional_p);
5829 /* Parse an unqualified-id.
5831 unqualified-id:
5832 identifier
5833 operator-function-id
5834 conversion-function-id
5835 ~ class-name
5836 template-id
5838 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5839 keyword, in a construct like `A::template ...'.
5841 Returns a representation of unqualified-id. For the `identifier'
5842 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5843 production a BIT_NOT_EXPR is returned; the operand of the
5844 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5845 other productions, see the documentation accompanying the
5846 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5847 names are looked up in uninstantiated templates. If DECLARATOR_P
5848 is true, the unqualified-id is appearing as part of a declarator,
5849 rather than as part of an expression. */
5851 static cp_expr
5852 cp_parser_unqualified_id (cp_parser* parser,
5853 bool template_keyword_p,
5854 bool check_dependency_p,
5855 bool declarator_p,
5856 bool optional_p)
5858 cp_token *token;
5860 /* Peek at the next token. */
5861 token = cp_lexer_peek_token (parser->lexer);
5863 switch ((int) token->type)
5865 case CPP_NAME:
5867 tree id;
5869 /* We don't know yet whether or not this will be a
5870 template-id. */
5871 cp_parser_parse_tentatively (parser);
5872 /* Try a template-id. */
5873 id = cp_parser_template_id (parser, template_keyword_p,
5874 check_dependency_p,
5875 none_type,
5876 declarator_p);
5877 /* If it worked, we're done. */
5878 if (cp_parser_parse_definitely (parser))
5879 return id;
5880 /* Otherwise, it's an ordinary identifier. */
5881 return cp_parser_identifier (parser);
5884 case CPP_TEMPLATE_ID:
5885 return cp_parser_template_id (parser, template_keyword_p,
5886 check_dependency_p,
5887 none_type,
5888 declarator_p);
5890 case CPP_COMPL:
5892 tree type_decl;
5893 tree qualifying_scope;
5894 tree object_scope;
5895 tree scope;
5896 bool done;
5898 /* Consume the `~' token. */
5899 cp_lexer_consume_token (parser->lexer);
5900 /* Parse the class-name. The standard, as written, seems to
5901 say that:
5903 template <typename T> struct S { ~S (); };
5904 template <typename T> S<T>::~S() {}
5906 is invalid, since `~' must be followed by a class-name, but
5907 `S<T>' is dependent, and so not known to be a class.
5908 That's not right; we need to look in uninstantiated
5909 templates. A further complication arises from:
5911 template <typename T> void f(T t) {
5912 t.T::~T();
5915 Here, it is not possible to look up `T' in the scope of `T'
5916 itself. We must look in both the current scope, and the
5917 scope of the containing complete expression.
5919 Yet another issue is:
5921 struct S {
5922 int S;
5923 ~S();
5926 S::~S() {}
5928 The standard does not seem to say that the `S' in `~S'
5929 should refer to the type `S' and not the data member
5930 `S::S'. */
5932 /* DR 244 says that we look up the name after the "~" in the
5933 same scope as we looked up the qualifying name. That idea
5934 isn't fully worked out; it's more complicated than that. */
5935 scope = parser->scope;
5936 object_scope = parser->object_scope;
5937 qualifying_scope = parser->qualifying_scope;
5939 /* Check for invalid scopes. */
5940 if (scope == error_mark_node)
5942 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5943 cp_lexer_consume_token (parser->lexer);
5944 return error_mark_node;
5946 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5948 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5949 error_at (token->location,
5950 "scope %qT before %<~%> is not a class-name",
5951 scope);
5952 cp_parser_simulate_error (parser);
5953 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5954 cp_lexer_consume_token (parser->lexer);
5955 return error_mark_node;
5957 gcc_assert (!scope || TYPE_P (scope));
5959 /* If the name is of the form "X::~X" it's OK even if X is a
5960 typedef. */
5961 token = cp_lexer_peek_token (parser->lexer);
5962 if (scope
5963 && token->type == CPP_NAME
5964 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5965 != CPP_LESS)
5966 && (token->u.value == TYPE_IDENTIFIER (scope)
5967 || (CLASS_TYPE_P (scope)
5968 && constructor_name_p (token->u.value, scope))))
5970 cp_lexer_consume_token (parser->lexer);
5971 return build_nt (BIT_NOT_EXPR, scope);
5974 /* ~auto means the destructor of whatever the object is. */
5975 if (cp_parser_is_keyword (token, RID_AUTO))
5977 if (cxx_dialect < cxx14)
5978 pedwarn (input_location, 0,
5979 "%<~auto%> only available with "
5980 "-std=c++14 or -std=gnu++14");
5981 cp_lexer_consume_token (parser->lexer);
5982 return build_nt (BIT_NOT_EXPR, make_auto ());
5985 /* If there was an explicit qualification (S::~T), first look
5986 in the scope given by the qualification (i.e., S).
5988 Note: in the calls to cp_parser_class_name below we pass
5989 typename_type so that lookup finds the injected-class-name
5990 rather than the constructor. */
5991 done = false;
5992 type_decl = NULL_TREE;
5993 if (scope)
5995 cp_parser_parse_tentatively (parser);
5996 type_decl = cp_parser_class_name (parser,
5997 /*typename_keyword_p=*/false,
5998 /*template_keyword_p=*/false,
5999 typename_type,
6000 /*check_dependency=*/false,
6001 /*class_head_p=*/false,
6002 declarator_p);
6003 if (cp_parser_parse_definitely (parser))
6004 done = true;
6006 /* In "N::S::~S", look in "N" as well. */
6007 if (!done && scope && qualifying_scope)
6009 cp_parser_parse_tentatively (parser);
6010 parser->scope = qualifying_scope;
6011 parser->object_scope = NULL_TREE;
6012 parser->qualifying_scope = NULL_TREE;
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 (cp_parser_parse_definitely (parser))
6022 done = true;
6024 /* In "p->S::~T", look in the scope given by "*p" as well. */
6025 else if (!done && object_scope)
6027 cp_parser_parse_tentatively (parser);
6028 parser->scope = object_scope;
6029 parser->object_scope = NULL_TREE;
6030 parser->qualifying_scope = NULL_TREE;
6031 type_decl
6032 = cp_parser_class_name (parser,
6033 /*typename_keyword_p=*/false,
6034 /*template_keyword_p=*/false,
6035 typename_type,
6036 /*check_dependency=*/false,
6037 /*class_head_p=*/false,
6038 declarator_p);
6039 if (cp_parser_parse_definitely (parser))
6040 done = true;
6042 /* Look in the surrounding context. */
6043 if (!done)
6045 parser->scope = NULL_TREE;
6046 parser->object_scope = NULL_TREE;
6047 parser->qualifying_scope = NULL_TREE;
6048 if (processing_template_decl)
6049 cp_parser_parse_tentatively (parser);
6050 type_decl
6051 = cp_parser_class_name (parser,
6052 /*typename_keyword_p=*/false,
6053 /*template_keyword_p=*/false,
6054 typename_type,
6055 /*check_dependency=*/false,
6056 /*class_head_p=*/false,
6057 declarator_p);
6058 if (processing_template_decl
6059 && ! cp_parser_parse_definitely (parser))
6061 /* We couldn't find a type with this name. If we're parsing
6062 tentatively, fail and try something else. */
6063 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6065 cp_parser_simulate_error (parser);
6066 return error_mark_node;
6068 /* Otherwise, accept it and check for a match at instantiation
6069 time. */
6070 type_decl = cp_parser_identifier (parser);
6071 if (type_decl != error_mark_node)
6072 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6073 return type_decl;
6076 /* If an error occurred, assume that the name of the
6077 destructor is the same as the name of the qualifying
6078 class. That allows us to keep parsing after running
6079 into ill-formed destructor names. */
6080 if (type_decl == error_mark_node && scope)
6081 return build_nt (BIT_NOT_EXPR, scope);
6082 else if (type_decl == error_mark_node)
6083 return error_mark_node;
6085 /* Check that destructor name and scope match. */
6086 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6088 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6089 error_at (token->location,
6090 "declaration of %<~%T%> as member of %qT",
6091 type_decl, scope);
6092 cp_parser_simulate_error (parser);
6093 return error_mark_node;
6096 /* [class.dtor]
6098 A typedef-name that names a class shall not be used as the
6099 identifier in the declarator for a destructor declaration. */
6100 if (declarator_p
6101 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6102 && !DECL_SELF_REFERENCE_P (type_decl)
6103 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6104 error_at (token->location,
6105 "typedef-name %qD used as destructor declarator",
6106 type_decl);
6108 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6111 case CPP_KEYWORD:
6112 if (token->keyword == RID_OPERATOR)
6114 cp_expr id;
6116 /* This could be a template-id, so we try that first. */
6117 cp_parser_parse_tentatively (parser);
6118 /* Try a template-id. */
6119 id = cp_parser_template_id (parser, template_keyword_p,
6120 /*check_dependency_p=*/true,
6121 none_type,
6122 declarator_p);
6123 /* If that worked, we're done. */
6124 if (cp_parser_parse_definitely (parser))
6125 return id;
6126 /* We still don't know whether we're looking at an
6127 operator-function-id or a conversion-function-id. */
6128 cp_parser_parse_tentatively (parser);
6129 /* Try an operator-function-id. */
6130 id = cp_parser_operator_function_id (parser);
6131 /* If that didn't work, try a conversion-function-id. */
6132 if (!cp_parser_parse_definitely (parser))
6133 id = cp_parser_conversion_function_id (parser);
6135 return id;
6137 /* Fall through. */
6139 default:
6140 if (optional_p)
6141 return NULL_TREE;
6142 cp_parser_error (parser, "expected unqualified-id");
6143 return error_mark_node;
6147 /* Parse an (optional) nested-name-specifier.
6149 nested-name-specifier: [C++98]
6150 class-or-namespace-name :: nested-name-specifier [opt]
6151 class-or-namespace-name :: template nested-name-specifier [opt]
6153 nested-name-specifier: [C++0x]
6154 type-name ::
6155 namespace-name ::
6156 nested-name-specifier identifier ::
6157 nested-name-specifier template [opt] simple-template-id ::
6159 PARSER->SCOPE should be set appropriately before this function is
6160 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6161 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6162 in name lookups.
6164 Sets PARSER->SCOPE to the class (TYPE) or namespace
6165 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6166 it unchanged if there is no nested-name-specifier. Returns the new
6167 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6169 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6170 part of a declaration and/or decl-specifier. */
6172 static tree
6173 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6174 bool typename_keyword_p,
6175 bool check_dependency_p,
6176 bool type_p,
6177 bool is_declaration,
6178 bool template_keyword_p /* = false */)
6180 bool success = false;
6181 cp_token_position start = 0;
6182 cp_token *token;
6184 /* Remember where the nested-name-specifier starts. */
6185 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6187 start = cp_lexer_token_position (parser->lexer, false);
6188 push_deferring_access_checks (dk_deferred);
6191 while (true)
6193 tree new_scope;
6194 tree old_scope;
6195 tree saved_qualifying_scope;
6197 /* Spot cases that cannot be the beginning of a
6198 nested-name-specifier. */
6199 token = cp_lexer_peek_token (parser->lexer);
6201 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6202 the already parsed nested-name-specifier. */
6203 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6205 /* Grab the nested-name-specifier and continue the loop. */
6206 cp_parser_pre_parsed_nested_name_specifier (parser);
6207 /* If we originally encountered this nested-name-specifier
6208 with IS_DECLARATION set to false, we will not have
6209 resolved TYPENAME_TYPEs, so we must do so here. */
6210 if (is_declaration
6211 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6213 new_scope = resolve_typename_type (parser->scope,
6214 /*only_current_p=*/false);
6215 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6216 parser->scope = new_scope;
6218 success = true;
6219 continue;
6222 /* Spot cases that cannot be the beginning of a
6223 nested-name-specifier. On the second and subsequent times
6224 through the loop, we look for the `template' keyword. */
6225 if (success && token->keyword == RID_TEMPLATE)
6227 /* A template-id can start a nested-name-specifier. */
6228 else if (token->type == CPP_TEMPLATE_ID)
6230 /* DR 743: decltype can be used in a nested-name-specifier. */
6231 else if (token_is_decltype (token))
6233 else
6235 /* If the next token is not an identifier, then it is
6236 definitely not a type-name or namespace-name. */
6237 if (token->type != CPP_NAME)
6238 break;
6239 /* If the following token is neither a `<' (to begin a
6240 template-id), nor a `::', then we are not looking at a
6241 nested-name-specifier. */
6242 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6244 if (token->type == CPP_COLON
6245 && parser->colon_corrects_to_scope_p
6246 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6248 gcc_rich_location richloc (token->location);
6249 richloc.add_fixit_replace ("::");
6250 error_at (&richloc,
6251 "found %<:%> in nested-name-specifier, "
6252 "expected %<::%>");
6253 token->type = CPP_SCOPE;
6256 if (token->type != CPP_SCOPE
6257 && !cp_parser_nth_token_starts_template_argument_list_p
6258 (parser, 2))
6259 break;
6262 /* The nested-name-specifier is optional, so we parse
6263 tentatively. */
6264 cp_parser_parse_tentatively (parser);
6266 /* Look for the optional `template' keyword, if this isn't the
6267 first time through the loop. */
6268 if (success)
6269 template_keyword_p = cp_parser_optional_template_keyword (parser);
6271 /* Save the old scope since the name lookup we are about to do
6272 might destroy it. */
6273 old_scope = parser->scope;
6274 saved_qualifying_scope = parser->qualifying_scope;
6275 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6276 look up names in "X<T>::I" in order to determine that "Y" is
6277 a template. So, if we have a typename at this point, we make
6278 an effort to look through it. */
6279 if (is_declaration
6280 && !typename_keyword_p
6281 && parser->scope
6282 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6283 parser->scope = resolve_typename_type (parser->scope,
6284 /*only_current_p=*/false);
6285 /* Parse the qualifying entity. */
6286 new_scope
6287 = cp_parser_qualifying_entity (parser,
6288 typename_keyword_p,
6289 template_keyword_p,
6290 check_dependency_p,
6291 type_p,
6292 is_declaration);
6293 /* Look for the `::' token. */
6294 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6296 /* If we found what we wanted, we keep going; otherwise, we're
6297 done. */
6298 if (!cp_parser_parse_definitely (parser))
6300 bool error_p = false;
6302 /* Restore the OLD_SCOPE since it was valid before the
6303 failed attempt at finding the last
6304 class-or-namespace-name. */
6305 parser->scope = old_scope;
6306 parser->qualifying_scope = saved_qualifying_scope;
6308 /* If the next token is a decltype, and the one after that is a
6309 `::', then the decltype has failed to resolve to a class or
6310 enumeration type. Give this error even when parsing
6311 tentatively since it can't possibly be valid--and we're going
6312 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6313 won't get another chance.*/
6314 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6315 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6316 == CPP_SCOPE))
6318 token = cp_lexer_consume_token (parser->lexer);
6319 error_at (token->location, "decltype evaluates to %qT, "
6320 "which is not a class or enumeration type",
6321 token->u.tree_check_value->value);
6322 parser->scope = error_mark_node;
6323 error_p = true;
6324 /* As below. */
6325 success = true;
6326 cp_lexer_consume_token (parser->lexer);
6329 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6330 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6332 /* If we have a non-type template-id followed by ::, it can't
6333 possibly be valid. */
6334 token = cp_lexer_peek_token (parser->lexer);
6335 tree tid = token->u.tree_check_value->value;
6336 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6337 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6339 tree tmpl = NULL_TREE;
6340 if (is_overloaded_fn (tid))
6342 tree fns = get_fns (tid);
6343 if (OVL_SINGLE_P (fns))
6344 tmpl = OVL_FIRST (fns);
6345 error_at (token->location, "function template-id %qD "
6346 "in nested-name-specifier", tid);
6348 else
6350 /* Variable template. */
6351 tmpl = TREE_OPERAND (tid, 0);
6352 gcc_assert (variable_template_p (tmpl));
6353 error_at (token->location, "variable template-id %qD "
6354 "in nested-name-specifier", tid);
6356 if (tmpl)
6357 inform (DECL_SOURCE_LOCATION (tmpl),
6358 "%qD declared here", tmpl);
6360 parser->scope = error_mark_node;
6361 error_p = true;
6362 /* As below. */
6363 success = true;
6364 cp_lexer_consume_token (parser->lexer);
6365 cp_lexer_consume_token (parser->lexer);
6369 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6370 break;
6371 /* If the next token is an identifier, and the one after
6372 that is a `::', then any valid interpretation would have
6373 found a class-or-namespace-name. */
6374 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6375 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6376 == CPP_SCOPE)
6377 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6378 != CPP_COMPL))
6380 token = cp_lexer_consume_token (parser->lexer);
6381 if (!error_p)
6383 if (!token->error_reported)
6385 tree decl;
6386 tree ambiguous_decls;
6388 decl = cp_parser_lookup_name (parser, token->u.value,
6389 none_type,
6390 /*is_template=*/false,
6391 /*is_namespace=*/false,
6392 /*check_dependency=*/true,
6393 &ambiguous_decls,
6394 token->location);
6395 if (TREE_CODE (decl) == TEMPLATE_DECL)
6396 error_at (token->location,
6397 "%qD used without template arguments",
6398 decl);
6399 else if (ambiguous_decls)
6401 // cp_parser_lookup_name has the same diagnostic,
6402 // thus make sure to emit it at most once.
6403 if (cp_parser_uncommitted_to_tentative_parse_p
6404 (parser))
6406 error_at (token->location,
6407 "reference to %qD is ambiguous",
6408 token->u.value);
6409 print_candidates (ambiguous_decls);
6411 decl = error_mark_node;
6413 else
6415 if (cxx_dialect != cxx98)
6416 cp_parser_name_lookup_error
6417 (parser, token->u.value, decl, NLE_NOT_CXX98,
6418 token->location);
6419 else
6420 cp_parser_name_lookup_error
6421 (parser, token->u.value, decl, NLE_CXX98,
6422 token->location);
6425 parser->scope = error_mark_node;
6426 error_p = true;
6427 /* Treat this as a successful nested-name-specifier
6428 due to:
6430 [basic.lookup.qual]
6432 If the name found is not a class-name (clause
6433 _class_) or namespace-name (_namespace.def_), the
6434 program is ill-formed. */
6435 success = true;
6437 cp_lexer_consume_token (parser->lexer);
6439 break;
6441 /* We've found one valid nested-name-specifier. */
6442 success = true;
6443 /* Name lookup always gives us a DECL. */
6444 if (TREE_CODE (new_scope) == TYPE_DECL)
6445 new_scope = TREE_TYPE (new_scope);
6446 /* Uses of "template" must be followed by actual templates. */
6447 if (template_keyword_p
6448 && !(CLASS_TYPE_P (new_scope)
6449 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6450 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6451 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6452 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6453 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6454 == TEMPLATE_ID_EXPR)))
6455 permerror (input_location, TYPE_P (new_scope)
6456 ? G_("%qT is not a template")
6457 : G_("%qD is not a template"),
6458 new_scope);
6459 /* If it is a class scope, try to complete it; we are about to
6460 be looking up names inside the class. */
6461 if (TYPE_P (new_scope)
6462 /* Since checking types for dependency can be expensive,
6463 avoid doing it if the type is already complete. */
6464 && !COMPLETE_TYPE_P (new_scope)
6465 /* Do not try to complete dependent types. */
6466 && !dependent_type_p (new_scope))
6468 new_scope = complete_type (new_scope);
6469 /* If it is a typedef to current class, use the current
6470 class instead, as the typedef won't have any names inside
6471 it yet. */
6472 if (!COMPLETE_TYPE_P (new_scope)
6473 && currently_open_class (new_scope))
6474 new_scope = TYPE_MAIN_VARIANT (new_scope);
6476 /* Make sure we look in the right scope the next time through
6477 the loop. */
6478 parser->scope = new_scope;
6481 /* If parsing tentatively, replace the sequence of tokens that makes
6482 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6483 token. That way, should we re-parse the token stream, we will
6484 not have to repeat the effort required to do the parse, nor will
6485 we issue duplicate error messages. */
6486 if (success && start)
6488 cp_token *token;
6490 token = cp_lexer_token_at (parser->lexer, start);
6491 /* Reset the contents of the START token. */
6492 token->type = CPP_NESTED_NAME_SPECIFIER;
6493 /* Retrieve any deferred checks. Do not pop this access checks yet
6494 so the memory will not be reclaimed during token replacing below. */
6495 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6496 token->u.tree_check_value->value = parser->scope;
6497 token->u.tree_check_value->checks = get_deferred_access_checks ();
6498 token->u.tree_check_value->qualifying_scope =
6499 parser->qualifying_scope;
6500 token->keyword = RID_MAX;
6502 /* Purge all subsequent tokens. */
6503 cp_lexer_purge_tokens_after (parser->lexer, start);
6506 if (start)
6507 pop_to_parent_deferring_access_checks ();
6509 return success ? parser->scope : NULL_TREE;
6512 /* Parse a nested-name-specifier. See
6513 cp_parser_nested_name_specifier_opt for details. This function
6514 behaves identically, except that it will an issue an error if no
6515 nested-name-specifier is present. */
6517 static tree
6518 cp_parser_nested_name_specifier (cp_parser *parser,
6519 bool typename_keyword_p,
6520 bool check_dependency_p,
6521 bool type_p,
6522 bool is_declaration)
6524 tree scope;
6526 /* Look for the nested-name-specifier. */
6527 scope = cp_parser_nested_name_specifier_opt (parser,
6528 typename_keyword_p,
6529 check_dependency_p,
6530 type_p,
6531 is_declaration);
6532 /* If it was not present, issue an error message. */
6533 if (!scope)
6535 cp_parser_error (parser, "expected nested-name-specifier");
6536 parser->scope = NULL_TREE;
6539 return scope;
6542 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6543 this is either a class-name or a namespace-name (which corresponds
6544 to the class-or-namespace-name production in the grammar). For
6545 C++0x, it can also be a type-name that refers to an enumeration
6546 type or a simple-template-id.
6548 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6549 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6550 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6551 TYPE_P is TRUE iff the next name should be taken as a class-name,
6552 even the same name is declared to be another entity in the same
6553 scope.
6555 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6556 specified by the class-or-namespace-name. If neither is found the
6557 ERROR_MARK_NODE is returned. */
6559 static tree
6560 cp_parser_qualifying_entity (cp_parser *parser,
6561 bool typename_keyword_p,
6562 bool template_keyword_p,
6563 bool check_dependency_p,
6564 bool type_p,
6565 bool is_declaration)
6567 tree saved_scope;
6568 tree saved_qualifying_scope;
6569 tree saved_object_scope;
6570 tree scope;
6571 bool only_class_p;
6572 bool successful_parse_p;
6574 /* DR 743: decltype can appear in a nested-name-specifier. */
6575 if (cp_lexer_next_token_is_decltype (parser->lexer))
6577 scope = cp_parser_decltype (parser);
6578 if (TREE_CODE (scope) != ENUMERAL_TYPE
6579 && !MAYBE_CLASS_TYPE_P (scope))
6581 cp_parser_simulate_error (parser);
6582 return error_mark_node;
6584 if (TYPE_NAME (scope))
6585 scope = TYPE_NAME (scope);
6586 return scope;
6589 /* Before we try to parse the class-name, we must save away the
6590 current PARSER->SCOPE since cp_parser_class_name will destroy
6591 it. */
6592 saved_scope = parser->scope;
6593 saved_qualifying_scope = parser->qualifying_scope;
6594 saved_object_scope = parser->object_scope;
6595 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6596 there is no need to look for a namespace-name. */
6597 only_class_p = template_keyword_p
6598 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6599 if (!only_class_p)
6600 cp_parser_parse_tentatively (parser);
6601 scope = cp_parser_class_name (parser,
6602 typename_keyword_p,
6603 template_keyword_p,
6604 type_p ? class_type : none_type,
6605 check_dependency_p,
6606 /*class_head_p=*/false,
6607 is_declaration,
6608 /*enum_ok=*/cxx_dialect > cxx98);
6609 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6610 /* If that didn't work, try for a namespace-name. */
6611 if (!only_class_p && !successful_parse_p)
6613 /* Restore the saved scope. */
6614 parser->scope = saved_scope;
6615 parser->qualifying_scope = saved_qualifying_scope;
6616 parser->object_scope = saved_object_scope;
6617 /* If we are not looking at an identifier followed by the scope
6618 resolution operator, then this is not part of a
6619 nested-name-specifier. (Note that this function is only used
6620 to parse the components of a nested-name-specifier.) */
6621 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6622 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6623 return error_mark_node;
6624 scope = cp_parser_namespace_name (parser);
6627 return scope;
6630 /* Return true if we are looking at a compound-literal, false otherwise. */
6632 static bool
6633 cp_parser_compound_literal_p (cp_parser *parser)
6635 cp_lexer_save_tokens (parser->lexer);
6637 /* Skip tokens until the next token is a closing parenthesis.
6638 If we find the closing `)', and the next token is a `{', then
6639 we are looking at a compound-literal. */
6640 bool compound_literal_p
6641 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6642 /*consume_paren=*/true)
6643 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6645 /* Roll back the tokens we skipped. */
6646 cp_lexer_rollback_tokens (parser->lexer);
6648 return compound_literal_p;
6651 /* Return true if EXPR is the integer constant zero or a complex constant
6652 of zero, without any folding, but ignoring location wrappers. */
6654 bool
6655 literal_integer_zerop (const_tree expr)
6657 return (location_wrapper_p (expr)
6658 && integer_zerop (TREE_OPERAND (expr, 0)));
6661 /* Parse a postfix-expression.
6663 postfix-expression:
6664 primary-expression
6665 postfix-expression [ expression ]
6666 postfix-expression ( expression-list [opt] )
6667 simple-type-specifier ( expression-list [opt] )
6668 typename :: [opt] nested-name-specifier identifier
6669 ( expression-list [opt] )
6670 typename :: [opt] nested-name-specifier template [opt] template-id
6671 ( expression-list [opt] )
6672 postfix-expression . template [opt] id-expression
6673 postfix-expression -> template [opt] id-expression
6674 postfix-expression . pseudo-destructor-name
6675 postfix-expression -> pseudo-destructor-name
6676 postfix-expression ++
6677 postfix-expression --
6678 dynamic_cast < type-id > ( expression )
6679 static_cast < type-id > ( expression )
6680 reinterpret_cast < type-id > ( expression )
6681 const_cast < type-id > ( expression )
6682 typeid ( expression )
6683 typeid ( type-id )
6685 GNU Extension:
6687 postfix-expression:
6688 ( type-id ) { initializer-list , [opt] }
6690 This extension is a GNU version of the C99 compound-literal
6691 construct. (The C99 grammar uses `type-name' instead of `type-id',
6692 but they are essentially the same concept.)
6694 If ADDRESS_P is true, the postfix expression is the operand of the
6695 `&' operator. CAST_P is true if this expression is the target of a
6696 cast.
6698 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6699 class member access expressions [expr.ref].
6701 Returns a representation of the expression. */
6703 static cp_expr
6704 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6705 bool member_access_only_p, bool decltype_p,
6706 cp_id_kind * pidk_return)
6708 cp_token *token;
6709 location_t loc;
6710 enum rid keyword;
6711 cp_id_kind idk = CP_ID_KIND_NONE;
6712 cp_expr postfix_expression = NULL_TREE;
6713 bool is_member_access = false;
6715 /* Peek at the next token. */
6716 token = cp_lexer_peek_token (parser->lexer);
6717 loc = token->location;
6718 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6720 /* Some of the productions are determined by keywords. */
6721 keyword = token->keyword;
6722 switch (keyword)
6724 case RID_DYNCAST:
6725 case RID_STATCAST:
6726 case RID_REINTCAST:
6727 case RID_CONSTCAST:
6729 tree type;
6730 cp_expr expression;
6731 const char *saved_message;
6732 bool saved_in_type_id_in_expr_p;
6734 /* All of these can be handled in the same way from the point
6735 of view of parsing. Begin by consuming the token
6736 identifying the cast. */
6737 cp_lexer_consume_token (parser->lexer);
6739 /* New types cannot be defined in the cast. */
6740 saved_message = parser->type_definition_forbidden_message;
6741 parser->type_definition_forbidden_message
6742 = G_("types may not be defined in casts");
6744 /* Look for the opening `<'. */
6745 cp_parser_require (parser, CPP_LESS, RT_LESS);
6746 /* Parse the type to which we are casting. */
6747 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6748 parser->in_type_id_in_expr_p = true;
6749 type = cp_parser_type_id (parser);
6750 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6751 /* Look for the closing `>'. */
6752 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6753 /* Restore the old message. */
6754 parser->type_definition_forbidden_message = saved_message;
6756 bool saved_greater_than_is_operator_p
6757 = parser->greater_than_is_operator_p;
6758 parser->greater_than_is_operator_p = true;
6760 /* And the expression which is being cast. */
6761 matching_parens parens;
6762 parens.require_open (parser);
6763 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6764 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6765 RT_CLOSE_PAREN);
6766 location_t end_loc = close_paren ?
6767 close_paren->location : UNKNOWN_LOCATION;
6769 parser->greater_than_is_operator_p
6770 = saved_greater_than_is_operator_p;
6772 /* Only type conversions to integral or enumeration types
6773 can be used in constant-expressions. */
6774 if (!cast_valid_in_integral_constant_expression_p (type)
6775 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6777 postfix_expression = error_mark_node;
6778 break;
6781 switch (keyword)
6783 case RID_DYNCAST:
6784 postfix_expression
6785 = build_dynamic_cast (type, expression, tf_warning_or_error);
6786 break;
6787 case RID_STATCAST:
6788 postfix_expression
6789 = build_static_cast (type, expression, tf_warning_or_error);
6790 break;
6791 case RID_REINTCAST:
6792 postfix_expression
6793 = build_reinterpret_cast (type, expression,
6794 tf_warning_or_error);
6795 break;
6796 case RID_CONSTCAST:
6797 postfix_expression
6798 = build_const_cast (type, expression, tf_warning_or_error);
6799 break;
6800 default:
6801 gcc_unreachable ();
6804 /* Construct a location e.g. :
6805 reinterpret_cast <int *> (expr)
6806 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6807 ranging from the start of the "*_cast" token to the final closing
6808 paren, with the caret at the start. */
6809 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6810 postfix_expression.set_location (cp_cast_loc);
6812 break;
6814 case RID_TYPEID:
6816 tree type;
6817 const char *saved_message;
6818 bool saved_in_type_id_in_expr_p;
6820 /* Consume the `typeid' token. */
6821 cp_lexer_consume_token (parser->lexer);
6822 /* Look for the `(' token. */
6823 matching_parens parens;
6824 parens.require_open (parser);
6825 /* Types cannot be defined in a `typeid' expression. */
6826 saved_message = parser->type_definition_forbidden_message;
6827 parser->type_definition_forbidden_message
6828 = G_("types may not be defined in a %<typeid%> expression");
6829 /* We can't be sure yet whether we're looking at a type-id or an
6830 expression. */
6831 cp_parser_parse_tentatively (parser);
6832 /* Try a type-id first. */
6833 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6834 parser->in_type_id_in_expr_p = true;
6835 type = cp_parser_type_id (parser);
6836 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6837 /* Look for the `)' token. Otherwise, we can't be sure that
6838 we're not looking at an expression: consider `typeid (int
6839 (3))', for example. */
6840 cp_token *close_paren = parens.require_close (parser);
6841 /* If all went well, simply lookup the type-id. */
6842 if (cp_parser_parse_definitely (parser))
6843 postfix_expression = get_typeid (type, tf_warning_or_error);
6844 /* Otherwise, fall back to the expression variant. */
6845 else
6847 tree expression;
6849 /* Look for an expression. */
6850 expression = cp_parser_expression (parser, & idk);
6851 /* Compute its typeid. */
6852 postfix_expression = build_typeid (expression, tf_warning_or_error);
6853 /* Look for the `)' token. */
6854 close_paren = parens.require_close (parser);
6856 /* Restore the saved message. */
6857 parser->type_definition_forbidden_message = saved_message;
6858 /* `typeid' may not appear in an integral constant expression. */
6859 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6860 postfix_expression = error_mark_node;
6862 /* Construct a location e.g. :
6863 typeid (expr)
6864 ^~~~~~~~~~~~~
6865 ranging from the start of the "typeid" token to the final closing
6866 paren, with the caret at the start. */
6867 if (close_paren)
6869 location_t typeid_loc
6870 = make_location (start_loc, start_loc, close_paren->location);
6871 postfix_expression.set_location (typeid_loc);
6872 postfix_expression.maybe_add_location_wrapper ();
6875 break;
6877 case RID_TYPENAME:
6879 tree type;
6880 /* The syntax permitted here is the same permitted for an
6881 elaborated-type-specifier. */
6882 ++parser->prevent_constrained_type_specifiers;
6883 type = cp_parser_elaborated_type_specifier (parser,
6884 /*is_friend=*/false,
6885 /*is_declaration=*/false);
6886 --parser->prevent_constrained_type_specifiers;
6887 postfix_expression = cp_parser_functional_cast (parser, type);
6889 break;
6891 case RID_ADDRESSOF:
6892 case RID_BUILTIN_SHUFFLE:
6893 case RID_BUILTIN_LAUNDER:
6895 vec<tree, va_gc> *vec;
6896 unsigned int i;
6897 tree p;
6899 cp_lexer_consume_token (parser->lexer);
6900 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6901 /*cast_p=*/false, /*allow_expansion_p=*/true,
6902 /*non_constant_p=*/NULL);
6903 if (vec == NULL)
6905 postfix_expression = error_mark_node;
6906 break;
6909 FOR_EACH_VEC_ELT (*vec, i, p)
6910 mark_exp_read (p);
6912 switch (keyword)
6914 case RID_ADDRESSOF:
6915 if (vec->length () == 1)
6916 postfix_expression
6917 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6918 else
6920 error_at (loc, "wrong number of arguments to "
6921 "%<__builtin_addressof%>");
6922 postfix_expression = error_mark_node;
6924 break;
6926 case RID_BUILTIN_LAUNDER:
6927 if (vec->length () == 1)
6928 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6929 tf_warning_or_error);
6930 else
6932 error_at (loc, "wrong number of arguments to "
6933 "%<__builtin_launder%>");
6934 postfix_expression = error_mark_node;
6936 break;
6938 case RID_BUILTIN_SHUFFLE:
6939 if (vec->length () == 2)
6940 postfix_expression
6941 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6942 (*vec)[1], tf_warning_or_error);
6943 else if (vec->length () == 3)
6944 postfix_expression
6945 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6946 (*vec)[2], tf_warning_or_error);
6947 else
6949 error_at (loc, "wrong number of arguments to "
6950 "%<__builtin_shuffle%>");
6951 postfix_expression = error_mark_node;
6953 break;
6955 default:
6956 gcc_unreachable ();
6958 break;
6961 default:
6963 tree type;
6965 /* If the next thing is a simple-type-specifier, we may be
6966 looking at a functional cast. We could also be looking at
6967 an id-expression. So, we try the functional cast, and if
6968 that doesn't work we fall back to the primary-expression. */
6969 cp_parser_parse_tentatively (parser);
6970 /* Look for the simple-type-specifier. */
6971 ++parser->prevent_constrained_type_specifiers;
6972 type = cp_parser_simple_type_specifier (parser,
6973 /*decl_specs=*/NULL,
6974 CP_PARSER_FLAGS_NONE);
6975 --parser->prevent_constrained_type_specifiers;
6976 /* Parse the cast itself. */
6977 if (!cp_parser_error_occurred (parser))
6978 postfix_expression
6979 = cp_parser_functional_cast (parser, type);
6980 /* If that worked, we're done. */
6981 if (cp_parser_parse_definitely (parser))
6982 break;
6984 /* If the functional-cast didn't work out, try a
6985 compound-literal. */
6986 if (cp_parser_allow_gnu_extensions_p (parser)
6987 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6989 cp_expr initializer = NULL_TREE;
6991 cp_parser_parse_tentatively (parser);
6993 matching_parens parens;
6994 parens.consume_open (parser);
6996 /* Avoid calling cp_parser_type_id pointlessly, see comment
6997 in cp_parser_cast_expression about c++/29234. */
6998 if (!cp_parser_compound_literal_p (parser))
6999 cp_parser_simulate_error (parser);
7000 else
7002 /* Parse the type. */
7003 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7004 parser->in_type_id_in_expr_p = true;
7005 type = cp_parser_type_id (parser);
7006 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7007 parens.require_close (parser);
7010 /* If things aren't going well, there's no need to
7011 keep going. */
7012 if (!cp_parser_error_occurred (parser))
7014 bool non_constant_p;
7015 /* Parse the brace-enclosed initializer list. */
7016 initializer = cp_parser_braced_list (parser,
7017 &non_constant_p);
7019 /* If that worked, we're definitely looking at a
7020 compound-literal expression. */
7021 if (cp_parser_parse_definitely (parser))
7023 /* Warn the user that a compound literal is not
7024 allowed in standard C++. */
7025 pedwarn (input_location, OPT_Wpedantic,
7026 "ISO C++ forbids compound-literals");
7027 /* For simplicity, we disallow compound literals in
7028 constant-expressions. We could
7029 allow compound literals of integer type, whose
7030 initializer was a constant, in constant
7031 expressions. Permitting that usage, as a further
7032 extension, would not change the meaning of any
7033 currently accepted programs. (Of course, as
7034 compound literals are not part of ISO C++, the
7035 standard has nothing to say.) */
7036 if (cp_parser_non_integral_constant_expression (parser,
7037 NIC_NCC))
7039 postfix_expression = error_mark_node;
7040 break;
7042 /* Form the representation of the compound-literal. */
7043 postfix_expression
7044 = finish_compound_literal (type, initializer,
7045 tf_warning_or_error, fcl_c99);
7046 postfix_expression.set_location (initializer.get_location ());
7047 break;
7051 /* It must be a primary-expression. */
7052 postfix_expression
7053 = cp_parser_primary_expression (parser, address_p, cast_p,
7054 /*template_arg_p=*/false,
7055 decltype_p,
7056 &idk);
7058 break;
7061 /* Note that we don't need to worry about calling build_cplus_new on a
7062 class-valued CALL_EXPR in decltype when it isn't the end of the
7063 postfix-expression; unary_complex_lvalue will take care of that for
7064 all these cases. */
7066 /* Keep looping until the postfix-expression is complete. */
7067 while (true)
7069 if (idk == CP_ID_KIND_UNQUALIFIED
7070 && identifier_p (postfix_expression)
7071 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7072 /* It is not a Koenig lookup function call. */
7073 postfix_expression
7074 = unqualified_name_lookup_error (postfix_expression);
7076 /* Peek at the next token. */
7077 token = cp_lexer_peek_token (parser->lexer);
7079 switch (token->type)
7081 case CPP_OPEN_SQUARE:
7082 if (cp_next_tokens_can_be_std_attribute_p (parser))
7084 cp_parser_error (parser,
7085 "two consecutive %<[%> shall "
7086 "only introduce an attribute");
7087 return error_mark_node;
7089 postfix_expression
7090 = cp_parser_postfix_open_square_expression (parser,
7091 postfix_expression,
7092 false,
7093 decltype_p);
7094 postfix_expression.set_range (start_loc,
7095 postfix_expression.get_location ());
7097 idk = CP_ID_KIND_NONE;
7098 is_member_access = false;
7099 break;
7101 case CPP_OPEN_PAREN:
7102 /* postfix-expression ( expression-list [opt] ) */
7104 bool koenig_p;
7105 bool is_builtin_constant_p;
7106 bool saved_integral_constant_expression_p = false;
7107 bool saved_non_integral_constant_expression_p = false;
7108 tsubst_flags_t complain = complain_flags (decltype_p);
7109 vec<tree, va_gc> *args;
7110 location_t close_paren_loc = UNKNOWN_LOCATION;
7112 is_member_access = false;
7114 is_builtin_constant_p
7115 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7116 if (is_builtin_constant_p)
7118 /* The whole point of __builtin_constant_p is to allow
7119 non-constant expressions to appear as arguments. */
7120 saved_integral_constant_expression_p
7121 = parser->integral_constant_expression_p;
7122 saved_non_integral_constant_expression_p
7123 = parser->non_integral_constant_expression_p;
7124 parser->integral_constant_expression_p = false;
7126 args = (cp_parser_parenthesized_expression_list
7127 (parser, non_attr,
7128 /*cast_p=*/false, /*allow_expansion_p=*/true,
7129 /*non_constant_p=*/NULL,
7130 /*close_paren_loc=*/&close_paren_loc,
7131 /*wrap_locations_p=*/true));
7132 if (is_builtin_constant_p)
7134 parser->integral_constant_expression_p
7135 = saved_integral_constant_expression_p;
7136 parser->non_integral_constant_expression_p
7137 = saved_non_integral_constant_expression_p;
7140 if (args == NULL)
7142 postfix_expression = error_mark_node;
7143 break;
7146 /* Function calls are not permitted in
7147 constant-expressions. */
7148 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7149 && cp_parser_non_integral_constant_expression (parser,
7150 NIC_FUNC_CALL))
7152 postfix_expression = error_mark_node;
7153 release_tree_vector (args);
7154 break;
7157 koenig_p = false;
7158 if (idk == CP_ID_KIND_UNQUALIFIED
7159 || idk == CP_ID_KIND_TEMPLATE_ID)
7161 if (identifier_p (postfix_expression))
7163 if (!args->is_empty ())
7165 koenig_p = true;
7166 if (!any_type_dependent_arguments_p (args))
7167 postfix_expression
7168 = perform_koenig_lookup (postfix_expression, args,
7169 complain);
7171 else
7172 postfix_expression
7173 = unqualified_fn_lookup_error (postfix_expression);
7175 /* We do not perform argument-dependent lookup if
7176 normal lookup finds a non-function, in accordance
7177 with the expected resolution of DR 218. */
7178 else if (!args->is_empty ()
7179 && is_overloaded_fn (postfix_expression))
7181 tree fn = get_first_fn (postfix_expression);
7182 fn = STRIP_TEMPLATE (fn);
7184 /* Do not do argument dependent lookup if regular
7185 lookup finds a member function or a block-scope
7186 function declaration. [basic.lookup.argdep]/3 */
7187 if (!DECL_FUNCTION_MEMBER_P (fn)
7188 && !DECL_LOCAL_FUNCTION_P (fn))
7190 koenig_p = true;
7191 if (!any_type_dependent_arguments_p (args))
7192 postfix_expression
7193 = perform_koenig_lookup (postfix_expression, args,
7194 complain);
7199 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7201 tree instance = TREE_OPERAND (postfix_expression, 0);
7202 tree fn = TREE_OPERAND (postfix_expression, 1);
7204 if (processing_template_decl
7205 && (type_dependent_object_expression_p (instance)
7206 || (!BASELINK_P (fn)
7207 && TREE_CODE (fn) != FIELD_DECL)
7208 || type_dependent_expression_p (fn)
7209 || any_type_dependent_arguments_p (args)))
7211 maybe_generic_this_capture (instance, fn);
7212 postfix_expression
7213 = build_min_nt_call_vec (postfix_expression, args);
7214 release_tree_vector (args);
7215 break;
7218 if (BASELINK_P (fn))
7220 postfix_expression
7221 = (build_new_method_call
7222 (instance, fn, &args, NULL_TREE,
7223 (idk == CP_ID_KIND_QUALIFIED
7224 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7225 : LOOKUP_NORMAL),
7226 /*fn_p=*/NULL,
7227 complain));
7229 else
7230 postfix_expression
7231 = finish_call_expr (postfix_expression, &args,
7232 /*disallow_virtual=*/false,
7233 /*koenig_p=*/false,
7234 complain);
7236 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7237 || TREE_CODE (postfix_expression) == MEMBER_REF
7238 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7239 postfix_expression = (build_offset_ref_call_from_tree
7240 (postfix_expression, &args,
7241 complain));
7242 else if (idk == CP_ID_KIND_QUALIFIED)
7243 /* A call to a static class member, or a namespace-scope
7244 function. */
7245 postfix_expression
7246 = finish_call_expr (postfix_expression, &args,
7247 /*disallow_virtual=*/true,
7248 koenig_p,
7249 complain);
7250 else
7251 /* All other function calls. */
7252 postfix_expression
7253 = finish_call_expr (postfix_expression, &args,
7254 /*disallow_virtual=*/false,
7255 koenig_p,
7256 complain);
7258 if (close_paren_loc != UNKNOWN_LOCATION)
7260 location_t combined_loc = make_location (token->location,
7261 start_loc,
7262 close_paren_loc);
7263 postfix_expression.set_location (combined_loc);
7266 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7267 idk = CP_ID_KIND_NONE;
7269 release_tree_vector (args);
7271 break;
7273 case CPP_DOT:
7274 case CPP_DEREF:
7275 /* postfix-expression . template [opt] id-expression
7276 postfix-expression . pseudo-destructor-name
7277 postfix-expression -> template [opt] id-expression
7278 postfix-expression -> pseudo-destructor-name */
7280 /* Consume the `.' or `->' operator. */
7281 cp_lexer_consume_token (parser->lexer);
7283 postfix_expression
7284 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7285 postfix_expression,
7286 false, &idk, loc);
7288 is_member_access = true;
7289 break;
7291 case CPP_PLUS_PLUS:
7292 /* postfix-expression ++ */
7293 /* Consume the `++' token. */
7294 cp_lexer_consume_token (parser->lexer);
7295 /* Generate a representation for the complete expression. */
7296 postfix_expression
7297 = finish_increment_expr (postfix_expression,
7298 POSTINCREMENT_EXPR);
7299 /* Increments may not appear in constant-expressions. */
7300 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7301 postfix_expression = error_mark_node;
7302 idk = CP_ID_KIND_NONE;
7303 is_member_access = false;
7304 break;
7306 case CPP_MINUS_MINUS:
7307 /* postfix-expression -- */
7308 /* Consume the `--' token. */
7309 cp_lexer_consume_token (parser->lexer);
7310 /* Generate a representation for the complete expression. */
7311 postfix_expression
7312 = finish_increment_expr (postfix_expression,
7313 POSTDECREMENT_EXPR);
7314 /* Decrements may not appear in constant-expressions. */
7315 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7316 postfix_expression = error_mark_node;
7317 idk = CP_ID_KIND_NONE;
7318 is_member_access = false;
7319 break;
7321 default:
7322 if (pidk_return != NULL)
7323 * pidk_return = idk;
7324 if (member_access_only_p)
7325 return is_member_access
7326 ? postfix_expression
7327 : cp_expr (error_mark_node);
7328 else
7329 return postfix_expression;
7333 /* We should never get here. */
7334 gcc_unreachable ();
7335 return error_mark_node;
7338 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7339 by cp_parser_builtin_offsetof. We're looking for
7341 postfix-expression [ expression ]
7342 postfix-expression [ braced-init-list ] (C++11)
7344 FOR_OFFSETOF is set if we're being called in that context, which
7345 changes how we deal with integer constant expressions. */
7347 static tree
7348 cp_parser_postfix_open_square_expression (cp_parser *parser,
7349 tree postfix_expression,
7350 bool for_offsetof,
7351 bool decltype_p)
7353 tree index = NULL_TREE;
7354 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7355 bool saved_greater_than_is_operator_p;
7357 /* Consume the `[' token. */
7358 cp_lexer_consume_token (parser->lexer);
7360 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7361 parser->greater_than_is_operator_p = true;
7363 /* Parse the index expression. */
7364 /* ??? For offsetof, there is a question of what to allow here. If
7365 offsetof is not being used in an integral constant expression context,
7366 then we *could* get the right answer by computing the value at runtime.
7367 If we are in an integral constant expression context, then we might
7368 could accept any constant expression; hard to say without analysis.
7369 Rather than open the barn door too wide right away, allow only integer
7370 constant expressions here. */
7371 if (for_offsetof)
7372 index = cp_parser_constant_expression (parser);
7373 else
7375 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7377 bool expr_nonconst_p;
7378 cp_lexer_set_source_position (parser->lexer);
7379 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7380 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7382 else
7383 index = cp_parser_expression (parser);
7386 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7388 /* Look for the closing `]'. */
7389 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7391 /* Build the ARRAY_REF. */
7392 postfix_expression = grok_array_decl (loc, postfix_expression,
7393 index, decltype_p);
7395 /* When not doing offsetof, array references are not permitted in
7396 constant-expressions. */
7397 if (!for_offsetof
7398 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7399 postfix_expression = error_mark_node;
7401 return postfix_expression;
7404 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7405 dereference of incomplete type, returns true if error_mark_node should
7406 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7407 and *DEPENDENT_P. */
7409 bool
7410 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7411 bool *dependent_p)
7413 /* In a template, be permissive by treating an object expression
7414 of incomplete type as dependent (after a pedwarn). */
7415 diagnostic_t kind = (processing_template_decl
7416 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7418 switch (TREE_CODE (*postfix_expression))
7420 case CAST_EXPR:
7421 case REINTERPRET_CAST_EXPR:
7422 case CONST_CAST_EXPR:
7423 case STATIC_CAST_EXPR:
7424 case DYNAMIC_CAST_EXPR:
7425 case IMPLICIT_CONV_EXPR:
7426 case VIEW_CONVERT_EXPR:
7427 case NON_LVALUE_EXPR:
7428 kind = DK_ERROR;
7429 break;
7430 case OVERLOAD:
7431 /* Don't emit any diagnostic for OVERLOADs. */
7432 kind = DK_IGNORED;
7433 break;
7434 default:
7435 /* Avoid clobbering e.g. DECLs. */
7436 if (!EXPR_P (*postfix_expression))
7437 kind = DK_ERROR;
7438 break;
7441 if (kind == DK_IGNORED)
7442 return false;
7444 location_t exploc = location_of (*postfix_expression);
7445 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7446 if (!MAYBE_CLASS_TYPE_P (*scope))
7447 return true;
7448 if (kind == DK_ERROR)
7449 *scope = *postfix_expression = error_mark_node;
7450 else if (processing_template_decl)
7452 *dependent_p = true;
7453 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7455 return false;
7458 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7459 by cp_parser_builtin_offsetof. We're looking for
7461 postfix-expression . template [opt] id-expression
7462 postfix-expression . pseudo-destructor-name
7463 postfix-expression -> template [opt] id-expression
7464 postfix-expression -> pseudo-destructor-name
7466 FOR_OFFSETOF is set if we're being called in that context. That sorta
7467 limits what of the above we'll actually accept, but nevermind.
7468 TOKEN_TYPE is the "." or "->" token, which will already have been
7469 removed from the stream. */
7471 static tree
7472 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7473 enum cpp_ttype token_type,
7474 cp_expr postfix_expression,
7475 bool for_offsetof, cp_id_kind *idk,
7476 location_t location)
7478 tree name;
7479 bool dependent_p;
7480 bool pseudo_destructor_p;
7481 tree scope = NULL_TREE;
7482 location_t start_loc = postfix_expression.get_start ();
7484 /* If this is a `->' operator, dereference the pointer. */
7485 if (token_type == CPP_DEREF)
7486 postfix_expression = build_x_arrow (location, postfix_expression,
7487 tf_warning_or_error);
7488 /* Check to see whether or not the expression is type-dependent and
7489 not the current instantiation. */
7490 dependent_p = type_dependent_object_expression_p (postfix_expression);
7491 /* The identifier following the `->' or `.' is not qualified. */
7492 parser->scope = NULL_TREE;
7493 parser->qualifying_scope = NULL_TREE;
7494 parser->object_scope = NULL_TREE;
7495 *idk = CP_ID_KIND_NONE;
7497 /* Enter the scope corresponding to the type of the object
7498 given by the POSTFIX_EXPRESSION. */
7499 if (!dependent_p)
7501 scope = TREE_TYPE (postfix_expression);
7502 /* According to the standard, no expression should ever have
7503 reference type. Unfortunately, we do not currently match
7504 the standard in this respect in that our internal representation
7505 of an expression may have reference type even when the standard
7506 says it does not. Therefore, we have to manually obtain the
7507 underlying type here. */
7508 scope = non_reference (scope);
7509 /* The type of the POSTFIX_EXPRESSION must be complete. */
7510 /* Unlike the object expression in other contexts, *this is not
7511 required to be of complete type for purposes of class member
7512 access (5.2.5) outside the member function body. */
7513 if (postfix_expression != current_class_ref
7514 && scope != error_mark_node
7515 && !currently_open_class (scope))
7517 scope = complete_type (scope);
7518 if (!COMPLETE_TYPE_P (scope)
7519 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7520 &dependent_p))
7521 return error_mark_node;
7524 if (!dependent_p)
7526 /* Let the name lookup machinery know that we are processing a
7527 class member access expression. */
7528 parser->context->object_type = scope;
7529 /* If something went wrong, we want to be able to discern that case,
7530 as opposed to the case where there was no SCOPE due to the type
7531 of expression being dependent. */
7532 if (!scope)
7533 scope = error_mark_node;
7534 /* If the SCOPE was erroneous, make the various semantic analysis
7535 functions exit quickly -- and without issuing additional error
7536 messages. */
7537 if (scope == error_mark_node)
7538 postfix_expression = error_mark_node;
7542 if (dependent_p)
7543 /* Tell cp_parser_lookup_name that there was an object, even though it's
7544 type-dependent. */
7545 parser->context->object_type = unknown_type_node;
7547 /* Assume this expression is not a pseudo-destructor access. */
7548 pseudo_destructor_p = false;
7550 /* If the SCOPE is a scalar type, then, if this is a valid program,
7551 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7552 is type dependent, it can be pseudo-destructor-name or something else.
7553 Try to parse it as pseudo-destructor-name first. */
7554 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7556 tree s;
7557 tree type;
7559 cp_parser_parse_tentatively (parser);
7560 /* Parse the pseudo-destructor-name. */
7561 s = NULL_TREE;
7562 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7563 &s, &type);
7564 if (dependent_p
7565 && (cp_parser_error_occurred (parser)
7566 || !SCALAR_TYPE_P (type)))
7567 cp_parser_abort_tentative_parse (parser);
7568 else if (cp_parser_parse_definitely (parser))
7570 pseudo_destructor_p = true;
7571 postfix_expression
7572 = finish_pseudo_destructor_expr (postfix_expression,
7573 s, type, location);
7577 if (!pseudo_destructor_p)
7579 /* If the SCOPE is not a scalar type, we are looking at an
7580 ordinary class member access expression, rather than a
7581 pseudo-destructor-name. */
7582 bool template_p;
7583 cp_token *token = cp_lexer_peek_token (parser->lexer);
7584 /* Parse the id-expression. */
7585 name = (cp_parser_id_expression
7586 (parser,
7587 cp_parser_optional_template_keyword (parser),
7588 /*check_dependency_p=*/true,
7589 &template_p,
7590 /*declarator_p=*/false,
7591 /*optional_p=*/false));
7592 /* In general, build a SCOPE_REF if the member name is qualified.
7593 However, if the name was not dependent and has already been
7594 resolved; there is no need to build the SCOPE_REF. For example;
7596 struct X { void f(); };
7597 template <typename T> void f(T* t) { t->X::f(); }
7599 Even though "t" is dependent, "X::f" is not and has been resolved
7600 to a BASELINK; there is no need to include scope information. */
7602 /* But we do need to remember that there was an explicit scope for
7603 virtual function calls. */
7604 if (parser->scope)
7605 *idk = CP_ID_KIND_QUALIFIED;
7607 /* If the name is a template-id that names a type, we will get a
7608 TYPE_DECL here. That is invalid code. */
7609 if (TREE_CODE (name) == TYPE_DECL)
7611 error_at (token->location, "invalid use of %qD", name);
7612 postfix_expression = error_mark_node;
7614 else
7616 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7618 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7620 error_at (token->location, "%<%D::%D%> is not a class member",
7621 parser->scope, name);
7622 postfix_expression = error_mark_node;
7624 else
7625 name = build_qualified_name (/*type=*/NULL_TREE,
7626 parser->scope,
7627 name,
7628 template_p);
7629 parser->scope = NULL_TREE;
7630 parser->qualifying_scope = NULL_TREE;
7631 parser->object_scope = NULL_TREE;
7633 if (parser->scope && name && BASELINK_P (name))
7634 adjust_result_of_qualified_name_lookup
7635 (name, parser->scope, scope);
7636 postfix_expression
7637 = finish_class_member_access_expr (postfix_expression, name,
7638 template_p,
7639 tf_warning_or_error);
7640 /* Build a location e.g.:
7641 ptr->access_expr
7642 ~~~^~~~~~~~~~~~~
7643 where the caret is at the deref token, ranging from
7644 the start of postfix_expression to the end of the access expr. */
7645 location_t end_loc
7646 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7647 location_t combined_loc
7648 = make_location (input_location, start_loc, end_loc);
7649 protected_set_expr_location (postfix_expression, combined_loc);
7653 /* We no longer need to look up names in the scope of the object on
7654 the left-hand side of the `.' or `->' operator. */
7655 parser->context->object_type = NULL_TREE;
7657 /* Outside of offsetof, these operators may not appear in
7658 constant-expressions. */
7659 if (!for_offsetof
7660 && (cp_parser_non_integral_constant_expression
7661 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7662 postfix_expression = error_mark_node;
7664 return postfix_expression;
7667 /* Parse a parenthesized expression-list.
7669 expression-list:
7670 assignment-expression
7671 expression-list, assignment-expression
7673 attribute-list:
7674 expression-list
7675 identifier
7676 identifier, expression-list
7678 CAST_P is true if this expression is the target of a cast.
7680 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7681 argument pack.
7683 WRAP_LOCATIONS_P is true if expressions within this list for which
7684 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7685 their source locations.
7687 Returns a vector of trees. Each element is a representation of an
7688 assignment-expression. NULL is returned if the ( and or ) are
7689 missing. An empty, but allocated, vector is returned on no
7690 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7691 if we are parsing an attribute list for an attribute that wants a
7692 plain identifier argument, normal_attr for an attribute that wants
7693 an expression, or non_attr if we aren't parsing an attribute list. If
7694 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7695 not all of the expressions in the list were constant.
7696 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7697 will be written to with the location of the closing parenthesis. If
7698 an error occurs, it may or may not be written to. */
7700 static vec<tree, va_gc> *
7701 cp_parser_parenthesized_expression_list (cp_parser* parser,
7702 int is_attribute_list,
7703 bool cast_p,
7704 bool allow_expansion_p,
7705 bool *non_constant_p,
7706 location_t *close_paren_loc,
7707 bool wrap_locations_p)
7709 vec<tree, va_gc> *expression_list;
7710 bool fold_expr_p = is_attribute_list != non_attr;
7711 tree identifier = NULL_TREE;
7712 bool saved_greater_than_is_operator_p;
7714 /* Assume all the expressions will be constant. */
7715 if (non_constant_p)
7716 *non_constant_p = false;
7718 matching_parens parens;
7719 if (!parens.require_open (parser))
7720 return NULL;
7722 expression_list = make_tree_vector ();
7724 /* Within a parenthesized expression, a `>' token is always
7725 the greater-than operator. */
7726 saved_greater_than_is_operator_p
7727 = parser->greater_than_is_operator_p;
7728 parser->greater_than_is_operator_p = true;
7730 cp_expr expr (NULL_TREE);
7732 /* Consume expressions until there are no more. */
7733 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7734 while (true)
7736 /* At the beginning of attribute lists, check to see if the
7737 next token is an identifier. */
7738 if (is_attribute_list == id_attr
7739 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7741 cp_token *token;
7743 /* Consume the identifier. */
7744 token = cp_lexer_consume_token (parser->lexer);
7745 /* Save the identifier. */
7746 identifier = token->u.value;
7748 else
7750 bool expr_non_constant_p;
7752 /* Parse the next assignment-expression. */
7753 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7755 /* A braced-init-list. */
7756 cp_lexer_set_source_position (parser->lexer);
7757 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7758 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7759 if (non_constant_p && expr_non_constant_p)
7760 *non_constant_p = true;
7762 else if (non_constant_p)
7764 expr = (cp_parser_constant_expression
7765 (parser, /*allow_non_constant_p=*/true,
7766 &expr_non_constant_p));
7767 if (expr_non_constant_p)
7768 *non_constant_p = true;
7770 else
7771 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7772 cast_p);
7774 if (fold_expr_p)
7775 expr = instantiate_non_dependent_expr (expr);
7777 /* If we have an ellipsis, then this is an expression
7778 expansion. */
7779 if (allow_expansion_p
7780 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7782 /* Consume the `...'. */
7783 cp_lexer_consume_token (parser->lexer);
7785 /* Build the argument pack. */
7786 expr = make_pack_expansion (expr);
7789 if (wrap_locations_p)
7790 expr.maybe_add_location_wrapper ();
7792 /* Add it to the list. We add error_mark_node
7793 expressions to the list, so that we can still tell if
7794 the correct form for a parenthesized expression-list
7795 is found. That gives better errors. */
7796 vec_safe_push (expression_list, expr.get_value ());
7798 if (expr == error_mark_node)
7799 goto skip_comma;
7802 /* After the first item, attribute lists look the same as
7803 expression lists. */
7804 is_attribute_list = non_attr;
7806 get_comma:;
7807 /* If the next token isn't a `,', then we are done. */
7808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7809 break;
7811 /* Otherwise, consume the `,' and keep going. */
7812 cp_lexer_consume_token (parser->lexer);
7815 if (close_paren_loc)
7816 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7818 if (!parens.require_close (parser))
7820 int ending;
7822 skip_comma:;
7823 /* We try and resync to an unnested comma, as that will give the
7824 user better diagnostics. */
7825 ending = cp_parser_skip_to_closing_parenthesis (parser,
7826 /*recovering=*/true,
7827 /*or_comma=*/true,
7828 /*consume_paren=*/true);
7829 if (ending < 0)
7830 goto get_comma;
7831 if (!ending)
7833 parser->greater_than_is_operator_p
7834 = saved_greater_than_is_operator_p;
7835 return NULL;
7839 parser->greater_than_is_operator_p
7840 = saved_greater_than_is_operator_p;
7842 if (identifier)
7843 vec_safe_insert (expression_list, 0, identifier);
7845 return expression_list;
7848 /* Parse a pseudo-destructor-name.
7850 pseudo-destructor-name:
7851 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7852 :: [opt] nested-name-specifier template template-id :: ~ type-name
7853 :: [opt] nested-name-specifier [opt] ~ type-name
7855 If either of the first two productions is used, sets *SCOPE to the
7856 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7857 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7858 or ERROR_MARK_NODE if the parse fails. */
7860 static void
7861 cp_parser_pseudo_destructor_name (cp_parser* parser,
7862 tree object,
7863 tree* scope,
7864 tree* type)
7866 bool nested_name_specifier_p;
7868 /* Handle ~auto. */
7869 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7870 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7871 && !type_dependent_expression_p (object))
7873 if (cxx_dialect < cxx14)
7874 pedwarn (input_location, 0,
7875 "%<~auto%> only available with "
7876 "-std=c++14 or -std=gnu++14");
7877 cp_lexer_consume_token (parser->lexer);
7878 cp_lexer_consume_token (parser->lexer);
7879 *scope = NULL_TREE;
7880 *type = TREE_TYPE (object);
7881 return;
7884 /* Assume that things will not work out. */
7885 *type = error_mark_node;
7887 /* Look for the optional `::' operator. */
7888 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7889 /* Look for the optional nested-name-specifier. */
7890 nested_name_specifier_p
7891 = (cp_parser_nested_name_specifier_opt (parser,
7892 /*typename_keyword_p=*/false,
7893 /*check_dependency_p=*/true,
7894 /*type_p=*/false,
7895 /*is_declaration=*/false)
7896 != NULL_TREE);
7897 /* Now, if we saw a nested-name-specifier, we might be doing the
7898 second production. */
7899 if (nested_name_specifier_p
7900 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7902 /* Consume the `template' keyword. */
7903 cp_lexer_consume_token (parser->lexer);
7904 /* Parse the template-id. */
7905 cp_parser_template_id (parser,
7906 /*template_keyword_p=*/true,
7907 /*check_dependency_p=*/false,
7908 class_type,
7909 /*is_declaration=*/true);
7910 /* Look for the `::' token. */
7911 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7913 /* If the next token is not a `~', then there might be some
7914 additional qualification. */
7915 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7917 /* At this point, we're looking for "type-name :: ~". The type-name
7918 must not be a class-name, since this is a pseudo-destructor. So,
7919 it must be either an enum-name, or a typedef-name -- both of which
7920 are just identifiers. So, we peek ahead to check that the "::"
7921 and "~" tokens are present; if they are not, then we can avoid
7922 calling type_name. */
7923 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7924 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7925 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7927 cp_parser_error (parser, "non-scalar type");
7928 return;
7931 /* Look for the type-name. */
7932 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7933 if (*scope == error_mark_node)
7934 return;
7936 /* Look for the `::' token. */
7937 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7939 else
7940 *scope = NULL_TREE;
7942 /* Look for the `~'. */
7943 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7945 /* Once we see the ~, this has to be a pseudo-destructor. */
7946 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7947 cp_parser_commit_to_topmost_tentative_parse (parser);
7949 /* Look for the type-name again. We are not responsible for
7950 checking that it matches the first type-name. */
7951 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7954 /* Parse a unary-expression.
7956 unary-expression:
7957 postfix-expression
7958 ++ cast-expression
7959 -- cast-expression
7960 unary-operator cast-expression
7961 sizeof unary-expression
7962 sizeof ( type-id )
7963 alignof ( type-id ) [C++0x]
7964 new-expression
7965 delete-expression
7967 GNU Extensions:
7969 unary-expression:
7970 __extension__ cast-expression
7971 __alignof__ unary-expression
7972 __alignof__ ( type-id )
7973 alignof unary-expression [C++0x]
7974 __real__ cast-expression
7975 __imag__ cast-expression
7976 && identifier
7977 sizeof ( type-id ) { initializer-list , [opt] }
7978 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7979 __alignof__ ( type-id ) { initializer-list , [opt] }
7981 ADDRESS_P is true iff the unary-expression is appearing as the
7982 operand of the `&' operator. CAST_P is true if this expression is
7983 the target of a cast.
7985 Returns a representation of the expression. */
7987 static cp_expr
7988 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7989 bool address_p, bool cast_p, bool decltype_p)
7991 cp_token *token;
7992 enum tree_code unary_operator;
7994 /* Peek at the next token. */
7995 token = cp_lexer_peek_token (parser->lexer);
7996 /* Some keywords give away the kind of expression. */
7997 if (token->type == CPP_KEYWORD)
7999 enum rid keyword = token->keyword;
8001 switch (keyword)
8003 case RID_ALIGNOF:
8004 case RID_SIZEOF:
8006 tree operand, ret;
8007 enum tree_code op;
8008 location_t start_loc = token->location;
8010 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8011 bool std_alignof = id_equal (token->u.value, "alignof");
8013 /* Consume the token. */
8014 cp_lexer_consume_token (parser->lexer);
8015 /* Parse the operand. */
8016 operand = cp_parser_sizeof_operand (parser, keyword);
8018 if (TYPE_P (operand))
8019 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8020 true);
8021 else
8023 /* ISO C++ defines alignof only with types, not with
8024 expressions. So pedwarn if alignof is used with a non-
8025 type expression. However, __alignof__ is ok. */
8026 if (std_alignof)
8027 pedwarn (token->location, OPT_Wpedantic,
8028 "ISO C++ does not allow %<alignof%> "
8029 "with a non-type");
8031 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8033 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8034 SIZEOF_EXPR with the original operand. */
8035 if (op == SIZEOF_EXPR && ret != error_mark_node)
8037 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8039 if (!processing_template_decl && TYPE_P (operand))
8041 ret = build_min (SIZEOF_EXPR, size_type_node,
8042 build1 (NOP_EXPR, operand,
8043 error_mark_node));
8044 SIZEOF_EXPR_TYPE_P (ret) = 1;
8046 else
8047 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8048 TREE_SIDE_EFFECTS (ret) = 0;
8049 TREE_READONLY (ret) = 1;
8053 /* Construct a location e.g. :
8054 alignof (expr)
8055 ^~~~~~~~~~~~~~
8056 with start == caret at the start of the "alignof"/"sizeof"
8057 token, with the endpoint at the final closing paren. */
8058 location_t finish_loc
8059 = cp_lexer_previous_token (parser->lexer)->location;
8060 location_t compound_loc
8061 = make_location (start_loc, start_loc, finish_loc);
8063 cp_expr ret_expr (ret);
8064 ret_expr.set_location (compound_loc);
8065 ret_expr = ret_expr.maybe_add_location_wrapper ();
8066 return ret_expr;
8069 case RID_NEW:
8070 return cp_parser_new_expression (parser);
8072 case RID_DELETE:
8073 return cp_parser_delete_expression (parser);
8075 case RID_EXTENSION:
8077 /* The saved value of the PEDANTIC flag. */
8078 int saved_pedantic;
8079 tree expr;
8081 /* Save away the PEDANTIC flag. */
8082 cp_parser_extension_opt (parser, &saved_pedantic);
8083 /* Parse the cast-expression. */
8084 expr = cp_parser_simple_cast_expression (parser);
8085 /* Restore the PEDANTIC flag. */
8086 pedantic = saved_pedantic;
8088 return expr;
8091 case RID_REALPART:
8092 case RID_IMAGPART:
8094 tree expression;
8096 /* Consume the `__real__' or `__imag__' token. */
8097 cp_lexer_consume_token (parser->lexer);
8098 /* Parse the cast-expression. */
8099 expression = cp_parser_simple_cast_expression (parser);
8100 /* Create the complete representation. */
8101 return build_x_unary_op (token->location,
8102 (keyword == RID_REALPART
8103 ? REALPART_EXPR : IMAGPART_EXPR),
8104 expression,
8105 tf_warning_or_error);
8107 break;
8109 case RID_TRANSACTION_ATOMIC:
8110 case RID_TRANSACTION_RELAXED:
8111 return cp_parser_transaction_expression (parser, keyword);
8113 case RID_NOEXCEPT:
8115 tree expr;
8116 const char *saved_message;
8117 bool saved_integral_constant_expression_p;
8118 bool saved_non_integral_constant_expression_p;
8119 bool saved_greater_than_is_operator_p;
8121 location_t start_loc = token->location;
8123 cp_lexer_consume_token (parser->lexer);
8124 matching_parens parens;
8125 parens.require_open (parser);
8127 saved_message = parser->type_definition_forbidden_message;
8128 parser->type_definition_forbidden_message
8129 = G_("types may not be defined in %<noexcept%> expressions");
8131 saved_integral_constant_expression_p
8132 = parser->integral_constant_expression_p;
8133 saved_non_integral_constant_expression_p
8134 = parser->non_integral_constant_expression_p;
8135 parser->integral_constant_expression_p = false;
8137 saved_greater_than_is_operator_p
8138 = parser->greater_than_is_operator_p;
8139 parser->greater_than_is_operator_p = true;
8141 ++cp_unevaluated_operand;
8142 ++c_inhibit_evaluation_warnings;
8143 ++cp_noexcept_operand;
8144 expr = cp_parser_expression (parser);
8145 --cp_noexcept_operand;
8146 --c_inhibit_evaluation_warnings;
8147 --cp_unevaluated_operand;
8149 parser->greater_than_is_operator_p
8150 = saved_greater_than_is_operator_p;
8152 parser->integral_constant_expression_p
8153 = saved_integral_constant_expression_p;
8154 parser->non_integral_constant_expression_p
8155 = saved_non_integral_constant_expression_p;
8157 parser->type_definition_forbidden_message = saved_message;
8159 location_t finish_loc
8160 = cp_lexer_peek_token (parser->lexer)->location;
8161 parens.require_close (parser);
8163 /* Construct a location of the form:
8164 noexcept (expr)
8165 ^~~~~~~~~~~~~~~
8166 with start == caret, finishing at the close-paren. */
8167 location_t noexcept_loc
8168 = make_location (start_loc, start_loc, finish_loc);
8170 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8171 noexcept_loc);
8174 default:
8175 break;
8179 /* Look for the `:: new' and `:: delete', which also signal the
8180 beginning of a new-expression, or delete-expression,
8181 respectively. If the next token is `::', then it might be one of
8182 these. */
8183 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8185 enum rid keyword;
8187 /* See if the token after the `::' is one of the keywords in
8188 which we're interested. */
8189 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8190 /* If it's `new', we have a new-expression. */
8191 if (keyword == RID_NEW)
8192 return cp_parser_new_expression (parser);
8193 /* Similarly, for `delete'. */
8194 else if (keyword == RID_DELETE)
8195 return cp_parser_delete_expression (parser);
8198 /* Look for a unary operator. */
8199 unary_operator = cp_parser_unary_operator (token);
8200 /* The `++' and `--' operators can be handled similarly, even though
8201 they are not technically unary-operators in the grammar. */
8202 if (unary_operator == ERROR_MARK)
8204 if (token->type == CPP_PLUS_PLUS)
8205 unary_operator = PREINCREMENT_EXPR;
8206 else if (token->type == CPP_MINUS_MINUS)
8207 unary_operator = PREDECREMENT_EXPR;
8208 /* Handle the GNU address-of-label extension. */
8209 else if (cp_parser_allow_gnu_extensions_p (parser)
8210 && token->type == CPP_AND_AND)
8212 tree identifier;
8213 tree expression;
8214 location_t start_loc = token->location;
8216 /* Consume the '&&' token. */
8217 cp_lexer_consume_token (parser->lexer);
8218 /* Look for the identifier. */
8219 location_t finish_loc
8220 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8221 identifier = cp_parser_identifier (parser);
8222 /* Construct a location of the form:
8223 &&label
8224 ^~~~~~~
8225 with caret==start at the "&&", finish at the end of the label. */
8226 location_t combined_loc
8227 = make_location (start_loc, start_loc, finish_loc);
8228 /* Create an expression representing the address. */
8229 expression = finish_label_address_expr (identifier, combined_loc);
8230 if (cp_parser_non_integral_constant_expression (parser,
8231 NIC_ADDR_LABEL))
8232 expression = error_mark_node;
8233 return expression;
8236 if (unary_operator != ERROR_MARK)
8238 cp_expr cast_expression;
8239 cp_expr expression = error_mark_node;
8240 non_integral_constant non_constant_p = NIC_NONE;
8241 location_t loc = token->location;
8242 tsubst_flags_t complain = complain_flags (decltype_p);
8244 /* Consume the operator token. */
8245 token = cp_lexer_consume_token (parser->lexer);
8246 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8248 /* Parse the cast-expression. */
8249 cast_expression
8250 = cp_parser_cast_expression (parser,
8251 unary_operator == ADDR_EXPR,
8252 /*cast_p=*/false,
8253 /*decltype*/false,
8254 pidk);
8256 /* Make a location:
8257 OP_TOKEN CAST_EXPRESSION
8258 ^~~~~~~~~~~~~~~~~~~~~~~~~
8259 with start==caret at the operator token, and
8260 extending to the end of the cast_expression. */
8261 loc = make_location (loc, loc, cast_expression.get_finish ());
8263 /* Now, build an appropriate representation. */
8264 switch (unary_operator)
8266 case INDIRECT_REF:
8267 non_constant_p = NIC_STAR;
8268 expression = build_x_indirect_ref (loc, cast_expression,
8269 RO_UNARY_STAR,
8270 complain);
8271 /* TODO: build_x_indirect_ref does not always honor the
8272 location, so ensure it is set. */
8273 expression.set_location (loc);
8274 break;
8276 case ADDR_EXPR:
8277 non_constant_p = NIC_ADDR;
8278 /* Fall through. */
8279 case BIT_NOT_EXPR:
8280 expression = build_x_unary_op (loc, unary_operator,
8281 cast_expression,
8282 complain);
8283 /* TODO: build_x_unary_op does not always honor the location,
8284 so ensure it is set. */
8285 expression.set_location (loc);
8286 break;
8288 case PREINCREMENT_EXPR:
8289 case PREDECREMENT_EXPR:
8290 non_constant_p = unary_operator == PREINCREMENT_EXPR
8291 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8292 /* Fall through. */
8293 case NEGATE_EXPR:
8294 /* Immediately fold negation of a constant, unless the constant is 0
8295 (since -0 == 0) or it would overflow. */
8296 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8297 && CONSTANT_CLASS_P (cast_expression)
8298 && !integer_zerop (cast_expression)
8299 && !TREE_OVERFLOW (cast_expression))
8301 tree folded = fold_build1 (unary_operator,
8302 TREE_TYPE (cast_expression),
8303 cast_expression);
8304 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8306 expression = cp_expr (folded, loc);
8307 break;
8310 /* Fall through. */
8311 case UNARY_PLUS_EXPR:
8312 case TRUTH_NOT_EXPR:
8313 expression = finish_unary_op_expr (loc, unary_operator,
8314 cast_expression, complain);
8315 break;
8317 default:
8318 gcc_unreachable ();
8321 if (non_constant_p != NIC_NONE
8322 && cp_parser_non_integral_constant_expression (parser,
8323 non_constant_p))
8324 expression = error_mark_node;
8326 return expression;
8329 return cp_parser_postfix_expression (parser, address_p, cast_p,
8330 /*member_access_only_p=*/false,
8331 decltype_p,
8332 pidk);
8335 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8336 unary-operator, the corresponding tree code is returned. */
8338 static enum tree_code
8339 cp_parser_unary_operator (cp_token* token)
8341 switch (token->type)
8343 case CPP_MULT:
8344 return INDIRECT_REF;
8346 case CPP_AND:
8347 return ADDR_EXPR;
8349 case CPP_PLUS:
8350 return UNARY_PLUS_EXPR;
8352 case CPP_MINUS:
8353 return NEGATE_EXPR;
8355 case CPP_NOT:
8356 return TRUTH_NOT_EXPR;
8358 case CPP_COMPL:
8359 return BIT_NOT_EXPR;
8361 default:
8362 return ERROR_MARK;
8366 /* Parse a new-expression.
8368 new-expression:
8369 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8370 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8372 Returns a representation of the expression. */
8374 static tree
8375 cp_parser_new_expression (cp_parser* parser)
8377 bool global_scope_p;
8378 vec<tree, va_gc> *placement;
8379 tree type;
8380 vec<tree, va_gc> *initializer;
8381 tree nelts = NULL_TREE;
8382 tree ret;
8384 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8386 /* Look for the optional `::' operator. */
8387 global_scope_p
8388 = (cp_parser_global_scope_opt (parser,
8389 /*current_scope_valid_p=*/false)
8390 != NULL_TREE);
8391 /* Look for the `new' operator. */
8392 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8393 /* There's no easy way to tell a new-placement from the
8394 `( type-id )' construct. */
8395 cp_parser_parse_tentatively (parser);
8396 /* Look for a new-placement. */
8397 placement = cp_parser_new_placement (parser);
8398 /* If that didn't work out, there's no new-placement. */
8399 if (!cp_parser_parse_definitely (parser))
8401 if (placement != NULL)
8402 release_tree_vector (placement);
8403 placement = NULL;
8406 /* If the next token is a `(', then we have a parenthesized
8407 type-id. */
8408 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8410 cp_token *token;
8411 const char *saved_message = parser->type_definition_forbidden_message;
8413 /* Consume the `('. */
8414 matching_parens parens;
8415 parens.consume_open (parser);
8417 /* Parse the type-id. */
8418 parser->type_definition_forbidden_message
8419 = G_("types may not be defined in a new-expression");
8421 type_id_in_expr_sentinel s (parser);
8422 type = cp_parser_type_id (parser);
8424 parser->type_definition_forbidden_message = saved_message;
8426 /* Look for the closing `)'. */
8427 parens.require_close (parser);
8428 token = cp_lexer_peek_token (parser->lexer);
8429 /* There should not be a direct-new-declarator in this production,
8430 but GCC used to allowed this, so we check and emit a sensible error
8431 message for this case. */
8432 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8434 error_at (token->location,
8435 "array bound forbidden after parenthesized type-id");
8436 inform (token->location,
8437 "try removing the parentheses around the type-id");
8438 cp_parser_direct_new_declarator (parser);
8441 /* Otherwise, there must be a new-type-id. */
8442 else
8443 type = cp_parser_new_type_id (parser, &nelts);
8445 /* If the next token is a `(' or '{', then we have a new-initializer. */
8446 cp_token *token = cp_lexer_peek_token (parser->lexer);
8447 if (token->type == CPP_OPEN_PAREN
8448 || token->type == CPP_OPEN_BRACE)
8449 initializer = cp_parser_new_initializer (parser);
8450 else
8451 initializer = NULL;
8453 /* A new-expression may not appear in an integral constant
8454 expression. */
8455 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8456 ret = error_mark_node;
8457 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8458 of a new-type-id or type-id of a new-expression, the new-expression shall
8459 contain a new-initializer of the form ( assignment-expression )".
8460 Additionally, consistently with the spirit of DR 1467, we want to accept
8461 'new auto { 2 }' too. */
8462 else if ((ret = type_uses_auto (type))
8463 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8464 && (vec_safe_length (initializer) != 1
8465 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8466 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8468 error_at (token->location,
8469 "initialization of new-expression for type %<auto%> "
8470 "requires exactly one element");
8471 ret = error_mark_node;
8473 else
8475 /* Construct a location e.g.:
8476 ptr = new int[100]
8477 ^~~~~~~~~~~~
8478 with caret == start at the start of the "new" token, and the end
8479 at the end of the final token we consumed. */
8480 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8481 location_t end_loc = get_finish (end_tok->location);
8482 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8484 /* Create a representation of the new-expression. */
8485 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8486 tf_warning_or_error);
8487 protected_set_expr_location (ret, combined_loc);
8490 if (placement != NULL)
8491 release_tree_vector (placement);
8492 if (initializer != NULL)
8493 release_tree_vector (initializer);
8495 return ret;
8498 /* Parse a new-placement.
8500 new-placement:
8501 ( expression-list )
8503 Returns the same representation as for an expression-list. */
8505 static vec<tree, va_gc> *
8506 cp_parser_new_placement (cp_parser* parser)
8508 vec<tree, va_gc> *expression_list;
8510 /* Parse the expression-list. */
8511 expression_list = (cp_parser_parenthesized_expression_list
8512 (parser, non_attr, /*cast_p=*/false,
8513 /*allow_expansion_p=*/true,
8514 /*non_constant_p=*/NULL));
8516 if (expression_list && expression_list->is_empty ())
8517 error ("expected expression-list or type-id");
8519 return expression_list;
8522 /* Parse a new-type-id.
8524 new-type-id:
8525 type-specifier-seq new-declarator [opt]
8527 Returns the TYPE allocated. If the new-type-id indicates an array
8528 type, *NELTS is set to the number of elements in the last array
8529 bound; the TYPE will not include the last array bound. */
8531 static tree
8532 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8534 cp_decl_specifier_seq type_specifier_seq;
8535 cp_declarator *new_declarator;
8536 cp_declarator *declarator;
8537 cp_declarator *outer_declarator;
8538 const char *saved_message;
8540 /* The type-specifier sequence must not contain type definitions.
8541 (It cannot contain declarations of new types either, but if they
8542 are not definitions we will catch that because they are not
8543 complete.) */
8544 saved_message = parser->type_definition_forbidden_message;
8545 parser->type_definition_forbidden_message
8546 = G_("types may not be defined in a new-type-id");
8547 /* Parse the type-specifier-seq. */
8548 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8549 /*is_trailing_return=*/false,
8550 &type_specifier_seq);
8551 /* Restore the old message. */
8552 parser->type_definition_forbidden_message = saved_message;
8554 if (type_specifier_seq.type == error_mark_node)
8555 return error_mark_node;
8557 /* Parse the new-declarator. */
8558 new_declarator = cp_parser_new_declarator_opt (parser);
8560 /* Determine the number of elements in the last array dimension, if
8561 any. */
8562 *nelts = NULL_TREE;
8563 /* Skip down to the last array dimension. */
8564 declarator = new_declarator;
8565 outer_declarator = NULL;
8566 while (declarator && (declarator->kind == cdk_pointer
8567 || declarator->kind == cdk_ptrmem))
8569 outer_declarator = declarator;
8570 declarator = declarator->declarator;
8572 while (declarator
8573 && declarator->kind == cdk_array
8574 && declarator->declarator
8575 && declarator->declarator->kind == cdk_array)
8577 outer_declarator = declarator;
8578 declarator = declarator->declarator;
8581 if (declarator && declarator->kind == cdk_array)
8583 *nelts = declarator->u.array.bounds;
8584 if (*nelts == error_mark_node)
8585 *nelts = integer_one_node;
8587 if (outer_declarator)
8588 outer_declarator->declarator = declarator->declarator;
8589 else
8590 new_declarator = NULL;
8593 return groktypename (&type_specifier_seq, new_declarator, false);
8596 /* Parse an (optional) new-declarator.
8598 new-declarator:
8599 ptr-operator new-declarator [opt]
8600 direct-new-declarator
8602 Returns the declarator. */
8604 static cp_declarator *
8605 cp_parser_new_declarator_opt (cp_parser* parser)
8607 enum tree_code code;
8608 tree type, std_attributes = NULL_TREE;
8609 cp_cv_quals cv_quals;
8611 /* We don't know if there's a ptr-operator next, or not. */
8612 cp_parser_parse_tentatively (parser);
8613 /* Look for a ptr-operator. */
8614 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8615 /* If that worked, look for more new-declarators. */
8616 if (cp_parser_parse_definitely (parser))
8618 cp_declarator *declarator;
8620 /* Parse another optional declarator. */
8621 declarator = cp_parser_new_declarator_opt (parser);
8623 declarator = cp_parser_make_indirect_declarator
8624 (code, type, cv_quals, declarator, std_attributes);
8626 return declarator;
8629 /* If the next token is a `[', there is a direct-new-declarator. */
8630 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8631 return cp_parser_direct_new_declarator (parser);
8633 return NULL;
8636 /* Parse a direct-new-declarator.
8638 direct-new-declarator:
8639 [ expression ]
8640 direct-new-declarator [constant-expression]
8644 static cp_declarator *
8645 cp_parser_direct_new_declarator (cp_parser* parser)
8647 cp_declarator *declarator = NULL;
8649 while (true)
8651 tree expression;
8652 cp_token *token;
8654 /* Look for the opening `['. */
8655 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8657 token = cp_lexer_peek_token (parser->lexer);
8658 expression = cp_parser_expression (parser);
8659 /* The standard requires that the expression have integral
8660 type. DR 74 adds enumeration types. We believe that the
8661 real intent is that these expressions be handled like the
8662 expression in a `switch' condition, which also allows
8663 classes with a single conversion to integral or
8664 enumeration type. */
8665 if (!processing_template_decl)
8667 expression
8668 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8669 expression,
8670 /*complain=*/true);
8671 if (!expression)
8673 error_at (token->location,
8674 "expression in new-declarator must have integral "
8675 "or enumeration type");
8676 expression = error_mark_node;
8680 /* Look for the closing `]'. */
8681 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8683 /* Add this bound to the declarator. */
8684 declarator = make_array_declarator (declarator, expression);
8686 /* If the next token is not a `[', then there are no more
8687 bounds. */
8688 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8689 break;
8692 return declarator;
8695 /* Parse a new-initializer.
8697 new-initializer:
8698 ( expression-list [opt] )
8699 braced-init-list
8701 Returns a representation of the expression-list. */
8703 static vec<tree, va_gc> *
8704 cp_parser_new_initializer (cp_parser* parser)
8706 vec<tree, va_gc> *expression_list;
8708 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8710 tree t;
8711 bool expr_non_constant_p;
8712 cp_lexer_set_source_position (parser->lexer);
8713 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8714 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8715 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8716 expression_list = make_tree_vector_single (t);
8718 else
8719 expression_list = (cp_parser_parenthesized_expression_list
8720 (parser, non_attr, /*cast_p=*/false,
8721 /*allow_expansion_p=*/true,
8722 /*non_constant_p=*/NULL));
8724 return expression_list;
8727 /* Parse a delete-expression.
8729 delete-expression:
8730 :: [opt] delete cast-expression
8731 :: [opt] delete [ ] cast-expression
8733 Returns a representation of the expression. */
8735 static tree
8736 cp_parser_delete_expression (cp_parser* parser)
8738 bool global_scope_p;
8739 bool array_p;
8740 tree expression;
8742 /* Look for the optional `::' operator. */
8743 global_scope_p
8744 = (cp_parser_global_scope_opt (parser,
8745 /*current_scope_valid_p=*/false)
8746 != NULL_TREE);
8747 /* Look for the `delete' keyword. */
8748 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8749 /* See if the array syntax is in use. */
8750 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8752 /* Consume the `[' token. */
8753 cp_lexer_consume_token (parser->lexer);
8754 /* Look for the `]' token. */
8755 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8756 /* Remember that this is the `[]' construct. */
8757 array_p = true;
8759 else
8760 array_p = false;
8762 /* Parse the cast-expression. */
8763 expression = cp_parser_simple_cast_expression (parser);
8765 /* A delete-expression may not appear in an integral constant
8766 expression. */
8767 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8768 return error_mark_node;
8770 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8771 tf_warning_or_error);
8774 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8775 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8776 0 otherwise. */
8778 static int
8779 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8781 cp_token *token = cp_lexer_peek_token (parser->lexer);
8782 switch (token->type)
8784 case CPP_COMMA:
8785 case CPP_SEMICOLON:
8786 case CPP_QUERY:
8787 case CPP_COLON:
8788 case CPP_CLOSE_SQUARE:
8789 case CPP_CLOSE_PAREN:
8790 case CPP_CLOSE_BRACE:
8791 case CPP_OPEN_BRACE:
8792 case CPP_DOT:
8793 case CPP_DOT_STAR:
8794 case CPP_DEREF:
8795 case CPP_DEREF_STAR:
8796 case CPP_DIV:
8797 case CPP_MOD:
8798 case CPP_LSHIFT:
8799 case CPP_RSHIFT:
8800 case CPP_LESS:
8801 case CPP_GREATER:
8802 case CPP_LESS_EQ:
8803 case CPP_GREATER_EQ:
8804 case CPP_EQ_EQ:
8805 case CPP_NOT_EQ:
8806 case CPP_EQ:
8807 case CPP_MULT_EQ:
8808 case CPP_DIV_EQ:
8809 case CPP_MOD_EQ:
8810 case CPP_PLUS_EQ:
8811 case CPP_MINUS_EQ:
8812 case CPP_RSHIFT_EQ:
8813 case CPP_LSHIFT_EQ:
8814 case CPP_AND_EQ:
8815 case CPP_XOR_EQ:
8816 case CPP_OR_EQ:
8817 case CPP_XOR:
8818 case CPP_OR:
8819 case CPP_OR_OR:
8820 case CPP_EOF:
8821 case CPP_ELLIPSIS:
8822 return 0;
8824 case CPP_OPEN_PAREN:
8825 /* In ((type ()) () the last () isn't a valid cast-expression,
8826 so the whole must be parsed as postfix-expression. */
8827 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8828 != CPP_CLOSE_PAREN;
8830 case CPP_OPEN_SQUARE:
8831 /* '[' may start a primary-expression in obj-c++ and in C++11,
8832 as a lambda-expression, eg, '(void)[]{}'. */
8833 if (cxx_dialect >= cxx11)
8834 return -1;
8835 return c_dialect_objc ();
8837 case CPP_PLUS_PLUS:
8838 case CPP_MINUS_MINUS:
8839 /* '++' and '--' may or may not start a cast-expression:
8841 struct T { void operator++(int); };
8842 void f() { (T())++; }
8846 int a;
8847 (int)++a; */
8848 return -1;
8850 default:
8851 return 1;
8855 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8856 in the order: const_cast, static_cast, reinterpret_cast.
8858 Don't suggest dynamic_cast.
8860 Return the first legal cast kind found, or NULL otherwise. */
8862 static const char *
8863 get_cast_suggestion (tree dst_type, tree orig_expr)
8865 tree trial;
8867 /* Reuse the parser logic by attempting to build the various kinds of
8868 cast, with "complain" disabled.
8869 Identify the first such cast that is valid. */
8871 /* Don't attempt to run such logic within template processing. */
8872 if (processing_template_decl)
8873 return NULL;
8875 /* First try const_cast. */
8876 trial = build_const_cast (dst_type, orig_expr, tf_none);
8877 if (trial != error_mark_node)
8878 return "const_cast";
8880 /* If that fails, try static_cast. */
8881 trial = build_static_cast (dst_type, orig_expr, tf_none);
8882 if (trial != error_mark_node)
8883 return "static_cast";
8885 /* Finally, try reinterpret_cast. */
8886 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8887 if (trial != error_mark_node)
8888 return "reinterpret_cast";
8890 /* No such cast possible. */
8891 return NULL;
8894 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8895 suggesting how to convert a C-style cast of the form:
8897 (DST_TYPE)ORIG_EXPR
8899 to a C++-style cast.
8901 The primary range of RICHLOC is asssumed to be that of the original
8902 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8903 of the parens in the C-style cast. */
8905 static void
8906 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8907 location_t close_paren_loc, tree orig_expr,
8908 tree dst_type)
8910 /* This function is non-trivial, so bail out now if the warning isn't
8911 going to be emitted. */
8912 if (!warn_old_style_cast)
8913 return;
8915 /* Try to find a legal C++ cast, trying them in order:
8916 const_cast, static_cast, reinterpret_cast. */
8917 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8918 if (!cast_suggestion)
8919 return;
8921 /* Replace the open paren with "CAST_SUGGESTION<". */
8922 pretty_printer pp;
8923 pp_printf (&pp, "%s<", cast_suggestion);
8924 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8926 /* Replace the close paren with "> (". */
8927 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8929 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8930 rich_loc->add_fixit_insert_after (")");
8934 /* Parse a cast-expression.
8936 cast-expression:
8937 unary-expression
8938 ( type-id ) cast-expression
8940 ADDRESS_P is true iff the unary-expression is appearing as the
8941 operand of the `&' operator. CAST_P is true if this expression is
8942 the target of a cast.
8944 Returns a representation of the expression. */
8946 static cp_expr
8947 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8948 bool decltype_p, cp_id_kind * pidk)
8950 /* If it's a `(', then we might be looking at a cast. */
8951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8953 tree type = NULL_TREE;
8954 cp_expr expr (NULL_TREE);
8955 int cast_expression = 0;
8956 const char *saved_message;
8958 /* There's no way to know yet whether or not this is a cast.
8959 For example, `(int (3))' is a unary-expression, while `(int)
8960 3' is a cast. So, we resort to parsing tentatively. */
8961 cp_parser_parse_tentatively (parser);
8962 /* Types may not be defined in a cast. */
8963 saved_message = parser->type_definition_forbidden_message;
8964 parser->type_definition_forbidden_message
8965 = G_("types may not be defined in casts");
8966 /* Consume the `('. */
8967 matching_parens parens;
8968 cp_token *open_paren = parens.consume_open (parser);
8969 location_t open_paren_loc = open_paren->location;
8970 location_t close_paren_loc = UNKNOWN_LOCATION;
8972 /* A very tricky bit is that `(struct S) { 3 }' is a
8973 compound-literal (which we permit in C++ as an extension).
8974 But, that construct is not a cast-expression -- it is a
8975 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8976 is legal; if the compound-literal were a cast-expression,
8977 you'd need an extra set of parentheses.) But, if we parse
8978 the type-id, and it happens to be a class-specifier, then we
8979 will commit to the parse at that point, because we cannot
8980 undo the action that is done when creating a new class. So,
8981 then we cannot back up and do a postfix-expression.
8983 Another tricky case is the following (c++/29234):
8985 struct S { void operator () (); };
8987 void foo ()
8989 ( S()() );
8992 As a type-id we parse the parenthesized S()() as a function
8993 returning a function, groktypename complains and we cannot
8994 back up in this case either.
8996 Therefore, we scan ahead to the closing `)', and check to see
8997 if the tokens after the `)' can start a cast-expression. Otherwise
8998 we are dealing with an unary-expression, a postfix-expression
8999 or something else.
9001 Yet another tricky case, in C++11, is the following (c++/54891):
9003 (void)[]{};
9005 The issue is that usually, besides the case of lambda-expressions,
9006 the parenthesized type-id cannot be followed by '[', and, eg, we
9007 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9008 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9009 we don't commit, we try a cast-expression, then an unary-expression.
9011 Save tokens so that we can put them back. */
9012 cp_lexer_save_tokens (parser->lexer);
9014 /* We may be looking at a cast-expression. */
9015 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9016 /*consume_paren=*/true))
9017 cast_expression
9018 = cp_parser_tokens_start_cast_expression (parser);
9020 /* Roll back the tokens we skipped. */
9021 cp_lexer_rollback_tokens (parser->lexer);
9022 /* If we aren't looking at a cast-expression, simulate an error so
9023 that the call to cp_parser_error_occurred below returns true. */
9024 if (!cast_expression)
9025 cp_parser_simulate_error (parser);
9026 else
9028 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9029 parser->in_type_id_in_expr_p = true;
9030 /* Look for the type-id. */
9031 type = cp_parser_type_id (parser);
9032 /* Look for the closing `)'. */
9033 cp_token *close_paren = parens.require_close (parser);
9034 if (close_paren)
9035 close_paren_loc = close_paren->location;
9036 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9039 /* Restore the saved message. */
9040 parser->type_definition_forbidden_message = saved_message;
9042 /* At this point this can only be either a cast or a
9043 parenthesized ctor such as `(T ())' that looks like a cast to
9044 function returning T. */
9045 if (!cp_parser_error_occurred (parser))
9047 /* Only commit if the cast-expression doesn't start with
9048 '++', '--', or '[' in C++11. */
9049 if (cast_expression > 0)
9050 cp_parser_commit_to_topmost_tentative_parse (parser);
9052 expr = cp_parser_cast_expression (parser,
9053 /*address_p=*/false,
9054 /*cast_p=*/true,
9055 /*decltype_p=*/false,
9056 pidk);
9058 if (cp_parser_parse_definitely (parser))
9060 /* Warn about old-style casts, if so requested. */
9061 if (warn_old_style_cast
9062 && !in_system_header_at (input_location)
9063 && !VOID_TYPE_P (type)
9064 && current_lang_name != lang_name_c)
9066 gcc_rich_location rich_loc (input_location);
9067 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9068 expr, type);
9069 warning_at (&rich_loc, OPT_Wold_style_cast,
9070 "use of old-style cast to %q#T", type);
9073 /* Only type conversions to integral or enumeration types
9074 can be used in constant-expressions. */
9075 if (!cast_valid_in_integral_constant_expression_p (type)
9076 && cp_parser_non_integral_constant_expression (parser,
9077 NIC_CAST))
9078 return error_mark_node;
9080 /* Perform the cast. */
9081 /* Make a location:
9082 (TYPE) EXPR
9083 ^~~~~~~~~~~
9084 with start==caret at the open paren, extending to the
9085 end of "expr". */
9086 location_t cast_loc = make_location (open_paren_loc,
9087 open_paren_loc,
9088 expr.get_finish ());
9089 expr = build_c_cast (cast_loc, type, expr);
9090 return expr;
9093 else
9094 cp_parser_abort_tentative_parse (parser);
9097 /* If we get here, then it's not a cast, so it must be a
9098 unary-expression. */
9099 return cp_parser_unary_expression (parser, pidk, address_p,
9100 cast_p, decltype_p);
9103 /* Parse a binary expression of the general form:
9105 pm-expression:
9106 cast-expression
9107 pm-expression .* cast-expression
9108 pm-expression ->* cast-expression
9110 multiplicative-expression:
9111 pm-expression
9112 multiplicative-expression * pm-expression
9113 multiplicative-expression / pm-expression
9114 multiplicative-expression % pm-expression
9116 additive-expression:
9117 multiplicative-expression
9118 additive-expression + multiplicative-expression
9119 additive-expression - multiplicative-expression
9121 shift-expression:
9122 additive-expression
9123 shift-expression << additive-expression
9124 shift-expression >> additive-expression
9126 relational-expression:
9127 shift-expression
9128 relational-expression < shift-expression
9129 relational-expression > shift-expression
9130 relational-expression <= shift-expression
9131 relational-expression >= shift-expression
9133 GNU Extension:
9135 relational-expression:
9136 relational-expression <? shift-expression
9137 relational-expression >? shift-expression
9139 equality-expression:
9140 relational-expression
9141 equality-expression == relational-expression
9142 equality-expression != relational-expression
9144 and-expression:
9145 equality-expression
9146 and-expression & equality-expression
9148 exclusive-or-expression:
9149 and-expression
9150 exclusive-or-expression ^ and-expression
9152 inclusive-or-expression:
9153 exclusive-or-expression
9154 inclusive-or-expression | exclusive-or-expression
9156 logical-and-expression:
9157 inclusive-or-expression
9158 logical-and-expression && inclusive-or-expression
9160 logical-or-expression:
9161 logical-and-expression
9162 logical-or-expression || logical-and-expression
9164 All these are implemented with a single function like:
9166 binary-expression:
9167 simple-cast-expression
9168 binary-expression <token> binary-expression
9170 CAST_P is true if this expression is the target of a cast.
9172 The binops_by_token map is used to get the tree codes for each <token> type.
9173 binary-expressions are associated according to a precedence table. */
9175 #define TOKEN_PRECEDENCE(token) \
9176 (((token->type == CPP_GREATER \
9177 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9178 && !parser->greater_than_is_operator_p) \
9179 ? PREC_NOT_OPERATOR \
9180 : binops_by_token[token->type].prec)
9182 static cp_expr
9183 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9184 bool no_toplevel_fold_p,
9185 bool decltype_p,
9186 enum cp_parser_prec prec,
9187 cp_id_kind * pidk)
9189 cp_parser_expression_stack stack;
9190 cp_parser_expression_stack_entry *sp = &stack[0];
9191 cp_parser_expression_stack_entry current;
9192 cp_expr rhs;
9193 cp_token *token;
9194 enum tree_code rhs_type;
9195 enum cp_parser_prec new_prec, lookahead_prec;
9196 tree overload;
9198 /* Parse the first expression. */
9199 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9200 ? TRUTH_NOT_EXPR : ERROR_MARK);
9201 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9202 cast_p, decltype_p, pidk);
9203 current.prec = prec;
9205 if (cp_parser_error_occurred (parser))
9206 return error_mark_node;
9208 for (;;)
9210 /* Get an operator token. */
9211 token = cp_lexer_peek_token (parser->lexer);
9213 if (warn_cxx11_compat
9214 && token->type == CPP_RSHIFT
9215 && !parser->greater_than_is_operator_p)
9217 if (warning_at (token->location, OPT_Wc__11_compat,
9218 "%<>>%> operator is treated"
9219 " as two right angle brackets in C++11"))
9220 inform (token->location,
9221 "suggest parentheses around %<>>%> expression");
9224 new_prec = TOKEN_PRECEDENCE (token);
9225 if (new_prec != PREC_NOT_OPERATOR
9226 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9227 /* This is a fold-expression; handle it later. */
9228 new_prec = PREC_NOT_OPERATOR;
9230 /* Popping an entry off the stack means we completed a subexpression:
9231 - either we found a token which is not an operator (`>' where it is not
9232 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9233 will happen repeatedly;
9234 - or, we found an operator which has lower priority. This is the case
9235 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9236 parsing `3 * 4'. */
9237 if (new_prec <= current.prec)
9239 if (sp == stack)
9240 break;
9241 else
9242 goto pop;
9245 get_rhs:
9246 current.tree_type = binops_by_token[token->type].tree_type;
9247 current.loc = token->location;
9249 /* We used the operator token. */
9250 cp_lexer_consume_token (parser->lexer);
9252 /* For "false && x" or "true || x", x will never be executed;
9253 disable warnings while evaluating it. */
9254 if (current.tree_type == TRUTH_ANDIF_EXPR)
9255 c_inhibit_evaluation_warnings +=
9256 cp_fully_fold (current.lhs) == truthvalue_false_node;
9257 else if (current.tree_type == TRUTH_ORIF_EXPR)
9258 c_inhibit_evaluation_warnings +=
9259 cp_fully_fold (current.lhs) == truthvalue_true_node;
9261 /* Extract another operand. It may be the RHS of this expression
9262 or the LHS of a new, higher priority expression. */
9263 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9264 ? TRUTH_NOT_EXPR : ERROR_MARK);
9265 rhs = cp_parser_simple_cast_expression (parser);
9267 /* Get another operator token. Look up its precedence to avoid
9268 building a useless (immediately popped) stack entry for common
9269 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9270 token = cp_lexer_peek_token (parser->lexer);
9271 lookahead_prec = TOKEN_PRECEDENCE (token);
9272 if (lookahead_prec != PREC_NOT_OPERATOR
9273 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9274 lookahead_prec = PREC_NOT_OPERATOR;
9275 if (lookahead_prec > new_prec)
9277 /* ... and prepare to parse the RHS of the new, higher priority
9278 expression. Since precedence levels on the stack are
9279 monotonically increasing, we do not have to care about
9280 stack overflows. */
9281 *sp = current;
9282 ++sp;
9283 current.lhs = rhs;
9284 current.lhs_type = rhs_type;
9285 current.prec = new_prec;
9286 new_prec = lookahead_prec;
9287 goto get_rhs;
9289 pop:
9290 lookahead_prec = new_prec;
9291 /* If the stack is not empty, we have parsed into LHS the right side
9292 (`4' in the example above) of an expression we had suspended.
9293 We can use the information on the stack to recover the LHS (`3')
9294 from the stack together with the tree code (`MULT_EXPR'), and
9295 the precedence of the higher level subexpression
9296 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9297 which will be used to actually build the additive expression. */
9298 rhs = current.lhs;
9299 rhs_type = current.lhs_type;
9300 --sp;
9301 current = *sp;
9304 /* Undo the disabling of warnings done above. */
9305 if (current.tree_type == TRUTH_ANDIF_EXPR)
9306 c_inhibit_evaluation_warnings -=
9307 cp_fully_fold (current.lhs) == truthvalue_false_node;
9308 else if (current.tree_type == TRUTH_ORIF_EXPR)
9309 c_inhibit_evaluation_warnings -=
9310 cp_fully_fold (current.lhs) == truthvalue_true_node;
9312 if (warn_logical_not_paren
9313 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9314 && current.lhs_type == TRUTH_NOT_EXPR
9315 /* Avoid warning for !!x == y. */
9316 && (TREE_CODE (current.lhs) != NE_EXPR
9317 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9318 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9319 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9320 /* Avoid warning for !b == y where b is boolean. */
9321 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9322 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9323 != BOOLEAN_TYPE))))
9324 /* Avoid warning for !!b == y where b is boolean. */
9325 && (!DECL_P (current.lhs)
9326 || TREE_TYPE (current.lhs) == NULL_TREE
9327 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9328 warn_logical_not_parentheses (current.loc, current.tree_type,
9329 current.lhs, maybe_constant_value (rhs));
9331 overload = NULL;
9333 location_t combined_loc = make_location (current.loc,
9334 current.lhs.get_start (),
9335 rhs.get_finish ());
9337 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9338 ERROR_MARK for everything that is not a binary expression.
9339 This makes warn_about_parentheses miss some warnings that
9340 involve unary operators. For unary expressions we should
9341 pass the correct tree_code unless the unary expression was
9342 surrounded by parentheses.
9344 if (no_toplevel_fold_p
9345 && lookahead_prec <= current.prec
9346 && sp == stack)
9348 if (current.lhs == error_mark_node || rhs == error_mark_node)
9349 current.lhs = error_mark_node;
9350 else
9352 current.lhs
9353 = build_min (current.tree_type,
9354 TREE_CODE_CLASS (current.tree_type)
9355 == tcc_comparison
9356 ? boolean_type_node : TREE_TYPE (current.lhs),
9357 current.lhs.get_value (), rhs.get_value ());
9358 SET_EXPR_LOCATION (current.lhs, combined_loc);
9361 else
9363 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9364 current.lhs, current.lhs_type,
9365 rhs, rhs_type, &overload,
9366 complain_flags (decltype_p));
9367 /* TODO: build_x_binary_op doesn't always honor the location. */
9368 current.lhs.set_location (combined_loc);
9370 current.lhs_type = current.tree_type;
9372 /* If the binary operator required the use of an overloaded operator,
9373 then this expression cannot be an integral constant-expression.
9374 An overloaded operator can be used even if both operands are
9375 otherwise permissible in an integral constant-expression if at
9376 least one of the operands is of enumeration type. */
9378 if (overload
9379 && cp_parser_non_integral_constant_expression (parser,
9380 NIC_OVERLOADED))
9381 return error_mark_node;
9384 return current.lhs;
9387 static cp_expr
9388 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9389 bool no_toplevel_fold_p,
9390 enum cp_parser_prec prec,
9391 cp_id_kind * pidk)
9393 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9394 /*decltype*/false, prec, pidk);
9397 /* Parse the `? expression : assignment-expression' part of a
9398 conditional-expression. The LOGICAL_OR_EXPR is the
9399 logical-or-expression that started the conditional-expression.
9400 Returns a representation of the entire conditional-expression.
9402 This routine is used by cp_parser_assignment_expression.
9404 ? expression : assignment-expression
9406 GNU Extensions:
9408 ? : assignment-expression */
9410 static tree
9411 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9413 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9414 cp_expr assignment_expr;
9415 struct cp_token *token;
9416 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9418 /* Consume the `?' token. */
9419 cp_lexer_consume_token (parser->lexer);
9420 token = cp_lexer_peek_token (parser->lexer);
9421 if (cp_parser_allow_gnu_extensions_p (parser)
9422 && token->type == CPP_COLON)
9424 pedwarn (token->location, OPT_Wpedantic,
9425 "ISO C++ does not allow ?: with omitted middle operand");
9426 /* Implicit true clause. */
9427 expr = NULL_TREE;
9428 c_inhibit_evaluation_warnings +=
9429 folded_logical_or_expr == truthvalue_true_node;
9430 warn_for_omitted_condop (token->location, logical_or_expr);
9432 else
9434 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9435 parser->colon_corrects_to_scope_p = false;
9436 /* Parse the expression. */
9437 c_inhibit_evaluation_warnings +=
9438 folded_logical_or_expr == truthvalue_false_node;
9439 expr = cp_parser_expression (parser);
9440 c_inhibit_evaluation_warnings +=
9441 ((folded_logical_or_expr == truthvalue_true_node)
9442 - (folded_logical_or_expr == truthvalue_false_node));
9443 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9446 /* The next token should be a `:'. */
9447 cp_parser_require (parser, CPP_COLON, RT_COLON);
9448 /* Parse the assignment-expression. */
9449 assignment_expr = cp_parser_assignment_expression (parser);
9450 c_inhibit_evaluation_warnings -=
9451 folded_logical_or_expr == truthvalue_true_node;
9453 /* Make a location:
9454 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9455 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9456 with the caret at the "?", ranging from the start of
9457 the logical_or_expr to the end of the assignment_expr. */
9458 loc = make_location (loc,
9459 logical_or_expr.get_start (),
9460 assignment_expr.get_finish ());
9462 /* Build the conditional-expression. */
9463 return build_x_conditional_expr (loc, logical_or_expr,
9464 expr,
9465 assignment_expr,
9466 tf_warning_or_error);
9469 /* Parse an assignment-expression.
9471 assignment-expression:
9472 conditional-expression
9473 logical-or-expression assignment-operator assignment_expression
9474 throw-expression
9476 CAST_P is true if this expression is the target of a cast.
9477 DECLTYPE_P is true if this expression is the operand of decltype.
9479 Returns a representation for the expression. */
9481 static cp_expr
9482 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9483 bool cast_p, bool decltype_p)
9485 cp_expr expr;
9487 /* If the next token is the `throw' keyword, then we're looking at
9488 a throw-expression. */
9489 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9490 expr = cp_parser_throw_expression (parser);
9491 /* Otherwise, it must be that we are looking at a
9492 logical-or-expression. */
9493 else
9495 /* Parse the binary expressions (logical-or-expression). */
9496 expr = cp_parser_binary_expression (parser, cast_p, false,
9497 decltype_p,
9498 PREC_NOT_OPERATOR, pidk);
9499 /* If the next token is a `?' then we're actually looking at a
9500 conditional-expression. */
9501 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9502 return cp_parser_question_colon_clause (parser, expr);
9503 else
9505 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9507 /* If it's an assignment-operator, we're using the second
9508 production. */
9509 enum tree_code assignment_operator
9510 = cp_parser_assignment_operator_opt (parser);
9511 if (assignment_operator != ERROR_MARK)
9513 bool non_constant_p;
9515 /* Parse the right-hand side of the assignment. */
9516 cp_expr rhs = cp_parser_initializer_clause (parser,
9517 &non_constant_p);
9519 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9520 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9522 /* An assignment may not appear in a
9523 constant-expression. */
9524 if (cp_parser_non_integral_constant_expression (parser,
9525 NIC_ASSIGNMENT))
9526 return error_mark_node;
9527 /* Build the assignment expression. Its default
9528 location:
9529 LHS = RHS
9530 ~~~~^~~~~
9531 is the location of the '=' token as the
9532 caret, ranging from the start of the lhs to the
9533 end of the rhs. */
9534 loc = make_location (loc,
9535 expr.get_start (),
9536 rhs.get_finish ());
9537 expr = build_x_modify_expr (loc, expr,
9538 assignment_operator,
9539 rhs,
9540 complain_flags (decltype_p));
9541 /* TODO: build_x_modify_expr doesn't honor the location,
9542 so we must set it here. */
9543 expr.set_location (loc);
9548 return expr;
9551 /* Parse an (optional) assignment-operator.
9553 assignment-operator: one of
9554 = *= /= %= += -= >>= <<= &= ^= |=
9556 GNU Extension:
9558 assignment-operator: one of
9559 <?= >?=
9561 If the next token is an assignment operator, the corresponding tree
9562 code is returned, and the token is consumed. For example, for
9563 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9564 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9565 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9566 operator, ERROR_MARK is returned. */
9568 static enum tree_code
9569 cp_parser_assignment_operator_opt (cp_parser* parser)
9571 enum tree_code op;
9572 cp_token *token;
9574 /* Peek at the next token. */
9575 token = cp_lexer_peek_token (parser->lexer);
9577 switch (token->type)
9579 case CPP_EQ:
9580 op = NOP_EXPR;
9581 break;
9583 case CPP_MULT_EQ:
9584 op = MULT_EXPR;
9585 break;
9587 case CPP_DIV_EQ:
9588 op = TRUNC_DIV_EXPR;
9589 break;
9591 case CPP_MOD_EQ:
9592 op = TRUNC_MOD_EXPR;
9593 break;
9595 case CPP_PLUS_EQ:
9596 op = PLUS_EXPR;
9597 break;
9599 case CPP_MINUS_EQ:
9600 op = MINUS_EXPR;
9601 break;
9603 case CPP_RSHIFT_EQ:
9604 op = RSHIFT_EXPR;
9605 break;
9607 case CPP_LSHIFT_EQ:
9608 op = LSHIFT_EXPR;
9609 break;
9611 case CPP_AND_EQ:
9612 op = BIT_AND_EXPR;
9613 break;
9615 case CPP_XOR_EQ:
9616 op = BIT_XOR_EXPR;
9617 break;
9619 case CPP_OR_EQ:
9620 op = BIT_IOR_EXPR;
9621 break;
9623 default:
9624 /* Nothing else is an assignment operator. */
9625 op = ERROR_MARK;
9628 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9629 if (op != ERROR_MARK
9630 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9631 op = ERROR_MARK;
9633 /* If it was an assignment operator, consume it. */
9634 if (op != ERROR_MARK)
9635 cp_lexer_consume_token (parser->lexer);
9637 return op;
9640 /* Parse an expression.
9642 expression:
9643 assignment-expression
9644 expression , assignment-expression
9646 CAST_P is true if this expression is the target of a cast.
9647 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9648 except possibly parenthesized or on the RHS of a comma (N3276).
9650 Returns a representation of the expression. */
9652 static cp_expr
9653 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9654 bool cast_p, bool decltype_p)
9656 cp_expr expression = NULL_TREE;
9657 location_t loc = UNKNOWN_LOCATION;
9659 while (true)
9661 cp_expr assignment_expression;
9663 /* Parse the next assignment-expression. */
9664 assignment_expression
9665 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9667 /* We don't create a temporary for a call that is the immediate operand
9668 of decltype or on the RHS of a comma. But when we see a comma, we
9669 need to create a temporary for a call on the LHS. */
9670 if (decltype_p && !processing_template_decl
9671 && TREE_CODE (assignment_expression) == CALL_EXPR
9672 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9673 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9674 assignment_expression
9675 = build_cplus_new (TREE_TYPE (assignment_expression),
9676 assignment_expression, tf_warning_or_error);
9678 /* If this is the first assignment-expression, we can just
9679 save it away. */
9680 if (!expression)
9681 expression = assignment_expression;
9682 else
9684 /* Create a location with caret at the comma, ranging
9685 from the start of the LHS to the end of the RHS. */
9686 loc = make_location (loc,
9687 expression.get_start (),
9688 assignment_expression.get_finish ());
9689 expression = build_x_compound_expr (loc, expression,
9690 assignment_expression,
9691 complain_flags (decltype_p));
9692 expression.set_location (loc);
9694 /* If the next token is not a comma, or we're in a fold-expression, then
9695 we are done with the expression. */
9696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9697 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9698 break;
9699 /* Consume the `,'. */
9700 loc = cp_lexer_peek_token (parser->lexer)->location;
9701 cp_lexer_consume_token (parser->lexer);
9702 /* A comma operator cannot appear in a constant-expression. */
9703 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9704 expression = error_mark_node;
9707 return expression;
9710 /* Parse a constant-expression.
9712 constant-expression:
9713 conditional-expression
9715 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9716 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9717 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9718 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9719 only parse a conditional-expression, otherwise parse an
9720 assignment-expression. See below for rationale. */
9722 static cp_expr
9723 cp_parser_constant_expression (cp_parser* parser,
9724 bool allow_non_constant_p,
9725 bool *non_constant_p,
9726 bool strict_p)
9728 bool saved_integral_constant_expression_p;
9729 bool saved_allow_non_integral_constant_expression_p;
9730 bool saved_non_integral_constant_expression_p;
9731 cp_expr expression;
9733 /* It might seem that we could simply parse the
9734 conditional-expression, and then check to see if it were
9735 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9736 one that the compiler can figure out is constant, possibly after
9737 doing some simplifications or optimizations. The standard has a
9738 precise definition of constant-expression, and we must honor
9739 that, even though it is somewhat more restrictive.
9741 For example:
9743 int i[(2, 3)];
9745 is not a legal declaration, because `(2, 3)' is not a
9746 constant-expression. The `,' operator is forbidden in a
9747 constant-expression. However, GCC's constant-folding machinery
9748 will fold this operation to an INTEGER_CST for `3'. */
9750 /* Save the old settings. */
9751 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9752 saved_allow_non_integral_constant_expression_p
9753 = parser->allow_non_integral_constant_expression_p;
9754 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9755 /* We are now parsing a constant-expression. */
9756 parser->integral_constant_expression_p = true;
9757 parser->allow_non_integral_constant_expression_p
9758 = (allow_non_constant_p || cxx_dialect >= cxx11);
9759 parser->non_integral_constant_expression_p = false;
9760 /* Although the grammar says "conditional-expression", when not STRICT_P,
9761 we parse an "assignment-expression", which also permits
9762 "throw-expression" and the use of assignment operators. In the case
9763 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9764 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9765 actually essential that we look for an assignment-expression.
9766 For example, cp_parser_initializer_clauses uses this function to
9767 determine whether a particular assignment-expression is in fact
9768 constant. */
9769 if (strict_p)
9771 /* Parse the binary expressions (logical-or-expression). */
9772 expression = cp_parser_binary_expression (parser, false, false, false,
9773 PREC_NOT_OPERATOR, NULL);
9774 /* If the next token is a `?' then we're actually looking at
9775 a conditional-expression; otherwise we're done. */
9776 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9777 expression = cp_parser_question_colon_clause (parser, expression);
9779 else
9780 expression = cp_parser_assignment_expression (parser);
9781 /* Restore the old settings. */
9782 parser->integral_constant_expression_p
9783 = saved_integral_constant_expression_p;
9784 parser->allow_non_integral_constant_expression_p
9785 = saved_allow_non_integral_constant_expression_p;
9786 if (cxx_dialect >= cxx11)
9788 /* Require an rvalue constant expression here; that's what our
9789 callers expect. Reference constant expressions are handled
9790 separately in e.g. cp_parser_template_argument. */
9791 tree decay = expression;
9792 if (TREE_TYPE (expression)
9793 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9794 decay = build_address (expression);
9795 bool is_const = potential_rvalue_constant_expression (decay);
9796 parser->non_integral_constant_expression_p = !is_const;
9797 if (!is_const && !allow_non_constant_p)
9798 require_potential_rvalue_constant_expression (decay);
9800 if (allow_non_constant_p)
9801 *non_constant_p = parser->non_integral_constant_expression_p;
9802 parser->non_integral_constant_expression_p
9803 = saved_non_integral_constant_expression_p;
9805 return expression;
9808 /* Parse __builtin_offsetof.
9810 offsetof-expression:
9811 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9813 offsetof-member-designator:
9814 id-expression
9815 | offsetof-member-designator "." id-expression
9816 | offsetof-member-designator "[" expression "]"
9817 | offsetof-member-designator "->" id-expression */
9819 static cp_expr
9820 cp_parser_builtin_offsetof (cp_parser *parser)
9822 int save_ice_p, save_non_ice_p;
9823 tree type;
9824 cp_expr expr;
9825 cp_id_kind dummy;
9826 cp_token *token;
9827 location_t finish_loc;
9829 /* We're about to accept non-integral-constant things, but will
9830 definitely yield an integral constant expression. Save and
9831 restore these values around our local parsing. */
9832 save_ice_p = parser->integral_constant_expression_p;
9833 save_non_ice_p = parser->non_integral_constant_expression_p;
9835 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9837 /* Consume the "__builtin_offsetof" token. */
9838 cp_lexer_consume_token (parser->lexer);
9839 /* Consume the opening `('. */
9840 matching_parens parens;
9841 parens.require_open (parser);
9842 /* Parse the type-id. */
9843 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9845 const char *saved_message = parser->type_definition_forbidden_message;
9846 parser->type_definition_forbidden_message
9847 = G_("types may not be defined within __builtin_offsetof");
9848 type = cp_parser_type_id (parser);
9849 parser->type_definition_forbidden_message = saved_message;
9851 /* Look for the `,'. */
9852 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9853 token = cp_lexer_peek_token (parser->lexer);
9855 /* Build the (type *)null that begins the traditional offsetof macro. */
9856 tree object_ptr
9857 = build_static_cast (build_pointer_type (type), null_pointer_node,
9858 tf_warning_or_error);
9860 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9861 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9862 true, &dummy, token->location);
9863 while (true)
9865 token = cp_lexer_peek_token (parser->lexer);
9866 switch (token->type)
9868 case CPP_OPEN_SQUARE:
9869 /* offsetof-member-designator "[" expression "]" */
9870 expr = cp_parser_postfix_open_square_expression (parser, expr,
9871 true, false);
9872 break;
9874 case CPP_DEREF:
9875 /* offsetof-member-designator "->" identifier */
9876 expr = grok_array_decl (token->location, expr,
9877 integer_zero_node, false);
9878 /* FALLTHRU */
9880 case CPP_DOT:
9881 /* offsetof-member-designator "." identifier */
9882 cp_lexer_consume_token (parser->lexer);
9883 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9884 expr, true, &dummy,
9885 token->location);
9886 break;
9888 case CPP_CLOSE_PAREN:
9889 /* Consume the ")" token. */
9890 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9891 cp_lexer_consume_token (parser->lexer);
9892 goto success;
9894 default:
9895 /* Error. We know the following require will fail, but
9896 that gives the proper error message. */
9897 parens.require_close (parser);
9898 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9899 expr = error_mark_node;
9900 goto failure;
9904 success:
9905 /* Make a location of the form:
9906 __builtin_offsetof (struct s, f)
9907 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9908 with caret at the type-id, ranging from the start of the
9909 "_builtin_offsetof" token to the close paren. */
9910 loc = make_location (loc, start_loc, finish_loc);
9911 /* The result will be an INTEGER_CST, so we need to explicitly
9912 preserve the location. */
9913 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9915 failure:
9916 parser->integral_constant_expression_p = save_ice_p;
9917 parser->non_integral_constant_expression_p = save_non_ice_p;
9919 expr = expr.maybe_add_location_wrapper ();
9920 return expr;
9923 /* Parse a trait expression.
9925 Returns a representation of the expression, the underlying type
9926 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9928 static cp_expr
9929 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9931 cp_trait_kind kind;
9932 tree type1, type2 = NULL_TREE;
9933 bool binary = false;
9934 bool variadic = false;
9936 switch (keyword)
9938 case RID_HAS_NOTHROW_ASSIGN:
9939 kind = CPTK_HAS_NOTHROW_ASSIGN;
9940 break;
9941 case RID_HAS_NOTHROW_CONSTRUCTOR:
9942 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9943 break;
9944 case RID_HAS_NOTHROW_COPY:
9945 kind = CPTK_HAS_NOTHROW_COPY;
9946 break;
9947 case RID_HAS_TRIVIAL_ASSIGN:
9948 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9949 break;
9950 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9951 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9952 break;
9953 case RID_HAS_TRIVIAL_COPY:
9954 kind = CPTK_HAS_TRIVIAL_COPY;
9955 break;
9956 case RID_HAS_TRIVIAL_DESTRUCTOR:
9957 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9958 break;
9959 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9960 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9961 break;
9962 case RID_HAS_VIRTUAL_DESTRUCTOR:
9963 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9964 break;
9965 case RID_IS_ABSTRACT:
9966 kind = CPTK_IS_ABSTRACT;
9967 break;
9968 case RID_IS_AGGREGATE:
9969 kind = CPTK_IS_AGGREGATE;
9970 break;
9971 case RID_IS_BASE_OF:
9972 kind = CPTK_IS_BASE_OF;
9973 binary = true;
9974 break;
9975 case RID_IS_CLASS:
9976 kind = CPTK_IS_CLASS;
9977 break;
9978 case RID_IS_EMPTY:
9979 kind = CPTK_IS_EMPTY;
9980 break;
9981 case RID_IS_ENUM:
9982 kind = CPTK_IS_ENUM;
9983 break;
9984 case RID_IS_FINAL:
9985 kind = CPTK_IS_FINAL;
9986 break;
9987 case RID_IS_LITERAL_TYPE:
9988 kind = CPTK_IS_LITERAL_TYPE;
9989 break;
9990 case RID_IS_POD:
9991 kind = CPTK_IS_POD;
9992 break;
9993 case RID_IS_POLYMORPHIC:
9994 kind = CPTK_IS_POLYMORPHIC;
9995 break;
9996 case RID_IS_SAME_AS:
9997 kind = CPTK_IS_SAME_AS;
9998 binary = true;
9999 break;
10000 case RID_IS_STD_LAYOUT:
10001 kind = CPTK_IS_STD_LAYOUT;
10002 break;
10003 case RID_IS_TRIVIAL:
10004 kind = CPTK_IS_TRIVIAL;
10005 break;
10006 case RID_IS_TRIVIALLY_ASSIGNABLE:
10007 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10008 binary = true;
10009 break;
10010 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10011 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10012 variadic = true;
10013 break;
10014 case RID_IS_TRIVIALLY_COPYABLE:
10015 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10016 break;
10017 case RID_IS_UNION:
10018 kind = CPTK_IS_UNION;
10019 break;
10020 case RID_UNDERLYING_TYPE:
10021 kind = CPTK_UNDERLYING_TYPE;
10022 break;
10023 case RID_BASES:
10024 kind = CPTK_BASES;
10025 break;
10026 case RID_DIRECT_BASES:
10027 kind = CPTK_DIRECT_BASES;
10028 break;
10029 case RID_IS_ASSIGNABLE:
10030 kind = CPTK_IS_ASSIGNABLE;
10031 binary = true;
10032 break;
10033 case RID_IS_CONSTRUCTIBLE:
10034 kind = CPTK_IS_CONSTRUCTIBLE;
10035 variadic = true;
10036 break;
10037 default:
10038 gcc_unreachable ();
10041 /* Get location of initial token. */
10042 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10044 /* Consume the token. */
10045 cp_lexer_consume_token (parser->lexer);
10047 matching_parens parens;
10048 parens.require_open (parser);
10051 type_id_in_expr_sentinel s (parser);
10052 type1 = cp_parser_type_id (parser);
10055 if (type1 == error_mark_node)
10056 return error_mark_node;
10058 if (binary)
10060 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10063 type_id_in_expr_sentinel s (parser);
10064 type2 = cp_parser_type_id (parser);
10067 if (type2 == error_mark_node)
10068 return error_mark_node;
10070 else if (variadic)
10072 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10074 cp_lexer_consume_token (parser->lexer);
10075 tree elt = cp_parser_type_id (parser);
10076 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10078 cp_lexer_consume_token (parser->lexer);
10079 elt = make_pack_expansion (elt);
10081 if (elt == error_mark_node)
10082 return error_mark_node;
10083 type2 = tree_cons (NULL_TREE, elt, type2);
10087 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10088 parens.require_close (parser);
10090 /* Construct a location of the form:
10091 __is_trivially_copyable(_Tp)
10092 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10093 with start == caret, finishing at the close-paren. */
10094 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10096 /* Complete the trait expression, which may mean either processing
10097 the trait expr now or saving it for template instantiation. */
10098 switch (kind)
10100 case CPTK_UNDERLYING_TYPE:
10101 return cp_expr (finish_underlying_type (type1), trait_loc);
10102 case CPTK_BASES:
10103 return cp_expr (finish_bases (type1, false), trait_loc);
10104 case CPTK_DIRECT_BASES:
10105 return cp_expr (finish_bases (type1, true), trait_loc);
10106 default:
10107 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10111 /* Parse a lambda expression.
10113 lambda-expression:
10114 lambda-introducer lambda-declarator [opt] compound-statement
10116 Returns a representation of the expression. */
10118 static cp_expr
10119 cp_parser_lambda_expression (cp_parser* parser)
10121 tree lambda_expr = build_lambda_expr ();
10122 tree type;
10123 bool ok = true;
10124 cp_token *token = cp_lexer_peek_token (parser->lexer);
10125 cp_token_position start = 0;
10127 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10129 if (cp_unevaluated_operand)
10131 if (!token->error_reported)
10133 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10134 "lambda-expression in unevaluated context");
10135 token->error_reported = true;
10137 ok = false;
10139 else if (parser->in_template_argument_list_p)
10141 if (!token->error_reported)
10143 error_at (token->location, "lambda-expression in template-argument");
10144 token->error_reported = true;
10146 ok = false;
10149 /* We may be in the middle of deferred access check. Disable
10150 it now. */
10151 push_deferring_access_checks (dk_no_deferred);
10153 cp_parser_lambda_introducer (parser, lambda_expr);
10155 type = begin_lambda_type (lambda_expr);
10156 if (type == error_mark_node)
10157 return error_mark_node;
10159 record_lambda_scope (lambda_expr);
10161 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10162 determine_visibility (TYPE_NAME (type));
10164 /* Now that we've started the type, add the capture fields for any
10165 explicit captures. */
10166 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10169 /* Inside the class, surrounding template-parameter-lists do not apply. */
10170 unsigned int saved_num_template_parameter_lists
10171 = parser->num_template_parameter_lists;
10172 unsigned char in_statement = parser->in_statement;
10173 bool in_switch_statement_p = parser->in_switch_statement_p;
10174 bool fully_implicit_function_template_p
10175 = parser->fully_implicit_function_template_p;
10176 tree implicit_template_parms = parser->implicit_template_parms;
10177 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10178 bool auto_is_implicit_function_template_parm_p
10179 = parser->auto_is_implicit_function_template_parm_p;
10181 parser->num_template_parameter_lists = 0;
10182 parser->in_statement = 0;
10183 parser->in_switch_statement_p = false;
10184 parser->fully_implicit_function_template_p = false;
10185 parser->implicit_template_parms = 0;
10186 parser->implicit_template_scope = 0;
10187 parser->auto_is_implicit_function_template_parm_p = false;
10189 /* By virtue of defining a local class, a lambda expression has access to
10190 the private variables of enclosing classes. */
10192 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10194 if (ok && cp_parser_error_occurred (parser))
10195 ok = false;
10197 if (ok)
10199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10200 && cp_parser_start_tentative_firewall (parser))
10201 start = token;
10202 cp_parser_lambda_body (parser, lambda_expr);
10204 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10206 if (cp_parser_skip_to_closing_brace (parser))
10207 cp_lexer_consume_token (parser->lexer);
10210 /* The capture list was built up in reverse order; fix that now. */
10211 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10212 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10214 if (ok)
10215 maybe_add_lambda_conv_op (type);
10217 type = finish_struct (type, /*attributes=*/NULL_TREE);
10219 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10220 parser->in_statement = in_statement;
10221 parser->in_switch_statement_p = in_switch_statement_p;
10222 parser->fully_implicit_function_template_p
10223 = fully_implicit_function_template_p;
10224 parser->implicit_template_parms = implicit_template_parms;
10225 parser->implicit_template_scope = implicit_template_scope;
10226 parser->auto_is_implicit_function_template_parm_p
10227 = auto_is_implicit_function_template_parm_p;
10230 /* This field is only used during parsing of the lambda. */
10231 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10233 /* This lambda shouldn't have any proxies left at this point. */
10234 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10235 /* And now that we're done, push proxies for an enclosing lambda. */
10236 insert_pending_capture_proxies ();
10238 /* Update the lambda expression to a range. */
10239 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10240 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10241 token->location,
10242 end_tok->location);
10244 if (ok)
10245 lambda_expr = build_lambda_object (lambda_expr);
10246 else
10247 lambda_expr = error_mark_node;
10249 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10251 pop_deferring_access_checks ();
10253 return lambda_expr;
10256 /* Parse the beginning of a lambda expression.
10258 lambda-introducer:
10259 [ lambda-capture [opt] ]
10261 LAMBDA_EXPR is the current representation of the lambda expression. */
10263 static void
10264 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10266 /* Need commas after the first capture. */
10267 bool first = true;
10269 /* Eat the leading `['. */
10270 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10272 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10273 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10274 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10275 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10276 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10277 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10279 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10281 cp_lexer_consume_token (parser->lexer);
10282 first = false;
10285 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10287 cp_token* capture_token;
10288 tree capture_id;
10289 tree capture_init_expr;
10290 cp_id_kind idk = CP_ID_KIND_NONE;
10291 bool explicit_init_p = false;
10293 enum capture_kind_type
10295 BY_COPY,
10296 BY_REFERENCE
10298 enum capture_kind_type capture_kind = BY_COPY;
10300 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10302 error ("expected end of capture-list");
10303 return;
10306 if (first)
10307 first = false;
10308 else
10309 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10311 /* Possibly capture `this'. */
10312 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10314 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10315 if (cxx_dialect < cxx2a
10316 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10317 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10318 "with by-copy capture default");
10319 cp_lexer_consume_token (parser->lexer);
10320 add_capture (lambda_expr,
10321 /*id=*/this_identifier,
10322 /*initializer=*/finish_this_expr (),
10323 /*by_reference_p=*/true,
10324 explicit_init_p);
10325 continue;
10328 /* Possibly capture `*this'. */
10329 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10330 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10332 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10333 if (cxx_dialect < cxx17)
10334 pedwarn (loc, 0, "%<*this%> capture only available with "
10335 "-std=c++17 or -std=gnu++17");
10336 cp_lexer_consume_token (parser->lexer);
10337 cp_lexer_consume_token (parser->lexer);
10338 add_capture (lambda_expr,
10339 /*id=*/this_identifier,
10340 /*initializer=*/finish_this_expr (),
10341 /*by_reference_p=*/false,
10342 explicit_init_p);
10343 continue;
10346 /* Remember whether we want to capture as a reference or not. */
10347 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10349 capture_kind = BY_REFERENCE;
10350 cp_lexer_consume_token (parser->lexer);
10353 /* Get the identifier. */
10354 capture_token = cp_lexer_peek_token (parser->lexer);
10355 capture_id = cp_parser_identifier (parser);
10357 if (capture_id == error_mark_node)
10358 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10359 delimiters, but I modified this to stop on unnested ']' as well. It
10360 was already changed to stop on unnested '}', so the
10361 "closing_parenthesis" name is no more misleading with my change. */
10363 cp_parser_skip_to_closing_parenthesis (parser,
10364 /*recovering=*/true,
10365 /*or_comma=*/true,
10366 /*consume_paren=*/true);
10367 break;
10370 /* Find the initializer for this capture. */
10371 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10372 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10373 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10375 bool direct, non_constant;
10376 /* An explicit initializer exists. */
10377 if (cxx_dialect < cxx14)
10378 pedwarn (input_location, 0,
10379 "lambda capture initializers "
10380 "only available with -std=c++14 or -std=gnu++14");
10381 capture_init_expr = cp_parser_initializer (parser, &direct,
10382 &non_constant, true);
10383 explicit_init_p = true;
10384 if (capture_init_expr == NULL_TREE)
10386 error ("empty initializer for lambda init-capture");
10387 capture_init_expr = error_mark_node;
10390 else
10392 const char* error_msg;
10394 /* Turn the identifier into an id-expression. */
10395 capture_init_expr
10396 = cp_parser_lookup_name_simple (parser, capture_id,
10397 capture_token->location);
10399 if (capture_init_expr == error_mark_node)
10401 unqualified_name_lookup_error (capture_id);
10402 continue;
10404 else if (!VAR_P (capture_init_expr)
10405 && TREE_CODE (capture_init_expr) != PARM_DECL)
10407 error_at (capture_token->location,
10408 "capture of non-variable %qE",
10409 capture_init_expr);
10410 if (DECL_P (capture_init_expr))
10411 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10412 "%q#D declared here", capture_init_expr);
10413 continue;
10415 if (VAR_P (capture_init_expr)
10416 && decl_storage_duration (capture_init_expr) != dk_auto)
10418 if (pedwarn (capture_token->location, 0, "capture of variable "
10419 "%qD with non-automatic storage duration",
10420 capture_init_expr))
10421 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10422 "%q#D declared here", capture_init_expr);
10423 continue;
10426 capture_init_expr
10427 = finish_id_expression
10428 (capture_id,
10429 capture_init_expr,
10430 parser->scope,
10431 &idk,
10432 /*integral_constant_expression_p=*/false,
10433 /*allow_non_integral_constant_expression_p=*/false,
10434 /*non_integral_constant_expression_p=*/NULL,
10435 /*template_p=*/false,
10436 /*done=*/true,
10437 /*address_p=*/false,
10438 /*template_arg_p=*/false,
10439 &error_msg,
10440 capture_token->location);
10442 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10444 cp_lexer_consume_token (parser->lexer);
10445 capture_init_expr = make_pack_expansion (capture_init_expr);
10449 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10450 && !explicit_init_p)
10452 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10453 && capture_kind == BY_COPY)
10454 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10455 "of %qD redundant with by-copy capture default",
10456 capture_id);
10457 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10458 && capture_kind == BY_REFERENCE)
10459 pedwarn (capture_token->location, 0, "explicit by-reference "
10460 "capture of %qD redundant with by-reference capture "
10461 "default", capture_id);
10464 add_capture (lambda_expr,
10465 capture_id,
10466 capture_init_expr,
10467 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10468 explicit_init_p);
10470 /* If there is any qualification still in effect, clear it
10471 now; we will be starting fresh with the next capture. */
10472 parser->scope = NULL_TREE;
10473 parser->qualifying_scope = NULL_TREE;
10474 parser->object_scope = NULL_TREE;
10477 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10480 /* Parse the (optional) middle of a lambda expression.
10482 lambda-declarator:
10483 < template-parameter-list [opt] >
10484 ( parameter-declaration-clause [opt] )
10485 attribute-specifier [opt]
10486 decl-specifier-seq [opt]
10487 exception-specification [opt]
10488 lambda-return-type-clause [opt]
10490 LAMBDA_EXPR is the current representation of the lambda expression. */
10492 static bool
10493 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10495 /* 5.1.1.4 of the standard says:
10496 If a lambda-expression does not include a lambda-declarator, it is as if
10497 the lambda-declarator were ().
10498 This means an empty parameter list, no attributes, and no exception
10499 specification. */
10500 tree param_list = void_list_node;
10501 tree attributes = NULL_TREE;
10502 tree exception_spec = NULL_TREE;
10503 tree template_param_list = NULL_TREE;
10504 tree tx_qual = NULL_TREE;
10505 tree return_type = NULL_TREE;
10506 cp_decl_specifier_seq lambda_specs;
10507 clear_decl_specs (&lambda_specs);
10509 /* The template-parameter-list is optional, but must begin with
10510 an opening angle if present. */
10511 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10513 if (cxx_dialect < cxx14)
10514 pedwarn (parser->lexer->next_token->location, 0,
10515 "lambda templates are only available with "
10516 "-std=c++14 or -std=gnu++14");
10517 else if (cxx_dialect < cxx2a)
10518 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10519 "lambda templates are only available with "
10520 "-std=c++2a or -std=gnu++2a");
10522 cp_lexer_consume_token (parser->lexer);
10524 template_param_list = cp_parser_template_parameter_list (parser);
10526 cp_parser_skip_to_end_of_template_parameter_list (parser);
10528 /* We just processed one more parameter list. */
10529 ++parser->num_template_parameter_lists;
10532 /* The parameter-declaration-clause is optional (unless
10533 template-parameter-list was given), but must begin with an
10534 opening parenthesis if present. */
10535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10537 matching_parens parens;
10538 parens.consume_open (parser);
10540 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10542 /* Parse parameters. */
10543 param_list = cp_parser_parameter_declaration_clause (parser);
10545 /* Default arguments shall not be specified in the
10546 parameter-declaration-clause of a lambda-declarator. */
10547 if (cxx_dialect < cxx14)
10548 for (tree t = param_list; t; t = TREE_CHAIN (t))
10549 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10550 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10551 "default argument specified for lambda parameter");
10553 parens.require_close (parser);
10555 attributes = cp_parser_attributes_opt (parser);
10557 /* In the decl-specifier-seq of the lambda-declarator, each
10558 decl-specifier shall either be mutable or constexpr. */
10559 int declares_class_or_enum;
10560 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10561 cp_parser_decl_specifier_seq (parser,
10562 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10563 &lambda_specs, &declares_class_or_enum);
10564 if (lambda_specs.storage_class == sc_mutable)
10566 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10567 if (lambda_specs.conflicting_specifiers_p)
10568 error_at (lambda_specs.locations[ds_storage_class],
10569 "duplicate %<mutable%>");
10572 tx_qual = cp_parser_tx_qualifier_opt (parser);
10574 /* Parse optional exception specification. */
10575 exception_spec = cp_parser_exception_specification_opt (parser);
10577 /* Parse optional trailing return type. */
10578 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10580 cp_lexer_consume_token (parser->lexer);
10581 return_type = cp_parser_trailing_type_id (parser);
10584 /* The function parameters must be in scope all the way until after the
10585 trailing-return-type in case of decltype. */
10586 pop_bindings_and_leave_scope ();
10588 else if (template_param_list != NULL_TREE) // generate diagnostic
10589 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10591 /* Create the function call operator.
10593 Messing with declarators like this is no uglier than building up the
10594 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10595 other code. */
10597 cp_decl_specifier_seq return_type_specs;
10598 cp_declarator* declarator;
10599 tree fco;
10600 int quals;
10601 void *p;
10603 clear_decl_specs (&return_type_specs);
10604 return_type_specs.type = make_auto ();
10606 if (lambda_specs.locations[ds_constexpr])
10608 if (cxx_dialect >= cxx17)
10609 return_type_specs.locations[ds_constexpr]
10610 = lambda_specs.locations[ds_constexpr];
10611 else
10612 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10613 "lambda only available with -std=c++17 or -std=gnu++17");
10616 p = obstack_alloc (&declarator_obstack, 0);
10618 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10620 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10621 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10622 declarator = make_call_declarator (declarator, param_list, quals,
10623 VIRT_SPEC_UNSPECIFIED,
10624 REF_QUAL_NONE,
10625 tx_qual,
10626 exception_spec,
10627 /*late_return_type=*/NULL_TREE,
10628 /*requires_clause*/NULL_TREE);
10629 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10630 if (return_type)
10631 declarator->u.function.late_return_type = return_type;
10633 fco = grokmethod (&return_type_specs,
10634 declarator,
10635 attributes);
10636 if (fco != error_mark_node)
10638 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10639 DECL_ARTIFICIAL (fco) = 1;
10640 /* Give the object parameter a different name. */
10641 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10643 if (template_param_list)
10645 fco = finish_member_template_decl (fco);
10646 finish_template_decl (template_param_list);
10647 --parser->num_template_parameter_lists;
10649 else if (parser->fully_implicit_function_template_p)
10650 fco = finish_fully_implicit_template (parser, fco);
10652 finish_member_declaration (fco);
10654 obstack_free (&declarator_obstack, p);
10656 return (fco != error_mark_node);
10660 /* Parse the body of a lambda expression, which is simply
10662 compound-statement
10664 but which requires special handling.
10665 LAMBDA_EXPR is the current representation of the lambda expression. */
10667 static void
10668 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10670 bool nested = (current_function_decl != NULL_TREE);
10671 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10672 bool in_function_body = parser->in_function_body;
10674 if (nested)
10675 push_function_context ();
10676 else
10677 /* Still increment function_depth so that we don't GC in the
10678 middle of an expression. */
10679 ++function_depth;
10681 vec<tree> omp_privatization_save;
10682 save_omp_privatization_clauses (omp_privatization_save);
10683 /* Clear this in case we're in the middle of a default argument. */
10684 parser->local_variables_forbidden_p = false;
10685 parser->in_function_body = true;
10688 local_specialization_stack s (lss_copy);
10689 tree fco = lambda_function (lambda_expr);
10690 tree body = start_lambda_function (fco, lambda_expr);
10691 matching_braces braces;
10693 if (braces.require_open (parser))
10695 tree compound_stmt = begin_compound_stmt (0);
10697 /* Originally C++11 required us to peek for 'return expr'; and
10698 process it specially here to deduce the return type. N3638
10699 removed the need for that. */
10701 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10702 cp_parser_label_declaration (parser);
10703 cp_parser_statement_seq_opt (parser, NULL_TREE);
10704 braces.require_close (parser);
10706 finish_compound_stmt (compound_stmt);
10709 finish_lambda_function (body);
10712 restore_omp_privatization_clauses (omp_privatization_save);
10713 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10714 parser->in_function_body = in_function_body;
10715 if (nested)
10716 pop_function_context();
10717 else
10718 --function_depth;
10721 /* Statements [gram.stmt.stmt] */
10723 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10725 static void
10726 add_debug_begin_stmt (location_t loc)
10728 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10729 return;
10730 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10731 /* A concept is never expanded normally. */
10732 return;
10734 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10735 SET_EXPR_LOCATION (stmt, loc);
10736 add_stmt (stmt);
10739 /* Parse a statement.
10741 statement:
10742 labeled-statement
10743 expression-statement
10744 compound-statement
10745 selection-statement
10746 iteration-statement
10747 jump-statement
10748 declaration-statement
10749 try-block
10751 C++11:
10753 statement:
10754 labeled-statement
10755 attribute-specifier-seq (opt) expression-statement
10756 attribute-specifier-seq (opt) compound-statement
10757 attribute-specifier-seq (opt) selection-statement
10758 attribute-specifier-seq (opt) iteration-statement
10759 attribute-specifier-seq (opt) jump-statement
10760 declaration-statement
10761 attribute-specifier-seq (opt) try-block
10763 init-statement:
10764 expression-statement
10765 simple-declaration
10767 TM Extension:
10769 statement:
10770 atomic-statement
10772 IN_COMPOUND is true when the statement is nested inside a
10773 cp_parser_compound_statement; this matters for certain pragmas.
10775 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10776 is a (possibly labeled) if statement which is not enclosed in braces
10777 and has an else clause. This is used to implement -Wparentheses.
10779 CHAIN is a vector of if-else-if conditions. */
10781 static void
10782 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10783 bool in_compound, bool *if_p, vec<tree> *chain,
10784 location_t *loc_after_labels)
10786 tree statement, std_attrs = NULL_TREE;
10787 cp_token *token;
10788 location_t statement_location, attrs_location;
10790 restart:
10791 if (if_p != NULL)
10792 *if_p = false;
10793 /* There is no statement yet. */
10794 statement = NULL_TREE;
10796 saved_token_sentinel saved_tokens (parser->lexer);
10797 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10798 if (c_dialect_objc ())
10799 /* In obj-c++, seeing '[[' might be the either the beginning of
10800 c++11 attributes, or a nested objc-message-expression. So
10801 let's parse the c++11 attributes tentatively. */
10802 cp_parser_parse_tentatively (parser);
10803 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10804 if (c_dialect_objc ())
10806 if (!cp_parser_parse_definitely (parser))
10807 std_attrs = NULL_TREE;
10810 /* Peek at the next token. */
10811 token = cp_lexer_peek_token (parser->lexer);
10812 /* Remember the location of the first token in the statement. */
10813 statement_location = token->location;
10814 add_debug_begin_stmt (statement_location);
10815 /* If this is a keyword, then that will often determine what kind of
10816 statement we have. */
10817 if (token->type == CPP_KEYWORD)
10819 enum rid keyword = token->keyword;
10821 switch (keyword)
10823 case RID_CASE:
10824 case RID_DEFAULT:
10825 /* Looks like a labeled-statement with a case label.
10826 Parse the label, and then use tail recursion to parse
10827 the statement. */
10828 cp_parser_label_for_labeled_statement (parser, std_attrs);
10829 in_compound = false;
10830 goto restart;
10832 case RID_IF:
10833 case RID_SWITCH:
10834 statement = cp_parser_selection_statement (parser, if_p, chain);
10835 break;
10837 case RID_WHILE:
10838 case RID_DO:
10839 case RID_FOR:
10840 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10841 break;
10843 case RID_BREAK:
10844 case RID_CONTINUE:
10845 case RID_RETURN:
10846 case RID_GOTO:
10847 statement = cp_parser_jump_statement (parser);
10848 break;
10850 /* Objective-C++ exception-handling constructs. */
10851 case RID_AT_TRY:
10852 case RID_AT_CATCH:
10853 case RID_AT_FINALLY:
10854 case RID_AT_SYNCHRONIZED:
10855 case RID_AT_THROW:
10856 statement = cp_parser_objc_statement (parser);
10857 break;
10859 case RID_TRY:
10860 statement = cp_parser_try_block (parser);
10861 break;
10863 case RID_NAMESPACE:
10864 /* This must be a namespace alias definition. */
10865 cp_parser_declaration_statement (parser);
10866 return;
10868 case RID_TRANSACTION_ATOMIC:
10869 case RID_TRANSACTION_RELAXED:
10870 case RID_SYNCHRONIZED:
10871 case RID_ATOMIC_NOEXCEPT:
10872 case RID_ATOMIC_CANCEL:
10873 statement = cp_parser_transaction (parser, token);
10874 break;
10875 case RID_TRANSACTION_CANCEL:
10876 statement = cp_parser_transaction_cancel (parser);
10877 break;
10879 default:
10880 /* It might be a keyword like `int' that can start a
10881 declaration-statement. */
10882 break;
10885 else if (token->type == CPP_NAME)
10887 /* If the next token is a `:', then we are looking at a
10888 labeled-statement. */
10889 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10890 if (token->type == CPP_COLON)
10892 /* Looks like a labeled-statement with an ordinary label.
10893 Parse the label, and then use tail recursion to parse
10894 the statement. */
10896 cp_parser_label_for_labeled_statement (parser, std_attrs);
10897 in_compound = false;
10898 goto restart;
10901 /* Anything that starts with a `{' must be a compound-statement. */
10902 else if (token->type == CPP_OPEN_BRACE)
10903 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10904 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10905 a statement all its own. */
10906 else if (token->type == CPP_PRAGMA)
10908 /* Only certain OpenMP pragmas are attached to statements, and thus
10909 are considered statements themselves. All others are not. In
10910 the context of a compound, accept the pragma as a "statement" and
10911 return so that we can check for a close brace. Otherwise we
10912 require a real statement and must go back and read one. */
10913 if (in_compound)
10914 cp_parser_pragma (parser, pragma_compound, if_p);
10915 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10916 goto restart;
10917 return;
10919 else if (token->type == CPP_EOF)
10921 cp_parser_error (parser, "expected statement");
10922 return;
10925 /* Everything else must be a declaration-statement or an
10926 expression-statement. Try for the declaration-statement
10927 first, unless we are looking at a `;', in which case we know that
10928 we have an expression-statement. */
10929 if (!statement)
10931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10933 if (std_attrs != NULL_TREE)
10935 /* Attributes should be parsed as part of the the
10936 declaration, so let's un-parse them. */
10937 saved_tokens.rollback();
10938 std_attrs = NULL_TREE;
10941 cp_parser_parse_tentatively (parser);
10942 /* Try to parse the declaration-statement. */
10943 cp_parser_declaration_statement (parser);
10944 /* If that worked, we're done. */
10945 if (cp_parser_parse_definitely (parser))
10946 return;
10948 /* All preceding labels have been parsed at this point. */
10949 if (loc_after_labels != NULL)
10950 *loc_after_labels = statement_location;
10952 /* Look for an expression-statement instead. */
10953 statement = cp_parser_expression_statement (parser, in_statement_expr);
10955 /* Handle [[fallthrough]];. */
10956 if (attribute_fallthrough_p (std_attrs))
10958 /* The next token after the fallthrough attribute is ';'. */
10959 if (statement == NULL_TREE)
10961 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10962 statement = build_call_expr_internal_loc (statement_location,
10963 IFN_FALLTHROUGH,
10964 void_type_node, 0);
10965 finish_expr_stmt (statement);
10967 else
10968 warning_at (statement_location, OPT_Wattributes,
10969 "%<fallthrough%> attribute not followed by %<;%>");
10970 std_attrs = NULL_TREE;
10974 /* Set the line number for the statement. */
10975 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10976 SET_EXPR_LOCATION (statement, statement_location);
10978 /* Allow "[[fallthrough]];", but warn otherwise. */
10979 if (std_attrs != NULL_TREE)
10980 warning_at (attrs_location,
10981 OPT_Wattributes,
10982 "attributes at the beginning of statement are ignored");
10985 /* Append ATTR to attribute list ATTRS. */
10987 static tree
10988 attr_chainon (tree attrs, tree attr)
10990 if (attrs == error_mark_node)
10991 return error_mark_node;
10992 if (attr == error_mark_node)
10993 return error_mark_node;
10994 return chainon (attrs, attr);
10997 /* Parse the label for a labeled-statement, i.e.
10999 identifier :
11000 case constant-expression :
11001 default :
11003 GNU Extension:
11004 case constant-expression ... constant-expression : statement
11006 When a label is parsed without errors, the label is added to the
11007 parse tree by the finish_* functions, so this function doesn't
11008 have to return the label. */
11010 static void
11011 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11013 cp_token *token;
11014 tree label = NULL_TREE;
11015 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11017 /* The next token should be an identifier. */
11018 token = cp_lexer_peek_token (parser->lexer);
11019 if (token->type != CPP_NAME
11020 && token->type != CPP_KEYWORD)
11022 cp_parser_error (parser, "expected labeled-statement");
11023 return;
11026 /* Remember whether this case or a user-defined label is allowed to fall
11027 through to. */
11028 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11030 parser->colon_corrects_to_scope_p = false;
11031 switch (token->keyword)
11033 case RID_CASE:
11035 tree expr, expr_hi;
11036 cp_token *ellipsis;
11038 /* Consume the `case' token. */
11039 cp_lexer_consume_token (parser->lexer);
11040 /* Parse the constant-expression. */
11041 expr = cp_parser_constant_expression (parser);
11042 if (check_for_bare_parameter_packs (expr))
11043 expr = error_mark_node;
11045 ellipsis = cp_lexer_peek_token (parser->lexer);
11046 if (ellipsis->type == CPP_ELLIPSIS)
11048 /* Consume the `...' token. */
11049 cp_lexer_consume_token (parser->lexer);
11050 expr_hi = cp_parser_constant_expression (parser);
11051 if (check_for_bare_parameter_packs (expr_hi))
11052 expr_hi = error_mark_node;
11054 /* We don't need to emit warnings here, as the common code
11055 will do this for us. */
11057 else
11058 expr_hi = NULL_TREE;
11060 if (parser->in_switch_statement_p)
11062 tree l = finish_case_label (token->location, expr, expr_hi);
11063 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11064 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11066 else
11067 error_at (token->location,
11068 "case label %qE not within a switch statement",
11069 expr);
11071 break;
11073 case RID_DEFAULT:
11074 /* Consume the `default' token. */
11075 cp_lexer_consume_token (parser->lexer);
11077 if (parser->in_switch_statement_p)
11079 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11080 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11081 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11083 else
11084 error_at (token->location, "case label not within a switch statement");
11085 break;
11087 default:
11088 /* Anything else must be an ordinary label. */
11089 label = finish_label_stmt (cp_parser_identifier (parser));
11090 if (label && TREE_CODE (label) == LABEL_DECL)
11091 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11092 break;
11095 /* Require the `:' token. */
11096 cp_parser_require (parser, CPP_COLON, RT_COLON);
11098 /* An ordinary label may optionally be followed by attributes.
11099 However, this is only permitted if the attributes are then
11100 followed by a semicolon. This is because, for backward
11101 compatibility, when parsing
11102 lab: __attribute__ ((unused)) int i;
11103 we want the attribute to attach to "i", not "lab". */
11104 if (label != NULL_TREE
11105 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11107 tree attrs;
11108 cp_parser_parse_tentatively (parser);
11109 attrs = cp_parser_gnu_attributes_opt (parser);
11110 if (attrs == NULL_TREE
11111 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11112 cp_parser_abort_tentative_parse (parser);
11113 else if (!cp_parser_parse_definitely (parser))
11115 else
11116 attributes = attr_chainon (attributes, attrs);
11119 if (attributes != NULL_TREE)
11120 cplus_decl_attributes (&label, attributes, 0);
11122 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11125 /* Parse an expression-statement.
11127 expression-statement:
11128 expression [opt] ;
11130 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11131 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11132 indicates whether this expression-statement is part of an
11133 expression statement. */
11135 static tree
11136 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11138 tree statement = NULL_TREE;
11139 cp_token *token = cp_lexer_peek_token (parser->lexer);
11140 location_t loc = token->location;
11142 /* There might be attribute fallthrough. */
11143 tree attr = cp_parser_gnu_attributes_opt (parser);
11145 /* If the next token is a ';', then there is no expression
11146 statement. */
11147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11149 statement = cp_parser_expression (parser);
11150 if (statement == error_mark_node
11151 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11153 cp_parser_skip_to_end_of_block_or_statement (parser);
11154 return error_mark_node;
11158 /* Handle [[fallthrough]];. */
11159 if (attribute_fallthrough_p (attr))
11161 /* The next token after the fallthrough attribute is ';'. */
11162 if (statement == NULL_TREE)
11163 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11164 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11165 void_type_node, 0);
11166 else
11167 warning_at (loc, OPT_Wattributes,
11168 "%<fallthrough%> attribute not followed by %<;%>");
11169 attr = NULL_TREE;
11172 /* Allow "[[fallthrough]];", but warn otherwise. */
11173 if (attr != NULL_TREE)
11174 warning_at (loc, OPT_Wattributes,
11175 "attributes at the beginning of statement are ignored");
11177 /* Give a helpful message for "A<T>::type t;" and the like. */
11178 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11179 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11181 if (TREE_CODE (statement) == SCOPE_REF)
11182 error_at (token->location, "need %<typename%> before %qE because "
11183 "%qT is a dependent scope",
11184 statement, TREE_OPERAND (statement, 0));
11185 else if (is_overloaded_fn (statement)
11186 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11188 /* A::A a; */
11189 tree fn = get_first_fn (statement);
11190 error_at (token->location,
11191 "%<%T::%D%> names the constructor, not the type",
11192 DECL_CONTEXT (fn), DECL_NAME (fn));
11196 /* Consume the final `;'. */
11197 cp_parser_consume_semicolon_at_end_of_statement (parser);
11199 if (in_statement_expr
11200 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11201 /* This is the final expression statement of a statement
11202 expression. */
11203 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11204 else if (statement)
11205 statement = finish_expr_stmt (statement);
11207 return statement;
11210 /* Parse a compound-statement.
11212 compound-statement:
11213 { statement-seq [opt] }
11215 GNU extension:
11217 compound-statement:
11218 { label-declaration-seq [opt] statement-seq [opt] }
11220 label-declaration-seq:
11221 label-declaration
11222 label-declaration-seq label-declaration
11224 Returns a tree representing the statement. */
11226 static tree
11227 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11228 int bcs_flags, bool function_body)
11230 tree compound_stmt;
11231 matching_braces braces;
11233 /* Consume the `{'. */
11234 if (!braces.require_open (parser))
11235 return error_mark_node;
11236 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11237 && !function_body && cxx_dialect < cxx14)
11238 pedwarn (input_location, OPT_Wpedantic,
11239 "compound-statement in %<constexpr%> function");
11240 /* Begin the compound-statement. */
11241 compound_stmt = begin_compound_stmt (bcs_flags);
11242 /* If the next keyword is `__label__' we have a label declaration. */
11243 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11244 cp_parser_label_declaration (parser);
11245 /* Parse an (optional) statement-seq. */
11246 cp_parser_statement_seq_opt (parser, in_statement_expr);
11247 /* Finish the compound-statement. */
11248 finish_compound_stmt (compound_stmt);
11249 /* Consume the `}'. */
11250 braces.require_close (parser);
11252 return compound_stmt;
11255 /* Parse an (optional) statement-seq.
11257 statement-seq:
11258 statement
11259 statement-seq [opt] statement */
11261 static void
11262 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11264 /* Scan statements until there aren't any more. */
11265 while (true)
11267 cp_token *token = cp_lexer_peek_token (parser->lexer);
11269 /* If we are looking at a `}', then we have run out of
11270 statements; the same is true if we have reached the end
11271 of file, or have stumbled upon a stray '@end'. */
11272 if (token->type == CPP_CLOSE_BRACE
11273 || token->type == CPP_EOF
11274 || token->type == CPP_PRAGMA_EOL
11275 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11276 break;
11278 /* If we are in a compound statement and find 'else' then
11279 something went wrong. */
11280 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11282 if (parser->in_statement & IN_IF_STMT)
11283 break;
11284 else
11286 token = cp_lexer_consume_token (parser->lexer);
11287 error_at (token->location, "%<else%> without a previous %<if%>");
11291 /* Parse the statement. */
11292 cp_parser_statement (parser, in_statement_expr, true, NULL);
11296 /* Return true if this is the C++20 version of range-based-for with
11297 init-statement. */
11299 static bool
11300 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11302 bool r = false;
11304 /* Save tokens so that we can put them back. */
11305 cp_lexer_save_tokens (parser->lexer);
11307 /* There has to be an unnested ; followed by an unnested :. */
11308 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11309 /*recovering=*/false,
11310 CPP_SEMICOLON,
11311 /*consume_paren=*/false) != -1)
11312 goto out;
11314 /* We found the semicolon, eat it now. */
11315 cp_lexer_consume_token (parser->lexer);
11317 /* Now look for ':' that is not nested in () or {}. */
11318 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11319 /*recovering=*/false,
11320 CPP_COLON,
11321 /*consume_paren=*/false) == -1);
11323 out:
11324 /* Roll back the tokens we skipped. */
11325 cp_lexer_rollback_tokens (parser->lexer);
11327 return r;
11330 /* Return true if we're looking at (init; cond), false otherwise. */
11332 static bool
11333 cp_parser_init_statement_p (cp_parser *parser)
11335 /* Save tokens so that we can put them back. */
11336 cp_lexer_save_tokens (parser->lexer);
11338 /* Look for ';' that is not nested in () or {}. */
11339 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11340 /*recovering=*/false,
11341 CPP_SEMICOLON,
11342 /*consume_paren=*/false);
11344 /* Roll back the tokens we skipped. */
11345 cp_lexer_rollback_tokens (parser->lexer);
11347 return ret == -1;
11350 /* Parse a selection-statement.
11352 selection-statement:
11353 if ( init-statement [opt] condition ) statement
11354 if ( init-statement [opt] condition ) statement else statement
11355 switch ( init-statement [opt] condition ) statement
11357 Returns the new IF_STMT or SWITCH_STMT.
11359 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11360 is a (possibly labeled) if statement which is not enclosed in
11361 braces and has an else clause. This is used to implement
11362 -Wparentheses.
11364 CHAIN is a vector of if-else-if conditions. This is used to implement
11365 -Wduplicated-cond. */
11367 static tree
11368 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11369 vec<tree> *chain)
11371 cp_token *token;
11372 enum rid keyword;
11373 token_indent_info guard_tinfo;
11375 if (if_p != NULL)
11376 *if_p = false;
11378 /* Peek at the next token. */
11379 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11380 guard_tinfo = get_token_indent_info (token);
11382 /* See what kind of keyword it is. */
11383 keyword = token->keyword;
11384 switch (keyword)
11386 case RID_IF:
11387 case RID_SWITCH:
11389 tree statement;
11390 tree condition;
11392 bool cx = false;
11393 if (keyword == RID_IF
11394 && cp_lexer_next_token_is_keyword (parser->lexer,
11395 RID_CONSTEXPR))
11397 cx = true;
11398 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11399 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11400 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11401 "with -std=c++17 or -std=gnu++17");
11404 /* Look for the `('. */
11405 matching_parens parens;
11406 if (!parens.require_open (parser))
11408 cp_parser_skip_to_end_of_statement (parser);
11409 return error_mark_node;
11412 /* Begin the selection-statement. */
11413 if (keyword == RID_IF)
11415 statement = begin_if_stmt ();
11416 IF_STMT_CONSTEXPR_P (statement) = cx;
11418 else
11419 statement = begin_switch_stmt ();
11421 /* Parse the optional init-statement. */
11422 if (cp_parser_init_statement_p (parser))
11424 tree decl;
11425 if (cxx_dialect < cxx17)
11426 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11427 "init-statement in selection statements only available "
11428 "with -std=c++17 or -std=gnu++17");
11429 cp_parser_init_statement (parser, &decl);
11432 /* Parse the condition. */
11433 condition = cp_parser_condition (parser);
11434 /* Look for the `)'. */
11435 if (!parens.require_close (parser))
11436 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11437 /*consume_paren=*/true);
11439 if (keyword == RID_IF)
11441 bool nested_if;
11442 unsigned char in_statement;
11444 /* Add the condition. */
11445 condition = finish_if_stmt_cond (condition, statement);
11447 if (warn_duplicated_cond)
11448 warn_duplicated_cond_add_or_warn (token->location, condition,
11449 &chain);
11451 /* Parse the then-clause. */
11452 in_statement = parser->in_statement;
11453 parser->in_statement |= IN_IF_STMT;
11455 /* Outside a template, the non-selected branch of a constexpr
11456 if is a 'discarded statement', i.e. unevaluated. */
11457 bool was_discarded = in_discarded_stmt;
11458 bool discard_then = (cx && !processing_template_decl
11459 && integer_zerop (condition));
11460 if (discard_then)
11462 in_discarded_stmt = true;
11463 ++c_inhibit_evaluation_warnings;
11466 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11467 guard_tinfo);
11469 parser->in_statement = in_statement;
11471 finish_then_clause (statement);
11473 if (discard_then)
11475 THEN_CLAUSE (statement) = NULL_TREE;
11476 in_discarded_stmt = was_discarded;
11477 --c_inhibit_evaluation_warnings;
11480 /* If the next token is `else', parse the else-clause. */
11481 if (cp_lexer_next_token_is_keyword (parser->lexer,
11482 RID_ELSE))
11484 bool discard_else = (cx && !processing_template_decl
11485 && integer_nonzerop (condition));
11486 if (discard_else)
11488 in_discarded_stmt = true;
11489 ++c_inhibit_evaluation_warnings;
11492 guard_tinfo
11493 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11494 /* Consume the `else' keyword. */
11495 cp_lexer_consume_token (parser->lexer);
11496 if (warn_duplicated_cond)
11498 if (cp_lexer_next_token_is_keyword (parser->lexer,
11499 RID_IF)
11500 && chain == NULL)
11502 /* We've got "if (COND) else if (COND2)". Start
11503 the condition chain and add COND as the first
11504 element. */
11505 chain = new vec<tree> ();
11506 if (!CONSTANT_CLASS_P (condition)
11507 && !TREE_SIDE_EFFECTS (condition))
11509 /* Wrap it in a NOP_EXPR so that we can set the
11510 location of the condition. */
11511 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11512 condition);
11513 SET_EXPR_LOCATION (e, token->location);
11514 chain->safe_push (e);
11517 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11518 RID_IF))
11520 /* This is if-else without subsequent if. Zap the
11521 condition chain; we would have already warned at
11522 this point. */
11523 delete chain;
11524 chain = NULL;
11527 begin_else_clause (statement);
11528 /* Parse the else-clause. */
11529 cp_parser_implicitly_scoped_statement (parser, NULL,
11530 guard_tinfo, chain);
11532 finish_else_clause (statement);
11534 /* If we are currently parsing a then-clause, then
11535 IF_P will not be NULL. We set it to true to
11536 indicate that this if statement has an else clause.
11537 This may trigger the Wparentheses warning below
11538 when we get back up to the parent if statement. */
11539 if (if_p != NULL)
11540 *if_p = true;
11542 if (discard_else)
11544 ELSE_CLAUSE (statement) = NULL_TREE;
11545 in_discarded_stmt = was_discarded;
11546 --c_inhibit_evaluation_warnings;
11549 else
11551 /* This if statement does not have an else clause. If
11552 NESTED_IF is true, then the then-clause has an if
11553 statement which does have an else clause. We warn
11554 about the potential ambiguity. */
11555 if (nested_if)
11556 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11557 "suggest explicit braces to avoid ambiguous"
11558 " %<else%>");
11559 if (warn_duplicated_cond)
11561 /* We don't need the condition chain anymore. */
11562 delete chain;
11563 chain = NULL;
11567 /* Now we're all done with the if-statement. */
11568 finish_if_stmt (statement);
11570 else
11572 bool in_switch_statement_p;
11573 unsigned char in_statement;
11575 /* Add the condition. */
11576 finish_switch_cond (condition, statement);
11578 /* Parse the body of the switch-statement. */
11579 in_switch_statement_p = parser->in_switch_statement_p;
11580 in_statement = parser->in_statement;
11581 parser->in_switch_statement_p = true;
11582 parser->in_statement |= IN_SWITCH_STMT;
11583 cp_parser_implicitly_scoped_statement (parser, if_p,
11584 guard_tinfo);
11585 parser->in_switch_statement_p = in_switch_statement_p;
11586 parser->in_statement = in_statement;
11588 /* Now we're all done with the switch-statement. */
11589 finish_switch_stmt (statement);
11592 return statement;
11594 break;
11596 default:
11597 cp_parser_error (parser, "expected selection-statement");
11598 return error_mark_node;
11602 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11603 If we have seen at least one decl-specifier, and the next token
11604 is not a parenthesis, then we must be looking at a declaration.
11605 (After "int (" we might be looking at a functional cast.) */
11607 static void
11608 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11609 bool any_specifiers_p)
11611 if (any_specifiers_p
11612 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11613 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11614 && !cp_parser_error_occurred (parser))
11615 cp_parser_commit_to_tentative_parse (parser);
11618 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11619 The declarator shall not specify a function or an array. Returns
11620 TRUE if the declarator is valid, FALSE otherwise. */
11622 static bool
11623 cp_parser_check_condition_declarator (cp_parser* parser,
11624 cp_declarator *declarator,
11625 location_t loc)
11627 if (declarator == cp_error_declarator
11628 || function_declarator_p (declarator)
11629 || declarator->kind == cdk_array)
11631 if (declarator == cp_error_declarator)
11632 /* Already complained. */;
11633 else if (declarator->kind == cdk_array)
11634 error_at (loc, "condition declares an array");
11635 else
11636 error_at (loc, "condition declares a function");
11637 if (parser->fully_implicit_function_template_p)
11638 abort_fully_implicit_template (parser);
11639 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11640 /*or_comma=*/false,
11641 /*consume_paren=*/false);
11642 return false;
11644 else
11645 return true;
11648 /* Parse a condition.
11650 condition:
11651 expression
11652 type-specifier-seq declarator = initializer-clause
11653 type-specifier-seq declarator braced-init-list
11655 GNU Extension:
11657 condition:
11658 type-specifier-seq declarator asm-specification [opt]
11659 attributes [opt] = assignment-expression
11661 Returns the expression that should be tested. */
11663 static tree
11664 cp_parser_condition (cp_parser* parser)
11666 cp_decl_specifier_seq type_specifiers;
11667 const char *saved_message;
11668 int declares_class_or_enum;
11670 /* Try the declaration first. */
11671 cp_parser_parse_tentatively (parser);
11672 /* New types are not allowed in the type-specifier-seq for a
11673 condition. */
11674 saved_message = parser->type_definition_forbidden_message;
11675 parser->type_definition_forbidden_message
11676 = G_("types may not be defined in conditions");
11677 /* Parse the type-specifier-seq. */
11678 cp_parser_decl_specifier_seq (parser,
11679 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11680 &type_specifiers,
11681 &declares_class_or_enum);
11682 /* Restore the saved message. */
11683 parser->type_definition_forbidden_message = saved_message;
11685 cp_parser_maybe_commit_to_declaration (parser,
11686 type_specifiers.any_specifiers_p);
11688 /* If all is well, we might be looking at a declaration. */
11689 if (!cp_parser_error_occurred (parser))
11691 tree decl;
11692 tree asm_specification;
11693 tree attributes;
11694 cp_declarator *declarator;
11695 tree initializer = NULL_TREE;
11696 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11698 /* Parse the declarator. */
11699 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11700 /*ctor_dtor_or_conv_p=*/NULL,
11701 /*parenthesized_p=*/NULL,
11702 /*member_p=*/false,
11703 /*friend_p=*/false);
11704 /* Parse the attributes. */
11705 attributes = cp_parser_attributes_opt (parser);
11706 /* Parse the asm-specification. */
11707 asm_specification = cp_parser_asm_specification_opt (parser);
11708 /* If the next token is not an `=' or '{', then we might still be
11709 looking at an expression. For example:
11711 if (A(a).x)
11713 looks like a decl-specifier-seq and a declarator -- but then
11714 there is no `=', so this is an expression. */
11715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11716 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11717 cp_parser_simulate_error (parser);
11719 /* If we did see an `=' or '{', then we are looking at a declaration
11720 for sure. */
11721 if (cp_parser_parse_definitely (parser))
11723 tree pushed_scope;
11724 bool non_constant_p;
11725 int flags = LOOKUP_ONLYCONVERTING;
11727 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11728 return error_mark_node;
11730 /* Create the declaration. */
11731 decl = start_decl (declarator, &type_specifiers,
11732 /*initialized_p=*/true,
11733 attributes, /*prefix_attributes=*/NULL_TREE,
11734 &pushed_scope);
11736 /* Parse the initializer. */
11737 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11739 initializer = cp_parser_braced_list (parser, &non_constant_p);
11740 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11741 flags = 0;
11743 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11745 /* Consume the `='. */
11746 cp_lexer_consume_token (parser->lexer);
11747 initializer = cp_parser_initializer_clause (parser,
11748 &non_constant_p);
11750 else
11752 cp_parser_error (parser, "expected initializer");
11753 initializer = error_mark_node;
11755 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11756 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11758 /* Process the initializer. */
11759 cp_finish_decl (decl,
11760 initializer, !non_constant_p,
11761 asm_specification,
11762 flags);
11764 if (pushed_scope)
11765 pop_scope (pushed_scope);
11767 return convert_from_reference (decl);
11770 /* If we didn't even get past the declarator successfully, we are
11771 definitely not looking at a declaration. */
11772 else
11773 cp_parser_abort_tentative_parse (parser);
11775 /* Otherwise, we are looking at an expression. */
11776 return cp_parser_expression (parser);
11779 /* Parses a for-statement or range-for-statement until the closing ')',
11780 not included. */
11782 static tree
11783 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11785 tree init, scope, decl;
11786 bool is_range_for;
11788 /* Begin the for-statement. */
11789 scope = begin_for_scope (&init);
11791 /* Parse the initialization. */
11792 is_range_for = cp_parser_init_statement (parser, &decl);
11794 if (is_range_for)
11795 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11796 else
11797 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11800 static tree
11801 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11802 unsigned short unroll)
11804 /* Normal for loop */
11805 tree condition = NULL_TREE;
11806 tree expression = NULL_TREE;
11807 tree stmt;
11809 stmt = begin_for_stmt (scope, init);
11810 /* The init-statement has already been parsed in
11811 cp_parser_init_statement, so no work is needed here. */
11812 finish_init_stmt (stmt);
11814 /* If there's a condition, process it. */
11815 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11816 condition = cp_parser_condition (parser);
11817 else if (ivdep)
11819 cp_parser_error (parser, "missing loop condition in loop with "
11820 "%<GCC ivdep%> pragma");
11821 condition = error_mark_node;
11823 else if (unroll)
11825 cp_parser_error (parser, "missing loop condition in loop with "
11826 "%<GCC unroll%> pragma");
11827 condition = error_mark_node;
11829 finish_for_cond (condition, stmt, ivdep, unroll);
11830 /* Look for the `;'. */
11831 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11833 /* If there's an expression, process it. */
11834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11835 expression = cp_parser_expression (parser);
11836 finish_for_expr (expression, stmt);
11838 return stmt;
11841 /* Tries to parse a range-based for-statement:
11843 range-based-for:
11844 decl-specifier-seq declarator : expression
11846 The decl-specifier-seq declarator and the `:' are already parsed by
11847 cp_parser_init_statement. If processing_template_decl it returns a
11848 newly created RANGE_FOR_STMT; if not, it is converted to a
11849 regular FOR_STMT. */
11851 static tree
11852 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11853 bool ivdep, unsigned short unroll)
11855 tree stmt, range_expr;
11856 auto_vec <cxx_binding *, 16> bindings;
11857 auto_vec <tree, 16> names;
11858 tree decomp_first_name = NULL_TREE;
11859 unsigned int decomp_cnt = 0;
11861 /* Get the range declaration momentarily out of the way so that
11862 the range expression doesn't clash with it. */
11863 if (range_decl != error_mark_node)
11865 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11867 tree v = DECL_VALUE_EXPR (range_decl);
11868 /* For decomposition declaration get all of the corresponding
11869 declarations out of the way. */
11870 if (TREE_CODE (v) == ARRAY_REF
11871 && VAR_P (TREE_OPERAND (v, 0))
11872 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11874 tree d = range_decl;
11875 range_decl = TREE_OPERAND (v, 0);
11876 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11877 decomp_first_name = d;
11878 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11880 tree name = DECL_NAME (d);
11881 names.safe_push (name);
11882 bindings.safe_push (IDENTIFIER_BINDING (name));
11883 IDENTIFIER_BINDING (name)
11884 = IDENTIFIER_BINDING (name)->previous;
11888 if (names.is_empty ())
11890 tree name = DECL_NAME (range_decl);
11891 names.safe_push (name);
11892 bindings.safe_push (IDENTIFIER_BINDING (name));
11893 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11899 bool expr_non_constant_p;
11900 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11902 else
11903 range_expr = cp_parser_expression (parser);
11905 /* Put the range declaration(s) back into scope. */
11906 for (unsigned int i = 0; i < names.length (); i++)
11908 cxx_binding *binding = bindings[i];
11909 binding->previous = IDENTIFIER_BINDING (names[i]);
11910 IDENTIFIER_BINDING (names[i]) = binding;
11913 /* If in template, STMT is converted to a normal for-statement
11914 at instantiation. If not, it is done just ahead. */
11915 if (processing_template_decl)
11917 if (check_for_bare_parameter_packs (range_expr))
11918 range_expr = error_mark_node;
11919 stmt = begin_range_for_stmt (scope, init);
11920 if (ivdep)
11921 RANGE_FOR_IVDEP (stmt) = 1;
11922 if (unroll)
11923 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11924 finish_range_for_decl (stmt, range_decl, range_expr);
11925 if (!type_dependent_expression_p (range_expr)
11926 /* do_auto_deduction doesn't mess with template init-lists. */
11927 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11928 do_range_for_auto_deduction (range_decl, range_expr);
11930 else
11932 stmt = begin_for_stmt (scope, init);
11933 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11934 decomp_first_name, decomp_cnt, ivdep,
11935 unroll);
11937 return stmt;
11940 /* Subroutine of cp_convert_range_for: given the initializer expression,
11941 builds up the range temporary. */
11943 static tree
11944 build_range_temp (tree range_expr)
11946 tree range_type, range_temp;
11948 /* Find out the type deduced by the declaration
11949 `auto &&__range = range_expr'. */
11950 range_type = cp_build_reference_type (make_auto (), true);
11951 range_type = do_auto_deduction (range_type, range_expr,
11952 type_uses_auto (range_type));
11954 /* Create the __range variable. */
11955 range_temp = build_decl (input_location, VAR_DECL,
11956 get_identifier ("__for_range"), range_type);
11957 TREE_USED (range_temp) = 1;
11958 DECL_ARTIFICIAL (range_temp) = 1;
11960 return range_temp;
11963 /* Used by cp_parser_range_for in template context: we aren't going to
11964 do a full conversion yet, but we still need to resolve auto in the
11965 type of the for-range-declaration if present. This is basically
11966 a shortcut version of cp_convert_range_for. */
11968 static void
11969 do_range_for_auto_deduction (tree decl, tree range_expr)
11971 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11972 if (auto_node)
11974 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11975 range_temp = convert_from_reference (build_range_temp (range_expr));
11976 iter_type = (cp_parser_perform_range_for_lookup
11977 (range_temp, &begin_dummy, &end_dummy));
11978 if (iter_type)
11980 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11981 iter_type);
11982 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11983 RO_UNARY_STAR,
11984 tf_warning_or_error);
11985 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11986 iter_decl, auto_node);
11991 /* Converts a range-based for-statement into a normal
11992 for-statement, as per the definition.
11994 for (RANGE_DECL : RANGE_EXPR)
11995 BLOCK
11997 should be equivalent to:
12000 auto &&__range = RANGE_EXPR;
12001 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12002 __begin != __end;
12003 ++__begin)
12005 RANGE_DECL = *__begin;
12006 BLOCK
12010 If RANGE_EXPR is an array:
12011 BEGIN_EXPR = __range
12012 END_EXPR = __range + ARRAY_SIZE(__range)
12013 Else if RANGE_EXPR has a member 'begin' or 'end':
12014 BEGIN_EXPR = __range.begin()
12015 END_EXPR = __range.end()
12016 Else:
12017 BEGIN_EXPR = begin(__range)
12018 END_EXPR = end(__range);
12020 If __range has a member 'begin' but not 'end', or vice versa, we must
12021 still use the second alternative (it will surely fail, however).
12022 When calling begin()/end() in the third alternative we must use
12023 argument dependent lookup, but always considering 'std' as an associated
12024 namespace. */
12026 tree
12027 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12028 tree decomp_first_name, unsigned int decomp_cnt,
12029 bool ivdep, unsigned short unroll)
12031 tree begin, end;
12032 tree iter_type, begin_expr, end_expr;
12033 tree condition, expression;
12035 range_expr = mark_lvalue_use (range_expr);
12037 if (range_decl == error_mark_node || range_expr == error_mark_node)
12038 /* If an error happened previously do nothing or else a lot of
12039 unhelpful errors would be issued. */
12040 begin_expr = end_expr = iter_type = error_mark_node;
12041 else
12043 tree range_temp;
12045 if (VAR_P (range_expr)
12046 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12047 /* Can't bind a reference to an array of runtime bound. */
12048 range_temp = range_expr;
12049 else
12051 range_temp = build_range_temp (range_expr);
12052 pushdecl (range_temp);
12053 cp_finish_decl (range_temp, range_expr,
12054 /*is_constant_init*/false, NULL_TREE,
12055 LOOKUP_ONLYCONVERTING);
12056 range_temp = convert_from_reference (range_temp);
12058 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12059 &begin_expr, &end_expr);
12062 /* The new for initialization statement. */
12063 begin = build_decl (input_location, VAR_DECL,
12064 get_identifier ("__for_begin"), iter_type);
12065 TREE_USED (begin) = 1;
12066 DECL_ARTIFICIAL (begin) = 1;
12067 pushdecl (begin);
12068 cp_finish_decl (begin, begin_expr,
12069 /*is_constant_init*/false, NULL_TREE,
12070 LOOKUP_ONLYCONVERTING);
12072 if (cxx_dialect >= cxx17)
12073 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12074 end = build_decl (input_location, VAR_DECL,
12075 get_identifier ("__for_end"), iter_type);
12076 TREE_USED (end) = 1;
12077 DECL_ARTIFICIAL (end) = 1;
12078 pushdecl (end);
12079 cp_finish_decl (end, end_expr,
12080 /*is_constant_init*/false, NULL_TREE,
12081 LOOKUP_ONLYCONVERTING);
12083 finish_init_stmt (statement);
12085 /* The new for condition. */
12086 condition = build_x_binary_op (input_location, NE_EXPR,
12087 begin, ERROR_MARK,
12088 end, ERROR_MARK,
12089 NULL, tf_warning_or_error);
12090 finish_for_cond (condition, statement, ivdep, unroll);
12092 /* The new increment expression. */
12093 expression = finish_unary_op_expr (input_location,
12094 PREINCREMENT_EXPR, begin,
12095 tf_warning_or_error);
12096 finish_for_expr (expression, statement);
12098 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12099 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12101 /* The declaration is initialized with *__begin inside the loop body. */
12102 cp_finish_decl (range_decl,
12103 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12104 tf_warning_or_error),
12105 /*is_constant_init*/false, NULL_TREE,
12106 LOOKUP_ONLYCONVERTING);
12107 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12108 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12110 return statement;
12113 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12114 We need to solve both at the same time because the method used
12115 depends on the existence of members begin or end.
12116 Returns the type deduced for the iterator expression. */
12118 static tree
12119 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12121 if (error_operand_p (range))
12123 *begin = *end = error_mark_node;
12124 return error_mark_node;
12127 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12129 error ("range-based %<for%> expression of type %qT "
12130 "has incomplete type", TREE_TYPE (range));
12131 *begin = *end = error_mark_node;
12132 return error_mark_node;
12134 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12136 /* If RANGE is an array, we will use pointer arithmetic. */
12137 *begin = decay_conversion (range, tf_warning_or_error);
12138 *end = build_binary_op (input_location, PLUS_EXPR,
12139 range,
12140 array_type_nelts_top (TREE_TYPE (range)),
12141 false);
12142 return TREE_TYPE (*begin);
12144 else
12146 /* If it is not an array, we must do a bit of magic. */
12147 tree id_begin, id_end;
12148 tree member_begin, member_end;
12150 *begin = *end = error_mark_node;
12152 id_begin = get_identifier ("begin");
12153 id_end = get_identifier ("end");
12154 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12155 /*protect=*/2, /*want_type=*/false,
12156 tf_warning_or_error);
12157 member_end = lookup_member (TREE_TYPE (range), id_end,
12158 /*protect=*/2, /*want_type=*/false,
12159 tf_warning_or_error);
12161 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12163 /* Use the member functions. */
12164 *begin = cp_parser_range_for_member_function (range, id_begin);
12165 *end = cp_parser_range_for_member_function (range, id_end);
12167 else
12169 /* Use global functions with ADL. */
12170 vec<tree, va_gc> *vec;
12171 vec = make_tree_vector ();
12173 vec_safe_push (vec, range);
12175 member_begin = perform_koenig_lookup (id_begin, vec,
12176 tf_warning_or_error);
12177 *begin = finish_call_expr (member_begin, &vec, false, true,
12178 tf_warning_or_error);
12179 member_end = perform_koenig_lookup (id_end, vec,
12180 tf_warning_or_error);
12181 *end = finish_call_expr (member_end, &vec, false, true,
12182 tf_warning_or_error);
12184 release_tree_vector (vec);
12187 /* Last common checks. */
12188 if (*begin == error_mark_node || *end == error_mark_node)
12190 /* If one of the expressions is an error do no more checks. */
12191 *begin = *end = error_mark_node;
12192 return error_mark_node;
12194 else if (type_dependent_expression_p (*begin)
12195 || type_dependent_expression_p (*end))
12196 /* Can happen, when, eg, in a template context, Koenig lookup
12197 can't resolve begin/end (c++/58503). */
12198 return NULL_TREE;
12199 else
12201 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12202 /* The unqualified type of the __begin and __end temporaries should
12203 be the same, as required by the multiple auto declaration. */
12204 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12206 if (cxx_dialect >= cxx17
12207 && (build_x_binary_op (input_location, NE_EXPR,
12208 *begin, ERROR_MARK,
12209 *end, ERROR_MARK,
12210 NULL, tf_none)
12211 != error_mark_node))
12212 /* P0184R0 allows __begin and __end to have different types,
12213 but make sure they are comparable so we can give a better
12214 diagnostic. */;
12215 else
12216 error ("inconsistent begin/end types in range-based %<for%> "
12217 "statement: %qT and %qT",
12218 TREE_TYPE (*begin), TREE_TYPE (*end));
12220 return iter_type;
12225 /* Helper function for cp_parser_perform_range_for_lookup.
12226 Builds a tree for RANGE.IDENTIFIER(). */
12228 static tree
12229 cp_parser_range_for_member_function (tree range, tree identifier)
12231 tree member, res;
12232 vec<tree, va_gc> *vec;
12234 member = finish_class_member_access_expr (range, identifier,
12235 false, tf_warning_or_error);
12236 if (member == error_mark_node)
12237 return error_mark_node;
12239 vec = make_tree_vector ();
12240 res = finish_call_expr (member, &vec,
12241 /*disallow_virtual=*/false,
12242 /*koenig_p=*/false,
12243 tf_warning_or_error);
12244 release_tree_vector (vec);
12245 return res;
12248 /* Parse an iteration-statement.
12250 iteration-statement:
12251 while ( condition ) statement
12252 do statement while ( expression ) ;
12253 for ( init-statement condition [opt] ; expression [opt] )
12254 statement
12256 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12258 static tree
12259 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12260 unsigned short unroll)
12262 cp_token *token;
12263 enum rid keyword;
12264 tree statement;
12265 unsigned char in_statement;
12266 token_indent_info guard_tinfo;
12268 /* Peek at the next token. */
12269 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12270 if (!token)
12271 return error_mark_node;
12273 guard_tinfo = get_token_indent_info (token);
12275 /* Remember whether or not we are already within an iteration
12276 statement. */
12277 in_statement = parser->in_statement;
12279 /* See what kind of keyword it is. */
12280 keyword = token->keyword;
12281 switch (keyword)
12283 case RID_WHILE:
12285 tree condition;
12287 /* Begin the while-statement. */
12288 statement = begin_while_stmt ();
12289 /* Look for the `('. */
12290 matching_parens parens;
12291 parens.require_open (parser);
12292 /* Parse the condition. */
12293 condition = cp_parser_condition (parser);
12294 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12295 /* Look for the `)'. */
12296 parens.require_close (parser);
12297 /* Parse the dependent statement. */
12298 parser->in_statement = IN_ITERATION_STMT;
12299 bool prev = note_iteration_stmt_body_start ();
12300 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12301 note_iteration_stmt_body_end (prev);
12302 parser->in_statement = in_statement;
12303 /* We're done with the while-statement. */
12304 finish_while_stmt (statement);
12306 break;
12308 case RID_DO:
12310 tree expression;
12312 /* Begin the do-statement. */
12313 statement = begin_do_stmt ();
12314 /* Parse the body of the do-statement. */
12315 parser->in_statement = IN_ITERATION_STMT;
12316 bool prev = note_iteration_stmt_body_start ();
12317 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12318 note_iteration_stmt_body_end (prev);
12319 parser->in_statement = in_statement;
12320 finish_do_body (statement);
12321 /* Look for the `while' keyword. */
12322 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12323 /* Look for the `('. */
12324 matching_parens parens;
12325 parens.require_open (parser);
12326 /* Parse the expression. */
12327 expression = cp_parser_expression (parser);
12328 /* We're done with the do-statement. */
12329 finish_do_stmt (expression, statement, ivdep, unroll);
12330 /* Look for the `)'. */
12331 parens.require_close (parser);
12332 /* Look for the `;'. */
12333 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12335 break;
12337 case RID_FOR:
12339 /* Look for the `('. */
12340 matching_parens parens;
12341 parens.require_open (parser);
12343 statement = cp_parser_for (parser, ivdep, unroll);
12345 /* Look for the `)'. */
12346 parens.require_close (parser);
12348 /* Parse the body of the for-statement. */
12349 parser->in_statement = IN_ITERATION_STMT;
12350 bool prev = note_iteration_stmt_body_start ();
12351 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12352 note_iteration_stmt_body_end (prev);
12353 parser->in_statement = in_statement;
12355 /* We're done with the for-statement. */
12356 finish_for_stmt (statement);
12358 break;
12360 default:
12361 cp_parser_error (parser, "expected iteration-statement");
12362 statement = error_mark_node;
12363 break;
12366 return statement;
12369 /* Parse a init-statement or the declarator of a range-based-for.
12370 Returns true if a range-based-for declaration is seen.
12372 init-statement:
12373 expression-statement
12374 simple-declaration */
12376 static bool
12377 cp_parser_init_statement (cp_parser *parser, tree *decl)
12379 /* If the next token is a `;', then we have an empty
12380 expression-statement. Grammatically, this is also a
12381 simple-declaration, but an invalid one, because it does not
12382 declare anything. Therefore, if we did not handle this case
12383 specially, we would issue an error message about an invalid
12384 declaration. */
12385 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12387 bool is_range_for = false;
12388 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12390 /* Try to parse the init-statement. */
12391 if (cp_parser_range_based_for_with_init_p (parser))
12393 tree dummy;
12394 cp_parser_parse_tentatively (parser);
12395 /* Parse the declaration. */
12396 cp_parser_simple_declaration (parser,
12397 /*function_definition_allowed_p=*/false,
12398 &dummy);
12399 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12400 if (!cp_parser_parse_definitely (parser))
12401 /* That didn't work, try to parse it as an expression-statement. */
12402 cp_parser_expression_statement (parser, NULL_TREE);
12404 if (cxx_dialect < cxx2a)
12406 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12407 "range-based %<for%> loops with initializer only "
12408 "available with -std=c++2a or -std=gnu++2a");
12409 *decl = error_mark_node;
12413 /* A colon is used in range-based for. */
12414 parser->colon_corrects_to_scope_p = false;
12416 /* We're going to speculatively look for a declaration, falling back
12417 to an expression, if necessary. */
12418 cp_parser_parse_tentatively (parser);
12419 /* Parse the declaration. */
12420 cp_parser_simple_declaration (parser,
12421 /*function_definition_allowed_p=*/false,
12422 decl);
12423 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12424 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12426 /* It is a range-for, consume the ':'. */
12427 cp_lexer_consume_token (parser->lexer);
12428 is_range_for = true;
12429 if (cxx_dialect < cxx11)
12430 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12431 "range-based %<for%> loops only available with "
12432 "-std=c++11 or -std=gnu++11");
12434 else
12435 /* The ';' is not consumed yet because we told
12436 cp_parser_simple_declaration not to. */
12437 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12439 if (cp_parser_parse_definitely (parser))
12440 return is_range_for;
12441 /* If the tentative parse failed, then we shall need to look for an
12442 expression-statement. */
12444 /* If we are here, it is an expression-statement. */
12445 cp_parser_expression_statement (parser, NULL_TREE);
12446 return false;
12449 /* Parse a jump-statement.
12451 jump-statement:
12452 break ;
12453 continue ;
12454 return expression [opt] ;
12455 return braced-init-list ;
12456 goto identifier ;
12458 GNU extension:
12460 jump-statement:
12461 goto * expression ;
12463 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12465 static tree
12466 cp_parser_jump_statement (cp_parser* parser)
12468 tree statement = error_mark_node;
12469 cp_token *token;
12470 enum rid keyword;
12471 unsigned char in_statement;
12473 /* Peek at the next token. */
12474 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12475 if (!token)
12476 return error_mark_node;
12478 /* See what kind of keyword it is. */
12479 keyword = token->keyword;
12480 switch (keyword)
12482 case RID_BREAK:
12483 in_statement = parser->in_statement & ~IN_IF_STMT;
12484 switch (in_statement)
12486 case 0:
12487 error_at (token->location, "break statement not within loop or switch");
12488 break;
12489 default:
12490 gcc_assert ((in_statement & IN_SWITCH_STMT)
12491 || in_statement == IN_ITERATION_STMT);
12492 statement = finish_break_stmt ();
12493 if (in_statement == IN_ITERATION_STMT)
12494 break_maybe_infinite_loop ();
12495 break;
12496 case IN_OMP_BLOCK:
12497 error_at (token->location, "invalid exit from OpenMP structured block");
12498 break;
12499 case IN_OMP_FOR:
12500 error_at (token->location, "break statement used with OpenMP for loop");
12501 break;
12503 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12504 break;
12506 case RID_CONTINUE:
12507 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12509 case 0:
12510 error_at (token->location, "continue statement not within a loop");
12511 break;
12512 /* Fall through. */
12513 case IN_ITERATION_STMT:
12514 case IN_OMP_FOR:
12515 statement = finish_continue_stmt ();
12516 break;
12517 case IN_OMP_BLOCK:
12518 error_at (token->location, "invalid exit from OpenMP structured block");
12519 break;
12520 default:
12521 gcc_unreachable ();
12523 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12524 break;
12526 case RID_RETURN:
12528 tree expr;
12529 bool expr_non_constant_p;
12531 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12533 cp_lexer_set_source_position (parser->lexer);
12534 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12535 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12537 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12538 expr = cp_parser_expression (parser);
12539 else
12540 /* If the next token is a `;', then there is no
12541 expression. */
12542 expr = NULL_TREE;
12543 /* Build the return-statement. */
12544 if (current_function_auto_return_pattern && in_discarded_stmt)
12545 /* Don't deduce from a discarded return statement. */;
12546 else
12547 statement = finish_return_stmt (expr);
12548 /* Look for the final `;'. */
12549 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12551 break;
12553 case RID_GOTO:
12554 if (parser->in_function_body
12555 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12557 error ("%<goto%> in %<constexpr%> function");
12558 cp_function_chain->invalid_constexpr = true;
12561 /* Create the goto-statement. */
12562 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12564 /* Issue a warning about this use of a GNU extension. */
12565 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12566 /* Consume the '*' token. */
12567 cp_lexer_consume_token (parser->lexer);
12568 /* Parse the dependent expression. */
12569 finish_goto_stmt (cp_parser_expression (parser));
12571 else
12572 finish_goto_stmt (cp_parser_identifier (parser));
12573 /* Look for the final `;'. */
12574 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12575 break;
12577 default:
12578 cp_parser_error (parser, "expected jump-statement");
12579 break;
12582 return statement;
12585 /* Parse a declaration-statement.
12587 declaration-statement:
12588 block-declaration */
12590 static void
12591 cp_parser_declaration_statement (cp_parser* parser)
12593 void *p;
12595 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12596 p = obstack_alloc (&declarator_obstack, 0);
12598 /* Parse the block-declaration. */
12599 cp_parser_block_declaration (parser, /*statement_p=*/true);
12601 /* Free any declarators allocated. */
12602 obstack_free (&declarator_obstack, p);
12605 /* Some dependent statements (like `if (cond) statement'), are
12606 implicitly in their own scope. In other words, if the statement is
12607 a single statement (as opposed to a compound-statement), it is
12608 none-the-less treated as if it were enclosed in braces. Any
12609 declarations appearing in the dependent statement are out of scope
12610 after control passes that point. This function parses a statement,
12611 but ensures that is in its own scope, even if it is not a
12612 compound-statement.
12614 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12615 is a (possibly labeled) if statement which is not enclosed in
12616 braces and has an else clause. This is used to implement
12617 -Wparentheses.
12619 CHAIN is a vector of if-else-if conditions. This is used to implement
12620 -Wduplicated-cond.
12622 Returns the new statement. */
12624 static tree
12625 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12626 const token_indent_info &guard_tinfo,
12627 vec<tree> *chain)
12629 tree statement;
12630 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12631 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12632 token_indent_info body_tinfo
12633 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12635 if (if_p != NULL)
12636 *if_p = false;
12638 /* Mark if () ; with a special NOP_EXPR. */
12639 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12641 cp_lexer_consume_token (parser->lexer);
12642 statement = add_stmt (build_empty_stmt (body_loc));
12644 if (guard_tinfo.keyword == RID_IF
12645 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12646 warning_at (body_loc, OPT_Wempty_body,
12647 "suggest braces around empty body in an %<if%> statement");
12648 else if (guard_tinfo.keyword == RID_ELSE)
12649 warning_at (body_loc, OPT_Wempty_body,
12650 "suggest braces around empty body in an %<else%> statement");
12652 /* if a compound is opened, we simply parse the statement directly. */
12653 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12654 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12655 /* If the token is not a `{', then we must take special action. */
12656 else
12658 /* Create a compound-statement. */
12659 statement = begin_compound_stmt (0);
12660 /* Parse the dependent-statement. */
12661 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12662 &body_loc_after_labels);
12663 /* Finish the dummy compound-statement. */
12664 finish_compound_stmt (statement);
12667 token_indent_info next_tinfo
12668 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12669 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12671 if (body_loc_after_labels != UNKNOWN_LOCATION
12672 && next_tinfo.type != CPP_SEMICOLON)
12673 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12674 guard_tinfo.location, guard_tinfo.keyword);
12676 /* Return the statement. */
12677 return statement;
12680 /* For some dependent statements (like `while (cond) statement'), we
12681 have already created a scope. Therefore, even if the dependent
12682 statement is a compound-statement, we do not want to create another
12683 scope. */
12685 static void
12686 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12687 const token_indent_info &guard_tinfo)
12689 /* If the token is a `{', then we must take special action. */
12690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12692 token_indent_info body_tinfo
12693 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12694 location_t loc_after_labels = UNKNOWN_LOCATION;
12696 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12697 &loc_after_labels);
12698 token_indent_info next_tinfo
12699 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12700 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12702 if (loc_after_labels != UNKNOWN_LOCATION
12703 && next_tinfo.type != CPP_SEMICOLON)
12704 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12705 guard_tinfo.location,
12706 guard_tinfo.keyword);
12708 else
12710 /* Avoid calling cp_parser_compound_statement, so that we
12711 don't create a new scope. Do everything else by hand. */
12712 matching_braces braces;
12713 braces.require_open (parser);
12714 /* If the next keyword is `__label__' we have a label declaration. */
12715 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12716 cp_parser_label_declaration (parser);
12717 /* Parse an (optional) statement-seq. */
12718 cp_parser_statement_seq_opt (parser, NULL_TREE);
12719 braces.require_close (parser);
12723 /* Declarations [gram.dcl.dcl] */
12725 /* Parse an optional declaration-sequence.
12727 declaration-seq:
12728 declaration
12729 declaration-seq declaration */
12731 static void
12732 cp_parser_declaration_seq_opt (cp_parser* parser)
12734 while (true)
12736 cp_token *token;
12738 token = cp_lexer_peek_token (parser->lexer);
12740 if (token->type == CPP_CLOSE_BRACE
12741 || token->type == CPP_EOF
12742 || token->type == CPP_PRAGMA_EOL)
12743 break;
12745 if (token->type == CPP_SEMICOLON)
12747 /* A declaration consisting of a single semicolon is
12748 invalid. Allow it unless we're being pedantic. */
12749 cp_lexer_consume_token (parser->lexer);
12750 if (!in_system_header_at (input_location))
12751 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12752 continue;
12755 /* If we're entering or exiting a region that's implicitly
12756 extern "C", modify the lang context appropriately. */
12757 if (!parser->implicit_extern_c && token->implicit_extern_c)
12759 push_lang_context (lang_name_c);
12760 parser->implicit_extern_c = true;
12762 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12764 pop_lang_context ();
12765 parser->implicit_extern_c = false;
12768 if (token->type == CPP_PRAGMA)
12770 /* A top-level declaration can consist solely of a #pragma.
12771 A nested declaration cannot, so this is done here and not
12772 in cp_parser_declaration. (A #pragma at block scope is
12773 handled in cp_parser_statement.) */
12774 cp_parser_pragma (parser, pragma_external, NULL);
12775 continue;
12778 /* Parse the declaration itself. */
12779 cp_parser_declaration (parser);
12783 /* Parse a declaration.
12785 declaration:
12786 block-declaration
12787 function-definition
12788 template-declaration
12789 explicit-instantiation
12790 explicit-specialization
12791 linkage-specification
12792 namespace-definition
12794 C++17:
12795 deduction-guide
12797 GNU extension:
12799 declaration:
12800 __extension__ declaration */
12802 static void
12803 cp_parser_declaration (cp_parser* parser)
12805 cp_token token1;
12806 cp_token token2;
12807 int saved_pedantic;
12808 void *p;
12809 tree attributes = NULL_TREE;
12811 /* Check for the `__extension__' keyword. */
12812 if (cp_parser_extension_opt (parser, &saved_pedantic))
12814 /* Parse the qualified declaration. */
12815 cp_parser_declaration (parser);
12816 /* Restore the PEDANTIC flag. */
12817 pedantic = saved_pedantic;
12819 return;
12822 /* Try to figure out what kind of declaration is present. */
12823 token1 = *cp_lexer_peek_token (parser->lexer);
12825 if (token1.type != CPP_EOF)
12826 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12827 else
12829 token2.type = CPP_EOF;
12830 token2.keyword = RID_MAX;
12833 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12834 p = obstack_alloc (&declarator_obstack, 0);
12836 /* If the next token is `extern' and the following token is a string
12837 literal, then we have a linkage specification. */
12838 if (token1.keyword == RID_EXTERN
12839 && cp_parser_is_pure_string_literal (&token2))
12840 cp_parser_linkage_specification (parser);
12841 /* If the next token is `template', then we have either a template
12842 declaration, an explicit instantiation, or an explicit
12843 specialization. */
12844 else if (token1.keyword == RID_TEMPLATE)
12846 /* `template <>' indicates a template specialization. */
12847 if (token2.type == CPP_LESS
12848 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12849 cp_parser_explicit_specialization (parser);
12850 /* `template <' indicates a template declaration. */
12851 else if (token2.type == CPP_LESS)
12852 cp_parser_template_declaration (parser, /*member_p=*/false);
12853 /* Anything else must be an explicit instantiation. */
12854 else
12855 cp_parser_explicit_instantiation (parser);
12857 /* If the next token is `export', then we have a template
12858 declaration. */
12859 else if (token1.keyword == RID_EXPORT)
12860 cp_parser_template_declaration (parser, /*member_p=*/false);
12861 /* If the next token is `extern', 'static' or 'inline' and the one
12862 after that is `template', we have a GNU extended explicit
12863 instantiation directive. */
12864 else if (cp_parser_allow_gnu_extensions_p (parser)
12865 && (token1.keyword == RID_EXTERN
12866 || token1.keyword == RID_STATIC
12867 || token1.keyword == RID_INLINE)
12868 && token2.keyword == RID_TEMPLATE)
12869 cp_parser_explicit_instantiation (parser);
12870 /* If the next token is `namespace', check for a named or unnamed
12871 namespace definition. */
12872 else if (token1.keyword == RID_NAMESPACE
12873 && (/* A named namespace definition. */
12874 (token2.type == CPP_NAME
12875 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12876 != CPP_EQ))
12877 || (token2.type == CPP_OPEN_SQUARE
12878 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12879 == CPP_OPEN_SQUARE)
12880 /* An unnamed namespace definition. */
12881 || token2.type == CPP_OPEN_BRACE
12882 || token2.keyword == RID_ATTRIBUTE))
12883 cp_parser_namespace_definition (parser);
12884 /* An inline (associated) namespace definition. */
12885 else if (token1.keyword == RID_INLINE
12886 && token2.keyword == RID_NAMESPACE)
12887 cp_parser_namespace_definition (parser);
12888 /* Objective-C++ declaration/definition. */
12889 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12890 cp_parser_objc_declaration (parser, NULL_TREE);
12891 else if (c_dialect_objc ()
12892 && token1.keyword == RID_ATTRIBUTE
12893 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12894 cp_parser_objc_declaration (parser, attributes);
12895 /* At this point we may have a template declared by a concept
12896 introduction. */
12897 else if (flag_concepts
12898 && cp_parser_template_declaration_after_export (parser,
12899 /*member_p=*/false))
12900 /* We did. */;
12901 else
12902 /* Try to parse a block-declaration, or a function-definition. */
12903 cp_parser_block_declaration (parser, /*statement_p=*/false);
12905 /* Free any declarators allocated. */
12906 obstack_free (&declarator_obstack, p);
12909 /* Parse a block-declaration.
12911 block-declaration:
12912 simple-declaration
12913 asm-definition
12914 namespace-alias-definition
12915 using-declaration
12916 using-directive
12918 GNU Extension:
12920 block-declaration:
12921 __extension__ block-declaration
12923 C++0x Extension:
12925 block-declaration:
12926 static_assert-declaration
12928 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12929 part of a declaration-statement. */
12931 static void
12932 cp_parser_block_declaration (cp_parser *parser,
12933 bool statement_p)
12935 cp_token *token1;
12936 int saved_pedantic;
12938 /* Check for the `__extension__' keyword. */
12939 if (cp_parser_extension_opt (parser, &saved_pedantic))
12941 /* Parse the qualified declaration. */
12942 cp_parser_block_declaration (parser, statement_p);
12943 /* Restore the PEDANTIC flag. */
12944 pedantic = saved_pedantic;
12946 return;
12949 /* Peek at the next token to figure out which kind of declaration is
12950 present. */
12951 token1 = cp_lexer_peek_token (parser->lexer);
12953 /* If the next keyword is `asm', we have an asm-definition. */
12954 if (token1->keyword == RID_ASM)
12956 if (statement_p)
12957 cp_parser_commit_to_tentative_parse (parser);
12958 cp_parser_asm_definition (parser);
12960 /* If the next keyword is `namespace', we have a
12961 namespace-alias-definition. */
12962 else if (token1->keyword == RID_NAMESPACE)
12963 cp_parser_namespace_alias_definition (parser);
12964 /* If the next keyword is `using', we have a
12965 using-declaration, a using-directive, or an alias-declaration. */
12966 else if (token1->keyword == RID_USING)
12968 cp_token *token2;
12970 if (statement_p)
12971 cp_parser_commit_to_tentative_parse (parser);
12972 /* If the token after `using' is `namespace', then we have a
12973 using-directive. */
12974 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12975 if (token2->keyword == RID_NAMESPACE)
12976 cp_parser_using_directive (parser);
12977 /* If the second token after 'using' is '=', then we have an
12978 alias-declaration. */
12979 else if (cxx_dialect >= cxx11
12980 && token2->type == CPP_NAME
12981 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12982 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12983 cp_parser_alias_declaration (parser);
12984 /* Otherwise, it's a using-declaration. */
12985 else
12986 cp_parser_using_declaration (parser,
12987 /*access_declaration_p=*/false);
12989 /* If the next keyword is `__label__' we have a misplaced label
12990 declaration. */
12991 else if (token1->keyword == RID_LABEL)
12993 cp_lexer_consume_token (parser->lexer);
12994 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12995 cp_parser_skip_to_end_of_statement (parser);
12996 /* If the next token is now a `;', consume it. */
12997 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12998 cp_lexer_consume_token (parser->lexer);
13000 /* If the next token is `static_assert' we have a static assertion. */
13001 else if (token1->keyword == RID_STATIC_ASSERT)
13002 cp_parser_static_assert (parser, /*member_p=*/false);
13003 /* Anything else must be a simple-declaration. */
13004 else
13005 cp_parser_simple_declaration (parser, !statement_p,
13006 /*maybe_range_for_decl*/NULL);
13009 /* Parse a simple-declaration.
13011 simple-declaration:
13012 decl-specifier-seq [opt] init-declarator-list [opt] ;
13013 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13014 brace-or-equal-initializer ;
13016 init-declarator-list:
13017 init-declarator
13018 init-declarator-list , init-declarator
13020 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13021 function-definition as a simple-declaration.
13023 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13024 parsed declaration if it is an uninitialized single declarator not followed
13025 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13026 if present, will not be consumed. */
13028 static void
13029 cp_parser_simple_declaration (cp_parser* parser,
13030 bool function_definition_allowed_p,
13031 tree *maybe_range_for_decl)
13033 cp_decl_specifier_seq decl_specifiers;
13034 int declares_class_or_enum;
13035 bool saw_declarator;
13036 location_t comma_loc = UNKNOWN_LOCATION;
13037 location_t init_loc = UNKNOWN_LOCATION;
13039 if (maybe_range_for_decl)
13040 *maybe_range_for_decl = NULL_TREE;
13042 /* Defer access checks until we know what is being declared; the
13043 checks for names appearing in the decl-specifier-seq should be
13044 done as if we were in the scope of the thing being declared. */
13045 push_deferring_access_checks (dk_deferred);
13047 /* Parse the decl-specifier-seq. We have to keep track of whether
13048 or not the decl-specifier-seq declares a named class or
13049 enumeration type, since that is the only case in which the
13050 init-declarator-list is allowed to be empty.
13052 [dcl.dcl]
13054 In a simple-declaration, the optional init-declarator-list can be
13055 omitted only when declaring a class or enumeration, that is when
13056 the decl-specifier-seq contains either a class-specifier, an
13057 elaborated-type-specifier, or an enum-specifier. */
13058 cp_parser_decl_specifier_seq (parser,
13059 CP_PARSER_FLAGS_OPTIONAL,
13060 &decl_specifiers,
13061 &declares_class_or_enum);
13062 /* We no longer need to defer access checks. */
13063 stop_deferring_access_checks ();
13065 /* In a block scope, a valid declaration must always have a
13066 decl-specifier-seq. By not trying to parse declarators, we can
13067 resolve the declaration/expression ambiguity more quickly. */
13068 if (!function_definition_allowed_p
13069 && !decl_specifiers.any_specifiers_p)
13071 cp_parser_error (parser, "expected declaration");
13072 goto done;
13075 /* If the next two tokens are both identifiers, the code is
13076 erroneous. The usual cause of this situation is code like:
13078 T t;
13080 where "T" should name a type -- but does not. */
13081 if (!decl_specifiers.any_type_specifiers_p
13082 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13084 /* If parsing tentatively, we should commit; we really are
13085 looking at a declaration. */
13086 cp_parser_commit_to_tentative_parse (parser);
13087 /* Give up. */
13088 goto done;
13091 cp_parser_maybe_commit_to_declaration (parser,
13092 decl_specifiers.any_specifiers_p);
13094 /* Look for C++17 decomposition declaration. */
13095 for (size_t n = 1; ; n++)
13096 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13097 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13098 continue;
13099 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13100 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13101 && decl_specifiers.any_specifiers_p)
13103 tree decl
13104 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13105 maybe_range_for_decl,
13106 &init_loc);
13108 /* The next token should be either a `,' or a `;'. */
13109 cp_token *token = cp_lexer_peek_token (parser->lexer);
13110 /* If it's a `;', we are done. */
13111 if (token->type == CPP_SEMICOLON)
13112 goto finish;
13113 else if (maybe_range_for_decl)
13115 if (*maybe_range_for_decl == NULL_TREE)
13116 *maybe_range_for_decl = error_mark_node;
13117 goto finish;
13119 /* Anything else is an error. */
13120 else
13122 /* If we have already issued an error message we don't need
13123 to issue another one. */
13124 if ((decl != error_mark_node
13125 && DECL_INITIAL (decl) != error_mark_node)
13126 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13127 cp_parser_error (parser, "expected %<,%> or %<;%>");
13128 /* Skip tokens until we reach the end of the statement. */
13129 cp_parser_skip_to_end_of_statement (parser);
13130 /* If the next token is now a `;', consume it. */
13131 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13132 cp_lexer_consume_token (parser->lexer);
13133 goto done;
13136 else
13137 break;
13139 tree last_type;
13140 bool auto_specifier_p;
13141 /* NULL_TREE if both variable and function declaration are allowed,
13142 error_mark_node if function declaration are not allowed and
13143 a FUNCTION_DECL that should be diagnosed if it is followed by
13144 variable declarations. */
13145 tree auto_function_declaration;
13147 last_type = NULL_TREE;
13148 auto_specifier_p
13149 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13150 auto_function_declaration = NULL_TREE;
13152 /* Keep going until we hit the `;' at the end of the simple
13153 declaration. */
13154 saw_declarator = false;
13155 while (cp_lexer_next_token_is_not (parser->lexer,
13156 CPP_SEMICOLON))
13158 cp_token *token;
13159 bool function_definition_p;
13160 tree decl;
13161 tree auto_result = NULL_TREE;
13163 if (saw_declarator)
13165 /* If we are processing next declarator, comma is expected */
13166 token = cp_lexer_peek_token (parser->lexer);
13167 gcc_assert (token->type == CPP_COMMA);
13168 cp_lexer_consume_token (parser->lexer);
13169 if (maybe_range_for_decl)
13171 *maybe_range_for_decl = error_mark_node;
13172 if (comma_loc == UNKNOWN_LOCATION)
13173 comma_loc = token->location;
13176 else
13177 saw_declarator = true;
13179 /* Parse the init-declarator. */
13180 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13181 /*checks=*/NULL,
13182 function_definition_allowed_p,
13183 /*member_p=*/false,
13184 declares_class_or_enum,
13185 &function_definition_p,
13186 maybe_range_for_decl,
13187 &init_loc,
13188 &auto_result);
13189 /* If an error occurred while parsing tentatively, exit quickly.
13190 (That usually happens when in the body of a function; each
13191 statement is treated as a declaration-statement until proven
13192 otherwise.) */
13193 if (cp_parser_error_occurred (parser))
13194 goto done;
13196 if (auto_specifier_p && cxx_dialect >= cxx14)
13198 /* If the init-declarator-list contains more than one
13199 init-declarator, they shall all form declarations of
13200 variables. */
13201 if (auto_function_declaration == NULL_TREE)
13202 auto_function_declaration
13203 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13204 else if (TREE_CODE (decl) == FUNCTION_DECL
13205 || auto_function_declaration != error_mark_node)
13207 error_at (decl_specifiers.locations[ds_type_spec],
13208 "non-variable %qD in declaration with more than one "
13209 "declarator with placeholder type",
13210 TREE_CODE (decl) == FUNCTION_DECL
13211 ? decl : auto_function_declaration);
13212 auto_function_declaration = error_mark_node;
13216 if (auto_result
13217 && (!processing_template_decl || !type_uses_auto (auto_result)))
13219 if (last_type
13220 && last_type != error_mark_node
13221 && !same_type_p (auto_result, last_type))
13223 /* If the list of declarators contains more than one declarator,
13224 the type of each declared variable is determined as described
13225 above. If the type deduced for the template parameter U is not
13226 the same in each deduction, the program is ill-formed. */
13227 error_at (decl_specifiers.locations[ds_type_spec],
13228 "inconsistent deduction for %qT: %qT and then %qT",
13229 decl_specifiers.type, last_type, auto_result);
13230 last_type = error_mark_node;
13232 else
13233 last_type = auto_result;
13236 /* Handle function definitions specially. */
13237 if (function_definition_p)
13239 /* If the next token is a `,', then we are probably
13240 processing something like:
13242 void f() {}, *p;
13244 which is erroneous. */
13245 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13247 cp_token *token = cp_lexer_peek_token (parser->lexer);
13248 error_at (token->location,
13249 "mixing"
13250 " declarations and function-definitions is forbidden");
13252 /* Otherwise, we're done with the list of declarators. */
13253 else
13255 pop_deferring_access_checks ();
13256 return;
13259 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13260 *maybe_range_for_decl = decl;
13261 /* The next token should be either a `,' or a `;'. */
13262 token = cp_lexer_peek_token (parser->lexer);
13263 /* If it's a `,', there are more declarators to come. */
13264 if (token->type == CPP_COMMA)
13265 /* will be consumed next time around */;
13266 /* If it's a `;', we are done. */
13267 else if (token->type == CPP_SEMICOLON)
13268 break;
13269 else if (maybe_range_for_decl)
13271 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13272 permerror (decl_specifiers.locations[ds_type_spec],
13273 "types may not be defined in a for-range-declaration");
13274 break;
13276 /* Anything else is an error. */
13277 else
13279 /* If we have already issued an error message we don't need
13280 to issue another one. */
13281 if ((decl != error_mark_node
13282 && DECL_INITIAL (decl) != error_mark_node)
13283 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13284 cp_parser_error (parser, "expected %<,%> or %<;%>");
13285 /* Skip tokens until we reach the end of the statement. */
13286 cp_parser_skip_to_end_of_statement (parser);
13287 /* If the next token is now a `;', consume it. */
13288 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13289 cp_lexer_consume_token (parser->lexer);
13290 goto done;
13292 /* After the first time around, a function-definition is not
13293 allowed -- even if it was OK at first. For example:
13295 int i, f() {}
13297 is not valid. */
13298 function_definition_allowed_p = false;
13301 /* Issue an error message if no declarators are present, and the
13302 decl-specifier-seq does not itself declare a class or
13303 enumeration: [dcl.dcl]/3. */
13304 if (!saw_declarator)
13306 if (cp_parser_declares_only_class_p (parser))
13308 if (!declares_class_or_enum
13309 && decl_specifiers.type
13310 && OVERLOAD_TYPE_P (decl_specifiers.type))
13311 /* Ensure an error is issued anyway when finish_decltype_type,
13312 called via cp_parser_decl_specifier_seq, returns a class or
13313 an enumeration (c++/51786). */
13314 decl_specifiers.type = NULL_TREE;
13315 shadow_tag (&decl_specifiers);
13317 /* Perform any deferred access checks. */
13318 perform_deferred_access_checks (tf_warning_or_error);
13321 /* Consume the `;'. */
13322 finish:
13323 if (!maybe_range_for_decl)
13324 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13325 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13327 if (init_loc != UNKNOWN_LOCATION)
13328 error_at (init_loc, "initializer in range-based %<for%> loop");
13329 if (comma_loc != UNKNOWN_LOCATION)
13330 error_at (comma_loc,
13331 "multiple declarations in range-based %<for%> loop");
13334 done:
13335 pop_deferring_access_checks ();
13338 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13339 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13340 initializer ; */
13342 static tree
13343 cp_parser_decomposition_declaration (cp_parser *parser,
13344 cp_decl_specifier_seq *decl_specifiers,
13345 tree *maybe_range_for_decl,
13346 location_t *init_loc)
13348 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13349 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13350 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13352 /* Parse the identifier-list. */
13353 auto_vec<cp_expr, 10> v;
13354 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13355 while (true)
13357 cp_expr e = cp_parser_identifier (parser);
13358 if (e.get_value () == error_mark_node)
13359 break;
13360 v.safe_push (e);
13361 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13362 break;
13363 cp_lexer_consume_token (parser->lexer);
13366 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13367 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13369 end_loc = UNKNOWN_LOCATION;
13370 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13371 false);
13372 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13373 cp_lexer_consume_token (parser->lexer);
13374 else
13376 cp_parser_skip_to_end_of_statement (parser);
13377 return error_mark_node;
13381 if (cxx_dialect < cxx17)
13382 pedwarn (loc, 0, "structured bindings only available with "
13383 "-std=c++17 or -std=gnu++17");
13385 tree pushed_scope;
13386 cp_declarator *declarator = make_declarator (cdk_decomp);
13387 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13388 declarator->id_loc = loc;
13389 if (ref_qual != REF_QUAL_NONE)
13390 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13391 ref_qual == REF_QUAL_RVALUE,
13392 NULL_TREE);
13393 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13394 NULL_TREE, decl_specifiers->attributes,
13395 &pushed_scope);
13396 tree orig_decl = decl;
13398 unsigned int i;
13399 cp_expr e;
13400 cp_decl_specifier_seq decl_specs;
13401 clear_decl_specs (&decl_specs);
13402 decl_specs.type = make_auto ();
13403 tree prev = decl;
13404 FOR_EACH_VEC_ELT (v, i, e)
13406 if (i == 0)
13407 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13408 else
13409 declarator->u.id.unqualified_name = e.get_value ();
13410 declarator->id_loc = e.get_location ();
13411 tree elt_pushed_scope;
13412 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13413 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13414 if (decl2 == error_mark_node)
13415 decl = error_mark_node;
13416 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13418 /* Ensure we've diagnosed redeclaration if we aren't creating
13419 a new VAR_DECL. */
13420 gcc_assert (errorcount);
13421 decl = error_mark_node;
13423 else
13424 prev = decl2;
13425 if (elt_pushed_scope)
13426 pop_scope (elt_pushed_scope);
13429 if (v.is_empty ())
13431 error_at (loc, "empty structured binding declaration");
13432 decl = error_mark_node;
13435 if (maybe_range_for_decl == NULL
13436 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13438 bool non_constant_p = false, is_direct_init = false;
13439 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13440 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13441 &non_constant_p);
13442 if (initializer == NULL_TREE
13443 || (TREE_CODE (initializer) == TREE_LIST
13444 && TREE_CHAIN (initializer))
13445 || (is_direct_init
13446 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13447 && CONSTRUCTOR_NELTS (initializer) != 1))
13449 error_at (loc, "invalid initializer for structured binding "
13450 "declaration");
13451 initializer = error_mark_node;
13454 if (decl != error_mark_node)
13456 cp_maybe_mangle_decomp (decl, prev, v.length ());
13457 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13458 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13459 cp_finish_decomp (decl, prev, v.length ());
13462 else if (decl != error_mark_node)
13464 *maybe_range_for_decl = prev;
13465 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13466 the underlying DECL. */
13467 cp_finish_decomp (decl, prev, v.length ());
13470 if (pushed_scope)
13471 pop_scope (pushed_scope);
13473 if (decl == error_mark_node && DECL_P (orig_decl))
13475 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13476 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13479 return decl;
13482 /* Parse a decl-specifier-seq.
13484 decl-specifier-seq:
13485 decl-specifier-seq [opt] decl-specifier
13486 decl-specifier attribute-specifier-seq [opt] (C++11)
13488 decl-specifier:
13489 storage-class-specifier
13490 type-specifier
13491 function-specifier
13492 friend
13493 typedef
13495 GNU Extension:
13497 decl-specifier:
13498 attributes
13500 Concepts Extension:
13502 decl-specifier:
13503 concept
13505 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13507 The parser flags FLAGS is used to control type-specifier parsing.
13509 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13510 flags:
13512 1: one of the decl-specifiers is an elaborated-type-specifier
13513 (i.e., a type declaration)
13514 2: one of the decl-specifiers is an enum-specifier or a
13515 class-specifier (i.e., a type definition)
13519 static void
13520 cp_parser_decl_specifier_seq (cp_parser* parser,
13521 cp_parser_flags flags,
13522 cp_decl_specifier_seq *decl_specs,
13523 int* declares_class_or_enum)
13525 bool constructor_possible_p = !parser->in_declarator_p;
13526 bool found_decl_spec = false;
13527 cp_token *start_token = NULL;
13528 cp_decl_spec ds;
13530 /* Clear DECL_SPECS. */
13531 clear_decl_specs (decl_specs);
13533 /* Assume no class or enumeration type is declared. */
13534 *declares_class_or_enum = 0;
13536 /* Keep reading specifiers until there are no more to read. */
13537 while (true)
13539 bool constructor_p;
13540 cp_token *token;
13541 ds = ds_last;
13543 /* Peek at the next token. */
13544 token = cp_lexer_peek_token (parser->lexer);
13546 /* Save the first token of the decl spec list for error
13547 reporting. */
13548 if (!start_token)
13549 start_token = token;
13550 /* Handle attributes. */
13551 if (cp_next_tokens_can_be_attribute_p (parser))
13553 /* Parse the attributes. */
13554 tree attrs = cp_parser_attributes_opt (parser);
13556 /* In a sequence of declaration specifiers, c++11 attributes
13557 appertain to the type that precede them. In that case
13558 [dcl.spec]/1 says:
13560 The attribute-specifier-seq affects the type only for
13561 the declaration it appears in, not other declarations
13562 involving the same type.
13564 But for now let's force the user to position the
13565 attribute either at the beginning of the declaration or
13566 after the declarator-id, which would clearly mean that it
13567 applies to the declarator. */
13568 if (cxx11_attribute_p (attrs))
13570 if (!found_decl_spec)
13571 /* The c++11 attribute is at the beginning of the
13572 declaration. It appertains to the entity being
13573 declared. */;
13574 else
13576 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13578 /* This is an attribute following a
13579 class-specifier. */
13580 if (decl_specs->type_definition_p)
13581 warn_misplaced_attr_for_class_type (token->location,
13582 decl_specs->type);
13583 attrs = NULL_TREE;
13585 else
13587 decl_specs->std_attributes
13588 = attr_chainon (decl_specs->std_attributes, attrs);
13589 if (decl_specs->locations[ds_std_attribute] == 0)
13590 decl_specs->locations[ds_std_attribute] = token->location;
13592 continue;
13596 decl_specs->attributes
13597 = attr_chainon (decl_specs->attributes, attrs);
13598 if (decl_specs->locations[ds_attribute] == 0)
13599 decl_specs->locations[ds_attribute] = token->location;
13600 continue;
13602 /* Assume we will find a decl-specifier keyword. */
13603 found_decl_spec = true;
13604 /* If the next token is an appropriate keyword, we can simply
13605 add it to the list. */
13606 switch (token->keyword)
13608 /* decl-specifier:
13609 friend
13610 constexpr */
13611 case RID_FRIEND:
13612 if (!at_class_scope_p ())
13614 gcc_rich_location richloc (token->location);
13615 richloc.add_fixit_remove ();
13616 error_at (&richloc, "%<friend%> used outside of class");
13617 cp_lexer_purge_token (parser->lexer);
13619 else
13621 ds = ds_friend;
13622 /* Consume the token. */
13623 cp_lexer_consume_token (parser->lexer);
13625 break;
13627 case RID_CONSTEXPR:
13628 ds = ds_constexpr;
13629 cp_lexer_consume_token (parser->lexer);
13630 break;
13632 case RID_CONCEPT:
13633 ds = ds_concept;
13634 cp_lexer_consume_token (parser->lexer);
13635 break;
13637 /* function-specifier:
13638 inline
13639 virtual
13640 explicit */
13641 case RID_INLINE:
13642 case RID_VIRTUAL:
13643 case RID_EXPLICIT:
13644 cp_parser_function_specifier_opt (parser, decl_specs);
13645 break;
13647 /* decl-specifier:
13648 typedef */
13649 case RID_TYPEDEF:
13650 ds = ds_typedef;
13651 /* Consume the token. */
13652 cp_lexer_consume_token (parser->lexer);
13653 /* A constructor declarator cannot appear in a typedef. */
13654 constructor_possible_p = false;
13655 /* The "typedef" keyword can only occur in a declaration; we
13656 may as well commit at this point. */
13657 cp_parser_commit_to_tentative_parse (parser);
13659 if (decl_specs->storage_class != sc_none)
13660 decl_specs->conflicting_specifiers_p = true;
13661 break;
13663 /* storage-class-specifier:
13664 auto
13665 register
13666 static
13667 extern
13668 mutable
13670 GNU Extension:
13671 thread */
13672 case RID_AUTO:
13673 if (cxx_dialect == cxx98)
13675 /* Consume the token. */
13676 cp_lexer_consume_token (parser->lexer);
13678 /* Complain about `auto' as a storage specifier, if
13679 we're complaining about C++0x compatibility. */
13680 gcc_rich_location richloc (token->location);
13681 richloc.add_fixit_remove ();
13682 warning_at (&richloc, OPT_Wc__11_compat,
13683 "%<auto%> changes meaning in C++11; "
13684 "please remove it");
13686 /* Set the storage class anyway. */
13687 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13688 token);
13690 else
13691 /* C++0x auto type-specifier. */
13692 found_decl_spec = false;
13693 break;
13695 case RID_REGISTER:
13696 case RID_STATIC:
13697 case RID_EXTERN:
13698 case RID_MUTABLE:
13699 /* Consume the token. */
13700 cp_lexer_consume_token (parser->lexer);
13701 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13702 token);
13703 break;
13704 case RID_THREAD:
13705 /* Consume the token. */
13706 ds = ds_thread;
13707 cp_lexer_consume_token (parser->lexer);
13708 break;
13710 default:
13711 /* We did not yet find a decl-specifier yet. */
13712 found_decl_spec = false;
13713 break;
13716 if (found_decl_spec
13717 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13718 && token->keyword != RID_CONSTEXPR)
13719 error ("decl-specifier invalid in condition");
13721 if (found_decl_spec
13722 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13723 && token->keyword != RID_MUTABLE
13724 && token->keyword != RID_CONSTEXPR)
13725 error_at (token->location, "%qD invalid in lambda",
13726 ridpointers[token->keyword]);
13728 if (ds != ds_last)
13729 set_and_check_decl_spec_loc (decl_specs, ds, token);
13731 /* Constructors are a special case. The `S' in `S()' is not a
13732 decl-specifier; it is the beginning of the declarator. */
13733 constructor_p
13734 = (!found_decl_spec
13735 && constructor_possible_p
13736 && (cp_parser_constructor_declarator_p
13737 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13739 /* If we don't have a DECL_SPEC yet, then we must be looking at
13740 a type-specifier. */
13741 if (!found_decl_spec && !constructor_p)
13743 int decl_spec_declares_class_or_enum;
13744 bool is_cv_qualifier;
13745 tree type_spec;
13747 type_spec
13748 = cp_parser_type_specifier (parser, flags,
13749 decl_specs,
13750 /*is_declaration=*/true,
13751 &decl_spec_declares_class_or_enum,
13752 &is_cv_qualifier);
13753 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13755 /* If this type-specifier referenced a user-defined type
13756 (a typedef, class-name, etc.), then we can't allow any
13757 more such type-specifiers henceforth.
13759 [dcl.spec]
13761 The longest sequence of decl-specifiers that could
13762 possibly be a type name is taken as the
13763 decl-specifier-seq of a declaration. The sequence shall
13764 be self-consistent as described below.
13766 [dcl.type]
13768 As a general rule, at most one type-specifier is allowed
13769 in the complete decl-specifier-seq of a declaration. The
13770 only exceptions are the following:
13772 -- const or volatile can be combined with any other
13773 type-specifier.
13775 -- signed or unsigned can be combined with char, long,
13776 short, or int.
13778 -- ..
13780 Example:
13782 typedef char* Pc;
13783 void g (const int Pc);
13785 Here, Pc is *not* part of the decl-specifier seq; it's
13786 the declarator. Therefore, once we see a type-specifier
13787 (other than a cv-qualifier), we forbid any additional
13788 user-defined types. We *do* still allow things like `int
13789 int' to be considered a decl-specifier-seq, and issue the
13790 error message later. */
13791 if (type_spec && !is_cv_qualifier)
13792 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13793 /* A constructor declarator cannot follow a type-specifier. */
13794 if (type_spec)
13796 constructor_possible_p = false;
13797 found_decl_spec = true;
13798 if (!is_cv_qualifier)
13799 decl_specs->any_type_specifiers_p = true;
13801 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13802 error_at (token->location, "type-specifier invalid in lambda");
13806 /* If we still do not have a DECL_SPEC, then there are no more
13807 decl-specifiers. */
13808 if (!found_decl_spec)
13809 break;
13811 decl_specs->any_specifiers_p = true;
13812 /* After we see one decl-specifier, further decl-specifiers are
13813 always optional. */
13814 flags |= CP_PARSER_FLAGS_OPTIONAL;
13817 /* Don't allow a friend specifier with a class definition. */
13818 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13819 && (*declares_class_or_enum & 2))
13820 error_at (decl_specs->locations[ds_friend],
13821 "class definition may not be declared a friend");
13824 /* Parse an (optional) storage-class-specifier.
13826 storage-class-specifier:
13827 auto
13828 register
13829 static
13830 extern
13831 mutable
13833 GNU Extension:
13835 storage-class-specifier:
13836 thread
13838 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13840 static tree
13841 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13843 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13845 case RID_AUTO:
13846 if (cxx_dialect != cxx98)
13847 return NULL_TREE;
13848 /* Fall through for C++98. */
13849 gcc_fallthrough ();
13851 case RID_REGISTER:
13852 case RID_STATIC:
13853 case RID_EXTERN:
13854 case RID_MUTABLE:
13855 case RID_THREAD:
13856 /* Consume the token. */
13857 return cp_lexer_consume_token (parser->lexer)->u.value;
13859 default:
13860 return NULL_TREE;
13864 /* Parse an (optional) function-specifier.
13866 function-specifier:
13867 inline
13868 virtual
13869 explicit
13871 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13872 Updates DECL_SPECS, if it is non-NULL. */
13874 static tree
13875 cp_parser_function_specifier_opt (cp_parser* parser,
13876 cp_decl_specifier_seq *decl_specs)
13878 cp_token *token = cp_lexer_peek_token (parser->lexer);
13879 switch (token->keyword)
13881 case RID_INLINE:
13882 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13883 break;
13885 case RID_VIRTUAL:
13886 /* 14.5.2.3 [temp.mem]
13888 A member function template shall not be virtual. */
13889 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13890 && current_class_type)
13891 error_at (token->location, "templates may not be %<virtual%>");
13892 else
13893 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13894 break;
13896 case RID_EXPLICIT:
13897 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13898 break;
13900 default:
13901 return NULL_TREE;
13904 /* Consume the token. */
13905 return cp_lexer_consume_token (parser->lexer)->u.value;
13908 /* Parse a linkage-specification.
13910 linkage-specification:
13911 extern string-literal { declaration-seq [opt] }
13912 extern string-literal declaration */
13914 static void
13915 cp_parser_linkage_specification (cp_parser* parser)
13917 tree linkage;
13919 /* Look for the `extern' keyword. */
13920 cp_token *extern_token
13921 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13923 /* Look for the string-literal. */
13924 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13925 linkage = cp_parser_string_literal (parser, false, false);
13927 /* Transform the literal into an identifier. If the literal is a
13928 wide-character string, or contains embedded NULs, then we can't
13929 handle it as the user wants. */
13930 if (strlen (TREE_STRING_POINTER (linkage))
13931 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13933 cp_parser_error (parser, "invalid linkage-specification");
13934 /* Assume C++ linkage. */
13935 linkage = lang_name_cplusplus;
13937 else
13938 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13940 /* We're now using the new linkage. */
13941 push_lang_context (linkage);
13943 /* Preserve the location of the the innermost linkage specification,
13944 tracking the locations of nested specifications via a local. */
13945 location_t saved_location
13946 = parser->innermost_linkage_specification_location;
13947 /* Construct a location ranging from the start of the "extern" to
13948 the end of the string-literal, with the caret at the start, e.g.:
13949 extern "C" {
13950 ^~~~~~~~~~
13952 parser->innermost_linkage_specification_location
13953 = make_location (extern_token->location,
13954 extern_token->location,
13955 get_finish (string_token->location));
13957 /* If the next token is a `{', then we're using the first
13958 production. */
13959 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13961 cp_ensure_no_omp_declare_simd (parser);
13962 cp_ensure_no_oacc_routine (parser);
13964 /* Consume the `{' token. */
13965 matching_braces braces;
13966 braces.consume_open (parser)->location;
13967 /* Parse the declarations. */
13968 cp_parser_declaration_seq_opt (parser);
13969 /* Look for the closing `}'. */
13970 braces.require_close (parser);
13972 /* Otherwise, there's just one declaration. */
13973 else
13975 bool saved_in_unbraced_linkage_specification_p;
13977 saved_in_unbraced_linkage_specification_p
13978 = parser->in_unbraced_linkage_specification_p;
13979 parser->in_unbraced_linkage_specification_p = true;
13980 cp_parser_declaration (parser);
13981 parser->in_unbraced_linkage_specification_p
13982 = saved_in_unbraced_linkage_specification_p;
13985 /* We're done with the linkage-specification. */
13986 pop_lang_context ();
13988 /* Restore location of parent linkage specification, if any. */
13989 parser->innermost_linkage_specification_location = saved_location;
13992 /* Parse a static_assert-declaration.
13994 static_assert-declaration:
13995 static_assert ( constant-expression , string-literal ) ;
13996 static_assert ( constant-expression ) ; (C++17)
13998 If MEMBER_P, this static_assert is a class member. */
14000 static void
14001 cp_parser_static_assert(cp_parser *parser, bool member_p)
14003 cp_expr condition;
14004 location_t token_loc;
14005 tree message;
14006 bool dummy;
14008 /* Peek at the `static_assert' token so we can keep track of exactly
14009 where the static assertion started. */
14010 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14012 /* Look for the `static_assert' keyword. */
14013 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14014 RT_STATIC_ASSERT))
14015 return;
14017 /* We know we are in a static assertion; commit to any tentative
14018 parse. */
14019 if (cp_parser_parsing_tentatively (parser))
14020 cp_parser_commit_to_tentative_parse (parser);
14022 /* Parse the `(' starting the static assertion condition. */
14023 matching_parens parens;
14024 parens.require_open (parser);
14026 /* Parse the constant-expression. Allow a non-constant expression
14027 here in order to give better diagnostics in finish_static_assert. */
14028 condition =
14029 cp_parser_constant_expression (parser,
14030 /*allow_non_constant_p=*/true,
14031 /*non_constant_p=*/&dummy);
14033 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14035 if (cxx_dialect < cxx17)
14036 pedwarn (input_location, OPT_Wpedantic,
14037 "static_assert without a message "
14038 "only available with -std=c++17 or -std=gnu++17");
14039 /* Eat the ')' */
14040 cp_lexer_consume_token (parser->lexer);
14041 message = build_string (1, "");
14042 TREE_TYPE (message) = char_array_type_node;
14043 fix_string_type (message);
14045 else
14047 /* Parse the separating `,'. */
14048 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14050 /* Parse the string-literal message. */
14051 message = cp_parser_string_literal (parser,
14052 /*translate=*/false,
14053 /*wide_ok=*/true);
14055 /* A `)' completes the static assertion. */
14056 if (!parens.require_close (parser))
14057 cp_parser_skip_to_closing_parenthesis (parser,
14058 /*recovering=*/true,
14059 /*or_comma=*/false,
14060 /*consume_paren=*/true);
14063 /* A semicolon terminates the declaration. */
14064 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14066 /* Get the location for the static assertion. Use that of the
14067 condition if available, otherwise, use that of the "static_assert"
14068 token. */
14069 location_t assert_loc = condition.get_location ();
14070 if (assert_loc == UNKNOWN_LOCATION)
14071 assert_loc = token_loc;
14073 /* Complete the static assertion, which may mean either processing
14074 the static assert now or saving it for template instantiation. */
14075 finish_static_assert (condition, message, assert_loc, member_p);
14078 /* Parse the expression in decltype ( expression ). */
14080 static tree
14081 cp_parser_decltype_expr (cp_parser *parser,
14082 bool &id_expression_or_member_access_p)
14084 cp_token *id_expr_start_token;
14085 tree expr;
14087 /* Since we're going to preserve any side-effects from this parse, set up a
14088 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14089 in the expression. */
14090 tentative_firewall firewall (parser);
14092 /* First, try parsing an id-expression. */
14093 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14094 cp_parser_parse_tentatively (parser);
14095 expr = cp_parser_id_expression (parser,
14096 /*template_keyword_p=*/false,
14097 /*check_dependency_p=*/true,
14098 /*template_p=*/NULL,
14099 /*declarator_p=*/false,
14100 /*optional_p=*/false);
14102 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14104 bool non_integral_constant_expression_p = false;
14105 tree id_expression = expr;
14106 cp_id_kind idk;
14107 const char *error_msg;
14109 if (identifier_p (expr))
14110 /* Lookup the name we got back from the id-expression. */
14111 expr = cp_parser_lookup_name_simple (parser, expr,
14112 id_expr_start_token->location);
14114 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14115 /* A template without args is not a complete id-expression. */
14116 expr = error_mark_node;
14118 if (expr
14119 && expr != error_mark_node
14120 && TREE_CODE (expr) != TYPE_DECL
14121 && (TREE_CODE (expr) != BIT_NOT_EXPR
14122 || !TYPE_P (TREE_OPERAND (expr, 0)))
14123 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14125 /* Complete lookup of the id-expression. */
14126 expr = (finish_id_expression
14127 (id_expression, expr, parser->scope, &idk,
14128 /*integral_constant_expression_p=*/false,
14129 /*allow_non_integral_constant_expression_p=*/true,
14130 &non_integral_constant_expression_p,
14131 /*template_p=*/false,
14132 /*done=*/true,
14133 /*address_p=*/false,
14134 /*template_arg_p=*/false,
14135 &error_msg,
14136 id_expr_start_token->location));
14138 if (expr == error_mark_node)
14139 /* We found an id-expression, but it was something that we
14140 should not have found. This is an error, not something
14141 we can recover from, so note that we found an
14142 id-expression and we'll recover as gracefully as
14143 possible. */
14144 id_expression_or_member_access_p = true;
14147 if (expr
14148 && expr != error_mark_node
14149 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14150 /* We have an id-expression. */
14151 id_expression_or_member_access_p = true;
14154 if (!id_expression_or_member_access_p)
14156 /* Abort the id-expression parse. */
14157 cp_parser_abort_tentative_parse (parser);
14159 /* Parsing tentatively, again. */
14160 cp_parser_parse_tentatively (parser);
14162 /* Parse a class member access. */
14163 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14164 /*cast_p=*/false, /*decltype*/true,
14165 /*member_access_only_p=*/true, NULL);
14167 if (expr
14168 && expr != error_mark_node
14169 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14170 /* We have an id-expression. */
14171 id_expression_or_member_access_p = true;
14174 if (id_expression_or_member_access_p)
14175 /* We have parsed the complete id-expression or member access. */
14176 cp_parser_parse_definitely (parser);
14177 else
14179 /* Abort our attempt to parse an id-expression or member access
14180 expression. */
14181 cp_parser_abort_tentative_parse (parser);
14183 /* Commit to the tentative_firewall so we get syntax errors. */
14184 cp_parser_commit_to_tentative_parse (parser);
14186 /* Parse a full expression. */
14187 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14188 /*decltype_p=*/true);
14191 return expr;
14194 /* Parse a `decltype' type. Returns the type.
14196 simple-type-specifier:
14197 decltype ( expression )
14198 C++14 proposal:
14199 decltype ( auto ) */
14201 static tree
14202 cp_parser_decltype (cp_parser *parser)
14204 bool id_expression_or_member_access_p = false;
14205 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14207 if (start_token->type == CPP_DECLTYPE)
14209 /* Already parsed. */
14210 cp_lexer_consume_token (parser->lexer);
14211 return saved_checks_value (start_token->u.tree_check_value);
14214 /* Look for the `decltype' token. */
14215 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14216 return error_mark_node;
14218 /* Parse the opening `('. */
14219 matching_parens parens;
14220 if (!parens.require_open (parser))
14221 return error_mark_node;
14223 push_deferring_access_checks (dk_deferred);
14225 tree expr = NULL_TREE;
14227 if (cxx_dialect >= cxx14
14228 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14229 /* decltype (auto) */
14230 cp_lexer_consume_token (parser->lexer);
14231 else
14233 /* decltype (expression) */
14235 /* Types cannot be defined in a `decltype' expression. Save away the
14236 old message and set the new one. */
14237 const char *saved_message = parser->type_definition_forbidden_message;
14238 parser->type_definition_forbidden_message
14239 = G_("types may not be defined in %<decltype%> expressions");
14241 /* The restrictions on constant-expressions do not apply inside
14242 decltype expressions. */
14243 bool saved_integral_constant_expression_p
14244 = parser->integral_constant_expression_p;
14245 bool saved_non_integral_constant_expression_p
14246 = parser->non_integral_constant_expression_p;
14247 parser->integral_constant_expression_p = false;
14249 /* Within a parenthesized expression, a `>' token is always
14250 the greater-than operator. */
14251 bool saved_greater_than_is_operator_p
14252 = parser->greater_than_is_operator_p;
14253 parser->greater_than_is_operator_p = true;
14255 /* Do not actually evaluate the expression. */
14256 ++cp_unevaluated_operand;
14258 /* Do not warn about problems with the expression. */
14259 ++c_inhibit_evaluation_warnings;
14261 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14263 /* Go back to evaluating expressions. */
14264 --cp_unevaluated_operand;
14265 --c_inhibit_evaluation_warnings;
14267 /* The `>' token might be the end of a template-id or
14268 template-parameter-list now. */
14269 parser->greater_than_is_operator_p
14270 = saved_greater_than_is_operator_p;
14272 /* Restore the old message and the integral constant expression
14273 flags. */
14274 parser->type_definition_forbidden_message = saved_message;
14275 parser->integral_constant_expression_p
14276 = saved_integral_constant_expression_p;
14277 parser->non_integral_constant_expression_p
14278 = saved_non_integral_constant_expression_p;
14281 /* Parse to the closing `)'. */
14282 if (!parens.require_close (parser))
14284 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14285 /*consume_paren=*/true);
14286 pop_deferring_access_checks ();
14287 return error_mark_node;
14290 if (!expr)
14292 /* Build auto. */
14293 expr = make_decltype_auto ();
14294 AUTO_IS_DECLTYPE (expr) = true;
14296 else
14297 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14298 tf_warning_or_error);
14300 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14301 it again. */
14302 start_token->type = CPP_DECLTYPE;
14303 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14304 start_token->u.tree_check_value->value = expr;
14305 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14306 start_token->keyword = RID_MAX;
14307 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14309 pop_to_parent_deferring_access_checks ();
14311 return expr;
14314 /* Special member functions [gram.special] */
14316 /* Parse a conversion-function-id.
14318 conversion-function-id:
14319 operator conversion-type-id
14321 Returns an IDENTIFIER_NODE representing the operator. */
14323 static tree
14324 cp_parser_conversion_function_id (cp_parser* parser)
14326 tree type;
14327 tree saved_scope;
14328 tree saved_qualifying_scope;
14329 tree saved_object_scope;
14330 tree pushed_scope = NULL_TREE;
14332 /* Look for the `operator' token. */
14333 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14334 return error_mark_node;
14335 /* When we parse the conversion-type-id, the current scope will be
14336 reset. However, we need that information in able to look up the
14337 conversion function later, so we save it here. */
14338 saved_scope = parser->scope;
14339 saved_qualifying_scope = parser->qualifying_scope;
14340 saved_object_scope = parser->object_scope;
14341 /* We must enter the scope of the class so that the names of
14342 entities declared within the class are available in the
14343 conversion-type-id. For example, consider:
14345 struct S {
14346 typedef int I;
14347 operator I();
14350 S::operator I() { ... }
14352 In order to see that `I' is a type-name in the definition, we
14353 must be in the scope of `S'. */
14354 if (saved_scope)
14355 pushed_scope = push_scope (saved_scope);
14356 /* Parse the conversion-type-id. */
14357 type = cp_parser_conversion_type_id (parser);
14358 /* Leave the scope of the class, if any. */
14359 if (pushed_scope)
14360 pop_scope (pushed_scope);
14361 /* Restore the saved scope. */
14362 parser->scope = saved_scope;
14363 parser->qualifying_scope = saved_qualifying_scope;
14364 parser->object_scope = saved_object_scope;
14365 /* If the TYPE is invalid, indicate failure. */
14366 if (type == error_mark_node)
14367 return error_mark_node;
14368 return make_conv_op_name (type);
14371 /* Parse a conversion-type-id:
14373 conversion-type-id:
14374 type-specifier-seq conversion-declarator [opt]
14376 Returns the TYPE specified. */
14378 static tree
14379 cp_parser_conversion_type_id (cp_parser* parser)
14381 tree attributes;
14382 cp_decl_specifier_seq type_specifiers;
14383 cp_declarator *declarator;
14384 tree type_specified;
14385 const char *saved_message;
14387 /* Parse the attributes. */
14388 attributes = cp_parser_attributes_opt (parser);
14390 saved_message = parser->type_definition_forbidden_message;
14391 parser->type_definition_forbidden_message
14392 = G_("types may not be defined in a conversion-type-id");
14394 /* Parse the type-specifiers. */
14395 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14396 /*is_trailing_return=*/false,
14397 &type_specifiers);
14399 parser->type_definition_forbidden_message = saved_message;
14401 /* If that didn't work, stop. */
14402 if (type_specifiers.type == error_mark_node)
14403 return error_mark_node;
14404 /* Parse the conversion-declarator. */
14405 declarator = cp_parser_conversion_declarator_opt (parser);
14407 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14408 /*initialized=*/0, &attributes);
14409 if (attributes)
14410 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14412 /* Don't give this error when parsing tentatively. This happens to
14413 work because we always parse this definitively once. */
14414 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14415 && type_uses_auto (type_specified))
14417 if (cxx_dialect < cxx14)
14419 error ("invalid use of %<auto%> in conversion operator");
14420 return error_mark_node;
14422 else if (template_parm_scope_p ())
14423 warning (0, "use of %<auto%> in member template "
14424 "conversion operator can never be deduced");
14427 return type_specified;
14430 /* Parse an (optional) conversion-declarator.
14432 conversion-declarator:
14433 ptr-operator conversion-declarator [opt]
14437 static cp_declarator *
14438 cp_parser_conversion_declarator_opt (cp_parser* parser)
14440 enum tree_code code;
14441 tree class_type, std_attributes = NULL_TREE;
14442 cp_cv_quals cv_quals;
14444 /* We don't know if there's a ptr-operator next, or not. */
14445 cp_parser_parse_tentatively (parser);
14446 /* Try the ptr-operator. */
14447 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14448 &std_attributes);
14449 /* If it worked, look for more conversion-declarators. */
14450 if (cp_parser_parse_definitely (parser))
14452 cp_declarator *declarator;
14454 /* Parse another optional declarator. */
14455 declarator = cp_parser_conversion_declarator_opt (parser);
14457 declarator = cp_parser_make_indirect_declarator
14458 (code, class_type, cv_quals, declarator, std_attributes);
14460 return declarator;
14463 return NULL;
14466 /* Parse an (optional) ctor-initializer.
14468 ctor-initializer:
14469 : mem-initializer-list */
14471 static void
14472 cp_parser_ctor_initializer_opt (cp_parser* parser)
14474 /* If the next token is not a `:', then there is no
14475 ctor-initializer. */
14476 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14478 /* Do default initialization of any bases and members. */
14479 if (DECL_CONSTRUCTOR_P (current_function_decl))
14480 finish_mem_initializers (NULL_TREE);
14481 return;
14484 /* Consume the `:' token. */
14485 cp_lexer_consume_token (parser->lexer);
14486 /* And the mem-initializer-list. */
14487 cp_parser_mem_initializer_list (parser);
14490 /* Parse a mem-initializer-list.
14492 mem-initializer-list:
14493 mem-initializer ... [opt]
14494 mem-initializer ... [opt] , mem-initializer-list */
14496 static void
14497 cp_parser_mem_initializer_list (cp_parser* parser)
14499 tree mem_initializer_list = NULL_TREE;
14500 tree target_ctor = error_mark_node;
14501 cp_token *token = cp_lexer_peek_token (parser->lexer);
14503 /* Let the semantic analysis code know that we are starting the
14504 mem-initializer-list. */
14505 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14506 error_at (token->location,
14507 "only constructors take member initializers");
14509 /* Loop through the list. */
14510 while (true)
14512 tree mem_initializer;
14514 token = cp_lexer_peek_token (parser->lexer);
14515 /* Parse the mem-initializer. */
14516 mem_initializer = cp_parser_mem_initializer (parser);
14517 /* If the next token is a `...', we're expanding member initializers. */
14518 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14519 if (ellipsis
14520 || (mem_initializer != error_mark_node
14521 && check_for_bare_parameter_packs (TREE_PURPOSE
14522 (mem_initializer))))
14524 /* Consume the `...'. */
14525 if (ellipsis)
14526 cp_lexer_consume_token (parser->lexer);
14528 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14529 can be expanded but members cannot. */
14530 if (mem_initializer != error_mark_node
14531 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14533 error_at (token->location,
14534 "cannot expand initializer for member %qD",
14535 TREE_PURPOSE (mem_initializer));
14536 mem_initializer = error_mark_node;
14539 /* Construct the pack expansion type. */
14540 if (mem_initializer != error_mark_node)
14541 mem_initializer = make_pack_expansion (mem_initializer);
14543 if (target_ctor != error_mark_node
14544 && mem_initializer != error_mark_node)
14546 error ("mem-initializer for %qD follows constructor delegation",
14547 TREE_PURPOSE (mem_initializer));
14548 mem_initializer = error_mark_node;
14550 /* Look for a target constructor. */
14551 if (mem_initializer != error_mark_node
14552 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14553 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14555 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14556 if (mem_initializer_list)
14558 error ("constructor delegation follows mem-initializer for %qD",
14559 TREE_PURPOSE (mem_initializer_list));
14560 mem_initializer = error_mark_node;
14562 target_ctor = mem_initializer;
14564 /* Add it to the list, unless it was erroneous. */
14565 if (mem_initializer != error_mark_node)
14567 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14568 mem_initializer_list = mem_initializer;
14570 /* If the next token is not a `,', we're done. */
14571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14572 break;
14573 /* Consume the `,' token. */
14574 cp_lexer_consume_token (parser->lexer);
14577 /* Perform semantic analysis. */
14578 if (DECL_CONSTRUCTOR_P (current_function_decl))
14579 finish_mem_initializers (mem_initializer_list);
14582 /* Parse a mem-initializer.
14584 mem-initializer:
14585 mem-initializer-id ( expression-list [opt] )
14586 mem-initializer-id braced-init-list
14588 GNU extension:
14590 mem-initializer:
14591 ( expression-list [opt] )
14593 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14594 class) or FIELD_DECL (for a non-static data member) to initialize;
14595 the TREE_VALUE is the expression-list. An empty initialization
14596 list is represented by void_list_node. */
14598 static tree
14599 cp_parser_mem_initializer (cp_parser* parser)
14601 tree mem_initializer_id;
14602 tree expression_list;
14603 tree member;
14604 cp_token *token = cp_lexer_peek_token (parser->lexer);
14606 /* Find out what is being initialized. */
14607 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14609 permerror (token->location,
14610 "anachronistic old-style base class initializer");
14611 mem_initializer_id = NULL_TREE;
14613 else
14615 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14616 if (mem_initializer_id == error_mark_node)
14617 return mem_initializer_id;
14619 member = expand_member_init (mem_initializer_id);
14620 if (member && !DECL_P (member))
14621 in_base_initializer = 1;
14623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14625 bool expr_non_constant_p;
14626 cp_lexer_set_source_position (parser->lexer);
14627 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14628 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14629 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14630 expression_list = build_tree_list (NULL_TREE, expression_list);
14632 else
14634 vec<tree, va_gc> *vec;
14635 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14636 /*cast_p=*/false,
14637 /*allow_expansion_p=*/true,
14638 /*non_constant_p=*/NULL);
14639 if (vec == NULL)
14640 return error_mark_node;
14641 expression_list = build_tree_list_vec (vec);
14642 release_tree_vector (vec);
14645 if (expression_list == error_mark_node)
14646 return error_mark_node;
14647 if (!expression_list)
14648 expression_list = void_type_node;
14650 in_base_initializer = 0;
14652 return member ? build_tree_list (member, expression_list) : error_mark_node;
14655 /* Parse a mem-initializer-id.
14657 mem-initializer-id:
14658 :: [opt] nested-name-specifier [opt] class-name
14659 decltype-specifier (C++11)
14660 identifier
14662 Returns a TYPE indicating the class to be initialized for the first
14663 production (and the second in C++11). Returns an IDENTIFIER_NODE
14664 indicating the data member to be initialized for the last production. */
14666 static tree
14667 cp_parser_mem_initializer_id (cp_parser* parser)
14669 bool global_scope_p;
14670 bool nested_name_specifier_p;
14671 bool template_p = false;
14672 tree id;
14674 cp_token *token = cp_lexer_peek_token (parser->lexer);
14676 /* `typename' is not allowed in this context ([temp.res]). */
14677 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14679 error_at (token->location,
14680 "keyword %<typename%> not allowed in this context (a qualified "
14681 "member initializer is implicitly a type)");
14682 cp_lexer_consume_token (parser->lexer);
14684 /* Look for the optional `::' operator. */
14685 global_scope_p
14686 = (cp_parser_global_scope_opt (parser,
14687 /*current_scope_valid_p=*/false)
14688 != NULL_TREE);
14689 /* Look for the optional nested-name-specifier. The simplest way to
14690 implement:
14692 [temp.res]
14694 The keyword `typename' is not permitted in a base-specifier or
14695 mem-initializer; in these contexts a qualified name that
14696 depends on a template-parameter is implicitly assumed to be a
14697 type name.
14699 is to assume that we have seen the `typename' keyword at this
14700 point. */
14701 nested_name_specifier_p
14702 = (cp_parser_nested_name_specifier_opt (parser,
14703 /*typename_keyword_p=*/true,
14704 /*check_dependency_p=*/true,
14705 /*type_p=*/true,
14706 /*is_declaration=*/true)
14707 != NULL_TREE);
14708 if (nested_name_specifier_p)
14709 template_p = cp_parser_optional_template_keyword (parser);
14710 /* If there is a `::' operator or a nested-name-specifier, then we
14711 are definitely looking for a class-name. */
14712 if (global_scope_p || nested_name_specifier_p)
14713 return cp_parser_class_name (parser,
14714 /*typename_keyword_p=*/true,
14715 /*template_keyword_p=*/template_p,
14716 typename_type,
14717 /*check_dependency_p=*/true,
14718 /*class_head_p=*/false,
14719 /*is_declaration=*/true);
14720 /* Otherwise, we could also be looking for an ordinary identifier. */
14721 cp_parser_parse_tentatively (parser);
14722 if (cp_lexer_next_token_is_decltype (parser->lexer))
14723 /* Try a decltype-specifier. */
14724 id = cp_parser_decltype (parser);
14725 else
14726 /* Otherwise, try a class-name. */
14727 id = cp_parser_class_name (parser,
14728 /*typename_keyword_p=*/true,
14729 /*template_keyword_p=*/false,
14730 none_type,
14731 /*check_dependency_p=*/true,
14732 /*class_head_p=*/false,
14733 /*is_declaration=*/true);
14734 /* If we found one, we're done. */
14735 if (cp_parser_parse_definitely (parser))
14736 return id;
14737 /* Otherwise, look for an ordinary identifier. */
14738 return cp_parser_identifier (parser);
14741 /* Overloading [gram.over] */
14743 /* Parse an operator-function-id.
14745 operator-function-id:
14746 operator operator
14748 Returns an IDENTIFIER_NODE for the operator which is a
14749 human-readable spelling of the identifier, e.g., `operator +'. */
14751 static cp_expr
14752 cp_parser_operator_function_id (cp_parser* parser)
14754 /* Look for the `operator' keyword. */
14755 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14756 return error_mark_node;
14757 /* And then the name of the operator itself. */
14758 return cp_parser_operator (parser);
14761 /* Return an identifier node for a user-defined literal operator.
14762 The suffix identifier is chained to the operator name identifier. */
14764 tree
14765 cp_literal_operator_id (const char* name)
14767 tree identifier;
14768 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14769 + strlen (name) + 10);
14770 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14771 identifier = get_identifier (buffer);
14773 return identifier;
14776 /* Parse an operator.
14778 operator:
14779 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14780 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14781 || ++ -- , ->* -> () []
14783 GNU Extensions:
14785 operator:
14786 <? >? <?= >?=
14788 Returns an IDENTIFIER_NODE for the operator which is a
14789 human-readable spelling of the identifier, e.g., `operator +'. */
14791 static cp_expr
14792 cp_parser_operator (cp_parser* parser)
14794 tree id = NULL_TREE;
14795 cp_token *token;
14796 bool utf8 = false;
14798 /* Peek at the next token. */
14799 token = cp_lexer_peek_token (parser->lexer);
14801 location_t start_loc = token->location;
14803 /* Figure out which operator we have. */
14804 enum tree_code op = ERROR_MARK;
14805 bool assop = false;
14806 bool consumed = false;
14807 switch (token->type)
14809 case CPP_KEYWORD:
14811 /* The keyword should be either `new' or `delete'. */
14812 if (token->keyword == RID_NEW)
14813 op = NEW_EXPR;
14814 else if (token->keyword == RID_DELETE)
14815 op = DELETE_EXPR;
14816 else
14817 break;
14819 /* Consume the `new' or `delete' token. */
14820 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14822 /* Peek at the next token. */
14823 token = cp_lexer_peek_token (parser->lexer);
14824 /* If it's a `[' token then this is the array variant of the
14825 operator. */
14826 if (token->type == CPP_OPEN_SQUARE)
14828 /* Consume the `[' token. */
14829 cp_lexer_consume_token (parser->lexer);
14830 /* Look for the `]' token. */
14831 if (cp_token *close_token
14832 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14833 end_loc = close_token->location;
14834 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14836 start_loc = make_location (start_loc, start_loc, end_loc);
14837 consumed = true;
14838 break;
14841 case CPP_PLUS:
14842 op = PLUS_EXPR;
14843 break;
14845 case CPP_MINUS:
14846 op = MINUS_EXPR;
14847 break;
14849 case CPP_MULT:
14850 op = MULT_EXPR;
14851 break;
14853 case CPP_DIV:
14854 op = TRUNC_DIV_EXPR;
14855 break;
14857 case CPP_MOD:
14858 op = TRUNC_MOD_EXPR;
14859 break;
14861 case CPP_XOR:
14862 op = BIT_XOR_EXPR;
14863 break;
14865 case CPP_AND:
14866 op = BIT_AND_EXPR;
14867 break;
14869 case CPP_OR:
14870 op = BIT_IOR_EXPR;
14871 break;
14873 case CPP_COMPL:
14874 op = BIT_NOT_EXPR;
14875 break;
14877 case CPP_NOT:
14878 op = TRUTH_NOT_EXPR;
14879 break;
14881 case CPP_EQ:
14882 assop = true;
14883 op = NOP_EXPR;
14884 break;
14886 case CPP_LESS:
14887 op = LT_EXPR;
14888 break;
14890 case CPP_GREATER:
14891 op = GT_EXPR;
14892 break;
14894 case CPP_PLUS_EQ:
14895 assop = true;
14896 op = PLUS_EXPR;
14897 break;
14899 case CPP_MINUS_EQ:
14900 assop = true;
14901 op = MINUS_EXPR;
14902 break;
14904 case CPP_MULT_EQ:
14905 assop = true;
14906 op = MULT_EXPR;
14907 break;
14909 case CPP_DIV_EQ:
14910 assop = true;
14911 op = TRUNC_DIV_EXPR;
14912 break;
14914 case CPP_MOD_EQ:
14915 assop = true;
14916 op = TRUNC_MOD_EXPR;
14917 break;
14919 case CPP_XOR_EQ:
14920 assop = true;
14921 op = BIT_XOR_EXPR;
14922 break;
14924 case CPP_AND_EQ:
14925 assop = true;
14926 op = BIT_AND_EXPR;
14927 break;
14929 case CPP_OR_EQ:
14930 assop = true;
14931 op = BIT_IOR_EXPR;
14932 break;
14934 case CPP_LSHIFT:
14935 op = LSHIFT_EXPR;
14936 break;
14938 case CPP_RSHIFT:
14939 op = RSHIFT_EXPR;
14940 break;
14942 case CPP_LSHIFT_EQ:
14943 assop = true;
14944 op = LSHIFT_EXPR;
14945 break;
14947 case CPP_RSHIFT_EQ:
14948 assop = true;
14949 op = RSHIFT_EXPR;
14950 break;
14952 case CPP_EQ_EQ:
14953 op = EQ_EXPR;
14954 break;
14956 case CPP_NOT_EQ:
14957 op = NE_EXPR;
14958 break;
14960 case CPP_LESS_EQ:
14961 op = LE_EXPR;
14962 break;
14964 case CPP_GREATER_EQ:
14965 op = GE_EXPR;
14966 break;
14968 case CPP_AND_AND:
14969 op = TRUTH_ANDIF_EXPR;
14970 break;
14972 case CPP_OR_OR:
14973 op = TRUTH_ORIF_EXPR;
14974 break;
14976 case CPP_PLUS_PLUS:
14977 op = POSTINCREMENT_EXPR;
14978 break;
14980 case CPP_MINUS_MINUS:
14981 op = PREDECREMENT_EXPR;
14982 break;
14984 case CPP_COMMA:
14985 op = COMPOUND_EXPR;
14986 break;
14988 case CPP_DEREF_STAR:
14989 op = MEMBER_REF;
14990 break;
14992 case CPP_DEREF:
14993 op = COMPONENT_REF;
14994 break;
14996 case CPP_OPEN_PAREN:
14998 /* Consume the `('. */
14999 matching_parens parens;
15000 parens.consume_open (parser);
15001 /* Look for the matching `)'. */
15002 parens.require_close (parser);
15003 op = CALL_EXPR;
15004 consumed = true;
15005 break;
15008 case CPP_OPEN_SQUARE:
15009 /* Consume the `['. */
15010 cp_lexer_consume_token (parser->lexer);
15011 /* Look for the matching `]'. */
15012 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15013 op = ARRAY_REF;
15014 consumed = true;
15015 break;
15017 case CPP_UTF8STRING:
15018 case CPP_UTF8STRING_USERDEF:
15019 utf8 = true;
15020 /* FALLTHRU */
15021 case CPP_STRING:
15022 case CPP_WSTRING:
15023 case CPP_STRING16:
15024 case CPP_STRING32:
15025 case CPP_STRING_USERDEF:
15026 case CPP_WSTRING_USERDEF:
15027 case CPP_STRING16_USERDEF:
15028 case CPP_STRING32_USERDEF:
15030 tree str, string_tree;
15031 int sz, len;
15033 if (cxx_dialect == cxx98)
15034 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15036 /* Consume the string. */
15037 str = cp_parser_string_literal (parser, /*translate=*/true,
15038 /*wide_ok=*/true, /*lookup_udlit=*/false);
15039 if (str == error_mark_node)
15040 return error_mark_node;
15041 else if (TREE_CODE (str) == USERDEF_LITERAL)
15043 string_tree = USERDEF_LITERAL_VALUE (str);
15044 id = USERDEF_LITERAL_SUFFIX_ID (str);
15046 else
15048 string_tree = str;
15049 /* Look for the suffix identifier. */
15050 token = cp_lexer_peek_token (parser->lexer);
15051 if (token->type == CPP_NAME)
15052 id = cp_parser_identifier (parser);
15053 else if (token->type == CPP_KEYWORD)
15055 error ("unexpected keyword;"
15056 " remove space between quotes and suffix identifier");
15057 return error_mark_node;
15059 else
15061 error ("expected suffix identifier");
15062 return error_mark_node;
15065 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15066 (TREE_TYPE (TREE_TYPE (string_tree))));
15067 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15068 if (len != 0)
15070 error ("expected empty string after %<operator%> keyword");
15071 return error_mark_node;
15073 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15074 != char_type_node)
15076 error ("invalid encoding prefix in literal operator");
15077 return error_mark_node;
15079 if (id != error_mark_node)
15081 const char *name = IDENTIFIER_POINTER (id);
15082 id = cp_literal_operator_id (name);
15084 return id;
15087 default:
15088 /* Anything else is an error. */
15089 break;
15092 /* If we have selected an identifier, we need to consume the
15093 operator token. */
15094 if (op != ERROR_MARK)
15096 id = ovl_op_identifier (assop, op);
15097 if (!consumed)
15098 cp_lexer_consume_token (parser->lexer);
15100 /* Otherwise, no valid operator name was present. */
15101 else
15103 cp_parser_error (parser, "expected operator");
15104 id = error_mark_node;
15107 return cp_expr (id, start_loc);
15110 /* Parse a template-declaration.
15112 template-declaration:
15113 export [opt] template < template-parameter-list > declaration
15115 If MEMBER_P is TRUE, this template-declaration occurs within a
15116 class-specifier.
15118 The grammar rule given by the standard isn't correct. What
15119 is really meant is:
15121 template-declaration:
15122 export [opt] template-parameter-list-seq
15123 decl-specifier-seq [opt] init-declarator [opt] ;
15124 export [opt] template-parameter-list-seq
15125 function-definition
15127 template-parameter-list-seq:
15128 template-parameter-list-seq [opt]
15129 template < template-parameter-list >
15131 Concept Extensions:
15133 template-parameter-list-seq:
15134 template < template-parameter-list > requires-clause [opt]
15136 requires-clause:
15137 requires logical-or-expression */
15139 static void
15140 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15142 /* Check for `export'. */
15143 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15145 /* Consume the `export' token. */
15146 cp_lexer_consume_token (parser->lexer);
15147 /* Warn that we do not support `export'. */
15148 warning (0, "keyword %<export%> not implemented, and will be ignored");
15151 cp_parser_template_declaration_after_export (parser, member_p);
15154 /* Parse a template-parameter-list.
15156 template-parameter-list:
15157 template-parameter
15158 template-parameter-list , template-parameter
15160 Returns a TREE_LIST. Each node represents a template parameter.
15161 The nodes are connected via their TREE_CHAINs. */
15163 static tree
15164 cp_parser_template_parameter_list (cp_parser* parser)
15166 tree parameter_list = NULL_TREE;
15168 begin_template_parm_list ();
15170 /* The loop below parses the template parms. We first need to know
15171 the total number of template parms to be able to compute proper
15172 canonical types of each dependent type. So after the loop, when
15173 we know the total number of template parms,
15174 end_template_parm_list computes the proper canonical types and
15175 fixes up the dependent types accordingly. */
15176 while (true)
15178 tree parameter;
15179 bool is_non_type;
15180 bool is_parameter_pack;
15181 location_t parm_loc;
15183 /* Parse the template-parameter. */
15184 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15185 parameter = cp_parser_template_parameter (parser,
15186 &is_non_type,
15187 &is_parameter_pack);
15188 /* Add it to the list. */
15189 if (parameter != error_mark_node)
15190 parameter_list = process_template_parm (parameter_list,
15191 parm_loc,
15192 parameter,
15193 is_non_type,
15194 is_parameter_pack);
15195 else
15197 tree err_parm = build_tree_list (parameter, parameter);
15198 parameter_list = chainon (parameter_list, err_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 return end_template_parm_list (parameter_list);
15211 /* Parse a introduction-list.
15213 introduction-list:
15214 introduced-parameter
15215 introduction-list , introduced-parameter
15217 introduced-parameter:
15218 ...[opt] identifier
15220 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15221 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15222 WILDCARD_DECL will also have DECL_NAME set and token location in
15223 DECL_SOURCE_LOCATION. */
15225 static tree
15226 cp_parser_introduction_list (cp_parser *parser)
15228 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15230 while (true)
15232 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15233 if (is_pack)
15234 cp_lexer_consume_token (parser->lexer);
15236 /* Build placeholder. */
15237 tree parm = build_nt (WILDCARD_DECL);
15238 DECL_SOURCE_LOCATION (parm)
15239 = cp_lexer_peek_token (parser->lexer)->location;
15240 DECL_NAME (parm) = cp_parser_identifier (parser);
15241 WILDCARD_PACK_P (parm) = is_pack;
15242 vec_safe_push (introduction_vec, parm);
15244 /* If the next token is not a `,', we're done. */
15245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15246 break;
15247 /* Otherwise, consume the `,' token. */
15248 cp_lexer_consume_token (parser->lexer);
15251 /* Convert the vec into a TREE_VEC. */
15252 tree introduction_list = make_tree_vec (introduction_vec->length ());
15253 unsigned int n;
15254 tree parm;
15255 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15256 TREE_VEC_ELT (introduction_list, n) = parm;
15258 release_tree_vector (introduction_vec);
15259 return introduction_list;
15262 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15263 is an abstract declarator. */
15265 static inline cp_declarator*
15266 get_id_declarator (cp_declarator *declarator)
15268 cp_declarator *d = declarator;
15269 while (d && d->kind != cdk_id)
15270 d = d->declarator;
15271 return d;
15274 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15275 is an abstract declarator. */
15277 static inline tree
15278 get_unqualified_id (cp_declarator *declarator)
15280 declarator = get_id_declarator (declarator);
15281 if (declarator)
15282 return declarator->u.id.unqualified_name;
15283 else
15284 return NULL_TREE;
15287 /* Returns true if DECL represents a constrained-parameter. */
15289 static inline bool
15290 is_constrained_parameter (tree decl)
15292 return (decl
15293 && TREE_CODE (decl) == TYPE_DECL
15294 && CONSTRAINED_PARM_CONCEPT (decl)
15295 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15298 /* Returns true if PARM declares a constrained-parameter. */
15300 static inline bool
15301 is_constrained_parameter (cp_parameter_declarator *parm)
15303 return is_constrained_parameter (parm->decl_specifiers.type);
15306 /* Check that the type parameter is only a declarator-id, and that its
15307 type is not cv-qualified. */
15309 bool
15310 cp_parser_check_constrained_type_parm (cp_parser *parser,
15311 cp_parameter_declarator *parm)
15313 if (!parm->declarator)
15314 return true;
15316 if (parm->declarator->kind != cdk_id)
15318 cp_parser_error (parser, "invalid constrained type parameter");
15319 return false;
15322 /* Don't allow cv-qualified type parameters. */
15323 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15324 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15326 cp_parser_error (parser, "cv-qualified type parameter");
15327 return false;
15330 return true;
15333 /* Finish parsing/processing a template type parameter and checking
15334 various restrictions. */
15336 static inline tree
15337 cp_parser_constrained_type_template_parm (cp_parser *parser,
15338 tree id,
15339 cp_parameter_declarator* parmdecl)
15341 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15342 return finish_template_type_parm (class_type_node, id);
15343 else
15344 return error_mark_node;
15347 static tree
15348 finish_constrained_template_template_parm (tree proto, tree id)
15350 /* FIXME: This should probably be copied, and we may need to adjust
15351 the template parameter depths. */
15352 tree saved_parms = current_template_parms;
15353 begin_template_parm_list ();
15354 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15355 end_template_parm_list ();
15357 tree parm = finish_template_template_parm (class_type_node, id);
15358 current_template_parms = saved_parms;
15360 return parm;
15363 /* Finish parsing/processing a template template parameter by borrowing
15364 the template parameter list from the prototype parameter. */
15366 static tree
15367 cp_parser_constrained_template_template_parm (cp_parser *parser,
15368 tree proto,
15369 tree id,
15370 cp_parameter_declarator *parmdecl)
15372 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15373 return error_mark_node;
15374 return finish_constrained_template_template_parm (proto, id);
15377 /* Create a new non-type template parameter from the given PARM
15378 declarator. */
15380 static tree
15381 constrained_non_type_template_parm (bool *is_non_type,
15382 cp_parameter_declarator *parm)
15384 *is_non_type = true;
15385 cp_declarator *decl = parm->declarator;
15386 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15387 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15388 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15391 /* Build a constrained template parameter based on the PARMDECL
15392 declarator. The type of PARMDECL is the constrained type, which
15393 refers to the prototype template parameter that ultimately
15394 specifies the type of the declared parameter. */
15396 static tree
15397 finish_constrained_parameter (cp_parser *parser,
15398 cp_parameter_declarator *parmdecl,
15399 bool *is_non_type,
15400 bool *is_parameter_pack)
15402 tree decl = parmdecl->decl_specifiers.type;
15403 tree id = get_unqualified_id (parmdecl->declarator);
15404 tree def = parmdecl->default_argument;
15405 tree proto = DECL_INITIAL (decl);
15407 /* A template parameter constrained by a variadic concept shall also
15408 be declared as a template parameter pack. */
15409 bool is_variadic = template_parameter_pack_p (proto);
15410 if (is_variadic && !*is_parameter_pack)
15411 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15413 /* Build the parameter. Return an error if the declarator was invalid. */
15414 tree parm;
15415 if (TREE_CODE (proto) == TYPE_DECL)
15416 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15417 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15418 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15419 parmdecl);
15420 else
15421 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15422 if (parm == error_mark_node)
15423 return error_mark_node;
15425 /* Finish the parameter decl and create a node attaching the
15426 default argument and constraint. */
15427 parm = build_tree_list (def, parm);
15428 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15430 return parm;
15433 /* Returns true if the parsed type actually represents the declaration
15434 of a type template-parameter. */
15436 static inline bool
15437 declares_constrained_type_template_parameter (tree type)
15439 return (is_constrained_parameter (type)
15440 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15444 /* Returns true if the parsed type actually represents the declaration of
15445 a template template-parameter. */
15447 static bool
15448 declares_constrained_template_template_parameter (tree type)
15450 return (is_constrained_parameter (type)
15451 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15454 /* Parse a default argument for a type template-parameter.
15455 Note that diagnostics are handled in cp_parser_template_parameter. */
15457 static tree
15458 cp_parser_default_type_template_argument (cp_parser *parser)
15460 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15462 /* Consume the `=' token. */
15463 cp_lexer_consume_token (parser->lexer);
15465 cp_token *token = cp_lexer_peek_token (parser->lexer);
15467 /* Parse the default-argument. */
15468 push_deferring_access_checks (dk_no_deferred);
15469 tree default_argument = cp_parser_type_id (parser);
15470 pop_deferring_access_checks ();
15472 if (flag_concepts && type_uses_auto (default_argument))
15474 error_at (token->location,
15475 "invalid use of %<auto%> in default template argument");
15476 return error_mark_node;
15479 return default_argument;
15482 /* Parse a default argument for a template template-parameter. */
15484 static tree
15485 cp_parser_default_template_template_argument (cp_parser *parser)
15487 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15489 bool is_template;
15491 /* Consume the `='. */
15492 cp_lexer_consume_token (parser->lexer);
15493 /* Parse the id-expression. */
15494 push_deferring_access_checks (dk_no_deferred);
15495 /* save token before parsing the id-expression, for error
15496 reporting */
15497 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15498 tree default_argument
15499 = cp_parser_id_expression (parser,
15500 /*template_keyword_p=*/false,
15501 /*check_dependency_p=*/true,
15502 /*template_p=*/&is_template,
15503 /*declarator_p=*/false,
15504 /*optional_p=*/false);
15505 if (TREE_CODE (default_argument) == TYPE_DECL)
15506 /* If the id-expression was a template-id that refers to
15507 a template-class, we already have the declaration here,
15508 so no further lookup is needed. */
15510 else
15511 /* Look up the name. */
15512 default_argument
15513 = cp_parser_lookup_name (parser, default_argument,
15514 none_type,
15515 /*is_template=*/is_template,
15516 /*is_namespace=*/false,
15517 /*check_dependency=*/true,
15518 /*ambiguous_decls=*/NULL,
15519 token->location);
15520 /* See if the default argument is valid. */
15521 default_argument = check_template_template_default_arg (default_argument);
15522 pop_deferring_access_checks ();
15523 return default_argument;
15526 /* Parse a template-parameter.
15528 template-parameter:
15529 type-parameter
15530 parameter-declaration
15532 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15533 the parameter. The TREE_PURPOSE is the default value, if any.
15534 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15535 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15536 set to true iff this parameter is a parameter pack. */
15538 static tree
15539 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15540 bool *is_parameter_pack)
15542 cp_token *token;
15543 cp_parameter_declarator *parameter_declarator;
15544 tree parm;
15546 /* Assume it is a type parameter or a template parameter. */
15547 *is_non_type = false;
15548 /* Assume it not a parameter pack. */
15549 *is_parameter_pack = false;
15550 /* Peek at the next token. */
15551 token = cp_lexer_peek_token (parser->lexer);
15552 /* If it is `template', we have a type-parameter. */
15553 if (token->keyword == RID_TEMPLATE)
15554 return cp_parser_type_parameter (parser, is_parameter_pack);
15555 /* If it is `class' or `typename' we do not know yet whether it is a
15556 type parameter or a non-type parameter. Consider:
15558 template <typename T, typename T::X X> ...
15562 template <class C, class D*> ...
15564 Here, the first parameter is a type parameter, and the second is
15565 a non-type parameter. We can tell by looking at the token after
15566 the identifier -- if it is a `,', `=', or `>' then we have a type
15567 parameter. */
15568 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15570 /* Peek at the token after `class' or `typename'. */
15571 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15572 /* If it's an ellipsis, we have a template type parameter
15573 pack. */
15574 if (token->type == CPP_ELLIPSIS)
15575 return cp_parser_type_parameter (parser, is_parameter_pack);
15576 /* If it's an identifier, skip it. */
15577 if (token->type == CPP_NAME)
15578 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15579 /* Now, see if the token looks like the end of a template
15580 parameter. */
15581 if (token->type == CPP_COMMA
15582 || token->type == CPP_EQ
15583 || token->type == CPP_GREATER)
15584 return cp_parser_type_parameter (parser, is_parameter_pack);
15587 /* Otherwise, it is a non-type parameter or a constrained parameter.
15589 [temp.param]
15591 When parsing a default template-argument for a non-type
15592 template-parameter, the first non-nested `>' is taken as the end
15593 of the template parameter-list rather than a greater-than
15594 operator. */
15595 parameter_declarator
15596 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15597 /*parenthesized_p=*/NULL);
15599 if (!parameter_declarator)
15600 return error_mark_node;
15602 /* If the parameter declaration is marked as a parameter pack, set
15603 *IS_PARAMETER_PACK to notify the caller. */
15604 if (parameter_declarator->template_parameter_pack_p)
15605 *is_parameter_pack = true;
15607 if (parameter_declarator->default_argument)
15609 /* Can happen in some cases of erroneous input (c++/34892). */
15610 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15611 /* Consume the `...' for better error recovery. */
15612 cp_lexer_consume_token (parser->lexer);
15615 // The parameter may have been constrained.
15616 if (is_constrained_parameter (parameter_declarator))
15617 return finish_constrained_parameter (parser,
15618 parameter_declarator,
15619 is_non_type,
15620 is_parameter_pack);
15622 // Now we're sure that the parameter is a non-type parameter.
15623 *is_non_type = true;
15625 parm = grokdeclarator (parameter_declarator->declarator,
15626 &parameter_declarator->decl_specifiers,
15627 TPARM, /*initialized=*/0,
15628 /*attrlist=*/NULL);
15629 if (parm == error_mark_node)
15630 return error_mark_node;
15632 return build_tree_list (parameter_declarator->default_argument, parm);
15635 /* Parse a type-parameter.
15637 type-parameter:
15638 class identifier [opt]
15639 class identifier [opt] = type-id
15640 typename identifier [opt]
15641 typename identifier [opt] = type-id
15642 template < template-parameter-list > class identifier [opt]
15643 template < template-parameter-list > class identifier [opt]
15644 = id-expression
15646 GNU Extension (variadic templates):
15648 type-parameter:
15649 class ... identifier [opt]
15650 typename ... identifier [opt]
15652 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15653 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15654 the declaration of the parameter.
15656 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15658 static tree
15659 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15661 cp_token *token;
15662 tree parameter;
15664 /* Look for a keyword to tell us what kind of parameter this is. */
15665 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15666 if (!token)
15667 return error_mark_node;
15669 switch (token->keyword)
15671 case RID_CLASS:
15672 case RID_TYPENAME:
15674 tree identifier;
15675 tree default_argument;
15677 /* If the next token is an ellipsis, we have a template
15678 argument pack. */
15679 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15681 /* Consume the `...' token. */
15682 cp_lexer_consume_token (parser->lexer);
15683 maybe_warn_variadic_templates ();
15685 *is_parameter_pack = true;
15688 /* If the next token is an identifier, then it names the
15689 parameter. */
15690 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15691 identifier = cp_parser_identifier (parser);
15692 else
15693 identifier = NULL_TREE;
15695 /* Create the parameter. */
15696 parameter = finish_template_type_parm (class_type_node, identifier);
15698 /* If the next token is an `=', we have a default argument. */
15699 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15701 default_argument
15702 = cp_parser_default_type_template_argument (parser);
15704 /* Template parameter packs cannot have default
15705 arguments. */
15706 if (*is_parameter_pack)
15708 if (identifier)
15709 error_at (token->location,
15710 "template parameter pack %qD cannot have a "
15711 "default argument", identifier);
15712 else
15713 error_at (token->location,
15714 "template parameter packs cannot have "
15715 "default arguments");
15716 default_argument = NULL_TREE;
15718 else if (check_for_bare_parameter_packs (default_argument))
15719 default_argument = error_mark_node;
15721 else
15722 default_argument = NULL_TREE;
15724 /* Create the combined representation of the parameter and the
15725 default argument. */
15726 parameter = build_tree_list (default_argument, parameter);
15728 break;
15730 case RID_TEMPLATE:
15732 tree identifier;
15733 tree default_argument;
15735 /* Look for the `<'. */
15736 cp_parser_require (parser, CPP_LESS, RT_LESS);
15737 /* Parse the template-parameter-list. */
15738 cp_parser_template_parameter_list (parser);
15739 /* Look for the `>'. */
15740 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15742 // If template requirements are present, parse them.
15743 if (flag_concepts)
15745 tree reqs = get_shorthand_constraints (current_template_parms);
15746 if (tree r = cp_parser_requires_clause_opt (parser))
15747 reqs = conjoin_constraints (reqs, normalize_expression (r));
15748 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15751 /* Look for the `class' or 'typename' keywords. */
15752 cp_parser_type_parameter_key (parser);
15753 /* If the next token is an ellipsis, we have a template
15754 argument pack. */
15755 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15757 /* Consume the `...' token. */
15758 cp_lexer_consume_token (parser->lexer);
15759 maybe_warn_variadic_templates ();
15761 *is_parameter_pack = true;
15763 /* If the next token is an `=', then there is a
15764 default-argument. If the next token is a `>', we are at
15765 the end of the parameter-list. If the next token is a `,',
15766 then we are at the end of this parameter. */
15767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15768 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15769 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15771 identifier = cp_parser_identifier (parser);
15772 /* Treat invalid names as if the parameter were nameless. */
15773 if (identifier == error_mark_node)
15774 identifier = NULL_TREE;
15776 else
15777 identifier = NULL_TREE;
15779 /* Create the template parameter. */
15780 parameter = finish_template_template_parm (class_type_node,
15781 identifier);
15783 /* If the next token is an `=', then there is a
15784 default-argument. */
15785 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15787 default_argument
15788 = cp_parser_default_template_template_argument (parser);
15790 /* Template parameter packs cannot have default
15791 arguments. */
15792 if (*is_parameter_pack)
15794 if (identifier)
15795 error_at (token->location,
15796 "template parameter pack %qD cannot "
15797 "have a default argument",
15798 identifier);
15799 else
15800 error_at (token->location, "template parameter packs cannot "
15801 "have default arguments");
15802 default_argument = NULL_TREE;
15805 else
15806 default_argument = NULL_TREE;
15808 /* Create the combined representation of the parameter and the
15809 default argument. */
15810 parameter = build_tree_list (default_argument, parameter);
15812 break;
15814 default:
15815 gcc_unreachable ();
15816 break;
15819 return parameter;
15822 /* Parse a template-id.
15824 template-id:
15825 template-name < template-argument-list [opt] >
15827 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15828 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15829 returned. Otherwise, if the template-name names a function, or set
15830 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15831 names a class, returns a TYPE_DECL for the specialization.
15833 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15834 uninstantiated templates. */
15836 static tree
15837 cp_parser_template_id (cp_parser *parser,
15838 bool template_keyword_p,
15839 bool check_dependency_p,
15840 enum tag_types tag_type,
15841 bool is_declaration)
15843 tree templ;
15844 tree arguments;
15845 tree template_id;
15846 cp_token_position start_of_id = 0;
15847 cp_token *next_token = NULL, *next_token_2 = NULL;
15848 bool is_identifier;
15850 /* If the next token corresponds to a template-id, there is no need
15851 to reparse it. */
15852 cp_token *token = cp_lexer_peek_token (parser->lexer);
15853 if (token->type == CPP_TEMPLATE_ID)
15855 cp_lexer_consume_token (parser->lexer);
15856 return saved_checks_value (token->u.tree_check_value);
15859 /* Avoid performing name lookup if there is no possibility of
15860 finding a template-id. */
15861 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15862 || (token->type == CPP_NAME
15863 && !cp_parser_nth_token_starts_template_argument_list_p
15864 (parser, 2)))
15866 cp_parser_error (parser, "expected template-id");
15867 return error_mark_node;
15870 /* Remember where the template-id starts. */
15871 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15872 start_of_id = cp_lexer_token_position (parser->lexer, false);
15874 push_deferring_access_checks (dk_deferred);
15876 /* Parse the template-name. */
15877 is_identifier = false;
15878 templ = cp_parser_template_name (parser, template_keyword_p,
15879 check_dependency_p,
15880 is_declaration,
15881 tag_type,
15882 &is_identifier);
15883 if (templ == error_mark_node || is_identifier)
15885 pop_deferring_access_checks ();
15886 return templ;
15889 /* Since we're going to preserve any side-effects from this parse, set up a
15890 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15891 in the template arguments. */
15892 tentative_firewall firewall (parser);
15894 /* If we find the sequence `[:' after a template-name, it's probably
15895 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15896 parse correctly the argument list. */
15897 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15898 == CPP_OPEN_SQUARE)
15899 && next_token->flags & DIGRAPH
15900 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15901 == CPP_COLON)
15902 && !(next_token_2->flags & PREV_WHITE))
15904 cp_parser_parse_tentatively (parser);
15905 /* Change `:' into `::'. */
15906 next_token_2->type = CPP_SCOPE;
15907 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15908 CPP_LESS. */
15909 cp_lexer_consume_token (parser->lexer);
15911 /* Parse the arguments. */
15912 arguments = cp_parser_enclosed_template_argument_list (parser);
15913 if (!cp_parser_parse_definitely (parser))
15915 /* If we couldn't parse an argument list, then we revert our changes
15916 and return simply an error. Maybe this is not a template-id
15917 after all. */
15918 next_token_2->type = CPP_COLON;
15919 cp_parser_error (parser, "expected %<<%>");
15920 pop_deferring_access_checks ();
15921 return error_mark_node;
15923 /* Otherwise, emit an error about the invalid digraph, but continue
15924 parsing because we got our argument list. */
15925 if (permerror (next_token->location,
15926 "%<<::%> cannot begin a template-argument list"))
15928 static bool hint = false;
15929 inform (next_token->location,
15930 "%<<:%> is an alternate spelling for %<[%>."
15931 " Insert whitespace between %<<%> and %<::%>");
15932 if (!hint && !flag_permissive)
15934 inform (next_token->location, "(if you use %<-fpermissive%> "
15935 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15936 "accept your code)");
15937 hint = true;
15941 else
15943 /* Look for the `<' that starts the template-argument-list. */
15944 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15946 pop_deferring_access_checks ();
15947 return error_mark_node;
15949 /* Parse the arguments. */
15950 arguments = cp_parser_enclosed_template_argument_list (parser);
15953 /* Set the location to be of the form:
15954 template-name < template-argument-list [opt] >
15955 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15956 with caret == start at the start of the template-name,
15957 ranging until the closing '>'. */
15958 location_t finish_loc
15959 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15960 location_t combined_loc
15961 = make_location (token->location, token->location, finish_loc);
15963 /* Check for concepts autos where they don't belong. We could
15964 identify types in some cases of idnetifier TEMPL, looking ahead
15965 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15966 types. We reject them in functions, but if what we have is an
15967 identifier, even with none_type we can't conclude it's NOT a
15968 type, we have to wait for template substitution. */
15969 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15970 template_id = error_mark_node;
15971 /* Build a representation of the specialization. */
15972 else if (identifier_p (templ))
15973 template_id = build_min_nt_loc (combined_loc,
15974 TEMPLATE_ID_EXPR,
15975 templ, arguments);
15976 else if (DECL_TYPE_TEMPLATE_P (templ)
15977 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15979 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15980 template (rather than some instantiation thereof) only if
15981 is not nested within some other construct. For example, in
15982 "template <typename T> void f(T) { A<T>::", A<T> is just an
15983 instantiation of A. */
15984 bool entering_scope
15985 = (template_parm_scope_p ()
15986 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
15987 template_id
15988 = finish_template_type (templ, arguments, entering_scope);
15990 /* A template-like identifier may be a partial concept id. */
15991 else if (flag_concepts
15992 && (template_id = (cp_parser_maybe_partial_concept_id
15993 (parser, templ, arguments))))
15994 return template_id;
15995 else if (variable_template_p (templ))
15997 template_id = lookup_template_variable (templ, arguments);
15998 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15999 SET_EXPR_LOCATION (template_id, combined_loc);
16001 else
16003 /* If it's not a class-template or a template-template, it should be
16004 a function-template. */
16005 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16006 || TREE_CODE (templ) == OVERLOAD
16007 || BASELINK_P (templ)));
16009 template_id = lookup_template_function (templ, arguments);
16010 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16011 SET_EXPR_LOCATION (template_id, combined_loc);
16014 /* If parsing tentatively, replace the sequence of tokens that makes
16015 up the template-id with a CPP_TEMPLATE_ID token. That way,
16016 should we re-parse the token stream, we will not have to repeat
16017 the effort required to do the parse, nor will we issue duplicate
16018 error messages about problems during instantiation of the
16019 template. */
16020 if (start_of_id
16021 /* Don't do this if we had a parse error in a declarator; re-parsing
16022 might succeed if a name changes meaning (60361). */
16023 && !(cp_parser_error_occurred (parser)
16024 && cp_parser_parsing_tentatively (parser)
16025 && parser->in_declarator_p))
16027 /* Reset the contents of the START_OF_ID token. */
16028 token->type = CPP_TEMPLATE_ID;
16029 token->location = combined_loc;
16031 /* Retrieve any deferred checks. Do not pop this access checks yet
16032 so the memory will not be reclaimed during token replacing below. */
16033 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16034 token->u.tree_check_value->value = template_id;
16035 token->u.tree_check_value->checks = get_deferred_access_checks ();
16036 token->keyword = RID_MAX;
16038 /* Purge all subsequent tokens. */
16039 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16041 /* ??? Can we actually assume that, if template_id ==
16042 error_mark_node, we will have issued a diagnostic to the
16043 user, as opposed to simply marking the tentative parse as
16044 failed? */
16045 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16046 error_at (token->location, "parse error in template argument list");
16049 pop_to_parent_deferring_access_checks ();
16050 return template_id;
16053 /* Parse a template-name.
16055 template-name:
16056 identifier
16058 The standard should actually say:
16060 template-name:
16061 identifier
16062 operator-function-id
16064 A defect report has been filed about this issue.
16066 A conversion-function-id cannot be a template name because they cannot
16067 be part of a template-id. In fact, looking at this code:
16069 a.operator K<int>()
16071 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16072 It is impossible to call a templated conversion-function-id with an
16073 explicit argument list, since the only allowed template parameter is
16074 the type to which it is converting.
16076 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16077 `template' keyword, in a construction like:
16079 T::template f<3>()
16081 In that case `f' is taken to be a template-name, even though there
16082 is no way of knowing for sure.
16084 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16085 name refers to a set of overloaded functions, at least one of which
16086 is a template, or an IDENTIFIER_NODE with the name of the template,
16087 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16088 names are looked up inside uninstantiated templates. */
16090 static tree
16091 cp_parser_template_name (cp_parser* parser,
16092 bool template_keyword_p,
16093 bool check_dependency_p,
16094 bool is_declaration,
16095 enum tag_types tag_type,
16096 bool *is_identifier)
16098 tree identifier;
16099 tree decl;
16100 cp_token *token = cp_lexer_peek_token (parser->lexer);
16102 /* If the next token is `operator', then we have either an
16103 operator-function-id or a conversion-function-id. */
16104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16106 /* We don't know whether we're looking at an
16107 operator-function-id or a conversion-function-id. */
16108 cp_parser_parse_tentatively (parser);
16109 /* Try an operator-function-id. */
16110 identifier = cp_parser_operator_function_id (parser);
16111 /* If that didn't work, try a conversion-function-id. */
16112 if (!cp_parser_parse_definitely (parser))
16114 cp_parser_error (parser, "expected template-name");
16115 return error_mark_node;
16118 /* Look for the identifier. */
16119 else
16120 identifier = cp_parser_identifier (parser);
16122 /* If we didn't find an identifier, we don't have a template-id. */
16123 if (identifier == error_mark_node)
16124 return error_mark_node;
16126 /* If the name immediately followed the `template' keyword, then it
16127 is a template-name. However, if the next token is not `<', then
16128 we do not treat it as a template-name, since it is not being used
16129 as part of a template-id. This enables us to handle constructs
16130 like:
16132 template <typename T> struct S { S(); };
16133 template <typename T> S<T>::S();
16135 correctly. We would treat `S' as a template -- if it were `S<T>'
16136 -- but we do not if there is no `<'. */
16138 if (processing_template_decl
16139 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16141 /* In a declaration, in a dependent context, we pretend that the
16142 "template" keyword was present in order to improve error
16143 recovery. For example, given:
16145 template <typename T> void f(T::X<int>);
16147 we want to treat "X<int>" as a template-id. */
16148 if (is_declaration
16149 && !template_keyword_p
16150 && parser->scope && TYPE_P (parser->scope)
16151 && check_dependency_p
16152 && dependent_scope_p (parser->scope)
16153 /* Do not do this for dtors (or ctors), since they never
16154 need the template keyword before their name. */
16155 && !constructor_name_p (identifier, parser->scope))
16157 cp_token_position start = 0;
16159 /* Explain what went wrong. */
16160 error_at (token->location, "non-template %qD used as template",
16161 identifier);
16162 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16163 parser->scope, identifier);
16164 /* If parsing tentatively, find the location of the "<" token. */
16165 if (cp_parser_simulate_error (parser))
16166 start = cp_lexer_token_position (parser->lexer, true);
16167 /* Parse the template arguments so that we can issue error
16168 messages about them. */
16169 cp_lexer_consume_token (parser->lexer);
16170 cp_parser_enclosed_template_argument_list (parser);
16171 /* Skip tokens until we find a good place from which to
16172 continue parsing. */
16173 cp_parser_skip_to_closing_parenthesis (parser,
16174 /*recovering=*/true,
16175 /*or_comma=*/true,
16176 /*consume_paren=*/false);
16177 /* If parsing tentatively, permanently remove the
16178 template argument list. That will prevent duplicate
16179 error messages from being issued about the missing
16180 "template" keyword. */
16181 if (start)
16182 cp_lexer_purge_tokens_after (parser->lexer, start);
16183 if (is_identifier)
16184 *is_identifier = true;
16185 parser->context->object_type = NULL_TREE;
16186 return identifier;
16189 /* If the "template" keyword is present, then there is generally
16190 no point in doing name-lookup, so we just return IDENTIFIER.
16191 But, if the qualifying scope is non-dependent then we can
16192 (and must) do name-lookup normally. */
16193 if (template_keyword_p)
16195 tree scope = (parser->scope ? parser->scope
16196 : parser->context->object_type);
16197 if (scope && TYPE_P (scope)
16198 && (!CLASS_TYPE_P (scope)
16199 || (check_dependency_p && dependent_type_p (scope))))
16201 /* We're optimizing away the call to cp_parser_lookup_name, but
16202 we still need to do this. */
16203 parser->context->object_type = NULL_TREE;
16204 return identifier;
16209 /* Look up the name. */
16210 decl = cp_parser_lookup_name (parser, identifier,
16211 tag_type,
16212 /*is_template=*/true,
16213 /*is_namespace=*/false,
16214 check_dependency_p,
16215 /*ambiguous_decls=*/NULL,
16216 token->location);
16218 decl = strip_using_decl (decl);
16220 /* If DECL is a template, then the name was a template-name. */
16221 if (TREE_CODE (decl) == TEMPLATE_DECL)
16223 if (TREE_DEPRECATED (decl)
16224 && deprecated_state != DEPRECATED_SUPPRESS)
16225 warn_deprecated_use (decl, NULL_TREE);
16227 else
16229 /* The standard does not explicitly indicate whether a name that
16230 names a set of overloaded declarations, some of which are
16231 templates, is a template-name. However, such a name should
16232 be a template-name; otherwise, there is no way to form a
16233 template-id for the overloaded templates. */
16234 bool found = false;
16236 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16237 !found && iter; ++iter)
16238 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16239 found = true;
16241 if (!found)
16243 /* The name does not name a template. */
16244 cp_parser_error (parser, "expected template-name");
16245 return error_mark_node;
16249 /* If DECL is dependent, and refers to a function, then just return
16250 its name; we will look it up again during template instantiation. */
16251 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16253 tree scope = ovl_scope (decl);
16254 if (TYPE_P (scope) && dependent_type_p (scope))
16255 return identifier;
16258 return decl;
16261 /* Parse a template-argument-list.
16263 template-argument-list:
16264 template-argument ... [opt]
16265 template-argument-list , template-argument ... [opt]
16267 Returns a TREE_VEC containing the arguments. */
16269 static tree
16270 cp_parser_template_argument_list (cp_parser* parser)
16272 tree fixed_args[10];
16273 unsigned n_args = 0;
16274 unsigned alloced = 10;
16275 tree *arg_ary = fixed_args;
16276 tree vec;
16277 bool saved_in_template_argument_list_p;
16278 bool saved_ice_p;
16279 bool saved_non_ice_p;
16281 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16282 parser->in_template_argument_list_p = true;
16283 /* Even if the template-id appears in an integral
16284 constant-expression, the contents of the argument list do
16285 not. */
16286 saved_ice_p = parser->integral_constant_expression_p;
16287 parser->integral_constant_expression_p = false;
16288 saved_non_ice_p = parser->non_integral_constant_expression_p;
16289 parser->non_integral_constant_expression_p = false;
16291 /* Parse the arguments. */
16294 tree argument;
16296 if (n_args)
16297 /* Consume the comma. */
16298 cp_lexer_consume_token (parser->lexer);
16300 /* Parse the template-argument. */
16301 argument = cp_parser_template_argument (parser);
16303 /* If the next token is an ellipsis, we're expanding a template
16304 argument pack. */
16305 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16307 if (argument == error_mark_node)
16309 cp_token *token = cp_lexer_peek_token (parser->lexer);
16310 error_at (token->location,
16311 "expected parameter pack before %<...%>");
16313 /* Consume the `...' token. */
16314 cp_lexer_consume_token (parser->lexer);
16316 /* Make the argument into a TYPE_PACK_EXPANSION or
16317 EXPR_PACK_EXPANSION. */
16318 argument = make_pack_expansion (argument);
16321 if (n_args == alloced)
16323 alloced *= 2;
16325 if (arg_ary == fixed_args)
16327 arg_ary = XNEWVEC (tree, alloced);
16328 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16330 else
16331 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16333 arg_ary[n_args++] = argument;
16335 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16337 vec = make_tree_vec (n_args);
16339 while (n_args--)
16340 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16342 if (arg_ary != fixed_args)
16343 free (arg_ary);
16344 parser->non_integral_constant_expression_p = saved_non_ice_p;
16345 parser->integral_constant_expression_p = saved_ice_p;
16346 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16347 if (CHECKING_P)
16348 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16349 return vec;
16352 /* Parse a template-argument.
16354 template-argument:
16355 assignment-expression
16356 type-id
16357 id-expression
16359 The representation is that of an assignment-expression, type-id, or
16360 id-expression -- except that the qualified id-expression is
16361 evaluated, so that the value returned is either a DECL or an
16362 OVERLOAD.
16364 Although the standard says "assignment-expression", it forbids
16365 throw-expressions or assignments in the template argument.
16366 Therefore, we use "conditional-expression" instead. */
16368 static tree
16369 cp_parser_template_argument (cp_parser* parser)
16371 tree argument;
16372 bool template_p;
16373 bool address_p;
16374 bool maybe_type_id = false;
16375 cp_token *token = NULL, *argument_start_token = NULL;
16376 location_t loc = 0;
16377 cp_id_kind idk;
16379 /* There's really no way to know what we're looking at, so we just
16380 try each alternative in order.
16382 [temp.arg]
16384 In a template-argument, an ambiguity between a type-id and an
16385 expression is resolved to a type-id, regardless of the form of
16386 the corresponding template-parameter.
16388 Therefore, we try a type-id first. */
16389 cp_parser_parse_tentatively (parser);
16390 argument = cp_parser_template_type_arg (parser);
16391 /* If there was no error parsing the type-id but the next token is a
16392 '>>', our behavior depends on which dialect of C++ we're
16393 parsing. In C++98, we probably found a typo for '> >'. But there
16394 are type-id which are also valid expressions. For instance:
16396 struct X { int operator >> (int); };
16397 template <int V> struct Foo {};
16398 Foo<X () >> 5> r;
16400 Here 'X()' is a valid type-id of a function type, but the user just
16401 wanted to write the expression "X() >> 5". Thus, we remember that we
16402 found a valid type-id, but we still try to parse the argument as an
16403 expression to see what happens.
16405 In C++0x, the '>>' will be considered two separate '>'
16406 tokens. */
16407 if (!cp_parser_error_occurred (parser)
16408 && cxx_dialect == cxx98
16409 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16411 maybe_type_id = true;
16412 cp_parser_abort_tentative_parse (parser);
16414 else
16416 /* If the next token isn't a `,' or a `>', then this argument wasn't
16417 really finished. This means that the argument is not a valid
16418 type-id. */
16419 if (!cp_parser_next_token_ends_template_argument_p (parser))
16420 cp_parser_error (parser, "expected template-argument");
16421 /* If that worked, we're done. */
16422 if (cp_parser_parse_definitely (parser))
16423 return argument;
16425 /* We're still not sure what the argument will be. */
16426 cp_parser_parse_tentatively (parser);
16427 /* Try a template. */
16428 argument_start_token = cp_lexer_peek_token (parser->lexer);
16429 argument = cp_parser_id_expression (parser,
16430 /*template_keyword_p=*/false,
16431 /*check_dependency_p=*/true,
16432 &template_p,
16433 /*declarator_p=*/false,
16434 /*optional_p=*/false);
16435 /* If the next token isn't a `,' or a `>', then this argument wasn't
16436 really finished. */
16437 if (!cp_parser_next_token_ends_template_argument_p (parser))
16438 cp_parser_error (parser, "expected template-argument");
16439 if (!cp_parser_error_occurred (parser))
16441 /* Figure out what is being referred to. If the id-expression
16442 was for a class template specialization, then we will have a
16443 TYPE_DECL at this point. There is no need to do name lookup
16444 at this point in that case. */
16445 if (TREE_CODE (argument) != TYPE_DECL)
16446 argument = cp_parser_lookup_name (parser, argument,
16447 none_type,
16448 /*is_template=*/template_p,
16449 /*is_namespace=*/false,
16450 /*check_dependency=*/true,
16451 /*ambiguous_decls=*/NULL,
16452 argument_start_token->location);
16453 /* Handle a constrained-type-specifier for a non-type template
16454 parameter. */
16455 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16456 argument = decl;
16457 else if (TREE_CODE (argument) != TEMPLATE_DECL
16458 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16459 cp_parser_error (parser, "expected template-name");
16461 if (cp_parser_parse_definitely (parser))
16463 if (TREE_DEPRECATED (argument))
16464 warn_deprecated_use (argument, NULL_TREE);
16465 return argument;
16467 /* It must be a non-type argument. In C++17 any constant-expression is
16468 allowed. */
16469 if (cxx_dialect > cxx14)
16470 goto general_expr;
16472 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16474 -- an integral constant-expression of integral or enumeration
16475 type; or
16477 -- the name of a non-type template-parameter; or
16479 -- the name of an object or function with external linkage...
16481 -- the address of an object or function with external linkage...
16483 -- a pointer to member... */
16484 /* Look for a non-type template parameter. */
16485 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16487 cp_parser_parse_tentatively (parser);
16488 argument = cp_parser_primary_expression (parser,
16489 /*address_p=*/false,
16490 /*cast_p=*/false,
16491 /*template_arg_p=*/true,
16492 &idk);
16493 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16494 || !cp_parser_next_token_ends_template_argument_p (parser))
16495 cp_parser_simulate_error (parser);
16496 if (cp_parser_parse_definitely (parser))
16497 return argument;
16500 /* If the next token is "&", the argument must be the address of an
16501 object or function with external linkage. */
16502 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16503 if (address_p)
16505 loc = cp_lexer_peek_token (parser->lexer)->location;
16506 cp_lexer_consume_token (parser->lexer);
16508 /* See if we might have an id-expression. */
16509 token = cp_lexer_peek_token (parser->lexer);
16510 if (token->type == CPP_NAME
16511 || token->keyword == RID_OPERATOR
16512 || token->type == CPP_SCOPE
16513 || token->type == CPP_TEMPLATE_ID
16514 || token->type == CPP_NESTED_NAME_SPECIFIER)
16516 cp_parser_parse_tentatively (parser);
16517 argument = cp_parser_primary_expression (parser,
16518 address_p,
16519 /*cast_p=*/false,
16520 /*template_arg_p=*/true,
16521 &idk);
16522 if (cp_parser_error_occurred (parser)
16523 || !cp_parser_next_token_ends_template_argument_p (parser))
16524 cp_parser_abort_tentative_parse (parser);
16525 else
16527 tree probe;
16529 if (INDIRECT_REF_P (argument))
16531 /* Strip the dereference temporarily. */
16532 gcc_assert (REFERENCE_REF_P (argument));
16533 argument = TREE_OPERAND (argument, 0);
16536 /* If we're in a template, we represent a qualified-id referring
16537 to a static data member as a SCOPE_REF even if the scope isn't
16538 dependent so that we can check access control later. */
16539 probe = argument;
16540 if (TREE_CODE (probe) == SCOPE_REF)
16541 probe = TREE_OPERAND (probe, 1);
16542 if (VAR_P (probe))
16544 /* A variable without external linkage might still be a
16545 valid constant-expression, so no error is issued here
16546 if the external-linkage check fails. */
16547 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16548 cp_parser_simulate_error (parser);
16550 else if (is_overloaded_fn (argument))
16551 /* All overloaded functions are allowed; if the external
16552 linkage test does not pass, an error will be issued
16553 later. */
16555 else if (address_p
16556 && (TREE_CODE (argument) == OFFSET_REF
16557 || TREE_CODE (argument) == SCOPE_REF))
16558 /* A pointer-to-member. */
16560 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16562 else
16563 cp_parser_simulate_error (parser);
16565 if (cp_parser_parse_definitely (parser))
16567 if (address_p)
16568 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16569 tf_warning_or_error);
16570 else
16571 argument = convert_from_reference (argument);
16572 return argument;
16576 /* If the argument started with "&", there are no other valid
16577 alternatives at this point. */
16578 if (address_p)
16580 cp_parser_error (parser, "invalid non-type template argument");
16581 return error_mark_node;
16584 general_expr:
16585 /* If the argument wasn't successfully parsed as a type-id followed
16586 by '>>', the argument can only be a constant expression now.
16587 Otherwise, we try parsing the constant-expression tentatively,
16588 because the argument could really be a type-id. */
16589 if (maybe_type_id)
16590 cp_parser_parse_tentatively (parser);
16592 if (cxx_dialect <= cxx14)
16593 argument = cp_parser_constant_expression (parser);
16594 else
16596 /* With C++17 generalized non-type template arguments we need to handle
16597 lvalue constant expressions, too. */
16598 argument = cp_parser_assignment_expression (parser);
16599 require_potential_constant_expression (argument);
16602 if (!maybe_type_id)
16603 return argument;
16604 if (!cp_parser_next_token_ends_template_argument_p (parser))
16605 cp_parser_error (parser, "expected template-argument");
16606 if (cp_parser_parse_definitely (parser))
16607 return argument;
16608 /* We did our best to parse the argument as a non type-id, but that
16609 was the only alternative that matched (albeit with a '>' after
16610 it). We can assume it's just a typo from the user, and a
16611 diagnostic will then be issued. */
16612 return cp_parser_template_type_arg (parser);
16615 /* Parse an explicit-instantiation.
16617 explicit-instantiation:
16618 template declaration
16620 Although the standard says `declaration', what it really means is:
16622 explicit-instantiation:
16623 template decl-specifier-seq [opt] declarator [opt] ;
16625 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16626 supposed to be allowed. A defect report has been filed about this
16627 issue.
16629 GNU Extension:
16631 explicit-instantiation:
16632 storage-class-specifier template
16633 decl-specifier-seq [opt] declarator [opt] ;
16634 function-specifier template
16635 decl-specifier-seq [opt] declarator [opt] ; */
16637 static void
16638 cp_parser_explicit_instantiation (cp_parser* parser)
16640 int declares_class_or_enum;
16641 cp_decl_specifier_seq decl_specifiers;
16642 tree extension_specifier = NULL_TREE;
16644 timevar_push (TV_TEMPLATE_INST);
16646 /* Look for an (optional) storage-class-specifier or
16647 function-specifier. */
16648 if (cp_parser_allow_gnu_extensions_p (parser))
16650 extension_specifier
16651 = cp_parser_storage_class_specifier_opt (parser);
16652 if (!extension_specifier)
16653 extension_specifier
16654 = cp_parser_function_specifier_opt (parser,
16655 /*decl_specs=*/NULL);
16658 /* Look for the `template' keyword. */
16659 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16660 /* Let the front end know that we are processing an explicit
16661 instantiation. */
16662 begin_explicit_instantiation ();
16663 /* [temp.explicit] says that we are supposed to ignore access
16664 control while processing explicit instantiation directives. */
16665 push_deferring_access_checks (dk_no_check);
16666 /* Parse a decl-specifier-seq. */
16667 cp_parser_decl_specifier_seq (parser,
16668 CP_PARSER_FLAGS_OPTIONAL,
16669 &decl_specifiers,
16670 &declares_class_or_enum);
16671 /* If there was exactly one decl-specifier, and it declared a class,
16672 and there's no declarator, then we have an explicit type
16673 instantiation. */
16674 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16676 tree type;
16678 type = check_tag_decl (&decl_specifiers,
16679 /*explicit_type_instantiation_p=*/true);
16680 /* Turn access control back on for names used during
16681 template instantiation. */
16682 pop_deferring_access_checks ();
16683 if (type)
16684 do_type_instantiation (type, extension_specifier,
16685 /*complain=*/tf_error);
16687 else
16689 cp_declarator *declarator;
16690 tree decl;
16692 /* Parse the declarator. */
16693 declarator
16694 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16695 /*ctor_dtor_or_conv_p=*/NULL,
16696 /*parenthesized_p=*/NULL,
16697 /*member_p=*/false,
16698 /*friend_p=*/false);
16699 if (declares_class_or_enum & 2)
16700 cp_parser_check_for_definition_in_return_type (declarator,
16701 decl_specifiers.type,
16702 decl_specifiers.locations[ds_type_spec]);
16703 if (declarator != cp_error_declarator)
16705 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16706 permerror (decl_specifiers.locations[ds_inline],
16707 "explicit instantiation shall not use"
16708 " %<inline%> specifier");
16709 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16710 permerror (decl_specifiers.locations[ds_constexpr],
16711 "explicit instantiation shall not use"
16712 " %<constexpr%> specifier");
16714 decl = grokdeclarator (declarator, &decl_specifiers,
16715 NORMAL, 0, &decl_specifiers.attributes);
16716 /* Turn access control back on for names used during
16717 template instantiation. */
16718 pop_deferring_access_checks ();
16719 /* Do the explicit instantiation. */
16720 do_decl_instantiation (decl, extension_specifier);
16722 else
16724 pop_deferring_access_checks ();
16725 /* Skip the body of the explicit instantiation. */
16726 cp_parser_skip_to_end_of_statement (parser);
16729 /* We're done with the instantiation. */
16730 end_explicit_instantiation ();
16732 cp_parser_consume_semicolon_at_end_of_statement (parser);
16734 timevar_pop (TV_TEMPLATE_INST);
16737 /* Parse an explicit-specialization.
16739 explicit-specialization:
16740 template < > declaration
16742 Although the standard says `declaration', what it really means is:
16744 explicit-specialization:
16745 template <> decl-specifier [opt] init-declarator [opt] ;
16746 template <> function-definition
16747 template <> explicit-specialization
16748 template <> template-declaration */
16750 static void
16751 cp_parser_explicit_specialization (cp_parser* parser)
16753 bool need_lang_pop;
16754 cp_token *token = cp_lexer_peek_token (parser->lexer);
16756 /* Look for the `template' keyword. */
16757 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16758 /* Look for the `<'. */
16759 cp_parser_require (parser, CPP_LESS, RT_LESS);
16760 /* Look for the `>'. */
16761 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16762 /* We have processed another parameter list. */
16763 ++parser->num_template_parameter_lists;
16764 /* [temp]
16766 A template ... explicit specialization ... shall not have C
16767 linkage. */
16768 if (current_lang_name == lang_name_c)
16770 error_at (token->location, "template specialization with C linkage");
16771 maybe_show_extern_c_location ();
16772 /* Give it C++ linkage to avoid confusing other parts of the
16773 front end. */
16774 push_lang_context (lang_name_cplusplus);
16775 need_lang_pop = true;
16777 else
16778 need_lang_pop = false;
16779 /* Let the front end know that we are beginning a specialization. */
16780 if (!begin_specialization ())
16782 end_specialization ();
16783 return;
16786 /* If the next keyword is `template', we need to figure out whether
16787 or not we're looking a template-declaration. */
16788 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16790 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16791 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16792 cp_parser_template_declaration_after_export (parser,
16793 /*member_p=*/false);
16794 else
16795 cp_parser_explicit_specialization (parser);
16797 else
16798 /* Parse the dependent declaration. */
16799 cp_parser_single_declaration (parser,
16800 /*checks=*/NULL,
16801 /*member_p=*/false,
16802 /*explicit_specialization_p=*/true,
16803 /*friend_p=*/NULL);
16804 /* We're done with the specialization. */
16805 end_specialization ();
16806 /* For the erroneous case of a template with C linkage, we pushed an
16807 implicit C++ linkage scope; exit that scope now. */
16808 if (need_lang_pop)
16809 pop_lang_context ();
16810 /* We're done with this parameter list. */
16811 --parser->num_template_parameter_lists;
16814 /* Parse a type-specifier.
16816 type-specifier:
16817 simple-type-specifier
16818 class-specifier
16819 enum-specifier
16820 elaborated-type-specifier
16821 cv-qualifier
16823 GNU Extension:
16825 type-specifier:
16826 __complex__
16828 Returns a representation of the type-specifier. For a
16829 class-specifier, enum-specifier, or elaborated-type-specifier, a
16830 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16832 The parser flags FLAGS is used to control type-specifier parsing.
16834 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16835 in a decl-specifier-seq.
16837 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16838 class-specifier, enum-specifier, or elaborated-type-specifier, then
16839 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16840 if a type is declared; 2 if it is defined. Otherwise, it is set to
16841 zero.
16843 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16844 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16845 is set to FALSE. */
16847 static tree
16848 cp_parser_type_specifier (cp_parser* parser,
16849 cp_parser_flags flags,
16850 cp_decl_specifier_seq *decl_specs,
16851 bool is_declaration,
16852 int* declares_class_or_enum,
16853 bool* is_cv_qualifier)
16855 tree type_spec = NULL_TREE;
16856 cp_token *token;
16857 enum rid keyword;
16858 cp_decl_spec ds = ds_last;
16860 /* Assume this type-specifier does not declare a new type. */
16861 if (declares_class_or_enum)
16862 *declares_class_or_enum = 0;
16863 /* And that it does not specify a cv-qualifier. */
16864 if (is_cv_qualifier)
16865 *is_cv_qualifier = false;
16866 /* Peek at the next token. */
16867 token = cp_lexer_peek_token (parser->lexer);
16869 /* If we're looking at a keyword, we can use that to guide the
16870 production we choose. */
16871 keyword = token->keyword;
16872 switch (keyword)
16874 case RID_ENUM:
16875 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16876 goto elaborated_type_specifier;
16878 /* Look for the enum-specifier. */
16879 type_spec = cp_parser_enum_specifier (parser);
16880 /* If that worked, we're done. */
16881 if (type_spec)
16883 if (declares_class_or_enum)
16884 *declares_class_or_enum = 2;
16885 if (decl_specs)
16886 cp_parser_set_decl_spec_type (decl_specs,
16887 type_spec,
16888 token,
16889 /*type_definition_p=*/true);
16890 return type_spec;
16892 else
16893 goto elaborated_type_specifier;
16895 /* Any of these indicate either a class-specifier, or an
16896 elaborated-type-specifier. */
16897 case RID_CLASS:
16898 case RID_STRUCT:
16899 case RID_UNION:
16900 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16901 goto elaborated_type_specifier;
16903 /* Parse tentatively so that we can back up if we don't find a
16904 class-specifier. */
16905 cp_parser_parse_tentatively (parser);
16906 /* Look for the class-specifier. */
16907 type_spec = cp_parser_class_specifier (parser);
16908 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16909 /* If that worked, we're done. */
16910 if (cp_parser_parse_definitely (parser))
16912 if (declares_class_or_enum)
16913 *declares_class_or_enum = 2;
16914 if (decl_specs)
16915 cp_parser_set_decl_spec_type (decl_specs,
16916 type_spec,
16917 token,
16918 /*type_definition_p=*/true);
16919 return type_spec;
16922 /* Fall through. */
16923 elaborated_type_specifier:
16924 /* We're declaring (not defining) a class or enum. */
16925 if (declares_class_or_enum)
16926 *declares_class_or_enum = 1;
16928 /* Fall through. */
16929 case RID_TYPENAME:
16930 /* Look for an elaborated-type-specifier. */
16931 type_spec
16932 = (cp_parser_elaborated_type_specifier
16933 (parser,
16934 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16935 is_declaration));
16936 if (decl_specs)
16937 cp_parser_set_decl_spec_type (decl_specs,
16938 type_spec,
16939 token,
16940 /*type_definition_p=*/false);
16941 return type_spec;
16943 case RID_CONST:
16944 ds = ds_const;
16945 if (is_cv_qualifier)
16946 *is_cv_qualifier = true;
16947 break;
16949 case RID_VOLATILE:
16950 ds = ds_volatile;
16951 if (is_cv_qualifier)
16952 *is_cv_qualifier = true;
16953 break;
16955 case RID_RESTRICT:
16956 ds = ds_restrict;
16957 if (is_cv_qualifier)
16958 *is_cv_qualifier = true;
16959 break;
16961 case RID_COMPLEX:
16962 /* The `__complex__' keyword is a GNU extension. */
16963 ds = ds_complex;
16964 break;
16966 default:
16967 break;
16970 /* Handle simple keywords. */
16971 if (ds != ds_last)
16973 if (decl_specs)
16975 set_and_check_decl_spec_loc (decl_specs, ds, token);
16976 decl_specs->any_specifiers_p = true;
16978 return cp_lexer_consume_token (parser->lexer)->u.value;
16981 /* If we do not already have a type-specifier, assume we are looking
16982 at a simple-type-specifier. */
16983 type_spec = cp_parser_simple_type_specifier (parser,
16984 decl_specs,
16985 flags);
16987 /* If we didn't find a type-specifier, and a type-specifier was not
16988 optional in this context, issue an error message. */
16989 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16991 cp_parser_error (parser, "expected type specifier");
16992 return error_mark_node;
16995 return type_spec;
16998 /* Parse a simple-type-specifier.
17000 simple-type-specifier:
17001 :: [opt] nested-name-specifier [opt] type-name
17002 :: [opt] nested-name-specifier template template-id
17003 char
17004 wchar_t
17005 bool
17006 short
17008 long
17009 signed
17010 unsigned
17011 float
17012 double
17013 void
17015 C++11 Extension:
17017 simple-type-specifier:
17018 auto
17019 decltype ( expression )
17020 char16_t
17021 char32_t
17022 __underlying_type ( type-id )
17024 C++17 extension:
17026 nested-name-specifier(opt) template-name
17028 GNU Extension:
17030 simple-type-specifier:
17031 __int128
17032 __typeof__ unary-expression
17033 __typeof__ ( type-id )
17034 __typeof__ ( type-id ) { initializer-list , [opt] }
17036 Concepts Extension:
17038 simple-type-specifier:
17039 constrained-type-specifier
17041 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17042 appropriately updated. */
17044 static tree
17045 cp_parser_simple_type_specifier (cp_parser* parser,
17046 cp_decl_specifier_seq *decl_specs,
17047 cp_parser_flags flags)
17049 tree type = NULL_TREE;
17050 cp_token *token;
17051 int idx;
17053 /* Peek at the next token. */
17054 token = cp_lexer_peek_token (parser->lexer);
17056 /* If we're looking at a keyword, things are easy. */
17057 switch (token->keyword)
17059 case RID_CHAR:
17060 if (decl_specs)
17061 decl_specs->explicit_char_p = true;
17062 type = char_type_node;
17063 break;
17064 case RID_CHAR16:
17065 type = char16_type_node;
17066 break;
17067 case RID_CHAR32:
17068 type = char32_type_node;
17069 break;
17070 case RID_WCHAR:
17071 type = wchar_type_node;
17072 break;
17073 case RID_BOOL:
17074 type = boolean_type_node;
17075 break;
17076 case RID_SHORT:
17077 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17078 type = short_integer_type_node;
17079 break;
17080 case RID_INT:
17081 if (decl_specs)
17082 decl_specs->explicit_int_p = true;
17083 type = integer_type_node;
17084 break;
17085 case RID_INT_N_0:
17086 case RID_INT_N_1:
17087 case RID_INT_N_2:
17088 case RID_INT_N_3:
17089 idx = token->keyword - RID_INT_N_0;
17090 if (! int_n_enabled_p [idx])
17091 break;
17092 if (decl_specs)
17094 decl_specs->explicit_intN_p = true;
17095 decl_specs->int_n_idx = idx;
17097 type = int_n_trees [idx].signed_type;
17098 break;
17099 case RID_LONG:
17100 if (decl_specs)
17101 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17102 type = long_integer_type_node;
17103 break;
17104 case RID_SIGNED:
17105 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17106 type = integer_type_node;
17107 break;
17108 case RID_UNSIGNED:
17109 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17110 type = unsigned_type_node;
17111 break;
17112 case RID_FLOAT:
17113 type = float_type_node;
17114 break;
17115 case RID_DOUBLE:
17116 type = double_type_node;
17117 break;
17118 case RID_VOID:
17119 type = void_type_node;
17120 break;
17122 case RID_AUTO:
17123 maybe_warn_cpp0x (CPP0X_AUTO);
17124 if (parser->auto_is_implicit_function_template_parm_p)
17126 /* The 'auto' might be the placeholder return type for a function decl
17127 with trailing return type. */
17128 bool have_trailing_return_fn_decl = false;
17130 cp_parser_parse_tentatively (parser);
17131 cp_lexer_consume_token (parser->lexer);
17132 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17133 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17134 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17135 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17137 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17139 cp_lexer_consume_token (parser->lexer);
17140 cp_parser_skip_to_closing_parenthesis (parser,
17141 /*recovering*/false,
17142 /*or_comma*/false,
17143 /*consume_paren*/true);
17144 continue;
17147 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17149 have_trailing_return_fn_decl = true;
17150 break;
17153 cp_lexer_consume_token (parser->lexer);
17155 cp_parser_abort_tentative_parse (parser);
17157 if (have_trailing_return_fn_decl)
17159 type = make_auto ();
17160 break;
17163 if (cxx_dialect >= cxx14)
17165 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17166 type = TREE_TYPE (type);
17168 else
17169 type = error_mark_node;
17171 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17173 if (cxx_dialect < cxx14)
17174 error_at (token->location,
17175 "use of %<auto%> in lambda parameter declaration "
17176 "only available with "
17177 "-std=c++14 or -std=gnu++14");
17179 else if (cxx_dialect < cxx14)
17180 error_at (token->location,
17181 "use of %<auto%> in parameter declaration "
17182 "only available with "
17183 "-std=c++14 or -std=gnu++14");
17184 else if (!flag_concepts)
17185 pedwarn (token->location, 0,
17186 "use of %<auto%> in parameter declaration "
17187 "only available with -fconcepts");
17189 else
17190 type = make_auto ();
17191 break;
17193 case RID_DECLTYPE:
17194 /* Since DR 743, decltype can either be a simple-type-specifier by
17195 itself or begin a nested-name-specifier. Parsing it will replace
17196 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17197 handling below decide what to do. */
17198 cp_parser_decltype (parser);
17199 cp_lexer_set_token_position (parser->lexer, token);
17200 break;
17202 case RID_TYPEOF:
17203 /* Consume the `typeof' token. */
17204 cp_lexer_consume_token (parser->lexer);
17205 /* Parse the operand to `typeof'. */
17206 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17207 /* If it is not already a TYPE, take its type. */
17208 if (!TYPE_P (type))
17209 type = finish_typeof (type);
17211 if (decl_specs)
17212 cp_parser_set_decl_spec_type (decl_specs, type,
17213 token,
17214 /*type_definition_p=*/false);
17216 return type;
17218 case RID_UNDERLYING_TYPE:
17219 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17220 if (decl_specs)
17221 cp_parser_set_decl_spec_type (decl_specs, type,
17222 token,
17223 /*type_definition_p=*/false);
17225 return type;
17227 case RID_BASES:
17228 case RID_DIRECT_BASES:
17229 type = cp_parser_trait_expr (parser, token->keyword);
17230 if (decl_specs)
17231 cp_parser_set_decl_spec_type (decl_specs, type,
17232 token,
17233 /*type_definition_p=*/false);
17234 return type;
17235 default:
17236 break;
17239 /* If token is an already-parsed decltype not followed by ::,
17240 it's a simple-type-specifier. */
17241 if (token->type == CPP_DECLTYPE
17242 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17244 type = saved_checks_value (token->u.tree_check_value);
17245 if (decl_specs)
17247 cp_parser_set_decl_spec_type (decl_specs, type,
17248 token,
17249 /*type_definition_p=*/false);
17250 /* Remember that we are handling a decltype in order to
17251 implement the resolution of DR 1510 when the argument
17252 isn't instantiation dependent. */
17253 decl_specs->decltype_p = true;
17255 cp_lexer_consume_token (parser->lexer);
17256 return type;
17259 /* If the type-specifier was for a built-in type, we're done. */
17260 if (type)
17262 /* Record the type. */
17263 if (decl_specs
17264 && (token->keyword != RID_SIGNED
17265 && token->keyword != RID_UNSIGNED
17266 && token->keyword != RID_SHORT
17267 && token->keyword != RID_LONG))
17268 cp_parser_set_decl_spec_type (decl_specs,
17269 type,
17270 token,
17271 /*type_definition_p=*/false);
17272 if (decl_specs)
17273 decl_specs->any_specifiers_p = true;
17275 /* Consume the token. */
17276 cp_lexer_consume_token (parser->lexer);
17278 if (type == error_mark_node)
17279 return error_mark_node;
17281 /* There is no valid C++ program where a non-template type is
17282 followed by a "<". That usually indicates that the user thought
17283 that the type was a template. */
17284 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17285 token->location);
17287 return TYPE_NAME (type);
17290 /* The type-specifier must be a user-defined type. */
17291 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17293 bool qualified_p;
17294 bool global_p;
17296 /* Don't gobble tokens or issue error messages if this is an
17297 optional type-specifier. */
17298 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17299 cp_parser_parse_tentatively (parser);
17301 token = cp_lexer_peek_token (parser->lexer);
17303 /* Look for the optional `::' operator. */
17304 global_p
17305 = (cp_parser_global_scope_opt (parser,
17306 /*current_scope_valid_p=*/false)
17307 != NULL_TREE);
17308 /* Look for the nested-name specifier. */
17309 qualified_p
17310 = (cp_parser_nested_name_specifier_opt (parser,
17311 /*typename_keyword_p=*/false,
17312 /*check_dependency_p=*/true,
17313 /*type_p=*/false,
17314 /*is_declaration=*/false)
17315 != NULL_TREE);
17316 /* If we have seen a nested-name-specifier, and the next token
17317 is `template', then we are using the template-id production. */
17318 if (parser->scope
17319 && cp_parser_optional_template_keyword (parser))
17321 /* Look for the template-id. */
17322 type = cp_parser_template_id (parser,
17323 /*template_keyword_p=*/true,
17324 /*check_dependency_p=*/true,
17325 none_type,
17326 /*is_declaration=*/false);
17327 /* If the template-id did not name a type, we are out of
17328 luck. */
17329 if (TREE_CODE (type) != TYPE_DECL)
17331 cp_parser_error (parser, "expected template-id for type");
17332 type = NULL_TREE;
17335 /* Otherwise, look for a type-name. */
17336 else
17337 type = cp_parser_type_name (parser);
17338 /* Keep track of all name-lookups performed in class scopes. */
17339 if (type
17340 && !global_p
17341 && !qualified_p
17342 && TREE_CODE (type) == TYPE_DECL
17343 && identifier_p (DECL_NAME (type)))
17344 maybe_note_name_used_in_class (DECL_NAME (type), type);
17345 /* If it didn't work out, we don't have a TYPE. */
17346 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17347 && !cp_parser_parse_definitely (parser))
17348 type = NULL_TREE;
17349 if (!type && cxx_dialect >= cxx17)
17351 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17352 cp_parser_parse_tentatively (parser);
17354 cp_parser_global_scope_opt (parser,
17355 /*current_scope_valid_p=*/false);
17356 cp_parser_nested_name_specifier_opt (parser,
17357 /*typename_keyword_p=*/false,
17358 /*check_dependency_p=*/true,
17359 /*type_p=*/false,
17360 /*is_declaration=*/false);
17361 tree name = cp_parser_identifier (parser);
17362 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17363 && parser->scope != error_mark_node)
17365 tree tmpl = cp_parser_lookup_name (parser, name,
17366 none_type,
17367 /*is_template=*/false,
17368 /*is_namespace=*/false,
17369 /*check_dependency=*/true,
17370 /*ambiguous_decls=*/NULL,
17371 token->location);
17372 if (tmpl && tmpl != error_mark_node
17373 && (DECL_CLASS_TEMPLATE_P (tmpl)
17374 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17375 type = make_template_placeholder (tmpl);
17376 else
17378 type = error_mark_node;
17379 if (!cp_parser_simulate_error (parser))
17380 cp_parser_name_lookup_error (parser, name, tmpl,
17381 NLE_TYPE, token->location);
17384 else
17385 type = error_mark_node;
17387 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17388 && !cp_parser_parse_definitely (parser))
17389 type = NULL_TREE;
17391 if (type && decl_specs)
17392 cp_parser_set_decl_spec_type (decl_specs, type,
17393 token,
17394 /*type_definition_p=*/false);
17397 /* If we didn't get a type-name, issue an error message. */
17398 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17400 cp_parser_error (parser, "expected type-name");
17401 return error_mark_node;
17404 if (type && type != error_mark_node)
17406 /* See if TYPE is an Objective-C type, and if so, parse and
17407 accept any protocol references following it. Do this before
17408 the cp_parser_check_for_invalid_template_id() call, because
17409 Objective-C types can be followed by '<...>' which would
17410 enclose protocol names rather than template arguments, and so
17411 everything is fine. */
17412 if (c_dialect_objc () && !parser->scope
17413 && (objc_is_id (type) || objc_is_class_name (type)))
17415 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17416 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17418 /* Clobber the "unqualified" type previously entered into
17419 DECL_SPECS with the new, improved protocol-qualified version. */
17420 if (decl_specs)
17421 decl_specs->type = qual_type;
17423 return qual_type;
17426 /* There is no valid C++ program where a non-template type is
17427 followed by a "<". That usually indicates that the user
17428 thought that the type was a template. */
17429 cp_parser_check_for_invalid_template_id (parser, type,
17430 none_type,
17431 token->location);
17434 return type;
17437 /* Parse a type-name.
17439 type-name:
17440 class-name
17441 enum-name
17442 typedef-name
17443 simple-template-id [in c++0x]
17445 enum-name:
17446 identifier
17448 typedef-name:
17449 identifier
17451 Concepts:
17453 type-name:
17454 concept-name
17455 partial-concept-id
17457 concept-name:
17458 identifier
17460 Returns a TYPE_DECL for the type. */
17462 static tree
17463 cp_parser_type_name (cp_parser* parser)
17465 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17468 /* See above. */
17469 static tree
17470 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17472 tree type_decl;
17474 /* We can't know yet whether it is a class-name or not. */
17475 cp_parser_parse_tentatively (parser);
17476 /* Try a class-name. */
17477 type_decl = cp_parser_class_name (parser,
17478 typename_keyword_p,
17479 /*template_keyword_p=*/false,
17480 none_type,
17481 /*check_dependency_p=*/true,
17482 /*class_head_p=*/false,
17483 /*is_declaration=*/false);
17484 /* If it's not a class-name, keep looking. */
17485 if (!cp_parser_parse_definitely (parser))
17487 if (cxx_dialect < cxx11)
17488 /* It must be a typedef-name or an enum-name. */
17489 return cp_parser_nonclass_name (parser);
17491 cp_parser_parse_tentatively (parser);
17492 /* It is either a simple-template-id representing an
17493 instantiation of an alias template... */
17494 type_decl = cp_parser_template_id (parser,
17495 /*template_keyword_p=*/false,
17496 /*check_dependency_p=*/true,
17497 none_type,
17498 /*is_declaration=*/false);
17499 /* Note that this must be an instantiation of an alias template
17500 because [temp.names]/6 says:
17502 A template-id that names an alias template specialization
17503 is a type-name.
17505 Whereas [temp.names]/7 says:
17507 A simple-template-id that names a class template
17508 specialization is a class-name.
17510 With concepts, this could also be a partial-concept-id that
17511 declares a non-type template parameter. */
17512 if (type_decl != NULL_TREE
17513 && TREE_CODE (type_decl) == TYPE_DECL
17514 && TYPE_DECL_ALIAS_P (type_decl))
17515 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17516 else if (is_constrained_parameter (type_decl))
17517 /* Don't do anything. */ ;
17518 else
17519 cp_parser_simulate_error (parser);
17521 if (!cp_parser_parse_definitely (parser))
17522 /* ... Or a typedef-name or an enum-name. */
17523 return cp_parser_nonclass_name (parser);
17526 return type_decl;
17529 /* Check if DECL and ARGS can form a constrained-type-specifier.
17530 If ARGS is non-null, we try to form a concept check of the
17531 form DECL<?, ARGS> where ? is a wildcard that matches any
17532 kind of template argument. If ARGS is NULL, then we try to
17533 form a concept check of the form DECL<?>. */
17535 static tree
17536 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17537 tree decl, tree args)
17539 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17541 /* If we a constrained-type-specifier cannot be deduced. */
17542 if (parser->prevent_constrained_type_specifiers)
17543 return NULL_TREE;
17545 /* A constrained type specifier can only be found in an
17546 overload set or as a reference to a template declaration.
17548 FIXME: This might be masking a bug. It's possible that
17549 that the deduction below is causing template specializations
17550 to be formed with the wildcard as an argument. */
17551 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17552 return NULL_TREE;
17554 /* Try to build a call expression that evaluates the
17555 concept. This can fail if the overload set refers
17556 only to non-templates. */
17557 tree placeholder = build_nt (WILDCARD_DECL);
17558 tree check = build_concept_check (decl, placeholder, args);
17559 if (check == error_mark_node)
17560 return NULL_TREE;
17562 /* Deduce the checked constraint and the prototype parameter.
17564 FIXME: In certain cases, failure to deduce should be a
17565 diagnosable error. */
17566 tree conc;
17567 tree proto;
17568 if (!deduce_constrained_parameter (check, conc, proto))
17569 return NULL_TREE;
17571 /* In template parameter scope, this results in a constrained
17572 parameter. Return a descriptor of that parm. */
17573 if (processing_template_parmlist)
17574 return build_constrained_parameter (conc, proto, args);
17576 /* In a parameter-declaration-clause, constrained-type
17577 specifiers result in invented template parameters. */
17578 if (parser->auto_is_implicit_function_template_parm_p)
17580 tree x = build_constrained_parameter (conc, proto, args);
17581 return synthesize_implicit_template_parm (parser, x);
17583 else
17585 /* Otherwise, we're in a context where the constrained
17586 type name is deduced and the constraint applies
17587 after deduction. */
17588 return make_constrained_auto (conc, args);
17591 return NULL_TREE;
17594 /* If DECL refers to a concept, return a TYPE_DECL representing
17595 the result of using the constrained type specifier in the
17596 current context. DECL refers to a concept if
17598 - it is an overload set containing a function concept taking a single
17599 type argument, or
17601 - it is a variable concept taking a single type argument. */
17603 static tree
17604 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17606 if (flag_concepts
17607 && (TREE_CODE (decl) == OVERLOAD
17608 || BASELINK_P (decl)
17609 || variable_concept_p (decl)))
17610 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17611 else
17612 return NULL_TREE;
17615 /* Check if DECL and ARGS form a partial-concept-id. If so,
17616 assign ID to the resulting constrained placeholder.
17618 Returns true if the partial-concept-id designates a placeholder
17619 and false otherwise. Note that *id is set to NULL_TREE in
17620 this case. */
17622 static tree
17623 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17625 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17628 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17629 or a concept-name.
17631 enum-name:
17632 identifier
17634 typedef-name:
17635 identifier
17637 concept-name:
17638 identifier
17640 Returns a TYPE_DECL for the type. */
17642 static tree
17643 cp_parser_nonclass_name (cp_parser* parser)
17645 tree type_decl;
17646 tree identifier;
17648 cp_token *token = cp_lexer_peek_token (parser->lexer);
17649 identifier = cp_parser_identifier (parser);
17650 if (identifier == error_mark_node)
17651 return error_mark_node;
17653 /* Look up the type-name. */
17654 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17656 type_decl = strip_using_decl (type_decl);
17658 /* If we found an overload set, then it may refer to a concept-name. */
17659 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17660 type_decl = decl;
17662 if (TREE_CODE (type_decl) != TYPE_DECL
17663 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17665 /* See if this is an Objective-C type. */
17666 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17667 tree type = objc_get_protocol_qualified_type (identifier, protos);
17668 if (type)
17669 type_decl = TYPE_NAME (type);
17672 /* Issue an error if we did not find a type-name. */
17673 if (TREE_CODE (type_decl) != TYPE_DECL
17674 /* In Objective-C, we have the complication that class names are
17675 normally type names and start declarations (eg, the
17676 "NSObject" in "NSObject *object;"), but can be used in an
17677 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17678 is an expression. So, a classname followed by a dot is not a
17679 valid type-name. */
17680 || (objc_is_class_name (TREE_TYPE (type_decl))
17681 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17683 if (!cp_parser_simulate_error (parser))
17684 cp_parser_name_lookup_error (parser, identifier, type_decl,
17685 NLE_TYPE, token->location);
17686 return error_mark_node;
17688 /* Remember that the name was used in the definition of the
17689 current class so that we can check later to see if the
17690 meaning would have been different after the class was
17691 entirely defined. */
17692 else if (type_decl != error_mark_node
17693 && !parser->scope)
17694 maybe_note_name_used_in_class (identifier, type_decl);
17696 return type_decl;
17699 /* Parse an elaborated-type-specifier. Note that the grammar given
17700 here incorporates the resolution to DR68.
17702 elaborated-type-specifier:
17703 class-key :: [opt] nested-name-specifier [opt] identifier
17704 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17705 enum-key :: [opt] nested-name-specifier [opt] identifier
17706 typename :: [opt] nested-name-specifier identifier
17707 typename :: [opt] nested-name-specifier template [opt]
17708 template-id
17710 GNU extension:
17712 elaborated-type-specifier:
17713 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17714 class-key attributes :: [opt] nested-name-specifier [opt]
17715 template [opt] template-id
17716 enum attributes :: [opt] nested-name-specifier [opt] identifier
17718 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17719 declared `friend'. If IS_DECLARATION is TRUE, then this
17720 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17721 something is being declared.
17723 Returns the TYPE specified. */
17725 static tree
17726 cp_parser_elaborated_type_specifier (cp_parser* parser,
17727 bool is_friend,
17728 bool is_declaration)
17730 enum tag_types tag_type;
17731 tree identifier;
17732 tree type = NULL_TREE;
17733 tree attributes = NULL_TREE;
17734 tree globalscope;
17735 cp_token *token = NULL;
17737 /* See if we're looking at the `enum' keyword. */
17738 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17740 /* Consume the `enum' token. */
17741 cp_lexer_consume_token (parser->lexer);
17742 /* Remember that it's an enumeration type. */
17743 tag_type = enum_type;
17744 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17745 enums) is used here. */
17746 cp_token *token = cp_lexer_peek_token (parser->lexer);
17747 if (cp_parser_is_keyword (token, RID_CLASS)
17748 || cp_parser_is_keyword (token, RID_STRUCT))
17750 gcc_rich_location richloc (token->location);
17751 richloc.add_range (input_location, false);
17752 richloc.add_fixit_remove ();
17753 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17754 "a scoped enum must not use the %qD keyword",
17755 token->u.value);
17756 /* Consume the `struct' or `class' and parse it anyway. */
17757 cp_lexer_consume_token (parser->lexer);
17759 /* Parse the attributes. */
17760 attributes = cp_parser_attributes_opt (parser);
17762 /* Or, it might be `typename'. */
17763 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17764 RID_TYPENAME))
17766 /* Consume the `typename' token. */
17767 cp_lexer_consume_token (parser->lexer);
17768 /* Remember that it's a `typename' type. */
17769 tag_type = typename_type;
17771 /* Otherwise it must be a class-key. */
17772 else
17774 tag_type = cp_parser_class_key (parser);
17775 if (tag_type == none_type)
17776 return error_mark_node;
17777 /* Parse the attributes. */
17778 attributes = cp_parser_attributes_opt (parser);
17781 /* Look for the `::' operator. */
17782 globalscope = cp_parser_global_scope_opt (parser,
17783 /*current_scope_valid_p=*/false);
17784 /* Look for the nested-name-specifier. */
17785 tree nested_name_specifier;
17786 if (tag_type == typename_type && !globalscope)
17788 nested_name_specifier
17789 = cp_parser_nested_name_specifier (parser,
17790 /*typename_keyword_p=*/true,
17791 /*check_dependency_p=*/true,
17792 /*type_p=*/true,
17793 is_declaration);
17794 if (!nested_name_specifier)
17795 return error_mark_node;
17797 else
17798 /* Even though `typename' is not present, the proposed resolution
17799 to Core Issue 180 says that in `class A<T>::B', `B' should be
17800 considered a type-name, even if `A<T>' is dependent. */
17801 nested_name_specifier
17802 = cp_parser_nested_name_specifier_opt (parser,
17803 /*typename_keyword_p=*/true,
17804 /*check_dependency_p=*/true,
17805 /*type_p=*/true,
17806 is_declaration);
17807 /* For everything but enumeration types, consider a template-id.
17808 For an enumeration type, consider only a plain identifier. */
17809 if (tag_type != enum_type)
17811 bool template_p = false;
17812 tree decl;
17814 /* Allow the `template' keyword. */
17815 template_p = cp_parser_optional_template_keyword (parser);
17816 /* If we didn't see `template', we don't know if there's a
17817 template-id or not. */
17818 if (!template_p)
17819 cp_parser_parse_tentatively (parser);
17820 /* Parse the template-id. */
17821 token = cp_lexer_peek_token (parser->lexer);
17822 decl = cp_parser_template_id (parser, template_p,
17823 /*check_dependency_p=*/true,
17824 tag_type,
17825 is_declaration);
17826 /* If we didn't find a template-id, look for an ordinary
17827 identifier. */
17828 if (!template_p && !cp_parser_parse_definitely (parser))
17830 /* We can get here when cp_parser_template_id, called by
17831 cp_parser_class_name with tag_type == none_type, succeeds
17832 and caches a BASELINK. Then, when called again here,
17833 instead of failing and returning an error_mark_node
17834 returns it (see template/typename17.C in C++11).
17835 ??? Could we diagnose this earlier? */
17836 else if (tag_type == typename_type && BASELINK_P (decl))
17838 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17839 type = error_mark_node;
17841 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17842 in effect, then we must assume that, upon instantiation, the
17843 template will correspond to a class. */
17844 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17845 && tag_type == typename_type)
17846 type = make_typename_type (parser->scope, decl,
17847 typename_type,
17848 /*complain=*/tf_error);
17849 /* If the `typename' keyword is in effect and DECL is not a type
17850 decl, then type is non existent. */
17851 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17853 else if (TREE_CODE (decl) == TYPE_DECL)
17855 type = check_elaborated_type_specifier (tag_type, decl,
17856 /*allow_template_p=*/true);
17858 /* If the next token is a semicolon, this must be a specialization,
17859 instantiation, or friend declaration. Check the scope while we
17860 still know whether or not we had a nested-name-specifier. */
17861 if (type != error_mark_node
17862 && !nested_name_specifier && !is_friend
17863 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17864 check_unqualified_spec_or_inst (type, token->location);
17866 else if (decl == error_mark_node)
17867 type = error_mark_node;
17870 if (!type)
17872 token = cp_lexer_peek_token (parser->lexer);
17873 identifier = cp_parser_identifier (parser);
17875 if (identifier == error_mark_node)
17877 parser->scope = NULL_TREE;
17878 return error_mark_node;
17881 /* For a `typename', we needn't call xref_tag. */
17882 if (tag_type == typename_type
17883 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17884 return cp_parser_make_typename_type (parser, identifier,
17885 token->location);
17887 /* Template parameter lists apply only if we are not within a
17888 function parameter list. */
17889 bool template_parm_lists_apply
17890 = parser->num_template_parameter_lists;
17891 if (template_parm_lists_apply)
17892 for (cp_binding_level *s = current_binding_level;
17893 s && s->kind != sk_template_parms;
17894 s = s->level_chain)
17895 if (s->kind == sk_function_parms)
17896 template_parm_lists_apply = false;
17898 /* Look up a qualified name in the usual way. */
17899 if (parser->scope)
17901 tree decl;
17902 tree ambiguous_decls;
17904 decl = cp_parser_lookup_name (parser, identifier,
17905 tag_type,
17906 /*is_template=*/false,
17907 /*is_namespace=*/false,
17908 /*check_dependency=*/true,
17909 &ambiguous_decls,
17910 token->location);
17912 /* If the lookup was ambiguous, an error will already have been
17913 issued. */
17914 if (ambiguous_decls)
17915 return error_mark_node;
17917 /* If we are parsing friend declaration, DECL may be a
17918 TEMPLATE_DECL tree node here. However, we need to check
17919 whether this TEMPLATE_DECL results in valid code. Consider
17920 the following example:
17922 namespace N {
17923 template <class T> class C {};
17925 class X {
17926 template <class T> friend class N::C; // #1, valid code
17928 template <class T> class Y {
17929 friend class N::C; // #2, invalid code
17932 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17933 name lookup of `N::C'. We see that friend declaration must
17934 be template for the code to be valid. Note that
17935 processing_template_decl does not work here since it is
17936 always 1 for the above two cases. */
17938 decl = (cp_parser_maybe_treat_template_as_class
17939 (decl, /*tag_name_p=*/is_friend
17940 && template_parm_lists_apply));
17942 if (TREE_CODE (decl) != TYPE_DECL)
17944 cp_parser_diagnose_invalid_type_name (parser,
17945 identifier,
17946 token->location);
17947 return error_mark_node;
17950 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17952 bool allow_template = (template_parm_lists_apply
17953 || DECL_SELF_REFERENCE_P (decl));
17954 type = check_elaborated_type_specifier (tag_type, decl,
17955 allow_template);
17957 if (type == error_mark_node)
17958 return error_mark_node;
17961 /* Forward declarations of nested types, such as
17963 class C1::C2;
17964 class C1::C2::C3;
17966 are invalid unless all components preceding the final '::'
17967 are complete. If all enclosing types are complete, these
17968 declarations become merely pointless.
17970 Invalid forward declarations of nested types are errors
17971 caught elsewhere in parsing. Those that are pointless arrive
17972 here. */
17974 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17975 && !is_friend && !processing_explicit_instantiation)
17976 warning (0, "declaration %qD does not declare anything", decl);
17978 type = TREE_TYPE (decl);
17980 else
17982 /* An elaborated-type-specifier sometimes introduces a new type and
17983 sometimes names an existing type. Normally, the rule is that it
17984 introduces a new type only if there is not an existing type of
17985 the same name already in scope. For example, given:
17987 struct S {};
17988 void f() { struct S s; }
17990 the `struct S' in the body of `f' is the same `struct S' as in
17991 the global scope; the existing definition is used. However, if
17992 there were no global declaration, this would introduce a new
17993 local class named `S'.
17995 An exception to this rule applies to the following code:
17997 namespace N { struct S; }
17999 Here, the elaborated-type-specifier names a new type
18000 unconditionally; even if there is already an `S' in the
18001 containing scope this declaration names a new type.
18002 This exception only applies if the elaborated-type-specifier
18003 forms the complete declaration:
18005 [class.name]
18007 A declaration consisting solely of `class-key identifier ;' is
18008 either a redeclaration of the name in the current scope or a
18009 forward declaration of the identifier as a class name. It
18010 introduces the name into the current scope.
18012 We are in this situation precisely when the next token is a `;'.
18014 An exception to the exception is that a `friend' declaration does
18015 *not* name a new type; i.e., given:
18017 struct S { friend struct T; };
18019 `T' is not a new type in the scope of `S'.
18021 Also, `new struct S' or `sizeof (struct S)' never results in the
18022 definition of a new type; a new type can only be declared in a
18023 declaration context. */
18025 tag_scope ts;
18026 bool template_p;
18028 if (is_friend)
18029 /* Friends have special name lookup rules. */
18030 ts = ts_within_enclosing_non_class;
18031 else if (is_declaration
18032 && cp_lexer_next_token_is (parser->lexer,
18033 CPP_SEMICOLON))
18034 /* This is a `class-key identifier ;' */
18035 ts = ts_current;
18036 else
18037 ts = ts_global;
18039 template_p =
18040 (template_parm_lists_apply
18041 && (cp_parser_next_token_starts_class_definition_p (parser)
18042 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18043 /* An unqualified name was used to reference this type, so
18044 there were no qualifying templates. */
18045 if (template_parm_lists_apply
18046 && !cp_parser_check_template_parameters (parser,
18047 /*num_templates=*/0,
18048 /*template_id*/false,
18049 token->location,
18050 /*declarator=*/NULL))
18051 return error_mark_node;
18052 type = xref_tag (tag_type, identifier, ts, template_p);
18056 if (type == error_mark_node)
18057 return error_mark_node;
18059 /* Allow attributes on forward declarations of classes. */
18060 if (attributes)
18062 if (TREE_CODE (type) == TYPENAME_TYPE)
18063 warning (OPT_Wattributes,
18064 "attributes ignored on uninstantiated type");
18065 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18066 && ! processing_explicit_instantiation)
18067 warning (OPT_Wattributes,
18068 "attributes ignored on template instantiation");
18069 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18070 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18071 else
18072 warning (OPT_Wattributes,
18073 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18076 if (tag_type != enum_type)
18078 /* Indicate whether this class was declared as a `class' or as a
18079 `struct'. */
18080 if (CLASS_TYPE_P (type))
18081 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18082 cp_parser_check_class_key (tag_type, type);
18085 /* A "<" cannot follow an elaborated type specifier. If that
18086 happens, the user was probably trying to form a template-id. */
18087 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18088 token->location);
18090 return type;
18093 /* Parse an enum-specifier.
18095 enum-specifier:
18096 enum-head { enumerator-list [opt] }
18097 enum-head { enumerator-list , } [C++0x]
18099 enum-head:
18100 enum-key identifier [opt] enum-base [opt]
18101 enum-key nested-name-specifier identifier enum-base [opt]
18103 enum-key:
18104 enum
18105 enum class [C++0x]
18106 enum struct [C++0x]
18108 enum-base: [C++0x]
18109 : type-specifier-seq
18111 opaque-enum-specifier:
18112 enum-key identifier enum-base [opt] ;
18114 GNU Extensions:
18115 enum-key attributes[opt] identifier [opt] enum-base [opt]
18116 { enumerator-list [opt] }attributes[opt]
18117 enum-key attributes[opt] identifier [opt] enum-base [opt]
18118 { enumerator-list, }attributes[opt] [C++0x]
18120 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18121 if the token stream isn't an enum-specifier after all. */
18123 static tree
18124 cp_parser_enum_specifier (cp_parser* parser)
18126 tree identifier;
18127 tree type = NULL_TREE;
18128 tree prev_scope;
18129 tree nested_name_specifier = NULL_TREE;
18130 tree attributes;
18131 bool scoped_enum_p = false;
18132 bool has_underlying_type = false;
18133 bool nested_being_defined = false;
18134 bool new_value_list = false;
18135 bool is_new_type = false;
18136 bool is_unnamed = false;
18137 tree underlying_type = NULL_TREE;
18138 cp_token *type_start_token = NULL;
18139 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18141 parser->colon_corrects_to_scope_p = false;
18143 /* Parse tentatively so that we can back up if we don't find a
18144 enum-specifier. */
18145 cp_parser_parse_tentatively (parser);
18147 /* Caller guarantees that the current token is 'enum', an identifier
18148 possibly follows, and the token after that is an opening brace.
18149 If we don't have an identifier, fabricate an anonymous name for
18150 the enumeration being defined. */
18151 cp_lexer_consume_token (parser->lexer);
18153 /* Parse the "class" or "struct", which indicates a scoped
18154 enumeration type in C++0x. */
18155 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18156 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18158 if (cxx_dialect < cxx11)
18159 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18161 /* Consume the `struct' or `class' token. */
18162 cp_lexer_consume_token (parser->lexer);
18164 scoped_enum_p = true;
18167 attributes = cp_parser_attributes_opt (parser);
18169 /* Clear the qualification. */
18170 parser->scope = NULL_TREE;
18171 parser->qualifying_scope = NULL_TREE;
18172 parser->object_scope = NULL_TREE;
18174 /* Figure out in what scope the declaration is being placed. */
18175 prev_scope = current_scope ();
18177 type_start_token = cp_lexer_peek_token (parser->lexer);
18179 push_deferring_access_checks (dk_no_check);
18180 nested_name_specifier
18181 = cp_parser_nested_name_specifier_opt (parser,
18182 /*typename_keyword_p=*/true,
18183 /*check_dependency_p=*/false,
18184 /*type_p=*/false,
18185 /*is_declaration=*/false);
18187 if (nested_name_specifier)
18189 tree name;
18191 identifier = cp_parser_identifier (parser);
18192 name = cp_parser_lookup_name (parser, identifier,
18193 enum_type,
18194 /*is_template=*/false,
18195 /*is_namespace=*/false,
18196 /*check_dependency=*/true,
18197 /*ambiguous_decls=*/NULL,
18198 input_location);
18199 if (name && name != error_mark_node)
18201 type = TREE_TYPE (name);
18202 if (TREE_CODE (type) == TYPENAME_TYPE)
18204 /* Are template enums allowed in ISO? */
18205 if (template_parm_scope_p ())
18206 pedwarn (type_start_token->location, OPT_Wpedantic,
18207 "%qD is an enumeration template", name);
18208 /* ignore a typename reference, for it will be solved by name
18209 in start_enum. */
18210 type = NULL_TREE;
18213 else if (nested_name_specifier == error_mark_node)
18214 /* We already issued an error. */;
18215 else
18217 error_at (type_start_token->location,
18218 "%qD does not name an enumeration in %qT",
18219 identifier, nested_name_specifier);
18220 nested_name_specifier = error_mark_node;
18223 else
18225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18226 identifier = cp_parser_identifier (parser);
18227 else
18229 identifier = make_anon_name ();
18230 is_unnamed = true;
18231 if (scoped_enum_p)
18232 error_at (type_start_token->location,
18233 "unnamed scoped enum is not allowed");
18236 pop_deferring_access_checks ();
18238 /* Check for the `:' that denotes a specified underlying type in C++0x.
18239 Note that a ':' could also indicate a bitfield width, however. */
18240 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18242 cp_decl_specifier_seq type_specifiers;
18244 /* Consume the `:'. */
18245 cp_lexer_consume_token (parser->lexer);
18247 /* Parse the type-specifier-seq. */
18248 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18249 /*is_trailing_return=*/false,
18250 &type_specifiers);
18252 /* At this point this is surely not elaborated type specifier. */
18253 if (!cp_parser_parse_definitely (parser))
18254 return NULL_TREE;
18256 if (cxx_dialect < cxx11)
18257 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18259 has_underlying_type = true;
18261 /* If that didn't work, stop. */
18262 if (type_specifiers.type != error_mark_node)
18264 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18265 /*initialized=*/0, NULL);
18266 if (underlying_type == error_mark_node
18267 || check_for_bare_parameter_packs (underlying_type))
18268 underlying_type = NULL_TREE;
18272 /* Look for the `{' but don't consume it yet. */
18273 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18275 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18277 cp_parser_error (parser, "expected %<{%>");
18278 if (has_underlying_type)
18280 type = NULL_TREE;
18281 goto out;
18284 /* An opaque-enum-specifier must have a ';' here. */
18285 if ((scoped_enum_p || underlying_type)
18286 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18288 cp_parser_error (parser, "expected %<;%> or %<{%>");
18289 if (has_underlying_type)
18291 type = NULL_TREE;
18292 goto out;
18297 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18298 return NULL_TREE;
18300 if (nested_name_specifier)
18302 if (CLASS_TYPE_P (nested_name_specifier))
18304 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18305 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18306 push_scope (nested_name_specifier);
18308 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18310 push_nested_namespace (nested_name_specifier);
18314 /* Issue an error message if type-definitions are forbidden here. */
18315 if (!cp_parser_check_type_definition (parser))
18316 type = error_mark_node;
18317 else
18318 /* Create the new type. We do this before consuming the opening
18319 brace so the enum will be recorded as being on the line of its
18320 tag (or the 'enum' keyword, if there is no tag). */
18321 type = start_enum (identifier, type, underlying_type,
18322 attributes, scoped_enum_p, &is_new_type);
18324 /* If the next token is not '{' it is an opaque-enum-specifier or an
18325 elaborated-type-specifier. */
18326 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18328 timevar_push (TV_PARSE_ENUM);
18329 if (nested_name_specifier
18330 && nested_name_specifier != error_mark_node)
18332 /* The following catches invalid code such as:
18333 enum class S<int>::E { A, B, C }; */
18334 if (!processing_specialization
18335 && CLASS_TYPE_P (nested_name_specifier)
18336 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18337 error_at (type_start_token->location, "cannot add an enumerator "
18338 "list to a template instantiation");
18340 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18342 error_at (type_start_token->location,
18343 "%<%T::%E%> has not been declared",
18344 TYPE_CONTEXT (nested_name_specifier),
18345 nested_name_specifier);
18346 type = error_mark_node;
18348 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18349 && !CLASS_TYPE_P (nested_name_specifier))
18351 error_at (type_start_token->location, "nested name specifier "
18352 "%qT for enum declaration does not name a class "
18353 "or namespace", nested_name_specifier);
18354 type = error_mark_node;
18356 /* If that scope does not contain the scope in which the
18357 class was originally declared, the program is invalid. */
18358 else if (prev_scope && !is_ancestor (prev_scope,
18359 nested_name_specifier))
18361 if (at_namespace_scope_p ())
18362 error_at (type_start_token->location,
18363 "declaration of %qD in namespace %qD which does not "
18364 "enclose %qD",
18365 type, prev_scope, nested_name_specifier);
18366 else
18367 error_at (type_start_token->location,
18368 "declaration of %qD in %qD which does not "
18369 "enclose %qD",
18370 type, prev_scope, nested_name_specifier);
18371 type = error_mark_node;
18373 /* If that scope is the scope where the declaration is being placed
18374 the program is invalid. */
18375 else if (CLASS_TYPE_P (nested_name_specifier)
18376 && CLASS_TYPE_P (prev_scope)
18377 && same_type_p (nested_name_specifier, prev_scope))
18379 permerror (type_start_token->location,
18380 "extra qualification not allowed");
18381 nested_name_specifier = NULL_TREE;
18385 if (scoped_enum_p)
18386 begin_scope (sk_scoped_enum, type);
18388 /* Consume the opening brace. */
18389 matching_braces braces;
18390 braces.consume_open (parser);
18392 if (type == error_mark_node)
18393 ; /* Nothing to add */
18394 else if (OPAQUE_ENUM_P (type)
18395 || (cxx_dialect > cxx98 && processing_specialization))
18397 new_value_list = true;
18398 SET_OPAQUE_ENUM_P (type, false);
18399 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18401 else
18403 error_at (type_start_token->location,
18404 "multiple definition of %q#T", type);
18405 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18406 "previous definition here");
18407 type = error_mark_node;
18410 if (type == error_mark_node)
18411 cp_parser_skip_to_end_of_block_or_statement (parser);
18412 /* If the next token is not '}', then there are some enumerators. */
18413 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18415 if (is_unnamed && !scoped_enum_p)
18416 pedwarn (type_start_token->location, OPT_Wpedantic,
18417 "ISO C++ forbids empty unnamed enum");
18419 else
18420 cp_parser_enumerator_list (parser, type);
18422 /* Consume the final '}'. */
18423 braces.require_close (parser);
18425 if (scoped_enum_p)
18426 finish_scope ();
18427 timevar_pop (TV_PARSE_ENUM);
18429 else
18431 /* If a ';' follows, then it is an opaque-enum-specifier
18432 and additional restrictions apply. */
18433 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18435 if (is_unnamed)
18436 error_at (type_start_token->location,
18437 "opaque-enum-specifier without name");
18438 else if (nested_name_specifier)
18439 error_at (type_start_token->location,
18440 "opaque-enum-specifier must use a simple identifier");
18444 /* Look for trailing attributes to apply to this enumeration, and
18445 apply them if appropriate. */
18446 if (cp_parser_allow_gnu_extensions_p (parser))
18448 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18449 cplus_decl_attributes (&type,
18450 trailing_attr,
18451 (int) ATTR_FLAG_TYPE_IN_PLACE);
18454 /* Finish up the enumeration. */
18455 if (type != error_mark_node)
18457 if (new_value_list)
18458 finish_enum_value_list (type);
18459 if (is_new_type)
18460 finish_enum (type);
18463 if (nested_name_specifier)
18465 if (CLASS_TYPE_P (nested_name_specifier))
18467 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18468 pop_scope (nested_name_specifier);
18470 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18472 pop_nested_namespace (nested_name_specifier);
18475 out:
18476 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18477 return type;
18480 /* Parse an enumerator-list. The enumerators all have the indicated
18481 TYPE.
18483 enumerator-list:
18484 enumerator-definition
18485 enumerator-list , enumerator-definition */
18487 static void
18488 cp_parser_enumerator_list (cp_parser* parser, tree type)
18490 while (true)
18492 /* Parse an enumerator-definition. */
18493 cp_parser_enumerator_definition (parser, type);
18495 /* If the next token is not a ',', we've reached the end of
18496 the list. */
18497 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18498 break;
18499 /* Otherwise, consume the `,' and keep going. */
18500 cp_lexer_consume_token (parser->lexer);
18501 /* If the next token is a `}', there is a trailing comma. */
18502 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18504 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18505 pedwarn (input_location, OPT_Wpedantic,
18506 "comma at end of enumerator list");
18507 break;
18512 /* Parse an enumerator-definition. The enumerator has the indicated
18513 TYPE.
18515 enumerator-definition:
18516 enumerator
18517 enumerator = constant-expression
18519 enumerator:
18520 identifier
18522 GNU Extensions:
18524 enumerator-definition:
18525 enumerator attributes [opt]
18526 enumerator attributes [opt] = constant-expression */
18528 static void
18529 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18531 tree identifier;
18532 tree value;
18533 location_t loc;
18535 /* Save the input location because we are interested in the location
18536 of the identifier and not the location of the explicit value. */
18537 loc = cp_lexer_peek_token (parser->lexer)->location;
18539 /* Look for the identifier. */
18540 identifier = cp_parser_identifier (parser);
18541 if (identifier == error_mark_node)
18542 return;
18544 /* Parse any specified attributes. */
18545 tree attrs = cp_parser_attributes_opt (parser);
18547 /* If the next token is an '=', then there is an explicit value. */
18548 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18550 /* Consume the `=' token. */
18551 cp_lexer_consume_token (parser->lexer);
18552 /* Parse the value. */
18553 value = cp_parser_constant_expression (parser);
18555 else
18556 value = NULL_TREE;
18558 /* If we are processing a template, make sure the initializer of the
18559 enumerator doesn't contain any bare template parameter pack. */
18560 if (check_for_bare_parameter_packs (value))
18561 value = error_mark_node;
18563 /* Create the enumerator. */
18564 build_enumerator (identifier, value, type, attrs, loc);
18567 /* Parse a namespace-name.
18569 namespace-name:
18570 original-namespace-name
18571 namespace-alias
18573 Returns the NAMESPACE_DECL for the namespace. */
18575 static tree
18576 cp_parser_namespace_name (cp_parser* parser)
18578 tree identifier;
18579 tree namespace_decl;
18581 cp_token *token = cp_lexer_peek_token (parser->lexer);
18583 /* Get the name of the namespace. */
18584 identifier = cp_parser_identifier (parser);
18585 if (identifier == error_mark_node)
18586 return error_mark_node;
18588 /* Look up the identifier in the currently active scope. Look only
18589 for namespaces, due to:
18591 [basic.lookup.udir]
18593 When looking up a namespace-name in a using-directive or alias
18594 definition, only namespace names are considered.
18596 And:
18598 [basic.lookup.qual]
18600 During the lookup of a name preceding the :: scope resolution
18601 operator, object, function, and enumerator names are ignored.
18603 (Note that cp_parser_qualifying_entity only calls this
18604 function if the token after the name is the scope resolution
18605 operator.) */
18606 namespace_decl = cp_parser_lookup_name (parser, identifier,
18607 none_type,
18608 /*is_template=*/false,
18609 /*is_namespace=*/true,
18610 /*check_dependency=*/true,
18611 /*ambiguous_decls=*/NULL,
18612 token->location);
18613 /* If it's not a namespace, issue an error. */
18614 if (namespace_decl == error_mark_node
18615 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18617 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18619 error_at (token->location, "%qD is not a namespace-name", identifier);
18620 if (namespace_decl == error_mark_node
18621 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18622 suggest_alternative_in_explicit_scope (token->location, identifier,
18623 parser->scope);
18625 cp_parser_error (parser, "expected namespace-name");
18626 namespace_decl = error_mark_node;
18629 return namespace_decl;
18632 /* Parse a namespace-definition.
18634 namespace-definition:
18635 named-namespace-definition
18636 unnamed-namespace-definition
18638 named-namespace-definition:
18639 original-namespace-definition
18640 extension-namespace-definition
18642 original-namespace-definition:
18643 namespace identifier { namespace-body }
18645 extension-namespace-definition:
18646 namespace original-namespace-name { namespace-body }
18648 unnamed-namespace-definition:
18649 namespace { namespace-body } */
18651 static void
18652 cp_parser_namespace_definition (cp_parser* parser)
18654 tree identifier;
18655 int nested_definition_count = 0;
18657 cp_ensure_no_omp_declare_simd (parser);
18658 cp_ensure_no_oacc_routine (parser);
18660 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18662 if (is_inline)
18664 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18665 cp_lexer_consume_token (parser->lexer);
18668 /* Look for the `namespace' keyword. */
18669 cp_token* token
18670 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18672 /* Parse any specified attributes before the identifier. */
18673 tree attribs = cp_parser_attributes_opt (parser);
18675 for (;;)
18677 identifier = NULL_TREE;
18679 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18681 identifier = cp_parser_identifier (parser);
18683 if (cp_next_tokens_can_be_std_attribute_p (parser))
18684 pedwarn (input_location, OPT_Wpedantic,
18685 "standard attributes on namespaces must precede "
18686 "the namespace name");
18688 /* Parse any attributes specified after the identifier. */
18689 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18693 break;
18695 if (!nested_definition_count && cxx_dialect < cxx17)
18696 pedwarn (input_location, OPT_Wpedantic,
18697 "nested namespace definitions only available with "
18698 "-std=c++17 or -std=gnu++17");
18700 /* Nested namespace names can create new namespaces (unlike
18701 other qualified-ids). */
18702 if (int count = identifier ? push_namespace (identifier) : 0)
18703 nested_definition_count += count;
18704 else
18705 cp_parser_error (parser, "nested namespace name required");
18706 cp_lexer_consume_token (parser->lexer);
18709 if (nested_definition_count && !identifier)
18710 cp_parser_error (parser, "namespace name required");
18712 if (nested_definition_count && attribs)
18713 error_at (token->location,
18714 "a nested namespace definition cannot have attributes");
18715 if (nested_definition_count && is_inline)
18716 error_at (token->location,
18717 "a nested namespace definition cannot be inline");
18719 /* Start the namespace. */
18720 nested_definition_count += push_namespace (identifier, is_inline);
18722 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18724 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18726 /* Look for the `{' to validate starting the namespace. */
18727 matching_braces braces;
18728 if (braces.require_open (parser))
18730 /* Parse the body of the namespace. */
18731 cp_parser_namespace_body (parser);
18733 /* Look for the final `}'. */
18734 braces.require_close (parser);
18737 if (has_visibility)
18738 pop_visibility (1);
18740 /* Pop the nested namespace definitions. */
18741 while (nested_definition_count--)
18742 pop_namespace ();
18745 /* Parse a namespace-body.
18747 namespace-body:
18748 declaration-seq [opt] */
18750 static void
18751 cp_parser_namespace_body (cp_parser* parser)
18753 cp_parser_declaration_seq_opt (parser);
18756 /* Parse a namespace-alias-definition.
18758 namespace-alias-definition:
18759 namespace identifier = qualified-namespace-specifier ; */
18761 static void
18762 cp_parser_namespace_alias_definition (cp_parser* parser)
18764 tree identifier;
18765 tree namespace_specifier;
18767 cp_token *token = cp_lexer_peek_token (parser->lexer);
18769 /* Look for the `namespace' keyword. */
18770 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18771 /* Look for the identifier. */
18772 identifier = cp_parser_identifier (parser);
18773 if (identifier == error_mark_node)
18774 return;
18775 /* Look for the `=' token. */
18776 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18777 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18779 error_at (token->location, "%<namespace%> definition is not allowed here");
18780 /* Skip the definition. */
18781 cp_lexer_consume_token (parser->lexer);
18782 if (cp_parser_skip_to_closing_brace (parser))
18783 cp_lexer_consume_token (parser->lexer);
18784 return;
18786 cp_parser_require (parser, CPP_EQ, RT_EQ);
18787 /* Look for the qualified-namespace-specifier. */
18788 namespace_specifier
18789 = cp_parser_qualified_namespace_specifier (parser);
18790 /* Look for the `;' token. */
18791 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18793 /* Register the alias in the symbol table. */
18794 do_namespace_alias (identifier, namespace_specifier);
18797 /* Parse a qualified-namespace-specifier.
18799 qualified-namespace-specifier:
18800 :: [opt] nested-name-specifier [opt] namespace-name
18802 Returns a NAMESPACE_DECL corresponding to the specified
18803 namespace. */
18805 static tree
18806 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18808 /* Look for the optional `::'. */
18809 cp_parser_global_scope_opt (parser,
18810 /*current_scope_valid_p=*/false);
18812 /* Look for the optional nested-name-specifier. */
18813 cp_parser_nested_name_specifier_opt (parser,
18814 /*typename_keyword_p=*/false,
18815 /*check_dependency_p=*/true,
18816 /*type_p=*/false,
18817 /*is_declaration=*/true);
18819 return cp_parser_namespace_name (parser);
18822 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18823 access declaration.
18825 using-declaration:
18826 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18827 using :: unqualified-id ;
18829 access-declaration:
18830 qualified-id ;
18834 static bool
18835 cp_parser_using_declaration (cp_parser* parser,
18836 bool access_declaration_p)
18838 cp_token *token;
18839 bool typename_p = false;
18840 bool global_scope_p;
18841 tree decl;
18842 tree identifier;
18843 tree qscope;
18844 int oldcount = errorcount;
18845 cp_token *diag_token = NULL;
18847 if (access_declaration_p)
18849 diag_token = cp_lexer_peek_token (parser->lexer);
18850 cp_parser_parse_tentatively (parser);
18852 else
18854 /* Look for the `using' keyword. */
18855 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18857 again:
18858 /* Peek at the next token. */
18859 token = cp_lexer_peek_token (parser->lexer);
18860 /* See if it's `typename'. */
18861 if (token->keyword == RID_TYPENAME)
18863 /* Remember that we've seen it. */
18864 typename_p = true;
18865 /* Consume the `typename' token. */
18866 cp_lexer_consume_token (parser->lexer);
18870 /* Look for the optional global scope qualification. */
18871 global_scope_p
18872 = (cp_parser_global_scope_opt (parser,
18873 /*current_scope_valid_p=*/false)
18874 != NULL_TREE);
18876 /* If we saw `typename', or didn't see `::', then there must be a
18877 nested-name-specifier present. */
18878 if (typename_p || !global_scope_p)
18880 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18881 /*check_dependency_p=*/true,
18882 /*type_p=*/false,
18883 /*is_declaration=*/true);
18884 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18886 cp_parser_skip_to_end_of_block_or_statement (parser);
18887 return false;
18890 /* Otherwise, we could be in either of the two productions. In that
18891 case, treat the nested-name-specifier as optional. */
18892 else
18893 qscope = cp_parser_nested_name_specifier_opt (parser,
18894 /*typename_keyword_p=*/false,
18895 /*check_dependency_p=*/true,
18896 /*type_p=*/false,
18897 /*is_declaration=*/true);
18898 if (!qscope)
18899 qscope = global_namespace;
18900 else if (UNSCOPED_ENUM_P (qscope))
18901 qscope = CP_TYPE_CONTEXT (qscope);
18903 if (access_declaration_p && cp_parser_error_occurred (parser))
18904 /* Something has already gone wrong; there's no need to parse
18905 further. Since an error has occurred, the return value of
18906 cp_parser_parse_definitely will be false, as required. */
18907 return cp_parser_parse_definitely (parser);
18909 token = cp_lexer_peek_token (parser->lexer);
18910 /* Parse the unqualified-id. */
18911 identifier = cp_parser_unqualified_id (parser,
18912 /*template_keyword_p=*/false,
18913 /*check_dependency_p=*/true,
18914 /*declarator_p=*/true,
18915 /*optional_p=*/false);
18917 if (access_declaration_p)
18919 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18920 cp_parser_simulate_error (parser);
18921 if (!cp_parser_parse_definitely (parser))
18922 return false;
18924 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18926 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18927 if (cxx_dialect < cxx17
18928 && !in_system_header_at (ell->location))
18929 pedwarn (ell->location, 0,
18930 "pack expansion in using-declaration only available "
18931 "with -std=c++17 or -std=gnu++17");
18932 qscope = make_pack_expansion (qscope);
18935 /* The function we call to handle a using-declaration is different
18936 depending on what scope we are in. */
18937 if (qscope == error_mark_node || identifier == error_mark_node)
18939 else if (!identifier_p (identifier)
18940 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18941 /* [namespace.udecl]
18943 A using declaration shall not name a template-id. */
18944 error_at (token->location,
18945 "a template-id may not appear in a using-declaration");
18946 else
18948 if (at_class_scope_p ())
18950 /* Create the USING_DECL. */
18951 decl = do_class_using_decl (qscope, identifier);
18953 if (decl && typename_p)
18954 USING_DECL_TYPENAME_P (decl) = 1;
18956 if (check_for_bare_parameter_packs (decl))
18958 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18959 return false;
18961 else
18962 /* Add it to the list of members in this class. */
18963 finish_member_declaration (decl);
18965 else
18967 decl = cp_parser_lookup_name_simple (parser,
18968 identifier,
18969 token->location);
18970 if (decl == error_mark_node)
18971 cp_parser_name_lookup_error (parser, identifier,
18972 decl, NLE_NULL,
18973 token->location);
18974 else if (check_for_bare_parameter_packs (decl))
18976 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18977 return false;
18979 else if (!at_namespace_scope_p ())
18980 finish_local_using_decl (decl, qscope, identifier);
18981 else
18982 finish_namespace_using_decl (decl, qscope, identifier);
18986 if (!access_declaration_p
18987 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18989 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18990 if (cxx_dialect < cxx17)
18991 pedwarn (comma->location, 0,
18992 "comma-separated list in using-declaration only available "
18993 "with -std=c++17 or -std=gnu++17");
18994 goto again;
18997 /* Look for the final `;'. */
18998 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19000 if (access_declaration_p && errorcount == oldcount)
19001 warning_at (diag_token->location, OPT_Wdeprecated,
19002 "access declarations are deprecated "
19003 "in favour of using-declarations; "
19004 "suggestion: add the %<using%> keyword");
19006 return true;
19009 /* Parse an alias-declaration.
19011 alias-declaration:
19012 using identifier attribute-specifier-seq [opt] = type-id */
19014 static tree
19015 cp_parser_alias_declaration (cp_parser* parser)
19017 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19018 location_t id_location;
19019 cp_declarator *declarator;
19020 cp_decl_specifier_seq decl_specs;
19021 bool member_p;
19022 const char *saved_message = NULL;
19024 /* Look for the `using' keyword. */
19025 cp_token *using_token
19026 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19027 if (using_token == NULL)
19028 return error_mark_node;
19030 id_location = cp_lexer_peek_token (parser->lexer)->location;
19031 id = cp_parser_identifier (parser);
19032 if (id == error_mark_node)
19033 return error_mark_node;
19035 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19036 attributes = cp_parser_attributes_opt (parser);
19037 if (attributes == error_mark_node)
19038 return error_mark_node;
19040 cp_parser_require (parser, CPP_EQ, RT_EQ);
19042 if (cp_parser_error_occurred (parser))
19043 return error_mark_node;
19045 cp_parser_commit_to_tentative_parse (parser);
19047 /* Now we are going to parse the type-id of the declaration. */
19050 [dcl.type]/3 says:
19052 "A type-specifier-seq shall not define a class or enumeration
19053 unless it appears in the type-id of an alias-declaration (7.1.3) that
19054 is not the declaration of a template-declaration."
19056 In other words, if we currently are in an alias template, the
19057 type-id should not define a type.
19059 So let's set parser->type_definition_forbidden_message in that
19060 case; cp_parser_check_type_definition (called by
19061 cp_parser_class_specifier) will then emit an error if a type is
19062 defined in the type-id. */
19063 if (parser->num_template_parameter_lists)
19065 saved_message = parser->type_definition_forbidden_message;
19066 parser->type_definition_forbidden_message =
19067 G_("types may not be defined in alias template declarations");
19070 type = cp_parser_type_id (parser);
19072 /* Restore the error message if need be. */
19073 if (parser->num_template_parameter_lists)
19074 parser->type_definition_forbidden_message = saved_message;
19076 if (type == error_mark_node
19077 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19079 cp_parser_skip_to_end_of_block_or_statement (parser);
19080 return error_mark_node;
19083 /* A typedef-name can also be introduced by an alias-declaration. The
19084 identifier following the using keyword becomes a typedef-name. It has
19085 the same semantics as if it were introduced by the typedef
19086 specifier. In particular, it does not define a new type and it shall
19087 not appear in the type-id. */
19089 clear_decl_specs (&decl_specs);
19090 decl_specs.type = type;
19091 if (attributes != NULL_TREE)
19093 decl_specs.attributes = attributes;
19094 set_and_check_decl_spec_loc (&decl_specs,
19095 ds_attribute,
19096 attrs_token);
19098 set_and_check_decl_spec_loc (&decl_specs,
19099 ds_typedef,
19100 using_token);
19101 set_and_check_decl_spec_loc (&decl_specs,
19102 ds_alias,
19103 using_token);
19105 if (parser->num_template_parameter_lists
19106 && !cp_parser_check_template_parameters (parser,
19107 /*num_templates=*/0,
19108 /*template_id*/false,
19109 id_location,
19110 /*declarator=*/NULL))
19111 return error_mark_node;
19113 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19114 declarator->id_loc = id_location;
19116 member_p = at_class_scope_p ();
19117 if (member_p)
19118 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19119 NULL_TREE, attributes);
19120 else
19121 decl = start_decl (declarator, &decl_specs, 0,
19122 attributes, NULL_TREE, &pushed_scope);
19123 if (decl == error_mark_node)
19124 return decl;
19126 // Attach constraints to the alias declaration.
19127 if (flag_concepts && current_template_parms)
19129 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19130 tree constr = build_constraints (reqs, NULL_TREE);
19131 set_constraints (decl, constr);
19134 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19136 if (pushed_scope)
19137 pop_scope (pushed_scope);
19139 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19140 added into the symbol table; otherwise, return the TYPE_DECL. */
19141 if (DECL_LANG_SPECIFIC (decl)
19142 && DECL_TEMPLATE_INFO (decl)
19143 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19145 decl = DECL_TI_TEMPLATE (decl);
19146 if (member_p)
19147 check_member_template (decl);
19150 return decl;
19153 /* Parse a using-directive.
19155 using-directive:
19156 using namespace :: [opt] nested-name-specifier [opt]
19157 namespace-name ; */
19159 static void
19160 cp_parser_using_directive (cp_parser* parser)
19162 tree namespace_decl;
19163 tree attribs;
19165 /* Look for the `using' keyword. */
19166 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19167 /* And the `namespace' keyword. */
19168 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19169 /* Look for the optional `::' operator. */
19170 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19171 /* And the optional nested-name-specifier. */
19172 cp_parser_nested_name_specifier_opt (parser,
19173 /*typename_keyword_p=*/false,
19174 /*check_dependency_p=*/true,
19175 /*type_p=*/false,
19176 /*is_declaration=*/true);
19177 /* Get the namespace being used. */
19178 namespace_decl = cp_parser_namespace_name (parser);
19179 /* And any specified attributes. */
19180 attribs = cp_parser_attributes_opt (parser);
19182 /* Update the symbol table. */
19183 if (namespace_bindings_p ())
19184 finish_namespace_using_directive (namespace_decl, attribs);
19185 else
19186 finish_local_using_directive (namespace_decl, attribs);
19188 /* Look for the final `;'. */
19189 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19192 /* Parse an asm-definition.
19194 asm-definition:
19195 asm ( string-literal ) ;
19197 GNU Extension:
19199 asm-definition:
19200 asm volatile [opt] ( string-literal ) ;
19201 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19202 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19203 : asm-operand-list [opt] ) ;
19204 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19205 : asm-operand-list [opt]
19206 : asm-clobber-list [opt] ) ;
19207 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19208 : asm-clobber-list [opt]
19209 : asm-goto-list ) ; */
19211 static void
19212 cp_parser_asm_definition (cp_parser* parser)
19214 tree string;
19215 tree outputs = NULL_TREE;
19216 tree inputs = NULL_TREE;
19217 tree clobbers = NULL_TREE;
19218 tree labels = NULL_TREE;
19219 tree asm_stmt;
19220 bool volatile_p = false;
19221 bool extended_p = false;
19222 bool invalid_inputs_p = false;
19223 bool invalid_outputs_p = false;
19224 bool goto_p = false;
19225 required_token missing = RT_NONE;
19227 /* Look for the `asm' keyword. */
19228 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19230 if (parser->in_function_body
19231 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19233 error ("%<asm%> in %<constexpr%> function");
19234 cp_function_chain->invalid_constexpr = true;
19237 /* See if the next token is `volatile'. */
19238 if (cp_parser_allow_gnu_extensions_p (parser)
19239 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19241 /* Remember that we saw the `volatile' keyword. */
19242 volatile_p = true;
19243 /* Consume the token. */
19244 cp_lexer_consume_token (parser->lexer);
19246 if (cp_parser_allow_gnu_extensions_p (parser)
19247 && parser->in_function_body
19248 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19250 /* Remember that we saw the `goto' keyword. */
19251 goto_p = true;
19252 /* Consume the token. */
19253 cp_lexer_consume_token (parser->lexer);
19255 /* Look for the opening `('. */
19256 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19257 return;
19258 /* Look for the string. */
19259 string = cp_parser_string_literal (parser, false, false);
19260 if (string == error_mark_node)
19262 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19263 /*consume_paren=*/true);
19264 return;
19267 /* If we're allowing GNU extensions, check for the extended assembly
19268 syntax. Unfortunately, the `:' tokens need not be separated by
19269 a space in C, and so, for compatibility, we tolerate that here
19270 too. Doing that means that we have to treat the `::' operator as
19271 two `:' tokens. */
19272 if (cp_parser_allow_gnu_extensions_p (parser)
19273 && parser->in_function_body
19274 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19275 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19277 bool inputs_p = false;
19278 bool clobbers_p = false;
19279 bool labels_p = false;
19281 /* The extended syntax was used. */
19282 extended_p = true;
19284 /* Look for outputs. */
19285 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19287 /* Consume the `:'. */
19288 cp_lexer_consume_token (parser->lexer);
19289 /* Parse the output-operands. */
19290 if (cp_lexer_next_token_is_not (parser->lexer,
19291 CPP_COLON)
19292 && cp_lexer_next_token_is_not (parser->lexer,
19293 CPP_SCOPE)
19294 && cp_lexer_next_token_is_not (parser->lexer,
19295 CPP_CLOSE_PAREN)
19296 && !goto_p)
19298 outputs = cp_parser_asm_operand_list (parser);
19299 if (outputs == error_mark_node)
19300 invalid_outputs_p = true;
19303 /* If the next token is `::', there are no outputs, and the
19304 next token is the beginning of the inputs. */
19305 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19306 /* The inputs are coming next. */
19307 inputs_p = true;
19309 /* Look for inputs. */
19310 if (inputs_p
19311 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19313 /* Consume the `:' or `::'. */
19314 cp_lexer_consume_token (parser->lexer);
19315 /* Parse the output-operands. */
19316 if (cp_lexer_next_token_is_not (parser->lexer,
19317 CPP_COLON)
19318 && cp_lexer_next_token_is_not (parser->lexer,
19319 CPP_SCOPE)
19320 && cp_lexer_next_token_is_not (parser->lexer,
19321 CPP_CLOSE_PAREN))
19323 inputs = cp_parser_asm_operand_list (parser);
19324 if (inputs == error_mark_node)
19325 invalid_inputs_p = true;
19328 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19329 /* The clobbers are coming next. */
19330 clobbers_p = true;
19332 /* Look for clobbers. */
19333 if (clobbers_p
19334 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19336 clobbers_p = true;
19337 /* Consume the `:' or `::'. */
19338 cp_lexer_consume_token (parser->lexer);
19339 /* Parse the clobbers. */
19340 if (cp_lexer_next_token_is_not (parser->lexer,
19341 CPP_COLON)
19342 && cp_lexer_next_token_is_not (parser->lexer,
19343 CPP_CLOSE_PAREN))
19344 clobbers = cp_parser_asm_clobber_list (parser);
19346 else if (goto_p
19347 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19348 /* The labels are coming next. */
19349 labels_p = true;
19351 /* Look for labels. */
19352 if (labels_p
19353 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19355 labels_p = true;
19356 /* Consume the `:' or `::'. */
19357 cp_lexer_consume_token (parser->lexer);
19358 /* Parse the labels. */
19359 labels = cp_parser_asm_label_list (parser);
19362 if (goto_p && !labels_p)
19363 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19365 else if (goto_p)
19366 missing = RT_COLON_SCOPE;
19368 /* Look for the closing `)'. */
19369 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19370 missing ? missing : RT_CLOSE_PAREN))
19371 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19372 /*consume_paren=*/true);
19373 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19375 if (!invalid_inputs_p && !invalid_outputs_p)
19377 /* Create the ASM_EXPR. */
19378 if (parser->in_function_body)
19380 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19381 inputs, clobbers, labels);
19382 /* If the extended syntax was not used, mark the ASM_EXPR. */
19383 if (!extended_p)
19385 tree temp = asm_stmt;
19386 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19387 temp = TREE_OPERAND (temp, 0);
19389 ASM_INPUT_P (temp) = 1;
19392 else
19393 symtab->finalize_toplevel_asm (string);
19397 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19398 type that comes from the decl-specifier-seq. */
19400 static tree
19401 strip_declarator_types (tree type, cp_declarator *declarator)
19403 for (cp_declarator *d = declarator; d;)
19404 switch (d->kind)
19406 case cdk_id:
19407 case cdk_decomp:
19408 case cdk_error:
19409 d = NULL;
19410 break;
19412 default:
19413 if (TYPE_PTRMEMFUNC_P (type))
19414 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19415 type = TREE_TYPE (type);
19416 d = d->declarator;
19417 break;
19420 return type;
19423 /* Declarators [gram.dcl.decl] */
19425 /* Parse an init-declarator.
19427 init-declarator:
19428 declarator initializer [opt]
19430 GNU Extension:
19432 init-declarator:
19433 declarator asm-specification [opt] attributes [opt] initializer [opt]
19435 function-definition:
19436 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19437 function-body
19438 decl-specifier-seq [opt] declarator function-try-block
19440 GNU Extension:
19442 function-definition:
19443 __extension__ function-definition
19445 TM Extension:
19447 function-definition:
19448 decl-specifier-seq [opt] declarator function-transaction-block
19450 The DECL_SPECIFIERS apply to this declarator. Returns a
19451 representation of the entity declared. If MEMBER_P is TRUE, then
19452 this declarator appears in a class scope. The new DECL created by
19453 this declarator is returned.
19455 The CHECKS are access checks that should be performed once we know
19456 what entity is being declared (and, therefore, what classes have
19457 befriended it).
19459 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19460 for a function-definition here as well. If the declarator is a
19461 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19462 be TRUE upon return. By that point, the function-definition will
19463 have been completely parsed.
19465 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19466 is FALSE.
19468 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19469 parsed declaration if it is an uninitialized single declarator not followed
19470 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19471 if present, will not be consumed. If returned, this declarator will be
19472 created with SD_INITIALIZED but will not call cp_finish_decl.
19474 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19475 and there is an initializer, the pointed location_t is set to the
19476 location of the '=' or `(', or '{' in C++11 token introducing the
19477 initializer. */
19479 static tree
19480 cp_parser_init_declarator (cp_parser* parser,
19481 cp_decl_specifier_seq *decl_specifiers,
19482 vec<deferred_access_check, va_gc> *checks,
19483 bool function_definition_allowed_p,
19484 bool member_p,
19485 int declares_class_or_enum,
19486 bool* function_definition_p,
19487 tree* maybe_range_for_decl,
19488 location_t* init_loc,
19489 tree* auto_result)
19491 cp_token *token = NULL, *asm_spec_start_token = NULL,
19492 *attributes_start_token = NULL;
19493 cp_declarator *declarator;
19494 tree prefix_attributes;
19495 tree attributes = NULL;
19496 tree asm_specification;
19497 tree initializer;
19498 tree decl = NULL_TREE;
19499 tree scope;
19500 int is_initialized;
19501 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19502 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19503 "(...)". */
19504 enum cpp_ttype initialization_kind;
19505 bool is_direct_init = false;
19506 bool is_non_constant_init;
19507 int ctor_dtor_or_conv_p;
19508 bool friend_p = cp_parser_friend_p (decl_specifiers);
19509 tree pushed_scope = NULL_TREE;
19510 bool range_for_decl_p = false;
19511 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19512 location_t tmp_init_loc = UNKNOWN_LOCATION;
19514 /* Gather the attributes that were provided with the
19515 decl-specifiers. */
19516 prefix_attributes = decl_specifiers->attributes;
19518 /* Assume that this is not the declarator for a function
19519 definition. */
19520 if (function_definition_p)
19521 *function_definition_p = false;
19523 /* Default arguments are only permitted for function parameters. */
19524 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19525 parser->default_arg_ok_p = false;
19527 /* Defer access checks while parsing the declarator; we cannot know
19528 what names are accessible until we know what is being
19529 declared. */
19530 resume_deferring_access_checks ();
19532 token = cp_lexer_peek_token (parser->lexer);
19534 /* Parse the declarator. */
19535 declarator
19536 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19537 &ctor_dtor_or_conv_p,
19538 /*parenthesized_p=*/NULL,
19539 member_p, friend_p);
19540 /* Gather up the deferred checks. */
19541 stop_deferring_access_checks ();
19543 parser->default_arg_ok_p = saved_default_arg_ok_p;
19545 /* If the DECLARATOR was erroneous, there's no need to go
19546 further. */
19547 if (declarator == cp_error_declarator)
19548 return error_mark_node;
19550 /* Check that the number of template-parameter-lists is OK. */
19551 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19552 token->location))
19553 return error_mark_node;
19555 if (declares_class_or_enum & 2)
19556 cp_parser_check_for_definition_in_return_type (declarator,
19557 decl_specifiers->type,
19558 decl_specifiers->locations[ds_type_spec]);
19560 /* Figure out what scope the entity declared by the DECLARATOR is
19561 located in. `grokdeclarator' sometimes changes the scope, so
19562 we compute it now. */
19563 scope = get_scope_of_declarator (declarator);
19565 /* Perform any lookups in the declared type which were thought to be
19566 dependent, but are not in the scope of the declarator. */
19567 decl_specifiers->type
19568 = maybe_update_decl_type (decl_specifiers->type, scope);
19570 /* If we're allowing GNU extensions, look for an
19571 asm-specification. */
19572 if (cp_parser_allow_gnu_extensions_p (parser))
19574 /* Look for an asm-specification. */
19575 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19576 asm_specification = cp_parser_asm_specification_opt (parser);
19578 else
19579 asm_specification = NULL_TREE;
19581 /* Look for attributes. */
19582 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19583 attributes = cp_parser_attributes_opt (parser);
19585 /* Peek at the next token. */
19586 token = cp_lexer_peek_token (parser->lexer);
19588 bool bogus_implicit_tmpl = false;
19590 if (function_declarator_p (declarator))
19592 /* Handle C++17 deduction guides. */
19593 if (!decl_specifiers->type
19594 && ctor_dtor_or_conv_p <= 0
19595 && cxx_dialect >= cxx17)
19597 cp_declarator *id = get_id_declarator (declarator);
19598 tree name = id->u.id.unqualified_name;
19599 parser->scope = id->u.id.qualifying_scope;
19600 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19601 if (tmpl
19602 && (DECL_CLASS_TEMPLATE_P (tmpl)
19603 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19605 id->u.id.unqualified_name = dguide_name (tmpl);
19606 id->u.id.sfk = sfk_deduction_guide;
19607 ctor_dtor_or_conv_p = 1;
19611 /* Check to see if the token indicates the start of a
19612 function-definition. */
19613 if (cp_parser_token_starts_function_definition_p (token))
19615 if (!function_definition_allowed_p)
19617 /* If a function-definition should not appear here, issue an
19618 error message. */
19619 cp_parser_error (parser,
19620 "a function-definition is not allowed here");
19621 return error_mark_node;
19624 location_t func_brace_location
19625 = cp_lexer_peek_token (parser->lexer)->location;
19627 /* Neither attributes nor an asm-specification are allowed
19628 on a function-definition. */
19629 if (asm_specification)
19630 error_at (asm_spec_start_token->location,
19631 "an asm-specification is not allowed "
19632 "on a function-definition");
19633 if (attributes)
19634 error_at (attributes_start_token->location,
19635 "attributes are not allowed "
19636 "on a function-definition");
19637 /* This is a function-definition. */
19638 *function_definition_p = true;
19640 /* Parse the function definition. */
19641 if (member_p)
19642 decl = cp_parser_save_member_function_body (parser,
19643 decl_specifiers,
19644 declarator,
19645 prefix_attributes);
19646 else
19647 decl =
19648 (cp_parser_function_definition_from_specifiers_and_declarator
19649 (parser, decl_specifiers, prefix_attributes, declarator));
19651 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19653 /* This is where the prologue starts... */
19654 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19655 = func_brace_location;
19658 return decl;
19661 else if (parser->fully_implicit_function_template_p)
19663 /* A non-template declaration involving a function parameter list
19664 containing an implicit template parameter will be made into a
19665 template. If the resulting declaration is not going to be an
19666 actual function then finish the template scope here to prevent it.
19667 An error message will be issued once we have a decl to talk about.
19669 FIXME probably we should do type deduction rather than create an
19670 implicit template, but the standard currently doesn't allow it. */
19671 bogus_implicit_tmpl = true;
19672 finish_fully_implicit_template (parser, NULL_TREE);
19675 /* [dcl.dcl]
19677 Only in function declarations for constructors, destructors, type
19678 conversions, and deduction guides can the decl-specifier-seq be omitted.
19680 We explicitly postpone this check past the point where we handle
19681 function-definitions because we tolerate function-definitions
19682 that are missing their return types in some modes. */
19683 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19685 cp_parser_error (parser,
19686 "expected constructor, destructor, or type conversion");
19687 return error_mark_node;
19690 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19691 if (token->type == CPP_EQ
19692 || token->type == CPP_OPEN_PAREN
19693 || token->type == CPP_OPEN_BRACE)
19695 is_initialized = SD_INITIALIZED;
19696 initialization_kind = token->type;
19697 if (maybe_range_for_decl)
19698 *maybe_range_for_decl = error_mark_node;
19699 tmp_init_loc = token->location;
19700 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19701 *init_loc = tmp_init_loc;
19703 if (token->type == CPP_EQ
19704 && function_declarator_p (declarator))
19706 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19707 if (t2->keyword == RID_DEFAULT)
19708 is_initialized = SD_DEFAULTED;
19709 else if (t2->keyword == RID_DELETE)
19710 is_initialized = SD_DELETED;
19713 else
19715 /* If the init-declarator isn't initialized and isn't followed by a
19716 `,' or `;', it's not a valid init-declarator. */
19717 if (token->type != CPP_COMMA
19718 && token->type != CPP_SEMICOLON)
19720 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19721 range_for_decl_p = true;
19722 else
19724 if (!maybe_range_for_decl)
19725 cp_parser_error (parser, "expected initializer");
19726 return error_mark_node;
19729 is_initialized = SD_UNINITIALIZED;
19730 initialization_kind = CPP_EOF;
19733 /* Because start_decl has side-effects, we should only call it if we
19734 know we're going ahead. By this point, we know that we cannot
19735 possibly be looking at any other construct. */
19736 cp_parser_commit_to_tentative_parse (parser);
19738 /* Enter the newly declared entry in the symbol table. If we're
19739 processing a declaration in a class-specifier, we wait until
19740 after processing the initializer. */
19741 if (!member_p)
19743 if (parser->in_unbraced_linkage_specification_p)
19744 decl_specifiers->storage_class = sc_extern;
19745 decl = start_decl (declarator, decl_specifiers,
19746 range_for_decl_p? SD_INITIALIZED : is_initialized,
19747 attributes, prefix_attributes, &pushed_scope);
19748 cp_finalize_omp_declare_simd (parser, decl);
19749 cp_finalize_oacc_routine (parser, decl, false);
19750 /* Adjust location of decl if declarator->id_loc is more appropriate:
19751 set, and decl wasn't merged with another decl, in which case its
19752 location would be different from input_location, and more accurate. */
19753 if (DECL_P (decl)
19754 && declarator->id_loc != UNKNOWN_LOCATION
19755 && DECL_SOURCE_LOCATION (decl) == input_location)
19756 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19758 else if (scope)
19759 /* Enter the SCOPE. That way unqualified names appearing in the
19760 initializer will be looked up in SCOPE. */
19761 pushed_scope = push_scope (scope);
19763 /* Perform deferred access control checks, now that we know in which
19764 SCOPE the declared entity resides. */
19765 if (!member_p && decl)
19767 tree saved_current_function_decl = NULL_TREE;
19769 /* If the entity being declared is a function, pretend that we
19770 are in its scope. If it is a `friend', it may have access to
19771 things that would not otherwise be accessible. */
19772 if (TREE_CODE (decl) == FUNCTION_DECL)
19774 saved_current_function_decl = current_function_decl;
19775 current_function_decl = decl;
19778 /* Perform access checks for template parameters. */
19779 cp_parser_perform_template_parameter_access_checks (checks);
19781 /* Perform the access control checks for the declarator and the
19782 decl-specifiers. */
19783 perform_deferred_access_checks (tf_warning_or_error);
19785 /* Restore the saved value. */
19786 if (TREE_CODE (decl) == FUNCTION_DECL)
19787 current_function_decl = saved_current_function_decl;
19790 /* Parse the initializer. */
19791 initializer = NULL_TREE;
19792 is_direct_init = false;
19793 is_non_constant_init = true;
19794 if (is_initialized)
19796 if (function_declarator_p (declarator))
19798 if (initialization_kind == CPP_EQ)
19799 initializer = cp_parser_pure_specifier (parser);
19800 else
19802 /* If the declaration was erroneous, we don't really
19803 know what the user intended, so just silently
19804 consume the initializer. */
19805 if (decl != error_mark_node)
19806 error_at (tmp_init_loc, "initializer provided for function");
19807 cp_parser_skip_to_closing_parenthesis (parser,
19808 /*recovering=*/true,
19809 /*or_comma=*/false,
19810 /*consume_paren=*/true);
19813 else
19815 /* We want to record the extra mangling scope for in-class
19816 initializers of class members and initializers of static data
19817 member templates. The former involves deferring
19818 parsing of the initializer until end of class as with default
19819 arguments. So right here we only handle the latter. */
19820 if (!member_p && processing_template_decl && decl != error_mark_node)
19821 start_lambda_scope (decl);
19822 initializer = cp_parser_initializer (parser,
19823 &is_direct_init,
19824 &is_non_constant_init);
19825 if (!member_p && processing_template_decl && decl != error_mark_node)
19826 finish_lambda_scope ();
19827 if (initializer == error_mark_node)
19828 cp_parser_skip_to_end_of_statement (parser);
19832 /* The old parser allows attributes to appear after a parenthesized
19833 initializer. Mark Mitchell proposed removing this functionality
19834 on the GCC mailing lists on 2002-08-13. This parser accepts the
19835 attributes -- but ignores them. Made a permerror in GCC 8. */
19836 if (cp_parser_allow_gnu_extensions_p (parser)
19837 && initialization_kind == CPP_OPEN_PAREN
19838 && cp_parser_attributes_opt (parser)
19839 && permerror (input_location,
19840 "attributes after parenthesized initializer ignored"))
19842 static bool hint;
19843 if (flag_permissive && !hint)
19845 hint = true;
19846 inform (input_location,
19847 "this flexibility is deprecated and will be removed");
19851 /* And now complain about a non-function implicit template. */
19852 if (bogus_implicit_tmpl && decl != error_mark_node)
19853 error_at (DECL_SOURCE_LOCATION (decl),
19854 "non-function %qD declared as implicit template", decl);
19856 /* For an in-class declaration, use `grokfield' to create the
19857 declaration. */
19858 if (member_p)
19860 if (pushed_scope)
19862 pop_scope (pushed_scope);
19863 pushed_scope = NULL_TREE;
19865 decl = grokfield (declarator, decl_specifiers,
19866 initializer, !is_non_constant_init,
19867 /*asmspec=*/NULL_TREE,
19868 attr_chainon (attributes, prefix_attributes));
19869 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19870 cp_parser_save_default_args (parser, decl);
19871 cp_finalize_omp_declare_simd (parser, decl);
19872 cp_finalize_oacc_routine (parser, decl, false);
19875 /* Finish processing the declaration. But, skip member
19876 declarations. */
19877 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19879 cp_finish_decl (decl,
19880 initializer, !is_non_constant_init,
19881 asm_specification,
19882 /* If the initializer is in parentheses, then this is
19883 a direct-initialization, which means that an
19884 `explicit' constructor is OK. Otherwise, an
19885 `explicit' constructor cannot be used. */
19886 ((is_direct_init || !is_initialized)
19887 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19889 else if ((cxx_dialect != cxx98) && friend_p
19890 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19891 /* Core issue #226 (C++0x only): A default template-argument
19892 shall not be specified in a friend class template
19893 declaration. */
19894 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19895 /*is_partial=*/false, /*is_friend_decl=*/1);
19897 if (!friend_p && pushed_scope)
19898 pop_scope (pushed_scope);
19900 if (function_declarator_p (declarator)
19901 && parser->fully_implicit_function_template_p)
19903 if (member_p)
19904 decl = finish_fully_implicit_template (parser, decl);
19905 else
19906 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19909 if (auto_result && is_initialized && decl_specifiers->type
19910 && type_uses_auto (decl_specifiers->type))
19911 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19913 return decl;
19916 /* Parse a declarator.
19918 declarator:
19919 direct-declarator
19920 ptr-operator declarator
19922 abstract-declarator:
19923 ptr-operator abstract-declarator [opt]
19924 direct-abstract-declarator
19926 GNU Extensions:
19928 declarator:
19929 attributes [opt] direct-declarator
19930 attributes [opt] ptr-operator declarator
19932 abstract-declarator:
19933 attributes [opt] ptr-operator abstract-declarator [opt]
19934 attributes [opt] direct-abstract-declarator
19936 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19937 detect constructors, destructors, deduction guides, or conversion operators.
19938 It is set to -1 if the declarator is a name, and +1 if it is a
19939 function. Otherwise it is set to zero. Usually you just want to
19940 test for >0, but internally the negative value is used.
19942 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19943 a decl-specifier-seq unless it declares a constructor, destructor,
19944 or conversion. It might seem that we could check this condition in
19945 semantic analysis, rather than parsing, but that makes it difficult
19946 to handle something like `f()'. We want to notice that there are
19947 no decl-specifiers, and therefore realize that this is an
19948 expression, not a declaration.)
19950 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19951 the declarator is a direct-declarator of the form "(...)".
19953 MEMBER_P is true iff this declarator is a member-declarator.
19955 FRIEND_P is true iff this declarator is a friend. */
19957 static cp_declarator *
19958 cp_parser_declarator (cp_parser* parser,
19959 cp_parser_declarator_kind dcl_kind,
19960 int* ctor_dtor_or_conv_p,
19961 bool* parenthesized_p,
19962 bool member_p, bool friend_p)
19964 cp_declarator *declarator;
19965 enum tree_code code;
19966 cp_cv_quals cv_quals;
19967 tree class_type;
19968 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19970 /* Assume this is not a constructor, destructor, or type-conversion
19971 operator. */
19972 if (ctor_dtor_or_conv_p)
19973 *ctor_dtor_or_conv_p = 0;
19975 if (cp_parser_allow_gnu_extensions_p (parser))
19976 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19978 /* Check for the ptr-operator production. */
19979 cp_parser_parse_tentatively (parser);
19980 /* Parse the ptr-operator. */
19981 code = cp_parser_ptr_operator (parser,
19982 &class_type,
19983 &cv_quals,
19984 &std_attributes);
19986 /* If that worked, then we have a ptr-operator. */
19987 if (cp_parser_parse_definitely (parser))
19989 /* If a ptr-operator was found, then this declarator was not
19990 parenthesized. */
19991 if (parenthesized_p)
19992 *parenthesized_p = true;
19993 /* The dependent declarator is optional if we are parsing an
19994 abstract-declarator. */
19995 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19996 cp_parser_parse_tentatively (parser);
19998 /* Parse the dependent declarator. */
19999 declarator = cp_parser_declarator (parser, dcl_kind,
20000 /*ctor_dtor_or_conv_p=*/NULL,
20001 /*parenthesized_p=*/NULL,
20002 /*member_p=*/false,
20003 friend_p);
20005 /* If we are parsing an abstract-declarator, we must handle the
20006 case where the dependent declarator is absent. */
20007 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20008 && !cp_parser_parse_definitely (parser))
20009 declarator = NULL;
20011 declarator = cp_parser_make_indirect_declarator
20012 (code, class_type, cv_quals, declarator, std_attributes);
20014 /* Everything else is a direct-declarator. */
20015 else
20017 if (parenthesized_p)
20018 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20019 CPP_OPEN_PAREN);
20020 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20021 ctor_dtor_or_conv_p,
20022 member_p, friend_p);
20025 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20026 declarator->attributes = gnu_attributes;
20027 return declarator;
20030 /* Parse a direct-declarator or direct-abstract-declarator.
20032 direct-declarator:
20033 declarator-id
20034 direct-declarator ( parameter-declaration-clause )
20035 cv-qualifier-seq [opt]
20036 ref-qualifier [opt]
20037 exception-specification [opt]
20038 direct-declarator [ constant-expression [opt] ]
20039 ( declarator )
20041 direct-abstract-declarator:
20042 direct-abstract-declarator [opt]
20043 ( parameter-declaration-clause )
20044 cv-qualifier-seq [opt]
20045 ref-qualifier [opt]
20046 exception-specification [opt]
20047 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20048 ( abstract-declarator )
20050 Returns a representation of the declarator. DCL_KIND is
20051 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20052 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20053 we are parsing a direct-declarator. It is
20054 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20055 of ambiguity we prefer an abstract declarator, as per
20056 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20057 as for cp_parser_declarator. */
20059 static cp_declarator *
20060 cp_parser_direct_declarator (cp_parser* parser,
20061 cp_parser_declarator_kind dcl_kind,
20062 int* ctor_dtor_or_conv_p,
20063 bool member_p, bool friend_p)
20065 cp_token *token;
20066 cp_declarator *declarator = NULL;
20067 tree scope = NULL_TREE;
20068 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20069 bool saved_in_declarator_p = parser->in_declarator_p;
20070 bool first = true;
20071 tree pushed_scope = NULL_TREE;
20072 cp_token *open_paren = NULL, *close_paren = NULL;
20074 while (true)
20076 /* Peek at the next token. */
20077 token = cp_lexer_peek_token (parser->lexer);
20078 if (token->type == CPP_OPEN_PAREN)
20080 /* This is either a parameter-declaration-clause, or a
20081 parenthesized declarator. When we know we are parsing a
20082 named declarator, it must be a parenthesized declarator
20083 if FIRST is true. For instance, `(int)' is a
20084 parameter-declaration-clause, with an omitted
20085 direct-abstract-declarator. But `((*))', is a
20086 parenthesized abstract declarator. Finally, when T is a
20087 template parameter `(T)' is a
20088 parameter-declaration-clause, and not a parenthesized
20089 named declarator.
20091 We first try and parse a parameter-declaration-clause,
20092 and then try a nested declarator (if FIRST is true).
20094 It is not an error for it not to be a
20095 parameter-declaration-clause, even when FIRST is
20096 false. Consider,
20098 int i (int);
20099 int i (3);
20101 The first is the declaration of a function while the
20102 second is the definition of a variable, including its
20103 initializer.
20105 Having seen only the parenthesis, we cannot know which of
20106 these two alternatives should be selected. Even more
20107 complex are examples like:
20109 int i (int (a));
20110 int i (int (3));
20112 The former is a function-declaration; the latter is a
20113 variable initialization.
20115 Thus again, we try a parameter-declaration-clause, and if
20116 that fails, we back out and return. */
20118 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20120 tree params;
20121 bool is_declarator = false;
20123 open_paren = NULL;
20125 /* In a member-declarator, the only valid interpretation
20126 of a parenthesis is the start of a
20127 parameter-declaration-clause. (It is invalid to
20128 initialize a static data member with a parenthesized
20129 initializer; only the "=" form of initialization is
20130 permitted.) */
20131 if (!member_p)
20132 cp_parser_parse_tentatively (parser);
20134 /* Consume the `('. */
20135 matching_parens parens;
20136 parens.consume_open (parser);
20137 if (first)
20139 /* If this is going to be an abstract declarator, we're
20140 in a declarator and we can't have default args. */
20141 parser->default_arg_ok_p = false;
20142 parser->in_declarator_p = true;
20145 begin_scope (sk_function_parms, NULL_TREE);
20147 /* Parse the parameter-declaration-clause. */
20148 params = cp_parser_parameter_declaration_clause (parser);
20150 /* Consume the `)'. */
20151 parens.require_close (parser);
20153 /* If all went well, parse the cv-qualifier-seq,
20154 ref-qualifier and the exception-specification. */
20155 if (member_p || cp_parser_parse_definitely (parser))
20157 cp_cv_quals cv_quals;
20158 cp_virt_specifiers virt_specifiers;
20159 cp_ref_qualifier ref_qual;
20160 tree exception_specification;
20161 tree late_return;
20162 tree attrs;
20163 bool memfn = (member_p || (pushed_scope
20164 && CLASS_TYPE_P (pushed_scope)));
20166 is_declarator = true;
20168 if (ctor_dtor_or_conv_p)
20169 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20170 first = false;
20172 /* Parse the cv-qualifier-seq. */
20173 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20174 /* Parse the ref-qualifier. */
20175 ref_qual = cp_parser_ref_qualifier_opt (parser);
20176 /* Parse the tx-qualifier. */
20177 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20178 /* And the exception-specification. */
20179 exception_specification
20180 = cp_parser_exception_specification_opt (parser);
20182 attrs = cp_parser_std_attribute_spec_seq (parser);
20184 /* In here, we handle cases where attribute is used after
20185 the function declaration. For example:
20186 void func (int x) __attribute__((vector(..))); */
20187 tree gnu_attrs = NULL_TREE;
20188 tree requires_clause = NULL_TREE;
20189 late_return = (cp_parser_late_return_type_opt
20190 (parser, declarator, requires_clause,
20191 memfn ? cv_quals : -1));
20193 /* Parse the virt-specifier-seq. */
20194 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20196 /* Create the function-declarator. */
20197 declarator = make_call_declarator (declarator,
20198 params,
20199 cv_quals,
20200 virt_specifiers,
20201 ref_qual,
20202 tx_qual,
20203 exception_specification,
20204 late_return,
20205 requires_clause);
20206 declarator->std_attributes = attrs;
20207 declarator->attributes = gnu_attrs;
20208 /* Any subsequent parameter lists are to do with
20209 return type, so are not those of the declared
20210 function. */
20211 parser->default_arg_ok_p = false;
20214 /* Remove the function parms from scope. */
20215 pop_bindings_and_leave_scope ();
20217 if (is_declarator)
20218 /* Repeat the main loop. */
20219 continue;
20222 /* If this is the first, we can try a parenthesized
20223 declarator. */
20224 if (first)
20226 bool saved_in_type_id_in_expr_p;
20228 parser->default_arg_ok_p = saved_default_arg_ok_p;
20229 parser->in_declarator_p = saved_in_declarator_p;
20231 open_paren = token;
20232 /* Consume the `('. */
20233 matching_parens parens;
20234 parens.consume_open (parser);
20235 /* Parse the nested declarator. */
20236 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20237 parser->in_type_id_in_expr_p = true;
20238 declarator
20239 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20240 /*parenthesized_p=*/NULL,
20241 member_p, friend_p);
20242 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20243 first = false;
20244 /* Expect a `)'. */
20245 close_paren = cp_lexer_peek_token (parser->lexer);
20246 if (!parens.require_close (parser))
20247 declarator = cp_error_declarator;
20248 if (declarator == cp_error_declarator)
20249 break;
20251 goto handle_declarator;
20253 /* Otherwise, we must be done. */
20254 else
20255 break;
20257 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20258 && token->type == CPP_OPEN_SQUARE
20259 && !cp_next_tokens_can_be_attribute_p (parser))
20261 /* Parse an array-declarator. */
20262 tree bounds, attrs;
20264 if (ctor_dtor_or_conv_p)
20265 *ctor_dtor_or_conv_p = 0;
20267 open_paren = NULL;
20268 first = false;
20269 parser->default_arg_ok_p = false;
20270 parser->in_declarator_p = true;
20271 /* Consume the `['. */
20272 cp_lexer_consume_token (parser->lexer);
20273 /* Peek at the next token. */
20274 token = cp_lexer_peek_token (parser->lexer);
20275 /* If the next token is `]', then there is no
20276 constant-expression. */
20277 if (token->type != CPP_CLOSE_SQUARE)
20279 bool non_constant_p;
20280 bounds
20281 = cp_parser_constant_expression (parser,
20282 /*allow_non_constant=*/true,
20283 &non_constant_p);
20284 if (!non_constant_p)
20285 /* OK */;
20286 else if (error_operand_p (bounds))
20287 /* Already gave an error. */;
20288 else if (!parser->in_function_body
20289 || current_binding_level->kind == sk_function_parms)
20291 /* Normally, the array bound must be an integral constant
20292 expression. However, as an extension, we allow VLAs
20293 in function scopes as long as they aren't part of a
20294 parameter declaration. */
20295 cp_parser_error (parser,
20296 "array bound is not an integer constant");
20297 bounds = error_mark_node;
20299 else if (processing_template_decl
20300 && !type_dependent_expression_p (bounds))
20302 /* Remember this wasn't a constant-expression. */
20303 bounds = build_nop (TREE_TYPE (bounds), bounds);
20304 TREE_SIDE_EFFECTS (bounds) = 1;
20307 else
20308 bounds = NULL_TREE;
20309 /* Look for the closing `]'. */
20310 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20312 declarator = cp_error_declarator;
20313 break;
20316 attrs = cp_parser_std_attribute_spec_seq (parser);
20317 declarator = make_array_declarator (declarator, bounds);
20318 declarator->std_attributes = attrs;
20320 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20323 tree qualifying_scope;
20324 tree unqualified_name;
20325 tree attrs;
20326 special_function_kind sfk;
20327 bool abstract_ok;
20328 bool pack_expansion_p = false;
20329 cp_token *declarator_id_start_token;
20331 /* Parse a declarator-id */
20332 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20333 if (abstract_ok)
20335 cp_parser_parse_tentatively (parser);
20337 /* If we see an ellipsis, we should be looking at a
20338 parameter pack. */
20339 if (token->type == CPP_ELLIPSIS)
20341 /* Consume the `...' */
20342 cp_lexer_consume_token (parser->lexer);
20344 pack_expansion_p = true;
20348 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20349 unqualified_name
20350 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20351 qualifying_scope = parser->scope;
20352 if (abstract_ok)
20354 bool okay = false;
20356 if (!unqualified_name && pack_expansion_p)
20358 /* Check whether an error occurred. */
20359 okay = !cp_parser_error_occurred (parser);
20361 /* We already consumed the ellipsis to mark a
20362 parameter pack, but we have no way to report it,
20363 so abort the tentative parse. We will be exiting
20364 immediately anyway. */
20365 cp_parser_abort_tentative_parse (parser);
20367 else
20368 okay = cp_parser_parse_definitely (parser);
20370 if (!okay)
20371 unqualified_name = error_mark_node;
20372 else if (unqualified_name
20373 && (qualifying_scope
20374 || (!identifier_p (unqualified_name))))
20376 cp_parser_error (parser, "expected unqualified-id");
20377 unqualified_name = error_mark_node;
20381 if (!unqualified_name)
20382 return NULL;
20383 if (unqualified_name == error_mark_node)
20385 declarator = cp_error_declarator;
20386 pack_expansion_p = false;
20387 declarator->parameter_pack_p = false;
20388 break;
20391 attrs = cp_parser_std_attribute_spec_seq (parser);
20393 if (qualifying_scope && at_namespace_scope_p ()
20394 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20396 /* In the declaration of a member of a template class
20397 outside of the class itself, the SCOPE will sometimes
20398 be a TYPENAME_TYPE. For example, given:
20400 template <typename T>
20401 int S<T>::R::i = 3;
20403 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20404 this context, we must resolve S<T>::R to an ordinary
20405 type, rather than a typename type.
20407 The reason we normally avoid resolving TYPENAME_TYPEs
20408 is that a specialization of `S' might render
20409 `S<T>::R' not a type. However, if `S' is
20410 specialized, then this `i' will not be used, so there
20411 is no harm in resolving the types here. */
20412 tree type;
20414 /* Resolve the TYPENAME_TYPE. */
20415 type = resolve_typename_type (qualifying_scope,
20416 /*only_current_p=*/false);
20417 /* If that failed, the declarator is invalid. */
20418 if (TREE_CODE (type) == TYPENAME_TYPE)
20420 if (typedef_variant_p (type))
20421 error_at (declarator_id_start_token->location,
20422 "cannot define member of dependent typedef "
20423 "%qT", type);
20424 else
20425 error_at (declarator_id_start_token->location,
20426 "%<%T::%E%> is not a type",
20427 TYPE_CONTEXT (qualifying_scope),
20428 TYPE_IDENTIFIER (qualifying_scope));
20430 qualifying_scope = type;
20433 sfk = sfk_none;
20435 if (unqualified_name)
20437 tree class_type;
20439 if (qualifying_scope
20440 && CLASS_TYPE_P (qualifying_scope))
20441 class_type = qualifying_scope;
20442 else
20443 class_type = current_class_type;
20445 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20447 tree name_type = TREE_TYPE (unqualified_name);
20449 if (!class_type || !same_type_p (name_type, class_type))
20451 /* We do not attempt to print the declarator
20452 here because we do not have enough
20453 information about its original syntactic
20454 form. */
20455 cp_parser_error (parser, "invalid declarator");
20456 declarator = cp_error_declarator;
20457 break;
20459 else if (qualifying_scope
20460 && CLASSTYPE_USE_TEMPLATE (name_type))
20462 error_at (declarator_id_start_token->location,
20463 "invalid use of constructor as a template");
20464 inform (declarator_id_start_token->location,
20465 "use %<%T::%D%> instead of %<%T::%D%> to "
20466 "name the constructor in a qualified name",
20467 class_type,
20468 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20469 class_type, name_type);
20470 declarator = cp_error_declarator;
20471 break;
20473 unqualified_name = constructor_name (class_type);
20476 if (class_type)
20478 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20479 sfk = sfk_destructor;
20480 else if (identifier_p (unqualified_name)
20481 && IDENTIFIER_CONV_OP_P (unqualified_name))
20482 sfk = sfk_conversion;
20483 else if (/* There's no way to declare a constructor
20484 for an unnamed type, even if the type
20485 got a name for linkage purposes. */
20486 !TYPE_WAS_UNNAMED (class_type)
20487 /* Handle correctly (c++/19200):
20489 struct S {
20490 struct T{};
20491 friend void S(T);
20494 and also:
20496 namespace N {
20497 void S();
20500 struct S {
20501 friend void N::S();
20502 }; */
20503 && (!friend_p || class_type == qualifying_scope)
20504 && constructor_name_p (unqualified_name,
20505 class_type))
20506 sfk = sfk_constructor;
20507 else if (is_overloaded_fn (unqualified_name)
20508 && DECL_CONSTRUCTOR_P (get_first_fn
20509 (unqualified_name)))
20510 sfk = sfk_constructor;
20512 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20513 *ctor_dtor_or_conv_p = -1;
20516 declarator = make_id_declarator (qualifying_scope,
20517 unqualified_name,
20518 sfk);
20519 declarator->std_attributes = attrs;
20520 declarator->id_loc = token->location;
20521 declarator->parameter_pack_p = pack_expansion_p;
20523 if (pack_expansion_p)
20524 maybe_warn_variadic_templates ();
20527 handle_declarator:;
20528 scope = get_scope_of_declarator (declarator);
20529 if (scope)
20531 /* Any names that appear after the declarator-id for a
20532 member are looked up in the containing scope. */
20533 if (at_function_scope_p ())
20535 /* But declarations with qualified-ids can't appear in a
20536 function. */
20537 cp_parser_error (parser, "qualified-id in declaration");
20538 declarator = cp_error_declarator;
20539 break;
20541 pushed_scope = push_scope (scope);
20543 parser->in_declarator_p = true;
20544 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20545 || (declarator && declarator->kind == cdk_id))
20546 /* Default args are only allowed on function
20547 declarations. */
20548 parser->default_arg_ok_p = saved_default_arg_ok_p;
20549 else
20550 parser->default_arg_ok_p = false;
20552 first = false;
20554 /* We're done. */
20555 else
20556 break;
20559 /* For an abstract declarator, we might wind up with nothing at this
20560 point. That's an error; the declarator is not optional. */
20561 if (!declarator)
20562 cp_parser_error (parser, "expected declarator");
20563 else if (open_paren)
20565 /* Record overly parenthesized declarator so we can give a
20566 diagnostic about confusing decl/expr disambiguation. */
20567 if (declarator->kind == cdk_array)
20569 /* If the open and close parens are on different lines, this
20570 is probably a formatting thing, so ignore. */
20571 expanded_location open = expand_location (open_paren->location);
20572 expanded_location close = expand_location (close_paren->location);
20573 if (open.line != close.line || open.file != close.file)
20574 open_paren = NULL;
20576 if (open_paren)
20577 declarator->parenthesized = open_paren->location;
20580 /* If we entered a scope, we must exit it now. */
20581 if (pushed_scope)
20582 pop_scope (pushed_scope);
20584 parser->default_arg_ok_p = saved_default_arg_ok_p;
20585 parser->in_declarator_p = saved_in_declarator_p;
20587 return declarator;
20590 /* Parse a ptr-operator.
20592 ptr-operator:
20593 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20594 * cv-qualifier-seq [opt]
20596 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20597 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20599 GNU Extension:
20601 ptr-operator:
20602 & cv-qualifier-seq [opt]
20604 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20605 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20606 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20607 filled in with the TYPE containing the member. *CV_QUALS is
20608 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20609 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20610 Note that the tree codes returned by this function have nothing
20611 to do with the types of trees that will be eventually be created
20612 to represent the pointer or reference type being parsed. They are
20613 just constants with suggestive names. */
20614 static enum tree_code
20615 cp_parser_ptr_operator (cp_parser* parser,
20616 tree* type,
20617 cp_cv_quals *cv_quals,
20618 tree *attributes)
20620 enum tree_code code = ERROR_MARK;
20621 cp_token *token;
20622 tree attrs = NULL_TREE;
20624 /* Assume that it's not a pointer-to-member. */
20625 *type = NULL_TREE;
20626 /* And that there are no cv-qualifiers. */
20627 *cv_quals = TYPE_UNQUALIFIED;
20629 /* Peek at the next token. */
20630 token = cp_lexer_peek_token (parser->lexer);
20632 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20633 if (token->type == CPP_MULT)
20634 code = INDIRECT_REF;
20635 else if (token->type == CPP_AND)
20636 code = ADDR_EXPR;
20637 else if ((cxx_dialect != cxx98) &&
20638 token->type == CPP_AND_AND) /* C++0x only */
20639 code = NON_LVALUE_EXPR;
20641 if (code != ERROR_MARK)
20643 /* Consume the `*', `&' or `&&'. */
20644 cp_lexer_consume_token (parser->lexer);
20646 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20647 `&', if we are allowing GNU extensions. (The only qualifier
20648 that can legally appear after `&' is `restrict', but that is
20649 enforced during semantic analysis. */
20650 if (code == INDIRECT_REF
20651 || cp_parser_allow_gnu_extensions_p (parser))
20652 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20654 attrs = cp_parser_std_attribute_spec_seq (parser);
20655 if (attributes != NULL)
20656 *attributes = attrs;
20658 else
20660 /* Try the pointer-to-member case. */
20661 cp_parser_parse_tentatively (parser);
20662 /* Look for the optional `::' operator. */
20663 cp_parser_global_scope_opt (parser,
20664 /*current_scope_valid_p=*/false);
20665 /* Look for the nested-name specifier. */
20666 token = cp_lexer_peek_token (parser->lexer);
20667 cp_parser_nested_name_specifier (parser,
20668 /*typename_keyword_p=*/false,
20669 /*check_dependency_p=*/true,
20670 /*type_p=*/false,
20671 /*is_declaration=*/false);
20672 /* If we found it, and the next token is a `*', then we are
20673 indeed looking at a pointer-to-member operator. */
20674 if (!cp_parser_error_occurred (parser)
20675 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20677 /* Indicate that the `*' operator was used. */
20678 code = INDIRECT_REF;
20680 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20681 error_at (token->location, "%qD is a namespace", parser->scope);
20682 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20683 error_at (token->location, "cannot form pointer to member of "
20684 "non-class %q#T", parser->scope);
20685 else
20687 /* The type of which the member is a member is given by the
20688 current SCOPE. */
20689 *type = parser->scope;
20690 /* The next name will not be qualified. */
20691 parser->scope = NULL_TREE;
20692 parser->qualifying_scope = NULL_TREE;
20693 parser->object_scope = NULL_TREE;
20694 /* Look for optional c++11 attributes. */
20695 attrs = cp_parser_std_attribute_spec_seq (parser);
20696 if (attributes != NULL)
20697 *attributes = attrs;
20698 /* Look for the optional cv-qualifier-seq. */
20699 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20702 /* If that didn't work we don't have a ptr-operator. */
20703 if (!cp_parser_parse_definitely (parser))
20704 cp_parser_error (parser, "expected ptr-operator");
20707 return code;
20710 /* Parse an (optional) cv-qualifier-seq.
20712 cv-qualifier-seq:
20713 cv-qualifier cv-qualifier-seq [opt]
20715 cv-qualifier:
20716 const
20717 volatile
20719 GNU Extension:
20721 cv-qualifier:
20722 __restrict__
20724 Returns a bitmask representing the cv-qualifiers. */
20726 static cp_cv_quals
20727 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20729 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20731 while (true)
20733 cp_token *token;
20734 cp_cv_quals cv_qualifier;
20736 /* Peek at the next token. */
20737 token = cp_lexer_peek_token (parser->lexer);
20738 /* See if it's a cv-qualifier. */
20739 switch (token->keyword)
20741 case RID_CONST:
20742 cv_qualifier = TYPE_QUAL_CONST;
20743 break;
20745 case RID_VOLATILE:
20746 cv_qualifier = TYPE_QUAL_VOLATILE;
20747 break;
20749 case RID_RESTRICT:
20750 cv_qualifier = TYPE_QUAL_RESTRICT;
20751 break;
20753 default:
20754 cv_qualifier = TYPE_UNQUALIFIED;
20755 break;
20758 if (!cv_qualifier)
20759 break;
20761 if (cv_quals & cv_qualifier)
20763 gcc_rich_location richloc (token->location);
20764 richloc.add_fixit_remove ();
20765 error_at (&richloc, "duplicate cv-qualifier");
20766 cp_lexer_purge_token (parser->lexer);
20768 else
20770 cp_lexer_consume_token (parser->lexer);
20771 cv_quals |= cv_qualifier;
20775 return cv_quals;
20778 /* Parse an (optional) ref-qualifier
20780 ref-qualifier:
20784 Returns cp_ref_qualifier representing ref-qualifier. */
20786 static cp_ref_qualifier
20787 cp_parser_ref_qualifier_opt (cp_parser* parser)
20789 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20791 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20792 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20793 return ref_qual;
20795 while (true)
20797 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20798 cp_token *token = cp_lexer_peek_token (parser->lexer);
20800 switch (token->type)
20802 case CPP_AND:
20803 curr_ref_qual = REF_QUAL_LVALUE;
20804 break;
20806 case CPP_AND_AND:
20807 curr_ref_qual = REF_QUAL_RVALUE;
20808 break;
20810 default:
20811 curr_ref_qual = REF_QUAL_NONE;
20812 break;
20815 if (!curr_ref_qual)
20816 break;
20817 else if (ref_qual)
20819 error_at (token->location, "multiple ref-qualifiers");
20820 cp_lexer_purge_token (parser->lexer);
20822 else
20824 ref_qual = curr_ref_qual;
20825 cp_lexer_consume_token (parser->lexer);
20829 return ref_qual;
20832 /* Parse an optional tx-qualifier.
20834 tx-qualifier:
20835 transaction_safe
20836 transaction_safe_dynamic */
20838 static tree
20839 cp_parser_tx_qualifier_opt (cp_parser *parser)
20841 cp_token *token = cp_lexer_peek_token (parser->lexer);
20842 if (token->type == CPP_NAME)
20844 tree name = token->u.value;
20845 const char *p = IDENTIFIER_POINTER (name);
20846 const int len = strlen ("transaction_safe");
20847 if (!strncmp (p, "transaction_safe", len))
20849 p += len;
20850 if (*p == '\0'
20851 || !strcmp (p, "_dynamic"))
20853 cp_lexer_consume_token (parser->lexer);
20854 if (!flag_tm)
20856 error ("%qE requires %<-fgnu-tm%>", name);
20857 return NULL_TREE;
20859 else
20860 return name;
20864 return NULL_TREE;
20867 /* Parse an (optional) virt-specifier-seq.
20869 virt-specifier-seq:
20870 virt-specifier virt-specifier-seq [opt]
20872 virt-specifier:
20873 override
20874 final
20876 Returns a bitmask representing the virt-specifiers. */
20878 static cp_virt_specifiers
20879 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20881 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20883 while (true)
20885 cp_token *token;
20886 cp_virt_specifiers virt_specifier;
20888 /* Peek at the next token. */
20889 token = cp_lexer_peek_token (parser->lexer);
20890 /* See if it's a virt-specifier-qualifier. */
20891 if (token->type != CPP_NAME)
20892 break;
20893 if (id_equal (token->u.value, "override"))
20895 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20896 virt_specifier = VIRT_SPEC_OVERRIDE;
20898 else if (id_equal (token->u.value, "final"))
20900 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20901 virt_specifier = VIRT_SPEC_FINAL;
20903 else if (id_equal (token->u.value, "__final"))
20905 virt_specifier = VIRT_SPEC_FINAL;
20907 else
20908 break;
20910 if (virt_specifiers & virt_specifier)
20912 gcc_rich_location richloc (token->location);
20913 richloc.add_fixit_remove ();
20914 error_at (&richloc, "duplicate virt-specifier");
20915 cp_lexer_purge_token (parser->lexer);
20917 else
20919 cp_lexer_consume_token (parser->lexer);
20920 virt_specifiers |= virt_specifier;
20923 return virt_specifiers;
20926 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20927 is in scope even though it isn't real. */
20929 void
20930 inject_this_parameter (tree ctype, cp_cv_quals quals)
20932 tree this_parm;
20934 if (current_class_ptr)
20936 /* We don't clear this between NSDMIs. Is it already what we want? */
20937 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20938 if (DECL_P (current_class_ptr)
20939 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20940 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20941 && cp_type_quals (type) == quals)
20942 return;
20945 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20946 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20947 current_class_ptr = NULL_TREE;
20948 current_class_ref
20949 = cp_build_fold_indirect_ref (this_parm);
20950 current_class_ptr = this_parm;
20953 /* Return true iff our current scope is a non-static data member
20954 initializer. */
20956 bool
20957 parsing_nsdmi (void)
20959 /* We recognize NSDMI context by the context-less 'this' pointer set up
20960 by the function above. */
20961 if (current_class_ptr
20962 && TREE_CODE (current_class_ptr) == PARM_DECL
20963 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20964 return true;
20965 return false;
20968 /* Parse a late-specified return type, if any. This is not a separate
20969 non-terminal, but part of a function declarator, which looks like
20971 -> trailing-type-specifier-seq abstract-declarator(opt)
20973 Returns the type indicated by the type-id.
20975 In addition to this, parse any queued up #pragma omp declare simd
20976 clauses, and #pragma acc routine clauses.
20978 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20979 function. */
20981 static tree
20982 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20983 tree& requires_clause, cp_cv_quals quals)
20985 cp_token *token;
20986 tree type = NULL_TREE;
20987 bool declare_simd_p = (parser->omp_declare_simd
20988 && declarator
20989 && declarator->kind == cdk_id);
20991 bool oacc_routine_p = (parser->oacc_routine
20992 && declarator
20993 && declarator->kind == cdk_id);
20995 /* Peek at the next token. */
20996 token = cp_lexer_peek_token (parser->lexer);
20997 /* A late-specified return type is indicated by an initial '->'. */
20998 if (token->type != CPP_DEREF
20999 && token->keyword != RID_REQUIRES
21000 && !(token->type == CPP_NAME
21001 && token->u.value == ridpointers[RID_REQUIRES])
21002 && !(declare_simd_p || oacc_routine_p))
21003 return NULL_TREE;
21005 tree save_ccp = current_class_ptr;
21006 tree save_ccr = current_class_ref;
21007 if (quals >= 0)
21009 /* DR 1207: 'this' is in scope in the trailing return type. */
21010 inject_this_parameter (current_class_type, quals);
21013 if (token->type == CPP_DEREF)
21015 /* Consume the ->. */
21016 cp_lexer_consume_token (parser->lexer);
21018 type = cp_parser_trailing_type_id (parser);
21021 /* Function declarations may be followed by a trailing
21022 requires-clause. */
21023 requires_clause = cp_parser_requires_clause_opt (parser);
21025 if (declare_simd_p)
21026 declarator->attributes
21027 = cp_parser_late_parsing_omp_declare_simd (parser,
21028 declarator->attributes);
21029 if (oacc_routine_p)
21030 declarator->attributes
21031 = cp_parser_late_parsing_oacc_routine (parser,
21032 declarator->attributes);
21034 if (quals >= 0)
21036 current_class_ptr = save_ccp;
21037 current_class_ref = save_ccr;
21040 return type;
21043 /* Parse a declarator-id.
21045 declarator-id:
21046 id-expression
21047 :: [opt] nested-name-specifier [opt] type-name
21049 In the `id-expression' case, the value returned is as for
21050 cp_parser_id_expression if the id-expression was an unqualified-id.
21051 If the id-expression was a qualified-id, then a SCOPE_REF is
21052 returned. The first operand is the scope (either a NAMESPACE_DECL
21053 or TREE_TYPE), but the second is still just a representation of an
21054 unqualified-id. */
21056 static tree
21057 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21059 tree id;
21060 /* The expression must be an id-expression. Assume that qualified
21061 names are the names of types so that:
21063 template <class T>
21064 int S<T>::R::i = 3;
21066 will work; we must treat `S<T>::R' as the name of a type.
21067 Similarly, assume that qualified names are templates, where
21068 required, so that:
21070 template <class T>
21071 int S<T>::R<T>::i = 3;
21073 will work, too. */
21074 id = cp_parser_id_expression (parser,
21075 /*template_keyword_p=*/false,
21076 /*check_dependency_p=*/false,
21077 /*template_p=*/NULL,
21078 /*declarator_p=*/true,
21079 optional_p);
21080 if (id && BASELINK_P (id))
21081 id = BASELINK_FUNCTIONS (id);
21082 return id;
21085 /* Parse a type-id.
21087 type-id:
21088 type-specifier-seq abstract-declarator [opt]
21090 Returns the TYPE specified. */
21092 static tree
21093 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21094 bool is_trailing_return)
21096 cp_decl_specifier_seq type_specifier_seq;
21097 cp_declarator *abstract_declarator;
21099 /* Parse the type-specifier-seq. */
21100 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21101 is_trailing_return,
21102 &type_specifier_seq);
21103 if (is_template_arg && type_specifier_seq.type
21104 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21105 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21106 /* A bare template name as a template argument is a template template
21107 argument, not a placeholder, so fail parsing it as a type argument. */
21109 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21110 cp_parser_simulate_error (parser);
21111 return error_mark_node;
21113 if (type_specifier_seq.type == error_mark_node)
21114 return error_mark_node;
21116 /* There might or might not be an abstract declarator. */
21117 cp_parser_parse_tentatively (parser);
21118 /* Look for the declarator. */
21119 abstract_declarator
21120 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21121 /*parenthesized_p=*/NULL,
21122 /*member_p=*/false,
21123 /*friend_p=*/false);
21124 /* Check to see if there really was a declarator. */
21125 if (!cp_parser_parse_definitely (parser))
21126 abstract_declarator = NULL;
21128 if (type_specifier_seq.type
21129 /* The concepts TS allows 'auto' as a type-id. */
21130 && (!flag_concepts || parser->in_type_id_in_expr_p)
21131 /* None of the valid uses of 'auto' in C++14 involve the type-id
21132 nonterminal, but it is valid in a trailing-return-type. */
21133 && !(cxx_dialect >= cxx14 && is_trailing_return))
21134 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21136 /* A type-id with type 'auto' is only ok if the abstract declarator
21137 is a function declarator with a late-specified return type.
21139 A type-id with 'auto' is also valid in a trailing-return-type
21140 in a compound-requirement. */
21141 if (abstract_declarator
21142 && abstract_declarator->kind == cdk_function
21143 && abstract_declarator->u.function.late_return_type)
21144 /* OK */;
21145 else if (parser->in_result_type_constraint_p)
21146 /* OK */;
21147 else
21149 location_t loc = type_specifier_seq.locations[ds_type_spec];
21150 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21152 error_at (loc, "missing template arguments after %qT",
21153 auto_node);
21154 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21155 tmpl);
21157 else
21158 error_at (loc, "invalid use of %qT", auto_node);
21159 return error_mark_node;
21163 return groktypename (&type_specifier_seq, abstract_declarator,
21164 is_template_arg);
21167 static tree
21168 cp_parser_type_id (cp_parser *parser)
21170 return cp_parser_type_id_1 (parser, false, false);
21173 static tree
21174 cp_parser_template_type_arg (cp_parser *parser)
21176 tree r;
21177 const char *saved_message = parser->type_definition_forbidden_message;
21178 parser->type_definition_forbidden_message
21179 = G_("types may not be defined in template arguments");
21180 r = cp_parser_type_id_1 (parser, true, false);
21181 parser->type_definition_forbidden_message = saved_message;
21182 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21184 error ("invalid use of %<auto%> in template argument");
21185 r = error_mark_node;
21187 return r;
21190 static tree
21191 cp_parser_trailing_type_id (cp_parser *parser)
21193 return cp_parser_type_id_1 (parser, false, true);
21196 /* Parse a type-specifier-seq.
21198 type-specifier-seq:
21199 type-specifier type-specifier-seq [opt]
21201 GNU extension:
21203 type-specifier-seq:
21204 attributes type-specifier-seq [opt]
21206 If IS_DECLARATION is true, we are at the start of a "condition" or
21207 exception-declaration, so we might be followed by a declarator-id.
21209 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21210 i.e. we've just seen "->".
21212 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21214 static void
21215 cp_parser_type_specifier_seq (cp_parser* parser,
21216 bool is_declaration,
21217 bool is_trailing_return,
21218 cp_decl_specifier_seq *type_specifier_seq)
21220 bool seen_type_specifier = false;
21221 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21222 cp_token *start_token = NULL;
21224 /* Clear the TYPE_SPECIFIER_SEQ. */
21225 clear_decl_specs (type_specifier_seq);
21227 /* In the context of a trailing return type, enum E { } is an
21228 elaborated-type-specifier followed by a function-body, not an
21229 enum-specifier. */
21230 if (is_trailing_return)
21231 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21233 /* Parse the type-specifiers and attributes. */
21234 while (true)
21236 tree type_specifier;
21237 bool is_cv_qualifier;
21239 /* Check for attributes first. */
21240 if (cp_next_tokens_can_be_attribute_p (parser))
21242 type_specifier_seq->attributes
21243 = attr_chainon (type_specifier_seq->attributes,
21244 cp_parser_attributes_opt (parser));
21245 continue;
21248 /* record the token of the beginning of the type specifier seq,
21249 for error reporting purposes*/
21250 if (!start_token)
21251 start_token = cp_lexer_peek_token (parser->lexer);
21253 /* Look for the type-specifier. */
21254 type_specifier = cp_parser_type_specifier (parser,
21255 flags,
21256 type_specifier_seq,
21257 /*is_declaration=*/false,
21258 NULL,
21259 &is_cv_qualifier);
21260 if (!type_specifier)
21262 /* If the first type-specifier could not be found, this is not a
21263 type-specifier-seq at all. */
21264 if (!seen_type_specifier)
21266 /* Set in_declarator_p to avoid skipping to the semicolon. */
21267 int in_decl = parser->in_declarator_p;
21268 parser->in_declarator_p = true;
21270 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21271 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21272 cp_parser_error (parser, "expected type-specifier");
21274 parser->in_declarator_p = in_decl;
21276 type_specifier_seq->type = error_mark_node;
21277 return;
21279 /* If subsequent type-specifiers could not be found, the
21280 type-specifier-seq is complete. */
21281 break;
21284 seen_type_specifier = true;
21285 /* The standard says that a condition can be:
21287 type-specifier-seq declarator = assignment-expression
21289 However, given:
21291 struct S {};
21292 if (int S = ...)
21294 we should treat the "S" as a declarator, not as a
21295 type-specifier. The standard doesn't say that explicitly for
21296 type-specifier-seq, but it does say that for
21297 decl-specifier-seq in an ordinary declaration. Perhaps it
21298 would be clearer just to allow a decl-specifier-seq here, and
21299 then add a semantic restriction that if any decl-specifiers
21300 that are not type-specifiers appear, the program is invalid. */
21301 if (is_declaration && !is_cv_qualifier)
21302 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21306 /* Return whether the function currently being declared has an associated
21307 template parameter list. */
21309 static bool
21310 function_being_declared_is_template_p (cp_parser* parser)
21312 if (!current_template_parms || processing_template_parmlist)
21313 return false;
21315 if (parser->implicit_template_scope)
21316 return true;
21318 if (at_class_scope_p ()
21319 && TYPE_BEING_DEFINED (current_class_type))
21320 return parser->num_template_parameter_lists != 0;
21322 return ((int) parser->num_template_parameter_lists > template_class_depth
21323 (current_class_type));
21326 /* Parse a parameter-declaration-clause.
21328 parameter-declaration-clause:
21329 parameter-declaration-list [opt] ... [opt]
21330 parameter-declaration-list , ...
21332 Returns a representation for the parameter declarations. A return
21333 value of NULL indicates a parameter-declaration-clause consisting
21334 only of an ellipsis. */
21336 static tree
21337 cp_parser_parameter_declaration_clause (cp_parser* parser)
21339 tree parameters;
21340 cp_token *token;
21341 bool ellipsis_p;
21343 temp_override<bool> cleanup
21344 (parser->auto_is_implicit_function_template_parm_p);
21346 if (!processing_specialization
21347 && !processing_template_parmlist
21348 && !processing_explicit_instantiation
21349 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21350 actual function or a random abstract declarator. */
21351 && parser->default_arg_ok_p)
21352 if (!current_function_decl
21353 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21354 parser->auto_is_implicit_function_template_parm_p = true;
21356 /* Peek at the next token. */
21357 token = cp_lexer_peek_token (parser->lexer);
21358 /* Check for trivial parameter-declaration-clauses. */
21359 if (token->type == CPP_ELLIPSIS)
21361 /* Consume the `...' token. */
21362 cp_lexer_consume_token (parser->lexer);
21363 return NULL_TREE;
21365 else if (token->type == CPP_CLOSE_PAREN)
21366 /* There are no parameters. */
21368 #ifdef SYSTEM_IMPLICIT_EXTERN_C
21369 if (in_system_header_at (input_location)
21370 && current_class_type == NULL
21371 && current_lang_name == lang_name_c)
21372 return NULL_TREE;
21373 else
21374 #endif
21375 return void_list_node;
21377 /* Check for `(void)', too, which is a special case. */
21378 else if (token->keyword == RID_VOID
21379 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21380 == CPP_CLOSE_PAREN))
21382 /* Consume the `void' token. */
21383 cp_lexer_consume_token (parser->lexer);
21384 /* There are no parameters. */
21385 return void_list_node;
21388 /* Parse the parameter-declaration-list. */
21389 parameters = cp_parser_parameter_declaration_list (parser);
21390 /* If a parse error occurred while parsing the
21391 parameter-declaration-list, then the entire
21392 parameter-declaration-clause is erroneous. */
21393 if (parameters == error_mark_node)
21394 return NULL_TREE;
21396 /* Peek at the next token. */
21397 token = cp_lexer_peek_token (parser->lexer);
21398 /* If it's a `,', the clause should terminate with an ellipsis. */
21399 if (token->type == CPP_COMMA)
21401 /* Consume the `,'. */
21402 cp_lexer_consume_token (parser->lexer);
21403 /* Expect an ellipsis. */
21404 ellipsis_p
21405 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21407 /* It might also be `...' if the optional trailing `,' was
21408 omitted. */
21409 else if (token->type == CPP_ELLIPSIS)
21411 /* Consume the `...' token. */
21412 cp_lexer_consume_token (parser->lexer);
21413 /* And remember that we saw it. */
21414 ellipsis_p = true;
21416 else
21417 ellipsis_p = false;
21419 /* Finish the parameter list. */
21420 if (!ellipsis_p)
21421 parameters = chainon (parameters, void_list_node);
21423 return parameters;
21426 /* Parse a parameter-declaration-list.
21428 parameter-declaration-list:
21429 parameter-declaration
21430 parameter-declaration-list , parameter-declaration
21432 Returns a representation of the parameter-declaration-list, as for
21433 cp_parser_parameter_declaration_clause. However, the
21434 `void_list_node' is never appended to the list. */
21436 static tree
21437 cp_parser_parameter_declaration_list (cp_parser* parser)
21439 tree parameters = NULL_TREE;
21440 tree *tail = &parameters;
21441 bool saved_in_unbraced_linkage_specification_p;
21442 int index = 0;
21444 /* The special considerations that apply to a function within an
21445 unbraced linkage specifications do not apply to the parameters
21446 to the function. */
21447 saved_in_unbraced_linkage_specification_p
21448 = parser->in_unbraced_linkage_specification_p;
21449 parser->in_unbraced_linkage_specification_p = false;
21451 /* Look for more parameters. */
21452 while (true)
21454 cp_parameter_declarator *parameter;
21455 tree decl = error_mark_node;
21456 bool parenthesized_p = false;
21458 /* Parse the parameter. */
21459 parameter
21460 = cp_parser_parameter_declaration (parser,
21461 /*template_parm_p=*/false,
21462 &parenthesized_p);
21464 /* We don't know yet if the enclosing context is deprecated, so wait
21465 and warn in grokparms if appropriate. */
21466 deprecated_state = DEPRECATED_SUPPRESS;
21468 if (parameter)
21470 decl = grokdeclarator (parameter->declarator,
21471 &parameter->decl_specifiers,
21472 PARM,
21473 parameter->default_argument != NULL_TREE,
21474 &parameter->decl_specifiers.attributes);
21475 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21476 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21479 deprecated_state = DEPRECATED_NORMAL;
21481 /* If a parse error occurred parsing the parameter declaration,
21482 then the entire parameter-declaration-list is erroneous. */
21483 if (decl == error_mark_node)
21485 parameters = error_mark_node;
21486 break;
21489 if (parameter->decl_specifiers.attributes)
21490 cplus_decl_attributes (&decl,
21491 parameter->decl_specifiers.attributes,
21493 if (DECL_NAME (decl))
21494 decl = pushdecl (decl);
21496 if (decl != error_mark_node)
21498 retrofit_lang_decl (decl);
21499 DECL_PARM_INDEX (decl) = ++index;
21500 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21503 /* Add the new parameter to the list. */
21504 *tail = build_tree_list (parameter->default_argument, decl);
21505 tail = &TREE_CHAIN (*tail);
21507 /* Peek at the next token. */
21508 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21509 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21510 /* These are for Objective-C++ */
21511 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21512 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21513 /* The parameter-declaration-list is complete. */
21514 break;
21515 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21517 cp_token *token;
21519 /* Peek at the next token. */
21520 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21521 /* If it's an ellipsis, then the list is complete. */
21522 if (token->type == CPP_ELLIPSIS)
21523 break;
21524 /* Otherwise, there must be more parameters. Consume the
21525 `,'. */
21526 cp_lexer_consume_token (parser->lexer);
21527 /* When parsing something like:
21529 int i(float f, double d)
21531 we can tell after seeing the declaration for "f" that we
21532 are not looking at an initialization of a variable "i",
21533 but rather at the declaration of a function "i".
21535 Due to the fact that the parsing of template arguments
21536 (as specified to a template-id) requires backtracking we
21537 cannot use this technique when inside a template argument
21538 list. */
21539 if (!parser->in_template_argument_list_p
21540 && !parser->in_type_id_in_expr_p
21541 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21542 /* However, a parameter-declaration of the form
21543 "float(f)" (which is a valid declaration of a
21544 parameter "f") can also be interpreted as an
21545 expression (the conversion of "f" to "float"). */
21546 && !parenthesized_p)
21547 cp_parser_commit_to_tentative_parse (parser);
21549 else
21551 cp_parser_error (parser, "expected %<,%> or %<...%>");
21552 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21553 cp_parser_skip_to_closing_parenthesis (parser,
21554 /*recovering=*/true,
21555 /*or_comma=*/false,
21556 /*consume_paren=*/false);
21557 break;
21561 parser->in_unbraced_linkage_specification_p
21562 = saved_in_unbraced_linkage_specification_p;
21564 /* Reset implicit_template_scope if we are about to leave the function
21565 parameter list that introduced it. Note that for out-of-line member
21566 definitions, there will be one or more class scopes before we get to
21567 the template parameter scope. */
21569 if (cp_binding_level *its = parser->implicit_template_scope)
21570 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21572 while (maybe_its->kind == sk_class)
21573 maybe_its = maybe_its->level_chain;
21574 if (maybe_its == its)
21576 parser->implicit_template_parms = 0;
21577 parser->implicit_template_scope = 0;
21581 return parameters;
21584 /* Parse a parameter declaration.
21586 parameter-declaration:
21587 decl-specifier-seq ... [opt] declarator
21588 decl-specifier-seq declarator = assignment-expression
21589 decl-specifier-seq ... [opt] abstract-declarator [opt]
21590 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21592 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21593 declares a template parameter. (In that case, a non-nested `>'
21594 token encountered during the parsing of the assignment-expression
21595 is not interpreted as a greater-than operator.)
21597 Returns a representation of the parameter, or NULL if an error
21598 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21599 true iff the declarator is of the form "(p)". */
21601 static cp_parameter_declarator *
21602 cp_parser_parameter_declaration (cp_parser *parser,
21603 bool template_parm_p,
21604 bool *parenthesized_p)
21606 int declares_class_or_enum;
21607 cp_decl_specifier_seq decl_specifiers;
21608 cp_declarator *declarator;
21609 tree default_argument;
21610 cp_token *token = NULL, *declarator_token_start = NULL;
21611 const char *saved_message;
21612 bool template_parameter_pack_p = false;
21614 /* In a template parameter, `>' is not an operator.
21616 [temp.param]
21618 When parsing a default template-argument for a non-type
21619 template-parameter, the first non-nested `>' is taken as the end
21620 of the template parameter-list rather than a greater-than
21621 operator. */
21623 /* Type definitions may not appear in parameter types. */
21624 saved_message = parser->type_definition_forbidden_message;
21625 parser->type_definition_forbidden_message
21626 = G_("types may not be defined in parameter types");
21628 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21629 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21630 (current_template_parms)) : 0);
21632 /* Parse the declaration-specifiers. */
21633 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21634 cp_parser_decl_specifier_seq (parser,
21635 CP_PARSER_FLAGS_NONE,
21636 &decl_specifiers,
21637 &declares_class_or_enum);
21639 /* Complain about missing 'typename' or other invalid type names. */
21640 if (!decl_specifiers.any_type_specifiers_p
21641 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21642 decl_specifiers.type = error_mark_node;
21644 /* If an error occurred, there's no reason to attempt to parse the
21645 rest of the declaration. */
21646 if (cp_parser_error_occurred (parser))
21648 parser->type_definition_forbidden_message = saved_message;
21649 return NULL;
21652 /* Peek at the next token. */
21653 token = cp_lexer_peek_token (parser->lexer);
21655 /* If the next token is a `)', `,', `=', `>', or `...', then there
21656 is no declarator. However, when variadic templates are enabled,
21657 there may be a declarator following `...'. */
21658 if (token->type == CPP_CLOSE_PAREN
21659 || token->type == CPP_COMMA
21660 || token->type == CPP_EQ
21661 || token->type == CPP_GREATER)
21663 declarator = NULL;
21664 if (parenthesized_p)
21665 *parenthesized_p = false;
21667 /* Otherwise, there should be a declarator. */
21668 else
21670 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21671 parser->default_arg_ok_p = false;
21673 /* After seeing a decl-specifier-seq, if the next token is not a
21674 "(", there is no possibility that the code is a valid
21675 expression. Therefore, if parsing tentatively, we commit at
21676 this point. */
21677 if (!parser->in_template_argument_list_p
21678 /* In an expression context, having seen:
21680 (int((char ...
21682 we cannot be sure whether we are looking at a
21683 function-type (taking a "char" as a parameter) or a cast
21684 of some object of type "char" to "int". */
21685 && !parser->in_type_id_in_expr_p
21686 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21687 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21688 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21689 cp_parser_commit_to_tentative_parse (parser);
21690 /* Parse the declarator. */
21691 declarator_token_start = token;
21692 declarator = cp_parser_declarator (parser,
21693 CP_PARSER_DECLARATOR_EITHER,
21694 /*ctor_dtor_or_conv_p=*/NULL,
21695 parenthesized_p,
21696 /*member_p=*/false,
21697 /*friend_p=*/false);
21698 parser->default_arg_ok_p = saved_default_arg_ok_p;
21699 /* After the declarator, allow more attributes. */
21700 decl_specifiers.attributes
21701 = attr_chainon (decl_specifiers.attributes,
21702 cp_parser_attributes_opt (parser));
21704 /* If the declarator is a template parameter pack, remember that and
21705 clear the flag in the declarator itself so we don't get errors
21706 from grokdeclarator. */
21707 if (template_parm_p && declarator && declarator->parameter_pack_p)
21709 declarator->parameter_pack_p = false;
21710 template_parameter_pack_p = true;
21714 /* If the next token is an ellipsis, and we have not seen a declarator
21715 name, and if either the type of the declarator contains parameter
21716 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21717 for, eg, abbreviated integral type names), then we actually have a
21718 parameter pack expansion expression. Otherwise, leave the ellipsis
21719 for a C-style variadic function. */
21720 token = cp_lexer_peek_token (parser->lexer);
21722 /* If a function parameter pack was specified and an implicit template
21723 parameter was introduced during cp_parser_parameter_declaration,
21724 change any implicit parameters introduced into packs. */
21725 if (parser->implicit_template_parms
21726 && (token->type == CPP_ELLIPSIS
21727 || (declarator && declarator->parameter_pack_p)))
21729 int latest_template_parm_idx = TREE_VEC_LENGTH
21730 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21732 if (latest_template_parm_idx != template_parm_idx)
21733 decl_specifiers.type = convert_generic_types_to_packs
21734 (decl_specifiers.type,
21735 template_parm_idx, latest_template_parm_idx);
21738 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21740 tree type = decl_specifiers.type;
21742 if (type && DECL_P (type))
21743 type = TREE_TYPE (type);
21745 if (((type
21746 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21747 && (template_parm_p || uses_parameter_packs (type)))
21748 || (!type && template_parm_p))
21749 && declarator_can_be_parameter_pack (declarator))
21751 /* Consume the `...'. */
21752 cp_lexer_consume_token (parser->lexer);
21753 maybe_warn_variadic_templates ();
21755 /* Build a pack expansion type */
21756 if (template_parm_p)
21757 template_parameter_pack_p = true;
21758 else if (declarator)
21759 declarator->parameter_pack_p = true;
21760 else
21761 decl_specifiers.type = make_pack_expansion (type);
21765 /* The restriction on defining new types applies only to the type
21766 of the parameter, not to the default argument. */
21767 parser->type_definition_forbidden_message = saved_message;
21769 /* If the next token is `=', then process a default argument. */
21770 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21772 tree type = decl_specifiers.type;
21773 token = cp_lexer_peek_token (parser->lexer);
21774 /* If we are defining a class, then the tokens that make up the
21775 default argument must be saved and processed later. */
21776 if (!template_parm_p && at_class_scope_p ()
21777 && TYPE_BEING_DEFINED (current_class_type)
21778 && !LAMBDA_TYPE_P (current_class_type))
21779 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21781 // A constrained-type-specifier may declare a type template-parameter.
21782 else if (declares_constrained_type_template_parameter (type))
21783 default_argument
21784 = cp_parser_default_type_template_argument (parser);
21786 // A constrained-type-specifier may declare a template-template-parameter.
21787 else if (declares_constrained_template_template_parameter (type))
21788 default_argument
21789 = cp_parser_default_template_template_argument (parser);
21791 /* Outside of a class definition, we can just parse the
21792 assignment-expression. */
21793 else
21794 default_argument
21795 = cp_parser_default_argument (parser, template_parm_p);
21797 if (!parser->default_arg_ok_p)
21799 permerror (token->location,
21800 "default arguments are only "
21801 "permitted for function parameters");
21803 else if ((declarator && declarator->parameter_pack_p)
21804 || template_parameter_pack_p
21805 || (decl_specifiers.type
21806 && PACK_EXPANSION_P (decl_specifiers.type)))
21808 /* Find the name of the parameter pack. */
21809 cp_declarator *id_declarator = declarator;
21810 while (id_declarator && id_declarator->kind != cdk_id)
21811 id_declarator = id_declarator->declarator;
21813 if (id_declarator && id_declarator->kind == cdk_id)
21814 error_at (declarator_token_start->location,
21815 template_parm_p
21816 ? G_("template parameter pack %qD "
21817 "cannot have a default argument")
21818 : G_("parameter pack %qD cannot have "
21819 "a default argument"),
21820 id_declarator->u.id.unqualified_name);
21821 else
21822 error_at (declarator_token_start->location,
21823 template_parm_p
21824 ? G_("template parameter pack cannot have "
21825 "a default argument")
21826 : G_("parameter pack cannot have a "
21827 "default argument"));
21829 default_argument = NULL_TREE;
21832 else
21833 default_argument = NULL_TREE;
21835 /* Generate a location for the parameter, ranging from the start of the
21836 initial token to the end of the final token (using input_location for
21837 the latter, set up by cp_lexer_set_source_position_from_token when
21838 consuming tokens).
21840 If we have a identifier, then use it for the caret location, e.g.
21842 extern int callee (int one, int (*two)(int, int), float three);
21843 ~~~~~~^~~~~~~~~~~~~~
21845 otherwise, reuse the start location for the caret location e.g.:
21847 extern int callee (int one, int (*)(int, int), float three);
21848 ^~~~~~~~~~~~~~~~~
21851 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21852 ? declarator->id_loc
21853 : decl_spec_token_start->location);
21854 location_t param_loc = make_location (caret_loc,
21855 decl_spec_token_start->location,
21856 input_location);
21858 return make_parameter_declarator (&decl_specifiers,
21859 declarator,
21860 default_argument,
21861 param_loc,
21862 template_parameter_pack_p);
21865 /* Parse a default argument and return it.
21867 TEMPLATE_PARM_P is true if this is a default argument for a
21868 non-type template parameter. */
21869 static tree
21870 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21872 tree default_argument = NULL_TREE;
21873 bool saved_greater_than_is_operator_p;
21874 bool saved_local_variables_forbidden_p;
21875 bool non_constant_p, is_direct_init;
21877 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21878 set correctly. */
21879 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21880 parser->greater_than_is_operator_p = !template_parm_p;
21881 /* Local variable names (and the `this' keyword) may not
21882 appear in a default argument. */
21883 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21884 parser->local_variables_forbidden_p = true;
21885 /* Parse the assignment-expression. */
21886 if (template_parm_p)
21887 push_deferring_access_checks (dk_no_deferred);
21888 tree saved_class_ptr = NULL_TREE;
21889 tree saved_class_ref = NULL_TREE;
21890 /* The "this" pointer is not valid in a default argument. */
21891 if (cfun)
21893 saved_class_ptr = current_class_ptr;
21894 cp_function_chain->x_current_class_ptr = NULL_TREE;
21895 saved_class_ref = current_class_ref;
21896 cp_function_chain->x_current_class_ref = NULL_TREE;
21898 default_argument
21899 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21900 /* Restore the "this" pointer. */
21901 if (cfun)
21903 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21904 cp_function_chain->x_current_class_ref = saved_class_ref;
21906 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21907 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21908 if (template_parm_p)
21909 pop_deferring_access_checks ();
21910 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21911 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21913 return default_argument;
21916 /* Parse a function-body.
21918 function-body:
21919 compound_statement */
21921 static void
21922 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21924 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21925 ? BCS_TRY_BLOCK : BCS_NORMAL),
21926 true);
21929 /* Parse a ctor-initializer-opt followed by a function-body. Return
21930 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21931 is true we are parsing a function-try-block. */
21933 static void
21934 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21935 bool in_function_try_block)
21937 tree body, list;
21938 const bool check_body_p =
21939 DECL_CONSTRUCTOR_P (current_function_decl)
21940 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21941 tree last = NULL;
21943 /* Begin the function body. */
21944 body = begin_function_body ();
21945 /* Parse the optional ctor-initializer. */
21946 cp_parser_ctor_initializer_opt (parser);
21948 /* If we're parsing a constexpr constructor definition, we need
21949 to check that the constructor body is indeed empty. However,
21950 before we get to cp_parser_function_body lot of junk has been
21951 generated, so we can't just check that we have an empty block.
21952 Rather we take a snapshot of the outermost block, and check whether
21953 cp_parser_function_body changed its state. */
21954 if (check_body_p)
21956 list = cur_stmt_list;
21957 if (STATEMENT_LIST_TAIL (list))
21958 last = STATEMENT_LIST_TAIL (list)->stmt;
21960 /* Parse the function-body. */
21961 cp_parser_function_body (parser, in_function_try_block);
21962 if (check_body_p)
21963 check_constexpr_ctor_body (last, list, /*complain=*/true);
21964 /* Finish the function body. */
21965 finish_function_body (body);
21968 /* Parse an initializer.
21970 initializer:
21971 = initializer-clause
21972 ( expression-list )
21974 Returns an expression representing the initializer. If no
21975 initializer is present, NULL_TREE is returned.
21977 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21978 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21979 set to TRUE if there is no initializer present. If there is an
21980 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21981 is set to true; otherwise it is set to false. */
21983 static tree
21984 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21985 bool* non_constant_p, bool subexpression_p)
21987 cp_token *token;
21988 tree init;
21990 /* Peek at the next token. */
21991 token = cp_lexer_peek_token (parser->lexer);
21993 /* Let our caller know whether or not this initializer was
21994 parenthesized. */
21995 *is_direct_init = (token->type != CPP_EQ);
21996 /* Assume that the initializer is constant. */
21997 *non_constant_p = false;
21999 if (token->type == CPP_EQ)
22001 /* Consume the `='. */
22002 cp_lexer_consume_token (parser->lexer);
22003 /* Parse the initializer-clause. */
22004 init = cp_parser_initializer_clause (parser, non_constant_p);
22006 else if (token->type == CPP_OPEN_PAREN)
22008 vec<tree, va_gc> *vec;
22009 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22010 /*cast_p=*/false,
22011 /*allow_expansion_p=*/true,
22012 non_constant_p);
22013 if (vec == NULL)
22014 return error_mark_node;
22015 init = build_tree_list_vec (vec);
22016 release_tree_vector (vec);
22018 else if (token->type == CPP_OPEN_BRACE)
22020 cp_lexer_set_source_position (parser->lexer);
22021 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22022 init = cp_parser_braced_list (parser, non_constant_p);
22023 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22025 else
22027 /* Anything else is an error. */
22028 cp_parser_error (parser, "expected initializer");
22029 init = error_mark_node;
22032 if (!subexpression_p && check_for_bare_parameter_packs (init))
22033 init = error_mark_node;
22035 return init;
22038 /* Parse an initializer-clause.
22040 initializer-clause:
22041 assignment-expression
22042 braced-init-list
22044 Returns an expression representing the initializer.
22046 If the `assignment-expression' production is used the value
22047 returned is simply a representation for the expression.
22049 Otherwise, calls cp_parser_braced_list. */
22051 static cp_expr
22052 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22054 cp_expr initializer;
22056 /* Assume the expression is constant. */
22057 *non_constant_p = false;
22059 /* If it is not a `{', then we are looking at an
22060 assignment-expression. */
22061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22063 initializer
22064 = cp_parser_constant_expression (parser,
22065 /*allow_non_constant_p=*/true,
22066 non_constant_p);
22068 else
22069 initializer = cp_parser_braced_list (parser, non_constant_p);
22071 return initializer;
22074 /* Parse a brace-enclosed initializer list.
22076 braced-init-list:
22077 { initializer-list , [opt] }
22078 { designated-initializer-list , [opt] }
22081 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22082 the elements of the initializer-list (or NULL, if the last
22083 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22084 NULL_TREE. There is no way to detect whether or not the optional
22085 trailing `,' was provided. NON_CONSTANT_P is as for
22086 cp_parser_initializer. */
22088 static cp_expr
22089 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22091 tree initializer;
22092 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22094 /* Consume the `{' token. */
22095 matching_braces braces;
22096 braces.require_open (parser);
22097 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22098 initializer = make_node (CONSTRUCTOR);
22099 /* If it's not a `}', then there is a non-trivial initializer. */
22100 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22102 /* Parse the initializer list. */
22103 CONSTRUCTOR_ELTS (initializer)
22104 = cp_parser_initializer_list (parser, non_constant_p);
22105 /* A trailing `,' token is allowed. */
22106 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22107 cp_lexer_consume_token (parser->lexer);
22109 else
22110 *non_constant_p = false;
22111 /* Now, there should be a trailing `}'. */
22112 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22113 braces.require_close (parser);
22114 TREE_TYPE (initializer) = init_list_type_node;
22116 cp_expr result (initializer);
22117 /* Build a location of the form:
22118 { ... }
22119 ^~~~~~~
22120 with caret==start at the open brace, finish at the close brace. */
22121 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22122 result.set_location (combined_loc);
22123 return result;
22126 /* Consume tokens up to, and including, the next non-nested closing `]'.
22127 Returns true iff we found a closing `]'. */
22129 static bool
22130 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22132 unsigned square_depth = 0;
22134 while (true)
22136 cp_token * token = cp_lexer_peek_token (parser->lexer);
22138 switch (token->type)
22140 case CPP_EOF:
22141 case CPP_PRAGMA_EOL:
22142 /* If we've run out of tokens, then there is no closing `]'. */
22143 return false;
22145 case CPP_OPEN_SQUARE:
22146 ++square_depth;
22147 break;
22149 case CPP_CLOSE_SQUARE:
22150 if (!square_depth--)
22152 cp_lexer_consume_token (parser->lexer);
22153 return true;
22155 break;
22157 default:
22158 break;
22161 /* Consume the token. */
22162 cp_lexer_consume_token (parser->lexer);
22166 /* Return true if we are looking at an array-designator, false otherwise. */
22168 static bool
22169 cp_parser_array_designator_p (cp_parser *parser)
22171 /* Consume the `['. */
22172 cp_lexer_consume_token (parser->lexer);
22174 cp_lexer_save_tokens (parser->lexer);
22176 /* Skip tokens until the next token is a closing square bracket.
22177 If we find the closing `]', and the next token is a `=', then
22178 we are looking at an array designator. */
22179 bool array_designator_p
22180 = (cp_parser_skip_to_closing_square_bracket (parser)
22181 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22183 /* Roll back the tokens we skipped. */
22184 cp_lexer_rollback_tokens (parser->lexer);
22186 return array_designator_p;
22189 /* Parse an initializer-list.
22191 initializer-list:
22192 initializer-clause ... [opt]
22193 initializer-list , initializer-clause ... [opt]
22195 C++2A Extension:
22197 designated-initializer-list:
22198 designated-initializer-clause
22199 designated-initializer-list , designated-initializer-clause
22201 designated-initializer-clause:
22202 designator brace-or-equal-initializer
22204 designator:
22205 . identifier
22207 GNU Extension:
22209 initializer-list:
22210 designation initializer-clause ...[opt]
22211 initializer-list , designation initializer-clause ...[opt]
22213 designation:
22214 . identifier =
22215 identifier :
22216 [ constant-expression ] =
22218 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22219 for the initializer. If the INDEX of the elt is non-NULL, it is the
22220 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22221 as for cp_parser_initializer. */
22223 static vec<constructor_elt, va_gc> *
22224 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22226 vec<constructor_elt, va_gc> *v = NULL;
22227 bool first_p = true;
22228 tree first_designator = NULL_TREE;
22230 /* Assume all of the expressions are constant. */
22231 *non_constant_p = false;
22233 /* Parse the rest of the list. */
22234 while (true)
22236 cp_token *token;
22237 tree designator;
22238 tree initializer;
22239 bool clause_non_constant_p;
22240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22242 /* Handle the C++2A syntax, '. id ='. */
22243 if ((cxx_dialect >= cxx2a
22244 || cp_parser_allow_gnu_extensions_p (parser))
22245 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22246 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22247 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22248 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22249 == CPP_OPEN_BRACE)))
22251 if (cxx_dialect < cxx2a)
22252 pedwarn (loc, OPT_Wpedantic,
22253 "C++ designated initializers only available with "
22254 "-std=c++2a or -std=gnu++2a");
22255 /* Consume the `.'. */
22256 cp_lexer_consume_token (parser->lexer);
22257 /* Consume the identifier. */
22258 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22259 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22260 /* Consume the `='. */
22261 cp_lexer_consume_token (parser->lexer);
22263 /* Also, if the next token is an identifier and the following one is a
22264 colon, we are looking at the GNU designated-initializer
22265 syntax. */
22266 else if (cp_parser_allow_gnu_extensions_p (parser)
22267 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22268 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22269 == CPP_COLON))
22271 /* Warn the user that they are using an extension. */
22272 pedwarn (loc, OPT_Wpedantic,
22273 "ISO C++ does not allow GNU designated initializers");
22274 /* Consume the identifier. */
22275 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22276 /* Consume the `:'. */
22277 cp_lexer_consume_token (parser->lexer);
22279 /* Also handle C99 array designators, '[ const ] ='. */
22280 else if (cp_parser_allow_gnu_extensions_p (parser)
22281 && !c_dialect_objc ()
22282 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22284 /* In C++11, [ could start a lambda-introducer. */
22285 bool non_const = false;
22287 cp_parser_parse_tentatively (parser);
22289 if (!cp_parser_array_designator_p (parser))
22291 cp_parser_simulate_error (parser);
22292 designator = NULL_TREE;
22294 else
22296 designator = cp_parser_constant_expression (parser, true,
22297 &non_const);
22298 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22299 cp_parser_require (parser, CPP_EQ, RT_EQ);
22302 if (!cp_parser_parse_definitely (parser))
22303 designator = NULL_TREE;
22304 else if (non_const
22305 && (!require_potential_rvalue_constant_expression
22306 (designator)))
22307 designator = NULL_TREE;
22308 if (designator)
22309 /* Warn the user that they are using an extension. */
22310 pedwarn (loc, OPT_Wpedantic,
22311 "ISO C++ does not allow C99 designated initializers");
22313 else
22314 designator = NULL_TREE;
22316 if (first_p)
22318 first_designator = designator;
22319 first_p = false;
22321 else if (cxx_dialect >= cxx2a
22322 && first_designator != error_mark_node
22323 && (!first_designator != !designator))
22325 error_at (loc, "either all initializer clauses should be designated "
22326 "or none of them should be");
22327 first_designator = error_mark_node;
22329 else if (cxx_dialect < cxx2a && !first_designator)
22330 first_designator = designator;
22332 /* Parse the initializer. */
22333 initializer = cp_parser_initializer_clause (parser,
22334 &clause_non_constant_p);
22335 /* If any clause is non-constant, so is the entire initializer. */
22336 if (clause_non_constant_p)
22337 *non_constant_p = true;
22339 /* If we have an ellipsis, this is an initializer pack
22340 expansion. */
22341 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22343 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22345 /* Consume the `...'. */
22346 cp_lexer_consume_token (parser->lexer);
22348 if (designator && cxx_dialect >= cxx2a)
22349 error_at (loc,
22350 "%<...%> not allowed in designated initializer list");
22352 /* Turn the initializer into an initializer expansion. */
22353 initializer = make_pack_expansion (initializer);
22356 /* Add it to the vector. */
22357 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22359 /* If the next token is not a comma, we have reached the end of
22360 the list. */
22361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22362 break;
22364 /* Peek at the next token. */
22365 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22366 /* If the next token is a `}', then we're still done. An
22367 initializer-clause can have a trailing `,' after the
22368 initializer-list and before the closing `}'. */
22369 if (token->type == CPP_CLOSE_BRACE)
22370 break;
22372 /* Consume the `,' token. */
22373 cp_lexer_consume_token (parser->lexer);
22376 /* The same identifier shall not appear in multiple designators
22377 of a designated-initializer-list. */
22378 if (first_designator)
22380 unsigned int i;
22381 tree designator, val;
22382 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22383 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22385 if (IDENTIFIER_MARKED (designator))
22387 error_at (cp_expr_loc_or_loc (val, input_location),
22388 "%<.%s%> designator used multiple times in "
22389 "the same initializer list",
22390 IDENTIFIER_POINTER (designator));
22391 (*v)[i].index = NULL_TREE;
22393 else
22394 IDENTIFIER_MARKED (designator) = 1;
22396 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22397 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22398 IDENTIFIER_MARKED (designator) = 0;
22401 return v;
22404 /* Classes [gram.class] */
22406 /* Parse a class-name.
22408 class-name:
22409 identifier
22410 template-id
22412 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22413 to indicate that names looked up in dependent types should be
22414 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22415 keyword has been used to indicate that the name that appears next
22416 is a template. TAG_TYPE indicates the explicit tag given before
22417 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22418 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22419 is the class being defined in a class-head. If ENUM_OK is TRUE,
22420 enum-names are also accepted.
22422 Returns the TYPE_DECL representing the class. */
22424 static tree
22425 cp_parser_class_name (cp_parser *parser,
22426 bool typename_keyword_p,
22427 bool template_keyword_p,
22428 enum tag_types tag_type,
22429 bool check_dependency_p,
22430 bool class_head_p,
22431 bool is_declaration,
22432 bool enum_ok)
22434 tree decl;
22435 tree scope;
22436 bool typename_p;
22437 cp_token *token;
22438 tree identifier = NULL_TREE;
22440 /* All class-names start with an identifier. */
22441 token = cp_lexer_peek_token (parser->lexer);
22442 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22444 cp_parser_error (parser, "expected class-name");
22445 return error_mark_node;
22448 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22449 to a template-id, so we save it here. */
22450 scope = parser->scope;
22451 if (scope == error_mark_node)
22452 return error_mark_node;
22454 /* Any name names a type if we're following the `typename' keyword
22455 in a qualified name where the enclosing scope is type-dependent. */
22456 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22457 && dependent_type_p (scope));
22458 /* Handle the common case (an identifier, but not a template-id)
22459 efficiently. */
22460 if (token->type == CPP_NAME
22461 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22463 cp_token *identifier_token;
22464 bool ambiguous_p;
22466 /* Look for the identifier. */
22467 identifier_token = cp_lexer_peek_token (parser->lexer);
22468 ambiguous_p = identifier_token->error_reported;
22469 identifier = cp_parser_identifier (parser);
22470 /* If the next token isn't an identifier, we are certainly not
22471 looking at a class-name. */
22472 if (identifier == error_mark_node)
22473 decl = error_mark_node;
22474 /* If we know this is a type-name, there's no need to look it
22475 up. */
22476 else if (typename_p)
22477 decl = identifier;
22478 else
22480 tree ambiguous_decls;
22481 /* If we already know that this lookup is ambiguous, then
22482 we've already issued an error message; there's no reason
22483 to check again. */
22484 if (ambiguous_p)
22486 cp_parser_simulate_error (parser);
22487 return error_mark_node;
22489 /* If the next token is a `::', then the name must be a type
22490 name.
22492 [basic.lookup.qual]
22494 During the lookup for a name preceding the :: scope
22495 resolution operator, object, function, and enumerator
22496 names are ignored. */
22497 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22498 tag_type = scope_type;
22499 /* Look up the name. */
22500 decl = cp_parser_lookup_name (parser, identifier,
22501 tag_type,
22502 /*is_template=*/false,
22503 /*is_namespace=*/false,
22504 check_dependency_p,
22505 &ambiguous_decls,
22506 identifier_token->location);
22507 if (ambiguous_decls)
22509 if (cp_parser_parsing_tentatively (parser))
22510 cp_parser_simulate_error (parser);
22511 return error_mark_node;
22515 else
22517 /* Try a template-id. */
22518 decl = cp_parser_template_id (parser, template_keyword_p,
22519 check_dependency_p,
22520 tag_type,
22521 is_declaration);
22522 if (decl == error_mark_node)
22523 return error_mark_node;
22526 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22528 /* If this is a typename, create a TYPENAME_TYPE. */
22529 if (typename_p && decl != error_mark_node)
22531 decl = make_typename_type (scope, decl, typename_type,
22532 /*complain=*/tf_error);
22533 if (decl != error_mark_node)
22534 decl = TYPE_NAME (decl);
22537 decl = strip_using_decl (decl);
22539 /* Check to see that it is really the name of a class. */
22540 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22541 && identifier_p (TREE_OPERAND (decl, 0))
22542 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22543 /* Situations like this:
22545 template <typename T> struct A {
22546 typename T::template X<int>::I i;
22549 are problematic. Is `T::template X<int>' a class-name? The
22550 standard does not seem to be definitive, but there is no other
22551 valid interpretation of the following `::'. Therefore, those
22552 names are considered class-names. */
22554 decl = make_typename_type (scope, decl, tag_type, tf_error);
22555 if (decl != error_mark_node)
22556 decl = TYPE_NAME (decl);
22558 else if (TREE_CODE (decl) != TYPE_DECL
22559 || TREE_TYPE (decl) == error_mark_node
22560 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22561 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22562 /* In Objective-C 2.0, a classname followed by '.' starts a
22563 dot-syntax expression, and it's not a type-name. */
22564 || (c_dialect_objc ()
22565 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22566 && objc_is_class_name (decl)))
22567 decl = error_mark_node;
22569 if (decl == error_mark_node)
22570 cp_parser_error (parser, "expected class-name");
22571 else if (identifier && !parser->scope)
22572 maybe_note_name_used_in_class (identifier, decl);
22574 return decl;
22577 /* Parse a class-specifier.
22579 class-specifier:
22580 class-head { member-specification [opt] }
22582 Returns the TREE_TYPE representing the class. */
22584 static tree
22585 cp_parser_class_specifier_1 (cp_parser* parser)
22587 tree type;
22588 tree attributes = NULL_TREE;
22589 bool nested_name_specifier_p;
22590 unsigned saved_num_template_parameter_lists;
22591 bool saved_in_function_body;
22592 unsigned char in_statement;
22593 bool in_switch_statement_p;
22594 bool saved_in_unbraced_linkage_specification_p;
22595 tree old_scope = NULL_TREE;
22596 tree scope = NULL_TREE;
22597 cp_token *closing_brace;
22599 push_deferring_access_checks (dk_no_deferred);
22601 /* Parse the class-head. */
22602 type = cp_parser_class_head (parser,
22603 &nested_name_specifier_p);
22604 /* If the class-head was a semantic disaster, skip the entire body
22605 of the class. */
22606 if (!type)
22608 cp_parser_skip_to_end_of_block_or_statement (parser);
22609 pop_deferring_access_checks ();
22610 return error_mark_node;
22613 /* Look for the `{'. */
22614 matching_braces braces;
22615 if (!braces.require_open (parser))
22617 pop_deferring_access_checks ();
22618 return error_mark_node;
22621 cp_ensure_no_omp_declare_simd (parser);
22622 cp_ensure_no_oacc_routine (parser);
22624 /* Issue an error message if type-definitions are forbidden here. */
22625 cp_parser_check_type_definition (parser);
22626 /* Remember that we are defining one more class. */
22627 ++parser->num_classes_being_defined;
22628 /* Inside the class, surrounding template-parameter-lists do not
22629 apply. */
22630 saved_num_template_parameter_lists
22631 = parser->num_template_parameter_lists;
22632 parser->num_template_parameter_lists = 0;
22633 /* We are not in a function body. */
22634 saved_in_function_body = parser->in_function_body;
22635 parser->in_function_body = false;
22636 /* Or in a loop. */
22637 in_statement = parser->in_statement;
22638 parser->in_statement = 0;
22639 /* Or in a switch. */
22640 in_switch_statement_p = parser->in_switch_statement_p;
22641 parser->in_switch_statement_p = false;
22642 /* We are not immediately inside an extern "lang" block. */
22643 saved_in_unbraced_linkage_specification_p
22644 = parser->in_unbraced_linkage_specification_p;
22645 parser->in_unbraced_linkage_specification_p = false;
22647 // Associate constraints with the type.
22648 if (flag_concepts)
22649 type = associate_classtype_constraints (type);
22651 /* Start the class. */
22652 if (nested_name_specifier_p)
22654 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22655 old_scope = push_inner_scope (scope);
22657 type = begin_class_definition (type);
22659 if (type == error_mark_node)
22660 /* If the type is erroneous, skip the entire body of the class. */
22661 cp_parser_skip_to_closing_brace (parser);
22662 else
22663 /* Parse the member-specification. */
22664 cp_parser_member_specification_opt (parser);
22666 /* Look for the trailing `}'. */
22667 closing_brace = braces.require_close (parser);
22668 /* Look for trailing attributes to apply to this class. */
22669 if (cp_parser_allow_gnu_extensions_p (parser))
22670 attributes = cp_parser_gnu_attributes_opt (parser);
22671 if (type != error_mark_node)
22672 type = finish_struct (type, attributes);
22673 if (nested_name_specifier_p)
22674 pop_inner_scope (old_scope, scope);
22676 /* We've finished a type definition. Check for the common syntax
22677 error of forgetting a semicolon after the definition. We need to
22678 be careful, as we can't just check for not-a-semicolon and be done
22679 with it; the user might have typed:
22681 class X { } c = ...;
22682 class X { } *p = ...;
22684 and so forth. Instead, enumerate all the possible tokens that
22685 might follow this production; if we don't see one of them, then
22686 complain and silently insert the semicolon. */
22688 cp_token *token = cp_lexer_peek_token (parser->lexer);
22689 bool want_semicolon = true;
22691 if (cp_next_tokens_can_be_std_attribute_p (parser))
22692 /* Don't try to parse c++11 attributes here. As per the
22693 grammar, that should be a task for
22694 cp_parser_decl_specifier_seq. */
22695 want_semicolon = false;
22697 switch (token->type)
22699 case CPP_NAME:
22700 case CPP_SEMICOLON:
22701 case CPP_MULT:
22702 case CPP_AND:
22703 case CPP_OPEN_PAREN:
22704 case CPP_CLOSE_PAREN:
22705 case CPP_COMMA:
22706 want_semicolon = false;
22707 break;
22709 /* While it's legal for type qualifiers and storage class
22710 specifiers to follow type definitions in the grammar, only
22711 compiler testsuites contain code like that. Assume that if
22712 we see such code, then what we're really seeing is a case
22713 like:
22715 class X { }
22716 const <type> var = ...;
22720 class Y { }
22721 static <type> func (...) ...
22723 i.e. the qualifier or specifier applies to the next
22724 declaration. To do so, however, we need to look ahead one
22725 more token to see if *that* token is a type specifier.
22727 This code could be improved to handle:
22729 class Z { }
22730 static const <type> var = ...; */
22731 case CPP_KEYWORD:
22732 if (keyword_is_decl_specifier (token->keyword))
22734 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22736 /* Handling user-defined types here would be nice, but very
22737 tricky. */
22738 want_semicolon
22739 = (lookahead->type == CPP_KEYWORD
22740 && keyword_begins_type_specifier (lookahead->keyword));
22742 break;
22743 default:
22744 break;
22747 /* If we don't have a type, then something is very wrong and we
22748 shouldn't try to do anything clever. Likewise for not seeing the
22749 closing brace. */
22750 if (closing_brace && TYPE_P (type) && want_semicolon)
22752 /* Locate the closing brace. */
22753 cp_token_position prev
22754 = cp_lexer_previous_token_position (parser->lexer);
22755 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22756 location_t loc = prev_token->location;
22758 /* We want to suggest insertion of a ';' immediately *after* the
22759 closing brace, so, if we can, offset the location by 1 column. */
22760 location_t next_loc = loc;
22761 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22762 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22764 rich_location richloc (line_table, next_loc);
22766 /* If we successfully offset the location, suggest the fix-it. */
22767 if (next_loc != loc)
22768 richloc.add_fixit_insert_before (next_loc, ";");
22770 if (CLASSTYPE_DECLARED_CLASS (type))
22771 error_at (&richloc,
22772 "expected %<;%> after class definition");
22773 else if (TREE_CODE (type) == RECORD_TYPE)
22774 error_at (&richloc,
22775 "expected %<;%> after struct definition");
22776 else if (TREE_CODE (type) == UNION_TYPE)
22777 error_at (&richloc,
22778 "expected %<;%> after union definition");
22779 else
22780 gcc_unreachable ();
22782 /* Unget one token and smash it to look as though we encountered
22783 a semicolon in the input stream. */
22784 cp_lexer_set_token_position (parser->lexer, prev);
22785 token = cp_lexer_peek_token (parser->lexer);
22786 token->type = CPP_SEMICOLON;
22787 token->keyword = RID_MAX;
22791 /* If this class is not itself within the scope of another class,
22792 then we need to parse the bodies of all of the queued function
22793 definitions. Note that the queued functions defined in a class
22794 are not always processed immediately following the
22795 class-specifier for that class. Consider:
22797 struct A {
22798 struct B { void f() { sizeof (A); } };
22801 If `f' were processed before the processing of `A' were
22802 completed, there would be no way to compute the size of `A'.
22803 Note that the nesting we are interested in here is lexical --
22804 not the semantic nesting given by TYPE_CONTEXT. In particular,
22805 for:
22807 struct A { struct B; };
22808 struct A::B { void f() { } };
22810 there is no need to delay the parsing of `A::B::f'. */
22811 if (--parser->num_classes_being_defined == 0)
22813 tree decl;
22814 tree class_type = NULL_TREE;
22815 tree pushed_scope = NULL_TREE;
22816 unsigned ix;
22817 cp_default_arg_entry *e;
22818 tree save_ccp, save_ccr;
22820 if (any_erroneous_template_args_p (type))
22822 /* Skip default arguments, NSDMIs, etc, in order to improve
22823 error recovery (c++/71169, c++/71832). */
22824 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22825 vec_safe_truncate (unparsed_nsdmis, 0);
22826 vec_safe_truncate (unparsed_classes, 0);
22827 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22830 /* In a first pass, parse default arguments to the functions.
22831 Then, in a second pass, parse the bodies of the functions.
22832 This two-phased approach handles cases like:
22834 struct S {
22835 void f() { g(); }
22836 void g(int i = 3);
22840 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22842 decl = e->decl;
22843 /* If there are default arguments that have not yet been processed,
22844 take care of them now. */
22845 if (class_type != e->class_type)
22847 if (pushed_scope)
22848 pop_scope (pushed_scope);
22849 class_type = e->class_type;
22850 pushed_scope = push_scope (class_type);
22852 /* Make sure that any template parameters are in scope. */
22853 maybe_begin_member_template_processing (decl);
22854 /* Parse the default argument expressions. */
22855 cp_parser_late_parsing_default_args (parser, decl);
22856 /* Remove any template parameters from the symbol table. */
22857 maybe_end_member_template_processing ();
22859 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22860 /* Now parse any NSDMIs. */
22861 save_ccp = current_class_ptr;
22862 save_ccr = current_class_ref;
22863 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22865 if (class_type != DECL_CONTEXT (decl))
22867 if (pushed_scope)
22868 pop_scope (pushed_scope);
22869 class_type = DECL_CONTEXT (decl);
22870 pushed_scope = push_scope (class_type);
22872 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22873 cp_parser_late_parsing_nsdmi (parser, decl);
22875 vec_safe_truncate (unparsed_nsdmis, 0);
22876 current_class_ptr = save_ccp;
22877 current_class_ref = save_ccr;
22878 if (pushed_scope)
22879 pop_scope (pushed_scope);
22881 /* Now do some post-NSDMI bookkeeping. */
22882 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22883 after_nsdmi_defaulted_late_checks (class_type);
22884 vec_safe_truncate (unparsed_classes, 0);
22885 after_nsdmi_defaulted_late_checks (type);
22887 /* Now parse the body of the functions. */
22888 if (flag_openmp)
22890 /* OpenMP UDRs need to be parsed before all other functions. */
22891 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22892 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22893 cp_parser_late_parsing_for_member (parser, decl);
22894 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22895 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22896 cp_parser_late_parsing_for_member (parser, decl);
22898 else
22899 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22900 cp_parser_late_parsing_for_member (parser, decl);
22901 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22903 else
22904 vec_safe_push (unparsed_classes, type);
22906 /* Put back any saved access checks. */
22907 pop_deferring_access_checks ();
22909 /* Restore saved state. */
22910 parser->in_switch_statement_p = in_switch_statement_p;
22911 parser->in_statement = in_statement;
22912 parser->in_function_body = saved_in_function_body;
22913 parser->num_template_parameter_lists
22914 = saved_num_template_parameter_lists;
22915 parser->in_unbraced_linkage_specification_p
22916 = saved_in_unbraced_linkage_specification_p;
22918 return type;
22921 static tree
22922 cp_parser_class_specifier (cp_parser* parser)
22924 tree ret;
22925 timevar_push (TV_PARSE_STRUCT);
22926 ret = cp_parser_class_specifier_1 (parser);
22927 timevar_pop (TV_PARSE_STRUCT);
22928 return ret;
22931 /* Parse a class-head.
22933 class-head:
22934 class-key identifier [opt] base-clause [opt]
22935 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22936 class-key nested-name-specifier [opt] template-id
22937 base-clause [opt]
22939 class-virt-specifier:
22940 final
22942 GNU Extensions:
22943 class-key attributes identifier [opt] base-clause [opt]
22944 class-key attributes nested-name-specifier identifier base-clause [opt]
22945 class-key attributes nested-name-specifier [opt] template-id
22946 base-clause [opt]
22948 Upon return BASES is initialized to the list of base classes (or
22949 NULL, if there are none) in the same form returned by
22950 cp_parser_base_clause.
22952 Returns the TYPE of the indicated class. Sets
22953 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22954 involving a nested-name-specifier was used, and FALSE otherwise.
22956 Returns error_mark_node if this is not a class-head.
22958 Returns NULL_TREE if the class-head is syntactically valid, but
22959 semantically invalid in a way that means we should skip the entire
22960 body of the class. */
22962 static tree
22963 cp_parser_class_head (cp_parser* parser,
22964 bool* nested_name_specifier_p)
22966 tree nested_name_specifier;
22967 enum tag_types class_key;
22968 tree id = NULL_TREE;
22969 tree type = NULL_TREE;
22970 tree attributes;
22971 tree bases;
22972 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22973 bool template_id_p = false;
22974 bool qualified_p = false;
22975 bool invalid_nested_name_p = false;
22976 bool invalid_explicit_specialization_p = false;
22977 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22978 tree pushed_scope = NULL_TREE;
22979 unsigned num_templates;
22980 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22981 /* Assume no nested-name-specifier will be present. */
22982 *nested_name_specifier_p = false;
22983 /* Assume no template parameter lists will be used in defining the
22984 type. */
22985 num_templates = 0;
22986 parser->colon_corrects_to_scope_p = false;
22988 /* Look for the class-key. */
22989 class_key = cp_parser_class_key (parser);
22990 if (class_key == none_type)
22991 return error_mark_node;
22993 location_t class_head_start_location = input_location;
22995 /* Parse the attributes. */
22996 attributes = cp_parser_attributes_opt (parser);
22998 /* If the next token is `::', that is invalid -- but sometimes
22999 people do try to write:
23001 struct ::S {};
23003 Handle this gracefully by accepting the extra qualifier, and then
23004 issuing an error about it later if this really is a
23005 class-head. If it turns out just to be an elaborated type
23006 specifier, remain silent. */
23007 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23008 qualified_p = true;
23010 push_deferring_access_checks (dk_no_check);
23012 /* Determine the name of the class. Begin by looking for an
23013 optional nested-name-specifier. */
23014 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23015 nested_name_specifier
23016 = cp_parser_nested_name_specifier_opt (parser,
23017 /*typename_keyword_p=*/false,
23018 /*check_dependency_p=*/false,
23019 /*type_p=*/true,
23020 /*is_declaration=*/false);
23021 /* If there was a nested-name-specifier, then there *must* be an
23022 identifier. */
23024 cp_token *bad_template_keyword = NULL;
23026 if (nested_name_specifier)
23028 type_start_token = cp_lexer_peek_token (parser->lexer);
23029 /* Although the grammar says `identifier', it really means
23030 `class-name' or `template-name'. You are only allowed to
23031 define a class that has already been declared with this
23032 syntax.
23034 The proposed resolution for Core Issue 180 says that wherever
23035 you see `class T::X' you should treat `X' as a type-name.
23037 It is OK to define an inaccessible class; for example:
23039 class A { class B; };
23040 class A::B {};
23042 We do not know if we will see a class-name, or a
23043 template-name. We look for a class-name first, in case the
23044 class-name is a template-id; if we looked for the
23045 template-name first we would stop after the template-name. */
23046 cp_parser_parse_tentatively (parser);
23047 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23048 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23049 type = cp_parser_class_name (parser,
23050 /*typename_keyword_p=*/false,
23051 /*template_keyword_p=*/false,
23052 class_type,
23053 /*check_dependency_p=*/false,
23054 /*class_head_p=*/true,
23055 /*is_declaration=*/false);
23056 /* If that didn't work, ignore the nested-name-specifier. */
23057 if (!cp_parser_parse_definitely (parser))
23059 invalid_nested_name_p = true;
23060 type_start_token = cp_lexer_peek_token (parser->lexer);
23061 id = cp_parser_identifier (parser);
23062 if (id == error_mark_node)
23063 id = NULL_TREE;
23065 /* If we could not find a corresponding TYPE, treat this
23066 declaration like an unqualified declaration. */
23067 if (type == error_mark_node)
23068 nested_name_specifier = NULL_TREE;
23069 /* Otherwise, count the number of templates used in TYPE and its
23070 containing scopes. */
23071 else
23072 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23074 /* Otherwise, the identifier is optional. */
23075 else
23077 /* We don't know whether what comes next is a template-id,
23078 an identifier, or nothing at all. */
23079 cp_parser_parse_tentatively (parser);
23080 /* Check for a template-id. */
23081 type_start_token = cp_lexer_peek_token (parser->lexer);
23082 id = cp_parser_template_id (parser,
23083 /*template_keyword_p=*/false,
23084 /*check_dependency_p=*/true,
23085 class_key,
23086 /*is_declaration=*/true);
23087 /* If that didn't work, it could still be an identifier. */
23088 if (!cp_parser_parse_definitely (parser))
23090 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23092 type_start_token = cp_lexer_peek_token (parser->lexer);
23093 id = cp_parser_identifier (parser);
23095 else
23096 id = NULL_TREE;
23098 else
23100 template_id_p = true;
23101 ++num_templates;
23105 pop_deferring_access_checks ();
23107 if (id)
23109 cp_parser_check_for_invalid_template_id (parser, id,
23110 class_key,
23111 type_start_token->location);
23113 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23115 /* If it's not a `:' or a `{' then we can't really be looking at a
23116 class-head, since a class-head only appears as part of a
23117 class-specifier. We have to detect this situation before calling
23118 xref_tag, since that has irreversible side-effects. */
23119 if (!cp_parser_next_token_starts_class_definition_p (parser))
23121 cp_parser_error (parser, "expected %<{%> or %<:%>");
23122 type = error_mark_node;
23123 goto out;
23126 /* At this point, we're going ahead with the class-specifier, even
23127 if some other problem occurs. */
23128 cp_parser_commit_to_tentative_parse (parser);
23129 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23131 cp_parser_error (parser,
23132 "cannot specify %<override%> for a class");
23133 type = error_mark_node;
23134 goto out;
23136 /* Issue the error about the overly-qualified name now. */
23137 if (qualified_p)
23139 cp_parser_error (parser,
23140 "global qualification of class name is invalid");
23141 type = error_mark_node;
23142 goto out;
23144 else if (invalid_nested_name_p)
23146 cp_parser_error (parser,
23147 "qualified name does not name a class");
23148 type = error_mark_node;
23149 goto out;
23151 else if (nested_name_specifier)
23153 tree scope;
23155 if (bad_template_keyword)
23156 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23157 keyword template shall not appear at the top level. */
23158 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23159 "keyword %<template%> not allowed in class-head-name");
23161 /* Reject typedef-names in class heads. */
23162 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23164 error_at (type_start_token->location,
23165 "invalid class name in declaration of %qD",
23166 type);
23167 type = NULL_TREE;
23168 goto done;
23171 /* Figure out in what scope the declaration is being placed. */
23172 scope = current_scope ();
23173 /* If that scope does not contain the scope in which the
23174 class was originally declared, the program is invalid. */
23175 if (scope && !is_ancestor (scope, nested_name_specifier))
23177 if (at_namespace_scope_p ())
23178 error_at (type_start_token->location,
23179 "declaration of %qD in namespace %qD which does not "
23180 "enclose %qD",
23181 type, scope, nested_name_specifier);
23182 else
23183 error_at (type_start_token->location,
23184 "declaration of %qD in %qD which does not enclose %qD",
23185 type, scope, nested_name_specifier);
23186 type = NULL_TREE;
23187 goto done;
23189 /* [dcl.meaning]
23191 A declarator-id shall not be qualified except for the
23192 definition of a ... nested class outside of its class
23193 ... [or] the definition or explicit instantiation of a
23194 class member of a namespace outside of its namespace. */
23195 if (scope == nested_name_specifier)
23197 permerror (nested_name_specifier_token_start->location,
23198 "extra qualification not allowed");
23199 nested_name_specifier = NULL_TREE;
23200 num_templates = 0;
23203 /* An explicit-specialization must be preceded by "template <>". If
23204 it is not, try to recover gracefully. */
23205 if (at_namespace_scope_p ()
23206 && parser->num_template_parameter_lists == 0
23207 && !processing_template_parmlist
23208 && template_id_p)
23210 /* Build a location of this form:
23211 struct typename <ARGS>
23212 ^~~~~~~~~~~~~~~~~~~~~~
23213 with caret==start at the start token, and
23214 finishing at the end of the type. */
23215 location_t reported_loc
23216 = make_location (class_head_start_location,
23217 class_head_start_location,
23218 get_finish (type_start_token->location));
23219 rich_location richloc (line_table, reported_loc);
23220 richloc.add_fixit_insert_before (class_head_start_location,
23221 "template <> ");
23222 error_at (&richloc,
23223 "an explicit specialization must be preceded by"
23224 " %<template <>%>");
23225 invalid_explicit_specialization_p = true;
23226 /* Take the same action that would have been taken by
23227 cp_parser_explicit_specialization. */
23228 ++parser->num_template_parameter_lists;
23229 begin_specialization ();
23231 /* There must be no "return" statements between this point and the
23232 end of this function; set "type "to the correct return value and
23233 use "goto done;" to return. */
23234 /* Make sure that the right number of template parameters were
23235 present. */
23236 if (!cp_parser_check_template_parameters (parser, num_templates,
23237 template_id_p,
23238 type_start_token->location,
23239 /*declarator=*/NULL))
23241 /* If something went wrong, there is no point in even trying to
23242 process the class-definition. */
23243 type = NULL_TREE;
23244 goto done;
23247 /* Look up the type. */
23248 if (template_id_p)
23250 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23251 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23252 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23254 error_at (type_start_token->location,
23255 "function template %qD redeclared as a class template", id);
23256 type = error_mark_node;
23258 else
23260 type = TREE_TYPE (id);
23261 type = maybe_process_partial_specialization (type);
23263 /* Check the scope while we still know whether or not we had a
23264 nested-name-specifier. */
23265 if (type != error_mark_node)
23266 check_unqualified_spec_or_inst (type, type_start_token->location);
23268 if (nested_name_specifier)
23269 pushed_scope = push_scope (nested_name_specifier);
23271 else if (nested_name_specifier)
23273 tree class_type;
23275 /* Given:
23277 template <typename T> struct S { struct T };
23278 template <typename T> struct S<T>::T { };
23280 we will get a TYPENAME_TYPE when processing the definition of
23281 `S::T'. We need to resolve it to the actual type before we
23282 try to define it. */
23283 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23285 class_type = resolve_typename_type (TREE_TYPE (type),
23286 /*only_current_p=*/false);
23287 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23288 type = TYPE_NAME (class_type);
23289 else
23291 cp_parser_error (parser, "could not resolve typename type");
23292 type = error_mark_node;
23296 if (maybe_process_partial_specialization (TREE_TYPE (type))
23297 == error_mark_node)
23299 type = NULL_TREE;
23300 goto done;
23303 class_type = current_class_type;
23304 /* Enter the scope indicated by the nested-name-specifier. */
23305 pushed_scope = push_scope (nested_name_specifier);
23306 /* Get the canonical version of this type. */
23307 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23308 /* Call push_template_decl if it seems like we should be defining a
23309 template either from the template headers or the type we're
23310 defining, so that we diagnose both extra and missing headers. */
23311 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23312 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23313 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23315 type = push_template_decl (type);
23316 if (type == error_mark_node)
23318 type = NULL_TREE;
23319 goto done;
23323 type = TREE_TYPE (type);
23324 *nested_name_specifier_p = true;
23326 else /* The name is not a nested name. */
23328 /* If the class was unnamed, create a dummy name. */
23329 if (!id)
23330 id = make_anon_name ();
23331 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23332 ? ts_within_enclosing_non_class
23333 : ts_current);
23334 type = xref_tag (class_key, id, tag_scope,
23335 parser->num_template_parameter_lists);
23338 /* Indicate whether this class was declared as a `class' or as a
23339 `struct'. */
23340 if (TREE_CODE (type) == RECORD_TYPE)
23341 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23342 cp_parser_check_class_key (class_key, type);
23344 /* If this type was already complete, and we see another definition,
23345 that's an error. */
23346 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23348 error_at (type_start_token->location, "redefinition of %q#T",
23349 type);
23350 inform (location_of (type), "previous definition of %q#T",
23351 type);
23352 type = NULL_TREE;
23353 goto done;
23355 else if (type == error_mark_node)
23356 type = NULL_TREE;
23358 if (type)
23360 /* Apply attributes now, before any use of the class as a template
23361 argument in its base list. */
23362 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23363 fixup_attribute_variants (type);
23366 /* We will have entered the scope containing the class; the names of
23367 base classes should be looked up in that context. For example:
23369 struct A { struct B {}; struct C; };
23370 struct A::C : B {};
23372 is valid. */
23374 /* Get the list of base-classes, if there is one. */
23375 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23377 /* PR59482: enter the class scope so that base-specifiers are looked
23378 up correctly. */
23379 if (type)
23380 pushclass (type);
23381 bases = cp_parser_base_clause (parser);
23382 /* PR59482: get out of the previously pushed class scope so that the
23383 subsequent pops pop the right thing. */
23384 if (type)
23385 popclass ();
23387 else
23388 bases = NULL_TREE;
23390 /* If we're really defining a class, process the base classes.
23391 If they're invalid, fail. */
23392 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23393 xref_basetypes (type, bases);
23395 done:
23396 /* Leave the scope given by the nested-name-specifier. We will
23397 enter the class scope itself while processing the members. */
23398 if (pushed_scope)
23399 pop_scope (pushed_scope);
23401 if (invalid_explicit_specialization_p)
23403 end_specialization ();
23404 --parser->num_template_parameter_lists;
23407 if (type)
23408 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23409 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23410 CLASSTYPE_FINAL (type) = 1;
23411 out:
23412 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23413 return type;
23416 /* Parse a class-key.
23418 class-key:
23419 class
23420 struct
23421 union
23423 Returns the kind of class-key specified, or none_type to indicate
23424 error. */
23426 static enum tag_types
23427 cp_parser_class_key (cp_parser* parser)
23429 cp_token *token;
23430 enum tag_types tag_type;
23432 /* Look for the class-key. */
23433 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23434 if (!token)
23435 return none_type;
23437 /* Check to see if the TOKEN is a class-key. */
23438 tag_type = cp_parser_token_is_class_key (token);
23439 if (!tag_type)
23440 cp_parser_error (parser, "expected class-key");
23441 return tag_type;
23444 /* Parse a type-parameter-key.
23446 type-parameter-key:
23447 class
23448 typename
23451 static void
23452 cp_parser_type_parameter_key (cp_parser* parser)
23454 /* Look for the type-parameter-key. */
23455 enum tag_types tag_type = none_type;
23456 cp_token *token = cp_lexer_peek_token (parser->lexer);
23457 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23459 cp_lexer_consume_token (parser->lexer);
23460 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23461 /* typename is not allowed in a template template parameter
23462 by the standard until C++17. */
23463 pedwarn (token->location, OPT_Wpedantic,
23464 "ISO C++ forbids typename key in template template parameter;"
23465 " use -std=c++17 or -std=gnu++17");
23467 else
23468 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23470 return;
23473 /* Parse an (optional) member-specification.
23475 member-specification:
23476 member-declaration member-specification [opt]
23477 access-specifier : member-specification [opt] */
23479 static void
23480 cp_parser_member_specification_opt (cp_parser* parser)
23482 while (true)
23484 cp_token *token;
23485 enum rid keyword;
23487 /* Peek at the next token. */
23488 token = cp_lexer_peek_token (parser->lexer);
23489 /* If it's a `}', or EOF then we've seen all the members. */
23490 if (token->type == CPP_CLOSE_BRACE
23491 || token->type == CPP_EOF
23492 || token->type == CPP_PRAGMA_EOL)
23493 break;
23495 /* See if this token is a keyword. */
23496 keyword = token->keyword;
23497 switch (keyword)
23499 case RID_PUBLIC:
23500 case RID_PROTECTED:
23501 case RID_PRIVATE:
23502 /* Consume the access-specifier. */
23503 cp_lexer_consume_token (parser->lexer);
23504 /* Remember which access-specifier is active. */
23505 current_access_specifier = token->u.value;
23506 /* Look for the `:'. */
23507 cp_parser_require (parser, CPP_COLON, RT_COLON);
23508 break;
23510 default:
23511 /* Accept #pragmas at class scope. */
23512 if (token->type == CPP_PRAGMA)
23514 cp_parser_pragma (parser, pragma_member, NULL);
23515 break;
23518 /* Otherwise, the next construction must be a
23519 member-declaration. */
23520 cp_parser_member_declaration (parser);
23525 /* Parse a member-declaration.
23527 member-declaration:
23528 decl-specifier-seq [opt] member-declarator-list [opt] ;
23529 function-definition ; [opt]
23530 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23531 using-declaration
23532 template-declaration
23533 alias-declaration
23535 member-declarator-list:
23536 member-declarator
23537 member-declarator-list , member-declarator
23539 member-declarator:
23540 declarator pure-specifier [opt]
23541 declarator constant-initializer [opt]
23542 identifier [opt] : constant-expression
23544 GNU Extensions:
23546 member-declaration:
23547 __extension__ member-declaration
23549 member-declarator:
23550 declarator attributes [opt] pure-specifier [opt]
23551 declarator attributes [opt] constant-initializer [opt]
23552 identifier [opt] attributes [opt] : constant-expression
23554 C++0x Extensions:
23556 member-declaration:
23557 static_assert-declaration */
23559 static void
23560 cp_parser_member_declaration (cp_parser* parser)
23562 cp_decl_specifier_seq decl_specifiers;
23563 tree prefix_attributes;
23564 tree decl;
23565 int declares_class_or_enum;
23566 bool friend_p;
23567 cp_token *token = NULL;
23568 cp_token *decl_spec_token_start = NULL;
23569 cp_token *initializer_token_start = NULL;
23570 int saved_pedantic;
23571 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23573 /* Check for the `__extension__' keyword. */
23574 if (cp_parser_extension_opt (parser, &saved_pedantic))
23576 /* Recurse. */
23577 cp_parser_member_declaration (parser);
23578 /* Restore the old value of the PEDANTIC flag. */
23579 pedantic = saved_pedantic;
23581 return;
23584 /* Check for a template-declaration. */
23585 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23587 /* An explicit specialization here is an error condition, and we
23588 expect the specialization handler to detect and report this. */
23589 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23590 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23591 cp_parser_explicit_specialization (parser);
23592 else
23593 cp_parser_template_declaration (parser, /*member_p=*/true);
23595 return;
23597 /* Check for a template introduction. */
23598 else if (cp_parser_template_declaration_after_export (parser, true))
23599 return;
23601 /* Check for a using-declaration. */
23602 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23604 if (cxx_dialect < cxx11)
23606 /* Parse the using-declaration. */
23607 cp_parser_using_declaration (parser,
23608 /*access_declaration_p=*/false);
23609 return;
23611 else
23613 tree decl;
23614 bool alias_decl_expected;
23615 cp_parser_parse_tentatively (parser);
23616 decl = cp_parser_alias_declaration (parser);
23617 /* Note that if we actually see the '=' token after the
23618 identifier, cp_parser_alias_declaration commits the
23619 tentative parse. In that case, we really expect an
23620 alias-declaration. Otherwise, we expect a using
23621 declaration. */
23622 alias_decl_expected =
23623 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23624 cp_parser_parse_definitely (parser);
23626 if (alias_decl_expected)
23627 finish_member_declaration (decl);
23628 else
23629 cp_parser_using_declaration (parser,
23630 /*access_declaration_p=*/false);
23631 return;
23635 /* Check for @defs. */
23636 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23638 tree ivar, member;
23639 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23640 ivar = ivar_chains;
23641 while (ivar)
23643 member = ivar;
23644 ivar = TREE_CHAIN (member);
23645 TREE_CHAIN (member) = NULL_TREE;
23646 finish_member_declaration (member);
23648 return;
23651 /* If the next token is `static_assert' we have a static assertion. */
23652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23654 cp_parser_static_assert (parser, /*member_p=*/true);
23655 return;
23658 parser->colon_corrects_to_scope_p = false;
23660 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23661 goto out;
23663 /* Parse the decl-specifier-seq. */
23664 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23665 cp_parser_decl_specifier_seq (parser,
23666 CP_PARSER_FLAGS_OPTIONAL,
23667 &decl_specifiers,
23668 &declares_class_or_enum);
23669 /* Check for an invalid type-name. */
23670 if (!decl_specifiers.any_type_specifiers_p
23671 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23672 goto out;
23673 /* If there is no declarator, then the decl-specifier-seq should
23674 specify a type. */
23675 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23677 /* If there was no decl-specifier-seq, and the next token is a
23678 `;', then we have something like:
23680 struct S { ; };
23682 [class.mem]
23684 Each member-declaration shall declare at least one member
23685 name of the class. */
23686 if (!decl_specifiers.any_specifiers_p)
23688 cp_token *token = cp_lexer_peek_token (parser->lexer);
23689 if (!in_system_header_at (token->location))
23691 gcc_rich_location richloc (token->location);
23692 richloc.add_fixit_remove ();
23693 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23696 else
23698 tree type;
23700 /* See if this declaration is a friend. */
23701 friend_p = cp_parser_friend_p (&decl_specifiers);
23702 /* If there were decl-specifiers, check to see if there was
23703 a class-declaration. */
23704 type = check_tag_decl (&decl_specifiers,
23705 /*explicit_type_instantiation_p=*/false);
23706 /* Nested classes have already been added to the class, but
23707 a `friend' needs to be explicitly registered. */
23708 if (friend_p)
23710 /* If the `friend' keyword was present, the friend must
23711 be introduced with a class-key. */
23712 if (!declares_class_or_enum && cxx_dialect < cxx11)
23713 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23714 "in C++03 a class-key must be used "
23715 "when declaring a friend");
23716 /* In this case:
23718 template <typename T> struct A {
23719 friend struct A<T>::B;
23722 A<T>::B will be represented by a TYPENAME_TYPE, and
23723 therefore not recognized by check_tag_decl. */
23724 if (!type)
23726 type = decl_specifiers.type;
23727 if (type && TREE_CODE (type) == TYPE_DECL)
23728 type = TREE_TYPE (type);
23730 if (!type || !TYPE_P (type))
23731 error_at (decl_spec_token_start->location,
23732 "friend declaration does not name a class or "
23733 "function");
23734 else
23735 make_friend_class (current_class_type, type,
23736 /*complain=*/true);
23738 /* If there is no TYPE, an error message will already have
23739 been issued. */
23740 else if (!type || type == error_mark_node)
23742 /* An anonymous aggregate has to be handled specially; such
23743 a declaration really declares a data member (with a
23744 particular type), as opposed to a nested class. */
23745 else if (ANON_AGGR_TYPE_P (type))
23747 /* C++11 9.5/6. */
23748 if (decl_specifiers.storage_class != sc_none)
23749 error_at (decl_spec_token_start->location,
23750 "a storage class on an anonymous aggregate "
23751 "in class scope is not allowed");
23753 /* Remove constructors and such from TYPE, now that we
23754 know it is an anonymous aggregate. */
23755 fixup_anonymous_aggr (type);
23756 /* And make the corresponding data member. */
23757 decl = build_decl (decl_spec_token_start->location,
23758 FIELD_DECL, NULL_TREE, type);
23759 /* Add it to the class. */
23760 finish_member_declaration (decl);
23762 else
23763 cp_parser_check_access_in_redeclaration
23764 (TYPE_NAME (type),
23765 decl_spec_token_start->location);
23768 else
23770 bool assume_semicolon = false;
23772 /* Clear attributes from the decl_specifiers but keep them
23773 around as prefix attributes that apply them to the entity
23774 being declared. */
23775 prefix_attributes = decl_specifiers.attributes;
23776 decl_specifiers.attributes = NULL_TREE;
23778 /* See if these declarations will be friends. */
23779 friend_p = cp_parser_friend_p (&decl_specifiers);
23781 /* Keep going until we hit the `;' at the end of the
23782 declaration. */
23783 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23785 tree attributes = NULL_TREE;
23786 tree first_attribute;
23787 tree initializer;
23788 bool named_bitfld = false;
23790 /* Peek at the next token. */
23791 token = cp_lexer_peek_token (parser->lexer);
23793 /* The following code wants to know early if it is a bit-field
23794 or some other declaration. Attributes can appear before
23795 the `:' token. Skip over them without consuming any tokens
23796 to peek if they are followed by `:'. */
23797 if (cp_next_tokens_can_be_attribute_p (parser)
23798 || (token->type == CPP_NAME
23799 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23800 && (named_bitfld = true)))
23802 size_t n
23803 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23804 token = cp_lexer_peek_nth_token (parser->lexer, n);
23807 /* Check for a bitfield declaration. */
23808 if (token->type == CPP_COLON
23809 || (token->type == CPP_NAME
23810 && token == cp_lexer_peek_token (parser->lexer)
23811 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23812 && (named_bitfld = true)))
23814 tree identifier;
23815 tree width;
23816 tree late_attributes = NULL_TREE;
23818 if (named_bitfld)
23819 identifier = cp_parser_identifier (parser);
23820 else
23821 identifier = NULL_TREE;
23823 /* Look for attributes that apply to the bitfield. */
23824 attributes = cp_parser_attributes_opt (parser);
23826 /* Consume the `:' token. */
23827 cp_lexer_consume_token (parser->lexer);
23829 /* Get the width of the bitfield. */
23830 width = cp_parser_constant_expression (parser, false, NULL,
23831 cxx_dialect >= cxx11);
23833 /* In C++2A and as extension for C++11 and above we allow
23834 default member initializers for bit-fields. */
23835 initializer = NULL_TREE;
23836 if (cxx_dialect >= cxx11
23837 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23838 || cp_lexer_next_token_is (parser->lexer,
23839 CPP_OPEN_BRACE)))
23841 location_t loc
23842 = cp_lexer_peek_token (parser->lexer)->location;
23843 if (cxx_dialect < cxx2a
23844 && !in_system_header_at (loc)
23845 && identifier != NULL_TREE)
23846 pedwarn (loc, 0,
23847 "default member initializers for bit-fields "
23848 "only available with -std=c++2a or "
23849 "-std=gnu++2a");
23851 initializer = cp_parser_save_nsdmi (parser);
23852 if (identifier == NULL_TREE)
23854 error_at (loc, "default member initializer for "
23855 "unnamed bit-field");
23856 initializer = NULL_TREE;
23859 else
23861 /* Look for attributes that apply to the bitfield after
23862 the `:' token and width. This is where GCC used to
23863 parse attributes in the past, pedwarn if there is
23864 a std attribute. */
23865 if (cp_next_tokens_can_be_std_attribute_p (parser))
23866 pedwarn (input_location, OPT_Wpedantic,
23867 "ISO C++ allows bit-field attributes only "
23868 "before the %<:%> token");
23870 late_attributes = cp_parser_attributes_opt (parser);
23873 attributes = attr_chainon (attributes, late_attributes);
23875 /* Remember which attributes are prefix attributes and
23876 which are not. */
23877 first_attribute = attributes;
23878 /* Combine the attributes. */
23879 attributes = attr_chainon (prefix_attributes, attributes);
23881 /* Create the bitfield declaration. */
23882 decl = grokbitfield (identifier
23883 ? make_id_declarator (NULL_TREE,
23884 identifier,
23885 sfk_none)
23886 : NULL,
23887 &decl_specifiers,
23888 width, initializer,
23889 attributes);
23891 else
23893 cp_declarator *declarator;
23894 tree asm_specification;
23895 int ctor_dtor_or_conv_p;
23897 /* Parse the declarator. */
23898 declarator
23899 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23900 &ctor_dtor_or_conv_p,
23901 /*parenthesized_p=*/NULL,
23902 /*member_p=*/true,
23903 friend_p);
23905 /* If something went wrong parsing the declarator, make sure
23906 that we at least consume some tokens. */
23907 if (declarator == cp_error_declarator)
23909 /* Skip to the end of the statement. */
23910 cp_parser_skip_to_end_of_statement (parser);
23911 /* If the next token is not a semicolon, that is
23912 probably because we just skipped over the body of
23913 a function. So, we consume a semicolon if
23914 present, but do not issue an error message if it
23915 is not present. */
23916 if (cp_lexer_next_token_is (parser->lexer,
23917 CPP_SEMICOLON))
23918 cp_lexer_consume_token (parser->lexer);
23919 goto out;
23922 if (declares_class_or_enum & 2)
23923 cp_parser_check_for_definition_in_return_type
23924 (declarator, decl_specifiers.type,
23925 decl_specifiers.locations[ds_type_spec]);
23927 /* Look for an asm-specification. */
23928 asm_specification = cp_parser_asm_specification_opt (parser);
23929 /* Look for attributes that apply to the declaration. */
23930 attributes = cp_parser_attributes_opt (parser);
23931 /* Remember which attributes are prefix attributes and
23932 which are not. */
23933 first_attribute = attributes;
23934 /* Combine the attributes. */
23935 attributes = attr_chainon (prefix_attributes, attributes);
23937 /* If it's an `=', then we have a constant-initializer or a
23938 pure-specifier. It is not correct to parse the
23939 initializer before registering the member declaration
23940 since the member declaration should be in scope while
23941 its initializer is processed. However, the rest of the
23942 front end does not yet provide an interface that allows
23943 us to handle this correctly. */
23944 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23946 /* In [class.mem]:
23948 A pure-specifier shall be used only in the declaration of
23949 a virtual function.
23951 A member-declarator can contain a constant-initializer
23952 only if it declares a static member of integral or
23953 enumeration type.
23955 Therefore, if the DECLARATOR is for a function, we look
23956 for a pure-specifier; otherwise, we look for a
23957 constant-initializer. When we call `grokfield', it will
23958 perform more stringent semantics checks. */
23959 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23960 if (function_declarator_p (declarator)
23961 || (decl_specifiers.type
23962 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23963 && declarator->kind == cdk_id
23964 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23965 == FUNCTION_TYPE)))
23966 initializer = cp_parser_pure_specifier (parser);
23967 else if (decl_specifiers.storage_class != sc_static)
23968 initializer = cp_parser_save_nsdmi (parser);
23969 else if (cxx_dialect >= cxx11)
23971 bool nonconst;
23972 /* Don't require a constant rvalue in C++11, since we
23973 might want a reference constant. We'll enforce
23974 constancy later. */
23975 cp_lexer_consume_token (parser->lexer);
23976 /* Parse the initializer. */
23977 initializer = cp_parser_initializer_clause (parser,
23978 &nonconst);
23980 else
23981 /* Parse the initializer. */
23982 initializer = cp_parser_constant_initializer (parser);
23984 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23985 && !function_declarator_p (declarator))
23987 bool x;
23988 if (decl_specifiers.storage_class != sc_static)
23989 initializer = cp_parser_save_nsdmi (parser);
23990 else
23991 initializer = cp_parser_initializer (parser, &x, &x);
23993 /* Otherwise, there is no initializer. */
23994 else
23995 initializer = NULL_TREE;
23997 /* See if we are probably looking at a function
23998 definition. We are certainly not looking at a
23999 member-declarator. Calling `grokfield' has
24000 side-effects, so we must not do it unless we are sure
24001 that we are looking at a member-declarator. */
24002 if (cp_parser_token_starts_function_definition_p
24003 (cp_lexer_peek_token (parser->lexer)))
24005 /* The grammar does not allow a pure-specifier to be
24006 used when a member function is defined. (It is
24007 possible that this fact is an oversight in the
24008 standard, since a pure function may be defined
24009 outside of the class-specifier. */
24010 if (initializer && initializer_token_start)
24011 error_at (initializer_token_start->location,
24012 "pure-specifier on function-definition");
24013 decl = cp_parser_save_member_function_body (parser,
24014 &decl_specifiers,
24015 declarator,
24016 attributes);
24017 if (parser->fully_implicit_function_template_p)
24018 decl = finish_fully_implicit_template (parser, decl);
24019 /* If the member was not a friend, declare it here. */
24020 if (!friend_p)
24021 finish_member_declaration (decl);
24022 /* Peek at the next token. */
24023 token = cp_lexer_peek_token (parser->lexer);
24024 /* If the next token is a semicolon, consume it. */
24025 if (token->type == CPP_SEMICOLON)
24027 location_t semicolon_loc
24028 = cp_lexer_consume_token (parser->lexer)->location;
24029 gcc_rich_location richloc (semicolon_loc);
24030 richloc.add_fixit_remove ();
24031 warning_at (&richloc, OPT_Wextra_semi,
24032 "extra %<;%> after in-class "
24033 "function definition");
24035 goto out;
24037 else
24038 if (declarator->kind == cdk_function)
24039 declarator->id_loc = token->location;
24040 /* Create the declaration. */
24041 decl = grokfield (declarator, &decl_specifiers,
24042 initializer, /*init_const_expr_p=*/true,
24043 asm_specification, attributes);
24044 if (parser->fully_implicit_function_template_p)
24046 if (friend_p)
24047 finish_fully_implicit_template (parser, 0);
24048 else
24049 decl = finish_fully_implicit_template (parser, decl);
24053 cp_finalize_omp_declare_simd (parser, decl);
24054 cp_finalize_oacc_routine (parser, decl, false);
24056 /* Reset PREFIX_ATTRIBUTES. */
24057 if (attributes != error_mark_node)
24059 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24060 attributes = TREE_CHAIN (attributes);
24061 if (attributes)
24062 TREE_CHAIN (attributes) = NULL_TREE;
24065 /* If there is any qualification still in effect, clear it
24066 now; we will be starting fresh with the next declarator. */
24067 parser->scope = NULL_TREE;
24068 parser->qualifying_scope = NULL_TREE;
24069 parser->object_scope = NULL_TREE;
24070 /* If it's a `,', then there are more declarators. */
24071 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24073 cp_lexer_consume_token (parser->lexer);
24074 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24076 cp_token *token = cp_lexer_previous_token (parser->lexer);
24077 gcc_rich_location richloc (token->location);
24078 richloc.add_fixit_remove ();
24079 error_at (&richloc, "stray %<,%> at end of "
24080 "member declaration");
24083 /* If the next token isn't a `;', then we have a parse error. */
24084 else if (cp_lexer_next_token_is_not (parser->lexer,
24085 CPP_SEMICOLON))
24087 /* The next token might be a ways away from where the
24088 actual semicolon is missing. Find the previous token
24089 and use that for our error position. */
24090 cp_token *token = cp_lexer_previous_token (parser->lexer);
24091 gcc_rich_location richloc (token->location);
24092 richloc.add_fixit_insert_after (";");
24093 error_at (&richloc, "expected %<;%> at end of "
24094 "member declaration");
24096 /* Assume that the user meant to provide a semicolon. If
24097 we were to cp_parser_skip_to_end_of_statement, we might
24098 skip to a semicolon inside a member function definition
24099 and issue nonsensical error messages. */
24100 assume_semicolon = true;
24103 if (decl)
24105 /* Add DECL to the list of members. */
24106 if (!friend_p
24107 /* Explicitly include, eg, NSDMIs, for better error
24108 recovery (c++/58650). */
24109 || !DECL_DECLARES_FUNCTION_P (decl))
24110 finish_member_declaration (decl);
24112 if (TREE_CODE (decl) == FUNCTION_DECL)
24113 cp_parser_save_default_args (parser, decl);
24114 else if (TREE_CODE (decl) == FIELD_DECL
24115 && DECL_INITIAL (decl))
24116 /* Add DECL to the queue of NSDMI to be parsed later. */
24117 vec_safe_push (unparsed_nsdmis, decl);
24120 if (assume_semicolon)
24121 goto out;
24125 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24126 out:
24127 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24130 /* Parse a pure-specifier.
24132 pure-specifier:
24135 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24136 Otherwise, ERROR_MARK_NODE is returned. */
24138 static tree
24139 cp_parser_pure_specifier (cp_parser* parser)
24141 cp_token *token;
24143 /* Look for the `=' token. */
24144 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24145 return error_mark_node;
24146 /* Look for the `0' token. */
24147 token = cp_lexer_peek_token (parser->lexer);
24149 if (token->type == CPP_EOF
24150 || token->type == CPP_PRAGMA_EOL)
24151 return error_mark_node;
24153 cp_lexer_consume_token (parser->lexer);
24155 /* Accept = default or = delete in c++0x mode. */
24156 if (token->keyword == RID_DEFAULT
24157 || token->keyword == RID_DELETE)
24159 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24160 return token->u.value;
24163 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24164 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24166 cp_parser_error (parser,
24167 "invalid pure specifier (only %<= 0%> is allowed)");
24168 cp_parser_skip_to_end_of_statement (parser);
24169 return error_mark_node;
24171 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24173 error_at (token->location, "templates may not be %<virtual%>");
24174 return error_mark_node;
24177 return integer_zero_node;
24180 /* Parse a constant-initializer.
24182 constant-initializer:
24183 = constant-expression
24185 Returns a representation of the constant-expression. */
24187 static tree
24188 cp_parser_constant_initializer (cp_parser* parser)
24190 /* Look for the `=' token. */
24191 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24192 return error_mark_node;
24194 /* It is invalid to write:
24196 struct S { static const int i = { 7 }; };
24199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24201 cp_parser_error (parser,
24202 "a brace-enclosed initializer is not allowed here");
24203 /* Consume the opening brace. */
24204 matching_braces braces;
24205 braces.consume_open (parser);
24206 /* Skip the initializer. */
24207 cp_parser_skip_to_closing_brace (parser);
24208 /* Look for the trailing `}'. */
24209 braces.require_close (parser);
24211 return error_mark_node;
24214 return cp_parser_constant_expression (parser);
24217 /* Derived classes [gram.class.derived] */
24219 /* Parse a base-clause.
24221 base-clause:
24222 : base-specifier-list
24224 base-specifier-list:
24225 base-specifier ... [opt]
24226 base-specifier-list , base-specifier ... [opt]
24228 Returns a TREE_LIST representing the base-classes, in the order in
24229 which they were declared. The representation of each node is as
24230 described by cp_parser_base_specifier.
24232 In the case that no bases are specified, this function will return
24233 NULL_TREE, not ERROR_MARK_NODE. */
24235 static tree
24236 cp_parser_base_clause (cp_parser* parser)
24238 tree bases = NULL_TREE;
24240 /* Look for the `:' that begins the list. */
24241 cp_parser_require (parser, CPP_COLON, RT_COLON);
24243 /* Scan the base-specifier-list. */
24244 while (true)
24246 cp_token *token;
24247 tree base;
24248 bool pack_expansion_p = false;
24250 /* Look for the base-specifier. */
24251 base = cp_parser_base_specifier (parser);
24252 /* Look for the (optional) ellipsis. */
24253 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24255 /* Consume the `...'. */
24256 cp_lexer_consume_token (parser->lexer);
24258 pack_expansion_p = true;
24261 /* Add BASE to the front of the list. */
24262 if (base && base != error_mark_node)
24264 if (pack_expansion_p)
24265 /* Make this a pack expansion type. */
24266 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24268 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24270 TREE_CHAIN (base) = bases;
24271 bases = base;
24274 /* Peek at the next token. */
24275 token = cp_lexer_peek_token (parser->lexer);
24276 /* If it's not a comma, then the list is complete. */
24277 if (token->type != CPP_COMMA)
24278 break;
24279 /* Consume the `,'. */
24280 cp_lexer_consume_token (parser->lexer);
24283 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24284 base class had a qualified name. However, the next name that
24285 appears is certainly not qualified. */
24286 parser->scope = NULL_TREE;
24287 parser->qualifying_scope = NULL_TREE;
24288 parser->object_scope = NULL_TREE;
24290 return nreverse (bases);
24293 /* Parse a base-specifier.
24295 base-specifier:
24296 :: [opt] nested-name-specifier [opt] class-name
24297 virtual access-specifier [opt] :: [opt] nested-name-specifier
24298 [opt] class-name
24299 access-specifier virtual [opt] :: [opt] nested-name-specifier
24300 [opt] class-name
24302 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24303 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24304 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24305 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24307 static tree
24308 cp_parser_base_specifier (cp_parser* parser)
24310 cp_token *token;
24311 bool done = false;
24312 bool virtual_p = false;
24313 bool duplicate_virtual_error_issued_p = false;
24314 bool duplicate_access_error_issued_p = false;
24315 bool class_scope_p, template_p;
24316 tree access = access_default_node;
24317 tree type;
24319 /* Process the optional `virtual' and `access-specifier'. */
24320 while (!done)
24322 /* Peek at the next token. */
24323 token = cp_lexer_peek_token (parser->lexer);
24324 /* Process `virtual'. */
24325 switch (token->keyword)
24327 case RID_VIRTUAL:
24328 /* If `virtual' appears more than once, issue an error. */
24329 if (virtual_p && !duplicate_virtual_error_issued_p)
24331 cp_parser_error (parser,
24332 "%<virtual%> specified more than once in base-specifier");
24333 duplicate_virtual_error_issued_p = true;
24336 virtual_p = true;
24338 /* Consume the `virtual' token. */
24339 cp_lexer_consume_token (parser->lexer);
24341 break;
24343 case RID_PUBLIC:
24344 case RID_PROTECTED:
24345 case RID_PRIVATE:
24346 /* If more than one access specifier appears, issue an
24347 error. */
24348 if (access != access_default_node
24349 && !duplicate_access_error_issued_p)
24351 cp_parser_error (parser,
24352 "more than one access specifier in base-specifier");
24353 duplicate_access_error_issued_p = true;
24356 access = ridpointers[(int) token->keyword];
24358 /* Consume the access-specifier. */
24359 cp_lexer_consume_token (parser->lexer);
24361 break;
24363 default:
24364 done = true;
24365 break;
24368 /* It is not uncommon to see programs mechanically, erroneously, use
24369 the 'typename' keyword to denote (dependent) qualified types
24370 as base classes. */
24371 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24373 token = cp_lexer_peek_token (parser->lexer);
24374 if (!processing_template_decl)
24375 error_at (token->location,
24376 "keyword %<typename%> not allowed outside of templates");
24377 else
24378 error_at (token->location,
24379 "keyword %<typename%> not allowed in this context "
24380 "(the base class is implicitly a type)");
24381 cp_lexer_consume_token (parser->lexer);
24384 /* Look for the optional `::' operator. */
24385 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24386 /* Look for the nested-name-specifier. The simplest way to
24387 implement:
24389 [temp.res]
24391 The keyword `typename' is not permitted in a base-specifier or
24392 mem-initializer; in these contexts a qualified name that
24393 depends on a template-parameter is implicitly assumed to be a
24394 type name.
24396 is to pretend that we have seen the `typename' keyword at this
24397 point. */
24398 cp_parser_nested_name_specifier_opt (parser,
24399 /*typename_keyword_p=*/true,
24400 /*check_dependency_p=*/true,
24401 /*type_p=*/true,
24402 /*is_declaration=*/true);
24403 /* If the base class is given by a qualified name, assume that names
24404 we see are type names or templates, as appropriate. */
24405 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24406 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24408 if (!parser->scope
24409 && cp_lexer_next_token_is_decltype (parser->lexer))
24410 /* DR 950 allows decltype as a base-specifier. */
24411 type = cp_parser_decltype (parser);
24412 else
24414 /* Otherwise, look for the class-name. */
24415 type = cp_parser_class_name (parser,
24416 class_scope_p,
24417 template_p,
24418 typename_type,
24419 /*check_dependency_p=*/true,
24420 /*class_head_p=*/false,
24421 /*is_declaration=*/true);
24422 type = TREE_TYPE (type);
24425 if (type == error_mark_node)
24426 return error_mark_node;
24428 return finish_base_specifier (type, access, virtual_p);
24431 /* Exception handling [gram.exception] */
24433 /* Parse an (optional) noexcept-specification.
24435 noexcept-specification:
24436 noexcept ( constant-expression ) [opt]
24438 If no noexcept-specification is present, returns NULL_TREE.
24439 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24440 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24441 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24442 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24443 in which case a boolean condition is returned instead. */
24445 static tree
24446 cp_parser_noexcept_specification_opt (cp_parser* parser,
24447 bool require_constexpr,
24448 bool* consumed_expr,
24449 bool return_cond)
24451 cp_token *token;
24452 const char *saved_message;
24454 /* Peek at the next token. */
24455 token = cp_lexer_peek_token (parser->lexer);
24457 /* Is it a noexcept-specification? */
24458 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24460 tree expr;
24461 cp_lexer_consume_token (parser->lexer);
24463 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24465 matching_parens parens;
24466 parens.consume_open (parser);
24468 if (require_constexpr)
24470 /* Types may not be defined in an exception-specification. */
24471 saved_message = parser->type_definition_forbidden_message;
24472 parser->type_definition_forbidden_message
24473 = G_("types may not be defined in an exception-specification");
24475 expr = cp_parser_constant_expression (parser);
24477 /* Restore the saved message. */
24478 parser->type_definition_forbidden_message = saved_message;
24480 else
24482 expr = cp_parser_expression (parser);
24483 *consumed_expr = true;
24486 parens.require_close (parser);
24488 else
24490 expr = boolean_true_node;
24491 if (!require_constexpr)
24492 *consumed_expr = false;
24495 /* We cannot build a noexcept-spec right away because this will check
24496 that expr is a constexpr. */
24497 if (!return_cond)
24498 return build_noexcept_spec (expr, tf_warning_or_error);
24499 else
24500 return expr;
24502 else
24503 return NULL_TREE;
24506 /* Parse an (optional) exception-specification.
24508 exception-specification:
24509 throw ( type-id-list [opt] )
24511 Returns a TREE_LIST representing the exception-specification. The
24512 TREE_VALUE of each node is a type. */
24514 static tree
24515 cp_parser_exception_specification_opt (cp_parser* parser)
24517 cp_token *token;
24518 tree type_id_list;
24519 const char *saved_message;
24521 /* Peek at the next token. */
24522 token = cp_lexer_peek_token (parser->lexer);
24524 /* Is it a noexcept-specification? */
24525 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24526 false);
24527 if (type_id_list != NULL_TREE)
24528 return type_id_list;
24530 /* If it's not `throw', then there's no exception-specification. */
24531 if (!cp_parser_is_keyword (token, RID_THROW))
24532 return NULL_TREE;
24534 location_t loc = token->location;
24536 /* Consume the `throw'. */
24537 cp_lexer_consume_token (parser->lexer);
24539 /* Look for the `('. */
24540 matching_parens parens;
24541 parens.require_open (parser);
24543 /* Peek at the next token. */
24544 token = cp_lexer_peek_token (parser->lexer);
24545 /* If it's not a `)', then there is a type-id-list. */
24546 if (token->type != CPP_CLOSE_PAREN)
24548 /* Types may not be defined in an exception-specification. */
24549 saved_message = parser->type_definition_forbidden_message;
24550 parser->type_definition_forbidden_message
24551 = G_("types may not be defined in an exception-specification");
24552 /* Parse the type-id-list. */
24553 type_id_list = cp_parser_type_id_list (parser);
24554 /* Restore the saved message. */
24555 parser->type_definition_forbidden_message = saved_message;
24557 if (cxx_dialect >= cxx17)
24559 error_at (loc, "ISO C++17 does not allow dynamic exception "
24560 "specifications");
24561 type_id_list = NULL_TREE;
24563 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24564 warning_at (loc, OPT_Wdeprecated,
24565 "dynamic exception specifications are deprecated in "
24566 "C++11");
24568 /* In C++17, throw() is equivalent to noexcept (true). throw()
24569 is deprecated in C++11 and above as well, but is still widely used,
24570 so don't warn about it yet. */
24571 else if (cxx_dialect >= cxx17)
24572 type_id_list = noexcept_true_spec;
24573 else
24574 type_id_list = empty_except_spec;
24576 /* Look for the `)'. */
24577 parens.require_close (parser);
24579 return type_id_list;
24582 /* Parse an (optional) type-id-list.
24584 type-id-list:
24585 type-id ... [opt]
24586 type-id-list , type-id ... [opt]
24588 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24589 in the order that the types were presented. */
24591 static tree
24592 cp_parser_type_id_list (cp_parser* parser)
24594 tree types = NULL_TREE;
24596 while (true)
24598 cp_token *token;
24599 tree type;
24601 token = cp_lexer_peek_token (parser->lexer);
24603 /* Get the next type-id. */
24604 type = cp_parser_type_id (parser);
24605 /* Check for invalid 'auto'. */
24606 if (flag_concepts && type_uses_auto (type))
24608 error_at (token->location,
24609 "invalid use of %<auto%> in exception-specification");
24610 type = error_mark_node;
24612 /* Parse the optional ellipsis. */
24613 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24615 /* Consume the `...'. */
24616 cp_lexer_consume_token (parser->lexer);
24618 /* Turn the type into a pack expansion expression. */
24619 type = make_pack_expansion (type);
24621 /* Add it to the list. */
24622 types = add_exception_specifier (types, type, /*complain=*/1);
24623 /* Peek at the next token. */
24624 token = cp_lexer_peek_token (parser->lexer);
24625 /* If it is not a `,', we are done. */
24626 if (token->type != CPP_COMMA)
24627 break;
24628 /* Consume the `,'. */
24629 cp_lexer_consume_token (parser->lexer);
24632 return nreverse (types);
24635 /* Parse a try-block.
24637 try-block:
24638 try compound-statement handler-seq */
24640 static tree
24641 cp_parser_try_block (cp_parser* parser)
24643 tree try_block;
24645 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24646 if (parser->in_function_body
24647 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24648 error ("%<try%> in %<constexpr%> function");
24650 try_block = begin_try_block ();
24651 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24652 finish_try_block (try_block);
24653 cp_parser_handler_seq (parser);
24654 finish_handler_sequence (try_block);
24656 return try_block;
24659 /* Parse a function-try-block.
24661 function-try-block:
24662 try ctor-initializer [opt] function-body handler-seq */
24664 static void
24665 cp_parser_function_try_block (cp_parser* parser)
24667 tree compound_stmt;
24668 tree try_block;
24670 /* Look for the `try' keyword. */
24671 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24672 return;
24673 /* Let the rest of the front end know where we are. */
24674 try_block = begin_function_try_block (&compound_stmt);
24675 /* Parse the function-body. */
24676 cp_parser_ctor_initializer_opt_and_function_body
24677 (parser, /*in_function_try_block=*/true);
24678 /* We're done with the `try' part. */
24679 finish_function_try_block (try_block);
24680 /* Parse the handlers. */
24681 cp_parser_handler_seq (parser);
24682 /* We're done with the handlers. */
24683 finish_function_handler_sequence (try_block, compound_stmt);
24686 /* Parse a handler-seq.
24688 handler-seq:
24689 handler handler-seq [opt] */
24691 static void
24692 cp_parser_handler_seq (cp_parser* parser)
24694 while (true)
24696 cp_token *token;
24698 /* Parse the handler. */
24699 cp_parser_handler (parser);
24700 /* Peek at the next token. */
24701 token = cp_lexer_peek_token (parser->lexer);
24702 /* If it's not `catch' then there are no more handlers. */
24703 if (!cp_parser_is_keyword (token, RID_CATCH))
24704 break;
24708 /* Parse a handler.
24710 handler:
24711 catch ( exception-declaration ) compound-statement */
24713 static void
24714 cp_parser_handler (cp_parser* parser)
24716 tree handler;
24717 tree declaration;
24719 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24720 handler = begin_handler ();
24721 matching_parens parens;
24722 parens.require_open (parser);
24723 declaration = cp_parser_exception_declaration (parser);
24724 finish_handler_parms (declaration, handler);
24725 parens.require_close (parser);
24726 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24727 finish_handler (handler);
24730 /* Parse an exception-declaration.
24732 exception-declaration:
24733 type-specifier-seq declarator
24734 type-specifier-seq abstract-declarator
24735 type-specifier-seq
24738 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24739 ellipsis variant is used. */
24741 static tree
24742 cp_parser_exception_declaration (cp_parser* parser)
24744 cp_decl_specifier_seq type_specifiers;
24745 cp_declarator *declarator;
24746 const char *saved_message;
24748 /* If it's an ellipsis, it's easy to handle. */
24749 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24751 /* Consume the `...' token. */
24752 cp_lexer_consume_token (parser->lexer);
24753 return NULL_TREE;
24756 /* Types may not be defined in exception-declarations. */
24757 saved_message = parser->type_definition_forbidden_message;
24758 parser->type_definition_forbidden_message
24759 = G_("types may not be defined in exception-declarations");
24761 /* Parse the type-specifier-seq. */
24762 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24763 /*is_trailing_return=*/false,
24764 &type_specifiers);
24765 /* If it's a `)', then there is no declarator. */
24766 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24767 declarator = NULL;
24768 else
24769 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24770 /*ctor_dtor_or_conv_p=*/NULL,
24771 /*parenthesized_p=*/NULL,
24772 /*member_p=*/false,
24773 /*friend_p=*/false);
24775 /* Restore the saved message. */
24776 parser->type_definition_forbidden_message = saved_message;
24778 if (!type_specifiers.any_specifiers_p)
24779 return error_mark_node;
24781 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24784 /* Parse a throw-expression.
24786 throw-expression:
24787 throw assignment-expression [opt]
24789 Returns a THROW_EXPR representing the throw-expression. */
24791 static tree
24792 cp_parser_throw_expression (cp_parser* parser)
24794 tree expression;
24795 cp_token* token;
24797 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24798 token = cp_lexer_peek_token (parser->lexer);
24799 /* Figure out whether or not there is an assignment-expression
24800 following the "throw" keyword. */
24801 if (token->type == CPP_COMMA
24802 || token->type == CPP_SEMICOLON
24803 || token->type == CPP_CLOSE_PAREN
24804 || token->type == CPP_CLOSE_SQUARE
24805 || token->type == CPP_CLOSE_BRACE
24806 || token->type == CPP_COLON)
24807 expression = NULL_TREE;
24808 else
24809 expression = cp_parser_assignment_expression (parser);
24811 return build_throw (expression);
24814 /* GNU Extensions */
24816 /* Parse an (optional) asm-specification.
24818 asm-specification:
24819 asm ( string-literal )
24821 If the asm-specification is present, returns a STRING_CST
24822 corresponding to the string-literal. Otherwise, returns
24823 NULL_TREE. */
24825 static tree
24826 cp_parser_asm_specification_opt (cp_parser* parser)
24828 cp_token *token;
24829 tree asm_specification;
24831 /* Peek at the next token. */
24832 token = cp_lexer_peek_token (parser->lexer);
24833 /* If the next token isn't the `asm' keyword, then there's no
24834 asm-specification. */
24835 if (!cp_parser_is_keyword (token, RID_ASM))
24836 return NULL_TREE;
24838 /* Consume the `asm' token. */
24839 cp_lexer_consume_token (parser->lexer);
24840 /* Look for the `('. */
24841 matching_parens parens;
24842 parens.require_open (parser);
24844 /* Look for the string-literal. */
24845 asm_specification = cp_parser_string_literal (parser, false, false);
24847 /* Look for the `)'. */
24848 parens.require_close (parser);
24850 return asm_specification;
24853 /* Parse an asm-operand-list.
24855 asm-operand-list:
24856 asm-operand
24857 asm-operand-list , asm-operand
24859 asm-operand:
24860 string-literal ( expression )
24861 [ string-literal ] string-literal ( expression )
24863 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24864 each node is the expression. The TREE_PURPOSE is itself a
24865 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24866 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24867 is a STRING_CST for the string literal before the parenthesis. Returns
24868 ERROR_MARK_NODE if any of the operands are invalid. */
24870 static tree
24871 cp_parser_asm_operand_list (cp_parser* parser)
24873 tree asm_operands = NULL_TREE;
24874 bool invalid_operands = false;
24876 while (true)
24878 tree string_literal;
24879 tree expression;
24880 tree name;
24882 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24884 /* Consume the `[' token. */
24885 cp_lexer_consume_token (parser->lexer);
24886 /* Read the operand name. */
24887 name = cp_parser_identifier (parser);
24888 if (name != error_mark_node)
24889 name = build_string (IDENTIFIER_LENGTH (name),
24890 IDENTIFIER_POINTER (name));
24891 /* Look for the closing `]'. */
24892 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24894 else
24895 name = NULL_TREE;
24896 /* Look for the string-literal. */
24897 string_literal = cp_parser_string_literal (parser, false, false);
24899 /* Look for the `('. */
24900 matching_parens parens;
24901 parens.require_open (parser);
24902 /* Parse the expression. */
24903 expression = cp_parser_expression (parser);
24904 /* Look for the `)'. */
24905 parens.require_close (parser);
24907 if (name == error_mark_node
24908 || string_literal == error_mark_node
24909 || expression == error_mark_node)
24910 invalid_operands = true;
24912 /* Add this operand to the list. */
24913 asm_operands = tree_cons (build_tree_list (name, string_literal),
24914 expression,
24915 asm_operands);
24916 /* If the next token is not a `,', there are no more
24917 operands. */
24918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24919 break;
24920 /* Consume the `,'. */
24921 cp_lexer_consume_token (parser->lexer);
24924 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24927 /* Parse an asm-clobber-list.
24929 asm-clobber-list:
24930 string-literal
24931 asm-clobber-list , string-literal
24933 Returns a TREE_LIST, indicating the clobbers in the order that they
24934 appeared. The TREE_VALUE of each node is a STRING_CST. */
24936 static tree
24937 cp_parser_asm_clobber_list (cp_parser* parser)
24939 tree clobbers = NULL_TREE;
24941 while (true)
24943 tree string_literal;
24945 /* Look for the string literal. */
24946 string_literal = cp_parser_string_literal (parser, false, false);
24947 /* Add it to the list. */
24948 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24949 /* If the next token is not a `,', then the list is
24950 complete. */
24951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24952 break;
24953 /* Consume the `,' token. */
24954 cp_lexer_consume_token (parser->lexer);
24957 return clobbers;
24960 /* Parse an asm-label-list.
24962 asm-label-list:
24963 identifier
24964 asm-label-list , identifier
24966 Returns a TREE_LIST, indicating the labels in the order that they
24967 appeared. The TREE_VALUE of each node is a label. */
24969 static tree
24970 cp_parser_asm_label_list (cp_parser* parser)
24972 tree labels = NULL_TREE;
24974 while (true)
24976 tree identifier, label, name;
24978 /* Look for the identifier. */
24979 identifier = cp_parser_identifier (parser);
24980 if (!error_operand_p (identifier))
24982 label = lookup_label (identifier);
24983 if (TREE_CODE (label) == LABEL_DECL)
24985 TREE_USED (label) = 1;
24986 check_goto (label);
24987 name = build_string (IDENTIFIER_LENGTH (identifier),
24988 IDENTIFIER_POINTER (identifier));
24989 labels = tree_cons (name, label, labels);
24992 /* If the next token is not a `,', then the list is
24993 complete. */
24994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24995 break;
24996 /* Consume the `,' token. */
24997 cp_lexer_consume_token (parser->lexer);
25000 return nreverse (labels);
25003 /* Return TRUE iff the next tokens in the stream are possibly the
25004 beginning of a GNU extension attribute. */
25006 static bool
25007 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25009 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25012 /* Return TRUE iff the next tokens in the stream are possibly the
25013 beginning of a standard C++-11 attribute specifier. */
25015 static bool
25016 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25018 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25021 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25022 beginning of a standard C++-11 attribute specifier. */
25024 static bool
25025 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25027 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25029 return (cxx_dialect >= cxx11
25030 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25031 || (token->type == CPP_OPEN_SQUARE
25032 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25033 && token->type == CPP_OPEN_SQUARE)));
25036 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25037 beginning of a GNU extension attribute. */
25039 static bool
25040 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25042 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25044 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25047 /* Return true iff the next tokens can be the beginning of either a
25048 GNU attribute list, or a standard C++11 attribute sequence. */
25050 static bool
25051 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25053 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25054 || cp_next_tokens_can_be_std_attribute_p (parser));
25057 /* Return true iff the next Nth tokens can be the beginning of either
25058 a GNU attribute list, or a standard C++11 attribute sequence. */
25060 static bool
25061 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25063 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25064 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25067 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25068 of GNU attributes, or return NULL. */
25070 static tree
25071 cp_parser_attributes_opt (cp_parser *parser)
25073 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25074 return cp_parser_gnu_attributes_opt (parser);
25075 return cp_parser_std_attribute_spec_seq (parser);
25078 /* Parse an (optional) series of attributes.
25080 attributes:
25081 attributes attribute
25083 attribute:
25084 __attribute__ (( attribute-list [opt] ))
25086 The return value is as for cp_parser_gnu_attribute_list. */
25088 static tree
25089 cp_parser_gnu_attributes_opt (cp_parser* parser)
25091 tree attributes = NULL_TREE;
25093 temp_override<bool> cleanup
25094 (parser->auto_is_implicit_function_template_parm_p, false);
25096 while (true)
25098 cp_token *token;
25099 tree attribute_list;
25100 bool ok = true;
25102 /* Peek at the next token. */
25103 token = cp_lexer_peek_token (parser->lexer);
25104 /* If it's not `__attribute__', then we're done. */
25105 if (token->keyword != RID_ATTRIBUTE)
25106 break;
25108 /* Consume the `__attribute__' keyword. */
25109 cp_lexer_consume_token (parser->lexer);
25110 /* Look for the two `(' tokens. */
25111 matching_parens outer_parens;
25112 outer_parens.require_open (parser);
25113 matching_parens inner_parens;
25114 inner_parens.require_open (parser);
25116 /* Peek at the next token. */
25117 token = cp_lexer_peek_token (parser->lexer);
25118 if (token->type != CPP_CLOSE_PAREN)
25119 /* Parse the attribute-list. */
25120 attribute_list = cp_parser_gnu_attribute_list (parser);
25121 else
25122 /* If the next token is a `)', then there is no attribute
25123 list. */
25124 attribute_list = NULL;
25126 /* Look for the two `)' tokens. */
25127 if (!inner_parens.require_close (parser))
25128 ok = false;
25129 if (!outer_parens.require_close (parser))
25130 ok = false;
25131 if (!ok)
25132 cp_parser_skip_to_end_of_statement (parser);
25134 /* Add these new attributes to the list. */
25135 attributes = attr_chainon (attributes, attribute_list);
25138 return attributes;
25141 /* Parse a GNU attribute-list.
25143 attribute-list:
25144 attribute
25145 attribute-list , attribute
25147 attribute:
25148 identifier
25149 identifier ( identifier )
25150 identifier ( identifier , expression-list )
25151 identifier ( expression-list )
25153 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25154 to an attribute. The TREE_PURPOSE of each node is the identifier
25155 indicating which attribute is in use. The TREE_VALUE represents
25156 the arguments, if any. */
25158 static tree
25159 cp_parser_gnu_attribute_list (cp_parser* parser)
25161 tree attribute_list = NULL_TREE;
25162 bool save_translate_strings_p = parser->translate_strings_p;
25164 parser->translate_strings_p = false;
25165 while (true)
25167 cp_token *token;
25168 tree identifier;
25169 tree attribute;
25171 /* Look for the identifier. We also allow keywords here; for
25172 example `__attribute__ ((const))' is legal. */
25173 token = cp_lexer_peek_token (parser->lexer);
25174 if (token->type == CPP_NAME
25175 || token->type == CPP_KEYWORD)
25177 tree arguments = NULL_TREE;
25179 /* Consume the token, but save it since we need it for the
25180 SIMD enabled function parsing. */
25181 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25183 /* Save away the identifier that indicates which attribute
25184 this is. */
25185 identifier = (token->type == CPP_KEYWORD)
25186 /* For keywords, use the canonical spelling, not the
25187 parsed identifier. */
25188 ? ridpointers[(int) token->keyword]
25189 : id_token->u.value;
25191 identifier = canonicalize_attr_name (identifier);
25192 attribute = build_tree_list (identifier, NULL_TREE);
25194 /* Peek at the next token. */
25195 token = cp_lexer_peek_token (parser->lexer);
25196 /* If it's an `(', then parse the attribute arguments. */
25197 if (token->type == CPP_OPEN_PAREN)
25199 vec<tree, va_gc> *vec;
25200 int attr_flag = (attribute_takes_identifier_p (identifier)
25201 ? id_attr : normal_attr);
25202 vec = cp_parser_parenthesized_expression_list
25203 (parser, attr_flag, /*cast_p=*/false,
25204 /*allow_expansion_p=*/false,
25205 /*non_constant_p=*/NULL);
25206 if (vec == NULL)
25207 arguments = error_mark_node;
25208 else
25210 arguments = build_tree_list_vec (vec);
25211 release_tree_vector (vec);
25213 /* Save the arguments away. */
25214 TREE_VALUE (attribute) = arguments;
25217 if (arguments != error_mark_node)
25219 /* Add this attribute to the list. */
25220 TREE_CHAIN (attribute) = attribute_list;
25221 attribute_list = attribute;
25224 token = cp_lexer_peek_token (parser->lexer);
25226 /* Now, look for more attributes. If the next token isn't a
25227 `,', we're done. */
25228 if (token->type != CPP_COMMA)
25229 break;
25231 /* Consume the comma and keep going. */
25232 cp_lexer_consume_token (parser->lexer);
25234 parser->translate_strings_p = save_translate_strings_p;
25236 /* We built up the list in reverse order. */
25237 return nreverse (attribute_list);
25240 /* Parse a standard C++11 attribute.
25242 The returned representation is a TREE_LIST which TREE_PURPOSE is
25243 the scoped name of the attribute, and the TREE_VALUE is its
25244 arguments list.
25246 Note that the scoped name of the attribute is itself a TREE_LIST
25247 which TREE_PURPOSE is the namespace of the attribute, and
25248 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25249 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25250 and which TREE_PURPOSE is directly the attribute name.
25252 Clients of the attribute code should use get_attribute_namespace
25253 and get_attribute_name to get the actual namespace and name of
25254 attributes, regardless of their being GNU or C++11 attributes.
25256 attribute:
25257 attribute-token attribute-argument-clause [opt]
25259 attribute-token:
25260 identifier
25261 attribute-scoped-token
25263 attribute-scoped-token:
25264 attribute-namespace :: identifier
25266 attribute-namespace:
25267 identifier
25269 attribute-argument-clause:
25270 ( balanced-token-seq )
25272 balanced-token-seq:
25273 balanced-token [opt]
25274 balanced-token-seq balanced-token
25276 balanced-token:
25277 ( balanced-token-seq )
25278 [ balanced-token-seq ]
25279 { balanced-token-seq }. */
25281 static tree
25282 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25284 tree attribute, attr_id = NULL_TREE, arguments;
25285 cp_token *token;
25287 temp_override<bool> cleanup
25288 (parser->auto_is_implicit_function_template_parm_p, false);
25290 /* First, parse name of the attribute, a.k.a attribute-token. */
25292 token = cp_lexer_peek_token (parser->lexer);
25293 if (token->type == CPP_NAME)
25294 attr_id = token->u.value;
25295 else if (token->type == CPP_KEYWORD)
25296 attr_id = ridpointers[(int) token->keyword];
25297 else if (token->flags & NAMED_OP)
25298 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25300 if (attr_id == NULL_TREE)
25301 return NULL_TREE;
25303 cp_lexer_consume_token (parser->lexer);
25305 token = cp_lexer_peek_token (parser->lexer);
25306 if (token->type == CPP_SCOPE)
25308 /* We are seeing a scoped attribute token. */
25310 cp_lexer_consume_token (parser->lexer);
25311 if (attr_ns)
25312 error_at (token->location, "attribute using prefix used together "
25313 "with scoped attribute token");
25314 attr_ns = attr_id;
25316 token = cp_lexer_consume_token (parser->lexer);
25317 if (token->type == CPP_NAME)
25318 attr_id = token->u.value;
25319 else if (token->type == CPP_KEYWORD)
25320 attr_id = ridpointers[(int) token->keyword];
25321 else if (token->flags & NAMED_OP)
25322 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25323 else
25325 error_at (token->location,
25326 "expected an identifier for the attribute name");
25327 return error_mark_node;
25330 attr_id = canonicalize_attr_name (attr_id);
25331 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25332 NULL_TREE);
25333 token = cp_lexer_peek_token (parser->lexer);
25335 else if (attr_ns)
25336 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25337 NULL_TREE);
25338 else
25340 attr_id = canonicalize_attr_name (attr_id);
25341 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25342 NULL_TREE);
25343 /* C++11 noreturn attribute is equivalent to GNU's. */
25344 if (is_attribute_p ("noreturn", attr_id))
25345 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25346 /* C++14 deprecated attribute is equivalent to GNU's. */
25347 else if (is_attribute_p ("deprecated", attr_id))
25348 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25349 /* C++17 fallthrough attribute is equivalent to GNU's. */
25350 else if (is_attribute_p ("fallthrough", attr_id))
25351 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25352 /* Transactional Memory TS optimize_for_synchronized attribute is
25353 equivalent to GNU transaction_callable. */
25354 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25355 TREE_PURPOSE (attribute)
25356 = get_identifier ("transaction_callable");
25357 /* Transactional Memory attributes are GNU attributes. */
25358 else if (tm_attr_to_mask (attr_id))
25359 TREE_PURPOSE (attribute) = attr_id;
25362 /* Now parse the optional argument clause of the attribute. */
25364 if (token->type != CPP_OPEN_PAREN)
25365 return attribute;
25368 vec<tree, va_gc> *vec;
25369 int attr_flag = normal_attr;
25371 if (attr_ns == get_identifier ("gnu")
25372 && attribute_takes_identifier_p (attr_id))
25373 /* A GNU attribute that takes an identifier in parameter. */
25374 attr_flag = id_attr;
25376 vec = cp_parser_parenthesized_expression_list
25377 (parser, attr_flag, /*cast_p=*/false,
25378 /*allow_expansion_p=*/true,
25379 /*non_constant_p=*/NULL);
25380 if (vec == NULL)
25381 arguments = error_mark_node;
25382 else
25384 arguments = build_tree_list_vec (vec);
25385 release_tree_vector (vec);
25388 if (arguments == error_mark_node)
25389 attribute = error_mark_node;
25390 else
25391 TREE_VALUE (attribute) = arguments;
25394 return attribute;
25397 /* Check that the attribute ATTRIBUTE appears at most once in the
25398 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25399 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25400 isn't implemented yet in GCC. */
25402 static void
25403 cp_parser_check_std_attribute (tree attributes, tree attribute)
25405 if (attributes)
25407 tree name = get_attribute_name (attribute);
25408 if (is_attribute_p ("noreturn", name)
25409 && lookup_attribute ("noreturn", attributes))
25410 error ("attribute %<noreturn%> can appear at most once "
25411 "in an attribute-list");
25412 else if (is_attribute_p ("deprecated", name)
25413 && lookup_attribute ("deprecated", attributes))
25414 error ("attribute %<deprecated%> can appear at most once "
25415 "in an attribute-list");
25419 /* Parse a list of standard C++-11 attributes.
25421 attribute-list:
25422 attribute [opt]
25423 attribute-list , attribute[opt]
25424 attribute ...
25425 attribute-list , attribute ...
25428 static tree
25429 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25431 tree attributes = NULL_TREE, attribute = NULL_TREE;
25432 cp_token *token = NULL;
25434 while (true)
25436 attribute = cp_parser_std_attribute (parser, attr_ns);
25437 if (attribute == error_mark_node)
25438 break;
25439 if (attribute != NULL_TREE)
25441 cp_parser_check_std_attribute (attributes, attribute);
25442 TREE_CHAIN (attribute) = attributes;
25443 attributes = attribute;
25445 token = cp_lexer_peek_token (parser->lexer);
25446 if (token->type == CPP_ELLIPSIS)
25448 cp_lexer_consume_token (parser->lexer);
25449 if (attribute == NULL_TREE)
25450 error_at (token->location,
25451 "expected attribute before %<...%>");
25452 else
25454 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25455 if (pack == error_mark_node)
25456 return error_mark_node;
25457 TREE_VALUE (attribute) = pack;
25459 token = cp_lexer_peek_token (parser->lexer);
25461 if (token->type != CPP_COMMA)
25462 break;
25463 cp_lexer_consume_token (parser->lexer);
25465 attributes = nreverse (attributes);
25466 return attributes;
25469 /* Parse a standard C++-11 attribute specifier.
25471 attribute-specifier:
25472 [ [ attribute-using-prefix [opt] attribute-list ] ]
25473 alignment-specifier
25475 attribute-using-prefix:
25476 using attribute-namespace :
25478 alignment-specifier:
25479 alignas ( type-id ... [opt] )
25480 alignas ( alignment-expression ... [opt] ). */
25482 static tree
25483 cp_parser_std_attribute_spec (cp_parser *parser)
25485 tree attributes = NULL_TREE;
25486 cp_token *token = cp_lexer_peek_token (parser->lexer);
25488 if (token->type == CPP_OPEN_SQUARE
25489 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25491 tree attr_ns = NULL_TREE;
25493 cp_lexer_consume_token (parser->lexer);
25494 cp_lexer_consume_token (parser->lexer);
25496 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25498 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25499 if (token->type == CPP_NAME)
25500 attr_ns = token->u.value;
25501 else if (token->type == CPP_KEYWORD)
25502 attr_ns = ridpointers[(int) token->keyword];
25503 else if (token->flags & NAMED_OP)
25504 attr_ns = get_identifier (cpp_type2name (token->type,
25505 token->flags));
25506 if (attr_ns
25507 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25509 if (cxx_dialect < cxx17
25510 && !in_system_header_at (input_location))
25511 pedwarn (input_location, 0,
25512 "attribute using prefix only available "
25513 "with -std=c++17 or -std=gnu++17");
25515 cp_lexer_consume_token (parser->lexer);
25516 cp_lexer_consume_token (parser->lexer);
25517 cp_lexer_consume_token (parser->lexer);
25519 else
25520 attr_ns = NULL_TREE;
25523 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25525 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25526 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25527 cp_parser_skip_to_end_of_statement (parser);
25528 else
25529 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25530 when we are sure that we have actually parsed them. */
25531 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25533 else
25535 tree alignas_expr;
25537 /* Look for an alignment-specifier. */
25539 token = cp_lexer_peek_token (parser->lexer);
25541 if (token->type != CPP_KEYWORD
25542 || token->keyword != RID_ALIGNAS)
25543 return NULL_TREE;
25545 cp_lexer_consume_token (parser->lexer);
25546 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25548 matching_parens parens;
25549 if (!parens.require_open (parser))
25550 return error_mark_node;
25552 cp_parser_parse_tentatively (parser);
25553 alignas_expr = cp_parser_type_id (parser);
25555 if (!cp_parser_parse_definitely (parser))
25557 alignas_expr = cp_parser_assignment_expression (parser);
25558 if (alignas_expr == error_mark_node)
25559 cp_parser_skip_to_end_of_statement (parser);
25560 if (alignas_expr == NULL_TREE
25561 || alignas_expr == error_mark_node)
25562 return alignas_expr;
25565 alignas_expr = cxx_alignas_expr (alignas_expr);
25566 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25568 /* Handle alignas (pack...). */
25569 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25571 cp_lexer_consume_token (parser->lexer);
25572 alignas_expr = make_pack_expansion (alignas_expr);
25575 /* Something went wrong, so don't build the attribute. */
25576 if (alignas_expr == error_mark_node)
25577 return error_mark_node;
25579 if (!parens.require_close (parser))
25580 return error_mark_node;
25582 /* Build the C++-11 representation of an 'aligned'
25583 attribute. */
25584 attributes =
25585 build_tree_list (build_tree_list (get_identifier ("gnu"),
25586 get_identifier ("aligned")),
25587 alignas_expr);
25590 return attributes;
25593 /* Parse a standard C++-11 attribute-specifier-seq.
25595 attribute-specifier-seq:
25596 attribute-specifier-seq [opt] attribute-specifier
25599 static tree
25600 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25602 tree attr_specs = NULL_TREE;
25603 tree attr_last = NULL_TREE;
25605 while (true)
25607 tree attr_spec = cp_parser_std_attribute_spec (parser);
25608 if (attr_spec == NULL_TREE)
25609 break;
25610 if (attr_spec == error_mark_node)
25611 return error_mark_node;
25613 if (attr_last)
25614 TREE_CHAIN (attr_last) = attr_spec;
25615 else
25616 attr_specs = attr_last = attr_spec;
25617 attr_last = tree_last (attr_last);
25620 return attr_specs;
25623 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25624 return index of the first token after balanced-token, or N on failure. */
25626 static size_t
25627 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25629 size_t orig_n = n;
25630 int nparens = 0, nbraces = 0, nsquares = 0;
25632 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25634 case CPP_EOF:
25635 case CPP_PRAGMA_EOL:
25636 /* Ran out of tokens. */
25637 return orig_n;
25638 case CPP_OPEN_PAREN:
25639 ++nparens;
25640 break;
25641 case CPP_OPEN_BRACE:
25642 ++nbraces;
25643 break;
25644 case CPP_OPEN_SQUARE:
25645 ++nsquares;
25646 break;
25647 case CPP_CLOSE_PAREN:
25648 --nparens;
25649 break;
25650 case CPP_CLOSE_BRACE:
25651 --nbraces;
25652 break;
25653 case CPP_CLOSE_SQUARE:
25654 --nsquares;
25655 break;
25656 default:
25657 break;
25659 while (nparens || nbraces || nsquares);
25660 return n;
25663 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25664 return index of the first token after the GNU attribute tokens, or N on
25665 failure. */
25667 static size_t
25668 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25670 while (true)
25672 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25673 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25674 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25675 break;
25677 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25678 if (n2 == n + 2)
25679 break;
25680 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25681 break;
25682 n = n2 + 1;
25684 return n;
25687 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25688 next token), return index of the first token after the standard C++11
25689 attribute tokens, or N on failure. */
25691 static size_t
25692 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25694 while (true)
25696 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25697 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25699 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25700 if (n2 == n + 1)
25701 break;
25702 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25703 break;
25704 n = n2 + 1;
25706 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25707 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25709 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25710 if (n2 == n + 1)
25711 break;
25712 n = n2;
25714 else
25715 break;
25717 return n;
25720 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25721 as the next token), return index of the first token after the attribute
25722 tokens, or N on failure. */
25724 static size_t
25725 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25727 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25728 return cp_parser_skip_gnu_attributes_opt (parser, n);
25729 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25732 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25733 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25734 current value of the PEDANTIC flag, regardless of whether or not
25735 the `__extension__' keyword is present. The caller is responsible
25736 for restoring the value of the PEDANTIC flag. */
25738 static bool
25739 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25741 /* Save the old value of the PEDANTIC flag. */
25742 *saved_pedantic = pedantic;
25744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25746 /* Consume the `__extension__' token. */
25747 cp_lexer_consume_token (parser->lexer);
25748 /* We're not being pedantic while the `__extension__' keyword is
25749 in effect. */
25750 pedantic = 0;
25752 return true;
25755 return false;
25758 /* Parse a label declaration.
25760 label-declaration:
25761 __label__ label-declarator-seq ;
25763 label-declarator-seq:
25764 identifier , label-declarator-seq
25765 identifier */
25767 static void
25768 cp_parser_label_declaration (cp_parser* parser)
25770 /* Look for the `__label__' keyword. */
25771 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25773 while (true)
25775 tree identifier;
25777 /* Look for an identifier. */
25778 identifier = cp_parser_identifier (parser);
25779 /* If we failed, stop. */
25780 if (identifier == error_mark_node)
25781 break;
25782 /* Declare it as a label. */
25783 finish_label_decl (identifier);
25784 /* If the next token is a `;', stop. */
25785 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25786 break;
25787 /* Look for the `,' separating the label declarations. */
25788 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25791 /* Look for the final `;'. */
25792 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25795 // -------------------------------------------------------------------------- //
25796 // Requires Clause
25798 // Parse a requires clause.
25800 // requires-clause:
25801 // 'requires' logical-or-expression
25803 // The required logical-or-expression must be a constant expression. Note
25804 // that we don't check that the expression is constepxr here. We defer until
25805 // we analyze constraints and then, we only check atomic constraints.
25806 static tree
25807 cp_parser_requires_clause (cp_parser *parser)
25809 // Parse the requires clause so that it is not automatically folded.
25810 ++processing_template_decl;
25811 tree expr = cp_parser_binary_expression (parser, false, false,
25812 PREC_NOT_OPERATOR, NULL);
25813 if (check_for_bare_parameter_packs (expr))
25814 expr = error_mark_node;
25815 --processing_template_decl;
25816 return expr;
25819 // Optionally parse a requires clause:
25820 static tree
25821 cp_parser_requires_clause_opt (cp_parser *parser)
25823 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25824 if (tok->keyword != RID_REQUIRES)
25826 if (!flag_concepts && tok->type == CPP_NAME
25827 && tok->u.value == ridpointers[RID_REQUIRES])
25829 error_at (cp_lexer_peek_token (parser->lexer)->location,
25830 "%<requires%> only available with -fconcepts");
25831 /* Parse and discard the requires-clause. */
25832 cp_lexer_consume_token (parser->lexer);
25833 cp_parser_requires_clause (parser);
25835 return NULL_TREE;
25837 cp_lexer_consume_token (parser->lexer);
25838 return cp_parser_requires_clause (parser);
25842 /*---------------------------------------------------------------------------
25843 Requires expressions
25844 ---------------------------------------------------------------------------*/
25846 /* Parse a requires expression
25848 requirement-expression:
25849 'requires' requirement-parameter-list [opt] requirement-body */
25850 static tree
25851 cp_parser_requires_expression (cp_parser *parser)
25853 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25854 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25856 /* A requires-expression shall appear only within a concept
25857 definition or a requires-clause.
25859 TODO: Implement this diagnostic correctly. */
25860 if (!processing_template_decl)
25862 error_at (loc, "a requires expression cannot appear outside a template");
25863 cp_parser_skip_to_end_of_statement (parser);
25864 return error_mark_node;
25867 tree parms, reqs;
25869 /* Local parameters are delared as variables within the scope
25870 of the expression. They are not visible past the end of
25871 the expression. Expressions within the requires-expression
25872 are unevaluated. */
25873 struct scope_sentinel
25875 scope_sentinel ()
25877 ++cp_unevaluated_operand;
25878 begin_scope (sk_block, NULL_TREE);
25881 ~scope_sentinel ()
25883 pop_bindings_and_leave_scope ();
25884 --cp_unevaluated_operand;
25886 } s;
25888 /* Parse the optional parameter list. */
25889 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25891 parms = cp_parser_requirement_parameter_list (parser);
25892 if (parms == error_mark_node)
25893 return error_mark_node;
25895 else
25896 parms = NULL_TREE;
25898 /* Parse the requirement body. */
25899 reqs = cp_parser_requirement_body (parser);
25900 if (reqs == error_mark_node)
25901 return error_mark_node;
25904 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25905 the parm chain. */
25906 grokparms (parms, &parms);
25907 return finish_requires_expr (parms, reqs);
25910 /* Parse a parameterized requirement.
25912 requirement-parameter-list:
25913 '(' parameter-declaration-clause ')' */
25914 static tree
25915 cp_parser_requirement_parameter_list (cp_parser *parser)
25917 matching_parens parens;
25918 if (!parens.require_open (parser))
25919 return error_mark_node;
25921 tree parms = cp_parser_parameter_declaration_clause (parser);
25923 if (!parens.require_close (parser))
25924 return error_mark_node;
25926 return parms;
25929 /* Parse the body of a requirement.
25931 requirement-body:
25932 '{' requirement-list '}' */
25933 static tree
25934 cp_parser_requirement_body (cp_parser *parser)
25936 matching_braces braces;
25937 if (!braces.require_open (parser))
25938 return error_mark_node;
25940 tree reqs = cp_parser_requirement_list (parser);
25942 if (!braces.require_close (parser))
25943 return error_mark_node;
25945 return reqs;
25948 /* Parse a list of requirements.
25950 requirement-list:
25951 requirement
25952 requirement-list ';' requirement[opt] */
25953 static tree
25954 cp_parser_requirement_list (cp_parser *parser)
25956 tree result = NULL_TREE;
25957 while (true)
25959 tree req = cp_parser_requirement (parser);
25960 if (req == error_mark_node)
25961 return error_mark_node;
25963 result = tree_cons (NULL_TREE, req, result);
25965 /* If we see a semi-colon, consume it. */
25966 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25967 cp_lexer_consume_token (parser->lexer);
25969 /* Stop processing at the end of the list. */
25970 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25971 break;
25974 /* Reverse the order of requirements so they are analyzed in
25975 declaration order. */
25976 return nreverse (result);
25979 /* Parse a syntactic requirement or type requirement.
25981 requirement:
25982 simple-requirement
25983 compound-requirement
25984 type-requirement
25985 nested-requirement */
25986 static tree
25987 cp_parser_requirement (cp_parser *parser)
25989 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25990 return cp_parser_compound_requirement (parser);
25991 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25992 return cp_parser_type_requirement (parser);
25993 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25994 return cp_parser_nested_requirement (parser);
25995 else
25996 return cp_parser_simple_requirement (parser);
25999 /* Parse a simple requirement.
26001 simple-requirement:
26002 expression ';' */
26003 static tree
26004 cp_parser_simple_requirement (cp_parser *parser)
26006 tree expr = cp_parser_expression (parser, NULL, false, false);
26007 if (!expr || expr == error_mark_node)
26008 return error_mark_node;
26010 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26011 return error_mark_node;
26013 return finish_simple_requirement (expr);
26016 /* Parse a type requirement
26018 type-requirement
26019 nested-name-specifier [opt] required-type-name ';'
26021 required-type-name:
26022 type-name
26023 'template' [opt] simple-template-id */
26024 static tree
26025 cp_parser_type_requirement (cp_parser *parser)
26027 cp_lexer_consume_token (parser->lexer);
26029 // Save the scope before parsing name specifiers.
26030 tree saved_scope = parser->scope;
26031 tree saved_object_scope = parser->object_scope;
26032 tree saved_qualifying_scope = parser->qualifying_scope;
26033 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26034 cp_parser_nested_name_specifier_opt (parser,
26035 /*typename_keyword_p=*/true,
26036 /*check_dependency_p=*/false,
26037 /*type_p=*/true,
26038 /*is_declaration=*/false);
26040 tree type;
26041 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26043 cp_lexer_consume_token (parser->lexer);
26044 type = cp_parser_template_id (parser,
26045 /*template_keyword_p=*/true,
26046 /*check_dependency=*/false,
26047 /*tag_type=*/none_type,
26048 /*is_declaration=*/false);
26049 type = make_typename_type (parser->scope, type, typename_type,
26050 /*complain=*/tf_error);
26052 else
26053 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26055 if (TREE_CODE (type) == TYPE_DECL)
26056 type = TREE_TYPE (type);
26058 parser->scope = saved_scope;
26059 parser->object_scope = saved_object_scope;
26060 parser->qualifying_scope = saved_qualifying_scope;
26062 if (type == error_mark_node)
26063 cp_parser_skip_to_end_of_statement (parser);
26065 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26066 return error_mark_node;
26067 if (type == error_mark_node)
26068 return error_mark_node;
26070 return finish_type_requirement (type);
26073 /* Parse a compound requirement
26075 compound-requirement:
26076 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26077 static tree
26078 cp_parser_compound_requirement (cp_parser *parser)
26080 /* Parse an expression enclosed in '{ }'s. */
26081 matching_braces braces;
26082 if (!braces.require_open (parser))
26083 return error_mark_node;
26085 tree expr = cp_parser_expression (parser, NULL, false, false);
26086 if (!expr || expr == error_mark_node)
26087 return error_mark_node;
26089 if (!braces.require_close (parser))
26090 return error_mark_node;
26092 /* Parse the optional noexcept. */
26093 bool noexcept_p = false;
26094 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26096 cp_lexer_consume_token (parser->lexer);
26097 noexcept_p = true;
26100 /* Parse the optional trailing return type. */
26101 tree type = NULL_TREE;
26102 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26104 cp_lexer_consume_token (parser->lexer);
26105 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26106 parser->in_result_type_constraint_p = true;
26107 type = cp_parser_trailing_type_id (parser);
26108 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26109 if (type == error_mark_node)
26110 return error_mark_node;
26113 return finish_compound_requirement (expr, type, noexcept_p);
26116 /* Parse a nested requirement. This is the same as a requires clause.
26118 nested-requirement:
26119 requires-clause */
26120 static tree
26121 cp_parser_nested_requirement (cp_parser *parser)
26123 cp_lexer_consume_token (parser->lexer);
26124 tree req = cp_parser_requires_clause (parser);
26125 if (req == error_mark_node)
26126 return error_mark_node;
26127 return finish_nested_requirement (req);
26130 /* Support Functions */
26132 /* Return the appropriate prefer_type argument for lookup_name_real based on
26133 tag_type and template_mem_access. */
26135 static inline int
26136 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26138 /* DR 141: When looking in the current enclosing context for a template-name
26139 after -> or ., only consider class templates. */
26140 if (template_mem_access)
26141 return 2;
26142 switch (tag_type)
26144 case none_type: return 0; // No preference.
26145 case scope_type: return 1; // Type or namespace.
26146 default: return 2; // Type only.
26150 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26151 NAME should have one of the representations used for an
26152 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26153 is returned. If PARSER->SCOPE is a dependent type, then a
26154 SCOPE_REF is returned.
26156 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26157 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26158 was formed. Abstractly, such entities should not be passed to this
26159 function, because they do not need to be looked up, but it is
26160 simpler to check for this special case here, rather than at the
26161 call-sites.
26163 In cases not explicitly covered above, this function returns a
26164 DECL, OVERLOAD, or baselink representing the result of the lookup.
26165 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26166 is returned.
26168 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26169 (e.g., "struct") that was used. In that case bindings that do not
26170 refer to types are ignored.
26172 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26173 ignored.
26175 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26176 are ignored.
26178 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26179 types.
26181 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26182 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26183 NULL_TREE otherwise. */
26185 static cp_expr
26186 cp_parser_lookup_name (cp_parser *parser, tree name,
26187 enum tag_types tag_type,
26188 bool is_template,
26189 bool is_namespace,
26190 bool check_dependency,
26191 tree *ambiguous_decls,
26192 location_t name_location)
26194 tree decl;
26195 tree object_type = parser->context->object_type;
26197 /* Assume that the lookup will be unambiguous. */
26198 if (ambiguous_decls)
26199 *ambiguous_decls = NULL_TREE;
26201 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26202 no longer valid. Note that if we are parsing tentatively, and
26203 the parse fails, OBJECT_TYPE will be automatically restored. */
26204 parser->context->object_type = NULL_TREE;
26206 if (name == error_mark_node)
26207 return error_mark_node;
26209 /* A template-id has already been resolved; there is no lookup to
26210 do. */
26211 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26212 return name;
26213 if (BASELINK_P (name))
26215 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26216 == TEMPLATE_ID_EXPR);
26217 return name;
26220 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26221 it should already have been checked to make sure that the name
26222 used matches the type being destroyed. */
26223 if (TREE_CODE (name) == BIT_NOT_EXPR)
26225 tree type;
26227 /* Figure out to which type this destructor applies. */
26228 if (parser->scope)
26229 type = parser->scope;
26230 else if (object_type)
26231 type = object_type;
26232 else
26233 type = current_class_type;
26234 /* If that's not a class type, there is no destructor. */
26235 if (!type || !CLASS_TYPE_P (type))
26236 return error_mark_node;
26238 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26239 lazily_declare_fn (sfk_destructor, type);
26241 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26242 return dtor;
26244 return error_mark_node;
26247 /* By this point, the NAME should be an ordinary identifier. If
26248 the id-expression was a qualified name, the qualifying scope is
26249 stored in PARSER->SCOPE at this point. */
26250 gcc_assert (identifier_p (name));
26252 /* Perform the lookup. */
26253 if (parser->scope)
26255 bool dependent_p;
26257 if (parser->scope == error_mark_node)
26258 return error_mark_node;
26260 /* If the SCOPE is dependent, the lookup must be deferred until
26261 the template is instantiated -- unless we are explicitly
26262 looking up names in uninstantiated templates. Even then, we
26263 cannot look up the name if the scope is not a class type; it
26264 might, for example, be a template type parameter. */
26265 dependent_p = (TYPE_P (parser->scope)
26266 && dependent_scope_p (parser->scope));
26267 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26268 && dependent_p)
26269 /* Defer lookup. */
26270 decl = error_mark_node;
26271 else
26273 tree pushed_scope = NULL_TREE;
26275 /* If PARSER->SCOPE is a dependent type, then it must be a
26276 class type, and we must not be checking dependencies;
26277 otherwise, we would have processed this lookup above. So
26278 that PARSER->SCOPE is not considered a dependent base by
26279 lookup_member, we must enter the scope here. */
26280 if (dependent_p)
26281 pushed_scope = push_scope (parser->scope);
26283 /* If the PARSER->SCOPE is a template specialization, it
26284 may be instantiated during name lookup. In that case,
26285 errors may be issued. Even if we rollback the current
26286 tentative parse, those errors are valid. */
26287 decl = lookup_qualified_name (parser->scope, name,
26288 prefer_type_arg (tag_type),
26289 /*complain=*/true);
26291 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26292 lookup result and the nested-name-specifier nominates a class C:
26293 * if the name specified after the nested-name-specifier, when
26294 looked up in C, is the injected-class-name of C (Clause 9), or
26295 * if the name specified after the nested-name-specifier is the
26296 same as the identifier or the simple-template-id's template-
26297 name in the last component of the nested-name-specifier,
26298 the name is instead considered to name the constructor of
26299 class C. [ Note: for example, the constructor is not an
26300 acceptable lookup result in an elaborated-type-specifier so
26301 the constructor would not be used in place of the
26302 injected-class-name. --end note ] Such a constructor name
26303 shall be used only in the declarator-id of a declaration that
26304 names a constructor or in a using-declaration. */
26305 if (tag_type == none_type
26306 && DECL_SELF_REFERENCE_P (decl)
26307 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26308 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26309 prefer_type_arg (tag_type),
26310 /*complain=*/true);
26312 /* If we have a single function from a using decl, pull it out. */
26313 if (TREE_CODE (decl) == OVERLOAD
26314 && !really_overloaded_fn (decl))
26315 decl = OVL_FUNCTION (decl);
26317 if (pushed_scope)
26318 pop_scope (pushed_scope);
26321 /* If the scope is a dependent type and either we deferred lookup or
26322 we did lookup but didn't find the name, rememeber the name. */
26323 if (decl == error_mark_node && TYPE_P (parser->scope)
26324 && dependent_type_p (parser->scope))
26326 if (tag_type)
26328 tree type;
26330 /* The resolution to Core Issue 180 says that `struct
26331 A::B' should be considered a type-name, even if `A'
26332 is dependent. */
26333 type = make_typename_type (parser->scope, name, tag_type,
26334 /*complain=*/tf_error);
26335 if (type != error_mark_node)
26336 decl = TYPE_NAME (type);
26338 else if (is_template
26339 && (cp_parser_next_token_ends_template_argument_p (parser)
26340 || cp_lexer_next_token_is (parser->lexer,
26341 CPP_CLOSE_PAREN)))
26342 decl = make_unbound_class_template (parser->scope,
26343 name, NULL_TREE,
26344 /*complain=*/tf_error);
26345 else
26346 decl = build_qualified_name (/*type=*/NULL_TREE,
26347 parser->scope, name,
26348 is_template);
26350 parser->qualifying_scope = parser->scope;
26351 parser->object_scope = NULL_TREE;
26353 else if (object_type)
26355 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26356 OBJECT_TYPE is not a class. */
26357 if (CLASS_TYPE_P (object_type))
26358 /* If the OBJECT_TYPE is a template specialization, it may
26359 be instantiated during name lookup. In that case, errors
26360 may be issued. Even if we rollback the current tentative
26361 parse, those errors are valid. */
26362 decl = lookup_member (object_type,
26363 name,
26364 /*protect=*/0,
26365 prefer_type_arg (tag_type),
26366 tf_warning_or_error);
26367 else
26368 decl = NULL_TREE;
26370 if (!decl)
26371 /* Look it up in the enclosing context. DR 141: When looking for a
26372 template-name after -> or ., only consider class templates. */
26373 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26374 /*nonclass=*/0,
26375 /*block_p=*/true, is_namespace, 0);
26376 if (object_type == unknown_type_node)
26377 /* The object is type-dependent, so we can't look anything up; we used
26378 this to get the DR 141 behavior. */
26379 object_type = NULL_TREE;
26380 parser->object_scope = object_type;
26381 parser->qualifying_scope = NULL_TREE;
26383 else
26385 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26386 /*nonclass=*/0,
26387 /*block_p=*/true, is_namespace, 0);
26388 parser->qualifying_scope = NULL_TREE;
26389 parser->object_scope = NULL_TREE;
26392 /* If the lookup failed, let our caller know. */
26393 if (!decl || decl == error_mark_node)
26394 return error_mark_node;
26396 /* Pull out the template from an injected-class-name (or multiple). */
26397 if (is_template)
26398 decl = maybe_get_template_decl_from_type_decl (decl);
26400 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26401 if (TREE_CODE (decl) == TREE_LIST)
26403 if (ambiguous_decls)
26404 *ambiguous_decls = decl;
26405 /* The error message we have to print is too complicated for
26406 cp_parser_error, so we incorporate its actions directly. */
26407 if (!cp_parser_simulate_error (parser))
26409 error_at (name_location, "reference to %qD is ambiguous",
26410 name);
26411 print_candidates (decl);
26413 return error_mark_node;
26416 gcc_assert (DECL_P (decl)
26417 || TREE_CODE (decl) == OVERLOAD
26418 || TREE_CODE (decl) == SCOPE_REF
26419 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26420 || BASELINK_P (decl));
26422 /* If we have resolved the name of a member declaration, check to
26423 see if the declaration is accessible. When the name resolves to
26424 set of overloaded functions, accessibility is checked when
26425 overload resolution is done.
26427 During an explicit instantiation, access is not checked at all,
26428 as per [temp.explicit]. */
26429 if (DECL_P (decl))
26430 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26432 maybe_record_typedef_use (decl);
26434 return cp_expr (decl, name_location);
26437 /* Like cp_parser_lookup_name, but for use in the typical case where
26438 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26439 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26441 static tree
26442 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26444 return cp_parser_lookup_name (parser, name,
26445 none_type,
26446 /*is_template=*/false,
26447 /*is_namespace=*/false,
26448 /*check_dependency=*/true,
26449 /*ambiguous_decls=*/NULL,
26450 location);
26453 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26454 the current context, return the TYPE_DECL. If TAG_NAME_P is
26455 true, the DECL indicates the class being defined in a class-head,
26456 or declared in an elaborated-type-specifier.
26458 Otherwise, return DECL. */
26460 static tree
26461 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26463 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26464 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26466 struct A {
26467 template <typename T> struct B;
26470 template <typename T> struct A::B {};
26472 Similarly, in an elaborated-type-specifier:
26474 namespace N { struct X{}; }
26476 struct A {
26477 template <typename T> friend struct N::X;
26480 However, if the DECL refers to a class type, and we are in
26481 the scope of the class, then the name lookup automatically
26482 finds the TYPE_DECL created by build_self_reference rather
26483 than a TEMPLATE_DECL. For example, in:
26485 template <class T> struct S {
26486 S s;
26489 there is no need to handle such case. */
26491 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26492 return DECL_TEMPLATE_RESULT (decl);
26494 return decl;
26497 /* If too many, or too few, template-parameter lists apply to the
26498 declarator, issue an error message. Returns TRUE if all went well,
26499 and FALSE otherwise. */
26501 static bool
26502 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26503 cp_declarator *declarator,
26504 location_t declarator_location)
26506 switch (declarator->kind)
26508 case cdk_id:
26510 unsigned num_templates = 0;
26511 tree scope = declarator->u.id.qualifying_scope;
26512 bool template_id_p = false;
26514 if (scope)
26515 num_templates = num_template_headers_for_class (scope);
26516 else if (TREE_CODE (declarator->u.id.unqualified_name)
26517 == TEMPLATE_ID_EXPR)
26519 /* If the DECLARATOR has the form `X<y>' then it uses one
26520 additional level of template parameters. */
26521 ++num_templates;
26522 template_id_p = true;
26525 return cp_parser_check_template_parameters
26526 (parser, num_templates, template_id_p, declarator_location,
26527 declarator);
26530 case cdk_function:
26531 case cdk_array:
26532 case cdk_pointer:
26533 case cdk_reference:
26534 case cdk_ptrmem:
26535 return (cp_parser_check_declarator_template_parameters
26536 (parser, declarator->declarator, declarator_location));
26538 case cdk_decomp:
26539 case cdk_error:
26540 return true;
26542 default:
26543 gcc_unreachable ();
26545 return false;
26548 /* NUM_TEMPLATES were used in the current declaration. If that is
26549 invalid, return FALSE and issue an error messages. Otherwise,
26550 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26551 declarator and we can print more accurate diagnostics. */
26553 static bool
26554 cp_parser_check_template_parameters (cp_parser* parser,
26555 unsigned num_templates,
26556 bool template_id_p,
26557 location_t location,
26558 cp_declarator *declarator)
26560 /* If there are the same number of template classes and parameter
26561 lists, that's OK. */
26562 if (parser->num_template_parameter_lists == num_templates)
26563 return true;
26564 /* If there are more, but only one more, and the name ends in an identifier,
26565 then we are declaring a primary template. That's OK too. */
26566 if (!template_id_p
26567 && parser->num_template_parameter_lists == num_templates + 1)
26568 return true;
26569 /* If there are more template classes than parameter lists, we have
26570 something like:
26572 template <class T> void S<T>::R<T>::f (); */
26573 if (parser->num_template_parameter_lists < num_templates)
26575 if (declarator && !current_function_decl)
26576 error_at (location, "specializing member %<%T::%E%> "
26577 "requires %<template<>%> syntax",
26578 declarator->u.id.qualifying_scope,
26579 declarator->u.id.unqualified_name);
26580 else if (declarator)
26581 error_at (location, "invalid declaration of %<%T::%E%>",
26582 declarator->u.id.qualifying_scope,
26583 declarator->u.id.unqualified_name);
26584 else
26585 error_at (location, "too few template-parameter-lists");
26586 return false;
26588 /* Otherwise, there are too many template parameter lists. We have
26589 something like:
26591 template <class T> template <class U> void S::f(); */
26592 error_at (location, "too many template-parameter-lists");
26593 return false;
26596 /* Parse an optional `::' token indicating that the following name is
26597 from the global namespace. If so, PARSER->SCOPE is set to the
26598 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26599 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26600 Returns the new value of PARSER->SCOPE, if the `::' token is
26601 present, and NULL_TREE otherwise. */
26603 static tree
26604 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26606 cp_token *token;
26608 /* Peek at the next token. */
26609 token = cp_lexer_peek_token (parser->lexer);
26610 /* If we're looking at a `::' token then we're starting from the
26611 global namespace, not our current location. */
26612 if (token->type == CPP_SCOPE)
26614 /* Consume the `::' token. */
26615 cp_lexer_consume_token (parser->lexer);
26616 /* Set the SCOPE so that we know where to start the lookup. */
26617 parser->scope = global_namespace;
26618 parser->qualifying_scope = global_namespace;
26619 parser->object_scope = NULL_TREE;
26621 return parser->scope;
26623 else if (!current_scope_valid_p)
26625 parser->scope = NULL_TREE;
26626 parser->qualifying_scope = NULL_TREE;
26627 parser->object_scope = NULL_TREE;
26630 return NULL_TREE;
26633 /* Returns TRUE if the upcoming token sequence is the start of a
26634 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26635 declarator is preceded by the `friend' specifier. */
26637 static bool
26638 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26640 bool constructor_p;
26641 bool outside_class_specifier_p;
26642 tree nested_name_specifier;
26643 cp_token *next_token;
26645 /* The common case is that this is not a constructor declarator, so
26646 try to avoid doing lots of work if at all possible. It's not
26647 valid declare a constructor at function scope. */
26648 if (parser->in_function_body)
26649 return false;
26650 /* And only certain tokens can begin a constructor declarator. */
26651 next_token = cp_lexer_peek_token (parser->lexer);
26652 if (next_token->type != CPP_NAME
26653 && next_token->type != CPP_SCOPE
26654 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26655 && next_token->type != CPP_TEMPLATE_ID)
26656 return false;
26658 /* Parse tentatively; we are going to roll back all of the tokens
26659 consumed here. */
26660 cp_parser_parse_tentatively (parser);
26661 /* Assume that we are looking at a constructor declarator. */
26662 constructor_p = true;
26664 /* Look for the optional `::' operator. */
26665 cp_parser_global_scope_opt (parser,
26666 /*current_scope_valid_p=*/false);
26667 /* Look for the nested-name-specifier. */
26668 nested_name_specifier
26669 = (cp_parser_nested_name_specifier_opt (parser,
26670 /*typename_keyword_p=*/false,
26671 /*check_dependency_p=*/false,
26672 /*type_p=*/false,
26673 /*is_declaration=*/false));
26675 outside_class_specifier_p = (!at_class_scope_p ()
26676 || !TYPE_BEING_DEFINED (current_class_type)
26677 || friend_p);
26679 /* Outside of a class-specifier, there must be a
26680 nested-name-specifier. Except in C++17 mode, where we
26681 might be declaring a guiding declaration. */
26682 if (!nested_name_specifier && outside_class_specifier_p
26683 && cxx_dialect < cxx17)
26684 constructor_p = false;
26685 else if (nested_name_specifier == error_mark_node)
26686 constructor_p = false;
26688 /* If we have a class scope, this is easy; DR 147 says that S::S always
26689 names the constructor, and no other qualified name could. */
26690 if (constructor_p && nested_name_specifier
26691 && CLASS_TYPE_P (nested_name_specifier))
26693 tree id = cp_parser_unqualified_id (parser,
26694 /*template_keyword_p=*/false,
26695 /*check_dependency_p=*/false,
26696 /*declarator_p=*/true,
26697 /*optional_p=*/false);
26698 if (is_overloaded_fn (id))
26699 id = DECL_NAME (get_first_fn (id));
26700 if (!constructor_name_p (id, nested_name_specifier))
26701 constructor_p = false;
26703 /* If we still think that this might be a constructor-declarator,
26704 look for a class-name. */
26705 else if (constructor_p)
26707 /* If we have:
26709 template <typename T> struct S {
26710 S();
26713 we must recognize that the nested `S' names a class. */
26714 if (cxx_dialect >= cxx17)
26715 cp_parser_parse_tentatively (parser);
26717 tree type_decl;
26718 type_decl = cp_parser_class_name (parser,
26719 /*typename_keyword_p=*/false,
26720 /*template_keyword_p=*/false,
26721 none_type,
26722 /*check_dependency_p=*/false,
26723 /*class_head_p=*/false,
26724 /*is_declaration=*/false);
26726 if (cxx_dialect >= cxx17
26727 && !cp_parser_parse_definitely (parser))
26729 type_decl = NULL_TREE;
26730 tree tmpl = cp_parser_template_name (parser,
26731 /*template_keyword*/false,
26732 /*check_dependency_p*/false,
26733 /*is_declaration*/false,
26734 none_type,
26735 /*is_identifier*/NULL);
26736 if (DECL_CLASS_TEMPLATE_P (tmpl)
26737 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26738 /* It's a deduction guide, return true. */;
26739 else
26740 cp_parser_simulate_error (parser);
26743 /* If there was no class-name, then this is not a constructor.
26744 Otherwise, if we are in a class-specifier and we aren't
26745 handling a friend declaration, check that its type matches
26746 current_class_type (c++/38313). Note: error_mark_node
26747 is left alone for error recovery purposes. */
26748 constructor_p = (!cp_parser_error_occurred (parser)
26749 && (outside_class_specifier_p
26750 || type_decl == NULL_TREE
26751 || type_decl == error_mark_node
26752 || same_type_p (current_class_type,
26753 TREE_TYPE (type_decl))));
26755 /* If we're still considering a constructor, we have to see a `(',
26756 to begin the parameter-declaration-clause, followed by either a
26757 `)', an `...', or a decl-specifier. We need to check for a
26758 type-specifier to avoid being fooled into thinking that:
26760 S (f) (int);
26762 is a constructor. (It is actually a function named `f' that
26763 takes one parameter (of type `int') and returns a value of type
26764 `S'. */
26765 if (constructor_p
26766 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26767 constructor_p = false;
26769 if (constructor_p
26770 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26771 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26772 /* A parameter declaration begins with a decl-specifier,
26773 which is either the "attribute" keyword, a storage class
26774 specifier, or (usually) a type-specifier. */
26775 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26777 tree type;
26778 tree pushed_scope = NULL_TREE;
26779 unsigned saved_num_template_parameter_lists;
26781 /* Names appearing in the type-specifier should be looked up
26782 in the scope of the class. */
26783 if (current_class_type)
26784 type = NULL_TREE;
26785 else if (type_decl)
26787 type = TREE_TYPE (type_decl);
26788 if (TREE_CODE (type) == TYPENAME_TYPE)
26790 type = resolve_typename_type (type,
26791 /*only_current_p=*/false);
26792 if (TREE_CODE (type) == TYPENAME_TYPE)
26794 cp_parser_abort_tentative_parse (parser);
26795 return false;
26798 pushed_scope = push_scope (type);
26801 /* Inside the constructor parameter list, surrounding
26802 template-parameter-lists do not apply. */
26803 saved_num_template_parameter_lists
26804 = parser->num_template_parameter_lists;
26805 parser->num_template_parameter_lists = 0;
26807 /* Look for the type-specifier. */
26808 cp_parser_type_specifier (parser,
26809 CP_PARSER_FLAGS_NONE,
26810 /*decl_specs=*/NULL,
26811 /*is_declarator=*/true,
26812 /*declares_class_or_enum=*/NULL,
26813 /*is_cv_qualifier=*/NULL);
26815 parser->num_template_parameter_lists
26816 = saved_num_template_parameter_lists;
26818 /* Leave the scope of the class. */
26819 if (pushed_scope)
26820 pop_scope (pushed_scope);
26822 constructor_p = !cp_parser_error_occurred (parser);
26826 /* We did not really want to consume any tokens. */
26827 cp_parser_abort_tentative_parse (parser);
26829 return constructor_p;
26832 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26833 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26834 they must be performed once we are in the scope of the function.
26836 Returns the function defined. */
26838 static tree
26839 cp_parser_function_definition_from_specifiers_and_declarator
26840 (cp_parser* parser,
26841 cp_decl_specifier_seq *decl_specifiers,
26842 tree attributes,
26843 const cp_declarator *declarator)
26845 tree fn;
26846 bool success_p;
26848 /* Begin the function-definition. */
26849 success_p = start_function (decl_specifiers, declarator, attributes);
26851 /* The things we're about to see are not directly qualified by any
26852 template headers we've seen thus far. */
26853 reset_specialization ();
26855 /* If there were names looked up in the decl-specifier-seq that we
26856 did not check, check them now. We must wait until we are in the
26857 scope of the function to perform the checks, since the function
26858 might be a friend. */
26859 perform_deferred_access_checks (tf_warning_or_error);
26861 if (success_p)
26863 cp_finalize_omp_declare_simd (parser, current_function_decl);
26864 parser->omp_declare_simd = NULL;
26865 cp_finalize_oacc_routine (parser, current_function_decl, true);
26866 parser->oacc_routine = NULL;
26869 if (!success_p)
26871 /* Skip the entire function. */
26872 cp_parser_skip_to_end_of_block_or_statement (parser);
26873 fn = error_mark_node;
26875 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26877 /* Seen already, skip it. An error message has already been output. */
26878 cp_parser_skip_to_end_of_block_or_statement (parser);
26879 fn = current_function_decl;
26880 current_function_decl = NULL_TREE;
26881 /* If this is a function from a class, pop the nested class. */
26882 if (current_class_name)
26883 pop_nested_class ();
26885 else
26887 timevar_id_t tv;
26888 if (DECL_DECLARED_INLINE_P (current_function_decl))
26889 tv = TV_PARSE_INLINE;
26890 else
26891 tv = TV_PARSE_FUNC;
26892 timevar_push (tv);
26893 fn = cp_parser_function_definition_after_declarator (parser,
26894 /*inline_p=*/false);
26895 timevar_pop (tv);
26898 return fn;
26901 /* Parse the part of a function-definition that follows the
26902 declarator. INLINE_P is TRUE iff this function is an inline
26903 function defined within a class-specifier.
26905 Returns the function defined. */
26907 static tree
26908 cp_parser_function_definition_after_declarator (cp_parser* parser,
26909 bool inline_p)
26911 tree fn;
26912 bool saved_in_unbraced_linkage_specification_p;
26913 bool saved_in_function_body;
26914 unsigned saved_num_template_parameter_lists;
26915 cp_token *token;
26916 bool fully_implicit_function_template_p
26917 = parser->fully_implicit_function_template_p;
26918 parser->fully_implicit_function_template_p = false;
26919 tree implicit_template_parms
26920 = parser->implicit_template_parms;
26921 parser->implicit_template_parms = 0;
26922 cp_binding_level* implicit_template_scope
26923 = parser->implicit_template_scope;
26924 parser->implicit_template_scope = 0;
26926 saved_in_function_body = parser->in_function_body;
26927 parser->in_function_body = true;
26928 /* If the next token is `return', then the code may be trying to
26929 make use of the "named return value" extension that G++ used to
26930 support. */
26931 token = cp_lexer_peek_token (parser->lexer);
26932 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26934 /* Consume the `return' keyword. */
26935 cp_lexer_consume_token (parser->lexer);
26936 /* Look for the identifier that indicates what value is to be
26937 returned. */
26938 cp_parser_identifier (parser);
26939 /* Issue an error message. */
26940 error_at (token->location,
26941 "named return values are no longer supported");
26942 /* Skip tokens until we reach the start of the function body. */
26943 while (true)
26945 cp_token *token = cp_lexer_peek_token (parser->lexer);
26946 if (token->type == CPP_OPEN_BRACE
26947 || token->type == CPP_EOF
26948 || token->type == CPP_PRAGMA_EOL)
26949 break;
26950 cp_lexer_consume_token (parser->lexer);
26953 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26954 anything declared inside `f'. */
26955 saved_in_unbraced_linkage_specification_p
26956 = parser->in_unbraced_linkage_specification_p;
26957 parser->in_unbraced_linkage_specification_p = false;
26958 /* Inside the function, surrounding template-parameter-lists do not
26959 apply. */
26960 saved_num_template_parameter_lists
26961 = parser->num_template_parameter_lists;
26962 parser->num_template_parameter_lists = 0;
26964 /* If the next token is `try', `__transaction_atomic', or
26965 `__transaction_relaxed`, then we are looking at either function-try-block
26966 or function-transaction-block. Note that all of these include the
26967 function-body. */
26968 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26969 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26970 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26971 RID_TRANSACTION_RELAXED))
26972 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26973 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26974 cp_parser_function_try_block (parser);
26975 else
26976 cp_parser_ctor_initializer_opt_and_function_body
26977 (parser, /*in_function_try_block=*/false);
26979 /* Finish the function. */
26980 fn = finish_function (inline_p);
26981 /* Generate code for it, if necessary. */
26982 expand_or_defer_fn (fn);
26983 /* Restore the saved values. */
26984 parser->in_unbraced_linkage_specification_p
26985 = saved_in_unbraced_linkage_specification_p;
26986 parser->num_template_parameter_lists
26987 = saved_num_template_parameter_lists;
26988 parser->in_function_body = saved_in_function_body;
26990 parser->fully_implicit_function_template_p
26991 = fully_implicit_function_template_p;
26992 parser->implicit_template_parms
26993 = implicit_template_parms;
26994 parser->implicit_template_scope
26995 = implicit_template_scope;
26997 if (parser->fully_implicit_function_template_p)
26998 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27000 return fn;
27003 /* Parse a template-declaration body (following argument list). */
27005 static void
27006 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27007 tree parameter_list,
27008 bool member_p)
27010 tree decl = NULL_TREE;
27011 bool friend_p = false;
27013 /* We just processed one more parameter list. */
27014 ++parser->num_template_parameter_lists;
27016 /* Get the deferred access checks from the parameter list. These
27017 will be checked once we know what is being declared, as for a
27018 member template the checks must be performed in the scope of the
27019 class containing the member. */
27020 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27022 /* Tentatively parse for a new template parameter list, which can either be
27023 the template keyword or a template introduction. */
27024 if (cp_parser_template_declaration_after_export (parser, member_p))
27025 /* OK */;
27026 else if (cxx_dialect >= cxx11
27027 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27028 decl = cp_parser_alias_declaration (parser);
27029 else
27031 /* There are no access checks when parsing a template, as we do not
27032 know if a specialization will be a friend. */
27033 push_deferring_access_checks (dk_no_check);
27034 cp_token *token = cp_lexer_peek_token (parser->lexer);
27035 decl = cp_parser_single_declaration (parser,
27036 checks,
27037 member_p,
27038 /*explicit_specialization_p=*/false,
27039 &friend_p);
27040 pop_deferring_access_checks ();
27042 /* If this is a member template declaration, let the front
27043 end know. */
27044 if (member_p && !friend_p && decl)
27046 if (TREE_CODE (decl) == TYPE_DECL)
27047 cp_parser_check_access_in_redeclaration (decl, token->location);
27049 decl = finish_member_template_decl (decl);
27051 else if (friend_p && decl
27052 && DECL_DECLARES_TYPE_P (decl))
27053 make_friend_class (current_class_type, TREE_TYPE (decl),
27054 /*complain=*/true);
27056 /* We are done with the current parameter list. */
27057 --parser->num_template_parameter_lists;
27059 pop_deferring_access_checks ();
27061 /* Finish up. */
27062 finish_template_decl (parameter_list);
27064 /* Check the template arguments for a literal operator template. */
27065 if (decl
27066 && DECL_DECLARES_FUNCTION_P (decl)
27067 && UDLIT_OPER_P (DECL_NAME (decl)))
27069 bool ok = true;
27070 if (parameter_list == NULL_TREE)
27071 ok = false;
27072 else
27074 int num_parms = TREE_VEC_LENGTH (parameter_list);
27075 if (num_parms == 1)
27077 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27078 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27079 if (TREE_TYPE (parm) != char_type_node
27080 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27081 ok = false;
27083 else if (num_parms == 2 && cxx_dialect >= cxx14)
27085 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27086 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27087 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27088 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27089 if (parm == error_mark_node
27090 || TREE_TYPE (parm) != TREE_TYPE (type)
27091 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27092 ok = false;
27094 else
27095 ok = false;
27097 if (!ok)
27099 if (cxx_dialect >= cxx14)
27100 error ("literal operator template %qD has invalid parameter list."
27101 " Expected non-type template argument pack <char...>"
27102 " or <typename CharT, CharT...>",
27103 decl);
27104 else
27105 error ("literal operator template %qD has invalid parameter list."
27106 " Expected non-type template argument pack <char...>",
27107 decl);
27111 /* Register member declarations. */
27112 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27113 finish_member_declaration (decl);
27114 /* If DECL is a function template, we must return to parse it later.
27115 (Even though there is no definition, there might be default
27116 arguments that need handling.) */
27117 if (member_p && decl
27118 && DECL_DECLARES_FUNCTION_P (decl))
27119 vec_safe_push (unparsed_funs_with_definitions, decl);
27122 /* Parse a template introduction header for a template-declaration. Returns
27123 false if tentative parse fails. */
27125 static bool
27126 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27128 cp_parser_parse_tentatively (parser);
27130 tree saved_scope = parser->scope;
27131 tree saved_object_scope = parser->object_scope;
27132 tree saved_qualifying_scope = parser->qualifying_scope;
27134 /* Look for the optional `::' operator. */
27135 cp_parser_global_scope_opt (parser,
27136 /*current_scope_valid_p=*/false);
27137 /* Look for the nested-name-specifier. */
27138 cp_parser_nested_name_specifier_opt (parser,
27139 /*typename_keyword_p=*/false,
27140 /*check_dependency_p=*/true,
27141 /*type_p=*/false,
27142 /*is_declaration=*/false);
27144 cp_token *token = cp_lexer_peek_token (parser->lexer);
27145 tree concept_name = cp_parser_identifier (parser);
27147 /* Look up the concept for which we will be matching
27148 template parameters. */
27149 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27150 token->location);
27151 parser->scope = saved_scope;
27152 parser->object_scope = saved_object_scope;
27153 parser->qualifying_scope = saved_qualifying_scope;
27155 if (concept_name == error_mark_node)
27156 cp_parser_simulate_error (parser);
27158 /* Look for opening brace for introduction. */
27159 matching_braces braces;
27160 braces.require_open (parser);
27162 if (!cp_parser_parse_definitely (parser))
27163 return false;
27165 push_deferring_access_checks (dk_deferred);
27167 /* Build vector of placeholder parameters and grab
27168 matching identifiers. */
27169 tree introduction_list = cp_parser_introduction_list (parser);
27171 /* The introduction-list shall not be empty. */
27172 int nargs = TREE_VEC_LENGTH (introduction_list);
27173 if (nargs == 0)
27175 error ("empty introduction-list");
27176 return true;
27179 /* Look for closing brace for introduction. */
27180 if (!braces.require_close (parser))
27181 return true;
27183 if (tmpl_decl == error_mark_node)
27185 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27186 token->location);
27187 return true;
27190 /* Build and associate the constraint. */
27191 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27192 if (parms && parms != error_mark_node)
27194 cp_parser_template_declaration_after_parameters (parser, parms,
27195 member_p);
27196 return true;
27199 error_at (token->location, "no matching concept for template-introduction");
27200 return true;
27203 /* Parse a normal template-declaration following the template keyword. */
27205 static void
27206 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27208 tree parameter_list;
27209 bool need_lang_pop;
27210 location_t location = input_location;
27212 /* Look for the `<' token. */
27213 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27214 return;
27215 if (at_class_scope_p () && current_function_decl)
27217 /* 14.5.2.2 [temp.mem]
27219 A local class shall not have member templates. */
27220 error_at (location,
27221 "invalid declaration of member template in local class");
27222 cp_parser_skip_to_end_of_block_or_statement (parser);
27223 return;
27225 /* [temp]
27227 A template ... shall not have C linkage. */
27228 if (current_lang_name == lang_name_c)
27230 error_at (location, "template with C linkage");
27231 maybe_show_extern_c_location ();
27232 /* Give it C++ linkage to avoid confusing other parts of the
27233 front end. */
27234 push_lang_context (lang_name_cplusplus);
27235 need_lang_pop = true;
27237 else
27238 need_lang_pop = false;
27240 /* We cannot perform access checks on the template parameter
27241 declarations until we know what is being declared, just as we
27242 cannot check the decl-specifier list. */
27243 push_deferring_access_checks (dk_deferred);
27245 /* If the next token is `>', then we have an invalid
27246 specialization. Rather than complain about an invalid template
27247 parameter, issue an error message here. */
27248 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27250 cp_parser_error (parser, "invalid explicit specialization");
27251 begin_specialization ();
27252 parameter_list = NULL_TREE;
27254 else
27256 /* Parse the template parameters. */
27257 parameter_list = cp_parser_template_parameter_list (parser);
27260 /* Look for the `>'. */
27261 cp_parser_skip_to_end_of_template_parameter_list (parser);
27263 /* Manage template requirements */
27264 if (flag_concepts)
27266 tree reqs = get_shorthand_constraints (current_template_parms);
27267 if (tree r = cp_parser_requires_clause_opt (parser))
27268 reqs = conjoin_constraints (reqs, normalize_expression (r));
27269 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27272 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27273 member_p);
27275 /* For the erroneous case of a template with C linkage, we pushed an
27276 implicit C++ linkage scope; exit that scope now. */
27277 if (need_lang_pop)
27278 pop_lang_context ();
27281 /* Parse a template-declaration, assuming that the `export' (and
27282 `extern') keywords, if present, has already been scanned. MEMBER_P
27283 is as for cp_parser_template_declaration. */
27285 static bool
27286 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27290 cp_lexer_consume_token (parser->lexer);
27291 cp_parser_explicit_template_declaration (parser, member_p);
27292 return true;
27294 else if (flag_concepts)
27295 return cp_parser_template_introduction (parser, member_p);
27297 return false;
27300 /* Perform the deferred access checks from a template-parameter-list.
27301 CHECKS is a TREE_LIST of access checks, as returned by
27302 get_deferred_access_checks. */
27304 static void
27305 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27307 ++processing_template_parmlist;
27308 perform_access_checks (checks, tf_warning_or_error);
27309 --processing_template_parmlist;
27312 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27313 `function-definition' sequence that follows a template header.
27314 If MEMBER_P is true, this declaration appears in a class scope.
27316 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27317 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27319 static tree
27320 cp_parser_single_declaration (cp_parser* parser,
27321 vec<deferred_access_check, va_gc> *checks,
27322 bool member_p,
27323 bool explicit_specialization_p,
27324 bool* friend_p)
27326 int declares_class_or_enum;
27327 tree decl = NULL_TREE;
27328 cp_decl_specifier_seq decl_specifiers;
27329 bool function_definition_p = false;
27330 cp_token *decl_spec_token_start;
27332 /* This function is only used when processing a template
27333 declaration. */
27334 gcc_assert (innermost_scope_kind () == sk_template_parms
27335 || innermost_scope_kind () == sk_template_spec);
27337 /* Defer access checks until we know what is being declared. */
27338 push_deferring_access_checks (dk_deferred);
27340 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27341 alternative. */
27342 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27343 cp_parser_decl_specifier_seq (parser,
27344 CP_PARSER_FLAGS_OPTIONAL,
27345 &decl_specifiers,
27346 &declares_class_or_enum);
27347 if (friend_p)
27348 *friend_p = cp_parser_friend_p (&decl_specifiers);
27350 /* There are no template typedefs. */
27351 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27353 error_at (decl_spec_token_start->location,
27354 "template declaration of %<typedef%>");
27355 decl = error_mark_node;
27358 /* Gather up the access checks that occurred the
27359 decl-specifier-seq. */
27360 stop_deferring_access_checks ();
27362 /* Check for the declaration of a template class. */
27363 if (declares_class_or_enum)
27365 if (cp_parser_declares_only_class_p (parser)
27366 || (declares_class_or_enum & 2))
27368 // If this is a declaration, but not a definition, associate
27369 // any constraints with the type declaration. Constraints
27370 // are associated with definitions in cp_parser_class_specifier.
27371 if (declares_class_or_enum == 1)
27372 associate_classtype_constraints (decl_specifiers.type);
27374 decl = shadow_tag (&decl_specifiers);
27376 /* In this case:
27378 struct C {
27379 friend template <typename T> struct A<T>::B;
27382 A<T>::B will be represented by a TYPENAME_TYPE, and
27383 therefore not recognized by shadow_tag. */
27384 if (friend_p && *friend_p
27385 && !decl
27386 && decl_specifiers.type
27387 && TYPE_P (decl_specifiers.type))
27388 decl = decl_specifiers.type;
27390 if (decl && decl != error_mark_node)
27391 decl = TYPE_NAME (decl);
27392 else
27393 decl = error_mark_node;
27395 /* Perform access checks for template parameters. */
27396 cp_parser_perform_template_parameter_access_checks (checks);
27398 /* Give a helpful diagnostic for
27399 template <class T> struct A { } a;
27400 if we aren't already recovering from an error. */
27401 if (!cp_parser_declares_only_class_p (parser)
27402 && !seen_error ())
27404 error_at (cp_lexer_peek_token (parser->lexer)->location,
27405 "a class template declaration must not declare "
27406 "anything else");
27407 cp_parser_skip_to_end_of_block_or_statement (parser);
27408 goto out;
27413 /* Complain about missing 'typename' or other invalid type names. */
27414 if (!decl_specifiers.any_type_specifiers_p
27415 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27417 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27418 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27419 the rest of this declaration. */
27420 decl = error_mark_node;
27421 goto out;
27424 /* If it's not a template class, try for a template function. If
27425 the next token is a `;', then this declaration does not declare
27426 anything. But, if there were errors in the decl-specifiers, then
27427 the error might well have come from an attempted class-specifier.
27428 In that case, there's no need to warn about a missing declarator. */
27429 if (!decl
27430 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27431 || decl_specifiers.type != error_mark_node))
27433 decl = cp_parser_init_declarator (parser,
27434 &decl_specifiers,
27435 checks,
27436 /*function_definition_allowed_p=*/true,
27437 member_p,
27438 declares_class_or_enum,
27439 &function_definition_p,
27440 NULL, NULL, NULL);
27442 /* 7.1.1-1 [dcl.stc]
27444 A storage-class-specifier shall not be specified in an explicit
27445 specialization... */
27446 if (decl
27447 && explicit_specialization_p
27448 && decl_specifiers.storage_class != sc_none)
27450 error_at (decl_spec_token_start->location,
27451 "explicit template specialization cannot have a storage class");
27452 decl = error_mark_node;
27455 if (decl && VAR_P (decl))
27456 check_template_variable (decl);
27459 /* Look for a trailing `;' after the declaration. */
27460 if (!function_definition_p
27461 && (decl == error_mark_node
27462 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27463 cp_parser_skip_to_end_of_block_or_statement (parser);
27465 out:
27466 pop_deferring_access_checks ();
27468 /* Clear any current qualification; whatever comes next is the start
27469 of something new. */
27470 parser->scope = NULL_TREE;
27471 parser->qualifying_scope = NULL_TREE;
27472 parser->object_scope = NULL_TREE;
27474 return decl;
27477 /* Parse a cast-expression that is not the operand of a unary "&". */
27479 static cp_expr
27480 cp_parser_simple_cast_expression (cp_parser *parser)
27482 return cp_parser_cast_expression (parser, /*address_p=*/false,
27483 /*cast_p=*/false, /*decltype*/false, NULL);
27486 /* Parse a functional cast to TYPE. Returns an expression
27487 representing the cast. */
27489 static cp_expr
27490 cp_parser_functional_cast (cp_parser* parser, tree type)
27492 vec<tree, va_gc> *vec;
27493 tree expression_list;
27494 cp_expr cast;
27495 bool nonconst_p;
27497 location_t start_loc = input_location;
27499 if (!type)
27500 type = error_mark_node;
27502 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27504 cp_lexer_set_source_position (parser->lexer);
27505 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27506 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27507 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27508 if (TREE_CODE (type) == TYPE_DECL)
27509 type = TREE_TYPE (type);
27511 cast = finish_compound_literal (type, expression_list,
27512 tf_warning_or_error, fcl_functional);
27513 /* Create a location of the form:
27514 type_name{i, f}
27515 ^~~~~~~~~~~~~~~
27516 with caret == start at the start of the type name,
27517 finishing at the closing brace. */
27518 location_t finish_loc
27519 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27520 location_t combined_loc = make_location (start_loc, start_loc,
27521 finish_loc);
27522 cast.set_location (combined_loc);
27523 return cast;
27527 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27528 /*cast_p=*/true,
27529 /*allow_expansion_p=*/true,
27530 /*non_constant_p=*/NULL);
27531 if (vec == NULL)
27532 expression_list = error_mark_node;
27533 else
27535 expression_list = build_tree_list_vec (vec);
27536 release_tree_vector (vec);
27539 cast = build_functional_cast (type, expression_list,
27540 tf_warning_or_error);
27541 /* [expr.const]/1: In an integral constant expression "only type
27542 conversions to integral or enumeration type can be used". */
27543 if (TREE_CODE (type) == TYPE_DECL)
27544 type = TREE_TYPE (type);
27545 if (cast != error_mark_node
27546 && !cast_valid_in_integral_constant_expression_p (type)
27547 && cp_parser_non_integral_constant_expression (parser,
27548 NIC_CONSTRUCTOR))
27549 return error_mark_node;
27551 /* Create a location of the form:
27552 float(i)
27553 ^~~~~~~~
27554 with caret == start at the start of the type name,
27555 finishing at the closing paren. */
27556 location_t finish_loc
27557 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27558 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27559 cast.set_location (combined_loc);
27560 return cast;
27563 /* Save the tokens that make up the body of a member function defined
27564 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27565 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27566 specifiers applied to the declaration. Returns the FUNCTION_DECL
27567 for the member function. */
27569 static tree
27570 cp_parser_save_member_function_body (cp_parser* parser,
27571 cp_decl_specifier_seq *decl_specifiers,
27572 cp_declarator *declarator,
27573 tree attributes)
27575 cp_token *first;
27576 cp_token *last;
27577 tree fn;
27578 bool function_try_block = false;
27580 /* Create the FUNCTION_DECL. */
27581 fn = grokmethod (decl_specifiers, declarator, attributes);
27582 cp_finalize_omp_declare_simd (parser, fn);
27583 cp_finalize_oacc_routine (parser, fn, true);
27584 /* If something went badly wrong, bail out now. */
27585 if (fn == error_mark_node)
27587 /* If there's a function-body, skip it. */
27588 if (cp_parser_token_starts_function_definition_p
27589 (cp_lexer_peek_token (parser->lexer)))
27590 cp_parser_skip_to_end_of_block_or_statement (parser);
27591 return error_mark_node;
27594 /* Remember it, if there default args to post process. */
27595 cp_parser_save_default_args (parser, fn);
27597 /* Save away the tokens that make up the body of the
27598 function. */
27599 first = parser->lexer->next_token;
27601 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27602 cp_lexer_consume_token (parser->lexer);
27603 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27604 RID_TRANSACTION_ATOMIC))
27606 cp_lexer_consume_token (parser->lexer);
27607 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27608 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27609 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27610 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27611 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27612 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27613 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27615 cp_lexer_consume_token (parser->lexer);
27616 cp_lexer_consume_token (parser->lexer);
27617 cp_lexer_consume_token (parser->lexer);
27618 cp_lexer_consume_token (parser->lexer);
27619 cp_lexer_consume_token (parser->lexer);
27621 else
27622 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27623 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27625 cp_lexer_consume_token (parser->lexer);
27626 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27627 break;
27631 /* Handle function try blocks. */
27632 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27634 cp_lexer_consume_token (parser->lexer);
27635 function_try_block = true;
27637 /* We can have braced-init-list mem-initializers before the fn body. */
27638 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27640 cp_lexer_consume_token (parser->lexer);
27641 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27643 /* cache_group will stop after an un-nested { } pair, too. */
27644 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27645 break;
27647 /* variadic mem-inits have ... after the ')'. */
27648 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27649 cp_lexer_consume_token (parser->lexer);
27652 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27653 /* Handle function try blocks. */
27654 if (function_try_block)
27655 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27656 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27657 last = parser->lexer->next_token;
27659 /* Save away the inline definition; we will process it when the
27660 class is complete. */
27661 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27662 DECL_PENDING_INLINE_P (fn) = 1;
27664 /* We need to know that this was defined in the class, so that
27665 friend templates are handled correctly. */
27666 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27668 /* Add FN to the queue of functions to be parsed later. */
27669 vec_safe_push (unparsed_funs_with_definitions, fn);
27671 return fn;
27674 /* Save the tokens that make up the in-class initializer for a non-static
27675 data member. Returns a DEFAULT_ARG. */
27677 static tree
27678 cp_parser_save_nsdmi (cp_parser* parser)
27680 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27683 /* Parse a template-argument-list, as well as the trailing ">" (but
27684 not the opening "<"). See cp_parser_template_argument_list for the
27685 return value. */
27687 static tree
27688 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27690 tree arguments;
27691 tree saved_scope;
27692 tree saved_qualifying_scope;
27693 tree saved_object_scope;
27694 bool saved_greater_than_is_operator_p;
27695 int saved_unevaluated_operand;
27696 int saved_inhibit_evaluation_warnings;
27698 /* [temp.names]
27700 When parsing a template-id, the first non-nested `>' is taken as
27701 the end of the template-argument-list rather than a greater-than
27702 operator. */
27703 saved_greater_than_is_operator_p
27704 = parser->greater_than_is_operator_p;
27705 parser->greater_than_is_operator_p = false;
27706 /* Parsing the argument list may modify SCOPE, so we save it
27707 here. */
27708 saved_scope = parser->scope;
27709 saved_qualifying_scope = parser->qualifying_scope;
27710 saved_object_scope = parser->object_scope;
27711 /* We need to evaluate the template arguments, even though this
27712 template-id may be nested within a "sizeof". */
27713 saved_unevaluated_operand = cp_unevaluated_operand;
27714 cp_unevaluated_operand = 0;
27715 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27716 c_inhibit_evaluation_warnings = 0;
27717 /* Parse the template-argument-list itself. */
27718 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27719 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27720 arguments = NULL_TREE;
27721 else
27722 arguments = cp_parser_template_argument_list (parser);
27723 /* Look for the `>' that ends the template-argument-list. If we find
27724 a '>>' instead, it's probably just a typo. */
27725 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27727 if (cxx_dialect != cxx98)
27729 /* In C++0x, a `>>' in a template argument list or cast
27730 expression is considered to be two separate `>'
27731 tokens. So, change the current token to a `>', but don't
27732 consume it: it will be consumed later when the outer
27733 template argument list (or cast expression) is parsed.
27734 Note that this replacement of `>' for `>>' is necessary
27735 even if we are parsing tentatively: in the tentative
27736 case, after calling
27737 cp_parser_enclosed_template_argument_list we will always
27738 throw away all of the template arguments and the first
27739 closing `>', either because the template argument list
27740 was erroneous or because we are replacing those tokens
27741 with a CPP_TEMPLATE_ID token. The second `>' (which will
27742 not have been thrown away) is needed either to close an
27743 outer template argument list or to complete a new-style
27744 cast. */
27745 cp_token *token = cp_lexer_peek_token (parser->lexer);
27746 token->type = CPP_GREATER;
27748 else if (!saved_greater_than_is_operator_p)
27750 /* If we're in a nested template argument list, the '>>' has
27751 to be a typo for '> >'. We emit the error message, but we
27752 continue parsing and we push a '>' as next token, so that
27753 the argument list will be parsed correctly. Note that the
27754 global source location is still on the token before the
27755 '>>', so we need to say explicitly where we want it. */
27756 cp_token *token = cp_lexer_peek_token (parser->lexer);
27757 gcc_rich_location richloc (token->location);
27758 richloc.add_fixit_replace ("> >");
27759 error_at (&richloc, "%<>>%> should be %<> >%> "
27760 "within a nested template argument list");
27762 token->type = CPP_GREATER;
27764 else
27766 /* If this is not a nested template argument list, the '>>'
27767 is a typo for '>'. Emit an error message and continue.
27768 Same deal about the token location, but here we can get it
27769 right by consuming the '>>' before issuing the diagnostic. */
27770 cp_token *token = cp_lexer_consume_token (parser->lexer);
27771 error_at (token->location,
27772 "spurious %<>>%>, use %<>%> to terminate "
27773 "a template argument list");
27776 else
27777 cp_parser_skip_to_end_of_template_parameter_list (parser);
27778 /* The `>' token might be a greater-than operator again now. */
27779 parser->greater_than_is_operator_p
27780 = saved_greater_than_is_operator_p;
27781 /* Restore the SAVED_SCOPE. */
27782 parser->scope = saved_scope;
27783 parser->qualifying_scope = saved_qualifying_scope;
27784 parser->object_scope = saved_object_scope;
27785 cp_unevaluated_operand = saved_unevaluated_operand;
27786 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27788 return arguments;
27791 /* MEMBER_FUNCTION is a member function, or a friend. If default
27792 arguments, or the body of the function have not yet been parsed,
27793 parse them now. */
27795 static void
27796 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27798 timevar_push (TV_PARSE_INMETH);
27799 /* If this member is a template, get the underlying
27800 FUNCTION_DECL. */
27801 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27802 member_function = DECL_TEMPLATE_RESULT (member_function);
27804 /* There should not be any class definitions in progress at this
27805 point; the bodies of members are only parsed outside of all class
27806 definitions. */
27807 gcc_assert (parser->num_classes_being_defined == 0);
27808 /* While we're parsing the member functions we might encounter more
27809 classes. We want to handle them right away, but we don't want
27810 them getting mixed up with functions that are currently in the
27811 queue. */
27812 push_unparsed_function_queues (parser);
27814 /* Make sure that any template parameters are in scope. */
27815 maybe_begin_member_template_processing (member_function);
27817 /* If the body of the function has not yet been parsed, parse it
27818 now. */
27819 if (DECL_PENDING_INLINE_P (member_function))
27821 tree function_scope;
27822 cp_token_cache *tokens;
27824 /* The function is no longer pending; we are processing it. */
27825 tokens = DECL_PENDING_INLINE_INFO (member_function);
27826 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27827 DECL_PENDING_INLINE_P (member_function) = 0;
27829 /* If this is a local class, enter the scope of the containing
27830 function. */
27831 function_scope = current_function_decl;
27832 if (function_scope)
27833 push_function_context ();
27835 /* Push the body of the function onto the lexer stack. */
27836 cp_parser_push_lexer_for_tokens (parser, tokens);
27838 /* Let the front end know that we going to be defining this
27839 function. */
27840 start_preparsed_function (member_function, NULL_TREE,
27841 SF_PRE_PARSED | SF_INCLASS_INLINE);
27843 /* Don't do access checking if it is a templated function. */
27844 if (processing_template_decl)
27845 push_deferring_access_checks (dk_no_check);
27847 /* #pragma omp declare reduction needs special parsing. */
27848 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27850 parser->lexer->in_pragma = true;
27851 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27852 finish_function (/*inline_p=*/true);
27853 cp_check_omp_declare_reduction (member_function);
27855 else
27856 /* Now, parse the body of the function. */
27857 cp_parser_function_definition_after_declarator (parser,
27858 /*inline_p=*/true);
27860 if (processing_template_decl)
27861 pop_deferring_access_checks ();
27863 /* Leave the scope of the containing function. */
27864 if (function_scope)
27865 pop_function_context ();
27866 cp_parser_pop_lexer (parser);
27869 /* Remove any template parameters from the symbol table. */
27870 maybe_end_member_template_processing ();
27872 /* Restore the queue. */
27873 pop_unparsed_function_queues (parser);
27874 timevar_pop (TV_PARSE_INMETH);
27877 /* If DECL contains any default args, remember it on the unparsed
27878 functions queue. */
27880 static void
27881 cp_parser_save_default_args (cp_parser* parser, tree decl)
27883 tree probe;
27885 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27886 probe;
27887 probe = TREE_CHAIN (probe))
27888 if (TREE_PURPOSE (probe))
27890 cp_default_arg_entry entry = {current_class_type, decl};
27891 vec_safe_push (unparsed_funs_with_default_args, entry);
27892 break;
27896 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27897 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27898 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27899 from the parameter-type-list. */
27901 static tree
27902 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27903 tree default_arg, tree parmtype)
27905 cp_token_cache *tokens;
27906 tree parsed_arg;
27907 bool dummy;
27909 if (default_arg == error_mark_node)
27910 return error_mark_node;
27912 /* Push the saved tokens for the default argument onto the parser's
27913 lexer stack. */
27914 tokens = DEFARG_TOKENS (default_arg);
27915 cp_parser_push_lexer_for_tokens (parser, tokens);
27917 start_lambda_scope (decl);
27919 /* Parse the default argument. */
27920 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27921 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27922 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27924 finish_lambda_scope ();
27926 if (parsed_arg == error_mark_node)
27927 cp_parser_skip_to_end_of_statement (parser);
27929 if (!processing_template_decl)
27931 /* In a non-template class, check conversions now. In a template,
27932 we'll wait and instantiate these as needed. */
27933 if (TREE_CODE (decl) == PARM_DECL)
27934 parsed_arg = check_default_argument (parmtype, parsed_arg,
27935 tf_warning_or_error);
27936 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27937 parsed_arg = error_mark_node;
27938 else
27939 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27942 /* If the token stream has not been completely used up, then
27943 there was extra junk after the end of the default
27944 argument. */
27945 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27947 if (TREE_CODE (decl) == PARM_DECL)
27948 cp_parser_error (parser, "expected %<,%>");
27949 else
27950 cp_parser_error (parser, "expected %<;%>");
27953 /* Revert to the main lexer. */
27954 cp_parser_pop_lexer (parser);
27956 return parsed_arg;
27959 /* FIELD is a non-static data member with an initializer which we saved for
27960 later; parse it now. */
27962 static void
27963 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27965 tree def;
27967 maybe_begin_member_template_processing (field);
27969 push_unparsed_function_queues (parser);
27970 def = cp_parser_late_parse_one_default_arg (parser, field,
27971 DECL_INITIAL (field),
27972 NULL_TREE);
27973 pop_unparsed_function_queues (parser);
27975 maybe_end_member_template_processing ();
27977 DECL_INITIAL (field) = def;
27980 /* FN is a FUNCTION_DECL which may contains a parameter with an
27981 unparsed DEFAULT_ARG. Parse the default args now. This function
27982 assumes that the current scope is the scope in which the default
27983 argument should be processed. */
27985 static void
27986 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27988 bool saved_local_variables_forbidden_p;
27989 tree parm, parmdecl;
27991 /* While we're parsing the default args, we might (due to the
27992 statement expression extension) encounter more classes. We want
27993 to handle them right away, but we don't want them getting mixed
27994 up with default args that are currently in the queue. */
27995 push_unparsed_function_queues (parser);
27997 /* Local variable names (and the `this' keyword) may not appear
27998 in a default argument. */
27999 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28000 parser->local_variables_forbidden_p = true;
28002 push_defarg_context (fn);
28004 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28005 parmdecl = DECL_ARGUMENTS (fn);
28006 parm && parm != void_list_node;
28007 parm = TREE_CHAIN (parm),
28008 parmdecl = DECL_CHAIN (parmdecl))
28010 tree default_arg = TREE_PURPOSE (parm);
28011 tree parsed_arg;
28012 vec<tree, va_gc> *insts;
28013 tree copy;
28014 unsigned ix;
28016 if (!default_arg)
28017 continue;
28019 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28020 /* This can happen for a friend declaration for a function
28021 already declared with default arguments. */
28022 continue;
28024 parsed_arg
28025 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28026 default_arg,
28027 TREE_VALUE (parm));
28028 TREE_PURPOSE (parm) = parsed_arg;
28030 /* Update any instantiations we've already created. */
28031 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28032 vec_safe_iterate (insts, ix, &copy); ix++)
28033 TREE_PURPOSE (copy) = parsed_arg;
28036 pop_defarg_context ();
28038 /* Make sure no default arg is missing. */
28039 check_default_args (fn);
28041 /* Restore the state of local_variables_forbidden_p. */
28042 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28044 /* Restore the queue. */
28045 pop_unparsed_function_queues (parser);
28048 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28050 sizeof ... ( identifier )
28052 where the 'sizeof' token has already been consumed. */
28054 static tree
28055 cp_parser_sizeof_pack (cp_parser *parser)
28057 /* Consume the `...'. */
28058 cp_lexer_consume_token (parser->lexer);
28059 maybe_warn_variadic_templates ();
28061 matching_parens parens;
28062 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28063 if (paren)
28064 parens.consume_open (parser);
28065 else
28066 permerror (cp_lexer_peek_token (parser->lexer)->location,
28067 "%<sizeof...%> argument must be surrounded by parentheses");
28069 cp_token *token = cp_lexer_peek_token (parser->lexer);
28070 tree name = cp_parser_identifier (parser);
28071 if (name == error_mark_node)
28072 return error_mark_node;
28073 /* The name is not qualified. */
28074 parser->scope = NULL_TREE;
28075 parser->qualifying_scope = NULL_TREE;
28076 parser->object_scope = NULL_TREE;
28077 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28078 if (expr == error_mark_node)
28079 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28080 token->location);
28081 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28082 expr = TREE_TYPE (expr);
28083 else if (TREE_CODE (expr) == CONST_DECL)
28084 expr = DECL_INITIAL (expr);
28085 expr = make_pack_expansion (expr);
28086 PACK_EXPANSION_SIZEOF_P (expr) = true;
28088 if (paren)
28089 parens.require_close (parser);
28091 return expr;
28094 /* Parse the operand of `sizeof' (or a similar operator). Returns
28095 either a TYPE or an expression, depending on the form of the
28096 input. The KEYWORD indicates which kind of expression we have
28097 encountered. */
28099 static tree
28100 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28102 tree expr = NULL_TREE;
28103 const char *saved_message;
28104 char *tmp;
28105 bool saved_integral_constant_expression_p;
28106 bool saved_non_integral_constant_expression_p;
28108 /* If it's a `...', then we are computing the length of a parameter
28109 pack. */
28110 if (keyword == RID_SIZEOF
28111 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28112 return cp_parser_sizeof_pack (parser);
28114 /* Types cannot be defined in a `sizeof' expression. Save away the
28115 old message. */
28116 saved_message = parser->type_definition_forbidden_message;
28117 /* And create the new one. */
28118 tmp = concat ("types may not be defined in %<",
28119 IDENTIFIER_POINTER (ridpointers[keyword]),
28120 "%> expressions", NULL);
28121 parser->type_definition_forbidden_message = tmp;
28123 /* The restrictions on constant-expressions do not apply inside
28124 sizeof expressions. */
28125 saved_integral_constant_expression_p
28126 = parser->integral_constant_expression_p;
28127 saved_non_integral_constant_expression_p
28128 = parser->non_integral_constant_expression_p;
28129 parser->integral_constant_expression_p = false;
28131 /* Do not actually evaluate the expression. */
28132 ++cp_unevaluated_operand;
28133 ++c_inhibit_evaluation_warnings;
28134 /* If it's a `(', then we might be looking at the type-id
28135 construction. */
28136 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28138 tree type = NULL_TREE;
28140 /* We can't be sure yet whether we're looking at a type-id or an
28141 expression. */
28142 cp_parser_parse_tentatively (parser);
28144 matching_parens parens;
28145 parens.consume_open (parser);
28147 /* Note: as a GNU Extension, compound literals are considered
28148 postfix-expressions as they are in C99, so they are valid
28149 arguments to sizeof. See comment in cp_parser_cast_expression
28150 for details. */
28151 if (cp_parser_compound_literal_p (parser))
28152 cp_parser_simulate_error (parser);
28153 else
28155 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28156 parser->in_type_id_in_expr_p = true;
28157 /* Look for the type-id. */
28158 type = cp_parser_type_id (parser);
28159 /* Look for the closing `)'. */
28160 parens.require_close (parser);
28161 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28164 /* If all went well, then we're done. */
28165 if (cp_parser_parse_definitely (parser))
28167 cp_decl_specifier_seq decl_specs;
28169 /* Build a trivial decl-specifier-seq. */
28170 clear_decl_specs (&decl_specs);
28171 decl_specs.type = type;
28173 /* Call grokdeclarator to figure out what type this is. */
28174 expr = grokdeclarator (NULL,
28175 &decl_specs,
28176 TYPENAME,
28177 /*initialized=*/0,
28178 /*attrlist=*/NULL);
28182 /* If the type-id production did not work out, then we must be
28183 looking at the unary-expression production. */
28184 if (!expr)
28185 expr = cp_parser_unary_expression (parser);
28187 /* Go back to evaluating expressions. */
28188 --cp_unevaluated_operand;
28189 --c_inhibit_evaluation_warnings;
28191 /* Free the message we created. */
28192 free (tmp);
28193 /* And restore the old one. */
28194 parser->type_definition_forbidden_message = saved_message;
28195 parser->integral_constant_expression_p
28196 = saved_integral_constant_expression_p;
28197 parser->non_integral_constant_expression_p
28198 = saved_non_integral_constant_expression_p;
28200 return expr;
28203 /* If the current declaration has no declarator, return true. */
28205 static bool
28206 cp_parser_declares_only_class_p (cp_parser *parser)
28208 /* If the next token is a `;' or a `,' then there is no
28209 declarator. */
28210 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28211 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28214 /* Update the DECL_SPECS to reflect the storage class indicated by
28215 KEYWORD. */
28217 static void
28218 cp_parser_set_storage_class (cp_parser *parser,
28219 cp_decl_specifier_seq *decl_specs,
28220 enum rid keyword,
28221 cp_token *token)
28223 cp_storage_class storage_class;
28225 if (parser->in_unbraced_linkage_specification_p)
28227 error_at (token->location, "invalid use of %qD in linkage specification",
28228 ridpointers[keyword]);
28229 return;
28231 else if (decl_specs->storage_class != sc_none)
28233 decl_specs->conflicting_specifiers_p = true;
28234 return;
28237 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28238 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28239 && decl_specs->gnu_thread_keyword_p)
28241 pedwarn (decl_specs->locations[ds_thread], 0,
28242 "%<__thread%> before %qD", ridpointers[keyword]);
28245 switch (keyword)
28247 case RID_AUTO:
28248 storage_class = sc_auto;
28249 break;
28250 case RID_REGISTER:
28251 storage_class = sc_register;
28252 break;
28253 case RID_STATIC:
28254 storage_class = sc_static;
28255 break;
28256 case RID_EXTERN:
28257 storage_class = sc_extern;
28258 break;
28259 case RID_MUTABLE:
28260 storage_class = sc_mutable;
28261 break;
28262 default:
28263 gcc_unreachable ();
28265 decl_specs->storage_class = storage_class;
28266 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28268 /* A storage class specifier cannot be applied alongside a typedef
28269 specifier. If there is a typedef specifier present then set
28270 conflicting_specifiers_p which will trigger an error later
28271 on in grokdeclarator. */
28272 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28273 decl_specs->conflicting_specifiers_p = true;
28276 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28277 is true, the type is a class or enum definition. */
28279 static void
28280 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28281 tree type_spec,
28282 cp_token *token,
28283 bool type_definition_p)
28285 decl_specs->any_specifiers_p = true;
28287 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28288 (with, for example, in "typedef int wchar_t;") we remember that
28289 this is what happened. In system headers, we ignore these
28290 declarations so that G++ can work with system headers that are not
28291 C++-safe. */
28292 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28293 && !type_definition_p
28294 && (type_spec == boolean_type_node
28295 || type_spec == char16_type_node
28296 || type_spec == char32_type_node
28297 || type_spec == wchar_type_node)
28298 && (decl_specs->type
28299 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28300 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28301 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28302 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28304 decl_specs->redefined_builtin_type = type_spec;
28305 set_and_check_decl_spec_loc (decl_specs,
28306 ds_redefined_builtin_type_spec,
28307 token);
28308 if (!decl_specs->type)
28310 decl_specs->type = type_spec;
28311 decl_specs->type_definition_p = false;
28312 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28315 else if (decl_specs->type)
28316 decl_specs->multiple_types_p = true;
28317 else
28319 decl_specs->type = type_spec;
28320 decl_specs->type_definition_p = type_definition_p;
28321 decl_specs->redefined_builtin_type = NULL_TREE;
28322 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28326 /* True iff TOKEN is the GNU keyword __thread. */
28328 static bool
28329 token_is__thread (cp_token *token)
28331 gcc_assert (token->keyword == RID_THREAD);
28332 return id_equal (token->u.value, "__thread");
28335 /* Set the location for a declarator specifier and check if it is
28336 duplicated.
28338 DECL_SPECS is the sequence of declarator specifiers onto which to
28339 set the location.
28341 DS is the single declarator specifier to set which location is to
28342 be set onto the existing sequence of declarators.
28344 LOCATION is the location for the declarator specifier to
28345 consider. */
28347 static void
28348 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28349 cp_decl_spec ds, cp_token *token)
28351 gcc_assert (ds < ds_last);
28353 if (decl_specs == NULL)
28354 return;
28356 source_location location = token->location;
28358 if (decl_specs->locations[ds] == 0)
28360 decl_specs->locations[ds] = location;
28361 if (ds == ds_thread)
28362 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28364 else
28366 if (ds == ds_long)
28368 if (decl_specs->locations[ds_long_long] != 0)
28369 error_at (location,
28370 "%<long long long%> is too long for GCC");
28371 else
28373 decl_specs->locations[ds_long_long] = location;
28374 pedwarn_cxx98 (location,
28375 OPT_Wlong_long,
28376 "ISO C++ 1998 does not support %<long long%>");
28379 else if (ds == ds_thread)
28381 bool gnu = token_is__thread (token);
28382 gcc_rich_location richloc (location);
28383 if (gnu != decl_specs->gnu_thread_keyword_p)
28385 richloc.add_range (decl_specs->locations[ds_thread], false);
28386 error_at (&richloc,
28387 "both %<__thread%> and %<thread_local%> specified");
28389 else
28391 richloc.add_fixit_remove ();
28392 error_at (&richloc, "duplicate %qD", token->u.value);
28395 else
28397 static const char *const decl_spec_names[] = {
28398 "signed",
28399 "unsigned",
28400 "short",
28401 "long",
28402 "const",
28403 "volatile",
28404 "restrict",
28405 "inline",
28406 "virtual",
28407 "explicit",
28408 "friend",
28409 "typedef",
28410 "using",
28411 "constexpr",
28412 "__complex"
28414 gcc_rich_location richloc (location);
28415 richloc.add_fixit_remove ();
28416 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28421 /* Return true iff the declarator specifier DS is present in the
28422 sequence of declarator specifiers DECL_SPECS. */
28424 bool
28425 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28426 cp_decl_spec ds)
28428 gcc_assert (ds < ds_last);
28430 if (decl_specs == NULL)
28431 return false;
28433 return decl_specs->locations[ds] != 0;
28436 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28437 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28439 static bool
28440 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28442 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28445 /* Issue an error message indicating that TOKEN_DESC was expected.
28446 If KEYWORD is true, it indicated this function is called by
28447 cp_parser_require_keword and the required token can only be
28448 a indicated keyword.
28450 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28451 within any error as the location of an "opening" token matching
28452 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28453 RT_CLOSE_PAREN). */
28455 static void
28456 cp_parser_required_error (cp_parser *parser,
28457 required_token token_desc,
28458 bool keyword,
28459 location_t matching_location)
28461 if (cp_parser_simulate_error (parser))
28462 return;
28464 const char *gmsgid = NULL;
28465 switch (token_desc)
28467 case RT_NEW:
28468 gmsgid = G_("expected %<new%>");
28469 break;
28470 case RT_DELETE:
28471 gmsgid = G_("expected %<delete%>");
28472 break;
28473 case RT_RETURN:
28474 gmsgid = G_("expected %<return%>");
28475 break;
28476 case RT_WHILE:
28477 gmsgid = G_("expected %<while%>");
28478 break;
28479 case RT_EXTERN:
28480 gmsgid = G_("expected %<extern%>");
28481 break;
28482 case RT_STATIC_ASSERT:
28483 gmsgid = G_("expected %<static_assert%>");
28484 break;
28485 case RT_DECLTYPE:
28486 gmsgid = G_("expected %<decltype%>");
28487 break;
28488 case RT_OPERATOR:
28489 gmsgid = G_("expected %<operator%>");
28490 break;
28491 case RT_CLASS:
28492 gmsgid = G_("expected %<class%>");
28493 break;
28494 case RT_TEMPLATE:
28495 gmsgid = G_("expected %<template%>");
28496 break;
28497 case RT_NAMESPACE:
28498 gmsgid = G_("expected %<namespace%>");
28499 break;
28500 case RT_USING:
28501 gmsgid = G_("expected %<using%>");
28502 break;
28503 case RT_ASM:
28504 gmsgid = G_("expected %<asm%>");
28505 break;
28506 case RT_TRY:
28507 gmsgid = G_("expected %<try%>");
28508 break;
28509 case RT_CATCH:
28510 gmsgid = G_("expected %<catch%>");
28511 break;
28512 case RT_THROW:
28513 gmsgid = G_("expected %<throw%>");
28514 break;
28515 case RT_LABEL:
28516 gmsgid = G_("expected %<__label__%>");
28517 break;
28518 case RT_AT_TRY:
28519 gmsgid = G_("expected %<@try%>");
28520 break;
28521 case RT_AT_SYNCHRONIZED:
28522 gmsgid = G_("expected %<@synchronized%>");
28523 break;
28524 case RT_AT_THROW:
28525 gmsgid = G_("expected %<@throw%>");
28526 break;
28527 case RT_TRANSACTION_ATOMIC:
28528 gmsgid = G_("expected %<__transaction_atomic%>");
28529 break;
28530 case RT_TRANSACTION_RELAXED:
28531 gmsgid = G_("expected %<__transaction_relaxed%>");
28532 break;
28533 default:
28534 break;
28537 if (!gmsgid && !keyword)
28539 switch (token_desc)
28541 case RT_SEMICOLON:
28542 gmsgid = G_("expected %<;%>");
28543 break;
28544 case RT_OPEN_PAREN:
28545 gmsgid = G_("expected %<(%>");
28546 break;
28547 case RT_CLOSE_BRACE:
28548 gmsgid = G_("expected %<}%>");
28549 break;
28550 case RT_OPEN_BRACE:
28551 gmsgid = G_("expected %<{%>");
28552 break;
28553 case RT_CLOSE_SQUARE:
28554 gmsgid = G_("expected %<]%>");
28555 break;
28556 case RT_OPEN_SQUARE:
28557 gmsgid = G_("expected %<[%>");
28558 break;
28559 case RT_COMMA:
28560 gmsgid = G_("expected %<,%>");
28561 break;
28562 case RT_SCOPE:
28563 gmsgid = G_("expected %<::%>");
28564 break;
28565 case RT_LESS:
28566 gmsgid = G_("expected %<<%>");
28567 break;
28568 case RT_GREATER:
28569 gmsgid = G_("expected %<>%>");
28570 break;
28571 case RT_EQ:
28572 gmsgid = G_("expected %<=%>");
28573 break;
28574 case RT_ELLIPSIS:
28575 gmsgid = G_("expected %<...%>");
28576 break;
28577 case RT_MULT:
28578 gmsgid = G_("expected %<*%>");
28579 break;
28580 case RT_COMPL:
28581 gmsgid = G_("expected %<~%>");
28582 break;
28583 case RT_COLON:
28584 gmsgid = G_("expected %<:%>");
28585 break;
28586 case RT_COLON_SCOPE:
28587 gmsgid = G_("expected %<:%> or %<::%>");
28588 break;
28589 case RT_CLOSE_PAREN:
28590 gmsgid = G_("expected %<)%>");
28591 break;
28592 case RT_COMMA_CLOSE_PAREN:
28593 gmsgid = G_("expected %<,%> or %<)%>");
28594 break;
28595 case RT_PRAGMA_EOL:
28596 gmsgid = G_("expected end of line");
28597 break;
28598 case RT_NAME:
28599 gmsgid = G_("expected identifier");
28600 break;
28601 case RT_SELECT:
28602 gmsgid = G_("expected selection-statement");
28603 break;
28604 case RT_ITERATION:
28605 gmsgid = G_("expected iteration-statement");
28606 break;
28607 case RT_JUMP:
28608 gmsgid = G_("expected jump-statement");
28609 break;
28610 case RT_CLASS_KEY:
28611 gmsgid = G_("expected class-key");
28612 break;
28613 case RT_CLASS_TYPENAME_TEMPLATE:
28614 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28615 break;
28616 default:
28617 gcc_unreachable ();
28621 if (gmsgid)
28622 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28626 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28627 issue an error message indicating that TOKEN_DESC was expected.
28629 Returns the token consumed, if the token had the appropriate type.
28630 Otherwise, returns NULL.
28632 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28633 within any error as the location of an "opening" token matching
28634 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28635 RT_CLOSE_PAREN). */
28637 static cp_token *
28638 cp_parser_require (cp_parser* parser,
28639 enum cpp_ttype type,
28640 required_token token_desc,
28641 location_t matching_location)
28643 if (cp_lexer_next_token_is (parser->lexer, type))
28644 return cp_lexer_consume_token (parser->lexer);
28645 else
28647 /* Output the MESSAGE -- unless we're parsing tentatively. */
28648 if (!cp_parser_simulate_error (parser))
28649 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28650 matching_location);
28651 return NULL;
28655 /* An error message is produced if the next token is not '>'.
28656 All further tokens are skipped until the desired token is
28657 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28659 static void
28660 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28662 /* Current level of '< ... >'. */
28663 unsigned level = 0;
28664 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28665 unsigned nesting_depth = 0;
28667 /* Are we ready, yet? If not, issue error message. */
28668 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28669 return;
28671 /* Skip tokens until the desired token is found. */
28672 while (true)
28674 /* Peek at the next token. */
28675 switch (cp_lexer_peek_token (parser->lexer)->type)
28677 case CPP_LESS:
28678 if (!nesting_depth)
28679 ++level;
28680 break;
28682 case CPP_RSHIFT:
28683 if (cxx_dialect == cxx98)
28684 /* C++0x views the `>>' operator as two `>' tokens, but
28685 C++98 does not. */
28686 break;
28687 else if (!nesting_depth && level-- == 0)
28689 /* We've hit a `>>' where the first `>' closes the
28690 template argument list, and the second `>' is
28691 spurious. Just consume the `>>' and stop; we've
28692 already produced at least one error. */
28693 cp_lexer_consume_token (parser->lexer);
28694 return;
28696 /* Fall through for C++0x, so we handle the second `>' in
28697 the `>>'. */
28698 gcc_fallthrough ();
28700 case CPP_GREATER:
28701 if (!nesting_depth && level-- == 0)
28703 /* We've reached the token we want, consume it and stop. */
28704 cp_lexer_consume_token (parser->lexer);
28705 return;
28707 break;
28709 case CPP_OPEN_PAREN:
28710 case CPP_OPEN_SQUARE:
28711 ++nesting_depth;
28712 break;
28714 case CPP_CLOSE_PAREN:
28715 case CPP_CLOSE_SQUARE:
28716 if (nesting_depth-- == 0)
28717 return;
28718 break;
28720 case CPP_EOF:
28721 case CPP_PRAGMA_EOL:
28722 case CPP_SEMICOLON:
28723 case CPP_OPEN_BRACE:
28724 case CPP_CLOSE_BRACE:
28725 /* The '>' was probably forgotten, don't look further. */
28726 return;
28728 default:
28729 break;
28732 /* Consume this token. */
28733 cp_lexer_consume_token (parser->lexer);
28737 /* If the next token is the indicated keyword, consume it. Otherwise,
28738 issue an error message indicating that TOKEN_DESC was expected.
28740 Returns the token consumed, if the token had the appropriate type.
28741 Otherwise, returns NULL. */
28743 static cp_token *
28744 cp_parser_require_keyword (cp_parser* parser,
28745 enum rid keyword,
28746 required_token token_desc)
28748 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28750 if (token && token->keyword != keyword)
28752 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28753 UNKNOWN_LOCATION);
28754 return NULL;
28757 return token;
28760 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28761 function-definition. */
28763 static bool
28764 cp_parser_token_starts_function_definition_p (cp_token* token)
28766 return (/* An ordinary function-body begins with an `{'. */
28767 token->type == CPP_OPEN_BRACE
28768 /* A ctor-initializer begins with a `:'. */
28769 || token->type == CPP_COLON
28770 /* A function-try-block begins with `try'. */
28771 || token->keyword == RID_TRY
28772 /* A function-transaction-block begins with `__transaction_atomic'
28773 or `__transaction_relaxed'. */
28774 || token->keyword == RID_TRANSACTION_ATOMIC
28775 || token->keyword == RID_TRANSACTION_RELAXED
28776 /* The named return value extension begins with `return'. */
28777 || token->keyword == RID_RETURN);
28780 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28781 definition. */
28783 static bool
28784 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28786 cp_token *token;
28788 token = cp_lexer_peek_token (parser->lexer);
28789 return (token->type == CPP_OPEN_BRACE
28790 || (token->type == CPP_COLON
28791 && !parser->colon_doesnt_start_class_def_p));
28794 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28795 C++0x) ending a template-argument. */
28797 static bool
28798 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28800 cp_token *token;
28802 token = cp_lexer_peek_token (parser->lexer);
28803 return (token->type == CPP_COMMA
28804 || token->type == CPP_GREATER
28805 || token->type == CPP_ELLIPSIS
28806 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28809 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28810 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28812 static bool
28813 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28814 size_t n)
28816 cp_token *token;
28818 token = cp_lexer_peek_nth_token (parser->lexer, n);
28819 if (token->type == CPP_LESS)
28820 return true;
28821 /* Check for the sequence `<::' in the original code. It would be lexed as
28822 `[:', where `[' is a digraph, and there is no whitespace before
28823 `:'. */
28824 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28826 cp_token *token2;
28827 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28828 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28829 return true;
28831 return false;
28834 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28835 or none_type otherwise. */
28837 static enum tag_types
28838 cp_parser_token_is_class_key (cp_token* token)
28840 switch (token->keyword)
28842 case RID_CLASS:
28843 return class_type;
28844 case RID_STRUCT:
28845 return record_type;
28846 case RID_UNION:
28847 return union_type;
28849 default:
28850 return none_type;
28854 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28855 or none_type otherwise or if the token is null. */
28857 static enum tag_types
28858 cp_parser_token_is_type_parameter_key (cp_token* token)
28860 if (!token)
28861 return none_type;
28863 switch (token->keyword)
28865 case RID_CLASS:
28866 return class_type;
28867 case RID_TYPENAME:
28868 return typename_type;
28870 default:
28871 return none_type;
28875 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28877 static void
28878 cp_parser_check_class_key (enum tag_types class_key, tree type)
28880 if (type == error_mark_node)
28881 return;
28882 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28884 if (permerror (input_location, "%qs tag used in naming %q#T",
28885 class_key == union_type ? "union"
28886 : class_key == record_type ? "struct" : "class",
28887 type))
28888 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28889 "%q#T was previously declared here", type);
28893 /* Issue an error message if DECL is redeclared with different
28894 access than its original declaration [class.access.spec/3].
28895 This applies to nested classes, nested class templates and
28896 enumerations [class.mem/1]. */
28898 static void
28899 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28901 if (!decl
28902 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28903 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28904 return;
28906 if ((TREE_PRIVATE (decl)
28907 != (current_access_specifier == access_private_node))
28908 || (TREE_PROTECTED (decl)
28909 != (current_access_specifier == access_protected_node)))
28910 error_at (location, "%qD redeclared with different access", decl);
28913 /* Look for the `template' keyword, as a syntactic disambiguator.
28914 Return TRUE iff it is present, in which case it will be
28915 consumed. */
28917 static bool
28918 cp_parser_optional_template_keyword (cp_parser *parser)
28920 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28922 /* In C++98 the `template' keyword can only be used within templates;
28923 outside templates the parser can always figure out what is a
28924 template and what is not. In C++11, per the resolution of DR 468,
28925 `template' is allowed in cases where it is not strictly necessary. */
28926 if (!processing_template_decl
28927 && pedantic && cxx_dialect == cxx98)
28929 cp_token *token = cp_lexer_peek_token (parser->lexer);
28930 pedwarn (token->location, OPT_Wpedantic,
28931 "in C++98 %<template%> (as a disambiguator) is only "
28932 "allowed within templates");
28933 /* If this part of the token stream is rescanned, the same
28934 error message would be generated. So, we purge the token
28935 from the stream. */
28936 cp_lexer_purge_token (parser->lexer);
28937 return false;
28939 else
28941 /* Consume the `template' keyword. */
28942 cp_lexer_consume_token (parser->lexer);
28943 return true;
28946 return false;
28949 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28950 set PARSER->SCOPE, and perform other related actions. */
28952 static void
28953 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28955 struct tree_check *check_value;
28957 /* Get the stored value. */
28958 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28959 /* Set the scope from the stored value. */
28960 parser->scope = saved_checks_value (check_value);
28961 parser->qualifying_scope = check_value->qualifying_scope;
28962 parser->object_scope = NULL_TREE;
28965 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28966 encounter the end of a block before what we were looking for. */
28968 static bool
28969 cp_parser_cache_group (cp_parser *parser,
28970 enum cpp_ttype end,
28971 unsigned depth)
28973 while (true)
28975 cp_token *token = cp_lexer_peek_token (parser->lexer);
28977 /* Abort a parenthesized expression if we encounter a semicolon. */
28978 if ((end == CPP_CLOSE_PAREN || depth == 0)
28979 && token->type == CPP_SEMICOLON)
28980 return true;
28981 /* If we've reached the end of the file, stop. */
28982 if (token->type == CPP_EOF
28983 || (end != CPP_PRAGMA_EOL
28984 && token->type == CPP_PRAGMA_EOL))
28985 return true;
28986 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28987 /* We've hit the end of an enclosing block, so there's been some
28988 kind of syntax error. */
28989 return true;
28991 /* Consume the token. */
28992 cp_lexer_consume_token (parser->lexer);
28993 /* See if it starts a new group. */
28994 if (token->type == CPP_OPEN_BRACE)
28996 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28997 /* In theory this should probably check end == '}', but
28998 cp_parser_save_member_function_body needs it to exit
28999 after either '}' or ')' when called with ')'. */
29000 if (depth == 0)
29001 return false;
29003 else if (token->type == CPP_OPEN_PAREN)
29005 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29006 if (depth == 0 && end == CPP_CLOSE_PAREN)
29007 return false;
29009 else if (token->type == CPP_PRAGMA)
29010 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29011 else if (token->type == end)
29012 return false;
29016 /* Like above, for caching a default argument or NSDMI. Both of these are
29017 terminated by a non-nested comma, but it can be unclear whether or not a
29018 comma is nested in a template argument list unless we do more parsing.
29019 In order to handle this ambiguity, when we encounter a ',' after a '<'
29020 we try to parse what follows as a parameter-declaration-list (in the
29021 case of a default argument) or a member-declarator (in the case of an
29022 NSDMI). If that succeeds, then we stop caching. */
29024 static tree
29025 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29027 unsigned depth = 0;
29028 int maybe_template_id = 0;
29029 cp_token *first_token;
29030 cp_token *token;
29031 tree default_argument;
29033 /* Add tokens until we have processed the entire default
29034 argument. We add the range [first_token, token). */
29035 first_token = cp_lexer_peek_token (parser->lexer);
29036 if (first_token->type == CPP_OPEN_BRACE)
29038 /* For list-initialization, this is straightforward. */
29039 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29040 token = cp_lexer_peek_token (parser->lexer);
29042 else while (true)
29044 bool done = false;
29046 /* Peek at the next token. */
29047 token = cp_lexer_peek_token (parser->lexer);
29048 /* What we do depends on what token we have. */
29049 switch (token->type)
29051 /* In valid code, a default argument must be
29052 immediately followed by a `,' `)', or `...'. */
29053 case CPP_COMMA:
29054 if (depth == 0 && maybe_template_id)
29056 /* If we've seen a '<', we might be in a
29057 template-argument-list. Until Core issue 325 is
29058 resolved, we don't know how this situation ought
29059 to be handled, so try to DTRT. We check whether
29060 what comes after the comma is a valid parameter
29061 declaration list. If it is, then the comma ends
29062 the default argument; otherwise the default
29063 argument continues. */
29064 bool error = false;
29065 cp_token *peek;
29067 /* Set ITALP so cp_parser_parameter_declaration_list
29068 doesn't decide to commit to this parse. */
29069 bool saved_italp = parser->in_template_argument_list_p;
29070 parser->in_template_argument_list_p = true;
29072 cp_parser_parse_tentatively (parser);
29074 if (nsdmi)
29076 /* Parse declarators until we reach a non-comma or
29077 somthing that cannot be an initializer.
29078 Just checking whether we're looking at a single
29079 declarator is insufficient. Consider:
29080 int var = tuple<T,U>::x;
29081 The template parameter 'U' looks exactly like a
29082 declarator. */
29085 int ctor_dtor_or_conv_p;
29086 cp_lexer_consume_token (parser->lexer);
29087 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29088 &ctor_dtor_or_conv_p,
29089 /*parenthesized_p=*/NULL,
29090 /*member_p=*/true,
29091 /*friend_p=*/false);
29092 peek = cp_lexer_peek_token (parser->lexer);
29093 if (cp_parser_error_occurred (parser))
29094 break;
29096 while (peek->type == CPP_COMMA);
29097 /* If we met an '=' or ';' then the original comma
29098 was the end of the NSDMI. Otherwise assume
29099 we're still in the NSDMI. */
29100 error = (peek->type != CPP_EQ
29101 && peek->type != CPP_SEMICOLON);
29103 else
29105 cp_lexer_consume_token (parser->lexer);
29106 begin_scope (sk_function_parms, NULL_TREE);
29107 if (cp_parser_parameter_declaration_list (parser)
29108 == error_mark_node)
29109 error = true;
29110 pop_bindings_and_leave_scope ();
29112 if (!cp_parser_error_occurred (parser) && !error)
29113 done = true;
29114 cp_parser_abort_tentative_parse (parser);
29116 parser->in_template_argument_list_p = saved_italp;
29117 break;
29119 /* FALLTHRU */
29120 case CPP_CLOSE_PAREN:
29121 case CPP_ELLIPSIS:
29122 /* If we run into a non-nested `;', `}', or `]',
29123 then the code is invalid -- but the default
29124 argument is certainly over. */
29125 case CPP_SEMICOLON:
29126 case CPP_CLOSE_BRACE:
29127 case CPP_CLOSE_SQUARE:
29128 if (depth == 0
29129 /* Handle correctly int n = sizeof ... ( p ); */
29130 && token->type != CPP_ELLIPSIS)
29131 done = true;
29132 /* Update DEPTH, if necessary. */
29133 else if (token->type == CPP_CLOSE_PAREN
29134 || token->type == CPP_CLOSE_BRACE
29135 || token->type == CPP_CLOSE_SQUARE)
29136 --depth;
29137 break;
29139 case CPP_OPEN_PAREN:
29140 case CPP_OPEN_SQUARE:
29141 case CPP_OPEN_BRACE:
29142 ++depth;
29143 break;
29145 case CPP_LESS:
29146 if (depth == 0)
29147 /* This might be the comparison operator, or it might
29148 start a template argument list. */
29149 ++maybe_template_id;
29150 break;
29152 case CPP_RSHIFT:
29153 if (cxx_dialect == cxx98)
29154 break;
29155 /* Fall through for C++0x, which treats the `>>'
29156 operator like two `>' tokens in certain
29157 cases. */
29158 gcc_fallthrough ();
29160 case CPP_GREATER:
29161 if (depth == 0)
29163 /* This might be an operator, or it might close a
29164 template argument list. But if a previous '<'
29165 started a template argument list, this will have
29166 closed it, so we can't be in one anymore. */
29167 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29168 if (maybe_template_id < 0)
29169 maybe_template_id = 0;
29171 break;
29173 /* If we run out of tokens, issue an error message. */
29174 case CPP_EOF:
29175 case CPP_PRAGMA_EOL:
29176 error_at (token->location, "file ends in default argument");
29177 return error_mark_node;
29179 case CPP_NAME:
29180 case CPP_SCOPE:
29181 /* In these cases, we should look for template-ids.
29182 For example, if the default argument is
29183 `X<int, double>()', we need to do name lookup to
29184 figure out whether or not `X' is a template; if
29185 so, the `,' does not end the default argument.
29187 That is not yet done. */
29188 break;
29190 default:
29191 break;
29194 /* If we've reached the end, stop. */
29195 if (done)
29196 break;
29198 /* Add the token to the token block. */
29199 token = cp_lexer_consume_token (parser->lexer);
29202 /* Create a DEFAULT_ARG to represent the unparsed default
29203 argument. */
29204 default_argument = make_node (DEFAULT_ARG);
29205 DEFARG_TOKENS (default_argument)
29206 = cp_token_cache_new (first_token, token);
29207 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29209 return default_argument;
29212 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29214 location_t
29215 defarg_location (tree default_argument)
29217 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29218 location_t start = tokens->first->location;
29219 location_t end = tokens->last->location;
29220 return make_location (start, start, end);
29223 /* Begin parsing tentatively. We always save tokens while parsing
29224 tentatively so that if the tentative parsing fails we can restore the
29225 tokens. */
29227 static void
29228 cp_parser_parse_tentatively (cp_parser* parser)
29230 /* Enter a new parsing context. */
29231 parser->context = cp_parser_context_new (parser->context);
29232 /* Begin saving tokens. */
29233 cp_lexer_save_tokens (parser->lexer);
29234 /* In order to avoid repetitive access control error messages,
29235 access checks are queued up until we are no longer parsing
29236 tentatively. */
29237 push_deferring_access_checks (dk_deferred);
29240 /* Commit to the currently active tentative parse. */
29242 static void
29243 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29245 cp_parser_context *context;
29246 cp_lexer *lexer;
29248 /* Mark all of the levels as committed. */
29249 lexer = parser->lexer;
29250 for (context = parser->context; context->next; context = context->next)
29252 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29253 break;
29254 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29255 while (!cp_lexer_saving_tokens (lexer))
29256 lexer = lexer->next;
29257 cp_lexer_commit_tokens (lexer);
29261 /* Commit to the topmost currently active tentative parse.
29263 Note that this function shouldn't be called when there are
29264 irreversible side-effects while in a tentative state. For
29265 example, we shouldn't create a permanent entry in the symbol
29266 table, or issue an error message that might not apply if the
29267 tentative parse is aborted. */
29269 static void
29270 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29272 cp_parser_context *context = parser->context;
29273 cp_lexer *lexer = parser->lexer;
29275 if (context)
29277 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29278 return;
29279 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29281 while (!cp_lexer_saving_tokens (lexer))
29282 lexer = lexer->next;
29283 cp_lexer_commit_tokens (lexer);
29287 /* Abort the currently active tentative parse. All consumed tokens
29288 will be rolled back, and no diagnostics will be issued. */
29290 static void
29291 cp_parser_abort_tentative_parse (cp_parser* parser)
29293 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29294 || errorcount > 0);
29295 cp_parser_simulate_error (parser);
29296 /* Now, pretend that we want to see if the construct was
29297 successfully parsed. */
29298 cp_parser_parse_definitely (parser);
29301 /* Stop parsing tentatively. If a parse error has occurred, restore the
29302 token stream. Otherwise, commit to the tokens we have consumed.
29303 Returns true if no error occurred; false otherwise. */
29305 static bool
29306 cp_parser_parse_definitely (cp_parser* parser)
29308 bool error_occurred;
29309 cp_parser_context *context;
29311 /* Remember whether or not an error occurred, since we are about to
29312 destroy that information. */
29313 error_occurred = cp_parser_error_occurred (parser);
29314 /* Remove the topmost context from the stack. */
29315 context = parser->context;
29316 parser->context = context->next;
29317 /* If no parse errors occurred, commit to the tentative parse. */
29318 if (!error_occurred)
29320 /* Commit to the tokens read tentatively, unless that was
29321 already done. */
29322 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29323 cp_lexer_commit_tokens (parser->lexer);
29325 pop_to_parent_deferring_access_checks ();
29327 /* Otherwise, if errors occurred, roll back our state so that things
29328 are just as they were before we began the tentative parse. */
29329 else
29331 cp_lexer_rollback_tokens (parser->lexer);
29332 pop_deferring_access_checks ();
29334 /* Add the context to the front of the free list. */
29335 context->next = cp_parser_context_free_list;
29336 cp_parser_context_free_list = context;
29338 return !error_occurred;
29341 /* Returns true if we are parsing tentatively and are not committed to
29342 this tentative parse. */
29344 static bool
29345 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29347 return (cp_parser_parsing_tentatively (parser)
29348 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29351 /* Returns nonzero iff an error has occurred during the most recent
29352 tentative parse. */
29354 static bool
29355 cp_parser_error_occurred (cp_parser* parser)
29357 return (cp_parser_parsing_tentatively (parser)
29358 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29361 /* Returns nonzero if GNU extensions are allowed. */
29363 static bool
29364 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29366 return parser->allow_gnu_extensions_p;
29369 /* Objective-C++ Productions */
29372 /* Parse an Objective-C expression, which feeds into a primary-expression
29373 above.
29375 objc-expression:
29376 objc-message-expression
29377 objc-string-literal
29378 objc-encode-expression
29379 objc-protocol-expression
29380 objc-selector-expression
29382 Returns a tree representation of the expression. */
29384 static cp_expr
29385 cp_parser_objc_expression (cp_parser* parser)
29387 /* Try to figure out what kind of declaration is present. */
29388 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29390 switch (kwd->type)
29392 case CPP_OPEN_SQUARE:
29393 return cp_parser_objc_message_expression (parser);
29395 case CPP_OBJC_STRING:
29396 kwd = cp_lexer_consume_token (parser->lexer);
29397 return objc_build_string_object (kwd->u.value);
29399 case CPP_KEYWORD:
29400 switch (kwd->keyword)
29402 case RID_AT_ENCODE:
29403 return cp_parser_objc_encode_expression (parser);
29405 case RID_AT_PROTOCOL:
29406 return cp_parser_objc_protocol_expression (parser);
29408 case RID_AT_SELECTOR:
29409 return cp_parser_objc_selector_expression (parser);
29411 default:
29412 break;
29414 /* FALLTHRU */
29415 default:
29416 error_at (kwd->location,
29417 "misplaced %<@%D%> Objective-C++ construct",
29418 kwd->u.value);
29419 cp_parser_skip_to_end_of_block_or_statement (parser);
29422 return error_mark_node;
29425 /* Parse an Objective-C message expression.
29427 objc-message-expression:
29428 [ objc-message-receiver objc-message-args ]
29430 Returns a representation of an Objective-C message. */
29432 static tree
29433 cp_parser_objc_message_expression (cp_parser* parser)
29435 tree receiver, messageargs;
29437 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29438 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29439 receiver = cp_parser_objc_message_receiver (parser);
29440 messageargs = cp_parser_objc_message_args (parser);
29441 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29442 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29444 tree result = objc_build_message_expr (receiver, messageargs);
29446 /* Construct a location e.g.
29447 [self func1:5]
29448 ^~~~~~~~~~~~~~
29449 ranging from the '[' to the ']', with the caret at the start. */
29450 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29451 protected_set_expr_location (result, combined_loc);
29453 return result;
29456 /* Parse an objc-message-receiver.
29458 objc-message-receiver:
29459 expression
29460 simple-type-specifier
29462 Returns a representation of the type or expression. */
29464 static tree
29465 cp_parser_objc_message_receiver (cp_parser* parser)
29467 tree rcv;
29469 /* An Objective-C message receiver may be either (1) a type
29470 or (2) an expression. */
29471 cp_parser_parse_tentatively (parser);
29472 rcv = cp_parser_expression (parser);
29474 /* If that worked out, fine. */
29475 if (cp_parser_parse_definitely (parser))
29476 return rcv;
29478 cp_parser_parse_tentatively (parser);
29479 rcv = cp_parser_simple_type_specifier (parser,
29480 /*decl_specs=*/NULL,
29481 CP_PARSER_FLAGS_NONE);
29483 if (cp_parser_parse_definitely (parser))
29484 return objc_get_class_reference (rcv);
29486 cp_parser_error (parser, "objective-c++ message receiver expected");
29487 return error_mark_node;
29490 /* Parse the arguments and selectors comprising an Objective-C message.
29492 objc-message-args:
29493 objc-selector
29494 objc-selector-args
29495 objc-selector-args , objc-comma-args
29497 objc-selector-args:
29498 objc-selector [opt] : assignment-expression
29499 objc-selector-args objc-selector [opt] : assignment-expression
29501 objc-comma-args:
29502 assignment-expression
29503 objc-comma-args , assignment-expression
29505 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29506 selector arguments and TREE_VALUE containing a list of comma
29507 arguments. */
29509 static tree
29510 cp_parser_objc_message_args (cp_parser* parser)
29512 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29513 bool maybe_unary_selector_p = true;
29514 cp_token *token = cp_lexer_peek_token (parser->lexer);
29516 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29518 tree selector = NULL_TREE, arg;
29520 if (token->type != CPP_COLON)
29521 selector = cp_parser_objc_selector (parser);
29523 /* Detect if we have a unary selector. */
29524 if (maybe_unary_selector_p
29525 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29526 return build_tree_list (selector, NULL_TREE);
29528 maybe_unary_selector_p = false;
29529 cp_parser_require (parser, CPP_COLON, RT_COLON);
29530 arg = cp_parser_assignment_expression (parser);
29532 sel_args
29533 = chainon (sel_args,
29534 build_tree_list (selector, arg));
29536 token = cp_lexer_peek_token (parser->lexer);
29539 /* Handle non-selector arguments, if any. */
29540 while (token->type == CPP_COMMA)
29542 tree arg;
29544 cp_lexer_consume_token (parser->lexer);
29545 arg = cp_parser_assignment_expression (parser);
29547 addl_args
29548 = chainon (addl_args,
29549 build_tree_list (NULL_TREE, arg));
29551 token = cp_lexer_peek_token (parser->lexer);
29554 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29556 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29557 return build_tree_list (error_mark_node, error_mark_node);
29560 return build_tree_list (sel_args, addl_args);
29563 /* Parse an Objective-C encode expression.
29565 objc-encode-expression:
29566 @encode objc-typename
29568 Returns an encoded representation of the type argument. */
29570 static cp_expr
29571 cp_parser_objc_encode_expression (cp_parser* parser)
29573 tree type;
29574 cp_token *token;
29575 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29577 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29578 matching_parens parens;
29579 parens.require_open (parser);
29580 token = cp_lexer_peek_token (parser->lexer);
29581 type = complete_type (cp_parser_type_id (parser));
29582 parens.require_close (parser);
29584 if (!type)
29586 error_at (token->location,
29587 "%<@encode%> must specify a type as an argument");
29588 return error_mark_node;
29591 /* This happens if we find @encode(T) (where T is a template
29592 typename or something dependent on a template typename) when
29593 parsing a template. In that case, we can't compile it
29594 immediately, but we rather create an AT_ENCODE_EXPR which will
29595 need to be instantiated when the template is used.
29597 if (dependent_type_p (type))
29599 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29600 TREE_READONLY (value) = 1;
29601 return value;
29605 /* Build a location of the form:
29606 @encode(int)
29607 ^~~~~~~~~~~~
29608 with caret==start at the @ token, finishing at the close paren. */
29609 location_t combined_loc
29610 = make_location (start_loc, start_loc,
29611 cp_lexer_previous_token (parser->lexer)->location);
29613 return cp_expr (objc_build_encode_expr (type), combined_loc);
29616 /* Parse an Objective-C @defs expression. */
29618 static tree
29619 cp_parser_objc_defs_expression (cp_parser *parser)
29621 tree name;
29623 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29624 matching_parens parens;
29625 parens.require_open (parser);
29626 name = cp_parser_identifier (parser);
29627 parens.require_close (parser);
29629 return objc_get_class_ivars (name);
29632 /* Parse an Objective-C protocol expression.
29634 objc-protocol-expression:
29635 @protocol ( identifier )
29637 Returns a representation of the protocol expression. */
29639 static tree
29640 cp_parser_objc_protocol_expression (cp_parser* parser)
29642 tree proto;
29643 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29645 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29646 matching_parens parens;
29647 parens.require_open (parser);
29648 proto = cp_parser_identifier (parser);
29649 parens.require_close (parser);
29651 /* Build a location of the form:
29652 @protocol(prot)
29653 ^~~~~~~~~~~~~~~
29654 with caret==start at the @ token, finishing at the close paren. */
29655 location_t combined_loc
29656 = make_location (start_loc, start_loc,
29657 cp_lexer_previous_token (parser->lexer)->location);
29658 tree result = objc_build_protocol_expr (proto);
29659 protected_set_expr_location (result, combined_loc);
29660 return result;
29663 /* Parse an Objective-C selector expression.
29665 objc-selector-expression:
29666 @selector ( objc-method-signature )
29668 objc-method-signature:
29669 objc-selector
29670 objc-selector-seq
29672 objc-selector-seq:
29673 objc-selector :
29674 objc-selector-seq objc-selector :
29676 Returns a representation of the method selector. */
29678 static tree
29679 cp_parser_objc_selector_expression (cp_parser* parser)
29681 tree sel_seq = NULL_TREE;
29682 bool maybe_unary_selector_p = true;
29683 cp_token *token;
29684 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29686 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29687 matching_parens parens;
29688 parens.require_open (parser);
29689 token = cp_lexer_peek_token (parser->lexer);
29691 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29692 || token->type == CPP_SCOPE)
29694 tree selector = NULL_TREE;
29696 if (token->type != CPP_COLON
29697 || token->type == CPP_SCOPE)
29698 selector = cp_parser_objc_selector (parser);
29700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29701 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29703 /* Detect if we have a unary selector. */
29704 if (maybe_unary_selector_p)
29706 sel_seq = selector;
29707 goto finish_selector;
29709 else
29711 cp_parser_error (parser, "expected %<:%>");
29714 maybe_unary_selector_p = false;
29715 token = cp_lexer_consume_token (parser->lexer);
29717 if (token->type == CPP_SCOPE)
29719 sel_seq
29720 = chainon (sel_seq,
29721 build_tree_list (selector, NULL_TREE));
29722 sel_seq
29723 = chainon (sel_seq,
29724 build_tree_list (NULL_TREE, NULL_TREE));
29726 else
29727 sel_seq
29728 = chainon (sel_seq,
29729 build_tree_list (selector, NULL_TREE));
29731 token = cp_lexer_peek_token (parser->lexer);
29734 finish_selector:
29735 parens.require_close (parser);
29738 /* Build a location of the form:
29739 @selector(func)
29740 ^~~~~~~~~~~~~~~
29741 with caret==start at the @ token, finishing at the close paren. */
29742 location_t combined_loc
29743 = make_location (loc, loc,
29744 cp_lexer_previous_token (parser->lexer)->location);
29745 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29746 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29747 protected_set_expr_location (result, combined_loc);
29748 return result;
29751 /* Parse a list of identifiers.
29753 objc-identifier-list:
29754 identifier
29755 objc-identifier-list , identifier
29757 Returns a TREE_LIST of identifier nodes. */
29759 static tree
29760 cp_parser_objc_identifier_list (cp_parser* parser)
29762 tree identifier;
29763 tree list;
29764 cp_token *sep;
29766 identifier = cp_parser_identifier (parser);
29767 if (identifier == error_mark_node)
29768 return error_mark_node;
29770 list = build_tree_list (NULL_TREE, identifier);
29771 sep = cp_lexer_peek_token (parser->lexer);
29773 while (sep->type == CPP_COMMA)
29775 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29776 identifier = cp_parser_identifier (parser);
29777 if (identifier == error_mark_node)
29778 return list;
29780 list = chainon (list, build_tree_list (NULL_TREE,
29781 identifier));
29782 sep = cp_lexer_peek_token (parser->lexer);
29785 return list;
29788 /* Parse an Objective-C alias declaration.
29790 objc-alias-declaration:
29791 @compatibility_alias identifier identifier ;
29793 This function registers the alias mapping with the Objective-C front end.
29794 It returns nothing. */
29796 static void
29797 cp_parser_objc_alias_declaration (cp_parser* parser)
29799 tree alias, orig;
29801 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29802 alias = cp_parser_identifier (parser);
29803 orig = cp_parser_identifier (parser);
29804 objc_declare_alias (alias, orig);
29805 cp_parser_consume_semicolon_at_end_of_statement (parser);
29808 /* Parse an Objective-C class forward-declaration.
29810 objc-class-declaration:
29811 @class objc-identifier-list ;
29813 The function registers the forward declarations with the Objective-C
29814 front end. It returns nothing. */
29816 static void
29817 cp_parser_objc_class_declaration (cp_parser* parser)
29819 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29820 while (true)
29822 tree id;
29824 id = cp_parser_identifier (parser);
29825 if (id == error_mark_node)
29826 break;
29828 objc_declare_class (id);
29830 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29831 cp_lexer_consume_token (parser->lexer);
29832 else
29833 break;
29835 cp_parser_consume_semicolon_at_end_of_statement (parser);
29838 /* Parse a list of Objective-C protocol references.
29840 objc-protocol-refs-opt:
29841 objc-protocol-refs [opt]
29843 objc-protocol-refs:
29844 < objc-identifier-list >
29846 Returns a TREE_LIST of identifiers, if any. */
29848 static tree
29849 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29851 tree protorefs = NULL_TREE;
29853 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29855 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29856 protorefs = cp_parser_objc_identifier_list (parser);
29857 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29860 return protorefs;
29863 /* Parse a Objective-C visibility specification. */
29865 static void
29866 cp_parser_objc_visibility_spec (cp_parser* parser)
29868 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29870 switch (vis->keyword)
29872 case RID_AT_PRIVATE:
29873 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29874 break;
29875 case RID_AT_PROTECTED:
29876 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29877 break;
29878 case RID_AT_PUBLIC:
29879 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29880 break;
29881 case RID_AT_PACKAGE:
29882 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29883 break;
29884 default:
29885 return;
29888 /* Eat '@private'/'@protected'/'@public'. */
29889 cp_lexer_consume_token (parser->lexer);
29892 /* Parse an Objective-C method type. Return 'true' if it is a class
29893 (+) method, and 'false' if it is an instance (-) method. */
29895 static inline bool
29896 cp_parser_objc_method_type (cp_parser* parser)
29898 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29899 return true;
29900 else
29901 return false;
29904 /* Parse an Objective-C protocol qualifier. */
29906 static tree
29907 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29909 tree quals = NULL_TREE, node;
29910 cp_token *token = cp_lexer_peek_token (parser->lexer);
29912 node = token->u.value;
29914 while (node && identifier_p (node)
29915 && (node == ridpointers [(int) RID_IN]
29916 || node == ridpointers [(int) RID_OUT]
29917 || node == ridpointers [(int) RID_INOUT]
29918 || node == ridpointers [(int) RID_BYCOPY]
29919 || node == ridpointers [(int) RID_BYREF]
29920 || node == ridpointers [(int) RID_ONEWAY]))
29922 quals = tree_cons (NULL_TREE, node, quals);
29923 cp_lexer_consume_token (parser->lexer);
29924 token = cp_lexer_peek_token (parser->lexer);
29925 node = token->u.value;
29928 return quals;
29931 /* Parse an Objective-C typename. */
29933 static tree
29934 cp_parser_objc_typename (cp_parser* parser)
29936 tree type_name = NULL_TREE;
29938 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29940 tree proto_quals, cp_type = NULL_TREE;
29942 matching_parens parens;
29943 parens.consume_open (parser); /* Eat '('. */
29944 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29946 /* An ObjC type name may consist of just protocol qualifiers, in which
29947 case the type shall default to 'id'. */
29948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29950 cp_type = cp_parser_type_id (parser);
29952 /* If the type could not be parsed, an error has already
29953 been produced. For error recovery, behave as if it had
29954 not been specified, which will use the default type
29955 'id'. */
29956 if (cp_type == error_mark_node)
29958 cp_type = NULL_TREE;
29959 /* We need to skip to the closing parenthesis as
29960 cp_parser_type_id() does not seem to do it for
29961 us. */
29962 cp_parser_skip_to_closing_parenthesis (parser,
29963 /*recovering=*/true,
29964 /*or_comma=*/false,
29965 /*consume_paren=*/false);
29969 parens.require_close (parser);
29970 type_name = build_tree_list (proto_quals, cp_type);
29973 return type_name;
29976 /* Check to see if TYPE refers to an Objective-C selector name. */
29978 static bool
29979 cp_parser_objc_selector_p (enum cpp_ttype type)
29981 return (type == CPP_NAME || type == CPP_KEYWORD
29982 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29983 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29984 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29985 || type == CPP_XOR || type == CPP_XOR_EQ);
29988 /* Parse an Objective-C selector. */
29990 static tree
29991 cp_parser_objc_selector (cp_parser* parser)
29993 cp_token *token = cp_lexer_consume_token (parser->lexer);
29995 if (!cp_parser_objc_selector_p (token->type))
29997 error_at (token->location, "invalid Objective-C++ selector name");
29998 return error_mark_node;
30001 /* C++ operator names are allowed to appear in ObjC selectors. */
30002 switch (token->type)
30004 case CPP_AND_AND: return get_identifier ("and");
30005 case CPP_AND_EQ: return get_identifier ("and_eq");
30006 case CPP_AND: return get_identifier ("bitand");
30007 case CPP_OR: return get_identifier ("bitor");
30008 case CPP_COMPL: return get_identifier ("compl");
30009 case CPP_NOT: return get_identifier ("not");
30010 case CPP_NOT_EQ: return get_identifier ("not_eq");
30011 case CPP_OR_OR: return get_identifier ("or");
30012 case CPP_OR_EQ: return get_identifier ("or_eq");
30013 case CPP_XOR: return get_identifier ("xor");
30014 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30015 default: return token->u.value;
30019 /* Parse an Objective-C params list. */
30021 static tree
30022 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30024 tree params = NULL_TREE;
30025 bool maybe_unary_selector_p = true;
30026 cp_token *token = cp_lexer_peek_token (parser->lexer);
30028 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30030 tree selector = NULL_TREE, type_name, identifier;
30031 tree parm_attr = NULL_TREE;
30033 if (token->keyword == RID_ATTRIBUTE)
30034 break;
30036 if (token->type != CPP_COLON)
30037 selector = cp_parser_objc_selector (parser);
30039 /* Detect if we have a unary selector. */
30040 if (maybe_unary_selector_p
30041 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30043 params = selector; /* Might be followed by attributes. */
30044 break;
30047 maybe_unary_selector_p = false;
30048 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30050 /* Something went quite wrong. There should be a colon
30051 here, but there is not. Stop parsing parameters. */
30052 break;
30054 type_name = cp_parser_objc_typename (parser);
30055 /* New ObjC allows attributes on parameters too. */
30056 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30057 parm_attr = cp_parser_attributes_opt (parser);
30058 identifier = cp_parser_identifier (parser);
30060 params
30061 = chainon (params,
30062 objc_build_keyword_decl (selector,
30063 type_name,
30064 identifier,
30065 parm_attr));
30067 token = cp_lexer_peek_token (parser->lexer);
30070 if (params == NULL_TREE)
30072 cp_parser_error (parser, "objective-c++ method declaration is expected");
30073 return error_mark_node;
30076 /* We allow tail attributes for the method. */
30077 if (token->keyword == RID_ATTRIBUTE)
30079 *attributes = cp_parser_attributes_opt (parser);
30080 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30081 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30082 return params;
30083 cp_parser_error (parser,
30084 "method attributes must be specified at the end");
30085 return error_mark_node;
30088 if (params == NULL_TREE)
30090 cp_parser_error (parser, "objective-c++ method declaration is expected");
30091 return error_mark_node;
30093 return params;
30096 /* Parse the non-keyword Objective-C params. */
30098 static tree
30099 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30100 tree* attributes)
30102 tree params = make_node (TREE_LIST);
30103 cp_token *token = cp_lexer_peek_token (parser->lexer);
30104 *ellipsisp = false; /* Initially, assume no ellipsis. */
30106 while (token->type == CPP_COMMA)
30108 cp_parameter_declarator *parmdecl;
30109 tree parm;
30111 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30112 token = cp_lexer_peek_token (parser->lexer);
30114 if (token->type == CPP_ELLIPSIS)
30116 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30117 *ellipsisp = true;
30118 token = cp_lexer_peek_token (parser->lexer);
30119 break;
30122 /* TODO: parse attributes for tail parameters. */
30123 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30124 parm = grokdeclarator (parmdecl->declarator,
30125 &parmdecl->decl_specifiers,
30126 PARM, /*initialized=*/0,
30127 /*attrlist=*/NULL);
30129 chainon (params, build_tree_list (NULL_TREE, parm));
30130 token = cp_lexer_peek_token (parser->lexer);
30133 /* We allow tail attributes for the method. */
30134 if (token->keyword == RID_ATTRIBUTE)
30136 if (*attributes == NULL_TREE)
30138 *attributes = cp_parser_attributes_opt (parser);
30139 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30140 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30141 return params;
30143 else
30144 /* We have an error, but parse the attributes, so that we can
30145 carry on. */
30146 *attributes = cp_parser_attributes_opt (parser);
30148 cp_parser_error (parser,
30149 "method attributes must be specified at the end");
30150 return error_mark_node;
30153 return params;
30156 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30158 static void
30159 cp_parser_objc_interstitial_code (cp_parser* parser)
30161 cp_token *token = cp_lexer_peek_token (parser->lexer);
30163 /* If the next token is `extern' and the following token is a string
30164 literal, then we have a linkage specification. */
30165 if (token->keyword == RID_EXTERN
30166 && cp_parser_is_pure_string_literal
30167 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30168 cp_parser_linkage_specification (parser);
30169 /* Handle #pragma, if any. */
30170 else if (token->type == CPP_PRAGMA)
30171 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30172 /* Allow stray semicolons. */
30173 else if (token->type == CPP_SEMICOLON)
30174 cp_lexer_consume_token (parser->lexer);
30175 /* Mark methods as optional or required, when building protocols. */
30176 else if (token->keyword == RID_AT_OPTIONAL)
30178 cp_lexer_consume_token (parser->lexer);
30179 objc_set_method_opt (true);
30181 else if (token->keyword == RID_AT_REQUIRED)
30183 cp_lexer_consume_token (parser->lexer);
30184 objc_set_method_opt (false);
30186 else if (token->keyword == RID_NAMESPACE)
30187 cp_parser_namespace_definition (parser);
30188 /* Other stray characters must generate errors. */
30189 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30191 cp_lexer_consume_token (parser->lexer);
30192 error ("stray %qs between Objective-C++ methods",
30193 token->type == CPP_OPEN_BRACE ? "{" : "}");
30195 /* Finally, try to parse a block-declaration, or a function-definition. */
30196 else
30197 cp_parser_block_declaration (parser, /*statement_p=*/false);
30200 /* Parse a method signature. */
30202 static tree
30203 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30205 tree rettype, kwdparms, optparms;
30206 bool ellipsis = false;
30207 bool is_class_method;
30209 is_class_method = cp_parser_objc_method_type (parser);
30210 rettype = cp_parser_objc_typename (parser);
30211 *attributes = NULL_TREE;
30212 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30213 if (kwdparms == error_mark_node)
30214 return error_mark_node;
30215 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30216 if (optparms == error_mark_node)
30217 return error_mark_node;
30219 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30222 static bool
30223 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30225 tree tattr;
30226 cp_lexer_save_tokens (parser->lexer);
30227 tattr = cp_parser_attributes_opt (parser);
30228 gcc_assert (tattr) ;
30230 /* If the attributes are followed by a method introducer, this is not allowed.
30231 Dump the attributes and flag the situation. */
30232 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30233 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30234 return true;
30236 /* Otherwise, the attributes introduce some interstitial code, possibly so
30237 rewind to allow that check. */
30238 cp_lexer_rollback_tokens (parser->lexer);
30239 return false;
30242 /* Parse an Objective-C method prototype list. */
30244 static void
30245 cp_parser_objc_method_prototype_list (cp_parser* parser)
30247 cp_token *token = cp_lexer_peek_token (parser->lexer);
30249 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30251 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30253 tree attributes, sig;
30254 bool is_class_method;
30255 if (token->type == CPP_PLUS)
30256 is_class_method = true;
30257 else
30258 is_class_method = false;
30259 sig = cp_parser_objc_method_signature (parser, &attributes);
30260 if (sig == error_mark_node)
30262 cp_parser_skip_to_end_of_block_or_statement (parser);
30263 token = cp_lexer_peek_token (parser->lexer);
30264 continue;
30266 objc_add_method_declaration (is_class_method, sig, attributes);
30267 cp_parser_consume_semicolon_at_end_of_statement (parser);
30269 else if (token->keyword == RID_AT_PROPERTY)
30270 cp_parser_objc_at_property_declaration (parser);
30271 else if (token->keyword == RID_ATTRIBUTE
30272 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30273 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30274 OPT_Wattributes,
30275 "prefix attributes are ignored for methods");
30276 else
30277 /* Allow for interspersed non-ObjC++ code. */
30278 cp_parser_objc_interstitial_code (parser);
30280 token = cp_lexer_peek_token (parser->lexer);
30283 if (token->type != CPP_EOF)
30284 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30285 else
30286 cp_parser_error (parser, "expected %<@end%>");
30288 objc_finish_interface ();
30291 /* Parse an Objective-C method definition list. */
30293 static void
30294 cp_parser_objc_method_definition_list (cp_parser* parser)
30296 cp_token *token = cp_lexer_peek_token (parser->lexer);
30298 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30300 tree meth;
30302 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30304 cp_token *ptk;
30305 tree sig, attribute;
30306 bool is_class_method;
30307 if (token->type == CPP_PLUS)
30308 is_class_method = true;
30309 else
30310 is_class_method = false;
30311 push_deferring_access_checks (dk_deferred);
30312 sig = cp_parser_objc_method_signature (parser, &attribute);
30313 if (sig == error_mark_node)
30315 cp_parser_skip_to_end_of_block_or_statement (parser);
30316 token = cp_lexer_peek_token (parser->lexer);
30317 continue;
30319 objc_start_method_definition (is_class_method, sig, attribute,
30320 NULL_TREE);
30322 /* For historical reasons, we accept an optional semicolon. */
30323 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30324 cp_lexer_consume_token (parser->lexer);
30326 ptk = cp_lexer_peek_token (parser->lexer);
30327 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30328 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30330 perform_deferred_access_checks (tf_warning_or_error);
30331 stop_deferring_access_checks ();
30332 meth = cp_parser_function_definition_after_declarator (parser,
30333 false);
30334 pop_deferring_access_checks ();
30335 objc_finish_method_definition (meth);
30338 /* The following case will be removed once @synthesize is
30339 completely implemented. */
30340 else if (token->keyword == RID_AT_PROPERTY)
30341 cp_parser_objc_at_property_declaration (parser);
30342 else if (token->keyword == RID_AT_SYNTHESIZE)
30343 cp_parser_objc_at_synthesize_declaration (parser);
30344 else if (token->keyword == RID_AT_DYNAMIC)
30345 cp_parser_objc_at_dynamic_declaration (parser);
30346 else if (token->keyword == RID_ATTRIBUTE
30347 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30348 warning_at (token->location, OPT_Wattributes,
30349 "prefix attributes are ignored for methods");
30350 else
30351 /* Allow for interspersed non-ObjC++ code. */
30352 cp_parser_objc_interstitial_code (parser);
30354 token = cp_lexer_peek_token (parser->lexer);
30357 if (token->type != CPP_EOF)
30358 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30359 else
30360 cp_parser_error (parser, "expected %<@end%>");
30362 objc_finish_implementation ();
30365 /* Parse Objective-C ivars. */
30367 static void
30368 cp_parser_objc_class_ivars (cp_parser* parser)
30370 cp_token *token = cp_lexer_peek_token (parser->lexer);
30372 if (token->type != CPP_OPEN_BRACE)
30373 return; /* No ivars specified. */
30375 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30376 token = cp_lexer_peek_token (parser->lexer);
30378 while (token->type != CPP_CLOSE_BRACE
30379 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30381 cp_decl_specifier_seq declspecs;
30382 int decl_class_or_enum_p;
30383 tree prefix_attributes;
30385 cp_parser_objc_visibility_spec (parser);
30387 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30388 break;
30390 cp_parser_decl_specifier_seq (parser,
30391 CP_PARSER_FLAGS_OPTIONAL,
30392 &declspecs,
30393 &decl_class_or_enum_p);
30395 /* auto, register, static, extern, mutable. */
30396 if (declspecs.storage_class != sc_none)
30398 cp_parser_error (parser, "invalid type for instance variable");
30399 declspecs.storage_class = sc_none;
30402 /* thread_local. */
30403 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30405 cp_parser_error (parser, "invalid type for instance variable");
30406 declspecs.locations[ds_thread] = 0;
30409 /* typedef. */
30410 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30412 cp_parser_error (parser, "invalid type for instance variable");
30413 declspecs.locations[ds_typedef] = 0;
30416 prefix_attributes = declspecs.attributes;
30417 declspecs.attributes = NULL_TREE;
30419 /* Keep going until we hit the `;' at the end of the
30420 declaration. */
30421 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30423 tree width = NULL_TREE, attributes, first_attribute, decl;
30424 cp_declarator *declarator = NULL;
30425 int ctor_dtor_or_conv_p;
30427 /* Check for a (possibly unnamed) bitfield declaration. */
30428 token = cp_lexer_peek_token (parser->lexer);
30429 if (token->type == CPP_COLON)
30430 goto eat_colon;
30432 if (token->type == CPP_NAME
30433 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30434 == CPP_COLON))
30436 /* Get the name of the bitfield. */
30437 declarator = make_id_declarator (NULL_TREE,
30438 cp_parser_identifier (parser),
30439 sfk_none);
30441 eat_colon:
30442 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30443 /* Get the width of the bitfield. */
30444 width
30445 = cp_parser_constant_expression (parser);
30447 else
30449 /* Parse the declarator. */
30450 declarator
30451 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30452 &ctor_dtor_or_conv_p,
30453 /*parenthesized_p=*/NULL,
30454 /*member_p=*/false,
30455 /*friend_p=*/false);
30458 /* Look for attributes that apply to the ivar. */
30459 attributes = cp_parser_attributes_opt (parser);
30460 /* Remember which attributes are prefix attributes and
30461 which are not. */
30462 first_attribute = attributes;
30463 /* Combine the attributes. */
30464 attributes = attr_chainon (prefix_attributes, attributes);
30466 if (width)
30467 /* Create the bitfield declaration. */
30468 decl = grokbitfield (declarator, &declspecs,
30469 width, NULL_TREE, attributes);
30470 else
30471 decl = grokfield (declarator, &declspecs,
30472 NULL_TREE, /*init_const_expr_p=*/false,
30473 NULL_TREE, attributes);
30475 /* Add the instance variable. */
30476 if (decl != error_mark_node && decl != NULL_TREE)
30477 objc_add_instance_variable (decl);
30479 /* Reset PREFIX_ATTRIBUTES. */
30480 if (attributes != error_mark_node)
30482 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30483 attributes = TREE_CHAIN (attributes);
30484 if (attributes)
30485 TREE_CHAIN (attributes) = NULL_TREE;
30488 token = cp_lexer_peek_token (parser->lexer);
30490 if (token->type == CPP_COMMA)
30492 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30493 continue;
30495 break;
30498 cp_parser_consume_semicolon_at_end_of_statement (parser);
30499 token = cp_lexer_peek_token (parser->lexer);
30502 if (token->keyword == RID_AT_END)
30503 cp_parser_error (parser, "expected %<}%>");
30505 /* Do not consume the RID_AT_END, so it will be read again as terminating
30506 the @interface of @implementation. */
30507 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30508 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30510 /* For historical reasons, we accept an optional semicolon. */
30511 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30512 cp_lexer_consume_token (parser->lexer);
30515 /* Parse an Objective-C protocol declaration. */
30517 static void
30518 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30520 tree proto, protorefs;
30521 cp_token *tok;
30523 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30526 tok = cp_lexer_peek_token (parser->lexer);
30527 error_at (tok->location, "identifier expected after %<@protocol%>");
30528 cp_parser_consume_semicolon_at_end_of_statement (parser);
30529 return;
30532 /* See if we have a forward declaration or a definition. */
30533 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30535 /* Try a forward declaration first. */
30536 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30538 while (true)
30540 tree id;
30542 id = cp_parser_identifier (parser);
30543 if (id == error_mark_node)
30544 break;
30546 objc_declare_protocol (id, attributes);
30548 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30549 cp_lexer_consume_token (parser->lexer);
30550 else
30551 break;
30553 cp_parser_consume_semicolon_at_end_of_statement (parser);
30556 /* Ok, we got a full-fledged definition (or at least should). */
30557 else
30559 proto = cp_parser_identifier (parser);
30560 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30561 objc_start_protocol (proto, protorefs, attributes);
30562 cp_parser_objc_method_prototype_list (parser);
30566 /* Parse an Objective-C superclass or category. */
30568 static void
30569 cp_parser_objc_superclass_or_category (cp_parser *parser,
30570 bool iface_p,
30571 tree *super,
30572 tree *categ, bool *is_class_extension)
30574 cp_token *next = cp_lexer_peek_token (parser->lexer);
30576 *super = *categ = NULL_TREE;
30577 *is_class_extension = false;
30578 if (next->type == CPP_COLON)
30580 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30581 *super = cp_parser_identifier (parser);
30583 else if (next->type == CPP_OPEN_PAREN)
30585 matching_parens parens;
30586 parens.consume_open (parser); /* Eat '('. */
30588 /* If there is no category name, and this is an @interface, we
30589 have a class extension. */
30590 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30592 *categ = NULL_TREE;
30593 *is_class_extension = true;
30595 else
30596 *categ = cp_parser_identifier (parser);
30598 parens.require_close (parser);
30602 /* Parse an Objective-C class interface. */
30604 static void
30605 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30607 tree name, super, categ, protos;
30608 bool is_class_extension;
30610 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30611 name = cp_parser_identifier (parser);
30612 if (name == error_mark_node)
30614 /* It's hard to recover because even if valid @interface stuff
30615 is to follow, we can't compile it (or validate it) if we
30616 don't even know which class it refers to. Let's assume this
30617 was a stray '@interface' token in the stream and skip it.
30619 return;
30621 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30622 &is_class_extension);
30623 protos = cp_parser_objc_protocol_refs_opt (parser);
30625 /* We have either a class or a category on our hands. */
30626 if (categ || is_class_extension)
30627 objc_start_category_interface (name, categ, protos, attributes);
30628 else
30630 objc_start_class_interface (name, super, protos, attributes);
30631 /* Handle instance variable declarations, if any. */
30632 cp_parser_objc_class_ivars (parser);
30633 objc_continue_interface ();
30636 cp_parser_objc_method_prototype_list (parser);
30639 /* Parse an Objective-C class implementation. */
30641 static void
30642 cp_parser_objc_class_implementation (cp_parser* parser)
30644 tree name, super, categ;
30645 bool is_class_extension;
30647 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30648 name = cp_parser_identifier (parser);
30649 if (name == error_mark_node)
30651 /* It's hard to recover because even if valid @implementation
30652 stuff is to follow, we can't compile it (or validate it) if
30653 we don't even know which class it refers to. Let's assume
30654 this was a stray '@implementation' token in the stream and
30655 skip it.
30657 return;
30659 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30660 &is_class_extension);
30662 /* We have either a class or a category on our hands. */
30663 if (categ)
30664 objc_start_category_implementation (name, categ);
30665 else
30667 objc_start_class_implementation (name, super);
30668 /* Handle instance variable declarations, if any. */
30669 cp_parser_objc_class_ivars (parser);
30670 objc_continue_implementation ();
30673 cp_parser_objc_method_definition_list (parser);
30676 /* Consume the @end token and finish off the implementation. */
30678 static void
30679 cp_parser_objc_end_implementation (cp_parser* parser)
30681 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30682 objc_finish_implementation ();
30685 /* Parse an Objective-C declaration. */
30687 static void
30688 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30690 /* Try to figure out what kind of declaration is present. */
30691 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30693 if (attributes)
30694 switch (kwd->keyword)
30696 case RID_AT_ALIAS:
30697 case RID_AT_CLASS:
30698 case RID_AT_END:
30699 error_at (kwd->location, "attributes may not be specified before"
30700 " the %<@%D%> Objective-C++ keyword",
30701 kwd->u.value);
30702 attributes = NULL;
30703 break;
30704 case RID_AT_IMPLEMENTATION:
30705 warning_at (kwd->location, OPT_Wattributes,
30706 "prefix attributes are ignored before %<@%D%>",
30707 kwd->u.value);
30708 attributes = NULL;
30709 default:
30710 break;
30713 switch (kwd->keyword)
30715 case RID_AT_ALIAS:
30716 cp_parser_objc_alias_declaration (parser);
30717 break;
30718 case RID_AT_CLASS:
30719 cp_parser_objc_class_declaration (parser);
30720 break;
30721 case RID_AT_PROTOCOL:
30722 cp_parser_objc_protocol_declaration (parser, attributes);
30723 break;
30724 case RID_AT_INTERFACE:
30725 cp_parser_objc_class_interface (parser, attributes);
30726 break;
30727 case RID_AT_IMPLEMENTATION:
30728 cp_parser_objc_class_implementation (parser);
30729 break;
30730 case RID_AT_END:
30731 cp_parser_objc_end_implementation (parser);
30732 break;
30733 default:
30734 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30735 kwd->u.value);
30736 cp_parser_skip_to_end_of_block_or_statement (parser);
30740 /* Parse an Objective-C try-catch-finally statement.
30742 objc-try-catch-finally-stmt:
30743 @try compound-statement objc-catch-clause-seq [opt]
30744 objc-finally-clause [opt]
30746 objc-catch-clause-seq:
30747 objc-catch-clause objc-catch-clause-seq [opt]
30749 objc-catch-clause:
30750 @catch ( objc-exception-declaration ) compound-statement
30752 objc-finally-clause:
30753 @finally compound-statement
30755 objc-exception-declaration:
30756 parameter-declaration
30757 '...'
30759 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30761 Returns NULL_TREE.
30763 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30764 for C. Keep them in sync. */
30766 static tree
30767 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30769 location_t location;
30770 tree stmt;
30772 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30773 location = cp_lexer_peek_token (parser->lexer)->location;
30774 objc_maybe_warn_exceptions (location);
30775 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30776 node, lest it get absorbed into the surrounding block. */
30777 stmt = push_stmt_list ();
30778 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30779 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30781 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30783 cp_parameter_declarator *parm;
30784 tree parameter_declaration = error_mark_node;
30785 bool seen_open_paren = false;
30786 matching_parens parens;
30788 cp_lexer_consume_token (parser->lexer);
30789 if (parens.require_open (parser))
30790 seen_open_paren = true;
30791 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30793 /* We have "@catch (...)" (where the '...' are literally
30794 what is in the code). Skip the '...'.
30795 parameter_declaration is set to NULL_TREE, and
30796 objc_being_catch_clauses() knows that that means
30797 '...'. */
30798 cp_lexer_consume_token (parser->lexer);
30799 parameter_declaration = NULL_TREE;
30801 else
30803 /* We have "@catch (NSException *exception)" or something
30804 like that. Parse the parameter declaration. */
30805 parm = cp_parser_parameter_declaration (parser, false, NULL);
30806 if (parm == NULL)
30807 parameter_declaration = error_mark_node;
30808 else
30809 parameter_declaration = grokdeclarator (parm->declarator,
30810 &parm->decl_specifiers,
30811 PARM, /*initialized=*/0,
30812 /*attrlist=*/NULL);
30814 if (seen_open_paren)
30815 parens.require_close (parser);
30816 else
30818 /* If there was no open parenthesis, we are recovering from
30819 an error, and we are trying to figure out what mistake
30820 the user has made. */
30822 /* If there is an immediate closing parenthesis, the user
30823 probably forgot the opening one (ie, they typed "@catch
30824 NSException *e)". Parse the closing parenthesis and keep
30825 going. */
30826 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30827 cp_lexer_consume_token (parser->lexer);
30829 /* If these is no immediate closing parenthesis, the user
30830 probably doesn't know that parenthesis are required at
30831 all (ie, they typed "@catch NSException *e"). So, just
30832 forget about the closing parenthesis and keep going. */
30834 objc_begin_catch_clause (parameter_declaration);
30835 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30836 objc_finish_catch_clause ();
30838 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30840 cp_lexer_consume_token (parser->lexer);
30841 location = cp_lexer_peek_token (parser->lexer)->location;
30842 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30843 node, lest it get absorbed into the surrounding block. */
30844 stmt = push_stmt_list ();
30845 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30846 objc_build_finally_clause (location, pop_stmt_list (stmt));
30849 return objc_finish_try_stmt ();
30852 /* Parse an Objective-C synchronized statement.
30854 objc-synchronized-stmt:
30855 @synchronized ( expression ) compound-statement
30857 Returns NULL_TREE. */
30859 static tree
30860 cp_parser_objc_synchronized_statement (cp_parser *parser)
30862 location_t location;
30863 tree lock, stmt;
30865 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30867 location = cp_lexer_peek_token (parser->lexer)->location;
30868 objc_maybe_warn_exceptions (location);
30869 matching_parens parens;
30870 parens.require_open (parser);
30871 lock = cp_parser_expression (parser);
30872 parens.require_close (parser);
30874 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30875 node, lest it get absorbed into the surrounding block. */
30876 stmt = push_stmt_list ();
30877 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30879 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30882 /* Parse an Objective-C throw statement.
30884 objc-throw-stmt:
30885 @throw assignment-expression [opt] ;
30887 Returns a constructed '@throw' statement. */
30889 static tree
30890 cp_parser_objc_throw_statement (cp_parser *parser)
30892 tree expr = NULL_TREE;
30893 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30895 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30897 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30898 expr = cp_parser_expression (parser);
30900 cp_parser_consume_semicolon_at_end_of_statement (parser);
30902 return objc_build_throw_stmt (loc, expr);
30905 /* Parse an Objective-C statement. */
30907 static tree
30908 cp_parser_objc_statement (cp_parser * parser)
30910 /* Try to figure out what kind of declaration is present. */
30911 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30913 switch (kwd->keyword)
30915 case RID_AT_TRY:
30916 return cp_parser_objc_try_catch_finally_statement (parser);
30917 case RID_AT_SYNCHRONIZED:
30918 return cp_parser_objc_synchronized_statement (parser);
30919 case RID_AT_THROW:
30920 return cp_parser_objc_throw_statement (parser);
30921 default:
30922 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30923 kwd->u.value);
30924 cp_parser_skip_to_end_of_block_or_statement (parser);
30927 return error_mark_node;
30930 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30931 look ahead to see if an objc keyword follows the attributes. This
30932 is to detect the use of prefix attributes on ObjC @interface and
30933 @protocol. */
30935 static bool
30936 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30938 cp_lexer_save_tokens (parser->lexer);
30939 *attrib = cp_parser_attributes_opt (parser);
30940 gcc_assert (*attrib);
30941 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30943 cp_lexer_commit_tokens (parser->lexer);
30944 return true;
30946 cp_lexer_rollback_tokens (parser->lexer);
30947 return false;
30950 /* This routine is a minimal replacement for
30951 c_parser_struct_declaration () used when parsing the list of
30952 types/names or ObjC++ properties. For example, when parsing the
30953 code
30955 @property (readonly) int a, b, c;
30957 this function is responsible for parsing "int a, int b, int c" and
30958 returning the declarations as CHAIN of DECLs.
30960 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30961 similar parsing. */
30962 static tree
30963 cp_parser_objc_struct_declaration (cp_parser *parser)
30965 tree decls = NULL_TREE;
30966 cp_decl_specifier_seq declspecs;
30967 int decl_class_or_enum_p;
30968 tree prefix_attributes;
30970 cp_parser_decl_specifier_seq (parser,
30971 CP_PARSER_FLAGS_NONE,
30972 &declspecs,
30973 &decl_class_or_enum_p);
30975 if (declspecs.type == error_mark_node)
30976 return error_mark_node;
30978 /* auto, register, static, extern, mutable. */
30979 if (declspecs.storage_class != sc_none)
30981 cp_parser_error (parser, "invalid type for property");
30982 declspecs.storage_class = sc_none;
30985 /* thread_local. */
30986 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30988 cp_parser_error (parser, "invalid type for property");
30989 declspecs.locations[ds_thread] = 0;
30992 /* typedef. */
30993 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30995 cp_parser_error (parser, "invalid type for property");
30996 declspecs.locations[ds_typedef] = 0;
30999 prefix_attributes = declspecs.attributes;
31000 declspecs.attributes = NULL_TREE;
31002 /* Keep going until we hit the `;' at the end of the declaration. */
31003 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31005 tree attributes, first_attribute, decl;
31006 cp_declarator *declarator;
31007 cp_token *token;
31009 /* Parse the declarator. */
31010 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31011 NULL, NULL, false, false);
31013 /* Look for attributes that apply to the ivar. */
31014 attributes = cp_parser_attributes_opt (parser);
31015 /* Remember which attributes are prefix attributes and
31016 which are not. */
31017 first_attribute = attributes;
31018 /* Combine the attributes. */
31019 attributes = attr_chainon (prefix_attributes, attributes);
31021 decl = grokfield (declarator, &declspecs,
31022 NULL_TREE, /*init_const_expr_p=*/false,
31023 NULL_TREE, attributes);
31025 if (decl == error_mark_node || decl == NULL_TREE)
31026 return error_mark_node;
31028 /* Reset PREFIX_ATTRIBUTES. */
31029 if (attributes != error_mark_node)
31031 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31032 attributes = TREE_CHAIN (attributes);
31033 if (attributes)
31034 TREE_CHAIN (attributes) = NULL_TREE;
31037 DECL_CHAIN (decl) = decls;
31038 decls = decl;
31040 token = cp_lexer_peek_token (parser->lexer);
31041 if (token->type == CPP_COMMA)
31043 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31044 continue;
31046 else
31047 break;
31049 return decls;
31052 /* Parse an Objective-C @property declaration. The syntax is:
31054 objc-property-declaration:
31055 '@property' objc-property-attributes[opt] struct-declaration ;
31057 objc-property-attributes:
31058 '(' objc-property-attribute-list ')'
31060 objc-property-attribute-list:
31061 objc-property-attribute
31062 objc-property-attribute-list, objc-property-attribute
31064 objc-property-attribute
31065 'getter' = identifier
31066 'setter' = identifier
31067 'readonly'
31068 'readwrite'
31069 'assign'
31070 'retain'
31071 'copy'
31072 'nonatomic'
31074 For example:
31075 @property NSString *name;
31076 @property (readonly) id object;
31077 @property (retain, nonatomic, getter=getTheName) id name;
31078 @property int a, b, c;
31080 PS: This function is identical to
31081 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31082 static void
31083 cp_parser_objc_at_property_declaration (cp_parser *parser)
31085 /* The following variables hold the attributes of the properties as
31086 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31087 seen. When we see an attribute, we set them to 'true' (if they
31088 are boolean properties) or to the identifier (if they have an
31089 argument, ie, for getter and setter). Note that here we only
31090 parse the list of attributes, check the syntax and accumulate the
31091 attributes that we find. objc_add_property_declaration() will
31092 then process the information. */
31093 bool property_assign = false;
31094 bool property_copy = false;
31095 tree property_getter_ident = NULL_TREE;
31096 bool property_nonatomic = false;
31097 bool property_readonly = false;
31098 bool property_readwrite = false;
31099 bool property_retain = false;
31100 tree property_setter_ident = NULL_TREE;
31102 /* 'properties' is the list of properties that we read. Usually a
31103 single one, but maybe more (eg, in "@property int a, b, c;" there
31104 are three). */
31105 tree properties;
31106 location_t loc;
31108 loc = cp_lexer_peek_token (parser->lexer)->location;
31110 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31112 /* Parse the optional attribute list... */
31113 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31115 /* Eat the '('. */
31116 matching_parens parens;
31117 parens.consume_open (parser);
31119 while (true)
31121 bool syntax_error = false;
31122 cp_token *token = cp_lexer_peek_token (parser->lexer);
31123 enum rid keyword;
31125 if (token->type != CPP_NAME)
31127 cp_parser_error (parser, "expected identifier");
31128 break;
31130 keyword = C_RID_CODE (token->u.value);
31131 cp_lexer_consume_token (parser->lexer);
31132 switch (keyword)
31134 case RID_ASSIGN: property_assign = true; break;
31135 case RID_COPY: property_copy = true; break;
31136 case RID_NONATOMIC: property_nonatomic = true; break;
31137 case RID_READONLY: property_readonly = true; break;
31138 case RID_READWRITE: property_readwrite = true; break;
31139 case RID_RETAIN: property_retain = true; break;
31141 case RID_GETTER:
31142 case RID_SETTER:
31143 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31145 if (keyword == RID_GETTER)
31146 cp_parser_error (parser,
31147 "missing %<=%> (after %<getter%> attribute)");
31148 else
31149 cp_parser_error (parser,
31150 "missing %<=%> (after %<setter%> attribute)");
31151 syntax_error = true;
31152 break;
31154 cp_lexer_consume_token (parser->lexer); /* eat the = */
31155 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31157 cp_parser_error (parser, "expected identifier");
31158 syntax_error = true;
31159 break;
31161 if (keyword == RID_SETTER)
31163 if (property_setter_ident != NULL_TREE)
31165 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31166 cp_lexer_consume_token (parser->lexer);
31168 else
31169 property_setter_ident = cp_parser_objc_selector (parser);
31170 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31171 cp_parser_error (parser, "setter name must terminate with %<:%>");
31172 else
31173 cp_lexer_consume_token (parser->lexer);
31175 else
31177 if (property_getter_ident != NULL_TREE)
31179 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31180 cp_lexer_consume_token (parser->lexer);
31182 else
31183 property_getter_ident = cp_parser_objc_selector (parser);
31185 break;
31186 default:
31187 cp_parser_error (parser, "unknown property attribute");
31188 syntax_error = true;
31189 break;
31192 if (syntax_error)
31193 break;
31195 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31196 cp_lexer_consume_token (parser->lexer);
31197 else
31198 break;
31201 /* FIXME: "@property (setter, assign);" will generate a spurious
31202 "error: expected ‘)’ before ‘,’ token". This is because
31203 cp_parser_require, unlike the C counterpart, will produce an
31204 error even if we are in error recovery. */
31205 if (!parens.require_close (parser))
31207 cp_parser_skip_to_closing_parenthesis (parser,
31208 /*recovering=*/true,
31209 /*or_comma=*/false,
31210 /*consume_paren=*/true);
31214 /* ... and the property declaration(s). */
31215 properties = cp_parser_objc_struct_declaration (parser);
31217 if (properties == error_mark_node)
31219 cp_parser_skip_to_end_of_statement (parser);
31220 /* If the next token is now a `;', consume it. */
31221 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31222 cp_lexer_consume_token (parser->lexer);
31223 return;
31226 if (properties == NULL_TREE)
31227 cp_parser_error (parser, "expected identifier");
31228 else
31230 /* Comma-separated properties are chained together in
31231 reverse order; add them one by one. */
31232 properties = nreverse (properties);
31234 for (; properties; properties = TREE_CHAIN (properties))
31235 objc_add_property_declaration (loc, copy_node (properties),
31236 property_readonly, property_readwrite,
31237 property_assign, property_retain,
31238 property_copy, property_nonatomic,
31239 property_getter_ident, property_setter_ident);
31242 cp_parser_consume_semicolon_at_end_of_statement (parser);
31245 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31247 objc-synthesize-declaration:
31248 @synthesize objc-synthesize-identifier-list ;
31250 objc-synthesize-identifier-list:
31251 objc-synthesize-identifier
31252 objc-synthesize-identifier-list, objc-synthesize-identifier
31254 objc-synthesize-identifier
31255 identifier
31256 identifier = identifier
31258 For example:
31259 @synthesize MyProperty;
31260 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31262 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31263 for C. Keep them in sync.
31265 static void
31266 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31268 tree list = NULL_TREE;
31269 location_t loc;
31270 loc = cp_lexer_peek_token (parser->lexer)->location;
31272 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31273 while (true)
31275 tree property, ivar;
31276 property = cp_parser_identifier (parser);
31277 if (property == error_mark_node)
31279 cp_parser_consume_semicolon_at_end_of_statement (parser);
31280 return;
31282 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31284 cp_lexer_consume_token (parser->lexer);
31285 ivar = cp_parser_identifier (parser);
31286 if (ivar == error_mark_node)
31288 cp_parser_consume_semicolon_at_end_of_statement (parser);
31289 return;
31292 else
31293 ivar = NULL_TREE;
31294 list = chainon (list, build_tree_list (ivar, property));
31295 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31296 cp_lexer_consume_token (parser->lexer);
31297 else
31298 break;
31300 cp_parser_consume_semicolon_at_end_of_statement (parser);
31301 objc_add_synthesize_declaration (loc, list);
31304 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31306 objc-dynamic-declaration:
31307 @dynamic identifier-list ;
31309 For example:
31310 @dynamic MyProperty;
31311 @dynamic MyProperty, AnotherProperty;
31313 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31314 for C. Keep them in sync.
31316 static void
31317 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31319 tree list = NULL_TREE;
31320 location_t loc;
31321 loc = cp_lexer_peek_token (parser->lexer)->location;
31323 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31324 while (true)
31326 tree property;
31327 property = cp_parser_identifier (parser);
31328 if (property == error_mark_node)
31330 cp_parser_consume_semicolon_at_end_of_statement (parser);
31331 return;
31333 list = chainon (list, build_tree_list (NULL, property));
31334 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31335 cp_lexer_consume_token (parser->lexer);
31336 else
31337 break;
31339 cp_parser_consume_semicolon_at_end_of_statement (parser);
31340 objc_add_dynamic_declaration (loc, list);
31344 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31346 /* Returns name of the next clause.
31347 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31348 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31349 returned and the token is consumed. */
31351 static pragma_omp_clause
31352 cp_parser_omp_clause_name (cp_parser *parser)
31354 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31356 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31357 result = PRAGMA_OACC_CLAUSE_AUTO;
31358 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31359 result = PRAGMA_OMP_CLAUSE_IF;
31360 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31361 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31362 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31363 result = PRAGMA_OACC_CLAUSE_DELETE;
31364 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31365 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31366 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31367 result = PRAGMA_OMP_CLAUSE_FOR;
31368 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31370 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31371 const char *p = IDENTIFIER_POINTER (id);
31373 switch (p[0])
31375 case 'a':
31376 if (!strcmp ("aligned", p))
31377 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31378 else if (!strcmp ("async", p))
31379 result = PRAGMA_OACC_CLAUSE_ASYNC;
31380 break;
31381 case 'c':
31382 if (!strcmp ("collapse", p))
31383 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31384 else if (!strcmp ("copy", p))
31385 result = PRAGMA_OACC_CLAUSE_COPY;
31386 else if (!strcmp ("copyin", p))
31387 result = PRAGMA_OMP_CLAUSE_COPYIN;
31388 else if (!strcmp ("copyout", p))
31389 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31390 else if (!strcmp ("copyprivate", p))
31391 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31392 else if (!strcmp ("create", p))
31393 result = PRAGMA_OACC_CLAUSE_CREATE;
31394 break;
31395 case 'd':
31396 if (!strcmp ("defaultmap", p))
31397 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31398 else if (!strcmp ("depend", p))
31399 result = PRAGMA_OMP_CLAUSE_DEPEND;
31400 else if (!strcmp ("device", p))
31401 result = PRAGMA_OMP_CLAUSE_DEVICE;
31402 else if (!strcmp ("deviceptr", p))
31403 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31404 else if (!strcmp ("device_resident", p))
31405 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31406 else if (!strcmp ("dist_schedule", p))
31407 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31408 break;
31409 case 'f':
31410 if (!strcmp ("final", p))
31411 result = PRAGMA_OMP_CLAUSE_FINAL;
31412 else if (!strcmp ("finalize", p))
31413 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31414 else if (!strcmp ("firstprivate", p))
31415 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31416 else if (!strcmp ("from", p))
31417 result = PRAGMA_OMP_CLAUSE_FROM;
31418 break;
31419 case 'g':
31420 if (!strcmp ("gang", p))
31421 result = PRAGMA_OACC_CLAUSE_GANG;
31422 else if (!strcmp ("grainsize", p))
31423 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31424 break;
31425 case 'h':
31426 if (!strcmp ("hint", p))
31427 result = PRAGMA_OMP_CLAUSE_HINT;
31428 else if (!strcmp ("host", p))
31429 result = PRAGMA_OACC_CLAUSE_HOST;
31430 break;
31431 case 'i':
31432 if (!strcmp ("if_present", p))
31433 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31434 else if (!strcmp ("inbranch", p))
31435 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31436 else if (!strcmp ("independent", p))
31437 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31438 else if (!strcmp ("is_device_ptr", p))
31439 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31440 break;
31441 case 'l':
31442 if (!strcmp ("lastprivate", p))
31443 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31444 else if (!strcmp ("linear", p))
31445 result = PRAGMA_OMP_CLAUSE_LINEAR;
31446 else if (!strcmp ("link", p))
31447 result = PRAGMA_OMP_CLAUSE_LINK;
31448 break;
31449 case 'm':
31450 if (!strcmp ("map", p))
31451 result = PRAGMA_OMP_CLAUSE_MAP;
31452 else if (!strcmp ("mergeable", p))
31453 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31454 break;
31455 case 'n':
31456 if (!strcmp ("nogroup", p))
31457 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31458 else if (!strcmp ("notinbranch", p))
31459 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31460 else if (!strcmp ("nowait", p))
31461 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31462 else if (!strcmp ("num_gangs", p))
31463 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31464 else if (!strcmp ("num_tasks", p))
31465 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31466 else if (!strcmp ("num_teams", p))
31467 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31468 else if (!strcmp ("num_threads", p))
31469 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31470 else if (!strcmp ("num_workers", p))
31471 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31472 break;
31473 case 'o':
31474 if (!strcmp ("ordered", p))
31475 result = PRAGMA_OMP_CLAUSE_ORDERED;
31476 break;
31477 case 'p':
31478 if (!strcmp ("parallel", p))
31479 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31480 else if (!strcmp ("present", p))
31481 result = PRAGMA_OACC_CLAUSE_PRESENT;
31482 else if (!strcmp ("present_or_copy", p)
31483 || !strcmp ("pcopy", p))
31484 result = PRAGMA_OACC_CLAUSE_COPY;
31485 else if (!strcmp ("present_or_copyin", p)
31486 || !strcmp ("pcopyin", p))
31487 result = PRAGMA_OACC_CLAUSE_COPYIN;
31488 else if (!strcmp ("present_or_copyout", p)
31489 || !strcmp ("pcopyout", p))
31490 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31491 else if (!strcmp ("present_or_create", p)
31492 || !strcmp ("pcreate", p))
31493 result = PRAGMA_OACC_CLAUSE_CREATE;
31494 else if (!strcmp ("priority", p))
31495 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31496 else if (!strcmp ("proc_bind", p))
31497 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31498 break;
31499 case 'r':
31500 if (!strcmp ("reduction", p))
31501 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31502 break;
31503 case 's':
31504 if (!strcmp ("safelen", p))
31505 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31506 else if (!strcmp ("schedule", p))
31507 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31508 else if (!strcmp ("sections", p))
31509 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31510 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31511 result = PRAGMA_OACC_CLAUSE_HOST;
31512 else if (!strcmp ("seq", p))
31513 result = PRAGMA_OACC_CLAUSE_SEQ;
31514 else if (!strcmp ("shared", p))
31515 result = PRAGMA_OMP_CLAUSE_SHARED;
31516 else if (!strcmp ("simd", p))
31517 result = PRAGMA_OMP_CLAUSE_SIMD;
31518 else if (!strcmp ("simdlen", p))
31519 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31520 break;
31521 case 't':
31522 if (!strcmp ("taskgroup", p))
31523 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31524 else if (!strcmp ("thread_limit", p))
31525 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31526 else if (!strcmp ("threads", p))
31527 result = PRAGMA_OMP_CLAUSE_THREADS;
31528 else if (!strcmp ("tile", p))
31529 result = PRAGMA_OACC_CLAUSE_TILE;
31530 else if (!strcmp ("to", p))
31531 result = PRAGMA_OMP_CLAUSE_TO;
31532 break;
31533 case 'u':
31534 if (!strcmp ("uniform", p))
31535 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31536 else if (!strcmp ("untied", p))
31537 result = PRAGMA_OMP_CLAUSE_UNTIED;
31538 else if (!strcmp ("use_device", p))
31539 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31540 else if (!strcmp ("use_device_ptr", p))
31541 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31542 break;
31543 case 'v':
31544 if (!strcmp ("vector", p))
31545 result = PRAGMA_OACC_CLAUSE_VECTOR;
31546 else if (!strcmp ("vector_length", p))
31547 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31548 break;
31549 case 'w':
31550 if (!strcmp ("wait", p))
31551 result = PRAGMA_OACC_CLAUSE_WAIT;
31552 else if (!strcmp ("worker", p))
31553 result = PRAGMA_OACC_CLAUSE_WORKER;
31554 break;
31558 if (result != PRAGMA_OMP_CLAUSE_NONE)
31559 cp_lexer_consume_token (parser->lexer);
31561 return result;
31564 /* Validate that a clause of the given type does not already exist. */
31566 static void
31567 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31568 const char *name, location_t location)
31570 tree c;
31572 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31573 if (OMP_CLAUSE_CODE (c) == code)
31575 error_at (location, "too many %qs clauses", name);
31576 break;
31580 /* OpenMP 2.5:
31581 variable-list:
31582 identifier
31583 variable-list , identifier
31585 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31586 colon). An opening parenthesis will have been consumed by the caller.
31588 If KIND is nonzero, create the appropriate node and install the decl
31589 in OMP_CLAUSE_DECL and add the node to the head of the list.
31591 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31592 return the list created.
31594 COLON can be NULL if only closing parenthesis should end the list,
31595 or pointer to bool which will receive false if the list is terminated
31596 by closing parenthesis or true if the list is terminated by colon. */
31598 static tree
31599 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31600 tree list, bool *colon)
31602 cp_token *token;
31603 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31604 if (colon)
31606 parser->colon_corrects_to_scope_p = false;
31607 *colon = false;
31609 while (1)
31611 tree name, decl;
31613 token = cp_lexer_peek_token (parser->lexer);
31614 if (kind != 0
31615 && current_class_ptr
31616 && cp_parser_is_keyword (token, RID_THIS))
31618 decl = finish_this_expr ();
31619 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31620 || CONVERT_EXPR_P (decl))
31621 decl = TREE_OPERAND (decl, 0);
31622 cp_lexer_consume_token (parser->lexer);
31624 else
31626 name = cp_parser_id_expression (parser, /*template_p=*/false,
31627 /*check_dependency_p=*/true,
31628 /*template_p=*/NULL,
31629 /*declarator_p=*/false,
31630 /*optional_p=*/false);
31631 if (name == error_mark_node)
31632 goto skip_comma;
31634 if (identifier_p (name))
31635 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31636 else
31637 decl = name;
31638 if (decl == error_mark_node)
31639 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31640 token->location);
31642 if (decl == error_mark_node)
31644 else if (kind != 0)
31646 switch (kind)
31648 case OMP_CLAUSE__CACHE_:
31649 /* The OpenACC cache directive explicitly only allows "array
31650 elements or subarrays". */
31651 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31653 error_at (token->location, "expected %<[%>");
31654 decl = error_mark_node;
31655 break;
31657 /* FALLTHROUGH. */
31658 case OMP_CLAUSE_MAP:
31659 case OMP_CLAUSE_FROM:
31660 case OMP_CLAUSE_TO:
31661 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31663 location_t loc
31664 = cp_lexer_peek_token (parser->lexer)->location;
31665 cp_id_kind idk = CP_ID_KIND_NONE;
31666 cp_lexer_consume_token (parser->lexer);
31667 decl = convert_from_reference (decl);
31668 decl
31669 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31670 decl, false,
31671 &idk, loc);
31673 /* FALLTHROUGH. */
31674 case OMP_CLAUSE_DEPEND:
31675 case OMP_CLAUSE_REDUCTION:
31676 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31678 tree low_bound = NULL_TREE, length = NULL_TREE;
31680 parser->colon_corrects_to_scope_p = false;
31681 cp_lexer_consume_token (parser->lexer);
31682 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31683 low_bound = cp_parser_expression (parser);
31684 if (!colon)
31685 parser->colon_corrects_to_scope_p
31686 = saved_colon_corrects_to_scope_p;
31687 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31688 length = integer_one_node;
31689 else
31691 /* Look for `:'. */
31692 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31693 goto skip_comma;
31694 if (!cp_lexer_next_token_is (parser->lexer,
31695 CPP_CLOSE_SQUARE))
31696 length = cp_parser_expression (parser);
31698 /* Look for the closing `]'. */
31699 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31700 RT_CLOSE_SQUARE))
31701 goto skip_comma;
31703 decl = tree_cons (low_bound, length, decl);
31705 break;
31706 default:
31707 break;
31710 tree u = build_omp_clause (token->location, kind);
31711 OMP_CLAUSE_DECL (u) = decl;
31712 OMP_CLAUSE_CHAIN (u) = list;
31713 list = u;
31715 else
31716 list = tree_cons (decl, NULL_TREE, list);
31718 get_comma:
31719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31720 break;
31721 cp_lexer_consume_token (parser->lexer);
31724 if (colon)
31725 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31727 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31729 *colon = true;
31730 cp_parser_require (parser, CPP_COLON, RT_COLON);
31731 return list;
31734 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31736 int ending;
31738 /* Try to resync to an unnested comma. Copied from
31739 cp_parser_parenthesized_expression_list. */
31740 skip_comma:
31741 if (colon)
31742 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31743 ending = cp_parser_skip_to_closing_parenthesis (parser,
31744 /*recovering=*/true,
31745 /*or_comma=*/true,
31746 /*consume_paren=*/true);
31747 if (ending < 0)
31748 goto get_comma;
31751 return list;
31754 /* Similarly, but expect leading and trailing parenthesis. This is a very
31755 common case for omp clauses. */
31757 static tree
31758 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31760 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31761 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31762 return list;
31765 /* OpenACC 2.0:
31766 copy ( variable-list )
31767 copyin ( variable-list )
31768 copyout ( variable-list )
31769 create ( variable-list )
31770 delete ( variable-list )
31771 present ( variable-list ) */
31773 static tree
31774 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31775 tree list)
31777 enum gomp_map_kind kind;
31778 switch (c_kind)
31780 case PRAGMA_OACC_CLAUSE_COPY:
31781 kind = GOMP_MAP_TOFROM;
31782 break;
31783 case PRAGMA_OACC_CLAUSE_COPYIN:
31784 kind = GOMP_MAP_TO;
31785 break;
31786 case PRAGMA_OACC_CLAUSE_COPYOUT:
31787 kind = GOMP_MAP_FROM;
31788 break;
31789 case PRAGMA_OACC_CLAUSE_CREATE:
31790 kind = GOMP_MAP_ALLOC;
31791 break;
31792 case PRAGMA_OACC_CLAUSE_DELETE:
31793 kind = GOMP_MAP_RELEASE;
31794 break;
31795 case PRAGMA_OACC_CLAUSE_DEVICE:
31796 kind = GOMP_MAP_FORCE_TO;
31797 break;
31798 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31799 kind = GOMP_MAP_DEVICE_RESIDENT;
31800 break;
31801 case PRAGMA_OACC_CLAUSE_HOST:
31802 kind = GOMP_MAP_FORCE_FROM;
31803 break;
31804 case PRAGMA_OACC_CLAUSE_LINK:
31805 kind = GOMP_MAP_LINK;
31806 break;
31807 case PRAGMA_OACC_CLAUSE_PRESENT:
31808 kind = GOMP_MAP_FORCE_PRESENT;
31809 break;
31810 default:
31811 gcc_unreachable ();
31813 tree nl, c;
31814 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31816 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31817 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31819 return nl;
31822 /* OpenACC 2.0:
31823 deviceptr ( variable-list ) */
31825 static tree
31826 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31828 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31829 tree vars, t;
31831 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31832 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31833 variable-list must only allow for pointer variables. */
31834 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31835 for (t = vars; t; t = TREE_CHAIN (t))
31837 tree v = TREE_PURPOSE (t);
31838 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31839 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31840 OMP_CLAUSE_DECL (u) = v;
31841 OMP_CLAUSE_CHAIN (u) = list;
31842 list = u;
31845 return list;
31848 /* OpenACC 2.5:
31849 auto
31850 finalize
31851 independent
31852 nohost
31853 seq */
31855 static tree
31856 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31857 enum omp_clause_code code,
31858 tree list, location_t location)
31860 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31861 tree c = build_omp_clause (location, code);
31862 OMP_CLAUSE_CHAIN (c) = list;
31863 return c;
31866 /* OpenACC:
31867 num_gangs ( expression )
31868 num_workers ( expression )
31869 vector_length ( expression ) */
31871 static tree
31872 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31873 const char *str, tree list)
31875 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31877 matching_parens parens;
31878 if (!parens.require_open (parser))
31879 return list;
31881 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31883 if (t == error_mark_node
31884 || !parens.require_close (parser))
31886 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31887 /*or_comma=*/false,
31888 /*consume_paren=*/true);
31889 return list;
31892 check_no_duplicate_clause (list, code, str, loc);
31894 tree c = build_omp_clause (loc, code);
31895 OMP_CLAUSE_OPERAND (c, 0) = t;
31896 OMP_CLAUSE_CHAIN (c) = list;
31897 return c;
31900 /* OpenACC:
31902 gang [( gang-arg-list )]
31903 worker [( [num:] int-expr )]
31904 vector [( [length:] int-expr )]
31906 where gang-arg is one of:
31908 [num:] int-expr
31909 static: size-expr
31911 and size-expr may be:
31914 int-expr
31917 static tree
31918 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31919 const char *str, tree list)
31921 const char *id = "num";
31922 cp_lexer *lexer = parser->lexer;
31923 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31924 location_t loc = cp_lexer_peek_token (lexer)->location;
31926 if (kind == OMP_CLAUSE_VECTOR)
31927 id = "length";
31929 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31931 matching_parens parens;
31932 parens.consume_open (parser);
31936 cp_token *next = cp_lexer_peek_token (lexer);
31937 int idx = 0;
31939 /* Gang static argument. */
31940 if (kind == OMP_CLAUSE_GANG
31941 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31943 cp_lexer_consume_token (lexer);
31945 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31946 goto cleanup_error;
31948 idx = 1;
31949 if (ops[idx] != NULL)
31951 cp_parser_error (parser, "too many %<static%> arguments");
31952 goto cleanup_error;
31955 /* Check for the '*' argument. */
31956 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31957 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31958 || cp_lexer_nth_token_is (parser->lexer, 2,
31959 CPP_CLOSE_PAREN)))
31961 cp_lexer_consume_token (lexer);
31962 ops[idx] = integer_minus_one_node;
31964 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31966 cp_lexer_consume_token (lexer);
31967 continue;
31969 else break;
31972 /* Worker num: argument and vector length: arguments. */
31973 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31974 && id_equal (next->u.value, id)
31975 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31977 cp_lexer_consume_token (lexer); /* id */
31978 cp_lexer_consume_token (lexer); /* ':' */
31981 /* Now collect the actual argument. */
31982 if (ops[idx] != NULL_TREE)
31984 cp_parser_error (parser, "unexpected argument");
31985 goto cleanup_error;
31988 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31989 false);
31990 if (expr == error_mark_node)
31991 goto cleanup_error;
31993 mark_exp_read (expr);
31994 ops[idx] = expr;
31996 if (kind == OMP_CLAUSE_GANG
31997 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31999 cp_lexer_consume_token (lexer);
32000 continue;
32002 break;
32004 while (1);
32006 if (!parens.require_close (parser))
32007 goto cleanup_error;
32010 check_no_duplicate_clause (list, kind, str, loc);
32012 c = build_omp_clause (loc, kind);
32014 if (ops[1])
32015 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32017 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32018 OMP_CLAUSE_CHAIN (c) = list;
32020 return c;
32022 cleanup_error:
32023 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32024 return list;
32027 /* OpenACC 2.0:
32028 tile ( size-expr-list ) */
32030 static tree
32031 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32033 tree c, expr = error_mark_node;
32034 tree tile = NULL_TREE;
32036 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32037 so, but the spec authors never considered such a case and have
32038 differing opinions on what it might mean, including 'not
32039 allowed'.) */
32040 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32041 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32042 clause_loc);
32044 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32045 return list;
32049 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32050 return list;
32052 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32053 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32054 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32056 cp_lexer_consume_token (parser->lexer);
32057 expr = integer_zero_node;
32059 else
32060 expr = cp_parser_constant_expression (parser);
32062 tile = tree_cons (NULL_TREE, expr, tile);
32064 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32066 /* Consume the trailing ')'. */
32067 cp_lexer_consume_token (parser->lexer);
32069 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32070 tile = nreverse (tile);
32071 OMP_CLAUSE_TILE_LIST (c) = tile;
32072 OMP_CLAUSE_CHAIN (c) = list;
32073 return c;
32076 /* OpenACC 2.0
32077 Parse wait clause or directive parameters. */
32079 static tree
32080 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32082 vec<tree, va_gc> *args;
32083 tree t, args_tree;
32085 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32086 /*cast_p=*/false,
32087 /*allow_expansion_p=*/true,
32088 /*non_constant_p=*/NULL);
32090 if (args == NULL || args->length () == 0)
32092 cp_parser_error (parser, "expected integer expression before ')'");
32093 if (args != NULL)
32094 release_tree_vector (args);
32095 return list;
32098 args_tree = build_tree_list_vec (args);
32100 release_tree_vector (args);
32102 for (t = args_tree; t; t = TREE_CHAIN (t))
32104 tree targ = TREE_VALUE (t);
32106 if (targ != error_mark_node)
32108 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32109 error ("%<wait%> expression must be integral");
32110 else
32112 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32114 targ = mark_rvalue_use (targ);
32115 OMP_CLAUSE_DECL (c) = targ;
32116 OMP_CLAUSE_CHAIN (c) = list;
32117 list = c;
32122 return list;
32125 /* OpenACC:
32126 wait ( int-expr-list ) */
32128 static tree
32129 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32131 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32133 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32134 return list;
32136 list = cp_parser_oacc_wait_list (parser, location, list);
32138 return list;
32141 /* OpenMP 3.0:
32142 collapse ( constant-expression ) */
32144 static tree
32145 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32147 tree c, num;
32148 location_t loc;
32149 HOST_WIDE_INT n;
32151 loc = cp_lexer_peek_token (parser->lexer)->location;
32152 matching_parens parens;
32153 if (!parens.require_open (parser))
32154 return list;
32156 num = cp_parser_constant_expression (parser);
32158 if (!parens.require_close (parser))
32159 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32160 /*or_comma=*/false,
32161 /*consume_paren=*/true);
32163 if (num == error_mark_node)
32164 return list;
32165 num = fold_non_dependent_expr (num);
32166 if (!tree_fits_shwi_p (num)
32167 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32168 || (n = tree_to_shwi (num)) <= 0
32169 || (int) n != n)
32171 error_at (loc, "collapse argument needs positive constant integer expression");
32172 return list;
32175 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32176 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32177 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32178 OMP_CLAUSE_CHAIN (c) = list;
32179 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32181 return c;
32184 /* OpenMP 2.5:
32185 default ( none | shared )
32187 OpenACC:
32188 default ( none | present ) */
32190 static tree
32191 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32192 location_t location, bool is_oacc)
32194 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32195 tree c;
32197 matching_parens parens;
32198 if (!parens.require_open (parser))
32199 return list;
32200 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32202 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32203 const char *p = IDENTIFIER_POINTER (id);
32205 switch (p[0])
32207 case 'n':
32208 if (strcmp ("none", p) != 0)
32209 goto invalid_kind;
32210 kind = OMP_CLAUSE_DEFAULT_NONE;
32211 break;
32213 case 'p':
32214 if (strcmp ("present", p) != 0 || !is_oacc)
32215 goto invalid_kind;
32216 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32217 break;
32219 case 's':
32220 if (strcmp ("shared", p) != 0 || is_oacc)
32221 goto invalid_kind;
32222 kind = OMP_CLAUSE_DEFAULT_SHARED;
32223 break;
32225 default:
32226 goto invalid_kind;
32229 cp_lexer_consume_token (parser->lexer);
32231 else
32233 invalid_kind:
32234 if (is_oacc)
32235 cp_parser_error (parser, "expected %<none%> or %<present%>");
32236 else
32237 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32240 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32241 || !parens.require_close (parser))
32242 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32243 /*or_comma=*/false,
32244 /*consume_paren=*/true);
32246 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32247 return list;
32249 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32250 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32251 OMP_CLAUSE_CHAIN (c) = list;
32252 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32254 return c;
32257 /* OpenMP 3.1:
32258 final ( expression ) */
32260 static tree
32261 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32263 tree t, c;
32265 matching_parens parens;
32266 if (!parens.require_open (parser))
32267 return list;
32269 t = cp_parser_condition (parser);
32271 if (t == error_mark_node
32272 || !parens.require_close (parser))
32273 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32274 /*or_comma=*/false,
32275 /*consume_paren=*/true);
32277 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32279 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32280 OMP_CLAUSE_FINAL_EXPR (c) = t;
32281 OMP_CLAUSE_CHAIN (c) = list;
32283 return c;
32286 /* OpenMP 2.5:
32287 if ( expression )
32289 OpenMP 4.5:
32290 if ( directive-name-modifier : expression )
32292 directive-name-modifier:
32293 parallel | task | taskloop | target data | target | target update
32294 | target enter data | target exit data */
32296 static tree
32297 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32298 bool is_omp)
32300 tree t, c;
32301 enum tree_code if_modifier = ERROR_MARK;
32303 matching_parens parens;
32304 if (!parens.require_open (parser))
32305 return list;
32307 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32309 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32310 const char *p = IDENTIFIER_POINTER (id);
32311 int n = 2;
32313 if (strcmp ("parallel", p) == 0)
32314 if_modifier = OMP_PARALLEL;
32315 else if (strcmp ("task", p) == 0)
32316 if_modifier = OMP_TASK;
32317 else if (strcmp ("taskloop", p) == 0)
32318 if_modifier = OMP_TASKLOOP;
32319 else if (strcmp ("target", p) == 0)
32321 if_modifier = OMP_TARGET;
32322 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32324 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32325 p = IDENTIFIER_POINTER (id);
32326 if (strcmp ("data", p) == 0)
32327 if_modifier = OMP_TARGET_DATA;
32328 else if (strcmp ("update", p) == 0)
32329 if_modifier = OMP_TARGET_UPDATE;
32330 else if (strcmp ("enter", p) == 0)
32331 if_modifier = OMP_TARGET_ENTER_DATA;
32332 else if (strcmp ("exit", p) == 0)
32333 if_modifier = OMP_TARGET_EXIT_DATA;
32334 if (if_modifier != OMP_TARGET)
32335 n = 3;
32336 else
32338 location_t loc
32339 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32340 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32341 "or %<exit%>");
32342 if_modifier = ERROR_MARK;
32344 if (if_modifier == OMP_TARGET_ENTER_DATA
32345 || if_modifier == OMP_TARGET_EXIT_DATA)
32347 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32349 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32350 p = IDENTIFIER_POINTER (id);
32351 if (strcmp ("data", p) == 0)
32352 n = 4;
32354 if (n != 4)
32356 location_t loc
32357 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32358 error_at (loc, "expected %<data%>");
32359 if_modifier = ERROR_MARK;
32364 if (if_modifier != ERROR_MARK)
32366 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32368 while (n-- > 0)
32369 cp_lexer_consume_token (parser->lexer);
32371 else
32373 if (n > 2)
32375 location_t loc
32376 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32377 error_at (loc, "expected %<:%>");
32379 if_modifier = ERROR_MARK;
32384 t = cp_parser_condition (parser);
32386 if (t == error_mark_node
32387 || !parens.require_close (parser))
32388 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32389 /*or_comma=*/false,
32390 /*consume_paren=*/true);
32392 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32393 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32395 if (if_modifier != ERROR_MARK
32396 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32398 const char *p = NULL;
32399 switch (if_modifier)
32401 case OMP_PARALLEL: p = "parallel"; break;
32402 case OMP_TASK: p = "task"; break;
32403 case OMP_TASKLOOP: p = "taskloop"; break;
32404 case OMP_TARGET_DATA: p = "target data"; break;
32405 case OMP_TARGET: p = "target"; break;
32406 case OMP_TARGET_UPDATE: p = "target update"; break;
32407 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32408 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32409 default: gcc_unreachable ();
32411 error_at (location, "too many %<if%> clauses with %qs modifier",
32413 return list;
32415 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32417 if (!is_omp)
32418 error_at (location, "too many %<if%> clauses");
32419 else
32420 error_at (location, "too many %<if%> clauses without modifier");
32421 return list;
32423 else if (if_modifier == ERROR_MARK
32424 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32426 error_at (location, "if any %<if%> clause has modifier, then all "
32427 "%<if%> clauses have to use modifier");
32428 return list;
32432 c = build_omp_clause (location, OMP_CLAUSE_IF);
32433 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32434 OMP_CLAUSE_IF_EXPR (c) = t;
32435 OMP_CLAUSE_CHAIN (c) = list;
32437 return c;
32440 /* OpenMP 3.1:
32441 mergeable */
32443 static tree
32444 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32445 tree list, location_t location)
32447 tree c;
32449 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32450 location);
32452 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32453 OMP_CLAUSE_CHAIN (c) = list;
32454 return c;
32457 /* OpenMP 2.5:
32458 nowait */
32460 static tree
32461 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32462 tree list, location_t location)
32464 tree c;
32466 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32468 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32469 OMP_CLAUSE_CHAIN (c) = list;
32470 return c;
32473 /* OpenMP 2.5:
32474 num_threads ( expression ) */
32476 static tree
32477 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32478 location_t location)
32480 tree t, c;
32482 matching_parens parens;
32483 if (!parens.require_open (parser))
32484 return list;
32486 t = cp_parser_expression (parser);
32488 if (t == error_mark_node
32489 || !parens.require_close (parser))
32490 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32491 /*or_comma=*/false,
32492 /*consume_paren=*/true);
32494 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32495 "num_threads", location);
32497 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32498 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32499 OMP_CLAUSE_CHAIN (c) = list;
32501 return c;
32504 /* OpenMP 4.5:
32505 num_tasks ( expression ) */
32507 static tree
32508 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32509 location_t location)
32511 tree t, c;
32513 matching_parens parens;
32514 if (!parens.require_open (parser))
32515 return list;
32517 t = cp_parser_expression (parser);
32519 if (t == error_mark_node
32520 || !parens.require_close (parser))
32521 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32522 /*or_comma=*/false,
32523 /*consume_paren=*/true);
32525 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32526 "num_tasks", location);
32528 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32529 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32530 OMP_CLAUSE_CHAIN (c) = list;
32532 return c;
32535 /* OpenMP 4.5:
32536 grainsize ( expression ) */
32538 static tree
32539 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32540 location_t location)
32542 tree t, c;
32544 matching_parens parens;
32545 if (!parens.require_open (parser))
32546 return list;
32548 t = cp_parser_expression (parser);
32550 if (t == error_mark_node
32551 || !parens.require_close (parser))
32552 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32553 /*or_comma=*/false,
32554 /*consume_paren=*/true);
32556 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32557 "grainsize", location);
32559 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32560 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32561 OMP_CLAUSE_CHAIN (c) = list;
32563 return c;
32566 /* OpenMP 4.5:
32567 priority ( expression ) */
32569 static tree
32570 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32571 location_t location)
32573 tree t, c;
32575 matching_parens parens;
32576 if (!parens.require_open (parser))
32577 return list;
32579 t = cp_parser_expression (parser);
32581 if (t == error_mark_node
32582 || !parens.require_close (parser))
32583 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32584 /*or_comma=*/false,
32585 /*consume_paren=*/true);
32587 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32588 "priority", location);
32590 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32591 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32592 OMP_CLAUSE_CHAIN (c) = list;
32594 return c;
32597 /* OpenMP 4.5:
32598 hint ( expression ) */
32600 static tree
32601 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32602 location_t location)
32604 tree t, c;
32606 matching_parens parens;
32607 if (!parens.require_open (parser))
32608 return list;
32610 t = cp_parser_expression (parser);
32612 if (t == error_mark_node
32613 || !parens.require_close (parser))
32614 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32615 /*or_comma=*/false,
32616 /*consume_paren=*/true);
32618 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32620 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32621 OMP_CLAUSE_HINT_EXPR (c) = t;
32622 OMP_CLAUSE_CHAIN (c) = list;
32624 return c;
32627 /* OpenMP 4.5:
32628 defaultmap ( tofrom : scalar ) */
32630 static tree
32631 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32632 location_t location)
32634 tree c, id;
32635 const char *p;
32637 matching_parens parens;
32638 if (!parens.require_open (parser))
32639 return list;
32641 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32643 cp_parser_error (parser, "expected %<tofrom%>");
32644 goto out_err;
32646 id = cp_lexer_peek_token (parser->lexer)->u.value;
32647 p = IDENTIFIER_POINTER (id);
32648 if (strcmp (p, "tofrom") != 0)
32650 cp_parser_error (parser, "expected %<tofrom%>");
32651 goto out_err;
32653 cp_lexer_consume_token (parser->lexer);
32654 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32655 goto out_err;
32657 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32659 cp_parser_error (parser, "expected %<scalar%>");
32660 goto out_err;
32662 id = cp_lexer_peek_token (parser->lexer)->u.value;
32663 p = IDENTIFIER_POINTER (id);
32664 if (strcmp (p, "scalar") != 0)
32666 cp_parser_error (parser, "expected %<scalar%>");
32667 goto out_err;
32669 cp_lexer_consume_token (parser->lexer);
32670 if (!parens.require_close (parser))
32671 goto out_err;
32673 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32674 location);
32676 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32677 OMP_CLAUSE_CHAIN (c) = list;
32678 return c;
32680 out_err:
32681 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32682 /*or_comma=*/false,
32683 /*consume_paren=*/true);
32684 return list;
32687 /* OpenMP 2.5:
32688 ordered
32690 OpenMP 4.5:
32691 ordered ( constant-expression ) */
32693 static tree
32694 cp_parser_omp_clause_ordered (cp_parser *parser,
32695 tree list, location_t location)
32697 tree c, num = NULL_TREE;
32698 HOST_WIDE_INT n;
32700 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32701 "ordered", location);
32703 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32705 matching_parens parens;
32706 parens.consume_open (parser);
32708 num = cp_parser_constant_expression (parser);
32710 if (!parens.require_close (parser))
32711 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32712 /*or_comma=*/false,
32713 /*consume_paren=*/true);
32715 if (num == error_mark_node)
32716 return list;
32717 num = fold_non_dependent_expr (num);
32718 if (!tree_fits_shwi_p (num)
32719 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32720 || (n = tree_to_shwi (num)) <= 0
32721 || (int) n != n)
32723 error_at (location,
32724 "ordered argument needs positive constant integer "
32725 "expression");
32726 return list;
32730 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32731 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32732 OMP_CLAUSE_CHAIN (c) = list;
32733 return c;
32736 /* OpenMP 2.5:
32737 reduction ( reduction-operator : variable-list )
32739 reduction-operator:
32740 One of: + * - & ^ | && ||
32742 OpenMP 3.1:
32744 reduction-operator:
32745 One of: + * - & ^ | && || min max
32747 OpenMP 4.0:
32749 reduction-operator:
32750 One of: + * - & ^ | && ||
32751 id-expression */
32753 static tree
32754 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32756 enum tree_code code = ERROR_MARK;
32757 tree nlist, c, id = NULL_TREE;
32759 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32760 return list;
32762 switch (cp_lexer_peek_token (parser->lexer)->type)
32764 case CPP_PLUS: code = PLUS_EXPR; break;
32765 case CPP_MULT: code = MULT_EXPR; break;
32766 case CPP_MINUS: code = MINUS_EXPR; break;
32767 case CPP_AND: code = BIT_AND_EXPR; break;
32768 case CPP_XOR: code = BIT_XOR_EXPR; break;
32769 case CPP_OR: code = BIT_IOR_EXPR; break;
32770 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32771 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32772 default: break;
32775 if (code != ERROR_MARK)
32776 cp_lexer_consume_token (parser->lexer);
32777 else
32779 bool saved_colon_corrects_to_scope_p;
32780 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32781 parser->colon_corrects_to_scope_p = false;
32782 id = cp_parser_id_expression (parser, /*template_p=*/false,
32783 /*check_dependency_p=*/true,
32784 /*template_p=*/NULL,
32785 /*declarator_p=*/false,
32786 /*optional_p=*/false);
32787 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32788 if (identifier_p (id))
32790 const char *p = IDENTIFIER_POINTER (id);
32792 if (strcmp (p, "min") == 0)
32793 code = MIN_EXPR;
32794 else if (strcmp (p, "max") == 0)
32795 code = MAX_EXPR;
32796 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32797 code = PLUS_EXPR;
32798 else if (id == ovl_op_identifier (false, MULT_EXPR))
32799 code = MULT_EXPR;
32800 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32801 code = MINUS_EXPR;
32802 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32803 code = BIT_AND_EXPR;
32804 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32805 code = BIT_IOR_EXPR;
32806 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32807 code = BIT_XOR_EXPR;
32808 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32809 code = TRUTH_ANDIF_EXPR;
32810 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32811 code = TRUTH_ORIF_EXPR;
32812 id = omp_reduction_id (code, id, NULL_TREE);
32813 tree scope = parser->scope;
32814 if (scope)
32815 id = build_qualified_name (NULL_TREE, scope, id, false);
32816 parser->scope = NULL_TREE;
32817 parser->qualifying_scope = NULL_TREE;
32818 parser->object_scope = NULL_TREE;
32820 else
32822 error ("invalid reduction-identifier");
32823 resync_fail:
32824 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32825 /*or_comma=*/false,
32826 /*consume_paren=*/true);
32827 return list;
32831 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32832 goto resync_fail;
32834 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32835 NULL);
32836 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32838 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32839 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32842 return nlist;
32845 /* OpenMP 2.5:
32846 schedule ( schedule-kind )
32847 schedule ( schedule-kind , expression )
32849 schedule-kind:
32850 static | dynamic | guided | runtime | auto
32852 OpenMP 4.5:
32853 schedule ( schedule-modifier : schedule-kind )
32854 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32856 schedule-modifier:
32857 simd
32858 monotonic
32859 nonmonotonic */
32861 static tree
32862 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32864 tree c, t;
32865 int modifiers = 0, nmodifiers = 0;
32867 matching_parens parens;
32868 if (!parens.require_open (parser))
32869 return list;
32871 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32873 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32875 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32876 const char *p = IDENTIFIER_POINTER (id);
32877 if (strcmp ("simd", p) == 0)
32878 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32879 else if (strcmp ("monotonic", p) == 0)
32880 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32881 else if (strcmp ("nonmonotonic", p) == 0)
32882 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32883 else
32884 break;
32885 cp_lexer_consume_token (parser->lexer);
32886 if (nmodifiers++ == 0
32887 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32888 cp_lexer_consume_token (parser->lexer);
32889 else
32891 cp_parser_require (parser, CPP_COLON, RT_COLON);
32892 break;
32896 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32898 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32899 const char *p = IDENTIFIER_POINTER (id);
32901 switch (p[0])
32903 case 'd':
32904 if (strcmp ("dynamic", p) != 0)
32905 goto invalid_kind;
32906 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32907 break;
32909 case 'g':
32910 if (strcmp ("guided", p) != 0)
32911 goto invalid_kind;
32912 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32913 break;
32915 case 'r':
32916 if (strcmp ("runtime", p) != 0)
32917 goto invalid_kind;
32918 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32919 break;
32921 default:
32922 goto invalid_kind;
32925 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32926 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32927 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32928 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32929 else
32930 goto invalid_kind;
32931 cp_lexer_consume_token (parser->lexer);
32933 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32934 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32935 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32936 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32938 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32939 "specified");
32940 modifiers = 0;
32943 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32945 cp_token *token;
32946 cp_lexer_consume_token (parser->lexer);
32948 token = cp_lexer_peek_token (parser->lexer);
32949 t = cp_parser_assignment_expression (parser);
32951 if (t == error_mark_node)
32952 goto resync_fail;
32953 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32954 error_at (token->location, "schedule %<runtime%> does not take "
32955 "a %<chunk_size%> parameter");
32956 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32957 error_at (token->location, "schedule %<auto%> does not take "
32958 "a %<chunk_size%> parameter");
32959 else
32960 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32962 if (!parens.require_close (parser))
32963 goto resync_fail;
32965 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32966 goto resync_fail;
32968 OMP_CLAUSE_SCHEDULE_KIND (c)
32969 = (enum omp_clause_schedule_kind)
32970 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32972 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32973 OMP_CLAUSE_CHAIN (c) = list;
32974 return c;
32976 invalid_kind:
32977 cp_parser_error (parser, "invalid schedule kind");
32978 resync_fail:
32979 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32980 /*or_comma=*/false,
32981 /*consume_paren=*/true);
32982 return list;
32985 /* OpenMP 3.0:
32986 untied */
32988 static tree
32989 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32990 tree list, location_t location)
32992 tree c;
32994 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32996 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32997 OMP_CLAUSE_CHAIN (c) = list;
32998 return c;
33001 /* OpenMP 4.0:
33002 inbranch
33003 notinbranch */
33005 static tree
33006 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33007 tree list, location_t location)
33009 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33010 tree c = build_omp_clause (location, code);
33011 OMP_CLAUSE_CHAIN (c) = list;
33012 return c;
33015 /* OpenMP 4.0:
33016 parallel
33018 sections
33019 taskgroup */
33021 static tree
33022 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33023 enum omp_clause_code code,
33024 tree list, location_t location)
33026 tree c = build_omp_clause (location, code);
33027 OMP_CLAUSE_CHAIN (c) = list;
33028 return c;
33031 /* OpenMP 4.5:
33032 nogroup */
33034 static tree
33035 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33036 tree list, location_t location)
33038 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33039 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33040 OMP_CLAUSE_CHAIN (c) = list;
33041 return c;
33044 /* OpenMP 4.5:
33045 simd
33046 threads */
33048 static tree
33049 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33050 enum omp_clause_code code,
33051 tree list, location_t location)
33053 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33054 tree c = build_omp_clause (location, code);
33055 OMP_CLAUSE_CHAIN (c) = list;
33056 return c;
33059 /* OpenMP 4.0:
33060 num_teams ( expression ) */
33062 static tree
33063 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33064 location_t location)
33066 tree t, c;
33068 matching_parens parens;
33069 if (!parens.require_open (parser))
33070 return list;
33072 t = cp_parser_expression (parser);
33074 if (t == error_mark_node
33075 || !parens.require_close (parser))
33076 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33077 /*or_comma=*/false,
33078 /*consume_paren=*/true);
33080 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33081 "num_teams", location);
33083 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33084 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33085 OMP_CLAUSE_CHAIN (c) = list;
33087 return c;
33090 /* OpenMP 4.0:
33091 thread_limit ( expression ) */
33093 static tree
33094 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33095 location_t location)
33097 tree t, c;
33099 matching_parens parens;
33100 if (!parens.require_open (parser))
33101 return list;
33103 t = cp_parser_expression (parser);
33105 if (t == error_mark_node
33106 || !parens.require_close (parser))
33107 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33108 /*or_comma=*/false,
33109 /*consume_paren=*/true);
33111 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33112 "thread_limit", location);
33114 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33115 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33116 OMP_CLAUSE_CHAIN (c) = list;
33118 return c;
33121 /* OpenMP 4.0:
33122 aligned ( variable-list )
33123 aligned ( variable-list : constant-expression ) */
33125 static tree
33126 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33128 tree nlist, c, alignment = NULL_TREE;
33129 bool colon;
33131 matching_parens parens;
33132 if (!parens.require_open (parser))
33133 return list;
33135 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33136 &colon);
33138 if (colon)
33140 alignment = cp_parser_constant_expression (parser);
33142 if (!parens.require_close (parser))
33143 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33144 /*or_comma=*/false,
33145 /*consume_paren=*/true);
33147 if (alignment == error_mark_node)
33148 alignment = NULL_TREE;
33151 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33152 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33154 return nlist;
33157 /* OpenMP 4.0:
33158 linear ( variable-list )
33159 linear ( variable-list : expression )
33161 OpenMP 4.5:
33162 linear ( modifier ( variable-list ) )
33163 linear ( modifier ( variable-list ) : expression ) */
33165 static tree
33166 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33167 bool declare_simd)
33169 tree nlist, c, step = integer_one_node;
33170 bool colon;
33171 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33173 matching_parens parens;
33174 if (!parens.require_open (parser))
33175 return list;
33177 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33179 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33180 const char *p = IDENTIFIER_POINTER (id);
33182 if (strcmp ("ref", p) == 0)
33183 kind = OMP_CLAUSE_LINEAR_REF;
33184 else if (strcmp ("val", p) == 0)
33185 kind = OMP_CLAUSE_LINEAR_VAL;
33186 else if (strcmp ("uval", p) == 0)
33187 kind = OMP_CLAUSE_LINEAR_UVAL;
33188 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33189 cp_lexer_consume_token (parser->lexer);
33190 else
33191 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33194 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33195 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33196 &colon);
33197 else
33199 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33200 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33201 if (colon)
33202 cp_parser_require (parser, CPP_COLON, RT_COLON);
33203 else if (!parens.require_close (parser))
33204 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33205 /*or_comma=*/false,
33206 /*consume_paren=*/true);
33209 if (colon)
33211 step = NULL_TREE;
33212 if (declare_simd
33213 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33214 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33216 cp_token *token = cp_lexer_peek_token (parser->lexer);
33217 cp_parser_parse_tentatively (parser);
33218 step = cp_parser_id_expression (parser, /*template_p=*/false,
33219 /*check_dependency_p=*/true,
33220 /*template_p=*/NULL,
33221 /*declarator_p=*/false,
33222 /*optional_p=*/false);
33223 if (step != error_mark_node)
33224 step = cp_parser_lookup_name_simple (parser, step, token->location);
33225 if (step == error_mark_node)
33227 step = NULL_TREE;
33228 cp_parser_abort_tentative_parse (parser);
33230 else if (!cp_parser_parse_definitely (parser))
33231 step = NULL_TREE;
33233 if (!step)
33234 step = cp_parser_expression (parser);
33236 if (!parens.require_close (parser))
33237 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33238 /*or_comma=*/false,
33239 /*consume_paren=*/true);
33241 if (step == error_mark_node)
33242 return list;
33245 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33247 OMP_CLAUSE_LINEAR_STEP (c) = step;
33248 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33251 return nlist;
33254 /* OpenMP 4.0:
33255 safelen ( constant-expression ) */
33257 static tree
33258 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33259 location_t location)
33261 tree t, c;
33263 matching_parens parens;
33264 if (!parens.require_open (parser))
33265 return list;
33267 t = cp_parser_constant_expression (parser);
33269 if (t == error_mark_node
33270 || !parens.require_close (parser))
33271 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33272 /*or_comma=*/false,
33273 /*consume_paren=*/true);
33275 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33277 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33278 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33279 OMP_CLAUSE_CHAIN (c) = list;
33281 return c;
33284 /* OpenMP 4.0:
33285 simdlen ( constant-expression ) */
33287 static tree
33288 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33289 location_t location)
33291 tree t, c;
33293 matching_parens parens;
33294 if (!parens.require_open (parser))
33295 return list;
33297 t = cp_parser_constant_expression (parser);
33299 if (t == error_mark_node
33300 || !parens.require_close (parser))
33301 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33302 /*or_comma=*/false,
33303 /*consume_paren=*/true);
33305 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33307 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33308 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33309 OMP_CLAUSE_CHAIN (c) = list;
33311 return c;
33314 /* OpenMP 4.5:
33315 vec:
33316 identifier [+/- integer]
33317 vec , identifier [+/- integer]
33320 static tree
33321 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33322 tree list)
33324 tree vec = NULL;
33326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33328 cp_parser_error (parser, "expected identifier");
33329 return list;
33332 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33334 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33335 tree t, identifier = cp_parser_identifier (parser);
33336 tree addend = NULL;
33338 if (identifier == error_mark_node)
33339 t = error_mark_node;
33340 else
33342 t = cp_parser_lookup_name_simple
33343 (parser, identifier,
33344 cp_lexer_peek_token (parser->lexer)->location);
33345 if (t == error_mark_node)
33346 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33347 id_loc);
33350 bool neg = false;
33351 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33352 neg = true;
33353 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33355 addend = integer_zero_node;
33356 goto add_to_vector;
33358 cp_lexer_consume_token (parser->lexer);
33360 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33362 cp_parser_error (parser, "expected integer");
33363 return list;
33366 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33367 if (TREE_CODE (addend) != INTEGER_CST)
33369 cp_parser_error (parser, "expected integer");
33370 return list;
33372 cp_lexer_consume_token (parser->lexer);
33374 add_to_vector:
33375 if (t != error_mark_node)
33377 vec = tree_cons (addend, t, vec);
33378 if (neg)
33379 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33382 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33383 break;
33385 cp_lexer_consume_token (parser->lexer);
33388 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33390 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33391 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33392 OMP_CLAUSE_DECL (u) = nreverse (vec);
33393 OMP_CLAUSE_CHAIN (u) = list;
33394 return u;
33396 return list;
33399 /* OpenMP 4.0:
33400 depend ( depend-kind : variable-list )
33402 depend-kind:
33403 in | out | inout
33405 OpenMP 4.5:
33406 depend ( source )
33408 depend ( sink : vec ) */
33410 static tree
33411 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33413 tree nlist, c;
33414 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33416 matching_parens parens;
33417 if (!parens.require_open (parser))
33418 return list;
33420 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33422 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33423 const char *p = IDENTIFIER_POINTER (id);
33425 if (strcmp ("in", p) == 0)
33426 kind = OMP_CLAUSE_DEPEND_IN;
33427 else if (strcmp ("inout", p) == 0)
33428 kind = OMP_CLAUSE_DEPEND_INOUT;
33429 else if (strcmp ("out", p) == 0)
33430 kind = OMP_CLAUSE_DEPEND_OUT;
33431 else if (strcmp ("source", p) == 0)
33432 kind = OMP_CLAUSE_DEPEND_SOURCE;
33433 else if (strcmp ("sink", p) == 0)
33434 kind = OMP_CLAUSE_DEPEND_SINK;
33435 else
33436 goto invalid_kind;
33438 else
33439 goto invalid_kind;
33441 cp_lexer_consume_token (parser->lexer);
33443 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33445 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33446 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33447 OMP_CLAUSE_DECL (c) = NULL_TREE;
33448 OMP_CLAUSE_CHAIN (c) = list;
33449 if (!parens.require_close (parser))
33450 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33451 /*or_comma=*/false,
33452 /*consume_paren=*/true);
33453 return c;
33456 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33457 goto resync_fail;
33459 if (kind == OMP_CLAUSE_DEPEND_SINK)
33460 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33461 else
33463 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33464 list, NULL);
33466 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33467 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33469 return nlist;
33471 invalid_kind:
33472 cp_parser_error (parser, "invalid depend kind");
33473 resync_fail:
33474 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33475 /*or_comma=*/false,
33476 /*consume_paren=*/true);
33477 return list;
33480 /* OpenMP 4.0:
33481 map ( map-kind : variable-list )
33482 map ( variable-list )
33484 map-kind:
33485 alloc | to | from | tofrom
33487 OpenMP 4.5:
33488 map-kind:
33489 alloc | to | from | tofrom | release | delete
33491 map ( always [,] map-kind: variable-list ) */
33493 static tree
33494 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33496 tree nlist, c;
33497 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33498 bool always = false;
33500 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33501 return list;
33503 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33505 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33506 const char *p = IDENTIFIER_POINTER (id);
33508 if (strcmp ("always", p) == 0)
33510 int nth = 2;
33511 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33512 nth++;
33513 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33514 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33515 == RID_DELETE))
33516 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33517 == CPP_COLON))
33519 always = true;
33520 cp_lexer_consume_token (parser->lexer);
33521 if (nth == 3)
33522 cp_lexer_consume_token (parser->lexer);
33527 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33528 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33530 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33531 const char *p = IDENTIFIER_POINTER (id);
33533 if (strcmp ("alloc", p) == 0)
33534 kind = GOMP_MAP_ALLOC;
33535 else if (strcmp ("to", p) == 0)
33536 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33537 else if (strcmp ("from", p) == 0)
33538 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33539 else if (strcmp ("tofrom", p) == 0)
33540 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33541 else if (strcmp ("release", p) == 0)
33542 kind = GOMP_MAP_RELEASE;
33543 else
33545 cp_parser_error (parser, "invalid map kind");
33546 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33547 /*or_comma=*/false,
33548 /*consume_paren=*/true);
33549 return list;
33551 cp_lexer_consume_token (parser->lexer);
33552 cp_lexer_consume_token (parser->lexer);
33554 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33555 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33557 kind = GOMP_MAP_DELETE;
33558 cp_lexer_consume_token (parser->lexer);
33559 cp_lexer_consume_token (parser->lexer);
33562 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33563 NULL);
33565 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33566 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33568 return nlist;
33571 /* OpenMP 4.0:
33572 device ( expression ) */
33574 static tree
33575 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33576 location_t location)
33578 tree t, c;
33580 matching_parens parens;
33581 if (!parens.require_open (parser))
33582 return list;
33584 t = cp_parser_expression (parser);
33586 if (t == error_mark_node
33587 || !parens.require_close (parser))
33588 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33589 /*or_comma=*/false,
33590 /*consume_paren=*/true);
33592 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33593 "device", location);
33595 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33596 OMP_CLAUSE_DEVICE_ID (c) = t;
33597 OMP_CLAUSE_CHAIN (c) = list;
33599 return c;
33602 /* OpenMP 4.0:
33603 dist_schedule ( static )
33604 dist_schedule ( static , expression ) */
33606 static tree
33607 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33608 location_t location)
33610 tree c, t;
33612 matching_parens parens;
33613 if (!parens.require_open (parser))
33614 return list;
33616 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33618 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33619 goto invalid_kind;
33620 cp_lexer_consume_token (parser->lexer);
33622 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33624 cp_lexer_consume_token (parser->lexer);
33626 t = cp_parser_assignment_expression (parser);
33628 if (t == error_mark_node)
33629 goto resync_fail;
33630 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33632 if (!parens.require_close (parser))
33633 goto resync_fail;
33635 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33636 goto resync_fail;
33638 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33639 location);
33640 OMP_CLAUSE_CHAIN (c) = list;
33641 return c;
33643 invalid_kind:
33644 cp_parser_error (parser, "invalid dist_schedule kind");
33645 resync_fail:
33646 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33647 /*or_comma=*/false,
33648 /*consume_paren=*/true);
33649 return list;
33652 /* OpenMP 4.0:
33653 proc_bind ( proc-bind-kind )
33655 proc-bind-kind:
33656 master | close | spread */
33658 static tree
33659 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33660 location_t location)
33662 tree c;
33663 enum omp_clause_proc_bind_kind kind;
33665 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33666 return list;
33668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33670 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33671 const char *p = IDENTIFIER_POINTER (id);
33673 if (strcmp ("master", p) == 0)
33674 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33675 else if (strcmp ("close", p) == 0)
33676 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33677 else if (strcmp ("spread", p) == 0)
33678 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33679 else
33680 goto invalid_kind;
33682 else
33683 goto invalid_kind;
33685 cp_lexer_consume_token (parser->lexer);
33686 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33687 goto resync_fail;
33689 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33690 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33691 location);
33692 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33693 OMP_CLAUSE_CHAIN (c) = list;
33694 return c;
33696 invalid_kind:
33697 cp_parser_error (parser, "invalid depend kind");
33698 resync_fail:
33699 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33700 /*or_comma=*/false,
33701 /*consume_paren=*/true);
33702 return list;
33705 /* OpenACC:
33706 async [( int-expr )] */
33708 static tree
33709 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33711 tree c, t;
33712 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33714 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33716 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33718 matching_parens parens;
33719 parens.consume_open (parser);
33721 t = cp_parser_expression (parser);
33722 if (t == error_mark_node
33723 || !parens.require_close (parser))
33724 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33725 /*or_comma=*/false,
33726 /*consume_paren=*/true);
33729 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33731 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33732 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33733 OMP_CLAUSE_CHAIN (c) = list;
33734 list = c;
33736 return list;
33739 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33740 is a bitmask in MASK. Return the list of clauses found. */
33742 static tree
33743 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33744 const char *where, cp_token *pragma_tok,
33745 bool finish_p = true)
33747 tree clauses = NULL;
33748 bool first = true;
33750 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33752 location_t here;
33753 pragma_omp_clause c_kind;
33754 omp_clause_code code;
33755 const char *c_name;
33756 tree prev = clauses;
33758 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33759 cp_lexer_consume_token (parser->lexer);
33761 here = cp_lexer_peek_token (parser->lexer)->location;
33762 c_kind = cp_parser_omp_clause_name (parser);
33764 switch (c_kind)
33766 case PRAGMA_OACC_CLAUSE_ASYNC:
33767 clauses = cp_parser_oacc_clause_async (parser, clauses);
33768 c_name = "async";
33769 break;
33770 case PRAGMA_OACC_CLAUSE_AUTO:
33771 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33772 clauses, here);
33773 c_name = "auto";
33774 break;
33775 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33776 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33777 c_name = "collapse";
33778 break;
33779 case PRAGMA_OACC_CLAUSE_COPY:
33780 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33781 c_name = "copy";
33782 break;
33783 case PRAGMA_OACC_CLAUSE_COPYIN:
33784 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33785 c_name = "copyin";
33786 break;
33787 case PRAGMA_OACC_CLAUSE_COPYOUT:
33788 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33789 c_name = "copyout";
33790 break;
33791 case PRAGMA_OACC_CLAUSE_CREATE:
33792 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33793 c_name = "create";
33794 break;
33795 case PRAGMA_OACC_CLAUSE_DELETE:
33796 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33797 c_name = "delete";
33798 break;
33799 case PRAGMA_OMP_CLAUSE_DEFAULT:
33800 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33801 c_name = "default";
33802 break;
33803 case PRAGMA_OACC_CLAUSE_DEVICE:
33804 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33805 c_name = "device";
33806 break;
33807 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33808 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33809 c_name = "deviceptr";
33810 break;
33811 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33812 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33813 c_name = "device_resident";
33814 break;
33815 case PRAGMA_OACC_CLAUSE_FINALIZE:
33816 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
33817 clauses, here);
33818 c_name = "finalize";
33819 break;
33820 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33821 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33822 clauses);
33823 c_name = "firstprivate";
33824 break;
33825 case PRAGMA_OACC_CLAUSE_GANG:
33826 c_name = "gang";
33827 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33828 c_name, clauses);
33829 break;
33830 case PRAGMA_OACC_CLAUSE_HOST:
33831 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33832 c_name = "host";
33833 break;
33834 case PRAGMA_OACC_CLAUSE_IF:
33835 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33836 c_name = "if";
33837 break;
33838 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
33839 clauses = cp_parser_oacc_simple_clause (parser,
33840 OMP_CLAUSE_IF_PRESENT,
33841 clauses, here);
33842 c_name = "if_present";
33843 break;
33844 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33845 clauses = cp_parser_oacc_simple_clause (parser,
33846 OMP_CLAUSE_INDEPENDENT,
33847 clauses, here);
33848 c_name = "independent";
33849 break;
33850 case PRAGMA_OACC_CLAUSE_LINK:
33851 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33852 c_name = "link";
33853 break;
33854 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33855 code = OMP_CLAUSE_NUM_GANGS;
33856 c_name = "num_gangs";
33857 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33858 clauses);
33859 break;
33860 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33861 c_name = "num_workers";
33862 code = OMP_CLAUSE_NUM_WORKERS;
33863 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33864 clauses);
33865 break;
33866 case PRAGMA_OACC_CLAUSE_PRESENT:
33867 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33868 c_name = "present";
33869 break;
33870 case PRAGMA_OACC_CLAUSE_PRIVATE:
33871 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33872 clauses);
33873 c_name = "private";
33874 break;
33875 case PRAGMA_OACC_CLAUSE_REDUCTION:
33876 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33877 c_name = "reduction";
33878 break;
33879 case PRAGMA_OACC_CLAUSE_SEQ:
33880 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33881 clauses, here);
33882 c_name = "seq";
33883 break;
33884 case PRAGMA_OACC_CLAUSE_TILE:
33885 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33886 c_name = "tile";
33887 break;
33888 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33889 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33890 clauses);
33891 c_name = "use_device";
33892 break;
33893 case PRAGMA_OACC_CLAUSE_VECTOR:
33894 c_name = "vector";
33895 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33896 c_name, clauses);
33897 break;
33898 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33899 c_name = "vector_length";
33900 code = OMP_CLAUSE_VECTOR_LENGTH;
33901 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33902 clauses);
33903 break;
33904 case PRAGMA_OACC_CLAUSE_WAIT:
33905 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33906 c_name = "wait";
33907 break;
33908 case PRAGMA_OACC_CLAUSE_WORKER:
33909 c_name = "worker";
33910 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33911 c_name, clauses);
33912 break;
33913 default:
33914 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33915 goto saw_error;
33918 first = false;
33920 if (((mask >> c_kind) & 1) == 0)
33922 /* Remove the invalid clause(s) from the list to avoid
33923 confusing the rest of the compiler. */
33924 clauses = prev;
33925 error_at (here, "%qs is not valid for %qs", c_name, where);
33929 saw_error:
33930 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33932 if (finish_p)
33933 return finish_omp_clauses (clauses, C_ORT_ACC);
33935 return clauses;
33938 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33939 is a bitmask in MASK. Return the list of clauses found; the result
33940 of clause default goes in *pdefault. */
33942 static tree
33943 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33944 const char *where, cp_token *pragma_tok,
33945 bool finish_p = true)
33947 tree clauses = NULL;
33948 bool first = true;
33949 cp_token *token = NULL;
33951 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33953 pragma_omp_clause c_kind;
33954 const char *c_name;
33955 tree prev = clauses;
33957 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33958 cp_lexer_consume_token (parser->lexer);
33960 token = cp_lexer_peek_token (parser->lexer);
33961 c_kind = cp_parser_omp_clause_name (parser);
33963 switch (c_kind)
33965 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33966 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33967 token->location);
33968 c_name = "collapse";
33969 break;
33970 case PRAGMA_OMP_CLAUSE_COPYIN:
33971 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33972 c_name = "copyin";
33973 break;
33974 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33975 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33976 clauses);
33977 c_name = "copyprivate";
33978 break;
33979 case PRAGMA_OMP_CLAUSE_DEFAULT:
33980 clauses = cp_parser_omp_clause_default (parser, clauses,
33981 token->location, false);
33982 c_name = "default";
33983 break;
33984 case PRAGMA_OMP_CLAUSE_FINAL:
33985 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33986 c_name = "final";
33987 break;
33988 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33989 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33990 clauses);
33991 c_name = "firstprivate";
33992 break;
33993 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33994 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33995 token->location);
33996 c_name = "grainsize";
33997 break;
33998 case PRAGMA_OMP_CLAUSE_HINT:
33999 clauses = cp_parser_omp_clause_hint (parser, clauses,
34000 token->location);
34001 c_name = "hint";
34002 break;
34003 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34004 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34005 token->location);
34006 c_name = "defaultmap";
34007 break;
34008 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34009 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34010 clauses);
34011 c_name = "use_device_ptr";
34012 break;
34013 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34014 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34015 clauses);
34016 c_name = "is_device_ptr";
34017 break;
34018 case PRAGMA_OMP_CLAUSE_IF:
34019 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34020 true);
34021 c_name = "if";
34022 break;
34023 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34024 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34025 clauses);
34026 c_name = "lastprivate";
34027 break;
34028 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34029 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34030 token->location);
34031 c_name = "mergeable";
34032 break;
34033 case PRAGMA_OMP_CLAUSE_NOWAIT:
34034 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34035 c_name = "nowait";
34036 break;
34037 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34038 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34039 token->location);
34040 c_name = "num_tasks";
34041 break;
34042 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34043 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34044 token->location);
34045 c_name = "num_threads";
34046 break;
34047 case PRAGMA_OMP_CLAUSE_ORDERED:
34048 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34049 token->location);
34050 c_name = "ordered";
34051 break;
34052 case PRAGMA_OMP_CLAUSE_PRIORITY:
34053 clauses = cp_parser_omp_clause_priority (parser, clauses,
34054 token->location);
34055 c_name = "priority";
34056 break;
34057 case PRAGMA_OMP_CLAUSE_PRIVATE:
34058 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34059 clauses);
34060 c_name = "private";
34061 break;
34062 case PRAGMA_OMP_CLAUSE_REDUCTION:
34063 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34064 c_name = "reduction";
34065 break;
34066 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34067 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34068 token->location);
34069 c_name = "schedule";
34070 break;
34071 case PRAGMA_OMP_CLAUSE_SHARED:
34072 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34073 clauses);
34074 c_name = "shared";
34075 break;
34076 case PRAGMA_OMP_CLAUSE_UNTIED:
34077 clauses = cp_parser_omp_clause_untied (parser, clauses,
34078 token->location);
34079 c_name = "untied";
34080 break;
34081 case PRAGMA_OMP_CLAUSE_INBRANCH:
34082 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34083 clauses, token->location);
34084 c_name = "inbranch";
34085 break;
34086 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34087 clauses = cp_parser_omp_clause_branch (parser,
34088 OMP_CLAUSE_NOTINBRANCH,
34089 clauses, token->location);
34090 c_name = "notinbranch";
34091 break;
34092 case PRAGMA_OMP_CLAUSE_PARALLEL:
34093 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34094 clauses, token->location);
34095 c_name = "parallel";
34096 if (!first)
34098 clause_not_first:
34099 error_at (token->location, "%qs must be the first clause of %qs",
34100 c_name, where);
34101 clauses = prev;
34103 break;
34104 case PRAGMA_OMP_CLAUSE_FOR:
34105 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34106 clauses, token->location);
34107 c_name = "for";
34108 if (!first)
34109 goto clause_not_first;
34110 break;
34111 case PRAGMA_OMP_CLAUSE_SECTIONS:
34112 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34113 clauses, token->location);
34114 c_name = "sections";
34115 if (!first)
34116 goto clause_not_first;
34117 break;
34118 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34119 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34120 clauses, token->location);
34121 c_name = "taskgroup";
34122 if (!first)
34123 goto clause_not_first;
34124 break;
34125 case PRAGMA_OMP_CLAUSE_LINK:
34126 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34127 c_name = "to";
34128 break;
34129 case PRAGMA_OMP_CLAUSE_TO:
34130 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34131 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34132 clauses);
34133 else
34134 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34135 c_name = "to";
34136 break;
34137 case PRAGMA_OMP_CLAUSE_FROM:
34138 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34139 c_name = "from";
34140 break;
34141 case PRAGMA_OMP_CLAUSE_UNIFORM:
34142 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34143 clauses);
34144 c_name = "uniform";
34145 break;
34146 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34147 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34148 token->location);
34149 c_name = "num_teams";
34150 break;
34151 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34152 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34153 token->location);
34154 c_name = "thread_limit";
34155 break;
34156 case PRAGMA_OMP_CLAUSE_ALIGNED:
34157 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34158 c_name = "aligned";
34159 break;
34160 case PRAGMA_OMP_CLAUSE_LINEAR:
34162 bool declare_simd = false;
34163 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34164 declare_simd = true;
34165 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34167 c_name = "linear";
34168 break;
34169 case PRAGMA_OMP_CLAUSE_DEPEND:
34170 clauses = cp_parser_omp_clause_depend (parser, clauses,
34171 token->location);
34172 c_name = "depend";
34173 break;
34174 case PRAGMA_OMP_CLAUSE_MAP:
34175 clauses = cp_parser_omp_clause_map (parser, clauses);
34176 c_name = "map";
34177 break;
34178 case PRAGMA_OMP_CLAUSE_DEVICE:
34179 clauses = cp_parser_omp_clause_device (parser, clauses,
34180 token->location);
34181 c_name = "device";
34182 break;
34183 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34184 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34185 token->location);
34186 c_name = "dist_schedule";
34187 break;
34188 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34189 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34190 token->location);
34191 c_name = "proc_bind";
34192 break;
34193 case PRAGMA_OMP_CLAUSE_SAFELEN:
34194 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34195 token->location);
34196 c_name = "safelen";
34197 break;
34198 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34199 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34200 token->location);
34201 c_name = "simdlen";
34202 break;
34203 case PRAGMA_OMP_CLAUSE_NOGROUP:
34204 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34205 token->location);
34206 c_name = "nogroup";
34207 break;
34208 case PRAGMA_OMP_CLAUSE_THREADS:
34209 clauses
34210 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34211 clauses, token->location);
34212 c_name = "threads";
34213 break;
34214 case PRAGMA_OMP_CLAUSE_SIMD:
34215 clauses
34216 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34217 clauses, token->location);
34218 c_name = "simd";
34219 break;
34220 default:
34221 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34222 goto saw_error;
34225 first = false;
34227 if (((mask >> c_kind) & 1) == 0)
34229 /* Remove the invalid clause(s) from the list to avoid
34230 confusing the rest of the compiler. */
34231 clauses = prev;
34232 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34235 saw_error:
34236 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34237 if (finish_p)
34239 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34240 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34241 else
34242 return finish_omp_clauses (clauses, C_ORT_OMP);
34244 return clauses;
34247 /* OpenMP 2.5:
34248 structured-block:
34249 statement
34251 In practice, we're also interested in adding the statement to an
34252 outer node. So it is convenient if we work around the fact that
34253 cp_parser_statement calls add_stmt. */
34255 static unsigned
34256 cp_parser_begin_omp_structured_block (cp_parser *parser)
34258 unsigned save = parser->in_statement;
34260 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34261 This preserves the "not within loop or switch" style error messages
34262 for nonsense cases like
34263 void foo() {
34264 #pragma omp single
34265 break;
34268 if (parser->in_statement)
34269 parser->in_statement = IN_OMP_BLOCK;
34271 return save;
34274 static void
34275 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34277 parser->in_statement = save;
34280 static tree
34281 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34283 tree stmt = begin_omp_structured_block ();
34284 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34286 cp_parser_statement (parser, NULL_TREE, false, if_p);
34288 cp_parser_end_omp_structured_block (parser, save);
34289 return finish_omp_structured_block (stmt);
34292 /* OpenMP 2.5:
34293 # pragma omp atomic new-line
34294 expression-stmt
34296 expression-stmt:
34297 x binop= expr | x++ | ++x | x-- | --x
34298 binop:
34299 +, *, -, /, &, ^, |, <<, >>
34301 where x is an lvalue expression with scalar type.
34303 OpenMP 3.1:
34304 # pragma omp atomic new-line
34305 update-stmt
34307 # pragma omp atomic read new-line
34308 read-stmt
34310 # pragma omp atomic write new-line
34311 write-stmt
34313 # pragma omp atomic update new-line
34314 update-stmt
34316 # pragma omp atomic capture new-line
34317 capture-stmt
34319 # pragma omp atomic capture new-line
34320 capture-block
34322 read-stmt:
34323 v = x
34324 write-stmt:
34325 x = expr
34326 update-stmt:
34327 expression-stmt | x = x binop expr
34328 capture-stmt:
34329 v = expression-stmt
34330 capture-block:
34331 { v = x; update-stmt; } | { update-stmt; v = x; }
34333 OpenMP 4.0:
34334 update-stmt:
34335 expression-stmt | x = x binop expr | x = expr binop x
34336 capture-stmt:
34337 v = update-stmt
34338 capture-block:
34339 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34341 where x and v are lvalue expressions with scalar type. */
34343 static void
34344 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34346 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34347 tree rhs1 = NULL_TREE, orig_lhs;
34348 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34349 bool structured_block = false;
34350 bool seq_cst = false;
34352 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34354 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34355 const char *p = IDENTIFIER_POINTER (id);
34357 if (!strcmp (p, "seq_cst"))
34359 seq_cst = true;
34360 cp_lexer_consume_token (parser->lexer);
34361 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34362 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34363 cp_lexer_consume_token (parser->lexer);
34366 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34368 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34369 const char *p = IDENTIFIER_POINTER (id);
34371 if (!strcmp (p, "read"))
34372 code = OMP_ATOMIC_READ;
34373 else if (!strcmp (p, "write"))
34374 code = NOP_EXPR;
34375 else if (!strcmp (p, "update"))
34376 code = OMP_ATOMIC;
34377 else if (!strcmp (p, "capture"))
34378 code = OMP_ATOMIC_CAPTURE_NEW;
34379 else
34380 p = NULL;
34381 if (p)
34382 cp_lexer_consume_token (parser->lexer);
34384 if (!seq_cst)
34386 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34387 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34388 cp_lexer_consume_token (parser->lexer);
34390 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34392 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34393 const char *p = IDENTIFIER_POINTER (id);
34395 if (!strcmp (p, "seq_cst"))
34397 seq_cst = true;
34398 cp_lexer_consume_token (parser->lexer);
34402 cp_parser_require_pragma_eol (parser, pragma_tok);
34404 switch (code)
34406 case OMP_ATOMIC_READ:
34407 case NOP_EXPR: /* atomic write */
34408 v = cp_parser_unary_expression (parser);
34409 if (v == error_mark_node)
34410 goto saw_error;
34411 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34412 goto saw_error;
34413 if (code == NOP_EXPR)
34414 lhs = cp_parser_expression (parser);
34415 else
34416 lhs = cp_parser_unary_expression (parser);
34417 if (lhs == error_mark_node)
34418 goto saw_error;
34419 if (code == NOP_EXPR)
34421 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34422 opcode. */
34423 code = OMP_ATOMIC;
34424 rhs = lhs;
34425 lhs = v;
34426 v = NULL_TREE;
34428 goto done;
34429 case OMP_ATOMIC_CAPTURE_NEW:
34430 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34432 cp_lexer_consume_token (parser->lexer);
34433 structured_block = true;
34435 else
34437 v = cp_parser_unary_expression (parser);
34438 if (v == error_mark_node)
34439 goto saw_error;
34440 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34441 goto saw_error;
34443 default:
34444 break;
34447 restart:
34448 lhs = cp_parser_unary_expression (parser);
34449 orig_lhs = lhs;
34450 switch (TREE_CODE (lhs))
34452 case ERROR_MARK:
34453 goto saw_error;
34455 case POSTINCREMENT_EXPR:
34456 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34457 code = OMP_ATOMIC_CAPTURE_OLD;
34458 /* FALLTHROUGH */
34459 case PREINCREMENT_EXPR:
34460 lhs = TREE_OPERAND (lhs, 0);
34461 opcode = PLUS_EXPR;
34462 rhs = integer_one_node;
34463 break;
34465 case POSTDECREMENT_EXPR:
34466 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34467 code = OMP_ATOMIC_CAPTURE_OLD;
34468 /* FALLTHROUGH */
34469 case PREDECREMENT_EXPR:
34470 lhs = TREE_OPERAND (lhs, 0);
34471 opcode = MINUS_EXPR;
34472 rhs = integer_one_node;
34473 break;
34475 case COMPOUND_EXPR:
34476 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34477 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34478 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34479 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34480 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34481 (TREE_OPERAND (lhs, 1), 0), 0)))
34482 == BOOLEAN_TYPE)
34483 /* Undo effects of boolean_increment for post {in,de}crement. */
34484 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34485 /* FALLTHRU */
34486 case MODIFY_EXPR:
34487 if (TREE_CODE (lhs) == MODIFY_EXPR
34488 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34490 /* Undo effects of boolean_increment. */
34491 if (integer_onep (TREE_OPERAND (lhs, 1)))
34493 /* This is pre or post increment. */
34494 rhs = TREE_OPERAND (lhs, 1);
34495 lhs = TREE_OPERAND (lhs, 0);
34496 opcode = NOP_EXPR;
34497 if (code == OMP_ATOMIC_CAPTURE_NEW
34498 && !structured_block
34499 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34500 code = OMP_ATOMIC_CAPTURE_OLD;
34501 break;
34504 /* FALLTHRU */
34505 default:
34506 switch (cp_lexer_peek_token (parser->lexer)->type)
34508 case CPP_MULT_EQ:
34509 opcode = MULT_EXPR;
34510 break;
34511 case CPP_DIV_EQ:
34512 opcode = TRUNC_DIV_EXPR;
34513 break;
34514 case CPP_PLUS_EQ:
34515 opcode = PLUS_EXPR;
34516 break;
34517 case CPP_MINUS_EQ:
34518 opcode = MINUS_EXPR;
34519 break;
34520 case CPP_LSHIFT_EQ:
34521 opcode = LSHIFT_EXPR;
34522 break;
34523 case CPP_RSHIFT_EQ:
34524 opcode = RSHIFT_EXPR;
34525 break;
34526 case CPP_AND_EQ:
34527 opcode = BIT_AND_EXPR;
34528 break;
34529 case CPP_OR_EQ:
34530 opcode = BIT_IOR_EXPR;
34531 break;
34532 case CPP_XOR_EQ:
34533 opcode = BIT_XOR_EXPR;
34534 break;
34535 case CPP_EQ:
34536 enum cp_parser_prec oprec;
34537 cp_token *token;
34538 cp_lexer_consume_token (parser->lexer);
34539 cp_parser_parse_tentatively (parser);
34540 rhs1 = cp_parser_simple_cast_expression (parser);
34541 if (rhs1 == error_mark_node)
34543 cp_parser_abort_tentative_parse (parser);
34544 cp_parser_simple_cast_expression (parser);
34545 goto saw_error;
34547 token = cp_lexer_peek_token (parser->lexer);
34548 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34550 cp_parser_abort_tentative_parse (parser);
34551 cp_parser_parse_tentatively (parser);
34552 rhs = cp_parser_binary_expression (parser, false, true,
34553 PREC_NOT_OPERATOR, NULL);
34554 if (rhs == error_mark_node)
34556 cp_parser_abort_tentative_parse (parser);
34557 cp_parser_binary_expression (parser, false, true,
34558 PREC_NOT_OPERATOR, NULL);
34559 goto saw_error;
34561 switch (TREE_CODE (rhs))
34563 case MULT_EXPR:
34564 case TRUNC_DIV_EXPR:
34565 case RDIV_EXPR:
34566 case PLUS_EXPR:
34567 case MINUS_EXPR:
34568 case LSHIFT_EXPR:
34569 case RSHIFT_EXPR:
34570 case BIT_AND_EXPR:
34571 case BIT_IOR_EXPR:
34572 case BIT_XOR_EXPR:
34573 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34575 if (cp_parser_parse_definitely (parser))
34577 opcode = TREE_CODE (rhs);
34578 rhs1 = TREE_OPERAND (rhs, 0);
34579 rhs = TREE_OPERAND (rhs, 1);
34580 goto stmt_done;
34582 else
34583 goto saw_error;
34585 break;
34586 default:
34587 break;
34589 cp_parser_abort_tentative_parse (parser);
34590 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34592 rhs = cp_parser_expression (parser);
34593 if (rhs == error_mark_node)
34594 goto saw_error;
34595 opcode = NOP_EXPR;
34596 rhs1 = NULL_TREE;
34597 goto stmt_done;
34599 cp_parser_error (parser,
34600 "invalid form of %<#pragma omp atomic%>");
34601 goto saw_error;
34603 if (!cp_parser_parse_definitely (parser))
34604 goto saw_error;
34605 switch (token->type)
34607 case CPP_SEMICOLON:
34608 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34610 code = OMP_ATOMIC_CAPTURE_OLD;
34611 v = lhs;
34612 lhs = NULL_TREE;
34613 lhs1 = rhs1;
34614 rhs1 = NULL_TREE;
34615 cp_lexer_consume_token (parser->lexer);
34616 goto restart;
34618 else if (structured_block)
34620 opcode = NOP_EXPR;
34621 rhs = rhs1;
34622 rhs1 = NULL_TREE;
34623 goto stmt_done;
34625 cp_parser_error (parser,
34626 "invalid form of %<#pragma omp atomic%>");
34627 goto saw_error;
34628 case CPP_MULT:
34629 opcode = MULT_EXPR;
34630 break;
34631 case CPP_DIV:
34632 opcode = TRUNC_DIV_EXPR;
34633 break;
34634 case CPP_PLUS:
34635 opcode = PLUS_EXPR;
34636 break;
34637 case CPP_MINUS:
34638 opcode = MINUS_EXPR;
34639 break;
34640 case CPP_LSHIFT:
34641 opcode = LSHIFT_EXPR;
34642 break;
34643 case CPP_RSHIFT:
34644 opcode = RSHIFT_EXPR;
34645 break;
34646 case CPP_AND:
34647 opcode = BIT_AND_EXPR;
34648 break;
34649 case CPP_OR:
34650 opcode = BIT_IOR_EXPR;
34651 break;
34652 case CPP_XOR:
34653 opcode = BIT_XOR_EXPR;
34654 break;
34655 default:
34656 cp_parser_error (parser,
34657 "invalid operator for %<#pragma omp atomic%>");
34658 goto saw_error;
34660 oprec = TOKEN_PRECEDENCE (token);
34661 gcc_assert (oprec != PREC_NOT_OPERATOR);
34662 if (commutative_tree_code (opcode))
34663 oprec = (enum cp_parser_prec) (oprec - 1);
34664 cp_lexer_consume_token (parser->lexer);
34665 rhs = cp_parser_binary_expression (parser, false, false,
34666 oprec, NULL);
34667 if (rhs == error_mark_node)
34668 goto saw_error;
34669 goto stmt_done;
34670 /* FALLTHROUGH */
34671 default:
34672 cp_parser_error (parser,
34673 "invalid operator for %<#pragma omp atomic%>");
34674 goto saw_error;
34676 cp_lexer_consume_token (parser->lexer);
34678 rhs = cp_parser_expression (parser);
34679 if (rhs == error_mark_node)
34680 goto saw_error;
34681 break;
34683 stmt_done:
34684 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34686 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34687 goto saw_error;
34688 v = cp_parser_unary_expression (parser);
34689 if (v == error_mark_node)
34690 goto saw_error;
34691 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34692 goto saw_error;
34693 lhs1 = cp_parser_unary_expression (parser);
34694 if (lhs1 == error_mark_node)
34695 goto saw_error;
34697 if (structured_block)
34699 cp_parser_consume_semicolon_at_end_of_statement (parser);
34700 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34702 done:
34703 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34704 if (!structured_block)
34705 cp_parser_consume_semicolon_at_end_of_statement (parser);
34706 return;
34708 saw_error:
34709 cp_parser_skip_to_end_of_block_or_statement (parser);
34710 if (structured_block)
34712 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34713 cp_lexer_consume_token (parser->lexer);
34714 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34716 cp_parser_skip_to_end_of_block_or_statement (parser);
34717 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34718 cp_lexer_consume_token (parser->lexer);
34724 /* OpenMP 2.5:
34725 # pragma omp barrier new-line */
34727 static void
34728 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34730 cp_parser_require_pragma_eol (parser, pragma_tok);
34731 finish_omp_barrier ();
34734 /* OpenMP 2.5:
34735 # pragma omp critical [(name)] new-line
34736 structured-block
34738 OpenMP 4.5:
34739 # pragma omp critical [(name) [hint(expression)]] new-line
34740 structured-block */
34742 #define OMP_CRITICAL_CLAUSE_MASK \
34743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34745 static tree
34746 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34748 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34750 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34752 matching_parens parens;
34753 parens.consume_open (parser);
34755 name = cp_parser_identifier (parser);
34757 if (name == error_mark_node
34758 || !parens.require_close (parser))
34759 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34760 /*or_comma=*/false,
34761 /*consume_paren=*/true);
34762 if (name == error_mark_node)
34763 name = NULL;
34765 clauses = cp_parser_omp_all_clauses (parser,
34766 OMP_CRITICAL_CLAUSE_MASK,
34767 "#pragma omp critical", pragma_tok);
34769 else
34770 cp_parser_require_pragma_eol (parser, pragma_tok);
34772 stmt = cp_parser_omp_structured_block (parser, if_p);
34773 return c_finish_omp_critical (input_location, stmt, name, clauses);
34776 /* OpenMP 2.5:
34777 # pragma omp flush flush-vars[opt] new-line
34779 flush-vars:
34780 ( variable-list ) */
34782 static void
34783 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34785 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34786 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34787 cp_parser_require_pragma_eol (parser, pragma_tok);
34789 finish_omp_flush ();
34792 /* Helper function, to parse omp for increment expression. */
34794 static tree
34795 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34797 tree cond = cp_parser_binary_expression (parser, false, true,
34798 PREC_NOT_OPERATOR, NULL);
34799 if (cond == error_mark_node
34800 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34802 cp_parser_skip_to_end_of_statement (parser);
34803 return error_mark_node;
34806 switch (TREE_CODE (cond))
34808 case GT_EXPR:
34809 case GE_EXPR:
34810 case LT_EXPR:
34811 case LE_EXPR:
34812 break;
34813 case NE_EXPR:
34814 /* Fall through: OpenMP disallows NE_EXPR. */
34815 gcc_fallthrough ();
34816 default:
34817 return error_mark_node;
34820 /* If decl is an iterator, preserve LHS and RHS of the relational
34821 expr until finish_omp_for. */
34822 if (decl
34823 && (type_dependent_expression_p (decl)
34824 || CLASS_TYPE_P (TREE_TYPE (decl))))
34825 return cond;
34827 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
34828 TREE_CODE (cond),
34829 TREE_OPERAND (cond, 0), ERROR_MARK,
34830 TREE_OPERAND (cond, 1), ERROR_MARK,
34831 /*overload=*/NULL, tf_warning_or_error);
34834 /* Helper function, to parse omp for increment expression. */
34836 static tree
34837 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34839 cp_token *token = cp_lexer_peek_token (parser->lexer);
34840 enum tree_code op;
34841 tree lhs, rhs;
34842 cp_id_kind idk;
34843 bool decl_first;
34845 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34847 op = (token->type == CPP_PLUS_PLUS
34848 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34849 cp_lexer_consume_token (parser->lexer);
34850 lhs = cp_parser_simple_cast_expression (parser);
34851 if (lhs != decl
34852 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34853 return error_mark_node;
34854 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34857 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34858 if (lhs != decl
34859 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34860 return error_mark_node;
34862 token = cp_lexer_peek_token (parser->lexer);
34863 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34865 op = (token->type == CPP_PLUS_PLUS
34866 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34867 cp_lexer_consume_token (parser->lexer);
34868 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34871 op = cp_parser_assignment_operator_opt (parser);
34872 if (op == ERROR_MARK)
34873 return error_mark_node;
34875 if (op != NOP_EXPR)
34877 rhs = cp_parser_assignment_expression (parser);
34878 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34879 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34882 lhs = cp_parser_binary_expression (parser, false, false,
34883 PREC_ADDITIVE_EXPRESSION, NULL);
34884 token = cp_lexer_peek_token (parser->lexer);
34885 decl_first = (lhs == decl
34886 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34887 if (decl_first)
34888 lhs = NULL_TREE;
34889 if (token->type != CPP_PLUS
34890 && token->type != CPP_MINUS)
34891 return error_mark_node;
34895 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34896 cp_lexer_consume_token (parser->lexer);
34897 rhs = cp_parser_binary_expression (parser, false, false,
34898 PREC_ADDITIVE_EXPRESSION, NULL);
34899 token = cp_lexer_peek_token (parser->lexer);
34900 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34902 if (lhs == NULL_TREE)
34904 if (op == PLUS_EXPR)
34905 lhs = rhs;
34906 else
34907 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34908 tf_warning_or_error);
34910 else
34911 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34912 ERROR_MARK, NULL, tf_warning_or_error);
34915 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34917 if (!decl_first)
34919 if ((rhs != decl
34920 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34921 || op == MINUS_EXPR)
34922 return error_mark_node;
34923 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34925 else
34926 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34928 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34931 /* Parse the initialization statement of an OpenMP for loop.
34933 Return true if the resulting construct should have an
34934 OMP_CLAUSE_PRIVATE added to it. */
34936 static tree
34937 cp_parser_omp_for_loop_init (cp_parser *parser,
34938 tree &this_pre_body,
34939 vec<tree, va_gc> *&for_block,
34940 tree &init,
34941 tree &orig_init,
34942 tree &decl,
34943 tree &real_decl)
34945 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34946 return NULL_TREE;
34948 tree add_private_clause = NULL_TREE;
34950 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34952 init-expr:
34953 var = lb
34954 integer-type var = lb
34955 random-access-iterator-type var = lb
34956 pointer-type var = lb
34958 cp_decl_specifier_seq type_specifiers;
34960 /* First, try to parse as an initialized declaration. See
34961 cp_parser_condition, from whence the bulk of this is copied. */
34963 cp_parser_parse_tentatively (parser);
34964 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34965 /*is_trailing_return=*/false,
34966 &type_specifiers);
34967 if (cp_parser_parse_definitely (parser))
34969 /* If parsing a type specifier seq succeeded, then this
34970 MUST be a initialized declaration. */
34971 tree asm_specification, attributes;
34972 cp_declarator *declarator;
34974 declarator = cp_parser_declarator (parser,
34975 CP_PARSER_DECLARATOR_NAMED,
34976 /*ctor_dtor_or_conv_p=*/NULL,
34977 /*parenthesized_p=*/NULL,
34978 /*member_p=*/false,
34979 /*friend_p=*/false);
34980 attributes = cp_parser_attributes_opt (parser);
34981 asm_specification = cp_parser_asm_specification_opt (parser);
34983 if (declarator == cp_error_declarator)
34984 cp_parser_skip_to_end_of_statement (parser);
34986 else
34988 tree pushed_scope, auto_node;
34990 decl = start_decl (declarator, &type_specifiers,
34991 SD_INITIALIZED, attributes,
34992 /*prefix_attributes=*/NULL_TREE,
34993 &pushed_scope);
34995 auto_node = type_uses_auto (TREE_TYPE (decl));
34996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34998 if (cp_lexer_next_token_is (parser->lexer,
34999 CPP_OPEN_PAREN))
35000 error ("parenthesized initialization is not allowed in "
35001 "OpenMP %<for%> loop");
35002 else
35003 /* Trigger an error. */
35004 cp_parser_require (parser, CPP_EQ, RT_EQ);
35006 init = error_mark_node;
35007 cp_parser_skip_to_end_of_statement (parser);
35009 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35010 || type_dependent_expression_p (decl)
35011 || auto_node)
35013 bool is_direct_init, is_non_constant_init;
35015 init = cp_parser_initializer (parser,
35016 &is_direct_init,
35017 &is_non_constant_init);
35019 if (auto_node)
35021 TREE_TYPE (decl)
35022 = do_auto_deduction (TREE_TYPE (decl), init,
35023 auto_node);
35025 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35026 && !type_dependent_expression_p (decl))
35027 goto non_class;
35030 cp_finish_decl (decl, init, !is_non_constant_init,
35031 asm_specification,
35032 LOOKUP_ONLYCONVERTING);
35033 orig_init = init;
35034 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35036 vec_safe_push (for_block, this_pre_body);
35037 init = NULL_TREE;
35039 else
35041 init = pop_stmt_list (this_pre_body);
35042 if (init && TREE_CODE (init) == STATEMENT_LIST)
35044 tree_stmt_iterator i = tsi_start (init);
35045 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35046 while (!tsi_end_p (i))
35048 tree t = tsi_stmt (i);
35049 if (TREE_CODE (t) == DECL_EXPR
35050 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35052 tsi_delink (&i);
35053 vec_safe_push (for_block, t);
35054 continue;
35056 break;
35058 if (tsi_one_before_end_p (i))
35060 tree t = tsi_stmt (i);
35061 tsi_delink (&i);
35062 free_stmt_list (init);
35063 init = t;
35067 this_pre_body = NULL_TREE;
35069 else
35071 /* Consume '='. */
35072 cp_lexer_consume_token (parser->lexer);
35073 init = cp_parser_assignment_expression (parser);
35075 non_class:
35076 if (TYPE_REF_P (TREE_TYPE (decl)))
35077 init = error_mark_node;
35078 else
35079 cp_finish_decl (decl, NULL_TREE,
35080 /*init_const_expr_p=*/false,
35081 asm_specification,
35082 LOOKUP_ONLYCONVERTING);
35085 if (pushed_scope)
35086 pop_scope (pushed_scope);
35089 else
35091 cp_id_kind idk;
35092 /* If parsing a type specifier sequence failed, then
35093 this MUST be a simple expression. */
35094 cp_parser_parse_tentatively (parser);
35095 decl = cp_parser_primary_expression (parser, false, false,
35096 false, &idk);
35097 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35098 if (!cp_parser_error_occurred (parser)
35099 && decl
35100 && (TREE_CODE (decl) == COMPONENT_REF
35101 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35103 cp_parser_abort_tentative_parse (parser);
35104 cp_parser_parse_tentatively (parser);
35105 cp_token *token = cp_lexer_peek_token (parser->lexer);
35106 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35107 /*check_dependency_p=*/true,
35108 /*template_p=*/NULL,
35109 /*declarator_p=*/false,
35110 /*optional_p=*/false);
35111 if (name != error_mark_node
35112 && last_tok == cp_lexer_peek_token (parser->lexer))
35114 decl = cp_parser_lookup_name_simple (parser, name,
35115 token->location);
35116 if (TREE_CODE (decl) == FIELD_DECL)
35117 add_private_clause = omp_privatize_field (decl, false);
35119 cp_parser_abort_tentative_parse (parser);
35120 cp_parser_parse_tentatively (parser);
35121 decl = cp_parser_primary_expression (parser, false, false,
35122 false, &idk);
35124 if (!cp_parser_error_occurred (parser)
35125 && decl
35126 && DECL_P (decl)
35127 && CLASS_TYPE_P (TREE_TYPE (decl)))
35129 tree rhs;
35131 cp_parser_parse_definitely (parser);
35132 cp_parser_require (parser, CPP_EQ, RT_EQ);
35133 rhs = cp_parser_assignment_expression (parser);
35134 orig_init = rhs;
35135 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35136 decl, NOP_EXPR,
35137 rhs,
35138 tf_warning_or_error));
35139 if (!add_private_clause)
35140 add_private_clause = decl;
35142 else
35144 decl = NULL;
35145 cp_parser_abort_tentative_parse (parser);
35146 init = cp_parser_expression (parser);
35147 if (init)
35149 if (TREE_CODE (init) == MODIFY_EXPR
35150 || TREE_CODE (init) == MODOP_EXPR)
35151 real_decl = TREE_OPERAND (init, 0);
35155 return add_private_clause;
35158 /* Parse the restricted form of the for statement allowed by OpenMP. */
35160 static tree
35161 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35162 tree *cclauses, bool *if_p)
35164 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35165 tree real_decl, initv, condv, incrv, declv;
35166 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35167 location_t loc_first;
35168 bool collapse_err = false;
35169 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35170 vec<tree, va_gc> *for_block = make_tree_vector ();
35171 auto_vec<tree, 4> orig_inits;
35172 bool tiling = false;
35174 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35175 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35176 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35177 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35179 tiling = true;
35180 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35182 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35183 && OMP_CLAUSE_ORDERED_EXPR (cl))
35185 ordered_cl = cl;
35186 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35189 if (ordered && ordered < collapse)
35191 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35192 "%<ordered%> clause parameter is less than %<collapse%>");
35193 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35194 = build_int_cst (NULL_TREE, collapse);
35195 ordered = collapse;
35197 if (ordered)
35199 for (tree *pc = &clauses; *pc; )
35200 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35202 error_at (OMP_CLAUSE_LOCATION (*pc),
35203 "%<linear%> clause may not be specified together "
35204 "with %<ordered%> clause with a parameter");
35205 *pc = OMP_CLAUSE_CHAIN (*pc);
35207 else
35208 pc = &OMP_CLAUSE_CHAIN (*pc);
35211 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35212 count = ordered ? ordered : collapse;
35214 declv = make_tree_vec (count);
35215 initv = make_tree_vec (count);
35216 condv = make_tree_vec (count);
35217 incrv = make_tree_vec (count);
35219 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35221 for (i = 0; i < count; i++)
35223 int bracecount = 0;
35224 tree add_private_clause = NULL_TREE;
35225 location_t loc;
35227 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35229 if (!collapse_err)
35230 cp_parser_error (parser, "for statement expected");
35231 return NULL;
35233 loc = cp_lexer_consume_token (parser->lexer)->location;
35235 matching_parens parens;
35236 if (!parens.require_open (parser))
35237 return NULL;
35239 init = orig_init = decl = real_decl = NULL;
35240 this_pre_body = push_stmt_list ();
35242 add_private_clause
35243 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35244 init, orig_init, decl, real_decl);
35246 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35247 if (this_pre_body)
35249 this_pre_body = pop_stmt_list (this_pre_body);
35250 if (pre_body)
35252 tree t = pre_body;
35253 pre_body = push_stmt_list ();
35254 add_stmt (t);
35255 add_stmt (this_pre_body);
35256 pre_body = pop_stmt_list (pre_body);
35258 else
35259 pre_body = this_pre_body;
35262 if (decl)
35263 real_decl = decl;
35264 if (cclauses != NULL
35265 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35266 && real_decl != NULL_TREE)
35268 tree *c;
35269 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35270 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35271 && OMP_CLAUSE_DECL (*c) == real_decl)
35273 error_at (loc, "iteration variable %qD"
35274 " should not be firstprivate", real_decl);
35275 *c = OMP_CLAUSE_CHAIN (*c);
35277 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35278 && OMP_CLAUSE_DECL (*c) == real_decl)
35280 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35281 tree l = *c;
35282 *c = OMP_CLAUSE_CHAIN (*c);
35283 if (code == OMP_SIMD)
35285 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35286 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35288 else
35290 OMP_CLAUSE_CHAIN (l) = clauses;
35291 clauses = l;
35293 add_private_clause = NULL_TREE;
35295 else
35297 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35298 && OMP_CLAUSE_DECL (*c) == real_decl)
35299 add_private_clause = NULL_TREE;
35300 c = &OMP_CLAUSE_CHAIN (*c);
35304 if (add_private_clause)
35306 tree c;
35307 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35309 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35310 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35311 && OMP_CLAUSE_DECL (c) == decl)
35312 break;
35313 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35314 && OMP_CLAUSE_DECL (c) == decl)
35315 error_at (loc, "iteration variable %qD "
35316 "should not be firstprivate",
35317 decl);
35318 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35319 && OMP_CLAUSE_DECL (c) == decl)
35320 error_at (loc, "iteration variable %qD should not be reduction",
35321 decl);
35323 if (c == NULL)
35325 if (code != OMP_SIMD)
35326 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35327 else if (collapse == 1)
35328 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35329 else
35330 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35331 OMP_CLAUSE_DECL (c) = add_private_clause;
35332 c = finish_omp_clauses (c, C_ORT_OMP);
35333 if (c)
35335 OMP_CLAUSE_CHAIN (c) = clauses;
35336 clauses = c;
35337 /* For linear, signal that we need to fill up
35338 the so far unknown linear step. */
35339 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35340 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35345 cond = NULL;
35346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35347 cond = cp_parser_omp_for_cond (parser, decl);
35348 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35350 incr = NULL;
35351 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35353 /* If decl is an iterator, preserve the operator on decl
35354 until finish_omp_for. */
35355 if (real_decl
35356 && ((processing_template_decl
35357 && (TREE_TYPE (real_decl) == NULL_TREE
35358 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
35359 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35360 incr = cp_parser_omp_for_incr (parser, real_decl);
35361 else
35362 incr = cp_parser_expression (parser);
35363 if (!EXPR_HAS_LOCATION (incr))
35364 protected_set_expr_location (incr, input_location);
35367 if (!parens.require_close (parser))
35368 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35369 /*or_comma=*/false,
35370 /*consume_paren=*/true);
35372 TREE_VEC_ELT (declv, i) = decl;
35373 TREE_VEC_ELT (initv, i) = init;
35374 TREE_VEC_ELT (condv, i) = cond;
35375 TREE_VEC_ELT (incrv, i) = incr;
35376 if (orig_init)
35378 orig_inits.safe_grow_cleared (i + 1);
35379 orig_inits[i] = orig_init;
35382 if (i == count - 1)
35383 break;
35385 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35386 in between the collapsed for loops to be still considered perfectly
35387 nested. Hopefully the final version clarifies this.
35388 For now handle (multiple) {'s and empty statements. */
35389 cp_parser_parse_tentatively (parser);
35390 for (;;)
35392 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35393 break;
35394 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35396 cp_lexer_consume_token (parser->lexer);
35397 bracecount++;
35399 else if (bracecount
35400 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35401 cp_lexer_consume_token (parser->lexer);
35402 else
35404 loc = cp_lexer_peek_token (parser->lexer)->location;
35405 error_at (loc, "not enough for loops to collapse");
35406 collapse_err = true;
35407 cp_parser_abort_tentative_parse (parser);
35408 declv = NULL_TREE;
35409 break;
35413 if (declv)
35415 cp_parser_parse_definitely (parser);
35416 nbraces += bracecount;
35420 if (nbraces)
35421 if_p = NULL;
35423 /* Note that we saved the original contents of this flag when we entered
35424 the structured block, and so we don't need to re-save it here. */
35425 parser->in_statement = IN_OMP_FOR;
35427 /* Note that the grammar doesn't call for a structured block here,
35428 though the loop as a whole is a structured block. */
35429 body = push_stmt_list ();
35430 cp_parser_statement (parser, NULL_TREE, false, if_p);
35431 body = pop_stmt_list (body);
35433 if (declv == NULL_TREE)
35434 ret = NULL_TREE;
35435 else
35436 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35437 body, pre_body, &orig_inits, clauses);
35439 while (nbraces)
35441 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35443 cp_lexer_consume_token (parser->lexer);
35444 nbraces--;
35446 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35447 cp_lexer_consume_token (parser->lexer);
35448 else
35450 if (!collapse_err)
35452 error_at (cp_lexer_peek_token (parser->lexer)->location,
35453 "collapsed loops not perfectly nested");
35455 collapse_err = true;
35456 cp_parser_statement_seq_opt (parser, NULL);
35457 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35458 break;
35462 while (!for_block->is_empty ())
35464 tree t = for_block->pop ();
35465 if (TREE_CODE (t) == STATEMENT_LIST)
35466 add_stmt (pop_stmt_list (t));
35467 else
35468 add_stmt (t);
35470 release_tree_vector (for_block);
35472 return ret;
35475 /* Helper function for OpenMP parsing, split clauses and call
35476 finish_omp_clauses on each of the set of clauses afterwards. */
35478 static void
35479 cp_omp_split_clauses (location_t loc, enum tree_code code,
35480 omp_clause_mask mask, tree clauses, tree *cclauses)
35482 int i;
35483 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35484 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35485 if (cclauses[i])
35486 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35489 /* OpenMP 4.0:
35490 #pragma omp simd simd-clause[optseq] new-line
35491 for-loop */
35493 #define OMP_SIMD_CLAUSE_MASK \
35494 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35503 static tree
35504 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35505 char *p_name, omp_clause_mask mask, tree *cclauses,
35506 bool *if_p)
35508 tree clauses, sb, ret;
35509 unsigned int save;
35510 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35512 strcat (p_name, " simd");
35513 mask |= OMP_SIMD_CLAUSE_MASK;
35515 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35516 cclauses == NULL);
35517 if (cclauses)
35519 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35520 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35521 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35522 OMP_CLAUSE_ORDERED);
35523 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35525 error_at (OMP_CLAUSE_LOCATION (c),
35526 "%<ordered%> clause with parameter may not be specified "
35527 "on %qs construct", p_name);
35528 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35532 sb = begin_omp_structured_block ();
35533 save = cp_parser_begin_omp_structured_block (parser);
35535 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35537 cp_parser_end_omp_structured_block (parser, save);
35538 add_stmt (finish_omp_structured_block (sb));
35540 return ret;
35543 /* OpenMP 2.5:
35544 #pragma omp for for-clause[optseq] new-line
35545 for-loop
35547 OpenMP 4.0:
35548 #pragma omp for simd for-simd-clause[optseq] new-line
35549 for-loop */
35551 #define OMP_FOR_CLAUSE_MASK \
35552 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35562 static tree
35563 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35564 char *p_name, omp_clause_mask mask, tree *cclauses,
35565 bool *if_p)
35567 tree clauses, sb, ret;
35568 unsigned int save;
35569 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35571 strcat (p_name, " for");
35572 mask |= OMP_FOR_CLAUSE_MASK;
35573 /* parallel for{, simd} disallows nowait clause, but for
35574 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35575 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35576 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35577 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35578 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35579 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35581 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35583 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35584 const char *p = IDENTIFIER_POINTER (id);
35586 if (strcmp (p, "simd") == 0)
35588 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35589 if (cclauses == NULL)
35590 cclauses = cclauses_buf;
35592 cp_lexer_consume_token (parser->lexer);
35593 if (!flag_openmp) /* flag_openmp_simd */
35594 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35595 cclauses, if_p);
35596 sb = begin_omp_structured_block ();
35597 save = cp_parser_begin_omp_structured_block (parser);
35598 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35599 cclauses, if_p);
35600 cp_parser_end_omp_structured_block (parser, save);
35601 tree body = finish_omp_structured_block (sb);
35602 if (ret == NULL)
35603 return ret;
35604 ret = make_node (OMP_FOR);
35605 TREE_TYPE (ret) = void_type_node;
35606 OMP_FOR_BODY (ret) = body;
35607 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35608 SET_EXPR_LOCATION (ret, loc);
35609 add_stmt (ret);
35610 return ret;
35613 if (!flag_openmp) /* flag_openmp_simd */
35615 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35616 return NULL_TREE;
35619 /* Composite distribute parallel for disallows linear clause. */
35620 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35621 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35623 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35624 cclauses == NULL);
35625 if (cclauses)
35627 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35628 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35631 sb = begin_omp_structured_block ();
35632 save = cp_parser_begin_omp_structured_block (parser);
35634 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35636 cp_parser_end_omp_structured_block (parser, save);
35637 add_stmt (finish_omp_structured_block (sb));
35639 return ret;
35642 /* OpenMP 2.5:
35643 # pragma omp master new-line
35644 structured-block */
35646 static tree
35647 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35649 cp_parser_require_pragma_eol (parser, pragma_tok);
35650 return c_finish_omp_master (input_location,
35651 cp_parser_omp_structured_block (parser, if_p));
35654 /* OpenMP 2.5:
35655 # pragma omp ordered new-line
35656 structured-block
35658 OpenMP 4.5:
35659 # pragma omp ordered ordered-clauses new-line
35660 structured-block */
35662 #define OMP_ORDERED_CLAUSE_MASK \
35663 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35666 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35667 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35669 static bool
35670 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35671 enum pragma_context context, bool *if_p)
35673 location_t loc = pragma_tok->location;
35675 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35677 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35678 const char *p = IDENTIFIER_POINTER (id);
35680 if (strcmp (p, "depend") == 0)
35682 if (!flag_openmp) /* flag_openmp_simd */
35684 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35685 return false;
35687 if (context == pragma_stmt)
35689 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35690 "%<depend%> clause may only be used in compound "
35691 "statements");
35692 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35693 return false;
35695 tree clauses
35696 = cp_parser_omp_all_clauses (parser,
35697 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35698 "#pragma omp ordered", pragma_tok);
35699 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35700 return false;
35704 tree clauses
35705 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35706 "#pragma omp ordered", pragma_tok);
35708 if (!flag_openmp /* flag_openmp_simd */
35709 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35710 return false;
35712 c_finish_omp_ordered (loc, clauses,
35713 cp_parser_omp_structured_block (parser, if_p));
35714 return true;
35717 /* OpenMP 2.5:
35719 section-scope:
35720 { section-sequence }
35722 section-sequence:
35723 section-directive[opt] structured-block
35724 section-sequence section-directive structured-block */
35726 static tree
35727 cp_parser_omp_sections_scope (cp_parser *parser)
35729 tree stmt, substmt;
35730 bool error_suppress = false;
35731 cp_token *tok;
35733 matching_braces braces;
35734 if (!braces.require_open (parser))
35735 return NULL_TREE;
35737 stmt = push_stmt_list ();
35739 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35740 != PRAGMA_OMP_SECTION)
35742 substmt = cp_parser_omp_structured_block (parser, NULL);
35743 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35744 add_stmt (substmt);
35747 while (1)
35749 tok = cp_lexer_peek_token (parser->lexer);
35750 if (tok->type == CPP_CLOSE_BRACE)
35751 break;
35752 if (tok->type == CPP_EOF)
35753 break;
35755 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35757 cp_lexer_consume_token (parser->lexer);
35758 cp_parser_require_pragma_eol (parser, tok);
35759 error_suppress = false;
35761 else if (!error_suppress)
35763 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35764 error_suppress = true;
35767 substmt = cp_parser_omp_structured_block (parser, NULL);
35768 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35769 add_stmt (substmt);
35771 braces.require_close (parser);
35773 substmt = pop_stmt_list (stmt);
35775 stmt = make_node (OMP_SECTIONS);
35776 TREE_TYPE (stmt) = void_type_node;
35777 OMP_SECTIONS_BODY (stmt) = substmt;
35779 add_stmt (stmt);
35780 return stmt;
35783 /* OpenMP 2.5:
35784 # pragma omp sections sections-clause[optseq] newline
35785 sections-scope */
35787 #define OMP_SECTIONS_CLAUSE_MASK \
35788 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35794 static tree
35795 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35796 char *p_name, omp_clause_mask mask, tree *cclauses)
35798 tree clauses, ret;
35799 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35801 strcat (p_name, " sections");
35802 mask |= OMP_SECTIONS_CLAUSE_MASK;
35803 if (cclauses)
35804 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35806 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35807 cclauses == NULL);
35808 if (cclauses)
35810 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35811 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35814 ret = cp_parser_omp_sections_scope (parser);
35815 if (ret)
35816 OMP_SECTIONS_CLAUSES (ret) = clauses;
35818 return ret;
35821 /* OpenMP 2.5:
35822 # pragma omp parallel parallel-clause[optseq] new-line
35823 structured-block
35824 # pragma omp parallel for parallel-for-clause[optseq] new-line
35825 structured-block
35826 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35827 structured-block
35829 OpenMP 4.0:
35830 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35831 structured-block */
35833 #define OMP_PARALLEL_CLAUSE_MASK \
35834 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35844 static tree
35845 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35846 char *p_name, omp_clause_mask mask, tree *cclauses,
35847 bool *if_p)
35849 tree stmt, clauses, block;
35850 unsigned int save;
35851 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35853 strcat (p_name, " parallel");
35854 mask |= OMP_PARALLEL_CLAUSE_MASK;
35855 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35856 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35857 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35858 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35860 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35862 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35863 if (cclauses == NULL)
35864 cclauses = cclauses_buf;
35866 cp_lexer_consume_token (parser->lexer);
35867 if (!flag_openmp) /* flag_openmp_simd */
35868 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35869 if_p);
35870 block = begin_omp_parallel ();
35871 save = cp_parser_begin_omp_structured_block (parser);
35872 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35873 if_p);
35874 cp_parser_end_omp_structured_block (parser, save);
35875 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35876 block);
35877 if (ret == NULL_TREE)
35878 return ret;
35879 OMP_PARALLEL_COMBINED (stmt) = 1;
35880 return stmt;
35882 /* When combined with distribute, parallel has to be followed by for.
35883 #pragma omp target parallel is allowed though. */
35884 else if (cclauses
35885 && (mask & (OMP_CLAUSE_MASK_1
35886 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35888 error_at (loc, "expected %<for%> after %qs", p_name);
35889 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35890 return NULL_TREE;
35892 else if (!flag_openmp) /* flag_openmp_simd */
35894 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35895 return NULL_TREE;
35897 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35899 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35900 const char *p = IDENTIFIER_POINTER (id);
35901 if (strcmp (p, "sections") == 0)
35903 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35904 cclauses = cclauses_buf;
35906 cp_lexer_consume_token (parser->lexer);
35907 block = begin_omp_parallel ();
35908 save = cp_parser_begin_omp_structured_block (parser);
35909 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35910 cp_parser_end_omp_structured_block (parser, save);
35911 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35912 block);
35913 OMP_PARALLEL_COMBINED (stmt) = 1;
35914 return stmt;
35918 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35919 cclauses == NULL);
35920 if (cclauses)
35922 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35923 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35926 block = begin_omp_parallel ();
35927 save = cp_parser_begin_omp_structured_block (parser);
35928 cp_parser_statement (parser, NULL_TREE, false, if_p);
35929 cp_parser_end_omp_structured_block (parser, save);
35930 stmt = finish_omp_parallel (clauses, block);
35931 return stmt;
35934 /* OpenMP 2.5:
35935 # pragma omp single single-clause[optseq] new-line
35936 structured-block */
35938 #define OMP_SINGLE_CLAUSE_MASK \
35939 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35944 static tree
35945 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35947 tree stmt = make_node (OMP_SINGLE);
35948 TREE_TYPE (stmt) = void_type_node;
35950 OMP_SINGLE_CLAUSES (stmt)
35951 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35952 "#pragma omp single", pragma_tok);
35953 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35955 return add_stmt (stmt);
35958 /* OpenMP 3.0:
35959 # pragma omp task task-clause[optseq] new-line
35960 structured-block */
35962 #define OMP_TASK_CLAUSE_MASK \
35963 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35974 static tree
35975 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35977 tree clauses, block;
35978 unsigned int save;
35980 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35981 "#pragma omp task", pragma_tok);
35982 block = begin_omp_task ();
35983 save = cp_parser_begin_omp_structured_block (parser);
35984 cp_parser_statement (parser, NULL_TREE, false, if_p);
35985 cp_parser_end_omp_structured_block (parser, save);
35986 return finish_omp_task (clauses, block);
35989 /* OpenMP 3.0:
35990 # pragma omp taskwait new-line */
35992 static void
35993 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35995 cp_parser_require_pragma_eol (parser, pragma_tok);
35996 finish_omp_taskwait ();
35999 /* OpenMP 3.1:
36000 # pragma omp taskyield new-line */
36002 static void
36003 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
36005 cp_parser_require_pragma_eol (parser, pragma_tok);
36006 finish_omp_taskyield ();
36009 /* OpenMP 4.0:
36010 # pragma omp taskgroup new-line
36011 structured-block */
36013 static tree
36014 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36016 cp_parser_require_pragma_eol (parser, pragma_tok);
36017 return c_finish_omp_taskgroup (input_location,
36018 cp_parser_omp_structured_block (parser,
36019 if_p));
36023 /* OpenMP 2.5:
36024 # pragma omp threadprivate (variable-list) */
36026 static void
36027 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36029 tree vars;
36031 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36032 cp_parser_require_pragma_eol (parser, pragma_tok);
36034 finish_omp_threadprivate (vars);
36037 /* OpenMP 4.0:
36038 # pragma omp cancel cancel-clause[optseq] new-line */
36040 #define OMP_CANCEL_CLAUSE_MASK \
36041 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36047 static void
36048 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36050 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36051 "#pragma omp cancel", pragma_tok);
36052 finish_omp_cancel (clauses);
36055 /* OpenMP 4.0:
36056 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36058 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36059 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36064 static void
36065 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36066 enum pragma_context context)
36068 tree clauses;
36069 bool point_seen = false;
36071 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36073 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36074 const char *p = IDENTIFIER_POINTER (id);
36076 if (strcmp (p, "point") == 0)
36078 cp_lexer_consume_token (parser->lexer);
36079 point_seen = true;
36082 if (!point_seen)
36084 cp_parser_error (parser, "expected %<point%>");
36085 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36086 return;
36089 if (context != pragma_compound)
36091 if (context == pragma_stmt)
36092 error_at (pragma_tok->location,
36093 "%<#pragma %s%> may only be used in compound statements",
36094 "omp cancellation point");
36095 else
36096 cp_parser_error (parser, "expected declaration specifiers");
36097 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36098 return;
36101 clauses = cp_parser_omp_all_clauses (parser,
36102 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36103 "#pragma omp cancellation point",
36104 pragma_tok);
36105 finish_omp_cancellation_point (clauses);
36108 /* OpenMP 4.0:
36109 #pragma omp distribute distribute-clause[optseq] new-line
36110 for-loop */
36112 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36113 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36119 static tree
36120 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36121 char *p_name, omp_clause_mask mask, tree *cclauses,
36122 bool *if_p)
36124 tree clauses, sb, ret;
36125 unsigned int save;
36126 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36128 strcat (p_name, " distribute");
36129 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36131 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36133 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36134 const char *p = IDENTIFIER_POINTER (id);
36135 bool simd = false;
36136 bool parallel = false;
36138 if (strcmp (p, "simd") == 0)
36139 simd = true;
36140 else
36141 parallel = strcmp (p, "parallel") == 0;
36142 if (parallel || simd)
36144 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36145 if (cclauses == NULL)
36146 cclauses = cclauses_buf;
36147 cp_lexer_consume_token (parser->lexer);
36148 if (!flag_openmp) /* flag_openmp_simd */
36150 if (simd)
36151 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36152 cclauses, if_p);
36153 else
36154 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36155 cclauses, if_p);
36157 sb = begin_omp_structured_block ();
36158 save = cp_parser_begin_omp_structured_block (parser);
36159 if (simd)
36160 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36161 cclauses, if_p);
36162 else
36163 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36164 cclauses, if_p);
36165 cp_parser_end_omp_structured_block (parser, save);
36166 tree body = finish_omp_structured_block (sb);
36167 if (ret == NULL)
36168 return ret;
36169 ret = make_node (OMP_DISTRIBUTE);
36170 TREE_TYPE (ret) = void_type_node;
36171 OMP_FOR_BODY (ret) = body;
36172 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36173 SET_EXPR_LOCATION (ret, loc);
36174 add_stmt (ret);
36175 return ret;
36178 if (!flag_openmp) /* flag_openmp_simd */
36180 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36181 return NULL_TREE;
36184 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36185 cclauses == NULL);
36186 if (cclauses)
36188 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36189 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36192 sb = begin_omp_structured_block ();
36193 save = cp_parser_begin_omp_structured_block (parser);
36195 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36197 cp_parser_end_omp_structured_block (parser, save);
36198 add_stmt (finish_omp_structured_block (sb));
36200 return ret;
36203 /* OpenMP 4.0:
36204 # pragma omp teams teams-clause[optseq] new-line
36205 structured-block */
36207 #define OMP_TEAMS_CLAUSE_MASK \
36208 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36216 static tree
36217 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36218 char *p_name, omp_clause_mask mask, tree *cclauses,
36219 bool *if_p)
36221 tree clauses, sb, ret;
36222 unsigned int save;
36223 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36225 strcat (p_name, " teams");
36226 mask |= OMP_TEAMS_CLAUSE_MASK;
36228 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36230 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36231 const char *p = IDENTIFIER_POINTER (id);
36232 if (strcmp (p, "distribute") == 0)
36234 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36235 if (cclauses == NULL)
36236 cclauses = cclauses_buf;
36238 cp_lexer_consume_token (parser->lexer);
36239 if (!flag_openmp) /* flag_openmp_simd */
36240 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36241 cclauses, if_p);
36242 sb = begin_omp_structured_block ();
36243 save = cp_parser_begin_omp_structured_block (parser);
36244 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36245 cclauses, if_p);
36246 cp_parser_end_omp_structured_block (parser, save);
36247 tree body = finish_omp_structured_block (sb);
36248 if (ret == NULL)
36249 return ret;
36250 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36251 ret = make_node (OMP_TEAMS);
36252 TREE_TYPE (ret) = void_type_node;
36253 OMP_TEAMS_CLAUSES (ret) = clauses;
36254 OMP_TEAMS_BODY (ret) = body;
36255 OMP_TEAMS_COMBINED (ret) = 1;
36256 SET_EXPR_LOCATION (ret, loc);
36257 return add_stmt (ret);
36260 if (!flag_openmp) /* flag_openmp_simd */
36262 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36263 return NULL_TREE;
36266 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36267 cclauses == NULL);
36268 if (cclauses)
36270 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36271 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36274 tree stmt = make_node (OMP_TEAMS);
36275 TREE_TYPE (stmt) = void_type_node;
36276 OMP_TEAMS_CLAUSES (stmt) = clauses;
36277 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36278 SET_EXPR_LOCATION (stmt, loc);
36280 return add_stmt (stmt);
36283 /* OpenMP 4.0:
36284 # pragma omp target data target-data-clause[optseq] new-line
36285 structured-block */
36287 #define OMP_TARGET_DATA_CLAUSE_MASK \
36288 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36293 static tree
36294 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36296 tree clauses
36297 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36298 "#pragma omp target data", pragma_tok);
36299 int map_seen = 0;
36300 for (tree *pc = &clauses; *pc;)
36302 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36303 switch (OMP_CLAUSE_MAP_KIND (*pc))
36305 case GOMP_MAP_TO:
36306 case GOMP_MAP_ALWAYS_TO:
36307 case GOMP_MAP_FROM:
36308 case GOMP_MAP_ALWAYS_FROM:
36309 case GOMP_MAP_TOFROM:
36310 case GOMP_MAP_ALWAYS_TOFROM:
36311 case GOMP_MAP_ALLOC:
36312 map_seen = 3;
36313 break;
36314 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36315 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36316 case GOMP_MAP_ALWAYS_POINTER:
36317 break;
36318 default:
36319 map_seen |= 1;
36320 error_at (OMP_CLAUSE_LOCATION (*pc),
36321 "%<#pragma omp target data%> with map-type other "
36322 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36323 "on %<map%> clause");
36324 *pc = OMP_CLAUSE_CHAIN (*pc);
36325 continue;
36327 pc = &OMP_CLAUSE_CHAIN (*pc);
36330 if (map_seen != 3)
36332 if (map_seen == 0)
36333 error_at (pragma_tok->location,
36334 "%<#pragma omp target data%> must contain at least "
36335 "one %<map%> clause");
36336 return NULL_TREE;
36339 tree stmt = make_node (OMP_TARGET_DATA);
36340 TREE_TYPE (stmt) = void_type_node;
36341 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36343 keep_next_level (true);
36344 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36346 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36347 return add_stmt (stmt);
36350 /* OpenMP 4.5:
36351 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36352 structured-block */
36354 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36355 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36361 static tree
36362 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36363 enum pragma_context context)
36365 bool data_seen = false;
36366 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36368 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36369 const char *p = IDENTIFIER_POINTER (id);
36371 if (strcmp (p, "data") == 0)
36373 cp_lexer_consume_token (parser->lexer);
36374 data_seen = true;
36377 if (!data_seen)
36379 cp_parser_error (parser, "expected %<data%>");
36380 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36381 return NULL_TREE;
36384 if (context == pragma_stmt)
36386 error_at (pragma_tok->location,
36387 "%<#pragma %s%> may only be used in compound statements",
36388 "omp target enter data");
36389 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36390 return NULL_TREE;
36393 tree clauses
36394 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36395 "#pragma omp target enter data", pragma_tok);
36396 int map_seen = 0;
36397 for (tree *pc = &clauses; *pc;)
36399 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36400 switch (OMP_CLAUSE_MAP_KIND (*pc))
36402 case GOMP_MAP_TO:
36403 case GOMP_MAP_ALWAYS_TO:
36404 case GOMP_MAP_ALLOC:
36405 map_seen = 3;
36406 break;
36407 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36408 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36409 case GOMP_MAP_ALWAYS_POINTER:
36410 break;
36411 default:
36412 map_seen |= 1;
36413 error_at (OMP_CLAUSE_LOCATION (*pc),
36414 "%<#pragma omp target enter data%> with map-type other "
36415 "than %<to%> or %<alloc%> on %<map%> clause");
36416 *pc = OMP_CLAUSE_CHAIN (*pc);
36417 continue;
36419 pc = &OMP_CLAUSE_CHAIN (*pc);
36422 if (map_seen != 3)
36424 if (map_seen == 0)
36425 error_at (pragma_tok->location,
36426 "%<#pragma omp target enter data%> must contain at least "
36427 "one %<map%> clause");
36428 return NULL_TREE;
36431 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36432 TREE_TYPE (stmt) = void_type_node;
36433 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36434 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36435 return add_stmt (stmt);
36438 /* OpenMP 4.5:
36439 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36440 structured-block */
36442 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36443 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36449 static tree
36450 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36451 enum pragma_context context)
36453 bool data_seen = false;
36454 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36456 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36457 const char *p = IDENTIFIER_POINTER (id);
36459 if (strcmp (p, "data") == 0)
36461 cp_lexer_consume_token (parser->lexer);
36462 data_seen = true;
36465 if (!data_seen)
36467 cp_parser_error (parser, "expected %<data%>");
36468 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36469 return NULL_TREE;
36472 if (context == pragma_stmt)
36474 error_at (pragma_tok->location,
36475 "%<#pragma %s%> may only be used in compound statements",
36476 "omp target exit data");
36477 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36478 return NULL_TREE;
36481 tree clauses
36482 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36483 "#pragma omp target exit data", pragma_tok);
36484 int map_seen = 0;
36485 for (tree *pc = &clauses; *pc;)
36487 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36488 switch (OMP_CLAUSE_MAP_KIND (*pc))
36490 case GOMP_MAP_FROM:
36491 case GOMP_MAP_ALWAYS_FROM:
36492 case GOMP_MAP_RELEASE:
36493 case GOMP_MAP_DELETE:
36494 map_seen = 3;
36495 break;
36496 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36497 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36498 case GOMP_MAP_ALWAYS_POINTER:
36499 break;
36500 default:
36501 map_seen |= 1;
36502 error_at (OMP_CLAUSE_LOCATION (*pc),
36503 "%<#pragma omp target exit data%> with map-type other "
36504 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36505 " clause");
36506 *pc = OMP_CLAUSE_CHAIN (*pc);
36507 continue;
36509 pc = &OMP_CLAUSE_CHAIN (*pc);
36512 if (map_seen != 3)
36514 if (map_seen == 0)
36515 error_at (pragma_tok->location,
36516 "%<#pragma omp target exit data%> must contain at least "
36517 "one %<map%> clause");
36518 return NULL_TREE;
36521 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36522 TREE_TYPE (stmt) = void_type_node;
36523 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36524 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36525 return add_stmt (stmt);
36528 /* OpenMP 4.0:
36529 # pragma omp target update target-update-clause[optseq] new-line */
36531 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36532 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36539 static bool
36540 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36541 enum pragma_context context)
36543 if (context == pragma_stmt)
36545 error_at (pragma_tok->location,
36546 "%<#pragma %s%> may only be used in compound statements",
36547 "omp target update");
36548 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36549 return false;
36552 tree clauses
36553 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36554 "#pragma omp target update", pragma_tok);
36555 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36556 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36558 error_at (pragma_tok->location,
36559 "%<#pragma omp target update%> must contain at least one "
36560 "%<from%> or %<to%> clauses");
36561 return false;
36564 tree stmt = make_node (OMP_TARGET_UPDATE);
36565 TREE_TYPE (stmt) = void_type_node;
36566 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36567 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36568 add_stmt (stmt);
36569 return false;
36572 /* OpenMP 4.0:
36573 # pragma omp target target-clause[optseq] new-line
36574 structured-block */
36576 #define OMP_TARGET_CLAUSE_MASK \
36577 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36587 static bool
36588 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36589 enum pragma_context context, bool *if_p)
36591 tree *pc = NULL, stmt;
36593 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36595 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36596 const char *p = IDENTIFIER_POINTER (id);
36597 enum tree_code ccode = ERROR_MARK;
36599 if (strcmp (p, "teams") == 0)
36600 ccode = OMP_TEAMS;
36601 else if (strcmp (p, "parallel") == 0)
36602 ccode = OMP_PARALLEL;
36603 else if (strcmp (p, "simd") == 0)
36604 ccode = OMP_SIMD;
36605 if (ccode != ERROR_MARK)
36607 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36608 char p_name[sizeof ("#pragma omp target teams distribute "
36609 "parallel for simd")];
36611 cp_lexer_consume_token (parser->lexer);
36612 strcpy (p_name, "#pragma omp target");
36613 if (!flag_openmp) /* flag_openmp_simd */
36615 tree stmt;
36616 switch (ccode)
36618 case OMP_TEAMS:
36619 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36620 OMP_TARGET_CLAUSE_MASK,
36621 cclauses, if_p);
36622 break;
36623 case OMP_PARALLEL:
36624 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36625 OMP_TARGET_CLAUSE_MASK,
36626 cclauses, if_p);
36627 break;
36628 case OMP_SIMD:
36629 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36630 OMP_TARGET_CLAUSE_MASK,
36631 cclauses, if_p);
36632 break;
36633 default:
36634 gcc_unreachable ();
36636 return stmt != NULL_TREE;
36638 keep_next_level (true);
36639 tree sb = begin_omp_structured_block (), ret;
36640 unsigned save = cp_parser_begin_omp_structured_block (parser);
36641 switch (ccode)
36643 case OMP_TEAMS:
36644 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36645 OMP_TARGET_CLAUSE_MASK, cclauses,
36646 if_p);
36647 break;
36648 case OMP_PARALLEL:
36649 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36650 OMP_TARGET_CLAUSE_MASK, cclauses,
36651 if_p);
36652 break;
36653 case OMP_SIMD:
36654 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36655 OMP_TARGET_CLAUSE_MASK, cclauses,
36656 if_p);
36657 break;
36658 default:
36659 gcc_unreachable ();
36661 cp_parser_end_omp_structured_block (parser, save);
36662 tree body = finish_omp_structured_block (sb);
36663 if (ret == NULL_TREE)
36664 return false;
36665 if (ccode == OMP_TEAMS && !processing_template_decl)
36667 /* For combined target teams, ensure the num_teams and
36668 thread_limit clause expressions are evaluated on the host,
36669 before entering the target construct. */
36670 tree c;
36671 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36672 c; c = OMP_CLAUSE_CHAIN (c))
36673 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36674 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36675 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36677 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36678 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36679 if (expr == error_mark_node)
36680 continue;
36681 tree tmp = TARGET_EXPR_SLOT (expr);
36682 add_stmt (expr);
36683 OMP_CLAUSE_OPERAND (c, 0) = expr;
36684 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36685 OMP_CLAUSE_FIRSTPRIVATE);
36686 OMP_CLAUSE_DECL (tc) = tmp;
36687 OMP_CLAUSE_CHAIN (tc)
36688 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36689 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36692 tree stmt = make_node (OMP_TARGET);
36693 TREE_TYPE (stmt) = void_type_node;
36694 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36695 OMP_TARGET_BODY (stmt) = body;
36696 OMP_TARGET_COMBINED (stmt) = 1;
36697 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36698 add_stmt (stmt);
36699 pc = &OMP_TARGET_CLAUSES (stmt);
36700 goto check_clauses;
36702 else if (!flag_openmp) /* flag_openmp_simd */
36704 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36705 return false;
36707 else if (strcmp (p, "data") == 0)
36709 cp_lexer_consume_token (parser->lexer);
36710 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36711 return true;
36713 else if (strcmp (p, "enter") == 0)
36715 cp_lexer_consume_token (parser->lexer);
36716 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36717 return false;
36719 else if (strcmp (p, "exit") == 0)
36721 cp_lexer_consume_token (parser->lexer);
36722 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36723 return false;
36725 else if (strcmp (p, "update") == 0)
36727 cp_lexer_consume_token (parser->lexer);
36728 return cp_parser_omp_target_update (parser, pragma_tok, context);
36731 if (!flag_openmp) /* flag_openmp_simd */
36733 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36734 return false;
36737 stmt = make_node (OMP_TARGET);
36738 TREE_TYPE (stmt) = void_type_node;
36740 OMP_TARGET_CLAUSES (stmt)
36741 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36742 "#pragma omp target", pragma_tok);
36743 pc = &OMP_TARGET_CLAUSES (stmt);
36744 keep_next_level (true);
36745 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36747 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36748 add_stmt (stmt);
36750 check_clauses:
36751 while (*pc)
36753 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36754 switch (OMP_CLAUSE_MAP_KIND (*pc))
36756 case GOMP_MAP_TO:
36757 case GOMP_MAP_ALWAYS_TO:
36758 case GOMP_MAP_FROM:
36759 case GOMP_MAP_ALWAYS_FROM:
36760 case GOMP_MAP_TOFROM:
36761 case GOMP_MAP_ALWAYS_TOFROM:
36762 case GOMP_MAP_ALLOC:
36763 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36764 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36765 case GOMP_MAP_ALWAYS_POINTER:
36766 break;
36767 default:
36768 error_at (OMP_CLAUSE_LOCATION (*pc),
36769 "%<#pragma omp target%> with map-type other "
36770 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36771 "on %<map%> clause");
36772 *pc = OMP_CLAUSE_CHAIN (*pc);
36773 continue;
36775 pc = &OMP_CLAUSE_CHAIN (*pc);
36777 return true;
36780 /* OpenACC 2.0:
36781 # pragma acc cache (variable-list) new-line
36784 static tree
36785 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36787 tree stmt, clauses;
36789 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36790 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36792 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36794 stmt = make_node (OACC_CACHE);
36795 TREE_TYPE (stmt) = void_type_node;
36796 OACC_CACHE_CLAUSES (stmt) = clauses;
36797 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36798 add_stmt (stmt);
36800 return stmt;
36803 /* OpenACC 2.0:
36804 # pragma acc data oacc-data-clause[optseq] new-line
36805 structured-block */
36807 #define OACC_DATA_CLAUSE_MASK \
36808 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36816 static tree
36817 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36819 tree stmt, clauses, block;
36820 unsigned int save;
36822 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36823 "#pragma acc data", pragma_tok);
36825 block = begin_omp_parallel ();
36826 save = cp_parser_begin_omp_structured_block (parser);
36827 cp_parser_statement (parser, NULL_TREE, false, if_p);
36828 cp_parser_end_omp_structured_block (parser, save);
36829 stmt = finish_oacc_data (clauses, block);
36830 return stmt;
36833 /* OpenACC 2.0:
36834 # pragma acc host_data <clauses> new-line
36835 structured-block */
36837 #define OACC_HOST_DATA_CLAUSE_MASK \
36838 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36840 static tree
36841 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36843 tree stmt, clauses, block;
36844 unsigned int save;
36846 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36847 "#pragma acc host_data", pragma_tok);
36849 block = begin_omp_parallel ();
36850 save = cp_parser_begin_omp_structured_block (parser);
36851 cp_parser_statement (parser, NULL_TREE, false, if_p);
36852 cp_parser_end_omp_structured_block (parser, save);
36853 stmt = finish_oacc_host_data (clauses, block);
36854 return stmt;
36857 /* OpenACC 2.0:
36858 # pragma acc declare oacc-data-clause[optseq] new-line
36861 #define OACC_DECLARE_CLAUSE_MASK \
36862 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36871 static tree
36872 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36874 tree clauses, stmt;
36875 bool error = false;
36877 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36878 "#pragma acc declare", pragma_tok, true);
36881 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36883 error_at (pragma_tok->location,
36884 "no valid clauses specified in %<#pragma acc declare%>");
36885 return NULL_TREE;
36888 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36890 location_t loc = OMP_CLAUSE_LOCATION (t);
36891 tree decl = OMP_CLAUSE_DECL (t);
36892 if (!DECL_P (decl))
36894 error_at (loc, "array section in %<#pragma acc declare%>");
36895 error = true;
36896 continue;
36898 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36899 switch (OMP_CLAUSE_MAP_KIND (t))
36901 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36902 case GOMP_MAP_ALLOC:
36903 case GOMP_MAP_TO:
36904 case GOMP_MAP_FORCE_DEVICEPTR:
36905 case GOMP_MAP_DEVICE_RESIDENT:
36906 break;
36908 case GOMP_MAP_LINK:
36909 if (!global_bindings_p ()
36910 && (TREE_STATIC (decl)
36911 || !DECL_EXTERNAL (decl)))
36913 error_at (loc,
36914 "%qD must be a global variable in "
36915 "%<#pragma acc declare link%>",
36916 decl);
36917 error = true;
36918 continue;
36920 break;
36922 default:
36923 if (global_bindings_p ())
36925 error_at (loc, "invalid OpenACC clause at file scope");
36926 error = true;
36927 continue;
36929 if (DECL_EXTERNAL (decl))
36931 error_at (loc,
36932 "invalid use of %<extern%> variable %qD "
36933 "in %<#pragma acc declare%>", decl);
36934 error = true;
36935 continue;
36937 else if (TREE_PUBLIC (decl))
36939 error_at (loc,
36940 "invalid use of %<global%> variable %qD "
36941 "in %<#pragma acc declare%>", decl);
36942 error = true;
36943 continue;
36945 break;
36948 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36949 || lookup_attribute ("omp declare target link",
36950 DECL_ATTRIBUTES (decl)))
36952 error_at (loc, "variable %qD used more than once with "
36953 "%<#pragma acc declare%>", decl);
36954 error = true;
36955 continue;
36958 if (!error)
36960 tree id;
36962 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36963 id = get_identifier ("omp declare target link");
36964 else
36965 id = get_identifier ("omp declare target");
36967 DECL_ATTRIBUTES (decl)
36968 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36969 if (global_bindings_p ())
36971 symtab_node *node = symtab_node::get (decl);
36972 if (node != NULL)
36974 node->offloadable = 1;
36975 if (ENABLE_OFFLOADING)
36977 g->have_offload = true;
36978 if (is_a <varpool_node *> (node))
36979 vec_safe_push (offload_vars, decl);
36986 if (error || global_bindings_p ())
36987 return NULL_TREE;
36989 stmt = make_node (OACC_DECLARE);
36990 TREE_TYPE (stmt) = void_type_node;
36991 OACC_DECLARE_CLAUSES (stmt) = clauses;
36992 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36994 add_stmt (stmt);
36996 return NULL_TREE;
36999 /* OpenACC 2.0:
37000 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37004 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37006 LOC is the location of the #pragma token.
37009 #define OACC_ENTER_DATA_CLAUSE_MASK \
37010 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37016 #define OACC_EXIT_DATA_CLAUSE_MASK \
37017 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37024 static tree
37025 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37026 bool enter)
37028 location_t loc = pragma_tok->location;
37029 tree stmt, clauses;
37030 const char *p = "";
37032 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37033 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37035 if (strcmp (p, "data") != 0)
37037 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37038 enter ? "enter" : "exit");
37039 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37040 return NULL_TREE;
37043 cp_lexer_consume_token (parser->lexer);
37045 if (enter)
37046 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37047 "#pragma acc enter data", pragma_tok);
37048 else
37049 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37050 "#pragma acc exit data", pragma_tok);
37052 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37054 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37055 enter ? "enter" : "exit");
37056 return NULL_TREE;
37059 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37060 TREE_TYPE (stmt) = void_type_node;
37061 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37062 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37063 add_stmt (stmt);
37064 return stmt;
37067 /* OpenACC 2.0:
37068 # pragma acc loop oacc-loop-clause[optseq] new-line
37069 structured-block */
37071 #define OACC_LOOP_CLAUSE_MASK \
37072 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37083 static tree
37084 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37085 omp_clause_mask mask, tree *cclauses, bool *if_p)
37087 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37089 strcat (p_name, " loop");
37090 mask |= OACC_LOOP_CLAUSE_MASK;
37092 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37093 cclauses == NULL);
37094 if (cclauses)
37096 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37097 if (*cclauses)
37098 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37099 if (clauses)
37100 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37103 tree block = begin_omp_structured_block ();
37104 int save = cp_parser_begin_omp_structured_block (parser);
37105 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37106 cp_parser_end_omp_structured_block (parser, save);
37107 add_stmt (finish_omp_structured_block (block));
37109 return stmt;
37112 /* OpenACC 2.0:
37113 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37114 structured-block
37118 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37119 structured-block
37122 #define OACC_KERNELS_CLAUSE_MASK \
37123 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37137 #define OACC_PARALLEL_CLAUSE_MASK \
37138 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37155 static tree
37156 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37157 char *p_name, bool *if_p)
37159 omp_clause_mask mask;
37160 enum tree_code code;
37161 switch (cp_parser_pragma_kind (pragma_tok))
37163 case PRAGMA_OACC_KERNELS:
37164 strcat (p_name, " kernels");
37165 mask = OACC_KERNELS_CLAUSE_MASK;
37166 code = OACC_KERNELS;
37167 break;
37168 case PRAGMA_OACC_PARALLEL:
37169 strcat (p_name, " parallel");
37170 mask = OACC_PARALLEL_CLAUSE_MASK;
37171 code = OACC_PARALLEL;
37172 break;
37173 default:
37174 gcc_unreachable ();
37177 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37179 const char *p
37180 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37181 if (strcmp (p, "loop") == 0)
37183 cp_lexer_consume_token (parser->lexer);
37184 tree block = begin_omp_parallel ();
37185 tree clauses;
37186 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37187 if_p);
37188 return finish_omp_construct (code, block, clauses);
37192 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37194 tree block = begin_omp_parallel ();
37195 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37196 cp_parser_statement (parser, NULL_TREE, false, if_p);
37197 cp_parser_end_omp_structured_block (parser, save);
37198 return finish_omp_construct (code, block, clauses);
37201 /* OpenACC 2.0:
37202 # pragma acc update oacc-update-clause[optseq] new-line
37205 #define OACC_UPDATE_CLAUSE_MASK \
37206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37213 static tree
37214 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37216 tree stmt, clauses;
37218 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37219 "#pragma acc update", pragma_tok);
37221 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37223 error_at (pragma_tok->location,
37224 "%<#pragma acc update%> must contain at least one "
37225 "%<device%> or %<host%> or %<self%> clause");
37226 return NULL_TREE;
37229 stmt = make_node (OACC_UPDATE);
37230 TREE_TYPE (stmt) = void_type_node;
37231 OACC_UPDATE_CLAUSES (stmt) = clauses;
37232 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37233 add_stmt (stmt);
37234 return stmt;
37237 /* OpenACC 2.0:
37238 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37240 LOC is the location of the #pragma token.
37243 #define OACC_WAIT_CLAUSE_MASK \
37244 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37246 static tree
37247 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37249 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37250 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37252 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37253 list = cp_parser_oacc_wait_list (parser, loc, list);
37255 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37256 "#pragma acc wait", pragma_tok);
37258 stmt = c_finish_oacc_wait (loc, list, clauses);
37259 stmt = finish_expr_stmt (stmt);
37261 return stmt;
37264 /* OpenMP 4.0:
37265 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37267 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37268 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37275 static void
37276 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37277 enum pragma_context context)
37279 bool first_p = parser->omp_declare_simd == NULL;
37280 cp_omp_declare_simd_data data;
37281 if (first_p)
37283 data.error_seen = false;
37284 data.fndecl_seen = false;
37285 data.tokens = vNULL;
37286 data.clauses = NULL_TREE;
37287 /* It is safe to take the address of a local variable; it will only be
37288 used while this scope is live. */
37289 parser->omp_declare_simd = &data;
37292 /* Store away all pragma tokens. */
37293 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37294 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37295 cp_lexer_consume_token (parser->lexer);
37296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37297 parser->omp_declare_simd->error_seen = true;
37298 cp_parser_require_pragma_eol (parser, pragma_tok);
37299 struct cp_token_cache *cp
37300 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37301 parser->omp_declare_simd->tokens.safe_push (cp);
37303 if (first_p)
37305 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37306 cp_parser_pragma (parser, context, NULL);
37307 switch (context)
37309 case pragma_external:
37310 cp_parser_declaration (parser);
37311 break;
37312 case pragma_member:
37313 cp_parser_member_declaration (parser);
37314 break;
37315 case pragma_objc_icode:
37316 cp_parser_block_declaration (parser, /*statement_p=*/false);
37317 break;
37318 default:
37319 cp_parser_declaration_statement (parser);
37320 break;
37322 if (parser->omp_declare_simd
37323 && !parser->omp_declare_simd->error_seen
37324 && !parser->omp_declare_simd->fndecl_seen)
37325 error_at (pragma_tok->location,
37326 "%<#pragma omp declare simd%> not immediately followed by "
37327 "function declaration or definition");
37328 data.tokens.release ();
37329 parser->omp_declare_simd = NULL;
37333 /* Finalize #pragma omp declare simd clauses after direct declarator has
37334 been parsed, and put that into "omp declare simd" attribute. */
37336 static tree
37337 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37339 struct cp_token_cache *ce;
37340 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37341 int i;
37343 if (!data->error_seen && data->fndecl_seen)
37345 error ("%<#pragma omp declare simd%> not immediately followed by "
37346 "a single function declaration or definition");
37347 data->error_seen = true;
37349 if (data->error_seen)
37350 return attrs;
37352 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37354 tree c, cl;
37356 cp_parser_push_lexer_for_tokens (parser, ce);
37357 parser->lexer->in_pragma = true;
37358 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37359 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37360 cp_lexer_consume_token (parser->lexer);
37361 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37362 "#pragma omp declare simd", pragma_tok);
37363 cp_parser_pop_lexer (parser);
37364 if (cl)
37365 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37366 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37367 TREE_CHAIN (c) = attrs;
37368 if (processing_template_decl)
37369 ATTR_IS_DEPENDENT (c) = 1;
37370 attrs = c;
37373 data->fndecl_seen = true;
37374 return attrs;
37378 /* OpenMP 4.0:
37379 # pragma omp declare target new-line
37380 declarations and definitions
37381 # pragma omp end declare target new-line
37383 OpenMP 4.5:
37384 # pragma omp declare target ( extended-list ) new-line
37386 # pragma omp declare target declare-target-clauses[seq] new-line */
37388 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37389 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37392 static void
37393 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37395 tree clauses = NULL_TREE;
37396 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37397 clauses
37398 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37399 "#pragma omp declare target", pragma_tok);
37400 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37402 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37403 clauses);
37404 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37405 cp_parser_require_pragma_eol (parser, pragma_tok);
37407 else
37409 cp_parser_require_pragma_eol (parser, pragma_tok);
37410 scope_chain->omp_declare_target_attribute++;
37411 return;
37413 if (scope_chain->omp_declare_target_attribute)
37414 error_at (pragma_tok->location,
37415 "%<#pragma omp declare target%> with clauses in between "
37416 "%<#pragma omp declare target%> without clauses and "
37417 "%<#pragma omp end declare target%>");
37418 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37420 tree t = OMP_CLAUSE_DECL (c), id;
37421 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37422 tree at2 = lookup_attribute ("omp declare target link",
37423 DECL_ATTRIBUTES (t));
37424 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37426 id = get_identifier ("omp declare target link");
37427 std::swap (at1, at2);
37429 else
37430 id = get_identifier ("omp declare target");
37431 if (at2)
37433 error_at (OMP_CLAUSE_LOCATION (c),
37434 "%qD specified both in declare target %<link%> and %<to%>"
37435 " clauses", t);
37436 continue;
37438 if (!at1)
37440 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37441 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37442 continue;
37444 symtab_node *node = symtab_node::get (t);
37445 if (node != NULL)
37447 node->offloadable = 1;
37448 if (ENABLE_OFFLOADING)
37450 g->have_offload = true;
37451 if (is_a <varpool_node *> (node))
37452 vec_safe_push (offload_vars, t);
37459 static void
37460 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37462 const char *p = "";
37463 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37465 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37466 p = IDENTIFIER_POINTER (id);
37468 if (strcmp (p, "declare") == 0)
37470 cp_lexer_consume_token (parser->lexer);
37471 p = "";
37472 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37474 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37475 p = IDENTIFIER_POINTER (id);
37477 if (strcmp (p, "target") == 0)
37478 cp_lexer_consume_token (parser->lexer);
37479 else
37481 cp_parser_error (parser, "expected %<target%>");
37482 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37483 return;
37486 else
37488 cp_parser_error (parser, "expected %<declare%>");
37489 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37490 return;
37492 cp_parser_require_pragma_eol (parser, pragma_tok);
37493 if (!scope_chain->omp_declare_target_attribute)
37494 error_at (pragma_tok->location,
37495 "%<#pragma omp end declare target%> without corresponding "
37496 "%<#pragma omp declare target%>");
37497 else
37498 scope_chain->omp_declare_target_attribute--;
37501 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37502 expression and optional initializer clause of
37503 #pragma omp declare reduction. We store the expression(s) as
37504 either 3, 6 or 7 special statements inside of the artificial function's
37505 body. The first two statements are DECL_EXPRs for the artificial
37506 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37507 expression that uses those variables.
37508 If there was any INITIALIZER clause, this is followed by further statements,
37509 the fourth and fifth statements are DECL_EXPRs for the artificial
37510 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37511 constructor variant (first token after open paren is not omp_priv),
37512 then the sixth statement is a statement with the function call expression
37513 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37514 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37515 to initialize the OMP_PRIV artificial variable and there is seventh
37516 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37518 static bool
37519 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37521 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37522 gcc_assert (TYPE_REF_P (type));
37523 type = TREE_TYPE (type);
37524 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37525 DECL_ARTIFICIAL (omp_out) = 1;
37526 pushdecl (omp_out);
37527 add_decl_expr (omp_out);
37528 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37529 DECL_ARTIFICIAL (omp_in) = 1;
37530 pushdecl (omp_in);
37531 add_decl_expr (omp_in);
37532 tree combiner;
37533 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37535 keep_next_level (true);
37536 tree block = begin_omp_structured_block ();
37537 combiner = cp_parser_expression (parser);
37538 finish_expr_stmt (combiner);
37539 block = finish_omp_structured_block (block);
37540 add_stmt (block);
37542 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37543 return false;
37545 const char *p = "";
37546 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37548 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37549 p = IDENTIFIER_POINTER (id);
37552 if (strcmp (p, "initializer") == 0)
37554 cp_lexer_consume_token (parser->lexer);
37555 matching_parens parens;
37556 if (!parens.require_open (parser))
37557 return false;
37559 p = "";
37560 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37562 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37563 p = IDENTIFIER_POINTER (id);
37566 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37567 DECL_ARTIFICIAL (omp_priv) = 1;
37568 pushdecl (omp_priv);
37569 add_decl_expr (omp_priv);
37570 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37571 DECL_ARTIFICIAL (omp_orig) = 1;
37572 pushdecl (omp_orig);
37573 add_decl_expr (omp_orig);
37575 keep_next_level (true);
37576 block = begin_omp_structured_block ();
37578 bool ctor = false;
37579 if (strcmp (p, "omp_priv") == 0)
37581 bool is_direct_init, is_non_constant_init;
37582 ctor = true;
37583 cp_lexer_consume_token (parser->lexer);
37584 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37585 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37586 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37587 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37588 == CPP_CLOSE_PAREN
37589 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37590 == CPP_CLOSE_PAREN))
37592 finish_omp_structured_block (block);
37593 error ("invalid initializer clause");
37594 return false;
37596 initializer = cp_parser_initializer (parser, &is_direct_init,
37597 &is_non_constant_init);
37598 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37599 NULL_TREE, LOOKUP_ONLYCONVERTING);
37601 else
37603 cp_parser_parse_tentatively (parser);
37604 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37605 /*check_dependency_p=*/true,
37606 /*template_p=*/NULL,
37607 /*declarator_p=*/false,
37608 /*optional_p=*/false);
37609 vec<tree, va_gc> *args;
37610 if (fn_name == error_mark_node
37611 || cp_parser_error_occurred (parser)
37612 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37613 || ((args = cp_parser_parenthesized_expression_list
37614 (parser, non_attr, /*cast_p=*/false,
37615 /*allow_expansion_p=*/true,
37616 /*non_constant_p=*/NULL)),
37617 cp_parser_error_occurred (parser)))
37619 finish_omp_structured_block (block);
37620 cp_parser_abort_tentative_parse (parser);
37621 cp_parser_error (parser, "expected id-expression (arguments)");
37622 return false;
37624 unsigned int i;
37625 tree arg;
37626 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37627 if (arg == omp_priv
37628 || (TREE_CODE (arg) == ADDR_EXPR
37629 && TREE_OPERAND (arg, 0) == omp_priv))
37630 break;
37631 cp_parser_abort_tentative_parse (parser);
37632 if (arg == NULL_TREE)
37633 error ("one of the initializer call arguments should be %<omp_priv%>"
37634 " or %<&omp_priv%>");
37635 initializer = cp_parser_postfix_expression (parser, false, false, false,
37636 false, NULL);
37637 finish_expr_stmt (initializer);
37640 block = finish_omp_structured_block (block);
37641 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37642 add_stmt (block);
37644 if (ctor)
37645 add_decl_expr (omp_orig);
37647 if (!parens.require_close (parser))
37648 return false;
37651 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37652 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37653 UNKNOWN_LOCATION);
37655 return true;
37658 /* OpenMP 4.0
37659 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37660 initializer-clause[opt] new-line
37662 initializer-clause:
37663 initializer (omp_priv initializer)
37664 initializer (function-name (argument-list)) */
37666 static void
37667 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37668 enum pragma_context)
37670 auto_vec<tree> types;
37671 enum tree_code reduc_code = ERROR_MARK;
37672 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37673 unsigned int i;
37674 cp_token *first_token;
37675 cp_token_cache *cp;
37676 int errs;
37677 void *p;
37679 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37680 p = obstack_alloc (&declarator_obstack, 0);
37682 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37683 goto fail;
37685 switch (cp_lexer_peek_token (parser->lexer)->type)
37687 case CPP_PLUS:
37688 reduc_code = PLUS_EXPR;
37689 break;
37690 case CPP_MULT:
37691 reduc_code = MULT_EXPR;
37692 break;
37693 case CPP_MINUS:
37694 reduc_code = MINUS_EXPR;
37695 break;
37696 case CPP_AND:
37697 reduc_code = BIT_AND_EXPR;
37698 break;
37699 case CPP_XOR:
37700 reduc_code = BIT_XOR_EXPR;
37701 break;
37702 case CPP_OR:
37703 reduc_code = BIT_IOR_EXPR;
37704 break;
37705 case CPP_AND_AND:
37706 reduc_code = TRUTH_ANDIF_EXPR;
37707 break;
37708 case CPP_OR_OR:
37709 reduc_code = TRUTH_ORIF_EXPR;
37710 break;
37711 case CPP_NAME:
37712 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37713 break;
37714 default:
37715 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37716 "%<|%>, %<&&%>, %<||%> or identifier");
37717 goto fail;
37720 if (reduc_code != ERROR_MARK)
37721 cp_lexer_consume_token (parser->lexer);
37723 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37724 if (reduc_id == error_mark_node)
37725 goto fail;
37727 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37728 goto fail;
37730 /* Types may not be defined in declare reduction type list. */
37731 const char *saved_message;
37732 saved_message = parser->type_definition_forbidden_message;
37733 parser->type_definition_forbidden_message
37734 = G_("types may not be defined in declare reduction type list");
37735 bool saved_colon_corrects_to_scope_p;
37736 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37737 parser->colon_corrects_to_scope_p = false;
37738 bool saved_colon_doesnt_start_class_def_p;
37739 saved_colon_doesnt_start_class_def_p
37740 = parser->colon_doesnt_start_class_def_p;
37741 parser->colon_doesnt_start_class_def_p = true;
37743 while (true)
37745 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37746 type = cp_parser_type_id (parser);
37747 if (type == error_mark_node)
37749 else if (ARITHMETIC_TYPE_P (type)
37750 && (orig_reduc_id == NULL_TREE
37751 || (TREE_CODE (type) != COMPLEX_TYPE
37752 && (id_equal (orig_reduc_id, "min")
37753 || id_equal (orig_reduc_id, "max")))))
37754 error_at (loc, "predeclared arithmetic type %qT in "
37755 "%<#pragma omp declare reduction%>", type);
37756 else if (TREE_CODE (type) == FUNCTION_TYPE
37757 || TREE_CODE (type) == METHOD_TYPE
37758 || TREE_CODE (type) == ARRAY_TYPE)
37759 error_at (loc, "function or array type %qT in "
37760 "%<#pragma omp declare reduction%>", type);
37761 else if (TYPE_REF_P (type))
37762 error_at (loc, "reference type %qT in "
37763 "%<#pragma omp declare reduction%>", type);
37764 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37765 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37766 "%<#pragma omp declare reduction%>", type);
37767 else
37768 types.safe_push (type);
37770 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37771 cp_lexer_consume_token (parser->lexer);
37772 else
37773 break;
37776 /* Restore the saved message. */
37777 parser->type_definition_forbidden_message = saved_message;
37778 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37779 parser->colon_doesnt_start_class_def_p
37780 = saved_colon_doesnt_start_class_def_p;
37782 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37783 || types.is_empty ())
37785 fail:
37786 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37787 goto done;
37790 first_token = cp_lexer_peek_token (parser->lexer);
37791 cp = NULL;
37792 errs = errorcount;
37793 FOR_EACH_VEC_ELT (types, i, type)
37795 tree fntype
37796 = build_function_type_list (void_type_node,
37797 cp_build_reference_type (type, false),
37798 NULL_TREE);
37799 tree this_reduc_id = reduc_id;
37800 if (!dependent_type_p (type))
37801 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37802 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37803 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37804 DECL_ARTIFICIAL (fndecl) = 1;
37805 DECL_EXTERNAL (fndecl) = 1;
37806 DECL_DECLARED_INLINE_P (fndecl) = 1;
37807 DECL_IGNORED_P (fndecl) = 1;
37808 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37809 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37810 DECL_ATTRIBUTES (fndecl)
37811 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37812 DECL_ATTRIBUTES (fndecl));
37813 if (processing_template_decl)
37814 fndecl = push_template_decl (fndecl);
37815 bool block_scope = false;
37816 tree block = NULL_TREE;
37817 if (current_function_decl)
37819 block_scope = true;
37820 DECL_CONTEXT (fndecl) = global_namespace;
37821 if (!processing_template_decl)
37822 pushdecl (fndecl);
37824 else if (current_class_type)
37826 if (cp == NULL)
37828 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37829 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37830 cp_lexer_consume_token (parser->lexer);
37831 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37832 goto fail;
37833 cp = cp_token_cache_new (first_token,
37834 cp_lexer_peek_nth_token (parser->lexer,
37835 2));
37837 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37838 finish_member_declaration (fndecl);
37839 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37840 DECL_PENDING_INLINE_P (fndecl) = 1;
37841 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37842 continue;
37844 else
37846 DECL_CONTEXT (fndecl) = current_namespace;
37847 pushdecl (fndecl);
37849 if (!block_scope)
37850 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37851 else
37852 block = begin_omp_structured_block ();
37853 if (cp)
37855 cp_parser_push_lexer_for_tokens (parser, cp);
37856 parser->lexer->in_pragma = true;
37858 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37860 if (!block_scope)
37861 finish_function (/*inline_p=*/false);
37862 else
37863 DECL_CONTEXT (fndecl) = current_function_decl;
37864 if (cp)
37865 cp_parser_pop_lexer (parser);
37866 goto fail;
37868 if (cp)
37869 cp_parser_pop_lexer (parser);
37870 if (!block_scope)
37871 finish_function (/*inline_p=*/false);
37872 else
37874 DECL_CONTEXT (fndecl) = current_function_decl;
37875 block = finish_omp_structured_block (block);
37876 if (TREE_CODE (block) == BIND_EXPR)
37877 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37878 else if (TREE_CODE (block) == STATEMENT_LIST)
37879 DECL_SAVED_TREE (fndecl) = block;
37880 if (processing_template_decl)
37881 add_decl_expr (fndecl);
37883 cp_check_omp_declare_reduction (fndecl);
37884 if (cp == NULL && types.length () > 1)
37885 cp = cp_token_cache_new (first_token,
37886 cp_lexer_peek_nth_token (parser->lexer, 2));
37887 if (errs != errorcount)
37888 break;
37891 cp_parser_require_pragma_eol (parser, pragma_tok);
37893 done:
37894 /* Free any declarators allocated. */
37895 obstack_free (&declarator_obstack, p);
37898 /* OpenMP 4.0
37899 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37900 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37901 initializer-clause[opt] new-line
37902 #pragma omp declare target new-line */
37904 static bool
37905 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37906 enum pragma_context context)
37908 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37910 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37911 const char *p = IDENTIFIER_POINTER (id);
37913 if (strcmp (p, "simd") == 0)
37915 cp_lexer_consume_token (parser->lexer);
37916 cp_parser_omp_declare_simd (parser, pragma_tok,
37917 context);
37918 return true;
37920 cp_ensure_no_omp_declare_simd (parser);
37921 if (strcmp (p, "reduction") == 0)
37923 cp_lexer_consume_token (parser->lexer);
37924 cp_parser_omp_declare_reduction (parser, pragma_tok,
37925 context);
37926 return false;
37928 if (!flag_openmp) /* flag_openmp_simd */
37930 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37931 return false;
37933 if (strcmp (p, "target") == 0)
37935 cp_lexer_consume_token (parser->lexer);
37936 cp_parser_omp_declare_target (parser, pragma_tok);
37937 return false;
37940 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37941 "or %<target%>");
37942 cp_parser_require_pragma_eol (parser, pragma_tok);
37943 return false;
37946 /* OpenMP 4.5:
37947 #pragma omp taskloop taskloop-clause[optseq] new-line
37948 for-loop
37950 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37951 for-loop */
37953 #define OMP_TASKLOOP_CLAUSE_MASK \
37954 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37969 static tree
37970 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37971 char *p_name, omp_clause_mask mask, tree *cclauses,
37972 bool *if_p)
37974 tree clauses, sb, ret;
37975 unsigned int save;
37976 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37978 strcat (p_name, " taskloop");
37979 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37981 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37983 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37984 const char *p = IDENTIFIER_POINTER (id);
37986 if (strcmp (p, "simd") == 0)
37988 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37989 if (cclauses == NULL)
37990 cclauses = cclauses_buf;
37992 cp_lexer_consume_token (parser->lexer);
37993 if (!flag_openmp) /* flag_openmp_simd */
37994 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37995 cclauses, if_p);
37996 sb = begin_omp_structured_block ();
37997 save = cp_parser_begin_omp_structured_block (parser);
37998 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37999 cclauses, if_p);
38000 cp_parser_end_omp_structured_block (parser, save);
38001 tree body = finish_omp_structured_block (sb);
38002 if (ret == NULL)
38003 return ret;
38004 ret = make_node (OMP_TASKLOOP);
38005 TREE_TYPE (ret) = void_type_node;
38006 OMP_FOR_BODY (ret) = body;
38007 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38008 SET_EXPR_LOCATION (ret, loc);
38009 add_stmt (ret);
38010 return ret;
38013 if (!flag_openmp) /* flag_openmp_simd */
38015 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38016 return NULL_TREE;
38019 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38020 cclauses == NULL);
38021 if (cclauses)
38023 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38024 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38027 sb = begin_omp_structured_block ();
38028 save = cp_parser_begin_omp_structured_block (parser);
38030 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38031 if_p);
38033 cp_parser_end_omp_structured_block (parser, save);
38034 add_stmt (finish_omp_structured_block (sb));
38036 return ret;
38040 /* OpenACC 2.0:
38041 # pragma acc routine oacc-routine-clause[optseq] new-line
38042 function-definition
38044 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38047 #define OACC_ROUTINE_CLAUSE_MASK \
38048 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38054 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38055 component, which must resolve to a declared namespace-scope
38056 function. The clauses are either processed directly (for a named
38057 function), or defered until the immediatley following declaration
38058 is parsed. */
38060 static void
38061 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38062 enum pragma_context context)
38064 gcc_checking_assert (context == pragma_external);
38065 /* The checking for "another pragma following this one" in the "no optional
38066 '( name )'" case makes sure that we dont re-enter. */
38067 gcc_checking_assert (parser->oacc_routine == NULL);
38069 cp_oacc_routine_data data;
38070 data.error_seen = false;
38071 data.fndecl_seen = false;
38072 data.tokens = vNULL;
38073 data.clauses = NULL_TREE;
38074 data.loc = pragma_tok->location;
38075 /* It is safe to take the address of a local variable; it will only be
38076 used while this scope is live. */
38077 parser->oacc_routine = &data;
38079 /* Look for optional '( name )'. */
38080 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38082 matching_parens parens;
38083 parens.consume_open (parser); /* '(' */
38085 /* We parse the name as an id-expression. If it resolves to
38086 anything other than a non-overloaded function at namespace
38087 scope, it's an error. */
38088 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38089 tree name = cp_parser_id_expression (parser,
38090 /*template_keyword_p=*/false,
38091 /*check_dependency_p=*/false,
38092 /*template_p=*/NULL,
38093 /*declarator_p=*/false,
38094 /*optional_p=*/false);
38095 tree decl = (identifier_p (name)
38096 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38097 : name);
38098 if (name != error_mark_node && decl == error_mark_node)
38099 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38101 if (decl == error_mark_node
38102 || !parens.require_close (parser))
38104 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38105 parser->oacc_routine = NULL;
38106 return;
38109 data.clauses
38110 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38111 "#pragma acc routine",
38112 cp_lexer_peek_token (parser->lexer));
38114 if (decl && is_overloaded_fn (decl)
38115 && (TREE_CODE (decl) != FUNCTION_DECL
38116 || DECL_FUNCTION_TEMPLATE_P (decl)))
38118 error_at (name_loc,
38119 "%<#pragma acc routine%> names a set of overloads");
38120 parser->oacc_routine = NULL;
38121 return;
38124 /* Perhaps we should use the same rule as declarations in different
38125 namespaces? */
38126 if (!DECL_NAMESPACE_SCOPE_P (decl))
38128 error_at (name_loc,
38129 "%qD does not refer to a namespace scope function", decl);
38130 parser->oacc_routine = NULL;
38131 return;
38134 if (TREE_CODE (decl) != FUNCTION_DECL)
38136 error_at (name_loc, "%qD does not refer to a function", decl);
38137 parser->oacc_routine = NULL;
38138 return;
38141 cp_finalize_oacc_routine (parser, decl, false);
38142 parser->oacc_routine = NULL;
38144 else /* No optional '( name )'. */
38146 /* Store away all pragma tokens. */
38147 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38148 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38149 cp_lexer_consume_token (parser->lexer);
38150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38151 parser->oacc_routine->error_seen = true;
38152 cp_parser_require_pragma_eol (parser, pragma_tok);
38153 struct cp_token_cache *cp
38154 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38155 parser->oacc_routine->tokens.safe_push (cp);
38157 /* Emit a helpful diagnostic if there's another pragma following this
38158 one. */
38159 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38161 cp_ensure_no_oacc_routine (parser);
38162 data.tokens.release ();
38163 /* ..., and then just keep going. */
38164 return;
38167 /* We only have to consider the pragma_external case here. */
38168 cp_parser_declaration (parser);
38169 if (parser->oacc_routine
38170 && !parser->oacc_routine->fndecl_seen)
38171 cp_ensure_no_oacc_routine (parser);
38172 else
38173 parser->oacc_routine = NULL;
38174 data.tokens.release ();
38178 /* Finalize #pragma acc routine clauses after direct declarator has
38179 been parsed. */
38181 static tree
38182 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38184 struct cp_token_cache *ce;
38185 cp_oacc_routine_data *data = parser->oacc_routine;
38187 if (!data->error_seen && data->fndecl_seen)
38189 error_at (data->loc,
38190 "%<#pragma acc routine%> not immediately followed by "
38191 "a single function declaration or definition");
38192 data->error_seen = true;
38194 if (data->error_seen)
38195 return attrs;
38197 gcc_checking_assert (data->tokens.length () == 1);
38198 ce = data->tokens[0];
38200 cp_parser_push_lexer_for_tokens (parser, ce);
38201 parser->lexer->in_pragma = true;
38202 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38204 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38205 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38206 parser->oacc_routine->clauses
38207 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38208 "#pragma acc routine", pragma_tok);
38209 cp_parser_pop_lexer (parser);
38210 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38211 fndecl_seen. */
38213 return attrs;
38216 /* Apply any saved OpenACC routine clauses to a just-parsed
38217 declaration. */
38219 static void
38220 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38222 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38224 /* Keep going if we're in error reporting mode. */
38225 if (parser->oacc_routine->error_seen
38226 || fndecl == error_mark_node)
38227 return;
38229 if (parser->oacc_routine->fndecl_seen)
38231 error_at (parser->oacc_routine->loc,
38232 "%<#pragma acc routine%> not immediately followed by"
38233 " a single function declaration or definition");
38234 parser->oacc_routine = NULL;
38235 return;
38237 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38239 cp_ensure_no_oacc_routine (parser);
38240 return;
38243 if (oacc_get_fn_attrib (fndecl))
38245 error_at (parser->oacc_routine->loc,
38246 "%<#pragma acc routine%> already applied to %qD", fndecl);
38247 parser->oacc_routine = NULL;
38248 return;
38251 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38253 error_at (parser->oacc_routine->loc,
38254 TREE_USED (fndecl)
38255 ? G_("%<#pragma acc routine%> must be applied before use")
38256 : G_("%<#pragma acc routine%> must be applied before "
38257 "definition"));
38258 parser->oacc_routine = NULL;
38259 return;
38262 /* Process the routine's dimension clauses. */
38263 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38264 oacc_replace_fn_attrib (fndecl, dims);
38266 /* Add an "omp declare target" attribute. */
38267 DECL_ATTRIBUTES (fndecl)
38268 = tree_cons (get_identifier ("omp declare target"),
38269 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38271 /* Don't unset parser->oacc_routine here: we may still need it to
38272 diagnose wrong usage. But, remember that we've used this "#pragma acc
38273 routine". */
38274 parser->oacc_routine->fndecl_seen = true;
38278 /* Main entry point to OpenMP statement pragmas. */
38280 static void
38281 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38283 tree stmt;
38284 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38285 omp_clause_mask mask (0);
38287 switch (cp_parser_pragma_kind (pragma_tok))
38289 case PRAGMA_OACC_ATOMIC:
38290 cp_parser_omp_atomic (parser, pragma_tok);
38291 return;
38292 case PRAGMA_OACC_CACHE:
38293 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38294 break;
38295 case PRAGMA_OACC_DATA:
38296 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38297 break;
38298 case PRAGMA_OACC_ENTER_DATA:
38299 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38300 break;
38301 case PRAGMA_OACC_EXIT_DATA:
38302 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38303 break;
38304 case PRAGMA_OACC_HOST_DATA:
38305 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38306 break;
38307 case PRAGMA_OACC_KERNELS:
38308 case PRAGMA_OACC_PARALLEL:
38309 strcpy (p_name, "#pragma acc");
38310 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38311 if_p);
38312 break;
38313 case PRAGMA_OACC_LOOP:
38314 strcpy (p_name, "#pragma acc");
38315 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38316 if_p);
38317 break;
38318 case PRAGMA_OACC_UPDATE:
38319 stmt = cp_parser_oacc_update (parser, pragma_tok);
38320 break;
38321 case PRAGMA_OACC_WAIT:
38322 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38323 break;
38324 case PRAGMA_OMP_ATOMIC:
38325 cp_parser_omp_atomic (parser, pragma_tok);
38326 return;
38327 case PRAGMA_OMP_CRITICAL:
38328 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38329 break;
38330 case PRAGMA_OMP_DISTRIBUTE:
38331 strcpy (p_name, "#pragma omp");
38332 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38333 if_p);
38334 break;
38335 case PRAGMA_OMP_FOR:
38336 strcpy (p_name, "#pragma omp");
38337 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38338 if_p);
38339 break;
38340 case PRAGMA_OMP_MASTER:
38341 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38342 break;
38343 case PRAGMA_OMP_PARALLEL:
38344 strcpy (p_name, "#pragma omp");
38345 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38346 if_p);
38347 break;
38348 case PRAGMA_OMP_SECTIONS:
38349 strcpy (p_name, "#pragma omp");
38350 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38351 break;
38352 case PRAGMA_OMP_SIMD:
38353 strcpy (p_name, "#pragma omp");
38354 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38355 if_p);
38356 break;
38357 case PRAGMA_OMP_SINGLE:
38358 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38359 break;
38360 case PRAGMA_OMP_TASK:
38361 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38362 break;
38363 case PRAGMA_OMP_TASKGROUP:
38364 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38365 break;
38366 case PRAGMA_OMP_TASKLOOP:
38367 strcpy (p_name, "#pragma omp");
38368 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38369 if_p);
38370 break;
38371 case PRAGMA_OMP_TEAMS:
38372 strcpy (p_name, "#pragma omp");
38373 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38374 if_p);
38375 break;
38376 default:
38377 gcc_unreachable ();
38380 protected_set_expr_location (stmt, pragma_tok->location);
38383 /* Transactional Memory parsing routines. */
38385 /* Parse a transaction attribute.
38387 txn-attribute:
38388 attribute
38389 [ [ identifier ] ]
38391 We use this instead of cp_parser_attributes_opt for transactions to avoid
38392 the pedwarn in C++98 mode. */
38394 static tree
38395 cp_parser_txn_attribute_opt (cp_parser *parser)
38397 cp_token *token;
38398 tree attr_name, attr = NULL;
38400 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38401 return cp_parser_attributes_opt (parser);
38403 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38404 return NULL_TREE;
38405 cp_lexer_consume_token (parser->lexer);
38406 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38407 goto error1;
38409 token = cp_lexer_peek_token (parser->lexer);
38410 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38412 token = cp_lexer_consume_token (parser->lexer);
38414 attr_name = (token->type == CPP_KEYWORD
38415 /* For keywords, use the canonical spelling,
38416 not the parsed identifier. */
38417 ? ridpointers[(int) token->keyword]
38418 : token->u.value);
38419 attr = build_tree_list (attr_name, NULL_TREE);
38421 else
38422 cp_parser_error (parser, "expected identifier");
38424 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38425 error1:
38426 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38427 return attr;
38430 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38432 transaction-statement:
38433 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38434 compound-statement
38435 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38438 static tree
38439 cp_parser_transaction (cp_parser *parser, cp_token *token)
38441 unsigned char old_in = parser->in_transaction;
38442 unsigned char this_in = 1, new_in;
38443 enum rid keyword = token->keyword;
38444 tree stmt, attrs, noex;
38446 cp_lexer_consume_token (parser->lexer);
38448 if (keyword == RID_TRANSACTION_RELAXED
38449 || keyword == RID_SYNCHRONIZED)
38450 this_in |= TM_STMT_ATTR_RELAXED;
38451 else
38453 attrs = cp_parser_txn_attribute_opt (parser);
38454 if (attrs)
38455 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38458 /* Parse a noexcept specification. */
38459 if (keyword == RID_ATOMIC_NOEXCEPT)
38460 noex = boolean_true_node;
38461 else if (keyword == RID_ATOMIC_CANCEL)
38463 /* cancel-and-throw is unimplemented. */
38464 sorry ("atomic_cancel");
38465 noex = NULL_TREE;
38467 else
38468 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38470 /* Keep track if we're in the lexical scope of an outer transaction. */
38471 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38473 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38475 parser->in_transaction = new_in;
38476 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38477 parser->in_transaction = old_in;
38479 finish_transaction_stmt (stmt, NULL, this_in, noex);
38481 return stmt;
38484 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38486 transaction-expression:
38487 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38488 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38491 static tree
38492 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38494 unsigned char old_in = parser->in_transaction;
38495 unsigned char this_in = 1;
38496 cp_token *token;
38497 tree expr, noex;
38498 bool noex_expr;
38499 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38501 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38502 || keyword == RID_TRANSACTION_RELAXED);
38504 if (!flag_tm)
38505 error_at (loc,
38506 keyword == RID_TRANSACTION_RELAXED
38507 ? G_("%<__transaction_relaxed%> without transactional memory "
38508 "support enabled")
38509 : G_("%<__transaction_atomic%> without transactional memory "
38510 "support enabled"));
38512 token = cp_parser_require_keyword (parser, keyword,
38513 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38514 : RT_TRANSACTION_RELAXED));
38515 gcc_assert (token != NULL);
38517 if (keyword == RID_TRANSACTION_RELAXED)
38518 this_in |= TM_STMT_ATTR_RELAXED;
38520 /* Set this early. This might mean that we allow transaction_cancel in
38521 an expression that we find out later actually has to be a constexpr.
38522 However, we expect that cxx_constant_value will be able to deal with
38523 this; also, if the noexcept has no constexpr, then what we parse next
38524 really is a transaction's body. */
38525 parser->in_transaction = this_in;
38527 /* Parse a noexcept specification. */
38528 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38529 true);
38531 if (!noex || !noex_expr
38532 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38534 matching_parens parens;
38535 parens.require_open (parser);
38537 expr = cp_parser_expression (parser);
38538 expr = finish_parenthesized_expr (expr);
38540 parens.require_close (parser);
38542 else
38544 /* The only expression that is available got parsed for the noexcept
38545 already. noexcept is true then. */
38546 expr = noex;
38547 noex = boolean_true_node;
38550 expr = build_transaction_expr (token->location, expr, this_in, noex);
38551 parser->in_transaction = old_in;
38553 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38554 return error_mark_node;
38556 return (flag_tm ? expr : error_mark_node);
38559 /* Parse a function-transaction-block.
38561 function-transaction-block:
38562 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38563 function-body
38564 __transaction_atomic txn-attribute[opt] function-try-block
38565 __transaction_relaxed ctor-initializer[opt] function-body
38566 __transaction_relaxed function-try-block
38569 static void
38570 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38572 unsigned char old_in = parser->in_transaction;
38573 unsigned char new_in = 1;
38574 tree compound_stmt, stmt, attrs;
38575 cp_token *token;
38577 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38578 || keyword == RID_TRANSACTION_RELAXED);
38579 token = cp_parser_require_keyword (parser, keyword,
38580 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38581 : RT_TRANSACTION_RELAXED));
38582 gcc_assert (token != NULL);
38584 if (keyword == RID_TRANSACTION_RELAXED)
38585 new_in |= TM_STMT_ATTR_RELAXED;
38586 else
38588 attrs = cp_parser_txn_attribute_opt (parser);
38589 if (attrs)
38590 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38593 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38595 parser->in_transaction = new_in;
38597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38598 cp_parser_function_try_block (parser);
38599 else
38600 cp_parser_ctor_initializer_opt_and_function_body
38601 (parser, /*in_function_try_block=*/false);
38603 parser->in_transaction = old_in;
38605 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38608 /* Parse a __transaction_cancel statement.
38610 cancel-statement:
38611 __transaction_cancel txn-attribute[opt] ;
38612 __transaction_cancel txn-attribute[opt] throw-expression ;
38614 ??? Cancel and throw is not yet implemented. */
38616 static tree
38617 cp_parser_transaction_cancel (cp_parser *parser)
38619 cp_token *token;
38620 bool is_outer = false;
38621 tree stmt, attrs;
38623 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38624 RT_TRANSACTION_CANCEL);
38625 gcc_assert (token != NULL);
38627 attrs = cp_parser_txn_attribute_opt (parser);
38628 if (attrs)
38629 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38631 /* ??? Parse cancel-and-throw here. */
38633 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38635 if (!flag_tm)
38637 error_at (token->location, "%<__transaction_cancel%> without "
38638 "transactional memory support enabled");
38639 return error_mark_node;
38641 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38643 error_at (token->location, "%<__transaction_cancel%> within a "
38644 "%<__transaction_relaxed%>");
38645 return error_mark_node;
38647 else if (is_outer)
38649 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38650 && !is_tm_may_cancel_outer (current_function_decl))
38652 error_at (token->location, "outer %<__transaction_cancel%> not "
38653 "within outer %<__transaction_atomic%>");
38654 error_at (token->location,
38655 " or a %<transaction_may_cancel_outer%> function");
38656 return error_mark_node;
38659 else if (parser->in_transaction == 0)
38661 error_at (token->location, "%<__transaction_cancel%> not within "
38662 "%<__transaction_atomic%>");
38663 return error_mark_node;
38666 stmt = build_tm_abort_call (token->location, is_outer);
38667 add_stmt (stmt);
38669 return stmt;
38672 /* The parser. */
38674 static GTY (()) cp_parser *the_parser;
38677 /* Special handling for the first token or line in the file. The first
38678 thing in the file might be #pragma GCC pch_preprocess, which loads a
38679 PCH file, which is a GC collection point. So we need to handle this
38680 first pragma without benefit of an existing lexer structure.
38682 Always returns one token to the caller in *FIRST_TOKEN. This is
38683 either the true first token of the file, or the first token after
38684 the initial pragma. */
38686 static void
38687 cp_parser_initial_pragma (cp_token *first_token)
38689 tree name = NULL;
38691 cp_lexer_get_preprocessor_token (NULL, first_token);
38692 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38693 return;
38695 cp_lexer_get_preprocessor_token (NULL, first_token);
38696 if (first_token->type == CPP_STRING)
38698 name = first_token->u.value;
38700 cp_lexer_get_preprocessor_token (NULL, first_token);
38701 if (first_token->type != CPP_PRAGMA_EOL)
38702 error_at (first_token->location,
38703 "junk at end of %<#pragma GCC pch_preprocess%>");
38705 else
38706 error_at (first_token->location, "expected string literal");
38708 /* Skip to the end of the pragma. */
38709 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38710 cp_lexer_get_preprocessor_token (NULL, first_token);
38712 /* Now actually load the PCH file. */
38713 if (name)
38714 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38716 /* Read one more token to return to our caller. We have to do this
38717 after reading the PCH file in, since its pointers have to be
38718 live. */
38719 cp_lexer_get_preprocessor_token (NULL, first_token);
38722 /* Parse a pragma GCC ivdep. */
38724 static bool
38725 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38727 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38728 return true;
38731 /* Parse a pragma GCC unroll. */
38733 static unsigned short
38734 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38736 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38737 tree expr = cp_parser_constant_expression (parser);
38738 unsigned short unroll;
38739 expr = maybe_constant_value (expr);
38740 HOST_WIDE_INT lunroll = 0;
38741 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38742 || TREE_CODE (expr) != INTEGER_CST
38743 || (lunroll = tree_to_shwi (expr)) < 0
38744 || lunroll >= USHRT_MAX)
38746 error_at (location, "%<#pragma GCC unroll%> requires an"
38747 " assignment-expression that evaluates to a non-negative"
38748 " integral constant less than %u", USHRT_MAX);
38749 unroll = 0;
38751 else
38753 unroll = (unsigned short)lunroll;
38754 if (unroll == 0)
38755 unroll = 1;
38757 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38758 return unroll;
38761 /* Normal parsing of a pragma token. Here we can (and must) use the
38762 regular lexer. */
38764 static bool
38765 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38767 cp_token *pragma_tok;
38768 unsigned int id;
38769 tree stmt;
38770 bool ret;
38772 pragma_tok = cp_lexer_consume_token (parser->lexer);
38773 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38774 parser->lexer->in_pragma = true;
38776 id = cp_parser_pragma_kind (pragma_tok);
38777 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38778 cp_ensure_no_omp_declare_simd (parser);
38779 switch (id)
38781 case PRAGMA_GCC_PCH_PREPROCESS:
38782 error_at (pragma_tok->location,
38783 "%<#pragma GCC pch_preprocess%> must be first");
38784 break;
38786 case PRAGMA_OMP_BARRIER:
38787 switch (context)
38789 case pragma_compound:
38790 cp_parser_omp_barrier (parser, pragma_tok);
38791 return false;
38792 case pragma_stmt:
38793 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38794 "used in compound statements", "omp barrier");
38795 break;
38796 default:
38797 goto bad_stmt;
38799 break;
38801 case PRAGMA_OMP_FLUSH:
38802 switch (context)
38804 case pragma_compound:
38805 cp_parser_omp_flush (parser, pragma_tok);
38806 return false;
38807 case pragma_stmt:
38808 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38809 "used in compound statements", "omp flush");
38810 break;
38811 default:
38812 goto bad_stmt;
38814 break;
38816 case PRAGMA_OMP_TASKWAIT:
38817 switch (context)
38819 case pragma_compound:
38820 cp_parser_omp_taskwait (parser, pragma_tok);
38821 return false;
38822 case pragma_stmt:
38823 error_at (pragma_tok->location,
38824 "%<#pragma %s%> may only be used in compound statements",
38825 "omp taskwait");
38826 break;
38827 default:
38828 goto bad_stmt;
38830 break;
38832 case PRAGMA_OMP_TASKYIELD:
38833 switch (context)
38835 case pragma_compound:
38836 cp_parser_omp_taskyield (parser, pragma_tok);
38837 return false;
38838 case pragma_stmt:
38839 error_at (pragma_tok->location,
38840 "%<#pragma %s%> may only be used in compound statements",
38841 "omp taskyield");
38842 break;
38843 default:
38844 goto bad_stmt;
38846 break;
38848 case PRAGMA_OMP_CANCEL:
38849 switch (context)
38851 case pragma_compound:
38852 cp_parser_omp_cancel (parser, pragma_tok);
38853 return false;
38854 case pragma_stmt:
38855 error_at (pragma_tok->location,
38856 "%<#pragma %s%> may only be used in compound statements",
38857 "omp cancel");
38858 break;
38859 default:
38860 goto bad_stmt;
38862 break;
38864 case PRAGMA_OMP_CANCELLATION_POINT:
38865 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38866 return false;
38868 case PRAGMA_OMP_THREADPRIVATE:
38869 cp_parser_omp_threadprivate (parser, pragma_tok);
38870 return false;
38872 case PRAGMA_OMP_DECLARE:
38873 return cp_parser_omp_declare (parser, pragma_tok, context);
38875 case PRAGMA_OACC_DECLARE:
38876 cp_parser_oacc_declare (parser, pragma_tok);
38877 return false;
38879 case PRAGMA_OACC_ENTER_DATA:
38880 if (context == pragma_stmt)
38882 error_at (pragma_tok->location,
38883 "%<#pragma %s%> may only be used in compound statements",
38884 "acc enter data");
38885 break;
38887 else if (context != pragma_compound)
38888 goto bad_stmt;
38889 cp_parser_omp_construct (parser, pragma_tok, if_p);
38890 return true;
38892 case PRAGMA_OACC_EXIT_DATA:
38893 if (context == pragma_stmt)
38895 error_at (pragma_tok->location,
38896 "%<#pragma %s%> may only be used in compound statements",
38897 "acc exit data");
38898 break;
38900 else if (context != pragma_compound)
38901 goto bad_stmt;
38902 cp_parser_omp_construct (parser, pragma_tok, if_p);
38903 return true;
38905 case PRAGMA_OACC_ROUTINE:
38906 if (context != pragma_external)
38908 error_at (pragma_tok->location,
38909 "%<#pragma acc routine%> must be at file scope");
38910 break;
38912 cp_parser_oacc_routine (parser, pragma_tok, context);
38913 return false;
38915 case PRAGMA_OACC_UPDATE:
38916 if (context == pragma_stmt)
38918 error_at (pragma_tok->location,
38919 "%<#pragma %s%> may only be used in compound statements",
38920 "acc update");
38921 break;
38923 else if (context != pragma_compound)
38924 goto bad_stmt;
38925 cp_parser_omp_construct (parser, pragma_tok, if_p);
38926 return true;
38928 case PRAGMA_OACC_WAIT:
38929 if (context == pragma_stmt)
38931 error_at (pragma_tok->location,
38932 "%<#pragma %s%> may only be used in compound statements",
38933 "acc wait");
38934 break;
38936 else if (context != pragma_compound)
38937 goto bad_stmt;
38938 cp_parser_omp_construct (parser, pragma_tok, if_p);
38939 return true;
38941 case PRAGMA_OACC_ATOMIC:
38942 case PRAGMA_OACC_CACHE:
38943 case PRAGMA_OACC_DATA:
38944 case PRAGMA_OACC_HOST_DATA:
38945 case PRAGMA_OACC_KERNELS:
38946 case PRAGMA_OACC_PARALLEL:
38947 case PRAGMA_OACC_LOOP:
38948 case PRAGMA_OMP_ATOMIC:
38949 case PRAGMA_OMP_CRITICAL:
38950 case PRAGMA_OMP_DISTRIBUTE:
38951 case PRAGMA_OMP_FOR:
38952 case PRAGMA_OMP_MASTER:
38953 case PRAGMA_OMP_PARALLEL:
38954 case PRAGMA_OMP_SECTIONS:
38955 case PRAGMA_OMP_SIMD:
38956 case PRAGMA_OMP_SINGLE:
38957 case PRAGMA_OMP_TASK:
38958 case PRAGMA_OMP_TASKGROUP:
38959 case PRAGMA_OMP_TASKLOOP:
38960 case PRAGMA_OMP_TEAMS:
38961 if (context != pragma_stmt && context != pragma_compound)
38962 goto bad_stmt;
38963 stmt = push_omp_privatization_clauses (false);
38964 cp_parser_omp_construct (parser, pragma_tok, if_p);
38965 pop_omp_privatization_clauses (stmt);
38966 return true;
38968 case PRAGMA_OMP_ORDERED:
38969 if (context != pragma_stmt && context != pragma_compound)
38970 goto bad_stmt;
38971 stmt = push_omp_privatization_clauses (false);
38972 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38973 pop_omp_privatization_clauses (stmt);
38974 return ret;
38976 case PRAGMA_OMP_TARGET:
38977 if (context != pragma_stmt && context != pragma_compound)
38978 goto bad_stmt;
38979 stmt = push_omp_privatization_clauses (false);
38980 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38981 pop_omp_privatization_clauses (stmt);
38982 return ret;
38984 case PRAGMA_OMP_END_DECLARE_TARGET:
38985 cp_parser_omp_end_declare_target (parser, pragma_tok);
38986 return false;
38988 case PRAGMA_OMP_SECTION:
38989 error_at (pragma_tok->location,
38990 "%<#pragma omp section%> may only be used in "
38991 "%<#pragma omp sections%> construct");
38992 break;
38994 case PRAGMA_IVDEP:
38996 if (context == pragma_external)
38998 error_at (pragma_tok->location,
38999 "%<#pragma GCC ivdep%> must be inside a function");
39000 break;
39002 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39003 unsigned short unroll;
39004 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39005 if (tok->type == CPP_PRAGMA
39006 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39008 tok = cp_lexer_consume_token (parser->lexer);
39009 unroll = cp_parser_pragma_unroll (parser, tok);
39010 tok = cp_lexer_peek_token (the_parser->lexer);
39012 else
39013 unroll = 0;
39014 if (tok->type != CPP_KEYWORD
39015 || (tok->keyword != RID_FOR
39016 && tok->keyword != RID_WHILE
39017 && tok->keyword != RID_DO))
39019 cp_parser_error (parser, "for, while or do statement expected");
39020 return false;
39022 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39023 return true;
39026 case PRAGMA_UNROLL:
39028 if (context == pragma_external)
39030 error_at (pragma_tok->location,
39031 "%<#pragma GCC unroll%> must be inside a function");
39032 break;
39034 const unsigned short unroll
39035 = cp_parser_pragma_unroll (parser, pragma_tok);
39036 bool ivdep;
39037 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39038 if (tok->type == CPP_PRAGMA
39039 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39041 tok = cp_lexer_consume_token (parser->lexer);
39042 ivdep = cp_parser_pragma_ivdep (parser, tok);
39043 tok = cp_lexer_peek_token (the_parser->lexer);
39045 else
39046 ivdep = false;
39047 if (tok->type != CPP_KEYWORD
39048 || (tok->keyword != RID_FOR
39049 && tok->keyword != RID_WHILE
39050 && tok->keyword != RID_DO))
39052 cp_parser_error (parser, "for, while or do statement expected");
39053 return false;
39055 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39056 return true;
39059 default:
39060 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39061 c_invoke_pragma_handler (id);
39062 break;
39064 bad_stmt:
39065 cp_parser_error (parser, "expected declaration specifiers");
39066 break;
39069 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39070 return false;
39073 /* The interface the pragma parsers have to the lexer. */
39075 enum cpp_ttype
39076 pragma_lex (tree *value, location_t *loc)
39078 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39079 enum cpp_ttype ret = tok->type;
39081 *value = tok->u.value;
39082 if (loc)
39083 *loc = tok->location;
39085 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39086 ret = CPP_EOF;
39087 else if (ret == CPP_STRING)
39088 *value = cp_parser_string_literal (the_parser, false, false);
39089 else
39091 if (ret == CPP_KEYWORD)
39092 ret = CPP_NAME;
39093 cp_lexer_consume_token (the_parser->lexer);
39096 return ret;
39100 /* External interface. */
39102 /* Parse one entire translation unit. */
39104 void
39105 c_parse_file (void)
39107 static bool already_called = false;
39109 if (already_called)
39110 fatal_error (input_location,
39111 "inter-module optimizations not implemented for C++");
39112 already_called = true;
39114 the_parser = cp_parser_new ();
39115 push_deferring_access_checks (flag_access_control
39116 ? dk_no_deferred : dk_no_check);
39117 cp_parser_translation_unit (the_parser);
39118 the_parser = NULL;
39121 /* Create an identifier for a generic parameter type (a synthesized
39122 template parameter implied by `auto' or a concept identifier). */
39124 static GTY(()) int generic_parm_count;
39125 static tree
39126 make_generic_type_name ()
39128 char buf[32];
39129 sprintf (buf, "auto:%d", ++generic_parm_count);
39130 return get_identifier (buf);
39133 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39134 (creating a new template parameter list if necessary). Returns the newly
39135 created template type parm. */
39137 static tree
39138 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39140 gcc_assert (current_binding_level->kind == sk_function_parms);
39142 /* Before committing to modifying any scope, if we're in an
39143 implicit template scope, and we're trying to synthesize a
39144 constrained parameter, try to find a previous parameter with
39145 the same name. This is the same-type rule for abbreviated
39146 function templates.
39148 NOTE: We can generate implicit parameters when tentatively
39149 parsing a nested name specifier, only to reject that parse
39150 later. However, matching the same template-id as part of a
39151 direct-declarator should generate an identical template
39152 parameter, so this rule will merge them. */
39153 if (parser->implicit_template_scope && constr)
39155 tree t = parser->implicit_template_parms;
39156 while (t)
39158 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39160 tree d = TREE_VALUE (t);
39161 if (TREE_CODE (d) == PARM_DECL)
39162 /* Return the TEMPLATE_PARM_INDEX. */
39163 d = DECL_INITIAL (d);
39164 return d;
39166 t = TREE_CHAIN (t);
39170 /* We are either continuing a function template that already contains implicit
39171 template parameters, creating a new fully-implicit function template, or
39172 extending an existing explicit function template with implicit template
39173 parameters. */
39175 cp_binding_level *const entry_scope = current_binding_level;
39177 bool become_template = false;
39178 cp_binding_level *parent_scope = 0;
39180 if (parser->implicit_template_scope)
39182 gcc_assert (parser->implicit_template_parms);
39184 current_binding_level = parser->implicit_template_scope;
39186 else
39188 /* Roll back to the existing template parameter scope (in the case of
39189 extending an explicit function template) or introduce a new template
39190 parameter scope ahead of the function parameter scope (or class scope
39191 in the case of out-of-line member definitions). The function scope is
39192 added back after template parameter synthesis below. */
39194 cp_binding_level *scope = entry_scope;
39196 while (scope->kind == sk_function_parms)
39198 parent_scope = scope;
39199 scope = scope->level_chain;
39201 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39203 /* If not defining a class, then any class scope is a scope level in
39204 an out-of-line member definition. In this case simply wind back
39205 beyond the first such scope to inject the template parameter list.
39206 Otherwise wind back to the class being defined. The latter can
39207 occur in class member friend declarations such as:
39209 class A {
39210 void foo (auto);
39212 class B {
39213 friend void A::foo (auto);
39216 The template parameter list synthesized for the friend declaration
39217 must be injected in the scope of 'B'. This can also occur in
39218 erroneous cases such as:
39220 struct A {
39221 struct B {
39222 void foo (auto);
39224 void B::foo (auto) {}
39227 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39228 but, nevertheless, the template parameter list synthesized for the
39229 declarator should be injected into the scope of 'A' as if the
39230 ill-formed template was specified explicitly. */
39232 while (scope->kind == sk_class && !scope->defining_class_p)
39234 parent_scope = scope;
39235 scope = scope->level_chain;
39239 current_binding_level = scope;
39241 if (scope->kind != sk_template_parms
39242 || !function_being_declared_is_template_p (parser))
39244 /* Introduce a new template parameter list for implicit template
39245 parameters. */
39247 become_template = true;
39249 parser->implicit_template_scope
39250 = begin_scope (sk_template_parms, NULL);
39252 ++processing_template_decl;
39254 parser->fully_implicit_function_template_p = true;
39255 ++parser->num_template_parameter_lists;
39257 else
39259 /* Synthesize implicit template parameters at the end of the explicit
39260 template parameter list. */
39262 gcc_assert (current_template_parms);
39264 parser->implicit_template_scope = scope;
39266 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39267 parser->implicit_template_parms
39268 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39272 /* Synthesize a new template parameter and track the current template
39273 parameter chain with implicit_template_parms. */
39275 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39276 tree synth_id = make_generic_type_name ();
39277 tree synth_tmpl_parm;
39278 bool non_type = false;
39280 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39281 synth_tmpl_parm
39282 = finish_template_type_parm (class_type_node, synth_id);
39283 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39284 synth_tmpl_parm
39285 = finish_constrained_template_template_parm (proto, synth_id);
39286 else
39288 synth_tmpl_parm = copy_decl (proto);
39289 DECL_NAME (synth_tmpl_parm) = synth_id;
39290 non_type = true;
39293 // Attach the constraint to the parm before processing.
39294 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39295 TREE_TYPE (node) = constr;
39296 tree new_parm
39297 = process_template_parm (parser->implicit_template_parms,
39298 input_location,
39299 node,
39300 /*non_type=*/non_type,
39301 /*param_pack=*/false);
39303 // Chain the new parameter to the list of implicit parameters.
39304 if (parser->implicit_template_parms)
39305 parser->implicit_template_parms
39306 = TREE_CHAIN (parser->implicit_template_parms);
39307 else
39308 parser->implicit_template_parms = new_parm;
39310 tree new_decl = get_local_decls ();
39311 if (non_type)
39312 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39313 new_decl = DECL_INITIAL (new_decl);
39315 /* If creating a fully implicit function template, start the new implicit
39316 template parameter list with this synthesized type, otherwise grow the
39317 current template parameter list. */
39319 if (become_template)
39321 parent_scope->level_chain = current_binding_level;
39323 tree new_parms = make_tree_vec (1);
39324 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39325 current_template_parms = tree_cons (size_int (processing_template_decl),
39326 new_parms, current_template_parms);
39328 else
39330 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39331 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39332 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39333 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39336 // If the new parameter was constrained, we need to add that to the
39337 // constraints in the template parameter list.
39338 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39340 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39341 reqs = conjoin_constraints (reqs, req);
39342 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39345 current_binding_level = entry_scope;
39347 return new_decl;
39350 /* Finish the declaration of a fully implicit function template. Such a
39351 template has no explicit template parameter list so has not been through the
39352 normal template head and tail processing. synthesize_implicit_template_parm
39353 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39354 provided if the declaration is a class member such that its template
39355 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39356 form is returned. Otherwise NULL_TREE is returned. */
39358 static tree
39359 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39361 gcc_assert (parser->fully_implicit_function_template_p);
39363 if (member_decl_opt && member_decl_opt != error_mark_node
39364 && DECL_VIRTUAL_P (member_decl_opt))
39366 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39367 "implicit templates may not be %<virtual%>");
39368 DECL_VIRTUAL_P (member_decl_opt) = false;
39371 if (member_decl_opt)
39372 member_decl_opt = finish_member_template_decl (member_decl_opt);
39373 end_template_decl ();
39375 parser->fully_implicit_function_template_p = false;
39376 parser->implicit_template_parms = 0;
39377 parser->implicit_template_scope = 0;
39378 --parser->num_template_parameter_lists;
39380 return member_decl_opt;
39383 /* Like finish_fully_implicit_template, but to be used in error
39384 recovery, rearranging scopes so that we restore the state we had
39385 before synthesize_implicit_template_parm inserted the implement
39386 template parms scope. */
39388 static void
39389 abort_fully_implicit_template (cp_parser *parser)
39391 cp_binding_level *return_to_scope = current_binding_level;
39393 if (parser->implicit_template_scope
39394 && return_to_scope != parser->implicit_template_scope)
39396 cp_binding_level *child = return_to_scope;
39397 for (cp_binding_level *scope = child->level_chain;
39398 scope != parser->implicit_template_scope;
39399 scope = child->level_chain)
39400 child = scope;
39401 child->level_chain = parser->implicit_template_scope->level_chain;
39402 parser->implicit_template_scope->level_chain = return_to_scope;
39403 current_binding_level = parser->implicit_template_scope;
39405 else
39406 return_to_scope = return_to_scope->level_chain;
39408 finish_fully_implicit_template (parser, NULL);
39410 gcc_assert (current_binding_level == return_to_scope);
39413 /* Helper function for diagnostics that have complained about things
39414 being used with 'extern "C"' linkage.
39416 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39418 void
39419 maybe_show_extern_c_location (void)
39421 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39422 inform (the_parser->innermost_linkage_specification_location,
39423 "%<extern \"C\"%> linkage started here");
39426 #include "gt-cp-parser.h"