PR target/84064
[official-gcc.git] / gcc / cp / parser.c
blob222db0cad3fa33e75764675e908be43be9d2f841
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
250 static void cp_parser_initial_pragma
251 (cp_token *);
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
262 /* Variables. */
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288 if (buffer == NULL)
289 return;
291 if (num == 0)
292 num = buffer->length ();
294 if (start_token == NULL)
295 start_token = buffer->address ();
297 if (start_token > buffer->address ())
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 if (token == start_token)
308 do_print = true;
310 if (!do_print)
311 continue;
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
317 cp_lexer_print_token (file, token);
319 if (token == curr_token)
320 fprintf (file, "]]");
322 switch (token->type)
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
331 default:
332 fputc (' ', file);
336 if (i == num && i < buffer->length ())
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
342 fprintf (file, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 if (t)
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
384 /* Dump parser context C to FILE. */
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 unsigned i;
402 cp_parser_context *c;
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
423 /* Print an unparsed function entry UF to FILE. */
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
460 fprintf (file, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 cp_token *next_token, *first_token, *start_token;
490 if (file == NULL)
491 file = stderr;
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
513 if (file == NULL)
514 file = stderr;
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
592 cp_debug_parser (stderr, &ref);
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
606 static cp_lexer *
607 cp_lexer_alloc (void)
609 cp_lexer *lexer;
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
624 return lexer;
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
631 static cp_lexer *
632 cp_lexer_new_main (void)
634 cp_lexer *lexer;
635 cp_token token;
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
642 lexer = cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
693 /* Frees all resources associated with LEXER. */
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
718 return lexer->debugging_p;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
727 return lexer->next_token - previous_p;
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
733 return pos;
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
763 return cp_lexer_token_at (lexer, tp);
766 /* nonzero if we are presently saving tokens. */
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
771 return lexer->saved_tokens.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
782 static int is_extern_c = 0;
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
809 else
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
826 token->keyword = RID_MAX;
829 else if (token->type == CPP_AT_NAME)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
860 if (token->type != CPP_EOF)
862 input_location = token->location;
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
880 if (cp_lexer_debugging_p (lexer))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
886 return lexer->next_token;
889 /* Return true if the next token has the indicated TYPE. */
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
894 return cp_lexer_peek_token (lexer)->type == type;
897 /* Return true if the next token does not have the indicated TYPE. */
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
902 return !cp_lexer_next_token_is (lexer, type);
905 /* Return true if the next token is the indicated KEYWORD. */
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 /* Return true if the next token is not the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
933 /* Return true if KEYWORD can start a decl-specifier. */
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
938 switch (keyword)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
987 /* Return true if the next token is a keyword for a decl-specifier. */
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
992 cp_token *token;
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
998 /* Returns TRUE iff the token T begins a decltype type. */
1000 static bool
1001 token_is_decltype (cp_token *t)
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1034 /* Return the stored value. */
1035 return check_value->value;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1047 cp_token *token;
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1061 ++token;
1062 if (token == lexer->last_token)
1064 token = &eof_token;
1065 break;
1068 if (!token->purged_p)
1069 --n;
1072 if (cp_lexer_debugging_p (lexer))
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1078 return token;
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1087 cp_token *token = lexer->next_token;
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1097 lexer->next_token = &eof_token;
1098 break;
1102 while (lexer->next_token->purged_p);
1104 cp_lexer_set_source_position_from_token (token);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1114 return token;
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1124 cp_token *tok = lexer->next_token;
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1134 tok++;
1135 if (tok == lexer->last_token)
1137 tok = &eof_token;
1138 break;
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1152 cp_token *peek = lexer->next_token;
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1157 gcc_assert (tok < peek);
1159 for ( tok += 1; tok != peek; tok += 1)
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1181 /* Commit to the portion of the token stream most recently saved. */
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1190 lexer->saved_tokens.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1203 lexer->next_token = lexer->saved_tokens.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1211 struct saved_token_sentinel
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1221 void rollback ()
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1226 ~saved_token_sentinel()
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1234 /* Print a representation of the TOKEN on the STREAM. */
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1307 /* Start emitting debugging information. */
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1320 /* Stop emitting debugging information. */
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1366 if (fndecl == error_mark_node)
1368 parser->omp_declare_simd = NULL;
1369 return;
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1454 return declarator;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1467 cp_declarator *declarator;
1469 /* It is valid to write:
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1491 return declarator;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1503 cp_declarator *declarator;
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1520 return declarator;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1531 cp_declarator *declarator;
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1543 else
1544 declarator->parameter_pack_p = false;
1546 declarator->std_attributes = attributes;
1548 return declarator;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1560 cp_declarator *declarator;
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1567 if (pointee)
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1572 else
1573 declarator->parameter_pack_p = false;
1575 declarator->std_attributes = attributes;
1577 return declarator;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1596 cp_declarator *declarator;
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1614 else
1615 declarator->parameter_pack_p = false;
1617 return declarator;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1626 cp_declarator *declarator;
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 return declarator;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1659 switch ((int)declarator->kind)
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1667 case cdk_error:
1668 return true;
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1676 return !found;
1679 cp_parameter_declarator *no_parameters;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1691 cp_parameter_declarator *parameter;
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1705 return parameter;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1713 while (declarator)
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1724 return false;
1727 /* The parser. */
1729 /* Overview
1730 --------
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1753 Methodology
1754 -----------
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1773 Future Improvements
1774 -------------------
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1785 enum
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1825 enum cp_parser_prec
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1877 /* Prototypes. */
1879 /* Constructors and destructors. */
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1884 /* Class variables. */
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1938 cp_parser_context *context;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1964 return context;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1992 /* Prototypes. */
1994 /* Constructors and destructors. */
1996 static cp_parser *cp_parser_new
1997 (void);
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051 bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059 (cp_token *);
2060 static tree cp_parser_new_expression
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063 (cp_parser *);
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067 (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071 (cp_parser *);
2072 static tree cp_parser_delete_expression
2073 (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083 (cp_parser *);
2084 static cp_expr cp_parser_expression
2085 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089 (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091 (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093 (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097 (cp_parser *, tree);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106 (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108 (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112 (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114 (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116 (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120 (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126 (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130 (tree, tree);
2131 static tree cp_parser_jump_statement
2132 (cp_parser *);
2133 static void cp_parser_declaration_statement
2134 (cp_parser *);
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser *, bool *, const token_indent_info &);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2144 (cp_parser *);
2145 static void cp_parser_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165 (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167 (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171 (cp_parser *);
2172 static void cp_parser_enumerator_list
2173 (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177 (cp_parser *);
2178 static void cp_parser_namespace_definition
2179 (cp_parser *);
2180 static void cp_parser_namespace_body
2181 (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183 (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185 (cp_parser *);
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189 (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191 (cp_parser *);
2192 static void cp_parser_asm_definition
2193 (cp_parser *);
2194 static void cp_parser_linkage_specification
2195 (cp_parser *);
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199 (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207 bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225 (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227 (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229 (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232 (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236 (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *, bool *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
2268 /* Classes [gram.class] */
2270 static tree cp_parser_class_name
2271 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2272 static tree cp_parser_class_specifier
2273 (cp_parser *);
2274 static tree cp_parser_class_head
2275 (cp_parser *, bool *);
2276 static enum tag_types cp_parser_class_key
2277 (cp_parser *);
2278 static void cp_parser_type_parameter_key
2279 (cp_parser* parser);
2280 static void cp_parser_member_specification_opt
2281 (cp_parser *);
2282 static void cp_parser_member_declaration
2283 (cp_parser *);
2284 static tree cp_parser_pure_specifier
2285 (cp_parser *);
2286 static tree cp_parser_constant_initializer
2287 (cp_parser *);
2289 /* Derived classes [gram.class.derived] */
2291 static tree cp_parser_base_clause
2292 (cp_parser *);
2293 static tree cp_parser_base_specifier
2294 (cp_parser *);
2296 /* Special member functions [gram.special] */
2298 static tree cp_parser_conversion_function_id
2299 (cp_parser *);
2300 static tree cp_parser_conversion_type_id
2301 (cp_parser *);
2302 static cp_declarator *cp_parser_conversion_declarator_opt
2303 (cp_parser *);
2304 static void cp_parser_ctor_initializer_opt
2305 (cp_parser *);
2306 static void cp_parser_mem_initializer_list
2307 (cp_parser *);
2308 static tree cp_parser_mem_initializer
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer_id
2311 (cp_parser *);
2313 /* Overloading [gram.over] */
2315 static cp_expr cp_parser_operator_function_id
2316 (cp_parser *);
2317 static cp_expr cp_parser_operator
2318 (cp_parser *);
2320 /* Templates [gram.temp] */
2322 static void cp_parser_template_declaration
2323 (cp_parser *, bool);
2324 static tree cp_parser_template_parameter_list
2325 (cp_parser *);
2326 static tree cp_parser_template_parameter
2327 (cp_parser *, bool *, bool *);
2328 static tree cp_parser_type_parameter
2329 (cp_parser *, bool *);
2330 static tree cp_parser_template_id
2331 (cp_parser *, bool, bool, enum tag_types, bool);
2332 static tree cp_parser_template_name
2333 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2334 static tree cp_parser_template_argument_list
2335 (cp_parser *);
2336 static tree cp_parser_template_argument
2337 (cp_parser *);
2338 static void cp_parser_explicit_instantiation
2339 (cp_parser *);
2340 static void cp_parser_explicit_specialization
2341 (cp_parser *);
2343 /* Exception handling [gram.exception] */
2345 static tree cp_parser_try_block
2346 (cp_parser *);
2347 static void cp_parser_function_try_block
2348 (cp_parser *);
2349 static void cp_parser_handler_seq
2350 (cp_parser *);
2351 static void cp_parser_handler
2352 (cp_parser *);
2353 static tree cp_parser_exception_declaration
2354 (cp_parser *);
2355 static tree cp_parser_throw_expression
2356 (cp_parser *);
2357 static tree cp_parser_exception_specification_opt
2358 (cp_parser *);
2359 static tree cp_parser_type_id_list
2360 (cp_parser *);
2362 /* GNU Extensions */
2364 static tree cp_parser_asm_specification_opt
2365 (cp_parser *);
2366 static tree cp_parser_asm_operand_list
2367 (cp_parser *);
2368 static tree cp_parser_asm_clobber_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_label_list
2371 (cp_parser *);
2372 static bool cp_next_tokens_can_be_attribute_p
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_gnu_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_std_attribute_p
2377 (cp_parser *);
2378 static bool cp_nth_tokens_can_be_std_attribute_p
2379 (cp_parser *, size_t);
2380 static bool cp_nth_tokens_can_be_gnu_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_attribute_p
2383 (cp_parser *, size_t);
2384 static tree cp_parser_attributes_opt
2385 (cp_parser *);
2386 static tree cp_parser_gnu_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attribute_list
2389 (cp_parser *);
2390 static tree cp_parser_std_attribute
2391 (cp_parser *, tree);
2392 static tree cp_parser_std_attribute_spec
2393 (cp_parser *);
2394 static tree cp_parser_std_attribute_spec_seq
2395 (cp_parser *);
2396 static bool cp_parser_extension_opt
2397 (cp_parser *, int *);
2398 static void cp_parser_label_declaration
2399 (cp_parser *);
2401 /* Concept Extensions */
2403 static tree cp_parser_requires_clause
2404 (cp_parser *);
2405 static tree cp_parser_requires_clause_opt
2406 (cp_parser *);
2407 static tree cp_parser_requires_expression
2408 (cp_parser *);
2409 static tree cp_parser_requirement_parameter_list
2410 (cp_parser *);
2411 static tree cp_parser_requirement_body
2412 (cp_parser *);
2413 static tree cp_parser_requirement_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement
2416 (cp_parser *);
2417 static tree cp_parser_simple_requirement
2418 (cp_parser *);
2419 static tree cp_parser_compound_requirement
2420 (cp_parser *);
2421 static tree cp_parser_type_requirement
2422 (cp_parser *);
2423 static tree cp_parser_nested_requirement
2424 (cp_parser *);
2426 /* Transactional Memory Extensions */
2428 static tree cp_parser_transaction
2429 (cp_parser *, cp_token *);
2430 static tree cp_parser_transaction_expression
2431 (cp_parser *, enum rid);
2432 static void cp_parser_function_transaction
2433 (cp_parser *, enum rid);
2434 static tree cp_parser_transaction_cancel
2435 (cp_parser *);
2437 enum pragma_context {
2438 pragma_external,
2439 pragma_member,
2440 pragma_objc_icode,
2441 pragma_stmt,
2442 pragma_compound
2444 static bool cp_parser_pragma
2445 (cp_parser *, enum pragma_context, bool *);
2447 /* Objective-C++ Productions */
2449 static tree cp_parser_objc_message_receiver
2450 (cp_parser *);
2451 static tree cp_parser_objc_message_args
2452 (cp_parser *);
2453 static tree cp_parser_objc_message_expression
2454 (cp_parser *);
2455 static cp_expr cp_parser_objc_encode_expression
2456 (cp_parser *);
2457 static tree cp_parser_objc_defs_expression
2458 (cp_parser *);
2459 static tree cp_parser_objc_protocol_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_selector_expression
2462 (cp_parser *);
2463 static cp_expr cp_parser_objc_expression
2464 (cp_parser *);
2465 static bool cp_parser_objc_selector_p
2466 (enum cpp_ttype);
2467 static tree cp_parser_objc_selector
2468 (cp_parser *);
2469 static tree cp_parser_objc_protocol_refs_opt
2470 (cp_parser *);
2471 static void cp_parser_objc_declaration
2472 (cp_parser *, tree);
2473 static tree cp_parser_objc_statement
2474 (cp_parser *);
2475 static bool cp_parser_objc_valid_prefix_attributes
2476 (cp_parser *, tree *);
2477 static void cp_parser_objc_at_property_declaration
2478 (cp_parser *) ;
2479 static void cp_parser_objc_at_synthesize_declaration
2480 (cp_parser *) ;
2481 static void cp_parser_objc_at_dynamic_declaration
2482 (cp_parser *) ;
2483 static tree cp_parser_objc_struct_declaration
2484 (cp_parser *) ;
2486 /* Utility Routines */
2488 static cp_expr cp_parser_lookup_name
2489 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2490 static tree cp_parser_lookup_name_simple
2491 (cp_parser *, tree, location_t);
2492 static tree cp_parser_maybe_treat_template_as_class
2493 (tree, bool);
2494 static bool cp_parser_check_declarator_template_parameters
2495 (cp_parser *, cp_declarator *, location_t);
2496 static bool cp_parser_check_template_parameters
2497 (cp_parser *, unsigned, location_t, cp_declarator *);
2498 static cp_expr cp_parser_simple_cast_expression
2499 (cp_parser *);
2500 static tree cp_parser_global_scope_opt
2501 (cp_parser *, bool);
2502 static bool cp_parser_constructor_declarator_p
2503 (cp_parser *, bool);
2504 static tree cp_parser_function_definition_from_specifiers_and_declarator
2505 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2506 static tree cp_parser_function_definition_after_declarator
2507 (cp_parser *, bool);
2508 static bool cp_parser_template_declaration_after_export
2509 (cp_parser *, bool);
2510 static void cp_parser_perform_template_parameter_access_checks
2511 (vec<deferred_access_check, va_gc> *);
2512 static tree cp_parser_single_declaration
2513 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2514 static cp_expr cp_parser_functional_cast
2515 (cp_parser *, tree);
2516 static tree cp_parser_save_member_function_body
2517 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2518 static tree cp_parser_save_nsdmi
2519 (cp_parser *);
2520 static tree cp_parser_enclosed_template_argument_list
2521 (cp_parser *);
2522 static void cp_parser_save_default_args
2523 (cp_parser *, tree);
2524 static void cp_parser_late_parsing_for_member
2525 (cp_parser *, tree);
2526 static tree cp_parser_late_parse_one_default_arg
2527 (cp_parser *, tree, tree, tree);
2528 static void cp_parser_late_parsing_nsdmi
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_default_args
2531 (cp_parser *, tree);
2532 static tree cp_parser_sizeof_operand
2533 (cp_parser *, enum rid);
2534 static cp_expr cp_parser_trait_expr
2535 (cp_parser *, enum rid);
2536 static bool cp_parser_declares_only_class_p
2537 (cp_parser *);
2538 static void cp_parser_set_storage_class
2539 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2540 static void cp_parser_set_decl_spec_type
2541 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2542 static void set_and_check_decl_spec_loc
2543 (cp_decl_specifier_seq *decl_specs,
2544 cp_decl_spec ds, cp_token *);
2545 static bool cp_parser_friend_p
2546 (const cp_decl_specifier_seq *);
2547 static void cp_parser_required_error
2548 (cp_parser *, required_token, bool, location_t);
2549 static cp_token *cp_parser_require
2550 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2551 static cp_token *cp_parser_require_keyword
2552 (cp_parser *, enum rid, required_token);
2553 static bool cp_parser_token_starts_function_definition_p
2554 (cp_token *);
2555 static bool cp_parser_next_token_starts_class_definition_p
2556 (cp_parser *);
2557 static bool cp_parser_next_token_ends_template_argument_p
2558 (cp_parser *);
2559 static bool cp_parser_nth_token_starts_template_argument_list_p
2560 (cp_parser *, size_t);
2561 static enum tag_types cp_parser_token_is_class_key
2562 (cp_token *);
2563 static enum tag_types cp_parser_token_is_type_parameter_key
2564 (cp_token *);
2565 static void cp_parser_check_class_key
2566 (enum tag_types, tree type);
2567 static void cp_parser_check_access_in_redeclaration
2568 (tree type, location_t location);
2569 static bool cp_parser_optional_template_keyword
2570 (cp_parser *);
2571 static void cp_parser_pre_parsed_nested_name_specifier
2572 (cp_parser *);
2573 static bool cp_parser_cache_group
2574 (cp_parser *, enum cpp_ttype, unsigned);
2575 static tree cp_parser_cache_defarg
2576 (cp_parser *parser, bool nsdmi);
2577 static void cp_parser_parse_tentatively
2578 (cp_parser *);
2579 static void cp_parser_commit_to_tentative_parse
2580 (cp_parser *);
2581 static void cp_parser_commit_to_topmost_tentative_parse
2582 (cp_parser *);
2583 static void cp_parser_abort_tentative_parse
2584 (cp_parser *);
2585 static bool cp_parser_parse_definitely
2586 (cp_parser *);
2587 static inline bool cp_parser_parsing_tentatively
2588 (cp_parser *);
2589 static bool cp_parser_uncommitted_to_tentative_parse_p
2590 (cp_parser *);
2591 static void cp_parser_error
2592 (cp_parser *, const char *);
2593 static void cp_parser_name_lookup_error
2594 (cp_parser *, tree, tree, name_lookup_error, location_t);
2595 static bool cp_parser_simulate_error
2596 (cp_parser *);
2597 static bool cp_parser_check_type_definition
2598 (cp_parser *);
2599 static void cp_parser_check_for_definition_in_return_type
2600 (cp_declarator *, tree, location_t type_location);
2601 static void cp_parser_check_for_invalid_template_id
2602 (cp_parser *, tree, enum tag_types, location_t location);
2603 static bool cp_parser_non_integral_constant_expression
2604 (cp_parser *, non_integral_constant);
2605 static void cp_parser_diagnose_invalid_type_name
2606 (cp_parser *, tree, location_t);
2607 static bool cp_parser_parse_and_diagnose_invalid_type_name
2608 (cp_parser *);
2609 static int cp_parser_skip_to_closing_parenthesis
2610 (cp_parser *, bool, bool, bool);
2611 static void cp_parser_skip_to_end_of_statement
2612 (cp_parser *);
2613 static void cp_parser_consume_semicolon_at_end_of_statement
2614 (cp_parser *);
2615 static void cp_parser_skip_to_end_of_block_or_statement
2616 (cp_parser *);
2617 static bool cp_parser_skip_to_closing_brace
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_template_parameter_list
2620 (cp_parser *);
2621 static void cp_parser_skip_to_pragma_eol
2622 (cp_parser*, cp_token *);
2623 static bool cp_parser_error_occurred
2624 (cp_parser *);
2625 static bool cp_parser_allow_gnu_extensions_p
2626 (cp_parser *);
2627 static bool cp_parser_is_pure_string_literal
2628 (cp_token *);
2629 static bool cp_parser_is_string_literal
2630 (cp_token *);
2631 static bool cp_parser_is_keyword
2632 (cp_token *, enum rid);
2633 static tree cp_parser_make_typename_type
2634 (cp_parser *, tree, location_t location);
2635 static cp_declarator * cp_parser_make_indirect_declarator
2636 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2637 static bool cp_parser_compound_literal_p
2638 (cp_parser *);
2639 static bool cp_parser_array_designator_p
2640 (cp_parser *);
2641 static bool cp_parser_init_statement_p
2642 (cp_parser *);
2643 static bool cp_parser_skip_to_closing_square_bracket
2644 (cp_parser *);
2646 /* Concept-related syntactic transformations */
2648 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2649 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2651 // -------------------------------------------------------------------------- //
2652 // Unevaluated Operand Guard
2654 // Implementation of an RAII helper for unevaluated operand parsing.
2655 cp_unevaluated::cp_unevaluated ()
2657 ++cp_unevaluated_operand;
2658 ++c_inhibit_evaluation_warnings;
2661 cp_unevaluated::~cp_unevaluated ()
2663 --c_inhibit_evaluation_warnings;
2664 --cp_unevaluated_operand;
2667 // -------------------------------------------------------------------------- //
2668 // Tentative Parsing
2670 /* Returns nonzero if we are parsing tentatively. */
2672 static inline bool
2673 cp_parser_parsing_tentatively (cp_parser* parser)
2675 return parser->context->next != NULL;
2678 /* Returns nonzero if TOKEN is a string literal. */
2680 static bool
2681 cp_parser_is_pure_string_literal (cp_token* token)
2683 return (token->type == CPP_STRING ||
2684 token->type == CPP_STRING16 ||
2685 token->type == CPP_STRING32 ||
2686 token->type == CPP_WSTRING ||
2687 token->type == CPP_UTF8STRING);
2690 /* Returns nonzero if TOKEN is a string literal
2691 of a user-defined string literal. */
2693 static bool
2694 cp_parser_is_string_literal (cp_token* token)
2696 return (cp_parser_is_pure_string_literal (token) ||
2697 token->type == CPP_STRING_USERDEF ||
2698 token->type == CPP_STRING16_USERDEF ||
2699 token->type == CPP_STRING32_USERDEF ||
2700 token->type == CPP_WSTRING_USERDEF ||
2701 token->type == CPP_UTF8STRING_USERDEF);
2704 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2706 static bool
2707 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2709 return token->keyword == keyword;
2712 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2713 PRAGMA_NONE. */
2715 static enum pragma_kind
2716 cp_parser_pragma_kind (cp_token *token)
2718 if (token->type != CPP_PRAGMA)
2719 return PRAGMA_NONE;
2720 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2721 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2724 /* Helper function for cp_parser_error.
2725 Having peeked a token of kind TOK1_KIND that might signify
2726 a conflict marker, peek successor tokens to determine
2727 if we actually do have a conflict marker.
2728 Specifically, we consider a run of 7 '<', '=' or '>' characters
2729 at the start of a line as a conflict marker.
2730 These come through the lexer as three pairs and a single,
2731 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2732 If it returns true, *OUT_LOC is written to with the location/range
2733 of the marker. */
2735 static bool
2736 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2737 location_t *out_loc)
2739 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2740 if (token2->type != tok1_kind)
2741 return false;
2742 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2743 if (token3->type != tok1_kind)
2744 return false;
2745 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2746 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2747 return false;
2749 /* It must be at the start of the line. */
2750 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2751 if (LOCATION_COLUMN (start_loc) != 1)
2752 return false;
2754 /* We have a conflict marker. Construct a location of the form:
2755 <<<<<<<
2756 ^~~~~~~
2757 with start == caret, finishing at the end of the marker. */
2758 location_t finish_loc = get_finish (token4->location);
2759 *out_loc = make_location (start_loc, start_loc, finish_loc);
2761 return true;
2764 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2765 RT_CLOSE_PAREN. */
2767 static const char *
2768 get_matching_symbol (required_token token_desc)
2770 switch (token_desc)
2772 default:
2773 gcc_unreachable ();
2774 return "";
2775 case RT_CLOSE_BRACE:
2776 return "{";
2777 case RT_CLOSE_PAREN:
2778 return "(";
2782 /* Attempt to convert TOKEN_DESC from a required_token to an
2783 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2785 static enum cpp_ttype
2786 get_required_cpp_ttype (required_token token_desc)
2788 switch (token_desc)
2790 case RT_SEMICOLON:
2791 return CPP_SEMICOLON;
2792 case RT_OPEN_PAREN:
2793 return CPP_OPEN_PAREN;
2794 case RT_CLOSE_BRACE:
2795 return CPP_CLOSE_BRACE;
2796 case RT_OPEN_BRACE:
2797 return CPP_OPEN_BRACE;
2798 case RT_CLOSE_SQUARE:
2799 return CPP_CLOSE_SQUARE;
2800 case RT_OPEN_SQUARE:
2801 return CPP_OPEN_SQUARE;
2802 case RT_COMMA:
2803 return CPP_COMMA;
2804 case RT_COLON:
2805 return CPP_COLON;
2806 case RT_CLOSE_PAREN:
2807 return CPP_CLOSE_PAREN;
2809 default:
2810 /* Use CPP_EOF as a "no completions possible" code. */
2811 return CPP_EOF;
2816 /* Subroutine of cp_parser_error and cp_parser_required_error.
2818 Issue a diagnostic of the form
2819 FILE:LINE: MESSAGE before TOKEN
2820 where TOKEN is the next token in the input stream. MESSAGE
2821 (specified by the caller) is usually of the form "expected
2822 OTHER-TOKEN".
2824 This bypasses the check for tentative passing, and potentially
2825 adds material needed by cp_parser_required_error.
2827 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2828 suggesting insertion of the missing token.
2830 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2831 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2832 location. */
2834 static void
2835 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2836 required_token missing_token_desc,
2837 location_t matching_location)
2839 cp_token *token = cp_lexer_peek_token (parser->lexer);
2840 /* This diagnostic makes more sense if it is tagged to the line
2841 of the token we just peeked at. */
2842 cp_lexer_set_source_position_from_token (token);
2844 if (token->type == CPP_PRAGMA)
2846 error_at (token->location,
2847 "%<#pragma%> is not allowed here");
2848 cp_parser_skip_to_pragma_eol (parser, token);
2849 return;
2852 /* If this is actually a conflict marker, report it as such. */
2853 if (token->type == CPP_LSHIFT
2854 || token->type == CPP_RSHIFT
2855 || token->type == CPP_EQ_EQ)
2857 location_t loc;
2858 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2860 error_at (loc, "version control conflict marker in file");
2861 return;
2865 gcc_rich_location richloc (input_location);
2867 bool added_matching_location = false;
2869 if (missing_token_desc != RT_NONE)
2871 /* Potentially supply a fix-it hint, suggesting to add the
2872 missing token immediately after the *previous* token.
2873 This may move the primary location within richloc. */
2874 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2875 location_t prev_token_loc
2876 = cp_lexer_previous_token (parser->lexer)->location;
2877 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2879 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2880 Attempt to consolidate diagnostics by printing it as a
2881 secondary range within the main diagnostic. */
2882 if (matching_location != UNKNOWN_LOCATION)
2883 added_matching_location
2884 = richloc.add_location_if_nearby (matching_location);
2887 /* Actually emit the error. */
2888 c_parse_error (gmsgid,
2889 /* Because c_parser_error does not understand
2890 CPP_KEYWORD, keywords are treated like
2891 identifiers. */
2892 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2893 token->u.value, token->flags, &richloc);
2895 if (missing_token_desc != RT_NONE)
2897 /* If we weren't able to consolidate matching_location, then
2898 print it as a secondary diagnostic. */
2899 if (matching_location != UNKNOWN_LOCATION
2900 && !added_matching_location)
2901 inform (matching_location, "to match this %qs",
2902 get_matching_symbol (missing_token_desc));
2906 /* If not parsing tentatively, issue a diagnostic of the form
2907 FILE:LINE: MESSAGE before TOKEN
2908 where TOKEN is the next token in the input stream. MESSAGE
2909 (specified by the caller) is usually of the form "expected
2910 OTHER-TOKEN". */
2912 static void
2913 cp_parser_error (cp_parser* parser, const char* gmsgid)
2915 if (!cp_parser_simulate_error (parser))
2916 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2919 /* Issue an error about name-lookup failing. NAME is the
2920 IDENTIFIER_NODE DECL is the result of
2921 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2922 the thing that we hoped to find. */
2924 static void
2925 cp_parser_name_lookup_error (cp_parser* parser,
2926 tree name,
2927 tree decl,
2928 name_lookup_error desired,
2929 location_t location)
2931 /* If name lookup completely failed, tell the user that NAME was not
2932 declared. */
2933 if (decl == error_mark_node)
2935 if (parser->scope && parser->scope != global_namespace)
2936 error_at (location, "%<%E::%E%> has not been declared",
2937 parser->scope, name);
2938 else if (parser->scope == global_namespace)
2939 error_at (location, "%<::%E%> has not been declared", name);
2940 else if (parser->object_scope
2941 && !CLASS_TYPE_P (parser->object_scope))
2942 error_at (location, "request for member %qE in non-class type %qT",
2943 name, parser->object_scope);
2944 else if (parser->object_scope)
2945 error_at (location, "%<%T::%E%> has not been declared",
2946 parser->object_scope, name);
2947 else
2948 error_at (location, "%qE has not been declared", name);
2950 else if (parser->scope && parser->scope != global_namespace)
2952 switch (desired)
2954 case NLE_TYPE:
2955 error_at (location, "%<%E::%E%> is not a type",
2956 parser->scope, name);
2957 break;
2958 case NLE_CXX98:
2959 error_at (location, "%<%E::%E%> is not a class or namespace",
2960 parser->scope, name);
2961 break;
2962 case NLE_NOT_CXX98:
2963 error_at (location,
2964 "%<%E::%E%> is not a class, namespace, or enumeration",
2965 parser->scope, name);
2966 break;
2967 default:
2968 gcc_unreachable ();
2972 else if (parser->scope == global_namespace)
2974 switch (desired)
2976 case NLE_TYPE:
2977 error_at (location, "%<::%E%> is not a type", name);
2978 break;
2979 case NLE_CXX98:
2980 error_at (location, "%<::%E%> is not a class or namespace", name);
2981 break;
2982 case NLE_NOT_CXX98:
2983 error_at (location,
2984 "%<::%E%> is not a class, namespace, or enumeration",
2985 name);
2986 break;
2987 default:
2988 gcc_unreachable ();
2991 else
2993 switch (desired)
2995 case NLE_TYPE:
2996 error_at (location, "%qE is not a type", name);
2997 break;
2998 case NLE_CXX98:
2999 error_at (location, "%qE is not a class or namespace", name);
3000 break;
3001 case NLE_NOT_CXX98:
3002 error_at (location,
3003 "%qE is not a class, namespace, or enumeration", name);
3004 break;
3005 default:
3006 gcc_unreachable ();
3011 /* If we are parsing tentatively, remember that an error has occurred
3012 during this tentative parse. Returns true if the error was
3013 simulated; false if a message should be issued by the caller. */
3015 static bool
3016 cp_parser_simulate_error (cp_parser* parser)
3018 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3020 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3021 return true;
3023 return false;
3026 /* This function is called when a type is defined. If type
3027 definitions are forbidden at this point, an error message is
3028 issued. */
3030 static bool
3031 cp_parser_check_type_definition (cp_parser* parser)
3033 /* If types are forbidden here, issue a message. */
3034 if (parser->type_definition_forbidden_message)
3036 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3037 in the message need to be interpreted. */
3038 error (parser->type_definition_forbidden_message);
3039 return false;
3041 return true;
3044 /* This function is called when the DECLARATOR is processed. The TYPE
3045 was a type defined in the decl-specifiers. If it is invalid to
3046 define a type in the decl-specifiers for DECLARATOR, an error is
3047 issued. TYPE_LOCATION is the location of TYPE and is used
3048 for error reporting. */
3050 static void
3051 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3052 tree type, location_t type_location)
3054 /* [dcl.fct] forbids type definitions in return types.
3055 Unfortunately, it's not easy to know whether or not we are
3056 processing a return type until after the fact. */
3057 while (declarator
3058 && (declarator->kind == cdk_pointer
3059 || declarator->kind == cdk_reference
3060 || declarator->kind == cdk_ptrmem))
3061 declarator = declarator->declarator;
3062 if (declarator
3063 && declarator->kind == cdk_function)
3065 error_at (type_location,
3066 "new types may not be defined in a return type");
3067 inform (type_location,
3068 "(perhaps a semicolon is missing after the definition of %qT)",
3069 type);
3073 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3074 "<" in any valid C++ program. If the next token is indeed "<",
3075 issue a message warning the user about what appears to be an
3076 invalid attempt to form a template-id. LOCATION is the location
3077 of the type-specifier (TYPE) */
3079 static void
3080 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3081 tree type,
3082 enum tag_types tag_type,
3083 location_t location)
3085 cp_token_position start = 0;
3087 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3089 if (TREE_CODE (type) == TYPE_DECL)
3090 type = TREE_TYPE (type);
3091 if (TYPE_P (type) && !template_placeholder_p (type))
3092 error_at (location, "%qT is not a template", type);
3093 else if (identifier_p (type))
3095 if (tag_type != none_type)
3096 error_at (location, "%qE is not a class template", type);
3097 else
3098 error_at (location, "%qE is not a template", type);
3100 else
3101 error_at (location, "invalid template-id");
3102 /* Remember the location of the invalid "<". */
3103 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3104 start = cp_lexer_token_position (parser->lexer, true);
3105 /* Consume the "<". */
3106 cp_lexer_consume_token (parser->lexer);
3107 /* Parse the template arguments. */
3108 cp_parser_enclosed_template_argument_list (parser);
3109 /* Permanently remove the invalid template arguments so that
3110 this error message is not issued again. */
3111 if (start)
3112 cp_lexer_purge_tokens_after (parser->lexer, start);
3116 /* If parsing an integral constant-expression, issue an error message
3117 about the fact that THING appeared and return true. Otherwise,
3118 return false. In either case, set
3119 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3121 static bool
3122 cp_parser_non_integral_constant_expression (cp_parser *parser,
3123 non_integral_constant thing)
3125 parser->non_integral_constant_expression_p = true;
3126 if (parser->integral_constant_expression_p)
3128 if (!parser->allow_non_integral_constant_expression_p)
3130 const char *msg = NULL;
3131 switch (thing)
3133 case NIC_FLOAT:
3134 pedwarn (input_location, OPT_Wpedantic,
3135 "ISO C++ forbids using a floating-point literal "
3136 "in a constant-expression");
3137 return true;
3138 case NIC_CAST:
3139 error ("a cast to a type other than an integral or "
3140 "enumeration type cannot appear in a "
3141 "constant-expression");
3142 return true;
3143 case NIC_TYPEID:
3144 error ("%<typeid%> operator "
3145 "cannot appear in a constant-expression");
3146 return true;
3147 case NIC_NCC:
3148 error ("non-constant compound literals "
3149 "cannot appear in a constant-expression");
3150 return true;
3151 case NIC_FUNC_CALL:
3152 error ("a function call "
3153 "cannot appear in a constant-expression");
3154 return true;
3155 case NIC_INC:
3156 error ("an increment "
3157 "cannot appear in a constant-expression");
3158 return true;
3159 case NIC_DEC:
3160 error ("an decrement "
3161 "cannot appear in a constant-expression");
3162 return true;
3163 case NIC_ARRAY_REF:
3164 error ("an array reference "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_ADDR_LABEL:
3168 error ("the address of a label "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_OVERLOADED:
3172 error ("calls to overloaded operators "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_ASSIGNMENT:
3176 error ("an assignment cannot appear in a constant-expression");
3177 return true;
3178 case NIC_COMMA:
3179 error ("a comma operator "
3180 "cannot appear in a constant-expression");
3181 return true;
3182 case NIC_CONSTRUCTOR:
3183 error ("a call to a constructor "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_TRANSACTION:
3187 error ("a transaction expression "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_THIS:
3191 msg = "this";
3192 break;
3193 case NIC_FUNC_NAME:
3194 msg = "__FUNCTION__";
3195 break;
3196 case NIC_PRETTY_FUNC:
3197 msg = "__PRETTY_FUNCTION__";
3198 break;
3199 case NIC_C99_FUNC:
3200 msg = "__func__";
3201 break;
3202 case NIC_VA_ARG:
3203 msg = "va_arg";
3204 break;
3205 case NIC_ARROW:
3206 msg = "->";
3207 break;
3208 case NIC_POINT:
3209 msg = ".";
3210 break;
3211 case NIC_STAR:
3212 msg = "*";
3213 break;
3214 case NIC_ADDR:
3215 msg = "&";
3216 break;
3217 case NIC_PREINCREMENT:
3218 msg = "++";
3219 break;
3220 case NIC_PREDECREMENT:
3221 msg = "--";
3222 break;
3223 case NIC_NEW:
3224 msg = "new";
3225 break;
3226 case NIC_DEL:
3227 msg = "delete";
3228 break;
3229 default:
3230 gcc_unreachable ();
3232 if (msg)
3233 error ("%qs cannot appear in a constant-expression", msg);
3234 return true;
3237 return false;
3240 /* Emit a diagnostic for an invalid type name. This function commits
3241 to the current active tentative parse, if any. (Otherwise, the
3242 problematic construct might be encountered again later, resulting
3243 in duplicate error messages.) LOCATION is the location of ID. */
3245 static void
3246 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3247 location_t location)
3249 tree decl, ambiguous_decls;
3250 cp_parser_commit_to_tentative_parse (parser);
3251 /* Try to lookup the identifier. */
3252 decl = cp_parser_lookup_name (parser, id, none_type,
3253 /*is_template=*/false,
3254 /*is_namespace=*/false,
3255 /*check_dependency=*/true,
3256 &ambiguous_decls, location);
3257 if (ambiguous_decls)
3258 /* If the lookup was ambiguous, an error will already have
3259 been issued. */
3260 return;
3261 /* If the lookup found a template-name, it means that the user forgot
3262 to specify an argument list. Emit a useful error message. */
3263 if (DECL_TYPE_TEMPLATE_P (decl))
3265 error_at (location,
3266 "invalid use of template-name %qE without an argument list",
3267 decl);
3268 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3269 inform (location, "class template argument deduction is only available "
3270 "with -std=c++17 or -std=gnu++17");
3271 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3273 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3274 error_at (location, "invalid use of destructor %qD as a type", id);
3275 else if (TREE_CODE (decl) == TYPE_DECL)
3276 /* Something like 'unsigned A a;' */
3277 error_at (location, "invalid combination of multiple type-specifiers");
3278 else if (!parser->scope)
3280 /* Issue an error message. */
3281 name_hint hint;
3282 if (TREE_CODE (id) == IDENTIFIER_NODE)
3283 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3284 if (hint)
3286 gcc_rich_location richloc (location);
3287 richloc.add_fixit_replace (hint.suggestion ());
3288 error_at (&richloc,
3289 "%qE does not name a type; did you mean %qs?",
3290 id, hint.suggestion ());
3292 else
3293 error_at (location, "%qE does not name a type", id);
3294 /* If we're in a template class, it's possible that the user was
3295 referring to a type from a base class. For example:
3297 template <typename T> struct A { typedef T X; };
3298 template <typename T> struct B : public A<T> { X x; };
3300 The user should have said "typename A<T>::X". */
3301 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3302 inform (location, "C++11 %<constexpr%> only available with "
3303 "-std=c++11 or -std=gnu++11");
3304 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3305 inform (location, "C++11 %<noexcept%> only available with "
3306 "-std=c++11 or -std=gnu++11");
3307 else if (cxx_dialect < cxx11
3308 && TREE_CODE (id) == IDENTIFIER_NODE
3309 && id_equal (id, "thread_local"))
3310 inform (location, "C++11 %<thread_local%> only available with "
3311 "-std=c++11 or -std=gnu++11");
3312 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3313 inform (location, "%<concept%> only available with -fconcepts");
3314 else if (processing_template_decl && current_class_type
3315 && TYPE_BINFO (current_class_type))
3317 tree b;
3319 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3321 b = TREE_CHAIN (b))
3323 tree base_type = BINFO_TYPE (b);
3324 if (CLASS_TYPE_P (base_type)
3325 && dependent_type_p (base_type))
3327 tree field;
3328 /* Go from a particular instantiation of the
3329 template (which will have an empty TYPE_FIELDs),
3330 to the main version. */
3331 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3332 for (field = TYPE_FIELDS (base_type);
3333 field;
3334 field = DECL_CHAIN (field))
3335 if (TREE_CODE (field) == TYPE_DECL
3336 && DECL_NAME (field) == id)
3338 inform (location,
3339 "(perhaps %<typename %T::%E%> was intended)",
3340 BINFO_TYPE (b), id);
3341 break;
3343 if (field)
3344 break;
3349 /* Here we diagnose qualified-ids where the scope is actually correct,
3350 but the identifier does not resolve to a valid type name. */
3351 else if (parser->scope != error_mark_node)
3353 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3355 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3356 error_at (location_of (id),
3357 "%qE in namespace %qE does not name a template type",
3358 id, parser->scope);
3359 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3360 error_at (location_of (id),
3361 "%qE in namespace %qE does not name a template type",
3362 TREE_OPERAND (id, 0), parser->scope);
3363 else
3364 error_at (location_of (id),
3365 "%qE in namespace %qE does not name a type",
3366 id, parser->scope);
3367 if (DECL_P (decl))
3368 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3369 else if (decl == error_mark_node)
3370 suggest_alternative_in_explicit_scope (location, id,
3371 parser->scope);
3373 else if (CLASS_TYPE_P (parser->scope)
3374 && constructor_name_p (id, parser->scope))
3376 /* A<T>::A<T>() */
3377 error_at (location, "%<%T::%E%> names the constructor, not"
3378 " the type", parser->scope, id);
3379 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3380 error_at (location, "and %qT has no template constructors",
3381 parser->scope);
3383 else if (TYPE_P (parser->scope)
3384 && dependent_scope_p (parser->scope))
3386 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3387 error_at (location,
3388 "need %<typename%> before %<%T::%D::%E%> because "
3389 "%<%T::%D%> is a dependent scope",
3390 TYPE_CONTEXT (parser->scope),
3391 TYPENAME_TYPE_FULLNAME (parser->scope),
3393 TYPE_CONTEXT (parser->scope),
3394 TYPENAME_TYPE_FULLNAME (parser->scope));
3395 else
3396 error_at (location, "need %<typename%> before %<%T::%E%> because "
3397 "%qT is a dependent scope",
3398 parser->scope, id, parser->scope);
3400 else if (TYPE_P (parser->scope))
3402 if (!COMPLETE_TYPE_P (parser->scope))
3403 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3404 parser->scope);
3405 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3406 error_at (location_of (id),
3407 "%qE in %q#T does not name a template type",
3408 id, parser->scope);
3409 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3410 error_at (location_of (id),
3411 "%qE in %q#T does not name a template type",
3412 TREE_OPERAND (id, 0), parser->scope);
3413 else
3414 error_at (location_of (id),
3415 "%qE in %q#T does not name a type",
3416 id, parser->scope);
3417 if (DECL_P (decl))
3418 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3420 else
3421 gcc_unreachable ();
3425 /* Check for a common situation where a type-name should be present,
3426 but is not, and issue a sensible error message. Returns true if an
3427 invalid type-name was detected.
3429 The situation handled by this function are variable declarations of the
3430 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3431 Usually, `ID' should name a type, but if we got here it means that it
3432 does not. We try to emit the best possible error message depending on
3433 how exactly the id-expression looks like. */
3435 static bool
3436 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3438 tree id;
3439 cp_token *token = cp_lexer_peek_token (parser->lexer);
3441 /* Avoid duplicate error about ambiguous lookup. */
3442 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3444 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3445 if (next->type == CPP_NAME && next->error_reported)
3446 goto out;
3449 cp_parser_parse_tentatively (parser);
3450 id = cp_parser_id_expression (parser,
3451 /*template_keyword_p=*/false,
3452 /*check_dependency_p=*/true,
3453 /*template_p=*/NULL,
3454 /*declarator_p=*/true,
3455 /*optional_p=*/false);
3456 /* If the next token is a (, this is a function with no explicit return
3457 type, i.e. constructor, destructor or conversion op. */
3458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3459 || TREE_CODE (id) == TYPE_DECL)
3461 cp_parser_abort_tentative_parse (parser);
3462 return false;
3464 if (!cp_parser_parse_definitely (parser))
3465 return false;
3467 /* Emit a diagnostic for the invalid type. */
3468 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3469 out:
3470 /* If we aren't in the middle of a declarator (i.e. in a
3471 parameter-declaration-clause), skip to the end of the declaration;
3472 there's no point in trying to process it. */
3473 if (!parser->in_declarator_p)
3474 cp_parser_skip_to_end_of_block_or_statement (parser);
3475 return true;
3478 /* Consume tokens up to, and including, the next non-nested closing `)'.
3479 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3480 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3481 found an unnested token of that type. */
3483 static int
3484 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3485 bool recovering,
3486 cpp_ttype or_ttype,
3487 bool consume_paren)
3489 unsigned paren_depth = 0;
3490 unsigned brace_depth = 0;
3491 unsigned square_depth = 0;
3493 if (recovering && or_ttype == CPP_EOF
3494 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3495 return 0;
3497 while (true)
3499 cp_token * token = cp_lexer_peek_token (parser->lexer);
3501 /* Have we found what we're looking for before the closing paren? */
3502 if (token->type == or_ttype && or_ttype != CPP_EOF
3503 && !brace_depth && !paren_depth && !square_depth)
3504 return -1;
3506 switch (token->type)
3508 case CPP_EOF:
3509 case CPP_PRAGMA_EOL:
3510 /* If we've run out of tokens, then there is no closing `)'. */
3511 return 0;
3513 /* This is good for lambda expression capture-lists. */
3514 case CPP_OPEN_SQUARE:
3515 ++square_depth;
3516 break;
3517 case CPP_CLOSE_SQUARE:
3518 if (!square_depth--)
3519 return 0;
3520 break;
3522 case CPP_SEMICOLON:
3523 /* This matches the processing in skip_to_end_of_statement. */
3524 if (!brace_depth)
3525 return 0;
3526 break;
3528 case CPP_OPEN_BRACE:
3529 ++brace_depth;
3530 break;
3531 case CPP_CLOSE_BRACE:
3532 if (!brace_depth--)
3533 return 0;
3534 break;
3536 case CPP_OPEN_PAREN:
3537 if (!brace_depth)
3538 ++paren_depth;
3539 break;
3541 case CPP_CLOSE_PAREN:
3542 if (!brace_depth && !paren_depth--)
3544 if (consume_paren)
3545 cp_lexer_consume_token (parser->lexer);
3546 return 1;
3548 break;
3550 default:
3551 break;
3554 /* Consume the token. */
3555 cp_lexer_consume_token (parser->lexer);
3559 /* Consume tokens up to, and including, the next non-nested closing `)'.
3560 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3561 are doing error recovery. Returns -1 if OR_COMMA is true and we
3562 found an unnested token of that type. */
3564 static int
3565 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3566 bool recovering,
3567 bool or_comma,
3568 bool consume_paren)
3570 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3571 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3572 ttype, consume_paren);
3575 /* Consume tokens until we reach the end of the current statement.
3576 Normally, that will be just before consuming a `;'. However, if a
3577 non-nested `}' comes first, then we stop before consuming that. */
3579 static void
3580 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3582 unsigned nesting_depth = 0;
3584 /* Unwind generic function template scope if necessary. */
3585 if (parser->fully_implicit_function_template_p)
3586 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3588 while (true)
3590 cp_token *token = cp_lexer_peek_token (parser->lexer);
3592 switch (token->type)
3594 case CPP_EOF:
3595 case CPP_PRAGMA_EOL:
3596 /* If we've run out of tokens, stop. */
3597 return;
3599 case CPP_SEMICOLON:
3600 /* If the next token is a `;', we have reached the end of the
3601 statement. */
3602 if (!nesting_depth)
3603 return;
3604 break;
3606 case CPP_CLOSE_BRACE:
3607 /* If this is a non-nested '}', stop before consuming it.
3608 That way, when confronted with something like:
3610 { 3 + }
3612 we stop before consuming the closing '}', even though we
3613 have not yet reached a `;'. */
3614 if (nesting_depth == 0)
3615 return;
3617 /* If it is the closing '}' for a block that we have
3618 scanned, stop -- but only after consuming the token.
3619 That way given:
3621 void f g () { ... }
3622 typedef int I;
3624 we will stop after the body of the erroneously declared
3625 function, but before consuming the following `typedef'
3626 declaration. */
3627 if (--nesting_depth == 0)
3629 cp_lexer_consume_token (parser->lexer);
3630 return;
3632 break;
3634 case CPP_OPEN_BRACE:
3635 ++nesting_depth;
3636 break;
3638 default:
3639 break;
3642 /* Consume the token. */
3643 cp_lexer_consume_token (parser->lexer);
3647 /* This function is called at the end of a statement or declaration.
3648 If the next token is a semicolon, it is consumed; otherwise, error
3649 recovery is attempted. */
3651 static void
3652 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3654 /* Look for the trailing `;'. */
3655 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3657 /* If there is additional (erroneous) input, skip to the end of
3658 the statement. */
3659 cp_parser_skip_to_end_of_statement (parser);
3660 /* If the next token is now a `;', consume it. */
3661 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3662 cp_lexer_consume_token (parser->lexer);
3666 /* Skip tokens until we have consumed an entire block, or until we
3667 have consumed a non-nested `;'. */
3669 static void
3670 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3672 int nesting_depth = 0;
3674 /* Unwind generic function template scope if necessary. */
3675 if (parser->fully_implicit_function_template_p)
3676 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3678 while (nesting_depth >= 0)
3680 cp_token *token = cp_lexer_peek_token (parser->lexer);
3682 switch (token->type)
3684 case CPP_EOF:
3685 case CPP_PRAGMA_EOL:
3686 /* If we've run out of tokens, stop. */
3687 return;
3689 case CPP_SEMICOLON:
3690 /* Stop if this is an unnested ';'. */
3691 if (!nesting_depth)
3692 nesting_depth = -1;
3693 break;
3695 case CPP_CLOSE_BRACE:
3696 /* Stop if this is an unnested '}', or closes the outermost
3697 nesting level. */
3698 nesting_depth--;
3699 if (nesting_depth < 0)
3700 return;
3701 if (!nesting_depth)
3702 nesting_depth = -1;
3703 break;
3705 case CPP_OPEN_BRACE:
3706 /* Nest. */
3707 nesting_depth++;
3708 break;
3710 default:
3711 break;
3714 /* Consume the token. */
3715 cp_lexer_consume_token (parser->lexer);
3719 /* Skip tokens until a non-nested closing curly brace is the next
3720 token, or there are no more tokens. Return true in the first case,
3721 false otherwise. */
3723 static bool
3724 cp_parser_skip_to_closing_brace (cp_parser *parser)
3726 unsigned nesting_depth = 0;
3728 while (true)
3730 cp_token *token = cp_lexer_peek_token (parser->lexer);
3732 switch (token->type)
3734 case CPP_EOF:
3735 case CPP_PRAGMA_EOL:
3736 /* If we've run out of tokens, stop. */
3737 return false;
3739 case CPP_CLOSE_BRACE:
3740 /* If the next token is a non-nested `}', then we have reached
3741 the end of the current block. */
3742 if (nesting_depth-- == 0)
3743 return true;
3744 break;
3746 case CPP_OPEN_BRACE:
3747 /* If it the next token is a `{', then we are entering a new
3748 block. Consume the entire block. */
3749 ++nesting_depth;
3750 break;
3752 default:
3753 break;
3756 /* Consume the token. */
3757 cp_lexer_consume_token (parser->lexer);
3761 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3762 parameter is the PRAGMA token, allowing us to purge the entire pragma
3763 sequence. */
3765 static void
3766 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3768 cp_token *token;
3770 parser->lexer->in_pragma = false;
3773 token = cp_lexer_consume_token (parser->lexer);
3774 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3776 /* Ensure that the pragma is not parsed again. */
3777 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3780 /* Require pragma end of line, resyncing with it as necessary. The
3781 arguments are as for cp_parser_skip_to_pragma_eol. */
3783 static void
3784 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3786 parser->lexer->in_pragma = false;
3787 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3788 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3791 /* This is a simple wrapper around make_typename_type. When the id is
3792 an unresolved identifier node, we can provide a superior diagnostic
3793 using cp_parser_diagnose_invalid_type_name. */
3795 static tree
3796 cp_parser_make_typename_type (cp_parser *parser, tree id,
3797 location_t id_location)
3799 tree result;
3800 if (identifier_p (id))
3802 result = make_typename_type (parser->scope, id, typename_type,
3803 /*complain=*/tf_none);
3804 if (result == error_mark_node)
3805 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3806 return result;
3808 return make_typename_type (parser->scope, id, typename_type, tf_error);
3811 /* This is a wrapper around the
3812 make_{pointer,ptrmem,reference}_declarator functions that decides
3813 which one to call based on the CODE and CLASS_TYPE arguments. The
3814 CODE argument should be one of the values returned by
3815 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3816 appertain to the pointer or reference. */
3818 static cp_declarator *
3819 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3820 cp_cv_quals cv_qualifiers,
3821 cp_declarator *target,
3822 tree attributes)
3824 if (code == ERROR_MARK)
3825 return cp_error_declarator;
3827 if (code == INDIRECT_REF)
3828 if (class_type == NULL_TREE)
3829 return make_pointer_declarator (cv_qualifiers, target, attributes);
3830 else
3831 return make_ptrmem_declarator (cv_qualifiers, class_type,
3832 target, attributes);
3833 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3834 return make_reference_declarator (cv_qualifiers, target,
3835 false, attributes);
3836 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3837 return make_reference_declarator (cv_qualifiers, target,
3838 true, attributes);
3839 gcc_unreachable ();
3842 /* Create a new C++ parser. */
3844 static cp_parser *
3845 cp_parser_new (void)
3847 cp_parser *parser;
3848 cp_lexer *lexer;
3849 unsigned i;
3851 /* cp_lexer_new_main is called before doing GC allocation because
3852 cp_lexer_new_main might load a PCH file. */
3853 lexer = cp_lexer_new_main ();
3855 /* Initialize the binops_by_token so that we can get the tree
3856 directly from the token. */
3857 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3858 binops_by_token[binops[i].token_type] = binops[i];
3860 parser = ggc_cleared_alloc<cp_parser> ();
3861 parser->lexer = lexer;
3862 parser->context = cp_parser_context_new (NULL);
3864 /* For now, we always accept GNU extensions. */
3865 parser->allow_gnu_extensions_p = 1;
3867 /* The `>' token is a greater-than operator, not the end of a
3868 template-id. */
3869 parser->greater_than_is_operator_p = true;
3871 parser->default_arg_ok_p = true;
3873 /* We are not parsing a constant-expression. */
3874 parser->integral_constant_expression_p = false;
3875 parser->allow_non_integral_constant_expression_p = false;
3876 parser->non_integral_constant_expression_p = false;
3878 /* Local variable names are not forbidden. */
3879 parser->local_variables_forbidden_p = false;
3881 /* We are not processing an `extern "C"' declaration. */
3882 parser->in_unbraced_linkage_specification_p = false;
3884 /* We are not processing a declarator. */
3885 parser->in_declarator_p = false;
3887 /* We are not processing a template-argument-list. */
3888 parser->in_template_argument_list_p = false;
3890 /* We are not in an iteration statement. */
3891 parser->in_statement = 0;
3893 /* We are not in a switch statement. */
3894 parser->in_switch_statement_p = false;
3896 /* We are not parsing a type-id inside an expression. */
3897 parser->in_type_id_in_expr_p = false;
3899 /* Declarations aren't implicitly extern "C". */
3900 parser->implicit_extern_c = false;
3902 /* String literals should be translated to the execution character set. */
3903 parser->translate_strings_p = true;
3905 /* We are not parsing a function body. */
3906 parser->in_function_body = false;
3908 /* We can correct until told otherwise. */
3909 parser->colon_corrects_to_scope_p = true;
3911 /* The unparsed function queue is empty. */
3912 push_unparsed_function_queues (parser);
3914 /* There are no classes being defined. */
3915 parser->num_classes_being_defined = 0;
3917 /* No template parameters apply. */
3918 parser->num_template_parameter_lists = 0;
3920 /* Special parsing data structures. */
3921 parser->omp_declare_simd = NULL;
3922 parser->oacc_routine = NULL;
3924 /* Not declaring an implicit function template. */
3925 parser->auto_is_implicit_function_template_parm_p = false;
3926 parser->fully_implicit_function_template_p = false;
3927 parser->implicit_template_parms = 0;
3928 parser->implicit_template_scope = 0;
3930 /* Allow constrained-type-specifiers. */
3931 parser->prevent_constrained_type_specifiers = 0;
3933 /* We haven't yet seen an 'extern "C"'. */
3934 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3936 return parser;
3939 /* Create a cp_lexer structure which will emit the tokens in CACHE
3940 and push it onto the parser's lexer stack. This is used for delayed
3941 parsing of in-class method bodies and default arguments, and should
3942 not be confused with tentative parsing. */
3943 static void
3944 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3946 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3947 lexer->next = parser->lexer;
3948 parser->lexer = lexer;
3950 /* Move the current source position to that of the first token in the
3951 new lexer. */
3952 cp_lexer_set_source_position_from_token (lexer->next_token);
3955 /* Pop the top lexer off the parser stack. This is never used for the
3956 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3957 static void
3958 cp_parser_pop_lexer (cp_parser *parser)
3960 cp_lexer *lexer = parser->lexer;
3961 parser->lexer = lexer->next;
3962 cp_lexer_destroy (lexer);
3964 /* Put the current source position back where it was before this
3965 lexer was pushed. */
3966 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3969 /* Lexical conventions [gram.lex] */
3971 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3972 identifier. */
3974 static cp_expr
3975 cp_parser_identifier (cp_parser* parser)
3977 cp_token *token;
3979 /* Look for the identifier. */
3980 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3981 /* Return the value. */
3982 if (token)
3983 return cp_expr (token->u.value, token->location);
3984 else
3985 return error_mark_node;
3988 /* Parse a sequence of adjacent string constants. Returns a
3989 TREE_STRING representing the combined, nul-terminated string
3990 constant. If TRANSLATE is true, translate the string to the
3991 execution character set. If WIDE_OK is true, a wide string is
3992 invalid here.
3994 C++98 [lex.string] says that if a narrow string literal token is
3995 adjacent to a wide string literal token, the behavior is undefined.
3996 However, C99 6.4.5p4 says that this results in a wide string literal.
3997 We follow C99 here, for consistency with the C front end.
3999 This code is largely lifted from lex_string() in c-lex.c.
4001 FUTURE: ObjC++ will need to handle @-strings here. */
4002 static cp_expr
4003 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4004 bool lookup_udlit = true)
4006 tree value;
4007 size_t count;
4008 struct obstack str_ob;
4009 cpp_string str, istr, *strs;
4010 cp_token *tok;
4011 enum cpp_ttype type, curr_type;
4012 int have_suffix_p = 0;
4013 tree string_tree;
4014 tree suffix_id = NULL_TREE;
4015 bool curr_tok_is_userdef_p = false;
4017 tok = cp_lexer_peek_token (parser->lexer);
4018 if (!cp_parser_is_string_literal (tok))
4020 cp_parser_error (parser, "expected string-literal");
4021 return error_mark_node;
4024 location_t loc = tok->location;
4026 if (cpp_userdef_string_p (tok->type))
4028 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4029 curr_type = cpp_userdef_string_remove_type (tok->type);
4030 curr_tok_is_userdef_p = true;
4032 else
4034 string_tree = tok->u.value;
4035 curr_type = tok->type;
4037 type = curr_type;
4039 /* Try to avoid the overhead of creating and destroying an obstack
4040 for the common case of just one string. */
4041 if (!cp_parser_is_string_literal
4042 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4044 cp_lexer_consume_token (parser->lexer);
4046 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4047 str.len = TREE_STRING_LENGTH (string_tree);
4048 count = 1;
4050 if (curr_tok_is_userdef_p)
4052 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4053 have_suffix_p = 1;
4054 curr_type = cpp_userdef_string_remove_type (tok->type);
4056 else
4057 curr_type = tok->type;
4059 strs = &str;
4061 else
4063 location_t last_tok_loc = tok->location;
4064 gcc_obstack_init (&str_ob);
4065 count = 0;
4069 cp_lexer_consume_token (parser->lexer);
4070 count++;
4071 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4072 str.len = TREE_STRING_LENGTH (string_tree);
4074 if (curr_tok_is_userdef_p)
4076 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4077 if (have_suffix_p == 0)
4079 suffix_id = curr_suffix_id;
4080 have_suffix_p = 1;
4082 else if (have_suffix_p == 1
4083 && curr_suffix_id != suffix_id)
4085 error ("inconsistent user-defined literal suffixes"
4086 " %qD and %qD in string literal",
4087 suffix_id, curr_suffix_id);
4088 have_suffix_p = -1;
4090 curr_type = cpp_userdef_string_remove_type (tok->type);
4092 else
4093 curr_type = tok->type;
4095 if (type != curr_type)
4097 if (type == CPP_STRING)
4098 type = curr_type;
4099 else if (curr_type != CPP_STRING)
4101 rich_location rich_loc (line_table, tok->location);
4102 rich_loc.add_range (last_tok_loc, false);
4103 error_at (&rich_loc,
4104 "unsupported non-standard concatenation "
4105 "of string literals");
4109 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4111 last_tok_loc = tok->location;
4113 tok = cp_lexer_peek_token (parser->lexer);
4114 if (cpp_userdef_string_p (tok->type))
4116 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4117 curr_type = cpp_userdef_string_remove_type (tok->type);
4118 curr_tok_is_userdef_p = true;
4120 else
4122 string_tree = tok->u.value;
4123 curr_type = tok->type;
4124 curr_tok_is_userdef_p = false;
4127 while (cp_parser_is_string_literal (tok));
4129 /* A string literal built by concatenation has its caret=start at
4130 the start of the initial string, and its finish at the finish of
4131 the final string literal. */
4132 loc = make_location (loc, loc, get_finish (last_tok_loc));
4134 strs = (cpp_string *) obstack_finish (&str_ob);
4137 if (type != CPP_STRING && !wide_ok)
4139 cp_parser_error (parser, "a wide string is invalid in this context");
4140 type = CPP_STRING;
4143 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4144 (parse_in, strs, count, &istr, type))
4146 value = build_string (istr.len, (const char *)istr.text);
4147 free (CONST_CAST (unsigned char *, istr.text));
4149 switch (type)
4151 default:
4152 case CPP_STRING:
4153 case CPP_UTF8STRING:
4154 TREE_TYPE (value) = char_array_type_node;
4155 break;
4156 case CPP_STRING16:
4157 TREE_TYPE (value) = char16_array_type_node;
4158 break;
4159 case CPP_STRING32:
4160 TREE_TYPE (value) = char32_array_type_node;
4161 break;
4162 case CPP_WSTRING:
4163 TREE_TYPE (value) = wchar_array_type_node;
4164 break;
4167 value = fix_string_type (value);
4169 if (have_suffix_p)
4171 tree literal = build_userdef_literal (suffix_id, value,
4172 OT_NONE, NULL_TREE);
4173 if (lookup_udlit)
4174 value = cp_parser_userdef_string_literal (literal);
4175 else
4176 value = literal;
4179 else
4180 /* cpp_interpret_string has issued an error. */
4181 value = error_mark_node;
4183 if (count > 1)
4184 obstack_free (&str_ob, 0);
4186 return cp_expr (value, loc);
4189 /* Look up a literal operator with the name and the exact arguments. */
4191 static tree
4192 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4194 tree decl;
4195 decl = lookup_name (name);
4196 if (!decl || !is_overloaded_fn (decl))
4197 return error_mark_node;
4199 for (lkp_iterator iter (decl); iter; ++iter)
4201 unsigned int ix;
4202 bool found = true;
4203 tree fn = *iter;
4204 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4205 if (parmtypes != NULL_TREE)
4207 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4208 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4210 tree tparm = TREE_VALUE (parmtypes);
4211 tree targ = TREE_TYPE ((*args)[ix]);
4212 bool ptr = TYPE_PTR_P (tparm);
4213 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4214 if ((ptr || arr || !same_type_p (tparm, targ))
4215 && (!ptr || !arr
4216 || !same_type_p (TREE_TYPE (tparm),
4217 TREE_TYPE (targ))))
4218 found = false;
4220 if (found
4221 && ix == vec_safe_length (args)
4222 /* May be this should be sufficient_parms_p instead,
4223 depending on how exactly should user-defined literals
4224 work in presence of default arguments on the literal
4225 operator parameters. */
4226 && parmtypes == void_list_node)
4227 return decl;
4231 return error_mark_node;
4234 /* Parse a user-defined char constant. Returns a call to a user-defined
4235 literal operator taking the character as an argument. */
4237 static cp_expr
4238 cp_parser_userdef_char_literal (cp_parser *parser)
4240 cp_token *token = cp_lexer_consume_token (parser->lexer);
4241 tree literal = token->u.value;
4242 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4243 tree value = USERDEF_LITERAL_VALUE (literal);
4244 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4245 tree decl, result;
4247 /* Build up a call to the user-defined operator */
4248 /* Lookup the name we got back from the id-expression. */
4249 vec<tree, va_gc> *args = make_tree_vector ();
4250 vec_safe_push (args, value);
4251 decl = lookup_literal_operator (name, args);
4252 if (!decl || decl == error_mark_node)
4254 error ("unable to find character literal operator %qD with %qT argument",
4255 name, TREE_TYPE (value));
4256 release_tree_vector (args);
4257 return error_mark_node;
4259 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4260 release_tree_vector (args);
4261 return result;
4264 /* A subroutine of cp_parser_userdef_numeric_literal to
4265 create a char... template parameter pack from a string node. */
4267 static tree
4268 make_char_string_pack (tree value)
4270 tree charvec;
4271 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4272 const char *str = TREE_STRING_POINTER (value);
4273 int i, len = TREE_STRING_LENGTH (value) - 1;
4274 tree argvec = make_tree_vec (1);
4276 /* Fill in CHARVEC with all of the parameters. */
4277 charvec = make_tree_vec (len);
4278 for (i = 0; i < len; ++i)
4279 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4281 /* Build the argument packs. */
4282 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4284 TREE_VEC_ELT (argvec, 0) = argpack;
4286 return argvec;
4289 /* A subroutine of cp_parser_userdef_numeric_literal to
4290 create a char... template parameter pack from a string node. */
4292 static tree
4293 make_string_pack (tree value)
4295 tree charvec;
4296 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4297 const unsigned char *str
4298 = (const unsigned char *) TREE_STRING_POINTER (value);
4299 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4300 int len = TREE_STRING_LENGTH (value) / sz - 1;
4301 tree argvec = make_tree_vec (2);
4303 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4304 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4306 /* First template parm is character type. */
4307 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4309 /* Fill in CHARVEC with all of the parameters. */
4310 charvec = make_tree_vec (len);
4311 for (int i = 0; i < len; ++i)
4312 TREE_VEC_ELT (charvec, i)
4313 = double_int_to_tree (str_char_type_node,
4314 double_int::from_buffer (str + i * sz, sz));
4316 /* Build the argument packs. */
4317 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4319 TREE_VEC_ELT (argvec, 1) = argpack;
4321 return argvec;
4324 /* Parse a user-defined numeric constant. returns a call to a user-defined
4325 literal operator. */
4327 static cp_expr
4328 cp_parser_userdef_numeric_literal (cp_parser *parser)
4330 cp_token *token = cp_lexer_consume_token (parser->lexer);
4331 tree literal = token->u.value;
4332 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4333 tree value = USERDEF_LITERAL_VALUE (literal);
4334 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4335 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4336 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4337 tree decl, result;
4338 vec<tree, va_gc> *args;
4340 /* Look for a literal operator taking the exact type of numeric argument
4341 as the literal value. */
4342 args = make_tree_vector ();
4343 vec_safe_push (args, value);
4344 decl = lookup_literal_operator (name, args);
4345 if (decl && decl != error_mark_node)
4347 result = finish_call_expr (decl, &args, false, true,
4348 tf_warning_or_error);
4350 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4352 warning_at (token->location, OPT_Woverflow,
4353 "integer literal exceeds range of %qT type",
4354 long_long_unsigned_type_node);
4356 else
4358 if (overflow > 0)
4359 warning_at (token->location, OPT_Woverflow,
4360 "floating literal exceeds range of %qT type",
4361 long_double_type_node);
4362 else if (overflow < 0)
4363 warning_at (token->location, OPT_Woverflow,
4364 "floating literal truncated to zero");
4367 release_tree_vector (args);
4368 return result;
4370 release_tree_vector (args);
4372 /* If the numeric argument didn't work, look for a raw literal
4373 operator taking a const char* argument consisting of the number
4374 in string format. */
4375 args = make_tree_vector ();
4376 vec_safe_push (args, num_string);
4377 decl = lookup_literal_operator (name, args);
4378 if (decl && decl != error_mark_node)
4380 result = finish_call_expr (decl, &args, false, true,
4381 tf_warning_or_error);
4382 release_tree_vector (args);
4383 return result;
4385 release_tree_vector (args);
4387 /* If the raw literal didn't work, look for a non-type template
4388 function with parameter pack char.... Call the function with
4389 template parameter characters representing the number. */
4390 args = make_tree_vector ();
4391 decl = lookup_literal_operator (name, args);
4392 if (decl && decl != error_mark_node)
4394 tree tmpl_args = make_char_string_pack (num_string);
4395 decl = lookup_template_function (decl, tmpl_args);
4396 result = finish_call_expr (decl, &args, false, true,
4397 tf_warning_or_error);
4398 release_tree_vector (args);
4399 return result;
4402 release_tree_vector (args);
4404 /* In C++14 the standard library defines complex number suffixes that
4405 conflict with GNU extensions. Prefer them if <complex> is #included. */
4406 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4407 bool i14 = (cxx_dialect > cxx11
4408 && (id_equal (suffix_id, "i")
4409 || id_equal (suffix_id, "if")
4410 || id_equal (suffix_id, "il")));
4411 diagnostic_t kind = DK_ERROR;
4412 int opt = 0;
4414 if (i14 && ext)
4416 tree cxlit = lookup_qualified_name (std_node,
4417 get_identifier ("complex_literals"),
4418 0, false, false);
4419 if (cxlit == error_mark_node)
4421 /* No <complex>, so pedwarn and use GNU semantics. */
4422 kind = DK_PEDWARN;
4423 opt = OPT_Wpedantic;
4427 bool complained
4428 = emit_diagnostic (kind, input_location, opt,
4429 "unable to find numeric literal operator %qD", name);
4431 if (!complained)
4432 /* Don't inform either. */;
4433 else if (i14)
4435 inform (token->location, "add %<using namespace std::complex_literals%> "
4436 "(from <complex>) to enable the C++14 user-defined literal "
4437 "suffixes");
4438 if (ext)
4439 inform (token->location, "or use %<j%> instead of %<i%> for the "
4440 "GNU built-in suffix");
4442 else if (!ext)
4443 inform (token->location, "use -fext-numeric-literals "
4444 "to enable more built-in suffixes");
4446 if (kind == DK_ERROR)
4447 value = error_mark_node;
4448 else
4450 /* Use the built-in semantics. */
4451 tree type;
4452 if (id_equal (suffix_id, "i"))
4454 if (TREE_CODE (value) == INTEGER_CST)
4455 type = integer_type_node;
4456 else
4457 type = double_type_node;
4459 else if (id_equal (suffix_id, "if"))
4460 type = float_type_node;
4461 else /* if (id_equal (suffix_id, "il")) */
4462 type = long_double_type_node;
4464 value = build_complex (build_complex_type (type),
4465 fold_convert (type, integer_zero_node),
4466 fold_convert (type, value));
4469 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4470 /* Avoid repeated diagnostics. */
4471 token->u.value = value;
4472 return value;
4475 /* Parse a user-defined string constant. Returns a call to a user-defined
4476 literal operator taking a character pointer and the length of the string
4477 as arguments. */
4479 static tree
4480 cp_parser_userdef_string_literal (tree literal)
4482 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4483 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4484 tree value = USERDEF_LITERAL_VALUE (literal);
4485 int len = TREE_STRING_LENGTH (value)
4486 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4487 tree decl, result;
4488 vec<tree, va_gc> *args;
4490 /* Build up a call to the user-defined operator. */
4491 /* Lookup the name we got back from the id-expression. */
4492 args = make_tree_vector ();
4493 vec_safe_push (args, value);
4494 vec_safe_push (args, build_int_cst (size_type_node, len));
4495 decl = lookup_literal_operator (name, args);
4497 if (decl && decl != error_mark_node)
4499 result = finish_call_expr (decl, &args, false, true,
4500 tf_warning_or_error);
4501 release_tree_vector (args);
4502 return result;
4504 release_tree_vector (args);
4506 /* Look for a template function with typename parameter CharT
4507 and parameter pack CharT... Call the function with
4508 template parameter characters representing the string. */
4509 args = make_tree_vector ();
4510 decl = lookup_literal_operator (name, args);
4511 if (decl && decl != error_mark_node)
4513 tree tmpl_args = make_string_pack (value);
4514 decl = lookup_template_function (decl, tmpl_args);
4515 result = finish_call_expr (decl, &args, false, true,
4516 tf_warning_or_error);
4517 release_tree_vector (args);
4518 return result;
4520 release_tree_vector (args);
4522 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4523 name, TREE_TYPE (value), size_type_node);
4524 return error_mark_node;
4528 /* Basic concepts [gram.basic] */
4530 /* Parse a translation-unit.
4532 translation-unit:
4533 declaration-seq [opt]
4535 Returns TRUE if all went well. */
4537 static bool
4538 cp_parser_translation_unit (cp_parser* parser)
4540 /* The address of the first non-permanent object on the declarator
4541 obstack. */
4542 static void *declarator_obstack_base;
4544 bool success;
4546 /* Create the declarator obstack, if necessary. */
4547 if (!cp_error_declarator)
4549 gcc_obstack_init (&declarator_obstack);
4550 /* Create the error declarator. */
4551 cp_error_declarator = make_declarator (cdk_error);
4552 /* Create the empty parameter list. */
4553 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4554 UNKNOWN_LOCATION);
4555 /* Remember where the base of the declarator obstack lies. */
4556 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4559 cp_parser_declaration_seq_opt (parser);
4561 /* If there are no tokens left then all went well. */
4562 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4564 /* Get rid of the token array; we don't need it any more. */
4565 cp_lexer_destroy (parser->lexer);
4566 parser->lexer = NULL;
4568 /* This file might have been a context that's implicitly extern
4569 "C". If so, pop the lang context. (Only relevant for PCH.) */
4570 if (parser->implicit_extern_c)
4572 pop_lang_context ();
4573 parser->implicit_extern_c = false;
4576 /* Finish up. */
4577 finish_translation_unit ();
4579 success = true;
4581 else
4583 cp_parser_error (parser, "expected declaration");
4584 success = false;
4587 /* Make sure the declarator obstack was fully cleaned up. */
4588 gcc_assert (obstack_next_free (&declarator_obstack)
4589 == declarator_obstack_base);
4591 /* All went well. */
4592 return success;
4595 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4596 decltype context. */
4598 static inline tsubst_flags_t
4599 complain_flags (bool decltype_p)
4601 tsubst_flags_t complain = tf_warning_or_error;
4602 if (decltype_p)
4603 complain |= tf_decltype;
4604 return complain;
4607 /* We're about to parse a collection of statements. If we're currently
4608 parsing tentatively, set up a firewall so that any nested
4609 cp_parser_commit_to_tentative_parse won't affect the current context. */
4611 static cp_token_position
4612 cp_parser_start_tentative_firewall (cp_parser *parser)
4614 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4615 return 0;
4617 cp_parser_parse_tentatively (parser);
4618 cp_parser_commit_to_topmost_tentative_parse (parser);
4619 return cp_lexer_token_position (parser->lexer, false);
4622 /* We've finished parsing the collection of statements. Wrap up the
4623 firewall and replace the relevant tokens with the parsed form. */
4625 static void
4626 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4627 tree expr)
4629 if (!start)
4630 return;
4632 /* Finish the firewall level. */
4633 cp_parser_parse_definitely (parser);
4634 /* And remember the result of the parse for when we try again. */
4635 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4636 token->type = CPP_PREPARSED_EXPR;
4637 token->u.value = expr;
4638 token->keyword = RID_MAX;
4639 cp_lexer_purge_tokens_after (parser->lexer, start);
4642 /* Like the above functions, but let the user modify the tokens. Used by
4643 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4644 later parses, so it makes sense to localize the effects of
4645 cp_parser_commit_to_tentative_parse. */
4647 struct tentative_firewall
4649 cp_parser *parser;
4650 bool set;
4652 tentative_firewall (cp_parser *p): parser(p)
4654 /* If we're currently parsing tentatively, start a committed level as a
4655 firewall and then an inner tentative parse. */
4656 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4658 cp_parser_parse_tentatively (parser);
4659 cp_parser_commit_to_topmost_tentative_parse (parser);
4660 cp_parser_parse_tentatively (parser);
4664 ~tentative_firewall()
4666 if (set)
4668 /* Finish the inner tentative parse and the firewall, propagating any
4669 uncommitted error state to the outer tentative parse. */
4670 bool err = cp_parser_error_occurred (parser);
4671 cp_parser_parse_definitely (parser);
4672 cp_parser_parse_definitely (parser);
4673 if (err)
4674 cp_parser_simulate_error (parser);
4679 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4680 This class is for tracking such a matching pair of symbols.
4681 In particular, it tracks the location of the first token,
4682 so that if the second token is missing, we can highlight the
4683 location of the first token when notifying the user about the
4684 problem. */
4686 template <typename traits_t>
4687 class token_pair
4689 public:
4690 /* token_pair's ctor. */
4691 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4693 /* If the next token is the opening symbol for this pair, consume it and
4694 return true.
4695 Otherwise, issue an error and return false.
4696 In either case, record the location of the opening token. */
4698 bool require_open (cp_parser *parser)
4700 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4701 return cp_parser_require (parser, traits_t::open_token_type,
4702 traits_t::required_token_open);
4705 /* Consume the next token from PARSER, recording its location as
4706 that of the opening token within the pair. */
4708 cp_token * consume_open (cp_parser *parser)
4710 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4711 gcc_assert (tok->type == traits_t::open_token_type);
4712 m_open_loc = tok->location;
4713 return tok;
4716 /* If the next token is the closing symbol for this pair, consume it
4717 and return it.
4718 Otherwise, issue an error, highlighting the location of the
4719 corresponding opening token, and return NULL. */
4721 cp_token *require_close (cp_parser *parser) const
4723 return cp_parser_require (parser, traits_t::close_token_type,
4724 traits_t::required_token_close,
4725 m_open_loc);
4728 private:
4729 location_t m_open_loc;
4732 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4734 struct matching_paren_traits
4736 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4737 static const enum required_token required_token_open = RT_OPEN_PAREN;
4738 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4739 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4742 /* "matching_parens" is a token_pair<T> class for tracking matching
4743 pairs of parentheses. */
4745 typedef token_pair<matching_paren_traits> matching_parens;
4747 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4749 struct matching_brace_traits
4751 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4752 static const enum required_token required_token_open = RT_OPEN_BRACE;
4753 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4754 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4757 /* "matching_braces" is a token_pair<T> class for tracking matching
4758 pairs of braces. */
4760 typedef token_pair<matching_brace_traits> matching_braces;
4763 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4764 enclosing parentheses. */
4766 static cp_expr
4767 cp_parser_statement_expr (cp_parser *parser)
4769 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4771 /* Consume the '('. */
4772 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4773 matching_parens parens;
4774 parens.consume_open (parser);
4775 /* Start the statement-expression. */
4776 tree expr = begin_stmt_expr ();
4777 /* Parse the compound-statement. */
4778 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4779 /* Finish up. */
4780 expr = finish_stmt_expr (expr, false);
4781 /* Consume the ')'. */
4782 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4783 if (!parens.require_close (parser))
4784 cp_parser_skip_to_end_of_statement (parser);
4786 cp_parser_end_tentative_firewall (parser, start, expr);
4787 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4788 return cp_expr (expr, combined_loc);
4791 /* Expressions [gram.expr] */
4793 /* Parse a fold-operator.
4795 fold-operator:
4796 - * / % ^ & | = < > << >>
4797 = -= *= /= %= ^= &= |= <<= >>=
4798 == != <= >= && || , .* ->*
4800 This returns the tree code corresponding to the matched operator
4801 as an int. When the current token matches a compound assignment
4802 opertor, the resulting tree code is the negative value of the
4803 non-assignment operator. */
4805 static int
4806 cp_parser_fold_operator (cp_token *token)
4808 switch (token->type)
4810 case CPP_PLUS: return PLUS_EXPR;
4811 case CPP_MINUS: return MINUS_EXPR;
4812 case CPP_MULT: return MULT_EXPR;
4813 case CPP_DIV: return TRUNC_DIV_EXPR;
4814 case CPP_MOD: return TRUNC_MOD_EXPR;
4815 case CPP_XOR: return BIT_XOR_EXPR;
4816 case CPP_AND: return BIT_AND_EXPR;
4817 case CPP_OR: return BIT_IOR_EXPR;
4818 case CPP_LSHIFT: return LSHIFT_EXPR;
4819 case CPP_RSHIFT: return RSHIFT_EXPR;
4821 case CPP_EQ: return -NOP_EXPR;
4822 case CPP_PLUS_EQ: return -PLUS_EXPR;
4823 case CPP_MINUS_EQ: return -MINUS_EXPR;
4824 case CPP_MULT_EQ: return -MULT_EXPR;
4825 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4826 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4827 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4828 case CPP_AND_EQ: return -BIT_AND_EXPR;
4829 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4830 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4831 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4833 case CPP_EQ_EQ: return EQ_EXPR;
4834 case CPP_NOT_EQ: return NE_EXPR;
4835 case CPP_LESS: return LT_EXPR;
4836 case CPP_GREATER: return GT_EXPR;
4837 case CPP_LESS_EQ: return LE_EXPR;
4838 case CPP_GREATER_EQ: return GE_EXPR;
4840 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4841 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4843 case CPP_COMMA: return COMPOUND_EXPR;
4845 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4846 case CPP_DEREF_STAR: return MEMBER_REF;
4848 default: return ERROR_MARK;
4852 /* Returns true if CODE indicates a binary expression, which is not allowed in
4853 the LHS of a fold-expression. More codes will need to be added to use this
4854 function in other contexts. */
4856 static bool
4857 is_binary_op (tree_code code)
4859 switch (code)
4861 case PLUS_EXPR:
4862 case POINTER_PLUS_EXPR:
4863 case MINUS_EXPR:
4864 case MULT_EXPR:
4865 case TRUNC_DIV_EXPR:
4866 case TRUNC_MOD_EXPR:
4867 case BIT_XOR_EXPR:
4868 case BIT_AND_EXPR:
4869 case BIT_IOR_EXPR:
4870 case LSHIFT_EXPR:
4871 case RSHIFT_EXPR:
4873 case MODOP_EXPR:
4875 case EQ_EXPR:
4876 case NE_EXPR:
4877 case LE_EXPR:
4878 case GE_EXPR:
4879 case LT_EXPR:
4880 case GT_EXPR:
4882 case TRUTH_ANDIF_EXPR:
4883 case TRUTH_ORIF_EXPR:
4885 case COMPOUND_EXPR:
4887 case DOTSTAR_EXPR:
4888 case MEMBER_REF:
4889 return true;
4891 default:
4892 return false;
4896 /* If the next token is a suitable fold operator, consume it and return as
4897 the function above. */
4899 static int
4900 cp_parser_fold_operator (cp_parser *parser)
4902 cp_token* token = cp_lexer_peek_token (parser->lexer);
4903 int code = cp_parser_fold_operator (token);
4904 if (code != ERROR_MARK)
4905 cp_lexer_consume_token (parser->lexer);
4906 return code;
4909 /* Parse a fold-expression.
4911 fold-expression:
4912 ( ... folding-operator cast-expression)
4913 ( cast-expression folding-operator ... )
4914 ( cast-expression folding operator ... folding-operator cast-expression)
4916 Note that the '(' and ')' are matched in primary expression. */
4918 static cp_expr
4919 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4921 cp_id_kind pidk;
4923 // Left fold.
4924 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4926 cp_lexer_consume_token (parser->lexer);
4927 int op = cp_parser_fold_operator (parser);
4928 if (op == ERROR_MARK)
4930 cp_parser_error (parser, "expected binary operator");
4931 return error_mark_node;
4934 tree expr = cp_parser_cast_expression (parser, false, false,
4935 false, &pidk);
4936 if (expr == error_mark_node)
4937 return error_mark_node;
4938 return finish_left_unary_fold_expr (expr, op);
4941 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4942 int op = cp_parser_fold_operator (parser);
4943 if (op == ERROR_MARK)
4945 cp_parser_error (parser, "expected binary operator");
4946 return error_mark_node;
4949 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4951 cp_parser_error (parser, "expected ...");
4952 return error_mark_node;
4954 cp_lexer_consume_token (parser->lexer);
4956 /* The operands of a fold-expression are cast-expressions, so binary or
4957 conditional expressions are not allowed. We check this here to avoid
4958 tentative parsing. */
4959 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4960 /* OK, the expression was parenthesized. */;
4961 else if (is_binary_op (TREE_CODE (expr1)))
4962 error_at (location_of (expr1),
4963 "binary expression in operand of fold-expression");
4964 else if (TREE_CODE (expr1) == COND_EXPR)
4965 error_at (location_of (expr1),
4966 "conditional expression in operand of fold-expression");
4968 // Right fold.
4969 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4970 return finish_right_unary_fold_expr (expr1, op);
4972 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4974 cp_parser_error (parser, "mismatched operator in fold-expression");
4975 return error_mark_node;
4977 cp_lexer_consume_token (parser->lexer);
4979 // Binary left or right fold.
4980 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4981 if (expr2 == error_mark_node)
4982 return error_mark_node;
4983 return finish_binary_fold_expr (expr1, expr2, op);
4986 /* Parse a primary-expression.
4988 primary-expression:
4989 literal
4990 this
4991 ( expression )
4992 id-expression
4993 lambda-expression (C++11)
4995 GNU Extensions:
4997 primary-expression:
4998 ( compound-statement )
4999 __builtin_va_arg ( assignment-expression , type-id )
5000 __builtin_offsetof ( type-id , offsetof-expression )
5002 C++ Extensions:
5003 __has_nothrow_assign ( type-id )
5004 __has_nothrow_constructor ( type-id )
5005 __has_nothrow_copy ( type-id )
5006 __has_trivial_assign ( type-id )
5007 __has_trivial_constructor ( type-id )
5008 __has_trivial_copy ( type-id )
5009 __has_trivial_destructor ( type-id )
5010 __has_virtual_destructor ( type-id )
5011 __is_abstract ( type-id )
5012 __is_base_of ( type-id , type-id )
5013 __is_class ( type-id )
5014 __is_empty ( type-id )
5015 __is_enum ( type-id )
5016 __is_final ( type-id )
5017 __is_literal_type ( type-id )
5018 __is_pod ( type-id )
5019 __is_polymorphic ( type-id )
5020 __is_std_layout ( type-id )
5021 __is_trivial ( type-id )
5022 __is_union ( type-id )
5024 Objective-C++ Extension:
5026 primary-expression:
5027 objc-expression
5029 literal:
5030 __null
5032 ADDRESS_P is true iff this expression was immediately preceded by
5033 "&" and therefore might denote a pointer-to-member. CAST_P is true
5034 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5035 true iff this expression is a template argument.
5037 Returns a representation of the expression. Upon return, *IDK
5038 indicates what kind of id-expression (if any) was present. */
5040 static cp_expr
5041 cp_parser_primary_expression (cp_parser *parser,
5042 bool address_p,
5043 bool cast_p,
5044 bool template_arg_p,
5045 bool decltype_p,
5046 cp_id_kind *idk)
5048 cp_token *token = NULL;
5050 /* Assume the primary expression is not an id-expression. */
5051 *idk = CP_ID_KIND_NONE;
5053 /* Peek at the next token. */
5054 token = cp_lexer_peek_token (parser->lexer);
5055 switch ((int) token->type)
5057 /* literal:
5058 integer-literal
5059 character-literal
5060 floating-literal
5061 string-literal
5062 boolean-literal
5063 pointer-literal
5064 user-defined-literal */
5065 case CPP_CHAR:
5066 case CPP_CHAR16:
5067 case CPP_CHAR32:
5068 case CPP_WCHAR:
5069 case CPP_UTF8CHAR:
5070 case CPP_NUMBER:
5071 case CPP_PREPARSED_EXPR:
5072 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5073 return cp_parser_userdef_numeric_literal (parser);
5074 token = cp_lexer_consume_token (parser->lexer);
5075 if (TREE_CODE (token->u.value) == FIXED_CST)
5077 error_at (token->location,
5078 "fixed-point types not supported in C++");
5079 return error_mark_node;
5081 /* Floating-point literals are only allowed in an integral
5082 constant expression if they are cast to an integral or
5083 enumeration type. */
5084 if (TREE_CODE (token->u.value) == REAL_CST
5085 && parser->integral_constant_expression_p
5086 && pedantic)
5088 /* CAST_P will be set even in invalid code like "int(2.7 +
5089 ...)". Therefore, we have to check that the next token
5090 is sure to end the cast. */
5091 if (cast_p)
5093 cp_token *next_token;
5095 next_token = cp_lexer_peek_token (parser->lexer);
5096 if (/* The comma at the end of an
5097 enumerator-definition. */
5098 next_token->type != CPP_COMMA
5099 /* The curly brace at the end of an enum-specifier. */
5100 && next_token->type != CPP_CLOSE_BRACE
5101 /* The end of a statement. */
5102 && next_token->type != CPP_SEMICOLON
5103 /* The end of the cast-expression. */
5104 && next_token->type != CPP_CLOSE_PAREN
5105 /* The end of an array bound. */
5106 && next_token->type != CPP_CLOSE_SQUARE
5107 /* The closing ">" in a template-argument-list. */
5108 && (next_token->type != CPP_GREATER
5109 || parser->greater_than_is_operator_p)
5110 /* C++0x only: A ">>" treated like two ">" tokens,
5111 in a template-argument-list. */
5112 && (next_token->type != CPP_RSHIFT
5113 || (cxx_dialect == cxx98)
5114 || parser->greater_than_is_operator_p))
5115 cast_p = false;
5118 /* If we are within a cast, then the constraint that the
5119 cast is to an integral or enumeration type will be
5120 checked at that point. If we are not within a cast, then
5121 this code is invalid. */
5122 if (!cast_p)
5123 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5125 return cp_expr (token->u.value, token->location);
5127 case CPP_CHAR_USERDEF:
5128 case CPP_CHAR16_USERDEF:
5129 case CPP_CHAR32_USERDEF:
5130 case CPP_WCHAR_USERDEF:
5131 case CPP_UTF8CHAR_USERDEF:
5132 return cp_parser_userdef_char_literal (parser);
5134 case CPP_STRING:
5135 case CPP_STRING16:
5136 case CPP_STRING32:
5137 case CPP_WSTRING:
5138 case CPP_UTF8STRING:
5139 case CPP_STRING_USERDEF:
5140 case CPP_STRING16_USERDEF:
5141 case CPP_STRING32_USERDEF:
5142 case CPP_WSTRING_USERDEF:
5143 case CPP_UTF8STRING_USERDEF:
5144 /* ??? Should wide strings be allowed when parser->translate_strings_p
5145 is false (i.e. in attributes)? If not, we can kill the third
5146 argument to cp_parser_string_literal. */
5147 return cp_parser_string_literal (parser,
5148 parser->translate_strings_p,
5149 true);
5151 case CPP_OPEN_PAREN:
5152 /* If we see `( { ' then we are looking at the beginning of
5153 a GNU statement-expression. */
5154 if (cp_parser_allow_gnu_extensions_p (parser)
5155 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5157 /* Statement-expressions are not allowed by the standard. */
5158 pedwarn (token->location, OPT_Wpedantic,
5159 "ISO C++ forbids braced-groups within expressions");
5161 /* And they're not allowed outside of a function-body; you
5162 cannot, for example, write:
5164 int i = ({ int j = 3; j + 1; });
5166 at class or namespace scope. */
5167 if (!parser->in_function_body
5168 || parser->in_template_argument_list_p)
5170 error_at (token->location,
5171 "statement-expressions are not allowed outside "
5172 "functions nor in template-argument lists");
5173 cp_parser_skip_to_end_of_block_or_statement (parser);
5174 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5175 cp_lexer_consume_token (parser->lexer);
5176 return error_mark_node;
5178 else
5179 return cp_parser_statement_expr (parser);
5181 /* Otherwise it's a normal parenthesized expression. */
5183 cp_expr expr;
5184 bool saved_greater_than_is_operator_p;
5186 location_t open_paren_loc = token->location;
5188 /* Consume the `('. */
5189 matching_parens parens;
5190 parens.consume_open (parser);
5191 /* Within a parenthesized expression, a `>' token is always
5192 the greater-than operator. */
5193 saved_greater_than_is_operator_p
5194 = parser->greater_than_is_operator_p;
5195 parser->greater_than_is_operator_p = true;
5197 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5198 /* Left fold expression. */
5199 expr = NULL_TREE;
5200 else
5201 /* Parse the parenthesized expression. */
5202 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5204 token = cp_lexer_peek_token (parser->lexer);
5205 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5207 expr = cp_parser_fold_expression (parser, expr);
5208 if (expr != error_mark_node
5209 && cxx_dialect < cxx17
5210 && !in_system_header_at (input_location))
5211 pedwarn (input_location, 0, "fold-expressions only available "
5212 "with -std=c++17 or -std=gnu++17");
5214 else
5215 /* Let the front end know that this expression was
5216 enclosed in parentheses. This matters in case, for
5217 example, the expression is of the form `A::B', since
5218 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5219 not. */
5220 expr = finish_parenthesized_expr (expr);
5222 /* DR 705: Wrapping an unqualified name in parentheses
5223 suppresses arg-dependent lookup. We want to pass back
5224 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5225 (c++/37862), but none of the others. */
5226 if (*idk != CP_ID_KIND_QUALIFIED)
5227 *idk = CP_ID_KIND_NONE;
5229 /* The `>' token might be the end of a template-id or
5230 template-parameter-list now. */
5231 parser->greater_than_is_operator_p
5232 = saved_greater_than_is_operator_p;
5234 /* Consume the `)'. */
5235 token = cp_lexer_peek_token (parser->lexer);
5236 location_t close_paren_loc = token->location;
5237 expr.set_range (open_paren_loc, close_paren_loc);
5238 if (!parens.require_close (parser)
5239 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5240 cp_parser_skip_to_end_of_statement (parser);
5242 return expr;
5245 case CPP_OPEN_SQUARE:
5247 if (c_dialect_objc ())
5249 /* We might have an Objective-C++ message. */
5250 cp_parser_parse_tentatively (parser);
5251 tree msg = cp_parser_objc_message_expression (parser);
5252 /* If that works out, we're done ... */
5253 if (cp_parser_parse_definitely (parser))
5254 return msg;
5255 /* ... else, fall though to see if it's a lambda. */
5257 cp_expr lam = cp_parser_lambda_expression (parser);
5258 /* Don't warn about a failed tentative parse. */
5259 if (cp_parser_error_occurred (parser))
5260 return error_mark_node;
5261 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5262 return lam;
5265 case CPP_OBJC_STRING:
5266 if (c_dialect_objc ())
5267 /* We have an Objective-C++ string literal. */
5268 return cp_parser_objc_expression (parser);
5269 cp_parser_error (parser, "expected primary-expression");
5270 return error_mark_node;
5272 case CPP_KEYWORD:
5273 switch (token->keyword)
5275 /* These two are the boolean literals. */
5276 case RID_TRUE:
5277 cp_lexer_consume_token (parser->lexer);
5278 return cp_expr (boolean_true_node, token->location);
5279 case RID_FALSE:
5280 cp_lexer_consume_token (parser->lexer);
5281 return cp_expr (boolean_false_node, token->location);
5283 /* The `__null' literal. */
5284 case RID_NULL:
5285 cp_lexer_consume_token (parser->lexer);
5286 return cp_expr (null_node, token->location);
5288 /* The `nullptr' literal. */
5289 case RID_NULLPTR:
5290 cp_lexer_consume_token (parser->lexer);
5291 return cp_expr (nullptr_node, token->location);
5293 /* Recognize the `this' keyword. */
5294 case RID_THIS:
5295 cp_lexer_consume_token (parser->lexer);
5296 if (parser->local_variables_forbidden_p)
5298 error_at (token->location,
5299 "%<this%> may not be used in this context");
5300 return error_mark_node;
5302 /* Pointers cannot appear in constant-expressions. */
5303 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5304 return error_mark_node;
5305 return cp_expr (finish_this_expr (), token->location);
5307 /* The `operator' keyword can be the beginning of an
5308 id-expression. */
5309 case RID_OPERATOR:
5310 goto id_expression;
5312 case RID_FUNCTION_NAME:
5313 case RID_PRETTY_FUNCTION_NAME:
5314 case RID_C99_FUNCTION_NAME:
5316 non_integral_constant name;
5318 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5319 __func__ are the names of variables -- but they are
5320 treated specially. Therefore, they are handled here,
5321 rather than relying on the generic id-expression logic
5322 below. Grammatically, these names are id-expressions.
5324 Consume the token. */
5325 token = cp_lexer_consume_token (parser->lexer);
5327 switch (token->keyword)
5329 case RID_FUNCTION_NAME:
5330 name = NIC_FUNC_NAME;
5331 break;
5332 case RID_PRETTY_FUNCTION_NAME:
5333 name = NIC_PRETTY_FUNC;
5334 break;
5335 case RID_C99_FUNCTION_NAME:
5336 name = NIC_C99_FUNC;
5337 break;
5338 default:
5339 gcc_unreachable ();
5342 if (cp_parser_non_integral_constant_expression (parser, name))
5343 return error_mark_node;
5345 /* Look up the name. */
5346 return finish_fname (token->u.value);
5349 case RID_VA_ARG:
5351 tree expression;
5352 tree type;
5353 source_location type_location;
5354 location_t start_loc
5355 = cp_lexer_peek_token (parser->lexer)->location;
5356 /* The `__builtin_va_arg' construct is used to handle
5357 `va_arg'. Consume the `__builtin_va_arg' token. */
5358 cp_lexer_consume_token (parser->lexer);
5359 /* Look for the opening `('. */
5360 matching_parens parens;
5361 parens.require_open (parser);
5362 /* Now, parse the assignment-expression. */
5363 expression = cp_parser_assignment_expression (parser);
5364 /* Look for the `,'. */
5365 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5366 type_location = cp_lexer_peek_token (parser->lexer)->location;
5367 /* Parse the type-id. */
5369 type_id_in_expr_sentinel s (parser);
5370 type = cp_parser_type_id (parser);
5372 /* Look for the closing `)'. */
5373 location_t finish_loc
5374 = cp_lexer_peek_token (parser->lexer)->location;
5375 parens.require_close (parser);
5376 /* Using `va_arg' in a constant-expression is not
5377 allowed. */
5378 if (cp_parser_non_integral_constant_expression (parser,
5379 NIC_VA_ARG))
5380 return error_mark_node;
5381 /* Construct a location of the form:
5382 __builtin_va_arg (v, int)
5383 ~~~~~~~~~~~~~~~~~~~~~^~~~
5384 with the caret at the type, ranging from the start of the
5385 "__builtin_va_arg" token to the close paren. */
5386 location_t combined_loc
5387 = make_location (type_location, start_loc, finish_loc);
5388 return build_x_va_arg (combined_loc, expression, type);
5391 case RID_OFFSETOF:
5392 return cp_parser_builtin_offsetof (parser);
5394 case RID_HAS_NOTHROW_ASSIGN:
5395 case RID_HAS_NOTHROW_CONSTRUCTOR:
5396 case RID_HAS_NOTHROW_COPY:
5397 case RID_HAS_TRIVIAL_ASSIGN:
5398 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5399 case RID_HAS_TRIVIAL_COPY:
5400 case RID_HAS_TRIVIAL_DESTRUCTOR:
5401 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5402 case RID_HAS_VIRTUAL_DESTRUCTOR:
5403 case RID_IS_ABSTRACT:
5404 case RID_IS_AGGREGATE:
5405 case RID_IS_BASE_OF:
5406 case RID_IS_CLASS:
5407 case RID_IS_EMPTY:
5408 case RID_IS_ENUM:
5409 case RID_IS_FINAL:
5410 case RID_IS_LITERAL_TYPE:
5411 case RID_IS_POD:
5412 case RID_IS_POLYMORPHIC:
5413 case RID_IS_SAME_AS:
5414 case RID_IS_STD_LAYOUT:
5415 case RID_IS_TRIVIAL:
5416 case RID_IS_TRIVIALLY_ASSIGNABLE:
5417 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5418 case RID_IS_TRIVIALLY_COPYABLE:
5419 case RID_IS_UNION:
5420 case RID_IS_ASSIGNABLE:
5421 case RID_IS_CONSTRUCTIBLE:
5422 return cp_parser_trait_expr (parser, token->keyword);
5424 // C++ concepts
5425 case RID_REQUIRES:
5426 return cp_parser_requires_expression (parser);
5428 /* Objective-C++ expressions. */
5429 case RID_AT_ENCODE:
5430 case RID_AT_PROTOCOL:
5431 case RID_AT_SELECTOR:
5432 return cp_parser_objc_expression (parser);
5434 case RID_TEMPLATE:
5435 if (parser->in_function_body
5436 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5437 == CPP_LESS))
5439 error_at (token->location,
5440 "a template declaration cannot appear at block scope");
5441 cp_parser_skip_to_end_of_block_or_statement (parser);
5442 return error_mark_node;
5444 /* FALLTHRU */
5445 default:
5446 cp_parser_error (parser, "expected primary-expression");
5447 return error_mark_node;
5450 /* An id-expression can start with either an identifier, a
5451 `::' as the beginning of a qualified-id, or the "operator"
5452 keyword. */
5453 case CPP_NAME:
5454 case CPP_SCOPE:
5455 case CPP_TEMPLATE_ID:
5456 case CPP_NESTED_NAME_SPECIFIER:
5458 id_expression:
5459 cp_expr id_expression;
5460 cp_expr decl;
5461 const char *error_msg;
5462 bool template_p;
5463 bool done;
5464 cp_token *id_expr_token;
5466 /* Parse the id-expression. */
5467 id_expression
5468 = cp_parser_id_expression (parser,
5469 /*template_keyword_p=*/false,
5470 /*check_dependency_p=*/true,
5471 &template_p,
5472 /*declarator_p=*/false,
5473 /*optional_p=*/false);
5474 if (id_expression == error_mark_node)
5475 return error_mark_node;
5476 id_expr_token = token;
5477 token = cp_lexer_peek_token (parser->lexer);
5478 done = (token->type != CPP_OPEN_SQUARE
5479 && token->type != CPP_OPEN_PAREN
5480 && token->type != CPP_DOT
5481 && token->type != CPP_DEREF
5482 && token->type != CPP_PLUS_PLUS
5483 && token->type != CPP_MINUS_MINUS);
5484 /* If we have a template-id, then no further lookup is
5485 required. If the template-id was for a template-class, we
5486 will sometimes have a TYPE_DECL at this point. */
5487 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5488 || TREE_CODE (id_expression) == TYPE_DECL)
5489 decl = id_expression;
5490 /* Look up the name. */
5491 else
5493 tree ambiguous_decls;
5495 /* If we already know that this lookup is ambiguous, then
5496 we've already issued an error message; there's no reason
5497 to check again. */
5498 if (id_expr_token->type == CPP_NAME
5499 && id_expr_token->error_reported)
5501 cp_parser_simulate_error (parser);
5502 return error_mark_node;
5505 decl = cp_parser_lookup_name (parser, id_expression,
5506 none_type,
5507 template_p,
5508 /*is_namespace=*/false,
5509 /*check_dependency=*/true,
5510 &ambiguous_decls,
5511 id_expr_token->location);
5512 /* If the lookup was ambiguous, an error will already have
5513 been issued. */
5514 if (ambiguous_decls)
5515 return error_mark_node;
5517 /* In Objective-C++, we may have an Objective-C 2.0
5518 dot-syntax for classes here. */
5519 if (c_dialect_objc ()
5520 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5521 && TREE_CODE (decl) == TYPE_DECL
5522 && objc_is_class_name (decl))
5524 tree component;
5525 cp_lexer_consume_token (parser->lexer);
5526 component = cp_parser_identifier (parser);
5527 if (component == error_mark_node)
5528 return error_mark_node;
5530 tree result = objc_build_class_component_ref (id_expression,
5531 component);
5532 /* Build a location of the form:
5533 expr.component
5534 ~~~~~^~~~~~~~~
5535 with caret at the start of the component name (at
5536 input_location), ranging from the start of the id_expression
5537 to the end of the component name. */
5538 location_t combined_loc
5539 = make_location (input_location, id_expression.get_start (),
5540 get_finish (input_location));
5541 protected_set_expr_location (result, combined_loc);
5542 return result;
5545 /* In Objective-C++, an instance variable (ivar) may be preferred
5546 to whatever cp_parser_lookup_name() found.
5547 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5548 rest of c-family, we have to do a little extra work to preserve
5549 any location information in cp_expr "decl". Given that
5550 objc_lookup_ivar is implemented in "c-family" and "objc", we
5551 have a trip through the pure "tree" type, rather than cp_expr.
5552 Naively copying it back to "decl" would implicitly give the
5553 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5554 store an EXPR_LOCATION. Hence we only update "decl" (and
5555 hence its location_t) if we get back a different tree node. */
5556 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5557 id_expression);
5558 if (decl_tree != decl.get_value ())
5559 decl = cp_expr (decl_tree);
5561 /* If name lookup gives us a SCOPE_REF, then the
5562 qualifying scope was dependent. */
5563 if (TREE_CODE (decl) == SCOPE_REF)
5565 /* At this point, we do not know if DECL is a valid
5566 integral constant expression. We assume that it is
5567 in fact such an expression, so that code like:
5569 template <int N> struct A {
5570 int a[B<N>::i];
5573 is accepted. At template-instantiation time, we
5574 will check that B<N>::i is actually a constant. */
5575 return decl;
5577 /* Check to see if DECL is a local variable in a context
5578 where that is forbidden. */
5579 if (parser->local_variables_forbidden_p
5580 && local_variable_p (decl))
5582 /* It might be that we only found DECL because we are
5583 trying to be generous with pre-ISO scoping rules.
5584 For example, consider:
5586 int i;
5587 void g() {
5588 for (int i = 0; i < 10; ++i) {}
5589 extern void f(int j = i);
5592 Here, name look up will originally find the out
5593 of scope `i'. We need to issue a warning message,
5594 but then use the global `i'. */
5595 decl = check_for_out_of_scope_variable (decl);
5596 if (local_variable_p (decl))
5598 error_at (id_expr_token->location,
5599 "local variable %qD may not appear in this context",
5600 decl.get_value ());
5601 return error_mark_node;
5606 decl = (finish_id_expression
5607 (id_expression, decl, parser->scope,
5608 idk,
5609 parser->integral_constant_expression_p,
5610 parser->allow_non_integral_constant_expression_p,
5611 &parser->non_integral_constant_expression_p,
5612 template_p, done, address_p,
5613 template_arg_p,
5614 &error_msg,
5615 id_expression.get_location ()));
5616 if (error_msg)
5617 cp_parser_error (parser, error_msg);
5618 decl.set_location (id_expr_token->location);
5619 return decl;
5622 /* Anything else is an error. */
5623 default:
5624 cp_parser_error (parser, "expected primary-expression");
5625 return error_mark_node;
5629 static inline cp_expr
5630 cp_parser_primary_expression (cp_parser *parser,
5631 bool address_p,
5632 bool cast_p,
5633 bool template_arg_p,
5634 cp_id_kind *idk)
5636 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5637 /*decltype*/false, idk);
5640 /* Parse an id-expression.
5642 id-expression:
5643 unqualified-id
5644 qualified-id
5646 qualified-id:
5647 :: [opt] nested-name-specifier template [opt] unqualified-id
5648 :: identifier
5649 :: operator-function-id
5650 :: template-id
5652 Return a representation of the unqualified portion of the
5653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5654 a `::' or nested-name-specifier.
5656 Often, if the id-expression was a qualified-id, the caller will
5657 want to make a SCOPE_REF to represent the qualified-id. This
5658 function does not do this in order to avoid wastefully creating
5659 SCOPE_REFs when they are not required.
5661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5662 `template' keyword.
5664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5665 uninstantiated templates.
5667 If *TEMPLATE_P is non-NULL, it is set to true iff the
5668 `template' keyword is used to explicitly indicate that the entity
5669 named is a template.
5671 If DECLARATOR_P is true, the id-expression is appearing as part of
5672 a declarator, rather than as part of an expression. */
5674 static cp_expr
5675 cp_parser_id_expression (cp_parser *parser,
5676 bool template_keyword_p,
5677 bool check_dependency_p,
5678 bool *template_p,
5679 bool declarator_p,
5680 bool optional_p)
5682 bool global_scope_p;
5683 bool nested_name_specifier_p;
5685 /* Assume the `template' keyword was not used. */
5686 if (template_p)
5687 *template_p = template_keyword_p;
5689 /* Look for the optional `::' operator. */
5690 global_scope_p
5691 = (!template_keyword_p
5692 && (cp_parser_global_scope_opt (parser,
5693 /*current_scope_valid_p=*/false)
5694 != NULL_TREE));
5696 /* Look for the optional nested-name-specifier. */
5697 nested_name_specifier_p
5698 = (cp_parser_nested_name_specifier_opt (parser,
5699 /*typename_keyword_p=*/false,
5700 check_dependency_p,
5701 /*type_p=*/false,
5702 declarator_p,
5703 template_keyword_p)
5704 != NULL_TREE);
5706 /* If there is a nested-name-specifier, then we are looking at
5707 the first qualified-id production. */
5708 if (nested_name_specifier_p)
5710 tree saved_scope;
5711 tree saved_object_scope;
5712 tree saved_qualifying_scope;
5713 cp_expr unqualified_id;
5714 bool is_template;
5716 /* See if the next token is the `template' keyword. */
5717 if (!template_p)
5718 template_p = &is_template;
5719 *template_p = cp_parser_optional_template_keyword (parser);
5720 /* Name lookup we do during the processing of the
5721 unqualified-id might obliterate SCOPE. */
5722 saved_scope = parser->scope;
5723 saved_object_scope = parser->object_scope;
5724 saved_qualifying_scope = parser->qualifying_scope;
5725 /* Process the final unqualified-id. */
5726 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5727 check_dependency_p,
5728 declarator_p,
5729 /*optional_p=*/false);
5730 /* Restore the SAVED_SCOPE for our caller. */
5731 parser->scope = saved_scope;
5732 parser->object_scope = saved_object_scope;
5733 parser->qualifying_scope = saved_qualifying_scope;
5735 return unqualified_id;
5737 /* Otherwise, if we are in global scope, then we are looking at one
5738 of the other qualified-id productions. */
5739 else if (global_scope_p)
5741 cp_token *token;
5742 tree id;
5744 /* Peek at the next token. */
5745 token = cp_lexer_peek_token (parser->lexer);
5747 /* If it's an identifier, and the next token is not a "<", then
5748 we can avoid the template-id case. This is an optimization
5749 for this common case. */
5750 if (token->type == CPP_NAME
5751 && !cp_parser_nth_token_starts_template_argument_list_p
5752 (parser, 2))
5753 return cp_parser_identifier (parser);
5755 cp_parser_parse_tentatively (parser);
5756 /* Try a template-id. */
5757 id = cp_parser_template_id (parser,
5758 /*template_keyword_p=*/false,
5759 /*check_dependency_p=*/true,
5760 none_type,
5761 declarator_p);
5762 /* If that worked, we're done. */
5763 if (cp_parser_parse_definitely (parser))
5764 return id;
5766 /* Peek at the next token. (Changes in the token buffer may
5767 have invalidated the pointer obtained above.) */
5768 token = cp_lexer_peek_token (parser->lexer);
5770 switch (token->type)
5772 case CPP_NAME:
5773 return cp_parser_identifier (parser);
5775 case CPP_KEYWORD:
5776 if (token->keyword == RID_OPERATOR)
5777 return cp_parser_operator_function_id (parser);
5778 /* Fall through. */
5780 default:
5781 cp_parser_error (parser, "expected id-expression");
5782 return error_mark_node;
5785 else
5786 return cp_parser_unqualified_id (parser, template_keyword_p,
5787 /*check_dependency_p=*/true,
5788 declarator_p,
5789 optional_p);
5792 /* Parse an unqualified-id.
5794 unqualified-id:
5795 identifier
5796 operator-function-id
5797 conversion-function-id
5798 ~ class-name
5799 template-id
5801 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5802 keyword, in a construct like `A::template ...'.
5804 Returns a representation of unqualified-id. For the `identifier'
5805 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5806 production a BIT_NOT_EXPR is returned; the operand of the
5807 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5808 other productions, see the documentation accompanying the
5809 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5810 names are looked up in uninstantiated templates. If DECLARATOR_P
5811 is true, the unqualified-id is appearing as part of a declarator,
5812 rather than as part of an expression. */
5814 static cp_expr
5815 cp_parser_unqualified_id (cp_parser* parser,
5816 bool template_keyword_p,
5817 bool check_dependency_p,
5818 bool declarator_p,
5819 bool optional_p)
5821 cp_token *token;
5823 /* Peek at the next token. */
5824 token = cp_lexer_peek_token (parser->lexer);
5826 switch ((int) token->type)
5828 case CPP_NAME:
5830 tree id;
5832 /* We don't know yet whether or not this will be a
5833 template-id. */
5834 cp_parser_parse_tentatively (parser);
5835 /* Try a template-id. */
5836 id = cp_parser_template_id (parser, template_keyword_p,
5837 check_dependency_p,
5838 none_type,
5839 declarator_p);
5840 /* If it worked, we're done. */
5841 if (cp_parser_parse_definitely (parser))
5842 return id;
5843 /* Otherwise, it's an ordinary identifier. */
5844 return cp_parser_identifier (parser);
5847 case CPP_TEMPLATE_ID:
5848 return cp_parser_template_id (parser, template_keyword_p,
5849 check_dependency_p,
5850 none_type,
5851 declarator_p);
5853 case CPP_COMPL:
5855 tree type_decl;
5856 tree qualifying_scope;
5857 tree object_scope;
5858 tree scope;
5859 bool done;
5861 /* Consume the `~' token. */
5862 cp_lexer_consume_token (parser->lexer);
5863 /* Parse the class-name. The standard, as written, seems to
5864 say that:
5866 template <typename T> struct S { ~S (); };
5867 template <typename T> S<T>::~S() {}
5869 is invalid, since `~' must be followed by a class-name, but
5870 `S<T>' is dependent, and so not known to be a class.
5871 That's not right; we need to look in uninstantiated
5872 templates. A further complication arises from:
5874 template <typename T> void f(T t) {
5875 t.T::~T();
5878 Here, it is not possible to look up `T' in the scope of `T'
5879 itself. We must look in both the current scope, and the
5880 scope of the containing complete expression.
5882 Yet another issue is:
5884 struct S {
5885 int S;
5886 ~S();
5889 S::~S() {}
5891 The standard does not seem to say that the `S' in `~S'
5892 should refer to the type `S' and not the data member
5893 `S::S'. */
5895 /* DR 244 says that we look up the name after the "~" in the
5896 same scope as we looked up the qualifying name. That idea
5897 isn't fully worked out; it's more complicated than that. */
5898 scope = parser->scope;
5899 object_scope = parser->object_scope;
5900 qualifying_scope = parser->qualifying_scope;
5902 /* Check for invalid scopes. */
5903 if (scope == error_mark_node)
5905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5906 cp_lexer_consume_token (parser->lexer);
5907 return error_mark_node;
5909 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5911 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5912 error_at (token->location,
5913 "scope %qT before %<~%> is not a class-name",
5914 scope);
5915 cp_parser_simulate_error (parser);
5916 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5917 cp_lexer_consume_token (parser->lexer);
5918 return error_mark_node;
5920 gcc_assert (!scope || TYPE_P (scope));
5922 /* If the name is of the form "X::~X" it's OK even if X is a
5923 typedef. */
5924 token = cp_lexer_peek_token (parser->lexer);
5925 if (scope
5926 && token->type == CPP_NAME
5927 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5928 != CPP_LESS)
5929 && (token->u.value == TYPE_IDENTIFIER (scope)
5930 || (CLASS_TYPE_P (scope)
5931 && constructor_name_p (token->u.value, scope))))
5933 cp_lexer_consume_token (parser->lexer);
5934 return build_nt (BIT_NOT_EXPR, scope);
5937 /* ~auto means the destructor of whatever the object is. */
5938 if (cp_parser_is_keyword (token, RID_AUTO))
5940 if (cxx_dialect < cxx14)
5941 pedwarn (input_location, 0,
5942 "%<~auto%> only available with "
5943 "-std=c++14 or -std=gnu++14");
5944 cp_lexer_consume_token (parser->lexer);
5945 return build_nt (BIT_NOT_EXPR, make_auto ());
5948 /* If there was an explicit qualification (S::~T), first look
5949 in the scope given by the qualification (i.e., S).
5951 Note: in the calls to cp_parser_class_name below we pass
5952 typename_type so that lookup finds the injected-class-name
5953 rather than the constructor. */
5954 done = false;
5955 type_decl = NULL_TREE;
5956 if (scope)
5958 cp_parser_parse_tentatively (parser);
5959 type_decl = cp_parser_class_name (parser,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5962 typename_type,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5965 declarator_p);
5966 if (cp_parser_parse_definitely (parser))
5967 done = true;
5969 /* In "N::S::~S", look in "N" as well. */
5970 if (!done && scope && qualifying_scope)
5972 cp_parser_parse_tentatively (parser);
5973 parser->scope = qualifying_scope;
5974 parser->object_scope = NULL_TREE;
5975 parser->qualifying_scope = NULL_TREE;
5976 type_decl
5977 = cp_parser_class_name (parser,
5978 /*typename_keyword_p=*/false,
5979 /*template_keyword_p=*/false,
5980 typename_type,
5981 /*check_dependency=*/false,
5982 /*class_head_p=*/false,
5983 declarator_p);
5984 if (cp_parser_parse_definitely (parser))
5985 done = true;
5987 /* In "p->S::~T", look in the scope given by "*p" as well. */
5988 else if (!done && object_scope)
5990 cp_parser_parse_tentatively (parser);
5991 parser->scope = object_scope;
5992 parser->object_scope = NULL_TREE;
5993 parser->qualifying_scope = NULL_TREE;
5994 type_decl
5995 = cp_parser_class_name (parser,
5996 /*typename_keyword_p=*/false,
5997 /*template_keyword_p=*/false,
5998 typename_type,
5999 /*check_dependency=*/false,
6000 /*class_head_p=*/false,
6001 declarator_p);
6002 if (cp_parser_parse_definitely (parser))
6003 done = true;
6005 /* Look in the surrounding context. */
6006 if (!done)
6008 parser->scope = NULL_TREE;
6009 parser->object_scope = NULL_TREE;
6010 parser->qualifying_scope = NULL_TREE;
6011 if (processing_template_decl)
6012 cp_parser_parse_tentatively (parser);
6013 type_decl
6014 = cp_parser_class_name (parser,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6017 typename_type,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6020 declarator_p);
6021 if (processing_template_decl
6022 && ! cp_parser_parse_definitely (parser))
6024 /* We couldn't find a type with this name. If we're parsing
6025 tentatively, fail and try something else. */
6026 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6028 cp_parser_simulate_error (parser);
6029 return error_mark_node;
6031 /* Otherwise, accept it and check for a match at instantiation
6032 time. */
6033 type_decl = cp_parser_identifier (parser);
6034 if (type_decl != error_mark_node)
6035 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6036 return type_decl;
6039 /* If an error occurred, assume that the name of the
6040 destructor is the same as the name of the qualifying
6041 class. That allows us to keep parsing after running
6042 into ill-formed destructor names. */
6043 if (type_decl == error_mark_node && scope)
6044 return build_nt (BIT_NOT_EXPR, scope);
6045 else if (type_decl == error_mark_node)
6046 return error_mark_node;
6048 /* Check that destructor name and scope match. */
6049 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6051 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6052 error_at (token->location,
6053 "declaration of %<~%T%> as member of %qT",
6054 type_decl, scope);
6055 cp_parser_simulate_error (parser);
6056 return error_mark_node;
6059 /* [class.dtor]
6061 A typedef-name that names a class shall not be used as the
6062 identifier in the declarator for a destructor declaration. */
6063 if (declarator_p
6064 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6065 && !DECL_SELF_REFERENCE_P (type_decl)
6066 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6067 error_at (token->location,
6068 "typedef-name %qD used as destructor declarator",
6069 type_decl);
6071 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6074 case CPP_KEYWORD:
6075 if (token->keyword == RID_OPERATOR)
6077 cp_expr id;
6079 /* This could be a template-id, so we try that first. */
6080 cp_parser_parse_tentatively (parser);
6081 /* Try a template-id. */
6082 id = cp_parser_template_id (parser, template_keyword_p,
6083 /*check_dependency_p=*/true,
6084 none_type,
6085 declarator_p);
6086 /* If that worked, we're done. */
6087 if (cp_parser_parse_definitely (parser))
6088 return id;
6089 /* We still don't know whether we're looking at an
6090 operator-function-id or a conversion-function-id. */
6091 cp_parser_parse_tentatively (parser);
6092 /* Try an operator-function-id. */
6093 id = cp_parser_operator_function_id (parser);
6094 /* If that didn't work, try a conversion-function-id. */
6095 if (!cp_parser_parse_definitely (parser))
6096 id = cp_parser_conversion_function_id (parser);
6097 else if (UDLIT_OPER_P (id))
6099 /* 17.6.3.3.5 */
6100 const char *name = UDLIT_OP_SUFFIX (id);
6101 if (name[0] != '_' && !in_system_header_at (input_location)
6102 && declarator_p)
6103 warning (OPT_Wliteral_suffix,
6104 "literal operator suffixes not preceded by %<_%>"
6105 " are reserved for future standardization");
6108 return id;
6110 /* Fall through. */
6112 default:
6113 if (optional_p)
6114 return NULL_TREE;
6115 cp_parser_error (parser, "expected unqualified-id");
6116 return error_mark_node;
6120 /* Parse an (optional) nested-name-specifier.
6122 nested-name-specifier: [C++98]
6123 class-or-namespace-name :: nested-name-specifier [opt]
6124 class-or-namespace-name :: template nested-name-specifier [opt]
6126 nested-name-specifier: [C++0x]
6127 type-name ::
6128 namespace-name ::
6129 nested-name-specifier identifier ::
6130 nested-name-specifier template [opt] simple-template-id ::
6132 PARSER->SCOPE should be set appropriately before this function is
6133 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6134 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6135 in name lookups.
6137 Sets PARSER->SCOPE to the class (TYPE) or namespace
6138 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6139 it unchanged if there is no nested-name-specifier. Returns the new
6140 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6142 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6143 part of a declaration and/or decl-specifier. */
6145 static tree
6146 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6147 bool typename_keyword_p,
6148 bool check_dependency_p,
6149 bool type_p,
6150 bool is_declaration,
6151 bool template_keyword_p /* = false */)
6153 bool success = false;
6154 cp_token_position start = 0;
6155 cp_token *token;
6157 /* Remember where the nested-name-specifier starts. */
6158 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6160 start = cp_lexer_token_position (parser->lexer, false);
6161 push_deferring_access_checks (dk_deferred);
6164 while (true)
6166 tree new_scope;
6167 tree old_scope;
6168 tree saved_qualifying_scope;
6170 /* Spot cases that cannot be the beginning of a
6171 nested-name-specifier. */
6172 token = cp_lexer_peek_token (parser->lexer);
6174 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6175 the already parsed nested-name-specifier. */
6176 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6178 /* Grab the nested-name-specifier and continue the loop. */
6179 cp_parser_pre_parsed_nested_name_specifier (parser);
6180 /* If we originally encountered this nested-name-specifier
6181 with IS_DECLARATION set to false, we will not have
6182 resolved TYPENAME_TYPEs, so we must do so here. */
6183 if (is_declaration
6184 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6186 new_scope = resolve_typename_type (parser->scope,
6187 /*only_current_p=*/false);
6188 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6189 parser->scope = new_scope;
6191 success = true;
6192 continue;
6195 /* Spot cases that cannot be the beginning of a
6196 nested-name-specifier. On the second and subsequent times
6197 through the loop, we look for the `template' keyword. */
6198 if (success && token->keyword == RID_TEMPLATE)
6200 /* A template-id can start a nested-name-specifier. */
6201 else if (token->type == CPP_TEMPLATE_ID)
6203 /* DR 743: decltype can be used in a nested-name-specifier. */
6204 else if (token_is_decltype (token))
6206 else
6208 /* If the next token is not an identifier, then it is
6209 definitely not a type-name or namespace-name. */
6210 if (token->type != CPP_NAME)
6211 break;
6212 /* If the following token is neither a `<' (to begin a
6213 template-id), nor a `::', then we are not looking at a
6214 nested-name-specifier. */
6215 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6217 if (token->type == CPP_COLON
6218 && parser->colon_corrects_to_scope_p
6219 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6221 gcc_rich_location richloc (token->location);
6222 richloc.add_fixit_replace ("::");
6223 error_at (&richloc,
6224 "found %<:%> in nested-name-specifier, "
6225 "expected %<::%>");
6226 token->type = CPP_SCOPE;
6229 if (token->type != CPP_SCOPE
6230 && !cp_parser_nth_token_starts_template_argument_list_p
6231 (parser, 2))
6232 break;
6235 /* The nested-name-specifier is optional, so we parse
6236 tentatively. */
6237 cp_parser_parse_tentatively (parser);
6239 /* Look for the optional `template' keyword, if this isn't the
6240 first time through the loop. */
6241 if (success)
6242 template_keyword_p = cp_parser_optional_template_keyword (parser);
6244 /* Save the old scope since the name lookup we are about to do
6245 might destroy it. */
6246 old_scope = parser->scope;
6247 saved_qualifying_scope = parser->qualifying_scope;
6248 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6249 look up names in "X<T>::I" in order to determine that "Y" is
6250 a template. So, if we have a typename at this point, we make
6251 an effort to look through it. */
6252 if (is_declaration
6253 && !typename_keyword_p
6254 && parser->scope
6255 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6256 parser->scope = resolve_typename_type (parser->scope,
6257 /*only_current_p=*/false);
6258 /* Parse the qualifying entity. */
6259 new_scope
6260 = cp_parser_qualifying_entity (parser,
6261 typename_keyword_p,
6262 template_keyword_p,
6263 check_dependency_p,
6264 type_p,
6265 is_declaration);
6266 /* Look for the `::' token. */
6267 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6269 /* If we found what we wanted, we keep going; otherwise, we're
6270 done. */
6271 if (!cp_parser_parse_definitely (parser))
6273 bool error_p = false;
6275 /* Restore the OLD_SCOPE since it was valid before the
6276 failed attempt at finding the last
6277 class-or-namespace-name. */
6278 parser->scope = old_scope;
6279 parser->qualifying_scope = saved_qualifying_scope;
6281 /* If the next token is a decltype, and the one after that is a
6282 `::', then the decltype has failed to resolve to a class or
6283 enumeration type. Give this error even when parsing
6284 tentatively since it can't possibly be valid--and we're going
6285 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6286 won't get another chance.*/
6287 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6288 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6289 == CPP_SCOPE))
6291 token = cp_lexer_consume_token (parser->lexer);
6292 error_at (token->location, "decltype evaluates to %qT, "
6293 "which is not a class or enumeration type",
6294 token->u.tree_check_value->value);
6295 parser->scope = error_mark_node;
6296 error_p = true;
6297 /* As below. */
6298 success = true;
6299 cp_lexer_consume_token (parser->lexer);
6302 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6303 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6305 /* If we have a non-type template-id followed by ::, it can't
6306 possibly be valid. */
6307 token = cp_lexer_peek_token (parser->lexer);
6308 tree tid = token->u.tree_check_value->value;
6309 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6310 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6312 tree tmpl = NULL_TREE;
6313 if (is_overloaded_fn (tid))
6315 tree fns = get_fns (tid);
6316 if (OVL_SINGLE_P (fns))
6317 tmpl = OVL_FIRST (fns);
6318 error_at (token->location, "function template-id %qD "
6319 "in nested-name-specifier", tid);
6321 else
6323 /* Variable template. */
6324 tmpl = TREE_OPERAND (tid, 0);
6325 gcc_assert (variable_template_p (tmpl));
6326 error_at (token->location, "variable template-id %qD "
6327 "in nested-name-specifier", tid);
6329 if (tmpl)
6330 inform (DECL_SOURCE_LOCATION (tmpl),
6331 "%qD declared here", tmpl);
6333 parser->scope = error_mark_node;
6334 error_p = true;
6335 /* As below. */
6336 success = true;
6337 cp_lexer_consume_token (parser->lexer);
6338 cp_lexer_consume_token (parser->lexer);
6342 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6343 break;
6344 /* If the next token is an identifier, and the one after
6345 that is a `::', then any valid interpretation would have
6346 found a class-or-namespace-name. */
6347 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6348 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6349 == CPP_SCOPE)
6350 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6351 != CPP_COMPL))
6353 token = cp_lexer_consume_token (parser->lexer);
6354 if (!error_p)
6356 if (!token->error_reported)
6358 tree decl;
6359 tree ambiguous_decls;
6361 decl = cp_parser_lookup_name (parser, token->u.value,
6362 none_type,
6363 /*is_template=*/false,
6364 /*is_namespace=*/false,
6365 /*check_dependency=*/true,
6366 &ambiguous_decls,
6367 token->location);
6368 if (TREE_CODE (decl) == TEMPLATE_DECL)
6369 error_at (token->location,
6370 "%qD used without template parameters",
6371 decl);
6372 else if (ambiguous_decls)
6374 // cp_parser_lookup_name has the same diagnostic,
6375 // thus make sure to emit it at most once.
6376 if (cp_parser_uncommitted_to_tentative_parse_p
6377 (parser))
6379 error_at (token->location,
6380 "reference to %qD is ambiguous",
6381 token->u.value);
6382 print_candidates (ambiguous_decls);
6384 decl = error_mark_node;
6386 else
6388 if (cxx_dialect != cxx98)
6389 cp_parser_name_lookup_error
6390 (parser, token->u.value, decl, NLE_NOT_CXX98,
6391 token->location);
6392 else
6393 cp_parser_name_lookup_error
6394 (parser, token->u.value, decl, NLE_CXX98,
6395 token->location);
6398 parser->scope = error_mark_node;
6399 error_p = true;
6400 /* Treat this as a successful nested-name-specifier
6401 due to:
6403 [basic.lookup.qual]
6405 If the name found is not a class-name (clause
6406 _class_) or namespace-name (_namespace.def_), the
6407 program is ill-formed. */
6408 success = true;
6410 cp_lexer_consume_token (parser->lexer);
6412 break;
6414 /* We've found one valid nested-name-specifier. */
6415 success = true;
6416 /* Name lookup always gives us a DECL. */
6417 if (TREE_CODE (new_scope) == TYPE_DECL)
6418 new_scope = TREE_TYPE (new_scope);
6419 /* Uses of "template" must be followed by actual templates. */
6420 if (template_keyword_p
6421 && !(CLASS_TYPE_P (new_scope)
6422 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6423 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6424 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6425 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6426 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6427 == TEMPLATE_ID_EXPR)))
6428 permerror (input_location, TYPE_P (new_scope)
6429 ? G_("%qT is not a template")
6430 : G_("%qD is not a template"),
6431 new_scope);
6432 /* If it is a class scope, try to complete it; we are about to
6433 be looking up names inside the class. */
6434 if (TYPE_P (new_scope)
6435 /* Since checking types for dependency can be expensive,
6436 avoid doing it if the type is already complete. */
6437 && !COMPLETE_TYPE_P (new_scope)
6438 /* Do not try to complete dependent types. */
6439 && !dependent_type_p (new_scope))
6441 new_scope = complete_type (new_scope);
6442 /* If it is a typedef to current class, use the current
6443 class instead, as the typedef won't have any names inside
6444 it yet. */
6445 if (!COMPLETE_TYPE_P (new_scope)
6446 && currently_open_class (new_scope))
6447 new_scope = TYPE_MAIN_VARIANT (new_scope);
6449 /* Make sure we look in the right scope the next time through
6450 the loop. */
6451 parser->scope = new_scope;
6454 /* If parsing tentatively, replace the sequence of tokens that makes
6455 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6456 token. That way, should we re-parse the token stream, we will
6457 not have to repeat the effort required to do the parse, nor will
6458 we issue duplicate error messages. */
6459 if (success && start)
6461 cp_token *token;
6463 token = cp_lexer_token_at (parser->lexer, start);
6464 /* Reset the contents of the START token. */
6465 token->type = CPP_NESTED_NAME_SPECIFIER;
6466 /* Retrieve any deferred checks. Do not pop this access checks yet
6467 so the memory will not be reclaimed during token replacing below. */
6468 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6469 token->u.tree_check_value->value = parser->scope;
6470 token->u.tree_check_value->checks = get_deferred_access_checks ();
6471 token->u.tree_check_value->qualifying_scope =
6472 parser->qualifying_scope;
6473 token->keyword = RID_MAX;
6475 /* Purge all subsequent tokens. */
6476 cp_lexer_purge_tokens_after (parser->lexer, start);
6479 if (start)
6480 pop_to_parent_deferring_access_checks ();
6482 return success ? parser->scope : NULL_TREE;
6485 /* Parse a nested-name-specifier. See
6486 cp_parser_nested_name_specifier_opt for details. This function
6487 behaves identically, except that it will an issue an error if no
6488 nested-name-specifier is present. */
6490 static tree
6491 cp_parser_nested_name_specifier (cp_parser *parser,
6492 bool typename_keyword_p,
6493 bool check_dependency_p,
6494 bool type_p,
6495 bool is_declaration)
6497 tree scope;
6499 /* Look for the nested-name-specifier. */
6500 scope = cp_parser_nested_name_specifier_opt (parser,
6501 typename_keyword_p,
6502 check_dependency_p,
6503 type_p,
6504 is_declaration);
6505 /* If it was not present, issue an error message. */
6506 if (!scope)
6508 cp_parser_error (parser, "expected nested-name-specifier");
6509 parser->scope = NULL_TREE;
6512 return scope;
6515 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6516 this is either a class-name or a namespace-name (which corresponds
6517 to the class-or-namespace-name production in the grammar). For
6518 C++0x, it can also be a type-name that refers to an enumeration
6519 type or a simple-template-id.
6521 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6522 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6523 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6524 TYPE_P is TRUE iff the next name should be taken as a class-name,
6525 even the same name is declared to be another entity in the same
6526 scope.
6528 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6529 specified by the class-or-namespace-name. If neither is found the
6530 ERROR_MARK_NODE is returned. */
6532 static tree
6533 cp_parser_qualifying_entity (cp_parser *parser,
6534 bool typename_keyword_p,
6535 bool template_keyword_p,
6536 bool check_dependency_p,
6537 bool type_p,
6538 bool is_declaration)
6540 tree saved_scope;
6541 tree saved_qualifying_scope;
6542 tree saved_object_scope;
6543 tree scope;
6544 bool only_class_p;
6545 bool successful_parse_p;
6547 /* DR 743: decltype can appear in a nested-name-specifier. */
6548 if (cp_lexer_next_token_is_decltype (parser->lexer))
6550 scope = cp_parser_decltype (parser);
6551 if (TREE_CODE (scope) != ENUMERAL_TYPE
6552 && !MAYBE_CLASS_TYPE_P (scope))
6554 cp_parser_simulate_error (parser);
6555 return error_mark_node;
6557 if (TYPE_NAME (scope))
6558 scope = TYPE_NAME (scope);
6559 return scope;
6562 /* Before we try to parse the class-name, we must save away the
6563 current PARSER->SCOPE since cp_parser_class_name will destroy
6564 it. */
6565 saved_scope = parser->scope;
6566 saved_qualifying_scope = parser->qualifying_scope;
6567 saved_object_scope = parser->object_scope;
6568 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6569 there is no need to look for a namespace-name. */
6570 only_class_p = template_keyword_p
6571 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6572 if (!only_class_p)
6573 cp_parser_parse_tentatively (parser);
6574 scope = cp_parser_class_name (parser,
6575 typename_keyword_p,
6576 template_keyword_p,
6577 type_p ? class_type : none_type,
6578 check_dependency_p,
6579 /*class_head_p=*/false,
6580 is_declaration,
6581 /*enum_ok=*/cxx_dialect > cxx98);
6582 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6583 /* If that didn't work, try for a namespace-name. */
6584 if (!only_class_p && !successful_parse_p)
6586 /* Restore the saved scope. */
6587 parser->scope = saved_scope;
6588 parser->qualifying_scope = saved_qualifying_scope;
6589 parser->object_scope = saved_object_scope;
6590 /* If we are not looking at an identifier followed by the scope
6591 resolution operator, then this is not part of a
6592 nested-name-specifier. (Note that this function is only used
6593 to parse the components of a nested-name-specifier.) */
6594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6595 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6596 return error_mark_node;
6597 scope = cp_parser_namespace_name (parser);
6600 return scope;
6603 /* Return true if we are looking at a compound-literal, false otherwise. */
6605 static bool
6606 cp_parser_compound_literal_p (cp_parser *parser)
6608 cp_lexer_save_tokens (parser->lexer);
6610 /* Skip tokens until the next token is a closing parenthesis.
6611 If we find the closing `)', and the next token is a `{', then
6612 we are looking at a compound-literal. */
6613 bool compound_literal_p
6614 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6615 /*consume_paren=*/true)
6616 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6618 /* Roll back the tokens we skipped. */
6619 cp_lexer_rollback_tokens (parser->lexer);
6621 return compound_literal_p;
6624 /* Return true if EXPR is the integer constant zero or a complex constant
6625 of zero, without any folding, but ignoring location wrappers. */
6627 static bool
6628 literal_integer_zerop (const_tree expr)
6630 STRIP_ANY_LOCATION_WRAPPER (expr);
6631 return integer_zerop (expr);
6634 /* Parse a postfix-expression.
6636 postfix-expression:
6637 primary-expression
6638 postfix-expression [ expression ]
6639 postfix-expression ( expression-list [opt] )
6640 simple-type-specifier ( expression-list [opt] )
6641 typename :: [opt] nested-name-specifier identifier
6642 ( expression-list [opt] )
6643 typename :: [opt] nested-name-specifier template [opt] template-id
6644 ( expression-list [opt] )
6645 postfix-expression . template [opt] id-expression
6646 postfix-expression -> template [opt] id-expression
6647 postfix-expression . pseudo-destructor-name
6648 postfix-expression -> pseudo-destructor-name
6649 postfix-expression ++
6650 postfix-expression --
6651 dynamic_cast < type-id > ( expression )
6652 static_cast < type-id > ( expression )
6653 reinterpret_cast < type-id > ( expression )
6654 const_cast < type-id > ( expression )
6655 typeid ( expression )
6656 typeid ( type-id )
6658 GNU Extension:
6660 postfix-expression:
6661 ( type-id ) { initializer-list , [opt] }
6663 This extension is a GNU version of the C99 compound-literal
6664 construct. (The C99 grammar uses `type-name' instead of `type-id',
6665 but they are essentially the same concept.)
6667 If ADDRESS_P is true, the postfix expression is the operand of the
6668 `&' operator. CAST_P is true if this expression is the target of a
6669 cast.
6671 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6672 class member access expressions [expr.ref].
6674 Returns a representation of the expression. */
6676 static cp_expr
6677 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6678 bool member_access_only_p, bool decltype_p,
6679 cp_id_kind * pidk_return)
6681 cp_token *token;
6682 location_t loc;
6683 enum rid keyword;
6684 cp_id_kind idk = CP_ID_KIND_NONE;
6685 cp_expr postfix_expression = NULL_TREE;
6686 bool is_member_access = false;
6688 /* Peek at the next token. */
6689 token = cp_lexer_peek_token (parser->lexer);
6690 loc = token->location;
6691 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6693 /* Some of the productions are determined by keywords. */
6694 keyword = token->keyword;
6695 switch (keyword)
6697 case RID_DYNCAST:
6698 case RID_STATCAST:
6699 case RID_REINTCAST:
6700 case RID_CONSTCAST:
6702 tree type;
6703 cp_expr expression;
6704 const char *saved_message;
6705 bool saved_in_type_id_in_expr_p;
6707 /* All of these can be handled in the same way from the point
6708 of view of parsing. Begin by consuming the token
6709 identifying the cast. */
6710 cp_lexer_consume_token (parser->lexer);
6712 /* New types cannot be defined in the cast. */
6713 saved_message = parser->type_definition_forbidden_message;
6714 parser->type_definition_forbidden_message
6715 = G_("types may not be defined in casts");
6717 /* Look for the opening `<'. */
6718 cp_parser_require (parser, CPP_LESS, RT_LESS);
6719 /* Parse the type to which we are casting. */
6720 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6721 parser->in_type_id_in_expr_p = true;
6722 type = cp_parser_type_id (parser);
6723 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6724 /* Look for the closing `>'. */
6725 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6726 /* Restore the old message. */
6727 parser->type_definition_forbidden_message = saved_message;
6729 bool saved_greater_than_is_operator_p
6730 = parser->greater_than_is_operator_p;
6731 parser->greater_than_is_operator_p = true;
6733 /* And the expression which is being cast. */
6734 matching_parens parens;
6735 parens.require_open (parser);
6736 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6737 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6738 RT_CLOSE_PAREN);
6739 location_t end_loc = close_paren ?
6740 close_paren->location : UNKNOWN_LOCATION;
6742 parser->greater_than_is_operator_p
6743 = saved_greater_than_is_operator_p;
6745 /* Only type conversions to integral or enumeration types
6746 can be used in constant-expressions. */
6747 if (!cast_valid_in_integral_constant_expression_p (type)
6748 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6750 postfix_expression = error_mark_node;
6751 break;
6754 switch (keyword)
6756 case RID_DYNCAST:
6757 postfix_expression
6758 = build_dynamic_cast (type, expression, tf_warning_or_error);
6759 break;
6760 case RID_STATCAST:
6761 postfix_expression
6762 = build_static_cast (type, expression, tf_warning_or_error);
6763 break;
6764 case RID_REINTCAST:
6765 postfix_expression
6766 = build_reinterpret_cast (type, expression,
6767 tf_warning_or_error);
6768 break;
6769 case RID_CONSTCAST:
6770 postfix_expression
6771 = build_const_cast (type, expression, tf_warning_or_error);
6772 break;
6773 default:
6774 gcc_unreachable ();
6777 /* Construct a location e.g. :
6778 reinterpret_cast <int *> (expr)
6779 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6780 ranging from the start of the "*_cast" token to the final closing
6781 paren, with the caret at the start. */
6782 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6783 postfix_expression.set_location (cp_cast_loc);
6785 break;
6787 case RID_TYPEID:
6789 tree type;
6790 const char *saved_message;
6791 bool saved_in_type_id_in_expr_p;
6793 /* Consume the `typeid' token. */
6794 cp_lexer_consume_token (parser->lexer);
6795 /* Look for the `(' token. */
6796 matching_parens parens;
6797 parens.require_open (parser);
6798 /* Types cannot be defined in a `typeid' expression. */
6799 saved_message = parser->type_definition_forbidden_message;
6800 parser->type_definition_forbidden_message
6801 = G_("types may not be defined in a %<typeid%> expression");
6802 /* We can't be sure yet whether we're looking at a type-id or an
6803 expression. */
6804 cp_parser_parse_tentatively (parser);
6805 /* Try a type-id first. */
6806 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6807 parser->in_type_id_in_expr_p = true;
6808 type = cp_parser_type_id (parser);
6809 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6810 /* Look for the `)' token. Otherwise, we can't be sure that
6811 we're not looking at an expression: consider `typeid (int
6812 (3))', for example. */
6813 cp_token *close_paren = parens.require_close (parser);
6814 /* If all went well, simply lookup the type-id. */
6815 if (cp_parser_parse_definitely (parser))
6816 postfix_expression = get_typeid (type, tf_warning_or_error);
6817 /* Otherwise, fall back to the expression variant. */
6818 else
6820 tree expression;
6822 /* Look for an expression. */
6823 expression = cp_parser_expression (parser, & idk);
6824 /* Compute its typeid. */
6825 postfix_expression = build_typeid (expression, tf_warning_or_error);
6826 /* Look for the `)' token. */
6827 close_paren = parens.require_close (parser);
6829 /* Restore the saved message. */
6830 parser->type_definition_forbidden_message = saved_message;
6831 /* `typeid' may not appear in an integral constant expression. */
6832 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6833 postfix_expression = error_mark_node;
6835 /* Construct a location e.g. :
6836 typeid (expr)
6837 ^~~~~~~~~~~~~
6838 ranging from the start of the "typeid" token to the final closing
6839 paren, with the caret at the start. */
6840 if (close_paren)
6842 location_t typeid_loc
6843 = make_location (start_loc, start_loc, close_paren->location);
6844 postfix_expression.set_location (typeid_loc);
6845 postfix_expression.maybe_add_location_wrapper ();
6848 break;
6850 case RID_TYPENAME:
6852 tree type;
6853 /* The syntax permitted here is the same permitted for an
6854 elaborated-type-specifier. */
6855 ++parser->prevent_constrained_type_specifiers;
6856 type = cp_parser_elaborated_type_specifier (parser,
6857 /*is_friend=*/false,
6858 /*is_declaration=*/false);
6859 --parser->prevent_constrained_type_specifiers;
6860 postfix_expression = cp_parser_functional_cast (parser, type);
6862 break;
6864 case RID_ADDRESSOF:
6865 case RID_BUILTIN_SHUFFLE:
6866 case RID_BUILTIN_LAUNDER:
6868 vec<tree, va_gc> *vec;
6869 unsigned int i;
6870 tree p;
6872 cp_lexer_consume_token (parser->lexer);
6873 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6874 /*cast_p=*/false, /*allow_expansion_p=*/true,
6875 /*non_constant_p=*/NULL);
6876 if (vec == NULL)
6878 postfix_expression = error_mark_node;
6879 break;
6882 FOR_EACH_VEC_ELT (*vec, i, p)
6883 mark_exp_read (p);
6885 switch (keyword)
6887 case RID_ADDRESSOF:
6888 if (vec->length () == 1)
6889 postfix_expression
6890 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6891 else
6893 error_at (loc, "wrong number of arguments to "
6894 "%<__builtin_addressof%>");
6895 postfix_expression = error_mark_node;
6897 break;
6899 case RID_BUILTIN_LAUNDER:
6900 if (vec->length () == 1)
6901 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6902 tf_warning_or_error);
6903 else
6905 error_at (loc, "wrong number of arguments to "
6906 "%<__builtin_launder%>");
6907 postfix_expression = error_mark_node;
6909 break;
6911 case RID_BUILTIN_SHUFFLE:
6912 if (vec->length () == 2)
6913 postfix_expression
6914 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6915 (*vec)[1], tf_warning_or_error);
6916 else if (vec->length () == 3)
6917 postfix_expression
6918 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6919 (*vec)[2], tf_warning_or_error);
6920 else
6922 error_at (loc, "wrong number of arguments to "
6923 "%<__builtin_shuffle%>");
6924 postfix_expression = error_mark_node;
6926 break;
6928 default:
6929 gcc_unreachable ();
6931 break;
6934 default:
6936 tree type;
6938 /* If the next thing is a simple-type-specifier, we may be
6939 looking at a functional cast. We could also be looking at
6940 an id-expression. So, we try the functional cast, and if
6941 that doesn't work we fall back to the primary-expression. */
6942 cp_parser_parse_tentatively (parser);
6943 /* Look for the simple-type-specifier. */
6944 ++parser->prevent_constrained_type_specifiers;
6945 type = cp_parser_simple_type_specifier (parser,
6946 /*decl_specs=*/NULL,
6947 CP_PARSER_FLAGS_NONE);
6948 --parser->prevent_constrained_type_specifiers;
6949 /* Parse the cast itself. */
6950 if (!cp_parser_error_occurred (parser))
6951 postfix_expression
6952 = cp_parser_functional_cast (parser, type);
6953 /* If that worked, we're done. */
6954 if (cp_parser_parse_definitely (parser))
6955 break;
6957 /* If the functional-cast didn't work out, try a
6958 compound-literal. */
6959 if (cp_parser_allow_gnu_extensions_p (parser)
6960 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6962 cp_expr initializer = NULL_TREE;
6964 cp_parser_parse_tentatively (parser);
6966 matching_parens parens;
6967 parens.consume_open (parser);
6969 /* Avoid calling cp_parser_type_id pointlessly, see comment
6970 in cp_parser_cast_expression about c++/29234. */
6971 if (!cp_parser_compound_literal_p (parser))
6972 cp_parser_simulate_error (parser);
6973 else
6975 /* Parse the type. */
6976 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6977 parser->in_type_id_in_expr_p = true;
6978 type = cp_parser_type_id (parser);
6979 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6980 parens.require_close (parser);
6983 /* If things aren't going well, there's no need to
6984 keep going. */
6985 if (!cp_parser_error_occurred (parser))
6987 bool non_constant_p;
6988 /* Parse the brace-enclosed initializer list. */
6989 initializer = cp_parser_braced_list (parser,
6990 &non_constant_p);
6992 /* If that worked, we're definitely looking at a
6993 compound-literal expression. */
6994 if (cp_parser_parse_definitely (parser))
6996 /* Warn the user that a compound literal is not
6997 allowed in standard C++. */
6998 pedwarn (input_location, OPT_Wpedantic,
6999 "ISO C++ forbids compound-literals");
7000 /* For simplicity, we disallow compound literals in
7001 constant-expressions. We could
7002 allow compound literals of integer type, whose
7003 initializer was a constant, in constant
7004 expressions. Permitting that usage, as a further
7005 extension, would not change the meaning of any
7006 currently accepted programs. (Of course, as
7007 compound literals are not part of ISO C++, the
7008 standard has nothing to say.) */
7009 if (cp_parser_non_integral_constant_expression (parser,
7010 NIC_NCC))
7012 postfix_expression = error_mark_node;
7013 break;
7015 /* Form the representation of the compound-literal. */
7016 postfix_expression
7017 = finish_compound_literal (type, initializer,
7018 tf_warning_or_error, fcl_c99);
7019 postfix_expression.set_location (initializer.get_location ());
7020 break;
7024 /* It must be a primary-expression. */
7025 postfix_expression
7026 = cp_parser_primary_expression (parser, address_p, cast_p,
7027 /*template_arg_p=*/false,
7028 decltype_p,
7029 &idk);
7031 break;
7034 /* Note that we don't need to worry about calling build_cplus_new on a
7035 class-valued CALL_EXPR in decltype when it isn't the end of the
7036 postfix-expression; unary_complex_lvalue will take care of that for
7037 all these cases. */
7039 /* Keep looping until the postfix-expression is complete. */
7040 while (true)
7042 if (idk == CP_ID_KIND_UNQUALIFIED
7043 && identifier_p (postfix_expression)
7044 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7045 /* It is not a Koenig lookup function call. */
7046 postfix_expression
7047 = unqualified_name_lookup_error (postfix_expression);
7049 /* Peek at the next token. */
7050 token = cp_lexer_peek_token (parser->lexer);
7052 switch (token->type)
7054 case CPP_OPEN_SQUARE:
7055 if (cp_next_tokens_can_be_std_attribute_p (parser))
7057 cp_parser_error (parser,
7058 "two consecutive %<[%> shall "
7059 "only introduce an attribute");
7060 return error_mark_node;
7062 postfix_expression
7063 = cp_parser_postfix_open_square_expression (parser,
7064 postfix_expression,
7065 false,
7066 decltype_p);
7067 postfix_expression.set_range (start_loc,
7068 postfix_expression.get_location ());
7070 idk = CP_ID_KIND_NONE;
7071 is_member_access = false;
7072 break;
7074 case CPP_OPEN_PAREN:
7075 /* postfix-expression ( expression-list [opt] ) */
7077 bool koenig_p;
7078 bool is_builtin_constant_p;
7079 bool saved_integral_constant_expression_p = false;
7080 bool saved_non_integral_constant_expression_p = false;
7081 tsubst_flags_t complain = complain_flags (decltype_p);
7082 vec<tree, va_gc> *args;
7083 location_t close_paren_loc = UNKNOWN_LOCATION;
7085 is_member_access = false;
7087 is_builtin_constant_p
7088 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7089 if (is_builtin_constant_p)
7091 /* The whole point of __builtin_constant_p is to allow
7092 non-constant expressions to appear as arguments. */
7093 saved_integral_constant_expression_p
7094 = parser->integral_constant_expression_p;
7095 saved_non_integral_constant_expression_p
7096 = parser->non_integral_constant_expression_p;
7097 parser->integral_constant_expression_p = false;
7099 args = (cp_parser_parenthesized_expression_list
7100 (parser, non_attr,
7101 /*cast_p=*/false, /*allow_expansion_p=*/true,
7102 /*non_constant_p=*/NULL,
7103 /*close_paren_loc=*/&close_paren_loc,
7104 /*wrap_locations_p=*/true));
7105 if (is_builtin_constant_p)
7107 parser->integral_constant_expression_p
7108 = saved_integral_constant_expression_p;
7109 parser->non_integral_constant_expression_p
7110 = saved_non_integral_constant_expression_p;
7113 if (args == NULL)
7115 postfix_expression = error_mark_node;
7116 break;
7119 /* Function calls are not permitted in
7120 constant-expressions. */
7121 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7122 && cp_parser_non_integral_constant_expression (parser,
7123 NIC_FUNC_CALL))
7125 postfix_expression = error_mark_node;
7126 release_tree_vector (args);
7127 break;
7130 koenig_p = false;
7131 if (idk == CP_ID_KIND_UNQUALIFIED
7132 || idk == CP_ID_KIND_TEMPLATE_ID)
7134 if (identifier_p (postfix_expression))
7136 if (!args->is_empty ())
7138 koenig_p = true;
7139 if (!any_type_dependent_arguments_p (args))
7140 postfix_expression
7141 = perform_koenig_lookup (postfix_expression, args,
7142 complain);
7144 else
7145 postfix_expression
7146 = unqualified_fn_lookup_error (postfix_expression);
7148 /* We do not perform argument-dependent lookup if
7149 normal lookup finds a non-function, in accordance
7150 with the expected resolution of DR 218. */
7151 else if (!args->is_empty ()
7152 && is_overloaded_fn (postfix_expression))
7154 tree fn = get_first_fn (postfix_expression);
7155 fn = STRIP_TEMPLATE (fn);
7157 /* Do not do argument dependent lookup if regular
7158 lookup finds a member function or a block-scope
7159 function declaration. [basic.lookup.argdep]/3 */
7160 if (!DECL_FUNCTION_MEMBER_P (fn)
7161 && !DECL_LOCAL_FUNCTION_P (fn))
7163 koenig_p = true;
7164 if (!any_type_dependent_arguments_p (args))
7165 postfix_expression
7166 = perform_koenig_lookup (postfix_expression, args,
7167 complain);
7172 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7173 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7174 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7175 && vec_safe_length (args) == 3)
7177 tree arg0 = (*args)[0];
7178 tree arg1 = (*args)[1];
7179 tree arg2 = (*args)[2];
7180 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7181 | (literal_integer_zerop (arg2) << 2));
7182 warn_for_memset (input_location, arg0, arg2, literal_mask);
7185 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7187 tree instance = TREE_OPERAND (postfix_expression, 0);
7188 tree fn = TREE_OPERAND (postfix_expression, 1);
7190 if (processing_template_decl
7191 && (type_dependent_object_expression_p (instance)
7192 || (!BASELINK_P (fn)
7193 && TREE_CODE (fn) != FIELD_DECL)
7194 || type_dependent_expression_p (fn)
7195 || any_type_dependent_arguments_p (args)))
7197 maybe_generic_this_capture (instance, fn);
7198 postfix_expression
7199 = build_min_nt_call_vec (postfix_expression, args);
7200 release_tree_vector (args);
7201 break;
7204 if (BASELINK_P (fn))
7206 postfix_expression
7207 = (build_new_method_call
7208 (instance, fn, &args, NULL_TREE,
7209 (idk == CP_ID_KIND_QUALIFIED
7210 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7211 : LOOKUP_NORMAL),
7212 /*fn_p=*/NULL,
7213 complain));
7215 else
7216 postfix_expression
7217 = finish_call_expr (postfix_expression, &args,
7218 /*disallow_virtual=*/false,
7219 /*koenig_p=*/false,
7220 complain);
7222 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7223 || TREE_CODE (postfix_expression) == MEMBER_REF
7224 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7225 postfix_expression = (build_offset_ref_call_from_tree
7226 (postfix_expression, &args,
7227 complain));
7228 else if (idk == CP_ID_KIND_QUALIFIED)
7229 /* A call to a static class member, or a namespace-scope
7230 function. */
7231 postfix_expression
7232 = finish_call_expr (postfix_expression, &args,
7233 /*disallow_virtual=*/true,
7234 koenig_p,
7235 complain);
7236 else
7237 /* All other function calls. */
7238 postfix_expression
7239 = finish_call_expr (postfix_expression, &args,
7240 /*disallow_virtual=*/false,
7241 koenig_p,
7242 complain);
7244 if (close_paren_loc != UNKNOWN_LOCATION)
7246 location_t combined_loc = make_location (token->location,
7247 start_loc,
7248 close_paren_loc);
7249 postfix_expression.set_location (combined_loc);
7252 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7253 idk = CP_ID_KIND_NONE;
7255 release_tree_vector (args);
7257 break;
7259 case CPP_DOT:
7260 case CPP_DEREF:
7261 /* postfix-expression . template [opt] id-expression
7262 postfix-expression . pseudo-destructor-name
7263 postfix-expression -> template [opt] id-expression
7264 postfix-expression -> pseudo-destructor-name */
7266 /* Consume the `.' or `->' operator. */
7267 cp_lexer_consume_token (parser->lexer);
7269 postfix_expression
7270 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7271 postfix_expression,
7272 false, &idk, loc);
7274 is_member_access = true;
7275 break;
7277 case CPP_PLUS_PLUS:
7278 /* postfix-expression ++ */
7279 /* Consume the `++' token. */
7280 cp_lexer_consume_token (parser->lexer);
7281 /* Generate a representation for the complete expression. */
7282 postfix_expression
7283 = finish_increment_expr (postfix_expression,
7284 POSTINCREMENT_EXPR);
7285 /* Increments may not appear in constant-expressions. */
7286 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7287 postfix_expression = error_mark_node;
7288 idk = CP_ID_KIND_NONE;
7289 is_member_access = false;
7290 break;
7292 case CPP_MINUS_MINUS:
7293 /* postfix-expression -- */
7294 /* Consume the `--' token. */
7295 cp_lexer_consume_token (parser->lexer);
7296 /* Generate a representation for the complete expression. */
7297 postfix_expression
7298 = finish_increment_expr (postfix_expression,
7299 POSTDECREMENT_EXPR);
7300 /* Decrements may not appear in constant-expressions. */
7301 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7302 postfix_expression = error_mark_node;
7303 idk = CP_ID_KIND_NONE;
7304 is_member_access = false;
7305 break;
7307 default:
7308 if (pidk_return != NULL)
7309 * pidk_return = idk;
7310 if (member_access_only_p)
7311 return is_member_access
7312 ? postfix_expression
7313 : cp_expr (error_mark_node);
7314 else
7315 return postfix_expression;
7319 /* We should never get here. */
7320 gcc_unreachable ();
7321 return error_mark_node;
7324 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7325 by cp_parser_builtin_offsetof. We're looking for
7327 postfix-expression [ expression ]
7328 postfix-expression [ braced-init-list ] (C++11)
7330 FOR_OFFSETOF is set if we're being called in that context, which
7331 changes how we deal with integer constant expressions. */
7333 static tree
7334 cp_parser_postfix_open_square_expression (cp_parser *parser,
7335 tree postfix_expression,
7336 bool for_offsetof,
7337 bool decltype_p)
7339 tree index = NULL_TREE;
7340 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7341 bool saved_greater_than_is_operator_p;
7343 /* Consume the `[' token. */
7344 cp_lexer_consume_token (parser->lexer);
7346 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7347 parser->greater_than_is_operator_p = true;
7349 /* Parse the index expression. */
7350 /* ??? For offsetof, there is a question of what to allow here. If
7351 offsetof is not being used in an integral constant expression context,
7352 then we *could* get the right answer by computing the value at runtime.
7353 If we are in an integral constant expression context, then we might
7354 could accept any constant expression; hard to say without analysis.
7355 Rather than open the barn door too wide right away, allow only integer
7356 constant expressions here. */
7357 if (for_offsetof)
7358 index = cp_parser_constant_expression (parser);
7359 else
7361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7363 bool expr_nonconst_p;
7364 cp_lexer_set_source_position (parser->lexer);
7365 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7366 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7368 else
7369 index = cp_parser_expression (parser);
7372 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7374 /* Look for the closing `]'. */
7375 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7377 /* Build the ARRAY_REF. */
7378 postfix_expression = grok_array_decl (loc, postfix_expression,
7379 index, decltype_p);
7381 /* When not doing offsetof, array references are not permitted in
7382 constant-expressions. */
7383 if (!for_offsetof
7384 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7385 postfix_expression = error_mark_node;
7387 return postfix_expression;
7390 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7391 by cp_parser_builtin_offsetof. We're looking for
7393 postfix-expression . template [opt] id-expression
7394 postfix-expression . pseudo-destructor-name
7395 postfix-expression -> template [opt] id-expression
7396 postfix-expression -> pseudo-destructor-name
7398 FOR_OFFSETOF is set if we're being called in that context. That sorta
7399 limits what of the above we'll actually accept, but nevermind.
7400 TOKEN_TYPE is the "." or "->" token, which will already have been
7401 removed from the stream. */
7403 static tree
7404 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7405 enum cpp_ttype token_type,
7406 cp_expr postfix_expression,
7407 bool for_offsetof, cp_id_kind *idk,
7408 location_t location)
7410 tree name;
7411 bool dependent_p;
7412 bool pseudo_destructor_p;
7413 tree scope = NULL_TREE;
7414 location_t start_loc = postfix_expression.get_start ();
7416 /* If this is a `->' operator, dereference the pointer. */
7417 if (token_type == CPP_DEREF)
7418 postfix_expression = build_x_arrow (location, postfix_expression,
7419 tf_warning_or_error);
7420 /* Check to see whether or not the expression is type-dependent and
7421 not the current instantiation. */
7422 dependent_p = type_dependent_object_expression_p (postfix_expression);
7423 /* The identifier following the `->' or `.' is not qualified. */
7424 parser->scope = NULL_TREE;
7425 parser->qualifying_scope = NULL_TREE;
7426 parser->object_scope = NULL_TREE;
7427 *idk = CP_ID_KIND_NONE;
7429 /* Enter the scope corresponding to the type of the object
7430 given by the POSTFIX_EXPRESSION. */
7431 if (!dependent_p)
7433 scope = TREE_TYPE (postfix_expression);
7434 /* According to the standard, no expression should ever have
7435 reference type. Unfortunately, we do not currently match
7436 the standard in this respect in that our internal representation
7437 of an expression may have reference type even when the standard
7438 says it does not. Therefore, we have to manually obtain the
7439 underlying type here. */
7440 scope = non_reference (scope);
7441 /* The type of the POSTFIX_EXPRESSION must be complete. */
7442 /* Unlike the object expression in other contexts, *this is not
7443 required to be of complete type for purposes of class member
7444 access (5.2.5) outside the member function body. */
7445 if (postfix_expression != current_class_ref
7446 && scope != error_mark_node
7447 && !(processing_template_decl
7448 && current_class_type
7449 && (same_type_ignoring_top_level_qualifiers_p
7450 (scope, current_class_type))))
7452 scope = complete_type (scope);
7453 if (!COMPLETE_TYPE_P (scope)
7454 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7455 && EXPR_P (postfix_expression))
7457 /* In a template, be permissive by treating an object expression
7458 of incomplete type as dependent (after a pedwarn). */
7459 diagnostic_t kind = (processing_template_decl
7460 && MAYBE_CLASS_TYPE_P (scope)
7461 ? DK_PEDWARN
7462 : DK_ERROR);
7463 cxx_incomplete_type_diagnostic
7464 (location_of (postfix_expression),
7465 postfix_expression, scope, kind);
7466 if (!MAYBE_CLASS_TYPE_P (scope))
7467 return error_mark_node;
7468 if (processing_template_decl)
7470 dependent_p = true;
7471 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7476 if (!dependent_p)
7478 /* Let the name lookup machinery know that we are processing a
7479 class member access expression. */
7480 parser->context->object_type = scope;
7481 /* If something went wrong, we want to be able to discern that case,
7482 as opposed to the case where there was no SCOPE due to the type
7483 of expression being dependent. */
7484 if (!scope)
7485 scope = error_mark_node;
7486 /* If the SCOPE was erroneous, make the various semantic analysis
7487 functions exit quickly -- and without issuing additional error
7488 messages. */
7489 if (scope == error_mark_node)
7490 postfix_expression = error_mark_node;
7494 if (dependent_p)
7495 /* Tell cp_parser_lookup_name that there was an object, even though it's
7496 type-dependent. */
7497 parser->context->object_type = unknown_type_node;
7499 /* Assume this expression is not a pseudo-destructor access. */
7500 pseudo_destructor_p = false;
7502 /* If the SCOPE is a scalar type, then, if this is a valid program,
7503 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7504 is type dependent, it can be pseudo-destructor-name or something else.
7505 Try to parse it as pseudo-destructor-name first. */
7506 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7508 tree s;
7509 tree type;
7511 cp_parser_parse_tentatively (parser);
7512 /* Parse the pseudo-destructor-name. */
7513 s = NULL_TREE;
7514 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7515 &s, &type);
7516 if (dependent_p
7517 && (cp_parser_error_occurred (parser)
7518 || !SCALAR_TYPE_P (type)))
7519 cp_parser_abort_tentative_parse (parser);
7520 else if (cp_parser_parse_definitely (parser))
7522 pseudo_destructor_p = true;
7523 postfix_expression
7524 = finish_pseudo_destructor_expr (postfix_expression,
7525 s, type, location);
7529 if (!pseudo_destructor_p)
7531 /* If the SCOPE is not a scalar type, we are looking at an
7532 ordinary class member access expression, rather than a
7533 pseudo-destructor-name. */
7534 bool template_p;
7535 cp_token *token = cp_lexer_peek_token (parser->lexer);
7536 /* Parse the id-expression. */
7537 name = (cp_parser_id_expression
7538 (parser,
7539 cp_parser_optional_template_keyword (parser),
7540 /*check_dependency_p=*/true,
7541 &template_p,
7542 /*declarator_p=*/false,
7543 /*optional_p=*/false));
7544 /* In general, build a SCOPE_REF if the member name is qualified.
7545 However, if the name was not dependent and has already been
7546 resolved; there is no need to build the SCOPE_REF. For example;
7548 struct X { void f(); };
7549 template <typename T> void f(T* t) { t->X::f(); }
7551 Even though "t" is dependent, "X::f" is not and has been resolved
7552 to a BASELINK; there is no need to include scope information. */
7554 /* But we do need to remember that there was an explicit scope for
7555 virtual function calls. */
7556 if (parser->scope)
7557 *idk = CP_ID_KIND_QUALIFIED;
7559 /* If the name is a template-id that names a type, we will get a
7560 TYPE_DECL here. That is invalid code. */
7561 if (TREE_CODE (name) == TYPE_DECL)
7563 error_at (token->location, "invalid use of %qD", name);
7564 postfix_expression = error_mark_node;
7566 else
7568 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7570 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7572 error_at (token->location, "%<%D::%D%> is not a class member",
7573 parser->scope, name);
7574 postfix_expression = error_mark_node;
7576 else
7577 name = build_qualified_name (/*type=*/NULL_TREE,
7578 parser->scope,
7579 name,
7580 template_p);
7581 parser->scope = NULL_TREE;
7582 parser->qualifying_scope = NULL_TREE;
7583 parser->object_scope = NULL_TREE;
7585 if (parser->scope && name && BASELINK_P (name))
7586 adjust_result_of_qualified_name_lookup
7587 (name, parser->scope, scope);
7588 postfix_expression
7589 = finish_class_member_access_expr (postfix_expression, name,
7590 template_p,
7591 tf_warning_or_error);
7592 /* Build a location e.g.:
7593 ptr->access_expr
7594 ~~~^~~~~~~~~~~~~
7595 where the caret is at the deref token, ranging from
7596 the start of postfix_expression to the end of the access expr. */
7597 location_t end_loc
7598 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7599 location_t combined_loc
7600 = make_location (input_location, start_loc, end_loc);
7601 protected_set_expr_location (postfix_expression, combined_loc);
7605 /* We no longer need to look up names in the scope of the object on
7606 the left-hand side of the `.' or `->' operator. */
7607 parser->context->object_type = NULL_TREE;
7609 /* Outside of offsetof, these operators may not appear in
7610 constant-expressions. */
7611 if (!for_offsetof
7612 && (cp_parser_non_integral_constant_expression
7613 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7614 postfix_expression = error_mark_node;
7616 return postfix_expression;
7619 /* Parse a parenthesized expression-list.
7621 expression-list:
7622 assignment-expression
7623 expression-list, assignment-expression
7625 attribute-list:
7626 expression-list
7627 identifier
7628 identifier, expression-list
7630 CAST_P is true if this expression is the target of a cast.
7632 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7633 argument pack.
7635 WRAP_LOCATIONS_P is true if expressions within this list for which
7636 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7637 their source locations.
7639 Returns a vector of trees. Each element is a representation of an
7640 assignment-expression. NULL is returned if the ( and or ) are
7641 missing. An empty, but allocated, vector is returned on no
7642 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7643 if we are parsing an attribute list for an attribute that wants a
7644 plain identifier argument, normal_attr for an attribute that wants
7645 an expression, or non_attr if we aren't parsing an attribute list. If
7646 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7647 not all of the expressions in the list were constant.
7648 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7649 will be written to with the location of the closing parenthesis. If
7650 an error occurs, it may or may not be written to. */
7652 static vec<tree, va_gc> *
7653 cp_parser_parenthesized_expression_list (cp_parser* parser,
7654 int is_attribute_list,
7655 bool cast_p,
7656 bool allow_expansion_p,
7657 bool *non_constant_p,
7658 location_t *close_paren_loc,
7659 bool wrap_locations_p)
7661 vec<tree, va_gc> *expression_list;
7662 bool fold_expr_p = is_attribute_list != non_attr;
7663 tree identifier = NULL_TREE;
7664 bool saved_greater_than_is_operator_p;
7666 /* Assume all the expressions will be constant. */
7667 if (non_constant_p)
7668 *non_constant_p = false;
7670 matching_parens parens;
7671 if (!parens.require_open (parser))
7672 return NULL;
7674 expression_list = make_tree_vector ();
7676 /* Within a parenthesized expression, a `>' token is always
7677 the greater-than operator. */
7678 saved_greater_than_is_operator_p
7679 = parser->greater_than_is_operator_p;
7680 parser->greater_than_is_operator_p = true;
7682 cp_expr expr (NULL_TREE);
7684 /* Consume expressions until there are no more. */
7685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7686 while (true)
7688 /* At the beginning of attribute lists, check to see if the
7689 next token is an identifier. */
7690 if (is_attribute_list == id_attr
7691 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7693 cp_token *token;
7695 /* Consume the identifier. */
7696 token = cp_lexer_consume_token (parser->lexer);
7697 /* Save the identifier. */
7698 identifier = token->u.value;
7700 else
7702 bool expr_non_constant_p;
7704 /* Parse the next assignment-expression. */
7705 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7707 /* A braced-init-list. */
7708 cp_lexer_set_source_position (parser->lexer);
7709 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7710 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7711 if (non_constant_p && expr_non_constant_p)
7712 *non_constant_p = true;
7714 else if (non_constant_p)
7716 expr = (cp_parser_constant_expression
7717 (parser, /*allow_non_constant_p=*/true,
7718 &expr_non_constant_p));
7719 if (expr_non_constant_p)
7720 *non_constant_p = true;
7722 else
7723 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7724 cast_p);
7726 if (fold_expr_p)
7727 expr = instantiate_non_dependent_expr (expr);
7729 /* If we have an ellipsis, then this is an expression
7730 expansion. */
7731 if (allow_expansion_p
7732 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7734 /* Consume the `...'. */
7735 cp_lexer_consume_token (parser->lexer);
7737 /* Build the argument pack. */
7738 expr = make_pack_expansion (expr);
7741 if (wrap_locations_p)
7742 expr.maybe_add_location_wrapper ();
7744 /* Add it to the list. We add error_mark_node
7745 expressions to the list, so that we can still tell if
7746 the correct form for a parenthesized expression-list
7747 is found. That gives better errors. */
7748 vec_safe_push (expression_list, expr.get_value ());
7750 if (expr == error_mark_node)
7751 goto skip_comma;
7754 /* After the first item, attribute lists look the same as
7755 expression lists. */
7756 is_attribute_list = non_attr;
7758 get_comma:;
7759 /* If the next token isn't a `,', then we are done. */
7760 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7761 break;
7763 /* Otherwise, consume the `,' and keep going. */
7764 cp_lexer_consume_token (parser->lexer);
7767 if (close_paren_loc)
7768 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7770 if (!parens.require_close (parser))
7772 int ending;
7774 skip_comma:;
7775 /* We try and resync to an unnested comma, as that will give the
7776 user better diagnostics. */
7777 ending = cp_parser_skip_to_closing_parenthesis (parser,
7778 /*recovering=*/true,
7779 /*or_comma=*/true,
7780 /*consume_paren=*/true);
7781 if (ending < 0)
7782 goto get_comma;
7783 if (!ending)
7785 parser->greater_than_is_operator_p
7786 = saved_greater_than_is_operator_p;
7787 return NULL;
7791 parser->greater_than_is_operator_p
7792 = saved_greater_than_is_operator_p;
7794 if (identifier)
7795 vec_safe_insert (expression_list, 0, identifier);
7797 return expression_list;
7800 /* Parse a pseudo-destructor-name.
7802 pseudo-destructor-name:
7803 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7804 :: [opt] nested-name-specifier template template-id :: ~ type-name
7805 :: [opt] nested-name-specifier [opt] ~ type-name
7807 If either of the first two productions is used, sets *SCOPE to the
7808 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7809 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7810 or ERROR_MARK_NODE if the parse fails. */
7812 static void
7813 cp_parser_pseudo_destructor_name (cp_parser* parser,
7814 tree object,
7815 tree* scope,
7816 tree* type)
7818 bool nested_name_specifier_p;
7820 /* Handle ~auto. */
7821 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7822 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7823 && !type_dependent_expression_p (object))
7825 if (cxx_dialect < cxx14)
7826 pedwarn (input_location, 0,
7827 "%<~auto%> only available with "
7828 "-std=c++14 or -std=gnu++14");
7829 cp_lexer_consume_token (parser->lexer);
7830 cp_lexer_consume_token (parser->lexer);
7831 *scope = NULL_TREE;
7832 *type = TREE_TYPE (object);
7833 return;
7836 /* Assume that things will not work out. */
7837 *type = error_mark_node;
7839 /* Look for the optional `::' operator. */
7840 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7841 /* Look for the optional nested-name-specifier. */
7842 nested_name_specifier_p
7843 = (cp_parser_nested_name_specifier_opt (parser,
7844 /*typename_keyword_p=*/false,
7845 /*check_dependency_p=*/true,
7846 /*type_p=*/false,
7847 /*is_declaration=*/false)
7848 != NULL_TREE);
7849 /* Now, if we saw a nested-name-specifier, we might be doing the
7850 second production. */
7851 if (nested_name_specifier_p
7852 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7854 /* Consume the `template' keyword. */
7855 cp_lexer_consume_token (parser->lexer);
7856 /* Parse the template-id. */
7857 cp_parser_template_id (parser,
7858 /*template_keyword_p=*/true,
7859 /*check_dependency_p=*/false,
7860 class_type,
7861 /*is_declaration=*/true);
7862 /* Look for the `::' token. */
7863 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7865 /* If the next token is not a `~', then there might be some
7866 additional qualification. */
7867 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7869 /* At this point, we're looking for "type-name :: ~". The type-name
7870 must not be a class-name, since this is a pseudo-destructor. So,
7871 it must be either an enum-name, or a typedef-name -- both of which
7872 are just identifiers. So, we peek ahead to check that the "::"
7873 and "~" tokens are present; if they are not, then we can avoid
7874 calling type_name. */
7875 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7876 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7877 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7879 cp_parser_error (parser, "non-scalar type");
7880 return;
7883 /* Look for the type-name. */
7884 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7885 if (*scope == error_mark_node)
7886 return;
7888 /* Look for the `::' token. */
7889 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7891 else
7892 *scope = NULL_TREE;
7894 /* Look for the `~'. */
7895 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7897 /* Once we see the ~, this has to be a pseudo-destructor. */
7898 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7899 cp_parser_commit_to_topmost_tentative_parse (parser);
7901 /* Look for the type-name again. We are not responsible for
7902 checking that it matches the first type-name. */
7903 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7906 /* Parse a unary-expression.
7908 unary-expression:
7909 postfix-expression
7910 ++ cast-expression
7911 -- cast-expression
7912 unary-operator cast-expression
7913 sizeof unary-expression
7914 sizeof ( type-id )
7915 alignof ( type-id ) [C++0x]
7916 new-expression
7917 delete-expression
7919 GNU Extensions:
7921 unary-expression:
7922 __extension__ cast-expression
7923 __alignof__ unary-expression
7924 __alignof__ ( type-id )
7925 alignof unary-expression [C++0x]
7926 __real__ cast-expression
7927 __imag__ cast-expression
7928 && identifier
7929 sizeof ( type-id ) { initializer-list , [opt] }
7930 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7931 __alignof__ ( type-id ) { initializer-list , [opt] }
7933 ADDRESS_P is true iff the unary-expression is appearing as the
7934 operand of the `&' operator. CAST_P is true if this expression is
7935 the target of a cast.
7937 Returns a representation of the expression. */
7939 static cp_expr
7940 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7941 bool address_p, bool cast_p, bool decltype_p)
7943 cp_token *token;
7944 enum tree_code unary_operator;
7946 /* Peek at the next token. */
7947 token = cp_lexer_peek_token (parser->lexer);
7948 /* Some keywords give away the kind of expression. */
7949 if (token->type == CPP_KEYWORD)
7951 enum rid keyword = token->keyword;
7953 switch (keyword)
7955 case RID_ALIGNOF:
7956 case RID_SIZEOF:
7958 tree operand, ret;
7959 enum tree_code op;
7960 location_t start_loc = token->location;
7962 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7963 /* Consume the token. */
7964 cp_lexer_consume_token (parser->lexer);
7965 /* Parse the operand. */
7966 operand = cp_parser_sizeof_operand (parser, keyword);
7968 if (TYPE_P (operand))
7969 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7970 else
7972 /* ISO C++ defines alignof only with types, not with
7973 expressions. So pedwarn if alignof is used with a non-
7974 type expression. However, __alignof__ is ok. */
7975 if (id_equal (token->u.value, "alignof"))
7976 pedwarn (token->location, OPT_Wpedantic,
7977 "ISO C++ does not allow %<alignof%> "
7978 "with a non-type");
7980 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7982 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7983 SIZEOF_EXPR with the original operand. */
7984 if (op == SIZEOF_EXPR && ret != error_mark_node)
7986 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7988 if (!processing_template_decl && TYPE_P (operand))
7990 ret = build_min (SIZEOF_EXPR, size_type_node,
7991 build1 (NOP_EXPR, operand,
7992 error_mark_node));
7993 SIZEOF_EXPR_TYPE_P (ret) = 1;
7995 else
7996 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7997 TREE_SIDE_EFFECTS (ret) = 0;
7998 TREE_READONLY (ret) = 1;
8002 /* Construct a location e.g. :
8003 alignof (expr)
8004 ^~~~~~~~~~~~~~
8005 with start == caret at the start of the "alignof"/"sizeof"
8006 token, with the endpoint at the final closing paren. */
8007 location_t finish_loc
8008 = cp_lexer_previous_token (parser->lexer)->location;
8009 location_t compound_loc
8010 = make_location (start_loc, start_loc, finish_loc);
8012 cp_expr ret_expr (ret);
8013 ret_expr.set_location (compound_loc);
8014 ret_expr = ret_expr.maybe_add_location_wrapper ();
8015 return ret_expr;
8018 case RID_NEW:
8019 return cp_parser_new_expression (parser);
8021 case RID_DELETE:
8022 return cp_parser_delete_expression (parser);
8024 case RID_EXTENSION:
8026 /* The saved value of the PEDANTIC flag. */
8027 int saved_pedantic;
8028 tree expr;
8030 /* Save away the PEDANTIC flag. */
8031 cp_parser_extension_opt (parser, &saved_pedantic);
8032 /* Parse the cast-expression. */
8033 expr = cp_parser_simple_cast_expression (parser);
8034 /* Restore the PEDANTIC flag. */
8035 pedantic = saved_pedantic;
8037 return expr;
8040 case RID_REALPART:
8041 case RID_IMAGPART:
8043 tree expression;
8045 /* Consume the `__real__' or `__imag__' token. */
8046 cp_lexer_consume_token (parser->lexer);
8047 /* Parse the cast-expression. */
8048 expression = cp_parser_simple_cast_expression (parser);
8049 /* Create the complete representation. */
8050 return build_x_unary_op (token->location,
8051 (keyword == RID_REALPART
8052 ? REALPART_EXPR : IMAGPART_EXPR),
8053 expression,
8054 tf_warning_or_error);
8056 break;
8058 case RID_TRANSACTION_ATOMIC:
8059 case RID_TRANSACTION_RELAXED:
8060 return cp_parser_transaction_expression (parser, keyword);
8062 case RID_NOEXCEPT:
8064 tree expr;
8065 const char *saved_message;
8066 bool saved_integral_constant_expression_p;
8067 bool saved_non_integral_constant_expression_p;
8068 bool saved_greater_than_is_operator_p;
8070 location_t start_loc = token->location;
8072 cp_lexer_consume_token (parser->lexer);
8073 matching_parens parens;
8074 parens.require_open (parser);
8076 saved_message = parser->type_definition_forbidden_message;
8077 parser->type_definition_forbidden_message
8078 = G_("types may not be defined in %<noexcept%> expressions");
8080 saved_integral_constant_expression_p
8081 = parser->integral_constant_expression_p;
8082 saved_non_integral_constant_expression_p
8083 = parser->non_integral_constant_expression_p;
8084 parser->integral_constant_expression_p = false;
8086 saved_greater_than_is_operator_p
8087 = parser->greater_than_is_operator_p;
8088 parser->greater_than_is_operator_p = true;
8090 ++cp_unevaluated_operand;
8091 ++c_inhibit_evaluation_warnings;
8092 ++cp_noexcept_operand;
8093 expr = cp_parser_expression (parser);
8094 --cp_noexcept_operand;
8095 --c_inhibit_evaluation_warnings;
8096 --cp_unevaluated_operand;
8098 parser->greater_than_is_operator_p
8099 = saved_greater_than_is_operator_p;
8101 parser->integral_constant_expression_p
8102 = saved_integral_constant_expression_p;
8103 parser->non_integral_constant_expression_p
8104 = saved_non_integral_constant_expression_p;
8106 parser->type_definition_forbidden_message = saved_message;
8108 location_t finish_loc
8109 = cp_lexer_peek_token (parser->lexer)->location;
8110 parens.require_close (parser);
8112 /* Construct a location of the form:
8113 noexcept (expr)
8114 ^~~~~~~~~~~~~~~
8115 with start == caret, finishing at the close-paren. */
8116 location_t noexcept_loc
8117 = make_location (start_loc, start_loc, finish_loc);
8119 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8120 noexcept_loc);
8123 default:
8124 break;
8128 /* Look for the `:: new' and `:: delete', which also signal the
8129 beginning of a new-expression, or delete-expression,
8130 respectively. If the next token is `::', then it might be one of
8131 these. */
8132 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8134 enum rid keyword;
8136 /* See if the token after the `::' is one of the keywords in
8137 which we're interested. */
8138 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8139 /* If it's `new', we have a new-expression. */
8140 if (keyword == RID_NEW)
8141 return cp_parser_new_expression (parser);
8142 /* Similarly, for `delete'. */
8143 else if (keyword == RID_DELETE)
8144 return cp_parser_delete_expression (parser);
8147 /* Look for a unary operator. */
8148 unary_operator = cp_parser_unary_operator (token);
8149 /* The `++' and `--' operators can be handled similarly, even though
8150 they are not technically unary-operators in the grammar. */
8151 if (unary_operator == ERROR_MARK)
8153 if (token->type == CPP_PLUS_PLUS)
8154 unary_operator = PREINCREMENT_EXPR;
8155 else if (token->type == CPP_MINUS_MINUS)
8156 unary_operator = PREDECREMENT_EXPR;
8157 /* Handle the GNU address-of-label extension. */
8158 else if (cp_parser_allow_gnu_extensions_p (parser)
8159 && token->type == CPP_AND_AND)
8161 tree identifier;
8162 tree expression;
8163 location_t start_loc = token->location;
8165 /* Consume the '&&' token. */
8166 cp_lexer_consume_token (parser->lexer);
8167 /* Look for the identifier. */
8168 location_t finish_loc
8169 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8170 identifier = cp_parser_identifier (parser);
8171 /* Construct a location of the form:
8172 &&label
8173 ^~~~~~~
8174 with caret==start at the "&&", finish at the end of the label. */
8175 location_t combined_loc
8176 = make_location (start_loc, start_loc, finish_loc);
8177 /* Create an expression representing the address. */
8178 expression = finish_label_address_expr (identifier, combined_loc);
8179 if (cp_parser_non_integral_constant_expression (parser,
8180 NIC_ADDR_LABEL))
8181 expression = error_mark_node;
8182 return expression;
8185 if (unary_operator != ERROR_MARK)
8187 cp_expr cast_expression;
8188 cp_expr expression = error_mark_node;
8189 non_integral_constant non_constant_p = NIC_NONE;
8190 location_t loc = token->location;
8191 tsubst_flags_t complain = complain_flags (decltype_p);
8193 /* Consume the operator token. */
8194 token = cp_lexer_consume_token (parser->lexer);
8195 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8197 /* Parse the cast-expression. */
8198 cast_expression
8199 = cp_parser_cast_expression (parser,
8200 unary_operator == ADDR_EXPR,
8201 /*cast_p=*/false,
8202 /*decltype*/false,
8203 pidk);
8205 /* Make a location:
8206 OP_TOKEN CAST_EXPRESSION
8207 ^~~~~~~~~~~~~~~~~~~~~~~~~
8208 with start==caret at the operator token, and
8209 extending to the end of the cast_expression. */
8210 loc = make_location (loc, loc, cast_expression.get_finish ());
8212 /* Now, build an appropriate representation. */
8213 switch (unary_operator)
8215 case INDIRECT_REF:
8216 non_constant_p = NIC_STAR;
8217 expression = build_x_indirect_ref (loc, cast_expression,
8218 RO_UNARY_STAR,
8219 complain);
8220 /* TODO: build_x_indirect_ref does not always honor the
8221 location, so ensure it is set. */
8222 expression.set_location (loc);
8223 break;
8225 case ADDR_EXPR:
8226 non_constant_p = NIC_ADDR;
8227 /* Fall through. */
8228 case BIT_NOT_EXPR:
8229 expression = build_x_unary_op (loc, unary_operator,
8230 cast_expression,
8231 complain);
8232 /* TODO: build_x_unary_op does not always honor the location,
8233 so ensure it is set. */
8234 expression.set_location (loc);
8235 break;
8237 case PREINCREMENT_EXPR:
8238 case PREDECREMENT_EXPR:
8239 non_constant_p = unary_operator == PREINCREMENT_EXPR
8240 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8241 /* Fall through. */
8242 case NEGATE_EXPR:
8243 /* Immediately fold negation of a constant, unless the constant is 0
8244 (since -0 == 0) or it would overflow. */
8245 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8246 && CONSTANT_CLASS_P (cast_expression)
8247 && !integer_zerop (cast_expression)
8248 && !TREE_OVERFLOW (cast_expression))
8250 tree folded = fold_build1 (unary_operator,
8251 TREE_TYPE (cast_expression),
8252 cast_expression);
8253 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8255 expression = cp_expr (folded, loc);
8256 break;
8259 /* Fall through. */
8260 case UNARY_PLUS_EXPR:
8261 case TRUTH_NOT_EXPR:
8262 expression = finish_unary_op_expr (loc, unary_operator,
8263 cast_expression, complain);
8264 break;
8266 default:
8267 gcc_unreachable ();
8270 if (non_constant_p != NIC_NONE
8271 && cp_parser_non_integral_constant_expression (parser,
8272 non_constant_p))
8273 expression = error_mark_node;
8275 return expression;
8278 return cp_parser_postfix_expression (parser, address_p, cast_p,
8279 /*member_access_only_p=*/false,
8280 decltype_p,
8281 pidk);
8284 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8285 unary-operator, the corresponding tree code is returned. */
8287 static enum tree_code
8288 cp_parser_unary_operator (cp_token* token)
8290 switch (token->type)
8292 case CPP_MULT:
8293 return INDIRECT_REF;
8295 case CPP_AND:
8296 return ADDR_EXPR;
8298 case CPP_PLUS:
8299 return UNARY_PLUS_EXPR;
8301 case CPP_MINUS:
8302 return NEGATE_EXPR;
8304 case CPP_NOT:
8305 return TRUTH_NOT_EXPR;
8307 case CPP_COMPL:
8308 return BIT_NOT_EXPR;
8310 default:
8311 return ERROR_MARK;
8315 /* Parse a new-expression.
8317 new-expression:
8318 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8319 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8321 Returns a representation of the expression. */
8323 static tree
8324 cp_parser_new_expression (cp_parser* parser)
8326 bool global_scope_p;
8327 vec<tree, va_gc> *placement;
8328 tree type;
8329 vec<tree, va_gc> *initializer;
8330 tree nelts = NULL_TREE;
8331 tree ret;
8333 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8335 /* Look for the optional `::' operator. */
8336 global_scope_p
8337 = (cp_parser_global_scope_opt (parser,
8338 /*current_scope_valid_p=*/false)
8339 != NULL_TREE);
8340 /* Look for the `new' operator. */
8341 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8342 /* There's no easy way to tell a new-placement from the
8343 `( type-id )' construct. */
8344 cp_parser_parse_tentatively (parser);
8345 /* Look for a new-placement. */
8346 placement = cp_parser_new_placement (parser);
8347 /* If that didn't work out, there's no new-placement. */
8348 if (!cp_parser_parse_definitely (parser))
8350 if (placement != NULL)
8351 release_tree_vector (placement);
8352 placement = NULL;
8355 /* If the next token is a `(', then we have a parenthesized
8356 type-id. */
8357 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8359 cp_token *token;
8360 const char *saved_message = parser->type_definition_forbidden_message;
8362 /* Consume the `('. */
8363 matching_parens parens;
8364 parens.consume_open (parser);
8366 /* Parse the type-id. */
8367 parser->type_definition_forbidden_message
8368 = G_("types may not be defined in a new-expression");
8370 type_id_in_expr_sentinel s (parser);
8371 type = cp_parser_type_id (parser);
8373 parser->type_definition_forbidden_message = saved_message;
8375 /* Look for the closing `)'. */
8376 parens.require_close (parser);
8377 token = cp_lexer_peek_token (parser->lexer);
8378 /* There should not be a direct-new-declarator in this production,
8379 but GCC used to allowed this, so we check and emit a sensible error
8380 message for this case. */
8381 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8383 error_at (token->location,
8384 "array bound forbidden after parenthesized type-id");
8385 inform (token->location,
8386 "try removing the parentheses around the type-id");
8387 cp_parser_direct_new_declarator (parser);
8390 /* Otherwise, there must be a new-type-id. */
8391 else
8392 type = cp_parser_new_type_id (parser, &nelts);
8394 /* If the next token is a `(' or '{', then we have a new-initializer. */
8395 cp_token *token = cp_lexer_peek_token (parser->lexer);
8396 if (token->type == CPP_OPEN_PAREN
8397 || token->type == CPP_OPEN_BRACE)
8398 initializer = cp_parser_new_initializer (parser);
8399 else
8400 initializer = NULL;
8402 /* A new-expression may not appear in an integral constant
8403 expression. */
8404 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8405 ret = error_mark_node;
8406 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8407 of a new-type-id or type-id of a new-expression, the new-expression shall
8408 contain a new-initializer of the form ( assignment-expression )".
8409 Additionally, consistently with the spirit of DR 1467, we want to accept
8410 'new auto { 2 }' too. */
8411 else if ((ret = type_uses_auto (type))
8412 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8413 && (vec_safe_length (initializer) != 1
8414 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8415 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8417 error_at (token->location,
8418 "initialization of new-expression for type %<auto%> "
8419 "requires exactly one element");
8420 ret = error_mark_node;
8422 else
8424 /* Construct a location e.g.:
8425 ptr = new int[100]
8426 ^~~~~~~~~~~~
8427 with caret == start at the start of the "new" token, and the end
8428 at the end of the final token we consumed. */
8429 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8430 location_t end_loc = get_finish (end_tok->location);
8431 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8433 /* Create a representation of the new-expression. */
8434 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8435 tf_warning_or_error);
8436 protected_set_expr_location (ret, combined_loc);
8439 if (placement != NULL)
8440 release_tree_vector (placement);
8441 if (initializer != NULL)
8442 release_tree_vector (initializer);
8444 return ret;
8447 /* Parse a new-placement.
8449 new-placement:
8450 ( expression-list )
8452 Returns the same representation as for an expression-list. */
8454 static vec<tree, va_gc> *
8455 cp_parser_new_placement (cp_parser* parser)
8457 vec<tree, va_gc> *expression_list;
8459 /* Parse the expression-list. */
8460 expression_list = (cp_parser_parenthesized_expression_list
8461 (parser, non_attr, /*cast_p=*/false,
8462 /*allow_expansion_p=*/true,
8463 /*non_constant_p=*/NULL));
8465 if (expression_list && expression_list->is_empty ())
8466 error ("expected expression-list or type-id");
8468 return expression_list;
8471 /* Parse a new-type-id.
8473 new-type-id:
8474 type-specifier-seq new-declarator [opt]
8476 Returns the TYPE allocated. If the new-type-id indicates an array
8477 type, *NELTS is set to the number of elements in the last array
8478 bound; the TYPE will not include the last array bound. */
8480 static tree
8481 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8483 cp_decl_specifier_seq type_specifier_seq;
8484 cp_declarator *new_declarator;
8485 cp_declarator *declarator;
8486 cp_declarator *outer_declarator;
8487 const char *saved_message;
8489 /* The type-specifier sequence must not contain type definitions.
8490 (It cannot contain declarations of new types either, but if they
8491 are not definitions we will catch that because they are not
8492 complete.) */
8493 saved_message = parser->type_definition_forbidden_message;
8494 parser->type_definition_forbidden_message
8495 = G_("types may not be defined in a new-type-id");
8496 /* Parse the type-specifier-seq. */
8497 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8498 /*is_trailing_return=*/false,
8499 &type_specifier_seq);
8500 /* Restore the old message. */
8501 parser->type_definition_forbidden_message = saved_message;
8503 if (type_specifier_seq.type == error_mark_node)
8504 return error_mark_node;
8506 /* Parse the new-declarator. */
8507 new_declarator = cp_parser_new_declarator_opt (parser);
8509 /* Determine the number of elements in the last array dimension, if
8510 any. */
8511 *nelts = NULL_TREE;
8512 /* Skip down to the last array dimension. */
8513 declarator = new_declarator;
8514 outer_declarator = NULL;
8515 while (declarator && (declarator->kind == cdk_pointer
8516 || declarator->kind == cdk_ptrmem))
8518 outer_declarator = declarator;
8519 declarator = declarator->declarator;
8521 while (declarator
8522 && declarator->kind == cdk_array
8523 && declarator->declarator
8524 && declarator->declarator->kind == cdk_array)
8526 outer_declarator = declarator;
8527 declarator = declarator->declarator;
8530 if (declarator && declarator->kind == cdk_array)
8532 *nelts = declarator->u.array.bounds;
8533 if (*nelts == error_mark_node)
8534 *nelts = integer_one_node;
8536 if (outer_declarator)
8537 outer_declarator->declarator = declarator->declarator;
8538 else
8539 new_declarator = NULL;
8542 return groktypename (&type_specifier_seq, new_declarator, false);
8545 /* Parse an (optional) new-declarator.
8547 new-declarator:
8548 ptr-operator new-declarator [opt]
8549 direct-new-declarator
8551 Returns the declarator. */
8553 static cp_declarator *
8554 cp_parser_new_declarator_opt (cp_parser* parser)
8556 enum tree_code code;
8557 tree type, std_attributes = NULL_TREE;
8558 cp_cv_quals cv_quals;
8560 /* We don't know if there's a ptr-operator next, or not. */
8561 cp_parser_parse_tentatively (parser);
8562 /* Look for a ptr-operator. */
8563 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8564 /* If that worked, look for more new-declarators. */
8565 if (cp_parser_parse_definitely (parser))
8567 cp_declarator *declarator;
8569 /* Parse another optional declarator. */
8570 declarator = cp_parser_new_declarator_opt (parser);
8572 declarator = cp_parser_make_indirect_declarator
8573 (code, type, cv_quals, declarator, std_attributes);
8575 return declarator;
8578 /* If the next token is a `[', there is a direct-new-declarator. */
8579 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8580 return cp_parser_direct_new_declarator (parser);
8582 return NULL;
8585 /* Parse a direct-new-declarator.
8587 direct-new-declarator:
8588 [ expression ]
8589 direct-new-declarator [constant-expression]
8593 static cp_declarator *
8594 cp_parser_direct_new_declarator (cp_parser* parser)
8596 cp_declarator *declarator = NULL;
8598 while (true)
8600 tree expression;
8601 cp_token *token;
8603 /* Look for the opening `['. */
8604 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8606 token = cp_lexer_peek_token (parser->lexer);
8607 expression = cp_parser_expression (parser);
8608 /* The standard requires that the expression have integral
8609 type. DR 74 adds enumeration types. We believe that the
8610 real intent is that these expressions be handled like the
8611 expression in a `switch' condition, which also allows
8612 classes with a single conversion to integral or
8613 enumeration type. */
8614 if (!processing_template_decl)
8616 expression
8617 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8618 expression,
8619 /*complain=*/true);
8620 if (!expression)
8622 error_at (token->location,
8623 "expression in new-declarator must have integral "
8624 "or enumeration type");
8625 expression = error_mark_node;
8629 /* Look for the closing `]'. */
8630 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8632 /* Add this bound to the declarator. */
8633 declarator = make_array_declarator (declarator, expression);
8635 /* If the next token is not a `[', then there are no more
8636 bounds. */
8637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8638 break;
8641 return declarator;
8644 /* Parse a new-initializer.
8646 new-initializer:
8647 ( expression-list [opt] )
8648 braced-init-list
8650 Returns a representation of the expression-list. */
8652 static vec<tree, va_gc> *
8653 cp_parser_new_initializer (cp_parser* parser)
8655 vec<tree, va_gc> *expression_list;
8657 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8659 tree t;
8660 bool expr_non_constant_p;
8661 cp_lexer_set_source_position (parser->lexer);
8662 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8663 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8664 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8665 expression_list = make_tree_vector_single (t);
8667 else
8668 expression_list = (cp_parser_parenthesized_expression_list
8669 (parser, non_attr, /*cast_p=*/false,
8670 /*allow_expansion_p=*/true,
8671 /*non_constant_p=*/NULL));
8673 return expression_list;
8676 /* Parse a delete-expression.
8678 delete-expression:
8679 :: [opt] delete cast-expression
8680 :: [opt] delete [ ] cast-expression
8682 Returns a representation of the expression. */
8684 static tree
8685 cp_parser_delete_expression (cp_parser* parser)
8687 bool global_scope_p;
8688 bool array_p;
8689 tree expression;
8691 /* Look for the optional `::' operator. */
8692 global_scope_p
8693 = (cp_parser_global_scope_opt (parser,
8694 /*current_scope_valid_p=*/false)
8695 != NULL_TREE);
8696 /* Look for the `delete' keyword. */
8697 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8698 /* See if the array syntax is in use. */
8699 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8701 /* Consume the `[' token. */
8702 cp_lexer_consume_token (parser->lexer);
8703 /* Look for the `]' token. */
8704 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8705 /* Remember that this is the `[]' construct. */
8706 array_p = true;
8708 else
8709 array_p = false;
8711 /* Parse the cast-expression. */
8712 expression = cp_parser_simple_cast_expression (parser);
8714 /* A delete-expression may not appear in an integral constant
8715 expression. */
8716 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8717 return error_mark_node;
8719 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8720 tf_warning_or_error);
8723 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8724 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8725 0 otherwise. */
8727 static int
8728 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8730 cp_token *token = cp_lexer_peek_token (parser->lexer);
8731 switch (token->type)
8733 case CPP_COMMA:
8734 case CPP_SEMICOLON:
8735 case CPP_QUERY:
8736 case CPP_COLON:
8737 case CPP_CLOSE_SQUARE:
8738 case CPP_CLOSE_PAREN:
8739 case CPP_CLOSE_BRACE:
8740 case CPP_OPEN_BRACE:
8741 case CPP_DOT:
8742 case CPP_DOT_STAR:
8743 case CPP_DEREF:
8744 case CPP_DEREF_STAR:
8745 case CPP_DIV:
8746 case CPP_MOD:
8747 case CPP_LSHIFT:
8748 case CPP_RSHIFT:
8749 case CPP_LESS:
8750 case CPP_GREATER:
8751 case CPP_LESS_EQ:
8752 case CPP_GREATER_EQ:
8753 case CPP_EQ_EQ:
8754 case CPP_NOT_EQ:
8755 case CPP_EQ:
8756 case CPP_MULT_EQ:
8757 case CPP_DIV_EQ:
8758 case CPP_MOD_EQ:
8759 case CPP_PLUS_EQ:
8760 case CPP_MINUS_EQ:
8761 case CPP_RSHIFT_EQ:
8762 case CPP_LSHIFT_EQ:
8763 case CPP_AND_EQ:
8764 case CPP_XOR_EQ:
8765 case CPP_OR_EQ:
8766 case CPP_XOR:
8767 case CPP_OR:
8768 case CPP_OR_OR:
8769 case CPP_EOF:
8770 case CPP_ELLIPSIS:
8771 return 0;
8773 case CPP_OPEN_PAREN:
8774 /* In ((type ()) () the last () isn't a valid cast-expression,
8775 so the whole must be parsed as postfix-expression. */
8776 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8777 != CPP_CLOSE_PAREN;
8779 case CPP_OPEN_SQUARE:
8780 /* '[' may start a primary-expression in obj-c++ and in C++11,
8781 as a lambda-expression, eg, '(void)[]{}'. */
8782 if (cxx_dialect >= cxx11)
8783 return -1;
8784 return c_dialect_objc ();
8786 case CPP_PLUS_PLUS:
8787 case CPP_MINUS_MINUS:
8788 /* '++' and '--' may or may not start a cast-expression:
8790 struct T { void operator++(int); };
8791 void f() { (T())++; }
8795 int a;
8796 (int)++a; */
8797 return -1;
8799 default:
8800 return 1;
8804 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8805 in the order: const_cast, static_cast, reinterpret_cast.
8807 Don't suggest dynamic_cast.
8809 Return the first legal cast kind found, or NULL otherwise. */
8811 static const char *
8812 get_cast_suggestion (tree dst_type, tree orig_expr)
8814 tree trial;
8816 /* Reuse the parser logic by attempting to build the various kinds of
8817 cast, with "complain" disabled.
8818 Identify the first such cast that is valid. */
8820 /* Don't attempt to run such logic within template processing. */
8821 if (processing_template_decl)
8822 return NULL;
8824 /* First try const_cast. */
8825 trial = build_const_cast (dst_type, orig_expr, tf_none);
8826 if (trial != error_mark_node)
8827 return "const_cast";
8829 /* If that fails, try static_cast. */
8830 trial = build_static_cast (dst_type, orig_expr, tf_none);
8831 if (trial != error_mark_node)
8832 return "static_cast";
8834 /* Finally, try reinterpret_cast. */
8835 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8836 if (trial != error_mark_node)
8837 return "reinterpret_cast";
8839 /* No such cast possible. */
8840 return NULL;
8843 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8844 suggesting how to convert a C-style cast of the form:
8846 (DST_TYPE)ORIG_EXPR
8848 to a C++-style cast.
8850 The primary range of RICHLOC is asssumed to be that of the original
8851 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8852 of the parens in the C-style cast. */
8854 static void
8855 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8856 location_t close_paren_loc, tree orig_expr,
8857 tree dst_type)
8859 /* This function is non-trivial, so bail out now if the warning isn't
8860 going to be emitted. */
8861 if (!warn_old_style_cast)
8862 return;
8864 /* Try to find a legal C++ cast, trying them in order:
8865 const_cast, static_cast, reinterpret_cast. */
8866 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8867 if (!cast_suggestion)
8868 return;
8870 /* Replace the open paren with "CAST_SUGGESTION<". */
8871 pretty_printer pp;
8872 pp_printf (&pp, "%s<", cast_suggestion);
8873 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8875 /* Replace the close paren with "> (". */
8876 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8878 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8879 rich_loc->add_fixit_insert_after (")");
8883 /* Parse a cast-expression.
8885 cast-expression:
8886 unary-expression
8887 ( type-id ) cast-expression
8889 ADDRESS_P is true iff the unary-expression is appearing as the
8890 operand of the `&' operator. CAST_P is true if this expression is
8891 the target of a cast.
8893 Returns a representation of the expression. */
8895 static cp_expr
8896 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8897 bool decltype_p, cp_id_kind * pidk)
8899 /* If it's a `(', then we might be looking at a cast. */
8900 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8902 tree type = NULL_TREE;
8903 cp_expr expr (NULL_TREE);
8904 int cast_expression = 0;
8905 const char *saved_message;
8907 /* There's no way to know yet whether or not this is a cast.
8908 For example, `(int (3))' is a unary-expression, while `(int)
8909 3' is a cast. So, we resort to parsing tentatively. */
8910 cp_parser_parse_tentatively (parser);
8911 /* Types may not be defined in a cast. */
8912 saved_message = parser->type_definition_forbidden_message;
8913 parser->type_definition_forbidden_message
8914 = G_("types may not be defined in casts");
8915 /* Consume the `('. */
8916 matching_parens parens;
8917 cp_token *open_paren = parens.consume_open (parser);
8918 location_t open_paren_loc = open_paren->location;
8919 location_t close_paren_loc = UNKNOWN_LOCATION;
8921 /* A very tricky bit is that `(struct S) { 3 }' is a
8922 compound-literal (which we permit in C++ as an extension).
8923 But, that construct is not a cast-expression -- it is a
8924 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8925 is legal; if the compound-literal were a cast-expression,
8926 you'd need an extra set of parentheses.) But, if we parse
8927 the type-id, and it happens to be a class-specifier, then we
8928 will commit to the parse at that point, because we cannot
8929 undo the action that is done when creating a new class. So,
8930 then we cannot back up and do a postfix-expression.
8932 Another tricky case is the following (c++/29234):
8934 struct S { void operator () (); };
8936 void foo ()
8938 ( S()() );
8941 As a type-id we parse the parenthesized S()() as a function
8942 returning a function, groktypename complains and we cannot
8943 back up in this case either.
8945 Therefore, we scan ahead to the closing `)', and check to see
8946 if the tokens after the `)' can start a cast-expression. Otherwise
8947 we are dealing with an unary-expression, a postfix-expression
8948 or something else.
8950 Yet another tricky case, in C++11, is the following (c++/54891):
8952 (void)[]{};
8954 The issue is that usually, besides the case of lambda-expressions,
8955 the parenthesized type-id cannot be followed by '[', and, eg, we
8956 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8957 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8958 we don't commit, we try a cast-expression, then an unary-expression.
8960 Save tokens so that we can put them back. */
8961 cp_lexer_save_tokens (parser->lexer);
8963 /* We may be looking at a cast-expression. */
8964 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8965 /*consume_paren=*/true))
8966 cast_expression
8967 = cp_parser_tokens_start_cast_expression (parser);
8969 /* Roll back the tokens we skipped. */
8970 cp_lexer_rollback_tokens (parser->lexer);
8971 /* If we aren't looking at a cast-expression, simulate an error so
8972 that the call to cp_parser_error_occurred below returns true. */
8973 if (!cast_expression)
8974 cp_parser_simulate_error (parser);
8975 else
8977 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8978 parser->in_type_id_in_expr_p = true;
8979 /* Look for the type-id. */
8980 type = cp_parser_type_id (parser);
8981 /* Look for the closing `)'. */
8982 cp_token *close_paren = parens.require_close (parser);
8983 if (close_paren)
8984 close_paren_loc = close_paren->location;
8985 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8988 /* Restore the saved message. */
8989 parser->type_definition_forbidden_message = saved_message;
8991 /* At this point this can only be either a cast or a
8992 parenthesized ctor such as `(T ())' that looks like a cast to
8993 function returning T. */
8994 if (!cp_parser_error_occurred (parser))
8996 /* Only commit if the cast-expression doesn't start with
8997 '++', '--', or '[' in C++11. */
8998 if (cast_expression > 0)
8999 cp_parser_commit_to_topmost_tentative_parse (parser);
9001 expr = cp_parser_cast_expression (parser,
9002 /*address_p=*/false,
9003 /*cast_p=*/true,
9004 /*decltype_p=*/false,
9005 pidk);
9007 if (cp_parser_parse_definitely (parser))
9009 /* Warn about old-style casts, if so requested. */
9010 if (warn_old_style_cast
9011 && !in_system_header_at (input_location)
9012 && !VOID_TYPE_P (type)
9013 && current_lang_name != lang_name_c)
9015 gcc_rich_location rich_loc (input_location);
9016 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9017 expr, type);
9018 warning_at (&rich_loc, OPT_Wold_style_cast,
9019 "use of old-style cast to %q#T", type);
9022 /* Only type conversions to integral or enumeration types
9023 can be used in constant-expressions. */
9024 if (!cast_valid_in_integral_constant_expression_p (type)
9025 && cp_parser_non_integral_constant_expression (parser,
9026 NIC_CAST))
9027 return error_mark_node;
9029 /* Perform the cast. */
9030 /* Make a location:
9031 (TYPE) EXPR
9032 ^~~~~~~~~~~
9033 with start==caret at the open paren, extending to the
9034 end of "expr". */
9035 location_t cast_loc = make_location (open_paren_loc,
9036 open_paren_loc,
9037 expr.get_finish ());
9038 expr = build_c_cast (cast_loc, type, expr);
9039 return expr;
9042 else
9043 cp_parser_abort_tentative_parse (parser);
9046 /* If we get here, then it's not a cast, so it must be a
9047 unary-expression. */
9048 return cp_parser_unary_expression (parser, pidk, address_p,
9049 cast_p, decltype_p);
9052 /* Parse a binary expression of the general form:
9054 pm-expression:
9055 cast-expression
9056 pm-expression .* cast-expression
9057 pm-expression ->* cast-expression
9059 multiplicative-expression:
9060 pm-expression
9061 multiplicative-expression * pm-expression
9062 multiplicative-expression / pm-expression
9063 multiplicative-expression % pm-expression
9065 additive-expression:
9066 multiplicative-expression
9067 additive-expression + multiplicative-expression
9068 additive-expression - multiplicative-expression
9070 shift-expression:
9071 additive-expression
9072 shift-expression << additive-expression
9073 shift-expression >> additive-expression
9075 relational-expression:
9076 shift-expression
9077 relational-expression < shift-expression
9078 relational-expression > shift-expression
9079 relational-expression <= shift-expression
9080 relational-expression >= shift-expression
9082 GNU Extension:
9084 relational-expression:
9085 relational-expression <? shift-expression
9086 relational-expression >? shift-expression
9088 equality-expression:
9089 relational-expression
9090 equality-expression == relational-expression
9091 equality-expression != relational-expression
9093 and-expression:
9094 equality-expression
9095 and-expression & equality-expression
9097 exclusive-or-expression:
9098 and-expression
9099 exclusive-or-expression ^ and-expression
9101 inclusive-or-expression:
9102 exclusive-or-expression
9103 inclusive-or-expression | exclusive-or-expression
9105 logical-and-expression:
9106 inclusive-or-expression
9107 logical-and-expression && inclusive-or-expression
9109 logical-or-expression:
9110 logical-and-expression
9111 logical-or-expression || logical-and-expression
9113 All these are implemented with a single function like:
9115 binary-expression:
9116 simple-cast-expression
9117 binary-expression <token> binary-expression
9119 CAST_P is true if this expression is the target of a cast.
9121 The binops_by_token map is used to get the tree codes for each <token> type.
9122 binary-expressions are associated according to a precedence table. */
9124 #define TOKEN_PRECEDENCE(token) \
9125 (((token->type == CPP_GREATER \
9126 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9127 && !parser->greater_than_is_operator_p) \
9128 ? PREC_NOT_OPERATOR \
9129 : binops_by_token[token->type].prec)
9131 static cp_expr
9132 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9133 bool no_toplevel_fold_p,
9134 bool decltype_p,
9135 enum cp_parser_prec prec,
9136 cp_id_kind * pidk)
9138 cp_parser_expression_stack stack;
9139 cp_parser_expression_stack_entry *sp = &stack[0];
9140 cp_parser_expression_stack_entry current;
9141 cp_expr rhs;
9142 cp_token *token;
9143 enum tree_code rhs_type;
9144 enum cp_parser_prec new_prec, lookahead_prec;
9145 tree overload;
9147 /* Parse the first expression. */
9148 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9149 ? TRUTH_NOT_EXPR : ERROR_MARK);
9150 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9151 cast_p, decltype_p, pidk);
9152 current.prec = prec;
9154 if (cp_parser_error_occurred (parser))
9155 return error_mark_node;
9157 for (;;)
9159 /* Get an operator token. */
9160 token = cp_lexer_peek_token (parser->lexer);
9162 if (warn_cxx11_compat
9163 && token->type == CPP_RSHIFT
9164 && !parser->greater_than_is_operator_p)
9166 if (warning_at (token->location, OPT_Wc__11_compat,
9167 "%<>>%> operator is treated"
9168 " as two right angle brackets in C++11"))
9169 inform (token->location,
9170 "suggest parentheses around %<>>%> expression");
9173 new_prec = TOKEN_PRECEDENCE (token);
9174 if (new_prec != PREC_NOT_OPERATOR
9175 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9176 /* This is a fold-expression; handle it later. */
9177 new_prec = PREC_NOT_OPERATOR;
9179 /* Popping an entry off the stack means we completed a subexpression:
9180 - either we found a token which is not an operator (`>' where it is not
9181 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9182 will happen repeatedly;
9183 - or, we found an operator which has lower priority. This is the case
9184 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9185 parsing `3 * 4'. */
9186 if (new_prec <= current.prec)
9188 if (sp == stack)
9189 break;
9190 else
9191 goto pop;
9194 get_rhs:
9195 current.tree_type = binops_by_token[token->type].tree_type;
9196 current.loc = token->location;
9198 /* We used the operator token. */
9199 cp_lexer_consume_token (parser->lexer);
9201 /* For "false && x" or "true || x", x will never be executed;
9202 disable warnings while evaluating it. */
9203 if (current.tree_type == TRUTH_ANDIF_EXPR)
9204 c_inhibit_evaluation_warnings +=
9205 cp_fully_fold (current.lhs) == truthvalue_false_node;
9206 else if (current.tree_type == TRUTH_ORIF_EXPR)
9207 c_inhibit_evaluation_warnings +=
9208 cp_fully_fold (current.lhs) == truthvalue_true_node;
9210 /* Extract another operand. It may be the RHS of this expression
9211 or the LHS of a new, higher priority expression. */
9212 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9213 ? TRUTH_NOT_EXPR : ERROR_MARK);
9214 rhs = cp_parser_simple_cast_expression (parser);
9216 /* Get another operator token. Look up its precedence to avoid
9217 building a useless (immediately popped) stack entry for common
9218 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9219 token = cp_lexer_peek_token (parser->lexer);
9220 lookahead_prec = TOKEN_PRECEDENCE (token);
9221 if (lookahead_prec != PREC_NOT_OPERATOR
9222 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9223 lookahead_prec = PREC_NOT_OPERATOR;
9224 if (lookahead_prec > new_prec)
9226 /* ... and prepare to parse the RHS of the new, higher priority
9227 expression. Since precedence levels on the stack are
9228 monotonically increasing, we do not have to care about
9229 stack overflows. */
9230 *sp = current;
9231 ++sp;
9232 current.lhs = rhs;
9233 current.lhs_type = rhs_type;
9234 current.prec = new_prec;
9235 new_prec = lookahead_prec;
9236 goto get_rhs;
9238 pop:
9239 lookahead_prec = new_prec;
9240 /* If the stack is not empty, we have parsed into LHS the right side
9241 (`4' in the example above) of an expression we had suspended.
9242 We can use the information on the stack to recover the LHS (`3')
9243 from the stack together with the tree code (`MULT_EXPR'), and
9244 the precedence of the higher level subexpression
9245 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9246 which will be used to actually build the additive expression. */
9247 rhs = current.lhs;
9248 rhs_type = current.lhs_type;
9249 --sp;
9250 current = *sp;
9253 /* Undo the disabling of warnings done above. */
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 if (warn_logical_not_paren
9262 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9263 && current.lhs_type == TRUTH_NOT_EXPR
9264 /* Avoid warning for !!x == y. */
9265 && (TREE_CODE (current.lhs) != NE_EXPR
9266 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9267 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9268 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9269 /* Avoid warning for !b == y where b is boolean. */
9270 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9271 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9272 != BOOLEAN_TYPE))))
9273 /* Avoid warning for !!b == y where b is boolean. */
9274 && (!DECL_P (current.lhs)
9275 || TREE_TYPE (current.lhs) == NULL_TREE
9276 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9277 warn_logical_not_parentheses (current.loc, current.tree_type,
9278 current.lhs, maybe_constant_value (rhs));
9280 overload = NULL;
9282 location_t combined_loc = make_location (current.loc,
9283 current.lhs.get_start (),
9284 rhs.get_finish ());
9286 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9287 ERROR_MARK for everything that is not a binary expression.
9288 This makes warn_about_parentheses miss some warnings that
9289 involve unary operators. For unary expressions we should
9290 pass the correct tree_code unless the unary expression was
9291 surrounded by parentheses.
9293 if (no_toplevel_fold_p
9294 && lookahead_prec <= current.prec
9295 && sp == stack)
9296 current.lhs = build2_loc (combined_loc,
9297 current.tree_type,
9298 TREE_CODE_CLASS (current.tree_type)
9299 == tcc_comparison
9300 ? boolean_type_node : TREE_TYPE (current.lhs),
9301 current.lhs, rhs);
9302 else
9304 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9305 current.lhs, current.lhs_type,
9306 rhs, rhs_type, &overload,
9307 complain_flags (decltype_p));
9308 /* TODO: build_x_binary_op doesn't always honor the location. */
9309 current.lhs.set_location (combined_loc);
9311 current.lhs_type = current.tree_type;
9313 /* If the binary operator required the use of an overloaded operator,
9314 then this expression cannot be an integral constant-expression.
9315 An overloaded operator can be used even if both operands are
9316 otherwise permissible in an integral constant-expression if at
9317 least one of the operands is of enumeration type. */
9319 if (overload
9320 && cp_parser_non_integral_constant_expression (parser,
9321 NIC_OVERLOADED))
9322 return error_mark_node;
9325 return current.lhs;
9328 static cp_expr
9329 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9330 bool no_toplevel_fold_p,
9331 enum cp_parser_prec prec,
9332 cp_id_kind * pidk)
9334 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9335 /*decltype*/false, prec, pidk);
9338 /* Parse the `? expression : assignment-expression' part of a
9339 conditional-expression. The LOGICAL_OR_EXPR is the
9340 logical-or-expression that started the conditional-expression.
9341 Returns a representation of the entire conditional-expression.
9343 This routine is used by cp_parser_assignment_expression.
9345 ? expression : assignment-expression
9347 GNU Extensions:
9349 ? : assignment-expression */
9351 static tree
9352 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9354 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9355 cp_expr assignment_expr;
9356 struct cp_token *token;
9357 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9359 /* Consume the `?' token. */
9360 cp_lexer_consume_token (parser->lexer);
9361 token = cp_lexer_peek_token (parser->lexer);
9362 if (cp_parser_allow_gnu_extensions_p (parser)
9363 && token->type == CPP_COLON)
9365 pedwarn (token->location, OPT_Wpedantic,
9366 "ISO C++ does not allow ?: with omitted middle operand");
9367 /* Implicit true clause. */
9368 expr = NULL_TREE;
9369 c_inhibit_evaluation_warnings +=
9370 folded_logical_or_expr == truthvalue_true_node;
9371 warn_for_omitted_condop (token->location, logical_or_expr);
9373 else
9375 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9376 parser->colon_corrects_to_scope_p = false;
9377 /* Parse the expression. */
9378 c_inhibit_evaluation_warnings +=
9379 folded_logical_or_expr == truthvalue_false_node;
9380 expr = cp_parser_expression (parser);
9381 c_inhibit_evaluation_warnings +=
9382 ((folded_logical_or_expr == truthvalue_true_node)
9383 - (folded_logical_or_expr == truthvalue_false_node));
9384 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9387 /* The next token should be a `:'. */
9388 cp_parser_require (parser, CPP_COLON, RT_COLON);
9389 /* Parse the assignment-expression. */
9390 assignment_expr = cp_parser_assignment_expression (parser);
9391 c_inhibit_evaluation_warnings -=
9392 folded_logical_or_expr == truthvalue_true_node;
9394 /* Make a location:
9395 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9396 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9397 with the caret at the "?", ranging from the start of
9398 the logical_or_expr to the end of the assignment_expr. */
9399 loc = make_location (loc,
9400 logical_or_expr.get_start (),
9401 assignment_expr.get_finish ());
9403 /* Build the conditional-expression. */
9404 return build_x_conditional_expr (loc, logical_or_expr,
9405 expr,
9406 assignment_expr,
9407 tf_warning_or_error);
9410 /* Parse an assignment-expression.
9412 assignment-expression:
9413 conditional-expression
9414 logical-or-expression assignment-operator assignment_expression
9415 throw-expression
9417 CAST_P is true if this expression is the target of a cast.
9418 DECLTYPE_P is true if this expression is the operand of decltype.
9420 Returns a representation for the expression. */
9422 static cp_expr
9423 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9424 bool cast_p, bool decltype_p)
9426 cp_expr expr;
9428 /* If the next token is the `throw' keyword, then we're looking at
9429 a throw-expression. */
9430 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9431 expr = cp_parser_throw_expression (parser);
9432 /* Otherwise, it must be that we are looking at a
9433 logical-or-expression. */
9434 else
9436 /* Parse the binary expressions (logical-or-expression). */
9437 expr = cp_parser_binary_expression (parser, cast_p, false,
9438 decltype_p,
9439 PREC_NOT_OPERATOR, pidk);
9440 /* If the next token is a `?' then we're actually looking at a
9441 conditional-expression. */
9442 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9443 return cp_parser_question_colon_clause (parser, expr);
9444 else
9446 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9448 /* If it's an assignment-operator, we're using the second
9449 production. */
9450 enum tree_code assignment_operator
9451 = cp_parser_assignment_operator_opt (parser);
9452 if (assignment_operator != ERROR_MARK)
9454 bool non_constant_p;
9456 /* Parse the right-hand side of the assignment. */
9457 cp_expr rhs = cp_parser_initializer_clause (parser,
9458 &non_constant_p);
9460 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9461 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9463 /* An assignment may not appear in a
9464 constant-expression. */
9465 if (cp_parser_non_integral_constant_expression (parser,
9466 NIC_ASSIGNMENT))
9467 return error_mark_node;
9468 /* Build the assignment expression. Its default
9469 location:
9470 LHS = RHS
9471 ~~~~^~~~~
9472 is the location of the '=' token as the
9473 caret, ranging from the start of the lhs to the
9474 end of the rhs. */
9475 loc = make_location (loc,
9476 expr.get_start (),
9477 rhs.get_finish ());
9478 expr = build_x_modify_expr (loc, expr,
9479 assignment_operator,
9480 rhs,
9481 complain_flags (decltype_p));
9482 /* TODO: build_x_modify_expr doesn't honor the location,
9483 so we must set it here. */
9484 expr.set_location (loc);
9489 return expr;
9492 /* Parse an (optional) assignment-operator.
9494 assignment-operator: one of
9495 = *= /= %= += -= >>= <<= &= ^= |=
9497 GNU Extension:
9499 assignment-operator: one of
9500 <?= >?=
9502 If the next token is an assignment operator, the corresponding tree
9503 code is returned, and the token is consumed. For example, for
9504 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9505 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9506 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9507 operator, ERROR_MARK is returned. */
9509 static enum tree_code
9510 cp_parser_assignment_operator_opt (cp_parser* parser)
9512 enum tree_code op;
9513 cp_token *token;
9515 /* Peek at the next token. */
9516 token = cp_lexer_peek_token (parser->lexer);
9518 switch (token->type)
9520 case CPP_EQ:
9521 op = NOP_EXPR;
9522 break;
9524 case CPP_MULT_EQ:
9525 op = MULT_EXPR;
9526 break;
9528 case CPP_DIV_EQ:
9529 op = TRUNC_DIV_EXPR;
9530 break;
9532 case CPP_MOD_EQ:
9533 op = TRUNC_MOD_EXPR;
9534 break;
9536 case CPP_PLUS_EQ:
9537 op = PLUS_EXPR;
9538 break;
9540 case CPP_MINUS_EQ:
9541 op = MINUS_EXPR;
9542 break;
9544 case CPP_RSHIFT_EQ:
9545 op = RSHIFT_EXPR;
9546 break;
9548 case CPP_LSHIFT_EQ:
9549 op = LSHIFT_EXPR;
9550 break;
9552 case CPP_AND_EQ:
9553 op = BIT_AND_EXPR;
9554 break;
9556 case CPP_XOR_EQ:
9557 op = BIT_XOR_EXPR;
9558 break;
9560 case CPP_OR_EQ:
9561 op = BIT_IOR_EXPR;
9562 break;
9564 default:
9565 /* Nothing else is an assignment operator. */
9566 op = ERROR_MARK;
9569 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9570 if (op != ERROR_MARK
9571 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9572 op = ERROR_MARK;
9574 /* If it was an assignment operator, consume it. */
9575 if (op != ERROR_MARK)
9576 cp_lexer_consume_token (parser->lexer);
9578 return op;
9581 /* Parse an expression.
9583 expression:
9584 assignment-expression
9585 expression , assignment-expression
9587 CAST_P is true if this expression is the target of a cast.
9588 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9589 except possibly parenthesized or on the RHS of a comma (N3276).
9591 Returns a representation of the expression. */
9593 static cp_expr
9594 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9595 bool cast_p, bool decltype_p)
9597 cp_expr expression = NULL_TREE;
9598 location_t loc = UNKNOWN_LOCATION;
9600 while (true)
9602 cp_expr assignment_expression;
9604 /* Parse the next assignment-expression. */
9605 assignment_expression
9606 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9608 /* We don't create a temporary for a call that is the immediate operand
9609 of decltype or on the RHS of a comma. But when we see a comma, we
9610 need to create a temporary for a call on the LHS. */
9611 if (decltype_p && !processing_template_decl
9612 && TREE_CODE (assignment_expression) == CALL_EXPR
9613 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9614 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9615 assignment_expression
9616 = build_cplus_new (TREE_TYPE (assignment_expression),
9617 assignment_expression, tf_warning_or_error);
9619 /* If this is the first assignment-expression, we can just
9620 save it away. */
9621 if (!expression)
9622 expression = assignment_expression;
9623 else
9625 /* Create a location with caret at the comma, ranging
9626 from the start of the LHS to the end of the RHS. */
9627 loc = make_location (loc,
9628 expression.get_start (),
9629 assignment_expression.get_finish ());
9630 expression = build_x_compound_expr (loc, expression,
9631 assignment_expression,
9632 complain_flags (decltype_p));
9633 expression.set_location (loc);
9635 /* If the next token is not a comma, or we're in a fold-expression, then
9636 we are done with the expression. */
9637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9638 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9639 break;
9640 /* Consume the `,'. */
9641 loc = cp_lexer_peek_token (parser->lexer)->location;
9642 cp_lexer_consume_token (parser->lexer);
9643 /* A comma operator cannot appear in a constant-expression. */
9644 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9645 expression = error_mark_node;
9648 return expression;
9651 /* Parse a constant-expression.
9653 constant-expression:
9654 conditional-expression
9656 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9657 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9658 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9659 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9660 only parse a conditional-expression, otherwise parse an
9661 assignment-expression. See below for rationale. */
9663 static cp_expr
9664 cp_parser_constant_expression (cp_parser* parser,
9665 bool allow_non_constant_p,
9666 bool *non_constant_p,
9667 bool strict_p)
9669 bool saved_integral_constant_expression_p;
9670 bool saved_allow_non_integral_constant_expression_p;
9671 bool saved_non_integral_constant_expression_p;
9672 cp_expr expression;
9674 /* It might seem that we could simply parse the
9675 conditional-expression, and then check to see if it were
9676 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9677 one that the compiler can figure out is constant, possibly after
9678 doing some simplifications or optimizations. The standard has a
9679 precise definition of constant-expression, and we must honor
9680 that, even though it is somewhat more restrictive.
9682 For example:
9684 int i[(2, 3)];
9686 is not a legal declaration, because `(2, 3)' is not a
9687 constant-expression. The `,' operator is forbidden in a
9688 constant-expression. However, GCC's constant-folding machinery
9689 will fold this operation to an INTEGER_CST for `3'. */
9691 /* Save the old settings. */
9692 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9693 saved_allow_non_integral_constant_expression_p
9694 = parser->allow_non_integral_constant_expression_p;
9695 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9696 /* We are now parsing a constant-expression. */
9697 parser->integral_constant_expression_p = true;
9698 parser->allow_non_integral_constant_expression_p
9699 = (allow_non_constant_p || cxx_dialect >= cxx11);
9700 parser->non_integral_constant_expression_p = false;
9701 /* Although the grammar says "conditional-expression", when not STRICT_P,
9702 we parse an "assignment-expression", which also permits
9703 "throw-expression" and the use of assignment operators. In the case
9704 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9705 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9706 actually essential that we look for an assignment-expression.
9707 For example, cp_parser_initializer_clauses uses this function to
9708 determine whether a particular assignment-expression is in fact
9709 constant. */
9710 if (strict_p)
9712 /* Parse the binary expressions (logical-or-expression). */
9713 expression = cp_parser_binary_expression (parser, false, false, false,
9714 PREC_NOT_OPERATOR, NULL);
9715 /* If the next token is a `?' then we're actually looking at
9716 a conditional-expression; otherwise we're done. */
9717 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9718 expression = cp_parser_question_colon_clause (parser, expression);
9720 else
9721 expression = cp_parser_assignment_expression (parser);
9722 /* Restore the old settings. */
9723 parser->integral_constant_expression_p
9724 = saved_integral_constant_expression_p;
9725 parser->allow_non_integral_constant_expression_p
9726 = saved_allow_non_integral_constant_expression_p;
9727 if (cxx_dialect >= cxx11)
9729 /* Require an rvalue constant expression here; that's what our
9730 callers expect. Reference constant expressions are handled
9731 separately in e.g. cp_parser_template_argument. */
9732 tree decay = expression;
9733 if (TREE_TYPE (expression)
9734 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9735 decay = build_address (expression);
9736 bool is_const = potential_rvalue_constant_expression (decay);
9737 parser->non_integral_constant_expression_p = !is_const;
9738 if (!is_const && !allow_non_constant_p)
9739 require_potential_rvalue_constant_expression (decay);
9741 if (allow_non_constant_p)
9742 *non_constant_p = parser->non_integral_constant_expression_p;
9743 parser->non_integral_constant_expression_p
9744 = saved_non_integral_constant_expression_p;
9746 return expression;
9749 /* Parse __builtin_offsetof.
9751 offsetof-expression:
9752 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9754 offsetof-member-designator:
9755 id-expression
9756 | offsetof-member-designator "." id-expression
9757 | offsetof-member-designator "[" expression "]"
9758 | offsetof-member-designator "->" id-expression */
9760 static cp_expr
9761 cp_parser_builtin_offsetof (cp_parser *parser)
9763 int save_ice_p, save_non_ice_p;
9764 tree type;
9765 cp_expr expr;
9766 cp_id_kind dummy;
9767 cp_token *token;
9768 location_t finish_loc;
9770 /* We're about to accept non-integral-constant things, but will
9771 definitely yield an integral constant expression. Save and
9772 restore these values around our local parsing. */
9773 save_ice_p = parser->integral_constant_expression_p;
9774 save_non_ice_p = parser->non_integral_constant_expression_p;
9776 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9778 /* Consume the "__builtin_offsetof" token. */
9779 cp_lexer_consume_token (parser->lexer);
9780 /* Consume the opening `('. */
9781 matching_parens parens;
9782 parens.require_open (parser);
9783 /* Parse the type-id. */
9784 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9785 type = cp_parser_type_id (parser);
9786 /* Look for the `,'. */
9787 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9788 token = cp_lexer_peek_token (parser->lexer);
9790 /* Build the (type *)null that begins the traditional offsetof macro. */
9791 tree object_ptr
9792 = build_static_cast (build_pointer_type (type), null_pointer_node,
9793 tf_warning_or_error);
9795 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9796 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9797 true, &dummy, token->location);
9798 while (true)
9800 token = cp_lexer_peek_token (parser->lexer);
9801 switch (token->type)
9803 case CPP_OPEN_SQUARE:
9804 /* offsetof-member-designator "[" expression "]" */
9805 expr = cp_parser_postfix_open_square_expression (parser, expr,
9806 true, false);
9807 break;
9809 case CPP_DEREF:
9810 /* offsetof-member-designator "->" identifier */
9811 expr = grok_array_decl (token->location, expr,
9812 integer_zero_node, false);
9813 /* FALLTHRU */
9815 case CPP_DOT:
9816 /* offsetof-member-designator "." identifier */
9817 cp_lexer_consume_token (parser->lexer);
9818 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9819 expr, true, &dummy,
9820 token->location);
9821 break;
9823 case CPP_CLOSE_PAREN:
9824 /* Consume the ")" token. */
9825 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9826 cp_lexer_consume_token (parser->lexer);
9827 goto success;
9829 default:
9830 /* Error. We know the following require will fail, but
9831 that gives the proper error message. */
9832 parens.require_close (parser);
9833 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9834 expr = error_mark_node;
9835 goto failure;
9839 success:
9840 /* Make a location of the form:
9841 __builtin_offsetof (struct s, f)
9842 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9843 with caret at the type-id, ranging from the start of the
9844 "_builtin_offsetof" token to the close paren. */
9845 loc = make_location (loc, start_loc, finish_loc);
9846 /* The result will be an INTEGER_CST, so we need to explicitly
9847 preserve the location. */
9848 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9850 failure:
9851 parser->integral_constant_expression_p = save_ice_p;
9852 parser->non_integral_constant_expression_p = save_non_ice_p;
9854 expr = expr.maybe_add_location_wrapper ();
9855 return expr;
9858 /* Parse a trait expression.
9860 Returns a representation of the expression, the underlying type
9861 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9863 static cp_expr
9864 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9866 cp_trait_kind kind;
9867 tree type1, type2 = NULL_TREE;
9868 bool binary = false;
9869 bool variadic = false;
9871 switch (keyword)
9873 case RID_HAS_NOTHROW_ASSIGN:
9874 kind = CPTK_HAS_NOTHROW_ASSIGN;
9875 break;
9876 case RID_HAS_NOTHROW_CONSTRUCTOR:
9877 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9878 break;
9879 case RID_HAS_NOTHROW_COPY:
9880 kind = CPTK_HAS_NOTHROW_COPY;
9881 break;
9882 case RID_HAS_TRIVIAL_ASSIGN:
9883 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9884 break;
9885 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9886 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9887 break;
9888 case RID_HAS_TRIVIAL_COPY:
9889 kind = CPTK_HAS_TRIVIAL_COPY;
9890 break;
9891 case RID_HAS_TRIVIAL_DESTRUCTOR:
9892 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9893 break;
9894 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9895 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9896 break;
9897 case RID_HAS_VIRTUAL_DESTRUCTOR:
9898 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9899 break;
9900 case RID_IS_ABSTRACT:
9901 kind = CPTK_IS_ABSTRACT;
9902 break;
9903 case RID_IS_AGGREGATE:
9904 kind = CPTK_IS_AGGREGATE;
9905 break;
9906 case RID_IS_BASE_OF:
9907 kind = CPTK_IS_BASE_OF;
9908 binary = true;
9909 break;
9910 case RID_IS_CLASS:
9911 kind = CPTK_IS_CLASS;
9912 break;
9913 case RID_IS_EMPTY:
9914 kind = CPTK_IS_EMPTY;
9915 break;
9916 case RID_IS_ENUM:
9917 kind = CPTK_IS_ENUM;
9918 break;
9919 case RID_IS_FINAL:
9920 kind = CPTK_IS_FINAL;
9921 break;
9922 case RID_IS_LITERAL_TYPE:
9923 kind = CPTK_IS_LITERAL_TYPE;
9924 break;
9925 case RID_IS_POD:
9926 kind = CPTK_IS_POD;
9927 break;
9928 case RID_IS_POLYMORPHIC:
9929 kind = CPTK_IS_POLYMORPHIC;
9930 break;
9931 case RID_IS_SAME_AS:
9932 kind = CPTK_IS_SAME_AS;
9933 binary = true;
9934 break;
9935 case RID_IS_STD_LAYOUT:
9936 kind = CPTK_IS_STD_LAYOUT;
9937 break;
9938 case RID_IS_TRIVIAL:
9939 kind = CPTK_IS_TRIVIAL;
9940 break;
9941 case RID_IS_TRIVIALLY_ASSIGNABLE:
9942 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9943 binary = true;
9944 break;
9945 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9946 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9947 variadic = true;
9948 break;
9949 case RID_IS_TRIVIALLY_COPYABLE:
9950 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9951 break;
9952 case RID_IS_UNION:
9953 kind = CPTK_IS_UNION;
9954 break;
9955 case RID_UNDERLYING_TYPE:
9956 kind = CPTK_UNDERLYING_TYPE;
9957 break;
9958 case RID_BASES:
9959 kind = CPTK_BASES;
9960 break;
9961 case RID_DIRECT_BASES:
9962 kind = CPTK_DIRECT_BASES;
9963 break;
9964 case RID_IS_ASSIGNABLE:
9965 kind = CPTK_IS_ASSIGNABLE;
9966 binary = true;
9967 break;
9968 case RID_IS_CONSTRUCTIBLE:
9969 kind = CPTK_IS_CONSTRUCTIBLE;
9970 variadic = true;
9971 break;
9972 default:
9973 gcc_unreachable ();
9976 /* Get location of initial token. */
9977 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9979 /* Consume the token. */
9980 cp_lexer_consume_token (parser->lexer);
9982 matching_parens parens;
9983 parens.require_open (parser);
9986 type_id_in_expr_sentinel s (parser);
9987 type1 = cp_parser_type_id (parser);
9990 if (type1 == error_mark_node)
9991 return error_mark_node;
9993 if (binary)
9995 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9998 type_id_in_expr_sentinel s (parser);
9999 type2 = cp_parser_type_id (parser);
10002 if (type2 == error_mark_node)
10003 return error_mark_node;
10005 else if (variadic)
10007 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10009 cp_lexer_consume_token (parser->lexer);
10010 tree elt = cp_parser_type_id (parser);
10011 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10013 cp_lexer_consume_token (parser->lexer);
10014 elt = make_pack_expansion (elt);
10016 if (elt == error_mark_node)
10017 return error_mark_node;
10018 type2 = tree_cons (NULL_TREE, elt, type2);
10022 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10023 parens.require_close (parser);
10025 /* Construct a location of the form:
10026 __is_trivially_copyable(_Tp)
10027 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10028 with start == caret, finishing at the close-paren. */
10029 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10031 /* Complete the trait expression, which may mean either processing
10032 the trait expr now or saving it for template instantiation. */
10033 switch (kind)
10035 case CPTK_UNDERLYING_TYPE:
10036 return cp_expr (finish_underlying_type (type1), trait_loc);
10037 case CPTK_BASES:
10038 return cp_expr (finish_bases (type1, false), trait_loc);
10039 case CPTK_DIRECT_BASES:
10040 return cp_expr (finish_bases (type1, true), trait_loc);
10041 default:
10042 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10046 /* Parse a lambda expression.
10048 lambda-expression:
10049 lambda-introducer lambda-declarator [opt] compound-statement
10051 Returns a representation of the expression. */
10053 static cp_expr
10054 cp_parser_lambda_expression (cp_parser* parser)
10056 tree lambda_expr = build_lambda_expr ();
10057 tree type;
10058 bool ok = true;
10059 cp_token *token = cp_lexer_peek_token (parser->lexer);
10060 cp_token_position start = 0;
10062 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10064 if (cp_unevaluated_operand)
10066 if (!token->error_reported)
10068 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10069 "lambda-expression in unevaluated context");
10070 token->error_reported = true;
10072 ok = false;
10074 else if (parser->in_template_argument_list_p)
10076 if (!token->error_reported)
10078 error_at (token->location, "lambda-expression in template-argument");
10079 token->error_reported = true;
10081 ok = false;
10084 /* We may be in the middle of deferred access check. Disable
10085 it now. */
10086 push_deferring_access_checks (dk_no_deferred);
10088 cp_parser_lambda_introducer (parser, lambda_expr);
10090 type = begin_lambda_type (lambda_expr);
10091 if (type == error_mark_node)
10092 return error_mark_node;
10094 record_lambda_scope (lambda_expr);
10096 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10097 determine_visibility (TYPE_NAME (type));
10099 /* Now that we've started the type, add the capture fields for any
10100 explicit captures. */
10101 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10104 /* Inside the class, surrounding template-parameter-lists do not apply. */
10105 unsigned int saved_num_template_parameter_lists
10106 = parser->num_template_parameter_lists;
10107 unsigned char in_statement = parser->in_statement;
10108 bool in_switch_statement_p = parser->in_switch_statement_p;
10109 bool fully_implicit_function_template_p
10110 = parser->fully_implicit_function_template_p;
10111 tree implicit_template_parms = parser->implicit_template_parms;
10112 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10113 bool auto_is_implicit_function_template_parm_p
10114 = parser->auto_is_implicit_function_template_parm_p;
10116 parser->num_template_parameter_lists = 0;
10117 parser->in_statement = 0;
10118 parser->in_switch_statement_p = false;
10119 parser->fully_implicit_function_template_p = false;
10120 parser->implicit_template_parms = 0;
10121 parser->implicit_template_scope = 0;
10122 parser->auto_is_implicit_function_template_parm_p = false;
10124 /* By virtue of defining a local class, a lambda expression has access to
10125 the private variables of enclosing classes. */
10127 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10129 if (ok && cp_parser_error_occurred (parser))
10130 ok = false;
10132 if (ok)
10134 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10135 && cp_parser_start_tentative_firewall (parser))
10136 start = token;
10137 cp_parser_lambda_body (parser, lambda_expr);
10139 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10141 if (cp_parser_skip_to_closing_brace (parser))
10142 cp_lexer_consume_token (parser->lexer);
10145 /* The capture list was built up in reverse order; fix that now. */
10146 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10147 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10149 if (ok)
10150 maybe_add_lambda_conv_op (type);
10152 type = finish_struct (type, /*attributes=*/NULL_TREE);
10154 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10155 parser->in_statement = in_statement;
10156 parser->in_switch_statement_p = in_switch_statement_p;
10157 parser->fully_implicit_function_template_p
10158 = fully_implicit_function_template_p;
10159 parser->implicit_template_parms = implicit_template_parms;
10160 parser->implicit_template_scope = implicit_template_scope;
10161 parser->auto_is_implicit_function_template_parm_p
10162 = auto_is_implicit_function_template_parm_p;
10165 /* This field is only used during parsing of the lambda. */
10166 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10168 /* This lambda shouldn't have any proxies left at this point. */
10169 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10170 /* And now that we're done, push proxies for an enclosing lambda. */
10171 insert_pending_capture_proxies ();
10173 if (ok)
10174 lambda_expr = build_lambda_object (lambda_expr);
10175 else
10176 lambda_expr = error_mark_node;
10178 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10180 pop_deferring_access_checks ();
10182 return lambda_expr;
10185 /* Parse the beginning of a lambda expression.
10187 lambda-introducer:
10188 [ lambda-capture [opt] ]
10190 LAMBDA_EXPR is the current representation of the lambda expression. */
10192 static void
10193 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10195 /* Need commas after the first capture. */
10196 bool first = true;
10198 /* Eat the leading `['. */
10199 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10201 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10202 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10203 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10204 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10205 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10206 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10208 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10210 cp_lexer_consume_token (parser->lexer);
10211 first = false;
10214 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10216 cp_token* capture_token;
10217 tree capture_id;
10218 tree capture_init_expr;
10219 cp_id_kind idk = CP_ID_KIND_NONE;
10220 bool explicit_init_p = false;
10222 enum capture_kind_type
10224 BY_COPY,
10225 BY_REFERENCE
10227 enum capture_kind_type capture_kind = BY_COPY;
10229 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10231 error ("expected end of capture-list");
10232 return;
10235 if (first)
10236 first = false;
10237 else
10238 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10240 /* Possibly capture `this'. */
10241 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10243 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10244 if (cxx_dialect < cxx2a
10245 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10246 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10247 "with by-copy capture default");
10248 cp_lexer_consume_token (parser->lexer);
10249 add_capture (lambda_expr,
10250 /*id=*/this_identifier,
10251 /*initializer=*/finish_this_expr (),
10252 /*by_reference_p=*/true,
10253 explicit_init_p);
10254 continue;
10257 /* Possibly capture `*this'. */
10258 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10259 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10261 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10262 if (cxx_dialect < cxx17)
10263 pedwarn (loc, 0, "%<*this%> capture only available with "
10264 "-std=c++17 or -std=gnu++17");
10265 cp_lexer_consume_token (parser->lexer);
10266 cp_lexer_consume_token (parser->lexer);
10267 add_capture (lambda_expr,
10268 /*id=*/this_identifier,
10269 /*initializer=*/finish_this_expr (),
10270 /*by_reference_p=*/false,
10271 explicit_init_p);
10272 continue;
10275 /* Remember whether we want to capture as a reference or not. */
10276 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10278 capture_kind = BY_REFERENCE;
10279 cp_lexer_consume_token (parser->lexer);
10282 /* Get the identifier. */
10283 capture_token = cp_lexer_peek_token (parser->lexer);
10284 capture_id = cp_parser_identifier (parser);
10286 if (capture_id == error_mark_node)
10287 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10288 delimiters, but I modified this to stop on unnested ']' as well. It
10289 was already changed to stop on unnested '}', so the
10290 "closing_parenthesis" name is no more misleading with my change. */
10292 cp_parser_skip_to_closing_parenthesis (parser,
10293 /*recovering=*/true,
10294 /*or_comma=*/true,
10295 /*consume_paren=*/true);
10296 break;
10299 /* Find the initializer for this capture. */
10300 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10301 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10302 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10304 bool direct, non_constant;
10305 /* An explicit initializer exists. */
10306 if (cxx_dialect < cxx14)
10307 pedwarn (input_location, 0,
10308 "lambda capture initializers "
10309 "only available with -std=c++14 or -std=gnu++14");
10310 capture_init_expr = cp_parser_initializer (parser, &direct,
10311 &non_constant);
10312 explicit_init_p = true;
10313 if (capture_init_expr == NULL_TREE)
10315 error ("empty initializer for lambda init-capture");
10316 capture_init_expr = error_mark_node;
10319 else
10321 const char* error_msg;
10323 /* Turn the identifier into an id-expression. */
10324 capture_init_expr
10325 = cp_parser_lookup_name_simple (parser, capture_id,
10326 capture_token->location);
10328 if (capture_init_expr == error_mark_node)
10330 unqualified_name_lookup_error (capture_id);
10331 continue;
10333 else if (DECL_P (capture_init_expr)
10334 && (!VAR_P (capture_init_expr)
10335 && TREE_CODE (capture_init_expr) != PARM_DECL))
10337 error_at (capture_token->location,
10338 "capture of non-variable %qD ",
10339 capture_init_expr);
10340 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10341 "%q#D declared here", capture_init_expr);
10342 continue;
10344 if (VAR_P (capture_init_expr)
10345 && decl_storage_duration (capture_init_expr) != dk_auto)
10347 if (pedwarn (capture_token->location, 0, "capture of variable "
10348 "%qD with non-automatic storage duration",
10349 capture_init_expr))
10350 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10351 "%q#D declared here", capture_init_expr);
10352 continue;
10355 capture_init_expr
10356 = finish_id_expression
10357 (capture_id,
10358 capture_init_expr,
10359 parser->scope,
10360 &idk,
10361 /*integral_constant_expression_p=*/false,
10362 /*allow_non_integral_constant_expression_p=*/false,
10363 /*non_integral_constant_expression_p=*/NULL,
10364 /*template_p=*/false,
10365 /*done=*/true,
10366 /*address_p=*/false,
10367 /*template_arg_p=*/false,
10368 &error_msg,
10369 capture_token->location);
10371 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10373 cp_lexer_consume_token (parser->lexer);
10374 capture_init_expr = make_pack_expansion (capture_init_expr);
10376 else
10377 check_for_bare_parameter_packs (capture_init_expr);
10380 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10381 && !explicit_init_p)
10383 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10384 && capture_kind == BY_COPY)
10385 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10386 "of %qD redundant with by-copy capture default",
10387 capture_id);
10388 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10389 && capture_kind == BY_REFERENCE)
10390 pedwarn (capture_token->location, 0, "explicit by-reference "
10391 "capture of %qD redundant with by-reference capture "
10392 "default", capture_id);
10395 add_capture (lambda_expr,
10396 capture_id,
10397 capture_init_expr,
10398 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10399 explicit_init_p);
10402 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10405 /* Parse the (optional) middle of a lambda expression.
10407 lambda-declarator:
10408 < template-parameter-list [opt] >
10409 ( parameter-declaration-clause [opt] )
10410 attribute-specifier [opt]
10411 decl-specifier-seq [opt]
10412 exception-specification [opt]
10413 lambda-return-type-clause [opt]
10415 LAMBDA_EXPR is the current representation of the lambda expression. */
10417 static bool
10418 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10420 /* 5.1.1.4 of the standard says:
10421 If a lambda-expression does not include a lambda-declarator, it is as if
10422 the lambda-declarator were ().
10423 This means an empty parameter list, no attributes, and no exception
10424 specification. */
10425 tree param_list = void_list_node;
10426 tree attributes = NULL_TREE;
10427 tree exception_spec = NULL_TREE;
10428 tree template_param_list = NULL_TREE;
10429 tree tx_qual = NULL_TREE;
10430 tree return_type = NULL_TREE;
10431 cp_decl_specifier_seq lambda_specs;
10432 clear_decl_specs (&lambda_specs);
10434 /* The template-parameter-list is optional, but must begin with
10435 an opening angle if present. */
10436 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10438 if (cxx_dialect < cxx14)
10439 pedwarn (parser->lexer->next_token->location, 0,
10440 "lambda templates are only available with "
10441 "-std=c++14 or -std=gnu++14");
10442 else if (cxx_dialect < cxx2a)
10443 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10444 "lambda templates are only available with "
10445 "-std=c++2a or -std=gnu++2a");
10447 cp_lexer_consume_token (parser->lexer);
10449 template_param_list = cp_parser_template_parameter_list (parser);
10451 cp_parser_skip_to_end_of_template_parameter_list (parser);
10453 /* We just processed one more parameter list. */
10454 ++parser->num_template_parameter_lists;
10457 /* The parameter-declaration-clause is optional (unless
10458 template-parameter-list was given), but must begin with an
10459 opening parenthesis if present. */
10460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10462 matching_parens parens;
10463 parens.consume_open (parser);
10465 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10467 /* Parse parameters. */
10468 param_list = cp_parser_parameter_declaration_clause (parser);
10470 /* Default arguments shall not be specified in the
10471 parameter-declaration-clause of a lambda-declarator. */
10472 if (cxx_dialect < cxx14)
10473 for (tree t = param_list; t; t = TREE_CHAIN (t))
10474 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10475 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10476 "default argument specified for lambda parameter");
10478 parens.require_close (parser);
10480 attributes = cp_parser_attributes_opt (parser);
10482 /* In the decl-specifier-seq of the lambda-declarator, each
10483 decl-specifier shall either be mutable or constexpr. */
10484 int declares_class_or_enum;
10485 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10486 cp_parser_decl_specifier_seq (parser,
10487 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10488 &lambda_specs, &declares_class_or_enum);
10489 if (lambda_specs.storage_class == sc_mutable)
10491 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10492 if (lambda_specs.conflicting_specifiers_p)
10493 error_at (lambda_specs.locations[ds_storage_class],
10494 "duplicate %<mutable%>");
10497 tx_qual = cp_parser_tx_qualifier_opt (parser);
10499 /* Parse optional exception specification. */
10500 exception_spec = cp_parser_exception_specification_opt (parser);
10502 /* Parse optional trailing return type. */
10503 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10505 cp_lexer_consume_token (parser->lexer);
10506 return_type = cp_parser_trailing_type_id (parser);
10509 /* The function parameters must be in scope all the way until after the
10510 trailing-return-type in case of decltype. */
10511 pop_bindings_and_leave_scope ();
10513 else if (template_param_list != NULL_TREE) // generate diagnostic
10514 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10516 /* Create the function call operator.
10518 Messing with declarators like this is no uglier than building up the
10519 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10520 other code. */
10522 cp_decl_specifier_seq return_type_specs;
10523 cp_declarator* declarator;
10524 tree fco;
10525 int quals;
10526 void *p;
10528 clear_decl_specs (&return_type_specs);
10529 if (return_type)
10530 return_type_specs.type = return_type;
10531 else
10532 /* Maybe we will deduce the return type later. */
10533 return_type_specs.type = make_auto ();
10535 if (lambda_specs.locations[ds_constexpr])
10537 if (cxx_dialect >= cxx17)
10538 return_type_specs.locations[ds_constexpr]
10539 = lambda_specs.locations[ds_constexpr];
10540 else
10541 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10542 "lambda only available with -std=c++17 or -std=gnu++17");
10545 p = obstack_alloc (&declarator_obstack, 0);
10547 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10549 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10550 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10551 declarator = make_call_declarator (declarator, param_list, quals,
10552 VIRT_SPEC_UNSPECIFIED,
10553 REF_QUAL_NONE,
10554 tx_qual,
10555 exception_spec,
10556 /*late_return_type=*/NULL_TREE,
10557 /*requires_clause*/NULL_TREE);
10558 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10560 fco = grokmethod (&return_type_specs,
10561 declarator,
10562 attributes);
10563 if (fco != error_mark_node)
10565 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10566 DECL_ARTIFICIAL (fco) = 1;
10567 /* Give the object parameter a different name. */
10568 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10569 if (return_type)
10570 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10572 if (template_param_list)
10574 fco = finish_member_template_decl (fco);
10575 finish_template_decl (template_param_list);
10576 --parser->num_template_parameter_lists;
10578 else if (parser->fully_implicit_function_template_p)
10579 fco = finish_fully_implicit_template (parser, fco);
10581 finish_member_declaration (fco);
10583 obstack_free (&declarator_obstack, p);
10585 return (fco != error_mark_node);
10589 /* Parse the body of a lambda expression, which is simply
10591 compound-statement
10593 but which requires special handling.
10594 LAMBDA_EXPR is the current representation of the lambda expression. */
10596 static void
10597 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10599 bool nested = (current_function_decl != NULL_TREE);
10600 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10601 bool in_function_body = parser->in_function_body;
10603 if (nested)
10604 push_function_context ();
10605 else
10606 /* Still increment function_depth so that we don't GC in the
10607 middle of an expression. */
10608 ++function_depth;
10610 vec<tree> omp_privatization_save;
10611 save_omp_privatization_clauses (omp_privatization_save);
10612 /* Clear this in case we're in the middle of a default argument. */
10613 parser->local_variables_forbidden_p = false;
10614 parser->in_function_body = true;
10617 local_specialization_stack s (lss_copy);
10618 tree fco = lambda_function (lambda_expr);
10619 tree body = start_lambda_function (fco, lambda_expr);
10620 matching_braces braces;
10622 if (braces.require_open (parser))
10624 tree compound_stmt = begin_compound_stmt (0);
10626 /* Originally C++11 required us to peek for 'return expr'; and
10627 process it specially here to deduce the return type. N3638
10628 removed the need for that. */
10630 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10631 cp_parser_label_declaration (parser);
10632 cp_parser_statement_seq_opt (parser, NULL_TREE);
10633 braces.require_close (parser);
10635 finish_compound_stmt (compound_stmt);
10638 finish_lambda_function (body);
10641 restore_omp_privatization_clauses (omp_privatization_save);
10642 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10643 parser->in_function_body = in_function_body;
10644 if (nested)
10645 pop_function_context();
10646 else
10647 --function_depth;
10650 /* Statements [gram.stmt.stmt] */
10652 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10654 static void
10655 add_debug_begin_stmt (location_t loc)
10657 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10658 return;
10660 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10661 SET_EXPR_LOCATION (stmt, loc);
10662 add_stmt (stmt);
10665 /* Parse a statement.
10667 statement:
10668 labeled-statement
10669 expression-statement
10670 compound-statement
10671 selection-statement
10672 iteration-statement
10673 jump-statement
10674 declaration-statement
10675 try-block
10677 C++11:
10679 statement:
10680 labeled-statement
10681 attribute-specifier-seq (opt) expression-statement
10682 attribute-specifier-seq (opt) compound-statement
10683 attribute-specifier-seq (opt) selection-statement
10684 attribute-specifier-seq (opt) iteration-statement
10685 attribute-specifier-seq (opt) jump-statement
10686 declaration-statement
10687 attribute-specifier-seq (opt) try-block
10689 init-statement:
10690 expression-statement
10691 simple-declaration
10693 TM Extension:
10695 statement:
10696 atomic-statement
10698 IN_COMPOUND is true when the statement is nested inside a
10699 cp_parser_compound_statement; this matters for certain pragmas.
10701 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10702 is a (possibly labeled) if statement which is not enclosed in braces
10703 and has an else clause. This is used to implement -Wparentheses.
10705 CHAIN is a vector of if-else-if conditions. */
10707 static void
10708 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10709 bool in_compound, bool *if_p, vec<tree> *chain,
10710 location_t *loc_after_labels)
10712 tree statement, std_attrs = NULL_TREE;
10713 cp_token *token;
10714 location_t statement_location, attrs_location;
10716 restart:
10717 if (if_p != NULL)
10718 *if_p = false;
10719 /* There is no statement yet. */
10720 statement = NULL_TREE;
10722 saved_token_sentinel saved_tokens (parser->lexer);
10723 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10724 if (c_dialect_objc ())
10725 /* In obj-c++, seeing '[[' might be the either the beginning of
10726 c++11 attributes, or a nested objc-message-expression. So
10727 let's parse the c++11 attributes tentatively. */
10728 cp_parser_parse_tentatively (parser);
10729 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10730 if (c_dialect_objc ())
10732 if (!cp_parser_parse_definitely (parser))
10733 std_attrs = NULL_TREE;
10736 /* Peek at the next token. */
10737 token = cp_lexer_peek_token (parser->lexer);
10738 /* Remember the location of the first token in the statement. */
10739 statement_location = token->location;
10740 add_debug_begin_stmt (statement_location);
10741 /* If this is a keyword, then that will often determine what kind of
10742 statement we have. */
10743 if (token->type == CPP_KEYWORD)
10745 enum rid keyword = token->keyword;
10747 switch (keyword)
10749 case RID_CASE:
10750 case RID_DEFAULT:
10751 /* Looks like a labeled-statement with a case label.
10752 Parse the label, and then use tail recursion to parse
10753 the statement. */
10754 cp_parser_label_for_labeled_statement (parser, std_attrs);
10755 in_compound = false;
10756 goto restart;
10758 case RID_IF:
10759 case RID_SWITCH:
10760 statement = cp_parser_selection_statement (parser, if_p, chain);
10761 break;
10763 case RID_WHILE:
10764 case RID_DO:
10765 case RID_FOR:
10766 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10767 break;
10769 case RID_BREAK:
10770 case RID_CONTINUE:
10771 case RID_RETURN:
10772 case RID_GOTO:
10773 statement = cp_parser_jump_statement (parser);
10774 break;
10776 /* Objective-C++ exception-handling constructs. */
10777 case RID_AT_TRY:
10778 case RID_AT_CATCH:
10779 case RID_AT_FINALLY:
10780 case RID_AT_SYNCHRONIZED:
10781 case RID_AT_THROW:
10782 statement = cp_parser_objc_statement (parser);
10783 break;
10785 case RID_TRY:
10786 statement = cp_parser_try_block (parser);
10787 break;
10789 case RID_NAMESPACE:
10790 /* This must be a namespace alias definition. */
10791 cp_parser_declaration_statement (parser);
10792 return;
10794 case RID_TRANSACTION_ATOMIC:
10795 case RID_TRANSACTION_RELAXED:
10796 case RID_SYNCHRONIZED:
10797 case RID_ATOMIC_NOEXCEPT:
10798 case RID_ATOMIC_CANCEL:
10799 statement = cp_parser_transaction (parser, token);
10800 break;
10801 case RID_TRANSACTION_CANCEL:
10802 statement = cp_parser_transaction_cancel (parser);
10803 break;
10805 default:
10806 /* It might be a keyword like `int' that can start a
10807 declaration-statement. */
10808 break;
10811 else if (token->type == CPP_NAME)
10813 /* If the next token is a `:', then we are looking at a
10814 labeled-statement. */
10815 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10816 if (token->type == CPP_COLON)
10818 /* Looks like a labeled-statement with an ordinary label.
10819 Parse the label, and then use tail recursion to parse
10820 the statement. */
10822 cp_parser_label_for_labeled_statement (parser, std_attrs);
10823 in_compound = false;
10824 goto restart;
10827 /* Anything that starts with a `{' must be a compound-statement. */
10828 else if (token->type == CPP_OPEN_BRACE)
10829 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10830 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10831 a statement all its own. */
10832 else if (token->type == CPP_PRAGMA)
10834 /* Only certain OpenMP pragmas are attached to statements, and thus
10835 are considered statements themselves. All others are not. In
10836 the context of a compound, accept the pragma as a "statement" and
10837 return so that we can check for a close brace. Otherwise we
10838 require a real statement and must go back and read one. */
10839 if (in_compound)
10840 cp_parser_pragma (parser, pragma_compound, if_p);
10841 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10842 goto restart;
10843 return;
10845 else if (token->type == CPP_EOF)
10847 cp_parser_error (parser, "expected statement");
10848 return;
10851 /* Everything else must be a declaration-statement or an
10852 expression-statement. Try for the declaration-statement
10853 first, unless we are looking at a `;', in which case we know that
10854 we have an expression-statement. */
10855 if (!statement)
10857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10859 if (std_attrs != NULL_TREE)
10861 /* Attributes should be parsed as part of the the
10862 declaration, so let's un-parse them. */
10863 saved_tokens.rollback();
10864 std_attrs = NULL_TREE;
10867 cp_parser_parse_tentatively (parser);
10868 /* Try to parse the declaration-statement. */
10869 cp_parser_declaration_statement (parser);
10870 /* If that worked, we're done. */
10871 if (cp_parser_parse_definitely (parser))
10872 return;
10874 /* All preceding labels have been parsed at this point. */
10875 if (loc_after_labels != NULL)
10876 *loc_after_labels = statement_location;
10878 /* Look for an expression-statement instead. */
10879 statement = cp_parser_expression_statement (parser, in_statement_expr);
10881 /* Handle [[fallthrough]];. */
10882 if (attribute_fallthrough_p (std_attrs))
10884 /* The next token after the fallthrough attribute is ';'. */
10885 if (statement == NULL_TREE)
10887 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10888 statement = build_call_expr_internal_loc (statement_location,
10889 IFN_FALLTHROUGH,
10890 void_type_node, 0);
10891 finish_expr_stmt (statement);
10893 else
10894 warning_at (statement_location, OPT_Wattributes,
10895 "%<fallthrough%> attribute not followed by %<;%>");
10896 std_attrs = NULL_TREE;
10900 /* Set the line number for the statement. */
10901 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10902 SET_EXPR_LOCATION (statement, statement_location);
10904 /* Allow "[[fallthrough]];", but warn otherwise. */
10905 if (std_attrs != NULL_TREE)
10906 warning_at (attrs_location,
10907 OPT_Wattributes,
10908 "attributes at the beginning of statement are ignored");
10911 /* Append ATTR to attribute list ATTRS. */
10913 static tree
10914 attr_chainon (tree attrs, tree attr)
10916 if (attrs == error_mark_node)
10917 return error_mark_node;
10918 if (attr == error_mark_node)
10919 return error_mark_node;
10920 return chainon (attrs, attr);
10923 /* Parse the label for a labeled-statement, i.e.
10925 identifier :
10926 case constant-expression :
10927 default :
10929 GNU Extension:
10930 case constant-expression ... constant-expression : statement
10932 When a label is parsed without errors, the label is added to the
10933 parse tree by the finish_* functions, so this function doesn't
10934 have to return the label. */
10936 static void
10937 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10939 cp_token *token;
10940 tree label = NULL_TREE;
10941 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10943 /* The next token should be an identifier. */
10944 token = cp_lexer_peek_token (parser->lexer);
10945 if (token->type != CPP_NAME
10946 && token->type != CPP_KEYWORD)
10948 cp_parser_error (parser, "expected labeled-statement");
10949 return;
10952 /* Remember whether this case or a user-defined label is allowed to fall
10953 through to. */
10954 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10956 parser->colon_corrects_to_scope_p = false;
10957 switch (token->keyword)
10959 case RID_CASE:
10961 tree expr, expr_hi;
10962 cp_token *ellipsis;
10964 /* Consume the `case' token. */
10965 cp_lexer_consume_token (parser->lexer);
10966 /* Parse the constant-expression. */
10967 expr = cp_parser_constant_expression (parser);
10968 if (check_for_bare_parameter_packs (expr))
10969 expr = error_mark_node;
10971 ellipsis = cp_lexer_peek_token (parser->lexer);
10972 if (ellipsis->type == CPP_ELLIPSIS)
10974 /* Consume the `...' token. */
10975 cp_lexer_consume_token (parser->lexer);
10976 expr_hi = cp_parser_constant_expression (parser);
10977 if (check_for_bare_parameter_packs (expr_hi))
10978 expr_hi = error_mark_node;
10980 /* We don't need to emit warnings here, as the common code
10981 will do this for us. */
10983 else
10984 expr_hi = NULL_TREE;
10986 if (parser->in_switch_statement_p)
10988 tree l = finish_case_label (token->location, expr, expr_hi);
10989 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10990 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10992 else
10993 error_at (token->location,
10994 "case label %qE not within a switch statement",
10995 expr);
10997 break;
10999 case RID_DEFAULT:
11000 /* Consume the `default' token. */
11001 cp_lexer_consume_token (parser->lexer);
11003 if (parser->in_switch_statement_p)
11005 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11006 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11007 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11009 else
11010 error_at (token->location, "case label not within a switch statement");
11011 break;
11013 default:
11014 /* Anything else must be an ordinary label. */
11015 label = finish_label_stmt (cp_parser_identifier (parser));
11016 if (label && TREE_CODE (label) == LABEL_DECL)
11017 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11018 break;
11021 /* Require the `:' token. */
11022 cp_parser_require (parser, CPP_COLON, RT_COLON);
11024 /* An ordinary label may optionally be followed by attributes.
11025 However, this is only permitted if the attributes are then
11026 followed by a semicolon. This is because, for backward
11027 compatibility, when parsing
11028 lab: __attribute__ ((unused)) int i;
11029 we want the attribute to attach to "i", not "lab". */
11030 if (label != NULL_TREE
11031 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11033 tree attrs;
11034 cp_parser_parse_tentatively (parser);
11035 attrs = cp_parser_gnu_attributes_opt (parser);
11036 if (attrs == NULL_TREE
11037 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11038 cp_parser_abort_tentative_parse (parser);
11039 else if (!cp_parser_parse_definitely (parser))
11041 else
11042 attributes = attr_chainon (attributes, attrs);
11045 if (attributes != NULL_TREE)
11046 cplus_decl_attributes (&label, attributes, 0);
11048 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11051 /* Parse an expression-statement.
11053 expression-statement:
11054 expression [opt] ;
11056 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11057 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11058 indicates whether this expression-statement is part of an
11059 expression statement. */
11061 static tree
11062 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11064 tree statement = NULL_TREE;
11065 cp_token *token = cp_lexer_peek_token (parser->lexer);
11066 location_t loc = token->location;
11068 /* There might be attribute fallthrough. */
11069 tree attr = cp_parser_gnu_attributes_opt (parser);
11071 /* If the next token is a ';', then there is no expression
11072 statement. */
11073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11075 statement = cp_parser_expression (parser);
11076 if (statement == error_mark_node
11077 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11079 cp_parser_skip_to_end_of_block_or_statement (parser);
11080 return error_mark_node;
11084 /* Handle [[fallthrough]];. */
11085 if (attribute_fallthrough_p (attr))
11087 /* The next token after the fallthrough attribute is ';'. */
11088 if (statement == NULL_TREE)
11089 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11090 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11091 void_type_node, 0);
11092 else
11093 warning_at (loc, OPT_Wattributes,
11094 "%<fallthrough%> attribute not followed by %<;%>");
11095 attr = NULL_TREE;
11098 /* Allow "[[fallthrough]];", but warn otherwise. */
11099 if (attr != NULL_TREE)
11100 warning_at (loc, OPT_Wattributes,
11101 "attributes at the beginning of statement are ignored");
11103 /* Give a helpful message for "A<T>::type t;" and the like. */
11104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11105 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11107 if (TREE_CODE (statement) == SCOPE_REF)
11108 error_at (token->location, "need %<typename%> before %qE because "
11109 "%qT is a dependent scope",
11110 statement, TREE_OPERAND (statement, 0));
11111 else if (is_overloaded_fn (statement)
11112 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11114 /* A::A a; */
11115 tree fn = get_first_fn (statement);
11116 error_at (token->location,
11117 "%<%T::%D%> names the constructor, not the type",
11118 DECL_CONTEXT (fn), DECL_NAME (fn));
11122 /* Consume the final `;'. */
11123 cp_parser_consume_semicolon_at_end_of_statement (parser);
11125 if (in_statement_expr
11126 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11127 /* This is the final expression statement of a statement
11128 expression. */
11129 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11130 else if (statement)
11131 statement = finish_expr_stmt (statement);
11133 return statement;
11136 /* Parse a compound-statement.
11138 compound-statement:
11139 { statement-seq [opt] }
11141 GNU extension:
11143 compound-statement:
11144 { label-declaration-seq [opt] statement-seq [opt] }
11146 label-declaration-seq:
11147 label-declaration
11148 label-declaration-seq label-declaration
11150 Returns a tree representing the statement. */
11152 static tree
11153 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11154 int bcs_flags, bool function_body)
11156 tree compound_stmt;
11157 matching_braces braces;
11159 /* Consume the `{'. */
11160 if (!braces.require_open (parser))
11161 return error_mark_node;
11162 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11163 && !function_body && cxx_dialect < cxx14)
11164 pedwarn (input_location, OPT_Wpedantic,
11165 "compound-statement in %<constexpr%> function");
11166 /* Begin the compound-statement. */
11167 compound_stmt = begin_compound_stmt (bcs_flags);
11168 /* If the next keyword is `__label__' we have a label declaration. */
11169 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11170 cp_parser_label_declaration (parser);
11171 /* Parse an (optional) statement-seq. */
11172 cp_parser_statement_seq_opt (parser, in_statement_expr);
11173 /* Finish the compound-statement. */
11174 finish_compound_stmt (compound_stmt);
11175 /* Consume the `}'. */
11176 braces.require_close (parser);
11178 return compound_stmt;
11181 /* Parse an (optional) statement-seq.
11183 statement-seq:
11184 statement
11185 statement-seq [opt] statement */
11187 static void
11188 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11190 /* Scan statements until there aren't any more. */
11191 while (true)
11193 cp_token *token = cp_lexer_peek_token (parser->lexer);
11195 /* If we are looking at a `}', then we have run out of
11196 statements; the same is true if we have reached the end
11197 of file, or have stumbled upon a stray '@end'. */
11198 if (token->type == CPP_CLOSE_BRACE
11199 || token->type == CPP_EOF
11200 || token->type == CPP_PRAGMA_EOL
11201 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11202 break;
11204 /* If we are in a compound statement and find 'else' then
11205 something went wrong. */
11206 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11208 if (parser->in_statement & IN_IF_STMT)
11209 break;
11210 else
11212 token = cp_lexer_consume_token (parser->lexer);
11213 error_at (token->location, "%<else%> without a previous %<if%>");
11217 /* Parse the statement. */
11218 cp_parser_statement (parser, in_statement_expr, true, NULL);
11222 /* Return true if we're looking at (init; cond), false otherwise. */
11224 static bool
11225 cp_parser_init_statement_p (cp_parser *parser)
11227 /* Save tokens so that we can put them back. */
11228 cp_lexer_save_tokens (parser->lexer);
11230 /* Look for ';' that is not nested in () or {}. */
11231 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11232 /*recovering=*/false,
11233 CPP_SEMICOLON,
11234 /*consume_paren=*/false);
11236 /* Roll back the tokens we skipped. */
11237 cp_lexer_rollback_tokens (parser->lexer);
11239 return ret == -1;
11242 /* Parse a selection-statement.
11244 selection-statement:
11245 if ( init-statement [opt] condition ) statement
11246 if ( init-statement [opt] condition ) statement else statement
11247 switch ( init-statement [opt] condition ) statement
11249 Returns the new IF_STMT or SWITCH_STMT.
11251 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11252 is a (possibly labeled) if statement which is not enclosed in
11253 braces and has an else clause. This is used to implement
11254 -Wparentheses.
11256 CHAIN is a vector of if-else-if conditions. This is used to implement
11257 -Wduplicated-cond. */
11259 static tree
11260 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11261 vec<tree> *chain)
11263 cp_token *token;
11264 enum rid keyword;
11265 token_indent_info guard_tinfo;
11267 if (if_p != NULL)
11268 *if_p = false;
11270 /* Peek at the next token. */
11271 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11272 guard_tinfo = get_token_indent_info (token);
11274 /* See what kind of keyword it is. */
11275 keyword = token->keyword;
11276 switch (keyword)
11278 case RID_IF:
11279 case RID_SWITCH:
11281 tree statement;
11282 tree condition;
11284 bool cx = false;
11285 if (keyword == RID_IF
11286 && cp_lexer_next_token_is_keyword (parser->lexer,
11287 RID_CONSTEXPR))
11289 cx = true;
11290 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11291 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11292 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11293 "with -std=c++17 or -std=gnu++17");
11296 /* Look for the `('. */
11297 matching_parens parens;
11298 if (!parens.require_open (parser))
11300 cp_parser_skip_to_end_of_statement (parser);
11301 return error_mark_node;
11304 /* Begin the selection-statement. */
11305 if (keyword == RID_IF)
11307 statement = begin_if_stmt ();
11308 IF_STMT_CONSTEXPR_P (statement) = cx;
11310 else
11311 statement = begin_switch_stmt ();
11313 /* Parse the optional init-statement. */
11314 if (cp_parser_init_statement_p (parser))
11316 tree decl;
11317 if (cxx_dialect < cxx17)
11318 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11319 "init-statement in selection statements only available "
11320 "with -std=c++17 or -std=gnu++17");
11321 cp_parser_init_statement (parser, &decl);
11324 /* Parse the condition. */
11325 condition = cp_parser_condition (parser);
11326 /* Look for the `)'. */
11327 if (!parens.require_close (parser))
11328 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11329 /*consume_paren=*/true);
11331 if (keyword == RID_IF)
11333 bool nested_if;
11334 unsigned char in_statement;
11336 /* Add the condition. */
11337 condition = finish_if_stmt_cond (condition, statement);
11339 if (warn_duplicated_cond)
11340 warn_duplicated_cond_add_or_warn (token->location, condition,
11341 &chain);
11343 /* Parse the then-clause. */
11344 in_statement = parser->in_statement;
11345 parser->in_statement |= IN_IF_STMT;
11347 /* Outside a template, the non-selected branch of a constexpr
11348 if is a 'discarded statement', i.e. unevaluated. */
11349 bool was_discarded = in_discarded_stmt;
11350 bool discard_then = (cx && !processing_template_decl
11351 && integer_zerop (condition));
11352 if (discard_then)
11354 in_discarded_stmt = true;
11355 ++c_inhibit_evaluation_warnings;
11358 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11359 guard_tinfo);
11361 parser->in_statement = in_statement;
11363 finish_then_clause (statement);
11365 if (discard_then)
11367 THEN_CLAUSE (statement) = NULL_TREE;
11368 in_discarded_stmt = was_discarded;
11369 --c_inhibit_evaluation_warnings;
11372 /* If the next token is `else', parse the else-clause. */
11373 if (cp_lexer_next_token_is_keyword (parser->lexer,
11374 RID_ELSE))
11376 bool discard_else = (cx && !processing_template_decl
11377 && integer_nonzerop (condition));
11378 if (discard_else)
11380 in_discarded_stmt = true;
11381 ++c_inhibit_evaluation_warnings;
11384 guard_tinfo
11385 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11386 /* Consume the `else' keyword. */
11387 cp_lexer_consume_token (parser->lexer);
11388 if (warn_duplicated_cond)
11390 if (cp_lexer_next_token_is_keyword (parser->lexer,
11391 RID_IF)
11392 && chain == NULL)
11394 /* We've got "if (COND) else if (COND2)". Start
11395 the condition chain and add COND as the first
11396 element. */
11397 chain = new vec<tree> ();
11398 if (!CONSTANT_CLASS_P (condition)
11399 && !TREE_SIDE_EFFECTS (condition))
11401 /* Wrap it in a NOP_EXPR so that we can set the
11402 location of the condition. */
11403 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11404 condition);
11405 SET_EXPR_LOCATION (e, token->location);
11406 chain->safe_push (e);
11409 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11410 RID_IF))
11412 /* This is if-else without subsequent if. Zap the
11413 condition chain; we would have already warned at
11414 this point. */
11415 delete chain;
11416 chain = NULL;
11419 begin_else_clause (statement);
11420 /* Parse the else-clause. */
11421 cp_parser_implicitly_scoped_statement (parser, NULL,
11422 guard_tinfo, chain);
11424 finish_else_clause (statement);
11426 /* If we are currently parsing a then-clause, then
11427 IF_P will not be NULL. We set it to true to
11428 indicate that this if statement has an else clause.
11429 This may trigger the Wparentheses warning below
11430 when we get back up to the parent if statement. */
11431 if (if_p != NULL)
11432 *if_p = true;
11434 if (discard_else)
11436 ELSE_CLAUSE (statement) = NULL_TREE;
11437 in_discarded_stmt = was_discarded;
11438 --c_inhibit_evaluation_warnings;
11441 else
11443 /* This if statement does not have an else clause. If
11444 NESTED_IF is true, then the then-clause has an if
11445 statement which does have an else clause. We warn
11446 about the potential ambiguity. */
11447 if (nested_if)
11448 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11449 "suggest explicit braces to avoid ambiguous"
11450 " %<else%>");
11451 if (warn_duplicated_cond)
11453 /* We don't need the condition chain anymore. */
11454 delete chain;
11455 chain = NULL;
11459 /* Now we're all done with the if-statement. */
11460 finish_if_stmt (statement);
11462 else
11464 bool in_switch_statement_p;
11465 unsigned char in_statement;
11467 /* Add the condition. */
11468 finish_switch_cond (condition, statement);
11470 /* Parse the body of the switch-statement. */
11471 in_switch_statement_p = parser->in_switch_statement_p;
11472 in_statement = parser->in_statement;
11473 parser->in_switch_statement_p = true;
11474 parser->in_statement |= IN_SWITCH_STMT;
11475 cp_parser_implicitly_scoped_statement (parser, if_p,
11476 guard_tinfo);
11477 parser->in_switch_statement_p = in_switch_statement_p;
11478 parser->in_statement = in_statement;
11480 /* Now we're all done with the switch-statement. */
11481 finish_switch_stmt (statement);
11484 return statement;
11486 break;
11488 default:
11489 cp_parser_error (parser, "expected selection-statement");
11490 return error_mark_node;
11494 /* Parse a condition.
11496 condition:
11497 expression
11498 type-specifier-seq declarator = initializer-clause
11499 type-specifier-seq declarator braced-init-list
11501 GNU Extension:
11503 condition:
11504 type-specifier-seq declarator asm-specification [opt]
11505 attributes [opt] = assignment-expression
11507 Returns the expression that should be tested. */
11509 static tree
11510 cp_parser_condition (cp_parser* parser)
11512 cp_decl_specifier_seq type_specifiers;
11513 const char *saved_message;
11514 int declares_class_or_enum;
11516 /* Try the declaration first. */
11517 cp_parser_parse_tentatively (parser);
11518 /* New types are not allowed in the type-specifier-seq for a
11519 condition. */
11520 saved_message = parser->type_definition_forbidden_message;
11521 parser->type_definition_forbidden_message
11522 = G_("types may not be defined in conditions");
11523 /* Parse the type-specifier-seq. */
11524 cp_parser_decl_specifier_seq (parser,
11525 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11526 &type_specifiers,
11527 &declares_class_or_enum);
11528 /* Restore the saved message. */
11529 parser->type_definition_forbidden_message = saved_message;
11530 /* If all is well, we might be looking at a declaration. */
11531 if (!cp_parser_error_occurred (parser))
11533 tree decl;
11534 tree asm_specification;
11535 tree attributes;
11536 cp_declarator *declarator;
11537 tree initializer = NULL_TREE;
11539 /* Parse the declarator. */
11540 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11541 /*ctor_dtor_or_conv_p=*/NULL,
11542 /*parenthesized_p=*/NULL,
11543 /*member_p=*/false,
11544 /*friend_p=*/false);
11545 /* Parse the attributes. */
11546 attributes = cp_parser_attributes_opt (parser);
11547 /* Parse the asm-specification. */
11548 asm_specification = cp_parser_asm_specification_opt (parser);
11549 /* If the next token is not an `=' or '{', then we might still be
11550 looking at an expression. For example:
11552 if (A(a).x)
11554 looks like a decl-specifier-seq and a declarator -- but then
11555 there is no `=', so this is an expression. */
11556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11557 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11558 cp_parser_simulate_error (parser);
11560 /* If we did see an `=' or '{', then we are looking at a declaration
11561 for sure. */
11562 if (cp_parser_parse_definitely (parser))
11564 tree pushed_scope;
11565 bool non_constant_p;
11566 int flags = LOOKUP_ONLYCONVERTING;
11568 /* Create the declaration. */
11569 decl = start_decl (declarator, &type_specifiers,
11570 /*initialized_p=*/true,
11571 attributes, /*prefix_attributes=*/NULL_TREE,
11572 &pushed_scope);
11574 /* Parse the initializer. */
11575 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11577 initializer = cp_parser_braced_list (parser, &non_constant_p);
11578 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11579 flags = 0;
11581 else
11583 /* Consume the `='. */
11584 cp_parser_require (parser, CPP_EQ, RT_EQ);
11585 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11587 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11588 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11590 /* Process the initializer. */
11591 cp_finish_decl (decl,
11592 initializer, !non_constant_p,
11593 asm_specification,
11594 flags);
11596 if (pushed_scope)
11597 pop_scope (pushed_scope);
11599 return convert_from_reference (decl);
11602 /* If we didn't even get past the declarator successfully, we are
11603 definitely not looking at a declaration. */
11604 else
11605 cp_parser_abort_tentative_parse (parser);
11607 /* Otherwise, we are looking at an expression. */
11608 return cp_parser_expression (parser);
11611 /* Parses a for-statement or range-for-statement until the closing ')',
11612 not included. */
11614 static tree
11615 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11617 tree init, scope, decl;
11618 bool is_range_for;
11620 /* Begin the for-statement. */
11621 scope = begin_for_scope (&init);
11623 /* Parse the initialization. */
11624 is_range_for = cp_parser_init_statement (parser, &decl);
11626 if (is_range_for)
11627 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11628 else
11629 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11632 static tree
11633 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11634 unsigned short unroll)
11636 /* Normal for loop */
11637 tree condition = NULL_TREE;
11638 tree expression = NULL_TREE;
11639 tree stmt;
11641 stmt = begin_for_stmt (scope, init);
11642 /* The init-statement has already been parsed in
11643 cp_parser_init_statement, so no work is needed here. */
11644 finish_init_stmt (stmt);
11646 /* If there's a condition, process it. */
11647 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11648 condition = cp_parser_condition (parser);
11649 else if (ivdep)
11651 cp_parser_error (parser, "missing loop condition in loop with "
11652 "%<GCC ivdep%> pragma");
11653 condition = error_mark_node;
11655 else if (unroll)
11657 cp_parser_error (parser, "missing loop condition in loop with "
11658 "%<GCC unroll%> pragma");
11659 condition = error_mark_node;
11661 finish_for_cond (condition, stmt, ivdep, unroll);
11662 /* Look for the `;'. */
11663 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11665 /* If there's an expression, process it. */
11666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11667 expression = cp_parser_expression (parser);
11668 finish_for_expr (expression, stmt);
11670 return stmt;
11673 /* Tries to parse a range-based for-statement:
11675 range-based-for:
11676 decl-specifier-seq declarator : expression
11678 The decl-specifier-seq declarator and the `:' are already parsed by
11679 cp_parser_init_statement. If processing_template_decl it returns a
11680 newly created RANGE_FOR_STMT; if not, it is converted to a
11681 regular FOR_STMT. */
11683 static tree
11684 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11685 bool ivdep, unsigned short unroll)
11687 tree stmt, range_expr;
11688 auto_vec <cxx_binding *, 16> bindings;
11689 auto_vec <tree, 16> names;
11690 tree decomp_first_name = NULL_TREE;
11691 unsigned int decomp_cnt = 0;
11693 /* Get the range declaration momentarily out of the way so that
11694 the range expression doesn't clash with it. */
11695 if (range_decl != error_mark_node)
11697 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11699 tree v = DECL_VALUE_EXPR (range_decl);
11700 /* For decomposition declaration get all of the corresponding
11701 declarations out of the way. */
11702 if (TREE_CODE (v) == ARRAY_REF
11703 && VAR_P (TREE_OPERAND (v, 0))
11704 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11706 tree d = range_decl;
11707 range_decl = TREE_OPERAND (v, 0);
11708 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11709 decomp_first_name = d;
11710 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11712 tree name = DECL_NAME (d);
11713 names.safe_push (name);
11714 bindings.safe_push (IDENTIFIER_BINDING (name));
11715 IDENTIFIER_BINDING (name)
11716 = IDENTIFIER_BINDING (name)->previous;
11720 if (names.is_empty ())
11722 tree name = DECL_NAME (range_decl);
11723 names.safe_push (name);
11724 bindings.safe_push (IDENTIFIER_BINDING (name));
11725 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11731 bool expr_non_constant_p;
11732 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11734 else
11735 range_expr = cp_parser_expression (parser);
11737 /* Put the range declaration(s) back into scope. */
11738 for (unsigned int i = 0; i < names.length (); i++)
11740 cxx_binding *binding = bindings[i];
11741 binding->previous = IDENTIFIER_BINDING (names[i]);
11742 IDENTIFIER_BINDING (names[i]) = binding;
11745 /* If in template, STMT is converted to a normal for-statement
11746 at instantiation. If not, it is done just ahead. */
11747 if (processing_template_decl)
11749 if (check_for_bare_parameter_packs (range_expr))
11750 range_expr = error_mark_node;
11751 stmt = begin_range_for_stmt (scope, init);
11752 if (ivdep)
11753 RANGE_FOR_IVDEP (stmt) = 1;
11754 if (unroll)
11755 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11756 finish_range_for_decl (stmt, range_decl, range_expr);
11757 if (!type_dependent_expression_p (range_expr)
11758 /* do_auto_deduction doesn't mess with template init-lists. */
11759 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11760 do_range_for_auto_deduction (range_decl, range_expr);
11762 else
11764 stmt = begin_for_stmt (scope, init);
11765 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11766 decomp_first_name, decomp_cnt, ivdep,
11767 unroll);
11769 return stmt;
11772 /* Subroutine of cp_convert_range_for: given the initializer expression,
11773 builds up the range temporary. */
11775 static tree
11776 build_range_temp (tree range_expr)
11778 tree range_type, range_temp;
11780 /* Find out the type deduced by the declaration
11781 `auto &&__range = range_expr'. */
11782 range_type = cp_build_reference_type (make_auto (), true);
11783 range_type = do_auto_deduction (range_type, range_expr,
11784 type_uses_auto (range_type));
11786 /* Create the __range variable. */
11787 range_temp = build_decl (input_location, VAR_DECL,
11788 get_identifier ("__for_range"), range_type);
11789 TREE_USED (range_temp) = 1;
11790 DECL_ARTIFICIAL (range_temp) = 1;
11792 return range_temp;
11795 /* Used by cp_parser_range_for in template context: we aren't going to
11796 do a full conversion yet, but we still need to resolve auto in the
11797 type of the for-range-declaration if present. This is basically
11798 a shortcut version of cp_convert_range_for. */
11800 static void
11801 do_range_for_auto_deduction (tree decl, tree range_expr)
11803 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11804 if (auto_node)
11806 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11807 range_temp = convert_from_reference (build_range_temp (range_expr));
11808 iter_type = (cp_parser_perform_range_for_lookup
11809 (range_temp, &begin_dummy, &end_dummy));
11810 if (iter_type)
11812 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11813 iter_type);
11814 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11815 RO_UNARY_STAR,
11816 tf_warning_or_error);
11817 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11818 iter_decl, auto_node);
11823 /* Converts a range-based for-statement into a normal
11824 for-statement, as per the definition.
11826 for (RANGE_DECL : RANGE_EXPR)
11827 BLOCK
11829 should be equivalent to:
11832 auto &&__range = RANGE_EXPR;
11833 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11834 __begin != __end;
11835 ++__begin)
11837 RANGE_DECL = *__begin;
11838 BLOCK
11842 If RANGE_EXPR is an array:
11843 BEGIN_EXPR = __range
11844 END_EXPR = __range + ARRAY_SIZE(__range)
11845 Else if RANGE_EXPR has a member 'begin' or 'end':
11846 BEGIN_EXPR = __range.begin()
11847 END_EXPR = __range.end()
11848 Else:
11849 BEGIN_EXPR = begin(__range)
11850 END_EXPR = end(__range);
11852 If __range has a member 'begin' but not 'end', or vice versa, we must
11853 still use the second alternative (it will surely fail, however).
11854 When calling begin()/end() in the third alternative we must use
11855 argument dependent lookup, but always considering 'std' as an associated
11856 namespace. */
11858 tree
11859 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11860 tree decomp_first_name, unsigned int decomp_cnt,
11861 bool ivdep, unsigned short unroll)
11863 tree begin, end;
11864 tree iter_type, begin_expr, end_expr;
11865 tree condition, expression;
11867 range_expr = mark_lvalue_use (range_expr);
11869 if (range_decl == error_mark_node || range_expr == error_mark_node)
11870 /* If an error happened previously do nothing or else a lot of
11871 unhelpful errors would be issued. */
11872 begin_expr = end_expr = iter_type = error_mark_node;
11873 else
11875 tree range_temp;
11877 if (VAR_P (range_expr)
11878 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11879 /* Can't bind a reference to an array of runtime bound. */
11880 range_temp = range_expr;
11881 else
11883 range_temp = build_range_temp (range_expr);
11884 pushdecl (range_temp);
11885 cp_finish_decl (range_temp, range_expr,
11886 /*is_constant_init*/false, NULL_TREE,
11887 LOOKUP_ONLYCONVERTING);
11888 range_temp = convert_from_reference (range_temp);
11890 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11891 &begin_expr, &end_expr);
11894 /* The new for initialization statement. */
11895 begin = build_decl (input_location, VAR_DECL,
11896 get_identifier ("__for_begin"), iter_type);
11897 TREE_USED (begin) = 1;
11898 DECL_ARTIFICIAL (begin) = 1;
11899 pushdecl (begin);
11900 cp_finish_decl (begin, begin_expr,
11901 /*is_constant_init*/false, NULL_TREE,
11902 LOOKUP_ONLYCONVERTING);
11904 if (cxx_dialect >= cxx17)
11905 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11906 end = build_decl (input_location, VAR_DECL,
11907 get_identifier ("__for_end"), iter_type);
11908 TREE_USED (end) = 1;
11909 DECL_ARTIFICIAL (end) = 1;
11910 pushdecl (end);
11911 cp_finish_decl (end, end_expr,
11912 /*is_constant_init*/false, NULL_TREE,
11913 LOOKUP_ONLYCONVERTING);
11915 finish_init_stmt (statement);
11917 /* The new for condition. */
11918 condition = build_x_binary_op (input_location, NE_EXPR,
11919 begin, ERROR_MARK,
11920 end, ERROR_MARK,
11921 NULL, tf_warning_or_error);
11922 finish_for_cond (condition, statement, ivdep, unroll);
11924 /* The new increment expression. */
11925 expression = finish_unary_op_expr (input_location,
11926 PREINCREMENT_EXPR, begin,
11927 tf_warning_or_error);
11928 finish_for_expr (expression, statement);
11930 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11931 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11933 /* The declaration is initialized with *__begin inside the loop body. */
11934 cp_finish_decl (range_decl,
11935 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11936 tf_warning_or_error),
11937 /*is_constant_init*/false, NULL_TREE,
11938 LOOKUP_ONLYCONVERTING);
11939 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11940 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11942 return statement;
11945 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11946 We need to solve both at the same time because the method used
11947 depends on the existence of members begin or end.
11948 Returns the type deduced for the iterator expression. */
11950 static tree
11951 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11953 if (error_operand_p (range))
11955 *begin = *end = error_mark_node;
11956 return error_mark_node;
11959 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11961 error ("range-based %<for%> expression of type %qT "
11962 "has incomplete type", TREE_TYPE (range));
11963 *begin = *end = error_mark_node;
11964 return error_mark_node;
11966 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11968 /* If RANGE is an array, we will use pointer arithmetic. */
11969 *begin = decay_conversion (range, tf_warning_or_error);
11970 *end = build_binary_op (input_location, PLUS_EXPR,
11971 range,
11972 array_type_nelts_top (TREE_TYPE (range)),
11973 false);
11974 return TREE_TYPE (*begin);
11976 else
11978 /* If it is not an array, we must do a bit of magic. */
11979 tree id_begin, id_end;
11980 tree member_begin, member_end;
11982 *begin = *end = error_mark_node;
11984 id_begin = get_identifier ("begin");
11985 id_end = get_identifier ("end");
11986 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11987 /*protect=*/2, /*want_type=*/false,
11988 tf_warning_or_error);
11989 member_end = lookup_member (TREE_TYPE (range), id_end,
11990 /*protect=*/2, /*want_type=*/false,
11991 tf_warning_or_error);
11993 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11995 /* Use the member functions. */
11996 if (member_begin != NULL_TREE)
11997 *begin = cp_parser_range_for_member_function (range, id_begin);
11998 else
11999 error ("range-based %<for%> expression of type %qT has an "
12000 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
12002 if (member_end != NULL_TREE)
12003 *end = cp_parser_range_for_member_function (range, id_end);
12004 else
12005 error ("range-based %<for%> expression of type %qT has a "
12006 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12008 else
12010 /* Use global functions with ADL. */
12011 vec<tree, va_gc> *vec;
12012 vec = make_tree_vector ();
12014 vec_safe_push (vec, range);
12016 member_begin = perform_koenig_lookup (id_begin, vec,
12017 tf_warning_or_error);
12018 *begin = finish_call_expr (member_begin, &vec, false, true,
12019 tf_warning_or_error);
12020 member_end = perform_koenig_lookup (id_end, vec,
12021 tf_warning_or_error);
12022 *end = finish_call_expr (member_end, &vec, false, true,
12023 tf_warning_or_error);
12025 release_tree_vector (vec);
12028 /* Last common checks. */
12029 if (*begin == error_mark_node || *end == error_mark_node)
12031 /* If one of the expressions is an error do no more checks. */
12032 *begin = *end = error_mark_node;
12033 return error_mark_node;
12035 else if (type_dependent_expression_p (*begin)
12036 || type_dependent_expression_p (*end))
12037 /* Can happen, when, eg, in a template context, Koenig lookup
12038 can't resolve begin/end (c++/58503). */
12039 return NULL_TREE;
12040 else
12042 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12043 /* The unqualified type of the __begin and __end temporaries should
12044 be the same, as required by the multiple auto declaration. */
12045 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12047 if (cxx_dialect >= cxx17
12048 && (build_x_binary_op (input_location, NE_EXPR,
12049 *begin, ERROR_MARK,
12050 *end, ERROR_MARK,
12051 NULL, tf_none)
12052 != error_mark_node))
12053 /* P0184R0 allows __begin and __end to have different types,
12054 but make sure they are comparable so we can give a better
12055 diagnostic. */;
12056 else
12057 error ("inconsistent begin/end types in range-based %<for%> "
12058 "statement: %qT and %qT",
12059 TREE_TYPE (*begin), TREE_TYPE (*end));
12061 return iter_type;
12066 /* Helper function for cp_parser_perform_range_for_lookup.
12067 Builds a tree for RANGE.IDENTIFIER(). */
12069 static tree
12070 cp_parser_range_for_member_function (tree range, tree identifier)
12072 tree member, res;
12073 vec<tree, va_gc> *vec;
12075 member = finish_class_member_access_expr (range, identifier,
12076 false, tf_warning_or_error);
12077 if (member == error_mark_node)
12078 return error_mark_node;
12080 vec = make_tree_vector ();
12081 res = finish_call_expr (member, &vec,
12082 /*disallow_virtual=*/false,
12083 /*koenig_p=*/false,
12084 tf_warning_or_error);
12085 release_tree_vector (vec);
12086 return res;
12089 /* Parse an iteration-statement.
12091 iteration-statement:
12092 while ( condition ) statement
12093 do statement while ( expression ) ;
12094 for ( init-statement condition [opt] ; expression [opt] )
12095 statement
12097 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12099 static tree
12100 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12101 unsigned short unroll)
12103 cp_token *token;
12104 enum rid keyword;
12105 tree statement;
12106 unsigned char in_statement;
12107 token_indent_info guard_tinfo;
12109 /* Peek at the next token. */
12110 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12111 if (!token)
12112 return error_mark_node;
12114 guard_tinfo = get_token_indent_info (token);
12116 /* Remember whether or not we are already within an iteration
12117 statement. */
12118 in_statement = parser->in_statement;
12120 /* See what kind of keyword it is. */
12121 keyword = token->keyword;
12122 switch (keyword)
12124 case RID_WHILE:
12126 tree condition;
12128 /* Begin the while-statement. */
12129 statement = begin_while_stmt ();
12130 /* Look for the `('. */
12131 matching_parens parens;
12132 parens.require_open (parser);
12133 /* Parse the condition. */
12134 condition = cp_parser_condition (parser);
12135 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12136 /* Look for the `)'. */
12137 parens.require_close (parser);
12138 /* Parse the dependent statement. */
12139 parser->in_statement = IN_ITERATION_STMT;
12140 bool prev = note_iteration_stmt_body_start ();
12141 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12142 note_iteration_stmt_body_end (prev);
12143 parser->in_statement = in_statement;
12144 /* We're done with the while-statement. */
12145 finish_while_stmt (statement);
12147 break;
12149 case RID_DO:
12151 tree expression;
12153 /* Begin the do-statement. */
12154 statement = begin_do_stmt ();
12155 /* Parse the body of the do-statement. */
12156 parser->in_statement = IN_ITERATION_STMT;
12157 bool prev = note_iteration_stmt_body_start ();
12158 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12159 note_iteration_stmt_body_end (prev);
12160 parser->in_statement = in_statement;
12161 finish_do_body (statement);
12162 /* Look for the `while' keyword. */
12163 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12164 /* Look for the `('. */
12165 matching_parens parens;
12166 parens.require_open (parser);
12167 /* Parse the expression. */
12168 expression = cp_parser_expression (parser);
12169 /* We're done with the do-statement. */
12170 finish_do_stmt (expression, statement, ivdep, unroll);
12171 /* Look for the `)'. */
12172 parens.require_close (parser);
12173 /* Look for the `;'. */
12174 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12176 break;
12178 case RID_FOR:
12180 /* Look for the `('. */
12181 matching_parens parens;
12182 parens.require_open (parser);
12184 statement = cp_parser_for (parser, ivdep, unroll);
12186 /* Look for the `)'. */
12187 parens.require_close (parser);
12189 /* Parse the body of the for-statement. */
12190 parser->in_statement = IN_ITERATION_STMT;
12191 bool prev = note_iteration_stmt_body_start ();
12192 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12193 note_iteration_stmt_body_end (prev);
12194 parser->in_statement = in_statement;
12196 /* We're done with the for-statement. */
12197 finish_for_stmt (statement);
12199 break;
12201 default:
12202 cp_parser_error (parser, "expected iteration-statement");
12203 statement = error_mark_node;
12204 break;
12207 return statement;
12210 /* Parse a init-statement or the declarator of a range-based-for.
12211 Returns true if a range-based-for declaration is seen.
12213 init-statement:
12214 expression-statement
12215 simple-declaration */
12217 static bool
12218 cp_parser_init_statement (cp_parser* parser, tree *decl)
12220 /* If the next token is a `;', then we have an empty
12221 expression-statement. Grammatically, this is also a
12222 simple-declaration, but an invalid one, because it does not
12223 declare anything. Therefore, if we did not handle this case
12224 specially, we would issue an error message about an invalid
12225 declaration. */
12226 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12228 bool is_range_for = false;
12229 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12231 /* A colon is used in range-based for. */
12232 parser->colon_corrects_to_scope_p = false;
12234 /* We're going to speculatively look for a declaration, falling back
12235 to an expression, if necessary. */
12236 cp_parser_parse_tentatively (parser);
12237 /* Parse the declaration. */
12238 cp_parser_simple_declaration (parser,
12239 /*function_definition_allowed_p=*/false,
12240 decl);
12241 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12242 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12244 /* It is a range-for, consume the ':' */
12245 cp_lexer_consume_token (parser->lexer);
12246 is_range_for = true;
12247 if (cxx_dialect < cxx11)
12249 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12250 "range-based %<for%> loops only available with "
12251 "-std=c++11 or -std=gnu++11");
12252 *decl = error_mark_node;
12255 else
12256 /* The ';' is not consumed yet because we told
12257 cp_parser_simple_declaration not to. */
12258 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12260 if (cp_parser_parse_definitely (parser))
12261 return is_range_for;
12262 /* If the tentative parse failed, then we shall need to look for an
12263 expression-statement. */
12265 /* If we are here, it is an expression-statement. */
12266 cp_parser_expression_statement (parser, NULL_TREE);
12267 return false;
12270 /* Parse a jump-statement.
12272 jump-statement:
12273 break ;
12274 continue ;
12275 return expression [opt] ;
12276 return braced-init-list ;
12277 goto identifier ;
12279 GNU extension:
12281 jump-statement:
12282 goto * expression ;
12284 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12286 static tree
12287 cp_parser_jump_statement (cp_parser* parser)
12289 tree statement = error_mark_node;
12290 cp_token *token;
12291 enum rid keyword;
12292 unsigned char in_statement;
12294 /* Peek at the next token. */
12295 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12296 if (!token)
12297 return error_mark_node;
12299 /* See what kind of keyword it is. */
12300 keyword = token->keyword;
12301 switch (keyword)
12303 case RID_BREAK:
12304 in_statement = parser->in_statement & ~IN_IF_STMT;
12305 switch (in_statement)
12307 case 0:
12308 error_at (token->location, "break statement not within loop or switch");
12309 break;
12310 default:
12311 gcc_assert ((in_statement & IN_SWITCH_STMT)
12312 || in_statement == IN_ITERATION_STMT);
12313 statement = finish_break_stmt ();
12314 if (in_statement == IN_ITERATION_STMT)
12315 break_maybe_infinite_loop ();
12316 break;
12317 case IN_OMP_BLOCK:
12318 error_at (token->location, "invalid exit from OpenMP structured block");
12319 break;
12320 case IN_OMP_FOR:
12321 error_at (token->location, "break statement used with OpenMP for loop");
12322 break;
12324 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12325 break;
12327 case RID_CONTINUE:
12328 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12330 case 0:
12331 error_at (token->location, "continue statement not within a loop");
12332 break;
12333 /* Fall through. */
12334 case IN_ITERATION_STMT:
12335 case IN_OMP_FOR:
12336 statement = finish_continue_stmt ();
12337 break;
12338 case IN_OMP_BLOCK:
12339 error_at (token->location, "invalid exit from OpenMP structured block");
12340 break;
12341 default:
12342 gcc_unreachable ();
12344 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12345 break;
12347 case RID_RETURN:
12349 tree expr;
12350 bool expr_non_constant_p;
12352 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12354 cp_lexer_set_source_position (parser->lexer);
12355 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12356 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12358 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12359 expr = cp_parser_expression (parser);
12360 else
12361 /* If the next token is a `;', then there is no
12362 expression. */
12363 expr = NULL_TREE;
12364 /* Build the return-statement. */
12365 if (current_function_auto_return_pattern && in_discarded_stmt)
12366 /* Don't deduce from a discarded return statement. */;
12367 else
12368 statement = finish_return_stmt (expr);
12369 /* Look for the final `;'. */
12370 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12372 break;
12374 case RID_GOTO:
12375 if (parser->in_function_body
12376 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12378 error ("%<goto%> in %<constexpr%> function");
12379 cp_function_chain->invalid_constexpr = true;
12382 /* Create the goto-statement. */
12383 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12385 /* Issue a warning about this use of a GNU extension. */
12386 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12387 /* Consume the '*' token. */
12388 cp_lexer_consume_token (parser->lexer);
12389 /* Parse the dependent expression. */
12390 finish_goto_stmt (cp_parser_expression (parser));
12392 else
12393 finish_goto_stmt (cp_parser_identifier (parser));
12394 /* Look for the final `;'. */
12395 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12396 break;
12398 default:
12399 cp_parser_error (parser, "expected jump-statement");
12400 break;
12403 return statement;
12406 /* Parse a declaration-statement.
12408 declaration-statement:
12409 block-declaration */
12411 static void
12412 cp_parser_declaration_statement (cp_parser* parser)
12414 void *p;
12416 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12417 p = obstack_alloc (&declarator_obstack, 0);
12419 /* Parse the block-declaration. */
12420 cp_parser_block_declaration (parser, /*statement_p=*/true);
12422 /* Free any declarators allocated. */
12423 obstack_free (&declarator_obstack, p);
12426 /* Some dependent statements (like `if (cond) statement'), are
12427 implicitly in their own scope. In other words, if the statement is
12428 a single statement (as opposed to a compound-statement), it is
12429 none-the-less treated as if it were enclosed in braces. Any
12430 declarations appearing in the dependent statement are out of scope
12431 after control passes that point. This function parses a statement,
12432 but ensures that is in its own scope, even if it is not a
12433 compound-statement.
12435 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12436 is a (possibly labeled) if statement which is not enclosed in
12437 braces and has an else clause. This is used to implement
12438 -Wparentheses.
12440 CHAIN is a vector of if-else-if conditions. This is used to implement
12441 -Wduplicated-cond.
12443 Returns the new statement. */
12445 static tree
12446 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12447 const token_indent_info &guard_tinfo,
12448 vec<tree> *chain)
12450 tree statement;
12451 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12452 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12453 token_indent_info body_tinfo
12454 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12456 if (if_p != NULL)
12457 *if_p = false;
12459 /* Mark if () ; with a special NOP_EXPR. */
12460 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12462 cp_lexer_consume_token (parser->lexer);
12463 statement = add_stmt (build_empty_stmt (body_loc));
12465 if (guard_tinfo.keyword == RID_IF
12466 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12467 warning_at (body_loc, OPT_Wempty_body,
12468 "suggest braces around empty body in an %<if%> statement");
12469 else if (guard_tinfo.keyword == RID_ELSE)
12470 warning_at (body_loc, OPT_Wempty_body,
12471 "suggest braces around empty body in an %<else%> statement");
12473 /* if a compound is opened, we simply parse the statement directly. */
12474 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12475 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12476 /* If the token is not a `{', then we must take special action. */
12477 else
12479 /* Create a compound-statement. */
12480 statement = begin_compound_stmt (0);
12481 /* Parse the dependent-statement. */
12482 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12483 &body_loc_after_labels);
12484 /* Finish the dummy compound-statement. */
12485 finish_compound_stmt (statement);
12488 token_indent_info next_tinfo
12489 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12490 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12492 if (body_loc_after_labels != UNKNOWN_LOCATION
12493 && next_tinfo.type != CPP_SEMICOLON)
12494 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12495 guard_tinfo.location, guard_tinfo.keyword);
12497 /* Return the statement. */
12498 return statement;
12501 /* For some dependent statements (like `while (cond) statement'), we
12502 have already created a scope. Therefore, even if the dependent
12503 statement is a compound-statement, we do not want to create another
12504 scope. */
12506 static void
12507 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12508 const token_indent_info &guard_tinfo)
12510 /* If the token is a `{', then we must take special action. */
12511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12513 token_indent_info body_tinfo
12514 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12515 location_t loc_after_labels = UNKNOWN_LOCATION;
12517 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12518 &loc_after_labels);
12519 token_indent_info next_tinfo
12520 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12521 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12523 if (loc_after_labels != UNKNOWN_LOCATION
12524 && next_tinfo.type != CPP_SEMICOLON)
12525 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12526 guard_tinfo.location,
12527 guard_tinfo.keyword);
12529 else
12531 /* Avoid calling cp_parser_compound_statement, so that we
12532 don't create a new scope. Do everything else by hand. */
12533 matching_braces braces;
12534 braces.require_open (parser);
12535 /* If the next keyword is `__label__' we have a label declaration. */
12536 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12537 cp_parser_label_declaration (parser);
12538 /* Parse an (optional) statement-seq. */
12539 cp_parser_statement_seq_opt (parser, NULL_TREE);
12540 braces.require_close (parser);
12544 /* Declarations [gram.dcl.dcl] */
12546 /* Parse an optional declaration-sequence.
12548 declaration-seq:
12549 declaration
12550 declaration-seq declaration */
12552 static void
12553 cp_parser_declaration_seq_opt (cp_parser* parser)
12555 while (true)
12557 cp_token *token;
12559 token = cp_lexer_peek_token (parser->lexer);
12561 if (token->type == CPP_CLOSE_BRACE
12562 || token->type == CPP_EOF
12563 || token->type == CPP_PRAGMA_EOL)
12564 break;
12566 if (token->type == CPP_SEMICOLON)
12568 /* A declaration consisting of a single semicolon is
12569 invalid. Allow it unless we're being pedantic. */
12570 cp_lexer_consume_token (parser->lexer);
12571 if (!in_system_header_at (input_location))
12572 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12573 continue;
12576 /* If we're entering or exiting a region that's implicitly
12577 extern "C", modify the lang context appropriately. */
12578 if (!parser->implicit_extern_c && token->implicit_extern_c)
12580 push_lang_context (lang_name_c);
12581 parser->implicit_extern_c = true;
12583 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12585 pop_lang_context ();
12586 parser->implicit_extern_c = false;
12589 if (token->type == CPP_PRAGMA)
12591 /* A top-level declaration can consist solely of a #pragma.
12592 A nested declaration cannot, so this is done here and not
12593 in cp_parser_declaration. (A #pragma at block scope is
12594 handled in cp_parser_statement.) */
12595 cp_parser_pragma (parser, pragma_external, NULL);
12596 continue;
12599 /* Parse the declaration itself. */
12600 cp_parser_declaration (parser);
12604 /* Parse a declaration.
12606 declaration:
12607 block-declaration
12608 function-definition
12609 template-declaration
12610 explicit-instantiation
12611 explicit-specialization
12612 linkage-specification
12613 namespace-definition
12615 C++17:
12616 deduction-guide
12618 GNU extension:
12620 declaration:
12621 __extension__ declaration */
12623 static void
12624 cp_parser_declaration (cp_parser* parser)
12626 cp_token token1;
12627 cp_token token2;
12628 int saved_pedantic;
12629 void *p;
12630 tree attributes = NULL_TREE;
12632 /* Check for the `__extension__' keyword. */
12633 if (cp_parser_extension_opt (parser, &saved_pedantic))
12635 /* Parse the qualified declaration. */
12636 cp_parser_declaration (parser);
12637 /* Restore the PEDANTIC flag. */
12638 pedantic = saved_pedantic;
12640 return;
12643 /* Try to figure out what kind of declaration is present. */
12644 token1 = *cp_lexer_peek_token (parser->lexer);
12646 if (token1.type != CPP_EOF)
12647 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12648 else
12650 token2.type = CPP_EOF;
12651 token2.keyword = RID_MAX;
12654 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12655 p = obstack_alloc (&declarator_obstack, 0);
12657 /* If the next token is `extern' and the following token is a string
12658 literal, then we have a linkage specification. */
12659 if (token1.keyword == RID_EXTERN
12660 && cp_parser_is_pure_string_literal (&token2))
12661 cp_parser_linkage_specification (parser);
12662 /* If the next token is `template', then we have either a template
12663 declaration, an explicit instantiation, or an explicit
12664 specialization. */
12665 else if (token1.keyword == RID_TEMPLATE)
12667 /* `template <>' indicates a template specialization. */
12668 if (token2.type == CPP_LESS
12669 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12670 cp_parser_explicit_specialization (parser);
12671 /* `template <' indicates a template declaration. */
12672 else if (token2.type == CPP_LESS)
12673 cp_parser_template_declaration (parser, /*member_p=*/false);
12674 /* Anything else must be an explicit instantiation. */
12675 else
12676 cp_parser_explicit_instantiation (parser);
12678 /* If the next token is `export', then we have a template
12679 declaration. */
12680 else if (token1.keyword == RID_EXPORT)
12681 cp_parser_template_declaration (parser, /*member_p=*/false);
12682 /* If the next token is `extern', 'static' or 'inline' and the one
12683 after that is `template', we have a GNU extended explicit
12684 instantiation directive. */
12685 else if (cp_parser_allow_gnu_extensions_p (parser)
12686 && (token1.keyword == RID_EXTERN
12687 || token1.keyword == RID_STATIC
12688 || token1.keyword == RID_INLINE)
12689 && token2.keyword == RID_TEMPLATE)
12690 cp_parser_explicit_instantiation (parser);
12691 /* If the next token is `namespace', check for a named or unnamed
12692 namespace definition. */
12693 else if (token1.keyword == RID_NAMESPACE
12694 && (/* A named namespace definition. */
12695 (token2.type == CPP_NAME
12696 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12697 != CPP_EQ))
12698 || (token2.type == CPP_OPEN_SQUARE
12699 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12700 == CPP_OPEN_SQUARE)
12701 /* An unnamed namespace definition. */
12702 || token2.type == CPP_OPEN_BRACE
12703 || token2.keyword == RID_ATTRIBUTE))
12704 cp_parser_namespace_definition (parser);
12705 /* An inline (associated) namespace definition. */
12706 else if (token1.keyword == RID_INLINE
12707 && token2.keyword == RID_NAMESPACE)
12708 cp_parser_namespace_definition (parser);
12709 /* Objective-C++ declaration/definition. */
12710 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12711 cp_parser_objc_declaration (parser, NULL_TREE);
12712 else if (c_dialect_objc ()
12713 && token1.keyword == RID_ATTRIBUTE
12714 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12715 cp_parser_objc_declaration (parser, attributes);
12716 /* At this point we may have a template declared by a concept
12717 introduction. */
12718 else if (flag_concepts
12719 && cp_parser_template_declaration_after_export (parser,
12720 /*member_p=*/false))
12721 /* We did. */;
12722 else
12723 /* Try to parse a block-declaration, or a function-definition. */
12724 cp_parser_block_declaration (parser, /*statement_p=*/false);
12726 /* Free any declarators allocated. */
12727 obstack_free (&declarator_obstack, p);
12730 /* Parse a block-declaration.
12732 block-declaration:
12733 simple-declaration
12734 asm-definition
12735 namespace-alias-definition
12736 using-declaration
12737 using-directive
12739 GNU Extension:
12741 block-declaration:
12742 __extension__ block-declaration
12744 C++0x Extension:
12746 block-declaration:
12747 static_assert-declaration
12749 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12750 part of a declaration-statement. */
12752 static void
12753 cp_parser_block_declaration (cp_parser *parser,
12754 bool statement_p)
12756 cp_token *token1;
12757 int saved_pedantic;
12759 /* Check for the `__extension__' keyword. */
12760 if (cp_parser_extension_opt (parser, &saved_pedantic))
12762 /* Parse the qualified declaration. */
12763 cp_parser_block_declaration (parser, statement_p);
12764 /* Restore the PEDANTIC flag. */
12765 pedantic = saved_pedantic;
12767 return;
12770 /* Peek at the next token to figure out which kind of declaration is
12771 present. */
12772 token1 = cp_lexer_peek_token (parser->lexer);
12774 /* If the next keyword is `asm', we have an asm-definition. */
12775 if (token1->keyword == RID_ASM)
12777 if (statement_p)
12778 cp_parser_commit_to_tentative_parse (parser);
12779 cp_parser_asm_definition (parser);
12781 /* If the next keyword is `namespace', we have a
12782 namespace-alias-definition. */
12783 else if (token1->keyword == RID_NAMESPACE)
12784 cp_parser_namespace_alias_definition (parser);
12785 /* If the next keyword is `using', we have a
12786 using-declaration, a using-directive, or an alias-declaration. */
12787 else if (token1->keyword == RID_USING)
12789 cp_token *token2;
12791 if (statement_p)
12792 cp_parser_commit_to_tentative_parse (parser);
12793 /* If the token after `using' is `namespace', then we have a
12794 using-directive. */
12795 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12796 if (token2->keyword == RID_NAMESPACE)
12797 cp_parser_using_directive (parser);
12798 /* If the second token after 'using' is '=', then we have an
12799 alias-declaration. */
12800 else if (cxx_dialect >= cxx11
12801 && token2->type == CPP_NAME
12802 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12803 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12804 cp_parser_alias_declaration (parser);
12805 /* Otherwise, it's a using-declaration. */
12806 else
12807 cp_parser_using_declaration (parser,
12808 /*access_declaration_p=*/false);
12810 /* If the next keyword is `__label__' we have a misplaced label
12811 declaration. */
12812 else if (token1->keyword == RID_LABEL)
12814 cp_lexer_consume_token (parser->lexer);
12815 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12816 cp_parser_skip_to_end_of_statement (parser);
12817 /* If the next token is now a `;', consume it. */
12818 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12819 cp_lexer_consume_token (parser->lexer);
12821 /* If the next token is `static_assert' we have a static assertion. */
12822 else if (token1->keyword == RID_STATIC_ASSERT)
12823 cp_parser_static_assert (parser, /*member_p=*/false);
12824 /* Anything else must be a simple-declaration. */
12825 else
12826 cp_parser_simple_declaration (parser, !statement_p,
12827 /*maybe_range_for_decl*/NULL);
12830 /* Parse a simple-declaration.
12832 simple-declaration:
12833 decl-specifier-seq [opt] init-declarator-list [opt] ;
12834 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12835 brace-or-equal-initializer ;
12837 init-declarator-list:
12838 init-declarator
12839 init-declarator-list , init-declarator
12841 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12842 function-definition as a simple-declaration.
12844 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12845 parsed declaration if it is an uninitialized single declarator not followed
12846 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12847 if present, will not be consumed. */
12849 static void
12850 cp_parser_simple_declaration (cp_parser* parser,
12851 bool function_definition_allowed_p,
12852 tree *maybe_range_for_decl)
12854 cp_decl_specifier_seq decl_specifiers;
12855 int declares_class_or_enum;
12856 bool saw_declarator;
12857 location_t comma_loc = UNKNOWN_LOCATION;
12858 location_t init_loc = UNKNOWN_LOCATION;
12860 if (maybe_range_for_decl)
12861 *maybe_range_for_decl = NULL_TREE;
12863 /* Defer access checks until we know what is being declared; the
12864 checks for names appearing in the decl-specifier-seq should be
12865 done as if we were in the scope of the thing being declared. */
12866 push_deferring_access_checks (dk_deferred);
12868 /* Parse the decl-specifier-seq. We have to keep track of whether
12869 or not the decl-specifier-seq declares a named class or
12870 enumeration type, since that is the only case in which the
12871 init-declarator-list is allowed to be empty.
12873 [dcl.dcl]
12875 In a simple-declaration, the optional init-declarator-list can be
12876 omitted only when declaring a class or enumeration, that is when
12877 the decl-specifier-seq contains either a class-specifier, an
12878 elaborated-type-specifier, or an enum-specifier. */
12879 cp_parser_decl_specifier_seq (parser,
12880 CP_PARSER_FLAGS_OPTIONAL,
12881 &decl_specifiers,
12882 &declares_class_or_enum);
12883 /* We no longer need to defer access checks. */
12884 stop_deferring_access_checks ();
12886 /* In a block scope, a valid declaration must always have a
12887 decl-specifier-seq. By not trying to parse declarators, we can
12888 resolve the declaration/expression ambiguity more quickly. */
12889 if (!function_definition_allowed_p
12890 && !decl_specifiers.any_specifiers_p)
12892 cp_parser_error (parser, "expected declaration");
12893 goto done;
12896 /* If the next two tokens are both identifiers, the code is
12897 erroneous. The usual cause of this situation is code like:
12899 T t;
12901 where "T" should name a type -- but does not. */
12902 if (!decl_specifiers.any_type_specifiers_p
12903 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12905 /* If parsing tentatively, we should commit; we really are
12906 looking at a declaration. */
12907 cp_parser_commit_to_tentative_parse (parser);
12908 /* Give up. */
12909 goto done;
12912 /* If we have seen at least one decl-specifier, and the next token
12913 is not a parenthesis, then we must be looking at a declaration.
12914 (After "int (" we might be looking at a functional cast.) */
12915 if (decl_specifiers.any_specifiers_p
12916 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12917 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12918 && !cp_parser_error_occurred (parser))
12919 cp_parser_commit_to_tentative_parse (parser);
12921 /* Look for C++17 decomposition declaration. */
12922 for (size_t n = 1; ; n++)
12923 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12924 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12925 continue;
12926 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12927 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12928 && decl_specifiers.any_specifiers_p)
12930 tree decl
12931 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12932 maybe_range_for_decl,
12933 &init_loc);
12935 /* The next token should be either a `,' or a `;'. */
12936 cp_token *token = cp_lexer_peek_token (parser->lexer);
12937 /* If it's a `;', we are done. */
12938 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12939 goto finish;
12940 /* Anything else is an error. */
12941 else
12943 /* If we have already issued an error message we don't need
12944 to issue another one. */
12945 if ((decl != error_mark_node
12946 && DECL_INITIAL (decl) != error_mark_node)
12947 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12948 cp_parser_error (parser, "expected %<,%> or %<;%>");
12949 /* Skip tokens until we reach the end of the statement. */
12950 cp_parser_skip_to_end_of_statement (parser);
12951 /* If the next token is now a `;', consume it. */
12952 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12953 cp_lexer_consume_token (parser->lexer);
12954 goto done;
12957 else
12958 break;
12960 tree last_type;
12961 bool auto_specifier_p;
12962 /* NULL_TREE if both variable and function declaration are allowed,
12963 error_mark_node if function declaration are not allowed and
12964 a FUNCTION_DECL that should be diagnosed if it is followed by
12965 variable declarations. */
12966 tree auto_function_declaration;
12968 last_type = NULL_TREE;
12969 auto_specifier_p
12970 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12971 auto_function_declaration = NULL_TREE;
12973 /* Keep going until we hit the `;' at the end of the simple
12974 declaration. */
12975 saw_declarator = false;
12976 while (cp_lexer_next_token_is_not (parser->lexer,
12977 CPP_SEMICOLON))
12979 cp_token *token;
12980 bool function_definition_p;
12981 tree decl;
12982 tree auto_result = NULL_TREE;
12984 if (saw_declarator)
12986 /* If we are processing next declarator, comma is expected */
12987 token = cp_lexer_peek_token (parser->lexer);
12988 gcc_assert (token->type == CPP_COMMA);
12989 cp_lexer_consume_token (parser->lexer);
12990 if (maybe_range_for_decl)
12992 *maybe_range_for_decl = error_mark_node;
12993 if (comma_loc == UNKNOWN_LOCATION)
12994 comma_loc = token->location;
12997 else
12998 saw_declarator = true;
13000 /* Parse the init-declarator. */
13001 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13002 /*checks=*/NULL,
13003 function_definition_allowed_p,
13004 /*member_p=*/false,
13005 declares_class_or_enum,
13006 &function_definition_p,
13007 maybe_range_for_decl,
13008 &init_loc,
13009 &auto_result);
13010 /* If an error occurred while parsing tentatively, exit quickly.
13011 (That usually happens when in the body of a function; each
13012 statement is treated as a declaration-statement until proven
13013 otherwise.) */
13014 if (cp_parser_error_occurred (parser))
13015 goto done;
13017 if (auto_specifier_p && cxx_dialect >= cxx14)
13019 /* If the init-declarator-list contains more than one
13020 init-declarator, they shall all form declarations of
13021 variables. */
13022 if (auto_function_declaration == NULL_TREE)
13023 auto_function_declaration
13024 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13025 else if (TREE_CODE (decl) == FUNCTION_DECL
13026 || auto_function_declaration != error_mark_node)
13028 error_at (decl_specifiers.locations[ds_type_spec],
13029 "non-variable %qD in declaration with more than one "
13030 "declarator with placeholder type",
13031 TREE_CODE (decl) == FUNCTION_DECL
13032 ? decl : auto_function_declaration);
13033 auto_function_declaration = error_mark_node;
13037 if (auto_result
13038 && (!processing_template_decl || !type_uses_auto (auto_result)))
13040 if (last_type
13041 && last_type != error_mark_node
13042 && !same_type_p (auto_result, last_type))
13044 /* If the list of declarators contains more than one declarator,
13045 the type of each declared variable is determined as described
13046 above. If the type deduced for the template parameter U is not
13047 the same in each deduction, the program is ill-formed. */
13048 error_at (decl_specifiers.locations[ds_type_spec],
13049 "inconsistent deduction for %qT: %qT and then %qT",
13050 decl_specifiers.type, last_type, auto_result);
13051 last_type = error_mark_node;
13053 else
13054 last_type = auto_result;
13057 /* Handle function definitions specially. */
13058 if (function_definition_p)
13060 /* If the next token is a `,', then we are probably
13061 processing something like:
13063 void f() {}, *p;
13065 which is erroneous. */
13066 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13068 cp_token *token = cp_lexer_peek_token (parser->lexer);
13069 error_at (token->location,
13070 "mixing"
13071 " declarations and function-definitions is forbidden");
13073 /* Otherwise, we're done with the list of declarators. */
13074 else
13076 pop_deferring_access_checks ();
13077 return;
13080 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13081 *maybe_range_for_decl = decl;
13082 /* The next token should be either a `,' or a `;'. */
13083 token = cp_lexer_peek_token (parser->lexer);
13084 /* If it's a `,', there are more declarators to come. */
13085 if (token->type == CPP_COMMA)
13086 /* will be consumed next time around */;
13087 /* If it's a `;', we are done. */
13088 else if (token->type == CPP_SEMICOLON)
13089 break;
13090 else if (maybe_range_for_decl)
13092 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13093 permerror (decl_specifiers.locations[ds_type_spec],
13094 "types may not be defined in a for-range-declaration");
13095 break;
13097 /* Anything else is an error. */
13098 else
13100 /* If we have already issued an error message we don't need
13101 to issue another one. */
13102 if ((decl != error_mark_node
13103 && DECL_INITIAL (decl) != error_mark_node)
13104 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13105 cp_parser_error (parser, "expected %<,%> or %<;%>");
13106 /* Skip tokens until we reach the end of the statement. */
13107 cp_parser_skip_to_end_of_statement (parser);
13108 /* If the next token is now a `;', consume it. */
13109 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13110 cp_lexer_consume_token (parser->lexer);
13111 goto done;
13113 /* After the first time around, a function-definition is not
13114 allowed -- even if it was OK at first. For example:
13116 int i, f() {}
13118 is not valid. */
13119 function_definition_allowed_p = false;
13122 /* Issue an error message if no declarators are present, and the
13123 decl-specifier-seq does not itself declare a class or
13124 enumeration: [dcl.dcl]/3. */
13125 if (!saw_declarator)
13127 if (cp_parser_declares_only_class_p (parser))
13129 if (!declares_class_or_enum
13130 && decl_specifiers.type
13131 && OVERLOAD_TYPE_P (decl_specifiers.type))
13132 /* Ensure an error is issued anyway when finish_decltype_type,
13133 called via cp_parser_decl_specifier_seq, returns a class or
13134 an enumeration (c++/51786). */
13135 decl_specifiers.type = NULL_TREE;
13136 shadow_tag (&decl_specifiers);
13138 /* Perform any deferred access checks. */
13139 perform_deferred_access_checks (tf_warning_or_error);
13142 /* Consume the `;'. */
13143 finish:
13144 if (!maybe_range_for_decl)
13145 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13146 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13148 if (init_loc != UNKNOWN_LOCATION)
13149 error_at (init_loc, "initializer in range-based %<for%> loop");
13150 if (comma_loc != UNKNOWN_LOCATION)
13151 error_at (comma_loc,
13152 "multiple declarations in range-based %<for%> loop");
13155 done:
13156 pop_deferring_access_checks ();
13159 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13160 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13161 initializer ; */
13163 static tree
13164 cp_parser_decomposition_declaration (cp_parser *parser,
13165 cp_decl_specifier_seq *decl_specifiers,
13166 tree *maybe_range_for_decl,
13167 location_t *init_loc)
13169 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13170 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13171 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13173 /* Parse the identifier-list. */
13174 auto_vec<cp_expr, 10> v;
13175 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13176 while (true)
13178 cp_expr e = cp_parser_identifier (parser);
13179 if (e.get_value () == error_mark_node)
13180 break;
13181 v.safe_push (e);
13182 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13183 break;
13184 cp_lexer_consume_token (parser->lexer);
13187 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13188 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13190 end_loc = UNKNOWN_LOCATION;
13191 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13192 false);
13193 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13194 cp_lexer_consume_token (parser->lexer);
13195 else
13197 cp_parser_skip_to_end_of_statement (parser);
13198 return error_mark_node;
13202 if (cxx_dialect < cxx17)
13203 pedwarn (loc, 0, "structured bindings only available with "
13204 "-std=c++17 or -std=gnu++17");
13206 tree pushed_scope;
13207 cp_declarator *declarator = make_declarator (cdk_decomp);
13208 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13209 declarator->id_loc = loc;
13210 if (ref_qual != REF_QUAL_NONE)
13211 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13212 ref_qual == REF_QUAL_RVALUE,
13213 NULL_TREE);
13214 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13215 NULL_TREE, decl_specifiers->attributes,
13216 &pushed_scope);
13217 tree orig_decl = decl;
13219 unsigned int i;
13220 cp_expr e;
13221 cp_decl_specifier_seq decl_specs;
13222 clear_decl_specs (&decl_specs);
13223 decl_specs.type = make_auto ();
13224 tree prev = decl;
13225 FOR_EACH_VEC_ELT (v, i, e)
13227 if (i == 0)
13228 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13229 else
13230 declarator->u.id.unqualified_name = e.get_value ();
13231 declarator->id_loc = e.get_location ();
13232 tree elt_pushed_scope;
13233 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13234 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13235 if (decl2 == error_mark_node)
13236 decl = error_mark_node;
13237 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13239 /* Ensure we've diagnosed redeclaration if we aren't creating
13240 a new VAR_DECL. */
13241 gcc_assert (errorcount);
13242 decl = error_mark_node;
13244 else
13245 prev = decl2;
13246 if (elt_pushed_scope)
13247 pop_scope (elt_pushed_scope);
13250 if (v.is_empty ())
13252 error_at (loc, "empty structured binding declaration");
13253 decl = error_mark_node;
13256 if (maybe_range_for_decl == NULL
13257 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13259 bool non_constant_p = false, is_direct_init = false;
13260 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13261 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13262 &non_constant_p);
13263 if (initializer == NULL_TREE
13264 || (TREE_CODE (initializer) == TREE_LIST
13265 && TREE_CHAIN (initializer))
13266 || (is_direct_init
13267 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13268 && CONSTRUCTOR_NELTS (initializer) != 1))
13270 error_at (loc, "invalid initializer for structured binding "
13271 "declaration");
13272 initializer = error_mark_node;
13275 if (decl != error_mark_node)
13277 cp_maybe_mangle_decomp (decl, prev, v.length ());
13278 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13279 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13280 cp_finish_decomp (decl, prev, v.length ());
13283 else if (decl != error_mark_node)
13285 *maybe_range_for_decl = prev;
13286 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13287 the underlying DECL. */
13288 cp_finish_decomp (decl, prev, v.length ());
13291 if (pushed_scope)
13292 pop_scope (pushed_scope);
13294 if (decl == error_mark_node && DECL_P (orig_decl))
13296 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13297 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13300 return decl;
13303 /* Parse a decl-specifier-seq.
13305 decl-specifier-seq:
13306 decl-specifier-seq [opt] decl-specifier
13307 decl-specifier attribute-specifier-seq [opt] (C++11)
13309 decl-specifier:
13310 storage-class-specifier
13311 type-specifier
13312 function-specifier
13313 friend
13314 typedef
13316 GNU Extension:
13318 decl-specifier:
13319 attributes
13321 Concepts Extension:
13323 decl-specifier:
13324 concept
13326 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13328 The parser flags FLAGS is used to control type-specifier parsing.
13330 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13331 flags:
13333 1: one of the decl-specifiers is an elaborated-type-specifier
13334 (i.e., a type declaration)
13335 2: one of the decl-specifiers is an enum-specifier or a
13336 class-specifier (i.e., a type definition)
13340 static void
13341 cp_parser_decl_specifier_seq (cp_parser* parser,
13342 cp_parser_flags flags,
13343 cp_decl_specifier_seq *decl_specs,
13344 int* declares_class_or_enum)
13346 bool constructor_possible_p = !parser->in_declarator_p;
13347 bool found_decl_spec = false;
13348 cp_token *start_token = NULL;
13349 cp_decl_spec ds;
13351 /* Clear DECL_SPECS. */
13352 clear_decl_specs (decl_specs);
13354 /* Assume no class or enumeration type is declared. */
13355 *declares_class_or_enum = 0;
13357 /* Keep reading specifiers until there are no more to read. */
13358 while (true)
13360 bool constructor_p;
13361 cp_token *token;
13362 ds = ds_last;
13364 /* Peek at the next token. */
13365 token = cp_lexer_peek_token (parser->lexer);
13367 /* Save the first token of the decl spec list for error
13368 reporting. */
13369 if (!start_token)
13370 start_token = token;
13371 /* Handle attributes. */
13372 if (cp_next_tokens_can_be_attribute_p (parser))
13374 /* Parse the attributes. */
13375 tree attrs = cp_parser_attributes_opt (parser);
13377 /* In a sequence of declaration specifiers, c++11 attributes
13378 appertain to the type that precede them. In that case
13379 [dcl.spec]/1 says:
13381 The attribute-specifier-seq affects the type only for
13382 the declaration it appears in, not other declarations
13383 involving the same type.
13385 But for now let's force the user to position the
13386 attribute either at the beginning of the declaration or
13387 after the declarator-id, which would clearly mean that it
13388 applies to the declarator. */
13389 if (cxx11_attribute_p (attrs))
13391 if (!found_decl_spec)
13392 /* The c++11 attribute is at the beginning of the
13393 declaration. It appertains to the entity being
13394 declared. */;
13395 else
13397 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13399 /* This is an attribute following a
13400 class-specifier. */
13401 if (decl_specs->type_definition_p)
13402 warn_misplaced_attr_for_class_type (token->location,
13403 decl_specs->type);
13404 attrs = NULL_TREE;
13406 else
13408 decl_specs->std_attributes
13409 = attr_chainon (decl_specs->std_attributes, attrs);
13410 if (decl_specs->locations[ds_std_attribute] == 0)
13411 decl_specs->locations[ds_std_attribute] = token->location;
13413 continue;
13417 decl_specs->attributes
13418 = attr_chainon (decl_specs->attributes, attrs);
13419 if (decl_specs->locations[ds_attribute] == 0)
13420 decl_specs->locations[ds_attribute] = token->location;
13421 continue;
13423 /* Assume we will find a decl-specifier keyword. */
13424 found_decl_spec = true;
13425 /* If the next token is an appropriate keyword, we can simply
13426 add it to the list. */
13427 switch (token->keyword)
13429 /* decl-specifier:
13430 friend
13431 constexpr */
13432 case RID_FRIEND:
13433 if (!at_class_scope_p ())
13435 gcc_rich_location richloc (token->location);
13436 richloc.add_fixit_remove ();
13437 error_at (&richloc, "%<friend%> used outside of class");
13438 cp_lexer_purge_token (parser->lexer);
13440 else
13442 ds = ds_friend;
13443 /* Consume the token. */
13444 cp_lexer_consume_token (parser->lexer);
13446 break;
13448 case RID_CONSTEXPR:
13449 ds = ds_constexpr;
13450 cp_lexer_consume_token (parser->lexer);
13451 break;
13453 case RID_CONCEPT:
13454 ds = ds_concept;
13455 cp_lexer_consume_token (parser->lexer);
13456 break;
13458 /* function-specifier:
13459 inline
13460 virtual
13461 explicit */
13462 case RID_INLINE:
13463 case RID_VIRTUAL:
13464 case RID_EXPLICIT:
13465 cp_parser_function_specifier_opt (parser, decl_specs);
13466 break;
13468 /* decl-specifier:
13469 typedef */
13470 case RID_TYPEDEF:
13471 ds = ds_typedef;
13472 /* Consume the token. */
13473 cp_lexer_consume_token (parser->lexer);
13474 /* A constructor declarator cannot appear in a typedef. */
13475 constructor_possible_p = false;
13476 /* The "typedef" keyword can only occur in a declaration; we
13477 may as well commit at this point. */
13478 cp_parser_commit_to_tentative_parse (parser);
13480 if (decl_specs->storage_class != sc_none)
13481 decl_specs->conflicting_specifiers_p = true;
13482 break;
13484 /* storage-class-specifier:
13485 auto
13486 register
13487 static
13488 extern
13489 mutable
13491 GNU Extension:
13492 thread */
13493 case RID_AUTO:
13494 if (cxx_dialect == cxx98)
13496 /* Consume the token. */
13497 cp_lexer_consume_token (parser->lexer);
13499 /* Complain about `auto' as a storage specifier, if
13500 we're complaining about C++0x compatibility. */
13501 gcc_rich_location richloc (token->location);
13502 richloc.add_fixit_remove ();
13503 warning_at (&richloc, OPT_Wc__11_compat,
13504 "%<auto%> changes meaning in C++11; "
13505 "please remove it");
13507 /* Set the storage class anyway. */
13508 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13509 token);
13511 else
13512 /* C++0x auto type-specifier. */
13513 found_decl_spec = false;
13514 break;
13516 case RID_REGISTER:
13517 case RID_STATIC:
13518 case RID_EXTERN:
13519 case RID_MUTABLE:
13520 /* Consume the token. */
13521 cp_lexer_consume_token (parser->lexer);
13522 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13523 token);
13524 break;
13525 case RID_THREAD:
13526 /* Consume the token. */
13527 ds = ds_thread;
13528 cp_lexer_consume_token (parser->lexer);
13529 break;
13531 default:
13532 /* We did not yet find a decl-specifier yet. */
13533 found_decl_spec = false;
13534 break;
13537 if (found_decl_spec
13538 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13539 && token->keyword != RID_CONSTEXPR)
13540 error ("decl-specifier invalid in condition");
13542 if (found_decl_spec
13543 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13544 && token->keyword != RID_MUTABLE
13545 && token->keyword != RID_CONSTEXPR)
13546 error_at (token->location, "%qD invalid in lambda",
13547 ridpointers[token->keyword]);
13549 if (ds != ds_last)
13550 set_and_check_decl_spec_loc (decl_specs, ds, token);
13552 /* Constructors are a special case. The `S' in `S()' is not a
13553 decl-specifier; it is the beginning of the declarator. */
13554 constructor_p
13555 = (!found_decl_spec
13556 && constructor_possible_p
13557 && (cp_parser_constructor_declarator_p
13558 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13560 /* If we don't have a DECL_SPEC yet, then we must be looking at
13561 a type-specifier. */
13562 if (!found_decl_spec && !constructor_p)
13564 int decl_spec_declares_class_or_enum;
13565 bool is_cv_qualifier;
13566 tree type_spec;
13568 type_spec
13569 = cp_parser_type_specifier (parser, flags,
13570 decl_specs,
13571 /*is_declaration=*/true,
13572 &decl_spec_declares_class_or_enum,
13573 &is_cv_qualifier);
13574 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13576 /* If this type-specifier referenced a user-defined type
13577 (a typedef, class-name, etc.), then we can't allow any
13578 more such type-specifiers henceforth.
13580 [dcl.spec]
13582 The longest sequence of decl-specifiers that could
13583 possibly be a type name is taken as the
13584 decl-specifier-seq of a declaration. The sequence shall
13585 be self-consistent as described below.
13587 [dcl.type]
13589 As a general rule, at most one type-specifier is allowed
13590 in the complete decl-specifier-seq of a declaration. The
13591 only exceptions are the following:
13593 -- const or volatile can be combined with any other
13594 type-specifier.
13596 -- signed or unsigned can be combined with char, long,
13597 short, or int.
13599 -- ..
13601 Example:
13603 typedef char* Pc;
13604 void g (const int Pc);
13606 Here, Pc is *not* part of the decl-specifier seq; it's
13607 the declarator. Therefore, once we see a type-specifier
13608 (other than a cv-qualifier), we forbid any additional
13609 user-defined types. We *do* still allow things like `int
13610 int' to be considered a decl-specifier-seq, and issue the
13611 error message later. */
13612 if (type_spec && !is_cv_qualifier)
13613 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13614 /* A constructor declarator cannot follow a type-specifier. */
13615 if (type_spec)
13617 constructor_possible_p = false;
13618 found_decl_spec = true;
13619 if (!is_cv_qualifier)
13620 decl_specs->any_type_specifiers_p = true;
13624 /* If we still do not have a DECL_SPEC, then there are no more
13625 decl-specifiers. */
13626 if (!found_decl_spec)
13627 break;
13629 decl_specs->any_specifiers_p = true;
13630 /* After we see one decl-specifier, further decl-specifiers are
13631 always optional. */
13632 flags |= CP_PARSER_FLAGS_OPTIONAL;
13635 /* Don't allow a friend specifier with a class definition. */
13636 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13637 && (*declares_class_or_enum & 2))
13638 error_at (decl_specs->locations[ds_friend],
13639 "class definition may not be declared a friend");
13642 /* Parse an (optional) storage-class-specifier.
13644 storage-class-specifier:
13645 auto
13646 register
13647 static
13648 extern
13649 mutable
13651 GNU Extension:
13653 storage-class-specifier:
13654 thread
13656 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13658 static tree
13659 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13661 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13663 case RID_AUTO:
13664 if (cxx_dialect != cxx98)
13665 return NULL_TREE;
13666 /* Fall through for C++98. */
13667 gcc_fallthrough ();
13669 case RID_REGISTER:
13670 case RID_STATIC:
13671 case RID_EXTERN:
13672 case RID_MUTABLE:
13673 case RID_THREAD:
13674 /* Consume the token. */
13675 return cp_lexer_consume_token (parser->lexer)->u.value;
13677 default:
13678 return NULL_TREE;
13682 /* Parse an (optional) function-specifier.
13684 function-specifier:
13685 inline
13686 virtual
13687 explicit
13689 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13690 Updates DECL_SPECS, if it is non-NULL. */
13692 static tree
13693 cp_parser_function_specifier_opt (cp_parser* parser,
13694 cp_decl_specifier_seq *decl_specs)
13696 cp_token *token = cp_lexer_peek_token (parser->lexer);
13697 switch (token->keyword)
13699 case RID_INLINE:
13700 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13701 break;
13703 case RID_VIRTUAL:
13704 /* 14.5.2.3 [temp.mem]
13706 A member function template shall not be virtual. */
13707 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13708 && current_class_type)
13709 error_at (token->location, "templates may not be %<virtual%>");
13710 else
13711 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13712 break;
13714 case RID_EXPLICIT:
13715 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13716 break;
13718 default:
13719 return NULL_TREE;
13722 /* Consume the token. */
13723 return cp_lexer_consume_token (parser->lexer)->u.value;
13726 /* Parse a linkage-specification.
13728 linkage-specification:
13729 extern string-literal { declaration-seq [opt] }
13730 extern string-literal declaration */
13732 static void
13733 cp_parser_linkage_specification (cp_parser* parser)
13735 tree linkage;
13737 /* Look for the `extern' keyword. */
13738 cp_token *extern_token
13739 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13741 /* Look for the string-literal. */
13742 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13743 linkage = cp_parser_string_literal (parser, false, false);
13745 /* Transform the literal into an identifier. If the literal is a
13746 wide-character string, or contains embedded NULs, then we can't
13747 handle it as the user wants. */
13748 if (strlen (TREE_STRING_POINTER (linkage))
13749 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13751 cp_parser_error (parser, "invalid linkage-specification");
13752 /* Assume C++ linkage. */
13753 linkage = lang_name_cplusplus;
13755 else
13756 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13758 /* We're now using the new linkage. */
13759 push_lang_context (linkage);
13761 /* Preserve the location of the the innermost linkage specification,
13762 tracking the locations of nested specifications via a local. */
13763 location_t saved_location
13764 = parser->innermost_linkage_specification_location;
13765 /* Construct a location ranging from the start of the "extern" to
13766 the end of the string-literal, with the caret at the start, e.g.:
13767 extern "C" {
13768 ^~~~~~~~~~
13770 parser->innermost_linkage_specification_location
13771 = make_location (extern_token->location,
13772 extern_token->location,
13773 get_finish (string_token->location));
13775 /* If the next token is a `{', then we're using the first
13776 production. */
13777 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13779 cp_ensure_no_omp_declare_simd (parser);
13780 cp_ensure_no_oacc_routine (parser);
13782 /* Consume the `{' token. */
13783 matching_braces braces;
13784 braces.consume_open (parser)->location;
13785 /* Parse the declarations. */
13786 cp_parser_declaration_seq_opt (parser);
13787 /* Look for the closing `}'. */
13788 braces.require_close (parser);
13790 /* Otherwise, there's just one declaration. */
13791 else
13793 bool saved_in_unbraced_linkage_specification_p;
13795 saved_in_unbraced_linkage_specification_p
13796 = parser->in_unbraced_linkage_specification_p;
13797 parser->in_unbraced_linkage_specification_p = true;
13798 cp_parser_declaration (parser);
13799 parser->in_unbraced_linkage_specification_p
13800 = saved_in_unbraced_linkage_specification_p;
13803 /* We're done with the linkage-specification. */
13804 pop_lang_context ();
13806 /* Restore location of parent linkage specification, if any. */
13807 parser->innermost_linkage_specification_location = saved_location;
13810 /* Parse a static_assert-declaration.
13812 static_assert-declaration:
13813 static_assert ( constant-expression , string-literal ) ;
13814 static_assert ( constant-expression ) ; (C++17)
13816 If MEMBER_P, this static_assert is a class member. */
13818 static void
13819 cp_parser_static_assert(cp_parser *parser, bool member_p)
13821 cp_expr condition;
13822 location_t token_loc;
13823 tree message;
13824 bool dummy;
13826 /* Peek at the `static_assert' token so we can keep track of exactly
13827 where the static assertion started. */
13828 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13830 /* Look for the `static_assert' keyword. */
13831 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13832 RT_STATIC_ASSERT))
13833 return;
13835 /* We know we are in a static assertion; commit to any tentative
13836 parse. */
13837 if (cp_parser_parsing_tentatively (parser))
13838 cp_parser_commit_to_tentative_parse (parser);
13840 /* Parse the `(' starting the static assertion condition. */
13841 matching_parens parens;
13842 parens.require_open (parser);
13844 /* Parse the constant-expression. Allow a non-constant expression
13845 here in order to give better diagnostics in finish_static_assert. */
13846 condition =
13847 cp_parser_constant_expression (parser,
13848 /*allow_non_constant_p=*/true,
13849 /*non_constant_p=*/&dummy);
13851 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13853 if (cxx_dialect < cxx17)
13854 pedwarn (input_location, OPT_Wpedantic,
13855 "static_assert without a message "
13856 "only available with -std=c++17 or -std=gnu++17");
13857 /* Eat the ')' */
13858 cp_lexer_consume_token (parser->lexer);
13859 message = build_string (1, "");
13860 TREE_TYPE (message) = char_array_type_node;
13861 fix_string_type (message);
13863 else
13865 /* Parse the separating `,'. */
13866 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13868 /* Parse the string-literal message. */
13869 message = cp_parser_string_literal (parser,
13870 /*translate=*/false,
13871 /*wide_ok=*/true);
13873 /* A `)' completes the static assertion. */
13874 if (!parens.require_close (parser))
13875 cp_parser_skip_to_closing_parenthesis (parser,
13876 /*recovering=*/true,
13877 /*or_comma=*/false,
13878 /*consume_paren=*/true);
13881 /* A semicolon terminates the declaration. */
13882 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13884 /* Get the location for the static assertion. Use that of the
13885 condition if available, otherwise, use that of the "static_assert"
13886 token. */
13887 location_t assert_loc = condition.get_location ();
13888 if (assert_loc == UNKNOWN_LOCATION)
13889 assert_loc = token_loc;
13891 /* Complete the static assertion, which may mean either processing
13892 the static assert now or saving it for template instantiation. */
13893 finish_static_assert (condition, message, assert_loc, member_p);
13896 /* Parse the expression in decltype ( expression ). */
13898 static tree
13899 cp_parser_decltype_expr (cp_parser *parser,
13900 bool &id_expression_or_member_access_p)
13902 cp_token *id_expr_start_token;
13903 tree expr;
13905 /* Since we're going to preserve any side-effects from this parse, set up a
13906 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13907 in the expression. */
13908 tentative_firewall firewall (parser);
13910 /* First, try parsing an id-expression. */
13911 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13912 cp_parser_parse_tentatively (parser);
13913 expr = cp_parser_id_expression (parser,
13914 /*template_keyword_p=*/false,
13915 /*check_dependency_p=*/true,
13916 /*template_p=*/NULL,
13917 /*declarator_p=*/false,
13918 /*optional_p=*/false);
13920 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13922 bool non_integral_constant_expression_p = false;
13923 tree id_expression = expr;
13924 cp_id_kind idk;
13925 const char *error_msg;
13927 if (identifier_p (expr))
13928 /* Lookup the name we got back from the id-expression. */
13929 expr = cp_parser_lookup_name_simple (parser, expr,
13930 id_expr_start_token->location);
13932 if (expr
13933 && expr != error_mark_node
13934 && TREE_CODE (expr) != TYPE_DECL
13935 && (TREE_CODE (expr) != BIT_NOT_EXPR
13936 || !TYPE_P (TREE_OPERAND (expr, 0)))
13937 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13939 /* Complete lookup of the id-expression. */
13940 expr = (finish_id_expression
13941 (id_expression, expr, parser->scope, &idk,
13942 /*integral_constant_expression_p=*/false,
13943 /*allow_non_integral_constant_expression_p=*/true,
13944 &non_integral_constant_expression_p,
13945 /*template_p=*/false,
13946 /*done=*/true,
13947 /*address_p=*/false,
13948 /*template_arg_p=*/false,
13949 &error_msg,
13950 id_expr_start_token->location));
13952 if (expr == error_mark_node)
13953 /* We found an id-expression, but it was something that we
13954 should not have found. This is an error, not something
13955 we can recover from, so note that we found an
13956 id-expression and we'll recover as gracefully as
13957 possible. */
13958 id_expression_or_member_access_p = true;
13961 if (expr
13962 && expr != error_mark_node
13963 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13964 /* We have an id-expression. */
13965 id_expression_or_member_access_p = true;
13968 if (!id_expression_or_member_access_p)
13970 /* Abort the id-expression parse. */
13971 cp_parser_abort_tentative_parse (parser);
13973 /* Parsing tentatively, again. */
13974 cp_parser_parse_tentatively (parser);
13976 /* Parse a class member access. */
13977 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13978 /*cast_p=*/false, /*decltype*/true,
13979 /*member_access_only_p=*/true, NULL);
13981 if (expr
13982 && expr != error_mark_node
13983 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13984 /* We have an id-expression. */
13985 id_expression_or_member_access_p = true;
13988 if (id_expression_or_member_access_p)
13989 /* We have parsed the complete id-expression or member access. */
13990 cp_parser_parse_definitely (parser);
13991 else
13993 /* Abort our attempt to parse an id-expression or member access
13994 expression. */
13995 cp_parser_abort_tentative_parse (parser);
13997 /* Parse a full expression. */
13998 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13999 /*decltype_p=*/true);
14002 return expr;
14005 /* Parse a `decltype' type. Returns the type.
14007 simple-type-specifier:
14008 decltype ( expression )
14009 C++14 proposal:
14010 decltype ( auto ) */
14012 static tree
14013 cp_parser_decltype (cp_parser *parser)
14015 tree expr;
14016 bool id_expression_or_member_access_p = false;
14017 const char *saved_message;
14018 bool saved_integral_constant_expression_p;
14019 bool saved_non_integral_constant_expression_p;
14020 bool saved_greater_than_is_operator_p;
14021 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14023 if (start_token->type == CPP_DECLTYPE)
14025 /* Already parsed. */
14026 cp_lexer_consume_token (parser->lexer);
14027 return saved_checks_value (start_token->u.tree_check_value);
14030 /* Look for the `decltype' token. */
14031 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14032 return error_mark_node;
14034 /* Parse the opening `('. */
14035 matching_parens parens;
14036 if (!parens.require_open (parser))
14037 return error_mark_node;
14039 /* decltype (auto) */
14040 if (cxx_dialect >= cxx14
14041 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14043 cp_lexer_consume_token (parser->lexer);
14044 if (!parens.require_close (parser))
14045 return error_mark_node;
14046 expr = make_decltype_auto ();
14047 AUTO_IS_DECLTYPE (expr) = true;
14048 goto rewrite;
14051 /* Types cannot be defined in a `decltype' expression. Save away the
14052 old message. */
14053 saved_message = parser->type_definition_forbidden_message;
14055 /* And create the new one. */
14056 parser->type_definition_forbidden_message
14057 = G_("types may not be defined in %<decltype%> expressions");
14059 /* The restrictions on constant-expressions do not apply inside
14060 decltype expressions. */
14061 saved_integral_constant_expression_p
14062 = parser->integral_constant_expression_p;
14063 saved_non_integral_constant_expression_p
14064 = parser->non_integral_constant_expression_p;
14065 parser->integral_constant_expression_p = false;
14067 /* Within a parenthesized expression, a `>' token is always
14068 the greater-than operator. */
14069 saved_greater_than_is_operator_p
14070 = parser->greater_than_is_operator_p;
14071 parser->greater_than_is_operator_p = true;
14073 /* Do not actually evaluate the expression. */
14074 ++cp_unevaluated_operand;
14076 /* Do not warn about problems with the expression. */
14077 ++c_inhibit_evaluation_warnings;
14079 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14081 /* Go back to evaluating expressions. */
14082 --cp_unevaluated_operand;
14083 --c_inhibit_evaluation_warnings;
14085 /* The `>' token might be the end of a template-id or
14086 template-parameter-list now. */
14087 parser->greater_than_is_operator_p
14088 = saved_greater_than_is_operator_p;
14090 /* Restore the old message and the integral constant expression
14091 flags. */
14092 parser->type_definition_forbidden_message = saved_message;
14093 parser->integral_constant_expression_p
14094 = saved_integral_constant_expression_p;
14095 parser->non_integral_constant_expression_p
14096 = saved_non_integral_constant_expression_p;
14098 /* Parse to the closing `)'. */
14099 if (!parens.require_close (parser))
14101 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14102 /*consume_paren=*/true);
14103 return error_mark_node;
14106 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14107 tf_warning_or_error);
14109 rewrite:
14110 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14111 it again. */
14112 start_token->type = CPP_DECLTYPE;
14113 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14114 start_token->u.tree_check_value->value = expr;
14115 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14116 start_token->keyword = RID_MAX;
14117 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14119 return expr;
14122 /* Special member functions [gram.special] */
14124 /* Parse a conversion-function-id.
14126 conversion-function-id:
14127 operator conversion-type-id
14129 Returns an IDENTIFIER_NODE representing the operator. */
14131 static tree
14132 cp_parser_conversion_function_id (cp_parser* parser)
14134 tree type;
14135 tree saved_scope;
14136 tree saved_qualifying_scope;
14137 tree saved_object_scope;
14138 tree pushed_scope = NULL_TREE;
14140 /* Look for the `operator' token. */
14141 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14142 return error_mark_node;
14143 /* When we parse the conversion-type-id, the current scope will be
14144 reset. However, we need that information in able to look up the
14145 conversion function later, so we save it here. */
14146 saved_scope = parser->scope;
14147 saved_qualifying_scope = parser->qualifying_scope;
14148 saved_object_scope = parser->object_scope;
14149 /* We must enter the scope of the class so that the names of
14150 entities declared within the class are available in the
14151 conversion-type-id. For example, consider:
14153 struct S {
14154 typedef int I;
14155 operator I();
14158 S::operator I() { ... }
14160 In order to see that `I' is a type-name in the definition, we
14161 must be in the scope of `S'. */
14162 if (saved_scope)
14163 pushed_scope = push_scope (saved_scope);
14164 /* Parse the conversion-type-id. */
14165 type = cp_parser_conversion_type_id (parser);
14166 /* Leave the scope of the class, if any. */
14167 if (pushed_scope)
14168 pop_scope (pushed_scope);
14169 /* Restore the saved scope. */
14170 parser->scope = saved_scope;
14171 parser->qualifying_scope = saved_qualifying_scope;
14172 parser->object_scope = saved_object_scope;
14173 /* If the TYPE is invalid, indicate failure. */
14174 if (type == error_mark_node)
14175 return error_mark_node;
14176 return make_conv_op_name (type);
14179 /* Parse a conversion-type-id:
14181 conversion-type-id:
14182 type-specifier-seq conversion-declarator [opt]
14184 Returns the TYPE specified. */
14186 static tree
14187 cp_parser_conversion_type_id (cp_parser* parser)
14189 tree attributes;
14190 cp_decl_specifier_seq type_specifiers;
14191 cp_declarator *declarator;
14192 tree type_specified;
14193 const char *saved_message;
14195 /* Parse the attributes. */
14196 attributes = cp_parser_attributes_opt (parser);
14198 saved_message = parser->type_definition_forbidden_message;
14199 parser->type_definition_forbidden_message
14200 = G_("types may not be defined in a conversion-type-id");
14202 /* Parse the type-specifiers. */
14203 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14204 /*is_trailing_return=*/false,
14205 &type_specifiers);
14207 parser->type_definition_forbidden_message = saved_message;
14209 /* If that didn't work, stop. */
14210 if (type_specifiers.type == error_mark_node)
14211 return error_mark_node;
14212 /* Parse the conversion-declarator. */
14213 declarator = cp_parser_conversion_declarator_opt (parser);
14215 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14216 /*initialized=*/0, &attributes);
14217 if (attributes)
14218 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14220 /* Don't give this error when parsing tentatively. This happens to
14221 work because we always parse this definitively once. */
14222 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14223 && type_uses_auto (type_specified))
14225 if (cxx_dialect < cxx14)
14227 error ("invalid use of %<auto%> in conversion operator");
14228 return error_mark_node;
14230 else if (template_parm_scope_p ())
14231 warning (0, "use of %<auto%> in member template "
14232 "conversion operator can never be deduced");
14235 return type_specified;
14238 /* Parse an (optional) conversion-declarator.
14240 conversion-declarator:
14241 ptr-operator conversion-declarator [opt]
14245 static cp_declarator *
14246 cp_parser_conversion_declarator_opt (cp_parser* parser)
14248 enum tree_code code;
14249 tree class_type, std_attributes = NULL_TREE;
14250 cp_cv_quals cv_quals;
14252 /* We don't know if there's a ptr-operator next, or not. */
14253 cp_parser_parse_tentatively (parser);
14254 /* Try the ptr-operator. */
14255 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14256 &std_attributes);
14257 /* If it worked, look for more conversion-declarators. */
14258 if (cp_parser_parse_definitely (parser))
14260 cp_declarator *declarator;
14262 /* Parse another optional declarator. */
14263 declarator = cp_parser_conversion_declarator_opt (parser);
14265 declarator = cp_parser_make_indirect_declarator
14266 (code, class_type, cv_quals, declarator, std_attributes);
14268 return declarator;
14271 return NULL;
14274 /* Parse an (optional) ctor-initializer.
14276 ctor-initializer:
14277 : mem-initializer-list */
14279 static void
14280 cp_parser_ctor_initializer_opt (cp_parser* parser)
14282 /* If the next token is not a `:', then there is no
14283 ctor-initializer. */
14284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14286 /* Do default initialization of any bases and members. */
14287 if (DECL_CONSTRUCTOR_P (current_function_decl))
14288 finish_mem_initializers (NULL_TREE);
14289 return;
14292 /* Consume the `:' token. */
14293 cp_lexer_consume_token (parser->lexer);
14294 /* And the mem-initializer-list. */
14295 cp_parser_mem_initializer_list (parser);
14298 /* Parse a mem-initializer-list.
14300 mem-initializer-list:
14301 mem-initializer ... [opt]
14302 mem-initializer ... [opt] , mem-initializer-list */
14304 static void
14305 cp_parser_mem_initializer_list (cp_parser* parser)
14307 tree mem_initializer_list = NULL_TREE;
14308 tree target_ctor = error_mark_node;
14309 cp_token *token = cp_lexer_peek_token (parser->lexer);
14311 /* Let the semantic analysis code know that we are starting the
14312 mem-initializer-list. */
14313 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14314 error_at (token->location,
14315 "only constructors take member initializers");
14317 /* Loop through the list. */
14318 while (true)
14320 tree mem_initializer;
14322 token = cp_lexer_peek_token (parser->lexer);
14323 /* Parse the mem-initializer. */
14324 mem_initializer = cp_parser_mem_initializer (parser);
14325 /* If the next token is a `...', we're expanding member initializers. */
14326 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14328 /* Consume the `...'. */
14329 cp_lexer_consume_token (parser->lexer);
14331 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14332 can be expanded but members cannot. */
14333 if (mem_initializer != error_mark_node
14334 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14336 error_at (token->location,
14337 "cannot expand initializer for member %qD",
14338 TREE_PURPOSE (mem_initializer));
14339 mem_initializer = error_mark_node;
14342 /* Construct the pack expansion type. */
14343 if (mem_initializer != error_mark_node)
14344 mem_initializer = make_pack_expansion (mem_initializer);
14346 if (target_ctor != error_mark_node
14347 && mem_initializer != error_mark_node)
14349 error ("mem-initializer for %qD follows constructor delegation",
14350 TREE_PURPOSE (mem_initializer));
14351 mem_initializer = error_mark_node;
14353 /* Look for a target constructor. */
14354 if (mem_initializer != error_mark_node
14355 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14356 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14358 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14359 if (mem_initializer_list)
14361 error ("constructor delegation follows mem-initializer for %qD",
14362 TREE_PURPOSE (mem_initializer_list));
14363 mem_initializer = error_mark_node;
14365 target_ctor = mem_initializer;
14367 /* Add it to the list, unless it was erroneous. */
14368 if (mem_initializer != error_mark_node)
14370 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14371 mem_initializer_list = mem_initializer;
14373 /* If the next token is not a `,', we're done. */
14374 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14375 break;
14376 /* Consume the `,' token. */
14377 cp_lexer_consume_token (parser->lexer);
14380 /* Perform semantic analysis. */
14381 if (DECL_CONSTRUCTOR_P (current_function_decl))
14382 finish_mem_initializers (mem_initializer_list);
14385 /* Parse a mem-initializer.
14387 mem-initializer:
14388 mem-initializer-id ( expression-list [opt] )
14389 mem-initializer-id braced-init-list
14391 GNU extension:
14393 mem-initializer:
14394 ( expression-list [opt] )
14396 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14397 class) or FIELD_DECL (for a non-static data member) to initialize;
14398 the TREE_VALUE is the expression-list. An empty initialization
14399 list is represented by void_list_node. */
14401 static tree
14402 cp_parser_mem_initializer (cp_parser* parser)
14404 tree mem_initializer_id;
14405 tree expression_list;
14406 tree member;
14407 cp_token *token = cp_lexer_peek_token (parser->lexer);
14409 /* Find out what is being initialized. */
14410 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14412 permerror (token->location,
14413 "anachronistic old-style base class initializer");
14414 mem_initializer_id = NULL_TREE;
14416 else
14418 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14419 if (mem_initializer_id == error_mark_node)
14420 return mem_initializer_id;
14422 member = expand_member_init (mem_initializer_id);
14423 if (member && !DECL_P (member))
14424 in_base_initializer = 1;
14426 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14428 bool expr_non_constant_p;
14429 cp_lexer_set_source_position (parser->lexer);
14430 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14431 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14432 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14433 expression_list = build_tree_list (NULL_TREE, expression_list);
14435 else
14437 vec<tree, va_gc> *vec;
14438 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14439 /*cast_p=*/false,
14440 /*allow_expansion_p=*/true,
14441 /*non_constant_p=*/NULL);
14442 if (vec == NULL)
14443 return error_mark_node;
14444 expression_list = build_tree_list_vec (vec);
14445 release_tree_vector (vec);
14448 if (expression_list == error_mark_node)
14449 return error_mark_node;
14450 if (!expression_list)
14451 expression_list = void_type_node;
14453 in_base_initializer = 0;
14455 return member ? build_tree_list (member, expression_list) : error_mark_node;
14458 /* Parse a mem-initializer-id.
14460 mem-initializer-id:
14461 :: [opt] nested-name-specifier [opt] class-name
14462 decltype-specifier (C++11)
14463 identifier
14465 Returns a TYPE indicating the class to be initialized for the first
14466 production (and the second in C++11). Returns an IDENTIFIER_NODE
14467 indicating the data member to be initialized for the last production. */
14469 static tree
14470 cp_parser_mem_initializer_id (cp_parser* parser)
14472 bool global_scope_p;
14473 bool nested_name_specifier_p;
14474 bool template_p = false;
14475 tree id;
14477 cp_token *token = cp_lexer_peek_token (parser->lexer);
14479 /* `typename' is not allowed in this context ([temp.res]). */
14480 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14482 error_at (token->location,
14483 "keyword %<typename%> not allowed in this context (a qualified "
14484 "member initializer is implicitly a type)");
14485 cp_lexer_consume_token (parser->lexer);
14487 /* Look for the optional `::' operator. */
14488 global_scope_p
14489 = (cp_parser_global_scope_opt (parser,
14490 /*current_scope_valid_p=*/false)
14491 != NULL_TREE);
14492 /* Look for the optional nested-name-specifier. The simplest way to
14493 implement:
14495 [temp.res]
14497 The keyword `typename' is not permitted in a base-specifier or
14498 mem-initializer; in these contexts a qualified name that
14499 depends on a template-parameter is implicitly assumed to be a
14500 type name.
14502 is to assume that we have seen the `typename' keyword at this
14503 point. */
14504 nested_name_specifier_p
14505 = (cp_parser_nested_name_specifier_opt (parser,
14506 /*typename_keyword_p=*/true,
14507 /*check_dependency_p=*/true,
14508 /*type_p=*/true,
14509 /*is_declaration=*/true)
14510 != NULL_TREE);
14511 if (nested_name_specifier_p)
14512 template_p = cp_parser_optional_template_keyword (parser);
14513 /* If there is a `::' operator or a nested-name-specifier, then we
14514 are definitely looking for a class-name. */
14515 if (global_scope_p || nested_name_specifier_p)
14516 return cp_parser_class_name (parser,
14517 /*typename_keyword_p=*/true,
14518 /*template_keyword_p=*/template_p,
14519 typename_type,
14520 /*check_dependency_p=*/true,
14521 /*class_head_p=*/false,
14522 /*is_declaration=*/true);
14523 /* Otherwise, we could also be looking for an ordinary identifier. */
14524 cp_parser_parse_tentatively (parser);
14525 if (cp_lexer_next_token_is_decltype (parser->lexer))
14526 /* Try a decltype-specifier. */
14527 id = cp_parser_decltype (parser);
14528 else
14529 /* Otherwise, try a class-name. */
14530 id = cp_parser_class_name (parser,
14531 /*typename_keyword_p=*/true,
14532 /*template_keyword_p=*/false,
14533 none_type,
14534 /*check_dependency_p=*/true,
14535 /*class_head_p=*/false,
14536 /*is_declaration=*/true);
14537 /* If we found one, we're done. */
14538 if (cp_parser_parse_definitely (parser))
14539 return id;
14540 /* Otherwise, look for an ordinary identifier. */
14541 return cp_parser_identifier (parser);
14544 /* Overloading [gram.over] */
14546 /* Parse an operator-function-id.
14548 operator-function-id:
14549 operator operator
14551 Returns an IDENTIFIER_NODE for the operator which is a
14552 human-readable spelling of the identifier, e.g., `operator +'. */
14554 static cp_expr
14555 cp_parser_operator_function_id (cp_parser* parser)
14557 /* Look for the `operator' keyword. */
14558 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14559 return error_mark_node;
14560 /* And then the name of the operator itself. */
14561 return cp_parser_operator (parser);
14564 /* Return an identifier node for a user-defined literal operator.
14565 The suffix identifier is chained to the operator name identifier. */
14567 tree
14568 cp_literal_operator_id (const char* name)
14570 tree identifier;
14571 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14572 + strlen (name) + 10);
14573 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14574 identifier = get_identifier (buffer);
14576 return identifier;
14579 /* Parse an operator.
14581 operator:
14582 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14583 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14584 || ++ -- , ->* -> () []
14586 GNU Extensions:
14588 operator:
14589 <? >? <?= >?=
14591 Returns an IDENTIFIER_NODE for the operator which is a
14592 human-readable spelling of the identifier, e.g., `operator +'. */
14594 static cp_expr
14595 cp_parser_operator (cp_parser* parser)
14597 tree id = NULL_TREE;
14598 cp_token *token;
14599 bool utf8 = false;
14601 /* Peek at the next token. */
14602 token = cp_lexer_peek_token (parser->lexer);
14604 location_t start_loc = token->location;
14606 /* Figure out which operator we have. */
14607 enum tree_code op = ERROR_MARK;
14608 bool assop = false;
14609 bool consumed = false;
14610 switch (token->type)
14612 case CPP_KEYWORD:
14614 /* The keyword should be either `new' or `delete'. */
14615 if (token->keyword == RID_NEW)
14616 op = NEW_EXPR;
14617 else if (token->keyword == RID_DELETE)
14618 op = DELETE_EXPR;
14619 else
14620 break;
14622 /* Consume the `new' or `delete' token. */
14623 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14625 /* Peek at the next token. */
14626 token = cp_lexer_peek_token (parser->lexer);
14627 /* If it's a `[' token then this is the array variant of the
14628 operator. */
14629 if (token->type == CPP_OPEN_SQUARE)
14631 /* Consume the `[' token. */
14632 cp_lexer_consume_token (parser->lexer);
14633 /* Look for the `]' token. */
14634 if (cp_token *close_token
14635 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14636 end_loc = close_token->location;
14637 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14639 start_loc = make_location (start_loc, start_loc, end_loc);
14640 consumed = true;
14641 break;
14644 case CPP_PLUS:
14645 op = PLUS_EXPR;
14646 break;
14648 case CPP_MINUS:
14649 op = MINUS_EXPR;
14650 break;
14652 case CPP_MULT:
14653 op = MULT_EXPR;
14654 break;
14656 case CPP_DIV:
14657 op = TRUNC_DIV_EXPR;
14658 break;
14660 case CPP_MOD:
14661 op = TRUNC_MOD_EXPR;
14662 break;
14664 case CPP_XOR:
14665 op = BIT_XOR_EXPR;
14666 break;
14668 case CPP_AND:
14669 op = BIT_AND_EXPR;
14670 break;
14672 case CPP_OR:
14673 op = BIT_IOR_EXPR;
14674 break;
14676 case CPP_COMPL:
14677 op = BIT_NOT_EXPR;
14678 break;
14680 case CPP_NOT:
14681 op = TRUTH_NOT_EXPR;
14682 break;
14684 case CPP_EQ:
14685 assop = true;
14686 op = NOP_EXPR;
14687 break;
14689 case CPP_LESS:
14690 op = LT_EXPR;
14691 break;
14693 case CPP_GREATER:
14694 op = GT_EXPR;
14695 break;
14697 case CPP_PLUS_EQ:
14698 assop = true;
14699 op = PLUS_EXPR;
14700 break;
14702 case CPP_MINUS_EQ:
14703 assop = true;
14704 op = MINUS_EXPR;
14705 break;
14707 case CPP_MULT_EQ:
14708 assop = true;
14709 op = MULT_EXPR;
14710 break;
14712 case CPP_DIV_EQ:
14713 assop = true;
14714 op = TRUNC_DIV_EXPR;
14715 break;
14717 case CPP_MOD_EQ:
14718 assop = true;
14719 op = TRUNC_MOD_EXPR;
14720 break;
14722 case CPP_XOR_EQ:
14723 assop = true;
14724 op = BIT_XOR_EXPR;
14725 break;
14727 case CPP_AND_EQ:
14728 assop = true;
14729 op = BIT_AND_EXPR;
14730 break;
14732 case CPP_OR_EQ:
14733 assop = true;
14734 op = BIT_IOR_EXPR;
14735 break;
14737 case CPP_LSHIFT:
14738 op = LSHIFT_EXPR;
14739 break;
14741 case CPP_RSHIFT:
14742 op = RSHIFT_EXPR;
14743 break;
14745 case CPP_LSHIFT_EQ:
14746 assop = true;
14747 op = LSHIFT_EXPR;
14748 break;
14750 case CPP_RSHIFT_EQ:
14751 assop = true;
14752 op = RSHIFT_EXPR;
14753 break;
14755 case CPP_EQ_EQ:
14756 op = EQ_EXPR;
14757 break;
14759 case CPP_NOT_EQ:
14760 op = NE_EXPR;
14761 break;
14763 case CPP_LESS_EQ:
14764 op = LE_EXPR;
14765 break;
14767 case CPP_GREATER_EQ:
14768 op = GE_EXPR;
14769 break;
14771 case CPP_AND_AND:
14772 op = TRUTH_ANDIF_EXPR;
14773 break;
14775 case CPP_OR_OR:
14776 op = TRUTH_ORIF_EXPR;
14777 break;
14779 case CPP_PLUS_PLUS:
14780 op = POSTINCREMENT_EXPR;
14781 break;
14783 case CPP_MINUS_MINUS:
14784 op = PREDECREMENT_EXPR;
14785 break;
14787 case CPP_COMMA:
14788 op = COMPOUND_EXPR;
14789 break;
14791 case CPP_DEREF_STAR:
14792 op = MEMBER_REF;
14793 break;
14795 case CPP_DEREF:
14796 op = COMPONENT_REF;
14797 break;
14799 case CPP_OPEN_PAREN:
14801 /* Consume the `('. */
14802 matching_parens parens;
14803 parens.consume_open (parser);
14804 /* Look for the matching `)'. */
14805 parens.require_close (parser);
14806 op = CALL_EXPR;
14807 consumed = true;
14808 break;
14811 case CPP_OPEN_SQUARE:
14812 /* Consume the `['. */
14813 cp_lexer_consume_token (parser->lexer);
14814 /* Look for the matching `]'. */
14815 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14816 op = ARRAY_REF;
14817 consumed = true;
14818 break;
14820 case CPP_UTF8STRING:
14821 case CPP_UTF8STRING_USERDEF:
14822 utf8 = true;
14823 /* FALLTHRU */
14824 case CPP_STRING:
14825 case CPP_WSTRING:
14826 case CPP_STRING16:
14827 case CPP_STRING32:
14828 case CPP_STRING_USERDEF:
14829 case CPP_WSTRING_USERDEF:
14830 case CPP_STRING16_USERDEF:
14831 case CPP_STRING32_USERDEF:
14833 tree str, string_tree;
14834 int sz, len;
14836 if (cxx_dialect == cxx98)
14837 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14839 /* Consume the string. */
14840 str = cp_parser_string_literal (parser, /*translate=*/true,
14841 /*wide_ok=*/true, /*lookup_udlit=*/false);
14842 if (str == error_mark_node)
14843 return error_mark_node;
14844 else if (TREE_CODE (str) == USERDEF_LITERAL)
14846 string_tree = USERDEF_LITERAL_VALUE (str);
14847 id = USERDEF_LITERAL_SUFFIX_ID (str);
14849 else
14851 string_tree = str;
14852 /* Look for the suffix identifier. */
14853 token = cp_lexer_peek_token (parser->lexer);
14854 if (token->type == CPP_NAME)
14855 id = cp_parser_identifier (parser);
14856 else if (token->type == CPP_KEYWORD)
14858 error ("unexpected keyword;"
14859 " remove space between quotes and suffix identifier");
14860 return error_mark_node;
14862 else
14864 error ("expected suffix identifier");
14865 return error_mark_node;
14868 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14869 (TREE_TYPE (TREE_TYPE (string_tree))));
14870 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14871 if (len != 0)
14873 error ("expected empty string after %<operator%> keyword");
14874 return error_mark_node;
14876 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14877 != char_type_node)
14879 error ("invalid encoding prefix in literal operator");
14880 return error_mark_node;
14882 if (id != error_mark_node)
14884 const char *name = IDENTIFIER_POINTER (id);
14885 id = cp_literal_operator_id (name);
14887 return id;
14890 default:
14891 /* Anything else is an error. */
14892 break;
14895 /* If we have selected an identifier, we need to consume the
14896 operator token. */
14897 if (op != ERROR_MARK)
14899 id = ovl_op_identifier (assop, op);
14900 if (!consumed)
14901 cp_lexer_consume_token (parser->lexer);
14903 /* Otherwise, no valid operator name was present. */
14904 else
14906 cp_parser_error (parser, "expected operator");
14907 id = error_mark_node;
14910 return cp_expr (id, start_loc);
14913 /* Parse a template-declaration.
14915 template-declaration:
14916 export [opt] template < template-parameter-list > declaration
14918 If MEMBER_P is TRUE, this template-declaration occurs within a
14919 class-specifier.
14921 The grammar rule given by the standard isn't correct. What
14922 is really meant is:
14924 template-declaration:
14925 export [opt] template-parameter-list-seq
14926 decl-specifier-seq [opt] init-declarator [opt] ;
14927 export [opt] template-parameter-list-seq
14928 function-definition
14930 template-parameter-list-seq:
14931 template-parameter-list-seq [opt]
14932 template < template-parameter-list >
14934 Concept Extensions:
14936 template-parameter-list-seq:
14937 template < template-parameter-list > requires-clause [opt]
14939 requires-clause:
14940 requires logical-or-expression */
14942 static void
14943 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14945 /* Check for `export'. */
14946 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14948 /* Consume the `export' token. */
14949 cp_lexer_consume_token (parser->lexer);
14950 /* Warn that we do not support `export'. */
14951 warning (0, "keyword %<export%> not implemented, and will be ignored");
14954 cp_parser_template_declaration_after_export (parser, member_p);
14957 /* Parse a template-parameter-list.
14959 template-parameter-list:
14960 template-parameter
14961 template-parameter-list , template-parameter
14963 Returns a TREE_LIST. Each node represents a template parameter.
14964 The nodes are connected via their TREE_CHAINs. */
14966 static tree
14967 cp_parser_template_parameter_list (cp_parser* parser)
14969 tree parameter_list = NULL_TREE;
14971 begin_template_parm_list ();
14973 /* The loop below parses the template parms. We first need to know
14974 the total number of template parms to be able to compute proper
14975 canonical types of each dependent type. So after the loop, when
14976 we know the total number of template parms,
14977 end_template_parm_list computes the proper canonical types and
14978 fixes up the dependent types accordingly. */
14979 while (true)
14981 tree parameter;
14982 bool is_non_type;
14983 bool is_parameter_pack;
14984 location_t parm_loc;
14986 /* Parse the template-parameter. */
14987 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14988 parameter = cp_parser_template_parameter (parser,
14989 &is_non_type,
14990 &is_parameter_pack);
14991 /* Add it to the list. */
14992 if (parameter != error_mark_node)
14993 parameter_list = process_template_parm (parameter_list,
14994 parm_loc,
14995 parameter,
14996 is_non_type,
14997 is_parameter_pack);
14998 else
15000 tree err_parm = build_tree_list (parameter, parameter);
15001 parameter_list = chainon (parameter_list, err_parm);
15004 /* If the next token is not a `,', we're done. */
15005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15006 break;
15007 /* Otherwise, consume the `,' token. */
15008 cp_lexer_consume_token (parser->lexer);
15011 return end_template_parm_list (parameter_list);
15014 /* Parse a introduction-list.
15016 introduction-list:
15017 introduced-parameter
15018 introduction-list , introduced-parameter
15020 introduced-parameter:
15021 ...[opt] identifier
15023 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15024 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15025 WILDCARD_DECL will also have DECL_NAME set and token location in
15026 DECL_SOURCE_LOCATION. */
15028 static tree
15029 cp_parser_introduction_list (cp_parser *parser)
15031 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15033 while (true)
15035 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15036 if (is_pack)
15037 cp_lexer_consume_token (parser->lexer);
15039 /* Build placeholder. */
15040 tree parm = build_nt (WILDCARD_DECL);
15041 DECL_SOURCE_LOCATION (parm)
15042 = cp_lexer_peek_token (parser->lexer)->location;
15043 DECL_NAME (parm) = cp_parser_identifier (parser);
15044 WILDCARD_PACK_P (parm) = is_pack;
15045 vec_safe_push (introduction_vec, parm);
15047 /* If the next token is not a `,', we're done. */
15048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15049 break;
15050 /* Otherwise, consume the `,' token. */
15051 cp_lexer_consume_token (parser->lexer);
15054 /* Convert the vec into a TREE_VEC. */
15055 tree introduction_list = make_tree_vec (introduction_vec->length ());
15056 unsigned int n;
15057 tree parm;
15058 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15059 TREE_VEC_ELT (introduction_list, n) = parm;
15061 release_tree_vector (introduction_vec);
15062 return introduction_list;
15065 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15066 is an abstract declarator. */
15068 static inline cp_declarator*
15069 get_id_declarator (cp_declarator *declarator)
15071 cp_declarator *d = declarator;
15072 while (d && d->kind != cdk_id)
15073 d = d->declarator;
15074 return d;
15077 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15078 is an abstract declarator. */
15080 static inline tree
15081 get_unqualified_id (cp_declarator *declarator)
15083 declarator = get_id_declarator (declarator);
15084 if (declarator)
15085 return declarator->u.id.unqualified_name;
15086 else
15087 return NULL_TREE;
15090 /* Returns true if DECL represents a constrained-parameter. */
15092 static inline bool
15093 is_constrained_parameter (tree decl)
15095 return (decl
15096 && TREE_CODE (decl) == TYPE_DECL
15097 && CONSTRAINED_PARM_CONCEPT (decl)
15098 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15101 /* Returns true if PARM declares a constrained-parameter. */
15103 static inline bool
15104 is_constrained_parameter (cp_parameter_declarator *parm)
15106 return is_constrained_parameter (parm->decl_specifiers.type);
15109 /* Check that the type parameter is only a declarator-id, and that its
15110 type is not cv-qualified. */
15112 bool
15113 cp_parser_check_constrained_type_parm (cp_parser *parser,
15114 cp_parameter_declarator *parm)
15116 if (!parm->declarator)
15117 return true;
15119 if (parm->declarator->kind != cdk_id)
15121 cp_parser_error (parser, "invalid constrained type parameter");
15122 return false;
15125 /* Don't allow cv-qualified type parameters. */
15126 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15127 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15129 cp_parser_error (parser, "cv-qualified type parameter");
15130 return false;
15133 return true;
15136 /* Finish parsing/processing a template type parameter and checking
15137 various restrictions. */
15139 static inline tree
15140 cp_parser_constrained_type_template_parm (cp_parser *parser,
15141 tree id,
15142 cp_parameter_declarator* parmdecl)
15144 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15145 return finish_template_type_parm (class_type_node, id);
15146 else
15147 return error_mark_node;
15150 static tree
15151 finish_constrained_template_template_parm (tree proto, tree id)
15153 /* FIXME: This should probably be copied, and we may need to adjust
15154 the template parameter depths. */
15155 tree saved_parms = current_template_parms;
15156 begin_template_parm_list ();
15157 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15158 end_template_parm_list ();
15160 tree parm = finish_template_template_parm (class_type_node, id);
15161 current_template_parms = saved_parms;
15163 return parm;
15166 /* Finish parsing/processing a template template parameter by borrowing
15167 the template parameter list from the prototype parameter. */
15169 static tree
15170 cp_parser_constrained_template_template_parm (cp_parser *parser,
15171 tree proto,
15172 tree id,
15173 cp_parameter_declarator *parmdecl)
15175 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15176 return error_mark_node;
15177 return finish_constrained_template_template_parm (proto, id);
15180 /* Create a new non-type template parameter from the given PARM
15181 declarator. */
15183 static tree
15184 constrained_non_type_template_parm (bool *is_non_type,
15185 cp_parameter_declarator *parm)
15187 *is_non_type = true;
15188 cp_declarator *decl = parm->declarator;
15189 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15190 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15191 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15194 /* Build a constrained template parameter based on the PARMDECL
15195 declarator. The type of PARMDECL is the constrained type, which
15196 refers to the prototype template parameter that ultimately
15197 specifies the type of the declared parameter. */
15199 static tree
15200 finish_constrained_parameter (cp_parser *parser,
15201 cp_parameter_declarator *parmdecl,
15202 bool *is_non_type,
15203 bool *is_parameter_pack)
15205 tree decl = parmdecl->decl_specifiers.type;
15206 tree id = get_unqualified_id (parmdecl->declarator);
15207 tree def = parmdecl->default_argument;
15208 tree proto = DECL_INITIAL (decl);
15210 /* A template parameter constrained by a variadic concept shall also
15211 be declared as a template parameter pack. */
15212 bool is_variadic = template_parameter_pack_p (proto);
15213 if (is_variadic && !*is_parameter_pack)
15214 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15216 /* Build the parameter. Return an error if the declarator was invalid. */
15217 tree parm;
15218 if (TREE_CODE (proto) == TYPE_DECL)
15219 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15220 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15221 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15222 parmdecl);
15223 else
15224 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15225 if (parm == error_mark_node)
15226 return error_mark_node;
15228 /* Finish the parameter decl and create a node attaching the
15229 default argument and constraint. */
15230 parm = build_tree_list (def, parm);
15231 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15233 return parm;
15236 /* Returns true if the parsed type actually represents the declaration
15237 of a type template-parameter. */
15239 static inline bool
15240 declares_constrained_type_template_parameter (tree type)
15242 return (is_constrained_parameter (type)
15243 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15247 /* Returns true if the parsed type actually represents the declaration of
15248 a template template-parameter. */
15250 static bool
15251 declares_constrained_template_template_parameter (tree type)
15253 return (is_constrained_parameter (type)
15254 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15257 /* Parse a default argument for a type template-parameter.
15258 Note that diagnostics are handled in cp_parser_template_parameter. */
15260 static tree
15261 cp_parser_default_type_template_argument (cp_parser *parser)
15263 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15265 /* Consume the `=' token. */
15266 cp_lexer_consume_token (parser->lexer);
15268 cp_token *token = cp_lexer_peek_token (parser->lexer);
15270 /* Parse the default-argument. */
15271 push_deferring_access_checks (dk_no_deferred);
15272 tree default_argument = cp_parser_type_id (parser);
15273 pop_deferring_access_checks ();
15275 if (flag_concepts && type_uses_auto (default_argument))
15277 error_at (token->location,
15278 "invalid use of %<auto%> in default template argument");
15279 return error_mark_node;
15282 return default_argument;
15285 /* Parse a default argument for a template template-parameter. */
15287 static tree
15288 cp_parser_default_template_template_argument (cp_parser *parser)
15290 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15292 bool is_template;
15294 /* Consume the `='. */
15295 cp_lexer_consume_token (parser->lexer);
15296 /* Parse the id-expression. */
15297 push_deferring_access_checks (dk_no_deferred);
15298 /* save token before parsing the id-expression, for error
15299 reporting */
15300 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15301 tree default_argument
15302 = cp_parser_id_expression (parser,
15303 /*template_keyword_p=*/false,
15304 /*check_dependency_p=*/true,
15305 /*template_p=*/&is_template,
15306 /*declarator_p=*/false,
15307 /*optional_p=*/false);
15308 if (TREE_CODE (default_argument) == TYPE_DECL)
15309 /* If the id-expression was a template-id that refers to
15310 a template-class, we already have the declaration here,
15311 so no further lookup is needed. */
15313 else
15314 /* Look up the name. */
15315 default_argument
15316 = cp_parser_lookup_name (parser, default_argument,
15317 none_type,
15318 /*is_template=*/is_template,
15319 /*is_namespace=*/false,
15320 /*check_dependency=*/true,
15321 /*ambiguous_decls=*/NULL,
15322 token->location);
15323 /* See if the default argument is valid. */
15324 default_argument = check_template_template_default_arg (default_argument);
15325 pop_deferring_access_checks ();
15326 return default_argument;
15329 /* Parse a template-parameter.
15331 template-parameter:
15332 type-parameter
15333 parameter-declaration
15335 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15336 the parameter. The TREE_PURPOSE is the default value, if any.
15337 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15338 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15339 set to true iff this parameter is a parameter pack. */
15341 static tree
15342 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15343 bool *is_parameter_pack)
15345 cp_token *token;
15346 cp_parameter_declarator *parameter_declarator;
15347 tree parm;
15349 /* Assume it is a type parameter or a template parameter. */
15350 *is_non_type = false;
15351 /* Assume it not a parameter pack. */
15352 *is_parameter_pack = false;
15353 /* Peek at the next token. */
15354 token = cp_lexer_peek_token (parser->lexer);
15355 /* If it is `template', we have a type-parameter. */
15356 if (token->keyword == RID_TEMPLATE)
15357 return cp_parser_type_parameter (parser, is_parameter_pack);
15358 /* If it is `class' or `typename' we do not know yet whether it is a
15359 type parameter or a non-type parameter. Consider:
15361 template <typename T, typename T::X X> ...
15365 template <class C, class D*> ...
15367 Here, the first parameter is a type parameter, and the second is
15368 a non-type parameter. We can tell by looking at the token after
15369 the identifier -- if it is a `,', `=', or `>' then we have a type
15370 parameter. */
15371 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15373 /* Peek at the token after `class' or `typename'. */
15374 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15375 /* If it's an ellipsis, we have a template type parameter
15376 pack. */
15377 if (token->type == CPP_ELLIPSIS)
15378 return cp_parser_type_parameter (parser, is_parameter_pack);
15379 /* If it's an identifier, skip it. */
15380 if (token->type == CPP_NAME)
15381 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15382 /* Now, see if the token looks like the end of a template
15383 parameter. */
15384 if (token->type == CPP_COMMA
15385 || token->type == CPP_EQ
15386 || token->type == CPP_GREATER)
15387 return cp_parser_type_parameter (parser, is_parameter_pack);
15390 /* Otherwise, it is a non-type parameter or a constrained parameter.
15392 [temp.param]
15394 When parsing a default template-argument for a non-type
15395 template-parameter, the first non-nested `>' is taken as the end
15396 of the template parameter-list rather than a greater-than
15397 operator. */
15398 parameter_declarator
15399 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15400 /*parenthesized_p=*/NULL);
15402 if (!parameter_declarator)
15403 return error_mark_node;
15405 /* If the parameter declaration is marked as a parameter pack, set
15406 *IS_PARAMETER_PACK to notify the caller. */
15407 if (parameter_declarator->template_parameter_pack_p)
15408 *is_parameter_pack = true;
15410 if (parameter_declarator->default_argument)
15412 /* Can happen in some cases of erroneous input (c++/34892). */
15413 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15414 /* Consume the `...' for better error recovery. */
15415 cp_lexer_consume_token (parser->lexer);
15418 // The parameter may have been constrained.
15419 if (is_constrained_parameter (parameter_declarator))
15420 return finish_constrained_parameter (parser,
15421 parameter_declarator,
15422 is_non_type,
15423 is_parameter_pack);
15425 // Now we're sure that the parameter is a non-type parameter.
15426 *is_non_type = true;
15428 parm = grokdeclarator (parameter_declarator->declarator,
15429 &parameter_declarator->decl_specifiers,
15430 TPARM, /*initialized=*/0,
15431 /*attrlist=*/NULL);
15432 if (parm == error_mark_node)
15433 return error_mark_node;
15435 return build_tree_list (parameter_declarator->default_argument, parm);
15438 /* Parse a type-parameter.
15440 type-parameter:
15441 class identifier [opt]
15442 class identifier [opt] = type-id
15443 typename identifier [opt]
15444 typename identifier [opt] = type-id
15445 template < template-parameter-list > class identifier [opt]
15446 template < template-parameter-list > class identifier [opt]
15447 = id-expression
15449 GNU Extension (variadic templates):
15451 type-parameter:
15452 class ... identifier [opt]
15453 typename ... identifier [opt]
15455 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15456 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15457 the declaration of the parameter.
15459 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15461 static tree
15462 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15464 cp_token *token;
15465 tree parameter;
15467 /* Look for a keyword to tell us what kind of parameter this is. */
15468 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15469 if (!token)
15470 return error_mark_node;
15472 switch (token->keyword)
15474 case RID_CLASS:
15475 case RID_TYPENAME:
15477 tree identifier;
15478 tree default_argument;
15480 /* If the next token is an ellipsis, we have a template
15481 argument pack. */
15482 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15484 /* Consume the `...' token. */
15485 cp_lexer_consume_token (parser->lexer);
15486 maybe_warn_variadic_templates ();
15488 *is_parameter_pack = true;
15491 /* If the next token is an identifier, then it names the
15492 parameter. */
15493 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15494 identifier = cp_parser_identifier (parser);
15495 else
15496 identifier = NULL_TREE;
15498 /* Create the parameter. */
15499 parameter = finish_template_type_parm (class_type_node, identifier);
15501 /* If the next token is an `=', we have a default argument. */
15502 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15504 default_argument
15505 = cp_parser_default_type_template_argument (parser);
15507 /* Template parameter packs cannot have default
15508 arguments. */
15509 if (*is_parameter_pack)
15511 if (identifier)
15512 error_at (token->location,
15513 "template parameter pack %qD cannot have a "
15514 "default argument", identifier);
15515 else
15516 error_at (token->location,
15517 "template parameter packs cannot have "
15518 "default arguments");
15519 default_argument = NULL_TREE;
15521 else if (check_for_bare_parameter_packs (default_argument))
15522 default_argument = error_mark_node;
15524 else
15525 default_argument = NULL_TREE;
15527 /* Create the combined representation of the parameter and the
15528 default argument. */
15529 parameter = build_tree_list (default_argument, parameter);
15531 break;
15533 case RID_TEMPLATE:
15535 tree identifier;
15536 tree default_argument;
15538 /* Look for the `<'. */
15539 cp_parser_require (parser, CPP_LESS, RT_LESS);
15540 /* Parse the template-parameter-list. */
15541 cp_parser_template_parameter_list (parser);
15542 /* Look for the `>'. */
15543 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15545 // If template requirements are present, parse them.
15546 if (flag_concepts)
15548 tree reqs = get_shorthand_constraints (current_template_parms);
15549 if (tree r = cp_parser_requires_clause_opt (parser))
15550 reqs = conjoin_constraints (reqs, normalize_expression (r));
15551 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15554 /* Look for the `class' or 'typename' keywords. */
15555 cp_parser_type_parameter_key (parser);
15556 /* If the next token is an ellipsis, we have a template
15557 argument pack. */
15558 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15560 /* Consume the `...' token. */
15561 cp_lexer_consume_token (parser->lexer);
15562 maybe_warn_variadic_templates ();
15564 *is_parameter_pack = true;
15566 /* If the next token is an `=', then there is a
15567 default-argument. If the next token is a `>', we are at
15568 the end of the parameter-list. If the next token is a `,',
15569 then we are at the end of this parameter. */
15570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15571 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15572 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15574 identifier = cp_parser_identifier (parser);
15575 /* Treat invalid names as if the parameter were nameless. */
15576 if (identifier == error_mark_node)
15577 identifier = NULL_TREE;
15579 else
15580 identifier = NULL_TREE;
15582 /* Create the template parameter. */
15583 parameter = finish_template_template_parm (class_type_node,
15584 identifier);
15586 /* If the next token is an `=', then there is a
15587 default-argument. */
15588 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15590 default_argument
15591 = cp_parser_default_template_template_argument (parser);
15593 /* Template parameter packs cannot have default
15594 arguments. */
15595 if (*is_parameter_pack)
15597 if (identifier)
15598 error_at (token->location,
15599 "template parameter pack %qD cannot "
15600 "have a default argument",
15601 identifier);
15602 else
15603 error_at (token->location, "template parameter packs cannot "
15604 "have default arguments");
15605 default_argument = NULL_TREE;
15608 else
15609 default_argument = NULL_TREE;
15611 /* Create the combined representation of the parameter and the
15612 default argument. */
15613 parameter = build_tree_list (default_argument, parameter);
15615 break;
15617 default:
15618 gcc_unreachable ();
15619 break;
15622 return parameter;
15625 /* Parse a template-id.
15627 template-id:
15628 template-name < template-argument-list [opt] >
15630 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15631 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15632 returned. Otherwise, if the template-name names a function, or set
15633 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15634 names a class, returns a TYPE_DECL for the specialization.
15636 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15637 uninstantiated templates. */
15639 static tree
15640 cp_parser_template_id (cp_parser *parser,
15641 bool template_keyword_p,
15642 bool check_dependency_p,
15643 enum tag_types tag_type,
15644 bool is_declaration)
15646 tree templ;
15647 tree arguments;
15648 tree template_id;
15649 cp_token_position start_of_id = 0;
15650 cp_token *next_token = NULL, *next_token_2 = NULL;
15651 bool is_identifier;
15653 /* If the next token corresponds to a template-id, there is no need
15654 to reparse it. */
15655 cp_token *token = cp_lexer_peek_token (parser->lexer);
15656 if (token->type == CPP_TEMPLATE_ID)
15658 cp_lexer_consume_token (parser->lexer);
15659 return saved_checks_value (token->u.tree_check_value);
15662 /* Avoid performing name lookup if there is no possibility of
15663 finding a template-id. */
15664 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15665 || (token->type == CPP_NAME
15666 && !cp_parser_nth_token_starts_template_argument_list_p
15667 (parser, 2)))
15669 cp_parser_error (parser, "expected template-id");
15670 return error_mark_node;
15673 /* Remember where the template-id starts. */
15674 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15675 start_of_id = cp_lexer_token_position (parser->lexer, false);
15677 push_deferring_access_checks (dk_deferred);
15679 /* Parse the template-name. */
15680 is_identifier = false;
15681 templ = cp_parser_template_name (parser, template_keyword_p,
15682 check_dependency_p,
15683 is_declaration,
15684 tag_type,
15685 &is_identifier);
15686 if (templ == error_mark_node || is_identifier)
15688 pop_deferring_access_checks ();
15689 return templ;
15692 /* Since we're going to preserve any side-effects from this parse, set up a
15693 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15694 in the template arguments. */
15695 tentative_firewall firewall (parser);
15697 /* If we find the sequence `[:' after a template-name, it's probably
15698 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15699 parse correctly the argument list. */
15700 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15701 == CPP_OPEN_SQUARE)
15702 && next_token->flags & DIGRAPH
15703 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15704 == CPP_COLON)
15705 && !(next_token_2->flags & PREV_WHITE))
15707 cp_parser_parse_tentatively (parser);
15708 /* Change `:' into `::'. */
15709 next_token_2->type = CPP_SCOPE;
15710 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15711 CPP_LESS. */
15712 cp_lexer_consume_token (parser->lexer);
15714 /* Parse the arguments. */
15715 arguments = cp_parser_enclosed_template_argument_list (parser);
15716 if (!cp_parser_parse_definitely (parser))
15718 /* If we couldn't parse an argument list, then we revert our changes
15719 and return simply an error. Maybe this is not a template-id
15720 after all. */
15721 next_token_2->type = CPP_COLON;
15722 cp_parser_error (parser, "expected %<<%>");
15723 pop_deferring_access_checks ();
15724 return error_mark_node;
15726 /* Otherwise, emit an error about the invalid digraph, but continue
15727 parsing because we got our argument list. */
15728 if (permerror (next_token->location,
15729 "%<<::%> cannot begin a template-argument list"))
15731 static bool hint = false;
15732 inform (next_token->location,
15733 "%<<:%> is an alternate spelling for %<[%>."
15734 " Insert whitespace between %<<%> and %<::%>");
15735 if (!hint && !flag_permissive)
15737 inform (next_token->location, "(if you use %<-fpermissive%> "
15738 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15739 "accept your code)");
15740 hint = true;
15744 else
15746 /* Look for the `<' that starts the template-argument-list. */
15747 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15749 pop_deferring_access_checks ();
15750 return error_mark_node;
15752 /* Parse the arguments. */
15753 arguments = cp_parser_enclosed_template_argument_list (parser);
15756 /* Set the location to be of the form:
15757 template-name < template-argument-list [opt] >
15758 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15759 with caret == start at the start of the template-name,
15760 ranging until the closing '>'. */
15761 location_t finish_loc
15762 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15763 location_t combined_loc
15764 = make_location (token->location, token->location, finish_loc);
15766 /* Build a representation of the specialization. */
15767 if (identifier_p (templ))
15768 template_id = build_min_nt_loc (combined_loc,
15769 TEMPLATE_ID_EXPR,
15770 templ, arguments);
15771 else if (DECL_TYPE_TEMPLATE_P (templ)
15772 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15774 bool entering_scope;
15775 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15776 template (rather than some instantiation thereof) only if
15777 is not nested within some other construct. For example, in
15778 "template <typename T> void f(T) { A<T>::", A<T> is just an
15779 instantiation of A. */
15780 entering_scope = (template_parm_scope_p ()
15781 && cp_lexer_next_token_is (parser->lexer,
15782 CPP_SCOPE));
15783 template_id
15784 = finish_template_type (templ, arguments, entering_scope);
15786 /* A template-like identifier may be a partial concept id. */
15787 else if (flag_concepts
15788 && (template_id = (cp_parser_maybe_partial_concept_id
15789 (parser, templ, arguments))))
15790 return template_id;
15791 else if (variable_template_p (templ))
15793 template_id = lookup_template_variable (templ, arguments);
15794 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15795 SET_EXPR_LOCATION (template_id, combined_loc);
15797 else
15799 /* If it's not a class-template or a template-template, it should be
15800 a function-template. */
15801 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15802 || TREE_CODE (templ) == OVERLOAD
15803 || BASELINK_P (templ)));
15805 template_id = lookup_template_function (templ, arguments);
15806 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15807 SET_EXPR_LOCATION (template_id, combined_loc);
15810 /* If parsing tentatively, replace the sequence of tokens that makes
15811 up the template-id with a CPP_TEMPLATE_ID token. That way,
15812 should we re-parse the token stream, we will not have to repeat
15813 the effort required to do the parse, nor will we issue duplicate
15814 error messages about problems during instantiation of the
15815 template. */
15816 if (start_of_id
15817 /* Don't do this if we had a parse error in a declarator; re-parsing
15818 might succeed if a name changes meaning (60361). */
15819 && !(cp_parser_error_occurred (parser)
15820 && cp_parser_parsing_tentatively (parser)
15821 && parser->in_declarator_p))
15823 /* Reset the contents of the START_OF_ID token. */
15824 token->type = CPP_TEMPLATE_ID;
15825 token->location = combined_loc;
15827 /* We must mark the lookup as kept, so we don't throw it away on
15828 the first parse. */
15829 if (is_overloaded_fn (template_id))
15830 lookup_keep (get_fns (template_id), true);
15832 /* Retrieve any deferred checks. Do not pop this access checks yet
15833 so the memory will not be reclaimed during token replacing below. */
15834 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15835 token->u.tree_check_value->value = template_id;
15836 token->u.tree_check_value->checks = get_deferred_access_checks ();
15837 token->keyword = RID_MAX;
15839 /* Purge all subsequent tokens. */
15840 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15842 /* ??? Can we actually assume that, if template_id ==
15843 error_mark_node, we will have issued a diagnostic to the
15844 user, as opposed to simply marking the tentative parse as
15845 failed? */
15846 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15847 error_at (token->location, "parse error in template argument list");
15850 pop_to_parent_deferring_access_checks ();
15851 return template_id;
15854 /* Parse a template-name.
15856 template-name:
15857 identifier
15859 The standard should actually say:
15861 template-name:
15862 identifier
15863 operator-function-id
15865 A defect report has been filed about this issue.
15867 A conversion-function-id cannot be a template name because they cannot
15868 be part of a template-id. In fact, looking at this code:
15870 a.operator K<int>()
15872 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15873 It is impossible to call a templated conversion-function-id with an
15874 explicit argument list, since the only allowed template parameter is
15875 the type to which it is converting.
15877 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15878 `template' keyword, in a construction like:
15880 T::template f<3>()
15882 In that case `f' is taken to be a template-name, even though there
15883 is no way of knowing for sure.
15885 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15886 name refers to a set of overloaded functions, at least one of which
15887 is a template, or an IDENTIFIER_NODE with the name of the template,
15888 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15889 names are looked up inside uninstantiated templates. */
15891 static tree
15892 cp_parser_template_name (cp_parser* parser,
15893 bool template_keyword_p,
15894 bool check_dependency_p,
15895 bool is_declaration,
15896 enum tag_types tag_type,
15897 bool *is_identifier)
15899 tree identifier;
15900 tree decl;
15901 cp_token *token = cp_lexer_peek_token (parser->lexer);
15903 /* If the next token is `operator', then we have either an
15904 operator-function-id or a conversion-function-id. */
15905 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15907 /* We don't know whether we're looking at an
15908 operator-function-id or a conversion-function-id. */
15909 cp_parser_parse_tentatively (parser);
15910 /* Try an operator-function-id. */
15911 identifier = cp_parser_operator_function_id (parser);
15912 /* If that didn't work, try a conversion-function-id. */
15913 if (!cp_parser_parse_definitely (parser))
15915 cp_parser_error (parser, "expected template-name");
15916 return error_mark_node;
15919 /* Look for the identifier. */
15920 else
15921 identifier = cp_parser_identifier (parser);
15923 /* If we didn't find an identifier, we don't have a template-id. */
15924 if (identifier == error_mark_node)
15925 return error_mark_node;
15927 /* If the name immediately followed the `template' keyword, then it
15928 is a template-name. However, if the next token is not `<', then
15929 we do not treat it as a template-name, since it is not being used
15930 as part of a template-id. This enables us to handle constructs
15931 like:
15933 template <typename T> struct S { S(); };
15934 template <typename T> S<T>::S();
15936 correctly. We would treat `S' as a template -- if it were `S<T>'
15937 -- but we do not if there is no `<'. */
15939 if (processing_template_decl
15940 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15942 /* In a declaration, in a dependent context, we pretend that the
15943 "template" keyword was present in order to improve error
15944 recovery. For example, given:
15946 template <typename T> void f(T::X<int>);
15948 we want to treat "X<int>" as a template-id. */
15949 if (is_declaration
15950 && !template_keyword_p
15951 && parser->scope && TYPE_P (parser->scope)
15952 && check_dependency_p
15953 && dependent_scope_p (parser->scope)
15954 /* Do not do this for dtors (or ctors), since they never
15955 need the template keyword before their name. */
15956 && !constructor_name_p (identifier, parser->scope))
15958 cp_token_position start = 0;
15960 /* Explain what went wrong. */
15961 error_at (token->location, "non-template %qD used as template",
15962 identifier);
15963 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15964 parser->scope, identifier);
15965 /* If parsing tentatively, find the location of the "<" token. */
15966 if (cp_parser_simulate_error (parser))
15967 start = cp_lexer_token_position (parser->lexer, true);
15968 /* Parse the template arguments so that we can issue error
15969 messages about them. */
15970 cp_lexer_consume_token (parser->lexer);
15971 cp_parser_enclosed_template_argument_list (parser);
15972 /* Skip tokens until we find a good place from which to
15973 continue parsing. */
15974 cp_parser_skip_to_closing_parenthesis (parser,
15975 /*recovering=*/true,
15976 /*or_comma=*/true,
15977 /*consume_paren=*/false);
15978 /* If parsing tentatively, permanently remove the
15979 template argument list. That will prevent duplicate
15980 error messages from being issued about the missing
15981 "template" keyword. */
15982 if (start)
15983 cp_lexer_purge_tokens_after (parser->lexer, start);
15984 if (is_identifier)
15985 *is_identifier = true;
15986 parser->context->object_type = NULL_TREE;
15987 return identifier;
15990 /* If the "template" keyword is present, then there is generally
15991 no point in doing name-lookup, so we just return IDENTIFIER.
15992 But, if the qualifying scope is non-dependent then we can
15993 (and must) do name-lookup normally. */
15994 if (template_keyword_p)
15996 tree scope = (parser->scope ? parser->scope
15997 : parser->context->object_type);
15998 if (scope && TYPE_P (scope)
15999 && (!CLASS_TYPE_P (scope)
16000 || (check_dependency_p && dependent_type_p (scope))))
16002 /* We're optimizing away the call to cp_parser_lookup_name, but
16003 we still need to do this. */
16004 parser->context->object_type = NULL_TREE;
16005 return identifier;
16010 /* Look up the name. */
16011 decl = cp_parser_lookup_name (parser, identifier,
16012 tag_type,
16013 /*is_template=*/true,
16014 /*is_namespace=*/false,
16015 check_dependency_p,
16016 /*ambiguous_decls=*/NULL,
16017 token->location);
16019 decl = strip_using_decl (decl);
16021 /* If DECL is a template, then the name was a template-name. */
16022 if (TREE_CODE (decl) == TEMPLATE_DECL)
16024 if (TREE_DEPRECATED (decl)
16025 && deprecated_state != DEPRECATED_SUPPRESS)
16026 warn_deprecated_use (decl, NULL_TREE);
16028 else
16030 /* The standard does not explicitly indicate whether a name that
16031 names a set of overloaded declarations, some of which are
16032 templates, is a template-name. However, such a name should
16033 be a template-name; otherwise, there is no way to form a
16034 template-id for the overloaded templates. */
16035 bool found = false;
16037 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16038 !found && iter; ++iter)
16039 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16040 found = true;
16042 if (!found)
16044 /* The name does not name a template. */
16045 cp_parser_error (parser, "expected template-name");
16046 return error_mark_node;
16050 /* If DECL is dependent, and refers to a function, then just return
16051 its name; we will look it up again during template instantiation. */
16052 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16054 tree scope = ovl_scope (decl);
16055 if (TYPE_P (scope) && dependent_type_p (scope))
16056 return identifier;
16059 return decl;
16062 /* Parse a template-argument-list.
16064 template-argument-list:
16065 template-argument ... [opt]
16066 template-argument-list , template-argument ... [opt]
16068 Returns a TREE_VEC containing the arguments. */
16070 static tree
16071 cp_parser_template_argument_list (cp_parser* parser)
16073 tree fixed_args[10];
16074 unsigned n_args = 0;
16075 unsigned alloced = 10;
16076 tree *arg_ary = fixed_args;
16077 tree vec;
16078 bool saved_in_template_argument_list_p;
16079 bool saved_ice_p;
16080 bool saved_non_ice_p;
16082 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16083 parser->in_template_argument_list_p = true;
16084 /* Even if the template-id appears in an integral
16085 constant-expression, the contents of the argument list do
16086 not. */
16087 saved_ice_p = parser->integral_constant_expression_p;
16088 parser->integral_constant_expression_p = false;
16089 saved_non_ice_p = parser->non_integral_constant_expression_p;
16090 parser->non_integral_constant_expression_p = false;
16092 /* Parse the arguments. */
16095 tree argument;
16097 if (n_args)
16098 /* Consume the comma. */
16099 cp_lexer_consume_token (parser->lexer);
16101 /* Parse the template-argument. */
16102 argument = cp_parser_template_argument (parser);
16104 /* If the next token is an ellipsis, we're expanding a template
16105 argument pack. */
16106 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16108 if (argument == error_mark_node)
16110 cp_token *token = cp_lexer_peek_token (parser->lexer);
16111 error_at (token->location,
16112 "expected parameter pack before %<...%>");
16114 /* Consume the `...' token. */
16115 cp_lexer_consume_token (parser->lexer);
16117 /* Make the argument into a TYPE_PACK_EXPANSION or
16118 EXPR_PACK_EXPANSION. */
16119 argument = make_pack_expansion (argument);
16122 if (n_args == alloced)
16124 alloced *= 2;
16126 if (arg_ary == fixed_args)
16128 arg_ary = XNEWVEC (tree, alloced);
16129 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16131 else
16132 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16134 arg_ary[n_args++] = argument;
16136 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16138 vec = make_tree_vec (n_args);
16140 while (n_args--)
16141 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16143 if (arg_ary != fixed_args)
16144 free (arg_ary);
16145 parser->non_integral_constant_expression_p = saved_non_ice_p;
16146 parser->integral_constant_expression_p = saved_ice_p;
16147 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16148 if (CHECKING_P)
16149 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16150 return vec;
16153 /* Parse a template-argument.
16155 template-argument:
16156 assignment-expression
16157 type-id
16158 id-expression
16160 The representation is that of an assignment-expression, type-id, or
16161 id-expression -- except that the qualified id-expression is
16162 evaluated, so that the value returned is either a DECL or an
16163 OVERLOAD.
16165 Although the standard says "assignment-expression", it forbids
16166 throw-expressions or assignments in the template argument.
16167 Therefore, we use "conditional-expression" instead. */
16169 static tree
16170 cp_parser_template_argument (cp_parser* parser)
16172 tree argument;
16173 bool template_p;
16174 bool address_p;
16175 bool maybe_type_id = false;
16176 cp_token *token = NULL, *argument_start_token = NULL;
16177 location_t loc = 0;
16178 cp_id_kind idk;
16180 /* There's really no way to know what we're looking at, so we just
16181 try each alternative in order.
16183 [temp.arg]
16185 In a template-argument, an ambiguity between a type-id and an
16186 expression is resolved to a type-id, regardless of the form of
16187 the corresponding template-parameter.
16189 Therefore, we try a type-id first. */
16190 cp_parser_parse_tentatively (parser);
16191 argument = cp_parser_template_type_arg (parser);
16192 /* If there was no error parsing the type-id but the next token is a
16193 '>>', our behavior depends on which dialect of C++ we're
16194 parsing. In C++98, we probably found a typo for '> >'. But there
16195 are type-id which are also valid expressions. For instance:
16197 struct X { int operator >> (int); };
16198 template <int V> struct Foo {};
16199 Foo<X () >> 5> r;
16201 Here 'X()' is a valid type-id of a function type, but the user just
16202 wanted to write the expression "X() >> 5". Thus, we remember that we
16203 found a valid type-id, but we still try to parse the argument as an
16204 expression to see what happens.
16206 In C++0x, the '>>' will be considered two separate '>'
16207 tokens. */
16208 if (!cp_parser_error_occurred (parser)
16209 && cxx_dialect == cxx98
16210 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16212 maybe_type_id = true;
16213 cp_parser_abort_tentative_parse (parser);
16215 else
16217 /* If the next token isn't a `,' or a `>', then this argument wasn't
16218 really finished. This means that the argument is not a valid
16219 type-id. */
16220 if (!cp_parser_next_token_ends_template_argument_p (parser))
16221 cp_parser_error (parser, "expected template-argument");
16222 /* If that worked, we're done. */
16223 if (cp_parser_parse_definitely (parser))
16224 return argument;
16226 /* We're still not sure what the argument will be. */
16227 cp_parser_parse_tentatively (parser);
16228 /* Try a template. */
16229 argument_start_token = cp_lexer_peek_token (parser->lexer);
16230 argument = cp_parser_id_expression (parser,
16231 /*template_keyword_p=*/false,
16232 /*check_dependency_p=*/true,
16233 &template_p,
16234 /*declarator_p=*/false,
16235 /*optional_p=*/false);
16236 /* If the next token isn't a `,' or a `>', then this argument wasn't
16237 really finished. */
16238 if (!cp_parser_next_token_ends_template_argument_p (parser))
16239 cp_parser_error (parser, "expected template-argument");
16240 if (!cp_parser_error_occurred (parser))
16242 /* Figure out what is being referred to. If the id-expression
16243 was for a class template specialization, then we will have a
16244 TYPE_DECL at this point. There is no need to do name lookup
16245 at this point in that case. */
16246 if (TREE_CODE (argument) != TYPE_DECL)
16247 argument = cp_parser_lookup_name (parser, argument,
16248 none_type,
16249 /*is_template=*/template_p,
16250 /*is_namespace=*/false,
16251 /*check_dependency=*/true,
16252 /*ambiguous_decls=*/NULL,
16253 argument_start_token->location);
16254 /* Handle a constrained-type-specifier for a non-type template
16255 parameter. */
16256 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16257 argument = decl;
16258 else if (TREE_CODE (argument) != TEMPLATE_DECL
16259 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16260 cp_parser_error (parser, "expected template-name");
16262 if (cp_parser_parse_definitely (parser))
16264 if (TREE_DEPRECATED (argument))
16265 warn_deprecated_use (argument, NULL_TREE);
16266 return argument;
16268 /* It must be a non-type argument. In C++17 any constant-expression is
16269 allowed. */
16270 if (cxx_dialect > cxx14)
16271 goto general_expr;
16273 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16275 -- an integral constant-expression of integral or enumeration
16276 type; or
16278 -- the name of a non-type template-parameter; or
16280 -- the name of an object or function with external linkage...
16282 -- the address of an object or function with external linkage...
16284 -- a pointer to member... */
16285 /* Look for a non-type template parameter. */
16286 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16288 cp_parser_parse_tentatively (parser);
16289 argument = cp_parser_primary_expression (parser,
16290 /*address_p=*/false,
16291 /*cast_p=*/false,
16292 /*template_arg_p=*/true,
16293 &idk);
16294 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16295 || !cp_parser_next_token_ends_template_argument_p (parser))
16296 cp_parser_simulate_error (parser);
16297 if (cp_parser_parse_definitely (parser))
16298 return argument;
16301 /* If the next token is "&", the argument must be the address of an
16302 object or function with external linkage. */
16303 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16304 if (address_p)
16306 loc = cp_lexer_peek_token (parser->lexer)->location;
16307 cp_lexer_consume_token (parser->lexer);
16309 /* See if we might have an id-expression. */
16310 token = cp_lexer_peek_token (parser->lexer);
16311 if (token->type == CPP_NAME
16312 || token->keyword == RID_OPERATOR
16313 || token->type == CPP_SCOPE
16314 || token->type == CPP_TEMPLATE_ID
16315 || token->type == CPP_NESTED_NAME_SPECIFIER)
16317 cp_parser_parse_tentatively (parser);
16318 argument = cp_parser_primary_expression (parser,
16319 address_p,
16320 /*cast_p=*/false,
16321 /*template_arg_p=*/true,
16322 &idk);
16323 if (cp_parser_error_occurred (parser)
16324 || !cp_parser_next_token_ends_template_argument_p (parser))
16325 cp_parser_abort_tentative_parse (parser);
16326 else
16328 tree probe;
16330 if (INDIRECT_REF_P (argument))
16332 /* Strip the dereference temporarily. */
16333 gcc_assert (REFERENCE_REF_P (argument));
16334 argument = TREE_OPERAND (argument, 0);
16337 /* If we're in a template, we represent a qualified-id referring
16338 to a static data member as a SCOPE_REF even if the scope isn't
16339 dependent so that we can check access control later. */
16340 probe = argument;
16341 if (TREE_CODE (probe) == SCOPE_REF)
16342 probe = TREE_OPERAND (probe, 1);
16343 if (VAR_P (probe))
16345 /* A variable without external linkage might still be a
16346 valid constant-expression, so no error is issued here
16347 if the external-linkage check fails. */
16348 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16349 cp_parser_simulate_error (parser);
16351 else if (is_overloaded_fn (argument))
16352 /* All overloaded functions are allowed; if the external
16353 linkage test does not pass, an error will be issued
16354 later. */
16356 else if (address_p
16357 && (TREE_CODE (argument) == OFFSET_REF
16358 || TREE_CODE (argument) == SCOPE_REF))
16359 /* A pointer-to-member. */
16361 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16363 else
16364 cp_parser_simulate_error (parser);
16366 if (cp_parser_parse_definitely (parser))
16368 if (address_p)
16369 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16370 tf_warning_or_error);
16371 else
16372 argument = convert_from_reference (argument);
16373 return argument;
16377 /* If the argument started with "&", there are no other valid
16378 alternatives at this point. */
16379 if (address_p)
16381 cp_parser_error (parser, "invalid non-type template argument");
16382 return error_mark_node;
16385 general_expr:
16386 /* If the argument wasn't successfully parsed as a type-id followed
16387 by '>>', the argument can only be a constant expression now.
16388 Otherwise, we try parsing the constant-expression tentatively,
16389 because the argument could really be a type-id. */
16390 if (maybe_type_id)
16391 cp_parser_parse_tentatively (parser);
16393 if (cxx_dialect <= cxx14)
16394 argument = cp_parser_constant_expression (parser);
16395 else
16397 /* With C++17 generalized non-type template arguments we need to handle
16398 lvalue constant expressions, too. */
16399 argument = cp_parser_assignment_expression (parser);
16400 require_potential_constant_expression (argument);
16403 if (!maybe_type_id)
16404 return argument;
16405 if (!cp_parser_next_token_ends_template_argument_p (parser))
16406 cp_parser_error (parser, "expected template-argument");
16407 if (cp_parser_parse_definitely (parser))
16408 return argument;
16409 /* We did our best to parse the argument as a non type-id, but that
16410 was the only alternative that matched (albeit with a '>' after
16411 it). We can assume it's just a typo from the user, and a
16412 diagnostic will then be issued. */
16413 return cp_parser_template_type_arg (parser);
16416 /* Parse an explicit-instantiation.
16418 explicit-instantiation:
16419 template declaration
16421 Although the standard says `declaration', what it really means is:
16423 explicit-instantiation:
16424 template decl-specifier-seq [opt] declarator [opt] ;
16426 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16427 supposed to be allowed. A defect report has been filed about this
16428 issue.
16430 GNU Extension:
16432 explicit-instantiation:
16433 storage-class-specifier template
16434 decl-specifier-seq [opt] declarator [opt] ;
16435 function-specifier template
16436 decl-specifier-seq [opt] declarator [opt] ; */
16438 static void
16439 cp_parser_explicit_instantiation (cp_parser* parser)
16441 int declares_class_or_enum;
16442 cp_decl_specifier_seq decl_specifiers;
16443 tree extension_specifier = NULL_TREE;
16445 timevar_push (TV_TEMPLATE_INST);
16447 /* Look for an (optional) storage-class-specifier or
16448 function-specifier. */
16449 if (cp_parser_allow_gnu_extensions_p (parser))
16451 extension_specifier
16452 = cp_parser_storage_class_specifier_opt (parser);
16453 if (!extension_specifier)
16454 extension_specifier
16455 = cp_parser_function_specifier_opt (parser,
16456 /*decl_specs=*/NULL);
16459 /* Look for the `template' keyword. */
16460 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16461 /* Let the front end know that we are processing an explicit
16462 instantiation. */
16463 begin_explicit_instantiation ();
16464 /* [temp.explicit] says that we are supposed to ignore access
16465 control while processing explicit instantiation directives. */
16466 push_deferring_access_checks (dk_no_check);
16467 /* Parse a decl-specifier-seq. */
16468 cp_parser_decl_specifier_seq (parser,
16469 CP_PARSER_FLAGS_OPTIONAL,
16470 &decl_specifiers,
16471 &declares_class_or_enum);
16472 /* If there was exactly one decl-specifier, and it declared a class,
16473 and there's no declarator, then we have an explicit type
16474 instantiation. */
16475 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16477 tree type;
16479 type = check_tag_decl (&decl_specifiers,
16480 /*explicit_type_instantiation_p=*/true);
16481 /* Turn access control back on for names used during
16482 template instantiation. */
16483 pop_deferring_access_checks ();
16484 if (type)
16485 do_type_instantiation (type, extension_specifier,
16486 /*complain=*/tf_error);
16488 else
16490 cp_declarator *declarator;
16491 tree decl;
16493 /* Parse the declarator. */
16494 declarator
16495 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16496 /*ctor_dtor_or_conv_p=*/NULL,
16497 /*parenthesized_p=*/NULL,
16498 /*member_p=*/false,
16499 /*friend_p=*/false);
16500 if (declares_class_or_enum & 2)
16501 cp_parser_check_for_definition_in_return_type (declarator,
16502 decl_specifiers.type,
16503 decl_specifiers.locations[ds_type_spec]);
16504 if (declarator != cp_error_declarator)
16506 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16507 permerror (decl_specifiers.locations[ds_inline],
16508 "explicit instantiation shall not use"
16509 " %<inline%> specifier");
16510 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16511 permerror (decl_specifiers.locations[ds_constexpr],
16512 "explicit instantiation shall not use"
16513 " %<constexpr%> specifier");
16515 decl = grokdeclarator (declarator, &decl_specifiers,
16516 NORMAL, 0, &decl_specifiers.attributes);
16517 /* Turn access control back on for names used during
16518 template instantiation. */
16519 pop_deferring_access_checks ();
16520 /* Do the explicit instantiation. */
16521 do_decl_instantiation (decl, extension_specifier);
16523 else
16525 pop_deferring_access_checks ();
16526 /* Skip the body of the explicit instantiation. */
16527 cp_parser_skip_to_end_of_statement (parser);
16530 /* We're done with the instantiation. */
16531 end_explicit_instantiation ();
16533 cp_parser_consume_semicolon_at_end_of_statement (parser);
16535 timevar_pop (TV_TEMPLATE_INST);
16538 /* Parse an explicit-specialization.
16540 explicit-specialization:
16541 template < > declaration
16543 Although the standard says `declaration', what it really means is:
16545 explicit-specialization:
16546 template <> decl-specifier [opt] init-declarator [opt] ;
16547 template <> function-definition
16548 template <> explicit-specialization
16549 template <> template-declaration */
16551 static void
16552 cp_parser_explicit_specialization (cp_parser* parser)
16554 bool need_lang_pop;
16555 cp_token *token = cp_lexer_peek_token (parser->lexer);
16557 /* Look for the `template' keyword. */
16558 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16559 /* Look for the `<'. */
16560 cp_parser_require (parser, CPP_LESS, RT_LESS);
16561 /* Look for the `>'. */
16562 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16563 /* We have processed another parameter list. */
16564 ++parser->num_template_parameter_lists;
16565 /* [temp]
16567 A template ... explicit specialization ... shall not have C
16568 linkage. */
16569 if (current_lang_name == lang_name_c)
16571 error_at (token->location, "template specialization with C linkage");
16572 maybe_show_extern_c_location ();
16573 /* Give it C++ linkage to avoid confusing other parts of the
16574 front end. */
16575 push_lang_context (lang_name_cplusplus);
16576 need_lang_pop = true;
16578 else
16579 need_lang_pop = false;
16580 /* Let the front end know that we are beginning a specialization. */
16581 if (!begin_specialization ())
16583 end_specialization ();
16584 return;
16587 /* If the next keyword is `template', we need to figure out whether
16588 or not we're looking a template-declaration. */
16589 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16591 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16592 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16593 cp_parser_template_declaration_after_export (parser,
16594 /*member_p=*/false);
16595 else
16596 cp_parser_explicit_specialization (parser);
16598 else
16599 /* Parse the dependent declaration. */
16600 cp_parser_single_declaration (parser,
16601 /*checks=*/NULL,
16602 /*member_p=*/false,
16603 /*explicit_specialization_p=*/true,
16604 /*friend_p=*/NULL);
16605 /* We're done with the specialization. */
16606 end_specialization ();
16607 /* For the erroneous case of a template with C linkage, we pushed an
16608 implicit C++ linkage scope; exit that scope now. */
16609 if (need_lang_pop)
16610 pop_lang_context ();
16611 /* We're done with this parameter list. */
16612 --parser->num_template_parameter_lists;
16615 /* Parse a type-specifier.
16617 type-specifier:
16618 simple-type-specifier
16619 class-specifier
16620 enum-specifier
16621 elaborated-type-specifier
16622 cv-qualifier
16624 GNU Extension:
16626 type-specifier:
16627 __complex__
16629 Returns a representation of the type-specifier. For a
16630 class-specifier, enum-specifier, or elaborated-type-specifier, a
16631 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16633 The parser flags FLAGS is used to control type-specifier parsing.
16635 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16636 in a decl-specifier-seq.
16638 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16639 class-specifier, enum-specifier, or elaborated-type-specifier, then
16640 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16641 if a type is declared; 2 if it is defined. Otherwise, it is set to
16642 zero.
16644 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16645 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16646 is set to FALSE. */
16648 static tree
16649 cp_parser_type_specifier (cp_parser* parser,
16650 cp_parser_flags flags,
16651 cp_decl_specifier_seq *decl_specs,
16652 bool is_declaration,
16653 int* declares_class_or_enum,
16654 bool* is_cv_qualifier)
16656 tree type_spec = NULL_TREE;
16657 cp_token *token;
16658 enum rid keyword;
16659 cp_decl_spec ds = ds_last;
16661 /* Assume this type-specifier does not declare a new type. */
16662 if (declares_class_or_enum)
16663 *declares_class_or_enum = 0;
16664 /* And that it does not specify a cv-qualifier. */
16665 if (is_cv_qualifier)
16666 *is_cv_qualifier = false;
16667 /* Peek at the next token. */
16668 token = cp_lexer_peek_token (parser->lexer);
16670 /* If we're looking at a keyword, we can use that to guide the
16671 production we choose. */
16672 keyword = token->keyword;
16673 switch (keyword)
16675 case RID_ENUM:
16676 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16677 goto elaborated_type_specifier;
16679 /* Look for the enum-specifier. */
16680 type_spec = cp_parser_enum_specifier (parser);
16681 /* If that worked, we're done. */
16682 if (type_spec)
16684 if (declares_class_or_enum)
16685 *declares_class_or_enum = 2;
16686 if (decl_specs)
16687 cp_parser_set_decl_spec_type (decl_specs,
16688 type_spec,
16689 token,
16690 /*type_definition_p=*/true);
16691 return type_spec;
16693 else
16694 goto elaborated_type_specifier;
16696 /* Any of these indicate either a class-specifier, or an
16697 elaborated-type-specifier. */
16698 case RID_CLASS:
16699 case RID_STRUCT:
16700 case RID_UNION:
16701 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16702 goto elaborated_type_specifier;
16704 /* Parse tentatively so that we can back up if we don't find a
16705 class-specifier. */
16706 cp_parser_parse_tentatively (parser);
16707 /* Look for the class-specifier. */
16708 type_spec = cp_parser_class_specifier (parser);
16709 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16710 /* If that worked, we're done. */
16711 if (cp_parser_parse_definitely (parser))
16713 if (declares_class_or_enum)
16714 *declares_class_or_enum = 2;
16715 if (decl_specs)
16716 cp_parser_set_decl_spec_type (decl_specs,
16717 type_spec,
16718 token,
16719 /*type_definition_p=*/true);
16720 return type_spec;
16723 /* Fall through. */
16724 elaborated_type_specifier:
16725 /* We're declaring (not defining) a class or enum. */
16726 if (declares_class_or_enum)
16727 *declares_class_or_enum = 1;
16729 /* Fall through. */
16730 case RID_TYPENAME:
16731 /* Look for an elaborated-type-specifier. */
16732 type_spec
16733 = (cp_parser_elaborated_type_specifier
16734 (parser,
16735 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16736 is_declaration));
16737 if (decl_specs)
16738 cp_parser_set_decl_spec_type (decl_specs,
16739 type_spec,
16740 token,
16741 /*type_definition_p=*/false);
16742 return type_spec;
16744 case RID_CONST:
16745 ds = ds_const;
16746 if (is_cv_qualifier)
16747 *is_cv_qualifier = true;
16748 break;
16750 case RID_VOLATILE:
16751 ds = ds_volatile;
16752 if (is_cv_qualifier)
16753 *is_cv_qualifier = true;
16754 break;
16756 case RID_RESTRICT:
16757 ds = ds_restrict;
16758 if (is_cv_qualifier)
16759 *is_cv_qualifier = true;
16760 break;
16762 case RID_COMPLEX:
16763 /* The `__complex__' keyword is a GNU extension. */
16764 ds = ds_complex;
16765 break;
16767 default:
16768 break;
16771 /* Handle simple keywords. */
16772 if (ds != ds_last)
16774 if (decl_specs)
16776 set_and_check_decl_spec_loc (decl_specs, ds, token);
16777 decl_specs->any_specifiers_p = true;
16779 return cp_lexer_consume_token (parser->lexer)->u.value;
16782 /* If we do not already have a type-specifier, assume we are looking
16783 at a simple-type-specifier. */
16784 type_spec = cp_parser_simple_type_specifier (parser,
16785 decl_specs,
16786 flags);
16788 /* If we didn't find a type-specifier, and a type-specifier was not
16789 optional in this context, issue an error message. */
16790 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16792 cp_parser_error (parser, "expected type specifier");
16793 return error_mark_node;
16796 return type_spec;
16799 /* Parse a simple-type-specifier.
16801 simple-type-specifier:
16802 :: [opt] nested-name-specifier [opt] type-name
16803 :: [opt] nested-name-specifier template template-id
16804 char
16805 wchar_t
16806 bool
16807 short
16809 long
16810 signed
16811 unsigned
16812 float
16813 double
16814 void
16816 C++11 Extension:
16818 simple-type-specifier:
16819 auto
16820 decltype ( expression )
16821 char16_t
16822 char32_t
16823 __underlying_type ( type-id )
16825 C++17 extension:
16827 nested-name-specifier(opt) template-name
16829 GNU Extension:
16831 simple-type-specifier:
16832 __int128
16833 __typeof__ unary-expression
16834 __typeof__ ( type-id )
16835 __typeof__ ( type-id ) { initializer-list , [opt] }
16837 Concepts Extension:
16839 simple-type-specifier:
16840 constrained-type-specifier
16842 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16843 appropriately updated. */
16845 static tree
16846 cp_parser_simple_type_specifier (cp_parser* parser,
16847 cp_decl_specifier_seq *decl_specs,
16848 cp_parser_flags flags)
16850 tree type = NULL_TREE;
16851 cp_token *token;
16852 int idx;
16854 /* Peek at the next token. */
16855 token = cp_lexer_peek_token (parser->lexer);
16857 /* If we're looking at a keyword, things are easy. */
16858 switch (token->keyword)
16860 case RID_CHAR:
16861 if (decl_specs)
16862 decl_specs->explicit_char_p = true;
16863 type = char_type_node;
16864 break;
16865 case RID_CHAR16:
16866 type = char16_type_node;
16867 break;
16868 case RID_CHAR32:
16869 type = char32_type_node;
16870 break;
16871 case RID_WCHAR:
16872 type = wchar_type_node;
16873 break;
16874 case RID_BOOL:
16875 type = boolean_type_node;
16876 break;
16877 case RID_SHORT:
16878 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16879 type = short_integer_type_node;
16880 break;
16881 case RID_INT:
16882 if (decl_specs)
16883 decl_specs->explicit_int_p = true;
16884 type = integer_type_node;
16885 break;
16886 case RID_INT_N_0:
16887 case RID_INT_N_1:
16888 case RID_INT_N_2:
16889 case RID_INT_N_3:
16890 idx = token->keyword - RID_INT_N_0;
16891 if (! int_n_enabled_p [idx])
16892 break;
16893 if (decl_specs)
16895 decl_specs->explicit_intN_p = true;
16896 decl_specs->int_n_idx = idx;
16898 type = int_n_trees [idx].signed_type;
16899 break;
16900 case RID_LONG:
16901 if (decl_specs)
16902 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16903 type = long_integer_type_node;
16904 break;
16905 case RID_SIGNED:
16906 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16907 type = integer_type_node;
16908 break;
16909 case RID_UNSIGNED:
16910 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16911 type = unsigned_type_node;
16912 break;
16913 case RID_FLOAT:
16914 type = float_type_node;
16915 break;
16916 case RID_DOUBLE:
16917 type = double_type_node;
16918 break;
16919 case RID_VOID:
16920 type = void_type_node;
16921 break;
16923 case RID_AUTO:
16924 maybe_warn_cpp0x (CPP0X_AUTO);
16925 if (parser->auto_is_implicit_function_template_parm_p)
16927 /* The 'auto' might be the placeholder return type for a function decl
16928 with trailing return type. */
16929 bool have_trailing_return_fn_decl = false;
16931 cp_parser_parse_tentatively (parser);
16932 cp_lexer_consume_token (parser->lexer);
16933 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16934 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16935 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16936 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16938 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16940 cp_lexer_consume_token (parser->lexer);
16941 cp_parser_skip_to_closing_parenthesis (parser,
16942 /*recovering*/false,
16943 /*or_comma*/false,
16944 /*consume_paren*/true);
16945 continue;
16948 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16950 have_trailing_return_fn_decl = true;
16951 break;
16954 cp_lexer_consume_token (parser->lexer);
16956 cp_parser_abort_tentative_parse (parser);
16958 if (have_trailing_return_fn_decl)
16960 type = make_auto ();
16961 break;
16964 if (cxx_dialect >= cxx14)
16966 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16967 type = TREE_TYPE (type);
16969 else
16970 type = error_mark_node;
16972 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16974 if (cxx_dialect < cxx14)
16975 error_at (token->location,
16976 "use of %<auto%> in lambda parameter declaration "
16977 "only available with "
16978 "-std=c++14 or -std=gnu++14");
16980 else if (cxx_dialect < cxx14)
16981 error_at (token->location,
16982 "use of %<auto%> in parameter declaration "
16983 "only available with "
16984 "-std=c++14 or -std=gnu++14");
16985 else if (!flag_concepts)
16986 pedwarn (token->location, OPT_Wpedantic,
16987 "ISO C++ forbids use of %<auto%> in parameter "
16988 "declaration");
16990 else
16991 type = make_auto ();
16992 break;
16994 case RID_DECLTYPE:
16995 /* Since DR 743, decltype can either be a simple-type-specifier by
16996 itself or begin a nested-name-specifier. Parsing it will replace
16997 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16998 handling below decide what to do. */
16999 cp_parser_decltype (parser);
17000 cp_lexer_set_token_position (parser->lexer, token);
17001 break;
17003 case RID_TYPEOF:
17004 /* Consume the `typeof' token. */
17005 cp_lexer_consume_token (parser->lexer);
17006 /* Parse the operand to `typeof'. */
17007 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17008 /* If it is not already a TYPE, take its type. */
17009 if (!TYPE_P (type))
17010 type = finish_typeof (type);
17012 if (decl_specs)
17013 cp_parser_set_decl_spec_type (decl_specs, type,
17014 token,
17015 /*type_definition_p=*/false);
17017 return type;
17019 case RID_UNDERLYING_TYPE:
17020 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17021 if (decl_specs)
17022 cp_parser_set_decl_spec_type (decl_specs, type,
17023 token,
17024 /*type_definition_p=*/false);
17026 return type;
17028 case RID_BASES:
17029 case RID_DIRECT_BASES:
17030 type = cp_parser_trait_expr (parser, token->keyword);
17031 if (decl_specs)
17032 cp_parser_set_decl_spec_type (decl_specs, type,
17033 token,
17034 /*type_definition_p=*/false);
17035 return type;
17036 default:
17037 break;
17040 /* If token is an already-parsed decltype not followed by ::,
17041 it's a simple-type-specifier. */
17042 if (token->type == CPP_DECLTYPE
17043 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17045 type = saved_checks_value (token->u.tree_check_value);
17046 if (decl_specs)
17048 cp_parser_set_decl_spec_type (decl_specs, type,
17049 token,
17050 /*type_definition_p=*/false);
17051 /* Remember that we are handling a decltype in order to
17052 implement the resolution of DR 1510 when the argument
17053 isn't instantiation dependent. */
17054 decl_specs->decltype_p = true;
17056 cp_lexer_consume_token (parser->lexer);
17057 return type;
17060 /* If the type-specifier was for a built-in type, we're done. */
17061 if (type)
17063 /* Record the type. */
17064 if (decl_specs
17065 && (token->keyword != RID_SIGNED
17066 && token->keyword != RID_UNSIGNED
17067 && token->keyword != RID_SHORT
17068 && token->keyword != RID_LONG))
17069 cp_parser_set_decl_spec_type (decl_specs,
17070 type,
17071 token,
17072 /*type_definition_p=*/false);
17073 if (decl_specs)
17074 decl_specs->any_specifiers_p = true;
17076 /* Consume the token. */
17077 cp_lexer_consume_token (parser->lexer);
17079 if (type == error_mark_node)
17080 return error_mark_node;
17082 /* There is no valid C++ program where a non-template type is
17083 followed by a "<". That usually indicates that the user thought
17084 that the type was a template. */
17085 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17086 token->location);
17088 return TYPE_NAME (type);
17091 /* The type-specifier must be a user-defined type. */
17092 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17094 bool qualified_p;
17095 bool global_p;
17097 /* Don't gobble tokens or issue error messages if this is an
17098 optional type-specifier. */
17099 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17100 cp_parser_parse_tentatively (parser);
17102 token = cp_lexer_peek_token (parser->lexer);
17104 /* Look for the optional `::' operator. */
17105 global_p
17106 = (cp_parser_global_scope_opt (parser,
17107 /*current_scope_valid_p=*/false)
17108 != NULL_TREE);
17109 /* Look for the nested-name specifier. */
17110 qualified_p
17111 = (cp_parser_nested_name_specifier_opt (parser,
17112 /*typename_keyword_p=*/false,
17113 /*check_dependency_p=*/true,
17114 /*type_p=*/false,
17115 /*is_declaration=*/false)
17116 != NULL_TREE);
17117 /* If we have seen a nested-name-specifier, and the next token
17118 is `template', then we are using the template-id production. */
17119 if (parser->scope
17120 && cp_parser_optional_template_keyword (parser))
17122 /* Look for the template-id. */
17123 type = cp_parser_template_id (parser,
17124 /*template_keyword_p=*/true,
17125 /*check_dependency_p=*/true,
17126 none_type,
17127 /*is_declaration=*/false);
17128 /* If the template-id did not name a type, we are out of
17129 luck. */
17130 if (TREE_CODE (type) != TYPE_DECL)
17132 cp_parser_error (parser, "expected template-id for type");
17133 type = NULL_TREE;
17136 /* Otherwise, look for a type-name. */
17137 else
17138 type = cp_parser_type_name (parser);
17139 /* Keep track of all name-lookups performed in class scopes. */
17140 if (type
17141 && !global_p
17142 && !qualified_p
17143 && TREE_CODE (type) == TYPE_DECL
17144 && identifier_p (DECL_NAME (type)))
17145 maybe_note_name_used_in_class (DECL_NAME (type), type);
17146 /* If it didn't work out, we don't have a TYPE. */
17147 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17148 && !cp_parser_parse_definitely (parser))
17149 type = NULL_TREE;
17150 if (!type && cxx_dialect >= cxx17)
17152 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17153 cp_parser_parse_tentatively (parser);
17155 cp_parser_global_scope_opt (parser,
17156 /*current_scope_valid_p=*/false);
17157 cp_parser_nested_name_specifier_opt (parser,
17158 /*typename_keyword_p=*/false,
17159 /*check_dependency_p=*/true,
17160 /*type_p=*/false,
17161 /*is_declaration=*/false);
17162 tree name = cp_parser_identifier (parser);
17163 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17164 && parser->scope != error_mark_node)
17166 tree tmpl = cp_parser_lookup_name (parser, name,
17167 none_type,
17168 /*is_template=*/false,
17169 /*is_namespace=*/false,
17170 /*check_dependency=*/true,
17171 /*ambiguous_decls=*/NULL,
17172 token->location);
17173 if (tmpl && tmpl != error_mark_node
17174 && (DECL_CLASS_TEMPLATE_P (tmpl)
17175 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17176 type = make_template_placeholder (tmpl);
17177 else
17179 type = error_mark_node;
17180 if (!cp_parser_simulate_error (parser))
17181 cp_parser_name_lookup_error (parser, name, tmpl,
17182 NLE_TYPE, token->location);
17185 else
17186 type = error_mark_node;
17188 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17189 && !cp_parser_parse_definitely (parser))
17190 type = NULL_TREE;
17192 if (type && decl_specs)
17193 cp_parser_set_decl_spec_type (decl_specs, type,
17194 token,
17195 /*type_definition_p=*/false);
17198 /* If we didn't get a type-name, issue an error message. */
17199 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17201 cp_parser_error (parser, "expected type-name");
17202 return error_mark_node;
17205 if (type && type != error_mark_node)
17207 /* See if TYPE is an Objective-C type, and if so, parse and
17208 accept any protocol references following it. Do this before
17209 the cp_parser_check_for_invalid_template_id() call, because
17210 Objective-C types can be followed by '<...>' which would
17211 enclose protocol names rather than template arguments, and so
17212 everything is fine. */
17213 if (c_dialect_objc () && !parser->scope
17214 && (objc_is_id (type) || objc_is_class_name (type)))
17216 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17217 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17219 /* Clobber the "unqualified" type previously entered into
17220 DECL_SPECS with the new, improved protocol-qualified version. */
17221 if (decl_specs)
17222 decl_specs->type = qual_type;
17224 return qual_type;
17227 /* There is no valid C++ program where a non-template type is
17228 followed by a "<". That usually indicates that the user
17229 thought that the type was a template. */
17230 cp_parser_check_for_invalid_template_id (parser, type,
17231 none_type,
17232 token->location);
17235 return type;
17238 /* Parse a type-name.
17240 type-name:
17241 class-name
17242 enum-name
17243 typedef-name
17244 simple-template-id [in c++0x]
17246 enum-name:
17247 identifier
17249 typedef-name:
17250 identifier
17252 Concepts:
17254 type-name:
17255 concept-name
17256 partial-concept-id
17258 concept-name:
17259 identifier
17261 Returns a TYPE_DECL for the type. */
17263 static tree
17264 cp_parser_type_name (cp_parser* parser)
17266 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17269 /* See above. */
17270 static tree
17271 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17273 tree type_decl;
17275 /* We can't know yet whether it is a class-name or not. */
17276 cp_parser_parse_tentatively (parser);
17277 /* Try a class-name. */
17278 type_decl = cp_parser_class_name (parser,
17279 typename_keyword_p,
17280 /*template_keyword_p=*/false,
17281 none_type,
17282 /*check_dependency_p=*/true,
17283 /*class_head_p=*/false,
17284 /*is_declaration=*/false);
17285 /* If it's not a class-name, keep looking. */
17286 if (!cp_parser_parse_definitely (parser))
17288 if (cxx_dialect < cxx11)
17289 /* It must be a typedef-name or an enum-name. */
17290 return cp_parser_nonclass_name (parser);
17292 cp_parser_parse_tentatively (parser);
17293 /* It is either a simple-template-id representing an
17294 instantiation of an alias template... */
17295 type_decl = cp_parser_template_id (parser,
17296 /*template_keyword_p=*/false,
17297 /*check_dependency_p=*/true,
17298 none_type,
17299 /*is_declaration=*/false);
17300 /* Note that this must be an instantiation of an alias template
17301 because [temp.names]/6 says:
17303 A template-id that names an alias template specialization
17304 is a type-name.
17306 Whereas [temp.names]/7 says:
17308 A simple-template-id that names a class template
17309 specialization is a class-name.
17311 With concepts, this could also be a partial-concept-id that
17312 declares a non-type template parameter. */
17313 if (type_decl != NULL_TREE
17314 && TREE_CODE (type_decl) == TYPE_DECL
17315 && TYPE_DECL_ALIAS_P (type_decl))
17316 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17317 else if (is_constrained_parameter (type_decl))
17318 /* Don't do anything. */ ;
17319 else
17320 cp_parser_simulate_error (parser);
17322 if (!cp_parser_parse_definitely (parser))
17323 /* ... Or a typedef-name or an enum-name. */
17324 return cp_parser_nonclass_name (parser);
17327 return type_decl;
17330 /* Check if DECL and ARGS can form a constrained-type-specifier.
17331 If ARGS is non-null, we try to form a concept check of the
17332 form DECL<?, ARGS> where ? is a wildcard that matches any
17333 kind of template argument. If ARGS is NULL, then we try to
17334 form a concept check of the form DECL<?>. */
17336 static tree
17337 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17338 tree decl, tree args)
17340 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17342 /* If we a constrained-type-specifier cannot be deduced. */
17343 if (parser->prevent_constrained_type_specifiers)
17344 return NULL_TREE;
17346 /* A constrained type specifier can only be found in an
17347 overload set or as a reference to a template declaration.
17349 FIXME: This might be masking a bug. It's possible that
17350 that the deduction below is causing template specializations
17351 to be formed with the wildcard as an argument. */
17352 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17353 return NULL_TREE;
17355 /* Try to build a call expression that evaluates the
17356 concept. This can fail if the overload set refers
17357 only to non-templates. */
17358 tree placeholder = build_nt (WILDCARD_DECL);
17359 tree check = build_concept_check (decl, placeholder, args);
17360 if (check == error_mark_node)
17361 return NULL_TREE;
17363 /* Deduce the checked constraint and the prototype parameter.
17365 FIXME: In certain cases, failure to deduce should be a
17366 diagnosable error. */
17367 tree conc;
17368 tree proto;
17369 if (!deduce_constrained_parameter (check, conc, proto))
17370 return NULL_TREE;
17372 /* In template parameter scope, this results in a constrained
17373 parameter. Return a descriptor of that parm. */
17374 if (processing_template_parmlist)
17375 return build_constrained_parameter (conc, proto, args);
17377 /* In a parameter-declaration-clause, constrained-type
17378 specifiers result in invented template parameters. */
17379 if (parser->auto_is_implicit_function_template_parm_p)
17381 tree x = build_constrained_parameter (conc, proto, args);
17382 return synthesize_implicit_template_parm (parser, x);
17384 else
17386 /* Otherwise, we're in a context where the constrained
17387 type name is deduced and the constraint applies
17388 after deduction. */
17389 return make_constrained_auto (conc, args);
17392 return NULL_TREE;
17395 /* If DECL refers to a concept, return a TYPE_DECL representing
17396 the result of using the constrained type specifier in the
17397 current context. DECL refers to a concept if
17399 - it is an overload set containing a function concept taking a single
17400 type argument, or
17402 - it is a variable concept taking a single type argument. */
17404 static tree
17405 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17407 if (flag_concepts
17408 && (TREE_CODE (decl) == OVERLOAD
17409 || BASELINK_P (decl)
17410 || variable_concept_p (decl)))
17411 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17412 else
17413 return NULL_TREE;
17416 /* Check if DECL and ARGS form a partial-concept-id. If so,
17417 assign ID to the resulting constrained placeholder.
17419 Returns true if the partial-concept-id designates a placeholder
17420 and false otherwise. Note that *id is set to NULL_TREE in
17421 this case. */
17423 static tree
17424 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17426 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17429 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17430 or a concept-name.
17432 enum-name:
17433 identifier
17435 typedef-name:
17436 identifier
17438 concept-name:
17439 identifier
17441 Returns a TYPE_DECL for the type. */
17443 static tree
17444 cp_parser_nonclass_name (cp_parser* parser)
17446 tree type_decl;
17447 tree identifier;
17449 cp_token *token = cp_lexer_peek_token (parser->lexer);
17450 identifier = cp_parser_identifier (parser);
17451 if (identifier == error_mark_node)
17452 return error_mark_node;
17454 /* Look up the type-name. */
17455 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17457 type_decl = strip_using_decl (type_decl);
17459 /* If we found an overload set, then it may refer to a concept-name. */
17460 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17461 type_decl = decl;
17463 if (TREE_CODE (type_decl) != TYPE_DECL
17464 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17466 /* See if this is an Objective-C type. */
17467 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17468 tree type = objc_get_protocol_qualified_type (identifier, protos);
17469 if (type)
17470 type_decl = TYPE_NAME (type);
17473 /* Issue an error if we did not find a type-name. */
17474 if (TREE_CODE (type_decl) != TYPE_DECL
17475 /* In Objective-C, we have the complication that class names are
17476 normally type names and start declarations (eg, the
17477 "NSObject" in "NSObject *object;"), but can be used in an
17478 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17479 is an expression. So, a classname followed by a dot is not a
17480 valid type-name. */
17481 || (objc_is_class_name (TREE_TYPE (type_decl))
17482 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17484 if (!cp_parser_simulate_error (parser))
17485 cp_parser_name_lookup_error (parser, identifier, type_decl,
17486 NLE_TYPE, token->location);
17487 return error_mark_node;
17489 /* Remember that the name was used in the definition of the
17490 current class so that we can check later to see if the
17491 meaning would have been different after the class was
17492 entirely defined. */
17493 else if (type_decl != error_mark_node
17494 && !parser->scope)
17495 maybe_note_name_used_in_class (identifier, type_decl);
17497 return type_decl;
17500 /* Parse an elaborated-type-specifier. Note that the grammar given
17501 here incorporates the resolution to DR68.
17503 elaborated-type-specifier:
17504 class-key :: [opt] nested-name-specifier [opt] identifier
17505 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17506 enum-key :: [opt] nested-name-specifier [opt] identifier
17507 typename :: [opt] nested-name-specifier identifier
17508 typename :: [opt] nested-name-specifier template [opt]
17509 template-id
17511 GNU extension:
17513 elaborated-type-specifier:
17514 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17515 class-key attributes :: [opt] nested-name-specifier [opt]
17516 template [opt] template-id
17517 enum attributes :: [opt] nested-name-specifier [opt] identifier
17519 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17520 declared `friend'. If IS_DECLARATION is TRUE, then this
17521 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17522 something is being declared.
17524 Returns the TYPE specified. */
17526 static tree
17527 cp_parser_elaborated_type_specifier (cp_parser* parser,
17528 bool is_friend,
17529 bool is_declaration)
17531 enum tag_types tag_type;
17532 tree identifier;
17533 tree type = NULL_TREE;
17534 tree attributes = NULL_TREE;
17535 tree globalscope;
17536 cp_token *token = NULL;
17538 /* See if we're looking at the `enum' keyword. */
17539 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17541 /* Consume the `enum' token. */
17542 cp_lexer_consume_token (parser->lexer);
17543 /* Remember that it's an enumeration type. */
17544 tag_type = enum_type;
17545 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17546 enums) is used here. */
17547 cp_token *token = cp_lexer_peek_token (parser->lexer);
17548 if (cp_parser_is_keyword (token, RID_CLASS)
17549 || cp_parser_is_keyword (token, RID_STRUCT))
17551 gcc_rich_location richloc (token->location);
17552 richloc.add_range (input_location, false);
17553 richloc.add_fixit_remove ();
17554 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17555 "a scoped enum must not use the %qD keyword",
17556 token->u.value);
17557 /* Consume the `struct' or `class' and parse it anyway. */
17558 cp_lexer_consume_token (parser->lexer);
17560 /* Parse the attributes. */
17561 attributes = cp_parser_attributes_opt (parser);
17563 /* Or, it might be `typename'. */
17564 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17565 RID_TYPENAME))
17567 /* Consume the `typename' token. */
17568 cp_lexer_consume_token (parser->lexer);
17569 /* Remember that it's a `typename' type. */
17570 tag_type = typename_type;
17572 /* Otherwise it must be a class-key. */
17573 else
17575 tag_type = cp_parser_class_key (parser);
17576 if (tag_type == none_type)
17577 return error_mark_node;
17578 /* Parse the attributes. */
17579 attributes = cp_parser_attributes_opt (parser);
17582 /* Look for the `::' operator. */
17583 globalscope = cp_parser_global_scope_opt (parser,
17584 /*current_scope_valid_p=*/false);
17585 /* Look for the nested-name-specifier. */
17586 tree nested_name_specifier;
17587 if (tag_type == typename_type && !globalscope)
17589 nested_name_specifier
17590 = cp_parser_nested_name_specifier (parser,
17591 /*typename_keyword_p=*/true,
17592 /*check_dependency_p=*/true,
17593 /*type_p=*/true,
17594 is_declaration);
17595 if (!nested_name_specifier)
17596 return error_mark_node;
17598 else
17599 /* Even though `typename' is not present, the proposed resolution
17600 to Core Issue 180 says that in `class A<T>::B', `B' should be
17601 considered a type-name, even if `A<T>' is dependent. */
17602 nested_name_specifier
17603 = cp_parser_nested_name_specifier_opt (parser,
17604 /*typename_keyword_p=*/true,
17605 /*check_dependency_p=*/true,
17606 /*type_p=*/true,
17607 is_declaration);
17608 /* For everything but enumeration types, consider a template-id.
17609 For an enumeration type, consider only a plain identifier. */
17610 if (tag_type != enum_type)
17612 bool template_p = false;
17613 tree decl;
17615 /* Allow the `template' keyword. */
17616 template_p = cp_parser_optional_template_keyword (parser);
17617 /* If we didn't see `template', we don't know if there's a
17618 template-id or not. */
17619 if (!template_p)
17620 cp_parser_parse_tentatively (parser);
17621 /* Parse the template-id. */
17622 token = cp_lexer_peek_token (parser->lexer);
17623 decl = cp_parser_template_id (parser, template_p,
17624 /*check_dependency_p=*/true,
17625 tag_type,
17626 is_declaration);
17627 /* If we didn't find a template-id, look for an ordinary
17628 identifier. */
17629 if (!template_p && !cp_parser_parse_definitely (parser))
17631 /* We can get here when cp_parser_template_id, called by
17632 cp_parser_class_name with tag_type == none_type, succeeds
17633 and caches a BASELINK. Then, when called again here,
17634 instead of failing and returning an error_mark_node
17635 returns it (see template/typename17.C in C++11).
17636 ??? Could we diagnose this earlier? */
17637 else if (tag_type == typename_type && BASELINK_P (decl))
17639 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17640 type = error_mark_node;
17642 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17643 in effect, then we must assume that, upon instantiation, the
17644 template will correspond to a class. */
17645 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17646 && tag_type == typename_type)
17647 type = make_typename_type (parser->scope, decl,
17648 typename_type,
17649 /*complain=*/tf_error);
17650 /* If the `typename' keyword is in effect and DECL is not a type
17651 decl, then type is non existent. */
17652 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17654 else if (TREE_CODE (decl) == TYPE_DECL)
17656 type = check_elaborated_type_specifier (tag_type, decl,
17657 /*allow_template_p=*/true);
17659 /* If the next token is a semicolon, this must be a specialization,
17660 instantiation, or friend declaration. Check the scope while we
17661 still know whether or not we had a nested-name-specifier. */
17662 if (type != error_mark_node
17663 && !nested_name_specifier && !is_friend
17664 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17665 check_unqualified_spec_or_inst (type, token->location);
17667 else if (decl == error_mark_node)
17668 type = error_mark_node;
17671 if (!type)
17673 token = cp_lexer_peek_token (parser->lexer);
17674 identifier = cp_parser_identifier (parser);
17676 if (identifier == error_mark_node)
17678 parser->scope = NULL_TREE;
17679 return error_mark_node;
17682 /* For a `typename', we needn't call xref_tag. */
17683 if (tag_type == typename_type
17684 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17685 return cp_parser_make_typename_type (parser, identifier,
17686 token->location);
17688 /* Template parameter lists apply only if we are not within a
17689 function parameter list. */
17690 bool template_parm_lists_apply
17691 = parser->num_template_parameter_lists;
17692 if (template_parm_lists_apply)
17693 for (cp_binding_level *s = current_binding_level;
17694 s && s->kind != sk_template_parms;
17695 s = s->level_chain)
17696 if (s->kind == sk_function_parms)
17697 template_parm_lists_apply = false;
17699 /* Look up a qualified name in the usual way. */
17700 if (parser->scope)
17702 tree decl;
17703 tree ambiguous_decls;
17705 decl = cp_parser_lookup_name (parser, identifier,
17706 tag_type,
17707 /*is_template=*/false,
17708 /*is_namespace=*/false,
17709 /*check_dependency=*/true,
17710 &ambiguous_decls,
17711 token->location);
17713 /* If the lookup was ambiguous, an error will already have been
17714 issued. */
17715 if (ambiguous_decls)
17716 return error_mark_node;
17718 /* If we are parsing friend declaration, DECL may be a
17719 TEMPLATE_DECL tree node here. However, we need to check
17720 whether this TEMPLATE_DECL results in valid code. Consider
17721 the following example:
17723 namespace N {
17724 template <class T> class C {};
17726 class X {
17727 template <class T> friend class N::C; // #1, valid code
17729 template <class T> class Y {
17730 friend class N::C; // #2, invalid code
17733 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17734 name lookup of `N::C'. We see that friend declaration must
17735 be template for the code to be valid. Note that
17736 processing_template_decl does not work here since it is
17737 always 1 for the above two cases. */
17739 decl = (cp_parser_maybe_treat_template_as_class
17740 (decl, /*tag_name_p=*/is_friend
17741 && template_parm_lists_apply));
17743 if (TREE_CODE (decl) != TYPE_DECL)
17745 cp_parser_diagnose_invalid_type_name (parser,
17746 identifier,
17747 token->location);
17748 return error_mark_node;
17751 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17753 bool allow_template = (template_parm_lists_apply
17754 || DECL_SELF_REFERENCE_P (decl));
17755 type = check_elaborated_type_specifier (tag_type, decl,
17756 allow_template);
17758 if (type == error_mark_node)
17759 return error_mark_node;
17762 /* Forward declarations of nested types, such as
17764 class C1::C2;
17765 class C1::C2::C3;
17767 are invalid unless all components preceding the final '::'
17768 are complete. If all enclosing types are complete, these
17769 declarations become merely pointless.
17771 Invalid forward declarations of nested types are errors
17772 caught elsewhere in parsing. Those that are pointless arrive
17773 here. */
17775 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17776 && !is_friend && !processing_explicit_instantiation)
17777 warning (0, "declaration %qD does not declare anything", decl);
17779 type = TREE_TYPE (decl);
17781 else
17783 /* An elaborated-type-specifier sometimes introduces a new type and
17784 sometimes names an existing type. Normally, the rule is that it
17785 introduces a new type only if there is not an existing type of
17786 the same name already in scope. For example, given:
17788 struct S {};
17789 void f() { struct S s; }
17791 the `struct S' in the body of `f' is the same `struct S' as in
17792 the global scope; the existing definition is used. However, if
17793 there were no global declaration, this would introduce a new
17794 local class named `S'.
17796 An exception to this rule applies to the following code:
17798 namespace N { struct S; }
17800 Here, the elaborated-type-specifier names a new type
17801 unconditionally; even if there is already an `S' in the
17802 containing scope this declaration names a new type.
17803 This exception only applies if the elaborated-type-specifier
17804 forms the complete declaration:
17806 [class.name]
17808 A declaration consisting solely of `class-key identifier ;' is
17809 either a redeclaration of the name in the current scope or a
17810 forward declaration of the identifier as a class name. It
17811 introduces the name into the current scope.
17813 We are in this situation precisely when the next token is a `;'.
17815 An exception to the exception is that a `friend' declaration does
17816 *not* name a new type; i.e., given:
17818 struct S { friend struct T; };
17820 `T' is not a new type in the scope of `S'.
17822 Also, `new struct S' or `sizeof (struct S)' never results in the
17823 definition of a new type; a new type can only be declared in a
17824 declaration context. */
17826 tag_scope ts;
17827 bool template_p;
17829 if (is_friend)
17830 /* Friends have special name lookup rules. */
17831 ts = ts_within_enclosing_non_class;
17832 else if (is_declaration
17833 && cp_lexer_next_token_is (parser->lexer,
17834 CPP_SEMICOLON))
17835 /* This is a `class-key identifier ;' */
17836 ts = ts_current;
17837 else
17838 ts = ts_global;
17840 template_p =
17841 (template_parm_lists_apply
17842 && (cp_parser_next_token_starts_class_definition_p (parser)
17843 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17844 /* An unqualified name was used to reference this type, so
17845 there were no qualifying templates. */
17846 if (template_parm_lists_apply
17847 && !cp_parser_check_template_parameters (parser,
17848 /*num_templates=*/0,
17849 token->location,
17850 /*declarator=*/NULL))
17851 return error_mark_node;
17852 type = xref_tag (tag_type, identifier, ts, template_p);
17856 if (type == error_mark_node)
17857 return error_mark_node;
17859 /* Allow attributes on forward declarations of classes. */
17860 if (attributes)
17862 if (TREE_CODE (type) == TYPENAME_TYPE)
17863 warning (OPT_Wattributes,
17864 "attributes ignored on uninstantiated type");
17865 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17866 && ! processing_explicit_instantiation)
17867 warning (OPT_Wattributes,
17868 "attributes ignored on template instantiation");
17869 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17870 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17871 else
17872 warning (OPT_Wattributes,
17873 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17876 if (tag_type != enum_type)
17878 /* Indicate whether this class was declared as a `class' or as a
17879 `struct'. */
17880 if (CLASS_TYPE_P (type))
17881 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17882 cp_parser_check_class_key (tag_type, type);
17885 /* A "<" cannot follow an elaborated type specifier. If that
17886 happens, the user was probably trying to form a template-id. */
17887 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17888 token->location);
17890 return type;
17893 /* Parse an enum-specifier.
17895 enum-specifier:
17896 enum-head { enumerator-list [opt] }
17897 enum-head { enumerator-list , } [C++0x]
17899 enum-head:
17900 enum-key identifier [opt] enum-base [opt]
17901 enum-key nested-name-specifier identifier enum-base [opt]
17903 enum-key:
17904 enum
17905 enum class [C++0x]
17906 enum struct [C++0x]
17908 enum-base: [C++0x]
17909 : type-specifier-seq
17911 opaque-enum-specifier:
17912 enum-key identifier enum-base [opt] ;
17914 GNU Extensions:
17915 enum-key attributes[opt] identifier [opt] enum-base [opt]
17916 { enumerator-list [opt] }attributes[opt]
17917 enum-key attributes[opt] identifier [opt] enum-base [opt]
17918 { enumerator-list, }attributes[opt] [C++0x]
17920 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17921 if the token stream isn't an enum-specifier after all. */
17923 static tree
17924 cp_parser_enum_specifier (cp_parser* parser)
17926 tree identifier;
17927 tree type = NULL_TREE;
17928 tree prev_scope;
17929 tree nested_name_specifier = NULL_TREE;
17930 tree attributes;
17931 bool scoped_enum_p = false;
17932 bool has_underlying_type = false;
17933 bool nested_being_defined = false;
17934 bool new_value_list = false;
17935 bool is_new_type = false;
17936 bool is_unnamed = false;
17937 tree underlying_type = NULL_TREE;
17938 cp_token *type_start_token = NULL;
17939 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17941 parser->colon_corrects_to_scope_p = false;
17943 /* Parse tentatively so that we can back up if we don't find a
17944 enum-specifier. */
17945 cp_parser_parse_tentatively (parser);
17947 /* Caller guarantees that the current token is 'enum', an identifier
17948 possibly follows, and the token after that is an opening brace.
17949 If we don't have an identifier, fabricate an anonymous name for
17950 the enumeration being defined. */
17951 cp_lexer_consume_token (parser->lexer);
17953 /* Parse the "class" or "struct", which indicates a scoped
17954 enumeration type in C++0x. */
17955 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17956 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17958 if (cxx_dialect < cxx11)
17959 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17961 /* Consume the `struct' or `class' token. */
17962 cp_lexer_consume_token (parser->lexer);
17964 scoped_enum_p = true;
17967 attributes = cp_parser_attributes_opt (parser);
17969 /* Clear the qualification. */
17970 parser->scope = NULL_TREE;
17971 parser->qualifying_scope = NULL_TREE;
17972 parser->object_scope = NULL_TREE;
17974 /* Figure out in what scope the declaration is being placed. */
17975 prev_scope = current_scope ();
17977 type_start_token = cp_lexer_peek_token (parser->lexer);
17979 push_deferring_access_checks (dk_no_check);
17980 nested_name_specifier
17981 = cp_parser_nested_name_specifier_opt (parser,
17982 /*typename_keyword_p=*/true,
17983 /*check_dependency_p=*/false,
17984 /*type_p=*/false,
17985 /*is_declaration=*/false);
17987 if (nested_name_specifier)
17989 tree name;
17991 identifier = cp_parser_identifier (parser);
17992 name = cp_parser_lookup_name (parser, identifier,
17993 enum_type,
17994 /*is_template=*/false,
17995 /*is_namespace=*/false,
17996 /*check_dependency=*/true,
17997 /*ambiguous_decls=*/NULL,
17998 input_location);
17999 if (name && name != error_mark_node)
18001 type = TREE_TYPE (name);
18002 if (TREE_CODE (type) == TYPENAME_TYPE)
18004 /* Are template enums allowed in ISO? */
18005 if (template_parm_scope_p ())
18006 pedwarn (type_start_token->location, OPT_Wpedantic,
18007 "%qD is an enumeration template", name);
18008 /* ignore a typename reference, for it will be solved by name
18009 in start_enum. */
18010 type = NULL_TREE;
18013 else if (nested_name_specifier == error_mark_node)
18014 /* We already issued an error. */;
18015 else
18017 error_at (type_start_token->location,
18018 "%qD does not name an enumeration in %qT",
18019 identifier, nested_name_specifier);
18020 nested_name_specifier = error_mark_node;
18023 else
18025 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18026 identifier = cp_parser_identifier (parser);
18027 else
18029 identifier = make_anon_name ();
18030 is_unnamed = true;
18031 if (scoped_enum_p)
18032 error_at (type_start_token->location,
18033 "unnamed scoped enum is not allowed");
18036 pop_deferring_access_checks ();
18038 /* Check for the `:' that denotes a specified underlying type in C++0x.
18039 Note that a ':' could also indicate a bitfield width, however. */
18040 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18042 cp_decl_specifier_seq type_specifiers;
18044 /* Consume the `:'. */
18045 cp_lexer_consume_token (parser->lexer);
18047 /* Parse the type-specifier-seq. */
18048 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18049 /*is_trailing_return=*/false,
18050 &type_specifiers);
18052 /* At this point this is surely not elaborated type specifier. */
18053 if (!cp_parser_parse_definitely (parser))
18054 return NULL_TREE;
18056 if (cxx_dialect < cxx11)
18057 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18059 has_underlying_type = true;
18061 /* If that didn't work, stop. */
18062 if (type_specifiers.type != error_mark_node)
18064 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18065 /*initialized=*/0, NULL);
18066 if (underlying_type == error_mark_node
18067 || check_for_bare_parameter_packs (underlying_type))
18068 underlying_type = NULL_TREE;
18072 /* Look for the `{' but don't consume it yet. */
18073 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18075 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18077 cp_parser_error (parser, "expected %<{%>");
18078 if (has_underlying_type)
18080 type = NULL_TREE;
18081 goto out;
18084 /* An opaque-enum-specifier must have a ';' here. */
18085 if ((scoped_enum_p || underlying_type)
18086 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18088 cp_parser_error (parser, "expected %<;%> or %<{%>");
18089 if (has_underlying_type)
18091 type = NULL_TREE;
18092 goto out;
18097 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18098 return NULL_TREE;
18100 if (nested_name_specifier)
18102 if (CLASS_TYPE_P (nested_name_specifier))
18104 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18105 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18106 push_scope (nested_name_specifier);
18108 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18110 push_nested_namespace (nested_name_specifier);
18114 /* Issue an error message if type-definitions are forbidden here. */
18115 if (!cp_parser_check_type_definition (parser))
18116 type = error_mark_node;
18117 else
18118 /* Create the new type. We do this before consuming the opening
18119 brace so the enum will be recorded as being on the line of its
18120 tag (or the 'enum' keyword, if there is no tag). */
18121 type = start_enum (identifier, type, underlying_type,
18122 attributes, scoped_enum_p, &is_new_type);
18124 /* If the next token is not '{' it is an opaque-enum-specifier or an
18125 elaborated-type-specifier. */
18126 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18128 timevar_push (TV_PARSE_ENUM);
18129 if (nested_name_specifier
18130 && nested_name_specifier != error_mark_node)
18132 /* The following catches invalid code such as:
18133 enum class S<int>::E { A, B, C }; */
18134 if (!processing_specialization
18135 && CLASS_TYPE_P (nested_name_specifier)
18136 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18137 error_at (type_start_token->location, "cannot add an enumerator "
18138 "list to a template instantiation");
18140 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18142 error_at (type_start_token->location,
18143 "%<%T::%E%> has not been declared",
18144 TYPE_CONTEXT (nested_name_specifier),
18145 nested_name_specifier);
18146 type = error_mark_node;
18148 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18149 && !CLASS_TYPE_P (nested_name_specifier))
18151 error_at (type_start_token->location, "nested name specifier "
18152 "%qT for enum declaration does not name a class "
18153 "or namespace", nested_name_specifier);
18154 type = error_mark_node;
18156 /* If that scope does not contain the scope in which the
18157 class was originally declared, the program is invalid. */
18158 else if (prev_scope && !is_ancestor (prev_scope,
18159 nested_name_specifier))
18161 if (at_namespace_scope_p ())
18162 error_at (type_start_token->location,
18163 "declaration of %qD in namespace %qD which does not "
18164 "enclose %qD",
18165 type, prev_scope, nested_name_specifier);
18166 else
18167 error_at (type_start_token->location,
18168 "declaration of %qD in %qD which does not "
18169 "enclose %qD",
18170 type, prev_scope, nested_name_specifier);
18171 type = error_mark_node;
18173 /* If that scope is the scope where the declaration is being placed
18174 the program is invalid. */
18175 else if (CLASS_TYPE_P (nested_name_specifier)
18176 && CLASS_TYPE_P (prev_scope)
18177 && same_type_p (nested_name_specifier, prev_scope))
18179 permerror (type_start_token->location,
18180 "extra qualification not allowed");
18181 nested_name_specifier = NULL_TREE;
18185 if (scoped_enum_p)
18186 begin_scope (sk_scoped_enum, type);
18188 /* Consume the opening brace. */
18189 matching_braces braces;
18190 braces.consume_open (parser);
18192 if (type == error_mark_node)
18193 ; /* Nothing to add */
18194 else if (OPAQUE_ENUM_P (type)
18195 || (cxx_dialect > cxx98 && processing_specialization))
18197 new_value_list = true;
18198 SET_OPAQUE_ENUM_P (type, false);
18199 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18201 else
18203 error_at (type_start_token->location,
18204 "multiple definition of %q#T", type);
18205 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18206 "previous definition here");
18207 type = error_mark_node;
18210 if (type == error_mark_node)
18211 cp_parser_skip_to_end_of_block_or_statement (parser);
18212 /* If the next token is not '}', then there are some enumerators. */
18213 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18215 if (is_unnamed && !scoped_enum_p)
18216 pedwarn (type_start_token->location, OPT_Wpedantic,
18217 "ISO C++ forbids empty unnamed enum");
18219 else
18220 cp_parser_enumerator_list (parser, type);
18222 /* Consume the final '}'. */
18223 braces.require_close (parser);
18225 if (scoped_enum_p)
18226 finish_scope ();
18227 timevar_pop (TV_PARSE_ENUM);
18229 else
18231 /* If a ';' follows, then it is an opaque-enum-specifier
18232 and additional restrictions apply. */
18233 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18235 if (is_unnamed)
18236 error_at (type_start_token->location,
18237 "opaque-enum-specifier without name");
18238 else if (nested_name_specifier)
18239 error_at (type_start_token->location,
18240 "opaque-enum-specifier must use a simple identifier");
18244 /* Look for trailing attributes to apply to this enumeration, and
18245 apply them if appropriate. */
18246 if (cp_parser_allow_gnu_extensions_p (parser))
18248 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18249 cplus_decl_attributes (&type,
18250 trailing_attr,
18251 (int) ATTR_FLAG_TYPE_IN_PLACE);
18254 /* Finish up the enumeration. */
18255 if (type != error_mark_node)
18257 if (new_value_list)
18258 finish_enum_value_list (type);
18259 if (is_new_type)
18260 finish_enum (type);
18263 if (nested_name_specifier)
18265 if (CLASS_TYPE_P (nested_name_specifier))
18267 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18268 pop_scope (nested_name_specifier);
18270 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18272 pop_nested_namespace (nested_name_specifier);
18275 out:
18276 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18277 return type;
18280 /* Parse an enumerator-list. The enumerators all have the indicated
18281 TYPE.
18283 enumerator-list:
18284 enumerator-definition
18285 enumerator-list , enumerator-definition */
18287 static void
18288 cp_parser_enumerator_list (cp_parser* parser, tree type)
18290 while (true)
18292 /* Parse an enumerator-definition. */
18293 cp_parser_enumerator_definition (parser, type);
18295 /* If the next token is not a ',', we've reached the end of
18296 the list. */
18297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18298 break;
18299 /* Otherwise, consume the `,' and keep going. */
18300 cp_lexer_consume_token (parser->lexer);
18301 /* If the next token is a `}', there is a trailing comma. */
18302 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18304 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18305 pedwarn (input_location, OPT_Wpedantic,
18306 "comma at end of enumerator list");
18307 break;
18312 /* Parse an enumerator-definition. The enumerator has the indicated
18313 TYPE.
18315 enumerator-definition:
18316 enumerator
18317 enumerator = constant-expression
18319 enumerator:
18320 identifier
18322 GNU Extensions:
18324 enumerator-definition:
18325 enumerator attributes [opt]
18326 enumerator attributes [opt] = constant-expression */
18328 static void
18329 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18331 tree identifier;
18332 tree value;
18333 location_t loc;
18335 /* Save the input location because we are interested in the location
18336 of the identifier and not the location of the explicit value. */
18337 loc = cp_lexer_peek_token (parser->lexer)->location;
18339 /* Look for the identifier. */
18340 identifier = cp_parser_identifier (parser);
18341 if (identifier == error_mark_node)
18342 return;
18344 /* Parse any specified attributes. */
18345 tree attrs = cp_parser_attributes_opt (parser);
18347 /* If the next token is an '=', then there is an explicit value. */
18348 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18350 /* Consume the `=' token. */
18351 cp_lexer_consume_token (parser->lexer);
18352 /* Parse the value. */
18353 value = cp_parser_constant_expression (parser);
18355 else
18356 value = NULL_TREE;
18358 /* If we are processing a template, make sure the initializer of the
18359 enumerator doesn't contain any bare template parameter pack. */
18360 if (check_for_bare_parameter_packs (value))
18361 value = error_mark_node;
18363 /* Create the enumerator. */
18364 build_enumerator (identifier, value, type, attrs, loc);
18367 /* Parse a namespace-name.
18369 namespace-name:
18370 original-namespace-name
18371 namespace-alias
18373 Returns the NAMESPACE_DECL for the namespace. */
18375 static tree
18376 cp_parser_namespace_name (cp_parser* parser)
18378 tree identifier;
18379 tree namespace_decl;
18381 cp_token *token = cp_lexer_peek_token (parser->lexer);
18383 /* Get the name of the namespace. */
18384 identifier = cp_parser_identifier (parser);
18385 if (identifier == error_mark_node)
18386 return error_mark_node;
18388 /* Look up the identifier in the currently active scope. Look only
18389 for namespaces, due to:
18391 [basic.lookup.udir]
18393 When looking up a namespace-name in a using-directive or alias
18394 definition, only namespace names are considered.
18396 And:
18398 [basic.lookup.qual]
18400 During the lookup of a name preceding the :: scope resolution
18401 operator, object, function, and enumerator names are ignored.
18403 (Note that cp_parser_qualifying_entity only calls this
18404 function if the token after the name is the scope resolution
18405 operator.) */
18406 namespace_decl = cp_parser_lookup_name (parser, identifier,
18407 none_type,
18408 /*is_template=*/false,
18409 /*is_namespace=*/true,
18410 /*check_dependency=*/true,
18411 /*ambiguous_decls=*/NULL,
18412 token->location);
18413 /* If it's not a namespace, issue an error. */
18414 if (namespace_decl == error_mark_node
18415 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18417 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18419 error_at (token->location, "%qD is not a namespace-name", identifier);
18420 if (namespace_decl == error_mark_node
18421 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18422 suggest_alternative_in_explicit_scope (token->location, identifier,
18423 parser->scope);
18425 cp_parser_error (parser, "expected namespace-name");
18426 namespace_decl = error_mark_node;
18429 return namespace_decl;
18432 /* Parse a namespace-definition.
18434 namespace-definition:
18435 named-namespace-definition
18436 unnamed-namespace-definition
18438 named-namespace-definition:
18439 original-namespace-definition
18440 extension-namespace-definition
18442 original-namespace-definition:
18443 namespace identifier { namespace-body }
18445 extension-namespace-definition:
18446 namespace original-namespace-name { namespace-body }
18448 unnamed-namespace-definition:
18449 namespace { namespace-body } */
18451 static void
18452 cp_parser_namespace_definition (cp_parser* parser)
18454 tree identifier;
18455 int nested_definition_count = 0;
18457 cp_ensure_no_omp_declare_simd (parser);
18458 cp_ensure_no_oacc_routine (parser);
18460 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18462 if (is_inline)
18464 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18465 cp_lexer_consume_token (parser->lexer);
18468 /* Look for the `namespace' keyword. */
18469 cp_token* token
18470 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18472 /* Parse any specified attributes before the identifier. */
18473 tree attribs = cp_parser_attributes_opt (parser);
18475 for (;;)
18477 identifier = NULL_TREE;
18479 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18481 identifier = cp_parser_identifier (parser);
18483 /* Parse any attributes specified after the identifier. */
18484 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18487 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18488 break;
18490 if (!nested_definition_count && cxx_dialect < cxx17)
18491 pedwarn (input_location, OPT_Wpedantic,
18492 "nested namespace definitions only available with "
18493 "-std=c++17 or -std=gnu++17");
18495 /* Nested namespace names can create new namespaces (unlike
18496 other qualified-ids). */
18497 if (int count = identifier ? push_namespace (identifier) : 0)
18498 nested_definition_count += count;
18499 else
18500 cp_parser_error (parser, "nested namespace name required");
18501 cp_lexer_consume_token (parser->lexer);
18504 if (nested_definition_count && !identifier)
18505 cp_parser_error (parser, "namespace name required");
18507 if (nested_definition_count && attribs)
18508 error_at (token->location,
18509 "a nested namespace definition cannot have attributes");
18510 if (nested_definition_count && is_inline)
18511 error_at (token->location,
18512 "a nested namespace definition cannot be inline");
18514 /* Start the namespace. */
18515 nested_definition_count += push_namespace (identifier, is_inline);
18517 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18519 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18521 /* Look for the `{' to validate starting the namespace. */
18522 matching_braces braces;
18523 if (braces.require_open (parser))
18525 /* Parse the body of the namespace. */
18526 cp_parser_namespace_body (parser);
18528 /* Look for the final `}'. */
18529 braces.require_close (parser);
18532 if (has_visibility)
18533 pop_visibility (1);
18535 /* Pop the nested namespace definitions. */
18536 while (nested_definition_count--)
18537 pop_namespace ();
18540 /* Parse a namespace-body.
18542 namespace-body:
18543 declaration-seq [opt] */
18545 static void
18546 cp_parser_namespace_body (cp_parser* parser)
18548 cp_parser_declaration_seq_opt (parser);
18551 /* Parse a namespace-alias-definition.
18553 namespace-alias-definition:
18554 namespace identifier = qualified-namespace-specifier ; */
18556 static void
18557 cp_parser_namespace_alias_definition (cp_parser* parser)
18559 tree identifier;
18560 tree namespace_specifier;
18562 cp_token *token = cp_lexer_peek_token (parser->lexer);
18564 /* Look for the `namespace' keyword. */
18565 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18566 /* Look for the identifier. */
18567 identifier = cp_parser_identifier (parser);
18568 if (identifier == error_mark_node)
18569 return;
18570 /* Look for the `=' token. */
18571 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18572 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18574 error_at (token->location, "%<namespace%> definition is not allowed here");
18575 /* Skip the definition. */
18576 cp_lexer_consume_token (parser->lexer);
18577 if (cp_parser_skip_to_closing_brace (parser))
18578 cp_lexer_consume_token (parser->lexer);
18579 return;
18581 cp_parser_require (parser, CPP_EQ, RT_EQ);
18582 /* Look for the qualified-namespace-specifier. */
18583 namespace_specifier
18584 = cp_parser_qualified_namespace_specifier (parser);
18585 /* Look for the `;' token. */
18586 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18588 /* Register the alias in the symbol table. */
18589 do_namespace_alias (identifier, namespace_specifier);
18592 /* Parse a qualified-namespace-specifier.
18594 qualified-namespace-specifier:
18595 :: [opt] nested-name-specifier [opt] namespace-name
18597 Returns a NAMESPACE_DECL corresponding to the specified
18598 namespace. */
18600 static tree
18601 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18603 /* Look for the optional `::'. */
18604 cp_parser_global_scope_opt (parser,
18605 /*current_scope_valid_p=*/false);
18607 /* Look for the optional nested-name-specifier. */
18608 cp_parser_nested_name_specifier_opt (parser,
18609 /*typename_keyword_p=*/false,
18610 /*check_dependency_p=*/true,
18611 /*type_p=*/false,
18612 /*is_declaration=*/true);
18614 return cp_parser_namespace_name (parser);
18617 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18618 access declaration.
18620 using-declaration:
18621 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18622 using :: unqualified-id ;
18624 access-declaration:
18625 qualified-id ;
18629 static bool
18630 cp_parser_using_declaration (cp_parser* parser,
18631 bool access_declaration_p)
18633 cp_token *token;
18634 bool typename_p = false;
18635 bool global_scope_p;
18636 tree decl;
18637 tree identifier;
18638 tree qscope;
18639 int oldcount = errorcount;
18640 cp_token *diag_token = NULL;
18642 if (access_declaration_p)
18644 diag_token = cp_lexer_peek_token (parser->lexer);
18645 cp_parser_parse_tentatively (parser);
18647 else
18649 /* Look for the `using' keyword. */
18650 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18652 again:
18653 /* Peek at the next token. */
18654 token = cp_lexer_peek_token (parser->lexer);
18655 /* See if it's `typename'. */
18656 if (token->keyword == RID_TYPENAME)
18658 /* Remember that we've seen it. */
18659 typename_p = true;
18660 /* Consume the `typename' token. */
18661 cp_lexer_consume_token (parser->lexer);
18665 /* Look for the optional global scope qualification. */
18666 global_scope_p
18667 = (cp_parser_global_scope_opt (parser,
18668 /*current_scope_valid_p=*/false)
18669 != NULL_TREE);
18671 /* If we saw `typename', or didn't see `::', then there must be a
18672 nested-name-specifier present. */
18673 if (typename_p || !global_scope_p)
18675 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18676 /*check_dependency_p=*/true,
18677 /*type_p=*/false,
18678 /*is_declaration=*/true);
18679 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18681 cp_parser_skip_to_end_of_block_or_statement (parser);
18682 return false;
18685 /* Otherwise, we could be in either of the two productions. In that
18686 case, treat the nested-name-specifier as optional. */
18687 else
18688 qscope = cp_parser_nested_name_specifier_opt (parser,
18689 /*typename_keyword_p=*/false,
18690 /*check_dependency_p=*/true,
18691 /*type_p=*/false,
18692 /*is_declaration=*/true);
18693 if (!qscope)
18694 qscope = global_namespace;
18695 else if (UNSCOPED_ENUM_P (qscope))
18696 qscope = CP_TYPE_CONTEXT (qscope);
18698 if (access_declaration_p && cp_parser_error_occurred (parser))
18699 /* Something has already gone wrong; there's no need to parse
18700 further. Since an error has occurred, the return value of
18701 cp_parser_parse_definitely will be false, as required. */
18702 return cp_parser_parse_definitely (parser);
18704 token = cp_lexer_peek_token (parser->lexer);
18705 /* Parse the unqualified-id. */
18706 identifier = cp_parser_unqualified_id (parser,
18707 /*template_keyword_p=*/false,
18708 /*check_dependency_p=*/true,
18709 /*declarator_p=*/true,
18710 /*optional_p=*/false);
18712 if (access_declaration_p)
18714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18715 cp_parser_simulate_error (parser);
18716 if (!cp_parser_parse_definitely (parser))
18717 return false;
18719 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18721 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18722 if (cxx_dialect < cxx17
18723 && !in_system_header_at (ell->location))
18724 pedwarn (ell->location, 0,
18725 "pack expansion in using-declaration only available "
18726 "with -std=c++17 or -std=gnu++17");
18727 qscope = make_pack_expansion (qscope);
18730 /* The function we call to handle a using-declaration is different
18731 depending on what scope we are in. */
18732 if (qscope == error_mark_node || identifier == error_mark_node)
18734 else if (!identifier_p (identifier)
18735 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18736 /* [namespace.udecl]
18738 A using declaration shall not name a template-id. */
18739 error_at (token->location,
18740 "a template-id may not appear in a using-declaration");
18741 else
18743 if (at_class_scope_p ())
18745 /* Create the USING_DECL. */
18746 decl = do_class_using_decl (qscope, identifier);
18748 if (decl && typename_p)
18749 USING_DECL_TYPENAME_P (decl) = 1;
18751 if (check_for_bare_parameter_packs (decl))
18753 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18754 return false;
18756 else
18757 /* Add it to the list of members in this class. */
18758 finish_member_declaration (decl);
18760 else
18762 decl = cp_parser_lookup_name_simple (parser,
18763 identifier,
18764 token->location);
18765 if (decl == error_mark_node)
18766 cp_parser_name_lookup_error (parser, identifier,
18767 decl, NLE_NULL,
18768 token->location);
18769 else if (check_for_bare_parameter_packs (decl))
18771 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18772 return false;
18774 else if (!at_namespace_scope_p ())
18775 finish_local_using_decl (decl, qscope, identifier);
18776 else
18777 finish_namespace_using_decl (decl, qscope, identifier);
18781 if (!access_declaration_p
18782 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18784 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18785 if (cxx_dialect < cxx17)
18786 pedwarn (comma->location, 0,
18787 "comma-separated list in using-declaration only available "
18788 "with -std=c++17 or -std=gnu++17");
18789 goto again;
18792 /* Look for the final `;'. */
18793 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18795 if (access_declaration_p && errorcount == oldcount)
18796 warning_at (diag_token->location, OPT_Wdeprecated,
18797 "access declarations are deprecated "
18798 "in favour of using-declarations; "
18799 "suggestion: add the %<using%> keyword");
18801 return true;
18804 /* Parse an alias-declaration.
18806 alias-declaration:
18807 using identifier attribute-specifier-seq [opt] = type-id */
18809 static tree
18810 cp_parser_alias_declaration (cp_parser* parser)
18812 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18813 location_t id_location;
18814 cp_declarator *declarator;
18815 cp_decl_specifier_seq decl_specs;
18816 bool member_p;
18817 const char *saved_message = NULL;
18819 /* Look for the `using' keyword. */
18820 cp_token *using_token
18821 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18822 if (using_token == NULL)
18823 return error_mark_node;
18825 id_location = cp_lexer_peek_token (parser->lexer)->location;
18826 id = cp_parser_identifier (parser);
18827 if (id == error_mark_node)
18828 return error_mark_node;
18830 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18831 attributes = cp_parser_attributes_opt (parser);
18832 if (attributes == error_mark_node)
18833 return error_mark_node;
18835 cp_parser_require (parser, CPP_EQ, RT_EQ);
18837 if (cp_parser_error_occurred (parser))
18838 return error_mark_node;
18840 cp_parser_commit_to_tentative_parse (parser);
18842 /* Now we are going to parse the type-id of the declaration. */
18845 [dcl.type]/3 says:
18847 "A type-specifier-seq shall not define a class or enumeration
18848 unless it appears in the type-id of an alias-declaration (7.1.3) that
18849 is not the declaration of a template-declaration."
18851 In other words, if we currently are in an alias template, the
18852 type-id should not define a type.
18854 So let's set parser->type_definition_forbidden_message in that
18855 case; cp_parser_check_type_definition (called by
18856 cp_parser_class_specifier) will then emit an error if a type is
18857 defined in the type-id. */
18858 if (parser->num_template_parameter_lists)
18860 saved_message = parser->type_definition_forbidden_message;
18861 parser->type_definition_forbidden_message =
18862 G_("types may not be defined in alias template declarations");
18865 type = cp_parser_type_id (parser);
18867 /* Restore the error message if need be. */
18868 if (parser->num_template_parameter_lists)
18869 parser->type_definition_forbidden_message = saved_message;
18871 if (type == error_mark_node
18872 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18874 cp_parser_skip_to_end_of_block_or_statement (parser);
18875 return error_mark_node;
18878 /* A typedef-name can also be introduced by an alias-declaration. The
18879 identifier following the using keyword becomes a typedef-name. It has
18880 the same semantics as if it were introduced by the typedef
18881 specifier. In particular, it does not define a new type and it shall
18882 not appear in the type-id. */
18884 clear_decl_specs (&decl_specs);
18885 decl_specs.type = type;
18886 if (attributes != NULL_TREE)
18888 decl_specs.attributes = attributes;
18889 set_and_check_decl_spec_loc (&decl_specs,
18890 ds_attribute,
18891 attrs_token);
18893 set_and_check_decl_spec_loc (&decl_specs,
18894 ds_typedef,
18895 using_token);
18896 set_and_check_decl_spec_loc (&decl_specs,
18897 ds_alias,
18898 using_token);
18900 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18901 declarator->id_loc = id_location;
18903 member_p = at_class_scope_p ();
18904 if (member_p)
18905 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18906 NULL_TREE, attributes);
18907 else
18908 decl = start_decl (declarator, &decl_specs, 0,
18909 attributes, NULL_TREE, &pushed_scope);
18910 if (decl == error_mark_node)
18911 return decl;
18913 // Attach constraints to the alias declaration.
18914 if (flag_concepts && current_template_parms)
18916 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18917 tree constr = build_constraints (reqs, NULL_TREE);
18918 set_constraints (decl, constr);
18921 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18923 if (pushed_scope)
18924 pop_scope (pushed_scope);
18926 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18927 added into the symbol table; otherwise, return the TYPE_DECL. */
18928 if (DECL_LANG_SPECIFIC (decl)
18929 && DECL_TEMPLATE_INFO (decl)
18930 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18932 decl = DECL_TI_TEMPLATE (decl);
18933 if (member_p)
18934 check_member_template (decl);
18937 return decl;
18940 /* Parse a using-directive.
18942 using-directive:
18943 using namespace :: [opt] nested-name-specifier [opt]
18944 namespace-name ; */
18946 static void
18947 cp_parser_using_directive (cp_parser* parser)
18949 tree namespace_decl;
18950 tree attribs;
18952 /* Look for the `using' keyword. */
18953 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18954 /* And the `namespace' keyword. */
18955 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18956 /* Look for the optional `::' operator. */
18957 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18958 /* And the optional nested-name-specifier. */
18959 cp_parser_nested_name_specifier_opt (parser,
18960 /*typename_keyword_p=*/false,
18961 /*check_dependency_p=*/true,
18962 /*type_p=*/false,
18963 /*is_declaration=*/true);
18964 /* Get the namespace being used. */
18965 namespace_decl = cp_parser_namespace_name (parser);
18966 /* And any specified attributes. */
18967 attribs = cp_parser_attributes_opt (parser);
18969 /* Update the symbol table. */
18970 if (namespace_bindings_p ())
18971 finish_namespace_using_directive (namespace_decl, attribs);
18972 else
18973 finish_local_using_directive (namespace_decl, attribs);
18975 /* Look for the final `;'. */
18976 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18979 /* Parse an asm-definition.
18981 asm-definition:
18982 asm ( string-literal ) ;
18984 GNU Extension:
18986 asm-definition:
18987 asm volatile [opt] ( string-literal ) ;
18988 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18989 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18990 : asm-operand-list [opt] ) ;
18991 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18992 : asm-operand-list [opt]
18993 : asm-clobber-list [opt] ) ;
18994 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18995 : asm-clobber-list [opt]
18996 : asm-goto-list ) ; */
18998 static void
18999 cp_parser_asm_definition (cp_parser* parser)
19001 tree string;
19002 tree outputs = NULL_TREE;
19003 tree inputs = NULL_TREE;
19004 tree clobbers = NULL_TREE;
19005 tree labels = NULL_TREE;
19006 tree asm_stmt;
19007 bool volatile_p = false;
19008 bool extended_p = false;
19009 bool invalid_inputs_p = false;
19010 bool invalid_outputs_p = false;
19011 bool goto_p = false;
19012 required_token missing = RT_NONE;
19014 /* Look for the `asm' keyword. */
19015 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19017 if (parser->in_function_body
19018 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19020 error ("%<asm%> in %<constexpr%> function");
19021 cp_function_chain->invalid_constexpr = true;
19024 /* See if the next token is `volatile'. */
19025 if (cp_parser_allow_gnu_extensions_p (parser)
19026 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19028 /* Remember that we saw the `volatile' keyword. */
19029 volatile_p = true;
19030 /* Consume the token. */
19031 cp_lexer_consume_token (parser->lexer);
19033 if (cp_parser_allow_gnu_extensions_p (parser)
19034 && parser->in_function_body
19035 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19037 /* Remember that we saw the `goto' keyword. */
19038 goto_p = true;
19039 /* Consume the token. */
19040 cp_lexer_consume_token (parser->lexer);
19042 /* Look for the opening `('. */
19043 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19044 return;
19045 /* Look for the string. */
19046 string = cp_parser_string_literal (parser, false, false);
19047 if (string == error_mark_node)
19049 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19050 /*consume_paren=*/true);
19051 return;
19054 /* If we're allowing GNU extensions, check for the extended assembly
19055 syntax. Unfortunately, the `:' tokens need not be separated by
19056 a space in C, and so, for compatibility, we tolerate that here
19057 too. Doing that means that we have to treat the `::' operator as
19058 two `:' tokens. */
19059 if (cp_parser_allow_gnu_extensions_p (parser)
19060 && parser->in_function_body
19061 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19062 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19064 bool inputs_p = false;
19065 bool clobbers_p = false;
19066 bool labels_p = false;
19068 /* The extended syntax was used. */
19069 extended_p = true;
19071 /* Look for outputs. */
19072 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19074 /* Consume the `:'. */
19075 cp_lexer_consume_token (parser->lexer);
19076 /* Parse the output-operands. */
19077 if (cp_lexer_next_token_is_not (parser->lexer,
19078 CPP_COLON)
19079 && cp_lexer_next_token_is_not (parser->lexer,
19080 CPP_SCOPE)
19081 && cp_lexer_next_token_is_not (parser->lexer,
19082 CPP_CLOSE_PAREN)
19083 && !goto_p)
19085 outputs = cp_parser_asm_operand_list (parser);
19086 if (outputs == error_mark_node)
19087 invalid_outputs_p = true;
19090 /* If the next token is `::', there are no outputs, and the
19091 next token is the beginning of the inputs. */
19092 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19093 /* The inputs are coming next. */
19094 inputs_p = true;
19096 /* Look for inputs. */
19097 if (inputs_p
19098 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19100 /* Consume the `:' or `::'. */
19101 cp_lexer_consume_token (parser->lexer);
19102 /* Parse the output-operands. */
19103 if (cp_lexer_next_token_is_not (parser->lexer,
19104 CPP_COLON)
19105 && cp_lexer_next_token_is_not (parser->lexer,
19106 CPP_SCOPE)
19107 && cp_lexer_next_token_is_not (parser->lexer,
19108 CPP_CLOSE_PAREN))
19110 inputs = cp_parser_asm_operand_list (parser);
19111 if (inputs == error_mark_node)
19112 invalid_inputs_p = true;
19115 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19116 /* The clobbers are coming next. */
19117 clobbers_p = true;
19119 /* Look for clobbers. */
19120 if (clobbers_p
19121 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19123 clobbers_p = true;
19124 /* Consume the `:' or `::'. */
19125 cp_lexer_consume_token (parser->lexer);
19126 /* Parse the clobbers. */
19127 if (cp_lexer_next_token_is_not (parser->lexer,
19128 CPP_COLON)
19129 && cp_lexer_next_token_is_not (parser->lexer,
19130 CPP_CLOSE_PAREN))
19131 clobbers = cp_parser_asm_clobber_list (parser);
19133 else if (goto_p
19134 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19135 /* The labels are coming next. */
19136 labels_p = true;
19138 /* Look for labels. */
19139 if (labels_p
19140 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19142 labels_p = true;
19143 /* Consume the `:' or `::'. */
19144 cp_lexer_consume_token (parser->lexer);
19145 /* Parse the labels. */
19146 labels = cp_parser_asm_label_list (parser);
19149 if (goto_p && !labels_p)
19150 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19152 else if (goto_p)
19153 missing = RT_COLON_SCOPE;
19155 /* Look for the closing `)'. */
19156 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19157 missing ? missing : RT_CLOSE_PAREN))
19158 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19159 /*consume_paren=*/true);
19160 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19162 if (!invalid_inputs_p && !invalid_outputs_p)
19164 /* Create the ASM_EXPR. */
19165 if (parser->in_function_body)
19167 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19168 inputs, clobbers, labels);
19169 /* If the extended syntax was not used, mark the ASM_EXPR. */
19170 if (!extended_p)
19172 tree temp = asm_stmt;
19173 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19174 temp = TREE_OPERAND (temp, 0);
19176 ASM_INPUT_P (temp) = 1;
19179 else
19180 symtab->finalize_toplevel_asm (string);
19184 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19185 type that comes from the decl-specifier-seq. */
19187 static tree
19188 strip_declarator_types (tree type, cp_declarator *declarator)
19190 for (cp_declarator *d = declarator; d;)
19191 switch (d->kind)
19193 case cdk_id:
19194 case cdk_decomp:
19195 case cdk_error:
19196 d = NULL;
19197 break;
19199 default:
19200 if (TYPE_PTRMEMFUNC_P (type))
19201 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19202 type = TREE_TYPE (type);
19203 d = d->declarator;
19204 break;
19207 return type;
19210 /* Declarators [gram.dcl.decl] */
19212 /* Parse an init-declarator.
19214 init-declarator:
19215 declarator initializer [opt]
19217 GNU Extension:
19219 init-declarator:
19220 declarator asm-specification [opt] attributes [opt] initializer [opt]
19222 function-definition:
19223 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19224 function-body
19225 decl-specifier-seq [opt] declarator function-try-block
19227 GNU Extension:
19229 function-definition:
19230 __extension__ function-definition
19232 TM Extension:
19234 function-definition:
19235 decl-specifier-seq [opt] declarator function-transaction-block
19237 The DECL_SPECIFIERS apply to this declarator. Returns a
19238 representation of the entity declared. If MEMBER_P is TRUE, then
19239 this declarator appears in a class scope. The new DECL created by
19240 this declarator is returned.
19242 The CHECKS are access checks that should be performed once we know
19243 what entity is being declared (and, therefore, what classes have
19244 befriended it).
19246 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19247 for a function-definition here as well. If the declarator is a
19248 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19249 be TRUE upon return. By that point, the function-definition will
19250 have been completely parsed.
19252 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19253 is FALSE.
19255 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19256 parsed declaration if it is an uninitialized single declarator not followed
19257 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19258 if present, will not be consumed. If returned, this declarator will be
19259 created with SD_INITIALIZED but will not call cp_finish_decl.
19261 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19262 and there is an initializer, the pointed location_t is set to the
19263 location of the '=' or `(', or '{' in C++11 token introducing the
19264 initializer. */
19266 static tree
19267 cp_parser_init_declarator (cp_parser* parser,
19268 cp_decl_specifier_seq *decl_specifiers,
19269 vec<deferred_access_check, va_gc> *checks,
19270 bool function_definition_allowed_p,
19271 bool member_p,
19272 int declares_class_or_enum,
19273 bool* function_definition_p,
19274 tree* maybe_range_for_decl,
19275 location_t* init_loc,
19276 tree* auto_result)
19278 cp_token *token = NULL, *asm_spec_start_token = NULL,
19279 *attributes_start_token = NULL;
19280 cp_declarator *declarator;
19281 tree prefix_attributes;
19282 tree attributes = NULL;
19283 tree asm_specification;
19284 tree initializer;
19285 tree decl = NULL_TREE;
19286 tree scope;
19287 int is_initialized;
19288 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19289 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19290 "(...)". */
19291 enum cpp_ttype initialization_kind;
19292 bool is_direct_init = false;
19293 bool is_non_constant_init;
19294 int ctor_dtor_or_conv_p;
19295 bool friend_p = cp_parser_friend_p (decl_specifiers);
19296 tree pushed_scope = NULL_TREE;
19297 bool range_for_decl_p = false;
19298 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19299 location_t tmp_init_loc = UNKNOWN_LOCATION;
19301 /* Gather the attributes that were provided with the
19302 decl-specifiers. */
19303 prefix_attributes = decl_specifiers->attributes;
19305 /* Assume that this is not the declarator for a function
19306 definition. */
19307 if (function_definition_p)
19308 *function_definition_p = false;
19310 /* Default arguments are only permitted for function parameters. */
19311 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19312 parser->default_arg_ok_p = false;
19314 /* Defer access checks while parsing the declarator; we cannot know
19315 what names are accessible until we know what is being
19316 declared. */
19317 resume_deferring_access_checks ();
19319 token = cp_lexer_peek_token (parser->lexer);
19321 /* Parse the declarator. */
19322 declarator
19323 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19324 &ctor_dtor_or_conv_p,
19325 /*parenthesized_p=*/NULL,
19326 member_p, friend_p);
19327 /* Gather up the deferred checks. */
19328 stop_deferring_access_checks ();
19330 parser->default_arg_ok_p = saved_default_arg_ok_p;
19332 /* If the DECLARATOR was erroneous, there's no need to go
19333 further. */
19334 if (declarator == cp_error_declarator)
19335 return error_mark_node;
19337 /* Check that the number of template-parameter-lists is OK. */
19338 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19339 token->location))
19340 return error_mark_node;
19342 if (declares_class_or_enum & 2)
19343 cp_parser_check_for_definition_in_return_type (declarator,
19344 decl_specifiers->type,
19345 decl_specifiers->locations[ds_type_spec]);
19347 /* Figure out what scope the entity declared by the DECLARATOR is
19348 located in. `grokdeclarator' sometimes changes the scope, so
19349 we compute it now. */
19350 scope = get_scope_of_declarator (declarator);
19352 /* Perform any lookups in the declared type which were thought to be
19353 dependent, but are not in the scope of the declarator. */
19354 decl_specifiers->type
19355 = maybe_update_decl_type (decl_specifiers->type, scope);
19357 /* If we're allowing GNU extensions, look for an
19358 asm-specification. */
19359 if (cp_parser_allow_gnu_extensions_p (parser))
19361 /* Look for an asm-specification. */
19362 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19363 asm_specification = cp_parser_asm_specification_opt (parser);
19365 else
19366 asm_specification = NULL_TREE;
19368 /* Look for attributes. */
19369 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19370 attributes = cp_parser_attributes_opt (parser);
19372 /* Peek at the next token. */
19373 token = cp_lexer_peek_token (parser->lexer);
19375 bool bogus_implicit_tmpl = false;
19377 if (function_declarator_p (declarator))
19379 /* Handle C++17 deduction guides. */
19380 if (!decl_specifiers->type
19381 && ctor_dtor_or_conv_p <= 0
19382 && cxx_dialect >= cxx17)
19384 cp_declarator *id = get_id_declarator (declarator);
19385 tree name = id->u.id.unqualified_name;
19386 parser->scope = id->u.id.qualifying_scope;
19387 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19388 if (tmpl
19389 && (DECL_CLASS_TEMPLATE_P (tmpl)
19390 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19392 id->u.id.unqualified_name = dguide_name (tmpl);
19393 id->u.id.sfk = sfk_deduction_guide;
19394 ctor_dtor_or_conv_p = 1;
19398 /* Check to see if the token indicates the start of a
19399 function-definition. */
19400 if (cp_parser_token_starts_function_definition_p (token))
19402 if (!function_definition_allowed_p)
19404 /* If a function-definition should not appear here, issue an
19405 error message. */
19406 cp_parser_error (parser,
19407 "a function-definition is not allowed here");
19408 return error_mark_node;
19411 location_t func_brace_location
19412 = cp_lexer_peek_token (parser->lexer)->location;
19414 /* Neither attributes nor an asm-specification are allowed
19415 on a function-definition. */
19416 if (asm_specification)
19417 error_at (asm_spec_start_token->location,
19418 "an asm-specification is not allowed "
19419 "on a function-definition");
19420 if (attributes)
19421 error_at (attributes_start_token->location,
19422 "attributes are not allowed "
19423 "on a function-definition");
19424 /* This is a function-definition. */
19425 *function_definition_p = true;
19427 /* Parse the function definition. */
19428 if (member_p)
19429 decl = cp_parser_save_member_function_body (parser,
19430 decl_specifiers,
19431 declarator,
19432 prefix_attributes);
19433 else
19434 decl =
19435 (cp_parser_function_definition_from_specifiers_and_declarator
19436 (parser, decl_specifiers, prefix_attributes, declarator));
19438 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19440 /* This is where the prologue starts... */
19441 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19442 = func_brace_location;
19445 return decl;
19448 else if (parser->fully_implicit_function_template_p)
19450 /* A non-template declaration involving a function parameter list
19451 containing an implicit template parameter will be made into a
19452 template. If the resulting declaration is not going to be an
19453 actual function then finish the template scope here to prevent it.
19454 An error message will be issued once we have a decl to talk about.
19456 FIXME probably we should do type deduction rather than create an
19457 implicit template, but the standard currently doesn't allow it. */
19458 bogus_implicit_tmpl = true;
19459 finish_fully_implicit_template (parser, NULL_TREE);
19462 /* [dcl.dcl]
19464 Only in function declarations for constructors, destructors, type
19465 conversions, and deduction guides can the decl-specifier-seq be omitted.
19467 We explicitly postpone this check past the point where we handle
19468 function-definitions because we tolerate function-definitions
19469 that are missing their return types in some modes. */
19470 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19472 cp_parser_error (parser,
19473 "expected constructor, destructor, or type conversion");
19474 return error_mark_node;
19477 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19478 if (token->type == CPP_EQ
19479 || token->type == CPP_OPEN_PAREN
19480 || token->type == CPP_OPEN_BRACE)
19482 is_initialized = SD_INITIALIZED;
19483 initialization_kind = token->type;
19484 if (maybe_range_for_decl)
19485 *maybe_range_for_decl = error_mark_node;
19486 tmp_init_loc = token->location;
19487 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19488 *init_loc = tmp_init_loc;
19490 if (token->type == CPP_EQ
19491 && function_declarator_p (declarator))
19493 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19494 if (t2->keyword == RID_DEFAULT)
19495 is_initialized = SD_DEFAULTED;
19496 else if (t2->keyword == RID_DELETE)
19497 is_initialized = SD_DELETED;
19500 else
19502 /* If the init-declarator isn't initialized and isn't followed by a
19503 `,' or `;', it's not a valid init-declarator. */
19504 if (token->type != CPP_COMMA
19505 && token->type != CPP_SEMICOLON)
19507 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19508 range_for_decl_p = true;
19509 else
19511 if (!maybe_range_for_decl)
19512 cp_parser_error (parser, "expected initializer");
19513 return error_mark_node;
19516 is_initialized = SD_UNINITIALIZED;
19517 initialization_kind = CPP_EOF;
19520 /* Because start_decl has side-effects, we should only call it if we
19521 know we're going ahead. By this point, we know that we cannot
19522 possibly be looking at any other construct. */
19523 cp_parser_commit_to_tentative_parse (parser);
19525 /* Enter the newly declared entry in the symbol table. If we're
19526 processing a declaration in a class-specifier, we wait until
19527 after processing the initializer. */
19528 if (!member_p)
19530 if (parser->in_unbraced_linkage_specification_p)
19531 decl_specifiers->storage_class = sc_extern;
19532 decl = start_decl (declarator, decl_specifiers,
19533 range_for_decl_p? SD_INITIALIZED : is_initialized,
19534 attributes, prefix_attributes, &pushed_scope);
19535 cp_finalize_omp_declare_simd (parser, decl);
19536 cp_finalize_oacc_routine (parser, decl, false);
19537 /* Adjust location of decl if declarator->id_loc is more appropriate:
19538 set, and decl wasn't merged with another decl, in which case its
19539 location would be different from input_location, and more accurate. */
19540 if (DECL_P (decl)
19541 && declarator->id_loc != UNKNOWN_LOCATION
19542 && DECL_SOURCE_LOCATION (decl) == input_location)
19543 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19545 else if (scope)
19546 /* Enter the SCOPE. That way unqualified names appearing in the
19547 initializer will be looked up in SCOPE. */
19548 pushed_scope = push_scope (scope);
19550 /* Perform deferred access control checks, now that we know in which
19551 SCOPE the declared entity resides. */
19552 if (!member_p && decl)
19554 tree saved_current_function_decl = NULL_TREE;
19556 /* If the entity being declared is a function, pretend that we
19557 are in its scope. If it is a `friend', it may have access to
19558 things that would not otherwise be accessible. */
19559 if (TREE_CODE (decl) == FUNCTION_DECL)
19561 saved_current_function_decl = current_function_decl;
19562 current_function_decl = decl;
19565 /* Perform access checks for template parameters. */
19566 cp_parser_perform_template_parameter_access_checks (checks);
19568 /* Perform the access control checks for the declarator and the
19569 decl-specifiers. */
19570 perform_deferred_access_checks (tf_warning_or_error);
19572 /* Restore the saved value. */
19573 if (TREE_CODE (decl) == FUNCTION_DECL)
19574 current_function_decl = saved_current_function_decl;
19577 /* Parse the initializer. */
19578 initializer = NULL_TREE;
19579 is_direct_init = false;
19580 is_non_constant_init = true;
19581 if (is_initialized)
19583 if (function_declarator_p (declarator))
19585 if (initialization_kind == CPP_EQ)
19586 initializer = cp_parser_pure_specifier (parser);
19587 else
19589 /* If the declaration was erroneous, we don't really
19590 know what the user intended, so just silently
19591 consume the initializer. */
19592 if (decl != error_mark_node)
19593 error_at (tmp_init_loc, "initializer provided for function");
19594 cp_parser_skip_to_closing_parenthesis (parser,
19595 /*recovering=*/true,
19596 /*or_comma=*/false,
19597 /*consume_paren=*/true);
19600 else
19602 /* We want to record the extra mangling scope for in-class
19603 initializers of class members and initializers of static data
19604 member templates. The former involves deferring
19605 parsing of the initializer until end of class as with default
19606 arguments. So right here we only handle the latter. */
19607 if (!member_p && processing_template_decl)
19608 start_lambda_scope (decl);
19609 initializer = cp_parser_initializer (parser,
19610 &is_direct_init,
19611 &is_non_constant_init);
19612 if (!member_p && processing_template_decl)
19613 finish_lambda_scope ();
19614 if (initializer == error_mark_node)
19615 cp_parser_skip_to_end_of_statement (parser);
19619 /* The old parser allows attributes to appear after a parenthesized
19620 initializer. Mark Mitchell proposed removing this functionality
19621 on the GCC mailing lists on 2002-08-13. This parser accepts the
19622 attributes -- but ignores them. */
19623 if (cp_parser_allow_gnu_extensions_p (parser)
19624 && initialization_kind == CPP_OPEN_PAREN)
19625 if (cp_parser_attributes_opt (parser))
19626 warning (OPT_Wattributes,
19627 "attributes after parenthesized initializer ignored");
19629 /* And now complain about a non-function implicit template. */
19630 if (bogus_implicit_tmpl && decl != error_mark_node)
19631 error_at (DECL_SOURCE_LOCATION (decl),
19632 "non-function %qD declared as implicit template", decl);
19634 /* For an in-class declaration, use `grokfield' to create the
19635 declaration. */
19636 if (member_p)
19638 if (pushed_scope)
19640 pop_scope (pushed_scope);
19641 pushed_scope = NULL_TREE;
19643 decl = grokfield (declarator, decl_specifiers,
19644 initializer, !is_non_constant_init,
19645 /*asmspec=*/NULL_TREE,
19646 attr_chainon (attributes, prefix_attributes));
19647 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19648 cp_parser_save_default_args (parser, decl);
19649 cp_finalize_omp_declare_simd (parser, decl);
19650 cp_finalize_oacc_routine (parser, decl, false);
19653 /* Finish processing the declaration. But, skip member
19654 declarations. */
19655 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19657 cp_finish_decl (decl,
19658 initializer, !is_non_constant_init,
19659 asm_specification,
19660 /* If the initializer is in parentheses, then this is
19661 a direct-initialization, which means that an
19662 `explicit' constructor is OK. Otherwise, an
19663 `explicit' constructor cannot be used. */
19664 ((is_direct_init || !is_initialized)
19665 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19667 else if ((cxx_dialect != cxx98) && friend_p
19668 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19669 /* Core issue #226 (C++0x only): A default template-argument
19670 shall not be specified in a friend class template
19671 declaration. */
19672 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19673 /*is_partial=*/false, /*is_friend_decl=*/1);
19675 if (!friend_p && pushed_scope)
19676 pop_scope (pushed_scope);
19678 if (function_declarator_p (declarator)
19679 && parser->fully_implicit_function_template_p)
19681 if (member_p)
19682 decl = finish_fully_implicit_template (parser, decl);
19683 else
19684 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19687 if (auto_result && is_initialized && decl_specifiers->type
19688 && type_uses_auto (decl_specifiers->type))
19689 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19691 return decl;
19694 /* Parse a declarator.
19696 declarator:
19697 direct-declarator
19698 ptr-operator declarator
19700 abstract-declarator:
19701 ptr-operator abstract-declarator [opt]
19702 direct-abstract-declarator
19704 GNU Extensions:
19706 declarator:
19707 attributes [opt] direct-declarator
19708 attributes [opt] ptr-operator declarator
19710 abstract-declarator:
19711 attributes [opt] ptr-operator abstract-declarator [opt]
19712 attributes [opt] direct-abstract-declarator
19714 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19715 detect constructors, destructors, deduction guides, or conversion operators.
19716 It is set to -1 if the declarator is a name, and +1 if it is a
19717 function. Otherwise it is set to zero. Usually you just want to
19718 test for >0, but internally the negative value is used.
19720 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19721 a decl-specifier-seq unless it declares a constructor, destructor,
19722 or conversion. It might seem that we could check this condition in
19723 semantic analysis, rather than parsing, but that makes it difficult
19724 to handle something like `f()'. We want to notice that there are
19725 no decl-specifiers, and therefore realize that this is an
19726 expression, not a declaration.)
19728 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19729 the declarator is a direct-declarator of the form "(...)".
19731 MEMBER_P is true iff this declarator is a member-declarator.
19733 FRIEND_P is true iff this declarator is a friend. */
19735 static cp_declarator *
19736 cp_parser_declarator (cp_parser* parser,
19737 cp_parser_declarator_kind dcl_kind,
19738 int* ctor_dtor_or_conv_p,
19739 bool* parenthesized_p,
19740 bool member_p, bool friend_p)
19742 cp_declarator *declarator;
19743 enum tree_code code;
19744 cp_cv_quals cv_quals;
19745 tree class_type;
19746 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19748 /* Assume this is not a constructor, destructor, or type-conversion
19749 operator. */
19750 if (ctor_dtor_or_conv_p)
19751 *ctor_dtor_or_conv_p = 0;
19753 if (cp_parser_allow_gnu_extensions_p (parser))
19754 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19756 /* Check for the ptr-operator production. */
19757 cp_parser_parse_tentatively (parser);
19758 /* Parse the ptr-operator. */
19759 code = cp_parser_ptr_operator (parser,
19760 &class_type,
19761 &cv_quals,
19762 &std_attributes);
19764 /* If that worked, then we have a ptr-operator. */
19765 if (cp_parser_parse_definitely (parser))
19767 /* If a ptr-operator was found, then this declarator was not
19768 parenthesized. */
19769 if (parenthesized_p)
19770 *parenthesized_p = true;
19771 /* The dependent declarator is optional if we are parsing an
19772 abstract-declarator. */
19773 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19774 cp_parser_parse_tentatively (parser);
19776 /* Parse the dependent declarator. */
19777 declarator = cp_parser_declarator (parser, dcl_kind,
19778 /*ctor_dtor_or_conv_p=*/NULL,
19779 /*parenthesized_p=*/NULL,
19780 /*member_p=*/false,
19781 friend_p);
19783 /* If we are parsing an abstract-declarator, we must handle the
19784 case where the dependent declarator is absent. */
19785 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19786 && !cp_parser_parse_definitely (parser))
19787 declarator = NULL;
19789 declarator = cp_parser_make_indirect_declarator
19790 (code, class_type, cv_quals, declarator, std_attributes);
19792 /* Everything else is a direct-declarator. */
19793 else
19795 if (parenthesized_p)
19796 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19797 CPP_OPEN_PAREN);
19798 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19799 ctor_dtor_or_conv_p,
19800 member_p, friend_p);
19803 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19804 declarator->attributes = gnu_attributes;
19805 return declarator;
19808 /* Parse a direct-declarator or direct-abstract-declarator.
19810 direct-declarator:
19811 declarator-id
19812 direct-declarator ( parameter-declaration-clause )
19813 cv-qualifier-seq [opt]
19814 ref-qualifier [opt]
19815 exception-specification [opt]
19816 direct-declarator [ constant-expression [opt] ]
19817 ( declarator )
19819 direct-abstract-declarator:
19820 direct-abstract-declarator [opt]
19821 ( parameter-declaration-clause )
19822 cv-qualifier-seq [opt]
19823 ref-qualifier [opt]
19824 exception-specification [opt]
19825 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19826 ( abstract-declarator )
19828 Returns a representation of the declarator. DCL_KIND is
19829 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19830 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19831 we are parsing a direct-declarator. It is
19832 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19833 of ambiguity we prefer an abstract declarator, as per
19834 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19835 as for cp_parser_declarator. */
19837 static cp_declarator *
19838 cp_parser_direct_declarator (cp_parser* parser,
19839 cp_parser_declarator_kind dcl_kind,
19840 int* ctor_dtor_or_conv_p,
19841 bool member_p, bool friend_p)
19843 cp_token *token;
19844 cp_declarator *declarator = NULL;
19845 tree scope = NULL_TREE;
19846 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19847 bool saved_in_declarator_p = parser->in_declarator_p;
19848 bool first = true;
19849 tree pushed_scope = NULL_TREE;
19850 cp_token *open_paren = NULL, *close_paren = NULL;
19852 while (true)
19854 /* Peek at the next token. */
19855 token = cp_lexer_peek_token (parser->lexer);
19856 if (token->type == CPP_OPEN_PAREN)
19858 /* This is either a parameter-declaration-clause, or a
19859 parenthesized declarator. When we know we are parsing a
19860 named declarator, it must be a parenthesized declarator
19861 if FIRST is true. For instance, `(int)' is a
19862 parameter-declaration-clause, with an omitted
19863 direct-abstract-declarator. But `((*))', is a
19864 parenthesized abstract declarator. Finally, when T is a
19865 template parameter `(T)' is a
19866 parameter-declaration-clause, and not a parenthesized
19867 named declarator.
19869 We first try and parse a parameter-declaration-clause,
19870 and then try a nested declarator (if FIRST is true).
19872 It is not an error for it not to be a
19873 parameter-declaration-clause, even when FIRST is
19874 false. Consider,
19876 int i (int);
19877 int i (3);
19879 The first is the declaration of a function while the
19880 second is the definition of a variable, including its
19881 initializer.
19883 Having seen only the parenthesis, we cannot know which of
19884 these two alternatives should be selected. Even more
19885 complex are examples like:
19887 int i (int (a));
19888 int i (int (3));
19890 The former is a function-declaration; the latter is a
19891 variable initialization.
19893 Thus again, we try a parameter-declaration-clause, and if
19894 that fails, we back out and return. */
19896 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19898 tree params;
19899 bool is_declarator = false;
19901 open_paren = NULL;
19903 /* In a member-declarator, the only valid interpretation
19904 of a parenthesis is the start of a
19905 parameter-declaration-clause. (It is invalid to
19906 initialize a static data member with a parenthesized
19907 initializer; only the "=" form of initialization is
19908 permitted.) */
19909 if (!member_p)
19910 cp_parser_parse_tentatively (parser);
19912 /* Consume the `('. */
19913 matching_parens parens;
19914 parens.consume_open (parser);
19915 if (first)
19917 /* If this is going to be an abstract declarator, we're
19918 in a declarator and we can't have default args. */
19919 parser->default_arg_ok_p = false;
19920 parser->in_declarator_p = true;
19923 begin_scope (sk_function_parms, NULL_TREE);
19925 /* Parse the parameter-declaration-clause. */
19926 params = cp_parser_parameter_declaration_clause (parser);
19928 /* Consume the `)'. */
19929 parens.require_close (parser);
19931 /* If all went well, parse the cv-qualifier-seq,
19932 ref-qualifier and the exception-specification. */
19933 if (member_p || cp_parser_parse_definitely (parser))
19935 cp_cv_quals cv_quals;
19936 cp_virt_specifiers virt_specifiers;
19937 cp_ref_qualifier ref_qual;
19938 tree exception_specification;
19939 tree late_return;
19940 tree attrs;
19941 bool memfn = (member_p || (pushed_scope
19942 && CLASS_TYPE_P (pushed_scope)));
19944 is_declarator = true;
19946 if (ctor_dtor_or_conv_p)
19947 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19948 first = false;
19950 /* Parse the cv-qualifier-seq. */
19951 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19952 /* Parse the ref-qualifier. */
19953 ref_qual = cp_parser_ref_qualifier_opt (parser);
19954 /* Parse the tx-qualifier. */
19955 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19956 /* And the exception-specification. */
19957 exception_specification
19958 = cp_parser_exception_specification_opt (parser);
19960 attrs = cp_parser_std_attribute_spec_seq (parser);
19962 /* In here, we handle cases where attribute is used after
19963 the function declaration. For example:
19964 void func (int x) __attribute__((vector(..))); */
19965 tree gnu_attrs = NULL_TREE;
19966 tree requires_clause = NULL_TREE;
19967 late_return = (cp_parser_late_return_type_opt
19968 (parser, declarator, requires_clause,
19969 memfn ? cv_quals : -1));
19971 /* Parse the virt-specifier-seq. */
19972 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19974 /* Create the function-declarator. */
19975 declarator = make_call_declarator (declarator,
19976 params,
19977 cv_quals,
19978 virt_specifiers,
19979 ref_qual,
19980 tx_qual,
19981 exception_specification,
19982 late_return,
19983 requires_clause);
19984 declarator->std_attributes = attrs;
19985 declarator->attributes = gnu_attrs;
19986 /* Any subsequent parameter lists are to do with
19987 return type, so are not those of the declared
19988 function. */
19989 parser->default_arg_ok_p = false;
19992 /* Remove the function parms from scope. */
19993 pop_bindings_and_leave_scope ();
19995 if (is_declarator)
19996 /* Repeat the main loop. */
19997 continue;
20000 /* If this is the first, we can try a parenthesized
20001 declarator. */
20002 if (first)
20004 bool saved_in_type_id_in_expr_p;
20006 parser->default_arg_ok_p = saved_default_arg_ok_p;
20007 parser->in_declarator_p = saved_in_declarator_p;
20009 open_paren = token;
20010 /* Consume the `('. */
20011 matching_parens parens;
20012 parens.consume_open (parser);
20013 /* Parse the nested declarator. */
20014 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20015 parser->in_type_id_in_expr_p = true;
20016 declarator
20017 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20018 /*parenthesized_p=*/NULL,
20019 member_p, friend_p);
20020 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20021 first = false;
20022 /* Expect a `)'. */
20023 close_paren = cp_lexer_peek_token (parser->lexer);
20024 if (!parens.require_close (parser))
20025 declarator = cp_error_declarator;
20026 if (declarator == cp_error_declarator)
20027 break;
20029 goto handle_declarator;
20031 /* Otherwise, we must be done. */
20032 else
20033 break;
20035 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20036 && token->type == CPP_OPEN_SQUARE
20037 && !cp_next_tokens_can_be_attribute_p (parser))
20039 /* Parse an array-declarator. */
20040 tree bounds, attrs;
20042 if (ctor_dtor_or_conv_p)
20043 *ctor_dtor_or_conv_p = 0;
20045 open_paren = NULL;
20046 first = false;
20047 parser->default_arg_ok_p = false;
20048 parser->in_declarator_p = true;
20049 /* Consume the `['. */
20050 cp_lexer_consume_token (parser->lexer);
20051 /* Peek at the next token. */
20052 token = cp_lexer_peek_token (parser->lexer);
20053 /* If the next token is `]', then there is no
20054 constant-expression. */
20055 if (token->type != CPP_CLOSE_SQUARE)
20057 bool non_constant_p;
20058 bounds
20059 = cp_parser_constant_expression (parser,
20060 /*allow_non_constant=*/true,
20061 &non_constant_p);
20062 if (!non_constant_p)
20063 /* OK */;
20064 else if (error_operand_p (bounds))
20065 /* Already gave an error. */;
20066 else if (!parser->in_function_body
20067 || current_binding_level->kind == sk_function_parms)
20069 /* Normally, the array bound must be an integral constant
20070 expression. However, as an extension, we allow VLAs
20071 in function scopes as long as they aren't part of a
20072 parameter declaration. */
20073 cp_parser_error (parser,
20074 "array bound is not an integer constant");
20075 bounds = error_mark_node;
20077 else if (processing_template_decl
20078 && !type_dependent_expression_p (bounds))
20080 /* Remember this wasn't a constant-expression. */
20081 bounds = build_nop (TREE_TYPE (bounds), bounds);
20082 TREE_SIDE_EFFECTS (bounds) = 1;
20085 else
20086 bounds = NULL_TREE;
20087 /* Look for the closing `]'. */
20088 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20090 declarator = cp_error_declarator;
20091 break;
20094 attrs = cp_parser_std_attribute_spec_seq (parser);
20095 declarator = make_array_declarator (declarator, bounds);
20096 declarator->std_attributes = attrs;
20098 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20101 tree qualifying_scope;
20102 tree unqualified_name;
20103 tree attrs;
20104 special_function_kind sfk;
20105 bool abstract_ok;
20106 bool pack_expansion_p = false;
20107 cp_token *declarator_id_start_token;
20109 /* Parse a declarator-id */
20110 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20111 if (abstract_ok)
20113 cp_parser_parse_tentatively (parser);
20115 /* If we see an ellipsis, we should be looking at a
20116 parameter pack. */
20117 if (token->type == CPP_ELLIPSIS)
20119 /* Consume the `...' */
20120 cp_lexer_consume_token (parser->lexer);
20122 pack_expansion_p = true;
20126 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20127 unqualified_name
20128 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20129 qualifying_scope = parser->scope;
20130 if (abstract_ok)
20132 bool okay = false;
20134 if (!unqualified_name && pack_expansion_p)
20136 /* Check whether an error occurred. */
20137 okay = !cp_parser_error_occurred (parser);
20139 /* We already consumed the ellipsis to mark a
20140 parameter pack, but we have no way to report it,
20141 so abort the tentative parse. We will be exiting
20142 immediately anyway. */
20143 cp_parser_abort_tentative_parse (parser);
20145 else
20146 okay = cp_parser_parse_definitely (parser);
20148 if (!okay)
20149 unqualified_name = error_mark_node;
20150 else if (unqualified_name
20151 && (qualifying_scope
20152 || (!identifier_p (unqualified_name))))
20154 cp_parser_error (parser, "expected unqualified-id");
20155 unqualified_name = error_mark_node;
20159 if (!unqualified_name)
20160 return NULL;
20161 if (unqualified_name == error_mark_node)
20163 declarator = cp_error_declarator;
20164 pack_expansion_p = false;
20165 declarator->parameter_pack_p = false;
20166 break;
20169 attrs = cp_parser_std_attribute_spec_seq (parser);
20171 if (qualifying_scope && at_namespace_scope_p ()
20172 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20174 /* In the declaration of a member of a template class
20175 outside of the class itself, the SCOPE will sometimes
20176 be a TYPENAME_TYPE. For example, given:
20178 template <typename T>
20179 int S<T>::R::i = 3;
20181 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20182 this context, we must resolve S<T>::R to an ordinary
20183 type, rather than a typename type.
20185 The reason we normally avoid resolving TYPENAME_TYPEs
20186 is that a specialization of `S' might render
20187 `S<T>::R' not a type. However, if `S' is
20188 specialized, then this `i' will not be used, so there
20189 is no harm in resolving the types here. */
20190 tree type;
20192 /* Resolve the TYPENAME_TYPE. */
20193 type = resolve_typename_type (qualifying_scope,
20194 /*only_current_p=*/false);
20195 /* If that failed, the declarator is invalid. */
20196 if (TREE_CODE (type) == TYPENAME_TYPE)
20198 if (typedef_variant_p (type))
20199 error_at (declarator_id_start_token->location,
20200 "cannot define member of dependent typedef "
20201 "%qT", type);
20202 else
20203 error_at (declarator_id_start_token->location,
20204 "%<%T::%E%> is not a type",
20205 TYPE_CONTEXT (qualifying_scope),
20206 TYPE_IDENTIFIER (qualifying_scope));
20208 qualifying_scope = type;
20211 sfk = sfk_none;
20213 if (unqualified_name)
20215 tree class_type;
20217 if (qualifying_scope
20218 && CLASS_TYPE_P (qualifying_scope))
20219 class_type = qualifying_scope;
20220 else
20221 class_type = current_class_type;
20223 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20225 tree name_type = TREE_TYPE (unqualified_name);
20227 if (!class_type || !same_type_p (name_type, class_type))
20229 /* We do not attempt to print the declarator
20230 here because we do not have enough
20231 information about its original syntactic
20232 form. */
20233 cp_parser_error (parser, "invalid declarator");
20234 declarator = cp_error_declarator;
20235 break;
20237 else if (qualifying_scope
20238 && CLASSTYPE_USE_TEMPLATE (name_type))
20240 error_at (declarator_id_start_token->location,
20241 "invalid use of constructor as a template");
20242 inform (declarator_id_start_token->location,
20243 "use %<%T::%D%> instead of %<%T::%D%> to "
20244 "name the constructor in a qualified name",
20245 class_type,
20246 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20247 class_type, name_type);
20248 declarator = cp_error_declarator;
20249 break;
20251 unqualified_name = constructor_name (class_type);
20254 if (class_type)
20256 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20257 sfk = sfk_destructor;
20258 else if (identifier_p (unqualified_name)
20259 && IDENTIFIER_CONV_OP_P (unqualified_name))
20260 sfk = sfk_conversion;
20261 else if (/* There's no way to declare a constructor
20262 for an unnamed type, even if the type
20263 got a name for linkage purposes. */
20264 !TYPE_WAS_UNNAMED (class_type)
20265 /* Handle correctly (c++/19200):
20267 struct S {
20268 struct T{};
20269 friend void S(T);
20272 and also:
20274 namespace N {
20275 void S();
20278 struct S {
20279 friend void N::S();
20280 }; */
20281 && (!friend_p || class_type == qualifying_scope)
20282 && constructor_name_p (unqualified_name,
20283 class_type))
20284 sfk = sfk_constructor;
20285 else if (is_overloaded_fn (unqualified_name)
20286 && DECL_CONSTRUCTOR_P (get_first_fn
20287 (unqualified_name)))
20288 sfk = sfk_constructor;
20290 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20291 *ctor_dtor_or_conv_p = -1;
20294 declarator = make_id_declarator (qualifying_scope,
20295 unqualified_name,
20296 sfk);
20297 declarator->std_attributes = attrs;
20298 declarator->id_loc = token->location;
20299 declarator->parameter_pack_p = pack_expansion_p;
20301 if (pack_expansion_p)
20302 maybe_warn_variadic_templates ();
20305 handle_declarator:;
20306 scope = get_scope_of_declarator (declarator);
20307 if (scope)
20309 /* Any names that appear after the declarator-id for a
20310 member are looked up in the containing scope. */
20311 if (at_function_scope_p ())
20313 /* But declarations with qualified-ids can't appear in a
20314 function. */
20315 cp_parser_error (parser, "qualified-id in declaration");
20316 declarator = cp_error_declarator;
20317 break;
20319 pushed_scope = push_scope (scope);
20321 parser->in_declarator_p = true;
20322 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20323 || (declarator && declarator->kind == cdk_id))
20324 /* Default args are only allowed on function
20325 declarations. */
20326 parser->default_arg_ok_p = saved_default_arg_ok_p;
20327 else
20328 parser->default_arg_ok_p = false;
20330 first = false;
20332 /* We're done. */
20333 else
20334 break;
20337 /* For an abstract declarator, we might wind up with nothing at this
20338 point. That's an error; the declarator is not optional. */
20339 if (!declarator)
20340 cp_parser_error (parser, "expected declarator");
20341 else if (open_paren)
20343 /* Record overly parenthesized declarator so we can give a
20344 diagnostic about confusing decl/expr disambiguation. */
20345 if (declarator->kind == cdk_array)
20347 /* If the open and close parens are on different lines, this
20348 is probably a formatting thing, so ignore. */
20349 expanded_location open = expand_location (open_paren->location);
20350 expanded_location close = expand_location (close_paren->location);
20351 if (open.line != close.line || open.file != close.file)
20352 open_paren = NULL;
20354 if (open_paren)
20355 declarator->parenthesized = open_paren->location;
20358 /* If we entered a scope, we must exit it now. */
20359 if (pushed_scope)
20360 pop_scope (pushed_scope);
20362 parser->default_arg_ok_p = saved_default_arg_ok_p;
20363 parser->in_declarator_p = saved_in_declarator_p;
20365 return declarator;
20368 /* Parse a ptr-operator.
20370 ptr-operator:
20371 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20372 * cv-qualifier-seq [opt]
20374 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20375 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20377 GNU Extension:
20379 ptr-operator:
20380 & cv-qualifier-seq [opt]
20382 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20383 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20384 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20385 filled in with the TYPE containing the member. *CV_QUALS is
20386 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20387 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20388 Note that the tree codes returned by this function have nothing
20389 to do with the types of trees that will be eventually be created
20390 to represent the pointer or reference type being parsed. They are
20391 just constants with suggestive names. */
20392 static enum tree_code
20393 cp_parser_ptr_operator (cp_parser* parser,
20394 tree* type,
20395 cp_cv_quals *cv_quals,
20396 tree *attributes)
20398 enum tree_code code = ERROR_MARK;
20399 cp_token *token;
20400 tree attrs = NULL_TREE;
20402 /* Assume that it's not a pointer-to-member. */
20403 *type = NULL_TREE;
20404 /* And that there are no cv-qualifiers. */
20405 *cv_quals = TYPE_UNQUALIFIED;
20407 /* Peek at the next token. */
20408 token = cp_lexer_peek_token (parser->lexer);
20410 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20411 if (token->type == CPP_MULT)
20412 code = INDIRECT_REF;
20413 else if (token->type == CPP_AND)
20414 code = ADDR_EXPR;
20415 else if ((cxx_dialect != cxx98) &&
20416 token->type == CPP_AND_AND) /* C++0x only */
20417 code = NON_LVALUE_EXPR;
20419 if (code != ERROR_MARK)
20421 /* Consume the `*', `&' or `&&'. */
20422 cp_lexer_consume_token (parser->lexer);
20424 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20425 `&', if we are allowing GNU extensions. (The only qualifier
20426 that can legally appear after `&' is `restrict', but that is
20427 enforced during semantic analysis. */
20428 if (code == INDIRECT_REF
20429 || cp_parser_allow_gnu_extensions_p (parser))
20430 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20432 attrs = cp_parser_std_attribute_spec_seq (parser);
20433 if (attributes != NULL)
20434 *attributes = attrs;
20436 else
20438 /* Try the pointer-to-member case. */
20439 cp_parser_parse_tentatively (parser);
20440 /* Look for the optional `::' operator. */
20441 cp_parser_global_scope_opt (parser,
20442 /*current_scope_valid_p=*/false);
20443 /* Look for the nested-name specifier. */
20444 token = cp_lexer_peek_token (parser->lexer);
20445 cp_parser_nested_name_specifier (parser,
20446 /*typename_keyword_p=*/false,
20447 /*check_dependency_p=*/true,
20448 /*type_p=*/false,
20449 /*is_declaration=*/false);
20450 /* If we found it, and the next token is a `*', then we are
20451 indeed looking at a pointer-to-member operator. */
20452 if (!cp_parser_error_occurred (parser)
20453 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20455 /* Indicate that the `*' operator was used. */
20456 code = INDIRECT_REF;
20458 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20459 error_at (token->location, "%qD is a namespace", parser->scope);
20460 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20461 error_at (token->location, "cannot form pointer to member of "
20462 "non-class %q#T", parser->scope);
20463 else
20465 /* The type of which the member is a member is given by the
20466 current SCOPE. */
20467 *type = parser->scope;
20468 /* The next name will not be qualified. */
20469 parser->scope = NULL_TREE;
20470 parser->qualifying_scope = NULL_TREE;
20471 parser->object_scope = NULL_TREE;
20472 /* Look for optional c++11 attributes. */
20473 attrs = cp_parser_std_attribute_spec_seq (parser);
20474 if (attributes != NULL)
20475 *attributes = attrs;
20476 /* Look for the optional cv-qualifier-seq. */
20477 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20480 /* If that didn't work we don't have a ptr-operator. */
20481 if (!cp_parser_parse_definitely (parser))
20482 cp_parser_error (parser, "expected ptr-operator");
20485 return code;
20488 /* Parse an (optional) cv-qualifier-seq.
20490 cv-qualifier-seq:
20491 cv-qualifier cv-qualifier-seq [opt]
20493 cv-qualifier:
20494 const
20495 volatile
20497 GNU Extension:
20499 cv-qualifier:
20500 __restrict__
20502 Returns a bitmask representing the cv-qualifiers. */
20504 static cp_cv_quals
20505 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20507 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20509 while (true)
20511 cp_token *token;
20512 cp_cv_quals cv_qualifier;
20514 /* Peek at the next token. */
20515 token = cp_lexer_peek_token (parser->lexer);
20516 /* See if it's a cv-qualifier. */
20517 switch (token->keyword)
20519 case RID_CONST:
20520 cv_qualifier = TYPE_QUAL_CONST;
20521 break;
20523 case RID_VOLATILE:
20524 cv_qualifier = TYPE_QUAL_VOLATILE;
20525 break;
20527 case RID_RESTRICT:
20528 cv_qualifier = TYPE_QUAL_RESTRICT;
20529 break;
20531 default:
20532 cv_qualifier = TYPE_UNQUALIFIED;
20533 break;
20536 if (!cv_qualifier)
20537 break;
20539 if (cv_quals & cv_qualifier)
20541 gcc_rich_location richloc (token->location);
20542 richloc.add_fixit_remove ();
20543 error_at (&richloc, "duplicate cv-qualifier");
20544 cp_lexer_purge_token (parser->lexer);
20546 else
20548 cp_lexer_consume_token (parser->lexer);
20549 cv_quals |= cv_qualifier;
20553 return cv_quals;
20556 /* Parse an (optional) ref-qualifier
20558 ref-qualifier:
20562 Returns cp_ref_qualifier representing ref-qualifier. */
20564 static cp_ref_qualifier
20565 cp_parser_ref_qualifier_opt (cp_parser* parser)
20567 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20569 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20570 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20571 return ref_qual;
20573 while (true)
20575 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20576 cp_token *token = cp_lexer_peek_token (parser->lexer);
20578 switch (token->type)
20580 case CPP_AND:
20581 curr_ref_qual = REF_QUAL_LVALUE;
20582 break;
20584 case CPP_AND_AND:
20585 curr_ref_qual = REF_QUAL_RVALUE;
20586 break;
20588 default:
20589 curr_ref_qual = REF_QUAL_NONE;
20590 break;
20593 if (!curr_ref_qual)
20594 break;
20595 else if (ref_qual)
20597 error_at (token->location, "multiple ref-qualifiers");
20598 cp_lexer_purge_token (parser->lexer);
20600 else
20602 ref_qual = curr_ref_qual;
20603 cp_lexer_consume_token (parser->lexer);
20607 return ref_qual;
20610 /* Parse an optional tx-qualifier.
20612 tx-qualifier:
20613 transaction_safe
20614 transaction_safe_dynamic */
20616 static tree
20617 cp_parser_tx_qualifier_opt (cp_parser *parser)
20619 cp_token *token = cp_lexer_peek_token (parser->lexer);
20620 if (token->type == CPP_NAME)
20622 tree name = token->u.value;
20623 const char *p = IDENTIFIER_POINTER (name);
20624 const int len = strlen ("transaction_safe");
20625 if (!strncmp (p, "transaction_safe", len))
20627 p += len;
20628 if (*p == '\0'
20629 || !strcmp (p, "_dynamic"))
20631 cp_lexer_consume_token (parser->lexer);
20632 if (!flag_tm)
20634 error ("%qE requires %<-fgnu-tm%>", name);
20635 return NULL_TREE;
20637 else
20638 return name;
20642 return NULL_TREE;
20645 /* Parse an (optional) virt-specifier-seq.
20647 virt-specifier-seq:
20648 virt-specifier virt-specifier-seq [opt]
20650 virt-specifier:
20651 override
20652 final
20654 Returns a bitmask representing the virt-specifiers. */
20656 static cp_virt_specifiers
20657 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20659 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20661 while (true)
20663 cp_token *token;
20664 cp_virt_specifiers virt_specifier;
20666 /* Peek at the next token. */
20667 token = cp_lexer_peek_token (parser->lexer);
20668 /* See if it's a virt-specifier-qualifier. */
20669 if (token->type != CPP_NAME)
20670 break;
20671 if (id_equal (token->u.value, "override"))
20673 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20674 virt_specifier = VIRT_SPEC_OVERRIDE;
20676 else if (id_equal (token->u.value, "final"))
20678 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20679 virt_specifier = VIRT_SPEC_FINAL;
20681 else if (id_equal (token->u.value, "__final"))
20683 virt_specifier = VIRT_SPEC_FINAL;
20685 else
20686 break;
20688 if (virt_specifiers & virt_specifier)
20690 gcc_rich_location richloc (token->location);
20691 richloc.add_fixit_remove ();
20692 error_at (&richloc, "duplicate virt-specifier");
20693 cp_lexer_purge_token (parser->lexer);
20695 else
20697 cp_lexer_consume_token (parser->lexer);
20698 virt_specifiers |= virt_specifier;
20701 return virt_specifiers;
20704 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20705 is in scope even though it isn't real. */
20707 void
20708 inject_this_parameter (tree ctype, cp_cv_quals quals)
20710 tree this_parm;
20712 if (current_class_ptr)
20714 /* We don't clear this between NSDMIs. Is it already what we want? */
20715 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20716 if (DECL_P (current_class_ptr)
20717 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20718 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20719 && cp_type_quals (type) == quals)
20720 return;
20723 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20724 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20725 current_class_ptr = NULL_TREE;
20726 current_class_ref
20727 = cp_build_fold_indirect_ref (this_parm);
20728 current_class_ptr = this_parm;
20731 /* Return true iff our current scope is a non-static data member
20732 initializer. */
20734 bool
20735 parsing_nsdmi (void)
20737 /* We recognize NSDMI context by the context-less 'this' pointer set up
20738 by the function above. */
20739 if (current_class_ptr
20740 && TREE_CODE (current_class_ptr) == PARM_DECL
20741 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20742 return true;
20743 return false;
20746 /* Parse a late-specified return type, if any. This is not a separate
20747 non-terminal, but part of a function declarator, which looks like
20749 -> trailing-type-specifier-seq abstract-declarator(opt)
20751 Returns the type indicated by the type-id.
20753 In addition to this, parse any queued up #pragma omp declare simd
20754 clauses, and #pragma acc routine clauses.
20756 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20757 function. */
20759 static tree
20760 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20761 tree& requires_clause, cp_cv_quals quals)
20763 cp_token *token;
20764 tree type = NULL_TREE;
20765 bool declare_simd_p = (parser->omp_declare_simd
20766 && declarator
20767 && declarator->kind == cdk_id);
20769 bool oacc_routine_p = (parser->oacc_routine
20770 && declarator
20771 && declarator->kind == cdk_id);
20773 /* Peek at the next token. */
20774 token = cp_lexer_peek_token (parser->lexer);
20775 /* A late-specified return type is indicated by an initial '->'. */
20776 if (token->type != CPP_DEREF
20777 && token->keyword != RID_REQUIRES
20778 && !(token->type == CPP_NAME
20779 && token->u.value == ridpointers[RID_REQUIRES])
20780 && !(declare_simd_p || oacc_routine_p))
20781 return NULL_TREE;
20783 tree save_ccp = current_class_ptr;
20784 tree save_ccr = current_class_ref;
20785 if (quals >= 0)
20787 /* DR 1207: 'this' is in scope in the trailing return type. */
20788 inject_this_parameter (current_class_type, quals);
20791 if (token->type == CPP_DEREF)
20793 /* Consume the ->. */
20794 cp_lexer_consume_token (parser->lexer);
20796 type = cp_parser_trailing_type_id (parser);
20799 /* Function declarations may be followed by a trailing
20800 requires-clause. */
20801 requires_clause = cp_parser_requires_clause_opt (parser);
20803 if (declare_simd_p)
20804 declarator->attributes
20805 = cp_parser_late_parsing_omp_declare_simd (parser,
20806 declarator->attributes);
20807 if (oacc_routine_p)
20808 declarator->attributes
20809 = cp_parser_late_parsing_oacc_routine (parser,
20810 declarator->attributes);
20812 if (quals >= 0)
20814 current_class_ptr = save_ccp;
20815 current_class_ref = save_ccr;
20818 return type;
20821 /* Parse a declarator-id.
20823 declarator-id:
20824 id-expression
20825 :: [opt] nested-name-specifier [opt] type-name
20827 In the `id-expression' case, the value returned is as for
20828 cp_parser_id_expression if the id-expression was an unqualified-id.
20829 If the id-expression was a qualified-id, then a SCOPE_REF is
20830 returned. The first operand is the scope (either a NAMESPACE_DECL
20831 or TREE_TYPE), but the second is still just a representation of an
20832 unqualified-id. */
20834 static tree
20835 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20837 tree id;
20838 /* The expression must be an id-expression. Assume that qualified
20839 names are the names of types so that:
20841 template <class T>
20842 int S<T>::R::i = 3;
20844 will work; we must treat `S<T>::R' as the name of a type.
20845 Similarly, assume that qualified names are templates, where
20846 required, so that:
20848 template <class T>
20849 int S<T>::R<T>::i = 3;
20851 will work, too. */
20852 id = cp_parser_id_expression (parser,
20853 /*template_keyword_p=*/false,
20854 /*check_dependency_p=*/false,
20855 /*template_p=*/NULL,
20856 /*declarator_p=*/true,
20857 optional_p);
20858 if (id && BASELINK_P (id))
20859 id = BASELINK_FUNCTIONS (id);
20860 return id;
20863 /* Parse a type-id.
20865 type-id:
20866 type-specifier-seq abstract-declarator [opt]
20868 Returns the TYPE specified. */
20870 static tree
20871 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20872 bool is_trailing_return)
20874 cp_decl_specifier_seq type_specifier_seq;
20875 cp_declarator *abstract_declarator;
20877 /* Parse the type-specifier-seq. */
20878 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20879 is_trailing_return,
20880 &type_specifier_seq);
20881 if (is_template_arg && type_specifier_seq.type
20882 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20883 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20884 /* A bare template name as a template argument is a template template
20885 argument, not a placeholder, so fail parsing it as a type argument. */
20887 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20888 cp_parser_simulate_error (parser);
20889 return error_mark_node;
20891 if (type_specifier_seq.type == error_mark_node)
20892 return error_mark_node;
20894 /* There might or might not be an abstract declarator. */
20895 cp_parser_parse_tentatively (parser);
20896 /* Look for the declarator. */
20897 abstract_declarator
20898 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20899 /*parenthesized_p=*/NULL,
20900 /*member_p=*/false,
20901 /*friend_p=*/false);
20902 /* Check to see if there really was a declarator. */
20903 if (!cp_parser_parse_definitely (parser))
20904 abstract_declarator = NULL;
20906 if (type_specifier_seq.type
20907 /* The concepts TS allows 'auto' as a type-id. */
20908 && (!flag_concepts || parser->in_type_id_in_expr_p)
20909 /* None of the valid uses of 'auto' in C++14 involve the type-id
20910 nonterminal, but it is valid in a trailing-return-type. */
20911 && !(cxx_dialect >= cxx14 && is_trailing_return))
20912 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20914 /* A type-id with type 'auto' is only ok if the abstract declarator
20915 is a function declarator with a late-specified return type.
20917 A type-id with 'auto' is also valid in a trailing-return-type
20918 in a compound-requirement. */
20919 if (abstract_declarator
20920 && abstract_declarator->kind == cdk_function
20921 && abstract_declarator->u.function.late_return_type)
20922 /* OK */;
20923 else if (parser->in_result_type_constraint_p)
20924 /* OK */;
20925 else
20927 location_t loc = type_specifier_seq.locations[ds_type_spec];
20928 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20930 error_at (loc, "missing template arguments after %qT",
20931 auto_node);
20932 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20933 tmpl);
20935 else
20936 error_at (loc, "invalid use of %qT", auto_node);
20937 return error_mark_node;
20941 return groktypename (&type_specifier_seq, abstract_declarator,
20942 is_template_arg);
20945 static tree
20946 cp_parser_type_id (cp_parser *parser)
20948 return cp_parser_type_id_1 (parser, false, false);
20951 static tree
20952 cp_parser_template_type_arg (cp_parser *parser)
20954 tree r;
20955 const char *saved_message = parser->type_definition_forbidden_message;
20956 parser->type_definition_forbidden_message
20957 = G_("types may not be defined in template arguments");
20958 r = cp_parser_type_id_1 (parser, true, false);
20959 parser->type_definition_forbidden_message = saved_message;
20960 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20962 error ("invalid use of %<auto%> in template argument");
20963 r = error_mark_node;
20965 return r;
20968 static tree
20969 cp_parser_trailing_type_id (cp_parser *parser)
20971 return cp_parser_type_id_1 (parser, false, true);
20974 /* Parse a type-specifier-seq.
20976 type-specifier-seq:
20977 type-specifier type-specifier-seq [opt]
20979 GNU extension:
20981 type-specifier-seq:
20982 attributes type-specifier-seq [opt]
20984 If IS_DECLARATION is true, we are at the start of a "condition" or
20985 exception-declaration, so we might be followed by a declarator-id.
20987 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20988 i.e. we've just seen "->".
20990 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20992 static void
20993 cp_parser_type_specifier_seq (cp_parser* parser,
20994 bool is_declaration,
20995 bool is_trailing_return,
20996 cp_decl_specifier_seq *type_specifier_seq)
20998 bool seen_type_specifier = false;
20999 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21000 cp_token *start_token = NULL;
21002 /* Clear the TYPE_SPECIFIER_SEQ. */
21003 clear_decl_specs (type_specifier_seq);
21005 /* In the context of a trailing return type, enum E { } is an
21006 elaborated-type-specifier followed by a function-body, not an
21007 enum-specifier. */
21008 if (is_trailing_return)
21009 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21011 /* Parse the type-specifiers and attributes. */
21012 while (true)
21014 tree type_specifier;
21015 bool is_cv_qualifier;
21017 /* Check for attributes first. */
21018 if (cp_next_tokens_can_be_attribute_p (parser))
21020 type_specifier_seq->attributes
21021 = attr_chainon (type_specifier_seq->attributes,
21022 cp_parser_attributes_opt (parser));
21023 continue;
21026 /* record the token of the beginning of the type specifier seq,
21027 for error reporting purposes*/
21028 if (!start_token)
21029 start_token = cp_lexer_peek_token (parser->lexer);
21031 /* Look for the type-specifier. */
21032 type_specifier = cp_parser_type_specifier (parser,
21033 flags,
21034 type_specifier_seq,
21035 /*is_declaration=*/false,
21036 NULL,
21037 &is_cv_qualifier);
21038 if (!type_specifier)
21040 /* If the first type-specifier could not be found, this is not a
21041 type-specifier-seq at all. */
21042 if (!seen_type_specifier)
21044 /* Set in_declarator_p to avoid skipping to the semicolon. */
21045 int in_decl = parser->in_declarator_p;
21046 parser->in_declarator_p = true;
21048 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21049 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21050 cp_parser_error (parser, "expected type-specifier");
21052 parser->in_declarator_p = in_decl;
21054 type_specifier_seq->type = error_mark_node;
21055 return;
21057 /* If subsequent type-specifiers could not be found, the
21058 type-specifier-seq is complete. */
21059 break;
21062 seen_type_specifier = true;
21063 /* The standard says that a condition can be:
21065 type-specifier-seq declarator = assignment-expression
21067 However, given:
21069 struct S {};
21070 if (int S = ...)
21072 we should treat the "S" as a declarator, not as a
21073 type-specifier. The standard doesn't say that explicitly for
21074 type-specifier-seq, but it does say that for
21075 decl-specifier-seq in an ordinary declaration. Perhaps it
21076 would be clearer just to allow a decl-specifier-seq here, and
21077 then add a semantic restriction that if any decl-specifiers
21078 that are not type-specifiers appear, the program is invalid. */
21079 if (is_declaration && !is_cv_qualifier)
21080 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21084 /* Return whether the function currently being declared has an associated
21085 template parameter list. */
21087 static bool
21088 function_being_declared_is_template_p (cp_parser* parser)
21090 if (!current_template_parms || processing_template_parmlist)
21091 return false;
21093 if (parser->implicit_template_scope)
21094 return true;
21096 if (at_class_scope_p ()
21097 && TYPE_BEING_DEFINED (current_class_type))
21098 return parser->num_template_parameter_lists != 0;
21100 return ((int) parser->num_template_parameter_lists > template_class_depth
21101 (current_class_type));
21104 /* Parse a parameter-declaration-clause.
21106 parameter-declaration-clause:
21107 parameter-declaration-list [opt] ... [opt]
21108 parameter-declaration-list , ...
21110 Returns a representation for the parameter declarations. A return
21111 value of NULL indicates a parameter-declaration-clause consisting
21112 only of an ellipsis. */
21114 static tree
21115 cp_parser_parameter_declaration_clause (cp_parser* parser)
21117 tree parameters;
21118 cp_token *token;
21119 bool ellipsis_p;
21120 bool is_error;
21122 struct cleanup {
21123 cp_parser* parser;
21124 int auto_is_implicit_function_template_parm_p;
21125 ~cleanup() {
21126 parser->auto_is_implicit_function_template_parm_p
21127 = auto_is_implicit_function_template_parm_p;
21129 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21131 (void) cleanup;
21133 if (!processing_specialization
21134 && !processing_template_parmlist
21135 && !processing_explicit_instantiation)
21136 if (!current_function_decl
21137 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21138 parser->auto_is_implicit_function_template_parm_p = true;
21140 /* Peek at the next token. */
21141 token = cp_lexer_peek_token (parser->lexer);
21142 /* Check for trivial parameter-declaration-clauses. */
21143 if (token->type == CPP_ELLIPSIS)
21145 /* Consume the `...' token. */
21146 cp_lexer_consume_token (parser->lexer);
21147 return NULL_TREE;
21149 else if (token->type == CPP_CLOSE_PAREN)
21150 /* There are no parameters. */
21152 #ifndef NO_IMPLICIT_EXTERN_C
21153 if (in_system_header_at (input_location)
21154 && current_class_type == NULL
21155 && current_lang_name == lang_name_c)
21156 return NULL_TREE;
21157 else
21158 #endif
21159 return void_list_node;
21161 /* Check for `(void)', too, which is a special case. */
21162 else if (token->keyword == RID_VOID
21163 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21164 == CPP_CLOSE_PAREN))
21166 /* Consume the `void' token. */
21167 cp_lexer_consume_token (parser->lexer);
21168 /* There are no parameters. */
21169 return void_list_node;
21172 /* Parse the parameter-declaration-list. */
21173 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21174 /* If a parse error occurred while parsing the
21175 parameter-declaration-list, then the entire
21176 parameter-declaration-clause is erroneous. */
21177 if (is_error)
21178 return NULL;
21180 /* Peek at the next token. */
21181 token = cp_lexer_peek_token (parser->lexer);
21182 /* If it's a `,', the clause should terminate with an ellipsis. */
21183 if (token->type == CPP_COMMA)
21185 /* Consume the `,'. */
21186 cp_lexer_consume_token (parser->lexer);
21187 /* Expect an ellipsis. */
21188 ellipsis_p
21189 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21191 /* It might also be `...' if the optional trailing `,' was
21192 omitted. */
21193 else if (token->type == CPP_ELLIPSIS)
21195 /* Consume the `...' token. */
21196 cp_lexer_consume_token (parser->lexer);
21197 /* And remember that we saw it. */
21198 ellipsis_p = true;
21200 else
21201 ellipsis_p = false;
21203 /* Finish the parameter list. */
21204 if (!ellipsis_p)
21205 parameters = chainon (parameters, void_list_node);
21207 return parameters;
21210 /* Parse a parameter-declaration-list.
21212 parameter-declaration-list:
21213 parameter-declaration
21214 parameter-declaration-list , parameter-declaration
21216 Returns a representation of the parameter-declaration-list, as for
21217 cp_parser_parameter_declaration_clause. However, the
21218 `void_list_node' is never appended to the list. Upon return,
21219 *IS_ERROR will be true iff an error occurred. */
21221 static tree
21222 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21224 tree parameters = NULL_TREE;
21225 tree *tail = &parameters;
21226 bool saved_in_unbraced_linkage_specification_p;
21227 int index = 0;
21229 /* Assume all will go well. */
21230 *is_error = false;
21231 /* The special considerations that apply to a function within an
21232 unbraced linkage specifications do not apply to the parameters
21233 to the function. */
21234 saved_in_unbraced_linkage_specification_p
21235 = parser->in_unbraced_linkage_specification_p;
21236 parser->in_unbraced_linkage_specification_p = false;
21238 /* Look for more parameters. */
21239 while (true)
21241 cp_parameter_declarator *parameter;
21242 tree decl = error_mark_node;
21243 bool parenthesized_p = false;
21244 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21245 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21246 (current_template_parms)) : 0);
21248 /* Parse the parameter. */
21249 parameter
21250 = cp_parser_parameter_declaration (parser,
21251 /*template_parm_p=*/false,
21252 &parenthesized_p);
21254 /* We don't know yet if the enclosing context is deprecated, so wait
21255 and warn in grokparms if appropriate. */
21256 deprecated_state = DEPRECATED_SUPPRESS;
21258 if (parameter)
21260 /* If a function parameter pack was specified and an implicit template
21261 parameter was introduced during cp_parser_parameter_declaration,
21262 change any implicit parameters introduced into packs. */
21263 if (parser->implicit_template_parms
21264 && parameter->declarator
21265 && parameter->declarator->parameter_pack_p)
21267 int latest_template_parm_idx = TREE_VEC_LENGTH
21268 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21270 if (latest_template_parm_idx != template_parm_idx)
21271 parameter->decl_specifiers.type = convert_generic_types_to_packs
21272 (parameter->decl_specifiers.type,
21273 template_parm_idx, latest_template_parm_idx);
21276 decl = grokdeclarator (parameter->declarator,
21277 &parameter->decl_specifiers,
21278 PARM,
21279 parameter->default_argument != NULL_TREE,
21280 &parameter->decl_specifiers.attributes);
21281 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21282 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21285 deprecated_state = DEPRECATED_NORMAL;
21287 /* If a parse error occurred parsing the parameter declaration,
21288 then the entire parameter-declaration-list is erroneous. */
21289 if (decl == error_mark_node)
21291 *is_error = true;
21292 parameters = error_mark_node;
21293 break;
21296 if (parameter->decl_specifiers.attributes)
21297 cplus_decl_attributes (&decl,
21298 parameter->decl_specifiers.attributes,
21300 if (DECL_NAME (decl))
21301 decl = pushdecl (decl);
21303 if (decl != error_mark_node)
21305 retrofit_lang_decl (decl);
21306 DECL_PARM_INDEX (decl) = ++index;
21307 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21310 /* Add the new parameter to the list. */
21311 *tail = build_tree_list (parameter->default_argument, decl);
21312 tail = &TREE_CHAIN (*tail);
21314 /* Peek at the next token. */
21315 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21316 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21317 /* These are for Objective-C++ */
21318 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21319 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21320 /* The parameter-declaration-list is complete. */
21321 break;
21322 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21324 cp_token *token;
21326 /* Peek at the next token. */
21327 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21328 /* If it's an ellipsis, then the list is complete. */
21329 if (token->type == CPP_ELLIPSIS)
21330 break;
21331 /* Otherwise, there must be more parameters. Consume the
21332 `,'. */
21333 cp_lexer_consume_token (parser->lexer);
21334 /* When parsing something like:
21336 int i(float f, double d)
21338 we can tell after seeing the declaration for "f" that we
21339 are not looking at an initialization of a variable "i",
21340 but rather at the declaration of a function "i".
21342 Due to the fact that the parsing of template arguments
21343 (as specified to a template-id) requires backtracking we
21344 cannot use this technique when inside a template argument
21345 list. */
21346 if (!parser->in_template_argument_list_p
21347 && !parser->in_type_id_in_expr_p
21348 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21349 /* However, a parameter-declaration of the form
21350 "float(f)" (which is a valid declaration of a
21351 parameter "f") can also be interpreted as an
21352 expression (the conversion of "f" to "float"). */
21353 && !parenthesized_p)
21354 cp_parser_commit_to_tentative_parse (parser);
21356 else
21358 cp_parser_error (parser, "expected %<,%> or %<...%>");
21359 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21360 cp_parser_skip_to_closing_parenthesis (parser,
21361 /*recovering=*/true,
21362 /*or_comma=*/false,
21363 /*consume_paren=*/false);
21364 break;
21368 parser->in_unbraced_linkage_specification_p
21369 = saved_in_unbraced_linkage_specification_p;
21371 /* Reset implicit_template_scope if we are about to leave the function
21372 parameter list that introduced it. Note that for out-of-line member
21373 definitions, there will be one or more class scopes before we get to
21374 the template parameter scope. */
21376 if (cp_binding_level *its = parser->implicit_template_scope)
21377 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21379 while (maybe_its->kind == sk_class)
21380 maybe_its = maybe_its->level_chain;
21381 if (maybe_its == its)
21383 parser->implicit_template_parms = 0;
21384 parser->implicit_template_scope = 0;
21388 return parameters;
21391 /* Parse a parameter declaration.
21393 parameter-declaration:
21394 decl-specifier-seq ... [opt] declarator
21395 decl-specifier-seq declarator = assignment-expression
21396 decl-specifier-seq ... [opt] abstract-declarator [opt]
21397 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21399 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21400 declares a template parameter. (In that case, a non-nested `>'
21401 token encountered during the parsing of the assignment-expression
21402 is not interpreted as a greater-than operator.)
21404 Returns a representation of the parameter, or NULL if an error
21405 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21406 true iff the declarator is of the form "(p)". */
21408 static cp_parameter_declarator *
21409 cp_parser_parameter_declaration (cp_parser *parser,
21410 bool template_parm_p,
21411 bool *parenthesized_p)
21413 int declares_class_or_enum;
21414 cp_decl_specifier_seq decl_specifiers;
21415 cp_declarator *declarator;
21416 tree default_argument;
21417 cp_token *token = NULL, *declarator_token_start = NULL;
21418 const char *saved_message;
21419 bool template_parameter_pack_p = false;
21421 /* In a template parameter, `>' is not an operator.
21423 [temp.param]
21425 When parsing a default template-argument for a non-type
21426 template-parameter, the first non-nested `>' is taken as the end
21427 of the template parameter-list rather than a greater-than
21428 operator. */
21430 /* Type definitions may not appear in parameter types. */
21431 saved_message = parser->type_definition_forbidden_message;
21432 parser->type_definition_forbidden_message
21433 = G_("types may not be defined in parameter types");
21435 /* Parse the declaration-specifiers. */
21436 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21437 cp_parser_decl_specifier_seq (parser,
21438 CP_PARSER_FLAGS_NONE,
21439 &decl_specifiers,
21440 &declares_class_or_enum);
21442 /* Complain about missing 'typename' or other invalid type names. */
21443 if (!decl_specifiers.any_type_specifiers_p
21444 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21445 decl_specifiers.type = error_mark_node;
21447 /* If an error occurred, there's no reason to attempt to parse the
21448 rest of the declaration. */
21449 if (cp_parser_error_occurred (parser))
21451 parser->type_definition_forbidden_message = saved_message;
21452 return NULL;
21455 /* Peek at the next token. */
21456 token = cp_lexer_peek_token (parser->lexer);
21458 /* If the next token is a `)', `,', `=', `>', or `...', then there
21459 is no declarator. However, when variadic templates are enabled,
21460 there may be a declarator following `...'. */
21461 if (token->type == CPP_CLOSE_PAREN
21462 || token->type == CPP_COMMA
21463 || token->type == CPP_EQ
21464 || token->type == CPP_GREATER)
21466 declarator = NULL;
21467 if (parenthesized_p)
21468 *parenthesized_p = false;
21470 /* Otherwise, there should be a declarator. */
21471 else
21473 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21474 parser->default_arg_ok_p = false;
21476 /* After seeing a decl-specifier-seq, if the next token is not a
21477 "(", there is no possibility that the code is a valid
21478 expression. Therefore, if parsing tentatively, we commit at
21479 this point. */
21480 if (!parser->in_template_argument_list_p
21481 /* In an expression context, having seen:
21483 (int((char ...
21485 we cannot be sure whether we are looking at a
21486 function-type (taking a "char" as a parameter) or a cast
21487 of some object of type "char" to "int". */
21488 && !parser->in_type_id_in_expr_p
21489 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21490 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21491 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21492 cp_parser_commit_to_tentative_parse (parser);
21493 /* Parse the declarator. */
21494 declarator_token_start = token;
21495 declarator = cp_parser_declarator (parser,
21496 CP_PARSER_DECLARATOR_EITHER,
21497 /*ctor_dtor_or_conv_p=*/NULL,
21498 parenthesized_p,
21499 /*member_p=*/false,
21500 /*friend_p=*/false);
21501 parser->default_arg_ok_p = saved_default_arg_ok_p;
21502 /* After the declarator, allow more attributes. */
21503 decl_specifiers.attributes
21504 = attr_chainon (decl_specifiers.attributes,
21505 cp_parser_attributes_opt (parser));
21507 /* If the declarator is a template parameter pack, remember that and
21508 clear the flag in the declarator itself so we don't get errors
21509 from grokdeclarator. */
21510 if (template_parm_p && declarator && declarator->parameter_pack_p)
21512 declarator->parameter_pack_p = false;
21513 template_parameter_pack_p = true;
21517 /* If the next token is an ellipsis, and we have not seen a declarator
21518 name, and if either the type of the declarator contains parameter
21519 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21520 for, eg, abbreviated integral type names), then we actually have a
21521 parameter pack expansion expression. Otherwise, leave the ellipsis
21522 for a C-style variadic function. */
21523 token = cp_lexer_peek_token (parser->lexer);
21524 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21526 tree type = decl_specifiers.type;
21528 if (type && DECL_P (type))
21529 type = TREE_TYPE (type);
21531 if (((type
21532 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21533 && (template_parm_p || uses_parameter_packs (type)))
21534 || (!type && template_parm_p))
21535 && declarator_can_be_parameter_pack (declarator))
21537 /* Consume the `...'. */
21538 cp_lexer_consume_token (parser->lexer);
21539 maybe_warn_variadic_templates ();
21541 /* Build a pack expansion type */
21542 if (template_parm_p)
21543 template_parameter_pack_p = true;
21544 else if (declarator)
21545 declarator->parameter_pack_p = true;
21546 else
21547 decl_specifiers.type = make_pack_expansion (type);
21551 /* The restriction on defining new types applies only to the type
21552 of the parameter, not to the default argument. */
21553 parser->type_definition_forbidden_message = saved_message;
21555 /* If the next token is `=', then process a default argument. */
21556 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21558 tree type = decl_specifiers.type;
21559 token = cp_lexer_peek_token (parser->lexer);
21560 /* If we are defining a class, then the tokens that make up the
21561 default argument must be saved and processed later. */
21562 if (!template_parm_p && at_class_scope_p ()
21563 && TYPE_BEING_DEFINED (current_class_type)
21564 && !LAMBDA_TYPE_P (current_class_type))
21565 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21567 // A constrained-type-specifier may declare a type template-parameter.
21568 else if (declares_constrained_type_template_parameter (type))
21569 default_argument
21570 = cp_parser_default_type_template_argument (parser);
21572 // A constrained-type-specifier may declare a template-template-parameter.
21573 else if (declares_constrained_template_template_parameter (type))
21574 default_argument
21575 = cp_parser_default_template_template_argument (parser);
21577 /* Outside of a class definition, we can just parse the
21578 assignment-expression. */
21579 else
21580 default_argument
21581 = cp_parser_default_argument (parser, template_parm_p);
21583 if (!parser->default_arg_ok_p)
21585 permerror (token->location,
21586 "default arguments are only "
21587 "permitted for function parameters");
21589 else if ((declarator && declarator->parameter_pack_p)
21590 || template_parameter_pack_p
21591 || (decl_specifiers.type
21592 && PACK_EXPANSION_P (decl_specifiers.type)))
21594 /* Find the name of the parameter pack. */
21595 cp_declarator *id_declarator = declarator;
21596 while (id_declarator && id_declarator->kind != cdk_id)
21597 id_declarator = id_declarator->declarator;
21599 if (id_declarator && id_declarator->kind == cdk_id)
21600 error_at (declarator_token_start->location,
21601 template_parm_p
21602 ? G_("template parameter pack %qD "
21603 "cannot have a default argument")
21604 : G_("parameter pack %qD cannot have "
21605 "a default argument"),
21606 id_declarator->u.id.unqualified_name);
21607 else
21608 error_at (declarator_token_start->location,
21609 template_parm_p
21610 ? G_("template parameter pack cannot have "
21611 "a default argument")
21612 : G_("parameter pack cannot have a "
21613 "default argument"));
21615 default_argument = NULL_TREE;
21618 else
21619 default_argument = NULL_TREE;
21621 /* Generate a location for the parameter, ranging from the start of the
21622 initial token to the end of the final token (using input_location for
21623 the latter, set up by cp_lexer_set_source_position_from_token when
21624 consuming tokens).
21626 If we have a identifier, then use it for the caret location, e.g.
21628 extern int callee (int one, int (*two)(int, int), float three);
21629 ~~~~~~^~~~~~~~~~~~~~
21631 otherwise, reuse the start location for the caret location e.g.:
21633 extern int callee (int one, int (*)(int, int), float three);
21634 ^~~~~~~~~~~~~~~~~
21637 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21638 ? declarator->id_loc
21639 : decl_spec_token_start->location);
21640 location_t param_loc = make_location (caret_loc,
21641 decl_spec_token_start->location,
21642 input_location);
21644 return make_parameter_declarator (&decl_specifiers,
21645 declarator,
21646 default_argument,
21647 param_loc,
21648 template_parameter_pack_p);
21651 /* Parse a default argument and return it.
21653 TEMPLATE_PARM_P is true if this is a default argument for a
21654 non-type template parameter. */
21655 static tree
21656 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21658 tree default_argument = NULL_TREE;
21659 bool saved_greater_than_is_operator_p;
21660 bool saved_local_variables_forbidden_p;
21661 bool non_constant_p, is_direct_init;
21663 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21664 set correctly. */
21665 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21666 parser->greater_than_is_operator_p = !template_parm_p;
21667 /* Local variable names (and the `this' keyword) may not
21668 appear in a default argument. */
21669 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21670 parser->local_variables_forbidden_p = true;
21671 /* Parse the assignment-expression. */
21672 if (template_parm_p)
21673 push_deferring_access_checks (dk_no_deferred);
21674 tree saved_class_ptr = NULL_TREE;
21675 tree saved_class_ref = NULL_TREE;
21676 /* The "this" pointer is not valid in a default argument. */
21677 if (cfun)
21679 saved_class_ptr = current_class_ptr;
21680 cp_function_chain->x_current_class_ptr = NULL_TREE;
21681 saved_class_ref = current_class_ref;
21682 cp_function_chain->x_current_class_ref = NULL_TREE;
21684 default_argument
21685 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21686 /* Restore the "this" pointer. */
21687 if (cfun)
21689 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21690 cp_function_chain->x_current_class_ref = saved_class_ref;
21692 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21693 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21694 if (template_parm_p)
21695 pop_deferring_access_checks ();
21696 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21697 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21699 return default_argument;
21702 /* Parse a function-body.
21704 function-body:
21705 compound_statement */
21707 static void
21708 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21710 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21711 ? BCS_TRY_BLOCK : BCS_NORMAL),
21712 true);
21715 /* Parse a ctor-initializer-opt followed by a function-body. Return
21716 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21717 is true we are parsing a function-try-block. */
21719 static void
21720 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21721 bool in_function_try_block)
21723 tree body, list;
21724 const bool check_body_p =
21725 DECL_CONSTRUCTOR_P (current_function_decl)
21726 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21727 tree last = NULL;
21729 /* Begin the function body. */
21730 body = begin_function_body ();
21731 /* Parse the optional ctor-initializer. */
21732 cp_parser_ctor_initializer_opt (parser);
21734 /* If we're parsing a constexpr constructor definition, we need
21735 to check that the constructor body is indeed empty. However,
21736 before we get to cp_parser_function_body lot of junk has been
21737 generated, so we can't just check that we have an empty block.
21738 Rather we take a snapshot of the outermost block, and check whether
21739 cp_parser_function_body changed its state. */
21740 if (check_body_p)
21742 list = cur_stmt_list;
21743 if (STATEMENT_LIST_TAIL (list))
21744 last = STATEMENT_LIST_TAIL (list)->stmt;
21746 /* Parse the function-body. */
21747 cp_parser_function_body (parser, in_function_try_block);
21748 if (check_body_p)
21749 check_constexpr_ctor_body (last, list, /*complain=*/true);
21750 /* Finish the function body. */
21751 finish_function_body (body);
21754 /* Parse an initializer.
21756 initializer:
21757 = initializer-clause
21758 ( expression-list )
21760 Returns an expression representing the initializer. If no
21761 initializer is present, NULL_TREE is returned.
21763 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21764 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21765 set to TRUE if there is no initializer present. If there is an
21766 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21767 is set to true; otherwise it is set to false. */
21769 static tree
21770 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21771 bool* non_constant_p)
21773 cp_token *token;
21774 tree init;
21776 /* Peek at the next token. */
21777 token = cp_lexer_peek_token (parser->lexer);
21779 /* Let our caller know whether or not this initializer was
21780 parenthesized. */
21781 *is_direct_init = (token->type != CPP_EQ);
21782 /* Assume that the initializer is constant. */
21783 *non_constant_p = false;
21785 if (token->type == CPP_EQ)
21787 /* Consume the `='. */
21788 cp_lexer_consume_token (parser->lexer);
21789 /* Parse the initializer-clause. */
21790 init = cp_parser_initializer_clause (parser, non_constant_p);
21792 else if (token->type == CPP_OPEN_PAREN)
21794 vec<tree, va_gc> *vec;
21795 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21796 /*cast_p=*/false,
21797 /*allow_expansion_p=*/true,
21798 non_constant_p);
21799 if (vec == NULL)
21800 return error_mark_node;
21801 init = build_tree_list_vec (vec);
21802 release_tree_vector (vec);
21804 else if (token->type == CPP_OPEN_BRACE)
21806 cp_lexer_set_source_position (parser->lexer);
21807 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21808 init = cp_parser_braced_list (parser, non_constant_p);
21809 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21811 else
21813 /* Anything else is an error. */
21814 cp_parser_error (parser, "expected initializer");
21815 init = error_mark_node;
21818 if (check_for_bare_parameter_packs (init))
21819 init = error_mark_node;
21821 return init;
21824 /* Parse an initializer-clause.
21826 initializer-clause:
21827 assignment-expression
21828 braced-init-list
21830 Returns an expression representing the initializer.
21832 If the `assignment-expression' production is used the value
21833 returned is simply a representation for the expression.
21835 Otherwise, calls cp_parser_braced_list. */
21837 static cp_expr
21838 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21840 cp_expr initializer;
21842 /* Assume the expression is constant. */
21843 *non_constant_p = false;
21845 /* If it is not a `{', then we are looking at an
21846 assignment-expression. */
21847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21849 initializer
21850 = cp_parser_constant_expression (parser,
21851 /*allow_non_constant_p=*/true,
21852 non_constant_p);
21854 else
21855 initializer = cp_parser_braced_list (parser, non_constant_p);
21857 return initializer;
21860 /* Parse a brace-enclosed initializer list.
21862 braced-init-list:
21863 { initializer-list , [opt] }
21864 { designated-initializer-list , [opt] }
21867 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21868 the elements of the initializer-list (or NULL, if the last
21869 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21870 NULL_TREE. There is no way to detect whether or not the optional
21871 trailing `,' was provided. NON_CONSTANT_P is as for
21872 cp_parser_initializer. */
21874 static cp_expr
21875 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21877 tree initializer;
21878 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21880 /* Consume the `{' token. */
21881 matching_braces braces;
21882 braces.consume_open (parser);
21883 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21884 initializer = make_node (CONSTRUCTOR);
21885 /* If it's not a `}', then there is a non-trivial initializer. */
21886 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21888 /* Parse the initializer list. */
21889 CONSTRUCTOR_ELTS (initializer)
21890 = cp_parser_initializer_list (parser, non_constant_p);
21891 /* A trailing `,' token is allowed. */
21892 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21893 cp_lexer_consume_token (parser->lexer);
21895 else
21896 *non_constant_p = false;
21897 /* Now, there should be a trailing `}'. */
21898 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21899 braces.require_close (parser);
21900 TREE_TYPE (initializer) = init_list_type_node;
21902 cp_expr result (initializer);
21903 /* Build a location of the form:
21904 { ... }
21905 ^~~~~~~
21906 with caret==start at the open brace, finish at the close brace. */
21907 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21908 result.set_location (combined_loc);
21909 return result;
21912 /* Consume tokens up to, and including, the next non-nested closing `]'.
21913 Returns true iff we found a closing `]'. */
21915 static bool
21916 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21918 unsigned square_depth = 0;
21920 while (true)
21922 cp_token * token = cp_lexer_peek_token (parser->lexer);
21924 switch (token->type)
21926 case CPP_EOF:
21927 case CPP_PRAGMA_EOL:
21928 /* If we've run out of tokens, then there is no closing `]'. */
21929 return false;
21931 case CPP_OPEN_SQUARE:
21932 ++square_depth;
21933 break;
21935 case CPP_CLOSE_SQUARE:
21936 if (!square_depth--)
21938 cp_lexer_consume_token (parser->lexer);
21939 return true;
21941 break;
21943 default:
21944 break;
21947 /* Consume the token. */
21948 cp_lexer_consume_token (parser->lexer);
21952 /* Return true if we are looking at an array-designator, false otherwise. */
21954 static bool
21955 cp_parser_array_designator_p (cp_parser *parser)
21957 /* Consume the `['. */
21958 cp_lexer_consume_token (parser->lexer);
21960 cp_lexer_save_tokens (parser->lexer);
21962 /* Skip tokens until the next token is a closing square bracket.
21963 If we find the closing `]', and the next token is a `=', then
21964 we are looking at an array designator. */
21965 bool array_designator_p
21966 = (cp_parser_skip_to_closing_square_bracket (parser)
21967 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21969 /* Roll back the tokens we skipped. */
21970 cp_lexer_rollback_tokens (parser->lexer);
21972 return array_designator_p;
21975 /* Parse an initializer-list.
21977 initializer-list:
21978 initializer-clause ... [opt]
21979 initializer-list , initializer-clause ... [opt]
21981 C++2A Extension:
21983 designated-initializer-list:
21984 designated-initializer-clause
21985 designated-initializer-list , designated-initializer-clause
21987 designated-initializer-clause:
21988 designator brace-or-equal-initializer
21990 designator:
21991 . identifier
21993 GNU Extension:
21995 initializer-list:
21996 designation initializer-clause ...[opt]
21997 initializer-list , designation initializer-clause ...[opt]
21999 designation:
22000 . identifier =
22001 identifier :
22002 [ constant-expression ] =
22004 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22005 for the initializer. If the INDEX of the elt is non-NULL, it is the
22006 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22007 as for cp_parser_initializer. */
22009 static vec<constructor_elt, va_gc> *
22010 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22012 vec<constructor_elt, va_gc> *v = NULL;
22013 bool first_p = true;
22014 tree first_designator = NULL_TREE;
22016 /* Assume all of the expressions are constant. */
22017 *non_constant_p = false;
22019 /* Parse the rest of the list. */
22020 while (true)
22022 cp_token *token;
22023 tree designator;
22024 tree initializer;
22025 bool clause_non_constant_p;
22026 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22028 /* Handle the C++2A syntax, '. id ='. */
22029 if ((cxx_dialect >= cxx2a
22030 || cp_parser_allow_gnu_extensions_p (parser))
22031 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22032 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22033 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22034 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22035 == CPP_OPEN_BRACE)))
22037 if (cxx_dialect < cxx2a)
22038 pedwarn (loc, OPT_Wpedantic,
22039 "C++ designated initializers only available with "
22040 "-std=c++2a or -std=gnu++2a");
22041 /* Consume the `.'. */
22042 cp_lexer_consume_token (parser->lexer);
22043 /* Consume the identifier. */
22044 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22045 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22046 /* Consume the `='. */
22047 cp_lexer_consume_token (parser->lexer);
22049 /* Also, if the next token is an identifier and the following one is a
22050 colon, we are looking at the GNU designated-initializer
22051 syntax. */
22052 else if (cp_parser_allow_gnu_extensions_p (parser)
22053 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22054 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22055 == CPP_COLON))
22057 /* Warn the user that they are using an extension. */
22058 pedwarn (loc, OPT_Wpedantic,
22059 "ISO C++ does not allow GNU designated initializers");
22060 /* Consume the identifier. */
22061 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22062 /* Consume the `:'. */
22063 cp_lexer_consume_token (parser->lexer);
22065 /* Also handle C99 array designators, '[ const ] ='. */
22066 else if (cp_parser_allow_gnu_extensions_p (parser)
22067 && !c_dialect_objc ()
22068 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22070 /* In C++11, [ could start a lambda-introducer. */
22071 bool non_const = false;
22073 cp_parser_parse_tentatively (parser);
22075 if (!cp_parser_array_designator_p (parser))
22077 cp_parser_simulate_error (parser);
22078 designator = NULL_TREE;
22080 else
22082 designator = cp_parser_constant_expression (parser, true,
22083 &non_const);
22084 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22085 cp_parser_require (parser, CPP_EQ, RT_EQ);
22088 if (!cp_parser_parse_definitely (parser))
22089 designator = NULL_TREE;
22090 else if (non_const
22091 && (!require_potential_rvalue_constant_expression
22092 (designator)))
22093 designator = NULL_TREE;
22094 if (designator)
22095 /* Warn the user that they are using an extension. */
22096 pedwarn (loc, OPT_Wpedantic,
22097 "ISO C++ does not allow C99 designated initializers");
22099 else
22100 designator = NULL_TREE;
22102 if (first_p)
22104 first_designator = designator;
22105 first_p = false;
22107 else if (cxx_dialect >= cxx2a
22108 && first_designator != error_mark_node
22109 && (!first_designator != !designator))
22111 error_at (loc, "either all initializer clauses should be designated "
22112 "or none of them should be");
22113 first_designator = error_mark_node;
22115 else if (cxx_dialect < cxx2a && !first_designator)
22116 first_designator = designator;
22118 /* Parse the initializer. */
22119 initializer = cp_parser_initializer_clause (parser,
22120 &clause_non_constant_p);
22121 /* If any clause is non-constant, so is the entire initializer. */
22122 if (clause_non_constant_p)
22123 *non_constant_p = true;
22125 /* If we have an ellipsis, this is an initializer pack
22126 expansion. */
22127 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22129 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22131 /* Consume the `...'. */
22132 cp_lexer_consume_token (parser->lexer);
22134 if (designator && cxx_dialect >= cxx2a)
22135 error_at (loc,
22136 "%<...%> not allowed in designated initializer list");
22138 /* Turn the initializer into an initializer expansion. */
22139 initializer = make_pack_expansion (initializer);
22142 /* Add it to the vector. */
22143 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22145 /* If the next token is not a comma, we have reached the end of
22146 the list. */
22147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22148 break;
22150 /* Peek at the next token. */
22151 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22152 /* If the next token is a `}', then we're still done. An
22153 initializer-clause can have a trailing `,' after the
22154 initializer-list and before the closing `}'. */
22155 if (token->type == CPP_CLOSE_BRACE)
22156 break;
22158 /* Consume the `,' token. */
22159 cp_lexer_consume_token (parser->lexer);
22162 /* The same identifier shall not appear in multiple designators
22163 of a designated-initializer-list. */
22164 if (first_designator)
22166 unsigned int i;
22167 tree designator, val;
22168 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22169 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22171 if (IDENTIFIER_MARKED (designator))
22173 error_at (EXPR_LOC_OR_LOC (val, input_location),
22174 "%<.%s%> designator used multiple times in "
22175 "the same initializer list",
22176 IDENTIFIER_POINTER (designator));
22177 (*v)[i].index = NULL_TREE;
22179 else
22180 IDENTIFIER_MARKED (designator) = 1;
22182 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22183 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22184 IDENTIFIER_MARKED (designator) = 0;
22187 return v;
22190 /* Classes [gram.class] */
22192 /* Parse a class-name.
22194 class-name:
22195 identifier
22196 template-id
22198 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22199 to indicate that names looked up in dependent types should be
22200 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22201 keyword has been used to indicate that the name that appears next
22202 is a template. TAG_TYPE indicates the explicit tag given before
22203 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22204 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22205 is the class being defined in a class-head. If ENUM_OK is TRUE,
22206 enum-names are also accepted.
22208 Returns the TYPE_DECL representing the class. */
22210 static tree
22211 cp_parser_class_name (cp_parser *parser,
22212 bool typename_keyword_p,
22213 bool template_keyword_p,
22214 enum tag_types tag_type,
22215 bool check_dependency_p,
22216 bool class_head_p,
22217 bool is_declaration,
22218 bool enum_ok)
22220 tree decl;
22221 tree scope;
22222 bool typename_p;
22223 cp_token *token;
22224 tree identifier = NULL_TREE;
22226 /* All class-names start with an identifier. */
22227 token = cp_lexer_peek_token (parser->lexer);
22228 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22230 cp_parser_error (parser, "expected class-name");
22231 return error_mark_node;
22234 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22235 to a template-id, so we save it here. */
22236 scope = parser->scope;
22237 if (scope == error_mark_node)
22238 return error_mark_node;
22240 /* Any name names a type if we're following the `typename' keyword
22241 in a qualified name where the enclosing scope is type-dependent. */
22242 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22243 && dependent_type_p (scope));
22244 /* Handle the common case (an identifier, but not a template-id)
22245 efficiently. */
22246 if (token->type == CPP_NAME
22247 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22249 cp_token *identifier_token;
22250 bool ambiguous_p;
22252 /* Look for the identifier. */
22253 identifier_token = cp_lexer_peek_token (parser->lexer);
22254 ambiguous_p = identifier_token->error_reported;
22255 identifier = cp_parser_identifier (parser);
22256 /* If the next token isn't an identifier, we are certainly not
22257 looking at a class-name. */
22258 if (identifier == error_mark_node)
22259 decl = error_mark_node;
22260 /* If we know this is a type-name, there's no need to look it
22261 up. */
22262 else if (typename_p)
22263 decl = identifier;
22264 else
22266 tree ambiguous_decls;
22267 /* If we already know that this lookup is ambiguous, then
22268 we've already issued an error message; there's no reason
22269 to check again. */
22270 if (ambiguous_p)
22272 cp_parser_simulate_error (parser);
22273 return error_mark_node;
22275 /* If the next token is a `::', then the name must be a type
22276 name.
22278 [basic.lookup.qual]
22280 During the lookup for a name preceding the :: scope
22281 resolution operator, object, function, and enumerator
22282 names are ignored. */
22283 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22284 tag_type = scope_type;
22285 /* Look up the name. */
22286 decl = cp_parser_lookup_name (parser, identifier,
22287 tag_type,
22288 /*is_template=*/false,
22289 /*is_namespace=*/false,
22290 check_dependency_p,
22291 &ambiguous_decls,
22292 identifier_token->location);
22293 if (ambiguous_decls)
22295 if (cp_parser_parsing_tentatively (parser))
22296 cp_parser_simulate_error (parser);
22297 return error_mark_node;
22301 else
22303 /* Try a template-id. */
22304 decl = cp_parser_template_id (parser, template_keyword_p,
22305 check_dependency_p,
22306 tag_type,
22307 is_declaration);
22308 if (decl == error_mark_node)
22309 return error_mark_node;
22312 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22314 /* If this is a typename, create a TYPENAME_TYPE. */
22315 if (typename_p && decl != error_mark_node)
22317 decl = make_typename_type (scope, decl, typename_type,
22318 /*complain=*/tf_error);
22319 if (decl != error_mark_node)
22320 decl = TYPE_NAME (decl);
22323 decl = strip_using_decl (decl);
22325 /* Check to see that it is really the name of a class. */
22326 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22327 && identifier_p (TREE_OPERAND (decl, 0))
22328 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22329 /* Situations like this:
22331 template <typename T> struct A {
22332 typename T::template X<int>::I i;
22335 are problematic. Is `T::template X<int>' a class-name? The
22336 standard does not seem to be definitive, but there is no other
22337 valid interpretation of the following `::'. Therefore, those
22338 names are considered class-names. */
22340 decl = make_typename_type (scope, decl, tag_type, tf_error);
22341 if (decl != error_mark_node)
22342 decl = TYPE_NAME (decl);
22344 else if (TREE_CODE (decl) != TYPE_DECL
22345 || TREE_TYPE (decl) == error_mark_node
22346 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22347 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22348 /* In Objective-C 2.0, a classname followed by '.' starts a
22349 dot-syntax expression, and it's not a type-name. */
22350 || (c_dialect_objc ()
22351 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22352 && objc_is_class_name (decl)))
22353 decl = error_mark_node;
22355 if (decl == error_mark_node)
22356 cp_parser_error (parser, "expected class-name");
22357 else if (identifier && !parser->scope)
22358 maybe_note_name_used_in_class (identifier, decl);
22360 return decl;
22363 /* Parse a class-specifier.
22365 class-specifier:
22366 class-head { member-specification [opt] }
22368 Returns the TREE_TYPE representing the class. */
22370 static tree
22371 cp_parser_class_specifier_1 (cp_parser* parser)
22373 tree type;
22374 tree attributes = NULL_TREE;
22375 bool nested_name_specifier_p;
22376 unsigned saved_num_template_parameter_lists;
22377 bool saved_in_function_body;
22378 unsigned char in_statement;
22379 bool in_switch_statement_p;
22380 bool saved_in_unbraced_linkage_specification_p;
22381 tree old_scope = NULL_TREE;
22382 tree scope = NULL_TREE;
22383 cp_token *closing_brace;
22385 push_deferring_access_checks (dk_no_deferred);
22387 /* Parse the class-head. */
22388 type = cp_parser_class_head (parser,
22389 &nested_name_specifier_p);
22390 /* If the class-head was a semantic disaster, skip the entire body
22391 of the class. */
22392 if (!type)
22394 cp_parser_skip_to_end_of_block_or_statement (parser);
22395 pop_deferring_access_checks ();
22396 return error_mark_node;
22399 /* Look for the `{'. */
22400 matching_braces braces;
22401 if (!braces.require_open (parser))
22403 pop_deferring_access_checks ();
22404 return error_mark_node;
22407 cp_ensure_no_omp_declare_simd (parser);
22408 cp_ensure_no_oacc_routine (parser);
22410 /* Issue an error message if type-definitions are forbidden here. */
22411 cp_parser_check_type_definition (parser);
22412 /* Remember that we are defining one more class. */
22413 ++parser->num_classes_being_defined;
22414 /* Inside the class, surrounding template-parameter-lists do not
22415 apply. */
22416 saved_num_template_parameter_lists
22417 = parser->num_template_parameter_lists;
22418 parser->num_template_parameter_lists = 0;
22419 /* We are not in a function body. */
22420 saved_in_function_body = parser->in_function_body;
22421 parser->in_function_body = false;
22422 /* Or in a loop. */
22423 in_statement = parser->in_statement;
22424 parser->in_statement = 0;
22425 /* Or in a switch. */
22426 in_switch_statement_p = parser->in_switch_statement_p;
22427 parser->in_switch_statement_p = false;
22428 /* We are not immediately inside an extern "lang" block. */
22429 saved_in_unbraced_linkage_specification_p
22430 = parser->in_unbraced_linkage_specification_p;
22431 parser->in_unbraced_linkage_specification_p = false;
22433 // Associate constraints with the type.
22434 if (flag_concepts)
22435 type = associate_classtype_constraints (type);
22437 /* Start the class. */
22438 if (nested_name_specifier_p)
22440 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22441 old_scope = push_inner_scope (scope);
22443 type = begin_class_definition (type);
22445 if (type == error_mark_node)
22446 /* If the type is erroneous, skip the entire body of the class. */
22447 cp_parser_skip_to_closing_brace (parser);
22448 else
22449 /* Parse the member-specification. */
22450 cp_parser_member_specification_opt (parser);
22452 /* Look for the trailing `}'. */
22453 closing_brace = braces.require_close (parser);
22454 /* Look for trailing attributes to apply to this class. */
22455 if (cp_parser_allow_gnu_extensions_p (parser))
22456 attributes = cp_parser_gnu_attributes_opt (parser);
22457 if (type != error_mark_node)
22458 type = finish_struct (type, attributes);
22459 if (nested_name_specifier_p)
22460 pop_inner_scope (old_scope, scope);
22462 /* We've finished a type definition. Check for the common syntax
22463 error of forgetting a semicolon after the definition. We need to
22464 be careful, as we can't just check for not-a-semicolon and be done
22465 with it; the user might have typed:
22467 class X { } c = ...;
22468 class X { } *p = ...;
22470 and so forth. Instead, enumerate all the possible tokens that
22471 might follow this production; if we don't see one of them, then
22472 complain and silently insert the semicolon. */
22474 cp_token *token = cp_lexer_peek_token (parser->lexer);
22475 bool want_semicolon = true;
22477 if (cp_next_tokens_can_be_std_attribute_p (parser))
22478 /* Don't try to parse c++11 attributes here. As per the
22479 grammar, that should be a task for
22480 cp_parser_decl_specifier_seq. */
22481 want_semicolon = false;
22483 switch (token->type)
22485 case CPP_NAME:
22486 case CPP_SEMICOLON:
22487 case CPP_MULT:
22488 case CPP_AND:
22489 case CPP_OPEN_PAREN:
22490 case CPP_CLOSE_PAREN:
22491 case CPP_COMMA:
22492 want_semicolon = false;
22493 break;
22495 /* While it's legal for type qualifiers and storage class
22496 specifiers to follow type definitions in the grammar, only
22497 compiler testsuites contain code like that. Assume that if
22498 we see such code, then what we're really seeing is a case
22499 like:
22501 class X { }
22502 const <type> var = ...;
22506 class Y { }
22507 static <type> func (...) ...
22509 i.e. the qualifier or specifier applies to the next
22510 declaration. To do so, however, we need to look ahead one
22511 more token to see if *that* token is a type specifier.
22513 This code could be improved to handle:
22515 class Z { }
22516 static const <type> var = ...; */
22517 case CPP_KEYWORD:
22518 if (keyword_is_decl_specifier (token->keyword))
22520 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22522 /* Handling user-defined types here would be nice, but very
22523 tricky. */
22524 want_semicolon
22525 = (lookahead->type == CPP_KEYWORD
22526 && keyword_begins_type_specifier (lookahead->keyword));
22528 break;
22529 default:
22530 break;
22533 /* If we don't have a type, then something is very wrong and we
22534 shouldn't try to do anything clever. Likewise for not seeing the
22535 closing brace. */
22536 if (closing_brace && TYPE_P (type) && want_semicolon)
22538 /* Locate the closing brace. */
22539 cp_token_position prev
22540 = cp_lexer_previous_token_position (parser->lexer);
22541 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22542 location_t loc = prev_token->location;
22544 /* We want to suggest insertion of a ';' immediately *after* the
22545 closing brace, so, if we can, offset the location by 1 column. */
22546 location_t next_loc = loc;
22547 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22548 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22550 rich_location richloc (line_table, next_loc);
22552 /* If we successfully offset the location, suggest the fix-it. */
22553 if (next_loc != loc)
22554 richloc.add_fixit_insert_before (next_loc, ";");
22556 if (CLASSTYPE_DECLARED_CLASS (type))
22557 error_at (&richloc,
22558 "expected %<;%> after class definition");
22559 else if (TREE_CODE (type) == RECORD_TYPE)
22560 error_at (&richloc,
22561 "expected %<;%> after struct definition");
22562 else if (TREE_CODE (type) == UNION_TYPE)
22563 error_at (&richloc,
22564 "expected %<;%> after union definition");
22565 else
22566 gcc_unreachable ();
22568 /* Unget one token and smash it to look as though we encountered
22569 a semicolon in the input stream. */
22570 cp_lexer_set_token_position (parser->lexer, prev);
22571 token = cp_lexer_peek_token (parser->lexer);
22572 token->type = CPP_SEMICOLON;
22573 token->keyword = RID_MAX;
22577 /* If this class is not itself within the scope of another class,
22578 then we need to parse the bodies of all of the queued function
22579 definitions. Note that the queued functions defined in a class
22580 are not always processed immediately following the
22581 class-specifier for that class. Consider:
22583 struct A {
22584 struct B { void f() { sizeof (A); } };
22587 If `f' were processed before the processing of `A' were
22588 completed, there would be no way to compute the size of `A'.
22589 Note that the nesting we are interested in here is lexical --
22590 not the semantic nesting given by TYPE_CONTEXT. In particular,
22591 for:
22593 struct A { struct B; };
22594 struct A::B { void f() { } };
22596 there is no need to delay the parsing of `A::B::f'. */
22597 if (--parser->num_classes_being_defined == 0)
22599 tree decl;
22600 tree class_type = NULL_TREE;
22601 tree pushed_scope = NULL_TREE;
22602 unsigned ix;
22603 cp_default_arg_entry *e;
22604 tree save_ccp, save_ccr;
22606 /* In a first pass, parse default arguments to the functions.
22607 Then, in a second pass, parse the bodies of the functions.
22608 This two-phased approach handles cases like:
22610 struct S {
22611 void f() { g(); }
22612 void g(int i = 3);
22616 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22618 decl = e->decl;
22619 /* If there are default arguments that have not yet been processed,
22620 take care of them now. */
22621 if (class_type != e->class_type)
22623 if (pushed_scope)
22624 pop_scope (pushed_scope);
22625 class_type = e->class_type;
22626 pushed_scope = push_scope (class_type);
22628 /* Make sure that any template parameters are in scope. */
22629 maybe_begin_member_template_processing (decl);
22630 /* Parse the default argument expressions. */
22631 cp_parser_late_parsing_default_args (parser, decl);
22632 /* Remove any template parameters from the symbol table. */
22633 maybe_end_member_template_processing ();
22635 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22636 /* Now parse any NSDMIs. */
22637 save_ccp = current_class_ptr;
22638 save_ccr = current_class_ref;
22639 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22641 if (class_type != DECL_CONTEXT (decl))
22643 if (pushed_scope)
22644 pop_scope (pushed_scope);
22645 class_type = DECL_CONTEXT (decl);
22646 pushed_scope = push_scope (class_type);
22648 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22649 cp_parser_late_parsing_nsdmi (parser, decl);
22651 vec_safe_truncate (unparsed_nsdmis, 0);
22652 current_class_ptr = save_ccp;
22653 current_class_ref = save_ccr;
22654 if (pushed_scope)
22655 pop_scope (pushed_scope);
22657 /* Now do some post-NSDMI bookkeeping. */
22658 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22659 after_nsdmi_defaulted_late_checks (class_type);
22660 vec_safe_truncate (unparsed_classes, 0);
22661 after_nsdmi_defaulted_late_checks (type);
22663 /* Now parse the body of the functions. */
22664 if (flag_openmp)
22666 /* OpenMP UDRs need to be parsed before all other functions. */
22667 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22668 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22669 cp_parser_late_parsing_for_member (parser, decl);
22670 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22671 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22672 cp_parser_late_parsing_for_member (parser, decl);
22674 else
22675 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22676 cp_parser_late_parsing_for_member (parser, decl);
22677 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22679 else
22680 vec_safe_push (unparsed_classes, type);
22682 /* Put back any saved access checks. */
22683 pop_deferring_access_checks ();
22685 /* Restore saved state. */
22686 parser->in_switch_statement_p = in_switch_statement_p;
22687 parser->in_statement = in_statement;
22688 parser->in_function_body = saved_in_function_body;
22689 parser->num_template_parameter_lists
22690 = saved_num_template_parameter_lists;
22691 parser->in_unbraced_linkage_specification_p
22692 = saved_in_unbraced_linkage_specification_p;
22694 return type;
22697 static tree
22698 cp_parser_class_specifier (cp_parser* parser)
22700 tree ret;
22701 timevar_push (TV_PARSE_STRUCT);
22702 ret = cp_parser_class_specifier_1 (parser);
22703 timevar_pop (TV_PARSE_STRUCT);
22704 return ret;
22707 /* Parse a class-head.
22709 class-head:
22710 class-key identifier [opt] base-clause [opt]
22711 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22712 class-key nested-name-specifier [opt] template-id
22713 base-clause [opt]
22715 class-virt-specifier:
22716 final
22718 GNU Extensions:
22719 class-key attributes identifier [opt] base-clause [opt]
22720 class-key attributes nested-name-specifier identifier base-clause [opt]
22721 class-key attributes nested-name-specifier [opt] template-id
22722 base-clause [opt]
22724 Upon return BASES is initialized to the list of base classes (or
22725 NULL, if there are none) in the same form returned by
22726 cp_parser_base_clause.
22728 Returns the TYPE of the indicated class. Sets
22729 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22730 involving a nested-name-specifier was used, and FALSE otherwise.
22732 Returns error_mark_node if this is not a class-head.
22734 Returns NULL_TREE if the class-head is syntactically valid, but
22735 semantically invalid in a way that means we should skip the entire
22736 body of the class. */
22738 static tree
22739 cp_parser_class_head (cp_parser* parser,
22740 bool* nested_name_specifier_p)
22742 tree nested_name_specifier;
22743 enum tag_types class_key;
22744 tree id = NULL_TREE;
22745 tree type = NULL_TREE;
22746 tree attributes;
22747 tree bases;
22748 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22749 bool template_id_p = false;
22750 bool qualified_p = false;
22751 bool invalid_nested_name_p = false;
22752 bool invalid_explicit_specialization_p = false;
22753 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22754 tree pushed_scope = NULL_TREE;
22755 unsigned num_templates;
22756 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22757 /* Assume no nested-name-specifier will be present. */
22758 *nested_name_specifier_p = false;
22759 /* Assume no template parameter lists will be used in defining the
22760 type. */
22761 num_templates = 0;
22762 parser->colon_corrects_to_scope_p = false;
22764 /* Look for the class-key. */
22765 class_key = cp_parser_class_key (parser);
22766 if (class_key == none_type)
22767 return error_mark_node;
22769 location_t class_head_start_location = input_location;
22771 /* Parse the attributes. */
22772 attributes = cp_parser_attributes_opt (parser);
22774 /* If the next token is `::', that is invalid -- but sometimes
22775 people do try to write:
22777 struct ::S {};
22779 Handle this gracefully by accepting the extra qualifier, and then
22780 issuing an error about it later if this really is a
22781 class-head. If it turns out just to be an elaborated type
22782 specifier, remain silent. */
22783 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22784 qualified_p = true;
22786 push_deferring_access_checks (dk_no_check);
22788 /* Determine the name of the class. Begin by looking for an
22789 optional nested-name-specifier. */
22790 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22791 nested_name_specifier
22792 = cp_parser_nested_name_specifier_opt (parser,
22793 /*typename_keyword_p=*/false,
22794 /*check_dependency_p=*/false,
22795 /*type_p=*/true,
22796 /*is_declaration=*/false);
22797 /* If there was a nested-name-specifier, then there *must* be an
22798 identifier. */
22800 cp_token *bad_template_keyword = NULL;
22802 if (nested_name_specifier)
22804 type_start_token = cp_lexer_peek_token (parser->lexer);
22805 /* Although the grammar says `identifier', it really means
22806 `class-name' or `template-name'. You are only allowed to
22807 define a class that has already been declared with this
22808 syntax.
22810 The proposed resolution for Core Issue 180 says that wherever
22811 you see `class T::X' you should treat `X' as a type-name.
22813 It is OK to define an inaccessible class; for example:
22815 class A { class B; };
22816 class A::B {};
22818 We do not know if we will see a class-name, or a
22819 template-name. We look for a class-name first, in case the
22820 class-name is a template-id; if we looked for the
22821 template-name first we would stop after the template-name. */
22822 cp_parser_parse_tentatively (parser);
22823 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22824 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22825 type = cp_parser_class_name (parser,
22826 /*typename_keyword_p=*/false,
22827 /*template_keyword_p=*/false,
22828 class_type,
22829 /*check_dependency_p=*/false,
22830 /*class_head_p=*/true,
22831 /*is_declaration=*/false);
22832 /* If that didn't work, ignore the nested-name-specifier. */
22833 if (!cp_parser_parse_definitely (parser))
22835 invalid_nested_name_p = true;
22836 type_start_token = cp_lexer_peek_token (parser->lexer);
22837 id = cp_parser_identifier (parser);
22838 if (id == error_mark_node)
22839 id = NULL_TREE;
22841 /* If we could not find a corresponding TYPE, treat this
22842 declaration like an unqualified declaration. */
22843 if (type == error_mark_node)
22844 nested_name_specifier = NULL_TREE;
22845 /* Otherwise, count the number of templates used in TYPE and its
22846 containing scopes. */
22847 else
22849 tree scope;
22851 for (scope = TREE_TYPE (type);
22852 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22853 scope = get_containing_scope (scope))
22854 if (TYPE_P (scope)
22855 && CLASS_TYPE_P (scope)
22856 && CLASSTYPE_TEMPLATE_INFO (scope)
22857 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22858 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22859 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22860 ++num_templates;
22863 /* Otherwise, the identifier is optional. */
22864 else
22866 /* We don't know whether what comes next is a template-id,
22867 an identifier, or nothing at all. */
22868 cp_parser_parse_tentatively (parser);
22869 /* Check for a template-id. */
22870 type_start_token = cp_lexer_peek_token (parser->lexer);
22871 id = cp_parser_template_id (parser,
22872 /*template_keyword_p=*/false,
22873 /*check_dependency_p=*/true,
22874 class_key,
22875 /*is_declaration=*/true);
22876 /* If that didn't work, it could still be an identifier. */
22877 if (!cp_parser_parse_definitely (parser))
22879 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22881 type_start_token = cp_lexer_peek_token (parser->lexer);
22882 id = cp_parser_identifier (parser);
22884 else
22885 id = NULL_TREE;
22887 else
22889 template_id_p = true;
22890 ++num_templates;
22894 pop_deferring_access_checks ();
22896 if (id)
22898 cp_parser_check_for_invalid_template_id (parser, id,
22899 class_key,
22900 type_start_token->location);
22902 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22904 /* If it's not a `:' or a `{' then we can't really be looking at a
22905 class-head, since a class-head only appears as part of a
22906 class-specifier. We have to detect this situation before calling
22907 xref_tag, since that has irreversible side-effects. */
22908 if (!cp_parser_next_token_starts_class_definition_p (parser))
22910 cp_parser_error (parser, "expected %<{%> or %<:%>");
22911 type = error_mark_node;
22912 goto out;
22915 /* At this point, we're going ahead with the class-specifier, even
22916 if some other problem occurs. */
22917 cp_parser_commit_to_tentative_parse (parser);
22918 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22920 cp_parser_error (parser,
22921 "cannot specify %<override%> for a class");
22922 type = error_mark_node;
22923 goto out;
22925 /* Issue the error about the overly-qualified name now. */
22926 if (qualified_p)
22928 cp_parser_error (parser,
22929 "global qualification of class name is invalid");
22930 type = error_mark_node;
22931 goto out;
22933 else if (invalid_nested_name_p)
22935 cp_parser_error (parser,
22936 "qualified name does not name a class");
22937 type = error_mark_node;
22938 goto out;
22940 else if (nested_name_specifier)
22942 tree scope;
22944 if (bad_template_keyword)
22945 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22946 keyword template shall not appear at the top level. */
22947 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
22948 "keyword %<template%> not allowed in class-head-name");
22950 /* Reject typedef-names in class heads. */
22951 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22953 error_at (type_start_token->location,
22954 "invalid class name in declaration of %qD",
22955 type);
22956 type = NULL_TREE;
22957 goto done;
22960 /* Figure out in what scope the declaration is being placed. */
22961 scope = current_scope ();
22962 /* If that scope does not contain the scope in which the
22963 class was originally declared, the program is invalid. */
22964 if (scope && !is_ancestor (scope, nested_name_specifier))
22966 if (at_namespace_scope_p ())
22967 error_at (type_start_token->location,
22968 "declaration of %qD in namespace %qD which does not "
22969 "enclose %qD",
22970 type, scope, nested_name_specifier);
22971 else
22972 error_at (type_start_token->location,
22973 "declaration of %qD in %qD which does not enclose %qD",
22974 type, scope, nested_name_specifier);
22975 type = NULL_TREE;
22976 goto done;
22978 /* [dcl.meaning]
22980 A declarator-id shall not be qualified except for the
22981 definition of a ... nested class outside of its class
22982 ... [or] the definition or explicit instantiation of a
22983 class member of a namespace outside of its namespace. */
22984 if (scope == nested_name_specifier)
22986 permerror (nested_name_specifier_token_start->location,
22987 "extra qualification not allowed");
22988 nested_name_specifier = NULL_TREE;
22989 num_templates = 0;
22992 /* An explicit-specialization must be preceded by "template <>". If
22993 it is not, try to recover gracefully. */
22994 if (at_namespace_scope_p ()
22995 && parser->num_template_parameter_lists == 0
22996 && !processing_template_parmlist
22997 && template_id_p)
22999 /* Build a location of this form:
23000 struct typename <ARGS>
23001 ^~~~~~~~~~~~~~~~~~~~~~
23002 with caret==start at the start token, and
23003 finishing at the end of the type. */
23004 location_t reported_loc
23005 = make_location (class_head_start_location,
23006 class_head_start_location,
23007 get_finish (type_start_token->location));
23008 rich_location richloc (line_table, reported_loc);
23009 richloc.add_fixit_insert_before (class_head_start_location,
23010 "template <> ");
23011 error_at (&richloc,
23012 "an explicit specialization must be preceded by"
23013 " %<template <>%>");
23014 invalid_explicit_specialization_p = true;
23015 /* Take the same action that would have been taken by
23016 cp_parser_explicit_specialization. */
23017 ++parser->num_template_parameter_lists;
23018 begin_specialization ();
23020 /* There must be no "return" statements between this point and the
23021 end of this function; set "type "to the correct return value and
23022 use "goto done;" to return. */
23023 /* Make sure that the right number of template parameters were
23024 present. */
23025 if (!cp_parser_check_template_parameters (parser, num_templates,
23026 type_start_token->location,
23027 /*declarator=*/NULL))
23029 /* If something went wrong, there is no point in even trying to
23030 process the class-definition. */
23031 type = NULL_TREE;
23032 goto done;
23035 /* Look up the type. */
23036 if (template_id_p)
23038 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23039 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23040 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23042 error_at (type_start_token->location,
23043 "function template %qD redeclared as a class template", id);
23044 type = error_mark_node;
23046 else
23048 type = TREE_TYPE (id);
23049 type = maybe_process_partial_specialization (type);
23051 /* Check the scope while we still know whether or not we had a
23052 nested-name-specifier. */
23053 if (type != error_mark_node)
23054 check_unqualified_spec_or_inst (type, type_start_token->location);
23056 if (nested_name_specifier)
23057 pushed_scope = push_scope (nested_name_specifier);
23059 else if (nested_name_specifier)
23061 tree class_type;
23063 /* Given:
23065 template <typename T> struct S { struct T };
23066 template <typename T> struct S<T>::T { };
23068 we will get a TYPENAME_TYPE when processing the definition of
23069 `S::T'. We need to resolve it to the actual type before we
23070 try to define it. */
23071 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23073 class_type = resolve_typename_type (TREE_TYPE (type),
23074 /*only_current_p=*/false);
23075 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23076 type = TYPE_NAME (class_type);
23077 else
23079 cp_parser_error (parser, "could not resolve typename type");
23080 type = error_mark_node;
23084 if (maybe_process_partial_specialization (TREE_TYPE (type))
23085 == error_mark_node)
23087 type = NULL_TREE;
23088 goto done;
23091 class_type = current_class_type;
23092 /* Enter the scope indicated by the nested-name-specifier. */
23093 pushed_scope = push_scope (nested_name_specifier);
23094 /* Get the canonical version of this type. */
23095 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23096 /* Call push_template_decl if it seems like we should be defining a
23097 template either from the template headers or the type we're
23098 defining, so that we diagnose both extra and missing headers. */
23099 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23100 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23101 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23103 type = push_template_decl (type);
23104 if (type == error_mark_node)
23106 type = NULL_TREE;
23107 goto done;
23111 type = TREE_TYPE (type);
23112 *nested_name_specifier_p = true;
23114 else /* The name is not a nested name. */
23116 /* If the class was unnamed, create a dummy name. */
23117 if (!id)
23118 id = make_anon_name ();
23119 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23120 ? ts_within_enclosing_non_class
23121 : ts_current);
23122 type = xref_tag (class_key, id, tag_scope,
23123 parser->num_template_parameter_lists);
23126 /* Indicate whether this class was declared as a `class' or as a
23127 `struct'. */
23128 if (TREE_CODE (type) == RECORD_TYPE)
23129 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23130 cp_parser_check_class_key (class_key, type);
23132 /* If this type was already complete, and we see another definition,
23133 that's an error. */
23134 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23136 error_at (type_start_token->location, "redefinition of %q#T",
23137 type);
23138 inform (location_of (type), "previous definition of %q#T",
23139 type);
23140 type = NULL_TREE;
23141 goto done;
23143 else if (type == error_mark_node)
23144 type = NULL_TREE;
23146 if (type)
23148 /* Apply attributes now, before any use of the class as a template
23149 argument in its base list. */
23150 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23151 fixup_attribute_variants (type);
23154 /* We will have entered the scope containing the class; the names of
23155 base classes should be looked up in that context. For example:
23157 struct A { struct B {}; struct C; };
23158 struct A::C : B {};
23160 is valid. */
23162 /* Get the list of base-classes, if there is one. */
23163 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23165 /* PR59482: enter the class scope so that base-specifiers are looked
23166 up correctly. */
23167 if (type)
23168 pushclass (type);
23169 bases = cp_parser_base_clause (parser);
23170 /* PR59482: get out of the previously pushed class scope so that the
23171 subsequent pops pop the right thing. */
23172 if (type)
23173 popclass ();
23175 else
23176 bases = NULL_TREE;
23178 /* If we're really defining a class, process the base classes.
23179 If they're invalid, fail. */
23180 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23181 xref_basetypes (type, bases);
23183 done:
23184 /* Leave the scope given by the nested-name-specifier. We will
23185 enter the class scope itself while processing the members. */
23186 if (pushed_scope)
23187 pop_scope (pushed_scope);
23189 if (invalid_explicit_specialization_p)
23191 end_specialization ();
23192 --parser->num_template_parameter_lists;
23195 if (type)
23196 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23197 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23198 CLASSTYPE_FINAL (type) = 1;
23199 out:
23200 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23201 return type;
23204 /* Parse a class-key.
23206 class-key:
23207 class
23208 struct
23209 union
23211 Returns the kind of class-key specified, or none_type to indicate
23212 error. */
23214 static enum tag_types
23215 cp_parser_class_key (cp_parser* parser)
23217 cp_token *token;
23218 enum tag_types tag_type;
23220 /* Look for the class-key. */
23221 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23222 if (!token)
23223 return none_type;
23225 /* Check to see if the TOKEN is a class-key. */
23226 tag_type = cp_parser_token_is_class_key (token);
23227 if (!tag_type)
23228 cp_parser_error (parser, "expected class-key");
23229 return tag_type;
23232 /* Parse a type-parameter-key.
23234 type-parameter-key:
23235 class
23236 typename
23239 static void
23240 cp_parser_type_parameter_key (cp_parser* parser)
23242 /* Look for the type-parameter-key. */
23243 enum tag_types tag_type = none_type;
23244 cp_token *token = cp_lexer_peek_token (parser->lexer);
23245 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23247 cp_lexer_consume_token (parser->lexer);
23248 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23249 /* typename is not allowed in a template template parameter
23250 by the standard until C++17. */
23251 pedwarn (token->location, OPT_Wpedantic,
23252 "ISO C++ forbids typename key in template template parameter;"
23253 " use -std=c++17 or -std=gnu++17");
23255 else
23256 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23258 return;
23261 /* Parse an (optional) member-specification.
23263 member-specification:
23264 member-declaration member-specification [opt]
23265 access-specifier : member-specification [opt] */
23267 static void
23268 cp_parser_member_specification_opt (cp_parser* parser)
23270 while (true)
23272 cp_token *token;
23273 enum rid keyword;
23275 /* Peek at the next token. */
23276 token = cp_lexer_peek_token (parser->lexer);
23277 /* If it's a `}', or EOF then we've seen all the members. */
23278 if (token->type == CPP_CLOSE_BRACE
23279 || token->type == CPP_EOF
23280 || token->type == CPP_PRAGMA_EOL)
23281 break;
23283 /* See if this token is a keyword. */
23284 keyword = token->keyword;
23285 switch (keyword)
23287 case RID_PUBLIC:
23288 case RID_PROTECTED:
23289 case RID_PRIVATE:
23290 /* Consume the access-specifier. */
23291 cp_lexer_consume_token (parser->lexer);
23292 /* Remember which access-specifier is active. */
23293 current_access_specifier = token->u.value;
23294 /* Look for the `:'. */
23295 cp_parser_require (parser, CPP_COLON, RT_COLON);
23296 break;
23298 default:
23299 /* Accept #pragmas at class scope. */
23300 if (token->type == CPP_PRAGMA)
23302 cp_parser_pragma (parser, pragma_member, NULL);
23303 break;
23306 /* Otherwise, the next construction must be a
23307 member-declaration. */
23308 cp_parser_member_declaration (parser);
23313 /* Parse a member-declaration.
23315 member-declaration:
23316 decl-specifier-seq [opt] member-declarator-list [opt] ;
23317 function-definition ; [opt]
23318 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23319 using-declaration
23320 template-declaration
23321 alias-declaration
23323 member-declarator-list:
23324 member-declarator
23325 member-declarator-list , member-declarator
23327 member-declarator:
23328 declarator pure-specifier [opt]
23329 declarator constant-initializer [opt]
23330 identifier [opt] : constant-expression
23332 GNU Extensions:
23334 member-declaration:
23335 __extension__ member-declaration
23337 member-declarator:
23338 declarator attributes [opt] pure-specifier [opt]
23339 declarator attributes [opt] constant-initializer [opt]
23340 identifier [opt] attributes [opt] : constant-expression
23342 C++0x Extensions:
23344 member-declaration:
23345 static_assert-declaration */
23347 static void
23348 cp_parser_member_declaration (cp_parser* parser)
23350 cp_decl_specifier_seq decl_specifiers;
23351 tree prefix_attributes;
23352 tree decl;
23353 int declares_class_or_enum;
23354 bool friend_p;
23355 cp_token *token = NULL;
23356 cp_token *decl_spec_token_start = NULL;
23357 cp_token *initializer_token_start = NULL;
23358 int saved_pedantic;
23359 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23361 /* Check for the `__extension__' keyword. */
23362 if (cp_parser_extension_opt (parser, &saved_pedantic))
23364 /* Recurse. */
23365 cp_parser_member_declaration (parser);
23366 /* Restore the old value of the PEDANTIC flag. */
23367 pedantic = saved_pedantic;
23369 return;
23372 /* Check for a template-declaration. */
23373 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23375 /* An explicit specialization here is an error condition, and we
23376 expect the specialization handler to detect and report this. */
23377 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23378 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23379 cp_parser_explicit_specialization (parser);
23380 else
23381 cp_parser_template_declaration (parser, /*member_p=*/true);
23383 return;
23385 /* Check for a template introduction. */
23386 else if (cp_parser_template_declaration_after_export (parser, true))
23387 return;
23389 /* Check for a using-declaration. */
23390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23392 if (cxx_dialect < cxx11)
23394 /* Parse the using-declaration. */
23395 cp_parser_using_declaration (parser,
23396 /*access_declaration_p=*/false);
23397 return;
23399 else
23401 tree decl;
23402 bool alias_decl_expected;
23403 cp_parser_parse_tentatively (parser);
23404 decl = cp_parser_alias_declaration (parser);
23405 /* Note that if we actually see the '=' token after the
23406 identifier, cp_parser_alias_declaration commits the
23407 tentative parse. In that case, we really expect an
23408 alias-declaration. Otherwise, we expect a using
23409 declaration. */
23410 alias_decl_expected =
23411 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23412 cp_parser_parse_definitely (parser);
23414 if (alias_decl_expected)
23415 finish_member_declaration (decl);
23416 else
23417 cp_parser_using_declaration (parser,
23418 /*access_declaration_p=*/false);
23419 return;
23423 /* Check for @defs. */
23424 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23426 tree ivar, member;
23427 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23428 ivar = ivar_chains;
23429 while (ivar)
23431 member = ivar;
23432 ivar = TREE_CHAIN (member);
23433 TREE_CHAIN (member) = NULL_TREE;
23434 finish_member_declaration (member);
23436 return;
23439 /* If the next token is `static_assert' we have a static assertion. */
23440 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23442 cp_parser_static_assert (parser, /*member_p=*/true);
23443 return;
23446 parser->colon_corrects_to_scope_p = false;
23448 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23449 goto out;
23451 /* Parse the decl-specifier-seq. */
23452 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23453 cp_parser_decl_specifier_seq (parser,
23454 CP_PARSER_FLAGS_OPTIONAL,
23455 &decl_specifiers,
23456 &declares_class_or_enum);
23457 /* Check for an invalid type-name. */
23458 if (!decl_specifiers.any_type_specifiers_p
23459 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23460 goto out;
23461 /* If there is no declarator, then the decl-specifier-seq should
23462 specify a type. */
23463 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23465 /* If there was no decl-specifier-seq, and the next token is a
23466 `;', then we have something like:
23468 struct S { ; };
23470 [class.mem]
23472 Each member-declaration shall declare at least one member
23473 name of the class. */
23474 if (!decl_specifiers.any_specifiers_p)
23476 cp_token *token = cp_lexer_peek_token (parser->lexer);
23477 if (!in_system_header_at (token->location))
23479 gcc_rich_location richloc (token->location);
23480 richloc.add_fixit_remove ();
23481 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23484 else
23486 tree type;
23488 /* See if this declaration is a friend. */
23489 friend_p = cp_parser_friend_p (&decl_specifiers);
23490 /* If there were decl-specifiers, check to see if there was
23491 a class-declaration. */
23492 type = check_tag_decl (&decl_specifiers,
23493 /*explicit_type_instantiation_p=*/false);
23494 /* Nested classes have already been added to the class, but
23495 a `friend' needs to be explicitly registered. */
23496 if (friend_p)
23498 /* If the `friend' keyword was present, the friend must
23499 be introduced with a class-key. */
23500 if (!declares_class_or_enum && cxx_dialect < cxx11)
23501 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23502 "in C++03 a class-key must be used "
23503 "when declaring a friend");
23504 /* In this case:
23506 template <typename T> struct A {
23507 friend struct A<T>::B;
23510 A<T>::B will be represented by a TYPENAME_TYPE, and
23511 therefore not recognized by check_tag_decl. */
23512 if (!type)
23514 type = decl_specifiers.type;
23515 if (type && TREE_CODE (type) == TYPE_DECL)
23516 type = TREE_TYPE (type);
23518 if (!type || !TYPE_P (type))
23519 error_at (decl_spec_token_start->location,
23520 "friend declaration does not name a class or "
23521 "function");
23522 else
23523 make_friend_class (current_class_type, type,
23524 /*complain=*/true);
23526 /* If there is no TYPE, an error message will already have
23527 been issued. */
23528 else if (!type || type == error_mark_node)
23530 /* An anonymous aggregate has to be handled specially; such
23531 a declaration really declares a data member (with a
23532 particular type), as opposed to a nested class. */
23533 else if (ANON_AGGR_TYPE_P (type))
23535 /* C++11 9.5/6. */
23536 if (decl_specifiers.storage_class != sc_none)
23537 error_at (decl_spec_token_start->location,
23538 "a storage class on an anonymous aggregate "
23539 "in class scope is not allowed");
23541 /* Remove constructors and such from TYPE, now that we
23542 know it is an anonymous aggregate. */
23543 fixup_anonymous_aggr (type);
23544 /* And make the corresponding data member. */
23545 decl = build_decl (decl_spec_token_start->location,
23546 FIELD_DECL, NULL_TREE, type);
23547 /* Add it to the class. */
23548 finish_member_declaration (decl);
23550 else
23551 cp_parser_check_access_in_redeclaration
23552 (TYPE_NAME (type),
23553 decl_spec_token_start->location);
23556 else
23558 bool assume_semicolon = false;
23560 /* Clear attributes from the decl_specifiers but keep them
23561 around as prefix attributes that apply them to the entity
23562 being declared. */
23563 prefix_attributes = decl_specifiers.attributes;
23564 decl_specifiers.attributes = NULL_TREE;
23566 /* See if these declarations will be friends. */
23567 friend_p = cp_parser_friend_p (&decl_specifiers);
23569 /* Keep going until we hit the `;' at the end of the
23570 declaration. */
23571 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23573 tree attributes = NULL_TREE;
23574 tree first_attribute;
23575 tree initializer;
23576 bool is_bitfld = false;
23577 bool named_bitfld = false;
23579 /* Peek at the next token. */
23580 token = cp_lexer_peek_token (parser->lexer);
23582 /* The following code wants to know early if it is a bit-field
23583 or some other declaration. Attributes can appear before
23584 the `:' token, but are hopefully rare enough that the
23585 simplicity of the tentative lookup pays off. */
23586 if (cp_next_tokens_can_be_attribute_p (parser)
23587 || (token->type == CPP_NAME
23588 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23589 && (named_bitfld = true)))
23591 cp_parser_parse_tentatively (parser);
23592 if (named_bitfld)
23593 cp_lexer_consume_token (parser->lexer);
23594 cp_parser_attributes_opt (parser);
23595 token = cp_lexer_peek_token (parser->lexer);
23596 is_bitfld = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
23597 cp_parser_abort_tentative_parse (parser);
23600 /* Check for a bitfield declaration. */
23601 if (is_bitfld
23602 || token->type == CPP_COLON
23603 || (token->type == CPP_NAME
23604 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23605 && (named_bitfld = true)))
23607 tree identifier;
23608 tree width;
23609 tree late_attributes = NULL_TREE;
23611 if (named_bitfld)
23612 identifier = cp_parser_identifier (parser);
23613 else
23614 identifier = NULL_TREE;
23616 /* Look for attributes that apply to the bitfield. */
23617 attributes = cp_parser_attributes_opt (parser);
23619 /* Consume the `:' token. */
23620 cp_lexer_consume_token (parser->lexer);
23622 /* Get the width of the bitfield. */
23623 width = cp_parser_constant_expression (parser, false, NULL,
23624 cxx_dialect >= cxx11);
23626 /* In C++2A and as extension for C++11 and above we allow
23627 default member initializers for bit-fields. */
23628 initializer = NULL_TREE;
23629 if (cxx_dialect >= cxx11
23630 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23631 || cp_lexer_next_token_is (parser->lexer,
23632 CPP_OPEN_BRACE)))
23634 location_t loc
23635 = cp_lexer_peek_token (parser->lexer)->location;
23636 if (cxx_dialect < cxx2a
23637 && !in_system_header_at (loc)
23638 && identifier != NULL_TREE)
23639 pedwarn (loc, 0,
23640 "default member initializers for bit-fields "
23641 "only available with -std=c++2a or "
23642 "-std=gnu++2a");
23644 initializer = cp_parser_save_nsdmi (parser);
23645 if (identifier == NULL_TREE)
23647 error_at (loc, "default member initializer for "
23648 "unnamed bit-field");
23649 initializer = NULL_TREE;
23652 else
23654 /* Look for attributes that apply to the bitfield after
23655 the `:' token and width. This is where GCC used to
23656 parse attributes in the past, pedwarn if there is
23657 a std attribute. */
23658 if (cp_next_tokens_can_be_std_attribute_p (parser))
23659 pedwarn (input_location, OPT_Wpedantic,
23660 "ISO C++ allows bit-field attributes only "
23661 "before the %<:%> token");
23663 late_attributes = cp_parser_attributes_opt (parser);
23666 attributes = attr_chainon (attributes, late_attributes);
23668 /* Remember which attributes are prefix attributes and
23669 which are not. */
23670 first_attribute = attributes;
23671 /* Combine the attributes. */
23672 attributes = attr_chainon (prefix_attributes, attributes);
23674 /* Create the bitfield declaration. */
23675 decl = grokbitfield (identifier
23676 ? make_id_declarator (NULL_TREE,
23677 identifier,
23678 sfk_none)
23679 : NULL,
23680 &decl_specifiers,
23681 width, initializer,
23682 attributes);
23684 else
23686 cp_declarator *declarator;
23687 tree asm_specification;
23688 int ctor_dtor_or_conv_p;
23690 /* Parse the declarator. */
23691 declarator
23692 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23693 &ctor_dtor_or_conv_p,
23694 /*parenthesized_p=*/NULL,
23695 /*member_p=*/true,
23696 friend_p);
23698 /* If something went wrong parsing the declarator, make sure
23699 that we at least consume some tokens. */
23700 if (declarator == cp_error_declarator)
23702 /* Skip to the end of the statement. */
23703 cp_parser_skip_to_end_of_statement (parser);
23704 /* If the next token is not a semicolon, that is
23705 probably because we just skipped over the body of
23706 a function. So, we consume a semicolon if
23707 present, but do not issue an error message if it
23708 is not present. */
23709 if (cp_lexer_next_token_is (parser->lexer,
23710 CPP_SEMICOLON))
23711 cp_lexer_consume_token (parser->lexer);
23712 goto out;
23715 if (declares_class_or_enum & 2)
23716 cp_parser_check_for_definition_in_return_type
23717 (declarator, decl_specifiers.type,
23718 decl_specifiers.locations[ds_type_spec]);
23720 /* Look for an asm-specification. */
23721 asm_specification = cp_parser_asm_specification_opt (parser);
23722 /* Look for attributes that apply to the declaration. */
23723 attributes = cp_parser_attributes_opt (parser);
23724 /* Remember which attributes are prefix attributes and
23725 which are not. */
23726 first_attribute = attributes;
23727 /* Combine the attributes. */
23728 attributes = attr_chainon (prefix_attributes, attributes);
23730 /* If it's an `=', then we have a constant-initializer or a
23731 pure-specifier. It is not correct to parse the
23732 initializer before registering the member declaration
23733 since the member declaration should be in scope while
23734 its initializer is processed. However, the rest of the
23735 front end does not yet provide an interface that allows
23736 us to handle this correctly. */
23737 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23739 /* In [class.mem]:
23741 A pure-specifier shall be used only in the declaration of
23742 a virtual function.
23744 A member-declarator can contain a constant-initializer
23745 only if it declares a static member of integral or
23746 enumeration type.
23748 Therefore, if the DECLARATOR is for a function, we look
23749 for a pure-specifier; otherwise, we look for a
23750 constant-initializer. When we call `grokfield', it will
23751 perform more stringent semantics checks. */
23752 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23753 if (function_declarator_p (declarator)
23754 || (decl_specifiers.type
23755 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23756 && declarator->kind == cdk_id
23757 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23758 == FUNCTION_TYPE)))
23759 initializer = cp_parser_pure_specifier (parser);
23760 else if (decl_specifiers.storage_class != sc_static)
23761 initializer = cp_parser_save_nsdmi (parser);
23762 else if (cxx_dialect >= cxx11)
23764 bool nonconst;
23765 /* Don't require a constant rvalue in C++11, since we
23766 might want a reference constant. We'll enforce
23767 constancy later. */
23768 cp_lexer_consume_token (parser->lexer);
23769 /* Parse the initializer. */
23770 initializer = cp_parser_initializer_clause (parser,
23771 &nonconst);
23773 else
23774 /* Parse the initializer. */
23775 initializer = cp_parser_constant_initializer (parser);
23777 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23778 && !function_declarator_p (declarator))
23780 bool x;
23781 if (decl_specifiers.storage_class != sc_static)
23782 initializer = cp_parser_save_nsdmi (parser);
23783 else
23784 initializer = cp_parser_initializer (parser, &x, &x);
23786 /* Otherwise, there is no initializer. */
23787 else
23788 initializer = NULL_TREE;
23790 /* See if we are probably looking at a function
23791 definition. We are certainly not looking at a
23792 member-declarator. Calling `grokfield' has
23793 side-effects, so we must not do it unless we are sure
23794 that we are looking at a member-declarator. */
23795 if (cp_parser_token_starts_function_definition_p
23796 (cp_lexer_peek_token (parser->lexer)))
23798 /* The grammar does not allow a pure-specifier to be
23799 used when a member function is defined. (It is
23800 possible that this fact is an oversight in the
23801 standard, since a pure function may be defined
23802 outside of the class-specifier. */
23803 if (initializer && initializer_token_start)
23804 error_at (initializer_token_start->location,
23805 "pure-specifier on function-definition");
23806 decl = cp_parser_save_member_function_body (parser,
23807 &decl_specifiers,
23808 declarator,
23809 attributes);
23810 if (parser->fully_implicit_function_template_p)
23811 decl = finish_fully_implicit_template (parser, decl);
23812 /* If the member was not a friend, declare it here. */
23813 if (!friend_p)
23814 finish_member_declaration (decl);
23815 /* Peek at the next token. */
23816 token = cp_lexer_peek_token (parser->lexer);
23817 /* If the next token is a semicolon, consume it. */
23818 if (token->type == CPP_SEMICOLON)
23820 location_t semicolon_loc
23821 = cp_lexer_consume_token (parser->lexer)->location;
23822 gcc_rich_location richloc (semicolon_loc);
23823 richloc.add_fixit_remove ();
23824 warning_at (&richloc, OPT_Wextra_semi,
23825 "extra %<;%> after in-class "
23826 "function definition");
23828 goto out;
23830 else
23831 if (declarator->kind == cdk_function)
23832 declarator->id_loc = token->location;
23833 /* Create the declaration. */
23834 decl = grokfield (declarator, &decl_specifiers,
23835 initializer, /*init_const_expr_p=*/true,
23836 asm_specification, attributes);
23837 if (parser->fully_implicit_function_template_p)
23839 if (friend_p)
23840 finish_fully_implicit_template (parser, 0);
23841 else
23842 decl = finish_fully_implicit_template (parser, decl);
23846 cp_finalize_omp_declare_simd (parser, decl);
23847 cp_finalize_oacc_routine (parser, decl, false);
23849 /* Reset PREFIX_ATTRIBUTES. */
23850 if (attributes != error_mark_node)
23852 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23853 attributes = TREE_CHAIN (attributes);
23854 if (attributes)
23855 TREE_CHAIN (attributes) = NULL_TREE;
23858 /* If there is any qualification still in effect, clear it
23859 now; we will be starting fresh with the next declarator. */
23860 parser->scope = NULL_TREE;
23861 parser->qualifying_scope = NULL_TREE;
23862 parser->object_scope = NULL_TREE;
23863 /* If it's a `,', then there are more declarators. */
23864 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23866 cp_lexer_consume_token (parser->lexer);
23867 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23869 cp_token *token = cp_lexer_previous_token (parser->lexer);
23870 gcc_rich_location richloc (token->location);
23871 richloc.add_fixit_remove ();
23872 error_at (&richloc, "stray %<,%> at end of "
23873 "member declaration");
23876 /* If the next token isn't a `;', then we have a parse error. */
23877 else if (cp_lexer_next_token_is_not (parser->lexer,
23878 CPP_SEMICOLON))
23880 /* The next token might be a ways away from where the
23881 actual semicolon is missing. Find the previous token
23882 and use that for our error position. */
23883 cp_token *token = cp_lexer_previous_token (parser->lexer);
23884 gcc_rich_location richloc (token->location);
23885 richloc.add_fixit_insert_after (";");
23886 error_at (&richloc, "expected %<;%> at end of "
23887 "member declaration");
23889 /* Assume that the user meant to provide a semicolon. If
23890 we were to cp_parser_skip_to_end_of_statement, we might
23891 skip to a semicolon inside a member function definition
23892 and issue nonsensical error messages. */
23893 assume_semicolon = true;
23896 if (decl)
23898 /* Add DECL to the list of members. */
23899 if (!friend_p
23900 /* Explicitly include, eg, NSDMIs, for better error
23901 recovery (c++/58650). */
23902 || !DECL_DECLARES_FUNCTION_P (decl))
23903 finish_member_declaration (decl);
23905 if (TREE_CODE (decl) == FUNCTION_DECL)
23906 cp_parser_save_default_args (parser, decl);
23907 else if (TREE_CODE (decl) == FIELD_DECL
23908 && DECL_INITIAL (decl))
23909 /* Add DECL to the queue of NSDMI to be parsed later. */
23910 vec_safe_push (unparsed_nsdmis, decl);
23913 if (assume_semicolon)
23914 goto out;
23918 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23919 out:
23920 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23923 /* Parse a pure-specifier.
23925 pure-specifier:
23928 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23929 Otherwise, ERROR_MARK_NODE is returned. */
23931 static tree
23932 cp_parser_pure_specifier (cp_parser* parser)
23934 cp_token *token;
23936 /* Look for the `=' token. */
23937 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23938 return error_mark_node;
23939 /* Look for the `0' token. */
23940 token = cp_lexer_peek_token (parser->lexer);
23942 if (token->type == CPP_EOF
23943 || token->type == CPP_PRAGMA_EOL)
23944 return error_mark_node;
23946 cp_lexer_consume_token (parser->lexer);
23948 /* Accept = default or = delete in c++0x mode. */
23949 if (token->keyword == RID_DEFAULT
23950 || token->keyword == RID_DELETE)
23952 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23953 return token->u.value;
23956 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23957 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23959 cp_parser_error (parser,
23960 "invalid pure specifier (only %<= 0%> is allowed)");
23961 cp_parser_skip_to_end_of_statement (parser);
23962 return error_mark_node;
23964 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23966 error_at (token->location, "templates may not be %<virtual%>");
23967 return error_mark_node;
23970 return integer_zero_node;
23973 /* Parse a constant-initializer.
23975 constant-initializer:
23976 = constant-expression
23978 Returns a representation of the constant-expression. */
23980 static tree
23981 cp_parser_constant_initializer (cp_parser* parser)
23983 /* Look for the `=' token. */
23984 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23985 return error_mark_node;
23987 /* It is invalid to write:
23989 struct S { static const int i = { 7 }; };
23992 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23994 cp_parser_error (parser,
23995 "a brace-enclosed initializer is not allowed here");
23996 /* Consume the opening brace. */
23997 matching_braces braces;
23998 braces.consume_open (parser);
23999 /* Skip the initializer. */
24000 cp_parser_skip_to_closing_brace (parser);
24001 /* Look for the trailing `}'. */
24002 braces.require_close (parser);
24004 return error_mark_node;
24007 return cp_parser_constant_expression (parser);
24010 /* Derived classes [gram.class.derived] */
24012 /* Parse a base-clause.
24014 base-clause:
24015 : base-specifier-list
24017 base-specifier-list:
24018 base-specifier ... [opt]
24019 base-specifier-list , base-specifier ... [opt]
24021 Returns a TREE_LIST representing the base-classes, in the order in
24022 which they were declared. The representation of each node is as
24023 described by cp_parser_base_specifier.
24025 In the case that no bases are specified, this function will return
24026 NULL_TREE, not ERROR_MARK_NODE. */
24028 static tree
24029 cp_parser_base_clause (cp_parser* parser)
24031 tree bases = NULL_TREE;
24033 /* Look for the `:' that begins the list. */
24034 cp_parser_require (parser, CPP_COLON, RT_COLON);
24036 /* Scan the base-specifier-list. */
24037 while (true)
24039 cp_token *token;
24040 tree base;
24041 bool pack_expansion_p = false;
24043 /* Look for the base-specifier. */
24044 base = cp_parser_base_specifier (parser);
24045 /* Look for the (optional) ellipsis. */
24046 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24048 /* Consume the `...'. */
24049 cp_lexer_consume_token (parser->lexer);
24051 pack_expansion_p = true;
24054 /* Add BASE to the front of the list. */
24055 if (base && base != error_mark_node)
24057 if (pack_expansion_p)
24058 /* Make this a pack expansion type. */
24059 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24061 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24063 TREE_CHAIN (base) = bases;
24064 bases = base;
24067 /* Peek at the next token. */
24068 token = cp_lexer_peek_token (parser->lexer);
24069 /* If it's not a comma, then the list is complete. */
24070 if (token->type != CPP_COMMA)
24071 break;
24072 /* Consume the `,'. */
24073 cp_lexer_consume_token (parser->lexer);
24076 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24077 base class had a qualified name. However, the next name that
24078 appears is certainly not qualified. */
24079 parser->scope = NULL_TREE;
24080 parser->qualifying_scope = NULL_TREE;
24081 parser->object_scope = NULL_TREE;
24083 return nreverse (bases);
24086 /* Parse a base-specifier.
24088 base-specifier:
24089 :: [opt] nested-name-specifier [opt] class-name
24090 virtual access-specifier [opt] :: [opt] nested-name-specifier
24091 [opt] class-name
24092 access-specifier virtual [opt] :: [opt] nested-name-specifier
24093 [opt] class-name
24095 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24096 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24097 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24098 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24100 static tree
24101 cp_parser_base_specifier (cp_parser* parser)
24103 cp_token *token;
24104 bool done = false;
24105 bool virtual_p = false;
24106 bool duplicate_virtual_error_issued_p = false;
24107 bool duplicate_access_error_issued_p = false;
24108 bool class_scope_p, template_p;
24109 tree access = access_default_node;
24110 tree type;
24112 /* Process the optional `virtual' and `access-specifier'. */
24113 while (!done)
24115 /* Peek at the next token. */
24116 token = cp_lexer_peek_token (parser->lexer);
24117 /* Process `virtual'. */
24118 switch (token->keyword)
24120 case RID_VIRTUAL:
24121 /* If `virtual' appears more than once, issue an error. */
24122 if (virtual_p && !duplicate_virtual_error_issued_p)
24124 cp_parser_error (parser,
24125 "%<virtual%> specified more than once in base-specifier");
24126 duplicate_virtual_error_issued_p = true;
24129 virtual_p = true;
24131 /* Consume the `virtual' token. */
24132 cp_lexer_consume_token (parser->lexer);
24134 break;
24136 case RID_PUBLIC:
24137 case RID_PROTECTED:
24138 case RID_PRIVATE:
24139 /* If more than one access specifier appears, issue an
24140 error. */
24141 if (access != access_default_node
24142 && !duplicate_access_error_issued_p)
24144 cp_parser_error (parser,
24145 "more than one access specifier in base-specifier");
24146 duplicate_access_error_issued_p = true;
24149 access = ridpointers[(int) token->keyword];
24151 /* Consume the access-specifier. */
24152 cp_lexer_consume_token (parser->lexer);
24154 break;
24156 default:
24157 done = true;
24158 break;
24161 /* It is not uncommon to see programs mechanically, erroneously, use
24162 the 'typename' keyword to denote (dependent) qualified types
24163 as base classes. */
24164 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24166 token = cp_lexer_peek_token (parser->lexer);
24167 if (!processing_template_decl)
24168 error_at (token->location,
24169 "keyword %<typename%> not allowed outside of templates");
24170 else
24171 error_at (token->location,
24172 "keyword %<typename%> not allowed in this context "
24173 "(the base class is implicitly a type)");
24174 cp_lexer_consume_token (parser->lexer);
24177 /* Look for the optional `::' operator. */
24178 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24179 /* Look for the nested-name-specifier. The simplest way to
24180 implement:
24182 [temp.res]
24184 The keyword `typename' is not permitted in a base-specifier or
24185 mem-initializer; in these contexts a qualified name that
24186 depends on a template-parameter is implicitly assumed to be a
24187 type name.
24189 is to pretend that we have seen the `typename' keyword at this
24190 point. */
24191 cp_parser_nested_name_specifier_opt (parser,
24192 /*typename_keyword_p=*/true,
24193 /*check_dependency_p=*/true,
24194 /*type_p=*/true,
24195 /*is_declaration=*/true);
24196 /* If the base class is given by a qualified name, assume that names
24197 we see are type names or templates, as appropriate. */
24198 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24199 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24201 if (!parser->scope
24202 && cp_lexer_next_token_is_decltype (parser->lexer))
24203 /* DR 950 allows decltype as a base-specifier. */
24204 type = cp_parser_decltype (parser);
24205 else
24207 /* Otherwise, look for the class-name. */
24208 type = cp_parser_class_name (parser,
24209 class_scope_p,
24210 template_p,
24211 typename_type,
24212 /*check_dependency_p=*/true,
24213 /*class_head_p=*/false,
24214 /*is_declaration=*/true);
24215 type = TREE_TYPE (type);
24218 if (type == error_mark_node)
24219 return error_mark_node;
24221 return finish_base_specifier (type, access, virtual_p);
24224 /* Exception handling [gram.exception] */
24226 /* Parse an (optional) noexcept-specification.
24228 noexcept-specification:
24229 noexcept ( constant-expression ) [opt]
24231 If no noexcept-specification is present, returns NULL_TREE.
24232 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24233 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24234 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24235 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24236 in which case a boolean condition is returned instead. */
24238 static tree
24239 cp_parser_noexcept_specification_opt (cp_parser* parser,
24240 bool require_constexpr,
24241 bool* consumed_expr,
24242 bool return_cond)
24244 cp_token *token;
24245 const char *saved_message;
24247 /* Peek at the next token. */
24248 token = cp_lexer_peek_token (parser->lexer);
24250 /* Is it a noexcept-specification? */
24251 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24253 tree expr;
24254 cp_lexer_consume_token (parser->lexer);
24256 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24258 matching_parens parens;
24259 parens.consume_open (parser);
24261 if (require_constexpr)
24263 /* Types may not be defined in an exception-specification. */
24264 saved_message = parser->type_definition_forbidden_message;
24265 parser->type_definition_forbidden_message
24266 = G_("types may not be defined in an exception-specification");
24268 expr = cp_parser_constant_expression (parser);
24270 /* Restore the saved message. */
24271 parser->type_definition_forbidden_message = saved_message;
24273 else
24275 expr = cp_parser_expression (parser);
24276 *consumed_expr = true;
24279 parens.require_close (parser);
24281 else
24283 expr = boolean_true_node;
24284 if (!require_constexpr)
24285 *consumed_expr = false;
24288 /* We cannot build a noexcept-spec right away because this will check
24289 that expr is a constexpr. */
24290 if (!return_cond)
24291 return build_noexcept_spec (expr, tf_warning_or_error);
24292 else
24293 return expr;
24295 else
24296 return NULL_TREE;
24299 /* Parse an (optional) exception-specification.
24301 exception-specification:
24302 throw ( type-id-list [opt] )
24304 Returns a TREE_LIST representing the exception-specification. The
24305 TREE_VALUE of each node is a type. */
24307 static tree
24308 cp_parser_exception_specification_opt (cp_parser* parser)
24310 cp_token *token;
24311 tree type_id_list;
24312 const char *saved_message;
24314 /* Peek at the next token. */
24315 token = cp_lexer_peek_token (parser->lexer);
24317 /* Is it a noexcept-specification? */
24318 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24319 false);
24320 if (type_id_list != NULL_TREE)
24321 return type_id_list;
24323 /* If it's not `throw', then there's no exception-specification. */
24324 if (!cp_parser_is_keyword (token, RID_THROW))
24325 return NULL_TREE;
24327 location_t loc = token->location;
24329 /* Consume the `throw'. */
24330 cp_lexer_consume_token (parser->lexer);
24332 /* Look for the `('. */
24333 matching_parens parens;
24334 parens.require_open (parser);
24336 /* Peek at the next token. */
24337 token = cp_lexer_peek_token (parser->lexer);
24338 /* If it's not a `)', then there is a type-id-list. */
24339 if (token->type != CPP_CLOSE_PAREN)
24341 /* Types may not be defined in an exception-specification. */
24342 saved_message = parser->type_definition_forbidden_message;
24343 parser->type_definition_forbidden_message
24344 = G_("types may not be defined in an exception-specification");
24345 /* Parse the type-id-list. */
24346 type_id_list = cp_parser_type_id_list (parser);
24347 /* Restore the saved message. */
24348 parser->type_definition_forbidden_message = saved_message;
24350 if (cxx_dialect >= cxx17)
24352 error_at (loc, "ISO C++17 does not allow dynamic exception "
24353 "specifications");
24354 type_id_list = NULL_TREE;
24356 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24357 warning_at (loc, OPT_Wdeprecated,
24358 "dynamic exception specifications are deprecated in "
24359 "C++11");
24361 /* In C++17, throw() is equivalent to noexcept (true). throw()
24362 is deprecated in C++11 and above as well, but is still widely used,
24363 so don't warn about it yet. */
24364 else if (cxx_dialect >= cxx17)
24365 type_id_list = noexcept_true_spec;
24366 else
24367 type_id_list = empty_except_spec;
24369 /* Look for the `)'. */
24370 parens.require_close (parser);
24372 return type_id_list;
24375 /* Parse an (optional) type-id-list.
24377 type-id-list:
24378 type-id ... [opt]
24379 type-id-list , type-id ... [opt]
24381 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24382 in the order that the types were presented. */
24384 static tree
24385 cp_parser_type_id_list (cp_parser* parser)
24387 tree types = NULL_TREE;
24389 while (true)
24391 cp_token *token;
24392 tree type;
24394 token = cp_lexer_peek_token (parser->lexer);
24396 /* Get the next type-id. */
24397 type = cp_parser_type_id (parser);
24398 /* Check for invalid 'auto'. */
24399 if (flag_concepts && type_uses_auto (type))
24401 error_at (token->location,
24402 "invalid use of %<auto%> in exception-specification");
24403 type = error_mark_node;
24405 /* Parse the optional ellipsis. */
24406 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24408 /* Consume the `...'. */
24409 cp_lexer_consume_token (parser->lexer);
24411 /* Turn the type into a pack expansion expression. */
24412 type = make_pack_expansion (type);
24414 /* Add it to the list. */
24415 types = add_exception_specifier (types, type, /*complain=*/1);
24416 /* Peek at the next token. */
24417 token = cp_lexer_peek_token (parser->lexer);
24418 /* If it is not a `,', we are done. */
24419 if (token->type != CPP_COMMA)
24420 break;
24421 /* Consume the `,'. */
24422 cp_lexer_consume_token (parser->lexer);
24425 return nreverse (types);
24428 /* Parse a try-block.
24430 try-block:
24431 try compound-statement handler-seq */
24433 static tree
24434 cp_parser_try_block (cp_parser* parser)
24436 tree try_block;
24438 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24439 if (parser->in_function_body
24440 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24441 error ("%<try%> in %<constexpr%> function");
24443 try_block = begin_try_block ();
24444 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24445 finish_try_block (try_block);
24446 cp_parser_handler_seq (parser);
24447 finish_handler_sequence (try_block);
24449 return try_block;
24452 /* Parse a function-try-block.
24454 function-try-block:
24455 try ctor-initializer [opt] function-body handler-seq */
24457 static void
24458 cp_parser_function_try_block (cp_parser* parser)
24460 tree compound_stmt;
24461 tree try_block;
24463 /* Look for the `try' keyword. */
24464 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24465 return;
24466 /* Let the rest of the front end know where we are. */
24467 try_block = begin_function_try_block (&compound_stmt);
24468 /* Parse the function-body. */
24469 cp_parser_ctor_initializer_opt_and_function_body
24470 (parser, /*in_function_try_block=*/true);
24471 /* We're done with the `try' part. */
24472 finish_function_try_block (try_block);
24473 /* Parse the handlers. */
24474 cp_parser_handler_seq (parser);
24475 /* We're done with the handlers. */
24476 finish_function_handler_sequence (try_block, compound_stmt);
24479 /* Parse a handler-seq.
24481 handler-seq:
24482 handler handler-seq [opt] */
24484 static void
24485 cp_parser_handler_seq (cp_parser* parser)
24487 while (true)
24489 cp_token *token;
24491 /* Parse the handler. */
24492 cp_parser_handler (parser);
24493 /* Peek at the next token. */
24494 token = cp_lexer_peek_token (parser->lexer);
24495 /* If it's not `catch' then there are no more handlers. */
24496 if (!cp_parser_is_keyword (token, RID_CATCH))
24497 break;
24501 /* Parse a handler.
24503 handler:
24504 catch ( exception-declaration ) compound-statement */
24506 static void
24507 cp_parser_handler (cp_parser* parser)
24509 tree handler;
24510 tree declaration;
24512 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24513 handler = begin_handler ();
24514 matching_parens parens;
24515 parens.require_open (parser);
24516 declaration = cp_parser_exception_declaration (parser);
24517 finish_handler_parms (declaration, handler);
24518 parens.require_close (parser);
24519 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24520 finish_handler (handler);
24523 /* Parse an exception-declaration.
24525 exception-declaration:
24526 type-specifier-seq declarator
24527 type-specifier-seq abstract-declarator
24528 type-specifier-seq
24531 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24532 ellipsis variant is used. */
24534 static tree
24535 cp_parser_exception_declaration (cp_parser* parser)
24537 cp_decl_specifier_seq type_specifiers;
24538 cp_declarator *declarator;
24539 const char *saved_message;
24541 /* If it's an ellipsis, it's easy to handle. */
24542 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24544 /* Consume the `...' token. */
24545 cp_lexer_consume_token (parser->lexer);
24546 return NULL_TREE;
24549 /* Types may not be defined in exception-declarations. */
24550 saved_message = parser->type_definition_forbidden_message;
24551 parser->type_definition_forbidden_message
24552 = G_("types may not be defined in exception-declarations");
24554 /* Parse the type-specifier-seq. */
24555 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24556 /*is_trailing_return=*/false,
24557 &type_specifiers);
24558 /* If it's a `)', then there is no declarator. */
24559 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24560 declarator = NULL;
24561 else
24562 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24563 /*ctor_dtor_or_conv_p=*/NULL,
24564 /*parenthesized_p=*/NULL,
24565 /*member_p=*/false,
24566 /*friend_p=*/false);
24568 /* Restore the saved message. */
24569 parser->type_definition_forbidden_message = saved_message;
24571 if (!type_specifiers.any_specifiers_p)
24572 return error_mark_node;
24574 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24577 /* Parse a throw-expression.
24579 throw-expression:
24580 throw assignment-expression [opt]
24582 Returns a THROW_EXPR representing the throw-expression. */
24584 static tree
24585 cp_parser_throw_expression (cp_parser* parser)
24587 tree expression;
24588 cp_token* token;
24590 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24591 token = cp_lexer_peek_token (parser->lexer);
24592 /* Figure out whether or not there is an assignment-expression
24593 following the "throw" keyword. */
24594 if (token->type == CPP_COMMA
24595 || token->type == CPP_SEMICOLON
24596 || token->type == CPP_CLOSE_PAREN
24597 || token->type == CPP_CLOSE_SQUARE
24598 || token->type == CPP_CLOSE_BRACE
24599 || token->type == CPP_COLON)
24600 expression = NULL_TREE;
24601 else
24602 expression = cp_parser_assignment_expression (parser);
24604 return build_throw (expression);
24607 /* GNU Extensions */
24609 /* Parse an (optional) asm-specification.
24611 asm-specification:
24612 asm ( string-literal )
24614 If the asm-specification is present, returns a STRING_CST
24615 corresponding to the string-literal. Otherwise, returns
24616 NULL_TREE. */
24618 static tree
24619 cp_parser_asm_specification_opt (cp_parser* parser)
24621 cp_token *token;
24622 tree asm_specification;
24624 /* Peek at the next token. */
24625 token = cp_lexer_peek_token (parser->lexer);
24626 /* If the next token isn't the `asm' keyword, then there's no
24627 asm-specification. */
24628 if (!cp_parser_is_keyword (token, RID_ASM))
24629 return NULL_TREE;
24631 /* Consume the `asm' token. */
24632 cp_lexer_consume_token (parser->lexer);
24633 /* Look for the `('. */
24634 matching_parens parens;
24635 parens.require_open (parser);
24637 /* Look for the string-literal. */
24638 asm_specification = cp_parser_string_literal (parser, false, false);
24640 /* Look for the `)'. */
24641 parens.require_close (parser);
24643 return asm_specification;
24646 /* Parse an asm-operand-list.
24648 asm-operand-list:
24649 asm-operand
24650 asm-operand-list , asm-operand
24652 asm-operand:
24653 string-literal ( expression )
24654 [ string-literal ] string-literal ( expression )
24656 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24657 each node is the expression. The TREE_PURPOSE is itself a
24658 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24659 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24660 is a STRING_CST for the string literal before the parenthesis. Returns
24661 ERROR_MARK_NODE if any of the operands are invalid. */
24663 static tree
24664 cp_parser_asm_operand_list (cp_parser* parser)
24666 tree asm_operands = NULL_TREE;
24667 bool invalid_operands = false;
24669 while (true)
24671 tree string_literal;
24672 tree expression;
24673 tree name;
24675 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24677 /* Consume the `[' token. */
24678 cp_lexer_consume_token (parser->lexer);
24679 /* Read the operand name. */
24680 name = cp_parser_identifier (parser);
24681 if (name != error_mark_node)
24682 name = build_string (IDENTIFIER_LENGTH (name),
24683 IDENTIFIER_POINTER (name));
24684 /* Look for the closing `]'. */
24685 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24687 else
24688 name = NULL_TREE;
24689 /* Look for the string-literal. */
24690 string_literal = cp_parser_string_literal (parser, false, false);
24692 /* Look for the `('. */
24693 matching_parens parens;
24694 parens.require_open (parser);
24695 /* Parse the expression. */
24696 expression = cp_parser_expression (parser);
24697 /* Look for the `)'. */
24698 parens.require_close (parser);
24700 if (name == error_mark_node
24701 || string_literal == error_mark_node
24702 || expression == error_mark_node)
24703 invalid_operands = true;
24705 /* Add this operand to the list. */
24706 asm_operands = tree_cons (build_tree_list (name, string_literal),
24707 expression,
24708 asm_operands);
24709 /* If the next token is not a `,', there are no more
24710 operands. */
24711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24712 break;
24713 /* Consume the `,'. */
24714 cp_lexer_consume_token (parser->lexer);
24717 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24720 /* Parse an asm-clobber-list.
24722 asm-clobber-list:
24723 string-literal
24724 asm-clobber-list , string-literal
24726 Returns a TREE_LIST, indicating the clobbers in the order that they
24727 appeared. The TREE_VALUE of each node is a STRING_CST. */
24729 static tree
24730 cp_parser_asm_clobber_list (cp_parser* parser)
24732 tree clobbers = NULL_TREE;
24734 while (true)
24736 tree string_literal;
24738 /* Look for the string literal. */
24739 string_literal = cp_parser_string_literal (parser, false, false);
24740 /* Add it to the list. */
24741 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24742 /* If the next token is not a `,', then the list is
24743 complete. */
24744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24745 break;
24746 /* Consume the `,' token. */
24747 cp_lexer_consume_token (parser->lexer);
24750 return clobbers;
24753 /* Parse an asm-label-list.
24755 asm-label-list:
24756 identifier
24757 asm-label-list , identifier
24759 Returns a TREE_LIST, indicating the labels in the order that they
24760 appeared. The TREE_VALUE of each node is a label. */
24762 static tree
24763 cp_parser_asm_label_list (cp_parser* parser)
24765 tree labels = NULL_TREE;
24767 while (true)
24769 tree identifier, label, name;
24771 /* Look for the identifier. */
24772 identifier = cp_parser_identifier (parser);
24773 if (!error_operand_p (identifier))
24775 label = lookup_label (identifier);
24776 if (TREE_CODE (label) == LABEL_DECL)
24778 TREE_USED (label) = 1;
24779 check_goto (label);
24780 name = build_string (IDENTIFIER_LENGTH (identifier),
24781 IDENTIFIER_POINTER (identifier));
24782 labels = tree_cons (name, label, labels);
24785 /* If the next token is not a `,', then the list is
24786 complete. */
24787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24788 break;
24789 /* Consume the `,' token. */
24790 cp_lexer_consume_token (parser->lexer);
24793 return nreverse (labels);
24796 /* Return TRUE iff the next tokens in the stream are possibly the
24797 beginning of a GNU extension attribute. */
24799 static bool
24800 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24802 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24805 /* Return TRUE iff the next tokens in the stream are possibly the
24806 beginning of a standard C++-11 attribute specifier. */
24808 static bool
24809 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24811 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24814 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24815 beginning of a standard C++-11 attribute specifier. */
24817 static bool
24818 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24820 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24822 return (cxx_dialect >= cxx11
24823 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24824 || (token->type == CPP_OPEN_SQUARE
24825 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24826 && token->type == CPP_OPEN_SQUARE)));
24829 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24830 beginning of a GNU extension attribute. */
24832 static bool
24833 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24835 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24837 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24840 /* Return true iff the next tokens can be the beginning of either a
24841 GNU attribute list, or a standard C++11 attribute sequence. */
24843 static bool
24844 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24846 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24847 || cp_next_tokens_can_be_std_attribute_p (parser));
24850 /* Return true iff the next Nth tokens can be the beginning of either
24851 a GNU attribute list, or a standard C++11 attribute sequence. */
24853 static bool
24854 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24856 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24857 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24860 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24861 of GNU attributes, or return NULL. */
24863 static tree
24864 cp_parser_attributes_opt (cp_parser *parser)
24866 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24867 return cp_parser_gnu_attributes_opt (parser);
24868 return cp_parser_std_attribute_spec_seq (parser);
24871 /* Parse an (optional) series of attributes.
24873 attributes:
24874 attributes attribute
24876 attribute:
24877 __attribute__ (( attribute-list [opt] ))
24879 The return value is as for cp_parser_gnu_attribute_list. */
24881 static tree
24882 cp_parser_gnu_attributes_opt (cp_parser* parser)
24884 tree attributes = NULL_TREE;
24886 while (true)
24888 cp_token *token;
24889 tree attribute_list;
24890 bool ok = true;
24892 /* Peek at the next token. */
24893 token = cp_lexer_peek_token (parser->lexer);
24894 /* If it's not `__attribute__', then we're done. */
24895 if (token->keyword != RID_ATTRIBUTE)
24896 break;
24898 /* Consume the `__attribute__' keyword. */
24899 cp_lexer_consume_token (parser->lexer);
24900 /* Look for the two `(' tokens. */
24901 matching_parens outer_parens;
24902 outer_parens.require_open (parser);
24903 matching_parens inner_parens;
24904 inner_parens.require_open (parser);
24906 /* Peek at the next token. */
24907 token = cp_lexer_peek_token (parser->lexer);
24908 if (token->type != CPP_CLOSE_PAREN)
24909 /* Parse the attribute-list. */
24910 attribute_list = cp_parser_gnu_attribute_list (parser);
24911 else
24912 /* If the next token is a `)', then there is no attribute
24913 list. */
24914 attribute_list = NULL;
24916 /* Look for the two `)' tokens. */
24917 if (!inner_parens.require_close (parser))
24918 ok = false;
24919 if (!outer_parens.require_close (parser))
24920 ok = false;
24921 if (!ok)
24922 cp_parser_skip_to_end_of_statement (parser);
24924 /* Add these new attributes to the list. */
24925 attributes = attr_chainon (attributes, attribute_list);
24928 return attributes;
24931 /* Parse a GNU attribute-list.
24933 attribute-list:
24934 attribute
24935 attribute-list , attribute
24937 attribute:
24938 identifier
24939 identifier ( identifier )
24940 identifier ( identifier , expression-list )
24941 identifier ( expression-list )
24943 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24944 to an attribute. The TREE_PURPOSE of each node is the identifier
24945 indicating which attribute is in use. The TREE_VALUE represents
24946 the arguments, if any. */
24948 static tree
24949 cp_parser_gnu_attribute_list (cp_parser* parser)
24951 tree attribute_list = NULL_TREE;
24952 bool save_translate_strings_p = parser->translate_strings_p;
24954 parser->translate_strings_p = false;
24955 while (true)
24957 cp_token *token;
24958 tree identifier;
24959 tree attribute;
24961 /* Look for the identifier. We also allow keywords here; for
24962 example `__attribute__ ((const))' is legal. */
24963 token = cp_lexer_peek_token (parser->lexer);
24964 if (token->type == CPP_NAME
24965 || token->type == CPP_KEYWORD)
24967 tree arguments = NULL_TREE;
24969 /* Consume the token, but save it since we need it for the
24970 SIMD enabled function parsing. */
24971 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24973 /* Save away the identifier that indicates which attribute
24974 this is. */
24975 identifier = (token->type == CPP_KEYWORD)
24976 /* For keywords, use the canonical spelling, not the
24977 parsed identifier. */
24978 ? ridpointers[(int) token->keyword]
24979 : id_token->u.value;
24981 identifier = canonicalize_attr_name (identifier);
24982 attribute = build_tree_list (identifier, NULL_TREE);
24984 /* Peek at the next token. */
24985 token = cp_lexer_peek_token (parser->lexer);
24986 /* If it's an `(', then parse the attribute arguments. */
24987 if (token->type == CPP_OPEN_PAREN)
24989 vec<tree, va_gc> *vec;
24990 int attr_flag = (attribute_takes_identifier_p (identifier)
24991 ? id_attr : normal_attr);
24992 vec = cp_parser_parenthesized_expression_list
24993 (parser, attr_flag, /*cast_p=*/false,
24994 /*allow_expansion_p=*/false,
24995 /*non_constant_p=*/NULL);
24996 if (vec == NULL)
24997 arguments = error_mark_node;
24998 else
25000 arguments = build_tree_list_vec (vec);
25001 release_tree_vector (vec);
25003 /* Save the arguments away. */
25004 TREE_VALUE (attribute) = arguments;
25007 if (arguments != error_mark_node)
25009 /* Add this attribute to the list. */
25010 TREE_CHAIN (attribute) = attribute_list;
25011 attribute_list = attribute;
25014 token = cp_lexer_peek_token (parser->lexer);
25016 /* Now, look for more attributes. If the next token isn't a
25017 `,', we're done. */
25018 if (token->type != CPP_COMMA)
25019 break;
25021 /* Consume the comma and keep going. */
25022 cp_lexer_consume_token (parser->lexer);
25024 parser->translate_strings_p = save_translate_strings_p;
25026 /* We built up the list in reverse order. */
25027 return nreverse (attribute_list);
25030 /* Parse a standard C++11 attribute.
25032 The returned representation is a TREE_LIST which TREE_PURPOSE is
25033 the scoped name of the attribute, and the TREE_VALUE is its
25034 arguments list.
25036 Note that the scoped name of the attribute is itself a TREE_LIST
25037 which TREE_PURPOSE is the namespace of the attribute, and
25038 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25039 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25040 and which TREE_PURPOSE is directly the attribute name.
25042 Clients of the attribute code should use get_attribute_namespace
25043 and get_attribute_name to get the actual namespace and name of
25044 attributes, regardless of their being GNU or C++11 attributes.
25046 attribute:
25047 attribute-token attribute-argument-clause [opt]
25049 attribute-token:
25050 identifier
25051 attribute-scoped-token
25053 attribute-scoped-token:
25054 attribute-namespace :: identifier
25056 attribute-namespace:
25057 identifier
25059 attribute-argument-clause:
25060 ( balanced-token-seq )
25062 balanced-token-seq:
25063 balanced-token [opt]
25064 balanced-token-seq balanced-token
25066 balanced-token:
25067 ( balanced-token-seq )
25068 [ balanced-token-seq ]
25069 { balanced-token-seq }. */
25071 static tree
25072 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25074 tree attribute, attr_id = NULL_TREE, arguments;
25075 cp_token *token;
25077 /* First, parse name of the attribute, a.k.a attribute-token. */
25079 token = cp_lexer_peek_token (parser->lexer);
25080 if (token->type == CPP_NAME)
25081 attr_id = token->u.value;
25082 else if (token->type == CPP_KEYWORD)
25083 attr_id = ridpointers[(int) token->keyword];
25084 else if (token->flags & NAMED_OP)
25085 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25087 if (attr_id == NULL_TREE)
25088 return NULL_TREE;
25090 cp_lexer_consume_token (parser->lexer);
25092 token = cp_lexer_peek_token (parser->lexer);
25093 if (token->type == CPP_SCOPE)
25095 /* We are seeing a scoped attribute token. */
25097 cp_lexer_consume_token (parser->lexer);
25098 if (attr_ns)
25099 error_at (token->location, "attribute using prefix used together "
25100 "with scoped attribute token");
25101 attr_ns = attr_id;
25103 token = cp_lexer_consume_token (parser->lexer);
25104 if (token->type == CPP_NAME)
25105 attr_id = token->u.value;
25106 else if (token->type == CPP_KEYWORD)
25107 attr_id = ridpointers[(int) token->keyword];
25108 else if (token->flags & NAMED_OP)
25109 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25110 else
25112 error_at (token->location,
25113 "expected an identifier for the attribute name");
25114 return error_mark_node;
25117 attr_id = canonicalize_attr_name (attr_id);
25118 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25119 NULL_TREE);
25120 token = cp_lexer_peek_token (parser->lexer);
25122 else if (attr_ns)
25123 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25124 NULL_TREE);
25125 else
25127 attr_id = canonicalize_attr_name (attr_id);
25128 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25129 NULL_TREE);
25130 /* C++11 noreturn attribute is equivalent to GNU's. */
25131 if (is_attribute_p ("noreturn", attr_id))
25132 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25133 /* C++14 deprecated attribute is equivalent to GNU's. */
25134 else if (is_attribute_p ("deprecated", attr_id))
25135 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25136 /* C++17 fallthrough attribute is equivalent to GNU's. */
25137 else if (is_attribute_p ("fallthrough", attr_id))
25138 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25139 /* Transactional Memory TS optimize_for_synchronized attribute is
25140 equivalent to GNU transaction_callable. */
25141 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25142 TREE_PURPOSE (attribute)
25143 = get_identifier ("transaction_callable");
25144 /* Transactional Memory attributes are GNU attributes. */
25145 else if (tm_attr_to_mask (attr_id))
25146 TREE_PURPOSE (attribute) = attr_id;
25149 /* Now parse the optional argument clause of the attribute. */
25151 if (token->type != CPP_OPEN_PAREN)
25152 return attribute;
25155 vec<tree, va_gc> *vec;
25156 int attr_flag = normal_attr;
25158 if (attr_ns == get_identifier ("gnu")
25159 && attribute_takes_identifier_p (attr_id))
25160 /* A GNU attribute that takes an identifier in parameter. */
25161 attr_flag = id_attr;
25163 vec = cp_parser_parenthesized_expression_list
25164 (parser, attr_flag, /*cast_p=*/false,
25165 /*allow_expansion_p=*/true,
25166 /*non_constant_p=*/NULL);
25167 if (vec == NULL)
25168 arguments = error_mark_node;
25169 else
25171 arguments = build_tree_list_vec (vec);
25172 release_tree_vector (vec);
25175 if (arguments == error_mark_node)
25176 attribute = error_mark_node;
25177 else
25178 TREE_VALUE (attribute) = arguments;
25181 return attribute;
25184 /* Check that the attribute ATTRIBUTE appears at most once in the
25185 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25186 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25187 isn't implemented yet in GCC. */
25189 static void
25190 cp_parser_check_std_attribute (tree attributes, tree attribute)
25192 if (attributes)
25194 tree name = get_attribute_name (attribute);
25195 if (is_attribute_p ("noreturn", name)
25196 && lookup_attribute ("noreturn", attributes))
25197 error ("attribute %<noreturn%> can appear at most once "
25198 "in an attribute-list");
25199 else if (is_attribute_p ("deprecated", name)
25200 && lookup_attribute ("deprecated", attributes))
25201 error ("attribute %<deprecated%> can appear at most once "
25202 "in an attribute-list");
25206 /* Parse a list of standard C++-11 attributes.
25208 attribute-list:
25209 attribute [opt]
25210 attribute-list , attribute[opt]
25211 attribute ...
25212 attribute-list , attribute ...
25215 static tree
25216 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25218 tree attributes = NULL_TREE, attribute = NULL_TREE;
25219 cp_token *token = NULL;
25221 while (true)
25223 attribute = cp_parser_std_attribute (parser, attr_ns);
25224 if (attribute == error_mark_node)
25225 break;
25226 if (attribute != NULL_TREE)
25228 cp_parser_check_std_attribute (attributes, attribute);
25229 TREE_CHAIN (attribute) = attributes;
25230 attributes = attribute;
25232 token = cp_lexer_peek_token (parser->lexer);
25233 if (token->type == CPP_ELLIPSIS)
25235 cp_lexer_consume_token (parser->lexer);
25236 if (attribute == NULL_TREE)
25237 error_at (token->location,
25238 "expected attribute before %<...%>");
25239 else
25241 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25242 if (pack == error_mark_node)
25243 return error_mark_node;
25244 TREE_VALUE (attribute) = pack;
25246 token = cp_lexer_peek_token (parser->lexer);
25248 if (token->type != CPP_COMMA)
25249 break;
25250 cp_lexer_consume_token (parser->lexer);
25252 attributes = nreverse (attributes);
25253 return attributes;
25256 /* Parse a standard C++-11 attribute specifier.
25258 attribute-specifier:
25259 [ [ attribute-using-prefix [opt] attribute-list ] ]
25260 alignment-specifier
25262 attribute-using-prefix:
25263 using attribute-namespace :
25265 alignment-specifier:
25266 alignas ( type-id ... [opt] )
25267 alignas ( alignment-expression ... [opt] ). */
25269 static tree
25270 cp_parser_std_attribute_spec (cp_parser *parser)
25272 tree attributes = NULL_TREE;
25273 cp_token *token = cp_lexer_peek_token (parser->lexer);
25275 if (token->type == CPP_OPEN_SQUARE
25276 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25278 tree attr_ns = NULL_TREE;
25280 cp_lexer_consume_token (parser->lexer);
25281 cp_lexer_consume_token (parser->lexer);
25283 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25285 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25286 if (token->type == CPP_NAME)
25287 attr_ns = token->u.value;
25288 else if (token->type == CPP_KEYWORD)
25289 attr_ns = ridpointers[(int) token->keyword];
25290 else if (token->flags & NAMED_OP)
25291 attr_ns = get_identifier (cpp_type2name (token->type,
25292 token->flags));
25293 if (attr_ns
25294 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25296 if (cxx_dialect < cxx17
25297 && !in_system_header_at (input_location))
25298 pedwarn (input_location, 0,
25299 "attribute using prefix only available "
25300 "with -std=c++17 or -std=gnu++17");
25302 cp_lexer_consume_token (parser->lexer);
25303 cp_lexer_consume_token (parser->lexer);
25304 cp_lexer_consume_token (parser->lexer);
25306 else
25307 attr_ns = NULL_TREE;
25310 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25312 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25313 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25314 cp_parser_skip_to_end_of_statement (parser);
25315 else
25316 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25317 when we are sure that we have actually parsed them. */
25318 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25320 else
25322 tree alignas_expr;
25324 /* Look for an alignment-specifier. */
25326 token = cp_lexer_peek_token (parser->lexer);
25328 if (token->type != CPP_KEYWORD
25329 || token->keyword != RID_ALIGNAS)
25330 return NULL_TREE;
25332 cp_lexer_consume_token (parser->lexer);
25333 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25335 matching_parens parens;
25336 if (!parens.require_open (parser))
25337 return error_mark_node;
25339 cp_parser_parse_tentatively (parser);
25340 alignas_expr = cp_parser_type_id (parser);
25342 if (!cp_parser_parse_definitely (parser))
25344 alignas_expr = cp_parser_assignment_expression (parser);
25345 if (alignas_expr == error_mark_node)
25346 cp_parser_skip_to_end_of_statement (parser);
25347 if (alignas_expr == NULL_TREE
25348 || alignas_expr == error_mark_node)
25349 return alignas_expr;
25352 alignas_expr = cxx_alignas_expr (alignas_expr);
25353 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25355 /* Handle alignas (pack...). */
25356 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25358 cp_lexer_consume_token (parser->lexer);
25359 alignas_expr = make_pack_expansion (alignas_expr);
25362 /* Something went wrong, so don't build the attribute. */
25363 if (alignas_expr == error_mark_node)
25364 return error_mark_node;
25366 if (!parens.require_close (parser))
25367 return error_mark_node;
25369 /* Build the C++-11 representation of an 'aligned'
25370 attribute. */
25371 attributes =
25372 build_tree_list (build_tree_list (get_identifier ("gnu"),
25373 get_identifier ("aligned")),
25374 alignas_expr);
25377 return attributes;
25380 /* Parse a standard C++-11 attribute-specifier-seq.
25382 attribute-specifier-seq:
25383 attribute-specifier-seq [opt] attribute-specifier
25386 static tree
25387 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25389 tree attr_specs = NULL_TREE;
25390 tree attr_last = NULL_TREE;
25392 while (true)
25394 tree attr_spec = cp_parser_std_attribute_spec (parser);
25395 if (attr_spec == NULL_TREE)
25396 break;
25397 if (attr_spec == error_mark_node)
25398 return error_mark_node;
25400 if (attr_last)
25401 TREE_CHAIN (attr_last) = attr_spec;
25402 else
25403 attr_specs = attr_last = attr_spec;
25404 attr_last = tree_last (attr_last);
25407 return attr_specs;
25410 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25411 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25412 current value of the PEDANTIC flag, regardless of whether or not
25413 the `__extension__' keyword is present. The caller is responsible
25414 for restoring the value of the PEDANTIC flag. */
25416 static bool
25417 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25419 /* Save the old value of the PEDANTIC flag. */
25420 *saved_pedantic = pedantic;
25422 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25424 /* Consume the `__extension__' token. */
25425 cp_lexer_consume_token (parser->lexer);
25426 /* We're not being pedantic while the `__extension__' keyword is
25427 in effect. */
25428 pedantic = 0;
25430 return true;
25433 return false;
25436 /* Parse a label declaration.
25438 label-declaration:
25439 __label__ label-declarator-seq ;
25441 label-declarator-seq:
25442 identifier , label-declarator-seq
25443 identifier */
25445 static void
25446 cp_parser_label_declaration (cp_parser* parser)
25448 /* Look for the `__label__' keyword. */
25449 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25451 while (true)
25453 tree identifier;
25455 /* Look for an identifier. */
25456 identifier = cp_parser_identifier (parser);
25457 /* If we failed, stop. */
25458 if (identifier == error_mark_node)
25459 break;
25460 /* Declare it as a label. */
25461 finish_label_decl (identifier);
25462 /* If the next token is a `;', stop. */
25463 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25464 break;
25465 /* Look for the `,' separating the label declarations. */
25466 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25469 /* Look for the final `;'. */
25470 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25473 // -------------------------------------------------------------------------- //
25474 // Requires Clause
25476 // Parse a requires clause.
25478 // requires-clause:
25479 // 'requires' logical-or-expression
25481 // The required logical-or-expression must be a constant expression. Note
25482 // that we don't check that the expression is constepxr here. We defer until
25483 // we analyze constraints and then, we only check atomic constraints.
25484 static tree
25485 cp_parser_requires_clause (cp_parser *parser)
25487 // Parse the requires clause so that it is not automatically folded.
25488 ++processing_template_decl;
25489 tree expr = cp_parser_binary_expression (parser, false, false,
25490 PREC_NOT_OPERATOR, NULL);
25491 if (check_for_bare_parameter_packs (expr))
25492 expr = error_mark_node;
25493 --processing_template_decl;
25494 return expr;
25497 // Optionally parse a requires clause:
25498 static tree
25499 cp_parser_requires_clause_opt (cp_parser *parser)
25501 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25502 if (tok->keyword != RID_REQUIRES)
25504 if (!flag_concepts && tok->type == CPP_NAME
25505 && tok->u.value == ridpointers[RID_REQUIRES])
25507 error_at (cp_lexer_peek_token (parser->lexer)->location,
25508 "%<requires%> only available with -fconcepts");
25509 /* Parse and discard the requires-clause. */
25510 cp_lexer_consume_token (parser->lexer);
25511 cp_parser_requires_clause (parser);
25513 return NULL_TREE;
25515 cp_lexer_consume_token (parser->lexer);
25516 return cp_parser_requires_clause (parser);
25520 /*---------------------------------------------------------------------------
25521 Requires expressions
25522 ---------------------------------------------------------------------------*/
25524 /* Parse a requires expression
25526 requirement-expression:
25527 'requires' requirement-parameter-list [opt] requirement-body */
25528 static tree
25529 cp_parser_requires_expression (cp_parser *parser)
25531 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25532 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25534 /* A requires-expression shall appear only within a concept
25535 definition or a requires-clause.
25537 TODO: Implement this diagnostic correctly. */
25538 if (!processing_template_decl)
25540 error_at (loc, "a requires expression cannot appear outside a template");
25541 cp_parser_skip_to_end_of_statement (parser);
25542 return error_mark_node;
25545 tree parms, reqs;
25547 /* Local parameters are delared as variables within the scope
25548 of the expression. They are not visible past the end of
25549 the expression. Expressions within the requires-expression
25550 are unevaluated. */
25551 struct scope_sentinel
25553 scope_sentinel ()
25555 ++cp_unevaluated_operand;
25556 begin_scope (sk_block, NULL_TREE);
25559 ~scope_sentinel ()
25561 pop_bindings_and_leave_scope ();
25562 --cp_unevaluated_operand;
25564 } s;
25566 /* Parse the optional parameter list. */
25567 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25569 parms = cp_parser_requirement_parameter_list (parser);
25570 if (parms == error_mark_node)
25571 return error_mark_node;
25573 else
25574 parms = NULL_TREE;
25576 /* Parse the requirement body. */
25577 reqs = cp_parser_requirement_body (parser);
25578 if (reqs == error_mark_node)
25579 return error_mark_node;
25582 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25583 the parm chain. */
25584 grokparms (parms, &parms);
25585 return finish_requires_expr (parms, reqs);
25588 /* Parse a parameterized requirement.
25590 requirement-parameter-list:
25591 '(' parameter-declaration-clause ')' */
25592 static tree
25593 cp_parser_requirement_parameter_list (cp_parser *parser)
25595 matching_parens parens;
25596 if (!parens.require_open (parser))
25597 return error_mark_node;
25599 tree parms = cp_parser_parameter_declaration_clause (parser);
25601 if (!parens.require_close (parser))
25602 return error_mark_node;
25604 return parms;
25607 /* Parse the body of a requirement.
25609 requirement-body:
25610 '{' requirement-list '}' */
25611 static tree
25612 cp_parser_requirement_body (cp_parser *parser)
25614 matching_braces braces;
25615 if (!braces.require_open (parser))
25616 return error_mark_node;
25618 tree reqs = cp_parser_requirement_list (parser);
25620 if (!braces.require_close (parser))
25621 return error_mark_node;
25623 return reqs;
25626 /* Parse a list of requirements.
25628 requirement-list:
25629 requirement
25630 requirement-list ';' requirement[opt] */
25631 static tree
25632 cp_parser_requirement_list (cp_parser *parser)
25634 tree result = NULL_TREE;
25635 while (true)
25637 tree req = cp_parser_requirement (parser);
25638 if (req == error_mark_node)
25639 return error_mark_node;
25641 result = tree_cons (NULL_TREE, req, result);
25643 /* If we see a semi-colon, consume it. */
25644 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25645 cp_lexer_consume_token (parser->lexer);
25647 /* Stop processing at the end of the list. */
25648 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25649 break;
25652 /* Reverse the order of requirements so they are analyzed in
25653 declaration order. */
25654 return nreverse (result);
25657 /* Parse a syntactic requirement or type requirement.
25659 requirement:
25660 simple-requirement
25661 compound-requirement
25662 type-requirement
25663 nested-requirement */
25664 static tree
25665 cp_parser_requirement (cp_parser *parser)
25667 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25668 return cp_parser_compound_requirement (parser);
25669 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25670 return cp_parser_type_requirement (parser);
25671 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25672 return cp_parser_nested_requirement (parser);
25673 else
25674 return cp_parser_simple_requirement (parser);
25677 /* Parse a simple requirement.
25679 simple-requirement:
25680 expression ';' */
25681 static tree
25682 cp_parser_simple_requirement (cp_parser *parser)
25684 tree expr = cp_parser_expression (parser, NULL, false, false);
25685 if (!expr || expr == error_mark_node)
25686 return error_mark_node;
25688 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25689 return error_mark_node;
25691 return finish_simple_requirement (expr);
25694 /* Parse a type requirement
25696 type-requirement
25697 nested-name-specifier [opt] required-type-name ';'
25699 required-type-name:
25700 type-name
25701 'template' [opt] simple-template-id */
25702 static tree
25703 cp_parser_type_requirement (cp_parser *parser)
25705 cp_lexer_consume_token (parser->lexer);
25707 // Save the scope before parsing name specifiers.
25708 tree saved_scope = parser->scope;
25709 tree saved_object_scope = parser->object_scope;
25710 tree saved_qualifying_scope = parser->qualifying_scope;
25711 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25712 cp_parser_nested_name_specifier_opt (parser,
25713 /*typename_keyword_p=*/true,
25714 /*check_dependency_p=*/false,
25715 /*type_p=*/true,
25716 /*is_declaration=*/false);
25718 tree type;
25719 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25721 cp_lexer_consume_token (parser->lexer);
25722 type = cp_parser_template_id (parser,
25723 /*template_keyword_p=*/true,
25724 /*check_dependency=*/false,
25725 /*tag_type=*/none_type,
25726 /*is_declaration=*/false);
25727 type = make_typename_type (parser->scope, type, typename_type,
25728 /*complain=*/tf_error);
25730 else
25731 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25733 if (TREE_CODE (type) == TYPE_DECL)
25734 type = TREE_TYPE (type);
25736 parser->scope = saved_scope;
25737 parser->object_scope = saved_object_scope;
25738 parser->qualifying_scope = saved_qualifying_scope;
25740 if (type == error_mark_node)
25741 cp_parser_skip_to_end_of_statement (parser);
25743 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25744 return error_mark_node;
25745 if (type == error_mark_node)
25746 return error_mark_node;
25748 return finish_type_requirement (type);
25751 /* Parse a compound requirement
25753 compound-requirement:
25754 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25755 static tree
25756 cp_parser_compound_requirement (cp_parser *parser)
25758 /* Parse an expression enclosed in '{ }'s. */
25759 matching_braces braces;
25760 if (!braces.require_open (parser))
25761 return error_mark_node;
25763 tree expr = cp_parser_expression (parser, NULL, false, false);
25764 if (!expr || expr == error_mark_node)
25765 return error_mark_node;
25767 if (!braces.require_close (parser))
25768 return error_mark_node;
25770 /* Parse the optional noexcept. */
25771 bool noexcept_p = false;
25772 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25774 cp_lexer_consume_token (parser->lexer);
25775 noexcept_p = true;
25778 /* Parse the optional trailing return type. */
25779 tree type = NULL_TREE;
25780 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25782 cp_lexer_consume_token (parser->lexer);
25783 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25784 parser->in_result_type_constraint_p = true;
25785 type = cp_parser_trailing_type_id (parser);
25786 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25787 if (type == error_mark_node)
25788 return error_mark_node;
25791 return finish_compound_requirement (expr, type, noexcept_p);
25794 /* Parse a nested requirement. This is the same as a requires clause.
25796 nested-requirement:
25797 requires-clause */
25798 static tree
25799 cp_parser_nested_requirement (cp_parser *parser)
25801 cp_lexer_consume_token (parser->lexer);
25802 tree req = cp_parser_requires_clause (parser);
25803 if (req == error_mark_node)
25804 return error_mark_node;
25805 return finish_nested_requirement (req);
25808 /* Support Functions */
25810 /* Return the appropriate prefer_type argument for lookup_name_real based on
25811 tag_type and template_mem_access. */
25813 static inline int
25814 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25816 /* DR 141: When looking in the current enclosing context for a template-name
25817 after -> or ., only consider class templates. */
25818 if (template_mem_access)
25819 return 2;
25820 switch (tag_type)
25822 case none_type: return 0; // No preference.
25823 case scope_type: return 1; // Type or namespace.
25824 default: return 2; // Type only.
25828 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25829 NAME should have one of the representations used for an
25830 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25831 is returned. If PARSER->SCOPE is a dependent type, then a
25832 SCOPE_REF is returned.
25834 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25835 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25836 was formed. Abstractly, such entities should not be passed to this
25837 function, because they do not need to be looked up, but it is
25838 simpler to check for this special case here, rather than at the
25839 call-sites.
25841 In cases not explicitly covered above, this function returns a
25842 DECL, OVERLOAD, or baselink representing the result of the lookup.
25843 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25844 is returned.
25846 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25847 (e.g., "struct") that was used. In that case bindings that do not
25848 refer to types are ignored.
25850 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25851 ignored.
25853 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25854 are ignored.
25856 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25857 types.
25859 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25860 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25861 NULL_TREE otherwise. */
25863 static cp_expr
25864 cp_parser_lookup_name (cp_parser *parser, tree name,
25865 enum tag_types tag_type,
25866 bool is_template,
25867 bool is_namespace,
25868 bool check_dependency,
25869 tree *ambiguous_decls,
25870 location_t name_location)
25872 tree decl;
25873 tree object_type = parser->context->object_type;
25875 /* Assume that the lookup will be unambiguous. */
25876 if (ambiguous_decls)
25877 *ambiguous_decls = NULL_TREE;
25879 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25880 no longer valid. Note that if we are parsing tentatively, and
25881 the parse fails, OBJECT_TYPE will be automatically restored. */
25882 parser->context->object_type = NULL_TREE;
25884 if (name == error_mark_node)
25885 return error_mark_node;
25887 /* A template-id has already been resolved; there is no lookup to
25888 do. */
25889 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25890 return name;
25891 if (BASELINK_P (name))
25893 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25894 == TEMPLATE_ID_EXPR);
25895 return name;
25898 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25899 it should already have been checked to make sure that the name
25900 used matches the type being destroyed. */
25901 if (TREE_CODE (name) == BIT_NOT_EXPR)
25903 tree type;
25905 /* Figure out to which type this destructor applies. */
25906 if (parser->scope)
25907 type = parser->scope;
25908 else if (object_type)
25909 type = object_type;
25910 else
25911 type = current_class_type;
25912 /* If that's not a class type, there is no destructor. */
25913 if (!type || !CLASS_TYPE_P (type))
25914 return error_mark_node;
25916 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25917 lazily_declare_fn (sfk_destructor, type);
25919 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
25920 return dtor;
25922 return error_mark_node;
25925 /* By this point, the NAME should be an ordinary identifier. If
25926 the id-expression was a qualified name, the qualifying scope is
25927 stored in PARSER->SCOPE at this point. */
25928 gcc_assert (identifier_p (name));
25930 /* Perform the lookup. */
25931 if (parser->scope)
25933 bool dependent_p;
25935 if (parser->scope == error_mark_node)
25936 return error_mark_node;
25938 /* If the SCOPE is dependent, the lookup must be deferred until
25939 the template is instantiated -- unless we are explicitly
25940 looking up names in uninstantiated templates. Even then, we
25941 cannot look up the name if the scope is not a class type; it
25942 might, for example, be a template type parameter. */
25943 dependent_p = (TYPE_P (parser->scope)
25944 && dependent_scope_p (parser->scope));
25945 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25946 && dependent_p)
25947 /* Defer lookup. */
25948 decl = error_mark_node;
25949 else
25951 tree pushed_scope = NULL_TREE;
25953 /* If PARSER->SCOPE is a dependent type, then it must be a
25954 class type, and we must not be checking dependencies;
25955 otherwise, we would have processed this lookup above. So
25956 that PARSER->SCOPE is not considered a dependent base by
25957 lookup_member, we must enter the scope here. */
25958 if (dependent_p)
25959 pushed_scope = push_scope (parser->scope);
25961 /* If the PARSER->SCOPE is a template specialization, it
25962 may be instantiated during name lookup. In that case,
25963 errors may be issued. Even if we rollback the current
25964 tentative parse, those errors are valid. */
25965 decl = lookup_qualified_name (parser->scope, name,
25966 prefer_type_arg (tag_type),
25967 /*complain=*/true);
25969 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25970 lookup result and the nested-name-specifier nominates a class C:
25971 * if the name specified after the nested-name-specifier, when
25972 looked up in C, is the injected-class-name of C (Clause 9), or
25973 * if the name specified after the nested-name-specifier is the
25974 same as the identifier or the simple-template-id's template-
25975 name in the last component of the nested-name-specifier,
25976 the name is instead considered to name the constructor of
25977 class C. [ Note: for example, the constructor is not an
25978 acceptable lookup result in an elaborated-type-specifier so
25979 the constructor would not be used in place of the
25980 injected-class-name. --end note ] Such a constructor name
25981 shall be used only in the declarator-id of a declaration that
25982 names a constructor or in a using-declaration. */
25983 if (tag_type == none_type
25984 && DECL_SELF_REFERENCE_P (decl)
25985 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25986 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25987 prefer_type_arg (tag_type),
25988 /*complain=*/true);
25990 /* If we have a single function from a using decl, pull it out. */
25991 if (TREE_CODE (decl) == OVERLOAD
25992 && !really_overloaded_fn (decl))
25993 decl = OVL_FUNCTION (decl);
25995 if (pushed_scope)
25996 pop_scope (pushed_scope);
25999 /* If the scope is a dependent type and either we deferred lookup or
26000 we did lookup but didn't find the name, rememeber the name. */
26001 if (decl == error_mark_node && TYPE_P (parser->scope)
26002 && dependent_type_p (parser->scope))
26004 if (tag_type)
26006 tree type;
26008 /* The resolution to Core Issue 180 says that `struct
26009 A::B' should be considered a type-name, even if `A'
26010 is dependent. */
26011 type = make_typename_type (parser->scope, name, tag_type,
26012 /*complain=*/tf_error);
26013 if (type != error_mark_node)
26014 decl = TYPE_NAME (type);
26016 else if (is_template
26017 && (cp_parser_next_token_ends_template_argument_p (parser)
26018 || cp_lexer_next_token_is (parser->lexer,
26019 CPP_CLOSE_PAREN)))
26020 decl = make_unbound_class_template (parser->scope,
26021 name, NULL_TREE,
26022 /*complain=*/tf_error);
26023 else
26024 decl = build_qualified_name (/*type=*/NULL_TREE,
26025 parser->scope, name,
26026 is_template);
26028 parser->qualifying_scope = parser->scope;
26029 parser->object_scope = NULL_TREE;
26031 else if (object_type)
26033 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26034 OBJECT_TYPE is not a class. */
26035 if (CLASS_TYPE_P (object_type))
26036 /* If the OBJECT_TYPE is a template specialization, it may
26037 be instantiated during name lookup. In that case, errors
26038 may be issued. Even if we rollback the current tentative
26039 parse, those errors are valid. */
26040 decl = lookup_member (object_type,
26041 name,
26042 /*protect=*/0,
26043 prefer_type_arg (tag_type),
26044 tf_warning_or_error);
26045 else
26046 decl = NULL_TREE;
26048 if (!decl)
26049 /* Look it up in the enclosing context. DR 141: When looking for a
26050 template-name after -> or ., only consider class templates. */
26051 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26052 /*nonclass=*/0,
26053 /*block_p=*/true, is_namespace, 0);
26054 if (object_type == unknown_type_node)
26055 /* The object is type-dependent, so we can't look anything up; we used
26056 this to get the DR 141 behavior. */
26057 object_type = NULL_TREE;
26058 parser->object_scope = object_type;
26059 parser->qualifying_scope = NULL_TREE;
26061 else
26063 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26064 /*nonclass=*/0,
26065 /*block_p=*/true, is_namespace, 0);
26066 parser->qualifying_scope = NULL_TREE;
26067 parser->object_scope = NULL_TREE;
26070 /* If the lookup failed, let our caller know. */
26071 if (!decl || decl == error_mark_node)
26072 return error_mark_node;
26074 /* Pull out the template from an injected-class-name (or multiple). */
26075 if (is_template)
26076 decl = maybe_get_template_decl_from_type_decl (decl);
26078 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26079 if (TREE_CODE (decl) == TREE_LIST)
26081 if (ambiguous_decls)
26082 *ambiguous_decls = decl;
26083 /* The error message we have to print is too complicated for
26084 cp_parser_error, so we incorporate its actions directly. */
26085 if (!cp_parser_simulate_error (parser))
26087 error_at (name_location, "reference to %qD is ambiguous",
26088 name);
26089 print_candidates (decl);
26091 return error_mark_node;
26094 gcc_assert (DECL_P (decl)
26095 || TREE_CODE (decl) == OVERLOAD
26096 || TREE_CODE (decl) == SCOPE_REF
26097 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26098 || BASELINK_P (decl));
26100 /* If we have resolved the name of a member declaration, check to
26101 see if the declaration is accessible. When the name resolves to
26102 set of overloaded functions, accessibility is checked when
26103 overload resolution is done.
26105 During an explicit instantiation, access is not checked at all,
26106 as per [temp.explicit]. */
26107 if (DECL_P (decl))
26108 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26110 maybe_record_typedef_use (decl);
26112 return cp_expr (decl, name_location);
26115 /* Like cp_parser_lookup_name, but for use in the typical case where
26116 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26117 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26119 static tree
26120 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26122 return cp_parser_lookup_name (parser, name,
26123 none_type,
26124 /*is_template=*/false,
26125 /*is_namespace=*/false,
26126 /*check_dependency=*/true,
26127 /*ambiguous_decls=*/NULL,
26128 location);
26131 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26132 the current context, return the TYPE_DECL. If TAG_NAME_P is
26133 true, the DECL indicates the class being defined in a class-head,
26134 or declared in an elaborated-type-specifier.
26136 Otherwise, return DECL. */
26138 static tree
26139 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26141 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26142 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26144 struct A {
26145 template <typename T> struct B;
26148 template <typename T> struct A::B {};
26150 Similarly, in an elaborated-type-specifier:
26152 namespace N { struct X{}; }
26154 struct A {
26155 template <typename T> friend struct N::X;
26158 However, if the DECL refers to a class type, and we are in
26159 the scope of the class, then the name lookup automatically
26160 finds the TYPE_DECL created by build_self_reference rather
26161 than a TEMPLATE_DECL. For example, in:
26163 template <class T> struct S {
26164 S s;
26167 there is no need to handle such case. */
26169 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26170 return DECL_TEMPLATE_RESULT (decl);
26172 return decl;
26175 /* If too many, or too few, template-parameter lists apply to the
26176 declarator, issue an error message. Returns TRUE if all went well,
26177 and FALSE otherwise. */
26179 static bool
26180 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26181 cp_declarator *declarator,
26182 location_t declarator_location)
26184 switch (declarator->kind)
26186 case cdk_id:
26188 unsigned num_templates = 0;
26189 tree scope = declarator->u.id.qualifying_scope;
26191 if (scope)
26192 num_templates = num_template_headers_for_class (scope);
26193 else if (TREE_CODE (declarator->u.id.unqualified_name)
26194 == TEMPLATE_ID_EXPR)
26195 /* If the DECLARATOR has the form `X<y>' then it uses one
26196 additional level of template parameters. */
26197 ++num_templates;
26199 return cp_parser_check_template_parameters
26200 (parser, num_templates, declarator_location, declarator);
26203 case cdk_function:
26204 case cdk_array:
26205 case cdk_pointer:
26206 case cdk_reference:
26207 case cdk_ptrmem:
26208 return (cp_parser_check_declarator_template_parameters
26209 (parser, declarator->declarator, declarator_location));
26211 case cdk_decomp:
26212 case cdk_error:
26213 return true;
26215 default:
26216 gcc_unreachable ();
26218 return false;
26221 /* NUM_TEMPLATES were used in the current declaration. If that is
26222 invalid, return FALSE and issue an error messages. Otherwise,
26223 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26224 declarator and we can print more accurate diagnostics. */
26226 static bool
26227 cp_parser_check_template_parameters (cp_parser* parser,
26228 unsigned num_templates,
26229 location_t location,
26230 cp_declarator *declarator)
26232 /* If there are the same number of template classes and parameter
26233 lists, that's OK. */
26234 if (parser->num_template_parameter_lists == num_templates)
26235 return true;
26236 /* If there are more, but only one more, then we are referring to a
26237 member template. That's OK too. */
26238 if (parser->num_template_parameter_lists == num_templates + 1)
26239 return true;
26240 /* If there are more template classes than parameter lists, we have
26241 something like:
26243 template <class T> void S<T>::R<T>::f (); */
26244 if (parser->num_template_parameter_lists < num_templates)
26246 if (declarator && !current_function_decl)
26247 error_at (location, "specializing member %<%T::%E%> "
26248 "requires %<template<>%> syntax",
26249 declarator->u.id.qualifying_scope,
26250 declarator->u.id.unqualified_name);
26251 else if (declarator)
26252 error_at (location, "invalid declaration of %<%T::%E%>",
26253 declarator->u.id.qualifying_scope,
26254 declarator->u.id.unqualified_name);
26255 else
26256 error_at (location, "too few template-parameter-lists");
26257 return false;
26259 /* Otherwise, there are too many template parameter lists. We have
26260 something like:
26262 template <class T> template <class U> void S::f(); */
26263 error_at (location, "too many template-parameter-lists");
26264 return false;
26267 /* Parse an optional `::' token indicating that the following name is
26268 from the global namespace. If so, PARSER->SCOPE is set to the
26269 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26270 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26271 Returns the new value of PARSER->SCOPE, if the `::' token is
26272 present, and NULL_TREE otherwise. */
26274 static tree
26275 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26277 cp_token *token;
26279 /* Peek at the next token. */
26280 token = cp_lexer_peek_token (parser->lexer);
26281 /* If we're looking at a `::' token then we're starting from the
26282 global namespace, not our current location. */
26283 if (token->type == CPP_SCOPE)
26285 /* Consume the `::' token. */
26286 cp_lexer_consume_token (parser->lexer);
26287 /* Set the SCOPE so that we know where to start the lookup. */
26288 parser->scope = global_namespace;
26289 parser->qualifying_scope = global_namespace;
26290 parser->object_scope = NULL_TREE;
26292 return parser->scope;
26294 else if (!current_scope_valid_p)
26296 parser->scope = NULL_TREE;
26297 parser->qualifying_scope = NULL_TREE;
26298 parser->object_scope = NULL_TREE;
26301 return NULL_TREE;
26304 /* Returns TRUE if the upcoming token sequence is the start of a
26305 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26306 declarator is preceded by the `friend' specifier. */
26308 static bool
26309 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26311 bool constructor_p;
26312 bool outside_class_specifier_p;
26313 tree nested_name_specifier;
26314 cp_token *next_token;
26316 /* The common case is that this is not a constructor declarator, so
26317 try to avoid doing lots of work if at all possible. It's not
26318 valid declare a constructor at function scope. */
26319 if (parser->in_function_body)
26320 return false;
26321 /* And only certain tokens can begin a constructor declarator. */
26322 next_token = cp_lexer_peek_token (parser->lexer);
26323 if (next_token->type != CPP_NAME
26324 && next_token->type != CPP_SCOPE
26325 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26326 && next_token->type != CPP_TEMPLATE_ID)
26327 return false;
26329 /* Parse tentatively; we are going to roll back all of the tokens
26330 consumed here. */
26331 cp_parser_parse_tentatively (parser);
26332 /* Assume that we are looking at a constructor declarator. */
26333 constructor_p = true;
26335 /* Look for the optional `::' operator. */
26336 cp_parser_global_scope_opt (parser,
26337 /*current_scope_valid_p=*/false);
26338 /* Look for the nested-name-specifier. */
26339 nested_name_specifier
26340 = (cp_parser_nested_name_specifier_opt (parser,
26341 /*typename_keyword_p=*/false,
26342 /*check_dependency_p=*/false,
26343 /*type_p=*/false,
26344 /*is_declaration=*/false));
26346 outside_class_specifier_p = (!at_class_scope_p ()
26347 || !TYPE_BEING_DEFINED (current_class_type)
26348 || friend_p);
26350 /* Outside of a class-specifier, there must be a
26351 nested-name-specifier. Except in C++17 mode, where we
26352 might be declaring a guiding declaration. */
26353 if (!nested_name_specifier && outside_class_specifier_p
26354 && cxx_dialect < cxx17)
26355 constructor_p = false;
26356 else if (nested_name_specifier == error_mark_node)
26357 constructor_p = false;
26359 /* If we have a class scope, this is easy; DR 147 says that S::S always
26360 names the constructor, and no other qualified name could. */
26361 if (constructor_p && nested_name_specifier
26362 && CLASS_TYPE_P (nested_name_specifier))
26364 tree id = cp_parser_unqualified_id (parser,
26365 /*template_keyword_p=*/false,
26366 /*check_dependency_p=*/false,
26367 /*declarator_p=*/true,
26368 /*optional_p=*/false);
26369 if (is_overloaded_fn (id))
26370 id = DECL_NAME (get_first_fn (id));
26371 if (!constructor_name_p (id, nested_name_specifier))
26372 constructor_p = false;
26374 /* If we still think that this might be a constructor-declarator,
26375 look for a class-name. */
26376 else if (constructor_p)
26378 /* If we have:
26380 template <typename T> struct S {
26381 S();
26384 we must recognize that the nested `S' names a class. */
26385 if (cxx_dialect >= cxx17)
26386 cp_parser_parse_tentatively (parser);
26388 tree type_decl;
26389 type_decl = cp_parser_class_name (parser,
26390 /*typename_keyword_p=*/false,
26391 /*template_keyword_p=*/false,
26392 none_type,
26393 /*check_dependency_p=*/false,
26394 /*class_head_p=*/false,
26395 /*is_declaration=*/false);
26397 if (cxx_dialect >= cxx17
26398 && !cp_parser_parse_definitely (parser))
26400 type_decl = NULL_TREE;
26401 tree tmpl = cp_parser_template_name (parser,
26402 /*template_keyword*/false,
26403 /*check_dependency_p*/false,
26404 /*is_declaration*/false,
26405 none_type,
26406 /*is_identifier*/NULL);
26407 if (DECL_CLASS_TEMPLATE_P (tmpl)
26408 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26409 /* It's a deduction guide, return true. */;
26410 else
26411 cp_parser_simulate_error (parser);
26414 /* If there was no class-name, then this is not a constructor.
26415 Otherwise, if we are in a class-specifier and we aren't
26416 handling a friend declaration, check that its type matches
26417 current_class_type (c++/38313). Note: error_mark_node
26418 is left alone for error recovery purposes. */
26419 constructor_p = (!cp_parser_error_occurred (parser)
26420 && (outside_class_specifier_p
26421 || type_decl == NULL_TREE
26422 || type_decl == error_mark_node
26423 || same_type_p (current_class_type,
26424 TREE_TYPE (type_decl))));
26426 /* If we're still considering a constructor, we have to see a `(',
26427 to begin the parameter-declaration-clause, followed by either a
26428 `)', an `...', or a decl-specifier. We need to check for a
26429 type-specifier to avoid being fooled into thinking that:
26431 S (f) (int);
26433 is a constructor. (It is actually a function named `f' that
26434 takes one parameter (of type `int') and returns a value of type
26435 `S'. */
26436 if (constructor_p
26437 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26438 constructor_p = false;
26440 if (constructor_p
26441 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26442 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26443 /* A parameter declaration begins with a decl-specifier,
26444 which is either the "attribute" keyword, a storage class
26445 specifier, or (usually) a type-specifier. */
26446 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26448 tree type;
26449 tree pushed_scope = NULL_TREE;
26450 unsigned saved_num_template_parameter_lists;
26452 /* Names appearing in the type-specifier should be looked up
26453 in the scope of the class. */
26454 if (current_class_type)
26455 type = NULL_TREE;
26456 else if (type_decl)
26458 type = TREE_TYPE (type_decl);
26459 if (TREE_CODE (type) == TYPENAME_TYPE)
26461 type = resolve_typename_type (type,
26462 /*only_current_p=*/false);
26463 if (TREE_CODE (type) == TYPENAME_TYPE)
26465 cp_parser_abort_tentative_parse (parser);
26466 return false;
26469 pushed_scope = push_scope (type);
26472 /* Inside the constructor parameter list, surrounding
26473 template-parameter-lists do not apply. */
26474 saved_num_template_parameter_lists
26475 = parser->num_template_parameter_lists;
26476 parser->num_template_parameter_lists = 0;
26478 /* Look for the type-specifier. */
26479 cp_parser_type_specifier (parser,
26480 CP_PARSER_FLAGS_NONE,
26481 /*decl_specs=*/NULL,
26482 /*is_declarator=*/true,
26483 /*declares_class_or_enum=*/NULL,
26484 /*is_cv_qualifier=*/NULL);
26486 parser->num_template_parameter_lists
26487 = saved_num_template_parameter_lists;
26489 /* Leave the scope of the class. */
26490 if (pushed_scope)
26491 pop_scope (pushed_scope);
26493 constructor_p = !cp_parser_error_occurred (parser);
26497 /* We did not really want to consume any tokens. */
26498 cp_parser_abort_tentative_parse (parser);
26500 return constructor_p;
26503 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26504 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26505 they must be performed once we are in the scope of the function.
26507 Returns the function defined. */
26509 static tree
26510 cp_parser_function_definition_from_specifiers_and_declarator
26511 (cp_parser* parser,
26512 cp_decl_specifier_seq *decl_specifiers,
26513 tree attributes,
26514 const cp_declarator *declarator)
26516 tree fn;
26517 bool success_p;
26519 /* Begin the function-definition. */
26520 success_p = start_function (decl_specifiers, declarator, attributes);
26522 /* The things we're about to see are not directly qualified by any
26523 template headers we've seen thus far. */
26524 reset_specialization ();
26526 /* If there were names looked up in the decl-specifier-seq that we
26527 did not check, check them now. We must wait until we are in the
26528 scope of the function to perform the checks, since the function
26529 might be a friend. */
26530 perform_deferred_access_checks (tf_warning_or_error);
26532 if (success_p)
26534 cp_finalize_omp_declare_simd (parser, current_function_decl);
26535 parser->omp_declare_simd = NULL;
26536 cp_finalize_oacc_routine (parser, current_function_decl, true);
26537 parser->oacc_routine = NULL;
26540 if (!success_p)
26542 /* Skip the entire function. */
26543 cp_parser_skip_to_end_of_block_or_statement (parser);
26544 fn = error_mark_node;
26546 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26548 /* Seen already, skip it. An error message has already been output. */
26549 cp_parser_skip_to_end_of_block_or_statement (parser);
26550 fn = current_function_decl;
26551 current_function_decl = NULL_TREE;
26552 /* If this is a function from a class, pop the nested class. */
26553 if (current_class_name)
26554 pop_nested_class ();
26556 else
26558 timevar_id_t tv;
26559 if (DECL_DECLARED_INLINE_P (current_function_decl))
26560 tv = TV_PARSE_INLINE;
26561 else
26562 tv = TV_PARSE_FUNC;
26563 timevar_push (tv);
26564 fn = cp_parser_function_definition_after_declarator (parser,
26565 /*inline_p=*/false);
26566 timevar_pop (tv);
26569 return fn;
26572 /* Parse the part of a function-definition that follows the
26573 declarator. INLINE_P is TRUE iff this function is an inline
26574 function defined within a class-specifier.
26576 Returns the function defined. */
26578 static tree
26579 cp_parser_function_definition_after_declarator (cp_parser* parser,
26580 bool inline_p)
26582 tree fn;
26583 bool saved_in_unbraced_linkage_specification_p;
26584 bool saved_in_function_body;
26585 unsigned saved_num_template_parameter_lists;
26586 cp_token *token;
26587 bool fully_implicit_function_template_p
26588 = parser->fully_implicit_function_template_p;
26589 parser->fully_implicit_function_template_p = false;
26590 tree implicit_template_parms
26591 = parser->implicit_template_parms;
26592 parser->implicit_template_parms = 0;
26593 cp_binding_level* implicit_template_scope
26594 = parser->implicit_template_scope;
26595 parser->implicit_template_scope = 0;
26597 saved_in_function_body = parser->in_function_body;
26598 parser->in_function_body = true;
26599 /* If the next token is `return', then the code may be trying to
26600 make use of the "named return value" extension that G++ used to
26601 support. */
26602 token = cp_lexer_peek_token (parser->lexer);
26603 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26605 /* Consume the `return' keyword. */
26606 cp_lexer_consume_token (parser->lexer);
26607 /* Look for the identifier that indicates what value is to be
26608 returned. */
26609 cp_parser_identifier (parser);
26610 /* Issue an error message. */
26611 error_at (token->location,
26612 "named return values are no longer supported");
26613 /* Skip tokens until we reach the start of the function body. */
26614 while (true)
26616 cp_token *token = cp_lexer_peek_token (parser->lexer);
26617 if (token->type == CPP_OPEN_BRACE
26618 || token->type == CPP_EOF
26619 || token->type == CPP_PRAGMA_EOL)
26620 break;
26621 cp_lexer_consume_token (parser->lexer);
26624 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26625 anything declared inside `f'. */
26626 saved_in_unbraced_linkage_specification_p
26627 = parser->in_unbraced_linkage_specification_p;
26628 parser->in_unbraced_linkage_specification_p = false;
26629 /* Inside the function, surrounding template-parameter-lists do not
26630 apply. */
26631 saved_num_template_parameter_lists
26632 = parser->num_template_parameter_lists;
26633 parser->num_template_parameter_lists = 0;
26635 /* If the next token is `try', `__transaction_atomic', or
26636 `__transaction_relaxed`, then we are looking at either function-try-block
26637 or function-transaction-block. Note that all of these include the
26638 function-body. */
26639 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26640 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26641 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26642 RID_TRANSACTION_RELAXED))
26643 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26644 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26645 cp_parser_function_try_block (parser);
26646 else
26647 cp_parser_ctor_initializer_opt_and_function_body
26648 (parser, /*in_function_try_block=*/false);
26650 /* Finish the function. */
26651 fn = finish_function (inline_p);
26652 /* Generate code for it, if necessary. */
26653 expand_or_defer_fn (fn);
26654 /* Restore the saved values. */
26655 parser->in_unbraced_linkage_specification_p
26656 = saved_in_unbraced_linkage_specification_p;
26657 parser->num_template_parameter_lists
26658 = saved_num_template_parameter_lists;
26659 parser->in_function_body = saved_in_function_body;
26661 parser->fully_implicit_function_template_p
26662 = fully_implicit_function_template_p;
26663 parser->implicit_template_parms
26664 = implicit_template_parms;
26665 parser->implicit_template_scope
26666 = implicit_template_scope;
26668 if (parser->fully_implicit_function_template_p)
26669 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26671 return fn;
26674 /* Parse a template-declaration body (following argument list). */
26676 static void
26677 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26678 tree parameter_list,
26679 bool member_p)
26681 tree decl = NULL_TREE;
26682 bool friend_p = false;
26684 /* We just processed one more parameter list. */
26685 ++parser->num_template_parameter_lists;
26687 /* Get the deferred access checks from the parameter list. These
26688 will be checked once we know what is being declared, as for a
26689 member template the checks must be performed in the scope of the
26690 class containing the member. */
26691 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26693 /* Tentatively parse for a new template parameter list, which can either be
26694 the template keyword or a template introduction. */
26695 if (cp_parser_template_declaration_after_export (parser, member_p))
26696 /* OK */;
26697 else if (cxx_dialect >= cxx11
26698 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26699 decl = cp_parser_alias_declaration (parser);
26700 else
26702 /* There are no access checks when parsing a template, as we do not
26703 know if a specialization will be a friend. */
26704 push_deferring_access_checks (dk_no_check);
26705 cp_token *token = cp_lexer_peek_token (parser->lexer);
26706 decl = cp_parser_single_declaration (parser,
26707 checks,
26708 member_p,
26709 /*explicit_specialization_p=*/false,
26710 &friend_p);
26711 pop_deferring_access_checks ();
26713 /* If this is a member template declaration, let the front
26714 end know. */
26715 if (member_p && !friend_p && decl)
26717 if (TREE_CODE (decl) == TYPE_DECL)
26718 cp_parser_check_access_in_redeclaration (decl, token->location);
26720 decl = finish_member_template_decl (decl);
26722 else if (friend_p && decl
26723 && DECL_DECLARES_TYPE_P (decl))
26724 make_friend_class (current_class_type, TREE_TYPE (decl),
26725 /*complain=*/true);
26727 /* We are done with the current parameter list. */
26728 --parser->num_template_parameter_lists;
26730 pop_deferring_access_checks ();
26732 /* Finish up. */
26733 finish_template_decl (parameter_list);
26735 /* Check the template arguments for a literal operator template. */
26736 if (decl
26737 && DECL_DECLARES_FUNCTION_P (decl)
26738 && UDLIT_OPER_P (DECL_NAME (decl)))
26740 bool ok = true;
26741 if (parameter_list == NULL_TREE)
26742 ok = false;
26743 else
26745 int num_parms = TREE_VEC_LENGTH (parameter_list);
26746 if (num_parms == 1)
26748 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26749 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26750 if (TREE_TYPE (parm) != char_type_node
26751 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26752 ok = false;
26754 else if (num_parms == 2 && cxx_dialect >= cxx14)
26756 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26757 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26758 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26759 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26760 if (parm == error_mark_node
26761 || TREE_TYPE (parm) != TREE_TYPE (type)
26762 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26763 ok = false;
26765 else
26766 ok = false;
26768 if (!ok)
26770 if (cxx_dialect >= cxx14)
26771 error ("literal operator template %qD has invalid parameter list."
26772 " Expected non-type template argument pack <char...>"
26773 " or <typename CharT, CharT...>",
26774 decl);
26775 else
26776 error ("literal operator template %qD has invalid parameter list."
26777 " Expected non-type template argument pack <char...>",
26778 decl);
26782 /* Register member declarations. */
26783 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26784 finish_member_declaration (decl);
26785 /* If DECL is a function template, we must return to parse it later.
26786 (Even though there is no definition, there might be default
26787 arguments that need handling.) */
26788 if (member_p && decl
26789 && DECL_DECLARES_FUNCTION_P (decl))
26790 vec_safe_push (unparsed_funs_with_definitions, decl);
26793 /* Parse a template introduction header for a template-declaration. Returns
26794 false if tentative parse fails. */
26796 static bool
26797 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26799 cp_parser_parse_tentatively (parser);
26801 tree saved_scope = parser->scope;
26802 tree saved_object_scope = parser->object_scope;
26803 tree saved_qualifying_scope = parser->qualifying_scope;
26805 /* Look for the optional `::' operator. */
26806 cp_parser_global_scope_opt (parser,
26807 /*current_scope_valid_p=*/false);
26808 /* Look for the nested-name-specifier. */
26809 cp_parser_nested_name_specifier_opt (parser,
26810 /*typename_keyword_p=*/false,
26811 /*check_dependency_p=*/true,
26812 /*type_p=*/false,
26813 /*is_declaration=*/false);
26815 cp_token *token = cp_lexer_peek_token (parser->lexer);
26816 tree concept_name = cp_parser_identifier (parser);
26818 /* Look up the concept for which we will be matching
26819 template parameters. */
26820 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26821 token->location);
26822 parser->scope = saved_scope;
26823 parser->object_scope = saved_object_scope;
26824 parser->qualifying_scope = saved_qualifying_scope;
26826 if (concept_name == error_mark_node)
26827 cp_parser_simulate_error (parser);
26829 /* Look for opening brace for introduction. */
26830 matching_braces braces;
26831 braces.require_open (parser);
26833 if (!cp_parser_parse_definitely (parser))
26834 return false;
26836 push_deferring_access_checks (dk_deferred);
26838 /* Build vector of placeholder parameters and grab
26839 matching identifiers. */
26840 tree introduction_list = cp_parser_introduction_list (parser);
26842 /* The introduction-list shall not be empty. */
26843 int nargs = TREE_VEC_LENGTH (introduction_list);
26844 if (nargs == 0)
26846 error ("empty introduction-list");
26847 return true;
26850 /* Look for closing brace for introduction. */
26851 if (!braces.require_close (parser))
26852 return true;
26854 if (tmpl_decl == error_mark_node)
26856 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26857 token->location);
26858 return true;
26861 /* Build and associate the constraint. */
26862 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26863 if (parms && parms != error_mark_node)
26865 cp_parser_template_declaration_after_parameters (parser, parms,
26866 member_p);
26867 return true;
26870 error_at (token->location, "no matching concept for template-introduction");
26871 return true;
26874 /* Parse a normal template-declaration following the template keyword. */
26876 static void
26877 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26879 tree parameter_list;
26880 bool need_lang_pop;
26881 location_t location = input_location;
26883 /* Look for the `<' token. */
26884 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26885 return;
26886 if (at_class_scope_p () && current_function_decl)
26888 /* 14.5.2.2 [temp.mem]
26890 A local class shall not have member templates. */
26891 error_at (location,
26892 "invalid declaration of member template in local class");
26893 cp_parser_skip_to_end_of_block_or_statement (parser);
26894 return;
26896 /* [temp]
26898 A template ... shall not have C linkage. */
26899 if (current_lang_name == lang_name_c)
26901 error_at (location, "template with C linkage");
26902 maybe_show_extern_c_location ();
26903 /* Give it C++ linkage to avoid confusing other parts of the
26904 front end. */
26905 push_lang_context (lang_name_cplusplus);
26906 need_lang_pop = true;
26908 else
26909 need_lang_pop = false;
26911 /* We cannot perform access checks on the template parameter
26912 declarations until we know what is being declared, just as we
26913 cannot check the decl-specifier list. */
26914 push_deferring_access_checks (dk_deferred);
26916 /* If the next token is `>', then we have an invalid
26917 specialization. Rather than complain about an invalid template
26918 parameter, issue an error message here. */
26919 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26921 cp_parser_error (parser, "invalid explicit specialization");
26922 begin_specialization ();
26923 parameter_list = NULL_TREE;
26925 else
26927 /* Parse the template parameters. */
26928 parameter_list = cp_parser_template_parameter_list (parser);
26931 /* Look for the `>'. */
26932 cp_parser_skip_to_end_of_template_parameter_list (parser);
26934 /* Manage template requirements */
26935 if (flag_concepts)
26937 tree reqs = get_shorthand_constraints (current_template_parms);
26938 if (tree r = cp_parser_requires_clause_opt (parser))
26939 reqs = conjoin_constraints (reqs, normalize_expression (r));
26940 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26943 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26944 member_p);
26946 /* For the erroneous case of a template with C linkage, we pushed an
26947 implicit C++ linkage scope; exit that scope now. */
26948 if (need_lang_pop)
26949 pop_lang_context ();
26952 /* Parse a template-declaration, assuming that the `export' (and
26953 `extern') keywords, if present, has already been scanned. MEMBER_P
26954 is as for cp_parser_template_declaration. */
26956 static bool
26957 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26959 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26961 cp_lexer_consume_token (parser->lexer);
26962 cp_parser_explicit_template_declaration (parser, member_p);
26963 return true;
26965 else if (flag_concepts)
26966 return cp_parser_template_introduction (parser, member_p);
26968 return false;
26971 /* Perform the deferred access checks from a template-parameter-list.
26972 CHECKS is a TREE_LIST of access checks, as returned by
26973 get_deferred_access_checks. */
26975 static void
26976 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26978 ++processing_template_parmlist;
26979 perform_access_checks (checks, tf_warning_or_error);
26980 --processing_template_parmlist;
26983 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26984 `function-definition' sequence that follows a template header.
26985 If MEMBER_P is true, this declaration appears in a class scope.
26987 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26988 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26990 static tree
26991 cp_parser_single_declaration (cp_parser* parser,
26992 vec<deferred_access_check, va_gc> *checks,
26993 bool member_p,
26994 bool explicit_specialization_p,
26995 bool* friend_p)
26997 int declares_class_or_enum;
26998 tree decl = NULL_TREE;
26999 cp_decl_specifier_seq decl_specifiers;
27000 bool function_definition_p = false;
27001 cp_token *decl_spec_token_start;
27003 /* This function is only used when processing a template
27004 declaration. */
27005 gcc_assert (innermost_scope_kind () == sk_template_parms
27006 || innermost_scope_kind () == sk_template_spec);
27008 /* Defer access checks until we know what is being declared. */
27009 push_deferring_access_checks (dk_deferred);
27011 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27012 alternative. */
27013 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27014 cp_parser_decl_specifier_seq (parser,
27015 CP_PARSER_FLAGS_OPTIONAL,
27016 &decl_specifiers,
27017 &declares_class_or_enum);
27018 if (friend_p)
27019 *friend_p = cp_parser_friend_p (&decl_specifiers);
27021 /* There are no template typedefs. */
27022 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27024 error_at (decl_spec_token_start->location,
27025 "template declaration of %<typedef%>");
27026 decl = error_mark_node;
27029 /* Gather up the access checks that occurred the
27030 decl-specifier-seq. */
27031 stop_deferring_access_checks ();
27033 /* Check for the declaration of a template class. */
27034 if (declares_class_or_enum)
27036 if (cp_parser_declares_only_class_p (parser)
27037 || (declares_class_or_enum & 2))
27039 // If this is a declaration, but not a definition, associate
27040 // any constraints with the type declaration. Constraints
27041 // are associated with definitions in cp_parser_class_specifier.
27042 if (declares_class_or_enum == 1)
27043 associate_classtype_constraints (decl_specifiers.type);
27045 decl = shadow_tag (&decl_specifiers);
27047 /* In this case:
27049 struct C {
27050 friend template <typename T> struct A<T>::B;
27053 A<T>::B will be represented by a TYPENAME_TYPE, and
27054 therefore not recognized by shadow_tag. */
27055 if (friend_p && *friend_p
27056 && !decl
27057 && decl_specifiers.type
27058 && TYPE_P (decl_specifiers.type))
27059 decl = decl_specifiers.type;
27061 if (decl && decl != error_mark_node)
27062 decl = TYPE_NAME (decl);
27063 else
27064 decl = error_mark_node;
27066 /* Perform access checks for template parameters. */
27067 cp_parser_perform_template_parameter_access_checks (checks);
27069 /* Give a helpful diagnostic for
27070 template <class T> struct A { } a;
27071 if we aren't already recovering from an error. */
27072 if (!cp_parser_declares_only_class_p (parser)
27073 && !seen_error ())
27075 error_at (cp_lexer_peek_token (parser->lexer)->location,
27076 "a class template declaration must not declare "
27077 "anything else");
27078 cp_parser_skip_to_end_of_block_or_statement (parser);
27079 goto out;
27084 /* Complain about missing 'typename' or other invalid type names. */
27085 if (!decl_specifiers.any_type_specifiers_p
27086 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27088 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27089 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27090 the rest of this declaration. */
27091 decl = error_mark_node;
27092 goto out;
27095 /* If it's not a template class, try for a template function. If
27096 the next token is a `;', then this declaration does not declare
27097 anything. But, if there were errors in the decl-specifiers, then
27098 the error might well have come from an attempted class-specifier.
27099 In that case, there's no need to warn about a missing declarator. */
27100 if (!decl
27101 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27102 || decl_specifiers.type != error_mark_node))
27104 decl = cp_parser_init_declarator (parser,
27105 &decl_specifiers,
27106 checks,
27107 /*function_definition_allowed_p=*/true,
27108 member_p,
27109 declares_class_or_enum,
27110 &function_definition_p,
27111 NULL, NULL, NULL);
27113 /* 7.1.1-1 [dcl.stc]
27115 A storage-class-specifier shall not be specified in an explicit
27116 specialization... */
27117 if (decl
27118 && explicit_specialization_p
27119 && decl_specifiers.storage_class != sc_none)
27121 error_at (decl_spec_token_start->location,
27122 "explicit template specialization cannot have a storage class");
27123 decl = error_mark_node;
27126 if (decl && VAR_P (decl))
27127 check_template_variable (decl);
27130 /* Look for a trailing `;' after the declaration. */
27131 if (!function_definition_p
27132 && (decl == error_mark_node
27133 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27134 cp_parser_skip_to_end_of_block_or_statement (parser);
27136 out:
27137 pop_deferring_access_checks ();
27139 /* Clear any current qualification; whatever comes next is the start
27140 of something new. */
27141 parser->scope = NULL_TREE;
27142 parser->qualifying_scope = NULL_TREE;
27143 parser->object_scope = NULL_TREE;
27145 return decl;
27148 /* Parse a cast-expression that is not the operand of a unary "&". */
27150 static cp_expr
27151 cp_parser_simple_cast_expression (cp_parser *parser)
27153 return cp_parser_cast_expression (parser, /*address_p=*/false,
27154 /*cast_p=*/false, /*decltype*/false, NULL);
27157 /* Parse a functional cast to TYPE. Returns an expression
27158 representing the cast. */
27160 static cp_expr
27161 cp_parser_functional_cast (cp_parser* parser, tree type)
27163 vec<tree, va_gc> *vec;
27164 tree expression_list;
27165 cp_expr cast;
27166 bool nonconst_p;
27168 location_t start_loc = input_location;
27170 if (!type)
27171 type = error_mark_node;
27173 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27175 cp_lexer_set_source_position (parser->lexer);
27176 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27177 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27178 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27179 if (TREE_CODE (type) == TYPE_DECL)
27180 type = TREE_TYPE (type);
27182 cast = finish_compound_literal (type, expression_list,
27183 tf_warning_or_error, fcl_functional);
27184 /* Create a location of the form:
27185 type_name{i, f}
27186 ^~~~~~~~~~~~~~~
27187 with caret == start at the start of the type name,
27188 finishing at the closing brace. */
27189 location_t finish_loc
27190 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27191 location_t combined_loc = make_location (start_loc, start_loc,
27192 finish_loc);
27193 cast.set_location (combined_loc);
27194 return cast;
27198 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27199 /*cast_p=*/true,
27200 /*allow_expansion_p=*/true,
27201 /*non_constant_p=*/NULL);
27202 if (vec == NULL)
27203 expression_list = error_mark_node;
27204 else
27206 expression_list = build_tree_list_vec (vec);
27207 release_tree_vector (vec);
27210 cast = build_functional_cast (type, expression_list,
27211 tf_warning_or_error);
27212 /* [expr.const]/1: In an integral constant expression "only type
27213 conversions to integral or enumeration type can be used". */
27214 if (TREE_CODE (type) == TYPE_DECL)
27215 type = TREE_TYPE (type);
27216 if (cast != error_mark_node
27217 && !cast_valid_in_integral_constant_expression_p (type)
27218 && cp_parser_non_integral_constant_expression (parser,
27219 NIC_CONSTRUCTOR))
27220 return error_mark_node;
27222 /* Create a location of the form:
27223 float(i)
27224 ^~~~~~~~
27225 with caret == start at the start of the type name,
27226 finishing at the closing paren. */
27227 location_t finish_loc
27228 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27229 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27230 cast.set_location (combined_loc);
27231 return cast;
27234 /* Save the tokens that make up the body of a member function defined
27235 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27236 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27237 specifiers applied to the declaration. Returns the FUNCTION_DECL
27238 for the member function. */
27240 static tree
27241 cp_parser_save_member_function_body (cp_parser* parser,
27242 cp_decl_specifier_seq *decl_specifiers,
27243 cp_declarator *declarator,
27244 tree attributes)
27246 cp_token *first;
27247 cp_token *last;
27248 tree fn;
27249 bool function_try_block = false;
27251 /* Create the FUNCTION_DECL. */
27252 fn = grokmethod (decl_specifiers, declarator, attributes);
27253 cp_finalize_omp_declare_simd (parser, fn);
27254 cp_finalize_oacc_routine (parser, fn, true);
27255 /* If something went badly wrong, bail out now. */
27256 if (fn == error_mark_node)
27258 /* If there's a function-body, skip it. */
27259 if (cp_parser_token_starts_function_definition_p
27260 (cp_lexer_peek_token (parser->lexer)))
27261 cp_parser_skip_to_end_of_block_or_statement (parser);
27262 return error_mark_node;
27265 /* Remember it, if there default args to post process. */
27266 cp_parser_save_default_args (parser, fn);
27268 /* Save away the tokens that make up the body of the
27269 function. */
27270 first = parser->lexer->next_token;
27272 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27273 cp_lexer_consume_token (parser->lexer);
27274 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27275 RID_TRANSACTION_ATOMIC))
27277 cp_lexer_consume_token (parser->lexer);
27278 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27279 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27280 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27281 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27282 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27283 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27284 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27286 cp_lexer_consume_token (parser->lexer);
27287 cp_lexer_consume_token (parser->lexer);
27288 cp_lexer_consume_token (parser->lexer);
27289 cp_lexer_consume_token (parser->lexer);
27290 cp_lexer_consume_token (parser->lexer);
27292 else
27293 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27294 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27296 cp_lexer_consume_token (parser->lexer);
27297 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27298 break;
27302 /* Handle function try blocks. */
27303 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27305 cp_lexer_consume_token (parser->lexer);
27306 function_try_block = true;
27308 /* We can have braced-init-list mem-initializers before the fn body. */
27309 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27311 cp_lexer_consume_token (parser->lexer);
27312 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27314 /* cache_group will stop after an un-nested { } pair, too. */
27315 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27316 break;
27318 /* variadic mem-inits have ... after the ')'. */
27319 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27320 cp_lexer_consume_token (parser->lexer);
27323 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27324 /* Handle function try blocks. */
27325 if (function_try_block)
27326 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27327 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27328 last = parser->lexer->next_token;
27330 /* Save away the inline definition; we will process it when the
27331 class is complete. */
27332 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27333 DECL_PENDING_INLINE_P (fn) = 1;
27335 /* We need to know that this was defined in the class, so that
27336 friend templates are handled correctly. */
27337 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27339 /* Add FN to the queue of functions to be parsed later. */
27340 vec_safe_push (unparsed_funs_with_definitions, fn);
27342 return fn;
27345 /* Save the tokens that make up the in-class initializer for a non-static
27346 data member. Returns a DEFAULT_ARG. */
27348 static tree
27349 cp_parser_save_nsdmi (cp_parser* parser)
27351 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27354 /* Parse a template-argument-list, as well as the trailing ">" (but
27355 not the opening "<"). See cp_parser_template_argument_list for the
27356 return value. */
27358 static tree
27359 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27361 tree arguments;
27362 tree saved_scope;
27363 tree saved_qualifying_scope;
27364 tree saved_object_scope;
27365 bool saved_greater_than_is_operator_p;
27366 int saved_unevaluated_operand;
27367 int saved_inhibit_evaluation_warnings;
27369 /* [temp.names]
27371 When parsing a template-id, the first non-nested `>' is taken as
27372 the end of the template-argument-list rather than a greater-than
27373 operator. */
27374 saved_greater_than_is_operator_p
27375 = parser->greater_than_is_operator_p;
27376 parser->greater_than_is_operator_p = false;
27377 /* Parsing the argument list may modify SCOPE, so we save it
27378 here. */
27379 saved_scope = parser->scope;
27380 saved_qualifying_scope = parser->qualifying_scope;
27381 saved_object_scope = parser->object_scope;
27382 /* We need to evaluate the template arguments, even though this
27383 template-id may be nested within a "sizeof". */
27384 saved_unevaluated_operand = cp_unevaluated_operand;
27385 cp_unevaluated_operand = 0;
27386 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27387 c_inhibit_evaluation_warnings = 0;
27388 /* Parse the template-argument-list itself. */
27389 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27390 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27391 arguments = NULL_TREE;
27392 else
27393 arguments = cp_parser_template_argument_list (parser);
27394 /* Look for the `>' that ends the template-argument-list. If we find
27395 a '>>' instead, it's probably just a typo. */
27396 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27398 if (cxx_dialect != cxx98)
27400 /* In C++0x, a `>>' in a template argument list or cast
27401 expression is considered to be two separate `>'
27402 tokens. So, change the current token to a `>', but don't
27403 consume it: it will be consumed later when the outer
27404 template argument list (or cast expression) is parsed.
27405 Note that this replacement of `>' for `>>' is necessary
27406 even if we are parsing tentatively: in the tentative
27407 case, after calling
27408 cp_parser_enclosed_template_argument_list we will always
27409 throw away all of the template arguments and the first
27410 closing `>', either because the template argument list
27411 was erroneous or because we are replacing those tokens
27412 with a CPP_TEMPLATE_ID token. The second `>' (which will
27413 not have been thrown away) is needed either to close an
27414 outer template argument list or to complete a new-style
27415 cast. */
27416 cp_token *token = cp_lexer_peek_token (parser->lexer);
27417 token->type = CPP_GREATER;
27419 else if (!saved_greater_than_is_operator_p)
27421 /* If we're in a nested template argument list, the '>>' has
27422 to be a typo for '> >'. We emit the error message, but we
27423 continue parsing and we push a '>' as next token, so that
27424 the argument list will be parsed correctly. Note that the
27425 global source location is still on the token before the
27426 '>>', so we need to say explicitly where we want it. */
27427 cp_token *token = cp_lexer_peek_token (parser->lexer);
27428 gcc_rich_location richloc (token->location);
27429 richloc.add_fixit_replace ("> >");
27430 error_at (&richloc, "%<>>%> should be %<> >%> "
27431 "within a nested template argument list");
27433 token->type = CPP_GREATER;
27435 else
27437 /* If this is not a nested template argument list, the '>>'
27438 is a typo for '>'. Emit an error message and continue.
27439 Same deal about the token location, but here we can get it
27440 right by consuming the '>>' before issuing the diagnostic. */
27441 cp_token *token = cp_lexer_consume_token (parser->lexer);
27442 error_at (token->location,
27443 "spurious %<>>%>, use %<>%> to terminate "
27444 "a template argument list");
27447 else
27448 cp_parser_skip_to_end_of_template_parameter_list (parser);
27449 /* The `>' token might be a greater-than operator again now. */
27450 parser->greater_than_is_operator_p
27451 = saved_greater_than_is_operator_p;
27452 /* Restore the SAVED_SCOPE. */
27453 parser->scope = saved_scope;
27454 parser->qualifying_scope = saved_qualifying_scope;
27455 parser->object_scope = saved_object_scope;
27456 cp_unevaluated_operand = saved_unevaluated_operand;
27457 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27459 return arguments;
27462 /* MEMBER_FUNCTION is a member function, or a friend. If default
27463 arguments, or the body of the function have not yet been parsed,
27464 parse them now. */
27466 static void
27467 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27469 timevar_push (TV_PARSE_INMETH);
27470 /* If this member is a template, get the underlying
27471 FUNCTION_DECL. */
27472 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27473 member_function = DECL_TEMPLATE_RESULT (member_function);
27475 /* There should not be any class definitions in progress at this
27476 point; the bodies of members are only parsed outside of all class
27477 definitions. */
27478 gcc_assert (parser->num_classes_being_defined == 0);
27479 /* While we're parsing the member functions we might encounter more
27480 classes. We want to handle them right away, but we don't want
27481 them getting mixed up with functions that are currently in the
27482 queue. */
27483 push_unparsed_function_queues (parser);
27485 /* Make sure that any template parameters are in scope. */
27486 maybe_begin_member_template_processing (member_function);
27488 /* If the body of the function has not yet been parsed, parse it
27489 now. */
27490 if (DECL_PENDING_INLINE_P (member_function))
27492 tree function_scope;
27493 cp_token_cache *tokens;
27495 /* The function is no longer pending; we are processing it. */
27496 tokens = DECL_PENDING_INLINE_INFO (member_function);
27497 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27498 DECL_PENDING_INLINE_P (member_function) = 0;
27500 /* If this is a local class, enter the scope of the containing
27501 function. */
27502 function_scope = current_function_decl;
27503 if (function_scope)
27504 push_function_context ();
27506 /* Push the body of the function onto the lexer stack. */
27507 cp_parser_push_lexer_for_tokens (parser, tokens);
27509 /* Let the front end know that we going to be defining this
27510 function. */
27511 start_preparsed_function (member_function, NULL_TREE,
27512 SF_PRE_PARSED | SF_INCLASS_INLINE);
27514 /* Don't do access checking if it is a templated function. */
27515 if (processing_template_decl)
27516 push_deferring_access_checks (dk_no_check);
27518 /* #pragma omp declare reduction needs special parsing. */
27519 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27521 parser->lexer->in_pragma = true;
27522 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27523 finish_function (/*inline_p=*/true);
27524 cp_check_omp_declare_reduction (member_function);
27526 else
27527 /* Now, parse the body of the function. */
27528 cp_parser_function_definition_after_declarator (parser,
27529 /*inline_p=*/true);
27531 if (processing_template_decl)
27532 pop_deferring_access_checks ();
27534 /* Leave the scope of the containing function. */
27535 if (function_scope)
27536 pop_function_context ();
27537 cp_parser_pop_lexer (parser);
27540 /* Remove any template parameters from the symbol table. */
27541 maybe_end_member_template_processing ();
27543 /* Restore the queue. */
27544 pop_unparsed_function_queues (parser);
27545 timevar_pop (TV_PARSE_INMETH);
27548 /* If DECL contains any default args, remember it on the unparsed
27549 functions queue. */
27551 static void
27552 cp_parser_save_default_args (cp_parser* parser, tree decl)
27554 tree probe;
27556 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27557 probe;
27558 probe = TREE_CHAIN (probe))
27559 if (TREE_PURPOSE (probe))
27561 cp_default_arg_entry entry = {current_class_type, decl};
27562 vec_safe_push (unparsed_funs_with_default_args, entry);
27563 break;
27567 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27568 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27569 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27570 from the parameter-type-list. */
27572 static tree
27573 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27574 tree default_arg, tree parmtype)
27576 cp_token_cache *tokens;
27577 tree parsed_arg;
27578 bool dummy;
27580 if (default_arg == error_mark_node)
27581 return error_mark_node;
27583 /* Push the saved tokens for the default argument onto the parser's
27584 lexer stack. */
27585 tokens = DEFARG_TOKENS (default_arg);
27586 cp_parser_push_lexer_for_tokens (parser, tokens);
27588 start_lambda_scope (decl);
27590 /* Parse the default argument. */
27591 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27592 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27593 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27595 finish_lambda_scope ();
27597 if (parsed_arg == error_mark_node)
27598 cp_parser_skip_to_end_of_statement (parser);
27600 if (!processing_template_decl)
27602 /* In a non-template class, check conversions now. In a template,
27603 we'll wait and instantiate these as needed. */
27604 if (TREE_CODE (decl) == PARM_DECL)
27605 parsed_arg = check_default_argument (parmtype, parsed_arg,
27606 tf_warning_or_error);
27607 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27608 parsed_arg = error_mark_node;
27609 else
27610 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27613 /* If the token stream has not been completely used up, then
27614 there was extra junk after the end of the default
27615 argument. */
27616 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27618 if (TREE_CODE (decl) == PARM_DECL)
27619 cp_parser_error (parser, "expected %<,%>");
27620 else
27621 cp_parser_error (parser, "expected %<;%>");
27624 /* Revert to the main lexer. */
27625 cp_parser_pop_lexer (parser);
27627 return parsed_arg;
27630 /* FIELD is a non-static data member with an initializer which we saved for
27631 later; parse it now. */
27633 static void
27634 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27636 tree def;
27638 maybe_begin_member_template_processing (field);
27640 push_unparsed_function_queues (parser);
27641 def = cp_parser_late_parse_one_default_arg (parser, field,
27642 DECL_INITIAL (field),
27643 NULL_TREE);
27644 pop_unparsed_function_queues (parser);
27646 maybe_end_member_template_processing ();
27648 DECL_INITIAL (field) = def;
27651 /* FN is a FUNCTION_DECL which may contains a parameter with an
27652 unparsed DEFAULT_ARG. Parse the default args now. This function
27653 assumes that the current scope is the scope in which the default
27654 argument should be processed. */
27656 static void
27657 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27659 bool saved_local_variables_forbidden_p;
27660 tree parm, parmdecl;
27662 /* While we're parsing the default args, we might (due to the
27663 statement expression extension) encounter more classes. We want
27664 to handle them right away, but we don't want them getting mixed
27665 up with default args that are currently in the queue. */
27666 push_unparsed_function_queues (parser);
27668 /* Local variable names (and the `this' keyword) may not appear
27669 in a default argument. */
27670 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27671 parser->local_variables_forbidden_p = true;
27673 push_defarg_context (fn);
27675 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27676 parmdecl = DECL_ARGUMENTS (fn);
27677 parm && parm != void_list_node;
27678 parm = TREE_CHAIN (parm),
27679 parmdecl = DECL_CHAIN (parmdecl))
27681 tree default_arg = TREE_PURPOSE (parm);
27682 tree parsed_arg;
27683 vec<tree, va_gc> *insts;
27684 tree copy;
27685 unsigned ix;
27687 if (!default_arg)
27688 continue;
27690 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27691 /* This can happen for a friend declaration for a function
27692 already declared with default arguments. */
27693 continue;
27695 parsed_arg
27696 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27697 default_arg,
27698 TREE_VALUE (parm));
27699 TREE_PURPOSE (parm) = parsed_arg;
27701 /* Update any instantiations we've already created. */
27702 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27703 vec_safe_iterate (insts, ix, &copy); ix++)
27704 TREE_PURPOSE (copy) = parsed_arg;
27707 pop_defarg_context ();
27709 /* Make sure no default arg is missing. */
27710 check_default_args (fn);
27712 /* Restore the state of local_variables_forbidden_p. */
27713 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27715 /* Restore the queue. */
27716 pop_unparsed_function_queues (parser);
27719 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27721 sizeof ... ( identifier )
27723 where the 'sizeof' token has already been consumed. */
27725 static tree
27726 cp_parser_sizeof_pack (cp_parser *parser)
27728 /* Consume the `...'. */
27729 cp_lexer_consume_token (parser->lexer);
27730 maybe_warn_variadic_templates ();
27732 matching_parens parens;
27733 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27734 if (paren)
27735 parens.consume_open (parser);
27736 else
27737 permerror (cp_lexer_peek_token (parser->lexer)->location,
27738 "%<sizeof...%> argument must be surrounded by parentheses");
27740 cp_token *token = cp_lexer_peek_token (parser->lexer);
27741 tree name = cp_parser_identifier (parser);
27742 if (name == error_mark_node)
27743 return error_mark_node;
27744 /* The name is not qualified. */
27745 parser->scope = NULL_TREE;
27746 parser->qualifying_scope = NULL_TREE;
27747 parser->object_scope = NULL_TREE;
27748 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27749 if (expr == error_mark_node)
27750 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27751 token->location);
27752 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27753 expr = TREE_TYPE (expr);
27754 else if (TREE_CODE (expr) == CONST_DECL)
27755 expr = DECL_INITIAL (expr);
27756 expr = make_pack_expansion (expr);
27757 PACK_EXPANSION_SIZEOF_P (expr) = true;
27759 if (paren)
27760 parens.require_close (parser);
27762 return expr;
27765 /* Parse the operand of `sizeof' (or a similar operator). Returns
27766 either a TYPE or an expression, depending on the form of the
27767 input. The KEYWORD indicates which kind of expression we have
27768 encountered. */
27770 static tree
27771 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27773 tree expr = NULL_TREE;
27774 const char *saved_message;
27775 char *tmp;
27776 bool saved_integral_constant_expression_p;
27777 bool saved_non_integral_constant_expression_p;
27779 /* If it's a `...', then we are computing the length of a parameter
27780 pack. */
27781 if (keyword == RID_SIZEOF
27782 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27783 return cp_parser_sizeof_pack (parser);
27785 /* Types cannot be defined in a `sizeof' expression. Save away the
27786 old message. */
27787 saved_message = parser->type_definition_forbidden_message;
27788 /* And create the new one. */
27789 tmp = concat ("types may not be defined in %<",
27790 IDENTIFIER_POINTER (ridpointers[keyword]),
27791 "%> expressions", NULL);
27792 parser->type_definition_forbidden_message = tmp;
27794 /* The restrictions on constant-expressions do not apply inside
27795 sizeof expressions. */
27796 saved_integral_constant_expression_p
27797 = parser->integral_constant_expression_p;
27798 saved_non_integral_constant_expression_p
27799 = parser->non_integral_constant_expression_p;
27800 parser->integral_constant_expression_p = false;
27802 /* Do not actually evaluate the expression. */
27803 ++cp_unevaluated_operand;
27804 ++c_inhibit_evaluation_warnings;
27805 /* If it's a `(', then we might be looking at the type-id
27806 construction. */
27807 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27809 tree type = NULL_TREE;
27811 /* We can't be sure yet whether we're looking at a type-id or an
27812 expression. */
27813 cp_parser_parse_tentatively (parser);
27815 matching_parens parens;
27816 parens.consume_open (parser);
27818 /* Note: as a GNU Extension, compound literals are considered
27819 postfix-expressions as they are in C99, so they are valid
27820 arguments to sizeof. See comment in cp_parser_cast_expression
27821 for details. */
27822 if (cp_parser_compound_literal_p (parser))
27823 cp_parser_simulate_error (parser);
27824 else
27826 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27827 parser->in_type_id_in_expr_p = true;
27828 /* Look for the type-id. */
27829 type = cp_parser_type_id (parser);
27830 /* Look for the closing `)'. */
27831 parens.require_close (parser);
27832 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27835 /* If all went well, then we're done. */
27836 if (cp_parser_parse_definitely (parser))
27838 cp_decl_specifier_seq decl_specs;
27840 /* Build a trivial decl-specifier-seq. */
27841 clear_decl_specs (&decl_specs);
27842 decl_specs.type = type;
27844 /* Call grokdeclarator to figure out what type this is. */
27845 expr = grokdeclarator (NULL,
27846 &decl_specs,
27847 TYPENAME,
27848 /*initialized=*/0,
27849 /*attrlist=*/NULL);
27853 /* If the type-id production did not work out, then we must be
27854 looking at the unary-expression production. */
27855 if (!expr)
27856 expr = cp_parser_unary_expression (parser);
27858 /* Go back to evaluating expressions. */
27859 --cp_unevaluated_operand;
27860 --c_inhibit_evaluation_warnings;
27862 /* Free the message we created. */
27863 free (tmp);
27864 /* And restore the old one. */
27865 parser->type_definition_forbidden_message = saved_message;
27866 parser->integral_constant_expression_p
27867 = saved_integral_constant_expression_p;
27868 parser->non_integral_constant_expression_p
27869 = saved_non_integral_constant_expression_p;
27871 return expr;
27874 /* If the current declaration has no declarator, return true. */
27876 static bool
27877 cp_parser_declares_only_class_p (cp_parser *parser)
27879 /* If the next token is a `;' or a `,' then there is no
27880 declarator. */
27881 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27882 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27885 /* Update the DECL_SPECS to reflect the storage class indicated by
27886 KEYWORD. */
27888 static void
27889 cp_parser_set_storage_class (cp_parser *parser,
27890 cp_decl_specifier_seq *decl_specs,
27891 enum rid keyword,
27892 cp_token *token)
27894 cp_storage_class storage_class;
27896 if (parser->in_unbraced_linkage_specification_p)
27898 error_at (token->location, "invalid use of %qD in linkage specification",
27899 ridpointers[keyword]);
27900 return;
27902 else if (decl_specs->storage_class != sc_none)
27904 decl_specs->conflicting_specifiers_p = true;
27905 return;
27908 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27909 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27910 && decl_specs->gnu_thread_keyword_p)
27912 pedwarn (decl_specs->locations[ds_thread], 0,
27913 "%<__thread%> before %qD", ridpointers[keyword]);
27916 switch (keyword)
27918 case RID_AUTO:
27919 storage_class = sc_auto;
27920 break;
27921 case RID_REGISTER:
27922 storage_class = sc_register;
27923 break;
27924 case RID_STATIC:
27925 storage_class = sc_static;
27926 break;
27927 case RID_EXTERN:
27928 storage_class = sc_extern;
27929 break;
27930 case RID_MUTABLE:
27931 storage_class = sc_mutable;
27932 break;
27933 default:
27934 gcc_unreachable ();
27936 decl_specs->storage_class = storage_class;
27937 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27939 /* A storage class specifier cannot be applied alongside a typedef
27940 specifier. If there is a typedef specifier present then set
27941 conflicting_specifiers_p which will trigger an error later
27942 on in grokdeclarator. */
27943 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27944 decl_specs->conflicting_specifiers_p = true;
27947 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27948 is true, the type is a class or enum definition. */
27950 static void
27951 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27952 tree type_spec,
27953 cp_token *token,
27954 bool type_definition_p)
27956 decl_specs->any_specifiers_p = true;
27958 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27959 (with, for example, in "typedef int wchar_t;") we remember that
27960 this is what happened. In system headers, we ignore these
27961 declarations so that G++ can work with system headers that are not
27962 C++-safe. */
27963 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27964 && !type_definition_p
27965 && (type_spec == boolean_type_node
27966 || type_spec == char16_type_node
27967 || type_spec == char32_type_node
27968 || type_spec == wchar_type_node)
27969 && (decl_specs->type
27970 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27971 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27972 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27973 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27975 decl_specs->redefined_builtin_type = type_spec;
27976 set_and_check_decl_spec_loc (decl_specs,
27977 ds_redefined_builtin_type_spec,
27978 token);
27979 if (!decl_specs->type)
27981 decl_specs->type = type_spec;
27982 decl_specs->type_definition_p = false;
27983 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27986 else if (decl_specs->type)
27987 decl_specs->multiple_types_p = true;
27988 else
27990 decl_specs->type = type_spec;
27991 decl_specs->type_definition_p = type_definition_p;
27992 decl_specs->redefined_builtin_type = NULL_TREE;
27993 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27997 /* True iff TOKEN is the GNU keyword __thread. */
27999 static bool
28000 token_is__thread (cp_token *token)
28002 gcc_assert (token->keyword == RID_THREAD);
28003 return id_equal (token->u.value, "__thread");
28006 /* Set the location for a declarator specifier and check if it is
28007 duplicated.
28009 DECL_SPECS is the sequence of declarator specifiers onto which to
28010 set the location.
28012 DS is the single declarator specifier to set which location is to
28013 be set onto the existing sequence of declarators.
28015 LOCATION is the location for the declarator specifier to
28016 consider. */
28018 static void
28019 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28020 cp_decl_spec ds, cp_token *token)
28022 gcc_assert (ds < ds_last);
28024 if (decl_specs == NULL)
28025 return;
28027 source_location location = token->location;
28029 if (decl_specs->locations[ds] == 0)
28031 decl_specs->locations[ds] = location;
28032 if (ds == ds_thread)
28033 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28035 else
28037 if (ds == ds_long)
28039 if (decl_specs->locations[ds_long_long] != 0)
28040 error_at (location,
28041 "%<long long long%> is too long for GCC");
28042 else
28044 decl_specs->locations[ds_long_long] = location;
28045 pedwarn_cxx98 (location,
28046 OPT_Wlong_long,
28047 "ISO C++ 1998 does not support %<long long%>");
28050 else if (ds == ds_thread)
28052 bool gnu = token_is__thread (token);
28053 if (gnu != decl_specs->gnu_thread_keyword_p)
28054 error_at (location,
28055 "both %<__thread%> and %<thread_local%> specified");
28056 else
28058 gcc_rich_location richloc (location);
28059 richloc.add_fixit_remove ();
28060 error_at (&richloc, "duplicate %qD", token->u.value);
28063 else
28065 static const char *const decl_spec_names[] = {
28066 "signed",
28067 "unsigned",
28068 "short",
28069 "long",
28070 "const",
28071 "volatile",
28072 "restrict",
28073 "inline",
28074 "virtual",
28075 "explicit",
28076 "friend",
28077 "typedef",
28078 "using",
28079 "constexpr",
28080 "__complex"
28082 gcc_rich_location richloc (location);
28083 richloc.add_fixit_remove ();
28084 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28089 /* Return true iff the declarator specifier DS is present in the
28090 sequence of declarator specifiers DECL_SPECS. */
28092 bool
28093 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28094 cp_decl_spec ds)
28096 gcc_assert (ds < ds_last);
28098 if (decl_specs == NULL)
28099 return false;
28101 return decl_specs->locations[ds] != 0;
28104 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28105 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28107 static bool
28108 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28110 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28113 /* Issue an error message indicating that TOKEN_DESC was expected.
28114 If KEYWORD is true, it indicated this function is called by
28115 cp_parser_require_keword and the required token can only be
28116 a indicated keyword.
28118 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28119 within any error as the location of an "opening" token matching
28120 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28121 RT_CLOSE_PAREN). */
28123 static void
28124 cp_parser_required_error (cp_parser *parser,
28125 required_token token_desc,
28126 bool keyword,
28127 location_t matching_location)
28129 if (cp_parser_simulate_error (parser))
28130 return;
28132 const char *gmsgid = NULL;
28133 switch (token_desc)
28135 case RT_NEW:
28136 gmsgid = G_("expected %<new%>");
28137 break;
28138 case RT_DELETE:
28139 gmsgid = G_("expected %<delete%>");
28140 break;
28141 case RT_RETURN:
28142 gmsgid = G_("expected %<return%>");
28143 break;
28144 case RT_WHILE:
28145 gmsgid = G_("expected %<while%>");
28146 break;
28147 case RT_EXTERN:
28148 gmsgid = G_("expected %<extern%>");
28149 break;
28150 case RT_STATIC_ASSERT:
28151 gmsgid = G_("expected %<static_assert%>");
28152 break;
28153 case RT_DECLTYPE:
28154 gmsgid = G_("expected %<decltype%>");
28155 break;
28156 case RT_OPERATOR:
28157 gmsgid = G_("expected %<operator%>");
28158 break;
28159 case RT_CLASS:
28160 gmsgid = G_("expected %<class%>");
28161 break;
28162 case RT_TEMPLATE:
28163 gmsgid = G_("expected %<template%>");
28164 break;
28165 case RT_NAMESPACE:
28166 gmsgid = G_("expected %<namespace%>");
28167 break;
28168 case RT_USING:
28169 gmsgid = G_("expected %<using%>");
28170 break;
28171 case RT_ASM:
28172 gmsgid = G_("expected %<asm%>");
28173 break;
28174 case RT_TRY:
28175 gmsgid = G_("expected %<try%>");
28176 break;
28177 case RT_CATCH:
28178 gmsgid = G_("expected %<catch%>");
28179 break;
28180 case RT_THROW:
28181 gmsgid = G_("expected %<throw%>");
28182 break;
28183 case RT_LABEL:
28184 gmsgid = G_("expected %<__label__%>");
28185 break;
28186 case RT_AT_TRY:
28187 gmsgid = G_("expected %<@try%>");
28188 break;
28189 case RT_AT_SYNCHRONIZED:
28190 gmsgid = G_("expected %<@synchronized%>");
28191 break;
28192 case RT_AT_THROW:
28193 gmsgid = G_("expected %<@throw%>");
28194 break;
28195 case RT_TRANSACTION_ATOMIC:
28196 gmsgid = G_("expected %<__transaction_atomic%>");
28197 break;
28198 case RT_TRANSACTION_RELAXED:
28199 gmsgid = G_("expected %<__transaction_relaxed%>");
28200 break;
28201 default:
28202 break;
28205 if (!gmsgid && !keyword)
28207 switch (token_desc)
28209 case RT_SEMICOLON:
28210 gmsgid = G_("expected %<;%>");
28211 break;
28212 case RT_OPEN_PAREN:
28213 gmsgid = G_("expected %<(%>");
28214 break;
28215 case RT_CLOSE_BRACE:
28216 gmsgid = G_("expected %<}%>");
28217 break;
28218 case RT_OPEN_BRACE:
28219 gmsgid = G_("expected %<{%>");
28220 break;
28221 case RT_CLOSE_SQUARE:
28222 gmsgid = G_("expected %<]%>");
28223 break;
28224 case RT_OPEN_SQUARE:
28225 gmsgid = G_("expected %<[%>");
28226 break;
28227 case RT_COMMA:
28228 gmsgid = G_("expected %<,%>");
28229 break;
28230 case RT_SCOPE:
28231 gmsgid = G_("expected %<::%>");
28232 break;
28233 case RT_LESS:
28234 gmsgid = G_("expected %<<%>");
28235 break;
28236 case RT_GREATER:
28237 gmsgid = G_("expected %<>%>");
28238 break;
28239 case RT_EQ:
28240 gmsgid = G_("expected %<=%>");
28241 break;
28242 case RT_ELLIPSIS:
28243 gmsgid = G_("expected %<...%>");
28244 break;
28245 case RT_MULT:
28246 gmsgid = G_("expected %<*%>");
28247 break;
28248 case RT_COMPL:
28249 gmsgid = G_("expected %<~%>");
28250 break;
28251 case RT_COLON:
28252 gmsgid = G_("expected %<:%>");
28253 break;
28254 case RT_COLON_SCOPE:
28255 gmsgid = G_("expected %<:%> or %<::%>");
28256 break;
28257 case RT_CLOSE_PAREN:
28258 gmsgid = G_("expected %<)%>");
28259 break;
28260 case RT_COMMA_CLOSE_PAREN:
28261 gmsgid = G_("expected %<,%> or %<)%>");
28262 break;
28263 case RT_PRAGMA_EOL:
28264 gmsgid = G_("expected end of line");
28265 break;
28266 case RT_NAME:
28267 gmsgid = G_("expected identifier");
28268 break;
28269 case RT_SELECT:
28270 gmsgid = G_("expected selection-statement");
28271 break;
28272 case RT_ITERATION:
28273 gmsgid = G_("expected iteration-statement");
28274 break;
28275 case RT_JUMP:
28276 gmsgid = G_("expected jump-statement");
28277 break;
28278 case RT_CLASS_KEY:
28279 gmsgid = G_("expected class-key");
28280 break;
28281 case RT_CLASS_TYPENAME_TEMPLATE:
28282 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28283 break;
28284 default:
28285 gcc_unreachable ();
28289 if (gmsgid)
28290 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28294 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28295 issue an error message indicating that TOKEN_DESC was expected.
28297 Returns the token consumed, if the token had the appropriate type.
28298 Otherwise, returns NULL.
28300 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28301 within any error as the location of an "opening" token matching
28302 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28303 RT_CLOSE_PAREN). */
28305 static cp_token *
28306 cp_parser_require (cp_parser* parser,
28307 enum cpp_ttype type,
28308 required_token token_desc,
28309 location_t matching_location)
28311 if (cp_lexer_next_token_is (parser->lexer, type))
28312 return cp_lexer_consume_token (parser->lexer);
28313 else
28315 /* Output the MESSAGE -- unless we're parsing tentatively. */
28316 if (!cp_parser_simulate_error (parser))
28317 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28318 matching_location);
28319 return NULL;
28323 /* An error message is produced if the next token is not '>'.
28324 All further tokens are skipped until the desired token is
28325 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28327 static void
28328 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28330 /* Current level of '< ... >'. */
28331 unsigned level = 0;
28332 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28333 unsigned nesting_depth = 0;
28335 /* Are we ready, yet? If not, issue error message. */
28336 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28337 return;
28339 /* Skip tokens until the desired token is found. */
28340 while (true)
28342 /* Peek at the next token. */
28343 switch (cp_lexer_peek_token (parser->lexer)->type)
28345 case CPP_LESS:
28346 if (!nesting_depth)
28347 ++level;
28348 break;
28350 case CPP_RSHIFT:
28351 if (cxx_dialect == cxx98)
28352 /* C++0x views the `>>' operator as two `>' tokens, but
28353 C++98 does not. */
28354 break;
28355 else if (!nesting_depth && level-- == 0)
28357 /* We've hit a `>>' where the first `>' closes the
28358 template argument list, and the second `>' is
28359 spurious. Just consume the `>>' and stop; we've
28360 already produced at least one error. */
28361 cp_lexer_consume_token (parser->lexer);
28362 return;
28364 /* Fall through for C++0x, so we handle the second `>' in
28365 the `>>'. */
28366 gcc_fallthrough ();
28368 case CPP_GREATER:
28369 if (!nesting_depth && level-- == 0)
28371 /* We've reached the token we want, consume it and stop. */
28372 cp_lexer_consume_token (parser->lexer);
28373 return;
28375 break;
28377 case CPP_OPEN_PAREN:
28378 case CPP_OPEN_SQUARE:
28379 ++nesting_depth;
28380 break;
28382 case CPP_CLOSE_PAREN:
28383 case CPP_CLOSE_SQUARE:
28384 if (nesting_depth-- == 0)
28385 return;
28386 break;
28388 case CPP_EOF:
28389 case CPP_PRAGMA_EOL:
28390 case CPP_SEMICOLON:
28391 case CPP_OPEN_BRACE:
28392 case CPP_CLOSE_BRACE:
28393 /* The '>' was probably forgotten, don't look further. */
28394 return;
28396 default:
28397 break;
28400 /* Consume this token. */
28401 cp_lexer_consume_token (parser->lexer);
28405 /* If the next token is the indicated keyword, consume it. Otherwise,
28406 issue an error message indicating that TOKEN_DESC was expected.
28408 Returns the token consumed, if the token had the appropriate type.
28409 Otherwise, returns NULL. */
28411 static cp_token *
28412 cp_parser_require_keyword (cp_parser* parser,
28413 enum rid keyword,
28414 required_token token_desc)
28416 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28418 if (token && token->keyword != keyword)
28420 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28421 UNKNOWN_LOCATION);
28422 return NULL;
28425 return token;
28428 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28429 function-definition. */
28431 static bool
28432 cp_parser_token_starts_function_definition_p (cp_token* token)
28434 return (/* An ordinary function-body begins with an `{'. */
28435 token->type == CPP_OPEN_BRACE
28436 /* A ctor-initializer begins with a `:'. */
28437 || token->type == CPP_COLON
28438 /* A function-try-block begins with `try'. */
28439 || token->keyword == RID_TRY
28440 /* A function-transaction-block begins with `__transaction_atomic'
28441 or `__transaction_relaxed'. */
28442 || token->keyword == RID_TRANSACTION_ATOMIC
28443 || token->keyword == RID_TRANSACTION_RELAXED
28444 /* The named return value extension begins with `return'. */
28445 || token->keyword == RID_RETURN);
28448 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28449 definition. */
28451 static bool
28452 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28454 cp_token *token;
28456 token = cp_lexer_peek_token (parser->lexer);
28457 return (token->type == CPP_OPEN_BRACE
28458 || (token->type == CPP_COLON
28459 && !parser->colon_doesnt_start_class_def_p));
28462 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28463 C++0x) ending a template-argument. */
28465 static bool
28466 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28468 cp_token *token;
28470 token = cp_lexer_peek_token (parser->lexer);
28471 return (token->type == CPP_COMMA
28472 || token->type == CPP_GREATER
28473 || token->type == CPP_ELLIPSIS
28474 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28477 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28478 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28480 static bool
28481 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28482 size_t n)
28484 cp_token *token;
28486 token = cp_lexer_peek_nth_token (parser->lexer, n);
28487 if (token->type == CPP_LESS)
28488 return true;
28489 /* Check for the sequence `<::' in the original code. It would be lexed as
28490 `[:', where `[' is a digraph, and there is no whitespace before
28491 `:'. */
28492 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28494 cp_token *token2;
28495 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28496 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28497 return true;
28499 return false;
28502 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28503 or none_type otherwise. */
28505 static enum tag_types
28506 cp_parser_token_is_class_key (cp_token* token)
28508 switch (token->keyword)
28510 case RID_CLASS:
28511 return class_type;
28512 case RID_STRUCT:
28513 return record_type;
28514 case RID_UNION:
28515 return union_type;
28517 default:
28518 return none_type;
28522 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28523 or none_type otherwise or if the token is null. */
28525 static enum tag_types
28526 cp_parser_token_is_type_parameter_key (cp_token* token)
28528 if (!token)
28529 return none_type;
28531 switch (token->keyword)
28533 case RID_CLASS:
28534 return class_type;
28535 case RID_TYPENAME:
28536 return typename_type;
28538 default:
28539 return none_type;
28543 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28545 static void
28546 cp_parser_check_class_key (enum tag_types class_key, tree type)
28548 if (type == error_mark_node)
28549 return;
28550 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28552 if (permerror (input_location, "%qs tag used in naming %q#T",
28553 class_key == union_type ? "union"
28554 : class_key == record_type ? "struct" : "class",
28555 type))
28556 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28557 "%q#T was previously declared here", type);
28561 /* Issue an error message if DECL is redeclared with different
28562 access than its original declaration [class.access.spec/3].
28563 This applies to nested classes, nested class templates and
28564 enumerations [class.mem/1]. */
28566 static void
28567 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28569 if (!decl
28570 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28571 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28572 return;
28574 if ((TREE_PRIVATE (decl)
28575 != (current_access_specifier == access_private_node))
28576 || (TREE_PROTECTED (decl)
28577 != (current_access_specifier == access_protected_node)))
28578 error_at (location, "%qD redeclared with different access", decl);
28581 /* Look for the `template' keyword, as a syntactic disambiguator.
28582 Return TRUE iff it is present, in which case it will be
28583 consumed. */
28585 static bool
28586 cp_parser_optional_template_keyword (cp_parser *parser)
28588 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28590 /* In C++98 the `template' keyword can only be used within templates;
28591 outside templates the parser can always figure out what is a
28592 template and what is not. In C++11, per the resolution of DR 468,
28593 `template' is allowed in cases where it is not strictly necessary. */
28594 if (!processing_template_decl
28595 && pedantic && cxx_dialect == cxx98)
28597 cp_token *token = cp_lexer_peek_token (parser->lexer);
28598 pedwarn (token->location, OPT_Wpedantic,
28599 "in C++98 %<template%> (as a disambiguator) is only "
28600 "allowed within templates");
28601 /* If this part of the token stream is rescanned, the same
28602 error message would be generated. So, we purge the token
28603 from the stream. */
28604 cp_lexer_purge_token (parser->lexer);
28605 return false;
28607 else
28609 /* Consume the `template' keyword. */
28610 cp_lexer_consume_token (parser->lexer);
28611 return true;
28614 return false;
28617 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28618 set PARSER->SCOPE, and perform other related actions. */
28620 static void
28621 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28623 struct tree_check *check_value;
28625 /* Get the stored value. */
28626 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28627 /* Set the scope from the stored value. */
28628 parser->scope = saved_checks_value (check_value);
28629 parser->qualifying_scope = check_value->qualifying_scope;
28630 parser->object_scope = NULL_TREE;
28633 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28634 encounter the end of a block before what we were looking for. */
28636 static bool
28637 cp_parser_cache_group (cp_parser *parser,
28638 enum cpp_ttype end,
28639 unsigned depth)
28641 while (true)
28643 cp_token *token = cp_lexer_peek_token (parser->lexer);
28645 /* Abort a parenthesized expression if we encounter a semicolon. */
28646 if ((end == CPP_CLOSE_PAREN || depth == 0)
28647 && token->type == CPP_SEMICOLON)
28648 return true;
28649 /* If we've reached the end of the file, stop. */
28650 if (token->type == CPP_EOF
28651 || (end != CPP_PRAGMA_EOL
28652 && token->type == CPP_PRAGMA_EOL))
28653 return true;
28654 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28655 /* We've hit the end of an enclosing block, so there's been some
28656 kind of syntax error. */
28657 return true;
28659 /* Consume the token. */
28660 cp_lexer_consume_token (parser->lexer);
28661 /* See if it starts a new group. */
28662 if (token->type == CPP_OPEN_BRACE)
28664 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28665 /* In theory this should probably check end == '}', but
28666 cp_parser_save_member_function_body needs it to exit
28667 after either '}' or ')' when called with ')'. */
28668 if (depth == 0)
28669 return false;
28671 else if (token->type == CPP_OPEN_PAREN)
28673 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28674 if (depth == 0 && end == CPP_CLOSE_PAREN)
28675 return false;
28677 else if (token->type == CPP_PRAGMA)
28678 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28679 else if (token->type == end)
28680 return false;
28684 /* Like above, for caching a default argument or NSDMI. Both of these are
28685 terminated by a non-nested comma, but it can be unclear whether or not a
28686 comma is nested in a template argument list unless we do more parsing.
28687 In order to handle this ambiguity, when we encounter a ',' after a '<'
28688 we try to parse what follows as a parameter-declaration-list (in the
28689 case of a default argument) or a member-declarator (in the case of an
28690 NSDMI). If that succeeds, then we stop caching. */
28692 static tree
28693 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28695 unsigned depth = 0;
28696 int maybe_template_id = 0;
28697 cp_token *first_token;
28698 cp_token *token;
28699 tree default_argument;
28701 /* Add tokens until we have processed the entire default
28702 argument. We add the range [first_token, token). */
28703 first_token = cp_lexer_peek_token (parser->lexer);
28704 if (first_token->type == CPP_OPEN_BRACE)
28706 /* For list-initialization, this is straightforward. */
28707 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28708 token = cp_lexer_peek_token (parser->lexer);
28710 else while (true)
28712 bool done = false;
28714 /* Peek at the next token. */
28715 token = cp_lexer_peek_token (parser->lexer);
28716 /* What we do depends on what token we have. */
28717 switch (token->type)
28719 /* In valid code, a default argument must be
28720 immediately followed by a `,' `)', or `...'. */
28721 case CPP_COMMA:
28722 if (depth == 0 && maybe_template_id)
28724 /* If we've seen a '<', we might be in a
28725 template-argument-list. Until Core issue 325 is
28726 resolved, we don't know how this situation ought
28727 to be handled, so try to DTRT. We check whether
28728 what comes after the comma is a valid parameter
28729 declaration list. If it is, then the comma ends
28730 the default argument; otherwise the default
28731 argument continues. */
28732 bool error = false;
28733 cp_token *peek;
28735 /* Set ITALP so cp_parser_parameter_declaration_list
28736 doesn't decide to commit to this parse. */
28737 bool saved_italp = parser->in_template_argument_list_p;
28738 parser->in_template_argument_list_p = true;
28740 cp_parser_parse_tentatively (parser);
28742 if (nsdmi)
28744 /* Parse declarators until we reach a non-comma or
28745 somthing that cannot be an initializer.
28746 Just checking whether we're looking at a single
28747 declarator is insufficient. Consider:
28748 int var = tuple<T,U>::x;
28749 The template parameter 'U' looks exactly like a
28750 declarator. */
28753 int ctor_dtor_or_conv_p;
28754 cp_lexer_consume_token (parser->lexer);
28755 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28756 &ctor_dtor_or_conv_p,
28757 /*parenthesized_p=*/NULL,
28758 /*member_p=*/true,
28759 /*friend_p=*/false);
28760 peek = cp_lexer_peek_token (parser->lexer);
28761 if (cp_parser_error_occurred (parser))
28762 break;
28764 while (peek->type == CPP_COMMA);
28765 /* If we met an '=' or ';' then the original comma
28766 was the end of the NSDMI. Otherwise assume
28767 we're still in the NSDMI. */
28768 error = (peek->type != CPP_EQ
28769 && peek->type != CPP_SEMICOLON);
28771 else
28773 cp_lexer_consume_token (parser->lexer);
28774 begin_scope (sk_function_parms, NULL_TREE);
28775 cp_parser_parameter_declaration_list (parser, &error);
28776 pop_bindings_and_leave_scope ();
28778 if (!cp_parser_error_occurred (parser) && !error)
28779 done = true;
28780 cp_parser_abort_tentative_parse (parser);
28782 parser->in_template_argument_list_p = saved_italp;
28783 break;
28785 /* FALLTHRU */
28786 case CPP_CLOSE_PAREN:
28787 case CPP_ELLIPSIS:
28788 /* If we run into a non-nested `;', `}', or `]',
28789 then the code is invalid -- but the default
28790 argument is certainly over. */
28791 case CPP_SEMICOLON:
28792 case CPP_CLOSE_BRACE:
28793 case CPP_CLOSE_SQUARE:
28794 if (depth == 0
28795 /* Handle correctly int n = sizeof ... ( p ); */
28796 && token->type != CPP_ELLIPSIS)
28797 done = true;
28798 /* Update DEPTH, if necessary. */
28799 else if (token->type == CPP_CLOSE_PAREN
28800 || token->type == CPP_CLOSE_BRACE
28801 || token->type == CPP_CLOSE_SQUARE)
28802 --depth;
28803 break;
28805 case CPP_OPEN_PAREN:
28806 case CPP_OPEN_SQUARE:
28807 case CPP_OPEN_BRACE:
28808 ++depth;
28809 break;
28811 case CPP_LESS:
28812 if (depth == 0)
28813 /* This might be the comparison operator, or it might
28814 start a template argument list. */
28815 ++maybe_template_id;
28816 break;
28818 case CPP_RSHIFT:
28819 if (cxx_dialect == cxx98)
28820 break;
28821 /* Fall through for C++0x, which treats the `>>'
28822 operator like two `>' tokens in certain
28823 cases. */
28824 gcc_fallthrough ();
28826 case CPP_GREATER:
28827 if (depth == 0)
28829 /* This might be an operator, or it might close a
28830 template argument list. But if a previous '<'
28831 started a template argument list, this will have
28832 closed it, so we can't be in one anymore. */
28833 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28834 if (maybe_template_id < 0)
28835 maybe_template_id = 0;
28837 break;
28839 /* If we run out of tokens, issue an error message. */
28840 case CPP_EOF:
28841 case CPP_PRAGMA_EOL:
28842 error_at (token->location, "file ends in default argument");
28843 return error_mark_node;
28845 case CPP_NAME:
28846 case CPP_SCOPE:
28847 /* In these cases, we should look for template-ids.
28848 For example, if the default argument is
28849 `X<int, double>()', we need to do name lookup to
28850 figure out whether or not `X' is a template; if
28851 so, the `,' does not end the default argument.
28853 That is not yet done. */
28854 break;
28856 default:
28857 break;
28860 /* If we've reached the end, stop. */
28861 if (done)
28862 break;
28864 /* Add the token to the token block. */
28865 token = cp_lexer_consume_token (parser->lexer);
28868 /* Create a DEFAULT_ARG to represent the unparsed default
28869 argument. */
28870 default_argument = make_node (DEFAULT_ARG);
28871 DEFARG_TOKENS (default_argument)
28872 = cp_token_cache_new (first_token, token);
28873 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28875 return default_argument;
28878 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28880 location_t
28881 defarg_location (tree default_argument)
28883 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
28884 location_t start = tokens->first->location;
28885 location_t end = tokens->last->location;
28886 return make_location (start, start, end);
28889 /* Begin parsing tentatively. We always save tokens while parsing
28890 tentatively so that if the tentative parsing fails we can restore the
28891 tokens. */
28893 static void
28894 cp_parser_parse_tentatively (cp_parser* parser)
28896 /* Enter a new parsing context. */
28897 parser->context = cp_parser_context_new (parser->context);
28898 /* Begin saving tokens. */
28899 cp_lexer_save_tokens (parser->lexer);
28900 /* In order to avoid repetitive access control error messages,
28901 access checks are queued up until we are no longer parsing
28902 tentatively. */
28903 push_deferring_access_checks (dk_deferred);
28906 /* Commit to the currently active tentative parse. */
28908 static void
28909 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28911 cp_parser_context *context;
28912 cp_lexer *lexer;
28914 /* Mark all of the levels as committed. */
28915 lexer = parser->lexer;
28916 for (context = parser->context; context->next; context = context->next)
28918 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28919 break;
28920 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28921 while (!cp_lexer_saving_tokens (lexer))
28922 lexer = lexer->next;
28923 cp_lexer_commit_tokens (lexer);
28927 /* Commit to the topmost currently active tentative parse.
28929 Note that this function shouldn't be called when there are
28930 irreversible side-effects while in a tentative state. For
28931 example, we shouldn't create a permanent entry in the symbol
28932 table, or issue an error message that might not apply if the
28933 tentative parse is aborted. */
28935 static void
28936 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28938 cp_parser_context *context = parser->context;
28939 cp_lexer *lexer = parser->lexer;
28941 if (context)
28943 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28944 return;
28945 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28947 while (!cp_lexer_saving_tokens (lexer))
28948 lexer = lexer->next;
28949 cp_lexer_commit_tokens (lexer);
28953 /* Abort the currently active tentative parse. All consumed tokens
28954 will be rolled back, and no diagnostics will be issued. */
28956 static void
28957 cp_parser_abort_tentative_parse (cp_parser* parser)
28959 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28960 || errorcount > 0);
28961 cp_parser_simulate_error (parser);
28962 /* Now, pretend that we want to see if the construct was
28963 successfully parsed. */
28964 cp_parser_parse_definitely (parser);
28967 /* Stop parsing tentatively. If a parse error has occurred, restore the
28968 token stream. Otherwise, commit to the tokens we have consumed.
28969 Returns true if no error occurred; false otherwise. */
28971 static bool
28972 cp_parser_parse_definitely (cp_parser* parser)
28974 bool error_occurred;
28975 cp_parser_context *context;
28977 /* Remember whether or not an error occurred, since we are about to
28978 destroy that information. */
28979 error_occurred = cp_parser_error_occurred (parser);
28980 /* Remove the topmost context from the stack. */
28981 context = parser->context;
28982 parser->context = context->next;
28983 /* If no parse errors occurred, commit to the tentative parse. */
28984 if (!error_occurred)
28986 /* Commit to the tokens read tentatively, unless that was
28987 already done. */
28988 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28989 cp_lexer_commit_tokens (parser->lexer);
28991 pop_to_parent_deferring_access_checks ();
28993 /* Otherwise, if errors occurred, roll back our state so that things
28994 are just as they were before we began the tentative parse. */
28995 else
28997 cp_lexer_rollback_tokens (parser->lexer);
28998 pop_deferring_access_checks ();
29000 /* Add the context to the front of the free list. */
29001 context->next = cp_parser_context_free_list;
29002 cp_parser_context_free_list = context;
29004 return !error_occurred;
29007 /* Returns true if we are parsing tentatively and are not committed to
29008 this tentative parse. */
29010 static bool
29011 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29013 return (cp_parser_parsing_tentatively (parser)
29014 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29017 /* Returns nonzero iff an error has occurred during the most recent
29018 tentative parse. */
29020 static bool
29021 cp_parser_error_occurred (cp_parser* parser)
29023 return (cp_parser_parsing_tentatively (parser)
29024 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29027 /* Returns nonzero if GNU extensions are allowed. */
29029 static bool
29030 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29032 return parser->allow_gnu_extensions_p;
29035 /* Objective-C++ Productions */
29038 /* Parse an Objective-C expression, which feeds into a primary-expression
29039 above.
29041 objc-expression:
29042 objc-message-expression
29043 objc-string-literal
29044 objc-encode-expression
29045 objc-protocol-expression
29046 objc-selector-expression
29048 Returns a tree representation of the expression. */
29050 static cp_expr
29051 cp_parser_objc_expression (cp_parser* parser)
29053 /* Try to figure out what kind of declaration is present. */
29054 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29056 switch (kwd->type)
29058 case CPP_OPEN_SQUARE:
29059 return cp_parser_objc_message_expression (parser);
29061 case CPP_OBJC_STRING:
29062 kwd = cp_lexer_consume_token (parser->lexer);
29063 return objc_build_string_object (kwd->u.value);
29065 case CPP_KEYWORD:
29066 switch (kwd->keyword)
29068 case RID_AT_ENCODE:
29069 return cp_parser_objc_encode_expression (parser);
29071 case RID_AT_PROTOCOL:
29072 return cp_parser_objc_protocol_expression (parser);
29074 case RID_AT_SELECTOR:
29075 return cp_parser_objc_selector_expression (parser);
29077 default:
29078 break;
29080 /* FALLTHRU */
29081 default:
29082 error_at (kwd->location,
29083 "misplaced %<@%D%> Objective-C++ construct",
29084 kwd->u.value);
29085 cp_parser_skip_to_end_of_block_or_statement (parser);
29088 return error_mark_node;
29091 /* Parse an Objective-C message expression.
29093 objc-message-expression:
29094 [ objc-message-receiver objc-message-args ]
29096 Returns a representation of an Objective-C message. */
29098 static tree
29099 cp_parser_objc_message_expression (cp_parser* parser)
29101 tree receiver, messageargs;
29103 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29104 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29105 receiver = cp_parser_objc_message_receiver (parser);
29106 messageargs = cp_parser_objc_message_args (parser);
29107 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29108 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29110 tree result = objc_build_message_expr (receiver, messageargs);
29112 /* Construct a location e.g.
29113 [self func1:5]
29114 ^~~~~~~~~~~~~~
29115 ranging from the '[' to the ']', with the caret at the start. */
29116 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29117 protected_set_expr_location (result, combined_loc);
29119 return result;
29122 /* Parse an objc-message-receiver.
29124 objc-message-receiver:
29125 expression
29126 simple-type-specifier
29128 Returns a representation of the type or expression. */
29130 static tree
29131 cp_parser_objc_message_receiver (cp_parser* parser)
29133 tree rcv;
29135 /* An Objective-C message receiver may be either (1) a type
29136 or (2) an expression. */
29137 cp_parser_parse_tentatively (parser);
29138 rcv = cp_parser_expression (parser);
29140 /* If that worked out, fine. */
29141 if (cp_parser_parse_definitely (parser))
29142 return rcv;
29144 cp_parser_parse_tentatively (parser);
29145 rcv = cp_parser_simple_type_specifier (parser,
29146 /*decl_specs=*/NULL,
29147 CP_PARSER_FLAGS_NONE);
29149 if (cp_parser_parse_definitely (parser))
29150 return objc_get_class_reference (rcv);
29152 cp_parser_error (parser, "objective-c++ message receiver expected");
29153 return error_mark_node;
29156 /* Parse the arguments and selectors comprising an Objective-C message.
29158 objc-message-args:
29159 objc-selector
29160 objc-selector-args
29161 objc-selector-args , objc-comma-args
29163 objc-selector-args:
29164 objc-selector [opt] : assignment-expression
29165 objc-selector-args objc-selector [opt] : assignment-expression
29167 objc-comma-args:
29168 assignment-expression
29169 objc-comma-args , assignment-expression
29171 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29172 selector arguments and TREE_VALUE containing a list of comma
29173 arguments. */
29175 static tree
29176 cp_parser_objc_message_args (cp_parser* parser)
29178 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29179 bool maybe_unary_selector_p = true;
29180 cp_token *token = cp_lexer_peek_token (parser->lexer);
29182 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29184 tree selector = NULL_TREE, arg;
29186 if (token->type != CPP_COLON)
29187 selector = cp_parser_objc_selector (parser);
29189 /* Detect if we have a unary selector. */
29190 if (maybe_unary_selector_p
29191 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29192 return build_tree_list (selector, NULL_TREE);
29194 maybe_unary_selector_p = false;
29195 cp_parser_require (parser, CPP_COLON, RT_COLON);
29196 arg = cp_parser_assignment_expression (parser);
29198 sel_args
29199 = chainon (sel_args,
29200 build_tree_list (selector, arg));
29202 token = cp_lexer_peek_token (parser->lexer);
29205 /* Handle non-selector arguments, if any. */
29206 while (token->type == CPP_COMMA)
29208 tree arg;
29210 cp_lexer_consume_token (parser->lexer);
29211 arg = cp_parser_assignment_expression (parser);
29213 addl_args
29214 = chainon (addl_args,
29215 build_tree_list (NULL_TREE, arg));
29217 token = cp_lexer_peek_token (parser->lexer);
29220 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29222 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29223 return build_tree_list (error_mark_node, error_mark_node);
29226 return build_tree_list (sel_args, addl_args);
29229 /* Parse an Objective-C encode expression.
29231 objc-encode-expression:
29232 @encode objc-typename
29234 Returns an encoded representation of the type argument. */
29236 static cp_expr
29237 cp_parser_objc_encode_expression (cp_parser* parser)
29239 tree type;
29240 cp_token *token;
29241 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29243 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29244 matching_parens parens;
29245 parens.require_open (parser);
29246 token = cp_lexer_peek_token (parser->lexer);
29247 type = complete_type (cp_parser_type_id (parser));
29248 parens.require_close (parser);
29250 if (!type)
29252 error_at (token->location,
29253 "%<@encode%> must specify a type as an argument");
29254 return error_mark_node;
29257 /* This happens if we find @encode(T) (where T is a template
29258 typename or something dependent on a template typename) when
29259 parsing a template. In that case, we can't compile it
29260 immediately, but we rather create an AT_ENCODE_EXPR which will
29261 need to be instantiated when the template is used.
29263 if (dependent_type_p (type))
29265 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29266 TREE_READONLY (value) = 1;
29267 return value;
29271 /* Build a location of the form:
29272 @encode(int)
29273 ^~~~~~~~~~~~
29274 with caret==start at the @ token, finishing at the close paren. */
29275 location_t combined_loc
29276 = make_location (start_loc, start_loc,
29277 cp_lexer_previous_token (parser->lexer)->location);
29279 return cp_expr (objc_build_encode_expr (type), combined_loc);
29282 /* Parse an Objective-C @defs expression. */
29284 static tree
29285 cp_parser_objc_defs_expression (cp_parser *parser)
29287 tree name;
29289 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29290 matching_parens parens;
29291 parens.require_open (parser);
29292 name = cp_parser_identifier (parser);
29293 parens.require_close (parser);
29295 return objc_get_class_ivars (name);
29298 /* Parse an Objective-C protocol expression.
29300 objc-protocol-expression:
29301 @protocol ( identifier )
29303 Returns a representation of the protocol expression. */
29305 static tree
29306 cp_parser_objc_protocol_expression (cp_parser* parser)
29308 tree proto;
29309 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29311 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29312 matching_parens parens;
29313 parens.require_open (parser);
29314 proto = cp_parser_identifier (parser);
29315 parens.require_close (parser);
29317 /* Build a location of the form:
29318 @protocol(prot)
29319 ^~~~~~~~~~~~~~~
29320 with caret==start at the @ token, finishing at the close paren. */
29321 location_t combined_loc
29322 = make_location (start_loc, start_loc,
29323 cp_lexer_previous_token (parser->lexer)->location);
29324 tree result = objc_build_protocol_expr (proto);
29325 protected_set_expr_location (result, combined_loc);
29326 return result;
29329 /* Parse an Objective-C selector expression.
29331 objc-selector-expression:
29332 @selector ( objc-method-signature )
29334 objc-method-signature:
29335 objc-selector
29336 objc-selector-seq
29338 objc-selector-seq:
29339 objc-selector :
29340 objc-selector-seq objc-selector :
29342 Returns a representation of the method selector. */
29344 static tree
29345 cp_parser_objc_selector_expression (cp_parser* parser)
29347 tree sel_seq = NULL_TREE;
29348 bool maybe_unary_selector_p = true;
29349 cp_token *token;
29350 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29352 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29353 matching_parens parens;
29354 parens.require_open (parser);
29355 token = cp_lexer_peek_token (parser->lexer);
29357 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29358 || token->type == CPP_SCOPE)
29360 tree selector = NULL_TREE;
29362 if (token->type != CPP_COLON
29363 || token->type == CPP_SCOPE)
29364 selector = cp_parser_objc_selector (parser);
29366 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29367 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29369 /* Detect if we have a unary selector. */
29370 if (maybe_unary_selector_p)
29372 sel_seq = selector;
29373 goto finish_selector;
29375 else
29377 cp_parser_error (parser, "expected %<:%>");
29380 maybe_unary_selector_p = false;
29381 token = cp_lexer_consume_token (parser->lexer);
29383 if (token->type == CPP_SCOPE)
29385 sel_seq
29386 = chainon (sel_seq,
29387 build_tree_list (selector, NULL_TREE));
29388 sel_seq
29389 = chainon (sel_seq,
29390 build_tree_list (NULL_TREE, NULL_TREE));
29392 else
29393 sel_seq
29394 = chainon (sel_seq,
29395 build_tree_list (selector, NULL_TREE));
29397 token = cp_lexer_peek_token (parser->lexer);
29400 finish_selector:
29401 parens.require_close (parser);
29404 /* Build a location of the form:
29405 @selector(func)
29406 ^~~~~~~~~~~~~~~
29407 with caret==start at the @ token, finishing at the close paren. */
29408 location_t combined_loc
29409 = make_location (loc, loc,
29410 cp_lexer_previous_token (parser->lexer)->location);
29411 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29412 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29413 protected_set_expr_location (result, combined_loc);
29414 return result;
29417 /* Parse a list of identifiers.
29419 objc-identifier-list:
29420 identifier
29421 objc-identifier-list , identifier
29423 Returns a TREE_LIST of identifier nodes. */
29425 static tree
29426 cp_parser_objc_identifier_list (cp_parser* parser)
29428 tree identifier;
29429 tree list;
29430 cp_token *sep;
29432 identifier = cp_parser_identifier (parser);
29433 if (identifier == error_mark_node)
29434 return error_mark_node;
29436 list = build_tree_list (NULL_TREE, identifier);
29437 sep = cp_lexer_peek_token (parser->lexer);
29439 while (sep->type == CPP_COMMA)
29441 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29442 identifier = cp_parser_identifier (parser);
29443 if (identifier == error_mark_node)
29444 return list;
29446 list = chainon (list, build_tree_list (NULL_TREE,
29447 identifier));
29448 sep = cp_lexer_peek_token (parser->lexer);
29451 return list;
29454 /* Parse an Objective-C alias declaration.
29456 objc-alias-declaration:
29457 @compatibility_alias identifier identifier ;
29459 This function registers the alias mapping with the Objective-C front end.
29460 It returns nothing. */
29462 static void
29463 cp_parser_objc_alias_declaration (cp_parser* parser)
29465 tree alias, orig;
29467 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29468 alias = cp_parser_identifier (parser);
29469 orig = cp_parser_identifier (parser);
29470 objc_declare_alias (alias, orig);
29471 cp_parser_consume_semicolon_at_end_of_statement (parser);
29474 /* Parse an Objective-C class forward-declaration.
29476 objc-class-declaration:
29477 @class objc-identifier-list ;
29479 The function registers the forward declarations with the Objective-C
29480 front end. It returns nothing. */
29482 static void
29483 cp_parser_objc_class_declaration (cp_parser* parser)
29485 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29486 while (true)
29488 tree id;
29490 id = cp_parser_identifier (parser);
29491 if (id == error_mark_node)
29492 break;
29494 objc_declare_class (id);
29496 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29497 cp_lexer_consume_token (parser->lexer);
29498 else
29499 break;
29501 cp_parser_consume_semicolon_at_end_of_statement (parser);
29504 /* Parse a list of Objective-C protocol references.
29506 objc-protocol-refs-opt:
29507 objc-protocol-refs [opt]
29509 objc-protocol-refs:
29510 < objc-identifier-list >
29512 Returns a TREE_LIST of identifiers, if any. */
29514 static tree
29515 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29517 tree protorefs = NULL_TREE;
29519 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29521 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29522 protorefs = cp_parser_objc_identifier_list (parser);
29523 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29526 return protorefs;
29529 /* Parse a Objective-C visibility specification. */
29531 static void
29532 cp_parser_objc_visibility_spec (cp_parser* parser)
29534 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29536 switch (vis->keyword)
29538 case RID_AT_PRIVATE:
29539 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29540 break;
29541 case RID_AT_PROTECTED:
29542 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29543 break;
29544 case RID_AT_PUBLIC:
29545 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29546 break;
29547 case RID_AT_PACKAGE:
29548 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29549 break;
29550 default:
29551 return;
29554 /* Eat '@private'/'@protected'/'@public'. */
29555 cp_lexer_consume_token (parser->lexer);
29558 /* Parse an Objective-C method type. Return 'true' if it is a class
29559 (+) method, and 'false' if it is an instance (-) method. */
29561 static inline bool
29562 cp_parser_objc_method_type (cp_parser* parser)
29564 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29565 return true;
29566 else
29567 return false;
29570 /* Parse an Objective-C protocol qualifier. */
29572 static tree
29573 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29575 tree quals = NULL_TREE, node;
29576 cp_token *token = cp_lexer_peek_token (parser->lexer);
29578 node = token->u.value;
29580 while (node && identifier_p (node)
29581 && (node == ridpointers [(int) RID_IN]
29582 || node == ridpointers [(int) RID_OUT]
29583 || node == ridpointers [(int) RID_INOUT]
29584 || node == ridpointers [(int) RID_BYCOPY]
29585 || node == ridpointers [(int) RID_BYREF]
29586 || node == ridpointers [(int) RID_ONEWAY]))
29588 quals = tree_cons (NULL_TREE, node, quals);
29589 cp_lexer_consume_token (parser->lexer);
29590 token = cp_lexer_peek_token (parser->lexer);
29591 node = token->u.value;
29594 return quals;
29597 /* Parse an Objective-C typename. */
29599 static tree
29600 cp_parser_objc_typename (cp_parser* parser)
29602 tree type_name = NULL_TREE;
29604 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29606 tree proto_quals, cp_type = NULL_TREE;
29608 matching_parens parens;
29609 parens.consume_open (parser); /* Eat '('. */
29610 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29612 /* An ObjC type name may consist of just protocol qualifiers, in which
29613 case the type shall default to 'id'. */
29614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29616 cp_type = cp_parser_type_id (parser);
29618 /* If the type could not be parsed, an error has already
29619 been produced. For error recovery, behave as if it had
29620 not been specified, which will use the default type
29621 'id'. */
29622 if (cp_type == error_mark_node)
29624 cp_type = NULL_TREE;
29625 /* We need to skip to the closing parenthesis as
29626 cp_parser_type_id() does not seem to do it for
29627 us. */
29628 cp_parser_skip_to_closing_parenthesis (parser,
29629 /*recovering=*/true,
29630 /*or_comma=*/false,
29631 /*consume_paren=*/false);
29635 parens.require_close (parser);
29636 type_name = build_tree_list (proto_quals, cp_type);
29639 return type_name;
29642 /* Check to see if TYPE refers to an Objective-C selector name. */
29644 static bool
29645 cp_parser_objc_selector_p (enum cpp_ttype type)
29647 return (type == CPP_NAME || type == CPP_KEYWORD
29648 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29649 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29650 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29651 || type == CPP_XOR || type == CPP_XOR_EQ);
29654 /* Parse an Objective-C selector. */
29656 static tree
29657 cp_parser_objc_selector (cp_parser* parser)
29659 cp_token *token = cp_lexer_consume_token (parser->lexer);
29661 if (!cp_parser_objc_selector_p (token->type))
29663 error_at (token->location, "invalid Objective-C++ selector name");
29664 return error_mark_node;
29667 /* C++ operator names are allowed to appear in ObjC selectors. */
29668 switch (token->type)
29670 case CPP_AND_AND: return get_identifier ("and");
29671 case CPP_AND_EQ: return get_identifier ("and_eq");
29672 case CPP_AND: return get_identifier ("bitand");
29673 case CPP_OR: return get_identifier ("bitor");
29674 case CPP_COMPL: return get_identifier ("compl");
29675 case CPP_NOT: return get_identifier ("not");
29676 case CPP_NOT_EQ: return get_identifier ("not_eq");
29677 case CPP_OR_OR: return get_identifier ("or");
29678 case CPP_OR_EQ: return get_identifier ("or_eq");
29679 case CPP_XOR: return get_identifier ("xor");
29680 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29681 default: return token->u.value;
29685 /* Parse an Objective-C params list. */
29687 static tree
29688 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29690 tree params = NULL_TREE;
29691 bool maybe_unary_selector_p = true;
29692 cp_token *token = cp_lexer_peek_token (parser->lexer);
29694 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29696 tree selector = NULL_TREE, type_name, identifier;
29697 tree parm_attr = NULL_TREE;
29699 if (token->keyword == RID_ATTRIBUTE)
29700 break;
29702 if (token->type != CPP_COLON)
29703 selector = cp_parser_objc_selector (parser);
29705 /* Detect if we have a unary selector. */
29706 if (maybe_unary_selector_p
29707 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29709 params = selector; /* Might be followed by attributes. */
29710 break;
29713 maybe_unary_selector_p = false;
29714 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29716 /* Something went quite wrong. There should be a colon
29717 here, but there is not. Stop parsing parameters. */
29718 break;
29720 type_name = cp_parser_objc_typename (parser);
29721 /* New ObjC allows attributes on parameters too. */
29722 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29723 parm_attr = cp_parser_attributes_opt (parser);
29724 identifier = cp_parser_identifier (parser);
29726 params
29727 = chainon (params,
29728 objc_build_keyword_decl (selector,
29729 type_name,
29730 identifier,
29731 parm_attr));
29733 token = cp_lexer_peek_token (parser->lexer);
29736 if (params == NULL_TREE)
29738 cp_parser_error (parser, "objective-c++ method declaration is expected");
29739 return error_mark_node;
29742 /* We allow tail attributes for the method. */
29743 if (token->keyword == RID_ATTRIBUTE)
29745 *attributes = cp_parser_attributes_opt (parser);
29746 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29747 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29748 return params;
29749 cp_parser_error (parser,
29750 "method attributes must be specified at the end");
29751 return error_mark_node;
29754 if (params == NULL_TREE)
29756 cp_parser_error (parser, "objective-c++ method declaration is expected");
29757 return error_mark_node;
29759 return params;
29762 /* Parse the non-keyword Objective-C params. */
29764 static tree
29765 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29766 tree* attributes)
29768 tree params = make_node (TREE_LIST);
29769 cp_token *token = cp_lexer_peek_token (parser->lexer);
29770 *ellipsisp = false; /* Initially, assume no ellipsis. */
29772 while (token->type == CPP_COMMA)
29774 cp_parameter_declarator *parmdecl;
29775 tree parm;
29777 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29778 token = cp_lexer_peek_token (parser->lexer);
29780 if (token->type == CPP_ELLIPSIS)
29782 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29783 *ellipsisp = true;
29784 token = cp_lexer_peek_token (parser->lexer);
29785 break;
29788 /* TODO: parse attributes for tail parameters. */
29789 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29790 parm = grokdeclarator (parmdecl->declarator,
29791 &parmdecl->decl_specifiers,
29792 PARM, /*initialized=*/0,
29793 /*attrlist=*/NULL);
29795 chainon (params, build_tree_list (NULL_TREE, parm));
29796 token = cp_lexer_peek_token (parser->lexer);
29799 /* We allow tail attributes for the method. */
29800 if (token->keyword == RID_ATTRIBUTE)
29802 if (*attributes == NULL_TREE)
29804 *attributes = cp_parser_attributes_opt (parser);
29805 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29806 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29807 return params;
29809 else
29810 /* We have an error, but parse the attributes, so that we can
29811 carry on. */
29812 *attributes = cp_parser_attributes_opt (parser);
29814 cp_parser_error (parser,
29815 "method attributes must be specified at the end");
29816 return error_mark_node;
29819 return params;
29822 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29824 static void
29825 cp_parser_objc_interstitial_code (cp_parser* parser)
29827 cp_token *token = cp_lexer_peek_token (parser->lexer);
29829 /* If the next token is `extern' and the following token is a string
29830 literal, then we have a linkage specification. */
29831 if (token->keyword == RID_EXTERN
29832 && cp_parser_is_pure_string_literal
29833 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29834 cp_parser_linkage_specification (parser);
29835 /* Handle #pragma, if any. */
29836 else if (token->type == CPP_PRAGMA)
29837 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29838 /* Allow stray semicolons. */
29839 else if (token->type == CPP_SEMICOLON)
29840 cp_lexer_consume_token (parser->lexer);
29841 /* Mark methods as optional or required, when building protocols. */
29842 else if (token->keyword == RID_AT_OPTIONAL)
29844 cp_lexer_consume_token (parser->lexer);
29845 objc_set_method_opt (true);
29847 else if (token->keyword == RID_AT_REQUIRED)
29849 cp_lexer_consume_token (parser->lexer);
29850 objc_set_method_opt (false);
29852 else if (token->keyword == RID_NAMESPACE)
29853 cp_parser_namespace_definition (parser);
29854 /* Other stray characters must generate errors. */
29855 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29857 cp_lexer_consume_token (parser->lexer);
29858 error ("stray %qs between Objective-C++ methods",
29859 token->type == CPP_OPEN_BRACE ? "{" : "}");
29861 /* Finally, try to parse a block-declaration, or a function-definition. */
29862 else
29863 cp_parser_block_declaration (parser, /*statement_p=*/false);
29866 /* Parse a method signature. */
29868 static tree
29869 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29871 tree rettype, kwdparms, optparms;
29872 bool ellipsis = false;
29873 bool is_class_method;
29875 is_class_method = cp_parser_objc_method_type (parser);
29876 rettype = cp_parser_objc_typename (parser);
29877 *attributes = NULL_TREE;
29878 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29879 if (kwdparms == error_mark_node)
29880 return error_mark_node;
29881 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29882 if (optparms == error_mark_node)
29883 return error_mark_node;
29885 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29888 static bool
29889 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29891 tree tattr;
29892 cp_lexer_save_tokens (parser->lexer);
29893 tattr = cp_parser_attributes_opt (parser);
29894 gcc_assert (tattr) ;
29896 /* If the attributes are followed by a method introducer, this is not allowed.
29897 Dump the attributes and flag the situation. */
29898 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29899 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29900 return true;
29902 /* Otherwise, the attributes introduce some interstitial code, possibly so
29903 rewind to allow that check. */
29904 cp_lexer_rollback_tokens (parser->lexer);
29905 return false;
29908 /* Parse an Objective-C method prototype list. */
29910 static void
29911 cp_parser_objc_method_prototype_list (cp_parser* parser)
29913 cp_token *token = cp_lexer_peek_token (parser->lexer);
29915 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29917 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29919 tree attributes, sig;
29920 bool is_class_method;
29921 if (token->type == CPP_PLUS)
29922 is_class_method = true;
29923 else
29924 is_class_method = false;
29925 sig = cp_parser_objc_method_signature (parser, &attributes);
29926 if (sig == error_mark_node)
29928 cp_parser_skip_to_end_of_block_or_statement (parser);
29929 token = cp_lexer_peek_token (parser->lexer);
29930 continue;
29932 objc_add_method_declaration (is_class_method, sig, attributes);
29933 cp_parser_consume_semicolon_at_end_of_statement (parser);
29935 else if (token->keyword == RID_AT_PROPERTY)
29936 cp_parser_objc_at_property_declaration (parser);
29937 else if (token->keyword == RID_ATTRIBUTE
29938 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29939 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29940 OPT_Wattributes,
29941 "prefix attributes are ignored for methods");
29942 else
29943 /* Allow for interspersed non-ObjC++ code. */
29944 cp_parser_objc_interstitial_code (parser);
29946 token = cp_lexer_peek_token (parser->lexer);
29949 if (token->type != CPP_EOF)
29950 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29951 else
29952 cp_parser_error (parser, "expected %<@end%>");
29954 objc_finish_interface ();
29957 /* Parse an Objective-C method definition list. */
29959 static void
29960 cp_parser_objc_method_definition_list (cp_parser* parser)
29962 cp_token *token = cp_lexer_peek_token (parser->lexer);
29964 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29966 tree meth;
29968 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29970 cp_token *ptk;
29971 tree sig, attribute;
29972 bool is_class_method;
29973 if (token->type == CPP_PLUS)
29974 is_class_method = true;
29975 else
29976 is_class_method = false;
29977 push_deferring_access_checks (dk_deferred);
29978 sig = cp_parser_objc_method_signature (parser, &attribute);
29979 if (sig == error_mark_node)
29981 cp_parser_skip_to_end_of_block_or_statement (parser);
29982 token = cp_lexer_peek_token (parser->lexer);
29983 continue;
29985 objc_start_method_definition (is_class_method, sig, attribute,
29986 NULL_TREE);
29988 /* For historical reasons, we accept an optional semicolon. */
29989 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29990 cp_lexer_consume_token (parser->lexer);
29992 ptk = cp_lexer_peek_token (parser->lexer);
29993 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29994 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29996 perform_deferred_access_checks (tf_warning_or_error);
29997 stop_deferring_access_checks ();
29998 meth = cp_parser_function_definition_after_declarator (parser,
29999 false);
30000 pop_deferring_access_checks ();
30001 objc_finish_method_definition (meth);
30004 /* The following case will be removed once @synthesize is
30005 completely implemented. */
30006 else if (token->keyword == RID_AT_PROPERTY)
30007 cp_parser_objc_at_property_declaration (parser);
30008 else if (token->keyword == RID_AT_SYNTHESIZE)
30009 cp_parser_objc_at_synthesize_declaration (parser);
30010 else if (token->keyword == RID_AT_DYNAMIC)
30011 cp_parser_objc_at_dynamic_declaration (parser);
30012 else if (token->keyword == RID_ATTRIBUTE
30013 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30014 warning_at (token->location, OPT_Wattributes,
30015 "prefix attributes are ignored for methods");
30016 else
30017 /* Allow for interspersed non-ObjC++ code. */
30018 cp_parser_objc_interstitial_code (parser);
30020 token = cp_lexer_peek_token (parser->lexer);
30023 if (token->type != CPP_EOF)
30024 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30025 else
30026 cp_parser_error (parser, "expected %<@end%>");
30028 objc_finish_implementation ();
30031 /* Parse Objective-C ivars. */
30033 static void
30034 cp_parser_objc_class_ivars (cp_parser* parser)
30036 cp_token *token = cp_lexer_peek_token (parser->lexer);
30038 if (token->type != CPP_OPEN_BRACE)
30039 return; /* No ivars specified. */
30041 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30042 token = cp_lexer_peek_token (parser->lexer);
30044 while (token->type != CPP_CLOSE_BRACE
30045 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30047 cp_decl_specifier_seq declspecs;
30048 int decl_class_or_enum_p;
30049 tree prefix_attributes;
30051 cp_parser_objc_visibility_spec (parser);
30053 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30054 break;
30056 cp_parser_decl_specifier_seq (parser,
30057 CP_PARSER_FLAGS_OPTIONAL,
30058 &declspecs,
30059 &decl_class_or_enum_p);
30061 /* auto, register, static, extern, mutable. */
30062 if (declspecs.storage_class != sc_none)
30064 cp_parser_error (parser, "invalid type for instance variable");
30065 declspecs.storage_class = sc_none;
30068 /* thread_local. */
30069 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30071 cp_parser_error (parser, "invalid type for instance variable");
30072 declspecs.locations[ds_thread] = 0;
30075 /* typedef. */
30076 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30078 cp_parser_error (parser, "invalid type for instance variable");
30079 declspecs.locations[ds_typedef] = 0;
30082 prefix_attributes = declspecs.attributes;
30083 declspecs.attributes = NULL_TREE;
30085 /* Keep going until we hit the `;' at the end of the
30086 declaration. */
30087 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30089 tree width = NULL_TREE, attributes, first_attribute, decl;
30090 cp_declarator *declarator = NULL;
30091 int ctor_dtor_or_conv_p;
30093 /* Check for a (possibly unnamed) bitfield declaration. */
30094 token = cp_lexer_peek_token (parser->lexer);
30095 if (token->type == CPP_COLON)
30096 goto eat_colon;
30098 if (token->type == CPP_NAME
30099 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30100 == CPP_COLON))
30102 /* Get the name of the bitfield. */
30103 declarator = make_id_declarator (NULL_TREE,
30104 cp_parser_identifier (parser),
30105 sfk_none);
30107 eat_colon:
30108 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30109 /* Get the width of the bitfield. */
30110 width
30111 = cp_parser_constant_expression (parser);
30113 else
30115 /* Parse the declarator. */
30116 declarator
30117 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30118 &ctor_dtor_or_conv_p,
30119 /*parenthesized_p=*/NULL,
30120 /*member_p=*/false,
30121 /*friend_p=*/false);
30124 /* Look for attributes that apply to the ivar. */
30125 attributes = cp_parser_attributes_opt (parser);
30126 /* Remember which attributes are prefix attributes and
30127 which are not. */
30128 first_attribute = attributes;
30129 /* Combine the attributes. */
30130 attributes = attr_chainon (prefix_attributes, attributes);
30132 if (width)
30133 /* Create the bitfield declaration. */
30134 decl = grokbitfield (declarator, &declspecs,
30135 width, NULL_TREE, attributes);
30136 else
30137 decl = grokfield (declarator, &declspecs,
30138 NULL_TREE, /*init_const_expr_p=*/false,
30139 NULL_TREE, attributes);
30141 /* Add the instance variable. */
30142 if (decl != error_mark_node && decl != NULL_TREE)
30143 objc_add_instance_variable (decl);
30145 /* Reset PREFIX_ATTRIBUTES. */
30146 if (attributes != error_mark_node)
30148 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30149 attributes = TREE_CHAIN (attributes);
30150 if (attributes)
30151 TREE_CHAIN (attributes) = NULL_TREE;
30154 token = cp_lexer_peek_token (parser->lexer);
30156 if (token->type == CPP_COMMA)
30158 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30159 continue;
30161 break;
30164 cp_parser_consume_semicolon_at_end_of_statement (parser);
30165 token = cp_lexer_peek_token (parser->lexer);
30168 if (token->keyword == RID_AT_END)
30169 cp_parser_error (parser, "expected %<}%>");
30171 /* Do not consume the RID_AT_END, so it will be read again as terminating
30172 the @interface of @implementation. */
30173 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30174 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30176 /* For historical reasons, we accept an optional semicolon. */
30177 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30178 cp_lexer_consume_token (parser->lexer);
30181 /* Parse an Objective-C protocol declaration. */
30183 static void
30184 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30186 tree proto, protorefs;
30187 cp_token *tok;
30189 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30190 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30192 tok = cp_lexer_peek_token (parser->lexer);
30193 error_at (tok->location, "identifier expected after %<@protocol%>");
30194 cp_parser_consume_semicolon_at_end_of_statement (parser);
30195 return;
30198 /* See if we have a forward declaration or a definition. */
30199 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30201 /* Try a forward declaration first. */
30202 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30204 while (true)
30206 tree id;
30208 id = cp_parser_identifier (parser);
30209 if (id == error_mark_node)
30210 break;
30212 objc_declare_protocol (id, attributes);
30214 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30215 cp_lexer_consume_token (parser->lexer);
30216 else
30217 break;
30219 cp_parser_consume_semicolon_at_end_of_statement (parser);
30222 /* Ok, we got a full-fledged definition (or at least should). */
30223 else
30225 proto = cp_parser_identifier (parser);
30226 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30227 objc_start_protocol (proto, protorefs, attributes);
30228 cp_parser_objc_method_prototype_list (parser);
30232 /* Parse an Objective-C superclass or category. */
30234 static void
30235 cp_parser_objc_superclass_or_category (cp_parser *parser,
30236 bool iface_p,
30237 tree *super,
30238 tree *categ, bool *is_class_extension)
30240 cp_token *next = cp_lexer_peek_token (parser->lexer);
30242 *super = *categ = NULL_TREE;
30243 *is_class_extension = false;
30244 if (next->type == CPP_COLON)
30246 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30247 *super = cp_parser_identifier (parser);
30249 else if (next->type == CPP_OPEN_PAREN)
30251 matching_parens parens;
30252 parens.consume_open (parser); /* Eat '('. */
30254 /* If there is no category name, and this is an @interface, we
30255 have a class extension. */
30256 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30258 *categ = NULL_TREE;
30259 *is_class_extension = true;
30261 else
30262 *categ = cp_parser_identifier (parser);
30264 parens.require_close (parser);
30268 /* Parse an Objective-C class interface. */
30270 static void
30271 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30273 tree name, super, categ, protos;
30274 bool is_class_extension;
30276 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30277 name = cp_parser_identifier (parser);
30278 if (name == error_mark_node)
30280 /* It's hard to recover because even if valid @interface stuff
30281 is to follow, we can't compile it (or validate it) if we
30282 don't even know which class it refers to. Let's assume this
30283 was a stray '@interface' token in the stream and skip it.
30285 return;
30287 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30288 &is_class_extension);
30289 protos = cp_parser_objc_protocol_refs_opt (parser);
30291 /* We have either a class or a category on our hands. */
30292 if (categ || is_class_extension)
30293 objc_start_category_interface (name, categ, protos, attributes);
30294 else
30296 objc_start_class_interface (name, super, protos, attributes);
30297 /* Handle instance variable declarations, if any. */
30298 cp_parser_objc_class_ivars (parser);
30299 objc_continue_interface ();
30302 cp_parser_objc_method_prototype_list (parser);
30305 /* Parse an Objective-C class implementation. */
30307 static void
30308 cp_parser_objc_class_implementation (cp_parser* parser)
30310 tree name, super, categ;
30311 bool is_class_extension;
30313 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30314 name = cp_parser_identifier (parser);
30315 if (name == error_mark_node)
30317 /* It's hard to recover because even if valid @implementation
30318 stuff is to follow, we can't compile it (or validate it) if
30319 we don't even know which class it refers to. Let's assume
30320 this was a stray '@implementation' token in the stream and
30321 skip it.
30323 return;
30325 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30326 &is_class_extension);
30328 /* We have either a class or a category on our hands. */
30329 if (categ)
30330 objc_start_category_implementation (name, categ);
30331 else
30333 objc_start_class_implementation (name, super);
30334 /* Handle instance variable declarations, if any. */
30335 cp_parser_objc_class_ivars (parser);
30336 objc_continue_implementation ();
30339 cp_parser_objc_method_definition_list (parser);
30342 /* Consume the @end token and finish off the implementation. */
30344 static void
30345 cp_parser_objc_end_implementation (cp_parser* parser)
30347 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30348 objc_finish_implementation ();
30351 /* Parse an Objective-C declaration. */
30353 static void
30354 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30356 /* Try to figure out what kind of declaration is present. */
30357 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30359 if (attributes)
30360 switch (kwd->keyword)
30362 case RID_AT_ALIAS:
30363 case RID_AT_CLASS:
30364 case RID_AT_END:
30365 error_at (kwd->location, "attributes may not be specified before"
30366 " the %<@%D%> Objective-C++ keyword",
30367 kwd->u.value);
30368 attributes = NULL;
30369 break;
30370 case RID_AT_IMPLEMENTATION:
30371 warning_at (kwd->location, OPT_Wattributes,
30372 "prefix attributes are ignored before %<@%D%>",
30373 kwd->u.value);
30374 attributes = NULL;
30375 default:
30376 break;
30379 switch (kwd->keyword)
30381 case RID_AT_ALIAS:
30382 cp_parser_objc_alias_declaration (parser);
30383 break;
30384 case RID_AT_CLASS:
30385 cp_parser_objc_class_declaration (parser);
30386 break;
30387 case RID_AT_PROTOCOL:
30388 cp_parser_objc_protocol_declaration (parser, attributes);
30389 break;
30390 case RID_AT_INTERFACE:
30391 cp_parser_objc_class_interface (parser, attributes);
30392 break;
30393 case RID_AT_IMPLEMENTATION:
30394 cp_parser_objc_class_implementation (parser);
30395 break;
30396 case RID_AT_END:
30397 cp_parser_objc_end_implementation (parser);
30398 break;
30399 default:
30400 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30401 kwd->u.value);
30402 cp_parser_skip_to_end_of_block_or_statement (parser);
30406 /* Parse an Objective-C try-catch-finally statement.
30408 objc-try-catch-finally-stmt:
30409 @try compound-statement objc-catch-clause-seq [opt]
30410 objc-finally-clause [opt]
30412 objc-catch-clause-seq:
30413 objc-catch-clause objc-catch-clause-seq [opt]
30415 objc-catch-clause:
30416 @catch ( objc-exception-declaration ) compound-statement
30418 objc-finally-clause:
30419 @finally compound-statement
30421 objc-exception-declaration:
30422 parameter-declaration
30423 '...'
30425 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30427 Returns NULL_TREE.
30429 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30430 for C. Keep them in sync. */
30432 static tree
30433 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30435 location_t location;
30436 tree stmt;
30438 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30439 location = cp_lexer_peek_token (parser->lexer)->location;
30440 objc_maybe_warn_exceptions (location);
30441 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30442 node, lest it get absorbed into the surrounding block. */
30443 stmt = push_stmt_list ();
30444 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30445 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30447 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30449 cp_parameter_declarator *parm;
30450 tree parameter_declaration = error_mark_node;
30451 bool seen_open_paren = false;
30452 matching_parens parens;
30454 cp_lexer_consume_token (parser->lexer);
30455 if (parens.require_open (parser))
30456 seen_open_paren = true;
30457 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30459 /* We have "@catch (...)" (where the '...' are literally
30460 what is in the code). Skip the '...'.
30461 parameter_declaration is set to NULL_TREE, and
30462 objc_being_catch_clauses() knows that that means
30463 '...'. */
30464 cp_lexer_consume_token (parser->lexer);
30465 parameter_declaration = NULL_TREE;
30467 else
30469 /* We have "@catch (NSException *exception)" or something
30470 like that. Parse the parameter declaration. */
30471 parm = cp_parser_parameter_declaration (parser, false, NULL);
30472 if (parm == NULL)
30473 parameter_declaration = error_mark_node;
30474 else
30475 parameter_declaration = grokdeclarator (parm->declarator,
30476 &parm->decl_specifiers,
30477 PARM, /*initialized=*/0,
30478 /*attrlist=*/NULL);
30480 if (seen_open_paren)
30481 parens.require_close (parser);
30482 else
30484 /* If there was no open parenthesis, we are recovering from
30485 an error, and we are trying to figure out what mistake
30486 the user has made. */
30488 /* If there is an immediate closing parenthesis, the user
30489 probably forgot the opening one (ie, they typed "@catch
30490 NSException *e)". Parse the closing parenthesis and keep
30491 going. */
30492 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30493 cp_lexer_consume_token (parser->lexer);
30495 /* If these is no immediate closing parenthesis, the user
30496 probably doesn't know that parenthesis are required at
30497 all (ie, they typed "@catch NSException *e"). So, just
30498 forget about the closing parenthesis and keep going. */
30500 objc_begin_catch_clause (parameter_declaration);
30501 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30502 objc_finish_catch_clause ();
30504 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30506 cp_lexer_consume_token (parser->lexer);
30507 location = cp_lexer_peek_token (parser->lexer)->location;
30508 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30509 node, lest it get absorbed into the surrounding block. */
30510 stmt = push_stmt_list ();
30511 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30512 objc_build_finally_clause (location, pop_stmt_list (stmt));
30515 return objc_finish_try_stmt ();
30518 /* Parse an Objective-C synchronized statement.
30520 objc-synchronized-stmt:
30521 @synchronized ( expression ) compound-statement
30523 Returns NULL_TREE. */
30525 static tree
30526 cp_parser_objc_synchronized_statement (cp_parser *parser)
30528 location_t location;
30529 tree lock, stmt;
30531 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30533 location = cp_lexer_peek_token (parser->lexer)->location;
30534 objc_maybe_warn_exceptions (location);
30535 matching_parens parens;
30536 parens.require_open (parser);
30537 lock = cp_parser_expression (parser);
30538 parens.require_close (parser);
30540 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30541 node, lest it get absorbed into the surrounding block. */
30542 stmt = push_stmt_list ();
30543 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30545 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30548 /* Parse an Objective-C throw statement.
30550 objc-throw-stmt:
30551 @throw assignment-expression [opt] ;
30553 Returns a constructed '@throw' statement. */
30555 static tree
30556 cp_parser_objc_throw_statement (cp_parser *parser)
30558 tree expr = NULL_TREE;
30559 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30561 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30564 expr = cp_parser_expression (parser);
30566 cp_parser_consume_semicolon_at_end_of_statement (parser);
30568 return objc_build_throw_stmt (loc, expr);
30571 /* Parse an Objective-C statement. */
30573 static tree
30574 cp_parser_objc_statement (cp_parser * parser)
30576 /* Try to figure out what kind of declaration is present. */
30577 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30579 switch (kwd->keyword)
30581 case RID_AT_TRY:
30582 return cp_parser_objc_try_catch_finally_statement (parser);
30583 case RID_AT_SYNCHRONIZED:
30584 return cp_parser_objc_synchronized_statement (parser);
30585 case RID_AT_THROW:
30586 return cp_parser_objc_throw_statement (parser);
30587 default:
30588 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30589 kwd->u.value);
30590 cp_parser_skip_to_end_of_block_or_statement (parser);
30593 return error_mark_node;
30596 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30597 look ahead to see if an objc keyword follows the attributes. This
30598 is to detect the use of prefix attributes on ObjC @interface and
30599 @protocol. */
30601 static bool
30602 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30604 cp_lexer_save_tokens (parser->lexer);
30605 *attrib = cp_parser_attributes_opt (parser);
30606 gcc_assert (*attrib);
30607 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30609 cp_lexer_commit_tokens (parser->lexer);
30610 return true;
30612 cp_lexer_rollback_tokens (parser->lexer);
30613 return false;
30616 /* This routine is a minimal replacement for
30617 c_parser_struct_declaration () used when parsing the list of
30618 types/names or ObjC++ properties. For example, when parsing the
30619 code
30621 @property (readonly) int a, b, c;
30623 this function is responsible for parsing "int a, int b, int c" and
30624 returning the declarations as CHAIN of DECLs.
30626 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30627 similar parsing. */
30628 static tree
30629 cp_parser_objc_struct_declaration (cp_parser *parser)
30631 tree decls = NULL_TREE;
30632 cp_decl_specifier_seq declspecs;
30633 int decl_class_or_enum_p;
30634 tree prefix_attributes;
30636 cp_parser_decl_specifier_seq (parser,
30637 CP_PARSER_FLAGS_NONE,
30638 &declspecs,
30639 &decl_class_or_enum_p);
30641 if (declspecs.type == error_mark_node)
30642 return error_mark_node;
30644 /* auto, register, static, extern, mutable. */
30645 if (declspecs.storage_class != sc_none)
30647 cp_parser_error (parser, "invalid type for property");
30648 declspecs.storage_class = sc_none;
30651 /* thread_local. */
30652 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30654 cp_parser_error (parser, "invalid type for property");
30655 declspecs.locations[ds_thread] = 0;
30658 /* typedef. */
30659 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30661 cp_parser_error (parser, "invalid type for property");
30662 declspecs.locations[ds_typedef] = 0;
30665 prefix_attributes = declspecs.attributes;
30666 declspecs.attributes = NULL_TREE;
30668 /* Keep going until we hit the `;' at the end of the declaration. */
30669 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30671 tree attributes, first_attribute, decl;
30672 cp_declarator *declarator;
30673 cp_token *token;
30675 /* Parse the declarator. */
30676 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30677 NULL, NULL, false, false);
30679 /* Look for attributes that apply to the ivar. */
30680 attributes = cp_parser_attributes_opt (parser);
30681 /* Remember which attributes are prefix attributes and
30682 which are not. */
30683 first_attribute = attributes;
30684 /* Combine the attributes. */
30685 attributes = attr_chainon (prefix_attributes, attributes);
30687 decl = grokfield (declarator, &declspecs,
30688 NULL_TREE, /*init_const_expr_p=*/false,
30689 NULL_TREE, attributes);
30691 if (decl == error_mark_node || decl == NULL_TREE)
30692 return error_mark_node;
30694 /* Reset PREFIX_ATTRIBUTES. */
30695 if (attributes != error_mark_node)
30697 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30698 attributes = TREE_CHAIN (attributes);
30699 if (attributes)
30700 TREE_CHAIN (attributes) = NULL_TREE;
30703 DECL_CHAIN (decl) = decls;
30704 decls = decl;
30706 token = cp_lexer_peek_token (parser->lexer);
30707 if (token->type == CPP_COMMA)
30709 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30710 continue;
30712 else
30713 break;
30715 return decls;
30718 /* Parse an Objective-C @property declaration. The syntax is:
30720 objc-property-declaration:
30721 '@property' objc-property-attributes[opt] struct-declaration ;
30723 objc-property-attributes:
30724 '(' objc-property-attribute-list ')'
30726 objc-property-attribute-list:
30727 objc-property-attribute
30728 objc-property-attribute-list, objc-property-attribute
30730 objc-property-attribute
30731 'getter' = identifier
30732 'setter' = identifier
30733 'readonly'
30734 'readwrite'
30735 'assign'
30736 'retain'
30737 'copy'
30738 'nonatomic'
30740 For example:
30741 @property NSString *name;
30742 @property (readonly) id object;
30743 @property (retain, nonatomic, getter=getTheName) id name;
30744 @property int a, b, c;
30746 PS: This function is identical to
30747 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30748 static void
30749 cp_parser_objc_at_property_declaration (cp_parser *parser)
30751 /* The following variables hold the attributes of the properties as
30752 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30753 seen. When we see an attribute, we set them to 'true' (if they
30754 are boolean properties) or to the identifier (if they have an
30755 argument, ie, for getter and setter). Note that here we only
30756 parse the list of attributes, check the syntax and accumulate the
30757 attributes that we find. objc_add_property_declaration() will
30758 then process the information. */
30759 bool property_assign = false;
30760 bool property_copy = false;
30761 tree property_getter_ident = NULL_TREE;
30762 bool property_nonatomic = false;
30763 bool property_readonly = false;
30764 bool property_readwrite = false;
30765 bool property_retain = false;
30766 tree property_setter_ident = NULL_TREE;
30768 /* 'properties' is the list of properties that we read. Usually a
30769 single one, but maybe more (eg, in "@property int a, b, c;" there
30770 are three). */
30771 tree properties;
30772 location_t loc;
30774 loc = cp_lexer_peek_token (parser->lexer)->location;
30776 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30778 /* Parse the optional attribute list... */
30779 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30781 /* Eat the '('. */
30782 matching_parens parens;
30783 parens.consume_open (parser);
30785 while (true)
30787 bool syntax_error = false;
30788 cp_token *token = cp_lexer_peek_token (parser->lexer);
30789 enum rid keyword;
30791 if (token->type != CPP_NAME)
30793 cp_parser_error (parser, "expected identifier");
30794 break;
30796 keyword = C_RID_CODE (token->u.value);
30797 cp_lexer_consume_token (parser->lexer);
30798 switch (keyword)
30800 case RID_ASSIGN: property_assign = true; break;
30801 case RID_COPY: property_copy = true; break;
30802 case RID_NONATOMIC: property_nonatomic = true; break;
30803 case RID_READONLY: property_readonly = true; break;
30804 case RID_READWRITE: property_readwrite = true; break;
30805 case RID_RETAIN: property_retain = true; break;
30807 case RID_GETTER:
30808 case RID_SETTER:
30809 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30811 if (keyword == RID_GETTER)
30812 cp_parser_error (parser,
30813 "missing %<=%> (after %<getter%> attribute)");
30814 else
30815 cp_parser_error (parser,
30816 "missing %<=%> (after %<setter%> attribute)");
30817 syntax_error = true;
30818 break;
30820 cp_lexer_consume_token (parser->lexer); /* eat the = */
30821 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30823 cp_parser_error (parser, "expected identifier");
30824 syntax_error = true;
30825 break;
30827 if (keyword == RID_SETTER)
30829 if (property_setter_ident != NULL_TREE)
30831 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30832 cp_lexer_consume_token (parser->lexer);
30834 else
30835 property_setter_ident = cp_parser_objc_selector (parser);
30836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30837 cp_parser_error (parser, "setter name must terminate with %<:%>");
30838 else
30839 cp_lexer_consume_token (parser->lexer);
30841 else
30843 if (property_getter_ident != NULL_TREE)
30845 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30846 cp_lexer_consume_token (parser->lexer);
30848 else
30849 property_getter_ident = cp_parser_objc_selector (parser);
30851 break;
30852 default:
30853 cp_parser_error (parser, "unknown property attribute");
30854 syntax_error = true;
30855 break;
30858 if (syntax_error)
30859 break;
30861 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30862 cp_lexer_consume_token (parser->lexer);
30863 else
30864 break;
30867 /* FIXME: "@property (setter, assign);" will generate a spurious
30868 "error: expected ‘)’ before ‘,’ token". This is because
30869 cp_parser_require, unlike the C counterpart, will produce an
30870 error even if we are in error recovery. */
30871 if (!parens.require_close (parser))
30873 cp_parser_skip_to_closing_parenthesis (parser,
30874 /*recovering=*/true,
30875 /*or_comma=*/false,
30876 /*consume_paren=*/true);
30880 /* ... and the property declaration(s). */
30881 properties = cp_parser_objc_struct_declaration (parser);
30883 if (properties == error_mark_node)
30885 cp_parser_skip_to_end_of_statement (parser);
30886 /* If the next token is now a `;', consume it. */
30887 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30888 cp_lexer_consume_token (parser->lexer);
30889 return;
30892 if (properties == NULL_TREE)
30893 cp_parser_error (parser, "expected identifier");
30894 else
30896 /* Comma-separated properties are chained together in
30897 reverse order; add them one by one. */
30898 properties = nreverse (properties);
30900 for (; properties; properties = TREE_CHAIN (properties))
30901 objc_add_property_declaration (loc, copy_node (properties),
30902 property_readonly, property_readwrite,
30903 property_assign, property_retain,
30904 property_copy, property_nonatomic,
30905 property_getter_ident, property_setter_ident);
30908 cp_parser_consume_semicolon_at_end_of_statement (parser);
30911 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30913 objc-synthesize-declaration:
30914 @synthesize objc-synthesize-identifier-list ;
30916 objc-synthesize-identifier-list:
30917 objc-synthesize-identifier
30918 objc-synthesize-identifier-list, objc-synthesize-identifier
30920 objc-synthesize-identifier
30921 identifier
30922 identifier = identifier
30924 For example:
30925 @synthesize MyProperty;
30926 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30928 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30929 for C. Keep them in sync.
30931 static void
30932 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30934 tree list = NULL_TREE;
30935 location_t loc;
30936 loc = cp_lexer_peek_token (parser->lexer)->location;
30938 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30939 while (true)
30941 tree property, ivar;
30942 property = cp_parser_identifier (parser);
30943 if (property == error_mark_node)
30945 cp_parser_consume_semicolon_at_end_of_statement (parser);
30946 return;
30948 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30950 cp_lexer_consume_token (parser->lexer);
30951 ivar = cp_parser_identifier (parser);
30952 if (ivar == error_mark_node)
30954 cp_parser_consume_semicolon_at_end_of_statement (parser);
30955 return;
30958 else
30959 ivar = NULL_TREE;
30960 list = chainon (list, build_tree_list (ivar, property));
30961 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30962 cp_lexer_consume_token (parser->lexer);
30963 else
30964 break;
30966 cp_parser_consume_semicolon_at_end_of_statement (parser);
30967 objc_add_synthesize_declaration (loc, list);
30970 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30972 objc-dynamic-declaration:
30973 @dynamic identifier-list ;
30975 For example:
30976 @dynamic MyProperty;
30977 @dynamic MyProperty, AnotherProperty;
30979 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30980 for C. Keep them in sync.
30982 static void
30983 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30985 tree list = NULL_TREE;
30986 location_t loc;
30987 loc = cp_lexer_peek_token (parser->lexer)->location;
30989 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30990 while (true)
30992 tree property;
30993 property = cp_parser_identifier (parser);
30994 if (property == error_mark_node)
30996 cp_parser_consume_semicolon_at_end_of_statement (parser);
30997 return;
30999 list = chainon (list, build_tree_list (NULL, property));
31000 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31001 cp_lexer_consume_token (parser->lexer);
31002 else
31003 break;
31005 cp_parser_consume_semicolon_at_end_of_statement (parser);
31006 objc_add_dynamic_declaration (loc, list);
31010 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31012 /* Returns name of the next clause.
31013 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31014 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31015 returned and the token is consumed. */
31017 static pragma_omp_clause
31018 cp_parser_omp_clause_name (cp_parser *parser)
31020 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31022 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31023 result = PRAGMA_OACC_CLAUSE_AUTO;
31024 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31025 result = PRAGMA_OMP_CLAUSE_IF;
31026 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31027 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31028 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31029 result = PRAGMA_OACC_CLAUSE_DELETE;
31030 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31031 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31032 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31033 result = PRAGMA_OMP_CLAUSE_FOR;
31034 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31036 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31037 const char *p = IDENTIFIER_POINTER (id);
31039 switch (p[0])
31041 case 'a':
31042 if (!strcmp ("aligned", p))
31043 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31044 else if (!strcmp ("async", p))
31045 result = PRAGMA_OACC_CLAUSE_ASYNC;
31046 break;
31047 case 'c':
31048 if (!strcmp ("collapse", p))
31049 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31050 else if (!strcmp ("copy", p))
31051 result = PRAGMA_OACC_CLAUSE_COPY;
31052 else if (!strcmp ("copyin", p))
31053 result = PRAGMA_OMP_CLAUSE_COPYIN;
31054 else if (!strcmp ("copyout", p))
31055 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31056 else if (!strcmp ("copyprivate", p))
31057 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31058 else if (!strcmp ("create", p))
31059 result = PRAGMA_OACC_CLAUSE_CREATE;
31060 break;
31061 case 'd':
31062 if (!strcmp ("defaultmap", p))
31063 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31064 else if (!strcmp ("depend", p))
31065 result = PRAGMA_OMP_CLAUSE_DEPEND;
31066 else if (!strcmp ("device", p))
31067 result = PRAGMA_OMP_CLAUSE_DEVICE;
31068 else if (!strcmp ("deviceptr", p))
31069 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31070 else if (!strcmp ("device_resident", p))
31071 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31072 else if (!strcmp ("dist_schedule", p))
31073 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31074 break;
31075 case 'f':
31076 if (!strcmp ("final", p))
31077 result = PRAGMA_OMP_CLAUSE_FINAL;
31078 else if (!strcmp ("firstprivate", p))
31079 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31080 else if (!strcmp ("from", p))
31081 result = PRAGMA_OMP_CLAUSE_FROM;
31082 break;
31083 case 'g':
31084 if (!strcmp ("gang", p))
31085 result = PRAGMA_OACC_CLAUSE_GANG;
31086 else if (!strcmp ("grainsize", p))
31087 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31088 break;
31089 case 'h':
31090 if (!strcmp ("hint", p))
31091 result = PRAGMA_OMP_CLAUSE_HINT;
31092 else if (!strcmp ("host", p))
31093 result = PRAGMA_OACC_CLAUSE_HOST;
31094 break;
31095 case 'i':
31096 if (!strcmp ("inbranch", p))
31097 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31098 else if (!strcmp ("independent", p))
31099 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31100 else if (!strcmp ("is_device_ptr", p))
31101 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31102 break;
31103 case 'l':
31104 if (!strcmp ("lastprivate", p))
31105 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31106 else if (!strcmp ("linear", p))
31107 result = PRAGMA_OMP_CLAUSE_LINEAR;
31108 else if (!strcmp ("link", p))
31109 result = PRAGMA_OMP_CLAUSE_LINK;
31110 break;
31111 case 'm':
31112 if (!strcmp ("map", p))
31113 result = PRAGMA_OMP_CLAUSE_MAP;
31114 else if (!strcmp ("mergeable", p))
31115 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31116 break;
31117 case 'n':
31118 if (!strcmp ("nogroup", p))
31119 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31120 else if (!strcmp ("notinbranch", p))
31121 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31122 else if (!strcmp ("nowait", p))
31123 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31124 else if (!strcmp ("num_gangs", p))
31125 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31126 else if (!strcmp ("num_tasks", p))
31127 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31128 else if (!strcmp ("num_teams", p))
31129 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31130 else if (!strcmp ("num_threads", p))
31131 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31132 else if (!strcmp ("num_workers", p))
31133 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31134 break;
31135 case 'o':
31136 if (!strcmp ("ordered", p))
31137 result = PRAGMA_OMP_CLAUSE_ORDERED;
31138 break;
31139 case 'p':
31140 if (!strcmp ("parallel", p))
31141 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31142 else if (!strcmp ("present", p))
31143 result = PRAGMA_OACC_CLAUSE_PRESENT;
31144 else if (!strcmp ("present_or_copy", p)
31145 || !strcmp ("pcopy", p))
31146 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31147 else if (!strcmp ("present_or_copyin", p)
31148 || !strcmp ("pcopyin", p))
31149 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31150 else if (!strcmp ("present_or_copyout", p)
31151 || !strcmp ("pcopyout", p))
31152 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31153 else if (!strcmp ("present_or_create", p)
31154 || !strcmp ("pcreate", p))
31155 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31156 else if (!strcmp ("priority", p))
31157 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31158 else if (!strcmp ("proc_bind", p))
31159 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31160 break;
31161 case 'r':
31162 if (!strcmp ("reduction", p))
31163 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31164 break;
31165 case 's':
31166 if (!strcmp ("safelen", p))
31167 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31168 else if (!strcmp ("schedule", p))
31169 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31170 else if (!strcmp ("sections", p))
31171 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31172 else if (!strcmp ("self", p))
31173 result = PRAGMA_OACC_CLAUSE_SELF;
31174 else if (!strcmp ("seq", p))
31175 result = PRAGMA_OACC_CLAUSE_SEQ;
31176 else if (!strcmp ("shared", p))
31177 result = PRAGMA_OMP_CLAUSE_SHARED;
31178 else if (!strcmp ("simd", p))
31179 result = PRAGMA_OMP_CLAUSE_SIMD;
31180 else if (!strcmp ("simdlen", p))
31181 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31182 break;
31183 case 't':
31184 if (!strcmp ("taskgroup", p))
31185 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31186 else if (!strcmp ("thread_limit", p))
31187 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31188 else if (!strcmp ("threads", p))
31189 result = PRAGMA_OMP_CLAUSE_THREADS;
31190 else if (!strcmp ("tile", p))
31191 result = PRAGMA_OACC_CLAUSE_TILE;
31192 else if (!strcmp ("to", p))
31193 result = PRAGMA_OMP_CLAUSE_TO;
31194 break;
31195 case 'u':
31196 if (!strcmp ("uniform", p))
31197 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31198 else if (!strcmp ("untied", p))
31199 result = PRAGMA_OMP_CLAUSE_UNTIED;
31200 else if (!strcmp ("use_device", p))
31201 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31202 else if (!strcmp ("use_device_ptr", p))
31203 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31204 break;
31205 case 'v':
31206 if (!strcmp ("vector", p))
31207 result = PRAGMA_OACC_CLAUSE_VECTOR;
31208 else if (!strcmp ("vector_length", p))
31209 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31210 break;
31211 case 'w':
31212 if (!strcmp ("wait", p))
31213 result = PRAGMA_OACC_CLAUSE_WAIT;
31214 else if (!strcmp ("worker", p))
31215 result = PRAGMA_OACC_CLAUSE_WORKER;
31216 break;
31220 if (result != PRAGMA_OMP_CLAUSE_NONE)
31221 cp_lexer_consume_token (parser->lexer);
31223 return result;
31226 /* Validate that a clause of the given type does not already exist. */
31228 static void
31229 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31230 const char *name, location_t location)
31232 tree c;
31234 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31235 if (OMP_CLAUSE_CODE (c) == code)
31237 error_at (location, "too many %qs clauses", name);
31238 break;
31242 /* OpenMP 2.5:
31243 variable-list:
31244 identifier
31245 variable-list , identifier
31247 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31248 colon). An opening parenthesis will have been consumed by the caller.
31250 If KIND is nonzero, create the appropriate node and install the decl
31251 in OMP_CLAUSE_DECL and add the node to the head of the list.
31253 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31254 return the list created.
31256 COLON can be NULL if only closing parenthesis should end the list,
31257 or pointer to bool which will receive false if the list is terminated
31258 by closing parenthesis or true if the list is terminated by colon. */
31260 static tree
31261 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31262 tree list, bool *colon)
31264 cp_token *token;
31265 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31266 if (colon)
31268 parser->colon_corrects_to_scope_p = false;
31269 *colon = false;
31271 while (1)
31273 tree name, decl;
31275 token = cp_lexer_peek_token (parser->lexer);
31276 if (kind != 0
31277 && current_class_ptr
31278 && cp_parser_is_keyword (token, RID_THIS))
31280 decl = finish_this_expr ();
31281 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31282 || CONVERT_EXPR_P (decl))
31283 decl = TREE_OPERAND (decl, 0);
31284 cp_lexer_consume_token (parser->lexer);
31286 else
31288 name = cp_parser_id_expression (parser, /*template_p=*/false,
31289 /*check_dependency_p=*/true,
31290 /*template_p=*/NULL,
31291 /*declarator_p=*/false,
31292 /*optional_p=*/false);
31293 if (name == error_mark_node)
31294 goto skip_comma;
31296 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31297 if (decl == error_mark_node)
31298 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31299 token->location);
31301 if (decl == error_mark_node)
31303 else if (kind != 0)
31305 switch (kind)
31307 case OMP_CLAUSE__CACHE_:
31308 /* The OpenACC cache directive explicitly only allows "array
31309 elements or subarrays". */
31310 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31312 error_at (token->location, "expected %<[%>");
31313 decl = error_mark_node;
31314 break;
31316 /* FALLTHROUGH. */
31317 case OMP_CLAUSE_MAP:
31318 case OMP_CLAUSE_FROM:
31319 case OMP_CLAUSE_TO:
31320 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31322 location_t loc
31323 = cp_lexer_peek_token (parser->lexer)->location;
31324 cp_id_kind idk = CP_ID_KIND_NONE;
31325 cp_lexer_consume_token (parser->lexer);
31326 decl = convert_from_reference (decl);
31327 decl
31328 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31329 decl, false,
31330 &idk, loc);
31332 /* FALLTHROUGH. */
31333 case OMP_CLAUSE_DEPEND:
31334 case OMP_CLAUSE_REDUCTION:
31335 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31337 tree low_bound = NULL_TREE, length = NULL_TREE;
31339 parser->colon_corrects_to_scope_p = false;
31340 cp_lexer_consume_token (parser->lexer);
31341 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31342 low_bound = cp_parser_expression (parser);
31343 if (!colon)
31344 parser->colon_corrects_to_scope_p
31345 = saved_colon_corrects_to_scope_p;
31346 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31347 length = integer_one_node;
31348 else
31350 /* Look for `:'. */
31351 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31352 goto skip_comma;
31353 if (!cp_lexer_next_token_is (parser->lexer,
31354 CPP_CLOSE_SQUARE))
31355 length = cp_parser_expression (parser);
31357 /* Look for the closing `]'. */
31358 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31359 RT_CLOSE_SQUARE))
31360 goto skip_comma;
31362 decl = tree_cons (low_bound, length, decl);
31364 break;
31365 default:
31366 break;
31369 tree u = build_omp_clause (token->location, kind);
31370 OMP_CLAUSE_DECL (u) = decl;
31371 OMP_CLAUSE_CHAIN (u) = list;
31372 list = u;
31374 else
31375 list = tree_cons (decl, NULL_TREE, list);
31377 get_comma:
31378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31379 break;
31380 cp_lexer_consume_token (parser->lexer);
31383 if (colon)
31384 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31386 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31388 *colon = true;
31389 cp_parser_require (parser, CPP_COLON, RT_COLON);
31390 return list;
31393 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31395 int ending;
31397 /* Try to resync to an unnested comma. Copied from
31398 cp_parser_parenthesized_expression_list. */
31399 skip_comma:
31400 if (colon)
31401 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31402 ending = cp_parser_skip_to_closing_parenthesis (parser,
31403 /*recovering=*/true,
31404 /*or_comma=*/true,
31405 /*consume_paren=*/true);
31406 if (ending < 0)
31407 goto get_comma;
31410 return list;
31413 /* Similarly, but expect leading and trailing parenthesis. This is a very
31414 common case for omp clauses. */
31416 static tree
31417 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31419 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31420 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31421 return list;
31424 /* OpenACC 2.0:
31425 copy ( variable-list )
31426 copyin ( variable-list )
31427 copyout ( variable-list )
31428 create ( variable-list )
31429 delete ( variable-list )
31430 present ( variable-list )
31431 present_or_copy ( variable-list )
31432 pcopy ( variable-list )
31433 present_or_copyin ( variable-list )
31434 pcopyin ( variable-list )
31435 present_or_copyout ( variable-list )
31436 pcopyout ( variable-list )
31437 present_or_create ( variable-list )
31438 pcreate ( variable-list ) */
31440 static tree
31441 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31442 tree list)
31444 enum gomp_map_kind kind;
31445 switch (c_kind)
31447 case PRAGMA_OACC_CLAUSE_COPY:
31448 kind = GOMP_MAP_FORCE_TOFROM;
31449 break;
31450 case PRAGMA_OACC_CLAUSE_COPYIN:
31451 kind = GOMP_MAP_FORCE_TO;
31452 break;
31453 case PRAGMA_OACC_CLAUSE_COPYOUT:
31454 kind = GOMP_MAP_FORCE_FROM;
31455 break;
31456 case PRAGMA_OACC_CLAUSE_CREATE:
31457 kind = GOMP_MAP_FORCE_ALLOC;
31458 break;
31459 case PRAGMA_OACC_CLAUSE_DELETE:
31460 kind = GOMP_MAP_DELETE;
31461 break;
31462 case PRAGMA_OACC_CLAUSE_DEVICE:
31463 kind = GOMP_MAP_FORCE_TO;
31464 break;
31465 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31466 kind = GOMP_MAP_DEVICE_RESIDENT;
31467 break;
31468 case PRAGMA_OACC_CLAUSE_HOST:
31469 case PRAGMA_OACC_CLAUSE_SELF:
31470 kind = GOMP_MAP_FORCE_FROM;
31471 break;
31472 case PRAGMA_OACC_CLAUSE_LINK:
31473 kind = GOMP_MAP_LINK;
31474 break;
31475 case PRAGMA_OACC_CLAUSE_PRESENT:
31476 kind = GOMP_MAP_FORCE_PRESENT;
31477 break;
31478 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31479 kind = GOMP_MAP_TOFROM;
31480 break;
31481 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31482 kind = GOMP_MAP_TO;
31483 break;
31484 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31485 kind = GOMP_MAP_FROM;
31486 break;
31487 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31488 kind = GOMP_MAP_ALLOC;
31489 break;
31490 default:
31491 gcc_unreachable ();
31493 tree nl, c;
31494 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31496 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31497 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31499 return nl;
31502 /* OpenACC 2.0:
31503 deviceptr ( variable-list ) */
31505 static tree
31506 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31508 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31509 tree vars, t;
31511 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31512 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31513 variable-list must only allow for pointer variables. */
31514 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31515 for (t = vars; t; t = TREE_CHAIN (t))
31517 tree v = TREE_PURPOSE (t);
31518 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31519 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31520 OMP_CLAUSE_DECL (u) = v;
31521 OMP_CLAUSE_CHAIN (u) = list;
31522 list = u;
31525 return list;
31528 /* OpenACC 2.0:
31529 auto
31530 independent
31531 nohost
31532 seq */
31534 static tree
31535 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31536 enum omp_clause_code code,
31537 tree list, location_t location)
31539 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31540 tree c = build_omp_clause (location, code);
31541 OMP_CLAUSE_CHAIN (c) = list;
31542 return c;
31545 /* OpenACC:
31546 num_gangs ( expression )
31547 num_workers ( expression )
31548 vector_length ( expression ) */
31550 static tree
31551 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31552 const char *str, tree list)
31554 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31556 matching_parens parens;
31557 if (!parens.require_open (parser))
31558 return list;
31560 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31562 if (t == error_mark_node
31563 || !parens.require_close (parser))
31565 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31566 /*or_comma=*/false,
31567 /*consume_paren=*/true);
31568 return list;
31571 check_no_duplicate_clause (list, code, str, loc);
31573 tree c = build_omp_clause (loc, code);
31574 OMP_CLAUSE_OPERAND (c, 0) = t;
31575 OMP_CLAUSE_CHAIN (c) = list;
31576 return c;
31579 /* OpenACC:
31581 gang [( gang-arg-list )]
31582 worker [( [num:] int-expr )]
31583 vector [( [length:] int-expr )]
31585 where gang-arg is one of:
31587 [num:] int-expr
31588 static: size-expr
31590 and size-expr may be:
31593 int-expr
31596 static tree
31597 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31598 const char *str, tree list)
31600 const char *id = "num";
31601 cp_lexer *lexer = parser->lexer;
31602 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31603 location_t loc = cp_lexer_peek_token (lexer)->location;
31605 if (kind == OMP_CLAUSE_VECTOR)
31606 id = "length";
31608 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31610 matching_parens parens;
31611 parens.consume_open (parser);
31615 cp_token *next = cp_lexer_peek_token (lexer);
31616 int idx = 0;
31618 /* Gang static argument. */
31619 if (kind == OMP_CLAUSE_GANG
31620 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31622 cp_lexer_consume_token (lexer);
31624 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31625 goto cleanup_error;
31627 idx = 1;
31628 if (ops[idx] != NULL)
31630 cp_parser_error (parser, "too many %<static%> arguments");
31631 goto cleanup_error;
31634 /* Check for the '*' argument. */
31635 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31636 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31637 || cp_lexer_nth_token_is (parser->lexer, 2,
31638 CPP_CLOSE_PAREN)))
31640 cp_lexer_consume_token (lexer);
31641 ops[idx] = integer_minus_one_node;
31643 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31645 cp_lexer_consume_token (lexer);
31646 continue;
31648 else break;
31651 /* Worker num: argument and vector length: arguments. */
31652 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31653 && id_equal (next->u.value, id)
31654 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31656 cp_lexer_consume_token (lexer); /* id */
31657 cp_lexer_consume_token (lexer); /* ':' */
31660 /* Now collect the actual argument. */
31661 if (ops[idx] != NULL_TREE)
31663 cp_parser_error (parser, "unexpected argument");
31664 goto cleanup_error;
31667 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31668 false);
31669 if (expr == error_mark_node)
31670 goto cleanup_error;
31672 mark_exp_read (expr);
31673 ops[idx] = expr;
31675 if (kind == OMP_CLAUSE_GANG
31676 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31678 cp_lexer_consume_token (lexer);
31679 continue;
31681 break;
31683 while (1);
31685 if (!parens.require_close (parser))
31686 goto cleanup_error;
31689 check_no_duplicate_clause (list, kind, str, loc);
31691 c = build_omp_clause (loc, kind);
31693 if (ops[1])
31694 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31696 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31697 OMP_CLAUSE_CHAIN (c) = list;
31699 return c;
31701 cleanup_error:
31702 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31703 return list;
31706 /* OpenACC 2.0:
31707 tile ( size-expr-list ) */
31709 static tree
31710 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31712 tree c, expr = error_mark_node;
31713 tree tile = NULL_TREE;
31715 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31716 so, but the spec authors never considered such a case and have
31717 differing opinions on what it might mean, including 'not
31718 allowed'.) */
31719 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31720 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31721 clause_loc);
31723 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31724 return list;
31728 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31729 return list;
31731 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31732 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31733 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31735 cp_lexer_consume_token (parser->lexer);
31736 expr = integer_zero_node;
31738 else
31739 expr = cp_parser_constant_expression (parser);
31741 tile = tree_cons (NULL_TREE, expr, tile);
31743 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31745 /* Consume the trailing ')'. */
31746 cp_lexer_consume_token (parser->lexer);
31748 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31749 tile = nreverse (tile);
31750 OMP_CLAUSE_TILE_LIST (c) = tile;
31751 OMP_CLAUSE_CHAIN (c) = list;
31752 return c;
31755 /* OpenACC 2.0
31756 Parse wait clause or directive parameters. */
31758 static tree
31759 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31761 vec<tree, va_gc> *args;
31762 tree t, args_tree;
31764 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31765 /*cast_p=*/false,
31766 /*allow_expansion_p=*/true,
31767 /*non_constant_p=*/NULL);
31769 if (args == NULL || args->length () == 0)
31771 cp_parser_error (parser, "expected integer expression before ')'");
31772 if (args != NULL)
31773 release_tree_vector (args);
31774 return list;
31777 args_tree = build_tree_list_vec (args);
31779 release_tree_vector (args);
31781 for (t = args_tree; t; t = TREE_CHAIN (t))
31783 tree targ = TREE_VALUE (t);
31785 if (targ != error_mark_node)
31787 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31788 error ("%<wait%> expression must be integral");
31789 else
31791 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31793 targ = mark_rvalue_use (targ);
31794 OMP_CLAUSE_DECL (c) = targ;
31795 OMP_CLAUSE_CHAIN (c) = list;
31796 list = c;
31801 return list;
31804 /* OpenACC:
31805 wait ( int-expr-list ) */
31807 static tree
31808 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31810 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31812 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31813 return list;
31815 list = cp_parser_oacc_wait_list (parser, location, list);
31817 return list;
31820 /* OpenMP 3.0:
31821 collapse ( constant-expression ) */
31823 static tree
31824 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31826 tree c, num;
31827 location_t loc;
31828 HOST_WIDE_INT n;
31830 loc = cp_lexer_peek_token (parser->lexer)->location;
31831 matching_parens parens;
31832 if (!parens.require_open (parser))
31833 return list;
31835 num = cp_parser_constant_expression (parser);
31837 if (!parens.require_close (parser))
31838 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31839 /*or_comma=*/false,
31840 /*consume_paren=*/true);
31842 if (num == error_mark_node)
31843 return list;
31844 num = fold_non_dependent_expr (num);
31845 if (!tree_fits_shwi_p (num)
31846 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31847 || (n = tree_to_shwi (num)) <= 0
31848 || (int) n != n)
31850 error_at (loc, "collapse argument needs positive constant integer expression");
31851 return list;
31854 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31855 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31856 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31857 OMP_CLAUSE_CHAIN (c) = list;
31858 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31860 return c;
31863 /* OpenMP 2.5:
31864 default ( none | shared )
31866 OpenACC:
31867 default ( none | present ) */
31869 static tree
31870 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31871 location_t location, bool is_oacc)
31873 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31874 tree c;
31876 matching_parens parens;
31877 if (!parens.require_open (parser))
31878 return list;
31879 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31881 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31882 const char *p = IDENTIFIER_POINTER (id);
31884 switch (p[0])
31886 case 'n':
31887 if (strcmp ("none", p) != 0)
31888 goto invalid_kind;
31889 kind = OMP_CLAUSE_DEFAULT_NONE;
31890 break;
31892 case 'p':
31893 if (strcmp ("present", p) != 0 || !is_oacc)
31894 goto invalid_kind;
31895 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31896 break;
31898 case 's':
31899 if (strcmp ("shared", p) != 0 || is_oacc)
31900 goto invalid_kind;
31901 kind = OMP_CLAUSE_DEFAULT_SHARED;
31902 break;
31904 default:
31905 goto invalid_kind;
31908 cp_lexer_consume_token (parser->lexer);
31910 else
31912 invalid_kind:
31913 if (is_oacc)
31914 cp_parser_error (parser, "expected %<none%> or %<present%>");
31915 else
31916 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31919 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31920 || !parens.require_close (parser))
31921 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31922 /*or_comma=*/false,
31923 /*consume_paren=*/true);
31925 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31926 return list;
31928 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31929 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31930 OMP_CLAUSE_CHAIN (c) = list;
31931 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31933 return c;
31936 /* OpenMP 3.1:
31937 final ( expression ) */
31939 static tree
31940 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31942 tree t, c;
31944 matching_parens parens;
31945 if (!parens.require_open (parser))
31946 return list;
31948 t = cp_parser_condition (parser);
31950 if (t == error_mark_node
31951 || !parens.require_close (parser))
31952 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31953 /*or_comma=*/false,
31954 /*consume_paren=*/true);
31956 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31958 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31959 OMP_CLAUSE_FINAL_EXPR (c) = t;
31960 OMP_CLAUSE_CHAIN (c) = list;
31962 return c;
31965 /* OpenMP 2.5:
31966 if ( expression )
31968 OpenMP 4.5:
31969 if ( directive-name-modifier : expression )
31971 directive-name-modifier:
31972 parallel | task | taskloop | target data | target | target update
31973 | target enter data | target exit data */
31975 static tree
31976 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31977 bool is_omp)
31979 tree t, c;
31980 enum tree_code if_modifier = ERROR_MARK;
31982 matching_parens parens;
31983 if (!parens.require_open (parser))
31984 return list;
31986 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31988 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31989 const char *p = IDENTIFIER_POINTER (id);
31990 int n = 2;
31992 if (strcmp ("parallel", p) == 0)
31993 if_modifier = OMP_PARALLEL;
31994 else if (strcmp ("task", p) == 0)
31995 if_modifier = OMP_TASK;
31996 else if (strcmp ("taskloop", p) == 0)
31997 if_modifier = OMP_TASKLOOP;
31998 else if (strcmp ("target", p) == 0)
32000 if_modifier = OMP_TARGET;
32001 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32003 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32004 p = IDENTIFIER_POINTER (id);
32005 if (strcmp ("data", p) == 0)
32006 if_modifier = OMP_TARGET_DATA;
32007 else if (strcmp ("update", p) == 0)
32008 if_modifier = OMP_TARGET_UPDATE;
32009 else if (strcmp ("enter", p) == 0)
32010 if_modifier = OMP_TARGET_ENTER_DATA;
32011 else if (strcmp ("exit", p) == 0)
32012 if_modifier = OMP_TARGET_EXIT_DATA;
32013 if (if_modifier != OMP_TARGET)
32014 n = 3;
32015 else
32017 location_t loc
32018 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32019 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32020 "or %<exit%>");
32021 if_modifier = ERROR_MARK;
32023 if (if_modifier == OMP_TARGET_ENTER_DATA
32024 || if_modifier == OMP_TARGET_EXIT_DATA)
32026 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32028 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32029 p = IDENTIFIER_POINTER (id);
32030 if (strcmp ("data", p) == 0)
32031 n = 4;
32033 if (n != 4)
32035 location_t loc
32036 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32037 error_at (loc, "expected %<data%>");
32038 if_modifier = ERROR_MARK;
32043 if (if_modifier != ERROR_MARK)
32045 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32047 while (n-- > 0)
32048 cp_lexer_consume_token (parser->lexer);
32050 else
32052 if (n > 2)
32054 location_t loc
32055 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32056 error_at (loc, "expected %<:%>");
32058 if_modifier = ERROR_MARK;
32063 t = cp_parser_condition (parser);
32065 if (t == error_mark_node
32066 || !parens.require_close (parser))
32067 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32068 /*or_comma=*/false,
32069 /*consume_paren=*/true);
32071 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32072 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32074 if (if_modifier != ERROR_MARK
32075 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32077 const char *p = NULL;
32078 switch (if_modifier)
32080 case OMP_PARALLEL: p = "parallel"; break;
32081 case OMP_TASK: p = "task"; break;
32082 case OMP_TASKLOOP: p = "taskloop"; break;
32083 case OMP_TARGET_DATA: p = "target data"; break;
32084 case OMP_TARGET: p = "target"; break;
32085 case OMP_TARGET_UPDATE: p = "target update"; break;
32086 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32087 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32088 default: gcc_unreachable ();
32090 error_at (location, "too many %<if%> clauses with %qs modifier",
32092 return list;
32094 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32096 if (!is_omp)
32097 error_at (location, "too many %<if%> clauses");
32098 else
32099 error_at (location, "too many %<if%> clauses without modifier");
32100 return list;
32102 else if (if_modifier == ERROR_MARK
32103 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32105 error_at (location, "if any %<if%> clause has modifier, then all "
32106 "%<if%> clauses have to use modifier");
32107 return list;
32111 c = build_omp_clause (location, OMP_CLAUSE_IF);
32112 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32113 OMP_CLAUSE_IF_EXPR (c) = t;
32114 OMP_CLAUSE_CHAIN (c) = list;
32116 return c;
32119 /* OpenMP 3.1:
32120 mergeable */
32122 static tree
32123 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32124 tree list, location_t location)
32126 tree c;
32128 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32129 location);
32131 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32132 OMP_CLAUSE_CHAIN (c) = list;
32133 return c;
32136 /* OpenMP 2.5:
32137 nowait */
32139 static tree
32140 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32141 tree list, location_t location)
32143 tree c;
32145 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32147 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32148 OMP_CLAUSE_CHAIN (c) = list;
32149 return c;
32152 /* OpenMP 2.5:
32153 num_threads ( expression ) */
32155 static tree
32156 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32157 location_t location)
32159 tree t, c;
32161 matching_parens parens;
32162 if (!parens.require_open (parser))
32163 return list;
32165 t = cp_parser_expression (parser);
32167 if (t == error_mark_node
32168 || !parens.require_close (parser))
32169 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32170 /*or_comma=*/false,
32171 /*consume_paren=*/true);
32173 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32174 "num_threads", location);
32176 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32177 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32178 OMP_CLAUSE_CHAIN (c) = list;
32180 return c;
32183 /* OpenMP 4.5:
32184 num_tasks ( expression ) */
32186 static tree
32187 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32188 location_t location)
32190 tree t, c;
32192 matching_parens parens;
32193 if (!parens.require_open (parser))
32194 return list;
32196 t = cp_parser_expression (parser);
32198 if (t == error_mark_node
32199 || !parens.require_close (parser))
32200 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32201 /*or_comma=*/false,
32202 /*consume_paren=*/true);
32204 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32205 "num_tasks", location);
32207 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32208 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32209 OMP_CLAUSE_CHAIN (c) = list;
32211 return c;
32214 /* OpenMP 4.5:
32215 grainsize ( expression ) */
32217 static tree
32218 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32219 location_t location)
32221 tree t, c;
32223 matching_parens parens;
32224 if (!parens.require_open (parser))
32225 return list;
32227 t = cp_parser_expression (parser);
32229 if (t == error_mark_node
32230 || !parens.require_close (parser))
32231 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32232 /*or_comma=*/false,
32233 /*consume_paren=*/true);
32235 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32236 "grainsize", location);
32238 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32239 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32240 OMP_CLAUSE_CHAIN (c) = list;
32242 return c;
32245 /* OpenMP 4.5:
32246 priority ( expression ) */
32248 static tree
32249 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32250 location_t location)
32252 tree t, c;
32254 matching_parens parens;
32255 if (!parens.require_open (parser))
32256 return list;
32258 t = cp_parser_expression (parser);
32260 if (t == error_mark_node
32261 || !parens.require_close (parser))
32262 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32263 /*or_comma=*/false,
32264 /*consume_paren=*/true);
32266 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32267 "priority", location);
32269 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32270 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32271 OMP_CLAUSE_CHAIN (c) = list;
32273 return c;
32276 /* OpenMP 4.5:
32277 hint ( expression ) */
32279 static tree
32280 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32281 location_t location)
32283 tree t, c;
32285 matching_parens parens;
32286 if (!parens.require_open (parser))
32287 return list;
32289 t = cp_parser_expression (parser);
32291 if (t == error_mark_node
32292 || !parens.require_close (parser))
32293 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32294 /*or_comma=*/false,
32295 /*consume_paren=*/true);
32297 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32299 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32300 OMP_CLAUSE_HINT_EXPR (c) = t;
32301 OMP_CLAUSE_CHAIN (c) = list;
32303 return c;
32306 /* OpenMP 4.5:
32307 defaultmap ( tofrom : scalar ) */
32309 static tree
32310 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32311 location_t location)
32313 tree c, id;
32314 const char *p;
32316 matching_parens parens;
32317 if (!parens.require_open (parser))
32318 return list;
32320 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32322 cp_parser_error (parser, "expected %<tofrom%>");
32323 goto out_err;
32325 id = cp_lexer_peek_token (parser->lexer)->u.value;
32326 p = IDENTIFIER_POINTER (id);
32327 if (strcmp (p, "tofrom") != 0)
32329 cp_parser_error (parser, "expected %<tofrom%>");
32330 goto out_err;
32332 cp_lexer_consume_token (parser->lexer);
32333 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32334 goto out_err;
32336 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32338 cp_parser_error (parser, "expected %<scalar%>");
32339 goto out_err;
32341 id = cp_lexer_peek_token (parser->lexer)->u.value;
32342 p = IDENTIFIER_POINTER (id);
32343 if (strcmp (p, "scalar") != 0)
32345 cp_parser_error (parser, "expected %<scalar%>");
32346 goto out_err;
32348 cp_lexer_consume_token (parser->lexer);
32349 if (!parens.require_close (parser))
32350 goto out_err;
32352 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32353 location);
32355 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32356 OMP_CLAUSE_CHAIN (c) = list;
32357 return c;
32359 out_err:
32360 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32361 /*or_comma=*/false,
32362 /*consume_paren=*/true);
32363 return list;
32366 /* OpenMP 2.5:
32367 ordered
32369 OpenMP 4.5:
32370 ordered ( constant-expression ) */
32372 static tree
32373 cp_parser_omp_clause_ordered (cp_parser *parser,
32374 tree list, location_t location)
32376 tree c, num = NULL_TREE;
32377 HOST_WIDE_INT n;
32379 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32380 "ordered", location);
32382 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32384 matching_parens parens;
32385 parens.consume_open (parser);
32387 num = cp_parser_constant_expression (parser);
32389 if (!parens.require_close (parser))
32390 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32391 /*or_comma=*/false,
32392 /*consume_paren=*/true);
32394 if (num == error_mark_node)
32395 return list;
32396 num = fold_non_dependent_expr (num);
32397 if (!tree_fits_shwi_p (num)
32398 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32399 || (n = tree_to_shwi (num)) <= 0
32400 || (int) n != n)
32402 error_at (location,
32403 "ordered argument needs positive constant integer "
32404 "expression");
32405 return list;
32409 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32410 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32411 OMP_CLAUSE_CHAIN (c) = list;
32412 return c;
32415 /* OpenMP 2.5:
32416 reduction ( reduction-operator : variable-list )
32418 reduction-operator:
32419 One of: + * - & ^ | && ||
32421 OpenMP 3.1:
32423 reduction-operator:
32424 One of: + * - & ^ | && || min max
32426 OpenMP 4.0:
32428 reduction-operator:
32429 One of: + * - & ^ | && ||
32430 id-expression */
32432 static tree
32433 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32435 enum tree_code code = ERROR_MARK;
32436 tree nlist, c, id = NULL_TREE;
32438 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32439 return list;
32441 switch (cp_lexer_peek_token (parser->lexer)->type)
32443 case CPP_PLUS: code = PLUS_EXPR; break;
32444 case CPP_MULT: code = MULT_EXPR; break;
32445 case CPP_MINUS: code = MINUS_EXPR; break;
32446 case CPP_AND: code = BIT_AND_EXPR; break;
32447 case CPP_XOR: code = BIT_XOR_EXPR; break;
32448 case CPP_OR: code = BIT_IOR_EXPR; break;
32449 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32450 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32451 default: break;
32454 if (code != ERROR_MARK)
32455 cp_lexer_consume_token (parser->lexer);
32456 else
32458 bool saved_colon_corrects_to_scope_p;
32459 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32460 parser->colon_corrects_to_scope_p = false;
32461 id = cp_parser_id_expression (parser, /*template_p=*/false,
32462 /*check_dependency_p=*/true,
32463 /*template_p=*/NULL,
32464 /*declarator_p=*/false,
32465 /*optional_p=*/false);
32466 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32467 if (identifier_p (id))
32469 const char *p = IDENTIFIER_POINTER (id);
32471 if (strcmp (p, "min") == 0)
32472 code = MIN_EXPR;
32473 else if (strcmp (p, "max") == 0)
32474 code = MAX_EXPR;
32475 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32476 code = PLUS_EXPR;
32477 else if (id == ovl_op_identifier (false, MULT_EXPR))
32478 code = MULT_EXPR;
32479 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32480 code = MINUS_EXPR;
32481 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32482 code = BIT_AND_EXPR;
32483 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32484 code = BIT_IOR_EXPR;
32485 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32486 code = BIT_XOR_EXPR;
32487 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32488 code = TRUTH_ANDIF_EXPR;
32489 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32490 code = TRUTH_ORIF_EXPR;
32491 id = omp_reduction_id (code, id, NULL_TREE);
32492 tree scope = parser->scope;
32493 if (scope)
32494 id = build_qualified_name (NULL_TREE, scope, id, false);
32495 parser->scope = NULL_TREE;
32496 parser->qualifying_scope = NULL_TREE;
32497 parser->object_scope = NULL_TREE;
32499 else
32501 error ("invalid reduction-identifier");
32502 resync_fail:
32503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32504 /*or_comma=*/false,
32505 /*consume_paren=*/true);
32506 return list;
32510 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32511 goto resync_fail;
32513 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32514 NULL);
32515 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32517 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32518 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32521 return nlist;
32524 /* OpenMP 2.5:
32525 schedule ( schedule-kind )
32526 schedule ( schedule-kind , expression )
32528 schedule-kind:
32529 static | dynamic | guided | runtime | auto
32531 OpenMP 4.5:
32532 schedule ( schedule-modifier : schedule-kind )
32533 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32535 schedule-modifier:
32536 simd
32537 monotonic
32538 nonmonotonic */
32540 static tree
32541 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32543 tree c, t;
32544 int modifiers = 0, nmodifiers = 0;
32546 matching_parens parens;
32547 if (!parens.require_open (parser))
32548 return list;
32550 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32552 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32554 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32555 const char *p = IDENTIFIER_POINTER (id);
32556 if (strcmp ("simd", p) == 0)
32557 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32558 else if (strcmp ("monotonic", p) == 0)
32559 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32560 else if (strcmp ("nonmonotonic", p) == 0)
32561 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32562 else
32563 break;
32564 cp_lexer_consume_token (parser->lexer);
32565 if (nmodifiers++ == 0
32566 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32567 cp_lexer_consume_token (parser->lexer);
32568 else
32570 cp_parser_require (parser, CPP_COLON, RT_COLON);
32571 break;
32575 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32577 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32578 const char *p = IDENTIFIER_POINTER (id);
32580 switch (p[0])
32582 case 'd':
32583 if (strcmp ("dynamic", p) != 0)
32584 goto invalid_kind;
32585 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32586 break;
32588 case 'g':
32589 if (strcmp ("guided", p) != 0)
32590 goto invalid_kind;
32591 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32592 break;
32594 case 'r':
32595 if (strcmp ("runtime", p) != 0)
32596 goto invalid_kind;
32597 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32598 break;
32600 default:
32601 goto invalid_kind;
32604 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32605 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32606 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32607 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32608 else
32609 goto invalid_kind;
32610 cp_lexer_consume_token (parser->lexer);
32612 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32613 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32614 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32615 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32617 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32618 "specified");
32619 modifiers = 0;
32622 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32624 cp_token *token;
32625 cp_lexer_consume_token (parser->lexer);
32627 token = cp_lexer_peek_token (parser->lexer);
32628 t = cp_parser_assignment_expression (parser);
32630 if (t == error_mark_node)
32631 goto resync_fail;
32632 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32633 error_at (token->location, "schedule %<runtime%> does not take "
32634 "a %<chunk_size%> parameter");
32635 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32636 error_at (token->location, "schedule %<auto%> does not take "
32637 "a %<chunk_size%> parameter");
32638 else
32639 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32641 if (!parens.require_close (parser))
32642 goto resync_fail;
32644 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32645 goto resync_fail;
32647 OMP_CLAUSE_SCHEDULE_KIND (c)
32648 = (enum omp_clause_schedule_kind)
32649 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32651 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32652 OMP_CLAUSE_CHAIN (c) = list;
32653 return c;
32655 invalid_kind:
32656 cp_parser_error (parser, "invalid schedule kind");
32657 resync_fail:
32658 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32659 /*or_comma=*/false,
32660 /*consume_paren=*/true);
32661 return list;
32664 /* OpenMP 3.0:
32665 untied */
32667 static tree
32668 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32669 tree list, location_t location)
32671 tree c;
32673 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32675 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32676 OMP_CLAUSE_CHAIN (c) = list;
32677 return c;
32680 /* OpenMP 4.0:
32681 inbranch
32682 notinbranch */
32684 static tree
32685 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32686 tree list, location_t location)
32688 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32689 tree c = build_omp_clause (location, code);
32690 OMP_CLAUSE_CHAIN (c) = list;
32691 return c;
32694 /* OpenMP 4.0:
32695 parallel
32697 sections
32698 taskgroup */
32700 static tree
32701 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32702 enum omp_clause_code code,
32703 tree list, location_t location)
32705 tree c = build_omp_clause (location, code);
32706 OMP_CLAUSE_CHAIN (c) = list;
32707 return c;
32710 /* OpenMP 4.5:
32711 nogroup */
32713 static tree
32714 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32715 tree list, location_t location)
32717 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32718 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32719 OMP_CLAUSE_CHAIN (c) = list;
32720 return c;
32723 /* OpenMP 4.5:
32724 simd
32725 threads */
32727 static tree
32728 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32729 enum omp_clause_code code,
32730 tree list, location_t location)
32732 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32733 tree c = build_omp_clause (location, code);
32734 OMP_CLAUSE_CHAIN (c) = list;
32735 return c;
32738 /* OpenMP 4.0:
32739 num_teams ( expression ) */
32741 static tree
32742 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32743 location_t location)
32745 tree t, c;
32747 matching_parens parens;
32748 if (!parens.require_open (parser))
32749 return list;
32751 t = cp_parser_expression (parser);
32753 if (t == error_mark_node
32754 || !parens.require_close (parser))
32755 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32756 /*or_comma=*/false,
32757 /*consume_paren=*/true);
32759 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32760 "num_teams", location);
32762 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32763 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32764 OMP_CLAUSE_CHAIN (c) = list;
32766 return c;
32769 /* OpenMP 4.0:
32770 thread_limit ( expression ) */
32772 static tree
32773 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32774 location_t location)
32776 tree t, c;
32778 matching_parens parens;
32779 if (!parens.require_open (parser))
32780 return list;
32782 t = cp_parser_expression (parser);
32784 if (t == error_mark_node
32785 || !parens.require_close (parser))
32786 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32787 /*or_comma=*/false,
32788 /*consume_paren=*/true);
32790 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32791 "thread_limit", location);
32793 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32794 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32795 OMP_CLAUSE_CHAIN (c) = list;
32797 return c;
32800 /* OpenMP 4.0:
32801 aligned ( variable-list )
32802 aligned ( variable-list : constant-expression ) */
32804 static tree
32805 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32807 tree nlist, c, alignment = NULL_TREE;
32808 bool colon;
32810 matching_parens parens;
32811 if (!parens.require_open (parser))
32812 return list;
32814 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32815 &colon);
32817 if (colon)
32819 alignment = cp_parser_constant_expression (parser);
32821 if (!parens.require_close (parser))
32822 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32823 /*or_comma=*/false,
32824 /*consume_paren=*/true);
32826 if (alignment == error_mark_node)
32827 alignment = NULL_TREE;
32830 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32831 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32833 return nlist;
32836 /* OpenMP 4.0:
32837 linear ( variable-list )
32838 linear ( variable-list : expression )
32840 OpenMP 4.5:
32841 linear ( modifier ( variable-list ) )
32842 linear ( modifier ( variable-list ) : expression ) */
32844 static tree
32845 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32846 bool declare_simd)
32848 tree nlist, c, step = integer_one_node;
32849 bool colon;
32850 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32852 matching_parens parens;
32853 if (!parens.require_open (parser))
32854 return list;
32856 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32858 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32859 const char *p = IDENTIFIER_POINTER (id);
32861 if (strcmp ("ref", p) == 0)
32862 kind = OMP_CLAUSE_LINEAR_REF;
32863 else if (strcmp ("val", p) == 0)
32864 kind = OMP_CLAUSE_LINEAR_VAL;
32865 else if (strcmp ("uval", p) == 0)
32866 kind = OMP_CLAUSE_LINEAR_UVAL;
32867 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32868 cp_lexer_consume_token (parser->lexer);
32869 else
32870 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32873 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32874 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32875 &colon);
32876 else
32878 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32879 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32880 if (colon)
32881 cp_parser_require (parser, CPP_COLON, RT_COLON);
32882 else if (!parens.require_close (parser))
32883 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32884 /*or_comma=*/false,
32885 /*consume_paren=*/true);
32888 if (colon)
32890 step = NULL_TREE;
32891 if (declare_simd
32892 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32893 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32895 cp_token *token = cp_lexer_peek_token (parser->lexer);
32896 cp_parser_parse_tentatively (parser);
32897 step = cp_parser_id_expression (parser, /*template_p=*/false,
32898 /*check_dependency_p=*/true,
32899 /*template_p=*/NULL,
32900 /*declarator_p=*/false,
32901 /*optional_p=*/false);
32902 if (step != error_mark_node)
32903 step = cp_parser_lookup_name_simple (parser, step, token->location);
32904 if (step == error_mark_node)
32906 step = NULL_TREE;
32907 cp_parser_abort_tentative_parse (parser);
32909 else if (!cp_parser_parse_definitely (parser))
32910 step = NULL_TREE;
32912 if (!step)
32913 step = cp_parser_expression (parser);
32915 if (!parens.require_close (parser))
32916 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32917 /*or_comma=*/false,
32918 /*consume_paren=*/true);
32920 if (step == error_mark_node)
32921 return list;
32924 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32926 OMP_CLAUSE_LINEAR_STEP (c) = step;
32927 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32930 return nlist;
32933 /* OpenMP 4.0:
32934 safelen ( constant-expression ) */
32936 static tree
32937 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32938 location_t location)
32940 tree t, c;
32942 matching_parens parens;
32943 if (!parens.require_open (parser))
32944 return list;
32946 t = cp_parser_constant_expression (parser);
32948 if (t == error_mark_node
32949 || !parens.require_close (parser))
32950 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32951 /*or_comma=*/false,
32952 /*consume_paren=*/true);
32954 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32956 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32957 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32958 OMP_CLAUSE_CHAIN (c) = list;
32960 return c;
32963 /* OpenMP 4.0:
32964 simdlen ( constant-expression ) */
32966 static tree
32967 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32968 location_t location)
32970 tree t, c;
32972 matching_parens parens;
32973 if (!parens.require_open (parser))
32974 return list;
32976 t = cp_parser_constant_expression (parser);
32978 if (t == error_mark_node
32979 || !parens.require_close (parser))
32980 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32981 /*or_comma=*/false,
32982 /*consume_paren=*/true);
32984 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32986 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32987 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32988 OMP_CLAUSE_CHAIN (c) = list;
32990 return c;
32993 /* OpenMP 4.5:
32994 vec:
32995 identifier [+/- integer]
32996 vec , identifier [+/- integer]
32999 static tree
33000 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33001 tree list)
33003 tree vec = NULL;
33005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33007 cp_parser_error (parser, "expected identifier");
33008 return list;
33011 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33013 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33014 tree t, identifier = cp_parser_identifier (parser);
33015 tree addend = NULL;
33017 if (identifier == error_mark_node)
33018 t = error_mark_node;
33019 else
33021 t = cp_parser_lookup_name_simple
33022 (parser, identifier,
33023 cp_lexer_peek_token (parser->lexer)->location);
33024 if (t == error_mark_node)
33025 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33026 id_loc);
33029 bool neg = false;
33030 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33031 neg = true;
33032 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33034 addend = integer_zero_node;
33035 goto add_to_vector;
33037 cp_lexer_consume_token (parser->lexer);
33039 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33041 cp_parser_error (parser, "expected integer");
33042 return list;
33045 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33046 if (TREE_CODE (addend) != INTEGER_CST)
33048 cp_parser_error (parser, "expected integer");
33049 return list;
33051 cp_lexer_consume_token (parser->lexer);
33053 add_to_vector:
33054 if (t != error_mark_node)
33056 vec = tree_cons (addend, t, vec);
33057 if (neg)
33058 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33062 break;
33064 cp_lexer_consume_token (parser->lexer);
33067 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33069 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33070 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33071 OMP_CLAUSE_DECL (u) = nreverse (vec);
33072 OMP_CLAUSE_CHAIN (u) = list;
33073 return u;
33075 return list;
33078 /* OpenMP 4.0:
33079 depend ( depend-kind : variable-list )
33081 depend-kind:
33082 in | out | inout
33084 OpenMP 4.5:
33085 depend ( source )
33087 depend ( sink : vec ) */
33089 static tree
33090 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33092 tree nlist, c;
33093 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33095 matching_parens parens;
33096 if (!parens.require_open (parser))
33097 return list;
33099 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33101 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33102 const char *p = IDENTIFIER_POINTER (id);
33104 if (strcmp ("in", p) == 0)
33105 kind = OMP_CLAUSE_DEPEND_IN;
33106 else if (strcmp ("inout", p) == 0)
33107 kind = OMP_CLAUSE_DEPEND_INOUT;
33108 else if (strcmp ("out", p) == 0)
33109 kind = OMP_CLAUSE_DEPEND_OUT;
33110 else if (strcmp ("source", p) == 0)
33111 kind = OMP_CLAUSE_DEPEND_SOURCE;
33112 else if (strcmp ("sink", p) == 0)
33113 kind = OMP_CLAUSE_DEPEND_SINK;
33114 else
33115 goto invalid_kind;
33117 else
33118 goto invalid_kind;
33120 cp_lexer_consume_token (parser->lexer);
33122 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33124 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33125 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33126 OMP_CLAUSE_DECL (c) = NULL_TREE;
33127 OMP_CLAUSE_CHAIN (c) = list;
33128 if (!parens.require_close (parser))
33129 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33130 /*or_comma=*/false,
33131 /*consume_paren=*/true);
33132 return c;
33135 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33136 goto resync_fail;
33138 if (kind == OMP_CLAUSE_DEPEND_SINK)
33139 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33140 else
33142 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33143 list, NULL);
33145 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33146 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33148 return nlist;
33150 invalid_kind:
33151 cp_parser_error (parser, "invalid depend kind");
33152 resync_fail:
33153 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33154 /*or_comma=*/false,
33155 /*consume_paren=*/true);
33156 return list;
33159 /* OpenMP 4.0:
33160 map ( map-kind : variable-list )
33161 map ( variable-list )
33163 map-kind:
33164 alloc | to | from | tofrom
33166 OpenMP 4.5:
33167 map-kind:
33168 alloc | to | from | tofrom | release | delete
33170 map ( always [,] map-kind: variable-list ) */
33172 static tree
33173 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33175 tree nlist, c;
33176 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33177 bool always = false;
33179 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33180 return list;
33182 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33184 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33185 const char *p = IDENTIFIER_POINTER (id);
33187 if (strcmp ("always", p) == 0)
33189 int nth = 2;
33190 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33191 nth++;
33192 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33193 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33194 == RID_DELETE))
33195 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33196 == CPP_COLON))
33198 always = true;
33199 cp_lexer_consume_token (parser->lexer);
33200 if (nth == 3)
33201 cp_lexer_consume_token (parser->lexer);
33206 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33207 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33209 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33210 const char *p = IDENTIFIER_POINTER (id);
33212 if (strcmp ("alloc", p) == 0)
33213 kind = GOMP_MAP_ALLOC;
33214 else if (strcmp ("to", p) == 0)
33215 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33216 else if (strcmp ("from", p) == 0)
33217 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33218 else if (strcmp ("tofrom", p) == 0)
33219 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33220 else if (strcmp ("release", p) == 0)
33221 kind = GOMP_MAP_RELEASE;
33222 else
33224 cp_parser_error (parser, "invalid map kind");
33225 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33226 /*or_comma=*/false,
33227 /*consume_paren=*/true);
33228 return list;
33230 cp_lexer_consume_token (parser->lexer);
33231 cp_lexer_consume_token (parser->lexer);
33233 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33234 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33236 kind = GOMP_MAP_DELETE;
33237 cp_lexer_consume_token (parser->lexer);
33238 cp_lexer_consume_token (parser->lexer);
33241 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33242 NULL);
33244 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33245 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33247 return nlist;
33250 /* OpenMP 4.0:
33251 device ( expression ) */
33253 static tree
33254 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33255 location_t location)
33257 tree t, c;
33259 matching_parens parens;
33260 if (!parens.require_open (parser))
33261 return list;
33263 t = cp_parser_expression (parser);
33265 if (t == error_mark_node
33266 || !parens.require_close (parser))
33267 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33268 /*or_comma=*/false,
33269 /*consume_paren=*/true);
33271 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33272 "device", location);
33274 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33275 OMP_CLAUSE_DEVICE_ID (c) = t;
33276 OMP_CLAUSE_CHAIN (c) = list;
33278 return c;
33281 /* OpenMP 4.0:
33282 dist_schedule ( static )
33283 dist_schedule ( static , expression ) */
33285 static tree
33286 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33287 location_t location)
33289 tree c, t;
33291 matching_parens parens;
33292 if (!parens.require_open (parser))
33293 return list;
33295 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33297 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33298 goto invalid_kind;
33299 cp_lexer_consume_token (parser->lexer);
33301 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33303 cp_lexer_consume_token (parser->lexer);
33305 t = cp_parser_assignment_expression (parser);
33307 if (t == error_mark_node)
33308 goto resync_fail;
33309 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33311 if (!parens.require_close (parser))
33312 goto resync_fail;
33314 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33315 goto resync_fail;
33317 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33318 location);
33319 OMP_CLAUSE_CHAIN (c) = list;
33320 return c;
33322 invalid_kind:
33323 cp_parser_error (parser, "invalid dist_schedule kind");
33324 resync_fail:
33325 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33326 /*or_comma=*/false,
33327 /*consume_paren=*/true);
33328 return list;
33331 /* OpenMP 4.0:
33332 proc_bind ( proc-bind-kind )
33334 proc-bind-kind:
33335 master | close | spread */
33337 static tree
33338 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33339 location_t location)
33341 tree c;
33342 enum omp_clause_proc_bind_kind kind;
33344 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33345 return list;
33347 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33349 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33350 const char *p = IDENTIFIER_POINTER (id);
33352 if (strcmp ("master", p) == 0)
33353 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33354 else if (strcmp ("close", p) == 0)
33355 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33356 else if (strcmp ("spread", p) == 0)
33357 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33358 else
33359 goto invalid_kind;
33361 else
33362 goto invalid_kind;
33364 cp_lexer_consume_token (parser->lexer);
33365 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33366 goto resync_fail;
33368 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33369 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33370 location);
33371 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33372 OMP_CLAUSE_CHAIN (c) = list;
33373 return c;
33375 invalid_kind:
33376 cp_parser_error (parser, "invalid depend kind");
33377 resync_fail:
33378 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33379 /*or_comma=*/false,
33380 /*consume_paren=*/true);
33381 return list;
33384 /* OpenACC:
33385 async [( int-expr )] */
33387 static tree
33388 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33390 tree c, t;
33391 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33393 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33395 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33397 matching_parens parens;
33398 parens.consume_open (parser);
33400 t = cp_parser_expression (parser);
33401 if (t == error_mark_node
33402 || !parens.require_close (parser))
33403 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33404 /*or_comma=*/false,
33405 /*consume_paren=*/true);
33408 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33410 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33411 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33412 OMP_CLAUSE_CHAIN (c) = list;
33413 list = c;
33415 return list;
33418 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33419 is a bitmask in MASK. Return the list of clauses found. */
33421 static tree
33422 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33423 const char *where, cp_token *pragma_tok,
33424 bool finish_p = true)
33426 tree clauses = NULL;
33427 bool first = true;
33429 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33431 location_t here;
33432 pragma_omp_clause c_kind;
33433 omp_clause_code code;
33434 const char *c_name;
33435 tree prev = clauses;
33437 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33438 cp_lexer_consume_token (parser->lexer);
33440 here = cp_lexer_peek_token (parser->lexer)->location;
33441 c_kind = cp_parser_omp_clause_name (parser);
33443 switch (c_kind)
33445 case PRAGMA_OACC_CLAUSE_ASYNC:
33446 clauses = cp_parser_oacc_clause_async (parser, clauses);
33447 c_name = "async";
33448 break;
33449 case PRAGMA_OACC_CLAUSE_AUTO:
33450 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33451 clauses, here);
33452 c_name = "auto";
33453 break;
33454 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33455 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33456 c_name = "collapse";
33457 break;
33458 case PRAGMA_OACC_CLAUSE_COPY:
33459 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33460 c_name = "copy";
33461 break;
33462 case PRAGMA_OACC_CLAUSE_COPYIN:
33463 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33464 c_name = "copyin";
33465 break;
33466 case PRAGMA_OACC_CLAUSE_COPYOUT:
33467 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33468 c_name = "copyout";
33469 break;
33470 case PRAGMA_OACC_CLAUSE_CREATE:
33471 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33472 c_name = "create";
33473 break;
33474 case PRAGMA_OACC_CLAUSE_DELETE:
33475 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33476 c_name = "delete";
33477 break;
33478 case PRAGMA_OMP_CLAUSE_DEFAULT:
33479 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33480 c_name = "default";
33481 break;
33482 case PRAGMA_OACC_CLAUSE_DEVICE:
33483 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33484 c_name = "device";
33485 break;
33486 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33487 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33488 c_name = "deviceptr";
33489 break;
33490 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33491 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33492 c_name = "device_resident";
33493 break;
33494 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33495 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33496 clauses);
33497 c_name = "firstprivate";
33498 break;
33499 case PRAGMA_OACC_CLAUSE_GANG:
33500 c_name = "gang";
33501 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33502 c_name, clauses);
33503 break;
33504 case PRAGMA_OACC_CLAUSE_HOST:
33505 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33506 c_name = "host";
33507 break;
33508 case PRAGMA_OACC_CLAUSE_IF:
33509 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33510 c_name = "if";
33511 break;
33512 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33513 clauses = cp_parser_oacc_simple_clause (parser,
33514 OMP_CLAUSE_INDEPENDENT,
33515 clauses, here);
33516 c_name = "independent";
33517 break;
33518 case PRAGMA_OACC_CLAUSE_LINK:
33519 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33520 c_name = "link";
33521 break;
33522 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33523 code = OMP_CLAUSE_NUM_GANGS;
33524 c_name = "num_gangs";
33525 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33526 clauses);
33527 break;
33528 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33529 c_name = "num_workers";
33530 code = OMP_CLAUSE_NUM_WORKERS;
33531 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33532 clauses);
33533 break;
33534 case PRAGMA_OACC_CLAUSE_PRESENT:
33535 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33536 c_name = "present";
33537 break;
33538 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33539 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33540 c_name = "present_or_copy";
33541 break;
33542 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33543 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33544 c_name = "present_or_copyin";
33545 break;
33546 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33547 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33548 c_name = "present_or_copyout";
33549 break;
33550 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33551 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33552 c_name = "present_or_create";
33553 break;
33554 case PRAGMA_OACC_CLAUSE_PRIVATE:
33555 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33556 clauses);
33557 c_name = "private";
33558 break;
33559 case PRAGMA_OACC_CLAUSE_REDUCTION:
33560 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33561 c_name = "reduction";
33562 break;
33563 case PRAGMA_OACC_CLAUSE_SELF:
33564 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33565 c_name = "self";
33566 break;
33567 case PRAGMA_OACC_CLAUSE_SEQ:
33568 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33569 clauses, here);
33570 c_name = "seq";
33571 break;
33572 case PRAGMA_OACC_CLAUSE_TILE:
33573 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33574 c_name = "tile";
33575 break;
33576 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33577 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33578 clauses);
33579 c_name = "use_device";
33580 break;
33581 case PRAGMA_OACC_CLAUSE_VECTOR:
33582 c_name = "vector";
33583 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33584 c_name, clauses);
33585 break;
33586 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33587 c_name = "vector_length";
33588 code = OMP_CLAUSE_VECTOR_LENGTH;
33589 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33590 clauses);
33591 break;
33592 case PRAGMA_OACC_CLAUSE_WAIT:
33593 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33594 c_name = "wait";
33595 break;
33596 case PRAGMA_OACC_CLAUSE_WORKER:
33597 c_name = "worker";
33598 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33599 c_name, clauses);
33600 break;
33601 default:
33602 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33603 goto saw_error;
33606 first = false;
33608 if (((mask >> c_kind) & 1) == 0)
33610 /* Remove the invalid clause(s) from the list to avoid
33611 confusing the rest of the compiler. */
33612 clauses = prev;
33613 error_at (here, "%qs is not valid for %qs", c_name, where);
33617 saw_error:
33618 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33620 if (finish_p)
33621 return finish_omp_clauses (clauses, C_ORT_ACC);
33623 return clauses;
33626 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33627 is a bitmask in MASK. Return the list of clauses found; the result
33628 of clause default goes in *pdefault. */
33630 static tree
33631 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33632 const char *where, cp_token *pragma_tok,
33633 bool finish_p = true)
33635 tree clauses = NULL;
33636 bool first = true;
33637 cp_token *token = NULL;
33639 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33641 pragma_omp_clause c_kind;
33642 const char *c_name;
33643 tree prev = clauses;
33645 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33646 cp_lexer_consume_token (parser->lexer);
33648 token = cp_lexer_peek_token (parser->lexer);
33649 c_kind = cp_parser_omp_clause_name (parser);
33651 switch (c_kind)
33653 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33654 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33655 token->location);
33656 c_name = "collapse";
33657 break;
33658 case PRAGMA_OMP_CLAUSE_COPYIN:
33659 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33660 c_name = "copyin";
33661 break;
33662 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33663 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33664 clauses);
33665 c_name = "copyprivate";
33666 break;
33667 case PRAGMA_OMP_CLAUSE_DEFAULT:
33668 clauses = cp_parser_omp_clause_default (parser, clauses,
33669 token->location, false);
33670 c_name = "default";
33671 break;
33672 case PRAGMA_OMP_CLAUSE_FINAL:
33673 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33674 c_name = "final";
33675 break;
33676 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33677 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33678 clauses);
33679 c_name = "firstprivate";
33680 break;
33681 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33682 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33683 token->location);
33684 c_name = "grainsize";
33685 break;
33686 case PRAGMA_OMP_CLAUSE_HINT:
33687 clauses = cp_parser_omp_clause_hint (parser, clauses,
33688 token->location);
33689 c_name = "hint";
33690 break;
33691 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33692 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33693 token->location);
33694 c_name = "defaultmap";
33695 break;
33696 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33697 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33698 clauses);
33699 c_name = "use_device_ptr";
33700 break;
33701 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33702 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33703 clauses);
33704 c_name = "is_device_ptr";
33705 break;
33706 case PRAGMA_OMP_CLAUSE_IF:
33707 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33708 true);
33709 c_name = "if";
33710 break;
33711 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33712 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33713 clauses);
33714 c_name = "lastprivate";
33715 break;
33716 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33717 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33718 token->location);
33719 c_name = "mergeable";
33720 break;
33721 case PRAGMA_OMP_CLAUSE_NOWAIT:
33722 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33723 c_name = "nowait";
33724 break;
33725 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33726 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33727 token->location);
33728 c_name = "num_tasks";
33729 break;
33730 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33731 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33732 token->location);
33733 c_name = "num_threads";
33734 break;
33735 case PRAGMA_OMP_CLAUSE_ORDERED:
33736 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33737 token->location);
33738 c_name = "ordered";
33739 break;
33740 case PRAGMA_OMP_CLAUSE_PRIORITY:
33741 clauses = cp_parser_omp_clause_priority (parser, clauses,
33742 token->location);
33743 c_name = "priority";
33744 break;
33745 case PRAGMA_OMP_CLAUSE_PRIVATE:
33746 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33747 clauses);
33748 c_name = "private";
33749 break;
33750 case PRAGMA_OMP_CLAUSE_REDUCTION:
33751 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33752 c_name = "reduction";
33753 break;
33754 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33755 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33756 token->location);
33757 c_name = "schedule";
33758 break;
33759 case PRAGMA_OMP_CLAUSE_SHARED:
33760 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33761 clauses);
33762 c_name = "shared";
33763 break;
33764 case PRAGMA_OMP_CLAUSE_UNTIED:
33765 clauses = cp_parser_omp_clause_untied (parser, clauses,
33766 token->location);
33767 c_name = "untied";
33768 break;
33769 case PRAGMA_OMP_CLAUSE_INBRANCH:
33770 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33771 clauses, token->location);
33772 c_name = "inbranch";
33773 break;
33774 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33775 clauses = cp_parser_omp_clause_branch (parser,
33776 OMP_CLAUSE_NOTINBRANCH,
33777 clauses, token->location);
33778 c_name = "notinbranch";
33779 break;
33780 case PRAGMA_OMP_CLAUSE_PARALLEL:
33781 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33782 clauses, token->location);
33783 c_name = "parallel";
33784 if (!first)
33786 clause_not_first:
33787 error_at (token->location, "%qs must be the first clause of %qs",
33788 c_name, where);
33789 clauses = prev;
33791 break;
33792 case PRAGMA_OMP_CLAUSE_FOR:
33793 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33794 clauses, token->location);
33795 c_name = "for";
33796 if (!first)
33797 goto clause_not_first;
33798 break;
33799 case PRAGMA_OMP_CLAUSE_SECTIONS:
33800 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33801 clauses, token->location);
33802 c_name = "sections";
33803 if (!first)
33804 goto clause_not_first;
33805 break;
33806 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33807 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33808 clauses, token->location);
33809 c_name = "taskgroup";
33810 if (!first)
33811 goto clause_not_first;
33812 break;
33813 case PRAGMA_OMP_CLAUSE_LINK:
33814 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33815 c_name = "to";
33816 break;
33817 case PRAGMA_OMP_CLAUSE_TO:
33818 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33819 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33820 clauses);
33821 else
33822 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33823 c_name = "to";
33824 break;
33825 case PRAGMA_OMP_CLAUSE_FROM:
33826 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33827 c_name = "from";
33828 break;
33829 case PRAGMA_OMP_CLAUSE_UNIFORM:
33830 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33831 clauses);
33832 c_name = "uniform";
33833 break;
33834 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33835 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33836 token->location);
33837 c_name = "num_teams";
33838 break;
33839 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33840 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33841 token->location);
33842 c_name = "thread_limit";
33843 break;
33844 case PRAGMA_OMP_CLAUSE_ALIGNED:
33845 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33846 c_name = "aligned";
33847 break;
33848 case PRAGMA_OMP_CLAUSE_LINEAR:
33850 bool declare_simd = false;
33851 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33852 declare_simd = true;
33853 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
33855 c_name = "linear";
33856 break;
33857 case PRAGMA_OMP_CLAUSE_DEPEND:
33858 clauses = cp_parser_omp_clause_depend (parser, clauses,
33859 token->location);
33860 c_name = "depend";
33861 break;
33862 case PRAGMA_OMP_CLAUSE_MAP:
33863 clauses = cp_parser_omp_clause_map (parser, clauses);
33864 c_name = "map";
33865 break;
33866 case PRAGMA_OMP_CLAUSE_DEVICE:
33867 clauses = cp_parser_omp_clause_device (parser, clauses,
33868 token->location);
33869 c_name = "device";
33870 break;
33871 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33872 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33873 token->location);
33874 c_name = "dist_schedule";
33875 break;
33876 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33877 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33878 token->location);
33879 c_name = "proc_bind";
33880 break;
33881 case PRAGMA_OMP_CLAUSE_SAFELEN:
33882 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33883 token->location);
33884 c_name = "safelen";
33885 break;
33886 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33887 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33888 token->location);
33889 c_name = "simdlen";
33890 break;
33891 case PRAGMA_OMP_CLAUSE_NOGROUP:
33892 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33893 token->location);
33894 c_name = "nogroup";
33895 break;
33896 case PRAGMA_OMP_CLAUSE_THREADS:
33897 clauses
33898 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33899 clauses, token->location);
33900 c_name = "threads";
33901 break;
33902 case PRAGMA_OMP_CLAUSE_SIMD:
33903 clauses
33904 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33905 clauses, token->location);
33906 c_name = "simd";
33907 break;
33908 default:
33909 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33910 goto saw_error;
33913 first = false;
33915 if (((mask >> c_kind) & 1) == 0)
33917 /* Remove the invalid clause(s) from the list to avoid
33918 confusing the rest of the compiler. */
33919 clauses = prev;
33920 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33923 saw_error:
33924 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33925 if (finish_p)
33927 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33928 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33929 else
33930 return finish_omp_clauses (clauses, C_ORT_OMP);
33932 return clauses;
33935 /* OpenMP 2.5:
33936 structured-block:
33937 statement
33939 In practice, we're also interested in adding the statement to an
33940 outer node. So it is convenient if we work around the fact that
33941 cp_parser_statement calls add_stmt. */
33943 static unsigned
33944 cp_parser_begin_omp_structured_block (cp_parser *parser)
33946 unsigned save = parser->in_statement;
33948 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33949 This preserves the "not within loop or switch" style error messages
33950 for nonsense cases like
33951 void foo() {
33952 #pragma omp single
33953 break;
33956 if (parser->in_statement)
33957 parser->in_statement = IN_OMP_BLOCK;
33959 return save;
33962 static void
33963 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33965 parser->in_statement = save;
33968 static tree
33969 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33971 tree stmt = begin_omp_structured_block ();
33972 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33974 cp_parser_statement (parser, NULL_TREE, false, if_p);
33976 cp_parser_end_omp_structured_block (parser, save);
33977 return finish_omp_structured_block (stmt);
33980 /* OpenMP 2.5:
33981 # pragma omp atomic new-line
33982 expression-stmt
33984 expression-stmt:
33985 x binop= expr | x++ | ++x | x-- | --x
33986 binop:
33987 +, *, -, /, &, ^, |, <<, >>
33989 where x is an lvalue expression with scalar type.
33991 OpenMP 3.1:
33992 # pragma omp atomic new-line
33993 update-stmt
33995 # pragma omp atomic read new-line
33996 read-stmt
33998 # pragma omp atomic write new-line
33999 write-stmt
34001 # pragma omp atomic update new-line
34002 update-stmt
34004 # pragma omp atomic capture new-line
34005 capture-stmt
34007 # pragma omp atomic capture new-line
34008 capture-block
34010 read-stmt:
34011 v = x
34012 write-stmt:
34013 x = expr
34014 update-stmt:
34015 expression-stmt | x = x binop expr
34016 capture-stmt:
34017 v = expression-stmt
34018 capture-block:
34019 { v = x; update-stmt; } | { update-stmt; v = x; }
34021 OpenMP 4.0:
34022 update-stmt:
34023 expression-stmt | x = x binop expr | x = expr binop x
34024 capture-stmt:
34025 v = update-stmt
34026 capture-block:
34027 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34029 where x and v are lvalue expressions with scalar type. */
34031 static void
34032 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34034 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34035 tree rhs1 = NULL_TREE, orig_lhs;
34036 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34037 bool structured_block = false;
34038 bool seq_cst = false;
34040 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34042 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34043 const char *p = IDENTIFIER_POINTER (id);
34045 if (!strcmp (p, "seq_cst"))
34047 seq_cst = true;
34048 cp_lexer_consume_token (parser->lexer);
34049 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34050 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34051 cp_lexer_consume_token (parser->lexer);
34054 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34056 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34057 const char *p = IDENTIFIER_POINTER (id);
34059 if (!strcmp (p, "read"))
34060 code = OMP_ATOMIC_READ;
34061 else if (!strcmp (p, "write"))
34062 code = NOP_EXPR;
34063 else if (!strcmp (p, "update"))
34064 code = OMP_ATOMIC;
34065 else if (!strcmp (p, "capture"))
34066 code = OMP_ATOMIC_CAPTURE_NEW;
34067 else
34068 p = NULL;
34069 if (p)
34070 cp_lexer_consume_token (parser->lexer);
34072 if (!seq_cst)
34074 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34075 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34076 cp_lexer_consume_token (parser->lexer);
34078 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34080 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34081 const char *p = IDENTIFIER_POINTER (id);
34083 if (!strcmp (p, "seq_cst"))
34085 seq_cst = true;
34086 cp_lexer_consume_token (parser->lexer);
34090 cp_parser_require_pragma_eol (parser, pragma_tok);
34092 switch (code)
34094 case OMP_ATOMIC_READ:
34095 case NOP_EXPR: /* atomic write */
34096 v = cp_parser_unary_expression (parser);
34097 if (v == error_mark_node)
34098 goto saw_error;
34099 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34100 goto saw_error;
34101 if (code == NOP_EXPR)
34102 lhs = cp_parser_expression (parser);
34103 else
34104 lhs = cp_parser_unary_expression (parser);
34105 if (lhs == error_mark_node)
34106 goto saw_error;
34107 if (code == NOP_EXPR)
34109 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34110 opcode. */
34111 code = OMP_ATOMIC;
34112 rhs = lhs;
34113 lhs = v;
34114 v = NULL_TREE;
34116 goto done;
34117 case OMP_ATOMIC_CAPTURE_NEW:
34118 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34120 cp_lexer_consume_token (parser->lexer);
34121 structured_block = true;
34123 else
34125 v = cp_parser_unary_expression (parser);
34126 if (v == error_mark_node)
34127 goto saw_error;
34128 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34129 goto saw_error;
34131 default:
34132 break;
34135 restart:
34136 lhs = cp_parser_unary_expression (parser);
34137 orig_lhs = lhs;
34138 switch (TREE_CODE (lhs))
34140 case ERROR_MARK:
34141 goto saw_error;
34143 case POSTINCREMENT_EXPR:
34144 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34145 code = OMP_ATOMIC_CAPTURE_OLD;
34146 /* FALLTHROUGH */
34147 case PREINCREMENT_EXPR:
34148 lhs = TREE_OPERAND (lhs, 0);
34149 opcode = PLUS_EXPR;
34150 rhs = integer_one_node;
34151 break;
34153 case POSTDECREMENT_EXPR:
34154 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34155 code = OMP_ATOMIC_CAPTURE_OLD;
34156 /* FALLTHROUGH */
34157 case PREDECREMENT_EXPR:
34158 lhs = TREE_OPERAND (lhs, 0);
34159 opcode = MINUS_EXPR;
34160 rhs = integer_one_node;
34161 break;
34163 case COMPOUND_EXPR:
34164 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34165 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34166 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34167 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34168 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34169 (TREE_OPERAND (lhs, 1), 0), 0)))
34170 == BOOLEAN_TYPE)
34171 /* Undo effects of boolean_increment for post {in,de}crement. */
34172 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34173 /* FALLTHRU */
34174 case MODIFY_EXPR:
34175 if (TREE_CODE (lhs) == MODIFY_EXPR
34176 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34178 /* Undo effects of boolean_increment. */
34179 if (integer_onep (TREE_OPERAND (lhs, 1)))
34181 /* This is pre or post increment. */
34182 rhs = TREE_OPERAND (lhs, 1);
34183 lhs = TREE_OPERAND (lhs, 0);
34184 opcode = NOP_EXPR;
34185 if (code == OMP_ATOMIC_CAPTURE_NEW
34186 && !structured_block
34187 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34188 code = OMP_ATOMIC_CAPTURE_OLD;
34189 break;
34192 /* FALLTHRU */
34193 default:
34194 switch (cp_lexer_peek_token (parser->lexer)->type)
34196 case CPP_MULT_EQ:
34197 opcode = MULT_EXPR;
34198 break;
34199 case CPP_DIV_EQ:
34200 opcode = TRUNC_DIV_EXPR;
34201 break;
34202 case CPP_PLUS_EQ:
34203 opcode = PLUS_EXPR;
34204 break;
34205 case CPP_MINUS_EQ:
34206 opcode = MINUS_EXPR;
34207 break;
34208 case CPP_LSHIFT_EQ:
34209 opcode = LSHIFT_EXPR;
34210 break;
34211 case CPP_RSHIFT_EQ:
34212 opcode = RSHIFT_EXPR;
34213 break;
34214 case CPP_AND_EQ:
34215 opcode = BIT_AND_EXPR;
34216 break;
34217 case CPP_OR_EQ:
34218 opcode = BIT_IOR_EXPR;
34219 break;
34220 case CPP_XOR_EQ:
34221 opcode = BIT_XOR_EXPR;
34222 break;
34223 case CPP_EQ:
34224 enum cp_parser_prec oprec;
34225 cp_token *token;
34226 cp_lexer_consume_token (parser->lexer);
34227 cp_parser_parse_tentatively (parser);
34228 rhs1 = cp_parser_simple_cast_expression (parser);
34229 if (rhs1 == error_mark_node)
34231 cp_parser_abort_tentative_parse (parser);
34232 cp_parser_simple_cast_expression (parser);
34233 goto saw_error;
34235 token = cp_lexer_peek_token (parser->lexer);
34236 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34238 cp_parser_abort_tentative_parse (parser);
34239 cp_parser_parse_tentatively (parser);
34240 rhs = cp_parser_binary_expression (parser, false, true,
34241 PREC_NOT_OPERATOR, NULL);
34242 if (rhs == error_mark_node)
34244 cp_parser_abort_tentative_parse (parser);
34245 cp_parser_binary_expression (parser, false, true,
34246 PREC_NOT_OPERATOR, NULL);
34247 goto saw_error;
34249 switch (TREE_CODE (rhs))
34251 case MULT_EXPR:
34252 case TRUNC_DIV_EXPR:
34253 case RDIV_EXPR:
34254 case PLUS_EXPR:
34255 case MINUS_EXPR:
34256 case LSHIFT_EXPR:
34257 case RSHIFT_EXPR:
34258 case BIT_AND_EXPR:
34259 case BIT_IOR_EXPR:
34260 case BIT_XOR_EXPR:
34261 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34263 if (cp_parser_parse_definitely (parser))
34265 opcode = TREE_CODE (rhs);
34266 rhs1 = TREE_OPERAND (rhs, 0);
34267 rhs = TREE_OPERAND (rhs, 1);
34268 goto stmt_done;
34270 else
34271 goto saw_error;
34273 break;
34274 default:
34275 break;
34277 cp_parser_abort_tentative_parse (parser);
34278 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34280 rhs = cp_parser_expression (parser);
34281 if (rhs == error_mark_node)
34282 goto saw_error;
34283 opcode = NOP_EXPR;
34284 rhs1 = NULL_TREE;
34285 goto stmt_done;
34287 cp_parser_error (parser,
34288 "invalid form of %<#pragma omp atomic%>");
34289 goto saw_error;
34291 if (!cp_parser_parse_definitely (parser))
34292 goto saw_error;
34293 switch (token->type)
34295 case CPP_SEMICOLON:
34296 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34298 code = OMP_ATOMIC_CAPTURE_OLD;
34299 v = lhs;
34300 lhs = NULL_TREE;
34301 lhs1 = rhs1;
34302 rhs1 = NULL_TREE;
34303 cp_lexer_consume_token (parser->lexer);
34304 goto restart;
34306 else if (structured_block)
34308 opcode = NOP_EXPR;
34309 rhs = rhs1;
34310 rhs1 = NULL_TREE;
34311 goto stmt_done;
34313 cp_parser_error (parser,
34314 "invalid form of %<#pragma omp atomic%>");
34315 goto saw_error;
34316 case CPP_MULT:
34317 opcode = MULT_EXPR;
34318 break;
34319 case CPP_DIV:
34320 opcode = TRUNC_DIV_EXPR;
34321 break;
34322 case CPP_PLUS:
34323 opcode = PLUS_EXPR;
34324 break;
34325 case CPP_MINUS:
34326 opcode = MINUS_EXPR;
34327 break;
34328 case CPP_LSHIFT:
34329 opcode = LSHIFT_EXPR;
34330 break;
34331 case CPP_RSHIFT:
34332 opcode = RSHIFT_EXPR;
34333 break;
34334 case CPP_AND:
34335 opcode = BIT_AND_EXPR;
34336 break;
34337 case CPP_OR:
34338 opcode = BIT_IOR_EXPR;
34339 break;
34340 case CPP_XOR:
34341 opcode = BIT_XOR_EXPR;
34342 break;
34343 default:
34344 cp_parser_error (parser,
34345 "invalid operator for %<#pragma omp atomic%>");
34346 goto saw_error;
34348 oprec = TOKEN_PRECEDENCE (token);
34349 gcc_assert (oprec != PREC_NOT_OPERATOR);
34350 if (commutative_tree_code (opcode))
34351 oprec = (enum cp_parser_prec) (oprec - 1);
34352 cp_lexer_consume_token (parser->lexer);
34353 rhs = cp_parser_binary_expression (parser, false, false,
34354 oprec, NULL);
34355 if (rhs == error_mark_node)
34356 goto saw_error;
34357 goto stmt_done;
34358 /* FALLTHROUGH */
34359 default:
34360 cp_parser_error (parser,
34361 "invalid operator for %<#pragma omp atomic%>");
34362 goto saw_error;
34364 cp_lexer_consume_token (parser->lexer);
34366 rhs = cp_parser_expression (parser);
34367 if (rhs == error_mark_node)
34368 goto saw_error;
34369 break;
34371 stmt_done:
34372 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34374 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34375 goto saw_error;
34376 v = cp_parser_unary_expression (parser);
34377 if (v == error_mark_node)
34378 goto saw_error;
34379 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34380 goto saw_error;
34381 lhs1 = cp_parser_unary_expression (parser);
34382 if (lhs1 == error_mark_node)
34383 goto saw_error;
34385 if (structured_block)
34387 cp_parser_consume_semicolon_at_end_of_statement (parser);
34388 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34390 done:
34391 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34392 if (!structured_block)
34393 cp_parser_consume_semicolon_at_end_of_statement (parser);
34394 return;
34396 saw_error:
34397 cp_parser_skip_to_end_of_block_or_statement (parser);
34398 if (structured_block)
34400 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34401 cp_lexer_consume_token (parser->lexer);
34402 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34404 cp_parser_skip_to_end_of_block_or_statement (parser);
34405 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34406 cp_lexer_consume_token (parser->lexer);
34412 /* OpenMP 2.5:
34413 # pragma omp barrier new-line */
34415 static void
34416 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34418 cp_parser_require_pragma_eol (parser, pragma_tok);
34419 finish_omp_barrier ();
34422 /* OpenMP 2.5:
34423 # pragma omp critical [(name)] new-line
34424 structured-block
34426 OpenMP 4.5:
34427 # pragma omp critical [(name) [hint(expression)]] new-line
34428 structured-block */
34430 #define OMP_CRITICAL_CLAUSE_MASK \
34431 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34433 static tree
34434 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34436 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34438 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34440 matching_parens parens;
34441 parens.consume_open (parser);
34443 name = cp_parser_identifier (parser);
34445 if (name == error_mark_node
34446 || !parens.require_close (parser))
34447 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34448 /*or_comma=*/false,
34449 /*consume_paren=*/true);
34450 if (name == error_mark_node)
34451 name = NULL;
34453 clauses = cp_parser_omp_all_clauses (parser,
34454 OMP_CRITICAL_CLAUSE_MASK,
34455 "#pragma omp critical", pragma_tok);
34457 else
34458 cp_parser_require_pragma_eol (parser, pragma_tok);
34460 stmt = cp_parser_omp_structured_block (parser, if_p);
34461 return c_finish_omp_critical (input_location, stmt, name, clauses);
34464 /* OpenMP 2.5:
34465 # pragma omp flush flush-vars[opt] new-line
34467 flush-vars:
34468 ( variable-list ) */
34470 static void
34471 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34473 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34474 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34475 cp_parser_require_pragma_eol (parser, pragma_tok);
34477 finish_omp_flush ();
34480 /* Helper function, to parse omp for increment expression. */
34482 static tree
34483 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34485 tree cond = cp_parser_binary_expression (parser, false, true,
34486 PREC_NOT_OPERATOR, NULL);
34487 if (cond == error_mark_node
34488 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34490 cp_parser_skip_to_end_of_statement (parser);
34491 return error_mark_node;
34494 switch (TREE_CODE (cond))
34496 case GT_EXPR:
34497 case GE_EXPR:
34498 case LT_EXPR:
34499 case LE_EXPR:
34500 break;
34501 case NE_EXPR:
34502 /* Fall through: OpenMP disallows NE_EXPR. */
34503 gcc_fallthrough ();
34504 default:
34505 return error_mark_node;
34508 /* If decl is an iterator, preserve LHS and RHS of the relational
34509 expr until finish_omp_for. */
34510 if (decl
34511 && (type_dependent_expression_p (decl)
34512 || CLASS_TYPE_P (TREE_TYPE (decl))))
34513 return cond;
34515 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34516 TREE_CODE (cond),
34517 TREE_OPERAND (cond, 0), ERROR_MARK,
34518 TREE_OPERAND (cond, 1), ERROR_MARK,
34519 /*overload=*/NULL, tf_warning_or_error);
34522 /* Helper function, to parse omp for increment expression. */
34524 static tree
34525 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34527 cp_token *token = cp_lexer_peek_token (parser->lexer);
34528 enum tree_code op;
34529 tree lhs, rhs;
34530 cp_id_kind idk;
34531 bool decl_first;
34533 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34535 op = (token->type == CPP_PLUS_PLUS
34536 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34537 cp_lexer_consume_token (parser->lexer);
34538 lhs = cp_parser_simple_cast_expression (parser);
34539 if (lhs != decl
34540 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34541 return error_mark_node;
34542 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34545 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34546 if (lhs != decl
34547 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34548 return error_mark_node;
34550 token = cp_lexer_peek_token (parser->lexer);
34551 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34553 op = (token->type == CPP_PLUS_PLUS
34554 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34555 cp_lexer_consume_token (parser->lexer);
34556 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34559 op = cp_parser_assignment_operator_opt (parser);
34560 if (op == ERROR_MARK)
34561 return error_mark_node;
34563 if (op != NOP_EXPR)
34565 rhs = cp_parser_assignment_expression (parser);
34566 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34567 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34570 lhs = cp_parser_binary_expression (parser, false, false,
34571 PREC_ADDITIVE_EXPRESSION, NULL);
34572 token = cp_lexer_peek_token (parser->lexer);
34573 decl_first = (lhs == decl
34574 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34575 if (decl_first)
34576 lhs = NULL_TREE;
34577 if (token->type != CPP_PLUS
34578 && token->type != CPP_MINUS)
34579 return error_mark_node;
34583 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34584 cp_lexer_consume_token (parser->lexer);
34585 rhs = cp_parser_binary_expression (parser, false, false,
34586 PREC_ADDITIVE_EXPRESSION, NULL);
34587 token = cp_lexer_peek_token (parser->lexer);
34588 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34590 if (lhs == NULL_TREE)
34592 if (op == PLUS_EXPR)
34593 lhs = rhs;
34594 else
34595 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34596 tf_warning_or_error);
34598 else
34599 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34600 ERROR_MARK, NULL, tf_warning_or_error);
34603 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34605 if (!decl_first)
34607 if ((rhs != decl
34608 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34609 || op == MINUS_EXPR)
34610 return error_mark_node;
34611 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34613 else
34614 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34616 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34619 /* Parse the initialization statement of an OpenMP for loop.
34621 Return true if the resulting construct should have an
34622 OMP_CLAUSE_PRIVATE added to it. */
34624 static tree
34625 cp_parser_omp_for_loop_init (cp_parser *parser,
34626 tree &this_pre_body,
34627 vec<tree, va_gc> *for_block,
34628 tree &init,
34629 tree &orig_init,
34630 tree &decl,
34631 tree &real_decl)
34633 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34634 return NULL_TREE;
34636 tree add_private_clause = NULL_TREE;
34638 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34640 init-expr:
34641 var = lb
34642 integer-type var = lb
34643 random-access-iterator-type var = lb
34644 pointer-type var = lb
34646 cp_decl_specifier_seq type_specifiers;
34648 /* First, try to parse as an initialized declaration. See
34649 cp_parser_condition, from whence the bulk of this is copied. */
34651 cp_parser_parse_tentatively (parser);
34652 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34653 /*is_trailing_return=*/false,
34654 &type_specifiers);
34655 if (cp_parser_parse_definitely (parser))
34657 /* If parsing a type specifier seq succeeded, then this
34658 MUST be a initialized declaration. */
34659 tree asm_specification, attributes;
34660 cp_declarator *declarator;
34662 declarator = cp_parser_declarator (parser,
34663 CP_PARSER_DECLARATOR_NAMED,
34664 /*ctor_dtor_or_conv_p=*/NULL,
34665 /*parenthesized_p=*/NULL,
34666 /*member_p=*/false,
34667 /*friend_p=*/false);
34668 attributes = cp_parser_attributes_opt (parser);
34669 asm_specification = cp_parser_asm_specification_opt (parser);
34671 if (declarator == cp_error_declarator)
34672 cp_parser_skip_to_end_of_statement (parser);
34674 else
34676 tree pushed_scope, auto_node;
34678 decl = start_decl (declarator, &type_specifiers,
34679 SD_INITIALIZED, attributes,
34680 /*prefix_attributes=*/NULL_TREE,
34681 &pushed_scope);
34683 auto_node = type_uses_auto (TREE_TYPE (decl));
34684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34686 if (cp_lexer_next_token_is (parser->lexer,
34687 CPP_OPEN_PAREN))
34688 error ("parenthesized initialization is not allowed in "
34689 "OpenMP %<for%> loop");
34690 else
34691 /* Trigger an error. */
34692 cp_parser_require (parser, CPP_EQ, RT_EQ);
34694 init = error_mark_node;
34695 cp_parser_skip_to_end_of_statement (parser);
34697 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34698 || type_dependent_expression_p (decl)
34699 || auto_node)
34701 bool is_direct_init, is_non_constant_init;
34703 init = cp_parser_initializer (parser,
34704 &is_direct_init,
34705 &is_non_constant_init);
34707 if (auto_node)
34709 TREE_TYPE (decl)
34710 = do_auto_deduction (TREE_TYPE (decl), init,
34711 auto_node);
34713 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34714 && !type_dependent_expression_p (decl))
34715 goto non_class;
34718 cp_finish_decl (decl, init, !is_non_constant_init,
34719 asm_specification,
34720 LOOKUP_ONLYCONVERTING);
34721 orig_init = init;
34722 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34724 vec_safe_push (for_block, this_pre_body);
34725 init = NULL_TREE;
34727 else
34729 init = pop_stmt_list (this_pre_body);
34730 if (init && TREE_CODE (init) == STATEMENT_LIST)
34732 tree_stmt_iterator i = tsi_start (init);
34733 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34734 while (!tsi_end_p (i))
34736 tree t = tsi_stmt (i);
34737 if (TREE_CODE (t) == DECL_EXPR
34738 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34740 tsi_delink (&i);
34741 vec_safe_push (for_block, t);
34742 continue;
34744 break;
34746 if (tsi_one_before_end_p (i))
34748 tree t = tsi_stmt (i);
34749 tsi_delink (&i);
34750 free_stmt_list (init);
34751 init = t;
34755 this_pre_body = NULL_TREE;
34757 else
34759 /* Consume '='. */
34760 cp_lexer_consume_token (parser->lexer);
34761 init = cp_parser_assignment_expression (parser);
34763 non_class:
34764 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34765 init = error_mark_node;
34766 else
34767 cp_finish_decl (decl, NULL_TREE,
34768 /*init_const_expr_p=*/false,
34769 asm_specification,
34770 LOOKUP_ONLYCONVERTING);
34773 if (pushed_scope)
34774 pop_scope (pushed_scope);
34777 else
34779 cp_id_kind idk;
34780 /* If parsing a type specifier sequence failed, then
34781 this MUST be a simple expression. */
34782 cp_parser_parse_tentatively (parser);
34783 decl = cp_parser_primary_expression (parser, false, false,
34784 false, &idk);
34785 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34786 if (!cp_parser_error_occurred (parser)
34787 && decl
34788 && (TREE_CODE (decl) == COMPONENT_REF
34789 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34791 cp_parser_abort_tentative_parse (parser);
34792 cp_parser_parse_tentatively (parser);
34793 cp_token *token = cp_lexer_peek_token (parser->lexer);
34794 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34795 /*check_dependency_p=*/true,
34796 /*template_p=*/NULL,
34797 /*declarator_p=*/false,
34798 /*optional_p=*/false);
34799 if (name != error_mark_node
34800 && last_tok == cp_lexer_peek_token (parser->lexer))
34802 decl = cp_parser_lookup_name_simple (parser, name,
34803 token->location);
34804 if (TREE_CODE (decl) == FIELD_DECL)
34805 add_private_clause = omp_privatize_field (decl, false);
34807 cp_parser_abort_tentative_parse (parser);
34808 cp_parser_parse_tentatively (parser);
34809 decl = cp_parser_primary_expression (parser, false, false,
34810 false, &idk);
34812 if (!cp_parser_error_occurred (parser)
34813 && decl
34814 && DECL_P (decl)
34815 && CLASS_TYPE_P (TREE_TYPE (decl)))
34817 tree rhs;
34819 cp_parser_parse_definitely (parser);
34820 cp_parser_require (parser, CPP_EQ, RT_EQ);
34821 rhs = cp_parser_assignment_expression (parser);
34822 orig_init = rhs;
34823 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34824 decl, NOP_EXPR,
34825 rhs,
34826 tf_warning_or_error));
34827 if (!add_private_clause)
34828 add_private_clause = decl;
34830 else
34832 decl = NULL;
34833 cp_parser_abort_tentative_parse (parser);
34834 init = cp_parser_expression (parser);
34835 if (init)
34837 if (TREE_CODE (init) == MODIFY_EXPR
34838 || TREE_CODE (init) == MODOP_EXPR)
34839 real_decl = TREE_OPERAND (init, 0);
34843 return add_private_clause;
34846 /* Parse the restricted form of the for statement allowed by OpenMP. */
34848 static tree
34849 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34850 tree *cclauses, bool *if_p)
34852 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34853 tree real_decl, initv, condv, incrv, declv;
34854 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34855 location_t loc_first;
34856 bool collapse_err = false;
34857 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34858 vec<tree, va_gc> *for_block = make_tree_vector ();
34859 auto_vec<tree, 4> orig_inits;
34860 bool tiling = false;
34862 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34863 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34864 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34865 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34867 tiling = true;
34868 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34870 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34871 && OMP_CLAUSE_ORDERED_EXPR (cl))
34873 ordered_cl = cl;
34874 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34877 if (ordered && ordered < collapse)
34879 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34880 "%<ordered%> clause parameter is less than %<collapse%>");
34881 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34882 = build_int_cst (NULL_TREE, collapse);
34883 ordered = collapse;
34885 if (ordered)
34887 for (tree *pc = &clauses; *pc; )
34888 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34890 error_at (OMP_CLAUSE_LOCATION (*pc),
34891 "%<linear%> clause may not be specified together "
34892 "with %<ordered%> clause with a parameter");
34893 *pc = OMP_CLAUSE_CHAIN (*pc);
34895 else
34896 pc = &OMP_CLAUSE_CHAIN (*pc);
34899 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34900 count = ordered ? ordered : collapse;
34902 declv = make_tree_vec (count);
34903 initv = make_tree_vec (count);
34904 condv = make_tree_vec (count);
34905 incrv = make_tree_vec (count);
34907 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34909 for (i = 0; i < count; i++)
34911 int bracecount = 0;
34912 tree add_private_clause = NULL_TREE;
34913 location_t loc;
34915 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34917 if (!collapse_err)
34918 cp_parser_error (parser, "for statement expected");
34919 return NULL;
34921 loc = cp_lexer_consume_token (parser->lexer)->location;
34923 matching_parens parens;
34924 if (!parens.require_open (parser))
34925 return NULL;
34927 init = orig_init = decl = real_decl = NULL;
34928 this_pre_body = push_stmt_list ();
34930 add_private_clause
34931 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
34932 init, orig_init, decl, real_decl);
34934 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34935 if (this_pre_body)
34937 this_pre_body = pop_stmt_list (this_pre_body);
34938 if (pre_body)
34940 tree t = pre_body;
34941 pre_body = push_stmt_list ();
34942 add_stmt (t);
34943 add_stmt (this_pre_body);
34944 pre_body = pop_stmt_list (pre_body);
34946 else
34947 pre_body = this_pre_body;
34950 if (decl)
34951 real_decl = decl;
34952 if (cclauses != NULL
34953 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34954 && real_decl != NULL_TREE)
34956 tree *c;
34957 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34958 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34959 && OMP_CLAUSE_DECL (*c) == real_decl)
34961 error_at (loc, "iteration variable %qD"
34962 " should not be firstprivate", real_decl);
34963 *c = OMP_CLAUSE_CHAIN (*c);
34965 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34966 && OMP_CLAUSE_DECL (*c) == real_decl)
34968 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34969 tree l = *c;
34970 *c = OMP_CLAUSE_CHAIN (*c);
34971 if (code == OMP_SIMD)
34973 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34974 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34976 else
34978 OMP_CLAUSE_CHAIN (l) = clauses;
34979 clauses = l;
34981 add_private_clause = NULL_TREE;
34983 else
34985 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34986 && OMP_CLAUSE_DECL (*c) == real_decl)
34987 add_private_clause = NULL_TREE;
34988 c = &OMP_CLAUSE_CHAIN (*c);
34992 if (add_private_clause)
34994 tree c;
34995 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34997 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34998 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34999 && OMP_CLAUSE_DECL (c) == decl)
35000 break;
35001 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35002 && OMP_CLAUSE_DECL (c) == decl)
35003 error_at (loc, "iteration variable %qD "
35004 "should not be firstprivate",
35005 decl);
35006 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35007 && OMP_CLAUSE_DECL (c) == decl)
35008 error_at (loc, "iteration variable %qD should not be reduction",
35009 decl);
35011 if (c == NULL)
35013 if (code != OMP_SIMD)
35014 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35015 else if (collapse == 1)
35016 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35017 else
35018 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35019 OMP_CLAUSE_DECL (c) = add_private_clause;
35020 c = finish_omp_clauses (c, C_ORT_OMP);
35021 if (c)
35023 OMP_CLAUSE_CHAIN (c) = clauses;
35024 clauses = c;
35025 /* For linear, signal that we need to fill up
35026 the so far unknown linear step. */
35027 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35028 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35033 cond = NULL;
35034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35035 cond = cp_parser_omp_for_cond (parser, decl);
35036 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35038 incr = NULL;
35039 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35041 /* If decl is an iterator, preserve the operator on decl
35042 until finish_omp_for. */
35043 if (real_decl
35044 && ((processing_template_decl
35045 && (TREE_TYPE (real_decl) == NULL_TREE
35046 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35047 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35048 incr = cp_parser_omp_for_incr (parser, real_decl);
35049 else
35050 incr = cp_parser_expression (parser);
35051 if (!EXPR_HAS_LOCATION (incr))
35052 protected_set_expr_location (incr, input_location);
35055 if (!parens.require_close (parser))
35056 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35057 /*or_comma=*/false,
35058 /*consume_paren=*/true);
35060 TREE_VEC_ELT (declv, i) = decl;
35061 TREE_VEC_ELT (initv, i) = init;
35062 TREE_VEC_ELT (condv, i) = cond;
35063 TREE_VEC_ELT (incrv, i) = incr;
35064 if (orig_init)
35066 orig_inits.safe_grow_cleared (i + 1);
35067 orig_inits[i] = orig_init;
35070 if (i == count - 1)
35071 break;
35073 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35074 in between the collapsed for loops to be still considered perfectly
35075 nested. Hopefully the final version clarifies this.
35076 For now handle (multiple) {'s and empty statements. */
35077 cp_parser_parse_tentatively (parser);
35078 for (;;)
35080 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35081 break;
35082 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35084 cp_lexer_consume_token (parser->lexer);
35085 bracecount++;
35087 else if (bracecount
35088 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35089 cp_lexer_consume_token (parser->lexer);
35090 else
35092 loc = cp_lexer_peek_token (parser->lexer)->location;
35093 error_at (loc, "not enough for loops to collapse");
35094 collapse_err = true;
35095 cp_parser_abort_tentative_parse (parser);
35096 declv = NULL_TREE;
35097 break;
35101 if (declv)
35103 cp_parser_parse_definitely (parser);
35104 nbraces += bracecount;
35108 if (nbraces)
35109 if_p = NULL;
35111 /* Note that we saved the original contents of this flag when we entered
35112 the structured block, and so we don't need to re-save it here. */
35113 parser->in_statement = IN_OMP_FOR;
35115 /* Note that the grammar doesn't call for a structured block here,
35116 though the loop as a whole is a structured block. */
35117 body = push_stmt_list ();
35118 cp_parser_statement (parser, NULL_TREE, false, if_p);
35119 body = pop_stmt_list (body);
35121 if (declv == NULL_TREE)
35122 ret = NULL_TREE;
35123 else
35124 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35125 body, pre_body, &orig_inits, clauses);
35127 while (nbraces)
35129 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35131 cp_lexer_consume_token (parser->lexer);
35132 nbraces--;
35134 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35135 cp_lexer_consume_token (parser->lexer);
35136 else
35138 if (!collapse_err)
35140 error_at (cp_lexer_peek_token (parser->lexer)->location,
35141 "collapsed loops not perfectly nested");
35143 collapse_err = true;
35144 cp_parser_statement_seq_opt (parser, NULL);
35145 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35146 break;
35150 while (!for_block->is_empty ())
35152 tree t = for_block->pop ();
35153 if (TREE_CODE (t) == STATEMENT_LIST)
35154 add_stmt (pop_stmt_list (t));
35155 else
35156 add_stmt (t);
35158 release_tree_vector (for_block);
35160 return ret;
35163 /* Helper function for OpenMP parsing, split clauses and call
35164 finish_omp_clauses on each of the set of clauses afterwards. */
35166 static void
35167 cp_omp_split_clauses (location_t loc, enum tree_code code,
35168 omp_clause_mask mask, tree clauses, tree *cclauses)
35170 int i;
35171 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35172 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35173 if (cclauses[i])
35174 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35177 /* OpenMP 4.0:
35178 #pragma omp simd simd-clause[optseq] new-line
35179 for-loop */
35181 #define OMP_SIMD_CLAUSE_MASK \
35182 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35191 static tree
35192 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35193 char *p_name, omp_clause_mask mask, tree *cclauses,
35194 bool *if_p)
35196 tree clauses, sb, ret;
35197 unsigned int save;
35198 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35200 strcat (p_name, " simd");
35201 mask |= OMP_SIMD_CLAUSE_MASK;
35203 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35204 cclauses == NULL);
35205 if (cclauses)
35207 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35208 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35209 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35210 OMP_CLAUSE_ORDERED);
35211 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35213 error_at (OMP_CLAUSE_LOCATION (c),
35214 "%<ordered%> clause with parameter may not be specified "
35215 "on %qs construct", p_name);
35216 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35220 sb = begin_omp_structured_block ();
35221 save = cp_parser_begin_omp_structured_block (parser);
35223 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35225 cp_parser_end_omp_structured_block (parser, save);
35226 add_stmt (finish_omp_structured_block (sb));
35228 return ret;
35231 /* OpenMP 2.5:
35232 #pragma omp for for-clause[optseq] new-line
35233 for-loop
35235 OpenMP 4.0:
35236 #pragma omp for simd for-simd-clause[optseq] new-line
35237 for-loop */
35239 #define OMP_FOR_CLAUSE_MASK \
35240 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35250 static tree
35251 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35252 char *p_name, omp_clause_mask mask, tree *cclauses,
35253 bool *if_p)
35255 tree clauses, sb, ret;
35256 unsigned int save;
35257 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35259 strcat (p_name, " for");
35260 mask |= OMP_FOR_CLAUSE_MASK;
35261 /* parallel for{, simd} disallows nowait clause, but for
35262 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35263 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35264 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35265 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35266 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35267 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35269 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35271 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35272 const char *p = IDENTIFIER_POINTER (id);
35274 if (strcmp (p, "simd") == 0)
35276 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35277 if (cclauses == NULL)
35278 cclauses = cclauses_buf;
35280 cp_lexer_consume_token (parser->lexer);
35281 if (!flag_openmp) /* flag_openmp_simd */
35282 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35283 cclauses, if_p);
35284 sb = begin_omp_structured_block ();
35285 save = cp_parser_begin_omp_structured_block (parser);
35286 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35287 cclauses, if_p);
35288 cp_parser_end_omp_structured_block (parser, save);
35289 tree body = finish_omp_structured_block (sb);
35290 if (ret == NULL)
35291 return ret;
35292 ret = make_node (OMP_FOR);
35293 TREE_TYPE (ret) = void_type_node;
35294 OMP_FOR_BODY (ret) = body;
35295 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35296 SET_EXPR_LOCATION (ret, loc);
35297 add_stmt (ret);
35298 return ret;
35301 if (!flag_openmp) /* flag_openmp_simd */
35303 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35304 return NULL_TREE;
35307 /* Composite distribute parallel for disallows linear clause. */
35308 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35309 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35311 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35312 cclauses == NULL);
35313 if (cclauses)
35315 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35316 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35319 sb = begin_omp_structured_block ();
35320 save = cp_parser_begin_omp_structured_block (parser);
35322 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35324 cp_parser_end_omp_structured_block (parser, save);
35325 add_stmt (finish_omp_structured_block (sb));
35327 return ret;
35330 /* OpenMP 2.5:
35331 # pragma omp master new-line
35332 structured-block */
35334 static tree
35335 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35337 cp_parser_require_pragma_eol (parser, pragma_tok);
35338 return c_finish_omp_master (input_location,
35339 cp_parser_omp_structured_block (parser, if_p));
35342 /* OpenMP 2.5:
35343 # pragma omp ordered new-line
35344 structured-block
35346 OpenMP 4.5:
35347 # pragma omp ordered ordered-clauses new-line
35348 structured-block */
35350 #define OMP_ORDERED_CLAUSE_MASK \
35351 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35354 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35355 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35357 static bool
35358 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35359 enum pragma_context context, bool *if_p)
35361 location_t loc = pragma_tok->location;
35363 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35365 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35366 const char *p = IDENTIFIER_POINTER (id);
35368 if (strcmp (p, "depend") == 0)
35370 if (!flag_openmp) /* flag_openmp_simd */
35372 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35373 return false;
35375 if (context == pragma_stmt)
35377 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35378 "%<depend%> clause may only be used in compound "
35379 "statements");
35380 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35381 return false;
35383 tree clauses
35384 = cp_parser_omp_all_clauses (parser,
35385 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35386 "#pragma omp ordered", pragma_tok);
35387 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35388 return false;
35392 tree clauses
35393 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35394 "#pragma omp ordered", pragma_tok);
35396 if (!flag_openmp /* flag_openmp_simd */
35397 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35398 return false;
35400 c_finish_omp_ordered (loc, clauses,
35401 cp_parser_omp_structured_block (parser, if_p));
35402 return true;
35405 /* OpenMP 2.5:
35407 section-scope:
35408 { section-sequence }
35410 section-sequence:
35411 section-directive[opt] structured-block
35412 section-sequence section-directive structured-block */
35414 static tree
35415 cp_parser_omp_sections_scope (cp_parser *parser)
35417 tree stmt, substmt;
35418 bool error_suppress = false;
35419 cp_token *tok;
35421 matching_braces braces;
35422 if (!braces.require_open (parser))
35423 return NULL_TREE;
35425 stmt = push_stmt_list ();
35427 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35428 != PRAGMA_OMP_SECTION)
35430 substmt = cp_parser_omp_structured_block (parser, NULL);
35431 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35432 add_stmt (substmt);
35435 while (1)
35437 tok = cp_lexer_peek_token (parser->lexer);
35438 if (tok->type == CPP_CLOSE_BRACE)
35439 break;
35440 if (tok->type == CPP_EOF)
35441 break;
35443 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35445 cp_lexer_consume_token (parser->lexer);
35446 cp_parser_require_pragma_eol (parser, tok);
35447 error_suppress = false;
35449 else if (!error_suppress)
35451 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35452 error_suppress = true;
35455 substmt = cp_parser_omp_structured_block (parser, NULL);
35456 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35457 add_stmt (substmt);
35459 braces.require_close (parser);
35461 substmt = pop_stmt_list (stmt);
35463 stmt = make_node (OMP_SECTIONS);
35464 TREE_TYPE (stmt) = void_type_node;
35465 OMP_SECTIONS_BODY (stmt) = substmt;
35467 add_stmt (stmt);
35468 return stmt;
35471 /* OpenMP 2.5:
35472 # pragma omp sections sections-clause[optseq] newline
35473 sections-scope */
35475 #define OMP_SECTIONS_CLAUSE_MASK \
35476 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35482 static tree
35483 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35484 char *p_name, omp_clause_mask mask, tree *cclauses)
35486 tree clauses, ret;
35487 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35489 strcat (p_name, " sections");
35490 mask |= OMP_SECTIONS_CLAUSE_MASK;
35491 if (cclauses)
35492 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35494 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35495 cclauses == NULL);
35496 if (cclauses)
35498 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35499 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35502 ret = cp_parser_omp_sections_scope (parser);
35503 if (ret)
35504 OMP_SECTIONS_CLAUSES (ret) = clauses;
35506 return ret;
35509 /* OpenMP 2.5:
35510 # pragma omp parallel parallel-clause[optseq] new-line
35511 structured-block
35512 # pragma omp parallel for parallel-for-clause[optseq] new-line
35513 structured-block
35514 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35515 structured-block
35517 OpenMP 4.0:
35518 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35519 structured-block */
35521 #define OMP_PARALLEL_CLAUSE_MASK \
35522 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35532 static tree
35533 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35534 char *p_name, omp_clause_mask mask, tree *cclauses,
35535 bool *if_p)
35537 tree stmt, clauses, block;
35538 unsigned int save;
35539 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35541 strcat (p_name, " parallel");
35542 mask |= OMP_PARALLEL_CLAUSE_MASK;
35543 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35544 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35545 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35546 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35548 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35550 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35551 if (cclauses == NULL)
35552 cclauses = cclauses_buf;
35554 cp_lexer_consume_token (parser->lexer);
35555 if (!flag_openmp) /* flag_openmp_simd */
35556 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35557 if_p);
35558 block = begin_omp_parallel ();
35559 save = cp_parser_begin_omp_structured_block (parser);
35560 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35561 if_p);
35562 cp_parser_end_omp_structured_block (parser, save);
35563 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35564 block);
35565 if (ret == NULL_TREE)
35566 return ret;
35567 OMP_PARALLEL_COMBINED (stmt) = 1;
35568 return stmt;
35570 /* When combined with distribute, parallel has to be followed by for.
35571 #pragma omp target parallel is allowed though. */
35572 else if (cclauses
35573 && (mask & (OMP_CLAUSE_MASK_1
35574 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35576 error_at (loc, "expected %<for%> after %qs", p_name);
35577 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35578 return NULL_TREE;
35580 else if (!flag_openmp) /* flag_openmp_simd */
35582 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35583 return NULL_TREE;
35585 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35587 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35588 const char *p = IDENTIFIER_POINTER (id);
35589 if (strcmp (p, "sections") == 0)
35591 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35592 cclauses = cclauses_buf;
35594 cp_lexer_consume_token (parser->lexer);
35595 block = begin_omp_parallel ();
35596 save = cp_parser_begin_omp_structured_block (parser);
35597 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35598 cp_parser_end_omp_structured_block (parser, save);
35599 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35600 block);
35601 OMP_PARALLEL_COMBINED (stmt) = 1;
35602 return stmt;
35606 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35607 cclauses == NULL);
35608 if (cclauses)
35610 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35611 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35614 block = begin_omp_parallel ();
35615 save = cp_parser_begin_omp_structured_block (parser);
35616 cp_parser_statement (parser, NULL_TREE, false, if_p);
35617 cp_parser_end_omp_structured_block (parser, save);
35618 stmt = finish_omp_parallel (clauses, block);
35619 return stmt;
35622 /* OpenMP 2.5:
35623 # pragma omp single single-clause[optseq] new-line
35624 structured-block */
35626 #define OMP_SINGLE_CLAUSE_MASK \
35627 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35632 static tree
35633 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35635 tree stmt = make_node (OMP_SINGLE);
35636 TREE_TYPE (stmt) = void_type_node;
35638 OMP_SINGLE_CLAUSES (stmt)
35639 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35640 "#pragma omp single", pragma_tok);
35641 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35643 return add_stmt (stmt);
35646 /* OpenMP 3.0:
35647 # pragma omp task task-clause[optseq] new-line
35648 structured-block */
35650 #define OMP_TASK_CLAUSE_MASK \
35651 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35662 static tree
35663 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35665 tree clauses, block;
35666 unsigned int save;
35668 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35669 "#pragma omp task", pragma_tok);
35670 block = begin_omp_task ();
35671 save = cp_parser_begin_omp_structured_block (parser);
35672 cp_parser_statement (parser, NULL_TREE, false, if_p);
35673 cp_parser_end_omp_structured_block (parser, save);
35674 return finish_omp_task (clauses, block);
35677 /* OpenMP 3.0:
35678 # pragma omp taskwait new-line */
35680 static void
35681 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35683 cp_parser_require_pragma_eol (parser, pragma_tok);
35684 finish_omp_taskwait ();
35687 /* OpenMP 3.1:
35688 # pragma omp taskyield new-line */
35690 static void
35691 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35693 cp_parser_require_pragma_eol (parser, pragma_tok);
35694 finish_omp_taskyield ();
35697 /* OpenMP 4.0:
35698 # pragma omp taskgroup new-line
35699 structured-block */
35701 static tree
35702 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35704 cp_parser_require_pragma_eol (parser, pragma_tok);
35705 return c_finish_omp_taskgroup (input_location,
35706 cp_parser_omp_structured_block (parser,
35707 if_p));
35711 /* OpenMP 2.5:
35712 # pragma omp threadprivate (variable-list) */
35714 static void
35715 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35717 tree vars;
35719 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35720 cp_parser_require_pragma_eol (parser, pragma_tok);
35722 finish_omp_threadprivate (vars);
35725 /* OpenMP 4.0:
35726 # pragma omp cancel cancel-clause[optseq] new-line */
35728 #define OMP_CANCEL_CLAUSE_MASK \
35729 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35735 static void
35736 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35738 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35739 "#pragma omp cancel", pragma_tok);
35740 finish_omp_cancel (clauses);
35743 /* OpenMP 4.0:
35744 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35746 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35747 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35752 static void
35753 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35754 enum pragma_context context)
35756 tree clauses;
35757 bool point_seen = false;
35759 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35761 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35762 const char *p = IDENTIFIER_POINTER (id);
35764 if (strcmp (p, "point") == 0)
35766 cp_lexer_consume_token (parser->lexer);
35767 point_seen = true;
35770 if (!point_seen)
35772 cp_parser_error (parser, "expected %<point%>");
35773 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35774 return;
35777 if (context != pragma_compound)
35779 if (context == pragma_stmt)
35780 error_at (pragma_tok->location,
35781 "%<#pragma %s%> may only be used in compound statements",
35782 "omp cancellation point");
35783 else
35784 cp_parser_error (parser, "expected declaration specifiers");
35785 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35786 return;
35789 clauses = cp_parser_omp_all_clauses (parser,
35790 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35791 "#pragma omp cancellation point",
35792 pragma_tok);
35793 finish_omp_cancellation_point (clauses);
35796 /* OpenMP 4.0:
35797 #pragma omp distribute distribute-clause[optseq] new-line
35798 for-loop */
35800 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35801 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35807 static tree
35808 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35809 char *p_name, omp_clause_mask mask, tree *cclauses,
35810 bool *if_p)
35812 tree clauses, sb, ret;
35813 unsigned int save;
35814 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35816 strcat (p_name, " distribute");
35817 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35819 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35821 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35822 const char *p = IDENTIFIER_POINTER (id);
35823 bool simd = false;
35824 bool parallel = false;
35826 if (strcmp (p, "simd") == 0)
35827 simd = true;
35828 else
35829 parallel = strcmp (p, "parallel") == 0;
35830 if (parallel || simd)
35832 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35833 if (cclauses == NULL)
35834 cclauses = cclauses_buf;
35835 cp_lexer_consume_token (parser->lexer);
35836 if (!flag_openmp) /* flag_openmp_simd */
35838 if (simd)
35839 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35840 cclauses, if_p);
35841 else
35842 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35843 cclauses, if_p);
35845 sb = begin_omp_structured_block ();
35846 save = cp_parser_begin_omp_structured_block (parser);
35847 if (simd)
35848 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35849 cclauses, if_p);
35850 else
35851 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35852 cclauses, if_p);
35853 cp_parser_end_omp_structured_block (parser, save);
35854 tree body = finish_omp_structured_block (sb);
35855 if (ret == NULL)
35856 return ret;
35857 ret = make_node (OMP_DISTRIBUTE);
35858 TREE_TYPE (ret) = void_type_node;
35859 OMP_FOR_BODY (ret) = body;
35860 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35861 SET_EXPR_LOCATION (ret, loc);
35862 add_stmt (ret);
35863 return ret;
35866 if (!flag_openmp) /* flag_openmp_simd */
35868 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35869 return NULL_TREE;
35872 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35873 cclauses == NULL);
35874 if (cclauses)
35876 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35877 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35880 sb = begin_omp_structured_block ();
35881 save = cp_parser_begin_omp_structured_block (parser);
35883 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35885 cp_parser_end_omp_structured_block (parser, save);
35886 add_stmt (finish_omp_structured_block (sb));
35888 return ret;
35891 /* OpenMP 4.0:
35892 # pragma omp teams teams-clause[optseq] new-line
35893 structured-block */
35895 #define OMP_TEAMS_CLAUSE_MASK \
35896 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35904 static tree
35905 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35906 char *p_name, omp_clause_mask mask, tree *cclauses,
35907 bool *if_p)
35909 tree clauses, sb, ret;
35910 unsigned int save;
35911 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35913 strcat (p_name, " teams");
35914 mask |= OMP_TEAMS_CLAUSE_MASK;
35916 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35918 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35919 const char *p = IDENTIFIER_POINTER (id);
35920 if (strcmp (p, "distribute") == 0)
35922 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35923 if (cclauses == NULL)
35924 cclauses = cclauses_buf;
35926 cp_lexer_consume_token (parser->lexer);
35927 if (!flag_openmp) /* flag_openmp_simd */
35928 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35929 cclauses, if_p);
35930 sb = begin_omp_structured_block ();
35931 save = cp_parser_begin_omp_structured_block (parser);
35932 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35933 cclauses, if_p);
35934 cp_parser_end_omp_structured_block (parser, save);
35935 tree body = finish_omp_structured_block (sb);
35936 if (ret == NULL)
35937 return ret;
35938 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35939 ret = make_node (OMP_TEAMS);
35940 TREE_TYPE (ret) = void_type_node;
35941 OMP_TEAMS_CLAUSES (ret) = clauses;
35942 OMP_TEAMS_BODY (ret) = body;
35943 OMP_TEAMS_COMBINED (ret) = 1;
35944 SET_EXPR_LOCATION (ret, loc);
35945 return add_stmt (ret);
35948 if (!flag_openmp) /* flag_openmp_simd */
35950 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35951 return NULL_TREE;
35954 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35955 cclauses == NULL);
35956 if (cclauses)
35958 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35959 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35962 tree stmt = make_node (OMP_TEAMS);
35963 TREE_TYPE (stmt) = void_type_node;
35964 OMP_TEAMS_CLAUSES (stmt) = clauses;
35965 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35966 SET_EXPR_LOCATION (stmt, loc);
35968 return add_stmt (stmt);
35971 /* OpenMP 4.0:
35972 # pragma omp target data target-data-clause[optseq] new-line
35973 structured-block */
35975 #define OMP_TARGET_DATA_CLAUSE_MASK \
35976 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35981 static tree
35982 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35984 tree clauses
35985 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35986 "#pragma omp target data", pragma_tok);
35987 int map_seen = 0;
35988 for (tree *pc = &clauses; *pc;)
35990 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35991 switch (OMP_CLAUSE_MAP_KIND (*pc))
35993 case GOMP_MAP_TO:
35994 case GOMP_MAP_ALWAYS_TO:
35995 case GOMP_MAP_FROM:
35996 case GOMP_MAP_ALWAYS_FROM:
35997 case GOMP_MAP_TOFROM:
35998 case GOMP_MAP_ALWAYS_TOFROM:
35999 case GOMP_MAP_ALLOC:
36000 map_seen = 3;
36001 break;
36002 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36003 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36004 case GOMP_MAP_ALWAYS_POINTER:
36005 break;
36006 default:
36007 map_seen |= 1;
36008 error_at (OMP_CLAUSE_LOCATION (*pc),
36009 "%<#pragma omp target data%> with map-type other "
36010 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36011 "on %<map%> clause");
36012 *pc = OMP_CLAUSE_CHAIN (*pc);
36013 continue;
36015 pc = &OMP_CLAUSE_CHAIN (*pc);
36018 if (map_seen != 3)
36020 if (map_seen == 0)
36021 error_at (pragma_tok->location,
36022 "%<#pragma omp target data%> must contain at least "
36023 "one %<map%> clause");
36024 return NULL_TREE;
36027 tree stmt = make_node (OMP_TARGET_DATA);
36028 TREE_TYPE (stmt) = void_type_node;
36029 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36031 keep_next_level (true);
36032 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36034 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36035 return add_stmt (stmt);
36038 /* OpenMP 4.5:
36039 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36040 structured-block */
36042 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36043 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36049 static tree
36050 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36051 enum pragma_context context)
36053 bool data_seen = false;
36054 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36056 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36057 const char *p = IDENTIFIER_POINTER (id);
36059 if (strcmp (p, "data") == 0)
36061 cp_lexer_consume_token (parser->lexer);
36062 data_seen = true;
36065 if (!data_seen)
36067 cp_parser_error (parser, "expected %<data%>");
36068 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36069 return NULL_TREE;
36072 if (context == pragma_stmt)
36074 error_at (pragma_tok->location,
36075 "%<#pragma %s%> may only be used in compound statements",
36076 "omp target enter data");
36077 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36078 return NULL_TREE;
36081 tree clauses
36082 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36083 "#pragma omp target enter data", pragma_tok);
36084 int map_seen = 0;
36085 for (tree *pc = &clauses; *pc;)
36087 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36088 switch (OMP_CLAUSE_MAP_KIND (*pc))
36090 case GOMP_MAP_TO:
36091 case GOMP_MAP_ALWAYS_TO:
36092 case GOMP_MAP_ALLOC:
36093 map_seen = 3;
36094 break;
36095 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36096 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36097 case GOMP_MAP_ALWAYS_POINTER:
36098 break;
36099 default:
36100 map_seen |= 1;
36101 error_at (OMP_CLAUSE_LOCATION (*pc),
36102 "%<#pragma omp target enter data%> with map-type other "
36103 "than %<to%> or %<alloc%> on %<map%> clause");
36104 *pc = OMP_CLAUSE_CHAIN (*pc);
36105 continue;
36107 pc = &OMP_CLAUSE_CHAIN (*pc);
36110 if (map_seen != 3)
36112 if (map_seen == 0)
36113 error_at (pragma_tok->location,
36114 "%<#pragma omp target enter data%> must contain at least "
36115 "one %<map%> clause");
36116 return NULL_TREE;
36119 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36120 TREE_TYPE (stmt) = void_type_node;
36121 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36122 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36123 return add_stmt (stmt);
36126 /* OpenMP 4.5:
36127 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36128 structured-block */
36130 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36131 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36137 static tree
36138 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36139 enum pragma_context context)
36141 bool data_seen = false;
36142 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36144 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36145 const char *p = IDENTIFIER_POINTER (id);
36147 if (strcmp (p, "data") == 0)
36149 cp_lexer_consume_token (parser->lexer);
36150 data_seen = true;
36153 if (!data_seen)
36155 cp_parser_error (parser, "expected %<data%>");
36156 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36157 return NULL_TREE;
36160 if (context == pragma_stmt)
36162 error_at (pragma_tok->location,
36163 "%<#pragma %s%> may only be used in compound statements",
36164 "omp target exit data");
36165 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36166 return NULL_TREE;
36169 tree clauses
36170 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36171 "#pragma omp target exit data", pragma_tok);
36172 int map_seen = 0;
36173 for (tree *pc = &clauses; *pc;)
36175 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36176 switch (OMP_CLAUSE_MAP_KIND (*pc))
36178 case GOMP_MAP_FROM:
36179 case GOMP_MAP_ALWAYS_FROM:
36180 case GOMP_MAP_RELEASE:
36181 case GOMP_MAP_DELETE:
36182 map_seen = 3;
36183 break;
36184 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36185 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36186 case GOMP_MAP_ALWAYS_POINTER:
36187 break;
36188 default:
36189 map_seen |= 1;
36190 error_at (OMP_CLAUSE_LOCATION (*pc),
36191 "%<#pragma omp target exit data%> with map-type other "
36192 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36193 " clause");
36194 *pc = OMP_CLAUSE_CHAIN (*pc);
36195 continue;
36197 pc = &OMP_CLAUSE_CHAIN (*pc);
36200 if (map_seen != 3)
36202 if (map_seen == 0)
36203 error_at (pragma_tok->location,
36204 "%<#pragma omp target exit data%> must contain at least "
36205 "one %<map%> clause");
36206 return NULL_TREE;
36209 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36210 TREE_TYPE (stmt) = void_type_node;
36211 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36212 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36213 return add_stmt (stmt);
36216 /* OpenMP 4.0:
36217 # pragma omp target update target-update-clause[optseq] new-line */
36219 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36220 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36227 static bool
36228 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36229 enum pragma_context context)
36231 if (context == pragma_stmt)
36233 error_at (pragma_tok->location,
36234 "%<#pragma %s%> may only be used in compound statements",
36235 "omp target update");
36236 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36237 return false;
36240 tree clauses
36241 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36242 "#pragma omp target update", pragma_tok);
36243 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36244 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36246 error_at (pragma_tok->location,
36247 "%<#pragma omp target update%> must contain at least one "
36248 "%<from%> or %<to%> clauses");
36249 return false;
36252 tree stmt = make_node (OMP_TARGET_UPDATE);
36253 TREE_TYPE (stmt) = void_type_node;
36254 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36255 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36256 add_stmt (stmt);
36257 return false;
36260 /* OpenMP 4.0:
36261 # pragma omp target target-clause[optseq] new-line
36262 structured-block */
36264 #define OMP_TARGET_CLAUSE_MASK \
36265 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36275 static bool
36276 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36277 enum pragma_context context, bool *if_p)
36279 tree *pc = NULL, stmt;
36281 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36283 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36284 const char *p = IDENTIFIER_POINTER (id);
36285 enum tree_code ccode = ERROR_MARK;
36287 if (strcmp (p, "teams") == 0)
36288 ccode = OMP_TEAMS;
36289 else if (strcmp (p, "parallel") == 0)
36290 ccode = OMP_PARALLEL;
36291 else if (strcmp (p, "simd") == 0)
36292 ccode = OMP_SIMD;
36293 if (ccode != ERROR_MARK)
36295 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36296 char p_name[sizeof ("#pragma omp target teams distribute "
36297 "parallel for simd")];
36299 cp_lexer_consume_token (parser->lexer);
36300 strcpy (p_name, "#pragma omp target");
36301 if (!flag_openmp) /* flag_openmp_simd */
36303 tree stmt;
36304 switch (ccode)
36306 case OMP_TEAMS:
36307 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36308 OMP_TARGET_CLAUSE_MASK,
36309 cclauses, if_p);
36310 break;
36311 case OMP_PARALLEL:
36312 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36313 OMP_TARGET_CLAUSE_MASK,
36314 cclauses, if_p);
36315 break;
36316 case OMP_SIMD:
36317 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36318 OMP_TARGET_CLAUSE_MASK,
36319 cclauses, if_p);
36320 break;
36321 default:
36322 gcc_unreachable ();
36324 return stmt != NULL_TREE;
36326 keep_next_level (true);
36327 tree sb = begin_omp_structured_block (), ret;
36328 unsigned save = cp_parser_begin_omp_structured_block (parser);
36329 switch (ccode)
36331 case OMP_TEAMS:
36332 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36333 OMP_TARGET_CLAUSE_MASK, cclauses,
36334 if_p);
36335 break;
36336 case OMP_PARALLEL:
36337 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36338 OMP_TARGET_CLAUSE_MASK, cclauses,
36339 if_p);
36340 break;
36341 case OMP_SIMD:
36342 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36343 OMP_TARGET_CLAUSE_MASK, cclauses,
36344 if_p);
36345 break;
36346 default:
36347 gcc_unreachable ();
36349 cp_parser_end_omp_structured_block (parser, save);
36350 tree body = finish_omp_structured_block (sb);
36351 if (ret == NULL_TREE)
36352 return false;
36353 if (ccode == OMP_TEAMS && !processing_template_decl)
36355 /* For combined target teams, ensure the num_teams and
36356 thread_limit clause expressions are evaluated on the host,
36357 before entering the target construct. */
36358 tree c;
36359 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36360 c; c = OMP_CLAUSE_CHAIN (c))
36361 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36362 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36363 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36365 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36366 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36367 if (expr == error_mark_node)
36368 continue;
36369 tree tmp = TARGET_EXPR_SLOT (expr);
36370 add_stmt (expr);
36371 OMP_CLAUSE_OPERAND (c, 0) = expr;
36372 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36373 OMP_CLAUSE_FIRSTPRIVATE);
36374 OMP_CLAUSE_DECL (tc) = tmp;
36375 OMP_CLAUSE_CHAIN (tc)
36376 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36377 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36380 tree stmt = make_node (OMP_TARGET);
36381 TREE_TYPE (stmt) = void_type_node;
36382 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36383 OMP_TARGET_BODY (stmt) = body;
36384 OMP_TARGET_COMBINED (stmt) = 1;
36385 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36386 add_stmt (stmt);
36387 pc = &OMP_TARGET_CLAUSES (stmt);
36388 goto check_clauses;
36390 else if (!flag_openmp) /* flag_openmp_simd */
36392 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36393 return false;
36395 else if (strcmp (p, "data") == 0)
36397 cp_lexer_consume_token (parser->lexer);
36398 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36399 return true;
36401 else if (strcmp (p, "enter") == 0)
36403 cp_lexer_consume_token (parser->lexer);
36404 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36405 return false;
36407 else if (strcmp (p, "exit") == 0)
36409 cp_lexer_consume_token (parser->lexer);
36410 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36411 return false;
36413 else if (strcmp (p, "update") == 0)
36415 cp_lexer_consume_token (parser->lexer);
36416 return cp_parser_omp_target_update (parser, pragma_tok, context);
36419 if (!flag_openmp) /* flag_openmp_simd */
36421 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36422 return false;
36425 stmt = make_node (OMP_TARGET);
36426 TREE_TYPE (stmt) = void_type_node;
36428 OMP_TARGET_CLAUSES (stmt)
36429 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36430 "#pragma omp target", pragma_tok);
36431 pc = &OMP_TARGET_CLAUSES (stmt);
36432 keep_next_level (true);
36433 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36435 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36436 add_stmt (stmt);
36438 check_clauses:
36439 while (*pc)
36441 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36442 switch (OMP_CLAUSE_MAP_KIND (*pc))
36444 case GOMP_MAP_TO:
36445 case GOMP_MAP_ALWAYS_TO:
36446 case GOMP_MAP_FROM:
36447 case GOMP_MAP_ALWAYS_FROM:
36448 case GOMP_MAP_TOFROM:
36449 case GOMP_MAP_ALWAYS_TOFROM:
36450 case GOMP_MAP_ALLOC:
36451 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36452 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36453 case GOMP_MAP_ALWAYS_POINTER:
36454 break;
36455 default:
36456 error_at (OMP_CLAUSE_LOCATION (*pc),
36457 "%<#pragma omp target%> with map-type other "
36458 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36459 "on %<map%> clause");
36460 *pc = OMP_CLAUSE_CHAIN (*pc);
36461 continue;
36463 pc = &OMP_CLAUSE_CHAIN (*pc);
36465 return true;
36468 /* OpenACC 2.0:
36469 # pragma acc cache (variable-list) new-line
36472 static tree
36473 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36475 tree stmt, clauses;
36477 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36478 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36480 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36482 stmt = make_node (OACC_CACHE);
36483 TREE_TYPE (stmt) = void_type_node;
36484 OACC_CACHE_CLAUSES (stmt) = clauses;
36485 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36486 add_stmt (stmt);
36488 return stmt;
36491 /* OpenACC 2.0:
36492 # pragma acc data oacc-data-clause[optseq] new-line
36493 structured-block */
36495 #define OACC_DATA_CLAUSE_MASK \
36496 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36508 static tree
36509 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36511 tree stmt, clauses, block;
36512 unsigned int save;
36514 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36515 "#pragma acc data", pragma_tok);
36517 block = begin_omp_parallel ();
36518 save = cp_parser_begin_omp_structured_block (parser);
36519 cp_parser_statement (parser, NULL_TREE, false, if_p);
36520 cp_parser_end_omp_structured_block (parser, save);
36521 stmt = finish_oacc_data (clauses, block);
36522 return stmt;
36525 /* OpenACC 2.0:
36526 # pragma acc host_data <clauses> new-line
36527 structured-block */
36529 #define OACC_HOST_DATA_CLAUSE_MASK \
36530 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36532 static tree
36533 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36535 tree stmt, clauses, block;
36536 unsigned int save;
36538 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36539 "#pragma acc host_data", pragma_tok);
36541 block = begin_omp_parallel ();
36542 save = cp_parser_begin_omp_structured_block (parser);
36543 cp_parser_statement (parser, NULL_TREE, false, if_p);
36544 cp_parser_end_omp_structured_block (parser, save);
36545 stmt = finish_oacc_host_data (clauses, block);
36546 return stmt;
36549 /* OpenACC 2.0:
36550 # pragma acc declare oacc-data-clause[optseq] new-line
36553 #define OACC_DECLARE_CLAUSE_MASK \
36554 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36567 static tree
36568 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36570 tree clauses, stmt;
36571 bool error = false;
36573 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36574 "#pragma acc declare", pragma_tok, true);
36577 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36579 error_at (pragma_tok->location,
36580 "no valid clauses specified in %<#pragma acc declare%>");
36581 return NULL_TREE;
36584 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36586 location_t loc = OMP_CLAUSE_LOCATION (t);
36587 tree decl = OMP_CLAUSE_DECL (t);
36588 if (!DECL_P (decl))
36590 error_at (loc, "array section in %<#pragma acc declare%>");
36591 error = true;
36592 continue;
36594 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36595 switch (OMP_CLAUSE_MAP_KIND (t))
36597 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36598 case GOMP_MAP_FORCE_ALLOC:
36599 case GOMP_MAP_FORCE_TO:
36600 case GOMP_MAP_FORCE_DEVICEPTR:
36601 case GOMP_MAP_DEVICE_RESIDENT:
36602 break;
36604 case GOMP_MAP_LINK:
36605 if (!global_bindings_p ()
36606 && (TREE_STATIC (decl)
36607 || !DECL_EXTERNAL (decl)))
36609 error_at (loc,
36610 "%qD must be a global variable in "
36611 "%<#pragma acc declare link%>",
36612 decl);
36613 error = true;
36614 continue;
36616 break;
36618 default:
36619 if (global_bindings_p ())
36621 error_at (loc, "invalid OpenACC clause at file scope");
36622 error = true;
36623 continue;
36625 if (DECL_EXTERNAL (decl))
36627 error_at (loc,
36628 "invalid use of %<extern%> variable %qD "
36629 "in %<#pragma acc declare%>", decl);
36630 error = true;
36631 continue;
36633 else if (TREE_PUBLIC (decl))
36635 error_at (loc,
36636 "invalid use of %<global%> variable %qD "
36637 "in %<#pragma acc declare%>", decl);
36638 error = true;
36639 continue;
36641 break;
36644 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36645 || lookup_attribute ("omp declare target link",
36646 DECL_ATTRIBUTES (decl)))
36648 error_at (loc, "variable %qD used more than once with "
36649 "%<#pragma acc declare%>", decl);
36650 error = true;
36651 continue;
36654 if (!error)
36656 tree id;
36658 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36659 id = get_identifier ("omp declare target link");
36660 else
36661 id = get_identifier ("omp declare target");
36663 DECL_ATTRIBUTES (decl)
36664 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36665 if (global_bindings_p ())
36667 symtab_node *node = symtab_node::get (decl);
36668 if (node != NULL)
36670 node->offloadable = 1;
36671 if (ENABLE_OFFLOADING)
36673 g->have_offload = true;
36674 if (is_a <varpool_node *> (node))
36675 vec_safe_push (offload_vars, decl);
36682 if (error || global_bindings_p ())
36683 return NULL_TREE;
36685 stmt = make_node (OACC_DECLARE);
36686 TREE_TYPE (stmt) = void_type_node;
36687 OACC_DECLARE_CLAUSES (stmt) = clauses;
36688 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36690 add_stmt (stmt);
36692 return NULL_TREE;
36695 /* OpenACC 2.0:
36696 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36700 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36702 LOC is the location of the #pragma token.
36705 #define OACC_ENTER_DATA_CLAUSE_MASK \
36706 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36714 #define OACC_EXIT_DATA_CLAUSE_MASK \
36715 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36721 static tree
36722 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36723 bool enter)
36725 location_t loc = pragma_tok->location;
36726 tree stmt, clauses;
36727 const char *p = "";
36729 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36730 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36732 if (strcmp (p, "data") != 0)
36734 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36735 enter ? "enter" : "exit");
36736 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36737 return NULL_TREE;
36740 cp_lexer_consume_token (parser->lexer);
36742 if (enter)
36743 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36744 "#pragma acc enter data", pragma_tok);
36745 else
36746 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36747 "#pragma acc exit data", pragma_tok);
36749 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36751 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36752 enter ? "enter" : "exit");
36753 return NULL_TREE;
36756 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36757 TREE_TYPE (stmt) = void_type_node;
36758 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36759 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36760 add_stmt (stmt);
36761 return stmt;
36764 /* OpenACC 2.0:
36765 # pragma acc loop oacc-loop-clause[optseq] new-line
36766 structured-block */
36768 #define OACC_LOOP_CLAUSE_MASK \
36769 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36780 static tree
36781 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36782 omp_clause_mask mask, tree *cclauses, bool *if_p)
36784 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36786 strcat (p_name, " loop");
36787 mask |= OACC_LOOP_CLAUSE_MASK;
36789 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36790 cclauses == NULL);
36791 if (cclauses)
36793 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36794 if (*cclauses)
36795 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36796 if (clauses)
36797 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36800 tree block = begin_omp_structured_block ();
36801 int save = cp_parser_begin_omp_structured_block (parser);
36802 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36803 cp_parser_end_omp_structured_block (parser, save);
36804 add_stmt (finish_omp_structured_block (block));
36806 return stmt;
36809 /* OpenACC 2.0:
36810 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36811 structured-block
36815 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36816 structured-block
36819 #define OACC_KERNELS_CLAUSE_MASK \
36820 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36838 #define OACC_PARALLEL_CLAUSE_MASK \
36839 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36860 static tree
36861 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36862 char *p_name, bool *if_p)
36864 omp_clause_mask mask;
36865 enum tree_code code;
36866 switch (cp_parser_pragma_kind (pragma_tok))
36868 case PRAGMA_OACC_KERNELS:
36869 strcat (p_name, " kernels");
36870 mask = OACC_KERNELS_CLAUSE_MASK;
36871 code = OACC_KERNELS;
36872 break;
36873 case PRAGMA_OACC_PARALLEL:
36874 strcat (p_name, " parallel");
36875 mask = OACC_PARALLEL_CLAUSE_MASK;
36876 code = OACC_PARALLEL;
36877 break;
36878 default:
36879 gcc_unreachable ();
36882 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36884 const char *p
36885 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36886 if (strcmp (p, "loop") == 0)
36888 cp_lexer_consume_token (parser->lexer);
36889 tree block = begin_omp_parallel ();
36890 tree clauses;
36891 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36892 if_p);
36893 return finish_omp_construct (code, block, clauses);
36897 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36899 tree block = begin_omp_parallel ();
36900 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36901 cp_parser_statement (parser, NULL_TREE, false, if_p);
36902 cp_parser_end_omp_structured_block (parser, save);
36903 return finish_omp_construct (code, block, clauses);
36906 /* OpenACC 2.0:
36907 # pragma acc update oacc-update-clause[optseq] new-line
36910 #define OACC_UPDATE_CLAUSE_MASK \
36911 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36918 static tree
36919 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36921 tree stmt, clauses;
36923 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36924 "#pragma acc update", pragma_tok);
36926 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36928 error_at (pragma_tok->location,
36929 "%<#pragma acc update%> must contain at least one "
36930 "%<device%> or %<host%> or %<self%> clause");
36931 return NULL_TREE;
36934 stmt = make_node (OACC_UPDATE);
36935 TREE_TYPE (stmt) = void_type_node;
36936 OACC_UPDATE_CLAUSES (stmt) = clauses;
36937 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36938 add_stmt (stmt);
36939 return stmt;
36942 /* OpenACC 2.0:
36943 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36945 LOC is the location of the #pragma token.
36948 #define OACC_WAIT_CLAUSE_MASK \
36949 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36951 static tree
36952 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36954 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36955 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36957 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36958 list = cp_parser_oacc_wait_list (parser, loc, list);
36960 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36961 "#pragma acc wait", pragma_tok);
36963 stmt = c_finish_oacc_wait (loc, list, clauses);
36964 stmt = finish_expr_stmt (stmt);
36966 return stmt;
36969 /* OpenMP 4.0:
36970 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36972 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36973 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36980 static void
36981 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36982 enum pragma_context context)
36984 bool first_p = parser->omp_declare_simd == NULL;
36985 cp_omp_declare_simd_data data;
36986 if (first_p)
36988 data.error_seen = false;
36989 data.fndecl_seen = false;
36990 data.tokens = vNULL;
36991 data.clauses = NULL_TREE;
36992 /* It is safe to take the address of a local variable; it will only be
36993 used while this scope is live. */
36994 parser->omp_declare_simd = &data;
36997 /* Store away all pragma tokens. */
36998 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36999 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37000 cp_lexer_consume_token (parser->lexer);
37001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37002 parser->omp_declare_simd->error_seen = true;
37003 cp_parser_require_pragma_eol (parser, pragma_tok);
37004 struct cp_token_cache *cp
37005 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37006 parser->omp_declare_simd->tokens.safe_push (cp);
37008 if (first_p)
37010 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37011 cp_parser_pragma (parser, context, NULL);
37012 switch (context)
37014 case pragma_external:
37015 cp_parser_declaration (parser);
37016 break;
37017 case pragma_member:
37018 cp_parser_member_declaration (parser);
37019 break;
37020 case pragma_objc_icode:
37021 cp_parser_block_declaration (parser, /*statement_p=*/false);
37022 break;
37023 default:
37024 cp_parser_declaration_statement (parser);
37025 break;
37027 if (parser->omp_declare_simd
37028 && !parser->omp_declare_simd->error_seen
37029 && !parser->omp_declare_simd->fndecl_seen)
37030 error_at (pragma_tok->location,
37031 "%<#pragma omp declare simd%> not immediately followed by "
37032 "function declaration or definition");
37033 data.tokens.release ();
37034 parser->omp_declare_simd = NULL;
37038 /* Finalize #pragma omp declare simd clauses after direct declarator has
37039 been parsed, and put that into "omp declare simd" attribute. */
37041 static tree
37042 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37044 struct cp_token_cache *ce;
37045 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37046 int i;
37048 if (!data->error_seen && data->fndecl_seen)
37050 error ("%<#pragma omp declare simd%> not immediately followed by "
37051 "a single function declaration or definition");
37052 data->error_seen = true;
37054 if (data->error_seen)
37055 return attrs;
37057 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37059 tree c, cl;
37061 cp_parser_push_lexer_for_tokens (parser, ce);
37062 parser->lexer->in_pragma = true;
37063 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37064 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37065 cp_lexer_consume_token (parser->lexer);
37066 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37067 "#pragma omp declare simd", pragma_tok);
37068 cp_parser_pop_lexer (parser);
37069 if (cl)
37070 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37071 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37072 TREE_CHAIN (c) = attrs;
37073 if (processing_template_decl)
37074 ATTR_IS_DEPENDENT (c) = 1;
37075 attrs = c;
37078 data->fndecl_seen = true;
37079 return attrs;
37083 /* OpenMP 4.0:
37084 # pragma omp declare target new-line
37085 declarations and definitions
37086 # pragma omp end declare target new-line
37088 OpenMP 4.5:
37089 # pragma omp declare target ( extended-list ) new-line
37091 # pragma omp declare target declare-target-clauses[seq] new-line */
37093 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37097 static void
37098 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37100 tree clauses = NULL_TREE;
37101 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37102 clauses
37103 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37104 "#pragma omp declare target", pragma_tok);
37105 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37107 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37108 clauses);
37109 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37110 cp_parser_require_pragma_eol (parser, pragma_tok);
37112 else
37114 cp_parser_require_pragma_eol (parser, pragma_tok);
37115 scope_chain->omp_declare_target_attribute++;
37116 return;
37118 if (scope_chain->omp_declare_target_attribute)
37119 error_at (pragma_tok->location,
37120 "%<#pragma omp declare target%> with clauses in between "
37121 "%<#pragma omp declare target%> without clauses and "
37122 "%<#pragma omp end declare target%>");
37123 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37125 tree t = OMP_CLAUSE_DECL (c), id;
37126 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37127 tree at2 = lookup_attribute ("omp declare target link",
37128 DECL_ATTRIBUTES (t));
37129 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37131 id = get_identifier ("omp declare target link");
37132 std::swap (at1, at2);
37134 else
37135 id = get_identifier ("omp declare target");
37136 if (at2)
37138 error_at (OMP_CLAUSE_LOCATION (c),
37139 "%qD specified both in declare target %<link%> and %<to%>"
37140 " clauses", t);
37141 continue;
37143 if (!at1)
37145 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37146 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37147 continue;
37149 symtab_node *node = symtab_node::get (t);
37150 if (node != NULL)
37152 node->offloadable = 1;
37153 if (ENABLE_OFFLOADING)
37155 g->have_offload = true;
37156 if (is_a <varpool_node *> (node))
37157 vec_safe_push (offload_vars, t);
37164 static void
37165 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37167 const char *p = "";
37168 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37170 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37171 p = IDENTIFIER_POINTER (id);
37173 if (strcmp (p, "declare") == 0)
37175 cp_lexer_consume_token (parser->lexer);
37176 p = "";
37177 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37179 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37180 p = IDENTIFIER_POINTER (id);
37182 if (strcmp (p, "target") == 0)
37183 cp_lexer_consume_token (parser->lexer);
37184 else
37186 cp_parser_error (parser, "expected %<target%>");
37187 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37188 return;
37191 else
37193 cp_parser_error (parser, "expected %<declare%>");
37194 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37195 return;
37197 cp_parser_require_pragma_eol (parser, pragma_tok);
37198 if (!scope_chain->omp_declare_target_attribute)
37199 error_at (pragma_tok->location,
37200 "%<#pragma omp end declare target%> without corresponding "
37201 "%<#pragma omp declare target%>");
37202 else
37203 scope_chain->omp_declare_target_attribute--;
37206 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37207 expression and optional initializer clause of
37208 #pragma omp declare reduction. We store the expression(s) as
37209 either 3, 6 or 7 special statements inside of the artificial function's
37210 body. The first two statements are DECL_EXPRs for the artificial
37211 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37212 expression that uses those variables.
37213 If there was any INITIALIZER clause, this is followed by further statements,
37214 the fourth and fifth statements are DECL_EXPRs for the artificial
37215 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37216 constructor variant (first token after open paren is not omp_priv),
37217 then the sixth statement is a statement with the function call expression
37218 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37219 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37220 to initialize the OMP_PRIV artificial variable and there is seventh
37221 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37223 static bool
37224 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37226 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37227 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37228 type = TREE_TYPE (type);
37229 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37230 DECL_ARTIFICIAL (omp_out) = 1;
37231 pushdecl (omp_out);
37232 add_decl_expr (omp_out);
37233 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37234 DECL_ARTIFICIAL (omp_in) = 1;
37235 pushdecl (omp_in);
37236 add_decl_expr (omp_in);
37237 tree combiner;
37238 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37240 keep_next_level (true);
37241 tree block = begin_omp_structured_block ();
37242 combiner = cp_parser_expression (parser);
37243 finish_expr_stmt (combiner);
37244 block = finish_omp_structured_block (block);
37245 add_stmt (block);
37247 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37248 return false;
37250 const char *p = "";
37251 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37253 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37254 p = IDENTIFIER_POINTER (id);
37257 if (strcmp (p, "initializer") == 0)
37259 cp_lexer_consume_token (parser->lexer);
37260 matching_parens parens;
37261 if (!parens.require_open (parser))
37262 return false;
37264 p = "";
37265 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37267 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37268 p = IDENTIFIER_POINTER (id);
37271 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37272 DECL_ARTIFICIAL (omp_priv) = 1;
37273 pushdecl (omp_priv);
37274 add_decl_expr (omp_priv);
37275 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37276 DECL_ARTIFICIAL (omp_orig) = 1;
37277 pushdecl (omp_orig);
37278 add_decl_expr (omp_orig);
37280 keep_next_level (true);
37281 block = begin_omp_structured_block ();
37283 bool ctor = false;
37284 if (strcmp (p, "omp_priv") == 0)
37286 bool is_direct_init, is_non_constant_init;
37287 ctor = true;
37288 cp_lexer_consume_token (parser->lexer);
37289 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37290 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37291 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37292 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37293 == CPP_CLOSE_PAREN
37294 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37295 == CPP_CLOSE_PAREN))
37297 finish_omp_structured_block (block);
37298 error ("invalid initializer clause");
37299 return false;
37301 initializer = cp_parser_initializer (parser, &is_direct_init,
37302 &is_non_constant_init);
37303 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37304 NULL_TREE, LOOKUP_ONLYCONVERTING);
37306 else
37308 cp_parser_parse_tentatively (parser);
37309 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37310 /*check_dependency_p=*/true,
37311 /*template_p=*/NULL,
37312 /*declarator_p=*/false,
37313 /*optional_p=*/false);
37314 vec<tree, va_gc> *args;
37315 if (fn_name == error_mark_node
37316 || cp_parser_error_occurred (parser)
37317 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37318 || ((args = cp_parser_parenthesized_expression_list
37319 (parser, non_attr, /*cast_p=*/false,
37320 /*allow_expansion_p=*/true,
37321 /*non_constant_p=*/NULL)),
37322 cp_parser_error_occurred (parser)))
37324 finish_omp_structured_block (block);
37325 cp_parser_abort_tentative_parse (parser);
37326 cp_parser_error (parser, "expected id-expression (arguments)");
37327 return false;
37329 unsigned int i;
37330 tree arg;
37331 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37332 if (arg == omp_priv
37333 || (TREE_CODE (arg) == ADDR_EXPR
37334 && TREE_OPERAND (arg, 0) == omp_priv))
37335 break;
37336 cp_parser_abort_tentative_parse (parser);
37337 if (arg == NULL_TREE)
37338 error ("one of the initializer call arguments should be %<omp_priv%>"
37339 " or %<&omp_priv%>");
37340 initializer = cp_parser_postfix_expression (parser, false, false, false,
37341 false, NULL);
37342 finish_expr_stmt (initializer);
37345 block = finish_omp_structured_block (block);
37346 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37347 add_stmt (block);
37349 if (ctor)
37350 add_decl_expr (omp_orig);
37352 if (!parens.require_close (parser))
37353 return false;
37356 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37357 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37358 UNKNOWN_LOCATION);
37360 return true;
37363 /* OpenMP 4.0
37364 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37365 initializer-clause[opt] new-line
37367 initializer-clause:
37368 initializer (omp_priv initializer)
37369 initializer (function-name (argument-list)) */
37371 static void
37372 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37373 enum pragma_context)
37375 auto_vec<tree> types;
37376 enum tree_code reduc_code = ERROR_MARK;
37377 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37378 unsigned int i;
37379 cp_token *first_token;
37380 cp_token_cache *cp;
37381 int errs;
37382 void *p;
37384 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37385 p = obstack_alloc (&declarator_obstack, 0);
37387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37388 goto fail;
37390 switch (cp_lexer_peek_token (parser->lexer)->type)
37392 case CPP_PLUS:
37393 reduc_code = PLUS_EXPR;
37394 break;
37395 case CPP_MULT:
37396 reduc_code = MULT_EXPR;
37397 break;
37398 case CPP_MINUS:
37399 reduc_code = MINUS_EXPR;
37400 break;
37401 case CPP_AND:
37402 reduc_code = BIT_AND_EXPR;
37403 break;
37404 case CPP_XOR:
37405 reduc_code = BIT_XOR_EXPR;
37406 break;
37407 case CPP_OR:
37408 reduc_code = BIT_IOR_EXPR;
37409 break;
37410 case CPP_AND_AND:
37411 reduc_code = TRUTH_ANDIF_EXPR;
37412 break;
37413 case CPP_OR_OR:
37414 reduc_code = TRUTH_ORIF_EXPR;
37415 break;
37416 case CPP_NAME:
37417 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37418 break;
37419 default:
37420 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37421 "%<|%>, %<&&%>, %<||%> or identifier");
37422 goto fail;
37425 if (reduc_code != ERROR_MARK)
37426 cp_lexer_consume_token (parser->lexer);
37428 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37429 if (reduc_id == error_mark_node)
37430 goto fail;
37432 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37433 goto fail;
37435 /* Types may not be defined in declare reduction type list. */
37436 const char *saved_message;
37437 saved_message = parser->type_definition_forbidden_message;
37438 parser->type_definition_forbidden_message
37439 = G_("types may not be defined in declare reduction type list");
37440 bool saved_colon_corrects_to_scope_p;
37441 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37442 parser->colon_corrects_to_scope_p = false;
37443 bool saved_colon_doesnt_start_class_def_p;
37444 saved_colon_doesnt_start_class_def_p
37445 = parser->colon_doesnt_start_class_def_p;
37446 parser->colon_doesnt_start_class_def_p = true;
37448 while (true)
37450 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37451 type = cp_parser_type_id (parser);
37452 if (type == error_mark_node)
37454 else if (ARITHMETIC_TYPE_P (type)
37455 && (orig_reduc_id == NULL_TREE
37456 || (TREE_CODE (type) != COMPLEX_TYPE
37457 && (id_equal (orig_reduc_id, "min")
37458 || id_equal (orig_reduc_id, "max")))))
37459 error_at (loc, "predeclared arithmetic type %qT in "
37460 "%<#pragma omp declare reduction%>", type);
37461 else if (TREE_CODE (type) == FUNCTION_TYPE
37462 || TREE_CODE (type) == METHOD_TYPE
37463 || TREE_CODE (type) == ARRAY_TYPE)
37464 error_at (loc, "function or array type %qT in "
37465 "%<#pragma omp declare reduction%>", type);
37466 else if (TREE_CODE (type) == REFERENCE_TYPE)
37467 error_at (loc, "reference type %qT in "
37468 "%<#pragma omp declare reduction%>", type);
37469 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37470 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37471 "%<#pragma omp declare reduction%>", type);
37472 else
37473 types.safe_push (type);
37475 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37476 cp_lexer_consume_token (parser->lexer);
37477 else
37478 break;
37481 /* Restore the saved message. */
37482 parser->type_definition_forbidden_message = saved_message;
37483 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37484 parser->colon_doesnt_start_class_def_p
37485 = saved_colon_doesnt_start_class_def_p;
37487 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37488 || types.is_empty ())
37490 fail:
37491 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37492 goto done;
37495 first_token = cp_lexer_peek_token (parser->lexer);
37496 cp = NULL;
37497 errs = errorcount;
37498 FOR_EACH_VEC_ELT (types, i, type)
37500 tree fntype
37501 = build_function_type_list (void_type_node,
37502 cp_build_reference_type (type, false),
37503 NULL_TREE);
37504 tree this_reduc_id = reduc_id;
37505 if (!dependent_type_p (type))
37506 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37507 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37508 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37509 DECL_ARTIFICIAL (fndecl) = 1;
37510 DECL_EXTERNAL (fndecl) = 1;
37511 DECL_DECLARED_INLINE_P (fndecl) = 1;
37512 DECL_IGNORED_P (fndecl) = 1;
37513 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37514 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37515 DECL_ATTRIBUTES (fndecl)
37516 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37517 DECL_ATTRIBUTES (fndecl));
37518 if (processing_template_decl)
37519 fndecl = push_template_decl (fndecl);
37520 bool block_scope = false;
37521 tree block = NULL_TREE;
37522 if (current_function_decl)
37524 block_scope = true;
37525 DECL_CONTEXT (fndecl) = global_namespace;
37526 if (!processing_template_decl)
37527 pushdecl (fndecl);
37529 else if (current_class_type)
37531 if (cp == NULL)
37533 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37534 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37535 cp_lexer_consume_token (parser->lexer);
37536 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37537 goto fail;
37538 cp = cp_token_cache_new (first_token,
37539 cp_lexer_peek_nth_token (parser->lexer,
37540 2));
37542 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37543 finish_member_declaration (fndecl);
37544 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37545 DECL_PENDING_INLINE_P (fndecl) = 1;
37546 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37547 continue;
37549 else
37551 DECL_CONTEXT (fndecl) = current_namespace;
37552 pushdecl (fndecl);
37554 if (!block_scope)
37555 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37556 else
37557 block = begin_omp_structured_block ();
37558 if (cp)
37560 cp_parser_push_lexer_for_tokens (parser, cp);
37561 parser->lexer->in_pragma = true;
37563 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37565 if (!block_scope)
37566 finish_function (/*inline_p=*/false);
37567 else
37568 DECL_CONTEXT (fndecl) = current_function_decl;
37569 if (cp)
37570 cp_parser_pop_lexer (parser);
37571 goto fail;
37573 if (cp)
37574 cp_parser_pop_lexer (parser);
37575 if (!block_scope)
37576 finish_function (/*inline_p=*/false);
37577 else
37579 DECL_CONTEXT (fndecl) = current_function_decl;
37580 block = finish_omp_structured_block (block);
37581 if (TREE_CODE (block) == BIND_EXPR)
37582 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37583 else if (TREE_CODE (block) == STATEMENT_LIST)
37584 DECL_SAVED_TREE (fndecl) = block;
37585 if (processing_template_decl)
37586 add_decl_expr (fndecl);
37588 cp_check_omp_declare_reduction (fndecl);
37589 if (cp == NULL && types.length () > 1)
37590 cp = cp_token_cache_new (first_token,
37591 cp_lexer_peek_nth_token (parser->lexer, 2));
37592 if (errs != errorcount)
37593 break;
37596 cp_parser_require_pragma_eol (parser, pragma_tok);
37598 done:
37599 /* Free any declarators allocated. */
37600 obstack_free (&declarator_obstack, p);
37603 /* OpenMP 4.0
37604 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37605 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37606 initializer-clause[opt] new-line
37607 #pragma omp declare target new-line */
37609 static bool
37610 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37611 enum pragma_context context)
37613 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37615 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37616 const char *p = IDENTIFIER_POINTER (id);
37618 if (strcmp (p, "simd") == 0)
37620 cp_lexer_consume_token (parser->lexer);
37621 cp_parser_omp_declare_simd (parser, pragma_tok,
37622 context);
37623 return true;
37625 cp_ensure_no_omp_declare_simd (parser);
37626 if (strcmp (p, "reduction") == 0)
37628 cp_lexer_consume_token (parser->lexer);
37629 cp_parser_omp_declare_reduction (parser, pragma_tok,
37630 context);
37631 return false;
37633 if (!flag_openmp) /* flag_openmp_simd */
37635 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37636 return false;
37638 if (strcmp (p, "target") == 0)
37640 cp_lexer_consume_token (parser->lexer);
37641 cp_parser_omp_declare_target (parser, pragma_tok);
37642 return false;
37645 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37646 "or %<target%>");
37647 cp_parser_require_pragma_eol (parser, pragma_tok);
37648 return false;
37651 /* OpenMP 4.5:
37652 #pragma omp taskloop taskloop-clause[optseq] new-line
37653 for-loop
37655 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37656 for-loop */
37658 #define OMP_TASKLOOP_CLAUSE_MASK \
37659 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37674 static tree
37675 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37676 char *p_name, omp_clause_mask mask, tree *cclauses,
37677 bool *if_p)
37679 tree clauses, sb, ret;
37680 unsigned int save;
37681 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37683 strcat (p_name, " taskloop");
37684 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37686 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37688 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37689 const char *p = IDENTIFIER_POINTER (id);
37691 if (strcmp (p, "simd") == 0)
37693 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37694 if (cclauses == NULL)
37695 cclauses = cclauses_buf;
37697 cp_lexer_consume_token (parser->lexer);
37698 if (!flag_openmp) /* flag_openmp_simd */
37699 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37700 cclauses, if_p);
37701 sb = begin_omp_structured_block ();
37702 save = cp_parser_begin_omp_structured_block (parser);
37703 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37704 cclauses, if_p);
37705 cp_parser_end_omp_structured_block (parser, save);
37706 tree body = finish_omp_structured_block (sb);
37707 if (ret == NULL)
37708 return ret;
37709 ret = make_node (OMP_TASKLOOP);
37710 TREE_TYPE (ret) = void_type_node;
37711 OMP_FOR_BODY (ret) = body;
37712 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37713 SET_EXPR_LOCATION (ret, loc);
37714 add_stmt (ret);
37715 return ret;
37718 if (!flag_openmp) /* flag_openmp_simd */
37720 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37721 return NULL_TREE;
37724 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37725 cclauses == NULL);
37726 if (cclauses)
37728 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37729 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37732 sb = begin_omp_structured_block ();
37733 save = cp_parser_begin_omp_structured_block (parser);
37735 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37736 if_p);
37738 cp_parser_end_omp_structured_block (parser, save);
37739 add_stmt (finish_omp_structured_block (sb));
37741 return ret;
37745 /* OpenACC 2.0:
37746 # pragma acc routine oacc-routine-clause[optseq] new-line
37747 function-definition
37749 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37752 #define OACC_ROUTINE_CLAUSE_MASK \
37753 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37759 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37760 component, which must resolve to a declared namespace-scope
37761 function. The clauses are either processed directly (for a named
37762 function), or defered until the immediatley following declaration
37763 is parsed. */
37765 static void
37766 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37767 enum pragma_context context)
37769 gcc_checking_assert (context == pragma_external);
37770 /* The checking for "another pragma following this one" in the "no optional
37771 '( name )'" case makes sure that we dont re-enter. */
37772 gcc_checking_assert (parser->oacc_routine == NULL);
37774 cp_oacc_routine_data data;
37775 data.error_seen = false;
37776 data.fndecl_seen = false;
37777 data.tokens = vNULL;
37778 data.clauses = NULL_TREE;
37779 data.loc = pragma_tok->location;
37780 /* It is safe to take the address of a local variable; it will only be
37781 used while this scope is live. */
37782 parser->oacc_routine = &data;
37784 /* Look for optional '( name )'. */
37785 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37787 matching_parens parens;
37788 parens.consume_open (parser); /* '(' */
37790 /* We parse the name as an id-expression. If it resolves to
37791 anything other than a non-overloaded function at namespace
37792 scope, it's an error. */
37793 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37794 tree name = cp_parser_id_expression (parser,
37795 /*template_keyword_p=*/false,
37796 /*check_dependency_p=*/false,
37797 /*template_p=*/NULL,
37798 /*declarator_p=*/false,
37799 /*optional_p=*/false);
37800 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37801 if (name != error_mark_node && decl == error_mark_node)
37802 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37804 if (decl == error_mark_node
37805 || !parens.require_close (parser))
37807 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37808 parser->oacc_routine = NULL;
37809 return;
37812 data.clauses
37813 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37814 "#pragma acc routine",
37815 cp_lexer_peek_token (parser->lexer));
37817 if (decl && is_overloaded_fn (decl)
37818 && (TREE_CODE (decl) != FUNCTION_DECL
37819 || DECL_FUNCTION_TEMPLATE_P (decl)))
37821 error_at (name_loc,
37822 "%<#pragma acc routine%> names a set of overloads");
37823 parser->oacc_routine = NULL;
37824 return;
37827 /* Perhaps we should use the same rule as declarations in different
37828 namespaces? */
37829 if (!DECL_NAMESPACE_SCOPE_P (decl))
37831 error_at (name_loc,
37832 "%qD does not refer to a namespace scope function", decl);
37833 parser->oacc_routine = NULL;
37834 return;
37837 if (TREE_CODE (decl) != FUNCTION_DECL)
37839 error_at (name_loc, "%qD does not refer to a function", decl);
37840 parser->oacc_routine = NULL;
37841 return;
37844 cp_finalize_oacc_routine (parser, decl, false);
37845 parser->oacc_routine = NULL;
37847 else /* No optional '( name )'. */
37849 /* Store away all pragma tokens. */
37850 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37851 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37852 cp_lexer_consume_token (parser->lexer);
37853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37854 parser->oacc_routine->error_seen = true;
37855 cp_parser_require_pragma_eol (parser, pragma_tok);
37856 struct cp_token_cache *cp
37857 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37858 parser->oacc_routine->tokens.safe_push (cp);
37860 /* Emit a helpful diagnostic if there's another pragma following this
37861 one. */
37862 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37864 cp_ensure_no_oacc_routine (parser);
37865 data.tokens.release ();
37866 /* ..., and then just keep going. */
37867 return;
37870 /* We only have to consider the pragma_external case here. */
37871 cp_parser_declaration (parser);
37872 if (parser->oacc_routine
37873 && !parser->oacc_routine->fndecl_seen)
37874 cp_ensure_no_oacc_routine (parser);
37875 else
37876 parser->oacc_routine = NULL;
37877 data.tokens.release ();
37881 /* Finalize #pragma acc routine clauses after direct declarator has
37882 been parsed. */
37884 static tree
37885 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37887 struct cp_token_cache *ce;
37888 cp_oacc_routine_data *data = parser->oacc_routine;
37890 if (!data->error_seen && data->fndecl_seen)
37892 error_at (data->loc,
37893 "%<#pragma acc routine%> not immediately followed by "
37894 "a single function declaration or definition");
37895 data->error_seen = true;
37897 if (data->error_seen)
37898 return attrs;
37900 gcc_checking_assert (data->tokens.length () == 1);
37901 ce = data->tokens[0];
37903 cp_parser_push_lexer_for_tokens (parser, ce);
37904 parser->lexer->in_pragma = true;
37905 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37907 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37908 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37909 parser->oacc_routine->clauses
37910 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37911 "#pragma acc routine", pragma_tok);
37912 cp_parser_pop_lexer (parser);
37913 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37914 fndecl_seen. */
37916 return attrs;
37919 /* Apply any saved OpenACC routine clauses to a just-parsed
37920 declaration. */
37922 static void
37923 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37925 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37927 /* Keep going if we're in error reporting mode. */
37928 if (parser->oacc_routine->error_seen
37929 || fndecl == error_mark_node)
37930 return;
37932 if (parser->oacc_routine->fndecl_seen)
37934 error_at (parser->oacc_routine->loc,
37935 "%<#pragma acc routine%> not immediately followed by"
37936 " a single function declaration or definition");
37937 parser->oacc_routine = NULL;
37938 return;
37940 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37942 cp_ensure_no_oacc_routine (parser);
37943 return;
37946 if (oacc_get_fn_attrib (fndecl))
37948 error_at (parser->oacc_routine->loc,
37949 "%<#pragma acc routine%> already applied to %qD", fndecl);
37950 parser->oacc_routine = NULL;
37951 return;
37954 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37956 error_at (parser->oacc_routine->loc,
37957 TREE_USED (fndecl)
37958 ? G_("%<#pragma acc routine%> must be applied before use")
37959 : G_("%<#pragma acc routine%> must be applied before "
37960 "definition"));
37961 parser->oacc_routine = NULL;
37962 return;
37965 /* Process the routine's dimension clauses. */
37966 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37967 oacc_replace_fn_attrib (fndecl, dims);
37969 /* Add an "omp declare target" attribute. */
37970 DECL_ATTRIBUTES (fndecl)
37971 = tree_cons (get_identifier ("omp declare target"),
37972 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37974 /* Don't unset parser->oacc_routine here: we may still need it to
37975 diagnose wrong usage. But, remember that we've used this "#pragma acc
37976 routine". */
37977 parser->oacc_routine->fndecl_seen = true;
37981 /* Main entry point to OpenMP statement pragmas. */
37983 static void
37984 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37986 tree stmt;
37987 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37988 omp_clause_mask mask (0);
37990 switch (cp_parser_pragma_kind (pragma_tok))
37992 case PRAGMA_OACC_ATOMIC:
37993 cp_parser_omp_atomic (parser, pragma_tok);
37994 return;
37995 case PRAGMA_OACC_CACHE:
37996 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37997 break;
37998 case PRAGMA_OACC_DATA:
37999 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38000 break;
38001 case PRAGMA_OACC_ENTER_DATA:
38002 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38003 break;
38004 case PRAGMA_OACC_EXIT_DATA:
38005 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38006 break;
38007 case PRAGMA_OACC_HOST_DATA:
38008 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38009 break;
38010 case PRAGMA_OACC_KERNELS:
38011 case PRAGMA_OACC_PARALLEL:
38012 strcpy (p_name, "#pragma acc");
38013 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38014 if_p);
38015 break;
38016 case PRAGMA_OACC_LOOP:
38017 strcpy (p_name, "#pragma acc");
38018 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38019 if_p);
38020 break;
38021 case PRAGMA_OACC_UPDATE:
38022 stmt = cp_parser_oacc_update (parser, pragma_tok);
38023 break;
38024 case PRAGMA_OACC_WAIT:
38025 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38026 break;
38027 case PRAGMA_OMP_ATOMIC:
38028 cp_parser_omp_atomic (parser, pragma_tok);
38029 return;
38030 case PRAGMA_OMP_CRITICAL:
38031 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38032 break;
38033 case PRAGMA_OMP_DISTRIBUTE:
38034 strcpy (p_name, "#pragma omp");
38035 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38036 if_p);
38037 break;
38038 case PRAGMA_OMP_FOR:
38039 strcpy (p_name, "#pragma omp");
38040 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38041 if_p);
38042 break;
38043 case PRAGMA_OMP_MASTER:
38044 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38045 break;
38046 case PRAGMA_OMP_PARALLEL:
38047 strcpy (p_name, "#pragma omp");
38048 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38049 if_p);
38050 break;
38051 case PRAGMA_OMP_SECTIONS:
38052 strcpy (p_name, "#pragma omp");
38053 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38054 break;
38055 case PRAGMA_OMP_SIMD:
38056 strcpy (p_name, "#pragma omp");
38057 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38058 if_p);
38059 break;
38060 case PRAGMA_OMP_SINGLE:
38061 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38062 break;
38063 case PRAGMA_OMP_TASK:
38064 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38065 break;
38066 case PRAGMA_OMP_TASKGROUP:
38067 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38068 break;
38069 case PRAGMA_OMP_TASKLOOP:
38070 strcpy (p_name, "#pragma omp");
38071 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38072 if_p);
38073 break;
38074 case PRAGMA_OMP_TEAMS:
38075 strcpy (p_name, "#pragma omp");
38076 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38077 if_p);
38078 break;
38079 default:
38080 gcc_unreachable ();
38083 protected_set_expr_location (stmt, pragma_tok->location);
38086 /* Transactional Memory parsing routines. */
38088 /* Parse a transaction attribute.
38090 txn-attribute:
38091 attribute
38092 [ [ identifier ] ]
38094 We use this instead of cp_parser_attributes_opt for transactions to avoid
38095 the pedwarn in C++98 mode. */
38097 static tree
38098 cp_parser_txn_attribute_opt (cp_parser *parser)
38100 cp_token *token;
38101 tree attr_name, attr = NULL;
38103 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38104 return cp_parser_attributes_opt (parser);
38106 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38107 return NULL_TREE;
38108 cp_lexer_consume_token (parser->lexer);
38109 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38110 goto error1;
38112 token = cp_lexer_peek_token (parser->lexer);
38113 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38115 token = cp_lexer_consume_token (parser->lexer);
38117 attr_name = (token->type == CPP_KEYWORD
38118 /* For keywords, use the canonical spelling,
38119 not the parsed identifier. */
38120 ? ridpointers[(int) token->keyword]
38121 : token->u.value);
38122 attr = build_tree_list (attr_name, NULL_TREE);
38124 else
38125 cp_parser_error (parser, "expected identifier");
38127 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38128 error1:
38129 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38130 return attr;
38133 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38135 transaction-statement:
38136 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38137 compound-statement
38138 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38141 static tree
38142 cp_parser_transaction (cp_parser *parser, cp_token *token)
38144 unsigned char old_in = parser->in_transaction;
38145 unsigned char this_in = 1, new_in;
38146 enum rid keyword = token->keyword;
38147 tree stmt, attrs, noex;
38149 cp_lexer_consume_token (parser->lexer);
38151 if (keyword == RID_TRANSACTION_RELAXED
38152 || keyword == RID_SYNCHRONIZED)
38153 this_in |= TM_STMT_ATTR_RELAXED;
38154 else
38156 attrs = cp_parser_txn_attribute_opt (parser);
38157 if (attrs)
38158 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38161 /* Parse a noexcept specification. */
38162 if (keyword == RID_ATOMIC_NOEXCEPT)
38163 noex = boolean_true_node;
38164 else if (keyword == RID_ATOMIC_CANCEL)
38166 /* cancel-and-throw is unimplemented. */
38167 sorry ("atomic_cancel");
38168 noex = NULL_TREE;
38170 else
38171 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38173 /* Keep track if we're in the lexical scope of an outer transaction. */
38174 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38176 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38178 parser->in_transaction = new_in;
38179 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38180 parser->in_transaction = old_in;
38182 finish_transaction_stmt (stmt, NULL, this_in, noex);
38184 return stmt;
38187 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38189 transaction-expression:
38190 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38191 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38194 static tree
38195 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38197 unsigned char old_in = parser->in_transaction;
38198 unsigned char this_in = 1;
38199 cp_token *token;
38200 tree expr, noex;
38201 bool noex_expr;
38202 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38204 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38205 || keyword == RID_TRANSACTION_RELAXED);
38207 if (!flag_tm)
38208 error_at (loc,
38209 keyword == RID_TRANSACTION_RELAXED
38210 ? G_("%<__transaction_relaxed%> without transactional memory "
38211 "support enabled")
38212 : G_("%<__transaction_atomic%> without transactional memory "
38213 "support enabled"));
38215 token = cp_parser_require_keyword (parser, keyword,
38216 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38217 : RT_TRANSACTION_RELAXED));
38218 gcc_assert (token != NULL);
38220 if (keyword == RID_TRANSACTION_RELAXED)
38221 this_in |= TM_STMT_ATTR_RELAXED;
38223 /* Set this early. This might mean that we allow transaction_cancel in
38224 an expression that we find out later actually has to be a constexpr.
38225 However, we expect that cxx_constant_value will be able to deal with
38226 this; also, if the noexcept has no constexpr, then what we parse next
38227 really is a transaction's body. */
38228 parser->in_transaction = this_in;
38230 /* Parse a noexcept specification. */
38231 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38232 true);
38234 if (!noex || !noex_expr
38235 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38237 matching_parens parens;
38238 parens.require_open (parser);
38240 expr = cp_parser_expression (parser);
38241 expr = finish_parenthesized_expr (expr);
38243 parens.require_close (parser);
38245 else
38247 /* The only expression that is available got parsed for the noexcept
38248 already. noexcept is true then. */
38249 expr = noex;
38250 noex = boolean_true_node;
38253 expr = build_transaction_expr (token->location, expr, this_in, noex);
38254 parser->in_transaction = old_in;
38256 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38257 return error_mark_node;
38259 return (flag_tm ? expr : error_mark_node);
38262 /* Parse a function-transaction-block.
38264 function-transaction-block:
38265 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38266 function-body
38267 __transaction_atomic txn-attribute[opt] function-try-block
38268 __transaction_relaxed ctor-initializer[opt] function-body
38269 __transaction_relaxed function-try-block
38272 static void
38273 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38275 unsigned char old_in = parser->in_transaction;
38276 unsigned char new_in = 1;
38277 tree compound_stmt, stmt, attrs;
38278 cp_token *token;
38280 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38281 || keyword == RID_TRANSACTION_RELAXED);
38282 token = cp_parser_require_keyword (parser, keyword,
38283 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38284 : RT_TRANSACTION_RELAXED));
38285 gcc_assert (token != NULL);
38287 if (keyword == RID_TRANSACTION_RELAXED)
38288 new_in |= TM_STMT_ATTR_RELAXED;
38289 else
38291 attrs = cp_parser_txn_attribute_opt (parser);
38292 if (attrs)
38293 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38296 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38298 parser->in_transaction = new_in;
38300 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38301 cp_parser_function_try_block (parser);
38302 else
38303 cp_parser_ctor_initializer_opt_and_function_body
38304 (parser, /*in_function_try_block=*/false);
38306 parser->in_transaction = old_in;
38308 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38311 /* Parse a __transaction_cancel statement.
38313 cancel-statement:
38314 __transaction_cancel txn-attribute[opt] ;
38315 __transaction_cancel txn-attribute[opt] throw-expression ;
38317 ??? Cancel and throw is not yet implemented. */
38319 static tree
38320 cp_parser_transaction_cancel (cp_parser *parser)
38322 cp_token *token;
38323 bool is_outer = false;
38324 tree stmt, attrs;
38326 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38327 RT_TRANSACTION_CANCEL);
38328 gcc_assert (token != NULL);
38330 attrs = cp_parser_txn_attribute_opt (parser);
38331 if (attrs)
38332 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38334 /* ??? Parse cancel-and-throw here. */
38336 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38338 if (!flag_tm)
38340 error_at (token->location, "%<__transaction_cancel%> without "
38341 "transactional memory support enabled");
38342 return error_mark_node;
38344 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38346 error_at (token->location, "%<__transaction_cancel%> within a "
38347 "%<__transaction_relaxed%>");
38348 return error_mark_node;
38350 else if (is_outer)
38352 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38353 && !is_tm_may_cancel_outer (current_function_decl))
38355 error_at (token->location, "outer %<__transaction_cancel%> not "
38356 "within outer %<__transaction_atomic%>");
38357 error_at (token->location,
38358 " or a %<transaction_may_cancel_outer%> function");
38359 return error_mark_node;
38362 else if (parser->in_transaction == 0)
38364 error_at (token->location, "%<__transaction_cancel%> not within "
38365 "%<__transaction_atomic%>");
38366 return error_mark_node;
38369 stmt = build_tm_abort_call (token->location, is_outer);
38370 add_stmt (stmt);
38372 return stmt;
38375 /* The parser. */
38377 static GTY (()) cp_parser *the_parser;
38380 /* Special handling for the first token or line in the file. The first
38381 thing in the file might be #pragma GCC pch_preprocess, which loads a
38382 PCH file, which is a GC collection point. So we need to handle this
38383 first pragma without benefit of an existing lexer structure.
38385 Always returns one token to the caller in *FIRST_TOKEN. This is
38386 either the true first token of the file, or the first token after
38387 the initial pragma. */
38389 static void
38390 cp_parser_initial_pragma (cp_token *first_token)
38392 tree name = NULL;
38394 cp_lexer_get_preprocessor_token (NULL, first_token);
38395 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38396 return;
38398 cp_lexer_get_preprocessor_token (NULL, first_token);
38399 if (first_token->type == CPP_STRING)
38401 name = first_token->u.value;
38403 cp_lexer_get_preprocessor_token (NULL, first_token);
38404 if (first_token->type != CPP_PRAGMA_EOL)
38405 error_at (first_token->location,
38406 "junk at end of %<#pragma GCC pch_preprocess%>");
38408 else
38409 error_at (first_token->location, "expected string literal");
38411 /* Skip to the end of the pragma. */
38412 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38413 cp_lexer_get_preprocessor_token (NULL, first_token);
38415 /* Now actually load the PCH file. */
38416 if (name)
38417 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38419 /* Read one more token to return to our caller. We have to do this
38420 after reading the PCH file in, since its pointers have to be
38421 live. */
38422 cp_lexer_get_preprocessor_token (NULL, first_token);
38425 /* Parse a pragma GCC ivdep. */
38427 static bool
38428 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38430 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38431 return true;
38434 /* Parse a pragma GCC unroll. */
38436 static unsigned short
38437 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38439 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38440 tree expr = cp_parser_constant_expression (parser);
38441 unsigned short unroll;
38442 expr = maybe_constant_value (expr);
38443 HOST_WIDE_INT lunroll = 0;
38444 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38445 || TREE_CODE (expr) != INTEGER_CST
38446 || (lunroll = tree_to_shwi (expr)) < 0
38447 || lunroll >= USHRT_MAX)
38449 error_at (location, "%<#pragma GCC unroll%> requires an"
38450 " assignment-expression that evaluates to a non-negative"
38451 " integral constant less than %u", USHRT_MAX);
38452 unroll = 0;
38454 else
38456 unroll = (unsigned short)lunroll;
38457 if (unroll == 0)
38458 unroll = 1;
38460 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38461 return unroll;
38464 /* Normal parsing of a pragma token. Here we can (and must) use the
38465 regular lexer. */
38467 static bool
38468 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38470 cp_token *pragma_tok;
38471 unsigned int id;
38472 tree stmt;
38473 bool ret;
38475 pragma_tok = cp_lexer_consume_token (parser->lexer);
38476 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38477 parser->lexer->in_pragma = true;
38479 id = cp_parser_pragma_kind (pragma_tok);
38480 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38481 cp_ensure_no_omp_declare_simd (parser);
38482 switch (id)
38484 case PRAGMA_GCC_PCH_PREPROCESS:
38485 error_at (pragma_tok->location,
38486 "%<#pragma GCC pch_preprocess%> must be first");
38487 break;
38489 case PRAGMA_OMP_BARRIER:
38490 switch (context)
38492 case pragma_compound:
38493 cp_parser_omp_barrier (parser, pragma_tok);
38494 return false;
38495 case pragma_stmt:
38496 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38497 "used in compound statements", "omp barrier");
38498 break;
38499 default:
38500 goto bad_stmt;
38502 break;
38504 case PRAGMA_OMP_FLUSH:
38505 switch (context)
38507 case pragma_compound:
38508 cp_parser_omp_flush (parser, pragma_tok);
38509 return false;
38510 case pragma_stmt:
38511 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38512 "used in compound statements", "omp flush");
38513 break;
38514 default:
38515 goto bad_stmt;
38517 break;
38519 case PRAGMA_OMP_TASKWAIT:
38520 switch (context)
38522 case pragma_compound:
38523 cp_parser_omp_taskwait (parser, pragma_tok);
38524 return false;
38525 case pragma_stmt:
38526 error_at (pragma_tok->location,
38527 "%<#pragma %s%> may only be used in compound statements",
38528 "omp taskwait");
38529 break;
38530 default:
38531 goto bad_stmt;
38533 break;
38535 case PRAGMA_OMP_TASKYIELD:
38536 switch (context)
38538 case pragma_compound:
38539 cp_parser_omp_taskyield (parser, pragma_tok);
38540 return false;
38541 case pragma_stmt:
38542 error_at (pragma_tok->location,
38543 "%<#pragma %s%> may only be used in compound statements",
38544 "omp taskyield");
38545 break;
38546 default:
38547 goto bad_stmt;
38549 break;
38551 case PRAGMA_OMP_CANCEL:
38552 switch (context)
38554 case pragma_compound:
38555 cp_parser_omp_cancel (parser, pragma_tok);
38556 return false;
38557 case pragma_stmt:
38558 error_at (pragma_tok->location,
38559 "%<#pragma %s%> may only be used in compound statements",
38560 "omp cancel");
38561 break;
38562 default:
38563 goto bad_stmt;
38565 break;
38567 case PRAGMA_OMP_CANCELLATION_POINT:
38568 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38569 return false;
38571 case PRAGMA_OMP_THREADPRIVATE:
38572 cp_parser_omp_threadprivate (parser, pragma_tok);
38573 return false;
38575 case PRAGMA_OMP_DECLARE:
38576 return cp_parser_omp_declare (parser, pragma_tok, context);
38578 case PRAGMA_OACC_DECLARE:
38579 cp_parser_oacc_declare (parser, pragma_tok);
38580 return false;
38582 case PRAGMA_OACC_ENTER_DATA:
38583 if (context == pragma_stmt)
38585 error_at (pragma_tok->location,
38586 "%<#pragma %s%> may only be used in compound statements",
38587 "acc enter data");
38588 break;
38590 else if (context != pragma_compound)
38591 goto bad_stmt;
38592 cp_parser_omp_construct (parser, pragma_tok, if_p);
38593 return true;
38595 case PRAGMA_OACC_EXIT_DATA:
38596 if (context == pragma_stmt)
38598 error_at (pragma_tok->location,
38599 "%<#pragma %s%> may only be used in compound statements",
38600 "acc exit data");
38601 break;
38603 else if (context != pragma_compound)
38604 goto bad_stmt;
38605 cp_parser_omp_construct (parser, pragma_tok, if_p);
38606 return true;
38608 case PRAGMA_OACC_ROUTINE:
38609 if (context != pragma_external)
38611 error_at (pragma_tok->location,
38612 "%<#pragma acc routine%> must be at file scope");
38613 break;
38615 cp_parser_oacc_routine (parser, pragma_tok, context);
38616 return false;
38618 case PRAGMA_OACC_UPDATE:
38619 if (context == pragma_stmt)
38621 error_at (pragma_tok->location,
38622 "%<#pragma %s%> may only be used in compound statements",
38623 "acc update");
38624 break;
38626 else if (context != pragma_compound)
38627 goto bad_stmt;
38628 cp_parser_omp_construct (parser, pragma_tok, if_p);
38629 return true;
38631 case PRAGMA_OACC_WAIT:
38632 if (context == pragma_stmt)
38634 error_at (pragma_tok->location,
38635 "%<#pragma %s%> may only be used in compound statements",
38636 "acc wait");
38637 break;
38639 else if (context != pragma_compound)
38640 goto bad_stmt;
38641 cp_parser_omp_construct (parser, pragma_tok, if_p);
38642 return true;
38644 case PRAGMA_OACC_ATOMIC:
38645 case PRAGMA_OACC_CACHE:
38646 case PRAGMA_OACC_DATA:
38647 case PRAGMA_OACC_HOST_DATA:
38648 case PRAGMA_OACC_KERNELS:
38649 case PRAGMA_OACC_PARALLEL:
38650 case PRAGMA_OACC_LOOP:
38651 case PRAGMA_OMP_ATOMIC:
38652 case PRAGMA_OMP_CRITICAL:
38653 case PRAGMA_OMP_DISTRIBUTE:
38654 case PRAGMA_OMP_FOR:
38655 case PRAGMA_OMP_MASTER:
38656 case PRAGMA_OMP_PARALLEL:
38657 case PRAGMA_OMP_SECTIONS:
38658 case PRAGMA_OMP_SIMD:
38659 case PRAGMA_OMP_SINGLE:
38660 case PRAGMA_OMP_TASK:
38661 case PRAGMA_OMP_TASKGROUP:
38662 case PRAGMA_OMP_TASKLOOP:
38663 case PRAGMA_OMP_TEAMS:
38664 if (context != pragma_stmt && context != pragma_compound)
38665 goto bad_stmt;
38666 stmt = push_omp_privatization_clauses (false);
38667 cp_parser_omp_construct (parser, pragma_tok, if_p);
38668 pop_omp_privatization_clauses (stmt);
38669 return true;
38671 case PRAGMA_OMP_ORDERED:
38672 if (context != pragma_stmt && context != pragma_compound)
38673 goto bad_stmt;
38674 stmt = push_omp_privatization_clauses (false);
38675 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38676 pop_omp_privatization_clauses (stmt);
38677 return ret;
38679 case PRAGMA_OMP_TARGET:
38680 if (context != pragma_stmt && context != pragma_compound)
38681 goto bad_stmt;
38682 stmt = push_omp_privatization_clauses (false);
38683 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38684 pop_omp_privatization_clauses (stmt);
38685 return ret;
38687 case PRAGMA_OMP_END_DECLARE_TARGET:
38688 cp_parser_omp_end_declare_target (parser, pragma_tok);
38689 return false;
38691 case PRAGMA_OMP_SECTION:
38692 error_at (pragma_tok->location,
38693 "%<#pragma omp section%> may only be used in "
38694 "%<#pragma omp sections%> construct");
38695 break;
38697 case PRAGMA_IVDEP:
38699 if (context == pragma_external)
38701 error_at (pragma_tok->location,
38702 "%<#pragma GCC ivdep%> must be inside a function");
38703 break;
38705 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38706 unsigned short unroll;
38707 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38708 if (tok->type == CPP_PRAGMA
38709 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38711 tok = cp_lexer_consume_token (parser->lexer);
38712 unroll = cp_parser_pragma_unroll (parser, tok);
38713 tok = cp_lexer_peek_token (the_parser->lexer);
38715 else
38716 unroll = 0;
38717 if (tok->type != CPP_KEYWORD
38718 || (tok->keyword != RID_FOR
38719 && tok->keyword != RID_WHILE
38720 && tok->keyword != RID_DO))
38722 cp_parser_error (parser, "for, while or do statement expected");
38723 return false;
38725 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38726 return true;
38729 case PRAGMA_UNROLL:
38731 if (context == pragma_external)
38733 error_at (pragma_tok->location,
38734 "%<#pragma GCC unroll%> must be inside a function");
38735 break;
38737 const unsigned short unroll
38738 = cp_parser_pragma_unroll (parser, pragma_tok);
38739 bool ivdep;
38740 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38741 if (tok->type == CPP_PRAGMA
38742 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
38744 tok = cp_lexer_consume_token (parser->lexer);
38745 ivdep = cp_parser_pragma_ivdep (parser, tok);
38746 tok = cp_lexer_peek_token (the_parser->lexer);
38748 else
38749 ivdep = false;
38750 if (tok->type != CPP_KEYWORD
38751 || (tok->keyword != RID_FOR
38752 && tok->keyword != RID_WHILE
38753 && tok->keyword != RID_DO))
38755 cp_parser_error (parser, "for, while or do statement expected");
38756 return false;
38758 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38759 return true;
38762 default:
38763 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38764 c_invoke_pragma_handler (id);
38765 break;
38767 bad_stmt:
38768 cp_parser_error (parser, "expected declaration specifiers");
38769 break;
38772 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38773 return false;
38776 /* The interface the pragma parsers have to the lexer. */
38778 enum cpp_ttype
38779 pragma_lex (tree *value, location_t *loc)
38781 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38782 enum cpp_ttype ret = tok->type;
38784 *value = tok->u.value;
38785 if (loc)
38786 *loc = tok->location;
38788 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38789 ret = CPP_EOF;
38790 else if (ret == CPP_STRING)
38791 *value = cp_parser_string_literal (the_parser, false, false);
38792 else
38794 if (ret == CPP_KEYWORD)
38795 ret = CPP_NAME;
38796 cp_lexer_consume_token (the_parser->lexer);
38799 return ret;
38803 /* External interface. */
38805 /* Parse one entire translation unit. */
38807 void
38808 c_parse_file (void)
38810 static bool already_called = false;
38812 if (already_called)
38813 fatal_error (input_location,
38814 "inter-module optimizations not implemented for C++");
38815 already_called = true;
38817 the_parser = cp_parser_new ();
38818 push_deferring_access_checks (flag_access_control
38819 ? dk_no_deferred : dk_no_check);
38820 cp_parser_translation_unit (the_parser);
38821 the_parser = NULL;
38824 /* Create an identifier for a generic parameter type (a synthesized
38825 template parameter implied by `auto' or a concept identifier). */
38827 static GTY(()) int generic_parm_count;
38828 static tree
38829 make_generic_type_name ()
38831 char buf[32];
38832 sprintf (buf, "auto:%d", ++generic_parm_count);
38833 return get_identifier (buf);
38836 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38837 (creating a new template parameter list if necessary). Returns the newly
38838 created template type parm. */
38840 static tree
38841 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38843 gcc_assert (current_binding_level->kind == sk_function_parms);
38845 /* Before committing to modifying any scope, if we're in an
38846 implicit template scope, and we're trying to synthesize a
38847 constrained parameter, try to find a previous parameter with
38848 the same name. This is the same-type rule for abbreviated
38849 function templates.
38851 NOTE: We can generate implicit parameters when tentatively
38852 parsing a nested name specifier, only to reject that parse
38853 later. However, matching the same template-id as part of a
38854 direct-declarator should generate an identical template
38855 parameter, so this rule will merge them. */
38856 if (parser->implicit_template_scope && constr)
38858 tree t = parser->implicit_template_parms;
38859 while (t)
38861 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38863 tree d = TREE_VALUE (t);
38864 if (TREE_CODE (d) == PARM_DECL)
38865 /* Return the TEMPLATE_PARM_INDEX. */
38866 d = DECL_INITIAL (d);
38867 return d;
38869 t = TREE_CHAIN (t);
38873 /* We are either continuing a function template that already contains implicit
38874 template parameters, creating a new fully-implicit function template, or
38875 extending an existing explicit function template with implicit template
38876 parameters. */
38878 cp_binding_level *const entry_scope = current_binding_level;
38880 bool become_template = false;
38881 cp_binding_level *parent_scope = 0;
38883 if (parser->implicit_template_scope)
38885 gcc_assert (parser->implicit_template_parms);
38887 current_binding_level = parser->implicit_template_scope;
38889 else
38891 /* Roll back to the existing template parameter scope (in the case of
38892 extending an explicit function template) or introduce a new template
38893 parameter scope ahead of the function parameter scope (or class scope
38894 in the case of out-of-line member definitions). The function scope is
38895 added back after template parameter synthesis below. */
38897 cp_binding_level *scope = entry_scope;
38899 while (scope->kind == sk_function_parms)
38901 parent_scope = scope;
38902 scope = scope->level_chain;
38904 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38906 /* If not defining a class, then any class scope is a scope level in
38907 an out-of-line member definition. In this case simply wind back
38908 beyond the first such scope to inject the template parameter list.
38909 Otherwise wind back to the class being defined. The latter can
38910 occur in class member friend declarations such as:
38912 class A {
38913 void foo (auto);
38915 class B {
38916 friend void A::foo (auto);
38919 The template parameter list synthesized for the friend declaration
38920 must be injected in the scope of 'B'. This can also occur in
38921 erroneous cases such as:
38923 struct A {
38924 struct B {
38925 void foo (auto);
38927 void B::foo (auto) {}
38930 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38931 but, nevertheless, the template parameter list synthesized for the
38932 declarator should be injected into the scope of 'A' as if the
38933 ill-formed template was specified explicitly. */
38935 while (scope->kind == sk_class && !scope->defining_class_p)
38937 parent_scope = scope;
38938 scope = scope->level_chain;
38942 current_binding_level = scope;
38944 if (scope->kind != sk_template_parms
38945 || !function_being_declared_is_template_p (parser))
38947 /* Introduce a new template parameter list for implicit template
38948 parameters. */
38950 become_template = true;
38952 parser->implicit_template_scope
38953 = begin_scope (sk_template_parms, NULL);
38955 ++processing_template_decl;
38957 parser->fully_implicit_function_template_p = true;
38958 ++parser->num_template_parameter_lists;
38960 else
38962 /* Synthesize implicit template parameters at the end of the explicit
38963 template parameter list. */
38965 gcc_assert (current_template_parms);
38967 parser->implicit_template_scope = scope;
38969 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38970 parser->implicit_template_parms
38971 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38975 /* Synthesize a new template parameter and track the current template
38976 parameter chain with implicit_template_parms. */
38978 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38979 tree synth_id = make_generic_type_name ();
38980 tree synth_tmpl_parm;
38981 bool non_type = false;
38983 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38984 synth_tmpl_parm
38985 = finish_template_type_parm (class_type_node, synth_id);
38986 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38987 synth_tmpl_parm
38988 = finish_constrained_template_template_parm (proto, synth_id);
38989 else
38991 synth_tmpl_parm = copy_decl (proto);
38992 DECL_NAME (synth_tmpl_parm) = synth_id;
38993 non_type = true;
38996 // Attach the constraint to the parm before processing.
38997 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38998 TREE_TYPE (node) = constr;
38999 tree new_parm
39000 = process_template_parm (parser->implicit_template_parms,
39001 input_location,
39002 node,
39003 /*non_type=*/non_type,
39004 /*param_pack=*/false);
39006 // Chain the new parameter to the list of implicit parameters.
39007 if (parser->implicit_template_parms)
39008 parser->implicit_template_parms
39009 = TREE_CHAIN (parser->implicit_template_parms);
39010 else
39011 parser->implicit_template_parms = new_parm;
39013 tree new_decl = get_local_decls ();
39014 if (non_type)
39015 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39016 new_decl = DECL_INITIAL (new_decl);
39018 /* If creating a fully implicit function template, start the new implicit
39019 template parameter list with this synthesized type, otherwise grow the
39020 current template parameter list. */
39022 if (become_template)
39024 parent_scope->level_chain = current_binding_level;
39026 tree new_parms = make_tree_vec (1);
39027 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39028 current_template_parms = tree_cons (size_int (processing_template_decl),
39029 new_parms, current_template_parms);
39031 else
39033 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39034 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39035 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39036 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39039 // If the new parameter was constrained, we need to add that to the
39040 // constraints in the template parameter list.
39041 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39043 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39044 reqs = conjoin_constraints (reqs, req);
39045 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39048 current_binding_level = entry_scope;
39050 return new_decl;
39053 /* Finish the declaration of a fully implicit function template. Such a
39054 template has no explicit template parameter list so has not been through the
39055 normal template head and tail processing. synthesize_implicit_template_parm
39056 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39057 provided if the declaration is a class member such that its template
39058 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39059 form is returned. Otherwise NULL_TREE is returned. */
39061 static tree
39062 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39064 gcc_assert (parser->fully_implicit_function_template_p);
39066 if (member_decl_opt && member_decl_opt != error_mark_node
39067 && DECL_VIRTUAL_P (member_decl_opt))
39069 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39070 "implicit templates may not be %<virtual%>");
39071 DECL_VIRTUAL_P (member_decl_opt) = false;
39074 if (member_decl_opt)
39075 member_decl_opt = finish_member_template_decl (member_decl_opt);
39076 end_template_decl ();
39078 parser->fully_implicit_function_template_p = false;
39079 --parser->num_template_parameter_lists;
39081 return member_decl_opt;
39084 /* Helper function for diagnostics that have complained about things
39085 being used with 'extern "C"' linkage.
39087 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39089 void
39090 maybe_show_extern_c_location (void)
39092 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39093 inform (the_parser->innermost_linkage_specification_location,
39094 "%<extern \"C\"%> linkage started here");
39097 #include "gt-cp-parser.h"