PR c++/85815 - reference to member of enclosing template.
[official-gcc.git] / gcc / cp / parser.c
blobd17beb8f93078194596f934b516631911e2cddd3
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
250 static void cp_parser_initial_pragma
251 (cp_token *);
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
262 /* Variables. */
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288 if (buffer == NULL)
289 return;
291 if (num == 0)
292 num = buffer->length ();
294 if (start_token == NULL)
295 start_token = buffer->address ();
297 if (start_token > buffer->address ())
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 if (token == start_token)
308 do_print = true;
310 if (!do_print)
311 continue;
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
317 cp_lexer_print_token (file, token);
319 if (token == curr_token)
320 fprintf (file, "]]");
322 switch (token->type)
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
331 default:
332 fputc (' ', file);
336 if (i == num && i < buffer->length ())
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
342 fprintf (file, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 if (t)
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
384 /* Dump parser context C to FILE. */
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 unsigned i;
402 cp_parser_context *c;
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
423 /* Print an unparsed function entry UF to FILE. */
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
460 fprintf (file, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 cp_token *next_token, *first_token, *start_token;
490 if (file == NULL)
491 file = stderr;
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
513 if (file == NULL)
514 file = stderr;
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
592 cp_debug_parser (stderr, &ref);
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
606 static cp_lexer *
607 cp_lexer_alloc (void)
609 cp_lexer *lexer;
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
624 return lexer;
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
631 static cp_lexer *
632 cp_lexer_new_main (void)
634 cp_lexer *lexer;
635 cp_token token;
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
642 lexer = cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
693 /* Frees all resources associated with LEXER. */
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
718 return lexer->debugging_p;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
727 return lexer->next_token - previous_p;
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
733 return pos;
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
763 return cp_lexer_token_at (lexer, tp);
766 /* nonzero if we are presently saving tokens. */
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
771 return lexer->saved_tokens.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
782 static int is_extern_c = 0;
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
809 else
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
826 token->keyword = RID_MAX;
829 else if (token->type == CPP_AT_NAME)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
860 if (token->type != CPP_EOF)
862 input_location = token->location;
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
880 if (cp_lexer_debugging_p (lexer))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
886 return lexer->next_token;
889 /* Return true if the next token has the indicated TYPE. */
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
894 return cp_lexer_peek_token (lexer)->type == type;
897 /* Return true if the next token does not have the indicated TYPE. */
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
902 return !cp_lexer_next_token_is (lexer, type);
905 /* Return true if the next token is the indicated KEYWORD. */
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 /* Return true if the next token is not the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
933 /* Return true if KEYWORD can start a decl-specifier. */
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
938 switch (keyword)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
987 /* Return true if the next token is a keyword for a decl-specifier. */
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
992 cp_token *token;
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
998 /* Returns TRUE iff the token T begins a decltype type. */
1000 static bool
1001 token_is_decltype (cp_token *t)
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1034 /* Return the stored value. */
1035 return check_value->value;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1047 cp_token *token;
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1061 ++token;
1062 if (token == lexer->last_token)
1064 token = &eof_token;
1065 break;
1068 if (!token->purged_p)
1069 --n;
1072 if (cp_lexer_debugging_p (lexer))
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1078 return token;
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1087 cp_token *token = lexer->next_token;
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1097 lexer->next_token = &eof_token;
1098 break;
1102 while (lexer->next_token->purged_p);
1104 cp_lexer_set_source_position_from_token (token);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1114 return token;
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1124 cp_token *tok = lexer->next_token;
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1134 tok++;
1135 if (tok == lexer->last_token)
1137 tok = &eof_token;
1138 break;
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1152 cp_token *peek = lexer->next_token;
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1157 gcc_assert (tok < peek);
1159 for ( tok += 1; tok != peek; tok += 1)
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1181 /* Commit to the portion of the token stream most recently saved. */
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1190 lexer->saved_tokens.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1203 lexer->next_token = lexer->saved_tokens.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1211 struct saved_token_sentinel
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1221 void rollback ()
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1226 ~saved_token_sentinel()
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1234 /* Print a representation of the TOKEN on the STREAM. */
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1307 /* Start emitting debugging information. */
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1320 /* Stop emitting debugging information. */
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1366 if (fndecl == error_mark_node)
1368 parser->omp_declare_simd = NULL;
1369 return;
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1454 return declarator;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1467 cp_declarator *declarator;
1469 /* It is valid to write:
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1491 return declarator;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1503 cp_declarator *declarator;
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1520 return declarator;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1531 cp_declarator *declarator;
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1543 else
1544 declarator->parameter_pack_p = false;
1546 declarator->std_attributes = attributes;
1548 return declarator;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1560 cp_declarator *declarator;
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1567 if (pointee)
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1572 else
1573 declarator->parameter_pack_p = false;
1575 declarator->std_attributes = attributes;
1577 return declarator;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1596 cp_declarator *declarator;
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1614 else
1615 declarator->parameter_pack_p = false;
1617 return declarator;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1626 cp_declarator *declarator;
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 return declarator;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1659 switch ((int)declarator->kind)
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1667 case cdk_error:
1668 return true;
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1676 return !found;
1679 cp_parameter_declarator *no_parameters;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1691 cp_parameter_declarator *parameter;
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1705 return parameter;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1713 while (declarator)
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1724 return false;
1727 /* The parser. */
1729 /* Overview
1730 --------
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1753 Methodology
1754 -----------
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1773 Future Improvements
1774 -------------------
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1785 enum
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1825 enum cp_parser_prec
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1877 /* Prototypes. */
1879 /* Constructors and destructors. */
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1884 /* Class variables. */
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1938 cp_parser_context *context;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1964 return context;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1992 /* Prototypes. */
1994 /* Constructors and destructors. */
1996 static cp_parser *cp_parser_new
1997 (void);
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051 bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059 (cp_token *);
2060 static tree cp_parser_new_expression
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063 (cp_parser *);
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067 (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071 (cp_parser *);
2072 static tree cp_parser_delete_expression
2073 (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083 (cp_parser *);
2084 static cp_expr cp_parser_expression
2085 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089 (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091 (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093 (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097 (cp_parser *, tree);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106 (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108 (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112 (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114 (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116 (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120 (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126 (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130 (tree, tree);
2131 static tree cp_parser_jump_statement
2132 (cp_parser *);
2133 static void cp_parser_declaration_statement
2134 (cp_parser *);
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser *, bool *, const token_indent_info &);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2144 (cp_parser *);
2145 static void cp_parser_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165 (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167 (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171 (cp_parser *);
2172 static void cp_parser_enumerator_list
2173 (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177 (cp_parser *);
2178 static void cp_parser_namespace_definition
2179 (cp_parser *);
2180 static void cp_parser_namespace_body
2181 (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183 (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185 (cp_parser *);
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189 (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191 (cp_parser *);
2192 static void cp_parser_asm_definition
2193 (cp_parser *);
2194 static void cp_parser_linkage_specification
2195 (cp_parser *);
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199 (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207 bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225 (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227 (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229 (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232 (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236 (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
2267 static void abort_fully_implicit_template
2268 (cp_parser *);
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2275 (cp_parser *);
2276 static tree cp_parser_class_head
2277 (cp_parser *, bool *);
2278 static enum tag_types cp_parser_class_key
2279 (cp_parser *);
2280 static void cp_parser_type_parameter_key
2281 (cp_parser* parser);
2282 static void cp_parser_member_specification_opt
2283 (cp_parser *);
2284 static void cp_parser_member_declaration
2285 (cp_parser *);
2286 static tree cp_parser_pure_specifier
2287 (cp_parser *);
2288 static tree cp_parser_constant_initializer
2289 (cp_parser *);
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2294 (cp_parser *);
2295 static tree cp_parser_base_specifier
2296 (cp_parser *);
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2301 (cp_parser *);
2302 static tree cp_parser_conversion_type_id
2303 (cp_parser *);
2304 static cp_declarator *cp_parser_conversion_declarator_opt
2305 (cp_parser *);
2306 static void cp_parser_ctor_initializer_opt
2307 (cp_parser *);
2308 static void cp_parser_mem_initializer_list
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer_id
2313 (cp_parser *);
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2318 (cp_parser *);
2319 static cp_expr cp_parser_operator
2320 (cp_parser *);
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser *, bool);
2326 static tree cp_parser_template_parameter_list
2327 (cp_parser *);
2328 static tree cp_parser_template_parameter
2329 (cp_parser *, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser *, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser *, bool, bool, enum tag_types, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2336 static tree cp_parser_template_argument_list
2337 (cp_parser *);
2338 static tree cp_parser_template_argument
2339 (cp_parser *);
2340 static void cp_parser_explicit_instantiation
2341 (cp_parser *);
2342 static void cp_parser_explicit_specialization
2343 (cp_parser *);
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2348 (cp_parser *);
2349 static void cp_parser_function_try_block
2350 (cp_parser *);
2351 static void cp_parser_handler_seq
2352 (cp_parser *);
2353 static void cp_parser_handler
2354 (cp_parser *);
2355 static tree cp_parser_exception_declaration
2356 (cp_parser *);
2357 static tree cp_parser_throw_expression
2358 (cp_parser *);
2359 static tree cp_parser_exception_specification_opt
2360 (cp_parser *);
2361 static tree cp_parser_type_id_list
2362 (cp_parser *);
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2367 (cp_parser *);
2368 static tree cp_parser_asm_operand_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_clobber_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_label_list
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_std_attribute_p
2379 (cp_parser *);
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser *, size_t);
2386 static tree cp_parser_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attribute_list
2391 (cp_parser *);
2392 static tree cp_parser_std_attribute
2393 (cp_parser *, tree);
2394 static tree cp_parser_std_attribute_spec
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute_spec_seq
2397 (cp_parser *);
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser *, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser *, int *);
2402 static void cp_parser_label_declaration
2403 (cp_parser *);
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2408 (cp_parser *);
2409 static tree cp_parser_requires_clause_opt
2410 (cp_parser *);
2411 static tree cp_parser_requires_expression
2412 (cp_parser *);
2413 static tree cp_parser_requirement_parameter_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement_body
2416 (cp_parser *);
2417 static tree cp_parser_requirement_list
2418 (cp_parser *);
2419 static tree cp_parser_requirement
2420 (cp_parser *);
2421 static tree cp_parser_simple_requirement
2422 (cp_parser *);
2423 static tree cp_parser_compound_requirement
2424 (cp_parser *);
2425 static tree cp_parser_type_requirement
2426 (cp_parser *);
2427 static tree cp_parser_nested_requirement
2428 (cp_parser *);
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser *, cp_token *);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser *, enum rid);
2436 static void cp_parser_function_transaction
2437 (cp_parser *, enum rid);
2438 static tree cp_parser_transaction_cancel
2439 (cp_parser *);
2441 enum pragma_context {
2442 pragma_external,
2443 pragma_member,
2444 pragma_objc_icode,
2445 pragma_stmt,
2446 pragma_compound
2448 static bool cp_parser_pragma
2449 (cp_parser *, enum pragma_context, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_args
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_encode_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_defs_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_protocol_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_selector_expression
2466 (cp_parser *);
2467 static cp_expr cp_parser_objc_expression
2468 (cp_parser *);
2469 static bool cp_parser_objc_selector_p
2470 (enum cpp_ttype);
2471 static tree cp_parser_objc_selector
2472 (cp_parser *);
2473 static tree cp_parser_objc_protocol_refs_opt
2474 (cp_parser *);
2475 static void cp_parser_objc_declaration
2476 (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2478 (cp_parser *);
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_synthesize_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_dynamic_declaration
2486 (cp_parser *) ;
2487 static tree cp_parser_objc_struct_declaration
2488 (cp_parser *) ;
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser *, tree, location_t);
2496 static tree cp_parser_maybe_treat_template_as_class
2497 (tree, bool);
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser *, cp_declarator *, location_t);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2502 static cp_expr cp_parser_simple_cast_expression
2503 (cp_parser *);
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser *, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser *, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser *, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser *, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec<deferred_access_check, va_gc> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser *, tree);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2522 static tree cp_parser_save_nsdmi
2523 (cp_parser *);
2524 static tree cp_parser_enclosed_template_argument_list
2525 (cp_parser *);
2526 static void cp_parser_save_default_args
2527 (cp_parser *, tree);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser *, tree);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser *, tree, tree, tree);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser *, tree);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser *, tree);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser *, enum rid);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser *, enum rid);
2540 static bool cp_parser_declares_only_class_p
2541 (cp_parser *);
2542 static void cp_parser_set_storage_class
2543 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq *decl_specs,
2548 cp_decl_spec ds, cp_token *);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq *);
2551 static void cp_parser_required_error
2552 (cp_parser *, required_token, bool, location_t);
2553 static cp_token *cp_parser_require
2554 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2555 static cp_token *cp_parser_require_keyword
2556 (cp_parser *, enum rid, required_token);
2557 static bool cp_parser_token_starts_function_definition_p
2558 (cp_token *);
2559 static bool cp_parser_next_token_starts_class_definition_p
2560 (cp_parser *);
2561 static bool cp_parser_next_token_ends_template_argument_p
2562 (cp_parser *);
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser *, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2566 (cp_token *);
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2568 (cp_token *);
2569 static void cp_parser_check_class_key
2570 (enum tag_types, tree type);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type, location_t location);
2573 static bool cp_parser_optional_template_keyword
2574 (cp_parser *);
2575 static void cp_parser_pre_parsed_nested_name_specifier
2576 (cp_parser *);
2577 static bool cp_parser_cache_group
2578 (cp_parser *, enum cpp_ttype, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser *parser, bool nsdmi);
2581 static void cp_parser_parse_tentatively
2582 (cp_parser *);
2583 static void cp_parser_commit_to_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_commit_to_topmost_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_abort_tentative_parse
2588 (cp_parser *);
2589 static bool cp_parser_parse_definitely
2590 (cp_parser *);
2591 static inline bool cp_parser_parsing_tentatively
2592 (cp_parser *);
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2594 (cp_parser *);
2595 static void cp_parser_error
2596 (cp_parser *, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser *, tree, tree, name_lookup_error, location_t);
2599 static bool cp_parser_simulate_error
2600 (cp_parser *);
2601 static bool cp_parser_check_type_definition
2602 (cp_parser *);
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator *, tree, location_t type_location);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser *, tree, enum tag_types, location_t location);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser *, non_integral_constant);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser *, tree, location_t);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2612 (cp_parser *);
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser *, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_block_or_statement
2620 (cp_parser *);
2621 static bool cp_parser_skip_to_closing_brace
2622 (cp_parser *);
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2624 (cp_parser *);
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2628 (cp_parser *);
2629 static bool cp_parser_allow_gnu_extensions_p
2630 (cp_parser *);
2631 static bool cp_parser_is_pure_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_keyword
2636 (cp_token *, enum rid);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser *, tree, location_t location);
2639 static cp_declarator * cp_parser_make_indirect_declarator
2640 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2641 static bool cp_parser_compound_literal_p
2642 (cp_parser *);
2643 static bool cp_parser_array_designator_p
2644 (cp_parser *);
2645 static bool cp_parser_init_statement_p
2646 (cp_parser *);
2647 static bool cp_parser_skip_to_closing_square_bracket
2648 (cp_parser *);
2650 /* Concept-related syntactic transformations */
2652 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2653 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand;
2662 ++c_inhibit_evaluation_warnings;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings;
2668 --cp_unevaluated_operand;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2676 static inline bool
2677 cp_parser_parsing_tentatively (cp_parser* parser)
2679 return parser->context->next != NULL;
2682 /* Returns nonzero if TOKEN is a string literal. */
2684 static bool
2685 cp_parser_is_pure_string_literal (cp_token* token)
2687 return (token->type == CPP_STRING ||
2688 token->type == CPP_STRING16 ||
2689 token->type == CPP_STRING32 ||
2690 token->type == CPP_WSTRING ||
2691 token->type == CPP_UTF8STRING);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2697 static bool
2698 cp_parser_is_string_literal (cp_token* token)
2700 return (cp_parser_is_pure_string_literal (token) ||
2701 token->type == CPP_STRING_USERDEF ||
2702 token->type == CPP_STRING16_USERDEF ||
2703 token->type == CPP_STRING32_USERDEF ||
2704 token->type == CPP_WSTRING_USERDEF ||
2705 token->type == CPP_UTF8STRING_USERDEF);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2710 static bool
2711 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2713 return token->keyword == keyword;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717 PRAGMA_NONE. */
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token *token)
2722 if (token->type != CPP_PRAGMA)
2723 return PRAGMA_NONE;
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2737 of the marker. */
2739 static bool
2740 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2741 location_t *out_loc)
2743 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2744 if (token2->type != tok1_kind)
2745 return false;
2746 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2747 if (token3->type != tok1_kind)
2748 return false;
2749 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2750 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2751 return false;
2753 /* It must be at the start of the line. */
2754 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2755 if (LOCATION_COLUMN (start_loc) != 1)
2756 return false;
2758 /* We have a conflict marker. Construct a location of the form:
2759 <<<<<<<
2760 ^~~~~~~
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc = get_finish (token4->location);
2763 *out_loc = make_location (start_loc, start_loc, finish_loc);
2765 return true;
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2769 RT_CLOSE_PAREN. */
2771 static const char *
2772 get_matching_symbol (required_token token_desc)
2774 switch (token_desc)
2776 default:
2777 gcc_unreachable ();
2778 return "";
2779 case RT_CLOSE_BRACE:
2780 return "{";
2781 case RT_CLOSE_PAREN:
2782 return "(";
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc)
2792 switch (token_desc)
2794 case RT_SEMICOLON:
2795 return CPP_SEMICOLON;
2796 case RT_OPEN_PAREN:
2797 return CPP_OPEN_PAREN;
2798 case RT_CLOSE_BRACE:
2799 return CPP_CLOSE_BRACE;
2800 case RT_OPEN_BRACE:
2801 return CPP_OPEN_BRACE;
2802 case RT_CLOSE_SQUARE:
2803 return CPP_CLOSE_SQUARE;
2804 case RT_OPEN_SQUARE:
2805 return CPP_OPEN_SQUARE;
2806 case RT_COMMA:
2807 return CPP_COMMA;
2808 case RT_COLON:
2809 return CPP_COLON;
2810 case RT_CLOSE_PAREN:
2811 return CPP_CLOSE_PAREN;
2813 default:
2814 /* Use CPP_EOF as a "no completions possible" code. */
2815 return CPP_EOF;
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2826 OTHER-TOKEN".
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2836 location. */
2838 static void
2839 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2840 required_token missing_token_desc,
2841 location_t matching_location)
2843 cp_token *token = cp_lexer_peek_token (parser->lexer);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token);
2848 if (token->type == CPP_PRAGMA)
2850 error_at (token->location,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser, token);
2853 return;
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token->type == CPP_LSHIFT
2858 || token->type == CPP_RSHIFT
2859 || token->type == CPP_EQ_EQ)
2861 location_t loc;
2862 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2864 error_at (loc, "version control conflict marker in file");
2865 return;
2869 gcc_rich_location richloc (input_location);
2871 bool added_matching_location = false;
2873 if (missing_token_desc != RT_NONE)
2875 /* Potentially supply a fix-it hint, suggesting to add the
2876 missing token immediately after the *previous* token.
2877 This may move the primary location within richloc. */
2878 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2879 location_t prev_token_loc
2880 = cp_lexer_previous_token (parser->lexer)->location;
2881 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2883 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884 Attempt to consolidate diagnostics by printing it as a
2885 secondary range within the main diagnostic. */
2886 if (matching_location != UNKNOWN_LOCATION)
2887 added_matching_location
2888 = richloc.add_location_if_nearby (matching_location);
2891 /* Actually emit the error. */
2892 c_parse_error (gmsgid,
2893 /* Because c_parser_error does not understand
2894 CPP_KEYWORD, keywords are treated like
2895 identifiers. */
2896 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2897 token->u.value, token->flags, &richloc);
2899 if (missing_token_desc != RT_NONE)
2901 /* If we weren't able to consolidate matching_location, then
2902 print it as a secondary diagnostic. */
2903 if (matching_location != UNKNOWN_LOCATION
2904 && !added_matching_location)
2905 inform (matching_location, "to match this %qs",
2906 get_matching_symbol (missing_token_desc));
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911 FILE:LINE: MESSAGE before TOKEN
2912 where TOKEN is the next token in the input stream. MESSAGE
2913 (specified by the caller) is usually of the form "expected
2914 OTHER-TOKEN". */
2916 static void
2917 cp_parser_error (cp_parser* parser, const char* gmsgid)
2919 if (!cp_parser_simulate_error (parser))
2920 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2923 /* Issue an error about name-lookup failing. NAME is the
2924 IDENTIFIER_NODE DECL is the result of
2925 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2926 the thing that we hoped to find. */
2928 static void
2929 cp_parser_name_lookup_error (cp_parser* parser,
2930 tree name,
2931 tree decl,
2932 name_lookup_error desired,
2933 location_t location)
2935 /* If name lookup completely failed, tell the user that NAME was not
2936 declared. */
2937 if (decl == error_mark_node)
2939 if (parser->scope && parser->scope != global_namespace)
2940 error_at (location, "%<%E::%E%> has not been declared",
2941 parser->scope, name);
2942 else if (parser->scope == global_namespace)
2943 error_at (location, "%<::%E%> has not been declared", name);
2944 else if (parser->object_scope
2945 && !CLASS_TYPE_P (parser->object_scope))
2946 error_at (location, "request for member %qE in non-class type %qT",
2947 name, parser->object_scope);
2948 else if (parser->object_scope)
2949 error_at (location, "%<%T::%E%> has not been declared",
2950 parser->object_scope, name);
2951 else
2952 error_at (location, "%qE has not been declared", name);
2954 else if (parser->scope && parser->scope != global_namespace)
2956 switch (desired)
2958 case NLE_TYPE:
2959 error_at (location, "%<%E::%E%> is not a type",
2960 parser->scope, name);
2961 break;
2962 case NLE_CXX98:
2963 error_at (location, "%<%E::%E%> is not a class or namespace",
2964 parser->scope, name);
2965 break;
2966 case NLE_NOT_CXX98:
2967 error_at (location,
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser->scope, name);
2970 break;
2971 default:
2972 gcc_unreachable ();
2976 else if (parser->scope == global_namespace)
2978 switch (desired)
2980 case NLE_TYPE:
2981 error_at (location, "%<::%E%> is not a type", name);
2982 break;
2983 case NLE_CXX98:
2984 error_at (location, "%<::%E%> is not a class or namespace", name);
2985 break;
2986 case NLE_NOT_CXX98:
2987 error_at (location,
2988 "%<::%E%> is not a class, namespace, or enumeration",
2989 name);
2990 break;
2991 default:
2992 gcc_unreachable ();
2995 else
2997 switch (desired)
2999 case NLE_TYPE:
3000 error_at (location, "%qE is not a type", name);
3001 break;
3002 case NLE_CXX98:
3003 error_at (location, "%qE is not a class or namespace", name);
3004 break;
3005 case NLE_NOT_CXX98:
3006 error_at (location,
3007 "%qE is not a class, namespace, or enumeration", name);
3008 break;
3009 default:
3010 gcc_unreachable ();
3015 /* If we are parsing tentatively, remember that an error has occurred
3016 during this tentative parse. Returns true if the error was
3017 simulated; false if a message should be issued by the caller. */
3019 static bool
3020 cp_parser_simulate_error (cp_parser* parser)
3022 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3024 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3025 return true;
3027 return false;
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
3032 issued. */
3034 static bool
3035 cp_parser_check_type_definition (cp_parser* parser)
3037 /* If types are forbidden here, issue a message. */
3038 if (parser->type_definition_forbidden_message)
3040 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041 in the message need to be interpreted. */
3042 error (parser->type_definition_forbidden_message);
3043 return false;
3045 return true;
3048 /* This function is called when the DECLARATOR is processed. The TYPE
3049 was a type defined in the decl-specifiers. If it is invalid to
3050 define a type in the decl-specifiers for DECLARATOR, an error is
3051 issued. TYPE_LOCATION is the location of TYPE and is used
3052 for error reporting. */
3054 static void
3055 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3056 tree type, location_t type_location)
3058 /* [dcl.fct] forbids type definitions in return types.
3059 Unfortunately, it's not easy to know whether or not we are
3060 processing a return type until after the fact. */
3061 while (declarator
3062 && (declarator->kind == cdk_pointer
3063 || declarator->kind == cdk_reference
3064 || declarator->kind == cdk_ptrmem))
3065 declarator = declarator->declarator;
3066 if (declarator
3067 && declarator->kind == cdk_function)
3069 error_at (type_location,
3070 "new types may not be defined in a return type");
3071 inform (type_location,
3072 "(perhaps a semicolon is missing after the definition of %qT)",
3073 type);
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078 "<" in any valid C++ program. If the next token is indeed "<",
3079 issue a message warning the user about what appears to be an
3080 invalid attempt to form a template-id. LOCATION is the location
3081 of the type-specifier (TYPE) */
3083 static void
3084 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3085 tree type,
3086 enum tag_types tag_type,
3087 location_t location)
3089 cp_token_position start = 0;
3091 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3093 if (TREE_CODE (type) == TYPE_DECL)
3094 type = TREE_TYPE (type);
3095 if (TYPE_P (type) && !template_placeholder_p (type))
3096 error_at (location, "%qT is not a template", type);
3097 else if (identifier_p (type))
3099 if (tag_type != none_type)
3100 error_at (location, "%qE is not a class template", type);
3101 else
3102 error_at (location, "%qE is not a template", type);
3104 else
3105 error_at (location, "invalid template-id");
3106 /* Remember the location of the invalid "<". */
3107 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3108 start = cp_lexer_token_position (parser->lexer, true);
3109 /* Consume the "<". */
3110 cp_lexer_consume_token (parser->lexer);
3111 /* Parse the template arguments. */
3112 cp_parser_enclosed_template_argument_list (parser);
3113 /* Permanently remove the invalid template arguments so that
3114 this error message is not issued again. */
3115 if (start)
3116 cp_lexer_purge_tokens_after (parser->lexer, start);
3120 /* If parsing an integral constant-expression, issue an error message
3121 about the fact that THING appeared and return true. Otherwise,
3122 return false. In either case, set
3123 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3125 static bool
3126 cp_parser_non_integral_constant_expression (cp_parser *parser,
3127 non_integral_constant thing)
3129 parser->non_integral_constant_expression_p = true;
3130 if (parser->integral_constant_expression_p)
3132 if (!parser->allow_non_integral_constant_expression_p)
3134 const char *msg = NULL;
3135 switch (thing)
3137 case NIC_FLOAT:
3138 pedwarn (input_location, OPT_Wpedantic,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3141 return true;
3142 case NIC_CAST:
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3146 return true;
3147 case NIC_TYPEID:
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3150 return true;
3151 case NIC_NCC:
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3154 return true;
3155 case NIC_FUNC_CALL:
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3158 return true;
3159 case NIC_INC:
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3162 return true;
3163 case NIC_DEC:
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_ARRAY_REF:
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_ADDR_LABEL:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_OVERLOADED:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3178 return true;
3179 case NIC_ASSIGNMENT:
3180 error ("an assignment cannot appear in a constant-expression");
3181 return true;
3182 case NIC_COMMA:
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_CONSTRUCTOR:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_TRANSACTION:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3193 return true;
3194 case NIC_THIS:
3195 msg = "this";
3196 break;
3197 case NIC_FUNC_NAME:
3198 msg = "__FUNCTION__";
3199 break;
3200 case NIC_PRETTY_FUNC:
3201 msg = "__PRETTY_FUNCTION__";
3202 break;
3203 case NIC_C99_FUNC:
3204 msg = "__func__";
3205 break;
3206 case NIC_VA_ARG:
3207 msg = "va_arg";
3208 break;
3209 case NIC_ARROW:
3210 msg = "->";
3211 break;
3212 case NIC_POINT:
3213 msg = ".";
3214 break;
3215 case NIC_STAR:
3216 msg = "*";
3217 break;
3218 case NIC_ADDR:
3219 msg = "&";
3220 break;
3221 case NIC_PREINCREMENT:
3222 msg = "++";
3223 break;
3224 case NIC_PREDECREMENT:
3225 msg = "--";
3226 break;
3227 case NIC_NEW:
3228 msg = "new";
3229 break;
3230 case NIC_DEL:
3231 msg = "delete";
3232 break;
3233 default:
3234 gcc_unreachable ();
3236 if (msg)
3237 error ("%qs cannot appear in a constant-expression", msg);
3238 return true;
3241 return false;
3244 /* Emit a diagnostic for an invalid type name. This function commits
3245 to the current active tentative parse, if any. (Otherwise, the
3246 problematic construct might be encountered again later, resulting
3247 in duplicate error messages.) LOCATION is the location of ID. */
3249 static void
3250 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3251 location_t location)
3253 tree decl, ambiguous_decls;
3254 cp_parser_commit_to_tentative_parse (parser);
3255 /* Try to lookup the identifier. */
3256 decl = cp_parser_lookup_name (parser, id, none_type,
3257 /*is_template=*/false,
3258 /*is_namespace=*/false,
3259 /*check_dependency=*/true,
3260 &ambiguous_decls, location);
3261 if (ambiguous_decls)
3262 /* If the lookup was ambiguous, an error will already have
3263 been issued. */
3264 return;
3265 /* If the lookup found a template-name, it means that the user forgot
3266 to specify an argument list. Emit a useful error message. */
3267 if (DECL_TYPE_TEMPLATE_P (decl))
3269 error_at (location,
3270 "invalid use of template-name %qE without an argument list",
3271 decl);
3272 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3273 inform (location, "class template argument deduction is only available "
3274 "with -std=c++17 or -std=gnu++17");
3275 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3277 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3278 error_at (location, "invalid use of destructor %qD as a type", id);
3279 else if (TREE_CODE (decl) == TYPE_DECL)
3280 /* Something like 'unsigned A a;' */
3281 error_at (location, "invalid combination of multiple type-specifiers");
3282 else if (!parser->scope)
3284 /* Issue an error message. */
3285 name_hint hint;
3286 if (TREE_CODE (id) == IDENTIFIER_NODE)
3287 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3288 if (hint)
3290 gcc_rich_location richloc (location);
3291 richloc.add_fixit_replace (hint.suggestion ());
3292 error_at (&richloc,
3293 "%qE does not name a type; did you mean %qs?",
3294 id, hint.suggestion ());
3296 else
3297 error_at (location, "%qE does not name a type", id);
3298 /* If we're in a template class, it's possible that the user was
3299 referring to a type from a base class. For example:
3301 template <typename T> struct A { typedef T X; };
3302 template <typename T> struct B : public A<T> { X x; };
3304 The user should have said "typename A<T>::X". */
3305 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3306 inform (location, "C++11 %<constexpr%> only available with "
3307 "-std=c++11 or -std=gnu++11");
3308 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3309 inform (location, "C++11 %<noexcept%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (cxx_dialect < cxx11
3312 && TREE_CODE (id) == IDENTIFIER_NODE
3313 && id_equal (id, "thread_local"))
3314 inform (location, "C++11 %<thread_local%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3317 inform (location, "%<concept%> only available with -fconcepts");
3318 else if (processing_template_decl && current_class_type
3319 && TYPE_BINFO (current_class_type))
3321 tree b;
3323 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3325 b = TREE_CHAIN (b))
3327 tree base_type = BINFO_TYPE (b);
3328 if (CLASS_TYPE_P (base_type)
3329 && dependent_type_p (base_type))
3331 tree field;
3332 /* Go from a particular instantiation of the
3333 template (which will have an empty TYPE_FIELDs),
3334 to the main version. */
3335 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3336 for (field = TYPE_FIELDS (base_type);
3337 field;
3338 field = DECL_CHAIN (field))
3339 if (TREE_CODE (field) == TYPE_DECL
3340 && DECL_NAME (field) == id)
3342 inform (location,
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b), id);
3345 break;
3347 if (field)
3348 break;
3353 /* Here we diagnose qualified-ids where the scope is actually correct,
3354 but the identifier does not resolve to a valid type name. */
3355 else if (parser->scope != error_mark_node)
3357 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3359 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3360 error_at (location_of (id),
3361 "%qE in namespace %qE does not name a template type",
3362 id, parser->scope);
3363 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3364 error_at (location_of (id),
3365 "%qE in namespace %qE does not name a template type",
3366 TREE_OPERAND (id, 0), parser->scope);
3367 else
3368 error_at (location_of (id),
3369 "%qE in namespace %qE does not name a type",
3370 id, parser->scope);
3371 if (DECL_P (decl))
3372 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3373 else if (decl == error_mark_node)
3374 suggest_alternative_in_explicit_scope (location, id,
3375 parser->scope);
3377 else if (CLASS_TYPE_P (parser->scope)
3378 && constructor_name_p (id, parser->scope))
3380 /* A<T>::A<T>() */
3381 error_at (location, "%<%T::%E%> names the constructor, not"
3382 " the type", parser->scope, id);
3383 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3384 error_at (location, "and %qT has no template constructors",
3385 parser->scope);
3387 else if (TYPE_P (parser->scope)
3388 && dependent_scope_p (parser->scope))
3390 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3391 error_at (location,
3392 "need %<typename%> before %<%T::%D::%E%> because "
3393 "%<%T::%D%> is a dependent scope",
3394 TYPE_CONTEXT (parser->scope),
3395 TYPENAME_TYPE_FULLNAME (parser->scope),
3397 TYPE_CONTEXT (parser->scope),
3398 TYPENAME_TYPE_FULLNAME (parser->scope));
3399 else
3400 error_at (location, "need %<typename%> before %<%T::%E%> because "
3401 "%qT is a dependent scope",
3402 parser->scope, id, parser->scope);
3404 else if (TYPE_P (parser->scope))
3406 if (!COMPLETE_TYPE_P (parser->scope))
3407 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3408 parser->scope);
3409 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3410 error_at (location_of (id),
3411 "%qE in %q#T does not name a template type",
3412 id, parser->scope);
3413 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3414 error_at (location_of (id),
3415 "%qE in %q#T does not name a template type",
3416 TREE_OPERAND (id, 0), parser->scope);
3417 else
3418 error_at (location_of (id),
3419 "%qE in %q#T does not name a type",
3420 id, parser->scope);
3421 if (DECL_P (decl))
3422 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3424 else
3425 gcc_unreachable ();
3429 /* Check for a common situation where a type-name should be present,
3430 but is not, and issue a sensible error message. Returns true if an
3431 invalid type-name was detected.
3433 The situation handled by this function are variable declarations of the
3434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435 Usually, `ID' should name a type, but if we got here it means that it
3436 does not. We try to emit the best possible error message depending on
3437 how exactly the id-expression looks like. */
3439 static bool
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3442 tree id;
3443 cp_token *token = cp_lexer_peek_token (parser->lexer);
3445 /* Avoid duplicate error about ambiguous lookup. */
3446 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3448 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3449 if (next->type == CPP_NAME && next->error_reported)
3450 goto out;
3453 cp_parser_parse_tentatively (parser);
3454 id = cp_parser_id_expression (parser,
3455 /*template_keyword_p=*/false,
3456 /*check_dependency_p=*/true,
3457 /*template_p=*/NULL,
3458 /*declarator_p=*/false,
3459 /*optional_p=*/false);
3460 /* If the next token is a (, this is a function with no explicit return
3461 type, i.e. constructor, destructor or conversion op. */
3462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3463 || TREE_CODE (id) == TYPE_DECL)
3465 cp_parser_abort_tentative_parse (parser);
3466 return false;
3468 if (!cp_parser_parse_definitely (parser))
3469 return false;
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3473 out:
3474 /* If we aren't in the middle of a declarator (i.e. in a
3475 parameter-declaration-clause), skip to the end of the declaration;
3476 there's no point in trying to process it. */
3477 if (!parser->in_declarator_p)
3478 cp_parser_skip_to_end_of_block_or_statement (parser);
3479 return true;
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3484 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485 found an unnested token of that type. */
3487 static int
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3489 bool recovering,
3490 cpp_ttype or_ttype,
3491 bool consume_paren)
3493 unsigned paren_depth = 0;
3494 unsigned brace_depth = 0;
3495 unsigned square_depth = 0;
3496 unsigned condop_depth = 0;
3498 if (recovering && or_ttype == CPP_EOF
3499 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3500 return 0;
3502 while (true)
3504 cp_token * token = cp_lexer_peek_token (parser->lexer);
3506 /* Have we found what we're looking for before the closing paren? */
3507 if (token->type == or_ttype && or_ttype != CPP_EOF
3508 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3509 return -1;
3511 switch (token->type)
3513 case CPP_EOF:
3514 case CPP_PRAGMA_EOL:
3515 /* If we've run out of tokens, then there is no closing `)'. */
3516 return 0;
3518 /* This is good for lambda expression capture-lists. */
3519 case CPP_OPEN_SQUARE:
3520 ++square_depth;
3521 break;
3522 case CPP_CLOSE_SQUARE:
3523 if (!square_depth--)
3524 return 0;
3525 break;
3527 case CPP_SEMICOLON:
3528 /* This matches the processing in skip_to_end_of_statement. */
3529 if (!brace_depth)
3530 return 0;
3531 break;
3533 case CPP_OPEN_BRACE:
3534 ++brace_depth;
3535 break;
3536 case CPP_CLOSE_BRACE:
3537 if (!brace_depth--)
3538 return 0;
3539 break;
3541 case CPP_OPEN_PAREN:
3542 if (!brace_depth)
3543 ++paren_depth;
3544 break;
3546 case CPP_CLOSE_PAREN:
3547 if (!brace_depth && !paren_depth--)
3549 if (consume_paren)
3550 cp_lexer_consume_token (parser->lexer);
3551 return 1;
3553 break;
3555 case CPP_QUERY:
3556 if (!brace_depth && !paren_depth && !square_depth)
3557 ++condop_depth;
3558 break;
3560 case CPP_COLON:
3561 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3562 condop_depth--;
3563 break;
3565 default:
3566 break;
3569 /* Consume the token. */
3570 cp_lexer_consume_token (parser->lexer);
3574 /* Consume tokens up to, and including, the next non-nested closing `)'.
3575 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3576 are doing error recovery. Returns -1 if OR_COMMA is true and we
3577 found an unnested token of that type. */
3579 static int
3580 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3581 bool recovering,
3582 bool or_comma,
3583 bool consume_paren)
3585 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3586 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3587 ttype, consume_paren);
3590 /* Consume tokens until we reach the end of the current statement.
3591 Normally, that will be just before consuming a `;'. However, if a
3592 non-nested `}' comes first, then we stop before consuming that. */
3594 static void
3595 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3597 unsigned nesting_depth = 0;
3599 /* Unwind generic function template scope if necessary. */
3600 if (parser->fully_implicit_function_template_p)
3601 abort_fully_implicit_template (parser);
3603 while (true)
3605 cp_token *token = cp_lexer_peek_token (parser->lexer);
3607 switch (token->type)
3609 case CPP_EOF:
3610 case CPP_PRAGMA_EOL:
3611 /* If we've run out of tokens, stop. */
3612 return;
3614 case CPP_SEMICOLON:
3615 /* If the next token is a `;', we have reached the end of the
3616 statement. */
3617 if (!nesting_depth)
3618 return;
3619 break;
3621 case CPP_CLOSE_BRACE:
3622 /* If this is a non-nested '}', stop before consuming it.
3623 That way, when confronted with something like:
3625 { 3 + }
3627 we stop before consuming the closing '}', even though we
3628 have not yet reached a `;'. */
3629 if (nesting_depth == 0)
3630 return;
3632 /* If it is the closing '}' for a block that we have
3633 scanned, stop -- but only after consuming the token.
3634 That way given:
3636 void f g () { ... }
3637 typedef int I;
3639 we will stop after the body of the erroneously declared
3640 function, but before consuming the following `typedef'
3641 declaration. */
3642 if (--nesting_depth == 0)
3644 cp_lexer_consume_token (parser->lexer);
3645 return;
3647 break;
3649 case CPP_OPEN_BRACE:
3650 ++nesting_depth;
3651 break;
3653 default:
3654 break;
3657 /* Consume the token. */
3658 cp_lexer_consume_token (parser->lexer);
3662 /* This function is called at the end of a statement or declaration.
3663 If the next token is a semicolon, it is consumed; otherwise, error
3664 recovery is attempted. */
3666 static void
3667 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3669 /* Look for the trailing `;'. */
3670 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3672 /* If there is additional (erroneous) input, skip to the end of
3673 the statement. */
3674 cp_parser_skip_to_end_of_statement (parser);
3675 /* If the next token is now a `;', consume it. */
3676 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3677 cp_lexer_consume_token (parser->lexer);
3681 /* Skip tokens until we have consumed an entire block, or until we
3682 have consumed a non-nested `;'. */
3684 static void
3685 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3687 int nesting_depth = 0;
3689 /* Unwind generic function template scope if necessary. */
3690 if (parser->fully_implicit_function_template_p)
3691 abort_fully_implicit_template (parser);
3693 while (nesting_depth >= 0)
3695 cp_token *token = cp_lexer_peek_token (parser->lexer);
3697 switch (token->type)
3699 case CPP_EOF:
3700 case CPP_PRAGMA_EOL:
3701 /* If we've run out of tokens, stop. */
3702 return;
3704 case CPP_SEMICOLON:
3705 /* Stop if this is an unnested ';'. */
3706 if (!nesting_depth)
3707 nesting_depth = -1;
3708 break;
3710 case CPP_CLOSE_BRACE:
3711 /* Stop if this is an unnested '}', or closes the outermost
3712 nesting level. */
3713 nesting_depth--;
3714 if (nesting_depth < 0)
3715 return;
3716 if (!nesting_depth)
3717 nesting_depth = -1;
3718 break;
3720 case CPP_OPEN_BRACE:
3721 /* Nest. */
3722 nesting_depth++;
3723 break;
3725 default:
3726 break;
3729 /* Consume the token. */
3730 cp_lexer_consume_token (parser->lexer);
3734 /* Skip tokens until a non-nested closing curly brace is the next
3735 token, or there are no more tokens. Return true in the first case,
3736 false otherwise. */
3738 static bool
3739 cp_parser_skip_to_closing_brace (cp_parser *parser)
3741 unsigned nesting_depth = 0;
3743 while (true)
3745 cp_token *token = cp_lexer_peek_token (parser->lexer);
3747 switch (token->type)
3749 case CPP_EOF:
3750 case CPP_PRAGMA_EOL:
3751 /* If we've run out of tokens, stop. */
3752 return false;
3754 case CPP_CLOSE_BRACE:
3755 /* If the next token is a non-nested `}', then we have reached
3756 the end of the current block. */
3757 if (nesting_depth-- == 0)
3758 return true;
3759 break;
3761 case CPP_OPEN_BRACE:
3762 /* If it the next token is a `{', then we are entering a new
3763 block. Consume the entire block. */
3764 ++nesting_depth;
3765 break;
3767 default:
3768 break;
3771 /* Consume the token. */
3772 cp_lexer_consume_token (parser->lexer);
3776 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3777 parameter is the PRAGMA token, allowing us to purge the entire pragma
3778 sequence. */
3780 static void
3781 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3783 cp_token *token;
3785 parser->lexer->in_pragma = false;
3788 token = cp_lexer_consume_token (parser->lexer);
3789 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3791 /* Ensure that the pragma is not parsed again. */
3792 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3795 /* Require pragma end of line, resyncing with it as necessary. The
3796 arguments are as for cp_parser_skip_to_pragma_eol. */
3798 static void
3799 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3801 parser->lexer->in_pragma = false;
3802 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3803 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3806 /* This is a simple wrapper around make_typename_type. When the id is
3807 an unresolved identifier node, we can provide a superior diagnostic
3808 using cp_parser_diagnose_invalid_type_name. */
3810 static tree
3811 cp_parser_make_typename_type (cp_parser *parser, tree id,
3812 location_t id_location)
3814 tree result;
3815 if (identifier_p (id))
3817 result = make_typename_type (parser->scope, id, typename_type,
3818 /*complain=*/tf_none);
3819 if (result == error_mark_node)
3820 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3821 return result;
3823 return make_typename_type (parser->scope, id, typename_type, tf_error);
3826 /* This is a wrapper around the
3827 make_{pointer,ptrmem,reference}_declarator functions that decides
3828 which one to call based on the CODE and CLASS_TYPE arguments. The
3829 CODE argument should be one of the values returned by
3830 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3831 appertain to the pointer or reference. */
3833 static cp_declarator *
3834 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3835 cp_cv_quals cv_qualifiers,
3836 cp_declarator *target,
3837 tree attributes)
3839 if (code == ERROR_MARK || target == cp_error_declarator)
3840 return cp_error_declarator;
3842 if (code == INDIRECT_REF)
3843 if (class_type == NULL_TREE)
3844 return make_pointer_declarator (cv_qualifiers, target, attributes);
3845 else
3846 return make_ptrmem_declarator (cv_qualifiers, class_type,
3847 target, attributes);
3848 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3849 return make_reference_declarator (cv_qualifiers, target,
3850 false, attributes);
3851 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3852 return make_reference_declarator (cv_qualifiers, target,
3853 true, attributes);
3854 gcc_unreachable ();
3857 /* Create a new C++ parser. */
3859 static cp_parser *
3860 cp_parser_new (void)
3862 cp_parser *parser;
3863 cp_lexer *lexer;
3864 unsigned i;
3866 /* cp_lexer_new_main is called before doing GC allocation because
3867 cp_lexer_new_main might load a PCH file. */
3868 lexer = cp_lexer_new_main ();
3870 /* Initialize the binops_by_token so that we can get the tree
3871 directly from the token. */
3872 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3873 binops_by_token[binops[i].token_type] = binops[i];
3875 parser = ggc_cleared_alloc<cp_parser> ();
3876 parser->lexer = lexer;
3877 parser->context = cp_parser_context_new (NULL);
3879 /* For now, we always accept GNU extensions. */
3880 parser->allow_gnu_extensions_p = 1;
3882 /* The `>' token is a greater-than operator, not the end of a
3883 template-id. */
3884 parser->greater_than_is_operator_p = true;
3886 parser->default_arg_ok_p = true;
3888 /* We are not parsing a constant-expression. */
3889 parser->integral_constant_expression_p = false;
3890 parser->allow_non_integral_constant_expression_p = false;
3891 parser->non_integral_constant_expression_p = false;
3893 /* Local variable names are not forbidden. */
3894 parser->local_variables_forbidden_p = false;
3896 /* We are not processing an `extern "C"' declaration. */
3897 parser->in_unbraced_linkage_specification_p = false;
3899 /* We are not processing a declarator. */
3900 parser->in_declarator_p = false;
3902 /* We are not processing a template-argument-list. */
3903 parser->in_template_argument_list_p = false;
3905 /* We are not in an iteration statement. */
3906 parser->in_statement = 0;
3908 /* We are not in a switch statement. */
3909 parser->in_switch_statement_p = false;
3911 /* We are not parsing a type-id inside an expression. */
3912 parser->in_type_id_in_expr_p = false;
3914 /* Declarations aren't implicitly extern "C". */
3915 parser->implicit_extern_c = false;
3917 /* String literals should be translated to the execution character set. */
3918 parser->translate_strings_p = true;
3920 /* We are not parsing a function body. */
3921 parser->in_function_body = false;
3923 /* We can correct until told otherwise. */
3924 parser->colon_corrects_to_scope_p = true;
3926 /* The unparsed function queue is empty. */
3927 push_unparsed_function_queues (parser);
3929 /* There are no classes being defined. */
3930 parser->num_classes_being_defined = 0;
3932 /* No template parameters apply. */
3933 parser->num_template_parameter_lists = 0;
3935 /* Special parsing data structures. */
3936 parser->omp_declare_simd = NULL;
3937 parser->oacc_routine = NULL;
3939 /* Not declaring an implicit function template. */
3940 parser->auto_is_implicit_function_template_parm_p = false;
3941 parser->fully_implicit_function_template_p = false;
3942 parser->implicit_template_parms = 0;
3943 parser->implicit_template_scope = 0;
3945 /* Allow constrained-type-specifiers. */
3946 parser->prevent_constrained_type_specifiers = 0;
3948 /* We haven't yet seen an 'extern "C"'. */
3949 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3951 return parser;
3954 /* Create a cp_lexer structure which will emit the tokens in CACHE
3955 and push it onto the parser's lexer stack. This is used for delayed
3956 parsing of in-class method bodies and default arguments, and should
3957 not be confused with tentative parsing. */
3958 static void
3959 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3961 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3962 lexer->next = parser->lexer;
3963 parser->lexer = lexer;
3965 /* Move the current source position to that of the first token in the
3966 new lexer. */
3967 cp_lexer_set_source_position_from_token (lexer->next_token);
3970 /* Pop the top lexer off the parser stack. This is never used for the
3971 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3972 static void
3973 cp_parser_pop_lexer (cp_parser *parser)
3975 cp_lexer *lexer = parser->lexer;
3976 parser->lexer = lexer->next;
3977 cp_lexer_destroy (lexer);
3979 /* Put the current source position back where it was before this
3980 lexer was pushed. */
3981 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3984 /* Lexical conventions [gram.lex] */
3986 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3987 identifier. */
3989 static cp_expr
3990 cp_parser_identifier (cp_parser* parser)
3992 cp_token *token;
3994 /* Look for the identifier. */
3995 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3996 /* Return the value. */
3997 if (token)
3998 return cp_expr (token->u.value, token->location);
3999 else
4000 return error_mark_node;
4003 /* Parse a sequence of adjacent string constants. Returns a
4004 TREE_STRING representing the combined, nul-terminated string
4005 constant. If TRANSLATE is true, translate the string to the
4006 execution character set. If WIDE_OK is true, a wide string is
4007 invalid here.
4009 C++98 [lex.string] says that if a narrow string literal token is
4010 adjacent to a wide string literal token, the behavior is undefined.
4011 However, C99 6.4.5p4 says that this results in a wide string literal.
4012 We follow C99 here, for consistency with the C front end.
4014 This code is largely lifted from lex_string() in c-lex.c.
4016 FUTURE: ObjC++ will need to handle @-strings here. */
4017 static cp_expr
4018 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4019 bool lookup_udlit = true)
4021 tree value;
4022 size_t count;
4023 struct obstack str_ob;
4024 cpp_string str, istr, *strs;
4025 cp_token *tok;
4026 enum cpp_ttype type, curr_type;
4027 int have_suffix_p = 0;
4028 tree string_tree;
4029 tree suffix_id = NULL_TREE;
4030 bool curr_tok_is_userdef_p = false;
4032 tok = cp_lexer_peek_token (parser->lexer);
4033 if (!cp_parser_is_string_literal (tok))
4035 cp_parser_error (parser, "expected string-literal");
4036 return error_mark_node;
4039 location_t loc = tok->location;
4041 if (cpp_userdef_string_p (tok->type))
4043 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4044 curr_type = cpp_userdef_string_remove_type (tok->type);
4045 curr_tok_is_userdef_p = true;
4047 else
4049 string_tree = tok->u.value;
4050 curr_type = tok->type;
4052 type = curr_type;
4054 /* Try to avoid the overhead of creating and destroying an obstack
4055 for the common case of just one string. */
4056 if (!cp_parser_is_string_literal
4057 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4059 cp_lexer_consume_token (parser->lexer);
4061 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4062 str.len = TREE_STRING_LENGTH (string_tree);
4063 count = 1;
4065 if (curr_tok_is_userdef_p)
4067 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4068 have_suffix_p = 1;
4069 curr_type = cpp_userdef_string_remove_type (tok->type);
4071 else
4072 curr_type = tok->type;
4074 strs = &str;
4076 else
4078 location_t last_tok_loc = tok->location;
4079 gcc_obstack_init (&str_ob);
4080 count = 0;
4084 cp_lexer_consume_token (parser->lexer);
4085 count++;
4086 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4087 str.len = TREE_STRING_LENGTH (string_tree);
4089 if (curr_tok_is_userdef_p)
4091 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4092 if (have_suffix_p == 0)
4094 suffix_id = curr_suffix_id;
4095 have_suffix_p = 1;
4097 else if (have_suffix_p == 1
4098 && curr_suffix_id != suffix_id)
4100 error ("inconsistent user-defined literal suffixes"
4101 " %qD and %qD in string literal",
4102 suffix_id, curr_suffix_id);
4103 have_suffix_p = -1;
4105 curr_type = cpp_userdef_string_remove_type (tok->type);
4107 else
4108 curr_type = tok->type;
4110 if (type != curr_type)
4112 if (type == CPP_STRING)
4113 type = curr_type;
4114 else if (curr_type != CPP_STRING)
4116 rich_location rich_loc (line_table, tok->location);
4117 rich_loc.add_range (last_tok_loc, false);
4118 error_at (&rich_loc,
4119 "unsupported non-standard concatenation "
4120 "of string literals");
4124 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4126 last_tok_loc = tok->location;
4128 tok = cp_lexer_peek_token (parser->lexer);
4129 if (cpp_userdef_string_p (tok->type))
4131 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4132 curr_type = cpp_userdef_string_remove_type (tok->type);
4133 curr_tok_is_userdef_p = true;
4135 else
4137 string_tree = tok->u.value;
4138 curr_type = tok->type;
4139 curr_tok_is_userdef_p = false;
4142 while (cp_parser_is_string_literal (tok));
4144 /* A string literal built by concatenation has its caret=start at
4145 the start of the initial string, and its finish at the finish of
4146 the final string literal. */
4147 loc = make_location (loc, loc, get_finish (last_tok_loc));
4149 strs = (cpp_string *) obstack_finish (&str_ob);
4152 if (type != CPP_STRING && !wide_ok)
4154 cp_parser_error (parser, "a wide string is invalid in this context");
4155 type = CPP_STRING;
4158 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4159 (parse_in, strs, count, &istr, type))
4161 value = build_string (istr.len, (const char *)istr.text);
4162 free (CONST_CAST (unsigned char *, istr.text));
4164 switch (type)
4166 default:
4167 case CPP_STRING:
4168 case CPP_UTF8STRING:
4169 TREE_TYPE (value) = char_array_type_node;
4170 break;
4171 case CPP_STRING16:
4172 TREE_TYPE (value) = char16_array_type_node;
4173 break;
4174 case CPP_STRING32:
4175 TREE_TYPE (value) = char32_array_type_node;
4176 break;
4177 case CPP_WSTRING:
4178 TREE_TYPE (value) = wchar_array_type_node;
4179 break;
4182 value = fix_string_type (value);
4184 if (have_suffix_p)
4186 tree literal = build_userdef_literal (suffix_id, value,
4187 OT_NONE, NULL_TREE);
4188 if (lookup_udlit)
4189 value = cp_parser_userdef_string_literal (literal);
4190 else
4191 value = literal;
4194 else
4195 /* cpp_interpret_string has issued an error. */
4196 value = error_mark_node;
4198 if (count > 1)
4199 obstack_free (&str_ob, 0);
4201 return cp_expr (value, loc);
4204 /* Look up a literal operator with the name and the exact arguments. */
4206 static tree
4207 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4209 tree decl;
4210 decl = lookup_name (name);
4211 if (!decl || !is_overloaded_fn (decl))
4212 return error_mark_node;
4214 for (lkp_iterator iter (decl); iter; ++iter)
4216 unsigned int ix;
4217 bool found = true;
4218 tree fn = *iter;
4219 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4220 if (parmtypes != NULL_TREE)
4222 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4223 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4225 tree tparm = TREE_VALUE (parmtypes);
4226 tree targ = TREE_TYPE ((*args)[ix]);
4227 bool ptr = TYPE_PTR_P (tparm);
4228 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4229 if ((ptr || arr || !same_type_p (tparm, targ))
4230 && (!ptr || !arr
4231 || !same_type_p (TREE_TYPE (tparm),
4232 TREE_TYPE (targ))))
4233 found = false;
4235 if (found
4236 && ix == vec_safe_length (args)
4237 /* May be this should be sufficient_parms_p instead,
4238 depending on how exactly should user-defined literals
4239 work in presence of default arguments on the literal
4240 operator parameters. */
4241 && parmtypes == void_list_node)
4242 return decl;
4246 return error_mark_node;
4249 /* Parse a user-defined char constant. Returns a call to a user-defined
4250 literal operator taking the character as an argument. */
4252 static cp_expr
4253 cp_parser_userdef_char_literal (cp_parser *parser)
4255 cp_token *token = cp_lexer_consume_token (parser->lexer);
4256 tree literal = token->u.value;
4257 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4258 tree value = USERDEF_LITERAL_VALUE (literal);
4259 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4260 tree decl, result;
4262 /* Build up a call to the user-defined operator */
4263 /* Lookup the name we got back from the id-expression. */
4264 vec<tree, va_gc> *args = make_tree_vector ();
4265 vec_safe_push (args, value);
4266 decl = lookup_literal_operator (name, args);
4267 if (!decl || decl == error_mark_node)
4269 error ("unable to find character literal operator %qD with %qT argument",
4270 name, TREE_TYPE (value));
4271 release_tree_vector (args);
4272 return error_mark_node;
4274 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4275 release_tree_vector (args);
4276 return result;
4279 /* A subroutine of cp_parser_userdef_numeric_literal to
4280 create a char... template parameter pack from a string node. */
4282 static tree
4283 make_char_string_pack (tree value)
4285 tree charvec;
4286 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4287 const char *str = TREE_STRING_POINTER (value);
4288 int i, len = TREE_STRING_LENGTH (value) - 1;
4289 tree argvec = make_tree_vec (1);
4291 /* Fill in CHARVEC with all of the parameters. */
4292 charvec = make_tree_vec (len);
4293 for (i = 0; i < len; ++i)
4294 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4296 /* Build the argument packs. */
4297 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4299 TREE_VEC_ELT (argvec, 0) = argpack;
4301 return argvec;
4304 /* A subroutine of cp_parser_userdef_numeric_literal to
4305 create a char... template parameter pack from a string node. */
4307 static tree
4308 make_string_pack (tree value)
4310 tree charvec;
4311 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4312 const unsigned char *str
4313 = (const unsigned char *) TREE_STRING_POINTER (value);
4314 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4315 int len = TREE_STRING_LENGTH (value) / sz - 1;
4316 tree argvec = make_tree_vec (2);
4318 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4319 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4321 /* First template parm is character type. */
4322 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4324 /* Fill in CHARVEC with all of the parameters. */
4325 charvec = make_tree_vec (len);
4326 for (int i = 0; i < len; ++i)
4327 TREE_VEC_ELT (charvec, i)
4328 = double_int_to_tree (str_char_type_node,
4329 double_int::from_buffer (str + i * sz, sz));
4331 /* Build the argument packs. */
4332 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4334 TREE_VEC_ELT (argvec, 1) = argpack;
4336 return argvec;
4339 /* Parse a user-defined numeric constant. returns a call to a user-defined
4340 literal operator. */
4342 static cp_expr
4343 cp_parser_userdef_numeric_literal (cp_parser *parser)
4345 cp_token *token = cp_lexer_consume_token (parser->lexer);
4346 tree literal = token->u.value;
4347 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4348 tree value = USERDEF_LITERAL_VALUE (literal);
4349 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4350 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4351 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4352 tree decl, result;
4353 vec<tree, va_gc> *args;
4355 /* Look for a literal operator taking the exact type of numeric argument
4356 as the literal value. */
4357 args = make_tree_vector ();
4358 vec_safe_push (args, value);
4359 decl = lookup_literal_operator (name, args);
4360 if (decl && decl != error_mark_node)
4362 result = finish_call_expr (decl, &args, false, true,
4363 tf_warning_or_error);
4365 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4367 warning_at (token->location, OPT_Woverflow,
4368 "integer literal exceeds range of %qT type",
4369 long_long_unsigned_type_node);
4371 else
4373 if (overflow > 0)
4374 warning_at (token->location, OPT_Woverflow,
4375 "floating literal exceeds range of %qT type",
4376 long_double_type_node);
4377 else if (overflow < 0)
4378 warning_at (token->location, OPT_Woverflow,
4379 "floating literal truncated to zero");
4382 release_tree_vector (args);
4383 return result;
4385 release_tree_vector (args);
4387 /* If the numeric argument didn't work, look for a raw literal
4388 operator taking a const char* argument consisting of the number
4389 in string format. */
4390 args = make_tree_vector ();
4391 vec_safe_push (args, num_string);
4392 decl = lookup_literal_operator (name, args);
4393 if (decl && decl != error_mark_node)
4395 result = finish_call_expr (decl, &args, false, true,
4396 tf_warning_or_error);
4397 release_tree_vector (args);
4398 return result;
4400 release_tree_vector (args);
4402 /* If the raw literal didn't work, look for a non-type template
4403 function with parameter pack char.... Call the function with
4404 template parameter characters representing the number. */
4405 args = make_tree_vector ();
4406 decl = lookup_literal_operator (name, args);
4407 if (decl && decl != error_mark_node)
4409 tree tmpl_args = make_char_string_pack (num_string);
4410 decl = lookup_template_function (decl, tmpl_args);
4411 result = finish_call_expr (decl, &args, false, true,
4412 tf_warning_or_error);
4413 release_tree_vector (args);
4414 return result;
4417 release_tree_vector (args);
4419 /* In C++14 the standard library defines complex number suffixes that
4420 conflict with GNU extensions. Prefer them if <complex> is #included. */
4421 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4422 bool i14 = (cxx_dialect > cxx11
4423 && (id_equal (suffix_id, "i")
4424 || id_equal (suffix_id, "if")
4425 || id_equal (suffix_id, "il")));
4426 diagnostic_t kind = DK_ERROR;
4427 int opt = 0;
4429 if (i14 && ext)
4431 tree cxlit = lookup_qualified_name (std_node,
4432 get_identifier ("complex_literals"),
4433 0, false, false);
4434 if (cxlit == error_mark_node)
4436 /* No <complex>, so pedwarn and use GNU semantics. */
4437 kind = DK_PEDWARN;
4438 opt = OPT_Wpedantic;
4442 bool complained
4443 = emit_diagnostic (kind, input_location, opt,
4444 "unable to find numeric literal operator %qD", name);
4446 if (!complained)
4447 /* Don't inform either. */;
4448 else if (i14)
4450 inform (token->location, "add %<using namespace std::complex_literals%> "
4451 "(from <complex>) to enable the C++14 user-defined literal "
4452 "suffixes");
4453 if (ext)
4454 inform (token->location, "or use %<j%> instead of %<i%> for the "
4455 "GNU built-in suffix");
4457 else if (!ext)
4458 inform (token->location, "use -fext-numeric-literals "
4459 "to enable more built-in suffixes");
4461 if (kind == DK_ERROR)
4462 value = error_mark_node;
4463 else
4465 /* Use the built-in semantics. */
4466 tree type;
4467 if (id_equal (suffix_id, "i"))
4469 if (TREE_CODE (value) == INTEGER_CST)
4470 type = integer_type_node;
4471 else
4472 type = double_type_node;
4474 else if (id_equal (suffix_id, "if"))
4475 type = float_type_node;
4476 else /* if (id_equal (suffix_id, "il")) */
4477 type = long_double_type_node;
4479 value = build_complex (build_complex_type (type),
4480 fold_convert (type, integer_zero_node),
4481 fold_convert (type, value));
4484 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4485 /* Avoid repeated diagnostics. */
4486 token->u.value = value;
4487 return value;
4490 /* Parse a user-defined string constant. Returns a call to a user-defined
4491 literal operator taking a character pointer and the length of the string
4492 as arguments. */
4494 static tree
4495 cp_parser_userdef_string_literal (tree literal)
4497 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4498 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4499 tree value = USERDEF_LITERAL_VALUE (literal);
4500 int len = TREE_STRING_LENGTH (value)
4501 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4502 tree decl, result;
4503 vec<tree, va_gc> *args;
4505 /* Build up a call to the user-defined operator. */
4506 /* Lookup the name we got back from the id-expression. */
4507 args = make_tree_vector ();
4508 vec_safe_push (args, value);
4509 vec_safe_push (args, build_int_cst (size_type_node, len));
4510 decl = lookup_literal_operator (name, args);
4512 if (decl && decl != error_mark_node)
4514 result = finish_call_expr (decl, &args, false, true,
4515 tf_warning_or_error);
4516 release_tree_vector (args);
4517 return result;
4519 release_tree_vector (args);
4521 /* Look for a template function with typename parameter CharT
4522 and parameter pack CharT... Call the function with
4523 template parameter characters representing the string. */
4524 args = make_tree_vector ();
4525 decl = lookup_literal_operator (name, args);
4526 if (decl && decl != error_mark_node)
4528 tree tmpl_args = make_string_pack (value);
4529 decl = lookup_template_function (decl, tmpl_args);
4530 result = finish_call_expr (decl, &args, false, true,
4531 tf_warning_or_error);
4532 release_tree_vector (args);
4533 return result;
4535 release_tree_vector (args);
4537 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4538 name, TREE_TYPE (value), size_type_node);
4539 return error_mark_node;
4543 /* Basic concepts [gram.basic] */
4545 /* Parse a translation-unit.
4547 translation-unit:
4548 declaration-seq [opt]
4550 Returns TRUE if all went well. */
4552 static bool
4553 cp_parser_translation_unit (cp_parser* parser)
4555 /* The address of the first non-permanent object on the declarator
4556 obstack. */
4557 static void *declarator_obstack_base;
4559 bool success;
4561 /* Create the declarator obstack, if necessary. */
4562 if (!cp_error_declarator)
4564 gcc_obstack_init (&declarator_obstack);
4565 /* Create the error declarator. */
4566 cp_error_declarator = make_declarator (cdk_error);
4567 /* Create the empty parameter list. */
4568 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4569 UNKNOWN_LOCATION);
4570 /* Remember where the base of the declarator obstack lies. */
4571 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4574 cp_parser_declaration_seq_opt (parser);
4576 /* If there are no tokens left then all went well. */
4577 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4579 /* Get rid of the token array; we don't need it any more. */
4580 cp_lexer_destroy (parser->lexer);
4581 parser->lexer = NULL;
4583 /* This file might have been a context that's implicitly extern
4584 "C". If so, pop the lang context. (Only relevant for PCH.) */
4585 if (parser->implicit_extern_c)
4587 pop_lang_context ();
4588 parser->implicit_extern_c = false;
4591 /* Finish up. */
4592 finish_translation_unit ();
4594 success = true;
4596 else
4598 cp_parser_error (parser, "expected declaration");
4599 success = false;
4602 /* Make sure the declarator obstack was fully cleaned up. */
4603 gcc_assert (obstack_next_free (&declarator_obstack)
4604 == declarator_obstack_base);
4606 /* All went well. */
4607 return success;
4610 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4611 decltype context. */
4613 static inline tsubst_flags_t
4614 complain_flags (bool decltype_p)
4616 tsubst_flags_t complain = tf_warning_or_error;
4617 if (decltype_p)
4618 complain |= tf_decltype;
4619 return complain;
4622 /* We're about to parse a collection of statements. If we're currently
4623 parsing tentatively, set up a firewall so that any nested
4624 cp_parser_commit_to_tentative_parse won't affect the current context. */
4626 static cp_token_position
4627 cp_parser_start_tentative_firewall (cp_parser *parser)
4629 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4630 return 0;
4632 cp_parser_parse_tentatively (parser);
4633 cp_parser_commit_to_topmost_tentative_parse (parser);
4634 return cp_lexer_token_position (parser->lexer, false);
4637 /* We've finished parsing the collection of statements. Wrap up the
4638 firewall and replace the relevant tokens with the parsed form. */
4640 static void
4641 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4642 tree expr)
4644 if (!start)
4645 return;
4647 /* Finish the firewall level. */
4648 cp_parser_parse_definitely (parser);
4649 /* And remember the result of the parse for when we try again. */
4650 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4651 token->type = CPP_PREPARSED_EXPR;
4652 token->u.value = expr;
4653 token->keyword = RID_MAX;
4654 cp_lexer_purge_tokens_after (parser->lexer, start);
4657 /* Like the above functions, but let the user modify the tokens. Used by
4658 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4659 later parses, so it makes sense to localize the effects of
4660 cp_parser_commit_to_tentative_parse. */
4662 struct tentative_firewall
4664 cp_parser *parser;
4665 bool set;
4667 tentative_firewall (cp_parser *p): parser(p)
4669 /* If we're currently parsing tentatively, start a committed level as a
4670 firewall and then an inner tentative parse. */
4671 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4673 cp_parser_parse_tentatively (parser);
4674 cp_parser_commit_to_topmost_tentative_parse (parser);
4675 cp_parser_parse_tentatively (parser);
4679 ~tentative_firewall()
4681 if (set)
4683 /* Finish the inner tentative parse and the firewall, propagating any
4684 uncommitted error state to the outer tentative parse. */
4685 bool err = cp_parser_error_occurred (parser);
4686 cp_parser_parse_definitely (parser);
4687 cp_parser_parse_definitely (parser);
4688 if (err)
4689 cp_parser_simulate_error (parser);
4694 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4695 This class is for tracking such a matching pair of symbols.
4696 In particular, it tracks the location of the first token,
4697 so that if the second token is missing, we can highlight the
4698 location of the first token when notifying the user about the
4699 problem. */
4701 template <typename traits_t>
4702 class token_pair
4704 public:
4705 /* token_pair's ctor. */
4706 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4708 /* If the next token is the opening symbol for this pair, consume it and
4709 return true.
4710 Otherwise, issue an error and return false.
4711 In either case, record the location of the opening token. */
4713 bool require_open (cp_parser *parser)
4715 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4716 return cp_parser_require (parser, traits_t::open_token_type,
4717 traits_t::required_token_open);
4720 /* Consume the next token from PARSER, recording its location as
4721 that of the opening token within the pair. */
4723 cp_token * consume_open (cp_parser *parser)
4725 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4726 gcc_assert (tok->type == traits_t::open_token_type);
4727 m_open_loc = tok->location;
4728 return tok;
4731 /* If the next token is the closing symbol for this pair, consume it
4732 and return it.
4733 Otherwise, issue an error, highlighting the location of the
4734 corresponding opening token, and return NULL. */
4736 cp_token *require_close (cp_parser *parser) const
4738 return cp_parser_require (parser, traits_t::close_token_type,
4739 traits_t::required_token_close,
4740 m_open_loc);
4743 private:
4744 location_t m_open_loc;
4747 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4749 struct matching_paren_traits
4751 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4752 static const enum required_token required_token_open = RT_OPEN_PAREN;
4753 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4754 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4757 /* "matching_parens" is a token_pair<T> class for tracking matching
4758 pairs of parentheses. */
4760 typedef token_pair<matching_paren_traits> matching_parens;
4762 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4764 struct matching_brace_traits
4766 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4767 static const enum required_token required_token_open = RT_OPEN_BRACE;
4768 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4769 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4772 /* "matching_braces" is a token_pair<T> class for tracking matching
4773 pairs of braces. */
4775 typedef token_pair<matching_brace_traits> matching_braces;
4778 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4779 enclosing parentheses. */
4781 static cp_expr
4782 cp_parser_statement_expr (cp_parser *parser)
4784 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4786 /* Consume the '('. */
4787 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4788 matching_parens parens;
4789 parens.consume_open (parser);
4790 /* Start the statement-expression. */
4791 tree expr = begin_stmt_expr ();
4792 /* Parse the compound-statement. */
4793 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4794 /* Finish up. */
4795 expr = finish_stmt_expr (expr, false);
4796 /* Consume the ')'. */
4797 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4798 if (!parens.require_close (parser))
4799 cp_parser_skip_to_end_of_statement (parser);
4801 cp_parser_end_tentative_firewall (parser, start, expr);
4802 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4803 return cp_expr (expr, combined_loc);
4806 /* Expressions [gram.expr] */
4808 /* Parse a fold-operator.
4810 fold-operator:
4811 - * / % ^ & | = < > << >>
4812 = -= *= /= %= ^= &= |= <<= >>=
4813 == != <= >= && || , .* ->*
4815 This returns the tree code corresponding to the matched operator
4816 as an int. When the current token matches a compound assignment
4817 opertor, the resulting tree code is the negative value of the
4818 non-assignment operator. */
4820 static int
4821 cp_parser_fold_operator (cp_token *token)
4823 switch (token->type)
4825 case CPP_PLUS: return PLUS_EXPR;
4826 case CPP_MINUS: return MINUS_EXPR;
4827 case CPP_MULT: return MULT_EXPR;
4828 case CPP_DIV: return TRUNC_DIV_EXPR;
4829 case CPP_MOD: return TRUNC_MOD_EXPR;
4830 case CPP_XOR: return BIT_XOR_EXPR;
4831 case CPP_AND: return BIT_AND_EXPR;
4832 case CPP_OR: return BIT_IOR_EXPR;
4833 case CPP_LSHIFT: return LSHIFT_EXPR;
4834 case CPP_RSHIFT: return RSHIFT_EXPR;
4836 case CPP_EQ: return -NOP_EXPR;
4837 case CPP_PLUS_EQ: return -PLUS_EXPR;
4838 case CPP_MINUS_EQ: return -MINUS_EXPR;
4839 case CPP_MULT_EQ: return -MULT_EXPR;
4840 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4841 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4842 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4843 case CPP_AND_EQ: return -BIT_AND_EXPR;
4844 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4845 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4846 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4848 case CPP_EQ_EQ: return EQ_EXPR;
4849 case CPP_NOT_EQ: return NE_EXPR;
4850 case CPP_LESS: return LT_EXPR;
4851 case CPP_GREATER: return GT_EXPR;
4852 case CPP_LESS_EQ: return LE_EXPR;
4853 case CPP_GREATER_EQ: return GE_EXPR;
4855 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4856 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4858 case CPP_COMMA: return COMPOUND_EXPR;
4860 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4861 case CPP_DEREF_STAR: return MEMBER_REF;
4863 default: return ERROR_MARK;
4867 /* Returns true if CODE indicates a binary expression, which is not allowed in
4868 the LHS of a fold-expression. More codes will need to be added to use this
4869 function in other contexts. */
4871 static bool
4872 is_binary_op (tree_code code)
4874 switch (code)
4876 case PLUS_EXPR:
4877 case POINTER_PLUS_EXPR:
4878 case MINUS_EXPR:
4879 case MULT_EXPR:
4880 case TRUNC_DIV_EXPR:
4881 case TRUNC_MOD_EXPR:
4882 case BIT_XOR_EXPR:
4883 case BIT_AND_EXPR:
4884 case BIT_IOR_EXPR:
4885 case LSHIFT_EXPR:
4886 case RSHIFT_EXPR:
4888 case MODOP_EXPR:
4890 case EQ_EXPR:
4891 case NE_EXPR:
4892 case LE_EXPR:
4893 case GE_EXPR:
4894 case LT_EXPR:
4895 case GT_EXPR:
4897 case TRUTH_ANDIF_EXPR:
4898 case TRUTH_ORIF_EXPR:
4900 case COMPOUND_EXPR:
4902 case DOTSTAR_EXPR:
4903 case MEMBER_REF:
4904 return true;
4906 default:
4907 return false;
4911 /* If the next token is a suitable fold operator, consume it and return as
4912 the function above. */
4914 static int
4915 cp_parser_fold_operator (cp_parser *parser)
4917 cp_token* token = cp_lexer_peek_token (parser->lexer);
4918 int code = cp_parser_fold_operator (token);
4919 if (code != ERROR_MARK)
4920 cp_lexer_consume_token (parser->lexer);
4921 return code;
4924 /* Parse a fold-expression.
4926 fold-expression:
4927 ( ... folding-operator cast-expression)
4928 ( cast-expression folding-operator ... )
4929 ( cast-expression folding operator ... folding-operator cast-expression)
4931 Note that the '(' and ')' are matched in primary expression. */
4933 static cp_expr
4934 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4936 cp_id_kind pidk;
4938 // Left fold.
4939 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4941 cp_lexer_consume_token (parser->lexer);
4942 int op = cp_parser_fold_operator (parser);
4943 if (op == ERROR_MARK)
4945 cp_parser_error (parser, "expected binary operator");
4946 return error_mark_node;
4949 tree expr = cp_parser_cast_expression (parser, false, false,
4950 false, &pidk);
4951 if (expr == error_mark_node)
4952 return error_mark_node;
4953 return finish_left_unary_fold_expr (expr, op);
4956 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4957 int op = cp_parser_fold_operator (parser);
4958 if (op == ERROR_MARK)
4960 cp_parser_error (parser, "expected binary operator");
4961 return error_mark_node;
4964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4966 cp_parser_error (parser, "expected ...");
4967 return error_mark_node;
4969 cp_lexer_consume_token (parser->lexer);
4971 /* The operands of a fold-expression are cast-expressions, so binary or
4972 conditional expressions are not allowed. We check this here to avoid
4973 tentative parsing. */
4974 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4975 /* OK, the expression was parenthesized. */;
4976 else if (is_binary_op (TREE_CODE (expr1)))
4977 error_at (location_of (expr1),
4978 "binary expression in operand of fold-expression");
4979 else if (TREE_CODE (expr1) == COND_EXPR
4980 || (REFERENCE_REF_P (expr1)
4981 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
4982 error_at (location_of (expr1),
4983 "conditional expression in operand of fold-expression");
4985 // Right fold.
4986 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4987 return finish_right_unary_fold_expr (expr1, op);
4989 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4991 cp_parser_error (parser, "mismatched operator in fold-expression");
4992 return error_mark_node;
4994 cp_lexer_consume_token (parser->lexer);
4996 // Binary left or right fold.
4997 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4998 if (expr2 == error_mark_node)
4999 return error_mark_node;
5000 return finish_binary_fold_expr (expr1, expr2, op);
5003 /* Parse a primary-expression.
5005 primary-expression:
5006 literal
5007 this
5008 ( expression )
5009 id-expression
5010 lambda-expression (C++11)
5012 GNU Extensions:
5014 primary-expression:
5015 ( compound-statement )
5016 __builtin_va_arg ( assignment-expression , type-id )
5017 __builtin_offsetof ( type-id , offsetof-expression )
5019 C++ Extensions:
5020 __has_nothrow_assign ( type-id )
5021 __has_nothrow_constructor ( type-id )
5022 __has_nothrow_copy ( type-id )
5023 __has_trivial_assign ( type-id )
5024 __has_trivial_constructor ( type-id )
5025 __has_trivial_copy ( type-id )
5026 __has_trivial_destructor ( type-id )
5027 __has_virtual_destructor ( type-id )
5028 __is_abstract ( type-id )
5029 __is_base_of ( type-id , type-id )
5030 __is_class ( type-id )
5031 __is_empty ( type-id )
5032 __is_enum ( type-id )
5033 __is_final ( type-id )
5034 __is_literal_type ( type-id )
5035 __is_pod ( type-id )
5036 __is_polymorphic ( type-id )
5037 __is_std_layout ( type-id )
5038 __is_trivial ( type-id )
5039 __is_union ( type-id )
5041 Objective-C++ Extension:
5043 primary-expression:
5044 objc-expression
5046 literal:
5047 __null
5049 ADDRESS_P is true iff this expression was immediately preceded by
5050 "&" and therefore might denote a pointer-to-member. CAST_P is true
5051 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5052 true iff this expression is a template argument.
5054 Returns a representation of the expression. Upon return, *IDK
5055 indicates what kind of id-expression (if any) was present. */
5057 static cp_expr
5058 cp_parser_primary_expression (cp_parser *parser,
5059 bool address_p,
5060 bool cast_p,
5061 bool template_arg_p,
5062 bool decltype_p,
5063 cp_id_kind *idk)
5065 cp_token *token = NULL;
5067 /* Assume the primary expression is not an id-expression. */
5068 *idk = CP_ID_KIND_NONE;
5070 /* Peek at the next token. */
5071 token = cp_lexer_peek_token (parser->lexer);
5072 switch ((int) token->type)
5074 /* literal:
5075 integer-literal
5076 character-literal
5077 floating-literal
5078 string-literal
5079 boolean-literal
5080 pointer-literal
5081 user-defined-literal */
5082 case CPP_CHAR:
5083 case CPP_CHAR16:
5084 case CPP_CHAR32:
5085 case CPP_WCHAR:
5086 case CPP_UTF8CHAR:
5087 case CPP_NUMBER:
5088 case CPP_PREPARSED_EXPR:
5089 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5090 return cp_parser_userdef_numeric_literal (parser);
5091 token = cp_lexer_consume_token (parser->lexer);
5092 if (TREE_CODE (token->u.value) == FIXED_CST)
5094 error_at (token->location,
5095 "fixed-point types not supported in C++");
5096 return error_mark_node;
5098 /* Floating-point literals are only allowed in an integral
5099 constant expression if they are cast to an integral or
5100 enumeration type. */
5101 if (TREE_CODE (token->u.value) == REAL_CST
5102 && parser->integral_constant_expression_p
5103 && pedantic)
5105 /* CAST_P will be set even in invalid code like "int(2.7 +
5106 ...)". Therefore, we have to check that the next token
5107 is sure to end the cast. */
5108 if (cast_p)
5110 cp_token *next_token;
5112 next_token = cp_lexer_peek_token (parser->lexer);
5113 if (/* The comma at the end of an
5114 enumerator-definition. */
5115 next_token->type != CPP_COMMA
5116 /* The curly brace at the end of an enum-specifier. */
5117 && next_token->type != CPP_CLOSE_BRACE
5118 /* The end of a statement. */
5119 && next_token->type != CPP_SEMICOLON
5120 /* The end of the cast-expression. */
5121 && next_token->type != CPP_CLOSE_PAREN
5122 /* The end of an array bound. */
5123 && next_token->type != CPP_CLOSE_SQUARE
5124 /* The closing ">" in a template-argument-list. */
5125 && (next_token->type != CPP_GREATER
5126 || parser->greater_than_is_operator_p)
5127 /* C++0x only: A ">>" treated like two ">" tokens,
5128 in a template-argument-list. */
5129 && (next_token->type != CPP_RSHIFT
5130 || (cxx_dialect == cxx98)
5131 || parser->greater_than_is_operator_p))
5132 cast_p = false;
5135 /* If we are within a cast, then the constraint that the
5136 cast is to an integral or enumeration type will be
5137 checked at that point. If we are not within a cast, then
5138 this code is invalid. */
5139 if (!cast_p)
5140 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5142 return cp_expr (token->u.value, token->location);
5144 case CPP_CHAR_USERDEF:
5145 case CPP_CHAR16_USERDEF:
5146 case CPP_CHAR32_USERDEF:
5147 case CPP_WCHAR_USERDEF:
5148 case CPP_UTF8CHAR_USERDEF:
5149 return cp_parser_userdef_char_literal (parser);
5151 case CPP_STRING:
5152 case CPP_STRING16:
5153 case CPP_STRING32:
5154 case CPP_WSTRING:
5155 case CPP_UTF8STRING:
5156 case CPP_STRING_USERDEF:
5157 case CPP_STRING16_USERDEF:
5158 case CPP_STRING32_USERDEF:
5159 case CPP_WSTRING_USERDEF:
5160 case CPP_UTF8STRING_USERDEF:
5161 /* ??? Should wide strings be allowed when parser->translate_strings_p
5162 is false (i.e. in attributes)? If not, we can kill the third
5163 argument to cp_parser_string_literal. */
5164 return cp_parser_string_literal (parser,
5165 parser->translate_strings_p,
5166 true);
5168 case CPP_OPEN_PAREN:
5169 /* If we see `( { ' then we are looking at the beginning of
5170 a GNU statement-expression. */
5171 if (cp_parser_allow_gnu_extensions_p (parser)
5172 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5174 /* Statement-expressions are not allowed by the standard. */
5175 pedwarn (token->location, OPT_Wpedantic,
5176 "ISO C++ forbids braced-groups within expressions");
5178 /* And they're not allowed outside of a function-body; you
5179 cannot, for example, write:
5181 int i = ({ int j = 3; j + 1; });
5183 at class or namespace scope. */
5184 if (!parser->in_function_body
5185 || parser->in_template_argument_list_p)
5187 error_at (token->location,
5188 "statement-expressions are not allowed outside "
5189 "functions nor in template-argument lists");
5190 cp_parser_skip_to_end_of_block_or_statement (parser);
5191 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5192 cp_lexer_consume_token (parser->lexer);
5193 return error_mark_node;
5195 else
5196 return cp_parser_statement_expr (parser);
5198 /* Otherwise it's a normal parenthesized expression. */
5200 cp_expr expr;
5201 bool saved_greater_than_is_operator_p;
5203 location_t open_paren_loc = token->location;
5205 /* Consume the `('. */
5206 matching_parens parens;
5207 parens.consume_open (parser);
5208 /* Within a parenthesized expression, a `>' token is always
5209 the greater-than operator. */
5210 saved_greater_than_is_operator_p
5211 = parser->greater_than_is_operator_p;
5212 parser->greater_than_is_operator_p = true;
5214 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5215 /* Left fold expression. */
5216 expr = NULL_TREE;
5217 else
5218 /* Parse the parenthesized expression. */
5219 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5221 token = cp_lexer_peek_token (parser->lexer);
5222 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5224 expr = cp_parser_fold_expression (parser, expr);
5225 if (expr != error_mark_node
5226 && cxx_dialect < cxx17
5227 && !in_system_header_at (input_location))
5228 pedwarn (input_location, 0, "fold-expressions only available "
5229 "with -std=c++17 or -std=gnu++17");
5231 else
5232 /* Let the front end know that this expression was
5233 enclosed in parentheses. This matters in case, for
5234 example, the expression is of the form `A::B', since
5235 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5236 not. */
5237 expr = finish_parenthesized_expr (expr);
5239 /* DR 705: Wrapping an unqualified name in parentheses
5240 suppresses arg-dependent lookup. We want to pass back
5241 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5242 (c++/37862), but none of the others. */
5243 if (*idk != CP_ID_KIND_QUALIFIED)
5244 *idk = CP_ID_KIND_NONE;
5246 /* The `>' token might be the end of a template-id or
5247 template-parameter-list now. */
5248 parser->greater_than_is_operator_p
5249 = saved_greater_than_is_operator_p;
5251 /* Consume the `)'. */
5252 token = cp_lexer_peek_token (parser->lexer);
5253 location_t close_paren_loc = token->location;
5254 expr.set_range (open_paren_loc, close_paren_loc);
5255 if (!parens.require_close (parser)
5256 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5257 cp_parser_skip_to_end_of_statement (parser);
5259 return expr;
5262 case CPP_OPEN_SQUARE:
5264 if (c_dialect_objc ())
5266 /* We might have an Objective-C++ message. */
5267 cp_parser_parse_tentatively (parser);
5268 tree msg = cp_parser_objc_message_expression (parser);
5269 /* If that works out, we're done ... */
5270 if (cp_parser_parse_definitely (parser))
5271 return msg;
5272 /* ... else, fall though to see if it's a lambda. */
5274 cp_expr lam = cp_parser_lambda_expression (parser);
5275 /* Don't warn about a failed tentative parse. */
5276 if (cp_parser_error_occurred (parser))
5277 return error_mark_node;
5278 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5279 return lam;
5282 case CPP_OBJC_STRING:
5283 if (c_dialect_objc ())
5284 /* We have an Objective-C++ string literal. */
5285 return cp_parser_objc_expression (parser);
5286 cp_parser_error (parser, "expected primary-expression");
5287 return error_mark_node;
5289 case CPP_KEYWORD:
5290 switch (token->keyword)
5292 /* These two are the boolean literals. */
5293 case RID_TRUE:
5294 cp_lexer_consume_token (parser->lexer);
5295 return cp_expr (boolean_true_node, token->location);
5296 case RID_FALSE:
5297 cp_lexer_consume_token (parser->lexer);
5298 return cp_expr (boolean_false_node, token->location);
5300 /* The `__null' literal. */
5301 case RID_NULL:
5302 cp_lexer_consume_token (parser->lexer);
5303 return cp_expr (null_node, token->location);
5305 /* The `nullptr' literal. */
5306 case RID_NULLPTR:
5307 cp_lexer_consume_token (parser->lexer);
5308 return cp_expr (nullptr_node, token->location);
5310 /* Recognize the `this' keyword. */
5311 case RID_THIS:
5312 cp_lexer_consume_token (parser->lexer);
5313 if (parser->local_variables_forbidden_p)
5315 error_at (token->location,
5316 "%<this%> may not be used in this context");
5317 return error_mark_node;
5319 /* Pointers cannot appear in constant-expressions. */
5320 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5321 return error_mark_node;
5322 return cp_expr (finish_this_expr (), token->location);
5324 /* The `operator' keyword can be the beginning of an
5325 id-expression. */
5326 case RID_OPERATOR:
5327 goto id_expression;
5329 case RID_FUNCTION_NAME:
5330 case RID_PRETTY_FUNCTION_NAME:
5331 case RID_C99_FUNCTION_NAME:
5333 non_integral_constant name;
5335 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5336 __func__ are the names of variables -- but they are
5337 treated specially. Therefore, they are handled here,
5338 rather than relying on the generic id-expression logic
5339 below. Grammatically, these names are id-expressions.
5341 Consume the token. */
5342 token = cp_lexer_consume_token (parser->lexer);
5344 switch (token->keyword)
5346 case RID_FUNCTION_NAME:
5347 name = NIC_FUNC_NAME;
5348 break;
5349 case RID_PRETTY_FUNCTION_NAME:
5350 name = NIC_PRETTY_FUNC;
5351 break;
5352 case RID_C99_FUNCTION_NAME:
5353 name = NIC_C99_FUNC;
5354 break;
5355 default:
5356 gcc_unreachable ();
5359 if (cp_parser_non_integral_constant_expression (parser, name))
5360 return error_mark_node;
5362 /* Look up the name. */
5363 return finish_fname (token->u.value);
5366 case RID_VA_ARG:
5368 tree expression;
5369 tree type;
5370 source_location type_location;
5371 location_t start_loc
5372 = cp_lexer_peek_token (parser->lexer)->location;
5373 /* The `__builtin_va_arg' construct is used to handle
5374 `va_arg'. Consume the `__builtin_va_arg' token. */
5375 cp_lexer_consume_token (parser->lexer);
5376 /* Look for the opening `('. */
5377 matching_parens parens;
5378 parens.require_open (parser);
5379 /* Now, parse the assignment-expression. */
5380 expression = cp_parser_assignment_expression (parser);
5381 /* Look for the `,'. */
5382 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5383 type_location = cp_lexer_peek_token (parser->lexer)->location;
5384 /* Parse the type-id. */
5386 type_id_in_expr_sentinel s (parser);
5387 type = cp_parser_type_id (parser);
5389 /* Look for the closing `)'. */
5390 location_t finish_loc
5391 = cp_lexer_peek_token (parser->lexer)->location;
5392 parens.require_close (parser);
5393 /* Using `va_arg' in a constant-expression is not
5394 allowed. */
5395 if (cp_parser_non_integral_constant_expression (parser,
5396 NIC_VA_ARG))
5397 return error_mark_node;
5398 /* Construct a location of the form:
5399 __builtin_va_arg (v, int)
5400 ~~~~~~~~~~~~~~~~~~~~~^~~~
5401 with the caret at the type, ranging from the start of the
5402 "__builtin_va_arg" token to the close paren. */
5403 location_t combined_loc
5404 = make_location (type_location, start_loc, finish_loc);
5405 return build_x_va_arg (combined_loc, expression, type);
5408 case RID_OFFSETOF:
5409 return cp_parser_builtin_offsetof (parser);
5411 case RID_HAS_NOTHROW_ASSIGN:
5412 case RID_HAS_NOTHROW_CONSTRUCTOR:
5413 case RID_HAS_NOTHROW_COPY:
5414 case RID_HAS_TRIVIAL_ASSIGN:
5415 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5416 case RID_HAS_TRIVIAL_COPY:
5417 case RID_HAS_TRIVIAL_DESTRUCTOR:
5418 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5419 case RID_HAS_VIRTUAL_DESTRUCTOR:
5420 case RID_IS_ABSTRACT:
5421 case RID_IS_AGGREGATE:
5422 case RID_IS_BASE_OF:
5423 case RID_IS_CLASS:
5424 case RID_IS_EMPTY:
5425 case RID_IS_ENUM:
5426 case RID_IS_FINAL:
5427 case RID_IS_LITERAL_TYPE:
5428 case RID_IS_POD:
5429 case RID_IS_POLYMORPHIC:
5430 case RID_IS_SAME_AS:
5431 case RID_IS_STD_LAYOUT:
5432 case RID_IS_TRIVIAL:
5433 case RID_IS_TRIVIALLY_ASSIGNABLE:
5434 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5435 case RID_IS_TRIVIALLY_COPYABLE:
5436 case RID_IS_UNION:
5437 case RID_IS_ASSIGNABLE:
5438 case RID_IS_CONSTRUCTIBLE:
5439 return cp_parser_trait_expr (parser, token->keyword);
5441 // C++ concepts
5442 case RID_REQUIRES:
5443 return cp_parser_requires_expression (parser);
5445 /* Objective-C++ expressions. */
5446 case RID_AT_ENCODE:
5447 case RID_AT_PROTOCOL:
5448 case RID_AT_SELECTOR:
5449 return cp_parser_objc_expression (parser);
5451 case RID_TEMPLATE:
5452 if (parser->in_function_body
5453 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5454 == CPP_LESS))
5456 error_at (token->location,
5457 "a template declaration cannot appear at block scope");
5458 cp_parser_skip_to_end_of_block_or_statement (parser);
5459 return error_mark_node;
5461 /* FALLTHRU */
5462 default:
5463 cp_parser_error (parser, "expected primary-expression");
5464 return error_mark_node;
5467 /* An id-expression can start with either an identifier, a
5468 `::' as the beginning of a qualified-id, or the "operator"
5469 keyword. */
5470 case CPP_NAME:
5471 case CPP_SCOPE:
5472 case CPP_TEMPLATE_ID:
5473 case CPP_NESTED_NAME_SPECIFIER:
5475 id_expression:
5476 cp_expr id_expression;
5477 cp_expr decl;
5478 const char *error_msg;
5479 bool template_p;
5480 bool done;
5481 cp_token *id_expr_token;
5483 /* Parse the id-expression. */
5484 id_expression
5485 = cp_parser_id_expression (parser,
5486 /*template_keyword_p=*/false,
5487 /*check_dependency_p=*/true,
5488 &template_p,
5489 /*declarator_p=*/false,
5490 /*optional_p=*/false);
5491 if (id_expression == error_mark_node)
5492 return error_mark_node;
5493 id_expr_token = token;
5494 token = cp_lexer_peek_token (parser->lexer);
5495 done = (token->type != CPP_OPEN_SQUARE
5496 && token->type != CPP_OPEN_PAREN
5497 && token->type != CPP_DOT
5498 && token->type != CPP_DEREF
5499 && token->type != CPP_PLUS_PLUS
5500 && token->type != CPP_MINUS_MINUS);
5501 /* If we have a template-id, then no further lookup is
5502 required. If the template-id was for a template-class, we
5503 will sometimes have a TYPE_DECL at this point. */
5504 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5505 || TREE_CODE (id_expression) == TYPE_DECL)
5506 decl = id_expression;
5507 /* Look up the name. */
5508 else
5510 tree ambiguous_decls;
5512 /* If we already know that this lookup is ambiguous, then
5513 we've already issued an error message; there's no reason
5514 to check again. */
5515 if (id_expr_token->type == CPP_NAME
5516 && id_expr_token->error_reported)
5518 cp_parser_simulate_error (parser);
5519 return error_mark_node;
5522 decl = cp_parser_lookup_name (parser, id_expression,
5523 none_type,
5524 template_p,
5525 /*is_namespace=*/false,
5526 /*check_dependency=*/true,
5527 &ambiguous_decls,
5528 id_expr_token->location);
5529 /* If the lookup was ambiguous, an error will already have
5530 been issued. */
5531 if (ambiguous_decls)
5532 return error_mark_node;
5534 /* In Objective-C++, we may have an Objective-C 2.0
5535 dot-syntax for classes here. */
5536 if (c_dialect_objc ()
5537 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5538 && TREE_CODE (decl) == TYPE_DECL
5539 && objc_is_class_name (decl))
5541 tree component;
5542 cp_lexer_consume_token (parser->lexer);
5543 component = cp_parser_identifier (parser);
5544 if (component == error_mark_node)
5545 return error_mark_node;
5547 tree result = objc_build_class_component_ref (id_expression,
5548 component);
5549 /* Build a location of the form:
5550 expr.component
5551 ~~~~~^~~~~~~~~
5552 with caret at the start of the component name (at
5553 input_location), ranging from the start of the id_expression
5554 to the end of the component name. */
5555 location_t combined_loc
5556 = make_location (input_location, id_expression.get_start (),
5557 get_finish (input_location));
5558 protected_set_expr_location (result, combined_loc);
5559 return result;
5562 /* In Objective-C++, an instance variable (ivar) may be preferred
5563 to whatever cp_parser_lookup_name() found.
5564 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5565 rest of c-family, we have to do a little extra work to preserve
5566 any location information in cp_expr "decl". Given that
5567 objc_lookup_ivar is implemented in "c-family" and "objc", we
5568 have a trip through the pure "tree" type, rather than cp_expr.
5569 Naively copying it back to "decl" would implicitly give the
5570 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5571 store an EXPR_LOCATION. Hence we only update "decl" (and
5572 hence its location_t) if we get back a different tree node. */
5573 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5574 id_expression);
5575 if (decl_tree != decl.get_value ())
5576 decl = cp_expr (decl_tree);
5578 /* If name lookup gives us a SCOPE_REF, then the
5579 qualifying scope was dependent. */
5580 if (TREE_CODE (decl) == SCOPE_REF)
5582 /* At this point, we do not know if DECL is a valid
5583 integral constant expression. We assume that it is
5584 in fact such an expression, so that code like:
5586 template <int N> struct A {
5587 int a[B<N>::i];
5590 is accepted. At template-instantiation time, we
5591 will check that B<N>::i is actually a constant. */
5592 return decl;
5594 /* Check to see if DECL is a local variable in a context
5595 where that is forbidden. */
5596 if (parser->local_variables_forbidden_p
5597 && local_variable_p (decl))
5599 error_at (id_expr_token->location,
5600 "local variable %qD may not appear in this context",
5601 decl.get_value ());
5602 return error_mark_node;
5606 decl = (finish_id_expression
5607 (id_expression, decl, parser->scope,
5608 idk,
5609 parser->integral_constant_expression_p,
5610 parser->allow_non_integral_constant_expression_p,
5611 &parser->non_integral_constant_expression_p,
5612 template_p, done, address_p,
5613 template_arg_p,
5614 &error_msg,
5615 id_expression.get_location ()));
5616 if (error_msg)
5617 cp_parser_error (parser, error_msg);
5618 decl.set_location (id_expr_token->location);
5619 return decl;
5622 /* Anything else is an error. */
5623 default:
5624 cp_parser_error (parser, "expected primary-expression");
5625 return error_mark_node;
5629 static inline cp_expr
5630 cp_parser_primary_expression (cp_parser *parser,
5631 bool address_p,
5632 bool cast_p,
5633 bool template_arg_p,
5634 cp_id_kind *idk)
5636 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5637 /*decltype*/false, idk);
5640 /* Parse an id-expression.
5642 id-expression:
5643 unqualified-id
5644 qualified-id
5646 qualified-id:
5647 :: [opt] nested-name-specifier template [opt] unqualified-id
5648 :: identifier
5649 :: operator-function-id
5650 :: template-id
5652 Return a representation of the unqualified portion of the
5653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5654 a `::' or nested-name-specifier.
5656 Often, if the id-expression was a qualified-id, the caller will
5657 want to make a SCOPE_REF to represent the qualified-id. This
5658 function does not do this in order to avoid wastefully creating
5659 SCOPE_REFs when they are not required.
5661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5662 `template' keyword.
5664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5665 uninstantiated templates.
5667 If *TEMPLATE_P is non-NULL, it is set to true iff the
5668 `template' keyword is used to explicitly indicate that the entity
5669 named is a template.
5671 If DECLARATOR_P is true, the id-expression is appearing as part of
5672 a declarator, rather than as part of an expression. */
5674 static cp_expr
5675 cp_parser_id_expression (cp_parser *parser,
5676 bool template_keyword_p,
5677 bool check_dependency_p,
5678 bool *template_p,
5679 bool declarator_p,
5680 bool optional_p)
5682 bool global_scope_p;
5683 bool nested_name_specifier_p;
5685 /* Assume the `template' keyword was not used. */
5686 if (template_p)
5687 *template_p = template_keyword_p;
5689 /* Look for the optional `::' operator. */
5690 global_scope_p
5691 = (!template_keyword_p
5692 && (cp_parser_global_scope_opt (parser,
5693 /*current_scope_valid_p=*/false)
5694 != NULL_TREE));
5696 /* Look for the optional nested-name-specifier. */
5697 nested_name_specifier_p
5698 = (cp_parser_nested_name_specifier_opt (parser,
5699 /*typename_keyword_p=*/false,
5700 check_dependency_p,
5701 /*type_p=*/false,
5702 declarator_p,
5703 template_keyword_p)
5704 != NULL_TREE);
5706 /* If there is a nested-name-specifier, then we are looking at
5707 the first qualified-id production. */
5708 if (nested_name_specifier_p)
5710 tree saved_scope;
5711 tree saved_object_scope;
5712 tree saved_qualifying_scope;
5713 cp_expr unqualified_id;
5714 bool is_template;
5716 /* See if the next token is the `template' keyword. */
5717 if (!template_p)
5718 template_p = &is_template;
5719 *template_p = cp_parser_optional_template_keyword (parser);
5720 /* Name lookup we do during the processing of the
5721 unqualified-id might obliterate SCOPE. */
5722 saved_scope = parser->scope;
5723 saved_object_scope = parser->object_scope;
5724 saved_qualifying_scope = parser->qualifying_scope;
5725 /* Process the final unqualified-id. */
5726 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5727 check_dependency_p,
5728 declarator_p,
5729 /*optional_p=*/false);
5730 /* Restore the SAVED_SCOPE for our caller. */
5731 parser->scope = saved_scope;
5732 parser->object_scope = saved_object_scope;
5733 parser->qualifying_scope = saved_qualifying_scope;
5735 return unqualified_id;
5737 /* Otherwise, if we are in global scope, then we are looking at one
5738 of the other qualified-id productions. */
5739 else if (global_scope_p)
5741 cp_token *token;
5742 tree id;
5744 /* Peek at the next token. */
5745 token = cp_lexer_peek_token (parser->lexer);
5747 /* If it's an identifier, and the next token is not a "<", then
5748 we can avoid the template-id case. This is an optimization
5749 for this common case. */
5750 if (token->type == CPP_NAME
5751 && !cp_parser_nth_token_starts_template_argument_list_p
5752 (parser, 2))
5753 return cp_parser_identifier (parser);
5755 cp_parser_parse_tentatively (parser);
5756 /* Try a template-id. */
5757 id = cp_parser_template_id (parser,
5758 /*template_keyword_p=*/false,
5759 /*check_dependency_p=*/true,
5760 none_type,
5761 declarator_p);
5762 /* If that worked, we're done. */
5763 if (cp_parser_parse_definitely (parser))
5764 return id;
5766 /* Peek at the next token. (Changes in the token buffer may
5767 have invalidated the pointer obtained above.) */
5768 token = cp_lexer_peek_token (parser->lexer);
5770 switch (token->type)
5772 case CPP_NAME:
5773 return cp_parser_identifier (parser);
5775 case CPP_KEYWORD:
5776 if (token->keyword == RID_OPERATOR)
5777 return cp_parser_operator_function_id (parser);
5778 /* Fall through. */
5780 default:
5781 cp_parser_error (parser, "expected id-expression");
5782 return error_mark_node;
5785 else
5786 return cp_parser_unqualified_id (parser, template_keyword_p,
5787 /*check_dependency_p=*/true,
5788 declarator_p,
5789 optional_p);
5792 /* Parse an unqualified-id.
5794 unqualified-id:
5795 identifier
5796 operator-function-id
5797 conversion-function-id
5798 ~ class-name
5799 template-id
5801 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5802 keyword, in a construct like `A::template ...'.
5804 Returns a representation of unqualified-id. For the `identifier'
5805 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5806 production a BIT_NOT_EXPR is returned; the operand of the
5807 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5808 other productions, see the documentation accompanying the
5809 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5810 names are looked up in uninstantiated templates. If DECLARATOR_P
5811 is true, the unqualified-id is appearing as part of a declarator,
5812 rather than as part of an expression. */
5814 static cp_expr
5815 cp_parser_unqualified_id (cp_parser* parser,
5816 bool template_keyword_p,
5817 bool check_dependency_p,
5818 bool declarator_p,
5819 bool optional_p)
5821 cp_token *token;
5823 /* Peek at the next token. */
5824 token = cp_lexer_peek_token (parser->lexer);
5826 switch ((int) token->type)
5828 case CPP_NAME:
5830 tree id;
5832 /* We don't know yet whether or not this will be a
5833 template-id. */
5834 cp_parser_parse_tentatively (parser);
5835 /* Try a template-id. */
5836 id = cp_parser_template_id (parser, template_keyword_p,
5837 check_dependency_p,
5838 none_type,
5839 declarator_p);
5840 /* If it worked, we're done. */
5841 if (cp_parser_parse_definitely (parser))
5842 return id;
5843 /* Otherwise, it's an ordinary identifier. */
5844 return cp_parser_identifier (parser);
5847 case CPP_TEMPLATE_ID:
5848 return cp_parser_template_id (parser, template_keyword_p,
5849 check_dependency_p,
5850 none_type,
5851 declarator_p);
5853 case CPP_COMPL:
5855 tree type_decl;
5856 tree qualifying_scope;
5857 tree object_scope;
5858 tree scope;
5859 bool done;
5861 /* Consume the `~' token. */
5862 cp_lexer_consume_token (parser->lexer);
5863 /* Parse the class-name. The standard, as written, seems to
5864 say that:
5866 template <typename T> struct S { ~S (); };
5867 template <typename T> S<T>::~S() {}
5869 is invalid, since `~' must be followed by a class-name, but
5870 `S<T>' is dependent, and so not known to be a class.
5871 That's not right; we need to look in uninstantiated
5872 templates. A further complication arises from:
5874 template <typename T> void f(T t) {
5875 t.T::~T();
5878 Here, it is not possible to look up `T' in the scope of `T'
5879 itself. We must look in both the current scope, and the
5880 scope of the containing complete expression.
5882 Yet another issue is:
5884 struct S {
5885 int S;
5886 ~S();
5889 S::~S() {}
5891 The standard does not seem to say that the `S' in `~S'
5892 should refer to the type `S' and not the data member
5893 `S::S'. */
5895 /* DR 244 says that we look up the name after the "~" in the
5896 same scope as we looked up the qualifying name. That idea
5897 isn't fully worked out; it's more complicated than that. */
5898 scope = parser->scope;
5899 object_scope = parser->object_scope;
5900 qualifying_scope = parser->qualifying_scope;
5902 /* Check for invalid scopes. */
5903 if (scope == error_mark_node)
5905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5906 cp_lexer_consume_token (parser->lexer);
5907 return error_mark_node;
5909 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5911 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5912 error_at (token->location,
5913 "scope %qT before %<~%> is not a class-name",
5914 scope);
5915 cp_parser_simulate_error (parser);
5916 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5917 cp_lexer_consume_token (parser->lexer);
5918 return error_mark_node;
5920 gcc_assert (!scope || TYPE_P (scope));
5922 /* If the name is of the form "X::~X" it's OK even if X is a
5923 typedef. */
5924 token = cp_lexer_peek_token (parser->lexer);
5925 if (scope
5926 && token->type == CPP_NAME
5927 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5928 != CPP_LESS)
5929 && (token->u.value == TYPE_IDENTIFIER (scope)
5930 || (CLASS_TYPE_P (scope)
5931 && constructor_name_p (token->u.value, scope))))
5933 cp_lexer_consume_token (parser->lexer);
5934 return build_nt (BIT_NOT_EXPR, scope);
5937 /* ~auto means the destructor of whatever the object is. */
5938 if (cp_parser_is_keyword (token, RID_AUTO))
5940 if (cxx_dialect < cxx14)
5941 pedwarn (input_location, 0,
5942 "%<~auto%> only available with "
5943 "-std=c++14 or -std=gnu++14");
5944 cp_lexer_consume_token (parser->lexer);
5945 return build_nt (BIT_NOT_EXPR, make_auto ());
5948 /* If there was an explicit qualification (S::~T), first look
5949 in the scope given by the qualification (i.e., S).
5951 Note: in the calls to cp_parser_class_name below we pass
5952 typename_type so that lookup finds the injected-class-name
5953 rather than the constructor. */
5954 done = false;
5955 type_decl = NULL_TREE;
5956 if (scope)
5958 cp_parser_parse_tentatively (parser);
5959 type_decl = cp_parser_class_name (parser,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5962 typename_type,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5965 declarator_p);
5966 if (cp_parser_parse_definitely (parser))
5967 done = true;
5969 /* In "N::S::~S", look in "N" as well. */
5970 if (!done && scope && qualifying_scope)
5972 cp_parser_parse_tentatively (parser);
5973 parser->scope = qualifying_scope;
5974 parser->object_scope = NULL_TREE;
5975 parser->qualifying_scope = NULL_TREE;
5976 type_decl
5977 = cp_parser_class_name (parser,
5978 /*typename_keyword_p=*/false,
5979 /*template_keyword_p=*/false,
5980 typename_type,
5981 /*check_dependency=*/false,
5982 /*class_head_p=*/false,
5983 declarator_p);
5984 if (cp_parser_parse_definitely (parser))
5985 done = true;
5987 /* In "p->S::~T", look in the scope given by "*p" as well. */
5988 else if (!done && object_scope)
5990 cp_parser_parse_tentatively (parser);
5991 parser->scope = object_scope;
5992 parser->object_scope = NULL_TREE;
5993 parser->qualifying_scope = NULL_TREE;
5994 type_decl
5995 = cp_parser_class_name (parser,
5996 /*typename_keyword_p=*/false,
5997 /*template_keyword_p=*/false,
5998 typename_type,
5999 /*check_dependency=*/false,
6000 /*class_head_p=*/false,
6001 declarator_p);
6002 if (cp_parser_parse_definitely (parser))
6003 done = true;
6005 /* Look in the surrounding context. */
6006 if (!done)
6008 parser->scope = NULL_TREE;
6009 parser->object_scope = NULL_TREE;
6010 parser->qualifying_scope = NULL_TREE;
6011 if (processing_template_decl)
6012 cp_parser_parse_tentatively (parser);
6013 type_decl
6014 = cp_parser_class_name (parser,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6017 typename_type,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6020 declarator_p);
6021 if (processing_template_decl
6022 && ! cp_parser_parse_definitely (parser))
6024 /* We couldn't find a type with this name. If we're parsing
6025 tentatively, fail and try something else. */
6026 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6028 cp_parser_simulate_error (parser);
6029 return error_mark_node;
6031 /* Otherwise, accept it and check for a match at instantiation
6032 time. */
6033 type_decl = cp_parser_identifier (parser);
6034 if (type_decl != error_mark_node)
6035 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6036 return type_decl;
6039 /* If an error occurred, assume that the name of the
6040 destructor is the same as the name of the qualifying
6041 class. That allows us to keep parsing after running
6042 into ill-formed destructor names. */
6043 if (type_decl == error_mark_node && scope)
6044 return build_nt (BIT_NOT_EXPR, scope);
6045 else if (type_decl == error_mark_node)
6046 return error_mark_node;
6048 /* Check that destructor name and scope match. */
6049 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6051 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6052 error_at (token->location,
6053 "declaration of %<~%T%> as member of %qT",
6054 type_decl, scope);
6055 cp_parser_simulate_error (parser);
6056 return error_mark_node;
6059 /* [class.dtor]
6061 A typedef-name that names a class shall not be used as the
6062 identifier in the declarator for a destructor declaration. */
6063 if (declarator_p
6064 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6065 && !DECL_SELF_REFERENCE_P (type_decl)
6066 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6067 error_at (token->location,
6068 "typedef-name %qD used as destructor declarator",
6069 type_decl);
6071 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6074 case CPP_KEYWORD:
6075 if (token->keyword == RID_OPERATOR)
6077 cp_expr id;
6079 /* This could be a template-id, so we try that first. */
6080 cp_parser_parse_tentatively (parser);
6081 /* Try a template-id. */
6082 id = cp_parser_template_id (parser, template_keyword_p,
6083 /*check_dependency_p=*/true,
6084 none_type,
6085 declarator_p);
6086 /* If that worked, we're done. */
6087 if (cp_parser_parse_definitely (parser))
6088 return id;
6089 /* We still don't know whether we're looking at an
6090 operator-function-id or a conversion-function-id. */
6091 cp_parser_parse_tentatively (parser);
6092 /* Try an operator-function-id. */
6093 id = cp_parser_operator_function_id (parser);
6094 /* If that didn't work, try a conversion-function-id. */
6095 if (!cp_parser_parse_definitely (parser))
6096 id = cp_parser_conversion_function_id (parser);
6098 return id;
6100 /* Fall through. */
6102 default:
6103 if (optional_p)
6104 return NULL_TREE;
6105 cp_parser_error (parser, "expected unqualified-id");
6106 return error_mark_node;
6110 /* Parse an (optional) nested-name-specifier.
6112 nested-name-specifier: [C++98]
6113 class-or-namespace-name :: nested-name-specifier [opt]
6114 class-or-namespace-name :: template nested-name-specifier [opt]
6116 nested-name-specifier: [C++0x]
6117 type-name ::
6118 namespace-name ::
6119 nested-name-specifier identifier ::
6120 nested-name-specifier template [opt] simple-template-id ::
6122 PARSER->SCOPE should be set appropriately before this function is
6123 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6124 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6125 in name lookups.
6127 Sets PARSER->SCOPE to the class (TYPE) or namespace
6128 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6129 it unchanged if there is no nested-name-specifier. Returns the new
6130 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6132 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6133 part of a declaration and/or decl-specifier. */
6135 static tree
6136 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6137 bool typename_keyword_p,
6138 bool check_dependency_p,
6139 bool type_p,
6140 bool is_declaration,
6141 bool template_keyword_p /* = false */)
6143 bool success = false;
6144 cp_token_position start = 0;
6145 cp_token *token;
6147 /* Remember where the nested-name-specifier starts. */
6148 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6150 start = cp_lexer_token_position (parser->lexer, false);
6151 push_deferring_access_checks (dk_deferred);
6154 while (true)
6156 tree new_scope;
6157 tree old_scope;
6158 tree saved_qualifying_scope;
6160 /* Spot cases that cannot be the beginning of a
6161 nested-name-specifier. */
6162 token = cp_lexer_peek_token (parser->lexer);
6164 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6165 the already parsed nested-name-specifier. */
6166 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6168 /* Grab the nested-name-specifier and continue the loop. */
6169 cp_parser_pre_parsed_nested_name_specifier (parser);
6170 /* If we originally encountered this nested-name-specifier
6171 with IS_DECLARATION set to false, we will not have
6172 resolved TYPENAME_TYPEs, so we must do so here. */
6173 if (is_declaration
6174 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6176 new_scope = resolve_typename_type (parser->scope,
6177 /*only_current_p=*/false);
6178 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6179 parser->scope = new_scope;
6181 success = true;
6182 continue;
6185 /* Spot cases that cannot be the beginning of a
6186 nested-name-specifier. On the second and subsequent times
6187 through the loop, we look for the `template' keyword. */
6188 if (success && token->keyword == RID_TEMPLATE)
6190 /* A template-id can start a nested-name-specifier. */
6191 else if (token->type == CPP_TEMPLATE_ID)
6193 /* DR 743: decltype can be used in a nested-name-specifier. */
6194 else if (token_is_decltype (token))
6196 else
6198 /* If the next token is not an identifier, then it is
6199 definitely not a type-name or namespace-name. */
6200 if (token->type != CPP_NAME)
6201 break;
6202 /* If the following token is neither a `<' (to begin a
6203 template-id), nor a `::', then we are not looking at a
6204 nested-name-specifier. */
6205 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6207 if (token->type == CPP_COLON
6208 && parser->colon_corrects_to_scope_p
6209 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6211 gcc_rich_location richloc (token->location);
6212 richloc.add_fixit_replace ("::");
6213 error_at (&richloc,
6214 "found %<:%> in nested-name-specifier, "
6215 "expected %<::%>");
6216 token->type = CPP_SCOPE;
6219 if (token->type != CPP_SCOPE
6220 && !cp_parser_nth_token_starts_template_argument_list_p
6221 (parser, 2))
6222 break;
6225 /* The nested-name-specifier is optional, so we parse
6226 tentatively. */
6227 cp_parser_parse_tentatively (parser);
6229 /* Look for the optional `template' keyword, if this isn't the
6230 first time through the loop. */
6231 if (success)
6232 template_keyword_p = cp_parser_optional_template_keyword (parser);
6234 /* Save the old scope since the name lookup we are about to do
6235 might destroy it. */
6236 old_scope = parser->scope;
6237 saved_qualifying_scope = parser->qualifying_scope;
6238 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6239 look up names in "X<T>::I" in order to determine that "Y" is
6240 a template. So, if we have a typename at this point, we make
6241 an effort to look through it. */
6242 if (is_declaration
6243 && !typename_keyword_p
6244 && parser->scope
6245 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6246 parser->scope = resolve_typename_type (parser->scope,
6247 /*only_current_p=*/false);
6248 /* Parse the qualifying entity. */
6249 new_scope
6250 = cp_parser_qualifying_entity (parser,
6251 typename_keyword_p,
6252 template_keyword_p,
6253 check_dependency_p,
6254 type_p,
6255 is_declaration);
6256 /* Look for the `::' token. */
6257 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6259 /* If we found what we wanted, we keep going; otherwise, we're
6260 done. */
6261 if (!cp_parser_parse_definitely (parser))
6263 bool error_p = false;
6265 /* Restore the OLD_SCOPE since it was valid before the
6266 failed attempt at finding the last
6267 class-or-namespace-name. */
6268 parser->scope = old_scope;
6269 parser->qualifying_scope = saved_qualifying_scope;
6271 /* If the next token is a decltype, and the one after that is a
6272 `::', then the decltype has failed to resolve to a class or
6273 enumeration type. Give this error even when parsing
6274 tentatively since it can't possibly be valid--and we're going
6275 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6276 won't get another chance.*/
6277 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6278 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6279 == CPP_SCOPE))
6281 token = cp_lexer_consume_token (parser->lexer);
6282 error_at (token->location, "decltype evaluates to %qT, "
6283 "which is not a class or enumeration type",
6284 token->u.tree_check_value->value);
6285 parser->scope = error_mark_node;
6286 error_p = true;
6287 /* As below. */
6288 success = true;
6289 cp_lexer_consume_token (parser->lexer);
6292 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6293 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6295 /* If we have a non-type template-id followed by ::, it can't
6296 possibly be valid. */
6297 token = cp_lexer_peek_token (parser->lexer);
6298 tree tid = token->u.tree_check_value->value;
6299 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6300 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6302 tree tmpl = NULL_TREE;
6303 if (is_overloaded_fn (tid))
6305 tree fns = get_fns (tid);
6306 if (OVL_SINGLE_P (fns))
6307 tmpl = OVL_FIRST (fns);
6308 error_at (token->location, "function template-id %qD "
6309 "in nested-name-specifier", tid);
6311 else
6313 /* Variable template. */
6314 tmpl = TREE_OPERAND (tid, 0);
6315 gcc_assert (variable_template_p (tmpl));
6316 error_at (token->location, "variable template-id %qD "
6317 "in nested-name-specifier", tid);
6319 if (tmpl)
6320 inform (DECL_SOURCE_LOCATION (tmpl),
6321 "%qD declared here", tmpl);
6323 parser->scope = error_mark_node;
6324 error_p = true;
6325 /* As below. */
6326 success = true;
6327 cp_lexer_consume_token (parser->lexer);
6328 cp_lexer_consume_token (parser->lexer);
6332 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6333 break;
6334 /* If the next token is an identifier, and the one after
6335 that is a `::', then any valid interpretation would have
6336 found a class-or-namespace-name. */
6337 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6338 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6339 == CPP_SCOPE)
6340 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6341 != CPP_COMPL))
6343 token = cp_lexer_consume_token (parser->lexer);
6344 if (!error_p)
6346 if (!token->error_reported)
6348 tree decl;
6349 tree ambiguous_decls;
6351 decl = cp_parser_lookup_name (parser, token->u.value,
6352 none_type,
6353 /*is_template=*/false,
6354 /*is_namespace=*/false,
6355 /*check_dependency=*/true,
6356 &ambiguous_decls,
6357 token->location);
6358 if (TREE_CODE (decl) == TEMPLATE_DECL)
6359 error_at (token->location,
6360 "%qD used without template arguments",
6361 decl);
6362 else if (ambiguous_decls)
6364 // cp_parser_lookup_name has the same diagnostic,
6365 // thus make sure to emit it at most once.
6366 if (cp_parser_uncommitted_to_tentative_parse_p
6367 (parser))
6369 error_at (token->location,
6370 "reference to %qD is ambiguous",
6371 token->u.value);
6372 print_candidates (ambiguous_decls);
6374 decl = error_mark_node;
6376 else
6378 if (cxx_dialect != cxx98)
6379 cp_parser_name_lookup_error
6380 (parser, token->u.value, decl, NLE_NOT_CXX98,
6381 token->location);
6382 else
6383 cp_parser_name_lookup_error
6384 (parser, token->u.value, decl, NLE_CXX98,
6385 token->location);
6388 parser->scope = error_mark_node;
6389 error_p = true;
6390 /* Treat this as a successful nested-name-specifier
6391 due to:
6393 [basic.lookup.qual]
6395 If the name found is not a class-name (clause
6396 _class_) or namespace-name (_namespace.def_), the
6397 program is ill-formed. */
6398 success = true;
6400 cp_lexer_consume_token (parser->lexer);
6402 break;
6404 /* We've found one valid nested-name-specifier. */
6405 success = true;
6406 /* Name lookup always gives us a DECL. */
6407 if (TREE_CODE (new_scope) == TYPE_DECL)
6408 new_scope = TREE_TYPE (new_scope);
6409 /* Uses of "template" must be followed by actual templates. */
6410 if (template_keyword_p
6411 && !(CLASS_TYPE_P (new_scope)
6412 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6413 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6414 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6415 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6416 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6417 == TEMPLATE_ID_EXPR)))
6418 permerror (input_location, TYPE_P (new_scope)
6419 ? G_("%qT is not a template")
6420 : G_("%qD is not a template"),
6421 new_scope);
6422 /* If it is a class scope, try to complete it; we are about to
6423 be looking up names inside the class. */
6424 if (TYPE_P (new_scope)
6425 /* Since checking types for dependency can be expensive,
6426 avoid doing it if the type is already complete. */
6427 && !COMPLETE_TYPE_P (new_scope)
6428 /* Do not try to complete dependent types. */
6429 && !dependent_type_p (new_scope))
6431 new_scope = complete_type (new_scope);
6432 /* If it is a typedef to current class, use the current
6433 class instead, as the typedef won't have any names inside
6434 it yet. */
6435 if (!COMPLETE_TYPE_P (new_scope)
6436 && currently_open_class (new_scope))
6437 new_scope = TYPE_MAIN_VARIANT (new_scope);
6439 /* Make sure we look in the right scope the next time through
6440 the loop. */
6441 parser->scope = new_scope;
6444 /* If parsing tentatively, replace the sequence of tokens that makes
6445 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6446 token. That way, should we re-parse the token stream, we will
6447 not have to repeat the effort required to do the parse, nor will
6448 we issue duplicate error messages. */
6449 if (success && start)
6451 cp_token *token;
6453 token = cp_lexer_token_at (parser->lexer, start);
6454 /* Reset the contents of the START token. */
6455 token->type = CPP_NESTED_NAME_SPECIFIER;
6456 /* Retrieve any deferred checks. Do not pop this access checks yet
6457 so the memory will not be reclaimed during token replacing below. */
6458 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6459 token->u.tree_check_value->value = parser->scope;
6460 token->u.tree_check_value->checks = get_deferred_access_checks ();
6461 token->u.tree_check_value->qualifying_scope =
6462 parser->qualifying_scope;
6463 token->keyword = RID_MAX;
6465 /* Purge all subsequent tokens. */
6466 cp_lexer_purge_tokens_after (parser->lexer, start);
6469 if (start)
6470 pop_to_parent_deferring_access_checks ();
6472 return success ? parser->scope : NULL_TREE;
6475 /* Parse a nested-name-specifier. See
6476 cp_parser_nested_name_specifier_opt for details. This function
6477 behaves identically, except that it will an issue an error if no
6478 nested-name-specifier is present. */
6480 static tree
6481 cp_parser_nested_name_specifier (cp_parser *parser,
6482 bool typename_keyword_p,
6483 bool check_dependency_p,
6484 bool type_p,
6485 bool is_declaration)
6487 tree scope;
6489 /* Look for the nested-name-specifier. */
6490 scope = cp_parser_nested_name_specifier_opt (parser,
6491 typename_keyword_p,
6492 check_dependency_p,
6493 type_p,
6494 is_declaration);
6495 /* If it was not present, issue an error message. */
6496 if (!scope)
6498 cp_parser_error (parser, "expected nested-name-specifier");
6499 parser->scope = NULL_TREE;
6502 return scope;
6505 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6506 this is either a class-name or a namespace-name (which corresponds
6507 to the class-or-namespace-name production in the grammar). For
6508 C++0x, it can also be a type-name that refers to an enumeration
6509 type or a simple-template-id.
6511 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6512 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6513 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6514 TYPE_P is TRUE iff the next name should be taken as a class-name,
6515 even the same name is declared to be another entity in the same
6516 scope.
6518 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6519 specified by the class-or-namespace-name. If neither is found the
6520 ERROR_MARK_NODE is returned. */
6522 static tree
6523 cp_parser_qualifying_entity (cp_parser *parser,
6524 bool typename_keyword_p,
6525 bool template_keyword_p,
6526 bool check_dependency_p,
6527 bool type_p,
6528 bool is_declaration)
6530 tree saved_scope;
6531 tree saved_qualifying_scope;
6532 tree saved_object_scope;
6533 tree scope;
6534 bool only_class_p;
6535 bool successful_parse_p;
6537 /* DR 743: decltype can appear in a nested-name-specifier. */
6538 if (cp_lexer_next_token_is_decltype (parser->lexer))
6540 scope = cp_parser_decltype (parser);
6541 if (TREE_CODE (scope) != ENUMERAL_TYPE
6542 && !MAYBE_CLASS_TYPE_P (scope))
6544 cp_parser_simulate_error (parser);
6545 return error_mark_node;
6547 if (TYPE_NAME (scope))
6548 scope = TYPE_NAME (scope);
6549 return scope;
6552 /* Before we try to parse the class-name, we must save away the
6553 current PARSER->SCOPE since cp_parser_class_name will destroy
6554 it. */
6555 saved_scope = parser->scope;
6556 saved_qualifying_scope = parser->qualifying_scope;
6557 saved_object_scope = parser->object_scope;
6558 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6559 there is no need to look for a namespace-name. */
6560 only_class_p = template_keyword_p
6561 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6562 if (!only_class_p)
6563 cp_parser_parse_tentatively (parser);
6564 scope = cp_parser_class_name (parser,
6565 typename_keyword_p,
6566 template_keyword_p,
6567 type_p ? class_type : none_type,
6568 check_dependency_p,
6569 /*class_head_p=*/false,
6570 is_declaration,
6571 /*enum_ok=*/cxx_dialect > cxx98);
6572 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6573 /* If that didn't work, try for a namespace-name. */
6574 if (!only_class_p && !successful_parse_p)
6576 /* Restore the saved scope. */
6577 parser->scope = saved_scope;
6578 parser->qualifying_scope = saved_qualifying_scope;
6579 parser->object_scope = saved_object_scope;
6580 /* If we are not looking at an identifier followed by the scope
6581 resolution operator, then this is not part of a
6582 nested-name-specifier. (Note that this function is only used
6583 to parse the components of a nested-name-specifier.) */
6584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6585 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6586 return error_mark_node;
6587 scope = cp_parser_namespace_name (parser);
6590 return scope;
6593 /* Return true if we are looking at a compound-literal, false otherwise. */
6595 static bool
6596 cp_parser_compound_literal_p (cp_parser *parser)
6598 cp_lexer_save_tokens (parser->lexer);
6600 /* Skip tokens until the next token is a closing parenthesis.
6601 If we find the closing `)', and the next token is a `{', then
6602 we are looking at a compound-literal. */
6603 bool compound_literal_p
6604 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6605 /*consume_paren=*/true)
6606 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6608 /* Roll back the tokens we skipped. */
6609 cp_lexer_rollback_tokens (parser->lexer);
6611 return compound_literal_p;
6614 /* Return true if EXPR is the integer constant zero or a complex constant
6615 of zero, without any folding, but ignoring location wrappers. */
6617 static bool
6618 literal_integer_zerop (const_tree expr)
6620 STRIP_ANY_LOCATION_WRAPPER (expr);
6621 return integer_zerop (expr);
6624 /* Parse a postfix-expression.
6626 postfix-expression:
6627 primary-expression
6628 postfix-expression [ expression ]
6629 postfix-expression ( expression-list [opt] )
6630 simple-type-specifier ( expression-list [opt] )
6631 typename :: [opt] nested-name-specifier identifier
6632 ( expression-list [opt] )
6633 typename :: [opt] nested-name-specifier template [opt] template-id
6634 ( expression-list [opt] )
6635 postfix-expression . template [opt] id-expression
6636 postfix-expression -> template [opt] id-expression
6637 postfix-expression . pseudo-destructor-name
6638 postfix-expression -> pseudo-destructor-name
6639 postfix-expression ++
6640 postfix-expression --
6641 dynamic_cast < type-id > ( expression )
6642 static_cast < type-id > ( expression )
6643 reinterpret_cast < type-id > ( expression )
6644 const_cast < type-id > ( expression )
6645 typeid ( expression )
6646 typeid ( type-id )
6648 GNU Extension:
6650 postfix-expression:
6651 ( type-id ) { initializer-list , [opt] }
6653 This extension is a GNU version of the C99 compound-literal
6654 construct. (The C99 grammar uses `type-name' instead of `type-id',
6655 but they are essentially the same concept.)
6657 If ADDRESS_P is true, the postfix expression is the operand of the
6658 `&' operator. CAST_P is true if this expression is the target of a
6659 cast.
6661 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6662 class member access expressions [expr.ref].
6664 Returns a representation of the expression. */
6666 static cp_expr
6667 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6668 bool member_access_only_p, bool decltype_p,
6669 cp_id_kind * pidk_return)
6671 cp_token *token;
6672 location_t loc;
6673 enum rid keyword;
6674 cp_id_kind idk = CP_ID_KIND_NONE;
6675 cp_expr postfix_expression = NULL_TREE;
6676 bool is_member_access = false;
6678 /* Peek at the next token. */
6679 token = cp_lexer_peek_token (parser->lexer);
6680 loc = token->location;
6681 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6683 /* Some of the productions are determined by keywords. */
6684 keyword = token->keyword;
6685 switch (keyword)
6687 case RID_DYNCAST:
6688 case RID_STATCAST:
6689 case RID_REINTCAST:
6690 case RID_CONSTCAST:
6692 tree type;
6693 cp_expr expression;
6694 const char *saved_message;
6695 bool saved_in_type_id_in_expr_p;
6697 /* All of these can be handled in the same way from the point
6698 of view of parsing. Begin by consuming the token
6699 identifying the cast. */
6700 cp_lexer_consume_token (parser->lexer);
6702 /* New types cannot be defined in the cast. */
6703 saved_message = parser->type_definition_forbidden_message;
6704 parser->type_definition_forbidden_message
6705 = G_("types may not be defined in casts");
6707 /* Look for the opening `<'. */
6708 cp_parser_require (parser, CPP_LESS, RT_LESS);
6709 /* Parse the type to which we are casting. */
6710 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6711 parser->in_type_id_in_expr_p = true;
6712 type = cp_parser_type_id (parser);
6713 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6714 /* Look for the closing `>'. */
6715 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6716 /* Restore the old message. */
6717 parser->type_definition_forbidden_message = saved_message;
6719 bool saved_greater_than_is_operator_p
6720 = parser->greater_than_is_operator_p;
6721 parser->greater_than_is_operator_p = true;
6723 /* And the expression which is being cast. */
6724 matching_parens parens;
6725 parens.require_open (parser);
6726 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6727 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6728 RT_CLOSE_PAREN);
6729 location_t end_loc = close_paren ?
6730 close_paren->location : UNKNOWN_LOCATION;
6732 parser->greater_than_is_operator_p
6733 = saved_greater_than_is_operator_p;
6735 /* Only type conversions to integral or enumeration types
6736 can be used in constant-expressions. */
6737 if (!cast_valid_in_integral_constant_expression_p (type)
6738 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6740 postfix_expression = error_mark_node;
6741 break;
6744 switch (keyword)
6746 case RID_DYNCAST:
6747 postfix_expression
6748 = build_dynamic_cast (type, expression, tf_warning_or_error);
6749 break;
6750 case RID_STATCAST:
6751 postfix_expression
6752 = build_static_cast (type, expression, tf_warning_or_error);
6753 break;
6754 case RID_REINTCAST:
6755 postfix_expression
6756 = build_reinterpret_cast (type, expression,
6757 tf_warning_or_error);
6758 break;
6759 case RID_CONSTCAST:
6760 postfix_expression
6761 = build_const_cast (type, expression, tf_warning_or_error);
6762 break;
6763 default:
6764 gcc_unreachable ();
6767 /* Construct a location e.g. :
6768 reinterpret_cast <int *> (expr)
6769 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6770 ranging from the start of the "*_cast" token to the final closing
6771 paren, with the caret at the start. */
6772 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6773 postfix_expression.set_location (cp_cast_loc);
6775 break;
6777 case RID_TYPEID:
6779 tree type;
6780 const char *saved_message;
6781 bool saved_in_type_id_in_expr_p;
6783 /* Consume the `typeid' token. */
6784 cp_lexer_consume_token (parser->lexer);
6785 /* Look for the `(' token. */
6786 matching_parens parens;
6787 parens.require_open (parser);
6788 /* Types cannot be defined in a `typeid' expression. */
6789 saved_message = parser->type_definition_forbidden_message;
6790 parser->type_definition_forbidden_message
6791 = G_("types may not be defined in a %<typeid%> expression");
6792 /* We can't be sure yet whether we're looking at a type-id or an
6793 expression. */
6794 cp_parser_parse_tentatively (parser);
6795 /* Try a type-id first. */
6796 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6797 parser->in_type_id_in_expr_p = true;
6798 type = cp_parser_type_id (parser);
6799 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6800 /* Look for the `)' token. Otherwise, we can't be sure that
6801 we're not looking at an expression: consider `typeid (int
6802 (3))', for example. */
6803 cp_token *close_paren = parens.require_close (parser);
6804 /* If all went well, simply lookup the type-id. */
6805 if (cp_parser_parse_definitely (parser))
6806 postfix_expression = get_typeid (type, tf_warning_or_error);
6807 /* Otherwise, fall back to the expression variant. */
6808 else
6810 tree expression;
6812 /* Look for an expression. */
6813 expression = cp_parser_expression (parser, & idk);
6814 /* Compute its typeid. */
6815 postfix_expression = build_typeid (expression, tf_warning_or_error);
6816 /* Look for the `)' token. */
6817 close_paren = parens.require_close (parser);
6819 /* Restore the saved message. */
6820 parser->type_definition_forbidden_message = saved_message;
6821 /* `typeid' may not appear in an integral constant expression. */
6822 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6823 postfix_expression = error_mark_node;
6825 /* Construct a location e.g. :
6826 typeid (expr)
6827 ^~~~~~~~~~~~~
6828 ranging from the start of the "typeid" token to the final closing
6829 paren, with the caret at the start. */
6830 if (close_paren)
6832 location_t typeid_loc
6833 = make_location (start_loc, start_loc, close_paren->location);
6834 postfix_expression.set_location (typeid_loc);
6835 postfix_expression.maybe_add_location_wrapper ();
6838 break;
6840 case RID_TYPENAME:
6842 tree type;
6843 /* The syntax permitted here is the same permitted for an
6844 elaborated-type-specifier. */
6845 ++parser->prevent_constrained_type_specifiers;
6846 type = cp_parser_elaborated_type_specifier (parser,
6847 /*is_friend=*/false,
6848 /*is_declaration=*/false);
6849 --parser->prevent_constrained_type_specifiers;
6850 postfix_expression = cp_parser_functional_cast (parser, type);
6852 break;
6854 case RID_ADDRESSOF:
6855 case RID_BUILTIN_SHUFFLE:
6856 case RID_BUILTIN_LAUNDER:
6858 vec<tree, va_gc> *vec;
6859 unsigned int i;
6860 tree p;
6862 cp_lexer_consume_token (parser->lexer);
6863 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6864 /*cast_p=*/false, /*allow_expansion_p=*/true,
6865 /*non_constant_p=*/NULL);
6866 if (vec == NULL)
6868 postfix_expression = error_mark_node;
6869 break;
6872 FOR_EACH_VEC_ELT (*vec, i, p)
6873 mark_exp_read (p);
6875 switch (keyword)
6877 case RID_ADDRESSOF:
6878 if (vec->length () == 1)
6879 postfix_expression
6880 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6881 else
6883 error_at (loc, "wrong number of arguments to "
6884 "%<__builtin_addressof%>");
6885 postfix_expression = error_mark_node;
6887 break;
6889 case RID_BUILTIN_LAUNDER:
6890 if (vec->length () == 1)
6891 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6892 tf_warning_or_error);
6893 else
6895 error_at (loc, "wrong number of arguments to "
6896 "%<__builtin_launder%>");
6897 postfix_expression = error_mark_node;
6899 break;
6901 case RID_BUILTIN_SHUFFLE:
6902 if (vec->length () == 2)
6903 postfix_expression
6904 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6905 (*vec)[1], tf_warning_or_error);
6906 else if (vec->length () == 3)
6907 postfix_expression
6908 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6909 (*vec)[2], tf_warning_or_error);
6910 else
6912 error_at (loc, "wrong number of arguments to "
6913 "%<__builtin_shuffle%>");
6914 postfix_expression = error_mark_node;
6916 break;
6918 default:
6919 gcc_unreachable ();
6921 break;
6924 default:
6926 tree type;
6928 /* If the next thing is a simple-type-specifier, we may be
6929 looking at a functional cast. We could also be looking at
6930 an id-expression. So, we try the functional cast, and if
6931 that doesn't work we fall back to the primary-expression. */
6932 cp_parser_parse_tentatively (parser);
6933 /* Look for the simple-type-specifier. */
6934 ++parser->prevent_constrained_type_specifiers;
6935 type = cp_parser_simple_type_specifier (parser,
6936 /*decl_specs=*/NULL,
6937 CP_PARSER_FLAGS_NONE);
6938 --parser->prevent_constrained_type_specifiers;
6939 /* Parse the cast itself. */
6940 if (!cp_parser_error_occurred (parser))
6941 postfix_expression
6942 = cp_parser_functional_cast (parser, type);
6943 /* If that worked, we're done. */
6944 if (cp_parser_parse_definitely (parser))
6945 break;
6947 /* If the functional-cast didn't work out, try a
6948 compound-literal. */
6949 if (cp_parser_allow_gnu_extensions_p (parser)
6950 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6952 cp_expr initializer = NULL_TREE;
6954 cp_parser_parse_tentatively (parser);
6956 matching_parens parens;
6957 parens.consume_open (parser);
6959 /* Avoid calling cp_parser_type_id pointlessly, see comment
6960 in cp_parser_cast_expression about c++/29234. */
6961 if (!cp_parser_compound_literal_p (parser))
6962 cp_parser_simulate_error (parser);
6963 else
6965 /* Parse the type. */
6966 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6967 parser->in_type_id_in_expr_p = true;
6968 type = cp_parser_type_id (parser);
6969 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6970 parens.require_close (parser);
6973 /* If things aren't going well, there's no need to
6974 keep going. */
6975 if (!cp_parser_error_occurred (parser))
6977 bool non_constant_p;
6978 /* Parse the brace-enclosed initializer list. */
6979 initializer = cp_parser_braced_list (parser,
6980 &non_constant_p);
6982 /* If that worked, we're definitely looking at a
6983 compound-literal expression. */
6984 if (cp_parser_parse_definitely (parser))
6986 /* Warn the user that a compound literal is not
6987 allowed in standard C++. */
6988 pedwarn (input_location, OPT_Wpedantic,
6989 "ISO C++ forbids compound-literals");
6990 /* For simplicity, we disallow compound literals in
6991 constant-expressions. We could
6992 allow compound literals of integer type, whose
6993 initializer was a constant, in constant
6994 expressions. Permitting that usage, as a further
6995 extension, would not change the meaning of any
6996 currently accepted programs. (Of course, as
6997 compound literals are not part of ISO C++, the
6998 standard has nothing to say.) */
6999 if (cp_parser_non_integral_constant_expression (parser,
7000 NIC_NCC))
7002 postfix_expression = error_mark_node;
7003 break;
7005 /* Form the representation of the compound-literal. */
7006 postfix_expression
7007 = finish_compound_literal (type, initializer,
7008 tf_warning_or_error, fcl_c99);
7009 postfix_expression.set_location (initializer.get_location ());
7010 break;
7014 /* It must be a primary-expression. */
7015 postfix_expression
7016 = cp_parser_primary_expression (parser, address_p, cast_p,
7017 /*template_arg_p=*/false,
7018 decltype_p,
7019 &idk);
7021 break;
7024 /* Note that we don't need to worry about calling build_cplus_new on a
7025 class-valued CALL_EXPR in decltype when it isn't the end of the
7026 postfix-expression; unary_complex_lvalue will take care of that for
7027 all these cases. */
7029 /* Keep looping until the postfix-expression is complete. */
7030 while (true)
7032 if (idk == CP_ID_KIND_UNQUALIFIED
7033 && identifier_p (postfix_expression)
7034 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7035 /* It is not a Koenig lookup function call. */
7036 postfix_expression
7037 = unqualified_name_lookup_error (postfix_expression);
7039 /* Peek at the next token. */
7040 token = cp_lexer_peek_token (parser->lexer);
7042 switch (token->type)
7044 case CPP_OPEN_SQUARE:
7045 if (cp_next_tokens_can_be_std_attribute_p (parser))
7047 cp_parser_error (parser,
7048 "two consecutive %<[%> shall "
7049 "only introduce an attribute");
7050 return error_mark_node;
7052 postfix_expression
7053 = cp_parser_postfix_open_square_expression (parser,
7054 postfix_expression,
7055 false,
7056 decltype_p);
7057 postfix_expression.set_range (start_loc,
7058 postfix_expression.get_location ());
7060 idk = CP_ID_KIND_NONE;
7061 is_member_access = false;
7062 break;
7064 case CPP_OPEN_PAREN:
7065 /* postfix-expression ( expression-list [opt] ) */
7067 bool koenig_p;
7068 bool is_builtin_constant_p;
7069 bool saved_integral_constant_expression_p = false;
7070 bool saved_non_integral_constant_expression_p = false;
7071 tsubst_flags_t complain = complain_flags (decltype_p);
7072 vec<tree, va_gc> *args;
7073 location_t close_paren_loc = UNKNOWN_LOCATION;
7075 is_member_access = false;
7077 is_builtin_constant_p
7078 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7079 if (is_builtin_constant_p)
7081 /* The whole point of __builtin_constant_p is to allow
7082 non-constant expressions to appear as arguments. */
7083 saved_integral_constant_expression_p
7084 = parser->integral_constant_expression_p;
7085 saved_non_integral_constant_expression_p
7086 = parser->non_integral_constant_expression_p;
7087 parser->integral_constant_expression_p = false;
7089 args = (cp_parser_parenthesized_expression_list
7090 (parser, non_attr,
7091 /*cast_p=*/false, /*allow_expansion_p=*/true,
7092 /*non_constant_p=*/NULL,
7093 /*close_paren_loc=*/&close_paren_loc,
7094 /*wrap_locations_p=*/true));
7095 if (is_builtin_constant_p)
7097 parser->integral_constant_expression_p
7098 = saved_integral_constant_expression_p;
7099 parser->non_integral_constant_expression_p
7100 = saved_non_integral_constant_expression_p;
7103 if (args == NULL)
7105 postfix_expression = error_mark_node;
7106 break;
7109 /* Function calls are not permitted in
7110 constant-expressions. */
7111 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7112 && cp_parser_non_integral_constant_expression (parser,
7113 NIC_FUNC_CALL))
7115 postfix_expression = error_mark_node;
7116 release_tree_vector (args);
7117 break;
7120 koenig_p = false;
7121 if (idk == CP_ID_KIND_UNQUALIFIED
7122 || idk == CP_ID_KIND_TEMPLATE_ID)
7124 if (identifier_p (postfix_expression))
7126 if (!args->is_empty ())
7128 koenig_p = true;
7129 if (!any_type_dependent_arguments_p (args))
7130 postfix_expression
7131 = perform_koenig_lookup (postfix_expression, args,
7132 complain);
7134 else
7135 postfix_expression
7136 = unqualified_fn_lookup_error (postfix_expression);
7138 /* We do not perform argument-dependent lookup if
7139 normal lookup finds a non-function, in accordance
7140 with the expected resolution of DR 218. */
7141 else if (!args->is_empty ()
7142 && is_overloaded_fn (postfix_expression))
7144 tree fn = get_first_fn (postfix_expression);
7145 fn = STRIP_TEMPLATE (fn);
7147 /* Do not do argument dependent lookup if regular
7148 lookup finds a member function or a block-scope
7149 function declaration. [basic.lookup.argdep]/3 */
7150 if (!DECL_FUNCTION_MEMBER_P (fn)
7151 && !DECL_LOCAL_FUNCTION_P (fn))
7153 koenig_p = true;
7154 if (!any_type_dependent_arguments_p (args))
7155 postfix_expression
7156 = perform_koenig_lookup (postfix_expression, args,
7157 complain);
7162 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7163 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7164 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7165 && vec_safe_length (args) == 3)
7167 tree arg0 = (*args)[0];
7168 tree arg1 = (*args)[1];
7169 tree arg2 = (*args)[2];
7170 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7171 | (literal_integer_zerop (arg2) << 2));
7172 warn_for_memset (input_location, arg0, arg2, literal_mask);
7175 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7177 tree instance = TREE_OPERAND (postfix_expression, 0);
7178 tree fn = TREE_OPERAND (postfix_expression, 1);
7180 if (processing_template_decl
7181 && (type_dependent_object_expression_p (instance)
7182 || (!BASELINK_P (fn)
7183 && TREE_CODE (fn) != FIELD_DECL)
7184 || type_dependent_expression_p (fn)
7185 || any_type_dependent_arguments_p (args)))
7187 maybe_generic_this_capture (instance, fn);
7188 postfix_expression
7189 = build_min_nt_call_vec (postfix_expression, args);
7190 release_tree_vector (args);
7191 break;
7194 if (BASELINK_P (fn))
7196 postfix_expression
7197 = (build_new_method_call
7198 (instance, fn, &args, NULL_TREE,
7199 (idk == CP_ID_KIND_QUALIFIED
7200 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7201 : LOOKUP_NORMAL),
7202 /*fn_p=*/NULL,
7203 complain));
7205 else
7206 postfix_expression
7207 = finish_call_expr (postfix_expression, &args,
7208 /*disallow_virtual=*/false,
7209 /*koenig_p=*/false,
7210 complain);
7212 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7213 || TREE_CODE (postfix_expression) == MEMBER_REF
7214 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7215 postfix_expression = (build_offset_ref_call_from_tree
7216 (postfix_expression, &args,
7217 complain));
7218 else if (idk == CP_ID_KIND_QUALIFIED)
7219 /* A call to a static class member, or a namespace-scope
7220 function. */
7221 postfix_expression
7222 = finish_call_expr (postfix_expression, &args,
7223 /*disallow_virtual=*/true,
7224 koenig_p,
7225 complain);
7226 else
7227 /* All other function calls. */
7228 postfix_expression
7229 = finish_call_expr (postfix_expression, &args,
7230 /*disallow_virtual=*/false,
7231 koenig_p,
7232 complain);
7234 if (close_paren_loc != UNKNOWN_LOCATION)
7236 location_t combined_loc = make_location (token->location,
7237 start_loc,
7238 close_paren_loc);
7239 postfix_expression.set_location (combined_loc);
7242 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7243 idk = CP_ID_KIND_NONE;
7245 release_tree_vector (args);
7247 break;
7249 case CPP_DOT:
7250 case CPP_DEREF:
7251 /* postfix-expression . template [opt] id-expression
7252 postfix-expression . pseudo-destructor-name
7253 postfix-expression -> template [opt] id-expression
7254 postfix-expression -> pseudo-destructor-name */
7256 /* Consume the `.' or `->' operator. */
7257 cp_lexer_consume_token (parser->lexer);
7259 postfix_expression
7260 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7261 postfix_expression,
7262 false, &idk, loc);
7264 is_member_access = true;
7265 break;
7267 case CPP_PLUS_PLUS:
7268 /* postfix-expression ++ */
7269 /* Consume the `++' token. */
7270 cp_lexer_consume_token (parser->lexer);
7271 /* Generate a representation for the complete expression. */
7272 postfix_expression
7273 = finish_increment_expr (postfix_expression,
7274 POSTINCREMENT_EXPR);
7275 /* Increments may not appear in constant-expressions. */
7276 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7277 postfix_expression = error_mark_node;
7278 idk = CP_ID_KIND_NONE;
7279 is_member_access = false;
7280 break;
7282 case CPP_MINUS_MINUS:
7283 /* postfix-expression -- */
7284 /* Consume the `--' token. */
7285 cp_lexer_consume_token (parser->lexer);
7286 /* Generate a representation for the complete expression. */
7287 postfix_expression
7288 = finish_increment_expr (postfix_expression,
7289 POSTDECREMENT_EXPR);
7290 /* Decrements may not appear in constant-expressions. */
7291 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7292 postfix_expression = error_mark_node;
7293 idk = CP_ID_KIND_NONE;
7294 is_member_access = false;
7295 break;
7297 default:
7298 if (pidk_return != NULL)
7299 * pidk_return = idk;
7300 if (member_access_only_p)
7301 return is_member_access
7302 ? postfix_expression
7303 : cp_expr (error_mark_node);
7304 else
7305 return postfix_expression;
7309 /* We should never get here. */
7310 gcc_unreachable ();
7311 return error_mark_node;
7314 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7315 by cp_parser_builtin_offsetof. We're looking for
7317 postfix-expression [ expression ]
7318 postfix-expression [ braced-init-list ] (C++11)
7320 FOR_OFFSETOF is set if we're being called in that context, which
7321 changes how we deal with integer constant expressions. */
7323 static tree
7324 cp_parser_postfix_open_square_expression (cp_parser *parser,
7325 tree postfix_expression,
7326 bool for_offsetof,
7327 bool decltype_p)
7329 tree index = NULL_TREE;
7330 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7331 bool saved_greater_than_is_operator_p;
7333 /* Consume the `[' token. */
7334 cp_lexer_consume_token (parser->lexer);
7336 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7337 parser->greater_than_is_operator_p = true;
7339 /* Parse the index expression. */
7340 /* ??? For offsetof, there is a question of what to allow here. If
7341 offsetof is not being used in an integral constant expression context,
7342 then we *could* get the right answer by computing the value at runtime.
7343 If we are in an integral constant expression context, then we might
7344 could accept any constant expression; hard to say without analysis.
7345 Rather than open the barn door too wide right away, allow only integer
7346 constant expressions here. */
7347 if (for_offsetof)
7348 index = cp_parser_constant_expression (parser);
7349 else
7351 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7353 bool expr_nonconst_p;
7354 cp_lexer_set_source_position (parser->lexer);
7355 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7356 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7358 else
7359 index = cp_parser_expression (parser);
7362 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7364 /* Look for the closing `]'. */
7365 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7367 /* Build the ARRAY_REF. */
7368 postfix_expression = grok_array_decl (loc, postfix_expression,
7369 index, decltype_p);
7371 /* When not doing offsetof, array references are not permitted in
7372 constant-expressions. */
7373 if (!for_offsetof
7374 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7375 postfix_expression = error_mark_node;
7377 return postfix_expression;
7380 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7381 dereference of incomplete type, returns true if error_mark_node should
7382 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7383 and *DEPENDENT_P. */
7385 bool
7386 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7387 bool *dependent_p)
7389 /* In a template, be permissive by treating an object expression
7390 of incomplete type as dependent (after a pedwarn). */
7391 diagnostic_t kind = (processing_template_decl
7392 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7394 switch (TREE_CODE (*postfix_expression))
7396 case CAST_EXPR:
7397 case REINTERPRET_CAST_EXPR:
7398 case CONST_CAST_EXPR:
7399 case STATIC_CAST_EXPR:
7400 case DYNAMIC_CAST_EXPR:
7401 case IMPLICIT_CONV_EXPR:
7402 case VIEW_CONVERT_EXPR:
7403 case NON_LVALUE_EXPR:
7404 kind = DK_ERROR;
7405 break;
7406 case OVERLOAD:
7407 /* Don't emit any diagnostic for OVERLOADs. */
7408 kind = DK_IGNORED;
7409 break;
7410 default:
7411 /* Avoid clobbering e.g. DECLs. */
7412 if (!EXPR_P (*postfix_expression))
7413 kind = DK_ERROR;
7414 break;
7417 if (kind == DK_IGNORED)
7418 return false;
7420 location_t exploc = location_of (*postfix_expression);
7421 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7422 if (!MAYBE_CLASS_TYPE_P (*scope))
7423 return true;
7424 if (kind == DK_ERROR)
7425 *scope = *postfix_expression = error_mark_node;
7426 else if (processing_template_decl)
7428 *dependent_p = true;
7429 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7431 return false;
7434 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7435 by cp_parser_builtin_offsetof. We're looking for
7437 postfix-expression . template [opt] id-expression
7438 postfix-expression . pseudo-destructor-name
7439 postfix-expression -> template [opt] id-expression
7440 postfix-expression -> pseudo-destructor-name
7442 FOR_OFFSETOF is set if we're being called in that context. That sorta
7443 limits what of the above we'll actually accept, but nevermind.
7444 TOKEN_TYPE is the "." or "->" token, which will already have been
7445 removed from the stream. */
7447 static tree
7448 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7449 enum cpp_ttype token_type,
7450 cp_expr postfix_expression,
7451 bool for_offsetof, cp_id_kind *idk,
7452 location_t location)
7454 tree name;
7455 bool dependent_p;
7456 bool pseudo_destructor_p;
7457 tree scope = NULL_TREE;
7458 location_t start_loc = postfix_expression.get_start ();
7460 /* If this is a `->' operator, dereference the pointer. */
7461 if (token_type == CPP_DEREF)
7462 postfix_expression = build_x_arrow (location, postfix_expression,
7463 tf_warning_or_error);
7464 /* Check to see whether or not the expression is type-dependent and
7465 not the current instantiation. */
7466 dependent_p = type_dependent_object_expression_p (postfix_expression);
7467 /* The identifier following the `->' or `.' is not qualified. */
7468 parser->scope = NULL_TREE;
7469 parser->qualifying_scope = NULL_TREE;
7470 parser->object_scope = NULL_TREE;
7471 *idk = CP_ID_KIND_NONE;
7473 /* Enter the scope corresponding to the type of the object
7474 given by the POSTFIX_EXPRESSION. */
7475 if (!dependent_p)
7477 scope = TREE_TYPE (postfix_expression);
7478 /* According to the standard, no expression should ever have
7479 reference type. Unfortunately, we do not currently match
7480 the standard in this respect in that our internal representation
7481 of an expression may have reference type even when the standard
7482 says it does not. Therefore, we have to manually obtain the
7483 underlying type here. */
7484 scope = non_reference (scope);
7485 /* The type of the POSTFIX_EXPRESSION must be complete. */
7486 /* Unlike the object expression in other contexts, *this is not
7487 required to be of complete type for purposes of class member
7488 access (5.2.5) outside the member function body. */
7489 if (postfix_expression != current_class_ref
7490 && scope != error_mark_node
7491 && !currently_open_class (scope))
7493 scope = complete_type (scope);
7494 if (!COMPLETE_TYPE_P (scope)
7495 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7496 &dependent_p))
7497 return error_mark_node;
7500 if (!dependent_p)
7502 /* Let the name lookup machinery know that we are processing a
7503 class member access expression. */
7504 parser->context->object_type = scope;
7505 /* If something went wrong, we want to be able to discern that case,
7506 as opposed to the case where there was no SCOPE due to the type
7507 of expression being dependent. */
7508 if (!scope)
7509 scope = error_mark_node;
7510 /* If the SCOPE was erroneous, make the various semantic analysis
7511 functions exit quickly -- and without issuing additional error
7512 messages. */
7513 if (scope == error_mark_node)
7514 postfix_expression = error_mark_node;
7518 if (dependent_p)
7519 /* Tell cp_parser_lookup_name that there was an object, even though it's
7520 type-dependent. */
7521 parser->context->object_type = unknown_type_node;
7523 /* Assume this expression is not a pseudo-destructor access. */
7524 pseudo_destructor_p = false;
7526 /* If the SCOPE is a scalar type, then, if this is a valid program,
7527 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7528 is type dependent, it can be pseudo-destructor-name or something else.
7529 Try to parse it as pseudo-destructor-name first. */
7530 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7532 tree s;
7533 tree type;
7535 cp_parser_parse_tentatively (parser);
7536 /* Parse the pseudo-destructor-name. */
7537 s = NULL_TREE;
7538 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7539 &s, &type);
7540 if (dependent_p
7541 && (cp_parser_error_occurred (parser)
7542 || !SCALAR_TYPE_P (type)))
7543 cp_parser_abort_tentative_parse (parser);
7544 else if (cp_parser_parse_definitely (parser))
7546 pseudo_destructor_p = true;
7547 postfix_expression
7548 = finish_pseudo_destructor_expr (postfix_expression,
7549 s, type, location);
7553 if (!pseudo_destructor_p)
7555 /* If the SCOPE is not a scalar type, we are looking at an
7556 ordinary class member access expression, rather than a
7557 pseudo-destructor-name. */
7558 bool template_p;
7559 cp_token *token = cp_lexer_peek_token (parser->lexer);
7560 /* Parse the id-expression. */
7561 name = (cp_parser_id_expression
7562 (parser,
7563 cp_parser_optional_template_keyword (parser),
7564 /*check_dependency_p=*/true,
7565 &template_p,
7566 /*declarator_p=*/false,
7567 /*optional_p=*/false));
7568 /* In general, build a SCOPE_REF if the member name is qualified.
7569 However, if the name was not dependent and has already been
7570 resolved; there is no need to build the SCOPE_REF. For example;
7572 struct X { void f(); };
7573 template <typename T> void f(T* t) { t->X::f(); }
7575 Even though "t" is dependent, "X::f" is not and has been resolved
7576 to a BASELINK; there is no need to include scope information. */
7578 /* But we do need to remember that there was an explicit scope for
7579 virtual function calls. */
7580 if (parser->scope)
7581 *idk = CP_ID_KIND_QUALIFIED;
7583 /* If the name is a template-id that names a type, we will get a
7584 TYPE_DECL here. That is invalid code. */
7585 if (TREE_CODE (name) == TYPE_DECL)
7587 error_at (token->location, "invalid use of %qD", name);
7588 postfix_expression = error_mark_node;
7590 else
7592 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7594 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7596 error_at (token->location, "%<%D::%D%> is not a class member",
7597 parser->scope, name);
7598 postfix_expression = error_mark_node;
7600 else
7601 name = build_qualified_name (/*type=*/NULL_TREE,
7602 parser->scope,
7603 name,
7604 template_p);
7605 parser->scope = NULL_TREE;
7606 parser->qualifying_scope = NULL_TREE;
7607 parser->object_scope = NULL_TREE;
7609 if (parser->scope && name && BASELINK_P (name))
7610 adjust_result_of_qualified_name_lookup
7611 (name, parser->scope, scope);
7612 postfix_expression
7613 = finish_class_member_access_expr (postfix_expression, name,
7614 template_p,
7615 tf_warning_or_error);
7616 /* Build a location e.g.:
7617 ptr->access_expr
7618 ~~~^~~~~~~~~~~~~
7619 where the caret is at the deref token, ranging from
7620 the start of postfix_expression to the end of the access expr. */
7621 location_t end_loc
7622 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7623 location_t combined_loc
7624 = make_location (input_location, start_loc, end_loc);
7625 protected_set_expr_location (postfix_expression, combined_loc);
7629 /* We no longer need to look up names in the scope of the object on
7630 the left-hand side of the `.' or `->' operator. */
7631 parser->context->object_type = NULL_TREE;
7633 /* Outside of offsetof, these operators may not appear in
7634 constant-expressions. */
7635 if (!for_offsetof
7636 && (cp_parser_non_integral_constant_expression
7637 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7638 postfix_expression = error_mark_node;
7640 return postfix_expression;
7643 /* Parse a parenthesized expression-list.
7645 expression-list:
7646 assignment-expression
7647 expression-list, assignment-expression
7649 attribute-list:
7650 expression-list
7651 identifier
7652 identifier, expression-list
7654 CAST_P is true if this expression is the target of a cast.
7656 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7657 argument pack.
7659 WRAP_LOCATIONS_P is true if expressions within this list for which
7660 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7661 their source locations.
7663 Returns a vector of trees. Each element is a representation of an
7664 assignment-expression. NULL is returned if the ( and or ) are
7665 missing. An empty, but allocated, vector is returned on no
7666 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7667 if we are parsing an attribute list for an attribute that wants a
7668 plain identifier argument, normal_attr for an attribute that wants
7669 an expression, or non_attr if we aren't parsing an attribute list. If
7670 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7671 not all of the expressions in the list were constant.
7672 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7673 will be written to with the location of the closing parenthesis. If
7674 an error occurs, it may or may not be written to. */
7676 static vec<tree, va_gc> *
7677 cp_parser_parenthesized_expression_list (cp_parser* parser,
7678 int is_attribute_list,
7679 bool cast_p,
7680 bool allow_expansion_p,
7681 bool *non_constant_p,
7682 location_t *close_paren_loc,
7683 bool wrap_locations_p)
7685 vec<tree, va_gc> *expression_list;
7686 bool fold_expr_p = is_attribute_list != non_attr;
7687 tree identifier = NULL_TREE;
7688 bool saved_greater_than_is_operator_p;
7690 /* Assume all the expressions will be constant. */
7691 if (non_constant_p)
7692 *non_constant_p = false;
7694 matching_parens parens;
7695 if (!parens.require_open (parser))
7696 return NULL;
7698 expression_list = make_tree_vector ();
7700 /* Within a parenthesized expression, a `>' token is always
7701 the greater-than operator. */
7702 saved_greater_than_is_operator_p
7703 = parser->greater_than_is_operator_p;
7704 parser->greater_than_is_operator_p = true;
7706 cp_expr expr (NULL_TREE);
7708 /* Consume expressions until there are no more. */
7709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7710 while (true)
7712 /* At the beginning of attribute lists, check to see if the
7713 next token is an identifier. */
7714 if (is_attribute_list == id_attr
7715 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7717 cp_token *token;
7719 /* Consume the identifier. */
7720 token = cp_lexer_consume_token (parser->lexer);
7721 /* Save the identifier. */
7722 identifier = token->u.value;
7724 else
7726 bool expr_non_constant_p;
7728 /* Parse the next assignment-expression. */
7729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7731 /* A braced-init-list. */
7732 cp_lexer_set_source_position (parser->lexer);
7733 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7734 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7735 if (non_constant_p && expr_non_constant_p)
7736 *non_constant_p = true;
7738 else if (non_constant_p)
7740 expr = (cp_parser_constant_expression
7741 (parser, /*allow_non_constant_p=*/true,
7742 &expr_non_constant_p));
7743 if (expr_non_constant_p)
7744 *non_constant_p = true;
7746 else
7747 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7748 cast_p);
7750 if (fold_expr_p)
7751 expr = instantiate_non_dependent_expr (expr);
7753 /* If we have an ellipsis, then this is an expression
7754 expansion. */
7755 if (allow_expansion_p
7756 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7758 /* Consume the `...'. */
7759 cp_lexer_consume_token (parser->lexer);
7761 /* Build the argument pack. */
7762 expr = make_pack_expansion (expr);
7765 if (wrap_locations_p)
7766 expr.maybe_add_location_wrapper ();
7768 /* Add it to the list. We add error_mark_node
7769 expressions to the list, so that we can still tell if
7770 the correct form for a parenthesized expression-list
7771 is found. That gives better errors. */
7772 vec_safe_push (expression_list, expr.get_value ());
7774 if (expr == error_mark_node)
7775 goto skip_comma;
7778 /* After the first item, attribute lists look the same as
7779 expression lists. */
7780 is_attribute_list = non_attr;
7782 get_comma:;
7783 /* If the next token isn't a `,', then we are done. */
7784 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7785 break;
7787 /* Otherwise, consume the `,' and keep going. */
7788 cp_lexer_consume_token (parser->lexer);
7791 if (close_paren_loc)
7792 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7794 if (!parens.require_close (parser))
7796 int ending;
7798 skip_comma:;
7799 /* We try and resync to an unnested comma, as that will give the
7800 user better diagnostics. */
7801 ending = cp_parser_skip_to_closing_parenthesis (parser,
7802 /*recovering=*/true,
7803 /*or_comma=*/true,
7804 /*consume_paren=*/true);
7805 if (ending < 0)
7806 goto get_comma;
7807 if (!ending)
7809 parser->greater_than_is_operator_p
7810 = saved_greater_than_is_operator_p;
7811 return NULL;
7815 parser->greater_than_is_operator_p
7816 = saved_greater_than_is_operator_p;
7818 if (identifier)
7819 vec_safe_insert (expression_list, 0, identifier);
7821 return expression_list;
7824 /* Parse a pseudo-destructor-name.
7826 pseudo-destructor-name:
7827 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7828 :: [opt] nested-name-specifier template template-id :: ~ type-name
7829 :: [opt] nested-name-specifier [opt] ~ type-name
7831 If either of the first two productions is used, sets *SCOPE to the
7832 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7833 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7834 or ERROR_MARK_NODE if the parse fails. */
7836 static void
7837 cp_parser_pseudo_destructor_name (cp_parser* parser,
7838 tree object,
7839 tree* scope,
7840 tree* type)
7842 bool nested_name_specifier_p;
7844 /* Handle ~auto. */
7845 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7846 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7847 && !type_dependent_expression_p (object))
7849 if (cxx_dialect < cxx14)
7850 pedwarn (input_location, 0,
7851 "%<~auto%> only available with "
7852 "-std=c++14 or -std=gnu++14");
7853 cp_lexer_consume_token (parser->lexer);
7854 cp_lexer_consume_token (parser->lexer);
7855 *scope = NULL_TREE;
7856 *type = TREE_TYPE (object);
7857 return;
7860 /* Assume that things will not work out. */
7861 *type = error_mark_node;
7863 /* Look for the optional `::' operator. */
7864 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7865 /* Look for the optional nested-name-specifier. */
7866 nested_name_specifier_p
7867 = (cp_parser_nested_name_specifier_opt (parser,
7868 /*typename_keyword_p=*/false,
7869 /*check_dependency_p=*/true,
7870 /*type_p=*/false,
7871 /*is_declaration=*/false)
7872 != NULL_TREE);
7873 /* Now, if we saw a nested-name-specifier, we might be doing the
7874 second production. */
7875 if (nested_name_specifier_p
7876 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7878 /* Consume the `template' keyword. */
7879 cp_lexer_consume_token (parser->lexer);
7880 /* Parse the template-id. */
7881 cp_parser_template_id (parser,
7882 /*template_keyword_p=*/true,
7883 /*check_dependency_p=*/false,
7884 class_type,
7885 /*is_declaration=*/true);
7886 /* Look for the `::' token. */
7887 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7889 /* If the next token is not a `~', then there might be some
7890 additional qualification. */
7891 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7893 /* At this point, we're looking for "type-name :: ~". The type-name
7894 must not be a class-name, since this is a pseudo-destructor. So,
7895 it must be either an enum-name, or a typedef-name -- both of which
7896 are just identifiers. So, we peek ahead to check that the "::"
7897 and "~" tokens are present; if they are not, then we can avoid
7898 calling type_name. */
7899 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7900 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7901 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7903 cp_parser_error (parser, "non-scalar type");
7904 return;
7907 /* Look for the type-name. */
7908 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7909 if (*scope == error_mark_node)
7910 return;
7912 /* Look for the `::' token. */
7913 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7915 else
7916 *scope = NULL_TREE;
7918 /* Look for the `~'. */
7919 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7921 /* Once we see the ~, this has to be a pseudo-destructor. */
7922 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7923 cp_parser_commit_to_topmost_tentative_parse (parser);
7925 /* Look for the type-name again. We are not responsible for
7926 checking that it matches the first type-name. */
7927 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7930 /* Parse a unary-expression.
7932 unary-expression:
7933 postfix-expression
7934 ++ cast-expression
7935 -- cast-expression
7936 unary-operator cast-expression
7937 sizeof unary-expression
7938 sizeof ( type-id )
7939 alignof ( type-id ) [C++0x]
7940 new-expression
7941 delete-expression
7943 GNU Extensions:
7945 unary-expression:
7946 __extension__ cast-expression
7947 __alignof__ unary-expression
7948 __alignof__ ( type-id )
7949 alignof unary-expression [C++0x]
7950 __real__ cast-expression
7951 __imag__ cast-expression
7952 && identifier
7953 sizeof ( type-id ) { initializer-list , [opt] }
7954 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7955 __alignof__ ( type-id ) { initializer-list , [opt] }
7957 ADDRESS_P is true iff the unary-expression is appearing as the
7958 operand of the `&' operator. CAST_P is true if this expression is
7959 the target of a cast.
7961 Returns a representation of the expression. */
7963 static cp_expr
7964 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7965 bool address_p, bool cast_p, bool decltype_p)
7967 cp_token *token;
7968 enum tree_code unary_operator;
7970 /* Peek at the next token. */
7971 token = cp_lexer_peek_token (parser->lexer);
7972 /* Some keywords give away the kind of expression. */
7973 if (token->type == CPP_KEYWORD)
7975 enum rid keyword = token->keyword;
7977 switch (keyword)
7979 case RID_ALIGNOF:
7980 case RID_SIZEOF:
7982 tree operand, ret;
7983 enum tree_code op;
7984 location_t start_loc = token->location;
7986 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7987 bool std_alignof = id_equal (token->u.value, "alignof");
7989 /* Consume the token. */
7990 cp_lexer_consume_token (parser->lexer);
7991 /* Parse the operand. */
7992 operand = cp_parser_sizeof_operand (parser, keyword);
7994 if (TYPE_P (operand))
7995 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
7996 true);
7997 else
7999 /* ISO C++ defines alignof only with types, not with
8000 expressions. So pedwarn if alignof is used with a non-
8001 type expression. However, __alignof__ is ok. */
8002 if (std_alignof)
8003 pedwarn (token->location, OPT_Wpedantic,
8004 "ISO C++ does not allow %<alignof%> "
8005 "with a non-type");
8007 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8009 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8010 SIZEOF_EXPR with the original operand. */
8011 if (op == SIZEOF_EXPR && ret != error_mark_node)
8013 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8015 if (!processing_template_decl && TYPE_P (operand))
8017 ret = build_min (SIZEOF_EXPR, size_type_node,
8018 build1 (NOP_EXPR, operand,
8019 error_mark_node));
8020 SIZEOF_EXPR_TYPE_P (ret) = 1;
8022 else
8023 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8024 TREE_SIDE_EFFECTS (ret) = 0;
8025 TREE_READONLY (ret) = 1;
8029 /* Construct a location e.g. :
8030 alignof (expr)
8031 ^~~~~~~~~~~~~~
8032 with start == caret at the start of the "alignof"/"sizeof"
8033 token, with the endpoint at the final closing paren. */
8034 location_t finish_loc
8035 = cp_lexer_previous_token (parser->lexer)->location;
8036 location_t compound_loc
8037 = make_location (start_loc, start_loc, finish_loc);
8039 cp_expr ret_expr (ret);
8040 ret_expr.set_location (compound_loc);
8041 ret_expr = ret_expr.maybe_add_location_wrapper ();
8042 return ret_expr;
8045 case RID_NEW:
8046 return cp_parser_new_expression (parser);
8048 case RID_DELETE:
8049 return cp_parser_delete_expression (parser);
8051 case RID_EXTENSION:
8053 /* The saved value of the PEDANTIC flag. */
8054 int saved_pedantic;
8055 tree expr;
8057 /* Save away the PEDANTIC flag. */
8058 cp_parser_extension_opt (parser, &saved_pedantic);
8059 /* Parse the cast-expression. */
8060 expr = cp_parser_simple_cast_expression (parser);
8061 /* Restore the PEDANTIC flag. */
8062 pedantic = saved_pedantic;
8064 return expr;
8067 case RID_REALPART:
8068 case RID_IMAGPART:
8070 tree expression;
8072 /* Consume the `__real__' or `__imag__' token. */
8073 cp_lexer_consume_token (parser->lexer);
8074 /* Parse the cast-expression. */
8075 expression = cp_parser_simple_cast_expression (parser);
8076 /* Create the complete representation. */
8077 return build_x_unary_op (token->location,
8078 (keyword == RID_REALPART
8079 ? REALPART_EXPR : IMAGPART_EXPR),
8080 expression,
8081 tf_warning_or_error);
8083 break;
8085 case RID_TRANSACTION_ATOMIC:
8086 case RID_TRANSACTION_RELAXED:
8087 return cp_parser_transaction_expression (parser, keyword);
8089 case RID_NOEXCEPT:
8091 tree expr;
8092 const char *saved_message;
8093 bool saved_integral_constant_expression_p;
8094 bool saved_non_integral_constant_expression_p;
8095 bool saved_greater_than_is_operator_p;
8097 location_t start_loc = token->location;
8099 cp_lexer_consume_token (parser->lexer);
8100 matching_parens parens;
8101 parens.require_open (parser);
8103 saved_message = parser->type_definition_forbidden_message;
8104 parser->type_definition_forbidden_message
8105 = G_("types may not be defined in %<noexcept%> expressions");
8107 saved_integral_constant_expression_p
8108 = parser->integral_constant_expression_p;
8109 saved_non_integral_constant_expression_p
8110 = parser->non_integral_constant_expression_p;
8111 parser->integral_constant_expression_p = false;
8113 saved_greater_than_is_operator_p
8114 = parser->greater_than_is_operator_p;
8115 parser->greater_than_is_operator_p = true;
8117 ++cp_unevaluated_operand;
8118 ++c_inhibit_evaluation_warnings;
8119 ++cp_noexcept_operand;
8120 expr = cp_parser_expression (parser);
8121 --cp_noexcept_operand;
8122 --c_inhibit_evaluation_warnings;
8123 --cp_unevaluated_operand;
8125 parser->greater_than_is_operator_p
8126 = saved_greater_than_is_operator_p;
8128 parser->integral_constant_expression_p
8129 = saved_integral_constant_expression_p;
8130 parser->non_integral_constant_expression_p
8131 = saved_non_integral_constant_expression_p;
8133 parser->type_definition_forbidden_message = saved_message;
8135 location_t finish_loc
8136 = cp_lexer_peek_token (parser->lexer)->location;
8137 parens.require_close (parser);
8139 /* Construct a location of the form:
8140 noexcept (expr)
8141 ^~~~~~~~~~~~~~~
8142 with start == caret, finishing at the close-paren. */
8143 location_t noexcept_loc
8144 = make_location (start_loc, start_loc, finish_loc);
8146 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8147 noexcept_loc);
8150 default:
8151 break;
8155 /* Look for the `:: new' and `:: delete', which also signal the
8156 beginning of a new-expression, or delete-expression,
8157 respectively. If the next token is `::', then it might be one of
8158 these. */
8159 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8161 enum rid keyword;
8163 /* See if the token after the `::' is one of the keywords in
8164 which we're interested. */
8165 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8166 /* If it's `new', we have a new-expression. */
8167 if (keyword == RID_NEW)
8168 return cp_parser_new_expression (parser);
8169 /* Similarly, for `delete'. */
8170 else if (keyword == RID_DELETE)
8171 return cp_parser_delete_expression (parser);
8174 /* Look for a unary operator. */
8175 unary_operator = cp_parser_unary_operator (token);
8176 /* The `++' and `--' operators can be handled similarly, even though
8177 they are not technically unary-operators in the grammar. */
8178 if (unary_operator == ERROR_MARK)
8180 if (token->type == CPP_PLUS_PLUS)
8181 unary_operator = PREINCREMENT_EXPR;
8182 else if (token->type == CPP_MINUS_MINUS)
8183 unary_operator = PREDECREMENT_EXPR;
8184 /* Handle the GNU address-of-label extension. */
8185 else if (cp_parser_allow_gnu_extensions_p (parser)
8186 && token->type == CPP_AND_AND)
8188 tree identifier;
8189 tree expression;
8190 location_t start_loc = token->location;
8192 /* Consume the '&&' token. */
8193 cp_lexer_consume_token (parser->lexer);
8194 /* Look for the identifier. */
8195 location_t finish_loc
8196 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8197 identifier = cp_parser_identifier (parser);
8198 /* Construct a location of the form:
8199 &&label
8200 ^~~~~~~
8201 with caret==start at the "&&", finish at the end of the label. */
8202 location_t combined_loc
8203 = make_location (start_loc, start_loc, finish_loc);
8204 /* Create an expression representing the address. */
8205 expression = finish_label_address_expr (identifier, combined_loc);
8206 if (cp_parser_non_integral_constant_expression (parser,
8207 NIC_ADDR_LABEL))
8208 expression = error_mark_node;
8209 return expression;
8212 if (unary_operator != ERROR_MARK)
8214 cp_expr cast_expression;
8215 cp_expr expression = error_mark_node;
8216 non_integral_constant non_constant_p = NIC_NONE;
8217 location_t loc = token->location;
8218 tsubst_flags_t complain = complain_flags (decltype_p);
8220 /* Consume the operator token. */
8221 token = cp_lexer_consume_token (parser->lexer);
8222 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8224 /* Parse the cast-expression. */
8225 cast_expression
8226 = cp_parser_cast_expression (parser,
8227 unary_operator == ADDR_EXPR,
8228 /*cast_p=*/false,
8229 /*decltype*/false,
8230 pidk);
8232 /* Make a location:
8233 OP_TOKEN CAST_EXPRESSION
8234 ^~~~~~~~~~~~~~~~~~~~~~~~~
8235 with start==caret at the operator token, and
8236 extending to the end of the cast_expression. */
8237 loc = make_location (loc, loc, cast_expression.get_finish ());
8239 /* Now, build an appropriate representation. */
8240 switch (unary_operator)
8242 case INDIRECT_REF:
8243 non_constant_p = NIC_STAR;
8244 expression = build_x_indirect_ref (loc, cast_expression,
8245 RO_UNARY_STAR,
8246 complain);
8247 /* TODO: build_x_indirect_ref does not always honor the
8248 location, so ensure it is set. */
8249 expression.set_location (loc);
8250 break;
8252 case ADDR_EXPR:
8253 non_constant_p = NIC_ADDR;
8254 /* Fall through. */
8255 case BIT_NOT_EXPR:
8256 expression = build_x_unary_op (loc, unary_operator,
8257 cast_expression,
8258 complain);
8259 /* TODO: build_x_unary_op does not always honor the location,
8260 so ensure it is set. */
8261 expression.set_location (loc);
8262 break;
8264 case PREINCREMENT_EXPR:
8265 case PREDECREMENT_EXPR:
8266 non_constant_p = unary_operator == PREINCREMENT_EXPR
8267 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8268 /* Fall through. */
8269 case NEGATE_EXPR:
8270 /* Immediately fold negation of a constant, unless the constant is 0
8271 (since -0 == 0) or it would overflow. */
8272 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8273 && CONSTANT_CLASS_P (cast_expression)
8274 && !integer_zerop (cast_expression)
8275 && !TREE_OVERFLOW (cast_expression))
8277 tree folded = fold_build1 (unary_operator,
8278 TREE_TYPE (cast_expression),
8279 cast_expression);
8280 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8282 expression = cp_expr (folded, loc);
8283 break;
8286 /* Fall through. */
8287 case UNARY_PLUS_EXPR:
8288 case TRUTH_NOT_EXPR:
8289 expression = finish_unary_op_expr (loc, unary_operator,
8290 cast_expression, complain);
8291 break;
8293 default:
8294 gcc_unreachable ();
8297 if (non_constant_p != NIC_NONE
8298 && cp_parser_non_integral_constant_expression (parser,
8299 non_constant_p))
8300 expression = error_mark_node;
8302 return expression;
8305 return cp_parser_postfix_expression (parser, address_p, cast_p,
8306 /*member_access_only_p=*/false,
8307 decltype_p,
8308 pidk);
8311 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8312 unary-operator, the corresponding tree code is returned. */
8314 static enum tree_code
8315 cp_parser_unary_operator (cp_token* token)
8317 switch (token->type)
8319 case CPP_MULT:
8320 return INDIRECT_REF;
8322 case CPP_AND:
8323 return ADDR_EXPR;
8325 case CPP_PLUS:
8326 return UNARY_PLUS_EXPR;
8328 case CPP_MINUS:
8329 return NEGATE_EXPR;
8331 case CPP_NOT:
8332 return TRUTH_NOT_EXPR;
8334 case CPP_COMPL:
8335 return BIT_NOT_EXPR;
8337 default:
8338 return ERROR_MARK;
8342 /* Parse a new-expression.
8344 new-expression:
8345 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8346 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8348 Returns a representation of the expression. */
8350 static tree
8351 cp_parser_new_expression (cp_parser* parser)
8353 bool global_scope_p;
8354 vec<tree, va_gc> *placement;
8355 tree type;
8356 vec<tree, va_gc> *initializer;
8357 tree nelts = NULL_TREE;
8358 tree ret;
8360 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8362 /* Look for the optional `::' operator. */
8363 global_scope_p
8364 = (cp_parser_global_scope_opt (parser,
8365 /*current_scope_valid_p=*/false)
8366 != NULL_TREE);
8367 /* Look for the `new' operator. */
8368 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8369 /* There's no easy way to tell a new-placement from the
8370 `( type-id )' construct. */
8371 cp_parser_parse_tentatively (parser);
8372 /* Look for a new-placement. */
8373 placement = cp_parser_new_placement (parser);
8374 /* If that didn't work out, there's no new-placement. */
8375 if (!cp_parser_parse_definitely (parser))
8377 if (placement != NULL)
8378 release_tree_vector (placement);
8379 placement = NULL;
8382 /* If the next token is a `(', then we have a parenthesized
8383 type-id. */
8384 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8386 cp_token *token;
8387 const char *saved_message = parser->type_definition_forbidden_message;
8389 /* Consume the `('. */
8390 matching_parens parens;
8391 parens.consume_open (parser);
8393 /* Parse the type-id. */
8394 parser->type_definition_forbidden_message
8395 = G_("types may not be defined in a new-expression");
8397 type_id_in_expr_sentinel s (parser);
8398 type = cp_parser_type_id (parser);
8400 parser->type_definition_forbidden_message = saved_message;
8402 /* Look for the closing `)'. */
8403 parens.require_close (parser);
8404 token = cp_lexer_peek_token (parser->lexer);
8405 /* There should not be a direct-new-declarator in this production,
8406 but GCC used to allowed this, so we check and emit a sensible error
8407 message for this case. */
8408 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8410 error_at (token->location,
8411 "array bound forbidden after parenthesized type-id");
8412 inform (token->location,
8413 "try removing the parentheses around the type-id");
8414 cp_parser_direct_new_declarator (parser);
8417 /* Otherwise, there must be a new-type-id. */
8418 else
8419 type = cp_parser_new_type_id (parser, &nelts);
8421 /* If the next token is a `(' or '{', then we have a new-initializer. */
8422 cp_token *token = cp_lexer_peek_token (parser->lexer);
8423 if (token->type == CPP_OPEN_PAREN
8424 || token->type == CPP_OPEN_BRACE)
8425 initializer = cp_parser_new_initializer (parser);
8426 else
8427 initializer = NULL;
8429 /* A new-expression may not appear in an integral constant
8430 expression. */
8431 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8432 ret = error_mark_node;
8433 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8434 of a new-type-id or type-id of a new-expression, the new-expression shall
8435 contain a new-initializer of the form ( assignment-expression )".
8436 Additionally, consistently with the spirit of DR 1467, we want to accept
8437 'new auto { 2 }' too. */
8438 else if ((ret = type_uses_auto (type))
8439 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8440 && (vec_safe_length (initializer) != 1
8441 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8442 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8444 error_at (token->location,
8445 "initialization of new-expression for type %<auto%> "
8446 "requires exactly one element");
8447 ret = error_mark_node;
8449 else
8451 /* Construct a location e.g.:
8452 ptr = new int[100]
8453 ^~~~~~~~~~~~
8454 with caret == start at the start of the "new" token, and the end
8455 at the end of the final token we consumed. */
8456 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8457 location_t end_loc = get_finish (end_tok->location);
8458 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8460 /* Create a representation of the new-expression. */
8461 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8462 tf_warning_or_error);
8463 protected_set_expr_location (ret, combined_loc);
8466 if (placement != NULL)
8467 release_tree_vector (placement);
8468 if (initializer != NULL)
8469 release_tree_vector (initializer);
8471 return ret;
8474 /* Parse a new-placement.
8476 new-placement:
8477 ( expression-list )
8479 Returns the same representation as for an expression-list. */
8481 static vec<tree, va_gc> *
8482 cp_parser_new_placement (cp_parser* parser)
8484 vec<tree, va_gc> *expression_list;
8486 /* Parse the expression-list. */
8487 expression_list = (cp_parser_parenthesized_expression_list
8488 (parser, non_attr, /*cast_p=*/false,
8489 /*allow_expansion_p=*/true,
8490 /*non_constant_p=*/NULL));
8492 if (expression_list && expression_list->is_empty ())
8493 error ("expected expression-list or type-id");
8495 return expression_list;
8498 /* Parse a new-type-id.
8500 new-type-id:
8501 type-specifier-seq new-declarator [opt]
8503 Returns the TYPE allocated. If the new-type-id indicates an array
8504 type, *NELTS is set to the number of elements in the last array
8505 bound; the TYPE will not include the last array bound. */
8507 static tree
8508 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8510 cp_decl_specifier_seq type_specifier_seq;
8511 cp_declarator *new_declarator;
8512 cp_declarator *declarator;
8513 cp_declarator *outer_declarator;
8514 const char *saved_message;
8516 /* The type-specifier sequence must not contain type definitions.
8517 (It cannot contain declarations of new types either, but if they
8518 are not definitions we will catch that because they are not
8519 complete.) */
8520 saved_message = parser->type_definition_forbidden_message;
8521 parser->type_definition_forbidden_message
8522 = G_("types may not be defined in a new-type-id");
8523 /* Parse the type-specifier-seq. */
8524 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8525 /*is_trailing_return=*/false,
8526 &type_specifier_seq);
8527 /* Restore the old message. */
8528 parser->type_definition_forbidden_message = saved_message;
8530 if (type_specifier_seq.type == error_mark_node)
8531 return error_mark_node;
8533 /* Parse the new-declarator. */
8534 new_declarator = cp_parser_new_declarator_opt (parser);
8536 /* Determine the number of elements in the last array dimension, if
8537 any. */
8538 *nelts = NULL_TREE;
8539 /* Skip down to the last array dimension. */
8540 declarator = new_declarator;
8541 outer_declarator = NULL;
8542 while (declarator && (declarator->kind == cdk_pointer
8543 || declarator->kind == cdk_ptrmem))
8545 outer_declarator = declarator;
8546 declarator = declarator->declarator;
8548 while (declarator
8549 && declarator->kind == cdk_array
8550 && declarator->declarator
8551 && declarator->declarator->kind == cdk_array)
8553 outer_declarator = declarator;
8554 declarator = declarator->declarator;
8557 if (declarator && declarator->kind == cdk_array)
8559 *nelts = declarator->u.array.bounds;
8560 if (*nelts == error_mark_node)
8561 *nelts = integer_one_node;
8563 if (outer_declarator)
8564 outer_declarator->declarator = declarator->declarator;
8565 else
8566 new_declarator = NULL;
8569 return groktypename (&type_specifier_seq, new_declarator, false);
8572 /* Parse an (optional) new-declarator.
8574 new-declarator:
8575 ptr-operator new-declarator [opt]
8576 direct-new-declarator
8578 Returns the declarator. */
8580 static cp_declarator *
8581 cp_parser_new_declarator_opt (cp_parser* parser)
8583 enum tree_code code;
8584 tree type, std_attributes = NULL_TREE;
8585 cp_cv_quals cv_quals;
8587 /* We don't know if there's a ptr-operator next, or not. */
8588 cp_parser_parse_tentatively (parser);
8589 /* Look for a ptr-operator. */
8590 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8591 /* If that worked, look for more new-declarators. */
8592 if (cp_parser_parse_definitely (parser))
8594 cp_declarator *declarator;
8596 /* Parse another optional declarator. */
8597 declarator = cp_parser_new_declarator_opt (parser);
8599 declarator = cp_parser_make_indirect_declarator
8600 (code, type, cv_quals, declarator, std_attributes);
8602 return declarator;
8605 /* If the next token is a `[', there is a direct-new-declarator. */
8606 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8607 return cp_parser_direct_new_declarator (parser);
8609 return NULL;
8612 /* Parse a direct-new-declarator.
8614 direct-new-declarator:
8615 [ expression ]
8616 direct-new-declarator [constant-expression]
8620 static cp_declarator *
8621 cp_parser_direct_new_declarator (cp_parser* parser)
8623 cp_declarator *declarator = NULL;
8625 while (true)
8627 tree expression;
8628 cp_token *token;
8630 /* Look for the opening `['. */
8631 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8633 token = cp_lexer_peek_token (parser->lexer);
8634 expression = cp_parser_expression (parser);
8635 /* The standard requires that the expression have integral
8636 type. DR 74 adds enumeration types. We believe that the
8637 real intent is that these expressions be handled like the
8638 expression in a `switch' condition, which also allows
8639 classes with a single conversion to integral or
8640 enumeration type. */
8641 if (!processing_template_decl)
8643 expression
8644 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8645 expression,
8646 /*complain=*/true);
8647 if (!expression)
8649 error_at (token->location,
8650 "expression in new-declarator must have integral "
8651 "or enumeration type");
8652 expression = error_mark_node;
8656 /* Look for the closing `]'. */
8657 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8659 /* Add this bound to the declarator. */
8660 declarator = make_array_declarator (declarator, expression);
8662 /* If the next token is not a `[', then there are no more
8663 bounds. */
8664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8665 break;
8668 return declarator;
8671 /* Parse a new-initializer.
8673 new-initializer:
8674 ( expression-list [opt] )
8675 braced-init-list
8677 Returns a representation of the expression-list. */
8679 static vec<tree, va_gc> *
8680 cp_parser_new_initializer (cp_parser* parser)
8682 vec<tree, va_gc> *expression_list;
8684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8686 tree t;
8687 bool expr_non_constant_p;
8688 cp_lexer_set_source_position (parser->lexer);
8689 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8690 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8691 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8692 expression_list = make_tree_vector_single (t);
8694 else
8695 expression_list = (cp_parser_parenthesized_expression_list
8696 (parser, non_attr, /*cast_p=*/false,
8697 /*allow_expansion_p=*/true,
8698 /*non_constant_p=*/NULL));
8700 return expression_list;
8703 /* Parse a delete-expression.
8705 delete-expression:
8706 :: [opt] delete cast-expression
8707 :: [opt] delete [ ] cast-expression
8709 Returns a representation of the expression. */
8711 static tree
8712 cp_parser_delete_expression (cp_parser* parser)
8714 bool global_scope_p;
8715 bool array_p;
8716 tree expression;
8718 /* Look for the optional `::' operator. */
8719 global_scope_p
8720 = (cp_parser_global_scope_opt (parser,
8721 /*current_scope_valid_p=*/false)
8722 != NULL_TREE);
8723 /* Look for the `delete' keyword. */
8724 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8725 /* See if the array syntax is in use. */
8726 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8728 /* Consume the `[' token. */
8729 cp_lexer_consume_token (parser->lexer);
8730 /* Look for the `]' token. */
8731 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8732 /* Remember that this is the `[]' construct. */
8733 array_p = true;
8735 else
8736 array_p = false;
8738 /* Parse the cast-expression. */
8739 expression = cp_parser_simple_cast_expression (parser);
8741 /* A delete-expression may not appear in an integral constant
8742 expression. */
8743 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8744 return error_mark_node;
8746 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8747 tf_warning_or_error);
8750 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8751 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8752 0 otherwise. */
8754 static int
8755 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8757 cp_token *token = cp_lexer_peek_token (parser->lexer);
8758 switch (token->type)
8760 case CPP_COMMA:
8761 case CPP_SEMICOLON:
8762 case CPP_QUERY:
8763 case CPP_COLON:
8764 case CPP_CLOSE_SQUARE:
8765 case CPP_CLOSE_PAREN:
8766 case CPP_CLOSE_BRACE:
8767 case CPP_OPEN_BRACE:
8768 case CPP_DOT:
8769 case CPP_DOT_STAR:
8770 case CPP_DEREF:
8771 case CPP_DEREF_STAR:
8772 case CPP_DIV:
8773 case CPP_MOD:
8774 case CPP_LSHIFT:
8775 case CPP_RSHIFT:
8776 case CPP_LESS:
8777 case CPP_GREATER:
8778 case CPP_LESS_EQ:
8779 case CPP_GREATER_EQ:
8780 case CPP_EQ_EQ:
8781 case CPP_NOT_EQ:
8782 case CPP_EQ:
8783 case CPP_MULT_EQ:
8784 case CPP_DIV_EQ:
8785 case CPP_MOD_EQ:
8786 case CPP_PLUS_EQ:
8787 case CPP_MINUS_EQ:
8788 case CPP_RSHIFT_EQ:
8789 case CPP_LSHIFT_EQ:
8790 case CPP_AND_EQ:
8791 case CPP_XOR_EQ:
8792 case CPP_OR_EQ:
8793 case CPP_XOR:
8794 case CPP_OR:
8795 case CPP_OR_OR:
8796 case CPP_EOF:
8797 case CPP_ELLIPSIS:
8798 return 0;
8800 case CPP_OPEN_PAREN:
8801 /* In ((type ()) () the last () isn't a valid cast-expression,
8802 so the whole must be parsed as postfix-expression. */
8803 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8804 != CPP_CLOSE_PAREN;
8806 case CPP_OPEN_SQUARE:
8807 /* '[' may start a primary-expression in obj-c++ and in C++11,
8808 as a lambda-expression, eg, '(void)[]{}'. */
8809 if (cxx_dialect >= cxx11)
8810 return -1;
8811 return c_dialect_objc ();
8813 case CPP_PLUS_PLUS:
8814 case CPP_MINUS_MINUS:
8815 /* '++' and '--' may or may not start a cast-expression:
8817 struct T { void operator++(int); };
8818 void f() { (T())++; }
8822 int a;
8823 (int)++a; */
8824 return -1;
8826 default:
8827 return 1;
8831 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8832 in the order: const_cast, static_cast, reinterpret_cast.
8834 Don't suggest dynamic_cast.
8836 Return the first legal cast kind found, or NULL otherwise. */
8838 static const char *
8839 get_cast_suggestion (tree dst_type, tree orig_expr)
8841 tree trial;
8843 /* Reuse the parser logic by attempting to build the various kinds of
8844 cast, with "complain" disabled.
8845 Identify the first such cast that is valid. */
8847 /* Don't attempt to run such logic within template processing. */
8848 if (processing_template_decl)
8849 return NULL;
8851 /* First try const_cast. */
8852 trial = build_const_cast (dst_type, orig_expr, tf_none);
8853 if (trial != error_mark_node)
8854 return "const_cast";
8856 /* If that fails, try static_cast. */
8857 trial = build_static_cast (dst_type, orig_expr, tf_none);
8858 if (trial != error_mark_node)
8859 return "static_cast";
8861 /* Finally, try reinterpret_cast. */
8862 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8863 if (trial != error_mark_node)
8864 return "reinterpret_cast";
8866 /* No such cast possible. */
8867 return NULL;
8870 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8871 suggesting how to convert a C-style cast of the form:
8873 (DST_TYPE)ORIG_EXPR
8875 to a C++-style cast.
8877 The primary range of RICHLOC is asssumed to be that of the original
8878 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8879 of the parens in the C-style cast. */
8881 static void
8882 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8883 location_t close_paren_loc, tree orig_expr,
8884 tree dst_type)
8886 /* This function is non-trivial, so bail out now if the warning isn't
8887 going to be emitted. */
8888 if (!warn_old_style_cast)
8889 return;
8891 /* Try to find a legal C++ cast, trying them in order:
8892 const_cast, static_cast, reinterpret_cast. */
8893 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8894 if (!cast_suggestion)
8895 return;
8897 /* Replace the open paren with "CAST_SUGGESTION<". */
8898 pretty_printer pp;
8899 pp_printf (&pp, "%s<", cast_suggestion);
8900 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8902 /* Replace the close paren with "> (". */
8903 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8905 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8906 rich_loc->add_fixit_insert_after (")");
8910 /* Parse a cast-expression.
8912 cast-expression:
8913 unary-expression
8914 ( type-id ) cast-expression
8916 ADDRESS_P is true iff the unary-expression is appearing as the
8917 operand of the `&' operator. CAST_P is true if this expression is
8918 the target of a cast.
8920 Returns a representation of the expression. */
8922 static cp_expr
8923 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8924 bool decltype_p, cp_id_kind * pidk)
8926 /* If it's a `(', then we might be looking at a cast. */
8927 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8929 tree type = NULL_TREE;
8930 cp_expr expr (NULL_TREE);
8931 int cast_expression = 0;
8932 const char *saved_message;
8934 /* There's no way to know yet whether or not this is a cast.
8935 For example, `(int (3))' is a unary-expression, while `(int)
8936 3' is a cast. So, we resort to parsing tentatively. */
8937 cp_parser_parse_tentatively (parser);
8938 /* Types may not be defined in a cast. */
8939 saved_message = parser->type_definition_forbidden_message;
8940 parser->type_definition_forbidden_message
8941 = G_("types may not be defined in casts");
8942 /* Consume the `('. */
8943 matching_parens parens;
8944 cp_token *open_paren = parens.consume_open (parser);
8945 location_t open_paren_loc = open_paren->location;
8946 location_t close_paren_loc = UNKNOWN_LOCATION;
8948 /* A very tricky bit is that `(struct S) { 3 }' is a
8949 compound-literal (which we permit in C++ as an extension).
8950 But, that construct is not a cast-expression -- it is a
8951 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8952 is legal; if the compound-literal were a cast-expression,
8953 you'd need an extra set of parentheses.) But, if we parse
8954 the type-id, and it happens to be a class-specifier, then we
8955 will commit to the parse at that point, because we cannot
8956 undo the action that is done when creating a new class. So,
8957 then we cannot back up and do a postfix-expression.
8959 Another tricky case is the following (c++/29234):
8961 struct S { void operator () (); };
8963 void foo ()
8965 ( S()() );
8968 As a type-id we parse the parenthesized S()() as a function
8969 returning a function, groktypename complains and we cannot
8970 back up in this case either.
8972 Therefore, we scan ahead to the closing `)', and check to see
8973 if the tokens after the `)' can start a cast-expression. Otherwise
8974 we are dealing with an unary-expression, a postfix-expression
8975 or something else.
8977 Yet another tricky case, in C++11, is the following (c++/54891):
8979 (void)[]{};
8981 The issue is that usually, besides the case of lambda-expressions,
8982 the parenthesized type-id cannot be followed by '[', and, eg, we
8983 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8984 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8985 we don't commit, we try a cast-expression, then an unary-expression.
8987 Save tokens so that we can put them back. */
8988 cp_lexer_save_tokens (parser->lexer);
8990 /* We may be looking at a cast-expression. */
8991 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8992 /*consume_paren=*/true))
8993 cast_expression
8994 = cp_parser_tokens_start_cast_expression (parser);
8996 /* Roll back the tokens we skipped. */
8997 cp_lexer_rollback_tokens (parser->lexer);
8998 /* If we aren't looking at a cast-expression, simulate an error so
8999 that the call to cp_parser_error_occurred below returns true. */
9000 if (!cast_expression)
9001 cp_parser_simulate_error (parser);
9002 else
9004 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9005 parser->in_type_id_in_expr_p = true;
9006 /* Look for the type-id. */
9007 type = cp_parser_type_id (parser);
9008 /* Look for the closing `)'. */
9009 cp_token *close_paren = parens.require_close (parser);
9010 if (close_paren)
9011 close_paren_loc = close_paren->location;
9012 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9015 /* Restore the saved message. */
9016 parser->type_definition_forbidden_message = saved_message;
9018 /* At this point this can only be either a cast or a
9019 parenthesized ctor such as `(T ())' that looks like a cast to
9020 function returning T. */
9021 if (!cp_parser_error_occurred (parser))
9023 /* Only commit if the cast-expression doesn't start with
9024 '++', '--', or '[' in C++11. */
9025 if (cast_expression > 0)
9026 cp_parser_commit_to_topmost_tentative_parse (parser);
9028 expr = cp_parser_cast_expression (parser,
9029 /*address_p=*/false,
9030 /*cast_p=*/true,
9031 /*decltype_p=*/false,
9032 pidk);
9034 if (cp_parser_parse_definitely (parser))
9036 /* Warn about old-style casts, if so requested. */
9037 if (warn_old_style_cast
9038 && !in_system_header_at (input_location)
9039 && !VOID_TYPE_P (type)
9040 && current_lang_name != lang_name_c)
9042 gcc_rich_location rich_loc (input_location);
9043 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9044 expr, type);
9045 warning_at (&rich_loc, OPT_Wold_style_cast,
9046 "use of old-style cast to %q#T", type);
9049 /* Only type conversions to integral or enumeration types
9050 can be used in constant-expressions. */
9051 if (!cast_valid_in_integral_constant_expression_p (type)
9052 && cp_parser_non_integral_constant_expression (parser,
9053 NIC_CAST))
9054 return error_mark_node;
9056 /* Perform the cast. */
9057 /* Make a location:
9058 (TYPE) EXPR
9059 ^~~~~~~~~~~
9060 with start==caret at the open paren, extending to the
9061 end of "expr". */
9062 location_t cast_loc = make_location (open_paren_loc,
9063 open_paren_loc,
9064 expr.get_finish ());
9065 expr = build_c_cast (cast_loc, type, expr);
9066 return expr;
9069 else
9070 cp_parser_abort_tentative_parse (parser);
9073 /* If we get here, then it's not a cast, so it must be a
9074 unary-expression. */
9075 return cp_parser_unary_expression (parser, pidk, address_p,
9076 cast_p, decltype_p);
9079 /* Parse a binary expression of the general form:
9081 pm-expression:
9082 cast-expression
9083 pm-expression .* cast-expression
9084 pm-expression ->* cast-expression
9086 multiplicative-expression:
9087 pm-expression
9088 multiplicative-expression * pm-expression
9089 multiplicative-expression / pm-expression
9090 multiplicative-expression % pm-expression
9092 additive-expression:
9093 multiplicative-expression
9094 additive-expression + multiplicative-expression
9095 additive-expression - multiplicative-expression
9097 shift-expression:
9098 additive-expression
9099 shift-expression << additive-expression
9100 shift-expression >> additive-expression
9102 relational-expression:
9103 shift-expression
9104 relational-expression < shift-expression
9105 relational-expression > shift-expression
9106 relational-expression <= shift-expression
9107 relational-expression >= shift-expression
9109 GNU Extension:
9111 relational-expression:
9112 relational-expression <? shift-expression
9113 relational-expression >? shift-expression
9115 equality-expression:
9116 relational-expression
9117 equality-expression == relational-expression
9118 equality-expression != relational-expression
9120 and-expression:
9121 equality-expression
9122 and-expression & equality-expression
9124 exclusive-or-expression:
9125 and-expression
9126 exclusive-or-expression ^ and-expression
9128 inclusive-or-expression:
9129 exclusive-or-expression
9130 inclusive-or-expression | exclusive-or-expression
9132 logical-and-expression:
9133 inclusive-or-expression
9134 logical-and-expression && inclusive-or-expression
9136 logical-or-expression:
9137 logical-and-expression
9138 logical-or-expression || logical-and-expression
9140 All these are implemented with a single function like:
9142 binary-expression:
9143 simple-cast-expression
9144 binary-expression <token> binary-expression
9146 CAST_P is true if this expression is the target of a cast.
9148 The binops_by_token map is used to get the tree codes for each <token> type.
9149 binary-expressions are associated according to a precedence table. */
9151 #define TOKEN_PRECEDENCE(token) \
9152 (((token->type == CPP_GREATER \
9153 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9154 && !parser->greater_than_is_operator_p) \
9155 ? PREC_NOT_OPERATOR \
9156 : binops_by_token[token->type].prec)
9158 static cp_expr
9159 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9160 bool no_toplevel_fold_p,
9161 bool decltype_p,
9162 enum cp_parser_prec prec,
9163 cp_id_kind * pidk)
9165 cp_parser_expression_stack stack;
9166 cp_parser_expression_stack_entry *sp = &stack[0];
9167 cp_parser_expression_stack_entry current;
9168 cp_expr rhs;
9169 cp_token *token;
9170 enum tree_code rhs_type;
9171 enum cp_parser_prec new_prec, lookahead_prec;
9172 tree overload;
9174 /* Parse the first expression. */
9175 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9176 ? TRUTH_NOT_EXPR : ERROR_MARK);
9177 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9178 cast_p, decltype_p, pidk);
9179 current.prec = prec;
9181 if (cp_parser_error_occurred (parser))
9182 return error_mark_node;
9184 for (;;)
9186 /* Get an operator token. */
9187 token = cp_lexer_peek_token (parser->lexer);
9189 if (warn_cxx11_compat
9190 && token->type == CPP_RSHIFT
9191 && !parser->greater_than_is_operator_p)
9193 if (warning_at (token->location, OPT_Wc__11_compat,
9194 "%<>>%> operator is treated"
9195 " as two right angle brackets in C++11"))
9196 inform (token->location,
9197 "suggest parentheses around %<>>%> expression");
9200 new_prec = TOKEN_PRECEDENCE (token);
9201 if (new_prec != PREC_NOT_OPERATOR
9202 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9203 /* This is a fold-expression; handle it later. */
9204 new_prec = PREC_NOT_OPERATOR;
9206 /* Popping an entry off the stack means we completed a subexpression:
9207 - either we found a token which is not an operator (`>' where it is not
9208 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9209 will happen repeatedly;
9210 - or, we found an operator which has lower priority. This is the case
9211 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9212 parsing `3 * 4'. */
9213 if (new_prec <= current.prec)
9215 if (sp == stack)
9216 break;
9217 else
9218 goto pop;
9221 get_rhs:
9222 current.tree_type = binops_by_token[token->type].tree_type;
9223 current.loc = token->location;
9225 /* We used the operator token. */
9226 cp_lexer_consume_token (parser->lexer);
9228 /* For "false && x" or "true || x", x will never be executed;
9229 disable warnings while evaluating it. */
9230 if (current.tree_type == TRUTH_ANDIF_EXPR)
9231 c_inhibit_evaluation_warnings +=
9232 cp_fully_fold (current.lhs) == truthvalue_false_node;
9233 else if (current.tree_type == TRUTH_ORIF_EXPR)
9234 c_inhibit_evaluation_warnings +=
9235 cp_fully_fold (current.lhs) == truthvalue_true_node;
9237 /* Extract another operand. It may be the RHS of this expression
9238 or the LHS of a new, higher priority expression. */
9239 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9240 ? TRUTH_NOT_EXPR : ERROR_MARK);
9241 rhs = cp_parser_simple_cast_expression (parser);
9243 /* Get another operator token. Look up its precedence to avoid
9244 building a useless (immediately popped) stack entry for common
9245 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9246 token = cp_lexer_peek_token (parser->lexer);
9247 lookahead_prec = TOKEN_PRECEDENCE (token);
9248 if (lookahead_prec != PREC_NOT_OPERATOR
9249 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9250 lookahead_prec = PREC_NOT_OPERATOR;
9251 if (lookahead_prec > new_prec)
9253 /* ... and prepare to parse the RHS of the new, higher priority
9254 expression. Since precedence levels on the stack are
9255 monotonically increasing, we do not have to care about
9256 stack overflows. */
9257 *sp = current;
9258 ++sp;
9259 current.lhs = rhs;
9260 current.lhs_type = rhs_type;
9261 current.prec = new_prec;
9262 new_prec = lookahead_prec;
9263 goto get_rhs;
9265 pop:
9266 lookahead_prec = new_prec;
9267 /* If the stack is not empty, we have parsed into LHS the right side
9268 (`4' in the example above) of an expression we had suspended.
9269 We can use the information on the stack to recover the LHS (`3')
9270 from the stack together with the tree code (`MULT_EXPR'), and
9271 the precedence of the higher level subexpression
9272 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9273 which will be used to actually build the additive expression. */
9274 rhs = current.lhs;
9275 rhs_type = current.lhs_type;
9276 --sp;
9277 current = *sp;
9280 /* Undo the disabling of warnings done above. */
9281 if (current.tree_type == TRUTH_ANDIF_EXPR)
9282 c_inhibit_evaluation_warnings -=
9283 cp_fully_fold (current.lhs) == truthvalue_false_node;
9284 else if (current.tree_type == TRUTH_ORIF_EXPR)
9285 c_inhibit_evaluation_warnings -=
9286 cp_fully_fold (current.lhs) == truthvalue_true_node;
9288 if (warn_logical_not_paren
9289 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9290 && current.lhs_type == TRUTH_NOT_EXPR
9291 /* Avoid warning for !!x == y. */
9292 && (TREE_CODE (current.lhs) != NE_EXPR
9293 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9294 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9295 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9296 /* Avoid warning for !b == y where b is boolean. */
9297 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9298 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9299 != BOOLEAN_TYPE))))
9300 /* Avoid warning for !!b == y where b is boolean. */
9301 && (!DECL_P (current.lhs)
9302 || TREE_TYPE (current.lhs) == NULL_TREE
9303 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9304 warn_logical_not_parentheses (current.loc, current.tree_type,
9305 current.lhs, maybe_constant_value (rhs));
9307 overload = NULL;
9309 location_t combined_loc = make_location (current.loc,
9310 current.lhs.get_start (),
9311 rhs.get_finish ());
9313 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9314 ERROR_MARK for everything that is not a binary expression.
9315 This makes warn_about_parentheses miss some warnings that
9316 involve unary operators. For unary expressions we should
9317 pass the correct tree_code unless the unary expression was
9318 surrounded by parentheses.
9320 if (no_toplevel_fold_p
9321 && lookahead_prec <= current.prec
9322 && sp == stack)
9324 if (current.lhs == error_mark_node || rhs == error_mark_node)
9325 current.lhs = error_mark_node;
9326 else
9328 current.lhs
9329 = build_min (current.tree_type,
9330 TREE_CODE_CLASS (current.tree_type)
9331 == tcc_comparison
9332 ? boolean_type_node : TREE_TYPE (current.lhs),
9333 current.lhs.get_value (), rhs.get_value ());
9334 SET_EXPR_LOCATION (current.lhs, combined_loc);
9337 else
9339 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9340 current.lhs, current.lhs_type,
9341 rhs, rhs_type, &overload,
9342 complain_flags (decltype_p));
9343 /* TODO: build_x_binary_op doesn't always honor the location. */
9344 current.lhs.set_location (combined_loc);
9346 current.lhs_type = current.tree_type;
9348 /* If the binary operator required the use of an overloaded operator,
9349 then this expression cannot be an integral constant-expression.
9350 An overloaded operator can be used even if both operands are
9351 otherwise permissible in an integral constant-expression if at
9352 least one of the operands is of enumeration type. */
9354 if (overload
9355 && cp_parser_non_integral_constant_expression (parser,
9356 NIC_OVERLOADED))
9357 return error_mark_node;
9360 return current.lhs;
9363 static cp_expr
9364 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9365 bool no_toplevel_fold_p,
9366 enum cp_parser_prec prec,
9367 cp_id_kind * pidk)
9369 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9370 /*decltype*/false, prec, pidk);
9373 /* Parse the `? expression : assignment-expression' part of a
9374 conditional-expression. The LOGICAL_OR_EXPR is the
9375 logical-or-expression that started the conditional-expression.
9376 Returns a representation of the entire conditional-expression.
9378 This routine is used by cp_parser_assignment_expression.
9380 ? expression : assignment-expression
9382 GNU Extensions:
9384 ? : assignment-expression */
9386 static tree
9387 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9389 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9390 cp_expr assignment_expr;
9391 struct cp_token *token;
9392 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9394 /* Consume the `?' token. */
9395 cp_lexer_consume_token (parser->lexer);
9396 token = cp_lexer_peek_token (parser->lexer);
9397 if (cp_parser_allow_gnu_extensions_p (parser)
9398 && token->type == CPP_COLON)
9400 pedwarn (token->location, OPT_Wpedantic,
9401 "ISO C++ does not allow ?: with omitted middle operand");
9402 /* Implicit true clause. */
9403 expr = NULL_TREE;
9404 c_inhibit_evaluation_warnings +=
9405 folded_logical_or_expr == truthvalue_true_node;
9406 warn_for_omitted_condop (token->location, logical_or_expr);
9408 else
9410 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9411 parser->colon_corrects_to_scope_p = false;
9412 /* Parse the expression. */
9413 c_inhibit_evaluation_warnings +=
9414 folded_logical_or_expr == truthvalue_false_node;
9415 expr = cp_parser_expression (parser);
9416 c_inhibit_evaluation_warnings +=
9417 ((folded_logical_or_expr == truthvalue_true_node)
9418 - (folded_logical_or_expr == truthvalue_false_node));
9419 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9422 /* The next token should be a `:'. */
9423 cp_parser_require (parser, CPP_COLON, RT_COLON);
9424 /* Parse the assignment-expression. */
9425 assignment_expr = cp_parser_assignment_expression (parser);
9426 c_inhibit_evaluation_warnings -=
9427 folded_logical_or_expr == truthvalue_true_node;
9429 /* Make a location:
9430 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9431 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9432 with the caret at the "?", ranging from the start of
9433 the logical_or_expr to the end of the assignment_expr. */
9434 loc = make_location (loc,
9435 logical_or_expr.get_start (),
9436 assignment_expr.get_finish ());
9438 /* Build the conditional-expression. */
9439 return build_x_conditional_expr (loc, logical_or_expr,
9440 expr,
9441 assignment_expr,
9442 tf_warning_or_error);
9445 /* Parse an assignment-expression.
9447 assignment-expression:
9448 conditional-expression
9449 logical-or-expression assignment-operator assignment_expression
9450 throw-expression
9452 CAST_P is true if this expression is the target of a cast.
9453 DECLTYPE_P is true if this expression is the operand of decltype.
9455 Returns a representation for the expression. */
9457 static cp_expr
9458 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9459 bool cast_p, bool decltype_p)
9461 cp_expr expr;
9463 /* If the next token is the `throw' keyword, then we're looking at
9464 a throw-expression. */
9465 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9466 expr = cp_parser_throw_expression (parser);
9467 /* Otherwise, it must be that we are looking at a
9468 logical-or-expression. */
9469 else
9471 /* Parse the binary expressions (logical-or-expression). */
9472 expr = cp_parser_binary_expression (parser, cast_p, false,
9473 decltype_p,
9474 PREC_NOT_OPERATOR, pidk);
9475 /* If the next token is a `?' then we're actually looking at a
9476 conditional-expression. */
9477 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9478 return cp_parser_question_colon_clause (parser, expr);
9479 else
9481 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9483 /* If it's an assignment-operator, we're using the second
9484 production. */
9485 enum tree_code assignment_operator
9486 = cp_parser_assignment_operator_opt (parser);
9487 if (assignment_operator != ERROR_MARK)
9489 bool non_constant_p;
9491 /* Parse the right-hand side of the assignment. */
9492 cp_expr rhs = cp_parser_initializer_clause (parser,
9493 &non_constant_p);
9495 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9496 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9498 /* An assignment may not appear in a
9499 constant-expression. */
9500 if (cp_parser_non_integral_constant_expression (parser,
9501 NIC_ASSIGNMENT))
9502 return error_mark_node;
9503 /* Build the assignment expression. Its default
9504 location:
9505 LHS = RHS
9506 ~~~~^~~~~
9507 is the location of the '=' token as the
9508 caret, ranging from the start of the lhs to the
9509 end of the rhs. */
9510 loc = make_location (loc,
9511 expr.get_start (),
9512 rhs.get_finish ());
9513 expr = build_x_modify_expr (loc, expr,
9514 assignment_operator,
9515 rhs,
9516 complain_flags (decltype_p));
9517 /* TODO: build_x_modify_expr doesn't honor the location,
9518 so we must set it here. */
9519 expr.set_location (loc);
9524 return expr;
9527 /* Parse an (optional) assignment-operator.
9529 assignment-operator: one of
9530 = *= /= %= += -= >>= <<= &= ^= |=
9532 GNU Extension:
9534 assignment-operator: one of
9535 <?= >?=
9537 If the next token is an assignment operator, the corresponding tree
9538 code is returned, and the token is consumed. For example, for
9539 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9540 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9541 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9542 operator, ERROR_MARK is returned. */
9544 static enum tree_code
9545 cp_parser_assignment_operator_opt (cp_parser* parser)
9547 enum tree_code op;
9548 cp_token *token;
9550 /* Peek at the next token. */
9551 token = cp_lexer_peek_token (parser->lexer);
9553 switch (token->type)
9555 case CPP_EQ:
9556 op = NOP_EXPR;
9557 break;
9559 case CPP_MULT_EQ:
9560 op = MULT_EXPR;
9561 break;
9563 case CPP_DIV_EQ:
9564 op = TRUNC_DIV_EXPR;
9565 break;
9567 case CPP_MOD_EQ:
9568 op = TRUNC_MOD_EXPR;
9569 break;
9571 case CPP_PLUS_EQ:
9572 op = PLUS_EXPR;
9573 break;
9575 case CPP_MINUS_EQ:
9576 op = MINUS_EXPR;
9577 break;
9579 case CPP_RSHIFT_EQ:
9580 op = RSHIFT_EXPR;
9581 break;
9583 case CPP_LSHIFT_EQ:
9584 op = LSHIFT_EXPR;
9585 break;
9587 case CPP_AND_EQ:
9588 op = BIT_AND_EXPR;
9589 break;
9591 case CPP_XOR_EQ:
9592 op = BIT_XOR_EXPR;
9593 break;
9595 case CPP_OR_EQ:
9596 op = BIT_IOR_EXPR;
9597 break;
9599 default:
9600 /* Nothing else is an assignment operator. */
9601 op = ERROR_MARK;
9604 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9605 if (op != ERROR_MARK
9606 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9607 op = ERROR_MARK;
9609 /* If it was an assignment operator, consume it. */
9610 if (op != ERROR_MARK)
9611 cp_lexer_consume_token (parser->lexer);
9613 return op;
9616 /* Parse an expression.
9618 expression:
9619 assignment-expression
9620 expression , assignment-expression
9622 CAST_P is true if this expression is the target of a cast.
9623 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9624 except possibly parenthesized or on the RHS of a comma (N3276).
9626 Returns a representation of the expression. */
9628 static cp_expr
9629 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9630 bool cast_p, bool decltype_p)
9632 cp_expr expression = NULL_TREE;
9633 location_t loc = UNKNOWN_LOCATION;
9635 while (true)
9637 cp_expr assignment_expression;
9639 /* Parse the next assignment-expression. */
9640 assignment_expression
9641 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9643 /* We don't create a temporary for a call that is the immediate operand
9644 of decltype or on the RHS of a comma. But when we see a comma, we
9645 need to create a temporary for a call on the LHS. */
9646 if (decltype_p && !processing_template_decl
9647 && TREE_CODE (assignment_expression) == CALL_EXPR
9648 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9649 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9650 assignment_expression
9651 = build_cplus_new (TREE_TYPE (assignment_expression),
9652 assignment_expression, tf_warning_or_error);
9654 /* If this is the first assignment-expression, we can just
9655 save it away. */
9656 if (!expression)
9657 expression = assignment_expression;
9658 else
9660 /* Create a location with caret at the comma, ranging
9661 from the start of the LHS to the end of the RHS. */
9662 loc = make_location (loc,
9663 expression.get_start (),
9664 assignment_expression.get_finish ());
9665 expression = build_x_compound_expr (loc, expression,
9666 assignment_expression,
9667 complain_flags (decltype_p));
9668 expression.set_location (loc);
9670 /* If the next token is not a comma, or we're in a fold-expression, then
9671 we are done with the expression. */
9672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9673 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9674 break;
9675 /* Consume the `,'. */
9676 loc = cp_lexer_peek_token (parser->lexer)->location;
9677 cp_lexer_consume_token (parser->lexer);
9678 /* A comma operator cannot appear in a constant-expression. */
9679 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9680 expression = error_mark_node;
9683 return expression;
9686 /* Parse a constant-expression.
9688 constant-expression:
9689 conditional-expression
9691 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9692 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9693 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9694 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9695 only parse a conditional-expression, otherwise parse an
9696 assignment-expression. See below for rationale. */
9698 static cp_expr
9699 cp_parser_constant_expression (cp_parser* parser,
9700 bool allow_non_constant_p,
9701 bool *non_constant_p,
9702 bool strict_p)
9704 bool saved_integral_constant_expression_p;
9705 bool saved_allow_non_integral_constant_expression_p;
9706 bool saved_non_integral_constant_expression_p;
9707 cp_expr expression;
9709 /* It might seem that we could simply parse the
9710 conditional-expression, and then check to see if it were
9711 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9712 one that the compiler can figure out is constant, possibly after
9713 doing some simplifications or optimizations. The standard has a
9714 precise definition of constant-expression, and we must honor
9715 that, even though it is somewhat more restrictive.
9717 For example:
9719 int i[(2, 3)];
9721 is not a legal declaration, because `(2, 3)' is not a
9722 constant-expression. The `,' operator is forbidden in a
9723 constant-expression. However, GCC's constant-folding machinery
9724 will fold this operation to an INTEGER_CST for `3'. */
9726 /* Save the old settings. */
9727 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9728 saved_allow_non_integral_constant_expression_p
9729 = parser->allow_non_integral_constant_expression_p;
9730 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9731 /* We are now parsing a constant-expression. */
9732 parser->integral_constant_expression_p = true;
9733 parser->allow_non_integral_constant_expression_p
9734 = (allow_non_constant_p || cxx_dialect >= cxx11);
9735 parser->non_integral_constant_expression_p = false;
9736 /* Although the grammar says "conditional-expression", when not STRICT_P,
9737 we parse an "assignment-expression", which also permits
9738 "throw-expression" and the use of assignment operators. In the case
9739 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9740 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9741 actually essential that we look for an assignment-expression.
9742 For example, cp_parser_initializer_clauses uses this function to
9743 determine whether a particular assignment-expression is in fact
9744 constant. */
9745 if (strict_p)
9747 /* Parse the binary expressions (logical-or-expression). */
9748 expression = cp_parser_binary_expression (parser, false, false, false,
9749 PREC_NOT_OPERATOR, NULL);
9750 /* If the next token is a `?' then we're actually looking at
9751 a conditional-expression; otherwise we're done. */
9752 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9753 expression = cp_parser_question_colon_clause (parser, expression);
9755 else
9756 expression = cp_parser_assignment_expression (parser);
9757 /* Restore the old settings. */
9758 parser->integral_constant_expression_p
9759 = saved_integral_constant_expression_p;
9760 parser->allow_non_integral_constant_expression_p
9761 = saved_allow_non_integral_constant_expression_p;
9762 if (cxx_dialect >= cxx11)
9764 /* Require an rvalue constant expression here; that's what our
9765 callers expect. Reference constant expressions are handled
9766 separately in e.g. cp_parser_template_argument. */
9767 tree decay = expression;
9768 if (TREE_TYPE (expression)
9769 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9770 decay = build_address (expression);
9771 bool is_const = potential_rvalue_constant_expression (decay);
9772 parser->non_integral_constant_expression_p = !is_const;
9773 if (!is_const && !allow_non_constant_p)
9774 require_potential_rvalue_constant_expression (decay);
9776 if (allow_non_constant_p)
9777 *non_constant_p = parser->non_integral_constant_expression_p;
9778 parser->non_integral_constant_expression_p
9779 = saved_non_integral_constant_expression_p;
9781 return expression;
9784 /* Parse __builtin_offsetof.
9786 offsetof-expression:
9787 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9789 offsetof-member-designator:
9790 id-expression
9791 | offsetof-member-designator "." id-expression
9792 | offsetof-member-designator "[" expression "]"
9793 | offsetof-member-designator "->" id-expression */
9795 static cp_expr
9796 cp_parser_builtin_offsetof (cp_parser *parser)
9798 int save_ice_p, save_non_ice_p;
9799 tree type;
9800 cp_expr expr;
9801 cp_id_kind dummy;
9802 cp_token *token;
9803 location_t finish_loc;
9805 /* We're about to accept non-integral-constant things, but will
9806 definitely yield an integral constant expression. Save and
9807 restore these values around our local parsing. */
9808 save_ice_p = parser->integral_constant_expression_p;
9809 save_non_ice_p = parser->non_integral_constant_expression_p;
9811 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9813 /* Consume the "__builtin_offsetof" token. */
9814 cp_lexer_consume_token (parser->lexer);
9815 /* Consume the opening `('. */
9816 matching_parens parens;
9817 parens.require_open (parser);
9818 /* Parse the type-id. */
9819 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9821 const char *saved_message = parser->type_definition_forbidden_message;
9822 parser->type_definition_forbidden_message
9823 = G_("types may not be defined within __builtin_offsetof");
9824 type = cp_parser_type_id (parser);
9825 parser->type_definition_forbidden_message = saved_message;
9827 /* Look for the `,'. */
9828 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9829 token = cp_lexer_peek_token (parser->lexer);
9831 /* Build the (type *)null that begins the traditional offsetof macro. */
9832 tree object_ptr
9833 = build_static_cast (build_pointer_type (type), null_pointer_node,
9834 tf_warning_or_error);
9836 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9837 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9838 true, &dummy, token->location);
9839 while (true)
9841 token = cp_lexer_peek_token (parser->lexer);
9842 switch (token->type)
9844 case CPP_OPEN_SQUARE:
9845 /* offsetof-member-designator "[" expression "]" */
9846 expr = cp_parser_postfix_open_square_expression (parser, expr,
9847 true, false);
9848 break;
9850 case CPP_DEREF:
9851 /* offsetof-member-designator "->" identifier */
9852 expr = grok_array_decl (token->location, expr,
9853 integer_zero_node, false);
9854 /* FALLTHRU */
9856 case CPP_DOT:
9857 /* offsetof-member-designator "." identifier */
9858 cp_lexer_consume_token (parser->lexer);
9859 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9860 expr, true, &dummy,
9861 token->location);
9862 break;
9864 case CPP_CLOSE_PAREN:
9865 /* Consume the ")" token. */
9866 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9867 cp_lexer_consume_token (parser->lexer);
9868 goto success;
9870 default:
9871 /* Error. We know the following require will fail, but
9872 that gives the proper error message. */
9873 parens.require_close (parser);
9874 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9875 expr = error_mark_node;
9876 goto failure;
9880 success:
9881 /* Make a location of the form:
9882 __builtin_offsetof (struct s, f)
9883 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9884 with caret at the type-id, ranging from the start of the
9885 "_builtin_offsetof" token to the close paren. */
9886 loc = make_location (loc, start_loc, finish_loc);
9887 /* The result will be an INTEGER_CST, so we need to explicitly
9888 preserve the location. */
9889 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9891 failure:
9892 parser->integral_constant_expression_p = save_ice_p;
9893 parser->non_integral_constant_expression_p = save_non_ice_p;
9895 expr = expr.maybe_add_location_wrapper ();
9896 return expr;
9899 /* Parse a trait expression.
9901 Returns a representation of the expression, the underlying type
9902 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9904 static cp_expr
9905 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9907 cp_trait_kind kind;
9908 tree type1, type2 = NULL_TREE;
9909 bool binary = false;
9910 bool variadic = false;
9912 switch (keyword)
9914 case RID_HAS_NOTHROW_ASSIGN:
9915 kind = CPTK_HAS_NOTHROW_ASSIGN;
9916 break;
9917 case RID_HAS_NOTHROW_CONSTRUCTOR:
9918 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9919 break;
9920 case RID_HAS_NOTHROW_COPY:
9921 kind = CPTK_HAS_NOTHROW_COPY;
9922 break;
9923 case RID_HAS_TRIVIAL_ASSIGN:
9924 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9925 break;
9926 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9927 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9928 break;
9929 case RID_HAS_TRIVIAL_COPY:
9930 kind = CPTK_HAS_TRIVIAL_COPY;
9931 break;
9932 case RID_HAS_TRIVIAL_DESTRUCTOR:
9933 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9934 break;
9935 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9936 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9937 break;
9938 case RID_HAS_VIRTUAL_DESTRUCTOR:
9939 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9940 break;
9941 case RID_IS_ABSTRACT:
9942 kind = CPTK_IS_ABSTRACT;
9943 break;
9944 case RID_IS_AGGREGATE:
9945 kind = CPTK_IS_AGGREGATE;
9946 break;
9947 case RID_IS_BASE_OF:
9948 kind = CPTK_IS_BASE_OF;
9949 binary = true;
9950 break;
9951 case RID_IS_CLASS:
9952 kind = CPTK_IS_CLASS;
9953 break;
9954 case RID_IS_EMPTY:
9955 kind = CPTK_IS_EMPTY;
9956 break;
9957 case RID_IS_ENUM:
9958 kind = CPTK_IS_ENUM;
9959 break;
9960 case RID_IS_FINAL:
9961 kind = CPTK_IS_FINAL;
9962 break;
9963 case RID_IS_LITERAL_TYPE:
9964 kind = CPTK_IS_LITERAL_TYPE;
9965 break;
9966 case RID_IS_POD:
9967 kind = CPTK_IS_POD;
9968 break;
9969 case RID_IS_POLYMORPHIC:
9970 kind = CPTK_IS_POLYMORPHIC;
9971 break;
9972 case RID_IS_SAME_AS:
9973 kind = CPTK_IS_SAME_AS;
9974 binary = true;
9975 break;
9976 case RID_IS_STD_LAYOUT:
9977 kind = CPTK_IS_STD_LAYOUT;
9978 break;
9979 case RID_IS_TRIVIAL:
9980 kind = CPTK_IS_TRIVIAL;
9981 break;
9982 case RID_IS_TRIVIALLY_ASSIGNABLE:
9983 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9984 binary = true;
9985 break;
9986 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9987 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9988 variadic = true;
9989 break;
9990 case RID_IS_TRIVIALLY_COPYABLE:
9991 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9992 break;
9993 case RID_IS_UNION:
9994 kind = CPTK_IS_UNION;
9995 break;
9996 case RID_UNDERLYING_TYPE:
9997 kind = CPTK_UNDERLYING_TYPE;
9998 break;
9999 case RID_BASES:
10000 kind = CPTK_BASES;
10001 break;
10002 case RID_DIRECT_BASES:
10003 kind = CPTK_DIRECT_BASES;
10004 break;
10005 case RID_IS_ASSIGNABLE:
10006 kind = CPTK_IS_ASSIGNABLE;
10007 binary = true;
10008 break;
10009 case RID_IS_CONSTRUCTIBLE:
10010 kind = CPTK_IS_CONSTRUCTIBLE;
10011 variadic = true;
10012 break;
10013 default:
10014 gcc_unreachable ();
10017 /* Get location of initial token. */
10018 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10020 /* Consume the token. */
10021 cp_lexer_consume_token (parser->lexer);
10023 matching_parens parens;
10024 parens.require_open (parser);
10027 type_id_in_expr_sentinel s (parser);
10028 type1 = cp_parser_type_id (parser);
10031 if (type1 == error_mark_node)
10032 return error_mark_node;
10034 if (binary)
10036 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10039 type_id_in_expr_sentinel s (parser);
10040 type2 = cp_parser_type_id (parser);
10043 if (type2 == error_mark_node)
10044 return error_mark_node;
10046 else if (variadic)
10048 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10050 cp_lexer_consume_token (parser->lexer);
10051 tree elt = cp_parser_type_id (parser);
10052 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10054 cp_lexer_consume_token (parser->lexer);
10055 elt = make_pack_expansion (elt);
10057 if (elt == error_mark_node)
10058 return error_mark_node;
10059 type2 = tree_cons (NULL_TREE, elt, type2);
10063 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10064 parens.require_close (parser);
10066 /* Construct a location of the form:
10067 __is_trivially_copyable(_Tp)
10068 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10069 with start == caret, finishing at the close-paren. */
10070 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10072 /* Complete the trait expression, which may mean either processing
10073 the trait expr now or saving it for template instantiation. */
10074 switch (kind)
10076 case CPTK_UNDERLYING_TYPE:
10077 return cp_expr (finish_underlying_type (type1), trait_loc);
10078 case CPTK_BASES:
10079 return cp_expr (finish_bases (type1, false), trait_loc);
10080 case CPTK_DIRECT_BASES:
10081 return cp_expr (finish_bases (type1, true), trait_loc);
10082 default:
10083 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10087 /* Parse a lambda expression.
10089 lambda-expression:
10090 lambda-introducer lambda-declarator [opt] compound-statement
10092 Returns a representation of the expression. */
10094 static cp_expr
10095 cp_parser_lambda_expression (cp_parser* parser)
10097 tree lambda_expr = build_lambda_expr ();
10098 tree type;
10099 bool ok = true;
10100 cp_token *token = cp_lexer_peek_token (parser->lexer);
10101 cp_token_position start = 0;
10103 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10105 if (cp_unevaluated_operand)
10107 if (!token->error_reported)
10109 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10110 "lambda-expression in unevaluated context");
10111 token->error_reported = true;
10113 ok = false;
10115 else if (parser->in_template_argument_list_p)
10117 if (!token->error_reported)
10119 error_at (token->location, "lambda-expression in template-argument");
10120 token->error_reported = true;
10122 ok = false;
10125 /* We may be in the middle of deferred access check. Disable
10126 it now. */
10127 push_deferring_access_checks (dk_no_deferred);
10129 cp_parser_lambda_introducer (parser, lambda_expr);
10131 type = begin_lambda_type (lambda_expr);
10132 if (type == error_mark_node)
10133 return error_mark_node;
10135 record_lambda_scope (lambda_expr);
10137 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10138 determine_visibility (TYPE_NAME (type));
10140 /* Now that we've started the type, add the capture fields for any
10141 explicit captures. */
10142 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10145 /* Inside the class, surrounding template-parameter-lists do not apply. */
10146 unsigned int saved_num_template_parameter_lists
10147 = parser->num_template_parameter_lists;
10148 unsigned char in_statement = parser->in_statement;
10149 bool in_switch_statement_p = parser->in_switch_statement_p;
10150 bool fully_implicit_function_template_p
10151 = parser->fully_implicit_function_template_p;
10152 tree implicit_template_parms = parser->implicit_template_parms;
10153 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10154 bool auto_is_implicit_function_template_parm_p
10155 = parser->auto_is_implicit_function_template_parm_p;
10157 parser->num_template_parameter_lists = 0;
10158 parser->in_statement = 0;
10159 parser->in_switch_statement_p = false;
10160 parser->fully_implicit_function_template_p = false;
10161 parser->implicit_template_parms = 0;
10162 parser->implicit_template_scope = 0;
10163 parser->auto_is_implicit_function_template_parm_p = false;
10165 /* By virtue of defining a local class, a lambda expression has access to
10166 the private variables of enclosing classes. */
10168 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10170 if (ok && cp_parser_error_occurred (parser))
10171 ok = false;
10173 if (ok)
10175 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10176 && cp_parser_start_tentative_firewall (parser))
10177 start = token;
10178 cp_parser_lambda_body (parser, lambda_expr);
10180 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10182 if (cp_parser_skip_to_closing_brace (parser))
10183 cp_lexer_consume_token (parser->lexer);
10186 /* The capture list was built up in reverse order; fix that now. */
10187 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10188 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10190 if (ok)
10191 maybe_add_lambda_conv_op (type);
10193 type = finish_struct (type, /*attributes=*/NULL_TREE);
10195 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10196 parser->in_statement = in_statement;
10197 parser->in_switch_statement_p = in_switch_statement_p;
10198 parser->fully_implicit_function_template_p
10199 = fully_implicit_function_template_p;
10200 parser->implicit_template_parms = implicit_template_parms;
10201 parser->implicit_template_scope = implicit_template_scope;
10202 parser->auto_is_implicit_function_template_parm_p
10203 = auto_is_implicit_function_template_parm_p;
10206 /* This field is only used during parsing of the lambda. */
10207 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10209 /* This lambda shouldn't have any proxies left at this point. */
10210 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10211 /* And now that we're done, push proxies for an enclosing lambda. */
10212 insert_pending_capture_proxies ();
10214 if (ok)
10215 lambda_expr = build_lambda_object (lambda_expr);
10216 else
10217 lambda_expr = error_mark_node;
10219 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10221 pop_deferring_access_checks ();
10223 return lambda_expr;
10226 /* Parse the beginning of a lambda expression.
10228 lambda-introducer:
10229 [ lambda-capture [opt] ]
10231 LAMBDA_EXPR is the current representation of the lambda expression. */
10233 static void
10234 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10236 /* Need commas after the first capture. */
10237 bool first = true;
10239 /* Eat the leading `['. */
10240 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10242 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10243 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10244 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10245 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10246 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10247 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10249 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10251 cp_lexer_consume_token (parser->lexer);
10252 first = false;
10255 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10257 cp_token* capture_token;
10258 tree capture_id;
10259 tree capture_init_expr;
10260 cp_id_kind idk = CP_ID_KIND_NONE;
10261 bool explicit_init_p = false;
10263 enum capture_kind_type
10265 BY_COPY,
10266 BY_REFERENCE
10268 enum capture_kind_type capture_kind = BY_COPY;
10270 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10272 error ("expected end of capture-list");
10273 return;
10276 if (first)
10277 first = false;
10278 else
10279 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10281 /* Possibly capture `this'. */
10282 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10284 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10285 if (cxx_dialect < cxx2a
10286 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10287 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10288 "with by-copy capture default");
10289 cp_lexer_consume_token (parser->lexer);
10290 add_capture (lambda_expr,
10291 /*id=*/this_identifier,
10292 /*initializer=*/finish_this_expr (),
10293 /*by_reference_p=*/true,
10294 explicit_init_p);
10295 continue;
10298 /* Possibly capture `*this'. */
10299 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10300 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10302 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10303 if (cxx_dialect < cxx17)
10304 pedwarn (loc, 0, "%<*this%> capture only available with "
10305 "-std=c++17 or -std=gnu++17");
10306 cp_lexer_consume_token (parser->lexer);
10307 cp_lexer_consume_token (parser->lexer);
10308 add_capture (lambda_expr,
10309 /*id=*/this_identifier,
10310 /*initializer=*/finish_this_expr (),
10311 /*by_reference_p=*/false,
10312 explicit_init_p);
10313 continue;
10316 /* Remember whether we want to capture as a reference or not. */
10317 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10319 capture_kind = BY_REFERENCE;
10320 cp_lexer_consume_token (parser->lexer);
10323 /* Get the identifier. */
10324 capture_token = cp_lexer_peek_token (parser->lexer);
10325 capture_id = cp_parser_identifier (parser);
10327 if (capture_id == error_mark_node)
10328 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10329 delimiters, but I modified this to stop on unnested ']' as well. It
10330 was already changed to stop on unnested '}', so the
10331 "closing_parenthesis" name is no more misleading with my change. */
10333 cp_parser_skip_to_closing_parenthesis (parser,
10334 /*recovering=*/true,
10335 /*or_comma=*/true,
10336 /*consume_paren=*/true);
10337 break;
10340 /* Find the initializer for this capture. */
10341 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10342 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10343 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10345 bool direct, non_constant;
10346 /* An explicit initializer exists. */
10347 if (cxx_dialect < cxx14)
10348 pedwarn (input_location, 0,
10349 "lambda capture initializers "
10350 "only available with -std=c++14 or -std=gnu++14");
10351 capture_init_expr = cp_parser_initializer (parser, &direct,
10352 &non_constant, true);
10353 explicit_init_p = true;
10354 if (capture_init_expr == NULL_TREE)
10356 error ("empty initializer for lambda init-capture");
10357 capture_init_expr = error_mark_node;
10360 else
10362 const char* error_msg;
10364 /* Turn the identifier into an id-expression. */
10365 capture_init_expr
10366 = cp_parser_lookup_name_simple (parser, capture_id,
10367 capture_token->location);
10369 if (capture_init_expr == error_mark_node)
10371 unqualified_name_lookup_error (capture_id);
10372 continue;
10374 else if (!VAR_P (capture_init_expr)
10375 && TREE_CODE (capture_init_expr) != PARM_DECL)
10377 error_at (capture_token->location,
10378 "capture of non-variable %qE",
10379 capture_init_expr);
10380 if (DECL_P (capture_init_expr))
10381 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10382 "%q#D declared here", capture_init_expr);
10383 continue;
10385 if (VAR_P (capture_init_expr)
10386 && decl_storage_duration (capture_init_expr) != dk_auto)
10388 if (pedwarn (capture_token->location, 0, "capture of variable "
10389 "%qD with non-automatic storage duration",
10390 capture_init_expr))
10391 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10392 "%q#D declared here", capture_init_expr);
10393 continue;
10396 capture_init_expr
10397 = finish_id_expression
10398 (capture_id,
10399 capture_init_expr,
10400 parser->scope,
10401 &idk,
10402 /*integral_constant_expression_p=*/false,
10403 /*allow_non_integral_constant_expression_p=*/false,
10404 /*non_integral_constant_expression_p=*/NULL,
10405 /*template_p=*/false,
10406 /*done=*/true,
10407 /*address_p=*/false,
10408 /*template_arg_p=*/false,
10409 &error_msg,
10410 capture_token->location);
10412 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10414 cp_lexer_consume_token (parser->lexer);
10415 capture_init_expr = make_pack_expansion (capture_init_expr);
10419 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10420 && !explicit_init_p)
10422 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10423 && capture_kind == BY_COPY)
10424 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10425 "of %qD redundant with by-copy capture default",
10426 capture_id);
10427 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10428 && capture_kind == BY_REFERENCE)
10429 pedwarn (capture_token->location, 0, "explicit by-reference "
10430 "capture of %qD redundant with by-reference capture "
10431 "default", capture_id);
10434 add_capture (lambda_expr,
10435 capture_id,
10436 capture_init_expr,
10437 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10438 explicit_init_p);
10440 /* If there is any qualification still in effect, clear it
10441 now; we will be starting fresh with the next capture. */
10442 parser->scope = NULL_TREE;
10443 parser->qualifying_scope = NULL_TREE;
10444 parser->object_scope = NULL_TREE;
10447 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10450 /* Parse the (optional) middle of a lambda expression.
10452 lambda-declarator:
10453 < template-parameter-list [opt] >
10454 ( parameter-declaration-clause [opt] )
10455 attribute-specifier [opt]
10456 decl-specifier-seq [opt]
10457 exception-specification [opt]
10458 lambda-return-type-clause [opt]
10460 LAMBDA_EXPR is the current representation of the lambda expression. */
10462 static bool
10463 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10465 /* 5.1.1.4 of the standard says:
10466 If a lambda-expression does not include a lambda-declarator, it is as if
10467 the lambda-declarator were ().
10468 This means an empty parameter list, no attributes, and no exception
10469 specification. */
10470 tree param_list = void_list_node;
10471 tree attributes = NULL_TREE;
10472 tree exception_spec = NULL_TREE;
10473 tree template_param_list = NULL_TREE;
10474 tree tx_qual = NULL_TREE;
10475 tree return_type = NULL_TREE;
10476 cp_decl_specifier_seq lambda_specs;
10477 clear_decl_specs (&lambda_specs);
10479 /* The template-parameter-list is optional, but must begin with
10480 an opening angle if present. */
10481 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10483 if (cxx_dialect < cxx14)
10484 pedwarn (parser->lexer->next_token->location, 0,
10485 "lambda templates are only available with "
10486 "-std=c++14 or -std=gnu++14");
10487 else if (cxx_dialect < cxx2a)
10488 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10489 "lambda templates are only available with "
10490 "-std=c++2a or -std=gnu++2a");
10492 cp_lexer_consume_token (parser->lexer);
10494 template_param_list = cp_parser_template_parameter_list (parser);
10496 cp_parser_skip_to_end_of_template_parameter_list (parser);
10498 /* We just processed one more parameter list. */
10499 ++parser->num_template_parameter_lists;
10502 /* The parameter-declaration-clause is optional (unless
10503 template-parameter-list was given), but must begin with an
10504 opening parenthesis if present. */
10505 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10507 matching_parens parens;
10508 parens.consume_open (parser);
10510 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10512 /* Parse parameters. */
10513 param_list = cp_parser_parameter_declaration_clause (parser);
10515 /* Default arguments shall not be specified in the
10516 parameter-declaration-clause of a lambda-declarator. */
10517 if (cxx_dialect < cxx14)
10518 for (tree t = param_list; t; t = TREE_CHAIN (t))
10519 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10520 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10521 "default argument specified for lambda parameter");
10523 parens.require_close (parser);
10525 attributes = cp_parser_attributes_opt (parser);
10527 /* In the decl-specifier-seq of the lambda-declarator, each
10528 decl-specifier shall either be mutable or constexpr. */
10529 int declares_class_or_enum;
10530 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10531 cp_parser_decl_specifier_seq (parser,
10532 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10533 &lambda_specs, &declares_class_or_enum);
10534 if (lambda_specs.storage_class == sc_mutable)
10536 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10537 if (lambda_specs.conflicting_specifiers_p)
10538 error_at (lambda_specs.locations[ds_storage_class],
10539 "duplicate %<mutable%>");
10542 tx_qual = cp_parser_tx_qualifier_opt (parser);
10544 /* Parse optional exception specification. */
10545 exception_spec = cp_parser_exception_specification_opt (parser);
10547 /* Parse optional trailing return type. */
10548 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10550 cp_lexer_consume_token (parser->lexer);
10551 return_type = cp_parser_trailing_type_id (parser);
10554 /* The function parameters must be in scope all the way until after the
10555 trailing-return-type in case of decltype. */
10556 pop_bindings_and_leave_scope ();
10558 else if (template_param_list != NULL_TREE) // generate diagnostic
10559 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10561 /* Create the function call operator.
10563 Messing with declarators like this is no uglier than building up the
10564 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10565 other code. */
10567 cp_decl_specifier_seq return_type_specs;
10568 cp_declarator* declarator;
10569 tree fco;
10570 int quals;
10571 void *p;
10573 clear_decl_specs (&return_type_specs);
10574 return_type_specs.type = make_auto ();
10576 if (lambda_specs.locations[ds_constexpr])
10578 if (cxx_dialect >= cxx17)
10579 return_type_specs.locations[ds_constexpr]
10580 = lambda_specs.locations[ds_constexpr];
10581 else
10582 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10583 "lambda only available with -std=c++17 or -std=gnu++17");
10586 p = obstack_alloc (&declarator_obstack, 0);
10588 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10590 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10591 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10592 declarator = make_call_declarator (declarator, param_list, quals,
10593 VIRT_SPEC_UNSPECIFIED,
10594 REF_QUAL_NONE,
10595 tx_qual,
10596 exception_spec,
10597 /*late_return_type=*/NULL_TREE,
10598 /*requires_clause*/NULL_TREE);
10599 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10600 if (return_type)
10601 declarator->u.function.late_return_type = return_type;
10603 fco = grokmethod (&return_type_specs,
10604 declarator,
10605 attributes);
10606 if (fco != error_mark_node)
10608 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10609 DECL_ARTIFICIAL (fco) = 1;
10610 /* Give the object parameter a different name. */
10611 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10613 if (template_param_list)
10615 fco = finish_member_template_decl (fco);
10616 finish_template_decl (template_param_list);
10617 --parser->num_template_parameter_lists;
10619 else if (parser->fully_implicit_function_template_p)
10620 fco = finish_fully_implicit_template (parser, fco);
10622 finish_member_declaration (fco);
10624 obstack_free (&declarator_obstack, p);
10626 return (fco != error_mark_node);
10630 /* Parse the body of a lambda expression, which is simply
10632 compound-statement
10634 but which requires special handling.
10635 LAMBDA_EXPR is the current representation of the lambda expression. */
10637 static void
10638 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10640 bool nested = (current_function_decl != NULL_TREE);
10641 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10642 bool in_function_body = parser->in_function_body;
10644 if (nested)
10645 push_function_context ();
10646 else
10647 /* Still increment function_depth so that we don't GC in the
10648 middle of an expression. */
10649 ++function_depth;
10651 vec<tree> omp_privatization_save;
10652 save_omp_privatization_clauses (omp_privatization_save);
10653 /* Clear this in case we're in the middle of a default argument. */
10654 parser->local_variables_forbidden_p = false;
10655 parser->in_function_body = true;
10658 local_specialization_stack s (lss_copy);
10659 tree fco = lambda_function (lambda_expr);
10660 tree body = start_lambda_function (fco, lambda_expr);
10661 matching_braces braces;
10663 if (braces.require_open (parser))
10665 tree compound_stmt = begin_compound_stmt (0);
10667 /* Originally C++11 required us to peek for 'return expr'; and
10668 process it specially here to deduce the return type. N3638
10669 removed the need for that. */
10671 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10672 cp_parser_label_declaration (parser);
10673 cp_parser_statement_seq_opt (parser, NULL_TREE);
10674 braces.require_close (parser);
10676 finish_compound_stmt (compound_stmt);
10679 finish_lambda_function (body);
10682 restore_omp_privatization_clauses (omp_privatization_save);
10683 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10684 parser->in_function_body = in_function_body;
10685 if (nested)
10686 pop_function_context();
10687 else
10688 --function_depth;
10691 /* Statements [gram.stmt.stmt] */
10693 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10695 static void
10696 add_debug_begin_stmt (location_t loc)
10698 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10699 return;
10700 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10701 /* A concept is never expanded normally. */
10702 return;
10704 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10705 SET_EXPR_LOCATION (stmt, loc);
10706 add_stmt (stmt);
10709 /* Parse a statement.
10711 statement:
10712 labeled-statement
10713 expression-statement
10714 compound-statement
10715 selection-statement
10716 iteration-statement
10717 jump-statement
10718 declaration-statement
10719 try-block
10721 C++11:
10723 statement:
10724 labeled-statement
10725 attribute-specifier-seq (opt) expression-statement
10726 attribute-specifier-seq (opt) compound-statement
10727 attribute-specifier-seq (opt) selection-statement
10728 attribute-specifier-seq (opt) iteration-statement
10729 attribute-specifier-seq (opt) jump-statement
10730 declaration-statement
10731 attribute-specifier-seq (opt) try-block
10733 init-statement:
10734 expression-statement
10735 simple-declaration
10737 TM Extension:
10739 statement:
10740 atomic-statement
10742 IN_COMPOUND is true when the statement is nested inside a
10743 cp_parser_compound_statement; this matters for certain pragmas.
10745 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10746 is a (possibly labeled) if statement which is not enclosed in braces
10747 and has an else clause. This is used to implement -Wparentheses.
10749 CHAIN is a vector of if-else-if conditions. */
10751 static void
10752 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10753 bool in_compound, bool *if_p, vec<tree> *chain,
10754 location_t *loc_after_labels)
10756 tree statement, std_attrs = NULL_TREE;
10757 cp_token *token;
10758 location_t statement_location, attrs_location;
10760 restart:
10761 if (if_p != NULL)
10762 *if_p = false;
10763 /* There is no statement yet. */
10764 statement = NULL_TREE;
10766 saved_token_sentinel saved_tokens (parser->lexer);
10767 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10768 if (c_dialect_objc ())
10769 /* In obj-c++, seeing '[[' might be the either the beginning of
10770 c++11 attributes, or a nested objc-message-expression. So
10771 let's parse the c++11 attributes tentatively. */
10772 cp_parser_parse_tentatively (parser);
10773 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10774 if (c_dialect_objc ())
10776 if (!cp_parser_parse_definitely (parser))
10777 std_attrs = NULL_TREE;
10780 /* Peek at the next token. */
10781 token = cp_lexer_peek_token (parser->lexer);
10782 /* Remember the location of the first token in the statement. */
10783 statement_location = token->location;
10784 add_debug_begin_stmt (statement_location);
10785 /* If this is a keyword, then that will often determine what kind of
10786 statement we have. */
10787 if (token->type == CPP_KEYWORD)
10789 enum rid keyword = token->keyword;
10791 switch (keyword)
10793 case RID_CASE:
10794 case RID_DEFAULT:
10795 /* Looks like a labeled-statement with a case label.
10796 Parse the label, and then use tail recursion to parse
10797 the statement. */
10798 cp_parser_label_for_labeled_statement (parser, std_attrs);
10799 in_compound = false;
10800 goto restart;
10802 case RID_IF:
10803 case RID_SWITCH:
10804 statement = cp_parser_selection_statement (parser, if_p, chain);
10805 break;
10807 case RID_WHILE:
10808 case RID_DO:
10809 case RID_FOR:
10810 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10811 break;
10813 case RID_BREAK:
10814 case RID_CONTINUE:
10815 case RID_RETURN:
10816 case RID_GOTO:
10817 statement = cp_parser_jump_statement (parser);
10818 break;
10820 /* Objective-C++ exception-handling constructs. */
10821 case RID_AT_TRY:
10822 case RID_AT_CATCH:
10823 case RID_AT_FINALLY:
10824 case RID_AT_SYNCHRONIZED:
10825 case RID_AT_THROW:
10826 statement = cp_parser_objc_statement (parser);
10827 break;
10829 case RID_TRY:
10830 statement = cp_parser_try_block (parser);
10831 break;
10833 case RID_NAMESPACE:
10834 /* This must be a namespace alias definition. */
10835 cp_parser_declaration_statement (parser);
10836 return;
10838 case RID_TRANSACTION_ATOMIC:
10839 case RID_TRANSACTION_RELAXED:
10840 case RID_SYNCHRONIZED:
10841 case RID_ATOMIC_NOEXCEPT:
10842 case RID_ATOMIC_CANCEL:
10843 statement = cp_parser_transaction (parser, token);
10844 break;
10845 case RID_TRANSACTION_CANCEL:
10846 statement = cp_parser_transaction_cancel (parser);
10847 break;
10849 default:
10850 /* It might be a keyword like `int' that can start a
10851 declaration-statement. */
10852 break;
10855 else if (token->type == CPP_NAME)
10857 /* If the next token is a `:', then we are looking at a
10858 labeled-statement. */
10859 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10860 if (token->type == CPP_COLON)
10862 /* Looks like a labeled-statement with an ordinary label.
10863 Parse the label, and then use tail recursion to parse
10864 the statement. */
10866 cp_parser_label_for_labeled_statement (parser, std_attrs);
10867 in_compound = false;
10868 goto restart;
10871 /* Anything that starts with a `{' must be a compound-statement. */
10872 else if (token->type == CPP_OPEN_BRACE)
10873 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10874 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10875 a statement all its own. */
10876 else if (token->type == CPP_PRAGMA)
10878 /* Only certain OpenMP pragmas are attached to statements, and thus
10879 are considered statements themselves. All others are not. In
10880 the context of a compound, accept the pragma as a "statement" and
10881 return so that we can check for a close brace. Otherwise we
10882 require a real statement and must go back and read one. */
10883 if (in_compound)
10884 cp_parser_pragma (parser, pragma_compound, if_p);
10885 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10886 goto restart;
10887 return;
10889 else if (token->type == CPP_EOF)
10891 cp_parser_error (parser, "expected statement");
10892 return;
10895 /* Everything else must be a declaration-statement or an
10896 expression-statement. Try for the declaration-statement
10897 first, unless we are looking at a `;', in which case we know that
10898 we have an expression-statement. */
10899 if (!statement)
10901 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10903 if (std_attrs != NULL_TREE)
10905 /* Attributes should be parsed as part of the the
10906 declaration, so let's un-parse them. */
10907 saved_tokens.rollback();
10908 std_attrs = NULL_TREE;
10911 cp_parser_parse_tentatively (parser);
10912 /* Try to parse the declaration-statement. */
10913 cp_parser_declaration_statement (parser);
10914 /* If that worked, we're done. */
10915 if (cp_parser_parse_definitely (parser))
10916 return;
10918 /* All preceding labels have been parsed at this point. */
10919 if (loc_after_labels != NULL)
10920 *loc_after_labels = statement_location;
10922 /* Look for an expression-statement instead. */
10923 statement = cp_parser_expression_statement (parser, in_statement_expr);
10925 /* Handle [[fallthrough]];. */
10926 if (attribute_fallthrough_p (std_attrs))
10928 /* The next token after the fallthrough attribute is ';'. */
10929 if (statement == NULL_TREE)
10931 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10932 statement = build_call_expr_internal_loc (statement_location,
10933 IFN_FALLTHROUGH,
10934 void_type_node, 0);
10935 finish_expr_stmt (statement);
10937 else
10938 warning_at (statement_location, OPT_Wattributes,
10939 "%<fallthrough%> attribute not followed by %<;%>");
10940 std_attrs = NULL_TREE;
10944 /* Set the line number for the statement. */
10945 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10946 SET_EXPR_LOCATION (statement, statement_location);
10948 /* Allow "[[fallthrough]];", but warn otherwise. */
10949 if (std_attrs != NULL_TREE)
10950 warning_at (attrs_location,
10951 OPT_Wattributes,
10952 "attributes at the beginning of statement are ignored");
10955 /* Append ATTR to attribute list ATTRS. */
10957 static tree
10958 attr_chainon (tree attrs, tree attr)
10960 if (attrs == error_mark_node)
10961 return error_mark_node;
10962 if (attr == error_mark_node)
10963 return error_mark_node;
10964 return chainon (attrs, attr);
10967 /* Parse the label for a labeled-statement, i.e.
10969 identifier :
10970 case constant-expression :
10971 default :
10973 GNU Extension:
10974 case constant-expression ... constant-expression : statement
10976 When a label is parsed without errors, the label is added to the
10977 parse tree by the finish_* functions, so this function doesn't
10978 have to return the label. */
10980 static void
10981 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10983 cp_token *token;
10984 tree label = NULL_TREE;
10985 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10987 /* The next token should be an identifier. */
10988 token = cp_lexer_peek_token (parser->lexer);
10989 if (token->type != CPP_NAME
10990 && token->type != CPP_KEYWORD)
10992 cp_parser_error (parser, "expected labeled-statement");
10993 return;
10996 /* Remember whether this case or a user-defined label is allowed to fall
10997 through to. */
10998 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11000 parser->colon_corrects_to_scope_p = false;
11001 switch (token->keyword)
11003 case RID_CASE:
11005 tree expr, expr_hi;
11006 cp_token *ellipsis;
11008 /* Consume the `case' token. */
11009 cp_lexer_consume_token (parser->lexer);
11010 /* Parse the constant-expression. */
11011 expr = cp_parser_constant_expression (parser);
11012 if (check_for_bare_parameter_packs (expr))
11013 expr = error_mark_node;
11015 ellipsis = cp_lexer_peek_token (parser->lexer);
11016 if (ellipsis->type == CPP_ELLIPSIS)
11018 /* Consume the `...' token. */
11019 cp_lexer_consume_token (parser->lexer);
11020 expr_hi = cp_parser_constant_expression (parser);
11021 if (check_for_bare_parameter_packs (expr_hi))
11022 expr_hi = error_mark_node;
11024 /* We don't need to emit warnings here, as the common code
11025 will do this for us. */
11027 else
11028 expr_hi = NULL_TREE;
11030 if (parser->in_switch_statement_p)
11032 tree l = finish_case_label (token->location, expr, expr_hi);
11033 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11034 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11036 else
11037 error_at (token->location,
11038 "case label %qE not within a switch statement",
11039 expr);
11041 break;
11043 case RID_DEFAULT:
11044 /* Consume the `default' token. */
11045 cp_lexer_consume_token (parser->lexer);
11047 if (parser->in_switch_statement_p)
11049 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11050 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11051 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11053 else
11054 error_at (token->location, "case label not within a switch statement");
11055 break;
11057 default:
11058 /* Anything else must be an ordinary label. */
11059 label = finish_label_stmt (cp_parser_identifier (parser));
11060 if (label && TREE_CODE (label) == LABEL_DECL)
11061 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11062 break;
11065 /* Require the `:' token. */
11066 cp_parser_require (parser, CPP_COLON, RT_COLON);
11068 /* An ordinary label may optionally be followed by attributes.
11069 However, this is only permitted if the attributes are then
11070 followed by a semicolon. This is because, for backward
11071 compatibility, when parsing
11072 lab: __attribute__ ((unused)) int i;
11073 we want the attribute to attach to "i", not "lab". */
11074 if (label != NULL_TREE
11075 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11077 tree attrs;
11078 cp_parser_parse_tentatively (parser);
11079 attrs = cp_parser_gnu_attributes_opt (parser);
11080 if (attrs == NULL_TREE
11081 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11082 cp_parser_abort_tentative_parse (parser);
11083 else if (!cp_parser_parse_definitely (parser))
11085 else
11086 attributes = attr_chainon (attributes, attrs);
11089 if (attributes != NULL_TREE)
11090 cplus_decl_attributes (&label, attributes, 0);
11092 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11095 /* Parse an expression-statement.
11097 expression-statement:
11098 expression [opt] ;
11100 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11101 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11102 indicates whether this expression-statement is part of an
11103 expression statement. */
11105 static tree
11106 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11108 tree statement = NULL_TREE;
11109 cp_token *token = cp_lexer_peek_token (parser->lexer);
11110 location_t loc = token->location;
11112 /* There might be attribute fallthrough. */
11113 tree attr = cp_parser_gnu_attributes_opt (parser);
11115 /* If the next token is a ';', then there is no expression
11116 statement. */
11117 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11119 statement = cp_parser_expression (parser);
11120 if (statement == error_mark_node
11121 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11123 cp_parser_skip_to_end_of_block_or_statement (parser);
11124 return error_mark_node;
11128 /* Handle [[fallthrough]];. */
11129 if (attribute_fallthrough_p (attr))
11131 /* The next token after the fallthrough attribute is ';'. */
11132 if (statement == NULL_TREE)
11133 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11134 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11135 void_type_node, 0);
11136 else
11137 warning_at (loc, OPT_Wattributes,
11138 "%<fallthrough%> attribute not followed by %<;%>");
11139 attr = NULL_TREE;
11142 /* Allow "[[fallthrough]];", but warn otherwise. */
11143 if (attr != NULL_TREE)
11144 warning_at (loc, OPT_Wattributes,
11145 "attributes at the beginning of statement are ignored");
11147 /* Give a helpful message for "A<T>::type t;" and the like. */
11148 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11149 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11151 if (TREE_CODE (statement) == SCOPE_REF)
11152 error_at (token->location, "need %<typename%> before %qE because "
11153 "%qT is a dependent scope",
11154 statement, TREE_OPERAND (statement, 0));
11155 else if (is_overloaded_fn (statement)
11156 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11158 /* A::A a; */
11159 tree fn = get_first_fn (statement);
11160 error_at (token->location,
11161 "%<%T::%D%> names the constructor, not the type",
11162 DECL_CONTEXT (fn), DECL_NAME (fn));
11166 /* Consume the final `;'. */
11167 cp_parser_consume_semicolon_at_end_of_statement (parser);
11169 if (in_statement_expr
11170 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11171 /* This is the final expression statement of a statement
11172 expression. */
11173 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11174 else if (statement)
11175 statement = finish_expr_stmt (statement);
11177 return statement;
11180 /* Parse a compound-statement.
11182 compound-statement:
11183 { statement-seq [opt] }
11185 GNU extension:
11187 compound-statement:
11188 { label-declaration-seq [opt] statement-seq [opt] }
11190 label-declaration-seq:
11191 label-declaration
11192 label-declaration-seq label-declaration
11194 Returns a tree representing the statement. */
11196 static tree
11197 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11198 int bcs_flags, bool function_body)
11200 tree compound_stmt;
11201 matching_braces braces;
11203 /* Consume the `{'. */
11204 if (!braces.require_open (parser))
11205 return error_mark_node;
11206 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11207 && !function_body && cxx_dialect < cxx14)
11208 pedwarn (input_location, OPT_Wpedantic,
11209 "compound-statement in %<constexpr%> function");
11210 /* Begin the compound-statement. */
11211 compound_stmt = begin_compound_stmt (bcs_flags);
11212 /* If the next keyword is `__label__' we have a label declaration. */
11213 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11214 cp_parser_label_declaration (parser);
11215 /* Parse an (optional) statement-seq. */
11216 cp_parser_statement_seq_opt (parser, in_statement_expr);
11217 /* Finish the compound-statement. */
11218 finish_compound_stmt (compound_stmt);
11219 /* Consume the `}'. */
11220 braces.require_close (parser);
11222 return compound_stmt;
11225 /* Parse an (optional) statement-seq.
11227 statement-seq:
11228 statement
11229 statement-seq [opt] statement */
11231 static void
11232 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11234 /* Scan statements until there aren't any more. */
11235 while (true)
11237 cp_token *token = cp_lexer_peek_token (parser->lexer);
11239 /* If we are looking at a `}', then we have run out of
11240 statements; the same is true if we have reached the end
11241 of file, or have stumbled upon a stray '@end'. */
11242 if (token->type == CPP_CLOSE_BRACE
11243 || token->type == CPP_EOF
11244 || token->type == CPP_PRAGMA_EOL
11245 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11246 break;
11248 /* If we are in a compound statement and find 'else' then
11249 something went wrong. */
11250 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11252 if (parser->in_statement & IN_IF_STMT)
11253 break;
11254 else
11256 token = cp_lexer_consume_token (parser->lexer);
11257 error_at (token->location, "%<else%> without a previous %<if%>");
11261 /* Parse the statement. */
11262 cp_parser_statement (parser, in_statement_expr, true, NULL);
11266 /* Return true if this is the C++20 version of range-based-for with
11267 init-statement. */
11269 static bool
11270 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11272 bool r = false;
11274 /* Save tokens so that we can put them back. */
11275 cp_lexer_save_tokens (parser->lexer);
11277 /* There has to be an unnested ; followed by an unnested :. */
11278 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11279 /*recovering=*/false,
11280 CPP_SEMICOLON,
11281 /*consume_paren=*/false) != -1)
11282 goto out;
11284 /* We found the semicolon, eat it now. */
11285 cp_lexer_consume_token (parser->lexer);
11287 /* Now look for ':' that is not nested in () or {}. */
11288 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11289 /*recovering=*/false,
11290 CPP_COLON,
11291 /*consume_paren=*/false) == -1);
11293 out:
11294 /* Roll back the tokens we skipped. */
11295 cp_lexer_rollback_tokens (parser->lexer);
11297 return r;
11300 /* Return true if we're looking at (init; cond), false otherwise. */
11302 static bool
11303 cp_parser_init_statement_p (cp_parser *parser)
11305 /* Save tokens so that we can put them back. */
11306 cp_lexer_save_tokens (parser->lexer);
11308 /* Look for ';' that is not nested in () or {}. */
11309 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11310 /*recovering=*/false,
11311 CPP_SEMICOLON,
11312 /*consume_paren=*/false);
11314 /* Roll back the tokens we skipped. */
11315 cp_lexer_rollback_tokens (parser->lexer);
11317 return ret == -1;
11320 /* Parse a selection-statement.
11322 selection-statement:
11323 if ( init-statement [opt] condition ) statement
11324 if ( init-statement [opt] condition ) statement else statement
11325 switch ( init-statement [opt] condition ) statement
11327 Returns the new IF_STMT or SWITCH_STMT.
11329 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11330 is a (possibly labeled) if statement which is not enclosed in
11331 braces and has an else clause. This is used to implement
11332 -Wparentheses.
11334 CHAIN is a vector of if-else-if conditions. This is used to implement
11335 -Wduplicated-cond. */
11337 static tree
11338 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11339 vec<tree> *chain)
11341 cp_token *token;
11342 enum rid keyword;
11343 token_indent_info guard_tinfo;
11345 if (if_p != NULL)
11346 *if_p = false;
11348 /* Peek at the next token. */
11349 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11350 guard_tinfo = get_token_indent_info (token);
11352 /* See what kind of keyword it is. */
11353 keyword = token->keyword;
11354 switch (keyword)
11356 case RID_IF:
11357 case RID_SWITCH:
11359 tree statement;
11360 tree condition;
11362 bool cx = false;
11363 if (keyword == RID_IF
11364 && cp_lexer_next_token_is_keyword (parser->lexer,
11365 RID_CONSTEXPR))
11367 cx = true;
11368 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11369 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11370 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11371 "with -std=c++17 or -std=gnu++17");
11374 /* Look for the `('. */
11375 matching_parens parens;
11376 if (!parens.require_open (parser))
11378 cp_parser_skip_to_end_of_statement (parser);
11379 return error_mark_node;
11382 /* Begin the selection-statement. */
11383 if (keyword == RID_IF)
11385 statement = begin_if_stmt ();
11386 IF_STMT_CONSTEXPR_P (statement) = cx;
11388 else
11389 statement = begin_switch_stmt ();
11391 /* Parse the optional init-statement. */
11392 if (cp_parser_init_statement_p (parser))
11394 tree decl;
11395 if (cxx_dialect < cxx17)
11396 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11397 "init-statement in selection statements only available "
11398 "with -std=c++17 or -std=gnu++17");
11399 cp_parser_init_statement (parser, &decl);
11402 /* Parse the condition. */
11403 condition = cp_parser_condition (parser);
11404 /* Look for the `)'. */
11405 if (!parens.require_close (parser))
11406 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11407 /*consume_paren=*/true);
11409 if (keyword == RID_IF)
11411 bool nested_if;
11412 unsigned char in_statement;
11414 /* Add the condition. */
11415 condition = finish_if_stmt_cond (condition, statement);
11417 if (warn_duplicated_cond)
11418 warn_duplicated_cond_add_or_warn (token->location, condition,
11419 &chain);
11421 /* Parse the then-clause. */
11422 in_statement = parser->in_statement;
11423 parser->in_statement |= IN_IF_STMT;
11425 /* Outside a template, the non-selected branch of a constexpr
11426 if is a 'discarded statement', i.e. unevaluated. */
11427 bool was_discarded = in_discarded_stmt;
11428 bool discard_then = (cx && !processing_template_decl
11429 && integer_zerop (condition));
11430 if (discard_then)
11432 in_discarded_stmt = true;
11433 ++c_inhibit_evaluation_warnings;
11436 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11437 guard_tinfo);
11439 parser->in_statement = in_statement;
11441 finish_then_clause (statement);
11443 if (discard_then)
11445 THEN_CLAUSE (statement) = NULL_TREE;
11446 in_discarded_stmt = was_discarded;
11447 --c_inhibit_evaluation_warnings;
11450 /* If the next token is `else', parse the else-clause. */
11451 if (cp_lexer_next_token_is_keyword (parser->lexer,
11452 RID_ELSE))
11454 bool discard_else = (cx && !processing_template_decl
11455 && integer_nonzerop (condition));
11456 if (discard_else)
11458 in_discarded_stmt = true;
11459 ++c_inhibit_evaluation_warnings;
11462 guard_tinfo
11463 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11464 /* Consume the `else' keyword. */
11465 cp_lexer_consume_token (parser->lexer);
11466 if (warn_duplicated_cond)
11468 if (cp_lexer_next_token_is_keyword (parser->lexer,
11469 RID_IF)
11470 && chain == NULL)
11472 /* We've got "if (COND) else if (COND2)". Start
11473 the condition chain and add COND as the first
11474 element. */
11475 chain = new vec<tree> ();
11476 if (!CONSTANT_CLASS_P (condition)
11477 && !TREE_SIDE_EFFECTS (condition))
11479 /* Wrap it in a NOP_EXPR so that we can set the
11480 location of the condition. */
11481 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11482 condition);
11483 SET_EXPR_LOCATION (e, token->location);
11484 chain->safe_push (e);
11487 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11488 RID_IF))
11490 /* This is if-else without subsequent if. Zap the
11491 condition chain; we would have already warned at
11492 this point. */
11493 delete chain;
11494 chain = NULL;
11497 begin_else_clause (statement);
11498 /* Parse the else-clause. */
11499 cp_parser_implicitly_scoped_statement (parser, NULL,
11500 guard_tinfo, chain);
11502 finish_else_clause (statement);
11504 /* If we are currently parsing a then-clause, then
11505 IF_P will not be NULL. We set it to true to
11506 indicate that this if statement has an else clause.
11507 This may trigger the Wparentheses warning below
11508 when we get back up to the parent if statement. */
11509 if (if_p != NULL)
11510 *if_p = true;
11512 if (discard_else)
11514 ELSE_CLAUSE (statement) = NULL_TREE;
11515 in_discarded_stmt = was_discarded;
11516 --c_inhibit_evaluation_warnings;
11519 else
11521 /* This if statement does not have an else clause. If
11522 NESTED_IF is true, then the then-clause has an if
11523 statement which does have an else clause. We warn
11524 about the potential ambiguity. */
11525 if (nested_if)
11526 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11527 "suggest explicit braces to avoid ambiguous"
11528 " %<else%>");
11529 if (warn_duplicated_cond)
11531 /* We don't need the condition chain anymore. */
11532 delete chain;
11533 chain = NULL;
11537 /* Now we're all done with the if-statement. */
11538 finish_if_stmt (statement);
11540 else
11542 bool in_switch_statement_p;
11543 unsigned char in_statement;
11545 /* Add the condition. */
11546 finish_switch_cond (condition, statement);
11548 /* Parse the body of the switch-statement. */
11549 in_switch_statement_p = parser->in_switch_statement_p;
11550 in_statement = parser->in_statement;
11551 parser->in_switch_statement_p = true;
11552 parser->in_statement |= IN_SWITCH_STMT;
11553 cp_parser_implicitly_scoped_statement (parser, if_p,
11554 guard_tinfo);
11555 parser->in_switch_statement_p = in_switch_statement_p;
11556 parser->in_statement = in_statement;
11558 /* Now we're all done with the switch-statement. */
11559 finish_switch_stmt (statement);
11562 return statement;
11564 break;
11566 default:
11567 cp_parser_error (parser, "expected selection-statement");
11568 return error_mark_node;
11572 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11573 If we have seen at least one decl-specifier, and the next token
11574 is not a parenthesis, then we must be looking at a declaration.
11575 (After "int (" we might be looking at a functional cast.) */
11577 static void
11578 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11579 bool any_specifiers_p)
11581 if (any_specifiers_p
11582 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11583 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11584 && !cp_parser_error_occurred (parser))
11585 cp_parser_commit_to_tentative_parse (parser);
11588 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11589 The declarator shall not specify a function or an array. Returns
11590 TRUE if the declarator is valid, FALSE otherwise. */
11592 static bool
11593 cp_parser_check_condition_declarator (cp_parser* parser,
11594 cp_declarator *declarator,
11595 location_t loc)
11597 if (function_declarator_p (declarator)
11598 || declarator->kind == cdk_array)
11600 if (declarator->kind == cdk_array)
11601 error_at (loc, "condition declares an array");
11602 else
11603 error_at (loc, "condition declares a function");
11604 if (parser->fully_implicit_function_template_p)
11605 abort_fully_implicit_template (parser);
11606 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11607 /*or_comma=*/false,
11608 /*consume_paren=*/false);
11609 return false;
11611 else
11612 return true;
11615 /* Parse a condition.
11617 condition:
11618 expression
11619 type-specifier-seq declarator = initializer-clause
11620 type-specifier-seq declarator braced-init-list
11622 GNU Extension:
11624 condition:
11625 type-specifier-seq declarator asm-specification [opt]
11626 attributes [opt] = assignment-expression
11628 Returns the expression that should be tested. */
11630 static tree
11631 cp_parser_condition (cp_parser* parser)
11633 cp_decl_specifier_seq type_specifiers;
11634 const char *saved_message;
11635 int declares_class_or_enum;
11637 /* Try the declaration first. */
11638 cp_parser_parse_tentatively (parser);
11639 /* New types are not allowed in the type-specifier-seq for a
11640 condition. */
11641 saved_message = parser->type_definition_forbidden_message;
11642 parser->type_definition_forbidden_message
11643 = G_("types may not be defined in conditions");
11644 /* Parse the type-specifier-seq. */
11645 cp_parser_decl_specifier_seq (parser,
11646 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11647 &type_specifiers,
11648 &declares_class_or_enum);
11649 /* Restore the saved message. */
11650 parser->type_definition_forbidden_message = saved_message;
11652 cp_parser_maybe_commit_to_declaration (parser,
11653 type_specifiers.any_specifiers_p);
11655 /* If all is well, we might be looking at a declaration. */
11656 if (!cp_parser_error_occurred (parser))
11658 tree decl;
11659 tree asm_specification;
11660 tree attributes;
11661 cp_declarator *declarator;
11662 tree initializer = NULL_TREE;
11663 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11665 /* Parse the declarator. */
11666 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11667 /*ctor_dtor_or_conv_p=*/NULL,
11668 /*parenthesized_p=*/NULL,
11669 /*member_p=*/false,
11670 /*friend_p=*/false);
11671 /* Parse the attributes. */
11672 attributes = cp_parser_attributes_opt (parser);
11673 /* Parse the asm-specification. */
11674 asm_specification = cp_parser_asm_specification_opt (parser);
11675 /* If the next token is not an `=' or '{', then we might still be
11676 looking at an expression. For example:
11678 if (A(a).x)
11680 looks like a decl-specifier-seq and a declarator -- but then
11681 there is no `=', so this is an expression. */
11682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11683 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11684 cp_parser_simulate_error (parser);
11686 /* If we did see an `=' or '{', then we are looking at a declaration
11687 for sure. */
11688 if (cp_parser_parse_definitely (parser))
11690 tree pushed_scope;
11691 bool non_constant_p;
11692 int flags = LOOKUP_ONLYCONVERTING;
11694 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11695 return error_mark_node;
11697 /* Create the declaration. */
11698 decl = start_decl (declarator, &type_specifiers,
11699 /*initialized_p=*/true,
11700 attributes, /*prefix_attributes=*/NULL_TREE,
11701 &pushed_scope);
11703 /* Parse the initializer. */
11704 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11706 initializer = cp_parser_braced_list (parser, &non_constant_p);
11707 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11708 flags = 0;
11710 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11712 /* Consume the `='. */
11713 cp_lexer_consume_token (parser->lexer);
11714 initializer = cp_parser_initializer_clause (parser,
11715 &non_constant_p);
11717 else
11719 cp_parser_error (parser, "expected initializer");
11720 initializer = error_mark_node;
11722 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11723 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11725 /* Process the initializer. */
11726 cp_finish_decl (decl,
11727 initializer, !non_constant_p,
11728 asm_specification,
11729 flags);
11731 if (pushed_scope)
11732 pop_scope (pushed_scope);
11734 return convert_from_reference (decl);
11737 /* If we didn't even get past the declarator successfully, we are
11738 definitely not looking at a declaration. */
11739 else
11740 cp_parser_abort_tentative_parse (parser);
11742 /* Otherwise, we are looking at an expression. */
11743 return cp_parser_expression (parser);
11746 /* Parses a for-statement or range-for-statement until the closing ')',
11747 not included. */
11749 static tree
11750 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11752 tree init, scope, decl;
11753 bool is_range_for;
11755 /* Begin the for-statement. */
11756 scope = begin_for_scope (&init);
11758 /* Parse the initialization. */
11759 is_range_for = cp_parser_init_statement (parser, &decl);
11761 if (is_range_for)
11762 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11763 else
11764 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11767 static tree
11768 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11769 unsigned short unroll)
11771 /* Normal for loop */
11772 tree condition = NULL_TREE;
11773 tree expression = NULL_TREE;
11774 tree stmt;
11776 stmt = begin_for_stmt (scope, init);
11777 /* The init-statement has already been parsed in
11778 cp_parser_init_statement, so no work is needed here. */
11779 finish_init_stmt (stmt);
11781 /* If there's a condition, process it. */
11782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11783 condition = cp_parser_condition (parser);
11784 else if (ivdep)
11786 cp_parser_error (parser, "missing loop condition in loop with "
11787 "%<GCC ivdep%> pragma");
11788 condition = error_mark_node;
11790 else if (unroll)
11792 cp_parser_error (parser, "missing loop condition in loop with "
11793 "%<GCC unroll%> pragma");
11794 condition = error_mark_node;
11796 finish_for_cond (condition, stmt, ivdep, unroll);
11797 /* Look for the `;'. */
11798 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11800 /* If there's an expression, process it. */
11801 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11802 expression = cp_parser_expression (parser);
11803 finish_for_expr (expression, stmt);
11805 return stmt;
11808 /* Tries to parse a range-based for-statement:
11810 range-based-for:
11811 decl-specifier-seq declarator : expression
11813 The decl-specifier-seq declarator and the `:' are already parsed by
11814 cp_parser_init_statement. If processing_template_decl it returns a
11815 newly created RANGE_FOR_STMT; if not, it is converted to a
11816 regular FOR_STMT. */
11818 static tree
11819 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11820 bool ivdep, unsigned short unroll)
11822 tree stmt, range_expr;
11823 auto_vec <cxx_binding *, 16> bindings;
11824 auto_vec <tree, 16> names;
11825 tree decomp_first_name = NULL_TREE;
11826 unsigned int decomp_cnt = 0;
11828 /* Get the range declaration momentarily out of the way so that
11829 the range expression doesn't clash with it. */
11830 if (range_decl != error_mark_node)
11832 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11834 tree v = DECL_VALUE_EXPR (range_decl);
11835 /* For decomposition declaration get all of the corresponding
11836 declarations out of the way. */
11837 if (TREE_CODE (v) == ARRAY_REF
11838 && VAR_P (TREE_OPERAND (v, 0))
11839 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11841 tree d = range_decl;
11842 range_decl = TREE_OPERAND (v, 0);
11843 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11844 decomp_first_name = d;
11845 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11847 tree name = DECL_NAME (d);
11848 names.safe_push (name);
11849 bindings.safe_push (IDENTIFIER_BINDING (name));
11850 IDENTIFIER_BINDING (name)
11851 = IDENTIFIER_BINDING (name)->previous;
11855 if (names.is_empty ())
11857 tree name = DECL_NAME (range_decl);
11858 names.safe_push (name);
11859 bindings.safe_push (IDENTIFIER_BINDING (name));
11860 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11864 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11866 bool expr_non_constant_p;
11867 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11869 else
11870 range_expr = cp_parser_expression (parser);
11872 /* Put the range declaration(s) back into scope. */
11873 for (unsigned int i = 0; i < names.length (); i++)
11875 cxx_binding *binding = bindings[i];
11876 binding->previous = IDENTIFIER_BINDING (names[i]);
11877 IDENTIFIER_BINDING (names[i]) = binding;
11880 /* If in template, STMT is converted to a normal for-statement
11881 at instantiation. If not, it is done just ahead. */
11882 if (processing_template_decl)
11884 if (check_for_bare_parameter_packs (range_expr))
11885 range_expr = error_mark_node;
11886 stmt = begin_range_for_stmt (scope, init);
11887 if (ivdep)
11888 RANGE_FOR_IVDEP (stmt) = 1;
11889 if (unroll)
11890 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11891 finish_range_for_decl (stmt, range_decl, range_expr);
11892 if (!type_dependent_expression_p (range_expr)
11893 /* do_auto_deduction doesn't mess with template init-lists. */
11894 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11895 do_range_for_auto_deduction (range_decl, range_expr);
11897 else
11899 stmt = begin_for_stmt (scope, init);
11900 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11901 decomp_first_name, decomp_cnt, ivdep,
11902 unroll);
11904 return stmt;
11907 /* Subroutine of cp_convert_range_for: given the initializer expression,
11908 builds up the range temporary. */
11910 static tree
11911 build_range_temp (tree range_expr)
11913 tree range_type, range_temp;
11915 /* Find out the type deduced by the declaration
11916 `auto &&__range = range_expr'. */
11917 range_type = cp_build_reference_type (make_auto (), true);
11918 range_type = do_auto_deduction (range_type, range_expr,
11919 type_uses_auto (range_type));
11921 /* Create the __range variable. */
11922 range_temp = build_decl (input_location, VAR_DECL,
11923 get_identifier ("__for_range"), range_type);
11924 TREE_USED (range_temp) = 1;
11925 DECL_ARTIFICIAL (range_temp) = 1;
11927 return range_temp;
11930 /* Used by cp_parser_range_for in template context: we aren't going to
11931 do a full conversion yet, but we still need to resolve auto in the
11932 type of the for-range-declaration if present. This is basically
11933 a shortcut version of cp_convert_range_for. */
11935 static void
11936 do_range_for_auto_deduction (tree decl, tree range_expr)
11938 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11939 if (auto_node)
11941 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11942 range_temp = convert_from_reference (build_range_temp (range_expr));
11943 iter_type = (cp_parser_perform_range_for_lookup
11944 (range_temp, &begin_dummy, &end_dummy));
11945 if (iter_type)
11947 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11948 iter_type);
11949 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11950 RO_UNARY_STAR,
11951 tf_warning_or_error);
11952 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11953 iter_decl, auto_node);
11958 /* Converts a range-based for-statement into a normal
11959 for-statement, as per the definition.
11961 for (RANGE_DECL : RANGE_EXPR)
11962 BLOCK
11964 should be equivalent to:
11967 auto &&__range = RANGE_EXPR;
11968 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11969 __begin != __end;
11970 ++__begin)
11972 RANGE_DECL = *__begin;
11973 BLOCK
11977 If RANGE_EXPR is an array:
11978 BEGIN_EXPR = __range
11979 END_EXPR = __range + ARRAY_SIZE(__range)
11980 Else if RANGE_EXPR has a member 'begin' or 'end':
11981 BEGIN_EXPR = __range.begin()
11982 END_EXPR = __range.end()
11983 Else:
11984 BEGIN_EXPR = begin(__range)
11985 END_EXPR = end(__range);
11987 If __range has a member 'begin' but not 'end', or vice versa, we must
11988 still use the second alternative (it will surely fail, however).
11989 When calling begin()/end() in the third alternative we must use
11990 argument dependent lookup, but always considering 'std' as an associated
11991 namespace. */
11993 tree
11994 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11995 tree decomp_first_name, unsigned int decomp_cnt,
11996 bool ivdep, unsigned short unroll)
11998 tree begin, end;
11999 tree iter_type, begin_expr, end_expr;
12000 tree condition, expression;
12002 range_expr = mark_lvalue_use (range_expr);
12004 if (range_decl == error_mark_node || range_expr == error_mark_node)
12005 /* If an error happened previously do nothing or else a lot of
12006 unhelpful errors would be issued. */
12007 begin_expr = end_expr = iter_type = error_mark_node;
12008 else
12010 tree range_temp;
12012 if (VAR_P (range_expr)
12013 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12014 /* Can't bind a reference to an array of runtime bound. */
12015 range_temp = range_expr;
12016 else
12018 range_temp = build_range_temp (range_expr);
12019 pushdecl (range_temp);
12020 cp_finish_decl (range_temp, range_expr,
12021 /*is_constant_init*/false, NULL_TREE,
12022 LOOKUP_ONLYCONVERTING);
12023 range_temp = convert_from_reference (range_temp);
12025 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12026 &begin_expr, &end_expr);
12029 /* The new for initialization statement. */
12030 begin = build_decl (input_location, VAR_DECL,
12031 get_identifier ("__for_begin"), iter_type);
12032 TREE_USED (begin) = 1;
12033 DECL_ARTIFICIAL (begin) = 1;
12034 pushdecl (begin);
12035 cp_finish_decl (begin, begin_expr,
12036 /*is_constant_init*/false, NULL_TREE,
12037 LOOKUP_ONLYCONVERTING);
12039 if (cxx_dialect >= cxx17)
12040 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12041 end = build_decl (input_location, VAR_DECL,
12042 get_identifier ("__for_end"), iter_type);
12043 TREE_USED (end) = 1;
12044 DECL_ARTIFICIAL (end) = 1;
12045 pushdecl (end);
12046 cp_finish_decl (end, end_expr,
12047 /*is_constant_init*/false, NULL_TREE,
12048 LOOKUP_ONLYCONVERTING);
12050 finish_init_stmt (statement);
12052 /* The new for condition. */
12053 condition = build_x_binary_op (input_location, NE_EXPR,
12054 begin, ERROR_MARK,
12055 end, ERROR_MARK,
12056 NULL, tf_warning_or_error);
12057 finish_for_cond (condition, statement, ivdep, unroll);
12059 /* The new increment expression. */
12060 expression = finish_unary_op_expr (input_location,
12061 PREINCREMENT_EXPR, begin,
12062 tf_warning_or_error);
12063 finish_for_expr (expression, statement);
12065 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12066 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12068 /* The declaration is initialized with *__begin inside the loop body. */
12069 cp_finish_decl (range_decl,
12070 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12071 tf_warning_or_error),
12072 /*is_constant_init*/false, NULL_TREE,
12073 LOOKUP_ONLYCONVERTING);
12074 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12075 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12077 return statement;
12080 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12081 We need to solve both at the same time because the method used
12082 depends on the existence of members begin or end.
12083 Returns the type deduced for the iterator expression. */
12085 static tree
12086 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12088 if (error_operand_p (range))
12090 *begin = *end = error_mark_node;
12091 return error_mark_node;
12094 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12096 error ("range-based %<for%> expression of type %qT "
12097 "has incomplete type", TREE_TYPE (range));
12098 *begin = *end = error_mark_node;
12099 return error_mark_node;
12101 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12103 /* If RANGE is an array, we will use pointer arithmetic. */
12104 *begin = decay_conversion (range, tf_warning_or_error);
12105 *end = build_binary_op (input_location, PLUS_EXPR,
12106 range,
12107 array_type_nelts_top (TREE_TYPE (range)),
12108 false);
12109 return TREE_TYPE (*begin);
12111 else
12113 /* If it is not an array, we must do a bit of magic. */
12114 tree id_begin, id_end;
12115 tree member_begin, member_end;
12117 *begin = *end = error_mark_node;
12119 id_begin = get_identifier ("begin");
12120 id_end = get_identifier ("end");
12121 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12122 /*protect=*/2, /*want_type=*/false,
12123 tf_warning_or_error);
12124 member_end = lookup_member (TREE_TYPE (range), id_end,
12125 /*protect=*/2, /*want_type=*/false,
12126 tf_warning_or_error);
12128 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12130 /* Use the member functions. */
12131 *begin = cp_parser_range_for_member_function (range, id_begin);
12132 *end = cp_parser_range_for_member_function (range, id_end);
12134 else
12136 /* Use global functions with ADL. */
12137 vec<tree, va_gc> *vec;
12138 vec = make_tree_vector ();
12140 vec_safe_push (vec, range);
12142 member_begin = perform_koenig_lookup (id_begin, vec,
12143 tf_warning_or_error);
12144 *begin = finish_call_expr (member_begin, &vec, false, true,
12145 tf_warning_or_error);
12146 member_end = perform_koenig_lookup (id_end, vec,
12147 tf_warning_or_error);
12148 *end = finish_call_expr (member_end, &vec, false, true,
12149 tf_warning_or_error);
12151 release_tree_vector (vec);
12154 /* Last common checks. */
12155 if (*begin == error_mark_node || *end == error_mark_node)
12157 /* If one of the expressions is an error do no more checks. */
12158 *begin = *end = error_mark_node;
12159 return error_mark_node;
12161 else if (type_dependent_expression_p (*begin)
12162 || type_dependent_expression_p (*end))
12163 /* Can happen, when, eg, in a template context, Koenig lookup
12164 can't resolve begin/end (c++/58503). */
12165 return NULL_TREE;
12166 else
12168 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12169 /* The unqualified type of the __begin and __end temporaries should
12170 be the same, as required by the multiple auto declaration. */
12171 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12173 if (cxx_dialect >= cxx17
12174 && (build_x_binary_op (input_location, NE_EXPR,
12175 *begin, ERROR_MARK,
12176 *end, ERROR_MARK,
12177 NULL, tf_none)
12178 != error_mark_node))
12179 /* P0184R0 allows __begin and __end to have different types,
12180 but make sure they are comparable so we can give a better
12181 diagnostic. */;
12182 else
12183 error ("inconsistent begin/end types in range-based %<for%> "
12184 "statement: %qT and %qT",
12185 TREE_TYPE (*begin), TREE_TYPE (*end));
12187 return iter_type;
12192 /* Helper function for cp_parser_perform_range_for_lookup.
12193 Builds a tree for RANGE.IDENTIFIER(). */
12195 static tree
12196 cp_parser_range_for_member_function (tree range, tree identifier)
12198 tree member, res;
12199 vec<tree, va_gc> *vec;
12201 member = finish_class_member_access_expr (range, identifier,
12202 false, tf_warning_or_error);
12203 if (member == error_mark_node)
12204 return error_mark_node;
12206 vec = make_tree_vector ();
12207 res = finish_call_expr (member, &vec,
12208 /*disallow_virtual=*/false,
12209 /*koenig_p=*/false,
12210 tf_warning_or_error);
12211 release_tree_vector (vec);
12212 return res;
12215 /* Parse an iteration-statement.
12217 iteration-statement:
12218 while ( condition ) statement
12219 do statement while ( expression ) ;
12220 for ( init-statement condition [opt] ; expression [opt] )
12221 statement
12223 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12225 static tree
12226 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12227 unsigned short unroll)
12229 cp_token *token;
12230 enum rid keyword;
12231 tree statement;
12232 unsigned char in_statement;
12233 token_indent_info guard_tinfo;
12235 /* Peek at the next token. */
12236 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12237 if (!token)
12238 return error_mark_node;
12240 guard_tinfo = get_token_indent_info (token);
12242 /* Remember whether or not we are already within an iteration
12243 statement. */
12244 in_statement = parser->in_statement;
12246 /* See what kind of keyword it is. */
12247 keyword = token->keyword;
12248 switch (keyword)
12250 case RID_WHILE:
12252 tree condition;
12254 /* Begin the while-statement. */
12255 statement = begin_while_stmt ();
12256 /* Look for the `('. */
12257 matching_parens parens;
12258 parens.require_open (parser);
12259 /* Parse the condition. */
12260 condition = cp_parser_condition (parser);
12261 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12262 /* Look for the `)'. */
12263 parens.require_close (parser);
12264 /* Parse the dependent statement. */
12265 parser->in_statement = IN_ITERATION_STMT;
12266 bool prev = note_iteration_stmt_body_start ();
12267 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12268 note_iteration_stmt_body_end (prev);
12269 parser->in_statement = in_statement;
12270 /* We're done with the while-statement. */
12271 finish_while_stmt (statement);
12273 break;
12275 case RID_DO:
12277 tree expression;
12279 /* Begin the do-statement. */
12280 statement = begin_do_stmt ();
12281 /* Parse the body of the do-statement. */
12282 parser->in_statement = IN_ITERATION_STMT;
12283 bool prev = note_iteration_stmt_body_start ();
12284 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12285 note_iteration_stmt_body_end (prev);
12286 parser->in_statement = in_statement;
12287 finish_do_body (statement);
12288 /* Look for the `while' keyword. */
12289 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12290 /* Look for the `('. */
12291 matching_parens parens;
12292 parens.require_open (parser);
12293 /* Parse the expression. */
12294 expression = cp_parser_expression (parser);
12295 /* We're done with the do-statement. */
12296 finish_do_stmt (expression, statement, ivdep, unroll);
12297 /* Look for the `)'. */
12298 parens.require_close (parser);
12299 /* Look for the `;'. */
12300 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12302 break;
12304 case RID_FOR:
12306 /* Look for the `('. */
12307 matching_parens parens;
12308 parens.require_open (parser);
12310 statement = cp_parser_for (parser, ivdep, unroll);
12312 /* Look for the `)'. */
12313 parens.require_close (parser);
12315 /* Parse the body of the for-statement. */
12316 parser->in_statement = IN_ITERATION_STMT;
12317 bool prev = note_iteration_stmt_body_start ();
12318 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12319 note_iteration_stmt_body_end (prev);
12320 parser->in_statement = in_statement;
12322 /* We're done with the for-statement. */
12323 finish_for_stmt (statement);
12325 break;
12327 default:
12328 cp_parser_error (parser, "expected iteration-statement");
12329 statement = error_mark_node;
12330 break;
12333 return statement;
12336 /* Parse a init-statement or the declarator of a range-based-for.
12337 Returns true if a range-based-for declaration is seen.
12339 init-statement:
12340 expression-statement
12341 simple-declaration */
12343 static bool
12344 cp_parser_init_statement (cp_parser *parser, tree *decl)
12346 /* If the next token is a `;', then we have an empty
12347 expression-statement. Grammatically, this is also a
12348 simple-declaration, but an invalid one, because it does not
12349 declare anything. Therefore, if we did not handle this case
12350 specially, we would issue an error message about an invalid
12351 declaration. */
12352 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12354 bool is_range_for = false;
12355 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12357 /* Try to parse the init-statement. */
12358 if (cp_parser_range_based_for_with_init_p (parser))
12360 tree dummy;
12361 cp_parser_parse_tentatively (parser);
12362 /* Parse the declaration. */
12363 cp_parser_simple_declaration (parser,
12364 /*function_definition_allowed_p=*/false,
12365 &dummy);
12366 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12367 if (!cp_parser_parse_definitely (parser))
12368 /* That didn't work, try to parse it as an expression-statement. */
12369 cp_parser_expression_statement (parser, NULL_TREE);
12371 if (cxx_dialect < cxx2a)
12373 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12374 "range-based %<for%> loops with initializer only "
12375 "available with -std=c++2a or -std=gnu++2a");
12376 *decl = error_mark_node;
12380 /* A colon is used in range-based for. */
12381 parser->colon_corrects_to_scope_p = false;
12383 /* We're going to speculatively look for a declaration, falling back
12384 to an expression, if necessary. */
12385 cp_parser_parse_tentatively (parser);
12386 /* Parse the declaration. */
12387 cp_parser_simple_declaration (parser,
12388 /*function_definition_allowed_p=*/false,
12389 decl);
12390 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12391 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12393 /* It is a range-for, consume the ':'. */
12394 cp_lexer_consume_token (parser->lexer);
12395 is_range_for = true;
12396 if (cxx_dialect < cxx11)
12398 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12399 "range-based %<for%> loops only available with "
12400 "-std=c++11 or -std=gnu++11");
12401 *decl = error_mark_node;
12404 else
12405 /* The ';' is not consumed yet because we told
12406 cp_parser_simple_declaration not to. */
12407 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12409 if (cp_parser_parse_definitely (parser))
12410 return is_range_for;
12411 /* If the tentative parse failed, then we shall need to look for an
12412 expression-statement. */
12414 /* If we are here, it is an expression-statement. */
12415 cp_parser_expression_statement (parser, NULL_TREE);
12416 return false;
12419 /* Parse a jump-statement.
12421 jump-statement:
12422 break ;
12423 continue ;
12424 return expression [opt] ;
12425 return braced-init-list ;
12426 goto identifier ;
12428 GNU extension:
12430 jump-statement:
12431 goto * expression ;
12433 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12435 static tree
12436 cp_parser_jump_statement (cp_parser* parser)
12438 tree statement = error_mark_node;
12439 cp_token *token;
12440 enum rid keyword;
12441 unsigned char in_statement;
12443 /* Peek at the next token. */
12444 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12445 if (!token)
12446 return error_mark_node;
12448 /* See what kind of keyword it is. */
12449 keyword = token->keyword;
12450 switch (keyword)
12452 case RID_BREAK:
12453 in_statement = parser->in_statement & ~IN_IF_STMT;
12454 switch (in_statement)
12456 case 0:
12457 error_at (token->location, "break statement not within loop or switch");
12458 break;
12459 default:
12460 gcc_assert ((in_statement & IN_SWITCH_STMT)
12461 || in_statement == IN_ITERATION_STMT);
12462 statement = finish_break_stmt ();
12463 if (in_statement == IN_ITERATION_STMT)
12464 break_maybe_infinite_loop ();
12465 break;
12466 case IN_OMP_BLOCK:
12467 error_at (token->location, "invalid exit from OpenMP structured block");
12468 break;
12469 case IN_OMP_FOR:
12470 error_at (token->location, "break statement used with OpenMP for loop");
12471 break;
12473 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12474 break;
12476 case RID_CONTINUE:
12477 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12479 case 0:
12480 error_at (token->location, "continue statement not within a loop");
12481 break;
12482 /* Fall through. */
12483 case IN_ITERATION_STMT:
12484 case IN_OMP_FOR:
12485 statement = finish_continue_stmt ();
12486 break;
12487 case IN_OMP_BLOCK:
12488 error_at (token->location, "invalid exit from OpenMP structured block");
12489 break;
12490 default:
12491 gcc_unreachable ();
12493 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12494 break;
12496 case RID_RETURN:
12498 tree expr;
12499 bool expr_non_constant_p;
12501 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12503 cp_lexer_set_source_position (parser->lexer);
12504 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12505 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12507 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12508 expr = cp_parser_expression (parser);
12509 else
12510 /* If the next token is a `;', then there is no
12511 expression. */
12512 expr = NULL_TREE;
12513 /* Build the return-statement. */
12514 if (current_function_auto_return_pattern && in_discarded_stmt)
12515 /* Don't deduce from a discarded return statement. */;
12516 else
12517 statement = finish_return_stmt (expr);
12518 /* Look for the final `;'. */
12519 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12521 break;
12523 case RID_GOTO:
12524 if (parser->in_function_body
12525 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12527 error ("%<goto%> in %<constexpr%> function");
12528 cp_function_chain->invalid_constexpr = true;
12531 /* Create the goto-statement. */
12532 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12534 /* Issue a warning about this use of a GNU extension. */
12535 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12536 /* Consume the '*' token. */
12537 cp_lexer_consume_token (parser->lexer);
12538 /* Parse the dependent expression. */
12539 finish_goto_stmt (cp_parser_expression (parser));
12541 else
12542 finish_goto_stmt (cp_parser_identifier (parser));
12543 /* Look for the final `;'. */
12544 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12545 break;
12547 default:
12548 cp_parser_error (parser, "expected jump-statement");
12549 break;
12552 return statement;
12555 /* Parse a declaration-statement.
12557 declaration-statement:
12558 block-declaration */
12560 static void
12561 cp_parser_declaration_statement (cp_parser* parser)
12563 void *p;
12565 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12566 p = obstack_alloc (&declarator_obstack, 0);
12568 /* Parse the block-declaration. */
12569 cp_parser_block_declaration (parser, /*statement_p=*/true);
12571 /* Free any declarators allocated. */
12572 obstack_free (&declarator_obstack, p);
12575 /* Some dependent statements (like `if (cond) statement'), are
12576 implicitly in their own scope. In other words, if the statement is
12577 a single statement (as opposed to a compound-statement), it is
12578 none-the-less treated as if it were enclosed in braces. Any
12579 declarations appearing in the dependent statement are out of scope
12580 after control passes that point. This function parses a statement,
12581 but ensures that is in its own scope, even if it is not a
12582 compound-statement.
12584 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12585 is a (possibly labeled) if statement which is not enclosed in
12586 braces and has an else clause. This is used to implement
12587 -Wparentheses.
12589 CHAIN is a vector of if-else-if conditions. This is used to implement
12590 -Wduplicated-cond.
12592 Returns the new statement. */
12594 static tree
12595 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12596 const token_indent_info &guard_tinfo,
12597 vec<tree> *chain)
12599 tree statement;
12600 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12601 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12602 token_indent_info body_tinfo
12603 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12605 if (if_p != NULL)
12606 *if_p = false;
12608 /* Mark if () ; with a special NOP_EXPR. */
12609 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12611 cp_lexer_consume_token (parser->lexer);
12612 statement = add_stmt (build_empty_stmt (body_loc));
12614 if (guard_tinfo.keyword == RID_IF
12615 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12616 warning_at (body_loc, OPT_Wempty_body,
12617 "suggest braces around empty body in an %<if%> statement");
12618 else if (guard_tinfo.keyword == RID_ELSE)
12619 warning_at (body_loc, OPT_Wempty_body,
12620 "suggest braces around empty body in an %<else%> statement");
12622 /* if a compound is opened, we simply parse the statement directly. */
12623 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12624 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12625 /* If the token is not a `{', then we must take special action. */
12626 else
12628 /* Create a compound-statement. */
12629 statement = begin_compound_stmt (0);
12630 /* Parse the dependent-statement. */
12631 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12632 &body_loc_after_labels);
12633 /* Finish the dummy compound-statement. */
12634 finish_compound_stmt (statement);
12637 token_indent_info next_tinfo
12638 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12639 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12641 if (body_loc_after_labels != UNKNOWN_LOCATION
12642 && next_tinfo.type != CPP_SEMICOLON)
12643 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12644 guard_tinfo.location, guard_tinfo.keyword);
12646 /* Return the statement. */
12647 return statement;
12650 /* For some dependent statements (like `while (cond) statement'), we
12651 have already created a scope. Therefore, even if the dependent
12652 statement is a compound-statement, we do not want to create another
12653 scope. */
12655 static void
12656 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12657 const token_indent_info &guard_tinfo)
12659 /* If the token is a `{', then we must take special action. */
12660 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12662 token_indent_info body_tinfo
12663 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12664 location_t loc_after_labels = UNKNOWN_LOCATION;
12666 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12667 &loc_after_labels);
12668 token_indent_info next_tinfo
12669 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12670 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12672 if (loc_after_labels != UNKNOWN_LOCATION
12673 && next_tinfo.type != CPP_SEMICOLON)
12674 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12675 guard_tinfo.location,
12676 guard_tinfo.keyword);
12678 else
12680 /* Avoid calling cp_parser_compound_statement, so that we
12681 don't create a new scope. Do everything else by hand. */
12682 matching_braces braces;
12683 braces.require_open (parser);
12684 /* If the next keyword is `__label__' we have a label declaration. */
12685 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12686 cp_parser_label_declaration (parser);
12687 /* Parse an (optional) statement-seq. */
12688 cp_parser_statement_seq_opt (parser, NULL_TREE);
12689 braces.require_close (parser);
12693 /* Declarations [gram.dcl.dcl] */
12695 /* Parse an optional declaration-sequence.
12697 declaration-seq:
12698 declaration
12699 declaration-seq declaration */
12701 static void
12702 cp_parser_declaration_seq_opt (cp_parser* parser)
12704 while (true)
12706 cp_token *token;
12708 token = cp_lexer_peek_token (parser->lexer);
12710 if (token->type == CPP_CLOSE_BRACE
12711 || token->type == CPP_EOF
12712 || token->type == CPP_PRAGMA_EOL)
12713 break;
12715 if (token->type == CPP_SEMICOLON)
12717 /* A declaration consisting of a single semicolon is
12718 invalid. Allow it unless we're being pedantic. */
12719 cp_lexer_consume_token (parser->lexer);
12720 if (!in_system_header_at (input_location))
12721 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12722 continue;
12725 /* If we're entering or exiting a region that's implicitly
12726 extern "C", modify the lang context appropriately. */
12727 if (!parser->implicit_extern_c && token->implicit_extern_c)
12729 push_lang_context (lang_name_c);
12730 parser->implicit_extern_c = true;
12732 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12734 pop_lang_context ();
12735 parser->implicit_extern_c = false;
12738 if (token->type == CPP_PRAGMA)
12740 /* A top-level declaration can consist solely of a #pragma.
12741 A nested declaration cannot, so this is done here and not
12742 in cp_parser_declaration. (A #pragma at block scope is
12743 handled in cp_parser_statement.) */
12744 cp_parser_pragma (parser, pragma_external, NULL);
12745 continue;
12748 /* Parse the declaration itself. */
12749 cp_parser_declaration (parser);
12753 /* Parse a declaration.
12755 declaration:
12756 block-declaration
12757 function-definition
12758 template-declaration
12759 explicit-instantiation
12760 explicit-specialization
12761 linkage-specification
12762 namespace-definition
12764 C++17:
12765 deduction-guide
12767 GNU extension:
12769 declaration:
12770 __extension__ declaration */
12772 static void
12773 cp_parser_declaration (cp_parser* parser)
12775 cp_token token1;
12776 cp_token token2;
12777 int saved_pedantic;
12778 void *p;
12779 tree attributes = NULL_TREE;
12781 /* Check for the `__extension__' keyword. */
12782 if (cp_parser_extension_opt (parser, &saved_pedantic))
12784 /* Parse the qualified declaration. */
12785 cp_parser_declaration (parser);
12786 /* Restore the PEDANTIC flag. */
12787 pedantic = saved_pedantic;
12789 return;
12792 /* Try to figure out what kind of declaration is present. */
12793 token1 = *cp_lexer_peek_token (parser->lexer);
12795 if (token1.type != CPP_EOF)
12796 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12797 else
12799 token2.type = CPP_EOF;
12800 token2.keyword = RID_MAX;
12803 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12804 p = obstack_alloc (&declarator_obstack, 0);
12806 /* If the next token is `extern' and the following token is a string
12807 literal, then we have a linkage specification. */
12808 if (token1.keyword == RID_EXTERN
12809 && cp_parser_is_pure_string_literal (&token2))
12810 cp_parser_linkage_specification (parser);
12811 /* If the next token is `template', then we have either a template
12812 declaration, an explicit instantiation, or an explicit
12813 specialization. */
12814 else if (token1.keyword == RID_TEMPLATE)
12816 /* `template <>' indicates a template specialization. */
12817 if (token2.type == CPP_LESS
12818 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12819 cp_parser_explicit_specialization (parser);
12820 /* `template <' indicates a template declaration. */
12821 else if (token2.type == CPP_LESS)
12822 cp_parser_template_declaration (parser, /*member_p=*/false);
12823 /* Anything else must be an explicit instantiation. */
12824 else
12825 cp_parser_explicit_instantiation (parser);
12827 /* If the next token is `export', then we have a template
12828 declaration. */
12829 else if (token1.keyword == RID_EXPORT)
12830 cp_parser_template_declaration (parser, /*member_p=*/false);
12831 /* If the next token is `extern', 'static' or 'inline' and the one
12832 after that is `template', we have a GNU extended explicit
12833 instantiation directive. */
12834 else if (cp_parser_allow_gnu_extensions_p (parser)
12835 && (token1.keyword == RID_EXTERN
12836 || token1.keyword == RID_STATIC
12837 || token1.keyword == RID_INLINE)
12838 && token2.keyword == RID_TEMPLATE)
12839 cp_parser_explicit_instantiation (parser);
12840 /* If the next token is `namespace', check for a named or unnamed
12841 namespace definition. */
12842 else if (token1.keyword == RID_NAMESPACE
12843 && (/* A named namespace definition. */
12844 (token2.type == CPP_NAME
12845 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12846 != CPP_EQ))
12847 || (token2.type == CPP_OPEN_SQUARE
12848 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12849 == CPP_OPEN_SQUARE)
12850 /* An unnamed namespace definition. */
12851 || token2.type == CPP_OPEN_BRACE
12852 || token2.keyword == RID_ATTRIBUTE))
12853 cp_parser_namespace_definition (parser);
12854 /* An inline (associated) namespace definition. */
12855 else if (token1.keyword == RID_INLINE
12856 && token2.keyword == RID_NAMESPACE)
12857 cp_parser_namespace_definition (parser);
12858 /* Objective-C++ declaration/definition. */
12859 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12860 cp_parser_objc_declaration (parser, NULL_TREE);
12861 else if (c_dialect_objc ()
12862 && token1.keyword == RID_ATTRIBUTE
12863 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12864 cp_parser_objc_declaration (parser, attributes);
12865 /* At this point we may have a template declared by a concept
12866 introduction. */
12867 else if (flag_concepts
12868 && cp_parser_template_declaration_after_export (parser,
12869 /*member_p=*/false))
12870 /* We did. */;
12871 else
12872 /* Try to parse a block-declaration, or a function-definition. */
12873 cp_parser_block_declaration (parser, /*statement_p=*/false);
12875 /* Free any declarators allocated. */
12876 obstack_free (&declarator_obstack, p);
12879 /* Parse a block-declaration.
12881 block-declaration:
12882 simple-declaration
12883 asm-definition
12884 namespace-alias-definition
12885 using-declaration
12886 using-directive
12888 GNU Extension:
12890 block-declaration:
12891 __extension__ block-declaration
12893 C++0x Extension:
12895 block-declaration:
12896 static_assert-declaration
12898 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12899 part of a declaration-statement. */
12901 static void
12902 cp_parser_block_declaration (cp_parser *parser,
12903 bool statement_p)
12905 cp_token *token1;
12906 int saved_pedantic;
12908 /* Check for the `__extension__' keyword. */
12909 if (cp_parser_extension_opt (parser, &saved_pedantic))
12911 /* Parse the qualified declaration. */
12912 cp_parser_block_declaration (parser, statement_p);
12913 /* Restore the PEDANTIC flag. */
12914 pedantic = saved_pedantic;
12916 return;
12919 /* Peek at the next token to figure out which kind of declaration is
12920 present. */
12921 token1 = cp_lexer_peek_token (parser->lexer);
12923 /* If the next keyword is `asm', we have an asm-definition. */
12924 if (token1->keyword == RID_ASM)
12926 if (statement_p)
12927 cp_parser_commit_to_tentative_parse (parser);
12928 cp_parser_asm_definition (parser);
12930 /* If the next keyword is `namespace', we have a
12931 namespace-alias-definition. */
12932 else if (token1->keyword == RID_NAMESPACE)
12933 cp_parser_namespace_alias_definition (parser);
12934 /* If the next keyword is `using', we have a
12935 using-declaration, a using-directive, or an alias-declaration. */
12936 else if (token1->keyword == RID_USING)
12938 cp_token *token2;
12940 if (statement_p)
12941 cp_parser_commit_to_tentative_parse (parser);
12942 /* If the token after `using' is `namespace', then we have a
12943 using-directive. */
12944 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12945 if (token2->keyword == RID_NAMESPACE)
12946 cp_parser_using_directive (parser);
12947 /* If the second token after 'using' is '=', then we have an
12948 alias-declaration. */
12949 else if (cxx_dialect >= cxx11
12950 && token2->type == CPP_NAME
12951 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12952 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12953 cp_parser_alias_declaration (parser);
12954 /* Otherwise, it's a using-declaration. */
12955 else
12956 cp_parser_using_declaration (parser,
12957 /*access_declaration_p=*/false);
12959 /* If the next keyword is `__label__' we have a misplaced label
12960 declaration. */
12961 else if (token1->keyword == RID_LABEL)
12963 cp_lexer_consume_token (parser->lexer);
12964 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12965 cp_parser_skip_to_end_of_statement (parser);
12966 /* If the next token is now a `;', consume it. */
12967 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12968 cp_lexer_consume_token (parser->lexer);
12970 /* If the next token is `static_assert' we have a static assertion. */
12971 else if (token1->keyword == RID_STATIC_ASSERT)
12972 cp_parser_static_assert (parser, /*member_p=*/false);
12973 /* Anything else must be a simple-declaration. */
12974 else
12975 cp_parser_simple_declaration (parser, !statement_p,
12976 /*maybe_range_for_decl*/NULL);
12979 /* Parse a simple-declaration.
12981 simple-declaration:
12982 decl-specifier-seq [opt] init-declarator-list [opt] ;
12983 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12984 brace-or-equal-initializer ;
12986 init-declarator-list:
12987 init-declarator
12988 init-declarator-list , init-declarator
12990 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12991 function-definition as a simple-declaration.
12993 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12994 parsed declaration if it is an uninitialized single declarator not followed
12995 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12996 if present, will not be consumed. */
12998 static void
12999 cp_parser_simple_declaration (cp_parser* parser,
13000 bool function_definition_allowed_p,
13001 tree *maybe_range_for_decl)
13003 cp_decl_specifier_seq decl_specifiers;
13004 int declares_class_or_enum;
13005 bool saw_declarator;
13006 location_t comma_loc = UNKNOWN_LOCATION;
13007 location_t init_loc = UNKNOWN_LOCATION;
13009 if (maybe_range_for_decl)
13010 *maybe_range_for_decl = NULL_TREE;
13012 /* Defer access checks until we know what is being declared; the
13013 checks for names appearing in the decl-specifier-seq should be
13014 done as if we were in the scope of the thing being declared. */
13015 push_deferring_access_checks (dk_deferred);
13017 /* Parse the decl-specifier-seq. We have to keep track of whether
13018 or not the decl-specifier-seq declares a named class or
13019 enumeration type, since that is the only case in which the
13020 init-declarator-list is allowed to be empty.
13022 [dcl.dcl]
13024 In a simple-declaration, the optional init-declarator-list can be
13025 omitted only when declaring a class or enumeration, that is when
13026 the decl-specifier-seq contains either a class-specifier, an
13027 elaborated-type-specifier, or an enum-specifier. */
13028 cp_parser_decl_specifier_seq (parser,
13029 CP_PARSER_FLAGS_OPTIONAL,
13030 &decl_specifiers,
13031 &declares_class_or_enum);
13032 /* We no longer need to defer access checks. */
13033 stop_deferring_access_checks ();
13035 /* In a block scope, a valid declaration must always have a
13036 decl-specifier-seq. By not trying to parse declarators, we can
13037 resolve the declaration/expression ambiguity more quickly. */
13038 if (!function_definition_allowed_p
13039 && !decl_specifiers.any_specifiers_p)
13041 cp_parser_error (parser, "expected declaration");
13042 goto done;
13045 /* If the next two tokens are both identifiers, the code is
13046 erroneous. The usual cause of this situation is code like:
13048 T t;
13050 where "T" should name a type -- but does not. */
13051 if (!decl_specifiers.any_type_specifiers_p
13052 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13054 /* If parsing tentatively, we should commit; we really are
13055 looking at a declaration. */
13056 cp_parser_commit_to_tentative_parse (parser);
13057 /* Give up. */
13058 goto done;
13061 cp_parser_maybe_commit_to_declaration (parser,
13062 decl_specifiers.any_specifiers_p);
13064 /* Look for C++17 decomposition declaration. */
13065 for (size_t n = 1; ; n++)
13066 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13067 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13068 continue;
13069 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13070 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13071 && decl_specifiers.any_specifiers_p)
13073 tree decl
13074 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13075 maybe_range_for_decl,
13076 &init_loc);
13078 /* The next token should be either a `,' or a `;'. */
13079 cp_token *token = cp_lexer_peek_token (parser->lexer);
13080 /* If it's a `;', we are done. */
13081 if (token->type == CPP_SEMICOLON)
13082 goto finish;
13083 else if (maybe_range_for_decl)
13085 if (*maybe_range_for_decl == NULL_TREE)
13086 *maybe_range_for_decl = error_mark_node;
13087 goto finish;
13089 /* Anything else is an error. */
13090 else
13092 /* If we have already issued an error message we don't need
13093 to issue another one. */
13094 if ((decl != error_mark_node
13095 && DECL_INITIAL (decl) != error_mark_node)
13096 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13097 cp_parser_error (parser, "expected %<,%> or %<;%>");
13098 /* Skip tokens until we reach the end of the statement. */
13099 cp_parser_skip_to_end_of_statement (parser);
13100 /* If the next token is now a `;', consume it. */
13101 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13102 cp_lexer_consume_token (parser->lexer);
13103 goto done;
13106 else
13107 break;
13109 tree last_type;
13110 bool auto_specifier_p;
13111 /* NULL_TREE if both variable and function declaration are allowed,
13112 error_mark_node if function declaration are not allowed and
13113 a FUNCTION_DECL that should be diagnosed if it is followed by
13114 variable declarations. */
13115 tree auto_function_declaration;
13117 last_type = NULL_TREE;
13118 auto_specifier_p
13119 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13120 auto_function_declaration = NULL_TREE;
13122 /* Keep going until we hit the `;' at the end of the simple
13123 declaration. */
13124 saw_declarator = false;
13125 while (cp_lexer_next_token_is_not (parser->lexer,
13126 CPP_SEMICOLON))
13128 cp_token *token;
13129 bool function_definition_p;
13130 tree decl;
13131 tree auto_result = NULL_TREE;
13133 if (saw_declarator)
13135 /* If we are processing next declarator, comma is expected */
13136 token = cp_lexer_peek_token (parser->lexer);
13137 gcc_assert (token->type == CPP_COMMA);
13138 cp_lexer_consume_token (parser->lexer);
13139 if (maybe_range_for_decl)
13141 *maybe_range_for_decl = error_mark_node;
13142 if (comma_loc == UNKNOWN_LOCATION)
13143 comma_loc = token->location;
13146 else
13147 saw_declarator = true;
13149 /* Parse the init-declarator. */
13150 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13151 /*checks=*/NULL,
13152 function_definition_allowed_p,
13153 /*member_p=*/false,
13154 declares_class_or_enum,
13155 &function_definition_p,
13156 maybe_range_for_decl,
13157 &init_loc,
13158 &auto_result);
13159 /* If an error occurred while parsing tentatively, exit quickly.
13160 (That usually happens when in the body of a function; each
13161 statement is treated as a declaration-statement until proven
13162 otherwise.) */
13163 if (cp_parser_error_occurred (parser))
13164 goto done;
13166 if (auto_specifier_p && cxx_dialect >= cxx14)
13168 /* If the init-declarator-list contains more than one
13169 init-declarator, they shall all form declarations of
13170 variables. */
13171 if (auto_function_declaration == NULL_TREE)
13172 auto_function_declaration
13173 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13174 else if (TREE_CODE (decl) == FUNCTION_DECL
13175 || auto_function_declaration != error_mark_node)
13177 error_at (decl_specifiers.locations[ds_type_spec],
13178 "non-variable %qD in declaration with more than one "
13179 "declarator with placeholder type",
13180 TREE_CODE (decl) == FUNCTION_DECL
13181 ? decl : auto_function_declaration);
13182 auto_function_declaration = error_mark_node;
13186 if (auto_result
13187 && (!processing_template_decl || !type_uses_auto (auto_result)))
13189 if (last_type
13190 && last_type != error_mark_node
13191 && !same_type_p (auto_result, last_type))
13193 /* If the list of declarators contains more than one declarator,
13194 the type of each declared variable is determined as described
13195 above. If the type deduced for the template parameter U is not
13196 the same in each deduction, the program is ill-formed. */
13197 error_at (decl_specifiers.locations[ds_type_spec],
13198 "inconsistent deduction for %qT: %qT and then %qT",
13199 decl_specifiers.type, last_type, auto_result);
13200 last_type = error_mark_node;
13202 else
13203 last_type = auto_result;
13206 /* Handle function definitions specially. */
13207 if (function_definition_p)
13209 /* If the next token is a `,', then we are probably
13210 processing something like:
13212 void f() {}, *p;
13214 which is erroneous. */
13215 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13217 cp_token *token = cp_lexer_peek_token (parser->lexer);
13218 error_at (token->location,
13219 "mixing"
13220 " declarations and function-definitions is forbidden");
13222 /* Otherwise, we're done with the list of declarators. */
13223 else
13225 pop_deferring_access_checks ();
13226 return;
13229 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13230 *maybe_range_for_decl = decl;
13231 /* The next token should be either a `,' or a `;'. */
13232 token = cp_lexer_peek_token (parser->lexer);
13233 /* If it's a `,', there are more declarators to come. */
13234 if (token->type == CPP_COMMA)
13235 /* will be consumed next time around */;
13236 /* If it's a `;', we are done. */
13237 else if (token->type == CPP_SEMICOLON)
13238 break;
13239 else if (maybe_range_for_decl)
13241 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13242 permerror (decl_specifiers.locations[ds_type_spec],
13243 "types may not be defined in a for-range-declaration");
13244 break;
13246 /* Anything else is an error. */
13247 else
13249 /* If we have already issued an error message we don't need
13250 to issue another one. */
13251 if ((decl != error_mark_node
13252 && DECL_INITIAL (decl) != error_mark_node)
13253 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13254 cp_parser_error (parser, "expected %<,%> or %<;%>");
13255 /* Skip tokens until we reach the end of the statement. */
13256 cp_parser_skip_to_end_of_statement (parser);
13257 /* If the next token is now a `;', consume it. */
13258 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13259 cp_lexer_consume_token (parser->lexer);
13260 goto done;
13262 /* After the first time around, a function-definition is not
13263 allowed -- even if it was OK at first. For example:
13265 int i, f() {}
13267 is not valid. */
13268 function_definition_allowed_p = false;
13271 /* Issue an error message if no declarators are present, and the
13272 decl-specifier-seq does not itself declare a class or
13273 enumeration: [dcl.dcl]/3. */
13274 if (!saw_declarator)
13276 if (cp_parser_declares_only_class_p (parser))
13278 if (!declares_class_or_enum
13279 && decl_specifiers.type
13280 && OVERLOAD_TYPE_P (decl_specifiers.type))
13281 /* Ensure an error is issued anyway when finish_decltype_type,
13282 called via cp_parser_decl_specifier_seq, returns a class or
13283 an enumeration (c++/51786). */
13284 decl_specifiers.type = NULL_TREE;
13285 shadow_tag (&decl_specifiers);
13287 /* Perform any deferred access checks. */
13288 perform_deferred_access_checks (tf_warning_or_error);
13291 /* Consume the `;'. */
13292 finish:
13293 if (!maybe_range_for_decl)
13294 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13295 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13297 if (init_loc != UNKNOWN_LOCATION)
13298 error_at (init_loc, "initializer in range-based %<for%> loop");
13299 if (comma_loc != UNKNOWN_LOCATION)
13300 error_at (comma_loc,
13301 "multiple declarations in range-based %<for%> loop");
13304 done:
13305 pop_deferring_access_checks ();
13308 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13309 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13310 initializer ; */
13312 static tree
13313 cp_parser_decomposition_declaration (cp_parser *parser,
13314 cp_decl_specifier_seq *decl_specifiers,
13315 tree *maybe_range_for_decl,
13316 location_t *init_loc)
13318 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13319 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13320 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13322 /* Parse the identifier-list. */
13323 auto_vec<cp_expr, 10> v;
13324 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13325 while (true)
13327 cp_expr e = cp_parser_identifier (parser);
13328 if (e.get_value () == error_mark_node)
13329 break;
13330 v.safe_push (e);
13331 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13332 break;
13333 cp_lexer_consume_token (parser->lexer);
13336 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13337 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13339 end_loc = UNKNOWN_LOCATION;
13340 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13341 false);
13342 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13343 cp_lexer_consume_token (parser->lexer);
13344 else
13346 cp_parser_skip_to_end_of_statement (parser);
13347 return error_mark_node;
13351 if (cxx_dialect < cxx17)
13352 pedwarn (loc, 0, "structured bindings only available with "
13353 "-std=c++17 or -std=gnu++17");
13355 tree pushed_scope;
13356 cp_declarator *declarator = make_declarator (cdk_decomp);
13357 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13358 declarator->id_loc = loc;
13359 if (ref_qual != REF_QUAL_NONE)
13360 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13361 ref_qual == REF_QUAL_RVALUE,
13362 NULL_TREE);
13363 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13364 NULL_TREE, decl_specifiers->attributes,
13365 &pushed_scope);
13366 tree orig_decl = decl;
13368 unsigned int i;
13369 cp_expr e;
13370 cp_decl_specifier_seq decl_specs;
13371 clear_decl_specs (&decl_specs);
13372 decl_specs.type = make_auto ();
13373 tree prev = decl;
13374 FOR_EACH_VEC_ELT (v, i, e)
13376 if (i == 0)
13377 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13378 else
13379 declarator->u.id.unqualified_name = e.get_value ();
13380 declarator->id_loc = e.get_location ();
13381 tree elt_pushed_scope;
13382 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13383 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13384 if (decl2 == error_mark_node)
13385 decl = error_mark_node;
13386 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13388 /* Ensure we've diagnosed redeclaration if we aren't creating
13389 a new VAR_DECL. */
13390 gcc_assert (errorcount);
13391 decl = error_mark_node;
13393 else
13394 prev = decl2;
13395 if (elt_pushed_scope)
13396 pop_scope (elt_pushed_scope);
13399 if (v.is_empty ())
13401 error_at (loc, "empty structured binding declaration");
13402 decl = error_mark_node;
13405 if (maybe_range_for_decl == NULL
13406 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13408 bool non_constant_p = false, is_direct_init = false;
13409 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13410 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13411 &non_constant_p);
13412 if (initializer == NULL_TREE
13413 || (TREE_CODE (initializer) == TREE_LIST
13414 && TREE_CHAIN (initializer))
13415 || (is_direct_init
13416 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13417 && CONSTRUCTOR_NELTS (initializer) != 1))
13419 error_at (loc, "invalid initializer for structured binding "
13420 "declaration");
13421 initializer = error_mark_node;
13424 if (decl != error_mark_node)
13426 cp_maybe_mangle_decomp (decl, prev, v.length ());
13427 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13428 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13429 cp_finish_decomp (decl, prev, v.length ());
13432 else if (decl != error_mark_node)
13434 *maybe_range_for_decl = prev;
13435 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13436 the underlying DECL. */
13437 cp_finish_decomp (decl, prev, v.length ());
13440 if (pushed_scope)
13441 pop_scope (pushed_scope);
13443 if (decl == error_mark_node && DECL_P (orig_decl))
13445 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13446 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13449 return decl;
13452 /* Parse a decl-specifier-seq.
13454 decl-specifier-seq:
13455 decl-specifier-seq [opt] decl-specifier
13456 decl-specifier attribute-specifier-seq [opt] (C++11)
13458 decl-specifier:
13459 storage-class-specifier
13460 type-specifier
13461 function-specifier
13462 friend
13463 typedef
13465 GNU Extension:
13467 decl-specifier:
13468 attributes
13470 Concepts Extension:
13472 decl-specifier:
13473 concept
13475 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13477 The parser flags FLAGS is used to control type-specifier parsing.
13479 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13480 flags:
13482 1: one of the decl-specifiers is an elaborated-type-specifier
13483 (i.e., a type declaration)
13484 2: one of the decl-specifiers is an enum-specifier or a
13485 class-specifier (i.e., a type definition)
13489 static void
13490 cp_parser_decl_specifier_seq (cp_parser* parser,
13491 cp_parser_flags flags,
13492 cp_decl_specifier_seq *decl_specs,
13493 int* declares_class_or_enum)
13495 bool constructor_possible_p = !parser->in_declarator_p;
13496 bool found_decl_spec = false;
13497 cp_token *start_token = NULL;
13498 cp_decl_spec ds;
13500 /* Clear DECL_SPECS. */
13501 clear_decl_specs (decl_specs);
13503 /* Assume no class or enumeration type is declared. */
13504 *declares_class_or_enum = 0;
13506 /* Keep reading specifiers until there are no more to read. */
13507 while (true)
13509 bool constructor_p;
13510 cp_token *token;
13511 ds = ds_last;
13513 /* Peek at the next token. */
13514 token = cp_lexer_peek_token (parser->lexer);
13516 /* Save the first token of the decl spec list for error
13517 reporting. */
13518 if (!start_token)
13519 start_token = token;
13520 /* Handle attributes. */
13521 if (cp_next_tokens_can_be_attribute_p (parser))
13523 /* Parse the attributes. */
13524 tree attrs = cp_parser_attributes_opt (parser);
13526 /* In a sequence of declaration specifiers, c++11 attributes
13527 appertain to the type that precede them. In that case
13528 [dcl.spec]/1 says:
13530 The attribute-specifier-seq affects the type only for
13531 the declaration it appears in, not other declarations
13532 involving the same type.
13534 But for now let's force the user to position the
13535 attribute either at the beginning of the declaration or
13536 after the declarator-id, which would clearly mean that it
13537 applies to the declarator. */
13538 if (cxx11_attribute_p (attrs))
13540 if (!found_decl_spec)
13541 /* The c++11 attribute is at the beginning of the
13542 declaration. It appertains to the entity being
13543 declared. */;
13544 else
13546 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13548 /* This is an attribute following a
13549 class-specifier. */
13550 if (decl_specs->type_definition_p)
13551 warn_misplaced_attr_for_class_type (token->location,
13552 decl_specs->type);
13553 attrs = NULL_TREE;
13555 else
13557 decl_specs->std_attributes
13558 = attr_chainon (decl_specs->std_attributes, attrs);
13559 if (decl_specs->locations[ds_std_attribute] == 0)
13560 decl_specs->locations[ds_std_attribute] = token->location;
13562 continue;
13566 decl_specs->attributes
13567 = attr_chainon (decl_specs->attributes, attrs);
13568 if (decl_specs->locations[ds_attribute] == 0)
13569 decl_specs->locations[ds_attribute] = token->location;
13570 continue;
13572 /* Assume we will find a decl-specifier keyword. */
13573 found_decl_spec = true;
13574 /* If the next token is an appropriate keyword, we can simply
13575 add it to the list. */
13576 switch (token->keyword)
13578 /* decl-specifier:
13579 friend
13580 constexpr */
13581 case RID_FRIEND:
13582 if (!at_class_scope_p ())
13584 gcc_rich_location richloc (token->location);
13585 richloc.add_fixit_remove ();
13586 error_at (&richloc, "%<friend%> used outside of class");
13587 cp_lexer_purge_token (parser->lexer);
13589 else
13591 ds = ds_friend;
13592 /* Consume the token. */
13593 cp_lexer_consume_token (parser->lexer);
13595 break;
13597 case RID_CONSTEXPR:
13598 ds = ds_constexpr;
13599 cp_lexer_consume_token (parser->lexer);
13600 break;
13602 case RID_CONCEPT:
13603 ds = ds_concept;
13604 cp_lexer_consume_token (parser->lexer);
13605 break;
13607 /* function-specifier:
13608 inline
13609 virtual
13610 explicit */
13611 case RID_INLINE:
13612 case RID_VIRTUAL:
13613 case RID_EXPLICIT:
13614 cp_parser_function_specifier_opt (parser, decl_specs);
13615 break;
13617 /* decl-specifier:
13618 typedef */
13619 case RID_TYPEDEF:
13620 ds = ds_typedef;
13621 /* Consume the token. */
13622 cp_lexer_consume_token (parser->lexer);
13623 /* A constructor declarator cannot appear in a typedef. */
13624 constructor_possible_p = false;
13625 /* The "typedef" keyword can only occur in a declaration; we
13626 may as well commit at this point. */
13627 cp_parser_commit_to_tentative_parse (parser);
13629 if (decl_specs->storage_class != sc_none)
13630 decl_specs->conflicting_specifiers_p = true;
13631 break;
13633 /* storage-class-specifier:
13634 auto
13635 register
13636 static
13637 extern
13638 mutable
13640 GNU Extension:
13641 thread */
13642 case RID_AUTO:
13643 if (cxx_dialect == cxx98)
13645 /* Consume the token. */
13646 cp_lexer_consume_token (parser->lexer);
13648 /* Complain about `auto' as a storage specifier, if
13649 we're complaining about C++0x compatibility. */
13650 gcc_rich_location richloc (token->location);
13651 richloc.add_fixit_remove ();
13652 warning_at (&richloc, OPT_Wc__11_compat,
13653 "%<auto%> changes meaning in C++11; "
13654 "please remove it");
13656 /* Set the storage class anyway. */
13657 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13658 token);
13660 else
13661 /* C++0x auto type-specifier. */
13662 found_decl_spec = false;
13663 break;
13665 case RID_REGISTER:
13666 case RID_STATIC:
13667 case RID_EXTERN:
13668 case RID_MUTABLE:
13669 /* Consume the token. */
13670 cp_lexer_consume_token (parser->lexer);
13671 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13672 token);
13673 break;
13674 case RID_THREAD:
13675 /* Consume the token. */
13676 ds = ds_thread;
13677 cp_lexer_consume_token (parser->lexer);
13678 break;
13680 default:
13681 /* We did not yet find a decl-specifier yet. */
13682 found_decl_spec = false;
13683 break;
13686 if (found_decl_spec
13687 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13688 && token->keyword != RID_CONSTEXPR)
13689 error ("decl-specifier invalid in condition");
13691 if (found_decl_spec
13692 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13693 && token->keyword != RID_MUTABLE
13694 && token->keyword != RID_CONSTEXPR)
13695 error_at (token->location, "%qD invalid in lambda",
13696 ridpointers[token->keyword]);
13698 if (ds != ds_last)
13699 set_and_check_decl_spec_loc (decl_specs, ds, token);
13701 /* Constructors are a special case. The `S' in `S()' is not a
13702 decl-specifier; it is the beginning of the declarator. */
13703 constructor_p
13704 = (!found_decl_spec
13705 && constructor_possible_p
13706 && (cp_parser_constructor_declarator_p
13707 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13709 /* If we don't have a DECL_SPEC yet, then we must be looking at
13710 a type-specifier. */
13711 if (!found_decl_spec && !constructor_p)
13713 int decl_spec_declares_class_or_enum;
13714 bool is_cv_qualifier;
13715 tree type_spec;
13717 type_spec
13718 = cp_parser_type_specifier (parser, flags,
13719 decl_specs,
13720 /*is_declaration=*/true,
13721 &decl_spec_declares_class_or_enum,
13722 &is_cv_qualifier);
13723 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13725 /* If this type-specifier referenced a user-defined type
13726 (a typedef, class-name, etc.), then we can't allow any
13727 more such type-specifiers henceforth.
13729 [dcl.spec]
13731 The longest sequence of decl-specifiers that could
13732 possibly be a type name is taken as the
13733 decl-specifier-seq of a declaration. The sequence shall
13734 be self-consistent as described below.
13736 [dcl.type]
13738 As a general rule, at most one type-specifier is allowed
13739 in the complete decl-specifier-seq of a declaration. The
13740 only exceptions are the following:
13742 -- const or volatile can be combined with any other
13743 type-specifier.
13745 -- signed or unsigned can be combined with char, long,
13746 short, or int.
13748 -- ..
13750 Example:
13752 typedef char* Pc;
13753 void g (const int Pc);
13755 Here, Pc is *not* part of the decl-specifier seq; it's
13756 the declarator. Therefore, once we see a type-specifier
13757 (other than a cv-qualifier), we forbid any additional
13758 user-defined types. We *do* still allow things like `int
13759 int' to be considered a decl-specifier-seq, and issue the
13760 error message later. */
13761 if (type_spec && !is_cv_qualifier)
13762 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13763 /* A constructor declarator cannot follow a type-specifier. */
13764 if (type_spec)
13766 constructor_possible_p = false;
13767 found_decl_spec = true;
13768 if (!is_cv_qualifier)
13769 decl_specs->any_type_specifiers_p = true;
13773 /* If we still do not have a DECL_SPEC, then there are no more
13774 decl-specifiers. */
13775 if (!found_decl_spec)
13776 break;
13778 decl_specs->any_specifiers_p = true;
13779 /* After we see one decl-specifier, further decl-specifiers are
13780 always optional. */
13781 flags |= CP_PARSER_FLAGS_OPTIONAL;
13784 /* Don't allow a friend specifier with a class definition. */
13785 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13786 && (*declares_class_or_enum & 2))
13787 error_at (decl_specs->locations[ds_friend],
13788 "class definition may not be declared a friend");
13791 /* Parse an (optional) storage-class-specifier.
13793 storage-class-specifier:
13794 auto
13795 register
13796 static
13797 extern
13798 mutable
13800 GNU Extension:
13802 storage-class-specifier:
13803 thread
13805 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13807 static tree
13808 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13810 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13812 case RID_AUTO:
13813 if (cxx_dialect != cxx98)
13814 return NULL_TREE;
13815 /* Fall through for C++98. */
13816 gcc_fallthrough ();
13818 case RID_REGISTER:
13819 case RID_STATIC:
13820 case RID_EXTERN:
13821 case RID_MUTABLE:
13822 case RID_THREAD:
13823 /* Consume the token. */
13824 return cp_lexer_consume_token (parser->lexer)->u.value;
13826 default:
13827 return NULL_TREE;
13831 /* Parse an (optional) function-specifier.
13833 function-specifier:
13834 inline
13835 virtual
13836 explicit
13838 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13839 Updates DECL_SPECS, if it is non-NULL. */
13841 static tree
13842 cp_parser_function_specifier_opt (cp_parser* parser,
13843 cp_decl_specifier_seq *decl_specs)
13845 cp_token *token = cp_lexer_peek_token (parser->lexer);
13846 switch (token->keyword)
13848 case RID_INLINE:
13849 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13850 break;
13852 case RID_VIRTUAL:
13853 /* 14.5.2.3 [temp.mem]
13855 A member function template shall not be virtual. */
13856 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13857 && current_class_type)
13858 error_at (token->location, "templates may not be %<virtual%>");
13859 else
13860 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13861 break;
13863 case RID_EXPLICIT:
13864 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13865 break;
13867 default:
13868 return NULL_TREE;
13871 /* Consume the token. */
13872 return cp_lexer_consume_token (parser->lexer)->u.value;
13875 /* Parse a linkage-specification.
13877 linkage-specification:
13878 extern string-literal { declaration-seq [opt] }
13879 extern string-literal declaration */
13881 static void
13882 cp_parser_linkage_specification (cp_parser* parser)
13884 tree linkage;
13886 /* Look for the `extern' keyword. */
13887 cp_token *extern_token
13888 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13890 /* Look for the string-literal. */
13891 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13892 linkage = cp_parser_string_literal (parser, false, false);
13894 /* Transform the literal into an identifier. If the literal is a
13895 wide-character string, or contains embedded NULs, then we can't
13896 handle it as the user wants. */
13897 if (strlen (TREE_STRING_POINTER (linkage))
13898 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13900 cp_parser_error (parser, "invalid linkage-specification");
13901 /* Assume C++ linkage. */
13902 linkage = lang_name_cplusplus;
13904 else
13905 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13907 /* We're now using the new linkage. */
13908 push_lang_context (linkage);
13910 /* Preserve the location of the the innermost linkage specification,
13911 tracking the locations of nested specifications via a local. */
13912 location_t saved_location
13913 = parser->innermost_linkage_specification_location;
13914 /* Construct a location ranging from the start of the "extern" to
13915 the end of the string-literal, with the caret at the start, e.g.:
13916 extern "C" {
13917 ^~~~~~~~~~
13919 parser->innermost_linkage_specification_location
13920 = make_location (extern_token->location,
13921 extern_token->location,
13922 get_finish (string_token->location));
13924 /* If the next token is a `{', then we're using the first
13925 production. */
13926 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13928 cp_ensure_no_omp_declare_simd (parser);
13929 cp_ensure_no_oacc_routine (parser);
13931 /* Consume the `{' token. */
13932 matching_braces braces;
13933 braces.consume_open (parser)->location;
13934 /* Parse the declarations. */
13935 cp_parser_declaration_seq_opt (parser);
13936 /* Look for the closing `}'. */
13937 braces.require_close (parser);
13939 /* Otherwise, there's just one declaration. */
13940 else
13942 bool saved_in_unbraced_linkage_specification_p;
13944 saved_in_unbraced_linkage_specification_p
13945 = parser->in_unbraced_linkage_specification_p;
13946 parser->in_unbraced_linkage_specification_p = true;
13947 cp_parser_declaration (parser);
13948 parser->in_unbraced_linkage_specification_p
13949 = saved_in_unbraced_linkage_specification_p;
13952 /* We're done with the linkage-specification. */
13953 pop_lang_context ();
13955 /* Restore location of parent linkage specification, if any. */
13956 parser->innermost_linkage_specification_location = saved_location;
13959 /* Parse a static_assert-declaration.
13961 static_assert-declaration:
13962 static_assert ( constant-expression , string-literal ) ;
13963 static_assert ( constant-expression ) ; (C++17)
13965 If MEMBER_P, this static_assert is a class member. */
13967 static void
13968 cp_parser_static_assert(cp_parser *parser, bool member_p)
13970 cp_expr condition;
13971 location_t token_loc;
13972 tree message;
13973 bool dummy;
13975 /* Peek at the `static_assert' token so we can keep track of exactly
13976 where the static assertion started. */
13977 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13979 /* Look for the `static_assert' keyword. */
13980 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13981 RT_STATIC_ASSERT))
13982 return;
13984 /* We know we are in a static assertion; commit to any tentative
13985 parse. */
13986 if (cp_parser_parsing_tentatively (parser))
13987 cp_parser_commit_to_tentative_parse (parser);
13989 /* Parse the `(' starting the static assertion condition. */
13990 matching_parens parens;
13991 parens.require_open (parser);
13993 /* Parse the constant-expression. Allow a non-constant expression
13994 here in order to give better diagnostics in finish_static_assert. */
13995 condition =
13996 cp_parser_constant_expression (parser,
13997 /*allow_non_constant_p=*/true,
13998 /*non_constant_p=*/&dummy);
14000 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14002 if (cxx_dialect < cxx17)
14003 pedwarn (input_location, OPT_Wpedantic,
14004 "static_assert without a message "
14005 "only available with -std=c++17 or -std=gnu++17");
14006 /* Eat the ')' */
14007 cp_lexer_consume_token (parser->lexer);
14008 message = build_string (1, "");
14009 TREE_TYPE (message) = char_array_type_node;
14010 fix_string_type (message);
14012 else
14014 /* Parse the separating `,'. */
14015 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14017 /* Parse the string-literal message. */
14018 message = cp_parser_string_literal (parser,
14019 /*translate=*/false,
14020 /*wide_ok=*/true);
14022 /* A `)' completes the static assertion. */
14023 if (!parens.require_close (parser))
14024 cp_parser_skip_to_closing_parenthesis (parser,
14025 /*recovering=*/true,
14026 /*or_comma=*/false,
14027 /*consume_paren=*/true);
14030 /* A semicolon terminates the declaration. */
14031 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14033 /* Get the location for the static assertion. Use that of the
14034 condition if available, otherwise, use that of the "static_assert"
14035 token. */
14036 location_t assert_loc = condition.get_location ();
14037 if (assert_loc == UNKNOWN_LOCATION)
14038 assert_loc = token_loc;
14040 /* Complete the static assertion, which may mean either processing
14041 the static assert now or saving it for template instantiation. */
14042 finish_static_assert (condition, message, assert_loc, member_p);
14045 /* Parse the expression in decltype ( expression ). */
14047 static tree
14048 cp_parser_decltype_expr (cp_parser *parser,
14049 bool &id_expression_or_member_access_p)
14051 cp_token *id_expr_start_token;
14052 tree expr;
14054 /* Since we're going to preserve any side-effects from this parse, set up a
14055 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14056 in the expression. */
14057 tentative_firewall firewall (parser);
14059 /* First, try parsing an id-expression. */
14060 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14061 cp_parser_parse_tentatively (parser);
14062 expr = cp_parser_id_expression (parser,
14063 /*template_keyword_p=*/false,
14064 /*check_dependency_p=*/true,
14065 /*template_p=*/NULL,
14066 /*declarator_p=*/false,
14067 /*optional_p=*/false);
14069 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14071 bool non_integral_constant_expression_p = false;
14072 tree id_expression = expr;
14073 cp_id_kind idk;
14074 const char *error_msg;
14076 if (identifier_p (expr))
14077 /* Lookup the name we got back from the id-expression. */
14078 expr = cp_parser_lookup_name_simple (parser, expr,
14079 id_expr_start_token->location);
14081 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14082 /* A template without args is not a complete id-expression. */
14083 expr = error_mark_node;
14085 if (expr
14086 && expr != error_mark_node
14087 && TREE_CODE (expr) != TYPE_DECL
14088 && (TREE_CODE (expr) != BIT_NOT_EXPR
14089 || !TYPE_P (TREE_OPERAND (expr, 0)))
14090 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14092 /* Complete lookup of the id-expression. */
14093 expr = (finish_id_expression
14094 (id_expression, expr, parser->scope, &idk,
14095 /*integral_constant_expression_p=*/false,
14096 /*allow_non_integral_constant_expression_p=*/true,
14097 &non_integral_constant_expression_p,
14098 /*template_p=*/false,
14099 /*done=*/true,
14100 /*address_p=*/false,
14101 /*template_arg_p=*/false,
14102 &error_msg,
14103 id_expr_start_token->location));
14105 if (expr == error_mark_node)
14106 /* We found an id-expression, but it was something that we
14107 should not have found. This is an error, not something
14108 we can recover from, so note that we found an
14109 id-expression and we'll recover as gracefully as
14110 possible. */
14111 id_expression_or_member_access_p = true;
14114 if (expr
14115 && expr != error_mark_node
14116 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14117 /* We have an id-expression. */
14118 id_expression_or_member_access_p = true;
14121 if (!id_expression_or_member_access_p)
14123 /* Abort the id-expression parse. */
14124 cp_parser_abort_tentative_parse (parser);
14126 /* Parsing tentatively, again. */
14127 cp_parser_parse_tentatively (parser);
14129 /* Parse a class member access. */
14130 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14131 /*cast_p=*/false, /*decltype*/true,
14132 /*member_access_only_p=*/true, NULL);
14134 if (expr
14135 && expr != error_mark_node
14136 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14137 /* We have an id-expression. */
14138 id_expression_or_member_access_p = true;
14141 if (id_expression_or_member_access_p)
14142 /* We have parsed the complete id-expression or member access. */
14143 cp_parser_parse_definitely (parser);
14144 else
14146 /* Abort our attempt to parse an id-expression or member access
14147 expression. */
14148 cp_parser_abort_tentative_parse (parser);
14150 /* Commit to the tentative_firewall so we get syntax errors. */
14151 cp_parser_commit_to_tentative_parse (parser);
14153 /* Parse a full expression. */
14154 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14155 /*decltype_p=*/true);
14158 return expr;
14161 /* Parse a `decltype' type. Returns the type.
14163 simple-type-specifier:
14164 decltype ( expression )
14165 C++14 proposal:
14166 decltype ( auto ) */
14168 static tree
14169 cp_parser_decltype (cp_parser *parser)
14171 bool id_expression_or_member_access_p = false;
14172 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14174 if (start_token->type == CPP_DECLTYPE)
14176 /* Already parsed. */
14177 cp_lexer_consume_token (parser->lexer);
14178 return saved_checks_value (start_token->u.tree_check_value);
14181 /* Look for the `decltype' token. */
14182 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14183 return error_mark_node;
14185 /* Parse the opening `('. */
14186 matching_parens parens;
14187 if (!parens.require_open (parser))
14188 return error_mark_node;
14190 push_deferring_access_checks (dk_deferred);
14192 tree expr = NULL_TREE;
14194 if (cxx_dialect >= cxx14
14195 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14196 /* decltype (auto) */
14197 cp_lexer_consume_token (parser->lexer);
14198 else
14200 /* decltype (expression) */
14202 /* Types cannot be defined in a `decltype' expression. Save away the
14203 old message and set the new one. */
14204 const char *saved_message = parser->type_definition_forbidden_message;
14205 parser->type_definition_forbidden_message
14206 = G_("types may not be defined in %<decltype%> expressions");
14208 /* The restrictions on constant-expressions do not apply inside
14209 decltype expressions. */
14210 bool saved_integral_constant_expression_p
14211 = parser->integral_constant_expression_p;
14212 bool saved_non_integral_constant_expression_p
14213 = parser->non_integral_constant_expression_p;
14214 parser->integral_constant_expression_p = false;
14216 /* Within a parenthesized expression, a `>' token is always
14217 the greater-than operator. */
14218 bool saved_greater_than_is_operator_p
14219 = parser->greater_than_is_operator_p;
14220 parser->greater_than_is_operator_p = true;
14222 /* Do not actually evaluate the expression. */
14223 ++cp_unevaluated_operand;
14225 /* Do not warn about problems with the expression. */
14226 ++c_inhibit_evaluation_warnings;
14228 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14230 /* Go back to evaluating expressions. */
14231 --cp_unevaluated_operand;
14232 --c_inhibit_evaluation_warnings;
14234 /* The `>' token might be the end of a template-id or
14235 template-parameter-list now. */
14236 parser->greater_than_is_operator_p
14237 = saved_greater_than_is_operator_p;
14239 /* Restore the old message and the integral constant expression
14240 flags. */
14241 parser->type_definition_forbidden_message = saved_message;
14242 parser->integral_constant_expression_p
14243 = saved_integral_constant_expression_p;
14244 parser->non_integral_constant_expression_p
14245 = saved_non_integral_constant_expression_p;
14248 /* Parse to the closing `)'. */
14249 if (!parens.require_close (parser))
14251 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14252 /*consume_paren=*/true);
14253 pop_deferring_access_checks ();
14254 return error_mark_node;
14257 if (!expr)
14259 /* Build auto. */
14260 expr = make_decltype_auto ();
14261 AUTO_IS_DECLTYPE (expr) = true;
14263 else
14264 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14265 tf_warning_or_error);
14267 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14268 it again. */
14269 start_token->type = CPP_DECLTYPE;
14270 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14271 start_token->u.tree_check_value->value = expr;
14272 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14273 start_token->keyword = RID_MAX;
14274 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14276 pop_to_parent_deferring_access_checks ();
14278 return expr;
14281 /* Special member functions [gram.special] */
14283 /* Parse a conversion-function-id.
14285 conversion-function-id:
14286 operator conversion-type-id
14288 Returns an IDENTIFIER_NODE representing the operator. */
14290 static tree
14291 cp_parser_conversion_function_id (cp_parser* parser)
14293 tree type;
14294 tree saved_scope;
14295 tree saved_qualifying_scope;
14296 tree saved_object_scope;
14297 tree pushed_scope = NULL_TREE;
14299 /* Look for the `operator' token. */
14300 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14301 return error_mark_node;
14302 /* When we parse the conversion-type-id, the current scope will be
14303 reset. However, we need that information in able to look up the
14304 conversion function later, so we save it here. */
14305 saved_scope = parser->scope;
14306 saved_qualifying_scope = parser->qualifying_scope;
14307 saved_object_scope = parser->object_scope;
14308 /* We must enter the scope of the class so that the names of
14309 entities declared within the class are available in the
14310 conversion-type-id. For example, consider:
14312 struct S {
14313 typedef int I;
14314 operator I();
14317 S::operator I() { ... }
14319 In order to see that `I' is a type-name in the definition, we
14320 must be in the scope of `S'. */
14321 if (saved_scope)
14322 pushed_scope = push_scope (saved_scope);
14323 /* Parse the conversion-type-id. */
14324 type = cp_parser_conversion_type_id (parser);
14325 /* Leave the scope of the class, if any. */
14326 if (pushed_scope)
14327 pop_scope (pushed_scope);
14328 /* Restore the saved scope. */
14329 parser->scope = saved_scope;
14330 parser->qualifying_scope = saved_qualifying_scope;
14331 parser->object_scope = saved_object_scope;
14332 /* If the TYPE is invalid, indicate failure. */
14333 if (type == error_mark_node)
14334 return error_mark_node;
14335 return make_conv_op_name (type);
14338 /* Parse a conversion-type-id:
14340 conversion-type-id:
14341 type-specifier-seq conversion-declarator [opt]
14343 Returns the TYPE specified. */
14345 static tree
14346 cp_parser_conversion_type_id (cp_parser* parser)
14348 tree attributes;
14349 cp_decl_specifier_seq type_specifiers;
14350 cp_declarator *declarator;
14351 tree type_specified;
14352 const char *saved_message;
14354 /* Parse the attributes. */
14355 attributes = cp_parser_attributes_opt (parser);
14357 saved_message = parser->type_definition_forbidden_message;
14358 parser->type_definition_forbidden_message
14359 = G_("types may not be defined in a conversion-type-id");
14361 /* Parse the type-specifiers. */
14362 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14363 /*is_trailing_return=*/false,
14364 &type_specifiers);
14366 parser->type_definition_forbidden_message = saved_message;
14368 /* If that didn't work, stop. */
14369 if (type_specifiers.type == error_mark_node)
14370 return error_mark_node;
14371 /* Parse the conversion-declarator. */
14372 declarator = cp_parser_conversion_declarator_opt (parser);
14374 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14375 /*initialized=*/0, &attributes);
14376 if (attributes)
14377 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14379 /* Don't give this error when parsing tentatively. This happens to
14380 work because we always parse this definitively once. */
14381 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14382 && type_uses_auto (type_specified))
14384 if (cxx_dialect < cxx14)
14386 error ("invalid use of %<auto%> in conversion operator");
14387 return error_mark_node;
14389 else if (template_parm_scope_p ())
14390 warning (0, "use of %<auto%> in member template "
14391 "conversion operator can never be deduced");
14394 return type_specified;
14397 /* Parse an (optional) conversion-declarator.
14399 conversion-declarator:
14400 ptr-operator conversion-declarator [opt]
14404 static cp_declarator *
14405 cp_parser_conversion_declarator_opt (cp_parser* parser)
14407 enum tree_code code;
14408 tree class_type, std_attributes = NULL_TREE;
14409 cp_cv_quals cv_quals;
14411 /* We don't know if there's a ptr-operator next, or not. */
14412 cp_parser_parse_tentatively (parser);
14413 /* Try the ptr-operator. */
14414 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14415 &std_attributes);
14416 /* If it worked, look for more conversion-declarators. */
14417 if (cp_parser_parse_definitely (parser))
14419 cp_declarator *declarator;
14421 /* Parse another optional declarator. */
14422 declarator = cp_parser_conversion_declarator_opt (parser);
14424 declarator = cp_parser_make_indirect_declarator
14425 (code, class_type, cv_quals, declarator, std_attributes);
14427 return declarator;
14430 return NULL;
14433 /* Parse an (optional) ctor-initializer.
14435 ctor-initializer:
14436 : mem-initializer-list */
14438 static void
14439 cp_parser_ctor_initializer_opt (cp_parser* parser)
14441 /* If the next token is not a `:', then there is no
14442 ctor-initializer. */
14443 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14445 /* Do default initialization of any bases and members. */
14446 if (DECL_CONSTRUCTOR_P (current_function_decl))
14447 finish_mem_initializers (NULL_TREE);
14448 return;
14451 /* Consume the `:' token. */
14452 cp_lexer_consume_token (parser->lexer);
14453 /* And the mem-initializer-list. */
14454 cp_parser_mem_initializer_list (parser);
14457 /* Parse a mem-initializer-list.
14459 mem-initializer-list:
14460 mem-initializer ... [opt]
14461 mem-initializer ... [opt] , mem-initializer-list */
14463 static void
14464 cp_parser_mem_initializer_list (cp_parser* parser)
14466 tree mem_initializer_list = NULL_TREE;
14467 tree target_ctor = error_mark_node;
14468 cp_token *token = cp_lexer_peek_token (parser->lexer);
14470 /* Let the semantic analysis code know that we are starting the
14471 mem-initializer-list. */
14472 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14473 error_at (token->location,
14474 "only constructors take member initializers");
14476 /* Loop through the list. */
14477 while (true)
14479 tree mem_initializer;
14481 token = cp_lexer_peek_token (parser->lexer);
14482 /* Parse the mem-initializer. */
14483 mem_initializer = cp_parser_mem_initializer (parser);
14484 /* If the next token is a `...', we're expanding member initializers. */
14485 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14486 if (ellipsis
14487 || (mem_initializer != error_mark_node
14488 && check_for_bare_parameter_packs (TREE_PURPOSE
14489 (mem_initializer))))
14491 /* Consume the `...'. */
14492 if (ellipsis)
14493 cp_lexer_consume_token (parser->lexer);
14495 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14496 can be expanded but members cannot. */
14497 if (mem_initializer != error_mark_node
14498 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14500 error_at (token->location,
14501 "cannot expand initializer for member %qD",
14502 TREE_PURPOSE (mem_initializer));
14503 mem_initializer = error_mark_node;
14506 /* Construct the pack expansion type. */
14507 if (mem_initializer != error_mark_node)
14508 mem_initializer = make_pack_expansion (mem_initializer);
14510 if (target_ctor != error_mark_node
14511 && mem_initializer != error_mark_node)
14513 error ("mem-initializer for %qD follows constructor delegation",
14514 TREE_PURPOSE (mem_initializer));
14515 mem_initializer = error_mark_node;
14517 /* Look for a target constructor. */
14518 if (mem_initializer != error_mark_node
14519 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14520 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14522 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14523 if (mem_initializer_list)
14525 error ("constructor delegation follows mem-initializer for %qD",
14526 TREE_PURPOSE (mem_initializer_list));
14527 mem_initializer = error_mark_node;
14529 target_ctor = mem_initializer;
14531 /* Add it to the list, unless it was erroneous. */
14532 if (mem_initializer != error_mark_node)
14534 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14535 mem_initializer_list = mem_initializer;
14537 /* If the next token is not a `,', we're done. */
14538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14539 break;
14540 /* Consume the `,' token. */
14541 cp_lexer_consume_token (parser->lexer);
14544 /* Perform semantic analysis. */
14545 if (DECL_CONSTRUCTOR_P (current_function_decl))
14546 finish_mem_initializers (mem_initializer_list);
14549 /* Parse a mem-initializer.
14551 mem-initializer:
14552 mem-initializer-id ( expression-list [opt] )
14553 mem-initializer-id braced-init-list
14555 GNU extension:
14557 mem-initializer:
14558 ( expression-list [opt] )
14560 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14561 class) or FIELD_DECL (for a non-static data member) to initialize;
14562 the TREE_VALUE is the expression-list. An empty initialization
14563 list is represented by void_list_node. */
14565 static tree
14566 cp_parser_mem_initializer (cp_parser* parser)
14568 tree mem_initializer_id;
14569 tree expression_list;
14570 tree member;
14571 cp_token *token = cp_lexer_peek_token (parser->lexer);
14573 /* Find out what is being initialized. */
14574 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14576 permerror (token->location,
14577 "anachronistic old-style base class initializer");
14578 mem_initializer_id = NULL_TREE;
14580 else
14582 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14583 if (mem_initializer_id == error_mark_node)
14584 return mem_initializer_id;
14586 member = expand_member_init (mem_initializer_id);
14587 if (member && !DECL_P (member))
14588 in_base_initializer = 1;
14590 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14592 bool expr_non_constant_p;
14593 cp_lexer_set_source_position (parser->lexer);
14594 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14595 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14596 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14597 expression_list = build_tree_list (NULL_TREE, expression_list);
14599 else
14601 vec<tree, va_gc> *vec;
14602 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14603 /*cast_p=*/false,
14604 /*allow_expansion_p=*/true,
14605 /*non_constant_p=*/NULL);
14606 if (vec == NULL)
14607 return error_mark_node;
14608 expression_list = build_tree_list_vec (vec);
14609 release_tree_vector (vec);
14612 if (expression_list == error_mark_node)
14613 return error_mark_node;
14614 if (!expression_list)
14615 expression_list = void_type_node;
14617 in_base_initializer = 0;
14619 return member ? build_tree_list (member, expression_list) : error_mark_node;
14622 /* Parse a mem-initializer-id.
14624 mem-initializer-id:
14625 :: [opt] nested-name-specifier [opt] class-name
14626 decltype-specifier (C++11)
14627 identifier
14629 Returns a TYPE indicating the class to be initialized for the first
14630 production (and the second in C++11). Returns an IDENTIFIER_NODE
14631 indicating the data member to be initialized for the last production. */
14633 static tree
14634 cp_parser_mem_initializer_id (cp_parser* parser)
14636 bool global_scope_p;
14637 bool nested_name_specifier_p;
14638 bool template_p = false;
14639 tree id;
14641 cp_token *token = cp_lexer_peek_token (parser->lexer);
14643 /* `typename' is not allowed in this context ([temp.res]). */
14644 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14646 error_at (token->location,
14647 "keyword %<typename%> not allowed in this context (a qualified "
14648 "member initializer is implicitly a type)");
14649 cp_lexer_consume_token (parser->lexer);
14651 /* Look for the optional `::' operator. */
14652 global_scope_p
14653 = (cp_parser_global_scope_opt (parser,
14654 /*current_scope_valid_p=*/false)
14655 != NULL_TREE);
14656 /* Look for the optional nested-name-specifier. The simplest way to
14657 implement:
14659 [temp.res]
14661 The keyword `typename' is not permitted in a base-specifier or
14662 mem-initializer; in these contexts a qualified name that
14663 depends on a template-parameter is implicitly assumed to be a
14664 type name.
14666 is to assume that we have seen the `typename' keyword at this
14667 point. */
14668 nested_name_specifier_p
14669 = (cp_parser_nested_name_specifier_opt (parser,
14670 /*typename_keyword_p=*/true,
14671 /*check_dependency_p=*/true,
14672 /*type_p=*/true,
14673 /*is_declaration=*/true)
14674 != NULL_TREE);
14675 if (nested_name_specifier_p)
14676 template_p = cp_parser_optional_template_keyword (parser);
14677 /* If there is a `::' operator or a nested-name-specifier, then we
14678 are definitely looking for a class-name. */
14679 if (global_scope_p || nested_name_specifier_p)
14680 return cp_parser_class_name (parser,
14681 /*typename_keyword_p=*/true,
14682 /*template_keyword_p=*/template_p,
14683 typename_type,
14684 /*check_dependency_p=*/true,
14685 /*class_head_p=*/false,
14686 /*is_declaration=*/true);
14687 /* Otherwise, we could also be looking for an ordinary identifier. */
14688 cp_parser_parse_tentatively (parser);
14689 if (cp_lexer_next_token_is_decltype (parser->lexer))
14690 /* Try a decltype-specifier. */
14691 id = cp_parser_decltype (parser);
14692 else
14693 /* Otherwise, try a class-name. */
14694 id = cp_parser_class_name (parser,
14695 /*typename_keyword_p=*/true,
14696 /*template_keyword_p=*/false,
14697 none_type,
14698 /*check_dependency_p=*/true,
14699 /*class_head_p=*/false,
14700 /*is_declaration=*/true);
14701 /* If we found one, we're done. */
14702 if (cp_parser_parse_definitely (parser))
14703 return id;
14704 /* Otherwise, look for an ordinary identifier. */
14705 return cp_parser_identifier (parser);
14708 /* Overloading [gram.over] */
14710 /* Parse an operator-function-id.
14712 operator-function-id:
14713 operator operator
14715 Returns an IDENTIFIER_NODE for the operator which is a
14716 human-readable spelling of the identifier, e.g., `operator +'. */
14718 static cp_expr
14719 cp_parser_operator_function_id (cp_parser* parser)
14721 /* Look for the `operator' keyword. */
14722 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14723 return error_mark_node;
14724 /* And then the name of the operator itself. */
14725 return cp_parser_operator (parser);
14728 /* Return an identifier node for a user-defined literal operator.
14729 The suffix identifier is chained to the operator name identifier. */
14731 tree
14732 cp_literal_operator_id (const char* name)
14734 tree identifier;
14735 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14736 + strlen (name) + 10);
14737 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14738 identifier = get_identifier (buffer);
14740 return identifier;
14743 /* Parse an operator.
14745 operator:
14746 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14747 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14748 || ++ -- , ->* -> () []
14750 GNU Extensions:
14752 operator:
14753 <? >? <?= >?=
14755 Returns an IDENTIFIER_NODE for the operator which is a
14756 human-readable spelling of the identifier, e.g., `operator +'. */
14758 static cp_expr
14759 cp_parser_operator (cp_parser* parser)
14761 tree id = NULL_TREE;
14762 cp_token *token;
14763 bool utf8 = false;
14765 /* Peek at the next token. */
14766 token = cp_lexer_peek_token (parser->lexer);
14768 location_t start_loc = token->location;
14770 /* Figure out which operator we have. */
14771 enum tree_code op = ERROR_MARK;
14772 bool assop = false;
14773 bool consumed = false;
14774 switch (token->type)
14776 case CPP_KEYWORD:
14778 /* The keyword should be either `new' or `delete'. */
14779 if (token->keyword == RID_NEW)
14780 op = NEW_EXPR;
14781 else if (token->keyword == RID_DELETE)
14782 op = DELETE_EXPR;
14783 else
14784 break;
14786 /* Consume the `new' or `delete' token. */
14787 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14789 /* Peek at the next token. */
14790 token = cp_lexer_peek_token (parser->lexer);
14791 /* If it's a `[' token then this is the array variant of the
14792 operator. */
14793 if (token->type == CPP_OPEN_SQUARE)
14795 /* Consume the `[' token. */
14796 cp_lexer_consume_token (parser->lexer);
14797 /* Look for the `]' token. */
14798 if (cp_token *close_token
14799 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14800 end_loc = close_token->location;
14801 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14803 start_loc = make_location (start_loc, start_loc, end_loc);
14804 consumed = true;
14805 break;
14808 case CPP_PLUS:
14809 op = PLUS_EXPR;
14810 break;
14812 case CPP_MINUS:
14813 op = MINUS_EXPR;
14814 break;
14816 case CPP_MULT:
14817 op = MULT_EXPR;
14818 break;
14820 case CPP_DIV:
14821 op = TRUNC_DIV_EXPR;
14822 break;
14824 case CPP_MOD:
14825 op = TRUNC_MOD_EXPR;
14826 break;
14828 case CPP_XOR:
14829 op = BIT_XOR_EXPR;
14830 break;
14832 case CPP_AND:
14833 op = BIT_AND_EXPR;
14834 break;
14836 case CPP_OR:
14837 op = BIT_IOR_EXPR;
14838 break;
14840 case CPP_COMPL:
14841 op = BIT_NOT_EXPR;
14842 break;
14844 case CPP_NOT:
14845 op = TRUTH_NOT_EXPR;
14846 break;
14848 case CPP_EQ:
14849 assop = true;
14850 op = NOP_EXPR;
14851 break;
14853 case CPP_LESS:
14854 op = LT_EXPR;
14855 break;
14857 case CPP_GREATER:
14858 op = GT_EXPR;
14859 break;
14861 case CPP_PLUS_EQ:
14862 assop = true;
14863 op = PLUS_EXPR;
14864 break;
14866 case CPP_MINUS_EQ:
14867 assop = true;
14868 op = MINUS_EXPR;
14869 break;
14871 case CPP_MULT_EQ:
14872 assop = true;
14873 op = MULT_EXPR;
14874 break;
14876 case CPP_DIV_EQ:
14877 assop = true;
14878 op = TRUNC_DIV_EXPR;
14879 break;
14881 case CPP_MOD_EQ:
14882 assop = true;
14883 op = TRUNC_MOD_EXPR;
14884 break;
14886 case CPP_XOR_EQ:
14887 assop = true;
14888 op = BIT_XOR_EXPR;
14889 break;
14891 case CPP_AND_EQ:
14892 assop = true;
14893 op = BIT_AND_EXPR;
14894 break;
14896 case CPP_OR_EQ:
14897 assop = true;
14898 op = BIT_IOR_EXPR;
14899 break;
14901 case CPP_LSHIFT:
14902 op = LSHIFT_EXPR;
14903 break;
14905 case CPP_RSHIFT:
14906 op = RSHIFT_EXPR;
14907 break;
14909 case CPP_LSHIFT_EQ:
14910 assop = true;
14911 op = LSHIFT_EXPR;
14912 break;
14914 case CPP_RSHIFT_EQ:
14915 assop = true;
14916 op = RSHIFT_EXPR;
14917 break;
14919 case CPP_EQ_EQ:
14920 op = EQ_EXPR;
14921 break;
14923 case CPP_NOT_EQ:
14924 op = NE_EXPR;
14925 break;
14927 case CPP_LESS_EQ:
14928 op = LE_EXPR;
14929 break;
14931 case CPP_GREATER_EQ:
14932 op = GE_EXPR;
14933 break;
14935 case CPP_AND_AND:
14936 op = TRUTH_ANDIF_EXPR;
14937 break;
14939 case CPP_OR_OR:
14940 op = TRUTH_ORIF_EXPR;
14941 break;
14943 case CPP_PLUS_PLUS:
14944 op = POSTINCREMENT_EXPR;
14945 break;
14947 case CPP_MINUS_MINUS:
14948 op = PREDECREMENT_EXPR;
14949 break;
14951 case CPP_COMMA:
14952 op = COMPOUND_EXPR;
14953 break;
14955 case CPP_DEREF_STAR:
14956 op = MEMBER_REF;
14957 break;
14959 case CPP_DEREF:
14960 op = COMPONENT_REF;
14961 break;
14963 case CPP_OPEN_PAREN:
14965 /* Consume the `('. */
14966 matching_parens parens;
14967 parens.consume_open (parser);
14968 /* Look for the matching `)'. */
14969 parens.require_close (parser);
14970 op = CALL_EXPR;
14971 consumed = true;
14972 break;
14975 case CPP_OPEN_SQUARE:
14976 /* Consume the `['. */
14977 cp_lexer_consume_token (parser->lexer);
14978 /* Look for the matching `]'. */
14979 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14980 op = ARRAY_REF;
14981 consumed = true;
14982 break;
14984 case CPP_UTF8STRING:
14985 case CPP_UTF8STRING_USERDEF:
14986 utf8 = true;
14987 /* FALLTHRU */
14988 case CPP_STRING:
14989 case CPP_WSTRING:
14990 case CPP_STRING16:
14991 case CPP_STRING32:
14992 case CPP_STRING_USERDEF:
14993 case CPP_WSTRING_USERDEF:
14994 case CPP_STRING16_USERDEF:
14995 case CPP_STRING32_USERDEF:
14997 tree str, string_tree;
14998 int sz, len;
15000 if (cxx_dialect == cxx98)
15001 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15003 /* Consume the string. */
15004 str = cp_parser_string_literal (parser, /*translate=*/true,
15005 /*wide_ok=*/true, /*lookup_udlit=*/false);
15006 if (str == error_mark_node)
15007 return error_mark_node;
15008 else if (TREE_CODE (str) == USERDEF_LITERAL)
15010 string_tree = USERDEF_LITERAL_VALUE (str);
15011 id = USERDEF_LITERAL_SUFFIX_ID (str);
15013 else
15015 string_tree = str;
15016 /* Look for the suffix identifier. */
15017 token = cp_lexer_peek_token (parser->lexer);
15018 if (token->type == CPP_NAME)
15019 id = cp_parser_identifier (parser);
15020 else if (token->type == CPP_KEYWORD)
15022 error ("unexpected keyword;"
15023 " remove space between quotes and suffix identifier");
15024 return error_mark_node;
15026 else
15028 error ("expected suffix identifier");
15029 return error_mark_node;
15032 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15033 (TREE_TYPE (TREE_TYPE (string_tree))));
15034 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15035 if (len != 0)
15037 error ("expected empty string after %<operator%> keyword");
15038 return error_mark_node;
15040 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15041 != char_type_node)
15043 error ("invalid encoding prefix in literal operator");
15044 return error_mark_node;
15046 if (id != error_mark_node)
15048 const char *name = IDENTIFIER_POINTER (id);
15049 id = cp_literal_operator_id (name);
15051 return id;
15054 default:
15055 /* Anything else is an error. */
15056 break;
15059 /* If we have selected an identifier, we need to consume the
15060 operator token. */
15061 if (op != ERROR_MARK)
15063 id = ovl_op_identifier (assop, op);
15064 if (!consumed)
15065 cp_lexer_consume_token (parser->lexer);
15067 /* Otherwise, no valid operator name was present. */
15068 else
15070 cp_parser_error (parser, "expected operator");
15071 id = error_mark_node;
15074 return cp_expr (id, start_loc);
15077 /* Parse a template-declaration.
15079 template-declaration:
15080 export [opt] template < template-parameter-list > declaration
15082 If MEMBER_P is TRUE, this template-declaration occurs within a
15083 class-specifier.
15085 The grammar rule given by the standard isn't correct. What
15086 is really meant is:
15088 template-declaration:
15089 export [opt] template-parameter-list-seq
15090 decl-specifier-seq [opt] init-declarator [opt] ;
15091 export [opt] template-parameter-list-seq
15092 function-definition
15094 template-parameter-list-seq:
15095 template-parameter-list-seq [opt]
15096 template < template-parameter-list >
15098 Concept Extensions:
15100 template-parameter-list-seq:
15101 template < template-parameter-list > requires-clause [opt]
15103 requires-clause:
15104 requires logical-or-expression */
15106 static void
15107 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15109 /* Check for `export'. */
15110 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15112 /* Consume the `export' token. */
15113 cp_lexer_consume_token (parser->lexer);
15114 /* Warn that we do not support `export'. */
15115 warning (0, "keyword %<export%> not implemented, and will be ignored");
15118 cp_parser_template_declaration_after_export (parser, member_p);
15121 /* Parse a template-parameter-list.
15123 template-parameter-list:
15124 template-parameter
15125 template-parameter-list , template-parameter
15127 Returns a TREE_LIST. Each node represents a template parameter.
15128 The nodes are connected via their TREE_CHAINs. */
15130 static tree
15131 cp_parser_template_parameter_list (cp_parser* parser)
15133 tree parameter_list = NULL_TREE;
15135 begin_template_parm_list ();
15137 /* The loop below parses the template parms. We first need to know
15138 the total number of template parms to be able to compute proper
15139 canonical types of each dependent type. So after the loop, when
15140 we know the total number of template parms,
15141 end_template_parm_list computes the proper canonical types and
15142 fixes up the dependent types accordingly. */
15143 while (true)
15145 tree parameter;
15146 bool is_non_type;
15147 bool is_parameter_pack;
15148 location_t parm_loc;
15150 /* Parse the template-parameter. */
15151 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15152 parameter = cp_parser_template_parameter (parser,
15153 &is_non_type,
15154 &is_parameter_pack);
15155 /* Add it to the list. */
15156 if (parameter != error_mark_node)
15157 parameter_list = process_template_parm (parameter_list,
15158 parm_loc,
15159 parameter,
15160 is_non_type,
15161 is_parameter_pack);
15162 else
15164 tree err_parm = build_tree_list (parameter, parameter);
15165 parameter_list = chainon (parameter_list, err_parm);
15168 /* If the next token is not a `,', we're done. */
15169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15170 break;
15171 /* Otherwise, consume the `,' token. */
15172 cp_lexer_consume_token (parser->lexer);
15175 return end_template_parm_list (parameter_list);
15178 /* Parse a introduction-list.
15180 introduction-list:
15181 introduced-parameter
15182 introduction-list , introduced-parameter
15184 introduced-parameter:
15185 ...[opt] identifier
15187 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15188 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15189 WILDCARD_DECL will also have DECL_NAME set and token location in
15190 DECL_SOURCE_LOCATION. */
15192 static tree
15193 cp_parser_introduction_list (cp_parser *parser)
15195 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15197 while (true)
15199 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15200 if (is_pack)
15201 cp_lexer_consume_token (parser->lexer);
15203 /* Build placeholder. */
15204 tree parm = build_nt (WILDCARD_DECL);
15205 DECL_SOURCE_LOCATION (parm)
15206 = cp_lexer_peek_token (parser->lexer)->location;
15207 DECL_NAME (parm) = cp_parser_identifier (parser);
15208 WILDCARD_PACK_P (parm) = is_pack;
15209 vec_safe_push (introduction_vec, parm);
15211 /* If the next token is not a `,', we're done. */
15212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15213 break;
15214 /* Otherwise, consume the `,' token. */
15215 cp_lexer_consume_token (parser->lexer);
15218 /* Convert the vec into a TREE_VEC. */
15219 tree introduction_list = make_tree_vec (introduction_vec->length ());
15220 unsigned int n;
15221 tree parm;
15222 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15223 TREE_VEC_ELT (introduction_list, n) = parm;
15225 release_tree_vector (introduction_vec);
15226 return introduction_list;
15229 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15230 is an abstract declarator. */
15232 static inline cp_declarator*
15233 get_id_declarator (cp_declarator *declarator)
15235 cp_declarator *d = declarator;
15236 while (d && d->kind != cdk_id)
15237 d = d->declarator;
15238 return d;
15241 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15242 is an abstract declarator. */
15244 static inline tree
15245 get_unqualified_id (cp_declarator *declarator)
15247 declarator = get_id_declarator (declarator);
15248 if (declarator)
15249 return declarator->u.id.unqualified_name;
15250 else
15251 return NULL_TREE;
15254 /* Returns true if DECL represents a constrained-parameter. */
15256 static inline bool
15257 is_constrained_parameter (tree decl)
15259 return (decl
15260 && TREE_CODE (decl) == TYPE_DECL
15261 && CONSTRAINED_PARM_CONCEPT (decl)
15262 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15265 /* Returns true if PARM declares a constrained-parameter. */
15267 static inline bool
15268 is_constrained_parameter (cp_parameter_declarator *parm)
15270 return is_constrained_parameter (parm->decl_specifiers.type);
15273 /* Check that the type parameter is only a declarator-id, and that its
15274 type is not cv-qualified. */
15276 bool
15277 cp_parser_check_constrained_type_parm (cp_parser *parser,
15278 cp_parameter_declarator *parm)
15280 if (!parm->declarator)
15281 return true;
15283 if (parm->declarator->kind != cdk_id)
15285 cp_parser_error (parser, "invalid constrained type parameter");
15286 return false;
15289 /* Don't allow cv-qualified type parameters. */
15290 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15291 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15293 cp_parser_error (parser, "cv-qualified type parameter");
15294 return false;
15297 return true;
15300 /* Finish parsing/processing a template type parameter and checking
15301 various restrictions. */
15303 static inline tree
15304 cp_parser_constrained_type_template_parm (cp_parser *parser,
15305 tree id,
15306 cp_parameter_declarator* parmdecl)
15308 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15309 return finish_template_type_parm (class_type_node, id);
15310 else
15311 return error_mark_node;
15314 static tree
15315 finish_constrained_template_template_parm (tree proto, tree id)
15317 /* FIXME: This should probably be copied, and we may need to adjust
15318 the template parameter depths. */
15319 tree saved_parms = current_template_parms;
15320 begin_template_parm_list ();
15321 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15322 end_template_parm_list ();
15324 tree parm = finish_template_template_parm (class_type_node, id);
15325 current_template_parms = saved_parms;
15327 return parm;
15330 /* Finish parsing/processing a template template parameter by borrowing
15331 the template parameter list from the prototype parameter. */
15333 static tree
15334 cp_parser_constrained_template_template_parm (cp_parser *parser,
15335 tree proto,
15336 tree id,
15337 cp_parameter_declarator *parmdecl)
15339 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15340 return error_mark_node;
15341 return finish_constrained_template_template_parm (proto, id);
15344 /* Create a new non-type template parameter from the given PARM
15345 declarator. */
15347 static tree
15348 constrained_non_type_template_parm (bool *is_non_type,
15349 cp_parameter_declarator *parm)
15351 *is_non_type = true;
15352 cp_declarator *decl = parm->declarator;
15353 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15354 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15355 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15358 /* Build a constrained template parameter based on the PARMDECL
15359 declarator. The type of PARMDECL is the constrained type, which
15360 refers to the prototype template parameter that ultimately
15361 specifies the type of the declared parameter. */
15363 static tree
15364 finish_constrained_parameter (cp_parser *parser,
15365 cp_parameter_declarator *parmdecl,
15366 bool *is_non_type,
15367 bool *is_parameter_pack)
15369 tree decl = parmdecl->decl_specifiers.type;
15370 tree id = get_unqualified_id (parmdecl->declarator);
15371 tree def = parmdecl->default_argument;
15372 tree proto = DECL_INITIAL (decl);
15374 /* A template parameter constrained by a variadic concept shall also
15375 be declared as a template parameter pack. */
15376 bool is_variadic = template_parameter_pack_p (proto);
15377 if (is_variadic && !*is_parameter_pack)
15378 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15380 /* Build the parameter. Return an error if the declarator was invalid. */
15381 tree parm;
15382 if (TREE_CODE (proto) == TYPE_DECL)
15383 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15384 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15385 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15386 parmdecl);
15387 else
15388 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15389 if (parm == error_mark_node)
15390 return error_mark_node;
15392 /* Finish the parameter decl and create a node attaching the
15393 default argument and constraint. */
15394 parm = build_tree_list (def, parm);
15395 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15397 return parm;
15400 /* Returns true if the parsed type actually represents the declaration
15401 of a type template-parameter. */
15403 static inline bool
15404 declares_constrained_type_template_parameter (tree type)
15406 return (is_constrained_parameter (type)
15407 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15411 /* Returns true if the parsed type actually represents the declaration of
15412 a template template-parameter. */
15414 static bool
15415 declares_constrained_template_template_parameter (tree type)
15417 return (is_constrained_parameter (type)
15418 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15421 /* Parse a default argument for a type template-parameter.
15422 Note that diagnostics are handled in cp_parser_template_parameter. */
15424 static tree
15425 cp_parser_default_type_template_argument (cp_parser *parser)
15427 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15429 /* Consume the `=' token. */
15430 cp_lexer_consume_token (parser->lexer);
15432 cp_token *token = cp_lexer_peek_token (parser->lexer);
15434 /* Parse the default-argument. */
15435 push_deferring_access_checks (dk_no_deferred);
15436 tree default_argument = cp_parser_type_id (parser);
15437 pop_deferring_access_checks ();
15439 if (flag_concepts && type_uses_auto (default_argument))
15441 error_at (token->location,
15442 "invalid use of %<auto%> in default template argument");
15443 return error_mark_node;
15446 return default_argument;
15449 /* Parse a default argument for a template template-parameter. */
15451 static tree
15452 cp_parser_default_template_template_argument (cp_parser *parser)
15454 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15456 bool is_template;
15458 /* Consume the `='. */
15459 cp_lexer_consume_token (parser->lexer);
15460 /* Parse the id-expression. */
15461 push_deferring_access_checks (dk_no_deferred);
15462 /* save token before parsing the id-expression, for error
15463 reporting */
15464 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15465 tree default_argument
15466 = cp_parser_id_expression (parser,
15467 /*template_keyword_p=*/false,
15468 /*check_dependency_p=*/true,
15469 /*template_p=*/&is_template,
15470 /*declarator_p=*/false,
15471 /*optional_p=*/false);
15472 if (TREE_CODE (default_argument) == TYPE_DECL)
15473 /* If the id-expression was a template-id that refers to
15474 a template-class, we already have the declaration here,
15475 so no further lookup is needed. */
15477 else
15478 /* Look up the name. */
15479 default_argument
15480 = cp_parser_lookup_name (parser, default_argument,
15481 none_type,
15482 /*is_template=*/is_template,
15483 /*is_namespace=*/false,
15484 /*check_dependency=*/true,
15485 /*ambiguous_decls=*/NULL,
15486 token->location);
15487 /* See if the default argument is valid. */
15488 default_argument = check_template_template_default_arg (default_argument);
15489 pop_deferring_access_checks ();
15490 return default_argument;
15493 /* Parse a template-parameter.
15495 template-parameter:
15496 type-parameter
15497 parameter-declaration
15499 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15500 the parameter. The TREE_PURPOSE is the default value, if any.
15501 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15502 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15503 set to true iff this parameter is a parameter pack. */
15505 static tree
15506 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15507 bool *is_parameter_pack)
15509 cp_token *token;
15510 cp_parameter_declarator *parameter_declarator;
15511 tree parm;
15513 /* Assume it is a type parameter or a template parameter. */
15514 *is_non_type = false;
15515 /* Assume it not a parameter pack. */
15516 *is_parameter_pack = false;
15517 /* Peek at the next token. */
15518 token = cp_lexer_peek_token (parser->lexer);
15519 /* If it is `template', we have a type-parameter. */
15520 if (token->keyword == RID_TEMPLATE)
15521 return cp_parser_type_parameter (parser, is_parameter_pack);
15522 /* If it is `class' or `typename' we do not know yet whether it is a
15523 type parameter or a non-type parameter. Consider:
15525 template <typename T, typename T::X X> ...
15529 template <class C, class D*> ...
15531 Here, the first parameter is a type parameter, and the second is
15532 a non-type parameter. We can tell by looking at the token after
15533 the identifier -- if it is a `,', `=', or `>' then we have a type
15534 parameter. */
15535 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15537 /* Peek at the token after `class' or `typename'. */
15538 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15539 /* If it's an ellipsis, we have a template type parameter
15540 pack. */
15541 if (token->type == CPP_ELLIPSIS)
15542 return cp_parser_type_parameter (parser, is_parameter_pack);
15543 /* If it's an identifier, skip it. */
15544 if (token->type == CPP_NAME)
15545 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15546 /* Now, see if the token looks like the end of a template
15547 parameter. */
15548 if (token->type == CPP_COMMA
15549 || token->type == CPP_EQ
15550 || token->type == CPP_GREATER)
15551 return cp_parser_type_parameter (parser, is_parameter_pack);
15554 /* Otherwise, it is a non-type parameter or a constrained parameter.
15556 [temp.param]
15558 When parsing a default template-argument for a non-type
15559 template-parameter, the first non-nested `>' is taken as the end
15560 of the template parameter-list rather than a greater-than
15561 operator. */
15562 parameter_declarator
15563 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15564 /*parenthesized_p=*/NULL);
15566 if (!parameter_declarator)
15567 return error_mark_node;
15569 /* If the parameter declaration is marked as a parameter pack, set
15570 *IS_PARAMETER_PACK to notify the caller. */
15571 if (parameter_declarator->template_parameter_pack_p)
15572 *is_parameter_pack = true;
15574 if (parameter_declarator->default_argument)
15576 /* Can happen in some cases of erroneous input (c++/34892). */
15577 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15578 /* Consume the `...' for better error recovery. */
15579 cp_lexer_consume_token (parser->lexer);
15582 // The parameter may have been constrained.
15583 if (is_constrained_parameter (parameter_declarator))
15584 return finish_constrained_parameter (parser,
15585 parameter_declarator,
15586 is_non_type,
15587 is_parameter_pack);
15589 // Now we're sure that the parameter is a non-type parameter.
15590 *is_non_type = true;
15592 parm = grokdeclarator (parameter_declarator->declarator,
15593 &parameter_declarator->decl_specifiers,
15594 TPARM, /*initialized=*/0,
15595 /*attrlist=*/NULL);
15596 if (parm == error_mark_node)
15597 return error_mark_node;
15599 return build_tree_list (parameter_declarator->default_argument, parm);
15602 /* Parse a type-parameter.
15604 type-parameter:
15605 class identifier [opt]
15606 class identifier [opt] = type-id
15607 typename identifier [opt]
15608 typename identifier [opt] = type-id
15609 template < template-parameter-list > class identifier [opt]
15610 template < template-parameter-list > class identifier [opt]
15611 = id-expression
15613 GNU Extension (variadic templates):
15615 type-parameter:
15616 class ... identifier [opt]
15617 typename ... identifier [opt]
15619 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15620 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15621 the declaration of the parameter.
15623 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15625 static tree
15626 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15628 cp_token *token;
15629 tree parameter;
15631 /* Look for a keyword to tell us what kind of parameter this is. */
15632 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15633 if (!token)
15634 return error_mark_node;
15636 switch (token->keyword)
15638 case RID_CLASS:
15639 case RID_TYPENAME:
15641 tree identifier;
15642 tree default_argument;
15644 /* If the next token is an ellipsis, we have a template
15645 argument pack. */
15646 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15648 /* Consume the `...' token. */
15649 cp_lexer_consume_token (parser->lexer);
15650 maybe_warn_variadic_templates ();
15652 *is_parameter_pack = true;
15655 /* If the next token is an identifier, then it names the
15656 parameter. */
15657 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15658 identifier = cp_parser_identifier (parser);
15659 else
15660 identifier = NULL_TREE;
15662 /* Create the parameter. */
15663 parameter = finish_template_type_parm (class_type_node, identifier);
15665 /* If the next token is an `=', we have a default argument. */
15666 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15668 default_argument
15669 = cp_parser_default_type_template_argument (parser);
15671 /* Template parameter packs cannot have default
15672 arguments. */
15673 if (*is_parameter_pack)
15675 if (identifier)
15676 error_at (token->location,
15677 "template parameter pack %qD cannot have a "
15678 "default argument", identifier);
15679 else
15680 error_at (token->location,
15681 "template parameter packs cannot have "
15682 "default arguments");
15683 default_argument = NULL_TREE;
15685 else if (check_for_bare_parameter_packs (default_argument))
15686 default_argument = error_mark_node;
15688 else
15689 default_argument = NULL_TREE;
15691 /* Create the combined representation of the parameter and the
15692 default argument. */
15693 parameter = build_tree_list (default_argument, parameter);
15695 break;
15697 case RID_TEMPLATE:
15699 tree identifier;
15700 tree default_argument;
15702 /* Look for the `<'. */
15703 cp_parser_require (parser, CPP_LESS, RT_LESS);
15704 /* Parse the template-parameter-list. */
15705 cp_parser_template_parameter_list (parser);
15706 /* Look for the `>'. */
15707 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15709 // If template requirements are present, parse them.
15710 if (flag_concepts)
15712 tree reqs = get_shorthand_constraints (current_template_parms);
15713 if (tree r = cp_parser_requires_clause_opt (parser))
15714 reqs = conjoin_constraints (reqs, normalize_expression (r));
15715 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15718 /* Look for the `class' or 'typename' keywords. */
15719 cp_parser_type_parameter_key (parser);
15720 /* If the next token is an ellipsis, we have a template
15721 argument pack. */
15722 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15724 /* Consume the `...' token. */
15725 cp_lexer_consume_token (parser->lexer);
15726 maybe_warn_variadic_templates ();
15728 *is_parameter_pack = true;
15730 /* If the next token is an `=', then there is a
15731 default-argument. If the next token is a `>', we are at
15732 the end of the parameter-list. If the next token is a `,',
15733 then we are at the end of this parameter. */
15734 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15735 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15736 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15738 identifier = cp_parser_identifier (parser);
15739 /* Treat invalid names as if the parameter were nameless. */
15740 if (identifier == error_mark_node)
15741 identifier = NULL_TREE;
15743 else
15744 identifier = NULL_TREE;
15746 /* Create the template parameter. */
15747 parameter = finish_template_template_parm (class_type_node,
15748 identifier);
15750 /* If the next token is an `=', then there is a
15751 default-argument. */
15752 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15754 default_argument
15755 = cp_parser_default_template_template_argument (parser);
15757 /* Template parameter packs cannot have default
15758 arguments. */
15759 if (*is_parameter_pack)
15761 if (identifier)
15762 error_at (token->location,
15763 "template parameter pack %qD cannot "
15764 "have a default argument",
15765 identifier);
15766 else
15767 error_at (token->location, "template parameter packs cannot "
15768 "have default arguments");
15769 default_argument = NULL_TREE;
15772 else
15773 default_argument = NULL_TREE;
15775 /* Create the combined representation of the parameter and the
15776 default argument. */
15777 parameter = build_tree_list (default_argument, parameter);
15779 break;
15781 default:
15782 gcc_unreachable ();
15783 break;
15786 return parameter;
15789 /* Parse a template-id.
15791 template-id:
15792 template-name < template-argument-list [opt] >
15794 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15795 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15796 returned. Otherwise, if the template-name names a function, or set
15797 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15798 names a class, returns a TYPE_DECL for the specialization.
15800 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15801 uninstantiated templates. */
15803 static tree
15804 cp_parser_template_id (cp_parser *parser,
15805 bool template_keyword_p,
15806 bool check_dependency_p,
15807 enum tag_types tag_type,
15808 bool is_declaration)
15810 tree templ;
15811 tree arguments;
15812 tree template_id;
15813 cp_token_position start_of_id = 0;
15814 cp_token *next_token = NULL, *next_token_2 = NULL;
15815 bool is_identifier;
15817 /* If the next token corresponds to a template-id, there is no need
15818 to reparse it. */
15819 cp_token *token = cp_lexer_peek_token (parser->lexer);
15820 if (token->type == CPP_TEMPLATE_ID)
15822 cp_lexer_consume_token (parser->lexer);
15823 return saved_checks_value (token->u.tree_check_value);
15826 /* Avoid performing name lookup if there is no possibility of
15827 finding a template-id. */
15828 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15829 || (token->type == CPP_NAME
15830 && !cp_parser_nth_token_starts_template_argument_list_p
15831 (parser, 2)))
15833 cp_parser_error (parser, "expected template-id");
15834 return error_mark_node;
15837 /* Remember where the template-id starts. */
15838 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15839 start_of_id = cp_lexer_token_position (parser->lexer, false);
15841 push_deferring_access_checks (dk_deferred);
15843 /* Parse the template-name. */
15844 is_identifier = false;
15845 templ = cp_parser_template_name (parser, template_keyword_p,
15846 check_dependency_p,
15847 is_declaration,
15848 tag_type,
15849 &is_identifier);
15850 if (templ == error_mark_node || is_identifier)
15852 pop_deferring_access_checks ();
15853 return templ;
15856 /* Since we're going to preserve any side-effects from this parse, set up a
15857 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15858 in the template arguments. */
15859 tentative_firewall firewall (parser);
15861 /* If we find the sequence `[:' after a template-name, it's probably
15862 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15863 parse correctly the argument list. */
15864 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15865 == CPP_OPEN_SQUARE)
15866 && next_token->flags & DIGRAPH
15867 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15868 == CPP_COLON)
15869 && !(next_token_2->flags & PREV_WHITE))
15871 cp_parser_parse_tentatively (parser);
15872 /* Change `:' into `::'. */
15873 next_token_2->type = CPP_SCOPE;
15874 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15875 CPP_LESS. */
15876 cp_lexer_consume_token (parser->lexer);
15878 /* Parse the arguments. */
15879 arguments = cp_parser_enclosed_template_argument_list (parser);
15880 if (!cp_parser_parse_definitely (parser))
15882 /* If we couldn't parse an argument list, then we revert our changes
15883 and return simply an error. Maybe this is not a template-id
15884 after all. */
15885 next_token_2->type = CPP_COLON;
15886 cp_parser_error (parser, "expected %<<%>");
15887 pop_deferring_access_checks ();
15888 return error_mark_node;
15890 /* Otherwise, emit an error about the invalid digraph, but continue
15891 parsing because we got our argument list. */
15892 if (permerror (next_token->location,
15893 "%<<::%> cannot begin a template-argument list"))
15895 static bool hint = false;
15896 inform (next_token->location,
15897 "%<<:%> is an alternate spelling for %<[%>."
15898 " Insert whitespace between %<<%> and %<::%>");
15899 if (!hint && !flag_permissive)
15901 inform (next_token->location, "(if you use %<-fpermissive%> "
15902 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15903 "accept your code)");
15904 hint = true;
15908 else
15910 /* Look for the `<' that starts the template-argument-list. */
15911 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15913 pop_deferring_access_checks ();
15914 return error_mark_node;
15916 /* Parse the arguments. */
15917 arguments = cp_parser_enclosed_template_argument_list (parser);
15920 /* Set the location to be of the form:
15921 template-name < template-argument-list [opt] >
15922 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15923 with caret == start at the start of the template-name,
15924 ranging until the closing '>'. */
15925 location_t finish_loc
15926 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15927 location_t combined_loc
15928 = make_location (token->location, token->location, finish_loc);
15930 /* Check for concepts autos where they don't belong. We could
15931 identify types in some cases of idnetifier TEMPL, looking ahead
15932 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15933 types. We reject them in functions, but if what we have is an
15934 identifier, even with none_type we can't conclude it's NOT a
15935 type, we have to wait for template substitution. */
15936 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15937 template_id = error_mark_node;
15938 /* Build a representation of the specialization. */
15939 else if (identifier_p (templ))
15940 template_id = build_min_nt_loc (combined_loc,
15941 TEMPLATE_ID_EXPR,
15942 templ, arguments);
15943 else if (DECL_TYPE_TEMPLATE_P (templ)
15944 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15946 bool entering_scope;
15947 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15948 template (rather than some instantiation thereof) only if
15949 is not nested within some other construct. For example, in
15950 "template <typename T> void f(T) { A<T>::", A<T> is just an
15951 instantiation of A. */
15952 entering_scope = (template_parm_scope_p ()
15953 && cp_lexer_next_token_is (parser->lexer,
15954 CPP_SCOPE));
15955 template_id
15956 = finish_template_type (templ, arguments, entering_scope);
15958 /* A template-like identifier may be a partial concept id. */
15959 else if (flag_concepts
15960 && (template_id = (cp_parser_maybe_partial_concept_id
15961 (parser, templ, arguments))))
15962 return template_id;
15963 else if (variable_template_p (templ))
15965 template_id = lookup_template_variable (templ, arguments);
15966 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15967 SET_EXPR_LOCATION (template_id, combined_loc);
15969 else
15971 /* If it's not a class-template or a template-template, it should be
15972 a function-template. */
15973 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15974 || TREE_CODE (templ) == OVERLOAD
15975 || BASELINK_P (templ)));
15977 template_id = lookup_template_function (templ, arguments);
15978 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15979 SET_EXPR_LOCATION (template_id, combined_loc);
15982 /* If parsing tentatively, replace the sequence of tokens that makes
15983 up the template-id with a CPP_TEMPLATE_ID token. That way,
15984 should we re-parse the token stream, we will not have to repeat
15985 the effort required to do the parse, nor will we issue duplicate
15986 error messages about problems during instantiation of the
15987 template. */
15988 if (start_of_id
15989 /* Don't do this if we had a parse error in a declarator; re-parsing
15990 might succeed if a name changes meaning (60361). */
15991 && !(cp_parser_error_occurred (parser)
15992 && cp_parser_parsing_tentatively (parser)
15993 && parser->in_declarator_p))
15995 /* Reset the contents of the START_OF_ID token. */
15996 token->type = CPP_TEMPLATE_ID;
15997 token->location = combined_loc;
15999 /* We must mark the lookup as kept, so we don't throw it away on
16000 the first parse. */
16001 if (is_overloaded_fn (template_id))
16002 lookup_keep (get_fns (template_id), true);
16004 /* Retrieve any deferred checks. Do not pop this access checks yet
16005 so the memory will not be reclaimed during token replacing below. */
16006 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16007 token->u.tree_check_value->value = template_id;
16008 token->u.tree_check_value->checks = get_deferred_access_checks ();
16009 token->keyword = RID_MAX;
16011 /* Purge all subsequent tokens. */
16012 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16014 /* ??? Can we actually assume that, if template_id ==
16015 error_mark_node, we will have issued a diagnostic to the
16016 user, as opposed to simply marking the tentative parse as
16017 failed? */
16018 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16019 error_at (token->location, "parse error in template argument list");
16022 pop_to_parent_deferring_access_checks ();
16023 return template_id;
16026 /* Parse a template-name.
16028 template-name:
16029 identifier
16031 The standard should actually say:
16033 template-name:
16034 identifier
16035 operator-function-id
16037 A defect report has been filed about this issue.
16039 A conversion-function-id cannot be a template name because they cannot
16040 be part of a template-id. In fact, looking at this code:
16042 a.operator K<int>()
16044 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16045 It is impossible to call a templated conversion-function-id with an
16046 explicit argument list, since the only allowed template parameter is
16047 the type to which it is converting.
16049 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16050 `template' keyword, in a construction like:
16052 T::template f<3>()
16054 In that case `f' is taken to be a template-name, even though there
16055 is no way of knowing for sure.
16057 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16058 name refers to a set of overloaded functions, at least one of which
16059 is a template, or an IDENTIFIER_NODE with the name of the template,
16060 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16061 names are looked up inside uninstantiated templates. */
16063 static tree
16064 cp_parser_template_name (cp_parser* parser,
16065 bool template_keyword_p,
16066 bool check_dependency_p,
16067 bool is_declaration,
16068 enum tag_types tag_type,
16069 bool *is_identifier)
16071 tree identifier;
16072 tree decl;
16073 cp_token *token = cp_lexer_peek_token (parser->lexer);
16075 /* If the next token is `operator', then we have either an
16076 operator-function-id or a conversion-function-id. */
16077 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16079 /* We don't know whether we're looking at an
16080 operator-function-id or a conversion-function-id. */
16081 cp_parser_parse_tentatively (parser);
16082 /* Try an operator-function-id. */
16083 identifier = cp_parser_operator_function_id (parser);
16084 /* If that didn't work, try a conversion-function-id. */
16085 if (!cp_parser_parse_definitely (parser))
16087 cp_parser_error (parser, "expected template-name");
16088 return error_mark_node;
16091 /* Look for the identifier. */
16092 else
16093 identifier = cp_parser_identifier (parser);
16095 /* If we didn't find an identifier, we don't have a template-id. */
16096 if (identifier == error_mark_node)
16097 return error_mark_node;
16099 /* If the name immediately followed the `template' keyword, then it
16100 is a template-name. However, if the next token is not `<', then
16101 we do not treat it as a template-name, since it is not being used
16102 as part of a template-id. This enables us to handle constructs
16103 like:
16105 template <typename T> struct S { S(); };
16106 template <typename T> S<T>::S();
16108 correctly. We would treat `S' as a template -- if it were `S<T>'
16109 -- but we do not if there is no `<'. */
16111 if (processing_template_decl
16112 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16114 /* In a declaration, in a dependent context, we pretend that the
16115 "template" keyword was present in order to improve error
16116 recovery. For example, given:
16118 template <typename T> void f(T::X<int>);
16120 we want to treat "X<int>" as a template-id. */
16121 if (is_declaration
16122 && !template_keyword_p
16123 && parser->scope && TYPE_P (parser->scope)
16124 && check_dependency_p
16125 && dependent_scope_p (parser->scope)
16126 /* Do not do this for dtors (or ctors), since they never
16127 need the template keyword before their name. */
16128 && !constructor_name_p (identifier, parser->scope))
16130 cp_token_position start = 0;
16132 /* Explain what went wrong. */
16133 error_at (token->location, "non-template %qD used as template",
16134 identifier);
16135 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16136 parser->scope, identifier);
16137 /* If parsing tentatively, find the location of the "<" token. */
16138 if (cp_parser_simulate_error (parser))
16139 start = cp_lexer_token_position (parser->lexer, true);
16140 /* Parse the template arguments so that we can issue error
16141 messages about them. */
16142 cp_lexer_consume_token (parser->lexer);
16143 cp_parser_enclosed_template_argument_list (parser);
16144 /* Skip tokens until we find a good place from which to
16145 continue parsing. */
16146 cp_parser_skip_to_closing_parenthesis (parser,
16147 /*recovering=*/true,
16148 /*or_comma=*/true,
16149 /*consume_paren=*/false);
16150 /* If parsing tentatively, permanently remove the
16151 template argument list. That will prevent duplicate
16152 error messages from being issued about the missing
16153 "template" keyword. */
16154 if (start)
16155 cp_lexer_purge_tokens_after (parser->lexer, start);
16156 if (is_identifier)
16157 *is_identifier = true;
16158 parser->context->object_type = NULL_TREE;
16159 return identifier;
16162 /* If the "template" keyword is present, then there is generally
16163 no point in doing name-lookup, so we just return IDENTIFIER.
16164 But, if the qualifying scope is non-dependent then we can
16165 (and must) do name-lookup normally. */
16166 if (template_keyword_p)
16168 tree scope = (parser->scope ? parser->scope
16169 : parser->context->object_type);
16170 if (scope && TYPE_P (scope)
16171 && (!CLASS_TYPE_P (scope)
16172 || (check_dependency_p && dependent_type_p (scope))))
16174 /* We're optimizing away the call to cp_parser_lookup_name, but
16175 we still need to do this. */
16176 parser->context->object_type = NULL_TREE;
16177 return identifier;
16182 /* Look up the name. */
16183 decl = cp_parser_lookup_name (parser, identifier,
16184 tag_type,
16185 /*is_template=*/true,
16186 /*is_namespace=*/false,
16187 check_dependency_p,
16188 /*ambiguous_decls=*/NULL,
16189 token->location);
16191 decl = strip_using_decl (decl);
16193 /* If DECL is a template, then the name was a template-name. */
16194 if (TREE_CODE (decl) == TEMPLATE_DECL)
16196 if (TREE_DEPRECATED (decl)
16197 && deprecated_state != DEPRECATED_SUPPRESS)
16198 warn_deprecated_use (decl, NULL_TREE);
16200 else
16202 /* The standard does not explicitly indicate whether a name that
16203 names a set of overloaded declarations, some of which are
16204 templates, is a template-name. However, such a name should
16205 be a template-name; otherwise, there is no way to form a
16206 template-id for the overloaded templates. */
16207 bool found = false;
16209 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16210 !found && iter; ++iter)
16211 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16212 found = true;
16214 if (!found)
16216 /* The name does not name a template. */
16217 cp_parser_error (parser, "expected template-name");
16218 return error_mark_node;
16222 /* If DECL is dependent, and refers to a function, then just return
16223 its name; we will look it up again during template instantiation. */
16224 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16226 tree scope = ovl_scope (decl);
16227 if (TYPE_P (scope) && dependent_type_p (scope))
16228 return identifier;
16231 return decl;
16234 /* Parse a template-argument-list.
16236 template-argument-list:
16237 template-argument ... [opt]
16238 template-argument-list , template-argument ... [opt]
16240 Returns a TREE_VEC containing the arguments. */
16242 static tree
16243 cp_parser_template_argument_list (cp_parser* parser)
16245 tree fixed_args[10];
16246 unsigned n_args = 0;
16247 unsigned alloced = 10;
16248 tree *arg_ary = fixed_args;
16249 tree vec;
16250 bool saved_in_template_argument_list_p;
16251 bool saved_ice_p;
16252 bool saved_non_ice_p;
16254 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16255 parser->in_template_argument_list_p = true;
16256 /* Even if the template-id appears in an integral
16257 constant-expression, the contents of the argument list do
16258 not. */
16259 saved_ice_p = parser->integral_constant_expression_p;
16260 parser->integral_constant_expression_p = false;
16261 saved_non_ice_p = parser->non_integral_constant_expression_p;
16262 parser->non_integral_constant_expression_p = false;
16264 /* Parse the arguments. */
16267 tree argument;
16269 if (n_args)
16270 /* Consume the comma. */
16271 cp_lexer_consume_token (parser->lexer);
16273 /* Parse the template-argument. */
16274 argument = cp_parser_template_argument (parser);
16276 /* If the next token is an ellipsis, we're expanding a template
16277 argument pack. */
16278 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16280 if (argument == error_mark_node)
16282 cp_token *token = cp_lexer_peek_token (parser->lexer);
16283 error_at (token->location,
16284 "expected parameter pack before %<...%>");
16286 /* Consume the `...' token. */
16287 cp_lexer_consume_token (parser->lexer);
16289 /* Make the argument into a TYPE_PACK_EXPANSION or
16290 EXPR_PACK_EXPANSION. */
16291 argument = make_pack_expansion (argument);
16294 if (n_args == alloced)
16296 alloced *= 2;
16298 if (arg_ary == fixed_args)
16300 arg_ary = XNEWVEC (tree, alloced);
16301 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16303 else
16304 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16306 arg_ary[n_args++] = argument;
16308 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16310 vec = make_tree_vec (n_args);
16312 while (n_args--)
16313 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16315 if (arg_ary != fixed_args)
16316 free (arg_ary);
16317 parser->non_integral_constant_expression_p = saved_non_ice_p;
16318 parser->integral_constant_expression_p = saved_ice_p;
16319 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16320 if (CHECKING_P)
16321 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16322 return vec;
16325 /* Parse a template-argument.
16327 template-argument:
16328 assignment-expression
16329 type-id
16330 id-expression
16332 The representation is that of an assignment-expression, type-id, or
16333 id-expression -- except that the qualified id-expression is
16334 evaluated, so that the value returned is either a DECL or an
16335 OVERLOAD.
16337 Although the standard says "assignment-expression", it forbids
16338 throw-expressions or assignments in the template argument.
16339 Therefore, we use "conditional-expression" instead. */
16341 static tree
16342 cp_parser_template_argument (cp_parser* parser)
16344 tree argument;
16345 bool template_p;
16346 bool address_p;
16347 bool maybe_type_id = false;
16348 cp_token *token = NULL, *argument_start_token = NULL;
16349 location_t loc = 0;
16350 cp_id_kind idk;
16352 /* There's really no way to know what we're looking at, so we just
16353 try each alternative in order.
16355 [temp.arg]
16357 In a template-argument, an ambiguity between a type-id and an
16358 expression is resolved to a type-id, regardless of the form of
16359 the corresponding template-parameter.
16361 Therefore, we try a type-id first. */
16362 cp_parser_parse_tentatively (parser);
16363 argument = cp_parser_template_type_arg (parser);
16364 /* If there was no error parsing the type-id but the next token is a
16365 '>>', our behavior depends on which dialect of C++ we're
16366 parsing. In C++98, we probably found a typo for '> >'. But there
16367 are type-id which are also valid expressions. For instance:
16369 struct X { int operator >> (int); };
16370 template <int V> struct Foo {};
16371 Foo<X () >> 5> r;
16373 Here 'X()' is a valid type-id of a function type, but the user just
16374 wanted to write the expression "X() >> 5". Thus, we remember that we
16375 found a valid type-id, but we still try to parse the argument as an
16376 expression to see what happens.
16378 In C++0x, the '>>' will be considered two separate '>'
16379 tokens. */
16380 if (!cp_parser_error_occurred (parser)
16381 && cxx_dialect == cxx98
16382 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16384 maybe_type_id = true;
16385 cp_parser_abort_tentative_parse (parser);
16387 else
16389 /* If the next token isn't a `,' or a `>', then this argument wasn't
16390 really finished. This means that the argument is not a valid
16391 type-id. */
16392 if (!cp_parser_next_token_ends_template_argument_p (parser))
16393 cp_parser_error (parser, "expected template-argument");
16394 /* If that worked, we're done. */
16395 if (cp_parser_parse_definitely (parser))
16396 return argument;
16398 /* We're still not sure what the argument will be. */
16399 cp_parser_parse_tentatively (parser);
16400 /* Try a template. */
16401 argument_start_token = cp_lexer_peek_token (parser->lexer);
16402 argument = cp_parser_id_expression (parser,
16403 /*template_keyword_p=*/false,
16404 /*check_dependency_p=*/true,
16405 &template_p,
16406 /*declarator_p=*/false,
16407 /*optional_p=*/false);
16408 /* If the next token isn't a `,' or a `>', then this argument wasn't
16409 really finished. */
16410 if (!cp_parser_next_token_ends_template_argument_p (parser))
16411 cp_parser_error (parser, "expected template-argument");
16412 if (!cp_parser_error_occurred (parser))
16414 /* Figure out what is being referred to. If the id-expression
16415 was for a class template specialization, then we will have a
16416 TYPE_DECL at this point. There is no need to do name lookup
16417 at this point in that case. */
16418 if (TREE_CODE (argument) != TYPE_DECL)
16419 argument = cp_parser_lookup_name (parser, argument,
16420 none_type,
16421 /*is_template=*/template_p,
16422 /*is_namespace=*/false,
16423 /*check_dependency=*/true,
16424 /*ambiguous_decls=*/NULL,
16425 argument_start_token->location);
16426 /* Handle a constrained-type-specifier for a non-type template
16427 parameter. */
16428 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16429 argument = decl;
16430 else if (TREE_CODE (argument) != TEMPLATE_DECL
16431 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16432 cp_parser_error (parser, "expected template-name");
16434 if (cp_parser_parse_definitely (parser))
16436 if (TREE_DEPRECATED (argument))
16437 warn_deprecated_use (argument, NULL_TREE);
16438 return argument;
16440 /* It must be a non-type argument. In C++17 any constant-expression is
16441 allowed. */
16442 if (cxx_dialect > cxx14)
16443 goto general_expr;
16445 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16447 -- an integral constant-expression of integral or enumeration
16448 type; or
16450 -- the name of a non-type template-parameter; or
16452 -- the name of an object or function with external linkage...
16454 -- the address of an object or function with external linkage...
16456 -- a pointer to member... */
16457 /* Look for a non-type template parameter. */
16458 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16460 cp_parser_parse_tentatively (parser);
16461 argument = cp_parser_primary_expression (parser,
16462 /*address_p=*/false,
16463 /*cast_p=*/false,
16464 /*template_arg_p=*/true,
16465 &idk);
16466 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16467 || !cp_parser_next_token_ends_template_argument_p (parser))
16468 cp_parser_simulate_error (parser);
16469 if (cp_parser_parse_definitely (parser))
16470 return argument;
16473 /* If the next token is "&", the argument must be the address of an
16474 object or function with external linkage. */
16475 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16476 if (address_p)
16478 loc = cp_lexer_peek_token (parser->lexer)->location;
16479 cp_lexer_consume_token (parser->lexer);
16481 /* See if we might have an id-expression. */
16482 token = cp_lexer_peek_token (parser->lexer);
16483 if (token->type == CPP_NAME
16484 || token->keyword == RID_OPERATOR
16485 || token->type == CPP_SCOPE
16486 || token->type == CPP_TEMPLATE_ID
16487 || token->type == CPP_NESTED_NAME_SPECIFIER)
16489 cp_parser_parse_tentatively (parser);
16490 argument = cp_parser_primary_expression (parser,
16491 address_p,
16492 /*cast_p=*/false,
16493 /*template_arg_p=*/true,
16494 &idk);
16495 if (cp_parser_error_occurred (parser)
16496 || !cp_parser_next_token_ends_template_argument_p (parser))
16497 cp_parser_abort_tentative_parse (parser);
16498 else
16500 tree probe;
16502 if (INDIRECT_REF_P (argument))
16504 /* Strip the dereference temporarily. */
16505 gcc_assert (REFERENCE_REF_P (argument));
16506 argument = TREE_OPERAND (argument, 0);
16509 /* If we're in a template, we represent a qualified-id referring
16510 to a static data member as a SCOPE_REF even if the scope isn't
16511 dependent so that we can check access control later. */
16512 probe = argument;
16513 if (TREE_CODE (probe) == SCOPE_REF)
16514 probe = TREE_OPERAND (probe, 1);
16515 if (VAR_P (probe))
16517 /* A variable without external linkage might still be a
16518 valid constant-expression, so no error is issued here
16519 if the external-linkage check fails. */
16520 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16521 cp_parser_simulate_error (parser);
16523 else if (is_overloaded_fn (argument))
16524 /* All overloaded functions are allowed; if the external
16525 linkage test does not pass, an error will be issued
16526 later. */
16528 else if (address_p
16529 && (TREE_CODE (argument) == OFFSET_REF
16530 || TREE_CODE (argument) == SCOPE_REF))
16531 /* A pointer-to-member. */
16533 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16535 else
16536 cp_parser_simulate_error (parser);
16538 if (cp_parser_parse_definitely (parser))
16540 if (address_p)
16541 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16542 tf_warning_or_error);
16543 else
16544 argument = convert_from_reference (argument);
16545 return argument;
16549 /* If the argument started with "&", there are no other valid
16550 alternatives at this point. */
16551 if (address_p)
16553 cp_parser_error (parser, "invalid non-type template argument");
16554 return error_mark_node;
16557 general_expr:
16558 /* If the argument wasn't successfully parsed as a type-id followed
16559 by '>>', the argument can only be a constant expression now.
16560 Otherwise, we try parsing the constant-expression tentatively,
16561 because the argument could really be a type-id. */
16562 if (maybe_type_id)
16563 cp_parser_parse_tentatively (parser);
16565 if (cxx_dialect <= cxx14)
16566 argument = cp_parser_constant_expression (parser);
16567 else
16569 /* With C++17 generalized non-type template arguments we need to handle
16570 lvalue constant expressions, too. */
16571 argument = cp_parser_assignment_expression (parser);
16572 require_potential_constant_expression (argument);
16575 if (!maybe_type_id)
16576 return argument;
16577 if (!cp_parser_next_token_ends_template_argument_p (parser))
16578 cp_parser_error (parser, "expected template-argument");
16579 if (cp_parser_parse_definitely (parser))
16580 return argument;
16581 /* We did our best to parse the argument as a non type-id, but that
16582 was the only alternative that matched (albeit with a '>' after
16583 it). We can assume it's just a typo from the user, and a
16584 diagnostic will then be issued. */
16585 return cp_parser_template_type_arg (parser);
16588 /* Parse an explicit-instantiation.
16590 explicit-instantiation:
16591 template declaration
16593 Although the standard says `declaration', what it really means is:
16595 explicit-instantiation:
16596 template decl-specifier-seq [opt] declarator [opt] ;
16598 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16599 supposed to be allowed. A defect report has been filed about this
16600 issue.
16602 GNU Extension:
16604 explicit-instantiation:
16605 storage-class-specifier template
16606 decl-specifier-seq [opt] declarator [opt] ;
16607 function-specifier template
16608 decl-specifier-seq [opt] declarator [opt] ; */
16610 static void
16611 cp_parser_explicit_instantiation (cp_parser* parser)
16613 int declares_class_or_enum;
16614 cp_decl_specifier_seq decl_specifiers;
16615 tree extension_specifier = NULL_TREE;
16617 timevar_push (TV_TEMPLATE_INST);
16619 /* Look for an (optional) storage-class-specifier or
16620 function-specifier. */
16621 if (cp_parser_allow_gnu_extensions_p (parser))
16623 extension_specifier
16624 = cp_parser_storage_class_specifier_opt (parser);
16625 if (!extension_specifier)
16626 extension_specifier
16627 = cp_parser_function_specifier_opt (parser,
16628 /*decl_specs=*/NULL);
16631 /* Look for the `template' keyword. */
16632 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16633 /* Let the front end know that we are processing an explicit
16634 instantiation. */
16635 begin_explicit_instantiation ();
16636 /* [temp.explicit] says that we are supposed to ignore access
16637 control while processing explicit instantiation directives. */
16638 push_deferring_access_checks (dk_no_check);
16639 /* Parse a decl-specifier-seq. */
16640 cp_parser_decl_specifier_seq (parser,
16641 CP_PARSER_FLAGS_OPTIONAL,
16642 &decl_specifiers,
16643 &declares_class_or_enum);
16644 /* If there was exactly one decl-specifier, and it declared a class,
16645 and there's no declarator, then we have an explicit type
16646 instantiation. */
16647 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16649 tree type;
16651 type = check_tag_decl (&decl_specifiers,
16652 /*explicit_type_instantiation_p=*/true);
16653 /* Turn access control back on for names used during
16654 template instantiation. */
16655 pop_deferring_access_checks ();
16656 if (type)
16657 do_type_instantiation (type, extension_specifier,
16658 /*complain=*/tf_error);
16660 else
16662 cp_declarator *declarator;
16663 tree decl;
16665 /* Parse the declarator. */
16666 declarator
16667 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16668 /*ctor_dtor_or_conv_p=*/NULL,
16669 /*parenthesized_p=*/NULL,
16670 /*member_p=*/false,
16671 /*friend_p=*/false);
16672 if (declares_class_or_enum & 2)
16673 cp_parser_check_for_definition_in_return_type (declarator,
16674 decl_specifiers.type,
16675 decl_specifiers.locations[ds_type_spec]);
16676 if (declarator != cp_error_declarator)
16678 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16679 permerror (decl_specifiers.locations[ds_inline],
16680 "explicit instantiation shall not use"
16681 " %<inline%> specifier");
16682 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16683 permerror (decl_specifiers.locations[ds_constexpr],
16684 "explicit instantiation shall not use"
16685 " %<constexpr%> specifier");
16687 decl = grokdeclarator (declarator, &decl_specifiers,
16688 NORMAL, 0, &decl_specifiers.attributes);
16689 /* Turn access control back on for names used during
16690 template instantiation. */
16691 pop_deferring_access_checks ();
16692 /* Do the explicit instantiation. */
16693 do_decl_instantiation (decl, extension_specifier);
16695 else
16697 pop_deferring_access_checks ();
16698 /* Skip the body of the explicit instantiation. */
16699 cp_parser_skip_to_end_of_statement (parser);
16702 /* We're done with the instantiation. */
16703 end_explicit_instantiation ();
16705 cp_parser_consume_semicolon_at_end_of_statement (parser);
16707 timevar_pop (TV_TEMPLATE_INST);
16710 /* Parse an explicit-specialization.
16712 explicit-specialization:
16713 template < > declaration
16715 Although the standard says `declaration', what it really means is:
16717 explicit-specialization:
16718 template <> decl-specifier [opt] init-declarator [opt] ;
16719 template <> function-definition
16720 template <> explicit-specialization
16721 template <> template-declaration */
16723 static void
16724 cp_parser_explicit_specialization (cp_parser* parser)
16726 bool need_lang_pop;
16727 cp_token *token = cp_lexer_peek_token (parser->lexer);
16729 /* Look for the `template' keyword. */
16730 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16731 /* Look for the `<'. */
16732 cp_parser_require (parser, CPP_LESS, RT_LESS);
16733 /* Look for the `>'. */
16734 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16735 /* We have processed another parameter list. */
16736 ++parser->num_template_parameter_lists;
16737 /* [temp]
16739 A template ... explicit specialization ... shall not have C
16740 linkage. */
16741 if (current_lang_name == lang_name_c)
16743 error_at (token->location, "template specialization with C linkage");
16744 maybe_show_extern_c_location ();
16745 /* Give it C++ linkage to avoid confusing other parts of the
16746 front end. */
16747 push_lang_context (lang_name_cplusplus);
16748 need_lang_pop = true;
16750 else
16751 need_lang_pop = false;
16752 /* Let the front end know that we are beginning a specialization. */
16753 if (!begin_specialization ())
16755 end_specialization ();
16756 return;
16759 /* If the next keyword is `template', we need to figure out whether
16760 or not we're looking a template-declaration. */
16761 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16763 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16764 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16765 cp_parser_template_declaration_after_export (parser,
16766 /*member_p=*/false);
16767 else
16768 cp_parser_explicit_specialization (parser);
16770 else
16771 /* Parse the dependent declaration. */
16772 cp_parser_single_declaration (parser,
16773 /*checks=*/NULL,
16774 /*member_p=*/false,
16775 /*explicit_specialization_p=*/true,
16776 /*friend_p=*/NULL);
16777 /* We're done with the specialization. */
16778 end_specialization ();
16779 /* For the erroneous case of a template with C linkage, we pushed an
16780 implicit C++ linkage scope; exit that scope now. */
16781 if (need_lang_pop)
16782 pop_lang_context ();
16783 /* We're done with this parameter list. */
16784 --parser->num_template_parameter_lists;
16787 /* Parse a type-specifier.
16789 type-specifier:
16790 simple-type-specifier
16791 class-specifier
16792 enum-specifier
16793 elaborated-type-specifier
16794 cv-qualifier
16796 GNU Extension:
16798 type-specifier:
16799 __complex__
16801 Returns a representation of the type-specifier. For a
16802 class-specifier, enum-specifier, or elaborated-type-specifier, a
16803 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16805 The parser flags FLAGS is used to control type-specifier parsing.
16807 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16808 in a decl-specifier-seq.
16810 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16811 class-specifier, enum-specifier, or elaborated-type-specifier, then
16812 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16813 if a type is declared; 2 if it is defined. Otherwise, it is set to
16814 zero.
16816 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16817 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16818 is set to FALSE. */
16820 static tree
16821 cp_parser_type_specifier (cp_parser* parser,
16822 cp_parser_flags flags,
16823 cp_decl_specifier_seq *decl_specs,
16824 bool is_declaration,
16825 int* declares_class_or_enum,
16826 bool* is_cv_qualifier)
16828 tree type_spec = NULL_TREE;
16829 cp_token *token;
16830 enum rid keyword;
16831 cp_decl_spec ds = ds_last;
16833 /* Assume this type-specifier does not declare a new type. */
16834 if (declares_class_or_enum)
16835 *declares_class_or_enum = 0;
16836 /* And that it does not specify a cv-qualifier. */
16837 if (is_cv_qualifier)
16838 *is_cv_qualifier = false;
16839 /* Peek at the next token. */
16840 token = cp_lexer_peek_token (parser->lexer);
16842 /* If we're looking at a keyword, we can use that to guide the
16843 production we choose. */
16844 keyword = token->keyword;
16845 switch (keyword)
16847 case RID_ENUM:
16848 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16849 goto elaborated_type_specifier;
16851 /* Look for the enum-specifier. */
16852 type_spec = cp_parser_enum_specifier (parser);
16853 /* If that worked, we're done. */
16854 if (type_spec)
16856 if (declares_class_or_enum)
16857 *declares_class_or_enum = 2;
16858 if (decl_specs)
16859 cp_parser_set_decl_spec_type (decl_specs,
16860 type_spec,
16861 token,
16862 /*type_definition_p=*/true);
16863 return type_spec;
16865 else
16866 goto elaborated_type_specifier;
16868 /* Any of these indicate either a class-specifier, or an
16869 elaborated-type-specifier. */
16870 case RID_CLASS:
16871 case RID_STRUCT:
16872 case RID_UNION:
16873 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16874 goto elaborated_type_specifier;
16876 /* Parse tentatively so that we can back up if we don't find a
16877 class-specifier. */
16878 cp_parser_parse_tentatively (parser);
16879 /* Look for the class-specifier. */
16880 type_spec = cp_parser_class_specifier (parser);
16881 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16882 /* If that worked, we're done. */
16883 if (cp_parser_parse_definitely (parser))
16885 if (declares_class_or_enum)
16886 *declares_class_or_enum = 2;
16887 if (decl_specs)
16888 cp_parser_set_decl_spec_type (decl_specs,
16889 type_spec,
16890 token,
16891 /*type_definition_p=*/true);
16892 return type_spec;
16895 /* Fall through. */
16896 elaborated_type_specifier:
16897 /* We're declaring (not defining) a class or enum. */
16898 if (declares_class_or_enum)
16899 *declares_class_or_enum = 1;
16901 /* Fall through. */
16902 case RID_TYPENAME:
16903 /* Look for an elaborated-type-specifier. */
16904 type_spec
16905 = (cp_parser_elaborated_type_specifier
16906 (parser,
16907 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16908 is_declaration));
16909 if (decl_specs)
16910 cp_parser_set_decl_spec_type (decl_specs,
16911 type_spec,
16912 token,
16913 /*type_definition_p=*/false);
16914 return type_spec;
16916 case RID_CONST:
16917 ds = ds_const;
16918 if (is_cv_qualifier)
16919 *is_cv_qualifier = true;
16920 break;
16922 case RID_VOLATILE:
16923 ds = ds_volatile;
16924 if (is_cv_qualifier)
16925 *is_cv_qualifier = true;
16926 break;
16928 case RID_RESTRICT:
16929 ds = ds_restrict;
16930 if (is_cv_qualifier)
16931 *is_cv_qualifier = true;
16932 break;
16934 case RID_COMPLEX:
16935 /* The `__complex__' keyword is a GNU extension. */
16936 ds = ds_complex;
16937 break;
16939 default:
16940 break;
16943 /* Handle simple keywords. */
16944 if (ds != ds_last)
16946 if (decl_specs)
16948 set_and_check_decl_spec_loc (decl_specs, ds, token);
16949 decl_specs->any_specifiers_p = true;
16951 return cp_lexer_consume_token (parser->lexer)->u.value;
16954 /* If we do not already have a type-specifier, assume we are looking
16955 at a simple-type-specifier. */
16956 type_spec = cp_parser_simple_type_specifier (parser,
16957 decl_specs,
16958 flags);
16960 /* If we didn't find a type-specifier, and a type-specifier was not
16961 optional in this context, issue an error message. */
16962 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16964 cp_parser_error (parser, "expected type specifier");
16965 return error_mark_node;
16968 return type_spec;
16971 /* Parse a simple-type-specifier.
16973 simple-type-specifier:
16974 :: [opt] nested-name-specifier [opt] type-name
16975 :: [opt] nested-name-specifier template template-id
16976 char
16977 wchar_t
16978 bool
16979 short
16981 long
16982 signed
16983 unsigned
16984 float
16985 double
16986 void
16988 C++11 Extension:
16990 simple-type-specifier:
16991 auto
16992 decltype ( expression )
16993 char16_t
16994 char32_t
16995 __underlying_type ( type-id )
16997 C++17 extension:
16999 nested-name-specifier(opt) template-name
17001 GNU Extension:
17003 simple-type-specifier:
17004 __int128
17005 __typeof__ unary-expression
17006 __typeof__ ( type-id )
17007 __typeof__ ( type-id ) { initializer-list , [opt] }
17009 Concepts Extension:
17011 simple-type-specifier:
17012 constrained-type-specifier
17014 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17015 appropriately updated. */
17017 static tree
17018 cp_parser_simple_type_specifier (cp_parser* parser,
17019 cp_decl_specifier_seq *decl_specs,
17020 cp_parser_flags flags)
17022 tree type = NULL_TREE;
17023 cp_token *token;
17024 int idx;
17026 /* Peek at the next token. */
17027 token = cp_lexer_peek_token (parser->lexer);
17029 /* If we're looking at a keyword, things are easy. */
17030 switch (token->keyword)
17032 case RID_CHAR:
17033 if (decl_specs)
17034 decl_specs->explicit_char_p = true;
17035 type = char_type_node;
17036 break;
17037 case RID_CHAR16:
17038 type = char16_type_node;
17039 break;
17040 case RID_CHAR32:
17041 type = char32_type_node;
17042 break;
17043 case RID_WCHAR:
17044 type = wchar_type_node;
17045 break;
17046 case RID_BOOL:
17047 type = boolean_type_node;
17048 break;
17049 case RID_SHORT:
17050 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17051 type = short_integer_type_node;
17052 break;
17053 case RID_INT:
17054 if (decl_specs)
17055 decl_specs->explicit_int_p = true;
17056 type = integer_type_node;
17057 break;
17058 case RID_INT_N_0:
17059 case RID_INT_N_1:
17060 case RID_INT_N_2:
17061 case RID_INT_N_3:
17062 idx = token->keyword - RID_INT_N_0;
17063 if (! int_n_enabled_p [idx])
17064 break;
17065 if (decl_specs)
17067 decl_specs->explicit_intN_p = true;
17068 decl_specs->int_n_idx = idx;
17070 type = int_n_trees [idx].signed_type;
17071 break;
17072 case RID_LONG:
17073 if (decl_specs)
17074 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17075 type = long_integer_type_node;
17076 break;
17077 case RID_SIGNED:
17078 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17079 type = integer_type_node;
17080 break;
17081 case RID_UNSIGNED:
17082 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17083 type = unsigned_type_node;
17084 break;
17085 case RID_FLOAT:
17086 type = float_type_node;
17087 break;
17088 case RID_DOUBLE:
17089 type = double_type_node;
17090 break;
17091 case RID_VOID:
17092 type = void_type_node;
17093 break;
17095 case RID_AUTO:
17096 maybe_warn_cpp0x (CPP0X_AUTO);
17097 if (parser->auto_is_implicit_function_template_parm_p)
17099 /* The 'auto' might be the placeholder return type for a function decl
17100 with trailing return type. */
17101 bool have_trailing_return_fn_decl = false;
17103 cp_parser_parse_tentatively (parser);
17104 cp_lexer_consume_token (parser->lexer);
17105 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17106 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17107 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17108 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17110 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17112 cp_lexer_consume_token (parser->lexer);
17113 cp_parser_skip_to_closing_parenthesis (parser,
17114 /*recovering*/false,
17115 /*or_comma*/false,
17116 /*consume_paren*/true);
17117 continue;
17120 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17122 have_trailing_return_fn_decl = true;
17123 break;
17126 cp_lexer_consume_token (parser->lexer);
17128 cp_parser_abort_tentative_parse (parser);
17130 if (have_trailing_return_fn_decl)
17132 type = make_auto ();
17133 break;
17136 if (cxx_dialect >= cxx14)
17138 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17139 type = TREE_TYPE (type);
17141 else
17142 type = error_mark_node;
17144 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17146 if (cxx_dialect < cxx14)
17147 error_at (token->location,
17148 "use of %<auto%> in lambda parameter declaration "
17149 "only available with "
17150 "-std=c++14 or -std=gnu++14");
17152 else if (cxx_dialect < cxx14)
17153 error_at (token->location,
17154 "use of %<auto%> in parameter declaration "
17155 "only available with "
17156 "-std=c++14 or -std=gnu++14");
17157 else if (!flag_concepts)
17158 pedwarn (token->location, 0,
17159 "use of %<auto%> in parameter declaration "
17160 "only available with -fconcepts");
17162 else
17163 type = make_auto ();
17164 break;
17166 case RID_DECLTYPE:
17167 /* Since DR 743, decltype can either be a simple-type-specifier by
17168 itself or begin a nested-name-specifier. Parsing it will replace
17169 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17170 handling below decide what to do. */
17171 cp_parser_decltype (parser);
17172 cp_lexer_set_token_position (parser->lexer, token);
17173 break;
17175 case RID_TYPEOF:
17176 /* Consume the `typeof' token. */
17177 cp_lexer_consume_token (parser->lexer);
17178 /* Parse the operand to `typeof'. */
17179 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17180 /* If it is not already a TYPE, take its type. */
17181 if (!TYPE_P (type))
17182 type = finish_typeof (type);
17184 if (decl_specs)
17185 cp_parser_set_decl_spec_type (decl_specs, type,
17186 token,
17187 /*type_definition_p=*/false);
17189 return type;
17191 case RID_UNDERLYING_TYPE:
17192 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17193 if (decl_specs)
17194 cp_parser_set_decl_spec_type (decl_specs, type,
17195 token,
17196 /*type_definition_p=*/false);
17198 return type;
17200 case RID_BASES:
17201 case RID_DIRECT_BASES:
17202 type = cp_parser_trait_expr (parser, token->keyword);
17203 if (decl_specs)
17204 cp_parser_set_decl_spec_type (decl_specs, type,
17205 token,
17206 /*type_definition_p=*/false);
17207 return type;
17208 default:
17209 break;
17212 /* If token is an already-parsed decltype not followed by ::,
17213 it's a simple-type-specifier. */
17214 if (token->type == CPP_DECLTYPE
17215 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17217 type = saved_checks_value (token->u.tree_check_value);
17218 if (decl_specs)
17220 cp_parser_set_decl_spec_type (decl_specs, type,
17221 token,
17222 /*type_definition_p=*/false);
17223 /* Remember that we are handling a decltype in order to
17224 implement the resolution of DR 1510 when the argument
17225 isn't instantiation dependent. */
17226 decl_specs->decltype_p = true;
17228 cp_lexer_consume_token (parser->lexer);
17229 return type;
17232 /* If the type-specifier was for a built-in type, we're done. */
17233 if (type)
17235 /* Record the type. */
17236 if (decl_specs
17237 && (token->keyword != RID_SIGNED
17238 && token->keyword != RID_UNSIGNED
17239 && token->keyword != RID_SHORT
17240 && token->keyword != RID_LONG))
17241 cp_parser_set_decl_spec_type (decl_specs,
17242 type,
17243 token,
17244 /*type_definition_p=*/false);
17245 if (decl_specs)
17246 decl_specs->any_specifiers_p = true;
17248 /* Consume the token. */
17249 cp_lexer_consume_token (parser->lexer);
17251 if (type == error_mark_node)
17252 return error_mark_node;
17254 /* There is no valid C++ program where a non-template type is
17255 followed by a "<". That usually indicates that the user thought
17256 that the type was a template. */
17257 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17258 token->location);
17260 return TYPE_NAME (type);
17263 /* The type-specifier must be a user-defined type. */
17264 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17266 bool qualified_p;
17267 bool global_p;
17269 /* Don't gobble tokens or issue error messages if this is an
17270 optional type-specifier. */
17271 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17272 cp_parser_parse_tentatively (parser);
17274 token = cp_lexer_peek_token (parser->lexer);
17276 /* Look for the optional `::' operator. */
17277 global_p
17278 = (cp_parser_global_scope_opt (parser,
17279 /*current_scope_valid_p=*/false)
17280 != NULL_TREE);
17281 /* Look for the nested-name specifier. */
17282 qualified_p
17283 = (cp_parser_nested_name_specifier_opt (parser,
17284 /*typename_keyword_p=*/false,
17285 /*check_dependency_p=*/true,
17286 /*type_p=*/false,
17287 /*is_declaration=*/false)
17288 != NULL_TREE);
17289 /* If we have seen a nested-name-specifier, and the next token
17290 is `template', then we are using the template-id production. */
17291 if (parser->scope
17292 && cp_parser_optional_template_keyword (parser))
17294 /* Look for the template-id. */
17295 type = cp_parser_template_id (parser,
17296 /*template_keyword_p=*/true,
17297 /*check_dependency_p=*/true,
17298 none_type,
17299 /*is_declaration=*/false);
17300 /* If the template-id did not name a type, we are out of
17301 luck. */
17302 if (TREE_CODE (type) != TYPE_DECL)
17304 cp_parser_error (parser, "expected template-id for type");
17305 type = NULL_TREE;
17308 /* Otherwise, look for a type-name. */
17309 else
17310 type = cp_parser_type_name (parser);
17311 /* Keep track of all name-lookups performed in class scopes. */
17312 if (type
17313 && !global_p
17314 && !qualified_p
17315 && TREE_CODE (type) == TYPE_DECL
17316 && identifier_p (DECL_NAME (type)))
17317 maybe_note_name_used_in_class (DECL_NAME (type), type);
17318 /* If it didn't work out, we don't have a TYPE. */
17319 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17320 && !cp_parser_parse_definitely (parser))
17321 type = NULL_TREE;
17322 if (!type && cxx_dialect >= cxx17)
17324 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17325 cp_parser_parse_tentatively (parser);
17327 cp_parser_global_scope_opt (parser,
17328 /*current_scope_valid_p=*/false);
17329 cp_parser_nested_name_specifier_opt (parser,
17330 /*typename_keyword_p=*/false,
17331 /*check_dependency_p=*/true,
17332 /*type_p=*/false,
17333 /*is_declaration=*/false);
17334 tree name = cp_parser_identifier (parser);
17335 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17336 && parser->scope != error_mark_node)
17338 tree tmpl = cp_parser_lookup_name (parser, name,
17339 none_type,
17340 /*is_template=*/false,
17341 /*is_namespace=*/false,
17342 /*check_dependency=*/true,
17343 /*ambiguous_decls=*/NULL,
17344 token->location);
17345 if (tmpl && tmpl != error_mark_node
17346 && (DECL_CLASS_TEMPLATE_P (tmpl)
17347 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17348 type = make_template_placeholder (tmpl);
17349 else
17351 type = error_mark_node;
17352 if (!cp_parser_simulate_error (parser))
17353 cp_parser_name_lookup_error (parser, name, tmpl,
17354 NLE_TYPE, token->location);
17357 else
17358 type = error_mark_node;
17360 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17361 && !cp_parser_parse_definitely (parser))
17362 type = NULL_TREE;
17364 if (type && decl_specs)
17365 cp_parser_set_decl_spec_type (decl_specs, type,
17366 token,
17367 /*type_definition_p=*/false);
17370 /* If we didn't get a type-name, issue an error message. */
17371 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17373 cp_parser_error (parser, "expected type-name");
17374 return error_mark_node;
17377 if (type && type != error_mark_node)
17379 /* See if TYPE is an Objective-C type, and if so, parse and
17380 accept any protocol references following it. Do this before
17381 the cp_parser_check_for_invalid_template_id() call, because
17382 Objective-C types can be followed by '<...>' which would
17383 enclose protocol names rather than template arguments, and so
17384 everything is fine. */
17385 if (c_dialect_objc () && !parser->scope
17386 && (objc_is_id (type) || objc_is_class_name (type)))
17388 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17389 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17391 /* Clobber the "unqualified" type previously entered into
17392 DECL_SPECS with the new, improved protocol-qualified version. */
17393 if (decl_specs)
17394 decl_specs->type = qual_type;
17396 return qual_type;
17399 /* There is no valid C++ program where a non-template type is
17400 followed by a "<". That usually indicates that the user
17401 thought that the type was a template. */
17402 cp_parser_check_for_invalid_template_id (parser, type,
17403 none_type,
17404 token->location);
17407 return type;
17410 /* Parse a type-name.
17412 type-name:
17413 class-name
17414 enum-name
17415 typedef-name
17416 simple-template-id [in c++0x]
17418 enum-name:
17419 identifier
17421 typedef-name:
17422 identifier
17424 Concepts:
17426 type-name:
17427 concept-name
17428 partial-concept-id
17430 concept-name:
17431 identifier
17433 Returns a TYPE_DECL for the type. */
17435 static tree
17436 cp_parser_type_name (cp_parser* parser)
17438 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17441 /* See above. */
17442 static tree
17443 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17445 tree type_decl;
17447 /* We can't know yet whether it is a class-name or not. */
17448 cp_parser_parse_tentatively (parser);
17449 /* Try a class-name. */
17450 type_decl = cp_parser_class_name (parser,
17451 typename_keyword_p,
17452 /*template_keyword_p=*/false,
17453 none_type,
17454 /*check_dependency_p=*/true,
17455 /*class_head_p=*/false,
17456 /*is_declaration=*/false);
17457 /* If it's not a class-name, keep looking. */
17458 if (!cp_parser_parse_definitely (parser))
17460 if (cxx_dialect < cxx11)
17461 /* It must be a typedef-name or an enum-name. */
17462 return cp_parser_nonclass_name (parser);
17464 cp_parser_parse_tentatively (parser);
17465 /* It is either a simple-template-id representing an
17466 instantiation of an alias template... */
17467 type_decl = cp_parser_template_id (parser,
17468 /*template_keyword_p=*/false,
17469 /*check_dependency_p=*/true,
17470 none_type,
17471 /*is_declaration=*/false);
17472 /* Note that this must be an instantiation of an alias template
17473 because [temp.names]/6 says:
17475 A template-id that names an alias template specialization
17476 is a type-name.
17478 Whereas [temp.names]/7 says:
17480 A simple-template-id that names a class template
17481 specialization is a class-name.
17483 With concepts, this could also be a partial-concept-id that
17484 declares a non-type template parameter. */
17485 if (type_decl != NULL_TREE
17486 && TREE_CODE (type_decl) == TYPE_DECL
17487 && TYPE_DECL_ALIAS_P (type_decl))
17488 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17489 else if (is_constrained_parameter (type_decl))
17490 /* Don't do anything. */ ;
17491 else
17492 cp_parser_simulate_error (parser);
17494 if (!cp_parser_parse_definitely (parser))
17495 /* ... Or a typedef-name or an enum-name. */
17496 return cp_parser_nonclass_name (parser);
17499 return type_decl;
17502 /* Check if DECL and ARGS can form a constrained-type-specifier.
17503 If ARGS is non-null, we try to form a concept check of the
17504 form DECL<?, ARGS> where ? is a wildcard that matches any
17505 kind of template argument. If ARGS is NULL, then we try to
17506 form a concept check of the form DECL<?>. */
17508 static tree
17509 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17510 tree decl, tree args)
17512 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17514 /* If we a constrained-type-specifier cannot be deduced. */
17515 if (parser->prevent_constrained_type_specifiers)
17516 return NULL_TREE;
17518 /* A constrained type specifier can only be found in an
17519 overload set or as a reference to a template declaration.
17521 FIXME: This might be masking a bug. It's possible that
17522 that the deduction below is causing template specializations
17523 to be formed with the wildcard as an argument. */
17524 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17525 return NULL_TREE;
17527 /* Try to build a call expression that evaluates the
17528 concept. This can fail if the overload set refers
17529 only to non-templates. */
17530 tree placeholder = build_nt (WILDCARD_DECL);
17531 tree check = build_concept_check (decl, placeholder, args);
17532 if (check == error_mark_node)
17533 return NULL_TREE;
17535 /* Deduce the checked constraint and the prototype parameter.
17537 FIXME: In certain cases, failure to deduce should be a
17538 diagnosable error. */
17539 tree conc;
17540 tree proto;
17541 if (!deduce_constrained_parameter (check, conc, proto))
17542 return NULL_TREE;
17544 /* In template parameter scope, this results in a constrained
17545 parameter. Return a descriptor of that parm. */
17546 if (processing_template_parmlist)
17547 return build_constrained_parameter (conc, proto, args);
17549 /* In a parameter-declaration-clause, constrained-type
17550 specifiers result in invented template parameters. */
17551 if (parser->auto_is_implicit_function_template_parm_p)
17553 tree x = build_constrained_parameter (conc, proto, args);
17554 return synthesize_implicit_template_parm (parser, x);
17556 else
17558 /* Otherwise, we're in a context where the constrained
17559 type name is deduced and the constraint applies
17560 after deduction. */
17561 return make_constrained_auto (conc, args);
17564 return NULL_TREE;
17567 /* If DECL refers to a concept, return a TYPE_DECL representing
17568 the result of using the constrained type specifier in the
17569 current context. DECL refers to a concept if
17571 - it is an overload set containing a function concept taking a single
17572 type argument, or
17574 - it is a variable concept taking a single type argument. */
17576 static tree
17577 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17579 if (flag_concepts
17580 && (TREE_CODE (decl) == OVERLOAD
17581 || BASELINK_P (decl)
17582 || variable_concept_p (decl)))
17583 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17584 else
17585 return NULL_TREE;
17588 /* Check if DECL and ARGS form a partial-concept-id. If so,
17589 assign ID to the resulting constrained placeholder.
17591 Returns true if the partial-concept-id designates a placeholder
17592 and false otherwise. Note that *id is set to NULL_TREE in
17593 this case. */
17595 static tree
17596 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17598 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17601 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17602 or a concept-name.
17604 enum-name:
17605 identifier
17607 typedef-name:
17608 identifier
17610 concept-name:
17611 identifier
17613 Returns a TYPE_DECL for the type. */
17615 static tree
17616 cp_parser_nonclass_name (cp_parser* parser)
17618 tree type_decl;
17619 tree identifier;
17621 cp_token *token = cp_lexer_peek_token (parser->lexer);
17622 identifier = cp_parser_identifier (parser);
17623 if (identifier == error_mark_node)
17624 return error_mark_node;
17626 /* Look up the type-name. */
17627 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17629 type_decl = strip_using_decl (type_decl);
17631 /* If we found an overload set, then it may refer to a concept-name. */
17632 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17633 type_decl = decl;
17635 if (TREE_CODE (type_decl) != TYPE_DECL
17636 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17638 /* See if this is an Objective-C type. */
17639 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17640 tree type = objc_get_protocol_qualified_type (identifier, protos);
17641 if (type)
17642 type_decl = TYPE_NAME (type);
17645 /* Issue an error if we did not find a type-name. */
17646 if (TREE_CODE (type_decl) != TYPE_DECL
17647 /* In Objective-C, we have the complication that class names are
17648 normally type names and start declarations (eg, the
17649 "NSObject" in "NSObject *object;"), but can be used in an
17650 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17651 is an expression. So, a classname followed by a dot is not a
17652 valid type-name. */
17653 || (objc_is_class_name (TREE_TYPE (type_decl))
17654 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17656 if (!cp_parser_simulate_error (parser))
17657 cp_parser_name_lookup_error (parser, identifier, type_decl,
17658 NLE_TYPE, token->location);
17659 return error_mark_node;
17661 /* Remember that the name was used in the definition of the
17662 current class so that we can check later to see if the
17663 meaning would have been different after the class was
17664 entirely defined. */
17665 else if (type_decl != error_mark_node
17666 && !parser->scope)
17667 maybe_note_name_used_in_class (identifier, type_decl);
17669 return type_decl;
17672 /* Parse an elaborated-type-specifier. Note that the grammar given
17673 here incorporates the resolution to DR68.
17675 elaborated-type-specifier:
17676 class-key :: [opt] nested-name-specifier [opt] identifier
17677 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17678 enum-key :: [opt] nested-name-specifier [opt] identifier
17679 typename :: [opt] nested-name-specifier identifier
17680 typename :: [opt] nested-name-specifier template [opt]
17681 template-id
17683 GNU extension:
17685 elaborated-type-specifier:
17686 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17687 class-key attributes :: [opt] nested-name-specifier [opt]
17688 template [opt] template-id
17689 enum attributes :: [opt] nested-name-specifier [opt] identifier
17691 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17692 declared `friend'. If IS_DECLARATION is TRUE, then this
17693 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17694 something is being declared.
17696 Returns the TYPE specified. */
17698 static tree
17699 cp_parser_elaborated_type_specifier (cp_parser* parser,
17700 bool is_friend,
17701 bool is_declaration)
17703 enum tag_types tag_type;
17704 tree identifier;
17705 tree type = NULL_TREE;
17706 tree attributes = NULL_TREE;
17707 tree globalscope;
17708 cp_token *token = NULL;
17710 /* See if we're looking at the `enum' keyword. */
17711 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17713 /* Consume the `enum' token. */
17714 cp_lexer_consume_token (parser->lexer);
17715 /* Remember that it's an enumeration type. */
17716 tag_type = enum_type;
17717 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17718 enums) is used here. */
17719 cp_token *token = cp_lexer_peek_token (parser->lexer);
17720 if (cp_parser_is_keyword (token, RID_CLASS)
17721 || cp_parser_is_keyword (token, RID_STRUCT))
17723 gcc_rich_location richloc (token->location);
17724 richloc.add_range (input_location, false);
17725 richloc.add_fixit_remove ();
17726 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17727 "a scoped enum must not use the %qD keyword",
17728 token->u.value);
17729 /* Consume the `struct' or `class' and parse it anyway. */
17730 cp_lexer_consume_token (parser->lexer);
17732 /* Parse the attributes. */
17733 attributes = cp_parser_attributes_opt (parser);
17735 /* Or, it might be `typename'. */
17736 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17737 RID_TYPENAME))
17739 /* Consume the `typename' token. */
17740 cp_lexer_consume_token (parser->lexer);
17741 /* Remember that it's a `typename' type. */
17742 tag_type = typename_type;
17744 /* Otherwise it must be a class-key. */
17745 else
17747 tag_type = cp_parser_class_key (parser);
17748 if (tag_type == none_type)
17749 return error_mark_node;
17750 /* Parse the attributes. */
17751 attributes = cp_parser_attributes_opt (parser);
17754 /* Look for the `::' operator. */
17755 globalscope = cp_parser_global_scope_opt (parser,
17756 /*current_scope_valid_p=*/false);
17757 /* Look for the nested-name-specifier. */
17758 tree nested_name_specifier;
17759 if (tag_type == typename_type && !globalscope)
17761 nested_name_specifier
17762 = cp_parser_nested_name_specifier (parser,
17763 /*typename_keyword_p=*/true,
17764 /*check_dependency_p=*/true,
17765 /*type_p=*/true,
17766 is_declaration);
17767 if (!nested_name_specifier)
17768 return error_mark_node;
17770 else
17771 /* Even though `typename' is not present, the proposed resolution
17772 to Core Issue 180 says that in `class A<T>::B', `B' should be
17773 considered a type-name, even if `A<T>' is dependent. */
17774 nested_name_specifier
17775 = cp_parser_nested_name_specifier_opt (parser,
17776 /*typename_keyword_p=*/true,
17777 /*check_dependency_p=*/true,
17778 /*type_p=*/true,
17779 is_declaration);
17780 /* For everything but enumeration types, consider a template-id.
17781 For an enumeration type, consider only a plain identifier. */
17782 if (tag_type != enum_type)
17784 bool template_p = false;
17785 tree decl;
17787 /* Allow the `template' keyword. */
17788 template_p = cp_parser_optional_template_keyword (parser);
17789 /* If we didn't see `template', we don't know if there's a
17790 template-id or not. */
17791 if (!template_p)
17792 cp_parser_parse_tentatively (parser);
17793 /* Parse the template-id. */
17794 token = cp_lexer_peek_token (parser->lexer);
17795 decl = cp_parser_template_id (parser, template_p,
17796 /*check_dependency_p=*/true,
17797 tag_type,
17798 is_declaration);
17799 /* If we didn't find a template-id, look for an ordinary
17800 identifier. */
17801 if (!template_p && !cp_parser_parse_definitely (parser))
17803 /* We can get here when cp_parser_template_id, called by
17804 cp_parser_class_name with tag_type == none_type, succeeds
17805 and caches a BASELINK. Then, when called again here,
17806 instead of failing and returning an error_mark_node
17807 returns it (see template/typename17.C in C++11).
17808 ??? Could we diagnose this earlier? */
17809 else if (tag_type == typename_type && BASELINK_P (decl))
17811 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17812 type = error_mark_node;
17814 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17815 in effect, then we must assume that, upon instantiation, the
17816 template will correspond to a class. */
17817 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17818 && tag_type == typename_type)
17819 type = make_typename_type (parser->scope, decl,
17820 typename_type,
17821 /*complain=*/tf_error);
17822 /* If the `typename' keyword is in effect and DECL is not a type
17823 decl, then type is non existent. */
17824 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17826 else if (TREE_CODE (decl) == TYPE_DECL)
17828 type = check_elaborated_type_specifier (tag_type, decl,
17829 /*allow_template_p=*/true);
17831 /* If the next token is a semicolon, this must be a specialization,
17832 instantiation, or friend declaration. Check the scope while we
17833 still know whether or not we had a nested-name-specifier. */
17834 if (type != error_mark_node
17835 && !nested_name_specifier && !is_friend
17836 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17837 check_unqualified_spec_or_inst (type, token->location);
17839 else if (decl == error_mark_node)
17840 type = error_mark_node;
17843 if (!type)
17845 token = cp_lexer_peek_token (parser->lexer);
17846 identifier = cp_parser_identifier (parser);
17848 if (identifier == error_mark_node)
17850 parser->scope = NULL_TREE;
17851 return error_mark_node;
17854 /* For a `typename', we needn't call xref_tag. */
17855 if (tag_type == typename_type
17856 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17857 return cp_parser_make_typename_type (parser, identifier,
17858 token->location);
17860 /* Template parameter lists apply only if we are not within a
17861 function parameter list. */
17862 bool template_parm_lists_apply
17863 = parser->num_template_parameter_lists;
17864 if (template_parm_lists_apply)
17865 for (cp_binding_level *s = current_binding_level;
17866 s && s->kind != sk_template_parms;
17867 s = s->level_chain)
17868 if (s->kind == sk_function_parms)
17869 template_parm_lists_apply = false;
17871 /* Look up a qualified name in the usual way. */
17872 if (parser->scope)
17874 tree decl;
17875 tree ambiguous_decls;
17877 decl = cp_parser_lookup_name (parser, identifier,
17878 tag_type,
17879 /*is_template=*/false,
17880 /*is_namespace=*/false,
17881 /*check_dependency=*/true,
17882 &ambiguous_decls,
17883 token->location);
17885 /* If the lookup was ambiguous, an error will already have been
17886 issued. */
17887 if (ambiguous_decls)
17888 return error_mark_node;
17890 /* If we are parsing friend declaration, DECL may be a
17891 TEMPLATE_DECL tree node here. However, we need to check
17892 whether this TEMPLATE_DECL results in valid code. Consider
17893 the following example:
17895 namespace N {
17896 template <class T> class C {};
17898 class X {
17899 template <class T> friend class N::C; // #1, valid code
17901 template <class T> class Y {
17902 friend class N::C; // #2, invalid code
17905 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17906 name lookup of `N::C'. We see that friend declaration must
17907 be template for the code to be valid. Note that
17908 processing_template_decl does not work here since it is
17909 always 1 for the above two cases. */
17911 decl = (cp_parser_maybe_treat_template_as_class
17912 (decl, /*tag_name_p=*/is_friend
17913 && template_parm_lists_apply));
17915 if (TREE_CODE (decl) != TYPE_DECL)
17917 cp_parser_diagnose_invalid_type_name (parser,
17918 identifier,
17919 token->location);
17920 return error_mark_node;
17923 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17925 bool allow_template = (template_parm_lists_apply
17926 || DECL_SELF_REFERENCE_P (decl));
17927 type = check_elaborated_type_specifier (tag_type, decl,
17928 allow_template);
17930 if (type == error_mark_node)
17931 return error_mark_node;
17934 /* Forward declarations of nested types, such as
17936 class C1::C2;
17937 class C1::C2::C3;
17939 are invalid unless all components preceding the final '::'
17940 are complete. If all enclosing types are complete, these
17941 declarations become merely pointless.
17943 Invalid forward declarations of nested types are errors
17944 caught elsewhere in parsing. Those that are pointless arrive
17945 here. */
17947 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17948 && !is_friend && !processing_explicit_instantiation)
17949 warning (0, "declaration %qD does not declare anything", decl);
17951 type = TREE_TYPE (decl);
17953 else
17955 /* An elaborated-type-specifier sometimes introduces a new type and
17956 sometimes names an existing type. Normally, the rule is that it
17957 introduces a new type only if there is not an existing type of
17958 the same name already in scope. For example, given:
17960 struct S {};
17961 void f() { struct S s; }
17963 the `struct S' in the body of `f' is the same `struct S' as in
17964 the global scope; the existing definition is used. However, if
17965 there were no global declaration, this would introduce a new
17966 local class named `S'.
17968 An exception to this rule applies to the following code:
17970 namespace N { struct S; }
17972 Here, the elaborated-type-specifier names a new type
17973 unconditionally; even if there is already an `S' in the
17974 containing scope this declaration names a new type.
17975 This exception only applies if the elaborated-type-specifier
17976 forms the complete declaration:
17978 [class.name]
17980 A declaration consisting solely of `class-key identifier ;' is
17981 either a redeclaration of the name in the current scope or a
17982 forward declaration of the identifier as a class name. It
17983 introduces the name into the current scope.
17985 We are in this situation precisely when the next token is a `;'.
17987 An exception to the exception is that a `friend' declaration does
17988 *not* name a new type; i.e., given:
17990 struct S { friend struct T; };
17992 `T' is not a new type in the scope of `S'.
17994 Also, `new struct S' or `sizeof (struct S)' never results in the
17995 definition of a new type; a new type can only be declared in a
17996 declaration context. */
17998 tag_scope ts;
17999 bool template_p;
18001 if (is_friend)
18002 /* Friends have special name lookup rules. */
18003 ts = ts_within_enclosing_non_class;
18004 else if (is_declaration
18005 && cp_lexer_next_token_is (parser->lexer,
18006 CPP_SEMICOLON))
18007 /* This is a `class-key identifier ;' */
18008 ts = ts_current;
18009 else
18010 ts = ts_global;
18012 template_p =
18013 (template_parm_lists_apply
18014 && (cp_parser_next_token_starts_class_definition_p (parser)
18015 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18016 /* An unqualified name was used to reference this type, so
18017 there were no qualifying templates. */
18018 if (template_parm_lists_apply
18019 && !cp_parser_check_template_parameters (parser,
18020 /*num_templates=*/0,
18021 /*template_id*/false,
18022 token->location,
18023 /*declarator=*/NULL))
18024 return error_mark_node;
18025 type = xref_tag (tag_type, identifier, ts, template_p);
18029 if (type == error_mark_node)
18030 return error_mark_node;
18032 /* Allow attributes on forward declarations of classes. */
18033 if (attributes)
18035 if (TREE_CODE (type) == TYPENAME_TYPE)
18036 warning (OPT_Wattributes,
18037 "attributes ignored on uninstantiated type");
18038 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18039 && ! processing_explicit_instantiation)
18040 warning (OPT_Wattributes,
18041 "attributes ignored on template instantiation");
18042 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18043 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18044 else
18045 warning (OPT_Wattributes,
18046 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18049 if (tag_type != enum_type)
18051 /* Indicate whether this class was declared as a `class' or as a
18052 `struct'. */
18053 if (CLASS_TYPE_P (type))
18054 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18055 cp_parser_check_class_key (tag_type, type);
18058 /* A "<" cannot follow an elaborated type specifier. If that
18059 happens, the user was probably trying to form a template-id. */
18060 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18061 token->location);
18063 return type;
18066 /* Parse an enum-specifier.
18068 enum-specifier:
18069 enum-head { enumerator-list [opt] }
18070 enum-head { enumerator-list , } [C++0x]
18072 enum-head:
18073 enum-key identifier [opt] enum-base [opt]
18074 enum-key nested-name-specifier identifier enum-base [opt]
18076 enum-key:
18077 enum
18078 enum class [C++0x]
18079 enum struct [C++0x]
18081 enum-base: [C++0x]
18082 : type-specifier-seq
18084 opaque-enum-specifier:
18085 enum-key identifier enum-base [opt] ;
18087 GNU Extensions:
18088 enum-key attributes[opt] identifier [opt] enum-base [opt]
18089 { enumerator-list [opt] }attributes[opt]
18090 enum-key attributes[opt] identifier [opt] enum-base [opt]
18091 { enumerator-list, }attributes[opt] [C++0x]
18093 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18094 if the token stream isn't an enum-specifier after all. */
18096 static tree
18097 cp_parser_enum_specifier (cp_parser* parser)
18099 tree identifier;
18100 tree type = NULL_TREE;
18101 tree prev_scope;
18102 tree nested_name_specifier = NULL_TREE;
18103 tree attributes;
18104 bool scoped_enum_p = false;
18105 bool has_underlying_type = false;
18106 bool nested_being_defined = false;
18107 bool new_value_list = false;
18108 bool is_new_type = false;
18109 bool is_unnamed = false;
18110 tree underlying_type = NULL_TREE;
18111 cp_token *type_start_token = NULL;
18112 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18114 parser->colon_corrects_to_scope_p = false;
18116 /* Parse tentatively so that we can back up if we don't find a
18117 enum-specifier. */
18118 cp_parser_parse_tentatively (parser);
18120 /* Caller guarantees that the current token is 'enum', an identifier
18121 possibly follows, and the token after that is an opening brace.
18122 If we don't have an identifier, fabricate an anonymous name for
18123 the enumeration being defined. */
18124 cp_lexer_consume_token (parser->lexer);
18126 /* Parse the "class" or "struct", which indicates a scoped
18127 enumeration type in C++0x. */
18128 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18129 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18131 if (cxx_dialect < cxx11)
18132 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18134 /* Consume the `struct' or `class' token. */
18135 cp_lexer_consume_token (parser->lexer);
18137 scoped_enum_p = true;
18140 attributes = cp_parser_attributes_opt (parser);
18142 /* Clear the qualification. */
18143 parser->scope = NULL_TREE;
18144 parser->qualifying_scope = NULL_TREE;
18145 parser->object_scope = NULL_TREE;
18147 /* Figure out in what scope the declaration is being placed. */
18148 prev_scope = current_scope ();
18150 type_start_token = cp_lexer_peek_token (parser->lexer);
18152 push_deferring_access_checks (dk_no_check);
18153 nested_name_specifier
18154 = cp_parser_nested_name_specifier_opt (parser,
18155 /*typename_keyword_p=*/true,
18156 /*check_dependency_p=*/false,
18157 /*type_p=*/false,
18158 /*is_declaration=*/false);
18160 if (nested_name_specifier)
18162 tree name;
18164 identifier = cp_parser_identifier (parser);
18165 name = cp_parser_lookup_name (parser, identifier,
18166 enum_type,
18167 /*is_template=*/false,
18168 /*is_namespace=*/false,
18169 /*check_dependency=*/true,
18170 /*ambiguous_decls=*/NULL,
18171 input_location);
18172 if (name && name != error_mark_node)
18174 type = TREE_TYPE (name);
18175 if (TREE_CODE (type) == TYPENAME_TYPE)
18177 /* Are template enums allowed in ISO? */
18178 if (template_parm_scope_p ())
18179 pedwarn (type_start_token->location, OPT_Wpedantic,
18180 "%qD is an enumeration template", name);
18181 /* ignore a typename reference, for it will be solved by name
18182 in start_enum. */
18183 type = NULL_TREE;
18186 else if (nested_name_specifier == error_mark_node)
18187 /* We already issued an error. */;
18188 else
18190 error_at (type_start_token->location,
18191 "%qD does not name an enumeration in %qT",
18192 identifier, nested_name_specifier);
18193 nested_name_specifier = error_mark_node;
18196 else
18198 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18199 identifier = cp_parser_identifier (parser);
18200 else
18202 identifier = make_anon_name ();
18203 is_unnamed = true;
18204 if (scoped_enum_p)
18205 error_at (type_start_token->location,
18206 "unnamed scoped enum is not allowed");
18209 pop_deferring_access_checks ();
18211 /* Check for the `:' that denotes a specified underlying type in C++0x.
18212 Note that a ':' could also indicate a bitfield width, however. */
18213 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18215 cp_decl_specifier_seq type_specifiers;
18217 /* Consume the `:'. */
18218 cp_lexer_consume_token (parser->lexer);
18220 /* Parse the type-specifier-seq. */
18221 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18222 /*is_trailing_return=*/false,
18223 &type_specifiers);
18225 /* At this point this is surely not elaborated type specifier. */
18226 if (!cp_parser_parse_definitely (parser))
18227 return NULL_TREE;
18229 if (cxx_dialect < cxx11)
18230 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18232 has_underlying_type = true;
18234 /* If that didn't work, stop. */
18235 if (type_specifiers.type != error_mark_node)
18237 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18238 /*initialized=*/0, NULL);
18239 if (underlying_type == error_mark_node
18240 || check_for_bare_parameter_packs (underlying_type))
18241 underlying_type = NULL_TREE;
18245 /* Look for the `{' but don't consume it yet. */
18246 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18248 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18250 cp_parser_error (parser, "expected %<{%>");
18251 if (has_underlying_type)
18253 type = NULL_TREE;
18254 goto out;
18257 /* An opaque-enum-specifier must have a ';' here. */
18258 if ((scoped_enum_p || underlying_type)
18259 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18261 cp_parser_error (parser, "expected %<;%> or %<{%>");
18262 if (has_underlying_type)
18264 type = NULL_TREE;
18265 goto out;
18270 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18271 return NULL_TREE;
18273 if (nested_name_specifier)
18275 if (CLASS_TYPE_P (nested_name_specifier))
18277 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18278 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18279 push_scope (nested_name_specifier);
18281 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18283 push_nested_namespace (nested_name_specifier);
18287 /* Issue an error message if type-definitions are forbidden here. */
18288 if (!cp_parser_check_type_definition (parser))
18289 type = error_mark_node;
18290 else
18291 /* Create the new type. We do this before consuming the opening
18292 brace so the enum will be recorded as being on the line of its
18293 tag (or the 'enum' keyword, if there is no tag). */
18294 type = start_enum (identifier, type, underlying_type,
18295 attributes, scoped_enum_p, &is_new_type);
18297 /* If the next token is not '{' it is an opaque-enum-specifier or an
18298 elaborated-type-specifier. */
18299 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18301 timevar_push (TV_PARSE_ENUM);
18302 if (nested_name_specifier
18303 && nested_name_specifier != error_mark_node)
18305 /* The following catches invalid code such as:
18306 enum class S<int>::E { A, B, C }; */
18307 if (!processing_specialization
18308 && CLASS_TYPE_P (nested_name_specifier)
18309 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18310 error_at (type_start_token->location, "cannot add an enumerator "
18311 "list to a template instantiation");
18313 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18315 error_at (type_start_token->location,
18316 "%<%T::%E%> has not been declared",
18317 TYPE_CONTEXT (nested_name_specifier),
18318 nested_name_specifier);
18319 type = error_mark_node;
18321 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18322 && !CLASS_TYPE_P (nested_name_specifier))
18324 error_at (type_start_token->location, "nested name specifier "
18325 "%qT for enum declaration does not name a class "
18326 "or namespace", nested_name_specifier);
18327 type = error_mark_node;
18329 /* If that scope does not contain the scope in which the
18330 class was originally declared, the program is invalid. */
18331 else if (prev_scope && !is_ancestor (prev_scope,
18332 nested_name_specifier))
18334 if (at_namespace_scope_p ())
18335 error_at (type_start_token->location,
18336 "declaration of %qD in namespace %qD which does not "
18337 "enclose %qD",
18338 type, prev_scope, nested_name_specifier);
18339 else
18340 error_at (type_start_token->location,
18341 "declaration of %qD in %qD which does not "
18342 "enclose %qD",
18343 type, prev_scope, nested_name_specifier);
18344 type = error_mark_node;
18346 /* If that scope is the scope where the declaration is being placed
18347 the program is invalid. */
18348 else if (CLASS_TYPE_P (nested_name_specifier)
18349 && CLASS_TYPE_P (prev_scope)
18350 && same_type_p (nested_name_specifier, prev_scope))
18352 permerror (type_start_token->location,
18353 "extra qualification not allowed");
18354 nested_name_specifier = NULL_TREE;
18358 if (scoped_enum_p)
18359 begin_scope (sk_scoped_enum, type);
18361 /* Consume the opening brace. */
18362 matching_braces braces;
18363 braces.consume_open (parser);
18365 if (type == error_mark_node)
18366 ; /* Nothing to add */
18367 else if (OPAQUE_ENUM_P (type)
18368 || (cxx_dialect > cxx98 && processing_specialization))
18370 new_value_list = true;
18371 SET_OPAQUE_ENUM_P (type, false);
18372 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18374 else
18376 error_at (type_start_token->location,
18377 "multiple definition of %q#T", type);
18378 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18379 "previous definition here");
18380 type = error_mark_node;
18383 if (type == error_mark_node)
18384 cp_parser_skip_to_end_of_block_or_statement (parser);
18385 /* If the next token is not '}', then there are some enumerators. */
18386 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18388 if (is_unnamed && !scoped_enum_p)
18389 pedwarn (type_start_token->location, OPT_Wpedantic,
18390 "ISO C++ forbids empty unnamed enum");
18392 else
18393 cp_parser_enumerator_list (parser, type);
18395 /* Consume the final '}'. */
18396 braces.require_close (parser);
18398 if (scoped_enum_p)
18399 finish_scope ();
18400 timevar_pop (TV_PARSE_ENUM);
18402 else
18404 /* If a ';' follows, then it is an opaque-enum-specifier
18405 and additional restrictions apply. */
18406 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18408 if (is_unnamed)
18409 error_at (type_start_token->location,
18410 "opaque-enum-specifier without name");
18411 else if (nested_name_specifier)
18412 error_at (type_start_token->location,
18413 "opaque-enum-specifier must use a simple identifier");
18417 /* Look for trailing attributes to apply to this enumeration, and
18418 apply them if appropriate. */
18419 if (cp_parser_allow_gnu_extensions_p (parser))
18421 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18422 cplus_decl_attributes (&type,
18423 trailing_attr,
18424 (int) ATTR_FLAG_TYPE_IN_PLACE);
18427 /* Finish up the enumeration. */
18428 if (type != error_mark_node)
18430 if (new_value_list)
18431 finish_enum_value_list (type);
18432 if (is_new_type)
18433 finish_enum (type);
18436 if (nested_name_specifier)
18438 if (CLASS_TYPE_P (nested_name_specifier))
18440 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18441 pop_scope (nested_name_specifier);
18443 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18445 pop_nested_namespace (nested_name_specifier);
18448 out:
18449 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18450 return type;
18453 /* Parse an enumerator-list. The enumerators all have the indicated
18454 TYPE.
18456 enumerator-list:
18457 enumerator-definition
18458 enumerator-list , enumerator-definition */
18460 static void
18461 cp_parser_enumerator_list (cp_parser* parser, tree type)
18463 while (true)
18465 /* Parse an enumerator-definition. */
18466 cp_parser_enumerator_definition (parser, type);
18468 /* If the next token is not a ',', we've reached the end of
18469 the list. */
18470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18471 break;
18472 /* Otherwise, consume the `,' and keep going. */
18473 cp_lexer_consume_token (parser->lexer);
18474 /* If the next token is a `}', there is a trailing comma. */
18475 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18477 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18478 pedwarn (input_location, OPT_Wpedantic,
18479 "comma at end of enumerator list");
18480 break;
18485 /* Parse an enumerator-definition. The enumerator has the indicated
18486 TYPE.
18488 enumerator-definition:
18489 enumerator
18490 enumerator = constant-expression
18492 enumerator:
18493 identifier
18495 GNU Extensions:
18497 enumerator-definition:
18498 enumerator attributes [opt]
18499 enumerator attributes [opt] = constant-expression */
18501 static void
18502 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18504 tree identifier;
18505 tree value;
18506 location_t loc;
18508 /* Save the input location because we are interested in the location
18509 of the identifier and not the location of the explicit value. */
18510 loc = cp_lexer_peek_token (parser->lexer)->location;
18512 /* Look for the identifier. */
18513 identifier = cp_parser_identifier (parser);
18514 if (identifier == error_mark_node)
18515 return;
18517 /* Parse any specified attributes. */
18518 tree attrs = cp_parser_attributes_opt (parser);
18520 /* If the next token is an '=', then there is an explicit value. */
18521 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18523 /* Consume the `=' token. */
18524 cp_lexer_consume_token (parser->lexer);
18525 /* Parse the value. */
18526 value = cp_parser_constant_expression (parser);
18528 else
18529 value = NULL_TREE;
18531 /* If we are processing a template, make sure the initializer of the
18532 enumerator doesn't contain any bare template parameter pack. */
18533 if (check_for_bare_parameter_packs (value))
18534 value = error_mark_node;
18536 /* Create the enumerator. */
18537 build_enumerator (identifier, value, type, attrs, loc);
18540 /* Parse a namespace-name.
18542 namespace-name:
18543 original-namespace-name
18544 namespace-alias
18546 Returns the NAMESPACE_DECL for the namespace. */
18548 static tree
18549 cp_parser_namespace_name (cp_parser* parser)
18551 tree identifier;
18552 tree namespace_decl;
18554 cp_token *token = cp_lexer_peek_token (parser->lexer);
18556 /* Get the name of the namespace. */
18557 identifier = cp_parser_identifier (parser);
18558 if (identifier == error_mark_node)
18559 return error_mark_node;
18561 /* Look up the identifier in the currently active scope. Look only
18562 for namespaces, due to:
18564 [basic.lookup.udir]
18566 When looking up a namespace-name in a using-directive or alias
18567 definition, only namespace names are considered.
18569 And:
18571 [basic.lookup.qual]
18573 During the lookup of a name preceding the :: scope resolution
18574 operator, object, function, and enumerator names are ignored.
18576 (Note that cp_parser_qualifying_entity only calls this
18577 function if the token after the name is the scope resolution
18578 operator.) */
18579 namespace_decl = cp_parser_lookup_name (parser, identifier,
18580 none_type,
18581 /*is_template=*/false,
18582 /*is_namespace=*/true,
18583 /*check_dependency=*/true,
18584 /*ambiguous_decls=*/NULL,
18585 token->location);
18586 /* If it's not a namespace, issue an error. */
18587 if (namespace_decl == error_mark_node
18588 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18590 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18592 error_at (token->location, "%qD is not a namespace-name", identifier);
18593 if (namespace_decl == error_mark_node
18594 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18595 suggest_alternative_in_explicit_scope (token->location, identifier,
18596 parser->scope);
18598 cp_parser_error (parser, "expected namespace-name");
18599 namespace_decl = error_mark_node;
18602 return namespace_decl;
18605 /* Parse a namespace-definition.
18607 namespace-definition:
18608 named-namespace-definition
18609 unnamed-namespace-definition
18611 named-namespace-definition:
18612 original-namespace-definition
18613 extension-namespace-definition
18615 original-namespace-definition:
18616 namespace identifier { namespace-body }
18618 extension-namespace-definition:
18619 namespace original-namespace-name { namespace-body }
18621 unnamed-namespace-definition:
18622 namespace { namespace-body } */
18624 static void
18625 cp_parser_namespace_definition (cp_parser* parser)
18627 tree identifier;
18628 int nested_definition_count = 0;
18630 cp_ensure_no_omp_declare_simd (parser);
18631 cp_ensure_no_oacc_routine (parser);
18633 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18635 if (is_inline)
18637 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18638 cp_lexer_consume_token (parser->lexer);
18641 /* Look for the `namespace' keyword. */
18642 cp_token* token
18643 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18645 /* Parse any specified attributes before the identifier. */
18646 tree attribs = cp_parser_attributes_opt (parser);
18648 for (;;)
18650 identifier = NULL_TREE;
18652 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18654 identifier = cp_parser_identifier (parser);
18656 if (cp_next_tokens_can_be_std_attribute_p (parser))
18657 pedwarn (input_location, OPT_Wpedantic,
18658 "standard attributes on namespaces must precede "
18659 "the namespace name");
18661 /* Parse any attributes specified after the identifier. */
18662 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18665 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18666 break;
18668 if (!nested_definition_count && cxx_dialect < cxx17)
18669 pedwarn (input_location, OPT_Wpedantic,
18670 "nested namespace definitions only available with "
18671 "-std=c++17 or -std=gnu++17");
18673 /* Nested namespace names can create new namespaces (unlike
18674 other qualified-ids). */
18675 if (int count = identifier ? push_namespace (identifier) : 0)
18676 nested_definition_count += count;
18677 else
18678 cp_parser_error (parser, "nested namespace name required");
18679 cp_lexer_consume_token (parser->lexer);
18682 if (nested_definition_count && !identifier)
18683 cp_parser_error (parser, "namespace name required");
18685 if (nested_definition_count && attribs)
18686 error_at (token->location,
18687 "a nested namespace definition cannot have attributes");
18688 if (nested_definition_count && is_inline)
18689 error_at (token->location,
18690 "a nested namespace definition cannot be inline");
18692 /* Start the namespace. */
18693 nested_definition_count += push_namespace (identifier, is_inline);
18695 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18697 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18699 /* Look for the `{' to validate starting the namespace. */
18700 matching_braces braces;
18701 if (braces.require_open (parser))
18703 /* Parse the body of the namespace. */
18704 cp_parser_namespace_body (parser);
18706 /* Look for the final `}'. */
18707 braces.require_close (parser);
18710 if (has_visibility)
18711 pop_visibility (1);
18713 /* Pop the nested namespace definitions. */
18714 while (nested_definition_count--)
18715 pop_namespace ();
18718 /* Parse a namespace-body.
18720 namespace-body:
18721 declaration-seq [opt] */
18723 static void
18724 cp_parser_namespace_body (cp_parser* parser)
18726 cp_parser_declaration_seq_opt (parser);
18729 /* Parse a namespace-alias-definition.
18731 namespace-alias-definition:
18732 namespace identifier = qualified-namespace-specifier ; */
18734 static void
18735 cp_parser_namespace_alias_definition (cp_parser* parser)
18737 tree identifier;
18738 tree namespace_specifier;
18740 cp_token *token = cp_lexer_peek_token (parser->lexer);
18742 /* Look for the `namespace' keyword. */
18743 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18744 /* Look for the identifier. */
18745 identifier = cp_parser_identifier (parser);
18746 if (identifier == error_mark_node)
18747 return;
18748 /* Look for the `=' token. */
18749 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18750 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18752 error_at (token->location, "%<namespace%> definition is not allowed here");
18753 /* Skip the definition. */
18754 cp_lexer_consume_token (parser->lexer);
18755 if (cp_parser_skip_to_closing_brace (parser))
18756 cp_lexer_consume_token (parser->lexer);
18757 return;
18759 cp_parser_require (parser, CPP_EQ, RT_EQ);
18760 /* Look for the qualified-namespace-specifier. */
18761 namespace_specifier
18762 = cp_parser_qualified_namespace_specifier (parser);
18763 /* Look for the `;' token. */
18764 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18766 /* Register the alias in the symbol table. */
18767 do_namespace_alias (identifier, namespace_specifier);
18770 /* Parse a qualified-namespace-specifier.
18772 qualified-namespace-specifier:
18773 :: [opt] nested-name-specifier [opt] namespace-name
18775 Returns a NAMESPACE_DECL corresponding to the specified
18776 namespace. */
18778 static tree
18779 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18781 /* Look for the optional `::'. */
18782 cp_parser_global_scope_opt (parser,
18783 /*current_scope_valid_p=*/false);
18785 /* Look for the optional nested-name-specifier. */
18786 cp_parser_nested_name_specifier_opt (parser,
18787 /*typename_keyword_p=*/false,
18788 /*check_dependency_p=*/true,
18789 /*type_p=*/false,
18790 /*is_declaration=*/true);
18792 return cp_parser_namespace_name (parser);
18795 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18796 access declaration.
18798 using-declaration:
18799 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18800 using :: unqualified-id ;
18802 access-declaration:
18803 qualified-id ;
18807 static bool
18808 cp_parser_using_declaration (cp_parser* parser,
18809 bool access_declaration_p)
18811 cp_token *token;
18812 bool typename_p = false;
18813 bool global_scope_p;
18814 tree decl;
18815 tree identifier;
18816 tree qscope;
18817 int oldcount = errorcount;
18818 cp_token *diag_token = NULL;
18820 if (access_declaration_p)
18822 diag_token = cp_lexer_peek_token (parser->lexer);
18823 cp_parser_parse_tentatively (parser);
18825 else
18827 /* Look for the `using' keyword. */
18828 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18830 again:
18831 /* Peek at the next token. */
18832 token = cp_lexer_peek_token (parser->lexer);
18833 /* See if it's `typename'. */
18834 if (token->keyword == RID_TYPENAME)
18836 /* Remember that we've seen it. */
18837 typename_p = true;
18838 /* Consume the `typename' token. */
18839 cp_lexer_consume_token (parser->lexer);
18843 /* Look for the optional global scope qualification. */
18844 global_scope_p
18845 = (cp_parser_global_scope_opt (parser,
18846 /*current_scope_valid_p=*/false)
18847 != NULL_TREE);
18849 /* If we saw `typename', or didn't see `::', then there must be a
18850 nested-name-specifier present. */
18851 if (typename_p || !global_scope_p)
18853 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18854 /*check_dependency_p=*/true,
18855 /*type_p=*/false,
18856 /*is_declaration=*/true);
18857 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18859 cp_parser_skip_to_end_of_block_or_statement (parser);
18860 return false;
18863 /* Otherwise, we could be in either of the two productions. In that
18864 case, treat the nested-name-specifier as optional. */
18865 else
18866 qscope = cp_parser_nested_name_specifier_opt (parser,
18867 /*typename_keyword_p=*/false,
18868 /*check_dependency_p=*/true,
18869 /*type_p=*/false,
18870 /*is_declaration=*/true);
18871 if (!qscope)
18872 qscope = global_namespace;
18873 else if (UNSCOPED_ENUM_P (qscope))
18874 qscope = CP_TYPE_CONTEXT (qscope);
18876 if (access_declaration_p && cp_parser_error_occurred (parser))
18877 /* Something has already gone wrong; there's no need to parse
18878 further. Since an error has occurred, the return value of
18879 cp_parser_parse_definitely will be false, as required. */
18880 return cp_parser_parse_definitely (parser);
18882 token = cp_lexer_peek_token (parser->lexer);
18883 /* Parse the unqualified-id. */
18884 identifier = cp_parser_unqualified_id (parser,
18885 /*template_keyword_p=*/false,
18886 /*check_dependency_p=*/true,
18887 /*declarator_p=*/true,
18888 /*optional_p=*/false);
18890 if (access_declaration_p)
18892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18893 cp_parser_simulate_error (parser);
18894 if (!cp_parser_parse_definitely (parser))
18895 return false;
18897 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18899 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18900 if (cxx_dialect < cxx17
18901 && !in_system_header_at (ell->location))
18902 pedwarn (ell->location, 0,
18903 "pack expansion in using-declaration only available "
18904 "with -std=c++17 or -std=gnu++17");
18905 qscope = make_pack_expansion (qscope);
18908 /* The function we call to handle a using-declaration is different
18909 depending on what scope we are in. */
18910 if (qscope == error_mark_node || identifier == error_mark_node)
18912 else if (!identifier_p (identifier)
18913 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18914 /* [namespace.udecl]
18916 A using declaration shall not name a template-id. */
18917 error_at (token->location,
18918 "a template-id may not appear in a using-declaration");
18919 else
18921 if (at_class_scope_p ())
18923 /* Create the USING_DECL. */
18924 decl = do_class_using_decl (qscope, identifier);
18926 if (decl && typename_p)
18927 USING_DECL_TYPENAME_P (decl) = 1;
18929 if (check_for_bare_parameter_packs (decl))
18931 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18932 return false;
18934 else
18935 /* Add it to the list of members in this class. */
18936 finish_member_declaration (decl);
18938 else
18940 decl = cp_parser_lookup_name_simple (parser,
18941 identifier,
18942 token->location);
18943 if (decl == error_mark_node)
18944 cp_parser_name_lookup_error (parser, identifier,
18945 decl, NLE_NULL,
18946 token->location);
18947 else if (check_for_bare_parameter_packs (decl))
18949 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18950 return false;
18952 else if (!at_namespace_scope_p ())
18953 finish_local_using_decl (decl, qscope, identifier);
18954 else
18955 finish_namespace_using_decl (decl, qscope, identifier);
18959 if (!access_declaration_p
18960 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18962 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18963 if (cxx_dialect < cxx17)
18964 pedwarn (comma->location, 0,
18965 "comma-separated list in using-declaration only available "
18966 "with -std=c++17 or -std=gnu++17");
18967 goto again;
18970 /* Look for the final `;'. */
18971 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18973 if (access_declaration_p && errorcount == oldcount)
18974 warning_at (diag_token->location, OPT_Wdeprecated,
18975 "access declarations are deprecated "
18976 "in favour of using-declarations; "
18977 "suggestion: add the %<using%> keyword");
18979 return true;
18982 /* Parse an alias-declaration.
18984 alias-declaration:
18985 using identifier attribute-specifier-seq [opt] = type-id */
18987 static tree
18988 cp_parser_alias_declaration (cp_parser* parser)
18990 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18991 location_t id_location;
18992 cp_declarator *declarator;
18993 cp_decl_specifier_seq decl_specs;
18994 bool member_p;
18995 const char *saved_message = NULL;
18997 /* Look for the `using' keyword. */
18998 cp_token *using_token
18999 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19000 if (using_token == NULL)
19001 return error_mark_node;
19003 id_location = cp_lexer_peek_token (parser->lexer)->location;
19004 id = cp_parser_identifier (parser);
19005 if (id == error_mark_node)
19006 return error_mark_node;
19008 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19009 attributes = cp_parser_attributes_opt (parser);
19010 if (attributes == error_mark_node)
19011 return error_mark_node;
19013 cp_parser_require (parser, CPP_EQ, RT_EQ);
19015 if (cp_parser_error_occurred (parser))
19016 return error_mark_node;
19018 cp_parser_commit_to_tentative_parse (parser);
19020 /* Now we are going to parse the type-id of the declaration. */
19023 [dcl.type]/3 says:
19025 "A type-specifier-seq shall not define a class or enumeration
19026 unless it appears in the type-id of an alias-declaration (7.1.3) that
19027 is not the declaration of a template-declaration."
19029 In other words, if we currently are in an alias template, the
19030 type-id should not define a type.
19032 So let's set parser->type_definition_forbidden_message in that
19033 case; cp_parser_check_type_definition (called by
19034 cp_parser_class_specifier) will then emit an error if a type is
19035 defined in the type-id. */
19036 if (parser->num_template_parameter_lists)
19038 saved_message = parser->type_definition_forbidden_message;
19039 parser->type_definition_forbidden_message =
19040 G_("types may not be defined in alias template declarations");
19043 type = cp_parser_type_id (parser);
19045 /* Restore the error message if need be. */
19046 if (parser->num_template_parameter_lists)
19047 parser->type_definition_forbidden_message = saved_message;
19049 if (type == error_mark_node
19050 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19052 cp_parser_skip_to_end_of_block_or_statement (parser);
19053 return error_mark_node;
19056 /* A typedef-name can also be introduced by an alias-declaration. The
19057 identifier following the using keyword becomes a typedef-name. It has
19058 the same semantics as if it were introduced by the typedef
19059 specifier. In particular, it does not define a new type and it shall
19060 not appear in the type-id. */
19062 clear_decl_specs (&decl_specs);
19063 decl_specs.type = type;
19064 if (attributes != NULL_TREE)
19066 decl_specs.attributes = attributes;
19067 set_and_check_decl_spec_loc (&decl_specs,
19068 ds_attribute,
19069 attrs_token);
19071 set_and_check_decl_spec_loc (&decl_specs,
19072 ds_typedef,
19073 using_token);
19074 set_and_check_decl_spec_loc (&decl_specs,
19075 ds_alias,
19076 using_token);
19078 if (parser->num_template_parameter_lists
19079 && !cp_parser_check_template_parameters (parser,
19080 /*num_templates=*/0,
19081 /*template_id*/false,
19082 id_location,
19083 /*declarator=*/NULL))
19084 return error_mark_node;
19086 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19087 declarator->id_loc = id_location;
19089 member_p = at_class_scope_p ();
19090 if (member_p)
19091 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19092 NULL_TREE, attributes);
19093 else
19094 decl = start_decl (declarator, &decl_specs, 0,
19095 attributes, NULL_TREE, &pushed_scope);
19096 if (decl == error_mark_node)
19097 return decl;
19099 // Attach constraints to the alias declaration.
19100 if (flag_concepts && current_template_parms)
19102 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19103 tree constr = build_constraints (reqs, NULL_TREE);
19104 set_constraints (decl, constr);
19107 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19109 if (pushed_scope)
19110 pop_scope (pushed_scope);
19112 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19113 added into the symbol table; otherwise, return the TYPE_DECL. */
19114 if (DECL_LANG_SPECIFIC (decl)
19115 && DECL_TEMPLATE_INFO (decl)
19116 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19118 decl = DECL_TI_TEMPLATE (decl);
19119 if (member_p)
19120 check_member_template (decl);
19123 return decl;
19126 /* Parse a using-directive.
19128 using-directive:
19129 using namespace :: [opt] nested-name-specifier [opt]
19130 namespace-name ; */
19132 static void
19133 cp_parser_using_directive (cp_parser* parser)
19135 tree namespace_decl;
19136 tree attribs;
19138 /* Look for the `using' keyword. */
19139 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19140 /* And the `namespace' keyword. */
19141 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19142 /* Look for the optional `::' operator. */
19143 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19144 /* And the optional nested-name-specifier. */
19145 cp_parser_nested_name_specifier_opt (parser,
19146 /*typename_keyword_p=*/false,
19147 /*check_dependency_p=*/true,
19148 /*type_p=*/false,
19149 /*is_declaration=*/true);
19150 /* Get the namespace being used. */
19151 namespace_decl = cp_parser_namespace_name (parser);
19152 /* And any specified attributes. */
19153 attribs = cp_parser_attributes_opt (parser);
19155 /* Update the symbol table. */
19156 if (namespace_bindings_p ())
19157 finish_namespace_using_directive (namespace_decl, attribs);
19158 else
19159 finish_local_using_directive (namespace_decl, attribs);
19161 /* Look for the final `;'. */
19162 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19165 /* Parse an asm-definition.
19167 asm-definition:
19168 asm ( string-literal ) ;
19170 GNU Extension:
19172 asm-definition:
19173 asm volatile [opt] ( string-literal ) ;
19174 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19175 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19176 : asm-operand-list [opt] ) ;
19177 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19178 : asm-operand-list [opt]
19179 : asm-clobber-list [opt] ) ;
19180 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19181 : asm-clobber-list [opt]
19182 : asm-goto-list ) ; */
19184 static void
19185 cp_parser_asm_definition (cp_parser* parser)
19187 tree string;
19188 tree outputs = NULL_TREE;
19189 tree inputs = NULL_TREE;
19190 tree clobbers = NULL_TREE;
19191 tree labels = NULL_TREE;
19192 tree asm_stmt;
19193 bool volatile_p = false;
19194 bool extended_p = false;
19195 bool invalid_inputs_p = false;
19196 bool invalid_outputs_p = false;
19197 bool goto_p = false;
19198 required_token missing = RT_NONE;
19200 /* Look for the `asm' keyword. */
19201 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19203 if (parser->in_function_body
19204 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19206 error ("%<asm%> in %<constexpr%> function");
19207 cp_function_chain->invalid_constexpr = true;
19210 /* See if the next token is `volatile'. */
19211 if (cp_parser_allow_gnu_extensions_p (parser)
19212 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19214 /* Remember that we saw the `volatile' keyword. */
19215 volatile_p = true;
19216 /* Consume the token. */
19217 cp_lexer_consume_token (parser->lexer);
19219 if (cp_parser_allow_gnu_extensions_p (parser)
19220 && parser->in_function_body
19221 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19223 /* Remember that we saw the `goto' keyword. */
19224 goto_p = true;
19225 /* Consume the token. */
19226 cp_lexer_consume_token (parser->lexer);
19228 /* Look for the opening `('. */
19229 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19230 return;
19231 /* Look for the string. */
19232 string = cp_parser_string_literal (parser, false, false);
19233 if (string == error_mark_node)
19235 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19236 /*consume_paren=*/true);
19237 return;
19240 /* If we're allowing GNU extensions, check for the extended assembly
19241 syntax. Unfortunately, the `:' tokens need not be separated by
19242 a space in C, and so, for compatibility, we tolerate that here
19243 too. Doing that means that we have to treat the `::' operator as
19244 two `:' tokens. */
19245 if (cp_parser_allow_gnu_extensions_p (parser)
19246 && parser->in_function_body
19247 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19248 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19250 bool inputs_p = false;
19251 bool clobbers_p = false;
19252 bool labels_p = false;
19254 /* The extended syntax was used. */
19255 extended_p = true;
19257 /* Look for outputs. */
19258 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19260 /* Consume the `:'. */
19261 cp_lexer_consume_token (parser->lexer);
19262 /* Parse the output-operands. */
19263 if (cp_lexer_next_token_is_not (parser->lexer,
19264 CPP_COLON)
19265 && cp_lexer_next_token_is_not (parser->lexer,
19266 CPP_SCOPE)
19267 && cp_lexer_next_token_is_not (parser->lexer,
19268 CPP_CLOSE_PAREN)
19269 && !goto_p)
19271 outputs = cp_parser_asm_operand_list (parser);
19272 if (outputs == error_mark_node)
19273 invalid_outputs_p = true;
19276 /* If the next token is `::', there are no outputs, and the
19277 next token is the beginning of the inputs. */
19278 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19279 /* The inputs are coming next. */
19280 inputs_p = true;
19282 /* Look for inputs. */
19283 if (inputs_p
19284 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19286 /* Consume the `:' or `::'. */
19287 cp_lexer_consume_token (parser->lexer);
19288 /* Parse the output-operands. */
19289 if (cp_lexer_next_token_is_not (parser->lexer,
19290 CPP_COLON)
19291 && cp_lexer_next_token_is_not (parser->lexer,
19292 CPP_SCOPE)
19293 && cp_lexer_next_token_is_not (parser->lexer,
19294 CPP_CLOSE_PAREN))
19296 inputs = cp_parser_asm_operand_list (parser);
19297 if (inputs == error_mark_node)
19298 invalid_inputs_p = true;
19301 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19302 /* The clobbers are coming next. */
19303 clobbers_p = true;
19305 /* Look for clobbers. */
19306 if (clobbers_p
19307 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19309 clobbers_p = true;
19310 /* Consume the `:' or `::'. */
19311 cp_lexer_consume_token (parser->lexer);
19312 /* Parse the clobbers. */
19313 if (cp_lexer_next_token_is_not (parser->lexer,
19314 CPP_COLON)
19315 && cp_lexer_next_token_is_not (parser->lexer,
19316 CPP_CLOSE_PAREN))
19317 clobbers = cp_parser_asm_clobber_list (parser);
19319 else if (goto_p
19320 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19321 /* The labels are coming next. */
19322 labels_p = true;
19324 /* Look for labels. */
19325 if (labels_p
19326 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19328 labels_p = true;
19329 /* Consume the `:' or `::'. */
19330 cp_lexer_consume_token (parser->lexer);
19331 /* Parse the labels. */
19332 labels = cp_parser_asm_label_list (parser);
19335 if (goto_p && !labels_p)
19336 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19338 else if (goto_p)
19339 missing = RT_COLON_SCOPE;
19341 /* Look for the closing `)'. */
19342 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19343 missing ? missing : RT_CLOSE_PAREN))
19344 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19345 /*consume_paren=*/true);
19346 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19348 if (!invalid_inputs_p && !invalid_outputs_p)
19350 /* Create the ASM_EXPR. */
19351 if (parser->in_function_body)
19353 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19354 inputs, clobbers, labels);
19355 /* If the extended syntax was not used, mark the ASM_EXPR. */
19356 if (!extended_p)
19358 tree temp = asm_stmt;
19359 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19360 temp = TREE_OPERAND (temp, 0);
19362 ASM_INPUT_P (temp) = 1;
19365 else
19366 symtab->finalize_toplevel_asm (string);
19370 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19371 type that comes from the decl-specifier-seq. */
19373 static tree
19374 strip_declarator_types (tree type, cp_declarator *declarator)
19376 for (cp_declarator *d = declarator; d;)
19377 switch (d->kind)
19379 case cdk_id:
19380 case cdk_decomp:
19381 case cdk_error:
19382 d = NULL;
19383 break;
19385 default:
19386 if (TYPE_PTRMEMFUNC_P (type))
19387 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19388 type = TREE_TYPE (type);
19389 d = d->declarator;
19390 break;
19393 return type;
19396 /* Declarators [gram.dcl.decl] */
19398 /* Parse an init-declarator.
19400 init-declarator:
19401 declarator initializer [opt]
19403 GNU Extension:
19405 init-declarator:
19406 declarator asm-specification [opt] attributes [opt] initializer [opt]
19408 function-definition:
19409 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19410 function-body
19411 decl-specifier-seq [opt] declarator function-try-block
19413 GNU Extension:
19415 function-definition:
19416 __extension__ function-definition
19418 TM Extension:
19420 function-definition:
19421 decl-specifier-seq [opt] declarator function-transaction-block
19423 The DECL_SPECIFIERS apply to this declarator. Returns a
19424 representation of the entity declared. If MEMBER_P is TRUE, then
19425 this declarator appears in a class scope. The new DECL created by
19426 this declarator is returned.
19428 The CHECKS are access checks that should be performed once we know
19429 what entity is being declared (and, therefore, what classes have
19430 befriended it).
19432 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19433 for a function-definition here as well. If the declarator is a
19434 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19435 be TRUE upon return. By that point, the function-definition will
19436 have been completely parsed.
19438 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19439 is FALSE.
19441 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19442 parsed declaration if it is an uninitialized single declarator not followed
19443 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19444 if present, will not be consumed. If returned, this declarator will be
19445 created with SD_INITIALIZED but will not call cp_finish_decl.
19447 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19448 and there is an initializer, the pointed location_t is set to the
19449 location of the '=' or `(', or '{' in C++11 token introducing the
19450 initializer. */
19452 static tree
19453 cp_parser_init_declarator (cp_parser* parser,
19454 cp_decl_specifier_seq *decl_specifiers,
19455 vec<deferred_access_check, va_gc> *checks,
19456 bool function_definition_allowed_p,
19457 bool member_p,
19458 int declares_class_or_enum,
19459 bool* function_definition_p,
19460 tree* maybe_range_for_decl,
19461 location_t* init_loc,
19462 tree* auto_result)
19464 cp_token *token = NULL, *asm_spec_start_token = NULL,
19465 *attributes_start_token = NULL;
19466 cp_declarator *declarator;
19467 tree prefix_attributes;
19468 tree attributes = NULL;
19469 tree asm_specification;
19470 tree initializer;
19471 tree decl = NULL_TREE;
19472 tree scope;
19473 int is_initialized;
19474 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19475 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19476 "(...)". */
19477 enum cpp_ttype initialization_kind;
19478 bool is_direct_init = false;
19479 bool is_non_constant_init;
19480 int ctor_dtor_or_conv_p;
19481 bool friend_p = cp_parser_friend_p (decl_specifiers);
19482 tree pushed_scope = NULL_TREE;
19483 bool range_for_decl_p = false;
19484 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19485 location_t tmp_init_loc = UNKNOWN_LOCATION;
19487 /* Gather the attributes that were provided with the
19488 decl-specifiers. */
19489 prefix_attributes = decl_specifiers->attributes;
19491 /* Assume that this is not the declarator for a function
19492 definition. */
19493 if (function_definition_p)
19494 *function_definition_p = false;
19496 /* Default arguments are only permitted for function parameters. */
19497 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19498 parser->default_arg_ok_p = false;
19500 /* Defer access checks while parsing the declarator; we cannot know
19501 what names are accessible until we know what is being
19502 declared. */
19503 resume_deferring_access_checks ();
19505 token = cp_lexer_peek_token (parser->lexer);
19507 /* Parse the declarator. */
19508 declarator
19509 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19510 &ctor_dtor_or_conv_p,
19511 /*parenthesized_p=*/NULL,
19512 member_p, friend_p);
19513 /* Gather up the deferred checks. */
19514 stop_deferring_access_checks ();
19516 parser->default_arg_ok_p = saved_default_arg_ok_p;
19518 /* If the DECLARATOR was erroneous, there's no need to go
19519 further. */
19520 if (declarator == cp_error_declarator)
19521 return error_mark_node;
19523 /* Check that the number of template-parameter-lists is OK. */
19524 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19525 token->location))
19526 return error_mark_node;
19528 if (declares_class_or_enum & 2)
19529 cp_parser_check_for_definition_in_return_type (declarator,
19530 decl_specifiers->type,
19531 decl_specifiers->locations[ds_type_spec]);
19533 /* Figure out what scope the entity declared by the DECLARATOR is
19534 located in. `grokdeclarator' sometimes changes the scope, so
19535 we compute it now. */
19536 scope = get_scope_of_declarator (declarator);
19538 /* Perform any lookups in the declared type which were thought to be
19539 dependent, but are not in the scope of the declarator. */
19540 decl_specifiers->type
19541 = maybe_update_decl_type (decl_specifiers->type, scope);
19543 /* If we're allowing GNU extensions, look for an
19544 asm-specification. */
19545 if (cp_parser_allow_gnu_extensions_p (parser))
19547 /* Look for an asm-specification. */
19548 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19549 asm_specification = cp_parser_asm_specification_opt (parser);
19551 else
19552 asm_specification = NULL_TREE;
19554 /* Look for attributes. */
19555 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19556 attributes = cp_parser_attributes_opt (parser);
19558 /* Peek at the next token. */
19559 token = cp_lexer_peek_token (parser->lexer);
19561 bool bogus_implicit_tmpl = false;
19563 if (function_declarator_p (declarator))
19565 /* Handle C++17 deduction guides. */
19566 if (!decl_specifiers->type
19567 && ctor_dtor_or_conv_p <= 0
19568 && cxx_dialect >= cxx17)
19570 cp_declarator *id = get_id_declarator (declarator);
19571 tree name = id->u.id.unqualified_name;
19572 parser->scope = id->u.id.qualifying_scope;
19573 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19574 if (tmpl
19575 && (DECL_CLASS_TEMPLATE_P (tmpl)
19576 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19578 id->u.id.unqualified_name = dguide_name (tmpl);
19579 id->u.id.sfk = sfk_deduction_guide;
19580 ctor_dtor_or_conv_p = 1;
19584 /* Check to see if the token indicates the start of a
19585 function-definition. */
19586 if (cp_parser_token_starts_function_definition_p (token))
19588 if (!function_definition_allowed_p)
19590 /* If a function-definition should not appear here, issue an
19591 error message. */
19592 cp_parser_error (parser,
19593 "a function-definition is not allowed here");
19594 return error_mark_node;
19597 location_t func_brace_location
19598 = cp_lexer_peek_token (parser->lexer)->location;
19600 /* Neither attributes nor an asm-specification are allowed
19601 on a function-definition. */
19602 if (asm_specification)
19603 error_at (asm_spec_start_token->location,
19604 "an asm-specification is not allowed "
19605 "on a function-definition");
19606 if (attributes)
19607 error_at (attributes_start_token->location,
19608 "attributes are not allowed "
19609 "on a function-definition");
19610 /* This is a function-definition. */
19611 *function_definition_p = true;
19613 /* Parse the function definition. */
19614 if (member_p)
19615 decl = cp_parser_save_member_function_body (parser,
19616 decl_specifiers,
19617 declarator,
19618 prefix_attributes);
19619 else
19620 decl =
19621 (cp_parser_function_definition_from_specifiers_and_declarator
19622 (parser, decl_specifiers, prefix_attributes, declarator));
19624 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19626 /* This is where the prologue starts... */
19627 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19628 = func_brace_location;
19631 return decl;
19634 else if (parser->fully_implicit_function_template_p)
19636 /* A non-template declaration involving a function parameter list
19637 containing an implicit template parameter will be made into a
19638 template. If the resulting declaration is not going to be an
19639 actual function then finish the template scope here to prevent it.
19640 An error message will be issued once we have a decl to talk about.
19642 FIXME probably we should do type deduction rather than create an
19643 implicit template, but the standard currently doesn't allow it. */
19644 bogus_implicit_tmpl = true;
19645 finish_fully_implicit_template (parser, NULL_TREE);
19648 /* [dcl.dcl]
19650 Only in function declarations for constructors, destructors, type
19651 conversions, and deduction guides can the decl-specifier-seq be omitted.
19653 We explicitly postpone this check past the point where we handle
19654 function-definitions because we tolerate function-definitions
19655 that are missing their return types in some modes. */
19656 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19658 cp_parser_error (parser,
19659 "expected constructor, destructor, or type conversion");
19660 return error_mark_node;
19663 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19664 if (token->type == CPP_EQ
19665 || token->type == CPP_OPEN_PAREN
19666 || token->type == CPP_OPEN_BRACE)
19668 is_initialized = SD_INITIALIZED;
19669 initialization_kind = token->type;
19670 if (maybe_range_for_decl)
19671 *maybe_range_for_decl = error_mark_node;
19672 tmp_init_loc = token->location;
19673 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19674 *init_loc = tmp_init_loc;
19676 if (token->type == CPP_EQ
19677 && function_declarator_p (declarator))
19679 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19680 if (t2->keyword == RID_DEFAULT)
19681 is_initialized = SD_DEFAULTED;
19682 else if (t2->keyword == RID_DELETE)
19683 is_initialized = SD_DELETED;
19686 else
19688 /* If the init-declarator isn't initialized and isn't followed by a
19689 `,' or `;', it's not a valid init-declarator. */
19690 if (token->type != CPP_COMMA
19691 && token->type != CPP_SEMICOLON)
19693 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19694 range_for_decl_p = true;
19695 else
19697 if (!maybe_range_for_decl)
19698 cp_parser_error (parser, "expected initializer");
19699 return error_mark_node;
19702 is_initialized = SD_UNINITIALIZED;
19703 initialization_kind = CPP_EOF;
19706 /* Because start_decl has side-effects, we should only call it if we
19707 know we're going ahead. By this point, we know that we cannot
19708 possibly be looking at any other construct. */
19709 cp_parser_commit_to_tentative_parse (parser);
19711 /* Enter the newly declared entry in the symbol table. If we're
19712 processing a declaration in a class-specifier, we wait until
19713 after processing the initializer. */
19714 if (!member_p)
19716 if (parser->in_unbraced_linkage_specification_p)
19717 decl_specifiers->storage_class = sc_extern;
19718 decl = start_decl (declarator, decl_specifiers,
19719 range_for_decl_p? SD_INITIALIZED : is_initialized,
19720 attributes, prefix_attributes, &pushed_scope);
19721 cp_finalize_omp_declare_simd (parser, decl);
19722 cp_finalize_oacc_routine (parser, decl, false);
19723 /* Adjust location of decl if declarator->id_loc is more appropriate:
19724 set, and decl wasn't merged with another decl, in which case its
19725 location would be different from input_location, and more accurate. */
19726 if (DECL_P (decl)
19727 && declarator->id_loc != UNKNOWN_LOCATION
19728 && DECL_SOURCE_LOCATION (decl) == input_location)
19729 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19731 else if (scope)
19732 /* Enter the SCOPE. That way unqualified names appearing in the
19733 initializer will be looked up in SCOPE. */
19734 pushed_scope = push_scope (scope);
19736 /* Perform deferred access control checks, now that we know in which
19737 SCOPE the declared entity resides. */
19738 if (!member_p && decl)
19740 tree saved_current_function_decl = NULL_TREE;
19742 /* If the entity being declared is a function, pretend that we
19743 are in its scope. If it is a `friend', it may have access to
19744 things that would not otherwise be accessible. */
19745 if (TREE_CODE (decl) == FUNCTION_DECL)
19747 saved_current_function_decl = current_function_decl;
19748 current_function_decl = decl;
19751 /* Perform access checks for template parameters. */
19752 cp_parser_perform_template_parameter_access_checks (checks);
19754 /* Perform the access control checks for the declarator and the
19755 decl-specifiers. */
19756 perform_deferred_access_checks (tf_warning_or_error);
19758 /* Restore the saved value. */
19759 if (TREE_CODE (decl) == FUNCTION_DECL)
19760 current_function_decl = saved_current_function_decl;
19763 /* Parse the initializer. */
19764 initializer = NULL_TREE;
19765 is_direct_init = false;
19766 is_non_constant_init = true;
19767 if (is_initialized)
19769 if (function_declarator_p (declarator))
19771 if (initialization_kind == CPP_EQ)
19772 initializer = cp_parser_pure_specifier (parser);
19773 else
19775 /* If the declaration was erroneous, we don't really
19776 know what the user intended, so just silently
19777 consume the initializer. */
19778 if (decl != error_mark_node)
19779 error_at (tmp_init_loc, "initializer provided for function");
19780 cp_parser_skip_to_closing_parenthesis (parser,
19781 /*recovering=*/true,
19782 /*or_comma=*/false,
19783 /*consume_paren=*/true);
19786 else
19788 /* We want to record the extra mangling scope for in-class
19789 initializers of class members and initializers of static data
19790 member templates. The former involves deferring
19791 parsing of the initializer until end of class as with default
19792 arguments. So right here we only handle the latter. */
19793 if (!member_p && processing_template_decl && decl != error_mark_node)
19794 start_lambda_scope (decl);
19795 initializer = cp_parser_initializer (parser,
19796 &is_direct_init,
19797 &is_non_constant_init);
19798 if (!member_p && processing_template_decl && decl != error_mark_node)
19799 finish_lambda_scope ();
19800 if (initializer == error_mark_node)
19801 cp_parser_skip_to_end_of_statement (parser);
19805 /* The old parser allows attributes to appear after a parenthesized
19806 initializer. Mark Mitchell proposed removing this functionality
19807 on the GCC mailing lists on 2002-08-13. This parser accepts the
19808 attributes -- but ignores them. Made a permerror in GCC 8. */
19809 if (cp_parser_allow_gnu_extensions_p (parser)
19810 && initialization_kind == CPP_OPEN_PAREN
19811 && cp_parser_attributes_opt (parser)
19812 && permerror (input_location,
19813 "attributes after parenthesized initializer ignored"))
19815 static bool hint;
19816 if (flag_permissive && !hint)
19818 hint = true;
19819 inform (input_location,
19820 "this flexibility is deprecated and will be removed");
19824 /* And now complain about a non-function implicit template. */
19825 if (bogus_implicit_tmpl && decl != error_mark_node)
19826 error_at (DECL_SOURCE_LOCATION (decl),
19827 "non-function %qD declared as implicit template", decl);
19829 /* For an in-class declaration, use `grokfield' to create the
19830 declaration. */
19831 if (member_p)
19833 if (pushed_scope)
19835 pop_scope (pushed_scope);
19836 pushed_scope = NULL_TREE;
19838 decl = grokfield (declarator, decl_specifiers,
19839 initializer, !is_non_constant_init,
19840 /*asmspec=*/NULL_TREE,
19841 attr_chainon (attributes, prefix_attributes));
19842 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19843 cp_parser_save_default_args (parser, decl);
19844 cp_finalize_omp_declare_simd (parser, decl);
19845 cp_finalize_oacc_routine (parser, decl, false);
19848 /* Finish processing the declaration. But, skip member
19849 declarations. */
19850 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19852 cp_finish_decl (decl,
19853 initializer, !is_non_constant_init,
19854 asm_specification,
19855 /* If the initializer is in parentheses, then this is
19856 a direct-initialization, which means that an
19857 `explicit' constructor is OK. Otherwise, an
19858 `explicit' constructor cannot be used. */
19859 ((is_direct_init || !is_initialized)
19860 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19862 else if ((cxx_dialect != cxx98) && friend_p
19863 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19864 /* Core issue #226 (C++0x only): A default template-argument
19865 shall not be specified in a friend class template
19866 declaration. */
19867 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19868 /*is_partial=*/false, /*is_friend_decl=*/1);
19870 if (!friend_p && pushed_scope)
19871 pop_scope (pushed_scope);
19873 if (function_declarator_p (declarator)
19874 && parser->fully_implicit_function_template_p)
19876 if (member_p)
19877 decl = finish_fully_implicit_template (parser, decl);
19878 else
19879 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19882 if (auto_result && is_initialized && decl_specifiers->type
19883 && type_uses_auto (decl_specifiers->type))
19884 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19886 return decl;
19889 /* Parse a declarator.
19891 declarator:
19892 direct-declarator
19893 ptr-operator declarator
19895 abstract-declarator:
19896 ptr-operator abstract-declarator [opt]
19897 direct-abstract-declarator
19899 GNU Extensions:
19901 declarator:
19902 attributes [opt] direct-declarator
19903 attributes [opt] ptr-operator declarator
19905 abstract-declarator:
19906 attributes [opt] ptr-operator abstract-declarator [opt]
19907 attributes [opt] direct-abstract-declarator
19909 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19910 detect constructors, destructors, deduction guides, or conversion operators.
19911 It is set to -1 if the declarator is a name, and +1 if it is a
19912 function. Otherwise it is set to zero. Usually you just want to
19913 test for >0, but internally the negative value is used.
19915 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19916 a decl-specifier-seq unless it declares a constructor, destructor,
19917 or conversion. It might seem that we could check this condition in
19918 semantic analysis, rather than parsing, but that makes it difficult
19919 to handle something like `f()'. We want to notice that there are
19920 no decl-specifiers, and therefore realize that this is an
19921 expression, not a declaration.)
19923 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19924 the declarator is a direct-declarator of the form "(...)".
19926 MEMBER_P is true iff this declarator is a member-declarator.
19928 FRIEND_P is true iff this declarator is a friend. */
19930 static cp_declarator *
19931 cp_parser_declarator (cp_parser* parser,
19932 cp_parser_declarator_kind dcl_kind,
19933 int* ctor_dtor_or_conv_p,
19934 bool* parenthesized_p,
19935 bool member_p, bool friend_p)
19937 cp_declarator *declarator;
19938 enum tree_code code;
19939 cp_cv_quals cv_quals;
19940 tree class_type;
19941 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19943 /* Assume this is not a constructor, destructor, or type-conversion
19944 operator. */
19945 if (ctor_dtor_or_conv_p)
19946 *ctor_dtor_or_conv_p = 0;
19948 if (cp_parser_allow_gnu_extensions_p (parser))
19949 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19951 /* Check for the ptr-operator production. */
19952 cp_parser_parse_tentatively (parser);
19953 /* Parse the ptr-operator. */
19954 code = cp_parser_ptr_operator (parser,
19955 &class_type,
19956 &cv_quals,
19957 &std_attributes);
19959 /* If that worked, then we have a ptr-operator. */
19960 if (cp_parser_parse_definitely (parser))
19962 /* If a ptr-operator was found, then this declarator was not
19963 parenthesized. */
19964 if (parenthesized_p)
19965 *parenthesized_p = true;
19966 /* The dependent declarator is optional if we are parsing an
19967 abstract-declarator. */
19968 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19969 cp_parser_parse_tentatively (parser);
19971 /* Parse the dependent declarator. */
19972 declarator = cp_parser_declarator (parser, dcl_kind,
19973 /*ctor_dtor_or_conv_p=*/NULL,
19974 /*parenthesized_p=*/NULL,
19975 /*member_p=*/false,
19976 friend_p);
19978 /* If we are parsing an abstract-declarator, we must handle the
19979 case where the dependent declarator is absent. */
19980 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19981 && !cp_parser_parse_definitely (parser))
19982 declarator = NULL;
19984 declarator = cp_parser_make_indirect_declarator
19985 (code, class_type, cv_quals, declarator, std_attributes);
19987 /* Everything else is a direct-declarator. */
19988 else
19990 if (parenthesized_p)
19991 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19992 CPP_OPEN_PAREN);
19993 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19994 ctor_dtor_or_conv_p,
19995 member_p, friend_p);
19998 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19999 declarator->attributes = gnu_attributes;
20000 return declarator;
20003 /* Parse a direct-declarator or direct-abstract-declarator.
20005 direct-declarator:
20006 declarator-id
20007 direct-declarator ( parameter-declaration-clause )
20008 cv-qualifier-seq [opt]
20009 ref-qualifier [opt]
20010 exception-specification [opt]
20011 direct-declarator [ constant-expression [opt] ]
20012 ( declarator )
20014 direct-abstract-declarator:
20015 direct-abstract-declarator [opt]
20016 ( parameter-declaration-clause )
20017 cv-qualifier-seq [opt]
20018 ref-qualifier [opt]
20019 exception-specification [opt]
20020 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20021 ( abstract-declarator )
20023 Returns a representation of the declarator. DCL_KIND is
20024 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20025 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20026 we are parsing a direct-declarator. It is
20027 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20028 of ambiguity we prefer an abstract declarator, as per
20029 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20030 as for cp_parser_declarator. */
20032 static cp_declarator *
20033 cp_parser_direct_declarator (cp_parser* parser,
20034 cp_parser_declarator_kind dcl_kind,
20035 int* ctor_dtor_or_conv_p,
20036 bool member_p, bool friend_p)
20038 cp_token *token;
20039 cp_declarator *declarator = NULL;
20040 tree scope = NULL_TREE;
20041 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20042 bool saved_in_declarator_p = parser->in_declarator_p;
20043 bool first = true;
20044 tree pushed_scope = NULL_TREE;
20045 cp_token *open_paren = NULL, *close_paren = NULL;
20047 while (true)
20049 /* Peek at the next token. */
20050 token = cp_lexer_peek_token (parser->lexer);
20051 if (token->type == CPP_OPEN_PAREN)
20053 /* This is either a parameter-declaration-clause, or a
20054 parenthesized declarator. When we know we are parsing a
20055 named declarator, it must be a parenthesized declarator
20056 if FIRST is true. For instance, `(int)' is a
20057 parameter-declaration-clause, with an omitted
20058 direct-abstract-declarator. But `((*))', is a
20059 parenthesized abstract declarator. Finally, when T is a
20060 template parameter `(T)' is a
20061 parameter-declaration-clause, and not a parenthesized
20062 named declarator.
20064 We first try and parse a parameter-declaration-clause,
20065 and then try a nested declarator (if FIRST is true).
20067 It is not an error for it not to be a
20068 parameter-declaration-clause, even when FIRST is
20069 false. Consider,
20071 int i (int);
20072 int i (3);
20074 The first is the declaration of a function while the
20075 second is the definition of a variable, including its
20076 initializer.
20078 Having seen only the parenthesis, we cannot know which of
20079 these two alternatives should be selected. Even more
20080 complex are examples like:
20082 int i (int (a));
20083 int i (int (3));
20085 The former is a function-declaration; the latter is a
20086 variable initialization.
20088 Thus again, we try a parameter-declaration-clause, and if
20089 that fails, we back out and return. */
20091 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20093 tree params;
20094 bool is_declarator = false;
20096 open_paren = NULL;
20098 /* In a member-declarator, the only valid interpretation
20099 of a parenthesis is the start of a
20100 parameter-declaration-clause. (It is invalid to
20101 initialize a static data member with a parenthesized
20102 initializer; only the "=" form of initialization is
20103 permitted.) */
20104 if (!member_p)
20105 cp_parser_parse_tentatively (parser);
20107 /* Consume the `('. */
20108 matching_parens parens;
20109 parens.consume_open (parser);
20110 if (first)
20112 /* If this is going to be an abstract declarator, we're
20113 in a declarator and we can't have default args. */
20114 parser->default_arg_ok_p = false;
20115 parser->in_declarator_p = true;
20118 begin_scope (sk_function_parms, NULL_TREE);
20120 /* Parse the parameter-declaration-clause. */
20121 params = cp_parser_parameter_declaration_clause (parser);
20123 /* Consume the `)'. */
20124 parens.require_close (parser);
20126 /* If all went well, parse the cv-qualifier-seq,
20127 ref-qualifier and the exception-specification. */
20128 if (member_p || cp_parser_parse_definitely (parser))
20130 cp_cv_quals cv_quals;
20131 cp_virt_specifiers virt_specifiers;
20132 cp_ref_qualifier ref_qual;
20133 tree exception_specification;
20134 tree late_return;
20135 tree attrs;
20136 bool memfn = (member_p || (pushed_scope
20137 && CLASS_TYPE_P (pushed_scope)));
20139 is_declarator = true;
20141 if (ctor_dtor_or_conv_p)
20142 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20143 first = false;
20145 /* Parse the cv-qualifier-seq. */
20146 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20147 /* Parse the ref-qualifier. */
20148 ref_qual = cp_parser_ref_qualifier_opt (parser);
20149 /* Parse the tx-qualifier. */
20150 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20151 /* And the exception-specification. */
20152 exception_specification
20153 = cp_parser_exception_specification_opt (parser);
20155 attrs = cp_parser_std_attribute_spec_seq (parser);
20157 /* In here, we handle cases where attribute is used after
20158 the function declaration. For example:
20159 void func (int x) __attribute__((vector(..))); */
20160 tree gnu_attrs = NULL_TREE;
20161 tree requires_clause = NULL_TREE;
20162 late_return = (cp_parser_late_return_type_opt
20163 (parser, declarator, requires_clause,
20164 memfn ? cv_quals : -1));
20166 /* Parse the virt-specifier-seq. */
20167 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20169 /* Create the function-declarator. */
20170 declarator = make_call_declarator (declarator,
20171 params,
20172 cv_quals,
20173 virt_specifiers,
20174 ref_qual,
20175 tx_qual,
20176 exception_specification,
20177 late_return,
20178 requires_clause);
20179 declarator->std_attributes = attrs;
20180 declarator->attributes = gnu_attrs;
20181 /* Any subsequent parameter lists are to do with
20182 return type, so are not those of the declared
20183 function. */
20184 parser->default_arg_ok_p = false;
20187 /* Remove the function parms from scope. */
20188 pop_bindings_and_leave_scope ();
20190 if (is_declarator)
20191 /* Repeat the main loop. */
20192 continue;
20195 /* If this is the first, we can try a parenthesized
20196 declarator. */
20197 if (first)
20199 bool saved_in_type_id_in_expr_p;
20201 parser->default_arg_ok_p = saved_default_arg_ok_p;
20202 parser->in_declarator_p = saved_in_declarator_p;
20204 open_paren = token;
20205 /* Consume the `('. */
20206 matching_parens parens;
20207 parens.consume_open (parser);
20208 /* Parse the nested declarator. */
20209 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20210 parser->in_type_id_in_expr_p = true;
20211 declarator
20212 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20213 /*parenthesized_p=*/NULL,
20214 member_p, friend_p);
20215 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20216 first = false;
20217 /* Expect a `)'. */
20218 close_paren = cp_lexer_peek_token (parser->lexer);
20219 if (!parens.require_close (parser))
20220 declarator = cp_error_declarator;
20221 if (declarator == cp_error_declarator)
20222 break;
20224 goto handle_declarator;
20226 /* Otherwise, we must be done. */
20227 else
20228 break;
20230 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20231 && token->type == CPP_OPEN_SQUARE
20232 && !cp_next_tokens_can_be_attribute_p (parser))
20234 /* Parse an array-declarator. */
20235 tree bounds, attrs;
20237 if (ctor_dtor_or_conv_p)
20238 *ctor_dtor_or_conv_p = 0;
20240 open_paren = NULL;
20241 first = false;
20242 parser->default_arg_ok_p = false;
20243 parser->in_declarator_p = true;
20244 /* Consume the `['. */
20245 cp_lexer_consume_token (parser->lexer);
20246 /* Peek at the next token. */
20247 token = cp_lexer_peek_token (parser->lexer);
20248 /* If the next token is `]', then there is no
20249 constant-expression. */
20250 if (token->type != CPP_CLOSE_SQUARE)
20252 bool non_constant_p;
20253 bounds
20254 = cp_parser_constant_expression (parser,
20255 /*allow_non_constant=*/true,
20256 &non_constant_p);
20257 if (!non_constant_p)
20258 /* OK */;
20259 else if (error_operand_p (bounds))
20260 /* Already gave an error. */;
20261 else if (!parser->in_function_body
20262 || current_binding_level->kind == sk_function_parms)
20264 /* Normally, the array bound must be an integral constant
20265 expression. However, as an extension, we allow VLAs
20266 in function scopes as long as they aren't part of a
20267 parameter declaration. */
20268 cp_parser_error (parser,
20269 "array bound is not an integer constant");
20270 bounds = error_mark_node;
20272 else if (processing_template_decl
20273 && !type_dependent_expression_p (bounds))
20275 /* Remember this wasn't a constant-expression. */
20276 bounds = build_nop (TREE_TYPE (bounds), bounds);
20277 TREE_SIDE_EFFECTS (bounds) = 1;
20280 else
20281 bounds = NULL_TREE;
20282 /* Look for the closing `]'. */
20283 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20285 declarator = cp_error_declarator;
20286 break;
20289 attrs = cp_parser_std_attribute_spec_seq (parser);
20290 declarator = make_array_declarator (declarator, bounds);
20291 declarator->std_attributes = attrs;
20293 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20296 tree qualifying_scope;
20297 tree unqualified_name;
20298 tree attrs;
20299 special_function_kind sfk;
20300 bool abstract_ok;
20301 bool pack_expansion_p = false;
20302 cp_token *declarator_id_start_token;
20304 /* Parse a declarator-id */
20305 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20306 if (abstract_ok)
20308 cp_parser_parse_tentatively (parser);
20310 /* If we see an ellipsis, we should be looking at a
20311 parameter pack. */
20312 if (token->type == CPP_ELLIPSIS)
20314 /* Consume the `...' */
20315 cp_lexer_consume_token (parser->lexer);
20317 pack_expansion_p = true;
20321 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20322 unqualified_name
20323 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20324 qualifying_scope = parser->scope;
20325 if (abstract_ok)
20327 bool okay = false;
20329 if (!unqualified_name && pack_expansion_p)
20331 /* Check whether an error occurred. */
20332 okay = !cp_parser_error_occurred (parser);
20334 /* We already consumed the ellipsis to mark a
20335 parameter pack, but we have no way to report it,
20336 so abort the tentative parse. We will be exiting
20337 immediately anyway. */
20338 cp_parser_abort_tentative_parse (parser);
20340 else
20341 okay = cp_parser_parse_definitely (parser);
20343 if (!okay)
20344 unqualified_name = error_mark_node;
20345 else if (unqualified_name
20346 && (qualifying_scope
20347 || (!identifier_p (unqualified_name))))
20349 cp_parser_error (parser, "expected unqualified-id");
20350 unqualified_name = error_mark_node;
20354 if (!unqualified_name)
20355 return NULL;
20356 if (unqualified_name == error_mark_node)
20358 declarator = cp_error_declarator;
20359 pack_expansion_p = false;
20360 declarator->parameter_pack_p = false;
20361 break;
20364 attrs = cp_parser_std_attribute_spec_seq (parser);
20366 if (qualifying_scope && at_namespace_scope_p ()
20367 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20369 /* In the declaration of a member of a template class
20370 outside of the class itself, the SCOPE will sometimes
20371 be a TYPENAME_TYPE. For example, given:
20373 template <typename T>
20374 int S<T>::R::i = 3;
20376 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20377 this context, we must resolve S<T>::R to an ordinary
20378 type, rather than a typename type.
20380 The reason we normally avoid resolving TYPENAME_TYPEs
20381 is that a specialization of `S' might render
20382 `S<T>::R' not a type. However, if `S' is
20383 specialized, then this `i' will not be used, so there
20384 is no harm in resolving the types here. */
20385 tree type;
20387 /* Resolve the TYPENAME_TYPE. */
20388 type = resolve_typename_type (qualifying_scope,
20389 /*only_current_p=*/false);
20390 /* If that failed, the declarator is invalid. */
20391 if (TREE_CODE (type) == TYPENAME_TYPE)
20393 if (typedef_variant_p (type))
20394 error_at (declarator_id_start_token->location,
20395 "cannot define member of dependent typedef "
20396 "%qT", type);
20397 else
20398 error_at (declarator_id_start_token->location,
20399 "%<%T::%E%> is not a type",
20400 TYPE_CONTEXT (qualifying_scope),
20401 TYPE_IDENTIFIER (qualifying_scope));
20403 qualifying_scope = type;
20406 sfk = sfk_none;
20408 if (unqualified_name)
20410 tree class_type;
20412 if (qualifying_scope
20413 && CLASS_TYPE_P (qualifying_scope))
20414 class_type = qualifying_scope;
20415 else
20416 class_type = current_class_type;
20418 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20420 tree name_type = TREE_TYPE (unqualified_name);
20422 if (!class_type || !same_type_p (name_type, class_type))
20424 /* We do not attempt to print the declarator
20425 here because we do not have enough
20426 information about its original syntactic
20427 form. */
20428 cp_parser_error (parser, "invalid declarator");
20429 declarator = cp_error_declarator;
20430 break;
20432 else if (qualifying_scope
20433 && CLASSTYPE_USE_TEMPLATE (name_type))
20435 error_at (declarator_id_start_token->location,
20436 "invalid use of constructor as a template");
20437 inform (declarator_id_start_token->location,
20438 "use %<%T::%D%> instead of %<%T::%D%> to "
20439 "name the constructor in a qualified name",
20440 class_type,
20441 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20442 class_type, name_type);
20443 declarator = cp_error_declarator;
20444 break;
20446 unqualified_name = constructor_name (class_type);
20449 if (class_type)
20451 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20452 sfk = sfk_destructor;
20453 else if (identifier_p (unqualified_name)
20454 && IDENTIFIER_CONV_OP_P (unqualified_name))
20455 sfk = sfk_conversion;
20456 else if (/* There's no way to declare a constructor
20457 for an unnamed type, even if the type
20458 got a name for linkage purposes. */
20459 !TYPE_WAS_UNNAMED (class_type)
20460 /* Handle correctly (c++/19200):
20462 struct S {
20463 struct T{};
20464 friend void S(T);
20467 and also:
20469 namespace N {
20470 void S();
20473 struct S {
20474 friend void N::S();
20475 }; */
20476 && (!friend_p || class_type == qualifying_scope)
20477 && constructor_name_p (unqualified_name,
20478 class_type))
20479 sfk = sfk_constructor;
20480 else if (is_overloaded_fn (unqualified_name)
20481 && DECL_CONSTRUCTOR_P (get_first_fn
20482 (unqualified_name)))
20483 sfk = sfk_constructor;
20485 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20486 *ctor_dtor_or_conv_p = -1;
20489 declarator = make_id_declarator (qualifying_scope,
20490 unqualified_name,
20491 sfk);
20492 declarator->std_attributes = attrs;
20493 declarator->id_loc = token->location;
20494 declarator->parameter_pack_p = pack_expansion_p;
20496 if (pack_expansion_p)
20497 maybe_warn_variadic_templates ();
20500 handle_declarator:;
20501 scope = get_scope_of_declarator (declarator);
20502 if (scope)
20504 /* Any names that appear after the declarator-id for a
20505 member are looked up in the containing scope. */
20506 if (at_function_scope_p ())
20508 /* But declarations with qualified-ids can't appear in a
20509 function. */
20510 cp_parser_error (parser, "qualified-id in declaration");
20511 declarator = cp_error_declarator;
20512 break;
20514 pushed_scope = push_scope (scope);
20516 parser->in_declarator_p = true;
20517 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20518 || (declarator && declarator->kind == cdk_id))
20519 /* Default args are only allowed on function
20520 declarations. */
20521 parser->default_arg_ok_p = saved_default_arg_ok_p;
20522 else
20523 parser->default_arg_ok_p = false;
20525 first = false;
20527 /* We're done. */
20528 else
20529 break;
20532 /* For an abstract declarator, we might wind up with nothing at this
20533 point. That's an error; the declarator is not optional. */
20534 if (!declarator)
20535 cp_parser_error (parser, "expected declarator");
20536 else if (open_paren)
20538 /* Record overly parenthesized declarator so we can give a
20539 diagnostic about confusing decl/expr disambiguation. */
20540 if (declarator->kind == cdk_array)
20542 /* If the open and close parens are on different lines, this
20543 is probably a formatting thing, so ignore. */
20544 expanded_location open = expand_location (open_paren->location);
20545 expanded_location close = expand_location (close_paren->location);
20546 if (open.line != close.line || open.file != close.file)
20547 open_paren = NULL;
20549 if (open_paren)
20550 declarator->parenthesized = open_paren->location;
20553 /* If we entered a scope, we must exit it now. */
20554 if (pushed_scope)
20555 pop_scope (pushed_scope);
20557 parser->default_arg_ok_p = saved_default_arg_ok_p;
20558 parser->in_declarator_p = saved_in_declarator_p;
20560 return declarator;
20563 /* Parse a ptr-operator.
20565 ptr-operator:
20566 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20567 * cv-qualifier-seq [opt]
20569 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20570 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20572 GNU Extension:
20574 ptr-operator:
20575 & cv-qualifier-seq [opt]
20577 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20578 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20579 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20580 filled in with the TYPE containing the member. *CV_QUALS is
20581 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20582 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20583 Note that the tree codes returned by this function have nothing
20584 to do with the types of trees that will be eventually be created
20585 to represent the pointer or reference type being parsed. They are
20586 just constants with suggestive names. */
20587 static enum tree_code
20588 cp_parser_ptr_operator (cp_parser* parser,
20589 tree* type,
20590 cp_cv_quals *cv_quals,
20591 tree *attributes)
20593 enum tree_code code = ERROR_MARK;
20594 cp_token *token;
20595 tree attrs = NULL_TREE;
20597 /* Assume that it's not a pointer-to-member. */
20598 *type = NULL_TREE;
20599 /* And that there are no cv-qualifiers. */
20600 *cv_quals = TYPE_UNQUALIFIED;
20602 /* Peek at the next token. */
20603 token = cp_lexer_peek_token (parser->lexer);
20605 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20606 if (token->type == CPP_MULT)
20607 code = INDIRECT_REF;
20608 else if (token->type == CPP_AND)
20609 code = ADDR_EXPR;
20610 else if ((cxx_dialect != cxx98) &&
20611 token->type == CPP_AND_AND) /* C++0x only */
20612 code = NON_LVALUE_EXPR;
20614 if (code != ERROR_MARK)
20616 /* Consume the `*', `&' or `&&'. */
20617 cp_lexer_consume_token (parser->lexer);
20619 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20620 `&', if we are allowing GNU extensions. (The only qualifier
20621 that can legally appear after `&' is `restrict', but that is
20622 enforced during semantic analysis. */
20623 if (code == INDIRECT_REF
20624 || cp_parser_allow_gnu_extensions_p (parser))
20625 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20627 attrs = cp_parser_std_attribute_spec_seq (parser);
20628 if (attributes != NULL)
20629 *attributes = attrs;
20631 else
20633 /* Try the pointer-to-member case. */
20634 cp_parser_parse_tentatively (parser);
20635 /* Look for the optional `::' operator. */
20636 cp_parser_global_scope_opt (parser,
20637 /*current_scope_valid_p=*/false);
20638 /* Look for the nested-name specifier. */
20639 token = cp_lexer_peek_token (parser->lexer);
20640 cp_parser_nested_name_specifier (parser,
20641 /*typename_keyword_p=*/false,
20642 /*check_dependency_p=*/true,
20643 /*type_p=*/false,
20644 /*is_declaration=*/false);
20645 /* If we found it, and the next token is a `*', then we are
20646 indeed looking at a pointer-to-member operator. */
20647 if (!cp_parser_error_occurred (parser)
20648 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20650 /* Indicate that the `*' operator was used. */
20651 code = INDIRECT_REF;
20653 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20654 error_at (token->location, "%qD is a namespace", parser->scope);
20655 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20656 error_at (token->location, "cannot form pointer to member of "
20657 "non-class %q#T", parser->scope);
20658 else
20660 /* The type of which the member is a member is given by the
20661 current SCOPE. */
20662 *type = parser->scope;
20663 /* The next name will not be qualified. */
20664 parser->scope = NULL_TREE;
20665 parser->qualifying_scope = NULL_TREE;
20666 parser->object_scope = NULL_TREE;
20667 /* Look for optional c++11 attributes. */
20668 attrs = cp_parser_std_attribute_spec_seq (parser);
20669 if (attributes != NULL)
20670 *attributes = attrs;
20671 /* Look for the optional cv-qualifier-seq. */
20672 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20675 /* If that didn't work we don't have a ptr-operator. */
20676 if (!cp_parser_parse_definitely (parser))
20677 cp_parser_error (parser, "expected ptr-operator");
20680 return code;
20683 /* Parse an (optional) cv-qualifier-seq.
20685 cv-qualifier-seq:
20686 cv-qualifier cv-qualifier-seq [opt]
20688 cv-qualifier:
20689 const
20690 volatile
20692 GNU Extension:
20694 cv-qualifier:
20695 __restrict__
20697 Returns a bitmask representing the cv-qualifiers. */
20699 static cp_cv_quals
20700 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20702 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20704 while (true)
20706 cp_token *token;
20707 cp_cv_quals cv_qualifier;
20709 /* Peek at the next token. */
20710 token = cp_lexer_peek_token (parser->lexer);
20711 /* See if it's a cv-qualifier. */
20712 switch (token->keyword)
20714 case RID_CONST:
20715 cv_qualifier = TYPE_QUAL_CONST;
20716 break;
20718 case RID_VOLATILE:
20719 cv_qualifier = TYPE_QUAL_VOLATILE;
20720 break;
20722 case RID_RESTRICT:
20723 cv_qualifier = TYPE_QUAL_RESTRICT;
20724 break;
20726 default:
20727 cv_qualifier = TYPE_UNQUALIFIED;
20728 break;
20731 if (!cv_qualifier)
20732 break;
20734 if (cv_quals & cv_qualifier)
20736 gcc_rich_location richloc (token->location);
20737 richloc.add_fixit_remove ();
20738 error_at (&richloc, "duplicate cv-qualifier");
20739 cp_lexer_purge_token (parser->lexer);
20741 else
20743 cp_lexer_consume_token (parser->lexer);
20744 cv_quals |= cv_qualifier;
20748 return cv_quals;
20751 /* Parse an (optional) ref-qualifier
20753 ref-qualifier:
20757 Returns cp_ref_qualifier representing ref-qualifier. */
20759 static cp_ref_qualifier
20760 cp_parser_ref_qualifier_opt (cp_parser* parser)
20762 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20764 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20765 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20766 return ref_qual;
20768 while (true)
20770 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20771 cp_token *token = cp_lexer_peek_token (parser->lexer);
20773 switch (token->type)
20775 case CPP_AND:
20776 curr_ref_qual = REF_QUAL_LVALUE;
20777 break;
20779 case CPP_AND_AND:
20780 curr_ref_qual = REF_QUAL_RVALUE;
20781 break;
20783 default:
20784 curr_ref_qual = REF_QUAL_NONE;
20785 break;
20788 if (!curr_ref_qual)
20789 break;
20790 else if (ref_qual)
20792 error_at (token->location, "multiple ref-qualifiers");
20793 cp_lexer_purge_token (parser->lexer);
20795 else
20797 ref_qual = curr_ref_qual;
20798 cp_lexer_consume_token (parser->lexer);
20802 return ref_qual;
20805 /* Parse an optional tx-qualifier.
20807 tx-qualifier:
20808 transaction_safe
20809 transaction_safe_dynamic */
20811 static tree
20812 cp_parser_tx_qualifier_opt (cp_parser *parser)
20814 cp_token *token = cp_lexer_peek_token (parser->lexer);
20815 if (token->type == CPP_NAME)
20817 tree name = token->u.value;
20818 const char *p = IDENTIFIER_POINTER (name);
20819 const int len = strlen ("transaction_safe");
20820 if (!strncmp (p, "transaction_safe", len))
20822 p += len;
20823 if (*p == '\0'
20824 || !strcmp (p, "_dynamic"))
20826 cp_lexer_consume_token (parser->lexer);
20827 if (!flag_tm)
20829 error ("%qE requires %<-fgnu-tm%>", name);
20830 return NULL_TREE;
20832 else
20833 return name;
20837 return NULL_TREE;
20840 /* Parse an (optional) virt-specifier-seq.
20842 virt-specifier-seq:
20843 virt-specifier virt-specifier-seq [opt]
20845 virt-specifier:
20846 override
20847 final
20849 Returns a bitmask representing the virt-specifiers. */
20851 static cp_virt_specifiers
20852 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20854 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20856 while (true)
20858 cp_token *token;
20859 cp_virt_specifiers virt_specifier;
20861 /* Peek at the next token. */
20862 token = cp_lexer_peek_token (parser->lexer);
20863 /* See if it's a virt-specifier-qualifier. */
20864 if (token->type != CPP_NAME)
20865 break;
20866 if (id_equal (token->u.value, "override"))
20868 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20869 virt_specifier = VIRT_SPEC_OVERRIDE;
20871 else if (id_equal (token->u.value, "final"))
20873 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20874 virt_specifier = VIRT_SPEC_FINAL;
20876 else if (id_equal (token->u.value, "__final"))
20878 virt_specifier = VIRT_SPEC_FINAL;
20880 else
20881 break;
20883 if (virt_specifiers & virt_specifier)
20885 gcc_rich_location richloc (token->location);
20886 richloc.add_fixit_remove ();
20887 error_at (&richloc, "duplicate virt-specifier");
20888 cp_lexer_purge_token (parser->lexer);
20890 else
20892 cp_lexer_consume_token (parser->lexer);
20893 virt_specifiers |= virt_specifier;
20896 return virt_specifiers;
20899 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20900 is in scope even though it isn't real. */
20902 void
20903 inject_this_parameter (tree ctype, cp_cv_quals quals)
20905 tree this_parm;
20907 if (current_class_ptr)
20909 /* We don't clear this between NSDMIs. Is it already what we want? */
20910 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20911 if (DECL_P (current_class_ptr)
20912 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20913 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20914 && cp_type_quals (type) == quals)
20915 return;
20918 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20919 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20920 current_class_ptr = NULL_TREE;
20921 current_class_ref
20922 = cp_build_fold_indirect_ref (this_parm);
20923 current_class_ptr = this_parm;
20926 /* Return true iff our current scope is a non-static data member
20927 initializer. */
20929 bool
20930 parsing_nsdmi (void)
20932 /* We recognize NSDMI context by the context-less 'this' pointer set up
20933 by the function above. */
20934 if (current_class_ptr
20935 && TREE_CODE (current_class_ptr) == PARM_DECL
20936 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20937 return true;
20938 return false;
20941 /* Parse a late-specified return type, if any. This is not a separate
20942 non-terminal, but part of a function declarator, which looks like
20944 -> trailing-type-specifier-seq abstract-declarator(opt)
20946 Returns the type indicated by the type-id.
20948 In addition to this, parse any queued up #pragma omp declare simd
20949 clauses, and #pragma acc routine clauses.
20951 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20952 function. */
20954 static tree
20955 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20956 tree& requires_clause, cp_cv_quals quals)
20958 cp_token *token;
20959 tree type = NULL_TREE;
20960 bool declare_simd_p = (parser->omp_declare_simd
20961 && declarator
20962 && declarator->kind == cdk_id);
20964 bool oacc_routine_p = (parser->oacc_routine
20965 && declarator
20966 && declarator->kind == cdk_id);
20968 /* Peek at the next token. */
20969 token = cp_lexer_peek_token (parser->lexer);
20970 /* A late-specified return type is indicated by an initial '->'. */
20971 if (token->type != CPP_DEREF
20972 && token->keyword != RID_REQUIRES
20973 && !(token->type == CPP_NAME
20974 && token->u.value == ridpointers[RID_REQUIRES])
20975 && !(declare_simd_p || oacc_routine_p))
20976 return NULL_TREE;
20978 tree save_ccp = current_class_ptr;
20979 tree save_ccr = current_class_ref;
20980 if (quals >= 0)
20982 /* DR 1207: 'this' is in scope in the trailing return type. */
20983 inject_this_parameter (current_class_type, quals);
20986 if (token->type == CPP_DEREF)
20988 /* Consume the ->. */
20989 cp_lexer_consume_token (parser->lexer);
20991 type = cp_parser_trailing_type_id (parser);
20994 /* Function declarations may be followed by a trailing
20995 requires-clause. */
20996 requires_clause = cp_parser_requires_clause_opt (parser);
20998 if (declare_simd_p)
20999 declarator->attributes
21000 = cp_parser_late_parsing_omp_declare_simd (parser,
21001 declarator->attributes);
21002 if (oacc_routine_p)
21003 declarator->attributes
21004 = cp_parser_late_parsing_oacc_routine (parser,
21005 declarator->attributes);
21007 if (quals >= 0)
21009 current_class_ptr = save_ccp;
21010 current_class_ref = save_ccr;
21013 return type;
21016 /* Parse a declarator-id.
21018 declarator-id:
21019 id-expression
21020 :: [opt] nested-name-specifier [opt] type-name
21022 In the `id-expression' case, the value returned is as for
21023 cp_parser_id_expression if the id-expression was an unqualified-id.
21024 If the id-expression was a qualified-id, then a SCOPE_REF is
21025 returned. The first operand is the scope (either a NAMESPACE_DECL
21026 or TREE_TYPE), but the second is still just a representation of an
21027 unqualified-id. */
21029 static tree
21030 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21032 tree id;
21033 /* The expression must be an id-expression. Assume that qualified
21034 names are the names of types so that:
21036 template <class T>
21037 int S<T>::R::i = 3;
21039 will work; we must treat `S<T>::R' as the name of a type.
21040 Similarly, assume that qualified names are templates, where
21041 required, so that:
21043 template <class T>
21044 int S<T>::R<T>::i = 3;
21046 will work, too. */
21047 id = cp_parser_id_expression (parser,
21048 /*template_keyword_p=*/false,
21049 /*check_dependency_p=*/false,
21050 /*template_p=*/NULL,
21051 /*declarator_p=*/true,
21052 optional_p);
21053 if (id && BASELINK_P (id))
21054 id = BASELINK_FUNCTIONS (id);
21055 return id;
21058 /* Parse a type-id.
21060 type-id:
21061 type-specifier-seq abstract-declarator [opt]
21063 Returns the TYPE specified. */
21065 static tree
21066 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21067 bool is_trailing_return)
21069 cp_decl_specifier_seq type_specifier_seq;
21070 cp_declarator *abstract_declarator;
21072 /* Parse the type-specifier-seq. */
21073 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21074 is_trailing_return,
21075 &type_specifier_seq);
21076 if (is_template_arg && type_specifier_seq.type
21077 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21078 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21079 /* A bare template name as a template argument is a template template
21080 argument, not a placeholder, so fail parsing it as a type argument. */
21082 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21083 cp_parser_simulate_error (parser);
21084 return error_mark_node;
21086 if (type_specifier_seq.type == error_mark_node)
21087 return error_mark_node;
21089 /* There might or might not be an abstract declarator. */
21090 cp_parser_parse_tentatively (parser);
21091 /* Look for the declarator. */
21092 abstract_declarator
21093 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21094 /*parenthesized_p=*/NULL,
21095 /*member_p=*/false,
21096 /*friend_p=*/false);
21097 /* Check to see if there really was a declarator. */
21098 if (!cp_parser_parse_definitely (parser))
21099 abstract_declarator = NULL;
21101 if (type_specifier_seq.type
21102 /* The concepts TS allows 'auto' as a type-id. */
21103 && (!flag_concepts || parser->in_type_id_in_expr_p)
21104 /* None of the valid uses of 'auto' in C++14 involve the type-id
21105 nonterminal, but it is valid in a trailing-return-type. */
21106 && !(cxx_dialect >= cxx14 && is_trailing_return))
21107 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21109 /* A type-id with type 'auto' is only ok if the abstract declarator
21110 is a function declarator with a late-specified return type.
21112 A type-id with 'auto' is also valid in a trailing-return-type
21113 in a compound-requirement. */
21114 if (abstract_declarator
21115 && abstract_declarator->kind == cdk_function
21116 && abstract_declarator->u.function.late_return_type)
21117 /* OK */;
21118 else if (parser->in_result_type_constraint_p)
21119 /* OK */;
21120 else
21122 location_t loc = type_specifier_seq.locations[ds_type_spec];
21123 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21125 error_at (loc, "missing template arguments after %qT",
21126 auto_node);
21127 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21128 tmpl);
21130 else
21131 error_at (loc, "invalid use of %qT", auto_node);
21132 return error_mark_node;
21136 return groktypename (&type_specifier_seq, abstract_declarator,
21137 is_template_arg);
21140 static tree
21141 cp_parser_type_id (cp_parser *parser)
21143 return cp_parser_type_id_1 (parser, false, false);
21146 static tree
21147 cp_parser_template_type_arg (cp_parser *parser)
21149 tree r;
21150 const char *saved_message = parser->type_definition_forbidden_message;
21151 parser->type_definition_forbidden_message
21152 = G_("types may not be defined in template arguments");
21153 r = cp_parser_type_id_1 (parser, true, false);
21154 parser->type_definition_forbidden_message = saved_message;
21155 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21157 error ("invalid use of %<auto%> in template argument");
21158 r = error_mark_node;
21160 return r;
21163 static tree
21164 cp_parser_trailing_type_id (cp_parser *parser)
21166 return cp_parser_type_id_1 (parser, false, true);
21169 /* Parse a type-specifier-seq.
21171 type-specifier-seq:
21172 type-specifier type-specifier-seq [opt]
21174 GNU extension:
21176 type-specifier-seq:
21177 attributes type-specifier-seq [opt]
21179 If IS_DECLARATION is true, we are at the start of a "condition" or
21180 exception-declaration, so we might be followed by a declarator-id.
21182 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21183 i.e. we've just seen "->".
21185 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21187 static void
21188 cp_parser_type_specifier_seq (cp_parser* parser,
21189 bool is_declaration,
21190 bool is_trailing_return,
21191 cp_decl_specifier_seq *type_specifier_seq)
21193 bool seen_type_specifier = false;
21194 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21195 cp_token *start_token = NULL;
21197 /* Clear the TYPE_SPECIFIER_SEQ. */
21198 clear_decl_specs (type_specifier_seq);
21200 /* In the context of a trailing return type, enum E { } is an
21201 elaborated-type-specifier followed by a function-body, not an
21202 enum-specifier. */
21203 if (is_trailing_return)
21204 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21206 /* Parse the type-specifiers and attributes. */
21207 while (true)
21209 tree type_specifier;
21210 bool is_cv_qualifier;
21212 /* Check for attributes first. */
21213 if (cp_next_tokens_can_be_attribute_p (parser))
21215 type_specifier_seq->attributes
21216 = attr_chainon (type_specifier_seq->attributes,
21217 cp_parser_attributes_opt (parser));
21218 continue;
21221 /* record the token of the beginning of the type specifier seq,
21222 for error reporting purposes*/
21223 if (!start_token)
21224 start_token = cp_lexer_peek_token (parser->lexer);
21226 /* Look for the type-specifier. */
21227 type_specifier = cp_parser_type_specifier (parser,
21228 flags,
21229 type_specifier_seq,
21230 /*is_declaration=*/false,
21231 NULL,
21232 &is_cv_qualifier);
21233 if (!type_specifier)
21235 /* If the first type-specifier could not be found, this is not a
21236 type-specifier-seq at all. */
21237 if (!seen_type_specifier)
21239 /* Set in_declarator_p to avoid skipping to the semicolon. */
21240 int in_decl = parser->in_declarator_p;
21241 parser->in_declarator_p = true;
21243 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21244 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21245 cp_parser_error (parser, "expected type-specifier");
21247 parser->in_declarator_p = in_decl;
21249 type_specifier_seq->type = error_mark_node;
21250 return;
21252 /* If subsequent type-specifiers could not be found, the
21253 type-specifier-seq is complete. */
21254 break;
21257 seen_type_specifier = true;
21258 /* The standard says that a condition can be:
21260 type-specifier-seq declarator = assignment-expression
21262 However, given:
21264 struct S {};
21265 if (int S = ...)
21267 we should treat the "S" as a declarator, not as a
21268 type-specifier. The standard doesn't say that explicitly for
21269 type-specifier-seq, but it does say that for
21270 decl-specifier-seq in an ordinary declaration. Perhaps it
21271 would be clearer just to allow a decl-specifier-seq here, and
21272 then add a semantic restriction that if any decl-specifiers
21273 that are not type-specifiers appear, the program is invalid. */
21274 if (is_declaration && !is_cv_qualifier)
21275 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21279 /* Return whether the function currently being declared has an associated
21280 template parameter list. */
21282 static bool
21283 function_being_declared_is_template_p (cp_parser* parser)
21285 if (!current_template_parms || processing_template_parmlist)
21286 return false;
21288 if (parser->implicit_template_scope)
21289 return true;
21291 if (at_class_scope_p ()
21292 && TYPE_BEING_DEFINED (current_class_type))
21293 return parser->num_template_parameter_lists != 0;
21295 return ((int) parser->num_template_parameter_lists > template_class_depth
21296 (current_class_type));
21299 /* Parse a parameter-declaration-clause.
21301 parameter-declaration-clause:
21302 parameter-declaration-list [opt] ... [opt]
21303 parameter-declaration-list , ...
21305 Returns a representation for the parameter declarations. A return
21306 value of NULL indicates a parameter-declaration-clause consisting
21307 only of an ellipsis. */
21309 static tree
21310 cp_parser_parameter_declaration_clause (cp_parser* parser)
21312 tree parameters;
21313 cp_token *token;
21314 bool ellipsis_p;
21316 temp_override<bool> cleanup
21317 (parser->auto_is_implicit_function_template_parm_p);
21319 if (!processing_specialization
21320 && !processing_template_parmlist
21321 && !processing_explicit_instantiation
21322 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21323 actual function or a random abstract declarator. */
21324 && parser->default_arg_ok_p)
21325 if (!current_function_decl
21326 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21327 parser->auto_is_implicit_function_template_parm_p = true;
21329 /* Peek at the next token. */
21330 token = cp_lexer_peek_token (parser->lexer);
21331 /* Check for trivial parameter-declaration-clauses. */
21332 if (token->type == CPP_ELLIPSIS)
21334 /* Consume the `...' token. */
21335 cp_lexer_consume_token (parser->lexer);
21336 return NULL_TREE;
21338 else if (token->type == CPP_CLOSE_PAREN)
21339 /* There are no parameters. */
21341 #ifndef NO_IMPLICIT_EXTERN_C
21342 if (in_system_header_at (input_location)
21343 && current_class_type == NULL
21344 && current_lang_name == lang_name_c)
21345 return NULL_TREE;
21346 else
21347 #endif
21348 return void_list_node;
21350 /* Check for `(void)', too, which is a special case. */
21351 else if (token->keyword == RID_VOID
21352 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21353 == CPP_CLOSE_PAREN))
21355 /* Consume the `void' token. */
21356 cp_lexer_consume_token (parser->lexer);
21357 /* There are no parameters. */
21358 return void_list_node;
21361 /* Parse the parameter-declaration-list. */
21362 parameters = cp_parser_parameter_declaration_list (parser);
21363 /* If a parse error occurred while parsing the
21364 parameter-declaration-list, then the entire
21365 parameter-declaration-clause is erroneous. */
21366 if (parameters == error_mark_node)
21367 return NULL_TREE;
21369 /* Peek at the next token. */
21370 token = cp_lexer_peek_token (parser->lexer);
21371 /* If it's a `,', the clause should terminate with an ellipsis. */
21372 if (token->type == CPP_COMMA)
21374 /* Consume the `,'. */
21375 cp_lexer_consume_token (parser->lexer);
21376 /* Expect an ellipsis. */
21377 ellipsis_p
21378 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21380 /* It might also be `...' if the optional trailing `,' was
21381 omitted. */
21382 else if (token->type == CPP_ELLIPSIS)
21384 /* Consume the `...' token. */
21385 cp_lexer_consume_token (parser->lexer);
21386 /* And remember that we saw it. */
21387 ellipsis_p = true;
21389 else
21390 ellipsis_p = false;
21392 /* Finish the parameter list. */
21393 if (!ellipsis_p)
21394 parameters = chainon (parameters, void_list_node);
21396 return parameters;
21399 /* Parse a parameter-declaration-list.
21401 parameter-declaration-list:
21402 parameter-declaration
21403 parameter-declaration-list , parameter-declaration
21405 Returns a representation of the parameter-declaration-list, as for
21406 cp_parser_parameter_declaration_clause. However, the
21407 `void_list_node' is never appended to the list. */
21409 static tree
21410 cp_parser_parameter_declaration_list (cp_parser* parser)
21412 tree parameters = NULL_TREE;
21413 tree *tail = &parameters;
21414 bool saved_in_unbraced_linkage_specification_p;
21415 int index = 0;
21417 /* The special considerations that apply to a function within an
21418 unbraced linkage specifications do not apply to the parameters
21419 to the function. */
21420 saved_in_unbraced_linkage_specification_p
21421 = parser->in_unbraced_linkage_specification_p;
21422 parser->in_unbraced_linkage_specification_p = false;
21424 /* Look for more parameters. */
21425 while (true)
21427 cp_parameter_declarator *parameter;
21428 tree decl = error_mark_node;
21429 bool parenthesized_p = false;
21431 /* Parse the parameter. */
21432 parameter
21433 = cp_parser_parameter_declaration (parser,
21434 /*template_parm_p=*/false,
21435 &parenthesized_p);
21437 /* We don't know yet if the enclosing context is deprecated, so wait
21438 and warn in grokparms if appropriate. */
21439 deprecated_state = DEPRECATED_SUPPRESS;
21441 if (parameter)
21443 decl = grokdeclarator (parameter->declarator,
21444 &parameter->decl_specifiers,
21445 PARM,
21446 parameter->default_argument != NULL_TREE,
21447 &parameter->decl_specifiers.attributes);
21448 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21449 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21452 deprecated_state = DEPRECATED_NORMAL;
21454 /* If a parse error occurred parsing the parameter declaration,
21455 then the entire parameter-declaration-list is erroneous. */
21456 if (decl == error_mark_node)
21458 parameters = error_mark_node;
21459 break;
21462 if (parameter->decl_specifiers.attributes)
21463 cplus_decl_attributes (&decl,
21464 parameter->decl_specifiers.attributes,
21466 if (DECL_NAME (decl))
21467 decl = pushdecl (decl);
21469 if (decl != error_mark_node)
21471 retrofit_lang_decl (decl);
21472 DECL_PARM_INDEX (decl) = ++index;
21473 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21476 /* Add the new parameter to the list. */
21477 *tail = build_tree_list (parameter->default_argument, decl);
21478 tail = &TREE_CHAIN (*tail);
21480 /* Peek at the next token. */
21481 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21482 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21483 /* These are for Objective-C++ */
21484 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21485 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21486 /* The parameter-declaration-list is complete. */
21487 break;
21488 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21490 cp_token *token;
21492 /* Peek at the next token. */
21493 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21494 /* If it's an ellipsis, then the list is complete. */
21495 if (token->type == CPP_ELLIPSIS)
21496 break;
21497 /* Otherwise, there must be more parameters. Consume the
21498 `,'. */
21499 cp_lexer_consume_token (parser->lexer);
21500 /* When parsing something like:
21502 int i(float f, double d)
21504 we can tell after seeing the declaration for "f" that we
21505 are not looking at an initialization of a variable "i",
21506 but rather at the declaration of a function "i".
21508 Due to the fact that the parsing of template arguments
21509 (as specified to a template-id) requires backtracking we
21510 cannot use this technique when inside a template argument
21511 list. */
21512 if (!parser->in_template_argument_list_p
21513 && !parser->in_type_id_in_expr_p
21514 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21515 /* However, a parameter-declaration of the form
21516 "float(f)" (which is a valid declaration of a
21517 parameter "f") can also be interpreted as an
21518 expression (the conversion of "f" to "float"). */
21519 && !parenthesized_p)
21520 cp_parser_commit_to_tentative_parse (parser);
21522 else
21524 cp_parser_error (parser, "expected %<,%> or %<...%>");
21525 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21526 cp_parser_skip_to_closing_parenthesis (parser,
21527 /*recovering=*/true,
21528 /*or_comma=*/false,
21529 /*consume_paren=*/false);
21530 break;
21534 parser->in_unbraced_linkage_specification_p
21535 = saved_in_unbraced_linkage_specification_p;
21537 /* Reset implicit_template_scope if we are about to leave the function
21538 parameter list that introduced it. Note that for out-of-line member
21539 definitions, there will be one or more class scopes before we get to
21540 the template parameter scope. */
21542 if (cp_binding_level *its = parser->implicit_template_scope)
21543 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21545 while (maybe_its->kind == sk_class)
21546 maybe_its = maybe_its->level_chain;
21547 if (maybe_its == its)
21549 parser->implicit_template_parms = 0;
21550 parser->implicit_template_scope = 0;
21554 return parameters;
21557 /* Parse a parameter declaration.
21559 parameter-declaration:
21560 decl-specifier-seq ... [opt] declarator
21561 decl-specifier-seq declarator = assignment-expression
21562 decl-specifier-seq ... [opt] abstract-declarator [opt]
21563 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21565 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21566 declares a template parameter. (In that case, a non-nested `>'
21567 token encountered during the parsing of the assignment-expression
21568 is not interpreted as a greater-than operator.)
21570 Returns a representation of the parameter, or NULL if an error
21571 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21572 true iff the declarator is of the form "(p)". */
21574 static cp_parameter_declarator *
21575 cp_parser_parameter_declaration (cp_parser *parser,
21576 bool template_parm_p,
21577 bool *parenthesized_p)
21579 int declares_class_or_enum;
21580 cp_decl_specifier_seq decl_specifiers;
21581 cp_declarator *declarator;
21582 tree default_argument;
21583 cp_token *token = NULL, *declarator_token_start = NULL;
21584 const char *saved_message;
21585 bool template_parameter_pack_p = false;
21587 /* In a template parameter, `>' is not an operator.
21589 [temp.param]
21591 When parsing a default template-argument for a non-type
21592 template-parameter, the first non-nested `>' is taken as the end
21593 of the template parameter-list rather than a greater-than
21594 operator. */
21596 /* Type definitions may not appear in parameter types. */
21597 saved_message = parser->type_definition_forbidden_message;
21598 parser->type_definition_forbidden_message
21599 = G_("types may not be defined in parameter types");
21601 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21602 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21603 (current_template_parms)) : 0);
21605 /* Parse the declaration-specifiers. */
21606 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21607 cp_parser_decl_specifier_seq (parser,
21608 CP_PARSER_FLAGS_NONE,
21609 &decl_specifiers,
21610 &declares_class_or_enum);
21612 /* Complain about missing 'typename' or other invalid type names. */
21613 if (!decl_specifiers.any_type_specifiers_p
21614 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21615 decl_specifiers.type = error_mark_node;
21617 /* If an error occurred, there's no reason to attempt to parse the
21618 rest of the declaration. */
21619 if (cp_parser_error_occurred (parser))
21621 parser->type_definition_forbidden_message = saved_message;
21622 return NULL;
21625 /* Peek at the next token. */
21626 token = cp_lexer_peek_token (parser->lexer);
21628 /* If the next token is a `)', `,', `=', `>', or `...', then there
21629 is no declarator. However, when variadic templates are enabled,
21630 there may be a declarator following `...'. */
21631 if (token->type == CPP_CLOSE_PAREN
21632 || token->type == CPP_COMMA
21633 || token->type == CPP_EQ
21634 || token->type == CPP_GREATER)
21636 declarator = NULL;
21637 if (parenthesized_p)
21638 *parenthesized_p = false;
21640 /* Otherwise, there should be a declarator. */
21641 else
21643 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21644 parser->default_arg_ok_p = false;
21646 /* After seeing a decl-specifier-seq, if the next token is not a
21647 "(", there is no possibility that the code is a valid
21648 expression. Therefore, if parsing tentatively, we commit at
21649 this point. */
21650 if (!parser->in_template_argument_list_p
21651 /* In an expression context, having seen:
21653 (int((char ...
21655 we cannot be sure whether we are looking at a
21656 function-type (taking a "char" as a parameter) or a cast
21657 of some object of type "char" to "int". */
21658 && !parser->in_type_id_in_expr_p
21659 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21660 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21661 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21662 cp_parser_commit_to_tentative_parse (parser);
21663 /* Parse the declarator. */
21664 declarator_token_start = token;
21665 declarator = cp_parser_declarator (parser,
21666 CP_PARSER_DECLARATOR_EITHER,
21667 /*ctor_dtor_or_conv_p=*/NULL,
21668 parenthesized_p,
21669 /*member_p=*/false,
21670 /*friend_p=*/false);
21671 parser->default_arg_ok_p = saved_default_arg_ok_p;
21672 /* After the declarator, allow more attributes. */
21673 decl_specifiers.attributes
21674 = attr_chainon (decl_specifiers.attributes,
21675 cp_parser_attributes_opt (parser));
21677 /* If the declarator is a template parameter pack, remember that and
21678 clear the flag in the declarator itself so we don't get errors
21679 from grokdeclarator. */
21680 if (template_parm_p && declarator && declarator->parameter_pack_p)
21682 declarator->parameter_pack_p = false;
21683 template_parameter_pack_p = true;
21687 /* If the next token is an ellipsis, and we have not seen a declarator
21688 name, and if either the type of the declarator contains parameter
21689 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21690 for, eg, abbreviated integral type names), then we actually have a
21691 parameter pack expansion expression. Otherwise, leave the ellipsis
21692 for a C-style variadic function. */
21693 token = cp_lexer_peek_token (parser->lexer);
21695 /* If a function parameter pack was specified and an implicit template
21696 parameter was introduced during cp_parser_parameter_declaration,
21697 change any implicit parameters introduced into packs. */
21698 if (parser->implicit_template_parms
21699 && (token->type == CPP_ELLIPSIS
21700 || (declarator && declarator->parameter_pack_p)))
21702 int latest_template_parm_idx = TREE_VEC_LENGTH
21703 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21705 if (latest_template_parm_idx != template_parm_idx)
21706 decl_specifiers.type = convert_generic_types_to_packs
21707 (decl_specifiers.type,
21708 template_parm_idx, latest_template_parm_idx);
21711 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21713 tree type = decl_specifiers.type;
21715 if (type && DECL_P (type))
21716 type = TREE_TYPE (type);
21718 if (((type
21719 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21720 && (template_parm_p || uses_parameter_packs (type)))
21721 || (!type && template_parm_p))
21722 && declarator_can_be_parameter_pack (declarator))
21724 /* Consume the `...'. */
21725 cp_lexer_consume_token (parser->lexer);
21726 maybe_warn_variadic_templates ();
21728 /* Build a pack expansion type */
21729 if (template_parm_p)
21730 template_parameter_pack_p = true;
21731 else if (declarator)
21732 declarator->parameter_pack_p = true;
21733 else
21734 decl_specifiers.type = make_pack_expansion (type);
21738 /* The restriction on defining new types applies only to the type
21739 of the parameter, not to the default argument. */
21740 parser->type_definition_forbidden_message = saved_message;
21742 /* If the next token is `=', then process a default argument. */
21743 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21745 tree type = decl_specifiers.type;
21746 token = cp_lexer_peek_token (parser->lexer);
21747 /* If we are defining a class, then the tokens that make up the
21748 default argument must be saved and processed later. */
21749 if (!template_parm_p && at_class_scope_p ()
21750 && TYPE_BEING_DEFINED (current_class_type)
21751 && !LAMBDA_TYPE_P (current_class_type))
21752 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21754 // A constrained-type-specifier may declare a type template-parameter.
21755 else if (declares_constrained_type_template_parameter (type))
21756 default_argument
21757 = cp_parser_default_type_template_argument (parser);
21759 // A constrained-type-specifier may declare a template-template-parameter.
21760 else if (declares_constrained_template_template_parameter (type))
21761 default_argument
21762 = cp_parser_default_template_template_argument (parser);
21764 /* Outside of a class definition, we can just parse the
21765 assignment-expression. */
21766 else
21767 default_argument
21768 = cp_parser_default_argument (parser, template_parm_p);
21770 if (!parser->default_arg_ok_p)
21772 permerror (token->location,
21773 "default arguments are only "
21774 "permitted for function parameters");
21776 else if ((declarator && declarator->parameter_pack_p)
21777 || template_parameter_pack_p
21778 || (decl_specifiers.type
21779 && PACK_EXPANSION_P (decl_specifiers.type)))
21781 /* Find the name of the parameter pack. */
21782 cp_declarator *id_declarator = declarator;
21783 while (id_declarator && id_declarator->kind != cdk_id)
21784 id_declarator = id_declarator->declarator;
21786 if (id_declarator && id_declarator->kind == cdk_id)
21787 error_at (declarator_token_start->location,
21788 template_parm_p
21789 ? G_("template parameter pack %qD "
21790 "cannot have a default argument")
21791 : G_("parameter pack %qD cannot have "
21792 "a default argument"),
21793 id_declarator->u.id.unqualified_name);
21794 else
21795 error_at (declarator_token_start->location,
21796 template_parm_p
21797 ? G_("template parameter pack cannot have "
21798 "a default argument")
21799 : G_("parameter pack cannot have a "
21800 "default argument"));
21802 default_argument = NULL_TREE;
21805 else
21806 default_argument = NULL_TREE;
21808 /* Generate a location for the parameter, ranging from the start of the
21809 initial token to the end of the final token (using input_location for
21810 the latter, set up by cp_lexer_set_source_position_from_token when
21811 consuming tokens).
21813 If we have a identifier, then use it for the caret location, e.g.
21815 extern int callee (int one, int (*two)(int, int), float three);
21816 ~~~~~~^~~~~~~~~~~~~~
21818 otherwise, reuse the start location for the caret location e.g.:
21820 extern int callee (int one, int (*)(int, int), float three);
21821 ^~~~~~~~~~~~~~~~~
21824 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21825 ? declarator->id_loc
21826 : decl_spec_token_start->location);
21827 location_t param_loc = make_location (caret_loc,
21828 decl_spec_token_start->location,
21829 input_location);
21831 return make_parameter_declarator (&decl_specifiers,
21832 declarator,
21833 default_argument,
21834 param_loc,
21835 template_parameter_pack_p);
21838 /* Parse a default argument and return it.
21840 TEMPLATE_PARM_P is true if this is a default argument for a
21841 non-type template parameter. */
21842 static tree
21843 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21845 tree default_argument = NULL_TREE;
21846 bool saved_greater_than_is_operator_p;
21847 bool saved_local_variables_forbidden_p;
21848 bool non_constant_p, is_direct_init;
21850 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21851 set correctly. */
21852 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21853 parser->greater_than_is_operator_p = !template_parm_p;
21854 /* Local variable names (and the `this' keyword) may not
21855 appear in a default argument. */
21856 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21857 parser->local_variables_forbidden_p = true;
21858 /* Parse the assignment-expression. */
21859 if (template_parm_p)
21860 push_deferring_access_checks (dk_no_deferred);
21861 tree saved_class_ptr = NULL_TREE;
21862 tree saved_class_ref = NULL_TREE;
21863 /* The "this" pointer is not valid in a default argument. */
21864 if (cfun)
21866 saved_class_ptr = current_class_ptr;
21867 cp_function_chain->x_current_class_ptr = NULL_TREE;
21868 saved_class_ref = current_class_ref;
21869 cp_function_chain->x_current_class_ref = NULL_TREE;
21871 default_argument
21872 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21873 /* Restore the "this" pointer. */
21874 if (cfun)
21876 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21877 cp_function_chain->x_current_class_ref = saved_class_ref;
21879 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21880 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21881 if (template_parm_p)
21882 pop_deferring_access_checks ();
21883 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21884 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21886 return default_argument;
21889 /* Parse a function-body.
21891 function-body:
21892 compound_statement */
21894 static void
21895 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21897 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21898 ? BCS_TRY_BLOCK : BCS_NORMAL),
21899 true);
21902 /* Parse a ctor-initializer-opt followed by a function-body. Return
21903 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21904 is true we are parsing a function-try-block. */
21906 static void
21907 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21908 bool in_function_try_block)
21910 tree body, list;
21911 const bool check_body_p =
21912 DECL_CONSTRUCTOR_P (current_function_decl)
21913 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21914 tree last = NULL;
21916 /* Begin the function body. */
21917 body = begin_function_body ();
21918 /* Parse the optional ctor-initializer. */
21919 cp_parser_ctor_initializer_opt (parser);
21921 /* If we're parsing a constexpr constructor definition, we need
21922 to check that the constructor body is indeed empty. However,
21923 before we get to cp_parser_function_body lot of junk has been
21924 generated, so we can't just check that we have an empty block.
21925 Rather we take a snapshot of the outermost block, and check whether
21926 cp_parser_function_body changed its state. */
21927 if (check_body_p)
21929 list = cur_stmt_list;
21930 if (STATEMENT_LIST_TAIL (list))
21931 last = STATEMENT_LIST_TAIL (list)->stmt;
21933 /* Parse the function-body. */
21934 cp_parser_function_body (parser, in_function_try_block);
21935 if (check_body_p)
21936 check_constexpr_ctor_body (last, list, /*complain=*/true);
21937 /* Finish the function body. */
21938 finish_function_body (body);
21941 /* Parse an initializer.
21943 initializer:
21944 = initializer-clause
21945 ( expression-list )
21947 Returns an expression representing the initializer. If no
21948 initializer is present, NULL_TREE is returned.
21950 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21951 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21952 set to TRUE if there is no initializer present. If there is an
21953 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21954 is set to true; otherwise it is set to false. */
21956 static tree
21957 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21958 bool* non_constant_p, bool subexpression_p)
21960 cp_token *token;
21961 tree init;
21963 /* Peek at the next token. */
21964 token = cp_lexer_peek_token (parser->lexer);
21966 /* Let our caller know whether or not this initializer was
21967 parenthesized. */
21968 *is_direct_init = (token->type != CPP_EQ);
21969 /* Assume that the initializer is constant. */
21970 *non_constant_p = false;
21972 if (token->type == CPP_EQ)
21974 /* Consume the `='. */
21975 cp_lexer_consume_token (parser->lexer);
21976 /* Parse the initializer-clause. */
21977 init = cp_parser_initializer_clause (parser, non_constant_p);
21979 else if (token->type == CPP_OPEN_PAREN)
21981 vec<tree, va_gc> *vec;
21982 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21983 /*cast_p=*/false,
21984 /*allow_expansion_p=*/true,
21985 non_constant_p);
21986 if (vec == NULL)
21987 return error_mark_node;
21988 init = build_tree_list_vec (vec);
21989 release_tree_vector (vec);
21991 else if (token->type == CPP_OPEN_BRACE)
21993 cp_lexer_set_source_position (parser->lexer);
21994 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21995 init = cp_parser_braced_list (parser, non_constant_p);
21996 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21998 else
22000 /* Anything else is an error. */
22001 cp_parser_error (parser, "expected initializer");
22002 init = error_mark_node;
22005 if (!subexpression_p && check_for_bare_parameter_packs (init))
22006 init = error_mark_node;
22008 return init;
22011 /* Parse an initializer-clause.
22013 initializer-clause:
22014 assignment-expression
22015 braced-init-list
22017 Returns an expression representing the initializer.
22019 If the `assignment-expression' production is used the value
22020 returned is simply a representation for the expression.
22022 Otherwise, calls cp_parser_braced_list. */
22024 static cp_expr
22025 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22027 cp_expr initializer;
22029 /* Assume the expression is constant. */
22030 *non_constant_p = false;
22032 /* If it is not a `{', then we are looking at an
22033 assignment-expression. */
22034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22036 initializer
22037 = cp_parser_constant_expression (parser,
22038 /*allow_non_constant_p=*/true,
22039 non_constant_p);
22041 else
22042 initializer = cp_parser_braced_list (parser, non_constant_p);
22044 return initializer;
22047 /* Parse a brace-enclosed initializer list.
22049 braced-init-list:
22050 { initializer-list , [opt] }
22051 { designated-initializer-list , [opt] }
22054 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22055 the elements of the initializer-list (or NULL, if the last
22056 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22057 NULL_TREE. There is no way to detect whether or not the optional
22058 trailing `,' was provided. NON_CONSTANT_P is as for
22059 cp_parser_initializer. */
22061 static cp_expr
22062 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22064 tree initializer;
22065 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22067 /* Consume the `{' token. */
22068 matching_braces braces;
22069 braces.require_open (parser);
22070 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22071 initializer = make_node (CONSTRUCTOR);
22072 /* If it's not a `}', then there is a non-trivial initializer. */
22073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22075 /* Parse the initializer list. */
22076 CONSTRUCTOR_ELTS (initializer)
22077 = cp_parser_initializer_list (parser, non_constant_p);
22078 /* A trailing `,' token is allowed. */
22079 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22080 cp_lexer_consume_token (parser->lexer);
22082 else
22083 *non_constant_p = false;
22084 /* Now, there should be a trailing `}'. */
22085 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22086 braces.require_close (parser);
22087 TREE_TYPE (initializer) = init_list_type_node;
22089 cp_expr result (initializer);
22090 /* Build a location of the form:
22091 { ... }
22092 ^~~~~~~
22093 with caret==start at the open brace, finish at the close brace. */
22094 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22095 result.set_location (combined_loc);
22096 return result;
22099 /* Consume tokens up to, and including, the next non-nested closing `]'.
22100 Returns true iff we found a closing `]'. */
22102 static bool
22103 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22105 unsigned square_depth = 0;
22107 while (true)
22109 cp_token * token = cp_lexer_peek_token (parser->lexer);
22111 switch (token->type)
22113 case CPP_EOF:
22114 case CPP_PRAGMA_EOL:
22115 /* If we've run out of tokens, then there is no closing `]'. */
22116 return false;
22118 case CPP_OPEN_SQUARE:
22119 ++square_depth;
22120 break;
22122 case CPP_CLOSE_SQUARE:
22123 if (!square_depth--)
22125 cp_lexer_consume_token (parser->lexer);
22126 return true;
22128 break;
22130 default:
22131 break;
22134 /* Consume the token. */
22135 cp_lexer_consume_token (parser->lexer);
22139 /* Return true if we are looking at an array-designator, false otherwise. */
22141 static bool
22142 cp_parser_array_designator_p (cp_parser *parser)
22144 /* Consume the `['. */
22145 cp_lexer_consume_token (parser->lexer);
22147 cp_lexer_save_tokens (parser->lexer);
22149 /* Skip tokens until the next token is a closing square bracket.
22150 If we find the closing `]', and the next token is a `=', then
22151 we are looking at an array designator. */
22152 bool array_designator_p
22153 = (cp_parser_skip_to_closing_square_bracket (parser)
22154 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22156 /* Roll back the tokens we skipped. */
22157 cp_lexer_rollback_tokens (parser->lexer);
22159 return array_designator_p;
22162 /* Parse an initializer-list.
22164 initializer-list:
22165 initializer-clause ... [opt]
22166 initializer-list , initializer-clause ... [opt]
22168 C++2A Extension:
22170 designated-initializer-list:
22171 designated-initializer-clause
22172 designated-initializer-list , designated-initializer-clause
22174 designated-initializer-clause:
22175 designator brace-or-equal-initializer
22177 designator:
22178 . identifier
22180 GNU Extension:
22182 initializer-list:
22183 designation initializer-clause ...[opt]
22184 initializer-list , designation initializer-clause ...[opt]
22186 designation:
22187 . identifier =
22188 identifier :
22189 [ constant-expression ] =
22191 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22192 for the initializer. If the INDEX of the elt is non-NULL, it is the
22193 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22194 as for cp_parser_initializer. */
22196 static vec<constructor_elt, va_gc> *
22197 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22199 vec<constructor_elt, va_gc> *v = NULL;
22200 bool first_p = true;
22201 tree first_designator = NULL_TREE;
22203 /* Assume all of the expressions are constant. */
22204 *non_constant_p = false;
22206 /* Parse the rest of the list. */
22207 while (true)
22209 cp_token *token;
22210 tree designator;
22211 tree initializer;
22212 bool clause_non_constant_p;
22213 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22215 /* Handle the C++2A syntax, '. id ='. */
22216 if ((cxx_dialect >= cxx2a
22217 || cp_parser_allow_gnu_extensions_p (parser))
22218 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22219 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22220 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22221 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22222 == CPP_OPEN_BRACE)))
22224 if (cxx_dialect < cxx2a)
22225 pedwarn (loc, OPT_Wpedantic,
22226 "C++ designated initializers only available with "
22227 "-std=c++2a or -std=gnu++2a");
22228 /* Consume the `.'. */
22229 cp_lexer_consume_token (parser->lexer);
22230 /* Consume the identifier. */
22231 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22232 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22233 /* Consume the `='. */
22234 cp_lexer_consume_token (parser->lexer);
22236 /* Also, if the next token is an identifier and the following one is a
22237 colon, we are looking at the GNU designated-initializer
22238 syntax. */
22239 else if (cp_parser_allow_gnu_extensions_p (parser)
22240 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22241 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22242 == CPP_COLON))
22244 /* Warn the user that they are using an extension. */
22245 pedwarn (loc, OPT_Wpedantic,
22246 "ISO C++ does not allow GNU designated initializers");
22247 /* Consume the identifier. */
22248 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22249 /* Consume the `:'. */
22250 cp_lexer_consume_token (parser->lexer);
22252 /* Also handle C99 array designators, '[ const ] ='. */
22253 else if (cp_parser_allow_gnu_extensions_p (parser)
22254 && !c_dialect_objc ()
22255 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22257 /* In C++11, [ could start a lambda-introducer. */
22258 bool non_const = false;
22260 cp_parser_parse_tentatively (parser);
22262 if (!cp_parser_array_designator_p (parser))
22264 cp_parser_simulate_error (parser);
22265 designator = NULL_TREE;
22267 else
22269 designator = cp_parser_constant_expression (parser, true,
22270 &non_const);
22271 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22272 cp_parser_require (parser, CPP_EQ, RT_EQ);
22275 if (!cp_parser_parse_definitely (parser))
22276 designator = NULL_TREE;
22277 else if (non_const
22278 && (!require_potential_rvalue_constant_expression
22279 (designator)))
22280 designator = NULL_TREE;
22281 if (designator)
22282 /* Warn the user that they are using an extension. */
22283 pedwarn (loc, OPT_Wpedantic,
22284 "ISO C++ does not allow C99 designated initializers");
22286 else
22287 designator = NULL_TREE;
22289 if (first_p)
22291 first_designator = designator;
22292 first_p = false;
22294 else if (cxx_dialect >= cxx2a
22295 && first_designator != error_mark_node
22296 && (!first_designator != !designator))
22298 error_at (loc, "either all initializer clauses should be designated "
22299 "or none of them should be");
22300 first_designator = error_mark_node;
22302 else if (cxx_dialect < cxx2a && !first_designator)
22303 first_designator = designator;
22305 /* Parse the initializer. */
22306 initializer = cp_parser_initializer_clause (parser,
22307 &clause_non_constant_p);
22308 /* If any clause is non-constant, so is the entire initializer. */
22309 if (clause_non_constant_p)
22310 *non_constant_p = true;
22312 /* If we have an ellipsis, this is an initializer pack
22313 expansion. */
22314 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22316 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22318 /* Consume the `...'. */
22319 cp_lexer_consume_token (parser->lexer);
22321 if (designator && cxx_dialect >= cxx2a)
22322 error_at (loc,
22323 "%<...%> not allowed in designated initializer list");
22325 /* Turn the initializer into an initializer expansion. */
22326 initializer = make_pack_expansion (initializer);
22329 /* Add it to the vector. */
22330 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22332 /* If the next token is not a comma, we have reached the end of
22333 the list. */
22334 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22335 break;
22337 /* Peek at the next token. */
22338 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22339 /* If the next token is a `}', then we're still done. An
22340 initializer-clause can have a trailing `,' after the
22341 initializer-list and before the closing `}'. */
22342 if (token->type == CPP_CLOSE_BRACE)
22343 break;
22345 /* Consume the `,' token. */
22346 cp_lexer_consume_token (parser->lexer);
22349 /* The same identifier shall not appear in multiple designators
22350 of a designated-initializer-list. */
22351 if (first_designator)
22353 unsigned int i;
22354 tree designator, val;
22355 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22356 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22358 if (IDENTIFIER_MARKED (designator))
22360 error_at (EXPR_LOC_OR_LOC (val, input_location),
22361 "%<.%s%> designator used multiple times in "
22362 "the same initializer list",
22363 IDENTIFIER_POINTER (designator));
22364 (*v)[i].index = NULL_TREE;
22366 else
22367 IDENTIFIER_MARKED (designator) = 1;
22369 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22370 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22371 IDENTIFIER_MARKED (designator) = 0;
22374 return v;
22377 /* Classes [gram.class] */
22379 /* Parse a class-name.
22381 class-name:
22382 identifier
22383 template-id
22385 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22386 to indicate that names looked up in dependent types should be
22387 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22388 keyword has been used to indicate that the name that appears next
22389 is a template. TAG_TYPE indicates the explicit tag given before
22390 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22391 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22392 is the class being defined in a class-head. If ENUM_OK is TRUE,
22393 enum-names are also accepted.
22395 Returns the TYPE_DECL representing the class. */
22397 static tree
22398 cp_parser_class_name (cp_parser *parser,
22399 bool typename_keyword_p,
22400 bool template_keyword_p,
22401 enum tag_types tag_type,
22402 bool check_dependency_p,
22403 bool class_head_p,
22404 bool is_declaration,
22405 bool enum_ok)
22407 tree decl;
22408 tree scope;
22409 bool typename_p;
22410 cp_token *token;
22411 tree identifier = NULL_TREE;
22413 /* All class-names start with an identifier. */
22414 token = cp_lexer_peek_token (parser->lexer);
22415 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22417 cp_parser_error (parser, "expected class-name");
22418 return error_mark_node;
22421 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22422 to a template-id, so we save it here. */
22423 scope = parser->scope;
22424 if (scope == error_mark_node)
22425 return error_mark_node;
22427 /* Any name names a type if we're following the `typename' keyword
22428 in a qualified name where the enclosing scope is type-dependent. */
22429 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22430 && dependent_type_p (scope));
22431 /* Handle the common case (an identifier, but not a template-id)
22432 efficiently. */
22433 if (token->type == CPP_NAME
22434 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22436 cp_token *identifier_token;
22437 bool ambiguous_p;
22439 /* Look for the identifier. */
22440 identifier_token = cp_lexer_peek_token (parser->lexer);
22441 ambiguous_p = identifier_token->error_reported;
22442 identifier = cp_parser_identifier (parser);
22443 /* If the next token isn't an identifier, we are certainly not
22444 looking at a class-name. */
22445 if (identifier == error_mark_node)
22446 decl = error_mark_node;
22447 /* If we know this is a type-name, there's no need to look it
22448 up. */
22449 else if (typename_p)
22450 decl = identifier;
22451 else
22453 tree ambiguous_decls;
22454 /* If we already know that this lookup is ambiguous, then
22455 we've already issued an error message; there's no reason
22456 to check again. */
22457 if (ambiguous_p)
22459 cp_parser_simulate_error (parser);
22460 return error_mark_node;
22462 /* If the next token is a `::', then the name must be a type
22463 name.
22465 [basic.lookup.qual]
22467 During the lookup for a name preceding the :: scope
22468 resolution operator, object, function, and enumerator
22469 names are ignored. */
22470 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22471 tag_type = scope_type;
22472 /* Look up the name. */
22473 decl = cp_parser_lookup_name (parser, identifier,
22474 tag_type,
22475 /*is_template=*/false,
22476 /*is_namespace=*/false,
22477 check_dependency_p,
22478 &ambiguous_decls,
22479 identifier_token->location);
22480 if (ambiguous_decls)
22482 if (cp_parser_parsing_tentatively (parser))
22483 cp_parser_simulate_error (parser);
22484 return error_mark_node;
22488 else
22490 /* Try a template-id. */
22491 decl = cp_parser_template_id (parser, template_keyword_p,
22492 check_dependency_p,
22493 tag_type,
22494 is_declaration);
22495 if (decl == error_mark_node)
22496 return error_mark_node;
22499 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22501 /* If this is a typename, create a TYPENAME_TYPE. */
22502 if (typename_p && decl != error_mark_node)
22504 decl = make_typename_type (scope, decl, typename_type,
22505 /*complain=*/tf_error);
22506 if (decl != error_mark_node)
22507 decl = TYPE_NAME (decl);
22510 decl = strip_using_decl (decl);
22512 /* Check to see that it is really the name of a class. */
22513 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22514 && identifier_p (TREE_OPERAND (decl, 0))
22515 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22516 /* Situations like this:
22518 template <typename T> struct A {
22519 typename T::template X<int>::I i;
22522 are problematic. Is `T::template X<int>' a class-name? The
22523 standard does not seem to be definitive, but there is no other
22524 valid interpretation of the following `::'. Therefore, those
22525 names are considered class-names. */
22527 decl = make_typename_type (scope, decl, tag_type, tf_error);
22528 if (decl != error_mark_node)
22529 decl = TYPE_NAME (decl);
22531 else if (TREE_CODE (decl) != TYPE_DECL
22532 || TREE_TYPE (decl) == error_mark_node
22533 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22534 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22535 /* In Objective-C 2.0, a classname followed by '.' starts a
22536 dot-syntax expression, and it's not a type-name. */
22537 || (c_dialect_objc ()
22538 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22539 && objc_is_class_name (decl)))
22540 decl = error_mark_node;
22542 if (decl == error_mark_node)
22543 cp_parser_error (parser, "expected class-name");
22544 else if (identifier && !parser->scope)
22545 maybe_note_name_used_in_class (identifier, decl);
22547 return decl;
22550 /* Parse a class-specifier.
22552 class-specifier:
22553 class-head { member-specification [opt] }
22555 Returns the TREE_TYPE representing the class. */
22557 static tree
22558 cp_parser_class_specifier_1 (cp_parser* parser)
22560 tree type;
22561 tree attributes = NULL_TREE;
22562 bool nested_name_specifier_p;
22563 unsigned saved_num_template_parameter_lists;
22564 bool saved_in_function_body;
22565 unsigned char in_statement;
22566 bool in_switch_statement_p;
22567 bool saved_in_unbraced_linkage_specification_p;
22568 tree old_scope = NULL_TREE;
22569 tree scope = NULL_TREE;
22570 cp_token *closing_brace;
22572 push_deferring_access_checks (dk_no_deferred);
22574 /* Parse the class-head. */
22575 type = cp_parser_class_head (parser,
22576 &nested_name_specifier_p);
22577 /* If the class-head was a semantic disaster, skip the entire body
22578 of the class. */
22579 if (!type)
22581 cp_parser_skip_to_end_of_block_or_statement (parser);
22582 pop_deferring_access_checks ();
22583 return error_mark_node;
22586 /* Look for the `{'. */
22587 matching_braces braces;
22588 if (!braces.require_open (parser))
22590 pop_deferring_access_checks ();
22591 return error_mark_node;
22594 cp_ensure_no_omp_declare_simd (parser);
22595 cp_ensure_no_oacc_routine (parser);
22597 /* Issue an error message if type-definitions are forbidden here. */
22598 cp_parser_check_type_definition (parser);
22599 /* Remember that we are defining one more class. */
22600 ++parser->num_classes_being_defined;
22601 /* Inside the class, surrounding template-parameter-lists do not
22602 apply. */
22603 saved_num_template_parameter_lists
22604 = parser->num_template_parameter_lists;
22605 parser->num_template_parameter_lists = 0;
22606 /* We are not in a function body. */
22607 saved_in_function_body = parser->in_function_body;
22608 parser->in_function_body = false;
22609 /* Or in a loop. */
22610 in_statement = parser->in_statement;
22611 parser->in_statement = 0;
22612 /* Or in a switch. */
22613 in_switch_statement_p = parser->in_switch_statement_p;
22614 parser->in_switch_statement_p = false;
22615 /* We are not immediately inside an extern "lang" block. */
22616 saved_in_unbraced_linkage_specification_p
22617 = parser->in_unbraced_linkage_specification_p;
22618 parser->in_unbraced_linkage_specification_p = false;
22620 // Associate constraints with the type.
22621 if (flag_concepts)
22622 type = associate_classtype_constraints (type);
22624 /* Start the class. */
22625 if (nested_name_specifier_p)
22627 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22628 old_scope = push_inner_scope (scope);
22630 type = begin_class_definition (type);
22632 if (type == error_mark_node)
22633 /* If the type is erroneous, skip the entire body of the class. */
22634 cp_parser_skip_to_closing_brace (parser);
22635 else
22636 /* Parse the member-specification. */
22637 cp_parser_member_specification_opt (parser);
22639 /* Look for the trailing `}'. */
22640 closing_brace = braces.require_close (parser);
22641 /* Look for trailing attributes to apply to this class. */
22642 if (cp_parser_allow_gnu_extensions_p (parser))
22643 attributes = cp_parser_gnu_attributes_opt (parser);
22644 if (type != error_mark_node)
22645 type = finish_struct (type, attributes);
22646 if (nested_name_specifier_p)
22647 pop_inner_scope (old_scope, scope);
22649 /* We've finished a type definition. Check for the common syntax
22650 error of forgetting a semicolon after the definition. We need to
22651 be careful, as we can't just check for not-a-semicolon and be done
22652 with it; the user might have typed:
22654 class X { } c = ...;
22655 class X { } *p = ...;
22657 and so forth. Instead, enumerate all the possible tokens that
22658 might follow this production; if we don't see one of them, then
22659 complain and silently insert the semicolon. */
22661 cp_token *token = cp_lexer_peek_token (parser->lexer);
22662 bool want_semicolon = true;
22664 if (cp_next_tokens_can_be_std_attribute_p (parser))
22665 /* Don't try to parse c++11 attributes here. As per the
22666 grammar, that should be a task for
22667 cp_parser_decl_specifier_seq. */
22668 want_semicolon = false;
22670 switch (token->type)
22672 case CPP_NAME:
22673 case CPP_SEMICOLON:
22674 case CPP_MULT:
22675 case CPP_AND:
22676 case CPP_OPEN_PAREN:
22677 case CPP_CLOSE_PAREN:
22678 case CPP_COMMA:
22679 want_semicolon = false;
22680 break;
22682 /* While it's legal for type qualifiers and storage class
22683 specifiers to follow type definitions in the grammar, only
22684 compiler testsuites contain code like that. Assume that if
22685 we see such code, then what we're really seeing is a case
22686 like:
22688 class X { }
22689 const <type> var = ...;
22693 class Y { }
22694 static <type> func (...) ...
22696 i.e. the qualifier or specifier applies to the next
22697 declaration. To do so, however, we need to look ahead one
22698 more token to see if *that* token is a type specifier.
22700 This code could be improved to handle:
22702 class Z { }
22703 static const <type> var = ...; */
22704 case CPP_KEYWORD:
22705 if (keyword_is_decl_specifier (token->keyword))
22707 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22709 /* Handling user-defined types here would be nice, but very
22710 tricky. */
22711 want_semicolon
22712 = (lookahead->type == CPP_KEYWORD
22713 && keyword_begins_type_specifier (lookahead->keyword));
22715 break;
22716 default:
22717 break;
22720 /* If we don't have a type, then something is very wrong and we
22721 shouldn't try to do anything clever. Likewise for not seeing the
22722 closing brace. */
22723 if (closing_brace && TYPE_P (type) && want_semicolon)
22725 /* Locate the closing brace. */
22726 cp_token_position prev
22727 = cp_lexer_previous_token_position (parser->lexer);
22728 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22729 location_t loc = prev_token->location;
22731 /* We want to suggest insertion of a ';' immediately *after* the
22732 closing brace, so, if we can, offset the location by 1 column. */
22733 location_t next_loc = loc;
22734 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22735 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22737 rich_location richloc (line_table, next_loc);
22739 /* If we successfully offset the location, suggest the fix-it. */
22740 if (next_loc != loc)
22741 richloc.add_fixit_insert_before (next_loc, ";");
22743 if (CLASSTYPE_DECLARED_CLASS (type))
22744 error_at (&richloc,
22745 "expected %<;%> after class definition");
22746 else if (TREE_CODE (type) == RECORD_TYPE)
22747 error_at (&richloc,
22748 "expected %<;%> after struct definition");
22749 else if (TREE_CODE (type) == UNION_TYPE)
22750 error_at (&richloc,
22751 "expected %<;%> after union definition");
22752 else
22753 gcc_unreachable ();
22755 /* Unget one token and smash it to look as though we encountered
22756 a semicolon in the input stream. */
22757 cp_lexer_set_token_position (parser->lexer, prev);
22758 token = cp_lexer_peek_token (parser->lexer);
22759 token->type = CPP_SEMICOLON;
22760 token->keyword = RID_MAX;
22764 /* If this class is not itself within the scope of another class,
22765 then we need to parse the bodies of all of the queued function
22766 definitions. Note that the queued functions defined in a class
22767 are not always processed immediately following the
22768 class-specifier for that class. Consider:
22770 struct A {
22771 struct B { void f() { sizeof (A); } };
22774 If `f' were processed before the processing of `A' were
22775 completed, there would be no way to compute the size of `A'.
22776 Note that the nesting we are interested in here is lexical --
22777 not the semantic nesting given by TYPE_CONTEXT. In particular,
22778 for:
22780 struct A { struct B; };
22781 struct A::B { void f() { } };
22783 there is no need to delay the parsing of `A::B::f'. */
22784 if (--parser->num_classes_being_defined == 0)
22786 tree decl;
22787 tree class_type = NULL_TREE;
22788 tree pushed_scope = NULL_TREE;
22789 unsigned ix;
22790 cp_default_arg_entry *e;
22791 tree save_ccp, save_ccr;
22793 if (any_erroneous_template_args_p (type))
22795 /* Skip default arguments, NSDMIs, etc, in order to improve
22796 error recovery (c++/71169, c++/71832). */
22797 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22798 vec_safe_truncate (unparsed_nsdmis, 0);
22799 vec_safe_truncate (unparsed_classes, 0);
22800 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22803 /* In a first pass, parse default arguments to the functions.
22804 Then, in a second pass, parse the bodies of the functions.
22805 This two-phased approach handles cases like:
22807 struct S {
22808 void f() { g(); }
22809 void g(int i = 3);
22813 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22815 decl = e->decl;
22816 /* If there are default arguments that have not yet been processed,
22817 take care of them now. */
22818 if (class_type != e->class_type)
22820 if (pushed_scope)
22821 pop_scope (pushed_scope);
22822 class_type = e->class_type;
22823 pushed_scope = push_scope (class_type);
22825 /* Make sure that any template parameters are in scope. */
22826 maybe_begin_member_template_processing (decl);
22827 /* Parse the default argument expressions. */
22828 cp_parser_late_parsing_default_args (parser, decl);
22829 /* Remove any template parameters from the symbol table. */
22830 maybe_end_member_template_processing ();
22832 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22833 /* Now parse any NSDMIs. */
22834 save_ccp = current_class_ptr;
22835 save_ccr = current_class_ref;
22836 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22838 if (class_type != DECL_CONTEXT (decl))
22840 if (pushed_scope)
22841 pop_scope (pushed_scope);
22842 class_type = DECL_CONTEXT (decl);
22843 pushed_scope = push_scope (class_type);
22845 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22846 cp_parser_late_parsing_nsdmi (parser, decl);
22848 vec_safe_truncate (unparsed_nsdmis, 0);
22849 current_class_ptr = save_ccp;
22850 current_class_ref = save_ccr;
22851 if (pushed_scope)
22852 pop_scope (pushed_scope);
22854 /* Now do some post-NSDMI bookkeeping. */
22855 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22856 after_nsdmi_defaulted_late_checks (class_type);
22857 vec_safe_truncate (unparsed_classes, 0);
22858 after_nsdmi_defaulted_late_checks (type);
22860 /* Now parse the body of the functions. */
22861 if (flag_openmp)
22863 /* OpenMP UDRs need to be parsed before all other functions. */
22864 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22865 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22866 cp_parser_late_parsing_for_member (parser, decl);
22867 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22868 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22869 cp_parser_late_parsing_for_member (parser, decl);
22871 else
22872 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22873 cp_parser_late_parsing_for_member (parser, decl);
22874 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22876 else
22877 vec_safe_push (unparsed_classes, type);
22879 /* Put back any saved access checks. */
22880 pop_deferring_access_checks ();
22882 /* Restore saved state. */
22883 parser->in_switch_statement_p = in_switch_statement_p;
22884 parser->in_statement = in_statement;
22885 parser->in_function_body = saved_in_function_body;
22886 parser->num_template_parameter_lists
22887 = saved_num_template_parameter_lists;
22888 parser->in_unbraced_linkage_specification_p
22889 = saved_in_unbraced_linkage_specification_p;
22891 return type;
22894 static tree
22895 cp_parser_class_specifier (cp_parser* parser)
22897 tree ret;
22898 timevar_push (TV_PARSE_STRUCT);
22899 ret = cp_parser_class_specifier_1 (parser);
22900 timevar_pop (TV_PARSE_STRUCT);
22901 return ret;
22904 /* Parse a class-head.
22906 class-head:
22907 class-key identifier [opt] base-clause [opt]
22908 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22909 class-key nested-name-specifier [opt] template-id
22910 base-clause [opt]
22912 class-virt-specifier:
22913 final
22915 GNU Extensions:
22916 class-key attributes identifier [opt] base-clause [opt]
22917 class-key attributes nested-name-specifier identifier base-clause [opt]
22918 class-key attributes nested-name-specifier [opt] template-id
22919 base-clause [opt]
22921 Upon return BASES is initialized to the list of base classes (or
22922 NULL, if there are none) in the same form returned by
22923 cp_parser_base_clause.
22925 Returns the TYPE of the indicated class. Sets
22926 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22927 involving a nested-name-specifier was used, and FALSE otherwise.
22929 Returns error_mark_node if this is not a class-head.
22931 Returns NULL_TREE if the class-head is syntactically valid, but
22932 semantically invalid in a way that means we should skip the entire
22933 body of the class. */
22935 static tree
22936 cp_parser_class_head (cp_parser* parser,
22937 bool* nested_name_specifier_p)
22939 tree nested_name_specifier;
22940 enum tag_types class_key;
22941 tree id = NULL_TREE;
22942 tree type = NULL_TREE;
22943 tree attributes;
22944 tree bases;
22945 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22946 bool template_id_p = false;
22947 bool qualified_p = false;
22948 bool invalid_nested_name_p = false;
22949 bool invalid_explicit_specialization_p = false;
22950 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22951 tree pushed_scope = NULL_TREE;
22952 unsigned num_templates;
22953 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22954 /* Assume no nested-name-specifier will be present. */
22955 *nested_name_specifier_p = false;
22956 /* Assume no template parameter lists will be used in defining the
22957 type. */
22958 num_templates = 0;
22959 parser->colon_corrects_to_scope_p = false;
22961 /* Look for the class-key. */
22962 class_key = cp_parser_class_key (parser);
22963 if (class_key == none_type)
22964 return error_mark_node;
22966 location_t class_head_start_location = input_location;
22968 /* Parse the attributes. */
22969 attributes = cp_parser_attributes_opt (parser);
22971 /* If the next token is `::', that is invalid -- but sometimes
22972 people do try to write:
22974 struct ::S {};
22976 Handle this gracefully by accepting the extra qualifier, and then
22977 issuing an error about it later if this really is a
22978 class-head. If it turns out just to be an elaborated type
22979 specifier, remain silent. */
22980 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22981 qualified_p = true;
22983 push_deferring_access_checks (dk_no_check);
22985 /* Determine the name of the class. Begin by looking for an
22986 optional nested-name-specifier. */
22987 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22988 nested_name_specifier
22989 = cp_parser_nested_name_specifier_opt (parser,
22990 /*typename_keyword_p=*/false,
22991 /*check_dependency_p=*/false,
22992 /*type_p=*/true,
22993 /*is_declaration=*/false);
22994 /* If there was a nested-name-specifier, then there *must* be an
22995 identifier. */
22997 cp_token *bad_template_keyword = NULL;
22999 if (nested_name_specifier)
23001 type_start_token = cp_lexer_peek_token (parser->lexer);
23002 /* Although the grammar says `identifier', it really means
23003 `class-name' or `template-name'. You are only allowed to
23004 define a class that has already been declared with this
23005 syntax.
23007 The proposed resolution for Core Issue 180 says that wherever
23008 you see `class T::X' you should treat `X' as a type-name.
23010 It is OK to define an inaccessible class; for example:
23012 class A { class B; };
23013 class A::B {};
23015 We do not know if we will see a class-name, or a
23016 template-name. We look for a class-name first, in case the
23017 class-name is a template-id; if we looked for the
23018 template-name first we would stop after the template-name. */
23019 cp_parser_parse_tentatively (parser);
23020 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23021 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23022 type = cp_parser_class_name (parser,
23023 /*typename_keyword_p=*/false,
23024 /*template_keyword_p=*/false,
23025 class_type,
23026 /*check_dependency_p=*/false,
23027 /*class_head_p=*/true,
23028 /*is_declaration=*/false);
23029 /* If that didn't work, ignore the nested-name-specifier. */
23030 if (!cp_parser_parse_definitely (parser))
23032 invalid_nested_name_p = true;
23033 type_start_token = cp_lexer_peek_token (parser->lexer);
23034 id = cp_parser_identifier (parser);
23035 if (id == error_mark_node)
23036 id = NULL_TREE;
23038 /* If we could not find a corresponding TYPE, treat this
23039 declaration like an unqualified declaration. */
23040 if (type == error_mark_node)
23041 nested_name_specifier = NULL_TREE;
23042 /* Otherwise, count the number of templates used in TYPE and its
23043 containing scopes. */
23044 else
23045 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23047 /* Otherwise, the identifier is optional. */
23048 else
23050 /* We don't know whether what comes next is a template-id,
23051 an identifier, or nothing at all. */
23052 cp_parser_parse_tentatively (parser);
23053 /* Check for a template-id. */
23054 type_start_token = cp_lexer_peek_token (parser->lexer);
23055 id = cp_parser_template_id (parser,
23056 /*template_keyword_p=*/false,
23057 /*check_dependency_p=*/true,
23058 class_key,
23059 /*is_declaration=*/true);
23060 /* If that didn't work, it could still be an identifier. */
23061 if (!cp_parser_parse_definitely (parser))
23063 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23065 type_start_token = cp_lexer_peek_token (parser->lexer);
23066 id = cp_parser_identifier (parser);
23068 else
23069 id = NULL_TREE;
23071 else
23073 template_id_p = true;
23074 ++num_templates;
23078 pop_deferring_access_checks ();
23080 if (id)
23082 cp_parser_check_for_invalid_template_id (parser, id,
23083 class_key,
23084 type_start_token->location);
23086 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23088 /* If it's not a `:' or a `{' then we can't really be looking at a
23089 class-head, since a class-head only appears as part of a
23090 class-specifier. We have to detect this situation before calling
23091 xref_tag, since that has irreversible side-effects. */
23092 if (!cp_parser_next_token_starts_class_definition_p (parser))
23094 cp_parser_error (parser, "expected %<{%> or %<:%>");
23095 type = error_mark_node;
23096 goto out;
23099 /* At this point, we're going ahead with the class-specifier, even
23100 if some other problem occurs. */
23101 cp_parser_commit_to_tentative_parse (parser);
23102 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23104 cp_parser_error (parser,
23105 "cannot specify %<override%> for a class");
23106 type = error_mark_node;
23107 goto out;
23109 /* Issue the error about the overly-qualified name now. */
23110 if (qualified_p)
23112 cp_parser_error (parser,
23113 "global qualification of class name is invalid");
23114 type = error_mark_node;
23115 goto out;
23117 else if (invalid_nested_name_p)
23119 cp_parser_error (parser,
23120 "qualified name does not name a class");
23121 type = error_mark_node;
23122 goto out;
23124 else if (nested_name_specifier)
23126 tree scope;
23128 if (bad_template_keyword)
23129 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23130 keyword template shall not appear at the top level. */
23131 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23132 "keyword %<template%> not allowed in class-head-name");
23134 /* Reject typedef-names in class heads. */
23135 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23137 error_at (type_start_token->location,
23138 "invalid class name in declaration of %qD",
23139 type);
23140 type = NULL_TREE;
23141 goto done;
23144 /* Figure out in what scope the declaration is being placed. */
23145 scope = current_scope ();
23146 /* If that scope does not contain the scope in which the
23147 class was originally declared, the program is invalid. */
23148 if (scope && !is_ancestor (scope, nested_name_specifier))
23150 if (at_namespace_scope_p ())
23151 error_at (type_start_token->location,
23152 "declaration of %qD in namespace %qD which does not "
23153 "enclose %qD",
23154 type, scope, nested_name_specifier);
23155 else
23156 error_at (type_start_token->location,
23157 "declaration of %qD in %qD which does not enclose %qD",
23158 type, scope, nested_name_specifier);
23159 type = NULL_TREE;
23160 goto done;
23162 /* [dcl.meaning]
23164 A declarator-id shall not be qualified except for the
23165 definition of a ... nested class outside of its class
23166 ... [or] the definition or explicit instantiation of a
23167 class member of a namespace outside of its namespace. */
23168 if (scope == nested_name_specifier)
23170 permerror (nested_name_specifier_token_start->location,
23171 "extra qualification not allowed");
23172 nested_name_specifier = NULL_TREE;
23173 num_templates = 0;
23176 /* An explicit-specialization must be preceded by "template <>". If
23177 it is not, try to recover gracefully. */
23178 if (at_namespace_scope_p ()
23179 && parser->num_template_parameter_lists == 0
23180 && !processing_template_parmlist
23181 && template_id_p)
23183 /* Build a location of this form:
23184 struct typename <ARGS>
23185 ^~~~~~~~~~~~~~~~~~~~~~
23186 with caret==start at the start token, and
23187 finishing at the end of the type. */
23188 location_t reported_loc
23189 = make_location (class_head_start_location,
23190 class_head_start_location,
23191 get_finish (type_start_token->location));
23192 rich_location richloc (line_table, reported_loc);
23193 richloc.add_fixit_insert_before (class_head_start_location,
23194 "template <> ");
23195 error_at (&richloc,
23196 "an explicit specialization must be preceded by"
23197 " %<template <>%>");
23198 invalid_explicit_specialization_p = true;
23199 /* Take the same action that would have been taken by
23200 cp_parser_explicit_specialization. */
23201 ++parser->num_template_parameter_lists;
23202 begin_specialization ();
23204 /* There must be no "return" statements between this point and the
23205 end of this function; set "type "to the correct return value and
23206 use "goto done;" to return. */
23207 /* Make sure that the right number of template parameters were
23208 present. */
23209 if (!cp_parser_check_template_parameters (parser, num_templates,
23210 template_id_p,
23211 type_start_token->location,
23212 /*declarator=*/NULL))
23214 /* If something went wrong, there is no point in even trying to
23215 process the class-definition. */
23216 type = NULL_TREE;
23217 goto done;
23220 /* Look up the type. */
23221 if (template_id_p)
23223 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23224 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23225 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23227 error_at (type_start_token->location,
23228 "function template %qD redeclared as a class template", id);
23229 type = error_mark_node;
23231 else
23233 type = TREE_TYPE (id);
23234 type = maybe_process_partial_specialization (type);
23236 /* Check the scope while we still know whether or not we had a
23237 nested-name-specifier. */
23238 if (type != error_mark_node)
23239 check_unqualified_spec_or_inst (type, type_start_token->location);
23241 if (nested_name_specifier)
23242 pushed_scope = push_scope (nested_name_specifier);
23244 else if (nested_name_specifier)
23246 tree class_type;
23248 /* Given:
23250 template <typename T> struct S { struct T };
23251 template <typename T> struct S<T>::T { };
23253 we will get a TYPENAME_TYPE when processing the definition of
23254 `S::T'. We need to resolve it to the actual type before we
23255 try to define it. */
23256 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23258 class_type = resolve_typename_type (TREE_TYPE (type),
23259 /*only_current_p=*/false);
23260 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23261 type = TYPE_NAME (class_type);
23262 else
23264 cp_parser_error (parser, "could not resolve typename type");
23265 type = error_mark_node;
23269 if (maybe_process_partial_specialization (TREE_TYPE (type))
23270 == error_mark_node)
23272 type = NULL_TREE;
23273 goto done;
23276 class_type = current_class_type;
23277 /* Enter the scope indicated by the nested-name-specifier. */
23278 pushed_scope = push_scope (nested_name_specifier);
23279 /* Get the canonical version of this type. */
23280 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23281 /* Call push_template_decl if it seems like we should be defining a
23282 template either from the template headers or the type we're
23283 defining, so that we diagnose both extra and missing headers. */
23284 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23285 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23286 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23288 type = push_template_decl (type);
23289 if (type == error_mark_node)
23291 type = NULL_TREE;
23292 goto done;
23296 type = TREE_TYPE (type);
23297 *nested_name_specifier_p = true;
23299 else /* The name is not a nested name. */
23301 /* If the class was unnamed, create a dummy name. */
23302 if (!id)
23303 id = make_anon_name ();
23304 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23305 ? ts_within_enclosing_non_class
23306 : ts_current);
23307 type = xref_tag (class_key, id, tag_scope,
23308 parser->num_template_parameter_lists);
23311 /* Indicate whether this class was declared as a `class' or as a
23312 `struct'. */
23313 if (TREE_CODE (type) == RECORD_TYPE)
23314 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23315 cp_parser_check_class_key (class_key, type);
23317 /* If this type was already complete, and we see another definition,
23318 that's an error. */
23319 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23321 error_at (type_start_token->location, "redefinition of %q#T",
23322 type);
23323 inform (location_of (type), "previous definition of %q#T",
23324 type);
23325 type = NULL_TREE;
23326 goto done;
23328 else if (type == error_mark_node)
23329 type = NULL_TREE;
23331 if (type)
23333 /* Apply attributes now, before any use of the class as a template
23334 argument in its base list. */
23335 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23336 fixup_attribute_variants (type);
23339 /* We will have entered the scope containing the class; the names of
23340 base classes should be looked up in that context. For example:
23342 struct A { struct B {}; struct C; };
23343 struct A::C : B {};
23345 is valid. */
23347 /* Get the list of base-classes, if there is one. */
23348 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23350 /* PR59482: enter the class scope so that base-specifiers are looked
23351 up correctly. */
23352 if (type)
23353 pushclass (type);
23354 bases = cp_parser_base_clause (parser);
23355 /* PR59482: get out of the previously pushed class scope so that the
23356 subsequent pops pop the right thing. */
23357 if (type)
23358 popclass ();
23360 else
23361 bases = NULL_TREE;
23363 /* If we're really defining a class, process the base classes.
23364 If they're invalid, fail. */
23365 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23366 xref_basetypes (type, bases);
23368 done:
23369 /* Leave the scope given by the nested-name-specifier. We will
23370 enter the class scope itself while processing the members. */
23371 if (pushed_scope)
23372 pop_scope (pushed_scope);
23374 if (invalid_explicit_specialization_p)
23376 end_specialization ();
23377 --parser->num_template_parameter_lists;
23380 if (type)
23381 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23382 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23383 CLASSTYPE_FINAL (type) = 1;
23384 out:
23385 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23386 return type;
23389 /* Parse a class-key.
23391 class-key:
23392 class
23393 struct
23394 union
23396 Returns the kind of class-key specified, or none_type to indicate
23397 error. */
23399 static enum tag_types
23400 cp_parser_class_key (cp_parser* parser)
23402 cp_token *token;
23403 enum tag_types tag_type;
23405 /* Look for the class-key. */
23406 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23407 if (!token)
23408 return none_type;
23410 /* Check to see if the TOKEN is a class-key. */
23411 tag_type = cp_parser_token_is_class_key (token);
23412 if (!tag_type)
23413 cp_parser_error (parser, "expected class-key");
23414 return tag_type;
23417 /* Parse a type-parameter-key.
23419 type-parameter-key:
23420 class
23421 typename
23424 static void
23425 cp_parser_type_parameter_key (cp_parser* parser)
23427 /* Look for the type-parameter-key. */
23428 enum tag_types tag_type = none_type;
23429 cp_token *token = cp_lexer_peek_token (parser->lexer);
23430 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23432 cp_lexer_consume_token (parser->lexer);
23433 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23434 /* typename is not allowed in a template template parameter
23435 by the standard until C++17. */
23436 pedwarn (token->location, OPT_Wpedantic,
23437 "ISO C++ forbids typename key in template template parameter;"
23438 " use -std=c++17 or -std=gnu++17");
23440 else
23441 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23443 return;
23446 /* Parse an (optional) member-specification.
23448 member-specification:
23449 member-declaration member-specification [opt]
23450 access-specifier : member-specification [opt] */
23452 static void
23453 cp_parser_member_specification_opt (cp_parser* parser)
23455 while (true)
23457 cp_token *token;
23458 enum rid keyword;
23460 /* Peek at the next token. */
23461 token = cp_lexer_peek_token (parser->lexer);
23462 /* If it's a `}', or EOF then we've seen all the members. */
23463 if (token->type == CPP_CLOSE_BRACE
23464 || token->type == CPP_EOF
23465 || token->type == CPP_PRAGMA_EOL)
23466 break;
23468 /* See if this token is a keyword. */
23469 keyword = token->keyword;
23470 switch (keyword)
23472 case RID_PUBLIC:
23473 case RID_PROTECTED:
23474 case RID_PRIVATE:
23475 /* Consume the access-specifier. */
23476 cp_lexer_consume_token (parser->lexer);
23477 /* Remember which access-specifier is active. */
23478 current_access_specifier = token->u.value;
23479 /* Look for the `:'. */
23480 cp_parser_require (parser, CPP_COLON, RT_COLON);
23481 break;
23483 default:
23484 /* Accept #pragmas at class scope. */
23485 if (token->type == CPP_PRAGMA)
23487 cp_parser_pragma (parser, pragma_member, NULL);
23488 break;
23491 /* Otherwise, the next construction must be a
23492 member-declaration. */
23493 cp_parser_member_declaration (parser);
23498 /* Parse a member-declaration.
23500 member-declaration:
23501 decl-specifier-seq [opt] member-declarator-list [opt] ;
23502 function-definition ; [opt]
23503 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23504 using-declaration
23505 template-declaration
23506 alias-declaration
23508 member-declarator-list:
23509 member-declarator
23510 member-declarator-list , member-declarator
23512 member-declarator:
23513 declarator pure-specifier [opt]
23514 declarator constant-initializer [opt]
23515 identifier [opt] : constant-expression
23517 GNU Extensions:
23519 member-declaration:
23520 __extension__ member-declaration
23522 member-declarator:
23523 declarator attributes [opt] pure-specifier [opt]
23524 declarator attributes [opt] constant-initializer [opt]
23525 identifier [opt] attributes [opt] : constant-expression
23527 C++0x Extensions:
23529 member-declaration:
23530 static_assert-declaration */
23532 static void
23533 cp_parser_member_declaration (cp_parser* parser)
23535 cp_decl_specifier_seq decl_specifiers;
23536 tree prefix_attributes;
23537 tree decl;
23538 int declares_class_or_enum;
23539 bool friend_p;
23540 cp_token *token = NULL;
23541 cp_token *decl_spec_token_start = NULL;
23542 cp_token *initializer_token_start = NULL;
23543 int saved_pedantic;
23544 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23546 /* Check for the `__extension__' keyword. */
23547 if (cp_parser_extension_opt (parser, &saved_pedantic))
23549 /* Recurse. */
23550 cp_parser_member_declaration (parser);
23551 /* Restore the old value of the PEDANTIC flag. */
23552 pedantic = saved_pedantic;
23554 return;
23557 /* Check for a template-declaration. */
23558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23560 /* An explicit specialization here is an error condition, and we
23561 expect the specialization handler to detect and report this. */
23562 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23563 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23564 cp_parser_explicit_specialization (parser);
23565 else
23566 cp_parser_template_declaration (parser, /*member_p=*/true);
23568 return;
23570 /* Check for a template introduction. */
23571 else if (cp_parser_template_declaration_after_export (parser, true))
23572 return;
23574 /* Check for a using-declaration. */
23575 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23577 if (cxx_dialect < cxx11)
23579 /* Parse the using-declaration. */
23580 cp_parser_using_declaration (parser,
23581 /*access_declaration_p=*/false);
23582 return;
23584 else
23586 tree decl;
23587 bool alias_decl_expected;
23588 cp_parser_parse_tentatively (parser);
23589 decl = cp_parser_alias_declaration (parser);
23590 /* Note that if we actually see the '=' token after the
23591 identifier, cp_parser_alias_declaration commits the
23592 tentative parse. In that case, we really expect an
23593 alias-declaration. Otherwise, we expect a using
23594 declaration. */
23595 alias_decl_expected =
23596 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23597 cp_parser_parse_definitely (parser);
23599 if (alias_decl_expected)
23600 finish_member_declaration (decl);
23601 else
23602 cp_parser_using_declaration (parser,
23603 /*access_declaration_p=*/false);
23604 return;
23608 /* Check for @defs. */
23609 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23611 tree ivar, member;
23612 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23613 ivar = ivar_chains;
23614 while (ivar)
23616 member = ivar;
23617 ivar = TREE_CHAIN (member);
23618 TREE_CHAIN (member) = NULL_TREE;
23619 finish_member_declaration (member);
23621 return;
23624 /* If the next token is `static_assert' we have a static assertion. */
23625 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23627 cp_parser_static_assert (parser, /*member_p=*/true);
23628 return;
23631 parser->colon_corrects_to_scope_p = false;
23633 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23634 goto out;
23636 /* Parse the decl-specifier-seq. */
23637 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23638 cp_parser_decl_specifier_seq (parser,
23639 CP_PARSER_FLAGS_OPTIONAL,
23640 &decl_specifiers,
23641 &declares_class_or_enum);
23642 /* Check for an invalid type-name. */
23643 if (!decl_specifiers.any_type_specifiers_p
23644 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23645 goto out;
23646 /* If there is no declarator, then the decl-specifier-seq should
23647 specify a type. */
23648 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23650 /* If there was no decl-specifier-seq, and the next token is a
23651 `;', then we have something like:
23653 struct S { ; };
23655 [class.mem]
23657 Each member-declaration shall declare at least one member
23658 name of the class. */
23659 if (!decl_specifiers.any_specifiers_p)
23661 cp_token *token = cp_lexer_peek_token (parser->lexer);
23662 if (!in_system_header_at (token->location))
23664 gcc_rich_location richloc (token->location);
23665 richloc.add_fixit_remove ();
23666 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23669 else
23671 tree type;
23673 /* See if this declaration is a friend. */
23674 friend_p = cp_parser_friend_p (&decl_specifiers);
23675 /* If there were decl-specifiers, check to see if there was
23676 a class-declaration. */
23677 type = check_tag_decl (&decl_specifiers,
23678 /*explicit_type_instantiation_p=*/false);
23679 /* Nested classes have already been added to the class, but
23680 a `friend' needs to be explicitly registered. */
23681 if (friend_p)
23683 /* If the `friend' keyword was present, the friend must
23684 be introduced with a class-key. */
23685 if (!declares_class_or_enum && cxx_dialect < cxx11)
23686 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23687 "in C++03 a class-key must be used "
23688 "when declaring a friend");
23689 /* In this case:
23691 template <typename T> struct A {
23692 friend struct A<T>::B;
23695 A<T>::B will be represented by a TYPENAME_TYPE, and
23696 therefore not recognized by check_tag_decl. */
23697 if (!type)
23699 type = decl_specifiers.type;
23700 if (type && TREE_CODE (type) == TYPE_DECL)
23701 type = TREE_TYPE (type);
23703 if (!type || !TYPE_P (type))
23704 error_at (decl_spec_token_start->location,
23705 "friend declaration does not name a class or "
23706 "function");
23707 else
23708 make_friend_class (current_class_type, type,
23709 /*complain=*/true);
23711 /* If there is no TYPE, an error message will already have
23712 been issued. */
23713 else if (!type || type == error_mark_node)
23715 /* An anonymous aggregate has to be handled specially; such
23716 a declaration really declares a data member (with a
23717 particular type), as opposed to a nested class. */
23718 else if (ANON_AGGR_TYPE_P (type))
23720 /* C++11 9.5/6. */
23721 if (decl_specifiers.storage_class != sc_none)
23722 error_at (decl_spec_token_start->location,
23723 "a storage class on an anonymous aggregate "
23724 "in class scope is not allowed");
23726 /* Remove constructors and such from TYPE, now that we
23727 know it is an anonymous aggregate. */
23728 fixup_anonymous_aggr (type);
23729 /* And make the corresponding data member. */
23730 decl = build_decl (decl_spec_token_start->location,
23731 FIELD_DECL, NULL_TREE, type);
23732 /* Add it to the class. */
23733 finish_member_declaration (decl);
23735 else
23736 cp_parser_check_access_in_redeclaration
23737 (TYPE_NAME (type),
23738 decl_spec_token_start->location);
23741 else
23743 bool assume_semicolon = false;
23745 /* Clear attributes from the decl_specifiers but keep them
23746 around as prefix attributes that apply them to the entity
23747 being declared. */
23748 prefix_attributes = decl_specifiers.attributes;
23749 decl_specifiers.attributes = NULL_TREE;
23751 /* See if these declarations will be friends. */
23752 friend_p = cp_parser_friend_p (&decl_specifiers);
23754 /* Keep going until we hit the `;' at the end of the
23755 declaration. */
23756 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23758 tree attributes = NULL_TREE;
23759 tree first_attribute;
23760 tree initializer;
23761 bool named_bitfld = false;
23763 /* Peek at the next token. */
23764 token = cp_lexer_peek_token (parser->lexer);
23766 /* The following code wants to know early if it is a bit-field
23767 or some other declaration. Attributes can appear before
23768 the `:' token. Skip over them without consuming any tokens
23769 to peek if they are followed by `:'. */
23770 if (cp_next_tokens_can_be_attribute_p (parser)
23771 || (token->type == CPP_NAME
23772 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23773 && (named_bitfld = true)))
23775 size_t n
23776 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23777 token = cp_lexer_peek_nth_token (parser->lexer, n);
23780 /* Check for a bitfield declaration. */
23781 if (token->type == CPP_COLON
23782 || (token->type == CPP_NAME
23783 && token == cp_lexer_peek_token (parser->lexer)
23784 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23785 && (named_bitfld = true)))
23787 tree identifier;
23788 tree width;
23789 tree late_attributes = NULL_TREE;
23791 if (named_bitfld)
23792 identifier = cp_parser_identifier (parser);
23793 else
23794 identifier = NULL_TREE;
23796 /* Look for attributes that apply to the bitfield. */
23797 attributes = cp_parser_attributes_opt (parser);
23799 /* Consume the `:' token. */
23800 cp_lexer_consume_token (parser->lexer);
23802 /* Get the width of the bitfield. */
23803 width = cp_parser_constant_expression (parser, false, NULL,
23804 cxx_dialect >= cxx11);
23806 /* In C++2A and as extension for C++11 and above we allow
23807 default member initializers for bit-fields. */
23808 initializer = NULL_TREE;
23809 if (cxx_dialect >= cxx11
23810 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23811 || cp_lexer_next_token_is (parser->lexer,
23812 CPP_OPEN_BRACE)))
23814 location_t loc
23815 = cp_lexer_peek_token (parser->lexer)->location;
23816 if (cxx_dialect < cxx2a
23817 && !in_system_header_at (loc)
23818 && identifier != NULL_TREE)
23819 pedwarn (loc, 0,
23820 "default member initializers for bit-fields "
23821 "only available with -std=c++2a or "
23822 "-std=gnu++2a");
23824 initializer = cp_parser_save_nsdmi (parser);
23825 if (identifier == NULL_TREE)
23827 error_at (loc, "default member initializer for "
23828 "unnamed bit-field");
23829 initializer = NULL_TREE;
23832 else
23834 /* Look for attributes that apply to the bitfield after
23835 the `:' token and width. This is where GCC used to
23836 parse attributes in the past, pedwarn if there is
23837 a std attribute. */
23838 if (cp_next_tokens_can_be_std_attribute_p (parser))
23839 pedwarn (input_location, OPT_Wpedantic,
23840 "ISO C++ allows bit-field attributes only "
23841 "before the %<:%> token");
23843 late_attributes = cp_parser_attributes_opt (parser);
23846 attributes = attr_chainon (attributes, late_attributes);
23848 /* Remember which attributes are prefix attributes and
23849 which are not. */
23850 first_attribute = attributes;
23851 /* Combine the attributes. */
23852 attributes = attr_chainon (prefix_attributes, attributes);
23854 /* Create the bitfield declaration. */
23855 decl = grokbitfield (identifier
23856 ? make_id_declarator (NULL_TREE,
23857 identifier,
23858 sfk_none)
23859 : NULL,
23860 &decl_specifiers,
23861 width, initializer,
23862 attributes);
23864 else
23866 cp_declarator *declarator;
23867 tree asm_specification;
23868 int ctor_dtor_or_conv_p;
23870 /* Parse the declarator. */
23871 declarator
23872 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23873 &ctor_dtor_or_conv_p,
23874 /*parenthesized_p=*/NULL,
23875 /*member_p=*/true,
23876 friend_p);
23878 /* If something went wrong parsing the declarator, make sure
23879 that we at least consume some tokens. */
23880 if (declarator == cp_error_declarator)
23882 /* Skip to the end of the statement. */
23883 cp_parser_skip_to_end_of_statement (parser);
23884 /* If the next token is not a semicolon, that is
23885 probably because we just skipped over the body of
23886 a function. So, we consume a semicolon if
23887 present, but do not issue an error message if it
23888 is not present. */
23889 if (cp_lexer_next_token_is (parser->lexer,
23890 CPP_SEMICOLON))
23891 cp_lexer_consume_token (parser->lexer);
23892 goto out;
23895 if (declares_class_or_enum & 2)
23896 cp_parser_check_for_definition_in_return_type
23897 (declarator, decl_specifiers.type,
23898 decl_specifiers.locations[ds_type_spec]);
23900 /* Look for an asm-specification. */
23901 asm_specification = cp_parser_asm_specification_opt (parser);
23902 /* Look for attributes that apply to the declaration. */
23903 attributes = cp_parser_attributes_opt (parser);
23904 /* Remember which attributes are prefix attributes and
23905 which are not. */
23906 first_attribute = attributes;
23907 /* Combine the attributes. */
23908 attributes = attr_chainon (prefix_attributes, attributes);
23910 /* If it's an `=', then we have a constant-initializer or a
23911 pure-specifier. It is not correct to parse the
23912 initializer before registering the member declaration
23913 since the member declaration should be in scope while
23914 its initializer is processed. However, the rest of the
23915 front end does not yet provide an interface that allows
23916 us to handle this correctly. */
23917 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23919 /* In [class.mem]:
23921 A pure-specifier shall be used only in the declaration of
23922 a virtual function.
23924 A member-declarator can contain a constant-initializer
23925 only if it declares a static member of integral or
23926 enumeration type.
23928 Therefore, if the DECLARATOR is for a function, we look
23929 for a pure-specifier; otherwise, we look for a
23930 constant-initializer. When we call `grokfield', it will
23931 perform more stringent semantics checks. */
23932 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23933 if (function_declarator_p (declarator)
23934 || (decl_specifiers.type
23935 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23936 && declarator->kind == cdk_id
23937 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23938 == FUNCTION_TYPE)))
23939 initializer = cp_parser_pure_specifier (parser);
23940 else if (decl_specifiers.storage_class != sc_static)
23941 initializer = cp_parser_save_nsdmi (parser);
23942 else if (cxx_dialect >= cxx11)
23944 bool nonconst;
23945 /* Don't require a constant rvalue in C++11, since we
23946 might want a reference constant. We'll enforce
23947 constancy later. */
23948 cp_lexer_consume_token (parser->lexer);
23949 /* Parse the initializer. */
23950 initializer = cp_parser_initializer_clause (parser,
23951 &nonconst);
23953 else
23954 /* Parse the initializer. */
23955 initializer = cp_parser_constant_initializer (parser);
23957 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23958 && !function_declarator_p (declarator))
23960 bool x;
23961 if (decl_specifiers.storage_class != sc_static)
23962 initializer = cp_parser_save_nsdmi (parser);
23963 else
23964 initializer = cp_parser_initializer (parser, &x, &x);
23966 /* Otherwise, there is no initializer. */
23967 else
23968 initializer = NULL_TREE;
23970 /* See if we are probably looking at a function
23971 definition. We are certainly not looking at a
23972 member-declarator. Calling `grokfield' has
23973 side-effects, so we must not do it unless we are sure
23974 that we are looking at a member-declarator. */
23975 if (cp_parser_token_starts_function_definition_p
23976 (cp_lexer_peek_token (parser->lexer)))
23978 /* The grammar does not allow a pure-specifier to be
23979 used when a member function is defined. (It is
23980 possible that this fact is an oversight in the
23981 standard, since a pure function may be defined
23982 outside of the class-specifier. */
23983 if (initializer && initializer_token_start)
23984 error_at (initializer_token_start->location,
23985 "pure-specifier on function-definition");
23986 decl = cp_parser_save_member_function_body (parser,
23987 &decl_specifiers,
23988 declarator,
23989 attributes);
23990 if (parser->fully_implicit_function_template_p)
23991 decl = finish_fully_implicit_template (parser, decl);
23992 /* If the member was not a friend, declare it here. */
23993 if (!friend_p)
23994 finish_member_declaration (decl);
23995 /* Peek at the next token. */
23996 token = cp_lexer_peek_token (parser->lexer);
23997 /* If the next token is a semicolon, consume it. */
23998 if (token->type == CPP_SEMICOLON)
24000 location_t semicolon_loc
24001 = cp_lexer_consume_token (parser->lexer)->location;
24002 gcc_rich_location richloc (semicolon_loc);
24003 richloc.add_fixit_remove ();
24004 warning_at (&richloc, OPT_Wextra_semi,
24005 "extra %<;%> after in-class "
24006 "function definition");
24008 goto out;
24010 else
24011 if (declarator->kind == cdk_function)
24012 declarator->id_loc = token->location;
24013 /* Create the declaration. */
24014 decl = grokfield (declarator, &decl_specifiers,
24015 initializer, /*init_const_expr_p=*/true,
24016 asm_specification, attributes);
24017 if (parser->fully_implicit_function_template_p)
24019 if (friend_p)
24020 finish_fully_implicit_template (parser, 0);
24021 else
24022 decl = finish_fully_implicit_template (parser, decl);
24026 cp_finalize_omp_declare_simd (parser, decl);
24027 cp_finalize_oacc_routine (parser, decl, false);
24029 /* Reset PREFIX_ATTRIBUTES. */
24030 if (attributes != error_mark_node)
24032 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24033 attributes = TREE_CHAIN (attributes);
24034 if (attributes)
24035 TREE_CHAIN (attributes) = NULL_TREE;
24038 /* If there is any qualification still in effect, clear it
24039 now; we will be starting fresh with the next declarator. */
24040 parser->scope = NULL_TREE;
24041 parser->qualifying_scope = NULL_TREE;
24042 parser->object_scope = NULL_TREE;
24043 /* If it's a `,', then there are more declarators. */
24044 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24046 cp_lexer_consume_token (parser->lexer);
24047 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24049 cp_token *token = cp_lexer_previous_token (parser->lexer);
24050 gcc_rich_location richloc (token->location);
24051 richloc.add_fixit_remove ();
24052 error_at (&richloc, "stray %<,%> at end of "
24053 "member declaration");
24056 /* If the next token isn't a `;', then we have a parse error. */
24057 else if (cp_lexer_next_token_is_not (parser->lexer,
24058 CPP_SEMICOLON))
24060 /* The next token might be a ways away from where the
24061 actual semicolon is missing. Find the previous token
24062 and use that for our error position. */
24063 cp_token *token = cp_lexer_previous_token (parser->lexer);
24064 gcc_rich_location richloc (token->location);
24065 richloc.add_fixit_insert_after (";");
24066 error_at (&richloc, "expected %<;%> at end of "
24067 "member declaration");
24069 /* Assume that the user meant to provide a semicolon. If
24070 we were to cp_parser_skip_to_end_of_statement, we might
24071 skip to a semicolon inside a member function definition
24072 and issue nonsensical error messages. */
24073 assume_semicolon = true;
24076 if (decl)
24078 /* Add DECL to the list of members. */
24079 if (!friend_p
24080 /* Explicitly include, eg, NSDMIs, for better error
24081 recovery (c++/58650). */
24082 || !DECL_DECLARES_FUNCTION_P (decl))
24083 finish_member_declaration (decl);
24085 if (TREE_CODE (decl) == FUNCTION_DECL)
24086 cp_parser_save_default_args (parser, decl);
24087 else if (TREE_CODE (decl) == FIELD_DECL
24088 && DECL_INITIAL (decl))
24089 /* Add DECL to the queue of NSDMI to be parsed later. */
24090 vec_safe_push (unparsed_nsdmis, decl);
24093 if (assume_semicolon)
24094 goto out;
24098 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24099 out:
24100 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24103 /* Parse a pure-specifier.
24105 pure-specifier:
24108 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24109 Otherwise, ERROR_MARK_NODE is returned. */
24111 static tree
24112 cp_parser_pure_specifier (cp_parser* parser)
24114 cp_token *token;
24116 /* Look for the `=' token. */
24117 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24118 return error_mark_node;
24119 /* Look for the `0' token. */
24120 token = cp_lexer_peek_token (parser->lexer);
24122 if (token->type == CPP_EOF
24123 || token->type == CPP_PRAGMA_EOL)
24124 return error_mark_node;
24126 cp_lexer_consume_token (parser->lexer);
24128 /* Accept = default or = delete in c++0x mode. */
24129 if (token->keyword == RID_DEFAULT
24130 || token->keyword == RID_DELETE)
24132 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24133 return token->u.value;
24136 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24137 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24139 cp_parser_error (parser,
24140 "invalid pure specifier (only %<= 0%> is allowed)");
24141 cp_parser_skip_to_end_of_statement (parser);
24142 return error_mark_node;
24144 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24146 error_at (token->location, "templates may not be %<virtual%>");
24147 return error_mark_node;
24150 return integer_zero_node;
24153 /* Parse a constant-initializer.
24155 constant-initializer:
24156 = constant-expression
24158 Returns a representation of the constant-expression. */
24160 static tree
24161 cp_parser_constant_initializer (cp_parser* parser)
24163 /* Look for the `=' token. */
24164 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24165 return error_mark_node;
24167 /* It is invalid to write:
24169 struct S { static const int i = { 7 }; };
24172 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24174 cp_parser_error (parser,
24175 "a brace-enclosed initializer is not allowed here");
24176 /* Consume the opening brace. */
24177 matching_braces braces;
24178 braces.consume_open (parser);
24179 /* Skip the initializer. */
24180 cp_parser_skip_to_closing_brace (parser);
24181 /* Look for the trailing `}'. */
24182 braces.require_close (parser);
24184 return error_mark_node;
24187 return cp_parser_constant_expression (parser);
24190 /* Derived classes [gram.class.derived] */
24192 /* Parse a base-clause.
24194 base-clause:
24195 : base-specifier-list
24197 base-specifier-list:
24198 base-specifier ... [opt]
24199 base-specifier-list , base-specifier ... [opt]
24201 Returns a TREE_LIST representing the base-classes, in the order in
24202 which they were declared. The representation of each node is as
24203 described by cp_parser_base_specifier.
24205 In the case that no bases are specified, this function will return
24206 NULL_TREE, not ERROR_MARK_NODE. */
24208 static tree
24209 cp_parser_base_clause (cp_parser* parser)
24211 tree bases = NULL_TREE;
24213 /* Look for the `:' that begins the list. */
24214 cp_parser_require (parser, CPP_COLON, RT_COLON);
24216 /* Scan the base-specifier-list. */
24217 while (true)
24219 cp_token *token;
24220 tree base;
24221 bool pack_expansion_p = false;
24223 /* Look for the base-specifier. */
24224 base = cp_parser_base_specifier (parser);
24225 /* Look for the (optional) ellipsis. */
24226 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24228 /* Consume the `...'. */
24229 cp_lexer_consume_token (parser->lexer);
24231 pack_expansion_p = true;
24234 /* Add BASE to the front of the list. */
24235 if (base && base != error_mark_node)
24237 if (pack_expansion_p)
24238 /* Make this a pack expansion type. */
24239 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24241 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24243 TREE_CHAIN (base) = bases;
24244 bases = base;
24247 /* Peek at the next token. */
24248 token = cp_lexer_peek_token (parser->lexer);
24249 /* If it's not a comma, then the list is complete. */
24250 if (token->type != CPP_COMMA)
24251 break;
24252 /* Consume the `,'. */
24253 cp_lexer_consume_token (parser->lexer);
24256 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24257 base class had a qualified name. However, the next name that
24258 appears is certainly not qualified. */
24259 parser->scope = NULL_TREE;
24260 parser->qualifying_scope = NULL_TREE;
24261 parser->object_scope = NULL_TREE;
24263 return nreverse (bases);
24266 /* Parse a base-specifier.
24268 base-specifier:
24269 :: [opt] nested-name-specifier [opt] class-name
24270 virtual access-specifier [opt] :: [opt] nested-name-specifier
24271 [opt] class-name
24272 access-specifier virtual [opt] :: [opt] nested-name-specifier
24273 [opt] class-name
24275 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24276 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24277 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24278 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24280 static tree
24281 cp_parser_base_specifier (cp_parser* parser)
24283 cp_token *token;
24284 bool done = false;
24285 bool virtual_p = false;
24286 bool duplicate_virtual_error_issued_p = false;
24287 bool duplicate_access_error_issued_p = false;
24288 bool class_scope_p, template_p;
24289 tree access = access_default_node;
24290 tree type;
24292 /* Process the optional `virtual' and `access-specifier'. */
24293 while (!done)
24295 /* Peek at the next token. */
24296 token = cp_lexer_peek_token (parser->lexer);
24297 /* Process `virtual'. */
24298 switch (token->keyword)
24300 case RID_VIRTUAL:
24301 /* If `virtual' appears more than once, issue an error. */
24302 if (virtual_p && !duplicate_virtual_error_issued_p)
24304 cp_parser_error (parser,
24305 "%<virtual%> specified more than once in base-specifier");
24306 duplicate_virtual_error_issued_p = true;
24309 virtual_p = true;
24311 /* Consume the `virtual' token. */
24312 cp_lexer_consume_token (parser->lexer);
24314 break;
24316 case RID_PUBLIC:
24317 case RID_PROTECTED:
24318 case RID_PRIVATE:
24319 /* If more than one access specifier appears, issue an
24320 error. */
24321 if (access != access_default_node
24322 && !duplicate_access_error_issued_p)
24324 cp_parser_error (parser,
24325 "more than one access specifier in base-specifier");
24326 duplicate_access_error_issued_p = true;
24329 access = ridpointers[(int) token->keyword];
24331 /* Consume the access-specifier. */
24332 cp_lexer_consume_token (parser->lexer);
24334 break;
24336 default:
24337 done = true;
24338 break;
24341 /* It is not uncommon to see programs mechanically, erroneously, use
24342 the 'typename' keyword to denote (dependent) qualified types
24343 as base classes. */
24344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24346 token = cp_lexer_peek_token (parser->lexer);
24347 if (!processing_template_decl)
24348 error_at (token->location,
24349 "keyword %<typename%> not allowed outside of templates");
24350 else
24351 error_at (token->location,
24352 "keyword %<typename%> not allowed in this context "
24353 "(the base class is implicitly a type)");
24354 cp_lexer_consume_token (parser->lexer);
24357 /* Look for the optional `::' operator. */
24358 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24359 /* Look for the nested-name-specifier. The simplest way to
24360 implement:
24362 [temp.res]
24364 The keyword `typename' is not permitted in a base-specifier or
24365 mem-initializer; in these contexts a qualified name that
24366 depends on a template-parameter is implicitly assumed to be a
24367 type name.
24369 is to pretend that we have seen the `typename' keyword at this
24370 point. */
24371 cp_parser_nested_name_specifier_opt (parser,
24372 /*typename_keyword_p=*/true,
24373 /*check_dependency_p=*/true,
24374 /*type_p=*/true,
24375 /*is_declaration=*/true);
24376 /* If the base class is given by a qualified name, assume that names
24377 we see are type names or templates, as appropriate. */
24378 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24379 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24381 if (!parser->scope
24382 && cp_lexer_next_token_is_decltype (parser->lexer))
24383 /* DR 950 allows decltype as a base-specifier. */
24384 type = cp_parser_decltype (parser);
24385 else
24387 /* Otherwise, look for the class-name. */
24388 type = cp_parser_class_name (parser,
24389 class_scope_p,
24390 template_p,
24391 typename_type,
24392 /*check_dependency_p=*/true,
24393 /*class_head_p=*/false,
24394 /*is_declaration=*/true);
24395 type = TREE_TYPE (type);
24398 if (type == error_mark_node)
24399 return error_mark_node;
24401 return finish_base_specifier (type, access, virtual_p);
24404 /* Exception handling [gram.exception] */
24406 /* Parse an (optional) noexcept-specification.
24408 noexcept-specification:
24409 noexcept ( constant-expression ) [opt]
24411 If no noexcept-specification is present, returns NULL_TREE.
24412 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24413 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24414 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24415 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24416 in which case a boolean condition is returned instead. */
24418 static tree
24419 cp_parser_noexcept_specification_opt (cp_parser* parser,
24420 bool require_constexpr,
24421 bool* consumed_expr,
24422 bool return_cond)
24424 cp_token *token;
24425 const char *saved_message;
24427 /* Peek at the next token. */
24428 token = cp_lexer_peek_token (parser->lexer);
24430 /* Is it a noexcept-specification? */
24431 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24433 tree expr;
24434 cp_lexer_consume_token (parser->lexer);
24436 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24438 matching_parens parens;
24439 parens.consume_open (parser);
24441 if (require_constexpr)
24443 /* Types may not be defined in an exception-specification. */
24444 saved_message = parser->type_definition_forbidden_message;
24445 parser->type_definition_forbidden_message
24446 = G_("types may not be defined in an exception-specification");
24448 expr = cp_parser_constant_expression (parser);
24450 /* Restore the saved message. */
24451 parser->type_definition_forbidden_message = saved_message;
24453 else
24455 expr = cp_parser_expression (parser);
24456 *consumed_expr = true;
24459 parens.require_close (parser);
24461 else
24463 expr = boolean_true_node;
24464 if (!require_constexpr)
24465 *consumed_expr = false;
24468 /* We cannot build a noexcept-spec right away because this will check
24469 that expr is a constexpr. */
24470 if (!return_cond)
24471 return build_noexcept_spec (expr, tf_warning_or_error);
24472 else
24473 return expr;
24475 else
24476 return NULL_TREE;
24479 /* Parse an (optional) exception-specification.
24481 exception-specification:
24482 throw ( type-id-list [opt] )
24484 Returns a TREE_LIST representing the exception-specification. The
24485 TREE_VALUE of each node is a type. */
24487 static tree
24488 cp_parser_exception_specification_opt (cp_parser* parser)
24490 cp_token *token;
24491 tree type_id_list;
24492 const char *saved_message;
24494 /* Peek at the next token. */
24495 token = cp_lexer_peek_token (parser->lexer);
24497 /* Is it a noexcept-specification? */
24498 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24499 false);
24500 if (type_id_list != NULL_TREE)
24501 return type_id_list;
24503 /* If it's not `throw', then there's no exception-specification. */
24504 if (!cp_parser_is_keyword (token, RID_THROW))
24505 return NULL_TREE;
24507 location_t loc = token->location;
24509 /* Consume the `throw'. */
24510 cp_lexer_consume_token (parser->lexer);
24512 /* Look for the `('. */
24513 matching_parens parens;
24514 parens.require_open (parser);
24516 /* Peek at the next token. */
24517 token = cp_lexer_peek_token (parser->lexer);
24518 /* If it's not a `)', then there is a type-id-list. */
24519 if (token->type != CPP_CLOSE_PAREN)
24521 /* Types may not be defined in an exception-specification. */
24522 saved_message = parser->type_definition_forbidden_message;
24523 parser->type_definition_forbidden_message
24524 = G_("types may not be defined in an exception-specification");
24525 /* Parse the type-id-list. */
24526 type_id_list = cp_parser_type_id_list (parser);
24527 /* Restore the saved message. */
24528 parser->type_definition_forbidden_message = saved_message;
24530 if (cxx_dialect >= cxx17)
24532 error_at (loc, "ISO C++17 does not allow dynamic exception "
24533 "specifications");
24534 type_id_list = NULL_TREE;
24536 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24537 warning_at (loc, OPT_Wdeprecated,
24538 "dynamic exception specifications are deprecated in "
24539 "C++11");
24541 /* In C++17, throw() is equivalent to noexcept (true). throw()
24542 is deprecated in C++11 and above as well, but is still widely used,
24543 so don't warn about it yet. */
24544 else if (cxx_dialect >= cxx17)
24545 type_id_list = noexcept_true_spec;
24546 else
24547 type_id_list = empty_except_spec;
24549 /* Look for the `)'. */
24550 parens.require_close (parser);
24552 return type_id_list;
24555 /* Parse an (optional) type-id-list.
24557 type-id-list:
24558 type-id ... [opt]
24559 type-id-list , type-id ... [opt]
24561 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24562 in the order that the types were presented. */
24564 static tree
24565 cp_parser_type_id_list (cp_parser* parser)
24567 tree types = NULL_TREE;
24569 while (true)
24571 cp_token *token;
24572 tree type;
24574 token = cp_lexer_peek_token (parser->lexer);
24576 /* Get the next type-id. */
24577 type = cp_parser_type_id (parser);
24578 /* Check for invalid 'auto'. */
24579 if (flag_concepts && type_uses_auto (type))
24581 error_at (token->location,
24582 "invalid use of %<auto%> in exception-specification");
24583 type = error_mark_node;
24585 /* Parse the optional ellipsis. */
24586 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24588 /* Consume the `...'. */
24589 cp_lexer_consume_token (parser->lexer);
24591 /* Turn the type into a pack expansion expression. */
24592 type = make_pack_expansion (type);
24594 /* Add it to the list. */
24595 types = add_exception_specifier (types, type, /*complain=*/1);
24596 /* Peek at the next token. */
24597 token = cp_lexer_peek_token (parser->lexer);
24598 /* If it is not a `,', we are done. */
24599 if (token->type != CPP_COMMA)
24600 break;
24601 /* Consume the `,'. */
24602 cp_lexer_consume_token (parser->lexer);
24605 return nreverse (types);
24608 /* Parse a try-block.
24610 try-block:
24611 try compound-statement handler-seq */
24613 static tree
24614 cp_parser_try_block (cp_parser* parser)
24616 tree try_block;
24618 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24619 if (parser->in_function_body
24620 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24621 error ("%<try%> in %<constexpr%> function");
24623 try_block = begin_try_block ();
24624 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24625 finish_try_block (try_block);
24626 cp_parser_handler_seq (parser);
24627 finish_handler_sequence (try_block);
24629 return try_block;
24632 /* Parse a function-try-block.
24634 function-try-block:
24635 try ctor-initializer [opt] function-body handler-seq */
24637 static void
24638 cp_parser_function_try_block (cp_parser* parser)
24640 tree compound_stmt;
24641 tree try_block;
24643 /* Look for the `try' keyword. */
24644 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24645 return;
24646 /* Let the rest of the front end know where we are. */
24647 try_block = begin_function_try_block (&compound_stmt);
24648 /* Parse the function-body. */
24649 cp_parser_ctor_initializer_opt_and_function_body
24650 (parser, /*in_function_try_block=*/true);
24651 /* We're done with the `try' part. */
24652 finish_function_try_block (try_block);
24653 /* Parse the handlers. */
24654 cp_parser_handler_seq (parser);
24655 /* We're done with the handlers. */
24656 finish_function_handler_sequence (try_block, compound_stmt);
24659 /* Parse a handler-seq.
24661 handler-seq:
24662 handler handler-seq [opt] */
24664 static void
24665 cp_parser_handler_seq (cp_parser* parser)
24667 while (true)
24669 cp_token *token;
24671 /* Parse the handler. */
24672 cp_parser_handler (parser);
24673 /* Peek at the next token. */
24674 token = cp_lexer_peek_token (parser->lexer);
24675 /* If it's not `catch' then there are no more handlers. */
24676 if (!cp_parser_is_keyword (token, RID_CATCH))
24677 break;
24681 /* Parse a handler.
24683 handler:
24684 catch ( exception-declaration ) compound-statement */
24686 static void
24687 cp_parser_handler (cp_parser* parser)
24689 tree handler;
24690 tree declaration;
24692 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24693 handler = begin_handler ();
24694 matching_parens parens;
24695 parens.require_open (parser);
24696 declaration = cp_parser_exception_declaration (parser);
24697 finish_handler_parms (declaration, handler);
24698 parens.require_close (parser);
24699 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24700 finish_handler (handler);
24703 /* Parse an exception-declaration.
24705 exception-declaration:
24706 type-specifier-seq declarator
24707 type-specifier-seq abstract-declarator
24708 type-specifier-seq
24711 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24712 ellipsis variant is used. */
24714 static tree
24715 cp_parser_exception_declaration (cp_parser* parser)
24717 cp_decl_specifier_seq type_specifiers;
24718 cp_declarator *declarator;
24719 const char *saved_message;
24721 /* If it's an ellipsis, it's easy to handle. */
24722 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24724 /* Consume the `...' token. */
24725 cp_lexer_consume_token (parser->lexer);
24726 return NULL_TREE;
24729 /* Types may not be defined in exception-declarations. */
24730 saved_message = parser->type_definition_forbidden_message;
24731 parser->type_definition_forbidden_message
24732 = G_("types may not be defined in exception-declarations");
24734 /* Parse the type-specifier-seq. */
24735 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24736 /*is_trailing_return=*/false,
24737 &type_specifiers);
24738 /* If it's a `)', then there is no declarator. */
24739 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24740 declarator = NULL;
24741 else
24742 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24743 /*ctor_dtor_or_conv_p=*/NULL,
24744 /*parenthesized_p=*/NULL,
24745 /*member_p=*/false,
24746 /*friend_p=*/false);
24748 /* Restore the saved message. */
24749 parser->type_definition_forbidden_message = saved_message;
24751 if (!type_specifiers.any_specifiers_p)
24752 return error_mark_node;
24754 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24757 /* Parse a throw-expression.
24759 throw-expression:
24760 throw assignment-expression [opt]
24762 Returns a THROW_EXPR representing the throw-expression. */
24764 static tree
24765 cp_parser_throw_expression (cp_parser* parser)
24767 tree expression;
24768 cp_token* token;
24770 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24771 token = cp_lexer_peek_token (parser->lexer);
24772 /* Figure out whether or not there is an assignment-expression
24773 following the "throw" keyword. */
24774 if (token->type == CPP_COMMA
24775 || token->type == CPP_SEMICOLON
24776 || token->type == CPP_CLOSE_PAREN
24777 || token->type == CPP_CLOSE_SQUARE
24778 || token->type == CPP_CLOSE_BRACE
24779 || token->type == CPP_COLON)
24780 expression = NULL_TREE;
24781 else
24782 expression = cp_parser_assignment_expression (parser);
24784 return build_throw (expression);
24787 /* GNU Extensions */
24789 /* Parse an (optional) asm-specification.
24791 asm-specification:
24792 asm ( string-literal )
24794 If the asm-specification is present, returns a STRING_CST
24795 corresponding to the string-literal. Otherwise, returns
24796 NULL_TREE. */
24798 static tree
24799 cp_parser_asm_specification_opt (cp_parser* parser)
24801 cp_token *token;
24802 tree asm_specification;
24804 /* Peek at the next token. */
24805 token = cp_lexer_peek_token (parser->lexer);
24806 /* If the next token isn't the `asm' keyword, then there's no
24807 asm-specification. */
24808 if (!cp_parser_is_keyword (token, RID_ASM))
24809 return NULL_TREE;
24811 /* Consume the `asm' token. */
24812 cp_lexer_consume_token (parser->lexer);
24813 /* Look for the `('. */
24814 matching_parens parens;
24815 parens.require_open (parser);
24817 /* Look for the string-literal. */
24818 asm_specification = cp_parser_string_literal (parser, false, false);
24820 /* Look for the `)'. */
24821 parens.require_close (parser);
24823 return asm_specification;
24826 /* Parse an asm-operand-list.
24828 asm-operand-list:
24829 asm-operand
24830 asm-operand-list , asm-operand
24832 asm-operand:
24833 string-literal ( expression )
24834 [ string-literal ] string-literal ( expression )
24836 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24837 each node is the expression. The TREE_PURPOSE is itself a
24838 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24839 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24840 is a STRING_CST for the string literal before the parenthesis. Returns
24841 ERROR_MARK_NODE if any of the operands are invalid. */
24843 static tree
24844 cp_parser_asm_operand_list (cp_parser* parser)
24846 tree asm_operands = NULL_TREE;
24847 bool invalid_operands = false;
24849 while (true)
24851 tree string_literal;
24852 tree expression;
24853 tree name;
24855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24857 /* Consume the `[' token. */
24858 cp_lexer_consume_token (parser->lexer);
24859 /* Read the operand name. */
24860 name = cp_parser_identifier (parser);
24861 if (name != error_mark_node)
24862 name = build_string (IDENTIFIER_LENGTH (name),
24863 IDENTIFIER_POINTER (name));
24864 /* Look for the closing `]'. */
24865 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24867 else
24868 name = NULL_TREE;
24869 /* Look for the string-literal. */
24870 string_literal = cp_parser_string_literal (parser, false, false);
24872 /* Look for the `('. */
24873 matching_parens parens;
24874 parens.require_open (parser);
24875 /* Parse the expression. */
24876 expression = cp_parser_expression (parser);
24877 /* Look for the `)'. */
24878 parens.require_close (parser);
24880 if (name == error_mark_node
24881 || string_literal == error_mark_node
24882 || expression == error_mark_node)
24883 invalid_operands = true;
24885 /* Add this operand to the list. */
24886 asm_operands = tree_cons (build_tree_list (name, string_literal),
24887 expression,
24888 asm_operands);
24889 /* If the next token is not a `,', there are no more
24890 operands. */
24891 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24892 break;
24893 /* Consume the `,'. */
24894 cp_lexer_consume_token (parser->lexer);
24897 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24900 /* Parse an asm-clobber-list.
24902 asm-clobber-list:
24903 string-literal
24904 asm-clobber-list , string-literal
24906 Returns a TREE_LIST, indicating the clobbers in the order that they
24907 appeared. The TREE_VALUE of each node is a STRING_CST. */
24909 static tree
24910 cp_parser_asm_clobber_list (cp_parser* parser)
24912 tree clobbers = NULL_TREE;
24914 while (true)
24916 tree string_literal;
24918 /* Look for the string literal. */
24919 string_literal = cp_parser_string_literal (parser, false, false);
24920 /* Add it to the list. */
24921 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24922 /* If the next token is not a `,', then the list is
24923 complete. */
24924 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24925 break;
24926 /* Consume the `,' token. */
24927 cp_lexer_consume_token (parser->lexer);
24930 return clobbers;
24933 /* Parse an asm-label-list.
24935 asm-label-list:
24936 identifier
24937 asm-label-list , identifier
24939 Returns a TREE_LIST, indicating the labels in the order that they
24940 appeared. The TREE_VALUE of each node is a label. */
24942 static tree
24943 cp_parser_asm_label_list (cp_parser* parser)
24945 tree labels = NULL_TREE;
24947 while (true)
24949 tree identifier, label, name;
24951 /* Look for the identifier. */
24952 identifier = cp_parser_identifier (parser);
24953 if (!error_operand_p (identifier))
24955 label = lookup_label (identifier);
24956 if (TREE_CODE (label) == LABEL_DECL)
24958 TREE_USED (label) = 1;
24959 check_goto (label);
24960 name = build_string (IDENTIFIER_LENGTH (identifier),
24961 IDENTIFIER_POINTER (identifier));
24962 labels = tree_cons (name, label, labels);
24965 /* If the next token is not a `,', then the list is
24966 complete. */
24967 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24968 break;
24969 /* Consume the `,' token. */
24970 cp_lexer_consume_token (parser->lexer);
24973 return nreverse (labels);
24976 /* Return TRUE iff the next tokens in the stream are possibly the
24977 beginning of a GNU extension attribute. */
24979 static bool
24980 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24982 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24985 /* Return TRUE iff the next tokens in the stream are possibly the
24986 beginning of a standard C++-11 attribute specifier. */
24988 static bool
24989 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24991 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24994 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24995 beginning of a standard C++-11 attribute specifier. */
24997 static bool
24998 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25000 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25002 return (cxx_dialect >= cxx11
25003 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25004 || (token->type == CPP_OPEN_SQUARE
25005 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25006 && token->type == CPP_OPEN_SQUARE)));
25009 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25010 beginning of a GNU extension attribute. */
25012 static bool
25013 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25015 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25017 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25020 /* Return true iff the next tokens can be the beginning of either a
25021 GNU attribute list, or a standard C++11 attribute sequence. */
25023 static bool
25024 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25026 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25027 || cp_next_tokens_can_be_std_attribute_p (parser));
25030 /* Return true iff the next Nth tokens can be the beginning of either
25031 a GNU attribute list, or a standard C++11 attribute sequence. */
25033 static bool
25034 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25036 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25037 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25040 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25041 of GNU attributes, or return NULL. */
25043 static tree
25044 cp_parser_attributes_opt (cp_parser *parser)
25046 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25047 return cp_parser_gnu_attributes_opt (parser);
25048 return cp_parser_std_attribute_spec_seq (parser);
25051 /* Parse an (optional) series of attributes.
25053 attributes:
25054 attributes attribute
25056 attribute:
25057 __attribute__ (( attribute-list [opt] ))
25059 The return value is as for cp_parser_gnu_attribute_list. */
25061 static tree
25062 cp_parser_gnu_attributes_opt (cp_parser* parser)
25064 tree attributes = NULL_TREE;
25066 temp_override<bool> cleanup
25067 (parser->auto_is_implicit_function_template_parm_p, false);
25069 while (true)
25071 cp_token *token;
25072 tree attribute_list;
25073 bool ok = true;
25075 /* Peek at the next token. */
25076 token = cp_lexer_peek_token (parser->lexer);
25077 /* If it's not `__attribute__', then we're done. */
25078 if (token->keyword != RID_ATTRIBUTE)
25079 break;
25081 /* Consume the `__attribute__' keyword. */
25082 cp_lexer_consume_token (parser->lexer);
25083 /* Look for the two `(' tokens. */
25084 matching_parens outer_parens;
25085 outer_parens.require_open (parser);
25086 matching_parens inner_parens;
25087 inner_parens.require_open (parser);
25089 /* Peek at the next token. */
25090 token = cp_lexer_peek_token (parser->lexer);
25091 if (token->type != CPP_CLOSE_PAREN)
25092 /* Parse the attribute-list. */
25093 attribute_list = cp_parser_gnu_attribute_list (parser);
25094 else
25095 /* If the next token is a `)', then there is no attribute
25096 list. */
25097 attribute_list = NULL;
25099 /* Look for the two `)' tokens. */
25100 if (!inner_parens.require_close (parser))
25101 ok = false;
25102 if (!outer_parens.require_close (parser))
25103 ok = false;
25104 if (!ok)
25105 cp_parser_skip_to_end_of_statement (parser);
25107 /* Add these new attributes to the list. */
25108 attributes = attr_chainon (attributes, attribute_list);
25111 return attributes;
25114 /* Parse a GNU attribute-list.
25116 attribute-list:
25117 attribute
25118 attribute-list , attribute
25120 attribute:
25121 identifier
25122 identifier ( identifier )
25123 identifier ( identifier , expression-list )
25124 identifier ( expression-list )
25126 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25127 to an attribute. The TREE_PURPOSE of each node is the identifier
25128 indicating which attribute is in use. The TREE_VALUE represents
25129 the arguments, if any. */
25131 static tree
25132 cp_parser_gnu_attribute_list (cp_parser* parser)
25134 tree attribute_list = NULL_TREE;
25135 bool save_translate_strings_p = parser->translate_strings_p;
25137 parser->translate_strings_p = false;
25138 while (true)
25140 cp_token *token;
25141 tree identifier;
25142 tree attribute;
25144 /* Look for the identifier. We also allow keywords here; for
25145 example `__attribute__ ((const))' is legal. */
25146 token = cp_lexer_peek_token (parser->lexer);
25147 if (token->type == CPP_NAME
25148 || token->type == CPP_KEYWORD)
25150 tree arguments = NULL_TREE;
25152 /* Consume the token, but save it since we need it for the
25153 SIMD enabled function parsing. */
25154 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25156 /* Save away the identifier that indicates which attribute
25157 this is. */
25158 identifier = (token->type == CPP_KEYWORD)
25159 /* For keywords, use the canonical spelling, not the
25160 parsed identifier. */
25161 ? ridpointers[(int) token->keyword]
25162 : id_token->u.value;
25164 identifier = canonicalize_attr_name (identifier);
25165 attribute = build_tree_list (identifier, NULL_TREE);
25167 /* Peek at the next token. */
25168 token = cp_lexer_peek_token (parser->lexer);
25169 /* If it's an `(', then parse the attribute arguments. */
25170 if (token->type == CPP_OPEN_PAREN)
25172 vec<tree, va_gc> *vec;
25173 int attr_flag = (attribute_takes_identifier_p (identifier)
25174 ? id_attr : normal_attr);
25175 vec = cp_parser_parenthesized_expression_list
25176 (parser, attr_flag, /*cast_p=*/false,
25177 /*allow_expansion_p=*/false,
25178 /*non_constant_p=*/NULL);
25179 if (vec == NULL)
25180 arguments = error_mark_node;
25181 else
25183 arguments = build_tree_list_vec (vec);
25184 release_tree_vector (vec);
25186 /* Save the arguments away. */
25187 TREE_VALUE (attribute) = arguments;
25190 if (arguments != error_mark_node)
25192 /* Add this attribute to the list. */
25193 TREE_CHAIN (attribute) = attribute_list;
25194 attribute_list = attribute;
25197 token = cp_lexer_peek_token (parser->lexer);
25199 /* Now, look for more attributes. If the next token isn't a
25200 `,', we're done. */
25201 if (token->type != CPP_COMMA)
25202 break;
25204 /* Consume the comma and keep going. */
25205 cp_lexer_consume_token (parser->lexer);
25207 parser->translate_strings_p = save_translate_strings_p;
25209 /* We built up the list in reverse order. */
25210 return nreverse (attribute_list);
25213 /* Parse a standard C++11 attribute.
25215 The returned representation is a TREE_LIST which TREE_PURPOSE is
25216 the scoped name of the attribute, and the TREE_VALUE is its
25217 arguments list.
25219 Note that the scoped name of the attribute is itself a TREE_LIST
25220 which TREE_PURPOSE is the namespace of the attribute, and
25221 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25222 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25223 and which TREE_PURPOSE is directly the attribute name.
25225 Clients of the attribute code should use get_attribute_namespace
25226 and get_attribute_name to get the actual namespace and name of
25227 attributes, regardless of their being GNU or C++11 attributes.
25229 attribute:
25230 attribute-token attribute-argument-clause [opt]
25232 attribute-token:
25233 identifier
25234 attribute-scoped-token
25236 attribute-scoped-token:
25237 attribute-namespace :: identifier
25239 attribute-namespace:
25240 identifier
25242 attribute-argument-clause:
25243 ( balanced-token-seq )
25245 balanced-token-seq:
25246 balanced-token [opt]
25247 balanced-token-seq balanced-token
25249 balanced-token:
25250 ( balanced-token-seq )
25251 [ balanced-token-seq ]
25252 { balanced-token-seq }. */
25254 static tree
25255 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25257 tree attribute, attr_id = NULL_TREE, arguments;
25258 cp_token *token;
25260 temp_override<bool> cleanup
25261 (parser->auto_is_implicit_function_template_parm_p, false);
25263 /* First, parse name of the attribute, a.k.a attribute-token. */
25265 token = cp_lexer_peek_token (parser->lexer);
25266 if (token->type == CPP_NAME)
25267 attr_id = token->u.value;
25268 else if (token->type == CPP_KEYWORD)
25269 attr_id = ridpointers[(int) token->keyword];
25270 else if (token->flags & NAMED_OP)
25271 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25273 if (attr_id == NULL_TREE)
25274 return NULL_TREE;
25276 cp_lexer_consume_token (parser->lexer);
25278 token = cp_lexer_peek_token (parser->lexer);
25279 if (token->type == CPP_SCOPE)
25281 /* We are seeing a scoped attribute token. */
25283 cp_lexer_consume_token (parser->lexer);
25284 if (attr_ns)
25285 error_at (token->location, "attribute using prefix used together "
25286 "with scoped attribute token");
25287 attr_ns = attr_id;
25289 token = cp_lexer_consume_token (parser->lexer);
25290 if (token->type == CPP_NAME)
25291 attr_id = token->u.value;
25292 else if (token->type == CPP_KEYWORD)
25293 attr_id = ridpointers[(int) token->keyword];
25294 else if (token->flags & NAMED_OP)
25295 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25296 else
25298 error_at (token->location,
25299 "expected an identifier for the attribute name");
25300 return error_mark_node;
25303 attr_id = canonicalize_attr_name (attr_id);
25304 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25305 NULL_TREE);
25306 token = cp_lexer_peek_token (parser->lexer);
25308 else if (attr_ns)
25309 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25310 NULL_TREE);
25311 else
25313 attr_id = canonicalize_attr_name (attr_id);
25314 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25315 NULL_TREE);
25316 /* C++11 noreturn attribute is equivalent to GNU's. */
25317 if (is_attribute_p ("noreturn", attr_id))
25318 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25319 /* C++14 deprecated attribute is equivalent to GNU's. */
25320 else if (is_attribute_p ("deprecated", attr_id))
25321 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25322 /* C++17 fallthrough attribute is equivalent to GNU's. */
25323 else if (is_attribute_p ("fallthrough", attr_id))
25324 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25325 /* Transactional Memory TS optimize_for_synchronized attribute is
25326 equivalent to GNU transaction_callable. */
25327 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25328 TREE_PURPOSE (attribute)
25329 = get_identifier ("transaction_callable");
25330 /* Transactional Memory attributes are GNU attributes. */
25331 else if (tm_attr_to_mask (attr_id))
25332 TREE_PURPOSE (attribute) = attr_id;
25335 /* Now parse the optional argument clause of the attribute. */
25337 if (token->type != CPP_OPEN_PAREN)
25338 return attribute;
25341 vec<tree, va_gc> *vec;
25342 int attr_flag = normal_attr;
25344 if (attr_ns == get_identifier ("gnu")
25345 && attribute_takes_identifier_p (attr_id))
25346 /* A GNU attribute that takes an identifier in parameter. */
25347 attr_flag = id_attr;
25349 vec = cp_parser_parenthesized_expression_list
25350 (parser, attr_flag, /*cast_p=*/false,
25351 /*allow_expansion_p=*/true,
25352 /*non_constant_p=*/NULL);
25353 if (vec == NULL)
25354 arguments = error_mark_node;
25355 else
25357 arguments = build_tree_list_vec (vec);
25358 release_tree_vector (vec);
25361 if (arguments == error_mark_node)
25362 attribute = error_mark_node;
25363 else
25364 TREE_VALUE (attribute) = arguments;
25367 return attribute;
25370 /* Check that the attribute ATTRIBUTE appears at most once in the
25371 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25372 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25373 isn't implemented yet in GCC. */
25375 static void
25376 cp_parser_check_std_attribute (tree attributes, tree attribute)
25378 if (attributes)
25380 tree name = get_attribute_name (attribute);
25381 if (is_attribute_p ("noreturn", name)
25382 && lookup_attribute ("noreturn", attributes))
25383 error ("attribute %<noreturn%> can appear at most once "
25384 "in an attribute-list");
25385 else if (is_attribute_p ("deprecated", name)
25386 && lookup_attribute ("deprecated", attributes))
25387 error ("attribute %<deprecated%> can appear at most once "
25388 "in an attribute-list");
25392 /* Parse a list of standard C++-11 attributes.
25394 attribute-list:
25395 attribute [opt]
25396 attribute-list , attribute[opt]
25397 attribute ...
25398 attribute-list , attribute ...
25401 static tree
25402 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25404 tree attributes = NULL_TREE, attribute = NULL_TREE;
25405 cp_token *token = NULL;
25407 while (true)
25409 attribute = cp_parser_std_attribute (parser, attr_ns);
25410 if (attribute == error_mark_node)
25411 break;
25412 if (attribute != NULL_TREE)
25414 cp_parser_check_std_attribute (attributes, attribute);
25415 TREE_CHAIN (attribute) = attributes;
25416 attributes = attribute;
25418 token = cp_lexer_peek_token (parser->lexer);
25419 if (token->type == CPP_ELLIPSIS)
25421 cp_lexer_consume_token (parser->lexer);
25422 if (attribute == NULL_TREE)
25423 error_at (token->location,
25424 "expected attribute before %<...%>");
25425 else
25427 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25428 if (pack == error_mark_node)
25429 return error_mark_node;
25430 TREE_VALUE (attribute) = pack;
25432 token = cp_lexer_peek_token (parser->lexer);
25434 if (token->type != CPP_COMMA)
25435 break;
25436 cp_lexer_consume_token (parser->lexer);
25438 attributes = nreverse (attributes);
25439 return attributes;
25442 /* Parse a standard C++-11 attribute specifier.
25444 attribute-specifier:
25445 [ [ attribute-using-prefix [opt] attribute-list ] ]
25446 alignment-specifier
25448 attribute-using-prefix:
25449 using attribute-namespace :
25451 alignment-specifier:
25452 alignas ( type-id ... [opt] )
25453 alignas ( alignment-expression ... [opt] ). */
25455 static tree
25456 cp_parser_std_attribute_spec (cp_parser *parser)
25458 tree attributes = NULL_TREE;
25459 cp_token *token = cp_lexer_peek_token (parser->lexer);
25461 if (token->type == CPP_OPEN_SQUARE
25462 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25464 tree attr_ns = NULL_TREE;
25466 cp_lexer_consume_token (parser->lexer);
25467 cp_lexer_consume_token (parser->lexer);
25469 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25471 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25472 if (token->type == CPP_NAME)
25473 attr_ns = token->u.value;
25474 else if (token->type == CPP_KEYWORD)
25475 attr_ns = ridpointers[(int) token->keyword];
25476 else if (token->flags & NAMED_OP)
25477 attr_ns = get_identifier (cpp_type2name (token->type,
25478 token->flags));
25479 if (attr_ns
25480 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25482 if (cxx_dialect < cxx17
25483 && !in_system_header_at (input_location))
25484 pedwarn (input_location, 0,
25485 "attribute using prefix only available "
25486 "with -std=c++17 or -std=gnu++17");
25488 cp_lexer_consume_token (parser->lexer);
25489 cp_lexer_consume_token (parser->lexer);
25490 cp_lexer_consume_token (parser->lexer);
25492 else
25493 attr_ns = NULL_TREE;
25496 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25498 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25499 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25500 cp_parser_skip_to_end_of_statement (parser);
25501 else
25502 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25503 when we are sure that we have actually parsed them. */
25504 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25506 else
25508 tree alignas_expr;
25510 /* Look for an alignment-specifier. */
25512 token = cp_lexer_peek_token (parser->lexer);
25514 if (token->type != CPP_KEYWORD
25515 || token->keyword != RID_ALIGNAS)
25516 return NULL_TREE;
25518 cp_lexer_consume_token (parser->lexer);
25519 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25521 matching_parens parens;
25522 if (!parens.require_open (parser))
25523 return error_mark_node;
25525 cp_parser_parse_tentatively (parser);
25526 alignas_expr = cp_parser_type_id (parser);
25528 if (!cp_parser_parse_definitely (parser))
25530 alignas_expr = cp_parser_assignment_expression (parser);
25531 if (alignas_expr == error_mark_node)
25532 cp_parser_skip_to_end_of_statement (parser);
25533 if (alignas_expr == NULL_TREE
25534 || alignas_expr == error_mark_node)
25535 return alignas_expr;
25538 alignas_expr = cxx_alignas_expr (alignas_expr);
25539 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25541 /* Handle alignas (pack...). */
25542 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25544 cp_lexer_consume_token (parser->lexer);
25545 alignas_expr = make_pack_expansion (alignas_expr);
25548 /* Something went wrong, so don't build the attribute. */
25549 if (alignas_expr == error_mark_node)
25550 return error_mark_node;
25552 if (!parens.require_close (parser))
25553 return error_mark_node;
25555 /* Build the C++-11 representation of an 'aligned'
25556 attribute. */
25557 attributes =
25558 build_tree_list (build_tree_list (get_identifier ("gnu"),
25559 get_identifier ("aligned")),
25560 alignas_expr);
25563 return attributes;
25566 /* Parse a standard C++-11 attribute-specifier-seq.
25568 attribute-specifier-seq:
25569 attribute-specifier-seq [opt] attribute-specifier
25572 static tree
25573 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25575 tree attr_specs = NULL_TREE;
25576 tree attr_last = NULL_TREE;
25578 while (true)
25580 tree attr_spec = cp_parser_std_attribute_spec (parser);
25581 if (attr_spec == NULL_TREE)
25582 break;
25583 if (attr_spec == error_mark_node)
25584 return error_mark_node;
25586 if (attr_last)
25587 TREE_CHAIN (attr_last) = attr_spec;
25588 else
25589 attr_specs = attr_last = attr_spec;
25590 attr_last = tree_last (attr_last);
25593 return attr_specs;
25596 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25597 return index of the first token after balanced-token, or N on failure. */
25599 static size_t
25600 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25602 size_t orig_n = n;
25603 int nparens = 0, nbraces = 0, nsquares = 0;
25605 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25607 case CPP_EOF:
25608 case CPP_PRAGMA_EOL:
25609 /* Ran out of tokens. */
25610 return orig_n;
25611 case CPP_OPEN_PAREN:
25612 ++nparens;
25613 break;
25614 case CPP_OPEN_BRACE:
25615 ++nbraces;
25616 break;
25617 case CPP_OPEN_SQUARE:
25618 ++nsquares;
25619 break;
25620 case CPP_CLOSE_PAREN:
25621 --nparens;
25622 break;
25623 case CPP_CLOSE_BRACE:
25624 --nbraces;
25625 break;
25626 case CPP_CLOSE_SQUARE:
25627 --nsquares;
25628 break;
25629 default:
25630 break;
25632 while (nparens || nbraces || nsquares);
25633 return n;
25636 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25637 return index of the first token after the GNU attribute tokens, or N on
25638 failure. */
25640 static size_t
25641 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25643 while (true)
25645 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25646 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25647 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25648 break;
25650 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25651 if (n2 == n + 2)
25652 break;
25653 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25654 break;
25655 n = n2 + 1;
25657 return n;
25660 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25661 next token), return index of the first token after the standard C++11
25662 attribute tokens, or N on failure. */
25664 static size_t
25665 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25667 while (true)
25669 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25670 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25672 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25673 if (n2 == n + 1)
25674 break;
25675 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25676 break;
25677 n = n2 + 1;
25679 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25680 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25682 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25683 if (n2 == n + 1)
25684 break;
25685 n = n2;
25687 else
25688 break;
25690 return n;
25693 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25694 as the next token), return index of the first token after the attribute
25695 tokens, or N on failure. */
25697 static size_t
25698 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25700 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25701 return cp_parser_skip_gnu_attributes_opt (parser, n);
25702 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25705 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25706 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25707 current value of the PEDANTIC flag, regardless of whether or not
25708 the `__extension__' keyword is present. The caller is responsible
25709 for restoring the value of the PEDANTIC flag. */
25711 static bool
25712 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25714 /* Save the old value of the PEDANTIC flag. */
25715 *saved_pedantic = pedantic;
25717 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25719 /* Consume the `__extension__' token. */
25720 cp_lexer_consume_token (parser->lexer);
25721 /* We're not being pedantic while the `__extension__' keyword is
25722 in effect. */
25723 pedantic = 0;
25725 return true;
25728 return false;
25731 /* Parse a label declaration.
25733 label-declaration:
25734 __label__ label-declarator-seq ;
25736 label-declarator-seq:
25737 identifier , label-declarator-seq
25738 identifier */
25740 static void
25741 cp_parser_label_declaration (cp_parser* parser)
25743 /* Look for the `__label__' keyword. */
25744 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25746 while (true)
25748 tree identifier;
25750 /* Look for an identifier. */
25751 identifier = cp_parser_identifier (parser);
25752 /* If we failed, stop. */
25753 if (identifier == error_mark_node)
25754 break;
25755 /* Declare it as a label. */
25756 finish_label_decl (identifier);
25757 /* If the next token is a `;', stop. */
25758 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25759 break;
25760 /* Look for the `,' separating the label declarations. */
25761 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25764 /* Look for the final `;'. */
25765 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25768 // -------------------------------------------------------------------------- //
25769 // Requires Clause
25771 // Parse a requires clause.
25773 // requires-clause:
25774 // 'requires' logical-or-expression
25776 // The required logical-or-expression must be a constant expression. Note
25777 // that we don't check that the expression is constepxr here. We defer until
25778 // we analyze constraints and then, we only check atomic constraints.
25779 static tree
25780 cp_parser_requires_clause (cp_parser *parser)
25782 // Parse the requires clause so that it is not automatically folded.
25783 ++processing_template_decl;
25784 tree expr = cp_parser_binary_expression (parser, false, false,
25785 PREC_NOT_OPERATOR, NULL);
25786 if (check_for_bare_parameter_packs (expr))
25787 expr = error_mark_node;
25788 --processing_template_decl;
25789 return expr;
25792 // Optionally parse a requires clause:
25793 static tree
25794 cp_parser_requires_clause_opt (cp_parser *parser)
25796 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25797 if (tok->keyword != RID_REQUIRES)
25799 if (!flag_concepts && tok->type == CPP_NAME
25800 && tok->u.value == ridpointers[RID_REQUIRES])
25802 error_at (cp_lexer_peek_token (parser->lexer)->location,
25803 "%<requires%> only available with -fconcepts");
25804 /* Parse and discard the requires-clause. */
25805 cp_lexer_consume_token (parser->lexer);
25806 cp_parser_requires_clause (parser);
25808 return NULL_TREE;
25810 cp_lexer_consume_token (parser->lexer);
25811 return cp_parser_requires_clause (parser);
25815 /*---------------------------------------------------------------------------
25816 Requires expressions
25817 ---------------------------------------------------------------------------*/
25819 /* Parse a requires expression
25821 requirement-expression:
25822 'requires' requirement-parameter-list [opt] requirement-body */
25823 static tree
25824 cp_parser_requires_expression (cp_parser *parser)
25826 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25827 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25829 /* A requires-expression shall appear only within a concept
25830 definition or a requires-clause.
25832 TODO: Implement this diagnostic correctly. */
25833 if (!processing_template_decl)
25835 error_at (loc, "a requires expression cannot appear outside a template");
25836 cp_parser_skip_to_end_of_statement (parser);
25837 return error_mark_node;
25840 tree parms, reqs;
25842 /* Local parameters are delared as variables within the scope
25843 of the expression. They are not visible past the end of
25844 the expression. Expressions within the requires-expression
25845 are unevaluated. */
25846 struct scope_sentinel
25848 scope_sentinel ()
25850 ++cp_unevaluated_operand;
25851 begin_scope (sk_block, NULL_TREE);
25854 ~scope_sentinel ()
25856 pop_bindings_and_leave_scope ();
25857 --cp_unevaluated_operand;
25859 } s;
25861 /* Parse the optional parameter list. */
25862 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25864 parms = cp_parser_requirement_parameter_list (parser);
25865 if (parms == error_mark_node)
25866 return error_mark_node;
25868 else
25869 parms = NULL_TREE;
25871 /* Parse the requirement body. */
25872 reqs = cp_parser_requirement_body (parser);
25873 if (reqs == error_mark_node)
25874 return error_mark_node;
25877 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25878 the parm chain. */
25879 grokparms (parms, &parms);
25880 return finish_requires_expr (parms, reqs);
25883 /* Parse a parameterized requirement.
25885 requirement-parameter-list:
25886 '(' parameter-declaration-clause ')' */
25887 static tree
25888 cp_parser_requirement_parameter_list (cp_parser *parser)
25890 matching_parens parens;
25891 if (!parens.require_open (parser))
25892 return error_mark_node;
25894 tree parms = cp_parser_parameter_declaration_clause (parser);
25896 if (!parens.require_close (parser))
25897 return error_mark_node;
25899 return parms;
25902 /* Parse the body of a requirement.
25904 requirement-body:
25905 '{' requirement-list '}' */
25906 static tree
25907 cp_parser_requirement_body (cp_parser *parser)
25909 matching_braces braces;
25910 if (!braces.require_open (parser))
25911 return error_mark_node;
25913 tree reqs = cp_parser_requirement_list (parser);
25915 if (!braces.require_close (parser))
25916 return error_mark_node;
25918 return reqs;
25921 /* Parse a list of requirements.
25923 requirement-list:
25924 requirement
25925 requirement-list ';' requirement[opt] */
25926 static tree
25927 cp_parser_requirement_list (cp_parser *parser)
25929 tree result = NULL_TREE;
25930 while (true)
25932 tree req = cp_parser_requirement (parser);
25933 if (req == error_mark_node)
25934 return error_mark_node;
25936 result = tree_cons (NULL_TREE, req, result);
25938 /* If we see a semi-colon, consume it. */
25939 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25940 cp_lexer_consume_token (parser->lexer);
25942 /* Stop processing at the end of the list. */
25943 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25944 break;
25947 /* Reverse the order of requirements so they are analyzed in
25948 declaration order. */
25949 return nreverse (result);
25952 /* Parse a syntactic requirement or type requirement.
25954 requirement:
25955 simple-requirement
25956 compound-requirement
25957 type-requirement
25958 nested-requirement */
25959 static tree
25960 cp_parser_requirement (cp_parser *parser)
25962 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25963 return cp_parser_compound_requirement (parser);
25964 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25965 return cp_parser_type_requirement (parser);
25966 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25967 return cp_parser_nested_requirement (parser);
25968 else
25969 return cp_parser_simple_requirement (parser);
25972 /* Parse a simple requirement.
25974 simple-requirement:
25975 expression ';' */
25976 static tree
25977 cp_parser_simple_requirement (cp_parser *parser)
25979 tree expr = cp_parser_expression (parser, NULL, false, false);
25980 if (!expr || expr == error_mark_node)
25981 return error_mark_node;
25983 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25984 return error_mark_node;
25986 return finish_simple_requirement (expr);
25989 /* Parse a type requirement
25991 type-requirement
25992 nested-name-specifier [opt] required-type-name ';'
25994 required-type-name:
25995 type-name
25996 'template' [opt] simple-template-id */
25997 static tree
25998 cp_parser_type_requirement (cp_parser *parser)
26000 cp_lexer_consume_token (parser->lexer);
26002 // Save the scope before parsing name specifiers.
26003 tree saved_scope = parser->scope;
26004 tree saved_object_scope = parser->object_scope;
26005 tree saved_qualifying_scope = parser->qualifying_scope;
26006 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26007 cp_parser_nested_name_specifier_opt (parser,
26008 /*typename_keyword_p=*/true,
26009 /*check_dependency_p=*/false,
26010 /*type_p=*/true,
26011 /*is_declaration=*/false);
26013 tree type;
26014 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26016 cp_lexer_consume_token (parser->lexer);
26017 type = cp_parser_template_id (parser,
26018 /*template_keyword_p=*/true,
26019 /*check_dependency=*/false,
26020 /*tag_type=*/none_type,
26021 /*is_declaration=*/false);
26022 type = make_typename_type (parser->scope, type, typename_type,
26023 /*complain=*/tf_error);
26025 else
26026 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26028 if (TREE_CODE (type) == TYPE_DECL)
26029 type = TREE_TYPE (type);
26031 parser->scope = saved_scope;
26032 parser->object_scope = saved_object_scope;
26033 parser->qualifying_scope = saved_qualifying_scope;
26035 if (type == error_mark_node)
26036 cp_parser_skip_to_end_of_statement (parser);
26038 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26039 return error_mark_node;
26040 if (type == error_mark_node)
26041 return error_mark_node;
26043 return finish_type_requirement (type);
26046 /* Parse a compound requirement
26048 compound-requirement:
26049 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26050 static tree
26051 cp_parser_compound_requirement (cp_parser *parser)
26053 /* Parse an expression enclosed in '{ }'s. */
26054 matching_braces braces;
26055 if (!braces.require_open (parser))
26056 return error_mark_node;
26058 tree expr = cp_parser_expression (parser, NULL, false, false);
26059 if (!expr || expr == error_mark_node)
26060 return error_mark_node;
26062 if (!braces.require_close (parser))
26063 return error_mark_node;
26065 /* Parse the optional noexcept. */
26066 bool noexcept_p = false;
26067 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26069 cp_lexer_consume_token (parser->lexer);
26070 noexcept_p = true;
26073 /* Parse the optional trailing return type. */
26074 tree type = NULL_TREE;
26075 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26077 cp_lexer_consume_token (parser->lexer);
26078 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26079 parser->in_result_type_constraint_p = true;
26080 type = cp_parser_trailing_type_id (parser);
26081 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26082 if (type == error_mark_node)
26083 return error_mark_node;
26086 return finish_compound_requirement (expr, type, noexcept_p);
26089 /* Parse a nested requirement. This is the same as a requires clause.
26091 nested-requirement:
26092 requires-clause */
26093 static tree
26094 cp_parser_nested_requirement (cp_parser *parser)
26096 cp_lexer_consume_token (parser->lexer);
26097 tree req = cp_parser_requires_clause (parser);
26098 if (req == error_mark_node)
26099 return error_mark_node;
26100 return finish_nested_requirement (req);
26103 /* Support Functions */
26105 /* Return the appropriate prefer_type argument for lookup_name_real based on
26106 tag_type and template_mem_access. */
26108 static inline int
26109 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26111 /* DR 141: When looking in the current enclosing context for a template-name
26112 after -> or ., only consider class templates. */
26113 if (template_mem_access)
26114 return 2;
26115 switch (tag_type)
26117 case none_type: return 0; // No preference.
26118 case scope_type: return 1; // Type or namespace.
26119 default: return 2; // Type only.
26123 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26124 NAME should have one of the representations used for an
26125 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26126 is returned. If PARSER->SCOPE is a dependent type, then a
26127 SCOPE_REF is returned.
26129 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26130 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26131 was formed. Abstractly, such entities should not be passed to this
26132 function, because they do not need to be looked up, but it is
26133 simpler to check for this special case here, rather than at the
26134 call-sites.
26136 In cases not explicitly covered above, this function returns a
26137 DECL, OVERLOAD, or baselink representing the result of the lookup.
26138 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26139 is returned.
26141 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26142 (e.g., "struct") that was used. In that case bindings that do not
26143 refer to types are ignored.
26145 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26146 ignored.
26148 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26149 are ignored.
26151 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26152 types.
26154 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26155 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26156 NULL_TREE otherwise. */
26158 static cp_expr
26159 cp_parser_lookup_name (cp_parser *parser, tree name,
26160 enum tag_types tag_type,
26161 bool is_template,
26162 bool is_namespace,
26163 bool check_dependency,
26164 tree *ambiguous_decls,
26165 location_t name_location)
26167 tree decl;
26168 tree object_type = parser->context->object_type;
26170 /* Assume that the lookup will be unambiguous. */
26171 if (ambiguous_decls)
26172 *ambiguous_decls = NULL_TREE;
26174 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26175 no longer valid. Note that if we are parsing tentatively, and
26176 the parse fails, OBJECT_TYPE will be automatically restored. */
26177 parser->context->object_type = NULL_TREE;
26179 if (name == error_mark_node)
26180 return error_mark_node;
26182 /* A template-id has already been resolved; there is no lookup to
26183 do. */
26184 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26185 return name;
26186 if (BASELINK_P (name))
26188 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26189 == TEMPLATE_ID_EXPR);
26190 return name;
26193 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26194 it should already have been checked to make sure that the name
26195 used matches the type being destroyed. */
26196 if (TREE_CODE (name) == BIT_NOT_EXPR)
26198 tree type;
26200 /* Figure out to which type this destructor applies. */
26201 if (parser->scope)
26202 type = parser->scope;
26203 else if (object_type)
26204 type = object_type;
26205 else
26206 type = current_class_type;
26207 /* If that's not a class type, there is no destructor. */
26208 if (!type || !CLASS_TYPE_P (type))
26209 return error_mark_node;
26211 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26212 lazily_declare_fn (sfk_destructor, type);
26214 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26215 return dtor;
26217 return error_mark_node;
26220 /* By this point, the NAME should be an ordinary identifier. If
26221 the id-expression was a qualified name, the qualifying scope is
26222 stored in PARSER->SCOPE at this point. */
26223 gcc_assert (identifier_p (name));
26225 /* Perform the lookup. */
26226 if (parser->scope)
26228 bool dependent_p;
26230 if (parser->scope == error_mark_node)
26231 return error_mark_node;
26233 /* If the SCOPE is dependent, the lookup must be deferred until
26234 the template is instantiated -- unless we are explicitly
26235 looking up names in uninstantiated templates. Even then, we
26236 cannot look up the name if the scope is not a class type; it
26237 might, for example, be a template type parameter. */
26238 dependent_p = (TYPE_P (parser->scope)
26239 && dependent_scope_p (parser->scope));
26240 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26241 && dependent_p)
26242 /* Defer lookup. */
26243 decl = error_mark_node;
26244 else
26246 tree pushed_scope = NULL_TREE;
26248 /* If PARSER->SCOPE is a dependent type, then it must be a
26249 class type, and we must not be checking dependencies;
26250 otherwise, we would have processed this lookup above. So
26251 that PARSER->SCOPE is not considered a dependent base by
26252 lookup_member, we must enter the scope here. */
26253 if (dependent_p)
26254 pushed_scope = push_scope (parser->scope);
26256 /* If the PARSER->SCOPE is a template specialization, it
26257 may be instantiated during name lookup. In that case,
26258 errors may be issued. Even if we rollback the current
26259 tentative parse, those errors are valid. */
26260 decl = lookup_qualified_name (parser->scope, name,
26261 prefer_type_arg (tag_type),
26262 /*complain=*/true);
26264 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26265 lookup result and the nested-name-specifier nominates a class C:
26266 * if the name specified after the nested-name-specifier, when
26267 looked up in C, is the injected-class-name of C (Clause 9), or
26268 * if the name specified after the nested-name-specifier is the
26269 same as the identifier or the simple-template-id's template-
26270 name in the last component of the nested-name-specifier,
26271 the name is instead considered to name the constructor of
26272 class C. [ Note: for example, the constructor is not an
26273 acceptable lookup result in an elaborated-type-specifier so
26274 the constructor would not be used in place of the
26275 injected-class-name. --end note ] Such a constructor name
26276 shall be used only in the declarator-id of a declaration that
26277 names a constructor or in a using-declaration. */
26278 if (tag_type == none_type
26279 && DECL_SELF_REFERENCE_P (decl)
26280 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26281 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26282 prefer_type_arg (tag_type),
26283 /*complain=*/true);
26285 /* If we have a single function from a using decl, pull it out. */
26286 if (TREE_CODE (decl) == OVERLOAD
26287 && !really_overloaded_fn (decl))
26288 decl = OVL_FUNCTION (decl);
26290 if (pushed_scope)
26291 pop_scope (pushed_scope);
26294 /* If the scope is a dependent type and either we deferred lookup or
26295 we did lookup but didn't find the name, rememeber the name. */
26296 if (decl == error_mark_node && TYPE_P (parser->scope)
26297 && dependent_type_p (parser->scope))
26299 if (tag_type)
26301 tree type;
26303 /* The resolution to Core Issue 180 says that `struct
26304 A::B' should be considered a type-name, even if `A'
26305 is dependent. */
26306 type = make_typename_type (parser->scope, name, tag_type,
26307 /*complain=*/tf_error);
26308 if (type != error_mark_node)
26309 decl = TYPE_NAME (type);
26311 else if (is_template
26312 && (cp_parser_next_token_ends_template_argument_p (parser)
26313 || cp_lexer_next_token_is (parser->lexer,
26314 CPP_CLOSE_PAREN)))
26315 decl = make_unbound_class_template (parser->scope,
26316 name, NULL_TREE,
26317 /*complain=*/tf_error);
26318 else
26319 decl = build_qualified_name (/*type=*/NULL_TREE,
26320 parser->scope, name,
26321 is_template);
26323 parser->qualifying_scope = parser->scope;
26324 parser->object_scope = NULL_TREE;
26326 else if (object_type)
26328 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26329 OBJECT_TYPE is not a class. */
26330 if (CLASS_TYPE_P (object_type))
26331 /* If the OBJECT_TYPE is a template specialization, it may
26332 be instantiated during name lookup. In that case, errors
26333 may be issued. Even if we rollback the current tentative
26334 parse, those errors are valid. */
26335 decl = lookup_member (object_type,
26336 name,
26337 /*protect=*/0,
26338 prefer_type_arg (tag_type),
26339 tf_warning_or_error);
26340 else
26341 decl = NULL_TREE;
26343 if (!decl)
26344 /* Look it up in the enclosing context. DR 141: When looking for a
26345 template-name after -> or ., only consider class templates. */
26346 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26347 /*nonclass=*/0,
26348 /*block_p=*/true, is_namespace, 0);
26349 if (object_type == unknown_type_node)
26350 /* The object is type-dependent, so we can't look anything up; we used
26351 this to get the DR 141 behavior. */
26352 object_type = NULL_TREE;
26353 parser->object_scope = object_type;
26354 parser->qualifying_scope = NULL_TREE;
26356 else
26358 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26359 /*nonclass=*/0,
26360 /*block_p=*/true, is_namespace, 0);
26361 parser->qualifying_scope = NULL_TREE;
26362 parser->object_scope = NULL_TREE;
26365 /* If the lookup failed, let our caller know. */
26366 if (!decl || decl == error_mark_node)
26367 return error_mark_node;
26369 /* Pull out the template from an injected-class-name (or multiple). */
26370 if (is_template)
26371 decl = maybe_get_template_decl_from_type_decl (decl);
26373 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26374 if (TREE_CODE (decl) == TREE_LIST)
26376 if (ambiguous_decls)
26377 *ambiguous_decls = decl;
26378 /* The error message we have to print is too complicated for
26379 cp_parser_error, so we incorporate its actions directly. */
26380 if (!cp_parser_simulate_error (parser))
26382 error_at (name_location, "reference to %qD is ambiguous",
26383 name);
26384 print_candidates (decl);
26386 return error_mark_node;
26389 gcc_assert (DECL_P (decl)
26390 || TREE_CODE (decl) == OVERLOAD
26391 || TREE_CODE (decl) == SCOPE_REF
26392 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26393 || BASELINK_P (decl));
26395 /* If we have resolved the name of a member declaration, check to
26396 see if the declaration is accessible. When the name resolves to
26397 set of overloaded functions, accessibility is checked when
26398 overload resolution is done.
26400 During an explicit instantiation, access is not checked at all,
26401 as per [temp.explicit]. */
26402 if (DECL_P (decl))
26403 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26405 maybe_record_typedef_use (decl);
26407 return cp_expr (decl, name_location);
26410 /* Like cp_parser_lookup_name, but for use in the typical case where
26411 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26412 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26414 static tree
26415 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26417 return cp_parser_lookup_name (parser, name,
26418 none_type,
26419 /*is_template=*/false,
26420 /*is_namespace=*/false,
26421 /*check_dependency=*/true,
26422 /*ambiguous_decls=*/NULL,
26423 location);
26426 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26427 the current context, return the TYPE_DECL. If TAG_NAME_P is
26428 true, the DECL indicates the class being defined in a class-head,
26429 or declared in an elaborated-type-specifier.
26431 Otherwise, return DECL. */
26433 static tree
26434 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26436 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26437 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26439 struct A {
26440 template <typename T> struct B;
26443 template <typename T> struct A::B {};
26445 Similarly, in an elaborated-type-specifier:
26447 namespace N { struct X{}; }
26449 struct A {
26450 template <typename T> friend struct N::X;
26453 However, if the DECL refers to a class type, and we are in
26454 the scope of the class, then the name lookup automatically
26455 finds the TYPE_DECL created by build_self_reference rather
26456 than a TEMPLATE_DECL. For example, in:
26458 template <class T> struct S {
26459 S s;
26462 there is no need to handle such case. */
26464 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26465 return DECL_TEMPLATE_RESULT (decl);
26467 return decl;
26470 /* If too many, or too few, template-parameter lists apply to the
26471 declarator, issue an error message. Returns TRUE if all went well,
26472 and FALSE otherwise. */
26474 static bool
26475 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26476 cp_declarator *declarator,
26477 location_t declarator_location)
26479 switch (declarator->kind)
26481 case cdk_id:
26483 unsigned num_templates = 0;
26484 tree scope = declarator->u.id.qualifying_scope;
26485 bool template_id_p = false;
26487 if (scope)
26488 num_templates = num_template_headers_for_class (scope);
26489 else if (TREE_CODE (declarator->u.id.unqualified_name)
26490 == TEMPLATE_ID_EXPR)
26492 /* If the DECLARATOR has the form `X<y>' then it uses one
26493 additional level of template parameters. */
26494 ++num_templates;
26495 template_id_p = true;
26498 return cp_parser_check_template_parameters
26499 (parser, num_templates, template_id_p, declarator_location,
26500 declarator);
26503 case cdk_function:
26504 case cdk_array:
26505 case cdk_pointer:
26506 case cdk_reference:
26507 case cdk_ptrmem:
26508 return (cp_parser_check_declarator_template_parameters
26509 (parser, declarator->declarator, declarator_location));
26511 case cdk_decomp:
26512 case cdk_error:
26513 return true;
26515 default:
26516 gcc_unreachable ();
26518 return false;
26521 /* NUM_TEMPLATES were used in the current declaration. If that is
26522 invalid, return FALSE and issue an error messages. Otherwise,
26523 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26524 declarator and we can print more accurate diagnostics. */
26526 static bool
26527 cp_parser_check_template_parameters (cp_parser* parser,
26528 unsigned num_templates,
26529 bool template_id_p,
26530 location_t location,
26531 cp_declarator *declarator)
26533 /* If there are the same number of template classes and parameter
26534 lists, that's OK. */
26535 if (parser->num_template_parameter_lists == num_templates)
26536 return true;
26537 /* If there are more, but only one more, and the name ends in an identifier,
26538 then we are declaring a primary template. That's OK too. */
26539 if (!template_id_p
26540 && parser->num_template_parameter_lists == num_templates + 1)
26541 return true;
26542 /* If there are more template classes than parameter lists, we have
26543 something like:
26545 template <class T> void S<T>::R<T>::f (); */
26546 if (parser->num_template_parameter_lists < num_templates)
26548 if (declarator && !current_function_decl)
26549 error_at (location, "specializing member %<%T::%E%> "
26550 "requires %<template<>%> syntax",
26551 declarator->u.id.qualifying_scope,
26552 declarator->u.id.unqualified_name);
26553 else if (declarator)
26554 error_at (location, "invalid declaration of %<%T::%E%>",
26555 declarator->u.id.qualifying_scope,
26556 declarator->u.id.unqualified_name);
26557 else
26558 error_at (location, "too few template-parameter-lists");
26559 return false;
26561 /* Otherwise, there are too many template parameter lists. We have
26562 something like:
26564 template <class T> template <class U> void S::f(); */
26565 error_at (location, "too many template-parameter-lists");
26566 return false;
26569 /* Parse an optional `::' token indicating that the following name is
26570 from the global namespace. If so, PARSER->SCOPE is set to the
26571 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26572 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26573 Returns the new value of PARSER->SCOPE, if the `::' token is
26574 present, and NULL_TREE otherwise. */
26576 static tree
26577 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26579 cp_token *token;
26581 /* Peek at the next token. */
26582 token = cp_lexer_peek_token (parser->lexer);
26583 /* If we're looking at a `::' token then we're starting from the
26584 global namespace, not our current location. */
26585 if (token->type == CPP_SCOPE)
26587 /* Consume the `::' token. */
26588 cp_lexer_consume_token (parser->lexer);
26589 /* Set the SCOPE so that we know where to start the lookup. */
26590 parser->scope = global_namespace;
26591 parser->qualifying_scope = global_namespace;
26592 parser->object_scope = NULL_TREE;
26594 return parser->scope;
26596 else if (!current_scope_valid_p)
26598 parser->scope = NULL_TREE;
26599 parser->qualifying_scope = NULL_TREE;
26600 parser->object_scope = NULL_TREE;
26603 return NULL_TREE;
26606 /* Returns TRUE if the upcoming token sequence is the start of a
26607 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26608 declarator is preceded by the `friend' specifier. */
26610 static bool
26611 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26613 bool constructor_p;
26614 bool outside_class_specifier_p;
26615 tree nested_name_specifier;
26616 cp_token *next_token;
26618 /* The common case is that this is not a constructor declarator, so
26619 try to avoid doing lots of work if at all possible. It's not
26620 valid declare a constructor at function scope. */
26621 if (parser->in_function_body)
26622 return false;
26623 /* And only certain tokens can begin a constructor declarator. */
26624 next_token = cp_lexer_peek_token (parser->lexer);
26625 if (next_token->type != CPP_NAME
26626 && next_token->type != CPP_SCOPE
26627 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26628 && next_token->type != CPP_TEMPLATE_ID)
26629 return false;
26631 /* Parse tentatively; we are going to roll back all of the tokens
26632 consumed here. */
26633 cp_parser_parse_tentatively (parser);
26634 /* Assume that we are looking at a constructor declarator. */
26635 constructor_p = true;
26637 /* Look for the optional `::' operator. */
26638 cp_parser_global_scope_opt (parser,
26639 /*current_scope_valid_p=*/false);
26640 /* Look for the nested-name-specifier. */
26641 nested_name_specifier
26642 = (cp_parser_nested_name_specifier_opt (parser,
26643 /*typename_keyword_p=*/false,
26644 /*check_dependency_p=*/false,
26645 /*type_p=*/false,
26646 /*is_declaration=*/false));
26648 outside_class_specifier_p = (!at_class_scope_p ()
26649 || !TYPE_BEING_DEFINED (current_class_type)
26650 || friend_p);
26652 /* Outside of a class-specifier, there must be a
26653 nested-name-specifier. Except in C++17 mode, where we
26654 might be declaring a guiding declaration. */
26655 if (!nested_name_specifier && outside_class_specifier_p
26656 && cxx_dialect < cxx17)
26657 constructor_p = false;
26658 else if (nested_name_specifier == error_mark_node)
26659 constructor_p = false;
26661 /* If we have a class scope, this is easy; DR 147 says that S::S always
26662 names the constructor, and no other qualified name could. */
26663 if (constructor_p && nested_name_specifier
26664 && CLASS_TYPE_P (nested_name_specifier))
26666 tree id = cp_parser_unqualified_id (parser,
26667 /*template_keyword_p=*/false,
26668 /*check_dependency_p=*/false,
26669 /*declarator_p=*/true,
26670 /*optional_p=*/false);
26671 if (is_overloaded_fn (id))
26672 id = DECL_NAME (get_first_fn (id));
26673 if (!constructor_name_p (id, nested_name_specifier))
26674 constructor_p = false;
26676 /* If we still think that this might be a constructor-declarator,
26677 look for a class-name. */
26678 else if (constructor_p)
26680 /* If we have:
26682 template <typename T> struct S {
26683 S();
26686 we must recognize that the nested `S' names a class. */
26687 if (cxx_dialect >= cxx17)
26688 cp_parser_parse_tentatively (parser);
26690 tree type_decl;
26691 type_decl = cp_parser_class_name (parser,
26692 /*typename_keyword_p=*/false,
26693 /*template_keyword_p=*/false,
26694 none_type,
26695 /*check_dependency_p=*/false,
26696 /*class_head_p=*/false,
26697 /*is_declaration=*/false);
26699 if (cxx_dialect >= cxx17
26700 && !cp_parser_parse_definitely (parser))
26702 type_decl = NULL_TREE;
26703 tree tmpl = cp_parser_template_name (parser,
26704 /*template_keyword*/false,
26705 /*check_dependency_p*/false,
26706 /*is_declaration*/false,
26707 none_type,
26708 /*is_identifier*/NULL);
26709 if (DECL_CLASS_TEMPLATE_P (tmpl)
26710 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26711 /* It's a deduction guide, return true. */;
26712 else
26713 cp_parser_simulate_error (parser);
26716 /* If there was no class-name, then this is not a constructor.
26717 Otherwise, if we are in a class-specifier and we aren't
26718 handling a friend declaration, check that its type matches
26719 current_class_type (c++/38313). Note: error_mark_node
26720 is left alone for error recovery purposes. */
26721 constructor_p = (!cp_parser_error_occurred (parser)
26722 && (outside_class_specifier_p
26723 || type_decl == NULL_TREE
26724 || type_decl == error_mark_node
26725 || same_type_p (current_class_type,
26726 TREE_TYPE (type_decl))));
26728 /* If we're still considering a constructor, we have to see a `(',
26729 to begin the parameter-declaration-clause, followed by either a
26730 `)', an `...', or a decl-specifier. We need to check for a
26731 type-specifier to avoid being fooled into thinking that:
26733 S (f) (int);
26735 is a constructor. (It is actually a function named `f' that
26736 takes one parameter (of type `int') and returns a value of type
26737 `S'. */
26738 if (constructor_p
26739 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26740 constructor_p = false;
26742 if (constructor_p
26743 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26744 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26745 /* A parameter declaration begins with a decl-specifier,
26746 which is either the "attribute" keyword, a storage class
26747 specifier, or (usually) a type-specifier. */
26748 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26750 tree type;
26751 tree pushed_scope = NULL_TREE;
26752 unsigned saved_num_template_parameter_lists;
26754 /* Names appearing in the type-specifier should be looked up
26755 in the scope of the class. */
26756 if (current_class_type)
26757 type = NULL_TREE;
26758 else if (type_decl)
26760 type = TREE_TYPE (type_decl);
26761 if (TREE_CODE (type) == TYPENAME_TYPE)
26763 type = resolve_typename_type (type,
26764 /*only_current_p=*/false);
26765 if (TREE_CODE (type) == TYPENAME_TYPE)
26767 cp_parser_abort_tentative_parse (parser);
26768 return false;
26771 pushed_scope = push_scope (type);
26774 /* Inside the constructor parameter list, surrounding
26775 template-parameter-lists do not apply. */
26776 saved_num_template_parameter_lists
26777 = parser->num_template_parameter_lists;
26778 parser->num_template_parameter_lists = 0;
26780 /* Look for the type-specifier. */
26781 cp_parser_type_specifier (parser,
26782 CP_PARSER_FLAGS_NONE,
26783 /*decl_specs=*/NULL,
26784 /*is_declarator=*/true,
26785 /*declares_class_or_enum=*/NULL,
26786 /*is_cv_qualifier=*/NULL);
26788 parser->num_template_parameter_lists
26789 = saved_num_template_parameter_lists;
26791 /* Leave the scope of the class. */
26792 if (pushed_scope)
26793 pop_scope (pushed_scope);
26795 constructor_p = !cp_parser_error_occurred (parser);
26799 /* We did not really want to consume any tokens. */
26800 cp_parser_abort_tentative_parse (parser);
26802 return constructor_p;
26805 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26806 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26807 they must be performed once we are in the scope of the function.
26809 Returns the function defined. */
26811 static tree
26812 cp_parser_function_definition_from_specifiers_and_declarator
26813 (cp_parser* parser,
26814 cp_decl_specifier_seq *decl_specifiers,
26815 tree attributes,
26816 const cp_declarator *declarator)
26818 tree fn;
26819 bool success_p;
26821 /* Begin the function-definition. */
26822 success_p = start_function (decl_specifiers, declarator, attributes);
26824 /* The things we're about to see are not directly qualified by any
26825 template headers we've seen thus far. */
26826 reset_specialization ();
26828 /* If there were names looked up in the decl-specifier-seq that we
26829 did not check, check them now. We must wait until we are in the
26830 scope of the function to perform the checks, since the function
26831 might be a friend. */
26832 perform_deferred_access_checks (tf_warning_or_error);
26834 if (success_p)
26836 cp_finalize_omp_declare_simd (parser, current_function_decl);
26837 parser->omp_declare_simd = NULL;
26838 cp_finalize_oacc_routine (parser, current_function_decl, true);
26839 parser->oacc_routine = NULL;
26842 if (!success_p)
26844 /* Skip the entire function. */
26845 cp_parser_skip_to_end_of_block_or_statement (parser);
26846 fn = error_mark_node;
26848 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26850 /* Seen already, skip it. An error message has already been output. */
26851 cp_parser_skip_to_end_of_block_or_statement (parser);
26852 fn = current_function_decl;
26853 current_function_decl = NULL_TREE;
26854 /* If this is a function from a class, pop the nested class. */
26855 if (current_class_name)
26856 pop_nested_class ();
26858 else
26860 timevar_id_t tv;
26861 if (DECL_DECLARED_INLINE_P (current_function_decl))
26862 tv = TV_PARSE_INLINE;
26863 else
26864 tv = TV_PARSE_FUNC;
26865 timevar_push (tv);
26866 fn = cp_parser_function_definition_after_declarator (parser,
26867 /*inline_p=*/false);
26868 timevar_pop (tv);
26871 return fn;
26874 /* Parse the part of a function-definition that follows the
26875 declarator. INLINE_P is TRUE iff this function is an inline
26876 function defined within a class-specifier.
26878 Returns the function defined. */
26880 static tree
26881 cp_parser_function_definition_after_declarator (cp_parser* parser,
26882 bool inline_p)
26884 tree fn;
26885 bool saved_in_unbraced_linkage_specification_p;
26886 bool saved_in_function_body;
26887 unsigned saved_num_template_parameter_lists;
26888 cp_token *token;
26889 bool fully_implicit_function_template_p
26890 = parser->fully_implicit_function_template_p;
26891 parser->fully_implicit_function_template_p = false;
26892 tree implicit_template_parms
26893 = parser->implicit_template_parms;
26894 parser->implicit_template_parms = 0;
26895 cp_binding_level* implicit_template_scope
26896 = parser->implicit_template_scope;
26897 parser->implicit_template_scope = 0;
26899 saved_in_function_body = parser->in_function_body;
26900 parser->in_function_body = true;
26901 /* If the next token is `return', then the code may be trying to
26902 make use of the "named return value" extension that G++ used to
26903 support. */
26904 token = cp_lexer_peek_token (parser->lexer);
26905 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26907 /* Consume the `return' keyword. */
26908 cp_lexer_consume_token (parser->lexer);
26909 /* Look for the identifier that indicates what value is to be
26910 returned. */
26911 cp_parser_identifier (parser);
26912 /* Issue an error message. */
26913 error_at (token->location,
26914 "named return values are no longer supported");
26915 /* Skip tokens until we reach the start of the function body. */
26916 while (true)
26918 cp_token *token = cp_lexer_peek_token (parser->lexer);
26919 if (token->type == CPP_OPEN_BRACE
26920 || token->type == CPP_EOF
26921 || token->type == CPP_PRAGMA_EOL)
26922 break;
26923 cp_lexer_consume_token (parser->lexer);
26926 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26927 anything declared inside `f'. */
26928 saved_in_unbraced_linkage_specification_p
26929 = parser->in_unbraced_linkage_specification_p;
26930 parser->in_unbraced_linkage_specification_p = false;
26931 /* Inside the function, surrounding template-parameter-lists do not
26932 apply. */
26933 saved_num_template_parameter_lists
26934 = parser->num_template_parameter_lists;
26935 parser->num_template_parameter_lists = 0;
26937 /* If the next token is `try', `__transaction_atomic', or
26938 `__transaction_relaxed`, then we are looking at either function-try-block
26939 or function-transaction-block. Note that all of these include the
26940 function-body. */
26941 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26942 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26943 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26944 RID_TRANSACTION_RELAXED))
26945 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26946 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26947 cp_parser_function_try_block (parser);
26948 else
26949 cp_parser_ctor_initializer_opt_and_function_body
26950 (parser, /*in_function_try_block=*/false);
26952 /* Finish the function. */
26953 fn = finish_function (inline_p);
26954 /* Generate code for it, if necessary. */
26955 expand_or_defer_fn (fn);
26956 /* Restore the saved values. */
26957 parser->in_unbraced_linkage_specification_p
26958 = saved_in_unbraced_linkage_specification_p;
26959 parser->num_template_parameter_lists
26960 = saved_num_template_parameter_lists;
26961 parser->in_function_body = saved_in_function_body;
26963 parser->fully_implicit_function_template_p
26964 = fully_implicit_function_template_p;
26965 parser->implicit_template_parms
26966 = implicit_template_parms;
26967 parser->implicit_template_scope
26968 = implicit_template_scope;
26970 if (parser->fully_implicit_function_template_p)
26971 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26973 return fn;
26976 /* Parse a template-declaration body (following argument list). */
26978 static void
26979 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26980 tree parameter_list,
26981 bool member_p)
26983 tree decl = NULL_TREE;
26984 bool friend_p = false;
26986 /* We just processed one more parameter list. */
26987 ++parser->num_template_parameter_lists;
26989 /* Get the deferred access checks from the parameter list. These
26990 will be checked once we know what is being declared, as for a
26991 member template the checks must be performed in the scope of the
26992 class containing the member. */
26993 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26995 /* Tentatively parse for a new template parameter list, which can either be
26996 the template keyword or a template introduction. */
26997 if (cp_parser_template_declaration_after_export (parser, member_p))
26998 /* OK */;
26999 else if (cxx_dialect >= cxx11
27000 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27001 decl = cp_parser_alias_declaration (parser);
27002 else
27004 /* There are no access checks when parsing a template, as we do not
27005 know if a specialization will be a friend. */
27006 push_deferring_access_checks (dk_no_check);
27007 cp_token *token = cp_lexer_peek_token (parser->lexer);
27008 decl = cp_parser_single_declaration (parser,
27009 checks,
27010 member_p,
27011 /*explicit_specialization_p=*/false,
27012 &friend_p);
27013 pop_deferring_access_checks ();
27015 /* If this is a member template declaration, let the front
27016 end know. */
27017 if (member_p && !friend_p && decl)
27019 if (TREE_CODE (decl) == TYPE_DECL)
27020 cp_parser_check_access_in_redeclaration (decl, token->location);
27022 decl = finish_member_template_decl (decl);
27024 else if (friend_p && decl
27025 && DECL_DECLARES_TYPE_P (decl))
27026 make_friend_class (current_class_type, TREE_TYPE (decl),
27027 /*complain=*/true);
27029 /* We are done with the current parameter list. */
27030 --parser->num_template_parameter_lists;
27032 pop_deferring_access_checks ();
27034 /* Finish up. */
27035 finish_template_decl (parameter_list);
27037 /* Check the template arguments for a literal operator template. */
27038 if (decl
27039 && DECL_DECLARES_FUNCTION_P (decl)
27040 && UDLIT_OPER_P (DECL_NAME (decl)))
27042 bool ok = true;
27043 if (parameter_list == NULL_TREE)
27044 ok = false;
27045 else
27047 int num_parms = TREE_VEC_LENGTH (parameter_list);
27048 if (num_parms == 1)
27050 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27051 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27052 if (TREE_TYPE (parm) != char_type_node
27053 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27054 ok = false;
27056 else if (num_parms == 2 && cxx_dialect >= cxx14)
27058 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27059 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27060 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27061 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27062 if (parm == error_mark_node
27063 || TREE_TYPE (parm) != TREE_TYPE (type)
27064 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27065 ok = false;
27067 else
27068 ok = false;
27070 if (!ok)
27072 if (cxx_dialect >= cxx14)
27073 error ("literal operator template %qD has invalid parameter list."
27074 " Expected non-type template argument pack <char...>"
27075 " or <typename CharT, CharT...>",
27076 decl);
27077 else
27078 error ("literal operator template %qD has invalid parameter list."
27079 " Expected non-type template argument pack <char...>",
27080 decl);
27084 /* Register member declarations. */
27085 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27086 finish_member_declaration (decl);
27087 /* If DECL is a function template, we must return to parse it later.
27088 (Even though there is no definition, there might be default
27089 arguments that need handling.) */
27090 if (member_p && decl
27091 && DECL_DECLARES_FUNCTION_P (decl))
27092 vec_safe_push (unparsed_funs_with_definitions, decl);
27095 /* Parse a template introduction header for a template-declaration. Returns
27096 false if tentative parse fails. */
27098 static bool
27099 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27101 cp_parser_parse_tentatively (parser);
27103 tree saved_scope = parser->scope;
27104 tree saved_object_scope = parser->object_scope;
27105 tree saved_qualifying_scope = parser->qualifying_scope;
27107 /* Look for the optional `::' operator. */
27108 cp_parser_global_scope_opt (parser,
27109 /*current_scope_valid_p=*/false);
27110 /* Look for the nested-name-specifier. */
27111 cp_parser_nested_name_specifier_opt (parser,
27112 /*typename_keyword_p=*/false,
27113 /*check_dependency_p=*/true,
27114 /*type_p=*/false,
27115 /*is_declaration=*/false);
27117 cp_token *token = cp_lexer_peek_token (parser->lexer);
27118 tree concept_name = cp_parser_identifier (parser);
27120 /* Look up the concept for which we will be matching
27121 template parameters. */
27122 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27123 token->location);
27124 parser->scope = saved_scope;
27125 parser->object_scope = saved_object_scope;
27126 parser->qualifying_scope = saved_qualifying_scope;
27128 if (concept_name == error_mark_node)
27129 cp_parser_simulate_error (parser);
27131 /* Look for opening brace for introduction. */
27132 matching_braces braces;
27133 braces.require_open (parser);
27135 if (!cp_parser_parse_definitely (parser))
27136 return false;
27138 push_deferring_access_checks (dk_deferred);
27140 /* Build vector of placeholder parameters and grab
27141 matching identifiers. */
27142 tree introduction_list = cp_parser_introduction_list (parser);
27144 /* The introduction-list shall not be empty. */
27145 int nargs = TREE_VEC_LENGTH (introduction_list);
27146 if (nargs == 0)
27148 error ("empty introduction-list");
27149 return true;
27152 /* Look for closing brace for introduction. */
27153 if (!braces.require_close (parser))
27154 return true;
27156 if (tmpl_decl == error_mark_node)
27158 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27159 token->location);
27160 return true;
27163 /* Build and associate the constraint. */
27164 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27165 if (parms && parms != error_mark_node)
27167 cp_parser_template_declaration_after_parameters (parser, parms,
27168 member_p);
27169 return true;
27172 error_at (token->location, "no matching concept for template-introduction");
27173 return true;
27176 /* Parse a normal template-declaration following the template keyword. */
27178 static void
27179 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27181 tree parameter_list;
27182 bool need_lang_pop;
27183 location_t location = input_location;
27185 /* Look for the `<' token. */
27186 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27187 return;
27188 if (at_class_scope_p () && current_function_decl)
27190 /* 14.5.2.2 [temp.mem]
27192 A local class shall not have member templates. */
27193 error_at (location,
27194 "invalid declaration of member template in local class");
27195 cp_parser_skip_to_end_of_block_or_statement (parser);
27196 return;
27198 /* [temp]
27200 A template ... shall not have C linkage. */
27201 if (current_lang_name == lang_name_c)
27203 error_at (location, "template with C linkage");
27204 maybe_show_extern_c_location ();
27205 /* Give it C++ linkage to avoid confusing other parts of the
27206 front end. */
27207 push_lang_context (lang_name_cplusplus);
27208 need_lang_pop = true;
27210 else
27211 need_lang_pop = false;
27213 /* We cannot perform access checks on the template parameter
27214 declarations until we know what is being declared, just as we
27215 cannot check the decl-specifier list. */
27216 push_deferring_access_checks (dk_deferred);
27218 /* If the next token is `>', then we have an invalid
27219 specialization. Rather than complain about an invalid template
27220 parameter, issue an error message here. */
27221 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27223 cp_parser_error (parser, "invalid explicit specialization");
27224 begin_specialization ();
27225 parameter_list = NULL_TREE;
27227 else
27229 /* Parse the template parameters. */
27230 parameter_list = cp_parser_template_parameter_list (parser);
27233 /* Look for the `>'. */
27234 cp_parser_skip_to_end_of_template_parameter_list (parser);
27236 /* Manage template requirements */
27237 if (flag_concepts)
27239 tree reqs = get_shorthand_constraints (current_template_parms);
27240 if (tree r = cp_parser_requires_clause_opt (parser))
27241 reqs = conjoin_constraints (reqs, normalize_expression (r));
27242 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27245 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27246 member_p);
27248 /* For the erroneous case of a template with C linkage, we pushed an
27249 implicit C++ linkage scope; exit that scope now. */
27250 if (need_lang_pop)
27251 pop_lang_context ();
27254 /* Parse a template-declaration, assuming that the `export' (and
27255 `extern') keywords, if present, has already been scanned. MEMBER_P
27256 is as for cp_parser_template_declaration. */
27258 static bool
27259 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27261 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27263 cp_lexer_consume_token (parser->lexer);
27264 cp_parser_explicit_template_declaration (parser, member_p);
27265 return true;
27267 else if (flag_concepts)
27268 return cp_parser_template_introduction (parser, member_p);
27270 return false;
27273 /* Perform the deferred access checks from a template-parameter-list.
27274 CHECKS is a TREE_LIST of access checks, as returned by
27275 get_deferred_access_checks. */
27277 static void
27278 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27280 ++processing_template_parmlist;
27281 perform_access_checks (checks, tf_warning_or_error);
27282 --processing_template_parmlist;
27285 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27286 `function-definition' sequence that follows a template header.
27287 If MEMBER_P is true, this declaration appears in a class scope.
27289 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27290 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27292 static tree
27293 cp_parser_single_declaration (cp_parser* parser,
27294 vec<deferred_access_check, va_gc> *checks,
27295 bool member_p,
27296 bool explicit_specialization_p,
27297 bool* friend_p)
27299 int declares_class_or_enum;
27300 tree decl = NULL_TREE;
27301 cp_decl_specifier_seq decl_specifiers;
27302 bool function_definition_p = false;
27303 cp_token *decl_spec_token_start;
27305 /* This function is only used when processing a template
27306 declaration. */
27307 gcc_assert (innermost_scope_kind () == sk_template_parms
27308 || innermost_scope_kind () == sk_template_spec);
27310 /* Defer access checks until we know what is being declared. */
27311 push_deferring_access_checks (dk_deferred);
27313 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27314 alternative. */
27315 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27316 cp_parser_decl_specifier_seq (parser,
27317 CP_PARSER_FLAGS_OPTIONAL,
27318 &decl_specifiers,
27319 &declares_class_or_enum);
27320 if (friend_p)
27321 *friend_p = cp_parser_friend_p (&decl_specifiers);
27323 /* There are no template typedefs. */
27324 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27326 error_at (decl_spec_token_start->location,
27327 "template declaration of %<typedef%>");
27328 decl = error_mark_node;
27331 /* Gather up the access checks that occurred the
27332 decl-specifier-seq. */
27333 stop_deferring_access_checks ();
27335 /* Check for the declaration of a template class. */
27336 if (declares_class_or_enum)
27338 if (cp_parser_declares_only_class_p (parser)
27339 || (declares_class_or_enum & 2))
27341 // If this is a declaration, but not a definition, associate
27342 // any constraints with the type declaration. Constraints
27343 // are associated with definitions in cp_parser_class_specifier.
27344 if (declares_class_or_enum == 1)
27345 associate_classtype_constraints (decl_specifiers.type);
27347 decl = shadow_tag (&decl_specifiers);
27349 /* In this case:
27351 struct C {
27352 friend template <typename T> struct A<T>::B;
27355 A<T>::B will be represented by a TYPENAME_TYPE, and
27356 therefore not recognized by shadow_tag. */
27357 if (friend_p && *friend_p
27358 && !decl
27359 && decl_specifiers.type
27360 && TYPE_P (decl_specifiers.type))
27361 decl = decl_specifiers.type;
27363 if (decl && decl != error_mark_node)
27364 decl = TYPE_NAME (decl);
27365 else
27366 decl = error_mark_node;
27368 /* Perform access checks for template parameters. */
27369 cp_parser_perform_template_parameter_access_checks (checks);
27371 /* Give a helpful diagnostic for
27372 template <class T> struct A { } a;
27373 if we aren't already recovering from an error. */
27374 if (!cp_parser_declares_only_class_p (parser)
27375 && !seen_error ())
27377 error_at (cp_lexer_peek_token (parser->lexer)->location,
27378 "a class template declaration must not declare "
27379 "anything else");
27380 cp_parser_skip_to_end_of_block_or_statement (parser);
27381 goto out;
27386 /* Complain about missing 'typename' or other invalid type names. */
27387 if (!decl_specifiers.any_type_specifiers_p
27388 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27390 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27391 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27392 the rest of this declaration. */
27393 decl = error_mark_node;
27394 goto out;
27397 /* If it's not a template class, try for a template function. If
27398 the next token is a `;', then this declaration does not declare
27399 anything. But, if there were errors in the decl-specifiers, then
27400 the error might well have come from an attempted class-specifier.
27401 In that case, there's no need to warn about a missing declarator. */
27402 if (!decl
27403 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27404 || decl_specifiers.type != error_mark_node))
27406 decl = cp_parser_init_declarator (parser,
27407 &decl_specifiers,
27408 checks,
27409 /*function_definition_allowed_p=*/true,
27410 member_p,
27411 declares_class_or_enum,
27412 &function_definition_p,
27413 NULL, NULL, NULL);
27415 /* 7.1.1-1 [dcl.stc]
27417 A storage-class-specifier shall not be specified in an explicit
27418 specialization... */
27419 if (decl
27420 && explicit_specialization_p
27421 && decl_specifiers.storage_class != sc_none)
27423 error_at (decl_spec_token_start->location,
27424 "explicit template specialization cannot have a storage class");
27425 decl = error_mark_node;
27428 if (decl && VAR_P (decl))
27429 check_template_variable (decl);
27432 /* Look for a trailing `;' after the declaration. */
27433 if (!function_definition_p
27434 && (decl == error_mark_node
27435 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27436 cp_parser_skip_to_end_of_block_or_statement (parser);
27438 out:
27439 pop_deferring_access_checks ();
27441 /* Clear any current qualification; whatever comes next is the start
27442 of something new. */
27443 parser->scope = NULL_TREE;
27444 parser->qualifying_scope = NULL_TREE;
27445 parser->object_scope = NULL_TREE;
27447 return decl;
27450 /* Parse a cast-expression that is not the operand of a unary "&". */
27452 static cp_expr
27453 cp_parser_simple_cast_expression (cp_parser *parser)
27455 return cp_parser_cast_expression (parser, /*address_p=*/false,
27456 /*cast_p=*/false, /*decltype*/false, NULL);
27459 /* Parse a functional cast to TYPE. Returns an expression
27460 representing the cast. */
27462 static cp_expr
27463 cp_parser_functional_cast (cp_parser* parser, tree type)
27465 vec<tree, va_gc> *vec;
27466 tree expression_list;
27467 cp_expr cast;
27468 bool nonconst_p;
27470 location_t start_loc = input_location;
27472 if (!type)
27473 type = error_mark_node;
27475 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27477 cp_lexer_set_source_position (parser->lexer);
27478 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27479 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27480 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27481 if (TREE_CODE (type) == TYPE_DECL)
27482 type = TREE_TYPE (type);
27484 cast = finish_compound_literal (type, expression_list,
27485 tf_warning_or_error, fcl_functional);
27486 /* Create a location of the form:
27487 type_name{i, f}
27488 ^~~~~~~~~~~~~~~
27489 with caret == start at the start of the type name,
27490 finishing at the closing brace. */
27491 location_t finish_loc
27492 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27493 location_t combined_loc = make_location (start_loc, start_loc,
27494 finish_loc);
27495 cast.set_location (combined_loc);
27496 return cast;
27500 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27501 /*cast_p=*/true,
27502 /*allow_expansion_p=*/true,
27503 /*non_constant_p=*/NULL);
27504 if (vec == NULL)
27505 expression_list = error_mark_node;
27506 else
27508 expression_list = build_tree_list_vec (vec);
27509 release_tree_vector (vec);
27512 cast = build_functional_cast (type, expression_list,
27513 tf_warning_or_error);
27514 /* [expr.const]/1: In an integral constant expression "only type
27515 conversions to integral or enumeration type can be used". */
27516 if (TREE_CODE (type) == TYPE_DECL)
27517 type = TREE_TYPE (type);
27518 if (cast != error_mark_node
27519 && !cast_valid_in_integral_constant_expression_p (type)
27520 && cp_parser_non_integral_constant_expression (parser,
27521 NIC_CONSTRUCTOR))
27522 return error_mark_node;
27524 /* Create a location of the form:
27525 float(i)
27526 ^~~~~~~~
27527 with caret == start at the start of the type name,
27528 finishing at the closing paren. */
27529 location_t finish_loc
27530 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27531 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27532 cast.set_location (combined_loc);
27533 return cast;
27536 /* Save the tokens that make up the body of a member function defined
27537 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27538 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27539 specifiers applied to the declaration. Returns the FUNCTION_DECL
27540 for the member function. */
27542 static tree
27543 cp_parser_save_member_function_body (cp_parser* parser,
27544 cp_decl_specifier_seq *decl_specifiers,
27545 cp_declarator *declarator,
27546 tree attributes)
27548 cp_token *first;
27549 cp_token *last;
27550 tree fn;
27551 bool function_try_block = false;
27553 /* Create the FUNCTION_DECL. */
27554 fn = grokmethod (decl_specifiers, declarator, attributes);
27555 cp_finalize_omp_declare_simd (parser, fn);
27556 cp_finalize_oacc_routine (parser, fn, true);
27557 /* If something went badly wrong, bail out now. */
27558 if (fn == error_mark_node)
27560 /* If there's a function-body, skip it. */
27561 if (cp_parser_token_starts_function_definition_p
27562 (cp_lexer_peek_token (parser->lexer)))
27563 cp_parser_skip_to_end_of_block_or_statement (parser);
27564 return error_mark_node;
27567 /* Remember it, if there default args to post process. */
27568 cp_parser_save_default_args (parser, fn);
27570 /* Save away the tokens that make up the body of the
27571 function. */
27572 first = parser->lexer->next_token;
27574 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27575 cp_lexer_consume_token (parser->lexer);
27576 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27577 RID_TRANSACTION_ATOMIC))
27579 cp_lexer_consume_token (parser->lexer);
27580 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27581 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27582 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27583 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27584 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27585 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27586 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27588 cp_lexer_consume_token (parser->lexer);
27589 cp_lexer_consume_token (parser->lexer);
27590 cp_lexer_consume_token (parser->lexer);
27591 cp_lexer_consume_token (parser->lexer);
27592 cp_lexer_consume_token (parser->lexer);
27594 else
27595 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27596 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27598 cp_lexer_consume_token (parser->lexer);
27599 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27600 break;
27604 /* Handle function try blocks. */
27605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27607 cp_lexer_consume_token (parser->lexer);
27608 function_try_block = true;
27610 /* We can have braced-init-list mem-initializers before the fn body. */
27611 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27613 cp_lexer_consume_token (parser->lexer);
27614 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27616 /* cache_group will stop after an un-nested { } pair, too. */
27617 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27618 break;
27620 /* variadic mem-inits have ... after the ')'. */
27621 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27622 cp_lexer_consume_token (parser->lexer);
27625 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27626 /* Handle function try blocks. */
27627 if (function_try_block)
27628 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27629 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27630 last = parser->lexer->next_token;
27632 /* Save away the inline definition; we will process it when the
27633 class is complete. */
27634 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27635 DECL_PENDING_INLINE_P (fn) = 1;
27637 /* We need to know that this was defined in the class, so that
27638 friend templates are handled correctly. */
27639 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27641 /* Add FN to the queue of functions to be parsed later. */
27642 vec_safe_push (unparsed_funs_with_definitions, fn);
27644 return fn;
27647 /* Save the tokens that make up the in-class initializer for a non-static
27648 data member. Returns a DEFAULT_ARG. */
27650 static tree
27651 cp_parser_save_nsdmi (cp_parser* parser)
27653 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27656 /* Parse a template-argument-list, as well as the trailing ">" (but
27657 not the opening "<"). See cp_parser_template_argument_list for the
27658 return value. */
27660 static tree
27661 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27663 tree arguments;
27664 tree saved_scope;
27665 tree saved_qualifying_scope;
27666 tree saved_object_scope;
27667 bool saved_greater_than_is_operator_p;
27668 int saved_unevaluated_operand;
27669 int saved_inhibit_evaluation_warnings;
27671 /* [temp.names]
27673 When parsing a template-id, the first non-nested `>' is taken as
27674 the end of the template-argument-list rather than a greater-than
27675 operator. */
27676 saved_greater_than_is_operator_p
27677 = parser->greater_than_is_operator_p;
27678 parser->greater_than_is_operator_p = false;
27679 /* Parsing the argument list may modify SCOPE, so we save it
27680 here. */
27681 saved_scope = parser->scope;
27682 saved_qualifying_scope = parser->qualifying_scope;
27683 saved_object_scope = parser->object_scope;
27684 /* We need to evaluate the template arguments, even though this
27685 template-id may be nested within a "sizeof". */
27686 saved_unevaluated_operand = cp_unevaluated_operand;
27687 cp_unevaluated_operand = 0;
27688 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27689 c_inhibit_evaluation_warnings = 0;
27690 /* Parse the template-argument-list itself. */
27691 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27692 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27693 arguments = NULL_TREE;
27694 else
27695 arguments = cp_parser_template_argument_list (parser);
27696 /* Look for the `>' that ends the template-argument-list. If we find
27697 a '>>' instead, it's probably just a typo. */
27698 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27700 if (cxx_dialect != cxx98)
27702 /* In C++0x, a `>>' in a template argument list or cast
27703 expression is considered to be two separate `>'
27704 tokens. So, change the current token to a `>', but don't
27705 consume it: it will be consumed later when the outer
27706 template argument list (or cast expression) is parsed.
27707 Note that this replacement of `>' for `>>' is necessary
27708 even if we are parsing tentatively: in the tentative
27709 case, after calling
27710 cp_parser_enclosed_template_argument_list we will always
27711 throw away all of the template arguments and the first
27712 closing `>', either because the template argument list
27713 was erroneous or because we are replacing those tokens
27714 with a CPP_TEMPLATE_ID token. The second `>' (which will
27715 not have been thrown away) is needed either to close an
27716 outer template argument list or to complete a new-style
27717 cast. */
27718 cp_token *token = cp_lexer_peek_token (parser->lexer);
27719 token->type = CPP_GREATER;
27721 else if (!saved_greater_than_is_operator_p)
27723 /* If we're in a nested template argument list, the '>>' has
27724 to be a typo for '> >'. We emit the error message, but we
27725 continue parsing and we push a '>' as next token, so that
27726 the argument list will be parsed correctly. Note that the
27727 global source location is still on the token before the
27728 '>>', so we need to say explicitly where we want it. */
27729 cp_token *token = cp_lexer_peek_token (parser->lexer);
27730 gcc_rich_location richloc (token->location);
27731 richloc.add_fixit_replace ("> >");
27732 error_at (&richloc, "%<>>%> should be %<> >%> "
27733 "within a nested template argument list");
27735 token->type = CPP_GREATER;
27737 else
27739 /* If this is not a nested template argument list, the '>>'
27740 is a typo for '>'. Emit an error message and continue.
27741 Same deal about the token location, but here we can get it
27742 right by consuming the '>>' before issuing the diagnostic. */
27743 cp_token *token = cp_lexer_consume_token (parser->lexer);
27744 error_at (token->location,
27745 "spurious %<>>%>, use %<>%> to terminate "
27746 "a template argument list");
27749 else
27750 cp_parser_skip_to_end_of_template_parameter_list (parser);
27751 /* The `>' token might be a greater-than operator again now. */
27752 parser->greater_than_is_operator_p
27753 = saved_greater_than_is_operator_p;
27754 /* Restore the SAVED_SCOPE. */
27755 parser->scope = saved_scope;
27756 parser->qualifying_scope = saved_qualifying_scope;
27757 parser->object_scope = saved_object_scope;
27758 cp_unevaluated_operand = saved_unevaluated_operand;
27759 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27761 return arguments;
27764 /* MEMBER_FUNCTION is a member function, or a friend. If default
27765 arguments, or the body of the function have not yet been parsed,
27766 parse them now. */
27768 static void
27769 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27771 timevar_push (TV_PARSE_INMETH);
27772 /* If this member is a template, get the underlying
27773 FUNCTION_DECL. */
27774 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27775 member_function = DECL_TEMPLATE_RESULT (member_function);
27777 /* There should not be any class definitions in progress at this
27778 point; the bodies of members are only parsed outside of all class
27779 definitions. */
27780 gcc_assert (parser->num_classes_being_defined == 0);
27781 /* While we're parsing the member functions we might encounter more
27782 classes. We want to handle them right away, but we don't want
27783 them getting mixed up with functions that are currently in the
27784 queue. */
27785 push_unparsed_function_queues (parser);
27787 /* Make sure that any template parameters are in scope. */
27788 maybe_begin_member_template_processing (member_function);
27790 /* If the body of the function has not yet been parsed, parse it
27791 now. */
27792 if (DECL_PENDING_INLINE_P (member_function))
27794 tree function_scope;
27795 cp_token_cache *tokens;
27797 /* The function is no longer pending; we are processing it. */
27798 tokens = DECL_PENDING_INLINE_INFO (member_function);
27799 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27800 DECL_PENDING_INLINE_P (member_function) = 0;
27802 /* If this is a local class, enter the scope of the containing
27803 function. */
27804 function_scope = current_function_decl;
27805 if (function_scope)
27806 push_function_context ();
27808 /* Push the body of the function onto the lexer stack. */
27809 cp_parser_push_lexer_for_tokens (parser, tokens);
27811 /* Let the front end know that we going to be defining this
27812 function. */
27813 start_preparsed_function (member_function, NULL_TREE,
27814 SF_PRE_PARSED | SF_INCLASS_INLINE);
27816 /* Don't do access checking if it is a templated function. */
27817 if (processing_template_decl)
27818 push_deferring_access_checks (dk_no_check);
27820 /* #pragma omp declare reduction needs special parsing. */
27821 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27823 parser->lexer->in_pragma = true;
27824 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27825 finish_function (/*inline_p=*/true);
27826 cp_check_omp_declare_reduction (member_function);
27828 else
27829 /* Now, parse the body of the function. */
27830 cp_parser_function_definition_after_declarator (parser,
27831 /*inline_p=*/true);
27833 if (processing_template_decl)
27834 pop_deferring_access_checks ();
27836 /* Leave the scope of the containing function. */
27837 if (function_scope)
27838 pop_function_context ();
27839 cp_parser_pop_lexer (parser);
27842 /* Remove any template parameters from the symbol table. */
27843 maybe_end_member_template_processing ();
27845 /* Restore the queue. */
27846 pop_unparsed_function_queues (parser);
27847 timevar_pop (TV_PARSE_INMETH);
27850 /* If DECL contains any default args, remember it on the unparsed
27851 functions queue. */
27853 static void
27854 cp_parser_save_default_args (cp_parser* parser, tree decl)
27856 tree probe;
27858 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27859 probe;
27860 probe = TREE_CHAIN (probe))
27861 if (TREE_PURPOSE (probe))
27863 cp_default_arg_entry entry = {current_class_type, decl};
27864 vec_safe_push (unparsed_funs_with_default_args, entry);
27865 break;
27869 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27870 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27871 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27872 from the parameter-type-list. */
27874 static tree
27875 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27876 tree default_arg, tree parmtype)
27878 cp_token_cache *tokens;
27879 tree parsed_arg;
27880 bool dummy;
27882 if (default_arg == error_mark_node)
27883 return error_mark_node;
27885 /* Push the saved tokens for the default argument onto the parser's
27886 lexer stack. */
27887 tokens = DEFARG_TOKENS (default_arg);
27888 cp_parser_push_lexer_for_tokens (parser, tokens);
27890 start_lambda_scope (decl);
27892 /* Parse the default argument. */
27893 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27894 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27895 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27897 finish_lambda_scope ();
27899 if (parsed_arg == error_mark_node)
27900 cp_parser_skip_to_end_of_statement (parser);
27902 if (!processing_template_decl)
27904 /* In a non-template class, check conversions now. In a template,
27905 we'll wait and instantiate these as needed. */
27906 if (TREE_CODE (decl) == PARM_DECL)
27907 parsed_arg = check_default_argument (parmtype, parsed_arg,
27908 tf_warning_or_error);
27909 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27910 parsed_arg = error_mark_node;
27911 else
27912 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27915 /* If the token stream has not been completely used up, then
27916 there was extra junk after the end of the default
27917 argument. */
27918 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27920 if (TREE_CODE (decl) == PARM_DECL)
27921 cp_parser_error (parser, "expected %<,%>");
27922 else
27923 cp_parser_error (parser, "expected %<;%>");
27926 /* Revert to the main lexer. */
27927 cp_parser_pop_lexer (parser);
27929 return parsed_arg;
27932 /* FIELD is a non-static data member with an initializer which we saved for
27933 later; parse it now. */
27935 static void
27936 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27938 tree def;
27940 maybe_begin_member_template_processing (field);
27942 push_unparsed_function_queues (parser);
27943 def = cp_parser_late_parse_one_default_arg (parser, field,
27944 DECL_INITIAL (field),
27945 NULL_TREE);
27946 pop_unparsed_function_queues (parser);
27948 maybe_end_member_template_processing ();
27950 DECL_INITIAL (field) = def;
27953 /* FN is a FUNCTION_DECL which may contains a parameter with an
27954 unparsed DEFAULT_ARG. Parse the default args now. This function
27955 assumes that the current scope is the scope in which the default
27956 argument should be processed. */
27958 static void
27959 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27961 bool saved_local_variables_forbidden_p;
27962 tree parm, parmdecl;
27964 /* While we're parsing the default args, we might (due to the
27965 statement expression extension) encounter more classes. We want
27966 to handle them right away, but we don't want them getting mixed
27967 up with default args that are currently in the queue. */
27968 push_unparsed_function_queues (parser);
27970 /* Local variable names (and the `this' keyword) may not appear
27971 in a default argument. */
27972 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27973 parser->local_variables_forbidden_p = true;
27975 push_defarg_context (fn);
27977 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27978 parmdecl = DECL_ARGUMENTS (fn);
27979 parm && parm != void_list_node;
27980 parm = TREE_CHAIN (parm),
27981 parmdecl = DECL_CHAIN (parmdecl))
27983 tree default_arg = TREE_PURPOSE (parm);
27984 tree parsed_arg;
27985 vec<tree, va_gc> *insts;
27986 tree copy;
27987 unsigned ix;
27989 if (!default_arg)
27990 continue;
27992 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27993 /* This can happen for a friend declaration for a function
27994 already declared with default arguments. */
27995 continue;
27997 parsed_arg
27998 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27999 default_arg,
28000 TREE_VALUE (parm));
28001 TREE_PURPOSE (parm) = parsed_arg;
28003 /* Update any instantiations we've already created. */
28004 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28005 vec_safe_iterate (insts, ix, &copy); ix++)
28006 TREE_PURPOSE (copy) = parsed_arg;
28009 pop_defarg_context ();
28011 /* Make sure no default arg is missing. */
28012 check_default_args (fn);
28014 /* Restore the state of local_variables_forbidden_p. */
28015 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28017 /* Restore the queue. */
28018 pop_unparsed_function_queues (parser);
28021 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28023 sizeof ... ( identifier )
28025 where the 'sizeof' token has already been consumed. */
28027 static tree
28028 cp_parser_sizeof_pack (cp_parser *parser)
28030 /* Consume the `...'. */
28031 cp_lexer_consume_token (parser->lexer);
28032 maybe_warn_variadic_templates ();
28034 matching_parens parens;
28035 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28036 if (paren)
28037 parens.consume_open (parser);
28038 else
28039 permerror (cp_lexer_peek_token (parser->lexer)->location,
28040 "%<sizeof...%> argument must be surrounded by parentheses");
28042 cp_token *token = cp_lexer_peek_token (parser->lexer);
28043 tree name = cp_parser_identifier (parser);
28044 if (name == error_mark_node)
28045 return error_mark_node;
28046 /* The name is not qualified. */
28047 parser->scope = NULL_TREE;
28048 parser->qualifying_scope = NULL_TREE;
28049 parser->object_scope = NULL_TREE;
28050 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28051 if (expr == error_mark_node)
28052 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28053 token->location);
28054 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28055 expr = TREE_TYPE (expr);
28056 else if (TREE_CODE (expr) == CONST_DECL)
28057 expr = DECL_INITIAL (expr);
28058 expr = make_pack_expansion (expr);
28059 PACK_EXPANSION_SIZEOF_P (expr) = true;
28061 if (paren)
28062 parens.require_close (parser);
28064 return expr;
28067 /* Parse the operand of `sizeof' (or a similar operator). Returns
28068 either a TYPE or an expression, depending on the form of the
28069 input. The KEYWORD indicates which kind of expression we have
28070 encountered. */
28072 static tree
28073 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28075 tree expr = NULL_TREE;
28076 const char *saved_message;
28077 char *tmp;
28078 bool saved_integral_constant_expression_p;
28079 bool saved_non_integral_constant_expression_p;
28081 /* If it's a `...', then we are computing the length of a parameter
28082 pack. */
28083 if (keyword == RID_SIZEOF
28084 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28085 return cp_parser_sizeof_pack (parser);
28087 /* Types cannot be defined in a `sizeof' expression. Save away the
28088 old message. */
28089 saved_message = parser->type_definition_forbidden_message;
28090 /* And create the new one. */
28091 tmp = concat ("types may not be defined in %<",
28092 IDENTIFIER_POINTER (ridpointers[keyword]),
28093 "%> expressions", NULL);
28094 parser->type_definition_forbidden_message = tmp;
28096 /* The restrictions on constant-expressions do not apply inside
28097 sizeof expressions. */
28098 saved_integral_constant_expression_p
28099 = parser->integral_constant_expression_p;
28100 saved_non_integral_constant_expression_p
28101 = parser->non_integral_constant_expression_p;
28102 parser->integral_constant_expression_p = false;
28104 /* Do not actually evaluate the expression. */
28105 ++cp_unevaluated_operand;
28106 ++c_inhibit_evaluation_warnings;
28107 /* If it's a `(', then we might be looking at the type-id
28108 construction. */
28109 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28111 tree type = NULL_TREE;
28113 /* We can't be sure yet whether we're looking at a type-id or an
28114 expression. */
28115 cp_parser_parse_tentatively (parser);
28117 matching_parens parens;
28118 parens.consume_open (parser);
28120 /* Note: as a GNU Extension, compound literals are considered
28121 postfix-expressions as they are in C99, so they are valid
28122 arguments to sizeof. See comment in cp_parser_cast_expression
28123 for details. */
28124 if (cp_parser_compound_literal_p (parser))
28125 cp_parser_simulate_error (parser);
28126 else
28128 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28129 parser->in_type_id_in_expr_p = true;
28130 /* Look for the type-id. */
28131 type = cp_parser_type_id (parser);
28132 /* Look for the closing `)'. */
28133 parens.require_close (parser);
28134 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28137 /* If all went well, then we're done. */
28138 if (cp_parser_parse_definitely (parser))
28140 cp_decl_specifier_seq decl_specs;
28142 /* Build a trivial decl-specifier-seq. */
28143 clear_decl_specs (&decl_specs);
28144 decl_specs.type = type;
28146 /* Call grokdeclarator to figure out what type this is. */
28147 expr = grokdeclarator (NULL,
28148 &decl_specs,
28149 TYPENAME,
28150 /*initialized=*/0,
28151 /*attrlist=*/NULL);
28155 /* If the type-id production did not work out, then we must be
28156 looking at the unary-expression production. */
28157 if (!expr)
28158 expr = cp_parser_unary_expression (parser);
28160 /* Go back to evaluating expressions. */
28161 --cp_unevaluated_operand;
28162 --c_inhibit_evaluation_warnings;
28164 /* Free the message we created. */
28165 free (tmp);
28166 /* And restore the old one. */
28167 parser->type_definition_forbidden_message = saved_message;
28168 parser->integral_constant_expression_p
28169 = saved_integral_constant_expression_p;
28170 parser->non_integral_constant_expression_p
28171 = saved_non_integral_constant_expression_p;
28173 return expr;
28176 /* If the current declaration has no declarator, return true. */
28178 static bool
28179 cp_parser_declares_only_class_p (cp_parser *parser)
28181 /* If the next token is a `;' or a `,' then there is no
28182 declarator. */
28183 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28184 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28187 /* Update the DECL_SPECS to reflect the storage class indicated by
28188 KEYWORD. */
28190 static void
28191 cp_parser_set_storage_class (cp_parser *parser,
28192 cp_decl_specifier_seq *decl_specs,
28193 enum rid keyword,
28194 cp_token *token)
28196 cp_storage_class storage_class;
28198 if (parser->in_unbraced_linkage_specification_p)
28200 error_at (token->location, "invalid use of %qD in linkage specification",
28201 ridpointers[keyword]);
28202 return;
28204 else if (decl_specs->storage_class != sc_none)
28206 decl_specs->conflicting_specifiers_p = true;
28207 return;
28210 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28211 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28212 && decl_specs->gnu_thread_keyword_p)
28214 pedwarn (decl_specs->locations[ds_thread], 0,
28215 "%<__thread%> before %qD", ridpointers[keyword]);
28218 switch (keyword)
28220 case RID_AUTO:
28221 storage_class = sc_auto;
28222 break;
28223 case RID_REGISTER:
28224 storage_class = sc_register;
28225 break;
28226 case RID_STATIC:
28227 storage_class = sc_static;
28228 break;
28229 case RID_EXTERN:
28230 storage_class = sc_extern;
28231 break;
28232 case RID_MUTABLE:
28233 storage_class = sc_mutable;
28234 break;
28235 default:
28236 gcc_unreachable ();
28238 decl_specs->storage_class = storage_class;
28239 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28241 /* A storage class specifier cannot be applied alongside a typedef
28242 specifier. If there is a typedef specifier present then set
28243 conflicting_specifiers_p which will trigger an error later
28244 on in grokdeclarator. */
28245 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28246 decl_specs->conflicting_specifiers_p = true;
28249 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28250 is true, the type is a class or enum definition. */
28252 static void
28253 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28254 tree type_spec,
28255 cp_token *token,
28256 bool type_definition_p)
28258 decl_specs->any_specifiers_p = true;
28260 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28261 (with, for example, in "typedef int wchar_t;") we remember that
28262 this is what happened. In system headers, we ignore these
28263 declarations so that G++ can work with system headers that are not
28264 C++-safe. */
28265 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28266 && !type_definition_p
28267 && (type_spec == boolean_type_node
28268 || type_spec == char16_type_node
28269 || type_spec == char32_type_node
28270 || type_spec == wchar_type_node)
28271 && (decl_specs->type
28272 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28273 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28274 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28275 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28277 decl_specs->redefined_builtin_type = type_spec;
28278 set_and_check_decl_spec_loc (decl_specs,
28279 ds_redefined_builtin_type_spec,
28280 token);
28281 if (!decl_specs->type)
28283 decl_specs->type = type_spec;
28284 decl_specs->type_definition_p = false;
28285 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28288 else if (decl_specs->type)
28289 decl_specs->multiple_types_p = true;
28290 else
28292 decl_specs->type = type_spec;
28293 decl_specs->type_definition_p = type_definition_p;
28294 decl_specs->redefined_builtin_type = NULL_TREE;
28295 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28299 /* True iff TOKEN is the GNU keyword __thread. */
28301 static bool
28302 token_is__thread (cp_token *token)
28304 gcc_assert (token->keyword == RID_THREAD);
28305 return id_equal (token->u.value, "__thread");
28308 /* Set the location for a declarator specifier and check if it is
28309 duplicated.
28311 DECL_SPECS is the sequence of declarator specifiers onto which to
28312 set the location.
28314 DS is the single declarator specifier to set which location is to
28315 be set onto the existing sequence of declarators.
28317 LOCATION is the location for the declarator specifier to
28318 consider. */
28320 static void
28321 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28322 cp_decl_spec ds, cp_token *token)
28324 gcc_assert (ds < ds_last);
28326 if (decl_specs == NULL)
28327 return;
28329 source_location location = token->location;
28331 if (decl_specs->locations[ds] == 0)
28333 decl_specs->locations[ds] = location;
28334 if (ds == ds_thread)
28335 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28337 else
28339 if (ds == ds_long)
28341 if (decl_specs->locations[ds_long_long] != 0)
28342 error_at (location,
28343 "%<long long long%> is too long for GCC");
28344 else
28346 decl_specs->locations[ds_long_long] = location;
28347 pedwarn_cxx98 (location,
28348 OPT_Wlong_long,
28349 "ISO C++ 1998 does not support %<long long%>");
28352 else if (ds == ds_thread)
28354 bool gnu = token_is__thread (token);
28355 if (gnu != decl_specs->gnu_thread_keyword_p)
28356 error_at (location,
28357 "both %<__thread%> and %<thread_local%> specified");
28358 else
28360 gcc_rich_location richloc (location);
28361 richloc.add_fixit_remove ();
28362 error_at (&richloc, "duplicate %qD", token->u.value);
28365 else
28367 static const char *const decl_spec_names[] = {
28368 "signed",
28369 "unsigned",
28370 "short",
28371 "long",
28372 "const",
28373 "volatile",
28374 "restrict",
28375 "inline",
28376 "virtual",
28377 "explicit",
28378 "friend",
28379 "typedef",
28380 "using",
28381 "constexpr",
28382 "__complex"
28384 gcc_rich_location richloc (location);
28385 richloc.add_fixit_remove ();
28386 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28391 /* Return true iff the declarator specifier DS is present in the
28392 sequence of declarator specifiers DECL_SPECS. */
28394 bool
28395 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28396 cp_decl_spec ds)
28398 gcc_assert (ds < ds_last);
28400 if (decl_specs == NULL)
28401 return false;
28403 return decl_specs->locations[ds] != 0;
28406 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28407 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28409 static bool
28410 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28412 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28415 /* Issue an error message indicating that TOKEN_DESC was expected.
28416 If KEYWORD is true, it indicated this function is called by
28417 cp_parser_require_keword and the required token can only be
28418 a indicated keyword.
28420 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28421 within any error as the location of an "opening" token matching
28422 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28423 RT_CLOSE_PAREN). */
28425 static void
28426 cp_parser_required_error (cp_parser *parser,
28427 required_token token_desc,
28428 bool keyword,
28429 location_t matching_location)
28431 if (cp_parser_simulate_error (parser))
28432 return;
28434 const char *gmsgid = NULL;
28435 switch (token_desc)
28437 case RT_NEW:
28438 gmsgid = G_("expected %<new%>");
28439 break;
28440 case RT_DELETE:
28441 gmsgid = G_("expected %<delete%>");
28442 break;
28443 case RT_RETURN:
28444 gmsgid = G_("expected %<return%>");
28445 break;
28446 case RT_WHILE:
28447 gmsgid = G_("expected %<while%>");
28448 break;
28449 case RT_EXTERN:
28450 gmsgid = G_("expected %<extern%>");
28451 break;
28452 case RT_STATIC_ASSERT:
28453 gmsgid = G_("expected %<static_assert%>");
28454 break;
28455 case RT_DECLTYPE:
28456 gmsgid = G_("expected %<decltype%>");
28457 break;
28458 case RT_OPERATOR:
28459 gmsgid = G_("expected %<operator%>");
28460 break;
28461 case RT_CLASS:
28462 gmsgid = G_("expected %<class%>");
28463 break;
28464 case RT_TEMPLATE:
28465 gmsgid = G_("expected %<template%>");
28466 break;
28467 case RT_NAMESPACE:
28468 gmsgid = G_("expected %<namespace%>");
28469 break;
28470 case RT_USING:
28471 gmsgid = G_("expected %<using%>");
28472 break;
28473 case RT_ASM:
28474 gmsgid = G_("expected %<asm%>");
28475 break;
28476 case RT_TRY:
28477 gmsgid = G_("expected %<try%>");
28478 break;
28479 case RT_CATCH:
28480 gmsgid = G_("expected %<catch%>");
28481 break;
28482 case RT_THROW:
28483 gmsgid = G_("expected %<throw%>");
28484 break;
28485 case RT_LABEL:
28486 gmsgid = G_("expected %<__label__%>");
28487 break;
28488 case RT_AT_TRY:
28489 gmsgid = G_("expected %<@try%>");
28490 break;
28491 case RT_AT_SYNCHRONIZED:
28492 gmsgid = G_("expected %<@synchronized%>");
28493 break;
28494 case RT_AT_THROW:
28495 gmsgid = G_("expected %<@throw%>");
28496 break;
28497 case RT_TRANSACTION_ATOMIC:
28498 gmsgid = G_("expected %<__transaction_atomic%>");
28499 break;
28500 case RT_TRANSACTION_RELAXED:
28501 gmsgid = G_("expected %<__transaction_relaxed%>");
28502 break;
28503 default:
28504 break;
28507 if (!gmsgid && !keyword)
28509 switch (token_desc)
28511 case RT_SEMICOLON:
28512 gmsgid = G_("expected %<;%>");
28513 break;
28514 case RT_OPEN_PAREN:
28515 gmsgid = G_("expected %<(%>");
28516 break;
28517 case RT_CLOSE_BRACE:
28518 gmsgid = G_("expected %<}%>");
28519 break;
28520 case RT_OPEN_BRACE:
28521 gmsgid = G_("expected %<{%>");
28522 break;
28523 case RT_CLOSE_SQUARE:
28524 gmsgid = G_("expected %<]%>");
28525 break;
28526 case RT_OPEN_SQUARE:
28527 gmsgid = G_("expected %<[%>");
28528 break;
28529 case RT_COMMA:
28530 gmsgid = G_("expected %<,%>");
28531 break;
28532 case RT_SCOPE:
28533 gmsgid = G_("expected %<::%>");
28534 break;
28535 case RT_LESS:
28536 gmsgid = G_("expected %<<%>");
28537 break;
28538 case RT_GREATER:
28539 gmsgid = G_("expected %<>%>");
28540 break;
28541 case RT_EQ:
28542 gmsgid = G_("expected %<=%>");
28543 break;
28544 case RT_ELLIPSIS:
28545 gmsgid = G_("expected %<...%>");
28546 break;
28547 case RT_MULT:
28548 gmsgid = G_("expected %<*%>");
28549 break;
28550 case RT_COMPL:
28551 gmsgid = G_("expected %<~%>");
28552 break;
28553 case RT_COLON:
28554 gmsgid = G_("expected %<:%>");
28555 break;
28556 case RT_COLON_SCOPE:
28557 gmsgid = G_("expected %<:%> or %<::%>");
28558 break;
28559 case RT_CLOSE_PAREN:
28560 gmsgid = G_("expected %<)%>");
28561 break;
28562 case RT_COMMA_CLOSE_PAREN:
28563 gmsgid = G_("expected %<,%> or %<)%>");
28564 break;
28565 case RT_PRAGMA_EOL:
28566 gmsgid = G_("expected end of line");
28567 break;
28568 case RT_NAME:
28569 gmsgid = G_("expected identifier");
28570 break;
28571 case RT_SELECT:
28572 gmsgid = G_("expected selection-statement");
28573 break;
28574 case RT_ITERATION:
28575 gmsgid = G_("expected iteration-statement");
28576 break;
28577 case RT_JUMP:
28578 gmsgid = G_("expected jump-statement");
28579 break;
28580 case RT_CLASS_KEY:
28581 gmsgid = G_("expected class-key");
28582 break;
28583 case RT_CLASS_TYPENAME_TEMPLATE:
28584 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28585 break;
28586 default:
28587 gcc_unreachable ();
28591 if (gmsgid)
28592 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28596 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28597 issue an error message indicating that TOKEN_DESC was expected.
28599 Returns the token consumed, if the token had the appropriate type.
28600 Otherwise, returns NULL.
28602 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28603 within any error as the location of an "opening" token matching
28604 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28605 RT_CLOSE_PAREN). */
28607 static cp_token *
28608 cp_parser_require (cp_parser* parser,
28609 enum cpp_ttype type,
28610 required_token token_desc,
28611 location_t matching_location)
28613 if (cp_lexer_next_token_is (parser->lexer, type))
28614 return cp_lexer_consume_token (parser->lexer);
28615 else
28617 /* Output the MESSAGE -- unless we're parsing tentatively. */
28618 if (!cp_parser_simulate_error (parser))
28619 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28620 matching_location);
28621 return NULL;
28625 /* An error message is produced if the next token is not '>'.
28626 All further tokens are skipped until the desired token is
28627 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28629 static void
28630 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28632 /* Current level of '< ... >'. */
28633 unsigned level = 0;
28634 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28635 unsigned nesting_depth = 0;
28637 /* Are we ready, yet? If not, issue error message. */
28638 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28639 return;
28641 /* Skip tokens until the desired token is found. */
28642 while (true)
28644 /* Peek at the next token. */
28645 switch (cp_lexer_peek_token (parser->lexer)->type)
28647 case CPP_LESS:
28648 if (!nesting_depth)
28649 ++level;
28650 break;
28652 case CPP_RSHIFT:
28653 if (cxx_dialect == cxx98)
28654 /* C++0x views the `>>' operator as two `>' tokens, but
28655 C++98 does not. */
28656 break;
28657 else if (!nesting_depth && level-- == 0)
28659 /* We've hit a `>>' where the first `>' closes the
28660 template argument list, and the second `>' is
28661 spurious. Just consume the `>>' and stop; we've
28662 already produced at least one error. */
28663 cp_lexer_consume_token (parser->lexer);
28664 return;
28666 /* Fall through for C++0x, so we handle the second `>' in
28667 the `>>'. */
28668 gcc_fallthrough ();
28670 case CPP_GREATER:
28671 if (!nesting_depth && level-- == 0)
28673 /* We've reached the token we want, consume it and stop. */
28674 cp_lexer_consume_token (parser->lexer);
28675 return;
28677 break;
28679 case CPP_OPEN_PAREN:
28680 case CPP_OPEN_SQUARE:
28681 ++nesting_depth;
28682 break;
28684 case CPP_CLOSE_PAREN:
28685 case CPP_CLOSE_SQUARE:
28686 if (nesting_depth-- == 0)
28687 return;
28688 break;
28690 case CPP_EOF:
28691 case CPP_PRAGMA_EOL:
28692 case CPP_SEMICOLON:
28693 case CPP_OPEN_BRACE:
28694 case CPP_CLOSE_BRACE:
28695 /* The '>' was probably forgotten, don't look further. */
28696 return;
28698 default:
28699 break;
28702 /* Consume this token. */
28703 cp_lexer_consume_token (parser->lexer);
28707 /* If the next token is the indicated keyword, consume it. Otherwise,
28708 issue an error message indicating that TOKEN_DESC was expected.
28710 Returns the token consumed, if the token had the appropriate type.
28711 Otherwise, returns NULL. */
28713 static cp_token *
28714 cp_parser_require_keyword (cp_parser* parser,
28715 enum rid keyword,
28716 required_token token_desc)
28718 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28720 if (token && token->keyword != keyword)
28722 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28723 UNKNOWN_LOCATION);
28724 return NULL;
28727 return token;
28730 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28731 function-definition. */
28733 static bool
28734 cp_parser_token_starts_function_definition_p (cp_token* token)
28736 return (/* An ordinary function-body begins with an `{'. */
28737 token->type == CPP_OPEN_BRACE
28738 /* A ctor-initializer begins with a `:'. */
28739 || token->type == CPP_COLON
28740 /* A function-try-block begins with `try'. */
28741 || token->keyword == RID_TRY
28742 /* A function-transaction-block begins with `__transaction_atomic'
28743 or `__transaction_relaxed'. */
28744 || token->keyword == RID_TRANSACTION_ATOMIC
28745 || token->keyword == RID_TRANSACTION_RELAXED
28746 /* The named return value extension begins with `return'. */
28747 || token->keyword == RID_RETURN);
28750 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28751 definition. */
28753 static bool
28754 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28756 cp_token *token;
28758 token = cp_lexer_peek_token (parser->lexer);
28759 return (token->type == CPP_OPEN_BRACE
28760 || (token->type == CPP_COLON
28761 && !parser->colon_doesnt_start_class_def_p));
28764 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28765 C++0x) ending a template-argument. */
28767 static bool
28768 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28770 cp_token *token;
28772 token = cp_lexer_peek_token (parser->lexer);
28773 return (token->type == CPP_COMMA
28774 || token->type == CPP_GREATER
28775 || token->type == CPP_ELLIPSIS
28776 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28779 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28780 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28782 static bool
28783 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28784 size_t n)
28786 cp_token *token;
28788 token = cp_lexer_peek_nth_token (parser->lexer, n);
28789 if (token->type == CPP_LESS)
28790 return true;
28791 /* Check for the sequence `<::' in the original code. It would be lexed as
28792 `[:', where `[' is a digraph, and there is no whitespace before
28793 `:'. */
28794 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28796 cp_token *token2;
28797 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28798 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28799 return true;
28801 return false;
28804 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28805 or none_type otherwise. */
28807 static enum tag_types
28808 cp_parser_token_is_class_key (cp_token* token)
28810 switch (token->keyword)
28812 case RID_CLASS:
28813 return class_type;
28814 case RID_STRUCT:
28815 return record_type;
28816 case RID_UNION:
28817 return union_type;
28819 default:
28820 return none_type;
28824 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28825 or none_type otherwise or if the token is null. */
28827 static enum tag_types
28828 cp_parser_token_is_type_parameter_key (cp_token* token)
28830 if (!token)
28831 return none_type;
28833 switch (token->keyword)
28835 case RID_CLASS:
28836 return class_type;
28837 case RID_TYPENAME:
28838 return typename_type;
28840 default:
28841 return none_type;
28845 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28847 static void
28848 cp_parser_check_class_key (enum tag_types class_key, tree type)
28850 if (type == error_mark_node)
28851 return;
28852 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28854 if (permerror (input_location, "%qs tag used in naming %q#T",
28855 class_key == union_type ? "union"
28856 : class_key == record_type ? "struct" : "class",
28857 type))
28858 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28859 "%q#T was previously declared here", type);
28863 /* Issue an error message if DECL is redeclared with different
28864 access than its original declaration [class.access.spec/3].
28865 This applies to nested classes, nested class templates and
28866 enumerations [class.mem/1]. */
28868 static void
28869 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28871 if (!decl
28872 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28873 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28874 return;
28876 if ((TREE_PRIVATE (decl)
28877 != (current_access_specifier == access_private_node))
28878 || (TREE_PROTECTED (decl)
28879 != (current_access_specifier == access_protected_node)))
28880 error_at (location, "%qD redeclared with different access", decl);
28883 /* Look for the `template' keyword, as a syntactic disambiguator.
28884 Return TRUE iff it is present, in which case it will be
28885 consumed. */
28887 static bool
28888 cp_parser_optional_template_keyword (cp_parser *parser)
28890 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28892 /* In C++98 the `template' keyword can only be used within templates;
28893 outside templates the parser can always figure out what is a
28894 template and what is not. In C++11, per the resolution of DR 468,
28895 `template' is allowed in cases where it is not strictly necessary. */
28896 if (!processing_template_decl
28897 && pedantic && cxx_dialect == cxx98)
28899 cp_token *token = cp_lexer_peek_token (parser->lexer);
28900 pedwarn (token->location, OPT_Wpedantic,
28901 "in C++98 %<template%> (as a disambiguator) is only "
28902 "allowed within templates");
28903 /* If this part of the token stream is rescanned, the same
28904 error message would be generated. So, we purge the token
28905 from the stream. */
28906 cp_lexer_purge_token (parser->lexer);
28907 return false;
28909 else
28911 /* Consume the `template' keyword. */
28912 cp_lexer_consume_token (parser->lexer);
28913 return true;
28916 return false;
28919 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28920 set PARSER->SCOPE, and perform other related actions. */
28922 static void
28923 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28925 struct tree_check *check_value;
28927 /* Get the stored value. */
28928 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28929 /* Set the scope from the stored value. */
28930 parser->scope = saved_checks_value (check_value);
28931 parser->qualifying_scope = check_value->qualifying_scope;
28932 parser->object_scope = NULL_TREE;
28935 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28936 encounter the end of a block before what we were looking for. */
28938 static bool
28939 cp_parser_cache_group (cp_parser *parser,
28940 enum cpp_ttype end,
28941 unsigned depth)
28943 while (true)
28945 cp_token *token = cp_lexer_peek_token (parser->lexer);
28947 /* Abort a parenthesized expression if we encounter a semicolon. */
28948 if ((end == CPP_CLOSE_PAREN || depth == 0)
28949 && token->type == CPP_SEMICOLON)
28950 return true;
28951 /* If we've reached the end of the file, stop. */
28952 if (token->type == CPP_EOF
28953 || (end != CPP_PRAGMA_EOL
28954 && token->type == CPP_PRAGMA_EOL))
28955 return true;
28956 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28957 /* We've hit the end of an enclosing block, so there's been some
28958 kind of syntax error. */
28959 return true;
28961 /* Consume the token. */
28962 cp_lexer_consume_token (parser->lexer);
28963 /* See if it starts a new group. */
28964 if (token->type == CPP_OPEN_BRACE)
28966 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28967 /* In theory this should probably check end == '}', but
28968 cp_parser_save_member_function_body needs it to exit
28969 after either '}' or ')' when called with ')'. */
28970 if (depth == 0)
28971 return false;
28973 else if (token->type == CPP_OPEN_PAREN)
28975 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28976 if (depth == 0 && end == CPP_CLOSE_PAREN)
28977 return false;
28979 else if (token->type == CPP_PRAGMA)
28980 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28981 else if (token->type == end)
28982 return false;
28986 /* Like above, for caching a default argument or NSDMI. Both of these are
28987 terminated by a non-nested comma, but it can be unclear whether or not a
28988 comma is nested in a template argument list unless we do more parsing.
28989 In order to handle this ambiguity, when we encounter a ',' after a '<'
28990 we try to parse what follows as a parameter-declaration-list (in the
28991 case of a default argument) or a member-declarator (in the case of an
28992 NSDMI). If that succeeds, then we stop caching. */
28994 static tree
28995 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28997 unsigned depth = 0;
28998 int maybe_template_id = 0;
28999 cp_token *first_token;
29000 cp_token *token;
29001 tree default_argument;
29003 /* Add tokens until we have processed the entire default
29004 argument. We add the range [first_token, token). */
29005 first_token = cp_lexer_peek_token (parser->lexer);
29006 if (first_token->type == CPP_OPEN_BRACE)
29008 /* For list-initialization, this is straightforward. */
29009 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29010 token = cp_lexer_peek_token (parser->lexer);
29012 else while (true)
29014 bool done = false;
29016 /* Peek at the next token. */
29017 token = cp_lexer_peek_token (parser->lexer);
29018 /* What we do depends on what token we have. */
29019 switch (token->type)
29021 /* In valid code, a default argument must be
29022 immediately followed by a `,' `)', or `...'. */
29023 case CPP_COMMA:
29024 if (depth == 0 && maybe_template_id)
29026 /* If we've seen a '<', we might be in a
29027 template-argument-list. Until Core issue 325 is
29028 resolved, we don't know how this situation ought
29029 to be handled, so try to DTRT. We check whether
29030 what comes after the comma is a valid parameter
29031 declaration list. If it is, then the comma ends
29032 the default argument; otherwise the default
29033 argument continues. */
29034 bool error = false;
29035 cp_token *peek;
29037 /* Set ITALP so cp_parser_parameter_declaration_list
29038 doesn't decide to commit to this parse. */
29039 bool saved_italp = parser->in_template_argument_list_p;
29040 parser->in_template_argument_list_p = true;
29042 cp_parser_parse_tentatively (parser);
29044 if (nsdmi)
29046 /* Parse declarators until we reach a non-comma or
29047 somthing that cannot be an initializer.
29048 Just checking whether we're looking at a single
29049 declarator is insufficient. Consider:
29050 int var = tuple<T,U>::x;
29051 The template parameter 'U' looks exactly like a
29052 declarator. */
29055 int ctor_dtor_or_conv_p;
29056 cp_lexer_consume_token (parser->lexer);
29057 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29058 &ctor_dtor_or_conv_p,
29059 /*parenthesized_p=*/NULL,
29060 /*member_p=*/true,
29061 /*friend_p=*/false);
29062 peek = cp_lexer_peek_token (parser->lexer);
29063 if (cp_parser_error_occurred (parser))
29064 break;
29066 while (peek->type == CPP_COMMA);
29067 /* If we met an '=' or ';' then the original comma
29068 was the end of the NSDMI. Otherwise assume
29069 we're still in the NSDMI. */
29070 error = (peek->type != CPP_EQ
29071 && peek->type != CPP_SEMICOLON);
29073 else
29075 cp_lexer_consume_token (parser->lexer);
29076 begin_scope (sk_function_parms, NULL_TREE);
29077 if (cp_parser_parameter_declaration_list (parser)
29078 == error_mark_node)
29079 error = true;
29080 pop_bindings_and_leave_scope ();
29082 if (!cp_parser_error_occurred (parser) && !error)
29083 done = true;
29084 cp_parser_abort_tentative_parse (parser);
29086 parser->in_template_argument_list_p = saved_italp;
29087 break;
29089 /* FALLTHRU */
29090 case CPP_CLOSE_PAREN:
29091 case CPP_ELLIPSIS:
29092 /* If we run into a non-nested `;', `}', or `]',
29093 then the code is invalid -- but the default
29094 argument is certainly over. */
29095 case CPP_SEMICOLON:
29096 case CPP_CLOSE_BRACE:
29097 case CPP_CLOSE_SQUARE:
29098 if (depth == 0
29099 /* Handle correctly int n = sizeof ... ( p ); */
29100 && token->type != CPP_ELLIPSIS)
29101 done = true;
29102 /* Update DEPTH, if necessary. */
29103 else if (token->type == CPP_CLOSE_PAREN
29104 || token->type == CPP_CLOSE_BRACE
29105 || token->type == CPP_CLOSE_SQUARE)
29106 --depth;
29107 break;
29109 case CPP_OPEN_PAREN:
29110 case CPP_OPEN_SQUARE:
29111 case CPP_OPEN_BRACE:
29112 ++depth;
29113 break;
29115 case CPP_LESS:
29116 if (depth == 0)
29117 /* This might be the comparison operator, or it might
29118 start a template argument list. */
29119 ++maybe_template_id;
29120 break;
29122 case CPP_RSHIFT:
29123 if (cxx_dialect == cxx98)
29124 break;
29125 /* Fall through for C++0x, which treats the `>>'
29126 operator like two `>' tokens in certain
29127 cases. */
29128 gcc_fallthrough ();
29130 case CPP_GREATER:
29131 if (depth == 0)
29133 /* This might be an operator, or it might close a
29134 template argument list. But if a previous '<'
29135 started a template argument list, this will have
29136 closed it, so we can't be in one anymore. */
29137 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29138 if (maybe_template_id < 0)
29139 maybe_template_id = 0;
29141 break;
29143 /* If we run out of tokens, issue an error message. */
29144 case CPP_EOF:
29145 case CPP_PRAGMA_EOL:
29146 error_at (token->location, "file ends in default argument");
29147 return error_mark_node;
29149 case CPP_NAME:
29150 case CPP_SCOPE:
29151 /* In these cases, we should look for template-ids.
29152 For example, if the default argument is
29153 `X<int, double>()', we need to do name lookup to
29154 figure out whether or not `X' is a template; if
29155 so, the `,' does not end the default argument.
29157 That is not yet done. */
29158 break;
29160 default:
29161 break;
29164 /* If we've reached the end, stop. */
29165 if (done)
29166 break;
29168 /* Add the token to the token block. */
29169 token = cp_lexer_consume_token (parser->lexer);
29172 /* Create a DEFAULT_ARG to represent the unparsed default
29173 argument. */
29174 default_argument = make_node (DEFAULT_ARG);
29175 DEFARG_TOKENS (default_argument)
29176 = cp_token_cache_new (first_token, token);
29177 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29179 return default_argument;
29182 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29184 location_t
29185 defarg_location (tree default_argument)
29187 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29188 location_t start = tokens->first->location;
29189 location_t end = tokens->last->location;
29190 return make_location (start, start, end);
29193 /* Begin parsing tentatively. We always save tokens while parsing
29194 tentatively so that if the tentative parsing fails we can restore the
29195 tokens. */
29197 static void
29198 cp_parser_parse_tentatively (cp_parser* parser)
29200 /* Enter a new parsing context. */
29201 parser->context = cp_parser_context_new (parser->context);
29202 /* Begin saving tokens. */
29203 cp_lexer_save_tokens (parser->lexer);
29204 /* In order to avoid repetitive access control error messages,
29205 access checks are queued up until we are no longer parsing
29206 tentatively. */
29207 push_deferring_access_checks (dk_deferred);
29210 /* Commit to the currently active tentative parse. */
29212 static void
29213 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29215 cp_parser_context *context;
29216 cp_lexer *lexer;
29218 /* Mark all of the levels as committed. */
29219 lexer = parser->lexer;
29220 for (context = parser->context; context->next; context = context->next)
29222 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29223 break;
29224 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29225 while (!cp_lexer_saving_tokens (lexer))
29226 lexer = lexer->next;
29227 cp_lexer_commit_tokens (lexer);
29231 /* Commit to the topmost currently active tentative parse.
29233 Note that this function shouldn't be called when there are
29234 irreversible side-effects while in a tentative state. For
29235 example, we shouldn't create a permanent entry in the symbol
29236 table, or issue an error message that might not apply if the
29237 tentative parse is aborted. */
29239 static void
29240 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29242 cp_parser_context *context = parser->context;
29243 cp_lexer *lexer = parser->lexer;
29245 if (context)
29247 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29248 return;
29249 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29251 while (!cp_lexer_saving_tokens (lexer))
29252 lexer = lexer->next;
29253 cp_lexer_commit_tokens (lexer);
29257 /* Abort the currently active tentative parse. All consumed tokens
29258 will be rolled back, and no diagnostics will be issued. */
29260 static void
29261 cp_parser_abort_tentative_parse (cp_parser* parser)
29263 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29264 || errorcount > 0);
29265 cp_parser_simulate_error (parser);
29266 /* Now, pretend that we want to see if the construct was
29267 successfully parsed. */
29268 cp_parser_parse_definitely (parser);
29271 /* Stop parsing tentatively. If a parse error has occurred, restore the
29272 token stream. Otherwise, commit to the tokens we have consumed.
29273 Returns true if no error occurred; false otherwise. */
29275 static bool
29276 cp_parser_parse_definitely (cp_parser* parser)
29278 bool error_occurred;
29279 cp_parser_context *context;
29281 /* Remember whether or not an error occurred, since we are about to
29282 destroy that information. */
29283 error_occurred = cp_parser_error_occurred (parser);
29284 /* Remove the topmost context from the stack. */
29285 context = parser->context;
29286 parser->context = context->next;
29287 /* If no parse errors occurred, commit to the tentative parse. */
29288 if (!error_occurred)
29290 /* Commit to the tokens read tentatively, unless that was
29291 already done. */
29292 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29293 cp_lexer_commit_tokens (parser->lexer);
29295 pop_to_parent_deferring_access_checks ();
29297 /* Otherwise, if errors occurred, roll back our state so that things
29298 are just as they were before we began the tentative parse. */
29299 else
29301 cp_lexer_rollback_tokens (parser->lexer);
29302 pop_deferring_access_checks ();
29304 /* Add the context to the front of the free list. */
29305 context->next = cp_parser_context_free_list;
29306 cp_parser_context_free_list = context;
29308 return !error_occurred;
29311 /* Returns true if we are parsing tentatively and are not committed to
29312 this tentative parse. */
29314 static bool
29315 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29317 return (cp_parser_parsing_tentatively (parser)
29318 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29321 /* Returns nonzero iff an error has occurred during the most recent
29322 tentative parse. */
29324 static bool
29325 cp_parser_error_occurred (cp_parser* parser)
29327 return (cp_parser_parsing_tentatively (parser)
29328 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29331 /* Returns nonzero if GNU extensions are allowed. */
29333 static bool
29334 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29336 return parser->allow_gnu_extensions_p;
29339 /* Objective-C++ Productions */
29342 /* Parse an Objective-C expression, which feeds into a primary-expression
29343 above.
29345 objc-expression:
29346 objc-message-expression
29347 objc-string-literal
29348 objc-encode-expression
29349 objc-protocol-expression
29350 objc-selector-expression
29352 Returns a tree representation of the expression. */
29354 static cp_expr
29355 cp_parser_objc_expression (cp_parser* parser)
29357 /* Try to figure out what kind of declaration is present. */
29358 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29360 switch (kwd->type)
29362 case CPP_OPEN_SQUARE:
29363 return cp_parser_objc_message_expression (parser);
29365 case CPP_OBJC_STRING:
29366 kwd = cp_lexer_consume_token (parser->lexer);
29367 return objc_build_string_object (kwd->u.value);
29369 case CPP_KEYWORD:
29370 switch (kwd->keyword)
29372 case RID_AT_ENCODE:
29373 return cp_parser_objc_encode_expression (parser);
29375 case RID_AT_PROTOCOL:
29376 return cp_parser_objc_protocol_expression (parser);
29378 case RID_AT_SELECTOR:
29379 return cp_parser_objc_selector_expression (parser);
29381 default:
29382 break;
29384 /* FALLTHRU */
29385 default:
29386 error_at (kwd->location,
29387 "misplaced %<@%D%> Objective-C++ construct",
29388 kwd->u.value);
29389 cp_parser_skip_to_end_of_block_or_statement (parser);
29392 return error_mark_node;
29395 /* Parse an Objective-C message expression.
29397 objc-message-expression:
29398 [ objc-message-receiver objc-message-args ]
29400 Returns a representation of an Objective-C message. */
29402 static tree
29403 cp_parser_objc_message_expression (cp_parser* parser)
29405 tree receiver, messageargs;
29407 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29408 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29409 receiver = cp_parser_objc_message_receiver (parser);
29410 messageargs = cp_parser_objc_message_args (parser);
29411 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29412 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29414 tree result = objc_build_message_expr (receiver, messageargs);
29416 /* Construct a location e.g.
29417 [self func1:5]
29418 ^~~~~~~~~~~~~~
29419 ranging from the '[' to the ']', with the caret at the start. */
29420 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29421 protected_set_expr_location (result, combined_loc);
29423 return result;
29426 /* Parse an objc-message-receiver.
29428 objc-message-receiver:
29429 expression
29430 simple-type-specifier
29432 Returns a representation of the type or expression. */
29434 static tree
29435 cp_parser_objc_message_receiver (cp_parser* parser)
29437 tree rcv;
29439 /* An Objective-C message receiver may be either (1) a type
29440 or (2) an expression. */
29441 cp_parser_parse_tentatively (parser);
29442 rcv = cp_parser_expression (parser);
29444 /* If that worked out, fine. */
29445 if (cp_parser_parse_definitely (parser))
29446 return rcv;
29448 cp_parser_parse_tentatively (parser);
29449 rcv = cp_parser_simple_type_specifier (parser,
29450 /*decl_specs=*/NULL,
29451 CP_PARSER_FLAGS_NONE);
29453 if (cp_parser_parse_definitely (parser))
29454 return objc_get_class_reference (rcv);
29456 cp_parser_error (parser, "objective-c++ message receiver expected");
29457 return error_mark_node;
29460 /* Parse the arguments and selectors comprising an Objective-C message.
29462 objc-message-args:
29463 objc-selector
29464 objc-selector-args
29465 objc-selector-args , objc-comma-args
29467 objc-selector-args:
29468 objc-selector [opt] : assignment-expression
29469 objc-selector-args objc-selector [opt] : assignment-expression
29471 objc-comma-args:
29472 assignment-expression
29473 objc-comma-args , assignment-expression
29475 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29476 selector arguments and TREE_VALUE containing a list of comma
29477 arguments. */
29479 static tree
29480 cp_parser_objc_message_args (cp_parser* parser)
29482 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29483 bool maybe_unary_selector_p = true;
29484 cp_token *token = cp_lexer_peek_token (parser->lexer);
29486 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29488 tree selector = NULL_TREE, arg;
29490 if (token->type != CPP_COLON)
29491 selector = cp_parser_objc_selector (parser);
29493 /* Detect if we have a unary selector. */
29494 if (maybe_unary_selector_p
29495 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29496 return build_tree_list (selector, NULL_TREE);
29498 maybe_unary_selector_p = false;
29499 cp_parser_require (parser, CPP_COLON, RT_COLON);
29500 arg = cp_parser_assignment_expression (parser);
29502 sel_args
29503 = chainon (sel_args,
29504 build_tree_list (selector, arg));
29506 token = cp_lexer_peek_token (parser->lexer);
29509 /* Handle non-selector arguments, if any. */
29510 while (token->type == CPP_COMMA)
29512 tree arg;
29514 cp_lexer_consume_token (parser->lexer);
29515 arg = cp_parser_assignment_expression (parser);
29517 addl_args
29518 = chainon (addl_args,
29519 build_tree_list (NULL_TREE, arg));
29521 token = cp_lexer_peek_token (parser->lexer);
29524 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29526 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29527 return build_tree_list (error_mark_node, error_mark_node);
29530 return build_tree_list (sel_args, addl_args);
29533 /* Parse an Objective-C encode expression.
29535 objc-encode-expression:
29536 @encode objc-typename
29538 Returns an encoded representation of the type argument. */
29540 static cp_expr
29541 cp_parser_objc_encode_expression (cp_parser* parser)
29543 tree type;
29544 cp_token *token;
29545 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29547 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29548 matching_parens parens;
29549 parens.require_open (parser);
29550 token = cp_lexer_peek_token (parser->lexer);
29551 type = complete_type (cp_parser_type_id (parser));
29552 parens.require_close (parser);
29554 if (!type)
29556 error_at (token->location,
29557 "%<@encode%> must specify a type as an argument");
29558 return error_mark_node;
29561 /* This happens if we find @encode(T) (where T is a template
29562 typename or something dependent on a template typename) when
29563 parsing a template. In that case, we can't compile it
29564 immediately, but we rather create an AT_ENCODE_EXPR which will
29565 need to be instantiated when the template is used.
29567 if (dependent_type_p (type))
29569 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29570 TREE_READONLY (value) = 1;
29571 return value;
29575 /* Build a location of the form:
29576 @encode(int)
29577 ^~~~~~~~~~~~
29578 with caret==start at the @ token, finishing at the close paren. */
29579 location_t combined_loc
29580 = make_location (start_loc, start_loc,
29581 cp_lexer_previous_token (parser->lexer)->location);
29583 return cp_expr (objc_build_encode_expr (type), combined_loc);
29586 /* Parse an Objective-C @defs expression. */
29588 static tree
29589 cp_parser_objc_defs_expression (cp_parser *parser)
29591 tree name;
29593 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29594 matching_parens parens;
29595 parens.require_open (parser);
29596 name = cp_parser_identifier (parser);
29597 parens.require_close (parser);
29599 return objc_get_class_ivars (name);
29602 /* Parse an Objective-C protocol expression.
29604 objc-protocol-expression:
29605 @protocol ( identifier )
29607 Returns a representation of the protocol expression. */
29609 static tree
29610 cp_parser_objc_protocol_expression (cp_parser* parser)
29612 tree proto;
29613 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29615 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29616 matching_parens parens;
29617 parens.require_open (parser);
29618 proto = cp_parser_identifier (parser);
29619 parens.require_close (parser);
29621 /* Build a location of the form:
29622 @protocol(prot)
29623 ^~~~~~~~~~~~~~~
29624 with caret==start at the @ token, finishing at the close paren. */
29625 location_t combined_loc
29626 = make_location (start_loc, start_loc,
29627 cp_lexer_previous_token (parser->lexer)->location);
29628 tree result = objc_build_protocol_expr (proto);
29629 protected_set_expr_location (result, combined_loc);
29630 return result;
29633 /* Parse an Objective-C selector expression.
29635 objc-selector-expression:
29636 @selector ( objc-method-signature )
29638 objc-method-signature:
29639 objc-selector
29640 objc-selector-seq
29642 objc-selector-seq:
29643 objc-selector :
29644 objc-selector-seq objc-selector :
29646 Returns a representation of the method selector. */
29648 static tree
29649 cp_parser_objc_selector_expression (cp_parser* parser)
29651 tree sel_seq = NULL_TREE;
29652 bool maybe_unary_selector_p = true;
29653 cp_token *token;
29654 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29656 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29657 matching_parens parens;
29658 parens.require_open (parser);
29659 token = cp_lexer_peek_token (parser->lexer);
29661 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29662 || token->type == CPP_SCOPE)
29664 tree selector = NULL_TREE;
29666 if (token->type != CPP_COLON
29667 || token->type == CPP_SCOPE)
29668 selector = cp_parser_objc_selector (parser);
29670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29671 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29673 /* Detect if we have a unary selector. */
29674 if (maybe_unary_selector_p)
29676 sel_seq = selector;
29677 goto finish_selector;
29679 else
29681 cp_parser_error (parser, "expected %<:%>");
29684 maybe_unary_selector_p = false;
29685 token = cp_lexer_consume_token (parser->lexer);
29687 if (token->type == CPP_SCOPE)
29689 sel_seq
29690 = chainon (sel_seq,
29691 build_tree_list (selector, NULL_TREE));
29692 sel_seq
29693 = chainon (sel_seq,
29694 build_tree_list (NULL_TREE, NULL_TREE));
29696 else
29697 sel_seq
29698 = chainon (sel_seq,
29699 build_tree_list (selector, NULL_TREE));
29701 token = cp_lexer_peek_token (parser->lexer);
29704 finish_selector:
29705 parens.require_close (parser);
29708 /* Build a location of the form:
29709 @selector(func)
29710 ^~~~~~~~~~~~~~~
29711 with caret==start at the @ token, finishing at the close paren. */
29712 location_t combined_loc
29713 = make_location (loc, loc,
29714 cp_lexer_previous_token (parser->lexer)->location);
29715 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29716 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29717 protected_set_expr_location (result, combined_loc);
29718 return result;
29721 /* Parse a list of identifiers.
29723 objc-identifier-list:
29724 identifier
29725 objc-identifier-list , identifier
29727 Returns a TREE_LIST of identifier nodes. */
29729 static tree
29730 cp_parser_objc_identifier_list (cp_parser* parser)
29732 tree identifier;
29733 tree list;
29734 cp_token *sep;
29736 identifier = cp_parser_identifier (parser);
29737 if (identifier == error_mark_node)
29738 return error_mark_node;
29740 list = build_tree_list (NULL_TREE, identifier);
29741 sep = cp_lexer_peek_token (parser->lexer);
29743 while (sep->type == CPP_COMMA)
29745 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29746 identifier = cp_parser_identifier (parser);
29747 if (identifier == error_mark_node)
29748 return list;
29750 list = chainon (list, build_tree_list (NULL_TREE,
29751 identifier));
29752 sep = cp_lexer_peek_token (parser->lexer);
29755 return list;
29758 /* Parse an Objective-C alias declaration.
29760 objc-alias-declaration:
29761 @compatibility_alias identifier identifier ;
29763 This function registers the alias mapping with the Objective-C front end.
29764 It returns nothing. */
29766 static void
29767 cp_parser_objc_alias_declaration (cp_parser* parser)
29769 tree alias, orig;
29771 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29772 alias = cp_parser_identifier (parser);
29773 orig = cp_parser_identifier (parser);
29774 objc_declare_alias (alias, orig);
29775 cp_parser_consume_semicolon_at_end_of_statement (parser);
29778 /* Parse an Objective-C class forward-declaration.
29780 objc-class-declaration:
29781 @class objc-identifier-list ;
29783 The function registers the forward declarations with the Objective-C
29784 front end. It returns nothing. */
29786 static void
29787 cp_parser_objc_class_declaration (cp_parser* parser)
29789 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29790 while (true)
29792 tree id;
29794 id = cp_parser_identifier (parser);
29795 if (id == error_mark_node)
29796 break;
29798 objc_declare_class (id);
29800 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29801 cp_lexer_consume_token (parser->lexer);
29802 else
29803 break;
29805 cp_parser_consume_semicolon_at_end_of_statement (parser);
29808 /* Parse a list of Objective-C protocol references.
29810 objc-protocol-refs-opt:
29811 objc-protocol-refs [opt]
29813 objc-protocol-refs:
29814 < objc-identifier-list >
29816 Returns a TREE_LIST of identifiers, if any. */
29818 static tree
29819 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29821 tree protorefs = NULL_TREE;
29823 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29825 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29826 protorefs = cp_parser_objc_identifier_list (parser);
29827 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29830 return protorefs;
29833 /* Parse a Objective-C visibility specification. */
29835 static void
29836 cp_parser_objc_visibility_spec (cp_parser* parser)
29838 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29840 switch (vis->keyword)
29842 case RID_AT_PRIVATE:
29843 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29844 break;
29845 case RID_AT_PROTECTED:
29846 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29847 break;
29848 case RID_AT_PUBLIC:
29849 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29850 break;
29851 case RID_AT_PACKAGE:
29852 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29853 break;
29854 default:
29855 return;
29858 /* Eat '@private'/'@protected'/'@public'. */
29859 cp_lexer_consume_token (parser->lexer);
29862 /* Parse an Objective-C method type. Return 'true' if it is a class
29863 (+) method, and 'false' if it is an instance (-) method. */
29865 static inline bool
29866 cp_parser_objc_method_type (cp_parser* parser)
29868 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29869 return true;
29870 else
29871 return false;
29874 /* Parse an Objective-C protocol qualifier. */
29876 static tree
29877 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29879 tree quals = NULL_TREE, node;
29880 cp_token *token = cp_lexer_peek_token (parser->lexer);
29882 node = token->u.value;
29884 while (node && identifier_p (node)
29885 && (node == ridpointers [(int) RID_IN]
29886 || node == ridpointers [(int) RID_OUT]
29887 || node == ridpointers [(int) RID_INOUT]
29888 || node == ridpointers [(int) RID_BYCOPY]
29889 || node == ridpointers [(int) RID_BYREF]
29890 || node == ridpointers [(int) RID_ONEWAY]))
29892 quals = tree_cons (NULL_TREE, node, quals);
29893 cp_lexer_consume_token (parser->lexer);
29894 token = cp_lexer_peek_token (parser->lexer);
29895 node = token->u.value;
29898 return quals;
29901 /* Parse an Objective-C typename. */
29903 static tree
29904 cp_parser_objc_typename (cp_parser* parser)
29906 tree type_name = NULL_TREE;
29908 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29910 tree proto_quals, cp_type = NULL_TREE;
29912 matching_parens parens;
29913 parens.consume_open (parser); /* Eat '('. */
29914 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29916 /* An ObjC type name may consist of just protocol qualifiers, in which
29917 case the type shall default to 'id'. */
29918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29920 cp_type = cp_parser_type_id (parser);
29922 /* If the type could not be parsed, an error has already
29923 been produced. For error recovery, behave as if it had
29924 not been specified, which will use the default type
29925 'id'. */
29926 if (cp_type == error_mark_node)
29928 cp_type = NULL_TREE;
29929 /* We need to skip to the closing parenthesis as
29930 cp_parser_type_id() does not seem to do it for
29931 us. */
29932 cp_parser_skip_to_closing_parenthesis (parser,
29933 /*recovering=*/true,
29934 /*or_comma=*/false,
29935 /*consume_paren=*/false);
29939 parens.require_close (parser);
29940 type_name = build_tree_list (proto_quals, cp_type);
29943 return type_name;
29946 /* Check to see if TYPE refers to an Objective-C selector name. */
29948 static bool
29949 cp_parser_objc_selector_p (enum cpp_ttype type)
29951 return (type == CPP_NAME || type == CPP_KEYWORD
29952 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29953 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29954 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29955 || type == CPP_XOR || type == CPP_XOR_EQ);
29958 /* Parse an Objective-C selector. */
29960 static tree
29961 cp_parser_objc_selector (cp_parser* parser)
29963 cp_token *token = cp_lexer_consume_token (parser->lexer);
29965 if (!cp_parser_objc_selector_p (token->type))
29967 error_at (token->location, "invalid Objective-C++ selector name");
29968 return error_mark_node;
29971 /* C++ operator names are allowed to appear in ObjC selectors. */
29972 switch (token->type)
29974 case CPP_AND_AND: return get_identifier ("and");
29975 case CPP_AND_EQ: return get_identifier ("and_eq");
29976 case CPP_AND: return get_identifier ("bitand");
29977 case CPP_OR: return get_identifier ("bitor");
29978 case CPP_COMPL: return get_identifier ("compl");
29979 case CPP_NOT: return get_identifier ("not");
29980 case CPP_NOT_EQ: return get_identifier ("not_eq");
29981 case CPP_OR_OR: return get_identifier ("or");
29982 case CPP_OR_EQ: return get_identifier ("or_eq");
29983 case CPP_XOR: return get_identifier ("xor");
29984 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29985 default: return token->u.value;
29989 /* Parse an Objective-C params list. */
29991 static tree
29992 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29994 tree params = NULL_TREE;
29995 bool maybe_unary_selector_p = true;
29996 cp_token *token = cp_lexer_peek_token (parser->lexer);
29998 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30000 tree selector = NULL_TREE, type_name, identifier;
30001 tree parm_attr = NULL_TREE;
30003 if (token->keyword == RID_ATTRIBUTE)
30004 break;
30006 if (token->type != CPP_COLON)
30007 selector = cp_parser_objc_selector (parser);
30009 /* Detect if we have a unary selector. */
30010 if (maybe_unary_selector_p
30011 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30013 params = selector; /* Might be followed by attributes. */
30014 break;
30017 maybe_unary_selector_p = false;
30018 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30020 /* Something went quite wrong. There should be a colon
30021 here, but there is not. Stop parsing parameters. */
30022 break;
30024 type_name = cp_parser_objc_typename (parser);
30025 /* New ObjC allows attributes on parameters too. */
30026 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30027 parm_attr = cp_parser_attributes_opt (parser);
30028 identifier = cp_parser_identifier (parser);
30030 params
30031 = chainon (params,
30032 objc_build_keyword_decl (selector,
30033 type_name,
30034 identifier,
30035 parm_attr));
30037 token = cp_lexer_peek_token (parser->lexer);
30040 if (params == NULL_TREE)
30042 cp_parser_error (parser, "objective-c++ method declaration is expected");
30043 return error_mark_node;
30046 /* We allow tail attributes for the method. */
30047 if (token->keyword == RID_ATTRIBUTE)
30049 *attributes = cp_parser_attributes_opt (parser);
30050 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30051 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30052 return params;
30053 cp_parser_error (parser,
30054 "method attributes must be specified at the end");
30055 return error_mark_node;
30058 if (params == NULL_TREE)
30060 cp_parser_error (parser, "objective-c++ method declaration is expected");
30061 return error_mark_node;
30063 return params;
30066 /* Parse the non-keyword Objective-C params. */
30068 static tree
30069 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30070 tree* attributes)
30072 tree params = make_node (TREE_LIST);
30073 cp_token *token = cp_lexer_peek_token (parser->lexer);
30074 *ellipsisp = false; /* Initially, assume no ellipsis. */
30076 while (token->type == CPP_COMMA)
30078 cp_parameter_declarator *parmdecl;
30079 tree parm;
30081 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30082 token = cp_lexer_peek_token (parser->lexer);
30084 if (token->type == CPP_ELLIPSIS)
30086 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30087 *ellipsisp = true;
30088 token = cp_lexer_peek_token (parser->lexer);
30089 break;
30092 /* TODO: parse attributes for tail parameters. */
30093 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30094 parm = grokdeclarator (parmdecl->declarator,
30095 &parmdecl->decl_specifiers,
30096 PARM, /*initialized=*/0,
30097 /*attrlist=*/NULL);
30099 chainon (params, build_tree_list (NULL_TREE, parm));
30100 token = cp_lexer_peek_token (parser->lexer);
30103 /* We allow tail attributes for the method. */
30104 if (token->keyword == RID_ATTRIBUTE)
30106 if (*attributes == NULL_TREE)
30108 *attributes = cp_parser_attributes_opt (parser);
30109 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30110 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30111 return params;
30113 else
30114 /* We have an error, but parse the attributes, so that we can
30115 carry on. */
30116 *attributes = cp_parser_attributes_opt (parser);
30118 cp_parser_error (parser,
30119 "method attributes must be specified at the end");
30120 return error_mark_node;
30123 return params;
30126 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30128 static void
30129 cp_parser_objc_interstitial_code (cp_parser* parser)
30131 cp_token *token = cp_lexer_peek_token (parser->lexer);
30133 /* If the next token is `extern' and the following token is a string
30134 literal, then we have a linkage specification. */
30135 if (token->keyword == RID_EXTERN
30136 && cp_parser_is_pure_string_literal
30137 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30138 cp_parser_linkage_specification (parser);
30139 /* Handle #pragma, if any. */
30140 else if (token->type == CPP_PRAGMA)
30141 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30142 /* Allow stray semicolons. */
30143 else if (token->type == CPP_SEMICOLON)
30144 cp_lexer_consume_token (parser->lexer);
30145 /* Mark methods as optional or required, when building protocols. */
30146 else if (token->keyword == RID_AT_OPTIONAL)
30148 cp_lexer_consume_token (parser->lexer);
30149 objc_set_method_opt (true);
30151 else if (token->keyword == RID_AT_REQUIRED)
30153 cp_lexer_consume_token (parser->lexer);
30154 objc_set_method_opt (false);
30156 else if (token->keyword == RID_NAMESPACE)
30157 cp_parser_namespace_definition (parser);
30158 /* Other stray characters must generate errors. */
30159 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30161 cp_lexer_consume_token (parser->lexer);
30162 error ("stray %qs between Objective-C++ methods",
30163 token->type == CPP_OPEN_BRACE ? "{" : "}");
30165 /* Finally, try to parse a block-declaration, or a function-definition. */
30166 else
30167 cp_parser_block_declaration (parser, /*statement_p=*/false);
30170 /* Parse a method signature. */
30172 static tree
30173 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30175 tree rettype, kwdparms, optparms;
30176 bool ellipsis = false;
30177 bool is_class_method;
30179 is_class_method = cp_parser_objc_method_type (parser);
30180 rettype = cp_parser_objc_typename (parser);
30181 *attributes = NULL_TREE;
30182 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30183 if (kwdparms == error_mark_node)
30184 return error_mark_node;
30185 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30186 if (optparms == error_mark_node)
30187 return error_mark_node;
30189 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30192 static bool
30193 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30195 tree tattr;
30196 cp_lexer_save_tokens (parser->lexer);
30197 tattr = cp_parser_attributes_opt (parser);
30198 gcc_assert (tattr) ;
30200 /* If the attributes are followed by a method introducer, this is not allowed.
30201 Dump the attributes and flag the situation. */
30202 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30203 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30204 return true;
30206 /* Otherwise, the attributes introduce some interstitial code, possibly so
30207 rewind to allow that check. */
30208 cp_lexer_rollback_tokens (parser->lexer);
30209 return false;
30212 /* Parse an Objective-C method prototype list. */
30214 static void
30215 cp_parser_objc_method_prototype_list (cp_parser* parser)
30217 cp_token *token = cp_lexer_peek_token (parser->lexer);
30219 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30221 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30223 tree attributes, sig;
30224 bool is_class_method;
30225 if (token->type == CPP_PLUS)
30226 is_class_method = true;
30227 else
30228 is_class_method = false;
30229 sig = cp_parser_objc_method_signature (parser, &attributes);
30230 if (sig == error_mark_node)
30232 cp_parser_skip_to_end_of_block_or_statement (parser);
30233 token = cp_lexer_peek_token (parser->lexer);
30234 continue;
30236 objc_add_method_declaration (is_class_method, sig, attributes);
30237 cp_parser_consume_semicolon_at_end_of_statement (parser);
30239 else if (token->keyword == RID_AT_PROPERTY)
30240 cp_parser_objc_at_property_declaration (parser);
30241 else if (token->keyword == RID_ATTRIBUTE
30242 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30243 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30244 OPT_Wattributes,
30245 "prefix attributes are ignored for methods");
30246 else
30247 /* Allow for interspersed non-ObjC++ code. */
30248 cp_parser_objc_interstitial_code (parser);
30250 token = cp_lexer_peek_token (parser->lexer);
30253 if (token->type != CPP_EOF)
30254 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30255 else
30256 cp_parser_error (parser, "expected %<@end%>");
30258 objc_finish_interface ();
30261 /* Parse an Objective-C method definition list. */
30263 static void
30264 cp_parser_objc_method_definition_list (cp_parser* parser)
30266 cp_token *token = cp_lexer_peek_token (parser->lexer);
30268 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30270 tree meth;
30272 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30274 cp_token *ptk;
30275 tree sig, attribute;
30276 bool is_class_method;
30277 if (token->type == CPP_PLUS)
30278 is_class_method = true;
30279 else
30280 is_class_method = false;
30281 push_deferring_access_checks (dk_deferred);
30282 sig = cp_parser_objc_method_signature (parser, &attribute);
30283 if (sig == error_mark_node)
30285 cp_parser_skip_to_end_of_block_or_statement (parser);
30286 token = cp_lexer_peek_token (parser->lexer);
30287 continue;
30289 objc_start_method_definition (is_class_method, sig, attribute,
30290 NULL_TREE);
30292 /* For historical reasons, we accept an optional semicolon. */
30293 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30294 cp_lexer_consume_token (parser->lexer);
30296 ptk = cp_lexer_peek_token (parser->lexer);
30297 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30298 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30300 perform_deferred_access_checks (tf_warning_or_error);
30301 stop_deferring_access_checks ();
30302 meth = cp_parser_function_definition_after_declarator (parser,
30303 false);
30304 pop_deferring_access_checks ();
30305 objc_finish_method_definition (meth);
30308 /* The following case will be removed once @synthesize is
30309 completely implemented. */
30310 else if (token->keyword == RID_AT_PROPERTY)
30311 cp_parser_objc_at_property_declaration (parser);
30312 else if (token->keyword == RID_AT_SYNTHESIZE)
30313 cp_parser_objc_at_synthesize_declaration (parser);
30314 else if (token->keyword == RID_AT_DYNAMIC)
30315 cp_parser_objc_at_dynamic_declaration (parser);
30316 else if (token->keyword == RID_ATTRIBUTE
30317 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30318 warning_at (token->location, OPT_Wattributes,
30319 "prefix attributes are ignored for methods");
30320 else
30321 /* Allow for interspersed non-ObjC++ code. */
30322 cp_parser_objc_interstitial_code (parser);
30324 token = cp_lexer_peek_token (parser->lexer);
30327 if (token->type != CPP_EOF)
30328 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30329 else
30330 cp_parser_error (parser, "expected %<@end%>");
30332 objc_finish_implementation ();
30335 /* Parse Objective-C ivars. */
30337 static void
30338 cp_parser_objc_class_ivars (cp_parser* parser)
30340 cp_token *token = cp_lexer_peek_token (parser->lexer);
30342 if (token->type != CPP_OPEN_BRACE)
30343 return; /* No ivars specified. */
30345 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30346 token = cp_lexer_peek_token (parser->lexer);
30348 while (token->type != CPP_CLOSE_BRACE
30349 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30351 cp_decl_specifier_seq declspecs;
30352 int decl_class_or_enum_p;
30353 tree prefix_attributes;
30355 cp_parser_objc_visibility_spec (parser);
30357 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30358 break;
30360 cp_parser_decl_specifier_seq (parser,
30361 CP_PARSER_FLAGS_OPTIONAL,
30362 &declspecs,
30363 &decl_class_or_enum_p);
30365 /* auto, register, static, extern, mutable. */
30366 if (declspecs.storage_class != sc_none)
30368 cp_parser_error (parser, "invalid type for instance variable");
30369 declspecs.storage_class = sc_none;
30372 /* thread_local. */
30373 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30375 cp_parser_error (parser, "invalid type for instance variable");
30376 declspecs.locations[ds_thread] = 0;
30379 /* typedef. */
30380 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30382 cp_parser_error (parser, "invalid type for instance variable");
30383 declspecs.locations[ds_typedef] = 0;
30386 prefix_attributes = declspecs.attributes;
30387 declspecs.attributes = NULL_TREE;
30389 /* Keep going until we hit the `;' at the end of the
30390 declaration. */
30391 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30393 tree width = NULL_TREE, attributes, first_attribute, decl;
30394 cp_declarator *declarator = NULL;
30395 int ctor_dtor_or_conv_p;
30397 /* Check for a (possibly unnamed) bitfield declaration. */
30398 token = cp_lexer_peek_token (parser->lexer);
30399 if (token->type == CPP_COLON)
30400 goto eat_colon;
30402 if (token->type == CPP_NAME
30403 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30404 == CPP_COLON))
30406 /* Get the name of the bitfield. */
30407 declarator = make_id_declarator (NULL_TREE,
30408 cp_parser_identifier (parser),
30409 sfk_none);
30411 eat_colon:
30412 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30413 /* Get the width of the bitfield. */
30414 width
30415 = cp_parser_constant_expression (parser);
30417 else
30419 /* Parse the declarator. */
30420 declarator
30421 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30422 &ctor_dtor_or_conv_p,
30423 /*parenthesized_p=*/NULL,
30424 /*member_p=*/false,
30425 /*friend_p=*/false);
30428 /* Look for attributes that apply to the ivar. */
30429 attributes = cp_parser_attributes_opt (parser);
30430 /* Remember which attributes are prefix attributes and
30431 which are not. */
30432 first_attribute = attributes;
30433 /* Combine the attributes. */
30434 attributes = attr_chainon (prefix_attributes, attributes);
30436 if (width)
30437 /* Create the bitfield declaration. */
30438 decl = grokbitfield (declarator, &declspecs,
30439 width, NULL_TREE, attributes);
30440 else
30441 decl = grokfield (declarator, &declspecs,
30442 NULL_TREE, /*init_const_expr_p=*/false,
30443 NULL_TREE, attributes);
30445 /* Add the instance variable. */
30446 if (decl != error_mark_node && decl != NULL_TREE)
30447 objc_add_instance_variable (decl);
30449 /* Reset PREFIX_ATTRIBUTES. */
30450 if (attributes != error_mark_node)
30452 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30453 attributes = TREE_CHAIN (attributes);
30454 if (attributes)
30455 TREE_CHAIN (attributes) = NULL_TREE;
30458 token = cp_lexer_peek_token (parser->lexer);
30460 if (token->type == CPP_COMMA)
30462 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30463 continue;
30465 break;
30468 cp_parser_consume_semicolon_at_end_of_statement (parser);
30469 token = cp_lexer_peek_token (parser->lexer);
30472 if (token->keyword == RID_AT_END)
30473 cp_parser_error (parser, "expected %<}%>");
30475 /* Do not consume the RID_AT_END, so it will be read again as terminating
30476 the @interface of @implementation. */
30477 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30478 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30480 /* For historical reasons, we accept an optional semicolon. */
30481 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30482 cp_lexer_consume_token (parser->lexer);
30485 /* Parse an Objective-C protocol declaration. */
30487 static void
30488 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30490 tree proto, protorefs;
30491 cp_token *tok;
30493 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30496 tok = cp_lexer_peek_token (parser->lexer);
30497 error_at (tok->location, "identifier expected after %<@protocol%>");
30498 cp_parser_consume_semicolon_at_end_of_statement (parser);
30499 return;
30502 /* See if we have a forward declaration or a definition. */
30503 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30505 /* Try a forward declaration first. */
30506 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30508 while (true)
30510 tree id;
30512 id = cp_parser_identifier (parser);
30513 if (id == error_mark_node)
30514 break;
30516 objc_declare_protocol (id, attributes);
30518 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30519 cp_lexer_consume_token (parser->lexer);
30520 else
30521 break;
30523 cp_parser_consume_semicolon_at_end_of_statement (parser);
30526 /* Ok, we got a full-fledged definition (or at least should). */
30527 else
30529 proto = cp_parser_identifier (parser);
30530 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30531 objc_start_protocol (proto, protorefs, attributes);
30532 cp_parser_objc_method_prototype_list (parser);
30536 /* Parse an Objective-C superclass or category. */
30538 static void
30539 cp_parser_objc_superclass_or_category (cp_parser *parser,
30540 bool iface_p,
30541 tree *super,
30542 tree *categ, bool *is_class_extension)
30544 cp_token *next = cp_lexer_peek_token (parser->lexer);
30546 *super = *categ = NULL_TREE;
30547 *is_class_extension = false;
30548 if (next->type == CPP_COLON)
30550 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30551 *super = cp_parser_identifier (parser);
30553 else if (next->type == CPP_OPEN_PAREN)
30555 matching_parens parens;
30556 parens.consume_open (parser); /* Eat '('. */
30558 /* If there is no category name, and this is an @interface, we
30559 have a class extension. */
30560 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30562 *categ = NULL_TREE;
30563 *is_class_extension = true;
30565 else
30566 *categ = cp_parser_identifier (parser);
30568 parens.require_close (parser);
30572 /* Parse an Objective-C class interface. */
30574 static void
30575 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30577 tree name, super, categ, protos;
30578 bool is_class_extension;
30580 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30581 name = cp_parser_identifier (parser);
30582 if (name == error_mark_node)
30584 /* It's hard to recover because even if valid @interface stuff
30585 is to follow, we can't compile it (or validate it) if we
30586 don't even know which class it refers to. Let's assume this
30587 was a stray '@interface' token in the stream and skip it.
30589 return;
30591 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30592 &is_class_extension);
30593 protos = cp_parser_objc_protocol_refs_opt (parser);
30595 /* We have either a class or a category on our hands. */
30596 if (categ || is_class_extension)
30597 objc_start_category_interface (name, categ, protos, attributes);
30598 else
30600 objc_start_class_interface (name, super, protos, attributes);
30601 /* Handle instance variable declarations, if any. */
30602 cp_parser_objc_class_ivars (parser);
30603 objc_continue_interface ();
30606 cp_parser_objc_method_prototype_list (parser);
30609 /* Parse an Objective-C class implementation. */
30611 static void
30612 cp_parser_objc_class_implementation (cp_parser* parser)
30614 tree name, super, categ;
30615 bool is_class_extension;
30617 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30618 name = cp_parser_identifier (parser);
30619 if (name == error_mark_node)
30621 /* It's hard to recover because even if valid @implementation
30622 stuff is to follow, we can't compile it (or validate it) if
30623 we don't even know which class it refers to. Let's assume
30624 this was a stray '@implementation' token in the stream and
30625 skip it.
30627 return;
30629 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30630 &is_class_extension);
30632 /* We have either a class or a category on our hands. */
30633 if (categ)
30634 objc_start_category_implementation (name, categ);
30635 else
30637 objc_start_class_implementation (name, super);
30638 /* Handle instance variable declarations, if any. */
30639 cp_parser_objc_class_ivars (parser);
30640 objc_continue_implementation ();
30643 cp_parser_objc_method_definition_list (parser);
30646 /* Consume the @end token and finish off the implementation. */
30648 static void
30649 cp_parser_objc_end_implementation (cp_parser* parser)
30651 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30652 objc_finish_implementation ();
30655 /* Parse an Objective-C declaration. */
30657 static void
30658 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30660 /* Try to figure out what kind of declaration is present. */
30661 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30663 if (attributes)
30664 switch (kwd->keyword)
30666 case RID_AT_ALIAS:
30667 case RID_AT_CLASS:
30668 case RID_AT_END:
30669 error_at (kwd->location, "attributes may not be specified before"
30670 " the %<@%D%> Objective-C++ keyword",
30671 kwd->u.value);
30672 attributes = NULL;
30673 break;
30674 case RID_AT_IMPLEMENTATION:
30675 warning_at (kwd->location, OPT_Wattributes,
30676 "prefix attributes are ignored before %<@%D%>",
30677 kwd->u.value);
30678 attributes = NULL;
30679 default:
30680 break;
30683 switch (kwd->keyword)
30685 case RID_AT_ALIAS:
30686 cp_parser_objc_alias_declaration (parser);
30687 break;
30688 case RID_AT_CLASS:
30689 cp_parser_objc_class_declaration (parser);
30690 break;
30691 case RID_AT_PROTOCOL:
30692 cp_parser_objc_protocol_declaration (parser, attributes);
30693 break;
30694 case RID_AT_INTERFACE:
30695 cp_parser_objc_class_interface (parser, attributes);
30696 break;
30697 case RID_AT_IMPLEMENTATION:
30698 cp_parser_objc_class_implementation (parser);
30699 break;
30700 case RID_AT_END:
30701 cp_parser_objc_end_implementation (parser);
30702 break;
30703 default:
30704 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30705 kwd->u.value);
30706 cp_parser_skip_to_end_of_block_or_statement (parser);
30710 /* Parse an Objective-C try-catch-finally statement.
30712 objc-try-catch-finally-stmt:
30713 @try compound-statement objc-catch-clause-seq [opt]
30714 objc-finally-clause [opt]
30716 objc-catch-clause-seq:
30717 objc-catch-clause objc-catch-clause-seq [opt]
30719 objc-catch-clause:
30720 @catch ( objc-exception-declaration ) compound-statement
30722 objc-finally-clause:
30723 @finally compound-statement
30725 objc-exception-declaration:
30726 parameter-declaration
30727 '...'
30729 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30731 Returns NULL_TREE.
30733 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30734 for C. Keep them in sync. */
30736 static tree
30737 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30739 location_t location;
30740 tree stmt;
30742 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30743 location = cp_lexer_peek_token (parser->lexer)->location;
30744 objc_maybe_warn_exceptions (location);
30745 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30746 node, lest it get absorbed into the surrounding block. */
30747 stmt = push_stmt_list ();
30748 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30749 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30751 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30753 cp_parameter_declarator *parm;
30754 tree parameter_declaration = error_mark_node;
30755 bool seen_open_paren = false;
30756 matching_parens parens;
30758 cp_lexer_consume_token (parser->lexer);
30759 if (parens.require_open (parser))
30760 seen_open_paren = true;
30761 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30763 /* We have "@catch (...)" (where the '...' are literally
30764 what is in the code). Skip the '...'.
30765 parameter_declaration is set to NULL_TREE, and
30766 objc_being_catch_clauses() knows that that means
30767 '...'. */
30768 cp_lexer_consume_token (parser->lexer);
30769 parameter_declaration = NULL_TREE;
30771 else
30773 /* We have "@catch (NSException *exception)" or something
30774 like that. Parse the parameter declaration. */
30775 parm = cp_parser_parameter_declaration (parser, false, NULL);
30776 if (parm == NULL)
30777 parameter_declaration = error_mark_node;
30778 else
30779 parameter_declaration = grokdeclarator (parm->declarator,
30780 &parm->decl_specifiers,
30781 PARM, /*initialized=*/0,
30782 /*attrlist=*/NULL);
30784 if (seen_open_paren)
30785 parens.require_close (parser);
30786 else
30788 /* If there was no open parenthesis, we are recovering from
30789 an error, and we are trying to figure out what mistake
30790 the user has made. */
30792 /* If there is an immediate closing parenthesis, the user
30793 probably forgot the opening one (ie, they typed "@catch
30794 NSException *e)". Parse the closing parenthesis and keep
30795 going. */
30796 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30797 cp_lexer_consume_token (parser->lexer);
30799 /* If these is no immediate closing parenthesis, the user
30800 probably doesn't know that parenthesis are required at
30801 all (ie, they typed "@catch NSException *e"). So, just
30802 forget about the closing parenthesis and keep going. */
30804 objc_begin_catch_clause (parameter_declaration);
30805 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30806 objc_finish_catch_clause ();
30808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30810 cp_lexer_consume_token (parser->lexer);
30811 location = cp_lexer_peek_token (parser->lexer)->location;
30812 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30813 node, lest it get absorbed into the surrounding block. */
30814 stmt = push_stmt_list ();
30815 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30816 objc_build_finally_clause (location, pop_stmt_list (stmt));
30819 return objc_finish_try_stmt ();
30822 /* Parse an Objective-C synchronized statement.
30824 objc-synchronized-stmt:
30825 @synchronized ( expression ) compound-statement
30827 Returns NULL_TREE. */
30829 static tree
30830 cp_parser_objc_synchronized_statement (cp_parser *parser)
30832 location_t location;
30833 tree lock, stmt;
30835 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30837 location = cp_lexer_peek_token (parser->lexer)->location;
30838 objc_maybe_warn_exceptions (location);
30839 matching_parens parens;
30840 parens.require_open (parser);
30841 lock = cp_parser_expression (parser);
30842 parens.require_close (parser);
30844 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30845 node, lest it get absorbed into the surrounding block. */
30846 stmt = push_stmt_list ();
30847 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30849 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30852 /* Parse an Objective-C throw statement.
30854 objc-throw-stmt:
30855 @throw assignment-expression [opt] ;
30857 Returns a constructed '@throw' statement. */
30859 static tree
30860 cp_parser_objc_throw_statement (cp_parser *parser)
30862 tree expr = NULL_TREE;
30863 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30865 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30867 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30868 expr = cp_parser_expression (parser);
30870 cp_parser_consume_semicolon_at_end_of_statement (parser);
30872 return objc_build_throw_stmt (loc, expr);
30875 /* Parse an Objective-C statement. */
30877 static tree
30878 cp_parser_objc_statement (cp_parser * parser)
30880 /* Try to figure out what kind of declaration is present. */
30881 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30883 switch (kwd->keyword)
30885 case RID_AT_TRY:
30886 return cp_parser_objc_try_catch_finally_statement (parser);
30887 case RID_AT_SYNCHRONIZED:
30888 return cp_parser_objc_synchronized_statement (parser);
30889 case RID_AT_THROW:
30890 return cp_parser_objc_throw_statement (parser);
30891 default:
30892 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30893 kwd->u.value);
30894 cp_parser_skip_to_end_of_block_or_statement (parser);
30897 return error_mark_node;
30900 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30901 look ahead to see if an objc keyword follows the attributes. This
30902 is to detect the use of prefix attributes on ObjC @interface and
30903 @protocol. */
30905 static bool
30906 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30908 cp_lexer_save_tokens (parser->lexer);
30909 *attrib = cp_parser_attributes_opt (parser);
30910 gcc_assert (*attrib);
30911 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30913 cp_lexer_commit_tokens (parser->lexer);
30914 return true;
30916 cp_lexer_rollback_tokens (parser->lexer);
30917 return false;
30920 /* This routine is a minimal replacement for
30921 c_parser_struct_declaration () used when parsing the list of
30922 types/names or ObjC++ properties. For example, when parsing the
30923 code
30925 @property (readonly) int a, b, c;
30927 this function is responsible for parsing "int a, int b, int c" and
30928 returning the declarations as CHAIN of DECLs.
30930 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30931 similar parsing. */
30932 static tree
30933 cp_parser_objc_struct_declaration (cp_parser *parser)
30935 tree decls = NULL_TREE;
30936 cp_decl_specifier_seq declspecs;
30937 int decl_class_or_enum_p;
30938 tree prefix_attributes;
30940 cp_parser_decl_specifier_seq (parser,
30941 CP_PARSER_FLAGS_NONE,
30942 &declspecs,
30943 &decl_class_or_enum_p);
30945 if (declspecs.type == error_mark_node)
30946 return error_mark_node;
30948 /* auto, register, static, extern, mutable. */
30949 if (declspecs.storage_class != sc_none)
30951 cp_parser_error (parser, "invalid type for property");
30952 declspecs.storage_class = sc_none;
30955 /* thread_local. */
30956 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30958 cp_parser_error (parser, "invalid type for property");
30959 declspecs.locations[ds_thread] = 0;
30962 /* typedef. */
30963 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30965 cp_parser_error (parser, "invalid type for property");
30966 declspecs.locations[ds_typedef] = 0;
30969 prefix_attributes = declspecs.attributes;
30970 declspecs.attributes = NULL_TREE;
30972 /* Keep going until we hit the `;' at the end of the declaration. */
30973 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30975 tree attributes, first_attribute, decl;
30976 cp_declarator *declarator;
30977 cp_token *token;
30979 /* Parse the declarator. */
30980 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30981 NULL, NULL, false, false);
30983 /* Look for attributes that apply to the ivar. */
30984 attributes = cp_parser_attributes_opt (parser);
30985 /* Remember which attributes are prefix attributes and
30986 which are not. */
30987 first_attribute = attributes;
30988 /* Combine the attributes. */
30989 attributes = attr_chainon (prefix_attributes, attributes);
30991 decl = grokfield (declarator, &declspecs,
30992 NULL_TREE, /*init_const_expr_p=*/false,
30993 NULL_TREE, attributes);
30995 if (decl == error_mark_node || decl == NULL_TREE)
30996 return error_mark_node;
30998 /* Reset PREFIX_ATTRIBUTES. */
30999 if (attributes != error_mark_node)
31001 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31002 attributes = TREE_CHAIN (attributes);
31003 if (attributes)
31004 TREE_CHAIN (attributes) = NULL_TREE;
31007 DECL_CHAIN (decl) = decls;
31008 decls = decl;
31010 token = cp_lexer_peek_token (parser->lexer);
31011 if (token->type == CPP_COMMA)
31013 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31014 continue;
31016 else
31017 break;
31019 return decls;
31022 /* Parse an Objective-C @property declaration. The syntax is:
31024 objc-property-declaration:
31025 '@property' objc-property-attributes[opt] struct-declaration ;
31027 objc-property-attributes:
31028 '(' objc-property-attribute-list ')'
31030 objc-property-attribute-list:
31031 objc-property-attribute
31032 objc-property-attribute-list, objc-property-attribute
31034 objc-property-attribute
31035 'getter' = identifier
31036 'setter' = identifier
31037 'readonly'
31038 'readwrite'
31039 'assign'
31040 'retain'
31041 'copy'
31042 'nonatomic'
31044 For example:
31045 @property NSString *name;
31046 @property (readonly) id object;
31047 @property (retain, nonatomic, getter=getTheName) id name;
31048 @property int a, b, c;
31050 PS: This function is identical to
31051 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31052 static void
31053 cp_parser_objc_at_property_declaration (cp_parser *parser)
31055 /* The following variables hold the attributes of the properties as
31056 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31057 seen. When we see an attribute, we set them to 'true' (if they
31058 are boolean properties) or to the identifier (if they have an
31059 argument, ie, for getter and setter). Note that here we only
31060 parse the list of attributes, check the syntax and accumulate the
31061 attributes that we find. objc_add_property_declaration() will
31062 then process the information. */
31063 bool property_assign = false;
31064 bool property_copy = false;
31065 tree property_getter_ident = NULL_TREE;
31066 bool property_nonatomic = false;
31067 bool property_readonly = false;
31068 bool property_readwrite = false;
31069 bool property_retain = false;
31070 tree property_setter_ident = NULL_TREE;
31072 /* 'properties' is the list of properties that we read. Usually a
31073 single one, but maybe more (eg, in "@property int a, b, c;" there
31074 are three). */
31075 tree properties;
31076 location_t loc;
31078 loc = cp_lexer_peek_token (parser->lexer)->location;
31080 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31082 /* Parse the optional attribute list... */
31083 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31085 /* Eat the '('. */
31086 matching_parens parens;
31087 parens.consume_open (parser);
31089 while (true)
31091 bool syntax_error = false;
31092 cp_token *token = cp_lexer_peek_token (parser->lexer);
31093 enum rid keyword;
31095 if (token->type != CPP_NAME)
31097 cp_parser_error (parser, "expected identifier");
31098 break;
31100 keyword = C_RID_CODE (token->u.value);
31101 cp_lexer_consume_token (parser->lexer);
31102 switch (keyword)
31104 case RID_ASSIGN: property_assign = true; break;
31105 case RID_COPY: property_copy = true; break;
31106 case RID_NONATOMIC: property_nonatomic = true; break;
31107 case RID_READONLY: property_readonly = true; break;
31108 case RID_READWRITE: property_readwrite = true; break;
31109 case RID_RETAIN: property_retain = true; break;
31111 case RID_GETTER:
31112 case RID_SETTER:
31113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31115 if (keyword == RID_GETTER)
31116 cp_parser_error (parser,
31117 "missing %<=%> (after %<getter%> attribute)");
31118 else
31119 cp_parser_error (parser,
31120 "missing %<=%> (after %<setter%> attribute)");
31121 syntax_error = true;
31122 break;
31124 cp_lexer_consume_token (parser->lexer); /* eat the = */
31125 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31127 cp_parser_error (parser, "expected identifier");
31128 syntax_error = true;
31129 break;
31131 if (keyword == RID_SETTER)
31133 if (property_setter_ident != NULL_TREE)
31135 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31136 cp_lexer_consume_token (parser->lexer);
31138 else
31139 property_setter_ident = cp_parser_objc_selector (parser);
31140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31141 cp_parser_error (parser, "setter name must terminate with %<:%>");
31142 else
31143 cp_lexer_consume_token (parser->lexer);
31145 else
31147 if (property_getter_ident != NULL_TREE)
31149 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31150 cp_lexer_consume_token (parser->lexer);
31152 else
31153 property_getter_ident = cp_parser_objc_selector (parser);
31155 break;
31156 default:
31157 cp_parser_error (parser, "unknown property attribute");
31158 syntax_error = true;
31159 break;
31162 if (syntax_error)
31163 break;
31165 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31166 cp_lexer_consume_token (parser->lexer);
31167 else
31168 break;
31171 /* FIXME: "@property (setter, assign);" will generate a spurious
31172 "error: expected ‘)’ before ‘,’ token". This is because
31173 cp_parser_require, unlike the C counterpart, will produce an
31174 error even if we are in error recovery. */
31175 if (!parens.require_close (parser))
31177 cp_parser_skip_to_closing_parenthesis (parser,
31178 /*recovering=*/true,
31179 /*or_comma=*/false,
31180 /*consume_paren=*/true);
31184 /* ... and the property declaration(s). */
31185 properties = cp_parser_objc_struct_declaration (parser);
31187 if (properties == error_mark_node)
31189 cp_parser_skip_to_end_of_statement (parser);
31190 /* If the next token is now a `;', consume it. */
31191 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31192 cp_lexer_consume_token (parser->lexer);
31193 return;
31196 if (properties == NULL_TREE)
31197 cp_parser_error (parser, "expected identifier");
31198 else
31200 /* Comma-separated properties are chained together in
31201 reverse order; add them one by one. */
31202 properties = nreverse (properties);
31204 for (; properties; properties = TREE_CHAIN (properties))
31205 objc_add_property_declaration (loc, copy_node (properties),
31206 property_readonly, property_readwrite,
31207 property_assign, property_retain,
31208 property_copy, property_nonatomic,
31209 property_getter_ident, property_setter_ident);
31212 cp_parser_consume_semicolon_at_end_of_statement (parser);
31215 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31217 objc-synthesize-declaration:
31218 @synthesize objc-synthesize-identifier-list ;
31220 objc-synthesize-identifier-list:
31221 objc-synthesize-identifier
31222 objc-synthesize-identifier-list, objc-synthesize-identifier
31224 objc-synthesize-identifier
31225 identifier
31226 identifier = identifier
31228 For example:
31229 @synthesize MyProperty;
31230 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31232 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31233 for C. Keep them in sync.
31235 static void
31236 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31238 tree list = NULL_TREE;
31239 location_t loc;
31240 loc = cp_lexer_peek_token (parser->lexer)->location;
31242 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31243 while (true)
31245 tree property, ivar;
31246 property = cp_parser_identifier (parser);
31247 if (property == error_mark_node)
31249 cp_parser_consume_semicolon_at_end_of_statement (parser);
31250 return;
31252 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31254 cp_lexer_consume_token (parser->lexer);
31255 ivar = cp_parser_identifier (parser);
31256 if (ivar == error_mark_node)
31258 cp_parser_consume_semicolon_at_end_of_statement (parser);
31259 return;
31262 else
31263 ivar = NULL_TREE;
31264 list = chainon (list, build_tree_list (ivar, property));
31265 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31266 cp_lexer_consume_token (parser->lexer);
31267 else
31268 break;
31270 cp_parser_consume_semicolon_at_end_of_statement (parser);
31271 objc_add_synthesize_declaration (loc, list);
31274 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31276 objc-dynamic-declaration:
31277 @dynamic identifier-list ;
31279 For example:
31280 @dynamic MyProperty;
31281 @dynamic MyProperty, AnotherProperty;
31283 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31284 for C. Keep them in sync.
31286 static void
31287 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31289 tree list = NULL_TREE;
31290 location_t loc;
31291 loc = cp_lexer_peek_token (parser->lexer)->location;
31293 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31294 while (true)
31296 tree property;
31297 property = cp_parser_identifier (parser);
31298 if (property == error_mark_node)
31300 cp_parser_consume_semicolon_at_end_of_statement (parser);
31301 return;
31303 list = chainon (list, build_tree_list (NULL, property));
31304 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31305 cp_lexer_consume_token (parser->lexer);
31306 else
31307 break;
31309 cp_parser_consume_semicolon_at_end_of_statement (parser);
31310 objc_add_dynamic_declaration (loc, list);
31314 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31316 /* Returns name of the next clause.
31317 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31318 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31319 returned and the token is consumed. */
31321 static pragma_omp_clause
31322 cp_parser_omp_clause_name (cp_parser *parser)
31324 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31326 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31327 result = PRAGMA_OACC_CLAUSE_AUTO;
31328 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31329 result = PRAGMA_OMP_CLAUSE_IF;
31330 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31331 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31332 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31333 result = PRAGMA_OACC_CLAUSE_DELETE;
31334 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31335 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31336 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31337 result = PRAGMA_OMP_CLAUSE_FOR;
31338 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31340 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31341 const char *p = IDENTIFIER_POINTER (id);
31343 switch (p[0])
31345 case 'a':
31346 if (!strcmp ("aligned", p))
31347 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31348 else if (!strcmp ("async", p))
31349 result = PRAGMA_OACC_CLAUSE_ASYNC;
31350 break;
31351 case 'c':
31352 if (!strcmp ("collapse", p))
31353 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31354 else if (!strcmp ("copy", p))
31355 result = PRAGMA_OACC_CLAUSE_COPY;
31356 else if (!strcmp ("copyin", p))
31357 result = PRAGMA_OMP_CLAUSE_COPYIN;
31358 else if (!strcmp ("copyout", p))
31359 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31360 else if (!strcmp ("copyprivate", p))
31361 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31362 else if (!strcmp ("create", p))
31363 result = PRAGMA_OACC_CLAUSE_CREATE;
31364 break;
31365 case 'd':
31366 if (!strcmp ("defaultmap", p))
31367 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31368 else if (!strcmp ("depend", p))
31369 result = PRAGMA_OMP_CLAUSE_DEPEND;
31370 else if (!strcmp ("device", p))
31371 result = PRAGMA_OMP_CLAUSE_DEVICE;
31372 else if (!strcmp ("deviceptr", p))
31373 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31374 else if (!strcmp ("device_resident", p))
31375 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31376 else if (!strcmp ("dist_schedule", p))
31377 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31378 break;
31379 case 'f':
31380 if (!strcmp ("final", p))
31381 result = PRAGMA_OMP_CLAUSE_FINAL;
31382 else if (!strcmp ("firstprivate", p))
31383 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31384 else if (!strcmp ("from", p))
31385 result = PRAGMA_OMP_CLAUSE_FROM;
31386 break;
31387 case 'g':
31388 if (!strcmp ("gang", p))
31389 result = PRAGMA_OACC_CLAUSE_GANG;
31390 else if (!strcmp ("grainsize", p))
31391 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31392 break;
31393 case 'h':
31394 if (!strcmp ("hint", p))
31395 result = PRAGMA_OMP_CLAUSE_HINT;
31396 else if (!strcmp ("host", p))
31397 result = PRAGMA_OACC_CLAUSE_HOST;
31398 break;
31399 case 'i':
31400 if (!strcmp ("inbranch", p))
31401 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31402 else if (!strcmp ("independent", p))
31403 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31404 else if (!strcmp ("is_device_ptr", p))
31405 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31406 break;
31407 case 'l':
31408 if (!strcmp ("lastprivate", p))
31409 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31410 else if (!strcmp ("linear", p))
31411 result = PRAGMA_OMP_CLAUSE_LINEAR;
31412 else if (!strcmp ("link", p))
31413 result = PRAGMA_OMP_CLAUSE_LINK;
31414 break;
31415 case 'm':
31416 if (!strcmp ("map", p))
31417 result = PRAGMA_OMP_CLAUSE_MAP;
31418 else if (!strcmp ("mergeable", p))
31419 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31420 break;
31421 case 'n':
31422 if (!strcmp ("nogroup", p))
31423 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31424 else if (!strcmp ("notinbranch", p))
31425 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31426 else if (!strcmp ("nowait", p))
31427 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31428 else if (!strcmp ("num_gangs", p))
31429 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31430 else if (!strcmp ("num_tasks", p))
31431 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31432 else if (!strcmp ("num_teams", p))
31433 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31434 else if (!strcmp ("num_threads", p))
31435 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31436 else if (!strcmp ("num_workers", p))
31437 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31438 break;
31439 case 'o':
31440 if (!strcmp ("ordered", p))
31441 result = PRAGMA_OMP_CLAUSE_ORDERED;
31442 break;
31443 case 'p':
31444 if (!strcmp ("parallel", p))
31445 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31446 else if (!strcmp ("present", p))
31447 result = PRAGMA_OACC_CLAUSE_PRESENT;
31448 else if (!strcmp ("present_or_copy", p)
31449 || !strcmp ("pcopy", p))
31450 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31451 else if (!strcmp ("present_or_copyin", p)
31452 || !strcmp ("pcopyin", p))
31453 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31454 else if (!strcmp ("present_or_copyout", p)
31455 || !strcmp ("pcopyout", p))
31456 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31457 else if (!strcmp ("present_or_create", p)
31458 || !strcmp ("pcreate", p))
31459 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31460 else if (!strcmp ("priority", p))
31461 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31462 else if (!strcmp ("proc_bind", p))
31463 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31464 break;
31465 case 'r':
31466 if (!strcmp ("reduction", p))
31467 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31468 break;
31469 case 's':
31470 if (!strcmp ("safelen", p))
31471 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31472 else if (!strcmp ("schedule", p))
31473 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31474 else if (!strcmp ("sections", p))
31475 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31476 else if (!strcmp ("self", p))
31477 result = PRAGMA_OACC_CLAUSE_SELF;
31478 else if (!strcmp ("seq", p))
31479 result = PRAGMA_OACC_CLAUSE_SEQ;
31480 else if (!strcmp ("shared", p))
31481 result = PRAGMA_OMP_CLAUSE_SHARED;
31482 else if (!strcmp ("simd", p))
31483 result = PRAGMA_OMP_CLAUSE_SIMD;
31484 else if (!strcmp ("simdlen", p))
31485 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31486 break;
31487 case 't':
31488 if (!strcmp ("taskgroup", p))
31489 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31490 else if (!strcmp ("thread_limit", p))
31491 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31492 else if (!strcmp ("threads", p))
31493 result = PRAGMA_OMP_CLAUSE_THREADS;
31494 else if (!strcmp ("tile", p))
31495 result = PRAGMA_OACC_CLAUSE_TILE;
31496 else if (!strcmp ("to", p))
31497 result = PRAGMA_OMP_CLAUSE_TO;
31498 break;
31499 case 'u':
31500 if (!strcmp ("uniform", p))
31501 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31502 else if (!strcmp ("untied", p))
31503 result = PRAGMA_OMP_CLAUSE_UNTIED;
31504 else if (!strcmp ("use_device", p))
31505 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31506 else if (!strcmp ("use_device_ptr", p))
31507 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31508 break;
31509 case 'v':
31510 if (!strcmp ("vector", p))
31511 result = PRAGMA_OACC_CLAUSE_VECTOR;
31512 else if (!strcmp ("vector_length", p))
31513 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31514 break;
31515 case 'w':
31516 if (!strcmp ("wait", p))
31517 result = PRAGMA_OACC_CLAUSE_WAIT;
31518 else if (!strcmp ("worker", p))
31519 result = PRAGMA_OACC_CLAUSE_WORKER;
31520 break;
31524 if (result != PRAGMA_OMP_CLAUSE_NONE)
31525 cp_lexer_consume_token (parser->lexer);
31527 return result;
31530 /* Validate that a clause of the given type does not already exist. */
31532 static void
31533 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31534 const char *name, location_t location)
31536 tree c;
31538 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31539 if (OMP_CLAUSE_CODE (c) == code)
31541 error_at (location, "too many %qs clauses", name);
31542 break;
31546 /* OpenMP 2.5:
31547 variable-list:
31548 identifier
31549 variable-list , identifier
31551 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31552 colon). An opening parenthesis will have been consumed by the caller.
31554 If KIND is nonzero, create the appropriate node and install the decl
31555 in OMP_CLAUSE_DECL and add the node to the head of the list.
31557 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31558 return the list created.
31560 COLON can be NULL if only closing parenthesis should end the list,
31561 or pointer to bool which will receive false if the list is terminated
31562 by closing parenthesis or true if the list is terminated by colon. */
31564 static tree
31565 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31566 tree list, bool *colon)
31568 cp_token *token;
31569 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31570 if (colon)
31572 parser->colon_corrects_to_scope_p = false;
31573 *colon = false;
31575 while (1)
31577 tree name, decl;
31579 token = cp_lexer_peek_token (parser->lexer);
31580 if (kind != 0
31581 && current_class_ptr
31582 && cp_parser_is_keyword (token, RID_THIS))
31584 decl = finish_this_expr ();
31585 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31586 || CONVERT_EXPR_P (decl))
31587 decl = TREE_OPERAND (decl, 0);
31588 cp_lexer_consume_token (parser->lexer);
31590 else
31592 name = cp_parser_id_expression (parser, /*template_p=*/false,
31593 /*check_dependency_p=*/true,
31594 /*template_p=*/NULL,
31595 /*declarator_p=*/false,
31596 /*optional_p=*/false);
31597 if (name == error_mark_node)
31598 goto skip_comma;
31600 if (identifier_p (name))
31601 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31602 else
31603 decl = name;
31604 if (decl == error_mark_node)
31605 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31606 token->location);
31608 if (decl == error_mark_node)
31610 else if (kind != 0)
31612 switch (kind)
31614 case OMP_CLAUSE__CACHE_:
31615 /* The OpenACC cache directive explicitly only allows "array
31616 elements or subarrays". */
31617 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31619 error_at (token->location, "expected %<[%>");
31620 decl = error_mark_node;
31621 break;
31623 /* FALLTHROUGH. */
31624 case OMP_CLAUSE_MAP:
31625 case OMP_CLAUSE_FROM:
31626 case OMP_CLAUSE_TO:
31627 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31629 location_t loc
31630 = cp_lexer_peek_token (parser->lexer)->location;
31631 cp_id_kind idk = CP_ID_KIND_NONE;
31632 cp_lexer_consume_token (parser->lexer);
31633 decl = convert_from_reference (decl);
31634 decl
31635 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31636 decl, false,
31637 &idk, loc);
31639 /* FALLTHROUGH. */
31640 case OMP_CLAUSE_DEPEND:
31641 case OMP_CLAUSE_REDUCTION:
31642 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31644 tree low_bound = NULL_TREE, length = NULL_TREE;
31646 parser->colon_corrects_to_scope_p = false;
31647 cp_lexer_consume_token (parser->lexer);
31648 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31649 low_bound = cp_parser_expression (parser);
31650 if (!colon)
31651 parser->colon_corrects_to_scope_p
31652 = saved_colon_corrects_to_scope_p;
31653 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31654 length = integer_one_node;
31655 else
31657 /* Look for `:'. */
31658 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31659 goto skip_comma;
31660 if (!cp_lexer_next_token_is (parser->lexer,
31661 CPP_CLOSE_SQUARE))
31662 length = cp_parser_expression (parser);
31664 /* Look for the closing `]'. */
31665 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31666 RT_CLOSE_SQUARE))
31667 goto skip_comma;
31669 decl = tree_cons (low_bound, length, decl);
31671 break;
31672 default:
31673 break;
31676 tree u = build_omp_clause (token->location, kind);
31677 OMP_CLAUSE_DECL (u) = decl;
31678 OMP_CLAUSE_CHAIN (u) = list;
31679 list = u;
31681 else
31682 list = tree_cons (decl, NULL_TREE, list);
31684 get_comma:
31685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31686 break;
31687 cp_lexer_consume_token (parser->lexer);
31690 if (colon)
31691 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31693 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31695 *colon = true;
31696 cp_parser_require (parser, CPP_COLON, RT_COLON);
31697 return list;
31700 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31702 int ending;
31704 /* Try to resync to an unnested comma. Copied from
31705 cp_parser_parenthesized_expression_list. */
31706 skip_comma:
31707 if (colon)
31708 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31709 ending = cp_parser_skip_to_closing_parenthesis (parser,
31710 /*recovering=*/true,
31711 /*or_comma=*/true,
31712 /*consume_paren=*/true);
31713 if (ending < 0)
31714 goto get_comma;
31717 return list;
31720 /* Similarly, but expect leading and trailing parenthesis. This is a very
31721 common case for omp clauses. */
31723 static tree
31724 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31726 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31727 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31728 return list;
31731 /* OpenACC 2.0:
31732 copy ( variable-list )
31733 copyin ( variable-list )
31734 copyout ( variable-list )
31735 create ( variable-list )
31736 delete ( variable-list )
31737 present ( variable-list )
31738 present_or_copy ( variable-list )
31739 pcopy ( variable-list )
31740 present_or_copyin ( variable-list )
31741 pcopyin ( variable-list )
31742 present_or_copyout ( variable-list )
31743 pcopyout ( variable-list )
31744 present_or_create ( variable-list )
31745 pcreate ( variable-list ) */
31747 static tree
31748 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31749 tree list)
31751 enum gomp_map_kind kind;
31752 switch (c_kind)
31754 case PRAGMA_OACC_CLAUSE_COPY:
31755 kind = GOMP_MAP_FORCE_TOFROM;
31756 break;
31757 case PRAGMA_OACC_CLAUSE_COPYIN:
31758 kind = GOMP_MAP_FORCE_TO;
31759 break;
31760 case PRAGMA_OACC_CLAUSE_COPYOUT:
31761 kind = GOMP_MAP_FORCE_FROM;
31762 break;
31763 case PRAGMA_OACC_CLAUSE_CREATE:
31764 kind = GOMP_MAP_FORCE_ALLOC;
31765 break;
31766 case PRAGMA_OACC_CLAUSE_DELETE:
31767 kind = GOMP_MAP_DELETE;
31768 break;
31769 case PRAGMA_OACC_CLAUSE_DEVICE:
31770 kind = GOMP_MAP_FORCE_TO;
31771 break;
31772 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31773 kind = GOMP_MAP_DEVICE_RESIDENT;
31774 break;
31775 case PRAGMA_OACC_CLAUSE_HOST:
31776 case PRAGMA_OACC_CLAUSE_SELF:
31777 kind = GOMP_MAP_FORCE_FROM;
31778 break;
31779 case PRAGMA_OACC_CLAUSE_LINK:
31780 kind = GOMP_MAP_LINK;
31781 break;
31782 case PRAGMA_OACC_CLAUSE_PRESENT:
31783 kind = GOMP_MAP_FORCE_PRESENT;
31784 break;
31785 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31786 kind = GOMP_MAP_TOFROM;
31787 break;
31788 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31789 kind = GOMP_MAP_TO;
31790 break;
31791 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31792 kind = GOMP_MAP_FROM;
31793 break;
31794 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31795 kind = GOMP_MAP_ALLOC;
31796 break;
31797 default:
31798 gcc_unreachable ();
31800 tree nl, c;
31801 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31803 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31804 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31806 return nl;
31809 /* OpenACC 2.0:
31810 deviceptr ( variable-list ) */
31812 static tree
31813 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31815 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31816 tree vars, t;
31818 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31819 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31820 variable-list must only allow for pointer variables. */
31821 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31822 for (t = vars; t; t = TREE_CHAIN (t))
31824 tree v = TREE_PURPOSE (t);
31825 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31826 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31827 OMP_CLAUSE_DECL (u) = v;
31828 OMP_CLAUSE_CHAIN (u) = list;
31829 list = u;
31832 return list;
31835 /* OpenACC 2.0:
31836 auto
31837 independent
31838 nohost
31839 seq */
31841 static tree
31842 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31843 enum omp_clause_code code,
31844 tree list, location_t location)
31846 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31847 tree c = build_omp_clause (location, code);
31848 OMP_CLAUSE_CHAIN (c) = list;
31849 return c;
31852 /* OpenACC:
31853 num_gangs ( expression )
31854 num_workers ( expression )
31855 vector_length ( expression ) */
31857 static tree
31858 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31859 const char *str, tree list)
31861 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31863 matching_parens parens;
31864 if (!parens.require_open (parser))
31865 return list;
31867 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31869 if (t == error_mark_node
31870 || !parens.require_close (parser))
31872 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31873 /*or_comma=*/false,
31874 /*consume_paren=*/true);
31875 return list;
31878 check_no_duplicate_clause (list, code, str, loc);
31880 tree c = build_omp_clause (loc, code);
31881 OMP_CLAUSE_OPERAND (c, 0) = t;
31882 OMP_CLAUSE_CHAIN (c) = list;
31883 return c;
31886 /* OpenACC:
31888 gang [( gang-arg-list )]
31889 worker [( [num:] int-expr )]
31890 vector [( [length:] int-expr )]
31892 where gang-arg is one of:
31894 [num:] int-expr
31895 static: size-expr
31897 and size-expr may be:
31900 int-expr
31903 static tree
31904 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31905 const char *str, tree list)
31907 const char *id = "num";
31908 cp_lexer *lexer = parser->lexer;
31909 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31910 location_t loc = cp_lexer_peek_token (lexer)->location;
31912 if (kind == OMP_CLAUSE_VECTOR)
31913 id = "length";
31915 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31917 matching_parens parens;
31918 parens.consume_open (parser);
31922 cp_token *next = cp_lexer_peek_token (lexer);
31923 int idx = 0;
31925 /* Gang static argument. */
31926 if (kind == OMP_CLAUSE_GANG
31927 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31929 cp_lexer_consume_token (lexer);
31931 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31932 goto cleanup_error;
31934 idx = 1;
31935 if (ops[idx] != NULL)
31937 cp_parser_error (parser, "too many %<static%> arguments");
31938 goto cleanup_error;
31941 /* Check for the '*' argument. */
31942 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31943 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31944 || cp_lexer_nth_token_is (parser->lexer, 2,
31945 CPP_CLOSE_PAREN)))
31947 cp_lexer_consume_token (lexer);
31948 ops[idx] = integer_minus_one_node;
31950 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31952 cp_lexer_consume_token (lexer);
31953 continue;
31955 else break;
31958 /* Worker num: argument and vector length: arguments. */
31959 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31960 && id_equal (next->u.value, id)
31961 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31963 cp_lexer_consume_token (lexer); /* id */
31964 cp_lexer_consume_token (lexer); /* ':' */
31967 /* Now collect the actual argument. */
31968 if (ops[idx] != NULL_TREE)
31970 cp_parser_error (parser, "unexpected argument");
31971 goto cleanup_error;
31974 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31975 false);
31976 if (expr == error_mark_node)
31977 goto cleanup_error;
31979 mark_exp_read (expr);
31980 ops[idx] = expr;
31982 if (kind == OMP_CLAUSE_GANG
31983 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31985 cp_lexer_consume_token (lexer);
31986 continue;
31988 break;
31990 while (1);
31992 if (!parens.require_close (parser))
31993 goto cleanup_error;
31996 check_no_duplicate_clause (list, kind, str, loc);
31998 c = build_omp_clause (loc, kind);
32000 if (ops[1])
32001 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32003 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32004 OMP_CLAUSE_CHAIN (c) = list;
32006 return c;
32008 cleanup_error:
32009 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32010 return list;
32013 /* OpenACC 2.0:
32014 tile ( size-expr-list ) */
32016 static tree
32017 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32019 tree c, expr = error_mark_node;
32020 tree tile = NULL_TREE;
32022 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32023 so, but the spec authors never considered such a case and have
32024 differing opinions on what it might mean, including 'not
32025 allowed'.) */
32026 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32027 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32028 clause_loc);
32030 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32031 return list;
32035 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32036 return list;
32038 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32039 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32040 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32042 cp_lexer_consume_token (parser->lexer);
32043 expr = integer_zero_node;
32045 else
32046 expr = cp_parser_constant_expression (parser);
32048 tile = tree_cons (NULL_TREE, expr, tile);
32050 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32052 /* Consume the trailing ')'. */
32053 cp_lexer_consume_token (parser->lexer);
32055 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32056 tile = nreverse (tile);
32057 OMP_CLAUSE_TILE_LIST (c) = tile;
32058 OMP_CLAUSE_CHAIN (c) = list;
32059 return c;
32062 /* OpenACC 2.0
32063 Parse wait clause or directive parameters. */
32065 static tree
32066 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32068 vec<tree, va_gc> *args;
32069 tree t, args_tree;
32071 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32072 /*cast_p=*/false,
32073 /*allow_expansion_p=*/true,
32074 /*non_constant_p=*/NULL);
32076 if (args == NULL || args->length () == 0)
32078 cp_parser_error (parser, "expected integer expression before ')'");
32079 if (args != NULL)
32080 release_tree_vector (args);
32081 return list;
32084 args_tree = build_tree_list_vec (args);
32086 release_tree_vector (args);
32088 for (t = args_tree; t; t = TREE_CHAIN (t))
32090 tree targ = TREE_VALUE (t);
32092 if (targ != error_mark_node)
32094 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32095 error ("%<wait%> expression must be integral");
32096 else
32098 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32100 targ = mark_rvalue_use (targ);
32101 OMP_CLAUSE_DECL (c) = targ;
32102 OMP_CLAUSE_CHAIN (c) = list;
32103 list = c;
32108 return list;
32111 /* OpenACC:
32112 wait ( int-expr-list ) */
32114 static tree
32115 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32117 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32119 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32120 return list;
32122 list = cp_parser_oacc_wait_list (parser, location, list);
32124 return list;
32127 /* OpenMP 3.0:
32128 collapse ( constant-expression ) */
32130 static tree
32131 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32133 tree c, num;
32134 location_t loc;
32135 HOST_WIDE_INT n;
32137 loc = cp_lexer_peek_token (parser->lexer)->location;
32138 matching_parens parens;
32139 if (!parens.require_open (parser))
32140 return list;
32142 num = cp_parser_constant_expression (parser);
32144 if (!parens.require_close (parser))
32145 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32146 /*or_comma=*/false,
32147 /*consume_paren=*/true);
32149 if (num == error_mark_node)
32150 return list;
32151 num = fold_non_dependent_expr (num);
32152 if (!tree_fits_shwi_p (num)
32153 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32154 || (n = tree_to_shwi (num)) <= 0
32155 || (int) n != n)
32157 error_at (loc, "collapse argument needs positive constant integer expression");
32158 return list;
32161 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32162 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32163 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32164 OMP_CLAUSE_CHAIN (c) = list;
32165 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32167 return c;
32170 /* OpenMP 2.5:
32171 default ( none | shared )
32173 OpenACC:
32174 default ( none | present ) */
32176 static tree
32177 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32178 location_t location, bool is_oacc)
32180 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32181 tree c;
32183 matching_parens parens;
32184 if (!parens.require_open (parser))
32185 return list;
32186 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32188 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32189 const char *p = IDENTIFIER_POINTER (id);
32191 switch (p[0])
32193 case 'n':
32194 if (strcmp ("none", p) != 0)
32195 goto invalid_kind;
32196 kind = OMP_CLAUSE_DEFAULT_NONE;
32197 break;
32199 case 'p':
32200 if (strcmp ("present", p) != 0 || !is_oacc)
32201 goto invalid_kind;
32202 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32203 break;
32205 case 's':
32206 if (strcmp ("shared", p) != 0 || is_oacc)
32207 goto invalid_kind;
32208 kind = OMP_CLAUSE_DEFAULT_SHARED;
32209 break;
32211 default:
32212 goto invalid_kind;
32215 cp_lexer_consume_token (parser->lexer);
32217 else
32219 invalid_kind:
32220 if (is_oacc)
32221 cp_parser_error (parser, "expected %<none%> or %<present%>");
32222 else
32223 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32226 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32227 || !parens.require_close (parser))
32228 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32229 /*or_comma=*/false,
32230 /*consume_paren=*/true);
32232 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32233 return list;
32235 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32236 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32237 OMP_CLAUSE_CHAIN (c) = list;
32238 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32240 return c;
32243 /* OpenMP 3.1:
32244 final ( expression ) */
32246 static tree
32247 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32249 tree t, c;
32251 matching_parens parens;
32252 if (!parens.require_open (parser))
32253 return list;
32255 t = cp_parser_condition (parser);
32257 if (t == error_mark_node
32258 || !parens.require_close (parser))
32259 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32260 /*or_comma=*/false,
32261 /*consume_paren=*/true);
32263 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32265 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32266 OMP_CLAUSE_FINAL_EXPR (c) = t;
32267 OMP_CLAUSE_CHAIN (c) = list;
32269 return c;
32272 /* OpenMP 2.5:
32273 if ( expression )
32275 OpenMP 4.5:
32276 if ( directive-name-modifier : expression )
32278 directive-name-modifier:
32279 parallel | task | taskloop | target data | target | target update
32280 | target enter data | target exit data */
32282 static tree
32283 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32284 bool is_omp)
32286 tree t, c;
32287 enum tree_code if_modifier = ERROR_MARK;
32289 matching_parens parens;
32290 if (!parens.require_open (parser))
32291 return list;
32293 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32296 const char *p = IDENTIFIER_POINTER (id);
32297 int n = 2;
32299 if (strcmp ("parallel", p) == 0)
32300 if_modifier = OMP_PARALLEL;
32301 else if (strcmp ("task", p) == 0)
32302 if_modifier = OMP_TASK;
32303 else if (strcmp ("taskloop", p) == 0)
32304 if_modifier = OMP_TASKLOOP;
32305 else if (strcmp ("target", p) == 0)
32307 if_modifier = OMP_TARGET;
32308 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32310 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32311 p = IDENTIFIER_POINTER (id);
32312 if (strcmp ("data", p) == 0)
32313 if_modifier = OMP_TARGET_DATA;
32314 else if (strcmp ("update", p) == 0)
32315 if_modifier = OMP_TARGET_UPDATE;
32316 else if (strcmp ("enter", p) == 0)
32317 if_modifier = OMP_TARGET_ENTER_DATA;
32318 else if (strcmp ("exit", p) == 0)
32319 if_modifier = OMP_TARGET_EXIT_DATA;
32320 if (if_modifier != OMP_TARGET)
32321 n = 3;
32322 else
32324 location_t loc
32325 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32326 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32327 "or %<exit%>");
32328 if_modifier = ERROR_MARK;
32330 if (if_modifier == OMP_TARGET_ENTER_DATA
32331 || if_modifier == OMP_TARGET_EXIT_DATA)
32333 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32335 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32336 p = IDENTIFIER_POINTER (id);
32337 if (strcmp ("data", p) == 0)
32338 n = 4;
32340 if (n != 4)
32342 location_t loc
32343 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32344 error_at (loc, "expected %<data%>");
32345 if_modifier = ERROR_MARK;
32350 if (if_modifier != ERROR_MARK)
32352 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32354 while (n-- > 0)
32355 cp_lexer_consume_token (parser->lexer);
32357 else
32359 if (n > 2)
32361 location_t loc
32362 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32363 error_at (loc, "expected %<:%>");
32365 if_modifier = ERROR_MARK;
32370 t = cp_parser_condition (parser);
32372 if (t == error_mark_node
32373 || !parens.require_close (parser))
32374 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32375 /*or_comma=*/false,
32376 /*consume_paren=*/true);
32378 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32379 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32381 if (if_modifier != ERROR_MARK
32382 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32384 const char *p = NULL;
32385 switch (if_modifier)
32387 case OMP_PARALLEL: p = "parallel"; break;
32388 case OMP_TASK: p = "task"; break;
32389 case OMP_TASKLOOP: p = "taskloop"; break;
32390 case OMP_TARGET_DATA: p = "target data"; break;
32391 case OMP_TARGET: p = "target"; break;
32392 case OMP_TARGET_UPDATE: p = "target update"; break;
32393 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32394 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32395 default: gcc_unreachable ();
32397 error_at (location, "too many %<if%> clauses with %qs modifier",
32399 return list;
32401 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32403 if (!is_omp)
32404 error_at (location, "too many %<if%> clauses");
32405 else
32406 error_at (location, "too many %<if%> clauses without modifier");
32407 return list;
32409 else if (if_modifier == ERROR_MARK
32410 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32412 error_at (location, "if any %<if%> clause has modifier, then all "
32413 "%<if%> clauses have to use modifier");
32414 return list;
32418 c = build_omp_clause (location, OMP_CLAUSE_IF);
32419 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32420 OMP_CLAUSE_IF_EXPR (c) = t;
32421 OMP_CLAUSE_CHAIN (c) = list;
32423 return c;
32426 /* OpenMP 3.1:
32427 mergeable */
32429 static tree
32430 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32431 tree list, location_t location)
32433 tree c;
32435 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32436 location);
32438 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32439 OMP_CLAUSE_CHAIN (c) = list;
32440 return c;
32443 /* OpenMP 2.5:
32444 nowait */
32446 static tree
32447 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32448 tree list, location_t location)
32450 tree c;
32452 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32454 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32455 OMP_CLAUSE_CHAIN (c) = list;
32456 return c;
32459 /* OpenMP 2.5:
32460 num_threads ( expression ) */
32462 static tree
32463 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32464 location_t location)
32466 tree t, c;
32468 matching_parens parens;
32469 if (!parens.require_open (parser))
32470 return list;
32472 t = cp_parser_expression (parser);
32474 if (t == error_mark_node
32475 || !parens.require_close (parser))
32476 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32477 /*or_comma=*/false,
32478 /*consume_paren=*/true);
32480 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32481 "num_threads", location);
32483 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32484 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32485 OMP_CLAUSE_CHAIN (c) = list;
32487 return c;
32490 /* OpenMP 4.5:
32491 num_tasks ( expression ) */
32493 static tree
32494 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32495 location_t location)
32497 tree t, c;
32499 matching_parens parens;
32500 if (!parens.require_open (parser))
32501 return list;
32503 t = cp_parser_expression (parser);
32505 if (t == error_mark_node
32506 || !parens.require_close (parser))
32507 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32508 /*or_comma=*/false,
32509 /*consume_paren=*/true);
32511 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32512 "num_tasks", location);
32514 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32515 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32516 OMP_CLAUSE_CHAIN (c) = list;
32518 return c;
32521 /* OpenMP 4.5:
32522 grainsize ( expression ) */
32524 static tree
32525 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32526 location_t location)
32528 tree t, c;
32530 matching_parens parens;
32531 if (!parens.require_open (parser))
32532 return list;
32534 t = cp_parser_expression (parser);
32536 if (t == error_mark_node
32537 || !parens.require_close (parser))
32538 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32539 /*or_comma=*/false,
32540 /*consume_paren=*/true);
32542 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32543 "grainsize", location);
32545 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32546 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32547 OMP_CLAUSE_CHAIN (c) = list;
32549 return c;
32552 /* OpenMP 4.5:
32553 priority ( expression ) */
32555 static tree
32556 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32557 location_t location)
32559 tree t, c;
32561 matching_parens parens;
32562 if (!parens.require_open (parser))
32563 return list;
32565 t = cp_parser_expression (parser);
32567 if (t == error_mark_node
32568 || !parens.require_close (parser))
32569 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32570 /*or_comma=*/false,
32571 /*consume_paren=*/true);
32573 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32574 "priority", location);
32576 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32577 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32578 OMP_CLAUSE_CHAIN (c) = list;
32580 return c;
32583 /* OpenMP 4.5:
32584 hint ( expression ) */
32586 static tree
32587 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32588 location_t location)
32590 tree t, c;
32592 matching_parens parens;
32593 if (!parens.require_open (parser))
32594 return list;
32596 t = cp_parser_expression (parser);
32598 if (t == error_mark_node
32599 || !parens.require_close (parser))
32600 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32601 /*or_comma=*/false,
32602 /*consume_paren=*/true);
32604 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32606 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32607 OMP_CLAUSE_HINT_EXPR (c) = t;
32608 OMP_CLAUSE_CHAIN (c) = list;
32610 return c;
32613 /* OpenMP 4.5:
32614 defaultmap ( tofrom : scalar ) */
32616 static tree
32617 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32618 location_t location)
32620 tree c, id;
32621 const char *p;
32623 matching_parens parens;
32624 if (!parens.require_open (parser))
32625 return list;
32627 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32629 cp_parser_error (parser, "expected %<tofrom%>");
32630 goto out_err;
32632 id = cp_lexer_peek_token (parser->lexer)->u.value;
32633 p = IDENTIFIER_POINTER (id);
32634 if (strcmp (p, "tofrom") != 0)
32636 cp_parser_error (parser, "expected %<tofrom%>");
32637 goto out_err;
32639 cp_lexer_consume_token (parser->lexer);
32640 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32641 goto out_err;
32643 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32645 cp_parser_error (parser, "expected %<scalar%>");
32646 goto out_err;
32648 id = cp_lexer_peek_token (parser->lexer)->u.value;
32649 p = IDENTIFIER_POINTER (id);
32650 if (strcmp (p, "scalar") != 0)
32652 cp_parser_error (parser, "expected %<scalar%>");
32653 goto out_err;
32655 cp_lexer_consume_token (parser->lexer);
32656 if (!parens.require_close (parser))
32657 goto out_err;
32659 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32660 location);
32662 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32663 OMP_CLAUSE_CHAIN (c) = list;
32664 return c;
32666 out_err:
32667 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32668 /*or_comma=*/false,
32669 /*consume_paren=*/true);
32670 return list;
32673 /* OpenMP 2.5:
32674 ordered
32676 OpenMP 4.5:
32677 ordered ( constant-expression ) */
32679 static tree
32680 cp_parser_omp_clause_ordered (cp_parser *parser,
32681 tree list, location_t location)
32683 tree c, num = NULL_TREE;
32684 HOST_WIDE_INT n;
32686 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32687 "ordered", location);
32689 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32691 matching_parens parens;
32692 parens.consume_open (parser);
32694 num = cp_parser_constant_expression (parser);
32696 if (!parens.require_close (parser))
32697 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32698 /*or_comma=*/false,
32699 /*consume_paren=*/true);
32701 if (num == error_mark_node)
32702 return list;
32703 num = fold_non_dependent_expr (num);
32704 if (!tree_fits_shwi_p (num)
32705 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32706 || (n = tree_to_shwi (num)) <= 0
32707 || (int) n != n)
32709 error_at (location,
32710 "ordered argument needs positive constant integer "
32711 "expression");
32712 return list;
32716 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32717 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32718 OMP_CLAUSE_CHAIN (c) = list;
32719 return c;
32722 /* OpenMP 2.5:
32723 reduction ( reduction-operator : variable-list )
32725 reduction-operator:
32726 One of: + * - & ^ | && ||
32728 OpenMP 3.1:
32730 reduction-operator:
32731 One of: + * - & ^ | && || min max
32733 OpenMP 4.0:
32735 reduction-operator:
32736 One of: + * - & ^ | && ||
32737 id-expression */
32739 static tree
32740 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32742 enum tree_code code = ERROR_MARK;
32743 tree nlist, c, id = NULL_TREE;
32745 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32746 return list;
32748 switch (cp_lexer_peek_token (parser->lexer)->type)
32750 case CPP_PLUS: code = PLUS_EXPR; break;
32751 case CPP_MULT: code = MULT_EXPR; break;
32752 case CPP_MINUS: code = MINUS_EXPR; break;
32753 case CPP_AND: code = BIT_AND_EXPR; break;
32754 case CPP_XOR: code = BIT_XOR_EXPR; break;
32755 case CPP_OR: code = BIT_IOR_EXPR; break;
32756 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32757 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32758 default: break;
32761 if (code != ERROR_MARK)
32762 cp_lexer_consume_token (parser->lexer);
32763 else
32765 bool saved_colon_corrects_to_scope_p;
32766 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32767 parser->colon_corrects_to_scope_p = false;
32768 id = cp_parser_id_expression (parser, /*template_p=*/false,
32769 /*check_dependency_p=*/true,
32770 /*template_p=*/NULL,
32771 /*declarator_p=*/false,
32772 /*optional_p=*/false);
32773 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32774 if (identifier_p (id))
32776 const char *p = IDENTIFIER_POINTER (id);
32778 if (strcmp (p, "min") == 0)
32779 code = MIN_EXPR;
32780 else if (strcmp (p, "max") == 0)
32781 code = MAX_EXPR;
32782 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32783 code = PLUS_EXPR;
32784 else if (id == ovl_op_identifier (false, MULT_EXPR))
32785 code = MULT_EXPR;
32786 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32787 code = MINUS_EXPR;
32788 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32789 code = BIT_AND_EXPR;
32790 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32791 code = BIT_IOR_EXPR;
32792 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32793 code = BIT_XOR_EXPR;
32794 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32795 code = TRUTH_ANDIF_EXPR;
32796 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32797 code = TRUTH_ORIF_EXPR;
32798 id = omp_reduction_id (code, id, NULL_TREE);
32799 tree scope = parser->scope;
32800 if (scope)
32801 id = build_qualified_name (NULL_TREE, scope, id, false);
32802 parser->scope = NULL_TREE;
32803 parser->qualifying_scope = NULL_TREE;
32804 parser->object_scope = NULL_TREE;
32806 else
32808 error ("invalid reduction-identifier");
32809 resync_fail:
32810 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32811 /*or_comma=*/false,
32812 /*consume_paren=*/true);
32813 return list;
32817 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32818 goto resync_fail;
32820 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32821 NULL);
32822 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32824 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32825 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32828 return nlist;
32831 /* OpenMP 2.5:
32832 schedule ( schedule-kind )
32833 schedule ( schedule-kind , expression )
32835 schedule-kind:
32836 static | dynamic | guided | runtime | auto
32838 OpenMP 4.5:
32839 schedule ( schedule-modifier : schedule-kind )
32840 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32842 schedule-modifier:
32843 simd
32844 monotonic
32845 nonmonotonic */
32847 static tree
32848 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32850 tree c, t;
32851 int modifiers = 0, nmodifiers = 0;
32853 matching_parens parens;
32854 if (!parens.require_open (parser))
32855 return list;
32857 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32859 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32861 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32862 const char *p = IDENTIFIER_POINTER (id);
32863 if (strcmp ("simd", p) == 0)
32864 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32865 else if (strcmp ("monotonic", p) == 0)
32866 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32867 else if (strcmp ("nonmonotonic", p) == 0)
32868 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32869 else
32870 break;
32871 cp_lexer_consume_token (parser->lexer);
32872 if (nmodifiers++ == 0
32873 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32874 cp_lexer_consume_token (parser->lexer);
32875 else
32877 cp_parser_require (parser, CPP_COLON, RT_COLON);
32878 break;
32882 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32884 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32885 const char *p = IDENTIFIER_POINTER (id);
32887 switch (p[0])
32889 case 'd':
32890 if (strcmp ("dynamic", p) != 0)
32891 goto invalid_kind;
32892 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32893 break;
32895 case 'g':
32896 if (strcmp ("guided", p) != 0)
32897 goto invalid_kind;
32898 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32899 break;
32901 case 'r':
32902 if (strcmp ("runtime", p) != 0)
32903 goto invalid_kind;
32904 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32905 break;
32907 default:
32908 goto invalid_kind;
32911 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32912 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32913 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32914 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32915 else
32916 goto invalid_kind;
32917 cp_lexer_consume_token (parser->lexer);
32919 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32920 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32921 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32922 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32924 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32925 "specified");
32926 modifiers = 0;
32929 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32931 cp_token *token;
32932 cp_lexer_consume_token (parser->lexer);
32934 token = cp_lexer_peek_token (parser->lexer);
32935 t = cp_parser_assignment_expression (parser);
32937 if (t == error_mark_node)
32938 goto resync_fail;
32939 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32940 error_at (token->location, "schedule %<runtime%> does not take "
32941 "a %<chunk_size%> parameter");
32942 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32943 error_at (token->location, "schedule %<auto%> does not take "
32944 "a %<chunk_size%> parameter");
32945 else
32946 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32948 if (!parens.require_close (parser))
32949 goto resync_fail;
32951 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32952 goto resync_fail;
32954 OMP_CLAUSE_SCHEDULE_KIND (c)
32955 = (enum omp_clause_schedule_kind)
32956 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32958 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32959 OMP_CLAUSE_CHAIN (c) = list;
32960 return c;
32962 invalid_kind:
32963 cp_parser_error (parser, "invalid schedule kind");
32964 resync_fail:
32965 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32966 /*or_comma=*/false,
32967 /*consume_paren=*/true);
32968 return list;
32971 /* OpenMP 3.0:
32972 untied */
32974 static tree
32975 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32976 tree list, location_t location)
32978 tree c;
32980 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32982 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32983 OMP_CLAUSE_CHAIN (c) = list;
32984 return c;
32987 /* OpenMP 4.0:
32988 inbranch
32989 notinbranch */
32991 static tree
32992 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32993 tree list, location_t location)
32995 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32996 tree c = build_omp_clause (location, code);
32997 OMP_CLAUSE_CHAIN (c) = list;
32998 return c;
33001 /* OpenMP 4.0:
33002 parallel
33004 sections
33005 taskgroup */
33007 static tree
33008 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33009 enum omp_clause_code code,
33010 tree list, location_t location)
33012 tree c = build_omp_clause (location, code);
33013 OMP_CLAUSE_CHAIN (c) = list;
33014 return c;
33017 /* OpenMP 4.5:
33018 nogroup */
33020 static tree
33021 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33022 tree list, location_t location)
33024 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33025 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33026 OMP_CLAUSE_CHAIN (c) = list;
33027 return c;
33030 /* OpenMP 4.5:
33031 simd
33032 threads */
33034 static tree
33035 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33036 enum omp_clause_code code,
33037 tree list, location_t location)
33039 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33040 tree c = build_omp_clause (location, code);
33041 OMP_CLAUSE_CHAIN (c) = list;
33042 return c;
33045 /* OpenMP 4.0:
33046 num_teams ( expression ) */
33048 static tree
33049 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33050 location_t location)
33052 tree t, c;
33054 matching_parens parens;
33055 if (!parens.require_open (parser))
33056 return list;
33058 t = cp_parser_expression (parser);
33060 if (t == error_mark_node
33061 || !parens.require_close (parser))
33062 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33063 /*or_comma=*/false,
33064 /*consume_paren=*/true);
33066 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33067 "num_teams", location);
33069 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33070 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33071 OMP_CLAUSE_CHAIN (c) = list;
33073 return c;
33076 /* OpenMP 4.0:
33077 thread_limit ( expression ) */
33079 static tree
33080 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33081 location_t location)
33083 tree t, c;
33085 matching_parens parens;
33086 if (!parens.require_open (parser))
33087 return list;
33089 t = cp_parser_expression (parser);
33091 if (t == error_mark_node
33092 || !parens.require_close (parser))
33093 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33094 /*or_comma=*/false,
33095 /*consume_paren=*/true);
33097 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33098 "thread_limit", location);
33100 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33101 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33102 OMP_CLAUSE_CHAIN (c) = list;
33104 return c;
33107 /* OpenMP 4.0:
33108 aligned ( variable-list )
33109 aligned ( variable-list : constant-expression ) */
33111 static tree
33112 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33114 tree nlist, c, alignment = NULL_TREE;
33115 bool colon;
33117 matching_parens parens;
33118 if (!parens.require_open (parser))
33119 return list;
33121 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33122 &colon);
33124 if (colon)
33126 alignment = cp_parser_constant_expression (parser);
33128 if (!parens.require_close (parser))
33129 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33130 /*or_comma=*/false,
33131 /*consume_paren=*/true);
33133 if (alignment == error_mark_node)
33134 alignment = NULL_TREE;
33137 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33138 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33140 return nlist;
33143 /* OpenMP 4.0:
33144 linear ( variable-list )
33145 linear ( variable-list : expression )
33147 OpenMP 4.5:
33148 linear ( modifier ( variable-list ) )
33149 linear ( modifier ( variable-list ) : expression ) */
33151 static tree
33152 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33153 bool declare_simd)
33155 tree nlist, c, step = integer_one_node;
33156 bool colon;
33157 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33159 matching_parens parens;
33160 if (!parens.require_open (parser))
33161 return list;
33163 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33165 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33166 const char *p = IDENTIFIER_POINTER (id);
33168 if (strcmp ("ref", p) == 0)
33169 kind = OMP_CLAUSE_LINEAR_REF;
33170 else if (strcmp ("val", p) == 0)
33171 kind = OMP_CLAUSE_LINEAR_VAL;
33172 else if (strcmp ("uval", p) == 0)
33173 kind = OMP_CLAUSE_LINEAR_UVAL;
33174 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33175 cp_lexer_consume_token (parser->lexer);
33176 else
33177 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33180 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33181 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33182 &colon);
33183 else
33185 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33186 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33187 if (colon)
33188 cp_parser_require (parser, CPP_COLON, RT_COLON);
33189 else if (!parens.require_close (parser))
33190 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33191 /*or_comma=*/false,
33192 /*consume_paren=*/true);
33195 if (colon)
33197 step = NULL_TREE;
33198 if (declare_simd
33199 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33200 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33202 cp_token *token = cp_lexer_peek_token (parser->lexer);
33203 cp_parser_parse_tentatively (parser);
33204 step = cp_parser_id_expression (parser, /*template_p=*/false,
33205 /*check_dependency_p=*/true,
33206 /*template_p=*/NULL,
33207 /*declarator_p=*/false,
33208 /*optional_p=*/false);
33209 if (step != error_mark_node)
33210 step = cp_parser_lookup_name_simple (parser, step, token->location);
33211 if (step == error_mark_node)
33213 step = NULL_TREE;
33214 cp_parser_abort_tentative_parse (parser);
33216 else if (!cp_parser_parse_definitely (parser))
33217 step = NULL_TREE;
33219 if (!step)
33220 step = cp_parser_expression (parser);
33222 if (!parens.require_close (parser))
33223 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33224 /*or_comma=*/false,
33225 /*consume_paren=*/true);
33227 if (step == error_mark_node)
33228 return list;
33231 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33233 OMP_CLAUSE_LINEAR_STEP (c) = step;
33234 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33237 return nlist;
33240 /* OpenMP 4.0:
33241 safelen ( constant-expression ) */
33243 static tree
33244 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33245 location_t location)
33247 tree t, c;
33249 matching_parens parens;
33250 if (!parens.require_open (parser))
33251 return list;
33253 t = cp_parser_constant_expression (parser);
33255 if (t == error_mark_node
33256 || !parens.require_close (parser))
33257 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33258 /*or_comma=*/false,
33259 /*consume_paren=*/true);
33261 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33263 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33264 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33265 OMP_CLAUSE_CHAIN (c) = list;
33267 return c;
33270 /* OpenMP 4.0:
33271 simdlen ( constant-expression ) */
33273 static tree
33274 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33275 location_t location)
33277 tree t, c;
33279 matching_parens parens;
33280 if (!parens.require_open (parser))
33281 return list;
33283 t = cp_parser_constant_expression (parser);
33285 if (t == error_mark_node
33286 || !parens.require_close (parser))
33287 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33288 /*or_comma=*/false,
33289 /*consume_paren=*/true);
33291 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33293 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33294 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33295 OMP_CLAUSE_CHAIN (c) = list;
33297 return c;
33300 /* OpenMP 4.5:
33301 vec:
33302 identifier [+/- integer]
33303 vec , identifier [+/- integer]
33306 static tree
33307 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33308 tree list)
33310 tree vec = NULL;
33312 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33314 cp_parser_error (parser, "expected identifier");
33315 return list;
33318 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33320 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33321 tree t, identifier = cp_parser_identifier (parser);
33322 tree addend = NULL;
33324 if (identifier == error_mark_node)
33325 t = error_mark_node;
33326 else
33328 t = cp_parser_lookup_name_simple
33329 (parser, identifier,
33330 cp_lexer_peek_token (parser->lexer)->location);
33331 if (t == error_mark_node)
33332 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33333 id_loc);
33336 bool neg = false;
33337 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33338 neg = true;
33339 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33341 addend = integer_zero_node;
33342 goto add_to_vector;
33344 cp_lexer_consume_token (parser->lexer);
33346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33348 cp_parser_error (parser, "expected integer");
33349 return list;
33352 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33353 if (TREE_CODE (addend) != INTEGER_CST)
33355 cp_parser_error (parser, "expected integer");
33356 return list;
33358 cp_lexer_consume_token (parser->lexer);
33360 add_to_vector:
33361 if (t != error_mark_node)
33363 vec = tree_cons (addend, t, vec);
33364 if (neg)
33365 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33368 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33369 break;
33371 cp_lexer_consume_token (parser->lexer);
33374 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33376 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33377 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33378 OMP_CLAUSE_DECL (u) = nreverse (vec);
33379 OMP_CLAUSE_CHAIN (u) = list;
33380 return u;
33382 return list;
33385 /* OpenMP 4.0:
33386 depend ( depend-kind : variable-list )
33388 depend-kind:
33389 in | out | inout
33391 OpenMP 4.5:
33392 depend ( source )
33394 depend ( sink : vec ) */
33396 static tree
33397 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33399 tree nlist, c;
33400 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33402 matching_parens parens;
33403 if (!parens.require_open (parser))
33404 return list;
33406 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33408 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33409 const char *p = IDENTIFIER_POINTER (id);
33411 if (strcmp ("in", p) == 0)
33412 kind = OMP_CLAUSE_DEPEND_IN;
33413 else if (strcmp ("inout", p) == 0)
33414 kind = OMP_CLAUSE_DEPEND_INOUT;
33415 else if (strcmp ("out", p) == 0)
33416 kind = OMP_CLAUSE_DEPEND_OUT;
33417 else if (strcmp ("source", p) == 0)
33418 kind = OMP_CLAUSE_DEPEND_SOURCE;
33419 else if (strcmp ("sink", p) == 0)
33420 kind = OMP_CLAUSE_DEPEND_SINK;
33421 else
33422 goto invalid_kind;
33424 else
33425 goto invalid_kind;
33427 cp_lexer_consume_token (parser->lexer);
33429 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33431 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33432 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33433 OMP_CLAUSE_DECL (c) = NULL_TREE;
33434 OMP_CLAUSE_CHAIN (c) = list;
33435 if (!parens.require_close (parser))
33436 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33437 /*or_comma=*/false,
33438 /*consume_paren=*/true);
33439 return c;
33442 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33443 goto resync_fail;
33445 if (kind == OMP_CLAUSE_DEPEND_SINK)
33446 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33447 else
33449 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33450 list, NULL);
33452 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33453 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33455 return nlist;
33457 invalid_kind:
33458 cp_parser_error (parser, "invalid depend kind");
33459 resync_fail:
33460 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33461 /*or_comma=*/false,
33462 /*consume_paren=*/true);
33463 return list;
33466 /* OpenMP 4.0:
33467 map ( map-kind : variable-list )
33468 map ( variable-list )
33470 map-kind:
33471 alloc | to | from | tofrom
33473 OpenMP 4.5:
33474 map-kind:
33475 alloc | to | from | tofrom | release | delete
33477 map ( always [,] map-kind: variable-list ) */
33479 static tree
33480 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33482 tree nlist, c;
33483 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33484 bool always = false;
33486 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33487 return list;
33489 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33491 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33492 const char *p = IDENTIFIER_POINTER (id);
33494 if (strcmp ("always", p) == 0)
33496 int nth = 2;
33497 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33498 nth++;
33499 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33500 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33501 == RID_DELETE))
33502 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33503 == CPP_COLON))
33505 always = true;
33506 cp_lexer_consume_token (parser->lexer);
33507 if (nth == 3)
33508 cp_lexer_consume_token (parser->lexer);
33513 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33514 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33516 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33517 const char *p = IDENTIFIER_POINTER (id);
33519 if (strcmp ("alloc", p) == 0)
33520 kind = GOMP_MAP_ALLOC;
33521 else if (strcmp ("to", p) == 0)
33522 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33523 else if (strcmp ("from", p) == 0)
33524 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33525 else if (strcmp ("tofrom", p) == 0)
33526 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33527 else if (strcmp ("release", p) == 0)
33528 kind = GOMP_MAP_RELEASE;
33529 else
33531 cp_parser_error (parser, "invalid map kind");
33532 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33533 /*or_comma=*/false,
33534 /*consume_paren=*/true);
33535 return list;
33537 cp_lexer_consume_token (parser->lexer);
33538 cp_lexer_consume_token (parser->lexer);
33540 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33541 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33543 kind = GOMP_MAP_DELETE;
33544 cp_lexer_consume_token (parser->lexer);
33545 cp_lexer_consume_token (parser->lexer);
33548 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33549 NULL);
33551 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33552 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33554 return nlist;
33557 /* OpenMP 4.0:
33558 device ( expression ) */
33560 static tree
33561 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33562 location_t location)
33564 tree t, c;
33566 matching_parens parens;
33567 if (!parens.require_open (parser))
33568 return list;
33570 t = cp_parser_expression (parser);
33572 if (t == error_mark_node
33573 || !parens.require_close (parser))
33574 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33575 /*or_comma=*/false,
33576 /*consume_paren=*/true);
33578 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33579 "device", location);
33581 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33582 OMP_CLAUSE_DEVICE_ID (c) = t;
33583 OMP_CLAUSE_CHAIN (c) = list;
33585 return c;
33588 /* OpenMP 4.0:
33589 dist_schedule ( static )
33590 dist_schedule ( static , expression ) */
33592 static tree
33593 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33594 location_t location)
33596 tree c, t;
33598 matching_parens parens;
33599 if (!parens.require_open (parser))
33600 return list;
33602 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33604 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33605 goto invalid_kind;
33606 cp_lexer_consume_token (parser->lexer);
33608 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33610 cp_lexer_consume_token (parser->lexer);
33612 t = cp_parser_assignment_expression (parser);
33614 if (t == error_mark_node)
33615 goto resync_fail;
33616 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33618 if (!parens.require_close (parser))
33619 goto resync_fail;
33621 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33622 goto resync_fail;
33624 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33625 location);
33626 OMP_CLAUSE_CHAIN (c) = list;
33627 return c;
33629 invalid_kind:
33630 cp_parser_error (parser, "invalid dist_schedule kind");
33631 resync_fail:
33632 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33633 /*or_comma=*/false,
33634 /*consume_paren=*/true);
33635 return list;
33638 /* OpenMP 4.0:
33639 proc_bind ( proc-bind-kind )
33641 proc-bind-kind:
33642 master | close | spread */
33644 static tree
33645 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33646 location_t location)
33648 tree c;
33649 enum omp_clause_proc_bind_kind kind;
33651 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33652 return list;
33654 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33656 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33657 const char *p = IDENTIFIER_POINTER (id);
33659 if (strcmp ("master", p) == 0)
33660 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33661 else if (strcmp ("close", p) == 0)
33662 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33663 else if (strcmp ("spread", p) == 0)
33664 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33665 else
33666 goto invalid_kind;
33668 else
33669 goto invalid_kind;
33671 cp_lexer_consume_token (parser->lexer);
33672 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33673 goto resync_fail;
33675 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33676 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33677 location);
33678 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33679 OMP_CLAUSE_CHAIN (c) = list;
33680 return c;
33682 invalid_kind:
33683 cp_parser_error (parser, "invalid depend kind");
33684 resync_fail:
33685 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33686 /*or_comma=*/false,
33687 /*consume_paren=*/true);
33688 return list;
33691 /* OpenACC:
33692 async [( int-expr )] */
33694 static tree
33695 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33697 tree c, t;
33698 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33700 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33702 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33704 matching_parens parens;
33705 parens.consume_open (parser);
33707 t = cp_parser_expression (parser);
33708 if (t == error_mark_node
33709 || !parens.require_close (parser))
33710 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33711 /*or_comma=*/false,
33712 /*consume_paren=*/true);
33715 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33717 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33718 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33719 OMP_CLAUSE_CHAIN (c) = list;
33720 list = c;
33722 return list;
33725 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33726 is a bitmask in MASK. Return the list of clauses found. */
33728 static tree
33729 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33730 const char *where, cp_token *pragma_tok,
33731 bool finish_p = true)
33733 tree clauses = NULL;
33734 bool first = true;
33736 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33738 location_t here;
33739 pragma_omp_clause c_kind;
33740 omp_clause_code code;
33741 const char *c_name;
33742 tree prev = clauses;
33744 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33745 cp_lexer_consume_token (parser->lexer);
33747 here = cp_lexer_peek_token (parser->lexer)->location;
33748 c_kind = cp_parser_omp_clause_name (parser);
33750 switch (c_kind)
33752 case PRAGMA_OACC_CLAUSE_ASYNC:
33753 clauses = cp_parser_oacc_clause_async (parser, clauses);
33754 c_name = "async";
33755 break;
33756 case PRAGMA_OACC_CLAUSE_AUTO:
33757 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33758 clauses, here);
33759 c_name = "auto";
33760 break;
33761 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33762 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33763 c_name = "collapse";
33764 break;
33765 case PRAGMA_OACC_CLAUSE_COPY:
33766 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33767 c_name = "copy";
33768 break;
33769 case PRAGMA_OACC_CLAUSE_COPYIN:
33770 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33771 c_name = "copyin";
33772 break;
33773 case PRAGMA_OACC_CLAUSE_COPYOUT:
33774 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33775 c_name = "copyout";
33776 break;
33777 case PRAGMA_OACC_CLAUSE_CREATE:
33778 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33779 c_name = "create";
33780 break;
33781 case PRAGMA_OACC_CLAUSE_DELETE:
33782 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33783 c_name = "delete";
33784 break;
33785 case PRAGMA_OMP_CLAUSE_DEFAULT:
33786 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33787 c_name = "default";
33788 break;
33789 case PRAGMA_OACC_CLAUSE_DEVICE:
33790 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33791 c_name = "device";
33792 break;
33793 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33794 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33795 c_name = "deviceptr";
33796 break;
33797 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33798 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33799 c_name = "device_resident";
33800 break;
33801 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33802 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33803 clauses);
33804 c_name = "firstprivate";
33805 break;
33806 case PRAGMA_OACC_CLAUSE_GANG:
33807 c_name = "gang";
33808 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33809 c_name, clauses);
33810 break;
33811 case PRAGMA_OACC_CLAUSE_HOST:
33812 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33813 c_name = "host";
33814 break;
33815 case PRAGMA_OACC_CLAUSE_IF:
33816 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33817 c_name = "if";
33818 break;
33819 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33820 clauses = cp_parser_oacc_simple_clause (parser,
33821 OMP_CLAUSE_INDEPENDENT,
33822 clauses, here);
33823 c_name = "independent";
33824 break;
33825 case PRAGMA_OACC_CLAUSE_LINK:
33826 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33827 c_name = "link";
33828 break;
33829 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33830 code = OMP_CLAUSE_NUM_GANGS;
33831 c_name = "num_gangs";
33832 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33833 clauses);
33834 break;
33835 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33836 c_name = "num_workers";
33837 code = OMP_CLAUSE_NUM_WORKERS;
33838 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33839 clauses);
33840 break;
33841 case PRAGMA_OACC_CLAUSE_PRESENT:
33842 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33843 c_name = "present";
33844 break;
33845 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33846 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33847 c_name = "present_or_copy";
33848 break;
33849 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33850 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33851 c_name = "present_or_copyin";
33852 break;
33853 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33854 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33855 c_name = "present_or_copyout";
33856 break;
33857 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33858 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33859 c_name = "present_or_create";
33860 break;
33861 case PRAGMA_OACC_CLAUSE_PRIVATE:
33862 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33863 clauses);
33864 c_name = "private";
33865 break;
33866 case PRAGMA_OACC_CLAUSE_REDUCTION:
33867 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33868 c_name = "reduction";
33869 break;
33870 case PRAGMA_OACC_CLAUSE_SELF:
33871 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33872 c_name = "self";
33873 break;
33874 case PRAGMA_OACC_CLAUSE_SEQ:
33875 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33876 clauses, here);
33877 c_name = "seq";
33878 break;
33879 case PRAGMA_OACC_CLAUSE_TILE:
33880 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33881 c_name = "tile";
33882 break;
33883 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33884 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33885 clauses);
33886 c_name = "use_device";
33887 break;
33888 case PRAGMA_OACC_CLAUSE_VECTOR:
33889 c_name = "vector";
33890 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33891 c_name, clauses);
33892 break;
33893 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33894 c_name = "vector_length";
33895 code = OMP_CLAUSE_VECTOR_LENGTH;
33896 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33897 clauses);
33898 break;
33899 case PRAGMA_OACC_CLAUSE_WAIT:
33900 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33901 c_name = "wait";
33902 break;
33903 case PRAGMA_OACC_CLAUSE_WORKER:
33904 c_name = "worker";
33905 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33906 c_name, clauses);
33907 break;
33908 default:
33909 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33910 goto saw_error;
33913 first = false;
33915 if (((mask >> c_kind) & 1) == 0)
33917 /* Remove the invalid clause(s) from the list to avoid
33918 confusing the rest of the compiler. */
33919 clauses = prev;
33920 error_at (here, "%qs is not valid for %qs", c_name, where);
33924 saw_error:
33925 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33927 if (finish_p)
33928 return finish_omp_clauses (clauses, C_ORT_ACC);
33930 return clauses;
33933 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33934 is a bitmask in MASK. Return the list of clauses found; the result
33935 of clause default goes in *pdefault. */
33937 static tree
33938 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33939 const char *where, cp_token *pragma_tok,
33940 bool finish_p = true)
33942 tree clauses = NULL;
33943 bool first = true;
33944 cp_token *token = NULL;
33946 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33948 pragma_omp_clause c_kind;
33949 const char *c_name;
33950 tree prev = clauses;
33952 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33953 cp_lexer_consume_token (parser->lexer);
33955 token = cp_lexer_peek_token (parser->lexer);
33956 c_kind = cp_parser_omp_clause_name (parser);
33958 switch (c_kind)
33960 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33961 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33962 token->location);
33963 c_name = "collapse";
33964 break;
33965 case PRAGMA_OMP_CLAUSE_COPYIN:
33966 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33967 c_name = "copyin";
33968 break;
33969 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33970 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33971 clauses);
33972 c_name = "copyprivate";
33973 break;
33974 case PRAGMA_OMP_CLAUSE_DEFAULT:
33975 clauses = cp_parser_omp_clause_default (parser, clauses,
33976 token->location, false);
33977 c_name = "default";
33978 break;
33979 case PRAGMA_OMP_CLAUSE_FINAL:
33980 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33981 c_name = "final";
33982 break;
33983 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33984 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33985 clauses);
33986 c_name = "firstprivate";
33987 break;
33988 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33989 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33990 token->location);
33991 c_name = "grainsize";
33992 break;
33993 case PRAGMA_OMP_CLAUSE_HINT:
33994 clauses = cp_parser_omp_clause_hint (parser, clauses,
33995 token->location);
33996 c_name = "hint";
33997 break;
33998 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33999 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34000 token->location);
34001 c_name = "defaultmap";
34002 break;
34003 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34004 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34005 clauses);
34006 c_name = "use_device_ptr";
34007 break;
34008 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34009 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34010 clauses);
34011 c_name = "is_device_ptr";
34012 break;
34013 case PRAGMA_OMP_CLAUSE_IF:
34014 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34015 true);
34016 c_name = "if";
34017 break;
34018 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34019 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34020 clauses);
34021 c_name = "lastprivate";
34022 break;
34023 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34024 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34025 token->location);
34026 c_name = "mergeable";
34027 break;
34028 case PRAGMA_OMP_CLAUSE_NOWAIT:
34029 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34030 c_name = "nowait";
34031 break;
34032 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34033 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34034 token->location);
34035 c_name = "num_tasks";
34036 break;
34037 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34038 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34039 token->location);
34040 c_name = "num_threads";
34041 break;
34042 case PRAGMA_OMP_CLAUSE_ORDERED:
34043 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34044 token->location);
34045 c_name = "ordered";
34046 break;
34047 case PRAGMA_OMP_CLAUSE_PRIORITY:
34048 clauses = cp_parser_omp_clause_priority (parser, clauses,
34049 token->location);
34050 c_name = "priority";
34051 break;
34052 case PRAGMA_OMP_CLAUSE_PRIVATE:
34053 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34054 clauses);
34055 c_name = "private";
34056 break;
34057 case PRAGMA_OMP_CLAUSE_REDUCTION:
34058 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34059 c_name = "reduction";
34060 break;
34061 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34062 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34063 token->location);
34064 c_name = "schedule";
34065 break;
34066 case PRAGMA_OMP_CLAUSE_SHARED:
34067 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34068 clauses);
34069 c_name = "shared";
34070 break;
34071 case PRAGMA_OMP_CLAUSE_UNTIED:
34072 clauses = cp_parser_omp_clause_untied (parser, clauses,
34073 token->location);
34074 c_name = "untied";
34075 break;
34076 case PRAGMA_OMP_CLAUSE_INBRANCH:
34077 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34078 clauses, token->location);
34079 c_name = "inbranch";
34080 break;
34081 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34082 clauses = cp_parser_omp_clause_branch (parser,
34083 OMP_CLAUSE_NOTINBRANCH,
34084 clauses, token->location);
34085 c_name = "notinbranch";
34086 break;
34087 case PRAGMA_OMP_CLAUSE_PARALLEL:
34088 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34089 clauses, token->location);
34090 c_name = "parallel";
34091 if (!first)
34093 clause_not_first:
34094 error_at (token->location, "%qs must be the first clause of %qs",
34095 c_name, where);
34096 clauses = prev;
34098 break;
34099 case PRAGMA_OMP_CLAUSE_FOR:
34100 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34101 clauses, token->location);
34102 c_name = "for";
34103 if (!first)
34104 goto clause_not_first;
34105 break;
34106 case PRAGMA_OMP_CLAUSE_SECTIONS:
34107 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34108 clauses, token->location);
34109 c_name = "sections";
34110 if (!first)
34111 goto clause_not_first;
34112 break;
34113 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34114 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34115 clauses, token->location);
34116 c_name = "taskgroup";
34117 if (!first)
34118 goto clause_not_first;
34119 break;
34120 case PRAGMA_OMP_CLAUSE_LINK:
34121 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34122 c_name = "to";
34123 break;
34124 case PRAGMA_OMP_CLAUSE_TO:
34125 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34126 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34127 clauses);
34128 else
34129 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34130 c_name = "to";
34131 break;
34132 case PRAGMA_OMP_CLAUSE_FROM:
34133 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34134 c_name = "from";
34135 break;
34136 case PRAGMA_OMP_CLAUSE_UNIFORM:
34137 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34138 clauses);
34139 c_name = "uniform";
34140 break;
34141 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34142 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34143 token->location);
34144 c_name = "num_teams";
34145 break;
34146 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34147 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34148 token->location);
34149 c_name = "thread_limit";
34150 break;
34151 case PRAGMA_OMP_CLAUSE_ALIGNED:
34152 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34153 c_name = "aligned";
34154 break;
34155 case PRAGMA_OMP_CLAUSE_LINEAR:
34157 bool declare_simd = false;
34158 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34159 declare_simd = true;
34160 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34162 c_name = "linear";
34163 break;
34164 case PRAGMA_OMP_CLAUSE_DEPEND:
34165 clauses = cp_parser_omp_clause_depend (parser, clauses,
34166 token->location);
34167 c_name = "depend";
34168 break;
34169 case PRAGMA_OMP_CLAUSE_MAP:
34170 clauses = cp_parser_omp_clause_map (parser, clauses);
34171 c_name = "map";
34172 break;
34173 case PRAGMA_OMP_CLAUSE_DEVICE:
34174 clauses = cp_parser_omp_clause_device (parser, clauses,
34175 token->location);
34176 c_name = "device";
34177 break;
34178 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34179 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34180 token->location);
34181 c_name = "dist_schedule";
34182 break;
34183 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34184 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34185 token->location);
34186 c_name = "proc_bind";
34187 break;
34188 case PRAGMA_OMP_CLAUSE_SAFELEN:
34189 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34190 token->location);
34191 c_name = "safelen";
34192 break;
34193 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34194 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34195 token->location);
34196 c_name = "simdlen";
34197 break;
34198 case PRAGMA_OMP_CLAUSE_NOGROUP:
34199 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34200 token->location);
34201 c_name = "nogroup";
34202 break;
34203 case PRAGMA_OMP_CLAUSE_THREADS:
34204 clauses
34205 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34206 clauses, token->location);
34207 c_name = "threads";
34208 break;
34209 case PRAGMA_OMP_CLAUSE_SIMD:
34210 clauses
34211 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34212 clauses, token->location);
34213 c_name = "simd";
34214 break;
34215 default:
34216 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34217 goto saw_error;
34220 first = false;
34222 if (((mask >> c_kind) & 1) == 0)
34224 /* Remove the invalid clause(s) from the list to avoid
34225 confusing the rest of the compiler. */
34226 clauses = prev;
34227 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34230 saw_error:
34231 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34232 if (finish_p)
34234 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34235 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34236 else
34237 return finish_omp_clauses (clauses, C_ORT_OMP);
34239 return clauses;
34242 /* OpenMP 2.5:
34243 structured-block:
34244 statement
34246 In practice, we're also interested in adding the statement to an
34247 outer node. So it is convenient if we work around the fact that
34248 cp_parser_statement calls add_stmt. */
34250 static unsigned
34251 cp_parser_begin_omp_structured_block (cp_parser *parser)
34253 unsigned save = parser->in_statement;
34255 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34256 This preserves the "not within loop or switch" style error messages
34257 for nonsense cases like
34258 void foo() {
34259 #pragma omp single
34260 break;
34263 if (parser->in_statement)
34264 parser->in_statement = IN_OMP_BLOCK;
34266 return save;
34269 static void
34270 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34272 parser->in_statement = save;
34275 static tree
34276 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34278 tree stmt = begin_omp_structured_block ();
34279 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34281 cp_parser_statement (parser, NULL_TREE, false, if_p);
34283 cp_parser_end_omp_structured_block (parser, save);
34284 return finish_omp_structured_block (stmt);
34287 /* OpenMP 2.5:
34288 # pragma omp atomic new-line
34289 expression-stmt
34291 expression-stmt:
34292 x binop= expr | x++ | ++x | x-- | --x
34293 binop:
34294 +, *, -, /, &, ^, |, <<, >>
34296 where x is an lvalue expression with scalar type.
34298 OpenMP 3.1:
34299 # pragma omp atomic new-line
34300 update-stmt
34302 # pragma omp atomic read new-line
34303 read-stmt
34305 # pragma omp atomic write new-line
34306 write-stmt
34308 # pragma omp atomic update new-line
34309 update-stmt
34311 # pragma omp atomic capture new-line
34312 capture-stmt
34314 # pragma omp atomic capture new-line
34315 capture-block
34317 read-stmt:
34318 v = x
34319 write-stmt:
34320 x = expr
34321 update-stmt:
34322 expression-stmt | x = x binop expr
34323 capture-stmt:
34324 v = expression-stmt
34325 capture-block:
34326 { v = x; update-stmt; } | { update-stmt; v = x; }
34328 OpenMP 4.0:
34329 update-stmt:
34330 expression-stmt | x = x binop expr | x = expr binop x
34331 capture-stmt:
34332 v = update-stmt
34333 capture-block:
34334 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34336 where x and v are lvalue expressions with scalar type. */
34338 static void
34339 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34341 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34342 tree rhs1 = NULL_TREE, orig_lhs;
34343 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34344 bool structured_block = false;
34345 bool seq_cst = false;
34347 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34349 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34350 const char *p = IDENTIFIER_POINTER (id);
34352 if (!strcmp (p, "seq_cst"))
34354 seq_cst = true;
34355 cp_lexer_consume_token (parser->lexer);
34356 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34357 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34358 cp_lexer_consume_token (parser->lexer);
34361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34363 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34364 const char *p = IDENTIFIER_POINTER (id);
34366 if (!strcmp (p, "read"))
34367 code = OMP_ATOMIC_READ;
34368 else if (!strcmp (p, "write"))
34369 code = NOP_EXPR;
34370 else if (!strcmp (p, "update"))
34371 code = OMP_ATOMIC;
34372 else if (!strcmp (p, "capture"))
34373 code = OMP_ATOMIC_CAPTURE_NEW;
34374 else
34375 p = NULL;
34376 if (p)
34377 cp_lexer_consume_token (parser->lexer);
34379 if (!seq_cst)
34381 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34382 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34383 cp_lexer_consume_token (parser->lexer);
34385 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34387 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34388 const char *p = IDENTIFIER_POINTER (id);
34390 if (!strcmp (p, "seq_cst"))
34392 seq_cst = true;
34393 cp_lexer_consume_token (parser->lexer);
34397 cp_parser_require_pragma_eol (parser, pragma_tok);
34399 switch (code)
34401 case OMP_ATOMIC_READ:
34402 case NOP_EXPR: /* atomic write */
34403 v = cp_parser_unary_expression (parser);
34404 if (v == error_mark_node)
34405 goto saw_error;
34406 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34407 goto saw_error;
34408 if (code == NOP_EXPR)
34409 lhs = cp_parser_expression (parser);
34410 else
34411 lhs = cp_parser_unary_expression (parser);
34412 if (lhs == error_mark_node)
34413 goto saw_error;
34414 if (code == NOP_EXPR)
34416 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34417 opcode. */
34418 code = OMP_ATOMIC;
34419 rhs = lhs;
34420 lhs = v;
34421 v = NULL_TREE;
34423 goto done;
34424 case OMP_ATOMIC_CAPTURE_NEW:
34425 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34427 cp_lexer_consume_token (parser->lexer);
34428 structured_block = true;
34430 else
34432 v = cp_parser_unary_expression (parser);
34433 if (v == error_mark_node)
34434 goto saw_error;
34435 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34436 goto saw_error;
34438 default:
34439 break;
34442 restart:
34443 lhs = cp_parser_unary_expression (parser);
34444 orig_lhs = lhs;
34445 switch (TREE_CODE (lhs))
34447 case ERROR_MARK:
34448 goto saw_error;
34450 case POSTINCREMENT_EXPR:
34451 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34452 code = OMP_ATOMIC_CAPTURE_OLD;
34453 /* FALLTHROUGH */
34454 case PREINCREMENT_EXPR:
34455 lhs = TREE_OPERAND (lhs, 0);
34456 opcode = PLUS_EXPR;
34457 rhs = integer_one_node;
34458 break;
34460 case POSTDECREMENT_EXPR:
34461 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34462 code = OMP_ATOMIC_CAPTURE_OLD;
34463 /* FALLTHROUGH */
34464 case PREDECREMENT_EXPR:
34465 lhs = TREE_OPERAND (lhs, 0);
34466 opcode = MINUS_EXPR;
34467 rhs = integer_one_node;
34468 break;
34470 case COMPOUND_EXPR:
34471 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34472 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34473 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34474 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34475 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34476 (TREE_OPERAND (lhs, 1), 0), 0)))
34477 == BOOLEAN_TYPE)
34478 /* Undo effects of boolean_increment for post {in,de}crement. */
34479 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34480 /* FALLTHRU */
34481 case MODIFY_EXPR:
34482 if (TREE_CODE (lhs) == MODIFY_EXPR
34483 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34485 /* Undo effects of boolean_increment. */
34486 if (integer_onep (TREE_OPERAND (lhs, 1)))
34488 /* This is pre or post increment. */
34489 rhs = TREE_OPERAND (lhs, 1);
34490 lhs = TREE_OPERAND (lhs, 0);
34491 opcode = NOP_EXPR;
34492 if (code == OMP_ATOMIC_CAPTURE_NEW
34493 && !structured_block
34494 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34495 code = OMP_ATOMIC_CAPTURE_OLD;
34496 break;
34499 /* FALLTHRU */
34500 default:
34501 switch (cp_lexer_peek_token (parser->lexer)->type)
34503 case CPP_MULT_EQ:
34504 opcode = MULT_EXPR;
34505 break;
34506 case CPP_DIV_EQ:
34507 opcode = TRUNC_DIV_EXPR;
34508 break;
34509 case CPP_PLUS_EQ:
34510 opcode = PLUS_EXPR;
34511 break;
34512 case CPP_MINUS_EQ:
34513 opcode = MINUS_EXPR;
34514 break;
34515 case CPP_LSHIFT_EQ:
34516 opcode = LSHIFT_EXPR;
34517 break;
34518 case CPP_RSHIFT_EQ:
34519 opcode = RSHIFT_EXPR;
34520 break;
34521 case CPP_AND_EQ:
34522 opcode = BIT_AND_EXPR;
34523 break;
34524 case CPP_OR_EQ:
34525 opcode = BIT_IOR_EXPR;
34526 break;
34527 case CPP_XOR_EQ:
34528 opcode = BIT_XOR_EXPR;
34529 break;
34530 case CPP_EQ:
34531 enum cp_parser_prec oprec;
34532 cp_token *token;
34533 cp_lexer_consume_token (parser->lexer);
34534 cp_parser_parse_tentatively (parser);
34535 rhs1 = cp_parser_simple_cast_expression (parser);
34536 if (rhs1 == error_mark_node)
34538 cp_parser_abort_tentative_parse (parser);
34539 cp_parser_simple_cast_expression (parser);
34540 goto saw_error;
34542 token = cp_lexer_peek_token (parser->lexer);
34543 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34545 cp_parser_abort_tentative_parse (parser);
34546 cp_parser_parse_tentatively (parser);
34547 rhs = cp_parser_binary_expression (parser, false, true,
34548 PREC_NOT_OPERATOR, NULL);
34549 if (rhs == error_mark_node)
34551 cp_parser_abort_tentative_parse (parser);
34552 cp_parser_binary_expression (parser, false, true,
34553 PREC_NOT_OPERATOR, NULL);
34554 goto saw_error;
34556 switch (TREE_CODE (rhs))
34558 case MULT_EXPR:
34559 case TRUNC_DIV_EXPR:
34560 case RDIV_EXPR:
34561 case PLUS_EXPR:
34562 case MINUS_EXPR:
34563 case LSHIFT_EXPR:
34564 case RSHIFT_EXPR:
34565 case BIT_AND_EXPR:
34566 case BIT_IOR_EXPR:
34567 case BIT_XOR_EXPR:
34568 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34570 if (cp_parser_parse_definitely (parser))
34572 opcode = TREE_CODE (rhs);
34573 rhs1 = TREE_OPERAND (rhs, 0);
34574 rhs = TREE_OPERAND (rhs, 1);
34575 goto stmt_done;
34577 else
34578 goto saw_error;
34580 break;
34581 default:
34582 break;
34584 cp_parser_abort_tentative_parse (parser);
34585 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34587 rhs = cp_parser_expression (parser);
34588 if (rhs == error_mark_node)
34589 goto saw_error;
34590 opcode = NOP_EXPR;
34591 rhs1 = NULL_TREE;
34592 goto stmt_done;
34594 cp_parser_error (parser,
34595 "invalid form of %<#pragma omp atomic%>");
34596 goto saw_error;
34598 if (!cp_parser_parse_definitely (parser))
34599 goto saw_error;
34600 switch (token->type)
34602 case CPP_SEMICOLON:
34603 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34605 code = OMP_ATOMIC_CAPTURE_OLD;
34606 v = lhs;
34607 lhs = NULL_TREE;
34608 lhs1 = rhs1;
34609 rhs1 = NULL_TREE;
34610 cp_lexer_consume_token (parser->lexer);
34611 goto restart;
34613 else if (structured_block)
34615 opcode = NOP_EXPR;
34616 rhs = rhs1;
34617 rhs1 = NULL_TREE;
34618 goto stmt_done;
34620 cp_parser_error (parser,
34621 "invalid form of %<#pragma omp atomic%>");
34622 goto saw_error;
34623 case CPP_MULT:
34624 opcode = MULT_EXPR;
34625 break;
34626 case CPP_DIV:
34627 opcode = TRUNC_DIV_EXPR;
34628 break;
34629 case CPP_PLUS:
34630 opcode = PLUS_EXPR;
34631 break;
34632 case CPP_MINUS:
34633 opcode = MINUS_EXPR;
34634 break;
34635 case CPP_LSHIFT:
34636 opcode = LSHIFT_EXPR;
34637 break;
34638 case CPP_RSHIFT:
34639 opcode = RSHIFT_EXPR;
34640 break;
34641 case CPP_AND:
34642 opcode = BIT_AND_EXPR;
34643 break;
34644 case CPP_OR:
34645 opcode = BIT_IOR_EXPR;
34646 break;
34647 case CPP_XOR:
34648 opcode = BIT_XOR_EXPR;
34649 break;
34650 default:
34651 cp_parser_error (parser,
34652 "invalid operator for %<#pragma omp atomic%>");
34653 goto saw_error;
34655 oprec = TOKEN_PRECEDENCE (token);
34656 gcc_assert (oprec != PREC_NOT_OPERATOR);
34657 if (commutative_tree_code (opcode))
34658 oprec = (enum cp_parser_prec) (oprec - 1);
34659 cp_lexer_consume_token (parser->lexer);
34660 rhs = cp_parser_binary_expression (parser, false, false,
34661 oprec, NULL);
34662 if (rhs == error_mark_node)
34663 goto saw_error;
34664 goto stmt_done;
34665 /* FALLTHROUGH */
34666 default:
34667 cp_parser_error (parser,
34668 "invalid operator for %<#pragma omp atomic%>");
34669 goto saw_error;
34671 cp_lexer_consume_token (parser->lexer);
34673 rhs = cp_parser_expression (parser);
34674 if (rhs == error_mark_node)
34675 goto saw_error;
34676 break;
34678 stmt_done:
34679 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34681 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34682 goto saw_error;
34683 v = cp_parser_unary_expression (parser);
34684 if (v == error_mark_node)
34685 goto saw_error;
34686 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34687 goto saw_error;
34688 lhs1 = cp_parser_unary_expression (parser);
34689 if (lhs1 == error_mark_node)
34690 goto saw_error;
34692 if (structured_block)
34694 cp_parser_consume_semicolon_at_end_of_statement (parser);
34695 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34697 done:
34698 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34699 if (!structured_block)
34700 cp_parser_consume_semicolon_at_end_of_statement (parser);
34701 return;
34703 saw_error:
34704 cp_parser_skip_to_end_of_block_or_statement (parser);
34705 if (structured_block)
34707 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34708 cp_lexer_consume_token (parser->lexer);
34709 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34711 cp_parser_skip_to_end_of_block_or_statement (parser);
34712 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34713 cp_lexer_consume_token (parser->lexer);
34719 /* OpenMP 2.5:
34720 # pragma omp barrier new-line */
34722 static void
34723 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34725 cp_parser_require_pragma_eol (parser, pragma_tok);
34726 finish_omp_barrier ();
34729 /* OpenMP 2.5:
34730 # pragma omp critical [(name)] new-line
34731 structured-block
34733 OpenMP 4.5:
34734 # pragma omp critical [(name) [hint(expression)]] new-line
34735 structured-block */
34737 #define OMP_CRITICAL_CLAUSE_MASK \
34738 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34740 static tree
34741 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34743 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34745 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34747 matching_parens parens;
34748 parens.consume_open (parser);
34750 name = cp_parser_identifier (parser);
34752 if (name == error_mark_node
34753 || !parens.require_close (parser))
34754 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34755 /*or_comma=*/false,
34756 /*consume_paren=*/true);
34757 if (name == error_mark_node)
34758 name = NULL;
34760 clauses = cp_parser_omp_all_clauses (parser,
34761 OMP_CRITICAL_CLAUSE_MASK,
34762 "#pragma omp critical", pragma_tok);
34764 else
34765 cp_parser_require_pragma_eol (parser, pragma_tok);
34767 stmt = cp_parser_omp_structured_block (parser, if_p);
34768 return c_finish_omp_critical (input_location, stmt, name, clauses);
34771 /* OpenMP 2.5:
34772 # pragma omp flush flush-vars[opt] new-line
34774 flush-vars:
34775 ( variable-list ) */
34777 static void
34778 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34781 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34782 cp_parser_require_pragma_eol (parser, pragma_tok);
34784 finish_omp_flush ();
34787 /* Helper function, to parse omp for increment expression. */
34789 static tree
34790 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34792 tree cond = cp_parser_binary_expression (parser, false, true,
34793 PREC_NOT_OPERATOR, NULL);
34794 if (cond == error_mark_node
34795 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34797 cp_parser_skip_to_end_of_statement (parser);
34798 return error_mark_node;
34801 switch (TREE_CODE (cond))
34803 case GT_EXPR:
34804 case GE_EXPR:
34805 case LT_EXPR:
34806 case LE_EXPR:
34807 break;
34808 case NE_EXPR:
34809 /* Fall through: OpenMP disallows NE_EXPR. */
34810 gcc_fallthrough ();
34811 default:
34812 return error_mark_node;
34815 /* If decl is an iterator, preserve LHS and RHS of the relational
34816 expr until finish_omp_for. */
34817 if (decl
34818 && (type_dependent_expression_p (decl)
34819 || CLASS_TYPE_P (TREE_TYPE (decl))))
34820 return cond;
34822 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34823 TREE_CODE (cond),
34824 TREE_OPERAND (cond, 0), ERROR_MARK,
34825 TREE_OPERAND (cond, 1), ERROR_MARK,
34826 /*overload=*/NULL, tf_warning_or_error);
34829 /* Helper function, to parse omp for increment expression. */
34831 static tree
34832 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34834 cp_token *token = cp_lexer_peek_token (parser->lexer);
34835 enum tree_code op;
34836 tree lhs, rhs;
34837 cp_id_kind idk;
34838 bool decl_first;
34840 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34842 op = (token->type == CPP_PLUS_PLUS
34843 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34844 cp_lexer_consume_token (parser->lexer);
34845 lhs = cp_parser_simple_cast_expression (parser);
34846 if (lhs != decl
34847 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34848 return error_mark_node;
34849 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34852 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34853 if (lhs != decl
34854 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34855 return error_mark_node;
34857 token = cp_lexer_peek_token (parser->lexer);
34858 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34860 op = (token->type == CPP_PLUS_PLUS
34861 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34862 cp_lexer_consume_token (parser->lexer);
34863 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34866 op = cp_parser_assignment_operator_opt (parser);
34867 if (op == ERROR_MARK)
34868 return error_mark_node;
34870 if (op != NOP_EXPR)
34872 rhs = cp_parser_assignment_expression (parser);
34873 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34874 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34877 lhs = cp_parser_binary_expression (parser, false, false,
34878 PREC_ADDITIVE_EXPRESSION, NULL);
34879 token = cp_lexer_peek_token (parser->lexer);
34880 decl_first = (lhs == decl
34881 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34882 if (decl_first)
34883 lhs = NULL_TREE;
34884 if (token->type != CPP_PLUS
34885 && token->type != CPP_MINUS)
34886 return error_mark_node;
34890 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34891 cp_lexer_consume_token (parser->lexer);
34892 rhs = cp_parser_binary_expression (parser, false, false,
34893 PREC_ADDITIVE_EXPRESSION, NULL);
34894 token = cp_lexer_peek_token (parser->lexer);
34895 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34897 if (lhs == NULL_TREE)
34899 if (op == PLUS_EXPR)
34900 lhs = rhs;
34901 else
34902 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34903 tf_warning_or_error);
34905 else
34906 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34907 ERROR_MARK, NULL, tf_warning_or_error);
34910 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34912 if (!decl_first)
34914 if ((rhs != decl
34915 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34916 || op == MINUS_EXPR)
34917 return error_mark_node;
34918 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34920 else
34921 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34923 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34926 /* Parse the initialization statement of an OpenMP for loop.
34928 Return true if the resulting construct should have an
34929 OMP_CLAUSE_PRIVATE added to it. */
34931 static tree
34932 cp_parser_omp_for_loop_init (cp_parser *parser,
34933 tree &this_pre_body,
34934 vec<tree, va_gc> *for_block,
34935 tree &init,
34936 tree &orig_init,
34937 tree &decl,
34938 tree &real_decl)
34940 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34941 return NULL_TREE;
34943 tree add_private_clause = NULL_TREE;
34945 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34947 init-expr:
34948 var = lb
34949 integer-type var = lb
34950 random-access-iterator-type var = lb
34951 pointer-type var = lb
34953 cp_decl_specifier_seq type_specifiers;
34955 /* First, try to parse as an initialized declaration. See
34956 cp_parser_condition, from whence the bulk of this is copied. */
34958 cp_parser_parse_tentatively (parser);
34959 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34960 /*is_trailing_return=*/false,
34961 &type_specifiers);
34962 if (cp_parser_parse_definitely (parser))
34964 /* If parsing a type specifier seq succeeded, then this
34965 MUST be a initialized declaration. */
34966 tree asm_specification, attributes;
34967 cp_declarator *declarator;
34969 declarator = cp_parser_declarator (parser,
34970 CP_PARSER_DECLARATOR_NAMED,
34971 /*ctor_dtor_or_conv_p=*/NULL,
34972 /*parenthesized_p=*/NULL,
34973 /*member_p=*/false,
34974 /*friend_p=*/false);
34975 attributes = cp_parser_attributes_opt (parser);
34976 asm_specification = cp_parser_asm_specification_opt (parser);
34978 if (declarator == cp_error_declarator)
34979 cp_parser_skip_to_end_of_statement (parser);
34981 else
34983 tree pushed_scope, auto_node;
34985 decl = start_decl (declarator, &type_specifiers,
34986 SD_INITIALIZED, attributes,
34987 /*prefix_attributes=*/NULL_TREE,
34988 &pushed_scope);
34990 auto_node = type_uses_auto (TREE_TYPE (decl));
34991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34993 if (cp_lexer_next_token_is (parser->lexer,
34994 CPP_OPEN_PAREN))
34995 error ("parenthesized initialization is not allowed in "
34996 "OpenMP %<for%> loop");
34997 else
34998 /* Trigger an error. */
34999 cp_parser_require (parser, CPP_EQ, RT_EQ);
35001 init = error_mark_node;
35002 cp_parser_skip_to_end_of_statement (parser);
35004 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35005 || type_dependent_expression_p (decl)
35006 || auto_node)
35008 bool is_direct_init, is_non_constant_init;
35010 init = cp_parser_initializer (parser,
35011 &is_direct_init,
35012 &is_non_constant_init);
35014 if (auto_node)
35016 TREE_TYPE (decl)
35017 = do_auto_deduction (TREE_TYPE (decl), init,
35018 auto_node);
35020 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35021 && !type_dependent_expression_p (decl))
35022 goto non_class;
35025 cp_finish_decl (decl, init, !is_non_constant_init,
35026 asm_specification,
35027 LOOKUP_ONLYCONVERTING);
35028 orig_init = init;
35029 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35031 vec_safe_push (for_block, this_pre_body);
35032 init = NULL_TREE;
35034 else
35036 init = pop_stmt_list (this_pre_body);
35037 if (init && TREE_CODE (init) == STATEMENT_LIST)
35039 tree_stmt_iterator i = tsi_start (init);
35040 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35041 while (!tsi_end_p (i))
35043 tree t = tsi_stmt (i);
35044 if (TREE_CODE (t) == DECL_EXPR
35045 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35047 tsi_delink (&i);
35048 vec_safe_push (for_block, t);
35049 continue;
35051 break;
35053 if (tsi_one_before_end_p (i))
35055 tree t = tsi_stmt (i);
35056 tsi_delink (&i);
35057 free_stmt_list (init);
35058 init = t;
35062 this_pre_body = NULL_TREE;
35064 else
35066 /* Consume '='. */
35067 cp_lexer_consume_token (parser->lexer);
35068 init = cp_parser_assignment_expression (parser);
35070 non_class:
35071 if (TYPE_REF_P (TREE_TYPE (decl)))
35072 init = error_mark_node;
35073 else
35074 cp_finish_decl (decl, NULL_TREE,
35075 /*init_const_expr_p=*/false,
35076 asm_specification,
35077 LOOKUP_ONLYCONVERTING);
35080 if (pushed_scope)
35081 pop_scope (pushed_scope);
35084 else
35086 cp_id_kind idk;
35087 /* If parsing a type specifier sequence failed, then
35088 this MUST be a simple expression. */
35089 cp_parser_parse_tentatively (parser);
35090 decl = cp_parser_primary_expression (parser, false, false,
35091 false, &idk);
35092 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35093 if (!cp_parser_error_occurred (parser)
35094 && decl
35095 && (TREE_CODE (decl) == COMPONENT_REF
35096 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35098 cp_parser_abort_tentative_parse (parser);
35099 cp_parser_parse_tentatively (parser);
35100 cp_token *token = cp_lexer_peek_token (parser->lexer);
35101 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35102 /*check_dependency_p=*/true,
35103 /*template_p=*/NULL,
35104 /*declarator_p=*/false,
35105 /*optional_p=*/false);
35106 if (name != error_mark_node
35107 && last_tok == cp_lexer_peek_token (parser->lexer))
35109 decl = cp_parser_lookup_name_simple (parser, name,
35110 token->location);
35111 if (TREE_CODE (decl) == FIELD_DECL)
35112 add_private_clause = omp_privatize_field (decl, false);
35114 cp_parser_abort_tentative_parse (parser);
35115 cp_parser_parse_tentatively (parser);
35116 decl = cp_parser_primary_expression (parser, false, false,
35117 false, &idk);
35119 if (!cp_parser_error_occurred (parser)
35120 && decl
35121 && DECL_P (decl)
35122 && CLASS_TYPE_P (TREE_TYPE (decl)))
35124 tree rhs;
35126 cp_parser_parse_definitely (parser);
35127 cp_parser_require (parser, CPP_EQ, RT_EQ);
35128 rhs = cp_parser_assignment_expression (parser);
35129 orig_init = rhs;
35130 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35131 decl, NOP_EXPR,
35132 rhs,
35133 tf_warning_or_error));
35134 if (!add_private_clause)
35135 add_private_clause = decl;
35137 else
35139 decl = NULL;
35140 cp_parser_abort_tentative_parse (parser);
35141 init = cp_parser_expression (parser);
35142 if (init)
35144 if (TREE_CODE (init) == MODIFY_EXPR
35145 || TREE_CODE (init) == MODOP_EXPR)
35146 real_decl = TREE_OPERAND (init, 0);
35150 return add_private_clause;
35153 /* Parse the restricted form of the for statement allowed by OpenMP. */
35155 static tree
35156 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35157 tree *cclauses, bool *if_p)
35159 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35160 tree real_decl, initv, condv, incrv, declv;
35161 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35162 location_t loc_first;
35163 bool collapse_err = false;
35164 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35165 vec<tree, va_gc> *for_block = make_tree_vector ();
35166 auto_vec<tree, 4> orig_inits;
35167 bool tiling = false;
35169 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35170 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35171 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35172 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35174 tiling = true;
35175 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35177 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35178 && OMP_CLAUSE_ORDERED_EXPR (cl))
35180 ordered_cl = cl;
35181 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35184 if (ordered && ordered < collapse)
35186 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35187 "%<ordered%> clause parameter is less than %<collapse%>");
35188 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35189 = build_int_cst (NULL_TREE, collapse);
35190 ordered = collapse;
35192 if (ordered)
35194 for (tree *pc = &clauses; *pc; )
35195 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35197 error_at (OMP_CLAUSE_LOCATION (*pc),
35198 "%<linear%> clause may not be specified together "
35199 "with %<ordered%> clause with a parameter");
35200 *pc = OMP_CLAUSE_CHAIN (*pc);
35202 else
35203 pc = &OMP_CLAUSE_CHAIN (*pc);
35206 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35207 count = ordered ? ordered : collapse;
35209 declv = make_tree_vec (count);
35210 initv = make_tree_vec (count);
35211 condv = make_tree_vec (count);
35212 incrv = make_tree_vec (count);
35214 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35216 for (i = 0; i < count; i++)
35218 int bracecount = 0;
35219 tree add_private_clause = NULL_TREE;
35220 location_t loc;
35222 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35224 if (!collapse_err)
35225 cp_parser_error (parser, "for statement expected");
35226 return NULL;
35228 loc = cp_lexer_consume_token (parser->lexer)->location;
35230 matching_parens parens;
35231 if (!parens.require_open (parser))
35232 return NULL;
35234 init = orig_init = decl = real_decl = NULL;
35235 this_pre_body = push_stmt_list ();
35237 add_private_clause
35238 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35239 init, orig_init, decl, real_decl);
35241 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35242 if (this_pre_body)
35244 this_pre_body = pop_stmt_list (this_pre_body);
35245 if (pre_body)
35247 tree t = pre_body;
35248 pre_body = push_stmt_list ();
35249 add_stmt (t);
35250 add_stmt (this_pre_body);
35251 pre_body = pop_stmt_list (pre_body);
35253 else
35254 pre_body = this_pre_body;
35257 if (decl)
35258 real_decl = decl;
35259 if (cclauses != NULL
35260 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35261 && real_decl != NULL_TREE)
35263 tree *c;
35264 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35265 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35266 && OMP_CLAUSE_DECL (*c) == real_decl)
35268 error_at (loc, "iteration variable %qD"
35269 " should not be firstprivate", real_decl);
35270 *c = OMP_CLAUSE_CHAIN (*c);
35272 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35273 && OMP_CLAUSE_DECL (*c) == real_decl)
35275 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35276 tree l = *c;
35277 *c = OMP_CLAUSE_CHAIN (*c);
35278 if (code == OMP_SIMD)
35280 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35281 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35283 else
35285 OMP_CLAUSE_CHAIN (l) = clauses;
35286 clauses = l;
35288 add_private_clause = NULL_TREE;
35290 else
35292 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35293 && OMP_CLAUSE_DECL (*c) == real_decl)
35294 add_private_clause = NULL_TREE;
35295 c = &OMP_CLAUSE_CHAIN (*c);
35299 if (add_private_clause)
35301 tree c;
35302 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35304 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35305 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35306 && OMP_CLAUSE_DECL (c) == decl)
35307 break;
35308 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35309 && OMP_CLAUSE_DECL (c) == decl)
35310 error_at (loc, "iteration variable %qD "
35311 "should not be firstprivate",
35312 decl);
35313 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35314 && OMP_CLAUSE_DECL (c) == decl)
35315 error_at (loc, "iteration variable %qD should not be reduction",
35316 decl);
35318 if (c == NULL)
35320 if (code != OMP_SIMD)
35321 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35322 else if (collapse == 1)
35323 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35324 else
35325 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35326 OMP_CLAUSE_DECL (c) = add_private_clause;
35327 c = finish_omp_clauses (c, C_ORT_OMP);
35328 if (c)
35330 OMP_CLAUSE_CHAIN (c) = clauses;
35331 clauses = c;
35332 /* For linear, signal that we need to fill up
35333 the so far unknown linear step. */
35334 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35335 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35340 cond = NULL;
35341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35342 cond = cp_parser_omp_for_cond (parser, decl);
35343 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35345 incr = NULL;
35346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35348 /* If decl is an iterator, preserve the operator on decl
35349 until finish_omp_for. */
35350 if (real_decl
35351 && ((processing_template_decl
35352 && (TREE_TYPE (real_decl) == NULL_TREE
35353 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
35354 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35355 incr = cp_parser_omp_for_incr (parser, real_decl);
35356 else
35357 incr = cp_parser_expression (parser);
35358 if (!EXPR_HAS_LOCATION (incr))
35359 protected_set_expr_location (incr, input_location);
35362 if (!parens.require_close (parser))
35363 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35364 /*or_comma=*/false,
35365 /*consume_paren=*/true);
35367 TREE_VEC_ELT (declv, i) = decl;
35368 TREE_VEC_ELT (initv, i) = init;
35369 TREE_VEC_ELT (condv, i) = cond;
35370 TREE_VEC_ELT (incrv, i) = incr;
35371 if (orig_init)
35373 orig_inits.safe_grow_cleared (i + 1);
35374 orig_inits[i] = orig_init;
35377 if (i == count - 1)
35378 break;
35380 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35381 in between the collapsed for loops to be still considered perfectly
35382 nested. Hopefully the final version clarifies this.
35383 For now handle (multiple) {'s and empty statements. */
35384 cp_parser_parse_tentatively (parser);
35385 for (;;)
35387 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35388 break;
35389 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35391 cp_lexer_consume_token (parser->lexer);
35392 bracecount++;
35394 else if (bracecount
35395 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35396 cp_lexer_consume_token (parser->lexer);
35397 else
35399 loc = cp_lexer_peek_token (parser->lexer)->location;
35400 error_at (loc, "not enough for loops to collapse");
35401 collapse_err = true;
35402 cp_parser_abort_tentative_parse (parser);
35403 declv = NULL_TREE;
35404 break;
35408 if (declv)
35410 cp_parser_parse_definitely (parser);
35411 nbraces += bracecount;
35415 if (nbraces)
35416 if_p = NULL;
35418 /* Note that we saved the original contents of this flag when we entered
35419 the structured block, and so we don't need to re-save it here. */
35420 parser->in_statement = IN_OMP_FOR;
35422 /* Note that the grammar doesn't call for a structured block here,
35423 though the loop as a whole is a structured block. */
35424 body = push_stmt_list ();
35425 cp_parser_statement (parser, NULL_TREE, false, if_p);
35426 body = pop_stmt_list (body);
35428 if (declv == NULL_TREE)
35429 ret = NULL_TREE;
35430 else
35431 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35432 body, pre_body, &orig_inits, clauses);
35434 while (nbraces)
35436 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35438 cp_lexer_consume_token (parser->lexer);
35439 nbraces--;
35441 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35442 cp_lexer_consume_token (parser->lexer);
35443 else
35445 if (!collapse_err)
35447 error_at (cp_lexer_peek_token (parser->lexer)->location,
35448 "collapsed loops not perfectly nested");
35450 collapse_err = true;
35451 cp_parser_statement_seq_opt (parser, NULL);
35452 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35453 break;
35457 while (!for_block->is_empty ())
35459 tree t = for_block->pop ();
35460 if (TREE_CODE (t) == STATEMENT_LIST)
35461 add_stmt (pop_stmt_list (t));
35462 else
35463 add_stmt (t);
35465 release_tree_vector (for_block);
35467 return ret;
35470 /* Helper function for OpenMP parsing, split clauses and call
35471 finish_omp_clauses on each of the set of clauses afterwards. */
35473 static void
35474 cp_omp_split_clauses (location_t loc, enum tree_code code,
35475 omp_clause_mask mask, tree clauses, tree *cclauses)
35477 int i;
35478 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35479 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35480 if (cclauses[i])
35481 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35484 /* OpenMP 4.0:
35485 #pragma omp simd simd-clause[optseq] new-line
35486 for-loop */
35488 #define OMP_SIMD_CLAUSE_MASK \
35489 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35498 static tree
35499 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35500 char *p_name, omp_clause_mask mask, tree *cclauses,
35501 bool *if_p)
35503 tree clauses, sb, ret;
35504 unsigned int save;
35505 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35507 strcat (p_name, " simd");
35508 mask |= OMP_SIMD_CLAUSE_MASK;
35510 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35511 cclauses == NULL);
35512 if (cclauses)
35514 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35515 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35516 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35517 OMP_CLAUSE_ORDERED);
35518 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35520 error_at (OMP_CLAUSE_LOCATION (c),
35521 "%<ordered%> clause with parameter may not be specified "
35522 "on %qs construct", p_name);
35523 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35527 sb = begin_omp_structured_block ();
35528 save = cp_parser_begin_omp_structured_block (parser);
35530 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35532 cp_parser_end_omp_structured_block (parser, save);
35533 add_stmt (finish_omp_structured_block (sb));
35535 return ret;
35538 /* OpenMP 2.5:
35539 #pragma omp for for-clause[optseq] new-line
35540 for-loop
35542 OpenMP 4.0:
35543 #pragma omp for simd for-simd-clause[optseq] new-line
35544 for-loop */
35546 #define OMP_FOR_CLAUSE_MASK \
35547 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35557 static tree
35558 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35559 char *p_name, omp_clause_mask mask, tree *cclauses,
35560 bool *if_p)
35562 tree clauses, sb, ret;
35563 unsigned int save;
35564 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35566 strcat (p_name, " for");
35567 mask |= OMP_FOR_CLAUSE_MASK;
35568 /* parallel for{, simd} disallows nowait clause, but for
35569 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35570 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35571 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35572 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35573 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35574 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35576 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35578 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35579 const char *p = IDENTIFIER_POINTER (id);
35581 if (strcmp (p, "simd") == 0)
35583 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35584 if (cclauses == NULL)
35585 cclauses = cclauses_buf;
35587 cp_lexer_consume_token (parser->lexer);
35588 if (!flag_openmp) /* flag_openmp_simd */
35589 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35590 cclauses, if_p);
35591 sb = begin_omp_structured_block ();
35592 save = cp_parser_begin_omp_structured_block (parser);
35593 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35594 cclauses, if_p);
35595 cp_parser_end_omp_structured_block (parser, save);
35596 tree body = finish_omp_structured_block (sb);
35597 if (ret == NULL)
35598 return ret;
35599 ret = make_node (OMP_FOR);
35600 TREE_TYPE (ret) = void_type_node;
35601 OMP_FOR_BODY (ret) = body;
35602 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35603 SET_EXPR_LOCATION (ret, loc);
35604 add_stmt (ret);
35605 return ret;
35608 if (!flag_openmp) /* flag_openmp_simd */
35610 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35611 return NULL_TREE;
35614 /* Composite distribute parallel for disallows linear clause. */
35615 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35616 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35618 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35619 cclauses == NULL);
35620 if (cclauses)
35622 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35623 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35626 sb = begin_omp_structured_block ();
35627 save = cp_parser_begin_omp_structured_block (parser);
35629 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35631 cp_parser_end_omp_structured_block (parser, save);
35632 add_stmt (finish_omp_structured_block (sb));
35634 return ret;
35637 /* OpenMP 2.5:
35638 # pragma omp master new-line
35639 structured-block */
35641 static tree
35642 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35644 cp_parser_require_pragma_eol (parser, pragma_tok);
35645 return c_finish_omp_master (input_location,
35646 cp_parser_omp_structured_block (parser, if_p));
35649 /* OpenMP 2.5:
35650 # pragma omp ordered new-line
35651 structured-block
35653 OpenMP 4.5:
35654 # pragma omp ordered ordered-clauses new-line
35655 structured-block */
35657 #define OMP_ORDERED_CLAUSE_MASK \
35658 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35661 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35662 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35664 static bool
35665 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35666 enum pragma_context context, bool *if_p)
35668 location_t loc = pragma_tok->location;
35670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35672 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35673 const char *p = IDENTIFIER_POINTER (id);
35675 if (strcmp (p, "depend") == 0)
35677 if (!flag_openmp) /* flag_openmp_simd */
35679 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35680 return false;
35682 if (context == pragma_stmt)
35684 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35685 "%<depend%> clause may only be used in compound "
35686 "statements");
35687 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35688 return false;
35690 tree clauses
35691 = cp_parser_omp_all_clauses (parser,
35692 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35693 "#pragma omp ordered", pragma_tok);
35694 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35695 return false;
35699 tree clauses
35700 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35701 "#pragma omp ordered", pragma_tok);
35703 if (!flag_openmp /* flag_openmp_simd */
35704 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35705 return false;
35707 c_finish_omp_ordered (loc, clauses,
35708 cp_parser_omp_structured_block (parser, if_p));
35709 return true;
35712 /* OpenMP 2.5:
35714 section-scope:
35715 { section-sequence }
35717 section-sequence:
35718 section-directive[opt] structured-block
35719 section-sequence section-directive structured-block */
35721 static tree
35722 cp_parser_omp_sections_scope (cp_parser *parser)
35724 tree stmt, substmt;
35725 bool error_suppress = false;
35726 cp_token *tok;
35728 matching_braces braces;
35729 if (!braces.require_open (parser))
35730 return NULL_TREE;
35732 stmt = push_stmt_list ();
35734 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35735 != PRAGMA_OMP_SECTION)
35737 substmt = cp_parser_omp_structured_block (parser, NULL);
35738 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35739 add_stmt (substmt);
35742 while (1)
35744 tok = cp_lexer_peek_token (parser->lexer);
35745 if (tok->type == CPP_CLOSE_BRACE)
35746 break;
35747 if (tok->type == CPP_EOF)
35748 break;
35750 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35752 cp_lexer_consume_token (parser->lexer);
35753 cp_parser_require_pragma_eol (parser, tok);
35754 error_suppress = false;
35756 else if (!error_suppress)
35758 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35759 error_suppress = true;
35762 substmt = cp_parser_omp_structured_block (parser, NULL);
35763 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35764 add_stmt (substmt);
35766 braces.require_close (parser);
35768 substmt = pop_stmt_list (stmt);
35770 stmt = make_node (OMP_SECTIONS);
35771 TREE_TYPE (stmt) = void_type_node;
35772 OMP_SECTIONS_BODY (stmt) = substmt;
35774 add_stmt (stmt);
35775 return stmt;
35778 /* OpenMP 2.5:
35779 # pragma omp sections sections-clause[optseq] newline
35780 sections-scope */
35782 #define OMP_SECTIONS_CLAUSE_MASK \
35783 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35789 static tree
35790 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35791 char *p_name, omp_clause_mask mask, tree *cclauses)
35793 tree clauses, ret;
35794 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35796 strcat (p_name, " sections");
35797 mask |= OMP_SECTIONS_CLAUSE_MASK;
35798 if (cclauses)
35799 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35801 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35802 cclauses == NULL);
35803 if (cclauses)
35805 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35806 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35809 ret = cp_parser_omp_sections_scope (parser);
35810 if (ret)
35811 OMP_SECTIONS_CLAUSES (ret) = clauses;
35813 return ret;
35816 /* OpenMP 2.5:
35817 # pragma omp parallel parallel-clause[optseq] new-line
35818 structured-block
35819 # pragma omp parallel for parallel-for-clause[optseq] new-line
35820 structured-block
35821 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35822 structured-block
35824 OpenMP 4.0:
35825 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35826 structured-block */
35828 #define OMP_PARALLEL_CLAUSE_MASK \
35829 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35839 static tree
35840 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35841 char *p_name, omp_clause_mask mask, tree *cclauses,
35842 bool *if_p)
35844 tree stmt, clauses, block;
35845 unsigned int save;
35846 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35848 strcat (p_name, " parallel");
35849 mask |= OMP_PARALLEL_CLAUSE_MASK;
35850 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35851 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35852 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35853 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35855 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35857 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35858 if (cclauses == NULL)
35859 cclauses = cclauses_buf;
35861 cp_lexer_consume_token (parser->lexer);
35862 if (!flag_openmp) /* flag_openmp_simd */
35863 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35864 if_p);
35865 block = begin_omp_parallel ();
35866 save = cp_parser_begin_omp_structured_block (parser);
35867 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35868 if_p);
35869 cp_parser_end_omp_structured_block (parser, save);
35870 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35871 block);
35872 if (ret == NULL_TREE)
35873 return ret;
35874 OMP_PARALLEL_COMBINED (stmt) = 1;
35875 return stmt;
35877 /* When combined with distribute, parallel has to be followed by for.
35878 #pragma omp target parallel is allowed though. */
35879 else if (cclauses
35880 && (mask & (OMP_CLAUSE_MASK_1
35881 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35883 error_at (loc, "expected %<for%> after %qs", p_name);
35884 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35885 return NULL_TREE;
35887 else if (!flag_openmp) /* flag_openmp_simd */
35889 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35890 return NULL_TREE;
35892 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35894 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35895 const char *p = IDENTIFIER_POINTER (id);
35896 if (strcmp (p, "sections") == 0)
35898 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35899 cclauses = cclauses_buf;
35901 cp_lexer_consume_token (parser->lexer);
35902 block = begin_omp_parallel ();
35903 save = cp_parser_begin_omp_structured_block (parser);
35904 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35905 cp_parser_end_omp_structured_block (parser, save);
35906 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35907 block);
35908 OMP_PARALLEL_COMBINED (stmt) = 1;
35909 return stmt;
35913 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35914 cclauses == NULL);
35915 if (cclauses)
35917 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35918 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35921 block = begin_omp_parallel ();
35922 save = cp_parser_begin_omp_structured_block (parser);
35923 cp_parser_statement (parser, NULL_TREE, false, if_p);
35924 cp_parser_end_omp_structured_block (parser, save);
35925 stmt = finish_omp_parallel (clauses, block);
35926 return stmt;
35929 /* OpenMP 2.5:
35930 # pragma omp single single-clause[optseq] new-line
35931 structured-block */
35933 #define OMP_SINGLE_CLAUSE_MASK \
35934 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35939 static tree
35940 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35942 tree stmt = make_node (OMP_SINGLE);
35943 TREE_TYPE (stmt) = void_type_node;
35945 OMP_SINGLE_CLAUSES (stmt)
35946 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35947 "#pragma omp single", pragma_tok);
35948 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35950 return add_stmt (stmt);
35953 /* OpenMP 3.0:
35954 # pragma omp task task-clause[optseq] new-line
35955 structured-block */
35957 #define OMP_TASK_CLAUSE_MASK \
35958 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35969 static tree
35970 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35972 tree clauses, block;
35973 unsigned int save;
35975 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35976 "#pragma omp task", pragma_tok);
35977 block = begin_omp_task ();
35978 save = cp_parser_begin_omp_structured_block (parser);
35979 cp_parser_statement (parser, NULL_TREE, false, if_p);
35980 cp_parser_end_omp_structured_block (parser, save);
35981 return finish_omp_task (clauses, block);
35984 /* OpenMP 3.0:
35985 # pragma omp taskwait new-line */
35987 static void
35988 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35990 cp_parser_require_pragma_eol (parser, pragma_tok);
35991 finish_omp_taskwait ();
35994 /* OpenMP 3.1:
35995 # pragma omp taskyield new-line */
35997 static void
35998 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
36000 cp_parser_require_pragma_eol (parser, pragma_tok);
36001 finish_omp_taskyield ();
36004 /* OpenMP 4.0:
36005 # pragma omp taskgroup new-line
36006 structured-block */
36008 static tree
36009 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36011 cp_parser_require_pragma_eol (parser, pragma_tok);
36012 return c_finish_omp_taskgroup (input_location,
36013 cp_parser_omp_structured_block (parser,
36014 if_p));
36018 /* OpenMP 2.5:
36019 # pragma omp threadprivate (variable-list) */
36021 static void
36022 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36024 tree vars;
36026 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36027 cp_parser_require_pragma_eol (parser, pragma_tok);
36029 finish_omp_threadprivate (vars);
36032 /* OpenMP 4.0:
36033 # pragma omp cancel cancel-clause[optseq] new-line */
36035 #define OMP_CANCEL_CLAUSE_MASK \
36036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36042 static void
36043 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36045 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36046 "#pragma omp cancel", pragma_tok);
36047 finish_omp_cancel (clauses);
36050 /* OpenMP 4.0:
36051 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36053 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36054 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36059 static void
36060 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36061 enum pragma_context context)
36063 tree clauses;
36064 bool point_seen = false;
36066 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36068 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36069 const char *p = IDENTIFIER_POINTER (id);
36071 if (strcmp (p, "point") == 0)
36073 cp_lexer_consume_token (parser->lexer);
36074 point_seen = true;
36077 if (!point_seen)
36079 cp_parser_error (parser, "expected %<point%>");
36080 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36081 return;
36084 if (context != pragma_compound)
36086 if (context == pragma_stmt)
36087 error_at (pragma_tok->location,
36088 "%<#pragma %s%> may only be used in compound statements",
36089 "omp cancellation point");
36090 else
36091 cp_parser_error (parser, "expected declaration specifiers");
36092 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36093 return;
36096 clauses = cp_parser_omp_all_clauses (parser,
36097 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36098 "#pragma omp cancellation point",
36099 pragma_tok);
36100 finish_omp_cancellation_point (clauses);
36103 /* OpenMP 4.0:
36104 #pragma omp distribute distribute-clause[optseq] new-line
36105 for-loop */
36107 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36108 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36114 static tree
36115 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36116 char *p_name, omp_clause_mask mask, tree *cclauses,
36117 bool *if_p)
36119 tree clauses, sb, ret;
36120 unsigned int save;
36121 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36123 strcat (p_name, " distribute");
36124 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36126 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36128 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36129 const char *p = IDENTIFIER_POINTER (id);
36130 bool simd = false;
36131 bool parallel = false;
36133 if (strcmp (p, "simd") == 0)
36134 simd = true;
36135 else
36136 parallel = strcmp (p, "parallel") == 0;
36137 if (parallel || simd)
36139 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36140 if (cclauses == NULL)
36141 cclauses = cclauses_buf;
36142 cp_lexer_consume_token (parser->lexer);
36143 if (!flag_openmp) /* flag_openmp_simd */
36145 if (simd)
36146 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36147 cclauses, if_p);
36148 else
36149 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36150 cclauses, if_p);
36152 sb = begin_omp_structured_block ();
36153 save = cp_parser_begin_omp_structured_block (parser);
36154 if (simd)
36155 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36156 cclauses, if_p);
36157 else
36158 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36159 cclauses, if_p);
36160 cp_parser_end_omp_structured_block (parser, save);
36161 tree body = finish_omp_structured_block (sb);
36162 if (ret == NULL)
36163 return ret;
36164 ret = make_node (OMP_DISTRIBUTE);
36165 TREE_TYPE (ret) = void_type_node;
36166 OMP_FOR_BODY (ret) = body;
36167 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36168 SET_EXPR_LOCATION (ret, loc);
36169 add_stmt (ret);
36170 return ret;
36173 if (!flag_openmp) /* flag_openmp_simd */
36175 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36176 return NULL_TREE;
36179 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36180 cclauses == NULL);
36181 if (cclauses)
36183 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36184 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36187 sb = begin_omp_structured_block ();
36188 save = cp_parser_begin_omp_structured_block (parser);
36190 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36192 cp_parser_end_omp_structured_block (parser, save);
36193 add_stmt (finish_omp_structured_block (sb));
36195 return ret;
36198 /* OpenMP 4.0:
36199 # pragma omp teams teams-clause[optseq] new-line
36200 structured-block */
36202 #define OMP_TEAMS_CLAUSE_MASK \
36203 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36211 static tree
36212 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36213 char *p_name, omp_clause_mask mask, tree *cclauses,
36214 bool *if_p)
36216 tree clauses, sb, ret;
36217 unsigned int save;
36218 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36220 strcat (p_name, " teams");
36221 mask |= OMP_TEAMS_CLAUSE_MASK;
36223 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36225 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36226 const char *p = IDENTIFIER_POINTER (id);
36227 if (strcmp (p, "distribute") == 0)
36229 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36230 if (cclauses == NULL)
36231 cclauses = cclauses_buf;
36233 cp_lexer_consume_token (parser->lexer);
36234 if (!flag_openmp) /* flag_openmp_simd */
36235 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36236 cclauses, if_p);
36237 sb = begin_omp_structured_block ();
36238 save = cp_parser_begin_omp_structured_block (parser);
36239 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36240 cclauses, if_p);
36241 cp_parser_end_omp_structured_block (parser, save);
36242 tree body = finish_omp_structured_block (sb);
36243 if (ret == NULL)
36244 return ret;
36245 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36246 ret = make_node (OMP_TEAMS);
36247 TREE_TYPE (ret) = void_type_node;
36248 OMP_TEAMS_CLAUSES (ret) = clauses;
36249 OMP_TEAMS_BODY (ret) = body;
36250 OMP_TEAMS_COMBINED (ret) = 1;
36251 SET_EXPR_LOCATION (ret, loc);
36252 return add_stmt (ret);
36255 if (!flag_openmp) /* flag_openmp_simd */
36257 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36258 return NULL_TREE;
36261 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36262 cclauses == NULL);
36263 if (cclauses)
36265 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36266 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36269 tree stmt = make_node (OMP_TEAMS);
36270 TREE_TYPE (stmt) = void_type_node;
36271 OMP_TEAMS_CLAUSES (stmt) = clauses;
36272 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36273 SET_EXPR_LOCATION (stmt, loc);
36275 return add_stmt (stmt);
36278 /* OpenMP 4.0:
36279 # pragma omp target data target-data-clause[optseq] new-line
36280 structured-block */
36282 #define OMP_TARGET_DATA_CLAUSE_MASK \
36283 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36288 static tree
36289 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36291 tree clauses
36292 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36293 "#pragma omp target data", pragma_tok);
36294 int map_seen = 0;
36295 for (tree *pc = &clauses; *pc;)
36297 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36298 switch (OMP_CLAUSE_MAP_KIND (*pc))
36300 case GOMP_MAP_TO:
36301 case GOMP_MAP_ALWAYS_TO:
36302 case GOMP_MAP_FROM:
36303 case GOMP_MAP_ALWAYS_FROM:
36304 case GOMP_MAP_TOFROM:
36305 case GOMP_MAP_ALWAYS_TOFROM:
36306 case GOMP_MAP_ALLOC:
36307 map_seen = 3;
36308 break;
36309 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36310 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36311 case GOMP_MAP_ALWAYS_POINTER:
36312 break;
36313 default:
36314 map_seen |= 1;
36315 error_at (OMP_CLAUSE_LOCATION (*pc),
36316 "%<#pragma omp target data%> with map-type other "
36317 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36318 "on %<map%> clause");
36319 *pc = OMP_CLAUSE_CHAIN (*pc);
36320 continue;
36322 pc = &OMP_CLAUSE_CHAIN (*pc);
36325 if (map_seen != 3)
36327 if (map_seen == 0)
36328 error_at (pragma_tok->location,
36329 "%<#pragma omp target data%> must contain at least "
36330 "one %<map%> clause");
36331 return NULL_TREE;
36334 tree stmt = make_node (OMP_TARGET_DATA);
36335 TREE_TYPE (stmt) = void_type_node;
36336 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36338 keep_next_level (true);
36339 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36341 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36342 return add_stmt (stmt);
36345 /* OpenMP 4.5:
36346 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36347 structured-block */
36349 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36350 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36356 static tree
36357 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36358 enum pragma_context context)
36360 bool data_seen = false;
36361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36363 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36364 const char *p = IDENTIFIER_POINTER (id);
36366 if (strcmp (p, "data") == 0)
36368 cp_lexer_consume_token (parser->lexer);
36369 data_seen = true;
36372 if (!data_seen)
36374 cp_parser_error (parser, "expected %<data%>");
36375 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36376 return NULL_TREE;
36379 if (context == pragma_stmt)
36381 error_at (pragma_tok->location,
36382 "%<#pragma %s%> may only be used in compound statements",
36383 "omp target enter data");
36384 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36385 return NULL_TREE;
36388 tree clauses
36389 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36390 "#pragma omp target enter data", pragma_tok);
36391 int map_seen = 0;
36392 for (tree *pc = &clauses; *pc;)
36394 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36395 switch (OMP_CLAUSE_MAP_KIND (*pc))
36397 case GOMP_MAP_TO:
36398 case GOMP_MAP_ALWAYS_TO:
36399 case GOMP_MAP_ALLOC:
36400 map_seen = 3;
36401 break;
36402 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36403 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36404 case GOMP_MAP_ALWAYS_POINTER:
36405 break;
36406 default:
36407 map_seen |= 1;
36408 error_at (OMP_CLAUSE_LOCATION (*pc),
36409 "%<#pragma omp target enter data%> with map-type other "
36410 "than %<to%> or %<alloc%> on %<map%> clause");
36411 *pc = OMP_CLAUSE_CHAIN (*pc);
36412 continue;
36414 pc = &OMP_CLAUSE_CHAIN (*pc);
36417 if (map_seen != 3)
36419 if (map_seen == 0)
36420 error_at (pragma_tok->location,
36421 "%<#pragma omp target enter data%> must contain at least "
36422 "one %<map%> clause");
36423 return NULL_TREE;
36426 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36427 TREE_TYPE (stmt) = void_type_node;
36428 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36429 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36430 return add_stmt (stmt);
36433 /* OpenMP 4.5:
36434 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36435 structured-block */
36437 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36438 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36444 static tree
36445 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36446 enum pragma_context context)
36448 bool data_seen = false;
36449 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36451 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36452 const char *p = IDENTIFIER_POINTER (id);
36454 if (strcmp (p, "data") == 0)
36456 cp_lexer_consume_token (parser->lexer);
36457 data_seen = true;
36460 if (!data_seen)
36462 cp_parser_error (parser, "expected %<data%>");
36463 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36464 return NULL_TREE;
36467 if (context == pragma_stmt)
36469 error_at (pragma_tok->location,
36470 "%<#pragma %s%> may only be used in compound statements",
36471 "omp target exit data");
36472 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36473 return NULL_TREE;
36476 tree clauses
36477 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36478 "#pragma omp target exit data", pragma_tok);
36479 int map_seen = 0;
36480 for (tree *pc = &clauses; *pc;)
36482 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36483 switch (OMP_CLAUSE_MAP_KIND (*pc))
36485 case GOMP_MAP_FROM:
36486 case GOMP_MAP_ALWAYS_FROM:
36487 case GOMP_MAP_RELEASE:
36488 case GOMP_MAP_DELETE:
36489 map_seen = 3;
36490 break;
36491 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36492 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36493 case GOMP_MAP_ALWAYS_POINTER:
36494 break;
36495 default:
36496 map_seen |= 1;
36497 error_at (OMP_CLAUSE_LOCATION (*pc),
36498 "%<#pragma omp target exit data%> with map-type other "
36499 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36500 " clause");
36501 *pc = OMP_CLAUSE_CHAIN (*pc);
36502 continue;
36504 pc = &OMP_CLAUSE_CHAIN (*pc);
36507 if (map_seen != 3)
36509 if (map_seen == 0)
36510 error_at (pragma_tok->location,
36511 "%<#pragma omp target exit data%> must contain at least "
36512 "one %<map%> clause");
36513 return NULL_TREE;
36516 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36517 TREE_TYPE (stmt) = void_type_node;
36518 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36519 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36520 return add_stmt (stmt);
36523 /* OpenMP 4.0:
36524 # pragma omp target update target-update-clause[optseq] new-line */
36526 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36527 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36534 static bool
36535 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36536 enum pragma_context context)
36538 if (context == pragma_stmt)
36540 error_at (pragma_tok->location,
36541 "%<#pragma %s%> may only be used in compound statements",
36542 "omp target update");
36543 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36544 return false;
36547 tree clauses
36548 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36549 "#pragma omp target update", pragma_tok);
36550 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36551 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36553 error_at (pragma_tok->location,
36554 "%<#pragma omp target update%> must contain at least one "
36555 "%<from%> or %<to%> clauses");
36556 return false;
36559 tree stmt = make_node (OMP_TARGET_UPDATE);
36560 TREE_TYPE (stmt) = void_type_node;
36561 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36562 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36563 add_stmt (stmt);
36564 return false;
36567 /* OpenMP 4.0:
36568 # pragma omp target target-clause[optseq] new-line
36569 structured-block */
36571 #define OMP_TARGET_CLAUSE_MASK \
36572 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36582 static bool
36583 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36584 enum pragma_context context, bool *if_p)
36586 tree *pc = NULL, stmt;
36588 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36590 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36591 const char *p = IDENTIFIER_POINTER (id);
36592 enum tree_code ccode = ERROR_MARK;
36594 if (strcmp (p, "teams") == 0)
36595 ccode = OMP_TEAMS;
36596 else if (strcmp (p, "parallel") == 0)
36597 ccode = OMP_PARALLEL;
36598 else if (strcmp (p, "simd") == 0)
36599 ccode = OMP_SIMD;
36600 if (ccode != ERROR_MARK)
36602 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36603 char p_name[sizeof ("#pragma omp target teams distribute "
36604 "parallel for simd")];
36606 cp_lexer_consume_token (parser->lexer);
36607 strcpy (p_name, "#pragma omp target");
36608 if (!flag_openmp) /* flag_openmp_simd */
36610 tree stmt;
36611 switch (ccode)
36613 case OMP_TEAMS:
36614 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36615 OMP_TARGET_CLAUSE_MASK,
36616 cclauses, if_p);
36617 break;
36618 case OMP_PARALLEL:
36619 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36620 OMP_TARGET_CLAUSE_MASK,
36621 cclauses, if_p);
36622 break;
36623 case OMP_SIMD:
36624 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36625 OMP_TARGET_CLAUSE_MASK,
36626 cclauses, if_p);
36627 break;
36628 default:
36629 gcc_unreachable ();
36631 return stmt != NULL_TREE;
36633 keep_next_level (true);
36634 tree sb = begin_omp_structured_block (), ret;
36635 unsigned save = cp_parser_begin_omp_structured_block (parser);
36636 switch (ccode)
36638 case OMP_TEAMS:
36639 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36640 OMP_TARGET_CLAUSE_MASK, cclauses,
36641 if_p);
36642 break;
36643 case OMP_PARALLEL:
36644 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36645 OMP_TARGET_CLAUSE_MASK, cclauses,
36646 if_p);
36647 break;
36648 case OMP_SIMD:
36649 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36650 OMP_TARGET_CLAUSE_MASK, cclauses,
36651 if_p);
36652 break;
36653 default:
36654 gcc_unreachable ();
36656 cp_parser_end_omp_structured_block (parser, save);
36657 tree body = finish_omp_structured_block (sb);
36658 if (ret == NULL_TREE)
36659 return false;
36660 if (ccode == OMP_TEAMS && !processing_template_decl)
36662 /* For combined target teams, ensure the num_teams and
36663 thread_limit clause expressions are evaluated on the host,
36664 before entering the target construct. */
36665 tree c;
36666 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36667 c; c = OMP_CLAUSE_CHAIN (c))
36668 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36669 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36670 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36672 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36673 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36674 if (expr == error_mark_node)
36675 continue;
36676 tree tmp = TARGET_EXPR_SLOT (expr);
36677 add_stmt (expr);
36678 OMP_CLAUSE_OPERAND (c, 0) = expr;
36679 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36680 OMP_CLAUSE_FIRSTPRIVATE);
36681 OMP_CLAUSE_DECL (tc) = tmp;
36682 OMP_CLAUSE_CHAIN (tc)
36683 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36684 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36687 tree stmt = make_node (OMP_TARGET);
36688 TREE_TYPE (stmt) = void_type_node;
36689 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36690 OMP_TARGET_BODY (stmt) = body;
36691 OMP_TARGET_COMBINED (stmt) = 1;
36692 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36693 add_stmt (stmt);
36694 pc = &OMP_TARGET_CLAUSES (stmt);
36695 goto check_clauses;
36697 else if (!flag_openmp) /* flag_openmp_simd */
36699 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36700 return false;
36702 else if (strcmp (p, "data") == 0)
36704 cp_lexer_consume_token (parser->lexer);
36705 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36706 return true;
36708 else if (strcmp (p, "enter") == 0)
36710 cp_lexer_consume_token (parser->lexer);
36711 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36712 return false;
36714 else if (strcmp (p, "exit") == 0)
36716 cp_lexer_consume_token (parser->lexer);
36717 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36718 return false;
36720 else if (strcmp (p, "update") == 0)
36722 cp_lexer_consume_token (parser->lexer);
36723 return cp_parser_omp_target_update (parser, pragma_tok, context);
36726 if (!flag_openmp) /* flag_openmp_simd */
36728 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36729 return false;
36732 stmt = make_node (OMP_TARGET);
36733 TREE_TYPE (stmt) = void_type_node;
36735 OMP_TARGET_CLAUSES (stmt)
36736 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36737 "#pragma omp target", pragma_tok);
36738 pc = &OMP_TARGET_CLAUSES (stmt);
36739 keep_next_level (true);
36740 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36742 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36743 add_stmt (stmt);
36745 check_clauses:
36746 while (*pc)
36748 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36749 switch (OMP_CLAUSE_MAP_KIND (*pc))
36751 case GOMP_MAP_TO:
36752 case GOMP_MAP_ALWAYS_TO:
36753 case GOMP_MAP_FROM:
36754 case GOMP_MAP_ALWAYS_FROM:
36755 case GOMP_MAP_TOFROM:
36756 case GOMP_MAP_ALWAYS_TOFROM:
36757 case GOMP_MAP_ALLOC:
36758 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36759 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36760 case GOMP_MAP_ALWAYS_POINTER:
36761 break;
36762 default:
36763 error_at (OMP_CLAUSE_LOCATION (*pc),
36764 "%<#pragma omp target%> with map-type other "
36765 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36766 "on %<map%> clause");
36767 *pc = OMP_CLAUSE_CHAIN (*pc);
36768 continue;
36770 pc = &OMP_CLAUSE_CHAIN (*pc);
36772 return true;
36775 /* OpenACC 2.0:
36776 # pragma acc cache (variable-list) new-line
36779 static tree
36780 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36782 tree stmt, clauses;
36784 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36785 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36787 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36789 stmt = make_node (OACC_CACHE);
36790 TREE_TYPE (stmt) = void_type_node;
36791 OACC_CACHE_CLAUSES (stmt) = clauses;
36792 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36793 add_stmt (stmt);
36795 return stmt;
36798 /* OpenACC 2.0:
36799 # pragma acc data oacc-data-clause[optseq] new-line
36800 structured-block */
36802 #define OACC_DATA_CLAUSE_MASK \
36803 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
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) \
36869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36874 static tree
36875 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36877 tree clauses, stmt;
36878 bool error = false;
36880 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36881 "#pragma acc declare", pragma_tok, true);
36884 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36886 error_at (pragma_tok->location,
36887 "no valid clauses specified in %<#pragma acc declare%>");
36888 return NULL_TREE;
36891 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36893 location_t loc = OMP_CLAUSE_LOCATION (t);
36894 tree decl = OMP_CLAUSE_DECL (t);
36895 if (!DECL_P (decl))
36897 error_at (loc, "array section in %<#pragma acc declare%>");
36898 error = true;
36899 continue;
36901 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36902 switch (OMP_CLAUSE_MAP_KIND (t))
36904 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36905 case GOMP_MAP_FORCE_ALLOC:
36906 case GOMP_MAP_FORCE_TO:
36907 case GOMP_MAP_FORCE_DEVICEPTR:
36908 case GOMP_MAP_DEVICE_RESIDENT:
36909 break;
36911 case GOMP_MAP_LINK:
36912 if (!global_bindings_p ()
36913 && (TREE_STATIC (decl)
36914 || !DECL_EXTERNAL (decl)))
36916 error_at (loc,
36917 "%qD must be a global variable in "
36918 "%<#pragma acc declare link%>",
36919 decl);
36920 error = true;
36921 continue;
36923 break;
36925 default:
36926 if (global_bindings_p ())
36928 error_at (loc, "invalid OpenACC clause at file scope");
36929 error = true;
36930 continue;
36932 if (DECL_EXTERNAL (decl))
36934 error_at (loc,
36935 "invalid use of %<extern%> variable %qD "
36936 "in %<#pragma acc declare%>", decl);
36937 error = true;
36938 continue;
36940 else if (TREE_PUBLIC (decl))
36942 error_at (loc,
36943 "invalid use of %<global%> variable %qD "
36944 "in %<#pragma acc declare%>", decl);
36945 error = true;
36946 continue;
36948 break;
36951 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36952 || lookup_attribute ("omp declare target link",
36953 DECL_ATTRIBUTES (decl)))
36955 error_at (loc, "variable %qD used more than once with "
36956 "%<#pragma acc declare%>", decl);
36957 error = true;
36958 continue;
36961 if (!error)
36963 tree id;
36965 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36966 id = get_identifier ("omp declare target link");
36967 else
36968 id = get_identifier ("omp declare target");
36970 DECL_ATTRIBUTES (decl)
36971 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36972 if (global_bindings_p ())
36974 symtab_node *node = symtab_node::get (decl);
36975 if (node != NULL)
36977 node->offloadable = 1;
36978 if (ENABLE_OFFLOADING)
36980 g->have_offload = true;
36981 if (is_a <varpool_node *> (node))
36982 vec_safe_push (offload_vars, decl);
36989 if (error || global_bindings_p ())
36990 return NULL_TREE;
36992 stmt = make_node (OACC_DECLARE);
36993 TREE_TYPE (stmt) = void_type_node;
36994 OACC_DECLARE_CLAUSES (stmt) = clauses;
36995 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36997 add_stmt (stmt);
36999 return NULL_TREE;
37002 /* OpenACC 2.0:
37003 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37007 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37009 LOC is the location of the #pragma token.
37012 #define OACC_ENTER_DATA_CLAUSE_MASK \
37013 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37021 #define OACC_EXIT_DATA_CLAUSE_MASK \
37022 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37028 static tree
37029 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37030 bool enter)
37032 location_t loc = pragma_tok->location;
37033 tree stmt, clauses;
37034 const char *p = "";
37036 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37037 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37039 if (strcmp (p, "data") != 0)
37041 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37042 enter ? "enter" : "exit");
37043 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37044 return NULL_TREE;
37047 cp_lexer_consume_token (parser->lexer);
37049 if (enter)
37050 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37051 "#pragma acc enter data", pragma_tok);
37052 else
37053 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37054 "#pragma acc exit data", pragma_tok);
37056 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37058 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37059 enter ? "enter" : "exit");
37060 return NULL_TREE;
37063 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37064 TREE_TYPE (stmt) = void_type_node;
37065 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37066 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37067 add_stmt (stmt);
37068 return stmt;
37071 /* OpenACC 2.0:
37072 # pragma acc loop oacc-loop-clause[optseq] new-line
37073 structured-block */
37075 #define OACC_LOOP_CLAUSE_MASK \
37076 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37087 static tree
37088 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37089 omp_clause_mask mask, tree *cclauses, bool *if_p)
37091 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37093 strcat (p_name, " loop");
37094 mask |= OACC_LOOP_CLAUSE_MASK;
37096 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37097 cclauses == NULL);
37098 if (cclauses)
37100 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37101 if (*cclauses)
37102 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37103 if (clauses)
37104 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37107 tree block = begin_omp_structured_block ();
37108 int save = cp_parser_begin_omp_structured_block (parser);
37109 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37110 cp_parser_end_omp_structured_block (parser, save);
37111 add_stmt (finish_omp_structured_block (block));
37113 return stmt;
37116 /* OpenACC 2.0:
37117 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37118 structured-block
37122 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37123 structured-block
37126 #define OACC_KERNELS_CLAUSE_MASK \
37127 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37145 #define OACC_PARALLEL_CLAUSE_MASK \
37146 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37167 static tree
37168 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37169 char *p_name, bool *if_p)
37171 omp_clause_mask mask;
37172 enum tree_code code;
37173 switch (cp_parser_pragma_kind (pragma_tok))
37175 case PRAGMA_OACC_KERNELS:
37176 strcat (p_name, " kernels");
37177 mask = OACC_KERNELS_CLAUSE_MASK;
37178 code = OACC_KERNELS;
37179 break;
37180 case PRAGMA_OACC_PARALLEL:
37181 strcat (p_name, " parallel");
37182 mask = OACC_PARALLEL_CLAUSE_MASK;
37183 code = OACC_PARALLEL;
37184 break;
37185 default:
37186 gcc_unreachable ();
37189 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37191 const char *p
37192 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37193 if (strcmp (p, "loop") == 0)
37195 cp_lexer_consume_token (parser->lexer);
37196 tree block = begin_omp_parallel ();
37197 tree clauses;
37198 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37199 if_p);
37200 return finish_omp_construct (code, block, clauses);
37204 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37206 tree block = begin_omp_parallel ();
37207 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37208 cp_parser_statement (parser, NULL_TREE, false, if_p);
37209 cp_parser_end_omp_structured_block (parser, save);
37210 return finish_omp_construct (code, block, clauses);
37213 /* OpenACC 2.0:
37214 # pragma acc update oacc-update-clause[optseq] new-line
37217 #define OACC_UPDATE_CLAUSE_MASK \
37218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37225 static tree
37226 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37228 tree stmt, clauses;
37230 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37231 "#pragma acc update", pragma_tok);
37233 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37235 error_at (pragma_tok->location,
37236 "%<#pragma acc update%> must contain at least one "
37237 "%<device%> or %<host%> or %<self%> clause");
37238 return NULL_TREE;
37241 stmt = make_node (OACC_UPDATE);
37242 TREE_TYPE (stmt) = void_type_node;
37243 OACC_UPDATE_CLAUSES (stmt) = clauses;
37244 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37245 add_stmt (stmt);
37246 return stmt;
37249 /* OpenACC 2.0:
37250 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37252 LOC is the location of the #pragma token.
37255 #define OACC_WAIT_CLAUSE_MASK \
37256 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37258 static tree
37259 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37261 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37262 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37264 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37265 list = cp_parser_oacc_wait_list (parser, loc, list);
37267 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37268 "#pragma acc wait", pragma_tok);
37270 stmt = c_finish_oacc_wait (loc, list, clauses);
37271 stmt = finish_expr_stmt (stmt);
37273 return stmt;
37276 /* OpenMP 4.0:
37277 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37279 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37280 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37287 static void
37288 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37289 enum pragma_context context)
37291 bool first_p = parser->omp_declare_simd == NULL;
37292 cp_omp_declare_simd_data data;
37293 if (first_p)
37295 data.error_seen = false;
37296 data.fndecl_seen = false;
37297 data.tokens = vNULL;
37298 data.clauses = NULL_TREE;
37299 /* It is safe to take the address of a local variable; it will only be
37300 used while this scope is live. */
37301 parser->omp_declare_simd = &data;
37304 /* Store away all pragma tokens. */
37305 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37306 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37307 cp_lexer_consume_token (parser->lexer);
37308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37309 parser->omp_declare_simd->error_seen = true;
37310 cp_parser_require_pragma_eol (parser, pragma_tok);
37311 struct cp_token_cache *cp
37312 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37313 parser->omp_declare_simd->tokens.safe_push (cp);
37315 if (first_p)
37317 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37318 cp_parser_pragma (parser, context, NULL);
37319 switch (context)
37321 case pragma_external:
37322 cp_parser_declaration (parser);
37323 break;
37324 case pragma_member:
37325 cp_parser_member_declaration (parser);
37326 break;
37327 case pragma_objc_icode:
37328 cp_parser_block_declaration (parser, /*statement_p=*/false);
37329 break;
37330 default:
37331 cp_parser_declaration_statement (parser);
37332 break;
37334 if (parser->omp_declare_simd
37335 && !parser->omp_declare_simd->error_seen
37336 && !parser->omp_declare_simd->fndecl_seen)
37337 error_at (pragma_tok->location,
37338 "%<#pragma omp declare simd%> not immediately followed by "
37339 "function declaration or definition");
37340 data.tokens.release ();
37341 parser->omp_declare_simd = NULL;
37345 /* Finalize #pragma omp declare simd clauses after direct declarator has
37346 been parsed, and put that into "omp declare simd" attribute. */
37348 static tree
37349 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37351 struct cp_token_cache *ce;
37352 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37353 int i;
37355 if (!data->error_seen && data->fndecl_seen)
37357 error ("%<#pragma omp declare simd%> not immediately followed by "
37358 "a single function declaration or definition");
37359 data->error_seen = true;
37361 if (data->error_seen)
37362 return attrs;
37364 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37366 tree c, cl;
37368 cp_parser_push_lexer_for_tokens (parser, ce);
37369 parser->lexer->in_pragma = true;
37370 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37371 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37372 cp_lexer_consume_token (parser->lexer);
37373 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37374 "#pragma omp declare simd", pragma_tok);
37375 cp_parser_pop_lexer (parser);
37376 if (cl)
37377 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37378 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37379 TREE_CHAIN (c) = attrs;
37380 if (processing_template_decl)
37381 ATTR_IS_DEPENDENT (c) = 1;
37382 attrs = c;
37385 data->fndecl_seen = true;
37386 return attrs;
37390 /* OpenMP 4.0:
37391 # pragma omp declare target new-line
37392 declarations and definitions
37393 # pragma omp end declare target new-line
37395 OpenMP 4.5:
37396 # pragma omp declare target ( extended-list ) new-line
37398 # pragma omp declare target declare-target-clauses[seq] new-line */
37400 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37401 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37404 static void
37405 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37407 tree clauses = NULL_TREE;
37408 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37409 clauses
37410 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37411 "#pragma omp declare target", pragma_tok);
37412 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37414 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37415 clauses);
37416 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37417 cp_parser_require_pragma_eol (parser, pragma_tok);
37419 else
37421 cp_parser_require_pragma_eol (parser, pragma_tok);
37422 scope_chain->omp_declare_target_attribute++;
37423 return;
37425 if (scope_chain->omp_declare_target_attribute)
37426 error_at (pragma_tok->location,
37427 "%<#pragma omp declare target%> with clauses in between "
37428 "%<#pragma omp declare target%> without clauses and "
37429 "%<#pragma omp end declare target%>");
37430 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37432 tree t = OMP_CLAUSE_DECL (c), id;
37433 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37434 tree at2 = lookup_attribute ("omp declare target link",
37435 DECL_ATTRIBUTES (t));
37436 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37438 id = get_identifier ("omp declare target link");
37439 std::swap (at1, at2);
37441 else
37442 id = get_identifier ("omp declare target");
37443 if (at2)
37445 error_at (OMP_CLAUSE_LOCATION (c),
37446 "%qD specified both in declare target %<link%> and %<to%>"
37447 " clauses", t);
37448 continue;
37450 if (!at1)
37452 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37453 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37454 continue;
37456 symtab_node *node = symtab_node::get (t);
37457 if (node != NULL)
37459 node->offloadable = 1;
37460 if (ENABLE_OFFLOADING)
37462 g->have_offload = true;
37463 if (is_a <varpool_node *> (node))
37464 vec_safe_push (offload_vars, t);
37471 static void
37472 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37474 const char *p = "";
37475 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37477 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37478 p = IDENTIFIER_POINTER (id);
37480 if (strcmp (p, "declare") == 0)
37482 cp_lexer_consume_token (parser->lexer);
37483 p = "";
37484 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37486 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37487 p = IDENTIFIER_POINTER (id);
37489 if (strcmp (p, "target") == 0)
37490 cp_lexer_consume_token (parser->lexer);
37491 else
37493 cp_parser_error (parser, "expected %<target%>");
37494 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37495 return;
37498 else
37500 cp_parser_error (parser, "expected %<declare%>");
37501 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37502 return;
37504 cp_parser_require_pragma_eol (parser, pragma_tok);
37505 if (!scope_chain->omp_declare_target_attribute)
37506 error_at (pragma_tok->location,
37507 "%<#pragma omp end declare target%> without corresponding "
37508 "%<#pragma omp declare target%>");
37509 else
37510 scope_chain->omp_declare_target_attribute--;
37513 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37514 expression and optional initializer clause of
37515 #pragma omp declare reduction. We store the expression(s) as
37516 either 3, 6 or 7 special statements inside of the artificial function's
37517 body. The first two statements are DECL_EXPRs for the artificial
37518 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37519 expression that uses those variables.
37520 If there was any INITIALIZER clause, this is followed by further statements,
37521 the fourth and fifth statements are DECL_EXPRs for the artificial
37522 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37523 constructor variant (first token after open paren is not omp_priv),
37524 then the sixth statement is a statement with the function call expression
37525 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37526 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37527 to initialize the OMP_PRIV artificial variable and there is seventh
37528 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37530 static bool
37531 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37533 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37534 gcc_assert (TYPE_REF_P (type));
37535 type = TREE_TYPE (type);
37536 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37537 DECL_ARTIFICIAL (omp_out) = 1;
37538 pushdecl (omp_out);
37539 add_decl_expr (omp_out);
37540 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37541 DECL_ARTIFICIAL (omp_in) = 1;
37542 pushdecl (omp_in);
37543 add_decl_expr (omp_in);
37544 tree combiner;
37545 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37547 keep_next_level (true);
37548 tree block = begin_omp_structured_block ();
37549 combiner = cp_parser_expression (parser);
37550 finish_expr_stmt (combiner);
37551 block = finish_omp_structured_block (block);
37552 add_stmt (block);
37554 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37555 return false;
37557 const char *p = "";
37558 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37560 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37561 p = IDENTIFIER_POINTER (id);
37564 if (strcmp (p, "initializer") == 0)
37566 cp_lexer_consume_token (parser->lexer);
37567 matching_parens parens;
37568 if (!parens.require_open (parser))
37569 return false;
37571 p = "";
37572 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37574 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37575 p = IDENTIFIER_POINTER (id);
37578 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37579 DECL_ARTIFICIAL (omp_priv) = 1;
37580 pushdecl (omp_priv);
37581 add_decl_expr (omp_priv);
37582 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37583 DECL_ARTIFICIAL (omp_orig) = 1;
37584 pushdecl (omp_orig);
37585 add_decl_expr (omp_orig);
37587 keep_next_level (true);
37588 block = begin_omp_structured_block ();
37590 bool ctor = false;
37591 if (strcmp (p, "omp_priv") == 0)
37593 bool is_direct_init, is_non_constant_init;
37594 ctor = true;
37595 cp_lexer_consume_token (parser->lexer);
37596 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37597 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37598 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37599 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37600 == CPP_CLOSE_PAREN
37601 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37602 == CPP_CLOSE_PAREN))
37604 finish_omp_structured_block (block);
37605 error ("invalid initializer clause");
37606 return false;
37608 initializer = cp_parser_initializer (parser, &is_direct_init,
37609 &is_non_constant_init);
37610 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37611 NULL_TREE, LOOKUP_ONLYCONVERTING);
37613 else
37615 cp_parser_parse_tentatively (parser);
37616 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37617 /*check_dependency_p=*/true,
37618 /*template_p=*/NULL,
37619 /*declarator_p=*/false,
37620 /*optional_p=*/false);
37621 vec<tree, va_gc> *args;
37622 if (fn_name == error_mark_node
37623 || cp_parser_error_occurred (parser)
37624 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37625 || ((args = cp_parser_parenthesized_expression_list
37626 (parser, non_attr, /*cast_p=*/false,
37627 /*allow_expansion_p=*/true,
37628 /*non_constant_p=*/NULL)),
37629 cp_parser_error_occurred (parser)))
37631 finish_omp_structured_block (block);
37632 cp_parser_abort_tentative_parse (parser);
37633 cp_parser_error (parser, "expected id-expression (arguments)");
37634 return false;
37636 unsigned int i;
37637 tree arg;
37638 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37639 if (arg == omp_priv
37640 || (TREE_CODE (arg) == ADDR_EXPR
37641 && TREE_OPERAND (arg, 0) == omp_priv))
37642 break;
37643 cp_parser_abort_tentative_parse (parser);
37644 if (arg == NULL_TREE)
37645 error ("one of the initializer call arguments should be %<omp_priv%>"
37646 " or %<&omp_priv%>");
37647 initializer = cp_parser_postfix_expression (parser, false, false, false,
37648 false, NULL);
37649 finish_expr_stmt (initializer);
37652 block = finish_omp_structured_block (block);
37653 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37654 add_stmt (block);
37656 if (ctor)
37657 add_decl_expr (omp_orig);
37659 if (!parens.require_close (parser))
37660 return false;
37663 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37664 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37665 UNKNOWN_LOCATION);
37667 return true;
37670 /* OpenMP 4.0
37671 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37672 initializer-clause[opt] new-line
37674 initializer-clause:
37675 initializer (omp_priv initializer)
37676 initializer (function-name (argument-list)) */
37678 static void
37679 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37680 enum pragma_context)
37682 auto_vec<tree> types;
37683 enum tree_code reduc_code = ERROR_MARK;
37684 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37685 unsigned int i;
37686 cp_token *first_token;
37687 cp_token_cache *cp;
37688 int errs;
37689 void *p;
37691 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37692 p = obstack_alloc (&declarator_obstack, 0);
37694 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37695 goto fail;
37697 switch (cp_lexer_peek_token (parser->lexer)->type)
37699 case CPP_PLUS:
37700 reduc_code = PLUS_EXPR;
37701 break;
37702 case CPP_MULT:
37703 reduc_code = MULT_EXPR;
37704 break;
37705 case CPP_MINUS:
37706 reduc_code = MINUS_EXPR;
37707 break;
37708 case CPP_AND:
37709 reduc_code = BIT_AND_EXPR;
37710 break;
37711 case CPP_XOR:
37712 reduc_code = BIT_XOR_EXPR;
37713 break;
37714 case CPP_OR:
37715 reduc_code = BIT_IOR_EXPR;
37716 break;
37717 case CPP_AND_AND:
37718 reduc_code = TRUTH_ANDIF_EXPR;
37719 break;
37720 case CPP_OR_OR:
37721 reduc_code = TRUTH_ORIF_EXPR;
37722 break;
37723 case CPP_NAME:
37724 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37725 break;
37726 default:
37727 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37728 "%<|%>, %<&&%>, %<||%> or identifier");
37729 goto fail;
37732 if (reduc_code != ERROR_MARK)
37733 cp_lexer_consume_token (parser->lexer);
37735 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37736 if (reduc_id == error_mark_node)
37737 goto fail;
37739 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37740 goto fail;
37742 /* Types may not be defined in declare reduction type list. */
37743 const char *saved_message;
37744 saved_message = parser->type_definition_forbidden_message;
37745 parser->type_definition_forbidden_message
37746 = G_("types may not be defined in declare reduction type list");
37747 bool saved_colon_corrects_to_scope_p;
37748 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37749 parser->colon_corrects_to_scope_p = false;
37750 bool saved_colon_doesnt_start_class_def_p;
37751 saved_colon_doesnt_start_class_def_p
37752 = parser->colon_doesnt_start_class_def_p;
37753 parser->colon_doesnt_start_class_def_p = true;
37755 while (true)
37757 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37758 type = cp_parser_type_id (parser);
37759 if (type == error_mark_node)
37761 else if (ARITHMETIC_TYPE_P (type)
37762 && (orig_reduc_id == NULL_TREE
37763 || (TREE_CODE (type) != COMPLEX_TYPE
37764 && (id_equal (orig_reduc_id, "min")
37765 || id_equal (orig_reduc_id, "max")))))
37766 error_at (loc, "predeclared arithmetic type %qT in "
37767 "%<#pragma omp declare reduction%>", type);
37768 else if (TREE_CODE (type) == FUNCTION_TYPE
37769 || TREE_CODE (type) == METHOD_TYPE
37770 || TREE_CODE (type) == ARRAY_TYPE)
37771 error_at (loc, "function or array type %qT in "
37772 "%<#pragma omp declare reduction%>", type);
37773 else if (TYPE_REF_P (type))
37774 error_at (loc, "reference type %qT in "
37775 "%<#pragma omp declare reduction%>", type);
37776 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37777 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37778 "%<#pragma omp declare reduction%>", type);
37779 else
37780 types.safe_push (type);
37782 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37783 cp_lexer_consume_token (parser->lexer);
37784 else
37785 break;
37788 /* Restore the saved message. */
37789 parser->type_definition_forbidden_message = saved_message;
37790 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37791 parser->colon_doesnt_start_class_def_p
37792 = saved_colon_doesnt_start_class_def_p;
37794 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37795 || types.is_empty ())
37797 fail:
37798 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37799 goto done;
37802 first_token = cp_lexer_peek_token (parser->lexer);
37803 cp = NULL;
37804 errs = errorcount;
37805 FOR_EACH_VEC_ELT (types, i, type)
37807 tree fntype
37808 = build_function_type_list (void_type_node,
37809 cp_build_reference_type (type, false),
37810 NULL_TREE);
37811 tree this_reduc_id = reduc_id;
37812 if (!dependent_type_p (type))
37813 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37814 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37815 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37816 DECL_ARTIFICIAL (fndecl) = 1;
37817 DECL_EXTERNAL (fndecl) = 1;
37818 DECL_DECLARED_INLINE_P (fndecl) = 1;
37819 DECL_IGNORED_P (fndecl) = 1;
37820 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37821 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37822 DECL_ATTRIBUTES (fndecl)
37823 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37824 DECL_ATTRIBUTES (fndecl));
37825 if (processing_template_decl)
37826 fndecl = push_template_decl (fndecl);
37827 bool block_scope = false;
37828 tree block = NULL_TREE;
37829 if (current_function_decl)
37831 block_scope = true;
37832 DECL_CONTEXT (fndecl) = global_namespace;
37833 if (!processing_template_decl)
37834 pushdecl (fndecl);
37836 else if (current_class_type)
37838 if (cp == NULL)
37840 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37841 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37842 cp_lexer_consume_token (parser->lexer);
37843 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37844 goto fail;
37845 cp = cp_token_cache_new (first_token,
37846 cp_lexer_peek_nth_token (parser->lexer,
37847 2));
37849 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37850 finish_member_declaration (fndecl);
37851 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37852 DECL_PENDING_INLINE_P (fndecl) = 1;
37853 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37854 continue;
37856 else
37858 DECL_CONTEXT (fndecl) = current_namespace;
37859 pushdecl (fndecl);
37861 if (!block_scope)
37862 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37863 else
37864 block = begin_omp_structured_block ();
37865 if (cp)
37867 cp_parser_push_lexer_for_tokens (parser, cp);
37868 parser->lexer->in_pragma = true;
37870 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37872 if (!block_scope)
37873 finish_function (/*inline_p=*/false);
37874 else
37875 DECL_CONTEXT (fndecl) = current_function_decl;
37876 if (cp)
37877 cp_parser_pop_lexer (parser);
37878 goto fail;
37880 if (cp)
37881 cp_parser_pop_lexer (parser);
37882 if (!block_scope)
37883 finish_function (/*inline_p=*/false);
37884 else
37886 DECL_CONTEXT (fndecl) = current_function_decl;
37887 block = finish_omp_structured_block (block);
37888 if (TREE_CODE (block) == BIND_EXPR)
37889 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37890 else if (TREE_CODE (block) == STATEMENT_LIST)
37891 DECL_SAVED_TREE (fndecl) = block;
37892 if (processing_template_decl)
37893 add_decl_expr (fndecl);
37895 cp_check_omp_declare_reduction (fndecl);
37896 if (cp == NULL && types.length () > 1)
37897 cp = cp_token_cache_new (first_token,
37898 cp_lexer_peek_nth_token (parser->lexer, 2));
37899 if (errs != errorcount)
37900 break;
37903 cp_parser_require_pragma_eol (parser, pragma_tok);
37905 done:
37906 /* Free any declarators allocated. */
37907 obstack_free (&declarator_obstack, p);
37910 /* OpenMP 4.0
37911 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37912 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37913 initializer-clause[opt] new-line
37914 #pragma omp declare target new-line */
37916 static bool
37917 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37918 enum pragma_context context)
37920 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37922 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37923 const char *p = IDENTIFIER_POINTER (id);
37925 if (strcmp (p, "simd") == 0)
37927 cp_lexer_consume_token (parser->lexer);
37928 cp_parser_omp_declare_simd (parser, pragma_tok,
37929 context);
37930 return true;
37932 cp_ensure_no_omp_declare_simd (parser);
37933 if (strcmp (p, "reduction") == 0)
37935 cp_lexer_consume_token (parser->lexer);
37936 cp_parser_omp_declare_reduction (parser, pragma_tok,
37937 context);
37938 return false;
37940 if (!flag_openmp) /* flag_openmp_simd */
37942 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37943 return false;
37945 if (strcmp (p, "target") == 0)
37947 cp_lexer_consume_token (parser->lexer);
37948 cp_parser_omp_declare_target (parser, pragma_tok);
37949 return false;
37952 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37953 "or %<target%>");
37954 cp_parser_require_pragma_eol (parser, pragma_tok);
37955 return false;
37958 /* OpenMP 4.5:
37959 #pragma omp taskloop taskloop-clause[optseq] new-line
37960 for-loop
37962 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37963 for-loop */
37965 #define OMP_TASKLOOP_CLAUSE_MASK \
37966 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37981 static tree
37982 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37983 char *p_name, omp_clause_mask mask, tree *cclauses,
37984 bool *if_p)
37986 tree clauses, sb, ret;
37987 unsigned int save;
37988 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37990 strcat (p_name, " taskloop");
37991 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37993 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37995 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37996 const char *p = IDENTIFIER_POINTER (id);
37998 if (strcmp (p, "simd") == 0)
38000 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38001 if (cclauses == NULL)
38002 cclauses = cclauses_buf;
38004 cp_lexer_consume_token (parser->lexer);
38005 if (!flag_openmp) /* flag_openmp_simd */
38006 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38007 cclauses, if_p);
38008 sb = begin_omp_structured_block ();
38009 save = cp_parser_begin_omp_structured_block (parser);
38010 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38011 cclauses, if_p);
38012 cp_parser_end_omp_structured_block (parser, save);
38013 tree body = finish_omp_structured_block (sb);
38014 if (ret == NULL)
38015 return ret;
38016 ret = make_node (OMP_TASKLOOP);
38017 TREE_TYPE (ret) = void_type_node;
38018 OMP_FOR_BODY (ret) = body;
38019 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38020 SET_EXPR_LOCATION (ret, loc);
38021 add_stmt (ret);
38022 return ret;
38025 if (!flag_openmp) /* flag_openmp_simd */
38027 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38028 return NULL_TREE;
38031 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38032 cclauses == NULL);
38033 if (cclauses)
38035 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38036 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38039 sb = begin_omp_structured_block ();
38040 save = cp_parser_begin_omp_structured_block (parser);
38042 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38043 if_p);
38045 cp_parser_end_omp_structured_block (parser, save);
38046 add_stmt (finish_omp_structured_block (sb));
38048 return ret;
38052 /* OpenACC 2.0:
38053 # pragma acc routine oacc-routine-clause[optseq] new-line
38054 function-definition
38056 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38059 #define OACC_ROUTINE_CLAUSE_MASK \
38060 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38066 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38067 component, which must resolve to a declared namespace-scope
38068 function. The clauses are either processed directly (for a named
38069 function), or defered until the immediatley following declaration
38070 is parsed. */
38072 static void
38073 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38074 enum pragma_context context)
38076 gcc_checking_assert (context == pragma_external);
38077 /* The checking for "another pragma following this one" in the "no optional
38078 '( name )'" case makes sure that we dont re-enter. */
38079 gcc_checking_assert (parser->oacc_routine == NULL);
38081 cp_oacc_routine_data data;
38082 data.error_seen = false;
38083 data.fndecl_seen = false;
38084 data.tokens = vNULL;
38085 data.clauses = NULL_TREE;
38086 data.loc = pragma_tok->location;
38087 /* It is safe to take the address of a local variable; it will only be
38088 used while this scope is live. */
38089 parser->oacc_routine = &data;
38091 /* Look for optional '( name )'. */
38092 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38094 matching_parens parens;
38095 parens.consume_open (parser); /* '(' */
38097 /* We parse the name as an id-expression. If it resolves to
38098 anything other than a non-overloaded function at namespace
38099 scope, it's an error. */
38100 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38101 tree name = cp_parser_id_expression (parser,
38102 /*template_keyword_p=*/false,
38103 /*check_dependency_p=*/false,
38104 /*template_p=*/NULL,
38105 /*declarator_p=*/false,
38106 /*optional_p=*/false);
38107 tree decl = (identifier_p (name)
38108 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38109 : name);
38110 if (name != error_mark_node && decl == error_mark_node)
38111 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38113 if (decl == error_mark_node
38114 || !parens.require_close (parser))
38116 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38117 parser->oacc_routine = NULL;
38118 return;
38121 data.clauses
38122 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38123 "#pragma acc routine",
38124 cp_lexer_peek_token (parser->lexer));
38126 if (decl && is_overloaded_fn (decl)
38127 && (TREE_CODE (decl) != FUNCTION_DECL
38128 || DECL_FUNCTION_TEMPLATE_P (decl)))
38130 error_at (name_loc,
38131 "%<#pragma acc routine%> names a set of overloads");
38132 parser->oacc_routine = NULL;
38133 return;
38136 /* Perhaps we should use the same rule as declarations in different
38137 namespaces? */
38138 if (!DECL_NAMESPACE_SCOPE_P (decl))
38140 error_at (name_loc,
38141 "%qD does not refer to a namespace scope function", decl);
38142 parser->oacc_routine = NULL;
38143 return;
38146 if (TREE_CODE (decl) != FUNCTION_DECL)
38148 error_at (name_loc, "%qD does not refer to a function", decl);
38149 parser->oacc_routine = NULL;
38150 return;
38153 cp_finalize_oacc_routine (parser, decl, false);
38154 parser->oacc_routine = NULL;
38156 else /* No optional '( name )'. */
38158 /* Store away all pragma tokens. */
38159 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38160 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38161 cp_lexer_consume_token (parser->lexer);
38162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38163 parser->oacc_routine->error_seen = true;
38164 cp_parser_require_pragma_eol (parser, pragma_tok);
38165 struct cp_token_cache *cp
38166 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38167 parser->oacc_routine->tokens.safe_push (cp);
38169 /* Emit a helpful diagnostic if there's another pragma following this
38170 one. */
38171 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38173 cp_ensure_no_oacc_routine (parser);
38174 data.tokens.release ();
38175 /* ..., and then just keep going. */
38176 return;
38179 /* We only have to consider the pragma_external case here. */
38180 cp_parser_declaration (parser);
38181 if (parser->oacc_routine
38182 && !parser->oacc_routine->fndecl_seen)
38183 cp_ensure_no_oacc_routine (parser);
38184 else
38185 parser->oacc_routine = NULL;
38186 data.tokens.release ();
38190 /* Finalize #pragma acc routine clauses after direct declarator has
38191 been parsed. */
38193 static tree
38194 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38196 struct cp_token_cache *ce;
38197 cp_oacc_routine_data *data = parser->oacc_routine;
38199 if (!data->error_seen && data->fndecl_seen)
38201 error_at (data->loc,
38202 "%<#pragma acc routine%> not immediately followed by "
38203 "a single function declaration or definition");
38204 data->error_seen = true;
38206 if (data->error_seen)
38207 return attrs;
38209 gcc_checking_assert (data->tokens.length () == 1);
38210 ce = data->tokens[0];
38212 cp_parser_push_lexer_for_tokens (parser, ce);
38213 parser->lexer->in_pragma = true;
38214 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38216 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38217 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38218 parser->oacc_routine->clauses
38219 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38220 "#pragma acc routine", pragma_tok);
38221 cp_parser_pop_lexer (parser);
38222 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38223 fndecl_seen. */
38225 return attrs;
38228 /* Apply any saved OpenACC routine clauses to a just-parsed
38229 declaration. */
38231 static void
38232 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38234 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38236 /* Keep going if we're in error reporting mode. */
38237 if (parser->oacc_routine->error_seen
38238 || fndecl == error_mark_node)
38239 return;
38241 if (parser->oacc_routine->fndecl_seen)
38243 error_at (parser->oacc_routine->loc,
38244 "%<#pragma acc routine%> not immediately followed by"
38245 " a single function declaration or definition");
38246 parser->oacc_routine = NULL;
38247 return;
38249 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38251 cp_ensure_no_oacc_routine (parser);
38252 return;
38255 if (oacc_get_fn_attrib (fndecl))
38257 error_at (parser->oacc_routine->loc,
38258 "%<#pragma acc routine%> already applied to %qD", fndecl);
38259 parser->oacc_routine = NULL;
38260 return;
38263 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38265 error_at (parser->oacc_routine->loc,
38266 TREE_USED (fndecl)
38267 ? G_("%<#pragma acc routine%> must be applied before use")
38268 : G_("%<#pragma acc routine%> must be applied before "
38269 "definition"));
38270 parser->oacc_routine = NULL;
38271 return;
38274 /* Process the routine's dimension clauses. */
38275 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38276 oacc_replace_fn_attrib (fndecl, dims);
38278 /* Add an "omp declare target" attribute. */
38279 DECL_ATTRIBUTES (fndecl)
38280 = tree_cons (get_identifier ("omp declare target"),
38281 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38283 /* Don't unset parser->oacc_routine here: we may still need it to
38284 diagnose wrong usage. But, remember that we've used this "#pragma acc
38285 routine". */
38286 parser->oacc_routine->fndecl_seen = true;
38290 /* Main entry point to OpenMP statement pragmas. */
38292 static void
38293 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38295 tree stmt;
38296 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38297 omp_clause_mask mask (0);
38299 switch (cp_parser_pragma_kind (pragma_tok))
38301 case PRAGMA_OACC_ATOMIC:
38302 cp_parser_omp_atomic (parser, pragma_tok);
38303 return;
38304 case PRAGMA_OACC_CACHE:
38305 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38306 break;
38307 case PRAGMA_OACC_DATA:
38308 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38309 break;
38310 case PRAGMA_OACC_ENTER_DATA:
38311 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38312 break;
38313 case PRAGMA_OACC_EXIT_DATA:
38314 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38315 break;
38316 case PRAGMA_OACC_HOST_DATA:
38317 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38318 break;
38319 case PRAGMA_OACC_KERNELS:
38320 case PRAGMA_OACC_PARALLEL:
38321 strcpy (p_name, "#pragma acc");
38322 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38323 if_p);
38324 break;
38325 case PRAGMA_OACC_LOOP:
38326 strcpy (p_name, "#pragma acc");
38327 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38328 if_p);
38329 break;
38330 case PRAGMA_OACC_UPDATE:
38331 stmt = cp_parser_oacc_update (parser, pragma_tok);
38332 break;
38333 case PRAGMA_OACC_WAIT:
38334 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38335 break;
38336 case PRAGMA_OMP_ATOMIC:
38337 cp_parser_omp_atomic (parser, pragma_tok);
38338 return;
38339 case PRAGMA_OMP_CRITICAL:
38340 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38341 break;
38342 case PRAGMA_OMP_DISTRIBUTE:
38343 strcpy (p_name, "#pragma omp");
38344 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38345 if_p);
38346 break;
38347 case PRAGMA_OMP_FOR:
38348 strcpy (p_name, "#pragma omp");
38349 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38350 if_p);
38351 break;
38352 case PRAGMA_OMP_MASTER:
38353 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38354 break;
38355 case PRAGMA_OMP_PARALLEL:
38356 strcpy (p_name, "#pragma omp");
38357 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38358 if_p);
38359 break;
38360 case PRAGMA_OMP_SECTIONS:
38361 strcpy (p_name, "#pragma omp");
38362 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38363 break;
38364 case PRAGMA_OMP_SIMD:
38365 strcpy (p_name, "#pragma omp");
38366 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38367 if_p);
38368 break;
38369 case PRAGMA_OMP_SINGLE:
38370 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38371 break;
38372 case PRAGMA_OMP_TASK:
38373 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38374 break;
38375 case PRAGMA_OMP_TASKGROUP:
38376 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38377 break;
38378 case PRAGMA_OMP_TASKLOOP:
38379 strcpy (p_name, "#pragma omp");
38380 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38381 if_p);
38382 break;
38383 case PRAGMA_OMP_TEAMS:
38384 strcpy (p_name, "#pragma omp");
38385 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38386 if_p);
38387 break;
38388 default:
38389 gcc_unreachable ();
38392 protected_set_expr_location (stmt, pragma_tok->location);
38395 /* Transactional Memory parsing routines. */
38397 /* Parse a transaction attribute.
38399 txn-attribute:
38400 attribute
38401 [ [ identifier ] ]
38403 We use this instead of cp_parser_attributes_opt for transactions to avoid
38404 the pedwarn in C++98 mode. */
38406 static tree
38407 cp_parser_txn_attribute_opt (cp_parser *parser)
38409 cp_token *token;
38410 tree attr_name, attr = NULL;
38412 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38413 return cp_parser_attributes_opt (parser);
38415 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38416 return NULL_TREE;
38417 cp_lexer_consume_token (parser->lexer);
38418 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38419 goto error1;
38421 token = cp_lexer_peek_token (parser->lexer);
38422 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38424 token = cp_lexer_consume_token (parser->lexer);
38426 attr_name = (token->type == CPP_KEYWORD
38427 /* For keywords, use the canonical spelling,
38428 not the parsed identifier. */
38429 ? ridpointers[(int) token->keyword]
38430 : token->u.value);
38431 attr = build_tree_list (attr_name, NULL_TREE);
38433 else
38434 cp_parser_error (parser, "expected identifier");
38436 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38437 error1:
38438 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38439 return attr;
38442 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38444 transaction-statement:
38445 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38446 compound-statement
38447 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38450 static tree
38451 cp_parser_transaction (cp_parser *parser, cp_token *token)
38453 unsigned char old_in = parser->in_transaction;
38454 unsigned char this_in = 1, new_in;
38455 enum rid keyword = token->keyword;
38456 tree stmt, attrs, noex;
38458 cp_lexer_consume_token (parser->lexer);
38460 if (keyword == RID_TRANSACTION_RELAXED
38461 || keyword == RID_SYNCHRONIZED)
38462 this_in |= TM_STMT_ATTR_RELAXED;
38463 else
38465 attrs = cp_parser_txn_attribute_opt (parser);
38466 if (attrs)
38467 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38470 /* Parse a noexcept specification. */
38471 if (keyword == RID_ATOMIC_NOEXCEPT)
38472 noex = boolean_true_node;
38473 else if (keyword == RID_ATOMIC_CANCEL)
38475 /* cancel-and-throw is unimplemented. */
38476 sorry ("atomic_cancel");
38477 noex = NULL_TREE;
38479 else
38480 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38482 /* Keep track if we're in the lexical scope of an outer transaction. */
38483 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38485 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38487 parser->in_transaction = new_in;
38488 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38489 parser->in_transaction = old_in;
38491 finish_transaction_stmt (stmt, NULL, this_in, noex);
38493 return stmt;
38496 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38498 transaction-expression:
38499 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38500 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38503 static tree
38504 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38506 unsigned char old_in = parser->in_transaction;
38507 unsigned char this_in = 1;
38508 cp_token *token;
38509 tree expr, noex;
38510 bool noex_expr;
38511 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38513 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38514 || keyword == RID_TRANSACTION_RELAXED);
38516 if (!flag_tm)
38517 error_at (loc,
38518 keyword == RID_TRANSACTION_RELAXED
38519 ? G_("%<__transaction_relaxed%> without transactional memory "
38520 "support enabled")
38521 : G_("%<__transaction_atomic%> without transactional memory "
38522 "support enabled"));
38524 token = cp_parser_require_keyword (parser, keyword,
38525 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38526 : RT_TRANSACTION_RELAXED));
38527 gcc_assert (token != NULL);
38529 if (keyword == RID_TRANSACTION_RELAXED)
38530 this_in |= TM_STMT_ATTR_RELAXED;
38532 /* Set this early. This might mean that we allow transaction_cancel in
38533 an expression that we find out later actually has to be a constexpr.
38534 However, we expect that cxx_constant_value will be able to deal with
38535 this; also, if the noexcept has no constexpr, then what we parse next
38536 really is a transaction's body. */
38537 parser->in_transaction = this_in;
38539 /* Parse a noexcept specification. */
38540 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38541 true);
38543 if (!noex || !noex_expr
38544 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38546 matching_parens parens;
38547 parens.require_open (parser);
38549 expr = cp_parser_expression (parser);
38550 expr = finish_parenthesized_expr (expr);
38552 parens.require_close (parser);
38554 else
38556 /* The only expression that is available got parsed for the noexcept
38557 already. noexcept is true then. */
38558 expr = noex;
38559 noex = boolean_true_node;
38562 expr = build_transaction_expr (token->location, expr, this_in, noex);
38563 parser->in_transaction = old_in;
38565 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38566 return error_mark_node;
38568 return (flag_tm ? expr : error_mark_node);
38571 /* Parse a function-transaction-block.
38573 function-transaction-block:
38574 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38575 function-body
38576 __transaction_atomic txn-attribute[opt] function-try-block
38577 __transaction_relaxed ctor-initializer[opt] function-body
38578 __transaction_relaxed function-try-block
38581 static void
38582 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38584 unsigned char old_in = parser->in_transaction;
38585 unsigned char new_in = 1;
38586 tree compound_stmt, stmt, attrs;
38587 cp_token *token;
38589 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38590 || keyword == RID_TRANSACTION_RELAXED);
38591 token = cp_parser_require_keyword (parser, keyword,
38592 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38593 : RT_TRANSACTION_RELAXED));
38594 gcc_assert (token != NULL);
38596 if (keyword == RID_TRANSACTION_RELAXED)
38597 new_in |= TM_STMT_ATTR_RELAXED;
38598 else
38600 attrs = cp_parser_txn_attribute_opt (parser);
38601 if (attrs)
38602 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38605 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38607 parser->in_transaction = new_in;
38609 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38610 cp_parser_function_try_block (parser);
38611 else
38612 cp_parser_ctor_initializer_opt_and_function_body
38613 (parser, /*in_function_try_block=*/false);
38615 parser->in_transaction = old_in;
38617 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38620 /* Parse a __transaction_cancel statement.
38622 cancel-statement:
38623 __transaction_cancel txn-attribute[opt] ;
38624 __transaction_cancel txn-attribute[opt] throw-expression ;
38626 ??? Cancel and throw is not yet implemented. */
38628 static tree
38629 cp_parser_transaction_cancel (cp_parser *parser)
38631 cp_token *token;
38632 bool is_outer = false;
38633 tree stmt, attrs;
38635 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38636 RT_TRANSACTION_CANCEL);
38637 gcc_assert (token != NULL);
38639 attrs = cp_parser_txn_attribute_opt (parser);
38640 if (attrs)
38641 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38643 /* ??? Parse cancel-and-throw here. */
38645 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38647 if (!flag_tm)
38649 error_at (token->location, "%<__transaction_cancel%> without "
38650 "transactional memory support enabled");
38651 return error_mark_node;
38653 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38655 error_at (token->location, "%<__transaction_cancel%> within a "
38656 "%<__transaction_relaxed%>");
38657 return error_mark_node;
38659 else if (is_outer)
38661 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38662 && !is_tm_may_cancel_outer (current_function_decl))
38664 error_at (token->location, "outer %<__transaction_cancel%> not "
38665 "within outer %<__transaction_atomic%>");
38666 error_at (token->location,
38667 " or a %<transaction_may_cancel_outer%> function");
38668 return error_mark_node;
38671 else if (parser->in_transaction == 0)
38673 error_at (token->location, "%<__transaction_cancel%> not within "
38674 "%<__transaction_atomic%>");
38675 return error_mark_node;
38678 stmt = build_tm_abort_call (token->location, is_outer);
38679 add_stmt (stmt);
38681 return stmt;
38684 /* The parser. */
38686 static GTY (()) cp_parser *the_parser;
38689 /* Special handling for the first token or line in the file. The first
38690 thing in the file might be #pragma GCC pch_preprocess, which loads a
38691 PCH file, which is a GC collection point. So we need to handle this
38692 first pragma without benefit of an existing lexer structure.
38694 Always returns one token to the caller in *FIRST_TOKEN. This is
38695 either the true first token of the file, or the first token after
38696 the initial pragma. */
38698 static void
38699 cp_parser_initial_pragma (cp_token *first_token)
38701 tree name = NULL;
38703 cp_lexer_get_preprocessor_token (NULL, first_token);
38704 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38705 return;
38707 cp_lexer_get_preprocessor_token (NULL, first_token);
38708 if (first_token->type == CPP_STRING)
38710 name = first_token->u.value;
38712 cp_lexer_get_preprocessor_token (NULL, first_token);
38713 if (first_token->type != CPP_PRAGMA_EOL)
38714 error_at (first_token->location,
38715 "junk at end of %<#pragma GCC pch_preprocess%>");
38717 else
38718 error_at (first_token->location, "expected string literal");
38720 /* Skip to the end of the pragma. */
38721 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38722 cp_lexer_get_preprocessor_token (NULL, first_token);
38724 /* Now actually load the PCH file. */
38725 if (name)
38726 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38728 /* Read one more token to return to our caller. We have to do this
38729 after reading the PCH file in, since its pointers have to be
38730 live. */
38731 cp_lexer_get_preprocessor_token (NULL, first_token);
38734 /* Parse a pragma GCC ivdep. */
38736 static bool
38737 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38739 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38740 return true;
38743 /* Parse a pragma GCC unroll. */
38745 static unsigned short
38746 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38748 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38749 tree expr = cp_parser_constant_expression (parser);
38750 unsigned short unroll;
38751 expr = maybe_constant_value (expr);
38752 HOST_WIDE_INT lunroll = 0;
38753 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38754 || TREE_CODE (expr) != INTEGER_CST
38755 || (lunroll = tree_to_shwi (expr)) < 0
38756 || lunroll >= USHRT_MAX)
38758 error_at (location, "%<#pragma GCC unroll%> requires an"
38759 " assignment-expression that evaluates to a non-negative"
38760 " integral constant less than %u", USHRT_MAX);
38761 unroll = 0;
38763 else
38765 unroll = (unsigned short)lunroll;
38766 if (unroll == 0)
38767 unroll = 1;
38769 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38770 return unroll;
38773 /* Normal parsing of a pragma token. Here we can (and must) use the
38774 regular lexer. */
38776 static bool
38777 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38779 cp_token *pragma_tok;
38780 unsigned int id;
38781 tree stmt;
38782 bool ret;
38784 pragma_tok = cp_lexer_consume_token (parser->lexer);
38785 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38786 parser->lexer->in_pragma = true;
38788 id = cp_parser_pragma_kind (pragma_tok);
38789 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38790 cp_ensure_no_omp_declare_simd (parser);
38791 switch (id)
38793 case PRAGMA_GCC_PCH_PREPROCESS:
38794 error_at (pragma_tok->location,
38795 "%<#pragma GCC pch_preprocess%> must be first");
38796 break;
38798 case PRAGMA_OMP_BARRIER:
38799 switch (context)
38801 case pragma_compound:
38802 cp_parser_omp_barrier (parser, pragma_tok);
38803 return false;
38804 case pragma_stmt:
38805 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38806 "used in compound statements", "omp barrier");
38807 break;
38808 default:
38809 goto bad_stmt;
38811 break;
38813 case PRAGMA_OMP_FLUSH:
38814 switch (context)
38816 case pragma_compound:
38817 cp_parser_omp_flush (parser, pragma_tok);
38818 return false;
38819 case pragma_stmt:
38820 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38821 "used in compound statements", "omp flush");
38822 break;
38823 default:
38824 goto bad_stmt;
38826 break;
38828 case PRAGMA_OMP_TASKWAIT:
38829 switch (context)
38831 case pragma_compound:
38832 cp_parser_omp_taskwait (parser, pragma_tok);
38833 return false;
38834 case pragma_stmt:
38835 error_at (pragma_tok->location,
38836 "%<#pragma %s%> may only be used in compound statements",
38837 "omp taskwait");
38838 break;
38839 default:
38840 goto bad_stmt;
38842 break;
38844 case PRAGMA_OMP_TASKYIELD:
38845 switch (context)
38847 case pragma_compound:
38848 cp_parser_omp_taskyield (parser, pragma_tok);
38849 return false;
38850 case pragma_stmt:
38851 error_at (pragma_tok->location,
38852 "%<#pragma %s%> may only be used in compound statements",
38853 "omp taskyield");
38854 break;
38855 default:
38856 goto bad_stmt;
38858 break;
38860 case PRAGMA_OMP_CANCEL:
38861 switch (context)
38863 case pragma_compound:
38864 cp_parser_omp_cancel (parser, pragma_tok);
38865 return false;
38866 case pragma_stmt:
38867 error_at (pragma_tok->location,
38868 "%<#pragma %s%> may only be used in compound statements",
38869 "omp cancel");
38870 break;
38871 default:
38872 goto bad_stmt;
38874 break;
38876 case PRAGMA_OMP_CANCELLATION_POINT:
38877 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38878 return false;
38880 case PRAGMA_OMP_THREADPRIVATE:
38881 cp_parser_omp_threadprivate (parser, pragma_tok);
38882 return false;
38884 case PRAGMA_OMP_DECLARE:
38885 return cp_parser_omp_declare (parser, pragma_tok, context);
38887 case PRAGMA_OACC_DECLARE:
38888 cp_parser_oacc_declare (parser, pragma_tok);
38889 return false;
38891 case PRAGMA_OACC_ENTER_DATA:
38892 if (context == pragma_stmt)
38894 error_at (pragma_tok->location,
38895 "%<#pragma %s%> may only be used in compound statements",
38896 "acc enter 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_EXIT_DATA:
38905 if (context == pragma_stmt)
38907 error_at (pragma_tok->location,
38908 "%<#pragma %s%> may only be used in compound statements",
38909 "acc exit data");
38910 break;
38912 else if (context != pragma_compound)
38913 goto bad_stmt;
38914 cp_parser_omp_construct (parser, pragma_tok, if_p);
38915 return true;
38917 case PRAGMA_OACC_ROUTINE:
38918 if (context != pragma_external)
38920 error_at (pragma_tok->location,
38921 "%<#pragma acc routine%> must be at file scope");
38922 break;
38924 cp_parser_oacc_routine (parser, pragma_tok, context);
38925 return false;
38927 case PRAGMA_OACC_UPDATE:
38928 if (context == pragma_stmt)
38930 error_at (pragma_tok->location,
38931 "%<#pragma %s%> may only be used in compound statements",
38932 "acc update");
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_WAIT:
38941 if (context == pragma_stmt)
38943 error_at (pragma_tok->location,
38944 "%<#pragma %s%> may only be used in compound statements",
38945 "acc wait");
38946 break;
38948 else if (context != pragma_compound)
38949 goto bad_stmt;
38950 cp_parser_omp_construct (parser, pragma_tok, if_p);
38951 return true;
38953 case PRAGMA_OACC_ATOMIC:
38954 case PRAGMA_OACC_CACHE:
38955 case PRAGMA_OACC_DATA:
38956 case PRAGMA_OACC_HOST_DATA:
38957 case PRAGMA_OACC_KERNELS:
38958 case PRAGMA_OACC_PARALLEL:
38959 case PRAGMA_OACC_LOOP:
38960 case PRAGMA_OMP_ATOMIC:
38961 case PRAGMA_OMP_CRITICAL:
38962 case PRAGMA_OMP_DISTRIBUTE:
38963 case PRAGMA_OMP_FOR:
38964 case PRAGMA_OMP_MASTER:
38965 case PRAGMA_OMP_PARALLEL:
38966 case PRAGMA_OMP_SECTIONS:
38967 case PRAGMA_OMP_SIMD:
38968 case PRAGMA_OMP_SINGLE:
38969 case PRAGMA_OMP_TASK:
38970 case PRAGMA_OMP_TASKGROUP:
38971 case PRAGMA_OMP_TASKLOOP:
38972 case PRAGMA_OMP_TEAMS:
38973 if (context != pragma_stmt && context != pragma_compound)
38974 goto bad_stmt;
38975 stmt = push_omp_privatization_clauses (false);
38976 cp_parser_omp_construct (parser, pragma_tok, if_p);
38977 pop_omp_privatization_clauses (stmt);
38978 return true;
38980 case PRAGMA_OMP_ORDERED:
38981 if (context != pragma_stmt && context != pragma_compound)
38982 goto bad_stmt;
38983 stmt = push_omp_privatization_clauses (false);
38984 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38985 pop_omp_privatization_clauses (stmt);
38986 return ret;
38988 case PRAGMA_OMP_TARGET:
38989 if (context != pragma_stmt && context != pragma_compound)
38990 goto bad_stmt;
38991 stmt = push_omp_privatization_clauses (false);
38992 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38993 pop_omp_privatization_clauses (stmt);
38994 return ret;
38996 case PRAGMA_OMP_END_DECLARE_TARGET:
38997 cp_parser_omp_end_declare_target (parser, pragma_tok);
38998 return false;
39000 case PRAGMA_OMP_SECTION:
39001 error_at (pragma_tok->location,
39002 "%<#pragma omp section%> may only be used in "
39003 "%<#pragma omp sections%> construct");
39004 break;
39006 case PRAGMA_IVDEP:
39008 if (context == pragma_external)
39010 error_at (pragma_tok->location,
39011 "%<#pragma GCC ivdep%> must be inside a function");
39012 break;
39014 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39015 unsigned short unroll;
39016 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39017 if (tok->type == CPP_PRAGMA
39018 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39020 tok = cp_lexer_consume_token (parser->lexer);
39021 unroll = cp_parser_pragma_unroll (parser, tok);
39022 tok = cp_lexer_peek_token (the_parser->lexer);
39024 else
39025 unroll = 0;
39026 if (tok->type != CPP_KEYWORD
39027 || (tok->keyword != RID_FOR
39028 && tok->keyword != RID_WHILE
39029 && tok->keyword != RID_DO))
39031 cp_parser_error (parser, "for, while or do statement expected");
39032 return false;
39034 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39035 return true;
39038 case PRAGMA_UNROLL:
39040 if (context == pragma_external)
39042 error_at (pragma_tok->location,
39043 "%<#pragma GCC unroll%> must be inside a function");
39044 break;
39046 const unsigned short unroll
39047 = cp_parser_pragma_unroll (parser, pragma_tok);
39048 bool ivdep;
39049 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39050 if (tok->type == CPP_PRAGMA
39051 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39053 tok = cp_lexer_consume_token (parser->lexer);
39054 ivdep = cp_parser_pragma_ivdep (parser, tok);
39055 tok = cp_lexer_peek_token (the_parser->lexer);
39057 else
39058 ivdep = false;
39059 if (tok->type != CPP_KEYWORD
39060 || (tok->keyword != RID_FOR
39061 && tok->keyword != RID_WHILE
39062 && tok->keyword != RID_DO))
39064 cp_parser_error (parser, "for, while or do statement expected");
39065 return false;
39067 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39068 return true;
39071 default:
39072 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39073 c_invoke_pragma_handler (id);
39074 break;
39076 bad_stmt:
39077 cp_parser_error (parser, "expected declaration specifiers");
39078 break;
39081 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39082 return false;
39085 /* The interface the pragma parsers have to the lexer. */
39087 enum cpp_ttype
39088 pragma_lex (tree *value, location_t *loc)
39090 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39091 enum cpp_ttype ret = tok->type;
39093 *value = tok->u.value;
39094 if (loc)
39095 *loc = tok->location;
39097 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39098 ret = CPP_EOF;
39099 else if (ret == CPP_STRING)
39100 *value = cp_parser_string_literal (the_parser, false, false);
39101 else
39103 if (ret == CPP_KEYWORD)
39104 ret = CPP_NAME;
39105 cp_lexer_consume_token (the_parser->lexer);
39108 return ret;
39112 /* External interface. */
39114 /* Parse one entire translation unit. */
39116 void
39117 c_parse_file (void)
39119 static bool already_called = false;
39121 if (already_called)
39122 fatal_error (input_location,
39123 "inter-module optimizations not implemented for C++");
39124 already_called = true;
39126 the_parser = cp_parser_new ();
39127 push_deferring_access_checks (flag_access_control
39128 ? dk_no_deferred : dk_no_check);
39129 cp_parser_translation_unit (the_parser);
39130 the_parser = NULL;
39133 /* Create an identifier for a generic parameter type (a synthesized
39134 template parameter implied by `auto' or a concept identifier). */
39136 static GTY(()) int generic_parm_count;
39137 static tree
39138 make_generic_type_name ()
39140 char buf[32];
39141 sprintf (buf, "auto:%d", ++generic_parm_count);
39142 return get_identifier (buf);
39145 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39146 (creating a new template parameter list if necessary). Returns the newly
39147 created template type parm. */
39149 static tree
39150 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39152 gcc_assert (current_binding_level->kind == sk_function_parms);
39154 /* Before committing to modifying any scope, if we're in an
39155 implicit template scope, and we're trying to synthesize a
39156 constrained parameter, try to find a previous parameter with
39157 the same name. This is the same-type rule for abbreviated
39158 function templates.
39160 NOTE: We can generate implicit parameters when tentatively
39161 parsing a nested name specifier, only to reject that parse
39162 later. However, matching the same template-id as part of a
39163 direct-declarator should generate an identical template
39164 parameter, so this rule will merge them. */
39165 if (parser->implicit_template_scope && constr)
39167 tree t = parser->implicit_template_parms;
39168 while (t)
39170 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39172 tree d = TREE_VALUE (t);
39173 if (TREE_CODE (d) == PARM_DECL)
39174 /* Return the TEMPLATE_PARM_INDEX. */
39175 d = DECL_INITIAL (d);
39176 return d;
39178 t = TREE_CHAIN (t);
39182 /* We are either continuing a function template that already contains implicit
39183 template parameters, creating a new fully-implicit function template, or
39184 extending an existing explicit function template with implicit template
39185 parameters. */
39187 cp_binding_level *const entry_scope = current_binding_level;
39189 bool become_template = false;
39190 cp_binding_level *parent_scope = 0;
39192 if (parser->implicit_template_scope)
39194 gcc_assert (parser->implicit_template_parms);
39196 current_binding_level = parser->implicit_template_scope;
39198 else
39200 /* Roll back to the existing template parameter scope (in the case of
39201 extending an explicit function template) or introduce a new template
39202 parameter scope ahead of the function parameter scope (or class scope
39203 in the case of out-of-line member definitions). The function scope is
39204 added back after template parameter synthesis below. */
39206 cp_binding_level *scope = entry_scope;
39208 while (scope->kind == sk_function_parms)
39210 parent_scope = scope;
39211 scope = scope->level_chain;
39213 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39215 /* If not defining a class, then any class scope is a scope level in
39216 an out-of-line member definition. In this case simply wind back
39217 beyond the first such scope to inject the template parameter list.
39218 Otherwise wind back to the class being defined. The latter can
39219 occur in class member friend declarations such as:
39221 class A {
39222 void foo (auto);
39224 class B {
39225 friend void A::foo (auto);
39228 The template parameter list synthesized for the friend declaration
39229 must be injected in the scope of 'B'. This can also occur in
39230 erroneous cases such as:
39232 struct A {
39233 struct B {
39234 void foo (auto);
39236 void B::foo (auto) {}
39239 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39240 but, nevertheless, the template parameter list synthesized for the
39241 declarator should be injected into the scope of 'A' as if the
39242 ill-formed template was specified explicitly. */
39244 while (scope->kind == sk_class && !scope->defining_class_p)
39246 parent_scope = scope;
39247 scope = scope->level_chain;
39251 current_binding_level = scope;
39253 if (scope->kind != sk_template_parms
39254 || !function_being_declared_is_template_p (parser))
39256 /* Introduce a new template parameter list for implicit template
39257 parameters. */
39259 become_template = true;
39261 parser->implicit_template_scope
39262 = begin_scope (sk_template_parms, NULL);
39264 ++processing_template_decl;
39266 parser->fully_implicit_function_template_p = true;
39267 ++parser->num_template_parameter_lists;
39269 else
39271 /* Synthesize implicit template parameters at the end of the explicit
39272 template parameter list. */
39274 gcc_assert (current_template_parms);
39276 parser->implicit_template_scope = scope;
39278 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39279 parser->implicit_template_parms
39280 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39284 /* Synthesize a new template parameter and track the current template
39285 parameter chain with implicit_template_parms. */
39287 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39288 tree synth_id = make_generic_type_name ();
39289 tree synth_tmpl_parm;
39290 bool non_type = false;
39292 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39293 synth_tmpl_parm
39294 = finish_template_type_parm (class_type_node, synth_id);
39295 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39296 synth_tmpl_parm
39297 = finish_constrained_template_template_parm (proto, synth_id);
39298 else
39300 synth_tmpl_parm = copy_decl (proto);
39301 DECL_NAME (synth_tmpl_parm) = synth_id;
39302 non_type = true;
39305 // Attach the constraint to the parm before processing.
39306 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39307 TREE_TYPE (node) = constr;
39308 tree new_parm
39309 = process_template_parm (parser->implicit_template_parms,
39310 input_location,
39311 node,
39312 /*non_type=*/non_type,
39313 /*param_pack=*/false);
39315 // Chain the new parameter to the list of implicit parameters.
39316 if (parser->implicit_template_parms)
39317 parser->implicit_template_parms
39318 = TREE_CHAIN (parser->implicit_template_parms);
39319 else
39320 parser->implicit_template_parms = new_parm;
39322 tree new_decl = get_local_decls ();
39323 if (non_type)
39324 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39325 new_decl = DECL_INITIAL (new_decl);
39327 /* If creating a fully implicit function template, start the new implicit
39328 template parameter list with this synthesized type, otherwise grow the
39329 current template parameter list. */
39331 if (become_template)
39333 parent_scope->level_chain = current_binding_level;
39335 tree new_parms = make_tree_vec (1);
39336 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39337 current_template_parms = tree_cons (size_int (processing_template_decl),
39338 new_parms, current_template_parms);
39340 else
39342 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39343 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39344 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39345 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39348 // If the new parameter was constrained, we need to add that to the
39349 // constraints in the template parameter list.
39350 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39352 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39353 reqs = conjoin_constraints (reqs, req);
39354 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39357 current_binding_level = entry_scope;
39359 return new_decl;
39362 /* Finish the declaration of a fully implicit function template. Such a
39363 template has no explicit template parameter list so has not been through the
39364 normal template head and tail processing. synthesize_implicit_template_parm
39365 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39366 provided if the declaration is a class member such that its template
39367 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39368 form is returned. Otherwise NULL_TREE is returned. */
39370 static tree
39371 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39373 gcc_assert (parser->fully_implicit_function_template_p);
39375 if (member_decl_opt && member_decl_opt != error_mark_node
39376 && DECL_VIRTUAL_P (member_decl_opt))
39378 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39379 "implicit templates may not be %<virtual%>");
39380 DECL_VIRTUAL_P (member_decl_opt) = false;
39383 if (member_decl_opt)
39384 member_decl_opt = finish_member_template_decl (member_decl_opt);
39385 end_template_decl ();
39387 parser->fully_implicit_function_template_p = false;
39388 parser->implicit_template_parms = 0;
39389 parser->implicit_template_scope = 0;
39390 --parser->num_template_parameter_lists;
39392 return member_decl_opt;
39395 /* Like finish_fully_implicit_template, but to be used in error
39396 recovery, rearranging scopes so that we restore the state we had
39397 before synthesize_implicit_template_parm inserted the implement
39398 template parms scope. */
39400 static void
39401 abort_fully_implicit_template (cp_parser *parser)
39403 cp_binding_level *return_to_scope = current_binding_level;
39405 if (parser->implicit_template_scope
39406 && return_to_scope != parser->implicit_template_scope)
39408 cp_binding_level *child = return_to_scope;
39409 for (cp_binding_level *scope = child->level_chain;
39410 scope != parser->implicit_template_scope;
39411 scope = child->level_chain)
39412 child = scope;
39413 child->level_chain = parser->implicit_template_scope->level_chain;
39414 parser->implicit_template_scope->level_chain = return_to_scope;
39415 current_binding_level = parser->implicit_template_scope;
39417 else
39418 return_to_scope = return_to_scope->level_chain;
39420 finish_fully_implicit_template (parser, NULL);
39422 gcc_assert (current_binding_level == return_to_scope);
39425 /* Helper function for diagnostics that have complained about things
39426 being used with 'extern "C"' linkage.
39428 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39430 void
39431 maybe_show_extern_c_location (void)
39433 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39434 inform (the_parser->innermost_linkage_specification_location,
39435 "%<extern \"C\"%> linkage started here");
39438 #include "gt-cp-parser.h"