Fix compilation failure with C++98 compilers
[official-gcc.git] / gcc / cp / parser.c
blobc46b77608b21fe332565a6ecd5e90b2318948abe
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, "String expressions should be translated "
560 "to execution character set",
561 parser->translate_strings_p);
562 cp_debug_print_flag (file, "Parsing function body outside of a "
563 "local class", parser->in_function_body);
564 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
565 parser->colon_corrects_to_scope_p);
566 cp_debug_print_flag (file, "Colon doesn't start a class definition",
567 parser->colon_doesnt_start_class_def_p);
568 if (parser->type_definition_forbidden_message)
569 fprintf (file, "Error message for forbidden type definitions: %s\n",
570 parser->type_definition_forbidden_message);
571 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
572 fprintf (file, "Number of class definitions in progress: %u\n",
573 parser->num_classes_being_defined);
574 fprintf (file, "Number of template parameter lists for the current "
575 "declaration: %u\n", parser->num_template_parameter_lists);
576 cp_debug_parser_tokens (file, parser, window_size);
577 token = parser->lexer->next_token;
578 fprintf (file, "Next token to parse:\n");
579 fprintf (file, "\tToken: ");
580 cp_lexer_print_token (file, token);
581 eloc = expand_location (token->location);
582 fprintf (file, "\n\tFile: %s\n", eloc.file);
583 fprintf (file, "\tLine: %d\n", eloc.line);
584 fprintf (file, "\tColumn: %d\n", eloc.column);
587 DEBUG_FUNCTION void
588 debug (cp_parser &ref)
590 cp_debug_parser (stderr, &ref);
593 DEBUG_FUNCTION void
594 debug (cp_parser *ptr)
596 if (ptr)
597 debug (*ptr);
598 else
599 fprintf (stderr, "<nil>\n");
602 /* Allocate memory for a new lexer object and return it. */
604 static cp_lexer *
605 cp_lexer_alloc (void)
607 cp_lexer *lexer;
609 c_common_no_more_pch ();
611 /* Allocate the memory. */
612 lexer = ggc_cleared_alloc<cp_lexer> ();
614 /* Initially we are not debugging. */
615 lexer->debugging_p = false;
617 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
619 /* Create the buffer. */
620 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
622 return lexer;
626 /* Create a new main C++ lexer, the lexer that gets tokens from the
627 preprocessor. */
629 static cp_lexer *
630 cp_lexer_new_main (void)
632 cp_lexer *lexer;
633 cp_token token;
635 /* It's possible that parsing the first pragma will load a PCH file,
636 which is a GC collection point. So we have to do that before
637 allocating any memory. */
638 cp_parser_initial_pragma (&token);
640 lexer = cp_lexer_alloc ();
642 /* Put the first token in the buffer. */
643 lexer->buffer->quick_push (token);
645 /* Get the remaining tokens from the preprocessor. */
646 while (token.type != CPP_EOF)
648 cp_lexer_get_preprocessor_token (lexer, &token);
649 vec_safe_push (lexer->buffer, token);
652 lexer->last_token = lexer->buffer->address ()
653 + lexer->buffer->length ()
654 - 1;
655 lexer->next_token = lexer->buffer->length ()
656 ? lexer->buffer->address ()
657 : &eof_token;
659 /* Subsequent preprocessor diagnostics should use compiler
660 diagnostic functions to get the compiler source location. */
661 done_lexing = true;
663 gcc_assert (!lexer->next_token->purged_p);
664 return lexer;
667 /* Create a new lexer whose token stream is primed with the tokens in
668 CACHE. When these tokens are exhausted, no new tokens will be read. */
670 static cp_lexer *
671 cp_lexer_new_from_tokens (cp_token_cache *cache)
673 cp_token *first = cache->first;
674 cp_token *last = cache->last;
675 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
677 /* We do not own the buffer. */
678 lexer->buffer = NULL;
679 lexer->next_token = first == last ? &eof_token : first;
680 lexer->last_token = last;
682 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
684 /* Initially we are not debugging. */
685 lexer->debugging_p = false;
687 gcc_assert (!lexer->next_token->purged_p);
688 return lexer;
691 /* Frees all resources associated with LEXER. */
693 static void
694 cp_lexer_destroy (cp_lexer *lexer)
696 vec_free (lexer->buffer);
697 lexer->saved_tokens.release ();
698 ggc_free (lexer);
701 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
702 be used. The point of this flag is to help the compiler to fold away calls
703 to cp_lexer_debugging_p within this source file at compile time, when the
704 lexer is not being debugged. */
706 #define LEXER_DEBUGGING_ENABLED_P false
708 /* Returns nonzero if debugging information should be output. */
710 static inline bool
711 cp_lexer_debugging_p (cp_lexer *lexer)
713 if (!LEXER_DEBUGGING_ENABLED_P)
714 return false;
716 return lexer->debugging_p;
720 static inline cp_token_position
721 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
723 gcc_assert (!previous_p || lexer->next_token != &eof_token);
725 return lexer->next_token - previous_p;
728 static inline cp_token *
729 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
731 return pos;
734 static inline void
735 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
737 lexer->next_token = cp_lexer_token_at (lexer, pos);
740 static inline cp_token_position
741 cp_lexer_previous_token_position (cp_lexer *lexer)
743 if (lexer->next_token == &eof_token)
744 return lexer->last_token - 1;
745 else
746 return cp_lexer_token_position (lexer, true);
749 static inline cp_token *
750 cp_lexer_previous_token (cp_lexer *lexer)
752 cp_token_position tp = cp_lexer_previous_token_position (lexer);
754 /* Skip past purged tokens. */
755 while (tp->purged_p)
757 gcc_assert (tp != vec_safe_address (lexer->buffer));
758 tp--;
761 return cp_lexer_token_at (lexer, tp);
764 /* nonzero if we are presently saving tokens. */
766 static inline int
767 cp_lexer_saving_tokens (const cp_lexer* lexer)
769 return lexer->saved_tokens.length () != 0;
772 /* Store the next token from the preprocessor in *TOKEN. Return true
773 if we reach EOF. If LEXER is NULL, assume we are handling an
774 initial #pragma pch_preprocess, and thus want the lexer to return
775 processed strings. */
777 static void
778 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
780 static int is_extern_c = 0;
782 /* Get a new token from the preprocessor. */
783 token->type
784 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
785 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
786 token->keyword = RID_MAX;
787 token->purged_p = false;
788 token->error_reported = false;
790 /* On some systems, some header files are surrounded by an
791 implicit extern "C" block. Set a flag in the token if it
792 comes from such a header. */
793 is_extern_c += pending_lang_change;
794 pending_lang_change = 0;
795 token->implicit_extern_c = is_extern_c > 0;
797 /* Check to see if this token is a keyword. */
798 if (token->type == CPP_NAME)
800 if (IDENTIFIER_KEYWORD_P (token->u.value))
802 /* Mark this token as a keyword. */
803 token->type = CPP_KEYWORD;
804 /* Record which keyword. */
805 token->keyword = C_RID_CODE (token->u.value);
807 else
809 if (warn_cxx11_compat
810 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
811 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
813 /* Warn about the C++0x keyword (but still treat it as
814 an identifier). */
815 warning (OPT_Wc__11_compat,
816 "identifier %qE is a keyword in C++11",
817 token->u.value);
819 /* Clear out the C_RID_CODE so we don't warn about this
820 particular identifier-turned-keyword again. */
821 C_SET_RID_CODE (token->u.value, RID_MAX);
824 token->keyword = RID_MAX;
827 else if (token->type == CPP_AT_NAME)
829 /* This only happens in Objective-C++; it must be a keyword. */
830 token->type = CPP_KEYWORD;
831 switch (C_RID_CODE (token->u.value))
833 /* Replace 'class' with '@class', 'private' with '@private',
834 etc. This prevents confusion with the C++ keyword
835 'class', and makes the tokens consistent with other
836 Objective-C 'AT' keywords. For example '@class' is
837 reported as RID_AT_CLASS which is consistent with
838 '@synchronized', which is reported as
839 RID_AT_SYNCHRONIZED.
841 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
842 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
843 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
844 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
845 case RID_THROW: token->keyword = RID_AT_THROW; break;
846 case RID_TRY: token->keyword = RID_AT_TRY; break;
847 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
848 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
849 default: token->keyword = C_RID_CODE (token->u.value);
854 /* Update the globals input_location and the input file stack from TOKEN. */
855 static inline void
856 cp_lexer_set_source_position_from_token (cp_token *token)
858 if (token->type != CPP_EOF)
860 input_location = token->location;
864 /* Update the globals input_location and the input file stack from LEXER. */
865 static inline void
866 cp_lexer_set_source_position (cp_lexer *lexer)
868 cp_token *token = cp_lexer_peek_token (lexer);
869 cp_lexer_set_source_position_from_token (token);
872 /* Return a pointer to the next token in the token stream, but do not
873 consume it. */
875 static inline cp_token *
876 cp_lexer_peek_token (cp_lexer *lexer)
878 if (cp_lexer_debugging_p (lexer))
880 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
881 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
882 putc ('\n', cp_lexer_debug_stream);
884 return lexer->next_token;
887 /* Return true if the next token has the indicated TYPE. */
889 static inline bool
890 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
892 return cp_lexer_peek_token (lexer)->type == type;
895 /* Return true if the next token does not have the indicated TYPE. */
897 static inline bool
898 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
900 return !cp_lexer_next_token_is (lexer, type);
903 /* Return true if the next token is the indicated KEYWORD. */
905 static inline bool
906 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
908 return cp_lexer_peek_token (lexer)->keyword == keyword;
911 static inline bool
912 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
914 return cp_lexer_peek_nth_token (lexer, n)->type == type;
917 static inline bool
918 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
920 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
923 /* Return true if KEYWORD can start a decl-specifier. */
925 bool
926 cp_keyword_starts_decl_specifier_p (enum rid keyword)
928 switch (keyword)
930 /* auto specifier: storage-class-specifier in C++,
931 simple-type-specifier in C++0x. */
932 case RID_AUTO:
933 /* Storage classes. */
934 case RID_REGISTER:
935 case RID_STATIC:
936 case RID_EXTERN:
937 case RID_MUTABLE:
938 case RID_THREAD:
939 /* Elaborated type specifiers. */
940 case RID_ENUM:
941 case RID_CLASS:
942 case RID_STRUCT:
943 case RID_UNION:
944 case RID_TYPENAME:
945 /* Simple type specifiers. */
946 case RID_CHAR:
947 case RID_CHAR16:
948 case RID_CHAR32:
949 case RID_WCHAR:
950 case RID_BOOL:
951 case RID_SHORT:
952 case RID_INT:
953 case RID_LONG:
954 case RID_SIGNED:
955 case RID_UNSIGNED:
956 case RID_FLOAT:
957 case RID_DOUBLE:
958 case RID_VOID:
959 /* GNU extensions. */
960 case RID_ATTRIBUTE:
961 case RID_TYPEOF:
962 /* C++0x extensions. */
963 case RID_DECLTYPE:
964 case RID_UNDERLYING_TYPE:
965 case RID_CONSTEXPR:
966 return true;
968 default:
969 if (keyword >= RID_FIRST_INT_N
970 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
971 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
972 return true;
973 return false;
977 /* Return true if the next token is a keyword for a decl-specifier. */
979 static bool
980 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
982 cp_token *token;
984 token = cp_lexer_peek_token (lexer);
985 return cp_keyword_starts_decl_specifier_p (token->keyword);
988 /* Returns TRUE iff the token T begins a decltype type. */
990 static bool
991 token_is_decltype (cp_token *t)
993 return (t->keyword == RID_DECLTYPE
994 || t->type == CPP_DECLTYPE);
997 /* Returns TRUE iff the next token begins a decltype type. */
999 static bool
1000 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1002 cp_token *t = cp_lexer_peek_token (lexer);
1003 return token_is_decltype (t);
1006 /* Called when processing a token with tree_check_value; perform or defer the
1007 associated checks and return the value. */
1009 static tree
1010 saved_checks_value (struct tree_check *check_value)
1012 /* Perform any access checks that were deferred. */
1013 vec<deferred_access_check, va_gc> *checks;
1014 deferred_access_check *chk;
1015 checks = check_value->checks;
1016 if (checks)
1018 int i;
1019 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1020 perform_or_defer_access_check (chk->binfo,
1021 chk->decl,
1022 chk->diag_decl, tf_warning_or_error);
1024 /* Return the stored value. */
1025 return check_value->value;
1028 /* Return a pointer to the Nth token in the token stream. If N is 1,
1029 then this is precisely equivalent to cp_lexer_peek_token (except
1030 that it is not inline). One would like to disallow that case, but
1031 there is one case (cp_parser_nth_token_starts_template_id) where
1032 the caller passes a variable for N and it might be 1. */
1034 static cp_token *
1035 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1037 cp_token *token;
1039 /* N is 1-based, not zero-based. */
1040 gcc_assert (n > 0);
1042 if (cp_lexer_debugging_p (lexer))
1043 fprintf (cp_lexer_debug_stream,
1044 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1046 --n;
1047 token = lexer->next_token;
1048 gcc_assert (!n || token != &eof_token);
1049 while (n != 0)
1051 ++token;
1052 if (token == lexer->last_token)
1054 token = &eof_token;
1055 break;
1058 if (!token->purged_p)
1059 --n;
1062 if (cp_lexer_debugging_p (lexer))
1064 cp_lexer_print_token (cp_lexer_debug_stream, token);
1065 putc ('\n', cp_lexer_debug_stream);
1068 return token;
1071 /* Return the next token, and advance the lexer's next_token pointer
1072 to point to the next non-purged token. */
1074 static cp_token *
1075 cp_lexer_consume_token (cp_lexer* lexer)
1077 cp_token *token = lexer->next_token;
1079 gcc_assert (token != &eof_token);
1080 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1084 lexer->next_token++;
1085 if (lexer->next_token == lexer->last_token)
1087 lexer->next_token = &eof_token;
1088 break;
1092 while (lexer->next_token->purged_p);
1094 cp_lexer_set_source_position_from_token (token);
1096 /* Provide debugging output. */
1097 if (cp_lexer_debugging_p (lexer))
1099 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1100 cp_lexer_print_token (cp_lexer_debug_stream, token);
1101 putc ('\n', cp_lexer_debug_stream);
1104 return token;
1107 /* Permanently remove the next token from the token stream, and
1108 advance the next_token pointer to refer to the next non-purged
1109 token. */
1111 static void
1112 cp_lexer_purge_token (cp_lexer *lexer)
1114 cp_token *tok = lexer->next_token;
1116 gcc_assert (tok != &eof_token);
1117 tok->purged_p = true;
1118 tok->location = UNKNOWN_LOCATION;
1119 tok->u.value = NULL_TREE;
1120 tok->keyword = RID_MAX;
1124 tok++;
1125 if (tok == lexer->last_token)
1127 tok = &eof_token;
1128 break;
1131 while (tok->purged_p);
1132 lexer->next_token = tok;
1135 /* Permanently remove all tokens after TOK, up to, but not
1136 including, the token that will be returned next by
1137 cp_lexer_peek_token. */
1139 static void
1140 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1142 cp_token *peek = lexer->next_token;
1144 if (peek == &eof_token)
1145 peek = lexer->last_token;
1147 gcc_assert (tok < peek);
1149 for ( tok += 1; tok != peek; tok += 1)
1151 tok->purged_p = true;
1152 tok->location = UNKNOWN_LOCATION;
1153 tok->u.value = NULL_TREE;
1154 tok->keyword = RID_MAX;
1158 /* Begin saving tokens. All tokens consumed after this point will be
1159 preserved. */
1161 static void
1162 cp_lexer_save_tokens (cp_lexer* lexer)
1164 /* Provide debugging output. */
1165 if (cp_lexer_debugging_p (lexer))
1166 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1168 lexer->saved_tokens.safe_push (lexer->next_token);
1171 /* Commit to the portion of the token stream most recently saved. */
1173 static void
1174 cp_lexer_commit_tokens (cp_lexer* lexer)
1176 /* Provide debugging output. */
1177 if (cp_lexer_debugging_p (lexer))
1178 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1180 lexer->saved_tokens.pop ();
1183 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1184 to the token stream. Stop saving tokens. */
1186 static void
1187 cp_lexer_rollback_tokens (cp_lexer* lexer)
1189 /* Provide debugging output. */
1190 if (cp_lexer_debugging_p (lexer))
1191 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1193 lexer->next_token = lexer->saved_tokens.pop ();
1196 /* RAII wrapper around the above functions, with sanity checking. Creating
1197 a variable saves tokens, which are committed when the variable is
1198 destroyed unless they are explicitly rolled back by calling the rollback
1199 member function. */
1201 struct saved_token_sentinel
1203 cp_lexer *lexer;
1204 unsigned len;
1205 bool commit;
1206 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1208 len = lexer->saved_tokens.length ();
1209 cp_lexer_save_tokens (lexer);
1211 void rollback ()
1213 cp_lexer_rollback_tokens (lexer);
1214 commit = false;
1216 ~saved_token_sentinel()
1218 if (commit)
1219 cp_lexer_commit_tokens (lexer);
1220 gcc_assert (lexer->saved_tokens.length () == len);
1224 /* Print a representation of the TOKEN on the STREAM. */
1226 static void
1227 cp_lexer_print_token (FILE * stream, cp_token *token)
1229 /* We don't use cpp_type2name here because the parser defines
1230 a few tokens of its own. */
1231 static const char *const token_names[] = {
1232 /* cpplib-defined token types */
1233 #define OP(e, s) #e,
1234 #define TK(e, s) #e,
1235 TTYPE_TABLE
1236 #undef OP
1237 #undef TK
1238 /* C++ parser token types - see "Manifest constants", above. */
1239 "KEYWORD",
1240 "TEMPLATE_ID",
1241 "NESTED_NAME_SPECIFIER",
1244 /* For some tokens, print the associated data. */
1245 switch (token->type)
1247 case CPP_KEYWORD:
1248 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1249 For example, `struct' is mapped to an INTEGER_CST. */
1250 if (!identifier_p (token->u.value))
1251 break;
1252 /* fall through */
1253 case CPP_NAME:
1254 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1255 break;
1257 case CPP_STRING:
1258 case CPP_STRING16:
1259 case CPP_STRING32:
1260 case CPP_WSTRING:
1261 case CPP_UTF8STRING:
1262 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1263 break;
1265 case CPP_NUMBER:
1266 print_generic_expr (stream, token->u.value);
1267 break;
1269 default:
1270 /* If we have a name for the token, print it out. Otherwise, we
1271 simply give the numeric code. */
1272 if (token->type < ARRAY_SIZE(token_names))
1273 fputs (token_names[token->type], stream);
1274 else
1275 fprintf (stream, "[%d]", token->type);
1276 break;
1280 DEBUG_FUNCTION void
1281 debug (cp_token &ref)
1283 cp_lexer_print_token (stderr, &ref);
1284 fprintf (stderr, "\n");
1287 DEBUG_FUNCTION void
1288 debug (cp_token *ptr)
1290 if (ptr)
1291 debug (*ptr);
1292 else
1293 fprintf (stderr, "<nil>\n");
1297 /* Start emitting debugging information. */
1299 static void
1300 cp_lexer_start_debugging (cp_lexer* lexer)
1302 if (!LEXER_DEBUGGING_ENABLED_P)
1303 fatal_error (input_location,
1304 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1306 lexer->debugging_p = true;
1307 cp_lexer_debug_stream = stderr;
1310 /* Stop emitting debugging information. */
1312 static void
1313 cp_lexer_stop_debugging (cp_lexer* lexer)
1315 if (!LEXER_DEBUGGING_ENABLED_P)
1316 fatal_error (input_location,
1317 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1319 lexer->debugging_p = false;
1320 cp_lexer_debug_stream = NULL;
1323 /* Create a new cp_token_cache, representing a range of tokens. */
1325 static cp_token_cache *
1326 cp_token_cache_new (cp_token *first, cp_token *last)
1328 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1329 cache->first = first;
1330 cache->last = last;
1331 return cache;
1334 /* Diagnose if #pragma omp declare simd isn't followed immediately
1335 by function declaration or definition. */
1337 static inline void
1338 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1340 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1342 error ("%<#pragma omp declare simd%> not immediately followed by "
1343 "function declaration or definition");
1344 parser->omp_declare_simd = NULL;
1348 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1349 and put that into "omp declare simd" attribute. */
1351 static inline void
1352 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1354 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1356 if (fndecl == error_mark_node)
1358 parser->omp_declare_simd = NULL;
1359 return;
1361 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1363 cp_ensure_no_omp_declare_simd (parser);
1364 return;
1369 /* Diagnose if #pragma acc routine isn't followed immediately by function
1370 declaration or definition. */
1372 static inline void
1373 cp_ensure_no_oacc_routine (cp_parser *parser)
1375 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1377 error_at (parser->oacc_routine->loc,
1378 "%<#pragma acc routine%> not immediately followed by "
1379 "function declaration or definition");
1380 parser->oacc_routine = NULL;
1384 /* Decl-specifiers. */
1386 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1388 static void
1389 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1391 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1394 /* Declarators. */
1396 /* Nothing other than the parser should be creating declarators;
1397 declarators are a semi-syntactic representation of C++ entities.
1398 Other parts of the front end that need to create entities (like
1399 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1401 static cp_declarator *make_call_declarator
1402 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1403 static cp_declarator *make_array_declarator
1404 (cp_declarator *, tree);
1405 static cp_declarator *make_pointer_declarator
1406 (cp_cv_quals, cp_declarator *, tree);
1407 static cp_declarator *make_reference_declarator
1408 (cp_cv_quals, cp_declarator *, bool, tree);
1409 static cp_declarator *make_ptrmem_declarator
1410 (cp_cv_quals, tree, cp_declarator *, tree);
1412 /* An erroneous declarator. */
1413 static cp_declarator *cp_error_declarator;
1415 /* The obstack on which declarators and related data structures are
1416 allocated. */
1417 static struct obstack declarator_obstack;
1419 /* Alloc BYTES from the declarator memory pool. */
1421 static inline void *
1422 alloc_declarator (size_t bytes)
1424 return obstack_alloc (&declarator_obstack, bytes);
1427 /* Allocate a declarator of the indicated KIND. Clear fields that are
1428 common to all declarators. */
1430 static cp_declarator *
1431 make_declarator (cp_declarator_kind kind)
1433 cp_declarator *declarator;
1435 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1436 declarator->kind = kind;
1437 declarator->parenthesized = UNKNOWN_LOCATION;
1438 declarator->attributes = NULL_TREE;
1439 declarator->std_attributes = NULL_TREE;
1440 declarator->declarator = NULL;
1441 declarator->parameter_pack_p = false;
1442 declarator->id_loc = UNKNOWN_LOCATION;
1444 return declarator;
1447 /* Make a declarator for a generalized identifier. If
1448 QUALIFYING_SCOPE is non-NULL, the identifier is
1449 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1450 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1451 is, if any. */
1453 static cp_declarator *
1454 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1455 special_function_kind sfk)
1457 cp_declarator *declarator;
1459 /* It is valid to write:
1461 class C { void f(); };
1462 typedef C D;
1463 void D::f();
1465 The standard is not clear about whether `typedef const C D' is
1466 legal; as of 2002-09-15 the committee is considering that
1467 question. EDG 3.0 allows that syntax. Therefore, we do as
1468 well. */
1469 if (qualifying_scope && TYPE_P (qualifying_scope))
1470 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1472 gcc_assert (identifier_p (unqualified_name)
1473 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1474 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1476 declarator = make_declarator (cdk_id);
1477 declarator->u.id.qualifying_scope = qualifying_scope;
1478 declarator->u.id.unqualified_name = unqualified_name;
1479 declarator->u.id.sfk = sfk;
1481 return declarator;
1484 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1485 of modifiers such as const or volatile to apply to the pointer
1486 type, represented as identifiers. ATTRIBUTES represent the attributes that
1487 appertain to the pointer or reference. */
1489 cp_declarator *
1490 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1491 tree attributes)
1493 cp_declarator *declarator;
1495 declarator = make_declarator (cdk_pointer);
1496 declarator->declarator = target;
1497 declarator->u.pointer.qualifiers = cv_qualifiers;
1498 declarator->u.pointer.class_type = NULL_TREE;
1499 if (target)
1501 declarator->id_loc = target->id_loc;
1502 declarator->parameter_pack_p = target->parameter_pack_p;
1503 target->parameter_pack_p = false;
1505 else
1506 declarator->parameter_pack_p = false;
1508 declarator->std_attributes = attributes;
1510 return declarator;
1513 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1514 represent the attributes that appertain to the pointer or
1515 reference. */
1517 cp_declarator *
1518 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1519 bool rvalue_ref, tree attributes)
1521 cp_declarator *declarator;
1523 declarator = make_declarator (cdk_reference);
1524 declarator->declarator = target;
1525 declarator->u.reference.qualifiers = cv_qualifiers;
1526 declarator->u.reference.rvalue_ref = rvalue_ref;
1527 if (target)
1529 declarator->id_loc = target->id_loc;
1530 declarator->parameter_pack_p = target->parameter_pack_p;
1531 target->parameter_pack_p = false;
1533 else
1534 declarator->parameter_pack_p = false;
1536 declarator->std_attributes = attributes;
1538 return declarator;
1541 /* Like make_pointer_declarator -- but for a pointer to a non-static
1542 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1543 appertain to the pointer or reference. */
1545 cp_declarator *
1546 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1547 cp_declarator *pointee,
1548 tree attributes)
1550 cp_declarator *declarator;
1552 declarator = make_declarator (cdk_ptrmem);
1553 declarator->declarator = pointee;
1554 declarator->u.pointer.qualifiers = cv_qualifiers;
1555 declarator->u.pointer.class_type = class_type;
1557 if (pointee)
1559 declarator->parameter_pack_p = pointee->parameter_pack_p;
1560 pointee->parameter_pack_p = false;
1562 else
1563 declarator->parameter_pack_p = false;
1565 declarator->std_attributes = attributes;
1567 return declarator;
1570 /* Make a declarator for the function given by TARGET, with the
1571 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1572 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1573 indicates what exceptions can be thrown. */
1575 cp_declarator *
1576 make_call_declarator (cp_declarator *target,
1577 tree parms,
1578 cp_cv_quals cv_qualifiers,
1579 cp_virt_specifiers virt_specifiers,
1580 cp_ref_qualifier ref_qualifier,
1581 tree tx_qualifier,
1582 tree exception_specification,
1583 tree late_return_type,
1584 tree requires_clause)
1586 cp_declarator *declarator;
1588 declarator = make_declarator (cdk_function);
1589 declarator->declarator = target;
1590 declarator->u.function.parameters = parms;
1591 declarator->u.function.qualifiers = cv_qualifiers;
1592 declarator->u.function.virt_specifiers = virt_specifiers;
1593 declarator->u.function.ref_qualifier = ref_qualifier;
1594 declarator->u.function.tx_qualifier = tx_qualifier;
1595 declarator->u.function.exception_specification = exception_specification;
1596 declarator->u.function.late_return_type = late_return_type;
1597 declarator->u.function.requires_clause = requires_clause;
1598 if (target)
1600 declarator->id_loc = target->id_loc;
1601 declarator->parameter_pack_p = target->parameter_pack_p;
1602 target->parameter_pack_p = false;
1604 else
1605 declarator->parameter_pack_p = false;
1607 return declarator;
1610 /* Make a declarator for an array of BOUNDS elements, each of which is
1611 defined by ELEMENT. */
1613 cp_declarator *
1614 make_array_declarator (cp_declarator *element, tree bounds)
1616 cp_declarator *declarator;
1618 declarator = make_declarator (cdk_array);
1619 declarator->declarator = element;
1620 declarator->u.array.bounds = bounds;
1621 if (element)
1623 declarator->id_loc = element->id_loc;
1624 declarator->parameter_pack_p = element->parameter_pack_p;
1625 element->parameter_pack_p = false;
1627 else
1628 declarator->parameter_pack_p = false;
1630 return declarator;
1633 /* Determine whether the declarator we've seen so far can be a
1634 parameter pack, when followed by an ellipsis. */
1635 static bool
1636 declarator_can_be_parameter_pack (cp_declarator *declarator)
1638 if (declarator && declarator->parameter_pack_p)
1639 /* We already saw an ellipsis. */
1640 return false;
1642 /* Search for a declarator name, or any other declarator that goes
1643 after the point where the ellipsis could appear in a parameter
1644 pack. If we find any of these, then this declarator can not be
1645 made into a parameter pack. */
1646 bool found = false;
1647 while (declarator && !found)
1649 switch ((int)declarator->kind)
1651 case cdk_id:
1652 case cdk_array:
1653 case cdk_decomp:
1654 found = true;
1655 break;
1657 case cdk_error:
1658 return true;
1660 default:
1661 declarator = declarator->declarator;
1662 break;
1666 return !found;
1669 cp_parameter_declarator *no_parameters;
1671 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1672 DECLARATOR and DEFAULT_ARGUMENT. */
1674 cp_parameter_declarator *
1675 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1676 cp_declarator *declarator,
1677 tree default_argument,
1678 location_t loc,
1679 bool template_parameter_pack_p = false)
1681 cp_parameter_declarator *parameter;
1683 parameter = ((cp_parameter_declarator *)
1684 alloc_declarator (sizeof (cp_parameter_declarator)));
1685 parameter->next = NULL;
1686 if (decl_specifiers)
1687 parameter->decl_specifiers = *decl_specifiers;
1688 else
1689 clear_decl_specs (&parameter->decl_specifiers);
1690 parameter->declarator = declarator;
1691 parameter->default_argument = default_argument;
1692 parameter->template_parameter_pack_p = template_parameter_pack_p;
1693 parameter->loc = loc;
1695 return parameter;
1698 /* Returns true iff DECLARATOR is a declaration for a function. */
1700 static bool
1701 function_declarator_p (const cp_declarator *declarator)
1703 while (declarator)
1705 if (declarator->kind == cdk_function
1706 && declarator->declarator->kind == cdk_id)
1707 return true;
1708 if (declarator->kind == cdk_id
1709 || declarator->kind == cdk_decomp
1710 || declarator->kind == cdk_error)
1711 return false;
1712 declarator = declarator->declarator;
1714 return false;
1717 /* The parser. */
1719 /* Overview
1720 --------
1722 A cp_parser parses the token stream as specified by the C++
1723 grammar. Its job is purely parsing, not semantic analysis. For
1724 example, the parser breaks the token stream into declarators,
1725 expressions, statements, and other similar syntactic constructs.
1726 It does not check that the types of the expressions on either side
1727 of an assignment-statement are compatible, or that a function is
1728 not declared with a parameter of type `void'.
1730 The parser invokes routines elsewhere in the compiler to perform
1731 semantic analysis and to build up the abstract syntax tree for the
1732 code processed.
1734 The parser (and the template instantiation code, which is, in a
1735 way, a close relative of parsing) are the only parts of the
1736 compiler that should be calling push_scope and pop_scope, or
1737 related functions. The parser (and template instantiation code)
1738 keeps track of what scope is presently active; everything else
1739 should simply honor that. (The code that generates static
1740 initializers may also need to set the scope, in order to check
1741 access control correctly when emitting the initializers.)
1743 Methodology
1744 -----------
1746 The parser is of the standard recursive-descent variety. Upcoming
1747 tokens in the token stream are examined in order to determine which
1748 production to use when parsing a non-terminal. Some C++ constructs
1749 require arbitrary look ahead to disambiguate. For example, it is
1750 impossible, in the general case, to tell whether a statement is an
1751 expression or declaration without scanning the entire statement.
1752 Therefore, the parser is capable of "parsing tentatively." When the
1753 parser is not sure what construct comes next, it enters this mode.
1754 Then, while we attempt to parse the construct, the parser queues up
1755 error messages, rather than issuing them immediately, and saves the
1756 tokens it consumes. If the construct is parsed successfully, the
1757 parser "commits", i.e., it issues any queued error messages and
1758 the tokens that were being preserved are permanently discarded.
1759 If, however, the construct is not parsed successfully, the parser
1760 rolls back its state completely so that it can resume parsing using
1761 a different alternative.
1763 Future Improvements
1764 -------------------
1766 The performance of the parser could probably be improved substantially.
1767 We could often eliminate the need to parse tentatively by looking ahead
1768 a little bit. In some places, this approach might not entirely eliminate
1769 the need to parse tentatively, but it might still speed up the average
1770 case. */
1772 /* Flags that are passed to some parsing functions. These values can
1773 be bitwise-ored together. */
1775 enum
1777 /* No flags. */
1778 CP_PARSER_FLAGS_NONE = 0x0,
1779 /* The construct is optional. If it is not present, then no error
1780 should be issued. */
1781 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1782 /* When parsing a type-specifier, treat user-defined type-names
1783 as non-type identifiers. */
1784 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1785 /* When parsing a type-specifier, do not try to parse a class-specifier
1786 or enum-specifier. */
1787 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1788 /* When parsing a decl-specifier-seq, only allow type-specifier or
1789 constexpr. */
1790 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1791 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1792 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1795 /* This type is used for parameters and variables which hold
1796 combinations of the above flags. */
1797 typedef int cp_parser_flags;
1799 /* The different kinds of declarators we want to parse. */
1801 enum cp_parser_declarator_kind
1803 /* We want an abstract declarator. */
1804 CP_PARSER_DECLARATOR_ABSTRACT,
1805 /* We want a named declarator. */
1806 CP_PARSER_DECLARATOR_NAMED,
1807 /* We don't mind, but the name must be an unqualified-id. */
1808 CP_PARSER_DECLARATOR_EITHER
1811 /* The precedence values used to parse binary expressions. The minimum value
1812 of PREC must be 1, because zero is reserved to quickly discriminate
1813 binary operators from other tokens. */
1815 enum cp_parser_prec
1817 PREC_NOT_OPERATOR,
1818 PREC_LOGICAL_OR_EXPRESSION,
1819 PREC_LOGICAL_AND_EXPRESSION,
1820 PREC_INCLUSIVE_OR_EXPRESSION,
1821 PREC_EXCLUSIVE_OR_EXPRESSION,
1822 PREC_AND_EXPRESSION,
1823 PREC_EQUALITY_EXPRESSION,
1824 PREC_RELATIONAL_EXPRESSION,
1825 PREC_SHIFT_EXPRESSION,
1826 PREC_ADDITIVE_EXPRESSION,
1827 PREC_MULTIPLICATIVE_EXPRESSION,
1828 PREC_PM_EXPRESSION,
1829 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1832 /* A mapping from a token type to a corresponding tree node type, with a
1833 precedence value. */
1835 struct cp_parser_binary_operations_map_node
1837 /* The token type. */
1838 enum cpp_ttype token_type;
1839 /* The corresponding tree code. */
1840 enum tree_code tree_type;
1841 /* The precedence of this operator. */
1842 enum cp_parser_prec prec;
1845 struct cp_parser_expression_stack_entry
1847 /* Left hand side of the binary operation we are currently
1848 parsing. */
1849 cp_expr lhs;
1850 /* Original tree code for left hand side, if it was a binary
1851 expression itself (used for -Wparentheses). */
1852 enum tree_code lhs_type;
1853 /* Tree code for the binary operation we are parsing. */
1854 enum tree_code tree_type;
1855 /* Precedence of the binary operation we are parsing. */
1856 enum cp_parser_prec prec;
1857 /* Location of the binary operation we are parsing. */
1858 location_t loc;
1861 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1862 entries because precedence levels on the stack are monotonically
1863 increasing. */
1864 typedef struct cp_parser_expression_stack_entry
1865 cp_parser_expression_stack[NUM_PREC_VALUES];
1867 /* Prototypes. */
1869 /* Constructors and destructors. */
1871 static cp_parser_context *cp_parser_context_new
1872 (cp_parser_context *);
1874 /* Class variables. */
1876 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1878 /* The operator-precedence table used by cp_parser_binary_expression.
1879 Transformed into an associative array (binops_by_token) by
1880 cp_parser_new. */
1882 static const cp_parser_binary_operations_map_node binops[] = {
1883 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1884 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1886 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1887 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1888 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1891 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1893 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1894 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1896 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1897 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1898 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1899 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1901 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1902 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1904 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1906 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1908 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1910 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1912 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1915 /* The same as binops, but initialized by cp_parser_new so that
1916 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1917 for speed. */
1918 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1920 /* Constructors and destructors. */
1922 /* Construct a new context. The context below this one on the stack
1923 is given by NEXT. */
1925 static cp_parser_context *
1926 cp_parser_context_new (cp_parser_context* next)
1928 cp_parser_context *context;
1930 /* Allocate the storage. */
1931 if (cp_parser_context_free_list != NULL)
1933 /* Pull the first entry from the free list. */
1934 context = cp_parser_context_free_list;
1935 cp_parser_context_free_list = context->next;
1936 memset (context, 0, sizeof (*context));
1938 else
1939 context = ggc_cleared_alloc<cp_parser_context> ();
1941 /* No errors have occurred yet in this context. */
1942 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1943 /* If this is not the bottommost context, copy information that we
1944 need from the previous context. */
1945 if (next)
1947 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1948 expression, then we are parsing one in this context, too. */
1949 context->object_type = next->object_type;
1950 /* Thread the stack. */
1951 context->next = next;
1954 return context;
1957 /* Managing the unparsed function queues. */
1959 #define unparsed_funs_with_default_args \
1960 parser->unparsed_queues->last ().funs_with_default_args
1961 #define unparsed_funs_with_definitions \
1962 parser->unparsed_queues->last ().funs_with_definitions
1963 #define unparsed_nsdmis \
1964 parser->unparsed_queues->last ().nsdmis
1965 #define unparsed_classes \
1966 parser->unparsed_queues->last ().classes
1968 static void
1969 push_unparsed_function_queues (cp_parser *parser)
1971 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1972 vec_safe_push (parser->unparsed_queues, e);
1975 static void
1976 pop_unparsed_function_queues (cp_parser *parser)
1978 release_tree_vector (unparsed_funs_with_definitions);
1979 parser->unparsed_queues->pop ();
1982 /* Prototypes. */
1984 /* Constructors and destructors. */
1986 static cp_parser *cp_parser_new
1987 (void);
1989 /* Routines to parse various constructs.
1991 Those that return `tree' will return the error_mark_node (rather
1992 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1993 Sometimes, they will return an ordinary node if error-recovery was
1994 attempted, even though a parse error occurred. So, to check
1995 whether or not a parse error occurred, you should always use
1996 cp_parser_error_occurred. If the construct is optional (indicated
1997 either by an `_opt' in the name of the function that does the
1998 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1999 the construct is not present. */
2001 /* Lexical conventions [gram.lex] */
2003 static cp_expr cp_parser_identifier
2004 (cp_parser *);
2005 static cp_expr cp_parser_string_literal
2006 (cp_parser *, bool, bool, bool);
2007 static cp_expr cp_parser_userdef_char_literal
2008 (cp_parser *);
2009 static tree cp_parser_userdef_string_literal
2010 (tree);
2011 static cp_expr cp_parser_userdef_numeric_literal
2012 (cp_parser *);
2014 /* Basic concepts [gram.basic] */
2016 static void cp_parser_translation_unit (cp_parser *);
2018 /* Expressions [gram.expr] */
2020 static cp_expr cp_parser_primary_expression
2021 (cp_parser *, bool, bool, bool, cp_id_kind *);
2022 static cp_expr cp_parser_id_expression
2023 (cp_parser *, bool, bool, bool *, bool, bool);
2024 static cp_expr cp_parser_unqualified_id
2025 (cp_parser *, bool, bool, bool, bool);
2026 static tree cp_parser_nested_name_specifier_opt
2027 (cp_parser *, bool, bool, bool, bool, bool = false);
2028 static tree cp_parser_nested_name_specifier
2029 (cp_parser *, bool, bool, bool, bool);
2030 static tree cp_parser_qualifying_entity
2031 (cp_parser *, bool, bool, bool, bool, bool);
2032 static cp_expr cp_parser_postfix_expression
2033 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2034 static tree cp_parser_postfix_open_square_expression
2035 (cp_parser *, tree, bool, bool);
2036 static tree cp_parser_postfix_dot_deref_expression
2037 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2038 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2039 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2040 bool = false);
2041 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2042 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2043 static void cp_parser_pseudo_destructor_name
2044 (cp_parser *, tree, tree *, tree *);
2045 static cp_expr cp_parser_unary_expression
2046 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2047 static enum tree_code cp_parser_unary_operator
2048 (cp_token *);
2049 static tree cp_parser_new_expression
2050 (cp_parser *);
2051 static vec<tree, va_gc> *cp_parser_new_placement
2052 (cp_parser *);
2053 static tree cp_parser_new_type_id
2054 (cp_parser *, tree *);
2055 static cp_declarator *cp_parser_new_declarator_opt
2056 (cp_parser *);
2057 static cp_declarator *cp_parser_direct_new_declarator
2058 (cp_parser *);
2059 static vec<tree, va_gc> *cp_parser_new_initializer
2060 (cp_parser *);
2061 static tree cp_parser_delete_expression
2062 (cp_parser *);
2063 static cp_expr cp_parser_cast_expression
2064 (cp_parser *, bool, bool, bool, cp_id_kind *);
2065 static cp_expr cp_parser_binary_expression
2066 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2067 static tree cp_parser_question_colon_clause
2068 (cp_parser *, cp_expr);
2069 static cp_expr cp_parser_assignment_expression
2070 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2071 static enum tree_code cp_parser_assignment_operator_opt
2072 (cp_parser *);
2073 static cp_expr cp_parser_expression
2074 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2075 static cp_expr cp_parser_constant_expression
2076 (cp_parser *, bool = false, bool * = NULL, bool = false);
2077 static cp_expr cp_parser_builtin_offsetof
2078 (cp_parser *);
2079 static cp_expr cp_parser_lambda_expression
2080 (cp_parser *);
2081 static void cp_parser_lambda_introducer
2082 (cp_parser *, tree);
2083 static bool cp_parser_lambda_declarator_opt
2084 (cp_parser *, tree);
2085 static void cp_parser_lambda_body
2086 (cp_parser *, tree);
2088 /* Statements [gram.stmt.stmt] */
2090 static void cp_parser_statement
2091 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2092 static void cp_parser_label_for_labeled_statement
2093 (cp_parser *, tree);
2094 static tree cp_parser_expression_statement
2095 (cp_parser *, tree);
2096 static tree cp_parser_compound_statement
2097 (cp_parser *, tree, int, bool);
2098 static void cp_parser_statement_seq_opt
2099 (cp_parser *, tree);
2100 static tree cp_parser_selection_statement
2101 (cp_parser *, bool *, vec<tree> *);
2102 static tree cp_parser_condition
2103 (cp_parser *);
2104 static tree cp_parser_iteration_statement
2105 (cp_parser *, bool *, bool, unsigned short);
2106 static bool cp_parser_init_statement
2107 (cp_parser *, tree *decl);
2108 static tree cp_parser_for
2109 (cp_parser *, bool, unsigned short);
2110 static tree cp_parser_c_for
2111 (cp_parser *, tree, tree, bool, unsigned short);
2112 static tree cp_parser_range_for
2113 (cp_parser *, tree, tree, tree, bool, unsigned short);
2114 static void do_range_for_auto_deduction
2115 (tree, tree);
2116 static tree cp_parser_perform_range_for_lookup
2117 (tree, tree *, tree *);
2118 static tree cp_parser_range_for_member_function
2119 (tree, tree);
2120 static tree cp_parser_jump_statement
2121 (cp_parser *);
2122 static void cp_parser_declaration_statement
2123 (cp_parser *);
2125 static tree cp_parser_implicitly_scoped_statement
2126 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2127 static void cp_parser_already_scoped_statement
2128 (cp_parser *, bool *, const token_indent_info &);
2130 /* Declarations [gram.dcl.dcl] */
2132 static void cp_parser_declaration_seq_opt
2133 (cp_parser *);
2134 static void cp_parser_declaration
2135 (cp_parser *);
2136 static void cp_parser_toplevel_declaration
2137 (cp_parser *);
2138 static void cp_parser_block_declaration
2139 (cp_parser *, bool);
2140 static void cp_parser_simple_declaration
2141 (cp_parser *, bool, tree *);
2142 static void cp_parser_decl_specifier_seq
2143 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2144 static tree cp_parser_storage_class_specifier_opt
2145 (cp_parser *);
2146 static tree cp_parser_function_specifier_opt
2147 (cp_parser *, cp_decl_specifier_seq *);
2148 static tree cp_parser_type_specifier
2149 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2150 int *, bool *);
2151 static tree cp_parser_simple_type_specifier
2152 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2153 static tree cp_parser_type_name
2154 (cp_parser *, bool);
2155 static tree cp_parser_type_name
2156 (cp_parser *);
2157 static tree cp_parser_nonclass_name
2158 (cp_parser* parser);
2159 static tree cp_parser_elaborated_type_specifier
2160 (cp_parser *, bool, bool);
2161 static tree cp_parser_enum_specifier
2162 (cp_parser *);
2163 static void cp_parser_enumerator_list
2164 (cp_parser *, tree);
2165 static void cp_parser_enumerator_definition
2166 (cp_parser *, tree);
2167 static tree cp_parser_namespace_name
2168 (cp_parser *);
2169 static void cp_parser_namespace_definition
2170 (cp_parser *);
2171 static void cp_parser_namespace_body
2172 (cp_parser *);
2173 static tree cp_parser_qualified_namespace_specifier
2174 (cp_parser *);
2175 static void cp_parser_namespace_alias_definition
2176 (cp_parser *);
2177 static bool cp_parser_using_declaration
2178 (cp_parser *, bool);
2179 static void cp_parser_using_directive
2180 (cp_parser *);
2181 static tree cp_parser_alias_declaration
2182 (cp_parser *);
2183 static void cp_parser_asm_definition
2184 (cp_parser *);
2185 static void cp_parser_linkage_specification
2186 (cp_parser *);
2187 static void cp_parser_static_assert
2188 (cp_parser *, bool);
2189 static tree cp_parser_decltype
2190 (cp_parser *);
2191 static tree cp_parser_decomposition_declaration
2192 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2194 /* Declarators [gram.dcl.decl] */
2196 static tree cp_parser_init_declarator
2197 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2198 bool, bool, int, bool *, tree *, location_t *, tree *);
2199 static cp_declarator *cp_parser_declarator
2200 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2201 static cp_declarator *cp_parser_direct_declarator
2202 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2203 static enum tree_code cp_parser_ptr_operator
2204 (cp_parser *, tree *, cp_cv_quals *, tree *);
2205 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2206 (cp_parser *);
2207 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2208 (cp_parser *);
2209 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2210 (cp_parser *);
2211 static tree cp_parser_tx_qualifier_opt
2212 (cp_parser *);
2213 static tree cp_parser_late_return_type_opt
2214 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2215 static tree cp_parser_declarator_id
2216 (cp_parser *, bool);
2217 static tree cp_parser_type_id
2218 (cp_parser *, location_t * = NULL);
2219 static tree cp_parser_template_type_arg
2220 (cp_parser *);
2221 static tree cp_parser_trailing_type_id (cp_parser *);
2222 static tree cp_parser_type_id_1
2223 (cp_parser *, bool, bool, location_t *);
2224 static void cp_parser_type_specifier_seq
2225 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2226 static tree cp_parser_parameter_declaration_clause
2227 (cp_parser *);
2228 static tree cp_parser_parameter_declaration_list
2229 (cp_parser *);
2230 static cp_parameter_declarator *cp_parser_parameter_declaration
2231 (cp_parser *, bool, bool *);
2232 static tree cp_parser_default_argument
2233 (cp_parser *, bool);
2234 static void cp_parser_function_body
2235 (cp_parser *, bool);
2236 static tree cp_parser_initializer
2237 (cp_parser *, bool *, bool *, bool = false);
2238 static cp_expr cp_parser_initializer_clause
2239 (cp_parser *, bool *);
2240 static cp_expr cp_parser_braced_list
2241 (cp_parser*, bool*);
2242 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2243 (cp_parser *, bool *);
2245 static void cp_parser_ctor_initializer_opt_and_function_body
2246 (cp_parser *, bool);
2248 static tree cp_parser_late_parsing_omp_declare_simd
2249 (cp_parser *, tree);
2251 static tree cp_parser_late_parsing_oacc_routine
2252 (cp_parser *, tree);
2254 static tree synthesize_implicit_template_parm
2255 (cp_parser *, tree);
2256 static tree finish_fully_implicit_template
2257 (cp_parser *, tree);
2258 static void abort_fully_implicit_template
2259 (cp_parser *);
2261 /* Classes [gram.class] */
2263 static tree cp_parser_class_name
2264 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2265 static tree cp_parser_class_specifier
2266 (cp_parser *);
2267 static tree cp_parser_class_head
2268 (cp_parser *, bool *);
2269 static enum tag_types cp_parser_class_key
2270 (cp_parser *);
2271 static void cp_parser_type_parameter_key
2272 (cp_parser* parser);
2273 static void cp_parser_member_specification_opt
2274 (cp_parser *);
2275 static void cp_parser_member_declaration
2276 (cp_parser *);
2277 static tree cp_parser_pure_specifier
2278 (cp_parser *);
2279 static tree cp_parser_constant_initializer
2280 (cp_parser *);
2282 /* Derived classes [gram.class.derived] */
2284 static tree cp_parser_base_clause
2285 (cp_parser *);
2286 static tree cp_parser_base_specifier
2287 (cp_parser *);
2289 /* Special member functions [gram.special] */
2291 static tree cp_parser_conversion_function_id
2292 (cp_parser *);
2293 static tree cp_parser_conversion_type_id
2294 (cp_parser *);
2295 static cp_declarator *cp_parser_conversion_declarator_opt
2296 (cp_parser *);
2297 static void cp_parser_ctor_initializer_opt
2298 (cp_parser *);
2299 static void cp_parser_mem_initializer_list
2300 (cp_parser *);
2301 static tree cp_parser_mem_initializer
2302 (cp_parser *);
2303 static tree cp_parser_mem_initializer_id
2304 (cp_parser *);
2306 /* Overloading [gram.over] */
2308 static cp_expr cp_parser_operator_function_id
2309 (cp_parser *);
2310 static cp_expr cp_parser_operator
2311 (cp_parser *);
2313 /* Templates [gram.temp] */
2315 static void cp_parser_template_declaration
2316 (cp_parser *, bool);
2317 static tree cp_parser_template_parameter_list
2318 (cp_parser *);
2319 static tree cp_parser_template_parameter
2320 (cp_parser *, bool *, bool *);
2321 static tree cp_parser_type_parameter
2322 (cp_parser *, bool *);
2323 static tree cp_parser_template_id
2324 (cp_parser *, bool, bool, enum tag_types, bool);
2325 static tree cp_parser_template_name
2326 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2327 static tree cp_parser_template_argument_list
2328 (cp_parser *);
2329 static tree cp_parser_template_argument
2330 (cp_parser *);
2331 static void cp_parser_explicit_instantiation
2332 (cp_parser *);
2333 static void cp_parser_explicit_specialization
2334 (cp_parser *);
2336 /* Exception handling [gram.exception] */
2338 static tree cp_parser_try_block
2339 (cp_parser *);
2340 static void cp_parser_function_try_block
2341 (cp_parser *);
2342 static void cp_parser_handler_seq
2343 (cp_parser *);
2344 static void cp_parser_handler
2345 (cp_parser *);
2346 static tree cp_parser_exception_declaration
2347 (cp_parser *);
2348 static tree cp_parser_throw_expression
2349 (cp_parser *);
2350 static tree cp_parser_exception_specification_opt
2351 (cp_parser *);
2352 static tree cp_parser_type_id_list
2353 (cp_parser *);
2355 /* GNU Extensions */
2357 static tree cp_parser_asm_specification_opt
2358 (cp_parser *);
2359 static tree cp_parser_asm_operand_list
2360 (cp_parser *);
2361 static tree cp_parser_asm_clobber_list
2362 (cp_parser *);
2363 static tree cp_parser_asm_label_list
2364 (cp_parser *);
2365 static bool cp_next_tokens_can_be_attribute_p
2366 (cp_parser *);
2367 static bool cp_next_tokens_can_be_gnu_attribute_p
2368 (cp_parser *);
2369 static bool cp_next_tokens_can_be_std_attribute_p
2370 (cp_parser *);
2371 static bool cp_nth_tokens_can_be_std_attribute_p
2372 (cp_parser *, size_t);
2373 static bool cp_nth_tokens_can_be_gnu_attribute_p
2374 (cp_parser *, size_t);
2375 static bool cp_nth_tokens_can_be_attribute_p
2376 (cp_parser *, size_t);
2377 static tree cp_parser_attributes_opt
2378 (cp_parser *);
2379 static tree cp_parser_gnu_attributes_opt
2380 (cp_parser *);
2381 static tree cp_parser_gnu_attribute_list
2382 (cp_parser *);
2383 static tree cp_parser_std_attribute
2384 (cp_parser *, tree);
2385 static tree cp_parser_std_attribute_spec
2386 (cp_parser *);
2387 static tree cp_parser_std_attribute_spec_seq
2388 (cp_parser *);
2389 static size_t cp_parser_skip_attributes_opt
2390 (cp_parser *, size_t);
2391 static bool cp_parser_extension_opt
2392 (cp_parser *, int *);
2393 static void cp_parser_label_declaration
2394 (cp_parser *);
2396 /* Concept Extensions */
2398 static tree cp_parser_requires_clause
2399 (cp_parser *);
2400 static tree cp_parser_requires_clause_opt
2401 (cp_parser *);
2402 static tree cp_parser_requires_expression
2403 (cp_parser *);
2404 static tree cp_parser_requirement_parameter_list
2405 (cp_parser *);
2406 static tree cp_parser_requirement_body
2407 (cp_parser *);
2408 static tree cp_parser_requirement_list
2409 (cp_parser *);
2410 static tree cp_parser_requirement
2411 (cp_parser *);
2412 static tree cp_parser_simple_requirement
2413 (cp_parser *);
2414 static tree cp_parser_compound_requirement
2415 (cp_parser *);
2416 static tree cp_parser_type_requirement
2417 (cp_parser *);
2418 static tree cp_parser_nested_requirement
2419 (cp_parser *);
2421 /* Transactional Memory Extensions */
2423 static tree cp_parser_transaction
2424 (cp_parser *, cp_token *);
2425 static tree cp_parser_transaction_expression
2426 (cp_parser *, enum rid);
2427 static void cp_parser_function_transaction
2428 (cp_parser *, enum rid);
2429 static tree cp_parser_transaction_cancel
2430 (cp_parser *);
2432 enum pragma_context {
2433 pragma_external,
2434 pragma_member,
2435 pragma_objc_icode,
2436 pragma_stmt,
2437 pragma_compound
2439 static bool cp_parser_pragma
2440 (cp_parser *, enum pragma_context, bool *);
2442 /* Objective-C++ Productions */
2444 static tree cp_parser_objc_message_receiver
2445 (cp_parser *);
2446 static tree cp_parser_objc_message_args
2447 (cp_parser *);
2448 static tree cp_parser_objc_message_expression
2449 (cp_parser *);
2450 static cp_expr cp_parser_objc_encode_expression
2451 (cp_parser *);
2452 static tree cp_parser_objc_defs_expression
2453 (cp_parser *);
2454 static tree cp_parser_objc_protocol_expression
2455 (cp_parser *);
2456 static tree cp_parser_objc_selector_expression
2457 (cp_parser *);
2458 static cp_expr cp_parser_objc_expression
2459 (cp_parser *);
2460 static bool cp_parser_objc_selector_p
2461 (enum cpp_ttype);
2462 static tree cp_parser_objc_selector
2463 (cp_parser *);
2464 static tree cp_parser_objc_protocol_refs_opt
2465 (cp_parser *);
2466 static void cp_parser_objc_declaration
2467 (cp_parser *, tree);
2468 static tree cp_parser_objc_statement
2469 (cp_parser *);
2470 static bool cp_parser_objc_valid_prefix_attributes
2471 (cp_parser *, tree *);
2472 static void cp_parser_objc_at_property_declaration
2473 (cp_parser *) ;
2474 static void cp_parser_objc_at_synthesize_declaration
2475 (cp_parser *) ;
2476 static void cp_parser_objc_at_dynamic_declaration
2477 (cp_parser *) ;
2478 static tree cp_parser_objc_struct_declaration
2479 (cp_parser *) ;
2481 /* Utility Routines */
2483 static cp_expr cp_parser_lookup_name
2484 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2485 static tree cp_parser_lookup_name_simple
2486 (cp_parser *, tree, location_t);
2487 static tree cp_parser_maybe_treat_template_as_class
2488 (tree, bool);
2489 static bool cp_parser_check_declarator_template_parameters
2490 (cp_parser *, cp_declarator *, location_t);
2491 static bool cp_parser_check_template_parameters
2492 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2493 static cp_expr cp_parser_simple_cast_expression
2494 (cp_parser *);
2495 static tree cp_parser_global_scope_opt
2496 (cp_parser *, bool);
2497 static bool cp_parser_constructor_declarator_p
2498 (cp_parser *, bool);
2499 static tree cp_parser_function_definition_from_specifiers_and_declarator
2500 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2501 static tree cp_parser_function_definition_after_declarator
2502 (cp_parser *, bool);
2503 static bool cp_parser_template_declaration_after_export
2504 (cp_parser *, bool);
2505 static void cp_parser_perform_template_parameter_access_checks
2506 (vec<deferred_access_check, va_gc> *);
2507 static tree cp_parser_single_declaration
2508 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2509 static cp_expr cp_parser_functional_cast
2510 (cp_parser *, tree);
2511 static tree cp_parser_save_member_function_body
2512 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2513 static tree cp_parser_save_nsdmi
2514 (cp_parser *);
2515 static tree cp_parser_enclosed_template_argument_list
2516 (cp_parser *);
2517 static void cp_parser_save_default_args
2518 (cp_parser *, tree);
2519 static void cp_parser_late_parsing_for_member
2520 (cp_parser *, tree);
2521 static tree cp_parser_late_parse_one_default_arg
2522 (cp_parser *, tree, tree, tree);
2523 static void cp_parser_late_parsing_nsdmi
2524 (cp_parser *, tree);
2525 static void cp_parser_late_parsing_default_args
2526 (cp_parser *, tree);
2527 static tree cp_parser_sizeof_operand
2528 (cp_parser *, enum rid);
2529 static cp_expr cp_parser_trait_expr
2530 (cp_parser *, enum rid);
2531 static bool cp_parser_declares_only_class_p
2532 (cp_parser *);
2533 static void cp_parser_set_storage_class
2534 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2535 static void cp_parser_set_decl_spec_type
2536 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2537 static void set_and_check_decl_spec_loc
2538 (cp_decl_specifier_seq *decl_specs,
2539 cp_decl_spec ds, cp_token *);
2540 static bool cp_parser_friend_p
2541 (const cp_decl_specifier_seq *);
2542 static void cp_parser_required_error
2543 (cp_parser *, required_token, bool, location_t);
2544 static cp_token *cp_parser_require
2545 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2546 static cp_token *cp_parser_require_keyword
2547 (cp_parser *, enum rid, required_token);
2548 static bool cp_parser_token_starts_function_definition_p
2549 (cp_token *);
2550 static bool cp_parser_next_token_starts_class_definition_p
2551 (cp_parser *);
2552 static bool cp_parser_next_token_ends_template_argument_p
2553 (cp_parser *);
2554 static bool cp_parser_nth_token_starts_template_argument_list_p
2555 (cp_parser *, size_t);
2556 static enum tag_types cp_parser_token_is_class_key
2557 (cp_token *);
2558 static enum tag_types cp_parser_token_is_type_parameter_key
2559 (cp_token *);
2560 static void cp_parser_check_class_key
2561 (enum tag_types, tree type);
2562 static void cp_parser_check_access_in_redeclaration
2563 (tree type, location_t location);
2564 static bool cp_parser_optional_template_keyword
2565 (cp_parser *);
2566 static void cp_parser_pre_parsed_nested_name_specifier
2567 (cp_parser *);
2568 static bool cp_parser_cache_group
2569 (cp_parser *, enum cpp_ttype, unsigned);
2570 static tree cp_parser_cache_defarg
2571 (cp_parser *parser, bool nsdmi);
2572 static void cp_parser_parse_tentatively
2573 (cp_parser *);
2574 static void cp_parser_commit_to_tentative_parse
2575 (cp_parser *);
2576 static void cp_parser_commit_to_topmost_tentative_parse
2577 (cp_parser *);
2578 static void cp_parser_abort_tentative_parse
2579 (cp_parser *);
2580 static bool cp_parser_parse_definitely
2581 (cp_parser *);
2582 static inline bool cp_parser_parsing_tentatively
2583 (cp_parser *);
2584 static bool cp_parser_uncommitted_to_tentative_parse_p
2585 (cp_parser *);
2586 static void cp_parser_error
2587 (cp_parser *, const char *);
2588 static void cp_parser_name_lookup_error
2589 (cp_parser *, tree, tree, name_lookup_error, location_t);
2590 static bool cp_parser_simulate_error
2591 (cp_parser *);
2592 static bool cp_parser_check_type_definition
2593 (cp_parser *);
2594 static void cp_parser_check_for_definition_in_return_type
2595 (cp_declarator *, tree, location_t type_location);
2596 static void cp_parser_check_for_invalid_template_id
2597 (cp_parser *, tree, enum tag_types, location_t location);
2598 static bool cp_parser_non_integral_constant_expression
2599 (cp_parser *, non_integral_constant);
2600 static void cp_parser_diagnose_invalid_type_name
2601 (cp_parser *, tree, location_t);
2602 static bool cp_parser_parse_and_diagnose_invalid_type_name
2603 (cp_parser *);
2604 static int cp_parser_skip_to_closing_parenthesis
2605 (cp_parser *, bool, bool, bool);
2606 static void cp_parser_skip_to_end_of_statement
2607 (cp_parser *);
2608 static void cp_parser_consume_semicolon_at_end_of_statement
2609 (cp_parser *);
2610 static void cp_parser_skip_to_end_of_block_or_statement
2611 (cp_parser *);
2612 static bool cp_parser_skip_to_closing_brace
2613 (cp_parser *);
2614 static void cp_parser_skip_to_end_of_template_parameter_list
2615 (cp_parser *);
2616 static void cp_parser_skip_to_pragma_eol
2617 (cp_parser*, cp_token *);
2618 static bool cp_parser_error_occurred
2619 (cp_parser *);
2620 static bool cp_parser_allow_gnu_extensions_p
2621 (cp_parser *);
2622 static bool cp_parser_is_pure_string_literal
2623 (cp_token *);
2624 static bool cp_parser_is_string_literal
2625 (cp_token *);
2626 static bool cp_parser_is_keyword
2627 (cp_token *, enum rid);
2628 static tree cp_parser_make_typename_type
2629 (cp_parser *, tree, location_t location);
2630 static cp_declarator * cp_parser_make_indirect_declarator
2631 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2632 static bool cp_parser_compound_literal_p
2633 (cp_parser *);
2634 static bool cp_parser_array_designator_p
2635 (cp_parser *);
2636 static bool cp_parser_init_statement_p
2637 (cp_parser *);
2638 static bool cp_parser_skip_to_closing_square_bracket
2639 (cp_parser *);
2641 /* Concept-related syntactic transformations */
2643 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2644 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2646 // -------------------------------------------------------------------------- //
2647 // Unevaluated Operand Guard
2649 // Implementation of an RAII helper for unevaluated operand parsing.
2650 cp_unevaluated::cp_unevaluated ()
2652 ++cp_unevaluated_operand;
2653 ++c_inhibit_evaluation_warnings;
2656 cp_unevaluated::~cp_unevaluated ()
2658 --c_inhibit_evaluation_warnings;
2659 --cp_unevaluated_operand;
2662 // -------------------------------------------------------------------------- //
2663 // Tentative Parsing
2665 /* Returns nonzero if we are parsing tentatively. */
2667 static inline bool
2668 cp_parser_parsing_tentatively (cp_parser* parser)
2670 return parser->context->next != NULL;
2673 /* Returns nonzero if TOKEN is a string literal. */
2675 static bool
2676 cp_parser_is_pure_string_literal (cp_token* token)
2678 return (token->type == CPP_STRING ||
2679 token->type == CPP_STRING16 ||
2680 token->type == CPP_STRING32 ||
2681 token->type == CPP_WSTRING ||
2682 token->type == CPP_UTF8STRING);
2685 /* Returns nonzero if TOKEN is a string literal
2686 of a user-defined string literal. */
2688 static bool
2689 cp_parser_is_string_literal (cp_token* token)
2691 return (cp_parser_is_pure_string_literal (token) ||
2692 token->type == CPP_STRING_USERDEF ||
2693 token->type == CPP_STRING16_USERDEF ||
2694 token->type == CPP_STRING32_USERDEF ||
2695 token->type == CPP_WSTRING_USERDEF ||
2696 token->type == CPP_UTF8STRING_USERDEF);
2699 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2701 static bool
2702 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2704 return token->keyword == keyword;
2707 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2708 PRAGMA_NONE. */
2710 static enum pragma_kind
2711 cp_parser_pragma_kind (cp_token *token)
2713 if (token->type != CPP_PRAGMA)
2714 return PRAGMA_NONE;
2715 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2716 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2719 /* Helper function for cp_parser_error.
2720 Having peeked a token of kind TOK1_KIND that might signify
2721 a conflict marker, peek successor tokens to determine
2722 if we actually do have a conflict marker.
2723 Specifically, we consider a run of 7 '<', '=' or '>' characters
2724 at the start of a line as a conflict marker.
2725 These come through the lexer as three pairs and a single,
2726 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2727 If it returns true, *OUT_LOC is written to with the location/range
2728 of the marker. */
2730 static bool
2731 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2732 location_t *out_loc)
2734 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2735 if (token2->type != tok1_kind)
2736 return false;
2737 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2738 if (token3->type != tok1_kind)
2739 return false;
2740 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2741 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2742 return false;
2744 /* It must be at the start of the line. */
2745 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2746 if (LOCATION_COLUMN (start_loc) != 1)
2747 return false;
2749 /* We have a conflict marker. Construct a location of the form:
2750 <<<<<<<
2751 ^~~~~~~
2752 with start == caret, finishing at the end of the marker. */
2753 location_t finish_loc = get_finish (token4->location);
2754 *out_loc = make_location (start_loc, start_loc, finish_loc);
2756 return true;
2759 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2760 RT_CLOSE_PAREN. */
2762 static const char *
2763 get_matching_symbol (required_token token_desc)
2765 switch (token_desc)
2767 default:
2768 gcc_unreachable ();
2769 return "";
2770 case RT_CLOSE_BRACE:
2771 return "{";
2772 case RT_CLOSE_PAREN:
2773 return "(";
2777 /* Attempt to convert TOKEN_DESC from a required_token to an
2778 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2780 static enum cpp_ttype
2781 get_required_cpp_ttype (required_token token_desc)
2783 switch (token_desc)
2785 case RT_SEMICOLON:
2786 return CPP_SEMICOLON;
2787 case RT_OPEN_PAREN:
2788 return CPP_OPEN_PAREN;
2789 case RT_CLOSE_BRACE:
2790 return CPP_CLOSE_BRACE;
2791 case RT_OPEN_BRACE:
2792 return CPP_OPEN_BRACE;
2793 case RT_CLOSE_SQUARE:
2794 return CPP_CLOSE_SQUARE;
2795 case RT_OPEN_SQUARE:
2796 return CPP_OPEN_SQUARE;
2797 case RT_COMMA:
2798 return CPP_COMMA;
2799 case RT_COLON:
2800 return CPP_COLON;
2801 case RT_CLOSE_PAREN:
2802 return CPP_CLOSE_PAREN;
2804 default:
2805 /* Use CPP_EOF as a "no completions possible" code. */
2806 return CPP_EOF;
2811 /* Subroutine of cp_parser_error and cp_parser_required_error.
2813 Issue a diagnostic of the form
2814 FILE:LINE: MESSAGE before TOKEN
2815 where TOKEN is the next token in the input stream. MESSAGE
2816 (specified by the caller) is usually of the form "expected
2817 OTHER-TOKEN".
2819 This bypasses the check for tentative passing, and potentially
2820 adds material needed by cp_parser_required_error.
2822 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2823 suggesting insertion of the missing token.
2825 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2826 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2827 location. */
2829 static void
2830 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2831 required_token missing_token_desc,
2832 location_t matching_location)
2834 cp_token *token = cp_lexer_peek_token (parser->lexer);
2835 /* This diagnostic makes more sense if it is tagged to the line
2836 of the token we just peeked at. */
2837 cp_lexer_set_source_position_from_token (token);
2839 if (token->type == CPP_PRAGMA)
2841 error_at (token->location,
2842 "%<#pragma%> is not allowed here");
2843 cp_parser_skip_to_pragma_eol (parser, token);
2844 return;
2847 /* If this is actually a conflict marker, report it as such. */
2848 if (token->type == CPP_LSHIFT
2849 || token->type == CPP_RSHIFT
2850 || token->type == CPP_EQ_EQ)
2852 location_t loc;
2853 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2855 error_at (loc, "version control conflict marker in file");
2856 expanded_location token_exploc = expand_location (token->location);
2857 /* Consume tokens until the end of the source line. */
2858 while (1)
2860 cp_lexer_consume_token (parser->lexer);
2861 cp_token *next = cp_lexer_peek_token (parser->lexer);
2862 if (next == NULL)
2863 break;
2864 expanded_location next_exploc = expand_location (next->location);
2865 if (next_exploc.file != token_exploc.file)
2866 break;
2867 if (next_exploc.line != token_exploc.line)
2868 break;
2870 return;
2874 gcc_rich_location richloc (input_location);
2876 bool added_matching_location = false;
2878 if (missing_token_desc != RT_NONE)
2880 /* Potentially supply a fix-it hint, suggesting to add the
2881 missing token immediately after the *previous* token.
2882 This may move the primary location within richloc. */
2883 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2884 location_t prev_token_loc
2885 = cp_lexer_previous_token (parser->lexer)->location;
2886 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2888 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2889 Attempt to consolidate diagnostics by printing it as a
2890 secondary range within the main diagnostic. */
2891 if (matching_location != UNKNOWN_LOCATION)
2892 added_matching_location
2893 = richloc.add_location_if_nearby (matching_location);
2896 /* Actually emit the error. */
2897 c_parse_error (gmsgid,
2898 /* Because c_parser_error does not understand
2899 CPP_KEYWORD, keywords are treated like
2900 identifiers. */
2901 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2902 token->u.value, token->flags, &richloc);
2904 if (missing_token_desc != RT_NONE)
2906 /* If we weren't able to consolidate matching_location, then
2907 print it as a secondary diagnostic. */
2908 if (matching_location != UNKNOWN_LOCATION
2909 && !added_matching_location)
2910 inform (matching_location, "to match this %qs",
2911 get_matching_symbol (missing_token_desc));
2915 /* If not parsing tentatively, issue a diagnostic of the form
2916 FILE:LINE: MESSAGE before TOKEN
2917 where TOKEN is the next token in the input stream. MESSAGE
2918 (specified by the caller) is usually of the form "expected
2919 OTHER-TOKEN". */
2921 static void
2922 cp_parser_error (cp_parser* parser, const char* gmsgid)
2924 if (!cp_parser_simulate_error (parser))
2925 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2928 /* Issue an error about name-lookup failing. NAME is the
2929 IDENTIFIER_NODE DECL is the result of
2930 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2931 the thing that we hoped to find. */
2933 static void
2934 cp_parser_name_lookup_error (cp_parser* parser,
2935 tree name,
2936 tree decl,
2937 name_lookup_error desired,
2938 location_t location)
2940 /* If name lookup completely failed, tell the user that NAME was not
2941 declared. */
2942 if (decl == error_mark_node)
2944 if (parser->scope && parser->scope != global_namespace)
2945 error_at (location, "%<%E::%E%> has not been declared",
2946 parser->scope, name);
2947 else if (parser->scope == global_namespace)
2948 error_at (location, "%<::%E%> has not been declared", name);
2949 else if (parser->object_scope
2950 && !CLASS_TYPE_P (parser->object_scope))
2951 error_at (location, "request for member %qE in non-class type %qT",
2952 name, parser->object_scope);
2953 else if (parser->object_scope)
2954 error_at (location, "%<%T::%E%> has not been declared",
2955 parser->object_scope, name);
2956 else
2957 error_at (location, "%qE has not been declared", name);
2959 else if (parser->scope && parser->scope != global_namespace)
2961 switch (desired)
2963 case NLE_TYPE:
2964 error_at (location, "%<%E::%E%> is not a type",
2965 parser->scope, name);
2966 break;
2967 case NLE_CXX98:
2968 error_at (location, "%<%E::%E%> is not a class or namespace",
2969 parser->scope, name);
2970 break;
2971 case NLE_NOT_CXX98:
2972 error_at (location,
2973 "%<%E::%E%> is not a class, namespace, or enumeration",
2974 parser->scope, name);
2975 break;
2976 default:
2977 gcc_unreachable ();
2981 else if (parser->scope == global_namespace)
2983 switch (desired)
2985 case NLE_TYPE:
2986 error_at (location, "%<::%E%> is not a type", name);
2987 break;
2988 case NLE_CXX98:
2989 error_at (location, "%<::%E%> is not a class or namespace", name);
2990 break;
2991 case NLE_NOT_CXX98:
2992 error_at (location,
2993 "%<::%E%> is not a class, namespace, or enumeration",
2994 name);
2995 break;
2996 default:
2997 gcc_unreachable ();
3000 else
3002 switch (desired)
3004 case NLE_TYPE:
3005 error_at (location, "%qE is not a type", name);
3006 break;
3007 case NLE_CXX98:
3008 error_at (location, "%qE is not a class or namespace", name);
3009 break;
3010 case NLE_NOT_CXX98:
3011 error_at (location,
3012 "%qE is not a class, namespace, or enumeration", name);
3013 break;
3014 default:
3015 gcc_unreachable ();
3020 /* If we are parsing tentatively, remember that an error has occurred
3021 during this tentative parse. Returns true if the error was
3022 simulated; false if a message should be issued by the caller. */
3024 static bool
3025 cp_parser_simulate_error (cp_parser* parser)
3027 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3029 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3030 return true;
3032 return false;
3035 /* This function is called when a type is defined. If type
3036 definitions are forbidden at this point, an error message is
3037 issued. */
3039 static bool
3040 cp_parser_check_type_definition (cp_parser* parser)
3042 /* If types are forbidden here, issue a message. */
3043 if (parser->type_definition_forbidden_message)
3045 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3046 in the message need to be interpreted. */
3047 error (parser->type_definition_forbidden_message);
3048 return false;
3050 return true;
3053 /* This function is called when the DECLARATOR is processed. The TYPE
3054 was a type defined in the decl-specifiers. If it is invalid to
3055 define a type in the decl-specifiers for DECLARATOR, an error is
3056 issued. TYPE_LOCATION is the location of TYPE and is used
3057 for error reporting. */
3059 static void
3060 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3061 tree type, location_t type_location)
3063 /* [dcl.fct] forbids type definitions in return types.
3064 Unfortunately, it's not easy to know whether or not we are
3065 processing a return type until after the fact. */
3066 while (declarator
3067 && (declarator->kind == cdk_pointer
3068 || declarator->kind == cdk_reference
3069 || declarator->kind == cdk_ptrmem))
3070 declarator = declarator->declarator;
3071 if (declarator
3072 && declarator->kind == cdk_function)
3074 error_at (type_location,
3075 "new types may not be defined in a return type");
3076 inform (type_location,
3077 "(perhaps a semicolon is missing after the definition of %qT)",
3078 type);
3082 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3083 "<" in any valid C++ program. If the next token is indeed "<",
3084 issue a message warning the user about what appears to be an
3085 invalid attempt to form a template-id. LOCATION is the location
3086 of the type-specifier (TYPE) */
3088 static void
3089 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3090 tree type,
3091 enum tag_types tag_type,
3092 location_t location)
3094 cp_token_position start = 0;
3096 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3098 if (TREE_CODE (type) == TYPE_DECL)
3099 type = TREE_TYPE (type);
3100 if (TYPE_P (type) && !template_placeholder_p (type))
3101 error_at (location, "%qT is not a template", type);
3102 else if (identifier_p (type))
3104 if (tag_type != none_type)
3105 error_at (location, "%qE is not a class template", type);
3106 else
3107 error_at (location, "%qE is not a template", type);
3109 else
3110 error_at (location, "invalid template-id");
3111 /* Remember the location of the invalid "<". */
3112 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3113 start = cp_lexer_token_position (parser->lexer, true);
3114 /* Consume the "<". */
3115 cp_lexer_consume_token (parser->lexer);
3116 /* Parse the template arguments. */
3117 cp_parser_enclosed_template_argument_list (parser);
3118 /* Permanently remove the invalid template arguments so that
3119 this error message is not issued again. */
3120 if (start)
3121 cp_lexer_purge_tokens_after (parser->lexer, start);
3125 /* If parsing an integral constant-expression, issue an error message
3126 about the fact that THING appeared and return true. Otherwise,
3127 return false. In either case, set
3128 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3130 static bool
3131 cp_parser_non_integral_constant_expression (cp_parser *parser,
3132 non_integral_constant thing)
3134 parser->non_integral_constant_expression_p = true;
3135 if (parser->integral_constant_expression_p)
3137 if (!parser->allow_non_integral_constant_expression_p)
3139 const char *msg = NULL;
3140 switch (thing)
3142 case NIC_FLOAT:
3143 pedwarn (input_location, OPT_Wpedantic,
3144 "ISO C++ forbids using a floating-point literal "
3145 "in a constant-expression");
3146 return true;
3147 case NIC_CAST:
3148 error ("a cast to a type other than an integral or "
3149 "enumeration type cannot appear in a "
3150 "constant-expression");
3151 return true;
3152 case NIC_TYPEID:
3153 error ("%<typeid%> operator "
3154 "cannot appear in a constant-expression");
3155 return true;
3156 case NIC_NCC:
3157 error ("non-constant compound literals "
3158 "cannot appear in a constant-expression");
3159 return true;
3160 case NIC_FUNC_CALL:
3161 error ("a function call "
3162 "cannot appear in a constant-expression");
3163 return true;
3164 case NIC_INC:
3165 error ("an increment "
3166 "cannot appear in a constant-expression");
3167 return true;
3168 case NIC_DEC:
3169 error ("an decrement "
3170 "cannot appear in a constant-expression");
3171 return true;
3172 case NIC_ARRAY_REF:
3173 error ("an array reference "
3174 "cannot appear in a constant-expression");
3175 return true;
3176 case NIC_ADDR_LABEL:
3177 error ("the address of a label "
3178 "cannot appear in a constant-expression");
3179 return true;
3180 case NIC_OVERLOADED:
3181 error ("calls to overloaded operators "
3182 "cannot appear in a constant-expression");
3183 return true;
3184 case NIC_ASSIGNMENT:
3185 error ("an assignment cannot appear in a constant-expression");
3186 return true;
3187 case NIC_COMMA:
3188 error ("a comma operator "
3189 "cannot appear in a constant-expression");
3190 return true;
3191 case NIC_CONSTRUCTOR:
3192 error ("a call to a constructor "
3193 "cannot appear in a constant-expression");
3194 return true;
3195 case NIC_TRANSACTION:
3196 error ("a transaction expression "
3197 "cannot appear in a constant-expression");
3198 return true;
3199 case NIC_THIS:
3200 msg = "this";
3201 break;
3202 case NIC_FUNC_NAME:
3203 msg = "__FUNCTION__";
3204 break;
3205 case NIC_PRETTY_FUNC:
3206 msg = "__PRETTY_FUNCTION__";
3207 break;
3208 case NIC_C99_FUNC:
3209 msg = "__func__";
3210 break;
3211 case NIC_VA_ARG:
3212 msg = "va_arg";
3213 break;
3214 case NIC_ARROW:
3215 msg = "->";
3216 break;
3217 case NIC_POINT:
3218 msg = ".";
3219 break;
3220 case NIC_STAR:
3221 msg = "*";
3222 break;
3223 case NIC_ADDR:
3224 msg = "&";
3225 break;
3226 case NIC_PREINCREMENT:
3227 msg = "++";
3228 break;
3229 case NIC_PREDECREMENT:
3230 msg = "--";
3231 break;
3232 case NIC_NEW:
3233 msg = "new";
3234 break;
3235 case NIC_DEL:
3236 msg = "delete";
3237 break;
3238 default:
3239 gcc_unreachable ();
3241 if (msg)
3242 error ("%qs cannot appear in a constant-expression", msg);
3243 return true;
3246 return false;
3249 /* Emit a diagnostic for an invalid type name. This function commits
3250 to the current active tentative parse, if any. (Otherwise, the
3251 problematic construct might be encountered again later, resulting
3252 in duplicate error messages.) LOCATION is the location of ID. */
3254 static void
3255 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3256 location_t location)
3258 tree decl, ambiguous_decls;
3259 cp_parser_commit_to_tentative_parse (parser);
3260 /* Try to lookup the identifier. */
3261 decl = cp_parser_lookup_name (parser, id, none_type,
3262 /*is_template=*/false,
3263 /*is_namespace=*/false,
3264 /*check_dependency=*/true,
3265 &ambiguous_decls, location);
3266 if (ambiguous_decls)
3267 /* If the lookup was ambiguous, an error will already have
3268 been issued. */
3269 return;
3270 /* If the lookup found a template-name, it means that the user forgot
3271 to specify an argument list. Emit a useful error message. */
3272 if (DECL_TYPE_TEMPLATE_P (decl))
3274 auto_diagnostic_group d;
3275 error_at (location,
3276 "invalid use of template-name %qE without an argument list",
3277 decl);
3278 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3279 inform (location, "class template argument deduction is only available "
3280 "with -std=c++17 or -std=gnu++17");
3281 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3283 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3284 error_at (location, "invalid use of destructor %qD as a type", id);
3285 else if (TREE_CODE (decl) == TYPE_DECL)
3286 /* Something like 'unsigned A a;' */
3287 error_at (location, "invalid combination of multiple type-specifiers");
3288 else if (!parser->scope)
3290 /* Issue an error message. */
3291 auto_diagnostic_group d;
3292 name_hint hint;
3293 if (TREE_CODE (id) == IDENTIFIER_NODE)
3294 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3295 if (hint)
3297 gcc_rich_location richloc (location);
3298 richloc.add_fixit_replace (hint.suggestion ());
3299 error_at (&richloc,
3300 "%qE does not name a type; did you mean %qs?",
3301 id, hint.suggestion ());
3303 else
3304 error_at (location, "%qE does not name a type", id);
3305 /* If we're in a template class, it's possible that the user was
3306 referring to a type from a base class. For example:
3308 template <typename T> struct A { typedef T X; };
3309 template <typename T> struct B : public A<T> { X x; };
3311 The user should have said "typename A<T>::X". */
3312 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3313 inform (location, "C++11 %<constexpr%> only available with "
3314 "-std=c++11 or -std=gnu++11");
3315 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3316 inform (location, "C++11 %<noexcept%> only available with "
3317 "-std=c++11 or -std=gnu++11");
3318 else if (cxx_dialect < cxx11
3319 && TREE_CODE (id) == IDENTIFIER_NODE
3320 && id_equal (id, "thread_local"))
3321 inform (location, "C++11 %<thread_local%> only available with "
3322 "-std=c++11 or -std=gnu++11");
3323 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3324 inform (location, "%<concept%> only available with -fconcepts");
3325 else if (processing_template_decl && current_class_type
3326 && TYPE_BINFO (current_class_type))
3328 tree b;
3330 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3332 b = TREE_CHAIN (b))
3334 tree base_type = BINFO_TYPE (b);
3335 if (CLASS_TYPE_P (base_type)
3336 && dependent_type_p (base_type))
3338 tree field;
3339 /* Go from a particular instantiation of the
3340 template (which will have an empty TYPE_FIELDs),
3341 to the main version. */
3342 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3343 for (field = TYPE_FIELDS (base_type);
3344 field;
3345 field = DECL_CHAIN (field))
3346 if (TREE_CODE (field) == TYPE_DECL
3347 && DECL_NAME (field) == id)
3349 inform (location,
3350 "(perhaps %<typename %T::%E%> was intended)",
3351 BINFO_TYPE (b), id);
3352 break;
3354 if (field)
3355 break;
3360 /* Here we diagnose qualified-ids where the scope is actually correct,
3361 but the identifier does not resolve to a valid type name. */
3362 else if (parser->scope != error_mark_node)
3364 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3366 auto_diagnostic_group d;
3367 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3368 error_at (location_of (id),
3369 "%qE in namespace %qE does not name a template type",
3370 id, parser->scope);
3371 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3372 error_at (location_of (id),
3373 "%qE in namespace %qE does not name a template type",
3374 TREE_OPERAND (id, 0), parser->scope);
3375 else
3376 error_at (location_of (id),
3377 "%qE in namespace %qE does not name a type",
3378 id, parser->scope);
3379 if (DECL_P (decl))
3380 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3381 else if (decl == error_mark_node)
3382 suggest_alternative_in_explicit_scope (location, id,
3383 parser->scope);
3385 else if (CLASS_TYPE_P (parser->scope)
3386 && constructor_name_p (id, parser->scope))
3388 /* A<T>::A<T>() */
3389 auto_diagnostic_group d;
3390 error_at (location, "%<%T::%E%> names the constructor, not"
3391 " the type", parser->scope, id);
3392 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3393 error_at (location, "and %qT has no template constructors",
3394 parser->scope);
3396 else if (TYPE_P (parser->scope)
3397 && dependent_scope_p (parser->scope))
3399 gcc_rich_location richloc (location);
3400 richloc.add_fixit_insert_before ("typename ");
3401 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3402 error_at (&richloc,
3403 "need %<typename%> before %<%T::%D::%E%> because "
3404 "%<%T::%D%> is a dependent scope",
3405 TYPE_CONTEXT (parser->scope),
3406 TYPENAME_TYPE_FULLNAME (parser->scope),
3408 TYPE_CONTEXT (parser->scope),
3409 TYPENAME_TYPE_FULLNAME (parser->scope));
3410 else
3411 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3412 "%qT is a dependent scope",
3413 parser->scope, id, parser->scope);
3415 else if (TYPE_P (parser->scope))
3417 auto_diagnostic_group d;
3418 if (!COMPLETE_TYPE_P (parser->scope))
3419 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3420 parser->scope);
3421 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3422 error_at (location_of (id),
3423 "%qE in %q#T does not name a template type",
3424 id, parser->scope);
3425 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3426 error_at (location_of (id),
3427 "%qE in %q#T does not name a template type",
3428 TREE_OPERAND (id, 0), parser->scope);
3429 else
3430 error_at (location_of (id),
3431 "%qE in %q#T does not name a type",
3432 id, parser->scope);
3433 if (DECL_P (decl))
3434 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3436 else
3437 gcc_unreachable ();
3441 /* Check for a common situation where a type-name should be present,
3442 but is not, and issue a sensible error message. Returns true if an
3443 invalid type-name was detected.
3445 The situation handled by this function are variable declarations of the
3446 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3447 Usually, `ID' should name a type, but if we got here it means that it
3448 does not. We try to emit the best possible error message depending on
3449 how exactly the id-expression looks like. */
3451 static bool
3452 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3454 tree id;
3455 cp_token *token = cp_lexer_peek_token (parser->lexer);
3457 /* Avoid duplicate error about ambiguous lookup. */
3458 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3460 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3461 if (next->type == CPP_NAME && next->error_reported)
3462 goto out;
3465 cp_parser_parse_tentatively (parser);
3466 id = cp_parser_id_expression (parser,
3467 /*template_keyword_p=*/false,
3468 /*check_dependency_p=*/true,
3469 /*template_p=*/NULL,
3470 /*declarator_p=*/false,
3471 /*optional_p=*/false);
3472 /* If the next token is a (, this is a function with no explicit return
3473 type, i.e. constructor, destructor or conversion op. */
3474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3475 || TREE_CODE (id) == TYPE_DECL)
3477 cp_parser_abort_tentative_parse (parser);
3478 return false;
3480 if (!cp_parser_parse_definitely (parser))
3481 return false;
3483 /* Emit a diagnostic for the invalid type. */
3484 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3485 out:
3486 /* If we aren't in the middle of a declarator (i.e. in a
3487 parameter-declaration-clause), skip to the end of the declaration;
3488 there's no point in trying to process it. */
3489 if (!parser->in_declarator_p)
3490 cp_parser_skip_to_end_of_block_or_statement (parser);
3491 return true;
3494 /* Consume tokens up to, and including, the next non-nested closing `)'.
3495 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3496 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3497 found an unnested token of that type. */
3499 static int
3500 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3501 bool recovering,
3502 cpp_ttype or_ttype,
3503 bool consume_paren)
3505 unsigned paren_depth = 0;
3506 unsigned brace_depth = 0;
3507 unsigned square_depth = 0;
3508 unsigned condop_depth = 0;
3510 if (recovering && or_ttype == CPP_EOF
3511 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3512 return 0;
3514 while (true)
3516 cp_token * token = cp_lexer_peek_token (parser->lexer);
3518 /* Have we found what we're looking for before the closing paren? */
3519 if (token->type == or_ttype && or_ttype != CPP_EOF
3520 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3521 return -1;
3523 switch (token->type)
3525 case CPP_EOF:
3526 case CPP_PRAGMA_EOL:
3527 /* If we've run out of tokens, then there is no closing `)'. */
3528 return 0;
3530 /* This is good for lambda expression capture-lists. */
3531 case CPP_OPEN_SQUARE:
3532 ++square_depth;
3533 break;
3534 case CPP_CLOSE_SQUARE:
3535 if (!square_depth--)
3536 return 0;
3537 break;
3539 case CPP_SEMICOLON:
3540 /* This matches the processing in skip_to_end_of_statement. */
3541 if (!brace_depth)
3542 return 0;
3543 break;
3545 case CPP_OPEN_BRACE:
3546 ++brace_depth;
3547 break;
3548 case CPP_CLOSE_BRACE:
3549 if (!brace_depth--)
3550 return 0;
3551 break;
3553 case CPP_OPEN_PAREN:
3554 if (!brace_depth)
3555 ++paren_depth;
3556 break;
3558 case CPP_CLOSE_PAREN:
3559 if (!brace_depth && !paren_depth--)
3561 if (consume_paren)
3562 cp_lexer_consume_token (parser->lexer);
3563 return 1;
3565 break;
3567 case CPP_QUERY:
3568 if (!brace_depth && !paren_depth && !square_depth)
3569 ++condop_depth;
3570 break;
3572 case CPP_COLON:
3573 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3574 condop_depth--;
3575 break;
3577 default:
3578 break;
3581 /* Consume the token. */
3582 cp_lexer_consume_token (parser->lexer);
3586 /* Consume tokens up to, and including, the next non-nested closing `)'.
3587 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3588 are doing error recovery. Returns -1 if OR_COMMA is true and we
3589 found an unnested token of that type. */
3591 static int
3592 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3593 bool recovering,
3594 bool or_comma,
3595 bool consume_paren)
3597 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3598 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3599 ttype, consume_paren);
3602 /* Consume tokens until we reach the end of the current statement.
3603 Normally, that will be just before consuming a `;'. However, if a
3604 non-nested `}' comes first, then we stop before consuming that. */
3606 static void
3607 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3609 unsigned nesting_depth = 0;
3611 /* Unwind generic function template scope if necessary. */
3612 if (parser->fully_implicit_function_template_p)
3613 abort_fully_implicit_template (parser);
3615 while (true)
3617 cp_token *token = cp_lexer_peek_token (parser->lexer);
3619 switch (token->type)
3621 case CPP_EOF:
3622 case CPP_PRAGMA_EOL:
3623 /* If we've run out of tokens, stop. */
3624 return;
3626 case CPP_SEMICOLON:
3627 /* If the next token is a `;', we have reached the end of the
3628 statement. */
3629 if (!nesting_depth)
3630 return;
3631 break;
3633 case CPP_CLOSE_BRACE:
3634 /* If this is a non-nested '}', stop before consuming it.
3635 That way, when confronted with something like:
3637 { 3 + }
3639 we stop before consuming the closing '}', even though we
3640 have not yet reached a `;'. */
3641 if (nesting_depth == 0)
3642 return;
3644 /* If it is the closing '}' for a block that we have
3645 scanned, stop -- but only after consuming the token.
3646 That way given:
3648 void f g () { ... }
3649 typedef int I;
3651 we will stop after the body of the erroneously declared
3652 function, but before consuming the following `typedef'
3653 declaration. */
3654 if (--nesting_depth == 0)
3656 cp_lexer_consume_token (parser->lexer);
3657 return;
3659 break;
3661 case CPP_OPEN_BRACE:
3662 ++nesting_depth;
3663 break;
3665 default:
3666 break;
3669 /* Consume the token. */
3670 cp_lexer_consume_token (parser->lexer);
3674 /* This function is called at the end of a statement or declaration.
3675 If the next token is a semicolon, it is consumed; otherwise, error
3676 recovery is attempted. */
3678 static void
3679 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3681 /* Look for the trailing `;'. */
3682 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3684 /* If there is additional (erroneous) input, skip to the end of
3685 the statement. */
3686 cp_parser_skip_to_end_of_statement (parser);
3687 /* If the next token is now a `;', consume it. */
3688 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3689 cp_lexer_consume_token (parser->lexer);
3693 /* Skip tokens until we have consumed an entire block, or until we
3694 have consumed a non-nested `;'. */
3696 static void
3697 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3699 int nesting_depth = 0;
3701 /* Unwind generic function template scope if necessary. */
3702 if (parser->fully_implicit_function_template_p)
3703 abort_fully_implicit_template (parser);
3705 while (nesting_depth >= 0)
3707 cp_token *token = cp_lexer_peek_token (parser->lexer);
3709 switch (token->type)
3711 case CPP_EOF:
3712 case CPP_PRAGMA_EOL:
3713 /* If we've run out of tokens, stop. */
3714 return;
3716 case CPP_SEMICOLON:
3717 /* Stop if this is an unnested ';'. */
3718 if (!nesting_depth)
3719 nesting_depth = -1;
3720 break;
3722 case CPP_CLOSE_BRACE:
3723 /* Stop if this is an unnested '}', or closes the outermost
3724 nesting level. */
3725 nesting_depth--;
3726 if (nesting_depth < 0)
3727 return;
3728 if (!nesting_depth)
3729 nesting_depth = -1;
3730 break;
3732 case CPP_OPEN_BRACE:
3733 /* Nest. */
3734 nesting_depth++;
3735 break;
3737 default:
3738 break;
3741 /* Consume the token. */
3742 cp_lexer_consume_token (parser->lexer);
3746 /* Skip tokens until a non-nested closing curly brace is the next
3747 token, or there are no more tokens. Return true in the first case,
3748 false otherwise. */
3750 static bool
3751 cp_parser_skip_to_closing_brace (cp_parser *parser)
3753 unsigned nesting_depth = 0;
3755 while (true)
3757 cp_token *token = cp_lexer_peek_token (parser->lexer);
3759 switch (token->type)
3761 case CPP_EOF:
3762 case CPP_PRAGMA_EOL:
3763 /* If we've run out of tokens, stop. */
3764 return false;
3766 case CPP_CLOSE_BRACE:
3767 /* If the next token is a non-nested `}', then we have reached
3768 the end of the current block. */
3769 if (nesting_depth-- == 0)
3770 return true;
3771 break;
3773 case CPP_OPEN_BRACE:
3774 /* If it the next token is a `{', then we are entering a new
3775 block. Consume the entire block. */
3776 ++nesting_depth;
3777 break;
3779 default:
3780 break;
3783 /* Consume the token. */
3784 cp_lexer_consume_token (parser->lexer);
3788 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3789 parameter is the PRAGMA token, allowing us to purge the entire pragma
3790 sequence. */
3792 static void
3793 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3795 cp_token *token;
3797 parser->lexer->in_pragma = false;
3800 token = cp_lexer_consume_token (parser->lexer);
3801 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3803 /* Ensure that the pragma is not parsed again. */
3804 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3807 /* Require pragma end of line, resyncing with it as necessary. The
3808 arguments are as for cp_parser_skip_to_pragma_eol. */
3810 static void
3811 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3813 parser->lexer->in_pragma = false;
3814 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3815 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3818 /* This is a simple wrapper around make_typename_type. When the id is
3819 an unresolved identifier node, we can provide a superior diagnostic
3820 using cp_parser_diagnose_invalid_type_name. */
3822 static tree
3823 cp_parser_make_typename_type (cp_parser *parser, tree id,
3824 location_t id_location)
3826 tree result;
3827 if (identifier_p (id))
3829 result = make_typename_type (parser->scope, id, typename_type,
3830 /*complain=*/tf_none);
3831 if (result == error_mark_node)
3832 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3833 return result;
3835 return make_typename_type (parser->scope, id, typename_type, tf_error);
3838 /* This is a wrapper around the
3839 make_{pointer,ptrmem,reference}_declarator functions that decides
3840 which one to call based on the CODE and CLASS_TYPE arguments. The
3841 CODE argument should be one of the values returned by
3842 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3843 appertain to the pointer or reference. */
3845 static cp_declarator *
3846 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3847 cp_cv_quals cv_qualifiers,
3848 cp_declarator *target,
3849 tree attributes)
3851 if (code == ERROR_MARK || target == cp_error_declarator)
3852 return cp_error_declarator;
3854 if (code == INDIRECT_REF)
3855 if (class_type == NULL_TREE)
3856 return make_pointer_declarator (cv_qualifiers, target, attributes);
3857 else
3858 return make_ptrmem_declarator (cv_qualifiers, class_type,
3859 target, attributes);
3860 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3861 return make_reference_declarator (cv_qualifiers, target,
3862 false, attributes);
3863 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3864 return make_reference_declarator (cv_qualifiers, target,
3865 true, attributes);
3866 gcc_unreachable ();
3869 /* Create a new C++ parser. */
3871 static cp_parser *
3872 cp_parser_new (void)
3874 cp_parser *parser;
3875 cp_lexer *lexer;
3876 unsigned i;
3878 /* cp_lexer_new_main is called before doing GC allocation because
3879 cp_lexer_new_main might load a PCH file. */
3880 lexer = cp_lexer_new_main ();
3882 /* Initialize the binops_by_token so that we can get the tree
3883 directly from the token. */
3884 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3885 binops_by_token[binops[i].token_type] = binops[i];
3887 parser = ggc_cleared_alloc<cp_parser> ();
3888 parser->lexer = lexer;
3889 parser->context = cp_parser_context_new (NULL);
3891 /* For now, we always accept GNU extensions. */
3892 parser->allow_gnu_extensions_p = 1;
3894 /* The `>' token is a greater-than operator, not the end of a
3895 template-id. */
3896 parser->greater_than_is_operator_p = true;
3898 parser->default_arg_ok_p = true;
3900 /* We are not parsing a constant-expression. */
3901 parser->integral_constant_expression_p = false;
3902 parser->allow_non_integral_constant_expression_p = false;
3903 parser->non_integral_constant_expression_p = false;
3905 /* Local variable names are not forbidden. */
3906 parser->local_variables_forbidden_p = false;
3908 /* We are not processing an `extern "C"' declaration. */
3909 parser->in_unbraced_linkage_specification_p = false;
3911 /* We are not processing a declarator. */
3912 parser->in_declarator_p = false;
3914 /* We are not processing a template-argument-list. */
3915 parser->in_template_argument_list_p = false;
3917 /* We are not in an iteration statement. */
3918 parser->in_statement = 0;
3920 /* We are not in a switch statement. */
3921 parser->in_switch_statement_p = false;
3923 /* We are not parsing a type-id inside an expression. */
3924 parser->in_type_id_in_expr_p = false;
3926 /* String literals should be translated to the execution character set. */
3927 parser->translate_strings_p = true;
3929 /* We are not parsing a function body. */
3930 parser->in_function_body = false;
3932 /* We can correct until told otherwise. */
3933 parser->colon_corrects_to_scope_p = true;
3935 /* The unparsed function queue is empty. */
3936 push_unparsed_function_queues (parser);
3938 /* There are no classes being defined. */
3939 parser->num_classes_being_defined = 0;
3941 /* No template parameters apply. */
3942 parser->num_template_parameter_lists = 0;
3944 /* Special parsing data structures. */
3945 parser->omp_declare_simd = NULL;
3946 parser->oacc_routine = NULL;
3948 /* Not declaring an implicit function template. */
3949 parser->auto_is_implicit_function_template_parm_p = false;
3950 parser->fully_implicit_function_template_p = false;
3951 parser->implicit_template_parms = 0;
3952 parser->implicit_template_scope = 0;
3954 /* Allow constrained-type-specifiers. */
3955 parser->prevent_constrained_type_specifiers = 0;
3957 /* We haven't yet seen an 'extern "C"'. */
3958 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3960 return parser;
3963 /* Create a cp_lexer structure which will emit the tokens in CACHE
3964 and push it onto the parser's lexer stack. This is used for delayed
3965 parsing of in-class method bodies and default arguments, and should
3966 not be confused with tentative parsing. */
3967 static void
3968 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3970 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3971 lexer->next = parser->lexer;
3972 parser->lexer = lexer;
3974 /* Move the current source position to that of the first token in the
3975 new lexer. */
3976 cp_lexer_set_source_position_from_token (lexer->next_token);
3979 /* Pop the top lexer off the parser stack. This is never used for the
3980 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3981 static void
3982 cp_parser_pop_lexer (cp_parser *parser)
3984 cp_lexer *lexer = parser->lexer;
3985 parser->lexer = lexer->next;
3986 cp_lexer_destroy (lexer);
3988 /* Put the current source position back where it was before this
3989 lexer was pushed. */
3990 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3993 /* Lexical conventions [gram.lex] */
3995 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3996 identifier. */
3998 static cp_expr
3999 cp_parser_identifier (cp_parser* parser)
4001 cp_token *token;
4003 /* Look for the identifier. */
4004 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4005 /* Return the value. */
4006 if (token)
4007 return cp_expr (token->u.value, token->location);
4008 else
4009 return error_mark_node;
4012 /* Parse a sequence of adjacent string constants. Returns a
4013 TREE_STRING representing the combined, nul-terminated string
4014 constant. If TRANSLATE is true, translate the string to the
4015 execution character set. If WIDE_OK is true, a wide string is
4016 invalid here.
4018 C++98 [lex.string] says that if a narrow string literal token is
4019 adjacent to a wide string literal token, the behavior is undefined.
4020 However, C99 6.4.5p4 says that this results in a wide string literal.
4021 We follow C99 here, for consistency with the C front end.
4023 This code is largely lifted from lex_string() in c-lex.c.
4025 FUTURE: ObjC++ will need to handle @-strings here. */
4026 static cp_expr
4027 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4028 bool lookup_udlit = true)
4030 tree value;
4031 size_t count;
4032 struct obstack str_ob;
4033 struct obstack loc_ob;
4034 cpp_string str, istr, *strs;
4035 cp_token *tok;
4036 enum cpp_ttype type, curr_type;
4037 int have_suffix_p = 0;
4038 tree string_tree;
4039 tree suffix_id = NULL_TREE;
4040 bool curr_tok_is_userdef_p = false;
4042 tok = cp_lexer_peek_token (parser->lexer);
4043 if (!cp_parser_is_string_literal (tok))
4045 cp_parser_error (parser, "expected string-literal");
4046 return error_mark_node;
4049 location_t loc = tok->location;
4051 if (cpp_userdef_string_p (tok->type))
4053 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4054 curr_type = cpp_userdef_string_remove_type (tok->type);
4055 curr_tok_is_userdef_p = true;
4057 else
4059 string_tree = tok->u.value;
4060 curr_type = tok->type;
4062 type = curr_type;
4064 /* Try to avoid the overhead of creating and destroying an obstack
4065 for the common case of just one string. */
4066 if (!cp_parser_is_string_literal
4067 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4069 cp_lexer_consume_token (parser->lexer);
4071 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4072 str.len = TREE_STRING_LENGTH (string_tree);
4073 count = 1;
4075 if (curr_tok_is_userdef_p)
4077 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4078 have_suffix_p = 1;
4079 curr_type = cpp_userdef_string_remove_type (tok->type);
4081 else
4082 curr_type = tok->type;
4084 strs = &str;
4086 else
4088 location_t last_tok_loc = tok->location;
4089 gcc_obstack_init (&str_ob);
4090 gcc_obstack_init (&loc_ob);
4091 count = 0;
4095 cp_lexer_consume_token (parser->lexer);
4096 count++;
4097 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4098 str.len = TREE_STRING_LENGTH (string_tree);
4100 if (curr_tok_is_userdef_p)
4102 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4103 if (have_suffix_p == 0)
4105 suffix_id = curr_suffix_id;
4106 have_suffix_p = 1;
4108 else if (have_suffix_p == 1
4109 && curr_suffix_id != suffix_id)
4111 error ("inconsistent user-defined literal suffixes"
4112 " %qD and %qD in string literal",
4113 suffix_id, curr_suffix_id);
4114 have_suffix_p = -1;
4116 curr_type = cpp_userdef_string_remove_type (tok->type);
4118 else
4119 curr_type = tok->type;
4121 if (type != curr_type)
4123 if (type == CPP_STRING)
4124 type = curr_type;
4125 else if (curr_type != CPP_STRING)
4127 rich_location rich_loc (line_table, tok->location);
4128 rich_loc.add_range (last_tok_loc);
4129 error_at (&rich_loc,
4130 "unsupported non-standard concatenation "
4131 "of string literals");
4135 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4136 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4138 last_tok_loc = tok->location;
4140 tok = cp_lexer_peek_token (parser->lexer);
4141 if (cpp_userdef_string_p (tok->type))
4143 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4144 curr_type = cpp_userdef_string_remove_type (tok->type);
4145 curr_tok_is_userdef_p = true;
4147 else
4149 string_tree = tok->u.value;
4150 curr_type = tok->type;
4151 curr_tok_is_userdef_p = false;
4154 while (cp_parser_is_string_literal (tok));
4156 /* A string literal built by concatenation has its caret=start at
4157 the start of the initial string, and its finish at the finish of
4158 the final string literal. */
4159 loc = make_location (loc, loc, get_finish (last_tok_loc));
4161 strs = (cpp_string *) obstack_finish (&str_ob);
4164 if (type != CPP_STRING && !wide_ok)
4166 cp_parser_error (parser, "a wide string is invalid in this context");
4167 type = CPP_STRING;
4170 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4171 (parse_in, strs, count, &istr, type))
4173 value = build_string (istr.len, (const char *)istr.text);
4174 free (CONST_CAST (unsigned char *, istr.text));
4175 if (count > 1)
4177 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4178 gcc_assert (g_string_concat_db);
4179 g_string_concat_db->record_string_concatenation (count, locs);
4182 switch (type)
4184 default:
4185 case CPP_STRING:
4186 case CPP_UTF8STRING:
4187 TREE_TYPE (value) = char_array_type_node;
4188 break;
4189 case CPP_STRING16:
4190 TREE_TYPE (value) = char16_array_type_node;
4191 break;
4192 case CPP_STRING32:
4193 TREE_TYPE (value) = char32_array_type_node;
4194 break;
4195 case CPP_WSTRING:
4196 TREE_TYPE (value) = wchar_array_type_node;
4197 break;
4200 value = fix_string_type (value);
4202 if (have_suffix_p)
4204 tree literal = build_userdef_literal (suffix_id, value,
4205 OT_NONE, NULL_TREE);
4206 if (lookup_udlit)
4207 value = cp_parser_userdef_string_literal (literal);
4208 else
4209 value = literal;
4212 else
4213 /* cpp_interpret_string has issued an error. */
4214 value = error_mark_node;
4216 if (count > 1)
4218 obstack_free (&str_ob, 0);
4219 obstack_free (&loc_ob, 0);
4222 return cp_expr (value, loc);
4225 /* Look up a literal operator with the name and the exact arguments. */
4227 static tree
4228 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4230 tree decl;
4231 decl = lookup_name (name);
4232 if (!decl || !is_overloaded_fn (decl))
4233 return error_mark_node;
4235 for (lkp_iterator iter (decl); iter; ++iter)
4237 unsigned int ix;
4238 bool found = true;
4239 tree fn = *iter;
4240 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4241 if (parmtypes != NULL_TREE)
4243 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4244 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4246 tree tparm = TREE_VALUE (parmtypes);
4247 tree targ = TREE_TYPE ((*args)[ix]);
4248 bool ptr = TYPE_PTR_P (tparm);
4249 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4250 if ((ptr || arr || !same_type_p (tparm, targ))
4251 && (!ptr || !arr
4252 || !same_type_p (TREE_TYPE (tparm),
4253 TREE_TYPE (targ))))
4254 found = false;
4256 if (found
4257 && ix == vec_safe_length (args)
4258 /* May be this should be sufficient_parms_p instead,
4259 depending on how exactly should user-defined literals
4260 work in presence of default arguments on the literal
4261 operator parameters. */
4262 && parmtypes == void_list_node)
4263 return decl;
4267 return error_mark_node;
4270 /* Parse a user-defined char constant. Returns a call to a user-defined
4271 literal operator taking the character as an argument. */
4273 static cp_expr
4274 cp_parser_userdef_char_literal (cp_parser *parser)
4276 cp_token *token = cp_lexer_consume_token (parser->lexer);
4277 tree literal = token->u.value;
4278 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4279 tree value = USERDEF_LITERAL_VALUE (literal);
4280 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4281 tree decl, result;
4283 /* Build up a call to the user-defined operator */
4284 /* Lookup the name we got back from the id-expression. */
4285 vec<tree, va_gc> *args = make_tree_vector ();
4286 vec_safe_push (args, value);
4287 decl = lookup_literal_operator (name, args);
4288 if (!decl || decl == error_mark_node)
4290 error ("unable to find character literal operator %qD with %qT argument",
4291 name, TREE_TYPE (value));
4292 release_tree_vector (args);
4293 return error_mark_node;
4295 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4296 release_tree_vector (args);
4297 return result;
4300 /* A subroutine of cp_parser_userdef_numeric_literal to
4301 create a char... template parameter pack from a string node. */
4303 static tree
4304 make_char_string_pack (tree value)
4306 tree charvec;
4307 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4308 const char *str = TREE_STRING_POINTER (value);
4309 int i, len = TREE_STRING_LENGTH (value) - 1;
4310 tree argvec = make_tree_vec (1);
4312 /* Fill in CHARVEC with all of the parameters. */
4313 charvec = make_tree_vec (len);
4314 for (i = 0; i < len; ++i)
4316 unsigned char s[3] = { '\'', str[i], '\'' };
4317 cpp_string in = { 3, s };
4318 cpp_string out = { 0, 0 };
4319 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4320 return NULL_TREE;
4321 gcc_assert (out.len == 2);
4322 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4323 out.text[0]);
4326 /* Build the argument packs. */
4327 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4329 TREE_VEC_ELT (argvec, 0) = argpack;
4331 return argvec;
4334 /* A subroutine of cp_parser_userdef_numeric_literal to
4335 create a char... template parameter pack from a string node. */
4337 static tree
4338 make_string_pack (tree value)
4340 tree charvec;
4341 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4342 const unsigned char *str
4343 = (const unsigned char *) TREE_STRING_POINTER (value);
4344 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4345 int len = TREE_STRING_LENGTH (value) / sz - 1;
4346 tree argvec = make_tree_vec (2);
4348 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4349 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4351 /* First template parm is character type. */
4352 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4354 /* Fill in CHARVEC with all of the parameters. */
4355 charvec = make_tree_vec (len);
4356 for (int i = 0; i < len; ++i)
4357 TREE_VEC_ELT (charvec, i)
4358 = double_int_to_tree (str_char_type_node,
4359 double_int::from_buffer (str + i * sz, sz));
4361 /* Build the argument packs. */
4362 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4364 TREE_VEC_ELT (argvec, 1) = argpack;
4366 return argvec;
4369 /* Parse a user-defined numeric constant. returns a call to a user-defined
4370 literal operator. */
4372 static cp_expr
4373 cp_parser_userdef_numeric_literal (cp_parser *parser)
4375 cp_token *token = cp_lexer_consume_token (parser->lexer);
4376 tree literal = token->u.value;
4377 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4378 tree value = USERDEF_LITERAL_VALUE (literal);
4379 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4380 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4381 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4382 tree decl, result;
4383 vec<tree, va_gc> *args;
4385 /* Look for a literal operator taking the exact type of numeric argument
4386 as the literal value. */
4387 args = make_tree_vector ();
4388 vec_safe_push (args, value);
4389 decl = lookup_literal_operator (name, args);
4390 if (decl && decl != error_mark_node)
4392 result = finish_call_expr (decl, &args, false, true,
4393 tf_warning_or_error);
4395 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4397 warning_at (token->location, OPT_Woverflow,
4398 "integer literal exceeds range of %qT type",
4399 long_long_unsigned_type_node);
4401 else
4403 if (overflow > 0)
4404 warning_at (token->location, OPT_Woverflow,
4405 "floating literal exceeds range of %qT type",
4406 long_double_type_node);
4407 else if (overflow < 0)
4408 warning_at (token->location, OPT_Woverflow,
4409 "floating literal truncated to zero");
4412 release_tree_vector (args);
4413 return result;
4415 release_tree_vector (args);
4417 /* If the numeric argument didn't work, look for a raw literal
4418 operator taking a const char* argument consisting of the number
4419 in string format. */
4420 args = make_tree_vector ();
4421 vec_safe_push (args, num_string);
4422 decl = lookup_literal_operator (name, args);
4423 if (decl && decl != error_mark_node)
4425 result = finish_call_expr (decl, &args, false, true,
4426 tf_warning_or_error);
4427 release_tree_vector (args);
4428 return result;
4430 release_tree_vector (args);
4432 /* If the raw literal didn't work, look for a non-type template
4433 function with parameter pack char.... Call the function with
4434 template parameter characters representing the number. */
4435 args = make_tree_vector ();
4436 decl = lookup_literal_operator (name, args);
4437 if (decl && decl != error_mark_node)
4439 tree tmpl_args = make_char_string_pack (num_string);
4440 if (tmpl_args == NULL_TREE)
4442 error ("failed to translate literal to execution character set %qT",
4443 num_string);
4444 return error_mark_node;
4446 decl = lookup_template_function (decl, tmpl_args);
4447 result = finish_call_expr (decl, &args, false, true,
4448 tf_warning_or_error);
4449 release_tree_vector (args);
4450 return result;
4453 release_tree_vector (args);
4455 /* In C++14 the standard library defines complex number suffixes that
4456 conflict with GNU extensions. Prefer them if <complex> is #included. */
4457 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4458 bool i14 = (cxx_dialect > cxx11
4459 && (id_equal (suffix_id, "i")
4460 || id_equal (suffix_id, "if")
4461 || id_equal (suffix_id, "il")));
4462 diagnostic_t kind = DK_ERROR;
4463 int opt = 0;
4465 if (i14 && ext)
4467 tree cxlit = lookup_qualified_name (std_node,
4468 get_identifier ("complex_literals"),
4469 0, false, false);
4470 if (cxlit == error_mark_node)
4472 /* No <complex>, so pedwarn and use GNU semantics. */
4473 kind = DK_PEDWARN;
4474 opt = OPT_Wpedantic;
4478 bool complained
4479 = emit_diagnostic (kind, input_location, opt,
4480 "unable to find numeric literal operator %qD", name);
4482 if (!complained)
4483 /* Don't inform either. */;
4484 else if (i14)
4486 inform (token->location, "add %<using namespace std::complex_literals%> "
4487 "(from <complex>) to enable the C++14 user-defined literal "
4488 "suffixes");
4489 if (ext)
4490 inform (token->location, "or use %<j%> instead of %<i%> for the "
4491 "GNU built-in suffix");
4493 else if (!ext)
4494 inform (token->location, "use -fext-numeric-literals "
4495 "to enable more built-in suffixes");
4497 if (kind == DK_ERROR)
4498 value = error_mark_node;
4499 else
4501 /* Use the built-in semantics. */
4502 tree type;
4503 if (id_equal (suffix_id, "i"))
4505 if (TREE_CODE (value) == INTEGER_CST)
4506 type = integer_type_node;
4507 else
4508 type = double_type_node;
4510 else if (id_equal (suffix_id, "if"))
4511 type = float_type_node;
4512 else /* if (id_equal (suffix_id, "il")) */
4513 type = long_double_type_node;
4515 value = build_complex (build_complex_type (type),
4516 fold_convert (type, integer_zero_node),
4517 fold_convert (type, value));
4520 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4521 /* Avoid repeated diagnostics. */
4522 token->u.value = value;
4523 return value;
4526 /* Parse a user-defined string constant. Returns a call to a user-defined
4527 literal operator taking a character pointer and the length of the string
4528 as arguments. */
4530 static tree
4531 cp_parser_userdef_string_literal (tree literal)
4533 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4534 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4535 tree value = USERDEF_LITERAL_VALUE (literal);
4536 int len = TREE_STRING_LENGTH (value)
4537 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4538 tree decl, result;
4539 vec<tree, va_gc> *args;
4541 /* Build up a call to the user-defined operator. */
4542 /* Lookup the name we got back from the id-expression. */
4543 args = make_tree_vector ();
4544 vec_safe_push (args, value);
4545 vec_safe_push (args, build_int_cst (size_type_node, len));
4546 decl = lookup_literal_operator (name, args);
4548 if (decl && decl != error_mark_node)
4550 result = finish_call_expr (decl, &args, false, true,
4551 tf_warning_or_error);
4552 release_tree_vector (args);
4553 return result;
4555 release_tree_vector (args);
4557 /* Look for a template function with typename parameter CharT
4558 and parameter pack CharT... Call the function with
4559 template parameter characters representing the string. */
4560 args = make_tree_vector ();
4561 decl = lookup_literal_operator (name, args);
4562 if (decl && decl != error_mark_node)
4564 tree tmpl_args = make_string_pack (value);
4565 decl = lookup_template_function (decl, tmpl_args);
4566 result = finish_call_expr (decl, &args, false, true,
4567 tf_warning_or_error);
4568 release_tree_vector (args);
4569 return result;
4571 release_tree_vector (args);
4573 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4574 name, TREE_TYPE (value), size_type_node);
4575 return error_mark_node;
4579 /* Basic concepts [gram.basic] */
4581 /* Parse a translation-unit.
4583 translation-unit:
4584 declaration-seq [opt] */
4586 static void
4587 cp_parser_translation_unit (cp_parser* parser)
4589 gcc_checking_assert (!cp_error_declarator);
4591 /* Create the declarator obstack. */
4592 gcc_obstack_init (&declarator_obstack);
4593 /* Create the error declarator. */
4594 cp_error_declarator = make_declarator (cdk_error);
4595 /* Create the empty parameter list. */
4596 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4597 UNKNOWN_LOCATION);
4598 /* Remember where the base of the declarator obstack lies. */
4599 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4601 bool implicit_extern_c = false;
4603 for (;;)
4605 cp_token *token = cp_lexer_peek_token (parser->lexer);
4607 /* If we're entering or exiting a region that's implicitly
4608 extern "C", modify the lang context appropriately. */
4609 if (implicit_extern_c
4610 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4612 implicit_extern_c = !implicit_extern_c;
4613 if (implicit_extern_c)
4614 push_lang_context (lang_name_c);
4615 else
4616 pop_lang_context ();
4619 if (token->type == CPP_EOF)
4620 break;
4622 if (token->type == CPP_CLOSE_BRACE)
4624 cp_parser_error (parser, "expected declaration");
4625 cp_lexer_consume_token (parser->lexer);
4626 /* If the next token is now a `;', consume it. */
4627 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4628 cp_lexer_consume_token (parser->lexer);
4630 else
4631 cp_parser_toplevel_declaration (parser);
4634 /* Get rid of the token array; we don't need it any more. */
4635 cp_lexer_destroy (parser->lexer);
4636 parser->lexer = NULL;
4638 /* The EOF should have reset this. */
4639 gcc_checking_assert (!implicit_extern_c);
4641 /* Make sure the declarator obstack was fully cleaned up. */
4642 gcc_assert (obstack_next_free (&declarator_obstack)
4643 == declarator_obstack_base);
4646 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4647 decltype context. */
4649 static inline tsubst_flags_t
4650 complain_flags (bool decltype_p)
4652 tsubst_flags_t complain = tf_warning_or_error;
4653 if (decltype_p)
4654 complain |= tf_decltype;
4655 return complain;
4658 /* We're about to parse a collection of statements. If we're currently
4659 parsing tentatively, set up a firewall so that any nested
4660 cp_parser_commit_to_tentative_parse won't affect the current context. */
4662 static cp_token_position
4663 cp_parser_start_tentative_firewall (cp_parser *parser)
4665 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4666 return 0;
4668 cp_parser_parse_tentatively (parser);
4669 cp_parser_commit_to_topmost_tentative_parse (parser);
4670 return cp_lexer_token_position (parser->lexer, false);
4673 /* We've finished parsing the collection of statements. Wrap up the
4674 firewall and replace the relevant tokens with the parsed form. */
4676 static void
4677 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4678 tree expr)
4680 if (!start)
4681 return;
4683 /* Finish the firewall level. */
4684 cp_parser_parse_definitely (parser);
4685 /* And remember the result of the parse for when we try again. */
4686 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4687 token->type = CPP_PREPARSED_EXPR;
4688 token->u.value = expr;
4689 token->keyword = RID_MAX;
4690 cp_lexer_purge_tokens_after (parser->lexer, start);
4693 /* Like the above functions, but let the user modify the tokens. Used by
4694 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4695 later parses, so it makes sense to localize the effects of
4696 cp_parser_commit_to_tentative_parse. */
4698 struct tentative_firewall
4700 cp_parser *parser;
4701 bool set;
4703 tentative_firewall (cp_parser *p): parser(p)
4705 /* If we're currently parsing tentatively, start a committed level as a
4706 firewall and then an inner tentative parse. */
4707 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4709 cp_parser_parse_tentatively (parser);
4710 cp_parser_commit_to_topmost_tentative_parse (parser);
4711 cp_parser_parse_tentatively (parser);
4715 ~tentative_firewall()
4717 if (set)
4719 /* Finish the inner tentative parse and the firewall, propagating any
4720 uncommitted error state to the outer tentative parse. */
4721 bool err = cp_parser_error_occurred (parser);
4722 cp_parser_parse_definitely (parser);
4723 cp_parser_parse_definitely (parser);
4724 if (err)
4725 cp_parser_simulate_error (parser);
4730 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4731 This class is for tracking such a matching pair of symbols.
4732 In particular, it tracks the location of the first token,
4733 so that if the second token is missing, we can highlight the
4734 location of the first token when notifying the user about the
4735 problem. */
4737 template <typename traits_t>
4738 class token_pair
4740 public:
4741 /* token_pair's ctor. */
4742 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4744 /* If the next token is the opening symbol for this pair, consume it and
4745 return true.
4746 Otherwise, issue an error and return false.
4747 In either case, record the location of the opening token. */
4749 bool require_open (cp_parser *parser)
4751 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4752 return cp_parser_require (parser, traits_t::open_token_type,
4753 traits_t::required_token_open);
4756 /* Consume the next token from PARSER, recording its location as
4757 that of the opening token within the pair. */
4759 cp_token * consume_open (cp_parser *parser)
4761 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4762 gcc_assert (tok->type == traits_t::open_token_type);
4763 m_open_loc = tok->location;
4764 return tok;
4767 /* If the next token is the closing symbol for this pair, consume it
4768 and return it.
4769 Otherwise, issue an error, highlighting the location of the
4770 corresponding opening token, and return NULL. */
4772 cp_token *require_close (cp_parser *parser) const
4774 return cp_parser_require (parser, traits_t::close_token_type,
4775 traits_t::required_token_close,
4776 m_open_loc);
4779 private:
4780 location_t m_open_loc;
4783 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4785 struct matching_paren_traits
4787 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4788 static const enum required_token required_token_open = RT_OPEN_PAREN;
4789 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4790 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4793 /* "matching_parens" is a token_pair<T> class for tracking matching
4794 pairs of parentheses. */
4796 typedef token_pair<matching_paren_traits> matching_parens;
4798 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4800 struct matching_brace_traits
4802 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4803 static const enum required_token required_token_open = RT_OPEN_BRACE;
4804 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4805 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4808 /* "matching_braces" is a token_pair<T> class for tracking matching
4809 pairs of braces. */
4811 typedef token_pair<matching_brace_traits> matching_braces;
4814 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4815 enclosing parentheses. */
4817 static cp_expr
4818 cp_parser_statement_expr (cp_parser *parser)
4820 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4822 /* Consume the '('. */
4823 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4824 matching_parens parens;
4825 parens.consume_open (parser);
4826 /* Start the statement-expression. */
4827 tree expr = begin_stmt_expr ();
4828 /* Parse the compound-statement. */
4829 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4830 /* Finish up. */
4831 expr = finish_stmt_expr (expr, false);
4832 /* Consume the ')'. */
4833 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4834 if (!parens.require_close (parser))
4835 cp_parser_skip_to_end_of_statement (parser);
4837 cp_parser_end_tentative_firewall (parser, start, expr);
4838 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4839 return cp_expr (expr, combined_loc);
4842 /* Expressions [gram.expr] */
4844 /* Parse a fold-operator.
4846 fold-operator:
4847 - * / % ^ & | = < > << >>
4848 = -= *= /= %= ^= &= |= <<= >>=
4849 == != <= >= && || , .* ->*
4851 This returns the tree code corresponding to the matched operator
4852 as an int. When the current token matches a compound assignment
4853 opertor, the resulting tree code is the negative value of the
4854 non-assignment operator. */
4856 static int
4857 cp_parser_fold_operator (cp_token *token)
4859 switch (token->type)
4861 case CPP_PLUS: return PLUS_EXPR;
4862 case CPP_MINUS: return MINUS_EXPR;
4863 case CPP_MULT: return MULT_EXPR;
4864 case CPP_DIV: return TRUNC_DIV_EXPR;
4865 case CPP_MOD: return TRUNC_MOD_EXPR;
4866 case CPP_XOR: return BIT_XOR_EXPR;
4867 case CPP_AND: return BIT_AND_EXPR;
4868 case CPP_OR: return BIT_IOR_EXPR;
4869 case CPP_LSHIFT: return LSHIFT_EXPR;
4870 case CPP_RSHIFT: return RSHIFT_EXPR;
4872 case CPP_EQ: return -NOP_EXPR;
4873 case CPP_PLUS_EQ: return -PLUS_EXPR;
4874 case CPP_MINUS_EQ: return -MINUS_EXPR;
4875 case CPP_MULT_EQ: return -MULT_EXPR;
4876 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4877 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4878 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4879 case CPP_AND_EQ: return -BIT_AND_EXPR;
4880 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4881 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4882 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4884 case CPP_EQ_EQ: return EQ_EXPR;
4885 case CPP_NOT_EQ: return NE_EXPR;
4886 case CPP_LESS: return LT_EXPR;
4887 case CPP_GREATER: return GT_EXPR;
4888 case CPP_LESS_EQ: return LE_EXPR;
4889 case CPP_GREATER_EQ: return GE_EXPR;
4891 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4892 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4894 case CPP_COMMA: return COMPOUND_EXPR;
4896 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4897 case CPP_DEREF_STAR: return MEMBER_REF;
4899 default: return ERROR_MARK;
4903 /* Returns true if CODE indicates a binary expression, which is not allowed in
4904 the LHS of a fold-expression. More codes will need to be added to use this
4905 function in other contexts. */
4907 static bool
4908 is_binary_op (tree_code code)
4910 switch (code)
4912 case PLUS_EXPR:
4913 case POINTER_PLUS_EXPR:
4914 case MINUS_EXPR:
4915 case MULT_EXPR:
4916 case TRUNC_DIV_EXPR:
4917 case TRUNC_MOD_EXPR:
4918 case BIT_XOR_EXPR:
4919 case BIT_AND_EXPR:
4920 case BIT_IOR_EXPR:
4921 case LSHIFT_EXPR:
4922 case RSHIFT_EXPR:
4924 case MODOP_EXPR:
4926 case EQ_EXPR:
4927 case NE_EXPR:
4928 case LE_EXPR:
4929 case GE_EXPR:
4930 case LT_EXPR:
4931 case GT_EXPR:
4933 case TRUTH_ANDIF_EXPR:
4934 case TRUTH_ORIF_EXPR:
4936 case COMPOUND_EXPR:
4938 case DOTSTAR_EXPR:
4939 case MEMBER_REF:
4940 return true;
4942 default:
4943 return false;
4947 /* If the next token is a suitable fold operator, consume it and return as
4948 the function above. */
4950 static int
4951 cp_parser_fold_operator (cp_parser *parser)
4953 cp_token* token = cp_lexer_peek_token (parser->lexer);
4954 int code = cp_parser_fold_operator (token);
4955 if (code != ERROR_MARK)
4956 cp_lexer_consume_token (parser->lexer);
4957 return code;
4960 /* Parse a fold-expression.
4962 fold-expression:
4963 ( ... folding-operator cast-expression)
4964 ( cast-expression folding-operator ... )
4965 ( cast-expression folding operator ... folding-operator cast-expression)
4967 Note that the '(' and ')' are matched in primary expression. */
4969 static cp_expr
4970 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4972 cp_id_kind pidk;
4974 // Left fold.
4975 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4977 cp_lexer_consume_token (parser->lexer);
4978 int op = cp_parser_fold_operator (parser);
4979 if (op == ERROR_MARK)
4981 cp_parser_error (parser, "expected binary operator");
4982 return error_mark_node;
4985 tree expr = cp_parser_cast_expression (parser, false, false,
4986 false, &pidk);
4987 if (expr == error_mark_node)
4988 return error_mark_node;
4989 return finish_left_unary_fold_expr (expr, op);
4992 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4993 int op = cp_parser_fold_operator (parser);
4994 if (op == ERROR_MARK)
4996 cp_parser_error (parser, "expected binary operator");
4997 return error_mark_node;
5000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5002 cp_parser_error (parser, "expected ...");
5003 return error_mark_node;
5005 cp_lexer_consume_token (parser->lexer);
5007 /* The operands of a fold-expression are cast-expressions, so binary or
5008 conditional expressions are not allowed. We check this here to avoid
5009 tentative parsing. */
5010 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5011 /* OK, the expression was parenthesized. */;
5012 else if (is_binary_op (TREE_CODE (expr1)))
5013 error_at (location_of (expr1),
5014 "binary expression in operand of fold-expression");
5015 else if (TREE_CODE (expr1) == COND_EXPR
5016 || (REFERENCE_REF_P (expr1)
5017 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5018 error_at (location_of (expr1),
5019 "conditional expression in operand of fold-expression");
5021 // Right fold.
5022 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5023 return finish_right_unary_fold_expr (expr1, op);
5025 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5027 cp_parser_error (parser, "mismatched operator in fold-expression");
5028 return error_mark_node;
5030 cp_lexer_consume_token (parser->lexer);
5032 // Binary left or right fold.
5033 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5034 if (expr2 == error_mark_node)
5035 return error_mark_node;
5036 return finish_binary_fold_expr (expr1, expr2, op);
5039 /* Parse a primary-expression.
5041 primary-expression:
5042 literal
5043 this
5044 ( expression )
5045 id-expression
5046 lambda-expression (C++11)
5048 GNU Extensions:
5050 primary-expression:
5051 ( compound-statement )
5052 __builtin_va_arg ( assignment-expression , type-id )
5053 __builtin_offsetof ( type-id , offsetof-expression )
5055 C++ Extensions:
5056 __has_nothrow_assign ( type-id )
5057 __has_nothrow_constructor ( type-id )
5058 __has_nothrow_copy ( type-id )
5059 __has_trivial_assign ( type-id )
5060 __has_trivial_constructor ( type-id )
5061 __has_trivial_copy ( type-id )
5062 __has_trivial_destructor ( type-id )
5063 __has_virtual_destructor ( type-id )
5064 __is_abstract ( type-id )
5065 __is_base_of ( type-id , type-id )
5066 __is_class ( type-id )
5067 __is_empty ( type-id )
5068 __is_enum ( type-id )
5069 __is_final ( type-id )
5070 __is_literal_type ( type-id )
5071 __is_pod ( type-id )
5072 __is_polymorphic ( type-id )
5073 __is_std_layout ( type-id )
5074 __is_trivial ( type-id )
5075 __is_union ( type-id )
5077 Objective-C++ Extension:
5079 primary-expression:
5080 objc-expression
5082 literal:
5083 __null
5085 ADDRESS_P is true iff this expression was immediately preceded by
5086 "&" and therefore might denote a pointer-to-member. CAST_P is true
5087 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5088 true iff this expression is a template argument.
5090 Returns a representation of the expression. Upon return, *IDK
5091 indicates what kind of id-expression (if any) was present. */
5093 static cp_expr
5094 cp_parser_primary_expression (cp_parser *parser,
5095 bool address_p,
5096 bool cast_p,
5097 bool template_arg_p,
5098 bool decltype_p,
5099 cp_id_kind *idk)
5101 cp_token *token = NULL;
5103 /* Assume the primary expression is not an id-expression. */
5104 *idk = CP_ID_KIND_NONE;
5106 /* Peek at the next token. */
5107 token = cp_lexer_peek_token (parser->lexer);
5108 switch ((int) token->type)
5110 /* literal:
5111 integer-literal
5112 character-literal
5113 floating-literal
5114 string-literal
5115 boolean-literal
5116 pointer-literal
5117 user-defined-literal */
5118 case CPP_CHAR:
5119 case CPP_CHAR16:
5120 case CPP_CHAR32:
5121 case CPP_WCHAR:
5122 case CPP_UTF8CHAR:
5123 case CPP_NUMBER:
5124 case CPP_PREPARSED_EXPR:
5125 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5126 return cp_parser_userdef_numeric_literal (parser);
5127 token = cp_lexer_consume_token (parser->lexer);
5128 if (TREE_CODE (token->u.value) == FIXED_CST)
5130 error_at (token->location,
5131 "fixed-point types not supported in C++");
5132 return error_mark_node;
5134 /* Floating-point literals are only allowed in an integral
5135 constant expression if they are cast to an integral or
5136 enumeration type. */
5137 if (TREE_CODE (token->u.value) == REAL_CST
5138 && parser->integral_constant_expression_p
5139 && pedantic)
5141 /* CAST_P will be set even in invalid code like "int(2.7 +
5142 ...)". Therefore, we have to check that the next token
5143 is sure to end the cast. */
5144 if (cast_p)
5146 cp_token *next_token;
5148 next_token = cp_lexer_peek_token (parser->lexer);
5149 if (/* The comma at the end of an
5150 enumerator-definition. */
5151 next_token->type != CPP_COMMA
5152 /* The curly brace at the end of an enum-specifier. */
5153 && next_token->type != CPP_CLOSE_BRACE
5154 /* The end of a statement. */
5155 && next_token->type != CPP_SEMICOLON
5156 /* The end of the cast-expression. */
5157 && next_token->type != CPP_CLOSE_PAREN
5158 /* The end of an array bound. */
5159 && next_token->type != CPP_CLOSE_SQUARE
5160 /* The closing ">" in a template-argument-list. */
5161 && (next_token->type != CPP_GREATER
5162 || parser->greater_than_is_operator_p)
5163 /* C++0x only: A ">>" treated like two ">" tokens,
5164 in a template-argument-list. */
5165 && (next_token->type != CPP_RSHIFT
5166 || (cxx_dialect == cxx98)
5167 || parser->greater_than_is_operator_p))
5168 cast_p = false;
5171 /* If we are within a cast, then the constraint that the
5172 cast is to an integral or enumeration type will be
5173 checked at that point. If we are not within a cast, then
5174 this code is invalid. */
5175 if (!cast_p)
5176 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5178 return cp_expr (token->u.value, token->location);
5180 case CPP_CHAR_USERDEF:
5181 case CPP_CHAR16_USERDEF:
5182 case CPP_CHAR32_USERDEF:
5183 case CPP_WCHAR_USERDEF:
5184 case CPP_UTF8CHAR_USERDEF:
5185 return cp_parser_userdef_char_literal (parser);
5187 case CPP_STRING:
5188 case CPP_STRING16:
5189 case CPP_STRING32:
5190 case CPP_WSTRING:
5191 case CPP_UTF8STRING:
5192 case CPP_STRING_USERDEF:
5193 case CPP_STRING16_USERDEF:
5194 case CPP_STRING32_USERDEF:
5195 case CPP_WSTRING_USERDEF:
5196 case CPP_UTF8STRING_USERDEF:
5197 /* ??? Should wide strings be allowed when parser->translate_strings_p
5198 is false (i.e. in attributes)? If not, we can kill the third
5199 argument to cp_parser_string_literal. */
5200 return cp_parser_string_literal (parser,
5201 parser->translate_strings_p,
5202 true);
5204 case CPP_OPEN_PAREN:
5205 /* If we see `( { ' then we are looking at the beginning of
5206 a GNU statement-expression. */
5207 if (cp_parser_allow_gnu_extensions_p (parser)
5208 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5210 /* Statement-expressions are not allowed by the standard. */
5211 pedwarn (token->location, OPT_Wpedantic,
5212 "ISO C++ forbids braced-groups within expressions");
5214 /* And they're not allowed outside of a function-body; you
5215 cannot, for example, write:
5217 int i = ({ int j = 3; j + 1; });
5219 at class or namespace scope. */
5220 if (!parser->in_function_body
5221 || parser->in_template_argument_list_p)
5223 error_at (token->location,
5224 "statement-expressions are not allowed outside "
5225 "functions nor in template-argument lists");
5226 cp_parser_skip_to_end_of_block_or_statement (parser);
5227 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5228 cp_lexer_consume_token (parser->lexer);
5229 return error_mark_node;
5231 else
5232 return cp_parser_statement_expr (parser);
5234 /* Otherwise it's a normal parenthesized expression. */
5236 cp_expr expr;
5237 bool saved_greater_than_is_operator_p;
5239 location_t open_paren_loc = token->location;
5241 /* Consume the `('. */
5242 matching_parens parens;
5243 parens.consume_open (parser);
5244 /* Within a parenthesized expression, a `>' token is always
5245 the greater-than operator. */
5246 saved_greater_than_is_operator_p
5247 = parser->greater_than_is_operator_p;
5248 parser->greater_than_is_operator_p = true;
5250 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5251 /* Left fold expression. */
5252 expr = NULL_TREE;
5253 else
5254 /* Parse the parenthesized expression. */
5255 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5257 token = cp_lexer_peek_token (parser->lexer);
5258 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5260 expr = cp_parser_fold_expression (parser, expr);
5261 if (expr != error_mark_node
5262 && cxx_dialect < cxx17
5263 && !in_system_header_at (input_location))
5264 pedwarn (input_location, 0, "fold-expressions only available "
5265 "with -std=c++17 or -std=gnu++17");
5267 else
5268 /* Let the front end know that this expression was
5269 enclosed in parentheses. This matters in case, for
5270 example, the expression is of the form `A::B', since
5271 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5272 not. */
5273 expr = finish_parenthesized_expr (expr);
5275 /* DR 705: Wrapping an unqualified name in parentheses
5276 suppresses arg-dependent lookup. We want to pass back
5277 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5278 (c++/37862), but none of the others. */
5279 if (*idk != CP_ID_KIND_QUALIFIED)
5280 *idk = CP_ID_KIND_NONE;
5282 /* The `>' token might be the end of a template-id or
5283 template-parameter-list now. */
5284 parser->greater_than_is_operator_p
5285 = saved_greater_than_is_operator_p;
5287 /* Consume the `)'. */
5288 token = cp_lexer_peek_token (parser->lexer);
5289 location_t close_paren_loc = token->location;
5290 expr.set_range (open_paren_loc, close_paren_loc);
5291 if (!parens.require_close (parser)
5292 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5293 cp_parser_skip_to_end_of_statement (parser);
5295 return expr;
5298 case CPP_OPEN_SQUARE:
5300 if (c_dialect_objc ())
5302 /* We might have an Objective-C++ message. */
5303 cp_parser_parse_tentatively (parser);
5304 tree msg = cp_parser_objc_message_expression (parser);
5305 /* If that works out, we're done ... */
5306 if (cp_parser_parse_definitely (parser))
5307 return msg;
5308 /* ... else, fall though to see if it's a lambda. */
5310 cp_expr lam = cp_parser_lambda_expression (parser);
5311 /* Don't warn about a failed tentative parse. */
5312 if (cp_parser_error_occurred (parser))
5313 return error_mark_node;
5314 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5315 return lam;
5318 case CPP_OBJC_STRING:
5319 if (c_dialect_objc ())
5320 /* We have an Objective-C++ string literal. */
5321 return cp_parser_objc_expression (parser);
5322 cp_parser_error (parser, "expected primary-expression");
5323 return error_mark_node;
5325 case CPP_KEYWORD:
5326 switch (token->keyword)
5328 /* These two are the boolean literals. */
5329 case RID_TRUE:
5330 cp_lexer_consume_token (parser->lexer);
5331 return cp_expr (boolean_true_node, token->location);
5332 case RID_FALSE:
5333 cp_lexer_consume_token (parser->lexer);
5334 return cp_expr (boolean_false_node, token->location);
5336 /* The `__null' literal. */
5337 case RID_NULL:
5338 cp_lexer_consume_token (parser->lexer);
5339 return cp_expr (null_node, token->location);
5341 /* The `nullptr' literal. */
5342 case RID_NULLPTR:
5343 cp_lexer_consume_token (parser->lexer);
5344 return cp_expr (nullptr_node, token->location);
5346 /* Recognize the `this' keyword. */
5347 case RID_THIS:
5348 cp_lexer_consume_token (parser->lexer);
5349 if (parser->local_variables_forbidden_p)
5351 error_at (token->location,
5352 "%<this%> may not be used in this context");
5353 return error_mark_node;
5355 /* Pointers cannot appear in constant-expressions. */
5356 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5357 return error_mark_node;
5358 return cp_expr (finish_this_expr (), token->location);
5360 /* The `operator' keyword can be the beginning of an
5361 id-expression. */
5362 case RID_OPERATOR:
5363 goto id_expression;
5365 case RID_FUNCTION_NAME:
5366 case RID_PRETTY_FUNCTION_NAME:
5367 case RID_C99_FUNCTION_NAME:
5369 non_integral_constant name;
5371 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5372 __func__ are the names of variables -- but they are
5373 treated specially. Therefore, they are handled here,
5374 rather than relying on the generic id-expression logic
5375 below. Grammatically, these names are id-expressions.
5377 Consume the token. */
5378 token = cp_lexer_consume_token (parser->lexer);
5380 switch (token->keyword)
5382 case RID_FUNCTION_NAME:
5383 name = NIC_FUNC_NAME;
5384 break;
5385 case RID_PRETTY_FUNCTION_NAME:
5386 name = NIC_PRETTY_FUNC;
5387 break;
5388 case RID_C99_FUNCTION_NAME:
5389 name = NIC_C99_FUNC;
5390 break;
5391 default:
5392 gcc_unreachable ();
5395 if (cp_parser_non_integral_constant_expression (parser, name))
5396 return error_mark_node;
5398 /* Look up the name. */
5399 return finish_fname (token->u.value);
5402 case RID_VA_ARG:
5404 tree expression;
5405 tree type;
5406 source_location type_location;
5407 location_t start_loc
5408 = cp_lexer_peek_token (parser->lexer)->location;
5409 /* The `__builtin_va_arg' construct is used to handle
5410 `va_arg'. Consume the `__builtin_va_arg' token. */
5411 cp_lexer_consume_token (parser->lexer);
5412 /* Look for the opening `('. */
5413 matching_parens parens;
5414 parens.require_open (parser);
5415 /* Now, parse the assignment-expression. */
5416 expression = cp_parser_assignment_expression (parser);
5417 /* Look for the `,'. */
5418 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5419 type_location = cp_lexer_peek_token (parser->lexer)->location;
5420 /* Parse the type-id. */
5422 type_id_in_expr_sentinel s (parser);
5423 type = cp_parser_type_id (parser);
5425 /* Look for the closing `)'. */
5426 location_t finish_loc
5427 = cp_lexer_peek_token (parser->lexer)->location;
5428 parens.require_close (parser);
5429 /* Using `va_arg' in a constant-expression is not
5430 allowed. */
5431 if (cp_parser_non_integral_constant_expression (parser,
5432 NIC_VA_ARG))
5433 return error_mark_node;
5434 /* Construct a location of the form:
5435 __builtin_va_arg (v, int)
5436 ~~~~~~~~~~~~~~~~~~~~~^~~~
5437 with the caret at the type, ranging from the start of the
5438 "__builtin_va_arg" token to the close paren. */
5439 location_t combined_loc
5440 = make_location (type_location, start_loc, finish_loc);
5441 return build_x_va_arg (combined_loc, expression, type);
5444 case RID_OFFSETOF:
5445 return cp_parser_builtin_offsetof (parser);
5447 case RID_HAS_NOTHROW_ASSIGN:
5448 case RID_HAS_NOTHROW_CONSTRUCTOR:
5449 case RID_HAS_NOTHROW_COPY:
5450 case RID_HAS_TRIVIAL_ASSIGN:
5451 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5452 case RID_HAS_TRIVIAL_COPY:
5453 case RID_HAS_TRIVIAL_DESTRUCTOR:
5454 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5455 case RID_HAS_VIRTUAL_DESTRUCTOR:
5456 case RID_IS_ABSTRACT:
5457 case RID_IS_AGGREGATE:
5458 case RID_IS_BASE_OF:
5459 case RID_IS_CLASS:
5460 case RID_IS_EMPTY:
5461 case RID_IS_ENUM:
5462 case RID_IS_FINAL:
5463 case RID_IS_LITERAL_TYPE:
5464 case RID_IS_POD:
5465 case RID_IS_POLYMORPHIC:
5466 case RID_IS_SAME_AS:
5467 case RID_IS_STD_LAYOUT:
5468 case RID_IS_TRIVIAL:
5469 case RID_IS_TRIVIALLY_ASSIGNABLE:
5470 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5471 case RID_IS_TRIVIALLY_COPYABLE:
5472 case RID_IS_UNION:
5473 case RID_IS_ASSIGNABLE:
5474 case RID_IS_CONSTRUCTIBLE:
5475 return cp_parser_trait_expr (parser, token->keyword);
5477 // C++ concepts
5478 case RID_REQUIRES:
5479 return cp_parser_requires_expression (parser);
5481 /* Objective-C++ expressions. */
5482 case RID_AT_ENCODE:
5483 case RID_AT_PROTOCOL:
5484 case RID_AT_SELECTOR:
5485 return cp_parser_objc_expression (parser);
5487 case RID_TEMPLATE:
5488 if (parser->in_function_body
5489 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5490 == CPP_LESS))
5492 error_at (token->location,
5493 "a template declaration cannot appear at block scope");
5494 cp_parser_skip_to_end_of_block_or_statement (parser);
5495 return error_mark_node;
5497 /* FALLTHRU */
5498 default:
5499 cp_parser_error (parser, "expected primary-expression");
5500 return error_mark_node;
5503 /* An id-expression can start with either an identifier, a
5504 `::' as the beginning of a qualified-id, or the "operator"
5505 keyword. */
5506 case CPP_NAME:
5507 case CPP_SCOPE:
5508 case CPP_TEMPLATE_ID:
5509 case CPP_NESTED_NAME_SPECIFIER:
5511 id_expression:
5512 cp_expr id_expression;
5513 cp_expr decl;
5514 const char *error_msg;
5515 bool template_p;
5516 bool done;
5517 cp_token *id_expr_token;
5519 /* Parse the id-expression. */
5520 id_expression
5521 = cp_parser_id_expression (parser,
5522 /*template_keyword_p=*/false,
5523 /*check_dependency_p=*/true,
5524 &template_p,
5525 /*declarator_p=*/false,
5526 /*optional_p=*/false);
5527 if (id_expression == error_mark_node)
5528 return error_mark_node;
5529 id_expr_token = token;
5530 token = cp_lexer_peek_token (parser->lexer);
5531 done = (token->type != CPP_OPEN_SQUARE
5532 && token->type != CPP_OPEN_PAREN
5533 && token->type != CPP_DOT
5534 && token->type != CPP_DEREF
5535 && token->type != CPP_PLUS_PLUS
5536 && token->type != CPP_MINUS_MINUS);
5537 /* If we have a template-id, then no further lookup is
5538 required. If the template-id was for a template-class, we
5539 will sometimes have a TYPE_DECL at this point. */
5540 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5541 || TREE_CODE (id_expression) == TYPE_DECL)
5542 decl = id_expression;
5543 /* Look up the name. */
5544 else
5546 tree ambiguous_decls;
5548 /* If we already know that this lookup is ambiguous, then
5549 we've already issued an error message; there's no reason
5550 to check again. */
5551 if (id_expr_token->type == CPP_NAME
5552 && id_expr_token->error_reported)
5554 cp_parser_simulate_error (parser);
5555 return error_mark_node;
5558 decl = cp_parser_lookup_name (parser, id_expression,
5559 none_type,
5560 template_p,
5561 /*is_namespace=*/false,
5562 /*check_dependency=*/true,
5563 &ambiguous_decls,
5564 id_expr_token->location);
5565 /* If the lookup was ambiguous, an error will already have
5566 been issued. */
5567 if (ambiguous_decls)
5568 return error_mark_node;
5570 /* In Objective-C++, we may have an Objective-C 2.0
5571 dot-syntax for classes here. */
5572 if (c_dialect_objc ()
5573 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5574 && TREE_CODE (decl) == TYPE_DECL
5575 && objc_is_class_name (decl))
5577 tree component;
5578 cp_lexer_consume_token (parser->lexer);
5579 component = cp_parser_identifier (parser);
5580 if (component == error_mark_node)
5581 return error_mark_node;
5583 tree result = objc_build_class_component_ref (id_expression,
5584 component);
5585 /* Build a location of the form:
5586 expr.component
5587 ~~~~~^~~~~~~~~
5588 with caret at the start of the component name (at
5589 input_location), ranging from the start of the id_expression
5590 to the end of the component name. */
5591 location_t combined_loc
5592 = make_location (input_location, id_expression.get_start (),
5593 get_finish (input_location));
5594 protected_set_expr_location (result, combined_loc);
5595 return result;
5598 /* In Objective-C++, an instance variable (ivar) may be preferred
5599 to whatever cp_parser_lookup_name() found.
5600 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5601 rest of c-family, we have to do a little extra work to preserve
5602 any location information in cp_expr "decl". Given that
5603 objc_lookup_ivar is implemented in "c-family" and "objc", we
5604 have a trip through the pure "tree" type, rather than cp_expr.
5605 Naively copying it back to "decl" would implicitly give the
5606 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5607 store an EXPR_LOCATION. Hence we only update "decl" (and
5608 hence its location_t) if we get back a different tree node. */
5609 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5610 id_expression);
5611 if (decl_tree != decl.get_value ())
5612 decl = cp_expr (decl_tree);
5614 /* If name lookup gives us a SCOPE_REF, then the
5615 qualifying scope was dependent. */
5616 if (TREE_CODE (decl) == SCOPE_REF)
5618 /* At this point, we do not know if DECL is a valid
5619 integral constant expression. We assume that it is
5620 in fact such an expression, so that code like:
5622 template <int N> struct A {
5623 int a[B<N>::i];
5626 is accepted. At template-instantiation time, we
5627 will check that B<N>::i is actually a constant. */
5628 return decl;
5630 /* Check to see if DECL is a local variable in a context
5631 where that is forbidden. */
5632 if (parser->local_variables_forbidden_p
5633 && local_variable_p (decl))
5635 error_at (id_expr_token->location,
5636 "local variable %qD may not appear in this context",
5637 decl.get_value ());
5638 return error_mark_node;
5642 if (processing_template_decl)
5643 if (tree fns = maybe_get_fns (decl))
5644 /* It's too difficult to mark ths in all the places where
5645 we know for sure we need to keep the lookup, so do it
5646 now. The cost is extra GC to recycle the lookups
5647 resolved at parse time. */
5648 lookup_keep (fns);
5650 decl = (finish_id_expression
5651 (id_expression, decl, parser->scope,
5652 idk,
5653 parser->integral_constant_expression_p,
5654 parser->allow_non_integral_constant_expression_p,
5655 &parser->non_integral_constant_expression_p,
5656 template_p, done, address_p,
5657 template_arg_p,
5658 &error_msg,
5659 id_expression.get_location ()));
5660 if (error_msg)
5661 cp_parser_error (parser, error_msg);
5662 decl.set_location (id_expr_token->location);
5663 return decl;
5666 /* Anything else is an error. */
5667 default:
5668 cp_parser_error (parser, "expected primary-expression");
5669 return error_mark_node;
5673 static inline cp_expr
5674 cp_parser_primary_expression (cp_parser *parser,
5675 bool address_p,
5676 bool cast_p,
5677 bool template_arg_p,
5678 cp_id_kind *idk)
5680 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5681 /*decltype*/false, idk);
5684 /* Parse an id-expression.
5686 id-expression:
5687 unqualified-id
5688 qualified-id
5690 qualified-id:
5691 :: [opt] nested-name-specifier template [opt] unqualified-id
5692 :: identifier
5693 :: operator-function-id
5694 :: template-id
5696 Return a representation of the unqualified portion of the
5697 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5698 a `::' or nested-name-specifier.
5700 Often, if the id-expression was a qualified-id, the caller will
5701 want to make a SCOPE_REF to represent the qualified-id. This
5702 function does not do this in order to avoid wastefully creating
5703 SCOPE_REFs when they are not required.
5705 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5706 `template' keyword.
5708 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5709 uninstantiated templates.
5711 If *TEMPLATE_P is non-NULL, it is set to true iff the
5712 `template' keyword is used to explicitly indicate that the entity
5713 named is a template.
5715 If DECLARATOR_P is true, the id-expression is appearing as part of
5716 a declarator, rather than as part of an expression. */
5718 static cp_expr
5719 cp_parser_id_expression (cp_parser *parser,
5720 bool template_keyword_p,
5721 bool check_dependency_p,
5722 bool *template_p,
5723 bool declarator_p,
5724 bool optional_p)
5726 bool global_scope_p;
5727 bool nested_name_specifier_p;
5729 /* Assume the `template' keyword was not used. */
5730 if (template_p)
5731 *template_p = template_keyword_p;
5733 /* Look for the optional `::' operator. */
5734 global_scope_p
5735 = (!template_keyword_p
5736 && (cp_parser_global_scope_opt (parser,
5737 /*current_scope_valid_p=*/false)
5738 != NULL_TREE));
5740 /* Look for the optional nested-name-specifier. */
5741 nested_name_specifier_p
5742 = (cp_parser_nested_name_specifier_opt (parser,
5743 /*typename_keyword_p=*/false,
5744 check_dependency_p,
5745 /*type_p=*/false,
5746 declarator_p,
5747 template_keyword_p)
5748 != NULL_TREE);
5750 /* If there is a nested-name-specifier, then we are looking at
5751 the first qualified-id production. */
5752 if (nested_name_specifier_p)
5754 tree saved_scope;
5755 tree saved_object_scope;
5756 tree saved_qualifying_scope;
5757 cp_expr unqualified_id;
5758 bool is_template;
5760 /* See if the next token is the `template' keyword. */
5761 if (!template_p)
5762 template_p = &is_template;
5763 *template_p = cp_parser_optional_template_keyword (parser);
5764 /* Name lookup we do during the processing of the
5765 unqualified-id might obliterate SCOPE. */
5766 saved_scope = parser->scope;
5767 saved_object_scope = parser->object_scope;
5768 saved_qualifying_scope = parser->qualifying_scope;
5769 /* Process the final unqualified-id. */
5770 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5771 check_dependency_p,
5772 declarator_p,
5773 /*optional_p=*/false);
5774 /* Restore the SAVED_SCOPE for our caller. */
5775 parser->scope = saved_scope;
5776 parser->object_scope = saved_object_scope;
5777 parser->qualifying_scope = saved_qualifying_scope;
5779 return unqualified_id;
5781 /* Otherwise, if we are in global scope, then we are looking at one
5782 of the other qualified-id productions. */
5783 else if (global_scope_p)
5785 cp_token *token;
5786 tree id;
5788 /* Peek at the next token. */
5789 token = cp_lexer_peek_token (parser->lexer);
5791 /* If it's an identifier, and the next token is not a "<", then
5792 we can avoid the template-id case. This is an optimization
5793 for this common case. */
5794 if (token->type == CPP_NAME
5795 && !cp_parser_nth_token_starts_template_argument_list_p
5796 (parser, 2))
5797 return cp_parser_identifier (parser);
5799 cp_parser_parse_tentatively (parser);
5800 /* Try a template-id. */
5801 id = cp_parser_template_id (parser,
5802 /*template_keyword_p=*/false,
5803 /*check_dependency_p=*/true,
5804 none_type,
5805 declarator_p);
5806 /* If that worked, we're done. */
5807 if (cp_parser_parse_definitely (parser))
5808 return id;
5810 /* Peek at the next token. (Changes in the token buffer may
5811 have invalidated the pointer obtained above.) */
5812 token = cp_lexer_peek_token (parser->lexer);
5814 switch (token->type)
5816 case CPP_NAME:
5817 return cp_parser_identifier (parser);
5819 case CPP_KEYWORD:
5820 if (token->keyword == RID_OPERATOR)
5821 return cp_parser_operator_function_id (parser);
5822 /* Fall through. */
5824 default:
5825 cp_parser_error (parser, "expected id-expression");
5826 return error_mark_node;
5829 else
5830 return cp_parser_unqualified_id (parser, template_keyword_p,
5831 /*check_dependency_p=*/true,
5832 declarator_p,
5833 optional_p);
5836 /* Parse an unqualified-id.
5838 unqualified-id:
5839 identifier
5840 operator-function-id
5841 conversion-function-id
5842 ~ class-name
5843 template-id
5845 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5846 keyword, in a construct like `A::template ...'.
5848 Returns a representation of unqualified-id. For the `identifier'
5849 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5850 production a BIT_NOT_EXPR is returned; the operand of the
5851 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5852 other productions, see the documentation accompanying the
5853 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5854 names are looked up in uninstantiated templates. If DECLARATOR_P
5855 is true, the unqualified-id is appearing as part of a declarator,
5856 rather than as part of an expression. */
5858 static cp_expr
5859 cp_parser_unqualified_id (cp_parser* parser,
5860 bool template_keyword_p,
5861 bool check_dependency_p,
5862 bool declarator_p,
5863 bool optional_p)
5865 cp_token *token;
5867 /* Peek at the next token. */
5868 token = cp_lexer_peek_token (parser->lexer);
5870 switch ((int) token->type)
5872 case CPP_NAME:
5874 tree id;
5876 /* We don't know yet whether or not this will be a
5877 template-id. */
5878 cp_parser_parse_tentatively (parser);
5879 /* Try a template-id. */
5880 id = cp_parser_template_id (parser, template_keyword_p,
5881 check_dependency_p,
5882 none_type,
5883 declarator_p);
5884 /* If it worked, we're done. */
5885 if (cp_parser_parse_definitely (parser))
5886 return id;
5887 /* Otherwise, it's an ordinary identifier. */
5888 return cp_parser_identifier (parser);
5891 case CPP_TEMPLATE_ID:
5892 return cp_parser_template_id (parser, template_keyword_p,
5893 check_dependency_p,
5894 none_type,
5895 declarator_p);
5897 case CPP_COMPL:
5899 tree type_decl;
5900 tree qualifying_scope;
5901 tree object_scope;
5902 tree scope;
5903 bool done;
5905 /* Consume the `~' token. */
5906 cp_lexer_consume_token (parser->lexer);
5907 /* Parse the class-name. The standard, as written, seems to
5908 say that:
5910 template <typename T> struct S { ~S (); };
5911 template <typename T> S<T>::~S() {}
5913 is invalid, since `~' must be followed by a class-name, but
5914 `S<T>' is dependent, and so not known to be a class.
5915 That's not right; we need to look in uninstantiated
5916 templates. A further complication arises from:
5918 template <typename T> void f(T t) {
5919 t.T::~T();
5922 Here, it is not possible to look up `T' in the scope of `T'
5923 itself. We must look in both the current scope, and the
5924 scope of the containing complete expression.
5926 Yet another issue is:
5928 struct S {
5929 int S;
5930 ~S();
5933 S::~S() {}
5935 The standard does not seem to say that the `S' in `~S'
5936 should refer to the type `S' and not the data member
5937 `S::S'. */
5939 /* DR 244 says that we look up the name after the "~" in the
5940 same scope as we looked up the qualifying name. That idea
5941 isn't fully worked out; it's more complicated than that. */
5942 scope = parser->scope;
5943 object_scope = parser->object_scope;
5944 qualifying_scope = parser->qualifying_scope;
5946 /* Check for invalid scopes. */
5947 if (scope == error_mark_node)
5949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5950 cp_lexer_consume_token (parser->lexer);
5951 return error_mark_node;
5953 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5955 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5956 error_at (token->location,
5957 "scope %qT before %<~%> is not a class-name",
5958 scope);
5959 cp_parser_simulate_error (parser);
5960 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5961 cp_lexer_consume_token (parser->lexer);
5962 return error_mark_node;
5964 gcc_assert (!scope || TYPE_P (scope));
5966 /* If the name is of the form "X::~X" it's OK even if X is a
5967 typedef. */
5968 token = cp_lexer_peek_token (parser->lexer);
5969 if (scope
5970 && token->type == CPP_NAME
5971 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5972 != CPP_LESS)
5973 && (token->u.value == TYPE_IDENTIFIER (scope)
5974 || (CLASS_TYPE_P (scope)
5975 && constructor_name_p (token->u.value, scope))))
5977 cp_lexer_consume_token (parser->lexer);
5978 return build_nt (BIT_NOT_EXPR, scope);
5981 /* ~auto means the destructor of whatever the object is. */
5982 if (cp_parser_is_keyword (token, RID_AUTO))
5984 if (cxx_dialect < cxx14)
5985 pedwarn (input_location, 0,
5986 "%<~auto%> only available with "
5987 "-std=c++14 or -std=gnu++14");
5988 cp_lexer_consume_token (parser->lexer);
5989 return build_nt (BIT_NOT_EXPR, make_auto ());
5992 /* If there was an explicit qualification (S::~T), first look
5993 in the scope given by the qualification (i.e., S).
5995 Note: in the calls to cp_parser_class_name below we pass
5996 typename_type so that lookup finds the injected-class-name
5997 rather than the constructor. */
5998 done = false;
5999 type_decl = NULL_TREE;
6000 if (scope)
6002 cp_parser_parse_tentatively (parser);
6003 type_decl = cp_parser_class_name (parser,
6004 /*typename_keyword_p=*/false,
6005 /*template_keyword_p=*/false,
6006 typename_type,
6007 /*check_dependency=*/false,
6008 /*class_head_p=*/false,
6009 declarator_p);
6010 if (cp_parser_parse_definitely (parser))
6011 done = true;
6013 /* In "N::S::~S", look in "N" as well. */
6014 if (!done && scope && qualifying_scope)
6016 cp_parser_parse_tentatively (parser);
6017 parser->scope = qualifying_scope;
6018 parser->object_scope = NULL_TREE;
6019 parser->qualifying_scope = NULL_TREE;
6020 type_decl
6021 = cp_parser_class_name (parser,
6022 /*typename_keyword_p=*/false,
6023 /*template_keyword_p=*/false,
6024 typename_type,
6025 /*check_dependency=*/false,
6026 /*class_head_p=*/false,
6027 declarator_p);
6028 if (cp_parser_parse_definitely (parser))
6029 done = true;
6031 /* In "p->S::~T", look in the scope given by "*p" as well. */
6032 else if (!done && object_scope)
6034 cp_parser_parse_tentatively (parser);
6035 parser->scope = object_scope;
6036 parser->object_scope = NULL_TREE;
6037 parser->qualifying_scope = NULL_TREE;
6038 type_decl
6039 = cp_parser_class_name (parser,
6040 /*typename_keyword_p=*/false,
6041 /*template_keyword_p=*/false,
6042 typename_type,
6043 /*check_dependency=*/false,
6044 /*class_head_p=*/false,
6045 declarator_p);
6046 if (cp_parser_parse_definitely (parser))
6047 done = true;
6049 /* Look in the surrounding context. */
6050 if (!done)
6052 parser->scope = NULL_TREE;
6053 parser->object_scope = NULL_TREE;
6054 parser->qualifying_scope = NULL_TREE;
6055 if (processing_template_decl)
6056 cp_parser_parse_tentatively (parser);
6057 type_decl
6058 = cp_parser_class_name (parser,
6059 /*typename_keyword_p=*/false,
6060 /*template_keyword_p=*/false,
6061 typename_type,
6062 /*check_dependency=*/false,
6063 /*class_head_p=*/false,
6064 declarator_p);
6065 if (processing_template_decl
6066 && ! cp_parser_parse_definitely (parser))
6068 /* We couldn't find a type with this name. If we're parsing
6069 tentatively, fail and try something else. */
6070 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6072 cp_parser_simulate_error (parser);
6073 return error_mark_node;
6075 /* Otherwise, accept it and check for a match at instantiation
6076 time. */
6077 type_decl = cp_parser_identifier (parser);
6078 if (type_decl != error_mark_node)
6079 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6080 return type_decl;
6083 /* If an error occurred, assume that the name of the
6084 destructor is the same as the name of the qualifying
6085 class. That allows us to keep parsing after running
6086 into ill-formed destructor names. */
6087 if (type_decl == error_mark_node && scope)
6088 return build_nt (BIT_NOT_EXPR, scope);
6089 else if (type_decl == error_mark_node)
6090 return error_mark_node;
6092 /* Check that destructor name and scope match. */
6093 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6095 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6096 error_at (token->location,
6097 "declaration of %<~%T%> as member of %qT",
6098 type_decl, scope);
6099 cp_parser_simulate_error (parser);
6100 return error_mark_node;
6103 /* [class.dtor]
6105 A typedef-name that names a class shall not be used as the
6106 identifier in the declarator for a destructor declaration. */
6107 if (declarator_p
6108 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6109 && !DECL_SELF_REFERENCE_P (type_decl)
6110 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6111 error_at (token->location,
6112 "typedef-name %qD used as destructor declarator",
6113 type_decl);
6115 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6118 case CPP_KEYWORD:
6119 if (token->keyword == RID_OPERATOR)
6121 cp_expr id;
6123 /* This could be a template-id, so we try that first. */
6124 cp_parser_parse_tentatively (parser);
6125 /* Try a template-id. */
6126 id = cp_parser_template_id (parser, template_keyword_p,
6127 /*check_dependency_p=*/true,
6128 none_type,
6129 declarator_p);
6130 /* If that worked, we're done. */
6131 if (cp_parser_parse_definitely (parser))
6132 return id;
6133 /* We still don't know whether we're looking at an
6134 operator-function-id or a conversion-function-id. */
6135 cp_parser_parse_tentatively (parser);
6136 /* Try an operator-function-id. */
6137 id = cp_parser_operator_function_id (parser);
6138 /* If that didn't work, try a conversion-function-id. */
6139 if (!cp_parser_parse_definitely (parser))
6140 id = cp_parser_conversion_function_id (parser);
6142 return id;
6144 /* Fall through. */
6146 default:
6147 if (optional_p)
6148 return NULL_TREE;
6149 cp_parser_error (parser, "expected unqualified-id");
6150 return error_mark_node;
6154 /* Parse an (optional) nested-name-specifier.
6156 nested-name-specifier: [C++98]
6157 class-or-namespace-name :: nested-name-specifier [opt]
6158 class-or-namespace-name :: template nested-name-specifier [opt]
6160 nested-name-specifier: [C++0x]
6161 type-name ::
6162 namespace-name ::
6163 nested-name-specifier identifier ::
6164 nested-name-specifier template [opt] simple-template-id ::
6166 PARSER->SCOPE should be set appropriately before this function is
6167 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6168 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6169 in name lookups.
6171 Sets PARSER->SCOPE to the class (TYPE) or namespace
6172 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6173 it unchanged if there is no nested-name-specifier. Returns the new
6174 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6176 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6177 part of a declaration and/or decl-specifier. */
6179 static tree
6180 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6181 bool typename_keyword_p,
6182 bool check_dependency_p,
6183 bool type_p,
6184 bool is_declaration,
6185 bool template_keyword_p /* = false */)
6187 bool success = false;
6188 cp_token_position start = 0;
6189 cp_token *token;
6191 /* Remember where the nested-name-specifier starts. */
6192 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6194 start = cp_lexer_token_position (parser->lexer, false);
6195 push_deferring_access_checks (dk_deferred);
6198 while (true)
6200 tree new_scope;
6201 tree old_scope;
6202 tree saved_qualifying_scope;
6204 /* Spot cases that cannot be the beginning of a
6205 nested-name-specifier. */
6206 token = cp_lexer_peek_token (parser->lexer);
6208 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6209 the already parsed nested-name-specifier. */
6210 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6212 /* Grab the nested-name-specifier and continue the loop. */
6213 cp_parser_pre_parsed_nested_name_specifier (parser);
6214 /* If we originally encountered this nested-name-specifier
6215 with IS_DECLARATION set to false, we will not have
6216 resolved TYPENAME_TYPEs, so we must do so here. */
6217 if (is_declaration
6218 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6220 new_scope = resolve_typename_type (parser->scope,
6221 /*only_current_p=*/false);
6222 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6223 parser->scope = new_scope;
6225 success = true;
6226 continue;
6229 /* Spot cases that cannot be the beginning of a
6230 nested-name-specifier. On the second and subsequent times
6231 through the loop, we look for the `template' keyword. */
6232 if (success && token->keyword == RID_TEMPLATE)
6234 /* A template-id can start a nested-name-specifier. */
6235 else if (token->type == CPP_TEMPLATE_ID)
6237 /* DR 743: decltype can be used in a nested-name-specifier. */
6238 else if (token_is_decltype (token))
6240 else
6242 /* If the next token is not an identifier, then it is
6243 definitely not a type-name or namespace-name. */
6244 if (token->type != CPP_NAME)
6245 break;
6246 /* If the following token is neither a `<' (to begin a
6247 template-id), nor a `::', then we are not looking at a
6248 nested-name-specifier. */
6249 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6251 if (token->type == CPP_COLON
6252 && parser->colon_corrects_to_scope_p
6253 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6255 gcc_rich_location richloc (token->location);
6256 richloc.add_fixit_replace ("::");
6257 error_at (&richloc,
6258 "found %<:%> in nested-name-specifier, "
6259 "expected %<::%>");
6260 token->type = CPP_SCOPE;
6263 if (token->type != CPP_SCOPE
6264 && !cp_parser_nth_token_starts_template_argument_list_p
6265 (parser, 2))
6266 break;
6269 /* The nested-name-specifier is optional, so we parse
6270 tentatively. */
6271 cp_parser_parse_tentatively (parser);
6273 /* Look for the optional `template' keyword, if this isn't the
6274 first time through the loop. */
6275 if (success)
6276 template_keyword_p = cp_parser_optional_template_keyword (parser);
6278 /* Save the old scope since the name lookup we are about to do
6279 might destroy it. */
6280 old_scope = parser->scope;
6281 saved_qualifying_scope = parser->qualifying_scope;
6282 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6283 look up names in "X<T>::I" in order to determine that "Y" is
6284 a template. So, if we have a typename at this point, we make
6285 an effort to look through it. */
6286 if (is_declaration
6287 && !typename_keyword_p
6288 && parser->scope
6289 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6290 parser->scope = resolve_typename_type (parser->scope,
6291 /*only_current_p=*/false);
6292 /* Parse the qualifying entity. */
6293 new_scope
6294 = cp_parser_qualifying_entity (parser,
6295 typename_keyword_p,
6296 template_keyword_p,
6297 check_dependency_p,
6298 type_p,
6299 is_declaration);
6300 /* Look for the `::' token. */
6301 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6303 /* If we found what we wanted, we keep going; otherwise, we're
6304 done. */
6305 if (!cp_parser_parse_definitely (parser))
6307 bool error_p = false;
6309 /* Restore the OLD_SCOPE since it was valid before the
6310 failed attempt at finding the last
6311 class-or-namespace-name. */
6312 parser->scope = old_scope;
6313 parser->qualifying_scope = saved_qualifying_scope;
6315 /* If the next token is a decltype, and the one after that is a
6316 `::', then the decltype has failed to resolve to a class or
6317 enumeration type. Give this error even when parsing
6318 tentatively since it can't possibly be valid--and we're going
6319 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6320 won't get another chance.*/
6321 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6322 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6323 == CPP_SCOPE))
6325 token = cp_lexer_consume_token (parser->lexer);
6326 error_at (token->location, "decltype evaluates to %qT, "
6327 "which is not a class or enumeration type",
6328 token->u.tree_check_value->value);
6329 parser->scope = error_mark_node;
6330 error_p = true;
6331 /* As below. */
6332 success = true;
6333 cp_lexer_consume_token (parser->lexer);
6336 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6337 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6339 /* If we have a non-type template-id followed by ::, it can't
6340 possibly be valid. */
6341 token = cp_lexer_peek_token (parser->lexer);
6342 tree tid = token->u.tree_check_value->value;
6343 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6344 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6346 tree tmpl = NULL_TREE;
6347 if (is_overloaded_fn (tid))
6349 tree fns = get_fns (tid);
6350 if (OVL_SINGLE_P (fns))
6351 tmpl = OVL_FIRST (fns);
6352 error_at (token->location, "function template-id %qD "
6353 "in nested-name-specifier", tid);
6355 else
6357 /* Variable template. */
6358 tmpl = TREE_OPERAND (tid, 0);
6359 gcc_assert (variable_template_p (tmpl));
6360 error_at (token->location, "variable template-id %qD "
6361 "in nested-name-specifier", tid);
6363 if (tmpl)
6364 inform (DECL_SOURCE_LOCATION (tmpl),
6365 "%qD declared here", tmpl);
6367 parser->scope = error_mark_node;
6368 error_p = true;
6369 /* As below. */
6370 success = true;
6371 cp_lexer_consume_token (parser->lexer);
6372 cp_lexer_consume_token (parser->lexer);
6376 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6377 break;
6378 /* If the next token is an identifier, and the one after
6379 that is a `::', then any valid interpretation would have
6380 found a class-or-namespace-name. */
6381 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6382 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6383 == CPP_SCOPE)
6384 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6385 != CPP_COMPL))
6387 token = cp_lexer_consume_token (parser->lexer);
6388 if (!error_p)
6390 if (!token->error_reported)
6392 tree decl;
6393 tree ambiguous_decls;
6395 decl = cp_parser_lookup_name (parser, token->u.value,
6396 none_type,
6397 /*is_template=*/false,
6398 /*is_namespace=*/false,
6399 /*check_dependency=*/true,
6400 &ambiguous_decls,
6401 token->location);
6402 if (TREE_CODE (decl) == TEMPLATE_DECL)
6403 error_at (token->location,
6404 "%qD used without template arguments",
6405 decl);
6406 else if (ambiguous_decls)
6408 // cp_parser_lookup_name has the same diagnostic,
6409 // thus make sure to emit it at most once.
6410 if (cp_parser_uncommitted_to_tentative_parse_p
6411 (parser))
6413 error_at (token->location,
6414 "reference to %qD is ambiguous",
6415 token->u.value);
6416 print_candidates (ambiguous_decls);
6418 decl = error_mark_node;
6420 else
6422 if (cxx_dialect != cxx98)
6423 cp_parser_name_lookup_error
6424 (parser, token->u.value, decl, NLE_NOT_CXX98,
6425 token->location);
6426 else
6427 cp_parser_name_lookup_error
6428 (parser, token->u.value, decl, NLE_CXX98,
6429 token->location);
6432 parser->scope = error_mark_node;
6433 error_p = true;
6434 /* Treat this as a successful nested-name-specifier
6435 due to:
6437 [basic.lookup.qual]
6439 If the name found is not a class-name (clause
6440 _class_) or namespace-name (_namespace.def_), the
6441 program is ill-formed. */
6442 success = true;
6444 cp_lexer_consume_token (parser->lexer);
6446 break;
6448 /* We've found one valid nested-name-specifier. */
6449 success = true;
6450 /* Name lookup always gives us a DECL. */
6451 if (TREE_CODE (new_scope) == TYPE_DECL)
6452 new_scope = TREE_TYPE (new_scope);
6453 /* Uses of "template" must be followed by actual templates. */
6454 if (template_keyword_p
6455 && !(CLASS_TYPE_P (new_scope)
6456 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6457 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6458 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6459 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6460 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6461 == TEMPLATE_ID_EXPR)))
6462 permerror (input_location, TYPE_P (new_scope)
6463 ? G_("%qT is not a template")
6464 : G_("%qD is not a template"),
6465 new_scope);
6466 /* If it is a class scope, try to complete it; we are about to
6467 be looking up names inside the class. */
6468 if (TYPE_P (new_scope)
6469 /* Since checking types for dependency can be expensive,
6470 avoid doing it if the type is already complete. */
6471 && !COMPLETE_TYPE_P (new_scope)
6472 /* Do not try to complete dependent types. */
6473 && !dependent_type_p (new_scope))
6475 new_scope = complete_type (new_scope);
6476 /* If it is a typedef to current class, use the current
6477 class instead, as the typedef won't have any names inside
6478 it yet. */
6479 if (!COMPLETE_TYPE_P (new_scope)
6480 && currently_open_class (new_scope))
6481 new_scope = TYPE_MAIN_VARIANT (new_scope);
6483 /* Make sure we look in the right scope the next time through
6484 the loop. */
6485 parser->scope = new_scope;
6488 /* If parsing tentatively, replace the sequence of tokens that makes
6489 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6490 token. That way, should we re-parse the token stream, we will
6491 not have to repeat the effort required to do the parse, nor will
6492 we issue duplicate error messages. */
6493 if (success && start)
6495 cp_token *token;
6497 token = cp_lexer_token_at (parser->lexer, start);
6498 /* Reset the contents of the START token. */
6499 token->type = CPP_NESTED_NAME_SPECIFIER;
6500 /* Retrieve any deferred checks. Do not pop this access checks yet
6501 so the memory will not be reclaimed during token replacing below. */
6502 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6503 token->u.tree_check_value->value = parser->scope;
6504 token->u.tree_check_value->checks = get_deferred_access_checks ();
6505 token->u.tree_check_value->qualifying_scope =
6506 parser->qualifying_scope;
6507 token->keyword = RID_MAX;
6509 /* Purge all subsequent tokens. */
6510 cp_lexer_purge_tokens_after (parser->lexer, start);
6513 if (start)
6514 pop_to_parent_deferring_access_checks ();
6516 return success ? parser->scope : NULL_TREE;
6519 /* Parse a nested-name-specifier. See
6520 cp_parser_nested_name_specifier_opt for details. This function
6521 behaves identically, except that it will an issue an error if no
6522 nested-name-specifier is present. */
6524 static tree
6525 cp_parser_nested_name_specifier (cp_parser *parser,
6526 bool typename_keyword_p,
6527 bool check_dependency_p,
6528 bool type_p,
6529 bool is_declaration)
6531 tree scope;
6533 /* Look for the nested-name-specifier. */
6534 scope = cp_parser_nested_name_specifier_opt (parser,
6535 typename_keyword_p,
6536 check_dependency_p,
6537 type_p,
6538 is_declaration);
6539 /* If it was not present, issue an error message. */
6540 if (!scope)
6542 cp_parser_error (parser, "expected nested-name-specifier");
6543 parser->scope = NULL_TREE;
6546 return scope;
6549 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6550 this is either a class-name or a namespace-name (which corresponds
6551 to the class-or-namespace-name production in the grammar). For
6552 C++0x, it can also be a type-name that refers to an enumeration
6553 type or a simple-template-id.
6555 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6556 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6557 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6558 TYPE_P is TRUE iff the next name should be taken as a class-name,
6559 even the same name is declared to be another entity in the same
6560 scope.
6562 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6563 specified by the class-or-namespace-name. If neither is found the
6564 ERROR_MARK_NODE is returned. */
6566 static tree
6567 cp_parser_qualifying_entity (cp_parser *parser,
6568 bool typename_keyword_p,
6569 bool template_keyword_p,
6570 bool check_dependency_p,
6571 bool type_p,
6572 bool is_declaration)
6574 tree saved_scope;
6575 tree saved_qualifying_scope;
6576 tree saved_object_scope;
6577 tree scope;
6578 bool only_class_p;
6579 bool successful_parse_p;
6581 /* DR 743: decltype can appear in a nested-name-specifier. */
6582 if (cp_lexer_next_token_is_decltype (parser->lexer))
6584 scope = cp_parser_decltype (parser);
6585 if (TREE_CODE (scope) != ENUMERAL_TYPE
6586 && !MAYBE_CLASS_TYPE_P (scope))
6588 cp_parser_simulate_error (parser);
6589 return error_mark_node;
6591 if (TYPE_NAME (scope))
6592 scope = TYPE_NAME (scope);
6593 return scope;
6596 /* Before we try to parse the class-name, we must save away the
6597 current PARSER->SCOPE since cp_parser_class_name will destroy
6598 it. */
6599 saved_scope = parser->scope;
6600 saved_qualifying_scope = parser->qualifying_scope;
6601 saved_object_scope = parser->object_scope;
6602 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6603 there is no need to look for a namespace-name. */
6604 only_class_p = template_keyword_p
6605 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6606 if (!only_class_p)
6607 cp_parser_parse_tentatively (parser);
6608 scope = cp_parser_class_name (parser,
6609 typename_keyword_p,
6610 template_keyword_p,
6611 type_p ? class_type : none_type,
6612 check_dependency_p,
6613 /*class_head_p=*/false,
6614 is_declaration,
6615 /*enum_ok=*/cxx_dialect > cxx98);
6616 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6617 /* If that didn't work, try for a namespace-name. */
6618 if (!only_class_p && !successful_parse_p)
6620 /* Restore the saved scope. */
6621 parser->scope = saved_scope;
6622 parser->qualifying_scope = saved_qualifying_scope;
6623 parser->object_scope = saved_object_scope;
6624 /* If we are not looking at an identifier followed by the scope
6625 resolution operator, then this is not part of a
6626 nested-name-specifier. (Note that this function is only used
6627 to parse the components of a nested-name-specifier.) */
6628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6629 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6630 return error_mark_node;
6631 scope = cp_parser_namespace_name (parser);
6634 return scope;
6637 /* Return true if we are looking at a compound-literal, false otherwise. */
6639 static bool
6640 cp_parser_compound_literal_p (cp_parser *parser)
6642 cp_lexer_save_tokens (parser->lexer);
6644 /* Skip tokens until the next token is a closing parenthesis.
6645 If we find the closing `)', and the next token is a `{', then
6646 we are looking at a compound-literal. */
6647 bool compound_literal_p
6648 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6649 /*consume_paren=*/true)
6650 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6652 /* Roll back the tokens we skipped. */
6653 cp_lexer_rollback_tokens (parser->lexer);
6655 return compound_literal_p;
6658 /* Return true if EXPR is the integer constant zero or a complex constant
6659 of zero, without any folding, but ignoring location wrappers. */
6661 bool
6662 literal_integer_zerop (const_tree expr)
6664 return (location_wrapper_p (expr)
6665 && integer_zerop (TREE_OPERAND (expr, 0)));
6668 /* Parse a postfix-expression.
6670 postfix-expression:
6671 primary-expression
6672 postfix-expression [ expression ]
6673 postfix-expression ( expression-list [opt] )
6674 simple-type-specifier ( expression-list [opt] )
6675 typename :: [opt] nested-name-specifier identifier
6676 ( expression-list [opt] )
6677 typename :: [opt] nested-name-specifier template [opt] template-id
6678 ( expression-list [opt] )
6679 postfix-expression . template [opt] id-expression
6680 postfix-expression -> template [opt] id-expression
6681 postfix-expression . pseudo-destructor-name
6682 postfix-expression -> pseudo-destructor-name
6683 postfix-expression ++
6684 postfix-expression --
6685 dynamic_cast < type-id > ( expression )
6686 static_cast < type-id > ( expression )
6687 reinterpret_cast < type-id > ( expression )
6688 const_cast < type-id > ( expression )
6689 typeid ( expression )
6690 typeid ( type-id )
6692 GNU Extension:
6694 postfix-expression:
6695 ( type-id ) { initializer-list , [opt] }
6697 This extension is a GNU version of the C99 compound-literal
6698 construct. (The C99 grammar uses `type-name' instead of `type-id',
6699 but they are essentially the same concept.)
6701 If ADDRESS_P is true, the postfix expression is the operand of the
6702 `&' operator. CAST_P is true if this expression is the target of a
6703 cast.
6705 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6706 class member access expressions [expr.ref].
6708 Returns a representation of the expression. */
6710 static cp_expr
6711 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6712 bool member_access_only_p, bool decltype_p,
6713 cp_id_kind * pidk_return)
6715 cp_token *token;
6716 location_t loc;
6717 enum rid keyword;
6718 cp_id_kind idk = CP_ID_KIND_NONE;
6719 cp_expr postfix_expression = NULL_TREE;
6720 bool is_member_access = false;
6722 /* Peek at the next token. */
6723 token = cp_lexer_peek_token (parser->lexer);
6724 loc = token->location;
6725 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6727 /* Some of the productions are determined by keywords. */
6728 keyword = token->keyword;
6729 switch (keyword)
6731 case RID_DYNCAST:
6732 case RID_STATCAST:
6733 case RID_REINTCAST:
6734 case RID_CONSTCAST:
6736 tree type;
6737 cp_expr expression;
6738 const char *saved_message;
6739 bool saved_in_type_id_in_expr_p;
6741 /* All of these can be handled in the same way from the point
6742 of view of parsing. Begin by consuming the token
6743 identifying the cast. */
6744 cp_lexer_consume_token (parser->lexer);
6746 /* New types cannot be defined in the cast. */
6747 saved_message = parser->type_definition_forbidden_message;
6748 parser->type_definition_forbidden_message
6749 = G_("types may not be defined in casts");
6751 /* Look for the opening `<'. */
6752 cp_parser_require (parser, CPP_LESS, RT_LESS);
6753 /* Parse the type to which we are casting. */
6754 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6755 parser->in_type_id_in_expr_p = true;
6756 type = cp_parser_type_id (parser);
6757 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6758 /* Look for the closing `>'. */
6759 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6760 /* Restore the old message. */
6761 parser->type_definition_forbidden_message = saved_message;
6763 bool saved_greater_than_is_operator_p
6764 = parser->greater_than_is_operator_p;
6765 parser->greater_than_is_operator_p = true;
6767 /* And the expression which is being cast. */
6768 matching_parens parens;
6769 parens.require_open (parser);
6770 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6771 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6772 RT_CLOSE_PAREN);
6773 location_t end_loc = close_paren ?
6774 close_paren->location : UNKNOWN_LOCATION;
6776 parser->greater_than_is_operator_p
6777 = saved_greater_than_is_operator_p;
6779 /* Only type conversions to integral or enumeration types
6780 can be used in constant-expressions. */
6781 if (!cast_valid_in_integral_constant_expression_p (type)
6782 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6784 postfix_expression = error_mark_node;
6785 break;
6788 switch (keyword)
6790 case RID_DYNCAST:
6791 postfix_expression
6792 = build_dynamic_cast (type, expression, tf_warning_or_error);
6793 break;
6794 case RID_STATCAST:
6795 postfix_expression
6796 = build_static_cast (type, expression, tf_warning_or_error);
6797 break;
6798 case RID_REINTCAST:
6799 postfix_expression
6800 = build_reinterpret_cast (type, expression,
6801 tf_warning_or_error);
6802 break;
6803 case RID_CONSTCAST:
6804 postfix_expression
6805 = build_const_cast (type, expression, tf_warning_or_error);
6806 break;
6807 default:
6808 gcc_unreachable ();
6811 /* Construct a location e.g. :
6812 reinterpret_cast <int *> (expr)
6813 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6814 ranging from the start of the "*_cast" token to the final closing
6815 paren, with the caret at the start. */
6816 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6817 postfix_expression.set_location (cp_cast_loc);
6819 break;
6821 case RID_TYPEID:
6823 tree type;
6824 const char *saved_message;
6825 bool saved_in_type_id_in_expr_p;
6827 /* Consume the `typeid' token. */
6828 cp_lexer_consume_token (parser->lexer);
6829 /* Look for the `(' token. */
6830 matching_parens parens;
6831 parens.require_open (parser);
6832 /* Types cannot be defined in a `typeid' expression. */
6833 saved_message = parser->type_definition_forbidden_message;
6834 parser->type_definition_forbidden_message
6835 = G_("types may not be defined in a %<typeid%> expression");
6836 /* We can't be sure yet whether we're looking at a type-id or an
6837 expression. */
6838 cp_parser_parse_tentatively (parser);
6839 /* Try a type-id first. */
6840 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6841 parser->in_type_id_in_expr_p = true;
6842 type = cp_parser_type_id (parser);
6843 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6844 /* Look for the `)' token. Otherwise, we can't be sure that
6845 we're not looking at an expression: consider `typeid (int
6846 (3))', for example. */
6847 cp_token *close_paren = parens.require_close (parser);
6848 /* If all went well, simply lookup the type-id. */
6849 if (cp_parser_parse_definitely (parser))
6850 postfix_expression = get_typeid (type, tf_warning_or_error);
6851 /* Otherwise, fall back to the expression variant. */
6852 else
6854 tree expression;
6856 /* Look for an expression. */
6857 expression = cp_parser_expression (parser, & idk);
6858 /* Compute its typeid. */
6859 postfix_expression = build_typeid (expression, tf_warning_or_error);
6860 /* Look for the `)' token. */
6861 close_paren = parens.require_close (parser);
6863 /* Restore the saved message. */
6864 parser->type_definition_forbidden_message = saved_message;
6865 /* `typeid' may not appear in an integral constant expression. */
6866 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6867 postfix_expression = error_mark_node;
6869 /* Construct a location e.g. :
6870 typeid (expr)
6871 ^~~~~~~~~~~~~
6872 ranging from the start of the "typeid" token to the final closing
6873 paren, with the caret at the start. */
6874 if (close_paren)
6876 location_t typeid_loc
6877 = make_location (start_loc, start_loc, close_paren->location);
6878 postfix_expression.set_location (typeid_loc);
6879 postfix_expression.maybe_add_location_wrapper ();
6882 break;
6884 case RID_TYPENAME:
6886 tree type;
6887 /* The syntax permitted here is the same permitted for an
6888 elaborated-type-specifier. */
6889 ++parser->prevent_constrained_type_specifiers;
6890 type = cp_parser_elaborated_type_specifier (parser,
6891 /*is_friend=*/false,
6892 /*is_declaration=*/false);
6893 --parser->prevent_constrained_type_specifiers;
6894 postfix_expression = cp_parser_functional_cast (parser, type);
6896 break;
6898 case RID_ADDRESSOF:
6899 case RID_BUILTIN_SHUFFLE:
6900 case RID_BUILTIN_LAUNDER:
6902 vec<tree, va_gc> *vec;
6903 unsigned int i;
6904 tree p;
6906 cp_lexer_consume_token (parser->lexer);
6907 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6908 /*cast_p=*/false, /*allow_expansion_p=*/true,
6909 /*non_constant_p=*/NULL);
6910 if (vec == NULL)
6912 postfix_expression = error_mark_node;
6913 break;
6916 FOR_EACH_VEC_ELT (*vec, i, p)
6917 mark_exp_read (p);
6919 switch (keyword)
6921 case RID_ADDRESSOF:
6922 if (vec->length () == 1)
6923 postfix_expression
6924 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6925 else
6927 error_at (loc, "wrong number of arguments to "
6928 "%<__builtin_addressof%>");
6929 postfix_expression = error_mark_node;
6931 break;
6933 case RID_BUILTIN_LAUNDER:
6934 if (vec->length () == 1)
6935 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6936 tf_warning_or_error);
6937 else
6939 error_at (loc, "wrong number of arguments to "
6940 "%<__builtin_launder%>");
6941 postfix_expression = error_mark_node;
6943 break;
6945 case RID_BUILTIN_SHUFFLE:
6946 if (vec->length () == 2)
6947 postfix_expression
6948 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6949 (*vec)[1], tf_warning_or_error);
6950 else if (vec->length () == 3)
6951 postfix_expression
6952 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6953 (*vec)[2], tf_warning_or_error);
6954 else
6956 error_at (loc, "wrong number of arguments to "
6957 "%<__builtin_shuffle%>");
6958 postfix_expression = error_mark_node;
6960 break;
6962 default:
6963 gcc_unreachable ();
6965 break;
6968 default:
6970 tree type;
6972 /* If the next thing is a simple-type-specifier, we may be
6973 looking at a functional cast. We could also be looking at
6974 an id-expression. So, we try the functional cast, and if
6975 that doesn't work we fall back to the primary-expression. */
6976 cp_parser_parse_tentatively (parser);
6977 /* Look for the simple-type-specifier. */
6978 ++parser->prevent_constrained_type_specifiers;
6979 type = cp_parser_simple_type_specifier (parser,
6980 /*decl_specs=*/NULL,
6981 CP_PARSER_FLAGS_NONE);
6982 --parser->prevent_constrained_type_specifiers;
6983 /* Parse the cast itself. */
6984 if (!cp_parser_error_occurred (parser))
6985 postfix_expression
6986 = cp_parser_functional_cast (parser, type);
6987 /* If that worked, we're done. */
6988 if (cp_parser_parse_definitely (parser))
6989 break;
6991 /* If the functional-cast didn't work out, try a
6992 compound-literal. */
6993 if (cp_parser_allow_gnu_extensions_p (parser)
6994 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6996 cp_expr initializer = NULL_TREE;
6998 cp_parser_parse_tentatively (parser);
7000 matching_parens parens;
7001 parens.consume_open (parser);
7003 /* Avoid calling cp_parser_type_id pointlessly, see comment
7004 in cp_parser_cast_expression about c++/29234. */
7005 if (!cp_parser_compound_literal_p (parser))
7006 cp_parser_simulate_error (parser);
7007 else
7009 /* Parse the type. */
7010 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7011 parser->in_type_id_in_expr_p = true;
7012 type = cp_parser_type_id (parser);
7013 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7014 parens.require_close (parser);
7017 /* If things aren't going well, there's no need to
7018 keep going. */
7019 if (!cp_parser_error_occurred (parser))
7021 bool non_constant_p;
7022 /* Parse the brace-enclosed initializer list. */
7023 initializer = cp_parser_braced_list (parser,
7024 &non_constant_p);
7026 /* If that worked, we're definitely looking at a
7027 compound-literal expression. */
7028 if (cp_parser_parse_definitely (parser))
7030 /* Warn the user that a compound literal is not
7031 allowed in standard C++. */
7032 pedwarn (input_location, OPT_Wpedantic,
7033 "ISO C++ forbids compound-literals");
7034 /* For simplicity, we disallow compound literals in
7035 constant-expressions. We could
7036 allow compound literals of integer type, whose
7037 initializer was a constant, in constant
7038 expressions. Permitting that usage, as a further
7039 extension, would not change the meaning of any
7040 currently accepted programs. (Of course, as
7041 compound literals are not part of ISO C++, the
7042 standard has nothing to say.) */
7043 if (cp_parser_non_integral_constant_expression (parser,
7044 NIC_NCC))
7046 postfix_expression = error_mark_node;
7047 break;
7049 /* Form the representation of the compound-literal. */
7050 postfix_expression
7051 = finish_compound_literal (type, initializer,
7052 tf_warning_or_error, fcl_c99);
7053 postfix_expression.set_location (initializer.get_location ());
7054 break;
7058 /* It must be a primary-expression. */
7059 postfix_expression
7060 = cp_parser_primary_expression (parser, address_p, cast_p,
7061 /*template_arg_p=*/false,
7062 decltype_p,
7063 &idk);
7065 break;
7068 /* Note that we don't need to worry about calling build_cplus_new on a
7069 class-valued CALL_EXPR in decltype when it isn't the end of the
7070 postfix-expression; unary_complex_lvalue will take care of that for
7071 all these cases. */
7073 /* Keep looping until the postfix-expression is complete. */
7074 while (true)
7076 if (idk == CP_ID_KIND_UNQUALIFIED
7077 && identifier_p (postfix_expression)
7078 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7079 /* It is not a Koenig lookup function call. */
7080 postfix_expression
7081 = unqualified_name_lookup_error (postfix_expression);
7083 /* Peek at the next token. */
7084 token = cp_lexer_peek_token (parser->lexer);
7086 switch (token->type)
7088 case CPP_OPEN_SQUARE:
7089 if (cp_next_tokens_can_be_std_attribute_p (parser))
7091 cp_parser_error (parser,
7092 "two consecutive %<[%> shall "
7093 "only introduce an attribute");
7094 return error_mark_node;
7096 postfix_expression
7097 = cp_parser_postfix_open_square_expression (parser,
7098 postfix_expression,
7099 false,
7100 decltype_p);
7101 postfix_expression.set_range (start_loc,
7102 postfix_expression.get_location ());
7104 idk = CP_ID_KIND_NONE;
7105 is_member_access = false;
7106 break;
7108 case CPP_OPEN_PAREN:
7109 /* postfix-expression ( expression-list [opt] ) */
7111 bool koenig_p;
7112 bool is_builtin_constant_p;
7113 bool saved_integral_constant_expression_p = false;
7114 bool saved_non_integral_constant_expression_p = false;
7115 tsubst_flags_t complain = complain_flags (decltype_p);
7116 vec<tree, va_gc> *args;
7117 location_t close_paren_loc = UNKNOWN_LOCATION;
7119 is_member_access = false;
7121 is_builtin_constant_p
7122 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7123 if (is_builtin_constant_p)
7125 /* The whole point of __builtin_constant_p is to allow
7126 non-constant expressions to appear as arguments. */
7127 saved_integral_constant_expression_p
7128 = parser->integral_constant_expression_p;
7129 saved_non_integral_constant_expression_p
7130 = parser->non_integral_constant_expression_p;
7131 parser->integral_constant_expression_p = false;
7133 args = (cp_parser_parenthesized_expression_list
7134 (parser, non_attr,
7135 /*cast_p=*/false, /*allow_expansion_p=*/true,
7136 /*non_constant_p=*/NULL,
7137 /*close_paren_loc=*/&close_paren_loc,
7138 /*wrap_locations_p=*/true));
7139 if (is_builtin_constant_p)
7141 parser->integral_constant_expression_p
7142 = saved_integral_constant_expression_p;
7143 parser->non_integral_constant_expression_p
7144 = saved_non_integral_constant_expression_p;
7147 if (args == NULL)
7149 postfix_expression = error_mark_node;
7150 break;
7153 /* Function calls are not permitted in
7154 constant-expressions. */
7155 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7156 && cp_parser_non_integral_constant_expression (parser,
7157 NIC_FUNC_CALL))
7159 postfix_expression = error_mark_node;
7160 release_tree_vector (args);
7161 break;
7164 koenig_p = false;
7165 if (idk == CP_ID_KIND_UNQUALIFIED
7166 || idk == CP_ID_KIND_TEMPLATE_ID)
7168 if (identifier_p (postfix_expression))
7170 if (!args->is_empty ())
7172 koenig_p = true;
7173 if (!any_type_dependent_arguments_p (args))
7174 postfix_expression
7175 = perform_koenig_lookup (postfix_expression, args,
7176 complain);
7178 else
7179 postfix_expression
7180 = unqualified_fn_lookup_error (postfix_expression);
7182 /* We do not perform argument-dependent lookup if
7183 normal lookup finds a non-function, in accordance
7184 with the expected resolution of DR 218. */
7185 else if (!args->is_empty ()
7186 && is_overloaded_fn (postfix_expression))
7188 tree fn = get_first_fn (postfix_expression);
7189 fn = STRIP_TEMPLATE (fn);
7191 /* Do not do argument dependent lookup if regular
7192 lookup finds a member function or a block-scope
7193 function declaration. [basic.lookup.argdep]/3 */
7194 if (!DECL_FUNCTION_MEMBER_P (fn)
7195 && !DECL_LOCAL_FUNCTION_P (fn))
7197 koenig_p = true;
7198 if (!any_type_dependent_arguments_p (args))
7199 postfix_expression
7200 = perform_koenig_lookup (postfix_expression, args,
7201 complain);
7206 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7208 tree instance = TREE_OPERAND (postfix_expression, 0);
7209 tree fn = TREE_OPERAND (postfix_expression, 1);
7211 if (processing_template_decl
7212 && (type_dependent_object_expression_p (instance)
7213 || (!BASELINK_P (fn)
7214 && TREE_CODE (fn) != FIELD_DECL)
7215 || type_dependent_expression_p (fn)
7216 || any_type_dependent_arguments_p (args)))
7218 maybe_generic_this_capture (instance, fn);
7219 postfix_expression
7220 = build_min_nt_call_vec (postfix_expression, args);
7221 release_tree_vector (args);
7222 break;
7225 if (BASELINK_P (fn))
7227 postfix_expression
7228 = (build_new_method_call
7229 (instance, fn, &args, NULL_TREE,
7230 (idk == CP_ID_KIND_QUALIFIED
7231 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7232 : LOOKUP_NORMAL),
7233 /*fn_p=*/NULL,
7234 complain));
7236 else
7237 postfix_expression
7238 = finish_call_expr (postfix_expression, &args,
7239 /*disallow_virtual=*/false,
7240 /*koenig_p=*/false,
7241 complain);
7243 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7244 || TREE_CODE (postfix_expression) == MEMBER_REF
7245 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7246 postfix_expression = (build_offset_ref_call_from_tree
7247 (postfix_expression, &args,
7248 complain));
7249 else if (idk == CP_ID_KIND_QUALIFIED)
7250 /* A call to a static class member, or a namespace-scope
7251 function. */
7252 postfix_expression
7253 = finish_call_expr (postfix_expression, &args,
7254 /*disallow_virtual=*/true,
7255 koenig_p,
7256 complain);
7257 else
7258 /* All other function calls. */
7259 postfix_expression
7260 = finish_call_expr (postfix_expression, &args,
7261 /*disallow_virtual=*/false,
7262 koenig_p,
7263 complain);
7265 if (close_paren_loc != UNKNOWN_LOCATION)
7267 location_t combined_loc = make_location (token->location,
7268 start_loc,
7269 close_paren_loc);
7270 postfix_expression.set_location (combined_loc);
7273 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7274 idk = CP_ID_KIND_NONE;
7276 release_tree_vector (args);
7278 break;
7280 case CPP_DOT:
7281 case CPP_DEREF:
7282 /* postfix-expression . template [opt] id-expression
7283 postfix-expression . pseudo-destructor-name
7284 postfix-expression -> template [opt] id-expression
7285 postfix-expression -> pseudo-destructor-name */
7287 /* Consume the `.' or `->' operator. */
7288 cp_lexer_consume_token (parser->lexer);
7290 postfix_expression
7291 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7292 postfix_expression,
7293 false, &idk, loc);
7295 is_member_access = true;
7296 break;
7298 case CPP_PLUS_PLUS:
7299 /* postfix-expression ++ */
7300 /* Consume the `++' token. */
7301 cp_lexer_consume_token (parser->lexer);
7302 /* Generate a representation for the complete expression. */
7303 postfix_expression
7304 = finish_increment_expr (postfix_expression,
7305 POSTINCREMENT_EXPR);
7306 /* Increments may not appear in constant-expressions. */
7307 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7308 postfix_expression = error_mark_node;
7309 idk = CP_ID_KIND_NONE;
7310 is_member_access = false;
7311 break;
7313 case CPP_MINUS_MINUS:
7314 /* postfix-expression -- */
7315 /* Consume the `--' token. */
7316 cp_lexer_consume_token (parser->lexer);
7317 /* Generate a representation for the complete expression. */
7318 postfix_expression
7319 = finish_increment_expr (postfix_expression,
7320 POSTDECREMENT_EXPR);
7321 /* Decrements may not appear in constant-expressions. */
7322 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7323 postfix_expression = error_mark_node;
7324 idk = CP_ID_KIND_NONE;
7325 is_member_access = false;
7326 break;
7328 default:
7329 if (pidk_return != NULL)
7330 * pidk_return = idk;
7331 if (member_access_only_p)
7332 return is_member_access
7333 ? postfix_expression
7334 : cp_expr (error_mark_node);
7335 else
7336 return postfix_expression;
7340 /* We should never get here. */
7341 gcc_unreachable ();
7342 return error_mark_node;
7345 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7346 by cp_parser_builtin_offsetof. We're looking for
7348 postfix-expression [ expression ]
7349 postfix-expression [ braced-init-list ] (C++11)
7351 FOR_OFFSETOF is set if we're being called in that context, which
7352 changes how we deal with integer constant expressions. */
7354 static tree
7355 cp_parser_postfix_open_square_expression (cp_parser *parser,
7356 tree postfix_expression,
7357 bool for_offsetof,
7358 bool decltype_p)
7360 tree index = NULL_TREE;
7361 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7362 bool saved_greater_than_is_operator_p;
7364 /* Consume the `[' token. */
7365 cp_lexer_consume_token (parser->lexer);
7367 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7368 parser->greater_than_is_operator_p = true;
7370 /* Parse the index expression. */
7371 /* ??? For offsetof, there is a question of what to allow here. If
7372 offsetof is not being used in an integral constant expression context,
7373 then we *could* get the right answer by computing the value at runtime.
7374 If we are in an integral constant expression context, then we might
7375 could accept any constant expression; hard to say without analysis.
7376 Rather than open the barn door too wide right away, allow only integer
7377 constant expressions here. */
7378 if (for_offsetof)
7379 index = cp_parser_constant_expression (parser);
7380 else
7382 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7384 bool expr_nonconst_p;
7385 cp_lexer_set_source_position (parser->lexer);
7386 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7387 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7389 else
7390 index = cp_parser_expression (parser);
7393 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7395 /* Look for the closing `]'. */
7396 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7398 /* Build the ARRAY_REF. */
7399 postfix_expression = grok_array_decl (loc, postfix_expression,
7400 index, decltype_p);
7402 /* When not doing offsetof, array references are not permitted in
7403 constant-expressions. */
7404 if (!for_offsetof
7405 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7406 postfix_expression = error_mark_node;
7408 return postfix_expression;
7411 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7412 dereference of incomplete type, returns true if error_mark_node should
7413 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7414 and *DEPENDENT_P. */
7416 bool
7417 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7418 bool *dependent_p)
7420 /* In a template, be permissive by treating an object expression
7421 of incomplete type as dependent (after a pedwarn). */
7422 diagnostic_t kind = (processing_template_decl
7423 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7425 switch (TREE_CODE (*postfix_expression))
7427 case CAST_EXPR:
7428 case REINTERPRET_CAST_EXPR:
7429 case CONST_CAST_EXPR:
7430 case STATIC_CAST_EXPR:
7431 case DYNAMIC_CAST_EXPR:
7432 case IMPLICIT_CONV_EXPR:
7433 case VIEW_CONVERT_EXPR:
7434 case NON_LVALUE_EXPR:
7435 kind = DK_ERROR;
7436 break;
7437 case OVERLOAD:
7438 /* Don't emit any diagnostic for OVERLOADs. */
7439 kind = DK_IGNORED;
7440 break;
7441 default:
7442 /* Avoid clobbering e.g. DECLs. */
7443 if (!EXPR_P (*postfix_expression))
7444 kind = DK_ERROR;
7445 break;
7448 if (kind == DK_IGNORED)
7449 return false;
7451 location_t exploc = location_of (*postfix_expression);
7452 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7453 if (!MAYBE_CLASS_TYPE_P (*scope))
7454 return true;
7455 if (kind == DK_ERROR)
7456 *scope = *postfix_expression = error_mark_node;
7457 else if (processing_template_decl)
7459 *dependent_p = true;
7460 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7462 return false;
7465 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7466 by cp_parser_builtin_offsetof. We're looking for
7468 postfix-expression . template [opt] id-expression
7469 postfix-expression . pseudo-destructor-name
7470 postfix-expression -> template [opt] id-expression
7471 postfix-expression -> pseudo-destructor-name
7473 FOR_OFFSETOF is set if we're being called in that context. That sorta
7474 limits what of the above we'll actually accept, but nevermind.
7475 TOKEN_TYPE is the "." or "->" token, which will already have been
7476 removed from the stream. */
7478 static tree
7479 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7480 enum cpp_ttype token_type,
7481 cp_expr postfix_expression,
7482 bool for_offsetof, cp_id_kind *idk,
7483 location_t location)
7485 tree name;
7486 bool dependent_p;
7487 bool pseudo_destructor_p;
7488 tree scope = NULL_TREE;
7489 location_t start_loc = postfix_expression.get_start ();
7491 /* If this is a `->' operator, dereference the pointer. */
7492 if (token_type == CPP_DEREF)
7493 postfix_expression = build_x_arrow (location, postfix_expression,
7494 tf_warning_or_error);
7495 /* Check to see whether or not the expression is type-dependent and
7496 not the current instantiation. */
7497 dependent_p = type_dependent_object_expression_p (postfix_expression);
7498 /* The identifier following the `->' or `.' is not qualified. */
7499 parser->scope = NULL_TREE;
7500 parser->qualifying_scope = NULL_TREE;
7501 parser->object_scope = NULL_TREE;
7502 *idk = CP_ID_KIND_NONE;
7504 /* Enter the scope corresponding to the type of the object
7505 given by the POSTFIX_EXPRESSION. */
7506 if (!dependent_p)
7508 scope = TREE_TYPE (postfix_expression);
7509 /* According to the standard, no expression should ever have
7510 reference type. Unfortunately, we do not currently match
7511 the standard in this respect in that our internal representation
7512 of an expression may have reference type even when the standard
7513 says it does not. Therefore, we have to manually obtain the
7514 underlying type here. */
7515 scope = non_reference (scope);
7516 /* The type of the POSTFIX_EXPRESSION must be complete. */
7517 /* Unlike the object expression in other contexts, *this is not
7518 required to be of complete type for purposes of class member
7519 access (5.2.5) outside the member function body. */
7520 if (postfix_expression != current_class_ref
7521 && scope != error_mark_node
7522 && !currently_open_class (scope))
7524 scope = complete_type (scope);
7525 if (!COMPLETE_TYPE_P (scope)
7526 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7527 &dependent_p))
7528 return error_mark_node;
7531 if (!dependent_p)
7533 /* Let the name lookup machinery know that we are processing a
7534 class member access expression. */
7535 parser->context->object_type = scope;
7536 /* If something went wrong, we want to be able to discern that case,
7537 as opposed to the case where there was no SCOPE due to the type
7538 of expression being dependent. */
7539 if (!scope)
7540 scope = error_mark_node;
7541 /* If the SCOPE was erroneous, make the various semantic analysis
7542 functions exit quickly -- and without issuing additional error
7543 messages. */
7544 if (scope == error_mark_node)
7545 postfix_expression = error_mark_node;
7549 if (dependent_p)
7550 /* Tell cp_parser_lookup_name that there was an object, even though it's
7551 type-dependent. */
7552 parser->context->object_type = unknown_type_node;
7554 /* Assume this expression is not a pseudo-destructor access. */
7555 pseudo_destructor_p = false;
7557 /* If the SCOPE is a scalar type, then, if this is a valid program,
7558 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7559 is type dependent, it can be pseudo-destructor-name or something else.
7560 Try to parse it as pseudo-destructor-name first. */
7561 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7563 tree s;
7564 tree type;
7566 cp_parser_parse_tentatively (parser);
7567 /* Parse the pseudo-destructor-name. */
7568 s = NULL_TREE;
7569 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7570 &s, &type);
7571 if (dependent_p
7572 && (cp_parser_error_occurred (parser)
7573 || !SCALAR_TYPE_P (type)))
7574 cp_parser_abort_tentative_parse (parser);
7575 else if (cp_parser_parse_definitely (parser))
7577 pseudo_destructor_p = true;
7578 postfix_expression
7579 = finish_pseudo_destructor_expr (postfix_expression,
7580 s, type, location);
7584 if (!pseudo_destructor_p)
7586 /* If the SCOPE is not a scalar type, we are looking at an
7587 ordinary class member access expression, rather than a
7588 pseudo-destructor-name. */
7589 bool template_p;
7590 cp_token *token = cp_lexer_peek_token (parser->lexer);
7591 /* Parse the id-expression. */
7592 name = (cp_parser_id_expression
7593 (parser,
7594 cp_parser_optional_template_keyword (parser),
7595 /*check_dependency_p=*/true,
7596 &template_p,
7597 /*declarator_p=*/false,
7598 /*optional_p=*/false));
7599 /* In general, build a SCOPE_REF if the member name is qualified.
7600 However, if the name was not dependent and has already been
7601 resolved; there is no need to build the SCOPE_REF. For example;
7603 struct X { void f(); };
7604 template <typename T> void f(T* t) { t->X::f(); }
7606 Even though "t" is dependent, "X::f" is not and has been resolved
7607 to a BASELINK; there is no need to include scope information. */
7609 /* But we do need to remember that there was an explicit scope for
7610 virtual function calls. */
7611 if (parser->scope)
7612 *idk = CP_ID_KIND_QUALIFIED;
7614 /* If the name is a template-id that names a type, we will get a
7615 TYPE_DECL here. That is invalid code. */
7616 if (TREE_CODE (name) == TYPE_DECL)
7618 error_at (token->location, "invalid use of %qD", name);
7619 postfix_expression = error_mark_node;
7621 else
7623 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7625 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7627 error_at (token->location, "%<%D::%D%> is not a class member",
7628 parser->scope, name);
7629 postfix_expression = error_mark_node;
7631 else
7632 name = build_qualified_name (/*type=*/NULL_TREE,
7633 parser->scope,
7634 name,
7635 template_p);
7636 parser->scope = NULL_TREE;
7637 parser->qualifying_scope = NULL_TREE;
7638 parser->object_scope = NULL_TREE;
7640 if (parser->scope && name && BASELINK_P (name))
7641 adjust_result_of_qualified_name_lookup
7642 (name, parser->scope, scope);
7643 postfix_expression
7644 = finish_class_member_access_expr (postfix_expression, name,
7645 template_p,
7646 tf_warning_or_error);
7647 /* Build a location e.g.:
7648 ptr->access_expr
7649 ~~~^~~~~~~~~~~~~
7650 where the caret is at the deref token, ranging from
7651 the start of postfix_expression to the end of the access expr. */
7652 location_t end_loc
7653 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7654 location_t combined_loc
7655 = make_location (input_location, start_loc, end_loc);
7656 protected_set_expr_location (postfix_expression, combined_loc);
7660 /* We no longer need to look up names in the scope of the object on
7661 the left-hand side of the `.' or `->' operator. */
7662 parser->context->object_type = NULL_TREE;
7664 /* Outside of offsetof, these operators may not appear in
7665 constant-expressions. */
7666 if (!for_offsetof
7667 && (cp_parser_non_integral_constant_expression
7668 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7669 postfix_expression = error_mark_node;
7671 return postfix_expression;
7674 /* Parse a parenthesized expression-list.
7676 expression-list:
7677 assignment-expression
7678 expression-list, assignment-expression
7680 attribute-list:
7681 expression-list
7682 identifier
7683 identifier, expression-list
7685 CAST_P is true if this expression is the target of a cast.
7687 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7688 argument pack.
7690 WRAP_LOCATIONS_P is true if expressions within this list for which
7691 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7692 their source locations.
7694 Returns a vector of trees. Each element is a representation of an
7695 assignment-expression. NULL is returned if the ( and or ) are
7696 missing. An empty, but allocated, vector is returned on no
7697 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7698 if we are parsing an attribute list for an attribute that wants a
7699 plain identifier argument, normal_attr for an attribute that wants
7700 an expression, or non_attr if we aren't parsing an attribute list. If
7701 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7702 not all of the expressions in the list were constant.
7703 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7704 will be written to with the location of the closing parenthesis. If
7705 an error occurs, it may or may not be written to. */
7707 static vec<tree, va_gc> *
7708 cp_parser_parenthesized_expression_list (cp_parser* parser,
7709 int is_attribute_list,
7710 bool cast_p,
7711 bool allow_expansion_p,
7712 bool *non_constant_p,
7713 location_t *close_paren_loc,
7714 bool wrap_locations_p)
7716 vec<tree, va_gc> *expression_list;
7717 bool fold_expr_p = is_attribute_list != non_attr;
7718 tree identifier = NULL_TREE;
7719 bool saved_greater_than_is_operator_p;
7721 /* Assume all the expressions will be constant. */
7722 if (non_constant_p)
7723 *non_constant_p = false;
7725 matching_parens parens;
7726 if (!parens.require_open (parser))
7727 return NULL;
7729 expression_list = make_tree_vector ();
7731 /* Within a parenthesized expression, a `>' token is always
7732 the greater-than operator. */
7733 saved_greater_than_is_operator_p
7734 = parser->greater_than_is_operator_p;
7735 parser->greater_than_is_operator_p = true;
7737 cp_expr expr (NULL_TREE);
7739 /* Consume expressions until there are no more. */
7740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7741 while (true)
7743 /* At the beginning of attribute lists, check to see if the
7744 next token is an identifier. */
7745 if (is_attribute_list == id_attr
7746 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7748 cp_token *token;
7750 /* Consume the identifier. */
7751 token = cp_lexer_consume_token (parser->lexer);
7752 /* Save the identifier. */
7753 identifier = token->u.value;
7755 else
7757 bool expr_non_constant_p;
7759 /* Parse the next assignment-expression. */
7760 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7762 /* A braced-init-list. */
7763 cp_lexer_set_source_position (parser->lexer);
7764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7765 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7766 if (non_constant_p && expr_non_constant_p)
7767 *non_constant_p = true;
7769 else if (non_constant_p)
7771 expr = (cp_parser_constant_expression
7772 (parser, /*allow_non_constant_p=*/true,
7773 &expr_non_constant_p));
7774 if (expr_non_constant_p)
7775 *non_constant_p = true;
7777 else
7778 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7779 cast_p);
7781 if (fold_expr_p)
7782 expr = instantiate_non_dependent_expr (expr);
7784 /* If we have an ellipsis, then this is an expression
7785 expansion. */
7786 if (allow_expansion_p
7787 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7789 /* Consume the `...'. */
7790 cp_lexer_consume_token (parser->lexer);
7792 /* Build the argument pack. */
7793 expr = make_pack_expansion (expr);
7796 if (wrap_locations_p)
7797 expr.maybe_add_location_wrapper ();
7799 /* Add it to the list. We add error_mark_node
7800 expressions to the list, so that we can still tell if
7801 the correct form for a parenthesized expression-list
7802 is found. That gives better errors. */
7803 vec_safe_push (expression_list, expr.get_value ());
7805 if (expr == error_mark_node)
7806 goto skip_comma;
7809 /* After the first item, attribute lists look the same as
7810 expression lists. */
7811 is_attribute_list = non_attr;
7813 get_comma:;
7814 /* If the next token isn't a `,', then we are done. */
7815 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7816 break;
7818 /* Otherwise, consume the `,' and keep going. */
7819 cp_lexer_consume_token (parser->lexer);
7822 if (close_paren_loc)
7823 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7825 if (!parens.require_close (parser))
7827 int ending;
7829 skip_comma:;
7830 /* We try and resync to an unnested comma, as that will give the
7831 user better diagnostics. */
7832 ending = cp_parser_skip_to_closing_parenthesis (parser,
7833 /*recovering=*/true,
7834 /*or_comma=*/true,
7835 /*consume_paren=*/true);
7836 if (ending < 0)
7837 goto get_comma;
7838 if (!ending)
7840 parser->greater_than_is_operator_p
7841 = saved_greater_than_is_operator_p;
7842 return NULL;
7846 parser->greater_than_is_operator_p
7847 = saved_greater_than_is_operator_p;
7849 if (identifier)
7850 vec_safe_insert (expression_list, 0, identifier);
7852 return expression_list;
7855 /* Parse a pseudo-destructor-name.
7857 pseudo-destructor-name:
7858 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7859 :: [opt] nested-name-specifier template template-id :: ~ type-name
7860 :: [opt] nested-name-specifier [opt] ~ type-name
7862 If either of the first two productions is used, sets *SCOPE to the
7863 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7864 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7865 or ERROR_MARK_NODE if the parse fails. */
7867 static void
7868 cp_parser_pseudo_destructor_name (cp_parser* parser,
7869 tree object,
7870 tree* scope,
7871 tree* type)
7873 bool nested_name_specifier_p;
7875 /* Handle ~auto. */
7876 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7877 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7878 && !type_dependent_expression_p (object))
7880 if (cxx_dialect < cxx14)
7881 pedwarn (input_location, 0,
7882 "%<~auto%> only available with "
7883 "-std=c++14 or -std=gnu++14");
7884 cp_lexer_consume_token (parser->lexer);
7885 cp_lexer_consume_token (parser->lexer);
7886 *scope = NULL_TREE;
7887 *type = TREE_TYPE (object);
7888 return;
7891 /* Assume that things will not work out. */
7892 *type = error_mark_node;
7894 /* Look for the optional `::' operator. */
7895 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7896 /* Look for the optional nested-name-specifier. */
7897 nested_name_specifier_p
7898 = (cp_parser_nested_name_specifier_opt (parser,
7899 /*typename_keyword_p=*/false,
7900 /*check_dependency_p=*/true,
7901 /*type_p=*/false,
7902 /*is_declaration=*/false)
7903 != NULL_TREE);
7904 /* Now, if we saw a nested-name-specifier, we might be doing the
7905 second production. */
7906 if (nested_name_specifier_p
7907 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7909 /* Consume the `template' keyword. */
7910 cp_lexer_consume_token (parser->lexer);
7911 /* Parse the template-id. */
7912 cp_parser_template_id (parser,
7913 /*template_keyword_p=*/true,
7914 /*check_dependency_p=*/false,
7915 class_type,
7916 /*is_declaration=*/true);
7917 /* Look for the `::' token. */
7918 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7920 /* If the next token is not a `~', then there might be some
7921 additional qualification. */
7922 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7924 /* At this point, we're looking for "type-name :: ~". The type-name
7925 must not be a class-name, since this is a pseudo-destructor. So,
7926 it must be either an enum-name, or a typedef-name -- both of which
7927 are just identifiers. So, we peek ahead to check that the "::"
7928 and "~" tokens are present; if they are not, then we can avoid
7929 calling type_name. */
7930 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7931 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7932 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7934 cp_parser_error (parser, "non-scalar type");
7935 return;
7938 /* Look for the type-name. */
7939 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7940 if (*scope == error_mark_node)
7941 return;
7943 /* Look for the `::' token. */
7944 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7946 else
7947 *scope = NULL_TREE;
7949 /* Look for the `~'. */
7950 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7952 /* Once we see the ~, this has to be a pseudo-destructor. */
7953 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7954 cp_parser_commit_to_topmost_tentative_parse (parser);
7956 /* Look for the type-name again. We are not responsible for
7957 checking that it matches the first type-name. */
7958 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7961 /* Parse a unary-expression.
7963 unary-expression:
7964 postfix-expression
7965 ++ cast-expression
7966 -- cast-expression
7967 unary-operator cast-expression
7968 sizeof unary-expression
7969 sizeof ( type-id )
7970 alignof ( type-id ) [C++0x]
7971 new-expression
7972 delete-expression
7974 GNU Extensions:
7976 unary-expression:
7977 __extension__ cast-expression
7978 __alignof__ unary-expression
7979 __alignof__ ( type-id )
7980 alignof unary-expression [C++0x]
7981 __real__ cast-expression
7982 __imag__ cast-expression
7983 && identifier
7984 sizeof ( type-id ) { initializer-list , [opt] }
7985 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7986 __alignof__ ( type-id ) { initializer-list , [opt] }
7988 ADDRESS_P is true iff the unary-expression is appearing as the
7989 operand of the `&' operator. CAST_P is true if this expression is
7990 the target of a cast.
7992 Returns a representation of the expression. */
7994 static cp_expr
7995 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7996 bool address_p, bool cast_p, bool decltype_p)
7998 cp_token *token;
7999 enum tree_code unary_operator;
8001 /* Peek at the next token. */
8002 token = cp_lexer_peek_token (parser->lexer);
8003 /* Some keywords give away the kind of expression. */
8004 if (token->type == CPP_KEYWORD)
8006 enum rid keyword = token->keyword;
8008 switch (keyword)
8010 case RID_ALIGNOF:
8011 case RID_SIZEOF:
8013 tree operand, ret;
8014 enum tree_code op;
8015 location_t start_loc = token->location;
8017 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8018 bool std_alignof = id_equal (token->u.value, "alignof");
8020 /* Consume the token. */
8021 cp_lexer_consume_token (parser->lexer);
8022 /* Parse the operand. */
8023 operand = cp_parser_sizeof_operand (parser, keyword);
8025 if (TYPE_P (operand))
8026 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8027 true);
8028 else
8030 /* ISO C++ defines alignof only with types, not with
8031 expressions. So pedwarn if alignof is used with a non-
8032 type expression. However, __alignof__ is ok. */
8033 if (std_alignof)
8034 pedwarn (token->location, OPT_Wpedantic,
8035 "ISO C++ does not allow %<alignof%> "
8036 "with a non-type");
8038 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8040 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8041 SIZEOF_EXPR with the original operand. */
8042 if (op == SIZEOF_EXPR && ret != error_mark_node)
8044 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8046 if (!processing_template_decl && TYPE_P (operand))
8048 ret = build_min (SIZEOF_EXPR, size_type_node,
8049 build1 (NOP_EXPR, operand,
8050 error_mark_node));
8051 SIZEOF_EXPR_TYPE_P (ret) = 1;
8053 else
8054 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8055 TREE_SIDE_EFFECTS (ret) = 0;
8056 TREE_READONLY (ret) = 1;
8060 /* Construct a location e.g. :
8061 alignof (expr)
8062 ^~~~~~~~~~~~~~
8063 with start == caret at the start of the "alignof"/"sizeof"
8064 token, with the endpoint at the final closing paren. */
8065 location_t finish_loc
8066 = cp_lexer_previous_token (parser->lexer)->location;
8067 location_t compound_loc
8068 = make_location (start_loc, start_loc, finish_loc);
8070 cp_expr ret_expr (ret);
8071 ret_expr.set_location (compound_loc);
8072 ret_expr = ret_expr.maybe_add_location_wrapper ();
8073 return ret_expr;
8076 case RID_NEW:
8077 return cp_parser_new_expression (parser);
8079 case RID_DELETE:
8080 return cp_parser_delete_expression (parser);
8082 case RID_EXTENSION:
8084 /* The saved value of the PEDANTIC flag. */
8085 int saved_pedantic;
8086 tree expr;
8088 /* Save away the PEDANTIC flag. */
8089 cp_parser_extension_opt (parser, &saved_pedantic);
8090 /* Parse the cast-expression. */
8091 expr = cp_parser_simple_cast_expression (parser);
8092 /* Restore the PEDANTIC flag. */
8093 pedantic = saved_pedantic;
8095 return expr;
8098 case RID_REALPART:
8099 case RID_IMAGPART:
8101 tree expression;
8103 /* Consume the `__real__' or `__imag__' token. */
8104 cp_lexer_consume_token (parser->lexer);
8105 /* Parse the cast-expression. */
8106 expression = cp_parser_simple_cast_expression (parser);
8107 /* Create the complete representation. */
8108 return build_x_unary_op (token->location,
8109 (keyword == RID_REALPART
8110 ? REALPART_EXPR : IMAGPART_EXPR),
8111 expression,
8112 tf_warning_or_error);
8114 break;
8116 case RID_TRANSACTION_ATOMIC:
8117 case RID_TRANSACTION_RELAXED:
8118 return cp_parser_transaction_expression (parser, keyword);
8120 case RID_NOEXCEPT:
8122 tree expr;
8123 const char *saved_message;
8124 bool saved_integral_constant_expression_p;
8125 bool saved_non_integral_constant_expression_p;
8126 bool saved_greater_than_is_operator_p;
8128 location_t start_loc = token->location;
8130 cp_lexer_consume_token (parser->lexer);
8131 matching_parens parens;
8132 parens.require_open (parser);
8134 saved_message = parser->type_definition_forbidden_message;
8135 parser->type_definition_forbidden_message
8136 = G_("types may not be defined in %<noexcept%> expressions");
8138 saved_integral_constant_expression_p
8139 = parser->integral_constant_expression_p;
8140 saved_non_integral_constant_expression_p
8141 = parser->non_integral_constant_expression_p;
8142 parser->integral_constant_expression_p = false;
8144 saved_greater_than_is_operator_p
8145 = parser->greater_than_is_operator_p;
8146 parser->greater_than_is_operator_p = true;
8148 ++cp_unevaluated_operand;
8149 ++c_inhibit_evaluation_warnings;
8150 ++cp_noexcept_operand;
8151 expr = cp_parser_expression (parser);
8152 --cp_noexcept_operand;
8153 --c_inhibit_evaluation_warnings;
8154 --cp_unevaluated_operand;
8156 parser->greater_than_is_operator_p
8157 = saved_greater_than_is_operator_p;
8159 parser->integral_constant_expression_p
8160 = saved_integral_constant_expression_p;
8161 parser->non_integral_constant_expression_p
8162 = saved_non_integral_constant_expression_p;
8164 parser->type_definition_forbidden_message = saved_message;
8166 location_t finish_loc
8167 = cp_lexer_peek_token (parser->lexer)->location;
8168 parens.require_close (parser);
8170 /* Construct a location of the form:
8171 noexcept (expr)
8172 ^~~~~~~~~~~~~~~
8173 with start == caret, finishing at the close-paren. */
8174 location_t noexcept_loc
8175 = make_location (start_loc, start_loc, finish_loc);
8177 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8178 noexcept_loc);
8181 default:
8182 break;
8186 /* Look for the `:: new' and `:: delete', which also signal the
8187 beginning of a new-expression, or delete-expression,
8188 respectively. If the next token is `::', then it might be one of
8189 these. */
8190 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8192 enum rid keyword;
8194 /* See if the token after the `::' is one of the keywords in
8195 which we're interested. */
8196 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8197 /* If it's `new', we have a new-expression. */
8198 if (keyword == RID_NEW)
8199 return cp_parser_new_expression (parser);
8200 /* Similarly, for `delete'. */
8201 else if (keyword == RID_DELETE)
8202 return cp_parser_delete_expression (parser);
8205 /* Look for a unary operator. */
8206 unary_operator = cp_parser_unary_operator (token);
8207 /* The `++' and `--' operators can be handled similarly, even though
8208 they are not technically unary-operators in the grammar. */
8209 if (unary_operator == ERROR_MARK)
8211 if (token->type == CPP_PLUS_PLUS)
8212 unary_operator = PREINCREMENT_EXPR;
8213 else if (token->type == CPP_MINUS_MINUS)
8214 unary_operator = PREDECREMENT_EXPR;
8215 /* Handle the GNU address-of-label extension. */
8216 else if (cp_parser_allow_gnu_extensions_p (parser)
8217 && token->type == CPP_AND_AND)
8219 tree identifier;
8220 tree expression;
8221 location_t start_loc = token->location;
8223 /* Consume the '&&' token. */
8224 cp_lexer_consume_token (parser->lexer);
8225 /* Look for the identifier. */
8226 location_t finish_loc
8227 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8228 identifier = cp_parser_identifier (parser);
8229 /* Construct a location of the form:
8230 &&label
8231 ^~~~~~~
8232 with caret==start at the "&&", finish at the end of the label. */
8233 location_t combined_loc
8234 = make_location (start_loc, start_loc, finish_loc);
8235 /* Create an expression representing the address. */
8236 expression = finish_label_address_expr (identifier, combined_loc);
8237 if (cp_parser_non_integral_constant_expression (parser,
8238 NIC_ADDR_LABEL))
8239 expression = error_mark_node;
8240 return expression;
8243 if (unary_operator != ERROR_MARK)
8245 cp_expr cast_expression;
8246 cp_expr expression = error_mark_node;
8247 non_integral_constant non_constant_p = NIC_NONE;
8248 location_t loc = token->location;
8249 tsubst_flags_t complain = complain_flags (decltype_p);
8251 /* Consume the operator token. */
8252 token = cp_lexer_consume_token (parser->lexer);
8253 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8255 /* Parse the cast-expression. */
8256 cast_expression
8257 = cp_parser_cast_expression (parser,
8258 unary_operator == ADDR_EXPR,
8259 /*cast_p=*/false,
8260 /*decltype*/false,
8261 pidk);
8263 /* Make a location:
8264 OP_TOKEN CAST_EXPRESSION
8265 ^~~~~~~~~~~~~~~~~~~~~~~~~
8266 with start==caret at the operator token, and
8267 extending to the end of the cast_expression. */
8268 loc = make_location (loc, loc, cast_expression.get_finish ());
8270 /* Now, build an appropriate representation. */
8271 switch (unary_operator)
8273 case INDIRECT_REF:
8274 non_constant_p = NIC_STAR;
8275 expression = build_x_indirect_ref (loc, cast_expression,
8276 RO_UNARY_STAR,
8277 complain);
8278 /* TODO: build_x_indirect_ref does not always honor the
8279 location, so ensure it is set. */
8280 expression.set_location (loc);
8281 break;
8283 case ADDR_EXPR:
8284 non_constant_p = NIC_ADDR;
8285 /* Fall through. */
8286 case BIT_NOT_EXPR:
8287 expression = build_x_unary_op (loc, unary_operator,
8288 cast_expression,
8289 complain);
8290 /* TODO: build_x_unary_op does not always honor the location,
8291 so ensure it is set. */
8292 expression.set_location (loc);
8293 break;
8295 case PREINCREMENT_EXPR:
8296 case PREDECREMENT_EXPR:
8297 non_constant_p = unary_operator == PREINCREMENT_EXPR
8298 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8299 /* Fall through. */
8300 case NEGATE_EXPR:
8301 /* Immediately fold negation of a constant, unless the constant is 0
8302 (since -0 == 0) or it would overflow. */
8303 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8304 && CONSTANT_CLASS_P (cast_expression)
8305 && !integer_zerop (cast_expression)
8306 && !TREE_OVERFLOW (cast_expression))
8308 tree folded = fold_build1 (unary_operator,
8309 TREE_TYPE (cast_expression),
8310 cast_expression);
8311 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8313 expression = cp_expr (folded, loc);
8314 break;
8317 /* Fall through. */
8318 case UNARY_PLUS_EXPR:
8319 case TRUTH_NOT_EXPR:
8320 expression = finish_unary_op_expr (loc, unary_operator,
8321 cast_expression, complain);
8322 break;
8324 default:
8325 gcc_unreachable ();
8328 if (non_constant_p != NIC_NONE
8329 && cp_parser_non_integral_constant_expression (parser,
8330 non_constant_p))
8331 expression = error_mark_node;
8333 return expression;
8336 return cp_parser_postfix_expression (parser, address_p, cast_p,
8337 /*member_access_only_p=*/false,
8338 decltype_p,
8339 pidk);
8342 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8343 unary-operator, the corresponding tree code is returned. */
8345 static enum tree_code
8346 cp_parser_unary_operator (cp_token* token)
8348 switch (token->type)
8350 case CPP_MULT:
8351 return INDIRECT_REF;
8353 case CPP_AND:
8354 return ADDR_EXPR;
8356 case CPP_PLUS:
8357 return UNARY_PLUS_EXPR;
8359 case CPP_MINUS:
8360 return NEGATE_EXPR;
8362 case CPP_NOT:
8363 return TRUTH_NOT_EXPR;
8365 case CPP_COMPL:
8366 return BIT_NOT_EXPR;
8368 default:
8369 return ERROR_MARK;
8373 /* Parse a new-expression.
8375 new-expression:
8376 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8377 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8379 Returns a representation of the expression. */
8381 static tree
8382 cp_parser_new_expression (cp_parser* parser)
8384 bool global_scope_p;
8385 vec<tree, va_gc> *placement;
8386 tree type;
8387 vec<tree, va_gc> *initializer;
8388 tree nelts = NULL_TREE;
8389 tree ret;
8391 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8393 /* Look for the optional `::' operator. */
8394 global_scope_p
8395 = (cp_parser_global_scope_opt (parser,
8396 /*current_scope_valid_p=*/false)
8397 != NULL_TREE);
8398 /* Look for the `new' operator. */
8399 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8400 /* There's no easy way to tell a new-placement from the
8401 `( type-id )' construct. */
8402 cp_parser_parse_tentatively (parser);
8403 /* Look for a new-placement. */
8404 placement = cp_parser_new_placement (parser);
8405 /* If that didn't work out, there's no new-placement. */
8406 if (!cp_parser_parse_definitely (parser))
8408 if (placement != NULL)
8409 release_tree_vector (placement);
8410 placement = NULL;
8413 /* If the next token is a `(', then we have a parenthesized
8414 type-id. */
8415 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8417 cp_token *token;
8418 const char *saved_message = parser->type_definition_forbidden_message;
8420 /* Consume the `('. */
8421 matching_parens parens;
8422 parens.consume_open (parser);
8424 /* Parse the type-id. */
8425 parser->type_definition_forbidden_message
8426 = G_("types may not be defined in a new-expression");
8428 type_id_in_expr_sentinel s (parser);
8429 type = cp_parser_type_id (parser);
8431 parser->type_definition_forbidden_message = saved_message;
8433 /* Look for the closing `)'. */
8434 parens.require_close (parser);
8435 token = cp_lexer_peek_token (parser->lexer);
8436 /* There should not be a direct-new-declarator in this production,
8437 but GCC used to allowed this, so we check and emit a sensible error
8438 message for this case. */
8439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8441 error_at (token->location,
8442 "array bound forbidden after parenthesized type-id");
8443 inform (token->location,
8444 "try removing the parentheses around the type-id");
8445 cp_parser_direct_new_declarator (parser);
8448 /* Otherwise, there must be a new-type-id. */
8449 else
8450 type = cp_parser_new_type_id (parser, &nelts);
8452 /* If the next token is a `(' or '{', then we have a new-initializer. */
8453 cp_token *token = cp_lexer_peek_token (parser->lexer);
8454 if (token->type == CPP_OPEN_PAREN
8455 || token->type == CPP_OPEN_BRACE)
8456 initializer = cp_parser_new_initializer (parser);
8457 else
8458 initializer = NULL;
8460 /* A new-expression may not appear in an integral constant
8461 expression. */
8462 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8463 ret = error_mark_node;
8464 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8465 of a new-type-id or type-id of a new-expression, the new-expression shall
8466 contain a new-initializer of the form ( assignment-expression )".
8467 Additionally, consistently with the spirit of DR 1467, we want to accept
8468 'new auto { 2 }' too. */
8469 else if ((ret = type_uses_auto (type))
8470 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8471 && (vec_safe_length (initializer) != 1
8472 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8473 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8475 error_at (token->location,
8476 "initialization of new-expression for type %<auto%> "
8477 "requires exactly one element");
8478 ret = error_mark_node;
8480 else
8482 /* Construct a location e.g.:
8483 ptr = new int[100]
8484 ^~~~~~~~~~~~
8485 with caret == start at the start of the "new" token, and the end
8486 at the end of the final token we consumed. */
8487 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8488 location_t end_loc = get_finish (end_tok->location);
8489 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8491 /* Create a representation of the new-expression. */
8492 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8493 tf_warning_or_error);
8494 protected_set_expr_location (ret, combined_loc);
8497 if (placement != NULL)
8498 release_tree_vector (placement);
8499 if (initializer != NULL)
8500 release_tree_vector (initializer);
8502 return ret;
8505 /* Parse a new-placement.
8507 new-placement:
8508 ( expression-list )
8510 Returns the same representation as for an expression-list. */
8512 static vec<tree, va_gc> *
8513 cp_parser_new_placement (cp_parser* parser)
8515 vec<tree, va_gc> *expression_list;
8517 /* Parse the expression-list. */
8518 expression_list = (cp_parser_parenthesized_expression_list
8519 (parser, non_attr, /*cast_p=*/false,
8520 /*allow_expansion_p=*/true,
8521 /*non_constant_p=*/NULL));
8523 if (expression_list && expression_list->is_empty ())
8524 error ("expected expression-list or type-id");
8526 return expression_list;
8529 /* Parse a new-type-id.
8531 new-type-id:
8532 type-specifier-seq new-declarator [opt]
8534 Returns the TYPE allocated. If the new-type-id indicates an array
8535 type, *NELTS is set to the number of elements in the last array
8536 bound; the TYPE will not include the last array bound. */
8538 static tree
8539 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8541 cp_decl_specifier_seq type_specifier_seq;
8542 cp_declarator *new_declarator;
8543 cp_declarator *declarator;
8544 cp_declarator *outer_declarator;
8545 const char *saved_message;
8547 /* The type-specifier sequence must not contain type definitions.
8548 (It cannot contain declarations of new types either, but if they
8549 are not definitions we will catch that because they are not
8550 complete.) */
8551 saved_message = parser->type_definition_forbidden_message;
8552 parser->type_definition_forbidden_message
8553 = G_("types may not be defined in a new-type-id");
8554 /* Parse the type-specifier-seq. */
8555 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8556 /*is_trailing_return=*/false,
8557 &type_specifier_seq);
8558 /* Restore the old message. */
8559 parser->type_definition_forbidden_message = saved_message;
8561 if (type_specifier_seq.type == error_mark_node)
8562 return error_mark_node;
8564 /* Parse the new-declarator. */
8565 new_declarator = cp_parser_new_declarator_opt (parser);
8567 /* Determine the number of elements in the last array dimension, if
8568 any. */
8569 *nelts = NULL_TREE;
8570 /* Skip down to the last array dimension. */
8571 declarator = new_declarator;
8572 outer_declarator = NULL;
8573 while (declarator && (declarator->kind == cdk_pointer
8574 || declarator->kind == cdk_ptrmem))
8576 outer_declarator = declarator;
8577 declarator = declarator->declarator;
8579 while (declarator
8580 && declarator->kind == cdk_array
8581 && declarator->declarator
8582 && declarator->declarator->kind == cdk_array)
8584 outer_declarator = declarator;
8585 declarator = declarator->declarator;
8588 if (declarator && declarator->kind == cdk_array)
8590 *nelts = declarator->u.array.bounds;
8591 if (*nelts == error_mark_node)
8592 *nelts = integer_one_node;
8594 if (outer_declarator)
8595 outer_declarator->declarator = declarator->declarator;
8596 else
8597 new_declarator = NULL;
8600 return groktypename (&type_specifier_seq, new_declarator, false);
8603 /* Parse an (optional) new-declarator.
8605 new-declarator:
8606 ptr-operator new-declarator [opt]
8607 direct-new-declarator
8609 Returns the declarator. */
8611 static cp_declarator *
8612 cp_parser_new_declarator_opt (cp_parser* parser)
8614 enum tree_code code;
8615 tree type, std_attributes = NULL_TREE;
8616 cp_cv_quals cv_quals;
8618 /* We don't know if there's a ptr-operator next, or not. */
8619 cp_parser_parse_tentatively (parser);
8620 /* Look for a ptr-operator. */
8621 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8622 /* If that worked, look for more new-declarators. */
8623 if (cp_parser_parse_definitely (parser))
8625 cp_declarator *declarator;
8627 /* Parse another optional declarator. */
8628 declarator = cp_parser_new_declarator_opt (parser);
8630 declarator = cp_parser_make_indirect_declarator
8631 (code, type, cv_quals, declarator, std_attributes);
8633 return declarator;
8636 /* If the next token is a `[', there is a direct-new-declarator. */
8637 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8638 return cp_parser_direct_new_declarator (parser);
8640 return NULL;
8643 /* Parse a direct-new-declarator.
8645 direct-new-declarator:
8646 [ expression ]
8647 direct-new-declarator [constant-expression]
8651 static cp_declarator *
8652 cp_parser_direct_new_declarator (cp_parser* parser)
8654 cp_declarator *declarator = NULL;
8656 while (true)
8658 tree expression;
8659 cp_token *token;
8661 /* Look for the opening `['. */
8662 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8664 token = cp_lexer_peek_token (parser->lexer);
8665 expression = cp_parser_expression (parser);
8666 /* The standard requires that the expression have integral
8667 type. DR 74 adds enumeration types. We believe that the
8668 real intent is that these expressions be handled like the
8669 expression in a `switch' condition, which also allows
8670 classes with a single conversion to integral or
8671 enumeration type. */
8672 if (!processing_template_decl)
8674 expression
8675 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8676 expression,
8677 /*complain=*/true);
8678 if (!expression)
8680 error_at (token->location,
8681 "expression in new-declarator must have integral "
8682 "or enumeration type");
8683 expression = error_mark_node;
8687 /* Look for the closing `]'. */
8688 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8690 /* Add this bound to the declarator. */
8691 declarator = make_array_declarator (declarator, expression);
8693 /* If the next token is not a `[', then there are no more
8694 bounds. */
8695 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8696 break;
8699 return declarator;
8702 /* Parse a new-initializer.
8704 new-initializer:
8705 ( expression-list [opt] )
8706 braced-init-list
8708 Returns a representation of the expression-list. */
8710 static vec<tree, va_gc> *
8711 cp_parser_new_initializer (cp_parser* parser)
8713 vec<tree, va_gc> *expression_list;
8715 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8717 tree t;
8718 bool expr_non_constant_p;
8719 cp_lexer_set_source_position (parser->lexer);
8720 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8721 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8722 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8723 expression_list = make_tree_vector_single (t);
8725 else
8726 expression_list = (cp_parser_parenthesized_expression_list
8727 (parser, non_attr, /*cast_p=*/false,
8728 /*allow_expansion_p=*/true,
8729 /*non_constant_p=*/NULL));
8731 return expression_list;
8734 /* Parse a delete-expression.
8736 delete-expression:
8737 :: [opt] delete cast-expression
8738 :: [opt] delete [ ] cast-expression
8740 Returns a representation of the expression. */
8742 static tree
8743 cp_parser_delete_expression (cp_parser* parser)
8745 bool global_scope_p;
8746 bool array_p;
8747 tree expression;
8749 /* Look for the optional `::' operator. */
8750 global_scope_p
8751 = (cp_parser_global_scope_opt (parser,
8752 /*current_scope_valid_p=*/false)
8753 != NULL_TREE);
8754 /* Look for the `delete' keyword. */
8755 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8756 /* See if the array syntax is in use. */
8757 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8759 /* Consume the `[' token. */
8760 cp_lexer_consume_token (parser->lexer);
8761 /* Look for the `]' token. */
8762 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8763 /* Remember that this is the `[]' construct. */
8764 array_p = true;
8766 else
8767 array_p = false;
8769 /* Parse the cast-expression. */
8770 expression = cp_parser_simple_cast_expression (parser);
8772 /* A delete-expression may not appear in an integral constant
8773 expression. */
8774 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8775 return error_mark_node;
8777 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8778 tf_warning_or_error);
8781 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8782 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8783 0 otherwise. */
8785 static int
8786 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8788 cp_token *token = cp_lexer_peek_token (parser->lexer);
8789 switch (token->type)
8791 case CPP_COMMA:
8792 case CPP_SEMICOLON:
8793 case CPP_QUERY:
8794 case CPP_COLON:
8795 case CPP_CLOSE_SQUARE:
8796 case CPP_CLOSE_PAREN:
8797 case CPP_CLOSE_BRACE:
8798 case CPP_OPEN_BRACE:
8799 case CPP_DOT:
8800 case CPP_DOT_STAR:
8801 case CPP_DEREF:
8802 case CPP_DEREF_STAR:
8803 case CPP_DIV:
8804 case CPP_MOD:
8805 case CPP_LSHIFT:
8806 case CPP_RSHIFT:
8807 case CPP_LESS:
8808 case CPP_GREATER:
8809 case CPP_LESS_EQ:
8810 case CPP_GREATER_EQ:
8811 case CPP_EQ_EQ:
8812 case CPP_NOT_EQ:
8813 case CPP_EQ:
8814 case CPP_MULT_EQ:
8815 case CPP_DIV_EQ:
8816 case CPP_MOD_EQ:
8817 case CPP_PLUS_EQ:
8818 case CPP_MINUS_EQ:
8819 case CPP_RSHIFT_EQ:
8820 case CPP_LSHIFT_EQ:
8821 case CPP_AND_EQ:
8822 case CPP_XOR_EQ:
8823 case CPP_OR_EQ:
8824 case CPP_XOR:
8825 case CPP_OR:
8826 case CPP_OR_OR:
8827 case CPP_EOF:
8828 case CPP_ELLIPSIS:
8829 return 0;
8831 case CPP_OPEN_PAREN:
8832 /* In ((type ()) () the last () isn't a valid cast-expression,
8833 so the whole must be parsed as postfix-expression. */
8834 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8835 != CPP_CLOSE_PAREN;
8837 case CPP_OPEN_SQUARE:
8838 /* '[' may start a primary-expression in obj-c++ and in C++11,
8839 as a lambda-expression, eg, '(void)[]{}'. */
8840 if (cxx_dialect >= cxx11)
8841 return -1;
8842 return c_dialect_objc ();
8844 case CPP_PLUS_PLUS:
8845 case CPP_MINUS_MINUS:
8846 /* '++' and '--' may or may not start a cast-expression:
8848 struct T { void operator++(int); };
8849 void f() { (T())++; }
8853 int a;
8854 (int)++a; */
8855 return -1;
8857 default:
8858 return 1;
8862 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8863 in the order: const_cast, static_cast, reinterpret_cast.
8865 Don't suggest dynamic_cast.
8867 Return the first legal cast kind found, or NULL otherwise. */
8869 static const char *
8870 get_cast_suggestion (tree dst_type, tree orig_expr)
8872 tree trial;
8874 /* Reuse the parser logic by attempting to build the various kinds of
8875 cast, with "complain" disabled.
8876 Identify the first such cast that is valid. */
8878 /* Don't attempt to run such logic within template processing. */
8879 if (processing_template_decl)
8880 return NULL;
8882 /* First try const_cast. */
8883 trial = build_const_cast (dst_type, orig_expr, tf_none);
8884 if (trial != error_mark_node)
8885 return "const_cast";
8887 /* If that fails, try static_cast. */
8888 trial = build_static_cast (dst_type, orig_expr, tf_none);
8889 if (trial != error_mark_node)
8890 return "static_cast";
8892 /* Finally, try reinterpret_cast. */
8893 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8894 if (trial != error_mark_node)
8895 return "reinterpret_cast";
8897 /* No such cast possible. */
8898 return NULL;
8901 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8902 suggesting how to convert a C-style cast of the form:
8904 (DST_TYPE)ORIG_EXPR
8906 to a C++-style cast.
8908 The primary range of RICHLOC is asssumed to be that of the original
8909 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8910 of the parens in the C-style cast. */
8912 static void
8913 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8914 location_t close_paren_loc, tree orig_expr,
8915 tree dst_type)
8917 /* This function is non-trivial, so bail out now if the warning isn't
8918 going to be emitted. */
8919 if (!warn_old_style_cast)
8920 return;
8922 /* Try to find a legal C++ cast, trying them in order:
8923 const_cast, static_cast, reinterpret_cast. */
8924 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8925 if (!cast_suggestion)
8926 return;
8928 /* Replace the open paren with "CAST_SUGGESTION<". */
8929 pretty_printer pp;
8930 pp_printf (&pp, "%s<", cast_suggestion);
8931 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8933 /* Replace the close paren with "> (". */
8934 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8936 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8937 rich_loc->add_fixit_insert_after (")");
8941 /* Parse a cast-expression.
8943 cast-expression:
8944 unary-expression
8945 ( type-id ) cast-expression
8947 ADDRESS_P is true iff the unary-expression is appearing as the
8948 operand of the `&' operator. CAST_P is true if this expression is
8949 the target of a cast.
8951 Returns a representation of the expression. */
8953 static cp_expr
8954 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8955 bool decltype_p, cp_id_kind * pidk)
8957 /* If it's a `(', then we might be looking at a cast. */
8958 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8960 tree type = NULL_TREE;
8961 cp_expr expr (NULL_TREE);
8962 int cast_expression = 0;
8963 const char *saved_message;
8965 /* There's no way to know yet whether or not this is a cast.
8966 For example, `(int (3))' is a unary-expression, while `(int)
8967 3' is a cast. So, we resort to parsing tentatively. */
8968 cp_parser_parse_tentatively (parser);
8969 /* Types may not be defined in a cast. */
8970 saved_message = parser->type_definition_forbidden_message;
8971 parser->type_definition_forbidden_message
8972 = G_("types may not be defined in casts");
8973 /* Consume the `('. */
8974 matching_parens parens;
8975 cp_token *open_paren = parens.consume_open (parser);
8976 location_t open_paren_loc = open_paren->location;
8977 location_t close_paren_loc = UNKNOWN_LOCATION;
8979 /* A very tricky bit is that `(struct S) { 3 }' is a
8980 compound-literal (which we permit in C++ as an extension).
8981 But, that construct is not a cast-expression -- it is a
8982 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8983 is legal; if the compound-literal were a cast-expression,
8984 you'd need an extra set of parentheses.) But, if we parse
8985 the type-id, and it happens to be a class-specifier, then we
8986 will commit to the parse at that point, because we cannot
8987 undo the action that is done when creating a new class. So,
8988 then we cannot back up and do a postfix-expression.
8990 Another tricky case is the following (c++/29234):
8992 struct S { void operator () (); };
8994 void foo ()
8996 ( S()() );
8999 As a type-id we parse the parenthesized S()() as a function
9000 returning a function, groktypename complains and we cannot
9001 back up in this case either.
9003 Therefore, we scan ahead to the closing `)', and check to see
9004 if the tokens after the `)' can start a cast-expression. Otherwise
9005 we are dealing with an unary-expression, a postfix-expression
9006 or something else.
9008 Yet another tricky case, in C++11, is the following (c++/54891):
9010 (void)[]{};
9012 The issue is that usually, besides the case of lambda-expressions,
9013 the parenthesized type-id cannot be followed by '[', and, eg, we
9014 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9015 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9016 we don't commit, we try a cast-expression, then an unary-expression.
9018 Save tokens so that we can put them back. */
9019 cp_lexer_save_tokens (parser->lexer);
9021 /* We may be looking at a cast-expression. */
9022 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9023 /*consume_paren=*/true))
9024 cast_expression
9025 = cp_parser_tokens_start_cast_expression (parser);
9027 /* Roll back the tokens we skipped. */
9028 cp_lexer_rollback_tokens (parser->lexer);
9029 /* If we aren't looking at a cast-expression, simulate an error so
9030 that the call to cp_parser_error_occurred below returns true. */
9031 if (!cast_expression)
9032 cp_parser_simulate_error (parser);
9033 else
9035 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9036 parser->in_type_id_in_expr_p = true;
9037 /* Look for the type-id. */
9038 type = cp_parser_type_id (parser);
9039 /* Look for the closing `)'. */
9040 cp_token *close_paren = parens.require_close (parser);
9041 if (close_paren)
9042 close_paren_loc = close_paren->location;
9043 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9046 /* Restore the saved message. */
9047 parser->type_definition_forbidden_message = saved_message;
9049 /* At this point this can only be either a cast or a
9050 parenthesized ctor such as `(T ())' that looks like a cast to
9051 function returning T. */
9052 if (!cp_parser_error_occurred (parser))
9054 /* Only commit if the cast-expression doesn't start with
9055 '++', '--', or '[' in C++11. */
9056 if (cast_expression > 0)
9057 cp_parser_commit_to_topmost_tentative_parse (parser);
9059 expr = cp_parser_cast_expression (parser,
9060 /*address_p=*/false,
9061 /*cast_p=*/true,
9062 /*decltype_p=*/false,
9063 pidk);
9065 if (cp_parser_parse_definitely (parser))
9067 /* Warn about old-style casts, if so requested. */
9068 if (warn_old_style_cast
9069 && !in_system_header_at (input_location)
9070 && !VOID_TYPE_P (type)
9071 && current_lang_name != lang_name_c)
9073 gcc_rich_location rich_loc (input_location);
9074 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9075 expr, type);
9076 warning_at (&rich_loc, OPT_Wold_style_cast,
9077 "use of old-style cast to %q#T", type);
9080 /* Only type conversions to integral or enumeration types
9081 can be used in constant-expressions. */
9082 if (!cast_valid_in_integral_constant_expression_p (type)
9083 && cp_parser_non_integral_constant_expression (parser,
9084 NIC_CAST))
9085 return error_mark_node;
9087 /* Perform the cast. */
9088 /* Make a location:
9089 (TYPE) EXPR
9090 ^~~~~~~~~~~
9091 with start==caret at the open paren, extending to the
9092 end of "expr". */
9093 location_t cast_loc = make_location (open_paren_loc,
9094 open_paren_loc,
9095 expr.get_finish ());
9096 expr = build_c_cast (cast_loc, type, expr);
9097 return expr;
9100 else
9101 cp_parser_abort_tentative_parse (parser);
9104 /* If we get here, then it's not a cast, so it must be a
9105 unary-expression. */
9106 return cp_parser_unary_expression (parser, pidk, address_p,
9107 cast_p, decltype_p);
9110 /* Parse a binary expression of the general form:
9112 pm-expression:
9113 cast-expression
9114 pm-expression .* cast-expression
9115 pm-expression ->* cast-expression
9117 multiplicative-expression:
9118 pm-expression
9119 multiplicative-expression * pm-expression
9120 multiplicative-expression / pm-expression
9121 multiplicative-expression % pm-expression
9123 additive-expression:
9124 multiplicative-expression
9125 additive-expression + multiplicative-expression
9126 additive-expression - multiplicative-expression
9128 shift-expression:
9129 additive-expression
9130 shift-expression << additive-expression
9131 shift-expression >> additive-expression
9133 relational-expression:
9134 shift-expression
9135 relational-expression < shift-expression
9136 relational-expression > shift-expression
9137 relational-expression <= shift-expression
9138 relational-expression >= shift-expression
9140 GNU Extension:
9142 relational-expression:
9143 relational-expression <? shift-expression
9144 relational-expression >? shift-expression
9146 equality-expression:
9147 relational-expression
9148 equality-expression == relational-expression
9149 equality-expression != relational-expression
9151 and-expression:
9152 equality-expression
9153 and-expression & equality-expression
9155 exclusive-or-expression:
9156 and-expression
9157 exclusive-or-expression ^ and-expression
9159 inclusive-or-expression:
9160 exclusive-or-expression
9161 inclusive-or-expression | exclusive-or-expression
9163 logical-and-expression:
9164 inclusive-or-expression
9165 logical-and-expression && inclusive-or-expression
9167 logical-or-expression:
9168 logical-and-expression
9169 logical-or-expression || logical-and-expression
9171 All these are implemented with a single function like:
9173 binary-expression:
9174 simple-cast-expression
9175 binary-expression <token> binary-expression
9177 CAST_P is true if this expression is the target of a cast.
9179 The binops_by_token map is used to get the tree codes for each <token> type.
9180 binary-expressions are associated according to a precedence table. */
9182 #define TOKEN_PRECEDENCE(token) \
9183 (((token->type == CPP_GREATER \
9184 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9185 && !parser->greater_than_is_operator_p) \
9186 ? PREC_NOT_OPERATOR \
9187 : binops_by_token[token->type].prec)
9189 static cp_expr
9190 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9191 bool no_toplevel_fold_p,
9192 bool decltype_p,
9193 enum cp_parser_prec prec,
9194 cp_id_kind * pidk)
9196 cp_parser_expression_stack stack;
9197 cp_parser_expression_stack_entry *sp = &stack[0];
9198 cp_parser_expression_stack_entry current;
9199 cp_expr rhs;
9200 cp_token *token;
9201 enum tree_code rhs_type;
9202 enum cp_parser_prec new_prec, lookahead_prec;
9203 tree overload;
9205 /* Parse the first expression. */
9206 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9207 ? TRUTH_NOT_EXPR : ERROR_MARK);
9208 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9209 cast_p, decltype_p, pidk);
9210 current.prec = prec;
9212 if (cp_parser_error_occurred (parser))
9213 return error_mark_node;
9215 for (;;)
9217 /* Get an operator token. */
9218 token = cp_lexer_peek_token (parser->lexer);
9220 if (warn_cxx11_compat
9221 && token->type == CPP_RSHIFT
9222 && !parser->greater_than_is_operator_p)
9224 if (warning_at (token->location, OPT_Wc__11_compat,
9225 "%<>>%> operator is treated"
9226 " as two right angle brackets in C++11"))
9227 inform (token->location,
9228 "suggest parentheses around %<>>%> expression");
9231 new_prec = TOKEN_PRECEDENCE (token);
9232 if (new_prec != PREC_NOT_OPERATOR
9233 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9234 /* This is a fold-expression; handle it later. */
9235 new_prec = PREC_NOT_OPERATOR;
9237 /* Popping an entry off the stack means we completed a subexpression:
9238 - either we found a token which is not an operator (`>' where it is not
9239 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9240 will happen repeatedly;
9241 - or, we found an operator which has lower priority. This is the case
9242 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9243 parsing `3 * 4'. */
9244 if (new_prec <= current.prec)
9246 if (sp == stack)
9247 break;
9248 else
9249 goto pop;
9252 get_rhs:
9253 current.tree_type = binops_by_token[token->type].tree_type;
9254 current.loc = token->location;
9256 /* We used the operator token. */
9257 cp_lexer_consume_token (parser->lexer);
9259 /* For "false && x" or "true || x", x will never be executed;
9260 disable warnings while evaluating it. */
9261 if (current.tree_type == TRUTH_ANDIF_EXPR)
9262 c_inhibit_evaluation_warnings +=
9263 cp_fully_fold (current.lhs) == truthvalue_false_node;
9264 else if (current.tree_type == TRUTH_ORIF_EXPR)
9265 c_inhibit_evaluation_warnings +=
9266 cp_fully_fold (current.lhs) == truthvalue_true_node;
9268 /* Extract another operand. It may be the RHS of this expression
9269 or the LHS of a new, higher priority expression. */
9270 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9271 ? TRUTH_NOT_EXPR : ERROR_MARK);
9272 rhs = cp_parser_simple_cast_expression (parser);
9274 /* Get another operator token. Look up its precedence to avoid
9275 building a useless (immediately popped) stack entry for common
9276 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9277 token = cp_lexer_peek_token (parser->lexer);
9278 lookahead_prec = TOKEN_PRECEDENCE (token);
9279 if (lookahead_prec != PREC_NOT_OPERATOR
9280 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9281 lookahead_prec = PREC_NOT_OPERATOR;
9282 if (lookahead_prec > new_prec)
9284 /* ... and prepare to parse the RHS of the new, higher priority
9285 expression. Since precedence levels on the stack are
9286 monotonically increasing, we do not have to care about
9287 stack overflows. */
9288 *sp = current;
9289 ++sp;
9290 current.lhs = rhs;
9291 current.lhs_type = rhs_type;
9292 current.prec = new_prec;
9293 new_prec = lookahead_prec;
9294 goto get_rhs;
9296 pop:
9297 lookahead_prec = new_prec;
9298 /* If the stack is not empty, we have parsed into LHS the right side
9299 (`4' in the example above) of an expression we had suspended.
9300 We can use the information on the stack to recover the LHS (`3')
9301 from the stack together with the tree code (`MULT_EXPR'), and
9302 the precedence of the higher level subexpression
9303 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9304 which will be used to actually build the additive expression. */
9305 rhs = current.lhs;
9306 rhs_type = current.lhs_type;
9307 --sp;
9308 current = *sp;
9311 /* Undo the disabling of warnings done above. */
9312 if (current.tree_type == TRUTH_ANDIF_EXPR)
9313 c_inhibit_evaluation_warnings -=
9314 cp_fully_fold (current.lhs) == truthvalue_false_node;
9315 else if (current.tree_type == TRUTH_ORIF_EXPR)
9316 c_inhibit_evaluation_warnings -=
9317 cp_fully_fold (current.lhs) == truthvalue_true_node;
9319 if (warn_logical_not_paren
9320 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9321 && current.lhs_type == TRUTH_NOT_EXPR
9322 /* Avoid warning for !!x == y. */
9323 && (TREE_CODE (current.lhs) != NE_EXPR
9324 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9325 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9326 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9327 /* Avoid warning for !b == y where b is boolean. */
9328 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9329 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9330 != BOOLEAN_TYPE))))
9331 /* Avoid warning for !!b == y where b is boolean. */
9332 && (!DECL_P (current.lhs)
9333 || TREE_TYPE (current.lhs) == NULL_TREE
9334 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9335 warn_logical_not_parentheses (current.loc, current.tree_type,
9336 current.lhs, maybe_constant_value (rhs));
9338 overload = NULL;
9340 location_t combined_loc = make_location (current.loc,
9341 current.lhs.get_start (),
9342 rhs.get_finish ());
9344 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9345 ERROR_MARK for everything that is not a binary expression.
9346 This makes warn_about_parentheses miss some warnings that
9347 involve unary operators. For unary expressions we should
9348 pass the correct tree_code unless the unary expression was
9349 surrounded by parentheses.
9351 if (no_toplevel_fold_p
9352 && lookahead_prec <= current.prec
9353 && sp == stack)
9355 if (current.lhs == error_mark_node || rhs == error_mark_node)
9356 current.lhs = error_mark_node;
9357 else
9359 current.lhs
9360 = build_min (current.tree_type,
9361 TREE_CODE_CLASS (current.tree_type)
9362 == tcc_comparison
9363 ? boolean_type_node : TREE_TYPE (current.lhs),
9364 current.lhs.get_value (), rhs.get_value ());
9365 SET_EXPR_LOCATION (current.lhs, combined_loc);
9368 else
9370 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9371 current.lhs, current.lhs_type,
9372 rhs, rhs_type, &overload,
9373 complain_flags (decltype_p));
9374 /* TODO: build_x_binary_op doesn't always honor the location. */
9375 current.lhs.set_location (combined_loc);
9377 current.lhs_type = current.tree_type;
9379 /* If the binary operator required the use of an overloaded operator,
9380 then this expression cannot be an integral constant-expression.
9381 An overloaded operator can be used even if both operands are
9382 otherwise permissible in an integral constant-expression if at
9383 least one of the operands is of enumeration type. */
9385 if (overload
9386 && cp_parser_non_integral_constant_expression (parser,
9387 NIC_OVERLOADED))
9388 return error_mark_node;
9391 return current.lhs;
9394 static cp_expr
9395 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9396 bool no_toplevel_fold_p,
9397 enum cp_parser_prec prec,
9398 cp_id_kind * pidk)
9400 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9401 /*decltype*/false, prec, pidk);
9404 /* Parse the `? expression : assignment-expression' part of a
9405 conditional-expression. The LOGICAL_OR_EXPR is the
9406 logical-or-expression that started the conditional-expression.
9407 Returns a representation of the entire conditional-expression.
9409 This routine is used by cp_parser_assignment_expression.
9411 ? expression : assignment-expression
9413 GNU Extensions:
9415 ? : assignment-expression */
9417 static tree
9418 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9420 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9421 cp_expr assignment_expr;
9422 struct cp_token *token;
9423 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9425 /* Consume the `?' token. */
9426 cp_lexer_consume_token (parser->lexer);
9427 token = cp_lexer_peek_token (parser->lexer);
9428 if (cp_parser_allow_gnu_extensions_p (parser)
9429 && token->type == CPP_COLON)
9431 pedwarn (token->location, OPT_Wpedantic,
9432 "ISO C++ does not allow ?: with omitted middle operand");
9433 /* Implicit true clause. */
9434 expr = NULL_TREE;
9435 c_inhibit_evaluation_warnings +=
9436 folded_logical_or_expr == truthvalue_true_node;
9437 warn_for_omitted_condop (token->location, logical_or_expr);
9439 else
9441 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9442 parser->colon_corrects_to_scope_p = false;
9443 /* Parse the expression. */
9444 c_inhibit_evaluation_warnings +=
9445 folded_logical_or_expr == truthvalue_false_node;
9446 expr = cp_parser_expression (parser);
9447 c_inhibit_evaluation_warnings +=
9448 ((folded_logical_or_expr == truthvalue_true_node)
9449 - (folded_logical_or_expr == truthvalue_false_node));
9450 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9453 /* The next token should be a `:'. */
9454 cp_parser_require (parser, CPP_COLON, RT_COLON);
9455 /* Parse the assignment-expression. */
9456 assignment_expr = cp_parser_assignment_expression (parser);
9457 c_inhibit_evaluation_warnings -=
9458 folded_logical_or_expr == truthvalue_true_node;
9460 /* Make a location:
9461 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9462 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9463 with the caret at the "?", ranging from the start of
9464 the logical_or_expr to the end of the assignment_expr. */
9465 loc = make_location (loc,
9466 logical_or_expr.get_start (),
9467 assignment_expr.get_finish ());
9469 /* Build the conditional-expression. */
9470 return build_x_conditional_expr (loc, logical_or_expr,
9471 expr,
9472 assignment_expr,
9473 tf_warning_or_error);
9476 /* Parse an assignment-expression.
9478 assignment-expression:
9479 conditional-expression
9480 logical-or-expression assignment-operator assignment_expression
9481 throw-expression
9483 CAST_P is true if this expression is the target of a cast.
9484 DECLTYPE_P is true if this expression is the operand of decltype.
9486 Returns a representation for the expression. */
9488 static cp_expr
9489 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9490 bool cast_p, bool decltype_p)
9492 cp_expr expr;
9494 /* If the next token is the `throw' keyword, then we're looking at
9495 a throw-expression. */
9496 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9497 expr = cp_parser_throw_expression (parser);
9498 /* Otherwise, it must be that we are looking at a
9499 logical-or-expression. */
9500 else
9502 /* Parse the binary expressions (logical-or-expression). */
9503 expr = cp_parser_binary_expression (parser, cast_p, false,
9504 decltype_p,
9505 PREC_NOT_OPERATOR, pidk);
9506 /* If the next token is a `?' then we're actually looking at a
9507 conditional-expression. */
9508 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9509 return cp_parser_question_colon_clause (parser, expr);
9510 else
9512 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9514 /* If it's an assignment-operator, we're using the second
9515 production. */
9516 enum tree_code assignment_operator
9517 = cp_parser_assignment_operator_opt (parser);
9518 if (assignment_operator != ERROR_MARK)
9520 bool non_constant_p;
9522 /* Parse the right-hand side of the assignment. */
9523 cp_expr rhs = cp_parser_initializer_clause (parser,
9524 &non_constant_p);
9526 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9527 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9529 /* An assignment may not appear in a
9530 constant-expression. */
9531 if (cp_parser_non_integral_constant_expression (parser,
9532 NIC_ASSIGNMENT))
9533 return error_mark_node;
9534 /* Build the assignment expression. Its default
9535 location:
9536 LHS = RHS
9537 ~~~~^~~~~
9538 is the location of the '=' token as the
9539 caret, ranging from the start of the lhs to the
9540 end of the rhs. */
9541 loc = make_location (loc,
9542 expr.get_start (),
9543 rhs.get_finish ());
9544 expr = build_x_modify_expr (loc, expr,
9545 assignment_operator,
9546 rhs,
9547 complain_flags (decltype_p));
9548 /* TODO: build_x_modify_expr doesn't honor the location,
9549 so we must set it here. */
9550 expr.set_location (loc);
9555 return expr;
9558 /* Parse an (optional) assignment-operator.
9560 assignment-operator: one of
9561 = *= /= %= += -= >>= <<= &= ^= |=
9563 GNU Extension:
9565 assignment-operator: one of
9566 <?= >?=
9568 If the next token is an assignment operator, the corresponding tree
9569 code is returned, and the token is consumed. For example, for
9570 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9571 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9572 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9573 operator, ERROR_MARK is returned. */
9575 static enum tree_code
9576 cp_parser_assignment_operator_opt (cp_parser* parser)
9578 enum tree_code op;
9579 cp_token *token;
9581 /* Peek at the next token. */
9582 token = cp_lexer_peek_token (parser->lexer);
9584 switch (token->type)
9586 case CPP_EQ:
9587 op = NOP_EXPR;
9588 break;
9590 case CPP_MULT_EQ:
9591 op = MULT_EXPR;
9592 break;
9594 case CPP_DIV_EQ:
9595 op = TRUNC_DIV_EXPR;
9596 break;
9598 case CPP_MOD_EQ:
9599 op = TRUNC_MOD_EXPR;
9600 break;
9602 case CPP_PLUS_EQ:
9603 op = PLUS_EXPR;
9604 break;
9606 case CPP_MINUS_EQ:
9607 op = MINUS_EXPR;
9608 break;
9610 case CPP_RSHIFT_EQ:
9611 op = RSHIFT_EXPR;
9612 break;
9614 case CPP_LSHIFT_EQ:
9615 op = LSHIFT_EXPR;
9616 break;
9618 case CPP_AND_EQ:
9619 op = BIT_AND_EXPR;
9620 break;
9622 case CPP_XOR_EQ:
9623 op = BIT_XOR_EXPR;
9624 break;
9626 case CPP_OR_EQ:
9627 op = BIT_IOR_EXPR;
9628 break;
9630 default:
9631 /* Nothing else is an assignment operator. */
9632 op = ERROR_MARK;
9635 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9636 if (op != ERROR_MARK
9637 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9638 op = ERROR_MARK;
9640 /* If it was an assignment operator, consume it. */
9641 if (op != ERROR_MARK)
9642 cp_lexer_consume_token (parser->lexer);
9644 return op;
9647 /* Parse an expression.
9649 expression:
9650 assignment-expression
9651 expression , assignment-expression
9653 CAST_P is true if this expression is the target of a cast.
9654 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9655 except possibly parenthesized or on the RHS of a comma (N3276).
9657 Returns a representation of the expression. */
9659 static cp_expr
9660 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9661 bool cast_p, bool decltype_p)
9663 cp_expr expression = NULL_TREE;
9664 location_t loc = UNKNOWN_LOCATION;
9666 while (true)
9668 cp_expr assignment_expression;
9670 /* Parse the next assignment-expression. */
9671 assignment_expression
9672 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9674 /* We don't create a temporary for a call that is the immediate operand
9675 of decltype or on the RHS of a comma. But when we see a comma, we
9676 need to create a temporary for a call on the LHS. */
9677 if (decltype_p && !processing_template_decl
9678 && TREE_CODE (assignment_expression) == CALL_EXPR
9679 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9680 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9681 assignment_expression
9682 = build_cplus_new (TREE_TYPE (assignment_expression),
9683 assignment_expression, tf_warning_or_error);
9685 /* If this is the first assignment-expression, we can just
9686 save it away. */
9687 if (!expression)
9688 expression = assignment_expression;
9689 else
9691 /* Create a location with caret at the comma, ranging
9692 from the start of the LHS to the end of the RHS. */
9693 loc = make_location (loc,
9694 expression.get_start (),
9695 assignment_expression.get_finish ());
9696 expression = build_x_compound_expr (loc, expression,
9697 assignment_expression,
9698 complain_flags (decltype_p));
9699 expression.set_location (loc);
9701 /* If the next token is not a comma, or we're in a fold-expression, then
9702 we are done with the expression. */
9703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9704 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9705 break;
9706 /* Consume the `,'. */
9707 loc = cp_lexer_peek_token (parser->lexer)->location;
9708 cp_lexer_consume_token (parser->lexer);
9709 /* A comma operator cannot appear in a constant-expression. */
9710 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9711 expression = error_mark_node;
9714 return expression;
9717 /* Parse a constant-expression.
9719 constant-expression:
9720 conditional-expression
9722 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9723 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9724 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9725 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9726 only parse a conditional-expression, otherwise parse an
9727 assignment-expression. See below for rationale. */
9729 static cp_expr
9730 cp_parser_constant_expression (cp_parser* parser,
9731 bool allow_non_constant_p,
9732 bool *non_constant_p,
9733 bool strict_p)
9735 bool saved_integral_constant_expression_p;
9736 bool saved_allow_non_integral_constant_expression_p;
9737 bool saved_non_integral_constant_expression_p;
9738 cp_expr expression;
9740 /* It might seem that we could simply parse the
9741 conditional-expression, and then check to see if it were
9742 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9743 one that the compiler can figure out is constant, possibly after
9744 doing some simplifications or optimizations. The standard has a
9745 precise definition of constant-expression, and we must honor
9746 that, even though it is somewhat more restrictive.
9748 For example:
9750 int i[(2, 3)];
9752 is not a legal declaration, because `(2, 3)' is not a
9753 constant-expression. The `,' operator is forbidden in a
9754 constant-expression. However, GCC's constant-folding machinery
9755 will fold this operation to an INTEGER_CST for `3'. */
9757 /* Save the old settings. */
9758 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9759 saved_allow_non_integral_constant_expression_p
9760 = parser->allow_non_integral_constant_expression_p;
9761 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9762 /* We are now parsing a constant-expression. */
9763 parser->integral_constant_expression_p = true;
9764 parser->allow_non_integral_constant_expression_p
9765 = (allow_non_constant_p || cxx_dialect >= cxx11);
9766 parser->non_integral_constant_expression_p = false;
9767 /* Although the grammar says "conditional-expression", when not STRICT_P,
9768 we parse an "assignment-expression", which also permits
9769 "throw-expression" and the use of assignment operators. In the case
9770 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9771 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9772 actually essential that we look for an assignment-expression.
9773 For example, cp_parser_initializer_clauses uses this function to
9774 determine whether a particular assignment-expression is in fact
9775 constant. */
9776 if (strict_p)
9778 /* Parse the binary expressions (logical-or-expression). */
9779 expression = cp_parser_binary_expression (parser, false, false, false,
9780 PREC_NOT_OPERATOR, NULL);
9781 /* If the next token is a `?' then we're actually looking at
9782 a conditional-expression; otherwise we're done. */
9783 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9784 expression = cp_parser_question_colon_clause (parser, expression);
9786 else
9787 expression = cp_parser_assignment_expression (parser);
9788 /* Restore the old settings. */
9789 parser->integral_constant_expression_p
9790 = saved_integral_constant_expression_p;
9791 parser->allow_non_integral_constant_expression_p
9792 = saved_allow_non_integral_constant_expression_p;
9793 if (cxx_dialect >= cxx11)
9795 /* Require an rvalue constant expression here; that's what our
9796 callers expect. Reference constant expressions are handled
9797 separately in e.g. cp_parser_template_argument. */
9798 tree decay = expression;
9799 if (TREE_TYPE (expression)
9800 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9801 decay = build_address (expression);
9802 bool is_const = potential_rvalue_constant_expression (decay);
9803 parser->non_integral_constant_expression_p = !is_const;
9804 if (!is_const && !allow_non_constant_p)
9805 require_potential_rvalue_constant_expression (decay);
9807 if (allow_non_constant_p)
9808 *non_constant_p = parser->non_integral_constant_expression_p;
9809 parser->non_integral_constant_expression_p
9810 = saved_non_integral_constant_expression_p;
9812 return expression;
9815 /* Parse __builtin_offsetof.
9817 offsetof-expression:
9818 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9820 offsetof-member-designator:
9821 id-expression
9822 | offsetof-member-designator "." id-expression
9823 | offsetof-member-designator "[" expression "]"
9824 | offsetof-member-designator "->" id-expression */
9826 static cp_expr
9827 cp_parser_builtin_offsetof (cp_parser *parser)
9829 int save_ice_p, save_non_ice_p;
9830 tree type;
9831 cp_expr expr;
9832 cp_id_kind dummy;
9833 cp_token *token;
9834 location_t finish_loc;
9836 /* We're about to accept non-integral-constant things, but will
9837 definitely yield an integral constant expression. Save and
9838 restore these values around our local parsing. */
9839 save_ice_p = parser->integral_constant_expression_p;
9840 save_non_ice_p = parser->non_integral_constant_expression_p;
9842 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9844 /* Consume the "__builtin_offsetof" token. */
9845 cp_lexer_consume_token (parser->lexer);
9846 /* Consume the opening `('. */
9847 matching_parens parens;
9848 parens.require_open (parser);
9849 /* Parse the type-id. */
9850 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9852 const char *saved_message = parser->type_definition_forbidden_message;
9853 parser->type_definition_forbidden_message
9854 = G_("types may not be defined within __builtin_offsetof");
9855 type = cp_parser_type_id (parser);
9856 parser->type_definition_forbidden_message = saved_message;
9858 /* Look for the `,'. */
9859 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9860 token = cp_lexer_peek_token (parser->lexer);
9862 /* Build the (type *)null that begins the traditional offsetof macro. */
9863 tree object_ptr
9864 = build_static_cast (build_pointer_type (type), null_pointer_node,
9865 tf_warning_or_error);
9867 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9868 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9869 true, &dummy, token->location);
9870 while (true)
9872 token = cp_lexer_peek_token (parser->lexer);
9873 switch (token->type)
9875 case CPP_OPEN_SQUARE:
9876 /* offsetof-member-designator "[" expression "]" */
9877 expr = cp_parser_postfix_open_square_expression (parser, expr,
9878 true, false);
9879 break;
9881 case CPP_DEREF:
9882 /* offsetof-member-designator "->" identifier */
9883 expr = grok_array_decl (token->location, expr,
9884 integer_zero_node, false);
9885 /* FALLTHRU */
9887 case CPP_DOT:
9888 /* offsetof-member-designator "." identifier */
9889 cp_lexer_consume_token (parser->lexer);
9890 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9891 expr, true, &dummy,
9892 token->location);
9893 break;
9895 case CPP_CLOSE_PAREN:
9896 /* Consume the ")" token. */
9897 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9898 cp_lexer_consume_token (parser->lexer);
9899 goto success;
9901 default:
9902 /* Error. We know the following require will fail, but
9903 that gives the proper error message. */
9904 parens.require_close (parser);
9905 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9906 expr = error_mark_node;
9907 goto failure;
9911 success:
9912 /* Make a location of the form:
9913 __builtin_offsetof (struct s, f)
9914 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9915 with caret at the type-id, ranging from the start of the
9916 "_builtin_offsetof" token to the close paren. */
9917 loc = make_location (loc, start_loc, finish_loc);
9918 /* The result will be an INTEGER_CST, so we need to explicitly
9919 preserve the location. */
9920 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9922 failure:
9923 parser->integral_constant_expression_p = save_ice_p;
9924 parser->non_integral_constant_expression_p = save_non_ice_p;
9926 expr = expr.maybe_add_location_wrapper ();
9927 return expr;
9930 /* Parse a trait expression.
9932 Returns a representation of the expression, the underlying type
9933 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9935 static cp_expr
9936 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9938 cp_trait_kind kind;
9939 tree type1, type2 = NULL_TREE;
9940 bool binary = false;
9941 bool variadic = false;
9943 switch (keyword)
9945 case RID_HAS_NOTHROW_ASSIGN:
9946 kind = CPTK_HAS_NOTHROW_ASSIGN;
9947 break;
9948 case RID_HAS_NOTHROW_CONSTRUCTOR:
9949 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9950 break;
9951 case RID_HAS_NOTHROW_COPY:
9952 kind = CPTK_HAS_NOTHROW_COPY;
9953 break;
9954 case RID_HAS_TRIVIAL_ASSIGN:
9955 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9956 break;
9957 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9958 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9959 break;
9960 case RID_HAS_TRIVIAL_COPY:
9961 kind = CPTK_HAS_TRIVIAL_COPY;
9962 break;
9963 case RID_HAS_TRIVIAL_DESTRUCTOR:
9964 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9965 break;
9966 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9967 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9968 break;
9969 case RID_HAS_VIRTUAL_DESTRUCTOR:
9970 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9971 break;
9972 case RID_IS_ABSTRACT:
9973 kind = CPTK_IS_ABSTRACT;
9974 break;
9975 case RID_IS_AGGREGATE:
9976 kind = CPTK_IS_AGGREGATE;
9977 break;
9978 case RID_IS_BASE_OF:
9979 kind = CPTK_IS_BASE_OF;
9980 binary = true;
9981 break;
9982 case RID_IS_CLASS:
9983 kind = CPTK_IS_CLASS;
9984 break;
9985 case RID_IS_EMPTY:
9986 kind = CPTK_IS_EMPTY;
9987 break;
9988 case RID_IS_ENUM:
9989 kind = CPTK_IS_ENUM;
9990 break;
9991 case RID_IS_FINAL:
9992 kind = CPTK_IS_FINAL;
9993 break;
9994 case RID_IS_LITERAL_TYPE:
9995 kind = CPTK_IS_LITERAL_TYPE;
9996 break;
9997 case RID_IS_POD:
9998 kind = CPTK_IS_POD;
9999 break;
10000 case RID_IS_POLYMORPHIC:
10001 kind = CPTK_IS_POLYMORPHIC;
10002 break;
10003 case RID_IS_SAME_AS:
10004 kind = CPTK_IS_SAME_AS;
10005 binary = true;
10006 break;
10007 case RID_IS_STD_LAYOUT:
10008 kind = CPTK_IS_STD_LAYOUT;
10009 break;
10010 case RID_IS_TRIVIAL:
10011 kind = CPTK_IS_TRIVIAL;
10012 break;
10013 case RID_IS_TRIVIALLY_ASSIGNABLE:
10014 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10015 binary = true;
10016 break;
10017 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10018 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10019 variadic = true;
10020 break;
10021 case RID_IS_TRIVIALLY_COPYABLE:
10022 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10023 break;
10024 case RID_IS_UNION:
10025 kind = CPTK_IS_UNION;
10026 break;
10027 case RID_UNDERLYING_TYPE:
10028 kind = CPTK_UNDERLYING_TYPE;
10029 break;
10030 case RID_BASES:
10031 kind = CPTK_BASES;
10032 break;
10033 case RID_DIRECT_BASES:
10034 kind = CPTK_DIRECT_BASES;
10035 break;
10036 case RID_IS_ASSIGNABLE:
10037 kind = CPTK_IS_ASSIGNABLE;
10038 binary = true;
10039 break;
10040 case RID_IS_CONSTRUCTIBLE:
10041 kind = CPTK_IS_CONSTRUCTIBLE;
10042 variadic = true;
10043 break;
10044 default:
10045 gcc_unreachable ();
10048 /* Get location of initial token. */
10049 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10051 /* Consume the token. */
10052 cp_lexer_consume_token (parser->lexer);
10054 matching_parens parens;
10055 parens.require_open (parser);
10058 type_id_in_expr_sentinel s (parser);
10059 type1 = cp_parser_type_id (parser);
10062 if (type1 == error_mark_node)
10063 return error_mark_node;
10065 if (binary)
10067 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10070 type_id_in_expr_sentinel s (parser);
10071 type2 = cp_parser_type_id (parser);
10074 if (type2 == error_mark_node)
10075 return error_mark_node;
10077 else if (variadic)
10079 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10081 cp_lexer_consume_token (parser->lexer);
10082 tree elt = cp_parser_type_id (parser);
10083 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10085 cp_lexer_consume_token (parser->lexer);
10086 elt = make_pack_expansion (elt);
10088 if (elt == error_mark_node)
10089 return error_mark_node;
10090 type2 = tree_cons (NULL_TREE, elt, type2);
10094 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10095 parens.require_close (parser);
10097 /* Construct a location of the form:
10098 __is_trivially_copyable(_Tp)
10099 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10100 with start == caret, finishing at the close-paren. */
10101 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10103 /* Complete the trait expression, which may mean either processing
10104 the trait expr now or saving it for template instantiation. */
10105 switch (kind)
10107 case CPTK_UNDERLYING_TYPE:
10108 return cp_expr (finish_underlying_type (type1), trait_loc);
10109 case CPTK_BASES:
10110 return cp_expr (finish_bases (type1, false), trait_loc);
10111 case CPTK_DIRECT_BASES:
10112 return cp_expr (finish_bases (type1, true), trait_loc);
10113 default:
10114 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10118 /* Parse a lambda expression.
10120 lambda-expression:
10121 lambda-introducer lambda-declarator [opt] compound-statement
10123 Returns a representation of the expression. */
10125 static cp_expr
10126 cp_parser_lambda_expression (cp_parser* parser)
10128 tree lambda_expr = build_lambda_expr ();
10129 tree type;
10130 bool ok = true;
10131 cp_token *token = cp_lexer_peek_token (parser->lexer);
10132 cp_token_position start = 0;
10134 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10136 if (cp_unevaluated_operand)
10138 if (!token->error_reported)
10140 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10141 "lambda-expression in unevaluated context");
10142 token->error_reported = true;
10144 ok = false;
10146 else if (parser->in_template_argument_list_p)
10148 if (!token->error_reported)
10150 error_at (token->location, "lambda-expression in template-argument");
10151 token->error_reported = true;
10153 ok = false;
10156 /* We may be in the middle of deferred access check. Disable
10157 it now. */
10158 push_deferring_access_checks (dk_no_deferred);
10160 cp_parser_lambda_introducer (parser, lambda_expr);
10162 type = begin_lambda_type (lambda_expr);
10163 if (type == error_mark_node)
10164 return error_mark_node;
10166 record_lambda_scope (lambda_expr);
10168 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10169 determine_visibility (TYPE_NAME (type));
10171 /* Now that we've started the type, add the capture fields for any
10172 explicit captures. */
10173 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10176 /* Inside the class, surrounding template-parameter-lists do not apply. */
10177 unsigned int saved_num_template_parameter_lists
10178 = parser->num_template_parameter_lists;
10179 unsigned char in_statement = parser->in_statement;
10180 bool in_switch_statement_p = parser->in_switch_statement_p;
10181 bool fully_implicit_function_template_p
10182 = parser->fully_implicit_function_template_p;
10183 tree implicit_template_parms = parser->implicit_template_parms;
10184 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10185 bool auto_is_implicit_function_template_parm_p
10186 = parser->auto_is_implicit_function_template_parm_p;
10188 parser->num_template_parameter_lists = 0;
10189 parser->in_statement = 0;
10190 parser->in_switch_statement_p = false;
10191 parser->fully_implicit_function_template_p = false;
10192 parser->implicit_template_parms = 0;
10193 parser->implicit_template_scope = 0;
10194 parser->auto_is_implicit_function_template_parm_p = false;
10196 /* By virtue of defining a local class, a lambda expression has access to
10197 the private variables of enclosing classes. */
10199 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10201 if (ok && cp_parser_error_occurred (parser))
10202 ok = false;
10204 if (ok)
10206 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10207 && cp_parser_start_tentative_firewall (parser))
10208 start = token;
10209 cp_parser_lambda_body (parser, lambda_expr);
10211 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10213 if (cp_parser_skip_to_closing_brace (parser))
10214 cp_lexer_consume_token (parser->lexer);
10217 /* The capture list was built up in reverse order; fix that now. */
10218 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10219 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10221 if (ok)
10222 maybe_add_lambda_conv_op (type);
10224 type = finish_struct (type, /*attributes=*/NULL_TREE);
10226 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10227 parser->in_statement = in_statement;
10228 parser->in_switch_statement_p = in_switch_statement_p;
10229 parser->fully_implicit_function_template_p
10230 = fully_implicit_function_template_p;
10231 parser->implicit_template_parms = implicit_template_parms;
10232 parser->implicit_template_scope = implicit_template_scope;
10233 parser->auto_is_implicit_function_template_parm_p
10234 = auto_is_implicit_function_template_parm_p;
10237 /* This field is only used during parsing of the lambda. */
10238 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10240 /* This lambda shouldn't have any proxies left at this point. */
10241 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10242 /* And now that we're done, push proxies for an enclosing lambda. */
10243 insert_pending_capture_proxies ();
10245 /* Update the lambda expression to a range. */
10246 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10247 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10248 token->location,
10249 end_tok->location);
10251 if (ok)
10252 lambda_expr = build_lambda_object (lambda_expr);
10253 else
10254 lambda_expr = error_mark_node;
10256 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10258 pop_deferring_access_checks ();
10260 return lambda_expr;
10263 /* Parse the beginning of a lambda expression.
10265 lambda-introducer:
10266 [ lambda-capture [opt] ]
10268 LAMBDA_EXPR is the current representation of the lambda expression. */
10270 static void
10271 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10273 /* Need commas after the first capture. */
10274 bool first = true;
10276 /* Eat the leading `['. */
10277 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10279 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10280 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10281 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10282 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10283 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10284 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10286 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10288 cp_lexer_consume_token (parser->lexer);
10289 first = false;
10291 if (!(at_function_scope_p () || parsing_nsdmi ()))
10292 error ("non-local lambda expression cannot have a capture-default");
10295 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10297 cp_token* capture_token;
10298 tree capture_id;
10299 tree capture_init_expr;
10300 cp_id_kind idk = CP_ID_KIND_NONE;
10301 bool explicit_init_p = false;
10303 enum capture_kind_type
10305 BY_COPY,
10306 BY_REFERENCE
10308 enum capture_kind_type capture_kind = BY_COPY;
10310 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10312 error ("expected end of capture-list");
10313 return;
10316 if (first)
10317 first = false;
10318 else
10319 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10321 /* Possibly capture `this'. */
10322 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10324 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10325 if (cxx_dialect < cxx2a
10326 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10327 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10328 "with by-copy capture default");
10329 cp_lexer_consume_token (parser->lexer);
10330 add_capture (lambda_expr,
10331 /*id=*/this_identifier,
10332 /*initializer=*/finish_this_expr (),
10333 /*by_reference_p=*/true,
10334 explicit_init_p);
10335 continue;
10338 /* Possibly capture `*this'. */
10339 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10340 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10342 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10343 if (cxx_dialect < cxx17)
10344 pedwarn (loc, 0, "%<*this%> capture only available with "
10345 "-std=c++17 or -std=gnu++17");
10346 cp_lexer_consume_token (parser->lexer);
10347 cp_lexer_consume_token (parser->lexer);
10348 add_capture (lambda_expr,
10349 /*id=*/this_identifier,
10350 /*initializer=*/finish_this_expr (),
10351 /*by_reference_p=*/false,
10352 explicit_init_p);
10353 continue;
10356 /* Remember whether we want to capture as a reference or not. */
10357 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10359 capture_kind = BY_REFERENCE;
10360 cp_lexer_consume_token (parser->lexer);
10363 /* Get the identifier. */
10364 capture_token = cp_lexer_peek_token (parser->lexer);
10365 capture_id = cp_parser_identifier (parser);
10367 if (capture_id == error_mark_node)
10368 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10369 delimiters, but I modified this to stop on unnested ']' as well. It
10370 was already changed to stop on unnested '}', so the
10371 "closing_parenthesis" name is no more misleading with my change. */
10373 cp_parser_skip_to_closing_parenthesis (parser,
10374 /*recovering=*/true,
10375 /*or_comma=*/true,
10376 /*consume_paren=*/true);
10377 break;
10380 /* Find the initializer for this capture. */
10381 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10382 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10383 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10385 bool direct, non_constant;
10386 /* An explicit initializer exists. */
10387 if (cxx_dialect < cxx14)
10388 pedwarn (input_location, 0,
10389 "lambda capture initializers "
10390 "only available with -std=c++14 or -std=gnu++14");
10391 capture_init_expr = cp_parser_initializer (parser, &direct,
10392 &non_constant, true);
10393 explicit_init_p = true;
10394 if (capture_init_expr == NULL_TREE)
10396 error ("empty initializer for lambda init-capture");
10397 capture_init_expr = error_mark_node;
10400 else
10402 const char* error_msg;
10404 /* Turn the identifier into an id-expression. */
10405 capture_init_expr
10406 = cp_parser_lookup_name_simple (parser, capture_id,
10407 capture_token->location);
10409 if (capture_init_expr == error_mark_node)
10411 unqualified_name_lookup_error (capture_id);
10412 continue;
10414 else if (!VAR_P (capture_init_expr)
10415 && TREE_CODE (capture_init_expr) != PARM_DECL)
10417 error_at (capture_token->location,
10418 "capture of non-variable %qE",
10419 capture_init_expr);
10420 if (DECL_P (capture_init_expr))
10421 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10422 "%q#D declared here", capture_init_expr);
10423 continue;
10425 if (VAR_P (capture_init_expr)
10426 && decl_storage_duration (capture_init_expr) != dk_auto)
10428 if (pedwarn (capture_token->location, 0, "capture of variable "
10429 "%qD with non-automatic storage duration",
10430 capture_init_expr))
10431 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10432 "%q#D declared here", capture_init_expr);
10433 continue;
10436 capture_init_expr
10437 = finish_id_expression
10438 (capture_id,
10439 capture_init_expr,
10440 parser->scope,
10441 &idk,
10442 /*integral_constant_expression_p=*/false,
10443 /*allow_non_integral_constant_expression_p=*/false,
10444 /*non_integral_constant_expression_p=*/NULL,
10445 /*template_p=*/false,
10446 /*done=*/true,
10447 /*address_p=*/false,
10448 /*template_arg_p=*/false,
10449 &error_msg,
10450 capture_token->location);
10452 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10454 cp_lexer_consume_token (parser->lexer);
10455 capture_init_expr = make_pack_expansion (capture_init_expr);
10459 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10460 && !explicit_init_p)
10462 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10463 && capture_kind == BY_COPY)
10464 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10465 "of %qD redundant with by-copy capture default",
10466 capture_id);
10467 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10468 && capture_kind == BY_REFERENCE)
10469 pedwarn (capture_token->location, 0, "explicit by-reference "
10470 "capture of %qD redundant with by-reference capture "
10471 "default", capture_id);
10474 add_capture (lambda_expr,
10475 capture_id,
10476 capture_init_expr,
10477 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10478 explicit_init_p);
10480 /* If there is any qualification still in effect, clear it
10481 now; we will be starting fresh with the next capture. */
10482 parser->scope = NULL_TREE;
10483 parser->qualifying_scope = NULL_TREE;
10484 parser->object_scope = NULL_TREE;
10487 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10490 /* Parse the (optional) middle of a lambda expression.
10492 lambda-declarator:
10493 < template-parameter-list [opt] >
10494 ( parameter-declaration-clause [opt] )
10495 attribute-specifier [opt]
10496 decl-specifier-seq [opt]
10497 exception-specification [opt]
10498 lambda-return-type-clause [opt]
10500 LAMBDA_EXPR is the current representation of the lambda expression. */
10502 static bool
10503 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10505 /* 5.1.1.4 of the standard says:
10506 If a lambda-expression does not include a lambda-declarator, it is as if
10507 the lambda-declarator were ().
10508 This means an empty parameter list, no attributes, and no exception
10509 specification. */
10510 tree param_list = void_list_node;
10511 tree attributes = NULL_TREE;
10512 tree exception_spec = NULL_TREE;
10513 tree template_param_list = NULL_TREE;
10514 tree tx_qual = NULL_TREE;
10515 tree return_type = NULL_TREE;
10516 cp_decl_specifier_seq lambda_specs;
10517 clear_decl_specs (&lambda_specs);
10519 /* The template-parameter-list is optional, but must begin with
10520 an opening angle if present. */
10521 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10523 if (cxx_dialect < cxx14)
10524 pedwarn (parser->lexer->next_token->location, 0,
10525 "lambda templates are only available with "
10526 "-std=c++14 or -std=gnu++14");
10527 else if (cxx_dialect < cxx2a)
10528 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10529 "lambda templates are only available with "
10530 "-std=c++2a or -std=gnu++2a");
10532 cp_lexer_consume_token (parser->lexer);
10534 template_param_list = cp_parser_template_parameter_list (parser);
10536 cp_parser_skip_to_end_of_template_parameter_list (parser);
10538 /* We just processed one more parameter list. */
10539 ++parser->num_template_parameter_lists;
10542 /* The parameter-declaration-clause is optional (unless
10543 template-parameter-list was given), but must begin with an
10544 opening parenthesis if present. */
10545 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10547 matching_parens parens;
10548 parens.consume_open (parser);
10550 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10552 /* Parse parameters. */
10553 param_list = cp_parser_parameter_declaration_clause (parser);
10555 /* Default arguments shall not be specified in the
10556 parameter-declaration-clause of a lambda-declarator. */
10557 if (cxx_dialect < cxx14)
10558 for (tree t = param_list; t; t = TREE_CHAIN (t))
10559 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10560 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10561 "default argument specified for lambda parameter");
10563 parens.require_close (parser);
10565 attributes = cp_parser_attributes_opt (parser);
10567 /* In the decl-specifier-seq of the lambda-declarator, each
10568 decl-specifier shall either be mutable or constexpr. */
10569 int declares_class_or_enum;
10570 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10571 cp_parser_decl_specifier_seq (parser,
10572 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10573 &lambda_specs, &declares_class_or_enum);
10574 if (lambda_specs.storage_class == sc_mutable)
10576 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10577 if (lambda_specs.conflicting_specifiers_p)
10578 error_at (lambda_specs.locations[ds_storage_class],
10579 "duplicate %<mutable%>");
10582 tx_qual = cp_parser_tx_qualifier_opt (parser);
10584 /* Parse optional exception specification. */
10585 exception_spec = cp_parser_exception_specification_opt (parser);
10587 /* Parse optional trailing return type. */
10588 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10590 cp_lexer_consume_token (parser->lexer);
10591 return_type = cp_parser_trailing_type_id (parser);
10594 /* The function parameters must be in scope all the way until after the
10595 trailing-return-type in case of decltype. */
10596 pop_bindings_and_leave_scope ();
10598 else if (template_param_list != NULL_TREE) // generate diagnostic
10599 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10601 /* Create the function call operator.
10603 Messing with declarators like this is no uglier than building up the
10604 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10605 other code. */
10607 cp_decl_specifier_seq return_type_specs;
10608 cp_declarator* declarator;
10609 tree fco;
10610 int quals;
10611 void *p;
10613 clear_decl_specs (&return_type_specs);
10614 return_type_specs.type = make_auto ();
10616 if (lambda_specs.locations[ds_constexpr])
10618 if (cxx_dialect >= cxx17)
10619 return_type_specs.locations[ds_constexpr]
10620 = lambda_specs.locations[ds_constexpr];
10621 else
10622 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10623 "lambda only available with -std=c++17 or -std=gnu++17");
10626 p = obstack_alloc (&declarator_obstack, 0);
10628 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10630 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10631 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10632 declarator = make_call_declarator (declarator, param_list, quals,
10633 VIRT_SPEC_UNSPECIFIED,
10634 REF_QUAL_NONE,
10635 tx_qual,
10636 exception_spec,
10637 /*late_return_type=*/NULL_TREE,
10638 /*requires_clause*/NULL_TREE);
10639 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10640 if (return_type)
10641 declarator->u.function.late_return_type = return_type;
10643 fco = grokmethod (&return_type_specs,
10644 declarator,
10645 attributes);
10646 if (fco != error_mark_node)
10648 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10649 DECL_ARTIFICIAL (fco) = 1;
10650 /* Give the object parameter a different name. */
10651 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10652 DECL_LAMBDA_FUNCTION (fco) = 1;
10654 if (template_param_list)
10656 fco = finish_member_template_decl (fco);
10657 finish_template_decl (template_param_list);
10658 --parser->num_template_parameter_lists;
10660 else if (parser->fully_implicit_function_template_p)
10661 fco = finish_fully_implicit_template (parser, fco);
10663 finish_member_declaration (fco);
10665 obstack_free (&declarator_obstack, p);
10667 return (fco != error_mark_node);
10671 /* Parse the body of a lambda expression, which is simply
10673 compound-statement
10675 but which requires special handling.
10676 LAMBDA_EXPR is the current representation of the lambda expression. */
10678 static void
10679 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10681 bool nested = (current_function_decl != NULL_TREE);
10682 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10683 bool in_function_body = parser->in_function_body;
10685 if (nested)
10686 push_function_context ();
10687 else
10688 /* Still increment function_depth so that we don't GC in the
10689 middle of an expression. */
10690 ++function_depth;
10692 vec<tree> omp_privatization_save;
10693 save_omp_privatization_clauses (omp_privatization_save);
10694 /* Clear this in case we're in the middle of a default argument. */
10695 parser->local_variables_forbidden_p = false;
10696 parser->in_function_body = true;
10699 local_specialization_stack s (lss_copy);
10700 tree fco = lambda_function (lambda_expr);
10701 tree body = start_lambda_function (fco, lambda_expr);
10702 matching_braces braces;
10704 if (braces.require_open (parser))
10706 tree compound_stmt = begin_compound_stmt (0);
10708 /* Originally C++11 required us to peek for 'return expr'; and
10709 process it specially here to deduce the return type. N3638
10710 removed the need for that. */
10712 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10713 cp_parser_label_declaration (parser);
10714 cp_parser_statement_seq_opt (parser, NULL_TREE);
10715 braces.require_close (parser);
10717 finish_compound_stmt (compound_stmt);
10720 finish_lambda_function (body);
10723 restore_omp_privatization_clauses (omp_privatization_save);
10724 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10725 parser->in_function_body = in_function_body;
10726 if (nested)
10727 pop_function_context();
10728 else
10729 --function_depth;
10732 /* Statements [gram.stmt.stmt] */
10734 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10736 static void
10737 add_debug_begin_stmt (location_t loc)
10739 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10740 return;
10741 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10742 /* A concept is never expanded normally. */
10743 return;
10745 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10746 SET_EXPR_LOCATION (stmt, loc);
10747 add_stmt (stmt);
10750 /* Parse a statement.
10752 statement:
10753 labeled-statement
10754 expression-statement
10755 compound-statement
10756 selection-statement
10757 iteration-statement
10758 jump-statement
10759 declaration-statement
10760 try-block
10762 C++11:
10764 statement:
10765 labeled-statement
10766 attribute-specifier-seq (opt) expression-statement
10767 attribute-specifier-seq (opt) compound-statement
10768 attribute-specifier-seq (opt) selection-statement
10769 attribute-specifier-seq (opt) iteration-statement
10770 attribute-specifier-seq (opt) jump-statement
10771 declaration-statement
10772 attribute-specifier-seq (opt) try-block
10774 init-statement:
10775 expression-statement
10776 simple-declaration
10778 TM Extension:
10780 statement:
10781 atomic-statement
10783 IN_COMPOUND is true when the statement is nested inside a
10784 cp_parser_compound_statement; this matters for certain pragmas.
10786 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10787 is a (possibly labeled) if statement which is not enclosed in braces
10788 and has an else clause. This is used to implement -Wparentheses.
10790 CHAIN is a vector of if-else-if conditions. */
10792 static void
10793 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10794 bool in_compound, bool *if_p, vec<tree> *chain,
10795 location_t *loc_after_labels)
10797 tree statement, std_attrs = NULL_TREE;
10798 cp_token *token;
10799 location_t statement_location, attrs_location;
10801 restart:
10802 if (if_p != NULL)
10803 *if_p = false;
10804 /* There is no statement yet. */
10805 statement = NULL_TREE;
10807 saved_token_sentinel saved_tokens (parser->lexer);
10808 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10809 if (c_dialect_objc ())
10810 /* In obj-c++, seeing '[[' might be the either the beginning of
10811 c++11 attributes, or a nested objc-message-expression. So
10812 let's parse the c++11 attributes tentatively. */
10813 cp_parser_parse_tentatively (parser);
10814 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10815 if (c_dialect_objc ())
10817 if (!cp_parser_parse_definitely (parser))
10818 std_attrs = NULL_TREE;
10821 /* Peek at the next token. */
10822 token = cp_lexer_peek_token (parser->lexer);
10823 /* Remember the location of the first token in the statement. */
10824 statement_location = token->location;
10825 add_debug_begin_stmt (statement_location);
10826 /* If this is a keyword, then that will often determine what kind of
10827 statement we have. */
10828 if (token->type == CPP_KEYWORD)
10830 enum rid keyword = token->keyword;
10832 switch (keyword)
10834 case RID_CASE:
10835 case RID_DEFAULT:
10836 /* Looks like a labeled-statement with a case label.
10837 Parse the label, and then use tail recursion to parse
10838 the statement. */
10839 cp_parser_label_for_labeled_statement (parser, std_attrs);
10840 in_compound = false;
10841 goto restart;
10843 case RID_IF:
10844 case RID_SWITCH:
10845 statement = cp_parser_selection_statement (parser, if_p, chain);
10846 break;
10848 case RID_WHILE:
10849 case RID_DO:
10850 case RID_FOR:
10851 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10852 break;
10854 case RID_BREAK:
10855 case RID_CONTINUE:
10856 case RID_RETURN:
10857 case RID_GOTO:
10858 statement = cp_parser_jump_statement (parser);
10859 break;
10861 /* Objective-C++ exception-handling constructs. */
10862 case RID_AT_TRY:
10863 case RID_AT_CATCH:
10864 case RID_AT_FINALLY:
10865 case RID_AT_SYNCHRONIZED:
10866 case RID_AT_THROW:
10867 statement = cp_parser_objc_statement (parser);
10868 break;
10870 case RID_TRY:
10871 statement = cp_parser_try_block (parser);
10872 break;
10874 case RID_NAMESPACE:
10875 /* This must be a namespace alias definition. */
10876 cp_parser_declaration_statement (parser);
10877 return;
10879 case RID_TRANSACTION_ATOMIC:
10880 case RID_TRANSACTION_RELAXED:
10881 case RID_SYNCHRONIZED:
10882 case RID_ATOMIC_NOEXCEPT:
10883 case RID_ATOMIC_CANCEL:
10884 statement = cp_parser_transaction (parser, token);
10885 break;
10886 case RID_TRANSACTION_CANCEL:
10887 statement = cp_parser_transaction_cancel (parser);
10888 break;
10890 default:
10891 /* It might be a keyword like `int' that can start a
10892 declaration-statement. */
10893 break;
10896 else if (token->type == CPP_NAME)
10898 /* If the next token is a `:', then we are looking at a
10899 labeled-statement. */
10900 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10901 if (token->type == CPP_COLON)
10903 /* Looks like a labeled-statement with an ordinary label.
10904 Parse the label, and then use tail recursion to parse
10905 the statement. */
10907 cp_parser_label_for_labeled_statement (parser, std_attrs);
10908 in_compound = false;
10909 goto restart;
10912 /* Anything that starts with a `{' must be a compound-statement. */
10913 else if (token->type == CPP_OPEN_BRACE)
10914 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10915 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10916 a statement all its own. */
10917 else if (token->type == CPP_PRAGMA)
10919 /* Only certain OpenMP pragmas are attached to statements, and thus
10920 are considered statements themselves. All others are not. In
10921 the context of a compound, accept the pragma as a "statement" and
10922 return so that we can check for a close brace. Otherwise we
10923 require a real statement and must go back and read one. */
10924 if (in_compound)
10925 cp_parser_pragma (parser, pragma_compound, if_p);
10926 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10927 goto restart;
10928 return;
10930 else if (token->type == CPP_EOF)
10932 cp_parser_error (parser, "expected statement");
10933 return;
10936 /* Everything else must be a declaration-statement or an
10937 expression-statement. Try for the declaration-statement
10938 first, unless we are looking at a `;', in which case we know that
10939 we have an expression-statement. */
10940 if (!statement)
10942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10944 if (std_attrs != NULL_TREE)
10946 /* Attributes should be parsed as part of the the
10947 declaration, so let's un-parse them. */
10948 saved_tokens.rollback();
10949 std_attrs = NULL_TREE;
10952 cp_parser_parse_tentatively (parser);
10953 /* Try to parse the declaration-statement. */
10954 cp_parser_declaration_statement (parser);
10955 /* If that worked, we're done. */
10956 if (cp_parser_parse_definitely (parser))
10957 return;
10959 /* All preceding labels have been parsed at this point. */
10960 if (loc_after_labels != NULL)
10961 *loc_after_labels = statement_location;
10963 /* Look for an expression-statement instead. */
10964 statement = cp_parser_expression_statement (parser, in_statement_expr);
10966 /* Handle [[fallthrough]];. */
10967 if (attribute_fallthrough_p (std_attrs))
10969 /* The next token after the fallthrough attribute is ';'. */
10970 if (statement == NULL_TREE)
10972 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10973 statement = build_call_expr_internal_loc (statement_location,
10974 IFN_FALLTHROUGH,
10975 void_type_node, 0);
10976 finish_expr_stmt (statement);
10978 else
10979 warning_at (statement_location, OPT_Wattributes,
10980 "%<fallthrough%> attribute not followed by %<;%>");
10981 std_attrs = NULL_TREE;
10985 /* Set the line number for the statement. */
10986 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10987 SET_EXPR_LOCATION (statement, statement_location);
10989 /* Allow "[[fallthrough]];", but warn otherwise. */
10990 if (std_attrs != NULL_TREE)
10991 warning_at (attrs_location,
10992 OPT_Wattributes,
10993 "attributes at the beginning of statement are ignored");
10996 /* Append ATTR to attribute list ATTRS. */
10998 static tree
10999 attr_chainon (tree attrs, tree attr)
11001 if (attrs == error_mark_node)
11002 return error_mark_node;
11003 if (attr == error_mark_node)
11004 return error_mark_node;
11005 return chainon (attrs, attr);
11008 /* Parse the label for a labeled-statement, i.e.
11010 identifier :
11011 case constant-expression :
11012 default :
11014 GNU Extension:
11015 case constant-expression ... constant-expression : statement
11017 When a label is parsed without errors, the label is added to the
11018 parse tree by the finish_* functions, so this function doesn't
11019 have to return the label. */
11021 static void
11022 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11024 cp_token *token;
11025 tree label = NULL_TREE;
11026 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11028 /* The next token should be an identifier. */
11029 token = cp_lexer_peek_token (parser->lexer);
11030 if (token->type != CPP_NAME
11031 && token->type != CPP_KEYWORD)
11033 cp_parser_error (parser, "expected labeled-statement");
11034 return;
11037 /* Remember whether this case or a user-defined label is allowed to fall
11038 through to. */
11039 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11041 parser->colon_corrects_to_scope_p = false;
11042 switch (token->keyword)
11044 case RID_CASE:
11046 tree expr, expr_hi;
11047 cp_token *ellipsis;
11049 /* Consume the `case' token. */
11050 cp_lexer_consume_token (parser->lexer);
11051 /* Parse the constant-expression. */
11052 expr = cp_parser_constant_expression (parser);
11053 if (check_for_bare_parameter_packs (expr))
11054 expr = error_mark_node;
11056 ellipsis = cp_lexer_peek_token (parser->lexer);
11057 if (ellipsis->type == CPP_ELLIPSIS)
11059 /* Consume the `...' token. */
11060 cp_lexer_consume_token (parser->lexer);
11061 expr_hi = cp_parser_constant_expression (parser);
11062 if (check_for_bare_parameter_packs (expr_hi))
11063 expr_hi = error_mark_node;
11065 /* We don't need to emit warnings here, as the common code
11066 will do this for us. */
11068 else
11069 expr_hi = NULL_TREE;
11071 if (parser->in_switch_statement_p)
11073 tree l = finish_case_label (token->location, expr, expr_hi);
11074 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11075 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11077 else
11078 error_at (token->location,
11079 "case label %qE not within a switch statement",
11080 expr);
11082 break;
11084 case RID_DEFAULT:
11085 /* Consume the `default' token. */
11086 cp_lexer_consume_token (parser->lexer);
11088 if (parser->in_switch_statement_p)
11090 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11091 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11092 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11094 else
11095 error_at (token->location, "case label not within a switch statement");
11096 break;
11098 default:
11099 /* Anything else must be an ordinary label. */
11100 label = finish_label_stmt (cp_parser_identifier (parser));
11101 if (label && TREE_CODE (label) == LABEL_DECL)
11102 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11103 break;
11106 /* Require the `:' token. */
11107 cp_parser_require (parser, CPP_COLON, RT_COLON);
11109 /* An ordinary label may optionally be followed by attributes.
11110 However, this is only permitted if the attributes are then
11111 followed by a semicolon. This is because, for backward
11112 compatibility, when parsing
11113 lab: __attribute__ ((unused)) int i;
11114 we want the attribute to attach to "i", not "lab". */
11115 if (label != NULL_TREE
11116 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11118 tree attrs;
11119 cp_parser_parse_tentatively (parser);
11120 attrs = cp_parser_gnu_attributes_opt (parser);
11121 if (attrs == NULL_TREE
11122 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11123 cp_parser_abort_tentative_parse (parser);
11124 else if (!cp_parser_parse_definitely (parser))
11126 else
11127 attributes = attr_chainon (attributes, attrs);
11130 if (attributes != NULL_TREE)
11131 cplus_decl_attributes (&label, attributes, 0);
11133 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11136 /* Parse an expression-statement.
11138 expression-statement:
11139 expression [opt] ;
11141 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11142 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11143 indicates whether this expression-statement is part of an
11144 expression statement. */
11146 static tree
11147 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11149 tree statement = NULL_TREE;
11150 cp_token *token = cp_lexer_peek_token (parser->lexer);
11151 location_t loc = token->location;
11153 /* There might be attribute fallthrough. */
11154 tree attr = cp_parser_gnu_attributes_opt (parser);
11156 /* If the next token is a ';', then there is no expression
11157 statement. */
11158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11160 statement = cp_parser_expression (parser);
11161 if (statement == error_mark_node
11162 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11164 cp_parser_skip_to_end_of_block_or_statement (parser);
11165 return error_mark_node;
11169 /* Handle [[fallthrough]];. */
11170 if (attribute_fallthrough_p (attr))
11172 /* The next token after the fallthrough attribute is ';'. */
11173 if (statement == NULL_TREE)
11174 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11175 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11176 void_type_node, 0);
11177 else
11178 warning_at (loc, OPT_Wattributes,
11179 "%<fallthrough%> attribute not followed by %<;%>");
11180 attr = NULL_TREE;
11183 /* Allow "[[fallthrough]];", but warn otherwise. */
11184 if (attr != NULL_TREE)
11185 warning_at (loc, OPT_Wattributes,
11186 "attributes at the beginning of statement are ignored");
11188 /* Give a helpful message for "A<T>::type t;" and the like. */
11189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11190 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11192 if (TREE_CODE (statement) == SCOPE_REF)
11193 error_at (token->location, "need %<typename%> before %qE because "
11194 "%qT is a dependent scope",
11195 statement, TREE_OPERAND (statement, 0));
11196 else if (is_overloaded_fn (statement)
11197 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11199 /* A::A a; */
11200 tree fn = get_first_fn (statement);
11201 error_at (token->location,
11202 "%<%T::%D%> names the constructor, not the type",
11203 DECL_CONTEXT (fn), DECL_NAME (fn));
11207 /* Consume the final `;'. */
11208 cp_parser_consume_semicolon_at_end_of_statement (parser);
11210 if (in_statement_expr
11211 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11212 /* This is the final expression statement of a statement
11213 expression. */
11214 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11215 else if (statement)
11216 statement = finish_expr_stmt (statement);
11218 return statement;
11221 /* Parse a compound-statement.
11223 compound-statement:
11224 { statement-seq [opt] }
11226 GNU extension:
11228 compound-statement:
11229 { label-declaration-seq [opt] statement-seq [opt] }
11231 label-declaration-seq:
11232 label-declaration
11233 label-declaration-seq label-declaration
11235 Returns a tree representing the statement. */
11237 static tree
11238 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11239 int bcs_flags, bool function_body)
11241 tree compound_stmt;
11242 matching_braces braces;
11244 /* Consume the `{'. */
11245 if (!braces.require_open (parser))
11246 return error_mark_node;
11247 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11248 && !function_body && cxx_dialect < cxx14)
11249 pedwarn (input_location, OPT_Wpedantic,
11250 "compound-statement in %<constexpr%> function");
11251 /* Begin the compound-statement. */
11252 compound_stmt = begin_compound_stmt (bcs_flags);
11253 /* If the next keyword is `__label__' we have a label declaration. */
11254 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11255 cp_parser_label_declaration (parser);
11256 /* Parse an (optional) statement-seq. */
11257 cp_parser_statement_seq_opt (parser, in_statement_expr);
11258 /* Finish the compound-statement. */
11259 finish_compound_stmt (compound_stmt);
11260 /* Consume the `}'. */
11261 braces.require_close (parser);
11263 return compound_stmt;
11266 /* Parse an (optional) statement-seq.
11268 statement-seq:
11269 statement
11270 statement-seq [opt] statement */
11272 static void
11273 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11275 /* Scan statements until there aren't any more. */
11276 while (true)
11278 cp_token *token = cp_lexer_peek_token (parser->lexer);
11280 /* If we are looking at a `}', then we have run out of
11281 statements; the same is true if we have reached the end
11282 of file, or have stumbled upon a stray '@end'. */
11283 if (token->type == CPP_CLOSE_BRACE
11284 || token->type == CPP_EOF
11285 || token->type == CPP_PRAGMA_EOL
11286 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11287 break;
11289 /* If we are in a compound statement and find 'else' then
11290 something went wrong. */
11291 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11293 if (parser->in_statement & IN_IF_STMT)
11294 break;
11295 else
11297 token = cp_lexer_consume_token (parser->lexer);
11298 error_at (token->location, "%<else%> without a previous %<if%>");
11302 /* Parse the statement. */
11303 cp_parser_statement (parser, in_statement_expr, true, NULL);
11307 /* Return true if this is the C++20 version of range-based-for with
11308 init-statement. */
11310 static bool
11311 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11313 bool r = false;
11315 /* Save tokens so that we can put them back. */
11316 cp_lexer_save_tokens (parser->lexer);
11318 /* There has to be an unnested ; followed by an unnested :. */
11319 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11320 /*recovering=*/false,
11321 CPP_SEMICOLON,
11322 /*consume_paren=*/false) != -1)
11323 goto out;
11325 /* We found the semicolon, eat it now. */
11326 cp_lexer_consume_token (parser->lexer);
11328 /* Now look for ':' that is not nested in () or {}. */
11329 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11330 /*recovering=*/false,
11331 CPP_COLON,
11332 /*consume_paren=*/false) == -1);
11334 out:
11335 /* Roll back the tokens we skipped. */
11336 cp_lexer_rollback_tokens (parser->lexer);
11338 return r;
11341 /* Return true if we're looking at (init; cond), false otherwise. */
11343 static bool
11344 cp_parser_init_statement_p (cp_parser *parser)
11346 /* Save tokens so that we can put them back. */
11347 cp_lexer_save_tokens (parser->lexer);
11349 /* Look for ';' that is not nested in () or {}. */
11350 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11351 /*recovering=*/false,
11352 CPP_SEMICOLON,
11353 /*consume_paren=*/false);
11355 /* Roll back the tokens we skipped. */
11356 cp_lexer_rollback_tokens (parser->lexer);
11358 return ret == -1;
11361 /* Parse a selection-statement.
11363 selection-statement:
11364 if ( init-statement [opt] condition ) statement
11365 if ( init-statement [opt] condition ) statement else statement
11366 switch ( init-statement [opt] condition ) statement
11368 Returns the new IF_STMT or SWITCH_STMT.
11370 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11371 is a (possibly labeled) if statement which is not enclosed in
11372 braces and has an else clause. This is used to implement
11373 -Wparentheses.
11375 CHAIN is a vector of if-else-if conditions. This is used to implement
11376 -Wduplicated-cond. */
11378 static tree
11379 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11380 vec<tree> *chain)
11382 cp_token *token;
11383 enum rid keyword;
11384 token_indent_info guard_tinfo;
11386 if (if_p != NULL)
11387 *if_p = false;
11389 /* Peek at the next token. */
11390 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11391 guard_tinfo = get_token_indent_info (token);
11393 /* See what kind of keyword it is. */
11394 keyword = token->keyword;
11395 switch (keyword)
11397 case RID_IF:
11398 case RID_SWITCH:
11400 tree statement;
11401 tree condition;
11403 bool cx = false;
11404 if (keyword == RID_IF
11405 && cp_lexer_next_token_is_keyword (parser->lexer,
11406 RID_CONSTEXPR))
11408 cx = true;
11409 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11410 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11411 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11412 "with -std=c++17 or -std=gnu++17");
11415 /* Look for the `('. */
11416 matching_parens parens;
11417 if (!parens.require_open (parser))
11419 cp_parser_skip_to_end_of_statement (parser);
11420 return error_mark_node;
11423 /* Begin the selection-statement. */
11424 if (keyword == RID_IF)
11426 statement = begin_if_stmt ();
11427 IF_STMT_CONSTEXPR_P (statement) = cx;
11429 else
11430 statement = begin_switch_stmt ();
11432 /* Parse the optional init-statement. */
11433 if (cp_parser_init_statement_p (parser))
11435 tree decl;
11436 if (cxx_dialect < cxx17)
11437 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11438 "init-statement in selection statements only available "
11439 "with -std=c++17 or -std=gnu++17");
11440 cp_parser_init_statement (parser, &decl);
11443 /* Parse the condition. */
11444 condition = cp_parser_condition (parser);
11445 /* Look for the `)'. */
11446 if (!parens.require_close (parser))
11447 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11448 /*consume_paren=*/true);
11450 if (keyword == RID_IF)
11452 bool nested_if;
11453 unsigned char in_statement;
11455 /* Add the condition. */
11456 condition = finish_if_stmt_cond (condition, statement);
11458 if (warn_duplicated_cond)
11459 warn_duplicated_cond_add_or_warn (token->location, condition,
11460 &chain);
11462 /* Parse the then-clause. */
11463 in_statement = parser->in_statement;
11464 parser->in_statement |= IN_IF_STMT;
11466 /* Outside a template, the non-selected branch of a constexpr
11467 if is a 'discarded statement', i.e. unevaluated. */
11468 bool was_discarded = in_discarded_stmt;
11469 bool discard_then = (cx && !processing_template_decl
11470 && integer_zerop (condition));
11471 if (discard_then)
11473 in_discarded_stmt = true;
11474 ++c_inhibit_evaluation_warnings;
11477 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11478 guard_tinfo);
11480 parser->in_statement = in_statement;
11482 finish_then_clause (statement);
11484 if (discard_then)
11486 THEN_CLAUSE (statement) = NULL_TREE;
11487 in_discarded_stmt = was_discarded;
11488 --c_inhibit_evaluation_warnings;
11491 /* If the next token is `else', parse the else-clause. */
11492 if (cp_lexer_next_token_is_keyword (parser->lexer,
11493 RID_ELSE))
11495 bool discard_else = (cx && !processing_template_decl
11496 && integer_nonzerop (condition));
11497 if (discard_else)
11499 in_discarded_stmt = true;
11500 ++c_inhibit_evaluation_warnings;
11503 guard_tinfo
11504 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11505 /* Consume the `else' keyword. */
11506 cp_lexer_consume_token (parser->lexer);
11507 if (warn_duplicated_cond)
11509 if (cp_lexer_next_token_is_keyword (parser->lexer,
11510 RID_IF)
11511 && chain == NULL)
11513 /* We've got "if (COND) else if (COND2)". Start
11514 the condition chain and add COND as the first
11515 element. */
11516 chain = new vec<tree> ();
11517 if (!CONSTANT_CLASS_P (condition)
11518 && !TREE_SIDE_EFFECTS (condition))
11520 /* Wrap it in a NOP_EXPR so that we can set the
11521 location of the condition. */
11522 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11523 condition);
11524 SET_EXPR_LOCATION (e, token->location);
11525 chain->safe_push (e);
11528 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11529 RID_IF))
11531 /* This is if-else without subsequent if. Zap the
11532 condition chain; we would have already warned at
11533 this point. */
11534 delete chain;
11535 chain = NULL;
11538 begin_else_clause (statement);
11539 /* Parse the else-clause. */
11540 cp_parser_implicitly_scoped_statement (parser, NULL,
11541 guard_tinfo, chain);
11543 finish_else_clause (statement);
11545 /* If we are currently parsing a then-clause, then
11546 IF_P will not be NULL. We set it to true to
11547 indicate that this if statement has an else clause.
11548 This may trigger the Wparentheses warning below
11549 when we get back up to the parent if statement. */
11550 if (if_p != NULL)
11551 *if_p = true;
11553 if (discard_else)
11555 ELSE_CLAUSE (statement) = NULL_TREE;
11556 in_discarded_stmt = was_discarded;
11557 --c_inhibit_evaluation_warnings;
11560 else
11562 /* This if statement does not have an else clause. If
11563 NESTED_IF is true, then the then-clause has an if
11564 statement which does have an else clause. We warn
11565 about the potential ambiguity. */
11566 if (nested_if)
11567 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11568 "suggest explicit braces to avoid ambiguous"
11569 " %<else%>");
11570 if (warn_duplicated_cond)
11572 /* We don't need the condition chain anymore. */
11573 delete chain;
11574 chain = NULL;
11578 /* Now we're all done with the if-statement. */
11579 finish_if_stmt (statement);
11581 else
11583 bool in_switch_statement_p;
11584 unsigned char in_statement;
11586 /* Add the condition. */
11587 finish_switch_cond (condition, statement);
11589 /* Parse the body of the switch-statement. */
11590 in_switch_statement_p = parser->in_switch_statement_p;
11591 in_statement = parser->in_statement;
11592 parser->in_switch_statement_p = true;
11593 parser->in_statement |= IN_SWITCH_STMT;
11594 cp_parser_implicitly_scoped_statement (parser, if_p,
11595 guard_tinfo);
11596 parser->in_switch_statement_p = in_switch_statement_p;
11597 parser->in_statement = in_statement;
11599 /* Now we're all done with the switch-statement. */
11600 finish_switch_stmt (statement);
11603 return statement;
11605 break;
11607 default:
11608 cp_parser_error (parser, "expected selection-statement");
11609 return error_mark_node;
11613 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11614 If we have seen at least one decl-specifier, and the next token
11615 is not a parenthesis, then we must be looking at a declaration.
11616 (After "int (" we might be looking at a functional cast.) */
11618 static void
11619 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11620 bool any_specifiers_p)
11622 if (any_specifiers_p
11623 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11624 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11625 && !cp_parser_error_occurred (parser))
11626 cp_parser_commit_to_tentative_parse (parser);
11629 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11630 The declarator shall not specify a function or an array. Returns
11631 TRUE if the declarator is valid, FALSE otherwise. */
11633 static bool
11634 cp_parser_check_condition_declarator (cp_parser* parser,
11635 cp_declarator *declarator,
11636 location_t loc)
11638 if (declarator == cp_error_declarator
11639 || function_declarator_p (declarator)
11640 || declarator->kind == cdk_array)
11642 if (declarator == cp_error_declarator)
11643 /* Already complained. */;
11644 else if (declarator->kind == cdk_array)
11645 error_at (loc, "condition declares an array");
11646 else
11647 error_at (loc, "condition declares a function");
11648 if (parser->fully_implicit_function_template_p)
11649 abort_fully_implicit_template (parser);
11650 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11651 /*or_comma=*/false,
11652 /*consume_paren=*/false);
11653 return false;
11655 else
11656 return true;
11659 /* Parse a condition.
11661 condition:
11662 expression
11663 type-specifier-seq declarator = initializer-clause
11664 type-specifier-seq declarator braced-init-list
11666 GNU Extension:
11668 condition:
11669 type-specifier-seq declarator asm-specification [opt]
11670 attributes [opt] = assignment-expression
11672 Returns the expression that should be tested. */
11674 static tree
11675 cp_parser_condition (cp_parser* parser)
11677 cp_decl_specifier_seq type_specifiers;
11678 const char *saved_message;
11679 int declares_class_or_enum;
11681 /* Try the declaration first. */
11682 cp_parser_parse_tentatively (parser);
11683 /* New types are not allowed in the type-specifier-seq for a
11684 condition. */
11685 saved_message = parser->type_definition_forbidden_message;
11686 parser->type_definition_forbidden_message
11687 = G_("types may not be defined in conditions");
11688 /* Parse the type-specifier-seq. */
11689 cp_parser_decl_specifier_seq (parser,
11690 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11691 &type_specifiers,
11692 &declares_class_or_enum);
11693 /* Restore the saved message. */
11694 parser->type_definition_forbidden_message = saved_message;
11696 cp_parser_maybe_commit_to_declaration (parser,
11697 type_specifiers.any_specifiers_p);
11699 /* If all is well, we might be looking at a declaration. */
11700 if (!cp_parser_error_occurred (parser))
11702 tree decl;
11703 tree asm_specification;
11704 tree attributes;
11705 cp_declarator *declarator;
11706 tree initializer = NULL_TREE;
11707 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11709 /* Parse the declarator. */
11710 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11711 /*ctor_dtor_or_conv_p=*/NULL,
11712 /*parenthesized_p=*/NULL,
11713 /*member_p=*/false,
11714 /*friend_p=*/false);
11715 /* Parse the attributes. */
11716 attributes = cp_parser_attributes_opt (parser);
11717 /* Parse the asm-specification. */
11718 asm_specification = cp_parser_asm_specification_opt (parser);
11719 /* If the next token is not an `=' or '{', then we might still be
11720 looking at an expression. For example:
11722 if (A(a).x)
11724 looks like a decl-specifier-seq and a declarator -- but then
11725 there is no `=', so this is an expression. */
11726 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11727 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11728 cp_parser_simulate_error (parser);
11730 /* If we did see an `=' or '{', then we are looking at a declaration
11731 for sure. */
11732 if (cp_parser_parse_definitely (parser))
11734 tree pushed_scope;
11735 bool non_constant_p = false;
11736 int flags = LOOKUP_ONLYCONVERTING;
11738 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11739 return error_mark_node;
11741 /* Create the declaration. */
11742 decl = start_decl (declarator, &type_specifiers,
11743 /*initialized_p=*/true,
11744 attributes, /*prefix_attributes=*/NULL_TREE,
11745 &pushed_scope);
11747 /* Parse the initializer. */
11748 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11750 initializer = cp_parser_braced_list (parser, &non_constant_p);
11751 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11752 flags = 0;
11754 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11756 /* Consume the `='. */
11757 cp_lexer_consume_token (parser->lexer);
11758 initializer = cp_parser_initializer_clause (parser,
11759 &non_constant_p);
11761 else
11763 cp_parser_error (parser, "expected initializer");
11764 initializer = error_mark_node;
11766 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11767 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11769 /* Process the initializer. */
11770 cp_finish_decl (decl,
11771 initializer, !non_constant_p,
11772 asm_specification,
11773 flags);
11775 if (pushed_scope)
11776 pop_scope (pushed_scope);
11778 return convert_from_reference (decl);
11781 /* If we didn't even get past the declarator successfully, we are
11782 definitely not looking at a declaration. */
11783 else
11784 cp_parser_abort_tentative_parse (parser);
11786 /* Otherwise, we are looking at an expression. */
11787 return cp_parser_expression (parser);
11790 /* Parses a for-statement or range-for-statement until the closing ')',
11791 not included. */
11793 static tree
11794 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11796 tree init, scope, decl;
11797 bool is_range_for;
11799 /* Begin the for-statement. */
11800 scope = begin_for_scope (&init);
11802 /* Parse the initialization. */
11803 is_range_for = cp_parser_init_statement (parser, &decl);
11805 if (is_range_for)
11806 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11807 else
11808 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11811 static tree
11812 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11813 unsigned short unroll)
11815 /* Normal for loop */
11816 tree condition = NULL_TREE;
11817 tree expression = NULL_TREE;
11818 tree stmt;
11820 stmt = begin_for_stmt (scope, init);
11821 /* The init-statement has already been parsed in
11822 cp_parser_init_statement, so no work is needed here. */
11823 finish_init_stmt (stmt);
11825 /* If there's a condition, process it. */
11826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11827 condition = cp_parser_condition (parser);
11828 else if (ivdep)
11830 cp_parser_error (parser, "missing loop condition in loop with "
11831 "%<GCC ivdep%> pragma");
11832 condition = error_mark_node;
11834 else if (unroll)
11836 cp_parser_error (parser, "missing loop condition in loop with "
11837 "%<GCC unroll%> pragma");
11838 condition = error_mark_node;
11840 finish_for_cond (condition, stmt, ivdep, unroll);
11841 /* Look for the `;'. */
11842 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11844 /* If there's an expression, process it. */
11845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11846 expression = cp_parser_expression (parser);
11847 finish_for_expr (expression, stmt);
11849 return stmt;
11852 /* Tries to parse a range-based for-statement:
11854 range-based-for:
11855 decl-specifier-seq declarator : expression
11857 The decl-specifier-seq declarator and the `:' are already parsed by
11858 cp_parser_init_statement. If processing_template_decl it returns a
11859 newly created RANGE_FOR_STMT; if not, it is converted to a
11860 regular FOR_STMT. */
11862 static tree
11863 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11864 bool ivdep, unsigned short unroll)
11866 tree stmt, range_expr;
11867 auto_vec <cxx_binding *, 16> bindings;
11868 auto_vec <tree, 16> names;
11869 tree decomp_first_name = NULL_TREE;
11870 unsigned int decomp_cnt = 0;
11872 /* Get the range declaration momentarily out of the way so that
11873 the range expression doesn't clash with it. */
11874 if (range_decl != error_mark_node)
11876 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11878 tree v = DECL_VALUE_EXPR (range_decl);
11879 /* For decomposition declaration get all of the corresponding
11880 declarations out of the way. */
11881 if (TREE_CODE (v) == ARRAY_REF
11882 && VAR_P (TREE_OPERAND (v, 0))
11883 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11885 tree d = range_decl;
11886 range_decl = TREE_OPERAND (v, 0);
11887 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11888 decomp_first_name = d;
11889 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11891 tree name = DECL_NAME (d);
11892 names.safe_push (name);
11893 bindings.safe_push (IDENTIFIER_BINDING (name));
11894 IDENTIFIER_BINDING (name)
11895 = IDENTIFIER_BINDING (name)->previous;
11899 if (names.is_empty ())
11901 tree name = DECL_NAME (range_decl);
11902 names.safe_push (name);
11903 bindings.safe_push (IDENTIFIER_BINDING (name));
11904 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11908 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11910 bool expr_non_constant_p;
11911 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11913 else
11914 range_expr = cp_parser_expression (parser);
11916 /* Put the range declaration(s) back into scope. */
11917 for (unsigned int i = 0; i < names.length (); i++)
11919 cxx_binding *binding = bindings[i];
11920 binding->previous = IDENTIFIER_BINDING (names[i]);
11921 IDENTIFIER_BINDING (names[i]) = binding;
11924 /* If in template, STMT is converted to a normal for-statement
11925 at instantiation. If not, it is done just ahead. */
11926 if (processing_template_decl)
11928 if (check_for_bare_parameter_packs (range_expr))
11929 range_expr = error_mark_node;
11930 stmt = begin_range_for_stmt (scope, init);
11931 if (ivdep)
11932 RANGE_FOR_IVDEP (stmt) = 1;
11933 if (unroll)
11934 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11935 finish_range_for_decl (stmt, range_decl, range_expr);
11936 if (!type_dependent_expression_p (range_expr)
11937 /* do_auto_deduction doesn't mess with template init-lists. */
11938 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11939 do_range_for_auto_deduction (range_decl, range_expr);
11941 else
11943 stmt = begin_for_stmt (scope, init);
11944 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11945 decomp_first_name, decomp_cnt, ivdep,
11946 unroll);
11948 return stmt;
11951 /* Subroutine of cp_convert_range_for: given the initializer expression,
11952 builds up the range temporary. */
11954 static tree
11955 build_range_temp (tree range_expr)
11957 tree range_type, range_temp;
11959 /* Find out the type deduced by the declaration
11960 `auto &&__range = range_expr'. */
11961 range_type = cp_build_reference_type (make_auto (), true);
11962 range_type = do_auto_deduction (range_type, range_expr,
11963 type_uses_auto (range_type));
11965 /* Create the __range variable. */
11966 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
11967 range_type);
11968 TREE_USED (range_temp) = 1;
11969 DECL_ARTIFICIAL (range_temp) = 1;
11971 return range_temp;
11974 /* Used by cp_parser_range_for in template context: we aren't going to
11975 do a full conversion yet, but we still need to resolve auto in the
11976 type of the for-range-declaration if present. This is basically
11977 a shortcut version of cp_convert_range_for. */
11979 static void
11980 do_range_for_auto_deduction (tree decl, tree range_expr)
11982 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11983 if (auto_node)
11985 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11986 range_temp = convert_from_reference (build_range_temp (range_expr));
11987 iter_type = (cp_parser_perform_range_for_lookup
11988 (range_temp, &begin_dummy, &end_dummy));
11989 if (iter_type)
11991 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11992 iter_type);
11993 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11994 RO_UNARY_STAR,
11995 tf_warning_or_error);
11996 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11997 iter_decl, auto_node);
12002 /* Converts a range-based for-statement into a normal
12003 for-statement, as per the definition.
12005 for (RANGE_DECL : RANGE_EXPR)
12006 BLOCK
12008 should be equivalent to:
12011 auto &&__range = RANGE_EXPR;
12012 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12013 __begin != __end;
12014 ++__begin)
12016 RANGE_DECL = *__begin;
12017 BLOCK
12021 If RANGE_EXPR is an array:
12022 BEGIN_EXPR = __range
12023 END_EXPR = __range + ARRAY_SIZE(__range)
12024 Else if RANGE_EXPR has a member 'begin' or 'end':
12025 BEGIN_EXPR = __range.begin()
12026 END_EXPR = __range.end()
12027 Else:
12028 BEGIN_EXPR = begin(__range)
12029 END_EXPR = end(__range);
12031 If __range has a member 'begin' but not 'end', or vice versa, we must
12032 still use the second alternative (it will surely fail, however).
12033 When calling begin()/end() in the third alternative we must use
12034 argument dependent lookup, but always considering 'std' as an associated
12035 namespace. */
12037 tree
12038 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12039 tree decomp_first_name, unsigned int decomp_cnt,
12040 bool ivdep, unsigned short unroll)
12042 tree begin, end;
12043 tree iter_type, begin_expr, end_expr;
12044 tree condition, expression;
12046 range_expr = mark_lvalue_use (range_expr);
12048 if (range_decl == error_mark_node || range_expr == error_mark_node)
12049 /* If an error happened previously do nothing or else a lot of
12050 unhelpful errors would be issued. */
12051 begin_expr = end_expr = iter_type = error_mark_node;
12052 else
12054 tree range_temp;
12056 if (VAR_P (range_expr)
12057 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12058 /* Can't bind a reference to an array of runtime bound. */
12059 range_temp = range_expr;
12060 else
12062 range_temp = build_range_temp (range_expr);
12063 pushdecl (range_temp);
12064 cp_finish_decl (range_temp, range_expr,
12065 /*is_constant_init*/false, NULL_TREE,
12066 LOOKUP_ONLYCONVERTING);
12067 range_temp = convert_from_reference (range_temp);
12069 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12070 &begin_expr, &end_expr);
12073 /* The new for initialization statement. */
12074 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12075 iter_type);
12076 TREE_USED (begin) = 1;
12077 DECL_ARTIFICIAL (begin) = 1;
12078 pushdecl (begin);
12079 cp_finish_decl (begin, begin_expr,
12080 /*is_constant_init*/false, NULL_TREE,
12081 LOOKUP_ONLYCONVERTING);
12083 if (cxx_dialect >= cxx17)
12084 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12085 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12086 TREE_USED (end) = 1;
12087 DECL_ARTIFICIAL (end) = 1;
12088 pushdecl (end);
12089 cp_finish_decl (end, end_expr,
12090 /*is_constant_init*/false, NULL_TREE,
12091 LOOKUP_ONLYCONVERTING);
12093 finish_init_stmt (statement);
12095 /* The new for condition. */
12096 condition = build_x_binary_op (input_location, NE_EXPR,
12097 begin, ERROR_MARK,
12098 end, ERROR_MARK,
12099 NULL, tf_warning_or_error);
12100 finish_for_cond (condition, statement, ivdep, unroll);
12102 /* The new increment expression. */
12103 expression = finish_unary_op_expr (input_location,
12104 PREINCREMENT_EXPR, begin,
12105 tf_warning_or_error);
12106 finish_for_expr (expression, statement);
12108 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12109 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12111 /* The declaration is initialized with *__begin inside the loop body. */
12112 cp_finish_decl (range_decl,
12113 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12114 tf_warning_or_error),
12115 /*is_constant_init*/false, NULL_TREE,
12116 LOOKUP_ONLYCONVERTING);
12117 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12118 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12120 return statement;
12123 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12124 We need to solve both at the same time because the method used
12125 depends on the existence of members begin or end.
12126 Returns the type deduced for the iterator expression. */
12128 static tree
12129 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12131 if (error_operand_p (range))
12133 *begin = *end = error_mark_node;
12134 return error_mark_node;
12137 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12139 error ("range-based %<for%> expression of type %qT "
12140 "has incomplete type", TREE_TYPE (range));
12141 *begin = *end = error_mark_node;
12142 return error_mark_node;
12144 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12146 /* If RANGE is an array, we will use pointer arithmetic. */
12147 *begin = decay_conversion (range, tf_warning_or_error);
12148 *end = build_binary_op (input_location, PLUS_EXPR,
12149 range,
12150 array_type_nelts_top (TREE_TYPE (range)),
12151 false);
12152 return TREE_TYPE (*begin);
12154 else
12156 /* If it is not an array, we must do a bit of magic. */
12157 tree id_begin, id_end;
12158 tree member_begin, member_end;
12160 *begin = *end = error_mark_node;
12162 id_begin = get_identifier ("begin");
12163 id_end = get_identifier ("end");
12164 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12165 /*protect=*/2, /*want_type=*/false,
12166 tf_warning_or_error);
12167 member_end = lookup_member (TREE_TYPE (range), id_end,
12168 /*protect=*/2, /*want_type=*/false,
12169 tf_warning_or_error);
12171 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12173 /* Use the member functions. */
12174 *begin = cp_parser_range_for_member_function (range, id_begin);
12175 *end = cp_parser_range_for_member_function (range, id_end);
12177 else
12179 /* Use global functions with ADL. */
12180 vec<tree, va_gc> *vec;
12181 vec = make_tree_vector ();
12183 vec_safe_push (vec, range);
12185 member_begin = perform_koenig_lookup (id_begin, vec,
12186 tf_warning_or_error);
12187 *begin = finish_call_expr (member_begin, &vec, false, true,
12188 tf_warning_or_error);
12189 member_end = perform_koenig_lookup (id_end, vec,
12190 tf_warning_or_error);
12191 *end = finish_call_expr (member_end, &vec, false, true,
12192 tf_warning_or_error);
12194 release_tree_vector (vec);
12197 /* Last common checks. */
12198 if (*begin == error_mark_node || *end == error_mark_node)
12200 /* If one of the expressions is an error do no more checks. */
12201 *begin = *end = error_mark_node;
12202 return error_mark_node;
12204 else if (type_dependent_expression_p (*begin)
12205 || type_dependent_expression_p (*end))
12206 /* Can happen, when, eg, in a template context, Koenig lookup
12207 can't resolve begin/end (c++/58503). */
12208 return NULL_TREE;
12209 else
12211 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12212 /* The unqualified type of the __begin and __end temporaries should
12213 be the same, as required by the multiple auto declaration. */
12214 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12216 if (cxx_dialect >= cxx17
12217 && (build_x_binary_op (input_location, NE_EXPR,
12218 *begin, ERROR_MARK,
12219 *end, ERROR_MARK,
12220 NULL, tf_none)
12221 != error_mark_node))
12222 /* P0184R0 allows __begin and __end to have different types,
12223 but make sure they are comparable so we can give a better
12224 diagnostic. */;
12225 else
12226 error ("inconsistent begin/end types in range-based %<for%> "
12227 "statement: %qT and %qT",
12228 TREE_TYPE (*begin), TREE_TYPE (*end));
12230 return iter_type;
12235 /* Helper function for cp_parser_perform_range_for_lookup.
12236 Builds a tree for RANGE.IDENTIFIER(). */
12238 static tree
12239 cp_parser_range_for_member_function (tree range, tree identifier)
12241 tree member, res;
12242 vec<tree, va_gc> *vec;
12244 member = finish_class_member_access_expr (range, identifier,
12245 false, tf_warning_or_error);
12246 if (member == error_mark_node)
12247 return error_mark_node;
12249 vec = make_tree_vector ();
12250 res = finish_call_expr (member, &vec,
12251 /*disallow_virtual=*/false,
12252 /*koenig_p=*/false,
12253 tf_warning_or_error);
12254 release_tree_vector (vec);
12255 return res;
12258 /* Parse an iteration-statement.
12260 iteration-statement:
12261 while ( condition ) statement
12262 do statement while ( expression ) ;
12263 for ( init-statement condition [opt] ; expression [opt] )
12264 statement
12266 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12268 static tree
12269 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12270 unsigned short unroll)
12272 cp_token *token;
12273 enum rid keyword;
12274 tree statement;
12275 unsigned char in_statement;
12276 token_indent_info guard_tinfo;
12278 /* Peek at the next token. */
12279 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12280 if (!token)
12281 return error_mark_node;
12283 guard_tinfo = get_token_indent_info (token);
12285 /* Remember whether or not we are already within an iteration
12286 statement. */
12287 in_statement = parser->in_statement;
12289 /* See what kind of keyword it is. */
12290 keyword = token->keyword;
12291 switch (keyword)
12293 case RID_WHILE:
12295 tree condition;
12297 /* Begin the while-statement. */
12298 statement = begin_while_stmt ();
12299 /* Look for the `('. */
12300 matching_parens parens;
12301 parens.require_open (parser);
12302 /* Parse the condition. */
12303 condition = cp_parser_condition (parser);
12304 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12305 /* Look for the `)'. */
12306 parens.require_close (parser);
12307 /* Parse the dependent statement. */
12308 parser->in_statement = IN_ITERATION_STMT;
12309 bool prev = note_iteration_stmt_body_start ();
12310 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12311 note_iteration_stmt_body_end (prev);
12312 parser->in_statement = in_statement;
12313 /* We're done with the while-statement. */
12314 finish_while_stmt (statement);
12316 break;
12318 case RID_DO:
12320 tree expression;
12322 /* Begin the do-statement. */
12323 statement = begin_do_stmt ();
12324 /* Parse the body of the do-statement. */
12325 parser->in_statement = IN_ITERATION_STMT;
12326 bool prev = note_iteration_stmt_body_start ();
12327 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12328 note_iteration_stmt_body_end (prev);
12329 parser->in_statement = in_statement;
12330 finish_do_body (statement);
12331 /* Look for the `while' keyword. */
12332 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12333 /* Look for the `('. */
12334 matching_parens parens;
12335 parens.require_open (parser);
12336 /* Parse the expression. */
12337 expression = cp_parser_expression (parser);
12338 /* We're done with the do-statement. */
12339 finish_do_stmt (expression, statement, ivdep, unroll);
12340 /* Look for the `)'. */
12341 parens.require_close (parser);
12342 /* Look for the `;'. */
12343 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12345 break;
12347 case RID_FOR:
12349 /* Look for the `('. */
12350 matching_parens parens;
12351 parens.require_open (parser);
12353 statement = cp_parser_for (parser, ivdep, unroll);
12355 /* Look for the `)'. */
12356 parens.require_close (parser);
12358 /* Parse the body of the for-statement. */
12359 parser->in_statement = IN_ITERATION_STMT;
12360 bool prev = note_iteration_stmt_body_start ();
12361 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12362 note_iteration_stmt_body_end (prev);
12363 parser->in_statement = in_statement;
12365 /* We're done with the for-statement. */
12366 finish_for_stmt (statement);
12368 break;
12370 default:
12371 cp_parser_error (parser, "expected iteration-statement");
12372 statement = error_mark_node;
12373 break;
12376 return statement;
12379 /* Parse a init-statement or the declarator of a range-based-for.
12380 Returns true if a range-based-for declaration is seen.
12382 init-statement:
12383 expression-statement
12384 simple-declaration */
12386 static bool
12387 cp_parser_init_statement (cp_parser *parser, tree *decl)
12389 /* If the next token is a `;', then we have an empty
12390 expression-statement. Grammatically, this is also a
12391 simple-declaration, but an invalid one, because it does not
12392 declare anything. Therefore, if we did not handle this case
12393 specially, we would issue an error message about an invalid
12394 declaration. */
12395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12397 bool is_range_for = false;
12398 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12400 /* Try to parse the init-statement. */
12401 if (cp_parser_range_based_for_with_init_p (parser))
12403 tree dummy;
12404 cp_parser_parse_tentatively (parser);
12405 /* Parse the declaration. */
12406 cp_parser_simple_declaration (parser,
12407 /*function_definition_allowed_p=*/false,
12408 &dummy);
12409 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12410 if (!cp_parser_parse_definitely (parser))
12411 /* That didn't work, try to parse it as an expression-statement. */
12412 cp_parser_expression_statement (parser, NULL_TREE);
12414 if (cxx_dialect < cxx2a)
12416 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12417 "range-based %<for%> loops with initializer only "
12418 "available with -std=c++2a or -std=gnu++2a");
12419 *decl = error_mark_node;
12423 /* A colon is used in range-based for. */
12424 parser->colon_corrects_to_scope_p = false;
12426 /* We're going to speculatively look for a declaration, falling back
12427 to an expression, if necessary. */
12428 cp_parser_parse_tentatively (parser);
12429 /* Parse the declaration. */
12430 cp_parser_simple_declaration (parser,
12431 /*function_definition_allowed_p=*/false,
12432 decl);
12433 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12434 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12436 /* It is a range-for, consume the ':'. */
12437 cp_lexer_consume_token (parser->lexer);
12438 is_range_for = true;
12439 if (cxx_dialect < cxx11)
12440 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12441 "range-based %<for%> loops only available with "
12442 "-std=c++11 or -std=gnu++11");
12444 else
12445 /* The ';' is not consumed yet because we told
12446 cp_parser_simple_declaration not to. */
12447 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12449 if (cp_parser_parse_definitely (parser))
12450 return is_range_for;
12451 /* If the tentative parse failed, then we shall need to look for an
12452 expression-statement. */
12454 /* If we are here, it is an expression-statement. */
12455 cp_parser_expression_statement (parser, NULL_TREE);
12456 return false;
12459 /* Parse a jump-statement.
12461 jump-statement:
12462 break ;
12463 continue ;
12464 return expression [opt] ;
12465 return braced-init-list ;
12466 goto identifier ;
12468 GNU extension:
12470 jump-statement:
12471 goto * expression ;
12473 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12475 static tree
12476 cp_parser_jump_statement (cp_parser* parser)
12478 tree statement = error_mark_node;
12479 cp_token *token;
12480 enum rid keyword;
12481 unsigned char in_statement;
12483 /* Peek at the next token. */
12484 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12485 if (!token)
12486 return error_mark_node;
12488 /* See what kind of keyword it is. */
12489 keyword = token->keyword;
12490 switch (keyword)
12492 case RID_BREAK:
12493 in_statement = parser->in_statement & ~IN_IF_STMT;
12494 switch (in_statement)
12496 case 0:
12497 error_at (token->location, "break statement not within loop or switch");
12498 break;
12499 default:
12500 gcc_assert ((in_statement & IN_SWITCH_STMT)
12501 || in_statement == IN_ITERATION_STMT);
12502 statement = finish_break_stmt ();
12503 if (in_statement == IN_ITERATION_STMT)
12504 break_maybe_infinite_loop ();
12505 break;
12506 case IN_OMP_BLOCK:
12507 error_at (token->location, "invalid exit from OpenMP structured block");
12508 break;
12509 case IN_OMP_FOR:
12510 error_at (token->location, "break statement used with OpenMP for loop");
12511 break;
12513 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12514 break;
12516 case RID_CONTINUE:
12517 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12519 case 0:
12520 error_at (token->location, "continue statement not within a loop");
12521 break;
12522 /* Fall through. */
12523 case IN_ITERATION_STMT:
12524 case IN_OMP_FOR:
12525 statement = finish_continue_stmt ();
12526 break;
12527 case IN_OMP_BLOCK:
12528 error_at (token->location, "invalid exit from OpenMP structured block");
12529 break;
12530 default:
12531 gcc_unreachable ();
12533 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12534 break;
12536 case RID_RETURN:
12538 tree expr;
12539 bool expr_non_constant_p;
12541 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12543 cp_lexer_set_source_position (parser->lexer);
12544 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12545 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12547 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12548 expr = cp_parser_expression (parser);
12549 else
12550 /* If the next token is a `;', then there is no
12551 expression. */
12552 expr = NULL_TREE;
12553 /* Build the return-statement. */
12554 if (current_function_auto_return_pattern && in_discarded_stmt)
12555 /* Don't deduce from a discarded return statement. */;
12556 else
12557 statement = finish_return_stmt (expr);
12558 /* Look for the final `;'. */
12559 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12561 break;
12563 case RID_GOTO:
12564 if (parser->in_function_body
12565 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12567 error ("%<goto%> in %<constexpr%> function");
12568 cp_function_chain->invalid_constexpr = true;
12571 /* Create the goto-statement. */
12572 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12574 /* Issue a warning about this use of a GNU extension. */
12575 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12576 /* Consume the '*' token. */
12577 cp_lexer_consume_token (parser->lexer);
12578 /* Parse the dependent expression. */
12579 finish_goto_stmt (cp_parser_expression (parser));
12581 else
12582 finish_goto_stmt (cp_parser_identifier (parser));
12583 /* Look for the final `;'. */
12584 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12585 break;
12587 default:
12588 cp_parser_error (parser, "expected jump-statement");
12589 break;
12592 return statement;
12595 /* Parse a declaration-statement.
12597 declaration-statement:
12598 block-declaration */
12600 static void
12601 cp_parser_declaration_statement (cp_parser* parser)
12603 void *p;
12605 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12606 p = obstack_alloc (&declarator_obstack, 0);
12608 /* Parse the block-declaration. */
12609 cp_parser_block_declaration (parser, /*statement_p=*/true);
12611 /* Free any declarators allocated. */
12612 obstack_free (&declarator_obstack, p);
12615 /* Some dependent statements (like `if (cond) statement'), are
12616 implicitly in their own scope. In other words, if the statement is
12617 a single statement (as opposed to a compound-statement), it is
12618 none-the-less treated as if it were enclosed in braces. Any
12619 declarations appearing in the dependent statement are out of scope
12620 after control passes that point. This function parses a statement,
12621 but ensures that is in its own scope, even if it is not a
12622 compound-statement.
12624 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12625 is a (possibly labeled) if statement which is not enclosed in
12626 braces and has an else clause. This is used to implement
12627 -Wparentheses.
12629 CHAIN is a vector of if-else-if conditions. This is used to implement
12630 -Wduplicated-cond.
12632 Returns the new statement. */
12634 static tree
12635 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12636 const token_indent_info &guard_tinfo,
12637 vec<tree> *chain)
12639 tree statement;
12640 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12641 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12642 token_indent_info body_tinfo
12643 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12645 if (if_p != NULL)
12646 *if_p = false;
12648 /* Mark if () ; with a special NOP_EXPR. */
12649 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12651 cp_lexer_consume_token (parser->lexer);
12652 statement = add_stmt (build_empty_stmt (body_loc));
12654 if (guard_tinfo.keyword == RID_IF
12655 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12656 warning_at (body_loc, OPT_Wempty_body,
12657 "suggest braces around empty body in an %<if%> statement");
12658 else if (guard_tinfo.keyword == RID_ELSE)
12659 warning_at (body_loc, OPT_Wempty_body,
12660 "suggest braces around empty body in an %<else%> statement");
12662 /* if a compound is opened, we simply parse the statement directly. */
12663 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12664 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12665 /* If the token is not a `{', then we must take special action. */
12666 else
12668 /* Create a compound-statement. */
12669 statement = begin_compound_stmt (0);
12670 /* Parse the dependent-statement. */
12671 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12672 &body_loc_after_labels);
12673 /* Finish the dummy compound-statement. */
12674 finish_compound_stmt (statement);
12677 token_indent_info next_tinfo
12678 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12679 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12681 if (body_loc_after_labels != UNKNOWN_LOCATION
12682 && next_tinfo.type != CPP_SEMICOLON)
12683 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12684 guard_tinfo.location, guard_tinfo.keyword);
12686 /* Return the statement. */
12687 return statement;
12690 /* For some dependent statements (like `while (cond) statement'), we
12691 have already created a scope. Therefore, even if the dependent
12692 statement is a compound-statement, we do not want to create another
12693 scope. */
12695 static void
12696 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12697 const token_indent_info &guard_tinfo)
12699 /* If the token is a `{', then we must take special action. */
12700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12702 token_indent_info body_tinfo
12703 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12704 location_t loc_after_labels = UNKNOWN_LOCATION;
12706 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12707 &loc_after_labels);
12708 token_indent_info next_tinfo
12709 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12710 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12712 if (loc_after_labels != UNKNOWN_LOCATION
12713 && next_tinfo.type != CPP_SEMICOLON)
12714 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12715 guard_tinfo.location,
12716 guard_tinfo.keyword);
12718 else
12720 /* Avoid calling cp_parser_compound_statement, so that we
12721 don't create a new scope. Do everything else by hand. */
12722 matching_braces braces;
12723 braces.require_open (parser);
12724 /* If the next keyword is `__label__' we have a label declaration. */
12725 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12726 cp_parser_label_declaration (parser);
12727 /* Parse an (optional) statement-seq. */
12728 cp_parser_statement_seq_opt (parser, NULL_TREE);
12729 braces.require_close (parser);
12733 /* Declarations [gram.dcl.dcl] */
12735 /* Parse an optional declaration-sequence.
12737 declaration-seq:
12738 declaration
12739 declaration-seq declaration */
12741 static void
12742 cp_parser_declaration_seq_opt (cp_parser* parser)
12744 while (true)
12746 cp_token *token = cp_lexer_peek_token (parser->lexer);
12748 if (token->type == CPP_CLOSE_BRACE
12749 || token->type == CPP_EOF)
12750 break;
12751 else
12752 cp_parser_toplevel_declaration (parser);
12756 /* Parse a declaration.
12758 declaration:
12759 block-declaration
12760 function-definition
12761 template-declaration
12762 explicit-instantiation
12763 explicit-specialization
12764 linkage-specification
12765 namespace-definition
12767 C++17:
12768 deduction-guide
12770 GNU extension:
12772 declaration:
12773 __extension__ declaration */
12775 static void
12776 cp_parser_declaration (cp_parser* parser)
12778 cp_token token1;
12779 cp_token token2;
12780 int saved_pedantic;
12781 void *p;
12782 tree attributes = NULL_TREE;
12784 /* Check for the `__extension__' keyword. */
12785 if (cp_parser_extension_opt (parser, &saved_pedantic))
12787 /* Parse the qualified declaration. */
12788 cp_parser_declaration (parser);
12789 /* Restore the PEDANTIC flag. */
12790 pedantic = saved_pedantic;
12792 return;
12795 /* Try to figure out what kind of declaration is present. */
12796 token1 = *cp_lexer_peek_token (parser->lexer);
12798 if (token1.type != CPP_EOF)
12799 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12800 else
12802 token2.type = CPP_EOF;
12803 token2.keyword = RID_MAX;
12806 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12807 p = obstack_alloc (&declarator_obstack, 0);
12809 /* If the next token is `extern' and the following token is a string
12810 literal, then we have a linkage specification. */
12811 if (token1.keyword == RID_EXTERN
12812 && cp_parser_is_pure_string_literal (&token2))
12813 cp_parser_linkage_specification (parser);
12814 /* If the next token is `template', then we have either a template
12815 declaration, an explicit instantiation, or an explicit
12816 specialization. */
12817 else if (token1.keyword == RID_TEMPLATE)
12819 /* `template <>' indicates a template specialization. */
12820 if (token2.type == CPP_LESS
12821 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12822 cp_parser_explicit_specialization (parser);
12823 /* `template <' indicates a template declaration. */
12824 else if (token2.type == CPP_LESS)
12825 cp_parser_template_declaration (parser, /*member_p=*/false);
12826 /* Anything else must be an explicit instantiation. */
12827 else
12828 cp_parser_explicit_instantiation (parser);
12830 /* If the next token is `export', then we have a template
12831 declaration. */
12832 else if (token1.keyword == RID_EXPORT)
12833 cp_parser_template_declaration (parser, /*member_p=*/false);
12834 /* If the next token is `extern', 'static' or 'inline' and the one
12835 after that is `template', we have a GNU extended explicit
12836 instantiation directive. */
12837 else if (cp_parser_allow_gnu_extensions_p (parser)
12838 && (token1.keyword == RID_EXTERN
12839 || token1.keyword == RID_STATIC
12840 || token1.keyword == RID_INLINE)
12841 && token2.keyword == RID_TEMPLATE)
12842 cp_parser_explicit_instantiation (parser);
12843 /* If the next token is `namespace', check for a named or unnamed
12844 namespace definition. */
12845 else if (token1.keyword == RID_NAMESPACE
12846 && (/* A named namespace definition. */
12847 (token2.type == CPP_NAME
12848 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12849 != CPP_EQ))
12850 || (token2.type == CPP_OPEN_SQUARE
12851 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12852 == CPP_OPEN_SQUARE)
12853 /* An unnamed namespace definition. */
12854 || token2.type == CPP_OPEN_BRACE
12855 || token2.keyword == RID_ATTRIBUTE))
12856 cp_parser_namespace_definition (parser);
12857 /* An inline (associated) namespace definition. */
12858 else if (token1.keyword == RID_INLINE
12859 && token2.keyword == RID_NAMESPACE)
12860 cp_parser_namespace_definition (parser);
12861 /* Objective-C++ declaration/definition. */
12862 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12863 cp_parser_objc_declaration (parser, NULL_TREE);
12864 else if (c_dialect_objc ()
12865 && token1.keyword == RID_ATTRIBUTE
12866 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12867 cp_parser_objc_declaration (parser, attributes);
12868 /* At this point we may have a template declared by a concept
12869 introduction. */
12870 else if (flag_concepts
12871 && cp_parser_template_declaration_after_export (parser,
12872 /*member_p=*/false))
12873 /* We did. */;
12874 else
12875 /* Try to parse a block-declaration, or a function-definition. */
12876 cp_parser_block_declaration (parser, /*statement_p=*/false);
12878 /* Free any declarators allocated. */
12879 obstack_free (&declarator_obstack, p);
12882 /* Parse a namespace-scope declaration. */
12884 static void
12885 cp_parser_toplevel_declaration (cp_parser* parser)
12887 cp_token *token = cp_lexer_peek_token (parser->lexer);
12889 if (token->type == CPP_PRAGMA)
12890 /* A top-level declaration can consist solely of a #pragma. A
12891 nested declaration cannot, so this is done here and not in
12892 cp_parser_declaration. (A #pragma at block scope is
12893 handled in cp_parser_statement.) */
12894 cp_parser_pragma (parser, pragma_external, NULL);
12895 else if (token->type == CPP_SEMICOLON)
12897 /* A declaration consisting of a single semicolon is
12898 invalid. Allow it unless we're being pedantic. */
12899 cp_lexer_consume_token (parser->lexer);
12900 if (!in_system_header_at (input_location))
12901 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12903 else
12904 /* Parse the declaration itself. */
12905 cp_parser_declaration (parser);
12908 /* Parse a block-declaration.
12910 block-declaration:
12911 simple-declaration
12912 asm-definition
12913 namespace-alias-definition
12914 using-declaration
12915 using-directive
12917 GNU Extension:
12919 block-declaration:
12920 __extension__ block-declaration
12922 C++0x Extension:
12924 block-declaration:
12925 static_assert-declaration
12927 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12928 part of a declaration-statement. */
12930 static void
12931 cp_parser_block_declaration (cp_parser *parser,
12932 bool statement_p)
12934 cp_token *token1;
12935 int saved_pedantic;
12937 /* Check for the `__extension__' keyword. */
12938 if (cp_parser_extension_opt (parser, &saved_pedantic))
12940 /* Parse the qualified declaration. */
12941 cp_parser_block_declaration (parser, statement_p);
12942 /* Restore the PEDANTIC flag. */
12943 pedantic = saved_pedantic;
12945 return;
12948 /* Peek at the next token to figure out which kind of declaration is
12949 present. */
12950 token1 = cp_lexer_peek_token (parser->lexer);
12952 /* If the next keyword is `asm', we have an asm-definition. */
12953 if (token1->keyword == RID_ASM)
12955 if (statement_p)
12956 cp_parser_commit_to_tentative_parse (parser);
12957 cp_parser_asm_definition (parser);
12959 /* If the next keyword is `namespace', we have a
12960 namespace-alias-definition. */
12961 else if (token1->keyword == RID_NAMESPACE)
12962 cp_parser_namespace_alias_definition (parser);
12963 /* If the next keyword is `using', we have a
12964 using-declaration, a using-directive, or an alias-declaration. */
12965 else if (token1->keyword == RID_USING)
12967 cp_token *token2;
12969 if (statement_p)
12970 cp_parser_commit_to_tentative_parse (parser);
12971 /* If the token after `using' is `namespace', then we have a
12972 using-directive. */
12973 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12974 if (token2->keyword == RID_NAMESPACE)
12975 cp_parser_using_directive (parser);
12976 /* If the second token after 'using' is '=', then we have an
12977 alias-declaration. */
12978 else if (cxx_dialect >= cxx11
12979 && token2->type == CPP_NAME
12980 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12981 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12982 cp_parser_alias_declaration (parser);
12983 /* Otherwise, it's a using-declaration. */
12984 else
12985 cp_parser_using_declaration (parser,
12986 /*access_declaration_p=*/false);
12988 /* If the next keyword is `__label__' we have a misplaced label
12989 declaration. */
12990 else if (token1->keyword == RID_LABEL)
12992 cp_lexer_consume_token (parser->lexer);
12993 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12994 cp_parser_skip_to_end_of_statement (parser);
12995 /* If the next token is now a `;', consume it. */
12996 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12997 cp_lexer_consume_token (parser->lexer);
12999 /* If the next token is `static_assert' we have a static assertion. */
13000 else if (token1->keyword == RID_STATIC_ASSERT)
13001 cp_parser_static_assert (parser, /*member_p=*/false);
13002 /* Anything else must be a simple-declaration. */
13003 else
13004 cp_parser_simple_declaration (parser, !statement_p,
13005 /*maybe_range_for_decl*/NULL);
13008 /* Parse a simple-declaration.
13010 simple-declaration:
13011 decl-specifier-seq [opt] init-declarator-list [opt] ;
13012 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13013 brace-or-equal-initializer ;
13015 init-declarator-list:
13016 init-declarator
13017 init-declarator-list , init-declarator
13019 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13020 function-definition as a simple-declaration.
13022 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13023 parsed declaration if it is an uninitialized single declarator not followed
13024 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13025 if present, will not be consumed. */
13027 static void
13028 cp_parser_simple_declaration (cp_parser* parser,
13029 bool function_definition_allowed_p,
13030 tree *maybe_range_for_decl)
13032 cp_decl_specifier_seq decl_specifiers;
13033 int declares_class_or_enum;
13034 bool saw_declarator;
13035 location_t comma_loc = UNKNOWN_LOCATION;
13036 location_t init_loc = UNKNOWN_LOCATION;
13038 if (maybe_range_for_decl)
13039 *maybe_range_for_decl = NULL_TREE;
13041 /* Defer access checks until we know what is being declared; the
13042 checks for names appearing in the decl-specifier-seq should be
13043 done as if we were in the scope of the thing being declared. */
13044 push_deferring_access_checks (dk_deferred);
13046 /* Parse the decl-specifier-seq. We have to keep track of whether
13047 or not the decl-specifier-seq declares a named class or
13048 enumeration type, since that is the only case in which the
13049 init-declarator-list is allowed to be empty.
13051 [dcl.dcl]
13053 In a simple-declaration, the optional init-declarator-list can be
13054 omitted only when declaring a class or enumeration, that is when
13055 the decl-specifier-seq contains either a class-specifier, an
13056 elaborated-type-specifier, or an enum-specifier. */
13057 cp_parser_decl_specifier_seq (parser,
13058 CP_PARSER_FLAGS_OPTIONAL,
13059 &decl_specifiers,
13060 &declares_class_or_enum);
13061 /* We no longer need to defer access checks. */
13062 stop_deferring_access_checks ();
13064 /* In a block scope, a valid declaration must always have a
13065 decl-specifier-seq. By not trying to parse declarators, we can
13066 resolve the declaration/expression ambiguity more quickly. */
13067 if (!function_definition_allowed_p
13068 && !decl_specifiers.any_specifiers_p)
13070 cp_parser_error (parser, "expected declaration");
13071 goto done;
13074 /* If the next two tokens are both identifiers, the code is
13075 erroneous. The usual cause of this situation is code like:
13077 T t;
13079 where "T" should name a type -- but does not. */
13080 if (!decl_specifiers.any_type_specifiers_p
13081 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13083 /* If parsing tentatively, we should commit; we really are
13084 looking at a declaration. */
13085 cp_parser_commit_to_tentative_parse (parser);
13086 /* Give up. */
13087 goto done;
13090 cp_parser_maybe_commit_to_declaration (parser,
13091 decl_specifiers.any_specifiers_p);
13093 /* Look for C++17 decomposition declaration. */
13094 for (size_t n = 1; ; n++)
13095 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13096 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13097 continue;
13098 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13099 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13100 && decl_specifiers.any_specifiers_p)
13102 tree decl
13103 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13104 maybe_range_for_decl,
13105 &init_loc);
13107 /* The next token should be either a `,' or a `;'. */
13108 cp_token *token = cp_lexer_peek_token (parser->lexer);
13109 /* If it's a `;', we are done. */
13110 if (token->type == CPP_SEMICOLON)
13111 goto finish;
13112 else if (maybe_range_for_decl)
13114 if (*maybe_range_for_decl == NULL_TREE)
13115 *maybe_range_for_decl = error_mark_node;
13116 goto finish;
13118 /* Anything else is an error. */
13119 else
13121 /* If we have already issued an error message we don't need
13122 to issue another one. */
13123 if ((decl != error_mark_node
13124 && DECL_INITIAL (decl) != error_mark_node)
13125 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13126 cp_parser_error (parser, "expected %<,%> or %<;%>");
13127 /* Skip tokens until we reach the end of the statement. */
13128 cp_parser_skip_to_end_of_statement (parser);
13129 /* If the next token is now a `;', consume it. */
13130 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13131 cp_lexer_consume_token (parser->lexer);
13132 goto done;
13135 else
13136 break;
13138 tree last_type;
13139 bool auto_specifier_p;
13140 /* NULL_TREE if both variable and function declaration are allowed,
13141 error_mark_node if function declaration are not allowed and
13142 a FUNCTION_DECL that should be diagnosed if it is followed by
13143 variable declarations. */
13144 tree auto_function_declaration;
13146 last_type = NULL_TREE;
13147 auto_specifier_p
13148 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13149 auto_function_declaration = NULL_TREE;
13151 /* Keep going until we hit the `;' at the end of the simple
13152 declaration. */
13153 saw_declarator = false;
13154 while (cp_lexer_next_token_is_not (parser->lexer,
13155 CPP_SEMICOLON))
13157 cp_token *token;
13158 bool function_definition_p;
13159 tree decl;
13160 tree auto_result = NULL_TREE;
13162 if (saw_declarator)
13164 /* If we are processing next declarator, comma is expected */
13165 token = cp_lexer_peek_token (parser->lexer);
13166 gcc_assert (token->type == CPP_COMMA);
13167 cp_lexer_consume_token (parser->lexer);
13168 if (maybe_range_for_decl)
13170 *maybe_range_for_decl = error_mark_node;
13171 if (comma_loc == UNKNOWN_LOCATION)
13172 comma_loc = token->location;
13175 else
13176 saw_declarator = true;
13178 /* Parse the init-declarator. */
13179 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13180 /*checks=*/NULL,
13181 function_definition_allowed_p,
13182 /*member_p=*/false,
13183 declares_class_or_enum,
13184 &function_definition_p,
13185 maybe_range_for_decl,
13186 &init_loc,
13187 &auto_result);
13188 /* If an error occurred while parsing tentatively, exit quickly.
13189 (That usually happens when in the body of a function; each
13190 statement is treated as a declaration-statement until proven
13191 otherwise.) */
13192 if (cp_parser_error_occurred (parser))
13193 goto done;
13195 if (auto_specifier_p && cxx_dialect >= cxx14)
13197 /* If the init-declarator-list contains more than one
13198 init-declarator, they shall all form declarations of
13199 variables. */
13200 if (auto_function_declaration == NULL_TREE)
13201 auto_function_declaration
13202 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13203 else if (TREE_CODE (decl) == FUNCTION_DECL
13204 || auto_function_declaration != error_mark_node)
13206 error_at (decl_specifiers.locations[ds_type_spec],
13207 "non-variable %qD in declaration with more than one "
13208 "declarator with placeholder type",
13209 TREE_CODE (decl) == FUNCTION_DECL
13210 ? decl : auto_function_declaration);
13211 auto_function_declaration = error_mark_node;
13215 if (auto_result
13216 && (!processing_template_decl || !type_uses_auto (auto_result)))
13218 if (last_type
13219 && last_type != error_mark_node
13220 && !same_type_p (auto_result, last_type))
13222 /* If the list of declarators contains more than one declarator,
13223 the type of each declared variable is determined as described
13224 above. If the type deduced for the template parameter U is not
13225 the same in each deduction, the program is ill-formed. */
13226 error_at (decl_specifiers.locations[ds_type_spec],
13227 "inconsistent deduction for %qT: %qT and then %qT",
13228 decl_specifiers.type, last_type, auto_result);
13229 last_type = error_mark_node;
13231 else
13232 last_type = auto_result;
13235 /* Handle function definitions specially. */
13236 if (function_definition_p)
13238 /* If the next token is a `,', then we are probably
13239 processing something like:
13241 void f() {}, *p;
13243 which is erroneous. */
13244 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13246 cp_token *token = cp_lexer_peek_token (parser->lexer);
13247 error_at (token->location,
13248 "mixing"
13249 " declarations and function-definitions is forbidden");
13251 /* Otherwise, we're done with the list of declarators. */
13252 else
13254 pop_deferring_access_checks ();
13255 return;
13258 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13259 *maybe_range_for_decl = decl;
13260 /* The next token should be either a `,' or a `;'. */
13261 token = cp_lexer_peek_token (parser->lexer);
13262 /* If it's a `,', there are more declarators to come. */
13263 if (token->type == CPP_COMMA)
13264 /* will be consumed next time around */;
13265 /* If it's a `;', we are done. */
13266 else if (token->type == CPP_SEMICOLON)
13267 break;
13268 else if (maybe_range_for_decl)
13270 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13271 permerror (decl_specifiers.locations[ds_type_spec],
13272 "types may not be defined in a for-range-declaration");
13273 break;
13275 /* Anything else is an error. */
13276 else
13278 /* If we have already issued an error message we don't need
13279 to issue another one. */
13280 if ((decl != error_mark_node
13281 && DECL_INITIAL (decl) != error_mark_node)
13282 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13283 cp_parser_error (parser, "expected %<,%> or %<;%>");
13284 /* Skip tokens until we reach the end of the statement. */
13285 cp_parser_skip_to_end_of_statement (parser);
13286 /* If the next token is now a `;', consume it. */
13287 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13288 cp_lexer_consume_token (parser->lexer);
13289 goto done;
13291 /* After the first time around, a function-definition is not
13292 allowed -- even if it was OK at first. For example:
13294 int i, f() {}
13296 is not valid. */
13297 function_definition_allowed_p = false;
13300 /* Issue an error message if no declarators are present, and the
13301 decl-specifier-seq does not itself declare a class or
13302 enumeration: [dcl.dcl]/3. */
13303 if (!saw_declarator)
13305 if (cp_parser_declares_only_class_p (parser))
13307 if (!declares_class_or_enum
13308 && decl_specifiers.type
13309 && OVERLOAD_TYPE_P (decl_specifiers.type))
13310 /* Ensure an error is issued anyway when finish_decltype_type,
13311 called via cp_parser_decl_specifier_seq, returns a class or
13312 an enumeration (c++/51786). */
13313 decl_specifiers.type = NULL_TREE;
13314 shadow_tag (&decl_specifiers);
13316 /* Perform any deferred access checks. */
13317 perform_deferred_access_checks (tf_warning_or_error);
13320 /* Consume the `;'. */
13321 finish:
13322 if (!maybe_range_for_decl)
13323 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13324 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13326 if (init_loc != UNKNOWN_LOCATION)
13327 error_at (init_loc, "initializer in range-based %<for%> loop");
13328 if (comma_loc != UNKNOWN_LOCATION)
13329 error_at (comma_loc,
13330 "multiple declarations in range-based %<for%> loop");
13333 done:
13334 pop_deferring_access_checks ();
13337 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13338 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13339 initializer ; */
13341 static tree
13342 cp_parser_decomposition_declaration (cp_parser *parser,
13343 cp_decl_specifier_seq *decl_specifiers,
13344 tree *maybe_range_for_decl,
13345 location_t *init_loc)
13347 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13348 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13349 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13351 /* Parse the identifier-list. */
13352 auto_vec<cp_expr, 10> v;
13353 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13354 while (true)
13356 cp_expr e = cp_parser_identifier (parser);
13357 if (e.get_value () == error_mark_node)
13358 break;
13359 v.safe_push (e);
13360 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13361 break;
13362 cp_lexer_consume_token (parser->lexer);
13365 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13366 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13368 end_loc = UNKNOWN_LOCATION;
13369 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13370 false);
13371 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13372 cp_lexer_consume_token (parser->lexer);
13373 else
13375 cp_parser_skip_to_end_of_statement (parser);
13376 return error_mark_node;
13380 if (cxx_dialect < cxx17)
13381 pedwarn (loc, 0, "structured bindings only available with "
13382 "-std=c++17 or -std=gnu++17");
13384 tree pushed_scope;
13385 cp_declarator *declarator = make_declarator (cdk_decomp);
13386 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13387 declarator->id_loc = loc;
13388 if (ref_qual != REF_QUAL_NONE)
13389 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13390 ref_qual == REF_QUAL_RVALUE,
13391 NULL_TREE);
13392 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13393 NULL_TREE, decl_specifiers->attributes,
13394 &pushed_scope);
13395 tree orig_decl = decl;
13397 unsigned int i;
13398 cp_expr e;
13399 cp_decl_specifier_seq decl_specs;
13400 clear_decl_specs (&decl_specs);
13401 decl_specs.type = make_auto ();
13402 tree prev = decl;
13403 FOR_EACH_VEC_ELT (v, i, e)
13405 if (i == 0)
13406 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13407 else
13408 declarator->u.id.unqualified_name = e.get_value ();
13409 declarator->id_loc = e.get_location ();
13410 tree elt_pushed_scope;
13411 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13412 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13413 if (decl2 == error_mark_node)
13414 decl = error_mark_node;
13415 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13417 /* Ensure we've diagnosed redeclaration if we aren't creating
13418 a new VAR_DECL. */
13419 gcc_assert (errorcount);
13420 decl = error_mark_node;
13422 else
13423 prev = decl2;
13424 if (elt_pushed_scope)
13425 pop_scope (elt_pushed_scope);
13428 if (v.is_empty ())
13430 error_at (loc, "empty structured binding declaration");
13431 decl = error_mark_node;
13434 if (maybe_range_for_decl == NULL
13435 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13437 bool non_constant_p = false, is_direct_init = false;
13438 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13439 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13440 &non_constant_p);
13441 if (initializer == NULL_TREE
13442 || (TREE_CODE (initializer) == TREE_LIST
13443 && TREE_CHAIN (initializer))
13444 || (is_direct_init
13445 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13446 && CONSTRUCTOR_NELTS (initializer) != 1))
13448 error_at (loc, "invalid initializer for structured binding "
13449 "declaration");
13450 initializer = error_mark_node;
13453 if (decl != error_mark_node)
13455 cp_maybe_mangle_decomp (decl, prev, v.length ());
13456 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13457 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13458 cp_finish_decomp (decl, prev, v.length ());
13461 else if (decl != error_mark_node)
13463 *maybe_range_for_decl = prev;
13464 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13465 the underlying DECL. */
13466 cp_finish_decomp (decl, prev, v.length ());
13469 if (pushed_scope)
13470 pop_scope (pushed_scope);
13472 if (decl == error_mark_node && DECL_P (orig_decl))
13474 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13475 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13478 return decl;
13481 /* Parse a decl-specifier-seq.
13483 decl-specifier-seq:
13484 decl-specifier-seq [opt] decl-specifier
13485 decl-specifier attribute-specifier-seq [opt] (C++11)
13487 decl-specifier:
13488 storage-class-specifier
13489 type-specifier
13490 function-specifier
13491 friend
13492 typedef
13494 GNU Extension:
13496 decl-specifier:
13497 attributes
13499 Concepts Extension:
13501 decl-specifier:
13502 concept
13504 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13506 The parser flags FLAGS is used to control type-specifier parsing.
13508 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13509 flags:
13511 1: one of the decl-specifiers is an elaborated-type-specifier
13512 (i.e., a type declaration)
13513 2: one of the decl-specifiers is an enum-specifier or a
13514 class-specifier (i.e., a type definition)
13518 static void
13519 cp_parser_decl_specifier_seq (cp_parser* parser,
13520 cp_parser_flags flags,
13521 cp_decl_specifier_seq *decl_specs,
13522 int* declares_class_or_enum)
13524 bool constructor_possible_p = !parser->in_declarator_p;
13525 bool found_decl_spec = false;
13526 cp_token *start_token = NULL;
13527 cp_decl_spec ds;
13529 /* Clear DECL_SPECS. */
13530 clear_decl_specs (decl_specs);
13532 /* Assume no class or enumeration type is declared. */
13533 *declares_class_or_enum = 0;
13535 /* Keep reading specifiers until there are no more to read. */
13536 while (true)
13538 bool constructor_p;
13539 cp_token *token;
13540 ds = ds_last;
13542 /* Peek at the next token. */
13543 token = cp_lexer_peek_token (parser->lexer);
13545 /* Save the first token of the decl spec list for error
13546 reporting. */
13547 if (!start_token)
13548 start_token = token;
13549 /* Handle attributes. */
13550 if (cp_next_tokens_can_be_attribute_p (parser))
13552 /* Parse the attributes. */
13553 tree attrs = cp_parser_attributes_opt (parser);
13555 /* In a sequence of declaration specifiers, c++11 attributes
13556 appertain to the type that precede them. In that case
13557 [dcl.spec]/1 says:
13559 The attribute-specifier-seq affects the type only for
13560 the declaration it appears in, not other declarations
13561 involving the same type.
13563 But for now let's force the user to position the
13564 attribute either at the beginning of the declaration or
13565 after the declarator-id, which would clearly mean that it
13566 applies to the declarator. */
13567 if (cxx11_attribute_p (attrs))
13569 if (!found_decl_spec)
13570 /* The c++11 attribute is at the beginning of the
13571 declaration. It appertains to the entity being
13572 declared. */;
13573 else
13575 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13577 /* This is an attribute following a
13578 class-specifier. */
13579 if (decl_specs->type_definition_p)
13580 warn_misplaced_attr_for_class_type (token->location,
13581 decl_specs->type);
13582 attrs = NULL_TREE;
13584 else
13586 decl_specs->std_attributes
13587 = attr_chainon (decl_specs->std_attributes, attrs);
13588 if (decl_specs->locations[ds_std_attribute] == 0)
13589 decl_specs->locations[ds_std_attribute] = token->location;
13591 continue;
13595 decl_specs->attributes
13596 = attr_chainon (decl_specs->attributes, attrs);
13597 if (decl_specs->locations[ds_attribute] == 0)
13598 decl_specs->locations[ds_attribute] = token->location;
13599 continue;
13601 /* Assume we will find a decl-specifier keyword. */
13602 found_decl_spec = true;
13603 /* If the next token is an appropriate keyword, we can simply
13604 add it to the list. */
13605 switch (token->keyword)
13607 /* decl-specifier:
13608 friend
13609 constexpr */
13610 case RID_FRIEND:
13611 if (!at_class_scope_p ())
13613 gcc_rich_location richloc (token->location);
13614 richloc.add_fixit_remove ();
13615 error_at (&richloc, "%<friend%> used outside of class");
13616 cp_lexer_purge_token (parser->lexer);
13618 else
13620 ds = ds_friend;
13621 /* Consume the token. */
13622 cp_lexer_consume_token (parser->lexer);
13624 break;
13626 case RID_CONSTEXPR:
13627 ds = ds_constexpr;
13628 cp_lexer_consume_token (parser->lexer);
13629 break;
13631 case RID_CONCEPT:
13632 ds = ds_concept;
13633 cp_lexer_consume_token (parser->lexer);
13634 break;
13636 /* function-specifier:
13637 inline
13638 virtual
13639 explicit */
13640 case RID_INLINE:
13641 case RID_VIRTUAL:
13642 case RID_EXPLICIT:
13643 cp_parser_function_specifier_opt (parser, decl_specs);
13644 break;
13646 /* decl-specifier:
13647 typedef */
13648 case RID_TYPEDEF:
13649 ds = ds_typedef;
13650 /* Consume the token. */
13651 cp_lexer_consume_token (parser->lexer);
13652 /* A constructor declarator cannot appear in a typedef. */
13653 constructor_possible_p = false;
13654 /* The "typedef" keyword can only occur in a declaration; we
13655 may as well commit at this point. */
13656 cp_parser_commit_to_tentative_parse (parser);
13658 if (decl_specs->storage_class != sc_none)
13659 decl_specs->conflicting_specifiers_p = true;
13660 break;
13662 /* storage-class-specifier:
13663 auto
13664 register
13665 static
13666 extern
13667 mutable
13669 GNU Extension:
13670 thread */
13671 case RID_AUTO:
13672 if (cxx_dialect == cxx98)
13674 /* Consume the token. */
13675 cp_lexer_consume_token (parser->lexer);
13677 /* Complain about `auto' as a storage specifier, if
13678 we're complaining about C++0x compatibility. */
13679 gcc_rich_location richloc (token->location);
13680 richloc.add_fixit_remove ();
13681 warning_at (&richloc, OPT_Wc__11_compat,
13682 "%<auto%> changes meaning in C++11; "
13683 "please remove it");
13685 /* Set the storage class anyway. */
13686 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13687 token);
13689 else
13690 /* C++0x auto type-specifier. */
13691 found_decl_spec = false;
13692 break;
13694 case RID_REGISTER:
13695 case RID_STATIC:
13696 case RID_EXTERN:
13697 case RID_MUTABLE:
13698 /* Consume the token. */
13699 cp_lexer_consume_token (parser->lexer);
13700 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13701 token);
13702 break;
13703 case RID_THREAD:
13704 /* Consume the token. */
13705 ds = ds_thread;
13706 cp_lexer_consume_token (parser->lexer);
13707 break;
13709 default:
13710 /* We did not yet find a decl-specifier yet. */
13711 found_decl_spec = false;
13712 break;
13715 if (found_decl_spec
13716 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13717 && token->keyword != RID_CONSTEXPR)
13718 error ("decl-specifier invalid in condition");
13720 if (found_decl_spec
13721 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13722 && token->keyword != RID_MUTABLE
13723 && token->keyword != RID_CONSTEXPR)
13724 error_at (token->location, "%qD invalid in lambda",
13725 ridpointers[token->keyword]);
13727 if (ds != ds_last)
13728 set_and_check_decl_spec_loc (decl_specs, ds, token);
13730 /* Constructors are a special case. The `S' in `S()' is not a
13731 decl-specifier; it is the beginning of the declarator. */
13732 constructor_p
13733 = (!found_decl_spec
13734 && constructor_possible_p
13735 && (cp_parser_constructor_declarator_p
13736 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13738 /* If we don't have a DECL_SPEC yet, then we must be looking at
13739 a type-specifier. */
13740 if (!found_decl_spec && !constructor_p)
13742 int decl_spec_declares_class_or_enum;
13743 bool is_cv_qualifier;
13744 tree type_spec;
13746 type_spec
13747 = cp_parser_type_specifier (parser, flags,
13748 decl_specs,
13749 /*is_declaration=*/true,
13750 &decl_spec_declares_class_or_enum,
13751 &is_cv_qualifier);
13752 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13754 /* If this type-specifier referenced a user-defined type
13755 (a typedef, class-name, etc.), then we can't allow any
13756 more such type-specifiers henceforth.
13758 [dcl.spec]
13760 The longest sequence of decl-specifiers that could
13761 possibly be a type name is taken as the
13762 decl-specifier-seq of a declaration. The sequence shall
13763 be self-consistent as described below.
13765 [dcl.type]
13767 As a general rule, at most one type-specifier is allowed
13768 in the complete decl-specifier-seq of a declaration. The
13769 only exceptions are the following:
13771 -- const or volatile can be combined with any other
13772 type-specifier.
13774 -- signed or unsigned can be combined with char, long,
13775 short, or int.
13777 -- ..
13779 Example:
13781 typedef char* Pc;
13782 void g (const int Pc);
13784 Here, Pc is *not* part of the decl-specifier seq; it's
13785 the declarator. Therefore, once we see a type-specifier
13786 (other than a cv-qualifier), we forbid any additional
13787 user-defined types. We *do* still allow things like `int
13788 int' to be considered a decl-specifier-seq, and issue the
13789 error message later. */
13790 if (type_spec && !is_cv_qualifier)
13791 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13792 /* A constructor declarator cannot follow a type-specifier. */
13793 if (type_spec)
13795 constructor_possible_p = false;
13796 found_decl_spec = true;
13797 if (!is_cv_qualifier)
13798 decl_specs->any_type_specifiers_p = true;
13800 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13801 error_at (token->location, "type-specifier invalid in lambda");
13805 /* If we still do not have a DECL_SPEC, then there are no more
13806 decl-specifiers. */
13807 if (!found_decl_spec)
13808 break;
13810 decl_specs->any_specifiers_p = true;
13811 /* After we see one decl-specifier, further decl-specifiers are
13812 always optional. */
13813 flags |= CP_PARSER_FLAGS_OPTIONAL;
13816 /* Don't allow a friend specifier with a class definition. */
13817 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13818 && (*declares_class_or_enum & 2))
13819 error_at (decl_specs->locations[ds_friend],
13820 "class definition may not be declared a friend");
13823 /* Parse an (optional) storage-class-specifier.
13825 storage-class-specifier:
13826 auto
13827 register
13828 static
13829 extern
13830 mutable
13832 GNU Extension:
13834 storage-class-specifier:
13835 thread
13837 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13839 static tree
13840 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13842 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13844 case RID_AUTO:
13845 if (cxx_dialect != cxx98)
13846 return NULL_TREE;
13847 /* Fall through for C++98. */
13848 gcc_fallthrough ();
13850 case RID_REGISTER:
13851 case RID_STATIC:
13852 case RID_EXTERN:
13853 case RID_MUTABLE:
13854 case RID_THREAD:
13855 /* Consume the token. */
13856 return cp_lexer_consume_token (parser->lexer)->u.value;
13858 default:
13859 return NULL_TREE;
13863 /* Parse an (optional) function-specifier.
13865 function-specifier:
13866 inline
13867 virtual
13868 explicit
13870 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13871 Updates DECL_SPECS, if it is non-NULL. */
13873 static tree
13874 cp_parser_function_specifier_opt (cp_parser* parser,
13875 cp_decl_specifier_seq *decl_specs)
13877 cp_token *token = cp_lexer_peek_token (parser->lexer);
13878 switch (token->keyword)
13880 case RID_INLINE:
13881 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13882 break;
13884 case RID_VIRTUAL:
13885 /* 14.5.2.3 [temp.mem]
13887 A member function template shall not be virtual. */
13888 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13889 && current_class_type)
13890 error_at (token->location, "templates may not be %<virtual%>");
13891 else
13892 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13893 break;
13895 case RID_EXPLICIT:
13896 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13897 break;
13899 default:
13900 return NULL_TREE;
13903 /* Consume the token. */
13904 return cp_lexer_consume_token (parser->lexer)->u.value;
13907 /* Parse a linkage-specification.
13909 linkage-specification:
13910 extern string-literal { declaration-seq [opt] }
13911 extern string-literal declaration */
13913 static void
13914 cp_parser_linkage_specification (cp_parser* parser)
13916 tree linkage;
13918 /* Look for the `extern' keyword. */
13919 cp_token *extern_token
13920 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13922 /* Look for the string-literal. */
13923 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13924 linkage = cp_parser_string_literal (parser, false, false);
13926 /* Transform the literal into an identifier. If the literal is a
13927 wide-character string, or contains embedded NULs, then we can't
13928 handle it as the user wants. */
13929 if (strlen (TREE_STRING_POINTER (linkage))
13930 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13932 cp_parser_error (parser, "invalid linkage-specification");
13933 /* Assume C++ linkage. */
13934 linkage = lang_name_cplusplus;
13936 else
13937 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13939 /* We're now using the new linkage. */
13940 push_lang_context (linkage);
13942 /* Preserve the location of the the innermost linkage specification,
13943 tracking the locations of nested specifications via a local. */
13944 location_t saved_location
13945 = parser->innermost_linkage_specification_location;
13946 /* Construct a location ranging from the start of the "extern" to
13947 the end of the string-literal, with the caret at the start, e.g.:
13948 extern "C" {
13949 ^~~~~~~~~~
13951 parser->innermost_linkage_specification_location
13952 = make_location (extern_token->location,
13953 extern_token->location,
13954 get_finish (string_token->location));
13956 /* If the next token is a `{', then we're using the first
13957 production. */
13958 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13960 cp_ensure_no_omp_declare_simd (parser);
13961 cp_ensure_no_oacc_routine (parser);
13963 /* Consume the `{' token. */
13964 matching_braces braces;
13965 braces.consume_open (parser)->location;
13966 /* Parse the declarations. */
13967 cp_parser_declaration_seq_opt (parser);
13968 /* Look for the closing `}'. */
13969 braces.require_close (parser);
13971 /* Otherwise, there's just one declaration. */
13972 else
13974 bool saved_in_unbraced_linkage_specification_p;
13976 saved_in_unbraced_linkage_specification_p
13977 = parser->in_unbraced_linkage_specification_p;
13978 parser->in_unbraced_linkage_specification_p = true;
13979 cp_parser_declaration (parser);
13980 parser->in_unbraced_linkage_specification_p
13981 = saved_in_unbraced_linkage_specification_p;
13984 /* We're done with the linkage-specification. */
13985 pop_lang_context ();
13987 /* Restore location of parent linkage specification, if any. */
13988 parser->innermost_linkage_specification_location = saved_location;
13991 /* Parse a static_assert-declaration.
13993 static_assert-declaration:
13994 static_assert ( constant-expression , string-literal ) ;
13995 static_assert ( constant-expression ) ; (C++17)
13997 If MEMBER_P, this static_assert is a class member. */
13999 static void
14000 cp_parser_static_assert(cp_parser *parser, bool member_p)
14002 cp_expr condition;
14003 location_t token_loc;
14004 tree message;
14005 bool dummy;
14007 /* Peek at the `static_assert' token so we can keep track of exactly
14008 where the static assertion started. */
14009 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14011 /* Look for the `static_assert' keyword. */
14012 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14013 RT_STATIC_ASSERT))
14014 return;
14016 /* We know we are in a static assertion; commit to any tentative
14017 parse. */
14018 if (cp_parser_parsing_tentatively (parser))
14019 cp_parser_commit_to_tentative_parse (parser);
14021 /* Parse the `(' starting the static assertion condition. */
14022 matching_parens parens;
14023 parens.require_open (parser);
14025 /* Parse the constant-expression. Allow a non-constant expression
14026 here in order to give better diagnostics in finish_static_assert. */
14027 condition =
14028 cp_parser_constant_expression (parser,
14029 /*allow_non_constant_p=*/true,
14030 /*non_constant_p=*/&dummy);
14032 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14034 if (cxx_dialect < cxx17)
14035 pedwarn (input_location, OPT_Wpedantic,
14036 "static_assert without a message "
14037 "only available with -std=c++17 or -std=gnu++17");
14038 /* Eat the ')' */
14039 cp_lexer_consume_token (parser->lexer);
14040 message = build_string (1, "");
14041 TREE_TYPE (message) = char_array_type_node;
14042 fix_string_type (message);
14044 else
14046 /* Parse the separating `,'. */
14047 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14049 /* Parse the string-literal message. */
14050 message = cp_parser_string_literal (parser,
14051 /*translate=*/false,
14052 /*wide_ok=*/true);
14054 /* A `)' completes the static assertion. */
14055 if (!parens.require_close (parser))
14056 cp_parser_skip_to_closing_parenthesis (parser,
14057 /*recovering=*/true,
14058 /*or_comma=*/false,
14059 /*consume_paren=*/true);
14062 /* A semicolon terminates the declaration. */
14063 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14065 /* Get the location for the static assertion. Use that of the
14066 condition if available, otherwise, use that of the "static_assert"
14067 token. */
14068 location_t assert_loc = condition.get_location ();
14069 if (assert_loc == UNKNOWN_LOCATION)
14070 assert_loc = token_loc;
14072 /* Complete the static assertion, which may mean either processing
14073 the static assert now or saving it for template instantiation. */
14074 finish_static_assert (condition, message, assert_loc, member_p);
14077 /* Parse the expression in decltype ( expression ). */
14079 static tree
14080 cp_parser_decltype_expr (cp_parser *parser,
14081 bool &id_expression_or_member_access_p)
14083 cp_token *id_expr_start_token;
14084 tree expr;
14086 /* Since we're going to preserve any side-effects from this parse, set up a
14087 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14088 in the expression. */
14089 tentative_firewall firewall (parser);
14091 /* First, try parsing an id-expression. */
14092 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14093 cp_parser_parse_tentatively (parser);
14094 expr = cp_parser_id_expression (parser,
14095 /*template_keyword_p=*/false,
14096 /*check_dependency_p=*/true,
14097 /*template_p=*/NULL,
14098 /*declarator_p=*/false,
14099 /*optional_p=*/false);
14101 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14103 bool non_integral_constant_expression_p = false;
14104 tree id_expression = expr;
14105 cp_id_kind idk;
14106 const char *error_msg;
14108 if (identifier_p (expr))
14109 /* Lookup the name we got back from the id-expression. */
14110 expr = cp_parser_lookup_name_simple (parser, expr,
14111 id_expr_start_token->location);
14113 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14114 /* A template without args is not a complete id-expression. */
14115 expr = error_mark_node;
14117 if (expr
14118 && expr != error_mark_node
14119 && TREE_CODE (expr) != TYPE_DECL
14120 && (TREE_CODE (expr) != BIT_NOT_EXPR
14121 || !TYPE_P (TREE_OPERAND (expr, 0)))
14122 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14124 /* Complete lookup of the id-expression. */
14125 expr = (finish_id_expression
14126 (id_expression, expr, parser->scope, &idk,
14127 /*integral_constant_expression_p=*/false,
14128 /*allow_non_integral_constant_expression_p=*/true,
14129 &non_integral_constant_expression_p,
14130 /*template_p=*/false,
14131 /*done=*/true,
14132 /*address_p=*/false,
14133 /*template_arg_p=*/false,
14134 &error_msg,
14135 id_expr_start_token->location));
14137 if (expr == error_mark_node)
14138 /* We found an id-expression, but it was something that we
14139 should not have found. This is an error, not something
14140 we can recover from, so note that we found an
14141 id-expression and we'll recover as gracefully as
14142 possible. */
14143 id_expression_or_member_access_p = true;
14146 if (expr
14147 && expr != error_mark_node
14148 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14149 /* We have an id-expression. */
14150 id_expression_or_member_access_p = true;
14153 if (!id_expression_or_member_access_p)
14155 /* Abort the id-expression parse. */
14156 cp_parser_abort_tentative_parse (parser);
14158 /* Parsing tentatively, again. */
14159 cp_parser_parse_tentatively (parser);
14161 /* Parse a class member access. */
14162 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14163 /*cast_p=*/false, /*decltype*/true,
14164 /*member_access_only_p=*/true, NULL);
14166 if (expr
14167 && expr != error_mark_node
14168 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14169 /* We have an id-expression. */
14170 id_expression_or_member_access_p = true;
14173 if (id_expression_or_member_access_p)
14174 /* We have parsed the complete id-expression or member access. */
14175 cp_parser_parse_definitely (parser);
14176 else
14178 /* Abort our attempt to parse an id-expression or member access
14179 expression. */
14180 cp_parser_abort_tentative_parse (parser);
14182 /* Commit to the tentative_firewall so we get syntax errors. */
14183 cp_parser_commit_to_tentative_parse (parser);
14185 /* Parse a full expression. */
14186 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14187 /*decltype_p=*/true);
14190 return expr;
14193 /* Parse a `decltype' type. Returns the type.
14195 simple-type-specifier:
14196 decltype ( expression )
14197 C++14 proposal:
14198 decltype ( auto ) */
14200 static tree
14201 cp_parser_decltype (cp_parser *parser)
14203 bool id_expression_or_member_access_p = false;
14204 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14206 if (start_token->type == CPP_DECLTYPE)
14208 /* Already parsed. */
14209 cp_lexer_consume_token (parser->lexer);
14210 return saved_checks_value (start_token->u.tree_check_value);
14213 /* Look for the `decltype' token. */
14214 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14215 return error_mark_node;
14217 /* Parse the opening `('. */
14218 matching_parens parens;
14219 if (!parens.require_open (parser))
14220 return error_mark_node;
14222 push_deferring_access_checks (dk_deferred);
14224 tree expr = NULL_TREE;
14226 if (cxx_dialect >= cxx14
14227 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14228 /* decltype (auto) */
14229 cp_lexer_consume_token (parser->lexer);
14230 else
14232 /* decltype (expression) */
14234 /* Types cannot be defined in a `decltype' expression. Save away the
14235 old message and set the new one. */
14236 const char *saved_message = parser->type_definition_forbidden_message;
14237 parser->type_definition_forbidden_message
14238 = G_("types may not be defined in %<decltype%> expressions");
14240 /* The restrictions on constant-expressions do not apply inside
14241 decltype expressions. */
14242 bool saved_integral_constant_expression_p
14243 = parser->integral_constant_expression_p;
14244 bool saved_non_integral_constant_expression_p
14245 = parser->non_integral_constant_expression_p;
14246 parser->integral_constant_expression_p = false;
14248 /* Within a parenthesized expression, a `>' token is always
14249 the greater-than operator. */
14250 bool saved_greater_than_is_operator_p
14251 = parser->greater_than_is_operator_p;
14252 parser->greater_than_is_operator_p = true;
14254 /* Do not actually evaluate the expression. */
14255 ++cp_unevaluated_operand;
14257 /* Do not warn about problems with the expression. */
14258 ++c_inhibit_evaluation_warnings;
14260 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14262 /* Go back to evaluating expressions. */
14263 --cp_unevaluated_operand;
14264 --c_inhibit_evaluation_warnings;
14266 /* The `>' token might be the end of a template-id or
14267 template-parameter-list now. */
14268 parser->greater_than_is_operator_p
14269 = saved_greater_than_is_operator_p;
14271 /* Restore the old message and the integral constant expression
14272 flags. */
14273 parser->type_definition_forbidden_message = saved_message;
14274 parser->integral_constant_expression_p
14275 = saved_integral_constant_expression_p;
14276 parser->non_integral_constant_expression_p
14277 = saved_non_integral_constant_expression_p;
14280 /* Parse to the closing `)'. */
14281 if (!parens.require_close (parser))
14283 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14284 /*consume_paren=*/true);
14285 pop_deferring_access_checks ();
14286 return error_mark_node;
14289 if (!expr)
14291 /* Build auto. */
14292 expr = make_decltype_auto ();
14293 AUTO_IS_DECLTYPE (expr) = true;
14295 else
14296 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14297 tf_warning_or_error);
14299 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14300 it again. */
14301 start_token->type = CPP_DECLTYPE;
14302 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14303 start_token->u.tree_check_value->value = expr;
14304 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14305 start_token->keyword = RID_MAX;
14306 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14308 pop_to_parent_deferring_access_checks ();
14310 return expr;
14313 /* Special member functions [gram.special] */
14315 /* Parse a conversion-function-id.
14317 conversion-function-id:
14318 operator conversion-type-id
14320 Returns an IDENTIFIER_NODE representing the operator. */
14322 static tree
14323 cp_parser_conversion_function_id (cp_parser* parser)
14325 tree type;
14326 tree saved_scope;
14327 tree saved_qualifying_scope;
14328 tree saved_object_scope;
14329 tree pushed_scope = NULL_TREE;
14331 /* Look for the `operator' token. */
14332 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14333 return error_mark_node;
14334 /* When we parse the conversion-type-id, the current scope will be
14335 reset. However, we need that information in able to look up the
14336 conversion function later, so we save it here. */
14337 saved_scope = parser->scope;
14338 saved_qualifying_scope = parser->qualifying_scope;
14339 saved_object_scope = parser->object_scope;
14340 /* We must enter the scope of the class so that the names of
14341 entities declared within the class are available in the
14342 conversion-type-id. For example, consider:
14344 struct S {
14345 typedef int I;
14346 operator I();
14349 S::operator I() { ... }
14351 In order to see that `I' is a type-name in the definition, we
14352 must be in the scope of `S'. */
14353 if (saved_scope)
14354 pushed_scope = push_scope (saved_scope);
14355 /* Parse the conversion-type-id. */
14356 type = cp_parser_conversion_type_id (parser);
14357 /* Leave the scope of the class, if any. */
14358 if (pushed_scope)
14359 pop_scope (pushed_scope);
14360 /* Restore the saved scope. */
14361 parser->scope = saved_scope;
14362 parser->qualifying_scope = saved_qualifying_scope;
14363 parser->object_scope = saved_object_scope;
14364 /* If the TYPE is invalid, indicate failure. */
14365 if (type == error_mark_node)
14366 return error_mark_node;
14367 return make_conv_op_name (type);
14370 /* Parse a conversion-type-id:
14372 conversion-type-id:
14373 type-specifier-seq conversion-declarator [opt]
14375 Returns the TYPE specified. */
14377 static tree
14378 cp_parser_conversion_type_id (cp_parser* parser)
14380 tree attributes;
14381 cp_decl_specifier_seq type_specifiers;
14382 cp_declarator *declarator;
14383 tree type_specified;
14384 const char *saved_message;
14386 /* Parse the attributes. */
14387 attributes = cp_parser_attributes_opt (parser);
14389 saved_message = parser->type_definition_forbidden_message;
14390 parser->type_definition_forbidden_message
14391 = G_("types may not be defined in a conversion-type-id");
14393 /* Parse the type-specifiers. */
14394 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14395 /*is_trailing_return=*/false,
14396 &type_specifiers);
14398 parser->type_definition_forbidden_message = saved_message;
14400 /* If that didn't work, stop. */
14401 if (type_specifiers.type == error_mark_node)
14402 return error_mark_node;
14403 /* Parse the conversion-declarator. */
14404 declarator = cp_parser_conversion_declarator_opt (parser);
14406 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14407 /*initialized=*/0, &attributes);
14408 if (attributes)
14409 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14411 /* Don't give this error when parsing tentatively. This happens to
14412 work because we always parse this definitively once. */
14413 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14414 && type_uses_auto (type_specified))
14416 if (cxx_dialect < cxx14)
14418 error ("invalid use of %<auto%> in conversion operator");
14419 return error_mark_node;
14421 else if (template_parm_scope_p ())
14422 warning (0, "use of %<auto%> in member template "
14423 "conversion operator can never be deduced");
14426 return type_specified;
14429 /* Parse an (optional) conversion-declarator.
14431 conversion-declarator:
14432 ptr-operator conversion-declarator [opt]
14436 static cp_declarator *
14437 cp_parser_conversion_declarator_opt (cp_parser* parser)
14439 enum tree_code code;
14440 tree class_type, std_attributes = NULL_TREE;
14441 cp_cv_quals cv_quals;
14443 /* We don't know if there's a ptr-operator next, or not. */
14444 cp_parser_parse_tentatively (parser);
14445 /* Try the ptr-operator. */
14446 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14447 &std_attributes);
14448 /* If it worked, look for more conversion-declarators. */
14449 if (cp_parser_parse_definitely (parser))
14451 cp_declarator *declarator;
14453 /* Parse another optional declarator. */
14454 declarator = cp_parser_conversion_declarator_opt (parser);
14456 declarator = cp_parser_make_indirect_declarator
14457 (code, class_type, cv_quals, declarator, std_attributes);
14459 return declarator;
14462 return NULL;
14465 /* Parse an (optional) ctor-initializer.
14467 ctor-initializer:
14468 : mem-initializer-list */
14470 static void
14471 cp_parser_ctor_initializer_opt (cp_parser* parser)
14473 /* If the next token is not a `:', then there is no
14474 ctor-initializer. */
14475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14477 /* Do default initialization of any bases and members. */
14478 if (DECL_CONSTRUCTOR_P (current_function_decl))
14479 finish_mem_initializers (NULL_TREE);
14480 return;
14483 /* Consume the `:' token. */
14484 cp_lexer_consume_token (parser->lexer);
14485 /* And the mem-initializer-list. */
14486 cp_parser_mem_initializer_list (parser);
14489 /* Parse a mem-initializer-list.
14491 mem-initializer-list:
14492 mem-initializer ... [opt]
14493 mem-initializer ... [opt] , mem-initializer-list */
14495 static void
14496 cp_parser_mem_initializer_list (cp_parser* parser)
14498 tree mem_initializer_list = NULL_TREE;
14499 tree target_ctor = error_mark_node;
14500 cp_token *token = cp_lexer_peek_token (parser->lexer);
14502 /* Let the semantic analysis code know that we are starting the
14503 mem-initializer-list. */
14504 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14505 error_at (token->location,
14506 "only constructors take member initializers");
14508 /* Loop through the list. */
14509 while (true)
14511 tree mem_initializer;
14513 token = cp_lexer_peek_token (parser->lexer);
14514 /* Parse the mem-initializer. */
14515 mem_initializer = cp_parser_mem_initializer (parser);
14516 /* If the next token is a `...', we're expanding member initializers. */
14517 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14518 if (ellipsis
14519 || (mem_initializer != error_mark_node
14520 && check_for_bare_parameter_packs (TREE_PURPOSE
14521 (mem_initializer))))
14523 /* Consume the `...'. */
14524 if (ellipsis)
14525 cp_lexer_consume_token (parser->lexer);
14527 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14528 can be expanded but members cannot. */
14529 if (mem_initializer != error_mark_node
14530 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14532 error_at (token->location,
14533 "cannot expand initializer for member %qD",
14534 TREE_PURPOSE (mem_initializer));
14535 mem_initializer = error_mark_node;
14538 /* Construct the pack expansion type. */
14539 if (mem_initializer != error_mark_node)
14540 mem_initializer = make_pack_expansion (mem_initializer);
14542 if (target_ctor != error_mark_node
14543 && mem_initializer != error_mark_node)
14545 error ("mem-initializer for %qD follows constructor delegation",
14546 TREE_PURPOSE (mem_initializer));
14547 mem_initializer = error_mark_node;
14549 /* Look for a target constructor. */
14550 if (mem_initializer != error_mark_node
14551 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14552 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14554 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14555 if (mem_initializer_list)
14557 error ("constructor delegation follows mem-initializer for %qD",
14558 TREE_PURPOSE (mem_initializer_list));
14559 mem_initializer = error_mark_node;
14561 target_ctor = mem_initializer;
14563 /* Add it to the list, unless it was erroneous. */
14564 if (mem_initializer != error_mark_node)
14566 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14567 mem_initializer_list = mem_initializer;
14569 /* If the next token is not a `,', we're done. */
14570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14571 break;
14572 /* Consume the `,' token. */
14573 cp_lexer_consume_token (parser->lexer);
14576 /* Perform semantic analysis. */
14577 if (DECL_CONSTRUCTOR_P (current_function_decl))
14578 finish_mem_initializers (mem_initializer_list);
14581 /* Parse a mem-initializer.
14583 mem-initializer:
14584 mem-initializer-id ( expression-list [opt] )
14585 mem-initializer-id braced-init-list
14587 GNU extension:
14589 mem-initializer:
14590 ( expression-list [opt] )
14592 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14593 class) or FIELD_DECL (for a non-static data member) to initialize;
14594 the TREE_VALUE is the expression-list. An empty initialization
14595 list is represented by void_list_node. */
14597 static tree
14598 cp_parser_mem_initializer (cp_parser* parser)
14600 tree mem_initializer_id;
14601 tree expression_list;
14602 tree member;
14603 cp_token *token = cp_lexer_peek_token (parser->lexer);
14605 /* Find out what is being initialized. */
14606 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14608 permerror (token->location,
14609 "anachronistic old-style base class initializer");
14610 mem_initializer_id = NULL_TREE;
14612 else
14614 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14615 if (mem_initializer_id == error_mark_node)
14616 return mem_initializer_id;
14618 member = expand_member_init (mem_initializer_id);
14619 if (member && !DECL_P (member))
14620 in_base_initializer = 1;
14622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14624 bool expr_non_constant_p;
14625 cp_lexer_set_source_position (parser->lexer);
14626 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14627 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14628 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14629 expression_list = build_tree_list (NULL_TREE, expression_list);
14631 else
14633 vec<tree, va_gc> *vec;
14634 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14635 /*cast_p=*/false,
14636 /*allow_expansion_p=*/true,
14637 /*non_constant_p=*/NULL);
14638 if (vec == NULL)
14639 return error_mark_node;
14640 expression_list = build_tree_list_vec (vec);
14641 release_tree_vector (vec);
14644 if (expression_list == error_mark_node)
14645 return error_mark_node;
14646 if (!expression_list)
14647 expression_list = void_type_node;
14649 in_base_initializer = 0;
14651 return member ? build_tree_list (member, expression_list) : error_mark_node;
14654 /* Parse a mem-initializer-id.
14656 mem-initializer-id:
14657 :: [opt] nested-name-specifier [opt] class-name
14658 decltype-specifier (C++11)
14659 identifier
14661 Returns a TYPE indicating the class to be initialized for the first
14662 production (and the second in C++11). Returns an IDENTIFIER_NODE
14663 indicating the data member to be initialized for the last production. */
14665 static tree
14666 cp_parser_mem_initializer_id (cp_parser* parser)
14668 bool global_scope_p;
14669 bool nested_name_specifier_p;
14670 bool template_p = false;
14671 tree id;
14673 cp_token *token = cp_lexer_peek_token (parser->lexer);
14675 /* `typename' is not allowed in this context ([temp.res]). */
14676 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14678 error_at (token->location,
14679 "keyword %<typename%> not allowed in this context (a qualified "
14680 "member initializer is implicitly a type)");
14681 cp_lexer_consume_token (parser->lexer);
14683 /* Look for the optional `::' operator. */
14684 global_scope_p
14685 = (cp_parser_global_scope_opt (parser,
14686 /*current_scope_valid_p=*/false)
14687 != NULL_TREE);
14688 /* Look for the optional nested-name-specifier. The simplest way to
14689 implement:
14691 [temp.res]
14693 The keyword `typename' is not permitted in a base-specifier or
14694 mem-initializer; in these contexts a qualified name that
14695 depends on a template-parameter is implicitly assumed to be a
14696 type name.
14698 is to assume that we have seen the `typename' keyword at this
14699 point. */
14700 nested_name_specifier_p
14701 = (cp_parser_nested_name_specifier_opt (parser,
14702 /*typename_keyword_p=*/true,
14703 /*check_dependency_p=*/true,
14704 /*type_p=*/true,
14705 /*is_declaration=*/true)
14706 != NULL_TREE);
14707 if (nested_name_specifier_p)
14708 template_p = cp_parser_optional_template_keyword (parser);
14709 /* If there is a `::' operator or a nested-name-specifier, then we
14710 are definitely looking for a class-name. */
14711 if (global_scope_p || nested_name_specifier_p)
14712 return cp_parser_class_name (parser,
14713 /*typename_keyword_p=*/true,
14714 /*template_keyword_p=*/template_p,
14715 typename_type,
14716 /*check_dependency_p=*/true,
14717 /*class_head_p=*/false,
14718 /*is_declaration=*/true);
14719 /* Otherwise, we could also be looking for an ordinary identifier. */
14720 cp_parser_parse_tentatively (parser);
14721 if (cp_lexer_next_token_is_decltype (parser->lexer))
14722 /* Try a decltype-specifier. */
14723 id = cp_parser_decltype (parser);
14724 else
14725 /* Otherwise, try a class-name. */
14726 id = cp_parser_class_name (parser,
14727 /*typename_keyword_p=*/true,
14728 /*template_keyword_p=*/false,
14729 none_type,
14730 /*check_dependency_p=*/true,
14731 /*class_head_p=*/false,
14732 /*is_declaration=*/true);
14733 /* If we found one, we're done. */
14734 if (cp_parser_parse_definitely (parser))
14735 return id;
14736 /* Otherwise, look for an ordinary identifier. */
14737 return cp_parser_identifier (parser);
14740 /* Overloading [gram.over] */
14742 /* Parse an operator-function-id.
14744 operator-function-id:
14745 operator operator
14747 Returns an IDENTIFIER_NODE for the operator which is a
14748 human-readable spelling of the identifier, e.g., `operator +'. */
14750 static cp_expr
14751 cp_parser_operator_function_id (cp_parser* parser)
14753 /* Look for the `operator' keyword. */
14754 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14755 return error_mark_node;
14756 /* And then the name of the operator itself. */
14757 return cp_parser_operator (parser);
14760 /* Return an identifier node for a user-defined literal operator.
14761 The suffix identifier is chained to the operator name identifier. */
14763 tree
14764 cp_literal_operator_id (const char* name)
14766 tree identifier;
14767 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14768 + strlen (name) + 10);
14769 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14770 identifier = get_identifier (buffer);
14772 return identifier;
14775 /* Parse an operator.
14777 operator:
14778 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14779 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14780 || ++ -- , ->* -> () []
14782 GNU Extensions:
14784 operator:
14785 <? >? <?= >?=
14787 Returns an IDENTIFIER_NODE for the operator which is a
14788 human-readable spelling of the identifier, e.g., `operator +'. */
14790 static cp_expr
14791 cp_parser_operator (cp_parser* parser)
14793 tree id = NULL_TREE;
14794 cp_token *token;
14795 bool utf8 = false;
14797 /* Peek at the next token. */
14798 token = cp_lexer_peek_token (parser->lexer);
14800 location_t start_loc = token->location;
14802 /* Figure out which operator we have. */
14803 enum tree_code op = ERROR_MARK;
14804 bool assop = false;
14805 bool consumed = false;
14806 switch (token->type)
14808 case CPP_KEYWORD:
14810 /* The keyword should be either `new' or `delete'. */
14811 if (token->keyword == RID_NEW)
14812 op = NEW_EXPR;
14813 else if (token->keyword == RID_DELETE)
14814 op = DELETE_EXPR;
14815 else
14816 break;
14818 /* Consume the `new' or `delete' token. */
14819 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14821 /* Peek at the next token. */
14822 token = cp_lexer_peek_token (parser->lexer);
14823 /* If it's a `[' token then this is the array variant of the
14824 operator. */
14825 if (token->type == CPP_OPEN_SQUARE)
14827 /* Consume the `[' token. */
14828 cp_lexer_consume_token (parser->lexer);
14829 /* Look for the `]' token. */
14830 if (cp_token *close_token
14831 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14832 end_loc = close_token->location;
14833 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14835 start_loc = make_location (start_loc, start_loc, end_loc);
14836 consumed = true;
14837 break;
14840 case CPP_PLUS:
14841 op = PLUS_EXPR;
14842 break;
14844 case CPP_MINUS:
14845 op = MINUS_EXPR;
14846 break;
14848 case CPP_MULT:
14849 op = MULT_EXPR;
14850 break;
14852 case CPP_DIV:
14853 op = TRUNC_DIV_EXPR;
14854 break;
14856 case CPP_MOD:
14857 op = TRUNC_MOD_EXPR;
14858 break;
14860 case CPP_XOR:
14861 op = BIT_XOR_EXPR;
14862 break;
14864 case CPP_AND:
14865 op = BIT_AND_EXPR;
14866 break;
14868 case CPP_OR:
14869 op = BIT_IOR_EXPR;
14870 break;
14872 case CPP_COMPL:
14873 op = BIT_NOT_EXPR;
14874 break;
14876 case CPP_NOT:
14877 op = TRUTH_NOT_EXPR;
14878 break;
14880 case CPP_EQ:
14881 assop = true;
14882 op = NOP_EXPR;
14883 break;
14885 case CPP_LESS:
14886 op = LT_EXPR;
14887 break;
14889 case CPP_GREATER:
14890 op = GT_EXPR;
14891 break;
14893 case CPP_PLUS_EQ:
14894 assop = true;
14895 op = PLUS_EXPR;
14896 break;
14898 case CPP_MINUS_EQ:
14899 assop = true;
14900 op = MINUS_EXPR;
14901 break;
14903 case CPP_MULT_EQ:
14904 assop = true;
14905 op = MULT_EXPR;
14906 break;
14908 case CPP_DIV_EQ:
14909 assop = true;
14910 op = TRUNC_DIV_EXPR;
14911 break;
14913 case CPP_MOD_EQ:
14914 assop = true;
14915 op = TRUNC_MOD_EXPR;
14916 break;
14918 case CPP_XOR_EQ:
14919 assop = true;
14920 op = BIT_XOR_EXPR;
14921 break;
14923 case CPP_AND_EQ:
14924 assop = true;
14925 op = BIT_AND_EXPR;
14926 break;
14928 case CPP_OR_EQ:
14929 assop = true;
14930 op = BIT_IOR_EXPR;
14931 break;
14933 case CPP_LSHIFT:
14934 op = LSHIFT_EXPR;
14935 break;
14937 case CPP_RSHIFT:
14938 op = RSHIFT_EXPR;
14939 break;
14941 case CPP_LSHIFT_EQ:
14942 assop = true;
14943 op = LSHIFT_EXPR;
14944 break;
14946 case CPP_RSHIFT_EQ:
14947 assop = true;
14948 op = RSHIFT_EXPR;
14949 break;
14951 case CPP_EQ_EQ:
14952 op = EQ_EXPR;
14953 break;
14955 case CPP_NOT_EQ:
14956 op = NE_EXPR;
14957 break;
14959 case CPP_LESS_EQ:
14960 op = LE_EXPR;
14961 break;
14963 case CPP_GREATER_EQ:
14964 op = GE_EXPR;
14965 break;
14967 case CPP_AND_AND:
14968 op = TRUTH_ANDIF_EXPR;
14969 break;
14971 case CPP_OR_OR:
14972 op = TRUTH_ORIF_EXPR;
14973 break;
14975 case CPP_PLUS_PLUS:
14976 op = POSTINCREMENT_EXPR;
14977 break;
14979 case CPP_MINUS_MINUS:
14980 op = PREDECREMENT_EXPR;
14981 break;
14983 case CPP_COMMA:
14984 op = COMPOUND_EXPR;
14985 break;
14987 case CPP_DEREF_STAR:
14988 op = MEMBER_REF;
14989 break;
14991 case CPP_DEREF:
14992 op = COMPONENT_REF;
14993 break;
14995 case CPP_OPEN_PAREN:
14997 /* Consume the `('. */
14998 matching_parens parens;
14999 parens.consume_open (parser);
15000 /* Look for the matching `)'. */
15001 parens.require_close (parser);
15002 op = CALL_EXPR;
15003 consumed = true;
15004 break;
15007 case CPP_OPEN_SQUARE:
15008 /* Consume the `['. */
15009 cp_lexer_consume_token (parser->lexer);
15010 /* Look for the matching `]'. */
15011 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15012 op = ARRAY_REF;
15013 consumed = true;
15014 break;
15016 case CPP_UTF8STRING:
15017 case CPP_UTF8STRING_USERDEF:
15018 utf8 = true;
15019 /* FALLTHRU */
15020 case CPP_STRING:
15021 case CPP_WSTRING:
15022 case CPP_STRING16:
15023 case CPP_STRING32:
15024 case CPP_STRING_USERDEF:
15025 case CPP_WSTRING_USERDEF:
15026 case CPP_STRING16_USERDEF:
15027 case CPP_STRING32_USERDEF:
15029 tree str, string_tree;
15030 int sz, len;
15032 if (cxx_dialect == cxx98)
15033 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15035 /* Consume the string. */
15036 str = cp_parser_string_literal (parser, /*translate=*/true,
15037 /*wide_ok=*/true, /*lookup_udlit=*/false);
15038 if (str == error_mark_node)
15039 return error_mark_node;
15040 else if (TREE_CODE (str) == USERDEF_LITERAL)
15042 string_tree = USERDEF_LITERAL_VALUE (str);
15043 id = USERDEF_LITERAL_SUFFIX_ID (str);
15045 else
15047 string_tree = str;
15048 /* Look for the suffix identifier. */
15049 token = cp_lexer_peek_token (parser->lexer);
15050 if (token->type == CPP_NAME)
15051 id = cp_parser_identifier (parser);
15052 else if (token->type == CPP_KEYWORD)
15054 error ("unexpected keyword;"
15055 " remove space between quotes and suffix identifier");
15056 return error_mark_node;
15058 else
15060 error ("expected suffix identifier");
15061 return error_mark_node;
15064 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15065 (TREE_TYPE (TREE_TYPE (string_tree))));
15066 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15067 if (len != 0)
15069 error ("expected empty string after %<operator%> keyword");
15070 return error_mark_node;
15072 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15073 != char_type_node)
15075 error ("invalid encoding prefix in literal operator");
15076 return error_mark_node;
15078 if (id != error_mark_node)
15080 const char *name = IDENTIFIER_POINTER (id);
15081 id = cp_literal_operator_id (name);
15083 return id;
15086 default:
15087 /* Anything else is an error. */
15088 break;
15091 /* If we have selected an identifier, we need to consume the
15092 operator token. */
15093 if (op != ERROR_MARK)
15095 id = ovl_op_identifier (assop, op);
15096 if (!consumed)
15097 cp_lexer_consume_token (parser->lexer);
15099 /* Otherwise, no valid operator name was present. */
15100 else
15102 cp_parser_error (parser, "expected operator");
15103 id = error_mark_node;
15106 return cp_expr (id, start_loc);
15109 /* Parse a template-declaration.
15111 template-declaration:
15112 export [opt] template < template-parameter-list > declaration
15114 If MEMBER_P is TRUE, this template-declaration occurs within a
15115 class-specifier.
15117 The grammar rule given by the standard isn't correct. What
15118 is really meant is:
15120 template-declaration:
15121 export [opt] template-parameter-list-seq
15122 decl-specifier-seq [opt] init-declarator [opt] ;
15123 export [opt] template-parameter-list-seq
15124 function-definition
15126 template-parameter-list-seq:
15127 template-parameter-list-seq [opt]
15128 template < template-parameter-list >
15130 Concept Extensions:
15132 template-parameter-list-seq:
15133 template < template-parameter-list > requires-clause [opt]
15135 requires-clause:
15136 requires logical-or-expression */
15138 static void
15139 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15141 /* Check for `export'. */
15142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15144 /* Consume the `export' token. */
15145 cp_lexer_consume_token (parser->lexer);
15146 /* Warn that we do not support `export'. */
15147 warning (0, "keyword %<export%> not implemented, and will be ignored");
15150 cp_parser_template_declaration_after_export (parser, member_p);
15153 /* Parse a template-parameter-list.
15155 template-parameter-list:
15156 template-parameter
15157 template-parameter-list , template-parameter
15159 Returns a TREE_LIST. Each node represents a template parameter.
15160 The nodes are connected via their TREE_CHAINs. */
15162 static tree
15163 cp_parser_template_parameter_list (cp_parser* parser)
15165 tree parameter_list = NULL_TREE;
15167 begin_template_parm_list ();
15169 /* The loop below parses the template parms. We first need to know
15170 the total number of template parms to be able to compute proper
15171 canonical types of each dependent type. So after the loop, when
15172 we know the total number of template parms,
15173 end_template_parm_list computes the proper canonical types and
15174 fixes up the dependent types accordingly. */
15175 while (true)
15177 tree parameter;
15178 bool is_non_type;
15179 bool is_parameter_pack;
15180 location_t parm_loc;
15182 /* Parse the template-parameter. */
15183 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15184 parameter = cp_parser_template_parameter (parser,
15185 &is_non_type,
15186 &is_parameter_pack);
15187 /* Add it to the list. */
15188 if (parameter != error_mark_node)
15189 parameter_list = process_template_parm (parameter_list,
15190 parm_loc,
15191 parameter,
15192 is_non_type,
15193 is_parameter_pack);
15194 else
15196 tree err_parm = build_tree_list (parameter, parameter);
15197 parameter_list = chainon (parameter_list, err_parm);
15200 /* If the next token is not a `,', we're done. */
15201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15202 break;
15203 /* Otherwise, consume the `,' token. */
15204 cp_lexer_consume_token (parser->lexer);
15207 return end_template_parm_list (parameter_list);
15210 /* Parse a introduction-list.
15212 introduction-list:
15213 introduced-parameter
15214 introduction-list , introduced-parameter
15216 introduced-parameter:
15217 ...[opt] identifier
15219 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15220 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15221 WILDCARD_DECL will also have DECL_NAME set and token location in
15222 DECL_SOURCE_LOCATION. */
15224 static tree
15225 cp_parser_introduction_list (cp_parser *parser)
15227 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15229 while (true)
15231 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15232 if (is_pack)
15233 cp_lexer_consume_token (parser->lexer);
15235 tree identifier = cp_parser_identifier (parser);
15236 if (identifier == error_mark_node)
15237 break;
15239 /* Build placeholder. */
15240 tree parm = build_nt (WILDCARD_DECL);
15241 DECL_SOURCE_LOCATION (parm)
15242 = cp_lexer_peek_token (parser->lexer)->location;
15243 DECL_NAME (parm) = identifier;
15244 WILDCARD_PACK_P (parm) = is_pack;
15245 vec_safe_push (introduction_vec, parm);
15247 /* If the next token is not a `,', we're done. */
15248 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15249 break;
15250 /* Otherwise, consume the `,' token. */
15251 cp_lexer_consume_token (parser->lexer);
15254 /* Convert the vec into a TREE_VEC. */
15255 tree introduction_list = make_tree_vec (introduction_vec->length ());
15256 unsigned int n;
15257 tree parm;
15258 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15259 TREE_VEC_ELT (introduction_list, n) = parm;
15261 release_tree_vector (introduction_vec);
15262 return introduction_list;
15265 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15266 is an abstract declarator. */
15268 static inline cp_declarator*
15269 get_id_declarator (cp_declarator *declarator)
15271 cp_declarator *d = declarator;
15272 while (d && d->kind != cdk_id)
15273 d = d->declarator;
15274 return d;
15277 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15278 is an abstract declarator. */
15280 static inline tree
15281 get_unqualified_id (cp_declarator *declarator)
15283 declarator = get_id_declarator (declarator);
15284 if (declarator)
15285 return declarator->u.id.unqualified_name;
15286 else
15287 return NULL_TREE;
15290 /* Returns true if DECL represents a constrained-parameter. */
15292 static inline bool
15293 is_constrained_parameter (tree decl)
15295 return (decl
15296 && TREE_CODE (decl) == TYPE_DECL
15297 && CONSTRAINED_PARM_CONCEPT (decl)
15298 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15301 /* Returns true if PARM declares a constrained-parameter. */
15303 static inline bool
15304 is_constrained_parameter (cp_parameter_declarator *parm)
15306 return is_constrained_parameter (parm->decl_specifiers.type);
15309 /* Check that the type parameter is only a declarator-id, and that its
15310 type is not cv-qualified. */
15312 bool
15313 cp_parser_check_constrained_type_parm (cp_parser *parser,
15314 cp_parameter_declarator *parm)
15316 if (!parm->declarator)
15317 return true;
15319 if (parm->declarator->kind != cdk_id)
15321 cp_parser_error (parser, "invalid constrained type parameter");
15322 return false;
15325 /* Don't allow cv-qualified type parameters. */
15326 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15327 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15329 cp_parser_error (parser, "cv-qualified type parameter");
15330 return false;
15333 return true;
15336 /* Finish parsing/processing a template type parameter and checking
15337 various restrictions. */
15339 static inline tree
15340 cp_parser_constrained_type_template_parm (cp_parser *parser,
15341 tree id,
15342 cp_parameter_declarator* parmdecl)
15344 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15345 return finish_template_type_parm (class_type_node, id);
15346 else
15347 return error_mark_node;
15350 static tree
15351 finish_constrained_template_template_parm (tree proto, tree id)
15353 /* FIXME: This should probably be copied, and we may need to adjust
15354 the template parameter depths. */
15355 tree saved_parms = current_template_parms;
15356 begin_template_parm_list ();
15357 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15358 end_template_parm_list ();
15360 tree parm = finish_template_template_parm (class_type_node, id);
15361 current_template_parms = saved_parms;
15363 return parm;
15366 /* Finish parsing/processing a template template parameter by borrowing
15367 the template parameter list from the prototype parameter. */
15369 static tree
15370 cp_parser_constrained_template_template_parm (cp_parser *parser,
15371 tree proto,
15372 tree id,
15373 cp_parameter_declarator *parmdecl)
15375 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15376 return error_mark_node;
15377 return finish_constrained_template_template_parm (proto, id);
15380 /* Create a new non-type template parameter from the given PARM
15381 declarator. */
15383 static tree
15384 constrained_non_type_template_parm (bool *is_non_type,
15385 cp_parameter_declarator *parm)
15387 *is_non_type = true;
15388 cp_declarator *decl = parm->declarator;
15389 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15390 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15391 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15394 /* Build a constrained template parameter based on the PARMDECL
15395 declarator. The type of PARMDECL is the constrained type, which
15396 refers to the prototype template parameter that ultimately
15397 specifies the type of the declared parameter. */
15399 static tree
15400 finish_constrained_parameter (cp_parser *parser,
15401 cp_parameter_declarator *parmdecl,
15402 bool *is_non_type,
15403 bool *is_parameter_pack)
15405 tree decl = parmdecl->decl_specifiers.type;
15406 tree id = get_unqualified_id (parmdecl->declarator);
15407 tree def = parmdecl->default_argument;
15408 tree proto = DECL_INITIAL (decl);
15410 /* A template parameter constrained by a variadic concept shall also
15411 be declared as a template parameter pack. */
15412 bool is_variadic = template_parameter_pack_p (proto);
15413 if (is_variadic && !*is_parameter_pack)
15414 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15416 /* Build the parameter. Return an error if the declarator was invalid. */
15417 tree parm;
15418 if (TREE_CODE (proto) == TYPE_DECL)
15419 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15420 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15421 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15422 parmdecl);
15423 else
15424 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15425 if (parm == error_mark_node)
15426 return error_mark_node;
15428 /* Finish the parameter decl and create a node attaching the
15429 default argument and constraint. */
15430 parm = build_tree_list (def, parm);
15431 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15433 return parm;
15436 /* Returns true if the parsed type actually represents the declaration
15437 of a type template-parameter. */
15439 static inline bool
15440 declares_constrained_type_template_parameter (tree type)
15442 return (is_constrained_parameter (type)
15443 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15447 /* Returns true if the parsed type actually represents the declaration of
15448 a template template-parameter. */
15450 static bool
15451 declares_constrained_template_template_parameter (tree type)
15453 return (is_constrained_parameter (type)
15454 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15457 /* Parse a default argument for a type template-parameter.
15458 Note that diagnostics are handled in cp_parser_template_parameter. */
15460 static tree
15461 cp_parser_default_type_template_argument (cp_parser *parser)
15463 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15465 /* Consume the `=' token. */
15466 cp_lexer_consume_token (parser->lexer);
15468 cp_token *token = cp_lexer_peek_token (parser->lexer);
15470 /* Parse the default-argument. */
15471 push_deferring_access_checks (dk_no_deferred);
15472 tree default_argument = cp_parser_type_id (parser);
15473 pop_deferring_access_checks ();
15475 if (flag_concepts && type_uses_auto (default_argument))
15477 error_at (token->location,
15478 "invalid use of %<auto%> in default template argument");
15479 return error_mark_node;
15482 return default_argument;
15485 /* Parse a default argument for a template template-parameter. */
15487 static tree
15488 cp_parser_default_template_template_argument (cp_parser *parser)
15490 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15492 bool is_template;
15494 /* Consume the `='. */
15495 cp_lexer_consume_token (parser->lexer);
15496 /* Parse the id-expression. */
15497 push_deferring_access_checks (dk_no_deferred);
15498 /* save token before parsing the id-expression, for error
15499 reporting */
15500 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15501 tree default_argument
15502 = cp_parser_id_expression (parser,
15503 /*template_keyword_p=*/false,
15504 /*check_dependency_p=*/true,
15505 /*template_p=*/&is_template,
15506 /*declarator_p=*/false,
15507 /*optional_p=*/false);
15508 if (TREE_CODE (default_argument) == TYPE_DECL)
15509 /* If the id-expression was a template-id that refers to
15510 a template-class, we already have the declaration here,
15511 so no further lookup is needed. */
15513 else
15514 /* Look up the name. */
15515 default_argument
15516 = cp_parser_lookup_name (parser, default_argument,
15517 none_type,
15518 /*is_template=*/is_template,
15519 /*is_namespace=*/false,
15520 /*check_dependency=*/true,
15521 /*ambiguous_decls=*/NULL,
15522 token->location);
15523 /* See if the default argument is valid. */
15524 default_argument = check_template_template_default_arg (default_argument);
15525 pop_deferring_access_checks ();
15526 return default_argument;
15529 /* Parse a template-parameter.
15531 template-parameter:
15532 type-parameter
15533 parameter-declaration
15535 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15536 the parameter. The TREE_PURPOSE is the default value, if any.
15537 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15538 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15539 set to true iff this parameter is a parameter pack. */
15541 static tree
15542 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15543 bool *is_parameter_pack)
15545 cp_token *token;
15546 cp_parameter_declarator *parameter_declarator;
15547 tree parm;
15549 /* Assume it is a type parameter or a template parameter. */
15550 *is_non_type = false;
15551 /* Assume it not a parameter pack. */
15552 *is_parameter_pack = false;
15553 /* Peek at the next token. */
15554 token = cp_lexer_peek_token (parser->lexer);
15555 /* If it is `template', we have a type-parameter. */
15556 if (token->keyword == RID_TEMPLATE)
15557 return cp_parser_type_parameter (parser, is_parameter_pack);
15558 /* If it is `class' or `typename' we do not know yet whether it is a
15559 type parameter or a non-type parameter. Consider:
15561 template <typename T, typename T::X X> ...
15565 template <class C, class D*> ...
15567 Here, the first parameter is a type parameter, and the second is
15568 a non-type parameter. We can tell by looking at the token after
15569 the identifier -- if it is a `,', `=', or `>' then we have a type
15570 parameter. */
15571 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15573 /* Peek at the token after `class' or `typename'. */
15574 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15575 /* If it's an ellipsis, we have a template type parameter
15576 pack. */
15577 if (token->type == CPP_ELLIPSIS)
15578 return cp_parser_type_parameter (parser, is_parameter_pack);
15579 /* If it's an identifier, skip it. */
15580 if (token->type == CPP_NAME)
15581 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15582 /* Now, see if the token looks like the end of a template
15583 parameter. */
15584 if (token->type == CPP_COMMA
15585 || token->type == CPP_EQ
15586 || token->type == CPP_GREATER)
15587 return cp_parser_type_parameter (parser, is_parameter_pack);
15590 /* Otherwise, it is a non-type parameter or a constrained parameter.
15592 [temp.param]
15594 When parsing a default template-argument for a non-type
15595 template-parameter, the first non-nested `>' is taken as the end
15596 of the template parameter-list rather than a greater-than
15597 operator. */
15598 parameter_declarator
15599 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15600 /*parenthesized_p=*/NULL);
15602 if (!parameter_declarator)
15603 return error_mark_node;
15605 /* If the parameter declaration is marked as a parameter pack, set
15606 *IS_PARAMETER_PACK to notify the caller. */
15607 if (parameter_declarator->template_parameter_pack_p)
15608 *is_parameter_pack = true;
15610 if (parameter_declarator->default_argument)
15612 /* Can happen in some cases of erroneous input (c++/34892). */
15613 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15614 /* Consume the `...' for better error recovery. */
15615 cp_lexer_consume_token (parser->lexer);
15618 // The parameter may have been constrained.
15619 if (is_constrained_parameter (parameter_declarator))
15620 return finish_constrained_parameter (parser,
15621 parameter_declarator,
15622 is_non_type,
15623 is_parameter_pack);
15625 // Now we're sure that the parameter is a non-type parameter.
15626 *is_non_type = true;
15628 parm = grokdeclarator (parameter_declarator->declarator,
15629 &parameter_declarator->decl_specifiers,
15630 TPARM, /*initialized=*/0,
15631 /*attrlist=*/NULL);
15632 if (parm == error_mark_node)
15633 return error_mark_node;
15635 return build_tree_list (parameter_declarator->default_argument, parm);
15638 /* Parse a type-parameter.
15640 type-parameter:
15641 class identifier [opt]
15642 class identifier [opt] = type-id
15643 typename identifier [opt]
15644 typename identifier [opt] = type-id
15645 template < template-parameter-list > class identifier [opt]
15646 template < template-parameter-list > class identifier [opt]
15647 = id-expression
15649 GNU Extension (variadic templates):
15651 type-parameter:
15652 class ... identifier [opt]
15653 typename ... identifier [opt]
15655 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15656 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15657 the declaration of the parameter.
15659 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15661 static tree
15662 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15664 cp_token *token;
15665 tree parameter;
15667 /* Look for a keyword to tell us what kind of parameter this is. */
15668 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15669 if (!token)
15670 return error_mark_node;
15672 switch (token->keyword)
15674 case RID_CLASS:
15675 case RID_TYPENAME:
15677 tree identifier;
15678 tree default_argument;
15680 /* If the next token is an ellipsis, we have a template
15681 argument pack. */
15682 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15684 /* Consume the `...' token. */
15685 cp_lexer_consume_token (parser->lexer);
15686 maybe_warn_variadic_templates ();
15688 *is_parameter_pack = true;
15691 /* If the next token is an identifier, then it names the
15692 parameter. */
15693 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15694 identifier = cp_parser_identifier (parser);
15695 else
15696 identifier = NULL_TREE;
15698 /* Create the parameter. */
15699 parameter = finish_template_type_parm (class_type_node, identifier);
15701 /* If the next token is an `=', we have a default argument. */
15702 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15704 default_argument
15705 = cp_parser_default_type_template_argument (parser);
15707 /* Template parameter packs cannot have default
15708 arguments. */
15709 if (*is_parameter_pack)
15711 if (identifier)
15712 error_at (token->location,
15713 "template parameter pack %qD cannot have a "
15714 "default argument", identifier);
15715 else
15716 error_at (token->location,
15717 "template parameter packs cannot have "
15718 "default arguments");
15719 default_argument = NULL_TREE;
15721 else if (check_for_bare_parameter_packs (default_argument))
15722 default_argument = error_mark_node;
15724 else
15725 default_argument = NULL_TREE;
15727 /* Create the combined representation of the parameter and the
15728 default argument. */
15729 parameter = build_tree_list (default_argument, parameter);
15731 break;
15733 case RID_TEMPLATE:
15735 tree identifier;
15736 tree default_argument;
15738 /* Look for the `<'. */
15739 cp_parser_require (parser, CPP_LESS, RT_LESS);
15740 /* Parse the template-parameter-list. */
15741 cp_parser_template_parameter_list (parser);
15742 /* Look for the `>'. */
15743 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15745 // If template requirements are present, parse them.
15746 if (flag_concepts)
15748 tree reqs = get_shorthand_constraints (current_template_parms);
15749 if (tree r = cp_parser_requires_clause_opt (parser))
15750 reqs = conjoin_constraints (reqs, normalize_expression (r));
15751 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15754 /* Look for the `class' or 'typename' keywords. */
15755 cp_parser_type_parameter_key (parser);
15756 /* If the next token is an ellipsis, we have a template
15757 argument pack. */
15758 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15760 /* Consume the `...' token. */
15761 cp_lexer_consume_token (parser->lexer);
15762 maybe_warn_variadic_templates ();
15764 *is_parameter_pack = true;
15766 /* If the next token is an `=', then there is a
15767 default-argument. If the next token is a `>', we are at
15768 the end of the parameter-list. If the next token is a `,',
15769 then we are at the end of this parameter. */
15770 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15771 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15772 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15774 identifier = cp_parser_identifier (parser);
15775 /* Treat invalid names as if the parameter were nameless. */
15776 if (identifier == error_mark_node)
15777 identifier = NULL_TREE;
15779 else
15780 identifier = NULL_TREE;
15782 /* Create the template parameter. */
15783 parameter = finish_template_template_parm (class_type_node,
15784 identifier);
15786 /* If the next token is an `=', then there is a
15787 default-argument. */
15788 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15790 default_argument
15791 = cp_parser_default_template_template_argument (parser);
15793 /* Template parameter packs cannot have default
15794 arguments. */
15795 if (*is_parameter_pack)
15797 if (identifier)
15798 error_at (token->location,
15799 "template parameter pack %qD cannot "
15800 "have a default argument",
15801 identifier);
15802 else
15803 error_at (token->location, "template parameter packs cannot "
15804 "have default arguments");
15805 default_argument = NULL_TREE;
15808 else
15809 default_argument = NULL_TREE;
15811 /* Create the combined representation of the parameter and the
15812 default argument. */
15813 parameter = build_tree_list (default_argument, parameter);
15815 break;
15817 default:
15818 gcc_unreachable ();
15819 break;
15822 return parameter;
15825 /* Parse a template-id.
15827 template-id:
15828 template-name < template-argument-list [opt] >
15830 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15831 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15832 returned. Otherwise, if the template-name names a function, or set
15833 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15834 names a class, returns a TYPE_DECL for the specialization.
15836 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15837 uninstantiated templates. */
15839 static tree
15840 cp_parser_template_id (cp_parser *parser,
15841 bool template_keyword_p,
15842 bool check_dependency_p,
15843 enum tag_types tag_type,
15844 bool is_declaration)
15846 tree templ;
15847 tree arguments;
15848 tree template_id;
15849 cp_token_position start_of_id = 0;
15850 cp_token *next_token = NULL, *next_token_2 = NULL;
15851 bool is_identifier;
15853 /* If the next token corresponds to a template-id, there is no need
15854 to reparse it. */
15855 cp_token *token = cp_lexer_peek_token (parser->lexer);
15856 if (token->type == CPP_TEMPLATE_ID)
15858 cp_lexer_consume_token (parser->lexer);
15859 return saved_checks_value (token->u.tree_check_value);
15862 /* Avoid performing name lookup if there is no possibility of
15863 finding a template-id. */
15864 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15865 || (token->type == CPP_NAME
15866 && !cp_parser_nth_token_starts_template_argument_list_p
15867 (parser, 2)))
15869 cp_parser_error (parser, "expected template-id");
15870 return error_mark_node;
15873 /* Remember where the template-id starts. */
15874 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15875 start_of_id = cp_lexer_token_position (parser->lexer, false);
15877 push_deferring_access_checks (dk_deferred);
15879 /* Parse the template-name. */
15880 is_identifier = false;
15881 templ = cp_parser_template_name (parser, template_keyword_p,
15882 check_dependency_p,
15883 is_declaration,
15884 tag_type,
15885 &is_identifier);
15886 if (templ == error_mark_node || is_identifier)
15888 pop_deferring_access_checks ();
15889 return templ;
15892 /* Since we're going to preserve any side-effects from this parse, set up a
15893 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15894 in the template arguments. */
15895 tentative_firewall firewall (parser);
15897 /* If we find the sequence `[:' after a template-name, it's probably
15898 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15899 parse correctly the argument list. */
15900 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15901 == CPP_OPEN_SQUARE)
15902 && next_token->flags & DIGRAPH
15903 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15904 == CPP_COLON)
15905 && !(next_token_2->flags & PREV_WHITE))
15907 cp_parser_parse_tentatively (parser);
15908 /* Change `:' into `::'. */
15909 next_token_2->type = CPP_SCOPE;
15910 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15911 CPP_LESS. */
15912 cp_lexer_consume_token (parser->lexer);
15914 /* Parse the arguments. */
15915 arguments = cp_parser_enclosed_template_argument_list (parser);
15916 if (!cp_parser_parse_definitely (parser))
15918 /* If we couldn't parse an argument list, then we revert our changes
15919 and return simply an error. Maybe this is not a template-id
15920 after all. */
15921 next_token_2->type = CPP_COLON;
15922 cp_parser_error (parser, "expected %<<%>");
15923 pop_deferring_access_checks ();
15924 return error_mark_node;
15926 /* Otherwise, emit an error about the invalid digraph, but continue
15927 parsing because we got our argument list. */
15928 if (permerror (next_token->location,
15929 "%<<::%> cannot begin a template-argument list"))
15931 static bool hint = false;
15932 inform (next_token->location,
15933 "%<<:%> is an alternate spelling for %<[%>."
15934 " Insert whitespace between %<<%> and %<::%>");
15935 if (!hint && !flag_permissive)
15937 inform (next_token->location, "(if you use %<-fpermissive%> "
15938 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15939 "accept your code)");
15940 hint = true;
15944 else
15946 /* Look for the `<' that starts the template-argument-list. */
15947 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15949 pop_deferring_access_checks ();
15950 return error_mark_node;
15952 /* Parse the arguments. */
15953 arguments = cp_parser_enclosed_template_argument_list (parser);
15956 /* Set the location to be of the form:
15957 template-name < template-argument-list [opt] >
15958 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15959 with caret == start at the start of the template-name,
15960 ranging until the closing '>'. */
15961 location_t finish_loc
15962 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15963 location_t combined_loc
15964 = make_location (token->location, token->location, finish_loc);
15966 /* Check for concepts autos where they don't belong. We could
15967 identify types in some cases of idnetifier TEMPL, looking ahead
15968 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15969 types. We reject them in functions, but if what we have is an
15970 identifier, even with none_type we can't conclude it's NOT a
15971 type, we have to wait for template substitution. */
15972 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15973 template_id = error_mark_node;
15974 /* Build a representation of the specialization. */
15975 else if (identifier_p (templ))
15976 template_id = build_min_nt_loc (combined_loc,
15977 TEMPLATE_ID_EXPR,
15978 templ, arguments);
15979 else if (DECL_TYPE_TEMPLATE_P (templ)
15980 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15982 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15983 template (rather than some instantiation thereof) only if
15984 is not nested within some other construct. For example, in
15985 "template <typename T> void f(T) { A<T>::", A<T> is just an
15986 instantiation of A. */
15987 bool entering_scope
15988 = (template_parm_scope_p ()
15989 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
15990 template_id
15991 = finish_template_type (templ, arguments, entering_scope);
15993 /* A template-like identifier may be a partial concept id. */
15994 else if (flag_concepts
15995 && (template_id = (cp_parser_maybe_partial_concept_id
15996 (parser, templ, arguments))))
15997 return template_id;
15998 else if (variable_template_p (templ))
16000 template_id = lookup_template_variable (templ, arguments);
16001 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16002 SET_EXPR_LOCATION (template_id, combined_loc);
16004 else
16006 /* If it's not a class-template or a template-template, it should be
16007 a function-template. */
16008 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16009 || TREE_CODE (templ) == OVERLOAD
16010 || BASELINK_P (templ)));
16012 template_id = lookup_template_function (templ, arguments);
16013 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16014 SET_EXPR_LOCATION (template_id, combined_loc);
16017 /* If parsing tentatively, replace the sequence of tokens that makes
16018 up the template-id with a CPP_TEMPLATE_ID token. That way,
16019 should we re-parse the token stream, we will not have to repeat
16020 the effort required to do the parse, nor will we issue duplicate
16021 error messages about problems during instantiation of the
16022 template. */
16023 if (start_of_id
16024 /* Don't do this if we had a parse error in a declarator; re-parsing
16025 might succeed if a name changes meaning (60361). */
16026 && !(cp_parser_error_occurred (parser)
16027 && cp_parser_parsing_tentatively (parser)
16028 && parser->in_declarator_p))
16030 /* Reset the contents of the START_OF_ID token. */
16031 token->type = CPP_TEMPLATE_ID;
16032 token->location = combined_loc;
16034 /* Retrieve any deferred checks. Do not pop this access checks yet
16035 so the memory will not be reclaimed during token replacing below. */
16036 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16037 token->u.tree_check_value->value = template_id;
16038 token->u.tree_check_value->checks = get_deferred_access_checks ();
16039 token->keyword = RID_MAX;
16041 /* Purge all subsequent tokens. */
16042 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16044 /* ??? Can we actually assume that, if template_id ==
16045 error_mark_node, we will have issued a diagnostic to the
16046 user, as opposed to simply marking the tentative parse as
16047 failed? */
16048 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16049 error_at (token->location, "parse error in template argument list");
16052 pop_to_parent_deferring_access_checks ();
16053 return template_id;
16056 /* Parse a template-name.
16058 template-name:
16059 identifier
16061 The standard should actually say:
16063 template-name:
16064 identifier
16065 operator-function-id
16067 A defect report has been filed about this issue.
16069 A conversion-function-id cannot be a template name because they cannot
16070 be part of a template-id. In fact, looking at this code:
16072 a.operator K<int>()
16074 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16075 It is impossible to call a templated conversion-function-id with an
16076 explicit argument list, since the only allowed template parameter is
16077 the type to which it is converting.
16079 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16080 `template' keyword, in a construction like:
16082 T::template f<3>()
16084 In that case `f' is taken to be a template-name, even though there
16085 is no way of knowing for sure.
16087 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16088 name refers to a set of overloaded functions, at least one of which
16089 is a template, or an IDENTIFIER_NODE with the name of the template,
16090 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16091 names are looked up inside uninstantiated templates. */
16093 static tree
16094 cp_parser_template_name (cp_parser* parser,
16095 bool template_keyword_p,
16096 bool check_dependency_p,
16097 bool is_declaration,
16098 enum tag_types tag_type,
16099 bool *is_identifier)
16101 tree identifier;
16102 tree decl;
16103 cp_token *token = cp_lexer_peek_token (parser->lexer);
16105 /* If the next token is `operator', then we have either an
16106 operator-function-id or a conversion-function-id. */
16107 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16109 /* We don't know whether we're looking at an
16110 operator-function-id or a conversion-function-id. */
16111 cp_parser_parse_tentatively (parser);
16112 /* Try an operator-function-id. */
16113 identifier = cp_parser_operator_function_id (parser);
16114 /* If that didn't work, try a conversion-function-id. */
16115 if (!cp_parser_parse_definitely (parser))
16117 cp_parser_error (parser, "expected template-name");
16118 return error_mark_node;
16121 /* Look for the identifier. */
16122 else
16123 identifier = cp_parser_identifier (parser);
16125 /* If we didn't find an identifier, we don't have a template-id. */
16126 if (identifier == error_mark_node)
16127 return error_mark_node;
16129 /* If the name immediately followed the `template' keyword, then it
16130 is a template-name. However, if the next token is not `<', then
16131 we do not treat it as a template-name, since it is not being used
16132 as part of a template-id. This enables us to handle constructs
16133 like:
16135 template <typename T> struct S { S(); };
16136 template <typename T> S<T>::S();
16138 correctly. We would treat `S' as a template -- if it were `S<T>'
16139 -- but we do not if there is no `<'. */
16141 if (processing_template_decl
16142 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16144 /* In a declaration, in a dependent context, we pretend that the
16145 "template" keyword was present in order to improve error
16146 recovery. For example, given:
16148 template <typename T> void f(T::X<int>);
16150 we want to treat "X<int>" as a template-id. */
16151 if (is_declaration
16152 && !template_keyword_p
16153 && parser->scope && TYPE_P (parser->scope)
16154 && check_dependency_p
16155 && dependent_scope_p (parser->scope)
16156 /* Do not do this for dtors (or ctors), since they never
16157 need the template keyword before their name. */
16158 && !constructor_name_p (identifier, parser->scope))
16160 cp_token_position start = 0;
16162 /* Explain what went wrong. */
16163 error_at (token->location, "non-template %qD used as template",
16164 identifier);
16165 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16166 parser->scope, identifier);
16167 /* If parsing tentatively, find the location of the "<" token. */
16168 if (cp_parser_simulate_error (parser))
16169 start = cp_lexer_token_position (parser->lexer, true);
16170 /* Parse the template arguments so that we can issue error
16171 messages about them. */
16172 cp_lexer_consume_token (parser->lexer);
16173 cp_parser_enclosed_template_argument_list (parser);
16174 /* Skip tokens until we find a good place from which to
16175 continue parsing. */
16176 cp_parser_skip_to_closing_parenthesis (parser,
16177 /*recovering=*/true,
16178 /*or_comma=*/true,
16179 /*consume_paren=*/false);
16180 /* If parsing tentatively, permanently remove the
16181 template argument list. That will prevent duplicate
16182 error messages from being issued about the missing
16183 "template" keyword. */
16184 if (start)
16185 cp_lexer_purge_tokens_after (parser->lexer, start);
16186 if (is_identifier)
16187 *is_identifier = true;
16188 parser->context->object_type = NULL_TREE;
16189 return identifier;
16192 /* If the "template" keyword is present, then there is generally
16193 no point in doing name-lookup, so we just return IDENTIFIER.
16194 But, if the qualifying scope is non-dependent then we can
16195 (and must) do name-lookup normally. */
16196 if (template_keyword_p)
16198 tree scope = (parser->scope ? parser->scope
16199 : parser->context->object_type);
16200 if (scope && TYPE_P (scope)
16201 && (!CLASS_TYPE_P (scope)
16202 || (check_dependency_p && dependent_type_p (scope))))
16204 /* We're optimizing away the call to cp_parser_lookup_name, but
16205 we still need to do this. */
16206 parser->context->object_type = NULL_TREE;
16207 return identifier;
16212 /* Look up the name. */
16213 decl = cp_parser_lookup_name (parser, identifier,
16214 tag_type,
16215 /*is_template=*/true,
16216 /*is_namespace=*/false,
16217 check_dependency_p,
16218 /*ambiguous_decls=*/NULL,
16219 token->location);
16221 decl = strip_using_decl (decl);
16223 /* If DECL is a template, then the name was a template-name. */
16224 if (TREE_CODE (decl) == TEMPLATE_DECL)
16226 if (TREE_DEPRECATED (decl)
16227 && deprecated_state != DEPRECATED_SUPPRESS)
16228 warn_deprecated_use (decl, NULL_TREE);
16230 else
16232 /* The standard does not explicitly indicate whether a name that
16233 names a set of overloaded declarations, some of which are
16234 templates, is a template-name. However, such a name should
16235 be a template-name; otherwise, there is no way to form a
16236 template-id for the overloaded templates. */
16237 bool found = false;
16239 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16240 !found && iter; ++iter)
16241 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16242 found = true;
16244 if (!found)
16246 /* The name does not name a template. */
16247 cp_parser_error (parser, "expected template-name");
16248 return error_mark_node;
16252 /* If DECL is dependent, and refers to a function, then just return
16253 its name; we will look it up again during template instantiation. */
16254 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16256 tree scope = ovl_scope (decl);
16257 if (TYPE_P (scope) && dependent_type_p (scope))
16258 return identifier;
16261 return decl;
16264 /* Parse a template-argument-list.
16266 template-argument-list:
16267 template-argument ... [opt]
16268 template-argument-list , template-argument ... [opt]
16270 Returns a TREE_VEC containing the arguments. */
16272 static tree
16273 cp_parser_template_argument_list (cp_parser* parser)
16275 tree fixed_args[10];
16276 unsigned n_args = 0;
16277 unsigned alloced = 10;
16278 tree *arg_ary = fixed_args;
16279 tree vec;
16280 bool saved_in_template_argument_list_p;
16281 bool saved_ice_p;
16282 bool saved_non_ice_p;
16284 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16285 parser->in_template_argument_list_p = true;
16286 /* Even if the template-id appears in an integral
16287 constant-expression, the contents of the argument list do
16288 not. */
16289 saved_ice_p = parser->integral_constant_expression_p;
16290 parser->integral_constant_expression_p = false;
16291 saved_non_ice_p = parser->non_integral_constant_expression_p;
16292 parser->non_integral_constant_expression_p = false;
16294 /* Parse the arguments. */
16297 tree argument;
16299 if (n_args)
16300 /* Consume the comma. */
16301 cp_lexer_consume_token (parser->lexer);
16303 /* Parse the template-argument. */
16304 argument = cp_parser_template_argument (parser);
16306 /* If the next token is an ellipsis, we're expanding a template
16307 argument pack. */
16308 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16310 if (argument == error_mark_node)
16312 cp_token *token = cp_lexer_peek_token (parser->lexer);
16313 error_at (token->location,
16314 "expected parameter pack before %<...%>");
16316 /* Consume the `...' token. */
16317 cp_lexer_consume_token (parser->lexer);
16319 /* Make the argument into a TYPE_PACK_EXPANSION or
16320 EXPR_PACK_EXPANSION. */
16321 argument = make_pack_expansion (argument);
16324 if (n_args == alloced)
16326 alloced *= 2;
16328 if (arg_ary == fixed_args)
16330 arg_ary = XNEWVEC (tree, alloced);
16331 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16333 else
16334 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16336 arg_ary[n_args++] = argument;
16338 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16340 vec = make_tree_vec (n_args);
16342 while (n_args--)
16343 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16345 if (arg_ary != fixed_args)
16346 free (arg_ary);
16347 parser->non_integral_constant_expression_p = saved_non_ice_p;
16348 parser->integral_constant_expression_p = saved_ice_p;
16349 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16350 if (CHECKING_P)
16351 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16352 return vec;
16355 /* Parse a template-argument.
16357 template-argument:
16358 assignment-expression
16359 type-id
16360 id-expression
16362 The representation is that of an assignment-expression, type-id, or
16363 id-expression -- except that the qualified id-expression is
16364 evaluated, so that the value returned is either a DECL or an
16365 OVERLOAD.
16367 Although the standard says "assignment-expression", it forbids
16368 throw-expressions or assignments in the template argument.
16369 Therefore, we use "conditional-expression" instead. */
16371 static tree
16372 cp_parser_template_argument (cp_parser* parser)
16374 tree argument;
16375 bool template_p;
16376 bool address_p;
16377 bool maybe_type_id = false;
16378 cp_token *token = NULL, *argument_start_token = NULL;
16379 location_t loc = 0;
16380 cp_id_kind idk;
16382 /* There's really no way to know what we're looking at, so we just
16383 try each alternative in order.
16385 [temp.arg]
16387 In a template-argument, an ambiguity between a type-id and an
16388 expression is resolved to a type-id, regardless of the form of
16389 the corresponding template-parameter.
16391 Therefore, we try a type-id first. */
16392 cp_parser_parse_tentatively (parser);
16393 argument = cp_parser_template_type_arg (parser);
16394 /* If there was no error parsing the type-id but the next token is a
16395 '>>', our behavior depends on which dialect of C++ we're
16396 parsing. In C++98, we probably found a typo for '> >'. But there
16397 are type-id which are also valid expressions. For instance:
16399 struct X { int operator >> (int); };
16400 template <int V> struct Foo {};
16401 Foo<X () >> 5> r;
16403 Here 'X()' is a valid type-id of a function type, but the user just
16404 wanted to write the expression "X() >> 5". Thus, we remember that we
16405 found a valid type-id, but we still try to parse the argument as an
16406 expression to see what happens.
16408 In C++0x, the '>>' will be considered two separate '>'
16409 tokens. */
16410 if (!cp_parser_error_occurred (parser)
16411 && cxx_dialect == cxx98
16412 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16414 maybe_type_id = true;
16415 cp_parser_abort_tentative_parse (parser);
16417 else
16419 /* If the next token isn't a `,' or a `>', then this argument wasn't
16420 really finished. This means that the argument is not a valid
16421 type-id. */
16422 if (!cp_parser_next_token_ends_template_argument_p (parser))
16423 cp_parser_error (parser, "expected template-argument");
16424 /* If that worked, we're done. */
16425 if (cp_parser_parse_definitely (parser))
16426 return argument;
16428 /* We're still not sure what the argument will be. */
16429 cp_parser_parse_tentatively (parser);
16430 /* Try a template. */
16431 argument_start_token = cp_lexer_peek_token (parser->lexer);
16432 argument = cp_parser_id_expression (parser,
16433 /*template_keyword_p=*/false,
16434 /*check_dependency_p=*/true,
16435 &template_p,
16436 /*declarator_p=*/false,
16437 /*optional_p=*/false);
16438 /* If the next token isn't a `,' or a `>', then this argument wasn't
16439 really finished. */
16440 if (!cp_parser_next_token_ends_template_argument_p (parser))
16441 cp_parser_error (parser, "expected template-argument");
16442 if (!cp_parser_error_occurred (parser))
16444 /* Figure out what is being referred to. If the id-expression
16445 was for a class template specialization, then we will have a
16446 TYPE_DECL at this point. There is no need to do name lookup
16447 at this point in that case. */
16448 if (TREE_CODE (argument) != TYPE_DECL)
16449 argument = cp_parser_lookup_name (parser, argument,
16450 none_type,
16451 /*is_template=*/template_p,
16452 /*is_namespace=*/false,
16453 /*check_dependency=*/true,
16454 /*ambiguous_decls=*/NULL,
16455 argument_start_token->location);
16456 /* Handle a constrained-type-specifier for a non-type template
16457 parameter. */
16458 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16459 argument = decl;
16460 else if (TREE_CODE (argument) != TEMPLATE_DECL
16461 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16462 cp_parser_error (parser, "expected template-name");
16464 if (cp_parser_parse_definitely (parser))
16466 if (TREE_DEPRECATED (argument))
16467 warn_deprecated_use (argument, NULL_TREE);
16468 return argument;
16470 /* It must be a non-type argument. In C++17 any constant-expression is
16471 allowed. */
16472 if (cxx_dialect > cxx14)
16473 goto general_expr;
16475 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16477 -- an integral constant-expression of integral or enumeration
16478 type; or
16480 -- the name of a non-type template-parameter; or
16482 -- the name of an object or function with external linkage...
16484 -- the address of an object or function with external linkage...
16486 -- a pointer to member... */
16487 /* Look for a non-type template parameter. */
16488 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16490 cp_parser_parse_tentatively (parser);
16491 argument = cp_parser_primary_expression (parser,
16492 /*address_p=*/false,
16493 /*cast_p=*/false,
16494 /*template_arg_p=*/true,
16495 &idk);
16496 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16497 || !cp_parser_next_token_ends_template_argument_p (parser))
16498 cp_parser_simulate_error (parser);
16499 if (cp_parser_parse_definitely (parser))
16500 return argument;
16503 /* If the next token is "&", the argument must be the address of an
16504 object or function with external linkage. */
16505 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16506 if (address_p)
16508 loc = cp_lexer_peek_token (parser->lexer)->location;
16509 cp_lexer_consume_token (parser->lexer);
16511 /* See if we might have an id-expression. */
16512 token = cp_lexer_peek_token (parser->lexer);
16513 if (token->type == CPP_NAME
16514 || token->keyword == RID_OPERATOR
16515 || token->type == CPP_SCOPE
16516 || token->type == CPP_TEMPLATE_ID
16517 || token->type == CPP_NESTED_NAME_SPECIFIER)
16519 cp_parser_parse_tentatively (parser);
16520 argument = cp_parser_primary_expression (parser,
16521 address_p,
16522 /*cast_p=*/false,
16523 /*template_arg_p=*/true,
16524 &idk);
16525 if (cp_parser_error_occurred (parser)
16526 || !cp_parser_next_token_ends_template_argument_p (parser))
16527 cp_parser_abort_tentative_parse (parser);
16528 else
16530 tree probe;
16532 if (INDIRECT_REF_P (argument))
16534 /* Strip the dereference temporarily. */
16535 gcc_assert (REFERENCE_REF_P (argument));
16536 argument = TREE_OPERAND (argument, 0);
16539 /* If we're in a template, we represent a qualified-id referring
16540 to a static data member as a SCOPE_REF even if the scope isn't
16541 dependent so that we can check access control later. */
16542 probe = argument;
16543 if (TREE_CODE (probe) == SCOPE_REF)
16544 probe = TREE_OPERAND (probe, 1);
16545 if (VAR_P (probe))
16547 /* A variable without external linkage might still be a
16548 valid constant-expression, so no error is issued here
16549 if the external-linkage check fails. */
16550 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16551 cp_parser_simulate_error (parser);
16553 else if (is_overloaded_fn (argument))
16554 /* All overloaded functions are allowed; if the external
16555 linkage test does not pass, an error will be issued
16556 later. */
16558 else if (address_p
16559 && (TREE_CODE (argument) == OFFSET_REF
16560 || TREE_CODE (argument) == SCOPE_REF))
16561 /* A pointer-to-member. */
16563 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16565 else
16566 cp_parser_simulate_error (parser);
16568 if (cp_parser_parse_definitely (parser))
16570 if (address_p)
16571 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16572 tf_warning_or_error);
16573 else
16574 argument = convert_from_reference (argument);
16575 return argument;
16579 /* If the argument started with "&", there are no other valid
16580 alternatives at this point. */
16581 if (address_p)
16583 cp_parser_error (parser, "invalid non-type template argument");
16584 return error_mark_node;
16587 general_expr:
16588 /* If the argument wasn't successfully parsed as a type-id followed
16589 by '>>', the argument can only be a constant expression now.
16590 Otherwise, we try parsing the constant-expression tentatively,
16591 because the argument could really be a type-id. */
16592 if (maybe_type_id)
16593 cp_parser_parse_tentatively (parser);
16595 if (cxx_dialect <= cxx14)
16596 argument = cp_parser_constant_expression (parser);
16597 else
16599 /* With C++17 generalized non-type template arguments we need to handle
16600 lvalue constant expressions, too. */
16601 argument = cp_parser_assignment_expression (parser);
16602 require_potential_constant_expression (argument);
16605 if (!maybe_type_id)
16606 return argument;
16607 if (!cp_parser_next_token_ends_template_argument_p (parser))
16608 cp_parser_error (parser, "expected template-argument");
16609 if (cp_parser_parse_definitely (parser))
16610 return argument;
16611 /* We did our best to parse the argument as a non type-id, but that
16612 was the only alternative that matched (albeit with a '>' after
16613 it). We can assume it's just a typo from the user, and a
16614 diagnostic will then be issued. */
16615 return cp_parser_template_type_arg (parser);
16618 /* Parse an explicit-instantiation.
16620 explicit-instantiation:
16621 template declaration
16623 Although the standard says `declaration', what it really means is:
16625 explicit-instantiation:
16626 template decl-specifier-seq [opt] declarator [opt] ;
16628 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16629 supposed to be allowed. A defect report has been filed about this
16630 issue.
16632 GNU Extension:
16634 explicit-instantiation:
16635 storage-class-specifier template
16636 decl-specifier-seq [opt] declarator [opt] ;
16637 function-specifier template
16638 decl-specifier-seq [opt] declarator [opt] ; */
16640 static void
16641 cp_parser_explicit_instantiation (cp_parser* parser)
16643 int declares_class_or_enum;
16644 cp_decl_specifier_seq decl_specifiers;
16645 tree extension_specifier = NULL_TREE;
16647 timevar_push (TV_TEMPLATE_INST);
16649 /* Look for an (optional) storage-class-specifier or
16650 function-specifier. */
16651 if (cp_parser_allow_gnu_extensions_p (parser))
16653 extension_specifier
16654 = cp_parser_storage_class_specifier_opt (parser);
16655 if (!extension_specifier)
16656 extension_specifier
16657 = cp_parser_function_specifier_opt (parser,
16658 /*decl_specs=*/NULL);
16661 /* Look for the `template' keyword. */
16662 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16663 /* Let the front end know that we are processing an explicit
16664 instantiation. */
16665 begin_explicit_instantiation ();
16666 /* [temp.explicit] says that we are supposed to ignore access
16667 control while processing explicit instantiation directives. */
16668 push_deferring_access_checks (dk_no_check);
16669 /* Parse a decl-specifier-seq. */
16670 cp_parser_decl_specifier_seq (parser,
16671 CP_PARSER_FLAGS_OPTIONAL,
16672 &decl_specifiers,
16673 &declares_class_or_enum);
16674 /* If there was exactly one decl-specifier, and it declared a class,
16675 and there's no declarator, then we have an explicit type
16676 instantiation. */
16677 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16679 tree type;
16681 type = check_tag_decl (&decl_specifiers,
16682 /*explicit_type_instantiation_p=*/true);
16683 /* Turn access control back on for names used during
16684 template instantiation. */
16685 pop_deferring_access_checks ();
16686 if (type)
16687 do_type_instantiation (type, extension_specifier,
16688 /*complain=*/tf_error);
16690 else
16692 cp_declarator *declarator;
16693 tree decl;
16695 /* Parse the declarator. */
16696 declarator
16697 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16698 /*ctor_dtor_or_conv_p=*/NULL,
16699 /*parenthesized_p=*/NULL,
16700 /*member_p=*/false,
16701 /*friend_p=*/false);
16702 if (declares_class_or_enum & 2)
16703 cp_parser_check_for_definition_in_return_type (declarator,
16704 decl_specifiers.type,
16705 decl_specifiers.locations[ds_type_spec]);
16706 if (declarator != cp_error_declarator)
16708 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16709 permerror (decl_specifiers.locations[ds_inline],
16710 "explicit instantiation shall not use"
16711 " %<inline%> specifier");
16712 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16713 permerror (decl_specifiers.locations[ds_constexpr],
16714 "explicit instantiation shall not use"
16715 " %<constexpr%> specifier");
16717 decl = grokdeclarator (declarator, &decl_specifiers,
16718 NORMAL, 0, &decl_specifiers.attributes);
16719 /* Turn access control back on for names used during
16720 template instantiation. */
16721 pop_deferring_access_checks ();
16722 /* Do the explicit instantiation. */
16723 do_decl_instantiation (decl, extension_specifier);
16725 else
16727 pop_deferring_access_checks ();
16728 /* Skip the body of the explicit instantiation. */
16729 cp_parser_skip_to_end_of_statement (parser);
16732 /* We're done with the instantiation. */
16733 end_explicit_instantiation ();
16735 cp_parser_consume_semicolon_at_end_of_statement (parser);
16737 timevar_pop (TV_TEMPLATE_INST);
16740 /* Parse an explicit-specialization.
16742 explicit-specialization:
16743 template < > declaration
16745 Although the standard says `declaration', what it really means is:
16747 explicit-specialization:
16748 template <> decl-specifier [opt] init-declarator [opt] ;
16749 template <> function-definition
16750 template <> explicit-specialization
16751 template <> template-declaration */
16753 static void
16754 cp_parser_explicit_specialization (cp_parser* parser)
16756 bool need_lang_pop;
16757 cp_token *token = cp_lexer_peek_token (parser->lexer);
16759 /* Look for the `template' keyword. */
16760 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16761 /* Look for the `<'. */
16762 cp_parser_require (parser, CPP_LESS, RT_LESS);
16763 /* Look for the `>'. */
16764 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16765 /* We have processed another parameter list. */
16766 ++parser->num_template_parameter_lists;
16767 /* [temp]
16769 A template ... explicit specialization ... shall not have C
16770 linkage. */
16771 if (current_lang_name == lang_name_c)
16773 error_at (token->location, "template specialization with C linkage");
16774 maybe_show_extern_c_location ();
16775 /* Give it C++ linkage to avoid confusing other parts of the
16776 front end. */
16777 push_lang_context (lang_name_cplusplus);
16778 need_lang_pop = true;
16780 else
16781 need_lang_pop = false;
16782 /* Let the front end know that we are beginning a specialization. */
16783 if (!begin_specialization ())
16785 end_specialization ();
16786 return;
16789 /* If the next keyword is `template', we need to figure out whether
16790 or not we're looking a template-declaration. */
16791 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16793 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16794 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16795 cp_parser_template_declaration_after_export (parser,
16796 /*member_p=*/false);
16797 else
16798 cp_parser_explicit_specialization (parser);
16800 else
16801 /* Parse the dependent declaration. */
16802 cp_parser_single_declaration (parser,
16803 /*checks=*/NULL,
16804 /*member_p=*/false,
16805 /*explicit_specialization_p=*/true,
16806 /*friend_p=*/NULL);
16807 /* We're done with the specialization. */
16808 end_specialization ();
16809 /* For the erroneous case of a template with C linkage, we pushed an
16810 implicit C++ linkage scope; exit that scope now. */
16811 if (need_lang_pop)
16812 pop_lang_context ();
16813 /* We're done with this parameter list. */
16814 --parser->num_template_parameter_lists;
16817 /* Parse a type-specifier.
16819 type-specifier:
16820 simple-type-specifier
16821 class-specifier
16822 enum-specifier
16823 elaborated-type-specifier
16824 cv-qualifier
16826 GNU Extension:
16828 type-specifier:
16829 __complex__
16831 Returns a representation of the type-specifier. For a
16832 class-specifier, enum-specifier, or elaborated-type-specifier, a
16833 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16835 The parser flags FLAGS is used to control type-specifier parsing.
16837 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16838 in a decl-specifier-seq.
16840 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16841 class-specifier, enum-specifier, or elaborated-type-specifier, then
16842 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16843 if a type is declared; 2 if it is defined. Otherwise, it is set to
16844 zero.
16846 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16847 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16848 is set to FALSE. */
16850 static tree
16851 cp_parser_type_specifier (cp_parser* parser,
16852 cp_parser_flags flags,
16853 cp_decl_specifier_seq *decl_specs,
16854 bool is_declaration,
16855 int* declares_class_or_enum,
16856 bool* is_cv_qualifier)
16858 tree type_spec = NULL_TREE;
16859 cp_token *token;
16860 enum rid keyword;
16861 cp_decl_spec ds = ds_last;
16863 /* Assume this type-specifier does not declare a new type. */
16864 if (declares_class_or_enum)
16865 *declares_class_or_enum = 0;
16866 /* And that it does not specify a cv-qualifier. */
16867 if (is_cv_qualifier)
16868 *is_cv_qualifier = false;
16869 /* Peek at the next token. */
16870 token = cp_lexer_peek_token (parser->lexer);
16872 /* If we're looking at a keyword, we can use that to guide the
16873 production we choose. */
16874 keyword = token->keyword;
16875 switch (keyword)
16877 case RID_ENUM:
16878 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16879 goto elaborated_type_specifier;
16881 /* Look for the enum-specifier. */
16882 type_spec = cp_parser_enum_specifier (parser);
16883 /* If that worked, we're done. */
16884 if (type_spec)
16886 if (declares_class_or_enum)
16887 *declares_class_or_enum = 2;
16888 if (decl_specs)
16889 cp_parser_set_decl_spec_type (decl_specs,
16890 type_spec,
16891 token,
16892 /*type_definition_p=*/true);
16893 return type_spec;
16895 else
16896 goto elaborated_type_specifier;
16898 /* Any of these indicate either a class-specifier, or an
16899 elaborated-type-specifier. */
16900 case RID_CLASS:
16901 case RID_STRUCT:
16902 case RID_UNION:
16903 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16904 goto elaborated_type_specifier;
16906 /* Parse tentatively so that we can back up if we don't find a
16907 class-specifier. */
16908 cp_parser_parse_tentatively (parser);
16909 /* Look for the class-specifier. */
16910 type_spec = cp_parser_class_specifier (parser);
16911 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16912 /* If that worked, we're done. */
16913 if (cp_parser_parse_definitely (parser))
16915 if (declares_class_or_enum)
16916 *declares_class_or_enum = 2;
16917 if (decl_specs)
16918 cp_parser_set_decl_spec_type (decl_specs,
16919 type_spec,
16920 token,
16921 /*type_definition_p=*/true);
16922 return type_spec;
16925 /* Fall through. */
16926 elaborated_type_specifier:
16927 /* We're declaring (not defining) a class or enum. */
16928 if (declares_class_or_enum)
16929 *declares_class_or_enum = 1;
16931 /* Fall through. */
16932 case RID_TYPENAME:
16933 /* Look for an elaborated-type-specifier. */
16934 type_spec
16935 = (cp_parser_elaborated_type_specifier
16936 (parser,
16937 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16938 is_declaration));
16939 if (decl_specs)
16940 cp_parser_set_decl_spec_type (decl_specs,
16941 type_spec,
16942 token,
16943 /*type_definition_p=*/false);
16944 return type_spec;
16946 case RID_CONST:
16947 ds = ds_const;
16948 if (is_cv_qualifier)
16949 *is_cv_qualifier = true;
16950 break;
16952 case RID_VOLATILE:
16953 ds = ds_volatile;
16954 if (is_cv_qualifier)
16955 *is_cv_qualifier = true;
16956 break;
16958 case RID_RESTRICT:
16959 ds = ds_restrict;
16960 if (is_cv_qualifier)
16961 *is_cv_qualifier = true;
16962 break;
16964 case RID_COMPLEX:
16965 /* The `__complex__' keyword is a GNU extension. */
16966 ds = ds_complex;
16967 break;
16969 default:
16970 break;
16973 /* Handle simple keywords. */
16974 if (ds != ds_last)
16976 if (decl_specs)
16978 set_and_check_decl_spec_loc (decl_specs, ds, token);
16979 decl_specs->any_specifiers_p = true;
16981 return cp_lexer_consume_token (parser->lexer)->u.value;
16984 /* If we do not already have a type-specifier, assume we are looking
16985 at a simple-type-specifier. */
16986 type_spec = cp_parser_simple_type_specifier (parser,
16987 decl_specs,
16988 flags);
16990 /* If we didn't find a type-specifier, and a type-specifier was not
16991 optional in this context, issue an error message. */
16992 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16994 cp_parser_error (parser, "expected type specifier");
16995 return error_mark_node;
16998 return type_spec;
17001 /* Parse a simple-type-specifier.
17003 simple-type-specifier:
17004 :: [opt] nested-name-specifier [opt] type-name
17005 :: [opt] nested-name-specifier template template-id
17006 char
17007 wchar_t
17008 bool
17009 short
17011 long
17012 signed
17013 unsigned
17014 float
17015 double
17016 void
17018 C++11 Extension:
17020 simple-type-specifier:
17021 auto
17022 decltype ( expression )
17023 char16_t
17024 char32_t
17025 __underlying_type ( type-id )
17027 C++17 extension:
17029 nested-name-specifier(opt) template-name
17031 GNU Extension:
17033 simple-type-specifier:
17034 __int128
17035 __typeof__ unary-expression
17036 __typeof__ ( type-id )
17037 __typeof__ ( type-id ) { initializer-list , [opt] }
17039 Concepts Extension:
17041 simple-type-specifier:
17042 constrained-type-specifier
17044 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17045 appropriately updated. */
17047 static tree
17048 cp_parser_simple_type_specifier (cp_parser* parser,
17049 cp_decl_specifier_seq *decl_specs,
17050 cp_parser_flags flags)
17052 tree type = NULL_TREE;
17053 cp_token *token;
17054 int idx;
17056 /* Peek at the next token. */
17057 token = cp_lexer_peek_token (parser->lexer);
17059 /* If we're looking at a keyword, things are easy. */
17060 switch (token->keyword)
17062 case RID_CHAR:
17063 if (decl_specs)
17064 decl_specs->explicit_char_p = true;
17065 type = char_type_node;
17066 break;
17067 case RID_CHAR16:
17068 type = char16_type_node;
17069 break;
17070 case RID_CHAR32:
17071 type = char32_type_node;
17072 break;
17073 case RID_WCHAR:
17074 type = wchar_type_node;
17075 break;
17076 case RID_BOOL:
17077 type = boolean_type_node;
17078 break;
17079 case RID_SHORT:
17080 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17081 type = short_integer_type_node;
17082 break;
17083 case RID_INT:
17084 if (decl_specs)
17085 decl_specs->explicit_int_p = true;
17086 type = integer_type_node;
17087 break;
17088 case RID_INT_N_0:
17089 case RID_INT_N_1:
17090 case RID_INT_N_2:
17091 case RID_INT_N_3:
17092 idx = token->keyword - RID_INT_N_0;
17093 if (! int_n_enabled_p [idx])
17094 break;
17095 if (decl_specs)
17097 decl_specs->explicit_intN_p = true;
17098 decl_specs->int_n_idx = idx;
17100 type = int_n_trees [idx].signed_type;
17101 break;
17102 case RID_LONG:
17103 if (decl_specs)
17104 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17105 type = long_integer_type_node;
17106 break;
17107 case RID_SIGNED:
17108 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17109 type = integer_type_node;
17110 break;
17111 case RID_UNSIGNED:
17112 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17113 type = unsigned_type_node;
17114 break;
17115 case RID_FLOAT:
17116 type = float_type_node;
17117 break;
17118 case RID_DOUBLE:
17119 type = double_type_node;
17120 break;
17121 case RID_VOID:
17122 type = void_type_node;
17123 break;
17125 case RID_AUTO:
17126 maybe_warn_cpp0x (CPP0X_AUTO);
17127 if (parser->auto_is_implicit_function_template_parm_p)
17129 /* The 'auto' might be the placeholder return type for a function decl
17130 with trailing return type. */
17131 bool have_trailing_return_fn_decl = false;
17133 cp_parser_parse_tentatively (parser);
17134 cp_lexer_consume_token (parser->lexer);
17135 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17136 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17137 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17138 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17140 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17142 cp_lexer_consume_token (parser->lexer);
17143 cp_parser_skip_to_closing_parenthesis (parser,
17144 /*recovering*/false,
17145 /*or_comma*/false,
17146 /*consume_paren*/true);
17147 continue;
17150 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17152 have_trailing_return_fn_decl = true;
17153 break;
17156 cp_lexer_consume_token (parser->lexer);
17158 cp_parser_abort_tentative_parse (parser);
17160 if (have_trailing_return_fn_decl)
17162 type = make_auto ();
17163 break;
17166 if (cxx_dialect >= cxx14)
17168 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17169 type = TREE_TYPE (type);
17171 else
17172 type = error_mark_node;
17174 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17176 if (cxx_dialect < cxx14)
17177 error_at (token->location,
17178 "use of %<auto%> in lambda parameter declaration "
17179 "only available with "
17180 "-std=c++14 or -std=gnu++14");
17182 else if (cxx_dialect < cxx14)
17183 error_at (token->location,
17184 "use of %<auto%> in parameter declaration "
17185 "only available with "
17186 "-std=c++14 or -std=gnu++14");
17187 else if (!flag_concepts)
17188 pedwarn (token->location, 0,
17189 "use of %<auto%> in parameter declaration "
17190 "only available with -fconcepts");
17192 else
17193 type = make_auto ();
17194 break;
17196 case RID_DECLTYPE:
17197 /* Since DR 743, decltype can either be a simple-type-specifier by
17198 itself or begin a nested-name-specifier. Parsing it will replace
17199 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17200 handling below decide what to do. */
17201 cp_parser_decltype (parser);
17202 cp_lexer_set_token_position (parser->lexer, token);
17203 break;
17205 case RID_TYPEOF:
17206 /* Consume the `typeof' token. */
17207 cp_lexer_consume_token (parser->lexer);
17208 /* Parse the operand to `typeof'. */
17209 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17210 /* If it is not already a TYPE, take its type. */
17211 if (!TYPE_P (type))
17212 type = finish_typeof (type);
17214 if (decl_specs)
17215 cp_parser_set_decl_spec_type (decl_specs, type,
17216 token,
17217 /*type_definition_p=*/false);
17219 return type;
17221 case RID_UNDERLYING_TYPE:
17222 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17223 if (decl_specs)
17224 cp_parser_set_decl_spec_type (decl_specs, type,
17225 token,
17226 /*type_definition_p=*/false);
17228 return type;
17230 case RID_BASES:
17231 case RID_DIRECT_BASES:
17232 type = cp_parser_trait_expr (parser, token->keyword);
17233 if (decl_specs)
17234 cp_parser_set_decl_spec_type (decl_specs, type,
17235 token,
17236 /*type_definition_p=*/false);
17237 return type;
17238 default:
17239 break;
17242 /* If token is an already-parsed decltype not followed by ::,
17243 it's a simple-type-specifier. */
17244 if (token->type == CPP_DECLTYPE
17245 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17247 type = saved_checks_value (token->u.tree_check_value);
17248 if (decl_specs)
17250 cp_parser_set_decl_spec_type (decl_specs, type,
17251 token,
17252 /*type_definition_p=*/false);
17253 /* Remember that we are handling a decltype in order to
17254 implement the resolution of DR 1510 when the argument
17255 isn't instantiation dependent. */
17256 decl_specs->decltype_p = true;
17258 cp_lexer_consume_token (parser->lexer);
17259 return type;
17262 /* If the type-specifier was for a built-in type, we're done. */
17263 if (type)
17265 /* Record the type. */
17266 if (decl_specs
17267 && (token->keyword != RID_SIGNED
17268 && token->keyword != RID_UNSIGNED
17269 && token->keyword != RID_SHORT
17270 && token->keyword != RID_LONG))
17271 cp_parser_set_decl_spec_type (decl_specs,
17272 type,
17273 token,
17274 /*type_definition_p=*/false);
17275 if (decl_specs)
17276 decl_specs->any_specifiers_p = true;
17278 /* Consume the token. */
17279 cp_lexer_consume_token (parser->lexer);
17281 if (type == error_mark_node)
17282 return error_mark_node;
17284 /* There is no valid C++ program where a non-template type is
17285 followed by a "<". That usually indicates that the user thought
17286 that the type was a template. */
17287 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17288 token->location);
17290 return TYPE_NAME (type);
17293 /* The type-specifier must be a user-defined type. */
17294 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17296 bool qualified_p;
17297 bool global_p;
17299 /* Don't gobble tokens or issue error messages if this is an
17300 optional type-specifier. */
17301 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17302 cp_parser_parse_tentatively (parser);
17304 token = cp_lexer_peek_token (parser->lexer);
17306 /* Look for the optional `::' operator. */
17307 global_p
17308 = (cp_parser_global_scope_opt (parser,
17309 /*current_scope_valid_p=*/false)
17310 != NULL_TREE);
17311 /* Look for the nested-name specifier. */
17312 qualified_p
17313 = (cp_parser_nested_name_specifier_opt (parser,
17314 /*typename_keyword_p=*/false,
17315 /*check_dependency_p=*/true,
17316 /*type_p=*/false,
17317 /*is_declaration=*/false)
17318 != NULL_TREE);
17319 /* If we have seen a nested-name-specifier, and the next token
17320 is `template', then we are using the template-id production. */
17321 if (parser->scope
17322 && cp_parser_optional_template_keyword (parser))
17324 /* Look for the template-id. */
17325 type = cp_parser_template_id (parser,
17326 /*template_keyword_p=*/true,
17327 /*check_dependency_p=*/true,
17328 none_type,
17329 /*is_declaration=*/false);
17330 /* If the template-id did not name a type, we are out of
17331 luck. */
17332 if (TREE_CODE (type) != TYPE_DECL)
17334 cp_parser_error (parser, "expected template-id for type");
17335 type = NULL_TREE;
17338 /* Otherwise, look for a type-name. */
17339 else
17340 type = cp_parser_type_name (parser);
17341 /* Keep track of all name-lookups performed in class scopes. */
17342 if (type
17343 && !global_p
17344 && !qualified_p
17345 && TREE_CODE (type) == TYPE_DECL
17346 && identifier_p (DECL_NAME (type)))
17347 maybe_note_name_used_in_class (DECL_NAME (type), type);
17348 /* If it didn't work out, we don't have a TYPE. */
17349 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17350 && !cp_parser_parse_definitely (parser))
17351 type = NULL_TREE;
17352 if (!type && cxx_dialect >= cxx17)
17354 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17355 cp_parser_parse_tentatively (parser);
17357 cp_parser_global_scope_opt (parser,
17358 /*current_scope_valid_p=*/false);
17359 cp_parser_nested_name_specifier_opt (parser,
17360 /*typename_keyword_p=*/false,
17361 /*check_dependency_p=*/true,
17362 /*type_p=*/false,
17363 /*is_declaration=*/false);
17364 tree name = cp_parser_identifier (parser);
17365 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17366 && parser->scope != error_mark_node)
17368 tree tmpl = cp_parser_lookup_name (parser, name,
17369 none_type,
17370 /*is_template=*/false,
17371 /*is_namespace=*/false,
17372 /*check_dependency=*/true,
17373 /*ambiguous_decls=*/NULL,
17374 token->location);
17375 if (tmpl && tmpl != error_mark_node
17376 && (DECL_CLASS_TEMPLATE_P (tmpl)
17377 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17378 type = make_template_placeholder (tmpl);
17379 else
17381 type = error_mark_node;
17382 if (!cp_parser_simulate_error (parser))
17383 cp_parser_name_lookup_error (parser, name, tmpl,
17384 NLE_TYPE, token->location);
17387 else
17388 type = error_mark_node;
17390 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17391 && !cp_parser_parse_definitely (parser))
17392 type = NULL_TREE;
17394 if (type && decl_specs)
17395 cp_parser_set_decl_spec_type (decl_specs, type,
17396 token,
17397 /*type_definition_p=*/false);
17400 /* If we didn't get a type-name, issue an error message. */
17401 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17403 cp_parser_error (parser, "expected type-name");
17404 return error_mark_node;
17407 if (type && type != error_mark_node)
17409 /* See if TYPE is an Objective-C type, and if so, parse and
17410 accept any protocol references following it. Do this before
17411 the cp_parser_check_for_invalid_template_id() call, because
17412 Objective-C types can be followed by '<...>' which would
17413 enclose protocol names rather than template arguments, and so
17414 everything is fine. */
17415 if (c_dialect_objc () && !parser->scope
17416 && (objc_is_id (type) || objc_is_class_name (type)))
17418 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17419 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17421 /* Clobber the "unqualified" type previously entered into
17422 DECL_SPECS with the new, improved protocol-qualified version. */
17423 if (decl_specs)
17424 decl_specs->type = qual_type;
17426 return qual_type;
17429 /* There is no valid C++ program where a non-template type is
17430 followed by a "<". That usually indicates that the user
17431 thought that the type was a template. */
17432 cp_parser_check_for_invalid_template_id (parser, type,
17433 none_type,
17434 token->location);
17437 return type;
17440 /* Parse a type-name.
17442 type-name:
17443 class-name
17444 enum-name
17445 typedef-name
17446 simple-template-id [in c++0x]
17448 enum-name:
17449 identifier
17451 typedef-name:
17452 identifier
17454 Concepts:
17456 type-name:
17457 concept-name
17458 partial-concept-id
17460 concept-name:
17461 identifier
17463 Returns a TYPE_DECL for the type. */
17465 static tree
17466 cp_parser_type_name (cp_parser* parser)
17468 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17471 /* See above. */
17472 static tree
17473 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17475 tree type_decl;
17477 /* We can't know yet whether it is a class-name or not. */
17478 cp_parser_parse_tentatively (parser);
17479 /* Try a class-name. */
17480 type_decl = cp_parser_class_name (parser,
17481 typename_keyword_p,
17482 /*template_keyword_p=*/false,
17483 none_type,
17484 /*check_dependency_p=*/true,
17485 /*class_head_p=*/false,
17486 /*is_declaration=*/false);
17487 /* If it's not a class-name, keep looking. */
17488 if (!cp_parser_parse_definitely (parser))
17490 if (cxx_dialect < cxx11)
17491 /* It must be a typedef-name or an enum-name. */
17492 return cp_parser_nonclass_name (parser);
17494 cp_parser_parse_tentatively (parser);
17495 /* It is either a simple-template-id representing an
17496 instantiation of an alias template... */
17497 type_decl = cp_parser_template_id (parser,
17498 /*template_keyword_p=*/false,
17499 /*check_dependency_p=*/true,
17500 none_type,
17501 /*is_declaration=*/false);
17502 /* Note that this must be an instantiation of an alias template
17503 because [temp.names]/6 says:
17505 A template-id that names an alias template specialization
17506 is a type-name.
17508 Whereas [temp.names]/7 says:
17510 A simple-template-id that names a class template
17511 specialization is a class-name.
17513 With concepts, this could also be a partial-concept-id that
17514 declares a non-type template parameter. */
17515 if (type_decl != NULL_TREE
17516 && TREE_CODE (type_decl) == TYPE_DECL
17517 && TYPE_DECL_ALIAS_P (type_decl))
17518 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17519 else if (is_constrained_parameter (type_decl))
17520 /* Don't do anything. */ ;
17521 else
17522 cp_parser_simulate_error (parser);
17524 if (!cp_parser_parse_definitely (parser))
17525 /* ... Or a typedef-name or an enum-name. */
17526 return cp_parser_nonclass_name (parser);
17529 return type_decl;
17532 /* Check if DECL and ARGS can form a constrained-type-specifier.
17533 If ARGS is non-null, we try to form a concept check of the
17534 form DECL<?, ARGS> where ? is a wildcard that matches any
17535 kind of template argument. If ARGS is NULL, then we try to
17536 form a concept check of the form DECL<?>. */
17538 static tree
17539 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17540 tree decl, tree args)
17542 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17544 /* If we a constrained-type-specifier cannot be deduced. */
17545 if (parser->prevent_constrained_type_specifiers)
17546 return NULL_TREE;
17548 /* A constrained type specifier can only be found in an
17549 overload set or as a reference to a template declaration.
17551 FIXME: This might be masking a bug. It's possible that
17552 that the deduction below is causing template specializations
17553 to be formed with the wildcard as an argument. */
17554 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17555 return NULL_TREE;
17557 /* Try to build a call expression that evaluates the
17558 concept. This can fail if the overload set refers
17559 only to non-templates. */
17560 tree placeholder = build_nt (WILDCARD_DECL);
17561 tree check = build_concept_check (decl, placeholder, args);
17562 if (check == error_mark_node)
17563 return NULL_TREE;
17565 /* Deduce the checked constraint and the prototype parameter.
17567 FIXME: In certain cases, failure to deduce should be a
17568 diagnosable error. */
17569 tree conc;
17570 tree proto;
17571 if (!deduce_constrained_parameter (check, conc, proto))
17572 return NULL_TREE;
17574 /* In template parameter scope, this results in a constrained
17575 parameter. Return a descriptor of that parm. */
17576 if (processing_template_parmlist)
17577 return build_constrained_parameter (conc, proto, args);
17579 /* In a parameter-declaration-clause, constrained-type
17580 specifiers result in invented template parameters. */
17581 if (parser->auto_is_implicit_function_template_parm_p)
17583 tree x = build_constrained_parameter (conc, proto, args);
17584 return synthesize_implicit_template_parm (parser, x);
17586 else
17588 /* Otherwise, we're in a context where the constrained
17589 type name is deduced and the constraint applies
17590 after deduction. */
17591 return make_constrained_auto (conc, args);
17594 return NULL_TREE;
17597 /* If DECL refers to a concept, return a TYPE_DECL representing
17598 the result of using the constrained type specifier in the
17599 current context. DECL refers to a concept if
17601 - it is an overload set containing a function concept taking a single
17602 type argument, or
17604 - it is a variable concept taking a single type argument. */
17606 static tree
17607 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17609 if (flag_concepts
17610 && (TREE_CODE (decl) == OVERLOAD
17611 || BASELINK_P (decl)
17612 || variable_concept_p (decl)))
17613 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17614 else
17615 return NULL_TREE;
17618 /* Check if DECL and ARGS form a partial-concept-id. If so,
17619 assign ID to the resulting constrained placeholder.
17621 Returns true if the partial-concept-id designates a placeholder
17622 and false otherwise. Note that *id is set to NULL_TREE in
17623 this case. */
17625 static tree
17626 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17628 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17631 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17632 or a concept-name.
17634 enum-name:
17635 identifier
17637 typedef-name:
17638 identifier
17640 concept-name:
17641 identifier
17643 Returns a TYPE_DECL for the type. */
17645 static tree
17646 cp_parser_nonclass_name (cp_parser* parser)
17648 tree type_decl;
17649 tree identifier;
17651 cp_token *token = cp_lexer_peek_token (parser->lexer);
17652 identifier = cp_parser_identifier (parser);
17653 if (identifier == error_mark_node)
17654 return error_mark_node;
17656 /* Look up the type-name. */
17657 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17659 type_decl = strip_using_decl (type_decl);
17661 /* If we found an overload set, then it may refer to a concept-name. */
17662 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17663 type_decl = decl;
17665 if (TREE_CODE (type_decl) != TYPE_DECL
17666 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17668 /* See if this is an Objective-C type. */
17669 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17670 tree type = objc_get_protocol_qualified_type (identifier, protos);
17671 if (type)
17672 type_decl = TYPE_NAME (type);
17675 /* Issue an error if we did not find a type-name. */
17676 if (TREE_CODE (type_decl) != TYPE_DECL
17677 /* In Objective-C, we have the complication that class names are
17678 normally type names and start declarations (eg, the
17679 "NSObject" in "NSObject *object;"), but can be used in an
17680 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17681 is an expression. So, a classname followed by a dot is not a
17682 valid type-name. */
17683 || (objc_is_class_name (TREE_TYPE (type_decl))
17684 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17686 if (!cp_parser_simulate_error (parser))
17687 cp_parser_name_lookup_error (parser, identifier, type_decl,
17688 NLE_TYPE, token->location);
17689 return error_mark_node;
17691 /* Remember that the name was used in the definition of the
17692 current class so that we can check later to see if the
17693 meaning would have been different after the class was
17694 entirely defined. */
17695 else if (type_decl != error_mark_node
17696 && !parser->scope)
17697 maybe_note_name_used_in_class (identifier, type_decl);
17699 return type_decl;
17702 /* Parse an elaborated-type-specifier. Note that the grammar given
17703 here incorporates the resolution to DR68.
17705 elaborated-type-specifier:
17706 class-key :: [opt] nested-name-specifier [opt] identifier
17707 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17708 enum-key :: [opt] nested-name-specifier [opt] identifier
17709 typename :: [opt] nested-name-specifier identifier
17710 typename :: [opt] nested-name-specifier template [opt]
17711 template-id
17713 GNU extension:
17715 elaborated-type-specifier:
17716 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17717 class-key attributes :: [opt] nested-name-specifier [opt]
17718 template [opt] template-id
17719 enum attributes :: [opt] nested-name-specifier [opt] identifier
17721 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17722 declared `friend'. If IS_DECLARATION is TRUE, then this
17723 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17724 something is being declared.
17726 Returns the TYPE specified. */
17728 static tree
17729 cp_parser_elaborated_type_specifier (cp_parser* parser,
17730 bool is_friend,
17731 bool is_declaration)
17733 enum tag_types tag_type;
17734 tree identifier;
17735 tree type = NULL_TREE;
17736 tree attributes = NULL_TREE;
17737 tree globalscope;
17738 cp_token *token = NULL;
17740 /* See if we're looking at the `enum' keyword. */
17741 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17743 /* Consume the `enum' token. */
17744 cp_lexer_consume_token (parser->lexer);
17745 /* Remember that it's an enumeration type. */
17746 tag_type = enum_type;
17747 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17748 enums) is used here. */
17749 cp_token *token = cp_lexer_peek_token (parser->lexer);
17750 if (cp_parser_is_keyword (token, RID_CLASS)
17751 || cp_parser_is_keyword (token, RID_STRUCT))
17753 gcc_rich_location richloc (token->location);
17754 richloc.add_range (input_location);
17755 richloc.add_fixit_remove ();
17756 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17757 "a scoped enum must not use the %qD keyword",
17758 token->u.value);
17759 /* Consume the `struct' or `class' and parse it anyway. */
17760 cp_lexer_consume_token (parser->lexer);
17762 /* Parse the attributes. */
17763 attributes = cp_parser_attributes_opt (parser);
17765 /* Or, it might be `typename'. */
17766 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17767 RID_TYPENAME))
17769 /* Consume the `typename' token. */
17770 cp_lexer_consume_token (parser->lexer);
17771 /* Remember that it's a `typename' type. */
17772 tag_type = typename_type;
17774 /* Otherwise it must be a class-key. */
17775 else
17777 tag_type = cp_parser_class_key (parser);
17778 if (tag_type == none_type)
17779 return error_mark_node;
17780 /* Parse the attributes. */
17781 attributes = cp_parser_attributes_opt (parser);
17784 /* Look for the `::' operator. */
17785 globalscope = cp_parser_global_scope_opt (parser,
17786 /*current_scope_valid_p=*/false);
17787 /* Look for the nested-name-specifier. */
17788 tree nested_name_specifier;
17789 if (tag_type == typename_type && !globalscope)
17791 nested_name_specifier
17792 = cp_parser_nested_name_specifier (parser,
17793 /*typename_keyword_p=*/true,
17794 /*check_dependency_p=*/true,
17795 /*type_p=*/true,
17796 is_declaration);
17797 if (!nested_name_specifier)
17798 return error_mark_node;
17800 else
17801 /* Even though `typename' is not present, the proposed resolution
17802 to Core Issue 180 says that in `class A<T>::B', `B' should be
17803 considered a type-name, even if `A<T>' is dependent. */
17804 nested_name_specifier
17805 = cp_parser_nested_name_specifier_opt (parser,
17806 /*typename_keyword_p=*/true,
17807 /*check_dependency_p=*/true,
17808 /*type_p=*/true,
17809 is_declaration);
17810 /* For everything but enumeration types, consider a template-id.
17811 For an enumeration type, consider only a plain identifier. */
17812 if (tag_type != enum_type)
17814 bool template_p = false;
17815 tree decl;
17817 /* Allow the `template' keyword. */
17818 template_p = cp_parser_optional_template_keyword (parser);
17819 /* If we didn't see `template', we don't know if there's a
17820 template-id or not. */
17821 if (!template_p)
17822 cp_parser_parse_tentatively (parser);
17823 /* Parse the template-id. */
17824 token = cp_lexer_peek_token (parser->lexer);
17825 decl = cp_parser_template_id (parser, template_p,
17826 /*check_dependency_p=*/true,
17827 tag_type,
17828 is_declaration);
17829 /* If we didn't find a template-id, look for an ordinary
17830 identifier. */
17831 if (!template_p && !cp_parser_parse_definitely (parser))
17833 /* We can get here when cp_parser_template_id, called by
17834 cp_parser_class_name with tag_type == none_type, succeeds
17835 and caches a BASELINK. Then, when called again here,
17836 instead of failing and returning an error_mark_node
17837 returns it (see template/typename17.C in C++11).
17838 ??? Could we diagnose this earlier? */
17839 else if (tag_type == typename_type && BASELINK_P (decl))
17841 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17842 type = error_mark_node;
17844 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17845 in effect, then we must assume that, upon instantiation, the
17846 template will correspond to a class. */
17847 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17848 && tag_type == typename_type)
17849 type = make_typename_type (parser->scope, decl,
17850 typename_type,
17851 /*complain=*/tf_error);
17852 /* If the `typename' keyword is in effect and DECL is not a type
17853 decl, then type is non existent. */
17854 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17856 else if (TREE_CODE (decl) == TYPE_DECL)
17858 type = check_elaborated_type_specifier (tag_type, decl,
17859 /*allow_template_p=*/true);
17861 /* If the next token is a semicolon, this must be a specialization,
17862 instantiation, or friend declaration. Check the scope while we
17863 still know whether or not we had a nested-name-specifier. */
17864 if (type != error_mark_node
17865 && !nested_name_specifier && !is_friend
17866 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17867 check_unqualified_spec_or_inst (type, token->location);
17869 else if (decl == error_mark_node)
17870 type = error_mark_node;
17873 if (!type)
17875 token = cp_lexer_peek_token (parser->lexer);
17876 identifier = cp_parser_identifier (parser);
17878 if (identifier == error_mark_node)
17880 parser->scope = NULL_TREE;
17881 return error_mark_node;
17884 /* For a `typename', we needn't call xref_tag. */
17885 if (tag_type == typename_type
17886 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17887 return cp_parser_make_typename_type (parser, identifier,
17888 token->location);
17890 /* Template parameter lists apply only if we are not within a
17891 function parameter list. */
17892 bool template_parm_lists_apply
17893 = parser->num_template_parameter_lists;
17894 if (template_parm_lists_apply)
17895 for (cp_binding_level *s = current_binding_level;
17896 s && s->kind != sk_template_parms;
17897 s = s->level_chain)
17898 if (s->kind == sk_function_parms)
17899 template_parm_lists_apply = false;
17901 /* Look up a qualified name in the usual way. */
17902 if (parser->scope)
17904 tree decl;
17905 tree ambiguous_decls;
17907 decl = cp_parser_lookup_name (parser, identifier,
17908 tag_type,
17909 /*is_template=*/false,
17910 /*is_namespace=*/false,
17911 /*check_dependency=*/true,
17912 &ambiguous_decls,
17913 token->location);
17915 /* If the lookup was ambiguous, an error will already have been
17916 issued. */
17917 if (ambiguous_decls)
17918 return error_mark_node;
17920 /* If we are parsing friend declaration, DECL may be a
17921 TEMPLATE_DECL tree node here. However, we need to check
17922 whether this TEMPLATE_DECL results in valid code. Consider
17923 the following example:
17925 namespace N {
17926 template <class T> class C {};
17928 class X {
17929 template <class T> friend class N::C; // #1, valid code
17931 template <class T> class Y {
17932 friend class N::C; // #2, invalid code
17935 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17936 name lookup of `N::C'. We see that friend declaration must
17937 be template for the code to be valid. Note that
17938 processing_template_decl does not work here since it is
17939 always 1 for the above two cases. */
17941 decl = (cp_parser_maybe_treat_template_as_class
17942 (decl, /*tag_name_p=*/is_friend
17943 && template_parm_lists_apply));
17945 if (TREE_CODE (decl) != TYPE_DECL)
17947 cp_parser_diagnose_invalid_type_name (parser,
17948 identifier,
17949 token->location);
17950 return error_mark_node;
17953 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17955 bool allow_template = (template_parm_lists_apply
17956 || DECL_SELF_REFERENCE_P (decl));
17957 type = check_elaborated_type_specifier (tag_type, decl,
17958 allow_template);
17960 if (type == error_mark_node)
17961 return error_mark_node;
17964 /* Forward declarations of nested types, such as
17966 class C1::C2;
17967 class C1::C2::C3;
17969 are invalid unless all components preceding the final '::'
17970 are complete. If all enclosing types are complete, these
17971 declarations become merely pointless.
17973 Invalid forward declarations of nested types are errors
17974 caught elsewhere in parsing. Those that are pointless arrive
17975 here. */
17977 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17978 && !is_friend && !processing_explicit_instantiation)
17979 warning (0, "declaration %qD does not declare anything", decl);
17981 type = TREE_TYPE (decl);
17983 else
17985 /* An elaborated-type-specifier sometimes introduces a new type and
17986 sometimes names an existing type. Normally, the rule is that it
17987 introduces a new type only if there is not an existing type of
17988 the same name already in scope. For example, given:
17990 struct S {};
17991 void f() { struct S s; }
17993 the `struct S' in the body of `f' is the same `struct S' as in
17994 the global scope; the existing definition is used. However, if
17995 there were no global declaration, this would introduce a new
17996 local class named `S'.
17998 An exception to this rule applies to the following code:
18000 namespace N { struct S; }
18002 Here, the elaborated-type-specifier names a new type
18003 unconditionally; even if there is already an `S' in the
18004 containing scope this declaration names a new type.
18005 This exception only applies if the elaborated-type-specifier
18006 forms the complete declaration:
18008 [class.name]
18010 A declaration consisting solely of `class-key identifier ;' is
18011 either a redeclaration of the name in the current scope or a
18012 forward declaration of the identifier as a class name. It
18013 introduces the name into the current scope.
18015 We are in this situation precisely when the next token is a `;'.
18017 An exception to the exception is that a `friend' declaration does
18018 *not* name a new type; i.e., given:
18020 struct S { friend struct T; };
18022 `T' is not a new type in the scope of `S'.
18024 Also, `new struct S' or `sizeof (struct S)' never results in the
18025 definition of a new type; a new type can only be declared in a
18026 declaration context. */
18028 tag_scope ts;
18029 bool template_p;
18031 if (is_friend)
18032 /* Friends have special name lookup rules. */
18033 ts = ts_within_enclosing_non_class;
18034 else if (is_declaration
18035 && cp_lexer_next_token_is (parser->lexer,
18036 CPP_SEMICOLON))
18037 /* This is a `class-key identifier ;' */
18038 ts = ts_current;
18039 else
18040 ts = ts_global;
18042 template_p =
18043 (template_parm_lists_apply
18044 && (cp_parser_next_token_starts_class_definition_p (parser)
18045 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18046 /* An unqualified name was used to reference this type, so
18047 there were no qualifying templates. */
18048 if (template_parm_lists_apply
18049 && !cp_parser_check_template_parameters (parser,
18050 /*num_templates=*/0,
18051 /*template_id*/false,
18052 token->location,
18053 /*declarator=*/NULL))
18054 return error_mark_node;
18055 type = xref_tag (tag_type, identifier, ts, template_p);
18059 if (type == error_mark_node)
18060 return error_mark_node;
18062 /* Allow attributes on forward declarations of classes. */
18063 if (attributes)
18065 if (TREE_CODE (type) == TYPENAME_TYPE)
18066 warning (OPT_Wattributes,
18067 "attributes ignored on uninstantiated type");
18068 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18069 && ! processing_explicit_instantiation)
18070 warning (OPT_Wattributes,
18071 "attributes ignored on template instantiation");
18072 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18073 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18074 else
18075 warning (OPT_Wattributes,
18076 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18079 if (tag_type != enum_type)
18081 /* Indicate whether this class was declared as a `class' or as a
18082 `struct'. */
18083 if (CLASS_TYPE_P (type))
18084 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18085 cp_parser_check_class_key (tag_type, type);
18088 /* A "<" cannot follow an elaborated type specifier. If that
18089 happens, the user was probably trying to form a template-id. */
18090 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18091 token->location);
18093 return type;
18096 /* Parse an enum-specifier.
18098 enum-specifier:
18099 enum-head { enumerator-list [opt] }
18100 enum-head { enumerator-list , } [C++0x]
18102 enum-head:
18103 enum-key identifier [opt] enum-base [opt]
18104 enum-key nested-name-specifier identifier enum-base [opt]
18106 enum-key:
18107 enum
18108 enum class [C++0x]
18109 enum struct [C++0x]
18111 enum-base: [C++0x]
18112 : type-specifier-seq
18114 opaque-enum-specifier:
18115 enum-key identifier enum-base [opt] ;
18117 GNU Extensions:
18118 enum-key attributes[opt] identifier [opt] enum-base [opt]
18119 { enumerator-list [opt] }attributes[opt]
18120 enum-key attributes[opt] identifier [opt] enum-base [opt]
18121 { enumerator-list, }attributes[opt] [C++0x]
18123 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18124 if the token stream isn't an enum-specifier after all. */
18126 static tree
18127 cp_parser_enum_specifier (cp_parser* parser)
18129 tree identifier;
18130 tree type = NULL_TREE;
18131 tree prev_scope;
18132 tree nested_name_specifier = NULL_TREE;
18133 tree attributes;
18134 bool scoped_enum_p = false;
18135 bool has_underlying_type = false;
18136 bool nested_being_defined = false;
18137 bool new_value_list = false;
18138 bool is_new_type = false;
18139 bool is_unnamed = false;
18140 tree underlying_type = NULL_TREE;
18141 cp_token *type_start_token = NULL;
18142 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18144 parser->colon_corrects_to_scope_p = false;
18146 /* Parse tentatively so that we can back up if we don't find a
18147 enum-specifier. */
18148 cp_parser_parse_tentatively (parser);
18150 /* Caller guarantees that the current token is 'enum', an identifier
18151 possibly follows, and the token after that is an opening brace.
18152 If we don't have an identifier, fabricate an anonymous name for
18153 the enumeration being defined. */
18154 cp_lexer_consume_token (parser->lexer);
18156 /* Parse the "class" or "struct", which indicates a scoped
18157 enumeration type in C++0x. */
18158 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18159 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18161 if (cxx_dialect < cxx11)
18162 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18164 /* Consume the `struct' or `class' token. */
18165 cp_lexer_consume_token (parser->lexer);
18167 scoped_enum_p = true;
18170 attributes = cp_parser_attributes_opt (parser);
18172 /* Clear the qualification. */
18173 parser->scope = NULL_TREE;
18174 parser->qualifying_scope = NULL_TREE;
18175 parser->object_scope = NULL_TREE;
18177 /* Figure out in what scope the declaration is being placed. */
18178 prev_scope = current_scope ();
18180 type_start_token = cp_lexer_peek_token (parser->lexer);
18182 push_deferring_access_checks (dk_no_check);
18183 nested_name_specifier
18184 = cp_parser_nested_name_specifier_opt (parser,
18185 /*typename_keyword_p=*/true,
18186 /*check_dependency_p=*/false,
18187 /*type_p=*/false,
18188 /*is_declaration=*/false);
18190 if (nested_name_specifier)
18192 tree name;
18194 identifier = cp_parser_identifier (parser);
18195 name = cp_parser_lookup_name (parser, identifier,
18196 enum_type,
18197 /*is_template=*/false,
18198 /*is_namespace=*/false,
18199 /*check_dependency=*/true,
18200 /*ambiguous_decls=*/NULL,
18201 input_location);
18202 if (name && name != error_mark_node)
18204 type = TREE_TYPE (name);
18205 if (TREE_CODE (type) == TYPENAME_TYPE)
18207 /* Are template enums allowed in ISO? */
18208 if (template_parm_scope_p ())
18209 pedwarn (type_start_token->location, OPT_Wpedantic,
18210 "%qD is an enumeration template", name);
18211 /* ignore a typename reference, for it will be solved by name
18212 in start_enum. */
18213 type = NULL_TREE;
18216 else if (nested_name_specifier == error_mark_node)
18217 /* We already issued an error. */;
18218 else
18220 error_at (type_start_token->location,
18221 "%qD does not name an enumeration in %qT",
18222 identifier, nested_name_specifier);
18223 nested_name_specifier = error_mark_node;
18226 else
18228 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18229 identifier = cp_parser_identifier (parser);
18230 else
18232 identifier = make_anon_name ();
18233 is_unnamed = true;
18234 if (scoped_enum_p)
18235 error_at (type_start_token->location,
18236 "unnamed scoped enum is not allowed");
18239 pop_deferring_access_checks ();
18241 /* Check for the `:' that denotes a specified underlying type in C++0x.
18242 Note that a ':' could also indicate a bitfield width, however. */
18243 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18245 cp_decl_specifier_seq type_specifiers;
18247 /* Consume the `:'. */
18248 cp_lexer_consume_token (parser->lexer);
18250 /* Parse the type-specifier-seq. */
18251 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18252 /*is_trailing_return=*/false,
18253 &type_specifiers);
18255 /* At this point this is surely not elaborated type specifier. */
18256 if (!cp_parser_parse_definitely (parser))
18257 return NULL_TREE;
18259 if (cxx_dialect < cxx11)
18260 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18262 has_underlying_type = true;
18264 /* If that didn't work, stop. */
18265 if (type_specifiers.type != error_mark_node)
18267 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18268 /*initialized=*/0, NULL);
18269 if (underlying_type == error_mark_node
18270 || check_for_bare_parameter_packs (underlying_type))
18271 underlying_type = NULL_TREE;
18275 /* Look for the `{' but don't consume it yet. */
18276 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18278 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18280 cp_parser_error (parser, "expected %<{%>");
18281 if (has_underlying_type)
18283 type = NULL_TREE;
18284 goto out;
18287 /* An opaque-enum-specifier must have a ';' here. */
18288 if ((scoped_enum_p || underlying_type)
18289 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18291 cp_parser_error (parser, "expected %<;%> or %<{%>");
18292 if (has_underlying_type)
18294 type = NULL_TREE;
18295 goto out;
18300 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18301 return NULL_TREE;
18303 if (nested_name_specifier)
18305 if (CLASS_TYPE_P (nested_name_specifier))
18307 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18308 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18309 push_scope (nested_name_specifier);
18311 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18313 push_nested_namespace (nested_name_specifier);
18317 /* Issue an error message if type-definitions are forbidden here. */
18318 if (!cp_parser_check_type_definition (parser))
18319 type = error_mark_node;
18320 else
18321 /* Create the new type. We do this before consuming the opening
18322 brace so the enum will be recorded as being on the line of its
18323 tag (or the 'enum' keyword, if there is no tag). */
18324 type = start_enum (identifier, type, underlying_type,
18325 attributes, scoped_enum_p, &is_new_type);
18327 /* If the next token is not '{' it is an opaque-enum-specifier or an
18328 elaborated-type-specifier. */
18329 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18331 timevar_push (TV_PARSE_ENUM);
18332 if (nested_name_specifier
18333 && nested_name_specifier != error_mark_node)
18335 /* The following catches invalid code such as:
18336 enum class S<int>::E { A, B, C }; */
18337 if (!processing_specialization
18338 && CLASS_TYPE_P (nested_name_specifier)
18339 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18340 error_at (type_start_token->location, "cannot add an enumerator "
18341 "list to a template instantiation");
18343 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18345 error_at (type_start_token->location,
18346 "%<%T::%E%> has not been declared",
18347 TYPE_CONTEXT (nested_name_specifier),
18348 nested_name_specifier);
18349 type = error_mark_node;
18351 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18352 && !CLASS_TYPE_P (nested_name_specifier))
18354 error_at (type_start_token->location, "nested name specifier "
18355 "%qT for enum declaration does not name a class "
18356 "or namespace", nested_name_specifier);
18357 type = error_mark_node;
18359 /* If that scope does not contain the scope in which the
18360 class was originally declared, the program is invalid. */
18361 else if (prev_scope && !is_ancestor (prev_scope,
18362 nested_name_specifier))
18364 if (at_namespace_scope_p ())
18365 error_at (type_start_token->location,
18366 "declaration of %qD in namespace %qD which does not "
18367 "enclose %qD",
18368 type, prev_scope, nested_name_specifier);
18369 else
18370 error_at (type_start_token->location,
18371 "declaration of %qD in %qD which does not "
18372 "enclose %qD",
18373 type, prev_scope, nested_name_specifier);
18374 type = error_mark_node;
18376 /* If that scope is the scope where the declaration is being placed
18377 the program is invalid. */
18378 else if (CLASS_TYPE_P (nested_name_specifier)
18379 && CLASS_TYPE_P (prev_scope)
18380 && same_type_p (nested_name_specifier, prev_scope))
18382 permerror (type_start_token->location,
18383 "extra qualification not allowed");
18384 nested_name_specifier = NULL_TREE;
18388 if (scoped_enum_p)
18389 begin_scope (sk_scoped_enum, type);
18391 /* Consume the opening brace. */
18392 matching_braces braces;
18393 braces.consume_open (parser);
18395 if (type == error_mark_node)
18396 ; /* Nothing to add */
18397 else if (OPAQUE_ENUM_P (type)
18398 || (cxx_dialect > cxx98 && processing_specialization))
18400 new_value_list = true;
18401 SET_OPAQUE_ENUM_P (type, false);
18402 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18404 else
18406 error_at (type_start_token->location,
18407 "multiple definition of %q#T", type);
18408 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18409 "previous definition here");
18410 type = error_mark_node;
18413 if (type == error_mark_node)
18414 cp_parser_skip_to_end_of_block_or_statement (parser);
18415 /* If the next token is not '}', then there are some enumerators. */
18416 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18418 if (is_unnamed && !scoped_enum_p)
18419 pedwarn (type_start_token->location, OPT_Wpedantic,
18420 "ISO C++ forbids empty unnamed enum");
18422 else
18423 cp_parser_enumerator_list (parser, type);
18425 /* Consume the final '}'. */
18426 braces.require_close (parser);
18428 if (scoped_enum_p)
18429 finish_scope ();
18430 timevar_pop (TV_PARSE_ENUM);
18432 else
18434 /* If a ';' follows, then it is an opaque-enum-specifier
18435 and additional restrictions apply. */
18436 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18438 if (is_unnamed)
18439 error_at (type_start_token->location,
18440 "opaque-enum-specifier without name");
18441 else if (nested_name_specifier)
18442 error_at (type_start_token->location,
18443 "opaque-enum-specifier must use a simple identifier");
18447 /* Look for trailing attributes to apply to this enumeration, and
18448 apply them if appropriate. */
18449 if (cp_parser_allow_gnu_extensions_p (parser))
18451 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18452 cplus_decl_attributes (&type,
18453 trailing_attr,
18454 (int) ATTR_FLAG_TYPE_IN_PLACE);
18457 /* Finish up the enumeration. */
18458 if (type != error_mark_node)
18460 if (new_value_list)
18461 finish_enum_value_list (type);
18462 if (is_new_type)
18463 finish_enum (type);
18466 if (nested_name_specifier)
18468 if (CLASS_TYPE_P (nested_name_specifier))
18470 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18471 pop_scope (nested_name_specifier);
18473 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18475 pop_nested_namespace (nested_name_specifier);
18478 out:
18479 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18480 return type;
18483 /* Parse an enumerator-list. The enumerators all have the indicated
18484 TYPE.
18486 enumerator-list:
18487 enumerator-definition
18488 enumerator-list , enumerator-definition */
18490 static void
18491 cp_parser_enumerator_list (cp_parser* parser, tree type)
18493 while (true)
18495 /* Parse an enumerator-definition. */
18496 cp_parser_enumerator_definition (parser, type);
18498 /* If the next token is not a ',', we've reached the end of
18499 the list. */
18500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18501 break;
18502 /* Otherwise, consume the `,' and keep going. */
18503 cp_lexer_consume_token (parser->lexer);
18504 /* If the next token is a `}', there is a trailing comma. */
18505 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18507 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18508 pedwarn (input_location, OPT_Wpedantic,
18509 "comma at end of enumerator list");
18510 break;
18515 /* Parse an enumerator-definition. The enumerator has the indicated
18516 TYPE.
18518 enumerator-definition:
18519 enumerator
18520 enumerator = constant-expression
18522 enumerator:
18523 identifier
18525 GNU Extensions:
18527 enumerator-definition:
18528 enumerator attributes [opt]
18529 enumerator attributes [opt] = constant-expression */
18531 static void
18532 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18534 tree identifier;
18535 tree value;
18536 location_t loc;
18538 /* Save the input location because we are interested in the location
18539 of the identifier and not the location of the explicit value. */
18540 loc = cp_lexer_peek_token (parser->lexer)->location;
18542 /* Look for the identifier. */
18543 identifier = cp_parser_identifier (parser);
18544 if (identifier == error_mark_node)
18545 return;
18547 /* Parse any specified attributes. */
18548 tree attrs = cp_parser_attributes_opt (parser);
18550 /* If the next token is an '=', then there is an explicit value. */
18551 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18553 /* Consume the `=' token. */
18554 cp_lexer_consume_token (parser->lexer);
18555 /* Parse the value. */
18556 value = cp_parser_constant_expression (parser);
18558 else
18559 value = NULL_TREE;
18561 /* If we are processing a template, make sure the initializer of the
18562 enumerator doesn't contain any bare template parameter pack. */
18563 if (check_for_bare_parameter_packs (value))
18564 value = error_mark_node;
18566 /* Create the enumerator. */
18567 build_enumerator (identifier, value, type, attrs, loc);
18570 /* Parse a namespace-name.
18572 namespace-name:
18573 original-namespace-name
18574 namespace-alias
18576 Returns the NAMESPACE_DECL for the namespace. */
18578 static tree
18579 cp_parser_namespace_name (cp_parser* parser)
18581 tree identifier;
18582 tree namespace_decl;
18584 cp_token *token = cp_lexer_peek_token (parser->lexer);
18586 /* Get the name of the namespace. */
18587 identifier = cp_parser_identifier (parser);
18588 if (identifier == error_mark_node)
18589 return error_mark_node;
18591 /* Look up the identifier in the currently active scope. Look only
18592 for namespaces, due to:
18594 [basic.lookup.udir]
18596 When looking up a namespace-name in a using-directive or alias
18597 definition, only namespace names are considered.
18599 And:
18601 [basic.lookup.qual]
18603 During the lookup of a name preceding the :: scope resolution
18604 operator, object, function, and enumerator names are ignored.
18606 (Note that cp_parser_qualifying_entity only calls this
18607 function if the token after the name is the scope resolution
18608 operator.) */
18609 namespace_decl = cp_parser_lookup_name (parser, identifier,
18610 none_type,
18611 /*is_template=*/false,
18612 /*is_namespace=*/true,
18613 /*check_dependency=*/true,
18614 /*ambiguous_decls=*/NULL,
18615 token->location);
18616 /* If it's not a namespace, issue an error. */
18617 if (namespace_decl == error_mark_node
18618 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18620 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18622 auto_diagnostic_group d;
18623 error_at (token->location, "%qD is not a namespace-name", identifier);
18624 if (namespace_decl == error_mark_node
18625 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18626 suggest_alternative_in_explicit_scope (token->location, identifier,
18627 parser->scope);
18629 cp_parser_error (parser, "expected namespace-name");
18630 namespace_decl = error_mark_node;
18633 return namespace_decl;
18636 /* Parse a namespace-definition.
18638 namespace-definition:
18639 named-namespace-definition
18640 unnamed-namespace-definition
18642 named-namespace-definition:
18643 original-namespace-definition
18644 extension-namespace-definition
18646 original-namespace-definition:
18647 namespace identifier { namespace-body }
18649 extension-namespace-definition:
18650 namespace original-namespace-name { namespace-body }
18652 unnamed-namespace-definition:
18653 namespace { namespace-body } */
18655 static void
18656 cp_parser_namespace_definition (cp_parser* parser)
18658 tree identifier;
18659 int nested_definition_count = 0;
18661 cp_ensure_no_omp_declare_simd (parser);
18662 cp_ensure_no_oacc_routine (parser);
18664 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18666 if (is_inline)
18668 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18669 cp_lexer_consume_token (parser->lexer);
18672 /* Look for the `namespace' keyword. */
18673 cp_token* token
18674 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18676 /* Parse any specified attributes before the identifier. */
18677 tree attribs = cp_parser_attributes_opt (parser);
18679 for (;;)
18681 identifier = NULL_TREE;
18683 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18685 identifier = cp_parser_identifier (parser);
18687 if (cp_next_tokens_can_be_std_attribute_p (parser))
18688 pedwarn (input_location, OPT_Wpedantic,
18689 "standard attributes on namespaces must precede "
18690 "the namespace name");
18692 /* Parse any attributes specified after the identifier. */
18693 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18697 break;
18699 if (!nested_definition_count && cxx_dialect < cxx17)
18700 pedwarn (input_location, OPT_Wpedantic,
18701 "nested namespace definitions only available with "
18702 "-std=c++17 or -std=gnu++17");
18704 /* Nested namespace names can create new namespaces (unlike
18705 other qualified-ids). */
18706 if (int count = identifier ? push_namespace (identifier) : 0)
18707 nested_definition_count += count;
18708 else
18709 cp_parser_error (parser, "nested namespace name required");
18710 cp_lexer_consume_token (parser->lexer);
18713 if (nested_definition_count && !identifier)
18714 cp_parser_error (parser, "namespace name required");
18716 if (nested_definition_count && attribs)
18717 error_at (token->location,
18718 "a nested namespace definition cannot have attributes");
18719 if (nested_definition_count && is_inline)
18720 error_at (token->location,
18721 "a nested namespace definition cannot be inline");
18723 /* Start the namespace. */
18724 nested_definition_count += push_namespace (identifier, is_inline);
18726 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18728 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18730 /* Look for the `{' to validate starting the namespace. */
18731 matching_braces braces;
18732 if (braces.require_open (parser))
18734 /* Parse the body of the namespace. */
18735 cp_parser_namespace_body (parser);
18737 /* Look for the final `}'. */
18738 braces.require_close (parser);
18741 if (has_visibility)
18742 pop_visibility (1);
18744 /* Pop the nested namespace definitions. */
18745 while (nested_definition_count--)
18746 pop_namespace ();
18749 /* Parse a namespace-body.
18751 namespace-body:
18752 declaration-seq [opt] */
18754 static void
18755 cp_parser_namespace_body (cp_parser* parser)
18757 cp_parser_declaration_seq_opt (parser);
18760 /* Parse a namespace-alias-definition.
18762 namespace-alias-definition:
18763 namespace identifier = qualified-namespace-specifier ; */
18765 static void
18766 cp_parser_namespace_alias_definition (cp_parser* parser)
18768 tree identifier;
18769 tree namespace_specifier;
18771 cp_token *token = cp_lexer_peek_token (parser->lexer);
18773 /* Look for the `namespace' keyword. */
18774 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18775 /* Look for the identifier. */
18776 identifier = cp_parser_identifier (parser);
18777 if (identifier == error_mark_node)
18778 return;
18779 /* Look for the `=' token. */
18780 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18781 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18783 error_at (token->location, "%<namespace%> definition is not allowed here");
18784 /* Skip the definition. */
18785 cp_lexer_consume_token (parser->lexer);
18786 if (cp_parser_skip_to_closing_brace (parser))
18787 cp_lexer_consume_token (parser->lexer);
18788 return;
18790 cp_parser_require (parser, CPP_EQ, RT_EQ);
18791 /* Look for the qualified-namespace-specifier. */
18792 namespace_specifier
18793 = cp_parser_qualified_namespace_specifier (parser);
18794 /* Look for the `;' token. */
18795 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18797 /* Register the alias in the symbol table. */
18798 do_namespace_alias (identifier, namespace_specifier);
18801 /* Parse a qualified-namespace-specifier.
18803 qualified-namespace-specifier:
18804 :: [opt] nested-name-specifier [opt] namespace-name
18806 Returns a NAMESPACE_DECL corresponding to the specified
18807 namespace. */
18809 static tree
18810 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18812 /* Look for the optional `::'. */
18813 cp_parser_global_scope_opt (parser,
18814 /*current_scope_valid_p=*/false);
18816 /* Look for the optional nested-name-specifier. */
18817 cp_parser_nested_name_specifier_opt (parser,
18818 /*typename_keyword_p=*/false,
18819 /*check_dependency_p=*/true,
18820 /*type_p=*/false,
18821 /*is_declaration=*/true);
18823 return cp_parser_namespace_name (parser);
18826 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18827 access declaration.
18829 using-declaration:
18830 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18831 using :: unqualified-id ;
18833 access-declaration:
18834 qualified-id ;
18838 static bool
18839 cp_parser_using_declaration (cp_parser* parser,
18840 bool access_declaration_p)
18842 cp_token *token;
18843 bool typename_p = false;
18844 bool global_scope_p;
18845 tree decl;
18846 tree identifier;
18847 tree qscope;
18848 int oldcount = errorcount;
18849 cp_token *diag_token = NULL;
18851 if (access_declaration_p)
18853 diag_token = cp_lexer_peek_token (parser->lexer);
18854 cp_parser_parse_tentatively (parser);
18856 else
18858 /* Look for the `using' keyword. */
18859 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18861 again:
18862 /* Peek at the next token. */
18863 token = cp_lexer_peek_token (parser->lexer);
18864 /* See if it's `typename'. */
18865 if (token->keyword == RID_TYPENAME)
18867 /* Remember that we've seen it. */
18868 typename_p = true;
18869 /* Consume the `typename' token. */
18870 cp_lexer_consume_token (parser->lexer);
18874 /* Look for the optional global scope qualification. */
18875 global_scope_p
18876 = (cp_parser_global_scope_opt (parser,
18877 /*current_scope_valid_p=*/false)
18878 != NULL_TREE);
18880 /* If we saw `typename', or didn't see `::', then there must be a
18881 nested-name-specifier present. */
18882 if (typename_p || !global_scope_p)
18884 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18885 /*check_dependency_p=*/true,
18886 /*type_p=*/false,
18887 /*is_declaration=*/true);
18888 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18890 cp_parser_skip_to_end_of_block_or_statement (parser);
18891 return false;
18894 /* Otherwise, we could be in either of the two productions. In that
18895 case, treat the nested-name-specifier as optional. */
18896 else
18897 qscope = cp_parser_nested_name_specifier_opt (parser,
18898 /*typename_keyword_p=*/false,
18899 /*check_dependency_p=*/true,
18900 /*type_p=*/false,
18901 /*is_declaration=*/true);
18902 if (!qscope)
18903 qscope = global_namespace;
18904 else if (UNSCOPED_ENUM_P (qscope))
18905 qscope = CP_TYPE_CONTEXT (qscope);
18907 if (access_declaration_p && cp_parser_error_occurred (parser))
18908 /* Something has already gone wrong; there's no need to parse
18909 further. Since an error has occurred, the return value of
18910 cp_parser_parse_definitely will be false, as required. */
18911 return cp_parser_parse_definitely (parser);
18913 token = cp_lexer_peek_token (parser->lexer);
18914 /* Parse the unqualified-id. */
18915 identifier = cp_parser_unqualified_id (parser,
18916 /*template_keyword_p=*/false,
18917 /*check_dependency_p=*/true,
18918 /*declarator_p=*/true,
18919 /*optional_p=*/false);
18921 if (access_declaration_p)
18923 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18924 cp_parser_simulate_error (parser);
18925 if (!cp_parser_parse_definitely (parser))
18926 return false;
18928 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18930 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18931 if (cxx_dialect < cxx17
18932 && !in_system_header_at (ell->location))
18933 pedwarn (ell->location, 0,
18934 "pack expansion in using-declaration only available "
18935 "with -std=c++17 or -std=gnu++17");
18936 qscope = make_pack_expansion (qscope);
18939 /* The function we call to handle a using-declaration is different
18940 depending on what scope we are in. */
18941 if (qscope == error_mark_node || identifier == error_mark_node)
18943 else if (!identifier_p (identifier)
18944 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18945 /* [namespace.udecl]
18947 A using declaration shall not name a template-id. */
18948 error_at (token->location,
18949 "a template-id may not appear in a using-declaration");
18950 else
18952 if (at_class_scope_p ())
18954 /* Create the USING_DECL. */
18955 decl = do_class_using_decl (qscope, identifier);
18957 if (decl && typename_p)
18958 USING_DECL_TYPENAME_P (decl) = 1;
18960 if (check_for_bare_parameter_packs (decl))
18962 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18963 return false;
18965 else
18966 /* Add it to the list of members in this class. */
18967 finish_member_declaration (decl);
18969 else
18971 decl = cp_parser_lookup_name_simple (parser,
18972 identifier,
18973 token->location);
18974 if (decl == error_mark_node)
18975 cp_parser_name_lookup_error (parser, identifier,
18976 decl, NLE_NULL,
18977 token->location);
18978 else if (check_for_bare_parameter_packs (decl))
18980 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18981 return false;
18983 else if (!at_namespace_scope_p ())
18984 finish_local_using_decl (decl, qscope, identifier);
18985 else
18986 finish_namespace_using_decl (decl, qscope, identifier);
18990 if (!access_declaration_p
18991 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18993 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18994 if (cxx_dialect < cxx17)
18995 pedwarn (comma->location, 0,
18996 "comma-separated list in using-declaration only available "
18997 "with -std=c++17 or -std=gnu++17");
18998 goto again;
19001 /* Look for the final `;'. */
19002 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19004 if (access_declaration_p && errorcount == oldcount)
19005 warning_at (diag_token->location, OPT_Wdeprecated,
19006 "access declarations are deprecated "
19007 "in favour of using-declarations; "
19008 "suggestion: add the %<using%> keyword");
19010 return true;
19013 /* Parse an alias-declaration.
19015 alias-declaration:
19016 using identifier attribute-specifier-seq [opt] = type-id */
19018 static tree
19019 cp_parser_alias_declaration (cp_parser* parser)
19021 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19022 location_t id_location, type_location;
19023 cp_declarator *declarator;
19024 cp_decl_specifier_seq decl_specs;
19025 bool member_p;
19026 const char *saved_message = NULL;
19028 /* Look for the `using' keyword. */
19029 cp_token *using_token
19030 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19031 if (using_token == NULL)
19032 return error_mark_node;
19034 id_location = cp_lexer_peek_token (parser->lexer)->location;
19035 id = cp_parser_identifier (parser);
19036 if (id == error_mark_node)
19037 return error_mark_node;
19039 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19040 attributes = cp_parser_attributes_opt (parser);
19041 if (attributes == error_mark_node)
19042 return error_mark_node;
19044 cp_parser_require (parser, CPP_EQ, RT_EQ);
19046 if (cp_parser_error_occurred (parser))
19047 return error_mark_node;
19049 cp_parser_commit_to_tentative_parse (parser);
19051 /* Now we are going to parse the type-id of the declaration. */
19054 [dcl.type]/3 says:
19056 "A type-specifier-seq shall not define a class or enumeration
19057 unless it appears in the type-id of an alias-declaration (7.1.3) that
19058 is not the declaration of a template-declaration."
19060 In other words, if we currently are in an alias template, the
19061 type-id should not define a type.
19063 So let's set parser->type_definition_forbidden_message in that
19064 case; cp_parser_check_type_definition (called by
19065 cp_parser_class_specifier) will then emit an error if a type is
19066 defined in the type-id. */
19067 if (parser->num_template_parameter_lists)
19069 saved_message = parser->type_definition_forbidden_message;
19070 parser->type_definition_forbidden_message =
19071 G_("types may not be defined in alias template declarations");
19074 type = cp_parser_type_id (parser, &type_location);
19076 /* Restore the error message if need be. */
19077 if (parser->num_template_parameter_lists)
19078 parser->type_definition_forbidden_message = saved_message;
19080 if (type == error_mark_node
19081 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19083 cp_parser_skip_to_end_of_block_or_statement (parser);
19084 return error_mark_node;
19087 /* A typedef-name can also be introduced by an alias-declaration. The
19088 identifier following the using keyword becomes a typedef-name. It has
19089 the same semantics as if it were introduced by the typedef
19090 specifier. In particular, it does not define a new type and it shall
19091 not appear in the type-id. */
19093 clear_decl_specs (&decl_specs);
19094 decl_specs.type = type;
19095 if (attributes != NULL_TREE)
19097 decl_specs.attributes = attributes;
19098 set_and_check_decl_spec_loc (&decl_specs,
19099 ds_attribute,
19100 attrs_token);
19102 set_and_check_decl_spec_loc (&decl_specs,
19103 ds_typedef,
19104 using_token);
19105 set_and_check_decl_spec_loc (&decl_specs,
19106 ds_alias,
19107 using_token);
19108 decl_specs.locations[ds_type_spec] = type_location;
19110 if (parser->num_template_parameter_lists
19111 && !cp_parser_check_template_parameters (parser,
19112 /*num_templates=*/0,
19113 /*template_id*/false,
19114 id_location,
19115 /*declarator=*/NULL))
19116 return error_mark_node;
19118 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19119 declarator->id_loc = id_location;
19121 member_p = at_class_scope_p ();
19122 if (member_p)
19123 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19124 NULL_TREE, attributes);
19125 else
19126 decl = start_decl (declarator, &decl_specs, 0,
19127 attributes, NULL_TREE, &pushed_scope);
19128 if (decl == error_mark_node)
19129 return decl;
19131 // Attach constraints to the alias declaration.
19132 if (flag_concepts && current_template_parms)
19134 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19135 tree constr = build_constraints (reqs, NULL_TREE);
19136 set_constraints (decl, constr);
19139 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19141 if (pushed_scope)
19142 pop_scope (pushed_scope);
19144 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19145 added into the symbol table; otherwise, return the TYPE_DECL. */
19146 if (DECL_LANG_SPECIFIC (decl)
19147 && DECL_TEMPLATE_INFO (decl)
19148 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19150 decl = DECL_TI_TEMPLATE (decl);
19151 if (member_p)
19152 check_member_template (decl);
19155 return decl;
19158 /* Parse a using-directive.
19160 using-directive:
19161 using namespace :: [opt] nested-name-specifier [opt]
19162 namespace-name ; */
19164 static void
19165 cp_parser_using_directive (cp_parser* parser)
19167 tree namespace_decl;
19168 tree attribs;
19170 /* Look for the `using' keyword. */
19171 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19172 /* And the `namespace' keyword. */
19173 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19174 /* Look for the optional `::' operator. */
19175 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19176 /* And the optional nested-name-specifier. */
19177 cp_parser_nested_name_specifier_opt (parser,
19178 /*typename_keyword_p=*/false,
19179 /*check_dependency_p=*/true,
19180 /*type_p=*/false,
19181 /*is_declaration=*/true);
19182 /* Get the namespace being used. */
19183 namespace_decl = cp_parser_namespace_name (parser);
19184 /* And any specified attributes. */
19185 attribs = cp_parser_attributes_opt (parser);
19187 /* Update the symbol table. */
19188 if (namespace_bindings_p ())
19189 finish_namespace_using_directive (namespace_decl, attribs);
19190 else
19191 finish_local_using_directive (namespace_decl, attribs);
19193 /* Look for the final `;'. */
19194 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19197 /* Parse an asm-definition.
19199 asm-definition:
19200 asm ( string-literal ) ;
19202 GNU Extension:
19204 asm-definition:
19205 asm volatile [opt] ( string-literal ) ;
19206 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19207 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19208 : asm-operand-list [opt] ) ;
19209 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19210 : asm-operand-list [opt]
19211 : asm-clobber-list [opt] ) ;
19212 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19213 : asm-clobber-list [opt]
19214 : asm-goto-list ) ; */
19216 static void
19217 cp_parser_asm_definition (cp_parser* parser)
19219 tree string;
19220 tree outputs = NULL_TREE;
19221 tree inputs = NULL_TREE;
19222 tree clobbers = NULL_TREE;
19223 tree labels = NULL_TREE;
19224 tree asm_stmt;
19225 bool volatile_p = false;
19226 bool extended_p = false;
19227 bool invalid_inputs_p = false;
19228 bool invalid_outputs_p = false;
19229 bool goto_p = false;
19230 required_token missing = RT_NONE;
19232 /* Look for the `asm' keyword. */
19233 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19235 if (parser->in_function_body
19236 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19238 error ("%<asm%> in %<constexpr%> function");
19239 cp_function_chain->invalid_constexpr = true;
19242 /* See if the next token is `volatile'. */
19243 if (cp_parser_allow_gnu_extensions_p (parser)
19244 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19246 /* Remember that we saw the `volatile' keyword. */
19247 volatile_p = true;
19248 /* Consume the token. */
19249 cp_lexer_consume_token (parser->lexer);
19251 if (cp_parser_allow_gnu_extensions_p (parser)
19252 && parser->in_function_body
19253 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19255 /* Remember that we saw the `goto' keyword. */
19256 goto_p = true;
19257 /* Consume the token. */
19258 cp_lexer_consume_token (parser->lexer);
19260 /* Look for the opening `('. */
19261 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19262 return;
19263 /* Look for the string. */
19264 string = cp_parser_string_literal (parser, false, false);
19265 if (string == error_mark_node)
19267 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19268 /*consume_paren=*/true);
19269 return;
19272 /* If we're allowing GNU extensions, check for the extended assembly
19273 syntax. Unfortunately, the `:' tokens need not be separated by
19274 a space in C, and so, for compatibility, we tolerate that here
19275 too. Doing that means that we have to treat the `::' operator as
19276 two `:' tokens. */
19277 if (cp_parser_allow_gnu_extensions_p (parser)
19278 && parser->in_function_body
19279 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19280 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19282 bool inputs_p = false;
19283 bool clobbers_p = false;
19284 bool labels_p = false;
19286 /* The extended syntax was used. */
19287 extended_p = true;
19289 /* Look for outputs. */
19290 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19292 /* Consume the `:'. */
19293 cp_lexer_consume_token (parser->lexer);
19294 /* Parse the output-operands. */
19295 if (cp_lexer_next_token_is_not (parser->lexer,
19296 CPP_COLON)
19297 && cp_lexer_next_token_is_not (parser->lexer,
19298 CPP_SCOPE)
19299 && cp_lexer_next_token_is_not (parser->lexer,
19300 CPP_CLOSE_PAREN)
19301 && !goto_p)
19303 outputs = cp_parser_asm_operand_list (parser);
19304 if (outputs == error_mark_node)
19305 invalid_outputs_p = true;
19308 /* If the next token is `::', there are no outputs, and the
19309 next token is the beginning of the inputs. */
19310 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19311 /* The inputs are coming next. */
19312 inputs_p = true;
19314 /* Look for inputs. */
19315 if (inputs_p
19316 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19318 /* Consume the `:' or `::'. */
19319 cp_lexer_consume_token (parser->lexer);
19320 /* Parse the output-operands. */
19321 if (cp_lexer_next_token_is_not (parser->lexer,
19322 CPP_COLON)
19323 && cp_lexer_next_token_is_not (parser->lexer,
19324 CPP_SCOPE)
19325 && cp_lexer_next_token_is_not (parser->lexer,
19326 CPP_CLOSE_PAREN))
19328 inputs = cp_parser_asm_operand_list (parser);
19329 if (inputs == error_mark_node)
19330 invalid_inputs_p = true;
19333 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19334 /* The clobbers are coming next. */
19335 clobbers_p = true;
19337 /* Look for clobbers. */
19338 if (clobbers_p
19339 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19341 clobbers_p = true;
19342 /* Consume the `:' or `::'. */
19343 cp_lexer_consume_token (parser->lexer);
19344 /* Parse the clobbers. */
19345 if (cp_lexer_next_token_is_not (parser->lexer,
19346 CPP_COLON)
19347 && cp_lexer_next_token_is_not (parser->lexer,
19348 CPP_CLOSE_PAREN))
19349 clobbers = cp_parser_asm_clobber_list (parser);
19351 else if (goto_p
19352 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19353 /* The labels are coming next. */
19354 labels_p = true;
19356 /* Look for labels. */
19357 if (labels_p
19358 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19360 labels_p = true;
19361 /* Consume the `:' or `::'. */
19362 cp_lexer_consume_token (parser->lexer);
19363 /* Parse the labels. */
19364 labels = cp_parser_asm_label_list (parser);
19367 if (goto_p && !labels_p)
19368 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19370 else if (goto_p)
19371 missing = RT_COLON_SCOPE;
19373 /* Look for the closing `)'. */
19374 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19375 missing ? missing : RT_CLOSE_PAREN))
19376 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19377 /*consume_paren=*/true);
19378 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19380 if (!invalid_inputs_p && !invalid_outputs_p)
19382 /* Create the ASM_EXPR. */
19383 if (parser->in_function_body)
19385 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19386 inputs, clobbers, labels);
19387 /* If the extended syntax was not used, mark the ASM_EXPR. */
19388 if (!extended_p)
19390 tree temp = asm_stmt;
19391 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19392 temp = TREE_OPERAND (temp, 0);
19394 ASM_INPUT_P (temp) = 1;
19397 else
19398 symtab->finalize_toplevel_asm (string);
19402 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19403 type that comes from the decl-specifier-seq. */
19405 static tree
19406 strip_declarator_types (tree type, cp_declarator *declarator)
19408 for (cp_declarator *d = declarator; d;)
19409 switch (d->kind)
19411 case cdk_id:
19412 case cdk_decomp:
19413 case cdk_error:
19414 d = NULL;
19415 break;
19417 default:
19418 if (TYPE_PTRMEMFUNC_P (type))
19419 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19420 type = TREE_TYPE (type);
19421 d = d->declarator;
19422 break;
19425 return type;
19428 /* Declarators [gram.dcl.decl] */
19430 /* Parse an init-declarator.
19432 init-declarator:
19433 declarator initializer [opt]
19435 GNU Extension:
19437 init-declarator:
19438 declarator asm-specification [opt] attributes [opt] initializer [opt]
19440 function-definition:
19441 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19442 function-body
19443 decl-specifier-seq [opt] declarator function-try-block
19445 GNU Extension:
19447 function-definition:
19448 __extension__ function-definition
19450 TM Extension:
19452 function-definition:
19453 decl-specifier-seq [opt] declarator function-transaction-block
19455 The DECL_SPECIFIERS apply to this declarator. Returns a
19456 representation of the entity declared. If MEMBER_P is TRUE, then
19457 this declarator appears in a class scope. The new DECL created by
19458 this declarator is returned.
19460 The CHECKS are access checks that should be performed once we know
19461 what entity is being declared (and, therefore, what classes have
19462 befriended it).
19464 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19465 for a function-definition here as well. If the declarator is a
19466 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19467 be TRUE upon return. By that point, the function-definition will
19468 have been completely parsed.
19470 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19471 is FALSE.
19473 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19474 parsed declaration if it is an uninitialized single declarator not followed
19475 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19476 if present, will not be consumed. If returned, this declarator will be
19477 created with SD_INITIALIZED but will not call cp_finish_decl.
19479 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19480 and there is an initializer, the pointed location_t is set to the
19481 location of the '=' or `(', or '{' in C++11 token introducing the
19482 initializer. */
19484 static tree
19485 cp_parser_init_declarator (cp_parser* parser,
19486 cp_decl_specifier_seq *decl_specifiers,
19487 vec<deferred_access_check, va_gc> *checks,
19488 bool function_definition_allowed_p,
19489 bool member_p,
19490 int declares_class_or_enum,
19491 bool* function_definition_p,
19492 tree* maybe_range_for_decl,
19493 location_t* init_loc,
19494 tree* auto_result)
19496 cp_token *token = NULL, *asm_spec_start_token = NULL,
19497 *attributes_start_token = NULL;
19498 cp_declarator *declarator;
19499 tree prefix_attributes;
19500 tree attributes = NULL;
19501 tree asm_specification;
19502 tree initializer;
19503 tree decl = NULL_TREE;
19504 tree scope;
19505 int is_initialized;
19506 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19507 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19508 "(...)". */
19509 enum cpp_ttype initialization_kind;
19510 bool is_direct_init = false;
19511 bool is_non_constant_init;
19512 int ctor_dtor_or_conv_p;
19513 bool friend_p = cp_parser_friend_p (decl_specifiers);
19514 tree pushed_scope = NULL_TREE;
19515 bool range_for_decl_p = false;
19516 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19517 location_t tmp_init_loc = UNKNOWN_LOCATION;
19519 /* Gather the attributes that were provided with the
19520 decl-specifiers. */
19521 prefix_attributes = decl_specifiers->attributes;
19523 /* Assume that this is not the declarator for a function
19524 definition. */
19525 if (function_definition_p)
19526 *function_definition_p = false;
19528 /* Default arguments are only permitted for function parameters. */
19529 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19530 parser->default_arg_ok_p = false;
19532 /* Defer access checks while parsing the declarator; we cannot know
19533 what names are accessible until we know what is being
19534 declared. */
19535 resume_deferring_access_checks ();
19537 token = cp_lexer_peek_token (parser->lexer);
19539 /* Parse the declarator. */
19540 declarator
19541 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19542 &ctor_dtor_or_conv_p,
19543 /*parenthesized_p=*/NULL,
19544 member_p, friend_p);
19545 /* Gather up the deferred checks. */
19546 stop_deferring_access_checks ();
19548 parser->default_arg_ok_p = saved_default_arg_ok_p;
19550 /* If the DECLARATOR was erroneous, there's no need to go
19551 further. */
19552 if (declarator == cp_error_declarator)
19553 return error_mark_node;
19555 /* Check that the number of template-parameter-lists is OK. */
19556 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19557 token->location))
19558 return error_mark_node;
19560 if (declares_class_or_enum & 2)
19561 cp_parser_check_for_definition_in_return_type (declarator,
19562 decl_specifiers->type,
19563 decl_specifiers->locations[ds_type_spec]);
19565 /* Figure out what scope the entity declared by the DECLARATOR is
19566 located in. `grokdeclarator' sometimes changes the scope, so
19567 we compute it now. */
19568 scope = get_scope_of_declarator (declarator);
19570 /* Perform any lookups in the declared type which were thought to be
19571 dependent, but are not in the scope of the declarator. */
19572 decl_specifiers->type
19573 = maybe_update_decl_type (decl_specifiers->type, scope);
19575 /* If we're allowing GNU extensions, look for an
19576 asm-specification. */
19577 if (cp_parser_allow_gnu_extensions_p (parser))
19579 /* Look for an asm-specification. */
19580 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19581 asm_specification = cp_parser_asm_specification_opt (parser);
19583 else
19584 asm_specification = NULL_TREE;
19586 /* Look for attributes. */
19587 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19588 attributes = cp_parser_attributes_opt (parser);
19590 /* Peek at the next token. */
19591 token = cp_lexer_peek_token (parser->lexer);
19593 bool bogus_implicit_tmpl = false;
19595 if (function_declarator_p (declarator))
19597 /* Handle C++17 deduction guides. */
19598 if (!decl_specifiers->type
19599 && ctor_dtor_or_conv_p <= 0
19600 && cxx_dialect >= cxx17)
19602 cp_declarator *id = get_id_declarator (declarator);
19603 tree name = id->u.id.unqualified_name;
19604 parser->scope = id->u.id.qualifying_scope;
19605 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19606 if (tmpl
19607 && (DECL_CLASS_TEMPLATE_P (tmpl)
19608 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19610 id->u.id.unqualified_name = dguide_name (tmpl);
19611 id->u.id.sfk = sfk_deduction_guide;
19612 ctor_dtor_or_conv_p = 1;
19616 /* Check to see if the token indicates the start of a
19617 function-definition. */
19618 if (cp_parser_token_starts_function_definition_p (token))
19620 if (!function_definition_allowed_p)
19622 /* If a function-definition should not appear here, issue an
19623 error message. */
19624 cp_parser_error (parser,
19625 "a function-definition is not allowed here");
19626 return error_mark_node;
19629 location_t func_brace_location
19630 = cp_lexer_peek_token (parser->lexer)->location;
19632 /* Neither attributes nor an asm-specification are allowed
19633 on a function-definition. */
19634 if (asm_specification)
19635 error_at (asm_spec_start_token->location,
19636 "an asm-specification is not allowed "
19637 "on a function-definition");
19638 if (attributes)
19639 error_at (attributes_start_token->location,
19640 "attributes are not allowed "
19641 "on a function-definition");
19642 /* This is a function-definition. */
19643 *function_definition_p = true;
19645 /* Parse the function definition. */
19646 if (member_p)
19647 decl = cp_parser_save_member_function_body (parser,
19648 decl_specifiers,
19649 declarator,
19650 prefix_attributes);
19651 else
19652 decl =
19653 (cp_parser_function_definition_from_specifiers_and_declarator
19654 (parser, decl_specifiers, prefix_attributes, declarator));
19656 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19658 /* This is where the prologue starts... */
19659 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19660 = func_brace_location;
19663 return decl;
19666 else if (parser->fully_implicit_function_template_p)
19668 /* A non-template declaration involving a function parameter list
19669 containing an implicit template parameter will be made into a
19670 template. If the resulting declaration is not going to be an
19671 actual function then finish the template scope here to prevent it.
19672 An error message will be issued once we have a decl to talk about.
19674 FIXME probably we should do type deduction rather than create an
19675 implicit template, but the standard currently doesn't allow it. */
19676 bogus_implicit_tmpl = true;
19677 finish_fully_implicit_template (parser, NULL_TREE);
19680 /* [dcl.dcl]
19682 Only in function declarations for constructors, destructors, type
19683 conversions, and deduction guides can the decl-specifier-seq be omitted.
19685 We explicitly postpone this check past the point where we handle
19686 function-definitions because we tolerate function-definitions
19687 that are missing their return types in some modes. */
19688 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19690 cp_parser_error (parser,
19691 "expected constructor, destructor, or type conversion");
19692 return error_mark_node;
19695 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19696 if (token->type == CPP_EQ
19697 || token->type == CPP_OPEN_PAREN
19698 || token->type == CPP_OPEN_BRACE)
19700 is_initialized = SD_INITIALIZED;
19701 initialization_kind = token->type;
19702 if (maybe_range_for_decl)
19703 *maybe_range_for_decl = error_mark_node;
19704 tmp_init_loc = token->location;
19705 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19706 *init_loc = tmp_init_loc;
19708 if (token->type == CPP_EQ
19709 && function_declarator_p (declarator))
19711 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19712 if (t2->keyword == RID_DEFAULT)
19713 is_initialized = SD_DEFAULTED;
19714 else if (t2->keyword == RID_DELETE)
19715 is_initialized = SD_DELETED;
19718 else
19720 /* If the init-declarator isn't initialized and isn't followed by a
19721 `,' or `;', it's not a valid init-declarator. */
19722 if (token->type != CPP_COMMA
19723 && token->type != CPP_SEMICOLON)
19725 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19726 range_for_decl_p = true;
19727 else
19729 if (!maybe_range_for_decl)
19730 cp_parser_error (parser, "expected initializer");
19731 return error_mark_node;
19734 is_initialized = SD_UNINITIALIZED;
19735 initialization_kind = CPP_EOF;
19738 /* Because start_decl has side-effects, we should only call it if we
19739 know we're going ahead. By this point, we know that we cannot
19740 possibly be looking at any other construct. */
19741 cp_parser_commit_to_tentative_parse (parser);
19743 /* Enter the newly declared entry in the symbol table. If we're
19744 processing a declaration in a class-specifier, we wait until
19745 after processing the initializer. */
19746 if (!member_p)
19748 if (parser->in_unbraced_linkage_specification_p)
19749 decl_specifiers->storage_class = sc_extern;
19750 decl = start_decl (declarator, decl_specifiers,
19751 range_for_decl_p? SD_INITIALIZED : is_initialized,
19752 attributes, prefix_attributes, &pushed_scope);
19753 cp_finalize_omp_declare_simd (parser, decl);
19754 cp_finalize_oacc_routine (parser, decl, false);
19755 /* Adjust location of decl if declarator->id_loc is more appropriate:
19756 set, and decl wasn't merged with another decl, in which case its
19757 location would be different from input_location, and more accurate. */
19758 if (DECL_P (decl)
19759 && declarator->id_loc != UNKNOWN_LOCATION
19760 && DECL_SOURCE_LOCATION (decl) == input_location)
19761 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19763 else if (scope)
19764 /* Enter the SCOPE. That way unqualified names appearing in the
19765 initializer will be looked up in SCOPE. */
19766 pushed_scope = push_scope (scope);
19768 /* Perform deferred access control checks, now that we know in which
19769 SCOPE the declared entity resides. */
19770 if (!member_p && decl)
19772 tree saved_current_function_decl = NULL_TREE;
19774 /* If the entity being declared is a function, pretend that we
19775 are in its scope. If it is a `friend', it may have access to
19776 things that would not otherwise be accessible. */
19777 if (TREE_CODE (decl) == FUNCTION_DECL)
19779 saved_current_function_decl = current_function_decl;
19780 current_function_decl = decl;
19783 /* Perform access checks for template parameters. */
19784 cp_parser_perform_template_parameter_access_checks (checks);
19786 /* Perform the access control checks for the declarator and the
19787 decl-specifiers. */
19788 perform_deferred_access_checks (tf_warning_or_error);
19790 /* Restore the saved value. */
19791 if (TREE_CODE (decl) == FUNCTION_DECL)
19792 current_function_decl = saved_current_function_decl;
19795 /* Parse the initializer. */
19796 initializer = NULL_TREE;
19797 is_direct_init = false;
19798 is_non_constant_init = true;
19799 if (is_initialized)
19801 if (function_declarator_p (declarator))
19803 if (initialization_kind == CPP_EQ)
19804 initializer = cp_parser_pure_specifier (parser);
19805 else
19807 /* If the declaration was erroneous, we don't really
19808 know what the user intended, so just silently
19809 consume the initializer. */
19810 if (decl != error_mark_node)
19811 error_at (tmp_init_loc, "initializer provided for function");
19812 cp_parser_skip_to_closing_parenthesis (parser,
19813 /*recovering=*/true,
19814 /*or_comma=*/false,
19815 /*consume_paren=*/true);
19818 else
19820 /* We want to record the extra mangling scope for in-class
19821 initializers of class members and initializers of static data
19822 member templates. The former involves deferring
19823 parsing of the initializer until end of class as with default
19824 arguments. So right here we only handle the latter. */
19825 if (!member_p && processing_template_decl && decl != error_mark_node)
19826 start_lambda_scope (decl);
19827 initializer = cp_parser_initializer (parser,
19828 &is_direct_init,
19829 &is_non_constant_init);
19830 if (!member_p && processing_template_decl && decl != error_mark_node)
19831 finish_lambda_scope ();
19832 if (initializer == error_mark_node)
19833 cp_parser_skip_to_end_of_statement (parser);
19837 /* The old parser allows attributes to appear after a parenthesized
19838 initializer. Mark Mitchell proposed removing this functionality
19839 on the GCC mailing lists on 2002-08-13. This parser accepts the
19840 attributes -- but ignores them. Made a permerror in GCC 8. */
19841 if (cp_parser_allow_gnu_extensions_p (parser)
19842 && initialization_kind == CPP_OPEN_PAREN
19843 && cp_parser_attributes_opt (parser)
19844 && permerror (input_location,
19845 "attributes after parenthesized initializer ignored"))
19847 static bool hint;
19848 if (flag_permissive && !hint)
19850 hint = true;
19851 inform (input_location,
19852 "this flexibility is deprecated and will be removed");
19856 /* And now complain about a non-function implicit template. */
19857 if (bogus_implicit_tmpl && decl != error_mark_node)
19858 error_at (DECL_SOURCE_LOCATION (decl),
19859 "non-function %qD declared as implicit template", decl);
19861 /* For an in-class declaration, use `grokfield' to create the
19862 declaration. */
19863 if (member_p)
19865 if (pushed_scope)
19867 pop_scope (pushed_scope);
19868 pushed_scope = NULL_TREE;
19870 decl = grokfield (declarator, decl_specifiers,
19871 initializer, !is_non_constant_init,
19872 /*asmspec=*/NULL_TREE,
19873 attr_chainon (attributes, prefix_attributes));
19874 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19875 cp_parser_save_default_args (parser, decl);
19876 cp_finalize_omp_declare_simd (parser, decl);
19877 cp_finalize_oacc_routine (parser, decl, false);
19880 /* Finish processing the declaration. But, skip member
19881 declarations. */
19882 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19884 cp_finish_decl (decl,
19885 initializer, !is_non_constant_init,
19886 asm_specification,
19887 /* If the initializer is in parentheses, then this is
19888 a direct-initialization, which means that an
19889 `explicit' constructor is OK. Otherwise, an
19890 `explicit' constructor cannot be used. */
19891 ((is_direct_init || !is_initialized)
19892 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19894 else if ((cxx_dialect != cxx98) && friend_p
19895 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19896 /* Core issue #226 (C++0x only): A default template-argument
19897 shall not be specified in a friend class template
19898 declaration. */
19899 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19900 /*is_partial=*/false, /*is_friend_decl=*/1);
19902 if (!friend_p && pushed_scope)
19903 pop_scope (pushed_scope);
19905 if (function_declarator_p (declarator)
19906 && parser->fully_implicit_function_template_p)
19908 if (member_p)
19909 decl = finish_fully_implicit_template (parser, decl);
19910 else
19911 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19914 if (auto_result && is_initialized && decl_specifiers->type
19915 && type_uses_auto (decl_specifiers->type))
19916 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19918 return decl;
19921 /* Parse a declarator.
19923 declarator:
19924 direct-declarator
19925 ptr-operator declarator
19927 abstract-declarator:
19928 ptr-operator abstract-declarator [opt]
19929 direct-abstract-declarator
19931 GNU Extensions:
19933 declarator:
19934 attributes [opt] direct-declarator
19935 attributes [opt] ptr-operator declarator
19937 abstract-declarator:
19938 attributes [opt] ptr-operator abstract-declarator [opt]
19939 attributes [opt] direct-abstract-declarator
19941 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19942 detect constructors, destructors, deduction guides, or conversion operators.
19943 It is set to -1 if the declarator is a name, and +1 if it is a
19944 function. Otherwise it is set to zero. Usually you just want to
19945 test for >0, but internally the negative value is used.
19947 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19948 a decl-specifier-seq unless it declares a constructor, destructor,
19949 or conversion. It might seem that we could check this condition in
19950 semantic analysis, rather than parsing, but that makes it difficult
19951 to handle something like `f()'. We want to notice that there are
19952 no decl-specifiers, and therefore realize that this is an
19953 expression, not a declaration.)
19955 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19956 the declarator is a direct-declarator of the form "(...)".
19958 MEMBER_P is true iff this declarator is a member-declarator.
19960 FRIEND_P is true iff this declarator is a friend. */
19962 static cp_declarator *
19963 cp_parser_declarator (cp_parser* parser,
19964 cp_parser_declarator_kind dcl_kind,
19965 int* ctor_dtor_or_conv_p,
19966 bool* parenthesized_p,
19967 bool member_p, bool friend_p)
19969 cp_declarator *declarator;
19970 enum tree_code code;
19971 cp_cv_quals cv_quals;
19972 tree class_type;
19973 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19975 /* Assume this is not a constructor, destructor, or type-conversion
19976 operator. */
19977 if (ctor_dtor_or_conv_p)
19978 *ctor_dtor_or_conv_p = 0;
19980 if (cp_parser_allow_gnu_extensions_p (parser))
19981 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19983 /* Check for the ptr-operator production. */
19984 cp_parser_parse_tentatively (parser);
19985 /* Parse the ptr-operator. */
19986 code = cp_parser_ptr_operator (parser,
19987 &class_type,
19988 &cv_quals,
19989 &std_attributes);
19991 /* If that worked, then we have a ptr-operator. */
19992 if (cp_parser_parse_definitely (parser))
19994 /* If a ptr-operator was found, then this declarator was not
19995 parenthesized. */
19996 if (parenthesized_p)
19997 *parenthesized_p = true;
19998 /* The dependent declarator is optional if we are parsing an
19999 abstract-declarator. */
20000 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20001 cp_parser_parse_tentatively (parser);
20003 /* Parse the dependent declarator. */
20004 declarator = cp_parser_declarator (parser, dcl_kind,
20005 /*ctor_dtor_or_conv_p=*/NULL,
20006 /*parenthesized_p=*/NULL,
20007 /*member_p=*/false,
20008 friend_p);
20010 /* If we are parsing an abstract-declarator, we must handle the
20011 case where the dependent declarator is absent. */
20012 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20013 && !cp_parser_parse_definitely (parser))
20014 declarator = NULL;
20016 declarator = cp_parser_make_indirect_declarator
20017 (code, class_type, cv_quals, declarator, std_attributes);
20019 /* Everything else is a direct-declarator. */
20020 else
20022 if (parenthesized_p)
20023 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20024 CPP_OPEN_PAREN);
20025 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20026 ctor_dtor_or_conv_p,
20027 member_p, friend_p);
20030 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20031 declarator->attributes = gnu_attributes;
20032 return declarator;
20035 /* Parse a direct-declarator or direct-abstract-declarator.
20037 direct-declarator:
20038 declarator-id
20039 direct-declarator ( parameter-declaration-clause )
20040 cv-qualifier-seq [opt]
20041 ref-qualifier [opt]
20042 exception-specification [opt]
20043 direct-declarator [ constant-expression [opt] ]
20044 ( declarator )
20046 direct-abstract-declarator:
20047 direct-abstract-declarator [opt]
20048 ( parameter-declaration-clause )
20049 cv-qualifier-seq [opt]
20050 ref-qualifier [opt]
20051 exception-specification [opt]
20052 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20053 ( abstract-declarator )
20055 Returns a representation of the declarator. DCL_KIND is
20056 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20057 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20058 we are parsing a direct-declarator. It is
20059 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20060 of ambiguity we prefer an abstract declarator, as per
20061 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20062 as for cp_parser_declarator. */
20064 static cp_declarator *
20065 cp_parser_direct_declarator (cp_parser* parser,
20066 cp_parser_declarator_kind dcl_kind,
20067 int* ctor_dtor_or_conv_p,
20068 bool member_p, bool friend_p)
20070 cp_token *token;
20071 cp_declarator *declarator = NULL;
20072 tree scope = NULL_TREE;
20073 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20074 bool saved_in_declarator_p = parser->in_declarator_p;
20075 bool first = true;
20076 tree pushed_scope = NULL_TREE;
20077 cp_token *open_paren = NULL, *close_paren = NULL;
20079 while (true)
20081 /* Peek at the next token. */
20082 token = cp_lexer_peek_token (parser->lexer);
20083 if (token->type == CPP_OPEN_PAREN)
20085 /* This is either a parameter-declaration-clause, or a
20086 parenthesized declarator. When we know we are parsing a
20087 named declarator, it must be a parenthesized declarator
20088 if FIRST is true. For instance, `(int)' is a
20089 parameter-declaration-clause, with an omitted
20090 direct-abstract-declarator. But `((*))', is a
20091 parenthesized abstract declarator. Finally, when T is a
20092 template parameter `(T)' is a
20093 parameter-declaration-clause, and not a parenthesized
20094 named declarator.
20096 We first try and parse a parameter-declaration-clause,
20097 and then try a nested declarator (if FIRST is true).
20099 It is not an error for it not to be a
20100 parameter-declaration-clause, even when FIRST is
20101 false. Consider,
20103 int i (int);
20104 int i (3);
20106 The first is the declaration of a function while the
20107 second is the definition of a variable, including its
20108 initializer.
20110 Having seen only the parenthesis, we cannot know which of
20111 these two alternatives should be selected. Even more
20112 complex are examples like:
20114 int i (int (a));
20115 int i (int (3));
20117 The former is a function-declaration; the latter is a
20118 variable initialization.
20120 Thus again, we try a parameter-declaration-clause, and if
20121 that fails, we back out and return. */
20123 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20125 tree params;
20126 bool is_declarator = false;
20128 open_paren = NULL;
20130 /* In a member-declarator, the only valid interpretation
20131 of a parenthesis is the start of a
20132 parameter-declaration-clause. (It is invalid to
20133 initialize a static data member with a parenthesized
20134 initializer; only the "=" form of initialization is
20135 permitted.) */
20136 if (!member_p)
20137 cp_parser_parse_tentatively (parser);
20139 /* Consume the `('. */
20140 matching_parens parens;
20141 parens.consume_open (parser);
20142 if (first)
20144 /* If this is going to be an abstract declarator, we're
20145 in a declarator and we can't have default args. */
20146 parser->default_arg_ok_p = false;
20147 parser->in_declarator_p = true;
20150 begin_scope (sk_function_parms, NULL_TREE);
20152 /* Parse the parameter-declaration-clause. */
20153 params = cp_parser_parameter_declaration_clause (parser);
20155 /* Consume the `)'. */
20156 parens.require_close (parser);
20158 /* If all went well, parse the cv-qualifier-seq,
20159 ref-qualifier and the exception-specification. */
20160 if (member_p || cp_parser_parse_definitely (parser))
20162 cp_cv_quals cv_quals;
20163 cp_virt_specifiers virt_specifiers;
20164 cp_ref_qualifier ref_qual;
20165 tree exception_specification;
20166 tree late_return;
20167 tree attrs;
20168 bool memfn = (member_p || (pushed_scope
20169 && CLASS_TYPE_P (pushed_scope)));
20171 is_declarator = true;
20173 if (ctor_dtor_or_conv_p)
20174 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20175 first = false;
20177 /* Parse the cv-qualifier-seq. */
20178 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20179 /* Parse the ref-qualifier. */
20180 ref_qual = cp_parser_ref_qualifier_opt (parser);
20181 /* Parse the tx-qualifier. */
20182 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20183 /* And the exception-specification. */
20184 exception_specification
20185 = cp_parser_exception_specification_opt (parser);
20187 attrs = cp_parser_std_attribute_spec_seq (parser);
20189 /* In here, we handle cases where attribute is used after
20190 the function declaration. For example:
20191 void func (int x) __attribute__((vector(..))); */
20192 tree gnu_attrs = NULL_TREE;
20193 tree requires_clause = NULL_TREE;
20194 late_return = (cp_parser_late_return_type_opt
20195 (parser, declarator, requires_clause,
20196 memfn ? cv_quals : -1));
20198 /* Parse the virt-specifier-seq. */
20199 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20201 /* Create the function-declarator. */
20202 declarator = make_call_declarator (declarator,
20203 params,
20204 cv_quals,
20205 virt_specifiers,
20206 ref_qual,
20207 tx_qual,
20208 exception_specification,
20209 late_return,
20210 requires_clause);
20211 declarator->std_attributes = attrs;
20212 declarator->attributes = gnu_attrs;
20213 /* Any subsequent parameter lists are to do with
20214 return type, so are not those of the declared
20215 function. */
20216 parser->default_arg_ok_p = false;
20219 /* Remove the function parms from scope. */
20220 pop_bindings_and_leave_scope ();
20222 if (is_declarator)
20223 /* Repeat the main loop. */
20224 continue;
20227 /* If this is the first, we can try a parenthesized
20228 declarator. */
20229 if (first)
20231 bool saved_in_type_id_in_expr_p;
20233 parser->default_arg_ok_p = saved_default_arg_ok_p;
20234 parser->in_declarator_p = saved_in_declarator_p;
20236 open_paren = token;
20237 /* Consume the `('. */
20238 matching_parens parens;
20239 parens.consume_open (parser);
20240 /* Parse the nested declarator. */
20241 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20242 parser->in_type_id_in_expr_p = true;
20243 declarator
20244 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20245 /*parenthesized_p=*/NULL,
20246 member_p, friend_p);
20247 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20248 first = false;
20249 /* Expect a `)'. */
20250 close_paren = cp_lexer_peek_token (parser->lexer);
20251 if (!parens.require_close (parser))
20252 declarator = cp_error_declarator;
20253 if (declarator == cp_error_declarator)
20254 break;
20256 goto handle_declarator;
20258 /* Otherwise, we must be done. */
20259 else
20260 break;
20262 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20263 && token->type == CPP_OPEN_SQUARE
20264 && !cp_next_tokens_can_be_attribute_p (parser))
20266 /* Parse an array-declarator. */
20267 tree bounds, attrs;
20269 if (ctor_dtor_or_conv_p)
20270 *ctor_dtor_or_conv_p = 0;
20272 open_paren = NULL;
20273 first = false;
20274 parser->default_arg_ok_p = false;
20275 parser->in_declarator_p = true;
20276 /* Consume the `['. */
20277 cp_lexer_consume_token (parser->lexer);
20278 /* Peek at the next token. */
20279 token = cp_lexer_peek_token (parser->lexer);
20280 /* If the next token is `]', then there is no
20281 constant-expression. */
20282 if (token->type != CPP_CLOSE_SQUARE)
20284 bool non_constant_p;
20285 bounds
20286 = cp_parser_constant_expression (parser,
20287 /*allow_non_constant=*/true,
20288 &non_constant_p);
20289 if (!non_constant_p)
20290 /* OK */;
20291 else if (error_operand_p (bounds))
20292 /* Already gave an error. */;
20293 else if (!parser->in_function_body
20294 || current_binding_level->kind == sk_function_parms)
20296 /* Normally, the array bound must be an integral constant
20297 expression. However, as an extension, we allow VLAs
20298 in function scopes as long as they aren't part of a
20299 parameter declaration. */
20300 cp_parser_error (parser,
20301 "array bound is not an integer constant");
20302 bounds = error_mark_node;
20304 else if (processing_template_decl
20305 && !type_dependent_expression_p (bounds))
20307 /* Remember this wasn't a constant-expression. */
20308 bounds = build_nop (TREE_TYPE (bounds), bounds);
20309 TREE_SIDE_EFFECTS (bounds) = 1;
20312 else
20313 bounds = NULL_TREE;
20314 /* Look for the closing `]'. */
20315 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20317 declarator = cp_error_declarator;
20318 break;
20321 attrs = cp_parser_std_attribute_spec_seq (parser);
20322 declarator = make_array_declarator (declarator, bounds);
20323 declarator->std_attributes = attrs;
20325 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20328 tree qualifying_scope;
20329 tree unqualified_name;
20330 tree attrs;
20331 special_function_kind sfk;
20332 bool abstract_ok;
20333 bool pack_expansion_p = false;
20334 cp_token *declarator_id_start_token;
20336 /* Parse a declarator-id */
20337 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20338 if (abstract_ok)
20340 cp_parser_parse_tentatively (parser);
20342 /* If we see an ellipsis, we should be looking at a
20343 parameter pack. */
20344 if (token->type == CPP_ELLIPSIS)
20346 /* Consume the `...' */
20347 cp_lexer_consume_token (parser->lexer);
20349 pack_expansion_p = true;
20353 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20354 unqualified_name
20355 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20356 qualifying_scope = parser->scope;
20357 if (abstract_ok)
20359 bool okay = false;
20361 if (!unqualified_name && pack_expansion_p)
20363 /* Check whether an error occurred. */
20364 okay = !cp_parser_error_occurred (parser);
20366 /* We already consumed the ellipsis to mark a
20367 parameter pack, but we have no way to report it,
20368 so abort the tentative parse. We will be exiting
20369 immediately anyway. */
20370 cp_parser_abort_tentative_parse (parser);
20372 else
20373 okay = cp_parser_parse_definitely (parser);
20375 if (!okay)
20376 unqualified_name = error_mark_node;
20377 else if (unqualified_name
20378 && (qualifying_scope
20379 || (!identifier_p (unqualified_name))))
20381 cp_parser_error (parser, "expected unqualified-id");
20382 unqualified_name = error_mark_node;
20386 if (!unqualified_name)
20387 return NULL;
20388 if (unqualified_name == error_mark_node)
20390 declarator = cp_error_declarator;
20391 pack_expansion_p = false;
20392 declarator->parameter_pack_p = false;
20393 break;
20396 attrs = cp_parser_std_attribute_spec_seq (parser);
20398 if (qualifying_scope && at_namespace_scope_p ()
20399 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20401 /* In the declaration of a member of a template class
20402 outside of the class itself, the SCOPE will sometimes
20403 be a TYPENAME_TYPE. For example, given:
20405 template <typename T>
20406 int S<T>::R::i = 3;
20408 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20409 this context, we must resolve S<T>::R to an ordinary
20410 type, rather than a typename type.
20412 The reason we normally avoid resolving TYPENAME_TYPEs
20413 is that a specialization of `S' might render
20414 `S<T>::R' not a type. However, if `S' is
20415 specialized, then this `i' will not be used, so there
20416 is no harm in resolving the types here. */
20417 tree type;
20419 /* Resolve the TYPENAME_TYPE. */
20420 type = resolve_typename_type (qualifying_scope,
20421 /*only_current_p=*/false);
20422 /* If that failed, the declarator is invalid. */
20423 if (TREE_CODE (type) == TYPENAME_TYPE)
20425 if (typedef_variant_p (type))
20426 error_at (declarator_id_start_token->location,
20427 "cannot define member of dependent typedef "
20428 "%qT", type);
20429 else
20430 error_at (declarator_id_start_token->location,
20431 "%<%T::%E%> is not a type",
20432 TYPE_CONTEXT (qualifying_scope),
20433 TYPE_IDENTIFIER (qualifying_scope));
20435 qualifying_scope = type;
20438 sfk = sfk_none;
20440 if (unqualified_name)
20442 tree class_type;
20444 if (qualifying_scope
20445 && CLASS_TYPE_P (qualifying_scope))
20446 class_type = qualifying_scope;
20447 else
20448 class_type = current_class_type;
20450 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20452 tree name_type = TREE_TYPE (unqualified_name);
20454 if (!class_type || !same_type_p (name_type, class_type))
20456 /* We do not attempt to print the declarator
20457 here because we do not have enough
20458 information about its original syntactic
20459 form. */
20460 cp_parser_error (parser, "invalid declarator");
20461 declarator = cp_error_declarator;
20462 break;
20464 else if (qualifying_scope
20465 && CLASSTYPE_USE_TEMPLATE (name_type))
20467 error_at (declarator_id_start_token->location,
20468 "invalid use of constructor as a template");
20469 inform (declarator_id_start_token->location,
20470 "use %<%T::%D%> instead of %<%T::%D%> to "
20471 "name the constructor in a qualified name",
20472 class_type,
20473 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20474 class_type, name_type);
20475 declarator = cp_error_declarator;
20476 break;
20478 unqualified_name = constructor_name (class_type);
20481 if (class_type)
20483 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20484 sfk = sfk_destructor;
20485 else if (identifier_p (unqualified_name)
20486 && IDENTIFIER_CONV_OP_P (unqualified_name))
20487 sfk = sfk_conversion;
20488 else if (/* There's no way to declare a constructor
20489 for an unnamed type, even if the type
20490 got a name for linkage purposes. */
20491 !TYPE_WAS_UNNAMED (class_type)
20492 /* Handle correctly (c++/19200):
20494 struct S {
20495 struct T{};
20496 friend void S(T);
20499 and also:
20501 namespace N {
20502 void S();
20505 struct S {
20506 friend void N::S();
20507 }; */
20508 && (!friend_p || class_type == qualifying_scope)
20509 && constructor_name_p (unqualified_name,
20510 class_type))
20511 sfk = sfk_constructor;
20512 else if (is_overloaded_fn (unqualified_name)
20513 && DECL_CONSTRUCTOR_P (get_first_fn
20514 (unqualified_name)))
20515 sfk = sfk_constructor;
20517 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20518 *ctor_dtor_or_conv_p = -1;
20521 declarator = make_id_declarator (qualifying_scope,
20522 unqualified_name,
20523 sfk);
20524 declarator->std_attributes = attrs;
20525 declarator->id_loc = token->location;
20526 declarator->parameter_pack_p = pack_expansion_p;
20528 if (pack_expansion_p)
20529 maybe_warn_variadic_templates ();
20532 handle_declarator:;
20533 scope = get_scope_of_declarator (declarator);
20534 if (scope)
20536 /* Any names that appear after the declarator-id for a
20537 member are looked up in the containing scope. */
20538 if (at_function_scope_p ())
20540 /* But declarations with qualified-ids can't appear in a
20541 function. */
20542 cp_parser_error (parser, "qualified-id in declaration");
20543 declarator = cp_error_declarator;
20544 break;
20546 pushed_scope = push_scope (scope);
20548 parser->in_declarator_p = true;
20549 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20550 || (declarator && declarator->kind == cdk_id))
20551 /* Default args are only allowed on function
20552 declarations. */
20553 parser->default_arg_ok_p = saved_default_arg_ok_p;
20554 else
20555 parser->default_arg_ok_p = false;
20557 first = false;
20559 /* We're done. */
20560 else
20561 break;
20564 /* For an abstract declarator, we might wind up with nothing at this
20565 point. That's an error; the declarator is not optional. */
20566 if (!declarator)
20567 cp_parser_error (parser, "expected declarator");
20568 else if (open_paren)
20570 /* Record overly parenthesized declarator so we can give a
20571 diagnostic about confusing decl/expr disambiguation. */
20572 if (declarator->kind == cdk_array)
20574 /* If the open and close parens are on different lines, this
20575 is probably a formatting thing, so ignore. */
20576 expanded_location open = expand_location (open_paren->location);
20577 expanded_location close = expand_location (close_paren->location);
20578 if (open.line != close.line || open.file != close.file)
20579 open_paren = NULL;
20581 if (open_paren)
20582 declarator->parenthesized = open_paren->location;
20585 /* If we entered a scope, we must exit it now. */
20586 if (pushed_scope)
20587 pop_scope (pushed_scope);
20589 parser->default_arg_ok_p = saved_default_arg_ok_p;
20590 parser->in_declarator_p = saved_in_declarator_p;
20592 return declarator;
20595 /* Parse a ptr-operator.
20597 ptr-operator:
20598 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20599 * cv-qualifier-seq [opt]
20601 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20602 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20604 GNU Extension:
20606 ptr-operator:
20607 & cv-qualifier-seq [opt]
20609 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20610 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20611 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20612 filled in with the TYPE containing the member. *CV_QUALS is
20613 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20614 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20615 Note that the tree codes returned by this function have nothing
20616 to do with the types of trees that will be eventually be created
20617 to represent the pointer or reference type being parsed. They are
20618 just constants with suggestive names. */
20619 static enum tree_code
20620 cp_parser_ptr_operator (cp_parser* parser,
20621 tree* type,
20622 cp_cv_quals *cv_quals,
20623 tree *attributes)
20625 enum tree_code code = ERROR_MARK;
20626 cp_token *token;
20627 tree attrs = NULL_TREE;
20629 /* Assume that it's not a pointer-to-member. */
20630 *type = NULL_TREE;
20631 /* And that there are no cv-qualifiers. */
20632 *cv_quals = TYPE_UNQUALIFIED;
20634 /* Peek at the next token. */
20635 token = cp_lexer_peek_token (parser->lexer);
20637 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20638 if (token->type == CPP_MULT)
20639 code = INDIRECT_REF;
20640 else if (token->type == CPP_AND)
20641 code = ADDR_EXPR;
20642 else if ((cxx_dialect != cxx98) &&
20643 token->type == CPP_AND_AND) /* C++0x only */
20644 code = NON_LVALUE_EXPR;
20646 if (code != ERROR_MARK)
20648 /* Consume the `*', `&' or `&&'. */
20649 cp_lexer_consume_token (parser->lexer);
20651 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20652 `&', if we are allowing GNU extensions. (The only qualifier
20653 that can legally appear after `&' is `restrict', but that is
20654 enforced during semantic analysis. */
20655 if (code == INDIRECT_REF
20656 || cp_parser_allow_gnu_extensions_p (parser))
20657 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20659 attrs = cp_parser_std_attribute_spec_seq (parser);
20660 if (attributes != NULL)
20661 *attributes = attrs;
20663 else
20665 /* Try the pointer-to-member case. */
20666 cp_parser_parse_tentatively (parser);
20667 /* Look for the optional `::' operator. */
20668 cp_parser_global_scope_opt (parser,
20669 /*current_scope_valid_p=*/false);
20670 /* Look for the nested-name specifier. */
20671 token = cp_lexer_peek_token (parser->lexer);
20672 cp_parser_nested_name_specifier (parser,
20673 /*typename_keyword_p=*/false,
20674 /*check_dependency_p=*/true,
20675 /*type_p=*/false,
20676 /*is_declaration=*/false);
20677 /* If we found it, and the next token is a `*', then we are
20678 indeed looking at a pointer-to-member operator. */
20679 if (!cp_parser_error_occurred (parser)
20680 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20682 /* Indicate that the `*' operator was used. */
20683 code = INDIRECT_REF;
20685 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20686 error_at (token->location, "%qD is a namespace", parser->scope);
20687 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20688 error_at (token->location, "cannot form pointer to member of "
20689 "non-class %q#T", parser->scope);
20690 else
20692 /* The type of which the member is a member is given by the
20693 current SCOPE. */
20694 *type = parser->scope;
20695 /* The next name will not be qualified. */
20696 parser->scope = NULL_TREE;
20697 parser->qualifying_scope = NULL_TREE;
20698 parser->object_scope = NULL_TREE;
20699 /* Look for optional c++11 attributes. */
20700 attrs = cp_parser_std_attribute_spec_seq (parser);
20701 if (attributes != NULL)
20702 *attributes = attrs;
20703 /* Look for the optional cv-qualifier-seq. */
20704 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20707 /* If that didn't work we don't have a ptr-operator. */
20708 if (!cp_parser_parse_definitely (parser))
20709 cp_parser_error (parser, "expected ptr-operator");
20712 return code;
20715 /* Parse an (optional) cv-qualifier-seq.
20717 cv-qualifier-seq:
20718 cv-qualifier cv-qualifier-seq [opt]
20720 cv-qualifier:
20721 const
20722 volatile
20724 GNU Extension:
20726 cv-qualifier:
20727 __restrict__
20729 Returns a bitmask representing the cv-qualifiers. */
20731 static cp_cv_quals
20732 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20734 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20736 while (true)
20738 cp_token *token;
20739 cp_cv_quals cv_qualifier;
20741 /* Peek at the next token. */
20742 token = cp_lexer_peek_token (parser->lexer);
20743 /* See if it's a cv-qualifier. */
20744 switch (token->keyword)
20746 case RID_CONST:
20747 cv_qualifier = TYPE_QUAL_CONST;
20748 break;
20750 case RID_VOLATILE:
20751 cv_qualifier = TYPE_QUAL_VOLATILE;
20752 break;
20754 case RID_RESTRICT:
20755 cv_qualifier = TYPE_QUAL_RESTRICT;
20756 break;
20758 default:
20759 cv_qualifier = TYPE_UNQUALIFIED;
20760 break;
20763 if (!cv_qualifier)
20764 break;
20766 if (cv_quals & cv_qualifier)
20768 gcc_rich_location richloc (token->location);
20769 richloc.add_fixit_remove ();
20770 error_at (&richloc, "duplicate cv-qualifier");
20771 cp_lexer_purge_token (parser->lexer);
20773 else
20775 cp_lexer_consume_token (parser->lexer);
20776 cv_quals |= cv_qualifier;
20780 return cv_quals;
20783 /* Parse an (optional) ref-qualifier
20785 ref-qualifier:
20789 Returns cp_ref_qualifier representing ref-qualifier. */
20791 static cp_ref_qualifier
20792 cp_parser_ref_qualifier_opt (cp_parser* parser)
20794 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20796 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20797 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20798 return ref_qual;
20800 while (true)
20802 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20803 cp_token *token = cp_lexer_peek_token (parser->lexer);
20805 switch (token->type)
20807 case CPP_AND:
20808 curr_ref_qual = REF_QUAL_LVALUE;
20809 break;
20811 case CPP_AND_AND:
20812 curr_ref_qual = REF_QUAL_RVALUE;
20813 break;
20815 default:
20816 curr_ref_qual = REF_QUAL_NONE;
20817 break;
20820 if (!curr_ref_qual)
20821 break;
20822 else if (ref_qual)
20824 error_at (token->location, "multiple ref-qualifiers");
20825 cp_lexer_purge_token (parser->lexer);
20827 else
20829 ref_qual = curr_ref_qual;
20830 cp_lexer_consume_token (parser->lexer);
20834 return ref_qual;
20837 /* Parse an optional tx-qualifier.
20839 tx-qualifier:
20840 transaction_safe
20841 transaction_safe_dynamic */
20843 static tree
20844 cp_parser_tx_qualifier_opt (cp_parser *parser)
20846 cp_token *token = cp_lexer_peek_token (parser->lexer);
20847 if (token->type == CPP_NAME)
20849 tree name = token->u.value;
20850 const char *p = IDENTIFIER_POINTER (name);
20851 const int len = strlen ("transaction_safe");
20852 if (!strncmp (p, "transaction_safe", len))
20854 p += len;
20855 if (*p == '\0'
20856 || !strcmp (p, "_dynamic"))
20858 cp_lexer_consume_token (parser->lexer);
20859 if (!flag_tm)
20861 error ("%qE requires %<-fgnu-tm%>", name);
20862 return NULL_TREE;
20864 else
20865 return name;
20869 return NULL_TREE;
20872 /* Parse an (optional) virt-specifier-seq.
20874 virt-specifier-seq:
20875 virt-specifier virt-specifier-seq [opt]
20877 virt-specifier:
20878 override
20879 final
20881 Returns a bitmask representing the virt-specifiers. */
20883 static cp_virt_specifiers
20884 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20886 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20888 while (true)
20890 cp_token *token;
20891 cp_virt_specifiers virt_specifier;
20893 /* Peek at the next token. */
20894 token = cp_lexer_peek_token (parser->lexer);
20895 /* See if it's a virt-specifier-qualifier. */
20896 if (token->type != CPP_NAME)
20897 break;
20898 if (id_equal (token->u.value, "override"))
20900 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20901 virt_specifier = VIRT_SPEC_OVERRIDE;
20903 else if (id_equal (token->u.value, "final"))
20905 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20906 virt_specifier = VIRT_SPEC_FINAL;
20908 else if (id_equal (token->u.value, "__final"))
20910 virt_specifier = VIRT_SPEC_FINAL;
20912 else
20913 break;
20915 if (virt_specifiers & virt_specifier)
20917 gcc_rich_location richloc (token->location);
20918 richloc.add_fixit_remove ();
20919 error_at (&richloc, "duplicate virt-specifier");
20920 cp_lexer_purge_token (parser->lexer);
20922 else
20924 cp_lexer_consume_token (parser->lexer);
20925 virt_specifiers |= virt_specifier;
20928 return virt_specifiers;
20931 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20932 is in scope even though it isn't real. */
20934 void
20935 inject_this_parameter (tree ctype, cp_cv_quals quals)
20937 tree this_parm;
20939 if (current_class_ptr)
20941 /* We don't clear this between NSDMIs. Is it already what we want? */
20942 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20943 if (DECL_P (current_class_ptr)
20944 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20945 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20946 && cp_type_quals (type) == quals)
20947 return;
20950 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20951 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20952 current_class_ptr = NULL_TREE;
20953 current_class_ref
20954 = cp_build_fold_indirect_ref (this_parm);
20955 current_class_ptr = this_parm;
20958 /* Return true iff our current scope is a non-static data member
20959 initializer. */
20961 bool
20962 parsing_nsdmi (void)
20964 /* We recognize NSDMI context by the context-less 'this' pointer set up
20965 by the function above. */
20966 if (current_class_ptr
20967 && TREE_CODE (current_class_ptr) == PARM_DECL
20968 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20969 return true;
20970 return false;
20973 /* Parse a late-specified return type, if any. This is not a separate
20974 non-terminal, but part of a function declarator, which looks like
20976 -> trailing-type-specifier-seq abstract-declarator(opt)
20978 Returns the type indicated by the type-id.
20980 In addition to this, parse any queued up #pragma omp declare simd
20981 clauses, and #pragma acc routine clauses.
20983 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20984 function. */
20986 static tree
20987 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20988 tree& requires_clause, cp_cv_quals quals)
20990 cp_token *token;
20991 tree type = NULL_TREE;
20992 bool declare_simd_p = (parser->omp_declare_simd
20993 && declarator
20994 && declarator->kind == cdk_id);
20996 bool oacc_routine_p = (parser->oacc_routine
20997 && declarator
20998 && declarator->kind == cdk_id);
21000 /* Peek at the next token. */
21001 token = cp_lexer_peek_token (parser->lexer);
21002 /* A late-specified return type is indicated by an initial '->'. */
21003 if (token->type != CPP_DEREF
21004 && token->keyword != RID_REQUIRES
21005 && !(token->type == CPP_NAME
21006 && token->u.value == ridpointers[RID_REQUIRES])
21007 && !(declare_simd_p || oacc_routine_p))
21008 return NULL_TREE;
21010 tree save_ccp = current_class_ptr;
21011 tree save_ccr = current_class_ref;
21012 if (quals >= 0)
21014 /* DR 1207: 'this' is in scope in the trailing return type. */
21015 inject_this_parameter (current_class_type, quals);
21018 if (token->type == CPP_DEREF)
21020 /* Consume the ->. */
21021 cp_lexer_consume_token (parser->lexer);
21023 type = cp_parser_trailing_type_id (parser);
21026 /* Function declarations may be followed by a trailing
21027 requires-clause. */
21028 requires_clause = cp_parser_requires_clause_opt (parser);
21030 if (declare_simd_p)
21031 declarator->attributes
21032 = cp_parser_late_parsing_omp_declare_simd (parser,
21033 declarator->attributes);
21034 if (oacc_routine_p)
21035 declarator->attributes
21036 = cp_parser_late_parsing_oacc_routine (parser,
21037 declarator->attributes);
21039 if (quals >= 0)
21041 current_class_ptr = save_ccp;
21042 current_class_ref = save_ccr;
21045 return type;
21048 /* Parse a declarator-id.
21050 declarator-id:
21051 id-expression
21052 :: [opt] nested-name-specifier [opt] type-name
21054 In the `id-expression' case, the value returned is as for
21055 cp_parser_id_expression if the id-expression was an unqualified-id.
21056 If the id-expression was a qualified-id, then a SCOPE_REF is
21057 returned. The first operand is the scope (either a NAMESPACE_DECL
21058 or TREE_TYPE), but the second is still just a representation of an
21059 unqualified-id. */
21061 static tree
21062 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21064 tree id;
21065 /* The expression must be an id-expression. Assume that qualified
21066 names are the names of types so that:
21068 template <class T>
21069 int S<T>::R::i = 3;
21071 will work; we must treat `S<T>::R' as the name of a type.
21072 Similarly, assume that qualified names are templates, where
21073 required, so that:
21075 template <class T>
21076 int S<T>::R<T>::i = 3;
21078 will work, too. */
21079 id = cp_parser_id_expression (parser,
21080 /*template_keyword_p=*/false,
21081 /*check_dependency_p=*/false,
21082 /*template_p=*/NULL,
21083 /*declarator_p=*/true,
21084 optional_p);
21085 if (id && BASELINK_P (id))
21086 id = BASELINK_FUNCTIONS (id);
21087 return id;
21090 /* Parse a type-id.
21092 type-id:
21093 type-specifier-seq abstract-declarator [opt]
21095 Returns the TYPE specified. */
21097 static tree
21098 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21099 bool is_trailing_return, location_t * type_location)
21101 cp_decl_specifier_seq type_specifier_seq;
21102 cp_declarator *abstract_declarator;
21104 /* Parse the type-specifier-seq. */
21105 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21106 is_trailing_return,
21107 &type_specifier_seq);
21108 if (type_location)
21109 *type_location = type_specifier_seq.locations[ds_type_spec];
21111 if (is_template_arg && type_specifier_seq.type
21112 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21113 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21114 /* A bare template name as a template argument is a template template
21115 argument, not a placeholder, so fail parsing it as a type argument. */
21117 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21118 cp_parser_simulate_error (parser);
21119 return error_mark_node;
21121 if (type_specifier_seq.type == error_mark_node)
21122 return error_mark_node;
21124 /* There might or might not be an abstract declarator. */
21125 cp_parser_parse_tentatively (parser);
21126 /* Look for the declarator. */
21127 abstract_declarator
21128 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21129 /*parenthesized_p=*/NULL,
21130 /*member_p=*/false,
21131 /*friend_p=*/false);
21132 /* Check to see if there really was a declarator. */
21133 if (!cp_parser_parse_definitely (parser))
21134 abstract_declarator = NULL;
21136 if (type_specifier_seq.type
21137 /* The concepts TS allows 'auto' as a type-id. */
21138 && (!flag_concepts || parser->in_type_id_in_expr_p)
21139 /* None of the valid uses of 'auto' in C++14 involve the type-id
21140 nonterminal, but it is valid in a trailing-return-type. */
21141 && !(cxx_dialect >= cxx14 && is_trailing_return))
21142 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21144 /* A type-id with type 'auto' is only ok if the abstract declarator
21145 is a function declarator with a late-specified return type.
21147 A type-id with 'auto' is also valid in a trailing-return-type
21148 in a compound-requirement. */
21149 if (abstract_declarator
21150 && abstract_declarator->kind == cdk_function
21151 && abstract_declarator->u.function.late_return_type)
21152 /* OK */;
21153 else if (parser->in_result_type_constraint_p)
21154 /* OK */;
21155 else
21157 location_t loc = type_specifier_seq.locations[ds_type_spec];
21158 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21160 error_at (loc, "missing template arguments after %qT",
21161 auto_node);
21162 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21163 tmpl);
21165 else
21166 error_at (loc, "invalid use of %qT", auto_node);
21167 return error_mark_node;
21171 return groktypename (&type_specifier_seq, abstract_declarator,
21172 is_template_arg);
21175 static tree
21176 cp_parser_type_id (cp_parser *parser, location_t * type_location)
21178 return cp_parser_type_id_1 (parser, false, false, type_location);
21181 static tree
21182 cp_parser_template_type_arg (cp_parser *parser)
21184 tree r;
21185 const char *saved_message = parser->type_definition_forbidden_message;
21186 parser->type_definition_forbidden_message
21187 = G_("types may not be defined in template arguments");
21188 r = cp_parser_type_id_1 (parser, true, false, NULL);
21189 parser->type_definition_forbidden_message = saved_message;
21190 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21192 error ("invalid use of %<auto%> in template argument");
21193 r = error_mark_node;
21195 return r;
21198 static tree
21199 cp_parser_trailing_type_id (cp_parser *parser)
21201 return cp_parser_type_id_1 (parser, false, true, NULL);
21204 /* Parse a type-specifier-seq.
21206 type-specifier-seq:
21207 type-specifier type-specifier-seq [opt]
21209 GNU extension:
21211 type-specifier-seq:
21212 attributes type-specifier-seq [opt]
21214 If IS_DECLARATION is true, we are at the start of a "condition" or
21215 exception-declaration, so we might be followed by a declarator-id.
21217 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21218 i.e. we've just seen "->".
21220 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21222 static void
21223 cp_parser_type_specifier_seq (cp_parser* parser,
21224 bool is_declaration,
21225 bool is_trailing_return,
21226 cp_decl_specifier_seq *type_specifier_seq)
21228 bool seen_type_specifier = false;
21229 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21230 cp_token *start_token = NULL;
21232 /* Clear the TYPE_SPECIFIER_SEQ. */
21233 clear_decl_specs (type_specifier_seq);
21235 /* In the context of a trailing return type, enum E { } is an
21236 elaborated-type-specifier followed by a function-body, not an
21237 enum-specifier. */
21238 if (is_trailing_return)
21239 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21241 /* Parse the type-specifiers and attributes. */
21242 while (true)
21244 tree type_specifier;
21245 bool is_cv_qualifier;
21247 /* Check for attributes first. */
21248 if (cp_next_tokens_can_be_attribute_p (parser))
21250 type_specifier_seq->attributes
21251 = attr_chainon (type_specifier_seq->attributes,
21252 cp_parser_attributes_opt (parser));
21253 continue;
21256 /* record the token of the beginning of the type specifier seq,
21257 for error reporting purposes*/
21258 if (!start_token)
21259 start_token = cp_lexer_peek_token (parser->lexer);
21261 /* Look for the type-specifier. */
21262 type_specifier = cp_parser_type_specifier (parser,
21263 flags,
21264 type_specifier_seq,
21265 /*is_declaration=*/false,
21266 NULL,
21267 &is_cv_qualifier);
21268 if (!type_specifier)
21270 /* If the first type-specifier could not be found, this is not a
21271 type-specifier-seq at all. */
21272 if (!seen_type_specifier)
21274 /* Set in_declarator_p to avoid skipping to the semicolon. */
21275 int in_decl = parser->in_declarator_p;
21276 parser->in_declarator_p = true;
21278 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21279 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21280 cp_parser_error (parser, "expected type-specifier");
21282 parser->in_declarator_p = in_decl;
21284 type_specifier_seq->type = error_mark_node;
21285 return;
21287 /* If subsequent type-specifiers could not be found, the
21288 type-specifier-seq is complete. */
21289 break;
21292 seen_type_specifier = true;
21293 /* The standard says that a condition can be:
21295 type-specifier-seq declarator = assignment-expression
21297 However, given:
21299 struct S {};
21300 if (int S = ...)
21302 we should treat the "S" as a declarator, not as a
21303 type-specifier. The standard doesn't say that explicitly for
21304 type-specifier-seq, but it does say that for
21305 decl-specifier-seq in an ordinary declaration. Perhaps it
21306 would be clearer just to allow a decl-specifier-seq here, and
21307 then add a semantic restriction that if any decl-specifiers
21308 that are not type-specifiers appear, the program is invalid. */
21309 if (is_declaration && !is_cv_qualifier)
21310 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21314 /* Return whether the function currently being declared has an associated
21315 template parameter list. */
21317 static bool
21318 function_being_declared_is_template_p (cp_parser* parser)
21320 if (!current_template_parms || processing_template_parmlist)
21321 return false;
21323 if (parser->implicit_template_scope)
21324 return true;
21326 if (at_class_scope_p ()
21327 && TYPE_BEING_DEFINED (current_class_type))
21328 return parser->num_template_parameter_lists != 0;
21330 return ((int) parser->num_template_parameter_lists > template_class_depth
21331 (current_class_type));
21334 /* Parse a parameter-declaration-clause.
21336 parameter-declaration-clause:
21337 parameter-declaration-list [opt] ... [opt]
21338 parameter-declaration-list , ...
21340 Returns a representation for the parameter declarations. A return
21341 value of NULL indicates a parameter-declaration-clause consisting
21342 only of an ellipsis. */
21344 static tree
21345 cp_parser_parameter_declaration_clause (cp_parser* parser)
21347 tree parameters;
21348 cp_token *token;
21349 bool ellipsis_p;
21351 temp_override<bool> cleanup
21352 (parser->auto_is_implicit_function_template_parm_p);
21354 if (!processing_specialization
21355 && !processing_template_parmlist
21356 && !processing_explicit_instantiation
21357 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21358 actual function or a random abstract declarator. */
21359 && parser->default_arg_ok_p)
21360 if (!current_function_decl
21361 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21362 parser->auto_is_implicit_function_template_parm_p = true;
21364 /* Peek at the next token. */
21365 token = cp_lexer_peek_token (parser->lexer);
21366 /* Check for trivial parameter-declaration-clauses. */
21367 if (token->type == CPP_ELLIPSIS)
21369 /* Consume the `...' token. */
21370 cp_lexer_consume_token (parser->lexer);
21371 return NULL_TREE;
21373 else if (token->type == CPP_CLOSE_PAREN)
21374 /* There are no parameters. */
21375 return void_list_node;
21376 /* Check for `(void)', too, which is a special case. */
21377 else if (token->keyword == RID_VOID
21378 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21379 == CPP_CLOSE_PAREN))
21381 /* Consume the `void' token. */
21382 cp_lexer_consume_token (parser->lexer);
21383 /* There are no parameters. */
21384 return void_list_node;
21387 /* Parse the parameter-declaration-list. */
21388 parameters = cp_parser_parameter_declaration_list (parser);
21389 /* If a parse error occurred while parsing the
21390 parameter-declaration-list, then the entire
21391 parameter-declaration-clause is erroneous. */
21392 if (parameters == error_mark_node)
21393 return NULL_TREE;
21395 /* Peek at the next token. */
21396 token = cp_lexer_peek_token (parser->lexer);
21397 /* If it's a `,', the clause should terminate with an ellipsis. */
21398 if (token->type == CPP_COMMA)
21400 /* Consume the `,'. */
21401 cp_lexer_consume_token (parser->lexer);
21402 /* Expect an ellipsis. */
21403 ellipsis_p
21404 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21406 /* It might also be `...' if the optional trailing `,' was
21407 omitted. */
21408 else if (token->type == CPP_ELLIPSIS)
21410 /* Consume the `...' token. */
21411 cp_lexer_consume_token (parser->lexer);
21412 /* And remember that we saw it. */
21413 ellipsis_p = true;
21415 else
21416 ellipsis_p = false;
21418 /* Finish the parameter list. */
21419 if (!ellipsis_p)
21420 parameters = chainon (parameters, void_list_node);
21422 return parameters;
21425 /* Parse a parameter-declaration-list.
21427 parameter-declaration-list:
21428 parameter-declaration
21429 parameter-declaration-list , parameter-declaration
21431 Returns a representation of the parameter-declaration-list, as for
21432 cp_parser_parameter_declaration_clause. However, the
21433 `void_list_node' is never appended to the list. */
21435 static tree
21436 cp_parser_parameter_declaration_list (cp_parser* parser)
21438 tree parameters = NULL_TREE;
21439 tree *tail = &parameters;
21440 bool saved_in_unbraced_linkage_specification_p;
21441 int index = 0;
21443 /* The special considerations that apply to a function within an
21444 unbraced linkage specifications do not apply to the parameters
21445 to the function. */
21446 saved_in_unbraced_linkage_specification_p
21447 = parser->in_unbraced_linkage_specification_p;
21448 parser->in_unbraced_linkage_specification_p = false;
21450 /* Look for more parameters. */
21451 while (true)
21453 cp_parameter_declarator *parameter;
21454 tree decl = error_mark_node;
21455 bool parenthesized_p = false;
21457 /* Parse the parameter. */
21458 parameter
21459 = cp_parser_parameter_declaration (parser,
21460 /*template_parm_p=*/false,
21461 &parenthesized_p);
21463 /* We don't know yet if the enclosing context is deprecated, so wait
21464 and warn in grokparms if appropriate. */
21465 deprecated_state = DEPRECATED_SUPPRESS;
21467 if (parameter)
21469 decl = grokdeclarator (parameter->declarator,
21470 &parameter->decl_specifiers,
21471 PARM,
21472 parameter->default_argument != NULL_TREE,
21473 &parameter->decl_specifiers.attributes);
21474 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21475 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21478 deprecated_state = DEPRECATED_NORMAL;
21480 /* If a parse error occurred parsing the parameter declaration,
21481 then the entire parameter-declaration-list is erroneous. */
21482 if (decl == error_mark_node)
21484 parameters = error_mark_node;
21485 break;
21488 if (parameter->decl_specifiers.attributes)
21489 cplus_decl_attributes (&decl,
21490 parameter->decl_specifiers.attributes,
21492 if (DECL_NAME (decl))
21493 decl = pushdecl (decl);
21495 if (decl != error_mark_node)
21497 retrofit_lang_decl (decl);
21498 DECL_PARM_INDEX (decl) = ++index;
21499 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21502 /* Add the new parameter to the list. */
21503 *tail = build_tree_list (parameter->default_argument, decl);
21504 tail = &TREE_CHAIN (*tail);
21506 /* Peek at the next token. */
21507 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21508 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21509 /* These are for Objective-C++ */
21510 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21511 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21512 /* The parameter-declaration-list is complete. */
21513 break;
21514 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21516 cp_token *token;
21518 /* Peek at the next token. */
21519 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21520 /* If it's an ellipsis, then the list is complete. */
21521 if (token->type == CPP_ELLIPSIS)
21522 break;
21523 /* Otherwise, there must be more parameters. Consume the
21524 `,'. */
21525 cp_lexer_consume_token (parser->lexer);
21526 /* When parsing something like:
21528 int i(float f, double d)
21530 we can tell after seeing the declaration for "f" that we
21531 are not looking at an initialization of a variable "i",
21532 but rather at the declaration of a function "i".
21534 Due to the fact that the parsing of template arguments
21535 (as specified to a template-id) requires backtracking we
21536 cannot use this technique when inside a template argument
21537 list. */
21538 if (!parser->in_template_argument_list_p
21539 && !parser->in_type_id_in_expr_p
21540 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21541 /* However, a parameter-declaration of the form
21542 "float(f)" (which is a valid declaration of a
21543 parameter "f") can also be interpreted as an
21544 expression (the conversion of "f" to "float"). */
21545 && !parenthesized_p)
21546 cp_parser_commit_to_tentative_parse (parser);
21548 else
21550 cp_parser_error (parser, "expected %<,%> or %<...%>");
21551 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21552 cp_parser_skip_to_closing_parenthesis (parser,
21553 /*recovering=*/true,
21554 /*or_comma=*/false,
21555 /*consume_paren=*/false);
21556 break;
21560 parser->in_unbraced_linkage_specification_p
21561 = saved_in_unbraced_linkage_specification_p;
21563 /* Reset implicit_template_scope if we are about to leave the function
21564 parameter list that introduced it. Note that for out-of-line member
21565 definitions, there will be one or more class scopes before we get to
21566 the template parameter scope. */
21568 if (cp_binding_level *its = parser->implicit_template_scope)
21569 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21571 while (maybe_its->kind == sk_class)
21572 maybe_its = maybe_its->level_chain;
21573 if (maybe_its == its)
21575 parser->implicit_template_parms = 0;
21576 parser->implicit_template_scope = 0;
21580 return parameters;
21583 /* Parse a parameter declaration.
21585 parameter-declaration:
21586 decl-specifier-seq ... [opt] declarator
21587 decl-specifier-seq declarator = assignment-expression
21588 decl-specifier-seq ... [opt] abstract-declarator [opt]
21589 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21591 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21592 declares a template parameter. (In that case, a non-nested `>'
21593 token encountered during the parsing of the assignment-expression
21594 is not interpreted as a greater-than operator.)
21596 Returns a representation of the parameter, or NULL if an error
21597 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21598 true iff the declarator is of the form "(p)". */
21600 static cp_parameter_declarator *
21601 cp_parser_parameter_declaration (cp_parser *parser,
21602 bool template_parm_p,
21603 bool *parenthesized_p)
21605 int declares_class_or_enum;
21606 cp_decl_specifier_seq decl_specifiers;
21607 cp_declarator *declarator;
21608 tree default_argument;
21609 cp_token *token = NULL, *declarator_token_start = NULL;
21610 const char *saved_message;
21611 bool template_parameter_pack_p = false;
21613 /* In a template parameter, `>' is not an operator.
21615 [temp.param]
21617 When parsing a default template-argument for a non-type
21618 template-parameter, the first non-nested `>' is taken as the end
21619 of the template parameter-list rather than a greater-than
21620 operator. */
21622 /* Type definitions may not appear in parameter types. */
21623 saved_message = parser->type_definition_forbidden_message;
21624 parser->type_definition_forbidden_message
21625 = G_("types may not be defined in parameter types");
21627 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21628 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21629 (current_template_parms)) : 0);
21631 /* Parse the declaration-specifiers. */
21632 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21633 cp_parser_decl_specifier_seq (parser,
21634 CP_PARSER_FLAGS_NONE,
21635 &decl_specifiers,
21636 &declares_class_or_enum);
21638 /* Complain about missing 'typename' or other invalid type names. */
21639 if (!decl_specifiers.any_type_specifiers_p
21640 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21641 decl_specifiers.type = error_mark_node;
21643 /* If an error occurred, there's no reason to attempt to parse the
21644 rest of the declaration. */
21645 if (cp_parser_error_occurred (parser))
21647 parser->type_definition_forbidden_message = saved_message;
21648 return NULL;
21651 /* Peek at the next token. */
21652 token = cp_lexer_peek_token (parser->lexer);
21654 /* If the next token is a `)', `,', `=', `>', or `...', then there
21655 is no declarator. However, when variadic templates are enabled,
21656 there may be a declarator following `...'. */
21657 if (token->type == CPP_CLOSE_PAREN
21658 || token->type == CPP_COMMA
21659 || token->type == CPP_EQ
21660 || token->type == CPP_GREATER)
21662 declarator = NULL;
21663 if (parenthesized_p)
21664 *parenthesized_p = false;
21666 /* Otherwise, there should be a declarator. */
21667 else
21669 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21670 parser->default_arg_ok_p = false;
21672 /* After seeing a decl-specifier-seq, if the next token is not a
21673 "(", there is no possibility that the code is a valid
21674 expression. Therefore, if parsing tentatively, we commit at
21675 this point. */
21676 if (!parser->in_template_argument_list_p
21677 /* In an expression context, having seen:
21679 (int((char ...
21681 we cannot be sure whether we are looking at a
21682 function-type (taking a "char" as a parameter) or a cast
21683 of some object of type "char" to "int". */
21684 && !parser->in_type_id_in_expr_p
21685 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21686 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21687 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21688 cp_parser_commit_to_tentative_parse (parser);
21689 /* Parse the declarator. */
21690 declarator_token_start = token;
21691 declarator = cp_parser_declarator (parser,
21692 CP_PARSER_DECLARATOR_EITHER,
21693 /*ctor_dtor_or_conv_p=*/NULL,
21694 parenthesized_p,
21695 /*member_p=*/false,
21696 /*friend_p=*/false);
21697 parser->default_arg_ok_p = saved_default_arg_ok_p;
21698 /* After the declarator, allow more attributes. */
21699 decl_specifiers.attributes
21700 = attr_chainon (decl_specifiers.attributes,
21701 cp_parser_attributes_opt (parser));
21703 /* If the declarator is a template parameter pack, remember that and
21704 clear the flag in the declarator itself so we don't get errors
21705 from grokdeclarator. */
21706 if (template_parm_p && declarator && declarator->parameter_pack_p)
21708 declarator->parameter_pack_p = false;
21709 template_parameter_pack_p = true;
21713 /* If the next token is an ellipsis, and we have not seen a declarator
21714 name, and if either the type of the declarator contains parameter
21715 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21716 for, eg, abbreviated integral type names), then we actually have a
21717 parameter pack expansion expression. Otherwise, leave the ellipsis
21718 for a C-style variadic function. */
21719 token = cp_lexer_peek_token (parser->lexer);
21721 /* If a function parameter pack was specified and an implicit template
21722 parameter was introduced during cp_parser_parameter_declaration,
21723 change any implicit parameters introduced into packs. */
21724 if (parser->implicit_template_parms
21725 && ((token->type == CPP_ELLIPSIS
21726 && declarator_can_be_parameter_pack (declarator))
21727 || (declarator && declarator->parameter_pack_p)))
21729 int latest_template_parm_idx = TREE_VEC_LENGTH
21730 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21732 if (latest_template_parm_idx != template_parm_idx)
21733 decl_specifiers.type = convert_generic_types_to_packs
21734 (decl_specifiers.type,
21735 template_parm_idx, latest_template_parm_idx);
21738 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21740 tree type = decl_specifiers.type;
21742 if (type && DECL_P (type))
21743 type = TREE_TYPE (type);
21745 if (((type
21746 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21747 && (template_parm_p || uses_parameter_packs (type)))
21748 || (!type && template_parm_p))
21749 && declarator_can_be_parameter_pack (declarator))
21751 /* Consume the `...'. */
21752 cp_lexer_consume_token (parser->lexer);
21753 maybe_warn_variadic_templates ();
21755 /* Build a pack expansion type */
21756 if (template_parm_p)
21757 template_parameter_pack_p = true;
21758 else if (declarator)
21759 declarator->parameter_pack_p = true;
21760 else
21761 decl_specifiers.type = make_pack_expansion (type);
21765 /* The restriction on defining new types applies only to the type
21766 of the parameter, not to the default argument. */
21767 parser->type_definition_forbidden_message = saved_message;
21769 /* If the next token is `=', then process a default argument. */
21770 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21772 tree type = decl_specifiers.type;
21773 token = cp_lexer_peek_token (parser->lexer);
21774 /* If we are defining a class, then the tokens that make up the
21775 default argument must be saved and processed later. */
21776 if (!template_parm_p && at_class_scope_p ()
21777 && TYPE_BEING_DEFINED (current_class_type)
21778 && !LAMBDA_TYPE_P (current_class_type))
21779 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21781 // A constrained-type-specifier may declare a type template-parameter.
21782 else if (declares_constrained_type_template_parameter (type))
21783 default_argument
21784 = cp_parser_default_type_template_argument (parser);
21786 // A constrained-type-specifier may declare a template-template-parameter.
21787 else if (declares_constrained_template_template_parameter (type))
21788 default_argument
21789 = cp_parser_default_template_template_argument (parser);
21791 /* Outside of a class definition, we can just parse the
21792 assignment-expression. */
21793 else
21794 default_argument
21795 = cp_parser_default_argument (parser, template_parm_p);
21797 if (!parser->default_arg_ok_p)
21799 permerror (token->location,
21800 "default arguments are only "
21801 "permitted for function parameters");
21803 else if ((declarator && declarator->parameter_pack_p)
21804 || template_parameter_pack_p
21805 || (decl_specifiers.type
21806 && PACK_EXPANSION_P (decl_specifiers.type)))
21808 /* Find the name of the parameter pack. */
21809 cp_declarator *id_declarator = declarator;
21810 while (id_declarator && id_declarator->kind != cdk_id)
21811 id_declarator = id_declarator->declarator;
21813 if (id_declarator && id_declarator->kind == cdk_id)
21814 error_at (declarator_token_start->location,
21815 template_parm_p
21816 ? G_("template parameter pack %qD "
21817 "cannot have a default argument")
21818 : G_("parameter pack %qD cannot have "
21819 "a default argument"),
21820 id_declarator->u.id.unqualified_name);
21821 else
21822 error_at (declarator_token_start->location,
21823 template_parm_p
21824 ? G_("template parameter pack cannot have "
21825 "a default argument")
21826 : G_("parameter pack cannot have a "
21827 "default argument"));
21829 default_argument = NULL_TREE;
21832 else
21833 default_argument = NULL_TREE;
21835 /* Generate a location for the parameter, ranging from the start of the
21836 initial token to the end of the final token (using input_location for
21837 the latter, set up by cp_lexer_set_source_position_from_token when
21838 consuming tokens).
21840 If we have a identifier, then use it for the caret location, e.g.
21842 extern int callee (int one, int (*two)(int, int), float three);
21843 ~~~~~~^~~~~~~~~~~~~~
21845 otherwise, reuse the start location for the caret location e.g.:
21847 extern int callee (int one, int (*)(int, int), float three);
21848 ^~~~~~~~~~~~~~~~~
21851 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21852 ? declarator->id_loc
21853 : decl_spec_token_start->location);
21854 location_t param_loc = make_location (caret_loc,
21855 decl_spec_token_start->location,
21856 input_location);
21858 return make_parameter_declarator (&decl_specifiers,
21859 declarator,
21860 default_argument,
21861 param_loc,
21862 template_parameter_pack_p);
21865 /* Parse a default argument and return it.
21867 TEMPLATE_PARM_P is true if this is a default argument for a
21868 non-type template parameter. */
21869 static tree
21870 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21872 tree default_argument = NULL_TREE;
21873 bool saved_greater_than_is_operator_p;
21874 bool saved_local_variables_forbidden_p;
21875 bool non_constant_p, is_direct_init;
21877 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21878 set correctly. */
21879 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21880 parser->greater_than_is_operator_p = !template_parm_p;
21881 /* Local variable names (and the `this' keyword) may not
21882 appear in a default argument. */
21883 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21884 parser->local_variables_forbidden_p = true;
21885 /* Parse the assignment-expression. */
21886 if (template_parm_p)
21887 push_deferring_access_checks (dk_no_deferred);
21888 tree saved_class_ptr = NULL_TREE;
21889 tree saved_class_ref = NULL_TREE;
21890 /* The "this" pointer is not valid in a default argument. */
21891 if (cfun)
21893 saved_class_ptr = current_class_ptr;
21894 cp_function_chain->x_current_class_ptr = NULL_TREE;
21895 saved_class_ref = current_class_ref;
21896 cp_function_chain->x_current_class_ref = NULL_TREE;
21898 default_argument
21899 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21900 /* Restore the "this" pointer. */
21901 if (cfun)
21903 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21904 cp_function_chain->x_current_class_ref = saved_class_ref;
21906 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21907 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21908 if (template_parm_p)
21909 pop_deferring_access_checks ();
21910 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21911 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21913 return default_argument;
21916 /* Parse a function-body.
21918 function-body:
21919 compound_statement */
21921 static void
21922 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21924 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21925 ? BCS_TRY_BLOCK : BCS_NORMAL),
21926 true);
21929 /* Parse a ctor-initializer-opt followed by a function-body. Return
21930 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21931 is true we are parsing a function-try-block. */
21933 static void
21934 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21935 bool in_function_try_block)
21937 tree body, list;
21938 const bool check_body_p =
21939 DECL_CONSTRUCTOR_P (current_function_decl)
21940 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21941 tree last = NULL;
21943 /* Begin the function body. */
21944 body = begin_function_body ();
21945 /* Parse the optional ctor-initializer. */
21946 cp_parser_ctor_initializer_opt (parser);
21948 /* If we're parsing a constexpr constructor definition, we need
21949 to check that the constructor body is indeed empty. However,
21950 before we get to cp_parser_function_body lot of junk has been
21951 generated, so we can't just check that we have an empty block.
21952 Rather we take a snapshot of the outermost block, and check whether
21953 cp_parser_function_body changed its state. */
21954 if (check_body_p)
21956 list = cur_stmt_list;
21957 if (STATEMENT_LIST_TAIL (list))
21958 last = STATEMENT_LIST_TAIL (list)->stmt;
21960 /* Parse the function-body. */
21961 cp_parser_function_body (parser, in_function_try_block);
21962 if (check_body_p)
21963 check_constexpr_ctor_body (last, list, /*complain=*/true);
21964 /* Finish the function body. */
21965 finish_function_body (body);
21968 /* Parse an initializer.
21970 initializer:
21971 = initializer-clause
21972 ( expression-list )
21974 Returns an expression representing the initializer. If no
21975 initializer is present, NULL_TREE is returned.
21977 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21978 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21979 set to TRUE if there is no initializer present. If there is an
21980 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21981 is set to true; otherwise it is set to false. */
21983 static tree
21984 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21985 bool* non_constant_p, bool subexpression_p)
21987 cp_token *token;
21988 tree init;
21990 /* Peek at the next token. */
21991 token = cp_lexer_peek_token (parser->lexer);
21993 /* Let our caller know whether or not this initializer was
21994 parenthesized. */
21995 *is_direct_init = (token->type != CPP_EQ);
21996 /* Assume that the initializer is constant. */
21997 *non_constant_p = false;
21999 if (token->type == CPP_EQ)
22001 /* Consume the `='. */
22002 cp_lexer_consume_token (parser->lexer);
22003 /* Parse the initializer-clause. */
22004 init = cp_parser_initializer_clause (parser, non_constant_p);
22006 else if (token->type == CPP_OPEN_PAREN)
22008 vec<tree, va_gc> *vec;
22009 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22010 /*cast_p=*/false,
22011 /*allow_expansion_p=*/true,
22012 non_constant_p);
22013 if (vec == NULL)
22014 return error_mark_node;
22015 init = build_tree_list_vec (vec);
22016 release_tree_vector (vec);
22018 else if (token->type == CPP_OPEN_BRACE)
22020 cp_lexer_set_source_position (parser->lexer);
22021 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22022 init = cp_parser_braced_list (parser, non_constant_p);
22023 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22025 else
22027 /* Anything else is an error. */
22028 cp_parser_error (parser, "expected initializer");
22029 init = error_mark_node;
22032 if (!subexpression_p && check_for_bare_parameter_packs (init))
22033 init = error_mark_node;
22035 return init;
22038 /* Parse an initializer-clause.
22040 initializer-clause:
22041 assignment-expression
22042 braced-init-list
22044 Returns an expression representing the initializer.
22046 If the `assignment-expression' production is used the value
22047 returned is simply a representation for the expression.
22049 Otherwise, calls cp_parser_braced_list. */
22051 static cp_expr
22052 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22054 cp_expr initializer;
22056 /* Assume the expression is constant. */
22057 *non_constant_p = false;
22059 /* If it is not a `{', then we are looking at an
22060 assignment-expression. */
22061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22063 initializer
22064 = cp_parser_constant_expression (parser,
22065 /*allow_non_constant_p=*/true,
22066 non_constant_p);
22068 else
22069 initializer = cp_parser_braced_list (parser, non_constant_p);
22071 return initializer;
22074 /* Parse a brace-enclosed initializer list.
22076 braced-init-list:
22077 { initializer-list , [opt] }
22078 { designated-initializer-list , [opt] }
22081 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22082 the elements of the initializer-list (or NULL, if the last
22083 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22084 NULL_TREE. There is no way to detect whether or not the optional
22085 trailing `,' was provided. NON_CONSTANT_P is as for
22086 cp_parser_initializer. */
22088 static cp_expr
22089 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22091 tree initializer;
22092 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22094 /* Consume the `{' token. */
22095 matching_braces braces;
22096 braces.require_open (parser);
22097 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22098 initializer = make_node (CONSTRUCTOR);
22099 /* If it's not a `}', then there is a non-trivial initializer. */
22100 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22102 /* Parse the initializer list. */
22103 CONSTRUCTOR_ELTS (initializer)
22104 = cp_parser_initializer_list (parser, non_constant_p);
22105 /* A trailing `,' token is allowed. */
22106 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22107 cp_lexer_consume_token (parser->lexer);
22109 else
22110 *non_constant_p = false;
22111 /* Now, there should be a trailing `}'. */
22112 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22113 braces.require_close (parser);
22114 TREE_TYPE (initializer) = init_list_type_node;
22116 cp_expr result (initializer);
22117 /* Build a location of the form:
22118 { ... }
22119 ^~~~~~~
22120 with caret==start at the open brace, finish at the close brace. */
22121 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22122 result.set_location (combined_loc);
22123 return result;
22126 /* Consume tokens up to, and including, the next non-nested closing `]'.
22127 Returns true iff we found a closing `]'. */
22129 static bool
22130 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22132 unsigned square_depth = 0;
22134 while (true)
22136 cp_token * token = cp_lexer_peek_token (parser->lexer);
22138 switch (token->type)
22140 case CPP_EOF:
22141 case CPP_PRAGMA_EOL:
22142 /* If we've run out of tokens, then there is no closing `]'. */
22143 return false;
22145 case CPP_OPEN_SQUARE:
22146 ++square_depth;
22147 break;
22149 case CPP_CLOSE_SQUARE:
22150 if (!square_depth--)
22152 cp_lexer_consume_token (parser->lexer);
22153 return true;
22155 break;
22157 default:
22158 break;
22161 /* Consume the token. */
22162 cp_lexer_consume_token (parser->lexer);
22166 /* Return true if we are looking at an array-designator, false otherwise. */
22168 static bool
22169 cp_parser_array_designator_p (cp_parser *parser)
22171 /* Consume the `['. */
22172 cp_lexer_consume_token (parser->lexer);
22174 cp_lexer_save_tokens (parser->lexer);
22176 /* Skip tokens until the next token is a closing square bracket.
22177 If we find the closing `]', and the next token is a `=', then
22178 we are looking at an array designator. */
22179 bool array_designator_p
22180 = (cp_parser_skip_to_closing_square_bracket (parser)
22181 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22183 /* Roll back the tokens we skipped. */
22184 cp_lexer_rollback_tokens (parser->lexer);
22186 return array_designator_p;
22189 /* Parse an initializer-list.
22191 initializer-list:
22192 initializer-clause ... [opt]
22193 initializer-list , initializer-clause ... [opt]
22195 C++2A Extension:
22197 designated-initializer-list:
22198 designated-initializer-clause
22199 designated-initializer-list , designated-initializer-clause
22201 designated-initializer-clause:
22202 designator brace-or-equal-initializer
22204 designator:
22205 . identifier
22207 GNU Extension:
22209 initializer-list:
22210 designation initializer-clause ...[opt]
22211 initializer-list , designation initializer-clause ...[opt]
22213 designation:
22214 . identifier =
22215 identifier :
22216 [ constant-expression ] =
22218 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22219 for the initializer. If the INDEX of the elt is non-NULL, it is the
22220 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22221 as for cp_parser_initializer. */
22223 static vec<constructor_elt, va_gc> *
22224 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22226 vec<constructor_elt, va_gc> *v = NULL;
22227 bool first_p = true;
22228 tree first_designator = NULL_TREE;
22230 /* Assume all of the expressions are constant. */
22231 *non_constant_p = false;
22233 /* Parse the rest of the list. */
22234 while (true)
22236 cp_token *token;
22237 tree designator;
22238 tree initializer;
22239 bool clause_non_constant_p;
22240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22242 /* Handle the C++2A syntax, '. id ='. */
22243 if ((cxx_dialect >= cxx2a
22244 || cp_parser_allow_gnu_extensions_p (parser))
22245 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22246 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22247 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22248 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22249 == CPP_OPEN_BRACE)))
22251 if (cxx_dialect < cxx2a)
22252 pedwarn (loc, OPT_Wpedantic,
22253 "C++ designated initializers only available with "
22254 "-std=c++2a or -std=gnu++2a");
22255 /* Consume the `.'. */
22256 cp_lexer_consume_token (parser->lexer);
22257 /* Consume the identifier. */
22258 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22259 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22260 /* Consume the `='. */
22261 cp_lexer_consume_token (parser->lexer);
22263 /* Also, if the next token is an identifier and the following one is a
22264 colon, we are looking at the GNU designated-initializer
22265 syntax. */
22266 else if (cp_parser_allow_gnu_extensions_p (parser)
22267 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22268 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22269 == CPP_COLON))
22271 /* Warn the user that they are using an extension. */
22272 pedwarn (loc, OPT_Wpedantic,
22273 "ISO C++ does not allow GNU designated initializers");
22274 /* Consume the identifier. */
22275 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22276 /* Consume the `:'. */
22277 cp_lexer_consume_token (parser->lexer);
22279 /* Also handle C99 array designators, '[ const ] ='. */
22280 else if (cp_parser_allow_gnu_extensions_p (parser)
22281 && !c_dialect_objc ()
22282 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22284 /* In C++11, [ could start a lambda-introducer. */
22285 bool non_const = false;
22287 cp_parser_parse_tentatively (parser);
22289 if (!cp_parser_array_designator_p (parser))
22291 cp_parser_simulate_error (parser);
22292 designator = NULL_TREE;
22294 else
22296 designator = cp_parser_constant_expression (parser, true,
22297 &non_const);
22298 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22299 cp_parser_require (parser, CPP_EQ, RT_EQ);
22302 if (!cp_parser_parse_definitely (parser))
22303 designator = NULL_TREE;
22304 else if (non_const
22305 && (!require_potential_rvalue_constant_expression
22306 (designator)))
22307 designator = NULL_TREE;
22308 if (designator)
22309 /* Warn the user that they are using an extension. */
22310 pedwarn (loc, OPT_Wpedantic,
22311 "ISO C++ does not allow C99 designated initializers");
22313 else
22314 designator = NULL_TREE;
22316 if (first_p)
22318 first_designator = designator;
22319 first_p = false;
22321 else if (cxx_dialect >= cxx2a
22322 && first_designator != error_mark_node
22323 && (!first_designator != !designator))
22325 error_at (loc, "either all initializer clauses should be designated "
22326 "or none of them should be");
22327 first_designator = error_mark_node;
22329 else if (cxx_dialect < cxx2a && !first_designator)
22330 first_designator = designator;
22332 /* Parse the initializer. */
22333 initializer = cp_parser_initializer_clause (parser,
22334 &clause_non_constant_p);
22335 /* If any clause is non-constant, so is the entire initializer. */
22336 if (clause_non_constant_p)
22337 *non_constant_p = true;
22339 /* If we have an ellipsis, this is an initializer pack
22340 expansion. */
22341 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22343 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22345 /* Consume the `...'. */
22346 cp_lexer_consume_token (parser->lexer);
22348 if (designator && cxx_dialect >= cxx2a)
22349 error_at (loc,
22350 "%<...%> not allowed in designated initializer list");
22352 /* Turn the initializer into an initializer expansion. */
22353 initializer = make_pack_expansion (initializer);
22356 /* Add it to the vector. */
22357 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22359 /* If the next token is not a comma, we have reached the end of
22360 the list. */
22361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22362 break;
22364 /* Peek at the next token. */
22365 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22366 /* If the next token is a `}', then we're still done. An
22367 initializer-clause can have a trailing `,' after the
22368 initializer-list and before the closing `}'. */
22369 if (token->type == CPP_CLOSE_BRACE)
22370 break;
22372 /* Consume the `,' token. */
22373 cp_lexer_consume_token (parser->lexer);
22376 /* The same identifier shall not appear in multiple designators
22377 of a designated-initializer-list. */
22378 if (first_designator)
22380 unsigned int i;
22381 tree designator, val;
22382 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22383 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22385 if (IDENTIFIER_MARKED (designator))
22387 error_at (cp_expr_loc_or_loc (val, input_location),
22388 "%<.%s%> designator used multiple times in "
22389 "the same initializer list",
22390 IDENTIFIER_POINTER (designator));
22391 (*v)[i].index = error_mark_node;
22393 else
22394 IDENTIFIER_MARKED (designator) = 1;
22396 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22397 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22398 IDENTIFIER_MARKED (designator) = 0;
22401 return v;
22404 /* Classes [gram.class] */
22406 /* Parse a class-name.
22408 class-name:
22409 identifier
22410 template-id
22412 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22413 to indicate that names looked up in dependent types should be
22414 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22415 keyword has been used to indicate that the name that appears next
22416 is a template. TAG_TYPE indicates the explicit tag given before
22417 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22418 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22419 is the class being defined in a class-head. If ENUM_OK is TRUE,
22420 enum-names are also accepted.
22422 Returns the TYPE_DECL representing the class. */
22424 static tree
22425 cp_parser_class_name (cp_parser *parser,
22426 bool typename_keyword_p,
22427 bool template_keyword_p,
22428 enum tag_types tag_type,
22429 bool check_dependency_p,
22430 bool class_head_p,
22431 bool is_declaration,
22432 bool enum_ok)
22434 tree decl;
22435 tree scope;
22436 bool typename_p;
22437 cp_token *token;
22438 tree identifier = NULL_TREE;
22440 /* All class-names start with an identifier. */
22441 token = cp_lexer_peek_token (parser->lexer);
22442 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22444 cp_parser_error (parser, "expected class-name");
22445 return error_mark_node;
22448 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22449 to a template-id, so we save it here. */
22450 scope = parser->scope;
22451 if (scope == error_mark_node)
22452 return error_mark_node;
22454 /* Any name names a type if we're following the `typename' keyword
22455 in a qualified name where the enclosing scope is type-dependent. */
22456 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22457 && dependent_type_p (scope));
22458 /* Handle the common case (an identifier, but not a template-id)
22459 efficiently. */
22460 if (token->type == CPP_NAME
22461 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22463 cp_token *identifier_token;
22464 bool ambiguous_p;
22466 /* Look for the identifier. */
22467 identifier_token = cp_lexer_peek_token (parser->lexer);
22468 ambiguous_p = identifier_token->error_reported;
22469 identifier = cp_parser_identifier (parser);
22470 /* If the next token isn't an identifier, we are certainly not
22471 looking at a class-name. */
22472 if (identifier == error_mark_node)
22473 decl = error_mark_node;
22474 /* If we know this is a type-name, there's no need to look it
22475 up. */
22476 else if (typename_p)
22477 decl = identifier;
22478 else
22480 tree ambiguous_decls;
22481 /* If we already know that this lookup is ambiguous, then
22482 we've already issued an error message; there's no reason
22483 to check again. */
22484 if (ambiguous_p)
22486 cp_parser_simulate_error (parser);
22487 return error_mark_node;
22489 /* If the next token is a `::', then the name must be a type
22490 name.
22492 [basic.lookup.qual]
22494 During the lookup for a name preceding the :: scope
22495 resolution operator, object, function, and enumerator
22496 names are ignored. */
22497 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22498 tag_type = scope_type;
22499 /* Look up the name. */
22500 decl = cp_parser_lookup_name (parser, identifier,
22501 tag_type,
22502 /*is_template=*/false,
22503 /*is_namespace=*/false,
22504 check_dependency_p,
22505 &ambiguous_decls,
22506 identifier_token->location);
22507 if (ambiguous_decls)
22509 if (cp_parser_parsing_tentatively (parser))
22510 cp_parser_simulate_error (parser);
22511 return error_mark_node;
22515 else
22517 /* Try a template-id. */
22518 decl = cp_parser_template_id (parser, template_keyword_p,
22519 check_dependency_p,
22520 tag_type,
22521 is_declaration);
22522 if (decl == error_mark_node)
22523 return error_mark_node;
22526 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22528 /* If this is a typename, create a TYPENAME_TYPE. */
22529 if (typename_p && decl != error_mark_node)
22531 decl = make_typename_type (scope, decl, typename_type,
22532 /*complain=*/tf_error);
22533 if (decl != error_mark_node)
22534 decl = TYPE_NAME (decl);
22537 decl = strip_using_decl (decl);
22539 /* Check to see that it is really the name of a class. */
22540 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22541 && identifier_p (TREE_OPERAND (decl, 0))
22542 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22543 /* Situations like this:
22545 template <typename T> struct A {
22546 typename T::template X<int>::I i;
22549 are problematic. Is `T::template X<int>' a class-name? The
22550 standard does not seem to be definitive, but there is no other
22551 valid interpretation of the following `::'. Therefore, those
22552 names are considered class-names. */
22554 decl = make_typename_type (scope, decl, tag_type, tf_error);
22555 if (decl != error_mark_node)
22556 decl = TYPE_NAME (decl);
22558 else if (TREE_CODE (decl) != TYPE_DECL
22559 || TREE_TYPE (decl) == error_mark_node
22560 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22561 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22562 /* In Objective-C 2.0, a classname followed by '.' starts a
22563 dot-syntax expression, and it's not a type-name. */
22564 || (c_dialect_objc ()
22565 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22566 && objc_is_class_name (decl)))
22567 decl = error_mark_node;
22569 if (decl == error_mark_node)
22570 cp_parser_error (parser, "expected class-name");
22571 else if (identifier && !parser->scope)
22572 maybe_note_name_used_in_class (identifier, decl);
22574 return decl;
22577 /* Parse a class-specifier.
22579 class-specifier:
22580 class-head { member-specification [opt] }
22582 Returns the TREE_TYPE representing the class. */
22584 static tree
22585 cp_parser_class_specifier_1 (cp_parser* parser)
22587 tree type;
22588 tree attributes = NULL_TREE;
22589 bool nested_name_specifier_p;
22590 unsigned saved_num_template_parameter_lists;
22591 bool saved_in_function_body;
22592 unsigned char in_statement;
22593 bool in_switch_statement_p;
22594 bool saved_in_unbraced_linkage_specification_p;
22595 tree old_scope = NULL_TREE;
22596 tree scope = NULL_TREE;
22597 cp_token *closing_brace;
22599 push_deferring_access_checks (dk_no_deferred);
22601 /* Parse the class-head. */
22602 type = cp_parser_class_head (parser,
22603 &nested_name_specifier_p);
22604 /* If the class-head was a semantic disaster, skip the entire body
22605 of the class. */
22606 if (!type)
22608 cp_parser_skip_to_end_of_block_or_statement (parser);
22609 pop_deferring_access_checks ();
22610 return error_mark_node;
22613 /* Look for the `{'. */
22614 matching_braces braces;
22615 if (!braces.require_open (parser))
22617 pop_deferring_access_checks ();
22618 return error_mark_node;
22621 cp_ensure_no_omp_declare_simd (parser);
22622 cp_ensure_no_oacc_routine (parser);
22624 /* Issue an error message if type-definitions are forbidden here. */
22625 cp_parser_check_type_definition (parser);
22626 /* Remember that we are defining one more class. */
22627 ++parser->num_classes_being_defined;
22628 /* Inside the class, surrounding template-parameter-lists do not
22629 apply. */
22630 saved_num_template_parameter_lists
22631 = parser->num_template_parameter_lists;
22632 parser->num_template_parameter_lists = 0;
22633 /* We are not in a function body. */
22634 saved_in_function_body = parser->in_function_body;
22635 parser->in_function_body = false;
22636 /* Or in a loop. */
22637 in_statement = parser->in_statement;
22638 parser->in_statement = 0;
22639 /* Or in a switch. */
22640 in_switch_statement_p = parser->in_switch_statement_p;
22641 parser->in_switch_statement_p = false;
22642 /* We are not immediately inside an extern "lang" block. */
22643 saved_in_unbraced_linkage_specification_p
22644 = parser->in_unbraced_linkage_specification_p;
22645 parser->in_unbraced_linkage_specification_p = false;
22647 // Associate constraints with the type.
22648 if (flag_concepts)
22649 type = associate_classtype_constraints (type);
22651 /* Start the class. */
22652 if (nested_name_specifier_p)
22654 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22655 old_scope = push_inner_scope (scope);
22657 type = begin_class_definition (type);
22659 if (type == error_mark_node)
22660 /* If the type is erroneous, skip the entire body of the class. */
22661 cp_parser_skip_to_closing_brace (parser);
22662 else
22663 /* Parse the member-specification. */
22664 cp_parser_member_specification_opt (parser);
22666 /* Look for the trailing `}'. */
22667 closing_brace = braces.require_close (parser);
22668 /* Look for trailing attributes to apply to this class. */
22669 if (cp_parser_allow_gnu_extensions_p (parser))
22670 attributes = cp_parser_gnu_attributes_opt (parser);
22671 if (type != error_mark_node)
22672 type = finish_struct (type, attributes);
22673 if (nested_name_specifier_p)
22674 pop_inner_scope (old_scope, scope);
22676 /* We've finished a type definition. Check for the common syntax
22677 error of forgetting a semicolon after the definition. We need to
22678 be careful, as we can't just check for not-a-semicolon and be done
22679 with it; the user might have typed:
22681 class X { } c = ...;
22682 class X { } *p = ...;
22684 and so forth. Instead, enumerate all the possible tokens that
22685 might follow this production; if we don't see one of them, then
22686 complain and silently insert the semicolon. */
22688 cp_token *token = cp_lexer_peek_token (parser->lexer);
22689 bool want_semicolon = true;
22691 if (cp_next_tokens_can_be_std_attribute_p (parser))
22692 /* Don't try to parse c++11 attributes here. As per the
22693 grammar, that should be a task for
22694 cp_parser_decl_specifier_seq. */
22695 want_semicolon = false;
22697 switch (token->type)
22699 case CPP_NAME:
22700 case CPP_SEMICOLON:
22701 case CPP_MULT:
22702 case CPP_AND:
22703 case CPP_OPEN_PAREN:
22704 case CPP_CLOSE_PAREN:
22705 case CPP_COMMA:
22706 want_semicolon = false;
22707 break;
22709 /* While it's legal for type qualifiers and storage class
22710 specifiers to follow type definitions in the grammar, only
22711 compiler testsuites contain code like that. Assume that if
22712 we see such code, then what we're really seeing is a case
22713 like:
22715 class X { }
22716 const <type> var = ...;
22720 class Y { }
22721 static <type> func (...) ...
22723 i.e. the qualifier or specifier applies to the next
22724 declaration. To do so, however, we need to look ahead one
22725 more token to see if *that* token is a type specifier.
22727 This code could be improved to handle:
22729 class Z { }
22730 static const <type> var = ...; */
22731 case CPP_KEYWORD:
22732 if (keyword_is_decl_specifier (token->keyword))
22734 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22736 /* Handling user-defined types here would be nice, but very
22737 tricky. */
22738 want_semicolon
22739 = (lookahead->type == CPP_KEYWORD
22740 && keyword_begins_type_specifier (lookahead->keyword));
22742 break;
22743 default:
22744 break;
22747 /* If we don't have a type, then something is very wrong and we
22748 shouldn't try to do anything clever. Likewise for not seeing the
22749 closing brace. */
22750 if (closing_brace && TYPE_P (type) && want_semicolon)
22752 /* Locate the closing brace. */
22753 cp_token_position prev
22754 = cp_lexer_previous_token_position (parser->lexer);
22755 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22756 location_t loc = prev_token->location;
22758 /* We want to suggest insertion of a ';' immediately *after* the
22759 closing brace, so, if we can, offset the location by 1 column. */
22760 location_t next_loc = loc;
22761 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22762 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22764 rich_location richloc (line_table, next_loc);
22766 /* If we successfully offset the location, suggest the fix-it. */
22767 if (next_loc != loc)
22768 richloc.add_fixit_insert_before (next_loc, ";");
22770 if (CLASSTYPE_DECLARED_CLASS (type))
22771 error_at (&richloc,
22772 "expected %<;%> after class definition");
22773 else if (TREE_CODE (type) == RECORD_TYPE)
22774 error_at (&richloc,
22775 "expected %<;%> after struct definition");
22776 else if (TREE_CODE (type) == UNION_TYPE)
22777 error_at (&richloc,
22778 "expected %<;%> after union definition");
22779 else
22780 gcc_unreachable ();
22782 /* Unget one token and smash it to look as though we encountered
22783 a semicolon in the input stream. */
22784 cp_lexer_set_token_position (parser->lexer, prev);
22785 token = cp_lexer_peek_token (parser->lexer);
22786 token->type = CPP_SEMICOLON;
22787 token->keyword = RID_MAX;
22791 /* If this class is not itself within the scope of another class,
22792 then we need to parse the bodies of all of the queued function
22793 definitions. Note that the queued functions defined in a class
22794 are not always processed immediately following the
22795 class-specifier for that class. Consider:
22797 struct A {
22798 struct B { void f() { sizeof (A); } };
22801 If `f' were processed before the processing of `A' were
22802 completed, there would be no way to compute the size of `A'.
22803 Note that the nesting we are interested in here is lexical --
22804 not the semantic nesting given by TYPE_CONTEXT. In particular,
22805 for:
22807 struct A { struct B; };
22808 struct A::B { void f() { } };
22810 there is no need to delay the parsing of `A::B::f'. */
22811 if (--parser->num_classes_being_defined == 0)
22813 tree decl;
22814 tree class_type = NULL_TREE;
22815 tree pushed_scope = NULL_TREE;
22816 unsigned ix;
22817 cp_default_arg_entry *e;
22818 tree save_ccp, save_ccr;
22820 if (any_erroneous_template_args_p (type))
22822 /* Skip default arguments, NSDMIs, etc, in order to improve
22823 error recovery (c++/71169, c++/71832). */
22824 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22825 vec_safe_truncate (unparsed_nsdmis, 0);
22826 vec_safe_truncate (unparsed_classes, 0);
22827 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22830 /* In a first pass, parse default arguments to the functions.
22831 Then, in a second pass, parse the bodies of the functions.
22832 This two-phased approach handles cases like:
22834 struct S {
22835 void f() { g(); }
22836 void g(int i = 3);
22840 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22842 decl = e->decl;
22843 /* If there are default arguments that have not yet been processed,
22844 take care of them now. */
22845 if (class_type != e->class_type)
22847 if (pushed_scope)
22848 pop_scope (pushed_scope);
22849 class_type = e->class_type;
22850 pushed_scope = push_scope (class_type);
22852 /* Make sure that any template parameters are in scope. */
22853 maybe_begin_member_template_processing (decl);
22854 /* Parse the default argument expressions. */
22855 cp_parser_late_parsing_default_args (parser, decl);
22856 /* Remove any template parameters from the symbol table. */
22857 maybe_end_member_template_processing ();
22859 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22860 /* Now parse any NSDMIs. */
22861 save_ccp = current_class_ptr;
22862 save_ccr = current_class_ref;
22863 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22865 if (class_type != DECL_CONTEXT (decl))
22867 if (pushed_scope)
22868 pop_scope (pushed_scope);
22869 class_type = DECL_CONTEXT (decl);
22870 pushed_scope = push_scope (class_type);
22872 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22873 cp_parser_late_parsing_nsdmi (parser, decl);
22875 vec_safe_truncate (unparsed_nsdmis, 0);
22876 current_class_ptr = save_ccp;
22877 current_class_ref = save_ccr;
22878 if (pushed_scope)
22879 pop_scope (pushed_scope);
22881 /* Now do some post-NSDMI bookkeeping. */
22882 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22883 after_nsdmi_defaulted_late_checks (class_type);
22884 vec_safe_truncate (unparsed_classes, 0);
22885 after_nsdmi_defaulted_late_checks (type);
22887 /* Now parse the body of the functions. */
22888 if (flag_openmp)
22890 /* OpenMP UDRs need to be parsed before all other functions. */
22891 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22892 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22893 cp_parser_late_parsing_for_member (parser, decl);
22894 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22895 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22896 cp_parser_late_parsing_for_member (parser, decl);
22898 else
22899 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22900 cp_parser_late_parsing_for_member (parser, decl);
22901 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22903 else
22904 vec_safe_push (unparsed_classes, type);
22906 /* Put back any saved access checks. */
22907 pop_deferring_access_checks ();
22909 /* Restore saved state. */
22910 parser->in_switch_statement_p = in_switch_statement_p;
22911 parser->in_statement = in_statement;
22912 parser->in_function_body = saved_in_function_body;
22913 parser->num_template_parameter_lists
22914 = saved_num_template_parameter_lists;
22915 parser->in_unbraced_linkage_specification_p
22916 = saved_in_unbraced_linkage_specification_p;
22918 return type;
22921 static tree
22922 cp_parser_class_specifier (cp_parser* parser)
22924 tree ret;
22925 timevar_push (TV_PARSE_STRUCT);
22926 ret = cp_parser_class_specifier_1 (parser);
22927 timevar_pop (TV_PARSE_STRUCT);
22928 return ret;
22931 /* Parse a class-head.
22933 class-head:
22934 class-key identifier [opt] base-clause [opt]
22935 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22936 class-key nested-name-specifier [opt] template-id
22937 base-clause [opt]
22939 class-virt-specifier:
22940 final
22942 GNU Extensions:
22943 class-key attributes identifier [opt] base-clause [opt]
22944 class-key attributes nested-name-specifier identifier base-clause [opt]
22945 class-key attributes nested-name-specifier [opt] template-id
22946 base-clause [opt]
22948 Upon return BASES is initialized to the list of base classes (or
22949 NULL, if there are none) in the same form returned by
22950 cp_parser_base_clause.
22952 Returns the TYPE of the indicated class. Sets
22953 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22954 involving a nested-name-specifier was used, and FALSE otherwise.
22956 Returns error_mark_node if this is not a class-head.
22958 Returns NULL_TREE if the class-head is syntactically valid, but
22959 semantically invalid in a way that means we should skip the entire
22960 body of the class. */
22962 static tree
22963 cp_parser_class_head (cp_parser* parser,
22964 bool* nested_name_specifier_p)
22966 tree nested_name_specifier;
22967 enum tag_types class_key;
22968 tree id = NULL_TREE;
22969 tree type = NULL_TREE;
22970 tree attributes;
22971 tree bases;
22972 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22973 bool template_id_p = false;
22974 bool qualified_p = false;
22975 bool invalid_nested_name_p = false;
22976 bool invalid_explicit_specialization_p = false;
22977 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22978 tree pushed_scope = NULL_TREE;
22979 unsigned num_templates;
22980 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22981 /* Assume no nested-name-specifier will be present. */
22982 *nested_name_specifier_p = false;
22983 /* Assume no template parameter lists will be used in defining the
22984 type. */
22985 num_templates = 0;
22986 parser->colon_corrects_to_scope_p = false;
22988 /* Look for the class-key. */
22989 class_key = cp_parser_class_key (parser);
22990 if (class_key == none_type)
22991 return error_mark_node;
22993 location_t class_head_start_location = input_location;
22995 /* Parse the attributes. */
22996 attributes = cp_parser_attributes_opt (parser);
22998 /* If the next token is `::', that is invalid -- but sometimes
22999 people do try to write:
23001 struct ::S {};
23003 Handle this gracefully by accepting the extra qualifier, and then
23004 issuing an error about it later if this really is a
23005 class-head. If it turns out just to be an elaborated type
23006 specifier, remain silent. */
23007 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23008 qualified_p = true;
23010 push_deferring_access_checks (dk_no_check);
23012 /* Determine the name of the class. Begin by looking for an
23013 optional nested-name-specifier. */
23014 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23015 nested_name_specifier
23016 = cp_parser_nested_name_specifier_opt (parser,
23017 /*typename_keyword_p=*/false,
23018 /*check_dependency_p=*/false,
23019 /*type_p=*/true,
23020 /*is_declaration=*/false);
23021 /* If there was a nested-name-specifier, then there *must* be an
23022 identifier. */
23024 cp_token *bad_template_keyword = NULL;
23026 if (nested_name_specifier)
23028 type_start_token = cp_lexer_peek_token (parser->lexer);
23029 /* Although the grammar says `identifier', it really means
23030 `class-name' or `template-name'. You are only allowed to
23031 define a class that has already been declared with this
23032 syntax.
23034 The proposed resolution for Core Issue 180 says that wherever
23035 you see `class T::X' you should treat `X' as a type-name.
23037 It is OK to define an inaccessible class; for example:
23039 class A { class B; };
23040 class A::B {};
23042 We do not know if we will see a class-name, or a
23043 template-name. We look for a class-name first, in case the
23044 class-name is a template-id; if we looked for the
23045 template-name first we would stop after the template-name. */
23046 cp_parser_parse_tentatively (parser);
23047 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23048 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23049 type = cp_parser_class_name (parser,
23050 /*typename_keyword_p=*/false,
23051 /*template_keyword_p=*/false,
23052 class_type,
23053 /*check_dependency_p=*/false,
23054 /*class_head_p=*/true,
23055 /*is_declaration=*/false);
23056 /* If that didn't work, ignore the nested-name-specifier. */
23057 if (!cp_parser_parse_definitely (parser))
23059 invalid_nested_name_p = true;
23060 type_start_token = cp_lexer_peek_token (parser->lexer);
23061 id = cp_parser_identifier (parser);
23062 if (id == error_mark_node)
23063 id = NULL_TREE;
23065 /* If we could not find a corresponding TYPE, treat this
23066 declaration like an unqualified declaration. */
23067 if (type == error_mark_node)
23068 nested_name_specifier = NULL_TREE;
23069 /* Otherwise, count the number of templates used in TYPE and its
23070 containing scopes. */
23071 else
23072 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23074 /* Otherwise, the identifier is optional. */
23075 else
23077 /* We don't know whether what comes next is a template-id,
23078 an identifier, or nothing at all. */
23079 cp_parser_parse_tentatively (parser);
23080 /* Check for a template-id. */
23081 type_start_token = cp_lexer_peek_token (parser->lexer);
23082 id = cp_parser_template_id (parser,
23083 /*template_keyword_p=*/false,
23084 /*check_dependency_p=*/true,
23085 class_key,
23086 /*is_declaration=*/true);
23087 /* If that didn't work, it could still be an identifier. */
23088 if (!cp_parser_parse_definitely (parser))
23090 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23092 type_start_token = cp_lexer_peek_token (parser->lexer);
23093 id = cp_parser_identifier (parser);
23095 else
23096 id = NULL_TREE;
23098 else
23100 template_id_p = true;
23101 ++num_templates;
23105 pop_deferring_access_checks ();
23107 if (id)
23109 cp_parser_check_for_invalid_template_id (parser, id,
23110 class_key,
23111 type_start_token->location);
23113 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23115 /* If it's not a `:' or a `{' then we can't really be looking at a
23116 class-head, since a class-head only appears as part of a
23117 class-specifier. We have to detect this situation before calling
23118 xref_tag, since that has irreversible side-effects. */
23119 if (!cp_parser_next_token_starts_class_definition_p (parser))
23121 cp_parser_error (parser, "expected %<{%> or %<:%>");
23122 type = error_mark_node;
23123 goto out;
23126 /* At this point, we're going ahead with the class-specifier, even
23127 if some other problem occurs. */
23128 cp_parser_commit_to_tentative_parse (parser);
23129 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23131 cp_parser_error (parser,
23132 "cannot specify %<override%> for a class");
23133 type = error_mark_node;
23134 goto out;
23136 /* Issue the error about the overly-qualified name now. */
23137 if (qualified_p)
23139 cp_parser_error (parser,
23140 "global qualification of class name is invalid");
23141 type = error_mark_node;
23142 goto out;
23144 else if (invalid_nested_name_p)
23146 cp_parser_error (parser,
23147 "qualified name does not name a class");
23148 type = error_mark_node;
23149 goto out;
23151 else if (nested_name_specifier)
23153 tree scope;
23155 if (bad_template_keyword)
23156 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23157 keyword template shall not appear at the top level. */
23158 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23159 "keyword %<template%> not allowed in class-head-name");
23161 /* Reject typedef-names in class heads. */
23162 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23164 error_at (type_start_token->location,
23165 "invalid class name in declaration of %qD",
23166 type);
23167 type = NULL_TREE;
23168 goto done;
23171 /* Figure out in what scope the declaration is being placed. */
23172 scope = current_scope ();
23173 /* If that scope does not contain the scope in which the
23174 class was originally declared, the program is invalid. */
23175 if (scope && !is_ancestor (scope, nested_name_specifier))
23177 if (at_namespace_scope_p ())
23178 error_at (type_start_token->location,
23179 "declaration of %qD in namespace %qD which does not "
23180 "enclose %qD",
23181 type, scope, nested_name_specifier);
23182 else
23183 error_at (type_start_token->location,
23184 "declaration of %qD in %qD which does not enclose %qD",
23185 type, scope, nested_name_specifier);
23186 type = NULL_TREE;
23187 goto done;
23189 /* [dcl.meaning]
23191 A declarator-id shall not be qualified except for the
23192 definition of a ... nested class outside of its class
23193 ... [or] the definition or explicit instantiation of a
23194 class member of a namespace outside of its namespace. */
23195 if (scope == nested_name_specifier)
23197 permerror (nested_name_specifier_token_start->location,
23198 "extra qualification not allowed");
23199 nested_name_specifier = NULL_TREE;
23200 num_templates = 0;
23203 /* An explicit-specialization must be preceded by "template <>". If
23204 it is not, try to recover gracefully. */
23205 if (at_namespace_scope_p ()
23206 && parser->num_template_parameter_lists == 0
23207 && !processing_template_parmlist
23208 && template_id_p)
23210 /* Build a location of this form:
23211 struct typename <ARGS>
23212 ^~~~~~~~~~~~~~~~~~~~~~
23213 with caret==start at the start token, and
23214 finishing at the end of the type. */
23215 location_t reported_loc
23216 = make_location (class_head_start_location,
23217 class_head_start_location,
23218 get_finish (type_start_token->location));
23219 rich_location richloc (line_table, reported_loc);
23220 richloc.add_fixit_insert_before (class_head_start_location,
23221 "template <> ");
23222 error_at (&richloc,
23223 "an explicit specialization must be preceded by"
23224 " %<template <>%>");
23225 invalid_explicit_specialization_p = true;
23226 /* Take the same action that would have been taken by
23227 cp_parser_explicit_specialization. */
23228 ++parser->num_template_parameter_lists;
23229 begin_specialization ();
23231 /* There must be no "return" statements between this point and the
23232 end of this function; set "type "to the correct return value and
23233 use "goto done;" to return. */
23234 /* Make sure that the right number of template parameters were
23235 present. */
23236 if (!cp_parser_check_template_parameters (parser, num_templates,
23237 template_id_p,
23238 type_start_token->location,
23239 /*declarator=*/NULL))
23241 /* If something went wrong, there is no point in even trying to
23242 process the class-definition. */
23243 type = NULL_TREE;
23244 goto done;
23247 /* Look up the type. */
23248 if (template_id_p)
23250 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23251 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23252 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23254 error_at (type_start_token->location,
23255 "function template %qD redeclared as a class template", id);
23256 type = error_mark_node;
23258 else
23260 type = TREE_TYPE (id);
23261 type = maybe_process_partial_specialization (type);
23263 /* Check the scope while we still know whether or not we had a
23264 nested-name-specifier. */
23265 if (type != error_mark_node)
23266 check_unqualified_spec_or_inst (type, type_start_token->location);
23268 if (nested_name_specifier)
23269 pushed_scope = push_scope (nested_name_specifier);
23271 else if (nested_name_specifier)
23273 tree class_type;
23275 /* Given:
23277 template <typename T> struct S { struct T };
23278 template <typename T> struct S<T>::T { };
23280 we will get a TYPENAME_TYPE when processing the definition of
23281 `S::T'. We need to resolve it to the actual type before we
23282 try to define it. */
23283 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23285 class_type = resolve_typename_type (TREE_TYPE (type),
23286 /*only_current_p=*/false);
23287 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23288 type = TYPE_NAME (class_type);
23289 else
23291 cp_parser_error (parser, "could not resolve typename type");
23292 type = error_mark_node;
23296 if (maybe_process_partial_specialization (TREE_TYPE (type))
23297 == error_mark_node)
23299 type = NULL_TREE;
23300 goto done;
23303 class_type = current_class_type;
23304 /* Enter the scope indicated by the nested-name-specifier. */
23305 pushed_scope = push_scope (nested_name_specifier);
23306 /* Get the canonical version of this type. */
23307 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23308 /* Call push_template_decl if it seems like we should be defining a
23309 template either from the template headers or the type we're
23310 defining, so that we diagnose both extra and missing headers. */
23311 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23312 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23313 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23315 type = push_template_decl (type);
23316 if (type == error_mark_node)
23318 type = NULL_TREE;
23319 goto done;
23323 type = TREE_TYPE (type);
23324 *nested_name_specifier_p = true;
23326 else /* The name is not a nested name. */
23328 /* If the class was unnamed, create a dummy name. */
23329 if (!id)
23330 id = make_anon_name ();
23331 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23332 ? ts_within_enclosing_non_class
23333 : ts_current);
23334 type = xref_tag (class_key, id, tag_scope,
23335 parser->num_template_parameter_lists);
23338 /* Indicate whether this class was declared as a `class' or as a
23339 `struct'. */
23340 if (TREE_CODE (type) == RECORD_TYPE)
23341 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23342 cp_parser_check_class_key (class_key, type);
23344 /* If this type was already complete, and we see another definition,
23345 that's an error. */
23346 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23348 error_at (type_start_token->location, "redefinition of %q#T",
23349 type);
23350 inform (location_of (type), "previous definition of %q#T",
23351 type);
23352 type = NULL_TREE;
23353 goto done;
23355 else if (type == error_mark_node)
23356 type = NULL_TREE;
23358 if (type)
23360 /* Apply attributes now, before any use of the class as a template
23361 argument in its base list. */
23362 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23363 fixup_attribute_variants (type);
23366 /* We will have entered the scope containing the class; the names of
23367 base classes should be looked up in that context. For example:
23369 struct A { struct B {}; struct C; };
23370 struct A::C : B {};
23372 is valid. */
23374 /* Get the list of base-classes, if there is one. */
23375 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23377 /* PR59482: enter the class scope so that base-specifiers are looked
23378 up correctly. */
23379 if (type)
23380 pushclass (type);
23381 bases = cp_parser_base_clause (parser);
23382 /* PR59482: get out of the previously pushed class scope so that the
23383 subsequent pops pop the right thing. */
23384 if (type)
23385 popclass ();
23387 else
23388 bases = NULL_TREE;
23390 /* If we're really defining a class, process the base classes.
23391 If they're invalid, fail. */
23392 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23393 xref_basetypes (type, bases);
23395 done:
23396 /* Leave the scope given by the nested-name-specifier. We will
23397 enter the class scope itself while processing the members. */
23398 if (pushed_scope)
23399 pop_scope (pushed_scope);
23401 if (invalid_explicit_specialization_p)
23403 end_specialization ();
23404 --parser->num_template_parameter_lists;
23407 if (type)
23408 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23409 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23410 CLASSTYPE_FINAL (type) = 1;
23411 out:
23412 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23413 return type;
23416 /* Parse a class-key.
23418 class-key:
23419 class
23420 struct
23421 union
23423 Returns the kind of class-key specified, or none_type to indicate
23424 error. */
23426 static enum tag_types
23427 cp_parser_class_key (cp_parser* parser)
23429 cp_token *token;
23430 enum tag_types tag_type;
23432 /* Look for the class-key. */
23433 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23434 if (!token)
23435 return none_type;
23437 /* Check to see if the TOKEN is a class-key. */
23438 tag_type = cp_parser_token_is_class_key (token);
23439 if (!tag_type)
23440 cp_parser_error (parser, "expected class-key");
23441 return tag_type;
23444 /* Parse a type-parameter-key.
23446 type-parameter-key:
23447 class
23448 typename
23451 static void
23452 cp_parser_type_parameter_key (cp_parser* parser)
23454 /* Look for the type-parameter-key. */
23455 enum tag_types tag_type = none_type;
23456 cp_token *token = cp_lexer_peek_token (parser->lexer);
23457 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23459 cp_lexer_consume_token (parser->lexer);
23460 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23461 /* typename is not allowed in a template template parameter
23462 by the standard until C++17. */
23463 pedwarn (token->location, OPT_Wpedantic,
23464 "ISO C++ forbids typename key in template template parameter;"
23465 " use -std=c++17 or -std=gnu++17");
23467 else
23468 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23470 return;
23473 /* Parse an (optional) member-specification.
23475 member-specification:
23476 member-declaration member-specification [opt]
23477 access-specifier : member-specification [opt] */
23479 static void
23480 cp_parser_member_specification_opt (cp_parser* parser)
23482 while (true)
23484 cp_token *token;
23485 enum rid keyword;
23487 /* Peek at the next token. */
23488 token = cp_lexer_peek_token (parser->lexer);
23489 /* If it's a `}', or EOF then we've seen all the members. */
23490 if (token->type == CPP_CLOSE_BRACE
23491 || token->type == CPP_EOF
23492 || token->type == CPP_PRAGMA_EOL)
23493 break;
23495 /* See if this token is a keyword. */
23496 keyword = token->keyword;
23497 switch (keyword)
23499 case RID_PUBLIC:
23500 case RID_PROTECTED:
23501 case RID_PRIVATE:
23502 /* Consume the access-specifier. */
23503 cp_lexer_consume_token (parser->lexer);
23504 /* Remember which access-specifier is active. */
23505 current_access_specifier = token->u.value;
23506 /* Look for the `:'. */
23507 cp_parser_require (parser, CPP_COLON, RT_COLON);
23508 break;
23510 default:
23511 /* Accept #pragmas at class scope. */
23512 if (token->type == CPP_PRAGMA)
23514 cp_parser_pragma (parser, pragma_member, NULL);
23515 break;
23518 /* Otherwise, the next construction must be a
23519 member-declaration. */
23520 cp_parser_member_declaration (parser);
23525 /* Parse a member-declaration.
23527 member-declaration:
23528 decl-specifier-seq [opt] member-declarator-list [opt] ;
23529 function-definition ; [opt]
23530 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23531 using-declaration
23532 template-declaration
23533 alias-declaration
23535 member-declarator-list:
23536 member-declarator
23537 member-declarator-list , member-declarator
23539 member-declarator:
23540 declarator pure-specifier [opt]
23541 declarator constant-initializer [opt]
23542 identifier [opt] : constant-expression
23544 GNU Extensions:
23546 member-declaration:
23547 __extension__ member-declaration
23549 member-declarator:
23550 declarator attributes [opt] pure-specifier [opt]
23551 declarator attributes [opt] constant-initializer [opt]
23552 identifier [opt] attributes [opt] : constant-expression
23554 C++0x Extensions:
23556 member-declaration:
23557 static_assert-declaration */
23559 static void
23560 cp_parser_member_declaration (cp_parser* parser)
23562 cp_decl_specifier_seq decl_specifiers;
23563 tree prefix_attributes;
23564 tree decl;
23565 int declares_class_or_enum;
23566 bool friend_p;
23567 cp_token *token = NULL;
23568 cp_token *decl_spec_token_start = NULL;
23569 cp_token *initializer_token_start = NULL;
23570 int saved_pedantic;
23571 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23573 /* Check for the `__extension__' keyword. */
23574 if (cp_parser_extension_opt (parser, &saved_pedantic))
23576 /* Recurse. */
23577 cp_parser_member_declaration (parser);
23578 /* Restore the old value of the PEDANTIC flag. */
23579 pedantic = saved_pedantic;
23581 return;
23584 /* Check for a template-declaration. */
23585 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23587 /* An explicit specialization here is an error condition, and we
23588 expect the specialization handler to detect and report this. */
23589 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23590 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23591 cp_parser_explicit_specialization (parser);
23592 else
23593 cp_parser_template_declaration (parser, /*member_p=*/true);
23595 return;
23597 /* Check for a template introduction. */
23598 else if (cp_parser_template_declaration_after_export (parser, true))
23599 return;
23601 /* Check for a using-declaration. */
23602 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23604 if (cxx_dialect < cxx11)
23606 /* Parse the using-declaration. */
23607 cp_parser_using_declaration (parser,
23608 /*access_declaration_p=*/false);
23609 return;
23611 else
23613 tree decl;
23614 bool alias_decl_expected;
23615 cp_parser_parse_tentatively (parser);
23616 decl = cp_parser_alias_declaration (parser);
23617 /* Note that if we actually see the '=' token after the
23618 identifier, cp_parser_alias_declaration commits the
23619 tentative parse. In that case, we really expect an
23620 alias-declaration. Otherwise, we expect a using
23621 declaration. */
23622 alias_decl_expected =
23623 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23624 cp_parser_parse_definitely (parser);
23626 if (alias_decl_expected)
23627 finish_member_declaration (decl);
23628 else
23629 cp_parser_using_declaration (parser,
23630 /*access_declaration_p=*/false);
23631 return;
23635 /* Check for @defs. */
23636 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23638 tree ivar, member;
23639 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23640 ivar = ivar_chains;
23641 while (ivar)
23643 member = ivar;
23644 ivar = TREE_CHAIN (member);
23645 TREE_CHAIN (member) = NULL_TREE;
23646 finish_member_declaration (member);
23648 return;
23651 /* If the next token is `static_assert' we have a static assertion. */
23652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23654 cp_parser_static_assert (parser, /*member_p=*/true);
23655 return;
23658 parser->colon_corrects_to_scope_p = false;
23660 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23661 goto out;
23663 /* Parse the decl-specifier-seq. */
23664 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23665 cp_parser_decl_specifier_seq (parser,
23666 CP_PARSER_FLAGS_OPTIONAL,
23667 &decl_specifiers,
23668 &declares_class_or_enum);
23669 /* Check for an invalid type-name. */
23670 if (!decl_specifiers.any_type_specifiers_p
23671 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23672 goto out;
23673 /* If there is no declarator, then the decl-specifier-seq should
23674 specify a type. */
23675 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23677 /* If there was no decl-specifier-seq, and the next token is a
23678 `;', then we have something like:
23680 struct S { ; };
23682 [class.mem]
23684 Each member-declaration shall declare at least one member
23685 name of the class. */
23686 if (!decl_specifiers.any_specifiers_p)
23688 cp_token *token = cp_lexer_peek_token (parser->lexer);
23689 if (!in_system_header_at (token->location))
23691 gcc_rich_location richloc (token->location);
23692 richloc.add_fixit_remove ();
23693 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23696 else
23698 tree type;
23700 /* See if this declaration is a friend. */
23701 friend_p = cp_parser_friend_p (&decl_specifiers);
23702 /* If there were decl-specifiers, check to see if there was
23703 a class-declaration. */
23704 type = check_tag_decl (&decl_specifiers,
23705 /*explicit_type_instantiation_p=*/false);
23706 /* Nested classes have already been added to the class, but
23707 a `friend' needs to be explicitly registered. */
23708 if (friend_p)
23710 /* If the `friend' keyword was present, the friend must
23711 be introduced with a class-key. */
23712 if (!declares_class_or_enum && cxx_dialect < cxx11)
23713 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23714 "in C++03 a class-key must be used "
23715 "when declaring a friend");
23716 /* In this case:
23718 template <typename T> struct A {
23719 friend struct A<T>::B;
23722 A<T>::B will be represented by a TYPENAME_TYPE, and
23723 therefore not recognized by check_tag_decl. */
23724 if (!type)
23726 type = decl_specifiers.type;
23727 if (type && TREE_CODE (type) == TYPE_DECL)
23728 type = TREE_TYPE (type);
23730 if (!type || !TYPE_P (type))
23731 error_at (decl_spec_token_start->location,
23732 "friend declaration does not name a class or "
23733 "function");
23734 else
23735 make_friend_class (current_class_type, type,
23736 /*complain=*/true);
23738 /* If there is no TYPE, an error message will already have
23739 been issued. */
23740 else if (!type || type == error_mark_node)
23742 /* An anonymous aggregate has to be handled specially; such
23743 a declaration really declares a data member (with a
23744 particular type), as opposed to a nested class. */
23745 else if (ANON_AGGR_TYPE_P (type))
23747 /* C++11 9.5/6. */
23748 if (decl_specifiers.storage_class != sc_none)
23749 error_at (decl_spec_token_start->location,
23750 "a storage class on an anonymous aggregate "
23751 "in class scope is not allowed");
23753 /* Remove constructors and such from TYPE, now that we
23754 know it is an anonymous aggregate. */
23755 fixup_anonymous_aggr (type);
23756 /* And make the corresponding data member. */
23757 decl = build_decl (decl_spec_token_start->location,
23758 FIELD_DECL, NULL_TREE, type);
23759 /* Add it to the class. */
23760 finish_member_declaration (decl);
23762 else
23763 cp_parser_check_access_in_redeclaration
23764 (TYPE_NAME (type),
23765 decl_spec_token_start->location);
23768 else
23770 bool assume_semicolon = false;
23772 /* Clear attributes from the decl_specifiers but keep them
23773 around as prefix attributes that apply them to the entity
23774 being declared. */
23775 prefix_attributes = decl_specifiers.attributes;
23776 decl_specifiers.attributes = NULL_TREE;
23778 /* See if these declarations will be friends. */
23779 friend_p = cp_parser_friend_p (&decl_specifiers);
23781 /* Keep going until we hit the `;' at the end of the
23782 declaration. */
23783 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23785 tree attributes = NULL_TREE;
23786 tree first_attribute;
23787 tree initializer;
23788 bool named_bitfld = false;
23790 /* Peek at the next token. */
23791 token = cp_lexer_peek_token (parser->lexer);
23793 /* The following code wants to know early if it is a bit-field
23794 or some other declaration. Attributes can appear before
23795 the `:' token. Skip over them without consuming any tokens
23796 to peek if they are followed by `:'. */
23797 if (cp_next_tokens_can_be_attribute_p (parser)
23798 || (token->type == CPP_NAME
23799 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23800 && (named_bitfld = true)))
23802 size_t n
23803 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23804 token = cp_lexer_peek_nth_token (parser->lexer, n);
23807 /* Check for a bitfield declaration. */
23808 if (token->type == CPP_COLON
23809 || (token->type == CPP_NAME
23810 && token == cp_lexer_peek_token (parser->lexer)
23811 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23812 && (named_bitfld = true)))
23814 tree identifier;
23815 tree width;
23816 tree late_attributes = NULL_TREE;
23818 if (named_bitfld)
23819 identifier = cp_parser_identifier (parser);
23820 else
23821 identifier = NULL_TREE;
23823 /* Look for attributes that apply to the bitfield. */
23824 attributes = cp_parser_attributes_opt (parser);
23826 /* Consume the `:' token. */
23827 cp_lexer_consume_token (parser->lexer);
23829 /* Get the width of the bitfield. */
23830 width = cp_parser_constant_expression (parser, false, NULL,
23831 cxx_dialect >= cxx11);
23833 /* In C++2A and as extension for C++11 and above we allow
23834 default member initializers for bit-fields. */
23835 initializer = NULL_TREE;
23836 if (cxx_dialect >= cxx11
23837 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23838 || cp_lexer_next_token_is (parser->lexer,
23839 CPP_OPEN_BRACE)))
23841 location_t loc
23842 = cp_lexer_peek_token (parser->lexer)->location;
23843 if (cxx_dialect < cxx2a
23844 && !in_system_header_at (loc)
23845 && identifier != NULL_TREE)
23846 pedwarn (loc, 0,
23847 "default member initializers for bit-fields "
23848 "only available with -std=c++2a or "
23849 "-std=gnu++2a");
23851 initializer = cp_parser_save_nsdmi (parser);
23852 if (identifier == NULL_TREE)
23854 error_at (loc, "default member initializer for "
23855 "unnamed bit-field");
23856 initializer = NULL_TREE;
23859 else
23861 /* Look for attributes that apply to the bitfield after
23862 the `:' token and width. This is where GCC used to
23863 parse attributes in the past, pedwarn if there is
23864 a std attribute. */
23865 if (cp_next_tokens_can_be_std_attribute_p (parser))
23866 pedwarn (input_location, OPT_Wpedantic,
23867 "ISO C++ allows bit-field attributes only "
23868 "before the %<:%> token");
23870 late_attributes = cp_parser_attributes_opt (parser);
23873 attributes = attr_chainon (attributes, late_attributes);
23875 /* Remember which attributes are prefix attributes and
23876 which are not. */
23877 first_attribute = attributes;
23878 /* Combine the attributes. */
23879 attributes = attr_chainon (prefix_attributes, attributes);
23881 /* Create the bitfield declaration. */
23882 decl = grokbitfield (identifier
23883 ? make_id_declarator (NULL_TREE,
23884 identifier,
23885 sfk_none)
23886 : NULL,
23887 &decl_specifiers,
23888 width, initializer,
23889 attributes);
23891 else
23893 cp_declarator *declarator;
23894 tree asm_specification;
23895 int ctor_dtor_or_conv_p;
23897 /* Parse the declarator. */
23898 declarator
23899 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23900 &ctor_dtor_or_conv_p,
23901 /*parenthesized_p=*/NULL,
23902 /*member_p=*/true,
23903 friend_p);
23905 /* If something went wrong parsing the declarator, make sure
23906 that we at least consume some tokens. */
23907 if (declarator == cp_error_declarator)
23909 /* Skip to the end of the statement. */
23910 cp_parser_skip_to_end_of_statement (parser);
23911 /* If the next token is not a semicolon, that is
23912 probably because we just skipped over the body of
23913 a function. So, we consume a semicolon if
23914 present, but do not issue an error message if it
23915 is not present. */
23916 if (cp_lexer_next_token_is (parser->lexer,
23917 CPP_SEMICOLON))
23918 cp_lexer_consume_token (parser->lexer);
23919 goto out;
23922 if (declares_class_or_enum & 2)
23923 cp_parser_check_for_definition_in_return_type
23924 (declarator, decl_specifiers.type,
23925 decl_specifiers.locations[ds_type_spec]);
23927 /* Look for an asm-specification. */
23928 asm_specification = cp_parser_asm_specification_opt (parser);
23929 /* Look for attributes that apply to the declaration. */
23930 attributes = cp_parser_attributes_opt (parser);
23931 /* Remember which attributes are prefix attributes and
23932 which are not. */
23933 first_attribute = attributes;
23934 /* Combine the attributes. */
23935 attributes = attr_chainon (prefix_attributes, attributes);
23937 /* If it's an `=', then we have a constant-initializer or a
23938 pure-specifier. It is not correct to parse the
23939 initializer before registering the member declaration
23940 since the member declaration should be in scope while
23941 its initializer is processed. However, the rest of the
23942 front end does not yet provide an interface that allows
23943 us to handle this correctly. */
23944 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23946 /* In [class.mem]:
23948 A pure-specifier shall be used only in the declaration of
23949 a virtual function.
23951 A member-declarator can contain a constant-initializer
23952 only if it declares a static member of integral or
23953 enumeration type.
23955 Therefore, if the DECLARATOR is for a function, we look
23956 for a pure-specifier; otherwise, we look for a
23957 constant-initializer. When we call `grokfield', it will
23958 perform more stringent semantics checks. */
23959 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23960 if (function_declarator_p (declarator)
23961 || (decl_specifiers.type
23962 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23963 && declarator->kind == cdk_id
23964 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23965 == FUNCTION_TYPE)))
23966 initializer = cp_parser_pure_specifier (parser);
23967 else if (decl_specifiers.storage_class != sc_static)
23968 initializer = cp_parser_save_nsdmi (parser);
23969 else if (cxx_dialect >= cxx11)
23971 bool nonconst;
23972 /* Don't require a constant rvalue in C++11, since we
23973 might want a reference constant. We'll enforce
23974 constancy later. */
23975 cp_lexer_consume_token (parser->lexer);
23976 /* Parse the initializer. */
23977 initializer = cp_parser_initializer_clause (parser,
23978 &nonconst);
23980 else
23981 /* Parse the initializer. */
23982 initializer = cp_parser_constant_initializer (parser);
23984 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23985 && !function_declarator_p (declarator))
23987 bool x;
23988 if (decl_specifiers.storage_class != sc_static)
23989 initializer = cp_parser_save_nsdmi (parser);
23990 else
23991 initializer = cp_parser_initializer (parser, &x, &x);
23993 /* Otherwise, there is no initializer. */
23994 else
23995 initializer = NULL_TREE;
23997 /* See if we are probably looking at a function
23998 definition. We are certainly not looking at a
23999 member-declarator. Calling `grokfield' has
24000 side-effects, so we must not do it unless we are sure
24001 that we are looking at a member-declarator. */
24002 if (cp_parser_token_starts_function_definition_p
24003 (cp_lexer_peek_token (parser->lexer)))
24005 /* The grammar does not allow a pure-specifier to be
24006 used when a member function is defined. (It is
24007 possible that this fact is an oversight in the
24008 standard, since a pure function may be defined
24009 outside of the class-specifier. */
24010 if (initializer && initializer_token_start)
24011 error_at (initializer_token_start->location,
24012 "pure-specifier on function-definition");
24013 decl = cp_parser_save_member_function_body (parser,
24014 &decl_specifiers,
24015 declarator,
24016 attributes);
24017 if (parser->fully_implicit_function_template_p)
24018 decl = finish_fully_implicit_template (parser, decl);
24019 /* If the member was not a friend, declare it here. */
24020 if (!friend_p)
24021 finish_member_declaration (decl);
24022 /* Peek at the next token. */
24023 token = cp_lexer_peek_token (parser->lexer);
24024 /* If the next token is a semicolon, consume it. */
24025 if (token->type == CPP_SEMICOLON)
24027 location_t semicolon_loc
24028 = cp_lexer_consume_token (parser->lexer)->location;
24029 gcc_rich_location richloc (semicolon_loc);
24030 richloc.add_fixit_remove ();
24031 warning_at (&richloc, OPT_Wextra_semi,
24032 "extra %<;%> after in-class "
24033 "function definition");
24035 goto out;
24037 else
24038 if (declarator->kind == cdk_function)
24039 declarator->id_loc = token->location;
24040 /* Create the declaration. */
24041 decl = grokfield (declarator, &decl_specifiers,
24042 initializer, /*init_const_expr_p=*/true,
24043 asm_specification, attributes);
24044 if (parser->fully_implicit_function_template_p)
24046 if (friend_p)
24047 finish_fully_implicit_template (parser, 0);
24048 else
24049 decl = finish_fully_implicit_template (parser, decl);
24053 cp_finalize_omp_declare_simd (parser, decl);
24054 cp_finalize_oacc_routine (parser, decl, false);
24056 /* Reset PREFIX_ATTRIBUTES. */
24057 if (attributes != error_mark_node)
24059 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24060 attributes = TREE_CHAIN (attributes);
24061 if (attributes)
24062 TREE_CHAIN (attributes) = NULL_TREE;
24065 /* If there is any qualification still in effect, clear it
24066 now; we will be starting fresh with the next declarator. */
24067 parser->scope = NULL_TREE;
24068 parser->qualifying_scope = NULL_TREE;
24069 parser->object_scope = NULL_TREE;
24070 /* If it's a `,', then there are more declarators. */
24071 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24073 cp_lexer_consume_token (parser->lexer);
24074 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24076 cp_token *token = cp_lexer_previous_token (parser->lexer);
24077 gcc_rich_location richloc (token->location);
24078 richloc.add_fixit_remove ();
24079 error_at (&richloc, "stray %<,%> at end of "
24080 "member declaration");
24083 /* If the next token isn't a `;', then we have a parse error. */
24084 else if (cp_lexer_next_token_is_not (parser->lexer,
24085 CPP_SEMICOLON))
24087 /* The next token might be a ways away from where the
24088 actual semicolon is missing. Find the previous token
24089 and use that for our error position. */
24090 cp_token *token = cp_lexer_previous_token (parser->lexer);
24091 gcc_rich_location richloc (token->location);
24092 richloc.add_fixit_insert_after (";");
24093 error_at (&richloc, "expected %<;%> at end of "
24094 "member declaration");
24096 /* Assume that the user meant to provide a semicolon. If
24097 we were to cp_parser_skip_to_end_of_statement, we might
24098 skip to a semicolon inside a member function definition
24099 and issue nonsensical error messages. */
24100 assume_semicolon = true;
24103 if (decl)
24105 /* Add DECL to the list of members. */
24106 if (!friend_p
24107 /* Explicitly include, eg, NSDMIs, for better error
24108 recovery (c++/58650). */
24109 || !DECL_DECLARES_FUNCTION_P (decl))
24110 finish_member_declaration (decl);
24112 if (TREE_CODE (decl) == FUNCTION_DECL)
24113 cp_parser_save_default_args (parser, decl);
24114 else if (TREE_CODE (decl) == FIELD_DECL
24115 && DECL_INITIAL (decl))
24116 /* Add DECL to the queue of NSDMI to be parsed later. */
24117 vec_safe_push (unparsed_nsdmis, decl);
24120 if (assume_semicolon)
24121 goto out;
24125 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24126 out:
24127 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24130 /* Parse a pure-specifier.
24132 pure-specifier:
24135 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24136 Otherwise, ERROR_MARK_NODE is returned. */
24138 static tree
24139 cp_parser_pure_specifier (cp_parser* parser)
24141 cp_token *token;
24143 /* Look for the `=' token. */
24144 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24145 return error_mark_node;
24146 /* Look for the `0' token. */
24147 token = cp_lexer_peek_token (parser->lexer);
24149 if (token->type == CPP_EOF
24150 || token->type == CPP_PRAGMA_EOL)
24151 return error_mark_node;
24153 cp_lexer_consume_token (parser->lexer);
24155 /* Accept = default or = delete in c++0x mode. */
24156 if (token->keyword == RID_DEFAULT
24157 || token->keyword == RID_DELETE)
24159 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24160 return token->u.value;
24163 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24164 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24166 cp_parser_error (parser,
24167 "invalid pure specifier (only %<= 0%> is allowed)");
24168 cp_parser_skip_to_end_of_statement (parser);
24169 return error_mark_node;
24171 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24173 error_at (token->location, "templates may not be %<virtual%>");
24174 return error_mark_node;
24177 return integer_zero_node;
24180 /* Parse a constant-initializer.
24182 constant-initializer:
24183 = constant-expression
24185 Returns a representation of the constant-expression. */
24187 static tree
24188 cp_parser_constant_initializer (cp_parser* parser)
24190 /* Look for the `=' token. */
24191 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24192 return error_mark_node;
24194 /* It is invalid to write:
24196 struct S { static const int i = { 7 }; };
24199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24201 cp_parser_error (parser,
24202 "a brace-enclosed initializer is not allowed here");
24203 /* Consume the opening brace. */
24204 matching_braces braces;
24205 braces.consume_open (parser);
24206 /* Skip the initializer. */
24207 cp_parser_skip_to_closing_brace (parser);
24208 /* Look for the trailing `}'. */
24209 braces.require_close (parser);
24211 return error_mark_node;
24214 return cp_parser_constant_expression (parser);
24217 /* Derived classes [gram.class.derived] */
24219 /* Parse a base-clause.
24221 base-clause:
24222 : base-specifier-list
24224 base-specifier-list:
24225 base-specifier ... [opt]
24226 base-specifier-list , base-specifier ... [opt]
24228 Returns a TREE_LIST representing the base-classes, in the order in
24229 which they were declared. The representation of each node is as
24230 described by cp_parser_base_specifier.
24232 In the case that no bases are specified, this function will return
24233 NULL_TREE, not ERROR_MARK_NODE. */
24235 static tree
24236 cp_parser_base_clause (cp_parser* parser)
24238 tree bases = NULL_TREE;
24240 /* Look for the `:' that begins the list. */
24241 cp_parser_require (parser, CPP_COLON, RT_COLON);
24243 /* Scan the base-specifier-list. */
24244 while (true)
24246 cp_token *token;
24247 tree base;
24248 bool pack_expansion_p = false;
24250 /* Look for the base-specifier. */
24251 base = cp_parser_base_specifier (parser);
24252 /* Look for the (optional) ellipsis. */
24253 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24255 /* Consume the `...'. */
24256 cp_lexer_consume_token (parser->lexer);
24258 pack_expansion_p = true;
24261 /* Add BASE to the front of the list. */
24262 if (base && base != error_mark_node)
24264 if (pack_expansion_p)
24265 /* Make this a pack expansion type. */
24266 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24268 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24270 TREE_CHAIN (base) = bases;
24271 bases = base;
24274 /* Peek at the next token. */
24275 token = cp_lexer_peek_token (parser->lexer);
24276 /* If it's not a comma, then the list is complete. */
24277 if (token->type != CPP_COMMA)
24278 break;
24279 /* Consume the `,'. */
24280 cp_lexer_consume_token (parser->lexer);
24283 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24284 base class had a qualified name. However, the next name that
24285 appears is certainly not qualified. */
24286 parser->scope = NULL_TREE;
24287 parser->qualifying_scope = NULL_TREE;
24288 parser->object_scope = NULL_TREE;
24290 return nreverse (bases);
24293 /* Parse a base-specifier.
24295 base-specifier:
24296 :: [opt] nested-name-specifier [opt] class-name
24297 virtual access-specifier [opt] :: [opt] nested-name-specifier
24298 [opt] class-name
24299 access-specifier virtual [opt] :: [opt] nested-name-specifier
24300 [opt] class-name
24302 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24303 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24304 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24305 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24307 static tree
24308 cp_parser_base_specifier (cp_parser* parser)
24310 cp_token *token;
24311 bool done = false;
24312 bool virtual_p = false;
24313 bool duplicate_virtual_error_issued_p = false;
24314 bool duplicate_access_error_issued_p = false;
24315 bool class_scope_p, template_p;
24316 tree access = access_default_node;
24317 tree type;
24319 /* Process the optional `virtual' and `access-specifier'. */
24320 while (!done)
24322 /* Peek at the next token. */
24323 token = cp_lexer_peek_token (parser->lexer);
24324 /* Process `virtual'. */
24325 switch (token->keyword)
24327 case RID_VIRTUAL:
24328 /* If `virtual' appears more than once, issue an error. */
24329 if (virtual_p && !duplicate_virtual_error_issued_p)
24331 cp_parser_error (parser,
24332 "%<virtual%> specified more than once in base-specifier");
24333 duplicate_virtual_error_issued_p = true;
24336 virtual_p = true;
24338 /* Consume the `virtual' token. */
24339 cp_lexer_consume_token (parser->lexer);
24341 break;
24343 case RID_PUBLIC:
24344 case RID_PROTECTED:
24345 case RID_PRIVATE:
24346 /* If more than one access specifier appears, issue an
24347 error. */
24348 if (access != access_default_node
24349 && !duplicate_access_error_issued_p)
24351 cp_parser_error (parser,
24352 "more than one access specifier in base-specifier");
24353 duplicate_access_error_issued_p = true;
24356 access = ridpointers[(int) token->keyword];
24358 /* Consume the access-specifier. */
24359 cp_lexer_consume_token (parser->lexer);
24361 break;
24363 default:
24364 done = true;
24365 break;
24368 /* It is not uncommon to see programs mechanically, erroneously, use
24369 the 'typename' keyword to denote (dependent) qualified types
24370 as base classes. */
24371 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24373 token = cp_lexer_peek_token (parser->lexer);
24374 if (!processing_template_decl)
24375 error_at (token->location,
24376 "keyword %<typename%> not allowed outside of templates");
24377 else
24378 error_at (token->location,
24379 "keyword %<typename%> not allowed in this context "
24380 "(the base class is implicitly a type)");
24381 cp_lexer_consume_token (parser->lexer);
24384 /* Look for the optional `::' operator. */
24385 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24386 /* Look for the nested-name-specifier. The simplest way to
24387 implement:
24389 [temp.res]
24391 The keyword `typename' is not permitted in a base-specifier or
24392 mem-initializer; in these contexts a qualified name that
24393 depends on a template-parameter is implicitly assumed to be a
24394 type name.
24396 is to pretend that we have seen the `typename' keyword at this
24397 point. */
24398 cp_parser_nested_name_specifier_opt (parser,
24399 /*typename_keyword_p=*/true,
24400 /*check_dependency_p=*/true,
24401 /*type_p=*/true,
24402 /*is_declaration=*/true);
24403 /* If the base class is given by a qualified name, assume that names
24404 we see are type names or templates, as appropriate. */
24405 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24406 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24408 if (!parser->scope
24409 && cp_lexer_next_token_is_decltype (parser->lexer))
24410 /* DR 950 allows decltype as a base-specifier. */
24411 type = cp_parser_decltype (parser);
24412 else
24414 /* Otherwise, look for the class-name. */
24415 type = cp_parser_class_name (parser,
24416 class_scope_p,
24417 template_p,
24418 typename_type,
24419 /*check_dependency_p=*/true,
24420 /*class_head_p=*/false,
24421 /*is_declaration=*/true);
24422 type = TREE_TYPE (type);
24425 if (type == error_mark_node)
24426 return error_mark_node;
24428 return finish_base_specifier (type, access, virtual_p);
24431 /* Exception handling [gram.exception] */
24433 /* Parse an (optional) noexcept-specification.
24435 noexcept-specification:
24436 noexcept ( constant-expression ) [opt]
24438 If no noexcept-specification is present, returns NULL_TREE.
24439 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24440 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24441 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24442 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24443 in which case a boolean condition is returned instead. */
24445 static tree
24446 cp_parser_noexcept_specification_opt (cp_parser* parser,
24447 bool require_constexpr,
24448 bool* consumed_expr,
24449 bool return_cond)
24451 cp_token *token;
24452 const char *saved_message;
24454 /* Peek at the next token. */
24455 token = cp_lexer_peek_token (parser->lexer);
24457 /* Is it a noexcept-specification? */
24458 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24460 tree expr;
24461 cp_lexer_consume_token (parser->lexer);
24463 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24465 matching_parens parens;
24466 parens.consume_open (parser);
24468 if (require_constexpr)
24470 /* Types may not be defined in an exception-specification. */
24471 saved_message = parser->type_definition_forbidden_message;
24472 parser->type_definition_forbidden_message
24473 = G_("types may not be defined in an exception-specification");
24475 expr = cp_parser_constant_expression (parser);
24477 /* Restore the saved message. */
24478 parser->type_definition_forbidden_message = saved_message;
24480 else
24482 expr = cp_parser_expression (parser);
24483 *consumed_expr = true;
24486 parens.require_close (parser);
24488 else
24490 expr = boolean_true_node;
24491 if (!require_constexpr)
24492 *consumed_expr = false;
24495 /* We cannot build a noexcept-spec right away because this will check
24496 that expr is a constexpr. */
24497 if (!return_cond)
24498 return build_noexcept_spec (expr, tf_warning_or_error);
24499 else
24500 return expr;
24502 else
24503 return NULL_TREE;
24506 /* Parse an (optional) exception-specification.
24508 exception-specification:
24509 throw ( type-id-list [opt] )
24511 Returns a TREE_LIST representing the exception-specification. The
24512 TREE_VALUE of each node is a type. */
24514 static tree
24515 cp_parser_exception_specification_opt (cp_parser* parser)
24517 cp_token *token;
24518 tree type_id_list;
24519 const char *saved_message;
24521 /* Peek at the next token. */
24522 token = cp_lexer_peek_token (parser->lexer);
24524 /* Is it a noexcept-specification? */
24525 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24526 false);
24527 if (type_id_list != NULL_TREE)
24528 return type_id_list;
24530 /* If it's not `throw', then there's no exception-specification. */
24531 if (!cp_parser_is_keyword (token, RID_THROW))
24532 return NULL_TREE;
24534 location_t loc = token->location;
24536 /* Consume the `throw'. */
24537 cp_lexer_consume_token (parser->lexer);
24539 /* Look for the `('. */
24540 matching_parens parens;
24541 parens.require_open (parser);
24543 /* Peek at the next token. */
24544 token = cp_lexer_peek_token (parser->lexer);
24545 /* If it's not a `)', then there is a type-id-list. */
24546 if (token->type != CPP_CLOSE_PAREN)
24548 /* Types may not be defined in an exception-specification. */
24549 saved_message = parser->type_definition_forbidden_message;
24550 parser->type_definition_forbidden_message
24551 = G_("types may not be defined in an exception-specification");
24552 /* Parse the type-id-list. */
24553 type_id_list = cp_parser_type_id_list (parser);
24554 /* Restore the saved message. */
24555 parser->type_definition_forbidden_message = saved_message;
24557 if (cxx_dialect >= cxx17)
24559 error_at (loc, "ISO C++17 does not allow dynamic exception "
24560 "specifications");
24561 type_id_list = NULL_TREE;
24563 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24564 warning_at (loc, OPT_Wdeprecated,
24565 "dynamic exception specifications are deprecated in "
24566 "C++11");
24568 /* In C++17, throw() is equivalent to noexcept (true). throw()
24569 is deprecated in C++11 and above as well, but is still widely used,
24570 so don't warn about it yet. */
24571 else if (cxx_dialect >= cxx17)
24572 type_id_list = noexcept_true_spec;
24573 else
24574 type_id_list = empty_except_spec;
24576 /* Look for the `)'. */
24577 parens.require_close (parser);
24579 return type_id_list;
24582 /* Parse an (optional) type-id-list.
24584 type-id-list:
24585 type-id ... [opt]
24586 type-id-list , type-id ... [opt]
24588 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24589 in the order that the types were presented. */
24591 static tree
24592 cp_parser_type_id_list (cp_parser* parser)
24594 tree types = NULL_TREE;
24596 while (true)
24598 cp_token *token;
24599 tree type;
24601 token = cp_lexer_peek_token (parser->lexer);
24603 /* Get the next type-id. */
24604 type = cp_parser_type_id (parser);
24605 /* Check for invalid 'auto'. */
24606 if (flag_concepts && type_uses_auto (type))
24608 error_at (token->location,
24609 "invalid use of %<auto%> in exception-specification");
24610 type = error_mark_node;
24612 /* Parse the optional ellipsis. */
24613 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24615 /* Consume the `...'. */
24616 cp_lexer_consume_token (parser->lexer);
24618 /* Turn the type into a pack expansion expression. */
24619 type = make_pack_expansion (type);
24621 /* Add it to the list. */
24622 types = add_exception_specifier (types, type, /*complain=*/1);
24623 /* Peek at the next token. */
24624 token = cp_lexer_peek_token (parser->lexer);
24625 /* If it is not a `,', we are done. */
24626 if (token->type != CPP_COMMA)
24627 break;
24628 /* Consume the `,'. */
24629 cp_lexer_consume_token (parser->lexer);
24632 return nreverse (types);
24635 /* Parse a try-block.
24637 try-block:
24638 try compound-statement handler-seq */
24640 static tree
24641 cp_parser_try_block (cp_parser* parser)
24643 tree try_block;
24645 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24646 if (parser->in_function_body
24647 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24648 error ("%<try%> in %<constexpr%> function");
24650 try_block = begin_try_block ();
24651 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24652 finish_try_block (try_block);
24653 cp_parser_handler_seq (parser);
24654 finish_handler_sequence (try_block);
24656 return try_block;
24659 /* Parse a function-try-block.
24661 function-try-block:
24662 try ctor-initializer [opt] function-body handler-seq */
24664 static void
24665 cp_parser_function_try_block (cp_parser* parser)
24667 tree compound_stmt;
24668 tree try_block;
24670 /* Look for the `try' keyword. */
24671 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24672 return;
24673 /* Let the rest of the front end know where we are. */
24674 try_block = begin_function_try_block (&compound_stmt);
24675 /* Parse the function-body. */
24676 cp_parser_ctor_initializer_opt_and_function_body
24677 (parser, /*in_function_try_block=*/true);
24678 /* We're done with the `try' part. */
24679 finish_function_try_block (try_block);
24680 /* Parse the handlers. */
24681 cp_parser_handler_seq (parser);
24682 /* We're done with the handlers. */
24683 finish_function_handler_sequence (try_block, compound_stmt);
24686 /* Parse a handler-seq.
24688 handler-seq:
24689 handler handler-seq [opt] */
24691 static void
24692 cp_parser_handler_seq (cp_parser* parser)
24694 while (true)
24696 cp_token *token;
24698 /* Parse the handler. */
24699 cp_parser_handler (parser);
24700 /* Peek at the next token. */
24701 token = cp_lexer_peek_token (parser->lexer);
24702 /* If it's not `catch' then there are no more handlers. */
24703 if (!cp_parser_is_keyword (token, RID_CATCH))
24704 break;
24708 /* Parse a handler.
24710 handler:
24711 catch ( exception-declaration ) compound-statement */
24713 static void
24714 cp_parser_handler (cp_parser* parser)
24716 tree handler;
24717 tree declaration;
24719 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24720 handler = begin_handler ();
24721 matching_parens parens;
24722 parens.require_open (parser);
24723 declaration = cp_parser_exception_declaration (parser);
24724 finish_handler_parms (declaration, handler);
24725 parens.require_close (parser);
24726 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24727 finish_handler (handler);
24730 /* Parse an exception-declaration.
24732 exception-declaration:
24733 type-specifier-seq declarator
24734 type-specifier-seq abstract-declarator
24735 type-specifier-seq
24738 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24739 ellipsis variant is used. */
24741 static tree
24742 cp_parser_exception_declaration (cp_parser* parser)
24744 cp_decl_specifier_seq type_specifiers;
24745 cp_declarator *declarator;
24746 const char *saved_message;
24748 /* If it's an ellipsis, it's easy to handle. */
24749 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24751 /* Consume the `...' token. */
24752 cp_lexer_consume_token (parser->lexer);
24753 return NULL_TREE;
24756 /* Types may not be defined in exception-declarations. */
24757 saved_message = parser->type_definition_forbidden_message;
24758 parser->type_definition_forbidden_message
24759 = G_("types may not be defined in exception-declarations");
24761 /* Parse the type-specifier-seq. */
24762 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24763 /*is_trailing_return=*/false,
24764 &type_specifiers);
24765 /* If it's a `)', then there is no declarator. */
24766 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24767 declarator = NULL;
24768 else
24769 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24770 /*ctor_dtor_or_conv_p=*/NULL,
24771 /*parenthesized_p=*/NULL,
24772 /*member_p=*/false,
24773 /*friend_p=*/false);
24775 /* Restore the saved message. */
24776 parser->type_definition_forbidden_message = saved_message;
24778 if (!type_specifiers.any_specifiers_p)
24779 return error_mark_node;
24781 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24784 /* Parse a throw-expression.
24786 throw-expression:
24787 throw assignment-expression [opt]
24789 Returns a THROW_EXPR representing the throw-expression. */
24791 static tree
24792 cp_parser_throw_expression (cp_parser* parser)
24794 tree expression;
24795 cp_token* token;
24797 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24798 token = cp_lexer_peek_token (parser->lexer);
24799 /* Figure out whether or not there is an assignment-expression
24800 following the "throw" keyword. */
24801 if (token->type == CPP_COMMA
24802 || token->type == CPP_SEMICOLON
24803 || token->type == CPP_CLOSE_PAREN
24804 || token->type == CPP_CLOSE_SQUARE
24805 || token->type == CPP_CLOSE_BRACE
24806 || token->type == CPP_COLON)
24807 expression = NULL_TREE;
24808 else
24809 expression = cp_parser_assignment_expression (parser);
24811 return build_throw (expression);
24814 /* GNU Extensions */
24816 /* Parse an (optional) asm-specification.
24818 asm-specification:
24819 asm ( string-literal )
24821 If the asm-specification is present, returns a STRING_CST
24822 corresponding to the string-literal. Otherwise, returns
24823 NULL_TREE. */
24825 static tree
24826 cp_parser_asm_specification_opt (cp_parser* parser)
24828 cp_token *token;
24829 tree asm_specification;
24831 /* Peek at the next token. */
24832 token = cp_lexer_peek_token (parser->lexer);
24833 /* If the next token isn't the `asm' keyword, then there's no
24834 asm-specification. */
24835 if (!cp_parser_is_keyword (token, RID_ASM))
24836 return NULL_TREE;
24838 /* Consume the `asm' token. */
24839 cp_lexer_consume_token (parser->lexer);
24840 /* Look for the `('. */
24841 matching_parens parens;
24842 parens.require_open (parser);
24844 /* Look for the string-literal. */
24845 asm_specification = cp_parser_string_literal (parser, false, false);
24847 /* Look for the `)'. */
24848 parens.require_close (parser);
24850 return asm_specification;
24853 /* Parse an asm-operand-list.
24855 asm-operand-list:
24856 asm-operand
24857 asm-operand-list , asm-operand
24859 asm-operand:
24860 string-literal ( expression )
24861 [ string-literal ] string-literal ( expression )
24863 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24864 each node is the expression. The TREE_PURPOSE is itself a
24865 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24866 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24867 is a STRING_CST for the string literal before the parenthesis. Returns
24868 ERROR_MARK_NODE if any of the operands are invalid. */
24870 static tree
24871 cp_parser_asm_operand_list (cp_parser* parser)
24873 tree asm_operands = NULL_TREE;
24874 bool invalid_operands = false;
24876 while (true)
24878 tree string_literal;
24879 tree expression;
24880 tree name;
24882 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24884 /* Consume the `[' token. */
24885 cp_lexer_consume_token (parser->lexer);
24886 /* Read the operand name. */
24887 name = cp_parser_identifier (parser);
24888 if (name != error_mark_node)
24889 name = build_string (IDENTIFIER_LENGTH (name),
24890 IDENTIFIER_POINTER (name));
24891 /* Look for the closing `]'. */
24892 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24894 else
24895 name = NULL_TREE;
24896 /* Look for the string-literal. */
24897 string_literal = cp_parser_string_literal (parser, false, false);
24899 /* Look for the `('. */
24900 matching_parens parens;
24901 parens.require_open (parser);
24902 /* Parse the expression. */
24903 expression = cp_parser_expression (parser);
24904 /* Look for the `)'. */
24905 parens.require_close (parser);
24907 if (name == error_mark_node
24908 || string_literal == error_mark_node
24909 || expression == error_mark_node)
24910 invalid_operands = true;
24912 /* Add this operand to the list. */
24913 asm_operands = tree_cons (build_tree_list (name, string_literal),
24914 expression,
24915 asm_operands);
24916 /* If the next token is not a `,', there are no more
24917 operands. */
24918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24919 break;
24920 /* Consume the `,'. */
24921 cp_lexer_consume_token (parser->lexer);
24924 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24927 /* Parse an asm-clobber-list.
24929 asm-clobber-list:
24930 string-literal
24931 asm-clobber-list , string-literal
24933 Returns a TREE_LIST, indicating the clobbers in the order that they
24934 appeared. The TREE_VALUE of each node is a STRING_CST. */
24936 static tree
24937 cp_parser_asm_clobber_list (cp_parser* parser)
24939 tree clobbers = NULL_TREE;
24941 while (true)
24943 tree string_literal;
24945 /* Look for the string literal. */
24946 string_literal = cp_parser_string_literal (parser, false, false);
24947 /* Add it to the list. */
24948 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24949 /* If the next token is not a `,', then the list is
24950 complete. */
24951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24952 break;
24953 /* Consume the `,' token. */
24954 cp_lexer_consume_token (parser->lexer);
24957 return clobbers;
24960 /* Parse an asm-label-list.
24962 asm-label-list:
24963 identifier
24964 asm-label-list , identifier
24966 Returns a TREE_LIST, indicating the labels in the order that they
24967 appeared. The TREE_VALUE of each node is a label. */
24969 static tree
24970 cp_parser_asm_label_list (cp_parser* parser)
24972 tree labels = NULL_TREE;
24974 while (true)
24976 tree identifier, label, name;
24978 /* Look for the identifier. */
24979 identifier = cp_parser_identifier (parser);
24980 if (!error_operand_p (identifier))
24982 label = lookup_label (identifier);
24983 if (TREE_CODE (label) == LABEL_DECL)
24985 TREE_USED (label) = 1;
24986 check_goto (label);
24987 name = build_string (IDENTIFIER_LENGTH (identifier),
24988 IDENTIFIER_POINTER (identifier));
24989 labels = tree_cons (name, label, labels);
24992 /* If the next token is not a `,', then the list is
24993 complete. */
24994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24995 break;
24996 /* Consume the `,' token. */
24997 cp_lexer_consume_token (parser->lexer);
25000 return nreverse (labels);
25003 /* Return TRUE iff the next tokens in the stream are possibly the
25004 beginning of a GNU extension attribute. */
25006 static bool
25007 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25009 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25012 /* Return TRUE iff the next tokens in the stream are possibly the
25013 beginning of a standard C++-11 attribute specifier. */
25015 static bool
25016 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25018 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25021 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25022 beginning of a standard C++-11 attribute specifier. */
25024 static bool
25025 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25027 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25029 return (cxx_dialect >= cxx11
25030 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25031 || (token->type == CPP_OPEN_SQUARE
25032 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25033 && token->type == CPP_OPEN_SQUARE)));
25036 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25037 beginning of a GNU extension attribute. */
25039 static bool
25040 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25042 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25044 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25047 /* Return true iff the next tokens can be the beginning of either a
25048 GNU attribute list, or a standard C++11 attribute sequence. */
25050 static bool
25051 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25053 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25054 || cp_next_tokens_can_be_std_attribute_p (parser));
25057 /* Return true iff the next Nth tokens can be the beginning of either
25058 a GNU attribute list, or a standard C++11 attribute sequence. */
25060 static bool
25061 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25063 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25064 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25067 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25068 of GNU attributes, or return NULL. */
25070 static tree
25071 cp_parser_attributes_opt (cp_parser *parser)
25073 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25074 return cp_parser_gnu_attributes_opt (parser);
25075 return cp_parser_std_attribute_spec_seq (parser);
25078 /* Parse an (optional) series of attributes.
25080 attributes:
25081 attributes attribute
25083 attribute:
25084 __attribute__ (( attribute-list [opt] ))
25086 The return value is as for cp_parser_gnu_attribute_list. */
25088 static tree
25089 cp_parser_gnu_attributes_opt (cp_parser* parser)
25091 tree attributes = NULL_TREE;
25093 temp_override<bool> cleanup
25094 (parser->auto_is_implicit_function_template_parm_p, false);
25096 while (true)
25098 cp_token *token;
25099 tree attribute_list;
25100 bool ok = true;
25102 /* Peek at the next token. */
25103 token = cp_lexer_peek_token (parser->lexer);
25104 /* If it's not `__attribute__', then we're done. */
25105 if (token->keyword != RID_ATTRIBUTE)
25106 break;
25108 /* Consume the `__attribute__' keyword. */
25109 cp_lexer_consume_token (parser->lexer);
25110 /* Look for the two `(' tokens. */
25111 matching_parens outer_parens;
25112 outer_parens.require_open (parser);
25113 matching_parens inner_parens;
25114 inner_parens.require_open (parser);
25116 /* Peek at the next token. */
25117 token = cp_lexer_peek_token (parser->lexer);
25118 if (token->type != CPP_CLOSE_PAREN)
25119 /* Parse the attribute-list. */
25120 attribute_list = cp_parser_gnu_attribute_list (parser);
25121 else
25122 /* If the next token is a `)', then there is no attribute
25123 list. */
25124 attribute_list = NULL;
25126 /* Look for the two `)' tokens. */
25127 if (!inner_parens.require_close (parser))
25128 ok = false;
25129 if (!outer_parens.require_close (parser))
25130 ok = false;
25131 if (!ok)
25132 cp_parser_skip_to_end_of_statement (parser);
25134 /* Add these new attributes to the list. */
25135 attributes = attr_chainon (attributes, attribute_list);
25138 return attributes;
25141 /* Parse a GNU attribute-list.
25143 attribute-list:
25144 attribute
25145 attribute-list , attribute
25147 attribute:
25148 identifier
25149 identifier ( identifier )
25150 identifier ( identifier , expression-list )
25151 identifier ( expression-list )
25153 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25154 to an attribute. The TREE_PURPOSE of each node is the identifier
25155 indicating which attribute is in use. The TREE_VALUE represents
25156 the arguments, if any. */
25158 static tree
25159 cp_parser_gnu_attribute_list (cp_parser* parser)
25161 tree attribute_list = NULL_TREE;
25162 bool save_translate_strings_p = parser->translate_strings_p;
25164 parser->translate_strings_p = false;
25165 while (true)
25167 cp_token *token;
25168 tree identifier;
25169 tree attribute;
25171 /* Look for the identifier. We also allow keywords here; for
25172 example `__attribute__ ((const))' is legal. */
25173 token = cp_lexer_peek_token (parser->lexer);
25174 if (token->type == CPP_NAME
25175 || token->type == CPP_KEYWORD)
25177 tree arguments = NULL_TREE;
25179 /* Consume the token, but save it since we need it for the
25180 SIMD enabled function parsing. */
25181 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25183 /* Save away the identifier that indicates which attribute
25184 this is. */
25185 identifier = (token->type == CPP_KEYWORD)
25186 /* For keywords, use the canonical spelling, not the
25187 parsed identifier. */
25188 ? ridpointers[(int) token->keyword]
25189 : id_token->u.value;
25191 identifier = canonicalize_attr_name (identifier);
25192 attribute = build_tree_list (identifier, NULL_TREE);
25194 /* Peek at the next token. */
25195 token = cp_lexer_peek_token (parser->lexer);
25196 /* If it's an `(', then parse the attribute arguments. */
25197 if (token->type == CPP_OPEN_PAREN)
25199 vec<tree, va_gc> *vec;
25200 int attr_flag = (attribute_takes_identifier_p (identifier)
25201 ? id_attr : normal_attr);
25202 vec = cp_parser_parenthesized_expression_list
25203 (parser, attr_flag, /*cast_p=*/false,
25204 /*allow_expansion_p=*/false,
25205 /*non_constant_p=*/NULL);
25206 if (vec == NULL)
25207 arguments = error_mark_node;
25208 else
25210 arguments = build_tree_list_vec (vec);
25211 release_tree_vector (vec);
25213 /* Save the arguments away. */
25214 TREE_VALUE (attribute) = arguments;
25217 if (arguments != error_mark_node)
25219 /* Add this attribute to the list. */
25220 TREE_CHAIN (attribute) = attribute_list;
25221 attribute_list = attribute;
25224 token = cp_lexer_peek_token (parser->lexer);
25226 /* Now, look for more attributes. If the next token isn't a
25227 `,', we're done. */
25228 if (token->type != CPP_COMMA)
25229 break;
25231 /* Consume the comma and keep going. */
25232 cp_lexer_consume_token (parser->lexer);
25234 parser->translate_strings_p = save_translate_strings_p;
25236 /* We built up the list in reverse order. */
25237 return nreverse (attribute_list);
25240 /* Parse a standard C++11 attribute.
25242 The returned representation is a TREE_LIST which TREE_PURPOSE is
25243 the scoped name of the attribute, and the TREE_VALUE is its
25244 arguments list.
25246 Note that the scoped name of the attribute is itself a TREE_LIST
25247 which TREE_PURPOSE is the namespace of the attribute, and
25248 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25249 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25250 and which TREE_PURPOSE is directly the attribute name.
25252 Clients of the attribute code should use get_attribute_namespace
25253 and get_attribute_name to get the actual namespace and name of
25254 attributes, regardless of their being GNU or C++11 attributes.
25256 attribute:
25257 attribute-token attribute-argument-clause [opt]
25259 attribute-token:
25260 identifier
25261 attribute-scoped-token
25263 attribute-scoped-token:
25264 attribute-namespace :: identifier
25266 attribute-namespace:
25267 identifier
25269 attribute-argument-clause:
25270 ( balanced-token-seq )
25272 balanced-token-seq:
25273 balanced-token [opt]
25274 balanced-token-seq balanced-token
25276 balanced-token:
25277 ( balanced-token-seq )
25278 [ balanced-token-seq ]
25279 { balanced-token-seq }. */
25281 static tree
25282 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25284 tree attribute, attr_id = NULL_TREE, arguments;
25285 cp_token *token;
25287 temp_override<bool> cleanup
25288 (parser->auto_is_implicit_function_template_parm_p, false);
25290 /* First, parse name of the attribute, a.k.a attribute-token. */
25292 token = cp_lexer_peek_token (parser->lexer);
25293 if (token->type == CPP_NAME)
25294 attr_id = token->u.value;
25295 else if (token->type == CPP_KEYWORD)
25296 attr_id = ridpointers[(int) token->keyword];
25297 else if (token->flags & NAMED_OP)
25298 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25300 if (attr_id == NULL_TREE)
25301 return NULL_TREE;
25303 cp_lexer_consume_token (parser->lexer);
25305 token = cp_lexer_peek_token (parser->lexer);
25306 if (token->type == CPP_SCOPE)
25308 /* We are seeing a scoped attribute token. */
25310 cp_lexer_consume_token (parser->lexer);
25311 if (attr_ns)
25312 error_at (token->location, "attribute using prefix used together "
25313 "with scoped attribute token");
25314 attr_ns = attr_id;
25316 token = cp_lexer_consume_token (parser->lexer);
25317 if (token->type == CPP_NAME)
25318 attr_id = token->u.value;
25319 else if (token->type == CPP_KEYWORD)
25320 attr_id = ridpointers[(int) token->keyword];
25321 else if (token->flags & NAMED_OP)
25322 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25323 else
25325 error_at (token->location,
25326 "expected an identifier for the attribute name");
25327 return error_mark_node;
25330 attr_id = canonicalize_attr_name (attr_id);
25331 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25332 NULL_TREE);
25333 token = cp_lexer_peek_token (parser->lexer);
25335 else if (attr_ns)
25336 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25337 NULL_TREE);
25338 else
25340 attr_id = canonicalize_attr_name (attr_id);
25341 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25342 NULL_TREE);
25343 /* C++11 noreturn attribute is equivalent to GNU's. */
25344 if (is_attribute_p ("noreturn", attr_id))
25345 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25346 /* C++14 deprecated attribute is equivalent to GNU's. */
25347 else if (is_attribute_p ("deprecated", attr_id))
25348 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25349 /* C++17 fallthrough attribute is equivalent to GNU's. */
25350 else if (is_attribute_p ("fallthrough", attr_id))
25351 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25352 /* Transactional Memory TS optimize_for_synchronized attribute is
25353 equivalent to GNU transaction_callable. */
25354 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25355 TREE_PURPOSE (attribute)
25356 = get_identifier ("transaction_callable");
25357 /* Transactional Memory attributes are GNU attributes. */
25358 else if (tm_attr_to_mask (attr_id))
25359 TREE_PURPOSE (attribute) = attr_id;
25362 /* Now parse the optional argument clause of the attribute. */
25364 if (token->type != CPP_OPEN_PAREN)
25365 return attribute;
25368 vec<tree, va_gc> *vec;
25369 int attr_flag = normal_attr;
25371 if (attr_ns == gnu_identifier
25372 && attribute_takes_identifier_p (attr_id))
25373 /* A GNU attribute that takes an identifier in parameter. */
25374 attr_flag = id_attr;
25376 vec = cp_parser_parenthesized_expression_list
25377 (parser, attr_flag, /*cast_p=*/false,
25378 /*allow_expansion_p=*/true,
25379 /*non_constant_p=*/NULL);
25380 if (vec == NULL)
25381 arguments = error_mark_node;
25382 else
25384 arguments = build_tree_list_vec (vec);
25385 release_tree_vector (vec);
25388 if (arguments == error_mark_node)
25389 attribute = error_mark_node;
25390 else
25391 TREE_VALUE (attribute) = arguments;
25394 return attribute;
25397 /* Check that the attribute ATTRIBUTE appears at most once in the
25398 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25399 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25400 isn't implemented yet in GCC. */
25402 static void
25403 cp_parser_check_std_attribute (tree attributes, tree attribute)
25405 if (attributes)
25407 tree name = get_attribute_name (attribute);
25408 if (is_attribute_p ("noreturn", name)
25409 && lookup_attribute ("noreturn", attributes))
25410 error ("attribute %<noreturn%> can appear at most once "
25411 "in an attribute-list");
25412 else if (is_attribute_p ("deprecated", name)
25413 && lookup_attribute ("deprecated", attributes))
25414 error ("attribute %<deprecated%> can appear at most once "
25415 "in an attribute-list");
25419 /* Parse a list of standard C++-11 attributes.
25421 attribute-list:
25422 attribute [opt]
25423 attribute-list , attribute[opt]
25424 attribute ...
25425 attribute-list , attribute ...
25428 static tree
25429 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25431 tree attributes = NULL_TREE, attribute = NULL_TREE;
25432 cp_token *token = NULL;
25434 while (true)
25436 attribute = cp_parser_std_attribute (parser, attr_ns);
25437 if (attribute == error_mark_node)
25438 break;
25439 if (attribute != NULL_TREE)
25441 cp_parser_check_std_attribute (attributes, attribute);
25442 TREE_CHAIN (attribute) = attributes;
25443 attributes = attribute;
25445 token = cp_lexer_peek_token (parser->lexer);
25446 if (token->type == CPP_ELLIPSIS)
25448 cp_lexer_consume_token (parser->lexer);
25449 if (attribute == NULL_TREE)
25450 error_at (token->location,
25451 "expected attribute before %<...%>");
25452 else
25454 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25455 if (pack == error_mark_node)
25456 return error_mark_node;
25457 TREE_VALUE (attribute) = pack;
25459 token = cp_lexer_peek_token (parser->lexer);
25461 if (token->type != CPP_COMMA)
25462 break;
25463 cp_lexer_consume_token (parser->lexer);
25465 attributes = nreverse (attributes);
25466 return attributes;
25469 /* Parse a standard C++-11 attribute specifier.
25471 attribute-specifier:
25472 [ [ attribute-using-prefix [opt] attribute-list ] ]
25473 alignment-specifier
25475 attribute-using-prefix:
25476 using attribute-namespace :
25478 alignment-specifier:
25479 alignas ( type-id ... [opt] )
25480 alignas ( alignment-expression ... [opt] ). */
25482 static tree
25483 cp_parser_std_attribute_spec (cp_parser *parser)
25485 tree attributes = NULL_TREE;
25486 cp_token *token = cp_lexer_peek_token (parser->lexer);
25488 if (token->type == CPP_OPEN_SQUARE
25489 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25491 tree attr_ns = NULL_TREE;
25493 cp_lexer_consume_token (parser->lexer);
25494 cp_lexer_consume_token (parser->lexer);
25496 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25498 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25499 if (token->type == CPP_NAME)
25500 attr_ns = token->u.value;
25501 else if (token->type == CPP_KEYWORD)
25502 attr_ns = ridpointers[(int) token->keyword];
25503 else if (token->flags & NAMED_OP)
25504 attr_ns = get_identifier (cpp_type2name (token->type,
25505 token->flags));
25506 if (attr_ns
25507 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25509 if (cxx_dialect < cxx17
25510 && !in_system_header_at (input_location))
25511 pedwarn (input_location, 0,
25512 "attribute using prefix only available "
25513 "with -std=c++17 or -std=gnu++17");
25515 cp_lexer_consume_token (parser->lexer);
25516 cp_lexer_consume_token (parser->lexer);
25517 cp_lexer_consume_token (parser->lexer);
25519 else
25520 attr_ns = NULL_TREE;
25523 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25525 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25526 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25527 cp_parser_skip_to_end_of_statement (parser);
25528 else
25529 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25530 when we are sure that we have actually parsed them. */
25531 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25533 else
25535 tree alignas_expr;
25537 /* Look for an alignment-specifier. */
25539 token = cp_lexer_peek_token (parser->lexer);
25541 if (token->type != CPP_KEYWORD
25542 || token->keyword != RID_ALIGNAS)
25543 return NULL_TREE;
25545 cp_lexer_consume_token (parser->lexer);
25546 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25548 matching_parens parens;
25549 if (!parens.require_open (parser))
25550 return error_mark_node;
25552 cp_parser_parse_tentatively (parser);
25553 alignas_expr = cp_parser_type_id (parser);
25555 if (!cp_parser_parse_definitely (parser))
25557 alignas_expr = cp_parser_assignment_expression (parser);
25558 if (alignas_expr == error_mark_node)
25559 cp_parser_skip_to_end_of_statement (parser);
25560 if (alignas_expr == NULL_TREE
25561 || alignas_expr == error_mark_node)
25562 return alignas_expr;
25565 alignas_expr = cxx_alignas_expr (alignas_expr);
25566 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25568 /* Handle alignas (pack...). */
25569 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25571 cp_lexer_consume_token (parser->lexer);
25572 alignas_expr = make_pack_expansion (alignas_expr);
25575 /* Something went wrong, so don't build the attribute. */
25576 if (alignas_expr == error_mark_node)
25577 return error_mark_node;
25579 if (!parens.require_close (parser))
25580 return error_mark_node;
25582 /* Build the C++-11 representation of an 'aligned'
25583 attribute. */
25584 attributes
25585 = build_tree_list (build_tree_list (gnu_identifier,
25586 aligned_identifier), alignas_expr);
25589 return attributes;
25592 /* Parse a standard C++-11 attribute-specifier-seq.
25594 attribute-specifier-seq:
25595 attribute-specifier-seq [opt] attribute-specifier
25598 static tree
25599 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25601 tree attr_specs = NULL_TREE;
25602 tree attr_last = NULL_TREE;
25604 while (true)
25606 tree attr_spec = cp_parser_std_attribute_spec (parser);
25607 if (attr_spec == NULL_TREE)
25608 break;
25609 if (attr_spec == error_mark_node)
25610 return error_mark_node;
25612 if (attr_last)
25613 TREE_CHAIN (attr_last) = attr_spec;
25614 else
25615 attr_specs = attr_last = attr_spec;
25616 attr_last = tree_last (attr_last);
25619 return attr_specs;
25622 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25623 return index of the first token after balanced-token, or N on failure. */
25625 static size_t
25626 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25628 size_t orig_n = n;
25629 int nparens = 0, nbraces = 0, nsquares = 0;
25631 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25633 case CPP_EOF:
25634 case CPP_PRAGMA_EOL:
25635 /* Ran out of tokens. */
25636 return orig_n;
25637 case CPP_OPEN_PAREN:
25638 ++nparens;
25639 break;
25640 case CPP_OPEN_BRACE:
25641 ++nbraces;
25642 break;
25643 case CPP_OPEN_SQUARE:
25644 ++nsquares;
25645 break;
25646 case CPP_CLOSE_PAREN:
25647 --nparens;
25648 break;
25649 case CPP_CLOSE_BRACE:
25650 --nbraces;
25651 break;
25652 case CPP_CLOSE_SQUARE:
25653 --nsquares;
25654 break;
25655 default:
25656 break;
25658 while (nparens || nbraces || nsquares);
25659 return n;
25662 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25663 return index of the first token after the GNU attribute tokens, or N on
25664 failure. */
25666 static size_t
25667 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25669 while (true)
25671 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25672 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25673 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25674 break;
25676 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25677 if (n2 == n + 2)
25678 break;
25679 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25680 break;
25681 n = n2 + 1;
25683 return n;
25686 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25687 next token), return index of the first token after the standard C++11
25688 attribute tokens, or N on failure. */
25690 static size_t
25691 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25693 while (true)
25695 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25696 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25698 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25699 if (n2 == n + 1)
25700 break;
25701 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25702 break;
25703 n = n2 + 1;
25705 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25706 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25708 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25709 if (n2 == n + 1)
25710 break;
25711 n = n2;
25713 else
25714 break;
25716 return n;
25719 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25720 as the next token), return index of the first token after the attribute
25721 tokens, or N on failure. */
25723 static size_t
25724 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25726 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25727 return cp_parser_skip_gnu_attributes_opt (parser, n);
25728 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25731 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25732 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25733 current value of the PEDANTIC flag, regardless of whether or not
25734 the `__extension__' keyword is present. The caller is responsible
25735 for restoring the value of the PEDANTIC flag. */
25737 static bool
25738 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25740 /* Save the old value of the PEDANTIC flag. */
25741 *saved_pedantic = pedantic;
25743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25745 /* Consume the `__extension__' token. */
25746 cp_lexer_consume_token (parser->lexer);
25747 /* We're not being pedantic while the `__extension__' keyword is
25748 in effect. */
25749 pedantic = 0;
25751 return true;
25754 return false;
25757 /* Parse a label declaration.
25759 label-declaration:
25760 __label__ label-declarator-seq ;
25762 label-declarator-seq:
25763 identifier , label-declarator-seq
25764 identifier */
25766 static void
25767 cp_parser_label_declaration (cp_parser* parser)
25769 /* Look for the `__label__' keyword. */
25770 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25772 while (true)
25774 tree identifier;
25776 /* Look for an identifier. */
25777 identifier = cp_parser_identifier (parser);
25778 /* If we failed, stop. */
25779 if (identifier == error_mark_node)
25780 break;
25781 /* Declare it as a label. */
25782 finish_label_decl (identifier);
25783 /* If the next token is a `;', stop. */
25784 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25785 break;
25786 /* Look for the `,' separating the label declarations. */
25787 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25790 /* Look for the final `;'. */
25791 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25794 // -------------------------------------------------------------------------- //
25795 // Requires Clause
25797 // Parse a requires clause.
25799 // requires-clause:
25800 // 'requires' logical-or-expression
25802 // The required logical-or-expression must be a constant expression. Note
25803 // that we don't check that the expression is constepxr here. We defer until
25804 // we analyze constraints and then, we only check atomic constraints.
25805 static tree
25806 cp_parser_requires_clause (cp_parser *parser)
25808 // Parse the requires clause so that it is not automatically folded.
25809 ++processing_template_decl;
25810 tree expr = cp_parser_binary_expression (parser, false, false,
25811 PREC_NOT_OPERATOR, NULL);
25812 if (check_for_bare_parameter_packs (expr))
25813 expr = error_mark_node;
25814 --processing_template_decl;
25815 return expr;
25818 // Optionally parse a requires clause:
25819 static tree
25820 cp_parser_requires_clause_opt (cp_parser *parser)
25822 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25823 if (tok->keyword != RID_REQUIRES)
25825 if (!flag_concepts && tok->type == CPP_NAME
25826 && tok->u.value == ridpointers[RID_REQUIRES])
25828 error_at (cp_lexer_peek_token (parser->lexer)->location,
25829 "%<requires%> only available with -fconcepts");
25830 /* Parse and discard the requires-clause. */
25831 cp_lexer_consume_token (parser->lexer);
25832 cp_parser_requires_clause (parser);
25834 return NULL_TREE;
25836 cp_lexer_consume_token (parser->lexer);
25837 return cp_parser_requires_clause (parser);
25841 /*---------------------------------------------------------------------------
25842 Requires expressions
25843 ---------------------------------------------------------------------------*/
25845 /* Parse a requires expression
25847 requirement-expression:
25848 'requires' requirement-parameter-list [opt] requirement-body */
25849 static tree
25850 cp_parser_requires_expression (cp_parser *parser)
25852 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25853 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25855 /* A requires-expression shall appear only within a concept
25856 definition or a requires-clause.
25858 TODO: Implement this diagnostic correctly. */
25859 if (!processing_template_decl)
25861 error_at (loc, "a requires expression cannot appear outside a template");
25862 cp_parser_skip_to_end_of_statement (parser);
25863 return error_mark_node;
25866 tree parms, reqs;
25868 /* Local parameters are delared as variables within the scope
25869 of the expression. They are not visible past the end of
25870 the expression. Expressions within the requires-expression
25871 are unevaluated. */
25872 struct scope_sentinel
25874 scope_sentinel ()
25876 ++cp_unevaluated_operand;
25877 begin_scope (sk_block, NULL_TREE);
25880 ~scope_sentinel ()
25882 pop_bindings_and_leave_scope ();
25883 --cp_unevaluated_operand;
25885 } s;
25887 /* Parse the optional parameter list. */
25888 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25890 parms = cp_parser_requirement_parameter_list (parser);
25891 if (parms == error_mark_node)
25892 return error_mark_node;
25894 else
25895 parms = NULL_TREE;
25897 /* Parse the requirement body. */
25898 reqs = cp_parser_requirement_body (parser);
25899 if (reqs == error_mark_node)
25900 return error_mark_node;
25903 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25904 the parm chain. */
25905 grokparms (parms, &parms);
25906 return finish_requires_expr (parms, reqs);
25909 /* Parse a parameterized requirement.
25911 requirement-parameter-list:
25912 '(' parameter-declaration-clause ')' */
25913 static tree
25914 cp_parser_requirement_parameter_list (cp_parser *parser)
25916 matching_parens parens;
25917 if (!parens.require_open (parser))
25918 return error_mark_node;
25920 tree parms = cp_parser_parameter_declaration_clause (parser);
25922 if (!parens.require_close (parser))
25923 return error_mark_node;
25925 return parms;
25928 /* Parse the body of a requirement.
25930 requirement-body:
25931 '{' requirement-list '}' */
25932 static tree
25933 cp_parser_requirement_body (cp_parser *parser)
25935 matching_braces braces;
25936 if (!braces.require_open (parser))
25937 return error_mark_node;
25939 tree reqs = cp_parser_requirement_list (parser);
25941 if (!braces.require_close (parser))
25942 return error_mark_node;
25944 return reqs;
25947 /* Parse a list of requirements.
25949 requirement-list:
25950 requirement
25951 requirement-list ';' requirement[opt] */
25952 static tree
25953 cp_parser_requirement_list (cp_parser *parser)
25955 tree result = NULL_TREE;
25956 while (true)
25958 tree req = cp_parser_requirement (parser);
25959 if (req == error_mark_node)
25960 return error_mark_node;
25962 result = tree_cons (NULL_TREE, req, result);
25964 /* If we see a semi-colon, consume it. */
25965 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25966 cp_lexer_consume_token (parser->lexer);
25968 /* Stop processing at the end of the list. */
25969 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25970 break;
25973 /* Reverse the order of requirements so they are analyzed in
25974 declaration order. */
25975 return nreverse (result);
25978 /* Parse a syntactic requirement or type requirement.
25980 requirement:
25981 simple-requirement
25982 compound-requirement
25983 type-requirement
25984 nested-requirement */
25985 static tree
25986 cp_parser_requirement (cp_parser *parser)
25988 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25989 return cp_parser_compound_requirement (parser);
25990 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25991 return cp_parser_type_requirement (parser);
25992 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25993 return cp_parser_nested_requirement (parser);
25994 else
25995 return cp_parser_simple_requirement (parser);
25998 /* Parse a simple requirement.
26000 simple-requirement:
26001 expression ';' */
26002 static tree
26003 cp_parser_simple_requirement (cp_parser *parser)
26005 tree expr = cp_parser_expression (parser, NULL, false, false);
26006 if (!expr || expr == error_mark_node)
26007 return error_mark_node;
26009 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26010 return error_mark_node;
26012 return finish_simple_requirement (expr);
26015 /* Parse a type requirement
26017 type-requirement
26018 nested-name-specifier [opt] required-type-name ';'
26020 required-type-name:
26021 type-name
26022 'template' [opt] simple-template-id */
26023 static tree
26024 cp_parser_type_requirement (cp_parser *parser)
26026 cp_lexer_consume_token (parser->lexer);
26028 // Save the scope before parsing name specifiers.
26029 tree saved_scope = parser->scope;
26030 tree saved_object_scope = parser->object_scope;
26031 tree saved_qualifying_scope = parser->qualifying_scope;
26032 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26033 cp_parser_nested_name_specifier_opt (parser,
26034 /*typename_keyword_p=*/true,
26035 /*check_dependency_p=*/false,
26036 /*type_p=*/true,
26037 /*is_declaration=*/false);
26039 tree type;
26040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26042 cp_lexer_consume_token (parser->lexer);
26043 type = cp_parser_template_id (parser,
26044 /*template_keyword_p=*/true,
26045 /*check_dependency=*/false,
26046 /*tag_type=*/none_type,
26047 /*is_declaration=*/false);
26048 type = make_typename_type (parser->scope, type, typename_type,
26049 /*complain=*/tf_error);
26051 else
26052 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26054 if (TREE_CODE (type) == TYPE_DECL)
26055 type = TREE_TYPE (type);
26057 parser->scope = saved_scope;
26058 parser->object_scope = saved_object_scope;
26059 parser->qualifying_scope = saved_qualifying_scope;
26061 if (type == error_mark_node)
26062 cp_parser_skip_to_end_of_statement (parser);
26064 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26065 return error_mark_node;
26066 if (type == error_mark_node)
26067 return error_mark_node;
26069 return finish_type_requirement (type);
26072 /* Parse a compound requirement
26074 compound-requirement:
26075 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26076 static tree
26077 cp_parser_compound_requirement (cp_parser *parser)
26079 /* Parse an expression enclosed in '{ }'s. */
26080 matching_braces braces;
26081 if (!braces.require_open (parser))
26082 return error_mark_node;
26084 tree expr = cp_parser_expression (parser, NULL, false, false);
26085 if (!expr || expr == error_mark_node)
26086 return error_mark_node;
26088 if (!braces.require_close (parser))
26089 return error_mark_node;
26091 /* Parse the optional noexcept. */
26092 bool noexcept_p = false;
26093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26095 cp_lexer_consume_token (parser->lexer);
26096 noexcept_p = true;
26099 /* Parse the optional trailing return type. */
26100 tree type = NULL_TREE;
26101 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26103 cp_lexer_consume_token (parser->lexer);
26104 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26105 parser->in_result_type_constraint_p = true;
26106 type = cp_parser_trailing_type_id (parser);
26107 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26108 if (type == error_mark_node)
26109 return error_mark_node;
26112 return finish_compound_requirement (expr, type, noexcept_p);
26115 /* Parse a nested requirement. This is the same as a requires clause.
26117 nested-requirement:
26118 requires-clause */
26119 static tree
26120 cp_parser_nested_requirement (cp_parser *parser)
26122 cp_lexer_consume_token (parser->lexer);
26123 tree req = cp_parser_requires_clause (parser);
26124 if (req == error_mark_node)
26125 return error_mark_node;
26126 return finish_nested_requirement (req);
26129 /* Support Functions */
26131 /* Return the appropriate prefer_type argument for lookup_name_real based on
26132 tag_type and template_mem_access. */
26134 static inline int
26135 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26137 /* DR 141: When looking in the current enclosing context for a template-name
26138 after -> or ., only consider class templates. */
26139 if (template_mem_access)
26140 return 2;
26141 switch (tag_type)
26143 case none_type: return 0; // No preference.
26144 case scope_type: return 1; // Type or namespace.
26145 default: return 2; // Type only.
26149 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26150 NAME should have one of the representations used for an
26151 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26152 is returned. If PARSER->SCOPE is a dependent type, then a
26153 SCOPE_REF is returned.
26155 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26156 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26157 was formed. Abstractly, such entities should not be passed to this
26158 function, because they do not need to be looked up, but it is
26159 simpler to check for this special case here, rather than at the
26160 call-sites.
26162 In cases not explicitly covered above, this function returns a
26163 DECL, OVERLOAD, or baselink representing the result of the lookup.
26164 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26165 is returned.
26167 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26168 (e.g., "struct") that was used. In that case bindings that do not
26169 refer to types are ignored.
26171 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26172 ignored.
26174 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26175 are ignored.
26177 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26178 types.
26180 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26181 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26182 NULL_TREE otherwise. */
26184 static cp_expr
26185 cp_parser_lookup_name (cp_parser *parser, tree name,
26186 enum tag_types tag_type,
26187 bool is_template,
26188 bool is_namespace,
26189 bool check_dependency,
26190 tree *ambiguous_decls,
26191 location_t name_location)
26193 tree decl;
26194 tree object_type = parser->context->object_type;
26196 /* Assume that the lookup will be unambiguous. */
26197 if (ambiguous_decls)
26198 *ambiguous_decls = NULL_TREE;
26200 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26201 no longer valid. Note that if we are parsing tentatively, and
26202 the parse fails, OBJECT_TYPE will be automatically restored. */
26203 parser->context->object_type = NULL_TREE;
26205 if (name == error_mark_node)
26206 return error_mark_node;
26208 /* A template-id has already been resolved; there is no lookup to
26209 do. */
26210 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26211 return name;
26212 if (BASELINK_P (name))
26214 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26215 == TEMPLATE_ID_EXPR);
26216 return name;
26219 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26220 it should already have been checked to make sure that the name
26221 used matches the type being destroyed. */
26222 if (TREE_CODE (name) == BIT_NOT_EXPR)
26224 tree type;
26226 /* Figure out to which type this destructor applies. */
26227 if (parser->scope)
26228 type = parser->scope;
26229 else if (object_type)
26230 type = object_type;
26231 else
26232 type = current_class_type;
26233 /* If that's not a class type, there is no destructor. */
26234 if (!type || !CLASS_TYPE_P (type))
26235 return error_mark_node;
26237 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26238 lazily_declare_fn (sfk_destructor, type);
26240 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26241 return dtor;
26243 return error_mark_node;
26246 /* By this point, the NAME should be an ordinary identifier. If
26247 the id-expression was a qualified name, the qualifying scope is
26248 stored in PARSER->SCOPE at this point. */
26249 gcc_assert (identifier_p (name));
26251 /* Perform the lookup. */
26252 if (parser->scope)
26254 bool dependent_p;
26256 if (parser->scope == error_mark_node)
26257 return error_mark_node;
26259 /* If the SCOPE is dependent, the lookup must be deferred until
26260 the template is instantiated -- unless we are explicitly
26261 looking up names in uninstantiated templates. Even then, we
26262 cannot look up the name if the scope is not a class type; it
26263 might, for example, be a template type parameter. */
26264 dependent_p = (TYPE_P (parser->scope)
26265 && dependent_scope_p (parser->scope));
26266 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26267 && dependent_p)
26268 /* Defer lookup. */
26269 decl = error_mark_node;
26270 else
26272 tree pushed_scope = NULL_TREE;
26274 /* If PARSER->SCOPE is a dependent type, then it must be a
26275 class type, and we must not be checking dependencies;
26276 otherwise, we would have processed this lookup above. So
26277 that PARSER->SCOPE is not considered a dependent base by
26278 lookup_member, we must enter the scope here. */
26279 if (dependent_p)
26280 pushed_scope = push_scope (parser->scope);
26282 /* If the PARSER->SCOPE is a template specialization, it
26283 may be instantiated during name lookup. In that case,
26284 errors may be issued. Even if we rollback the current
26285 tentative parse, those errors are valid. */
26286 decl = lookup_qualified_name (parser->scope, name,
26287 prefer_type_arg (tag_type),
26288 /*complain=*/true);
26290 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26291 lookup result and the nested-name-specifier nominates a class C:
26292 * if the name specified after the nested-name-specifier, when
26293 looked up in C, is the injected-class-name of C (Clause 9), or
26294 * if the name specified after the nested-name-specifier is the
26295 same as the identifier or the simple-template-id's template-
26296 name in the last component of the nested-name-specifier,
26297 the name is instead considered to name the constructor of
26298 class C. [ Note: for example, the constructor is not an
26299 acceptable lookup result in an elaborated-type-specifier so
26300 the constructor would not be used in place of the
26301 injected-class-name. --end note ] Such a constructor name
26302 shall be used only in the declarator-id of a declaration that
26303 names a constructor or in a using-declaration. */
26304 if (tag_type == none_type
26305 && DECL_SELF_REFERENCE_P (decl)
26306 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26307 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26308 prefer_type_arg (tag_type),
26309 /*complain=*/true);
26311 /* If we have a single function from a using decl, pull it out. */
26312 if (TREE_CODE (decl) == OVERLOAD
26313 && !really_overloaded_fn (decl))
26314 decl = OVL_FUNCTION (decl);
26316 if (pushed_scope)
26317 pop_scope (pushed_scope);
26320 /* If the scope is a dependent type and either we deferred lookup or
26321 we did lookup but didn't find the name, rememeber the name. */
26322 if (decl == error_mark_node && TYPE_P (parser->scope)
26323 && dependent_type_p (parser->scope))
26325 if (tag_type)
26327 tree type;
26329 /* The resolution to Core Issue 180 says that `struct
26330 A::B' should be considered a type-name, even if `A'
26331 is dependent. */
26332 type = make_typename_type (parser->scope, name, tag_type,
26333 /*complain=*/tf_error);
26334 if (type != error_mark_node)
26335 decl = TYPE_NAME (type);
26337 else if (is_template
26338 && (cp_parser_next_token_ends_template_argument_p (parser)
26339 || cp_lexer_next_token_is (parser->lexer,
26340 CPP_CLOSE_PAREN)))
26341 decl = make_unbound_class_template (parser->scope,
26342 name, NULL_TREE,
26343 /*complain=*/tf_error);
26344 else
26345 decl = build_qualified_name (/*type=*/NULL_TREE,
26346 parser->scope, name,
26347 is_template);
26349 parser->qualifying_scope = parser->scope;
26350 parser->object_scope = NULL_TREE;
26352 else if (object_type)
26354 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26355 OBJECT_TYPE is not a class. */
26356 if (CLASS_TYPE_P (object_type))
26357 /* If the OBJECT_TYPE is a template specialization, it may
26358 be instantiated during name lookup. In that case, errors
26359 may be issued. Even if we rollback the current tentative
26360 parse, those errors are valid. */
26361 decl = lookup_member (object_type,
26362 name,
26363 /*protect=*/0,
26364 prefer_type_arg (tag_type),
26365 tf_warning_or_error);
26366 else
26367 decl = NULL_TREE;
26369 if (!decl)
26370 /* Look it up in the enclosing context. DR 141: When looking for a
26371 template-name after -> or ., only consider class templates. */
26372 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26373 /*nonclass=*/0,
26374 /*block_p=*/true, is_namespace, 0);
26375 if (object_type == unknown_type_node)
26376 /* The object is type-dependent, so we can't look anything up; we used
26377 this to get the DR 141 behavior. */
26378 object_type = NULL_TREE;
26379 parser->object_scope = object_type;
26380 parser->qualifying_scope = NULL_TREE;
26382 else
26384 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26385 /*nonclass=*/0,
26386 /*block_p=*/true, is_namespace, 0);
26387 parser->qualifying_scope = NULL_TREE;
26388 parser->object_scope = NULL_TREE;
26391 /* If the lookup failed, let our caller know. */
26392 if (!decl || decl == error_mark_node)
26393 return error_mark_node;
26395 /* Pull out the template from an injected-class-name (or multiple). */
26396 if (is_template)
26397 decl = maybe_get_template_decl_from_type_decl (decl);
26399 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26400 if (TREE_CODE (decl) == TREE_LIST)
26402 if (ambiguous_decls)
26403 *ambiguous_decls = decl;
26404 /* The error message we have to print is too complicated for
26405 cp_parser_error, so we incorporate its actions directly. */
26406 if (!cp_parser_simulate_error (parser))
26408 error_at (name_location, "reference to %qD is ambiguous",
26409 name);
26410 print_candidates (decl);
26412 return error_mark_node;
26415 gcc_assert (DECL_P (decl)
26416 || TREE_CODE (decl) == OVERLOAD
26417 || TREE_CODE (decl) == SCOPE_REF
26418 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26419 || BASELINK_P (decl));
26421 /* If we have resolved the name of a member declaration, check to
26422 see if the declaration is accessible. When the name resolves to
26423 set of overloaded functions, accessibility is checked when
26424 overload resolution is done.
26426 During an explicit instantiation, access is not checked at all,
26427 as per [temp.explicit]. */
26428 if (DECL_P (decl))
26429 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26431 maybe_record_typedef_use (decl);
26433 return cp_expr (decl, name_location);
26436 /* Like cp_parser_lookup_name, but for use in the typical case where
26437 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26438 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26440 static tree
26441 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26443 return cp_parser_lookup_name (parser, name,
26444 none_type,
26445 /*is_template=*/false,
26446 /*is_namespace=*/false,
26447 /*check_dependency=*/true,
26448 /*ambiguous_decls=*/NULL,
26449 location);
26452 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26453 the current context, return the TYPE_DECL. If TAG_NAME_P is
26454 true, the DECL indicates the class being defined in a class-head,
26455 or declared in an elaborated-type-specifier.
26457 Otherwise, return DECL. */
26459 static tree
26460 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26462 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26463 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26465 struct A {
26466 template <typename T> struct B;
26469 template <typename T> struct A::B {};
26471 Similarly, in an elaborated-type-specifier:
26473 namespace N { struct X{}; }
26475 struct A {
26476 template <typename T> friend struct N::X;
26479 However, if the DECL refers to a class type, and we are in
26480 the scope of the class, then the name lookup automatically
26481 finds the TYPE_DECL created by build_self_reference rather
26482 than a TEMPLATE_DECL. For example, in:
26484 template <class T> struct S {
26485 S s;
26488 there is no need to handle such case. */
26490 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26491 return DECL_TEMPLATE_RESULT (decl);
26493 return decl;
26496 /* If too many, or too few, template-parameter lists apply to the
26497 declarator, issue an error message. Returns TRUE if all went well,
26498 and FALSE otherwise. */
26500 static bool
26501 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26502 cp_declarator *declarator,
26503 location_t declarator_location)
26505 switch (declarator->kind)
26507 case cdk_id:
26509 unsigned num_templates = 0;
26510 tree scope = declarator->u.id.qualifying_scope;
26511 bool template_id_p = false;
26513 if (scope)
26514 num_templates = num_template_headers_for_class (scope);
26515 else if (TREE_CODE (declarator->u.id.unqualified_name)
26516 == TEMPLATE_ID_EXPR)
26518 /* If the DECLARATOR has the form `X<y>' then it uses one
26519 additional level of template parameters. */
26520 ++num_templates;
26521 template_id_p = true;
26524 return cp_parser_check_template_parameters
26525 (parser, num_templates, template_id_p, declarator_location,
26526 declarator);
26529 case cdk_function:
26530 case cdk_array:
26531 case cdk_pointer:
26532 case cdk_reference:
26533 case cdk_ptrmem:
26534 return (cp_parser_check_declarator_template_parameters
26535 (parser, declarator->declarator, declarator_location));
26537 case cdk_decomp:
26538 case cdk_error:
26539 return true;
26541 default:
26542 gcc_unreachable ();
26544 return false;
26547 /* NUM_TEMPLATES were used in the current declaration. If that is
26548 invalid, return FALSE and issue an error messages. Otherwise,
26549 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26550 declarator and we can print more accurate diagnostics. */
26552 static bool
26553 cp_parser_check_template_parameters (cp_parser* parser,
26554 unsigned num_templates,
26555 bool template_id_p,
26556 location_t location,
26557 cp_declarator *declarator)
26559 /* If there are the same number of template classes and parameter
26560 lists, that's OK. */
26561 if (parser->num_template_parameter_lists == num_templates)
26562 return true;
26563 /* If there are more, but only one more, and the name ends in an identifier,
26564 then we are declaring a primary template. That's OK too. */
26565 if (!template_id_p
26566 && parser->num_template_parameter_lists == num_templates + 1)
26567 return true;
26568 /* If there are more template classes than parameter lists, we have
26569 something like:
26571 template <class T> void S<T>::R<T>::f (); */
26572 if (parser->num_template_parameter_lists < num_templates)
26574 if (declarator && !current_function_decl)
26575 error_at (location, "specializing member %<%T::%E%> "
26576 "requires %<template<>%> syntax",
26577 declarator->u.id.qualifying_scope,
26578 declarator->u.id.unqualified_name);
26579 else if (declarator)
26580 error_at (location, "invalid declaration of %<%T::%E%>",
26581 declarator->u.id.qualifying_scope,
26582 declarator->u.id.unqualified_name);
26583 else
26584 error_at (location, "too few template-parameter-lists");
26585 return false;
26587 /* Otherwise, there are too many template parameter lists. We have
26588 something like:
26590 template <class T> template <class U> void S::f(); */
26591 error_at (location, "too many template-parameter-lists");
26592 return false;
26595 /* Parse an optional `::' token indicating that the following name is
26596 from the global namespace. If so, PARSER->SCOPE is set to the
26597 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26598 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26599 Returns the new value of PARSER->SCOPE, if the `::' token is
26600 present, and NULL_TREE otherwise. */
26602 static tree
26603 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26605 cp_token *token;
26607 /* Peek at the next token. */
26608 token = cp_lexer_peek_token (parser->lexer);
26609 /* If we're looking at a `::' token then we're starting from the
26610 global namespace, not our current location. */
26611 if (token->type == CPP_SCOPE)
26613 /* Consume the `::' token. */
26614 cp_lexer_consume_token (parser->lexer);
26615 /* Set the SCOPE so that we know where to start the lookup. */
26616 parser->scope = global_namespace;
26617 parser->qualifying_scope = global_namespace;
26618 parser->object_scope = NULL_TREE;
26620 return parser->scope;
26622 else if (!current_scope_valid_p)
26624 parser->scope = NULL_TREE;
26625 parser->qualifying_scope = NULL_TREE;
26626 parser->object_scope = NULL_TREE;
26629 return NULL_TREE;
26632 /* Returns TRUE if the upcoming token sequence is the start of a
26633 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26634 declarator is preceded by the `friend' specifier. */
26636 static bool
26637 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26639 bool constructor_p;
26640 bool outside_class_specifier_p;
26641 tree nested_name_specifier;
26642 cp_token *next_token;
26644 /* The common case is that this is not a constructor declarator, so
26645 try to avoid doing lots of work if at all possible. It's not
26646 valid declare a constructor at function scope. */
26647 if (parser->in_function_body)
26648 return false;
26649 /* And only certain tokens can begin a constructor declarator. */
26650 next_token = cp_lexer_peek_token (parser->lexer);
26651 if (next_token->type != CPP_NAME
26652 && next_token->type != CPP_SCOPE
26653 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26654 && next_token->type != CPP_TEMPLATE_ID)
26655 return false;
26657 /* Parse tentatively; we are going to roll back all of the tokens
26658 consumed here. */
26659 cp_parser_parse_tentatively (parser);
26660 /* Assume that we are looking at a constructor declarator. */
26661 constructor_p = true;
26663 /* Look for the optional `::' operator. */
26664 cp_parser_global_scope_opt (parser,
26665 /*current_scope_valid_p=*/false);
26666 /* Look for the nested-name-specifier. */
26667 nested_name_specifier
26668 = (cp_parser_nested_name_specifier_opt (parser,
26669 /*typename_keyword_p=*/false,
26670 /*check_dependency_p=*/false,
26671 /*type_p=*/false,
26672 /*is_declaration=*/false));
26674 outside_class_specifier_p = (!at_class_scope_p ()
26675 || !TYPE_BEING_DEFINED (current_class_type)
26676 || friend_p);
26678 /* Outside of a class-specifier, there must be a
26679 nested-name-specifier. Except in C++17 mode, where we
26680 might be declaring a guiding declaration. */
26681 if (!nested_name_specifier && outside_class_specifier_p
26682 && cxx_dialect < cxx17)
26683 constructor_p = false;
26684 else if (nested_name_specifier == error_mark_node)
26685 constructor_p = false;
26687 /* If we have a class scope, this is easy; DR 147 says that S::S always
26688 names the constructor, and no other qualified name could. */
26689 if (constructor_p && nested_name_specifier
26690 && CLASS_TYPE_P (nested_name_specifier))
26692 tree id = cp_parser_unqualified_id (parser,
26693 /*template_keyword_p=*/false,
26694 /*check_dependency_p=*/false,
26695 /*declarator_p=*/true,
26696 /*optional_p=*/false);
26697 if (is_overloaded_fn (id))
26698 id = DECL_NAME (get_first_fn (id));
26699 if (!constructor_name_p (id, nested_name_specifier))
26700 constructor_p = false;
26702 /* If we still think that this might be a constructor-declarator,
26703 look for a class-name. */
26704 else if (constructor_p)
26706 /* If we have:
26708 template <typename T> struct S {
26709 S();
26712 we must recognize that the nested `S' names a class. */
26713 if (cxx_dialect >= cxx17)
26714 cp_parser_parse_tentatively (parser);
26716 tree type_decl;
26717 type_decl = cp_parser_class_name (parser,
26718 /*typename_keyword_p=*/false,
26719 /*template_keyword_p=*/false,
26720 none_type,
26721 /*check_dependency_p=*/false,
26722 /*class_head_p=*/false,
26723 /*is_declaration=*/false);
26725 if (cxx_dialect >= cxx17
26726 && !cp_parser_parse_definitely (parser))
26728 type_decl = NULL_TREE;
26729 tree tmpl = cp_parser_template_name (parser,
26730 /*template_keyword*/false,
26731 /*check_dependency_p*/false,
26732 /*is_declaration*/false,
26733 none_type,
26734 /*is_identifier*/NULL);
26735 if (DECL_CLASS_TEMPLATE_P (tmpl)
26736 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26737 /* It's a deduction guide, return true. */;
26738 else
26739 cp_parser_simulate_error (parser);
26742 /* If there was no class-name, then this is not a constructor.
26743 Otherwise, if we are in a class-specifier and we aren't
26744 handling a friend declaration, check that its type matches
26745 current_class_type (c++/38313). Note: error_mark_node
26746 is left alone for error recovery purposes. */
26747 constructor_p = (!cp_parser_error_occurred (parser)
26748 && (outside_class_specifier_p
26749 || type_decl == NULL_TREE
26750 || type_decl == error_mark_node
26751 || same_type_p (current_class_type,
26752 TREE_TYPE (type_decl))));
26754 /* If we're still considering a constructor, we have to see a `(',
26755 to begin the parameter-declaration-clause, followed by either a
26756 `)', an `...', or a decl-specifier. We need to check for a
26757 type-specifier to avoid being fooled into thinking that:
26759 S (f) (int);
26761 is a constructor. (It is actually a function named `f' that
26762 takes one parameter (of type `int') and returns a value of type
26763 `S'. */
26764 if (constructor_p
26765 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26766 constructor_p = false;
26768 if (constructor_p
26769 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26770 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26771 /* A parameter declaration begins with a decl-specifier,
26772 which is either the "attribute" keyword, a storage class
26773 specifier, or (usually) a type-specifier. */
26774 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26776 tree type;
26777 tree pushed_scope = NULL_TREE;
26778 unsigned saved_num_template_parameter_lists;
26780 /* Names appearing in the type-specifier should be looked up
26781 in the scope of the class. */
26782 if (current_class_type)
26783 type = NULL_TREE;
26784 else if (type_decl)
26786 type = TREE_TYPE (type_decl);
26787 if (TREE_CODE (type) == TYPENAME_TYPE)
26789 type = resolve_typename_type (type,
26790 /*only_current_p=*/false);
26791 if (TREE_CODE (type) == TYPENAME_TYPE)
26793 cp_parser_abort_tentative_parse (parser);
26794 return false;
26797 pushed_scope = push_scope (type);
26800 /* Inside the constructor parameter list, surrounding
26801 template-parameter-lists do not apply. */
26802 saved_num_template_parameter_lists
26803 = parser->num_template_parameter_lists;
26804 parser->num_template_parameter_lists = 0;
26806 /* Look for the type-specifier. */
26807 cp_parser_type_specifier (parser,
26808 CP_PARSER_FLAGS_NONE,
26809 /*decl_specs=*/NULL,
26810 /*is_declarator=*/true,
26811 /*declares_class_or_enum=*/NULL,
26812 /*is_cv_qualifier=*/NULL);
26814 parser->num_template_parameter_lists
26815 = saved_num_template_parameter_lists;
26817 /* Leave the scope of the class. */
26818 if (pushed_scope)
26819 pop_scope (pushed_scope);
26821 constructor_p = !cp_parser_error_occurred (parser);
26825 /* We did not really want to consume any tokens. */
26826 cp_parser_abort_tentative_parse (parser);
26828 return constructor_p;
26831 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26832 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26833 they must be performed once we are in the scope of the function.
26835 Returns the function defined. */
26837 static tree
26838 cp_parser_function_definition_from_specifiers_and_declarator
26839 (cp_parser* parser,
26840 cp_decl_specifier_seq *decl_specifiers,
26841 tree attributes,
26842 const cp_declarator *declarator)
26844 tree fn;
26845 bool success_p;
26847 /* Begin the function-definition. */
26848 success_p = start_function (decl_specifiers, declarator, attributes);
26850 /* The things we're about to see are not directly qualified by any
26851 template headers we've seen thus far. */
26852 reset_specialization ();
26854 /* If there were names looked up in the decl-specifier-seq that we
26855 did not check, check them now. We must wait until we are in the
26856 scope of the function to perform the checks, since the function
26857 might be a friend. */
26858 perform_deferred_access_checks (tf_warning_or_error);
26860 if (success_p)
26862 cp_finalize_omp_declare_simd (parser, current_function_decl);
26863 parser->omp_declare_simd = NULL;
26864 cp_finalize_oacc_routine (parser, current_function_decl, true);
26865 parser->oacc_routine = NULL;
26868 if (!success_p)
26870 /* Skip the entire function. */
26871 cp_parser_skip_to_end_of_block_or_statement (parser);
26872 fn = error_mark_node;
26874 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26876 /* Seen already, skip it. An error message has already been output. */
26877 cp_parser_skip_to_end_of_block_or_statement (parser);
26878 fn = current_function_decl;
26879 current_function_decl = NULL_TREE;
26880 /* If this is a function from a class, pop the nested class. */
26881 if (current_class_name)
26882 pop_nested_class ();
26884 else
26886 timevar_id_t tv;
26887 if (DECL_DECLARED_INLINE_P (current_function_decl))
26888 tv = TV_PARSE_INLINE;
26889 else
26890 tv = TV_PARSE_FUNC;
26891 timevar_push (tv);
26892 fn = cp_parser_function_definition_after_declarator (parser,
26893 /*inline_p=*/false);
26894 timevar_pop (tv);
26897 return fn;
26900 /* Parse the part of a function-definition that follows the
26901 declarator. INLINE_P is TRUE iff this function is an inline
26902 function defined within a class-specifier.
26904 Returns the function defined. */
26906 static tree
26907 cp_parser_function_definition_after_declarator (cp_parser* parser,
26908 bool inline_p)
26910 tree fn;
26911 bool saved_in_unbraced_linkage_specification_p;
26912 bool saved_in_function_body;
26913 unsigned saved_num_template_parameter_lists;
26914 cp_token *token;
26915 bool fully_implicit_function_template_p
26916 = parser->fully_implicit_function_template_p;
26917 parser->fully_implicit_function_template_p = false;
26918 tree implicit_template_parms
26919 = parser->implicit_template_parms;
26920 parser->implicit_template_parms = 0;
26921 cp_binding_level* implicit_template_scope
26922 = parser->implicit_template_scope;
26923 parser->implicit_template_scope = 0;
26925 saved_in_function_body = parser->in_function_body;
26926 parser->in_function_body = true;
26927 /* If the next token is `return', then the code may be trying to
26928 make use of the "named return value" extension that G++ used to
26929 support. */
26930 token = cp_lexer_peek_token (parser->lexer);
26931 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26933 /* Consume the `return' keyword. */
26934 cp_lexer_consume_token (parser->lexer);
26935 /* Look for the identifier that indicates what value is to be
26936 returned. */
26937 cp_parser_identifier (parser);
26938 /* Issue an error message. */
26939 error_at (token->location,
26940 "named return values are no longer supported");
26941 /* Skip tokens until we reach the start of the function body. */
26942 while (true)
26944 cp_token *token = cp_lexer_peek_token (parser->lexer);
26945 if (token->type == CPP_OPEN_BRACE
26946 || token->type == CPP_EOF
26947 || token->type == CPP_PRAGMA_EOL)
26948 break;
26949 cp_lexer_consume_token (parser->lexer);
26952 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26953 anything declared inside `f'. */
26954 saved_in_unbraced_linkage_specification_p
26955 = parser->in_unbraced_linkage_specification_p;
26956 parser->in_unbraced_linkage_specification_p = false;
26957 /* Inside the function, surrounding template-parameter-lists do not
26958 apply. */
26959 saved_num_template_parameter_lists
26960 = parser->num_template_parameter_lists;
26961 parser->num_template_parameter_lists = 0;
26963 /* If the next token is `try', `__transaction_atomic', or
26964 `__transaction_relaxed`, then we are looking at either function-try-block
26965 or function-transaction-block. Note that all of these include the
26966 function-body. */
26967 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26968 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26969 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26970 RID_TRANSACTION_RELAXED))
26971 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26972 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26973 cp_parser_function_try_block (parser);
26974 else
26975 cp_parser_ctor_initializer_opt_and_function_body
26976 (parser, /*in_function_try_block=*/false);
26978 /* Finish the function. */
26979 fn = finish_function (inline_p);
26980 /* Generate code for it, if necessary. */
26981 expand_or_defer_fn (fn);
26982 /* Restore the saved values. */
26983 parser->in_unbraced_linkage_specification_p
26984 = saved_in_unbraced_linkage_specification_p;
26985 parser->num_template_parameter_lists
26986 = saved_num_template_parameter_lists;
26987 parser->in_function_body = saved_in_function_body;
26989 parser->fully_implicit_function_template_p
26990 = fully_implicit_function_template_p;
26991 parser->implicit_template_parms
26992 = implicit_template_parms;
26993 parser->implicit_template_scope
26994 = implicit_template_scope;
26996 if (parser->fully_implicit_function_template_p)
26997 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26999 return fn;
27002 /* Parse a template-declaration body (following argument list). */
27004 static void
27005 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27006 tree parameter_list,
27007 bool member_p)
27009 tree decl = NULL_TREE;
27010 bool friend_p = false;
27012 /* We just processed one more parameter list. */
27013 ++parser->num_template_parameter_lists;
27015 /* Get the deferred access checks from the parameter list. These
27016 will be checked once we know what is being declared, as for a
27017 member template the checks must be performed in the scope of the
27018 class containing the member. */
27019 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27021 /* Tentatively parse for a new template parameter list, which can either be
27022 the template keyword or a template introduction. */
27023 if (cp_parser_template_declaration_after_export (parser, member_p))
27024 /* OK */;
27025 else if (cxx_dialect >= cxx11
27026 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27027 decl = cp_parser_alias_declaration (parser);
27028 else
27030 /* There are no access checks when parsing a template, as we do not
27031 know if a specialization will be a friend. */
27032 push_deferring_access_checks (dk_no_check);
27033 cp_token *token = cp_lexer_peek_token (parser->lexer);
27034 decl = cp_parser_single_declaration (parser,
27035 checks,
27036 member_p,
27037 /*explicit_specialization_p=*/false,
27038 &friend_p);
27039 pop_deferring_access_checks ();
27041 /* If this is a member template declaration, let the front
27042 end know. */
27043 if (member_p && !friend_p && decl)
27045 if (TREE_CODE (decl) == TYPE_DECL)
27046 cp_parser_check_access_in_redeclaration (decl, token->location);
27048 decl = finish_member_template_decl (decl);
27050 else if (friend_p && decl
27051 && DECL_DECLARES_TYPE_P (decl))
27052 make_friend_class (current_class_type, TREE_TYPE (decl),
27053 /*complain=*/true);
27055 /* We are done with the current parameter list. */
27056 --parser->num_template_parameter_lists;
27058 pop_deferring_access_checks ();
27060 /* Finish up. */
27061 finish_template_decl (parameter_list);
27063 /* Check the template arguments for a literal operator template. */
27064 if (decl
27065 && DECL_DECLARES_FUNCTION_P (decl)
27066 && UDLIT_OPER_P (DECL_NAME (decl)))
27068 bool ok = true;
27069 if (parameter_list == NULL_TREE)
27070 ok = false;
27071 else
27073 int num_parms = TREE_VEC_LENGTH (parameter_list);
27074 if (num_parms == 1)
27076 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27077 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27078 if (TREE_TYPE (parm) != char_type_node
27079 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27080 ok = false;
27082 else if (num_parms == 2 && cxx_dialect >= cxx14)
27084 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27085 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27086 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27087 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27088 if (parm == error_mark_node
27089 || TREE_TYPE (parm) != TREE_TYPE (type)
27090 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27091 ok = false;
27093 else
27094 ok = false;
27096 if (!ok)
27098 if (cxx_dialect >= cxx14)
27099 error ("literal operator template %qD has invalid parameter list."
27100 " Expected non-type template argument pack <char...>"
27101 " or <typename CharT, CharT...>",
27102 decl);
27103 else
27104 error ("literal operator template %qD has invalid parameter list."
27105 " Expected non-type template argument pack <char...>",
27106 decl);
27110 /* Register member declarations. */
27111 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27112 finish_member_declaration (decl);
27113 /* If DECL is a function template, we must return to parse it later.
27114 (Even though there is no definition, there might be default
27115 arguments that need handling.) */
27116 if (member_p && decl
27117 && DECL_DECLARES_FUNCTION_P (decl))
27118 vec_safe_push (unparsed_funs_with_definitions, decl);
27121 /* Parse a template introduction header for a template-declaration. Returns
27122 false if tentative parse fails. */
27124 static bool
27125 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27127 cp_parser_parse_tentatively (parser);
27129 tree saved_scope = parser->scope;
27130 tree saved_object_scope = parser->object_scope;
27131 tree saved_qualifying_scope = parser->qualifying_scope;
27133 /* Look for the optional `::' operator. */
27134 cp_parser_global_scope_opt (parser,
27135 /*current_scope_valid_p=*/false);
27136 /* Look for the nested-name-specifier. */
27137 cp_parser_nested_name_specifier_opt (parser,
27138 /*typename_keyword_p=*/false,
27139 /*check_dependency_p=*/true,
27140 /*type_p=*/false,
27141 /*is_declaration=*/false);
27143 cp_token *token = cp_lexer_peek_token (parser->lexer);
27144 tree concept_name = cp_parser_identifier (parser);
27146 /* Look up the concept for which we will be matching
27147 template parameters. */
27148 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27149 token->location);
27150 parser->scope = saved_scope;
27151 parser->object_scope = saved_object_scope;
27152 parser->qualifying_scope = saved_qualifying_scope;
27154 if (concept_name == error_mark_node)
27155 cp_parser_simulate_error (parser);
27157 /* Look for opening brace for introduction. */
27158 matching_braces braces;
27159 braces.require_open (parser);
27161 if (!cp_parser_parse_definitely (parser))
27162 return false;
27164 push_deferring_access_checks (dk_deferred);
27166 /* Build vector of placeholder parameters and grab
27167 matching identifiers. */
27168 tree introduction_list = cp_parser_introduction_list (parser);
27170 /* Look for closing brace for introduction. */
27171 if (!braces.require_close (parser))
27172 return true;
27174 /* The introduction-list shall not be empty. */
27175 int nargs = TREE_VEC_LENGTH (introduction_list);
27176 if (nargs == 0)
27178 /* In cp_parser_introduction_list we have already issued an error. */
27179 return true;
27182 if (tmpl_decl == error_mark_node)
27184 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27185 token->location);
27186 return true;
27189 /* Build and associate the constraint. */
27190 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27191 if (parms && parms != error_mark_node)
27193 cp_parser_template_declaration_after_parameters (parser, parms,
27194 member_p);
27195 return true;
27198 error_at (token->location, "no matching concept for template-introduction");
27199 return true;
27202 /* Parse a normal template-declaration following the template keyword. */
27204 static void
27205 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27207 tree parameter_list;
27208 bool need_lang_pop;
27209 location_t location = input_location;
27211 /* Look for the `<' token. */
27212 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27213 return;
27214 if (at_class_scope_p () && current_function_decl)
27216 /* 14.5.2.2 [temp.mem]
27218 A local class shall not have member templates. */
27219 error_at (location,
27220 "invalid declaration of member template in local class");
27221 cp_parser_skip_to_end_of_block_or_statement (parser);
27222 return;
27224 /* [temp]
27226 A template ... shall not have C linkage. */
27227 if (current_lang_name == lang_name_c)
27229 error_at (location, "template with C linkage");
27230 maybe_show_extern_c_location ();
27231 /* Give it C++ linkage to avoid confusing other parts of the
27232 front end. */
27233 push_lang_context (lang_name_cplusplus);
27234 need_lang_pop = true;
27236 else
27237 need_lang_pop = false;
27239 /* We cannot perform access checks on the template parameter
27240 declarations until we know what is being declared, just as we
27241 cannot check the decl-specifier list. */
27242 push_deferring_access_checks (dk_deferred);
27244 /* If the next token is `>', then we have an invalid
27245 specialization. Rather than complain about an invalid template
27246 parameter, issue an error message here. */
27247 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27249 cp_parser_error (parser, "invalid explicit specialization");
27250 begin_specialization ();
27251 parameter_list = NULL_TREE;
27253 else
27255 /* Parse the template parameters. */
27256 parameter_list = cp_parser_template_parameter_list (parser);
27259 /* Look for the `>'. */
27260 cp_parser_skip_to_end_of_template_parameter_list (parser);
27262 /* Manage template requirements */
27263 if (flag_concepts)
27265 tree reqs = get_shorthand_constraints (current_template_parms);
27266 if (tree r = cp_parser_requires_clause_opt (parser))
27267 reqs = conjoin_constraints (reqs, normalize_expression (r));
27268 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27271 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27272 member_p);
27274 /* For the erroneous case of a template with C linkage, we pushed an
27275 implicit C++ linkage scope; exit that scope now. */
27276 if (need_lang_pop)
27277 pop_lang_context ();
27280 /* Parse a template-declaration, assuming that the `export' (and
27281 `extern') keywords, if present, has already been scanned. MEMBER_P
27282 is as for cp_parser_template_declaration. */
27284 static bool
27285 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27287 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27289 cp_lexer_consume_token (parser->lexer);
27290 cp_parser_explicit_template_declaration (parser, member_p);
27291 return true;
27293 else if (flag_concepts)
27294 return cp_parser_template_introduction (parser, member_p);
27296 return false;
27299 /* Perform the deferred access checks from a template-parameter-list.
27300 CHECKS is a TREE_LIST of access checks, as returned by
27301 get_deferred_access_checks. */
27303 static void
27304 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27306 ++processing_template_parmlist;
27307 perform_access_checks (checks, tf_warning_or_error);
27308 --processing_template_parmlist;
27311 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27312 `function-definition' sequence that follows a template header.
27313 If MEMBER_P is true, this declaration appears in a class scope.
27315 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27316 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27318 static tree
27319 cp_parser_single_declaration (cp_parser* parser,
27320 vec<deferred_access_check, va_gc> *checks,
27321 bool member_p,
27322 bool explicit_specialization_p,
27323 bool* friend_p)
27325 int declares_class_or_enum;
27326 tree decl = NULL_TREE;
27327 cp_decl_specifier_seq decl_specifiers;
27328 bool function_definition_p = false;
27329 cp_token *decl_spec_token_start;
27331 /* This function is only used when processing a template
27332 declaration. */
27333 gcc_assert (innermost_scope_kind () == sk_template_parms
27334 || innermost_scope_kind () == sk_template_spec);
27336 /* Defer access checks until we know what is being declared. */
27337 push_deferring_access_checks (dk_deferred);
27339 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27340 alternative. */
27341 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27342 cp_parser_decl_specifier_seq (parser,
27343 CP_PARSER_FLAGS_OPTIONAL,
27344 &decl_specifiers,
27345 &declares_class_or_enum);
27346 if (friend_p)
27347 *friend_p = cp_parser_friend_p (&decl_specifiers);
27349 /* There are no template typedefs. */
27350 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27352 error_at (decl_spec_token_start->location,
27353 "template declaration of %<typedef%>");
27354 decl = error_mark_node;
27357 /* Gather up the access checks that occurred the
27358 decl-specifier-seq. */
27359 stop_deferring_access_checks ();
27361 /* Check for the declaration of a template class. */
27362 if (declares_class_or_enum)
27364 if (cp_parser_declares_only_class_p (parser)
27365 || (declares_class_or_enum & 2))
27367 // If this is a declaration, but not a definition, associate
27368 // any constraints with the type declaration. Constraints
27369 // are associated with definitions in cp_parser_class_specifier.
27370 if (declares_class_or_enum == 1)
27371 associate_classtype_constraints (decl_specifiers.type);
27373 decl = shadow_tag (&decl_specifiers);
27375 /* In this case:
27377 struct C {
27378 friend template <typename T> struct A<T>::B;
27381 A<T>::B will be represented by a TYPENAME_TYPE, and
27382 therefore not recognized by shadow_tag. */
27383 if (friend_p && *friend_p
27384 && !decl
27385 && decl_specifiers.type
27386 && TYPE_P (decl_specifiers.type))
27387 decl = decl_specifiers.type;
27389 if (decl && decl != error_mark_node)
27390 decl = TYPE_NAME (decl);
27391 else
27392 decl = error_mark_node;
27394 /* Perform access checks for template parameters. */
27395 cp_parser_perform_template_parameter_access_checks (checks);
27397 /* Give a helpful diagnostic for
27398 template <class T> struct A { } a;
27399 if we aren't already recovering from an error. */
27400 if (!cp_parser_declares_only_class_p (parser)
27401 && !seen_error ())
27403 error_at (cp_lexer_peek_token (parser->lexer)->location,
27404 "a class template declaration must not declare "
27405 "anything else");
27406 cp_parser_skip_to_end_of_block_or_statement (parser);
27407 goto out;
27412 /* Complain about missing 'typename' or other invalid type names. */
27413 if (!decl_specifiers.any_type_specifiers_p
27414 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27416 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27417 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27418 the rest of this declaration. */
27419 decl = error_mark_node;
27420 goto out;
27423 /* If it's not a template class, try for a template function. If
27424 the next token is a `;', then this declaration does not declare
27425 anything. But, if there were errors in the decl-specifiers, then
27426 the error might well have come from an attempted class-specifier.
27427 In that case, there's no need to warn about a missing declarator. */
27428 if (!decl
27429 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27430 || decl_specifiers.type != error_mark_node))
27432 decl = cp_parser_init_declarator (parser,
27433 &decl_specifiers,
27434 checks,
27435 /*function_definition_allowed_p=*/true,
27436 member_p,
27437 declares_class_or_enum,
27438 &function_definition_p,
27439 NULL, NULL, NULL);
27441 /* 7.1.1-1 [dcl.stc]
27443 A storage-class-specifier shall not be specified in an explicit
27444 specialization... */
27445 if (decl
27446 && explicit_specialization_p
27447 && decl_specifiers.storage_class != sc_none)
27449 error_at (decl_spec_token_start->location,
27450 "explicit template specialization cannot have a storage class");
27451 decl = error_mark_node;
27454 if (decl && VAR_P (decl))
27455 check_template_variable (decl);
27458 /* Look for a trailing `;' after the declaration. */
27459 if (!function_definition_p
27460 && (decl == error_mark_node
27461 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27462 cp_parser_skip_to_end_of_block_or_statement (parser);
27464 out:
27465 pop_deferring_access_checks ();
27467 /* Clear any current qualification; whatever comes next is the start
27468 of something new. */
27469 parser->scope = NULL_TREE;
27470 parser->qualifying_scope = NULL_TREE;
27471 parser->object_scope = NULL_TREE;
27473 return decl;
27476 /* Parse a cast-expression that is not the operand of a unary "&". */
27478 static cp_expr
27479 cp_parser_simple_cast_expression (cp_parser *parser)
27481 return cp_parser_cast_expression (parser, /*address_p=*/false,
27482 /*cast_p=*/false, /*decltype*/false, NULL);
27485 /* Parse a functional cast to TYPE. Returns an expression
27486 representing the cast. */
27488 static cp_expr
27489 cp_parser_functional_cast (cp_parser* parser, tree type)
27491 vec<tree, va_gc> *vec;
27492 tree expression_list;
27493 cp_expr cast;
27494 bool nonconst_p;
27496 location_t start_loc = input_location;
27498 if (!type)
27499 type = error_mark_node;
27501 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27503 cp_lexer_set_source_position (parser->lexer);
27504 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27505 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27506 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27507 if (TREE_CODE (type) == TYPE_DECL)
27508 type = TREE_TYPE (type);
27510 cast = finish_compound_literal (type, expression_list,
27511 tf_warning_or_error, fcl_functional);
27512 /* Create a location of the form:
27513 type_name{i, f}
27514 ^~~~~~~~~~~~~~~
27515 with caret == start at the start of the type name,
27516 finishing at the closing brace. */
27517 location_t finish_loc
27518 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27519 location_t combined_loc = make_location (start_loc, start_loc,
27520 finish_loc);
27521 cast.set_location (combined_loc);
27522 return cast;
27526 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27527 /*cast_p=*/true,
27528 /*allow_expansion_p=*/true,
27529 /*non_constant_p=*/NULL);
27530 if (vec == NULL)
27531 expression_list = error_mark_node;
27532 else
27534 expression_list = build_tree_list_vec (vec);
27535 release_tree_vector (vec);
27538 cast = build_functional_cast (type, expression_list,
27539 tf_warning_or_error);
27540 /* [expr.const]/1: In an integral constant expression "only type
27541 conversions to integral or enumeration type can be used". */
27542 if (TREE_CODE (type) == TYPE_DECL)
27543 type = TREE_TYPE (type);
27544 if (cast != error_mark_node
27545 && !cast_valid_in_integral_constant_expression_p (type)
27546 && cp_parser_non_integral_constant_expression (parser,
27547 NIC_CONSTRUCTOR))
27548 return error_mark_node;
27550 /* Create a location of the form:
27551 float(i)
27552 ^~~~~~~~
27553 with caret == start at the start of the type name,
27554 finishing at the closing paren. */
27555 location_t finish_loc
27556 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27557 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27558 cast.set_location (combined_loc);
27559 return cast;
27562 /* Save the tokens that make up the body of a member function defined
27563 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27564 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27565 specifiers applied to the declaration. Returns the FUNCTION_DECL
27566 for the member function. */
27568 static tree
27569 cp_parser_save_member_function_body (cp_parser* parser,
27570 cp_decl_specifier_seq *decl_specifiers,
27571 cp_declarator *declarator,
27572 tree attributes)
27574 cp_token *first;
27575 cp_token *last;
27576 tree fn;
27577 bool function_try_block = false;
27579 /* Create the FUNCTION_DECL. */
27580 fn = grokmethod (decl_specifiers, declarator, attributes);
27581 cp_finalize_omp_declare_simd (parser, fn);
27582 cp_finalize_oacc_routine (parser, fn, true);
27583 /* If something went badly wrong, bail out now. */
27584 if (fn == error_mark_node)
27586 /* If there's a function-body, skip it. */
27587 if (cp_parser_token_starts_function_definition_p
27588 (cp_lexer_peek_token (parser->lexer)))
27589 cp_parser_skip_to_end_of_block_or_statement (parser);
27590 return error_mark_node;
27593 /* Remember it, if there default args to post process. */
27594 cp_parser_save_default_args (parser, fn);
27596 /* Save away the tokens that make up the body of the
27597 function. */
27598 first = parser->lexer->next_token;
27600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27601 cp_lexer_consume_token (parser->lexer);
27602 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27603 RID_TRANSACTION_ATOMIC))
27605 cp_lexer_consume_token (parser->lexer);
27606 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27607 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27608 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27609 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27610 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27611 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27612 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27614 cp_lexer_consume_token (parser->lexer);
27615 cp_lexer_consume_token (parser->lexer);
27616 cp_lexer_consume_token (parser->lexer);
27617 cp_lexer_consume_token (parser->lexer);
27618 cp_lexer_consume_token (parser->lexer);
27620 else
27621 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27622 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27624 cp_lexer_consume_token (parser->lexer);
27625 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27626 break;
27630 /* Handle function try blocks. */
27631 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27633 cp_lexer_consume_token (parser->lexer);
27634 function_try_block = true;
27636 /* We can have braced-init-list mem-initializers before the fn body. */
27637 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27639 cp_lexer_consume_token (parser->lexer);
27640 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27642 /* cache_group will stop after an un-nested { } pair, too. */
27643 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27644 break;
27646 /* variadic mem-inits have ... after the ')'. */
27647 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27648 cp_lexer_consume_token (parser->lexer);
27651 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27652 /* Handle function try blocks. */
27653 if (function_try_block)
27654 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27655 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27656 last = parser->lexer->next_token;
27658 /* Save away the inline definition; we will process it when the
27659 class is complete. */
27660 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27661 DECL_PENDING_INLINE_P (fn) = 1;
27663 /* We need to know that this was defined in the class, so that
27664 friend templates are handled correctly. */
27665 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27667 /* Add FN to the queue of functions to be parsed later. */
27668 vec_safe_push (unparsed_funs_with_definitions, fn);
27670 return fn;
27673 /* Save the tokens that make up the in-class initializer for a non-static
27674 data member. Returns a DEFAULT_ARG. */
27676 static tree
27677 cp_parser_save_nsdmi (cp_parser* parser)
27679 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27682 /* Parse a template-argument-list, as well as the trailing ">" (but
27683 not the opening "<"). See cp_parser_template_argument_list for the
27684 return value. */
27686 static tree
27687 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27689 tree arguments;
27690 tree saved_scope;
27691 tree saved_qualifying_scope;
27692 tree saved_object_scope;
27693 bool saved_greater_than_is_operator_p;
27694 int saved_unevaluated_operand;
27695 int saved_inhibit_evaluation_warnings;
27697 /* [temp.names]
27699 When parsing a template-id, the first non-nested `>' is taken as
27700 the end of the template-argument-list rather than a greater-than
27701 operator. */
27702 saved_greater_than_is_operator_p
27703 = parser->greater_than_is_operator_p;
27704 parser->greater_than_is_operator_p = false;
27705 /* Parsing the argument list may modify SCOPE, so we save it
27706 here. */
27707 saved_scope = parser->scope;
27708 saved_qualifying_scope = parser->qualifying_scope;
27709 saved_object_scope = parser->object_scope;
27710 /* We need to evaluate the template arguments, even though this
27711 template-id may be nested within a "sizeof". */
27712 saved_unevaluated_operand = cp_unevaluated_operand;
27713 cp_unevaluated_operand = 0;
27714 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27715 c_inhibit_evaluation_warnings = 0;
27716 /* Parse the template-argument-list itself. */
27717 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27718 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27719 arguments = NULL_TREE;
27720 else
27721 arguments = cp_parser_template_argument_list (parser);
27722 /* Look for the `>' that ends the template-argument-list. If we find
27723 a '>>' instead, it's probably just a typo. */
27724 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27726 if (cxx_dialect != cxx98)
27728 /* In C++0x, a `>>' in a template argument list or cast
27729 expression is considered to be two separate `>'
27730 tokens. So, change the current token to a `>', but don't
27731 consume it: it will be consumed later when the outer
27732 template argument list (or cast expression) is parsed.
27733 Note that this replacement of `>' for `>>' is necessary
27734 even if we are parsing tentatively: in the tentative
27735 case, after calling
27736 cp_parser_enclosed_template_argument_list we will always
27737 throw away all of the template arguments and the first
27738 closing `>', either because the template argument list
27739 was erroneous or because we are replacing those tokens
27740 with a CPP_TEMPLATE_ID token. The second `>' (which will
27741 not have been thrown away) is needed either to close an
27742 outer template argument list or to complete a new-style
27743 cast. */
27744 cp_token *token = cp_lexer_peek_token (parser->lexer);
27745 token->type = CPP_GREATER;
27747 else if (!saved_greater_than_is_operator_p)
27749 /* If we're in a nested template argument list, the '>>' has
27750 to be a typo for '> >'. We emit the error message, but we
27751 continue parsing and we push a '>' as next token, so that
27752 the argument list will be parsed correctly. Note that the
27753 global source location is still on the token before the
27754 '>>', so we need to say explicitly where we want it. */
27755 cp_token *token = cp_lexer_peek_token (parser->lexer);
27756 gcc_rich_location richloc (token->location);
27757 richloc.add_fixit_replace ("> >");
27758 error_at (&richloc, "%<>>%> should be %<> >%> "
27759 "within a nested template argument list");
27761 token->type = CPP_GREATER;
27763 else
27765 /* If this is not a nested template argument list, the '>>'
27766 is a typo for '>'. Emit an error message and continue.
27767 Same deal about the token location, but here we can get it
27768 right by consuming the '>>' before issuing the diagnostic. */
27769 cp_token *token = cp_lexer_consume_token (parser->lexer);
27770 error_at (token->location,
27771 "spurious %<>>%>, use %<>%> to terminate "
27772 "a template argument list");
27775 else
27776 cp_parser_skip_to_end_of_template_parameter_list (parser);
27777 /* The `>' token might be a greater-than operator again now. */
27778 parser->greater_than_is_operator_p
27779 = saved_greater_than_is_operator_p;
27780 /* Restore the SAVED_SCOPE. */
27781 parser->scope = saved_scope;
27782 parser->qualifying_scope = saved_qualifying_scope;
27783 parser->object_scope = saved_object_scope;
27784 cp_unevaluated_operand = saved_unevaluated_operand;
27785 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27787 return arguments;
27790 /* MEMBER_FUNCTION is a member function, or a friend. If default
27791 arguments, or the body of the function have not yet been parsed,
27792 parse them now. */
27794 static void
27795 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27797 timevar_push (TV_PARSE_INMETH);
27798 /* If this member is a template, get the underlying
27799 FUNCTION_DECL. */
27800 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27801 member_function = DECL_TEMPLATE_RESULT (member_function);
27803 /* There should not be any class definitions in progress at this
27804 point; the bodies of members are only parsed outside of all class
27805 definitions. */
27806 gcc_assert (parser->num_classes_being_defined == 0);
27807 /* While we're parsing the member functions we might encounter more
27808 classes. We want to handle them right away, but we don't want
27809 them getting mixed up with functions that are currently in the
27810 queue. */
27811 push_unparsed_function_queues (parser);
27813 /* Make sure that any template parameters are in scope. */
27814 maybe_begin_member_template_processing (member_function);
27816 /* If the body of the function has not yet been parsed, parse it
27817 now. */
27818 if (DECL_PENDING_INLINE_P (member_function))
27820 tree function_scope;
27821 cp_token_cache *tokens;
27823 /* The function is no longer pending; we are processing it. */
27824 tokens = DECL_PENDING_INLINE_INFO (member_function);
27825 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27826 DECL_PENDING_INLINE_P (member_function) = 0;
27828 /* If this is a local class, enter the scope of the containing
27829 function. */
27830 function_scope = current_function_decl;
27831 if (function_scope)
27832 push_function_context ();
27834 /* Push the body of the function onto the lexer stack. */
27835 cp_parser_push_lexer_for_tokens (parser, tokens);
27837 /* Let the front end know that we going to be defining this
27838 function. */
27839 start_preparsed_function (member_function, NULL_TREE,
27840 SF_PRE_PARSED | SF_INCLASS_INLINE);
27842 /* Don't do access checking if it is a templated function. */
27843 if (processing_template_decl)
27844 push_deferring_access_checks (dk_no_check);
27846 /* #pragma omp declare reduction needs special parsing. */
27847 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27849 parser->lexer->in_pragma = true;
27850 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27851 finish_function (/*inline_p=*/true);
27852 cp_check_omp_declare_reduction (member_function);
27854 else
27855 /* Now, parse the body of the function. */
27856 cp_parser_function_definition_after_declarator (parser,
27857 /*inline_p=*/true);
27859 if (processing_template_decl)
27860 pop_deferring_access_checks ();
27862 /* Leave the scope of the containing function. */
27863 if (function_scope)
27864 pop_function_context ();
27865 cp_parser_pop_lexer (parser);
27868 /* Remove any template parameters from the symbol table. */
27869 maybe_end_member_template_processing ();
27871 /* Restore the queue. */
27872 pop_unparsed_function_queues (parser);
27873 timevar_pop (TV_PARSE_INMETH);
27876 /* If DECL contains any default args, remember it on the unparsed
27877 functions queue. */
27879 static void
27880 cp_parser_save_default_args (cp_parser* parser, tree decl)
27882 tree probe;
27884 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27885 probe;
27886 probe = TREE_CHAIN (probe))
27887 if (TREE_PURPOSE (probe))
27889 cp_default_arg_entry entry = {current_class_type, decl};
27890 vec_safe_push (unparsed_funs_with_default_args, entry);
27891 break;
27895 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27896 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27897 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27898 from the parameter-type-list. */
27900 static tree
27901 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27902 tree default_arg, tree parmtype)
27904 cp_token_cache *tokens;
27905 tree parsed_arg;
27906 bool dummy;
27908 if (default_arg == error_mark_node)
27909 return error_mark_node;
27911 /* Push the saved tokens for the default argument onto the parser's
27912 lexer stack. */
27913 tokens = DEFARG_TOKENS (default_arg);
27914 cp_parser_push_lexer_for_tokens (parser, tokens);
27916 start_lambda_scope (decl);
27918 /* Parse the default argument. */
27919 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27920 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27921 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27923 finish_lambda_scope ();
27925 if (parsed_arg == error_mark_node)
27926 cp_parser_skip_to_end_of_statement (parser);
27928 if (!processing_template_decl)
27930 /* In a non-template class, check conversions now. In a template,
27931 we'll wait and instantiate these as needed. */
27932 if (TREE_CODE (decl) == PARM_DECL)
27933 parsed_arg = check_default_argument (parmtype, parsed_arg,
27934 tf_warning_or_error);
27935 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27936 parsed_arg = error_mark_node;
27937 else
27938 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27941 /* If the token stream has not been completely used up, then
27942 there was extra junk after the end of the default
27943 argument. */
27944 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27946 if (TREE_CODE (decl) == PARM_DECL)
27947 cp_parser_error (parser, "expected %<,%>");
27948 else
27949 cp_parser_error (parser, "expected %<;%>");
27952 /* Revert to the main lexer. */
27953 cp_parser_pop_lexer (parser);
27955 return parsed_arg;
27958 /* FIELD is a non-static data member with an initializer which we saved for
27959 later; parse it now. */
27961 static void
27962 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27964 tree def;
27966 maybe_begin_member_template_processing (field);
27968 push_unparsed_function_queues (parser);
27969 def = cp_parser_late_parse_one_default_arg (parser, field,
27970 DECL_INITIAL (field),
27971 NULL_TREE);
27972 pop_unparsed_function_queues (parser);
27974 maybe_end_member_template_processing ();
27976 DECL_INITIAL (field) = def;
27979 /* FN is a FUNCTION_DECL which may contains a parameter with an
27980 unparsed DEFAULT_ARG. Parse the default args now. This function
27981 assumes that the current scope is the scope in which the default
27982 argument should be processed. */
27984 static void
27985 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27987 bool saved_local_variables_forbidden_p;
27988 tree parm, parmdecl;
27990 /* While we're parsing the default args, we might (due to the
27991 statement expression extension) encounter more classes. We want
27992 to handle them right away, but we don't want them getting mixed
27993 up with default args that are currently in the queue. */
27994 push_unparsed_function_queues (parser);
27996 /* Local variable names (and the `this' keyword) may not appear
27997 in a default argument. */
27998 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27999 parser->local_variables_forbidden_p = true;
28001 push_defarg_context (fn);
28003 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28004 parmdecl = DECL_ARGUMENTS (fn);
28005 parm && parm != void_list_node;
28006 parm = TREE_CHAIN (parm),
28007 parmdecl = DECL_CHAIN (parmdecl))
28009 tree default_arg = TREE_PURPOSE (parm);
28010 tree parsed_arg;
28011 vec<tree, va_gc> *insts;
28012 tree copy;
28013 unsigned ix;
28015 if (!default_arg)
28016 continue;
28018 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28019 /* This can happen for a friend declaration for a function
28020 already declared with default arguments. */
28021 continue;
28023 parsed_arg
28024 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28025 default_arg,
28026 TREE_VALUE (parm));
28027 TREE_PURPOSE (parm) = parsed_arg;
28029 /* Update any instantiations we've already created. */
28030 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28031 vec_safe_iterate (insts, ix, &copy); ix++)
28032 TREE_PURPOSE (copy) = parsed_arg;
28035 pop_defarg_context ();
28037 /* Make sure no default arg is missing. */
28038 check_default_args (fn);
28040 /* Restore the state of local_variables_forbidden_p. */
28041 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28043 /* Restore the queue. */
28044 pop_unparsed_function_queues (parser);
28047 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28049 sizeof ... ( identifier )
28051 where the 'sizeof' token has already been consumed. */
28053 static tree
28054 cp_parser_sizeof_pack (cp_parser *parser)
28056 /* Consume the `...'. */
28057 cp_lexer_consume_token (parser->lexer);
28058 maybe_warn_variadic_templates ();
28060 matching_parens parens;
28061 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28062 if (paren)
28063 parens.consume_open (parser);
28064 else
28065 permerror (cp_lexer_peek_token (parser->lexer)->location,
28066 "%<sizeof...%> argument must be surrounded by parentheses");
28068 cp_token *token = cp_lexer_peek_token (parser->lexer);
28069 tree name = cp_parser_identifier (parser);
28070 if (name == error_mark_node)
28071 return error_mark_node;
28072 /* The name is not qualified. */
28073 parser->scope = NULL_TREE;
28074 parser->qualifying_scope = NULL_TREE;
28075 parser->object_scope = NULL_TREE;
28076 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28077 if (expr == error_mark_node)
28078 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28079 token->location);
28080 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28081 expr = TREE_TYPE (expr);
28082 else if (TREE_CODE (expr) == CONST_DECL)
28083 expr = DECL_INITIAL (expr);
28084 expr = make_pack_expansion (expr);
28085 PACK_EXPANSION_SIZEOF_P (expr) = true;
28087 if (paren)
28088 parens.require_close (parser);
28090 return expr;
28093 /* Parse the operand of `sizeof' (or a similar operator). Returns
28094 either a TYPE or an expression, depending on the form of the
28095 input. The KEYWORD indicates which kind of expression we have
28096 encountered. */
28098 static tree
28099 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28101 tree expr = NULL_TREE;
28102 const char *saved_message;
28103 char *tmp;
28104 bool saved_integral_constant_expression_p;
28105 bool saved_non_integral_constant_expression_p;
28107 /* If it's a `...', then we are computing the length of a parameter
28108 pack. */
28109 if (keyword == RID_SIZEOF
28110 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28111 return cp_parser_sizeof_pack (parser);
28113 /* Types cannot be defined in a `sizeof' expression. Save away the
28114 old message. */
28115 saved_message = parser->type_definition_forbidden_message;
28116 /* And create the new one. */
28117 tmp = concat ("types may not be defined in %<",
28118 IDENTIFIER_POINTER (ridpointers[keyword]),
28119 "%> expressions", NULL);
28120 parser->type_definition_forbidden_message = tmp;
28122 /* The restrictions on constant-expressions do not apply inside
28123 sizeof expressions. */
28124 saved_integral_constant_expression_p
28125 = parser->integral_constant_expression_p;
28126 saved_non_integral_constant_expression_p
28127 = parser->non_integral_constant_expression_p;
28128 parser->integral_constant_expression_p = false;
28130 /* Do not actually evaluate the expression. */
28131 ++cp_unevaluated_operand;
28132 ++c_inhibit_evaluation_warnings;
28133 /* If it's a `(', then we might be looking at the type-id
28134 construction. */
28135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28137 tree type = NULL_TREE;
28139 /* We can't be sure yet whether we're looking at a type-id or an
28140 expression. */
28141 cp_parser_parse_tentatively (parser);
28143 matching_parens parens;
28144 parens.consume_open (parser);
28146 /* Note: as a GNU Extension, compound literals are considered
28147 postfix-expressions as they are in C99, so they are valid
28148 arguments to sizeof. See comment in cp_parser_cast_expression
28149 for details. */
28150 if (cp_parser_compound_literal_p (parser))
28151 cp_parser_simulate_error (parser);
28152 else
28154 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28155 parser->in_type_id_in_expr_p = true;
28156 /* Look for the type-id. */
28157 type = cp_parser_type_id (parser);
28158 /* Look for the closing `)'. */
28159 parens.require_close (parser);
28160 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28163 /* If all went well, then we're done. */
28164 if (cp_parser_parse_definitely (parser))
28166 cp_decl_specifier_seq decl_specs;
28168 /* Build a trivial decl-specifier-seq. */
28169 clear_decl_specs (&decl_specs);
28170 decl_specs.type = type;
28172 /* Call grokdeclarator to figure out what type this is. */
28173 expr = grokdeclarator (NULL,
28174 &decl_specs,
28175 TYPENAME,
28176 /*initialized=*/0,
28177 /*attrlist=*/NULL);
28181 /* If the type-id production did not work out, then we must be
28182 looking at the unary-expression production. */
28183 if (!expr)
28184 expr = cp_parser_unary_expression (parser);
28186 /* Go back to evaluating expressions. */
28187 --cp_unevaluated_operand;
28188 --c_inhibit_evaluation_warnings;
28190 /* Free the message we created. */
28191 free (tmp);
28192 /* And restore the old one. */
28193 parser->type_definition_forbidden_message = saved_message;
28194 parser->integral_constant_expression_p
28195 = saved_integral_constant_expression_p;
28196 parser->non_integral_constant_expression_p
28197 = saved_non_integral_constant_expression_p;
28199 return expr;
28202 /* If the current declaration has no declarator, return true. */
28204 static bool
28205 cp_parser_declares_only_class_p (cp_parser *parser)
28207 /* If the next token is a `;' or a `,' then there is no
28208 declarator. */
28209 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28210 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28213 /* Update the DECL_SPECS to reflect the storage class indicated by
28214 KEYWORD. */
28216 static void
28217 cp_parser_set_storage_class (cp_parser *parser,
28218 cp_decl_specifier_seq *decl_specs,
28219 enum rid keyword,
28220 cp_token *token)
28222 cp_storage_class storage_class;
28224 if (parser->in_unbraced_linkage_specification_p)
28226 error_at (token->location, "invalid use of %qD in linkage specification",
28227 ridpointers[keyword]);
28228 return;
28230 else if (decl_specs->storage_class != sc_none)
28232 decl_specs->conflicting_specifiers_p = true;
28233 return;
28236 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28237 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28238 && decl_specs->gnu_thread_keyword_p)
28240 pedwarn (decl_specs->locations[ds_thread], 0,
28241 "%<__thread%> before %qD", ridpointers[keyword]);
28244 switch (keyword)
28246 case RID_AUTO:
28247 storage_class = sc_auto;
28248 break;
28249 case RID_REGISTER:
28250 storage_class = sc_register;
28251 break;
28252 case RID_STATIC:
28253 storage_class = sc_static;
28254 break;
28255 case RID_EXTERN:
28256 storage_class = sc_extern;
28257 break;
28258 case RID_MUTABLE:
28259 storage_class = sc_mutable;
28260 break;
28261 default:
28262 gcc_unreachable ();
28264 decl_specs->storage_class = storage_class;
28265 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28267 /* A storage class specifier cannot be applied alongside a typedef
28268 specifier. If there is a typedef specifier present then set
28269 conflicting_specifiers_p which will trigger an error later
28270 on in grokdeclarator. */
28271 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28272 decl_specs->conflicting_specifiers_p = true;
28275 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28276 is true, the type is a class or enum definition. */
28278 static void
28279 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28280 tree type_spec,
28281 cp_token *token,
28282 bool type_definition_p)
28284 decl_specs->any_specifiers_p = true;
28286 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28287 (with, for example, in "typedef int wchar_t;") we remember that
28288 this is what happened. In system headers, we ignore these
28289 declarations so that G++ can work with system headers that are not
28290 C++-safe. */
28291 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28292 && !type_definition_p
28293 && (type_spec == boolean_type_node
28294 || type_spec == char16_type_node
28295 || type_spec == char32_type_node
28296 || type_spec == wchar_type_node)
28297 && (decl_specs->type
28298 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28299 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28300 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28301 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28303 decl_specs->redefined_builtin_type = type_spec;
28304 set_and_check_decl_spec_loc (decl_specs,
28305 ds_redefined_builtin_type_spec,
28306 token);
28307 if (!decl_specs->type)
28309 decl_specs->type = type_spec;
28310 decl_specs->type_definition_p = false;
28311 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28314 else if (decl_specs->type)
28315 decl_specs->multiple_types_p = true;
28316 else
28318 decl_specs->type = type_spec;
28319 decl_specs->type_definition_p = type_definition_p;
28320 decl_specs->redefined_builtin_type = NULL_TREE;
28321 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28325 /* True iff TOKEN is the GNU keyword __thread. */
28327 static bool
28328 token_is__thread (cp_token *token)
28330 gcc_assert (token->keyword == RID_THREAD);
28331 return id_equal (token->u.value, "__thread");
28334 /* Set the location for a declarator specifier and check if it is
28335 duplicated.
28337 DECL_SPECS is the sequence of declarator specifiers onto which to
28338 set the location.
28340 DS is the single declarator specifier to set which location is to
28341 be set onto the existing sequence of declarators.
28343 LOCATION is the location for the declarator specifier to
28344 consider. */
28346 static void
28347 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28348 cp_decl_spec ds, cp_token *token)
28350 gcc_assert (ds < ds_last);
28352 if (decl_specs == NULL)
28353 return;
28355 source_location location = token->location;
28357 if (decl_specs->locations[ds] == 0)
28359 decl_specs->locations[ds] = location;
28360 if (ds == ds_thread)
28361 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28363 else
28365 if (ds == ds_long)
28367 if (decl_specs->locations[ds_long_long] != 0)
28368 error_at (location,
28369 "%<long long long%> is too long for GCC");
28370 else
28372 decl_specs->locations[ds_long_long] = location;
28373 pedwarn_cxx98 (location,
28374 OPT_Wlong_long,
28375 "ISO C++ 1998 does not support %<long long%>");
28378 else if (ds == ds_thread)
28380 bool gnu = token_is__thread (token);
28381 gcc_rich_location richloc (location);
28382 if (gnu != decl_specs->gnu_thread_keyword_p)
28384 richloc.add_range (decl_specs->locations[ds_thread]);
28385 error_at (&richloc,
28386 "both %<__thread%> and %<thread_local%> specified");
28388 else
28390 richloc.add_fixit_remove ();
28391 error_at (&richloc, "duplicate %qD", token->u.value);
28394 else
28396 static const char *const decl_spec_names[] = {
28397 "signed",
28398 "unsigned",
28399 "short",
28400 "long",
28401 "const",
28402 "volatile",
28403 "restrict",
28404 "inline",
28405 "virtual",
28406 "explicit",
28407 "friend",
28408 "typedef",
28409 "using",
28410 "constexpr",
28411 "__complex"
28413 gcc_rich_location richloc (location);
28414 richloc.add_fixit_remove ();
28415 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28420 /* Return true iff the declarator specifier DS is present in the
28421 sequence of declarator specifiers DECL_SPECS. */
28423 bool
28424 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28425 cp_decl_spec ds)
28427 gcc_assert (ds < ds_last);
28429 if (decl_specs == NULL)
28430 return false;
28432 return decl_specs->locations[ds] != 0;
28435 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28436 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28438 static bool
28439 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28441 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28444 /* Issue an error message indicating that TOKEN_DESC was expected.
28445 If KEYWORD is true, it indicated this function is called by
28446 cp_parser_require_keword and the required token can only be
28447 a indicated keyword.
28449 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28450 within any error as the location of an "opening" token matching
28451 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28452 RT_CLOSE_PAREN). */
28454 static void
28455 cp_parser_required_error (cp_parser *parser,
28456 required_token token_desc,
28457 bool keyword,
28458 location_t matching_location)
28460 if (cp_parser_simulate_error (parser))
28461 return;
28463 const char *gmsgid = NULL;
28464 switch (token_desc)
28466 case RT_NEW:
28467 gmsgid = G_("expected %<new%>");
28468 break;
28469 case RT_DELETE:
28470 gmsgid = G_("expected %<delete%>");
28471 break;
28472 case RT_RETURN:
28473 gmsgid = G_("expected %<return%>");
28474 break;
28475 case RT_WHILE:
28476 gmsgid = G_("expected %<while%>");
28477 break;
28478 case RT_EXTERN:
28479 gmsgid = G_("expected %<extern%>");
28480 break;
28481 case RT_STATIC_ASSERT:
28482 gmsgid = G_("expected %<static_assert%>");
28483 break;
28484 case RT_DECLTYPE:
28485 gmsgid = G_("expected %<decltype%>");
28486 break;
28487 case RT_OPERATOR:
28488 gmsgid = G_("expected %<operator%>");
28489 break;
28490 case RT_CLASS:
28491 gmsgid = G_("expected %<class%>");
28492 break;
28493 case RT_TEMPLATE:
28494 gmsgid = G_("expected %<template%>");
28495 break;
28496 case RT_NAMESPACE:
28497 gmsgid = G_("expected %<namespace%>");
28498 break;
28499 case RT_USING:
28500 gmsgid = G_("expected %<using%>");
28501 break;
28502 case RT_ASM:
28503 gmsgid = G_("expected %<asm%>");
28504 break;
28505 case RT_TRY:
28506 gmsgid = G_("expected %<try%>");
28507 break;
28508 case RT_CATCH:
28509 gmsgid = G_("expected %<catch%>");
28510 break;
28511 case RT_THROW:
28512 gmsgid = G_("expected %<throw%>");
28513 break;
28514 case RT_LABEL:
28515 gmsgid = G_("expected %<__label__%>");
28516 break;
28517 case RT_AT_TRY:
28518 gmsgid = G_("expected %<@try%>");
28519 break;
28520 case RT_AT_SYNCHRONIZED:
28521 gmsgid = G_("expected %<@synchronized%>");
28522 break;
28523 case RT_AT_THROW:
28524 gmsgid = G_("expected %<@throw%>");
28525 break;
28526 case RT_TRANSACTION_ATOMIC:
28527 gmsgid = G_("expected %<__transaction_atomic%>");
28528 break;
28529 case RT_TRANSACTION_RELAXED:
28530 gmsgid = G_("expected %<__transaction_relaxed%>");
28531 break;
28532 default:
28533 break;
28536 if (!gmsgid && !keyword)
28538 switch (token_desc)
28540 case RT_SEMICOLON:
28541 gmsgid = G_("expected %<;%>");
28542 break;
28543 case RT_OPEN_PAREN:
28544 gmsgid = G_("expected %<(%>");
28545 break;
28546 case RT_CLOSE_BRACE:
28547 gmsgid = G_("expected %<}%>");
28548 break;
28549 case RT_OPEN_BRACE:
28550 gmsgid = G_("expected %<{%>");
28551 break;
28552 case RT_CLOSE_SQUARE:
28553 gmsgid = G_("expected %<]%>");
28554 break;
28555 case RT_OPEN_SQUARE:
28556 gmsgid = G_("expected %<[%>");
28557 break;
28558 case RT_COMMA:
28559 gmsgid = G_("expected %<,%>");
28560 break;
28561 case RT_SCOPE:
28562 gmsgid = G_("expected %<::%>");
28563 break;
28564 case RT_LESS:
28565 gmsgid = G_("expected %<<%>");
28566 break;
28567 case RT_GREATER:
28568 gmsgid = G_("expected %<>%>");
28569 break;
28570 case RT_EQ:
28571 gmsgid = G_("expected %<=%>");
28572 break;
28573 case RT_ELLIPSIS:
28574 gmsgid = G_("expected %<...%>");
28575 break;
28576 case RT_MULT:
28577 gmsgid = G_("expected %<*%>");
28578 break;
28579 case RT_COMPL:
28580 gmsgid = G_("expected %<~%>");
28581 break;
28582 case RT_COLON:
28583 gmsgid = G_("expected %<:%>");
28584 break;
28585 case RT_COLON_SCOPE:
28586 gmsgid = G_("expected %<:%> or %<::%>");
28587 break;
28588 case RT_CLOSE_PAREN:
28589 gmsgid = G_("expected %<)%>");
28590 break;
28591 case RT_COMMA_CLOSE_PAREN:
28592 gmsgid = G_("expected %<,%> or %<)%>");
28593 break;
28594 case RT_PRAGMA_EOL:
28595 gmsgid = G_("expected end of line");
28596 break;
28597 case RT_NAME:
28598 gmsgid = G_("expected identifier");
28599 break;
28600 case RT_SELECT:
28601 gmsgid = G_("expected selection-statement");
28602 break;
28603 case RT_ITERATION:
28604 gmsgid = G_("expected iteration-statement");
28605 break;
28606 case RT_JUMP:
28607 gmsgid = G_("expected jump-statement");
28608 break;
28609 case RT_CLASS_KEY:
28610 gmsgid = G_("expected class-key");
28611 break;
28612 case RT_CLASS_TYPENAME_TEMPLATE:
28613 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28614 break;
28615 default:
28616 gcc_unreachable ();
28620 if (gmsgid)
28621 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28625 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28626 issue an error message indicating that TOKEN_DESC was expected.
28628 Returns the token consumed, if the token had the appropriate type.
28629 Otherwise, returns NULL.
28631 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28632 within any error as the location of an "opening" token matching
28633 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28634 RT_CLOSE_PAREN). */
28636 static cp_token *
28637 cp_parser_require (cp_parser* parser,
28638 enum cpp_ttype type,
28639 required_token token_desc,
28640 location_t matching_location)
28642 if (cp_lexer_next_token_is (parser->lexer, type))
28643 return cp_lexer_consume_token (parser->lexer);
28644 else
28646 /* Output the MESSAGE -- unless we're parsing tentatively. */
28647 if (!cp_parser_simulate_error (parser))
28648 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28649 matching_location);
28650 return NULL;
28654 /* An error message is produced if the next token is not '>'.
28655 All further tokens are skipped until the desired token is
28656 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28658 static void
28659 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28661 /* Current level of '< ... >'. */
28662 unsigned level = 0;
28663 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28664 unsigned nesting_depth = 0;
28666 /* Are we ready, yet? If not, issue error message. */
28667 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28668 return;
28670 /* Skip tokens until the desired token is found. */
28671 while (true)
28673 /* Peek at the next token. */
28674 switch (cp_lexer_peek_token (parser->lexer)->type)
28676 case CPP_LESS:
28677 if (!nesting_depth)
28678 ++level;
28679 break;
28681 case CPP_RSHIFT:
28682 if (cxx_dialect == cxx98)
28683 /* C++0x views the `>>' operator as two `>' tokens, but
28684 C++98 does not. */
28685 break;
28686 else if (!nesting_depth && level-- == 0)
28688 /* We've hit a `>>' where the first `>' closes the
28689 template argument list, and the second `>' is
28690 spurious. Just consume the `>>' and stop; we've
28691 already produced at least one error. */
28692 cp_lexer_consume_token (parser->lexer);
28693 return;
28695 /* Fall through for C++0x, so we handle the second `>' in
28696 the `>>'. */
28697 gcc_fallthrough ();
28699 case CPP_GREATER:
28700 if (!nesting_depth && level-- == 0)
28702 /* We've reached the token we want, consume it and stop. */
28703 cp_lexer_consume_token (parser->lexer);
28704 return;
28706 break;
28708 case CPP_OPEN_PAREN:
28709 case CPP_OPEN_SQUARE:
28710 ++nesting_depth;
28711 break;
28713 case CPP_CLOSE_PAREN:
28714 case CPP_CLOSE_SQUARE:
28715 if (nesting_depth-- == 0)
28716 return;
28717 break;
28719 case CPP_EOF:
28720 case CPP_PRAGMA_EOL:
28721 case CPP_SEMICOLON:
28722 case CPP_OPEN_BRACE:
28723 case CPP_CLOSE_BRACE:
28724 /* The '>' was probably forgotten, don't look further. */
28725 return;
28727 default:
28728 break;
28731 /* Consume this token. */
28732 cp_lexer_consume_token (parser->lexer);
28736 /* If the next token is the indicated keyword, consume it. Otherwise,
28737 issue an error message indicating that TOKEN_DESC was expected.
28739 Returns the token consumed, if the token had the appropriate type.
28740 Otherwise, returns NULL. */
28742 static cp_token *
28743 cp_parser_require_keyword (cp_parser* parser,
28744 enum rid keyword,
28745 required_token token_desc)
28747 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28749 if (token && token->keyword != keyword)
28751 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28752 UNKNOWN_LOCATION);
28753 return NULL;
28756 return token;
28759 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28760 function-definition. */
28762 static bool
28763 cp_parser_token_starts_function_definition_p (cp_token* token)
28765 return (/* An ordinary function-body begins with an `{'. */
28766 token->type == CPP_OPEN_BRACE
28767 /* A ctor-initializer begins with a `:'. */
28768 || token->type == CPP_COLON
28769 /* A function-try-block begins with `try'. */
28770 || token->keyword == RID_TRY
28771 /* A function-transaction-block begins with `__transaction_atomic'
28772 or `__transaction_relaxed'. */
28773 || token->keyword == RID_TRANSACTION_ATOMIC
28774 || token->keyword == RID_TRANSACTION_RELAXED
28775 /* The named return value extension begins with `return'. */
28776 || token->keyword == RID_RETURN);
28779 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28780 definition. */
28782 static bool
28783 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28785 cp_token *token;
28787 token = cp_lexer_peek_token (parser->lexer);
28788 return (token->type == CPP_OPEN_BRACE
28789 || (token->type == CPP_COLON
28790 && !parser->colon_doesnt_start_class_def_p));
28793 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28794 C++0x) ending a template-argument. */
28796 static bool
28797 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28799 cp_token *token;
28801 token = cp_lexer_peek_token (parser->lexer);
28802 return (token->type == CPP_COMMA
28803 || token->type == CPP_GREATER
28804 || token->type == CPP_ELLIPSIS
28805 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28808 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28809 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28811 static bool
28812 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28813 size_t n)
28815 cp_token *token;
28817 token = cp_lexer_peek_nth_token (parser->lexer, n);
28818 if (token->type == CPP_LESS)
28819 return true;
28820 /* Check for the sequence `<::' in the original code. It would be lexed as
28821 `[:', where `[' is a digraph, and there is no whitespace before
28822 `:'. */
28823 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28825 cp_token *token2;
28826 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28827 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28828 return true;
28830 return false;
28833 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28834 or none_type otherwise. */
28836 static enum tag_types
28837 cp_parser_token_is_class_key (cp_token* token)
28839 switch (token->keyword)
28841 case RID_CLASS:
28842 return class_type;
28843 case RID_STRUCT:
28844 return record_type;
28845 case RID_UNION:
28846 return union_type;
28848 default:
28849 return none_type;
28853 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28854 or none_type otherwise or if the token is null. */
28856 static enum tag_types
28857 cp_parser_token_is_type_parameter_key (cp_token* token)
28859 if (!token)
28860 return none_type;
28862 switch (token->keyword)
28864 case RID_CLASS:
28865 return class_type;
28866 case RID_TYPENAME:
28867 return typename_type;
28869 default:
28870 return none_type;
28874 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28876 static void
28877 cp_parser_check_class_key (enum tag_types class_key, tree type)
28879 if (type == error_mark_node)
28880 return;
28881 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28883 if (permerror (input_location, "%qs tag used in naming %q#T",
28884 class_key == union_type ? "union"
28885 : class_key == record_type ? "struct" : "class",
28886 type))
28887 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28888 "%q#T was previously declared here", type);
28892 /* Issue an error message if DECL is redeclared with different
28893 access than its original declaration [class.access.spec/3].
28894 This applies to nested classes, nested class templates and
28895 enumerations [class.mem/1]. */
28897 static void
28898 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28900 if (!decl
28901 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28902 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28903 return;
28905 if ((TREE_PRIVATE (decl)
28906 != (current_access_specifier == access_private_node))
28907 || (TREE_PROTECTED (decl)
28908 != (current_access_specifier == access_protected_node)))
28909 error_at (location, "%qD redeclared with different access", decl);
28912 /* Look for the `template' keyword, as a syntactic disambiguator.
28913 Return TRUE iff it is present, in which case it will be
28914 consumed. */
28916 static bool
28917 cp_parser_optional_template_keyword (cp_parser *parser)
28919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28921 /* In C++98 the `template' keyword can only be used within templates;
28922 outside templates the parser can always figure out what is a
28923 template and what is not. In C++11, per the resolution of DR 468,
28924 `template' is allowed in cases where it is not strictly necessary. */
28925 if (!processing_template_decl
28926 && pedantic && cxx_dialect == cxx98)
28928 cp_token *token = cp_lexer_peek_token (parser->lexer);
28929 pedwarn (token->location, OPT_Wpedantic,
28930 "in C++98 %<template%> (as a disambiguator) is only "
28931 "allowed within templates");
28932 /* If this part of the token stream is rescanned, the same
28933 error message would be generated. So, we purge the token
28934 from the stream. */
28935 cp_lexer_purge_token (parser->lexer);
28936 return false;
28938 else
28940 /* Consume the `template' keyword. */
28941 cp_lexer_consume_token (parser->lexer);
28942 return true;
28945 return false;
28948 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28949 set PARSER->SCOPE, and perform other related actions. */
28951 static void
28952 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28954 struct tree_check *check_value;
28956 /* Get the stored value. */
28957 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28958 /* Set the scope from the stored value. */
28959 parser->scope = saved_checks_value (check_value);
28960 parser->qualifying_scope = check_value->qualifying_scope;
28961 parser->object_scope = NULL_TREE;
28964 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28965 encounter the end of a block before what we were looking for. */
28967 static bool
28968 cp_parser_cache_group (cp_parser *parser,
28969 enum cpp_ttype end,
28970 unsigned depth)
28972 while (true)
28974 cp_token *token = cp_lexer_peek_token (parser->lexer);
28976 /* Abort a parenthesized expression if we encounter a semicolon. */
28977 if ((end == CPP_CLOSE_PAREN || depth == 0)
28978 && token->type == CPP_SEMICOLON)
28979 return true;
28980 /* If we've reached the end of the file, stop. */
28981 if (token->type == CPP_EOF
28982 || (end != CPP_PRAGMA_EOL
28983 && token->type == CPP_PRAGMA_EOL))
28984 return true;
28985 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28986 /* We've hit the end of an enclosing block, so there's been some
28987 kind of syntax error. */
28988 return true;
28990 /* Consume the token. */
28991 cp_lexer_consume_token (parser->lexer);
28992 /* See if it starts a new group. */
28993 if (token->type == CPP_OPEN_BRACE)
28995 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28996 /* In theory this should probably check end == '}', but
28997 cp_parser_save_member_function_body needs it to exit
28998 after either '}' or ')' when called with ')'. */
28999 if (depth == 0)
29000 return false;
29002 else if (token->type == CPP_OPEN_PAREN)
29004 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29005 if (depth == 0 && end == CPP_CLOSE_PAREN)
29006 return false;
29008 else if (token->type == CPP_PRAGMA)
29009 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29010 else if (token->type == end)
29011 return false;
29015 /* Like above, for caching a default argument or NSDMI. Both of these are
29016 terminated by a non-nested comma, but it can be unclear whether or not a
29017 comma is nested in a template argument list unless we do more parsing.
29018 In order to handle this ambiguity, when we encounter a ',' after a '<'
29019 we try to parse what follows as a parameter-declaration-list (in the
29020 case of a default argument) or a member-declarator (in the case of an
29021 NSDMI). If that succeeds, then we stop caching. */
29023 static tree
29024 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29026 unsigned depth = 0;
29027 int maybe_template_id = 0;
29028 cp_token *first_token;
29029 cp_token *token;
29030 tree default_argument;
29032 /* Add tokens until we have processed the entire default
29033 argument. We add the range [first_token, token). */
29034 first_token = cp_lexer_peek_token (parser->lexer);
29035 if (first_token->type == CPP_OPEN_BRACE)
29037 /* For list-initialization, this is straightforward. */
29038 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29039 token = cp_lexer_peek_token (parser->lexer);
29041 else while (true)
29043 bool done = false;
29045 /* Peek at the next token. */
29046 token = cp_lexer_peek_token (parser->lexer);
29047 /* What we do depends on what token we have. */
29048 switch (token->type)
29050 /* In valid code, a default argument must be
29051 immediately followed by a `,' `)', or `...'. */
29052 case CPP_COMMA:
29053 if (depth == 0 && maybe_template_id)
29055 /* If we've seen a '<', we might be in a
29056 template-argument-list. Until Core issue 325 is
29057 resolved, we don't know how this situation ought
29058 to be handled, so try to DTRT. We check whether
29059 what comes after the comma is a valid parameter
29060 declaration list. If it is, then the comma ends
29061 the default argument; otherwise the default
29062 argument continues. */
29063 bool error = false;
29064 cp_token *peek;
29066 /* Set ITALP so cp_parser_parameter_declaration_list
29067 doesn't decide to commit to this parse. */
29068 bool saved_italp = parser->in_template_argument_list_p;
29069 parser->in_template_argument_list_p = true;
29071 cp_parser_parse_tentatively (parser);
29073 if (nsdmi)
29075 /* Parse declarators until we reach a non-comma or
29076 somthing that cannot be an initializer.
29077 Just checking whether we're looking at a single
29078 declarator is insufficient. Consider:
29079 int var = tuple<T,U>::x;
29080 The template parameter 'U' looks exactly like a
29081 declarator. */
29084 int ctor_dtor_or_conv_p;
29085 cp_lexer_consume_token (parser->lexer);
29086 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29087 &ctor_dtor_or_conv_p,
29088 /*parenthesized_p=*/NULL,
29089 /*member_p=*/true,
29090 /*friend_p=*/false);
29091 peek = cp_lexer_peek_token (parser->lexer);
29092 if (cp_parser_error_occurred (parser))
29093 break;
29095 while (peek->type == CPP_COMMA);
29096 /* If we met an '=' or ';' then the original comma
29097 was the end of the NSDMI. Otherwise assume
29098 we're still in the NSDMI. */
29099 error = (peek->type != CPP_EQ
29100 && peek->type != CPP_SEMICOLON);
29102 else
29104 cp_lexer_consume_token (parser->lexer);
29105 begin_scope (sk_function_parms, NULL_TREE);
29106 if (cp_parser_parameter_declaration_list (parser)
29107 == error_mark_node)
29108 error = true;
29109 pop_bindings_and_leave_scope ();
29111 if (!cp_parser_error_occurred (parser) && !error)
29112 done = true;
29113 cp_parser_abort_tentative_parse (parser);
29115 parser->in_template_argument_list_p = saved_italp;
29116 break;
29118 /* FALLTHRU */
29119 case CPP_CLOSE_PAREN:
29120 case CPP_ELLIPSIS:
29121 /* If we run into a non-nested `;', `}', or `]',
29122 then the code is invalid -- but the default
29123 argument is certainly over. */
29124 case CPP_SEMICOLON:
29125 case CPP_CLOSE_BRACE:
29126 case CPP_CLOSE_SQUARE:
29127 if (depth == 0
29128 /* Handle correctly int n = sizeof ... ( p ); */
29129 && token->type != CPP_ELLIPSIS)
29130 done = true;
29131 /* Update DEPTH, if necessary. */
29132 else if (token->type == CPP_CLOSE_PAREN
29133 || token->type == CPP_CLOSE_BRACE
29134 || token->type == CPP_CLOSE_SQUARE)
29135 --depth;
29136 break;
29138 case CPP_OPEN_PAREN:
29139 case CPP_OPEN_SQUARE:
29140 case CPP_OPEN_BRACE:
29141 ++depth;
29142 break;
29144 case CPP_LESS:
29145 if (depth == 0)
29146 /* This might be the comparison operator, or it might
29147 start a template argument list. */
29148 ++maybe_template_id;
29149 break;
29151 case CPP_RSHIFT:
29152 if (cxx_dialect == cxx98)
29153 break;
29154 /* Fall through for C++0x, which treats the `>>'
29155 operator like two `>' tokens in certain
29156 cases. */
29157 gcc_fallthrough ();
29159 case CPP_GREATER:
29160 if (depth == 0)
29162 /* This might be an operator, or it might close a
29163 template argument list. But if a previous '<'
29164 started a template argument list, this will have
29165 closed it, so we can't be in one anymore. */
29166 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29167 if (maybe_template_id < 0)
29168 maybe_template_id = 0;
29170 break;
29172 /* If we run out of tokens, issue an error message. */
29173 case CPP_EOF:
29174 case CPP_PRAGMA_EOL:
29175 error_at (token->location, "file ends in default argument");
29176 return error_mark_node;
29178 case CPP_NAME:
29179 case CPP_SCOPE:
29180 /* In these cases, we should look for template-ids.
29181 For example, if the default argument is
29182 `X<int, double>()', we need to do name lookup to
29183 figure out whether or not `X' is a template; if
29184 so, the `,' does not end the default argument.
29186 That is not yet done. */
29187 break;
29189 default:
29190 break;
29193 /* If we've reached the end, stop. */
29194 if (done)
29195 break;
29197 /* Add the token to the token block. */
29198 token = cp_lexer_consume_token (parser->lexer);
29201 /* Create a DEFAULT_ARG to represent the unparsed default
29202 argument. */
29203 default_argument = make_node (DEFAULT_ARG);
29204 DEFARG_TOKENS (default_argument)
29205 = cp_token_cache_new (first_token, token);
29206 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29208 return default_argument;
29211 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29213 location_t
29214 defarg_location (tree default_argument)
29216 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29217 location_t start = tokens->first->location;
29218 location_t end = tokens->last->location;
29219 return make_location (start, start, end);
29222 /* Begin parsing tentatively. We always save tokens while parsing
29223 tentatively so that if the tentative parsing fails we can restore the
29224 tokens. */
29226 static void
29227 cp_parser_parse_tentatively (cp_parser* parser)
29229 /* Enter a new parsing context. */
29230 parser->context = cp_parser_context_new (parser->context);
29231 /* Begin saving tokens. */
29232 cp_lexer_save_tokens (parser->lexer);
29233 /* In order to avoid repetitive access control error messages,
29234 access checks are queued up until we are no longer parsing
29235 tentatively. */
29236 push_deferring_access_checks (dk_deferred);
29239 /* Commit to the currently active tentative parse. */
29241 static void
29242 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29244 cp_parser_context *context;
29245 cp_lexer *lexer;
29247 /* Mark all of the levels as committed. */
29248 lexer = parser->lexer;
29249 for (context = parser->context; context->next; context = context->next)
29251 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29252 break;
29253 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29254 while (!cp_lexer_saving_tokens (lexer))
29255 lexer = lexer->next;
29256 cp_lexer_commit_tokens (lexer);
29260 /* Commit to the topmost currently active tentative parse.
29262 Note that this function shouldn't be called when there are
29263 irreversible side-effects while in a tentative state. For
29264 example, we shouldn't create a permanent entry in the symbol
29265 table, or issue an error message that might not apply if the
29266 tentative parse is aborted. */
29268 static void
29269 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29271 cp_parser_context *context = parser->context;
29272 cp_lexer *lexer = parser->lexer;
29274 if (context)
29276 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29277 return;
29278 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29280 while (!cp_lexer_saving_tokens (lexer))
29281 lexer = lexer->next;
29282 cp_lexer_commit_tokens (lexer);
29286 /* Abort the currently active tentative parse. All consumed tokens
29287 will be rolled back, and no diagnostics will be issued. */
29289 static void
29290 cp_parser_abort_tentative_parse (cp_parser* parser)
29292 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29293 || errorcount > 0);
29294 cp_parser_simulate_error (parser);
29295 /* Now, pretend that we want to see if the construct was
29296 successfully parsed. */
29297 cp_parser_parse_definitely (parser);
29300 /* Stop parsing tentatively. If a parse error has occurred, restore the
29301 token stream. Otherwise, commit to the tokens we have consumed.
29302 Returns true if no error occurred; false otherwise. */
29304 static bool
29305 cp_parser_parse_definitely (cp_parser* parser)
29307 bool error_occurred;
29308 cp_parser_context *context;
29310 /* Remember whether or not an error occurred, since we are about to
29311 destroy that information. */
29312 error_occurred = cp_parser_error_occurred (parser);
29313 /* Remove the topmost context from the stack. */
29314 context = parser->context;
29315 parser->context = context->next;
29316 /* If no parse errors occurred, commit to the tentative parse. */
29317 if (!error_occurred)
29319 /* Commit to the tokens read tentatively, unless that was
29320 already done. */
29321 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29322 cp_lexer_commit_tokens (parser->lexer);
29324 pop_to_parent_deferring_access_checks ();
29326 /* Otherwise, if errors occurred, roll back our state so that things
29327 are just as they were before we began the tentative parse. */
29328 else
29330 cp_lexer_rollback_tokens (parser->lexer);
29331 pop_deferring_access_checks ();
29333 /* Add the context to the front of the free list. */
29334 context->next = cp_parser_context_free_list;
29335 cp_parser_context_free_list = context;
29337 return !error_occurred;
29340 /* Returns true if we are parsing tentatively and are not committed to
29341 this tentative parse. */
29343 static bool
29344 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29346 return (cp_parser_parsing_tentatively (parser)
29347 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29350 /* Returns nonzero iff an error has occurred during the most recent
29351 tentative parse. */
29353 static bool
29354 cp_parser_error_occurred (cp_parser* parser)
29356 return (cp_parser_parsing_tentatively (parser)
29357 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29360 /* Returns nonzero if GNU extensions are allowed. */
29362 static bool
29363 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29365 return parser->allow_gnu_extensions_p;
29368 /* Objective-C++ Productions */
29371 /* Parse an Objective-C expression, which feeds into a primary-expression
29372 above.
29374 objc-expression:
29375 objc-message-expression
29376 objc-string-literal
29377 objc-encode-expression
29378 objc-protocol-expression
29379 objc-selector-expression
29381 Returns a tree representation of the expression. */
29383 static cp_expr
29384 cp_parser_objc_expression (cp_parser* parser)
29386 /* Try to figure out what kind of declaration is present. */
29387 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29389 switch (kwd->type)
29391 case CPP_OPEN_SQUARE:
29392 return cp_parser_objc_message_expression (parser);
29394 case CPP_OBJC_STRING:
29395 kwd = cp_lexer_consume_token (parser->lexer);
29396 return objc_build_string_object (kwd->u.value);
29398 case CPP_KEYWORD:
29399 switch (kwd->keyword)
29401 case RID_AT_ENCODE:
29402 return cp_parser_objc_encode_expression (parser);
29404 case RID_AT_PROTOCOL:
29405 return cp_parser_objc_protocol_expression (parser);
29407 case RID_AT_SELECTOR:
29408 return cp_parser_objc_selector_expression (parser);
29410 default:
29411 break;
29413 /* FALLTHRU */
29414 default:
29415 error_at (kwd->location,
29416 "misplaced %<@%D%> Objective-C++ construct",
29417 kwd->u.value);
29418 cp_parser_skip_to_end_of_block_or_statement (parser);
29421 return error_mark_node;
29424 /* Parse an Objective-C message expression.
29426 objc-message-expression:
29427 [ objc-message-receiver objc-message-args ]
29429 Returns a representation of an Objective-C message. */
29431 static tree
29432 cp_parser_objc_message_expression (cp_parser* parser)
29434 tree receiver, messageargs;
29436 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29437 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29438 receiver = cp_parser_objc_message_receiver (parser);
29439 messageargs = cp_parser_objc_message_args (parser);
29440 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29441 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29443 tree result = objc_build_message_expr (receiver, messageargs);
29445 /* Construct a location e.g.
29446 [self func1:5]
29447 ^~~~~~~~~~~~~~
29448 ranging from the '[' to the ']', with the caret at the start. */
29449 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29450 protected_set_expr_location (result, combined_loc);
29452 return result;
29455 /* Parse an objc-message-receiver.
29457 objc-message-receiver:
29458 expression
29459 simple-type-specifier
29461 Returns a representation of the type or expression. */
29463 static tree
29464 cp_parser_objc_message_receiver (cp_parser* parser)
29466 tree rcv;
29468 /* An Objective-C message receiver may be either (1) a type
29469 or (2) an expression. */
29470 cp_parser_parse_tentatively (parser);
29471 rcv = cp_parser_expression (parser);
29473 /* If that worked out, fine. */
29474 if (cp_parser_parse_definitely (parser))
29475 return rcv;
29477 cp_parser_parse_tentatively (parser);
29478 rcv = cp_parser_simple_type_specifier (parser,
29479 /*decl_specs=*/NULL,
29480 CP_PARSER_FLAGS_NONE);
29482 if (cp_parser_parse_definitely (parser))
29483 return objc_get_class_reference (rcv);
29485 cp_parser_error (parser, "objective-c++ message receiver expected");
29486 return error_mark_node;
29489 /* Parse the arguments and selectors comprising an Objective-C message.
29491 objc-message-args:
29492 objc-selector
29493 objc-selector-args
29494 objc-selector-args , objc-comma-args
29496 objc-selector-args:
29497 objc-selector [opt] : assignment-expression
29498 objc-selector-args objc-selector [opt] : assignment-expression
29500 objc-comma-args:
29501 assignment-expression
29502 objc-comma-args , assignment-expression
29504 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29505 selector arguments and TREE_VALUE containing a list of comma
29506 arguments. */
29508 static tree
29509 cp_parser_objc_message_args (cp_parser* parser)
29511 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29512 bool maybe_unary_selector_p = true;
29513 cp_token *token = cp_lexer_peek_token (parser->lexer);
29515 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29517 tree selector = NULL_TREE, arg;
29519 if (token->type != CPP_COLON)
29520 selector = cp_parser_objc_selector (parser);
29522 /* Detect if we have a unary selector. */
29523 if (maybe_unary_selector_p
29524 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29525 return build_tree_list (selector, NULL_TREE);
29527 maybe_unary_selector_p = false;
29528 cp_parser_require (parser, CPP_COLON, RT_COLON);
29529 arg = cp_parser_assignment_expression (parser);
29531 sel_args
29532 = chainon (sel_args,
29533 build_tree_list (selector, arg));
29535 token = cp_lexer_peek_token (parser->lexer);
29538 /* Handle non-selector arguments, if any. */
29539 while (token->type == CPP_COMMA)
29541 tree arg;
29543 cp_lexer_consume_token (parser->lexer);
29544 arg = cp_parser_assignment_expression (parser);
29546 addl_args
29547 = chainon (addl_args,
29548 build_tree_list (NULL_TREE, arg));
29550 token = cp_lexer_peek_token (parser->lexer);
29553 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29555 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29556 return build_tree_list (error_mark_node, error_mark_node);
29559 return build_tree_list (sel_args, addl_args);
29562 /* Parse an Objective-C encode expression.
29564 objc-encode-expression:
29565 @encode objc-typename
29567 Returns an encoded representation of the type argument. */
29569 static cp_expr
29570 cp_parser_objc_encode_expression (cp_parser* parser)
29572 tree type;
29573 cp_token *token;
29574 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29576 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29577 matching_parens parens;
29578 parens.require_open (parser);
29579 token = cp_lexer_peek_token (parser->lexer);
29580 type = complete_type (cp_parser_type_id (parser));
29581 parens.require_close (parser);
29583 if (!type)
29585 error_at (token->location,
29586 "%<@encode%> must specify a type as an argument");
29587 return error_mark_node;
29590 /* This happens if we find @encode(T) (where T is a template
29591 typename or something dependent on a template typename) when
29592 parsing a template. In that case, we can't compile it
29593 immediately, but we rather create an AT_ENCODE_EXPR which will
29594 need to be instantiated when the template is used.
29596 if (dependent_type_p (type))
29598 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29599 TREE_READONLY (value) = 1;
29600 return value;
29604 /* Build a location of the form:
29605 @encode(int)
29606 ^~~~~~~~~~~~
29607 with caret==start at the @ token, finishing at the close paren. */
29608 location_t combined_loc
29609 = make_location (start_loc, start_loc,
29610 cp_lexer_previous_token (parser->lexer)->location);
29612 return cp_expr (objc_build_encode_expr (type), combined_loc);
29615 /* Parse an Objective-C @defs expression. */
29617 static tree
29618 cp_parser_objc_defs_expression (cp_parser *parser)
29620 tree name;
29622 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29623 matching_parens parens;
29624 parens.require_open (parser);
29625 name = cp_parser_identifier (parser);
29626 parens.require_close (parser);
29628 return objc_get_class_ivars (name);
29631 /* Parse an Objective-C protocol expression.
29633 objc-protocol-expression:
29634 @protocol ( identifier )
29636 Returns a representation of the protocol expression. */
29638 static tree
29639 cp_parser_objc_protocol_expression (cp_parser* parser)
29641 tree proto;
29642 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29644 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29645 matching_parens parens;
29646 parens.require_open (parser);
29647 proto = cp_parser_identifier (parser);
29648 parens.require_close (parser);
29650 /* Build a location of the form:
29651 @protocol(prot)
29652 ^~~~~~~~~~~~~~~
29653 with caret==start at the @ token, finishing at the close paren. */
29654 location_t combined_loc
29655 = make_location (start_loc, start_loc,
29656 cp_lexer_previous_token (parser->lexer)->location);
29657 tree result = objc_build_protocol_expr (proto);
29658 protected_set_expr_location (result, combined_loc);
29659 return result;
29662 /* Parse an Objective-C selector expression.
29664 objc-selector-expression:
29665 @selector ( objc-method-signature )
29667 objc-method-signature:
29668 objc-selector
29669 objc-selector-seq
29671 objc-selector-seq:
29672 objc-selector :
29673 objc-selector-seq objc-selector :
29675 Returns a representation of the method selector. */
29677 static tree
29678 cp_parser_objc_selector_expression (cp_parser* parser)
29680 tree sel_seq = NULL_TREE;
29681 bool maybe_unary_selector_p = true;
29682 cp_token *token;
29683 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29685 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29686 matching_parens parens;
29687 parens.require_open (parser);
29688 token = cp_lexer_peek_token (parser->lexer);
29690 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29691 || token->type == CPP_SCOPE)
29693 tree selector = NULL_TREE;
29695 if (token->type != CPP_COLON
29696 || token->type == CPP_SCOPE)
29697 selector = cp_parser_objc_selector (parser);
29699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29700 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29702 /* Detect if we have a unary selector. */
29703 if (maybe_unary_selector_p)
29705 sel_seq = selector;
29706 goto finish_selector;
29708 else
29710 cp_parser_error (parser, "expected %<:%>");
29713 maybe_unary_selector_p = false;
29714 token = cp_lexer_consume_token (parser->lexer);
29716 if (token->type == CPP_SCOPE)
29718 sel_seq
29719 = chainon (sel_seq,
29720 build_tree_list (selector, NULL_TREE));
29721 sel_seq
29722 = chainon (sel_seq,
29723 build_tree_list (NULL_TREE, NULL_TREE));
29725 else
29726 sel_seq
29727 = chainon (sel_seq,
29728 build_tree_list (selector, NULL_TREE));
29730 token = cp_lexer_peek_token (parser->lexer);
29733 finish_selector:
29734 parens.require_close (parser);
29737 /* Build a location of the form:
29738 @selector(func)
29739 ^~~~~~~~~~~~~~~
29740 with caret==start at the @ token, finishing at the close paren. */
29741 location_t combined_loc
29742 = make_location (loc, loc,
29743 cp_lexer_previous_token (parser->lexer)->location);
29744 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29745 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29746 protected_set_expr_location (result, combined_loc);
29747 return result;
29750 /* Parse a list of identifiers.
29752 objc-identifier-list:
29753 identifier
29754 objc-identifier-list , identifier
29756 Returns a TREE_LIST of identifier nodes. */
29758 static tree
29759 cp_parser_objc_identifier_list (cp_parser* parser)
29761 tree identifier;
29762 tree list;
29763 cp_token *sep;
29765 identifier = cp_parser_identifier (parser);
29766 if (identifier == error_mark_node)
29767 return error_mark_node;
29769 list = build_tree_list (NULL_TREE, identifier);
29770 sep = cp_lexer_peek_token (parser->lexer);
29772 while (sep->type == CPP_COMMA)
29774 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29775 identifier = cp_parser_identifier (parser);
29776 if (identifier == error_mark_node)
29777 return list;
29779 list = chainon (list, build_tree_list (NULL_TREE,
29780 identifier));
29781 sep = cp_lexer_peek_token (parser->lexer);
29784 return list;
29787 /* Parse an Objective-C alias declaration.
29789 objc-alias-declaration:
29790 @compatibility_alias identifier identifier ;
29792 This function registers the alias mapping with the Objective-C front end.
29793 It returns nothing. */
29795 static void
29796 cp_parser_objc_alias_declaration (cp_parser* parser)
29798 tree alias, orig;
29800 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29801 alias = cp_parser_identifier (parser);
29802 orig = cp_parser_identifier (parser);
29803 objc_declare_alias (alias, orig);
29804 cp_parser_consume_semicolon_at_end_of_statement (parser);
29807 /* Parse an Objective-C class forward-declaration.
29809 objc-class-declaration:
29810 @class objc-identifier-list ;
29812 The function registers the forward declarations with the Objective-C
29813 front end. It returns nothing. */
29815 static void
29816 cp_parser_objc_class_declaration (cp_parser* parser)
29818 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29819 while (true)
29821 tree id;
29823 id = cp_parser_identifier (parser);
29824 if (id == error_mark_node)
29825 break;
29827 objc_declare_class (id);
29829 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29830 cp_lexer_consume_token (parser->lexer);
29831 else
29832 break;
29834 cp_parser_consume_semicolon_at_end_of_statement (parser);
29837 /* Parse a list of Objective-C protocol references.
29839 objc-protocol-refs-opt:
29840 objc-protocol-refs [opt]
29842 objc-protocol-refs:
29843 < objc-identifier-list >
29845 Returns a TREE_LIST of identifiers, if any. */
29847 static tree
29848 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29850 tree protorefs = NULL_TREE;
29852 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29854 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29855 protorefs = cp_parser_objc_identifier_list (parser);
29856 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29859 return protorefs;
29862 /* Parse a Objective-C visibility specification. */
29864 static void
29865 cp_parser_objc_visibility_spec (cp_parser* parser)
29867 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29869 switch (vis->keyword)
29871 case RID_AT_PRIVATE:
29872 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29873 break;
29874 case RID_AT_PROTECTED:
29875 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29876 break;
29877 case RID_AT_PUBLIC:
29878 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29879 break;
29880 case RID_AT_PACKAGE:
29881 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29882 break;
29883 default:
29884 return;
29887 /* Eat '@private'/'@protected'/'@public'. */
29888 cp_lexer_consume_token (parser->lexer);
29891 /* Parse an Objective-C method type. Return 'true' if it is a class
29892 (+) method, and 'false' if it is an instance (-) method. */
29894 static inline bool
29895 cp_parser_objc_method_type (cp_parser* parser)
29897 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29898 return true;
29899 else
29900 return false;
29903 /* Parse an Objective-C protocol qualifier. */
29905 static tree
29906 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29908 tree quals = NULL_TREE, node;
29909 cp_token *token = cp_lexer_peek_token (parser->lexer);
29911 node = token->u.value;
29913 while (node && identifier_p (node)
29914 && (node == ridpointers [(int) RID_IN]
29915 || node == ridpointers [(int) RID_OUT]
29916 || node == ridpointers [(int) RID_INOUT]
29917 || node == ridpointers [(int) RID_BYCOPY]
29918 || node == ridpointers [(int) RID_BYREF]
29919 || node == ridpointers [(int) RID_ONEWAY]))
29921 quals = tree_cons (NULL_TREE, node, quals);
29922 cp_lexer_consume_token (parser->lexer);
29923 token = cp_lexer_peek_token (parser->lexer);
29924 node = token->u.value;
29927 return quals;
29930 /* Parse an Objective-C typename. */
29932 static tree
29933 cp_parser_objc_typename (cp_parser* parser)
29935 tree type_name = NULL_TREE;
29937 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29939 tree proto_quals, cp_type = NULL_TREE;
29941 matching_parens parens;
29942 parens.consume_open (parser); /* Eat '('. */
29943 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29945 /* An ObjC type name may consist of just protocol qualifiers, in which
29946 case the type shall default to 'id'. */
29947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29949 cp_type = cp_parser_type_id (parser);
29951 /* If the type could not be parsed, an error has already
29952 been produced. For error recovery, behave as if it had
29953 not been specified, which will use the default type
29954 'id'. */
29955 if (cp_type == error_mark_node)
29957 cp_type = NULL_TREE;
29958 /* We need to skip to the closing parenthesis as
29959 cp_parser_type_id() does not seem to do it for
29960 us. */
29961 cp_parser_skip_to_closing_parenthesis (parser,
29962 /*recovering=*/true,
29963 /*or_comma=*/false,
29964 /*consume_paren=*/false);
29968 parens.require_close (parser);
29969 type_name = build_tree_list (proto_quals, cp_type);
29972 return type_name;
29975 /* Check to see if TYPE refers to an Objective-C selector name. */
29977 static bool
29978 cp_parser_objc_selector_p (enum cpp_ttype type)
29980 return (type == CPP_NAME || type == CPP_KEYWORD
29981 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29982 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29983 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29984 || type == CPP_XOR || type == CPP_XOR_EQ);
29987 /* Parse an Objective-C selector. */
29989 static tree
29990 cp_parser_objc_selector (cp_parser* parser)
29992 cp_token *token = cp_lexer_consume_token (parser->lexer);
29994 if (!cp_parser_objc_selector_p (token->type))
29996 error_at (token->location, "invalid Objective-C++ selector name");
29997 return error_mark_node;
30000 /* C++ operator names are allowed to appear in ObjC selectors. */
30001 switch (token->type)
30003 case CPP_AND_AND: return get_identifier ("and");
30004 case CPP_AND_EQ: return get_identifier ("and_eq");
30005 case CPP_AND: return get_identifier ("bitand");
30006 case CPP_OR: return get_identifier ("bitor");
30007 case CPP_COMPL: return get_identifier ("compl");
30008 case CPP_NOT: return get_identifier ("not");
30009 case CPP_NOT_EQ: return get_identifier ("not_eq");
30010 case CPP_OR_OR: return get_identifier ("or");
30011 case CPP_OR_EQ: return get_identifier ("or_eq");
30012 case CPP_XOR: return get_identifier ("xor");
30013 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30014 default: return token->u.value;
30018 /* Parse an Objective-C params list. */
30020 static tree
30021 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30023 tree params = NULL_TREE;
30024 bool maybe_unary_selector_p = true;
30025 cp_token *token = cp_lexer_peek_token (parser->lexer);
30027 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30029 tree selector = NULL_TREE, type_name, identifier;
30030 tree parm_attr = NULL_TREE;
30032 if (token->keyword == RID_ATTRIBUTE)
30033 break;
30035 if (token->type != CPP_COLON)
30036 selector = cp_parser_objc_selector (parser);
30038 /* Detect if we have a unary selector. */
30039 if (maybe_unary_selector_p
30040 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30042 params = selector; /* Might be followed by attributes. */
30043 break;
30046 maybe_unary_selector_p = false;
30047 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30049 /* Something went quite wrong. There should be a colon
30050 here, but there is not. Stop parsing parameters. */
30051 break;
30053 type_name = cp_parser_objc_typename (parser);
30054 /* New ObjC allows attributes on parameters too. */
30055 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30056 parm_attr = cp_parser_attributes_opt (parser);
30057 identifier = cp_parser_identifier (parser);
30059 params
30060 = chainon (params,
30061 objc_build_keyword_decl (selector,
30062 type_name,
30063 identifier,
30064 parm_attr));
30066 token = cp_lexer_peek_token (parser->lexer);
30069 if (params == NULL_TREE)
30071 cp_parser_error (parser, "objective-c++ method declaration is expected");
30072 return error_mark_node;
30075 /* We allow tail attributes for the method. */
30076 if (token->keyword == RID_ATTRIBUTE)
30078 *attributes = cp_parser_attributes_opt (parser);
30079 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30080 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30081 return params;
30082 cp_parser_error (parser,
30083 "method attributes must be specified at the end");
30084 return error_mark_node;
30087 if (params == NULL_TREE)
30089 cp_parser_error (parser, "objective-c++ method declaration is expected");
30090 return error_mark_node;
30092 return params;
30095 /* Parse the non-keyword Objective-C params. */
30097 static tree
30098 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30099 tree* attributes)
30101 tree params = make_node (TREE_LIST);
30102 cp_token *token = cp_lexer_peek_token (parser->lexer);
30103 *ellipsisp = false; /* Initially, assume no ellipsis. */
30105 while (token->type == CPP_COMMA)
30107 cp_parameter_declarator *parmdecl;
30108 tree parm;
30110 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30111 token = cp_lexer_peek_token (parser->lexer);
30113 if (token->type == CPP_ELLIPSIS)
30115 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30116 *ellipsisp = true;
30117 token = cp_lexer_peek_token (parser->lexer);
30118 break;
30121 /* TODO: parse attributes for tail parameters. */
30122 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30123 parm = grokdeclarator (parmdecl->declarator,
30124 &parmdecl->decl_specifiers,
30125 PARM, /*initialized=*/0,
30126 /*attrlist=*/NULL);
30128 chainon (params, build_tree_list (NULL_TREE, parm));
30129 token = cp_lexer_peek_token (parser->lexer);
30132 /* We allow tail attributes for the method. */
30133 if (token->keyword == RID_ATTRIBUTE)
30135 if (*attributes == NULL_TREE)
30137 *attributes = cp_parser_attributes_opt (parser);
30138 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30139 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30140 return params;
30142 else
30143 /* We have an error, but parse the attributes, so that we can
30144 carry on. */
30145 *attributes = cp_parser_attributes_opt (parser);
30147 cp_parser_error (parser,
30148 "method attributes must be specified at the end");
30149 return error_mark_node;
30152 return params;
30155 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30157 static void
30158 cp_parser_objc_interstitial_code (cp_parser* parser)
30160 cp_token *token = cp_lexer_peek_token (parser->lexer);
30162 /* If the next token is `extern' and the following token is a string
30163 literal, then we have a linkage specification. */
30164 if (token->keyword == RID_EXTERN
30165 && cp_parser_is_pure_string_literal
30166 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30167 cp_parser_linkage_specification (parser);
30168 /* Handle #pragma, if any. */
30169 else if (token->type == CPP_PRAGMA)
30170 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30171 /* Allow stray semicolons. */
30172 else if (token->type == CPP_SEMICOLON)
30173 cp_lexer_consume_token (parser->lexer);
30174 /* Mark methods as optional or required, when building protocols. */
30175 else if (token->keyword == RID_AT_OPTIONAL)
30177 cp_lexer_consume_token (parser->lexer);
30178 objc_set_method_opt (true);
30180 else if (token->keyword == RID_AT_REQUIRED)
30182 cp_lexer_consume_token (parser->lexer);
30183 objc_set_method_opt (false);
30185 else if (token->keyword == RID_NAMESPACE)
30186 cp_parser_namespace_definition (parser);
30187 /* Other stray characters must generate errors. */
30188 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30190 cp_lexer_consume_token (parser->lexer);
30191 error ("stray %qs between Objective-C++ methods",
30192 token->type == CPP_OPEN_BRACE ? "{" : "}");
30194 /* Finally, try to parse a block-declaration, or a function-definition. */
30195 else
30196 cp_parser_block_declaration (parser, /*statement_p=*/false);
30199 /* Parse a method signature. */
30201 static tree
30202 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30204 tree rettype, kwdparms, optparms;
30205 bool ellipsis = false;
30206 bool is_class_method;
30208 is_class_method = cp_parser_objc_method_type (parser);
30209 rettype = cp_parser_objc_typename (parser);
30210 *attributes = NULL_TREE;
30211 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30212 if (kwdparms == error_mark_node)
30213 return error_mark_node;
30214 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30215 if (optparms == error_mark_node)
30216 return error_mark_node;
30218 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30221 static bool
30222 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30224 tree tattr;
30225 cp_lexer_save_tokens (parser->lexer);
30226 tattr = cp_parser_attributes_opt (parser);
30227 gcc_assert (tattr) ;
30229 /* If the attributes are followed by a method introducer, this is not allowed.
30230 Dump the attributes and flag the situation. */
30231 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30232 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30233 return true;
30235 /* Otherwise, the attributes introduce some interstitial code, possibly so
30236 rewind to allow that check. */
30237 cp_lexer_rollback_tokens (parser->lexer);
30238 return false;
30241 /* Parse an Objective-C method prototype list. */
30243 static void
30244 cp_parser_objc_method_prototype_list (cp_parser* parser)
30246 cp_token *token = cp_lexer_peek_token (parser->lexer);
30248 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30250 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30252 tree attributes, sig;
30253 bool is_class_method;
30254 if (token->type == CPP_PLUS)
30255 is_class_method = true;
30256 else
30257 is_class_method = false;
30258 sig = cp_parser_objc_method_signature (parser, &attributes);
30259 if (sig == error_mark_node)
30261 cp_parser_skip_to_end_of_block_or_statement (parser);
30262 token = cp_lexer_peek_token (parser->lexer);
30263 continue;
30265 objc_add_method_declaration (is_class_method, sig, attributes);
30266 cp_parser_consume_semicolon_at_end_of_statement (parser);
30268 else if (token->keyword == RID_AT_PROPERTY)
30269 cp_parser_objc_at_property_declaration (parser);
30270 else if (token->keyword == RID_ATTRIBUTE
30271 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30272 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30273 OPT_Wattributes,
30274 "prefix attributes are ignored for methods");
30275 else
30276 /* Allow for interspersed non-ObjC++ code. */
30277 cp_parser_objc_interstitial_code (parser);
30279 token = cp_lexer_peek_token (parser->lexer);
30282 if (token->type != CPP_EOF)
30283 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30284 else
30285 cp_parser_error (parser, "expected %<@end%>");
30287 objc_finish_interface ();
30290 /* Parse an Objective-C method definition list. */
30292 static void
30293 cp_parser_objc_method_definition_list (cp_parser* parser)
30295 cp_token *token = cp_lexer_peek_token (parser->lexer);
30297 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30299 tree meth;
30301 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30303 cp_token *ptk;
30304 tree sig, attribute;
30305 bool is_class_method;
30306 if (token->type == CPP_PLUS)
30307 is_class_method = true;
30308 else
30309 is_class_method = false;
30310 push_deferring_access_checks (dk_deferred);
30311 sig = cp_parser_objc_method_signature (parser, &attribute);
30312 if (sig == error_mark_node)
30314 cp_parser_skip_to_end_of_block_or_statement (parser);
30315 token = cp_lexer_peek_token (parser->lexer);
30316 continue;
30318 objc_start_method_definition (is_class_method, sig, attribute,
30319 NULL_TREE);
30321 /* For historical reasons, we accept an optional semicolon. */
30322 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30323 cp_lexer_consume_token (parser->lexer);
30325 ptk = cp_lexer_peek_token (parser->lexer);
30326 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30327 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30329 perform_deferred_access_checks (tf_warning_or_error);
30330 stop_deferring_access_checks ();
30331 meth = cp_parser_function_definition_after_declarator (parser,
30332 false);
30333 pop_deferring_access_checks ();
30334 objc_finish_method_definition (meth);
30337 /* The following case will be removed once @synthesize is
30338 completely implemented. */
30339 else if (token->keyword == RID_AT_PROPERTY)
30340 cp_parser_objc_at_property_declaration (parser);
30341 else if (token->keyword == RID_AT_SYNTHESIZE)
30342 cp_parser_objc_at_synthesize_declaration (parser);
30343 else if (token->keyword == RID_AT_DYNAMIC)
30344 cp_parser_objc_at_dynamic_declaration (parser);
30345 else if (token->keyword == RID_ATTRIBUTE
30346 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30347 warning_at (token->location, OPT_Wattributes,
30348 "prefix attributes are ignored for methods");
30349 else
30350 /* Allow for interspersed non-ObjC++ code. */
30351 cp_parser_objc_interstitial_code (parser);
30353 token = cp_lexer_peek_token (parser->lexer);
30356 if (token->type != CPP_EOF)
30357 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30358 else
30359 cp_parser_error (parser, "expected %<@end%>");
30361 objc_finish_implementation ();
30364 /* Parse Objective-C ivars. */
30366 static void
30367 cp_parser_objc_class_ivars (cp_parser* parser)
30369 cp_token *token = cp_lexer_peek_token (parser->lexer);
30371 if (token->type != CPP_OPEN_BRACE)
30372 return; /* No ivars specified. */
30374 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30375 token = cp_lexer_peek_token (parser->lexer);
30377 while (token->type != CPP_CLOSE_BRACE
30378 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30380 cp_decl_specifier_seq declspecs;
30381 int decl_class_or_enum_p;
30382 tree prefix_attributes;
30384 cp_parser_objc_visibility_spec (parser);
30386 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30387 break;
30389 cp_parser_decl_specifier_seq (parser,
30390 CP_PARSER_FLAGS_OPTIONAL,
30391 &declspecs,
30392 &decl_class_or_enum_p);
30394 /* auto, register, static, extern, mutable. */
30395 if (declspecs.storage_class != sc_none)
30397 cp_parser_error (parser, "invalid type for instance variable");
30398 declspecs.storage_class = sc_none;
30401 /* thread_local. */
30402 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30404 cp_parser_error (parser, "invalid type for instance variable");
30405 declspecs.locations[ds_thread] = 0;
30408 /* typedef. */
30409 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30411 cp_parser_error (parser, "invalid type for instance variable");
30412 declspecs.locations[ds_typedef] = 0;
30415 prefix_attributes = declspecs.attributes;
30416 declspecs.attributes = NULL_TREE;
30418 /* Keep going until we hit the `;' at the end of the
30419 declaration. */
30420 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30422 tree width = NULL_TREE, attributes, first_attribute, decl;
30423 cp_declarator *declarator = NULL;
30424 int ctor_dtor_or_conv_p;
30426 /* Check for a (possibly unnamed) bitfield declaration. */
30427 token = cp_lexer_peek_token (parser->lexer);
30428 if (token->type == CPP_COLON)
30429 goto eat_colon;
30431 if (token->type == CPP_NAME
30432 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30433 == CPP_COLON))
30435 /* Get the name of the bitfield. */
30436 declarator = make_id_declarator (NULL_TREE,
30437 cp_parser_identifier (parser),
30438 sfk_none);
30440 eat_colon:
30441 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30442 /* Get the width of the bitfield. */
30443 width
30444 = cp_parser_constant_expression (parser);
30446 else
30448 /* Parse the declarator. */
30449 declarator
30450 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30451 &ctor_dtor_or_conv_p,
30452 /*parenthesized_p=*/NULL,
30453 /*member_p=*/false,
30454 /*friend_p=*/false);
30457 /* Look for attributes that apply to the ivar. */
30458 attributes = cp_parser_attributes_opt (parser);
30459 /* Remember which attributes are prefix attributes and
30460 which are not. */
30461 first_attribute = attributes;
30462 /* Combine the attributes. */
30463 attributes = attr_chainon (prefix_attributes, attributes);
30465 if (width)
30466 /* Create the bitfield declaration. */
30467 decl = grokbitfield (declarator, &declspecs,
30468 width, NULL_TREE, attributes);
30469 else
30470 decl = grokfield (declarator, &declspecs,
30471 NULL_TREE, /*init_const_expr_p=*/false,
30472 NULL_TREE, attributes);
30474 /* Add the instance variable. */
30475 if (decl != error_mark_node && decl != NULL_TREE)
30476 objc_add_instance_variable (decl);
30478 /* Reset PREFIX_ATTRIBUTES. */
30479 if (attributes != error_mark_node)
30481 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30482 attributes = TREE_CHAIN (attributes);
30483 if (attributes)
30484 TREE_CHAIN (attributes) = NULL_TREE;
30487 token = cp_lexer_peek_token (parser->lexer);
30489 if (token->type == CPP_COMMA)
30491 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30492 continue;
30494 break;
30497 cp_parser_consume_semicolon_at_end_of_statement (parser);
30498 token = cp_lexer_peek_token (parser->lexer);
30501 if (token->keyword == RID_AT_END)
30502 cp_parser_error (parser, "expected %<}%>");
30504 /* Do not consume the RID_AT_END, so it will be read again as terminating
30505 the @interface of @implementation. */
30506 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30507 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30509 /* For historical reasons, we accept an optional semicolon. */
30510 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30511 cp_lexer_consume_token (parser->lexer);
30514 /* Parse an Objective-C protocol declaration. */
30516 static void
30517 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30519 tree proto, protorefs;
30520 cp_token *tok;
30522 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30525 tok = cp_lexer_peek_token (parser->lexer);
30526 error_at (tok->location, "identifier expected after %<@protocol%>");
30527 cp_parser_consume_semicolon_at_end_of_statement (parser);
30528 return;
30531 /* See if we have a forward declaration or a definition. */
30532 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30534 /* Try a forward declaration first. */
30535 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30537 while (true)
30539 tree id;
30541 id = cp_parser_identifier (parser);
30542 if (id == error_mark_node)
30543 break;
30545 objc_declare_protocol (id, attributes);
30547 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30548 cp_lexer_consume_token (parser->lexer);
30549 else
30550 break;
30552 cp_parser_consume_semicolon_at_end_of_statement (parser);
30555 /* Ok, we got a full-fledged definition (or at least should). */
30556 else
30558 proto = cp_parser_identifier (parser);
30559 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30560 objc_start_protocol (proto, protorefs, attributes);
30561 cp_parser_objc_method_prototype_list (parser);
30565 /* Parse an Objective-C superclass or category. */
30567 static void
30568 cp_parser_objc_superclass_or_category (cp_parser *parser,
30569 bool iface_p,
30570 tree *super,
30571 tree *categ, bool *is_class_extension)
30573 cp_token *next = cp_lexer_peek_token (parser->lexer);
30575 *super = *categ = NULL_TREE;
30576 *is_class_extension = false;
30577 if (next->type == CPP_COLON)
30579 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30580 *super = cp_parser_identifier (parser);
30582 else if (next->type == CPP_OPEN_PAREN)
30584 matching_parens parens;
30585 parens.consume_open (parser); /* Eat '('. */
30587 /* If there is no category name, and this is an @interface, we
30588 have a class extension. */
30589 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30591 *categ = NULL_TREE;
30592 *is_class_extension = true;
30594 else
30595 *categ = cp_parser_identifier (parser);
30597 parens.require_close (parser);
30601 /* Parse an Objective-C class interface. */
30603 static void
30604 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30606 tree name, super, categ, protos;
30607 bool is_class_extension;
30609 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30610 name = cp_parser_identifier (parser);
30611 if (name == error_mark_node)
30613 /* It's hard to recover because even if valid @interface stuff
30614 is to follow, we can't compile it (or validate it) if we
30615 don't even know which class it refers to. Let's assume this
30616 was a stray '@interface' token in the stream and skip it.
30618 return;
30620 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30621 &is_class_extension);
30622 protos = cp_parser_objc_protocol_refs_opt (parser);
30624 /* We have either a class or a category on our hands. */
30625 if (categ || is_class_extension)
30626 objc_start_category_interface (name, categ, protos, attributes);
30627 else
30629 objc_start_class_interface (name, super, protos, attributes);
30630 /* Handle instance variable declarations, if any. */
30631 cp_parser_objc_class_ivars (parser);
30632 objc_continue_interface ();
30635 cp_parser_objc_method_prototype_list (parser);
30638 /* Parse an Objective-C class implementation. */
30640 static void
30641 cp_parser_objc_class_implementation (cp_parser* parser)
30643 tree name, super, categ;
30644 bool is_class_extension;
30646 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30647 name = cp_parser_identifier (parser);
30648 if (name == error_mark_node)
30650 /* It's hard to recover because even if valid @implementation
30651 stuff is to follow, we can't compile it (or validate it) if
30652 we don't even know which class it refers to. Let's assume
30653 this was a stray '@implementation' token in the stream and
30654 skip it.
30656 return;
30658 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30659 &is_class_extension);
30661 /* We have either a class or a category on our hands. */
30662 if (categ)
30663 objc_start_category_implementation (name, categ);
30664 else
30666 objc_start_class_implementation (name, super);
30667 /* Handle instance variable declarations, if any. */
30668 cp_parser_objc_class_ivars (parser);
30669 objc_continue_implementation ();
30672 cp_parser_objc_method_definition_list (parser);
30675 /* Consume the @end token and finish off the implementation. */
30677 static void
30678 cp_parser_objc_end_implementation (cp_parser* parser)
30680 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30681 objc_finish_implementation ();
30684 /* Parse an Objective-C declaration. */
30686 static void
30687 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30689 /* Try to figure out what kind of declaration is present. */
30690 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30692 if (attributes)
30693 switch (kwd->keyword)
30695 case RID_AT_ALIAS:
30696 case RID_AT_CLASS:
30697 case RID_AT_END:
30698 error_at (kwd->location, "attributes may not be specified before"
30699 " the %<@%D%> Objective-C++ keyword",
30700 kwd->u.value);
30701 attributes = NULL;
30702 break;
30703 case RID_AT_IMPLEMENTATION:
30704 warning_at (kwd->location, OPT_Wattributes,
30705 "prefix attributes are ignored before %<@%D%>",
30706 kwd->u.value);
30707 attributes = NULL;
30708 default:
30709 break;
30712 switch (kwd->keyword)
30714 case RID_AT_ALIAS:
30715 cp_parser_objc_alias_declaration (parser);
30716 break;
30717 case RID_AT_CLASS:
30718 cp_parser_objc_class_declaration (parser);
30719 break;
30720 case RID_AT_PROTOCOL:
30721 cp_parser_objc_protocol_declaration (parser, attributes);
30722 break;
30723 case RID_AT_INTERFACE:
30724 cp_parser_objc_class_interface (parser, attributes);
30725 break;
30726 case RID_AT_IMPLEMENTATION:
30727 cp_parser_objc_class_implementation (parser);
30728 break;
30729 case RID_AT_END:
30730 cp_parser_objc_end_implementation (parser);
30731 break;
30732 default:
30733 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30734 kwd->u.value);
30735 cp_parser_skip_to_end_of_block_or_statement (parser);
30739 /* Parse an Objective-C try-catch-finally statement.
30741 objc-try-catch-finally-stmt:
30742 @try compound-statement objc-catch-clause-seq [opt]
30743 objc-finally-clause [opt]
30745 objc-catch-clause-seq:
30746 objc-catch-clause objc-catch-clause-seq [opt]
30748 objc-catch-clause:
30749 @catch ( objc-exception-declaration ) compound-statement
30751 objc-finally-clause:
30752 @finally compound-statement
30754 objc-exception-declaration:
30755 parameter-declaration
30756 '...'
30758 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30760 Returns NULL_TREE.
30762 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30763 for C. Keep them in sync. */
30765 static tree
30766 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30768 location_t location;
30769 tree stmt;
30771 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30772 location = cp_lexer_peek_token (parser->lexer)->location;
30773 objc_maybe_warn_exceptions (location);
30774 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30775 node, lest it get absorbed into the surrounding block. */
30776 stmt = push_stmt_list ();
30777 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30778 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30780 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30782 cp_parameter_declarator *parm;
30783 tree parameter_declaration = error_mark_node;
30784 bool seen_open_paren = false;
30785 matching_parens parens;
30787 cp_lexer_consume_token (parser->lexer);
30788 if (parens.require_open (parser))
30789 seen_open_paren = true;
30790 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30792 /* We have "@catch (...)" (where the '...' are literally
30793 what is in the code). Skip the '...'.
30794 parameter_declaration is set to NULL_TREE, and
30795 objc_being_catch_clauses() knows that that means
30796 '...'. */
30797 cp_lexer_consume_token (parser->lexer);
30798 parameter_declaration = NULL_TREE;
30800 else
30802 /* We have "@catch (NSException *exception)" or something
30803 like that. Parse the parameter declaration. */
30804 parm = cp_parser_parameter_declaration (parser, false, NULL);
30805 if (parm == NULL)
30806 parameter_declaration = error_mark_node;
30807 else
30808 parameter_declaration = grokdeclarator (parm->declarator,
30809 &parm->decl_specifiers,
30810 PARM, /*initialized=*/0,
30811 /*attrlist=*/NULL);
30813 if (seen_open_paren)
30814 parens.require_close (parser);
30815 else
30817 /* If there was no open parenthesis, we are recovering from
30818 an error, and we are trying to figure out what mistake
30819 the user has made. */
30821 /* If there is an immediate closing parenthesis, the user
30822 probably forgot the opening one (ie, they typed "@catch
30823 NSException *e)". Parse the closing parenthesis and keep
30824 going. */
30825 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30826 cp_lexer_consume_token (parser->lexer);
30828 /* If these is no immediate closing parenthesis, the user
30829 probably doesn't know that parenthesis are required at
30830 all (ie, they typed "@catch NSException *e"). So, just
30831 forget about the closing parenthesis and keep going. */
30833 objc_begin_catch_clause (parameter_declaration);
30834 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30835 objc_finish_catch_clause ();
30837 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30839 cp_lexer_consume_token (parser->lexer);
30840 location = cp_lexer_peek_token (parser->lexer)->location;
30841 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30842 node, lest it get absorbed into the surrounding block. */
30843 stmt = push_stmt_list ();
30844 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30845 objc_build_finally_clause (location, pop_stmt_list (stmt));
30848 return objc_finish_try_stmt ();
30851 /* Parse an Objective-C synchronized statement.
30853 objc-synchronized-stmt:
30854 @synchronized ( expression ) compound-statement
30856 Returns NULL_TREE. */
30858 static tree
30859 cp_parser_objc_synchronized_statement (cp_parser *parser)
30861 location_t location;
30862 tree lock, stmt;
30864 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30866 location = cp_lexer_peek_token (parser->lexer)->location;
30867 objc_maybe_warn_exceptions (location);
30868 matching_parens parens;
30869 parens.require_open (parser);
30870 lock = cp_parser_expression (parser);
30871 parens.require_close (parser);
30873 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30874 node, lest it get absorbed into the surrounding block. */
30875 stmt = push_stmt_list ();
30876 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30878 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30881 /* Parse an Objective-C throw statement.
30883 objc-throw-stmt:
30884 @throw assignment-expression [opt] ;
30886 Returns a constructed '@throw' statement. */
30888 static tree
30889 cp_parser_objc_throw_statement (cp_parser *parser)
30891 tree expr = NULL_TREE;
30892 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30894 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30897 expr = cp_parser_expression (parser);
30899 cp_parser_consume_semicolon_at_end_of_statement (parser);
30901 return objc_build_throw_stmt (loc, expr);
30904 /* Parse an Objective-C statement. */
30906 static tree
30907 cp_parser_objc_statement (cp_parser * parser)
30909 /* Try to figure out what kind of declaration is present. */
30910 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30912 switch (kwd->keyword)
30914 case RID_AT_TRY:
30915 return cp_parser_objc_try_catch_finally_statement (parser);
30916 case RID_AT_SYNCHRONIZED:
30917 return cp_parser_objc_synchronized_statement (parser);
30918 case RID_AT_THROW:
30919 return cp_parser_objc_throw_statement (parser);
30920 default:
30921 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30922 kwd->u.value);
30923 cp_parser_skip_to_end_of_block_or_statement (parser);
30926 return error_mark_node;
30929 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30930 look ahead to see if an objc keyword follows the attributes. This
30931 is to detect the use of prefix attributes on ObjC @interface and
30932 @protocol. */
30934 static bool
30935 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30937 cp_lexer_save_tokens (parser->lexer);
30938 *attrib = cp_parser_attributes_opt (parser);
30939 gcc_assert (*attrib);
30940 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30942 cp_lexer_commit_tokens (parser->lexer);
30943 return true;
30945 cp_lexer_rollback_tokens (parser->lexer);
30946 return false;
30949 /* This routine is a minimal replacement for
30950 c_parser_struct_declaration () used when parsing the list of
30951 types/names or ObjC++ properties. For example, when parsing the
30952 code
30954 @property (readonly) int a, b, c;
30956 this function is responsible for parsing "int a, int b, int c" and
30957 returning the declarations as CHAIN of DECLs.
30959 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30960 similar parsing. */
30961 static tree
30962 cp_parser_objc_struct_declaration (cp_parser *parser)
30964 tree decls = NULL_TREE;
30965 cp_decl_specifier_seq declspecs;
30966 int decl_class_or_enum_p;
30967 tree prefix_attributes;
30969 cp_parser_decl_specifier_seq (parser,
30970 CP_PARSER_FLAGS_NONE,
30971 &declspecs,
30972 &decl_class_or_enum_p);
30974 if (declspecs.type == error_mark_node)
30975 return error_mark_node;
30977 /* auto, register, static, extern, mutable. */
30978 if (declspecs.storage_class != sc_none)
30980 cp_parser_error (parser, "invalid type for property");
30981 declspecs.storage_class = sc_none;
30984 /* thread_local. */
30985 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30987 cp_parser_error (parser, "invalid type for property");
30988 declspecs.locations[ds_thread] = 0;
30991 /* typedef. */
30992 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30994 cp_parser_error (parser, "invalid type for property");
30995 declspecs.locations[ds_typedef] = 0;
30998 prefix_attributes = declspecs.attributes;
30999 declspecs.attributes = NULL_TREE;
31001 /* Keep going until we hit the `;' at the end of the declaration. */
31002 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31004 tree attributes, first_attribute, decl;
31005 cp_declarator *declarator;
31006 cp_token *token;
31008 /* Parse the declarator. */
31009 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31010 NULL, NULL, false, false);
31012 /* Look for attributes that apply to the ivar. */
31013 attributes = cp_parser_attributes_opt (parser);
31014 /* Remember which attributes are prefix attributes and
31015 which are not. */
31016 first_attribute = attributes;
31017 /* Combine the attributes. */
31018 attributes = attr_chainon (prefix_attributes, attributes);
31020 decl = grokfield (declarator, &declspecs,
31021 NULL_TREE, /*init_const_expr_p=*/false,
31022 NULL_TREE, attributes);
31024 if (decl == error_mark_node || decl == NULL_TREE)
31025 return error_mark_node;
31027 /* Reset PREFIX_ATTRIBUTES. */
31028 if (attributes != error_mark_node)
31030 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31031 attributes = TREE_CHAIN (attributes);
31032 if (attributes)
31033 TREE_CHAIN (attributes) = NULL_TREE;
31036 DECL_CHAIN (decl) = decls;
31037 decls = decl;
31039 token = cp_lexer_peek_token (parser->lexer);
31040 if (token->type == CPP_COMMA)
31042 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31043 continue;
31045 else
31046 break;
31048 return decls;
31051 /* Parse an Objective-C @property declaration. The syntax is:
31053 objc-property-declaration:
31054 '@property' objc-property-attributes[opt] struct-declaration ;
31056 objc-property-attributes:
31057 '(' objc-property-attribute-list ')'
31059 objc-property-attribute-list:
31060 objc-property-attribute
31061 objc-property-attribute-list, objc-property-attribute
31063 objc-property-attribute
31064 'getter' = identifier
31065 'setter' = identifier
31066 'readonly'
31067 'readwrite'
31068 'assign'
31069 'retain'
31070 'copy'
31071 'nonatomic'
31073 For example:
31074 @property NSString *name;
31075 @property (readonly) id object;
31076 @property (retain, nonatomic, getter=getTheName) id name;
31077 @property int a, b, c;
31079 PS: This function is identical to
31080 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31081 static void
31082 cp_parser_objc_at_property_declaration (cp_parser *parser)
31084 /* The following variables hold the attributes of the properties as
31085 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31086 seen. When we see an attribute, we set them to 'true' (if they
31087 are boolean properties) or to the identifier (if they have an
31088 argument, ie, for getter and setter). Note that here we only
31089 parse the list of attributes, check the syntax and accumulate the
31090 attributes that we find. objc_add_property_declaration() will
31091 then process the information. */
31092 bool property_assign = false;
31093 bool property_copy = false;
31094 tree property_getter_ident = NULL_TREE;
31095 bool property_nonatomic = false;
31096 bool property_readonly = false;
31097 bool property_readwrite = false;
31098 bool property_retain = false;
31099 tree property_setter_ident = NULL_TREE;
31101 /* 'properties' is the list of properties that we read. Usually a
31102 single one, but maybe more (eg, in "@property int a, b, c;" there
31103 are three). */
31104 tree properties;
31105 location_t loc;
31107 loc = cp_lexer_peek_token (parser->lexer)->location;
31109 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31111 /* Parse the optional attribute list... */
31112 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31114 /* Eat the '('. */
31115 matching_parens parens;
31116 parens.consume_open (parser);
31118 while (true)
31120 bool syntax_error = false;
31121 cp_token *token = cp_lexer_peek_token (parser->lexer);
31122 enum rid keyword;
31124 if (token->type != CPP_NAME)
31126 cp_parser_error (parser, "expected identifier");
31127 break;
31129 keyword = C_RID_CODE (token->u.value);
31130 cp_lexer_consume_token (parser->lexer);
31131 switch (keyword)
31133 case RID_ASSIGN: property_assign = true; break;
31134 case RID_COPY: property_copy = true; break;
31135 case RID_NONATOMIC: property_nonatomic = true; break;
31136 case RID_READONLY: property_readonly = true; break;
31137 case RID_READWRITE: property_readwrite = true; break;
31138 case RID_RETAIN: property_retain = true; break;
31140 case RID_GETTER:
31141 case RID_SETTER:
31142 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31144 if (keyword == RID_GETTER)
31145 cp_parser_error (parser,
31146 "missing %<=%> (after %<getter%> attribute)");
31147 else
31148 cp_parser_error (parser,
31149 "missing %<=%> (after %<setter%> attribute)");
31150 syntax_error = true;
31151 break;
31153 cp_lexer_consume_token (parser->lexer); /* eat the = */
31154 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31156 cp_parser_error (parser, "expected identifier");
31157 syntax_error = true;
31158 break;
31160 if (keyword == RID_SETTER)
31162 if (property_setter_ident != NULL_TREE)
31164 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31165 cp_lexer_consume_token (parser->lexer);
31167 else
31168 property_setter_ident = cp_parser_objc_selector (parser);
31169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31170 cp_parser_error (parser, "setter name must terminate with %<:%>");
31171 else
31172 cp_lexer_consume_token (parser->lexer);
31174 else
31176 if (property_getter_ident != NULL_TREE)
31178 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31179 cp_lexer_consume_token (parser->lexer);
31181 else
31182 property_getter_ident = cp_parser_objc_selector (parser);
31184 break;
31185 default:
31186 cp_parser_error (parser, "unknown property attribute");
31187 syntax_error = true;
31188 break;
31191 if (syntax_error)
31192 break;
31194 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31195 cp_lexer_consume_token (parser->lexer);
31196 else
31197 break;
31200 /* FIXME: "@property (setter, assign);" will generate a spurious
31201 "error: expected ‘)’ before ‘,’ token". This is because
31202 cp_parser_require, unlike the C counterpart, will produce an
31203 error even if we are in error recovery. */
31204 if (!parens.require_close (parser))
31206 cp_parser_skip_to_closing_parenthesis (parser,
31207 /*recovering=*/true,
31208 /*or_comma=*/false,
31209 /*consume_paren=*/true);
31213 /* ... and the property declaration(s). */
31214 properties = cp_parser_objc_struct_declaration (parser);
31216 if (properties == error_mark_node)
31218 cp_parser_skip_to_end_of_statement (parser);
31219 /* If the next token is now a `;', consume it. */
31220 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31221 cp_lexer_consume_token (parser->lexer);
31222 return;
31225 if (properties == NULL_TREE)
31226 cp_parser_error (parser, "expected identifier");
31227 else
31229 /* Comma-separated properties are chained together in
31230 reverse order; add them one by one. */
31231 properties = nreverse (properties);
31233 for (; properties; properties = TREE_CHAIN (properties))
31234 objc_add_property_declaration (loc, copy_node (properties),
31235 property_readonly, property_readwrite,
31236 property_assign, property_retain,
31237 property_copy, property_nonatomic,
31238 property_getter_ident, property_setter_ident);
31241 cp_parser_consume_semicolon_at_end_of_statement (parser);
31244 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31246 objc-synthesize-declaration:
31247 @synthesize objc-synthesize-identifier-list ;
31249 objc-synthesize-identifier-list:
31250 objc-synthesize-identifier
31251 objc-synthesize-identifier-list, objc-synthesize-identifier
31253 objc-synthesize-identifier
31254 identifier
31255 identifier = identifier
31257 For example:
31258 @synthesize MyProperty;
31259 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31261 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31262 for C. Keep them in sync.
31264 static void
31265 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31267 tree list = NULL_TREE;
31268 location_t loc;
31269 loc = cp_lexer_peek_token (parser->lexer)->location;
31271 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31272 while (true)
31274 tree property, ivar;
31275 property = cp_parser_identifier (parser);
31276 if (property == error_mark_node)
31278 cp_parser_consume_semicolon_at_end_of_statement (parser);
31279 return;
31281 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31283 cp_lexer_consume_token (parser->lexer);
31284 ivar = cp_parser_identifier (parser);
31285 if (ivar == error_mark_node)
31287 cp_parser_consume_semicolon_at_end_of_statement (parser);
31288 return;
31291 else
31292 ivar = NULL_TREE;
31293 list = chainon (list, build_tree_list (ivar, property));
31294 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31295 cp_lexer_consume_token (parser->lexer);
31296 else
31297 break;
31299 cp_parser_consume_semicolon_at_end_of_statement (parser);
31300 objc_add_synthesize_declaration (loc, list);
31303 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31305 objc-dynamic-declaration:
31306 @dynamic identifier-list ;
31308 For example:
31309 @dynamic MyProperty;
31310 @dynamic MyProperty, AnotherProperty;
31312 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31313 for C. Keep them in sync.
31315 static void
31316 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31318 tree list = NULL_TREE;
31319 location_t loc;
31320 loc = cp_lexer_peek_token (parser->lexer)->location;
31322 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31323 while (true)
31325 tree property;
31326 property = cp_parser_identifier (parser);
31327 if (property == error_mark_node)
31329 cp_parser_consume_semicolon_at_end_of_statement (parser);
31330 return;
31332 list = chainon (list, build_tree_list (NULL, property));
31333 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31334 cp_lexer_consume_token (parser->lexer);
31335 else
31336 break;
31338 cp_parser_consume_semicolon_at_end_of_statement (parser);
31339 objc_add_dynamic_declaration (loc, list);
31343 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31345 /* Returns name of the next clause.
31346 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31347 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31348 returned and the token is consumed. */
31350 static pragma_omp_clause
31351 cp_parser_omp_clause_name (cp_parser *parser)
31353 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31355 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31356 result = PRAGMA_OACC_CLAUSE_AUTO;
31357 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31358 result = PRAGMA_OMP_CLAUSE_IF;
31359 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31360 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31361 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31362 result = PRAGMA_OACC_CLAUSE_DELETE;
31363 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31364 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31365 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31366 result = PRAGMA_OMP_CLAUSE_FOR;
31367 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31369 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31370 const char *p = IDENTIFIER_POINTER (id);
31372 switch (p[0])
31374 case 'a':
31375 if (!strcmp ("aligned", p))
31376 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31377 else if (!strcmp ("async", p))
31378 result = PRAGMA_OACC_CLAUSE_ASYNC;
31379 break;
31380 case 'c':
31381 if (!strcmp ("collapse", p))
31382 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31383 else if (!strcmp ("copy", p))
31384 result = PRAGMA_OACC_CLAUSE_COPY;
31385 else if (!strcmp ("copyin", p))
31386 result = PRAGMA_OMP_CLAUSE_COPYIN;
31387 else if (!strcmp ("copyout", p))
31388 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31389 else if (!strcmp ("copyprivate", p))
31390 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31391 else if (!strcmp ("create", p))
31392 result = PRAGMA_OACC_CLAUSE_CREATE;
31393 break;
31394 case 'd':
31395 if (!strcmp ("defaultmap", p))
31396 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31397 else if (!strcmp ("depend", p))
31398 result = PRAGMA_OMP_CLAUSE_DEPEND;
31399 else if (!strcmp ("device", p))
31400 result = PRAGMA_OMP_CLAUSE_DEVICE;
31401 else if (!strcmp ("deviceptr", p))
31402 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31403 else if (!strcmp ("device_resident", p))
31404 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31405 else if (!strcmp ("dist_schedule", p))
31406 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31407 break;
31408 case 'f':
31409 if (!strcmp ("final", p))
31410 result = PRAGMA_OMP_CLAUSE_FINAL;
31411 else if (!strcmp ("finalize", p))
31412 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31413 else if (!strcmp ("firstprivate", p))
31414 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31415 else if (!strcmp ("from", p))
31416 result = PRAGMA_OMP_CLAUSE_FROM;
31417 break;
31418 case 'g':
31419 if (!strcmp ("gang", p))
31420 result = PRAGMA_OACC_CLAUSE_GANG;
31421 else if (!strcmp ("grainsize", p))
31422 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31423 break;
31424 case 'h':
31425 if (!strcmp ("hint", p))
31426 result = PRAGMA_OMP_CLAUSE_HINT;
31427 else if (!strcmp ("host", p))
31428 result = PRAGMA_OACC_CLAUSE_HOST;
31429 break;
31430 case 'i':
31431 if (!strcmp ("if_present", p))
31432 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31433 else if (!strcmp ("inbranch", p))
31434 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31435 else if (!strcmp ("independent", p))
31436 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31437 else if (!strcmp ("is_device_ptr", p))
31438 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31439 break;
31440 case 'l':
31441 if (!strcmp ("lastprivate", p))
31442 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31443 else if (!strcmp ("linear", p))
31444 result = PRAGMA_OMP_CLAUSE_LINEAR;
31445 else if (!strcmp ("link", p))
31446 result = PRAGMA_OMP_CLAUSE_LINK;
31447 break;
31448 case 'm':
31449 if (!strcmp ("map", p))
31450 result = PRAGMA_OMP_CLAUSE_MAP;
31451 else if (!strcmp ("mergeable", p))
31452 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31453 break;
31454 case 'n':
31455 if (!strcmp ("nogroup", p))
31456 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31457 else if (!strcmp ("notinbranch", p))
31458 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31459 else if (!strcmp ("nowait", p))
31460 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31461 else if (!strcmp ("num_gangs", p))
31462 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31463 else if (!strcmp ("num_tasks", p))
31464 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31465 else if (!strcmp ("num_teams", p))
31466 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31467 else if (!strcmp ("num_threads", p))
31468 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31469 else if (!strcmp ("num_workers", p))
31470 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31471 break;
31472 case 'o':
31473 if (!strcmp ("ordered", p))
31474 result = PRAGMA_OMP_CLAUSE_ORDERED;
31475 break;
31476 case 'p':
31477 if (!strcmp ("parallel", p))
31478 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31479 else if (!strcmp ("present", p))
31480 result = PRAGMA_OACC_CLAUSE_PRESENT;
31481 else if (!strcmp ("present_or_copy", p)
31482 || !strcmp ("pcopy", p))
31483 result = PRAGMA_OACC_CLAUSE_COPY;
31484 else if (!strcmp ("present_or_copyin", p)
31485 || !strcmp ("pcopyin", p))
31486 result = PRAGMA_OACC_CLAUSE_COPYIN;
31487 else if (!strcmp ("present_or_copyout", p)
31488 || !strcmp ("pcopyout", p))
31489 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31490 else if (!strcmp ("present_or_create", p)
31491 || !strcmp ("pcreate", p))
31492 result = PRAGMA_OACC_CLAUSE_CREATE;
31493 else if (!strcmp ("priority", p))
31494 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31495 else if (!strcmp ("proc_bind", p))
31496 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31497 break;
31498 case 'r':
31499 if (!strcmp ("reduction", p))
31500 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31501 break;
31502 case 's':
31503 if (!strcmp ("safelen", p))
31504 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31505 else if (!strcmp ("schedule", p))
31506 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31507 else if (!strcmp ("sections", p))
31508 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31509 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31510 result = PRAGMA_OACC_CLAUSE_HOST;
31511 else if (!strcmp ("seq", p))
31512 result = PRAGMA_OACC_CLAUSE_SEQ;
31513 else if (!strcmp ("shared", p))
31514 result = PRAGMA_OMP_CLAUSE_SHARED;
31515 else if (!strcmp ("simd", p))
31516 result = PRAGMA_OMP_CLAUSE_SIMD;
31517 else if (!strcmp ("simdlen", p))
31518 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31519 break;
31520 case 't':
31521 if (!strcmp ("taskgroup", p))
31522 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31523 else if (!strcmp ("thread_limit", p))
31524 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31525 else if (!strcmp ("threads", p))
31526 result = PRAGMA_OMP_CLAUSE_THREADS;
31527 else if (!strcmp ("tile", p))
31528 result = PRAGMA_OACC_CLAUSE_TILE;
31529 else if (!strcmp ("to", p))
31530 result = PRAGMA_OMP_CLAUSE_TO;
31531 break;
31532 case 'u':
31533 if (!strcmp ("uniform", p))
31534 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31535 else if (!strcmp ("untied", p))
31536 result = PRAGMA_OMP_CLAUSE_UNTIED;
31537 else if (!strcmp ("use_device", p))
31538 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31539 else if (!strcmp ("use_device_ptr", p))
31540 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31541 break;
31542 case 'v':
31543 if (!strcmp ("vector", p))
31544 result = PRAGMA_OACC_CLAUSE_VECTOR;
31545 else if (!strcmp ("vector_length", p))
31546 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31547 break;
31548 case 'w':
31549 if (!strcmp ("wait", p))
31550 result = PRAGMA_OACC_CLAUSE_WAIT;
31551 else if (!strcmp ("worker", p))
31552 result = PRAGMA_OACC_CLAUSE_WORKER;
31553 break;
31557 if (result != PRAGMA_OMP_CLAUSE_NONE)
31558 cp_lexer_consume_token (parser->lexer);
31560 return result;
31563 /* Validate that a clause of the given type does not already exist. */
31565 static void
31566 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31567 const char *name, location_t location)
31569 tree c;
31571 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31572 if (OMP_CLAUSE_CODE (c) == code)
31574 error_at (location, "too many %qs clauses", name);
31575 break;
31579 /* OpenMP 2.5:
31580 variable-list:
31581 identifier
31582 variable-list , identifier
31584 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31585 colon). An opening parenthesis will have been consumed by the caller.
31587 If KIND is nonzero, create the appropriate node and install the decl
31588 in OMP_CLAUSE_DECL and add the node to the head of the list.
31590 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31591 return the list created.
31593 COLON can be NULL if only closing parenthesis should end the list,
31594 or pointer to bool which will receive false if the list is terminated
31595 by closing parenthesis or true if the list is terminated by colon. */
31597 static tree
31598 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31599 tree list, bool *colon)
31601 cp_token *token;
31602 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31603 if (colon)
31605 parser->colon_corrects_to_scope_p = false;
31606 *colon = false;
31608 while (1)
31610 tree name, decl;
31612 token = cp_lexer_peek_token (parser->lexer);
31613 if (kind != 0
31614 && current_class_ptr
31615 && cp_parser_is_keyword (token, RID_THIS))
31617 decl = finish_this_expr ();
31618 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31619 || CONVERT_EXPR_P (decl))
31620 decl = TREE_OPERAND (decl, 0);
31621 cp_lexer_consume_token (parser->lexer);
31623 else
31625 name = cp_parser_id_expression (parser, /*template_p=*/false,
31626 /*check_dependency_p=*/true,
31627 /*template_p=*/NULL,
31628 /*declarator_p=*/false,
31629 /*optional_p=*/false);
31630 if (name == error_mark_node)
31631 goto skip_comma;
31633 if (identifier_p (name))
31634 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31635 else
31636 decl = name;
31637 if (decl == error_mark_node)
31638 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31639 token->location);
31641 if (decl == error_mark_node)
31643 else if (kind != 0)
31645 switch (kind)
31647 case OMP_CLAUSE__CACHE_:
31648 /* The OpenACC cache directive explicitly only allows "array
31649 elements or subarrays". */
31650 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31652 error_at (token->location, "expected %<[%>");
31653 decl = error_mark_node;
31654 break;
31656 /* FALLTHROUGH. */
31657 case OMP_CLAUSE_MAP:
31658 case OMP_CLAUSE_FROM:
31659 case OMP_CLAUSE_TO:
31660 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31662 location_t loc
31663 = cp_lexer_peek_token (parser->lexer)->location;
31664 cp_id_kind idk = CP_ID_KIND_NONE;
31665 cp_lexer_consume_token (parser->lexer);
31666 decl = convert_from_reference (decl);
31667 decl
31668 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31669 decl, false,
31670 &idk, loc);
31672 /* FALLTHROUGH. */
31673 case OMP_CLAUSE_DEPEND:
31674 case OMP_CLAUSE_REDUCTION:
31675 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31677 tree low_bound = NULL_TREE, length = NULL_TREE;
31679 parser->colon_corrects_to_scope_p = false;
31680 cp_lexer_consume_token (parser->lexer);
31681 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31682 low_bound = cp_parser_expression (parser);
31683 if (!colon)
31684 parser->colon_corrects_to_scope_p
31685 = saved_colon_corrects_to_scope_p;
31686 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31687 length = integer_one_node;
31688 else
31690 /* Look for `:'. */
31691 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31692 goto skip_comma;
31693 if (!cp_lexer_next_token_is (parser->lexer,
31694 CPP_CLOSE_SQUARE))
31695 length = cp_parser_expression (parser);
31697 /* Look for the closing `]'. */
31698 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31699 RT_CLOSE_SQUARE))
31700 goto skip_comma;
31702 decl = tree_cons (low_bound, length, decl);
31704 break;
31705 default:
31706 break;
31709 tree u = build_omp_clause (token->location, kind);
31710 OMP_CLAUSE_DECL (u) = decl;
31711 OMP_CLAUSE_CHAIN (u) = list;
31712 list = u;
31714 else
31715 list = tree_cons (decl, NULL_TREE, list);
31717 get_comma:
31718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31719 break;
31720 cp_lexer_consume_token (parser->lexer);
31723 if (colon)
31724 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31726 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31728 *colon = true;
31729 cp_parser_require (parser, CPP_COLON, RT_COLON);
31730 return list;
31733 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31735 int ending;
31737 /* Try to resync to an unnested comma. Copied from
31738 cp_parser_parenthesized_expression_list. */
31739 skip_comma:
31740 if (colon)
31741 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31742 ending = cp_parser_skip_to_closing_parenthesis (parser,
31743 /*recovering=*/true,
31744 /*or_comma=*/true,
31745 /*consume_paren=*/true);
31746 if (ending < 0)
31747 goto get_comma;
31750 return list;
31753 /* Similarly, but expect leading and trailing parenthesis. This is a very
31754 common case for omp clauses. */
31756 static tree
31757 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31759 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31760 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31761 return list;
31764 /* OpenACC 2.0:
31765 copy ( variable-list )
31766 copyin ( variable-list )
31767 copyout ( variable-list )
31768 create ( variable-list )
31769 delete ( variable-list )
31770 present ( variable-list ) */
31772 static tree
31773 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31774 tree list)
31776 enum gomp_map_kind kind;
31777 switch (c_kind)
31779 case PRAGMA_OACC_CLAUSE_COPY:
31780 kind = GOMP_MAP_TOFROM;
31781 break;
31782 case PRAGMA_OACC_CLAUSE_COPYIN:
31783 kind = GOMP_MAP_TO;
31784 break;
31785 case PRAGMA_OACC_CLAUSE_COPYOUT:
31786 kind = GOMP_MAP_FROM;
31787 break;
31788 case PRAGMA_OACC_CLAUSE_CREATE:
31789 kind = GOMP_MAP_ALLOC;
31790 break;
31791 case PRAGMA_OACC_CLAUSE_DELETE:
31792 kind = GOMP_MAP_RELEASE;
31793 break;
31794 case PRAGMA_OACC_CLAUSE_DEVICE:
31795 kind = GOMP_MAP_FORCE_TO;
31796 break;
31797 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31798 kind = GOMP_MAP_DEVICE_RESIDENT;
31799 break;
31800 case PRAGMA_OACC_CLAUSE_HOST:
31801 kind = GOMP_MAP_FORCE_FROM;
31802 break;
31803 case PRAGMA_OACC_CLAUSE_LINK:
31804 kind = GOMP_MAP_LINK;
31805 break;
31806 case PRAGMA_OACC_CLAUSE_PRESENT:
31807 kind = GOMP_MAP_FORCE_PRESENT;
31808 break;
31809 default:
31810 gcc_unreachable ();
31812 tree nl, c;
31813 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31815 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31816 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31818 return nl;
31821 /* OpenACC 2.0:
31822 deviceptr ( variable-list ) */
31824 static tree
31825 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31827 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31828 tree vars, t;
31830 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31831 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31832 variable-list must only allow for pointer variables. */
31833 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31834 for (t = vars; t; t = TREE_CHAIN (t))
31836 tree v = TREE_PURPOSE (t);
31837 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31838 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31839 OMP_CLAUSE_DECL (u) = v;
31840 OMP_CLAUSE_CHAIN (u) = list;
31841 list = u;
31844 return list;
31847 /* OpenACC 2.5:
31848 auto
31849 finalize
31850 independent
31851 nohost
31852 seq */
31854 static tree
31855 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31856 enum omp_clause_code code,
31857 tree list, location_t location)
31859 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31860 tree c = build_omp_clause (location, code);
31861 OMP_CLAUSE_CHAIN (c) = list;
31862 return c;
31865 /* OpenACC:
31866 num_gangs ( expression )
31867 num_workers ( expression )
31868 vector_length ( expression ) */
31870 static tree
31871 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31872 const char *str, tree list)
31874 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31876 matching_parens parens;
31877 if (!parens.require_open (parser))
31878 return list;
31880 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31882 if (t == error_mark_node
31883 || !parens.require_close (parser))
31885 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31886 /*or_comma=*/false,
31887 /*consume_paren=*/true);
31888 return list;
31891 check_no_duplicate_clause (list, code, str, loc);
31893 tree c = build_omp_clause (loc, code);
31894 OMP_CLAUSE_OPERAND (c, 0) = t;
31895 OMP_CLAUSE_CHAIN (c) = list;
31896 return c;
31899 /* OpenACC:
31901 gang [( gang-arg-list )]
31902 worker [( [num:] int-expr )]
31903 vector [( [length:] int-expr )]
31905 where gang-arg is one of:
31907 [num:] int-expr
31908 static: size-expr
31910 and size-expr may be:
31913 int-expr
31916 static tree
31917 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31918 const char *str, tree list)
31920 const char *id = "num";
31921 cp_lexer *lexer = parser->lexer;
31922 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31923 location_t loc = cp_lexer_peek_token (lexer)->location;
31925 if (kind == OMP_CLAUSE_VECTOR)
31926 id = "length";
31928 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31930 matching_parens parens;
31931 parens.consume_open (parser);
31935 cp_token *next = cp_lexer_peek_token (lexer);
31936 int idx = 0;
31938 /* Gang static argument. */
31939 if (kind == OMP_CLAUSE_GANG
31940 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31942 cp_lexer_consume_token (lexer);
31944 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31945 goto cleanup_error;
31947 idx = 1;
31948 if (ops[idx] != NULL)
31950 cp_parser_error (parser, "too many %<static%> arguments");
31951 goto cleanup_error;
31954 /* Check for the '*' argument. */
31955 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31956 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31957 || cp_lexer_nth_token_is (parser->lexer, 2,
31958 CPP_CLOSE_PAREN)))
31960 cp_lexer_consume_token (lexer);
31961 ops[idx] = integer_minus_one_node;
31963 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31965 cp_lexer_consume_token (lexer);
31966 continue;
31968 else break;
31971 /* Worker num: argument and vector length: arguments. */
31972 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31973 && id_equal (next->u.value, id)
31974 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31976 cp_lexer_consume_token (lexer); /* id */
31977 cp_lexer_consume_token (lexer); /* ':' */
31980 /* Now collect the actual argument. */
31981 if (ops[idx] != NULL_TREE)
31983 cp_parser_error (parser, "unexpected argument");
31984 goto cleanup_error;
31987 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31988 false);
31989 if (expr == error_mark_node)
31990 goto cleanup_error;
31992 mark_exp_read (expr);
31993 ops[idx] = expr;
31995 if (kind == OMP_CLAUSE_GANG
31996 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31998 cp_lexer_consume_token (lexer);
31999 continue;
32001 break;
32003 while (1);
32005 if (!parens.require_close (parser))
32006 goto cleanup_error;
32009 check_no_duplicate_clause (list, kind, str, loc);
32011 c = build_omp_clause (loc, kind);
32013 if (ops[1])
32014 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32016 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32017 OMP_CLAUSE_CHAIN (c) = list;
32019 return c;
32021 cleanup_error:
32022 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32023 return list;
32026 /* OpenACC 2.0:
32027 tile ( size-expr-list ) */
32029 static tree
32030 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32032 tree c, expr = error_mark_node;
32033 tree tile = NULL_TREE;
32035 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32036 so, but the spec authors never considered such a case and have
32037 differing opinions on what it might mean, including 'not
32038 allowed'.) */
32039 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32040 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32041 clause_loc);
32043 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32044 return list;
32048 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32049 return list;
32051 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32052 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32053 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32055 cp_lexer_consume_token (parser->lexer);
32056 expr = integer_zero_node;
32058 else
32059 expr = cp_parser_constant_expression (parser);
32061 tile = tree_cons (NULL_TREE, expr, tile);
32063 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32065 /* Consume the trailing ')'. */
32066 cp_lexer_consume_token (parser->lexer);
32068 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32069 tile = nreverse (tile);
32070 OMP_CLAUSE_TILE_LIST (c) = tile;
32071 OMP_CLAUSE_CHAIN (c) = list;
32072 return c;
32075 /* OpenACC 2.0
32076 Parse wait clause or directive parameters. */
32078 static tree
32079 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32081 vec<tree, va_gc> *args;
32082 tree t, args_tree;
32084 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32085 /*cast_p=*/false,
32086 /*allow_expansion_p=*/true,
32087 /*non_constant_p=*/NULL);
32089 if (args == NULL || args->length () == 0)
32091 cp_parser_error (parser, "expected integer expression before ')'");
32092 if (args != NULL)
32093 release_tree_vector (args);
32094 return list;
32097 args_tree = build_tree_list_vec (args);
32099 release_tree_vector (args);
32101 for (t = args_tree; t; t = TREE_CHAIN (t))
32103 tree targ = TREE_VALUE (t);
32105 if (targ != error_mark_node)
32107 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32108 error ("%<wait%> expression must be integral");
32109 else
32111 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32113 targ = mark_rvalue_use (targ);
32114 OMP_CLAUSE_DECL (c) = targ;
32115 OMP_CLAUSE_CHAIN (c) = list;
32116 list = c;
32121 return list;
32124 /* OpenACC:
32125 wait ( int-expr-list ) */
32127 static tree
32128 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32130 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32132 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32133 return list;
32135 list = cp_parser_oacc_wait_list (parser, location, list);
32137 return list;
32140 /* OpenMP 3.0:
32141 collapse ( constant-expression ) */
32143 static tree
32144 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32146 tree c, num;
32147 location_t loc;
32148 HOST_WIDE_INT n;
32150 loc = cp_lexer_peek_token (parser->lexer)->location;
32151 matching_parens parens;
32152 if (!parens.require_open (parser))
32153 return list;
32155 num = cp_parser_constant_expression (parser);
32157 if (!parens.require_close (parser))
32158 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32159 /*or_comma=*/false,
32160 /*consume_paren=*/true);
32162 if (num == error_mark_node)
32163 return list;
32164 num = fold_non_dependent_expr (num);
32165 if (!tree_fits_shwi_p (num)
32166 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32167 || (n = tree_to_shwi (num)) <= 0
32168 || (int) n != n)
32170 error_at (loc, "collapse argument needs positive constant integer expression");
32171 return list;
32174 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32175 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32176 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32177 OMP_CLAUSE_CHAIN (c) = list;
32178 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32180 return c;
32183 /* OpenMP 2.5:
32184 default ( none | shared )
32186 OpenACC:
32187 default ( none | present ) */
32189 static tree
32190 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32191 location_t location, bool is_oacc)
32193 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32194 tree c;
32196 matching_parens parens;
32197 if (!parens.require_open (parser))
32198 return list;
32199 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32201 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32202 const char *p = IDENTIFIER_POINTER (id);
32204 switch (p[0])
32206 case 'n':
32207 if (strcmp ("none", p) != 0)
32208 goto invalid_kind;
32209 kind = OMP_CLAUSE_DEFAULT_NONE;
32210 break;
32212 case 'p':
32213 if (strcmp ("present", p) != 0 || !is_oacc)
32214 goto invalid_kind;
32215 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32216 break;
32218 case 's':
32219 if (strcmp ("shared", p) != 0 || is_oacc)
32220 goto invalid_kind;
32221 kind = OMP_CLAUSE_DEFAULT_SHARED;
32222 break;
32224 default:
32225 goto invalid_kind;
32228 cp_lexer_consume_token (parser->lexer);
32230 else
32232 invalid_kind:
32233 if (is_oacc)
32234 cp_parser_error (parser, "expected %<none%> or %<present%>");
32235 else
32236 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32239 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32240 || !parens.require_close (parser))
32241 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32242 /*or_comma=*/false,
32243 /*consume_paren=*/true);
32245 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32246 return list;
32248 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32249 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32250 OMP_CLAUSE_CHAIN (c) = list;
32251 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32253 return c;
32256 /* OpenMP 3.1:
32257 final ( expression ) */
32259 static tree
32260 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32262 tree t, c;
32264 matching_parens parens;
32265 if (!parens.require_open (parser))
32266 return list;
32268 t = cp_parser_condition (parser);
32270 if (t == error_mark_node
32271 || !parens.require_close (parser))
32272 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32273 /*or_comma=*/false,
32274 /*consume_paren=*/true);
32276 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32278 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32279 OMP_CLAUSE_FINAL_EXPR (c) = t;
32280 OMP_CLAUSE_CHAIN (c) = list;
32282 return c;
32285 /* OpenMP 2.5:
32286 if ( expression )
32288 OpenMP 4.5:
32289 if ( directive-name-modifier : expression )
32291 directive-name-modifier:
32292 parallel | task | taskloop | target data | target | target update
32293 | target enter data | target exit data */
32295 static tree
32296 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32297 bool is_omp)
32299 tree t, c;
32300 enum tree_code if_modifier = ERROR_MARK;
32302 matching_parens parens;
32303 if (!parens.require_open (parser))
32304 return list;
32306 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32308 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32309 const char *p = IDENTIFIER_POINTER (id);
32310 int n = 2;
32312 if (strcmp ("parallel", p) == 0)
32313 if_modifier = OMP_PARALLEL;
32314 else if (strcmp ("task", p) == 0)
32315 if_modifier = OMP_TASK;
32316 else if (strcmp ("taskloop", p) == 0)
32317 if_modifier = OMP_TASKLOOP;
32318 else if (strcmp ("target", p) == 0)
32320 if_modifier = OMP_TARGET;
32321 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32323 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32324 p = IDENTIFIER_POINTER (id);
32325 if (strcmp ("data", p) == 0)
32326 if_modifier = OMP_TARGET_DATA;
32327 else if (strcmp ("update", p) == 0)
32328 if_modifier = OMP_TARGET_UPDATE;
32329 else if (strcmp ("enter", p) == 0)
32330 if_modifier = OMP_TARGET_ENTER_DATA;
32331 else if (strcmp ("exit", p) == 0)
32332 if_modifier = OMP_TARGET_EXIT_DATA;
32333 if (if_modifier != OMP_TARGET)
32334 n = 3;
32335 else
32337 location_t loc
32338 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32339 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32340 "or %<exit%>");
32341 if_modifier = ERROR_MARK;
32343 if (if_modifier == OMP_TARGET_ENTER_DATA
32344 || if_modifier == OMP_TARGET_EXIT_DATA)
32346 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32348 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32349 p = IDENTIFIER_POINTER (id);
32350 if (strcmp ("data", p) == 0)
32351 n = 4;
32353 if (n != 4)
32355 location_t loc
32356 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32357 error_at (loc, "expected %<data%>");
32358 if_modifier = ERROR_MARK;
32363 if (if_modifier != ERROR_MARK)
32365 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32367 while (n-- > 0)
32368 cp_lexer_consume_token (parser->lexer);
32370 else
32372 if (n > 2)
32374 location_t loc
32375 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32376 error_at (loc, "expected %<:%>");
32378 if_modifier = ERROR_MARK;
32383 t = cp_parser_condition (parser);
32385 if (t == error_mark_node
32386 || !parens.require_close (parser))
32387 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32388 /*or_comma=*/false,
32389 /*consume_paren=*/true);
32391 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32392 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32394 if (if_modifier != ERROR_MARK
32395 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32397 const char *p = NULL;
32398 switch (if_modifier)
32400 case OMP_PARALLEL: p = "parallel"; break;
32401 case OMP_TASK: p = "task"; break;
32402 case OMP_TASKLOOP: p = "taskloop"; break;
32403 case OMP_TARGET_DATA: p = "target data"; break;
32404 case OMP_TARGET: p = "target"; break;
32405 case OMP_TARGET_UPDATE: p = "target update"; break;
32406 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32407 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32408 default: gcc_unreachable ();
32410 error_at (location, "too many %<if%> clauses with %qs modifier",
32412 return list;
32414 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32416 if (!is_omp)
32417 error_at (location, "too many %<if%> clauses");
32418 else
32419 error_at (location, "too many %<if%> clauses without modifier");
32420 return list;
32422 else if (if_modifier == ERROR_MARK
32423 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32425 error_at (location, "if any %<if%> clause has modifier, then all "
32426 "%<if%> clauses have to use modifier");
32427 return list;
32431 c = build_omp_clause (location, OMP_CLAUSE_IF);
32432 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32433 OMP_CLAUSE_IF_EXPR (c) = t;
32434 OMP_CLAUSE_CHAIN (c) = list;
32436 return c;
32439 /* OpenMP 3.1:
32440 mergeable */
32442 static tree
32443 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32444 tree list, location_t location)
32446 tree c;
32448 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32449 location);
32451 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32452 OMP_CLAUSE_CHAIN (c) = list;
32453 return c;
32456 /* OpenMP 2.5:
32457 nowait */
32459 static tree
32460 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32461 tree list, location_t location)
32463 tree c;
32465 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32467 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32468 OMP_CLAUSE_CHAIN (c) = list;
32469 return c;
32472 /* OpenMP 2.5:
32473 num_threads ( expression ) */
32475 static tree
32476 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32477 location_t location)
32479 tree t, c;
32481 matching_parens parens;
32482 if (!parens.require_open (parser))
32483 return list;
32485 t = cp_parser_expression (parser);
32487 if (t == error_mark_node
32488 || !parens.require_close (parser))
32489 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32490 /*or_comma=*/false,
32491 /*consume_paren=*/true);
32493 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32494 "num_threads", location);
32496 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32497 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32498 OMP_CLAUSE_CHAIN (c) = list;
32500 return c;
32503 /* OpenMP 4.5:
32504 num_tasks ( expression ) */
32506 static tree
32507 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32508 location_t location)
32510 tree t, c;
32512 matching_parens parens;
32513 if (!parens.require_open (parser))
32514 return list;
32516 t = cp_parser_expression (parser);
32518 if (t == error_mark_node
32519 || !parens.require_close (parser))
32520 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32521 /*or_comma=*/false,
32522 /*consume_paren=*/true);
32524 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32525 "num_tasks", location);
32527 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32528 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32529 OMP_CLAUSE_CHAIN (c) = list;
32531 return c;
32534 /* OpenMP 4.5:
32535 grainsize ( expression ) */
32537 static tree
32538 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32539 location_t location)
32541 tree t, c;
32543 matching_parens parens;
32544 if (!parens.require_open (parser))
32545 return list;
32547 t = cp_parser_expression (parser);
32549 if (t == error_mark_node
32550 || !parens.require_close (parser))
32551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32552 /*or_comma=*/false,
32553 /*consume_paren=*/true);
32555 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32556 "grainsize", location);
32558 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32559 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32560 OMP_CLAUSE_CHAIN (c) = list;
32562 return c;
32565 /* OpenMP 4.5:
32566 priority ( expression ) */
32568 static tree
32569 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32570 location_t location)
32572 tree t, c;
32574 matching_parens parens;
32575 if (!parens.require_open (parser))
32576 return list;
32578 t = cp_parser_expression (parser);
32580 if (t == error_mark_node
32581 || !parens.require_close (parser))
32582 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32583 /*or_comma=*/false,
32584 /*consume_paren=*/true);
32586 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32587 "priority", location);
32589 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32590 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32591 OMP_CLAUSE_CHAIN (c) = list;
32593 return c;
32596 /* OpenMP 4.5:
32597 hint ( expression ) */
32599 static tree
32600 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32601 location_t location)
32603 tree t, c;
32605 matching_parens parens;
32606 if (!parens.require_open (parser))
32607 return list;
32609 t = cp_parser_expression (parser);
32611 if (t == error_mark_node
32612 || !parens.require_close (parser))
32613 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32614 /*or_comma=*/false,
32615 /*consume_paren=*/true);
32617 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32619 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32620 OMP_CLAUSE_HINT_EXPR (c) = t;
32621 OMP_CLAUSE_CHAIN (c) = list;
32623 return c;
32626 /* OpenMP 4.5:
32627 defaultmap ( tofrom : scalar ) */
32629 static tree
32630 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32631 location_t location)
32633 tree c, id;
32634 const char *p;
32636 matching_parens parens;
32637 if (!parens.require_open (parser))
32638 return list;
32640 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32642 cp_parser_error (parser, "expected %<tofrom%>");
32643 goto out_err;
32645 id = cp_lexer_peek_token (parser->lexer)->u.value;
32646 p = IDENTIFIER_POINTER (id);
32647 if (strcmp (p, "tofrom") != 0)
32649 cp_parser_error (parser, "expected %<tofrom%>");
32650 goto out_err;
32652 cp_lexer_consume_token (parser->lexer);
32653 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32654 goto out_err;
32656 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32658 cp_parser_error (parser, "expected %<scalar%>");
32659 goto out_err;
32661 id = cp_lexer_peek_token (parser->lexer)->u.value;
32662 p = IDENTIFIER_POINTER (id);
32663 if (strcmp (p, "scalar") != 0)
32665 cp_parser_error (parser, "expected %<scalar%>");
32666 goto out_err;
32668 cp_lexer_consume_token (parser->lexer);
32669 if (!parens.require_close (parser))
32670 goto out_err;
32672 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32673 location);
32675 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32676 OMP_CLAUSE_CHAIN (c) = list;
32677 return c;
32679 out_err:
32680 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32681 /*or_comma=*/false,
32682 /*consume_paren=*/true);
32683 return list;
32686 /* OpenMP 2.5:
32687 ordered
32689 OpenMP 4.5:
32690 ordered ( constant-expression ) */
32692 static tree
32693 cp_parser_omp_clause_ordered (cp_parser *parser,
32694 tree list, location_t location)
32696 tree c, num = NULL_TREE;
32697 HOST_WIDE_INT n;
32699 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32700 "ordered", location);
32702 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32704 matching_parens parens;
32705 parens.consume_open (parser);
32707 num = cp_parser_constant_expression (parser);
32709 if (!parens.require_close (parser))
32710 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32711 /*or_comma=*/false,
32712 /*consume_paren=*/true);
32714 if (num == error_mark_node)
32715 return list;
32716 num = fold_non_dependent_expr (num);
32717 if (!tree_fits_shwi_p (num)
32718 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32719 || (n = tree_to_shwi (num)) <= 0
32720 || (int) n != n)
32722 error_at (location,
32723 "ordered argument needs positive constant integer "
32724 "expression");
32725 return list;
32729 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32730 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32731 OMP_CLAUSE_CHAIN (c) = list;
32732 return c;
32735 /* OpenMP 2.5:
32736 reduction ( reduction-operator : variable-list )
32738 reduction-operator:
32739 One of: + * - & ^ | && ||
32741 OpenMP 3.1:
32743 reduction-operator:
32744 One of: + * - & ^ | && || min max
32746 OpenMP 4.0:
32748 reduction-operator:
32749 One of: + * - & ^ | && ||
32750 id-expression */
32752 static tree
32753 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32755 enum tree_code code = ERROR_MARK;
32756 tree nlist, c, id = NULL_TREE;
32758 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32759 return list;
32761 switch (cp_lexer_peek_token (parser->lexer)->type)
32763 case CPP_PLUS: code = PLUS_EXPR; break;
32764 case CPP_MULT: code = MULT_EXPR; break;
32765 case CPP_MINUS: code = MINUS_EXPR; break;
32766 case CPP_AND: code = BIT_AND_EXPR; break;
32767 case CPP_XOR: code = BIT_XOR_EXPR; break;
32768 case CPP_OR: code = BIT_IOR_EXPR; break;
32769 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32770 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32771 default: break;
32774 if (code != ERROR_MARK)
32775 cp_lexer_consume_token (parser->lexer);
32776 else
32778 bool saved_colon_corrects_to_scope_p;
32779 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32780 parser->colon_corrects_to_scope_p = false;
32781 id = cp_parser_id_expression (parser, /*template_p=*/false,
32782 /*check_dependency_p=*/true,
32783 /*template_p=*/NULL,
32784 /*declarator_p=*/false,
32785 /*optional_p=*/false);
32786 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32787 if (identifier_p (id))
32789 const char *p = IDENTIFIER_POINTER (id);
32791 if (strcmp (p, "min") == 0)
32792 code = MIN_EXPR;
32793 else if (strcmp (p, "max") == 0)
32794 code = MAX_EXPR;
32795 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32796 code = PLUS_EXPR;
32797 else if (id == ovl_op_identifier (false, MULT_EXPR))
32798 code = MULT_EXPR;
32799 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32800 code = MINUS_EXPR;
32801 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32802 code = BIT_AND_EXPR;
32803 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32804 code = BIT_IOR_EXPR;
32805 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32806 code = BIT_XOR_EXPR;
32807 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32808 code = TRUTH_ANDIF_EXPR;
32809 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32810 code = TRUTH_ORIF_EXPR;
32811 id = omp_reduction_id (code, id, NULL_TREE);
32812 tree scope = parser->scope;
32813 if (scope)
32814 id = build_qualified_name (NULL_TREE, scope, id, false);
32815 parser->scope = NULL_TREE;
32816 parser->qualifying_scope = NULL_TREE;
32817 parser->object_scope = NULL_TREE;
32819 else
32821 error ("invalid reduction-identifier");
32822 resync_fail:
32823 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32824 /*or_comma=*/false,
32825 /*consume_paren=*/true);
32826 return list;
32830 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32831 goto resync_fail;
32833 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32834 NULL);
32835 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32837 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32838 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32841 return nlist;
32844 /* OpenMP 2.5:
32845 schedule ( schedule-kind )
32846 schedule ( schedule-kind , expression )
32848 schedule-kind:
32849 static | dynamic | guided | runtime | auto
32851 OpenMP 4.5:
32852 schedule ( schedule-modifier : schedule-kind )
32853 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32855 schedule-modifier:
32856 simd
32857 monotonic
32858 nonmonotonic */
32860 static tree
32861 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32863 tree c, t;
32864 int modifiers = 0, nmodifiers = 0;
32866 matching_parens parens;
32867 if (!parens.require_open (parser))
32868 return list;
32870 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32872 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32874 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32875 const char *p = IDENTIFIER_POINTER (id);
32876 if (strcmp ("simd", p) == 0)
32877 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32878 else if (strcmp ("monotonic", p) == 0)
32879 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32880 else if (strcmp ("nonmonotonic", p) == 0)
32881 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32882 else
32883 break;
32884 cp_lexer_consume_token (parser->lexer);
32885 if (nmodifiers++ == 0
32886 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32887 cp_lexer_consume_token (parser->lexer);
32888 else
32890 cp_parser_require (parser, CPP_COLON, RT_COLON);
32891 break;
32895 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32897 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32898 const char *p = IDENTIFIER_POINTER (id);
32900 switch (p[0])
32902 case 'd':
32903 if (strcmp ("dynamic", p) != 0)
32904 goto invalid_kind;
32905 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32906 break;
32908 case 'g':
32909 if (strcmp ("guided", p) != 0)
32910 goto invalid_kind;
32911 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32912 break;
32914 case 'r':
32915 if (strcmp ("runtime", p) != 0)
32916 goto invalid_kind;
32917 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32918 break;
32920 default:
32921 goto invalid_kind;
32924 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32925 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32926 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32927 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32928 else
32929 goto invalid_kind;
32930 cp_lexer_consume_token (parser->lexer);
32932 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32933 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32934 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32935 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32937 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32938 "specified");
32939 modifiers = 0;
32942 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32944 cp_token *token;
32945 cp_lexer_consume_token (parser->lexer);
32947 token = cp_lexer_peek_token (parser->lexer);
32948 t = cp_parser_assignment_expression (parser);
32950 if (t == error_mark_node)
32951 goto resync_fail;
32952 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32953 error_at (token->location, "schedule %<runtime%> does not take "
32954 "a %<chunk_size%> parameter");
32955 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32956 error_at (token->location, "schedule %<auto%> does not take "
32957 "a %<chunk_size%> parameter");
32958 else
32959 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32961 if (!parens.require_close (parser))
32962 goto resync_fail;
32964 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32965 goto resync_fail;
32967 OMP_CLAUSE_SCHEDULE_KIND (c)
32968 = (enum omp_clause_schedule_kind)
32969 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32971 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32972 OMP_CLAUSE_CHAIN (c) = list;
32973 return c;
32975 invalid_kind:
32976 cp_parser_error (parser, "invalid schedule kind");
32977 resync_fail:
32978 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32979 /*or_comma=*/false,
32980 /*consume_paren=*/true);
32981 return list;
32984 /* OpenMP 3.0:
32985 untied */
32987 static tree
32988 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32989 tree list, location_t location)
32991 tree c;
32993 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32995 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32996 OMP_CLAUSE_CHAIN (c) = list;
32997 return c;
33000 /* OpenMP 4.0:
33001 inbranch
33002 notinbranch */
33004 static tree
33005 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33006 tree list, location_t location)
33008 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33009 tree c = build_omp_clause (location, code);
33010 OMP_CLAUSE_CHAIN (c) = list;
33011 return c;
33014 /* OpenMP 4.0:
33015 parallel
33017 sections
33018 taskgroup */
33020 static tree
33021 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33022 enum omp_clause_code code,
33023 tree list, location_t location)
33025 tree c = build_omp_clause (location, code);
33026 OMP_CLAUSE_CHAIN (c) = list;
33027 return c;
33030 /* OpenMP 4.5:
33031 nogroup */
33033 static tree
33034 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33035 tree list, location_t location)
33037 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33038 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33039 OMP_CLAUSE_CHAIN (c) = list;
33040 return c;
33043 /* OpenMP 4.5:
33044 simd
33045 threads */
33047 static tree
33048 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33049 enum omp_clause_code code,
33050 tree list, location_t location)
33052 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33053 tree c = build_omp_clause (location, code);
33054 OMP_CLAUSE_CHAIN (c) = list;
33055 return c;
33058 /* OpenMP 4.0:
33059 num_teams ( expression ) */
33061 static tree
33062 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33063 location_t location)
33065 tree t, c;
33067 matching_parens parens;
33068 if (!parens.require_open (parser))
33069 return list;
33071 t = cp_parser_expression (parser);
33073 if (t == error_mark_node
33074 || !parens.require_close (parser))
33075 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33076 /*or_comma=*/false,
33077 /*consume_paren=*/true);
33079 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33080 "num_teams", location);
33082 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33083 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33084 OMP_CLAUSE_CHAIN (c) = list;
33086 return c;
33089 /* OpenMP 4.0:
33090 thread_limit ( expression ) */
33092 static tree
33093 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33094 location_t location)
33096 tree t, c;
33098 matching_parens parens;
33099 if (!parens.require_open (parser))
33100 return list;
33102 t = cp_parser_expression (parser);
33104 if (t == error_mark_node
33105 || !parens.require_close (parser))
33106 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33107 /*or_comma=*/false,
33108 /*consume_paren=*/true);
33110 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33111 "thread_limit", location);
33113 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33114 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33115 OMP_CLAUSE_CHAIN (c) = list;
33117 return c;
33120 /* OpenMP 4.0:
33121 aligned ( variable-list )
33122 aligned ( variable-list : constant-expression ) */
33124 static tree
33125 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33127 tree nlist, c, alignment = NULL_TREE;
33128 bool colon;
33130 matching_parens parens;
33131 if (!parens.require_open (parser))
33132 return list;
33134 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33135 &colon);
33137 if (colon)
33139 alignment = cp_parser_constant_expression (parser);
33141 if (!parens.require_close (parser))
33142 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33143 /*or_comma=*/false,
33144 /*consume_paren=*/true);
33146 if (alignment == error_mark_node)
33147 alignment = NULL_TREE;
33150 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33151 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33153 return nlist;
33156 /* OpenMP 4.0:
33157 linear ( variable-list )
33158 linear ( variable-list : expression )
33160 OpenMP 4.5:
33161 linear ( modifier ( variable-list ) )
33162 linear ( modifier ( variable-list ) : expression ) */
33164 static tree
33165 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33166 bool declare_simd)
33168 tree nlist, c, step = integer_one_node;
33169 bool colon;
33170 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33172 matching_parens parens;
33173 if (!parens.require_open (parser))
33174 return list;
33176 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33178 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33179 const char *p = IDENTIFIER_POINTER (id);
33181 if (strcmp ("ref", p) == 0)
33182 kind = OMP_CLAUSE_LINEAR_REF;
33183 else if (strcmp ("val", p) == 0)
33184 kind = OMP_CLAUSE_LINEAR_VAL;
33185 else if (strcmp ("uval", p) == 0)
33186 kind = OMP_CLAUSE_LINEAR_UVAL;
33187 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33188 cp_lexer_consume_token (parser->lexer);
33189 else
33190 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33193 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33194 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33195 &colon);
33196 else
33198 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33199 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33200 if (colon)
33201 cp_parser_require (parser, CPP_COLON, RT_COLON);
33202 else if (!parens.require_close (parser))
33203 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33204 /*or_comma=*/false,
33205 /*consume_paren=*/true);
33208 if (colon)
33210 step = NULL_TREE;
33211 if (declare_simd
33212 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33213 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33215 cp_token *token = cp_lexer_peek_token (parser->lexer);
33216 cp_parser_parse_tentatively (parser);
33217 step = cp_parser_id_expression (parser, /*template_p=*/false,
33218 /*check_dependency_p=*/true,
33219 /*template_p=*/NULL,
33220 /*declarator_p=*/false,
33221 /*optional_p=*/false);
33222 if (step != error_mark_node)
33223 step = cp_parser_lookup_name_simple (parser, step, token->location);
33224 if (step == error_mark_node)
33226 step = NULL_TREE;
33227 cp_parser_abort_tentative_parse (parser);
33229 else if (!cp_parser_parse_definitely (parser))
33230 step = NULL_TREE;
33232 if (!step)
33233 step = cp_parser_expression (parser);
33235 if (!parens.require_close (parser))
33236 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33237 /*or_comma=*/false,
33238 /*consume_paren=*/true);
33240 if (step == error_mark_node)
33241 return list;
33244 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33246 OMP_CLAUSE_LINEAR_STEP (c) = step;
33247 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33250 return nlist;
33253 /* OpenMP 4.0:
33254 safelen ( constant-expression ) */
33256 static tree
33257 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33258 location_t location)
33260 tree t, c;
33262 matching_parens parens;
33263 if (!parens.require_open (parser))
33264 return list;
33266 t = cp_parser_constant_expression (parser);
33268 if (t == error_mark_node
33269 || !parens.require_close (parser))
33270 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33271 /*or_comma=*/false,
33272 /*consume_paren=*/true);
33274 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33276 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33277 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33278 OMP_CLAUSE_CHAIN (c) = list;
33280 return c;
33283 /* OpenMP 4.0:
33284 simdlen ( constant-expression ) */
33286 static tree
33287 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33288 location_t location)
33290 tree t, c;
33292 matching_parens parens;
33293 if (!parens.require_open (parser))
33294 return list;
33296 t = cp_parser_constant_expression (parser);
33298 if (t == error_mark_node
33299 || !parens.require_close (parser))
33300 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33301 /*or_comma=*/false,
33302 /*consume_paren=*/true);
33304 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33306 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33307 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33308 OMP_CLAUSE_CHAIN (c) = list;
33310 return c;
33313 /* OpenMP 4.5:
33314 vec:
33315 identifier [+/- integer]
33316 vec , identifier [+/- integer]
33319 static tree
33320 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33321 tree list)
33323 tree vec = NULL;
33325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33327 cp_parser_error (parser, "expected identifier");
33328 return list;
33331 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33333 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33334 tree t, identifier = cp_parser_identifier (parser);
33335 tree addend = NULL;
33337 if (identifier == error_mark_node)
33338 t = error_mark_node;
33339 else
33341 t = cp_parser_lookup_name_simple
33342 (parser, identifier,
33343 cp_lexer_peek_token (parser->lexer)->location);
33344 if (t == error_mark_node)
33345 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33346 id_loc);
33349 bool neg = false;
33350 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33351 neg = true;
33352 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33354 addend = integer_zero_node;
33355 goto add_to_vector;
33357 cp_lexer_consume_token (parser->lexer);
33359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33361 cp_parser_error (parser, "expected integer");
33362 return list;
33365 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33366 if (TREE_CODE (addend) != INTEGER_CST)
33368 cp_parser_error (parser, "expected integer");
33369 return list;
33371 cp_lexer_consume_token (parser->lexer);
33373 add_to_vector:
33374 if (t != error_mark_node)
33376 vec = tree_cons (addend, t, vec);
33377 if (neg)
33378 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33381 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33382 break;
33384 cp_lexer_consume_token (parser->lexer);
33387 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33389 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33390 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33391 OMP_CLAUSE_DECL (u) = nreverse (vec);
33392 OMP_CLAUSE_CHAIN (u) = list;
33393 return u;
33395 return list;
33398 /* OpenMP 4.0:
33399 depend ( depend-kind : variable-list )
33401 depend-kind:
33402 in | out | inout
33404 OpenMP 4.5:
33405 depend ( source )
33407 depend ( sink : vec ) */
33409 static tree
33410 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33412 tree nlist, c;
33413 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33415 matching_parens parens;
33416 if (!parens.require_open (parser))
33417 return list;
33419 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33421 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33422 const char *p = IDENTIFIER_POINTER (id);
33424 if (strcmp ("in", p) == 0)
33425 kind = OMP_CLAUSE_DEPEND_IN;
33426 else if (strcmp ("inout", p) == 0)
33427 kind = OMP_CLAUSE_DEPEND_INOUT;
33428 else if (strcmp ("out", p) == 0)
33429 kind = OMP_CLAUSE_DEPEND_OUT;
33430 else if (strcmp ("source", p) == 0)
33431 kind = OMP_CLAUSE_DEPEND_SOURCE;
33432 else if (strcmp ("sink", p) == 0)
33433 kind = OMP_CLAUSE_DEPEND_SINK;
33434 else
33435 goto invalid_kind;
33437 else
33438 goto invalid_kind;
33440 cp_lexer_consume_token (parser->lexer);
33442 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33444 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33445 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33446 OMP_CLAUSE_DECL (c) = NULL_TREE;
33447 OMP_CLAUSE_CHAIN (c) = list;
33448 if (!parens.require_close (parser))
33449 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33450 /*or_comma=*/false,
33451 /*consume_paren=*/true);
33452 return c;
33455 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33456 goto resync_fail;
33458 if (kind == OMP_CLAUSE_DEPEND_SINK)
33459 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33460 else
33462 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33463 list, NULL);
33465 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33466 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33468 return nlist;
33470 invalid_kind:
33471 cp_parser_error (parser, "invalid depend kind");
33472 resync_fail:
33473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33474 /*or_comma=*/false,
33475 /*consume_paren=*/true);
33476 return list;
33479 /* OpenMP 4.0:
33480 map ( map-kind : variable-list )
33481 map ( variable-list )
33483 map-kind:
33484 alloc | to | from | tofrom
33486 OpenMP 4.5:
33487 map-kind:
33488 alloc | to | from | tofrom | release | delete
33490 map ( always [,] map-kind: variable-list ) */
33492 static tree
33493 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33495 tree nlist, c;
33496 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33497 bool always = false;
33499 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33500 return list;
33502 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33504 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33505 const char *p = IDENTIFIER_POINTER (id);
33507 if (strcmp ("always", p) == 0)
33509 int nth = 2;
33510 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33511 nth++;
33512 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33513 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33514 == RID_DELETE))
33515 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33516 == CPP_COLON))
33518 always = true;
33519 cp_lexer_consume_token (parser->lexer);
33520 if (nth == 3)
33521 cp_lexer_consume_token (parser->lexer);
33526 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33527 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33529 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33530 const char *p = IDENTIFIER_POINTER (id);
33532 if (strcmp ("alloc", p) == 0)
33533 kind = GOMP_MAP_ALLOC;
33534 else if (strcmp ("to", p) == 0)
33535 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33536 else if (strcmp ("from", p) == 0)
33537 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33538 else if (strcmp ("tofrom", p) == 0)
33539 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33540 else if (strcmp ("release", p) == 0)
33541 kind = GOMP_MAP_RELEASE;
33542 else
33544 cp_parser_error (parser, "invalid map kind");
33545 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33546 /*or_comma=*/false,
33547 /*consume_paren=*/true);
33548 return list;
33550 cp_lexer_consume_token (parser->lexer);
33551 cp_lexer_consume_token (parser->lexer);
33553 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33554 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33556 kind = GOMP_MAP_DELETE;
33557 cp_lexer_consume_token (parser->lexer);
33558 cp_lexer_consume_token (parser->lexer);
33561 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33562 NULL);
33564 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33565 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33567 return nlist;
33570 /* OpenMP 4.0:
33571 device ( expression ) */
33573 static tree
33574 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33575 location_t location)
33577 tree t, c;
33579 matching_parens parens;
33580 if (!parens.require_open (parser))
33581 return list;
33583 t = cp_parser_expression (parser);
33585 if (t == error_mark_node
33586 || !parens.require_close (parser))
33587 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33588 /*or_comma=*/false,
33589 /*consume_paren=*/true);
33591 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33592 "device", location);
33594 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33595 OMP_CLAUSE_DEVICE_ID (c) = t;
33596 OMP_CLAUSE_CHAIN (c) = list;
33598 return c;
33601 /* OpenMP 4.0:
33602 dist_schedule ( static )
33603 dist_schedule ( static , expression ) */
33605 static tree
33606 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33607 location_t location)
33609 tree c, t;
33611 matching_parens parens;
33612 if (!parens.require_open (parser))
33613 return list;
33615 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33617 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33618 goto invalid_kind;
33619 cp_lexer_consume_token (parser->lexer);
33621 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33623 cp_lexer_consume_token (parser->lexer);
33625 t = cp_parser_assignment_expression (parser);
33627 if (t == error_mark_node)
33628 goto resync_fail;
33629 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33631 if (!parens.require_close (parser))
33632 goto resync_fail;
33634 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33635 goto resync_fail;
33637 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33638 location);
33639 OMP_CLAUSE_CHAIN (c) = list;
33640 return c;
33642 invalid_kind:
33643 cp_parser_error (parser, "invalid dist_schedule kind");
33644 resync_fail:
33645 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33646 /*or_comma=*/false,
33647 /*consume_paren=*/true);
33648 return list;
33651 /* OpenMP 4.0:
33652 proc_bind ( proc-bind-kind )
33654 proc-bind-kind:
33655 master | close | spread */
33657 static tree
33658 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33659 location_t location)
33661 tree c;
33662 enum omp_clause_proc_bind_kind kind;
33664 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33665 return list;
33667 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33669 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33670 const char *p = IDENTIFIER_POINTER (id);
33672 if (strcmp ("master", p) == 0)
33673 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33674 else if (strcmp ("close", p) == 0)
33675 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33676 else if (strcmp ("spread", p) == 0)
33677 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33678 else
33679 goto invalid_kind;
33681 else
33682 goto invalid_kind;
33684 cp_lexer_consume_token (parser->lexer);
33685 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33686 goto resync_fail;
33688 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33689 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33690 location);
33691 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33692 OMP_CLAUSE_CHAIN (c) = list;
33693 return c;
33695 invalid_kind:
33696 cp_parser_error (parser, "invalid depend kind");
33697 resync_fail:
33698 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33699 /*or_comma=*/false,
33700 /*consume_paren=*/true);
33701 return list;
33704 /* OpenACC:
33705 async [( int-expr )] */
33707 static tree
33708 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33710 tree c, t;
33711 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33713 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33715 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33717 matching_parens parens;
33718 parens.consume_open (parser);
33720 t = cp_parser_expression (parser);
33721 if (t == error_mark_node
33722 || !parens.require_close (parser))
33723 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33724 /*or_comma=*/false,
33725 /*consume_paren=*/true);
33728 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33730 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33731 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33732 OMP_CLAUSE_CHAIN (c) = list;
33733 list = c;
33735 return list;
33738 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33739 is a bitmask in MASK. Return the list of clauses found. */
33741 static tree
33742 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33743 const char *where, cp_token *pragma_tok,
33744 bool finish_p = true)
33746 tree clauses = NULL;
33747 bool first = true;
33749 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33751 location_t here;
33752 pragma_omp_clause c_kind;
33753 omp_clause_code code;
33754 const char *c_name;
33755 tree prev = clauses;
33757 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33758 cp_lexer_consume_token (parser->lexer);
33760 here = cp_lexer_peek_token (parser->lexer)->location;
33761 c_kind = cp_parser_omp_clause_name (parser);
33763 switch (c_kind)
33765 case PRAGMA_OACC_CLAUSE_ASYNC:
33766 clauses = cp_parser_oacc_clause_async (parser, clauses);
33767 c_name = "async";
33768 break;
33769 case PRAGMA_OACC_CLAUSE_AUTO:
33770 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33771 clauses, here);
33772 c_name = "auto";
33773 break;
33774 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33775 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33776 c_name = "collapse";
33777 break;
33778 case PRAGMA_OACC_CLAUSE_COPY:
33779 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33780 c_name = "copy";
33781 break;
33782 case PRAGMA_OACC_CLAUSE_COPYIN:
33783 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33784 c_name = "copyin";
33785 break;
33786 case PRAGMA_OACC_CLAUSE_COPYOUT:
33787 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33788 c_name = "copyout";
33789 break;
33790 case PRAGMA_OACC_CLAUSE_CREATE:
33791 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33792 c_name = "create";
33793 break;
33794 case PRAGMA_OACC_CLAUSE_DELETE:
33795 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33796 c_name = "delete";
33797 break;
33798 case PRAGMA_OMP_CLAUSE_DEFAULT:
33799 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33800 c_name = "default";
33801 break;
33802 case PRAGMA_OACC_CLAUSE_DEVICE:
33803 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33804 c_name = "device";
33805 break;
33806 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33807 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33808 c_name = "deviceptr";
33809 break;
33810 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33811 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33812 c_name = "device_resident";
33813 break;
33814 case PRAGMA_OACC_CLAUSE_FINALIZE:
33815 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
33816 clauses, here);
33817 c_name = "finalize";
33818 break;
33819 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33820 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33821 clauses);
33822 c_name = "firstprivate";
33823 break;
33824 case PRAGMA_OACC_CLAUSE_GANG:
33825 c_name = "gang";
33826 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33827 c_name, clauses);
33828 break;
33829 case PRAGMA_OACC_CLAUSE_HOST:
33830 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33831 c_name = "host";
33832 break;
33833 case PRAGMA_OACC_CLAUSE_IF:
33834 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33835 c_name = "if";
33836 break;
33837 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
33838 clauses = cp_parser_oacc_simple_clause (parser,
33839 OMP_CLAUSE_IF_PRESENT,
33840 clauses, here);
33841 c_name = "if_present";
33842 break;
33843 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33844 clauses = cp_parser_oacc_simple_clause (parser,
33845 OMP_CLAUSE_INDEPENDENT,
33846 clauses, here);
33847 c_name = "independent";
33848 break;
33849 case PRAGMA_OACC_CLAUSE_LINK:
33850 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33851 c_name = "link";
33852 break;
33853 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33854 code = OMP_CLAUSE_NUM_GANGS;
33855 c_name = "num_gangs";
33856 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33857 clauses);
33858 break;
33859 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33860 c_name = "num_workers";
33861 code = OMP_CLAUSE_NUM_WORKERS;
33862 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33863 clauses);
33864 break;
33865 case PRAGMA_OACC_CLAUSE_PRESENT:
33866 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33867 c_name = "present";
33868 break;
33869 case PRAGMA_OACC_CLAUSE_PRIVATE:
33870 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33871 clauses);
33872 c_name = "private";
33873 break;
33874 case PRAGMA_OACC_CLAUSE_REDUCTION:
33875 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33876 c_name = "reduction";
33877 break;
33878 case PRAGMA_OACC_CLAUSE_SEQ:
33879 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33880 clauses, here);
33881 c_name = "seq";
33882 break;
33883 case PRAGMA_OACC_CLAUSE_TILE:
33884 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33885 c_name = "tile";
33886 break;
33887 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33888 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33889 clauses);
33890 c_name = "use_device";
33891 break;
33892 case PRAGMA_OACC_CLAUSE_VECTOR:
33893 c_name = "vector";
33894 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33895 c_name, clauses);
33896 break;
33897 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33898 c_name = "vector_length";
33899 code = OMP_CLAUSE_VECTOR_LENGTH;
33900 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33901 clauses);
33902 break;
33903 case PRAGMA_OACC_CLAUSE_WAIT:
33904 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33905 c_name = "wait";
33906 break;
33907 case PRAGMA_OACC_CLAUSE_WORKER:
33908 c_name = "worker";
33909 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33910 c_name, clauses);
33911 break;
33912 default:
33913 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33914 goto saw_error;
33917 first = false;
33919 if (((mask >> c_kind) & 1) == 0)
33921 /* Remove the invalid clause(s) from the list to avoid
33922 confusing the rest of the compiler. */
33923 clauses = prev;
33924 error_at (here, "%qs is not valid for %qs", c_name, where);
33928 saw_error:
33929 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33931 if (finish_p)
33932 return finish_omp_clauses (clauses, C_ORT_ACC);
33934 return clauses;
33937 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33938 is a bitmask in MASK. Return the list of clauses found; the result
33939 of clause default goes in *pdefault. */
33941 static tree
33942 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33943 const char *where, cp_token *pragma_tok,
33944 bool finish_p = true)
33946 tree clauses = NULL;
33947 bool first = true;
33948 cp_token *token = NULL;
33950 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33952 pragma_omp_clause c_kind;
33953 const char *c_name;
33954 tree prev = clauses;
33956 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33957 cp_lexer_consume_token (parser->lexer);
33959 token = cp_lexer_peek_token (parser->lexer);
33960 c_kind = cp_parser_omp_clause_name (parser);
33962 switch (c_kind)
33964 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33965 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33966 token->location);
33967 c_name = "collapse";
33968 break;
33969 case PRAGMA_OMP_CLAUSE_COPYIN:
33970 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33971 c_name = "copyin";
33972 break;
33973 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33974 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33975 clauses);
33976 c_name = "copyprivate";
33977 break;
33978 case PRAGMA_OMP_CLAUSE_DEFAULT:
33979 clauses = cp_parser_omp_clause_default (parser, clauses,
33980 token->location, false);
33981 c_name = "default";
33982 break;
33983 case PRAGMA_OMP_CLAUSE_FINAL:
33984 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33985 c_name = "final";
33986 break;
33987 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33988 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33989 clauses);
33990 c_name = "firstprivate";
33991 break;
33992 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33993 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33994 token->location);
33995 c_name = "grainsize";
33996 break;
33997 case PRAGMA_OMP_CLAUSE_HINT:
33998 clauses = cp_parser_omp_clause_hint (parser, clauses,
33999 token->location);
34000 c_name = "hint";
34001 break;
34002 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34003 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34004 token->location);
34005 c_name = "defaultmap";
34006 break;
34007 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34008 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34009 clauses);
34010 c_name = "use_device_ptr";
34011 break;
34012 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34013 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34014 clauses);
34015 c_name = "is_device_ptr";
34016 break;
34017 case PRAGMA_OMP_CLAUSE_IF:
34018 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34019 true);
34020 c_name = "if";
34021 break;
34022 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34023 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34024 clauses);
34025 c_name = "lastprivate";
34026 break;
34027 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34028 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34029 token->location);
34030 c_name = "mergeable";
34031 break;
34032 case PRAGMA_OMP_CLAUSE_NOWAIT:
34033 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34034 c_name = "nowait";
34035 break;
34036 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34037 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34038 token->location);
34039 c_name = "num_tasks";
34040 break;
34041 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34042 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34043 token->location);
34044 c_name = "num_threads";
34045 break;
34046 case PRAGMA_OMP_CLAUSE_ORDERED:
34047 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34048 token->location);
34049 c_name = "ordered";
34050 break;
34051 case PRAGMA_OMP_CLAUSE_PRIORITY:
34052 clauses = cp_parser_omp_clause_priority (parser, clauses,
34053 token->location);
34054 c_name = "priority";
34055 break;
34056 case PRAGMA_OMP_CLAUSE_PRIVATE:
34057 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34058 clauses);
34059 c_name = "private";
34060 break;
34061 case PRAGMA_OMP_CLAUSE_REDUCTION:
34062 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34063 c_name = "reduction";
34064 break;
34065 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34066 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34067 token->location);
34068 c_name = "schedule";
34069 break;
34070 case PRAGMA_OMP_CLAUSE_SHARED:
34071 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34072 clauses);
34073 c_name = "shared";
34074 break;
34075 case PRAGMA_OMP_CLAUSE_UNTIED:
34076 clauses = cp_parser_omp_clause_untied (parser, clauses,
34077 token->location);
34078 c_name = "untied";
34079 break;
34080 case PRAGMA_OMP_CLAUSE_INBRANCH:
34081 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34082 clauses, token->location);
34083 c_name = "inbranch";
34084 break;
34085 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34086 clauses = cp_parser_omp_clause_branch (parser,
34087 OMP_CLAUSE_NOTINBRANCH,
34088 clauses, token->location);
34089 c_name = "notinbranch";
34090 break;
34091 case PRAGMA_OMP_CLAUSE_PARALLEL:
34092 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34093 clauses, token->location);
34094 c_name = "parallel";
34095 if (!first)
34097 clause_not_first:
34098 error_at (token->location, "%qs must be the first clause of %qs",
34099 c_name, where);
34100 clauses = prev;
34102 break;
34103 case PRAGMA_OMP_CLAUSE_FOR:
34104 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34105 clauses, token->location);
34106 c_name = "for";
34107 if (!first)
34108 goto clause_not_first;
34109 break;
34110 case PRAGMA_OMP_CLAUSE_SECTIONS:
34111 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34112 clauses, token->location);
34113 c_name = "sections";
34114 if (!first)
34115 goto clause_not_first;
34116 break;
34117 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34118 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34119 clauses, token->location);
34120 c_name = "taskgroup";
34121 if (!first)
34122 goto clause_not_first;
34123 break;
34124 case PRAGMA_OMP_CLAUSE_LINK:
34125 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34126 c_name = "to";
34127 break;
34128 case PRAGMA_OMP_CLAUSE_TO:
34129 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34130 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34131 clauses);
34132 else
34133 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34134 c_name = "to";
34135 break;
34136 case PRAGMA_OMP_CLAUSE_FROM:
34137 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34138 c_name = "from";
34139 break;
34140 case PRAGMA_OMP_CLAUSE_UNIFORM:
34141 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34142 clauses);
34143 c_name = "uniform";
34144 break;
34145 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34146 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34147 token->location);
34148 c_name = "num_teams";
34149 break;
34150 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34151 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34152 token->location);
34153 c_name = "thread_limit";
34154 break;
34155 case PRAGMA_OMP_CLAUSE_ALIGNED:
34156 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34157 c_name = "aligned";
34158 break;
34159 case PRAGMA_OMP_CLAUSE_LINEAR:
34161 bool declare_simd = false;
34162 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34163 declare_simd = true;
34164 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34166 c_name = "linear";
34167 break;
34168 case PRAGMA_OMP_CLAUSE_DEPEND:
34169 clauses = cp_parser_omp_clause_depend (parser, clauses,
34170 token->location);
34171 c_name = "depend";
34172 break;
34173 case PRAGMA_OMP_CLAUSE_MAP:
34174 clauses = cp_parser_omp_clause_map (parser, clauses);
34175 c_name = "map";
34176 break;
34177 case PRAGMA_OMP_CLAUSE_DEVICE:
34178 clauses = cp_parser_omp_clause_device (parser, clauses,
34179 token->location);
34180 c_name = "device";
34181 break;
34182 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34183 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34184 token->location);
34185 c_name = "dist_schedule";
34186 break;
34187 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34188 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34189 token->location);
34190 c_name = "proc_bind";
34191 break;
34192 case PRAGMA_OMP_CLAUSE_SAFELEN:
34193 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34194 token->location);
34195 c_name = "safelen";
34196 break;
34197 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34198 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34199 token->location);
34200 c_name = "simdlen";
34201 break;
34202 case PRAGMA_OMP_CLAUSE_NOGROUP:
34203 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34204 token->location);
34205 c_name = "nogroup";
34206 break;
34207 case PRAGMA_OMP_CLAUSE_THREADS:
34208 clauses
34209 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34210 clauses, token->location);
34211 c_name = "threads";
34212 break;
34213 case PRAGMA_OMP_CLAUSE_SIMD:
34214 clauses
34215 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34216 clauses, token->location);
34217 c_name = "simd";
34218 break;
34219 default:
34220 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34221 goto saw_error;
34224 first = false;
34226 if (((mask >> c_kind) & 1) == 0)
34228 /* Remove the invalid clause(s) from the list to avoid
34229 confusing the rest of the compiler. */
34230 clauses = prev;
34231 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34234 saw_error:
34235 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34236 if (finish_p)
34238 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34239 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34240 else
34241 return finish_omp_clauses (clauses, C_ORT_OMP);
34243 return clauses;
34246 /* OpenMP 2.5:
34247 structured-block:
34248 statement
34250 In practice, we're also interested in adding the statement to an
34251 outer node. So it is convenient if we work around the fact that
34252 cp_parser_statement calls add_stmt. */
34254 static unsigned
34255 cp_parser_begin_omp_structured_block (cp_parser *parser)
34257 unsigned save = parser->in_statement;
34259 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34260 This preserves the "not within loop or switch" style error messages
34261 for nonsense cases like
34262 void foo() {
34263 #pragma omp single
34264 break;
34267 if (parser->in_statement)
34268 parser->in_statement = IN_OMP_BLOCK;
34270 return save;
34273 static void
34274 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34276 parser->in_statement = save;
34279 static tree
34280 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34282 tree stmt = begin_omp_structured_block ();
34283 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34285 cp_parser_statement (parser, NULL_TREE, false, if_p);
34287 cp_parser_end_omp_structured_block (parser, save);
34288 return finish_omp_structured_block (stmt);
34291 /* OpenMP 2.5:
34292 # pragma omp atomic new-line
34293 expression-stmt
34295 expression-stmt:
34296 x binop= expr | x++ | ++x | x-- | --x
34297 binop:
34298 +, *, -, /, &, ^, |, <<, >>
34300 where x is an lvalue expression with scalar type.
34302 OpenMP 3.1:
34303 # pragma omp atomic new-line
34304 update-stmt
34306 # pragma omp atomic read new-line
34307 read-stmt
34309 # pragma omp atomic write new-line
34310 write-stmt
34312 # pragma omp atomic update new-line
34313 update-stmt
34315 # pragma omp atomic capture new-line
34316 capture-stmt
34318 # pragma omp atomic capture new-line
34319 capture-block
34321 read-stmt:
34322 v = x
34323 write-stmt:
34324 x = expr
34325 update-stmt:
34326 expression-stmt | x = x binop expr
34327 capture-stmt:
34328 v = expression-stmt
34329 capture-block:
34330 { v = x; update-stmt; } | { update-stmt; v = x; }
34332 OpenMP 4.0:
34333 update-stmt:
34334 expression-stmt | x = x binop expr | x = expr binop x
34335 capture-stmt:
34336 v = update-stmt
34337 capture-block:
34338 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34340 where x and v are lvalue expressions with scalar type. */
34342 static void
34343 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34345 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34346 tree rhs1 = NULL_TREE, orig_lhs;
34347 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34348 bool structured_block = false;
34349 bool seq_cst = false;
34351 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34353 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34354 const char *p = IDENTIFIER_POINTER (id);
34356 if (!strcmp (p, "seq_cst"))
34358 seq_cst = true;
34359 cp_lexer_consume_token (parser->lexer);
34360 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34361 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34362 cp_lexer_consume_token (parser->lexer);
34365 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34367 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34368 const char *p = IDENTIFIER_POINTER (id);
34370 if (!strcmp (p, "read"))
34371 code = OMP_ATOMIC_READ;
34372 else if (!strcmp (p, "write"))
34373 code = NOP_EXPR;
34374 else if (!strcmp (p, "update"))
34375 code = OMP_ATOMIC;
34376 else if (!strcmp (p, "capture"))
34377 code = OMP_ATOMIC_CAPTURE_NEW;
34378 else
34379 p = NULL;
34380 if (p)
34381 cp_lexer_consume_token (parser->lexer);
34383 if (!seq_cst)
34385 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34386 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34387 cp_lexer_consume_token (parser->lexer);
34389 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34391 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34392 const char *p = IDENTIFIER_POINTER (id);
34394 if (!strcmp (p, "seq_cst"))
34396 seq_cst = true;
34397 cp_lexer_consume_token (parser->lexer);
34401 cp_parser_require_pragma_eol (parser, pragma_tok);
34403 switch (code)
34405 case OMP_ATOMIC_READ:
34406 case NOP_EXPR: /* atomic write */
34407 v = cp_parser_unary_expression (parser);
34408 if (v == error_mark_node)
34409 goto saw_error;
34410 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34411 goto saw_error;
34412 if (code == NOP_EXPR)
34413 lhs = cp_parser_expression (parser);
34414 else
34415 lhs = cp_parser_unary_expression (parser);
34416 if (lhs == error_mark_node)
34417 goto saw_error;
34418 if (code == NOP_EXPR)
34420 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34421 opcode. */
34422 code = OMP_ATOMIC;
34423 rhs = lhs;
34424 lhs = v;
34425 v = NULL_TREE;
34427 goto done;
34428 case OMP_ATOMIC_CAPTURE_NEW:
34429 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34431 cp_lexer_consume_token (parser->lexer);
34432 structured_block = true;
34434 else
34436 v = cp_parser_unary_expression (parser);
34437 if (v == error_mark_node)
34438 goto saw_error;
34439 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34440 goto saw_error;
34442 default:
34443 break;
34446 restart:
34447 lhs = cp_parser_unary_expression (parser);
34448 orig_lhs = lhs;
34449 switch (TREE_CODE (lhs))
34451 case ERROR_MARK:
34452 goto saw_error;
34454 case POSTINCREMENT_EXPR:
34455 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34456 code = OMP_ATOMIC_CAPTURE_OLD;
34457 /* FALLTHROUGH */
34458 case PREINCREMENT_EXPR:
34459 lhs = TREE_OPERAND (lhs, 0);
34460 opcode = PLUS_EXPR;
34461 rhs = integer_one_node;
34462 break;
34464 case POSTDECREMENT_EXPR:
34465 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34466 code = OMP_ATOMIC_CAPTURE_OLD;
34467 /* FALLTHROUGH */
34468 case PREDECREMENT_EXPR:
34469 lhs = TREE_OPERAND (lhs, 0);
34470 opcode = MINUS_EXPR;
34471 rhs = integer_one_node;
34472 break;
34474 case COMPOUND_EXPR:
34475 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34476 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34477 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34478 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34479 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34480 (TREE_OPERAND (lhs, 1), 0), 0)))
34481 == BOOLEAN_TYPE)
34482 /* Undo effects of boolean_increment for post {in,de}crement. */
34483 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34484 /* FALLTHRU */
34485 case MODIFY_EXPR:
34486 if (TREE_CODE (lhs) == MODIFY_EXPR
34487 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34489 /* Undo effects of boolean_increment. */
34490 if (integer_onep (TREE_OPERAND (lhs, 1)))
34492 /* This is pre or post increment. */
34493 rhs = TREE_OPERAND (lhs, 1);
34494 lhs = TREE_OPERAND (lhs, 0);
34495 opcode = NOP_EXPR;
34496 if (code == OMP_ATOMIC_CAPTURE_NEW
34497 && !structured_block
34498 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34499 code = OMP_ATOMIC_CAPTURE_OLD;
34500 break;
34503 /* FALLTHRU */
34504 default:
34505 switch (cp_lexer_peek_token (parser->lexer)->type)
34507 case CPP_MULT_EQ:
34508 opcode = MULT_EXPR;
34509 break;
34510 case CPP_DIV_EQ:
34511 opcode = TRUNC_DIV_EXPR;
34512 break;
34513 case CPP_PLUS_EQ:
34514 opcode = PLUS_EXPR;
34515 break;
34516 case CPP_MINUS_EQ:
34517 opcode = MINUS_EXPR;
34518 break;
34519 case CPP_LSHIFT_EQ:
34520 opcode = LSHIFT_EXPR;
34521 break;
34522 case CPP_RSHIFT_EQ:
34523 opcode = RSHIFT_EXPR;
34524 break;
34525 case CPP_AND_EQ:
34526 opcode = BIT_AND_EXPR;
34527 break;
34528 case CPP_OR_EQ:
34529 opcode = BIT_IOR_EXPR;
34530 break;
34531 case CPP_XOR_EQ:
34532 opcode = BIT_XOR_EXPR;
34533 break;
34534 case CPP_EQ:
34535 enum cp_parser_prec oprec;
34536 cp_token *token;
34537 cp_lexer_consume_token (parser->lexer);
34538 cp_parser_parse_tentatively (parser);
34539 rhs1 = cp_parser_simple_cast_expression (parser);
34540 if (rhs1 == error_mark_node)
34542 cp_parser_abort_tentative_parse (parser);
34543 cp_parser_simple_cast_expression (parser);
34544 goto saw_error;
34546 token = cp_lexer_peek_token (parser->lexer);
34547 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34549 cp_parser_abort_tentative_parse (parser);
34550 cp_parser_parse_tentatively (parser);
34551 rhs = cp_parser_binary_expression (parser, false, true,
34552 PREC_NOT_OPERATOR, NULL);
34553 if (rhs == error_mark_node)
34555 cp_parser_abort_tentative_parse (parser);
34556 cp_parser_binary_expression (parser, false, true,
34557 PREC_NOT_OPERATOR, NULL);
34558 goto saw_error;
34560 switch (TREE_CODE (rhs))
34562 case MULT_EXPR:
34563 case TRUNC_DIV_EXPR:
34564 case RDIV_EXPR:
34565 case PLUS_EXPR:
34566 case MINUS_EXPR:
34567 case LSHIFT_EXPR:
34568 case RSHIFT_EXPR:
34569 case BIT_AND_EXPR:
34570 case BIT_IOR_EXPR:
34571 case BIT_XOR_EXPR:
34572 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34574 if (cp_parser_parse_definitely (parser))
34576 opcode = TREE_CODE (rhs);
34577 rhs1 = TREE_OPERAND (rhs, 0);
34578 rhs = TREE_OPERAND (rhs, 1);
34579 goto stmt_done;
34581 else
34582 goto saw_error;
34584 break;
34585 default:
34586 break;
34588 cp_parser_abort_tentative_parse (parser);
34589 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34591 rhs = cp_parser_expression (parser);
34592 if (rhs == error_mark_node)
34593 goto saw_error;
34594 opcode = NOP_EXPR;
34595 rhs1 = NULL_TREE;
34596 goto stmt_done;
34598 cp_parser_error (parser,
34599 "invalid form of %<#pragma omp atomic%>");
34600 goto saw_error;
34602 if (!cp_parser_parse_definitely (parser))
34603 goto saw_error;
34604 switch (token->type)
34606 case CPP_SEMICOLON:
34607 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34609 code = OMP_ATOMIC_CAPTURE_OLD;
34610 v = lhs;
34611 lhs = NULL_TREE;
34612 lhs1 = rhs1;
34613 rhs1 = NULL_TREE;
34614 cp_lexer_consume_token (parser->lexer);
34615 goto restart;
34617 else if (structured_block)
34619 opcode = NOP_EXPR;
34620 rhs = rhs1;
34621 rhs1 = NULL_TREE;
34622 goto stmt_done;
34624 cp_parser_error (parser,
34625 "invalid form of %<#pragma omp atomic%>");
34626 goto saw_error;
34627 case CPP_MULT:
34628 opcode = MULT_EXPR;
34629 break;
34630 case CPP_DIV:
34631 opcode = TRUNC_DIV_EXPR;
34632 break;
34633 case CPP_PLUS:
34634 opcode = PLUS_EXPR;
34635 break;
34636 case CPP_MINUS:
34637 opcode = MINUS_EXPR;
34638 break;
34639 case CPP_LSHIFT:
34640 opcode = LSHIFT_EXPR;
34641 break;
34642 case CPP_RSHIFT:
34643 opcode = RSHIFT_EXPR;
34644 break;
34645 case CPP_AND:
34646 opcode = BIT_AND_EXPR;
34647 break;
34648 case CPP_OR:
34649 opcode = BIT_IOR_EXPR;
34650 break;
34651 case CPP_XOR:
34652 opcode = BIT_XOR_EXPR;
34653 break;
34654 default:
34655 cp_parser_error (parser,
34656 "invalid operator for %<#pragma omp atomic%>");
34657 goto saw_error;
34659 oprec = TOKEN_PRECEDENCE (token);
34660 gcc_assert (oprec != PREC_NOT_OPERATOR);
34661 if (commutative_tree_code (opcode))
34662 oprec = (enum cp_parser_prec) (oprec - 1);
34663 cp_lexer_consume_token (parser->lexer);
34664 rhs = cp_parser_binary_expression (parser, false, false,
34665 oprec, NULL);
34666 if (rhs == error_mark_node)
34667 goto saw_error;
34668 goto stmt_done;
34669 /* FALLTHROUGH */
34670 default:
34671 cp_parser_error (parser,
34672 "invalid operator for %<#pragma omp atomic%>");
34673 goto saw_error;
34675 cp_lexer_consume_token (parser->lexer);
34677 rhs = cp_parser_expression (parser);
34678 if (rhs == error_mark_node)
34679 goto saw_error;
34680 break;
34682 stmt_done:
34683 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34685 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34686 goto saw_error;
34687 v = cp_parser_unary_expression (parser);
34688 if (v == error_mark_node)
34689 goto saw_error;
34690 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34691 goto saw_error;
34692 lhs1 = cp_parser_unary_expression (parser);
34693 if (lhs1 == error_mark_node)
34694 goto saw_error;
34696 if (structured_block)
34698 cp_parser_consume_semicolon_at_end_of_statement (parser);
34699 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34701 done:
34702 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34703 if (!structured_block)
34704 cp_parser_consume_semicolon_at_end_of_statement (parser);
34705 return;
34707 saw_error:
34708 cp_parser_skip_to_end_of_block_or_statement (parser);
34709 if (structured_block)
34711 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34712 cp_lexer_consume_token (parser->lexer);
34713 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34715 cp_parser_skip_to_end_of_block_or_statement (parser);
34716 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34717 cp_lexer_consume_token (parser->lexer);
34723 /* OpenMP 2.5:
34724 # pragma omp barrier new-line */
34726 static void
34727 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34729 cp_parser_require_pragma_eol (parser, pragma_tok);
34730 finish_omp_barrier ();
34733 /* OpenMP 2.5:
34734 # pragma omp critical [(name)] new-line
34735 structured-block
34737 OpenMP 4.5:
34738 # pragma omp critical [(name) [hint(expression)]] new-line
34739 structured-block */
34741 #define OMP_CRITICAL_CLAUSE_MASK \
34742 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34744 static tree
34745 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34747 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34749 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34751 matching_parens parens;
34752 parens.consume_open (parser);
34754 name = cp_parser_identifier (parser);
34756 if (name == error_mark_node
34757 || !parens.require_close (parser))
34758 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34759 /*or_comma=*/false,
34760 /*consume_paren=*/true);
34761 if (name == error_mark_node)
34762 name = NULL;
34764 clauses = cp_parser_omp_all_clauses (parser,
34765 OMP_CRITICAL_CLAUSE_MASK,
34766 "#pragma omp critical", pragma_tok);
34768 else
34769 cp_parser_require_pragma_eol (parser, pragma_tok);
34771 stmt = cp_parser_omp_structured_block (parser, if_p);
34772 return c_finish_omp_critical (input_location, stmt, name, clauses);
34775 /* OpenMP 2.5:
34776 # pragma omp flush flush-vars[opt] new-line
34778 flush-vars:
34779 ( variable-list ) */
34781 static void
34782 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34785 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34786 cp_parser_require_pragma_eol (parser, pragma_tok);
34788 finish_omp_flush ();
34791 /* Helper function, to parse omp for increment expression. */
34793 static tree
34794 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34796 tree cond = cp_parser_binary_expression (parser, false, true,
34797 PREC_NOT_OPERATOR, NULL);
34798 if (cond == error_mark_node
34799 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34801 cp_parser_skip_to_end_of_statement (parser);
34802 return error_mark_node;
34805 switch (TREE_CODE (cond))
34807 case GT_EXPR:
34808 case GE_EXPR:
34809 case LT_EXPR:
34810 case LE_EXPR:
34811 break;
34812 case NE_EXPR:
34813 /* Fall through: OpenMP disallows NE_EXPR. */
34814 gcc_fallthrough ();
34815 default:
34816 return error_mark_node;
34819 /* If decl is an iterator, preserve LHS and RHS of the relational
34820 expr until finish_omp_for. */
34821 if (decl
34822 && (type_dependent_expression_p (decl)
34823 || CLASS_TYPE_P (TREE_TYPE (decl))))
34824 return cond;
34826 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
34827 TREE_CODE (cond),
34828 TREE_OPERAND (cond, 0), ERROR_MARK,
34829 TREE_OPERAND (cond, 1), ERROR_MARK,
34830 /*overload=*/NULL, tf_warning_or_error);
34833 /* Helper function, to parse omp for increment expression. */
34835 static tree
34836 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34838 cp_token *token = cp_lexer_peek_token (parser->lexer);
34839 enum tree_code op;
34840 tree lhs, rhs;
34841 cp_id_kind idk;
34842 bool decl_first;
34844 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34846 op = (token->type == CPP_PLUS_PLUS
34847 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34848 cp_lexer_consume_token (parser->lexer);
34849 lhs = cp_parser_simple_cast_expression (parser);
34850 if (lhs != decl
34851 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34852 return error_mark_node;
34853 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34856 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34857 if (lhs != decl
34858 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34859 return error_mark_node;
34861 token = cp_lexer_peek_token (parser->lexer);
34862 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34864 op = (token->type == CPP_PLUS_PLUS
34865 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34866 cp_lexer_consume_token (parser->lexer);
34867 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34870 op = cp_parser_assignment_operator_opt (parser);
34871 if (op == ERROR_MARK)
34872 return error_mark_node;
34874 if (op != NOP_EXPR)
34876 rhs = cp_parser_assignment_expression (parser);
34877 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34878 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34881 lhs = cp_parser_binary_expression (parser, false, false,
34882 PREC_ADDITIVE_EXPRESSION, NULL);
34883 token = cp_lexer_peek_token (parser->lexer);
34884 decl_first = (lhs == decl
34885 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34886 if (decl_first)
34887 lhs = NULL_TREE;
34888 if (token->type != CPP_PLUS
34889 && token->type != CPP_MINUS)
34890 return error_mark_node;
34894 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34895 cp_lexer_consume_token (parser->lexer);
34896 rhs = cp_parser_binary_expression (parser, false, false,
34897 PREC_ADDITIVE_EXPRESSION, NULL);
34898 token = cp_lexer_peek_token (parser->lexer);
34899 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34901 if (lhs == NULL_TREE)
34903 if (op == PLUS_EXPR)
34904 lhs = rhs;
34905 else
34906 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34907 tf_warning_or_error);
34909 else
34910 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34911 ERROR_MARK, NULL, tf_warning_or_error);
34914 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34916 if (!decl_first)
34918 if ((rhs != decl
34919 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34920 || op == MINUS_EXPR)
34921 return error_mark_node;
34922 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34924 else
34925 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34927 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34930 /* Parse the initialization statement of an OpenMP for loop.
34932 Return true if the resulting construct should have an
34933 OMP_CLAUSE_PRIVATE added to it. */
34935 static tree
34936 cp_parser_omp_for_loop_init (cp_parser *parser,
34937 tree &this_pre_body,
34938 vec<tree, va_gc> *&for_block,
34939 tree &init,
34940 tree &orig_init,
34941 tree &decl,
34942 tree &real_decl)
34944 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34945 return NULL_TREE;
34947 tree add_private_clause = NULL_TREE;
34949 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34951 init-expr:
34952 var = lb
34953 integer-type var = lb
34954 random-access-iterator-type var = lb
34955 pointer-type var = lb
34957 cp_decl_specifier_seq type_specifiers;
34959 /* First, try to parse as an initialized declaration. See
34960 cp_parser_condition, from whence the bulk of this is copied. */
34962 cp_parser_parse_tentatively (parser);
34963 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34964 /*is_trailing_return=*/false,
34965 &type_specifiers);
34966 if (cp_parser_parse_definitely (parser))
34968 /* If parsing a type specifier seq succeeded, then this
34969 MUST be a initialized declaration. */
34970 tree asm_specification, attributes;
34971 cp_declarator *declarator;
34973 declarator = cp_parser_declarator (parser,
34974 CP_PARSER_DECLARATOR_NAMED,
34975 /*ctor_dtor_or_conv_p=*/NULL,
34976 /*parenthesized_p=*/NULL,
34977 /*member_p=*/false,
34978 /*friend_p=*/false);
34979 attributes = cp_parser_attributes_opt (parser);
34980 asm_specification = cp_parser_asm_specification_opt (parser);
34982 if (declarator == cp_error_declarator)
34983 cp_parser_skip_to_end_of_statement (parser);
34985 else
34987 tree pushed_scope, auto_node;
34989 decl = start_decl (declarator, &type_specifiers,
34990 SD_INITIALIZED, attributes,
34991 /*prefix_attributes=*/NULL_TREE,
34992 &pushed_scope);
34994 auto_node = type_uses_auto (TREE_TYPE (decl));
34995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34997 if (cp_lexer_next_token_is (parser->lexer,
34998 CPP_OPEN_PAREN))
34999 error ("parenthesized initialization is not allowed in "
35000 "OpenMP %<for%> loop");
35001 else
35002 /* Trigger an error. */
35003 cp_parser_require (parser, CPP_EQ, RT_EQ);
35005 init = error_mark_node;
35006 cp_parser_skip_to_end_of_statement (parser);
35008 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35009 || type_dependent_expression_p (decl)
35010 || auto_node)
35012 bool is_direct_init, is_non_constant_init;
35014 init = cp_parser_initializer (parser,
35015 &is_direct_init,
35016 &is_non_constant_init);
35018 if (auto_node)
35020 TREE_TYPE (decl)
35021 = do_auto_deduction (TREE_TYPE (decl), init,
35022 auto_node);
35024 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35025 && !type_dependent_expression_p (decl))
35026 goto non_class;
35029 cp_finish_decl (decl, init, !is_non_constant_init,
35030 asm_specification,
35031 LOOKUP_ONLYCONVERTING);
35032 orig_init = init;
35033 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35035 vec_safe_push (for_block, this_pre_body);
35036 init = NULL_TREE;
35038 else
35040 init = pop_stmt_list (this_pre_body);
35041 if (init && TREE_CODE (init) == STATEMENT_LIST)
35043 tree_stmt_iterator i = tsi_start (init);
35044 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35045 while (!tsi_end_p (i))
35047 tree t = tsi_stmt (i);
35048 if (TREE_CODE (t) == DECL_EXPR
35049 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35051 tsi_delink (&i);
35052 vec_safe_push (for_block, t);
35053 continue;
35055 break;
35057 if (tsi_one_before_end_p (i))
35059 tree t = tsi_stmt (i);
35060 tsi_delink (&i);
35061 free_stmt_list (init);
35062 init = t;
35066 this_pre_body = NULL_TREE;
35068 else
35070 /* Consume '='. */
35071 cp_lexer_consume_token (parser->lexer);
35072 init = cp_parser_assignment_expression (parser);
35074 non_class:
35075 if (TYPE_REF_P (TREE_TYPE (decl)))
35076 init = error_mark_node;
35077 else
35078 cp_finish_decl (decl, NULL_TREE,
35079 /*init_const_expr_p=*/false,
35080 asm_specification,
35081 LOOKUP_ONLYCONVERTING);
35084 if (pushed_scope)
35085 pop_scope (pushed_scope);
35088 else
35090 cp_id_kind idk;
35091 /* If parsing a type specifier sequence failed, then
35092 this MUST be a simple expression. */
35093 cp_parser_parse_tentatively (parser);
35094 decl = cp_parser_primary_expression (parser, false, false,
35095 false, &idk);
35096 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35097 if (!cp_parser_error_occurred (parser)
35098 && decl
35099 && (TREE_CODE (decl) == COMPONENT_REF
35100 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35102 cp_parser_abort_tentative_parse (parser);
35103 cp_parser_parse_tentatively (parser);
35104 cp_token *token = cp_lexer_peek_token (parser->lexer);
35105 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35106 /*check_dependency_p=*/true,
35107 /*template_p=*/NULL,
35108 /*declarator_p=*/false,
35109 /*optional_p=*/false);
35110 if (name != error_mark_node
35111 && last_tok == cp_lexer_peek_token (parser->lexer))
35113 decl = cp_parser_lookup_name_simple (parser, name,
35114 token->location);
35115 if (TREE_CODE (decl) == FIELD_DECL)
35116 add_private_clause = omp_privatize_field (decl, false);
35118 cp_parser_abort_tentative_parse (parser);
35119 cp_parser_parse_tentatively (parser);
35120 decl = cp_parser_primary_expression (parser, false, false,
35121 false, &idk);
35123 if (!cp_parser_error_occurred (parser)
35124 && decl
35125 && DECL_P (decl)
35126 && CLASS_TYPE_P (TREE_TYPE (decl)))
35128 tree rhs;
35130 cp_parser_parse_definitely (parser);
35131 cp_parser_require (parser, CPP_EQ, RT_EQ);
35132 rhs = cp_parser_assignment_expression (parser);
35133 orig_init = rhs;
35134 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35135 decl, NOP_EXPR,
35136 rhs,
35137 tf_warning_or_error));
35138 if (!add_private_clause)
35139 add_private_clause = decl;
35141 else
35143 decl = NULL;
35144 cp_parser_abort_tentative_parse (parser);
35145 init = cp_parser_expression (parser);
35146 if (init)
35148 if (TREE_CODE (init) == MODIFY_EXPR
35149 || TREE_CODE (init) == MODOP_EXPR)
35150 real_decl = TREE_OPERAND (init, 0);
35154 return add_private_clause;
35157 /* Parse the restricted form of the for statement allowed by OpenMP. */
35159 static tree
35160 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35161 tree *cclauses, bool *if_p)
35163 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35164 tree real_decl, initv, condv, incrv, declv;
35165 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35166 location_t loc_first;
35167 bool collapse_err = false;
35168 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35169 vec<tree, va_gc> *for_block = make_tree_vector ();
35170 auto_vec<tree, 4> orig_inits;
35171 bool tiling = false;
35173 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35174 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35175 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35176 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35178 tiling = true;
35179 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35181 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35182 && OMP_CLAUSE_ORDERED_EXPR (cl))
35184 ordered_cl = cl;
35185 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35188 if (ordered && ordered < collapse)
35190 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35191 "%<ordered%> clause parameter is less than %<collapse%>");
35192 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35193 = build_int_cst (NULL_TREE, collapse);
35194 ordered = collapse;
35196 if (ordered)
35198 for (tree *pc = &clauses; *pc; )
35199 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35201 error_at (OMP_CLAUSE_LOCATION (*pc),
35202 "%<linear%> clause may not be specified together "
35203 "with %<ordered%> clause with a parameter");
35204 *pc = OMP_CLAUSE_CHAIN (*pc);
35206 else
35207 pc = &OMP_CLAUSE_CHAIN (*pc);
35210 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35211 count = ordered ? ordered : collapse;
35213 declv = make_tree_vec (count);
35214 initv = make_tree_vec (count);
35215 condv = make_tree_vec (count);
35216 incrv = make_tree_vec (count);
35218 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35220 for (i = 0; i < count; i++)
35222 int bracecount = 0;
35223 tree add_private_clause = NULL_TREE;
35224 location_t loc;
35226 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35228 if (!collapse_err)
35229 cp_parser_error (parser, "for statement expected");
35230 return NULL;
35232 loc = cp_lexer_consume_token (parser->lexer)->location;
35234 matching_parens parens;
35235 if (!parens.require_open (parser))
35236 return NULL;
35238 init = orig_init = decl = real_decl = NULL;
35239 this_pre_body = push_stmt_list ();
35241 add_private_clause
35242 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35243 init, orig_init, decl, real_decl);
35245 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35246 if (this_pre_body)
35248 this_pre_body = pop_stmt_list (this_pre_body);
35249 if (pre_body)
35251 tree t = pre_body;
35252 pre_body = push_stmt_list ();
35253 add_stmt (t);
35254 add_stmt (this_pre_body);
35255 pre_body = pop_stmt_list (pre_body);
35257 else
35258 pre_body = this_pre_body;
35261 if (decl)
35262 real_decl = decl;
35263 if (cclauses != NULL
35264 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35265 && real_decl != NULL_TREE)
35267 tree *c;
35268 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35269 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35270 && OMP_CLAUSE_DECL (*c) == real_decl)
35272 error_at (loc, "iteration variable %qD"
35273 " should not be firstprivate", real_decl);
35274 *c = OMP_CLAUSE_CHAIN (*c);
35276 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35277 && OMP_CLAUSE_DECL (*c) == real_decl)
35279 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35280 tree l = *c;
35281 *c = OMP_CLAUSE_CHAIN (*c);
35282 if (code == OMP_SIMD)
35284 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35285 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35287 else
35289 OMP_CLAUSE_CHAIN (l) = clauses;
35290 clauses = l;
35292 add_private_clause = NULL_TREE;
35294 else
35296 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35297 && OMP_CLAUSE_DECL (*c) == real_decl)
35298 add_private_clause = NULL_TREE;
35299 c = &OMP_CLAUSE_CHAIN (*c);
35303 if (add_private_clause)
35305 tree c;
35306 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35308 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35309 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35310 && OMP_CLAUSE_DECL (c) == decl)
35311 break;
35312 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35313 && OMP_CLAUSE_DECL (c) == decl)
35314 error_at (loc, "iteration variable %qD "
35315 "should not be firstprivate",
35316 decl);
35317 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35318 && OMP_CLAUSE_DECL (c) == decl)
35319 error_at (loc, "iteration variable %qD should not be reduction",
35320 decl);
35322 if (c == NULL)
35324 if (code != OMP_SIMD)
35325 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35326 else if (collapse == 1)
35327 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35328 else
35329 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35330 OMP_CLAUSE_DECL (c) = add_private_clause;
35331 c = finish_omp_clauses (c, C_ORT_OMP);
35332 if (c)
35334 OMP_CLAUSE_CHAIN (c) = clauses;
35335 clauses = c;
35336 /* For linear, signal that we need to fill up
35337 the so far unknown linear step. */
35338 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35339 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35344 cond = NULL;
35345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35346 cond = cp_parser_omp_for_cond (parser, decl);
35347 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35349 incr = NULL;
35350 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35352 /* If decl is an iterator, preserve the operator on decl
35353 until finish_omp_for. */
35354 if (real_decl
35355 && ((processing_template_decl
35356 && (TREE_TYPE (real_decl) == NULL_TREE
35357 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
35358 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35359 incr = cp_parser_omp_for_incr (parser, real_decl);
35360 else
35361 incr = cp_parser_expression (parser);
35362 if (!EXPR_HAS_LOCATION (incr))
35363 protected_set_expr_location (incr, input_location);
35366 if (!parens.require_close (parser))
35367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35368 /*or_comma=*/false,
35369 /*consume_paren=*/true);
35371 TREE_VEC_ELT (declv, i) = decl;
35372 TREE_VEC_ELT (initv, i) = init;
35373 TREE_VEC_ELT (condv, i) = cond;
35374 TREE_VEC_ELT (incrv, i) = incr;
35375 if (orig_init)
35377 orig_inits.safe_grow_cleared (i + 1);
35378 orig_inits[i] = orig_init;
35381 if (i == count - 1)
35382 break;
35384 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35385 in between the collapsed for loops to be still considered perfectly
35386 nested. Hopefully the final version clarifies this.
35387 For now handle (multiple) {'s and empty statements. */
35388 cp_parser_parse_tentatively (parser);
35389 for (;;)
35391 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35392 break;
35393 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35395 cp_lexer_consume_token (parser->lexer);
35396 bracecount++;
35398 else if (bracecount
35399 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35400 cp_lexer_consume_token (parser->lexer);
35401 else
35403 loc = cp_lexer_peek_token (parser->lexer)->location;
35404 error_at (loc, "not enough for loops to collapse");
35405 collapse_err = true;
35406 cp_parser_abort_tentative_parse (parser);
35407 declv = NULL_TREE;
35408 break;
35412 if (declv)
35414 cp_parser_parse_definitely (parser);
35415 nbraces += bracecount;
35419 if (nbraces)
35420 if_p = NULL;
35422 /* Note that we saved the original contents of this flag when we entered
35423 the structured block, and so we don't need to re-save it here. */
35424 parser->in_statement = IN_OMP_FOR;
35426 /* Note that the grammar doesn't call for a structured block here,
35427 though the loop as a whole is a structured block. */
35428 body = push_stmt_list ();
35429 cp_parser_statement (parser, NULL_TREE, false, if_p);
35430 body = pop_stmt_list (body);
35432 if (declv == NULL_TREE)
35433 ret = NULL_TREE;
35434 else
35435 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35436 body, pre_body, &orig_inits, clauses);
35438 while (nbraces)
35440 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35442 cp_lexer_consume_token (parser->lexer);
35443 nbraces--;
35445 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35446 cp_lexer_consume_token (parser->lexer);
35447 else
35449 if (!collapse_err)
35451 error_at (cp_lexer_peek_token (parser->lexer)->location,
35452 "collapsed loops not perfectly nested");
35454 collapse_err = true;
35455 cp_parser_statement_seq_opt (parser, NULL);
35456 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35457 break;
35461 while (!for_block->is_empty ())
35463 tree t = for_block->pop ();
35464 if (TREE_CODE (t) == STATEMENT_LIST)
35465 add_stmt (pop_stmt_list (t));
35466 else
35467 add_stmt (t);
35469 release_tree_vector (for_block);
35471 return ret;
35474 /* Helper function for OpenMP parsing, split clauses and call
35475 finish_omp_clauses on each of the set of clauses afterwards. */
35477 static void
35478 cp_omp_split_clauses (location_t loc, enum tree_code code,
35479 omp_clause_mask mask, tree clauses, tree *cclauses)
35481 int i;
35482 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35483 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35484 if (cclauses[i])
35485 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35488 /* OpenMP 4.0:
35489 #pragma omp simd simd-clause[optseq] new-line
35490 for-loop */
35492 #define OMP_SIMD_CLAUSE_MASK \
35493 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35502 static tree
35503 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35504 char *p_name, omp_clause_mask mask, tree *cclauses,
35505 bool *if_p)
35507 tree clauses, sb, ret;
35508 unsigned int save;
35509 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35511 strcat (p_name, " simd");
35512 mask |= OMP_SIMD_CLAUSE_MASK;
35514 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35515 cclauses == NULL);
35516 if (cclauses)
35518 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35519 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35520 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35521 OMP_CLAUSE_ORDERED);
35522 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35524 error_at (OMP_CLAUSE_LOCATION (c),
35525 "%<ordered%> clause with parameter may not be specified "
35526 "on %qs construct", p_name);
35527 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35531 sb = begin_omp_structured_block ();
35532 save = cp_parser_begin_omp_structured_block (parser);
35534 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35536 cp_parser_end_omp_structured_block (parser, save);
35537 add_stmt (finish_omp_structured_block (sb));
35539 return ret;
35542 /* OpenMP 2.5:
35543 #pragma omp for for-clause[optseq] new-line
35544 for-loop
35546 OpenMP 4.0:
35547 #pragma omp for simd for-simd-clause[optseq] new-line
35548 for-loop */
35550 #define OMP_FOR_CLAUSE_MASK \
35551 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35561 static tree
35562 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35563 char *p_name, omp_clause_mask mask, tree *cclauses,
35564 bool *if_p)
35566 tree clauses, sb, ret;
35567 unsigned int save;
35568 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35570 strcat (p_name, " for");
35571 mask |= OMP_FOR_CLAUSE_MASK;
35572 /* parallel for{, simd} disallows nowait clause, but for
35573 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35574 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35575 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35576 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35577 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35578 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35580 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35582 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35583 const char *p = IDENTIFIER_POINTER (id);
35585 if (strcmp (p, "simd") == 0)
35587 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35588 if (cclauses == NULL)
35589 cclauses = cclauses_buf;
35591 cp_lexer_consume_token (parser->lexer);
35592 if (!flag_openmp) /* flag_openmp_simd */
35593 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35594 cclauses, if_p);
35595 sb = begin_omp_structured_block ();
35596 save = cp_parser_begin_omp_structured_block (parser);
35597 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35598 cclauses, if_p);
35599 cp_parser_end_omp_structured_block (parser, save);
35600 tree body = finish_omp_structured_block (sb);
35601 if (ret == NULL)
35602 return ret;
35603 ret = make_node (OMP_FOR);
35604 TREE_TYPE (ret) = void_type_node;
35605 OMP_FOR_BODY (ret) = body;
35606 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35607 SET_EXPR_LOCATION (ret, loc);
35608 add_stmt (ret);
35609 return ret;
35612 if (!flag_openmp) /* flag_openmp_simd */
35614 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35615 return NULL_TREE;
35618 /* Composite distribute parallel for disallows linear clause. */
35619 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35620 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35622 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35623 cclauses == NULL);
35624 if (cclauses)
35626 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35627 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35630 sb = begin_omp_structured_block ();
35631 save = cp_parser_begin_omp_structured_block (parser);
35633 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35635 cp_parser_end_omp_structured_block (parser, save);
35636 add_stmt (finish_omp_structured_block (sb));
35638 return ret;
35641 /* OpenMP 2.5:
35642 # pragma omp master new-line
35643 structured-block */
35645 static tree
35646 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35648 cp_parser_require_pragma_eol (parser, pragma_tok);
35649 return c_finish_omp_master (input_location,
35650 cp_parser_omp_structured_block (parser, if_p));
35653 /* OpenMP 2.5:
35654 # pragma omp ordered new-line
35655 structured-block
35657 OpenMP 4.5:
35658 # pragma omp ordered ordered-clauses new-line
35659 structured-block */
35661 #define OMP_ORDERED_CLAUSE_MASK \
35662 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35665 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35666 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35668 static bool
35669 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35670 enum pragma_context context, bool *if_p)
35672 location_t loc = pragma_tok->location;
35674 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35676 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35677 const char *p = IDENTIFIER_POINTER (id);
35679 if (strcmp (p, "depend") == 0)
35681 if (!flag_openmp) /* flag_openmp_simd */
35683 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35684 return false;
35686 if (context == pragma_stmt)
35688 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35689 "%<depend%> clause may only be used in compound "
35690 "statements");
35691 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35692 return false;
35694 tree clauses
35695 = cp_parser_omp_all_clauses (parser,
35696 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35697 "#pragma omp ordered", pragma_tok);
35698 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35699 return false;
35703 tree clauses
35704 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35705 "#pragma omp ordered", pragma_tok);
35707 if (!flag_openmp /* flag_openmp_simd */
35708 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35709 return false;
35711 c_finish_omp_ordered (loc, clauses,
35712 cp_parser_omp_structured_block (parser, if_p));
35713 return true;
35716 /* OpenMP 2.5:
35718 section-scope:
35719 { section-sequence }
35721 section-sequence:
35722 section-directive[opt] structured-block
35723 section-sequence section-directive structured-block */
35725 static tree
35726 cp_parser_omp_sections_scope (cp_parser *parser)
35728 tree stmt, substmt;
35729 bool error_suppress = false;
35730 cp_token *tok;
35732 matching_braces braces;
35733 if (!braces.require_open (parser))
35734 return NULL_TREE;
35736 stmt = push_stmt_list ();
35738 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35739 != PRAGMA_OMP_SECTION)
35741 substmt = cp_parser_omp_structured_block (parser, NULL);
35742 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35743 add_stmt (substmt);
35746 while (1)
35748 tok = cp_lexer_peek_token (parser->lexer);
35749 if (tok->type == CPP_CLOSE_BRACE)
35750 break;
35751 if (tok->type == CPP_EOF)
35752 break;
35754 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35756 cp_lexer_consume_token (parser->lexer);
35757 cp_parser_require_pragma_eol (parser, tok);
35758 error_suppress = false;
35760 else if (!error_suppress)
35762 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35763 error_suppress = true;
35766 substmt = cp_parser_omp_structured_block (parser, NULL);
35767 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35768 add_stmt (substmt);
35770 braces.require_close (parser);
35772 substmt = pop_stmt_list (stmt);
35774 stmt = make_node (OMP_SECTIONS);
35775 TREE_TYPE (stmt) = void_type_node;
35776 OMP_SECTIONS_BODY (stmt) = substmt;
35778 add_stmt (stmt);
35779 return stmt;
35782 /* OpenMP 2.5:
35783 # pragma omp sections sections-clause[optseq] newline
35784 sections-scope */
35786 #define OMP_SECTIONS_CLAUSE_MASK \
35787 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35793 static tree
35794 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35795 char *p_name, omp_clause_mask mask, tree *cclauses)
35797 tree clauses, ret;
35798 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35800 strcat (p_name, " sections");
35801 mask |= OMP_SECTIONS_CLAUSE_MASK;
35802 if (cclauses)
35803 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35805 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35806 cclauses == NULL);
35807 if (cclauses)
35809 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35810 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35813 ret = cp_parser_omp_sections_scope (parser);
35814 if (ret)
35815 OMP_SECTIONS_CLAUSES (ret) = clauses;
35817 return ret;
35820 /* OpenMP 2.5:
35821 # pragma omp parallel parallel-clause[optseq] new-line
35822 structured-block
35823 # pragma omp parallel for parallel-for-clause[optseq] new-line
35824 structured-block
35825 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35826 structured-block
35828 OpenMP 4.0:
35829 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35830 structured-block */
35832 #define OMP_PARALLEL_CLAUSE_MASK \
35833 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35843 static tree
35844 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35845 char *p_name, omp_clause_mask mask, tree *cclauses,
35846 bool *if_p)
35848 tree stmt, clauses, block;
35849 unsigned int save;
35850 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35852 strcat (p_name, " parallel");
35853 mask |= OMP_PARALLEL_CLAUSE_MASK;
35854 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35855 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35856 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35857 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35859 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35861 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35862 if (cclauses == NULL)
35863 cclauses = cclauses_buf;
35865 cp_lexer_consume_token (parser->lexer);
35866 if (!flag_openmp) /* flag_openmp_simd */
35867 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35868 if_p);
35869 block = begin_omp_parallel ();
35870 save = cp_parser_begin_omp_structured_block (parser);
35871 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35872 if_p);
35873 cp_parser_end_omp_structured_block (parser, save);
35874 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35875 block);
35876 if (ret == NULL_TREE)
35877 return ret;
35878 OMP_PARALLEL_COMBINED (stmt) = 1;
35879 return stmt;
35881 /* When combined with distribute, parallel has to be followed by for.
35882 #pragma omp target parallel is allowed though. */
35883 else if (cclauses
35884 && (mask & (OMP_CLAUSE_MASK_1
35885 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35887 error_at (loc, "expected %<for%> after %qs", p_name);
35888 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35889 return NULL_TREE;
35891 else if (!flag_openmp) /* flag_openmp_simd */
35893 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35894 return NULL_TREE;
35896 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35898 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35899 const char *p = IDENTIFIER_POINTER (id);
35900 if (strcmp (p, "sections") == 0)
35902 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35903 cclauses = cclauses_buf;
35905 cp_lexer_consume_token (parser->lexer);
35906 block = begin_omp_parallel ();
35907 save = cp_parser_begin_omp_structured_block (parser);
35908 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35909 cp_parser_end_omp_structured_block (parser, save);
35910 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35911 block);
35912 OMP_PARALLEL_COMBINED (stmt) = 1;
35913 return stmt;
35917 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35918 cclauses == NULL);
35919 if (cclauses)
35921 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35922 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35925 block = begin_omp_parallel ();
35926 save = cp_parser_begin_omp_structured_block (parser);
35927 cp_parser_statement (parser, NULL_TREE, false, if_p);
35928 cp_parser_end_omp_structured_block (parser, save);
35929 stmt = finish_omp_parallel (clauses, block);
35930 return stmt;
35933 /* OpenMP 2.5:
35934 # pragma omp single single-clause[optseq] new-line
35935 structured-block */
35937 #define OMP_SINGLE_CLAUSE_MASK \
35938 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35943 static tree
35944 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35946 tree stmt = make_node (OMP_SINGLE);
35947 TREE_TYPE (stmt) = void_type_node;
35949 OMP_SINGLE_CLAUSES (stmt)
35950 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35951 "#pragma omp single", pragma_tok);
35952 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35954 return add_stmt (stmt);
35957 /* OpenMP 3.0:
35958 # pragma omp task task-clause[optseq] new-line
35959 structured-block */
35961 #define OMP_TASK_CLAUSE_MASK \
35962 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35973 static tree
35974 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35976 tree clauses, block;
35977 unsigned int save;
35979 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35980 "#pragma omp task", pragma_tok);
35981 block = begin_omp_task ();
35982 save = cp_parser_begin_omp_structured_block (parser);
35983 cp_parser_statement (parser, NULL_TREE, false, if_p);
35984 cp_parser_end_omp_structured_block (parser, save);
35985 return finish_omp_task (clauses, block);
35988 /* OpenMP 3.0:
35989 # pragma omp taskwait new-line */
35991 static void
35992 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35994 cp_parser_require_pragma_eol (parser, pragma_tok);
35995 finish_omp_taskwait ();
35998 /* OpenMP 3.1:
35999 # pragma omp taskyield new-line */
36001 static void
36002 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
36004 cp_parser_require_pragma_eol (parser, pragma_tok);
36005 finish_omp_taskyield ();
36008 /* OpenMP 4.0:
36009 # pragma omp taskgroup new-line
36010 structured-block */
36012 static tree
36013 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36015 cp_parser_require_pragma_eol (parser, pragma_tok);
36016 return c_finish_omp_taskgroup (input_location,
36017 cp_parser_omp_structured_block (parser,
36018 if_p));
36022 /* OpenMP 2.5:
36023 # pragma omp threadprivate (variable-list) */
36025 static void
36026 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36028 tree vars;
36030 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36031 cp_parser_require_pragma_eol (parser, pragma_tok);
36033 finish_omp_threadprivate (vars);
36036 /* OpenMP 4.0:
36037 # pragma omp cancel cancel-clause[optseq] new-line */
36039 #define OMP_CANCEL_CLAUSE_MASK \
36040 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36046 static void
36047 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36049 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36050 "#pragma omp cancel", pragma_tok);
36051 finish_omp_cancel (clauses);
36054 /* OpenMP 4.0:
36055 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36057 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36063 static void
36064 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36065 enum pragma_context context)
36067 tree clauses;
36068 bool point_seen = false;
36070 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36072 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36073 const char *p = IDENTIFIER_POINTER (id);
36075 if (strcmp (p, "point") == 0)
36077 cp_lexer_consume_token (parser->lexer);
36078 point_seen = true;
36081 if (!point_seen)
36083 cp_parser_error (parser, "expected %<point%>");
36084 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36085 return;
36088 if (context != pragma_compound)
36090 if (context == pragma_stmt)
36091 error_at (pragma_tok->location,
36092 "%<#pragma %s%> may only be used in compound statements",
36093 "omp cancellation point");
36094 else
36095 cp_parser_error (parser, "expected declaration specifiers");
36096 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36097 return;
36100 clauses = cp_parser_omp_all_clauses (parser,
36101 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36102 "#pragma omp cancellation point",
36103 pragma_tok);
36104 finish_omp_cancellation_point (clauses);
36107 /* OpenMP 4.0:
36108 #pragma omp distribute distribute-clause[optseq] new-line
36109 for-loop */
36111 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36112 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36118 static tree
36119 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36120 char *p_name, omp_clause_mask mask, tree *cclauses,
36121 bool *if_p)
36123 tree clauses, sb, ret;
36124 unsigned int save;
36125 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36127 strcat (p_name, " distribute");
36128 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36130 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36132 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36133 const char *p = IDENTIFIER_POINTER (id);
36134 bool simd = false;
36135 bool parallel = false;
36137 if (strcmp (p, "simd") == 0)
36138 simd = true;
36139 else
36140 parallel = strcmp (p, "parallel") == 0;
36141 if (parallel || simd)
36143 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36144 if (cclauses == NULL)
36145 cclauses = cclauses_buf;
36146 cp_lexer_consume_token (parser->lexer);
36147 if (!flag_openmp) /* flag_openmp_simd */
36149 if (simd)
36150 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36151 cclauses, if_p);
36152 else
36153 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36154 cclauses, if_p);
36156 sb = begin_omp_structured_block ();
36157 save = cp_parser_begin_omp_structured_block (parser);
36158 if (simd)
36159 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36160 cclauses, if_p);
36161 else
36162 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36163 cclauses, if_p);
36164 cp_parser_end_omp_structured_block (parser, save);
36165 tree body = finish_omp_structured_block (sb);
36166 if (ret == NULL)
36167 return ret;
36168 ret = make_node (OMP_DISTRIBUTE);
36169 TREE_TYPE (ret) = void_type_node;
36170 OMP_FOR_BODY (ret) = body;
36171 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36172 SET_EXPR_LOCATION (ret, loc);
36173 add_stmt (ret);
36174 return ret;
36177 if (!flag_openmp) /* flag_openmp_simd */
36179 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36180 return NULL_TREE;
36183 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36184 cclauses == NULL);
36185 if (cclauses)
36187 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36188 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36191 sb = begin_omp_structured_block ();
36192 save = cp_parser_begin_omp_structured_block (parser);
36194 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36196 cp_parser_end_omp_structured_block (parser, save);
36197 add_stmt (finish_omp_structured_block (sb));
36199 return ret;
36202 /* OpenMP 4.0:
36203 # pragma omp teams teams-clause[optseq] new-line
36204 structured-block */
36206 #define OMP_TEAMS_CLAUSE_MASK \
36207 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36215 static tree
36216 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36217 char *p_name, omp_clause_mask mask, tree *cclauses,
36218 bool *if_p)
36220 tree clauses, sb, ret;
36221 unsigned int save;
36222 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36224 strcat (p_name, " teams");
36225 mask |= OMP_TEAMS_CLAUSE_MASK;
36227 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36229 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36230 const char *p = IDENTIFIER_POINTER (id);
36231 if (strcmp (p, "distribute") == 0)
36233 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36234 if (cclauses == NULL)
36235 cclauses = cclauses_buf;
36237 cp_lexer_consume_token (parser->lexer);
36238 if (!flag_openmp) /* flag_openmp_simd */
36239 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36240 cclauses, if_p);
36241 sb = begin_omp_structured_block ();
36242 save = cp_parser_begin_omp_structured_block (parser);
36243 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36244 cclauses, if_p);
36245 cp_parser_end_omp_structured_block (parser, save);
36246 tree body = finish_omp_structured_block (sb);
36247 if (ret == NULL)
36248 return ret;
36249 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36250 ret = make_node (OMP_TEAMS);
36251 TREE_TYPE (ret) = void_type_node;
36252 OMP_TEAMS_CLAUSES (ret) = clauses;
36253 OMP_TEAMS_BODY (ret) = body;
36254 OMP_TEAMS_COMBINED (ret) = 1;
36255 SET_EXPR_LOCATION (ret, loc);
36256 return add_stmt (ret);
36259 if (!flag_openmp) /* flag_openmp_simd */
36261 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36262 return NULL_TREE;
36265 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36266 cclauses == NULL);
36267 if (cclauses)
36269 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36270 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36273 tree stmt = make_node (OMP_TEAMS);
36274 TREE_TYPE (stmt) = void_type_node;
36275 OMP_TEAMS_CLAUSES (stmt) = clauses;
36276 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36277 SET_EXPR_LOCATION (stmt, loc);
36279 return add_stmt (stmt);
36282 /* OpenMP 4.0:
36283 # pragma omp target data target-data-clause[optseq] new-line
36284 structured-block */
36286 #define OMP_TARGET_DATA_CLAUSE_MASK \
36287 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36292 static tree
36293 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36295 tree clauses
36296 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36297 "#pragma omp target data", pragma_tok);
36298 int map_seen = 0;
36299 for (tree *pc = &clauses; *pc;)
36301 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36302 switch (OMP_CLAUSE_MAP_KIND (*pc))
36304 case GOMP_MAP_TO:
36305 case GOMP_MAP_ALWAYS_TO:
36306 case GOMP_MAP_FROM:
36307 case GOMP_MAP_ALWAYS_FROM:
36308 case GOMP_MAP_TOFROM:
36309 case GOMP_MAP_ALWAYS_TOFROM:
36310 case GOMP_MAP_ALLOC:
36311 map_seen = 3;
36312 break;
36313 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36314 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36315 case GOMP_MAP_ALWAYS_POINTER:
36316 break;
36317 default:
36318 map_seen |= 1;
36319 error_at (OMP_CLAUSE_LOCATION (*pc),
36320 "%<#pragma omp target data%> with map-type other "
36321 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36322 "on %<map%> clause");
36323 *pc = OMP_CLAUSE_CHAIN (*pc);
36324 continue;
36326 pc = &OMP_CLAUSE_CHAIN (*pc);
36329 if (map_seen != 3)
36331 if (map_seen == 0)
36332 error_at (pragma_tok->location,
36333 "%<#pragma omp target data%> must contain at least "
36334 "one %<map%> clause");
36335 return NULL_TREE;
36338 tree stmt = make_node (OMP_TARGET_DATA);
36339 TREE_TYPE (stmt) = void_type_node;
36340 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36342 keep_next_level (true);
36343 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36345 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36346 return add_stmt (stmt);
36349 /* OpenMP 4.5:
36350 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36351 structured-block */
36353 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36354 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36360 static tree
36361 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36362 enum pragma_context context)
36364 bool data_seen = false;
36365 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36367 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36368 const char *p = IDENTIFIER_POINTER (id);
36370 if (strcmp (p, "data") == 0)
36372 cp_lexer_consume_token (parser->lexer);
36373 data_seen = true;
36376 if (!data_seen)
36378 cp_parser_error (parser, "expected %<data%>");
36379 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36380 return NULL_TREE;
36383 if (context == pragma_stmt)
36385 error_at (pragma_tok->location,
36386 "%<#pragma %s%> may only be used in compound statements",
36387 "omp target enter data");
36388 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36389 return NULL_TREE;
36392 tree clauses
36393 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36394 "#pragma omp target enter data", pragma_tok);
36395 int map_seen = 0;
36396 for (tree *pc = &clauses; *pc;)
36398 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36399 switch (OMP_CLAUSE_MAP_KIND (*pc))
36401 case GOMP_MAP_TO:
36402 case GOMP_MAP_ALWAYS_TO:
36403 case GOMP_MAP_ALLOC:
36404 map_seen = 3;
36405 break;
36406 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36407 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36408 case GOMP_MAP_ALWAYS_POINTER:
36409 break;
36410 default:
36411 map_seen |= 1;
36412 error_at (OMP_CLAUSE_LOCATION (*pc),
36413 "%<#pragma omp target enter data%> with map-type other "
36414 "than %<to%> or %<alloc%> on %<map%> clause");
36415 *pc = OMP_CLAUSE_CHAIN (*pc);
36416 continue;
36418 pc = &OMP_CLAUSE_CHAIN (*pc);
36421 if (map_seen != 3)
36423 if (map_seen == 0)
36424 error_at (pragma_tok->location,
36425 "%<#pragma omp target enter data%> must contain at least "
36426 "one %<map%> clause");
36427 return NULL_TREE;
36430 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36431 TREE_TYPE (stmt) = void_type_node;
36432 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36433 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36434 return add_stmt (stmt);
36437 /* OpenMP 4.5:
36438 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36439 structured-block */
36441 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36448 static tree
36449 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36450 enum pragma_context context)
36452 bool data_seen = false;
36453 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36455 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36456 const char *p = IDENTIFIER_POINTER (id);
36458 if (strcmp (p, "data") == 0)
36460 cp_lexer_consume_token (parser->lexer);
36461 data_seen = true;
36464 if (!data_seen)
36466 cp_parser_error (parser, "expected %<data%>");
36467 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36468 return NULL_TREE;
36471 if (context == pragma_stmt)
36473 error_at (pragma_tok->location,
36474 "%<#pragma %s%> may only be used in compound statements",
36475 "omp target exit data");
36476 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36477 return NULL_TREE;
36480 tree clauses
36481 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36482 "#pragma omp target exit data", pragma_tok);
36483 int map_seen = 0;
36484 for (tree *pc = &clauses; *pc;)
36486 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36487 switch (OMP_CLAUSE_MAP_KIND (*pc))
36489 case GOMP_MAP_FROM:
36490 case GOMP_MAP_ALWAYS_FROM:
36491 case GOMP_MAP_RELEASE:
36492 case GOMP_MAP_DELETE:
36493 map_seen = 3;
36494 break;
36495 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36496 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36497 case GOMP_MAP_ALWAYS_POINTER:
36498 break;
36499 default:
36500 map_seen |= 1;
36501 error_at (OMP_CLAUSE_LOCATION (*pc),
36502 "%<#pragma omp target exit data%> with map-type other "
36503 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36504 " clause");
36505 *pc = OMP_CLAUSE_CHAIN (*pc);
36506 continue;
36508 pc = &OMP_CLAUSE_CHAIN (*pc);
36511 if (map_seen != 3)
36513 if (map_seen == 0)
36514 error_at (pragma_tok->location,
36515 "%<#pragma omp target exit data%> must contain at least "
36516 "one %<map%> clause");
36517 return NULL_TREE;
36520 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36521 TREE_TYPE (stmt) = void_type_node;
36522 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36523 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36524 return add_stmt (stmt);
36527 /* OpenMP 4.0:
36528 # pragma omp target update target-update-clause[optseq] new-line */
36530 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36531 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36538 static bool
36539 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36540 enum pragma_context context)
36542 if (context == pragma_stmt)
36544 error_at (pragma_tok->location,
36545 "%<#pragma %s%> may only be used in compound statements",
36546 "omp target update");
36547 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36548 return false;
36551 tree clauses
36552 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36553 "#pragma omp target update", pragma_tok);
36554 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36555 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36557 error_at (pragma_tok->location,
36558 "%<#pragma omp target update%> must contain at least one "
36559 "%<from%> or %<to%> clauses");
36560 return false;
36563 tree stmt = make_node (OMP_TARGET_UPDATE);
36564 TREE_TYPE (stmt) = void_type_node;
36565 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36566 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36567 add_stmt (stmt);
36568 return false;
36571 /* OpenMP 4.0:
36572 # pragma omp target target-clause[optseq] new-line
36573 structured-block */
36575 #define OMP_TARGET_CLAUSE_MASK \
36576 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36586 static bool
36587 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36588 enum pragma_context context, bool *if_p)
36590 tree *pc = NULL, stmt;
36592 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36594 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36595 const char *p = IDENTIFIER_POINTER (id);
36596 enum tree_code ccode = ERROR_MARK;
36598 if (strcmp (p, "teams") == 0)
36599 ccode = OMP_TEAMS;
36600 else if (strcmp (p, "parallel") == 0)
36601 ccode = OMP_PARALLEL;
36602 else if (strcmp (p, "simd") == 0)
36603 ccode = OMP_SIMD;
36604 if (ccode != ERROR_MARK)
36606 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36607 char p_name[sizeof ("#pragma omp target teams distribute "
36608 "parallel for simd")];
36610 cp_lexer_consume_token (parser->lexer);
36611 strcpy (p_name, "#pragma omp target");
36612 if (!flag_openmp) /* flag_openmp_simd */
36614 tree stmt;
36615 switch (ccode)
36617 case OMP_TEAMS:
36618 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36619 OMP_TARGET_CLAUSE_MASK,
36620 cclauses, if_p);
36621 break;
36622 case OMP_PARALLEL:
36623 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36624 OMP_TARGET_CLAUSE_MASK,
36625 cclauses, if_p);
36626 break;
36627 case OMP_SIMD:
36628 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36629 OMP_TARGET_CLAUSE_MASK,
36630 cclauses, if_p);
36631 break;
36632 default:
36633 gcc_unreachable ();
36635 return stmt != NULL_TREE;
36637 keep_next_level (true);
36638 tree sb = begin_omp_structured_block (), ret;
36639 unsigned save = cp_parser_begin_omp_structured_block (parser);
36640 switch (ccode)
36642 case OMP_TEAMS:
36643 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36644 OMP_TARGET_CLAUSE_MASK, cclauses,
36645 if_p);
36646 break;
36647 case OMP_PARALLEL:
36648 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36649 OMP_TARGET_CLAUSE_MASK, cclauses,
36650 if_p);
36651 break;
36652 case OMP_SIMD:
36653 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36654 OMP_TARGET_CLAUSE_MASK, cclauses,
36655 if_p);
36656 break;
36657 default:
36658 gcc_unreachable ();
36660 cp_parser_end_omp_structured_block (parser, save);
36661 tree body = finish_omp_structured_block (sb);
36662 if (ret == NULL_TREE)
36663 return false;
36664 if (ccode == OMP_TEAMS && !processing_template_decl)
36666 /* For combined target teams, ensure the num_teams and
36667 thread_limit clause expressions are evaluated on the host,
36668 before entering the target construct. */
36669 tree c;
36670 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36671 c; c = OMP_CLAUSE_CHAIN (c))
36672 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36673 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36674 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36676 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36677 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36678 if (expr == error_mark_node)
36679 continue;
36680 tree tmp = TARGET_EXPR_SLOT (expr);
36681 add_stmt (expr);
36682 OMP_CLAUSE_OPERAND (c, 0) = expr;
36683 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36684 OMP_CLAUSE_FIRSTPRIVATE);
36685 OMP_CLAUSE_DECL (tc) = tmp;
36686 OMP_CLAUSE_CHAIN (tc)
36687 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36688 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36691 tree stmt = make_node (OMP_TARGET);
36692 TREE_TYPE (stmt) = void_type_node;
36693 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36694 OMP_TARGET_BODY (stmt) = body;
36695 OMP_TARGET_COMBINED (stmt) = 1;
36696 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36697 add_stmt (stmt);
36698 pc = &OMP_TARGET_CLAUSES (stmt);
36699 goto check_clauses;
36701 else if (!flag_openmp) /* flag_openmp_simd */
36703 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36704 return false;
36706 else if (strcmp (p, "data") == 0)
36708 cp_lexer_consume_token (parser->lexer);
36709 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36710 return true;
36712 else if (strcmp (p, "enter") == 0)
36714 cp_lexer_consume_token (parser->lexer);
36715 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36716 return false;
36718 else if (strcmp (p, "exit") == 0)
36720 cp_lexer_consume_token (parser->lexer);
36721 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36722 return false;
36724 else if (strcmp (p, "update") == 0)
36726 cp_lexer_consume_token (parser->lexer);
36727 return cp_parser_omp_target_update (parser, pragma_tok, context);
36730 if (!flag_openmp) /* flag_openmp_simd */
36732 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36733 return false;
36736 stmt = make_node (OMP_TARGET);
36737 TREE_TYPE (stmt) = void_type_node;
36739 OMP_TARGET_CLAUSES (stmt)
36740 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36741 "#pragma omp target", pragma_tok);
36742 pc = &OMP_TARGET_CLAUSES (stmt);
36743 keep_next_level (true);
36744 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36746 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36747 add_stmt (stmt);
36749 check_clauses:
36750 while (*pc)
36752 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36753 switch (OMP_CLAUSE_MAP_KIND (*pc))
36755 case GOMP_MAP_TO:
36756 case GOMP_MAP_ALWAYS_TO:
36757 case GOMP_MAP_FROM:
36758 case GOMP_MAP_ALWAYS_FROM:
36759 case GOMP_MAP_TOFROM:
36760 case GOMP_MAP_ALWAYS_TOFROM:
36761 case GOMP_MAP_ALLOC:
36762 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36763 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36764 case GOMP_MAP_ALWAYS_POINTER:
36765 break;
36766 default:
36767 error_at (OMP_CLAUSE_LOCATION (*pc),
36768 "%<#pragma omp target%> with map-type other "
36769 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36770 "on %<map%> clause");
36771 *pc = OMP_CLAUSE_CHAIN (*pc);
36772 continue;
36774 pc = &OMP_CLAUSE_CHAIN (*pc);
36776 return true;
36779 /* OpenACC 2.0:
36780 # pragma acc cache (variable-list) new-line
36783 static tree
36784 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36786 tree stmt, clauses;
36788 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36789 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36791 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36793 stmt = make_node (OACC_CACHE);
36794 TREE_TYPE (stmt) = void_type_node;
36795 OACC_CACHE_CLAUSES (stmt) = clauses;
36796 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36797 add_stmt (stmt);
36799 return stmt;
36802 /* OpenACC 2.0:
36803 # pragma acc data oacc-data-clause[optseq] new-line
36804 structured-block */
36806 #define OACC_DATA_CLAUSE_MASK \
36807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36815 static tree
36816 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36818 tree stmt, clauses, block;
36819 unsigned int save;
36821 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36822 "#pragma acc data", pragma_tok);
36824 block = begin_omp_parallel ();
36825 save = cp_parser_begin_omp_structured_block (parser);
36826 cp_parser_statement (parser, NULL_TREE, false, if_p);
36827 cp_parser_end_omp_structured_block (parser, save);
36828 stmt = finish_oacc_data (clauses, block);
36829 return stmt;
36832 /* OpenACC 2.0:
36833 # pragma acc host_data <clauses> new-line
36834 structured-block */
36836 #define OACC_HOST_DATA_CLAUSE_MASK \
36837 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36839 static tree
36840 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36842 tree stmt, clauses, block;
36843 unsigned int save;
36845 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36846 "#pragma acc host_data", pragma_tok);
36848 block = begin_omp_parallel ();
36849 save = cp_parser_begin_omp_structured_block (parser);
36850 cp_parser_statement (parser, NULL_TREE, false, if_p);
36851 cp_parser_end_omp_structured_block (parser, save);
36852 stmt = finish_oacc_host_data (clauses, block);
36853 return stmt;
36856 /* OpenACC 2.0:
36857 # pragma acc declare oacc-data-clause[optseq] new-line
36860 #define OACC_DECLARE_CLAUSE_MASK \
36861 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36870 static tree
36871 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36873 tree clauses, stmt;
36874 bool error = false;
36876 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36877 "#pragma acc declare", pragma_tok, true);
36880 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36882 error_at (pragma_tok->location,
36883 "no valid clauses specified in %<#pragma acc declare%>");
36884 return NULL_TREE;
36887 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36889 location_t loc = OMP_CLAUSE_LOCATION (t);
36890 tree decl = OMP_CLAUSE_DECL (t);
36891 if (!DECL_P (decl))
36893 error_at (loc, "array section in %<#pragma acc declare%>");
36894 error = true;
36895 continue;
36897 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36898 switch (OMP_CLAUSE_MAP_KIND (t))
36900 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36901 case GOMP_MAP_ALLOC:
36902 case GOMP_MAP_TO:
36903 case GOMP_MAP_FORCE_DEVICEPTR:
36904 case GOMP_MAP_DEVICE_RESIDENT:
36905 break;
36907 case GOMP_MAP_LINK:
36908 if (!global_bindings_p ()
36909 && (TREE_STATIC (decl)
36910 || !DECL_EXTERNAL (decl)))
36912 error_at (loc,
36913 "%qD must be a global variable in "
36914 "%<#pragma acc declare link%>",
36915 decl);
36916 error = true;
36917 continue;
36919 break;
36921 default:
36922 if (global_bindings_p ())
36924 error_at (loc, "invalid OpenACC clause at file scope");
36925 error = true;
36926 continue;
36928 if (DECL_EXTERNAL (decl))
36930 error_at (loc,
36931 "invalid use of %<extern%> variable %qD "
36932 "in %<#pragma acc declare%>", decl);
36933 error = true;
36934 continue;
36936 else if (TREE_PUBLIC (decl))
36938 error_at (loc,
36939 "invalid use of %<global%> variable %qD "
36940 "in %<#pragma acc declare%>", decl);
36941 error = true;
36942 continue;
36944 break;
36947 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36948 || lookup_attribute ("omp declare target link",
36949 DECL_ATTRIBUTES (decl)))
36951 error_at (loc, "variable %qD used more than once with "
36952 "%<#pragma acc declare%>", decl);
36953 error = true;
36954 continue;
36957 if (!error)
36959 tree id;
36961 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36962 id = get_identifier ("omp declare target link");
36963 else
36964 id = get_identifier ("omp declare target");
36966 DECL_ATTRIBUTES (decl)
36967 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36968 if (global_bindings_p ())
36970 symtab_node *node = symtab_node::get (decl);
36971 if (node != NULL)
36973 node->offloadable = 1;
36974 if (ENABLE_OFFLOADING)
36976 g->have_offload = true;
36977 if (is_a <varpool_node *> (node))
36978 vec_safe_push (offload_vars, decl);
36985 if (error || global_bindings_p ())
36986 return NULL_TREE;
36988 stmt = make_node (OACC_DECLARE);
36989 TREE_TYPE (stmt) = void_type_node;
36990 OACC_DECLARE_CLAUSES (stmt) = clauses;
36991 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36993 add_stmt (stmt);
36995 return NULL_TREE;
36998 /* OpenACC 2.0:
36999 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37003 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37005 LOC is the location of the #pragma token.
37008 #define OACC_ENTER_DATA_CLAUSE_MASK \
37009 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37015 #define OACC_EXIT_DATA_CLAUSE_MASK \
37016 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37023 static tree
37024 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37025 bool enter)
37027 location_t loc = pragma_tok->location;
37028 tree stmt, clauses;
37029 const char *p = "";
37031 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37032 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37034 if (strcmp (p, "data") != 0)
37036 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37037 enter ? "enter" : "exit");
37038 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37039 return NULL_TREE;
37042 cp_lexer_consume_token (parser->lexer);
37044 if (enter)
37045 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37046 "#pragma acc enter data", pragma_tok);
37047 else
37048 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37049 "#pragma acc exit data", pragma_tok);
37051 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37053 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37054 enter ? "enter" : "exit");
37055 return NULL_TREE;
37058 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37059 TREE_TYPE (stmt) = void_type_node;
37060 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37061 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37062 add_stmt (stmt);
37063 return stmt;
37066 /* OpenACC 2.0:
37067 # pragma acc loop oacc-loop-clause[optseq] new-line
37068 structured-block */
37070 #define OACC_LOOP_CLAUSE_MASK \
37071 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37082 static tree
37083 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37084 omp_clause_mask mask, tree *cclauses, bool *if_p)
37086 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37088 strcat (p_name, " loop");
37089 mask |= OACC_LOOP_CLAUSE_MASK;
37091 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37092 cclauses == NULL);
37093 if (cclauses)
37095 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37096 if (*cclauses)
37097 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37098 if (clauses)
37099 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37102 tree block = begin_omp_structured_block ();
37103 int save = cp_parser_begin_omp_structured_block (parser);
37104 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37105 cp_parser_end_omp_structured_block (parser, save);
37106 add_stmt (finish_omp_structured_block (block));
37108 return stmt;
37111 /* OpenACC 2.0:
37112 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37113 structured-block
37117 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37118 structured-block
37121 #define OACC_KERNELS_CLAUSE_MASK \
37122 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37136 #define OACC_PARALLEL_CLAUSE_MASK \
37137 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37154 static tree
37155 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37156 char *p_name, bool *if_p)
37158 omp_clause_mask mask;
37159 enum tree_code code;
37160 switch (cp_parser_pragma_kind (pragma_tok))
37162 case PRAGMA_OACC_KERNELS:
37163 strcat (p_name, " kernels");
37164 mask = OACC_KERNELS_CLAUSE_MASK;
37165 code = OACC_KERNELS;
37166 break;
37167 case PRAGMA_OACC_PARALLEL:
37168 strcat (p_name, " parallel");
37169 mask = OACC_PARALLEL_CLAUSE_MASK;
37170 code = OACC_PARALLEL;
37171 break;
37172 default:
37173 gcc_unreachable ();
37176 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37178 const char *p
37179 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37180 if (strcmp (p, "loop") == 0)
37182 cp_lexer_consume_token (parser->lexer);
37183 tree block = begin_omp_parallel ();
37184 tree clauses;
37185 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37186 if_p);
37187 return finish_omp_construct (code, block, clauses);
37191 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37193 tree block = begin_omp_parallel ();
37194 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37195 cp_parser_statement (parser, NULL_TREE, false, if_p);
37196 cp_parser_end_omp_structured_block (parser, save);
37197 return finish_omp_construct (code, block, clauses);
37200 /* OpenACC 2.0:
37201 # pragma acc update oacc-update-clause[optseq] new-line
37204 #define OACC_UPDATE_CLAUSE_MASK \
37205 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37212 static tree
37213 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37215 tree stmt, clauses;
37217 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37218 "#pragma acc update", pragma_tok);
37220 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37222 error_at (pragma_tok->location,
37223 "%<#pragma acc update%> must contain at least one "
37224 "%<device%> or %<host%> or %<self%> clause");
37225 return NULL_TREE;
37228 stmt = make_node (OACC_UPDATE);
37229 TREE_TYPE (stmt) = void_type_node;
37230 OACC_UPDATE_CLAUSES (stmt) = clauses;
37231 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37232 add_stmt (stmt);
37233 return stmt;
37236 /* OpenACC 2.0:
37237 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37239 LOC is the location of the #pragma token.
37242 #define OACC_WAIT_CLAUSE_MASK \
37243 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37245 static tree
37246 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37248 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37249 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37251 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37252 list = cp_parser_oacc_wait_list (parser, loc, list);
37254 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37255 "#pragma acc wait", pragma_tok);
37257 stmt = c_finish_oacc_wait (loc, list, clauses);
37258 stmt = finish_expr_stmt (stmt);
37260 return stmt;
37263 /* OpenMP 4.0:
37264 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37266 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37274 static void
37275 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37276 enum pragma_context context)
37278 bool first_p = parser->omp_declare_simd == NULL;
37279 cp_omp_declare_simd_data data;
37280 if (first_p)
37282 data.error_seen = false;
37283 data.fndecl_seen = false;
37284 data.tokens = vNULL;
37285 data.clauses = NULL_TREE;
37286 /* It is safe to take the address of a local variable; it will only be
37287 used while this scope is live. */
37288 parser->omp_declare_simd = &data;
37291 /* Store away all pragma tokens. */
37292 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37293 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37294 cp_lexer_consume_token (parser->lexer);
37295 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37296 parser->omp_declare_simd->error_seen = true;
37297 cp_parser_require_pragma_eol (parser, pragma_tok);
37298 struct cp_token_cache *cp
37299 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37300 parser->omp_declare_simd->tokens.safe_push (cp);
37302 if (first_p)
37304 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37305 cp_parser_pragma (parser, context, NULL);
37306 switch (context)
37308 case pragma_external:
37309 cp_parser_declaration (parser);
37310 break;
37311 case pragma_member:
37312 cp_parser_member_declaration (parser);
37313 break;
37314 case pragma_objc_icode:
37315 cp_parser_block_declaration (parser, /*statement_p=*/false);
37316 break;
37317 default:
37318 cp_parser_declaration_statement (parser);
37319 break;
37321 if (parser->omp_declare_simd
37322 && !parser->omp_declare_simd->error_seen
37323 && !parser->omp_declare_simd->fndecl_seen)
37324 error_at (pragma_tok->location,
37325 "%<#pragma omp declare simd%> not immediately followed by "
37326 "function declaration or definition");
37327 data.tokens.release ();
37328 parser->omp_declare_simd = NULL;
37332 /* Finalize #pragma omp declare simd clauses after direct declarator has
37333 been parsed, and put that into "omp declare simd" attribute. */
37335 static tree
37336 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37338 struct cp_token_cache *ce;
37339 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37340 int i;
37342 if (!data->error_seen && data->fndecl_seen)
37344 error ("%<#pragma omp declare simd%> not immediately followed by "
37345 "a single function declaration or definition");
37346 data->error_seen = true;
37348 if (data->error_seen)
37349 return attrs;
37351 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37353 tree c, cl;
37355 cp_parser_push_lexer_for_tokens (parser, ce);
37356 parser->lexer->in_pragma = true;
37357 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37358 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37359 cp_lexer_consume_token (parser->lexer);
37360 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37361 "#pragma omp declare simd", pragma_tok);
37362 cp_parser_pop_lexer (parser);
37363 if (cl)
37364 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37365 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37366 TREE_CHAIN (c) = attrs;
37367 if (processing_template_decl)
37368 ATTR_IS_DEPENDENT (c) = 1;
37369 attrs = c;
37372 data->fndecl_seen = true;
37373 return attrs;
37377 /* OpenMP 4.0:
37378 # pragma omp declare target new-line
37379 declarations and definitions
37380 # pragma omp end declare target new-line
37382 OpenMP 4.5:
37383 # pragma omp declare target ( extended-list ) new-line
37385 # pragma omp declare target declare-target-clauses[seq] new-line */
37387 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37388 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37391 static void
37392 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37394 tree clauses = NULL_TREE;
37395 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37396 clauses
37397 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37398 "#pragma omp declare target", pragma_tok);
37399 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37401 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37402 clauses);
37403 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37404 cp_parser_require_pragma_eol (parser, pragma_tok);
37406 else
37408 cp_parser_require_pragma_eol (parser, pragma_tok);
37409 scope_chain->omp_declare_target_attribute++;
37410 return;
37412 if (scope_chain->omp_declare_target_attribute)
37413 error_at (pragma_tok->location,
37414 "%<#pragma omp declare target%> with clauses in between "
37415 "%<#pragma omp declare target%> without clauses and "
37416 "%<#pragma omp end declare target%>");
37417 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37419 tree t = OMP_CLAUSE_DECL (c), id;
37420 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37421 tree at2 = lookup_attribute ("omp declare target link",
37422 DECL_ATTRIBUTES (t));
37423 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37425 id = get_identifier ("omp declare target link");
37426 std::swap (at1, at2);
37428 else
37429 id = get_identifier ("omp declare target");
37430 if (at2)
37432 error_at (OMP_CLAUSE_LOCATION (c),
37433 "%qD specified both in declare target %<link%> and %<to%>"
37434 " clauses", t);
37435 continue;
37437 if (!at1)
37439 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37440 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37441 continue;
37443 symtab_node *node = symtab_node::get (t);
37444 if (node != NULL)
37446 node->offloadable = 1;
37447 if (ENABLE_OFFLOADING)
37449 g->have_offload = true;
37450 if (is_a <varpool_node *> (node))
37451 vec_safe_push (offload_vars, t);
37458 static void
37459 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37461 const char *p = "";
37462 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37464 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37465 p = IDENTIFIER_POINTER (id);
37467 if (strcmp (p, "declare") == 0)
37469 cp_lexer_consume_token (parser->lexer);
37470 p = "";
37471 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37473 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37474 p = IDENTIFIER_POINTER (id);
37476 if (strcmp (p, "target") == 0)
37477 cp_lexer_consume_token (parser->lexer);
37478 else
37480 cp_parser_error (parser, "expected %<target%>");
37481 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37482 return;
37485 else
37487 cp_parser_error (parser, "expected %<declare%>");
37488 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37489 return;
37491 cp_parser_require_pragma_eol (parser, pragma_tok);
37492 if (!scope_chain->omp_declare_target_attribute)
37493 error_at (pragma_tok->location,
37494 "%<#pragma omp end declare target%> without corresponding "
37495 "%<#pragma omp declare target%>");
37496 else
37497 scope_chain->omp_declare_target_attribute--;
37500 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37501 expression and optional initializer clause of
37502 #pragma omp declare reduction. We store the expression(s) as
37503 either 3, 6 or 7 special statements inside of the artificial function's
37504 body. The first two statements are DECL_EXPRs for the artificial
37505 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37506 expression that uses those variables.
37507 If there was any INITIALIZER clause, this is followed by further statements,
37508 the fourth and fifth statements are DECL_EXPRs for the artificial
37509 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37510 constructor variant (first token after open paren is not omp_priv),
37511 then the sixth statement is a statement with the function call expression
37512 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37513 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37514 to initialize the OMP_PRIV artificial variable and there is seventh
37515 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37517 static bool
37518 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37520 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37521 gcc_assert (TYPE_REF_P (type));
37522 type = TREE_TYPE (type);
37523 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37524 DECL_ARTIFICIAL (omp_out) = 1;
37525 pushdecl (omp_out);
37526 add_decl_expr (omp_out);
37527 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37528 DECL_ARTIFICIAL (omp_in) = 1;
37529 pushdecl (omp_in);
37530 add_decl_expr (omp_in);
37531 tree combiner;
37532 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37534 keep_next_level (true);
37535 tree block = begin_omp_structured_block ();
37536 combiner = cp_parser_expression (parser);
37537 finish_expr_stmt (combiner);
37538 block = finish_omp_structured_block (block);
37539 add_stmt (block);
37541 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37542 return false;
37544 const char *p = "";
37545 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37547 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37548 p = IDENTIFIER_POINTER (id);
37551 if (strcmp (p, "initializer") == 0)
37553 cp_lexer_consume_token (parser->lexer);
37554 matching_parens parens;
37555 if (!parens.require_open (parser))
37556 return false;
37558 p = "";
37559 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37561 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37562 p = IDENTIFIER_POINTER (id);
37565 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37566 DECL_ARTIFICIAL (omp_priv) = 1;
37567 pushdecl (omp_priv);
37568 add_decl_expr (omp_priv);
37569 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37570 DECL_ARTIFICIAL (omp_orig) = 1;
37571 pushdecl (omp_orig);
37572 add_decl_expr (omp_orig);
37574 keep_next_level (true);
37575 block = begin_omp_structured_block ();
37577 bool ctor = false;
37578 if (strcmp (p, "omp_priv") == 0)
37580 bool is_direct_init, is_non_constant_init;
37581 ctor = true;
37582 cp_lexer_consume_token (parser->lexer);
37583 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37584 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37585 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37586 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37587 == CPP_CLOSE_PAREN
37588 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37589 == CPP_CLOSE_PAREN))
37591 finish_omp_structured_block (block);
37592 error ("invalid initializer clause");
37593 return false;
37595 initializer = cp_parser_initializer (parser, &is_direct_init,
37596 &is_non_constant_init);
37597 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37598 NULL_TREE, LOOKUP_ONLYCONVERTING);
37600 else
37602 cp_parser_parse_tentatively (parser);
37603 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37604 /*check_dependency_p=*/true,
37605 /*template_p=*/NULL,
37606 /*declarator_p=*/false,
37607 /*optional_p=*/false);
37608 vec<tree, va_gc> *args;
37609 if (fn_name == error_mark_node
37610 || cp_parser_error_occurred (parser)
37611 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37612 || ((args = cp_parser_parenthesized_expression_list
37613 (parser, non_attr, /*cast_p=*/false,
37614 /*allow_expansion_p=*/true,
37615 /*non_constant_p=*/NULL)),
37616 cp_parser_error_occurred (parser)))
37618 finish_omp_structured_block (block);
37619 cp_parser_abort_tentative_parse (parser);
37620 cp_parser_error (parser, "expected id-expression (arguments)");
37621 return false;
37623 unsigned int i;
37624 tree arg;
37625 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37626 if (arg == omp_priv
37627 || (TREE_CODE (arg) == ADDR_EXPR
37628 && TREE_OPERAND (arg, 0) == omp_priv))
37629 break;
37630 cp_parser_abort_tentative_parse (parser);
37631 if (arg == NULL_TREE)
37632 error ("one of the initializer call arguments should be %<omp_priv%>"
37633 " or %<&omp_priv%>");
37634 initializer = cp_parser_postfix_expression (parser, false, false, false,
37635 false, NULL);
37636 finish_expr_stmt (initializer);
37639 block = finish_omp_structured_block (block);
37640 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37641 add_stmt (block);
37643 if (ctor)
37644 add_decl_expr (omp_orig);
37646 if (!parens.require_close (parser))
37647 return false;
37650 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37651 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37652 UNKNOWN_LOCATION);
37654 return true;
37657 /* OpenMP 4.0
37658 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37659 initializer-clause[opt] new-line
37661 initializer-clause:
37662 initializer (omp_priv initializer)
37663 initializer (function-name (argument-list)) */
37665 static void
37666 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37667 enum pragma_context)
37669 auto_vec<tree> types;
37670 enum tree_code reduc_code = ERROR_MARK;
37671 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37672 unsigned int i;
37673 cp_token *first_token;
37674 cp_token_cache *cp;
37675 int errs;
37676 void *p;
37678 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37679 p = obstack_alloc (&declarator_obstack, 0);
37681 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37682 goto fail;
37684 switch (cp_lexer_peek_token (parser->lexer)->type)
37686 case CPP_PLUS:
37687 reduc_code = PLUS_EXPR;
37688 break;
37689 case CPP_MULT:
37690 reduc_code = MULT_EXPR;
37691 break;
37692 case CPP_MINUS:
37693 reduc_code = MINUS_EXPR;
37694 break;
37695 case CPP_AND:
37696 reduc_code = BIT_AND_EXPR;
37697 break;
37698 case CPP_XOR:
37699 reduc_code = BIT_XOR_EXPR;
37700 break;
37701 case CPP_OR:
37702 reduc_code = BIT_IOR_EXPR;
37703 break;
37704 case CPP_AND_AND:
37705 reduc_code = TRUTH_ANDIF_EXPR;
37706 break;
37707 case CPP_OR_OR:
37708 reduc_code = TRUTH_ORIF_EXPR;
37709 break;
37710 case CPP_NAME:
37711 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37712 break;
37713 default:
37714 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37715 "%<|%>, %<&&%>, %<||%> or identifier");
37716 goto fail;
37719 if (reduc_code != ERROR_MARK)
37720 cp_lexer_consume_token (parser->lexer);
37722 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37723 if (reduc_id == error_mark_node)
37724 goto fail;
37726 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37727 goto fail;
37729 /* Types may not be defined in declare reduction type list. */
37730 const char *saved_message;
37731 saved_message = parser->type_definition_forbidden_message;
37732 parser->type_definition_forbidden_message
37733 = G_("types may not be defined in declare reduction type list");
37734 bool saved_colon_corrects_to_scope_p;
37735 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37736 parser->colon_corrects_to_scope_p = false;
37737 bool saved_colon_doesnt_start_class_def_p;
37738 saved_colon_doesnt_start_class_def_p
37739 = parser->colon_doesnt_start_class_def_p;
37740 parser->colon_doesnt_start_class_def_p = true;
37742 while (true)
37744 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37745 type = cp_parser_type_id (parser);
37746 if (type == error_mark_node)
37748 else if (ARITHMETIC_TYPE_P (type)
37749 && (orig_reduc_id == NULL_TREE
37750 || (TREE_CODE (type) != COMPLEX_TYPE
37751 && (id_equal (orig_reduc_id, "min")
37752 || id_equal (orig_reduc_id, "max")))))
37753 error_at (loc, "predeclared arithmetic type %qT in "
37754 "%<#pragma omp declare reduction%>", type);
37755 else if (TREE_CODE (type) == FUNCTION_TYPE
37756 || TREE_CODE (type) == METHOD_TYPE
37757 || TREE_CODE (type) == ARRAY_TYPE)
37758 error_at (loc, "function or array type %qT in "
37759 "%<#pragma omp declare reduction%>", type);
37760 else if (TYPE_REF_P (type))
37761 error_at (loc, "reference type %qT in "
37762 "%<#pragma omp declare reduction%>", type);
37763 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37764 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37765 "%<#pragma omp declare reduction%>", type);
37766 else
37767 types.safe_push (type);
37769 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37770 cp_lexer_consume_token (parser->lexer);
37771 else
37772 break;
37775 /* Restore the saved message. */
37776 parser->type_definition_forbidden_message = saved_message;
37777 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37778 parser->colon_doesnt_start_class_def_p
37779 = saved_colon_doesnt_start_class_def_p;
37781 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37782 || types.is_empty ())
37784 fail:
37785 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37786 goto done;
37789 first_token = cp_lexer_peek_token (parser->lexer);
37790 cp = NULL;
37791 errs = errorcount;
37792 FOR_EACH_VEC_ELT (types, i, type)
37794 tree fntype
37795 = build_function_type_list (void_type_node,
37796 cp_build_reference_type (type, false),
37797 NULL_TREE);
37798 tree this_reduc_id = reduc_id;
37799 if (!dependent_type_p (type))
37800 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37801 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37802 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37803 DECL_ARTIFICIAL (fndecl) = 1;
37804 DECL_EXTERNAL (fndecl) = 1;
37805 DECL_DECLARED_INLINE_P (fndecl) = 1;
37806 DECL_IGNORED_P (fndecl) = 1;
37807 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37808 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37809 DECL_ATTRIBUTES (fndecl)
37810 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37811 DECL_ATTRIBUTES (fndecl));
37812 if (processing_template_decl)
37813 fndecl = push_template_decl (fndecl);
37814 bool block_scope = false;
37815 tree block = NULL_TREE;
37816 if (current_function_decl)
37818 block_scope = true;
37819 DECL_CONTEXT (fndecl) = global_namespace;
37820 if (!processing_template_decl)
37821 pushdecl (fndecl);
37823 else if (current_class_type)
37825 if (cp == NULL)
37827 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37828 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37829 cp_lexer_consume_token (parser->lexer);
37830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37831 goto fail;
37832 cp = cp_token_cache_new (first_token,
37833 cp_lexer_peek_nth_token (parser->lexer,
37834 2));
37836 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37837 finish_member_declaration (fndecl);
37838 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37839 DECL_PENDING_INLINE_P (fndecl) = 1;
37840 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37841 continue;
37843 else
37845 DECL_CONTEXT (fndecl) = current_namespace;
37846 pushdecl (fndecl);
37848 if (!block_scope)
37849 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37850 else
37851 block = begin_omp_structured_block ();
37852 if (cp)
37854 cp_parser_push_lexer_for_tokens (parser, cp);
37855 parser->lexer->in_pragma = true;
37857 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37859 if (!block_scope)
37860 finish_function (/*inline_p=*/false);
37861 else
37862 DECL_CONTEXT (fndecl) = current_function_decl;
37863 if (cp)
37864 cp_parser_pop_lexer (parser);
37865 goto fail;
37867 if (cp)
37868 cp_parser_pop_lexer (parser);
37869 if (!block_scope)
37870 finish_function (/*inline_p=*/false);
37871 else
37873 DECL_CONTEXT (fndecl) = current_function_decl;
37874 block = finish_omp_structured_block (block);
37875 if (TREE_CODE (block) == BIND_EXPR)
37876 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37877 else if (TREE_CODE (block) == STATEMENT_LIST)
37878 DECL_SAVED_TREE (fndecl) = block;
37879 if (processing_template_decl)
37880 add_decl_expr (fndecl);
37882 cp_check_omp_declare_reduction (fndecl);
37883 if (cp == NULL && types.length () > 1)
37884 cp = cp_token_cache_new (first_token,
37885 cp_lexer_peek_nth_token (parser->lexer, 2));
37886 if (errs != errorcount)
37887 break;
37890 cp_parser_require_pragma_eol (parser, pragma_tok);
37892 done:
37893 /* Free any declarators allocated. */
37894 obstack_free (&declarator_obstack, p);
37897 /* OpenMP 4.0
37898 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37899 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37900 initializer-clause[opt] new-line
37901 #pragma omp declare target new-line */
37903 static bool
37904 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37905 enum pragma_context context)
37907 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37909 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37910 const char *p = IDENTIFIER_POINTER (id);
37912 if (strcmp (p, "simd") == 0)
37914 cp_lexer_consume_token (parser->lexer);
37915 cp_parser_omp_declare_simd (parser, pragma_tok,
37916 context);
37917 return true;
37919 cp_ensure_no_omp_declare_simd (parser);
37920 if (strcmp (p, "reduction") == 0)
37922 cp_lexer_consume_token (parser->lexer);
37923 cp_parser_omp_declare_reduction (parser, pragma_tok,
37924 context);
37925 return false;
37927 if (!flag_openmp) /* flag_openmp_simd */
37929 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37930 return false;
37932 if (strcmp (p, "target") == 0)
37934 cp_lexer_consume_token (parser->lexer);
37935 cp_parser_omp_declare_target (parser, pragma_tok);
37936 return false;
37939 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37940 "or %<target%>");
37941 cp_parser_require_pragma_eol (parser, pragma_tok);
37942 return false;
37945 /* OpenMP 4.5:
37946 #pragma omp taskloop taskloop-clause[optseq] new-line
37947 for-loop
37949 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37950 for-loop */
37952 #define OMP_TASKLOOP_CLAUSE_MASK \
37953 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37968 static tree
37969 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37970 char *p_name, omp_clause_mask mask, tree *cclauses,
37971 bool *if_p)
37973 tree clauses, sb, ret;
37974 unsigned int save;
37975 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37977 strcat (p_name, " taskloop");
37978 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37980 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37982 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37983 const char *p = IDENTIFIER_POINTER (id);
37985 if (strcmp (p, "simd") == 0)
37987 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37988 if (cclauses == NULL)
37989 cclauses = cclauses_buf;
37991 cp_lexer_consume_token (parser->lexer);
37992 if (!flag_openmp) /* flag_openmp_simd */
37993 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37994 cclauses, if_p);
37995 sb = begin_omp_structured_block ();
37996 save = cp_parser_begin_omp_structured_block (parser);
37997 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37998 cclauses, if_p);
37999 cp_parser_end_omp_structured_block (parser, save);
38000 tree body = finish_omp_structured_block (sb);
38001 if (ret == NULL)
38002 return ret;
38003 ret = make_node (OMP_TASKLOOP);
38004 TREE_TYPE (ret) = void_type_node;
38005 OMP_FOR_BODY (ret) = body;
38006 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38007 SET_EXPR_LOCATION (ret, loc);
38008 add_stmt (ret);
38009 return ret;
38012 if (!flag_openmp) /* flag_openmp_simd */
38014 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38015 return NULL_TREE;
38018 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38019 cclauses == NULL);
38020 if (cclauses)
38022 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38023 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38026 sb = begin_omp_structured_block ();
38027 save = cp_parser_begin_omp_structured_block (parser);
38029 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38030 if_p);
38032 cp_parser_end_omp_structured_block (parser, save);
38033 add_stmt (finish_omp_structured_block (sb));
38035 return ret;
38039 /* OpenACC 2.0:
38040 # pragma acc routine oacc-routine-clause[optseq] new-line
38041 function-definition
38043 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38046 #define OACC_ROUTINE_CLAUSE_MASK \
38047 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38053 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38054 component, which must resolve to a declared namespace-scope
38055 function. The clauses are either processed directly (for a named
38056 function), or defered until the immediatley following declaration
38057 is parsed. */
38059 static void
38060 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38061 enum pragma_context context)
38063 gcc_checking_assert (context == pragma_external);
38064 /* The checking for "another pragma following this one" in the "no optional
38065 '( name )'" case makes sure that we dont re-enter. */
38066 gcc_checking_assert (parser->oacc_routine == NULL);
38068 cp_oacc_routine_data data;
38069 data.error_seen = false;
38070 data.fndecl_seen = false;
38071 data.tokens = vNULL;
38072 data.clauses = NULL_TREE;
38073 data.loc = pragma_tok->location;
38074 /* It is safe to take the address of a local variable; it will only be
38075 used while this scope is live. */
38076 parser->oacc_routine = &data;
38078 /* Look for optional '( name )'. */
38079 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38081 matching_parens parens;
38082 parens.consume_open (parser); /* '(' */
38084 /* We parse the name as an id-expression. If it resolves to
38085 anything other than a non-overloaded function at namespace
38086 scope, it's an error. */
38087 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38088 tree name = cp_parser_id_expression (parser,
38089 /*template_keyword_p=*/false,
38090 /*check_dependency_p=*/false,
38091 /*template_p=*/NULL,
38092 /*declarator_p=*/false,
38093 /*optional_p=*/false);
38094 tree decl = (identifier_p (name)
38095 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38096 : name);
38097 if (name != error_mark_node && decl == error_mark_node)
38098 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38100 if (decl == error_mark_node
38101 || !parens.require_close (parser))
38103 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38104 parser->oacc_routine = NULL;
38105 return;
38108 data.clauses
38109 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38110 "#pragma acc routine",
38111 cp_lexer_peek_token (parser->lexer));
38113 if (decl && is_overloaded_fn (decl)
38114 && (TREE_CODE (decl) != FUNCTION_DECL
38115 || DECL_FUNCTION_TEMPLATE_P (decl)))
38117 error_at (name_loc,
38118 "%<#pragma acc routine%> names a set of overloads");
38119 parser->oacc_routine = NULL;
38120 return;
38123 /* Perhaps we should use the same rule as declarations in different
38124 namespaces? */
38125 if (!DECL_NAMESPACE_SCOPE_P (decl))
38127 error_at (name_loc,
38128 "%qD does not refer to a namespace scope function", decl);
38129 parser->oacc_routine = NULL;
38130 return;
38133 if (TREE_CODE (decl) != FUNCTION_DECL)
38135 error_at (name_loc, "%qD does not refer to a function", decl);
38136 parser->oacc_routine = NULL;
38137 return;
38140 cp_finalize_oacc_routine (parser, decl, false);
38141 parser->oacc_routine = NULL;
38143 else /* No optional '( name )'. */
38145 /* Store away all pragma tokens. */
38146 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38147 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38148 cp_lexer_consume_token (parser->lexer);
38149 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38150 parser->oacc_routine->error_seen = true;
38151 cp_parser_require_pragma_eol (parser, pragma_tok);
38152 struct cp_token_cache *cp
38153 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38154 parser->oacc_routine->tokens.safe_push (cp);
38156 /* Emit a helpful diagnostic if there's another pragma following this
38157 one. */
38158 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38160 cp_ensure_no_oacc_routine (parser);
38161 data.tokens.release ();
38162 /* ..., and then just keep going. */
38163 return;
38166 /* We only have to consider the pragma_external case here. */
38167 cp_parser_declaration (parser);
38168 if (parser->oacc_routine
38169 && !parser->oacc_routine->fndecl_seen)
38170 cp_ensure_no_oacc_routine (parser);
38171 else
38172 parser->oacc_routine = NULL;
38173 data.tokens.release ();
38177 /* Finalize #pragma acc routine clauses after direct declarator has
38178 been parsed. */
38180 static tree
38181 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38183 struct cp_token_cache *ce;
38184 cp_oacc_routine_data *data = parser->oacc_routine;
38186 if (!data->error_seen && data->fndecl_seen)
38188 error_at (data->loc,
38189 "%<#pragma acc routine%> not immediately followed by "
38190 "a single function declaration or definition");
38191 data->error_seen = true;
38193 if (data->error_seen)
38194 return attrs;
38196 gcc_checking_assert (data->tokens.length () == 1);
38197 ce = data->tokens[0];
38199 cp_parser_push_lexer_for_tokens (parser, ce);
38200 parser->lexer->in_pragma = true;
38201 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38203 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38204 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38205 parser->oacc_routine->clauses
38206 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38207 "#pragma acc routine", pragma_tok);
38208 cp_parser_pop_lexer (parser);
38209 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38210 fndecl_seen. */
38212 return attrs;
38215 /* Apply any saved OpenACC routine clauses to a just-parsed
38216 declaration. */
38218 static void
38219 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38221 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38223 /* Keep going if we're in error reporting mode. */
38224 if (parser->oacc_routine->error_seen
38225 || fndecl == error_mark_node)
38226 return;
38228 if (parser->oacc_routine->fndecl_seen)
38230 error_at (parser->oacc_routine->loc,
38231 "%<#pragma acc routine%> not immediately followed by"
38232 " a single function declaration or definition");
38233 parser->oacc_routine = NULL;
38234 return;
38236 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38238 cp_ensure_no_oacc_routine (parser);
38239 return;
38242 if (oacc_get_fn_attrib (fndecl))
38244 error_at (parser->oacc_routine->loc,
38245 "%<#pragma acc routine%> already applied to %qD", fndecl);
38246 parser->oacc_routine = NULL;
38247 return;
38250 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38252 error_at (parser->oacc_routine->loc,
38253 TREE_USED (fndecl)
38254 ? G_("%<#pragma acc routine%> must be applied before use")
38255 : G_("%<#pragma acc routine%> must be applied before "
38256 "definition"));
38257 parser->oacc_routine = NULL;
38258 return;
38261 /* Process the routine's dimension clauses. */
38262 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38263 oacc_replace_fn_attrib (fndecl, dims);
38265 /* Add an "omp declare target" attribute. */
38266 DECL_ATTRIBUTES (fndecl)
38267 = tree_cons (get_identifier ("omp declare target"),
38268 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38270 /* Don't unset parser->oacc_routine here: we may still need it to
38271 diagnose wrong usage. But, remember that we've used this "#pragma acc
38272 routine". */
38273 parser->oacc_routine->fndecl_seen = true;
38277 /* Main entry point to OpenMP statement pragmas. */
38279 static void
38280 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38282 tree stmt;
38283 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38284 omp_clause_mask mask (0);
38286 switch (cp_parser_pragma_kind (pragma_tok))
38288 case PRAGMA_OACC_ATOMIC:
38289 cp_parser_omp_atomic (parser, pragma_tok);
38290 return;
38291 case PRAGMA_OACC_CACHE:
38292 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38293 break;
38294 case PRAGMA_OACC_DATA:
38295 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38296 break;
38297 case PRAGMA_OACC_ENTER_DATA:
38298 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38299 break;
38300 case PRAGMA_OACC_EXIT_DATA:
38301 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38302 break;
38303 case PRAGMA_OACC_HOST_DATA:
38304 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38305 break;
38306 case PRAGMA_OACC_KERNELS:
38307 case PRAGMA_OACC_PARALLEL:
38308 strcpy (p_name, "#pragma acc");
38309 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38310 if_p);
38311 break;
38312 case PRAGMA_OACC_LOOP:
38313 strcpy (p_name, "#pragma acc");
38314 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38315 if_p);
38316 break;
38317 case PRAGMA_OACC_UPDATE:
38318 stmt = cp_parser_oacc_update (parser, pragma_tok);
38319 break;
38320 case PRAGMA_OACC_WAIT:
38321 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38322 break;
38323 case PRAGMA_OMP_ATOMIC:
38324 cp_parser_omp_atomic (parser, pragma_tok);
38325 return;
38326 case PRAGMA_OMP_CRITICAL:
38327 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38328 break;
38329 case PRAGMA_OMP_DISTRIBUTE:
38330 strcpy (p_name, "#pragma omp");
38331 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38332 if_p);
38333 break;
38334 case PRAGMA_OMP_FOR:
38335 strcpy (p_name, "#pragma omp");
38336 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38337 if_p);
38338 break;
38339 case PRAGMA_OMP_MASTER:
38340 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38341 break;
38342 case PRAGMA_OMP_PARALLEL:
38343 strcpy (p_name, "#pragma omp");
38344 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38345 if_p);
38346 break;
38347 case PRAGMA_OMP_SECTIONS:
38348 strcpy (p_name, "#pragma omp");
38349 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38350 break;
38351 case PRAGMA_OMP_SIMD:
38352 strcpy (p_name, "#pragma omp");
38353 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38354 if_p);
38355 break;
38356 case PRAGMA_OMP_SINGLE:
38357 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38358 break;
38359 case PRAGMA_OMP_TASK:
38360 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38361 break;
38362 case PRAGMA_OMP_TASKGROUP:
38363 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38364 break;
38365 case PRAGMA_OMP_TASKLOOP:
38366 strcpy (p_name, "#pragma omp");
38367 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38368 if_p);
38369 break;
38370 case PRAGMA_OMP_TEAMS:
38371 strcpy (p_name, "#pragma omp");
38372 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38373 if_p);
38374 break;
38375 default:
38376 gcc_unreachable ();
38379 protected_set_expr_location (stmt, pragma_tok->location);
38382 /* Transactional Memory parsing routines. */
38384 /* Parse a transaction attribute.
38386 txn-attribute:
38387 attribute
38388 [ [ identifier ] ]
38390 We use this instead of cp_parser_attributes_opt for transactions to avoid
38391 the pedwarn in C++98 mode. */
38393 static tree
38394 cp_parser_txn_attribute_opt (cp_parser *parser)
38396 cp_token *token;
38397 tree attr_name, attr = NULL;
38399 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38400 return cp_parser_attributes_opt (parser);
38402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38403 return NULL_TREE;
38404 cp_lexer_consume_token (parser->lexer);
38405 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38406 goto error1;
38408 token = cp_lexer_peek_token (parser->lexer);
38409 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38411 token = cp_lexer_consume_token (parser->lexer);
38413 attr_name = (token->type == CPP_KEYWORD
38414 /* For keywords, use the canonical spelling,
38415 not the parsed identifier. */
38416 ? ridpointers[(int) token->keyword]
38417 : token->u.value);
38418 attr = build_tree_list (attr_name, NULL_TREE);
38420 else
38421 cp_parser_error (parser, "expected identifier");
38423 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38424 error1:
38425 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38426 return attr;
38429 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38431 transaction-statement:
38432 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38433 compound-statement
38434 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38437 static tree
38438 cp_parser_transaction (cp_parser *parser, cp_token *token)
38440 unsigned char old_in = parser->in_transaction;
38441 unsigned char this_in = 1, new_in;
38442 enum rid keyword = token->keyword;
38443 tree stmt, attrs, noex;
38445 cp_lexer_consume_token (parser->lexer);
38447 if (keyword == RID_TRANSACTION_RELAXED
38448 || keyword == RID_SYNCHRONIZED)
38449 this_in |= TM_STMT_ATTR_RELAXED;
38450 else
38452 attrs = cp_parser_txn_attribute_opt (parser);
38453 if (attrs)
38454 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38457 /* Parse a noexcept specification. */
38458 if (keyword == RID_ATOMIC_NOEXCEPT)
38459 noex = boolean_true_node;
38460 else if (keyword == RID_ATOMIC_CANCEL)
38462 /* cancel-and-throw is unimplemented. */
38463 sorry ("atomic_cancel");
38464 noex = NULL_TREE;
38466 else
38467 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38469 /* Keep track if we're in the lexical scope of an outer transaction. */
38470 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38472 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38474 parser->in_transaction = new_in;
38475 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38476 parser->in_transaction = old_in;
38478 finish_transaction_stmt (stmt, NULL, this_in, noex);
38480 return stmt;
38483 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38485 transaction-expression:
38486 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38487 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38490 static tree
38491 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38493 unsigned char old_in = parser->in_transaction;
38494 unsigned char this_in = 1;
38495 cp_token *token;
38496 tree expr, noex;
38497 bool noex_expr;
38498 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38500 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38501 || keyword == RID_TRANSACTION_RELAXED);
38503 if (!flag_tm)
38504 error_at (loc,
38505 keyword == RID_TRANSACTION_RELAXED
38506 ? G_("%<__transaction_relaxed%> without transactional memory "
38507 "support enabled")
38508 : G_("%<__transaction_atomic%> without transactional memory "
38509 "support enabled"));
38511 token = cp_parser_require_keyword (parser, keyword,
38512 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38513 : RT_TRANSACTION_RELAXED));
38514 gcc_assert (token != NULL);
38516 if (keyword == RID_TRANSACTION_RELAXED)
38517 this_in |= TM_STMT_ATTR_RELAXED;
38519 /* Set this early. This might mean that we allow transaction_cancel in
38520 an expression that we find out later actually has to be a constexpr.
38521 However, we expect that cxx_constant_value will be able to deal with
38522 this; also, if the noexcept has no constexpr, then what we parse next
38523 really is a transaction's body. */
38524 parser->in_transaction = this_in;
38526 /* Parse a noexcept specification. */
38527 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38528 true);
38530 if (!noex || !noex_expr
38531 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38533 matching_parens parens;
38534 parens.require_open (parser);
38536 expr = cp_parser_expression (parser);
38537 expr = finish_parenthesized_expr (expr);
38539 parens.require_close (parser);
38541 else
38543 /* The only expression that is available got parsed for the noexcept
38544 already. noexcept is true then. */
38545 expr = noex;
38546 noex = boolean_true_node;
38549 expr = build_transaction_expr (token->location, expr, this_in, noex);
38550 parser->in_transaction = old_in;
38552 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38553 return error_mark_node;
38555 return (flag_tm ? expr : error_mark_node);
38558 /* Parse a function-transaction-block.
38560 function-transaction-block:
38561 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38562 function-body
38563 __transaction_atomic txn-attribute[opt] function-try-block
38564 __transaction_relaxed ctor-initializer[opt] function-body
38565 __transaction_relaxed function-try-block
38568 static void
38569 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38571 unsigned char old_in = parser->in_transaction;
38572 unsigned char new_in = 1;
38573 tree compound_stmt, stmt, attrs;
38574 cp_token *token;
38576 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38577 || keyword == RID_TRANSACTION_RELAXED);
38578 token = cp_parser_require_keyword (parser, keyword,
38579 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38580 : RT_TRANSACTION_RELAXED));
38581 gcc_assert (token != NULL);
38583 if (keyword == RID_TRANSACTION_RELAXED)
38584 new_in |= TM_STMT_ATTR_RELAXED;
38585 else
38587 attrs = cp_parser_txn_attribute_opt (parser);
38588 if (attrs)
38589 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38592 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38594 parser->in_transaction = new_in;
38596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38597 cp_parser_function_try_block (parser);
38598 else
38599 cp_parser_ctor_initializer_opt_and_function_body
38600 (parser, /*in_function_try_block=*/false);
38602 parser->in_transaction = old_in;
38604 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38607 /* Parse a __transaction_cancel statement.
38609 cancel-statement:
38610 __transaction_cancel txn-attribute[opt] ;
38611 __transaction_cancel txn-attribute[opt] throw-expression ;
38613 ??? Cancel and throw is not yet implemented. */
38615 static tree
38616 cp_parser_transaction_cancel (cp_parser *parser)
38618 cp_token *token;
38619 bool is_outer = false;
38620 tree stmt, attrs;
38622 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38623 RT_TRANSACTION_CANCEL);
38624 gcc_assert (token != NULL);
38626 attrs = cp_parser_txn_attribute_opt (parser);
38627 if (attrs)
38628 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38630 /* ??? Parse cancel-and-throw here. */
38632 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38634 if (!flag_tm)
38636 error_at (token->location, "%<__transaction_cancel%> without "
38637 "transactional memory support enabled");
38638 return error_mark_node;
38640 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38642 error_at (token->location, "%<__transaction_cancel%> within a "
38643 "%<__transaction_relaxed%>");
38644 return error_mark_node;
38646 else if (is_outer)
38648 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38649 && !is_tm_may_cancel_outer (current_function_decl))
38651 error_at (token->location, "outer %<__transaction_cancel%> not "
38652 "within outer %<__transaction_atomic%>");
38653 error_at (token->location,
38654 " or a %<transaction_may_cancel_outer%> function");
38655 return error_mark_node;
38658 else if (parser->in_transaction == 0)
38660 error_at (token->location, "%<__transaction_cancel%> not within "
38661 "%<__transaction_atomic%>");
38662 return error_mark_node;
38665 stmt = build_tm_abort_call (token->location, is_outer);
38666 add_stmt (stmt);
38668 return stmt;
38671 /* The parser. */
38673 static GTY (()) cp_parser *the_parser;
38676 /* Special handling for the first token or line in the file. The first
38677 thing in the file might be #pragma GCC pch_preprocess, which loads a
38678 PCH file, which is a GC collection point. So we need to handle this
38679 first pragma without benefit of an existing lexer structure.
38681 Always returns one token to the caller in *FIRST_TOKEN. This is
38682 either the true first token of the file, or the first token after
38683 the initial pragma. */
38685 static void
38686 cp_parser_initial_pragma (cp_token *first_token)
38688 tree name = NULL;
38690 cp_lexer_get_preprocessor_token (NULL, first_token);
38691 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38692 return;
38694 cp_lexer_get_preprocessor_token (NULL, first_token);
38695 if (first_token->type == CPP_STRING)
38697 name = first_token->u.value;
38699 cp_lexer_get_preprocessor_token (NULL, first_token);
38700 if (first_token->type != CPP_PRAGMA_EOL)
38701 error_at (first_token->location,
38702 "junk at end of %<#pragma GCC pch_preprocess%>");
38704 else
38705 error_at (first_token->location, "expected string literal");
38707 /* Skip to the end of the pragma. */
38708 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38709 cp_lexer_get_preprocessor_token (NULL, first_token);
38711 /* Now actually load the PCH file. */
38712 if (name)
38713 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38715 /* Read one more token to return to our caller. We have to do this
38716 after reading the PCH file in, since its pointers have to be
38717 live. */
38718 cp_lexer_get_preprocessor_token (NULL, first_token);
38721 /* Parse a pragma GCC ivdep. */
38723 static bool
38724 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38726 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38727 return true;
38730 /* Parse a pragma GCC unroll. */
38732 static unsigned short
38733 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38735 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38736 tree expr = cp_parser_constant_expression (parser);
38737 unsigned short unroll;
38738 expr = maybe_constant_value (expr);
38739 HOST_WIDE_INT lunroll = 0;
38740 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38741 || TREE_CODE (expr) != INTEGER_CST
38742 || (lunroll = tree_to_shwi (expr)) < 0
38743 || lunroll >= USHRT_MAX)
38745 error_at (location, "%<#pragma GCC unroll%> requires an"
38746 " assignment-expression that evaluates to a non-negative"
38747 " integral constant less than %u", USHRT_MAX);
38748 unroll = 0;
38750 else
38752 unroll = (unsigned short)lunroll;
38753 if (unroll == 0)
38754 unroll = 1;
38756 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38757 return unroll;
38760 /* Normal parsing of a pragma token. Here we can (and must) use the
38761 regular lexer. */
38763 static bool
38764 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38766 cp_token *pragma_tok;
38767 unsigned int id;
38768 tree stmt;
38769 bool ret;
38771 pragma_tok = cp_lexer_consume_token (parser->lexer);
38772 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38773 parser->lexer->in_pragma = true;
38775 id = cp_parser_pragma_kind (pragma_tok);
38776 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38777 cp_ensure_no_omp_declare_simd (parser);
38778 switch (id)
38780 case PRAGMA_GCC_PCH_PREPROCESS:
38781 error_at (pragma_tok->location,
38782 "%<#pragma GCC pch_preprocess%> must be first");
38783 break;
38785 case PRAGMA_OMP_BARRIER:
38786 switch (context)
38788 case pragma_compound:
38789 cp_parser_omp_barrier (parser, pragma_tok);
38790 return false;
38791 case pragma_stmt:
38792 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38793 "used in compound statements", "omp barrier");
38794 break;
38795 default:
38796 goto bad_stmt;
38798 break;
38800 case PRAGMA_OMP_FLUSH:
38801 switch (context)
38803 case pragma_compound:
38804 cp_parser_omp_flush (parser, pragma_tok);
38805 return false;
38806 case pragma_stmt:
38807 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38808 "used in compound statements", "omp flush");
38809 break;
38810 default:
38811 goto bad_stmt;
38813 break;
38815 case PRAGMA_OMP_TASKWAIT:
38816 switch (context)
38818 case pragma_compound:
38819 cp_parser_omp_taskwait (parser, pragma_tok);
38820 return false;
38821 case pragma_stmt:
38822 error_at (pragma_tok->location,
38823 "%<#pragma %s%> may only be used in compound statements",
38824 "omp taskwait");
38825 break;
38826 default:
38827 goto bad_stmt;
38829 break;
38831 case PRAGMA_OMP_TASKYIELD:
38832 switch (context)
38834 case pragma_compound:
38835 cp_parser_omp_taskyield (parser, pragma_tok);
38836 return false;
38837 case pragma_stmt:
38838 error_at (pragma_tok->location,
38839 "%<#pragma %s%> may only be used in compound statements",
38840 "omp taskyield");
38841 break;
38842 default:
38843 goto bad_stmt;
38845 break;
38847 case PRAGMA_OMP_CANCEL:
38848 switch (context)
38850 case pragma_compound:
38851 cp_parser_omp_cancel (parser, pragma_tok);
38852 return false;
38853 case pragma_stmt:
38854 error_at (pragma_tok->location,
38855 "%<#pragma %s%> may only be used in compound statements",
38856 "omp cancel");
38857 break;
38858 default:
38859 goto bad_stmt;
38861 break;
38863 case PRAGMA_OMP_CANCELLATION_POINT:
38864 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38865 return false;
38867 case PRAGMA_OMP_THREADPRIVATE:
38868 cp_parser_omp_threadprivate (parser, pragma_tok);
38869 return false;
38871 case PRAGMA_OMP_DECLARE:
38872 return cp_parser_omp_declare (parser, pragma_tok, context);
38874 case PRAGMA_OACC_DECLARE:
38875 cp_parser_oacc_declare (parser, pragma_tok);
38876 return false;
38878 case PRAGMA_OACC_ENTER_DATA:
38879 if (context == pragma_stmt)
38881 error_at (pragma_tok->location,
38882 "%<#pragma %s%> may only be used in compound statements",
38883 "acc enter data");
38884 break;
38886 else if (context != pragma_compound)
38887 goto bad_stmt;
38888 cp_parser_omp_construct (parser, pragma_tok, if_p);
38889 return true;
38891 case PRAGMA_OACC_EXIT_DATA:
38892 if (context == pragma_stmt)
38894 error_at (pragma_tok->location,
38895 "%<#pragma %s%> may only be used in compound statements",
38896 "acc exit data");
38897 break;
38899 else if (context != pragma_compound)
38900 goto bad_stmt;
38901 cp_parser_omp_construct (parser, pragma_tok, if_p);
38902 return true;
38904 case PRAGMA_OACC_ROUTINE:
38905 if (context != pragma_external)
38907 error_at (pragma_tok->location,
38908 "%<#pragma acc routine%> must be at file scope");
38909 break;
38911 cp_parser_oacc_routine (parser, pragma_tok, context);
38912 return false;
38914 case PRAGMA_OACC_UPDATE:
38915 if (context == pragma_stmt)
38917 error_at (pragma_tok->location,
38918 "%<#pragma %s%> may only be used in compound statements",
38919 "acc update");
38920 break;
38922 else if (context != pragma_compound)
38923 goto bad_stmt;
38924 cp_parser_omp_construct (parser, pragma_tok, if_p);
38925 return true;
38927 case PRAGMA_OACC_WAIT:
38928 if (context == pragma_stmt)
38930 error_at (pragma_tok->location,
38931 "%<#pragma %s%> may only be used in compound statements",
38932 "acc wait");
38933 break;
38935 else if (context != pragma_compound)
38936 goto bad_stmt;
38937 cp_parser_omp_construct (parser, pragma_tok, if_p);
38938 return true;
38940 case PRAGMA_OACC_ATOMIC:
38941 case PRAGMA_OACC_CACHE:
38942 case PRAGMA_OACC_DATA:
38943 case PRAGMA_OACC_HOST_DATA:
38944 case PRAGMA_OACC_KERNELS:
38945 case PRAGMA_OACC_PARALLEL:
38946 case PRAGMA_OACC_LOOP:
38947 case PRAGMA_OMP_ATOMIC:
38948 case PRAGMA_OMP_CRITICAL:
38949 case PRAGMA_OMP_DISTRIBUTE:
38950 case PRAGMA_OMP_FOR:
38951 case PRAGMA_OMP_MASTER:
38952 case PRAGMA_OMP_PARALLEL:
38953 case PRAGMA_OMP_SECTIONS:
38954 case PRAGMA_OMP_SIMD:
38955 case PRAGMA_OMP_SINGLE:
38956 case PRAGMA_OMP_TASK:
38957 case PRAGMA_OMP_TASKGROUP:
38958 case PRAGMA_OMP_TASKLOOP:
38959 case PRAGMA_OMP_TEAMS:
38960 if (context != pragma_stmt && context != pragma_compound)
38961 goto bad_stmt;
38962 stmt = push_omp_privatization_clauses (false);
38963 cp_parser_omp_construct (parser, pragma_tok, if_p);
38964 pop_omp_privatization_clauses (stmt);
38965 return true;
38967 case PRAGMA_OMP_ORDERED:
38968 if (context != pragma_stmt && context != pragma_compound)
38969 goto bad_stmt;
38970 stmt = push_omp_privatization_clauses (false);
38971 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38972 pop_omp_privatization_clauses (stmt);
38973 return ret;
38975 case PRAGMA_OMP_TARGET:
38976 if (context != pragma_stmt && context != pragma_compound)
38977 goto bad_stmt;
38978 stmt = push_omp_privatization_clauses (false);
38979 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38980 pop_omp_privatization_clauses (stmt);
38981 return ret;
38983 case PRAGMA_OMP_END_DECLARE_TARGET:
38984 cp_parser_omp_end_declare_target (parser, pragma_tok);
38985 return false;
38987 case PRAGMA_OMP_SECTION:
38988 error_at (pragma_tok->location,
38989 "%<#pragma omp section%> may only be used in "
38990 "%<#pragma omp sections%> construct");
38991 break;
38993 case PRAGMA_IVDEP:
38995 if (context == pragma_external)
38997 error_at (pragma_tok->location,
38998 "%<#pragma GCC ivdep%> must be inside a function");
38999 break;
39001 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39002 unsigned short unroll;
39003 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39004 if (tok->type == CPP_PRAGMA
39005 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39007 tok = cp_lexer_consume_token (parser->lexer);
39008 unroll = cp_parser_pragma_unroll (parser, tok);
39009 tok = cp_lexer_peek_token (the_parser->lexer);
39011 else
39012 unroll = 0;
39013 if (tok->type != CPP_KEYWORD
39014 || (tok->keyword != RID_FOR
39015 && tok->keyword != RID_WHILE
39016 && tok->keyword != RID_DO))
39018 cp_parser_error (parser, "for, while or do statement expected");
39019 return false;
39021 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39022 return true;
39025 case PRAGMA_UNROLL:
39027 if (context == pragma_external)
39029 error_at (pragma_tok->location,
39030 "%<#pragma GCC unroll%> must be inside a function");
39031 break;
39033 const unsigned short unroll
39034 = cp_parser_pragma_unroll (parser, pragma_tok);
39035 bool ivdep;
39036 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39037 if (tok->type == CPP_PRAGMA
39038 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39040 tok = cp_lexer_consume_token (parser->lexer);
39041 ivdep = cp_parser_pragma_ivdep (parser, tok);
39042 tok = cp_lexer_peek_token (the_parser->lexer);
39044 else
39045 ivdep = false;
39046 if (tok->type != CPP_KEYWORD
39047 || (tok->keyword != RID_FOR
39048 && tok->keyword != RID_WHILE
39049 && tok->keyword != RID_DO))
39051 cp_parser_error (parser, "for, while or do statement expected");
39052 return false;
39054 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39055 return true;
39058 default:
39059 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39060 c_invoke_pragma_handler (id);
39061 break;
39063 bad_stmt:
39064 cp_parser_error (parser, "expected declaration specifiers");
39065 break;
39068 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39069 return false;
39072 /* The interface the pragma parsers have to the lexer. */
39074 enum cpp_ttype
39075 pragma_lex (tree *value, location_t *loc)
39077 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39078 enum cpp_ttype ret = tok->type;
39080 *value = tok->u.value;
39081 if (loc)
39082 *loc = tok->location;
39084 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39085 ret = CPP_EOF;
39086 else if (ret == CPP_STRING)
39087 *value = cp_parser_string_literal (the_parser, false, false);
39088 else
39090 if (ret == CPP_KEYWORD)
39091 ret = CPP_NAME;
39092 cp_lexer_consume_token (the_parser->lexer);
39095 return ret;
39099 /* External interface. */
39101 /* Parse one entire translation unit. */
39103 void
39104 c_parse_file (void)
39106 static bool already_called = false;
39108 if (already_called)
39109 fatal_error (input_location,
39110 "inter-module optimizations not implemented for C++");
39111 already_called = true;
39113 the_parser = cp_parser_new ();
39114 push_deferring_access_checks (flag_access_control
39115 ? dk_no_deferred : dk_no_check);
39116 cp_parser_translation_unit (the_parser);
39117 the_parser = NULL;
39119 finish_translation_unit ();
39122 /* Create an identifier for a generic parameter type (a synthesized
39123 template parameter implied by `auto' or a concept identifier). */
39125 static GTY(()) int generic_parm_count;
39126 static tree
39127 make_generic_type_name ()
39129 char buf[32];
39130 sprintf (buf, "auto:%d", ++generic_parm_count);
39131 return get_identifier (buf);
39134 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39135 (creating a new template parameter list if necessary). Returns the newly
39136 created template type parm. */
39138 static tree
39139 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39141 gcc_assert (current_binding_level->kind == sk_function_parms);
39143 /* Before committing to modifying any scope, if we're in an
39144 implicit template scope, and we're trying to synthesize a
39145 constrained parameter, try to find a previous parameter with
39146 the same name. This is the same-type rule for abbreviated
39147 function templates.
39149 NOTE: We can generate implicit parameters when tentatively
39150 parsing a nested name specifier, only to reject that parse
39151 later. However, matching the same template-id as part of a
39152 direct-declarator should generate an identical template
39153 parameter, so this rule will merge them. */
39154 if (parser->implicit_template_scope && constr)
39156 tree t = parser->implicit_template_parms;
39157 while (t)
39159 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39161 tree d = TREE_VALUE (t);
39162 if (TREE_CODE (d) == PARM_DECL)
39163 /* Return the TEMPLATE_PARM_INDEX. */
39164 d = DECL_INITIAL (d);
39165 return d;
39167 t = TREE_CHAIN (t);
39171 /* We are either continuing a function template that already contains implicit
39172 template parameters, creating a new fully-implicit function template, or
39173 extending an existing explicit function template with implicit template
39174 parameters. */
39176 cp_binding_level *const entry_scope = current_binding_level;
39178 bool become_template = false;
39179 cp_binding_level *parent_scope = 0;
39181 if (parser->implicit_template_scope)
39183 gcc_assert (parser->implicit_template_parms);
39185 current_binding_level = parser->implicit_template_scope;
39187 else
39189 /* Roll back to the existing template parameter scope (in the case of
39190 extending an explicit function template) or introduce a new template
39191 parameter scope ahead of the function parameter scope (or class scope
39192 in the case of out-of-line member definitions). The function scope is
39193 added back after template parameter synthesis below. */
39195 cp_binding_level *scope = entry_scope;
39197 while (scope->kind == sk_function_parms)
39199 parent_scope = scope;
39200 scope = scope->level_chain;
39202 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39204 /* If not defining a class, then any class scope is a scope level in
39205 an out-of-line member definition. In this case simply wind back
39206 beyond the first such scope to inject the template parameter list.
39207 Otherwise wind back to the class being defined. The latter can
39208 occur in class member friend declarations such as:
39210 class A {
39211 void foo (auto);
39213 class B {
39214 friend void A::foo (auto);
39217 The template parameter list synthesized for the friend declaration
39218 must be injected in the scope of 'B'. This can also occur in
39219 erroneous cases such as:
39221 struct A {
39222 struct B {
39223 void foo (auto);
39225 void B::foo (auto) {}
39228 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39229 but, nevertheless, the template parameter list synthesized for the
39230 declarator should be injected into the scope of 'A' as if the
39231 ill-formed template was specified explicitly. */
39233 while (scope->kind == sk_class && !scope->defining_class_p)
39235 parent_scope = scope;
39236 scope = scope->level_chain;
39240 current_binding_level = scope;
39242 if (scope->kind != sk_template_parms
39243 || !function_being_declared_is_template_p (parser))
39245 /* Introduce a new template parameter list for implicit template
39246 parameters. */
39248 become_template = true;
39250 parser->implicit_template_scope
39251 = begin_scope (sk_template_parms, NULL);
39253 ++processing_template_decl;
39255 parser->fully_implicit_function_template_p = true;
39256 ++parser->num_template_parameter_lists;
39258 else
39260 /* Synthesize implicit template parameters at the end of the explicit
39261 template parameter list. */
39263 gcc_assert (current_template_parms);
39265 parser->implicit_template_scope = scope;
39267 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39268 parser->implicit_template_parms
39269 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39273 /* Synthesize a new template parameter and track the current template
39274 parameter chain with implicit_template_parms. */
39276 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39277 tree synth_id = make_generic_type_name ();
39278 tree synth_tmpl_parm;
39279 bool non_type = false;
39281 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39282 synth_tmpl_parm
39283 = finish_template_type_parm (class_type_node, synth_id);
39284 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39285 synth_tmpl_parm
39286 = finish_constrained_template_template_parm (proto, synth_id);
39287 else
39289 synth_tmpl_parm = copy_decl (proto);
39290 DECL_NAME (synth_tmpl_parm) = synth_id;
39291 non_type = true;
39294 // Attach the constraint to the parm before processing.
39295 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39296 TREE_TYPE (node) = constr;
39297 tree new_parm
39298 = process_template_parm (parser->implicit_template_parms,
39299 input_location,
39300 node,
39301 /*non_type=*/non_type,
39302 /*param_pack=*/false);
39304 // Chain the new parameter to the list of implicit parameters.
39305 if (parser->implicit_template_parms)
39306 parser->implicit_template_parms
39307 = TREE_CHAIN (parser->implicit_template_parms);
39308 else
39309 parser->implicit_template_parms = new_parm;
39311 tree new_decl = get_local_decls ();
39312 if (non_type)
39313 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39314 new_decl = DECL_INITIAL (new_decl);
39316 /* If creating a fully implicit function template, start the new implicit
39317 template parameter list with this synthesized type, otherwise grow the
39318 current template parameter list. */
39320 if (become_template)
39322 parent_scope->level_chain = current_binding_level;
39324 tree new_parms = make_tree_vec (1);
39325 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39326 current_template_parms = tree_cons (size_int (processing_template_decl),
39327 new_parms, current_template_parms);
39329 else
39331 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39332 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39333 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39334 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39337 // If the new parameter was constrained, we need to add that to the
39338 // constraints in the template parameter list.
39339 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39341 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39342 reqs = conjoin_constraints (reqs, req);
39343 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39346 current_binding_level = entry_scope;
39348 return new_decl;
39351 /* Finish the declaration of a fully implicit function template. Such a
39352 template has no explicit template parameter list so has not been through the
39353 normal template head and tail processing. synthesize_implicit_template_parm
39354 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39355 provided if the declaration is a class member such that its template
39356 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39357 form is returned. Otherwise NULL_TREE is returned. */
39359 static tree
39360 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39362 gcc_assert (parser->fully_implicit_function_template_p);
39364 if (member_decl_opt && member_decl_opt != error_mark_node
39365 && DECL_VIRTUAL_P (member_decl_opt))
39367 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39368 "implicit templates may not be %<virtual%>");
39369 DECL_VIRTUAL_P (member_decl_opt) = false;
39372 if (member_decl_opt)
39373 member_decl_opt = finish_member_template_decl (member_decl_opt);
39374 end_template_decl ();
39376 parser->fully_implicit_function_template_p = false;
39377 parser->implicit_template_parms = 0;
39378 parser->implicit_template_scope = 0;
39379 --parser->num_template_parameter_lists;
39381 return member_decl_opt;
39384 /* Like finish_fully_implicit_template, but to be used in error
39385 recovery, rearranging scopes so that we restore the state we had
39386 before synthesize_implicit_template_parm inserted the implement
39387 template parms scope. */
39389 static void
39390 abort_fully_implicit_template (cp_parser *parser)
39392 cp_binding_level *return_to_scope = current_binding_level;
39394 if (parser->implicit_template_scope
39395 && return_to_scope != parser->implicit_template_scope)
39397 cp_binding_level *child = return_to_scope;
39398 for (cp_binding_level *scope = child->level_chain;
39399 scope != parser->implicit_template_scope;
39400 scope = child->level_chain)
39401 child = scope;
39402 child->level_chain = parser->implicit_template_scope->level_chain;
39403 parser->implicit_template_scope->level_chain = return_to_scope;
39404 current_binding_level = parser->implicit_template_scope;
39406 else
39407 return_to_scope = return_to_scope->level_chain;
39409 finish_fully_implicit_template (parser, NULL);
39411 gcc_assert (current_binding_level == return_to_scope);
39414 /* Helper function for diagnostics that have complained about things
39415 being used with 'extern "C"' linkage.
39417 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39419 void
39420 maybe_show_extern_c_location (void)
39422 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39423 inform (the_parser->innermost_linkage_specification_location,
39424 "%<extern \"C\"%> linkage started here");
39427 #include "gt-cp-parser.h"