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)
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/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
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 {
62 /* floating-point literal */
66 /* %<__FUNCTION__%> */
68 /* %<__PRETTY_FUNCTION__%> */
76 /* %<typeid%> operator */
78 /* non-constant compound literals */
86 /* an array reference */
92 /* the address of a label */
106 /* calls to overloaded operators */
110 /* a comma operator */
112 /* a call to a constructor */
114 /* a transaction expression */
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
124 /* is not a class or namespace */
126 /* is not a class, namespace, or enumeration */
130 /* The various kinds of required token */
131 enum required_token {
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
142 RT_GREATER, /* '>' */
144 RT_ELLIPSIS, /* '...' */
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 */
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 */
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
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
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; }
204 static cp_lexer *cp_lexer_new_main
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
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
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
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
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
253 static bool cp_parser_omp_declare_reduction_exprs
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
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 [[ ]]. */
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;
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
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, " ... ");
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 if (token == start_token)
314 if (token == curr_token)
315 fprintf (file, "[[");
317 cp_lexer_print_token (file, token);
319 if (token == curr_token)
320 fprintf (file, "]]");
326 case CPP_CLOSE_BRACE:
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. */
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
355 debug (vec<cp_token, va_gc> &ref)
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
361 debug (vec<cp_token, va_gc> *ptr)
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. */
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
384 /* Dump parser context C to FILE. */
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. */
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
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. */
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
419 fprintf (file, "%s: true\n", desc);
423 /* Print an unparsed function entry UF to FILE. */
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
429 cp_default_arg_entry *default_arg_fn;
432 fprintf (file, "\tFunctions with default args:\n");
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
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);
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);
460 fprintf (file, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
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. */
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 cp_token *next_token, *first_token, *start_token;
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
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
507 cp_debug_parser (FILE *file, cp_parser *parser)
509 const size_t window_size = 20;
511 expanded_location eloc;
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 "
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);
590 debug (cp_parser &ref)
592 cp_debug_parser (stderr, &ref);
596 debug (cp_parser *ptr)
601 fprintf (stderr, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
607 cp_lexer_alloc (void)
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);
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
632 cp_lexer_new_main (void)
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 ()
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
665 gcc_assert (!lexer->next_token->purged_p);
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. */
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);
693 /* Frees all resources associated with LEXER. */
696 cp_lexer_destroy (cp_lexer *lexer)
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
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. */
713 cp_lexer_debugging_p (cp_lexer *lexer)
715 if (!LEXER_DEBUGGING_ENABLED_P)
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)
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;
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. */
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
763 return cp_lexer_token_at (lexer, tp);
766 /* nonzero if we are presently saving tokens. */
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. */
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. */
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);
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
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
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
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. */
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. */
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
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. */
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. */
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. */
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
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;
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. */
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. */
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
943 /* Storage classes. */
949 /* Elaborated type specifiers. */
955 /* Simple type specifiers. */
969 /* GNU extensions. */
972 /* C++0x extensions. */
974 case RID_UNDERLYING_TYPE:
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])
987 /* Return true if the next token is a keyword for a decl-specifier. */
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
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. */
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. */
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. */
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;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
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. */
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1049 /* N is 1-based, not zero-based. */
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1062 if (token == lexer->last_token)
1068 if (!token->purged_p)
1072 if (cp_lexer_debugging_p (lexer))
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged 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;
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);
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
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;
1135 if (tok == lexer->last_token)
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. */
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
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. */
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. */
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
1211 struct saved_token_sentinel
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1223 cp_lexer_rollback_tokens (lexer);
1226 ~saved_token_sentinel()
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1234 /* Print a representation of the TOKEN on the STREAM. */
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,
1248 /* C++ parser token types - see "Manifest constants", above. */
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
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))
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1276 print_generic_expr (stream, token->u.value);
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);
1285 fprintf (stream, "[%d]", token->type);
1291 debug (cp_token &ref)
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1298 debug (cp_token *ptr)
1303 fprintf (stderr, "<nil>\n");
1307 /* Start emitting debugging information. */
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. */
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;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
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. */
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;
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1373 cp_ensure_no_omp_declare_simd (parser);
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
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. */
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
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
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;
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
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(); };
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
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;
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. */
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
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;
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
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;
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1544 declarator->parameter_pack_p = false;
1546 declarator->std_attributes = attributes;
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. */
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
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;
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1573 declarator->parameter_pack_p = false;
1575 declarator->std_attributes = attributes;
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. */
1586 make_call_declarator (cp_declarator *target,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_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;
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1615 declarator->parameter_pack_p = false;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
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;
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1638 declarator->parameter_pack_p = false;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
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. */
1657 while (declarator && !found)
1659 switch ((int)declarator->kind)
1671 declarator = declarator->declarator;
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,
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;
1699 clear_decl_specs (¶meter->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;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1711 function_declarator_p (const cp_declarator *declarator)
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1722 declarator = declarator->declarator;
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
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.)
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.
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
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
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
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. */
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,
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
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. */
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
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
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
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));
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. */
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;
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
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);
1986 pop_unparsed_function_queues (cp_parser *parser)
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1994 /* Constructors and destructors. */
1996 static cp_parser *cp_parser_new
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
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2019 static tree cp_parser_userdef_string_literal
2021 static cp_expr cp_parser_userdef_numeric_literal
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
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,
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
2060 static tree cp_parser_new_expression
2062 static vec<tree, va_gc> *cp_parser_new_placement
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2068 static cp_declarator *cp_parser_direct_new_declarator
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2072 static tree cp_parser_delete_expression
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
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
2090 static cp_expr cp_parser_lambda_expression
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
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
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2131 static tree cp_parser_jump_statement
2133 static void cp_parser_declaration_statement
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
2145 static void cp_parser_declaration
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
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,
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
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
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
2178 static void cp_parser_namespace_definition
2180 static void cp_parser_namespace_body
2182 static tree cp_parser_qualified_namespace_specifier
2184 static void cp_parser_namespace_alias_definition
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2190 static tree cp_parser_alias_declaration
2192 static void cp_parser_asm_definition
2194 static void cp_parser_linkage_specification
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
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
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 static tree cp_parser_tx_qualifier_opt
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
2228 static tree cp_parser_template_type_arg
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
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *, bool *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *, 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
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
2276 static tree cp_parser_class_head
2277 (cp_parser *, bool *);
2278 static enum tag_types cp_parser_class_key
2280 static void cp_parser_type_parameter_key
2281 (cp_parser* parser);
2282 static void cp_parser_member_specification_opt
2284 static void cp_parser_member_declaration
2286 static tree cp_parser_pure_specifier
2288 static tree cp_parser_constant_initializer
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2295 static tree cp_parser_base_specifier
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2302 static tree cp_parser_conversion_type_id
2304 static cp_declarator *cp_parser_conversion_declarator_opt
2306 static void cp_parser_ctor_initializer_opt
2308 static void cp_parser_mem_initializer_list
2310 static tree cp_parser_mem_initializer
2312 static tree cp_parser_mem_initializer_id
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2319 static cp_expr cp_parser_operator
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser *, bool);
2326 static tree cp_parser_template_parameter_list
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
2338 static tree cp_parser_template_argument
2340 static void cp_parser_explicit_instantiation
2342 static void cp_parser_explicit_specialization
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2349 static void cp_parser_function_try_block
2351 static void cp_parser_handler_seq
2353 static void cp_parser_handler
2355 static tree cp_parser_exception_declaration
2357 static tree cp_parser_throw_expression
2359 static tree cp_parser_exception_specification_opt
2361 static tree cp_parser_type_id_list
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2368 static tree cp_parser_asm_operand_list
2370 static tree cp_parser_asm_clobber_list
2372 static tree cp_parser_asm_label_list
2374 static bool cp_next_tokens_can_be_attribute_p
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2378 static bool cp_next_tokens_can_be_std_attribute_p
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
2388 static tree cp_parser_gnu_attributes_opt
2390 static tree cp_parser_gnu_attribute_list
2392 static tree cp_parser_std_attribute
2393 (cp_parser *, tree);
2394 static tree cp_parser_std_attribute_spec
2396 static tree cp_parser_std_attribute_spec_seq
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
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2409 static tree cp_parser_requires_clause_opt
2411 static tree cp_parser_requires_expression
2413 static tree cp_parser_requirement_parameter_list
2415 static tree cp_parser_requirement_body
2417 static tree cp_parser_requirement_list
2419 static tree cp_parser_requirement
2421 static tree cp_parser_simple_requirement
2423 static tree cp_parser_compound_requirement
2425 static tree cp_parser_type_requirement
2427 static tree cp_parser_nested_requirement
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
2441 enum pragma_context {
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
2455 static tree cp_parser_objc_message_args
2457 static tree cp_parser_objc_message_expression
2459 static cp_expr cp_parser_objc_encode_expression
2461 static tree cp_parser_objc_defs_expression
2463 static tree cp_parser_objc_protocol_expression
2465 static tree cp_parser_objc_selector_expression
2467 static cp_expr cp_parser_objc_expression
2469 static bool cp_parser_objc_selector_p
2471 static tree cp_parser_objc_selector
2473 static tree cp_parser_objc_protocol_refs_opt
2475 static void cp_parser_objc_declaration
2476 (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration
2483 static void cp_parser_objc_at_synthesize_declaration
2485 static void cp_parser_objc_at_dynamic_declaration
2487 static tree cp_parser_objc_struct_declaration
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
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
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
2524 static tree cp_parser_enclosed_template_argument_list
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
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
2559 static bool cp_parser_next_token_starts_class_definition_p
2561 static bool cp_parser_next_token_ends_template_argument_p
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
2567 static enum tag_types cp_parser_token_is_type_parameter_key
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
2575 static void cp_parser_pre_parsed_nested_name_specifier
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
2583 static void cp_parser_commit_to_tentative_parse
2585 static void cp_parser_commit_to_topmost_tentative_parse
2587 static void cp_parser_abort_tentative_parse
2589 static bool cp_parser_parse_definitely
2591 static inline bool cp_parser_parsing_tentatively
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
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
2601 static bool cp_parser_check_type_definition
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
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
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2619 static void cp_parser_skip_to_end_of_block_or_statement
2621 static bool cp_parser_skip_to_closing_brace
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2629 static bool cp_parser_allow_gnu_extensions_p
2631 static bool cp_parser_is_pure_string_literal
2633 static bool cp_parser_is_string_literal
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
2643 static bool cp_parser_array_designator_p
2645 static bool cp_parser_init_statement_p
2647 static bool cp_parser_skip_to_closing_square_bracket
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. */
2677 cp_parser_parsing_tentatively (cp_parser* parser)
2679 return parser->context->next != NULL;
2682 /* Returns nonzero if TOKEN is a string literal. */
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. */
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. */
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
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token *token)
2722 if (token->type != CPP_PRAGMA)
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
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)
2746 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2747 if (token3->type != tok1_kind)
2749 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2750 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
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)
2758 /* We have a conflict marker. Construct a location of the form:
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);
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2772 get_matching_symbol (required_token token_desc)
2779 case RT_CLOSE_BRACE:
2781 case RT_CLOSE_PAREN:
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)
2795 return CPP_SEMICOLON;
2797 return CPP_OPEN_PAREN;
2798 case RT_CLOSE_BRACE:
2799 return CPP_CLOSE_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;
2810 case RT_CLOSE_PAREN:
2811 return CPP_CLOSE_PAREN;
2814 /* Use CPP_EOF as a "no completions possible" code. */
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
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
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);
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)
2862 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2864 error_at (loc, "version control conflict marker in file");
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
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
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. */
2929 cp_parser_name_lookup_error (cp_parser* parser,
2932 name_lookup_error desired,
2933 location_t location)
2935 /* If name lookup completely failed, tell the user that NAME was not
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);
2952 error_at (location, "%qE has not been declared", name);
2954 else if (parser->scope && parser->scope != global_namespace)
2959 error_at (location, "%<%E::%E%> is not a type",
2960 parser->scope, name);
2963 error_at (location, "%<%E::%E%> is not a class or namespace",
2964 parser->scope, name);
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser->scope, name);
2976 else if (parser->scope == global_namespace)
2981 error_at (location, "%<::%E%> is not a type", name);
2984 error_at (location, "%<::%E%> is not a class or namespace", name);
2988 "%<::%E%> is not a class, namespace, or enumeration",
3000 error_at (location, "%qE is not a type", name);
3003 error_at (location, "%qE is not a class or namespace", name);
3007 "%qE is not a class, namespace, or enumeration", name);
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. */
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;
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
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);
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. */
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. */
3062 && (declarator->kind == cdk_pointer
3063 || declarator->kind == cdk_reference
3064 || declarator->kind == cdk_ptrmem))
3065 declarator = declarator->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)",
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) */
3084 cp_parser_check_for_invalid_template_id (cp_parser* parser,
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);
3102 error_at (location, "%qE is not a template", type);
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. */
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. */
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;
3138 pedwarn (input_location, OPT_Wpedantic,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3171 case NIC_ADDR_LABEL:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3175 case NIC_OVERLOADED:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3179 case NIC_ASSIGNMENT:
3180 error ("an assignment cannot appear in a constant-expression");
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3186 case NIC_CONSTRUCTOR:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3190 case NIC_TRANSACTION:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3198 msg = "__FUNCTION__";
3200 case NIC_PRETTY_FUNC:
3201 msg = "__PRETTY_FUNCTION__";
3221 case NIC_PREINCREMENT:
3224 case NIC_PREDECREMENT:
3237 error ("%qs cannot appear in a constant-expression", msg);
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. */
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
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))
3270 "invalid use of template-name %qE without an argument list",
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. */
3286 if (TREE_CODE (id) == IDENTIFIER_NODE)
3287 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3290 gcc_rich_location richloc (location);
3291 richloc.add_fixit_replace (hint.suggestion ());
3293 "%qE does not name a type; did you mean %qs?",
3294 id, hint.suggestion ());
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))
3323 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3327 tree base_type = BINFO_TYPE (b);
3328 if (CLASS_TYPE_P (base_type)
3329 && dependent_type_p (base_type))
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);
3338 field = DECL_CHAIN (field))
3339 if (TREE_CODE (field) == TYPE_DECL
3340 && DECL_NAME (field) == id)
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b), id);
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",
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);
3368 error_at (location_of (id),
3369 "%qE in namespace %qE does not name a type",
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,
3377 else if (CLASS_TYPE_P (parser->scope)
3378 && constructor_name_p (id, parser->scope))
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",
3387 else if (TYPE_P (parser->scope)
3388 && dependent_scope_p (parser->scope))
3390 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
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));
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,
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",
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);
3418 error_at (location_of (id),
3419 "%qE in %q#T does not name a type",
3422 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
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. */
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
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)
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);
3468 if (!cp_parser_parse_definitely (parser))
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
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);
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. */
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3493 unsigned paren_depth = 0;
3494 unsigned brace_depth = 0;
3495 unsigned square_depth = 0;
3497 if (recovering && or_ttype == CPP_EOF
3498 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3503 cp_token * token = cp_lexer_peek_token (parser->lexer);
3505 /* Have we found what we're looking for before the closing paren? */
3506 if (token->type == or_ttype && or_ttype != CPP_EOF
3507 && !brace_depth && !paren_depth && !square_depth)
3510 switch (token->type)
3513 case CPP_PRAGMA_EOL:
3514 /* If we've run out of tokens, then there is no closing `)'. */
3517 /* This is good for lambda expression capture-lists. */
3518 case CPP_OPEN_SQUARE:
3521 case CPP_CLOSE_SQUARE:
3522 if (!square_depth--)
3527 /* This matches the processing in skip_to_end_of_statement. */
3532 case CPP_OPEN_BRACE:
3535 case CPP_CLOSE_BRACE:
3540 case CPP_OPEN_PAREN:
3545 case CPP_CLOSE_PAREN:
3546 if (!brace_depth && !paren_depth--)
3549 cp_lexer_consume_token (parser->lexer);
3558 /* Consume the token. */
3559 cp_lexer_consume_token (parser->lexer);
3563 /* Consume tokens up to, and including, the next non-nested closing `)'.
3564 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3565 are doing error recovery. Returns -1 if OR_COMMA is true and we
3566 found an unnested token of that type. */
3569 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3574 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3575 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3576 ttype, consume_paren);
3579 /* Consume tokens until we reach the end of the current statement.
3580 Normally, that will be just before consuming a `;'. However, if a
3581 non-nested `}' comes first, then we stop before consuming that. */
3584 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3586 unsigned nesting_depth = 0;
3588 /* Unwind generic function template scope if necessary. */
3589 if (parser->fully_implicit_function_template_p)
3590 abort_fully_implicit_template (parser);
3594 cp_token *token = cp_lexer_peek_token (parser->lexer);
3596 switch (token->type)
3599 case CPP_PRAGMA_EOL:
3600 /* If we've run out of tokens, stop. */
3604 /* If the next token is a `;', we have reached the end of the
3610 case CPP_CLOSE_BRACE:
3611 /* If this is a non-nested '}', stop before consuming it.
3612 That way, when confronted with something like:
3616 we stop before consuming the closing '}', even though we
3617 have not yet reached a `;'. */
3618 if (nesting_depth == 0)
3621 /* If it is the closing '}' for a block that we have
3622 scanned, stop -- but only after consuming the token.
3628 we will stop after the body of the erroneously declared
3629 function, but before consuming the following `typedef'
3631 if (--nesting_depth == 0)
3633 cp_lexer_consume_token (parser->lexer);
3638 case CPP_OPEN_BRACE:
3646 /* Consume the token. */
3647 cp_lexer_consume_token (parser->lexer);
3651 /* This function is called at the end of a statement or declaration.
3652 If the next token is a semicolon, it is consumed; otherwise, error
3653 recovery is attempted. */
3656 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3658 /* Look for the trailing `;'. */
3659 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3661 /* If there is additional (erroneous) input, skip to the end of
3663 cp_parser_skip_to_end_of_statement (parser);
3664 /* If the next token is now a `;', consume it. */
3665 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3666 cp_lexer_consume_token (parser->lexer);
3670 /* Skip tokens until we have consumed an entire block, or until we
3671 have consumed a non-nested `;'. */
3674 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3676 int nesting_depth = 0;
3678 /* Unwind generic function template scope if necessary. */
3679 if (parser->fully_implicit_function_template_p)
3680 abort_fully_implicit_template (parser);
3682 while (nesting_depth >= 0)
3684 cp_token *token = cp_lexer_peek_token (parser->lexer);
3686 switch (token->type)
3689 case CPP_PRAGMA_EOL:
3690 /* If we've run out of tokens, stop. */
3694 /* Stop if this is an unnested ';'. */
3699 case CPP_CLOSE_BRACE:
3700 /* Stop if this is an unnested '}', or closes the outermost
3703 if (nesting_depth < 0)
3709 case CPP_OPEN_BRACE:
3718 /* Consume the token. */
3719 cp_lexer_consume_token (parser->lexer);
3723 /* Skip tokens until a non-nested closing curly brace is the next
3724 token, or there are no more tokens. Return true in the first case,
3728 cp_parser_skip_to_closing_brace (cp_parser *parser)
3730 unsigned nesting_depth = 0;
3734 cp_token *token = cp_lexer_peek_token (parser->lexer);
3736 switch (token->type)
3739 case CPP_PRAGMA_EOL:
3740 /* If we've run out of tokens, stop. */
3743 case CPP_CLOSE_BRACE:
3744 /* If the next token is a non-nested `}', then we have reached
3745 the end of the current block. */
3746 if (nesting_depth-- == 0)
3750 case CPP_OPEN_BRACE:
3751 /* If it the next token is a `{', then we are entering a new
3752 block. Consume the entire block. */
3760 /* Consume the token. */
3761 cp_lexer_consume_token (parser->lexer);
3765 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3766 parameter is the PRAGMA token, allowing us to purge the entire pragma
3770 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3774 parser->lexer->in_pragma = false;
3777 token = cp_lexer_consume_token (parser->lexer);
3778 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3780 /* Ensure that the pragma is not parsed again. */
3781 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3784 /* Require pragma end of line, resyncing with it as necessary. The
3785 arguments are as for cp_parser_skip_to_pragma_eol. */
3788 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3790 parser->lexer->in_pragma = false;
3791 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3792 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3795 /* This is a simple wrapper around make_typename_type. When the id is
3796 an unresolved identifier node, we can provide a superior diagnostic
3797 using cp_parser_diagnose_invalid_type_name. */
3800 cp_parser_make_typename_type (cp_parser *parser, tree id,
3801 location_t id_location)
3804 if (identifier_p (id))
3806 result = make_typename_type (parser->scope, id, typename_type,
3807 /*complain=*/tf_none);
3808 if (result == error_mark_node)
3809 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3812 return make_typename_type (parser->scope, id, typename_type, tf_error);
3815 /* This is a wrapper around the
3816 make_{pointer,ptrmem,reference}_declarator functions that decides
3817 which one to call based on the CODE and CLASS_TYPE arguments. The
3818 CODE argument should be one of the values returned by
3819 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3820 appertain to the pointer or reference. */
3822 static cp_declarator *
3823 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3824 cp_cv_quals cv_qualifiers,
3825 cp_declarator *target,
3828 if (code == ERROR_MARK || target == cp_error_declarator)
3829 return cp_error_declarator;
3831 if (code == INDIRECT_REF)
3832 if (class_type == NULL_TREE)
3833 return make_pointer_declarator (cv_qualifiers, target, attributes);
3835 return make_ptrmem_declarator (cv_qualifiers, class_type,
3836 target, attributes);
3837 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3838 return make_reference_declarator (cv_qualifiers, target,
3840 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3841 return make_reference_declarator (cv_qualifiers, target,
3846 /* Create a new C++ parser. */
3849 cp_parser_new (void)
3855 /* cp_lexer_new_main is called before doing GC allocation because
3856 cp_lexer_new_main might load a PCH file. */
3857 lexer = cp_lexer_new_main ();
3859 /* Initialize the binops_by_token so that we can get the tree
3860 directly from the token. */
3861 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3862 binops_by_token[binops[i].token_type] = binops[i];
3864 parser = ggc_cleared_alloc<cp_parser> ();
3865 parser->lexer = lexer;
3866 parser->context = cp_parser_context_new (NULL);
3868 /* For now, we always accept GNU extensions. */
3869 parser->allow_gnu_extensions_p = 1;
3871 /* The `>' token is a greater-than operator, not the end of a
3873 parser->greater_than_is_operator_p = true;
3875 parser->default_arg_ok_p = true;
3877 /* We are not parsing a constant-expression. */
3878 parser->integral_constant_expression_p = false;
3879 parser->allow_non_integral_constant_expression_p = false;
3880 parser->non_integral_constant_expression_p = false;
3882 /* Local variable names are not forbidden. */
3883 parser->local_variables_forbidden_p = false;
3885 /* We are not processing an `extern "C"' declaration. */
3886 parser->in_unbraced_linkage_specification_p = false;
3888 /* We are not processing a declarator. */
3889 parser->in_declarator_p = false;
3891 /* We are not processing a template-argument-list. */
3892 parser->in_template_argument_list_p = false;
3894 /* We are not in an iteration statement. */
3895 parser->in_statement = 0;
3897 /* We are not in a switch statement. */
3898 parser->in_switch_statement_p = false;
3900 /* We are not parsing a type-id inside an expression. */
3901 parser->in_type_id_in_expr_p = false;
3903 /* Declarations aren't implicitly extern "C". */
3904 parser->implicit_extern_c = false;
3906 /* String literals should be translated to the execution character set. */
3907 parser->translate_strings_p = true;
3909 /* We are not parsing a function body. */
3910 parser->in_function_body = false;
3912 /* We can correct until told otherwise. */
3913 parser->colon_corrects_to_scope_p = true;
3915 /* The unparsed function queue is empty. */
3916 push_unparsed_function_queues (parser);
3918 /* There are no classes being defined. */
3919 parser->num_classes_being_defined = 0;
3921 /* No template parameters apply. */
3922 parser->num_template_parameter_lists = 0;
3924 /* Special parsing data structures. */
3925 parser->omp_declare_simd = NULL;
3926 parser->oacc_routine = NULL;
3928 /* Not declaring an implicit function template. */
3929 parser->auto_is_implicit_function_template_parm_p = false;
3930 parser->fully_implicit_function_template_p = false;
3931 parser->implicit_template_parms = 0;
3932 parser->implicit_template_scope = 0;
3934 /* Allow constrained-type-specifiers. */
3935 parser->prevent_constrained_type_specifiers = 0;
3937 /* We haven't yet seen an 'extern "C"'. */
3938 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3943 /* Create a cp_lexer structure which will emit the tokens in CACHE
3944 and push it onto the parser's lexer stack. This is used for delayed
3945 parsing of in-class method bodies and default arguments, and should
3946 not be confused with tentative parsing. */
3948 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3950 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3951 lexer->next = parser->lexer;
3952 parser->lexer = lexer;
3954 /* Move the current source position to that of the first token in the
3956 cp_lexer_set_source_position_from_token (lexer->next_token);
3959 /* Pop the top lexer off the parser stack. This is never used for the
3960 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3962 cp_parser_pop_lexer (cp_parser *parser)
3964 cp_lexer *lexer = parser->lexer;
3965 parser->lexer = lexer->next;
3966 cp_lexer_destroy (lexer);
3968 /* Put the current source position back where it was before this
3969 lexer was pushed. */
3970 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3973 /* Lexical conventions [gram.lex] */
3975 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3979 cp_parser_identifier (cp_parser* parser)
3983 /* Look for the identifier. */
3984 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3985 /* Return the value. */
3987 return cp_expr (token->u.value, token->location);
3989 return error_mark_node;
3992 /* Parse a sequence of adjacent string constants. Returns a
3993 TREE_STRING representing the combined, nul-terminated string
3994 constant. If TRANSLATE is true, translate the string to the
3995 execution character set. If WIDE_OK is true, a wide string is
3998 C++98 [lex.string] says that if a narrow string literal token is
3999 adjacent to a wide string literal token, the behavior is undefined.
4000 However, C99 6.4.5p4 says that this results in a wide string literal.
4001 We follow C99 here, for consistency with the C front end.
4003 This code is largely lifted from lex_string() in c-lex.c.
4005 FUTURE: ObjC++ will need to handle @-strings here. */
4007 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4008 bool lookup_udlit = true)
4012 struct obstack str_ob;
4013 cpp_string str, istr, *strs;
4015 enum cpp_ttype type, curr_type;
4016 int have_suffix_p = 0;
4018 tree suffix_id = NULL_TREE;
4019 bool curr_tok_is_userdef_p = false;
4021 tok = cp_lexer_peek_token (parser->lexer);
4022 if (!cp_parser_is_string_literal (tok))
4024 cp_parser_error (parser, "expected string-literal");
4025 return error_mark_node;
4028 location_t loc = tok->location;
4030 if (cpp_userdef_string_p (tok->type))
4032 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4033 curr_type = cpp_userdef_string_remove_type (tok->type);
4034 curr_tok_is_userdef_p = true;
4038 string_tree = tok->u.value;
4039 curr_type = tok->type;
4043 /* Try to avoid the overhead of creating and destroying an obstack
4044 for the common case of just one string. */
4045 if (!cp_parser_is_string_literal
4046 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4048 cp_lexer_consume_token (parser->lexer);
4050 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4051 str.len = TREE_STRING_LENGTH (string_tree);
4054 if (curr_tok_is_userdef_p)
4056 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4058 curr_type = cpp_userdef_string_remove_type (tok->type);
4061 curr_type = tok->type;
4067 location_t last_tok_loc = tok->location;
4068 gcc_obstack_init (&str_ob);
4073 cp_lexer_consume_token (parser->lexer);
4075 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4076 str.len = TREE_STRING_LENGTH (string_tree);
4078 if (curr_tok_is_userdef_p)
4080 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4081 if (have_suffix_p == 0)
4083 suffix_id = curr_suffix_id;
4086 else if (have_suffix_p == 1
4087 && curr_suffix_id != suffix_id)
4089 error ("inconsistent user-defined literal suffixes"
4090 " %qD and %qD in string literal",
4091 suffix_id, curr_suffix_id);
4094 curr_type = cpp_userdef_string_remove_type (tok->type);
4097 curr_type = tok->type;
4099 if (type != curr_type)
4101 if (type == CPP_STRING)
4103 else if (curr_type != CPP_STRING)
4105 rich_location rich_loc (line_table, tok->location);
4106 rich_loc.add_range (last_tok_loc, false);
4107 error_at (&rich_loc,
4108 "unsupported non-standard concatenation "
4109 "of string literals");
4113 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4115 last_tok_loc = tok->location;
4117 tok = cp_lexer_peek_token (parser->lexer);
4118 if (cpp_userdef_string_p (tok->type))
4120 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4121 curr_type = cpp_userdef_string_remove_type (tok->type);
4122 curr_tok_is_userdef_p = true;
4126 string_tree = tok->u.value;
4127 curr_type = tok->type;
4128 curr_tok_is_userdef_p = false;
4131 while (cp_parser_is_string_literal (tok));
4133 /* A string literal built by concatenation has its caret=start at
4134 the start of the initial string, and its finish at the finish of
4135 the final string literal. */
4136 loc = make_location (loc, loc, get_finish (last_tok_loc));
4138 strs = (cpp_string *) obstack_finish (&str_ob);
4141 if (type != CPP_STRING && !wide_ok)
4143 cp_parser_error (parser, "a wide string is invalid in this context");
4147 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4148 (parse_in, strs, count, &istr, type))
4150 value = build_string (istr.len, (const char *)istr.text);
4151 free (CONST_CAST (unsigned char *, istr.text));
4157 case CPP_UTF8STRING:
4158 TREE_TYPE (value) = char_array_type_node;
4161 TREE_TYPE (value) = char16_array_type_node;
4164 TREE_TYPE (value) = char32_array_type_node;
4167 TREE_TYPE (value) = wchar_array_type_node;
4171 value = fix_string_type (value);
4175 tree literal = build_userdef_literal (suffix_id, value,
4176 OT_NONE, NULL_TREE);
4178 value = cp_parser_userdef_string_literal (literal);
4184 /* cpp_interpret_string has issued an error. */
4185 value = error_mark_node;
4188 obstack_free (&str_ob, 0);
4190 return cp_expr (value, loc);
4193 /* Look up a literal operator with the name and the exact arguments. */
4196 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4199 decl = lookup_name (name);
4200 if (!decl || !is_overloaded_fn (decl))
4201 return error_mark_node;
4203 for (lkp_iterator iter (decl); iter; ++iter)
4208 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4209 if (parmtypes != NULL_TREE)
4211 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4212 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4214 tree tparm = TREE_VALUE (parmtypes);
4215 tree targ = TREE_TYPE ((*args)[ix]);
4216 bool ptr = TYPE_PTR_P (tparm);
4217 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4218 if ((ptr || arr || !same_type_p (tparm, targ))
4220 || !same_type_p (TREE_TYPE (tparm),
4225 && ix == vec_safe_length (args)
4226 /* May be this should be sufficient_parms_p instead,
4227 depending on how exactly should user-defined literals
4228 work in presence of default arguments on the literal
4229 operator parameters. */
4230 && parmtypes == void_list_node)
4235 return error_mark_node;
4238 /* Parse a user-defined char constant. Returns a call to a user-defined
4239 literal operator taking the character as an argument. */
4242 cp_parser_userdef_char_literal (cp_parser *parser)
4244 cp_token *token = cp_lexer_consume_token (parser->lexer);
4245 tree literal = token->u.value;
4246 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4247 tree value = USERDEF_LITERAL_VALUE (literal);
4248 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4251 /* Build up a call to the user-defined operator */
4252 /* Lookup the name we got back from the id-expression. */
4253 vec<tree, va_gc> *args = make_tree_vector ();
4254 vec_safe_push (args, value);
4255 decl = lookup_literal_operator (name, args);
4256 if (!decl || decl == error_mark_node)
4258 error ("unable to find character literal operator %qD with %qT argument",
4259 name, TREE_TYPE (value));
4260 release_tree_vector (args);
4261 return error_mark_node;
4263 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4264 release_tree_vector (args);
4268 /* A subroutine of cp_parser_userdef_numeric_literal to
4269 create a char... template parameter pack from a string node. */
4272 make_char_string_pack (tree value)
4275 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4276 const char *str = TREE_STRING_POINTER (value);
4277 int i, len = TREE_STRING_LENGTH (value) - 1;
4278 tree argvec = make_tree_vec (1);
4280 /* Fill in CHARVEC with all of the parameters. */
4281 charvec = make_tree_vec (len);
4282 for (i = 0; i < len; ++i)
4283 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4285 /* Build the argument packs. */
4286 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4288 TREE_VEC_ELT (argvec, 0) = argpack;
4293 /* A subroutine of cp_parser_userdef_numeric_literal to
4294 create a char... template parameter pack from a string node. */
4297 make_string_pack (tree value)
4300 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4301 const unsigned char *str
4302 = (const unsigned char *) TREE_STRING_POINTER (value);
4303 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4304 int len = TREE_STRING_LENGTH (value) / sz - 1;
4305 tree argvec = make_tree_vec (2);
4307 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4308 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4310 /* First template parm is character type. */
4311 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4313 /* Fill in CHARVEC with all of the parameters. */
4314 charvec = make_tree_vec (len);
4315 for (int i = 0; i < len; ++i)
4316 TREE_VEC_ELT (charvec, i)
4317 = double_int_to_tree (str_char_type_node,
4318 double_int::from_buffer (str + i * sz, sz));
4320 /* Build the argument packs. */
4321 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4323 TREE_VEC_ELT (argvec, 1) = argpack;
4328 /* Parse a user-defined numeric constant. returns a call to a user-defined
4329 literal operator. */
4332 cp_parser_userdef_numeric_literal (cp_parser *parser)
4334 cp_token *token = cp_lexer_consume_token (parser->lexer);
4335 tree literal = token->u.value;
4336 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4337 tree value = USERDEF_LITERAL_VALUE (literal);
4338 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4339 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4340 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4342 vec<tree, va_gc> *args;
4344 /* Look for a literal operator taking the exact type of numeric argument
4345 as the literal value. */
4346 args = make_tree_vector ();
4347 vec_safe_push (args, value);
4348 decl = lookup_literal_operator (name, args);
4349 if (decl && decl != error_mark_node)
4351 result = finish_call_expr (decl, &args, false, true,
4352 tf_warning_or_error);
4354 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4356 warning_at (token->location, OPT_Woverflow,
4357 "integer literal exceeds range of %qT type",
4358 long_long_unsigned_type_node);
4363 warning_at (token->location, OPT_Woverflow,
4364 "floating literal exceeds range of %qT type",
4365 long_double_type_node);
4366 else if (overflow < 0)
4367 warning_at (token->location, OPT_Woverflow,
4368 "floating literal truncated to zero");
4371 release_tree_vector (args);
4374 release_tree_vector (args);
4376 /* If the numeric argument didn't work, look for a raw literal
4377 operator taking a const char* argument consisting of the number
4378 in string format. */
4379 args = make_tree_vector ();
4380 vec_safe_push (args, num_string);
4381 decl = lookup_literal_operator (name, args);
4382 if (decl && decl != error_mark_node)
4384 result = finish_call_expr (decl, &args, false, true,
4385 tf_warning_or_error);
4386 release_tree_vector (args);
4389 release_tree_vector (args);
4391 /* If the raw literal didn't work, look for a non-type template
4392 function with parameter pack char.... Call the function with
4393 template parameter characters representing the number. */
4394 args = make_tree_vector ();
4395 decl = lookup_literal_operator (name, args);
4396 if (decl && decl != error_mark_node)
4398 tree tmpl_args = make_char_string_pack (num_string);
4399 decl = lookup_template_function (decl, tmpl_args);
4400 result = finish_call_expr (decl, &args, false, true,
4401 tf_warning_or_error);
4402 release_tree_vector (args);
4406 release_tree_vector (args);
4408 /* In C++14 the standard library defines complex number suffixes that
4409 conflict with GNU extensions. Prefer them if <complex> is #included. */
4410 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4411 bool i14 = (cxx_dialect > cxx11
4412 && (id_equal (suffix_id, "i")
4413 || id_equal (suffix_id, "if")
4414 || id_equal (suffix_id, "il")));
4415 diagnostic_t kind = DK_ERROR;
4420 tree cxlit = lookup_qualified_name (std_node,
4421 get_identifier ("complex_literals"),
4423 if (cxlit == error_mark_node)
4425 /* No <complex>, so pedwarn and use GNU semantics. */
4427 opt = OPT_Wpedantic;
4432 = emit_diagnostic (kind, input_location, opt,
4433 "unable to find numeric literal operator %qD", name);
4436 /* Don't inform either. */;
4439 inform (token->location, "add %<using namespace std::complex_literals%> "
4440 "(from <complex>) to enable the C++14 user-defined literal "
4443 inform (token->location, "or use %<j%> instead of %<i%> for the "
4444 "GNU built-in suffix");
4447 inform (token->location, "use -fext-numeric-literals "
4448 "to enable more built-in suffixes");
4450 if (kind == DK_ERROR)
4451 value = error_mark_node;
4454 /* Use the built-in semantics. */
4456 if (id_equal (suffix_id, "i"))
4458 if (TREE_CODE (value) == INTEGER_CST)
4459 type = integer_type_node;
4461 type = double_type_node;
4463 else if (id_equal (suffix_id, "if"))
4464 type = float_type_node;
4465 else /* if (id_equal (suffix_id, "il")) */
4466 type = long_double_type_node;
4468 value = build_complex (build_complex_type (type),
4469 fold_convert (type, integer_zero_node),
4470 fold_convert (type, value));
4473 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4474 /* Avoid repeated diagnostics. */
4475 token->u.value = value;
4479 /* Parse a user-defined string constant. Returns a call to a user-defined
4480 literal operator taking a character pointer and the length of the string
4484 cp_parser_userdef_string_literal (tree literal)
4486 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4487 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4488 tree value = USERDEF_LITERAL_VALUE (literal);
4489 int len = TREE_STRING_LENGTH (value)
4490 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4492 vec<tree, va_gc> *args;
4494 /* Build up a call to the user-defined operator. */
4495 /* Lookup the name we got back from the id-expression. */
4496 args = make_tree_vector ();
4497 vec_safe_push (args, value);
4498 vec_safe_push (args, build_int_cst (size_type_node, len));
4499 decl = lookup_literal_operator (name, args);
4501 if (decl && decl != error_mark_node)
4503 result = finish_call_expr (decl, &args, false, true,
4504 tf_warning_or_error);
4505 release_tree_vector (args);
4508 release_tree_vector (args);
4510 /* Look for a template function with typename parameter CharT
4511 and parameter pack CharT... Call the function with
4512 template parameter characters representing the string. */
4513 args = make_tree_vector ();
4514 decl = lookup_literal_operator (name, args);
4515 if (decl && decl != error_mark_node)
4517 tree tmpl_args = make_string_pack (value);
4518 decl = lookup_template_function (decl, tmpl_args);
4519 result = finish_call_expr (decl, &args, false, true,
4520 tf_warning_or_error);
4521 release_tree_vector (args);
4524 release_tree_vector (args);
4526 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4527 name, TREE_TYPE (value), size_type_node);
4528 return error_mark_node;
4532 /* Basic concepts [gram.basic] */
4534 /* Parse a translation-unit.
4537 declaration-seq [opt]
4539 Returns TRUE if all went well. */
4542 cp_parser_translation_unit (cp_parser* parser)
4544 /* The address of the first non-permanent object on the declarator
4546 static void *declarator_obstack_base;
4550 /* Create the declarator obstack, if necessary. */
4551 if (!cp_error_declarator)
4553 gcc_obstack_init (&declarator_obstack);
4554 /* Create the error declarator. */
4555 cp_error_declarator = make_declarator (cdk_error);
4556 /* Create the empty parameter list. */
4557 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4559 /* Remember where the base of the declarator obstack lies. */
4560 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4563 cp_parser_declaration_seq_opt (parser);
4565 /* If there are no tokens left then all went well. */
4566 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4568 /* Get rid of the token array; we don't need it any more. */
4569 cp_lexer_destroy (parser->lexer);
4570 parser->lexer = NULL;
4572 /* This file might have been a context that's implicitly extern
4573 "C". If so, pop the lang context. (Only relevant for PCH.) */
4574 if (parser->implicit_extern_c)
4576 pop_lang_context ();
4577 parser->implicit_extern_c = false;
4581 finish_translation_unit ();
4587 cp_parser_error (parser, "expected declaration");
4591 /* Make sure the declarator obstack was fully cleaned up. */
4592 gcc_assert (obstack_next_free (&declarator_obstack)
4593 == declarator_obstack_base);
4595 /* All went well. */
4599 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4600 decltype context. */
4602 static inline tsubst_flags_t
4603 complain_flags (bool decltype_p)
4605 tsubst_flags_t complain = tf_warning_or_error;
4607 complain |= tf_decltype;
4611 /* We're about to parse a collection of statements. If we're currently
4612 parsing tentatively, set up a firewall so that any nested
4613 cp_parser_commit_to_tentative_parse won't affect the current context. */
4615 static cp_token_position
4616 cp_parser_start_tentative_firewall (cp_parser *parser)
4618 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4621 cp_parser_parse_tentatively (parser);
4622 cp_parser_commit_to_topmost_tentative_parse (parser);
4623 return cp_lexer_token_position (parser->lexer, false);
4626 /* We've finished parsing the collection of statements. Wrap up the
4627 firewall and replace the relevant tokens with the parsed form. */
4630 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4636 /* Finish the firewall level. */
4637 cp_parser_parse_definitely (parser);
4638 /* And remember the result of the parse for when we try again. */
4639 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4640 token->type = CPP_PREPARSED_EXPR;
4641 token->u.value = expr;
4642 token->keyword = RID_MAX;
4643 cp_lexer_purge_tokens_after (parser->lexer, start);
4646 /* Like the above functions, but let the user modify the tokens. Used by
4647 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4648 later parses, so it makes sense to localize the effects of
4649 cp_parser_commit_to_tentative_parse. */
4651 struct tentative_firewall
4656 tentative_firewall (cp_parser *p): parser(p)
4658 /* If we're currently parsing tentatively, start a committed level as a
4659 firewall and then an inner tentative parse. */
4660 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4662 cp_parser_parse_tentatively (parser);
4663 cp_parser_commit_to_topmost_tentative_parse (parser);
4664 cp_parser_parse_tentatively (parser);
4668 ~tentative_firewall()
4672 /* Finish the inner tentative parse and the firewall, propagating any
4673 uncommitted error state to the outer tentative parse. */
4674 bool err = cp_parser_error_occurred (parser);
4675 cp_parser_parse_definitely (parser);
4676 cp_parser_parse_definitely (parser);
4678 cp_parser_simulate_error (parser);
4683 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4684 This class is for tracking such a matching pair of symbols.
4685 In particular, it tracks the location of the first token,
4686 so that if the second token is missing, we can highlight the
4687 location of the first token when notifying the user about the
4690 template <typename traits_t>
4694 /* token_pair's ctor. */
4695 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4697 /* If the next token is the opening symbol for this pair, consume it and
4699 Otherwise, issue an error and return false.
4700 In either case, record the location of the opening token. */
4702 bool require_open (cp_parser *parser)
4704 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4705 return cp_parser_require (parser, traits_t::open_token_type,
4706 traits_t::required_token_open);
4709 /* Consume the next token from PARSER, recording its location as
4710 that of the opening token within the pair. */
4712 cp_token * consume_open (cp_parser *parser)
4714 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4715 gcc_assert (tok->type == traits_t::open_token_type);
4716 m_open_loc = tok->location;
4720 /* If the next token is the closing symbol for this pair, consume it
4722 Otherwise, issue an error, highlighting the location of the
4723 corresponding opening token, and return NULL. */
4725 cp_token *require_close (cp_parser *parser) const
4727 return cp_parser_require (parser, traits_t::close_token_type,
4728 traits_t::required_token_close,
4733 location_t m_open_loc;
4736 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4738 struct matching_paren_traits
4740 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4741 static const enum required_token required_token_open = RT_OPEN_PAREN;
4742 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4743 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4746 /* "matching_parens" is a token_pair<T> class for tracking matching
4747 pairs of parentheses. */
4749 typedef token_pair<matching_paren_traits> matching_parens;
4751 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4753 struct matching_brace_traits
4755 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4756 static const enum required_token required_token_open = RT_OPEN_BRACE;
4757 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4758 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4761 /* "matching_braces" is a token_pair<T> class for tracking matching
4764 typedef token_pair<matching_brace_traits> matching_braces;
4767 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4768 enclosing parentheses. */
4771 cp_parser_statement_expr (cp_parser *parser)
4773 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4775 /* Consume the '('. */
4776 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4777 matching_parens parens;
4778 parens.consume_open (parser);
4779 /* Start the statement-expression. */
4780 tree expr = begin_stmt_expr ();
4781 /* Parse the compound-statement. */
4782 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4784 expr = finish_stmt_expr (expr, false);
4785 /* Consume the ')'. */
4786 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4787 if (!parens.require_close (parser))
4788 cp_parser_skip_to_end_of_statement (parser);
4790 cp_parser_end_tentative_firewall (parser, start, expr);
4791 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4792 return cp_expr (expr, combined_loc);
4795 /* Expressions [gram.expr] */
4797 /* Parse a fold-operator.
4800 - * / % ^ & | = < > << >>
4801 = -= *= /= %= ^= &= |= <<= >>=
4802 == != <= >= && || , .* ->*
4804 This returns the tree code corresponding to the matched operator
4805 as an int. When the current token matches a compound assignment
4806 opertor, the resulting tree code is the negative value of the
4807 non-assignment operator. */
4810 cp_parser_fold_operator (cp_token *token)
4812 switch (token->type)
4814 case CPP_PLUS: return PLUS_EXPR;
4815 case CPP_MINUS: return MINUS_EXPR;
4816 case CPP_MULT: return MULT_EXPR;
4817 case CPP_DIV: return TRUNC_DIV_EXPR;
4818 case CPP_MOD: return TRUNC_MOD_EXPR;
4819 case CPP_XOR: return BIT_XOR_EXPR;
4820 case CPP_AND: return BIT_AND_EXPR;
4821 case CPP_OR: return BIT_IOR_EXPR;
4822 case CPP_LSHIFT: return LSHIFT_EXPR;
4823 case CPP_RSHIFT: return RSHIFT_EXPR;
4825 case CPP_EQ: return -NOP_EXPR;
4826 case CPP_PLUS_EQ: return -PLUS_EXPR;
4827 case CPP_MINUS_EQ: return -MINUS_EXPR;
4828 case CPP_MULT_EQ: return -MULT_EXPR;
4829 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4830 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4831 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4832 case CPP_AND_EQ: return -BIT_AND_EXPR;
4833 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4834 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4835 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4837 case CPP_EQ_EQ: return EQ_EXPR;
4838 case CPP_NOT_EQ: return NE_EXPR;
4839 case CPP_LESS: return LT_EXPR;
4840 case CPP_GREATER: return GT_EXPR;
4841 case CPP_LESS_EQ: return LE_EXPR;
4842 case CPP_GREATER_EQ: return GE_EXPR;
4844 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4845 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4847 case CPP_COMMA: return COMPOUND_EXPR;
4849 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4850 case CPP_DEREF_STAR: return MEMBER_REF;
4852 default: return ERROR_MARK;
4856 /* Returns true if CODE indicates a binary expression, which is not allowed in
4857 the LHS of a fold-expression. More codes will need to be added to use this
4858 function in other contexts. */
4861 is_binary_op (tree_code code)
4866 case POINTER_PLUS_EXPR:
4869 case TRUNC_DIV_EXPR:
4870 case TRUNC_MOD_EXPR:
4886 case TRUTH_ANDIF_EXPR:
4887 case TRUTH_ORIF_EXPR:
4900 /* If the next token is a suitable fold operator, consume it and return as
4901 the function above. */
4904 cp_parser_fold_operator (cp_parser *parser)
4906 cp_token* token = cp_lexer_peek_token (parser->lexer);
4907 int code = cp_parser_fold_operator (token);
4908 if (code != ERROR_MARK)
4909 cp_lexer_consume_token (parser->lexer);
4913 /* Parse a fold-expression.
4916 ( ... folding-operator cast-expression)
4917 ( cast-expression folding-operator ... )
4918 ( cast-expression folding operator ... folding-operator cast-expression)
4920 Note that the '(' and ')' are matched in primary expression. */
4923 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4928 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4930 cp_lexer_consume_token (parser->lexer);
4931 int op = cp_parser_fold_operator (parser);
4932 if (op == ERROR_MARK)
4934 cp_parser_error (parser, "expected binary operator");
4935 return error_mark_node;
4938 tree expr = cp_parser_cast_expression (parser, false, false,
4940 if (expr == error_mark_node)
4941 return error_mark_node;
4942 return finish_left_unary_fold_expr (expr, op);
4945 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4946 int op = cp_parser_fold_operator (parser);
4947 if (op == ERROR_MARK)
4949 cp_parser_error (parser, "expected binary operator");
4950 return error_mark_node;
4953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4955 cp_parser_error (parser, "expected ...");
4956 return error_mark_node;
4958 cp_lexer_consume_token (parser->lexer);
4960 /* The operands of a fold-expression are cast-expressions, so binary or
4961 conditional expressions are not allowed. We check this here to avoid
4962 tentative parsing. */
4963 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4964 /* OK, the expression was parenthesized. */;
4965 else if (is_binary_op (TREE_CODE (expr1)))
4966 error_at (location_of (expr1),
4967 "binary expression in operand of fold-expression");
4968 else if (TREE_CODE (expr1) == COND_EXPR
4969 || (REFERENCE_REF_P (expr1)
4970 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
4971 error_at (location_of (expr1),
4972 "conditional expression in operand of fold-expression");
4975 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4976 return finish_right_unary_fold_expr (expr1, op);
4978 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4980 cp_parser_error (parser, "mismatched operator in fold-expression");
4981 return error_mark_node;
4983 cp_lexer_consume_token (parser->lexer);
4985 // Binary left or right fold.
4986 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4987 if (expr2 == error_mark_node)
4988 return error_mark_node;
4989 return finish_binary_fold_expr (expr1, expr2, op);
4992 /* Parse a primary-expression.
4999 lambda-expression (C++11)
5004 ( compound-statement )
5005 __builtin_va_arg ( assignment-expression , type-id )
5006 __builtin_offsetof ( type-id , offsetof-expression )
5009 __has_nothrow_assign ( type-id )
5010 __has_nothrow_constructor ( type-id )
5011 __has_nothrow_copy ( type-id )
5012 __has_trivial_assign ( type-id )
5013 __has_trivial_constructor ( type-id )
5014 __has_trivial_copy ( type-id )
5015 __has_trivial_destructor ( type-id )
5016 __has_virtual_destructor ( type-id )
5017 __is_abstract ( type-id )
5018 __is_base_of ( type-id , type-id )
5019 __is_class ( type-id )
5020 __is_empty ( type-id )
5021 __is_enum ( type-id )
5022 __is_final ( type-id )
5023 __is_literal_type ( type-id )
5024 __is_pod ( type-id )
5025 __is_polymorphic ( type-id )
5026 __is_std_layout ( type-id )
5027 __is_trivial ( type-id )
5028 __is_union ( type-id )
5030 Objective-C++ Extension:
5038 ADDRESS_P is true iff this expression was immediately preceded by
5039 "&" and therefore might denote a pointer-to-member. CAST_P is true
5040 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5041 true iff this expression is a template argument.
5043 Returns a representation of the expression. Upon return, *IDK
5044 indicates what kind of id-expression (if any) was present. */
5047 cp_parser_primary_expression (cp_parser *parser,
5050 bool template_arg_p,
5054 cp_token *token = NULL;
5056 /* Assume the primary expression is not an id-expression. */
5057 *idk = CP_ID_KIND_NONE;
5059 /* Peek at the next token. */
5060 token = cp_lexer_peek_token (parser->lexer);
5061 switch ((int) token->type)
5070 user-defined-literal */
5077 case CPP_PREPARSED_EXPR:
5078 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5079 return cp_parser_userdef_numeric_literal (parser);
5080 token = cp_lexer_consume_token (parser->lexer);
5081 if (TREE_CODE (token->u.value) == FIXED_CST)
5083 error_at (token->location,
5084 "fixed-point types not supported in C++");
5085 return error_mark_node;
5087 /* Floating-point literals are only allowed in an integral
5088 constant expression if they are cast to an integral or
5089 enumeration type. */
5090 if (TREE_CODE (token->u.value) == REAL_CST
5091 && parser->integral_constant_expression_p
5094 /* CAST_P will be set even in invalid code like "int(2.7 +
5095 ...)". Therefore, we have to check that the next token
5096 is sure to end the cast. */
5099 cp_token *next_token;
5101 next_token = cp_lexer_peek_token (parser->lexer);
5102 if (/* The comma at the end of an
5103 enumerator-definition. */
5104 next_token->type != CPP_COMMA
5105 /* The curly brace at the end of an enum-specifier. */
5106 && next_token->type != CPP_CLOSE_BRACE
5107 /* The end of a statement. */
5108 && next_token->type != CPP_SEMICOLON
5109 /* The end of the cast-expression. */
5110 && next_token->type != CPP_CLOSE_PAREN
5111 /* The end of an array bound. */
5112 && next_token->type != CPP_CLOSE_SQUARE
5113 /* The closing ">" in a template-argument-list. */
5114 && (next_token->type != CPP_GREATER
5115 || parser->greater_than_is_operator_p)
5116 /* C++0x only: A ">>" treated like two ">" tokens,
5117 in a template-argument-list. */
5118 && (next_token->type != CPP_RSHIFT
5119 || (cxx_dialect == cxx98)
5120 || parser->greater_than_is_operator_p))
5124 /* If we are within a cast, then the constraint that the
5125 cast is to an integral or enumeration type will be
5126 checked at that point. If we are not within a cast, then
5127 this code is invalid. */
5129 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5131 return cp_expr (token->u.value, token->location);
5133 case CPP_CHAR_USERDEF:
5134 case CPP_CHAR16_USERDEF:
5135 case CPP_CHAR32_USERDEF:
5136 case CPP_WCHAR_USERDEF:
5137 case CPP_UTF8CHAR_USERDEF:
5138 return cp_parser_userdef_char_literal (parser);
5144 case CPP_UTF8STRING:
5145 case CPP_STRING_USERDEF:
5146 case CPP_STRING16_USERDEF:
5147 case CPP_STRING32_USERDEF:
5148 case CPP_WSTRING_USERDEF:
5149 case CPP_UTF8STRING_USERDEF:
5150 /* ??? Should wide strings be allowed when parser->translate_strings_p
5151 is false (i.e. in attributes)? If not, we can kill the third
5152 argument to cp_parser_string_literal. */
5153 return cp_parser_string_literal (parser,
5154 parser->translate_strings_p,
5157 case CPP_OPEN_PAREN:
5158 /* If we see `( { ' then we are looking at the beginning of
5159 a GNU statement-expression. */
5160 if (cp_parser_allow_gnu_extensions_p (parser)
5161 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5163 /* Statement-expressions are not allowed by the standard. */
5164 pedwarn (token->location, OPT_Wpedantic,
5165 "ISO C++ forbids braced-groups within expressions");
5167 /* And they're not allowed outside of a function-body; you
5168 cannot, for example, write:
5170 int i = ({ int j = 3; j + 1; });
5172 at class or namespace scope. */
5173 if (!parser->in_function_body
5174 || parser->in_template_argument_list_p)
5176 error_at (token->location,
5177 "statement-expressions are not allowed outside "
5178 "functions nor in template-argument lists");
5179 cp_parser_skip_to_end_of_block_or_statement (parser);
5180 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5181 cp_lexer_consume_token (parser->lexer);
5182 return error_mark_node;
5185 return cp_parser_statement_expr (parser);
5187 /* Otherwise it's a normal parenthesized expression. */
5190 bool saved_greater_than_is_operator_p;
5192 location_t open_paren_loc = token->location;
5194 /* Consume the `('. */
5195 matching_parens parens;
5196 parens.consume_open (parser);
5197 /* Within a parenthesized expression, a `>' token is always
5198 the greater-than operator. */
5199 saved_greater_than_is_operator_p
5200 = parser->greater_than_is_operator_p;
5201 parser->greater_than_is_operator_p = true;
5203 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5204 /* Left fold expression. */
5207 /* Parse the parenthesized expression. */
5208 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5210 token = cp_lexer_peek_token (parser->lexer);
5211 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5213 expr = cp_parser_fold_expression (parser, expr);
5214 if (expr != error_mark_node
5215 && cxx_dialect < cxx17
5216 && !in_system_header_at (input_location))
5217 pedwarn (input_location, 0, "fold-expressions only available "
5218 "with -std=c++17 or -std=gnu++17");
5221 /* Let the front end know that this expression was
5222 enclosed in parentheses. This matters in case, for
5223 example, the expression is of the form `A::B', since
5224 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5226 expr = finish_parenthesized_expr (expr);
5228 /* DR 705: Wrapping an unqualified name in parentheses
5229 suppresses arg-dependent lookup. We want to pass back
5230 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5231 (c++/37862), but none of the others. */
5232 if (*idk != CP_ID_KIND_QUALIFIED)
5233 *idk = CP_ID_KIND_NONE;
5235 /* The `>' token might be the end of a template-id or
5236 template-parameter-list now. */
5237 parser->greater_than_is_operator_p
5238 = saved_greater_than_is_operator_p;
5240 /* Consume the `)'. */
5241 token = cp_lexer_peek_token (parser->lexer);
5242 location_t close_paren_loc = token->location;
5243 expr.set_range (open_paren_loc, close_paren_loc);
5244 if (!parens.require_close (parser)
5245 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5246 cp_parser_skip_to_end_of_statement (parser);
5251 case CPP_OPEN_SQUARE:
5253 if (c_dialect_objc ())
5255 /* We might have an Objective-C++ message. */
5256 cp_parser_parse_tentatively (parser);
5257 tree msg = cp_parser_objc_message_expression (parser);
5258 /* If that works out, we're done ... */
5259 if (cp_parser_parse_definitely (parser))
5261 /* ... else, fall though to see if it's a lambda. */
5263 cp_expr lam = cp_parser_lambda_expression (parser);
5264 /* Don't warn about a failed tentative parse. */
5265 if (cp_parser_error_occurred (parser))
5266 return error_mark_node;
5267 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5271 case CPP_OBJC_STRING:
5272 if (c_dialect_objc ())
5273 /* We have an Objective-C++ string literal. */
5274 return cp_parser_objc_expression (parser);
5275 cp_parser_error (parser, "expected primary-expression");
5276 return error_mark_node;
5279 switch (token->keyword)
5281 /* These two are the boolean literals. */
5283 cp_lexer_consume_token (parser->lexer);
5284 return cp_expr (boolean_true_node, token->location);
5286 cp_lexer_consume_token (parser->lexer);
5287 return cp_expr (boolean_false_node, token->location);
5289 /* The `__null' literal. */
5291 cp_lexer_consume_token (parser->lexer);
5292 return cp_expr (null_node, token->location);
5294 /* The `nullptr' literal. */
5296 cp_lexer_consume_token (parser->lexer);
5297 return cp_expr (nullptr_node, token->location);
5299 /* Recognize the `this' keyword. */
5301 cp_lexer_consume_token (parser->lexer);
5302 if (parser->local_variables_forbidden_p)
5304 error_at (token->location,
5305 "%<this%> may not be used in this context");
5306 return error_mark_node;
5308 /* Pointers cannot appear in constant-expressions. */
5309 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5310 return error_mark_node;
5311 return cp_expr (finish_this_expr (), token->location);
5313 /* The `operator' keyword can be the beginning of an
5318 case RID_FUNCTION_NAME:
5319 case RID_PRETTY_FUNCTION_NAME:
5320 case RID_C99_FUNCTION_NAME:
5322 non_integral_constant name;
5324 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5325 __func__ are the names of variables -- but they are
5326 treated specially. Therefore, they are handled here,
5327 rather than relying on the generic id-expression logic
5328 below. Grammatically, these names are id-expressions.
5330 Consume the token. */
5331 token = cp_lexer_consume_token (parser->lexer);
5333 switch (token->keyword)
5335 case RID_FUNCTION_NAME:
5336 name = NIC_FUNC_NAME;
5338 case RID_PRETTY_FUNCTION_NAME:
5339 name = NIC_PRETTY_FUNC;
5341 case RID_C99_FUNCTION_NAME:
5342 name = NIC_C99_FUNC;
5348 if (cp_parser_non_integral_constant_expression (parser, name))
5349 return error_mark_node;
5351 /* Look up the name. */
5352 return finish_fname (token->u.value);
5359 source_location type_location;
5360 location_t start_loc
5361 = cp_lexer_peek_token (parser->lexer)->location;
5362 /* The `__builtin_va_arg' construct is used to handle
5363 `va_arg'. Consume the `__builtin_va_arg' token. */
5364 cp_lexer_consume_token (parser->lexer);
5365 /* Look for the opening `('. */
5366 matching_parens parens;
5367 parens.require_open (parser);
5368 /* Now, parse the assignment-expression. */
5369 expression = cp_parser_assignment_expression (parser);
5370 /* Look for the `,'. */
5371 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5372 type_location = cp_lexer_peek_token (parser->lexer)->location;
5373 /* Parse the type-id. */
5375 type_id_in_expr_sentinel s (parser);
5376 type = cp_parser_type_id (parser);
5378 /* Look for the closing `)'. */
5379 location_t finish_loc
5380 = cp_lexer_peek_token (parser->lexer)->location;
5381 parens.require_close (parser);
5382 /* Using `va_arg' in a constant-expression is not
5384 if (cp_parser_non_integral_constant_expression (parser,
5386 return error_mark_node;
5387 /* Construct a location of the form:
5388 __builtin_va_arg (v, int)
5389 ~~~~~~~~~~~~~~~~~~~~~^~~~
5390 with the caret at the type, ranging from the start of the
5391 "__builtin_va_arg" token to the close paren. */
5392 location_t combined_loc
5393 = make_location (type_location, start_loc, finish_loc);
5394 return build_x_va_arg (combined_loc, expression, type);
5398 return cp_parser_builtin_offsetof (parser);
5400 case RID_HAS_NOTHROW_ASSIGN:
5401 case RID_HAS_NOTHROW_CONSTRUCTOR:
5402 case RID_HAS_NOTHROW_COPY:
5403 case RID_HAS_TRIVIAL_ASSIGN:
5404 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5405 case RID_HAS_TRIVIAL_COPY:
5406 case RID_HAS_TRIVIAL_DESTRUCTOR:
5407 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5408 case RID_HAS_VIRTUAL_DESTRUCTOR:
5409 case RID_IS_ABSTRACT:
5410 case RID_IS_AGGREGATE:
5411 case RID_IS_BASE_OF:
5416 case RID_IS_LITERAL_TYPE:
5418 case RID_IS_POLYMORPHIC:
5419 case RID_IS_SAME_AS:
5420 case RID_IS_STD_LAYOUT:
5421 case RID_IS_TRIVIAL:
5422 case RID_IS_TRIVIALLY_ASSIGNABLE:
5423 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5424 case RID_IS_TRIVIALLY_COPYABLE:
5426 case RID_IS_ASSIGNABLE:
5427 case RID_IS_CONSTRUCTIBLE:
5428 return cp_parser_trait_expr (parser, token->keyword);
5432 return cp_parser_requires_expression (parser);
5434 /* Objective-C++ expressions. */
5436 case RID_AT_PROTOCOL:
5437 case RID_AT_SELECTOR:
5438 return cp_parser_objc_expression (parser);
5441 if (parser->in_function_body
5442 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5445 error_at (token->location,
5446 "a template declaration cannot appear at block scope");
5447 cp_parser_skip_to_end_of_block_or_statement (parser);
5448 return error_mark_node;
5452 cp_parser_error (parser, "expected primary-expression");
5453 return error_mark_node;
5456 /* An id-expression can start with either an identifier, a
5457 `::' as the beginning of a qualified-id, or the "operator"
5461 case CPP_TEMPLATE_ID:
5462 case CPP_NESTED_NAME_SPECIFIER:
5465 cp_expr id_expression;
5467 const char *error_msg;
5470 cp_token *id_expr_token;
5472 /* Parse the id-expression. */
5474 = cp_parser_id_expression (parser,
5475 /*template_keyword_p=*/false,
5476 /*check_dependency_p=*/true,
5478 /*declarator_p=*/false,
5479 /*optional_p=*/false);
5480 if (id_expression == error_mark_node)
5481 return error_mark_node;
5482 id_expr_token = token;
5483 token = cp_lexer_peek_token (parser->lexer);
5484 done = (token->type != CPP_OPEN_SQUARE
5485 && token->type != CPP_OPEN_PAREN
5486 && token->type != CPP_DOT
5487 && token->type != CPP_DEREF
5488 && token->type != CPP_PLUS_PLUS
5489 && token->type != CPP_MINUS_MINUS);
5490 /* If we have a template-id, then no further lookup is
5491 required. If the template-id was for a template-class, we
5492 will sometimes have a TYPE_DECL at this point. */
5493 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5494 || TREE_CODE (id_expression) == TYPE_DECL)
5495 decl = id_expression;
5496 /* Look up the name. */
5499 tree ambiguous_decls;
5501 /* If we already know that this lookup is ambiguous, then
5502 we've already issued an error message; there's no reason
5504 if (id_expr_token->type == CPP_NAME
5505 && id_expr_token->error_reported)
5507 cp_parser_simulate_error (parser);
5508 return error_mark_node;
5511 decl = cp_parser_lookup_name (parser, id_expression,
5514 /*is_namespace=*/false,
5515 /*check_dependency=*/true,
5517 id_expr_token->location);
5518 /* If the lookup was ambiguous, an error will already have
5520 if (ambiguous_decls)
5521 return error_mark_node;
5523 /* In Objective-C++, we may have an Objective-C 2.0
5524 dot-syntax for classes here. */
5525 if (c_dialect_objc ()
5526 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5527 && TREE_CODE (decl) == TYPE_DECL
5528 && objc_is_class_name (decl))
5531 cp_lexer_consume_token (parser->lexer);
5532 component = cp_parser_identifier (parser);
5533 if (component == error_mark_node)
5534 return error_mark_node;
5536 tree result = objc_build_class_component_ref (id_expression,
5538 /* Build a location of the form:
5541 with caret at the start of the component name (at
5542 input_location), ranging from the start of the id_expression
5543 to the end of the component name. */
5544 location_t combined_loc
5545 = make_location (input_location, id_expression.get_start (),
5546 get_finish (input_location));
5547 protected_set_expr_location (result, combined_loc);
5551 /* In Objective-C++, an instance variable (ivar) may be preferred
5552 to whatever cp_parser_lookup_name() found.
5553 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5554 rest of c-family, we have to do a little extra work to preserve
5555 any location information in cp_expr "decl". Given that
5556 objc_lookup_ivar is implemented in "c-family" and "objc", we
5557 have a trip through the pure "tree" type, rather than cp_expr.
5558 Naively copying it back to "decl" would implicitly give the
5559 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5560 store an EXPR_LOCATION. Hence we only update "decl" (and
5561 hence its location_t) if we get back a different tree node. */
5562 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5564 if (decl_tree != decl.get_value ())
5565 decl = cp_expr (decl_tree);
5567 /* If name lookup gives us a SCOPE_REF, then the
5568 qualifying scope was dependent. */
5569 if (TREE_CODE (decl) == SCOPE_REF)
5571 /* At this point, we do not know if DECL is a valid
5572 integral constant expression. We assume that it is
5573 in fact such an expression, so that code like:
5575 template <int N> struct A {
5579 is accepted. At template-instantiation time, we
5580 will check that B<N>::i is actually a constant. */
5583 /* Check to see if DECL is a local variable in a context
5584 where that is forbidden. */
5585 if (parser->local_variables_forbidden_p
5586 && local_variable_p (decl))
5588 /* It might be that we only found DECL because we are
5589 trying to be generous with pre-ISO scoping rules.
5590 For example, consider:
5594 for (int i = 0; i < 10; ++i) {}
5595 extern void f(int j = i);
5598 Here, name look up will originally find the out
5599 of scope `i'. We need to issue a warning message,
5600 but then use the global `i'. */
5601 decl = check_for_out_of_scope_variable (decl);
5602 if (local_variable_p (decl))
5604 error_at (id_expr_token->location,
5605 "local variable %qD may not appear in this context",
5607 return error_mark_node;
5612 if (processing_template_decl && is_overloaded_fn (decl))
5613 lookup_keep (get_fns (decl), true);
5615 decl = (finish_id_expression
5616 (id_expression, decl, parser->scope,
5618 parser->integral_constant_expression_p,
5619 parser->allow_non_integral_constant_expression_p,
5620 &parser->non_integral_constant_expression_p,
5621 template_p, done, address_p,
5624 id_expression.get_location ()));
5626 cp_parser_error (parser, error_msg);
5627 decl.set_location (id_expr_token->location);
5631 /* Anything else is an error. */
5633 cp_parser_error (parser, "expected primary-expression");
5634 return error_mark_node;
5638 static inline cp_expr
5639 cp_parser_primary_expression (cp_parser *parser,
5642 bool template_arg_p,
5645 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5646 /*decltype*/false, idk);
5649 /* Parse an id-expression.
5656 :: [opt] nested-name-specifier template [opt] unqualified-id
5658 :: operator-function-id
5661 Return a representation of the unqualified portion of the
5662 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5663 a `::' or nested-name-specifier.
5665 Often, if the id-expression was a qualified-id, the caller will
5666 want to make a SCOPE_REF to represent the qualified-id. This
5667 function does not do this in order to avoid wastefully creating
5668 SCOPE_REFs when they are not required.
5670 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5673 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5674 uninstantiated templates.
5676 If *TEMPLATE_P is non-NULL, it is set to true iff the
5677 `template' keyword is used to explicitly indicate that the entity
5678 named is a template.
5680 If DECLARATOR_P is true, the id-expression is appearing as part of
5681 a declarator, rather than as part of an expression. */
5684 cp_parser_id_expression (cp_parser *parser,
5685 bool template_keyword_p,
5686 bool check_dependency_p,
5691 bool global_scope_p;
5692 bool nested_name_specifier_p;
5694 /* Assume the `template' keyword was not used. */
5696 *template_p = template_keyword_p;
5698 /* Look for the optional `::' operator. */
5700 = (!template_keyword_p
5701 && (cp_parser_global_scope_opt (parser,
5702 /*current_scope_valid_p=*/false)
5705 /* Look for the optional nested-name-specifier. */
5706 nested_name_specifier_p
5707 = (cp_parser_nested_name_specifier_opt (parser,
5708 /*typename_keyword_p=*/false,
5715 /* If there is a nested-name-specifier, then we are looking at
5716 the first qualified-id production. */
5717 if (nested_name_specifier_p)
5720 tree saved_object_scope;
5721 tree saved_qualifying_scope;
5722 cp_expr unqualified_id;
5725 /* See if the next token is the `template' keyword. */
5727 template_p = &is_template;
5728 *template_p = cp_parser_optional_template_keyword (parser);
5729 /* Name lookup we do during the processing of the
5730 unqualified-id might obliterate SCOPE. */
5731 saved_scope = parser->scope;
5732 saved_object_scope = parser->object_scope;
5733 saved_qualifying_scope = parser->qualifying_scope;
5734 /* Process the final unqualified-id. */
5735 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5738 /*optional_p=*/false);
5739 /* Restore the SAVED_SCOPE for our caller. */
5740 parser->scope = saved_scope;
5741 parser->object_scope = saved_object_scope;
5742 parser->qualifying_scope = saved_qualifying_scope;
5744 return unqualified_id;
5746 /* Otherwise, if we are in global scope, then we are looking at one
5747 of the other qualified-id productions. */
5748 else if (global_scope_p)
5753 /* Peek at the next token. */
5754 token = cp_lexer_peek_token (parser->lexer);
5756 /* If it's an identifier, and the next token is not a "<", then
5757 we can avoid the template-id case. This is an optimization
5758 for this common case. */
5759 if (token->type == CPP_NAME
5760 && !cp_parser_nth_token_starts_template_argument_list_p
5762 return cp_parser_identifier (parser);
5764 cp_parser_parse_tentatively (parser);
5765 /* Try a template-id. */
5766 id = cp_parser_template_id (parser,
5767 /*template_keyword_p=*/false,
5768 /*check_dependency_p=*/true,
5771 /* If that worked, we're done. */
5772 if (cp_parser_parse_definitely (parser))
5775 /* Peek at the next token. (Changes in the token buffer may
5776 have invalidated the pointer obtained above.) */
5777 token = cp_lexer_peek_token (parser->lexer);
5779 switch (token->type)
5782 return cp_parser_identifier (parser);
5785 if (token->keyword == RID_OPERATOR)
5786 return cp_parser_operator_function_id (parser);
5790 cp_parser_error (parser, "expected id-expression");
5791 return error_mark_node;
5795 return cp_parser_unqualified_id (parser, template_keyword_p,
5796 /*check_dependency_p=*/true,
5801 /* Parse an unqualified-id.
5805 operator-function-id
5806 conversion-function-id
5810 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5811 keyword, in a construct like `A::template ...'.
5813 Returns a representation of unqualified-id. For the `identifier'
5814 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5815 production a BIT_NOT_EXPR is returned; the operand of the
5816 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5817 other productions, see the documentation accompanying the
5818 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5819 names are looked up in uninstantiated templates. If DECLARATOR_P
5820 is true, the unqualified-id is appearing as part of a declarator,
5821 rather than as part of an expression. */
5824 cp_parser_unqualified_id (cp_parser* parser,
5825 bool template_keyword_p,
5826 bool check_dependency_p,
5832 /* Peek at the next token. */
5833 token = cp_lexer_peek_token (parser->lexer);
5835 switch ((int) token->type)
5841 /* We don't know yet whether or not this will be a
5843 cp_parser_parse_tentatively (parser);
5844 /* Try a template-id. */
5845 id = cp_parser_template_id (parser, template_keyword_p,
5849 /* If it worked, we're done. */
5850 if (cp_parser_parse_definitely (parser))
5852 /* Otherwise, it's an ordinary identifier. */
5853 return cp_parser_identifier (parser);
5856 case CPP_TEMPLATE_ID:
5857 return cp_parser_template_id (parser, template_keyword_p,
5865 tree qualifying_scope;
5870 /* Consume the `~' token. */
5871 cp_lexer_consume_token (parser->lexer);
5872 /* Parse the class-name. The standard, as written, seems to
5875 template <typename T> struct S { ~S (); };
5876 template <typename T> S<T>::~S() {}
5878 is invalid, since `~' must be followed by a class-name, but
5879 `S<T>' is dependent, and so not known to be a class.
5880 That's not right; we need to look in uninstantiated
5881 templates. A further complication arises from:
5883 template <typename T> void f(T t) {
5887 Here, it is not possible to look up `T' in the scope of `T'
5888 itself. We must look in both the current scope, and the
5889 scope of the containing complete expression.
5891 Yet another issue is:
5900 The standard does not seem to say that the `S' in `~S'
5901 should refer to the type `S' and not the data member
5904 /* DR 244 says that we look up the name after the "~" in the
5905 same scope as we looked up the qualifying name. That idea
5906 isn't fully worked out; it's more complicated than that. */
5907 scope = parser->scope;
5908 object_scope = parser->object_scope;
5909 qualifying_scope = parser->qualifying_scope;
5911 /* Check for invalid scopes. */
5912 if (scope == error_mark_node)
5914 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5915 cp_lexer_consume_token (parser->lexer);
5916 return error_mark_node;
5918 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5920 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5921 error_at (token->location,
5922 "scope %qT before %<~%> is not a class-name",
5924 cp_parser_simulate_error (parser);
5925 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5926 cp_lexer_consume_token (parser->lexer);
5927 return error_mark_node;
5929 gcc_assert (!scope || TYPE_P (scope));
5931 /* If the name is of the form "X::~X" it's OK even if X is a
5933 token = cp_lexer_peek_token (parser->lexer);
5935 && token->type == CPP_NAME
5936 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5938 && (token->u.value == TYPE_IDENTIFIER (scope)
5939 || (CLASS_TYPE_P (scope)
5940 && constructor_name_p (token->u.value, scope))))
5942 cp_lexer_consume_token (parser->lexer);
5943 return build_nt (BIT_NOT_EXPR, scope);
5946 /* ~auto means the destructor of whatever the object is. */
5947 if (cp_parser_is_keyword (token, RID_AUTO))
5949 if (cxx_dialect < cxx14)
5950 pedwarn (input_location, 0,
5951 "%<~auto%> only available with "
5952 "-std=c++14 or -std=gnu++14");
5953 cp_lexer_consume_token (parser->lexer);
5954 return build_nt (BIT_NOT_EXPR, make_auto ());
5957 /* If there was an explicit qualification (S::~T), first look
5958 in the scope given by the qualification (i.e., S).
5960 Note: in the calls to cp_parser_class_name below we pass
5961 typename_type so that lookup finds the injected-class-name
5962 rather than the constructor. */
5964 type_decl = NULL_TREE;
5967 cp_parser_parse_tentatively (parser);
5968 type_decl = cp_parser_class_name (parser,
5969 /*typename_keyword_p=*/false,
5970 /*template_keyword_p=*/false,
5972 /*check_dependency=*/false,
5973 /*class_head_p=*/false,
5975 if (cp_parser_parse_definitely (parser))
5978 /* In "N::S::~S", look in "N" as well. */
5979 if (!done && scope && qualifying_scope)
5981 cp_parser_parse_tentatively (parser);
5982 parser->scope = qualifying_scope;
5983 parser->object_scope = NULL_TREE;
5984 parser->qualifying_scope = NULL_TREE;
5986 = cp_parser_class_name (parser,
5987 /*typename_keyword_p=*/false,
5988 /*template_keyword_p=*/false,
5990 /*check_dependency=*/false,
5991 /*class_head_p=*/false,
5993 if (cp_parser_parse_definitely (parser))
5996 /* In "p->S::~T", look in the scope given by "*p" as well. */
5997 else if (!done && object_scope)
5999 cp_parser_parse_tentatively (parser);
6000 parser->scope = object_scope;
6001 parser->object_scope = NULL_TREE;
6002 parser->qualifying_scope = NULL_TREE;
6004 = cp_parser_class_name (parser,
6005 /*typename_keyword_p=*/false,
6006 /*template_keyword_p=*/false,
6008 /*check_dependency=*/false,
6009 /*class_head_p=*/false,
6011 if (cp_parser_parse_definitely (parser))
6014 /* Look in the surrounding context. */
6017 parser->scope = NULL_TREE;
6018 parser->object_scope = NULL_TREE;
6019 parser->qualifying_scope = NULL_TREE;
6020 if (processing_template_decl)
6021 cp_parser_parse_tentatively (parser);
6023 = cp_parser_class_name (parser,
6024 /*typename_keyword_p=*/false,
6025 /*template_keyword_p=*/false,
6027 /*check_dependency=*/false,
6028 /*class_head_p=*/false,
6030 if (processing_template_decl
6031 && ! cp_parser_parse_definitely (parser))
6033 /* We couldn't find a type with this name. If we're parsing
6034 tentatively, fail and try something else. */
6035 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6037 cp_parser_simulate_error (parser);
6038 return error_mark_node;
6040 /* Otherwise, accept it and check for a match at instantiation
6042 type_decl = cp_parser_identifier (parser);
6043 if (type_decl != error_mark_node)
6044 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6048 /* If an error occurred, assume that the name of the
6049 destructor is the same as the name of the qualifying
6050 class. That allows us to keep parsing after running
6051 into ill-formed destructor names. */
6052 if (type_decl == error_mark_node && scope)
6053 return build_nt (BIT_NOT_EXPR, scope);
6054 else if (type_decl == error_mark_node)
6055 return error_mark_node;
6057 /* Check that destructor name and scope match. */
6058 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6060 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6061 error_at (token->location,
6062 "declaration of %<~%T%> as member of %qT",
6064 cp_parser_simulate_error (parser);
6065 return error_mark_node;
6070 A typedef-name that names a class shall not be used as the
6071 identifier in the declarator for a destructor declaration. */
6073 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6074 && !DECL_SELF_REFERENCE_P (type_decl)
6075 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6076 error_at (token->location,
6077 "typedef-name %qD used as destructor declarator",
6080 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6084 if (token->keyword == RID_OPERATOR)
6088 /* This could be a template-id, so we try that first. */
6089 cp_parser_parse_tentatively (parser);
6090 /* Try a template-id. */
6091 id = cp_parser_template_id (parser, template_keyword_p,
6092 /*check_dependency_p=*/true,
6095 /* If that worked, we're done. */
6096 if (cp_parser_parse_definitely (parser))
6098 /* We still don't know whether we're looking at an
6099 operator-function-id or a conversion-function-id. */
6100 cp_parser_parse_tentatively (parser);
6101 /* Try an operator-function-id. */
6102 id = cp_parser_operator_function_id (parser);
6103 /* If that didn't work, try a conversion-function-id. */
6104 if (!cp_parser_parse_definitely (parser))
6105 id = cp_parser_conversion_function_id (parser);
6114 cp_parser_error (parser, "expected unqualified-id");
6115 return error_mark_node;
6119 /* Parse an (optional) nested-name-specifier.
6121 nested-name-specifier: [C++98]
6122 class-or-namespace-name :: nested-name-specifier [opt]
6123 class-or-namespace-name :: template nested-name-specifier [opt]
6125 nested-name-specifier: [C++0x]
6128 nested-name-specifier identifier ::
6129 nested-name-specifier template [opt] simple-template-id ::
6131 PARSER->SCOPE should be set appropriately before this function is
6132 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6133 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6136 Sets PARSER->SCOPE to the class (TYPE) or namespace
6137 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6138 it unchanged if there is no nested-name-specifier. Returns the new
6139 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6141 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6142 part of a declaration and/or decl-specifier. */
6145 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6146 bool typename_keyword_p,
6147 bool check_dependency_p,
6149 bool is_declaration,
6150 bool template_keyword_p /* = false */)
6152 bool success = false;
6153 cp_token_position start = 0;
6156 /* Remember where the nested-name-specifier starts. */
6157 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6159 start = cp_lexer_token_position (parser->lexer, false);
6160 push_deferring_access_checks (dk_deferred);
6167 tree saved_qualifying_scope;
6169 /* Spot cases that cannot be the beginning of a
6170 nested-name-specifier. */
6171 token = cp_lexer_peek_token (parser->lexer);
6173 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6174 the already parsed nested-name-specifier. */
6175 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6177 /* Grab the nested-name-specifier and continue the loop. */
6178 cp_parser_pre_parsed_nested_name_specifier (parser);
6179 /* If we originally encountered this nested-name-specifier
6180 with IS_DECLARATION set to false, we will not have
6181 resolved TYPENAME_TYPEs, so we must do so here. */
6183 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6185 new_scope = resolve_typename_type (parser->scope,
6186 /*only_current_p=*/false);
6187 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6188 parser->scope = new_scope;
6194 /* Spot cases that cannot be the beginning of a
6195 nested-name-specifier. On the second and subsequent times
6196 through the loop, we look for the `template' keyword. */
6197 if (success && token->keyword == RID_TEMPLATE)
6199 /* A template-id can start a nested-name-specifier. */
6200 else if (token->type == CPP_TEMPLATE_ID)
6202 /* DR 743: decltype can be used in a nested-name-specifier. */
6203 else if (token_is_decltype (token))
6207 /* If the next token is not an identifier, then it is
6208 definitely not a type-name or namespace-name. */
6209 if (token->type != CPP_NAME)
6211 /* If the following token is neither a `<' (to begin a
6212 template-id), nor a `::', then we are not looking at a
6213 nested-name-specifier. */
6214 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6216 if (token->type == CPP_COLON
6217 && parser->colon_corrects_to_scope_p
6218 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6220 gcc_rich_location richloc (token->location);
6221 richloc.add_fixit_replace ("::");
6223 "found %<:%> in nested-name-specifier, "
6225 token->type = CPP_SCOPE;
6228 if (token->type != CPP_SCOPE
6229 && !cp_parser_nth_token_starts_template_argument_list_p
6234 /* The nested-name-specifier is optional, so we parse
6236 cp_parser_parse_tentatively (parser);
6238 /* Look for the optional `template' keyword, if this isn't the
6239 first time through the loop. */
6241 template_keyword_p = cp_parser_optional_template_keyword (parser);
6243 /* Save the old scope since the name lookup we are about to do
6244 might destroy it. */
6245 old_scope = parser->scope;
6246 saved_qualifying_scope = parser->qualifying_scope;
6247 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6248 look up names in "X<T>::I" in order to determine that "Y" is
6249 a template. So, if we have a typename at this point, we make
6250 an effort to look through it. */
6252 && !typename_keyword_p
6254 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6255 parser->scope = resolve_typename_type (parser->scope,
6256 /*only_current_p=*/false);
6257 /* Parse the qualifying entity. */
6259 = cp_parser_qualifying_entity (parser,
6265 /* Look for the `::' token. */
6266 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6268 /* If we found what we wanted, we keep going; otherwise, we're
6270 if (!cp_parser_parse_definitely (parser))
6272 bool error_p = false;
6274 /* Restore the OLD_SCOPE since it was valid before the
6275 failed attempt at finding the last
6276 class-or-namespace-name. */
6277 parser->scope = old_scope;
6278 parser->qualifying_scope = saved_qualifying_scope;
6280 /* If the next token is a decltype, and the one after that is a
6281 `::', then the decltype has failed to resolve to a class or
6282 enumeration type. Give this error even when parsing
6283 tentatively since it can't possibly be valid--and we're going
6284 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6285 won't get another chance.*/
6286 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6287 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6290 token = cp_lexer_consume_token (parser->lexer);
6291 error_at (token->location, "decltype evaluates to %qT, "
6292 "which is not a class or enumeration type",
6293 token->u.tree_check_value->value);
6294 parser->scope = error_mark_node;
6298 cp_lexer_consume_token (parser->lexer);
6301 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6302 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6304 /* If we have a non-type template-id followed by ::, it can't
6305 possibly be valid. */
6306 token = cp_lexer_peek_token (parser->lexer);
6307 tree tid = token->u.tree_check_value->value;
6308 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6309 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6311 tree tmpl = NULL_TREE;
6312 if (is_overloaded_fn (tid))
6314 tree fns = get_fns (tid);
6315 if (OVL_SINGLE_P (fns))
6316 tmpl = OVL_FIRST (fns);
6317 error_at (token->location, "function template-id %qD "
6318 "in nested-name-specifier", tid);
6322 /* Variable template. */
6323 tmpl = TREE_OPERAND (tid, 0);
6324 gcc_assert (variable_template_p (tmpl));
6325 error_at (token->location, "variable template-id %qD "
6326 "in nested-name-specifier", tid);
6329 inform (DECL_SOURCE_LOCATION (tmpl),
6330 "%qD declared here", tmpl);
6332 parser->scope = error_mark_node;
6336 cp_lexer_consume_token (parser->lexer);
6337 cp_lexer_consume_token (parser->lexer);
6341 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6343 /* If the next token is an identifier, and the one after
6344 that is a `::', then any valid interpretation would have
6345 found a class-or-namespace-name. */
6346 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6347 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6349 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6352 token = cp_lexer_consume_token (parser->lexer);
6355 if (!token->error_reported)
6358 tree ambiguous_decls;
6360 decl = cp_parser_lookup_name (parser, token->u.value,
6362 /*is_template=*/false,
6363 /*is_namespace=*/false,
6364 /*check_dependency=*/true,
6367 if (TREE_CODE (decl) == TEMPLATE_DECL)
6368 error_at (token->location,
6369 "%qD used without template parameters",
6371 else if (ambiguous_decls)
6373 // cp_parser_lookup_name has the same diagnostic,
6374 // thus make sure to emit it at most once.
6375 if (cp_parser_uncommitted_to_tentative_parse_p
6378 error_at (token->location,
6379 "reference to %qD is ambiguous",
6381 print_candidates (ambiguous_decls);
6383 decl = error_mark_node;
6387 if (cxx_dialect != cxx98)
6388 cp_parser_name_lookup_error
6389 (parser, token->u.value, decl, NLE_NOT_CXX98,
6392 cp_parser_name_lookup_error
6393 (parser, token->u.value, decl, NLE_CXX98,
6397 parser->scope = error_mark_node;
6399 /* Treat this as a successful nested-name-specifier
6404 If the name found is not a class-name (clause
6405 _class_) or namespace-name (_namespace.def_), the
6406 program is ill-formed. */
6409 cp_lexer_consume_token (parser->lexer);
6413 /* We've found one valid nested-name-specifier. */
6415 /* Name lookup always gives us a DECL. */
6416 if (TREE_CODE (new_scope) == TYPE_DECL)
6417 new_scope = TREE_TYPE (new_scope);
6418 /* Uses of "template" must be followed by actual templates. */
6419 if (template_keyword_p
6420 && !(CLASS_TYPE_P (new_scope)
6421 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6422 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6423 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6424 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6425 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6426 == TEMPLATE_ID_EXPR)))
6427 permerror (input_location, TYPE_P (new_scope)
6428 ? G_("%qT is not a template")
6429 : G_("%qD is not a template"),
6431 /* If it is a class scope, try to complete it; we are about to
6432 be looking up names inside the class. */
6433 if (TYPE_P (new_scope)
6434 /* Since checking types for dependency can be expensive,
6435 avoid doing it if the type is already complete. */
6436 && !COMPLETE_TYPE_P (new_scope)
6437 /* Do not try to complete dependent types. */
6438 && !dependent_type_p (new_scope))
6440 new_scope = complete_type (new_scope);
6441 /* If it is a typedef to current class, use the current
6442 class instead, as the typedef won't have any names inside
6444 if (!COMPLETE_TYPE_P (new_scope)
6445 && currently_open_class (new_scope))
6446 new_scope = TYPE_MAIN_VARIANT (new_scope);
6448 /* Make sure we look in the right scope the next time through
6450 parser->scope = new_scope;
6453 /* If parsing tentatively, replace the sequence of tokens that makes
6454 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6455 token. That way, should we re-parse the token stream, we will
6456 not have to repeat the effort required to do the parse, nor will
6457 we issue duplicate error messages. */
6458 if (success && start)
6462 token = cp_lexer_token_at (parser->lexer, start);
6463 /* Reset the contents of the START token. */
6464 token->type = CPP_NESTED_NAME_SPECIFIER;
6465 /* Retrieve any deferred checks. Do not pop this access checks yet
6466 so the memory will not be reclaimed during token replacing below. */
6467 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6468 token->u.tree_check_value->value = parser->scope;
6469 token->u.tree_check_value->checks = get_deferred_access_checks ();
6470 token->u.tree_check_value->qualifying_scope =
6471 parser->qualifying_scope;
6472 token->keyword = RID_MAX;
6474 /* Purge all subsequent tokens. */
6475 cp_lexer_purge_tokens_after (parser->lexer, start);
6479 pop_to_parent_deferring_access_checks ();
6481 return success ? parser->scope : NULL_TREE;
6484 /* Parse a nested-name-specifier. See
6485 cp_parser_nested_name_specifier_opt for details. This function
6486 behaves identically, except that it will an issue an error if no
6487 nested-name-specifier is present. */
6490 cp_parser_nested_name_specifier (cp_parser *parser,
6491 bool typename_keyword_p,
6492 bool check_dependency_p,
6494 bool is_declaration)
6498 /* Look for the nested-name-specifier. */
6499 scope = cp_parser_nested_name_specifier_opt (parser,
6504 /* If it was not present, issue an error message. */
6507 cp_parser_error (parser, "expected nested-name-specifier");
6508 parser->scope = NULL_TREE;
6514 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6515 this is either a class-name or a namespace-name (which corresponds
6516 to the class-or-namespace-name production in the grammar). For
6517 C++0x, it can also be a type-name that refers to an enumeration
6518 type or a simple-template-id.
6520 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6521 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6522 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6523 TYPE_P is TRUE iff the next name should be taken as a class-name,
6524 even the same name is declared to be another entity in the same
6527 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6528 specified by the class-or-namespace-name. If neither is found the
6529 ERROR_MARK_NODE is returned. */
6532 cp_parser_qualifying_entity (cp_parser *parser,
6533 bool typename_keyword_p,
6534 bool template_keyword_p,
6535 bool check_dependency_p,
6537 bool is_declaration)
6540 tree saved_qualifying_scope;
6541 tree saved_object_scope;
6544 bool successful_parse_p;
6546 /* DR 743: decltype can appear in a nested-name-specifier. */
6547 if (cp_lexer_next_token_is_decltype (parser->lexer))
6549 scope = cp_parser_decltype (parser);
6550 if (TREE_CODE (scope) != ENUMERAL_TYPE
6551 && !MAYBE_CLASS_TYPE_P (scope))
6553 cp_parser_simulate_error (parser);
6554 return error_mark_node;
6556 if (TYPE_NAME (scope))
6557 scope = TYPE_NAME (scope);
6561 /* Before we try to parse the class-name, we must save away the
6562 current PARSER->SCOPE since cp_parser_class_name will destroy
6564 saved_scope = parser->scope;
6565 saved_qualifying_scope = parser->qualifying_scope;
6566 saved_object_scope = parser->object_scope;
6567 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6568 there is no need to look for a namespace-name. */
6569 only_class_p = template_keyword_p
6570 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6572 cp_parser_parse_tentatively (parser);
6573 scope = cp_parser_class_name (parser,
6576 type_p ? class_type : none_type,
6578 /*class_head_p=*/false,
6580 /*enum_ok=*/cxx_dialect > cxx98);
6581 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6582 /* If that didn't work, try for a namespace-name. */
6583 if (!only_class_p && !successful_parse_p)
6585 /* Restore the saved scope. */
6586 parser->scope = saved_scope;
6587 parser->qualifying_scope = saved_qualifying_scope;
6588 parser->object_scope = saved_object_scope;
6589 /* If we are not looking at an identifier followed by the scope
6590 resolution operator, then this is not part of a
6591 nested-name-specifier. (Note that this function is only used
6592 to parse the components of a nested-name-specifier.) */
6593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6594 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6595 return error_mark_node;
6596 scope = cp_parser_namespace_name (parser);
6602 /* Return true if we are looking at a compound-literal, false otherwise. */
6605 cp_parser_compound_literal_p (cp_parser *parser)
6607 cp_lexer_save_tokens (parser->lexer);
6609 /* Skip tokens until the next token is a closing parenthesis.
6610 If we find the closing `)', and the next token is a `{', then
6611 we are looking at a compound-literal. */
6612 bool compound_literal_p
6613 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6614 /*consume_paren=*/true)
6615 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6617 /* Roll back the tokens we skipped. */
6618 cp_lexer_rollback_tokens (parser->lexer);
6620 return compound_literal_p;
6623 /* Return true if EXPR is the integer constant zero or a complex constant
6624 of zero, without any folding, but ignoring location wrappers. */
6627 literal_integer_zerop (const_tree expr)
6629 STRIP_ANY_LOCATION_WRAPPER (expr);
6630 return integer_zerop (expr);
6633 /* Parse a postfix-expression.
6637 postfix-expression [ expression ]
6638 postfix-expression ( expression-list [opt] )
6639 simple-type-specifier ( expression-list [opt] )
6640 typename :: [opt] nested-name-specifier identifier
6641 ( expression-list [opt] )
6642 typename :: [opt] nested-name-specifier template [opt] template-id
6643 ( expression-list [opt] )
6644 postfix-expression . template [opt] id-expression
6645 postfix-expression -> template [opt] id-expression
6646 postfix-expression . pseudo-destructor-name
6647 postfix-expression -> pseudo-destructor-name
6648 postfix-expression ++
6649 postfix-expression --
6650 dynamic_cast < type-id > ( expression )
6651 static_cast < type-id > ( expression )
6652 reinterpret_cast < type-id > ( expression )
6653 const_cast < type-id > ( expression )
6654 typeid ( expression )
6660 ( type-id ) { initializer-list , [opt] }
6662 This extension is a GNU version of the C99 compound-literal
6663 construct. (The C99 grammar uses `type-name' instead of `type-id',
6664 but they are essentially the same concept.)
6666 If ADDRESS_P is true, the postfix expression is the operand of the
6667 `&' operator. CAST_P is true if this expression is the target of a
6670 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6671 class member access expressions [expr.ref].
6673 Returns a representation of the expression. */
6676 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6677 bool member_access_only_p, bool decltype_p,
6678 cp_id_kind * pidk_return)
6683 cp_id_kind idk = CP_ID_KIND_NONE;
6684 cp_expr postfix_expression = NULL_TREE;
6685 bool is_member_access = false;
6687 /* Peek at the next token. */
6688 token = cp_lexer_peek_token (parser->lexer);
6689 loc = token->location;
6690 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6692 /* Some of the productions are determined by keywords. */
6693 keyword = token->keyword;
6703 const char *saved_message;
6704 bool saved_in_type_id_in_expr_p;
6706 /* All of these can be handled in the same way from the point
6707 of view of parsing. Begin by consuming the token
6708 identifying the cast. */
6709 cp_lexer_consume_token (parser->lexer);
6711 /* New types cannot be defined in the cast. */
6712 saved_message = parser->type_definition_forbidden_message;
6713 parser->type_definition_forbidden_message
6714 = G_("types may not be defined in casts");
6716 /* Look for the opening `<'. */
6717 cp_parser_require (parser, CPP_LESS, RT_LESS);
6718 /* Parse the type to which we are casting. */
6719 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6720 parser->in_type_id_in_expr_p = true;
6721 type = cp_parser_type_id (parser);
6722 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6723 /* Look for the closing `>'. */
6724 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6725 /* Restore the old message. */
6726 parser->type_definition_forbidden_message = saved_message;
6728 bool saved_greater_than_is_operator_p
6729 = parser->greater_than_is_operator_p;
6730 parser->greater_than_is_operator_p = true;
6732 /* And the expression which is being cast. */
6733 matching_parens parens;
6734 parens.require_open (parser);
6735 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6736 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6738 location_t end_loc = close_paren ?
6739 close_paren->location : UNKNOWN_LOCATION;
6741 parser->greater_than_is_operator_p
6742 = saved_greater_than_is_operator_p;
6744 /* Only type conversions to integral or enumeration types
6745 can be used in constant-expressions. */
6746 if (!cast_valid_in_integral_constant_expression_p (type)
6747 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6749 postfix_expression = error_mark_node;
6757 = build_dynamic_cast (type, expression, tf_warning_or_error);
6761 = build_static_cast (type, expression, tf_warning_or_error);
6765 = build_reinterpret_cast (type, expression,
6766 tf_warning_or_error);
6770 = build_const_cast (type, expression, tf_warning_or_error);
6776 /* Construct a location e.g. :
6777 reinterpret_cast <int *> (expr)
6778 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6779 ranging from the start of the "*_cast" token to the final closing
6780 paren, with the caret at the start. */
6781 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6782 postfix_expression.set_location (cp_cast_loc);
6789 const char *saved_message;
6790 bool saved_in_type_id_in_expr_p;
6792 /* Consume the `typeid' token. */
6793 cp_lexer_consume_token (parser->lexer);
6794 /* Look for the `(' token. */
6795 matching_parens parens;
6796 parens.require_open (parser);
6797 /* Types cannot be defined in a `typeid' expression. */
6798 saved_message = parser->type_definition_forbidden_message;
6799 parser->type_definition_forbidden_message
6800 = G_("types may not be defined in a %<typeid%> expression");
6801 /* We can't be sure yet whether we're looking at a type-id or an
6803 cp_parser_parse_tentatively (parser);
6804 /* Try a type-id first. */
6805 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6806 parser->in_type_id_in_expr_p = true;
6807 type = cp_parser_type_id (parser);
6808 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6809 /* Look for the `)' token. Otherwise, we can't be sure that
6810 we're not looking at an expression: consider `typeid (int
6811 (3))', for example. */
6812 cp_token *close_paren = parens.require_close (parser);
6813 /* If all went well, simply lookup the type-id. */
6814 if (cp_parser_parse_definitely (parser))
6815 postfix_expression = get_typeid (type, tf_warning_or_error);
6816 /* Otherwise, fall back to the expression variant. */
6821 /* Look for an expression. */
6822 expression = cp_parser_expression (parser, & idk);
6823 /* Compute its typeid. */
6824 postfix_expression = build_typeid (expression, tf_warning_or_error);
6825 /* Look for the `)' token. */
6826 close_paren = parens.require_close (parser);
6828 /* Restore the saved message. */
6829 parser->type_definition_forbidden_message = saved_message;
6830 /* `typeid' may not appear in an integral constant expression. */
6831 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6832 postfix_expression = error_mark_node;
6834 /* Construct a location e.g. :
6837 ranging from the start of the "typeid" token to the final closing
6838 paren, with the caret at the start. */
6841 location_t typeid_loc
6842 = make_location (start_loc, start_loc, close_paren->location);
6843 postfix_expression.set_location (typeid_loc);
6844 postfix_expression.maybe_add_location_wrapper ();
6852 /* The syntax permitted here is the same permitted for an
6853 elaborated-type-specifier. */
6854 ++parser->prevent_constrained_type_specifiers;
6855 type = cp_parser_elaborated_type_specifier (parser,
6856 /*is_friend=*/false,
6857 /*is_declaration=*/false);
6858 --parser->prevent_constrained_type_specifiers;
6859 postfix_expression = cp_parser_functional_cast (parser, type);
6864 case RID_BUILTIN_SHUFFLE:
6865 case RID_BUILTIN_LAUNDER:
6867 vec<tree, va_gc> *vec;
6871 cp_lexer_consume_token (parser->lexer);
6872 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6873 /*cast_p=*/false, /*allow_expansion_p=*/true,
6874 /*non_constant_p=*/NULL);
6877 postfix_expression = error_mark_node;
6881 FOR_EACH_VEC_ELT (*vec, i, p)
6887 if (vec->length () == 1)
6889 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6892 error_at (loc, "wrong number of arguments to "
6893 "%<__builtin_addressof%>");
6894 postfix_expression = error_mark_node;
6898 case RID_BUILTIN_LAUNDER:
6899 if (vec->length () == 1)
6900 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6901 tf_warning_or_error);
6904 error_at (loc, "wrong number of arguments to "
6905 "%<__builtin_launder%>");
6906 postfix_expression = error_mark_node;
6910 case RID_BUILTIN_SHUFFLE:
6911 if (vec->length () == 2)
6913 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6914 (*vec)[1], tf_warning_or_error);
6915 else if (vec->length () == 3)
6917 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6918 (*vec)[2], tf_warning_or_error);
6921 error_at (loc, "wrong number of arguments to "
6922 "%<__builtin_shuffle%>");
6923 postfix_expression = error_mark_node;
6937 /* If the next thing is a simple-type-specifier, we may be
6938 looking at a functional cast. We could also be looking at
6939 an id-expression. So, we try the functional cast, and if
6940 that doesn't work we fall back to the primary-expression. */
6941 cp_parser_parse_tentatively (parser);
6942 /* Look for the simple-type-specifier. */
6943 ++parser->prevent_constrained_type_specifiers;
6944 type = cp_parser_simple_type_specifier (parser,
6945 /*decl_specs=*/NULL,
6946 CP_PARSER_FLAGS_NONE);
6947 --parser->prevent_constrained_type_specifiers;
6948 /* Parse the cast itself. */
6949 if (!cp_parser_error_occurred (parser))
6951 = cp_parser_functional_cast (parser, type);
6952 /* If that worked, we're done. */
6953 if (cp_parser_parse_definitely (parser))
6956 /* If the functional-cast didn't work out, try a
6957 compound-literal. */
6958 if (cp_parser_allow_gnu_extensions_p (parser)
6959 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6961 cp_expr initializer = NULL_TREE;
6963 cp_parser_parse_tentatively (parser);
6965 matching_parens parens;
6966 parens.consume_open (parser);
6968 /* Avoid calling cp_parser_type_id pointlessly, see comment
6969 in cp_parser_cast_expression about c++/29234. */
6970 if (!cp_parser_compound_literal_p (parser))
6971 cp_parser_simulate_error (parser);
6974 /* Parse the type. */
6975 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6976 parser->in_type_id_in_expr_p = true;
6977 type = cp_parser_type_id (parser);
6978 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6979 parens.require_close (parser);
6982 /* If things aren't going well, there's no need to
6984 if (!cp_parser_error_occurred (parser))
6986 bool non_constant_p;
6987 /* Parse the brace-enclosed initializer list. */
6988 initializer = cp_parser_braced_list (parser,
6991 /* If that worked, we're definitely looking at a
6992 compound-literal expression. */
6993 if (cp_parser_parse_definitely (parser))
6995 /* Warn the user that a compound literal is not
6996 allowed in standard C++. */
6997 pedwarn (input_location, OPT_Wpedantic,
6998 "ISO C++ forbids compound-literals");
6999 /* For simplicity, we disallow compound literals in
7000 constant-expressions. We could
7001 allow compound literals of integer type, whose
7002 initializer was a constant, in constant
7003 expressions. Permitting that usage, as a further
7004 extension, would not change the meaning of any
7005 currently accepted programs. (Of course, as
7006 compound literals are not part of ISO C++, the
7007 standard has nothing to say.) */
7008 if (cp_parser_non_integral_constant_expression (parser,
7011 postfix_expression = error_mark_node;
7014 /* Form the representation of the compound-literal. */
7016 = finish_compound_literal (type, initializer,
7017 tf_warning_or_error, fcl_c99);
7018 postfix_expression.set_location (initializer.get_location ());
7023 /* It must be a primary-expression. */
7025 = cp_parser_primary_expression (parser, address_p, cast_p,
7026 /*template_arg_p=*/false,
7033 /* Note that we don't need to worry about calling build_cplus_new on a
7034 class-valued CALL_EXPR in decltype when it isn't the end of the
7035 postfix-expression; unary_complex_lvalue will take care of that for
7038 /* Keep looping until the postfix-expression is complete. */
7041 if (idk == CP_ID_KIND_UNQUALIFIED
7042 && identifier_p (postfix_expression)
7043 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7044 /* It is not a Koenig lookup function call. */
7046 = unqualified_name_lookup_error (postfix_expression);
7048 /* Peek at the next token. */
7049 token = cp_lexer_peek_token (parser->lexer);
7051 switch (token->type)
7053 case CPP_OPEN_SQUARE:
7054 if (cp_next_tokens_can_be_std_attribute_p (parser))
7056 cp_parser_error (parser,
7057 "two consecutive %<[%> shall "
7058 "only introduce an attribute");
7059 return error_mark_node;
7062 = cp_parser_postfix_open_square_expression (parser,
7066 postfix_expression.set_range (start_loc,
7067 postfix_expression.get_location ());
7069 idk = CP_ID_KIND_NONE;
7070 is_member_access = false;
7073 case CPP_OPEN_PAREN:
7074 /* postfix-expression ( expression-list [opt] ) */
7077 bool is_builtin_constant_p;
7078 bool saved_integral_constant_expression_p = false;
7079 bool saved_non_integral_constant_expression_p = false;
7080 tsubst_flags_t complain = complain_flags (decltype_p);
7081 vec<tree, va_gc> *args;
7082 location_t close_paren_loc = UNKNOWN_LOCATION;
7084 is_member_access = false;
7086 is_builtin_constant_p
7087 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7088 if (is_builtin_constant_p)
7090 /* The whole point of __builtin_constant_p is to allow
7091 non-constant expressions to appear as arguments. */
7092 saved_integral_constant_expression_p
7093 = parser->integral_constant_expression_p;
7094 saved_non_integral_constant_expression_p
7095 = parser->non_integral_constant_expression_p;
7096 parser->integral_constant_expression_p = false;
7098 args = (cp_parser_parenthesized_expression_list
7100 /*cast_p=*/false, /*allow_expansion_p=*/true,
7101 /*non_constant_p=*/NULL,
7102 /*close_paren_loc=*/&close_paren_loc,
7103 /*wrap_locations_p=*/true));
7104 if (is_builtin_constant_p)
7106 parser->integral_constant_expression_p
7107 = saved_integral_constant_expression_p;
7108 parser->non_integral_constant_expression_p
7109 = saved_non_integral_constant_expression_p;
7114 postfix_expression = error_mark_node;
7118 /* Function calls are not permitted in
7119 constant-expressions. */
7120 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7121 && cp_parser_non_integral_constant_expression (parser,
7124 postfix_expression = error_mark_node;
7125 release_tree_vector (args);
7130 if (idk == CP_ID_KIND_UNQUALIFIED
7131 || idk == CP_ID_KIND_TEMPLATE_ID)
7133 if (identifier_p (postfix_expression))
7135 if (!args->is_empty ())
7138 if (!any_type_dependent_arguments_p (args))
7140 = perform_koenig_lookup (postfix_expression, args,
7145 = unqualified_fn_lookup_error (postfix_expression);
7147 /* We do not perform argument-dependent lookup if
7148 normal lookup finds a non-function, in accordance
7149 with the expected resolution of DR 218. */
7150 else if (!args->is_empty ()
7151 && is_overloaded_fn (postfix_expression))
7153 /* We only need to look at the first function,
7154 because all the fns share the attribute we're
7155 concerned with (all member fns or all local
7157 tree fn = get_first_fn (postfix_expression);
7158 fn = STRIP_TEMPLATE (fn);
7160 /* Do not do argument dependent lookup if regular
7161 lookup finds a member function or a block-scope
7162 function declaration. [basic.lookup.argdep]/3 */
7163 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7164 || DECL_FUNCTION_MEMBER_P (fn)
7165 || DECL_LOCAL_FUNCTION_P (fn)))
7168 if (!any_type_dependent_arguments_p (args))
7170 = perform_koenig_lookup (postfix_expression, args,
7176 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7177 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7178 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7179 && vec_safe_length (args) == 3)
7181 tree arg0 = (*args)[0];
7182 tree arg1 = (*args)[1];
7183 tree arg2 = (*args)[2];
7184 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7185 | (literal_integer_zerop (arg2) << 2));
7186 warn_for_memset (input_location, arg0, arg2, literal_mask);
7189 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7191 tree instance = TREE_OPERAND (postfix_expression, 0);
7192 tree fn = TREE_OPERAND (postfix_expression, 1);
7194 if (processing_template_decl
7195 && (type_dependent_object_expression_p (instance)
7196 || (!BASELINK_P (fn)
7197 && TREE_CODE (fn) != FIELD_DECL)
7198 || type_dependent_expression_p (fn)
7199 || any_type_dependent_arguments_p (args)))
7201 maybe_generic_this_capture (instance, fn);
7203 = build_min_nt_call_vec (postfix_expression, args);
7204 release_tree_vector (args);
7208 if (BASELINK_P (fn))
7211 = (build_new_method_call
7212 (instance, fn, &args, NULL_TREE,
7213 (idk == CP_ID_KIND_QUALIFIED
7214 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7221 = finish_call_expr (postfix_expression, &args,
7222 /*disallow_virtual=*/false,
7226 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7227 || TREE_CODE (postfix_expression) == MEMBER_REF
7228 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7229 postfix_expression = (build_offset_ref_call_from_tree
7230 (postfix_expression, &args,
7232 else if (idk == CP_ID_KIND_QUALIFIED)
7233 /* A call to a static class member, or a namespace-scope
7236 = finish_call_expr (postfix_expression, &args,
7237 /*disallow_virtual=*/true,
7241 /* All other function calls. */
7243 = finish_call_expr (postfix_expression, &args,
7244 /*disallow_virtual=*/false,
7248 if (close_paren_loc != UNKNOWN_LOCATION)
7250 location_t combined_loc = make_location (token->location,
7253 postfix_expression.set_location (combined_loc);
7256 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7257 idk = CP_ID_KIND_NONE;
7259 release_tree_vector (args);
7265 /* postfix-expression . template [opt] id-expression
7266 postfix-expression . pseudo-destructor-name
7267 postfix-expression -> template [opt] id-expression
7268 postfix-expression -> pseudo-destructor-name */
7270 /* Consume the `.' or `->' operator. */
7271 cp_lexer_consume_token (parser->lexer);
7274 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7278 is_member_access = true;
7282 /* postfix-expression ++ */
7283 /* Consume the `++' token. */
7284 cp_lexer_consume_token (parser->lexer);
7285 /* Generate a representation for the complete expression. */
7287 = finish_increment_expr (postfix_expression,
7288 POSTINCREMENT_EXPR);
7289 /* Increments may not appear in constant-expressions. */
7290 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7291 postfix_expression = error_mark_node;
7292 idk = CP_ID_KIND_NONE;
7293 is_member_access = false;
7296 case CPP_MINUS_MINUS:
7297 /* postfix-expression -- */
7298 /* Consume the `--' token. */
7299 cp_lexer_consume_token (parser->lexer);
7300 /* Generate a representation for the complete expression. */
7302 = finish_increment_expr (postfix_expression,
7303 POSTDECREMENT_EXPR);
7304 /* Decrements may not appear in constant-expressions. */
7305 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7306 postfix_expression = error_mark_node;
7307 idk = CP_ID_KIND_NONE;
7308 is_member_access = false;
7312 if (pidk_return != NULL)
7313 * pidk_return = idk;
7314 if (member_access_only_p)
7315 return is_member_access
7316 ? postfix_expression
7317 : cp_expr (error_mark_node);
7319 return postfix_expression;
7323 /* We should never get here. */
7325 return error_mark_node;
7328 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7329 by cp_parser_builtin_offsetof. We're looking for
7331 postfix-expression [ expression ]
7332 postfix-expression [ braced-init-list ] (C++11)
7334 FOR_OFFSETOF is set if we're being called in that context, which
7335 changes how we deal with integer constant expressions. */
7338 cp_parser_postfix_open_square_expression (cp_parser *parser,
7339 tree postfix_expression,
7343 tree index = NULL_TREE;
7344 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7345 bool saved_greater_than_is_operator_p;
7347 /* Consume the `[' token. */
7348 cp_lexer_consume_token (parser->lexer);
7350 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7351 parser->greater_than_is_operator_p = true;
7353 /* Parse the index expression. */
7354 /* ??? For offsetof, there is a question of what to allow here. If
7355 offsetof is not being used in an integral constant expression context,
7356 then we *could* get the right answer by computing the value at runtime.
7357 If we are in an integral constant expression context, then we might
7358 could accept any constant expression; hard to say without analysis.
7359 Rather than open the barn door too wide right away, allow only integer
7360 constant expressions here. */
7362 index = cp_parser_constant_expression (parser);
7365 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7367 bool expr_nonconst_p;
7368 cp_lexer_set_source_position (parser->lexer);
7369 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7370 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7373 index = cp_parser_expression (parser);
7376 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7378 /* Look for the closing `]'. */
7379 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7381 /* Build the ARRAY_REF. */
7382 postfix_expression = grok_array_decl (loc, postfix_expression,
7385 /* When not doing offsetof, array references are not permitted in
7386 constant-expressions. */
7388 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7389 postfix_expression = error_mark_node;
7391 return postfix_expression;
7394 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7395 dereference of incomplete type, returns true if error_mark_node should
7396 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7397 and *DEPENDENT_P. */
7400 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7403 /* In a template, be permissive by treating an object expression
7404 of incomplete type as dependent (after a pedwarn). */
7405 diagnostic_t kind = (processing_template_decl
7406 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7408 switch (TREE_CODE (*postfix_expression))
7411 case REINTERPRET_CAST_EXPR:
7412 case CONST_CAST_EXPR:
7413 case STATIC_CAST_EXPR:
7414 case DYNAMIC_CAST_EXPR:
7415 case IMPLICIT_CONV_EXPR:
7416 case VIEW_CONVERT_EXPR:
7417 case NON_LVALUE_EXPR:
7421 /* Don't emit any diagnostic for OVERLOADs. */
7425 /* Avoid clobbering e.g. DECLs. */
7426 if (!EXPR_P (*postfix_expression))
7431 if (kind == DK_IGNORED)
7434 location_t exploc = location_of (*postfix_expression);
7435 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7436 if (!MAYBE_CLASS_TYPE_P (*scope))
7438 if (kind == DK_ERROR)
7439 *scope = *postfix_expression = error_mark_node;
7440 else if (processing_template_decl)
7442 *dependent_p = true;
7443 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7448 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7449 by cp_parser_builtin_offsetof. We're looking for
7451 postfix-expression . template [opt] id-expression
7452 postfix-expression . pseudo-destructor-name
7453 postfix-expression -> template [opt] id-expression
7454 postfix-expression -> pseudo-destructor-name
7456 FOR_OFFSETOF is set if we're being called in that context. That sorta
7457 limits what of the above we'll actually accept, but nevermind.
7458 TOKEN_TYPE is the "." or "->" token, which will already have been
7459 removed from the stream. */
7462 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7463 enum cpp_ttype token_type,
7464 cp_expr postfix_expression,
7465 bool for_offsetof, cp_id_kind *idk,
7466 location_t location)
7470 bool pseudo_destructor_p;
7471 tree scope = NULL_TREE;
7472 location_t start_loc = postfix_expression.get_start ();
7474 /* If this is a `->' operator, dereference the pointer. */
7475 if (token_type == CPP_DEREF)
7476 postfix_expression = build_x_arrow (location, postfix_expression,
7477 tf_warning_or_error);
7478 /* Check to see whether or not the expression is type-dependent and
7479 not the current instantiation. */
7480 dependent_p = type_dependent_object_expression_p (postfix_expression);
7481 /* The identifier following the `->' or `.' is not qualified. */
7482 parser->scope = NULL_TREE;
7483 parser->qualifying_scope = NULL_TREE;
7484 parser->object_scope = NULL_TREE;
7485 *idk = CP_ID_KIND_NONE;
7487 /* Enter the scope corresponding to the type of the object
7488 given by the POSTFIX_EXPRESSION. */
7491 scope = TREE_TYPE (postfix_expression);
7492 /* According to the standard, no expression should ever have
7493 reference type. Unfortunately, we do not currently match
7494 the standard in this respect in that our internal representation
7495 of an expression may have reference type even when the standard
7496 says it does not. Therefore, we have to manually obtain the
7497 underlying type here. */
7498 scope = non_reference (scope);
7499 /* The type of the POSTFIX_EXPRESSION must be complete. */
7500 /* Unlike the object expression in other contexts, *this is not
7501 required to be of complete type for purposes of class member
7502 access (5.2.5) outside the member function body. */
7503 if (postfix_expression != current_class_ref
7504 && scope != error_mark_node
7505 && !currently_open_class (scope))
7507 scope = complete_type (scope);
7508 if (!COMPLETE_TYPE_P (scope)
7509 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7511 return error_mark_node;
7516 /* Let the name lookup machinery know that we are processing a
7517 class member access expression. */
7518 parser->context->object_type = scope;
7519 /* If something went wrong, we want to be able to discern that case,
7520 as opposed to the case where there was no SCOPE due to the type
7521 of expression being dependent. */
7523 scope = error_mark_node;
7524 /* If the SCOPE was erroneous, make the various semantic analysis
7525 functions exit quickly -- and without issuing additional error
7527 if (scope == error_mark_node)
7528 postfix_expression = error_mark_node;
7533 /* Tell cp_parser_lookup_name that there was an object, even though it's
7535 parser->context->object_type = unknown_type_node;
7537 /* Assume this expression is not a pseudo-destructor access. */
7538 pseudo_destructor_p = false;
7540 /* If the SCOPE is a scalar type, then, if this is a valid program,
7541 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7542 is type dependent, it can be pseudo-destructor-name or something else.
7543 Try to parse it as pseudo-destructor-name first. */
7544 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7549 cp_parser_parse_tentatively (parser);
7550 /* Parse the pseudo-destructor-name. */
7552 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7555 && (cp_parser_error_occurred (parser)
7556 || !SCALAR_TYPE_P (type)))
7557 cp_parser_abort_tentative_parse (parser);
7558 else if (cp_parser_parse_definitely (parser))
7560 pseudo_destructor_p = true;
7562 = finish_pseudo_destructor_expr (postfix_expression,
7567 if (!pseudo_destructor_p)
7569 /* If the SCOPE is not a scalar type, we are looking at an
7570 ordinary class member access expression, rather than a
7571 pseudo-destructor-name. */
7573 cp_token *token = cp_lexer_peek_token (parser->lexer);
7574 /* Parse the id-expression. */
7575 name = (cp_parser_id_expression
7577 cp_parser_optional_template_keyword (parser),
7578 /*check_dependency_p=*/true,
7580 /*declarator_p=*/false,
7581 /*optional_p=*/false));
7582 /* In general, build a SCOPE_REF if the member name is qualified.
7583 However, if the name was not dependent and has already been
7584 resolved; there is no need to build the SCOPE_REF. For example;
7586 struct X { void f(); };
7587 template <typename T> void f(T* t) { t->X::f(); }
7589 Even though "t" is dependent, "X::f" is not and has been resolved
7590 to a BASELINK; there is no need to include scope information. */
7592 /* But we do need to remember that there was an explicit scope for
7593 virtual function calls. */
7595 *idk = CP_ID_KIND_QUALIFIED;
7597 /* If the name is a template-id that names a type, we will get a
7598 TYPE_DECL here. That is invalid code. */
7599 if (TREE_CODE (name) == TYPE_DECL)
7601 error_at (token->location, "invalid use of %qD", name);
7602 postfix_expression = error_mark_node;
7606 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7608 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7610 error_at (token->location, "%<%D::%D%> is not a class member",
7611 parser->scope, name);
7612 postfix_expression = error_mark_node;
7615 name = build_qualified_name (/*type=*/NULL_TREE,
7619 parser->scope = NULL_TREE;
7620 parser->qualifying_scope = NULL_TREE;
7621 parser->object_scope = NULL_TREE;
7623 if (parser->scope && name && BASELINK_P (name))
7624 adjust_result_of_qualified_name_lookup
7625 (name, parser->scope, scope);
7627 = finish_class_member_access_expr (postfix_expression, name,
7629 tf_warning_or_error);
7630 /* Build a location e.g.:
7633 where the caret is at the deref token, ranging from
7634 the start of postfix_expression to the end of the access expr. */
7636 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7637 location_t combined_loc
7638 = make_location (input_location, start_loc, end_loc);
7639 protected_set_expr_location (postfix_expression, combined_loc);
7643 /* We no longer need to look up names in the scope of the object on
7644 the left-hand side of the `.' or `->' operator. */
7645 parser->context->object_type = NULL_TREE;
7647 /* Outside of offsetof, these operators may not appear in
7648 constant-expressions. */
7650 && (cp_parser_non_integral_constant_expression
7651 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7652 postfix_expression = error_mark_node;
7654 return postfix_expression;
7657 /* Parse a parenthesized expression-list.
7660 assignment-expression
7661 expression-list, assignment-expression
7666 identifier, expression-list
7668 CAST_P is true if this expression is the target of a cast.
7670 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7673 WRAP_LOCATIONS_P is true if expressions within this list for which
7674 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7675 their source locations.
7677 Returns a vector of trees. Each element is a representation of an
7678 assignment-expression. NULL is returned if the ( and or ) are
7679 missing. An empty, but allocated, vector is returned on no
7680 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7681 if we are parsing an attribute list for an attribute that wants a
7682 plain identifier argument, normal_attr for an attribute that wants
7683 an expression, or non_attr if we aren't parsing an attribute list. If
7684 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7685 not all of the expressions in the list were constant.
7686 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7687 will be written to with the location of the closing parenthesis. If
7688 an error occurs, it may or may not be written to. */
7690 static vec<tree, va_gc> *
7691 cp_parser_parenthesized_expression_list (cp_parser* parser,
7692 int is_attribute_list,
7694 bool allow_expansion_p,
7695 bool *non_constant_p,
7696 location_t *close_paren_loc,
7697 bool wrap_locations_p)
7699 vec<tree, va_gc> *expression_list;
7700 bool fold_expr_p = is_attribute_list != non_attr;
7701 tree identifier = NULL_TREE;
7702 bool saved_greater_than_is_operator_p;
7704 /* Assume all the expressions will be constant. */
7706 *non_constant_p = false;
7708 matching_parens parens;
7709 if (!parens.require_open (parser))
7712 expression_list = make_tree_vector ();
7714 /* Within a parenthesized expression, a `>' token is always
7715 the greater-than operator. */
7716 saved_greater_than_is_operator_p
7717 = parser->greater_than_is_operator_p;
7718 parser->greater_than_is_operator_p = true;
7720 cp_expr expr (NULL_TREE);
7722 /* Consume expressions until there are no more. */
7723 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7726 /* At the beginning of attribute lists, check to see if the
7727 next token is an identifier. */
7728 if (is_attribute_list == id_attr
7729 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7733 /* Consume the identifier. */
7734 token = cp_lexer_consume_token (parser->lexer);
7735 /* Save the identifier. */
7736 identifier = token->u.value;
7740 bool expr_non_constant_p;
7742 /* Parse the next assignment-expression. */
7743 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7745 /* A braced-init-list. */
7746 cp_lexer_set_source_position (parser->lexer);
7747 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7748 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7749 if (non_constant_p && expr_non_constant_p)
7750 *non_constant_p = true;
7752 else if (non_constant_p)
7754 expr = (cp_parser_constant_expression
7755 (parser, /*allow_non_constant_p=*/true,
7756 &expr_non_constant_p));
7757 if (expr_non_constant_p)
7758 *non_constant_p = true;
7761 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7765 expr = instantiate_non_dependent_expr (expr);
7767 /* If we have an ellipsis, then this is an expression
7769 if (allow_expansion_p
7770 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7772 /* Consume the `...'. */
7773 cp_lexer_consume_token (parser->lexer);
7775 /* Build the argument pack. */
7776 expr = make_pack_expansion (expr);
7779 if (wrap_locations_p)
7780 expr.maybe_add_location_wrapper ();
7782 /* Add it to the list. We add error_mark_node
7783 expressions to the list, so that we can still tell if
7784 the correct form for a parenthesized expression-list
7785 is found. That gives better errors. */
7786 vec_safe_push (expression_list, expr.get_value ());
7788 if (expr == error_mark_node)
7792 /* After the first item, attribute lists look the same as
7793 expression lists. */
7794 is_attribute_list = non_attr;
7797 /* If the next token isn't a `,', then we are done. */
7798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7801 /* Otherwise, consume the `,' and keep going. */
7802 cp_lexer_consume_token (parser->lexer);
7805 if (close_paren_loc)
7806 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7808 if (!parens.require_close (parser))
7813 /* We try and resync to an unnested comma, as that will give the
7814 user better diagnostics. */
7815 ending = cp_parser_skip_to_closing_parenthesis (parser,
7816 /*recovering=*/true,
7818 /*consume_paren=*/true);
7823 parser->greater_than_is_operator_p
7824 = saved_greater_than_is_operator_p;
7829 parser->greater_than_is_operator_p
7830 = saved_greater_than_is_operator_p;
7833 vec_safe_insert (expression_list, 0, identifier);
7835 return expression_list;
7838 /* Parse a pseudo-destructor-name.
7840 pseudo-destructor-name:
7841 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7842 :: [opt] nested-name-specifier template template-id :: ~ type-name
7843 :: [opt] nested-name-specifier [opt] ~ type-name
7845 If either of the first two productions is used, sets *SCOPE to the
7846 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7847 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7848 or ERROR_MARK_NODE if the parse fails. */
7851 cp_parser_pseudo_destructor_name (cp_parser* parser,
7856 bool nested_name_specifier_p;
7859 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7860 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7861 && !type_dependent_expression_p (object))
7863 if (cxx_dialect < cxx14)
7864 pedwarn (input_location, 0,
7865 "%<~auto%> only available with "
7866 "-std=c++14 or -std=gnu++14");
7867 cp_lexer_consume_token (parser->lexer);
7868 cp_lexer_consume_token (parser->lexer);
7870 *type = TREE_TYPE (object);
7874 /* Assume that things will not work out. */
7875 *type = error_mark_node;
7877 /* Look for the optional `::' operator. */
7878 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7879 /* Look for the optional nested-name-specifier. */
7880 nested_name_specifier_p
7881 = (cp_parser_nested_name_specifier_opt (parser,
7882 /*typename_keyword_p=*/false,
7883 /*check_dependency_p=*/true,
7885 /*is_declaration=*/false)
7887 /* Now, if we saw a nested-name-specifier, we might be doing the
7888 second production. */
7889 if (nested_name_specifier_p
7890 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7892 /* Consume the `template' keyword. */
7893 cp_lexer_consume_token (parser->lexer);
7894 /* Parse the template-id. */
7895 cp_parser_template_id (parser,
7896 /*template_keyword_p=*/true,
7897 /*check_dependency_p=*/false,
7899 /*is_declaration=*/true);
7900 /* Look for the `::' token. */
7901 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7903 /* If the next token is not a `~', then there might be some
7904 additional qualification. */
7905 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7907 /* At this point, we're looking for "type-name :: ~". The type-name
7908 must not be a class-name, since this is a pseudo-destructor. So,
7909 it must be either an enum-name, or a typedef-name -- both of which
7910 are just identifiers. So, we peek ahead to check that the "::"
7911 and "~" tokens are present; if they are not, then we can avoid
7912 calling type_name. */
7913 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7914 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7915 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7917 cp_parser_error (parser, "non-scalar type");
7921 /* Look for the type-name. */
7922 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7923 if (*scope == error_mark_node)
7926 /* Look for the `::' token. */
7927 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7932 /* Look for the `~'. */
7933 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7935 /* Once we see the ~, this has to be a pseudo-destructor. */
7936 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7937 cp_parser_commit_to_topmost_tentative_parse (parser);
7939 /* Look for the type-name again. We are not responsible for
7940 checking that it matches the first type-name. */
7941 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7944 /* Parse a unary-expression.
7950 unary-operator cast-expression
7951 sizeof unary-expression
7953 alignof ( type-id ) [C++0x]
7960 __extension__ cast-expression
7961 __alignof__ unary-expression
7962 __alignof__ ( type-id )
7963 alignof unary-expression [C++0x]
7964 __real__ cast-expression
7965 __imag__ cast-expression
7967 sizeof ( type-id ) { initializer-list , [opt] }
7968 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7969 __alignof__ ( type-id ) { initializer-list , [opt] }
7971 ADDRESS_P is true iff the unary-expression is appearing as the
7972 operand of the `&' operator. CAST_P is true if this expression is
7973 the target of a cast.
7975 Returns a representation of the expression. */
7978 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7979 bool address_p, bool cast_p, bool decltype_p)
7982 enum tree_code unary_operator;
7984 /* Peek at the next token. */
7985 token = cp_lexer_peek_token (parser->lexer);
7986 /* Some keywords give away the kind of expression. */
7987 if (token->type == CPP_KEYWORD)
7989 enum rid keyword = token->keyword;
7998 location_t start_loc = token->location;
8000 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8001 bool std_alignof = id_equal (token->u.value, "alignof");
8003 /* Consume the token. */
8004 cp_lexer_consume_token (parser->lexer);
8005 /* Parse the operand. */
8006 operand = cp_parser_sizeof_operand (parser, keyword);
8008 if (TYPE_P (operand))
8009 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8013 /* ISO C++ defines alignof only with types, not with
8014 expressions. So pedwarn if alignof is used with a non-
8015 type expression. However, __alignof__ is ok. */
8017 pedwarn (token->location, OPT_Wpedantic,
8018 "ISO C++ does not allow %<alignof%> "
8021 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8023 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8024 SIZEOF_EXPR with the original operand. */
8025 if (op == SIZEOF_EXPR && ret != error_mark_node)
8027 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8029 if (!processing_template_decl && TYPE_P (operand))
8031 ret = build_min (SIZEOF_EXPR, size_type_node,
8032 build1 (NOP_EXPR, operand,
8034 SIZEOF_EXPR_TYPE_P (ret) = 1;
8037 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8038 TREE_SIDE_EFFECTS (ret) = 0;
8039 TREE_READONLY (ret) = 1;
8043 /* Construct a location e.g. :
8046 with start == caret at the start of the "alignof"/"sizeof"
8047 token, with the endpoint at the final closing paren. */
8048 location_t finish_loc
8049 = cp_lexer_previous_token (parser->lexer)->location;
8050 location_t compound_loc
8051 = make_location (start_loc, start_loc, finish_loc);
8053 cp_expr ret_expr (ret);
8054 ret_expr.set_location (compound_loc);
8055 ret_expr = ret_expr.maybe_add_location_wrapper ();
8060 return cp_parser_new_expression (parser);
8063 return cp_parser_delete_expression (parser);
8067 /* The saved value of the PEDANTIC flag. */
8071 /* Save away the PEDANTIC flag. */
8072 cp_parser_extension_opt (parser, &saved_pedantic);
8073 /* Parse the cast-expression. */
8074 expr = cp_parser_simple_cast_expression (parser);
8075 /* Restore the PEDANTIC flag. */
8076 pedantic = saved_pedantic;
8086 /* Consume the `__real__' or `__imag__' token. */
8087 cp_lexer_consume_token (parser->lexer);
8088 /* Parse the cast-expression. */
8089 expression = cp_parser_simple_cast_expression (parser);
8090 /* Create the complete representation. */
8091 return build_x_unary_op (token->location,
8092 (keyword == RID_REALPART
8093 ? REALPART_EXPR : IMAGPART_EXPR),
8095 tf_warning_or_error);
8099 case RID_TRANSACTION_ATOMIC:
8100 case RID_TRANSACTION_RELAXED:
8101 return cp_parser_transaction_expression (parser, keyword);
8106 const char *saved_message;
8107 bool saved_integral_constant_expression_p;
8108 bool saved_non_integral_constant_expression_p;
8109 bool saved_greater_than_is_operator_p;
8111 location_t start_loc = token->location;
8113 cp_lexer_consume_token (parser->lexer);
8114 matching_parens parens;
8115 parens.require_open (parser);
8117 saved_message = parser->type_definition_forbidden_message;
8118 parser->type_definition_forbidden_message
8119 = G_("types may not be defined in %<noexcept%> expressions");
8121 saved_integral_constant_expression_p
8122 = parser->integral_constant_expression_p;
8123 saved_non_integral_constant_expression_p
8124 = parser->non_integral_constant_expression_p;
8125 parser->integral_constant_expression_p = false;
8127 saved_greater_than_is_operator_p
8128 = parser->greater_than_is_operator_p;
8129 parser->greater_than_is_operator_p = true;
8131 ++cp_unevaluated_operand;
8132 ++c_inhibit_evaluation_warnings;
8133 ++cp_noexcept_operand;
8134 expr = cp_parser_expression (parser);
8135 --cp_noexcept_operand;
8136 --c_inhibit_evaluation_warnings;
8137 --cp_unevaluated_operand;
8139 parser->greater_than_is_operator_p
8140 = saved_greater_than_is_operator_p;
8142 parser->integral_constant_expression_p
8143 = saved_integral_constant_expression_p;
8144 parser->non_integral_constant_expression_p
8145 = saved_non_integral_constant_expression_p;
8147 parser->type_definition_forbidden_message = saved_message;
8149 location_t finish_loc
8150 = cp_lexer_peek_token (parser->lexer)->location;
8151 parens.require_close (parser);
8153 /* Construct a location of the form:
8156 with start == caret, finishing at the close-paren. */
8157 location_t noexcept_loc
8158 = make_location (start_loc, start_loc, finish_loc);
8160 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8169 /* Look for the `:: new' and `:: delete', which also signal the
8170 beginning of a new-expression, or delete-expression,
8171 respectively. If the next token is `::', then it might be one of
8173 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8177 /* See if the token after the `::' is one of the keywords in
8178 which we're interested. */
8179 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8180 /* If it's `new', we have a new-expression. */
8181 if (keyword == RID_NEW)
8182 return cp_parser_new_expression (parser);
8183 /* Similarly, for `delete'. */
8184 else if (keyword == RID_DELETE)
8185 return cp_parser_delete_expression (parser);
8188 /* Look for a unary operator. */
8189 unary_operator = cp_parser_unary_operator (token);
8190 /* The `++' and `--' operators can be handled similarly, even though
8191 they are not technically unary-operators in the grammar. */
8192 if (unary_operator == ERROR_MARK)
8194 if (token->type == CPP_PLUS_PLUS)
8195 unary_operator = PREINCREMENT_EXPR;
8196 else if (token->type == CPP_MINUS_MINUS)
8197 unary_operator = PREDECREMENT_EXPR;
8198 /* Handle the GNU address-of-label extension. */
8199 else if (cp_parser_allow_gnu_extensions_p (parser)
8200 && token->type == CPP_AND_AND)
8204 location_t start_loc = token->location;
8206 /* Consume the '&&' token. */
8207 cp_lexer_consume_token (parser->lexer);
8208 /* Look for the identifier. */
8209 location_t finish_loc
8210 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8211 identifier = cp_parser_identifier (parser);
8212 /* Construct a location of the form:
8215 with caret==start at the "&&", finish at the end of the label. */
8216 location_t combined_loc
8217 = make_location (start_loc, start_loc, finish_loc);
8218 /* Create an expression representing the address. */
8219 expression = finish_label_address_expr (identifier, combined_loc);
8220 if (cp_parser_non_integral_constant_expression (parser,
8222 expression = error_mark_node;
8226 if (unary_operator != ERROR_MARK)
8228 cp_expr cast_expression;
8229 cp_expr expression = error_mark_node;
8230 non_integral_constant non_constant_p = NIC_NONE;
8231 location_t loc = token->location;
8232 tsubst_flags_t complain = complain_flags (decltype_p);
8234 /* Consume the operator token. */
8235 token = cp_lexer_consume_token (parser->lexer);
8236 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8238 /* Parse the cast-expression. */
8240 = cp_parser_cast_expression (parser,
8241 unary_operator == ADDR_EXPR,
8247 OP_TOKEN CAST_EXPRESSION
8248 ^~~~~~~~~~~~~~~~~~~~~~~~~
8249 with start==caret at the operator token, and
8250 extending to the end of the cast_expression. */
8251 loc = make_location (loc, loc, cast_expression.get_finish ());
8253 /* Now, build an appropriate representation. */
8254 switch (unary_operator)
8257 non_constant_p = NIC_STAR;
8258 expression = build_x_indirect_ref (loc, cast_expression,
8261 /* TODO: build_x_indirect_ref does not always honor the
8262 location, so ensure it is set. */
8263 expression.set_location (loc);
8267 non_constant_p = NIC_ADDR;
8270 expression = build_x_unary_op (loc, unary_operator,
8273 /* TODO: build_x_unary_op does not always honor the location,
8274 so ensure it is set. */
8275 expression.set_location (loc);
8278 case PREINCREMENT_EXPR:
8279 case PREDECREMENT_EXPR:
8280 non_constant_p = unary_operator == PREINCREMENT_EXPR
8281 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8284 /* Immediately fold negation of a constant, unless the constant is 0
8285 (since -0 == 0) or it would overflow. */
8286 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8287 && CONSTANT_CLASS_P (cast_expression)
8288 && !integer_zerop (cast_expression)
8289 && !TREE_OVERFLOW (cast_expression))
8291 tree folded = fold_build1 (unary_operator,
8292 TREE_TYPE (cast_expression),
8294 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8296 expression = cp_expr (folded, loc);
8301 case UNARY_PLUS_EXPR:
8302 case TRUTH_NOT_EXPR:
8303 expression = finish_unary_op_expr (loc, unary_operator,
8304 cast_expression, complain);
8311 if (non_constant_p != NIC_NONE
8312 && cp_parser_non_integral_constant_expression (parser,
8314 expression = error_mark_node;
8319 return cp_parser_postfix_expression (parser, address_p, cast_p,
8320 /*member_access_only_p=*/false,
8325 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8326 unary-operator, the corresponding tree code is returned. */
8328 static enum tree_code
8329 cp_parser_unary_operator (cp_token* token)
8331 switch (token->type)
8334 return INDIRECT_REF;
8340 return UNARY_PLUS_EXPR;
8346 return TRUTH_NOT_EXPR;
8349 return BIT_NOT_EXPR;
8356 /* Parse a new-expression.
8359 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8360 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8362 Returns a representation of the expression. */
8365 cp_parser_new_expression (cp_parser* parser)
8367 bool global_scope_p;
8368 vec<tree, va_gc> *placement;
8370 vec<tree, va_gc> *initializer;
8371 tree nelts = NULL_TREE;
8374 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8376 /* Look for the optional `::' operator. */
8378 = (cp_parser_global_scope_opt (parser,
8379 /*current_scope_valid_p=*/false)
8381 /* Look for the `new' operator. */
8382 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8383 /* There's no easy way to tell a new-placement from the
8384 `( type-id )' construct. */
8385 cp_parser_parse_tentatively (parser);
8386 /* Look for a new-placement. */
8387 placement = cp_parser_new_placement (parser);
8388 /* If that didn't work out, there's no new-placement. */
8389 if (!cp_parser_parse_definitely (parser))
8391 if (placement != NULL)
8392 release_tree_vector (placement);
8396 /* If the next token is a `(', then we have a parenthesized
8398 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8401 const char *saved_message = parser->type_definition_forbidden_message;
8403 /* Consume the `('. */
8404 matching_parens parens;
8405 parens.consume_open (parser);
8407 /* Parse the type-id. */
8408 parser->type_definition_forbidden_message
8409 = G_("types may not be defined in a new-expression");
8411 type_id_in_expr_sentinel s (parser);
8412 type = cp_parser_type_id (parser);
8414 parser->type_definition_forbidden_message = saved_message;
8416 /* Look for the closing `)'. */
8417 parens.require_close (parser);
8418 token = cp_lexer_peek_token (parser->lexer);
8419 /* There should not be a direct-new-declarator in this production,
8420 but GCC used to allowed this, so we check and emit a sensible error
8421 message for this case. */
8422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8424 error_at (token->location,
8425 "array bound forbidden after parenthesized type-id");
8426 inform (token->location,
8427 "try removing the parentheses around the type-id");
8428 cp_parser_direct_new_declarator (parser);
8431 /* Otherwise, there must be a new-type-id. */
8433 type = cp_parser_new_type_id (parser, &nelts);
8435 /* If the next token is a `(' or '{', then we have a new-initializer. */
8436 cp_token *token = cp_lexer_peek_token (parser->lexer);
8437 if (token->type == CPP_OPEN_PAREN
8438 || token->type == CPP_OPEN_BRACE)
8439 initializer = cp_parser_new_initializer (parser);
8443 /* A new-expression may not appear in an integral constant
8445 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8446 ret = error_mark_node;
8447 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8448 of a new-type-id or type-id of a new-expression, the new-expression shall
8449 contain a new-initializer of the form ( assignment-expression )".
8450 Additionally, consistently with the spirit of DR 1467, we want to accept
8451 'new auto { 2 }' too. */
8452 else if ((ret = type_uses_auto (type))
8453 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8454 && (vec_safe_length (initializer) != 1
8455 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8456 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8458 error_at (token->location,
8459 "initialization of new-expression for type %<auto%> "
8460 "requires exactly one element");
8461 ret = error_mark_node;
8465 /* Construct a location e.g.:
8468 with caret == start at the start of the "new" token, and the end
8469 at the end of the final token we consumed. */
8470 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8471 location_t end_loc = get_finish (end_tok->location);
8472 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8474 /* Create a representation of the new-expression. */
8475 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8476 tf_warning_or_error);
8477 protected_set_expr_location (ret, combined_loc);
8480 if (placement != NULL)
8481 release_tree_vector (placement);
8482 if (initializer != NULL)
8483 release_tree_vector (initializer);
8488 /* Parse a new-placement.
8493 Returns the same representation as for an expression-list. */
8495 static vec<tree, va_gc> *
8496 cp_parser_new_placement (cp_parser* parser)
8498 vec<tree, va_gc> *expression_list;
8500 /* Parse the expression-list. */
8501 expression_list = (cp_parser_parenthesized_expression_list
8502 (parser, non_attr, /*cast_p=*/false,
8503 /*allow_expansion_p=*/true,
8504 /*non_constant_p=*/NULL));
8506 if (expression_list && expression_list->is_empty ())
8507 error ("expected expression-list or type-id");
8509 return expression_list;
8512 /* Parse a new-type-id.
8515 type-specifier-seq new-declarator [opt]
8517 Returns the TYPE allocated. If the new-type-id indicates an array
8518 type, *NELTS is set to the number of elements in the last array
8519 bound; the TYPE will not include the last array bound. */
8522 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8524 cp_decl_specifier_seq type_specifier_seq;
8525 cp_declarator *new_declarator;
8526 cp_declarator *declarator;
8527 cp_declarator *outer_declarator;
8528 const char *saved_message;
8530 /* The type-specifier sequence must not contain type definitions.
8531 (It cannot contain declarations of new types either, but if they
8532 are not definitions we will catch that because they are not
8534 saved_message = parser->type_definition_forbidden_message;
8535 parser->type_definition_forbidden_message
8536 = G_("types may not be defined in a new-type-id");
8537 /* Parse the type-specifier-seq. */
8538 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8539 /*is_trailing_return=*/false,
8540 &type_specifier_seq);
8541 /* Restore the old message. */
8542 parser->type_definition_forbidden_message = saved_message;
8544 if (type_specifier_seq.type == error_mark_node)
8545 return error_mark_node;
8547 /* Parse the new-declarator. */
8548 new_declarator = cp_parser_new_declarator_opt (parser);
8550 /* Determine the number of elements in the last array dimension, if
8553 /* Skip down to the last array dimension. */
8554 declarator = new_declarator;
8555 outer_declarator = NULL;
8556 while (declarator && (declarator->kind == cdk_pointer
8557 || declarator->kind == cdk_ptrmem))
8559 outer_declarator = declarator;
8560 declarator = declarator->declarator;
8563 && declarator->kind == cdk_array
8564 && declarator->declarator
8565 && declarator->declarator->kind == cdk_array)
8567 outer_declarator = declarator;
8568 declarator = declarator->declarator;
8571 if (declarator && declarator->kind == cdk_array)
8573 *nelts = declarator->u.array.bounds;
8574 if (*nelts == error_mark_node)
8575 *nelts = integer_one_node;
8577 if (outer_declarator)
8578 outer_declarator->declarator = declarator->declarator;
8580 new_declarator = NULL;
8583 return groktypename (&type_specifier_seq, new_declarator, false);
8586 /* Parse an (optional) new-declarator.
8589 ptr-operator new-declarator [opt]
8590 direct-new-declarator
8592 Returns the declarator. */
8594 static cp_declarator *
8595 cp_parser_new_declarator_opt (cp_parser* parser)
8597 enum tree_code code;
8598 tree type, std_attributes = NULL_TREE;
8599 cp_cv_quals cv_quals;
8601 /* We don't know if there's a ptr-operator next, or not. */
8602 cp_parser_parse_tentatively (parser);
8603 /* Look for a ptr-operator. */
8604 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8605 /* If that worked, look for more new-declarators. */
8606 if (cp_parser_parse_definitely (parser))
8608 cp_declarator *declarator;
8610 /* Parse another optional declarator. */
8611 declarator = cp_parser_new_declarator_opt (parser);
8613 declarator = cp_parser_make_indirect_declarator
8614 (code, type, cv_quals, declarator, std_attributes);
8619 /* If the next token is a `[', there is a direct-new-declarator. */
8620 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8621 return cp_parser_direct_new_declarator (parser);
8626 /* Parse a direct-new-declarator.
8628 direct-new-declarator:
8630 direct-new-declarator [constant-expression]
8634 static cp_declarator *
8635 cp_parser_direct_new_declarator (cp_parser* parser)
8637 cp_declarator *declarator = NULL;
8644 /* Look for the opening `['. */
8645 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8647 token = cp_lexer_peek_token (parser->lexer);
8648 expression = cp_parser_expression (parser);
8649 /* The standard requires that the expression have integral
8650 type. DR 74 adds enumeration types. We believe that the
8651 real intent is that these expressions be handled like the
8652 expression in a `switch' condition, which also allows
8653 classes with a single conversion to integral or
8654 enumeration type. */
8655 if (!processing_template_decl)
8658 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8663 error_at (token->location,
8664 "expression in new-declarator must have integral "
8665 "or enumeration type");
8666 expression = error_mark_node;
8670 /* Look for the closing `]'. */
8671 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8673 /* Add this bound to the declarator. */
8674 declarator = make_array_declarator (declarator, expression);
8676 /* If the next token is not a `[', then there are no more
8678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8685 /* Parse a new-initializer.
8688 ( expression-list [opt] )
8691 Returns a representation of the expression-list. */
8693 static vec<tree, va_gc> *
8694 cp_parser_new_initializer (cp_parser* parser)
8696 vec<tree, va_gc> *expression_list;
8698 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8701 bool expr_non_constant_p;
8702 cp_lexer_set_source_position (parser->lexer);
8703 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8704 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8705 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8706 expression_list = make_tree_vector_single (t);
8709 expression_list = (cp_parser_parenthesized_expression_list
8710 (parser, non_attr, /*cast_p=*/false,
8711 /*allow_expansion_p=*/true,
8712 /*non_constant_p=*/NULL));
8714 return expression_list;
8717 /* Parse a delete-expression.
8720 :: [opt] delete cast-expression
8721 :: [opt] delete [ ] cast-expression
8723 Returns a representation of the expression. */
8726 cp_parser_delete_expression (cp_parser* parser)
8728 bool global_scope_p;
8732 /* Look for the optional `::' operator. */
8734 = (cp_parser_global_scope_opt (parser,
8735 /*current_scope_valid_p=*/false)
8737 /* Look for the `delete' keyword. */
8738 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8739 /* See if the array syntax is in use. */
8740 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8742 /* Consume the `[' token. */
8743 cp_lexer_consume_token (parser->lexer);
8744 /* Look for the `]' token. */
8745 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8746 /* Remember that this is the `[]' construct. */
8752 /* Parse the cast-expression. */
8753 expression = cp_parser_simple_cast_expression (parser);
8755 /* A delete-expression may not appear in an integral constant
8757 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8758 return error_mark_node;
8760 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8761 tf_warning_or_error);
8764 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8765 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8769 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8771 cp_token *token = cp_lexer_peek_token (parser->lexer);
8772 switch (token->type)
8778 case CPP_CLOSE_SQUARE:
8779 case CPP_CLOSE_PAREN:
8780 case CPP_CLOSE_BRACE:
8781 case CPP_OPEN_BRACE:
8785 case CPP_DEREF_STAR:
8793 case CPP_GREATER_EQ:
8814 case CPP_OPEN_PAREN:
8815 /* In ((type ()) () the last () isn't a valid cast-expression,
8816 so the whole must be parsed as postfix-expression. */
8817 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8820 case CPP_OPEN_SQUARE:
8821 /* '[' may start a primary-expression in obj-c++ and in C++11,
8822 as a lambda-expression, eg, '(void)[]{}'. */
8823 if (cxx_dialect >= cxx11)
8825 return c_dialect_objc ();
8828 case CPP_MINUS_MINUS:
8829 /* '++' and '--' may or may not start a cast-expression:
8831 struct T { void operator++(int); };
8832 void f() { (T())++; }
8845 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8846 in the order: const_cast, static_cast, reinterpret_cast.
8848 Don't suggest dynamic_cast.
8850 Return the first legal cast kind found, or NULL otherwise. */
8853 get_cast_suggestion (tree dst_type, tree orig_expr)
8857 /* Reuse the parser logic by attempting to build the various kinds of
8858 cast, with "complain" disabled.
8859 Identify the first such cast that is valid. */
8861 /* Don't attempt to run such logic within template processing. */
8862 if (processing_template_decl)
8865 /* First try const_cast. */
8866 trial = build_const_cast (dst_type, orig_expr, tf_none);
8867 if (trial != error_mark_node)
8868 return "const_cast";
8870 /* If that fails, try static_cast. */
8871 trial = build_static_cast (dst_type, orig_expr, tf_none);
8872 if (trial != error_mark_node)
8873 return "static_cast";
8875 /* Finally, try reinterpret_cast. */
8876 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8877 if (trial != error_mark_node)
8878 return "reinterpret_cast";
8880 /* No such cast possible. */
8884 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8885 suggesting how to convert a C-style cast of the form:
8889 to a C++-style cast.
8891 The primary range of RICHLOC is asssumed to be that of the original
8892 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8893 of the parens in the C-style cast. */
8896 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8897 location_t close_paren_loc, tree orig_expr,
8900 /* This function is non-trivial, so bail out now if the warning isn't
8901 going to be emitted. */
8902 if (!warn_old_style_cast)
8905 /* Try to find a legal C++ cast, trying them in order:
8906 const_cast, static_cast, reinterpret_cast. */
8907 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8908 if (!cast_suggestion)
8911 /* Replace the open paren with "CAST_SUGGESTION<". */
8913 pp_printf (&pp, "%s<", cast_suggestion);
8914 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8916 /* Replace the close paren with "> (". */
8917 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8919 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8920 rich_loc->add_fixit_insert_after (")");
8924 /* Parse a cast-expression.
8928 ( type-id ) cast-expression
8930 ADDRESS_P is true iff the unary-expression is appearing as the
8931 operand of the `&' operator. CAST_P is true if this expression is
8932 the target of a cast.
8934 Returns a representation of the expression. */
8937 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8938 bool decltype_p, cp_id_kind * pidk)
8940 /* If it's a `(', then we might be looking at a cast. */
8941 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8943 tree type = NULL_TREE;
8944 cp_expr expr (NULL_TREE);
8945 int cast_expression = 0;
8946 const char *saved_message;
8948 /* There's no way to know yet whether or not this is a cast.
8949 For example, `(int (3))' is a unary-expression, while `(int)
8950 3' is a cast. So, we resort to parsing tentatively. */
8951 cp_parser_parse_tentatively (parser);
8952 /* Types may not be defined in a cast. */
8953 saved_message = parser->type_definition_forbidden_message;
8954 parser->type_definition_forbidden_message
8955 = G_("types may not be defined in casts");
8956 /* Consume the `('. */
8957 matching_parens parens;
8958 cp_token *open_paren = parens.consume_open (parser);
8959 location_t open_paren_loc = open_paren->location;
8960 location_t close_paren_loc = UNKNOWN_LOCATION;
8962 /* A very tricky bit is that `(struct S) { 3 }' is a
8963 compound-literal (which we permit in C++ as an extension).
8964 But, that construct is not a cast-expression -- it is a
8965 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8966 is legal; if the compound-literal were a cast-expression,
8967 you'd need an extra set of parentheses.) But, if we parse
8968 the type-id, and it happens to be a class-specifier, then we
8969 will commit to the parse at that point, because we cannot
8970 undo the action that is done when creating a new class. So,
8971 then we cannot back up and do a postfix-expression.
8973 Another tricky case is the following (c++/29234):
8975 struct S { void operator () (); };
8982 As a type-id we parse the parenthesized S()() as a function
8983 returning a function, groktypename complains and we cannot
8984 back up in this case either.
8986 Therefore, we scan ahead to the closing `)', and check to see
8987 if the tokens after the `)' can start a cast-expression. Otherwise
8988 we are dealing with an unary-expression, a postfix-expression
8991 Yet another tricky case, in C++11, is the following (c++/54891):
8995 The issue is that usually, besides the case of lambda-expressions,
8996 the parenthesized type-id cannot be followed by '[', and, eg, we
8997 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8998 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8999 we don't commit, we try a cast-expression, then an unary-expression.
9001 Save tokens so that we can put them back. */
9002 cp_lexer_save_tokens (parser->lexer);
9004 /* We may be looking at a cast-expression. */
9005 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9006 /*consume_paren=*/true))
9008 = cp_parser_tokens_start_cast_expression (parser);
9010 /* Roll back the tokens we skipped. */
9011 cp_lexer_rollback_tokens (parser->lexer);
9012 /* If we aren't looking at a cast-expression, simulate an error so
9013 that the call to cp_parser_error_occurred below returns true. */
9014 if (!cast_expression)
9015 cp_parser_simulate_error (parser);
9018 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9019 parser->in_type_id_in_expr_p = true;
9020 /* Look for the type-id. */
9021 type = cp_parser_type_id (parser);
9022 /* Look for the closing `)'. */
9023 cp_token *close_paren = parens.require_close (parser);
9025 close_paren_loc = close_paren->location;
9026 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9029 /* Restore the saved message. */
9030 parser->type_definition_forbidden_message = saved_message;
9032 /* At this point this can only be either a cast or a
9033 parenthesized ctor such as `(T ())' that looks like a cast to
9034 function returning T. */
9035 if (!cp_parser_error_occurred (parser))
9037 /* Only commit if the cast-expression doesn't start with
9038 '++', '--', or '[' in C++11. */
9039 if (cast_expression > 0)
9040 cp_parser_commit_to_topmost_tentative_parse (parser);
9042 expr = cp_parser_cast_expression (parser,
9043 /*address_p=*/false,
9045 /*decltype_p=*/false,
9048 if (cp_parser_parse_definitely (parser))
9050 /* Warn about old-style casts, if so requested. */
9051 if (warn_old_style_cast
9052 && !in_system_header_at (input_location)
9053 && !VOID_TYPE_P (type)
9054 && current_lang_name != lang_name_c)
9056 gcc_rich_location rich_loc (input_location);
9057 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9059 warning_at (&rich_loc, OPT_Wold_style_cast,
9060 "use of old-style cast to %q#T", type);
9063 /* Only type conversions to integral or enumeration types
9064 can be used in constant-expressions. */
9065 if (!cast_valid_in_integral_constant_expression_p (type)
9066 && cp_parser_non_integral_constant_expression (parser,
9068 return error_mark_node;
9070 /* Perform the cast. */
9074 with start==caret at the open paren, extending to the
9076 location_t cast_loc = make_location (open_paren_loc,
9078 expr.get_finish ());
9079 expr = build_c_cast (cast_loc, type, expr);
9084 cp_parser_abort_tentative_parse (parser);
9087 /* If we get here, then it's not a cast, so it must be a
9088 unary-expression. */
9089 return cp_parser_unary_expression (parser, pidk, address_p,
9090 cast_p, decltype_p);
9093 /* Parse a binary expression of the general form:
9097 pm-expression .* cast-expression
9098 pm-expression ->* cast-expression
9100 multiplicative-expression:
9102 multiplicative-expression * pm-expression
9103 multiplicative-expression / pm-expression
9104 multiplicative-expression % pm-expression
9106 additive-expression:
9107 multiplicative-expression
9108 additive-expression + multiplicative-expression
9109 additive-expression - multiplicative-expression
9113 shift-expression << additive-expression
9114 shift-expression >> additive-expression
9116 relational-expression:
9118 relational-expression < shift-expression
9119 relational-expression > shift-expression
9120 relational-expression <= shift-expression
9121 relational-expression >= shift-expression
9125 relational-expression:
9126 relational-expression <? shift-expression
9127 relational-expression >? shift-expression
9129 equality-expression:
9130 relational-expression
9131 equality-expression == relational-expression
9132 equality-expression != relational-expression
9136 and-expression & equality-expression
9138 exclusive-or-expression:
9140 exclusive-or-expression ^ and-expression
9142 inclusive-or-expression:
9143 exclusive-or-expression
9144 inclusive-or-expression | exclusive-or-expression
9146 logical-and-expression:
9147 inclusive-or-expression
9148 logical-and-expression && inclusive-or-expression
9150 logical-or-expression:
9151 logical-and-expression
9152 logical-or-expression || logical-and-expression
9154 All these are implemented with a single function like:
9157 simple-cast-expression
9158 binary-expression <token> binary-expression
9160 CAST_P is true if this expression is the target of a cast.
9162 The binops_by_token map is used to get the tree codes for each <token> type.
9163 binary-expressions are associated according to a precedence table. */
9165 #define TOKEN_PRECEDENCE(token) \
9166 (((token->type == CPP_GREATER \
9167 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9168 && !parser->greater_than_is_operator_p) \
9169 ? PREC_NOT_OPERATOR \
9170 : binops_by_token[token->type].prec)
9173 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9174 bool no_toplevel_fold_p,
9176 enum cp_parser_prec prec,
9179 cp_parser_expression_stack stack;
9180 cp_parser_expression_stack_entry *sp = &stack[0];
9181 cp_parser_expression_stack_entry current;
9184 enum tree_code rhs_type;
9185 enum cp_parser_prec new_prec, lookahead_prec;
9188 /* Parse the first expression. */
9189 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9190 ? TRUTH_NOT_EXPR : ERROR_MARK);
9191 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9192 cast_p, decltype_p, pidk);
9193 current.prec = prec;
9195 if (cp_parser_error_occurred (parser))
9196 return error_mark_node;
9200 /* Get an operator token. */
9201 token = cp_lexer_peek_token (parser->lexer);
9203 if (warn_cxx11_compat
9204 && token->type == CPP_RSHIFT
9205 && !parser->greater_than_is_operator_p)
9207 if (warning_at (token->location, OPT_Wc__11_compat,
9208 "%<>>%> operator is treated"
9209 " as two right angle brackets in C++11"))
9210 inform (token->location,
9211 "suggest parentheses around %<>>%> expression");
9214 new_prec = TOKEN_PRECEDENCE (token);
9215 if (new_prec != PREC_NOT_OPERATOR
9216 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9217 /* This is a fold-expression; handle it later. */
9218 new_prec = PREC_NOT_OPERATOR;
9220 /* Popping an entry off the stack means we completed a subexpression:
9221 - either we found a token which is not an operator (`>' where it is not
9222 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9223 will happen repeatedly;
9224 - or, we found an operator which has lower priority. This is the case
9225 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9227 if (new_prec <= current.prec)
9236 current.tree_type = binops_by_token[token->type].tree_type;
9237 current.loc = token->location;
9239 /* We used the operator token. */
9240 cp_lexer_consume_token (parser->lexer);
9242 /* For "false && x" or "true || x", x will never be executed;
9243 disable warnings while evaluating it. */
9244 if (current.tree_type == TRUTH_ANDIF_EXPR)
9245 c_inhibit_evaluation_warnings +=
9246 cp_fully_fold (current.lhs) == truthvalue_false_node;
9247 else if (current.tree_type == TRUTH_ORIF_EXPR)
9248 c_inhibit_evaluation_warnings +=
9249 cp_fully_fold (current.lhs) == truthvalue_true_node;
9251 /* Extract another operand. It may be the RHS of this expression
9252 or the LHS of a new, higher priority expression. */
9253 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9254 ? TRUTH_NOT_EXPR : ERROR_MARK);
9255 rhs = cp_parser_simple_cast_expression (parser);
9257 /* Get another operator token. Look up its precedence to avoid
9258 building a useless (immediately popped) stack entry for common
9259 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9260 token = cp_lexer_peek_token (parser->lexer);
9261 lookahead_prec = TOKEN_PRECEDENCE (token);
9262 if (lookahead_prec != PREC_NOT_OPERATOR
9263 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9264 lookahead_prec = PREC_NOT_OPERATOR;
9265 if (lookahead_prec > new_prec)
9267 /* ... and prepare to parse the RHS of the new, higher priority
9268 expression. Since precedence levels on the stack are
9269 monotonically increasing, we do not have to care about
9274 current.lhs_type = rhs_type;
9275 current.prec = new_prec;
9276 new_prec = lookahead_prec;
9280 lookahead_prec = new_prec;
9281 /* If the stack is not empty, we have parsed into LHS the right side
9282 (`4' in the example above) of an expression we had suspended.
9283 We can use the information on the stack to recover the LHS (`3')
9284 from the stack together with the tree code (`MULT_EXPR'), and
9285 the precedence of the higher level subexpression
9286 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9287 which will be used to actually build the additive expression. */
9289 rhs_type = current.lhs_type;
9294 /* Undo the disabling of warnings done above. */
9295 if (current.tree_type == TRUTH_ANDIF_EXPR)
9296 c_inhibit_evaluation_warnings -=
9297 cp_fully_fold (current.lhs) == truthvalue_false_node;
9298 else if (current.tree_type == TRUTH_ORIF_EXPR)
9299 c_inhibit_evaluation_warnings -=
9300 cp_fully_fold (current.lhs) == truthvalue_true_node;
9302 if (warn_logical_not_paren
9303 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9304 && current.lhs_type == TRUTH_NOT_EXPR
9305 /* Avoid warning for !!x == y. */
9306 && (TREE_CODE (current.lhs) != NE_EXPR
9307 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9308 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9309 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9310 /* Avoid warning for !b == y where b is boolean. */
9311 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9312 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9314 /* Avoid warning for !!b == y where b is boolean. */
9315 && (!DECL_P (current.lhs)
9316 || TREE_TYPE (current.lhs) == NULL_TREE
9317 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9318 warn_logical_not_parentheses (current.loc, current.tree_type,
9319 current.lhs, maybe_constant_value (rhs));
9323 location_t combined_loc = make_location (current.loc,
9324 current.lhs.get_start (),
9327 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9328 ERROR_MARK for everything that is not a binary expression.
9329 This makes warn_about_parentheses miss some warnings that
9330 involve unary operators. For unary expressions we should
9331 pass the correct tree_code unless the unary expression was
9332 surrounded by parentheses.
9334 if (no_toplevel_fold_p
9335 && lookahead_prec <= current.prec
9338 if (current.lhs == error_mark_node || rhs == error_mark_node)
9339 current.lhs = error_mark_node;
9343 = build_min (current.tree_type,
9344 TREE_CODE_CLASS (current.tree_type)
9346 ? boolean_type_node : TREE_TYPE (current.lhs),
9347 current.lhs.get_value (), rhs.get_value ());
9348 SET_EXPR_LOCATION (current.lhs, combined_loc);
9353 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9354 current.lhs, current.lhs_type,
9355 rhs, rhs_type, &overload,
9356 complain_flags (decltype_p));
9357 /* TODO: build_x_binary_op doesn't always honor the location. */
9358 current.lhs.set_location (combined_loc);
9360 current.lhs_type = current.tree_type;
9362 /* If the binary operator required the use of an overloaded operator,
9363 then this expression cannot be an integral constant-expression.
9364 An overloaded operator can be used even if both operands are
9365 otherwise permissible in an integral constant-expression if at
9366 least one of the operands is of enumeration type. */
9369 && cp_parser_non_integral_constant_expression (parser,
9371 return error_mark_node;
9378 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9379 bool no_toplevel_fold_p,
9380 enum cp_parser_prec prec,
9383 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9384 /*decltype*/false, prec, pidk);
9387 /* Parse the `? expression : assignment-expression' part of a
9388 conditional-expression. The LOGICAL_OR_EXPR is the
9389 logical-or-expression that started the conditional-expression.
9390 Returns a representation of the entire conditional-expression.
9392 This routine is used by cp_parser_assignment_expression.
9394 ? expression : assignment-expression
9398 ? : assignment-expression */
9401 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9403 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9404 cp_expr assignment_expr;
9405 struct cp_token *token;
9406 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9408 /* Consume the `?' token. */
9409 cp_lexer_consume_token (parser->lexer);
9410 token = cp_lexer_peek_token (parser->lexer);
9411 if (cp_parser_allow_gnu_extensions_p (parser)
9412 && token->type == CPP_COLON)
9414 pedwarn (token->location, OPT_Wpedantic,
9415 "ISO C++ does not allow ?: with omitted middle operand");
9416 /* Implicit true clause. */
9418 c_inhibit_evaluation_warnings +=
9419 folded_logical_or_expr == truthvalue_true_node;
9420 warn_for_omitted_condop (token->location, logical_or_expr);
9424 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9425 parser->colon_corrects_to_scope_p = false;
9426 /* Parse the expression. */
9427 c_inhibit_evaluation_warnings +=
9428 folded_logical_or_expr == truthvalue_false_node;
9429 expr = cp_parser_expression (parser);
9430 c_inhibit_evaluation_warnings +=
9431 ((folded_logical_or_expr == truthvalue_true_node)
9432 - (folded_logical_or_expr == truthvalue_false_node));
9433 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9436 /* The next token should be a `:'. */
9437 cp_parser_require (parser, CPP_COLON, RT_COLON);
9438 /* Parse the assignment-expression. */
9439 assignment_expr = cp_parser_assignment_expression (parser);
9440 c_inhibit_evaluation_warnings -=
9441 folded_logical_or_expr == truthvalue_true_node;
9444 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9445 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9446 with the caret at the "?", ranging from the start of
9447 the logical_or_expr to the end of the assignment_expr. */
9448 loc = make_location (loc,
9449 logical_or_expr.get_start (),
9450 assignment_expr.get_finish ());
9452 /* Build the conditional-expression. */
9453 return build_x_conditional_expr (loc, logical_or_expr,
9456 tf_warning_or_error);
9459 /* Parse an assignment-expression.
9461 assignment-expression:
9462 conditional-expression
9463 logical-or-expression assignment-operator assignment_expression
9466 CAST_P is true if this expression is the target of a cast.
9467 DECLTYPE_P is true if this expression is the operand of decltype.
9469 Returns a representation for the expression. */
9472 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9473 bool cast_p, bool decltype_p)
9477 /* If the next token is the `throw' keyword, then we're looking at
9478 a throw-expression. */
9479 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9480 expr = cp_parser_throw_expression (parser);
9481 /* Otherwise, it must be that we are looking at a
9482 logical-or-expression. */
9485 /* Parse the binary expressions (logical-or-expression). */
9486 expr = cp_parser_binary_expression (parser, cast_p, false,
9488 PREC_NOT_OPERATOR, pidk);
9489 /* If the next token is a `?' then we're actually looking at a
9490 conditional-expression. */
9491 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9492 return cp_parser_question_colon_clause (parser, expr);
9495 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9497 /* If it's an assignment-operator, we're using the second
9499 enum tree_code assignment_operator
9500 = cp_parser_assignment_operator_opt (parser);
9501 if (assignment_operator != ERROR_MARK)
9503 bool non_constant_p;
9505 /* Parse the right-hand side of the assignment. */
9506 cp_expr rhs = cp_parser_initializer_clause (parser,
9509 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9510 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9512 /* An assignment may not appear in a
9513 constant-expression. */
9514 if (cp_parser_non_integral_constant_expression (parser,
9516 return error_mark_node;
9517 /* Build the assignment expression. Its default
9521 is the location of the '=' token as the
9522 caret, ranging from the start of the lhs to the
9524 loc = make_location (loc,
9527 expr = build_x_modify_expr (loc, expr,
9528 assignment_operator,
9530 complain_flags (decltype_p));
9531 /* TODO: build_x_modify_expr doesn't honor the location,
9532 so we must set it here. */
9533 expr.set_location (loc);
9541 /* Parse an (optional) assignment-operator.
9543 assignment-operator: one of
9544 = *= /= %= += -= >>= <<= &= ^= |=
9548 assignment-operator: one of
9551 If the next token is an assignment operator, the corresponding tree
9552 code is returned, and the token is consumed. For example, for
9553 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9554 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9555 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9556 operator, ERROR_MARK is returned. */
9558 static enum tree_code
9559 cp_parser_assignment_operator_opt (cp_parser* parser)
9564 /* Peek at the next token. */
9565 token = cp_lexer_peek_token (parser->lexer);
9567 switch (token->type)
9578 op = TRUNC_DIV_EXPR;
9582 op = TRUNC_MOD_EXPR;
9614 /* Nothing else is an assignment operator. */
9618 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9619 if (op != ERROR_MARK
9620 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9623 /* If it was an assignment operator, consume it. */
9624 if (op != ERROR_MARK)
9625 cp_lexer_consume_token (parser->lexer);
9630 /* Parse an expression.
9633 assignment-expression
9634 expression , assignment-expression
9636 CAST_P is true if this expression is the target of a cast.
9637 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9638 except possibly parenthesized or on the RHS of a comma (N3276).
9640 Returns a representation of the expression. */
9643 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9644 bool cast_p, bool decltype_p)
9646 cp_expr expression = NULL_TREE;
9647 location_t loc = UNKNOWN_LOCATION;
9651 cp_expr assignment_expression;
9653 /* Parse the next assignment-expression. */
9654 assignment_expression
9655 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9657 /* We don't create a temporary for a call that is the immediate operand
9658 of decltype or on the RHS of a comma. But when we see a comma, we
9659 need to create a temporary for a call on the LHS. */
9660 if (decltype_p && !processing_template_decl
9661 && TREE_CODE (assignment_expression) == CALL_EXPR
9662 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9663 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9664 assignment_expression
9665 = build_cplus_new (TREE_TYPE (assignment_expression),
9666 assignment_expression, tf_warning_or_error);
9668 /* If this is the first assignment-expression, we can just
9671 expression = assignment_expression;
9674 /* Create a location with caret at the comma, ranging
9675 from the start of the LHS to the end of the RHS. */
9676 loc = make_location (loc,
9677 expression.get_start (),
9678 assignment_expression.get_finish ());
9679 expression = build_x_compound_expr (loc, expression,
9680 assignment_expression,
9681 complain_flags (decltype_p));
9682 expression.set_location (loc);
9684 /* If the next token is not a comma, or we're in a fold-expression, then
9685 we are done with the expression. */
9686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9687 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9689 /* Consume the `,'. */
9690 loc = cp_lexer_peek_token (parser->lexer)->location;
9691 cp_lexer_consume_token (parser->lexer);
9692 /* A comma operator cannot appear in a constant-expression. */
9693 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9694 expression = error_mark_node;
9700 /* Parse a constant-expression.
9702 constant-expression:
9703 conditional-expression
9705 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9706 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9707 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9708 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9709 only parse a conditional-expression, otherwise parse an
9710 assignment-expression. See below for rationale. */
9713 cp_parser_constant_expression (cp_parser* parser,
9714 bool allow_non_constant_p,
9715 bool *non_constant_p,
9718 bool saved_integral_constant_expression_p;
9719 bool saved_allow_non_integral_constant_expression_p;
9720 bool saved_non_integral_constant_expression_p;
9723 /* It might seem that we could simply parse the
9724 conditional-expression, and then check to see if it were
9725 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9726 one that the compiler can figure out is constant, possibly after
9727 doing some simplifications or optimizations. The standard has a
9728 precise definition of constant-expression, and we must honor
9729 that, even though it is somewhat more restrictive.
9735 is not a legal declaration, because `(2, 3)' is not a
9736 constant-expression. The `,' operator is forbidden in a
9737 constant-expression. However, GCC's constant-folding machinery
9738 will fold this operation to an INTEGER_CST for `3'. */
9740 /* Save the old settings. */
9741 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9742 saved_allow_non_integral_constant_expression_p
9743 = parser->allow_non_integral_constant_expression_p;
9744 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9745 /* We are now parsing a constant-expression. */
9746 parser->integral_constant_expression_p = true;
9747 parser->allow_non_integral_constant_expression_p
9748 = (allow_non_constant_p || cxx_dialect >= cxx11);
9749 parser->non_integral_constant_expression_p = false;
9750 /* Although the grammar says "conditional-expression", when not STRICT_P,
9751 we parse an "assignment-expression", which also permits
9752 "throw-expression" and the use of assignment operators. In the case
9753 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9754 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9755 actually essential that we look for an assignment-expression.
9756 For example, cp_parser_initializer_clauses uses this function to
9757 determine whether a particular assignment-expression is in fact
9761 /* Parse the binary expressions (logical-or-expression). */
9762 expression = cp_parser_binary_expression (parser, false, false, false,
9763 PREC_NOT_OPERATOR, NULL);
9764 /* If the next token is a `?' then we're actually looking at
9765 a conditional-expression; otherwise we're done. */
9766 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9767 expression = cp_parser_question_colon_clause (parser, expression);
9770 expression = cp_parser_assignment_expression (parser);
9771 /* Restore the old settings. */
9772 parser->integral_constant_expression_p
9773 = saved_integral_constant_expression_p;
9774 parser->allow_non_integral_constant_expression_p
9775 = saved_allow_non_integral_constant_expression_p;
9776 if (cxx_dialect >= cxx11)
9778 /* Require an rvalue constant expression here; that's what our
9779 callers expect. Reference constant expressions are handled
9780 separately in e.g. cp_parser_template_argument. */
9781 tree decay = expression;
9782 if (TREE_TYPE (expression)
9783 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9784 decay = build_address (expression);
9785 bool is_const = potential_rvalue_constant_expression (decay);
9786 parser->non_integral_constant_expression_p = !is_const;
9787 if (!is_const && !allow_non_constant_p)
9788 require_potential_rvalue_constant_expression (decay);
9790 if (allow_non_constant_p)
9791 *non_constant_p = parser->non_integral_constant_expression_p;
9792 parser->non_integral_constant_expression_p
9793 = saved_non_integral_constant_expression_p;
9798 /* Parse __builtin_offsetof.
9800 offsetof-expression:
9801 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9803 offsetof-member-designator:
9805 | offsetof-member-designator "." id-expression
9806 | offsetof-member-designator "[" expression "]"
9807 | offsetof-member-designator "->" id-expression */
9810 cp_parser_builtin_offsetof (cp_parser *parser)
9812 int save_ice_p, save_non_ice_p;
9817 location_t finish_loc;
9819 /* We're about to accept non-integral-constant things, but will
9820 definitely yield an integral constant expression. Save and
9821 restore these values around our local parsing. */
9822 save_ice_p = parser->integral_constant_expression_p;
9823 save_non_ice_p = parser->non_integral_constant_expression_p;
9825 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9827 /* Consume the "__builtin_offsetof" token. */
9828 cp_lexer_consume_token (parser->lexer);
9829 /* Consume the opening `('. */
9830 matching_parens parens;
9831 parens.require_open (parser);
9832 /* Parse the type-id. */
9833 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9835 const char *saved_message = parser->type_definition_forbidden_message;
9836 parser->type_definition_forbidden_message
9837 = G_("types may not be defined within __builtin_offsetof");
9838 type = cp_parser_type_id (parser);
9839 parser->type_definition_forbidden_message = saved_message;
9841 /* Look for the `,'. */
9842 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9843 token = cp_lexer_peek_token (parser->lexer);
9845 /* Build the (type *)null that begins the traditional offsetof macro. */
9847 = build_static_cast (build_pointer_type (type), null_pointer_node,
9848 tf_warning_or_error);
9850 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9851 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9852 true, &dummy, token->location);
9855 token = cp_lexer_peek_token (parser->lexer);
9856 switch (token->type)
9858 case CPP_OPEN_SQUARE:
9859 /* offsetof-member-designator "[" expression "]" */
9860 expr = cp_parser_postfix_open_square_expression (parser, expr,
9865 /* offsetof-member-designator "->" identifier */
9866 expr = grok_array_decl (token->location, expr,
9867 integer_zero_node, false);
9871 /* offsetof-member-designator "." identifier */
9872 cp_lexer_consume_token (parser->lexer);
9873 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9878 case CPP_CLOSE_PAREN:
9879 /* Consume the ")" token. */
9880 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9881 cp_lexer_consume_token (parser->lexer);
9885 /* Error. We know the following require will fail, but
9886 that gives the proper error message. */
9887 parens.require_close (parser);
9888 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9889 expr = error_mark_node;
9895 /* Make a location of the form:
9896 __builtin_offsetof (struct s, f)
9897 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9898 with caret at the type-id, ranging from the start of the
9899 "_builtin_offsetof" token to the close paren. */
9900 loc = make_location (loc, start_loc, finish_loc);
9901 /* The result will be an INTEGER_CST, so we need to explicitly
9902 preserve the location. */
9903 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9906 parser->integral_constant_expression_p = save_ice_p;
9907 parser->non_integral_constant_expression_p = save_non_ice_p;
9909 expr = expr.maybe_add_location_wrapper ();
9913 /* Parse a trait expression.
9915 Returns a representation of the expression, the underlying type
9916 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9919 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9922 tree type1, type2 = NULL_TREE;
9923 bool binary = false;
9924 bool variadic = false;
9928 case RID_HAS_NOTHROW_ASSIGN:
9929 kind = CPTK_HAS_NOTHROW_ASSIGN;
9931 case RID_HAS_NOTHROW_CONSTRUCTOR:
9932 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9934 case RID_HAS_NOTHROW_COPY:
9935 kind = CPTK_HAS_NOTHROW_COPY;
9937 case RID_HAS_TRIVIAL_ASSIGN:
9938 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9940 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9941 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9943 case RID_HAS_TRIVIAL_COPY:
9944 kind = CPTK_HAS_TRIVIAL_COPY;
9946 case RID_HAS_TRIVIAL_DESTRUCTOR:
9947 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9949 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9950 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9952 case RID_HAS_VIRTUAL_DESTRUCTOR:
9953 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9955 case RID_IS_ABSTRACT:
9956 kind = CPTK_IS_ABSTRACT;
9958 case RID_IS_AGGREGATE:
9959 kind = CPTK_IS_AGGREGATE;
9961 case RID_IS_BASE_OF:
9962 kind = CPTK_IS_BASE_OF;
9966 kind = CPTK_IS_CLASS;
9969 kind = CPTK_IS_EMPTY;
9972 kind = CPTK_IS_ENUM;
9975 kind = CPTK_IS_FINAL;
9977 case RID_IS_LITERAL_TYPE:
9978 kind = CPTK_IS_LITERAL_TYPE;
9983 case RID_IS_POLYMORPHIC:
9984 kind = CPTK_IS_POLYMORPHIC;
9986 case RID_IS_SAME_AS:
9987 kind = CPTK_IS_SAME_AS;
9990 case RID_IS_STD_LAYOUT:
9991 kind = CPTK_IS_STD_LAYOUT;
9993 case RID_IS_TRIVIAL:
9994 kind = CPTK_IS_TRIVIAL;
9996 case RID_IS_TRIVIALLY_ASSIGNABLE:
9997 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10000 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10001 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10004 case RID_IS_TRIVIALLY_COPYABLE:
10005 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10008 kind = CPTK_IS_UNION;
10010 case RID_UNDERLYING_TYPE:
10011 kind = CPTK_UNDERLYING_TYPE;
10016 case RID_DIRECT_BASES:
10017 kind = CPTK_DIRECT_BASES;
10019 case RID_IS_ASSIGNABLE:
10020 kind = CPTK_IS_ASSIGNABLE;
10023 case RID_IS_CONSTRUCTIBLE:
10024 kind = CPTK_IS_CONSTRUCTIBLE;
10028 gcc_unreachable ();
10031 /* Get location of initial token. */
10032 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10034 /* Consume the token. */
10035 cp_lexer_consume_token (parser->lexer);
10037 matching_parens parens;
10038 parens.require_open (parser);
10041 type_id_in_expr_sentinel s (parser);
10042 type1 = cp_parser_type_id (parser);
10045 if (type1 == error_mark_node)
10046 return error_mark_node;
10050 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10053 type_id_in_expr_sentinel s (parser);
10054 type2 = cp_parser_type_id (parser);
10057 if (type2 == error_mark_node)
10058 return error_mark_node;
10062 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10064 cp_lexer_consume_token (parser->lexer);
10065 tree elt = cp_parser_type_id (parser);
10066 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10068 cp_lexer_consume_token (parser->lexer);
10069 elt = make_pack_expansion (elt);
10071 if (elt == error_mark_node)
10072 return error_mark_node;
10073 type2 = tree_cons (NULL_TREE, elt, type2);
10077 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10078 parens.require_close (parser);
10080 /* Construct a location of the form:
10081 __is_trivially_copyable(_Tp)
10082 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10083 with start == caret, finishing at the close-paren. */
10084 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10086 /* Complete the trait expression, which may mean either processing
10087 the trait expr now or saving it for template instantiation. */
10090 case CPTK_UNDERLYING_TYPE:
10091 return cp_expr (finish_underlying_type (type1), trait_loc);
10093 return cp_expr (finish_bases (type1, false), trait_loc);
10094 case CPTK_DIRECT_BASES:
10095 return cp_expr (finish_bases (type1, true), trait_loc);
10097 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10101 /* Parse a lambda expression.
10104 lambda-introducer lambda-declarator [opt] compound-statement
10106 Returns a representation of the expression. */
10109 cp_parser_lambda_expression (cp_parser* parser)
10111 tree lambda_expr = build_lambda_expr ();
10114 cp_token *token = cp_lexer_peek_token (parser->lexer);
10115 cp_token_position start = 0;
10117 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10119 if (cp_unevaluated_operand)
10121 if (!token->error_reported)
10123 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10124 "lambda-expression in unevaluated context");
10125 token->error_reported = true;
10129 else if (parser->in_template_argument_list_p)
10131 if (!token->error_reported)
10133 error_at (token->location, "lambda-expression in template-argument");
10134 token->error_reported = true;
10139 /* We may be in the middle of deferred access check. Disable
10141 push_deferring_access_checks (dk_no_deferred);
10143 cp_parser_lambda_introducer (parser, lambda_expr);
10145 type = begin_lambda_type (lambda_expr);
10146 if (type == error_mark_node)
10147 return error_mark_node;
10149 record_lambda_scope (lambda_expr);
10151 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10152 determine_visibility (TYPE_NAME (type));
10154 /* Now that we've started the type, add the capture fields for any
10155 explicit captures. */
10156 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10159 /* Inside the class, surrounding template-parameter-lists do not apply. */
10160 unsigned int saved_num_template_parameter_lists
10161 = parser->num_template_parameter_lists;
10162 unsigned char in_statement = parser->in_statement;
10163 bool in_switch_statement_p = parser->in_switch_statement_p;
10164 bool fully_implicit_function_template_p
10165 = parser->fully_implicit_function_template_p;
10166 tree implicit_template_parms = parser->implicit_template_parms;
10167 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10168 bool auto_is_implicit_function_template_parm_p
10169 = parser->auto_is_implicit_function_template_parm_p;
10171 parser->num_template_parameter_lists = 0;
10172 parser->in_statement = 0;
10173 parser->in_switch_statement_p = false;
10174 parser->fully_implicit_function_template_p = false;
10175 parser->implicit_template_parms = 0;
10176 parser->implicit_template_scope = 0;
10177 parser->auto_is_implicit_function_template_parm_p = false;
10179 /* By virtue of defining a local class, a lambda expression has access to
10180 the private variables of enclosing classes. */
10182 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10184 if (ok && cp_parser_error_occurred (parser))
10189 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10190 && cp_parser_start_tentative_firewall (parser))
10192 cp_parser_lambda_body (parser, lambda_expr);
10194 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10196 if (cp_parser_skip_to_closing_brace (parser))
10197 cp_lexer_consume_token (parser->lexer);
10200 /* The capture list was built up in reverse order; fix that now. */
10201 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10202 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10205 maybe_add_lambda_conv_op (type);
10207 type = finish_struct (type, /*attributes=*/NULL_TREE);
10209 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10210 parser->in_statement = in_statement;
10211 parser->in_switch_statement_p = in_switch_statement_p;
10212 parser->fully_implicit_function_template_p
10213 = fully_implicit_function_template_p;
10214 parser->implicit_template_parms = implicit_template_parms;
10215 parser->implicit_template_scope = implicit_template_scope;
10216 parser->auto_is_implicit_function_template_parm_p
10217 = auto_is_implicit_function_template_parm_p;
10220 /* This field is only used during parsing of the lambda. */
10221 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10223 /* This lambda shouldn't have any proxies left at this point. */
10224 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10225 /* And now that we're done, push proxies for an enclosing lambda. */
10226 insert_pending_capture_proxies ();
10229 lambda_expr = build_lambda_object (lambda_expr);
10231 lambda_expr = error_mark_node;
10233 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10235 pop_deferring_access_checks ();
10237 return lambda_expr;
10240 /* Parse the beginning of a lambda expression.
10243 [ lambda-capture [opt] ]
10245 LAMBDA_EXPR is the current representation of the lambda expression. */
10248 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10250 /* Need commas after the first capture. */
10253 /* Eat the leading `['. */
10254 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10256 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10257 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10258 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10259 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10260 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10261 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10263 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10265 cp_lexer_consume_token (parser->lexer);
10269 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10271 cp_token* capture_token;
10273 tree capture_init_expr;
10274 cp_id_kind idk = CP_ID_KIND_NONE;
10275 bool explicit_init_p = false;
10277 enum capture_kind_type
10282 enum capture_kind_type capture_kind = BY_COPY;
10284 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10286 error ("expected end of capture-list");
10293 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10295 /* Possibly capture `this'. */
10296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10298 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10299 if (cxx_dialect < cxx2a
10300 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10301 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10302 "with by-copy capture default");
10303 cp_lexer_consume_token (parser->lexer);
10304 add_capture (lambda_expr,
10305 /*id=*/this_identifier,
10306 /*initializer=*/finish_this_expr (),
10307 /*by_reference_p=*/true,
10312 /* Possibly capture `*this'. */
10313 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10314 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10316 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10317 if (cxx_dialect < cxx17)
10318 pedwarn (loc, 0, "%<*this%> capture only available with "
10319 "-std=c++17 or -std=gnu++17");
10320 cp_lexer_consume_token (parser->lexer);
10321 cp_lexer_consume_token (parser->lexer);
10322 add_capture (lambda_expr,
10323 /*id=*/this_identifier,
10324 /*initializer=*/finish_this_expr (),
10325 /*by_reference_p=*/false,
10330 /* Remember whether we want to capture as a reference or not. */
10331 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10333 capture_kind = BY_REFERENCE;
10334 cp_lexer_consume_token (parser->lexer);
10337 /* Get the identifier. */
10338 capture_token = cp_lexer_peek_token (parser->lexer);
10339 capture_id = cp_parser_identifier (parser);
10341 if (capture_id == error_mark_node)
10342 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10343 delimiters, but I modified this to stop on unnested ']' as well. It
10344 was already changed to stop on unnested '}', so the
10345 "closing_parenthesis" name is no more misleading with my change. */
10347 cp_parser_skip_to_closing_parenthesis (parser,
10348 /*recovering=*/true,
10350 /*consume_paren=*/true);
10354 /* Find the initializer for this capture. */
10355 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10356 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10357 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10359 bool direct, non_constant;
10360 /* An explicit initializer exists. */
10361 if (cxx_dialect < cxx14)
10362 pedwarn (input_location, 0,
10363 "lambda capture initializers "
10364 "only available with -std=c++14 or -std=gnu++14");
10365 capture_init_expr = cp_parser_initializer (parser, &direct,
10366 &non_constant, true);
10367 explicit_init_p = true;
10368 if (capture_init_expr == NULL_TREE)
10370 error ("empty initializer for lambda init-capture");
10371 capture_init_expr = error_mark_node;
10376 const char* error_msg;
10378 /* Turn the identifier into an id-expression. */
10380 = cp_parser_lookup_name_simple (parser, capture_id,
10381 capture_token->location);
10383 if (capture_init_expr == error_mark_node)
10385 unqualified_name_lookup_error (capture_id);
10388 else if (!VAR_P (capture_init_expr)
10389 && TREE_CODE (capture_init_expr) != PARM_DECL)
10391 error_at (capture_token->location,
10392 "capture of non-variable %qE",
10393 capture_init_expr);
10394 if (DECL_P (capture_init_expr))
10395 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10396 "%q#D declared here", capture_init_expr);
10399 if (VAR_P (capture_init_expr)
10400 && decl_storage_duration (capture_init_expr) != dk_auto)
10402 if (pedwarn (capture_token->location, 0, "capture of variable "
10403 "%qD with non-automatic storage duration",
10404 capture_init_expr))
10405 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10406 "%q#D declared here", capture_init_expr);
10411 = finish_id_expression
10416 /*integral_constant_expression_p=*/false,
10417 /*allow_non_integral_constant_expression_p=*/false,
10418 /*non_integral_constant_expression_p=*/NULL,
10419 /*template_p=*/false,
10421 /*address_p=*/false,
10422 /*template_arg_p=*/false,
10424 capture_token->location);
10426 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10428 cp_lexer_consume_token (parser->lexer);
10429 capture_init_expr = make_pack_expansion (capture_init_expr);
10433 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10434 && !explicit_init_p)
10436 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10437 && capture_kind == BY_COPY)
10438 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10439 "of %qD redundant with by-copy capture default",
10441 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10442 && capture_kind == BY_REFERENCE)
10443 pedwarn (capture_token->location, 0, "explicit by-reference "
10444 "capture of %qD redundant with by-reference capture "
10445 "default", capture_id);
10448 add_capture (lambda_expr,
10451 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10454 /* If there is any qualification still in effect, clear it
10455 now; we will be starting fresh with the next capture. */
10456 parser->scope = NULL_TREE;
10457 parser->qualifying_scope = NULL_TREE;
10458 parser->object_scope = NULL_TREE;
10461 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10464 /* Parse the (optional) middle of a lambda expression.
10467 < template-parameter-list [opt] >
10468 ( parameter-declaration-clause [opt] )
10469 attribute-specifier [opt]
10470 decl-specifier-seq [opt]
10471 exception-specification [opt]
10472 lambda-return-type-clause [opt]
10474 LAMBDA_EXPR is the current representation of the lambda expression. */
10477 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10479 /* 5.1.1.4 of the standard says:
10480 If a lambda-expression does not include a lambda-declarator, it is as if
10481 the lambda-declarator were ().
10482 This means an empty parameter list, no attributes, and no exception
10484 tree param_list = void_list_node;
10485 tree attributes = NULL_TREE;
10486 tree exception_spec = NULL_TREE;
10487 tree template_param_list = NULL_TREE;
10488 tree tx_qual = NULL_TREE;
10489 tree return_type = NULL_TREE;
10490 cp_decl_specifier_seq lambda_specs;
10491 clear_decl_specs (&lambda_specs);
10493 /* The template-parameter-list is optional, but must begin with
10494 an opening angle if present. */
10495 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10497 if (cxx_dialect < cxx14)
10498 pedwarn (parser->lexer->next_token->location, 0,
10499 "lambda templates are only available with "
10500 "-std=c++14 or -std=gnu++14");
10501 else if (cxx_dialect < cxx2a)
10502 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10503 "lambda templates are only available with "
10504 "-std=c++2a or -std=gnu++2a");
10506 cp_lexer_consume_token (parser->lexer);
10508 template_param_list = cp_parser_template_parameter_list (parser);
10510 cp_parser_skip_to_end_of_template_parameter_list (parser);
10512 /* We just processed one more parameter list. */
10513 ++parser->num_template_parameter_lists;
10516 /* The parameter-declaration-clause is optional (unless
10517 template-parameter-list was given), but must begin with an
10518 opening parenthesis if present. */
10519 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10521 matching_parens parens;
10522 parens.consume_open (parser);
10524 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10526 /* Parse parameters. */
10527 param_list = cp_parser_parameter_declaration_clause (parser);
10529 /* Default arguments shall not be specified in the
10530 parameter-declaration-clause of a lambda-declarator. */
10531 if (cxx_dialect < cxx14)
10532 for (tree t = param_list; t; t = TREE_CHAIN (t))
10533 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10534 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10535 "default argument specified for lambda parameter");
10537 parens.require_close (parser);
10539 attributes = cp_parser_attributes_opt (parser);
10541 /* In the decl-specifier-seq of the lambda-declarator, each
10542 decl-specifier shall either be mutable or constexpr. */
10543 int declares_class_or_enum;
10544 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10545 cp_parser_decl_specifier_seq (parser,
10546 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10547 &lambda_specs, &declares_class_or_enum);
10548 if (lambda_specs.storage_class == sc_mutable)
10550 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10551 if (lambda_specs.conflicting_specifiers_p)
10552 error_at (lambda_specs.locations[ds_storage_class],
10553 "duplicate %<mutable%>");
10556 tx_qual = cp_parser_tx_qualifier_opt (parser);
10558 /* Parse optional exception specification. */
10559 exception_spec = cp_parser_exception_specification_opt (parser);
10561 /* Parse optional trailing return type. */
10562 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10564 cp_lexer_consume_token (parser->lexer);
10565 return_type = cp_parser_trailing_type_id (parser);
10568 /* The function parameters must be in scope all the way until after the
10569 trailing-return-type in case of decltype. */
10570 pop_bindings_and_leave_scope ();
10572 else if (template_param_list != NULL_TREE) // generate diagnostic
10573 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10575 /* Create the function call operator.
10577 Messing with declarators like this is no uglier than building up the
10578 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10581 cp_decl_specifier_seq return_type_specs;
10582 cp_declarator* declarator;
10587 clear_decl_specs (&return_type_specs);
10589 return_type_specs.type = return_type;
10591 /* Maybe we will deduce the return type later. */
10592 return_type_specs.type = make_auto ();
10594 if (lambda_specs.locations[ds_constexpr])
10596 if (cxx_dialect >= cxx17)
10597 return_type_specs.locations[ds_constexpr]
10598 = lambda_specs.locations[ds_constexpr];
10600 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10601 "lambda only available with -std=c++17 or -std=gnu++17");
10604 p = obstack_alloc (&declarator_obstack, 0);
10606 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10608 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10609 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10610 declarator = make_call_declarator (declarator, param_list, quals,
10611 VIRT_SPEC_UNSPECIFIED,
10615 /*late_return_type=*/NULL_TREE,
10616 /*requires_clause*/NULL_TREE);
10617 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10619 fco = grokmethod (&return_type_specs,
10622 if (fco != error_mark_node)
10624 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10625 DECL_ARTIFICIAL (fco) = 1;
10626 /* Give the object parameter a different name. */
10627 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10628 DECL_LAMBDA_FUNCTION (fco) = 1;
10630 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10632 if (template_param_list)
10634 fco = finish_member_template_decl (fco);
10635 finish_template_decl (template_param_list);
10636 --parser->num_template_parameter_lists;
10638 else if (parser->fully_implicit_function_template_p)
10639 fco = finish_fully_implicit_template (parser, fco);
10641 finish_member_declaration (fco);
10643 obstack_free (&declarator_obstack, p);
10645 return (fco != error_mark_node);
10649 /* Parse the body of a lambda expression, which is simply
10653 but which requires special handling.
10654 LAMBDA_EXPR is the current representation of the lambda expression. */
10657 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10659 bool nested = (current_function_decl != NULL_TREE);
10660 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10661 bool in_function_body = parser->in_function_body;
10664 push_function_context ();
10666 /* Still increment function_depth so that we don't GC in the
10667 middle of an expression. */
10670 vec<tree> omp_privatization_save;
10671 save_omp_privatization_clauses (omp_privatization_save);
10672 /* Clear this in case we're in the middle of a default argument. */
10673 parser->local_variables_forbidden_p = false;
10674 parser->in_function_body = true;
10677 local_specialization_stack s (lss_copy);
10678 tree fco = lambda_function (lambda_expr);
10679 tree body = start_lambda_function (fco, lambda_expr);
10680 matching_braces braces;
10682 if (braces.require_open (parser))
10684 tree compound_stmt = begin_compound_stmt (0);
10686 /* Originally C++11 required us to peek for 'return expr'; and
10687 process it specially here to deduce the return type. N3638
10688 removed the need for that. */
10690 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10691 cp_parser_label_declaration (parser);
10692 cp_parser_statement_seq_opt (parser, NULL_TREE);
10693 braces.require_close (parser);
10695 finish_compound_stmt (compound_stmt);
10698 finish_lambda_function (body);
10701 restore_omp_privatization_clauses (omp_privatization_save);
10702 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10703 parser->in_function_body = in_function_body;
10705 pop_function_context();
10710 /* Statements [gram.stmt.stmt] */
10712 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10715 add_debug_begin_stmt (location_t loc)
10717 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10719 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10720 /* A concept is never expanded normally. */
10723 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10724 SET_EXPR_LOCATION (stmt, loc);
10728 /* Parse a statement.
10732 expression-statement
10734 selection-statement
10735 iteration-statement
10737 declaration-statement
10744 attribute-specifier-seq (opt) expression-statement
10745 attribute-specifier-seq (opt) compound-statement
10746 attribute-specifier-seq (opt) selection-statement
10747 attribute-specifier-seq (opt) iteration-statement
10748 attribute-specifier-seq (opt) jump-statement
10749 declaration-statement
10750 attribute-specifier-seq (opt) try-block
10753 expression-statement
10761 IN_COMPOUND is true when the statement is nested inside a
10762 cp_parser_compound_statement; this matters for certain pragmas.
10764 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10765 is a (possibly labeled) if statement which is not enclosed in braces
10766 and has an else clause. This is used to implement -Wparentheses.
10768 CHAIN is a vector of if-else-if conditions. */
10771 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10772 bool in_compound, bool *if_p, vec<tree> *chain,
10773 location_t *loc_after_labels)
10775 tree statement, std_attrs = NULL_TREE;
10777 location_t statement_location, attrs_location;
10782 /* There is no statement yet. */
10783 statement = NULL_TREE;
10785 saved_token_sentinel saved_tokens (parser->lexer);
10786 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10787 if (c_dialect_objc ())
10788 /* In obj-c++, seeing '[[' might be the either the beginning of
10789 c++11 attributes, or a nested objc-message-expression. So
10790 let's parse the c++11 attributes tentatively. */
10791 cp_parser_parse_tentatively (parser);
10792 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10793 if (c_dialect_objc ())
10795 if (!cp_parser_parse_definitely (parser))
10796 std_attrs = NULL_TREE;
10799 /* Peek at the next token. */
10800 token = cp_lexer_peek_token (parser->lexer);
10801 /* Remember the location of the first token in the statement. */
10802 statement_location = token->location;
10803 add_debug_begin_stmt (statement_location);
10804 /* If this is a keyword, then that will often determine what kind of
10805 statement we have. */
10806 if (token->type == CPP_KEYWORD)
10808 enum rid keyword = token->keyword;
10814 /* Looks like a labeled-statement with a case label.
10815 Parse the label, and then use tail recursion to parse
10817 cp_parser_label_for_labeled_statement (parser, std_attrs);
10818 in_compound = false;
10823 statement = cp_parser_selection_statement (parser, if_p, chain);
10829 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10836 statement = cp_parser_jump_statement (parser);
10839 /* Objective-C++ exception-handling constructs. */
10842 case RID_AT_FINALLY:
10843 case RID_AT_SYNCHRONIZED:
10845 statement = cp_parser_objc_statement (parser);
10849 statement = cp_parser_try_block (parser);
10852 case RID_NAMESPACE:
10853 /* This must be a namespace alias definition. */
10854 cp_parser_declaration_statement (parser);
10857 case RID_TRANSACTION_ATOMIC:
10858 case RID_TRANSACTION_RELAXED:
10859 case RID_SYNCHRONIZED:
10860 case RID_ATOMIC_NOEXCEPT:
10861 case RID_ATOMIC_CANCEL:
10862 statement = cp_parser_transaction (parser, token);
10864 case RID_TRANSACTION_CANCEL:
10865 statement = cp_parser_transaction_cancel (parser);
10869 /* It might be a keyword like `int' that can start a
10870 declaration-statement. */
10874 else if (token->type == CPP_NAME)
10876 /* If the next token is a `:', then we are looking at a
10877 labeled-statement. */
10878 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10879 if (token->type == CPP_COLON)
10881 /* Looks like a labeled-statement with an ordinary label.
10882 Parse the label, and then use tail recursion to parse
10885 cp_parser_label_for_labeled_statement (parser, std_attrs);
10886 in_compound = false;
10890 /* Anything that starts with a `{' must be a compound-statement. */
10891 else if (token->type == CPP_OPEN_BRACE)
10892 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10893 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10894 a statement all its own. */
10895 else if (token->type == CPP_PRAGMA)
10897 /* Only certain OpenMP pragmas are attached to statements, and thus
10898 are considered statements themselves. All others are not. In
10899 the context of a compound, accept the pragma as a "statement" and
10900 return so that we can check for a close brace. Otherwise we
10901 require a real statement and must go back and read one. */
10903 cp_parser_pragma (parser, pragma_compound, if_p);
10904 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10908 else if (token->type == CPP_EOF)
10910 cp_parser_error (parser, "expected statement");
10914 /* Everything else must be a declaration-statement or an
10915 expression-statement. Try for the declaration-statement
10916 first, unless we are looking at a `;', in which case we know that
10917 we have an expression-statement. */
10920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10922 if (std_attrs != NULL_TREE)
10924 /* Attributes should be parsed as part of the the
10925 declaration, so let's un-parse them. */
10926 saved_tokens.rollback();
10927 std_attrs = NULL_TREE;
10930 cp_parser_parse_tentatively (parser);
10931 /* Try to parse the declaration-statement. */
10932 cp_parser_declaration_statement (parser);
10933 /* If that worked, we're done. */
10934 if (cp_parser_parse_definitely (parser))
10937 /* All preceding labels have been parsed at this point. */
10938 if (loc_after_labels != NULL)
10939 *loc_after_labels = statement_location;
10941 /* Look for an expression-statement instead. */
10942 statement = cp_parser_expression_statement (parser, in_statement_expr);
10944 /* Handle [[fallthrough]];. */
10945 if (attribute_fallthrough_p (std_attrs))
10947 /* The next token after the fallthrough attribute is ';'. */
10948 if (statement == NULL_TREE)
10950 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10951 statement = build_call_expr_internal_loc (statement_location,
10953 void_type_node, 0);
10954 finish_expr_stmt (statement);
10957 warning_at (statement_location, OPT_Wattributes,
10958 "%<fallthrough%> attribute not followed by %<;%>");
10959 std_attrs = NULL_TREE;
10963 /* Set the line number for the statement. */
10964 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10965 SET_EXPR_LOCATION (statement, statement_location);
10967 /* Allow "[[fallthrough]];", but warn otherwise. */
10968 if (std_attrs != NULL_TREE)
10969 warning_at (attrs_location,
10971 "attributes at the beginning of statement are ignored");
10974 /* Append ATTR to attribute list ATTRS. */
10977 attr_chainon (tree attrs, tree attr)
10979 if (attrs == error_mark_node)
10980 return error_mark_node;
10981 if (attr == error_mark_node)
10982 return error_mark_node;
10983 return chainon (attrs, attr);
10986 /* Parse the label for a labeled-statement, i.e.
10989 case constant-expression :
10993 case constant-expression ... constant-expression : statement
10995 When a label is parsed without errors, the label is added to the
10996 parse tree by the finish_* functions, so this function doesn't
10997 have to return the label. */
11000 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11003 tree label = NULL_TREE;
11004 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11006 /* The next token should be an identifier. */
11007 token = cp_lexer_peek_token (parser->lexer);
11008 if (token->type != CPP_NAME
11009 && token->type != CPP_KEYWORD)
11011 cp_parser_error (parser, "expected labeled-statement");
11015 /* Remember whether this case or a user-defined label is allowed to fall
11017 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11019 parser->colon_corrects_to_scope_p = false;
11020 switch (token->keyword)
11024 tree expr, expr_hi;
11025 cp_token *ellipsis;
11027 /* Consume the `case' token. */
11028 cp_lexer_consume_token (parser->lexer);
11029 /* Parse the constant-expression. */
11030 expr = cp_parser_constant_expression (parser);
11031 if (check_for_bare_parameter_packs (expr))
11032 expr = error_mark_node;
11034 ellipsis = cp_lexer_peek_token (parser->lexer);
11035 if (ellipsis->type == CPP_ELLIPSIS)
11037 /* Consume the `...' token. */
11038 cp_lexer_consume_token (parser->lexer);
11039 expr_hi = cp_parser_constant_expression (parser);
11040 if (check_for_bare_parameter_packs (expr_hi))
11041 expr_hi = error_mark_node;
11043 /* We don't need to emit warnings here, as the common code
11044 will do this for us. */
11047 expr_hi = NULL_TREE;
11049 if (parser->in_switch_statement_p)
11051 tree l = finish_case_label (token->location, expr, expr_hi);
11052 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11053 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11056 error_at (token->location,
11057 "case label %qE not within a switch statement",
11063 /* Consume the `default' token. */
11064 cp_lexer_consume_token (parser->lexer);
11066 if (parser->in_switch_statement_p)
11068 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11069 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11070 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11073 error_at (token->location, "case label not within a switch statement");
11077 /* Anything else must be an ordinary label. */
11078 label = finish_label_stmt (cp_parser_identifier (parser));
11079 if (label && TREE_CODE (label) == LABEL_DECL)
11080 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11084 /* Require the `:' token. */
11085 cp_parser_require (parser, CPP_COLON, RT_COLON);
11087 /* An ordinary label may optionally be followed by attributes.
11088 However, this is only permitted if the attributes are then
11089 followed by a semicolon. This is because, for backward
11090 compatibility, when parsing
11091 lab: __attribute__ ((unused)) int i;
11092 we want the attribute to attach to "i", not "lab". */
11093 if (label != NULL_TREE
11094 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11097 cp_parser_parse_tentatively (parser);
11098 attrs = cp_parser_gnu_attributes_opt (parser);
11099 if (attrs == NULL_TREE
11100 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11101 cp_parser_abort_tentative_parse (parser);
11102 else if (!cp_parser_parse_definitely (parser))
11105 attributes = attr_chainon (attributes, attrs);
11108 if (attributes != NULL_TREE)
11109 cplus_decl_attributes (&label, attributes, 0);
11111 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11114 /* Parse an expression-statement.
11116 expression-statement:
11119 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11120 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11121 indicates whether this expression-statement is part of an
11122 expression statement. */
11125 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11127 tree statement = NULL_TREE;
11128 cp_token *token = cp_lexer_peek_token (parser->lexer);
11129 location_t loc = token->location;
11131 /* There might be attribute fallthrough. */
11132 tree attr = cp_parser_gnu_attributes_opt (parser);
11134 /* If the next token is a ';', then there is no expression
11136 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11138 statement = cp_parser_expression (parser);
11139 if (statement == error_mark_node
11140 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11142 cp_parser_skip_to_end_of_block_or_statement (parser);
11143 return error_mark_node;
11147 /* Handle [[fallthrough]];. */
11148 if (attribute_fallthrough_p (attr))
11150 /* The next token after the fallthrough attribute is ';'. */
11151 if (statement == NULL_TREE)
11152 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11153 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11154 void_type_node, 0);
11156 warning_at (loc, OPT_Wattributes,
11157 "%<fallthrough%> attribute not followed by %<;%>");
11161 /* Allow "[[fallthrough]];", but warn otherwise. */
11162 if (attr != NULL_TREE)
11163 warning_at (loc, OPT_Wattributes,
11164 "attributes at the beginning of statement are ignored");
11166 /* Give a helpful message for "A<T>::type t;" and the like. */
11167 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11168 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11170 if (TREE_CODE (statement) == SCOPE_REF)
11171 error_at (token->location, "need %<typename%> before %qE because "
11172 "%qT is a dependent scope",
11173 statement, TREE_OPERAND (statement, 0));
11174 else if (is_overloaded_fn (statement)
11175 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11178 tree fn = get_first_fn (statement);
11179 error_at (token->location,
11180 "%<%T::%D%> names the constructor, not the type",
11181 DECL_CONTEXT (fn), DECL_NAME (fn));
11185 /* Consume the final `;'. */
11186 cp_parser_consume_semicolon_at_end_of_statement (parser);
11188 if (in_statement_expr
11189 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11190 /* This is the final expression statement of a statement
11192 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11193 else if (statement)
11194 statement = finish_expr_stmt (statement);
11199 /* Parse a compound-statement.
11201 compound-statement:
11202 { statement-seq [opt] }
11206 compound-statement:
11207 { label-declaration-seq [opt] statement-seq [opt] }
11209 label-declaration-seq:
11211 label-declaration-seq label-declaration
11213 Returns a tree representing the statement. */
11216 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11217 int bcs_flags, bool function_body)
11219 tree compound_stmt;
11220 matching_braces braces;
11222 /* Consume the `{'. */
11223 if (!braces.require_open (parser))
11224 return error_mark_node;
11225 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11226 && !function_body && cxx_dialect < cxx14)
11227 pedwarn (input_location, OPT_Wpedantic,
11228 "compound-statement in %<constexpr%> function");
11229 /* Begin the compound-statement. */
11230 compound_stmt = begin_compound_stmt (bcs_flags);
11231 /* If the next keyword is `__label__' we have a label declaration. */
11232 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11233 cp_parser_label_declaration (parser);
11234 /* Parse an (optional) statement-seq. */
11235 cp_parser_statement_seq_opt (parser, in_statement_expr);
11236 /* Finish the compound-statement. */
11237 finish_compound_stmt (compound_stmt);
11238 /* Consume the `}'. */
11239 braces.require_close (parser);
11241 return compound_stmt;
11244 /* Parse an (optional) statement-seq.
11248 statement-seq [opt] statement */
11251 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11253 /* Scan statements until there aren't any more. */
11256 cp_token *token = cp_lexer_peek_token (parser->lexer);
11258 /* If we are looking at a `}', then we have run out of
11259 statements; the same is true if we have reached the end
11260 of file, or have stumbled upon a stray '@end'. */
11261 if (token->type == CPP_CLOSE_BRACE
11262 || token->type == CPP_EOF
11263 || token->type == CPP_PRAGMA_EOL
11264 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11267 /* If we are in a compound statement and find 'else' then
11268 something went wrong. */
11269 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11271 if (parser->in_statement & IN_IF_STMT)
11275 token = cp_lexer_consume_token (parser->lexer);
11276 error_at (token->location, "%<else%> without a previous %<if%>");
11280 /* Parse the statement. */
11281 cp_parser_statement (parser, in_statement_expr, true, NULL);
11285 /* Return true if we're looking at (init; cond), false otherwise. */
11288 cp_parser_init_statement_p (cp_parser *parser)
11290 /* Save tokens so that we can put them back. */
11291 cp_lexer_save_tokens (parser->lexer);
11293 /* Look for ';' that is not nested in () or {}. */
11294 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11295 /*recovering=*/false,
11297 /*consume_paren=*/false);
11299 /* Roll back the tokens we skipped. */
11300 cp_lexer_rollback_tokens (parser->lexer);
11305 /* Parse a selection-statement.
11307 selection-statement:
11308 if ( init-statement [opt] condition ) statement
11309 if ( init-statement [opt] condition ) statement else statement
11310 switch ( init-statement [opt] condition ) statement
11312 Returns the new IF_STMT or SWITCH_STMT.
11314 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11315 is a (possibly labeled) if statement which is not enclosed in
11316 braces and has an else clause. This is used to implement
11319 CHAIN is a vector of if-else-if conditions. This is used to implement
11320 -Wduplicated-cond. */
11323 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11328 token_indent_info guard_tinfo;
11333 /* Peek at the next token. */
11334 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11335 guard_tinfo = get_token_indent_info (token);
11337 /* See what kind of keyword it is. */
11338 keyword = token->keyword;
11348 if (keyword == RID_IF
11349 && cp_lexer_next_token_is_keyword (parser->lexer,
11353 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11354 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11355 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11356 "with -std=c++17 or -std=gnu++17");
11359 /* Look for the `('. */
11360 matching_parens parens;
11361 if (!parens.require_open (parser))
11363 cp_parser_skip_to_end_of_statement (parser);
11364 return error_mark_node;
11367 /* Begin the selection-statement. */
11368 if (keyword == RID_IF)
11370 statement = begin_if_stmt ();
11371 IF_STMT_CONSTEXPR_P (statement) = cx;
11374 statement = begin_switch_stmt ();
11376 /* Parse the optional init-statement. */
11377 if (cp_parser_init_statement_p (parser))
11380 if (cxx_dialect < cxx17)
11381 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11382 "init-statement in selection statements only available "
11383 "with -std=c++17 or -std=gnu++17");
11384 cp_parser_init_statement (parser, &decl);
11387 /* Parse the condition. */
11388 condition = cp_parser_condition (parser);
11389 /* Look for the `)'. */
11390 if (!parens.require_close (parser))
11391 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11392 /*consume_paren=*/true);
11394 if (keyword == RID_IF)
11397 unsigned char in_statement;
11399 /* Add the condition. */
11400 condition = finish_if_stmt_cond (condition, statement);
11402 if (warn_duplicated_cond)
11403 warn_duplicated_cond_add_or_warn (token->location, condition,
11406 /* Parse the then-clause. */
11407 in_statement = parser->in_statement;
11408 parser->in_statement |= IN_IF_STMT;
11410 /* Outside a template, the non-selected branch of a constexpr
11411 if is a 'discarded statement', i.e. unevaluated. */
11412 bool was_discarded = in_discarded_stmt;
11413 bool discard_then = (cx && !processing_template_decl
11414 && integer_zerop (condition));
11417 in_discarded_stmt = true;
11418 ++c_inhibit_evaluation_warnings;
11421 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11424 parser->in_statement = in_statement;
11426 finish_then_clause (statement);
11430 THEN_CLAUSE (statement) = NULL_TREE;
11431 in_discarded_stmt = was_discarded;
11432 --c_inhibit_evaluation_warnings;
11435 /* If the next token is `else', parse the else-clause. */
11436 if (cp_lexer_next_token_is_keyword (parser->lexer,
11439 bool discard_else = (cx && !processing_template_decl
11440 && integer_nonzerop (condition));
11443 in_discarded_stmt = true;
11444 ++c_inhibit_evaluation_warnings;
11448 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11449 /* Consume the `else' keyword. */
11450 cp_lexer_consume_token (parser->lexer);
11451 if (warn_duplicated_cond)
11453 if (cp_lexer_next_token_is_keyword (parser->lexer,
11457 /* We've got "if (COND) else if (COND2)". Start
11458 the condition chain and add COND as the first
11460 chain = new vec<tree> ();
11461 if (!CONSTANT_CLASS_P (condition)
11462 && !TREE_SIDE_EFFECTS (condition))
11464 /* Wrap it in a NOP_EXPR so that we can set the
11465 location of the condition. */
11466 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11468 SET_EXPR_LOCATION (e, token->location);
11469 chain->safe_push (e);
11472 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11475 /* This is if-else without subsequent if. Zap the
11476 condition chain; we would have already warned at
11482 begin_else_clause (statement);
11483 /* Parse the else-clause. */
11484 cp_parser_implicitly_scoped_statement (parser, NULL,
11485 guard_tinfo, chain);
11487 finish_else_clause (statement);
11489 /* If we are currently parsing a then-clause, then
11490 IF_P will not be NULL. We set it to true to
11491 indicate that this if statement has an else clause.
11492 This may trigger the Wparentheses warning below
11493 when we get back up to the parent if statement. */
11499 ELSE_CLAUSE (statement) = NULL_TREE;
11500 in_discarded_stmt = was_discarded;
11501 --c_inhibit_evaluation_warnings;
11506 /* This if statement does not have an else clause. If
11507 NESTED_IF is true, then the then-clause has an if
11508 statement which does have an else clause. We warn
11509 about the potential ambiguity. */
11511 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11512 "suggest explicit braces to avoid ambiguous"
11514 if (warn_duplicated_cond)
11516 /* We don't need the condition chain anymore. */
11522 /* Now we're all done with the if-statement. */
11523 finish_if_stmt (statement);
11527 bool in_switch_statement_p;
11528 unsigned char in_statement;
11530 /* Add the condition. */
11531 finish_switch_cond (condition, statement);
11533 /* Parse the body of the switch-statement. */
11534 in_switch_statement_p = parser->in_switch_statement_p;
11535 in_statement = parser->in_statement;
11536 parser->in_switch_statement_p = true;
11537 parser->in_statement |= IN_SWITCH_STMT;
11538 cp_parser_implicitly_scoped_statement (parser, if_p,
11540 parser->in_switch_statement_p = in_switch_statement_p;
11541 parser->in_statement = in_statement;
11543 /* Now we're all done with the switch-statement. */
11544 finish_switch_stmt (statement);
11552 cp_parser_error (parser, "expected selection-statement");
11553 return error_mark_node;
11557 /* Parse a condition.
11561 type-specifier-seq declarator = initializer-clause
11562 type-specifier-seq declarator braced-init-list
11567 type-specifier-seq declarator asm-specification [opt]
11568 attributes [opt] = assignment-expression
11570 Returns the expression that should be tested. */
11573 cp_parser_condition (cp_parser* parser)
11575 cp_decl_specifier_seq type_specifiers;
11576 const char *saved_message;
11577 int declares_class_or_enum;
11579 /* Try the declaration first. */
11580 cp_parser_parse_tentatively (parser);
11581 /* New types are not allowed in the type-specifier-seq for a
11583 saved_message = parser->type_definition_forbidden_message;
11584 parser->type_definition_forbidden_message
11585 = G_("types may not be defined in conditions");
11586 /* Parse the type-specifier-seq. */
11587 cp_parser_decl_specifier_seq (parser,
11588 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11590 &declares_class_or_enum);
11591 /* Restore the saved message. */
11592 parser->type_definition_forbidden_message = saved_message;
11593 /* If all is well, we might be looking at a declaration. */
11594 if (!cp_parser_error_occurred (parser))
11597 tree asm_specification;
11599 cp_declarator *declarator;
11600 tree initializer = NULL_TREE;
11602 /* Parse the declarator. */
11603 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11604 /*ctor_dtor_or_conv_p=*/NULL,
11605 /*parenthesized_p=*/NULL,
11606 /*member_p=*/false,
11607 /*friend_p=*/false);
11608 /* Parse the attributes. */
11609 attributes = cp_parser_attributes_opt (parser);
11610 /* Parse the asm-specification. */
11611 asm_specification = cp_parser_asm_specification_opt (parser);
11612 /* If the next token is not an `=' or '{', then we might still be
11613 looking at an expression. For example:
11617 looks like a decl-specifier-seq and a declarator -- but then
11618 there is no `=', so this is an expression. */
11619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11620 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11621 cp_parser_simulate_error (parser);
11623 /* If we did see an `=' or '{', then we are looking at a declaration
11625 if (cp_parser_parse_definitely (parser))
11628 bool non_constant_p;
11629 int flags = LOOKUP_ONLYCONVERTING;
11631 /* Create the declaration. */
11632 decl = start_decl (declarator, &type_specifiers,
11633 /*initialized_p=*/true,
11634 attributes, /*prefix_attributes=*/NULL_TREE,
11637 /* Parse the initializer. */
11638 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11640 initializer = cp_parser_braced_list (parser, &non_constant_p);
11641 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11646 /* Consume the `='. */
11647 cp_parser_require (parser, CPP_EQ, RT_EQ);
11648 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11650 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11651 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11653 /* Process the initializer. */
11654 cp_finish_decl (decl,
11655 initializer, !non_constant_p,
11660 pop_scope (pushed_scope);
11662 return convert_from_reference (decl);
11665 /* If we didn't even get past the declarator successfully, we are
11666 definitely not looking at a declaration. */
11668 cp_parser_abort_tentative_parse (parser);
11670 /* Otherwise, we are looking at an expression. */
11671 return cp_parser_expression (parser);
11674 /* Parses a for-statement or range-for-statement until the closing ')',
11678 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11680 tree init, scope, decl;
11683 /* Begin the for-statement. */
11684 scope = begin_for_scope (&init);
11686 /* Parse the initialization. */
11687 is_range_for = cp_parser_init_statement (parser, &decl);
11690 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11692 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11696 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11697 unsigned short unroll)
11699 /* Normal for loop */
11700 tree condition = NULL_TREE;
11701 tree expression = NULL_TREE;
11704 stmt = begin_for_stmt (scope, init);
11705 /* The init-statement has already been parsed in
11706 cp_parser_init_statement, so no work is needed here. */
11707 finish_init_stmt (stmt);
11709 /* If there's a condition, process it. */
11710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11711 condition = cp_parser_condition (parser);
11714 cp_parser_error (parser, "missing loop condition in loop with "
11715 "%<GCC ivdep%> pragma");
11716 condition = error_mark_node;
11720 cp_parser_error (parser, "missing loop condition in loop with "
11721 "%<GCC unroll%> pragma");
11722 condition = error_mark_node;
11724 finish_for_cond (condition, stmt, ivdep, unroll);
11725 /* Look for the `;'. */
11726 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11728 /* If there's an expression, process it. */
11729 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11730 expression = cp_parser_expression (parser);
11731 finish_for_expr (expression, stmt);
11736 /* Tries to parse a range-based for-statement:
11739 decl-specifier-seq declarator : expression
11741 The decl-specifier-seq declarator and the `:' are already parsed by
11742 cp_parser_init_statement. If processing_template_decl it returns a
11743 newly created RANGE_FOR_STMT; if not, it is converted to a
11744 regular FOR_STMT. */
11747 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11748 bool ivdep, unsigned short unroll)
11750 tree stmt, range_expr;
11751 auto_vec <cxx_binding *, 16> bindings;
11752 auto_vec <tree, 16> names;
11753 tree decomp_first_name = NULL_TREE;
11754 unsigned int decomp_cnt = 0;
11756 /* Get the range declaration momentarily out of the way so that
11757 the range expression doesn't clash with it. */
11758 if (range_decl != error_mark_node)
11760 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11762 tree v = DECL_VALUE_EXPR (range_decl);
11763 /* For decomposition declaration get all of the corresponding
11764 declarations out of the way. */
11765 if (TREE_CODE (v) == ARRAY_REF
11766 && VAR_P (TREE_OPERAND (v, 0))
11767 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11769 tree d = range_decl;
11770 range_decl = TREE_OPERAND (v, 0);
11771 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11772 decomp_first_name = d;
11773 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11775 tree name = DECL_NAME (d);
11776 names.safe_push (name);
11777 bindings.safe_push (IDENTIFIER_BINDING (name));
11778 IDENTIFIER_BINDING (name)
11779 = IDENTIFIER_BINDING (name)->previous;
11783 if (names.is_empty ())
11785 tree name = DECL_NAME (range_decl);
11786 names.safe_push (name);
11787 bindings.safe_push (IDENTIFIER_BINDING (name));
11788 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11792 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11794 bool expr_non_constant_p;
11795 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11798 range_expr = cp_parser_expression (parser);
11800 /* Put the range declaration(s) back into scope. */
11801 for (unsigned int i = 0; i < names.length (); i++)
11803 cxx_binding *binding = bindings[i];
11804 binding->previous = IDENTIFIER_BINDING (names[i]);
11805 IDENTIFIER_BINDING (names[i]) = binding;
11808 /* If in template, STMT is converted to a normal for-statement
11809 at instantiation. If not, it is done just ahead. */
11810 if (processing_template_decl)
11812 if (check_for_bare_parameter_packs (range_expr))
11813 range_expr = error_mark_node;
11814 stmt = begin_range_for_stmt (scope, init);
11816 RANGE_FOR_IVDEP (stmt) = 1;
11818 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11819 finish_range_for_decl (stmt, range_decl, range_expr);
11820 if (!type_dependent_expression_p (range_expr)
11821 /* do_auto_deduction doesn't mess with template init-lists. */
11822 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11823 do_range_for_auto_deduction (range_decl, range_expr);
11827 stmt = begin_for_stmt (scope, init);
11828 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11829 decomp_first_name, decomp_cnt, ivdep,
11835 /* Subroutine of cp_convert_range_for: given the initializer expression,
11836 builds up the range temporary. */
11839 build_range_temp (tree range_expr)
11841 tree range_type, range_temp;
11843 /* Find out the type deduced by the declaration
11844 `auto &&__range = range_expr'. */
11845 range_type = cp_build_reference_type (make_auto (), true);
11846 range_type = do_auto_deduction (range_type, range_expr,
11847 type_uses_auto (range_type));
11849 /* Create the __range variable. */
11850 range_temp = build_decl (input_location, VAR_DECL,
11851 get_identifier ("__for_range"), range_type);
11852 TREE_USED (range_temp) = 1;
11853 DECL_ARTIFICIAL (range_temp) = 1;
11858 /* Used by cp_parser_range_for in template context: we aren't going to
11859 do a full conversion yet, but we still need to resolve auto in the
11860 type of the for-range-declaration if present. This is basically
11861 a shortcut version of cp_convert_range_for. */
11864 do_range_for_auto_deduction (tree decl, tree range_expr)
11866 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11869 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11870 range_temp = convert_from_reference (build_range_temp (range_expr));
11871 iter_type = (cp_parser_perform_range_for_lookup
11872 (range_temp, &begin_dummy, &end_dummy));
11875 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11877 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11879 tf_warning_or_error);
11880 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11881 iter_decl, auto_node);
11886 /* Converts a range-based for-statement into a normal
11887 for-statement, as per the definition.
11889 for (RANGE_DECL : RANGE_EXPR)
11892 should be equivalent to:
11895 auto &&__range = RANGE_EXPR;
11896 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11900 RANGE_DECL = *__begin;
11905 If RANGE_EXPR is an array:
11906 BEGIN_EXPR = __range
11907 END_EXPR = __range + ARRAY_SIZE(__range)
11908 Else if RANGE_EXPR has a member 'begin' or 'end':
11909 BEGIN_EXPR = __range.begin()
11910 END_EXPR = __range.end()
11912 BEGIN_EXPR = begin(__range)
11913 END_EXPR = end(__range);
11915 If __range has a member 'begin' but not 'end', or vice versa, we must
11916 still use the second alternative (it will surely fail, however).
11917 When calling begin()/end() in the third alternative we must use
11918 argument dependent lookup, but always considering 'std' as an associated
11922 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11923 tree decomp_first_name, unsigned int decomp_cnt,
11924 bool ivdep, unsigned short unroll)
11927 tree iter_type, begin_expr, end_expr;
11928 tree condition, expression;
11930 range_expr = mark_lvalue_use (range_expr);
11932 if (range_decl == error_mark_node || range_expr == error_mark_node)
11933 /* If an error happened previously do nothing or else a lot of
11934 unhelpful errors would be issued. */
11935 begin_expr = end_expr = iter_type = error_mark_node;
11940 if (VAR_P (range_expr)
11941 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11942 /* Can't bind a reference to an array of runtime bound. */
11943 range_temp = range_expr;
11946 range_temp = build_range_temp (range_expr);
11947 pushdecl (range_temp);
11948 cp_finish_decl (range_temp, range_expr,
11949 /*is_constant_init*/false, NULL_TREE,
11950 LOOKUP_ONLYCONVERTING);
11951 range_temp = convert_from_reference (range_temp);
11953 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11954 &begin_expr, &end_expr);
11957 /* The new for initialization statement. */
11958 begin = build_decl (input_location, VAR_DECL,
11959 get_identifier ("__for_begin"), iter_type);
11960 TREE_USED (begin) = 1;
11961 DECL_ARTIFICIAL (begin) = 1;
11963 cp_finish_decl (begin, begin_expr,
11964 /*is_constant_init*/false, NULL_TREE,
11965 LOOKUP_ONLYCONVERTING);
11967 if (cxx_dialect >= cxx17)
11968 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11969 end = build_decl (input_location, VAR_DECL,
11970 get_identifier ("__for_end"), iter_type);
11971 TREE_USED (end) = 1;
11972 DECL_ARTIFICIAL (end) = 1;
11974 cp_finish_decl (end, end_expr,
11975 /*is_constant_init*/false, NULL_TREE,
11976 LOOKUP_ONLYCONVERTING);
11978 finish_init_stmt (statement);
11980 /* The new for condition. */
11981 condition = build_x_binary_op (input_location, NE_EXPR,
11984 NULL, tf_warning_or_error);
11985 finish_for_cond (condition, statement, ivdep, unroll);
11987 /* The new increment expression. */
11988 expression = finish_unary_op_expr (input_location,
11989 PREINCREMENT_EXPR, begin,
11990 tf_warning_or_error);
11991 finish_for_expr (expression, statement);
11993 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11994 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11996 /* The declaration is initialized with *__begin inside the loop body. */
11997 cp_finish_decl (range_decl,
11998 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11999 tf_warning_or_error),
12000 /*is_constant_init*/false, NULL_TREE,
12001 LOOKUP_ONLYCONVERTING);
12002 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12003 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12008 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12009 We need to solve both at the same time because the method used
12010 depends on the existence of members begin or end.
12011 Returns the type deduced for the iterator expression. */
12014 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12016 if (error_operand_p (range))
12018 *begin = *end = error_mark_node;
12019 return error_mark_node;
12022 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12024 error ("range-based %<for%> expression of type %qT "
12025 "has incomplete type", TREE_TYPE (range));
12026 *begin = *end = error_mark_node;
12027 return error_mark_node;
12029 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12031 /* If RANGE is an array, we will use pointer arithmetic. */
12032 *begin = decay_conversion (range, tf_warning_or_error);
12033 *end = build_binary_op (input_location, PLUS_EXPR,
12035 array_type_nelts_top (TREE_TYPE (range)),
12037 return TREE_TYPE (*begin);
12041 /* If it is not an array, we must do a bit of magic. */
12042 tree id_begin, id_end;
12043 tree member_begin, member_end;
12045 *begin = *end = error_mark_node;
12047 id_begin = get_identifier ("begin");
12048 id_end = get_identifier ("end");
12049 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12050 /*protect=*/2, /*want_type=*/false,
12051 tf_warning_or_error);
12052 member_end = lookup_member (TREE_TYPE (range), id_end,
12053 /*protect=*/2, /*want_type=*/false,
12054 tf_warning_or_error);
12056 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12058 /* Use the member functions. */
12059 *begin = cp_parser_range_for_member_function (range, id_begin);
12060 *end = cp_parser_range_for_member_function (range, id_end);
12064 /* Use global functions with ADL. */
12065 vec<tree, va_gc> *vec;
12066 vec = make_tree_vector ();
12068 vec_safe_push (vec, range);
12070 member_begin = perform_koenig_lookup (id_begin, vec,
12071 tf_warning_or_error);
12072 *begin = finish_call_expr (member_begin, &vec, false, true,
12073 tf_warning_or_error);
12074 member_end = perform_koenig_lookup (id_end, vec,
12075 tf_warning_or_error);
12076 *end = finish_call_expr (member_end, &vec, false, true,
12077 tf_warning_or_error);
12079 release_tree_vector (vec);
12082 /* Last common checks. */
12083 if (*begin == error_mark_node || *end == error_mark_node)
12085 /* If one of the expressions is an error do no more checks. */
12086 *begin = *end = error_mark_node;
12087 return error_mark_node;
12089 else if (type_dependent_expression_p (*begin)
12090 || type_dependent_expression_p (*end))
12091 /* Can happen, when, eg, in a template context, Koenig lookup
12092 can't resolve begin/end (c++/58503). */
12096 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12097 /* The unqualified type of the __begin and __end temporaries should
12098 be the same, as required by the multiple auto declaration. */
12099 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12101 if (cxx_dialect >= cxx17
12102 && (build_x_binary_op (input_location, NE_EXPR,
12103 *begin, ERROR_MARK,
12106 != error_mark_node))
12107 /* P0184R0 allows __begin and __end to have different types,
12108 but make sure they are comparable so we can give a better
12111 error ("inconsistent begin/end types in range-based %<for%> "
12112 "statement: %qT and %qT",
12113 TREE_TYPE (*begin), TREE_TYPE (*end));
12120 /* Helper function for cp_parser_perform_range_for_lookup.
12121 Builds a tree for RANGE.IDENTIFIER(). */
12124 cp_parser_range_for_member_function (tree range, tree identifier)
12127 vec<tree, va_gc> *vec;
12129 member = finish_class_member_access_expr (range, identifier,
12130 false, tf_warning_or_error);
12131 if (member == error_mark_node)
12132 return error_mark_node;
12134 vec = make_tree_vector ();
12135 res = finish_call_expr (member, &vec,
12136 /*disallow_virtual=*/false,
12137 /*koenig_p=*/false,
12138 tf_warning_or_error);
12139 release_tree_vector (vec);
12143 /* Parse an iteration-statement.
12145 iteration-statement:
12146 while ( condition ) statement
12147 do statement while ( expression ) ;
12148 for ( init-statement condition [opt] ; expression [opt] )
12151 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12154 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12155 unsigned short unroll)
12160 unsigned char in_statement;
12161 token_indent_info guard_tinfo;
12163 /* Peek at the next token. */
12164 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12166 return error_mark_node;
12168 guard_tinfo = get_token_indent_info (token);
12170 /* Remember whether or not we are already within an iteration
12172 in_statement = parser->in_statement;
12174 /* See what kind of keyword it is. */
12175 keyword = token->keyword;
12182 /* Begin the while-statement. */
12183 statement = begin_while_stmt ();
12184 /* Look for the `('. */
12185 matching_parens parens;
12186 parens.require_open (parser);
12187 /* Parse the condition. */
12188 condition = cp_parser_condition (parser);
12189 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12190 /* Look for the `)'. */
12191 parens.require_close (parser);
12192 /* Parse the dependent statement. */
12193 parser->in_statement = IN_ITERATION_STMT;
12194 bool prev = note_iteration_stmt_body_start ();
12195 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12196 note_iteration_stmt_body_end (prev);
12197 parser->in_statement = in_statement;
12198 /* We're done with the while-statement. */
12199 finish_while_stmt (statement);
12207 /* Begin the do-statement. */
12208 statement = begin_do_stmt ();
12209 /* Parse the body of the do-statement. */
12210 parser->in_statement = IN_ITERATION_STMT;
12211 bool prev = note_iteration_stmt_body_start ();
12212 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12213 note_iteration_stmt_body_end (prev);
12214 parser->in_statement = in_statement;
12215 finish_do_body (statement);
12216 /* Look for the `while' keyword. */
12217 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12218 /* Look for the `('. */
12219 matching_parens parens;
12220 parens.require_open (parser);
12221 /* Parse the expression. */
12222 expression = cp_parser_expression (parser);
12223 /* We're done with the do-statement. */
12224 finish_do_stmt (expression, statement, ivdep, unroll);
12225 /* Look for the `)'. */
12226 parens.require_close (parser);
12227 /* Look for the `;'. */
12228 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12234 /* Look for the `('. */
12235 matching_parens parens;
12236 parens.require_open (parser);
12238 statement = cp_parser_for (parser, ivdep, unroll);
12240 /* Look for the `)'. */
12241 parens.require_close (parser);
12243 /* Parse the body of the for-statement. */
12244 parser->in_statement = IN_ITERATION_STMT;
12245 bool prev = note_iteration_stmt_body_start ();
12246 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12247 note_iteration_stmt_body_end (prev);
12248 parser->in_statement = in_statement;
12250 /* We're done with the for-statement. */
12251 finish_for_stmt (statement);
12256 cp_parser_error (parser, "expected iteration-statement");
12257 statement = error_mark_node;
12264 /* Parse a init-statement or the declarator of a range-based-for.
12265 Returns true if a range-based-for declaration is seen.
12268 expression-statement
12269 simple-declaration */
12272 cp_parser_init_statement (cp_parser* parser, tree *decl)
12274 /* If the next token is a `;', then we have an empty
12275 expression-statement. Grammatically, this is also a
12276 simple-declaration, but an invalid one, because it does not
12277 declare anything. Therefore, if we did not handle this case
12278 specially, we would issue an error message about an invalid
12280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12282 bool is_range_for = false;
12283 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12285 /* A colon is used in range-based for. */
12286 parser->colon_corrects_to_scope_p = false;
12288 /* We're going to speculatively look for a declaration, falling back
12289 to an expression, if necessary. */
12290 cp_parser_parse_tentatively (parser);
12291 /* Parse the declaration. */
12292 cp_parser_simple_declaration (parser,
12293 /*function_definition_allowed_p=*/false,
12295 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12296 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12298 /* It is a range-for, consume the ':' */
12299 cp_lexer_consume_token (parser->lexer);
12300 is_range_for = true;
12301 if (cxx_dialect < cxx11)
12302 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12303 "range-based %<for%> loops only available with "
12304 "-std=c++11 or -std=gnu++11");
12307 /* The ';' is not consumed yet because we told
12308 cp_parser_simple_declaration not to. */
12309 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12311 if (cp_parser_parse_definitely (parser))
12312 return is_range_for;
12313 /* If the tentative parse failed, then we shall need to look for an
12314 expression-statement. */
12316 /* If we are here, it is an expression-statement. */
12317 cp_parser_expression_statement (parser, NULL_TREE);
12321 /* Parse a jump-statement.
12326 return expression [opt] ;
12327 return braced-init-list ;
12333 goto * expression ;
12335 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12338 cp_parser_jump_statement (cp_parser* parser)
12340 tree statement = error_mark_node;
12343 unsigned char in_statement;
12345 /* Peek at the next token. */
12346 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12348 return error_mark_node;
12350 /* See what kind of keyword it is. */
12351 keyword = token->keyword;
12355 in_statement = parser->in_statement & ~IN_IF_STMT;
12356 switch (in_statement)
12359 error_at (token->location, "break statement not within loop or switch");
12362 gcc_assert ((in_statement & IN_SWITCH_STMT)
12363 || in_statement == IN_ITERATION_STMT);
12364 statement = finish_break_stmt ();
12365 if (in_statement == IN_ITERATION_STMT)
12366 break_maybe_infinite_loop ();
12369 error_at (token->location, "invalid exit from OpenMP structured block");
12372 error_at (token->location, "break statement used with OpenMP for loop");
12375 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12379 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12382 error_at (token->location, "continue statement not within a loop");
12384 /* Fall through. */
12385 case IN_ITERATION_STMT:
12387 statement = finish_continue_stmt ();
12390 error_at (token->location, "invalid exit from OpenMP structured block");
12393 gcc_unreachable ();
12395 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12401 bool expr_non_constant_p;
12403 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12405 cp_lexer_set_source_position (parser->lexer);
12406 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12407 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12409 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12410 expr = cp_parser_expression (parser);
12412 /* If the next token is a `;', then there is no
12415 /* Build the return-statement. */
12416 if (current_function_auto_return_pattern && in_discarded_stmt)
12417 /* Don't deduce from a discarded return statement. */;
12419 statement = finish_return_stmt (expr);
12420 /* Look for the final `;'. */
12421 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12426 if (parser->in_function_body
12427 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12429 error ("%<goto%> in %<constexpr%> function");
12430 cp_function_chain->invalid_constexpr = true;
12433 /* Create the goto-statement. */
12434 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12436 /* Issue a warning about this use of a GNU extension. */
12437 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12438 /* Consume the '*' token. */
12439 cp_lexer_consume_token (parser->lexer);
12440 /* Parse the dependent expression. */
12441 finish_goto_stmt (cp_parser_expression (parser));
12444 finish_goto_stmt (cp_parser_identifier (parser));
12445 /* Look for the final `;'. */
12446 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12450 cp_parser_error (parser, "expected jump-statement");
12457 /* Parse a declaration-statement.
12459 declaration-statement:
12460 block-declaration */
12463 cp_parser_declaration_statement (cp_parser* parser)
12467 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12468 p = obstack_alloc (&declarator_obstack, 0);
12470 /* Parse the block-declaration. */
12471 cp_parser_block_declaration (parser, /*statement_p=*/true);
12473 /* Free any declarators allocated. */
12474 obstack_free (&declarator_obstack, p);
12477 /* Some dependent statements (like `if (cond) statement'), are
12478 implicitly in their own scope. In other words, if the statement is
12479 a single statement (as opposed to a compound-statement), it is
12480 none-the-less treated as if it were enclosed in braces. Any
12481 declarations appearing in the dependent statement are out of scope
12482 after control passes that point. This function parses a statement,
12483 but ensures that is in its own scope, even if it is not a
12484 compound-statement.
12486 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12487 is a (possibly labeled) if statement which is not enclosed in
12488 braces and has an else clause. This is used to implement
12491 CHAIN is a vector of if-else-if conditions. This is used to implement
12494 Returns the new statement. */
12497 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12498 const token_indent_info &guard_tinfo,
12502 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12503 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12504 token_indent_info body_tinfo
12505 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12510 /* Mark if () ; with a special NOP_EXPR. */
12511 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12513 cp_lexer_consume_token (parser->lexer);
12514 statement = add_stmt (build_empty_stmt (body_loc));
12516 if (guard_tinfo.keyword == RID_IF
12517 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12518 warning_at (body_loc, OPT_Wempty_body,
12519 "suggest braces around empty body in an %<if%> statement");
12520 else if (guard_tinfo.keyword == RID_ELSE)
12521 warning_at (body_loc, OPT_Wempty_body,
12522 "suggest braces around empty body in an %<else%> statement");
12524 /* if a compound is opened, we simply parse the statement directly. */
12525 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12526 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12527 /* If the token is not a `{', then we must take special action. */
12530 /* Create a compound-statement. */
12531 statement = begin_compound_stmt (0);
12532 /* Parse the dependent-statement. */
12533 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12534 &body_loc_after_labels);
12535 /* Finish the dummy compound-statement. */
12536 finish_compound_stmt (statement);
12539 token_indent_info next_tinfo
12540 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12541 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12543 if (body_loc_after_labels != UNKNOWN_LOCATION
12544 && next_tinfo.type != CPP_SEMICOLON)
12545 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12546 guard_tinfo.location, guard_tinfo.keyword);
12548 /* Return the statement. */
12552 /* For some dependent statements (like `while (cond) statement'), we
12553 have already created a scope. Therefore, even if the dependent
12554 statement is a compound-statement, we do not want to create another
12558 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12559 const token_indent_info &guard_tinfo)
12561 /* If the token is a `{', then we must take special action. */
12562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12564 token_indent_info body_tinfo
12565 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12566 location_t loc_after_labels = UNKNOWN_LOCATION;
12568 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12569 &loc_after_labels);
12570 token_indent_info next_tinfo
12571 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12572 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12574 if (loc_after_labels != UNKNOWN_LOCATION
12575 && next_tinfo.type != CPP_SEMICOLON)
12576 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12577 guard_tinfo.location,
12578 guard_tinfo.keyword);
12582 /* Avoid calling cp_parser_compound_statement, so that we
12583 don't create a new scope. Do everything else by hand. */
12584 matching_braces braces;
12585 braces.require_open (parser);
12586 /* If the next keyword is `__label__' we have a label declaration. */
12587 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12588 cp_parser_label_declaration (parser);
12589 /* Parse an (optional) statement-seq. */
12590 cp_parser_statement_seq_opt (parser, NULL_TREE);
12591 braces.require_close (parser);
12595 /* Declarations [gram.dcl.dcl] */
12597 /* Parse an optional declaration-sequence.
12601 declaration-seq declaration */
12604 cp_parser_declaration_seq_opt (cp_parser* parser)
12610 token = cp_lexer_peek_token (parser->lexer);
12612 if (token->type == CPP_CLOSE_BRACE
12613 || token->type == CPP_EOF
12614 || token->type == CPP_PRAGMA_EOL)
12617 if (token->type == CPP_SEMICOLON)
12619 /* A declaration consisting of a single semicolon is
12620 invalid. Allow it unless we're being pedantic. */
12621 cp_lexer_consume_token (parser->lexer);
12622 if (!in_system_header_at (input_location))
12623 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12627 /* If we're entering or exiting a region that's implicitly
12628 extern "C", modify the lang context appropriately. */
12629 if (!parser->implicit_extern_c && token->implicit_extern_c)
12631 push_lang_context (lang_name_c);
12632 parser->implicit_extern_c = true;
12634 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12636 pop_lang_context ();
12637 parser->implicit_extern_c = false;
12640 if (token->type == CPP_PRAGMA)
12642 /* A top-level declaration can consist solely of a #pragma.
12643 A nested declaration cannot, so this is done here and not
12644 in cp_parser_declaration. (A #pragma at block scope is
12645 handled in cp_parser_statement.) */
12646 cp_parser_pragma (parser, pragma_external, NULL);
12650 /* Parse the declaration itself. */
12651 cp_parser_declaration (parser);
12655 /* Parse a declaration.
12659 function-definition
12660 template-declaration
12661 explicit-instantiation
12662 explicit-specialization
12663 linkage-specification
12664 namespace-definition
12672 __extension__ declaration */
12675 cp_parser_declaration (cp_parser* parser)
12679 int saved_pedantic;
12681 tree attributes = NULL_TREE;
12683 /* Check for the `__extension__' keyword. */
12684 if (cp_parser_extension_opt (parser, &saved_pedantic))
12686 /* Parse the qualified declaration. */
12687 cp_parser_declaration (parser);
12688 /* Restore the PEDANTIC flag. */
12689 pedantic = saved_pedantic;
12694 /* Try to figure out what kind of declaration is present. */
12695 token1 = *cp_lexer_peek_token (parser->lexer);
12697 if (token1.type != CPP_EOF)
12698 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12701 token2.type = CPP_EOF;
12702 token2.keyword = RID_MAX;
12705 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12706 p = obstack_alloc (&declarator_obstack, 0);
12708 /* If the next token is `extern' and the following token is a string
12709 literal, then we have a linkage specification. */
12710 if (token1.keyword == RID_EXTERN
12711 && cp_parser_is_pure_string_literal (&token2))
12712 cp_parser_linkage_specification (parser);
12713 /* If the next token is `template', then we have either a template
12714 declaration, an explicit instantiation, or an explicit
12716 else if (token1.keyword == RID_TEMPLATE)
12718 /* `template <>' indicates a template specialization. */
12719 if (token2.type == CPP_LESS
12720 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12721 cp_parser_explicit_specialization (parser);
12722 /* `template <' indicates a template declaration. */
12723 else if (token2.type == CPP_LESS)
12724 cp_parser_template_declaration (parser, /*member_p=*/false);
12725 /* Anything else must be an explicit instantiation. */
12727 cp_parser_explicit_instantiation (parser);
12729 /* If the next token is `export', then we have a template
12731 else if (token1.keyword == RID_EXPORT)
12732 cp_parser_template_declaration (parser, /*member_p=*/false);
12733 /* If the next token is `extern', 'static' or 'inline' and the one
12734 after that is `template', we have a GNU extended explicit
12735 instantiation directive. */
12736 else if (cp_parser_allow_gnu_extensions_p (parser)
12737 && (token1.keyword == RID_EXTERN
12738 || token1.keyword == RID_STATIC
12739 || token1.keyword == RID_INLINE)
12740 && token2.keyword == RID_TEMPLATE)
12741 cp_parser_explicit_instantiation (parser);
12742 /* If the next token is `namespace', check for a named or unnamed
12743 namespace definition. */
12744 else if (token1.keyword == RID_NAMESPACE
12745 && (/* A named namespace definition. */
12746 (token2.type == CPP_NAME
12747 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12749 || (token2.type == CPP_OPEN_SQUARE
12750 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12751 == CPP_OPEN_SQUARE)
12752 /* An unnamed namespace definition. */
12753 || token2.type == CPP_OPEN_BRACE
12754 || token2.keyword == RID_ATTRIBUTE))
12755 cp_parser_namespace_definition (parser);
12756 /* An inline (associated) namespace definition. */
12757 else if (token1.keyword == RID_INLINE
12758 && token2.keyword == RID_NAMESPACE)
12759 cp_parser_namespace_definition (parser);
12760 /* Objective-C++ declaration/definition. */
12761 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12762 cp_parser_objc_declaration (parser, NULL_TREE);
12763 else if (c_dialect_objc ()
12764 && token1.keyword == RID_ATTRIBUTE
12765 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12766 cp_parser_objc_declaration (parser, attributes);
12767 /* At this point we may have a template declared by a concept
12769 else if (flag_concepts
12770 && cp_parser_template_declaration_after_export (parser,
12771 /*member_p=*/false))
12774 /* Try to parse a block-declaration, or a function-definition. */
12775 cp_parser_block_declaration (parser, /*statement_p=*/false);
12777 /* Free any declarators allocated. */
12778 obstack_free (&declarator_obstack, p);
12781 /* Parse a block-declaration.
12786 namespace-alias-definition
12793 __extension__ block-declaration
12798 static_assert-declaration
12800 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12801 part of a declaration-statement. */
12804 cp_parser_block_declaration (cp_parser *parser,
12808 int saved_pedantic;
12810 /* Check for the `__extension__' keyword. */
12811 if (cp_parser_extension_opt (parser, &saved_pedantic))
12813 /* Parse the qualified declaration. */
12814 cp_parser_block_declaration (parser, statement_p);
12815 /* Restore the PEDANTIC flag. */
12816 pedantic = saved_pedantic;
12821 /* Peek at the next token to figure out which kind of declaration is
12823 token1 = cp_lexer_peek_token (parser->lexer);
12825 /* If the next keyword is `asm', we have an asm-definition. */
12826 if (token1->keyword == RID_ASM)
12829 cp_parser_commit_to_tentative_parse (parser);
12830 cp_parser_asm_definition (parser);
12832 /* If the next keyword is `namespace', we have a
12833 namespace-alias-definition. */
12834 else if (token1->keyword == RID_NAMESPACE)
12835 cp_parser_namespace_alias_definition (parser);
12836 /* If the next keyword is `using', we have a
12837 using-declaration, a using-directive, or an alias-declaration. */
12838 else if (token1->keyword == RID_USING)
12843 cp_parser_commit_to_tentative_parse (parser);
12844 /* If the token after `using' is `namespace', then we have a
12845 using-directive. */
12846 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12847 if (token2->keyword == RID_NAMESPACE)
12848 cp_parser_using_directive (parser);
12849 /* If the second token after 'using' is '=', then we have an
12850 alias-declaration. */
12851 else if (cxx_dialect >= cxx11
12852 && token2->type == CPP_NAME
12853 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12854 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12855 cp_parser_alias_declaration (parser);
12856 /* Otherwise, it's a using-declaration. */
12858 cp_parser_using_declaration (parser,
12859 /*access_declaration_p=*/false);
12861 /* If the next keyword is `__label__' we have a misplaced label
12863 else if (token1->keyword == RID_LABEL)
12865 cp_lexer_consume_token (parser->lexer);
12866 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12867 cp_parser_skip_to_end_of_statement (parser);
12868 /* If the next token is now a `;', consume it. */
12869 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12870 cp_lexer_consume_token (parser->lexer);
12872 /* If the next token is `static_assert' we have a static assertion. */
12873 else if (token1->keyword == RID_STATIC_ASSERT)
12874 cp_parser_static_assert (parser, /*member_p=*/false);
12875 /* Anything else must be a simple-declaration. */
12877 cp_parser_simple_declaration (parser, !statement_p,
12878 /*maybe_range_for_decl*/NULL);
12881 /* Parse a simple-declaration.
12883 simple-declaration:
12884 decl-specifier-seq [opt] init-declarator-list [opt] ;
12885 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12886 brace-or-equal-initializer ;
12888 init-declarator-list:
12890 init-declarator-list , init-declarator
12892 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12893 function-definition as a simple-declaration.
12895 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12896 parsed declaration if it is an uninitialized single declarator not followed
12897 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12898 if present, will not be consumed. */
12901 cp_parser_simple_declaration (cp_parser* parser,
12902 bool function_definition_allowed_p,
12903 tree *maybe_range_for_decl)
12905 cp_decl_specifier_seq decl_specifiers;
12906 int declares_class_or_enum;
12907 bool saw_declarator;
12908 location_t comma_loc = UNKNOWN_LOCATION;
12909 location_t init_loc = UNKNOWN_LOCATION;
12911 if (maybe_range_for_decl)
12912 *maybe_range_for_decl = NULL_TREE;
12914 /* Defer access checks until we know what is being declared; the
12915 checks for names appearing in the decl-specifier-seq should be
12916 done as if we were in the scope of the thing being declared. */
12917 push_deferring_access_checks (dk_deferred);
12919 /* Parse the decl-specifier-seq. We have to keep track of whether
12920 or not the decl-specifier-seq declares a named class or
12921 enumeration type, since that is the only case in which the
12922 init-declarator-list is allowed to be empty.
12926 In a simple-declaration, the optional init-declarator-list can be
12927 omitted only when declaring a class or enumeration, that is when
12928 the decl-specifier-seq contains either a class-specifier, an
12929 elaborated-type-specifier, or an enum-specifier. */
12930 cp_parser_decl_specifier_seq (parser,
12931 CP_PARSER_FLAGS_OPTIONAL,
12933 &declares_class_or_enum);
12934 /* We no longer need to defer access checks. */
12935 stop_deferring_access_checks ();
12937 /* In a block scope, a valid declaration must always have a
12938 decl-specifier-seq. By not trying to parse declarators, we can
12939 resolve the declaration/expression ambiguity more quickly. */
12940 if (!function_definition_allowed_p
12941 && !decl_specifiers.any_specifiers_p)
12943 cp_parser_error (parser, "expected declaration");
12947 /* If the next two tokens are both identifiers, the code is
12948 erroneous. The usual cause of this situation is code like:
12952 where "T" should name a type -- but does not. */
12953 if (!decl_specifiers.any_type_specifiers_p
12954 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12956 /* If parsing tentatively, we should commit; we really are
12957 looking at a declaration. */
12958 cp_parser_commit_to_tentative_parse (parser);
12963 /* If we have seen at least one decl-specifier, and the next token
12964 is not a parenthesis, then we must be looking at a declaration.
12965 (After "int (" we might be looking at a functional cast.) */
12966 if (decl_specifiers.any_specifiers_p
12967 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12968 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12969 && !cp_parser_error_occurred (parser))
12970 cp_parser_commit_to_tentative_parse (parser);
12972 /* Look for C++17 decomposition declaration. */
12973 for (size_t n = 1; ; n++)
12974 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12975 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12977 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12978 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12979 && decl_specifiers.any_specifiers_p)
12982 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12983 maybe_range_for_decl,
12986 /* The next token should be either a `,' or a `;'. */
12987 cp_token *token = cp_lexer_peek_token (parser->lexer);
12988 /* If it's a `;', we are done. */
12989 if (token->type == CPP_SEMICOLON)
12991 else if (maybe_range_for_decl)
12993 if (*maybe_range_for_decl == NULL_TREE)
12994 *maybe_range_for_decl = error_mark_node;
12997 /* Anything else is an error. */
13000 /* If we have already issued an error message we don't need
13001 to issue another one. */
13002 if ((decl != error_mark_node
13003 && DECL_INITIAL (decl) != error_mark_node)
13004 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13005 cp_parser_error (parser, "expected %<,%> or %<;%>");
13006 /* Skip tokens until we reach the end of the statement. */
13007 cp_parser_skip_to_end_of_statement (parser);
13008 /* If the next token is now a `;', consume it. */
13009 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13010 cp_lexer_consume_token (parser->lexer);
13018 bool auto_specifier_p;
13019 /* NULL_TREE if both variable and function declaration are allowed,
13020 error_mark_node if function declaration are not allowed and
13021 a FUNCTION_DECL that should be diagnosed if it is followed by
13022 variable declarations. */
13023 tree auto_function_declaration;
13025 last_type = NULL_TREE;
13027 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13028 auto_function_declaration = NULL_TREE;
13030 /* Keep going until we hit the `;' at the end of the simple
13032 saw_declarator = false;
13033 while (cp_lexer_next_token_is_not (parser->lexer,
13037 bool function_definition_p;
13039 tree auto_result = NULL_TREE;
13041 if (saw_declarator)
13043 /* If we are processing next declarator, comma is expected */
13044 token = cp_lexer_peek_token (parser->lexer);
13045 gcc_assert (token->type == CPP_COMMA);
13046 cp_lexer_consume_token (parser->lexer);
13047 if (maybe_range_for_decl)
13049 *maybe_range_for_decl = error_mark_node;
13050 if (comma_loc == UNKNOWN_LOCATION)
13051 comma_loc = token->location;
13055 saw_declarator = true;
13057 /* Parse the init-declarator. */
13058 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13060 function_definition_allowed_p,
13061 /*member_p=*/false,
13062 declares_class_or_enum,
13063 &function_definition_p,
13064 maybe_range_for_decl,
13067 /* If an error occurred while parsing tentatively, exit quickly.
13068 (That usually happens when in the body of a function; each
13069 statement is treated as a declaration-statement until proven
13071 if (cp_parser_error_occurred (parser))
13074 if (auto_specifier_p && cxx_dialect >= cxx14)
13076 /* If the init-declarator-list contains more than one
13077 init-declarator, they shall all form declarations of
13079 if (auto_function_declaration == NULL_TREE)
13080 auto_function_declaration
13081 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13082 else if (TREE_CODE (decl) == FUNCTION_DECL
13083 || auto_function_declaration != error_mark_node)
13085 error_at (decl_specifiers.locations[ds_type_spec],
13086 "non-variable %qD in declaration with more than one "
13087 "declarator with placeholder type",
13088 TREE_CODE (decl) == FUNCTION_DECL
13089 ? decl : auto_function_declaration);
13090 auto_function_declaration = error_mark_node;
13095 && (!processing_template_decl || !type_uses_auto (auto_result)))
13098 && last_type != error_mark_node
13099 && !same_type_p (auto_result, last_type))
13101 /* If the list of declarators contains more than one declarator,
13102 the type of each declared variable is determined as described
13103 above. If the type deduced for the template parameter U is not
13104 the same in each deduction, the program is ill-formed. */
13105 error_at (decl_specifiers.locations[ds_type_spec],
13106 "inconsistent deduction for %qT: %qT and then %qT",
13107 decl_specifiers.type, last_type, auto_result);
13108 last_type = error_mark_node;
13111 last_type = auto_result;
13114 /* Handle function definitions specially. */
13115 if (function_definition_p)
13117 /* If the next token is a `,', then we are probably
13118 processing something like:
13122 which is erroneous. */
13123 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13125 cp_token *token = cp_lexer_peek_token (parser->lexer);
13126 error_at (token->location,
13128 " declarations and function-definitions is forbidden");
13130 /* Otherwise, we're done with the list of declarators. */
13133 pop_deferring_access_checks ();
13137 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13138 *maybe_range_for_decl = decl;
13139 /* The next token should be either a `,' or a `;'. */
13140 token = cp_lexer_peek_token (parser->lexer);
13141 /* If it's a `,', there are more declarators to come. */
13142 if (token->type == CPP_COMMA)
13143 /* will be consumed next time around */;
13144 /* If it's a `;', we are done. */
13145 else if (token->type == CPP_SEMICOLON)
13147 else if (maybe_range_for_decl)
13149 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13150 permerror (decl_specifiers.locations[ds_type_spec],
13151 "types may not be defined in a for-range-declaration");
13154 /* Anything else is an error. */
13157 /* If we have already issued an error message we don't need
13158 to issue another one. */
13159 if ((decl != error_mark_node
13160 && DECL_INITIAL (decl) != error_mark_node)
13161 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13162 cp_parser_error (parser, "expected %<,%> or %<;%>");
13163 /* Skip tokens until we reach the end of the statement. */
13164 cp_parser_skip_to_end_of_statement (parser);
13165 /* If the next token is now a `;', consume it. */
13166 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13167 cp_lexer_consume_token (parser->lexer);
13170 /* After the first time around, a function-definition is not
13171 allowed -- even if it was OK at first. For example:
13176 function_definition_allowed_p = false;
13179 /* Issue an error message if no declarators are present, and the
13180 decl-specifier-seq does not itself declare a class or
13181 enumeration: [dcl.dcl]/3. */
13182 if (!saw_declarator)
13184 if (cp_parser_declares_only_class_p (parser))
13186 if (!declares_class_or_enum
13187 && decl_specifiers.type
13188 && OVERLOAD_TYPE_P (decl_specifiers.type))
13189 /* Ensure an error is issued anyway when finish_decltype_type,
13190 called via cp_parser_decl_specifier_seq, returns a class or
13191 an enumeration (c++/51786). */
13192 decl_specifiers.type = NULL_TREE;
13193 shadow_tag (&decl_specifiers);
13195 /* Perform any deferred access checks. */
13196 perform_deferred_access_checks (tf_warning_or_error);
13199 /* Consume the `;'. */
13201 if (!maybe_range_for_decl)
13202 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13203 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13205 if (init_loc != UNKNOWN_LOCATION)
13206 error_at (init_loc, "initializer in range-based %<for%> loop");
13207 if (comma_loc != UNKNOWN_LOCATION)
13208 error_at (comma_loc,
13209 "multiple declarations in range-based %<for%> loop");
13213 pop_deferring_access_checks ();
13216 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13217 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13221 cp_parser_decomposition_declaration (cp_parser *parser,
13222 cp_decl_specifier_seq *decl_specifiers,
13223 tree *maybe_range_for_decl,
13224 location_t *init_loc)
13226 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13227 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13228 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13230 /* Parse the identifier-list. */
13231 auto_vec<cp_expr, 10> v;
13232 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13235 cp_expr e = cp_parser_identifier (parser);
13236 if (e.get_value () == error_mark_node)
13239 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13241 cp_lexer_consume_token (parser->lexer);
13244 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13245 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13247 end_loc = UNKNOWN_LOCATION;
13248 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13250 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13251 cp_lexer_consume_token (parser->lexer);
13254 cp_parser_skip_to_end_of_statement (parser);
13255 return error_mark_node;
13259 if (cxx_dialect < cxx17)
13260 pedwarn (loc, 0, "structured bindings only available with "
13261 "-std=c++17 or -std=gnu++17");
13264 cp_declarator *declarator = make_declarator (cdk_decomp);
13265 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13266 declarator->id_loc = loc;
13267 if (ref_qual != REF_QUAL_NONE)
13268 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13269 ref_qual == REF_QUAL_RVALUE,
13271 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13272 NULL_TREE, decl_specifiers->attributes,
13274 tree orig_decl = decl;
13278 cp_decl_specifier_seq decl_specs;
13279 clear_decl_specs (&decl_specs);
13280 decl_specs.type = make_auto ();
13282 FOR_EACH_VEC_ELT (v, i, e)
13285 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13287 declarator->u.id.unqualified_name = e.get_value ();
13288 declarator->id_loc = e.get_location ();
13289 tree elt_pushed_scope;
13290 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13291 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13292 if (decl2 == error_mark_node)
13293 decl = error_mark_node;
13294 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13296 /* Ensure we've diagnosed redeclaration if we aren't creating
13298 gcc_assert (errorcount);
13299 decl = error_mark_node;
13303 if (elt_pushed_scope)
13304 pop_scope (elt_pushed_scope);
13309 error_at (loc, "empty structured binding declaration");
13310 decl = error_mark_node;
13313 if (maybe_range_for_decl == NULL
13314 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13316 bool non_constant_p = false, is_direct_init = false;
13317 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13318 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13320 if (initializer == NULL_TREE
13321 || (TREE_CODE (initializer) == TREE_LIST
13322 && TREE_CHAIN (initializer))
13324 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13325 && CONSTRUCTOR_NELTS (initializer) != 1))
13327 error_at (loc, "invalid initializer for structured binding "
13329 initializer = error_mark_node;
13332 if (decl != error_mark_node)
13334 cp_maybe_mangle_decomp (decl, prev, v.length ());
13335 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13336 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13337 cp_finish_decomp (decl, prev, v.length ());
13340 else if (decl != error_mark_node)
13342 *maybe_range_for_decl = prev;
13343 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13344 the underlying DECL. */
13345 cp_finish_decomp (decl, prev, v.length ());
13349 pop_scope (pushed_scope);
13351 if (decl == error_mark_node && DECL_P (orig_decl))
13353 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13354 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13360 /* Parse a decl-specifier-seq.
13362 decl-specifier-seq:
13363 decl-specifier-seq [opt] decl-specifier
13364 decl-specifier attribute-specifier-seq [opt] (C++11)
13367 storage-class-specifier
13378 Concepts Extension:
13383 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13385 The parser flags FLAGS is used to control type-specifier parsing.
13387 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13390 1: one of the decl-specifiers is an elaborated-type-specifier
13391 (i.e., a type declaration)
13392 2: one of the decl-specifiers is an enum-specifier or a
13393 class-specifier (i.e., a type definition)
13398 cp_parser_decl_specifier_seq (cp_parser* parser,
13399 cp_parser_flags flags,
13400 cp_decl_specifier_seq *decl_specs,
13401 int* declares_class_or_enum)
13403 bool constructor_possible_p = !parser->in_declarator_p;
13404 bool found_decl_spec = false;
13405 cp_token *start_token = NULL;
13408 /* Clear DECL_SPECS. */
13409 clear_decl_specs (decl_specs);
13411 /* Assume no class or enumeration type is declared. */
13412 *declares_class_or_enum = 0;
13414 /* Keep reading specifiers until there are no more to read. */
13417 bool constructor_p;
13421 /* Peek at the next token. */
13422 token = cp_lexer_peek_token (parser->lexer);
13424 /* Save the first token of the decl spec list for error
13427 start_token = token;
13428 /* Handle attributes. */
13429 if (cp_next_tokens_can_be_attribute_p (parser))
13431 /* Parse the attributes. */
13432 tree attrs = cp_parser_attributes_opt (parser);
13434 /* In a sequence of declaration specifiers, c++11 attributes
13435 appertain to the type that precede them. In that case
13438 The attribute-specifier-seq affects the type only for
13439 the declaration it appears in, not other declarations
13440 involving the same type.
13442 But for now let's force the user to position the
13443 attribute either at the beginning of the declaration or
13444 after the declarator-id, which would clearly mean that it
13445 applies to the declarator. */
13446 if (cxx11_attribute_p (attrs))
13448 if (!found_decl_spec)
13449 /* The c++11 attribute is at the beginning of the
13450 declaration. It appertains to the entity being
13454 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13456 /* This is an attribute following a
13457 class-specifier. */
13458 if (decl_specs->type_definition_p)
13459 warn_misplaced_attr_for_class_type (token->location,
13465 decl_specs->std_attributes
13466 = attr_chainon (decl_specs->std_attributes, attrs);
13467 if (decl_specs->locations[ds_std_attribute] == 0)
13468 decl_specs->locations[ds_std_attribute] = token->location;
13474 decl_specs->attributes
13475 = attr_chainon (decl_specs->attributes, attrs);
13476 if (decl_specs->locations[ds_attribute] == 0)
13477 decl_specs->locations[ds_attribute] = token->location;
13480 /* Assume we will find a decl-specifier keyword. */
13481 found_decl_spec = true;
13482 /* If the next token is an appropriate keyword, we can simply
13483 add it to the list. */
13484 switch (token->keyword)
13490 if (!at_class_scope_p ())
13492 gcc_rich_location richloc (token->location);
13493 richloc.add_fixit_remove ();
13494 error_at (&richloc, "%<friend%> used outside of class");
13495 cp_lexer_purge_token (parser->lexer);
13500 /* Consume the token. */
13501 cp_lexer_consume_token (parser->lexer);
13505 case RID_CONSTEXPR:
13507 cp_lexer_consume_token (parser->lexer);
13512 cp_lexer_consume_token (parser->lexer);
13515 /* function-specifier:
13522 cp_parser_function_specifier_opt (parser, decl_specs);
13529 /* Consume the token. */
13530 cp_lexer_consume_token (parser->lexer);
13531 /* A constructor declarator cannot appear in a typedef. */
13532 constructor_possible_p = false;
13533 /* The "typedef" keyword can only occur in a declaration; we
13534 may as well commit at this point. */
13535 cp_parser_commit_to_tentative_parse (parser);
13537 if (decl_specs->storage_class != sc_none)
13538 decl_specs->conflicting_specifiers_p = true;
13541 /* storage-class-specifier:
13551 if (cxx_dialect == cxx98)
13553 /* Consume the token. */
13554 cp_lexer_consume_token (parser->lexer);
13556 /* Complain about `auto' as a storage specifier, if
13557 we're complaining about C++0x compatibility. */
13558 gcc_rich_location richloc (token->location);
13559 richloc.add_fixit_remove ();
13560 warning_at (&richloc, OPT_Wc__11_compat,
13561 "%<auto%> changes meaning in C++11; "
13562 "please remove it");
13564 /* Set the storage class anyway. */
13565 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13569 /* C++0x auto type-specifier. */
13570 found_decl_spec = false;
13577 /* Consume the token. */
13578 cp_lexer_consume_token (parser->lexer);
13579 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13583 /* Consume the token. */
13585 cp_lexer_consume_token (parser->lexer);
13589 /* We did not yet find a decl-specifier yet. */
13590 found_decl_spec = false;
13594 if (found_decl_spec
13595 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13596 && token->keyword != RID_CONSTEXPR)
13597 error ("decl-specifier invalid in condition");
13599 if (found_decl_spec
13600 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13601 && token->keyword != RID_MUTABLE
13602 && token->keyword != RID_CONSTEXPR)
13603 error_at (token->location, "%qD invalid in lambda",
13604 ridpointers[token->keyword]);
13607 set_and_check_decl_spec_loc (decl_specs, ds, token);
13609 /* Constructors are a special case. The `S' in `S()' is not a
13610 decl-specifier; it is the beginning of the declarator. */
13612 = (!found_decl_spec
13613 && constructor_possible_p
13614 && (cp_parser_constructor_declarator_p
13615 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13617 /* If we don't have a DECL_SPEC yet, then we must be looking at
13618 a type-specifier. */
13619 if (!found_decl_spec && !constructor_p)
13621 int decl_spec_declares_class_or_enum;
13622 bool is_cv_qualifier;
13626 = cp_parser_type_specifier (parser, flags,
13628 /*is_declaration=*/true,
13629 &decl_spec_declares_class_or_enum,
13631 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13633 /* If this type-specifier referenced a user-defined type
13634 (a typedef, class-name, etc.), then we can't allow any
13635 more such type-specifiers henceforth.
13639 The longest sequence of decl-specifiers that could
13640 possibly be a type name is taken as the
13641 decl-specifier-seq of a declaration. The sequence shall
13642 be self-consistent as described below.
13646 As a general rule, at most one type-specifier is allowed
13647 in the complete decl-specifier-seq of a declaration. The
13648 only exceptions are the following:
13650 -- const or volatile can be combined with any other
13653 -- signed or unsigned can be combined with char, long,
13661 void g (const int Pc);
13663 Here, Pc is *not* part of the decl-specifier seq; it's
13664 the declarator. Therefore, once we see a type-specifier
13665 (other than a cv-qualifier), we forbid any additional
13666 user-defined types. We *do* still allow things like `int
13667 int' to be considered a decl-specifier-seq, and issue the
13668 error message later. */
13669 if (type_spec && !is_cv_qualifier)
13670 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13671 /* A constructor declarator cannot follow a type-specifier. */
13674 constructor_possible_p = false;
13675 found_decl_spec = true;
13676 if (!is_cv_qualifier)
13677 decl_specs->any_type_specifiers_p = true;
13681 /* If we still do not have a DECL_SPEC, then there are no more
13682 decl-specifiers. */
13683 if (!found_decl_spec)
13686 decl_specs->any_specifiers_p = true;
13687 /* After we see one decl-specifier, further decl-specifiers are
13688 always optional. */
13689 flags |= CP_PARSER_FLAGS_OPTIONAL;
13692 /* Don't allow a friend specifier with a class definition. */
13693 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13694 && (*declares_class_or_enum & 2))
13695 error_at (decl_specs->locations[ds_friend],
13696 "class definition may not be declared a friend");
13699 /* Parse an (optional) storage-class-specifier.
13701 storage-class-specifier:
13710 storage-class-specifier:
13713 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13716 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13718 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13721 if (cxx_dialect != cxx98)
13723 /* Fall through for C++98. */
13724 gcc_fallthrough ();
13731 /* Consume the token. */
13732 return cp_lexer_consume_token (parser->lexer)->u.value;
13739 /* Parse an (optional) function-specifier.
13741 function-specifier:
13746 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13747 Updates DECL_SPECS, if it is non-NULL. */
13750 cp_parser_function_specifier_opt (cp_parser* parser,
13751 cp_decl_specifier_seq *decl_specs)
13753 cp_token *token = cp_lexer_peek_token (parser->lexer);
13754 switch (token->keyword)
13757 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13761 /* 14.5.2.3 [temp.mem]
13763 A member function template shall not be virtual. */
13764 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13765 && current_class_type)
13766 error_at (token->location, "templates may not be %<virtual%>");
13768 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13772 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13779 /* Consume the token. */
13780 return cp_lexer_consume_token (parser->lexer)->u.value;
13783 /* Parse a linkage-specification.
13785 linkage-specification:
13786 extern string-literal { declaration-seq [opt] }
13787 extern string-literal declaration */
13790 cp_parser_linkage_specification (cp_parser* parser)
13794 /* Look for the `extern' keyword. */
13795 cp_token *extern_token
13796 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13798 /* Look for the string-literal. */
13799 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13800 linkage = cp_parser_string_literal (parser, false, false);
13802 /* Transform the literal into an identifier. If the literal is a
13803 wide-character string, or contains embedded NULs, then we can't
13804 handle it as the user wants. */
13805 if (strlen (TREE_STRING_POINTER (linkage))
13806 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13808 cp_parser_error (parser, "invalid linkage-specification");
13809 /* Assume C++ linkage. */
13810 linkage = lang_name_cplusplus;
13813 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13815 /* We're now using the new linkage. */
13816 push_lang_context (linkage);
13818 /* Preserve the location of the the innermost linkage specification,
13819 tracking the locations of nested specifications via a local. */
13820 location_t saved_location
13821 = parser->innermost_linkage_specification_location;
13822 /* Construct a location ranging from the start of the "extern" to
13823 the end of the string-literal, with the caret at the start, e.g.:
13827 parser->innermost_linkage_specification_location
13828 = make_location (extern_token->location,
13829 extern_token->location,
13830 get_finish (string_token->location));
13832 /* If the next token is a `{', then we're using the first
13834 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13836 cp_ensure_no_omp_declare_simd (parser);
13837 cp_ensure_no_oacc_routine (parser);
13839 /* Consume the `{' token. */
13840 matching_braces braces;
13841 braces.consume_open (parser)->location;
13842 /* Parse the declarations. */
13843 cp_parser_declaration_seq_opt (parser);
13844 /* Look for the closing `}'. */
13845 braces.require_close (parser);
13847 /* Otherwise, there's just one declaration. */
13850 bool saved_in_unbraced_linkage_specification_p;
13852 saved_in_unbraced_linkage_specification_p
13853 = parser->in_unbraced_linkage_specification_p;
13854 parser->in_unbraced_linkage_specification_p = true;
13855 cp_parser_declaration (parser);
13856 parser->in_unbraced_linkage_specification_p
13857 = saved_in_unbraced_linkage_specification_p;
13860 /* We're done with the linkage-specification. */
13861 pop_lang_context ();
13863 /* Restore location of parent linkage specification, if any. */
13864 parser->innermost_linkage_specification_location = saved_location;
13867 /* Parse a static_assert-declaration.
13869 static_assert-declaration:
13870 static_assert ( constant-expression , string-literal ) ;
13871 static_assert ( constant-expression ) ; (C++17)
13873 If MEMBER_P, this static_assert is a class member. */
13876 cp_parser_static_assert(cp_parser *parser, bool member_p)
13879 location_t token_loc;
13883 /* Peek at the `static_assert' token so we can keep track of exactly
13884 where the static assertion started. */
13885 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13887 /* Look for the `static_assert' keyword. */
13888 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13892 /* We know we are in a static assertion; commit to any tentative
13894 if (cp_parser_parsing_tentatively (parser))
13895 cp_parser_commit_to_tentative_parse (parser);
13897 /* Parse the `(' starting the static assertion condition. */
13898 matching_parens parens;
13899 parens.require_open (parser);
13901 /* Parse the constant-expression. Allow a non-constant expression
13902 here in order to give better diagnostics in finish_static_assert. */
13904 cp_parser_constant_expression (parser,
13905 /*allow_non_constant_p=*/true,
13906 /*non_constant_p=*/&dummy);
13908 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13910 if (cxx_dialect < cxx17)
13911 pedwarn (input_location, OPT_Wpedantic,
13912 "static_assert without a message "
13913 "only available with -std=c++17 or -std=gnu++17");
13915 cp_lexer_consume_token (parser->lexer);
13916 message = build_string (1, "");
13917 TREE_TYPE (message) = char_array_type_node;
13918 fix_string_type (message);
13922 /* Parse the separating `,'. */
13923 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13925 /* Parse the string-literal message. */
13926 message = cp_parser_string_literal (parser,
13927 /*translate=*/false,
13930 /* A `)' completes the static assertion. */
13931 if (!parens.require_close (parser))
13932 cp_parser_skip_to_closing_parenthesis (parser,
13933 /*recovering=*/true,
13934 /*or_comma=*/false,
13935 /*consume_paren=*/true);
13938 /* A semicolon terminates the declaration. */
13939 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13941 /* Get the location for the static assertion. Use that of the
13942 condition if available, otherwise, use that of the "static_assert"
13944 location_t assert_loc = condition.get_location ();
13945 if (assert_loc == UNKNOWN_LOCATION)
13946 assert_loc = token_loc;
13948 /* Complete the static assertion, which may mean either processing
13949 the static assert now or saving it for template instantiation. */
13950 finish_static_assert (condition, message, assert_loc, member_p);
13953 /* Parse the expression in decltype ( expression ). */
13956 cp_parser_decltype_expr (cp_parser *parser,
13957 bool &id_expression_or_member_access_p)
13959 cp_token *id_expr_start_token;
13962 /* Since we're going to preserve any side-effects from this parse, set up a
13963 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13964 in the expression. */
13965 tentative_firewall firewall (parser);
13967 /* First, try parsing an id-expression. */
13968 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13969 cp_parser_parse_tentatively (parser);
13970 expr = cp_parser_id_expression (parser,
13971 /*template_keyword_p=*/false,
13972 /*check_dependency_p=*/true,
13973 /*template_p=*/NULL,
13974 /*declarator_p=*/false,
13975 /*optional_p=*/false);
13977 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13979 bool non_integral_constant_expression_p = false;
13980 tree id_expression = expr;
13982 const char *error_msg;
13984 if (identifier_p (expr))
13985 /* Lookup the name we got back from the id-expression. */
13986 expr = cp_parser_lookup_name_simple (parser, expr,
13987 id_expr_start_token->location);
13989 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
13990 /* A template without args is not a complete id-expression. */
13991 expr = error_mark_node;
13994 && expr != error_mark_node
13995 && TREE_CODE (expr) != TYPE_DECL
13996 && (TREE_CODE (expr) != BIT_NOT_EXPR
13997 || !TYPE_P (TREE_OPERAND (expr, 0)))
13998 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14000 /* Complete lookup of the id-expression. */
14001 expr = (finish_id_expression
14002 (id_expression, expr, parser->scope, &idk,
14003 /*integral_constant_expression_p=*/false,
14004 /*allow_non_integral_constant_expression_p=*/true,
14005 &non_integral_constant_expression_p,
14006 /*template_p=*/false,
14008 /*address_p=*/false,
14009 /*template_arg_p=*/false,
14011 id_expr_start_token->location));
14013 if (expr == error_mark_node)
14014 /* We found an id-expression, but it was something that we
14015 should not have found. This is an error, not something
14016 we can recover from, so note that we found an
14017 id-expression and we'll recover as gracefully as
14019 id_expression_or_member_access_p = true;
14023 && expr != error_mark_node
14024 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14025 /* We have an id-expression. */
14026 id_expression_or_member_access_p = true;
14029 if (!id_expression_or_member_access_p)
14031 /* Abort the id-expression parse. */
14032 cp_parser_abort_tentative_parse (parser);
14034 /* Parsing tentatively, again. */
14035 cp_parser_parse_tentatively (parser);
14037 /* Parse a class member access. */
14038 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14039 /*cast_p=*/false, /*decltype*/true,
14040 /*member_access_only_p=*/true, NULL);
14043 && expr != error_mark_node
14044 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14045 /* We have an id-expression. */
14046 id_expression_or_member_access_p = true;
14049 if (id_expression_or_member_access_p)
14050 /* We have parsed the complete id-expression or member access. */
14051 cp_parser_parse_definitely (parser);
14054 /* Abort our attempt to parse an id-expression or member access
14056 cp_parser_abort_tentative_parse (parser);
14058 /* Commit to the tentative_firewall so we get syntax errors. */
14059 cp_parser_commit_to_tentative_parse (parser);
14061 /* Parse a full expression. */
14062 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14063 /*decltype_p=*/true);
14069 /* Parse a `decltype' type. Returns the type.
14071 simple-type-specifier:
14072 decltype ( expression )
14074 decltype ( auto ) */
14077 cp_parser_decltype (cp_parser *parser)
14079 bool id_expression_or_member_access_p = false;
14080 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14082 if (start_token->type == CPP_DECLTYPE)
14084 /* Already parsed. */
14085 cp_lexer_consume_token (parser->lexer);
14086 return saved_checks_value (start_token->u.tree_check_value);
14089 /* Look for the `decltype' token. */
14090 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14091 return error_mark_node;
14093 /* Parse the opening `('. */
14094 matching_parens parens;
14095 if (!parens.require_open (parser))
14096 return error_mark_node;
14098 push_deferring_access_checks (dk_deferred);
14100 tree expr = NULL_TREE;
14102 if (cxx_dialect >= cxx14
14103 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14104 /* decltype (auto) */
14105 cp_lexer_consume_token (parser->lexer);
14108 /* decltype (expression) */
14110 /* Types cannot be defined in a `decltype' expression. Save away the
14111 old message and set the new one. */
14112 const char *saved_message = parser->type_definition_forbidden_message;
14113 parser->type_definition_forbidden_message
14114 = G_("types may not be defined in %<decltype%> expressions");
14116 /* The restrictions on constant-expressions do not apply inside
14117 decltype expressions. */
14118 bool saved_integral_constant_expression_p
14119 = parser->integral_constant_expression_p;
14120 bool saved_non_integral_constant_expression_p
14121 = parser->non_integral_constant_expression_p;
14122 parser->integral_constant_expression_p = false;
14124 /* Within a parenthesized expression, a `>' token is always
14125 the greater-than operator. */
14126 bool saved_greater_than_is_operator_p
14127 = parser->greater_than_is_operator_p;
14128 parser->greater_than_is_operator_p = true;
14130 /* Do not actually evaluate the expression. */
14131 ++cp_unevaluated_operand;
14133 /* Do not warn about problems with the expression. */
14134 ++c_inhibit_evaluation_warnings;
14136 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14138 /* Go back to evaluating expressions. */
14139 --cp_unevaluated_operand;
14140 --c_inhibit_evaluation_warnings;
14142 /* The `>' token might be the end of a template-id or
14143 template-parameter-list now. */
14144 parser->greater_than_is_operator_p
14145 = saved_greater_than_is_operator_p;
14147 /* Restore the old message and the integral constant expression
14149 parser->type_definition_forbidden_message = saved_message;
14150 parser->integral_constant_expression_p
14151 = saved_integral_constant_expression_p;
14152 parser->non_integral_constant_expression_p
14153 = saved_non_integral_constant_expression_p;
14156 /* Parse to the closing `)'. */
14157 if (!parens.require_close (parser))
14159 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14160 /*consume_paren=*/true);
14161 pop_deferring_access_checks ();
14162 return error_mark_node;
14168 expr = make_decltype_auto ();
14169 AUTO_IS_DECLTYPE (expr) = true;
14172 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14173 tf_warning_or_error);
14175 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14177 start_token->type = CPP_DECLTYPE;
14178 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14179 start_token->u.tree_check_value->value = expr;
14180 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14181 start_token->keyword = RID_MAX;
14182 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14184 pop_to_parent_deferring_access_checks ();
14189 /* Special member functions [gram.special] */
14191 /* Parse a conversion-function-id.
14193 conversion-function-id:
14194 operator conversion-type-id
14196 Returns an IDENTIFIER_NODE representing the operator. */
14199 cp_parser_conversion_function_id (cp_parser* parser)
14203 tree saved_qualifying_scope;
14204 tree saved_object_scope;
14205 tree pushed_scope = NULL_TREE;
14207 /* Look for the `operator' token. */
14208 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14209 return error_mark_node;
14210 /* When we parse the conversion-type-id, the current scope will be
14211 reset. However, we need that information in able to look up the
14212 conversion function later, so we save it here. */
14213 saved_scope = parser->scope;
14214 saved_qualifying_scope = parser->qualifying_scope;
14215 saved_object_scope = parser->object_scope;
14216 /* We must enter the scope of the class so that the names of
14217 entities declared within the class are available in the
14218 conversion-type-id. For example, consider:
14225 S::operator I() { ... }
14227 In order to see that `I' is a type-name in the definition, we
14228 must be in the scope of `S'. */
14230 pushed_scope = push_scope (saved_scope);
14231 /* Parse the conversion-type-id. */
14232 type = cp_parser_conversion_type_id (parser);
14233 /* Leave the scope of the class, if any. */
14235 pop_scope (pushed_scope);
14236 /* Restore the saved scope. */
14237 parser->scope = saved_scope;
14238 parser->qualifying_scope = saved_qualifying_scope;
14239 parser->object_scope = saved_object_scope;
14240 /* If the TYPE is invalid, indicate failure. */
14241 if (type == error_mark_node)
14242 return error_mark_node;
14243 return make_conv_op_name (type);
14246 /* Parse a conversion-type-id:
14248 conversion-type-id:
14249 type-specifier-seq conversion-declarator [opt]
14251 Returns the TYPE specified. */
14254 cp_parser_conversion_type_id (cp_parser* parser)
14257 cp_decl_specifier_seq type_specifiers;
14258 cp_declarator *declarator;
14259 tree type_specified;
14260 const char *saved_message;
14262 /* Parse the attributes. */
14263 attributes = cp_parser_attributes_opt (parser);
14265 saved_message = parser->type_definition_forbidden_message;
14266 parser->type_definition_forbidden_message
14267 = G_("types may not be defined in a conversion-type-id");
14269 /* Parse the type-specifiers. */
14270 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14271 /*is_trailing_return=*/false,
14274 parser->type_definition_forbidden_message = saved_message;
14276 /* If that didn't work, stop. */
14277 if (type_specifiers.type == error_mark_node)
14278 return error_mark_node;
14279 /* Parse the conversion-declarator. */
14280 declarator = cp_parser_conversion_declarator_opt (parser);
14282 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14283 /*initialized=*/0, &attributes);
14285 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14287 /* Don't give this error when parsing tentatively. This happens to
14288 work because we always parse this definitively once. */
14289 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14290 && type_uses_auto (type_specified))
14292 if (cxx_dialect < cxx14)
14294 error ("invalid use of %<auto%> in conversion operator");
14295 return error_mark_node;
14297 else if (template_parm_scope_p ())
14298 warning (0, "use of %<auto%> in member template "
14299 "conversion operator can never be deduced");
14302 return type_specified;
14305 /* Parse an (optional) conversion-declarator.
14307 conversion-declarator:
14308 ptr-operator conversion-declarator [opt]
14312 static cp_declarator *
14313 cp_parser_conversion_declarator_opt (cp_parser* parser)
14315 enum tree_code code;
14316 tree class_type, std_attributes = NULL_TREE;
14317 cp_cv_quals cv_quals;
14319 /* We don't know if there's a ptr-operator next, or not. */
14320 cp_parser_parse_tentatively (parser);
14321 /* Try the ptr-operator. */
14322 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14324 /* If it worked, look for more conversion-declarators. */
14325 if (cp_parser_parse_definitely (parser))
14327 cp_declarator *declarator;
14329 /* Parse another optional declarator. */
14330 declarator = cp_parser_conversion_declarator_opt (parser);
14332 declarator = cp_parser_make_indirect_declarator
14333 (code, class_type, cv_quals, declarator, std_attributes);
14341 /* Parse an (optional) ctor-initializer.
14344 : mem-initializer-list */
14347 cp_parser_ctor_initializer_opt (cp_parser* parser)
14349 /* If the next token is not a `:', then there is no
14350 ctor-initializer. */
14351 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14353 /* Do default initialization of any bases and members. */
14354 if (DECL_CONSTRUCTOR_P (current_function_decl))
14355 finish_mem_initializers (NULL_TREE);
14359 /* Consume the `:' token. */
14360 cp_lexer_consume_token (parser->lexer);
14361 /* And the mem-initializer-list. */
14362 cp_parser_mem_initializer_list (parser);
14365 /* Parse a mem-initializer-list.
14367 mem-initializer-list:
14368 mem-initializer ... [opt]
14369 mem-initializer ... [opt] , mem-initializer-list */
14372 cp_parser_mem_initializer_list (cp_parser* parser)
14374 tree mem_initializer_list = NULL_TREE;
14375 tree target_ctor = error_mark_node;
14376 cp_token *token = cp_lexer_peek_token (parser->lexer);
14378 /* Let the semantic analysis code know that we are starting the
14379 mem-initializer-list. */
14380 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14381 error_at (token->location,
14382 "only constructors take member initializers");
14384 /* Loop through the list. */
14387 tree mem_initializer;
14389 token = cp_lexer_peek_token (parser->lexer);
14390 /* Parse the mem-initializer. */
14391 mem_initializer = cp_parser_mem_initializer (parser);
14392 /* If the next token is a `...', we're expanding member initializers. */
14393 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14395 || (mem_initializer != error_mark_node
14396 && check_for_bare_parameter_packs (TREE_PURPOSE
14397 (mem_initializer))))
14399 /* Consume the `...'. */
14401 cp_lexer_consume_token (parser->lexer);
14403 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14404 can be expanded but members cannot. */
14405 if (mem_initializer != error_mark_node
14406 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14408 error_at (token->location,
14409 "cannot expand initializer for member %qD",
14410 TREE_PURPOSE (mem_initializer));
14411 mem_initializer = error_mark_node;
14414 /* Construct the pack expansion type. */
14415 if (mem_initializer != error_mark_node)
14416 mem_initializer = make_pack_expansion (mem_initializer);
14418 if (target_ctor != error_mark_node
14419 && mem_initializer != error_mark_node)
14421 error ("mem-initializer for %qD follows constructor delegation",
14422 TREE_PURPOSE (mem_initializer));
14423 mem_initializer = error_mark_node;
14425 /* Look for a target constructor. */
14426 if (mem_initializer != error_mark_node
14427 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14428 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14430 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14431 if (mem_initializer_list)
14433 error ("constructor delegation follows mem-initializer for %qD",
14434 TREE_PURPOSE (mem_initializer_list));
14435 mem_initializer = error_mark_node;
14437 target_ctor = mem_initializer;
14439 /* Add it to the list, unless it was erroneous. */
14440 if (mem_initializer != error_mark_node)
14442 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14443 mem_initializer_list = mem_initializer;
14445 /* If the next token is not a `,', we're done. */
14446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14448 /* Consume the `,' token. */
14449 cp_lexer_consume_token (parser->lexer);
14452 /* Perform semantic analysis. */
14453 if (DECL_CONSTRUCTOR_P (current_function_decl))
14454 finish_mem_initializers (mem_initializer_list);
14457 /* Parse a mem-initializer.
14460 mem-initializer-id ( expression-list [opt] )
14461 mem-initializer-id braced-init-list
14466 ( expression-list [opt] )
14468 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14469 class) or FIELD_DECL (for a non-static data member) to initialize;
14470 the TREE_VALUE is the expression-list. An empty initialization
14471 list is represented by void_list_node. */
14474 cp_parser_mem_initializer (cp_parser* parser)
14476 tree mem_initializer_id;
14477 tree expression_list;
14479 cp_token *token = cp_lexer_peek_token (parser->lexer);
14481 /* Find out what is being initialized. */
14482 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14484 permerror (token->location,
14485 "anachronistic old-style base class initializer");
14486 mem_initializer_id = NULL_TREE;
14490 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14491 if (mem_initializer_id == error_mark_node)
14492 return mem_initializer_id;
14494 member = expand_member_init (mem_initializer_id);
14495 if (member && !DECL_P (member))
14496 in_base_initializer = 1;
14498 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14500 bool expr_non_constant_p;
14501 cp_lexer_set_source_position (parser->lexer);
14502 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14503 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14504 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14505 expression_list = build_tree_list (NULL_TREE, expression_list);
14509 vec<tree, va_gc> *vec;
14510 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14512 /*allow_expansion_p=*/true,
14513 /*non_constant_p=*/NULL);
14515 return error_mark_node;
14516 expression_list = build_tree_list_vec (vec);
14517 release_tree_vector (vec);
14520 if (expression_list == error_mark_node)
14521 return error_mark_node;
14522 if (!expression_list)
14523 expression_list = void_type_node;
14525 in_base_initializer = 0;
14527 return member ? build_tree_list (member, expression_list) : error_mark_node;
14530 /* Parse a mem-initializer-id.
14532 mem-initializer-id:
14533 :: [opt] nested-name-specifier [opt] class-name
14534 decltype-specifier (C++11)
14537 Returns a TYPE indicating the class to be initialized for the first
14538 production (and the second in C++11). Returns an IDENTIFIER_NODE
14539 indicating the data member to be initialized for the last production. */
14542 cp_parser_mem_initializer_id (cp_parser* parser)
14544 bool global_scope_p;
14545 bool nested_name_specifier_p;
14546 bool template_p = false;
14549 cp_token *token = cp_lexer_peek_token (parser->lexer);
14551 /* `typename' is not allowed in this context ([temp.res]). */
14552 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14554 error_at (token->location,
14555 "keyword %<typename%> not allowed in this context (a qualified "
14556 "member initializer is implicitly a type)");
14557 cp_lexer_consume_token (parser->lexer);
14559 /* Look for the optional `::' operator. */
14561 = (cp_parser_global_scope_opt (parser,
14562 /*current_scope_valid_p=*/false)
14564 /* Look for the optional nested-name-specifier. The simplest way to
14569 The keyword `typename' is not permitted in a base-specifier or
14570 mem-initializer; in these contexts a qualified name that
14571 depends on a template-parameter is implicitly assumed to be a
14574 is to assume that we have seen the `typename' keyword at this
14576 nested_name_specifier_p
14577 = (cp_parser_nested_name_specifier_opt (parser,
14578 /*typename_keyword_p=*/true,
14579 /*check_dependency_p=*/true,
14581 /*is_declaration=*/true)
14583 if (nested_name_specifier_p)
14584 template_p = cp_parser_optional_template_keyword (parser);
14585 /* If there is a `::' operator or a nested-name-specifier, then we
14586 are definitely looking for a class-name. */
14587 if (global_scope_p || nested_name_specifier_p)
14588 return cp_parser_class_name (parser,
14589 /*typename_keyword_p=*/true,
14590 /*template_keyword_p=*/template_p,
14592 /*check_dependency_p=*/true,
14593 /*class_head_p=*/false,
14594 /*is_declaration=*/true);
14595 /* Otherwise, we could also be looking for an ordinary identifier. */
14596 cp_parser_parse_tentatively (parser);
14597 if (cp_lexer_next_token_is_decltype (parser->lexer))
14598 /* Try a decltype-specifier. */
14599 id = cp_parser_decltype (parser);
14601 /* Otherwise, try a class-name. */
14602 id = cp_parser_class_name (parser,
14603 /*typename_keyword_p=*/true,
14604 /*template_keyword_p=*/false,
14606 /*check_dependency_p=*/true,
14607 /*class_head_p=*/false,
14608 /*is_declaration=*/true);
14609 /* If we found one, we're done. */
14610 if (cp_parser_parse_definitely (parser))
14612 /* Otherwise, look for an ordinary identifier. */
14613 return cp_parser_identifier (parser);
14616 /* Overloading [gram.over] */
14618 /* Parse an operator-function-id.
14620 operator-function-id:
14623 Returns an IDENTIFIER_NODE for the operator which is a
14624 human-readable spelling of the identifier, e.g., `operator +'. */
14627 cp_parser_operator_function_id (cp_parser* parser)
14629 /* Look for the `operator' keyword. */
14630 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14631 return error_mark_node;
14632 /* And then the name of the operator itself. */
14633 return cp_parser_operator (parser);
14636 /* Return an identifier node for a user-defined literal operator.
14637 The suffix identifier is chained to the operator name identifier. */
14640 cp_literal_operator_id (const char* name)
14643 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14644 + strlen (name) + 10);
14645 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14646 identifier = get_identifier (buffer);
14651 /* Parse an operator.
14654 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14655 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14656 || ++ -- , ->* -> () []
14663 Returns an IDENTIFIER_NODE for the operator which is a
14664 human-readable spelling of the identifier, e.g., `operator +'. */
14667 cp_parser_operator (cp_parser* parser)
14669 tree id = NULL_TREE;
14673 /* Peek at the next token. */
14674 token = cp_lexer_peek_token (parser->lexer);
14676 location_t start_loc = token->location;
14678 /* Figure out which operator we have. */
14679 enum tree_code op = ERROR_MARK;
14680 bool assop = false;
14681 bool consumed = false;
14682 switch (token->type)
14686 /* The keyword should be either `new' or `delete'. */
14687 if (token->keyword == RID_NEW)
14689 else if (token->keyword == RID_DELETE)
14694 /* Consume the `new' or `delete' token. */
14695 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14697 /* Peek at the next token. */
14698 token = cp_lexer_peek_token (parser->lexer);
14699 /* If it's a `[' token then this is the array variant of the
14701 if (token->type == CPP_OPEN_SQUARE)
14703 /* Consume the `[' token. */
14704 cp_lexer_consume_token (parser->lexer);
14705 /* Look for the `]' token. */
14706 if (cp_token *close_token
14707 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14708 end_loc = close_token->location;
14709 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14711 start_loc = make_location (start_loc, start_loc, end_loc);
14729 op = TRUNC_DIV_EXPR;
14733 op = TRUNC_MOD_EXPR;
14753 op = TRUTH_NOT_EXPR;
14786 op = TRUNC_DIV_EXPR;
14791 op = TRUNC_MOD_EXPR;
14817 case CPP_LSHIFT_EQ:
14822 case CPP_RSHIFT_EQ:
14839 case CPP_GREATER_EQ:
14844 op = TRUTH_ANDIF_EXPR;
14848 op = TRUTH_ORIF_EXPR;
14851 case CPP_PLUS_PLUS:
14852 op = POSTINCREMENT_EXPR;
14855 case CPP_MINUS_MINUS:
14856 op = PREDECREMENT_EXPR;
14860 op = COMPOUND_EXPR;
14863 case CPP_DEREF_STAR:
14868 op = COMPONENT_REF;
14871 case CPP_OPEN_PAREN:
14873 /* Consume the `('. */
14874 matching_parens parens;
14875 parens.consume_open (parser);
14876 /* Look for the matching `)'. */
14877 parens.require_close (parser);
14883 case CPP_OPEN_SQUARE:
14884 /* Consume the `['. */
14885 cp_lexer_consume_token (parser->lexer);
14886 /* Look for the matching `]'. */
14887 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14892 case CPP_UTF8STRING:
14893 case CPP_UTF8STRING_USERDEF:
14900 case CPP_STRING_USERDEF:
14901 case CPP_WSTRING_USERDEF:
14902 case CPP_STRING16_USERDEF:
14903 case CPP_STRING32_USERDEF:
14905 tree str, string_tree;
14908 if (cxx_dialect == cxx98)
14909 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14911 /* Consume the string. */
14912 str = cp_parser_string_literal (parser, /*translate=*/true,
14913 /*wide_ok=*/true, /*lookup_udlit=*/false);
14914 if (str == error_mark_node)
14915 return error_mark_node;
14916 else if (TREE_CODE (str) == USERDEF_LITERAL)
14918 string_tree = USERDEF_LITERAL_VALUE (str);
14919 id = USERDEF_LITERAL_SUFFIX_ID (str);
14924 /* Look for the suffix identifier. */
14925 token = cp_lexer_peek_token (parser->lexer);
14926 if (token->type == CPP_NAME)
14927 id = cp_parser_identifier (parser);
14928 else if (token->type == CPP_KEYWORD)
14930 error ("unexpected keyword;"
14931 " remove space between quotes and suffix identifier");
14932 return error_mark_node;
14936 error ("expected suffix identifier");
14937 return error_mark_node;
14940 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14941 (TREE_TYPE (TREE_TYPE (string_tree))));
14942 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14945 error ("expected empty string after %<operator%> keyword");
14946 return error_mark_node;
14948 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14951 error ("invalid encoding prefix in literal operator");
14952 return error_mark_node;
14954 if (id != error_mark_node)
14956 const char *name = IDENTIFIER_POINTER (id);
14957 id = cp_literal_operator_id (name);
14963 /* Anything else is an error. */
14967 /* If we have selected an identifier, we need to consume the
14969 if (op != ERROR_MARK)
14971 id = ovl_op_identifier (assop, op);
14973 cp_lexer_consume_token (parser->lexer);
14975 /* Otherwise, no valid operator name was present. */
14978 cp_parser_error (parser, "expected operator");
14979 id = error_mark_node;
14982 return cp_expr (id, start_loc);
14985 /* Parse a template-declaration.
14987 template-declaration:
14988 export [opt] template < template-parameter-list > declaration
14990 If MEMBER_P is TRUE, this template-declaration occurs within a
14993 The grammar rule given by the standard isn't correct. What
14994 is really meant is:
14996 template-declaration:
14997 export [opt] template-parameter-list-seq
14998 decl-specifier-seq [opt] init-declarator [opt] ;
14999 export [opt] template-parameter-list-seq
15000 function-definition
15002 template-parameter-list-seq:
15003 template-parameter-list-seq [opt]
15004 template < template-parameter-list >
15006 Concept Extensions:
15008 template-parameter-list-seq:
15009 template < template-parameter-list > requires-clause [opt]
15012 requires logical-or-expression */
15015 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15017 /* Check for `export'. */
15018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15020 /* Consume the `export' token. */
15021 cp_lexer_consume_token (parser->lexer);
15022 /* Warn that we do not support `export'. */
15023 warning (0, "keyword %<export%> not implemented, and will be ignored");
15026 cp_parser_template_declaration_after_export (parser, member_p);
15029 /* Parse a template-parameter-list.
15031 template-parameter-list:
15033 template-parameter-list , template-parameter
15035 Returns a TREE_LIST. Each node represents a template parameter.
15036 The nodes are connected via their TREE_CHAINs. */
15039 cp_parser_template_parameter_list (cp_parser* parser)
15041 tree parameter_list = NULL_TREE;
15043 begin_template_parm_list ();
15045 /* The loop below parses the template parms. We first need to know
15046 the total number of template parms to be able to compute proper
15047 canonical types of each dependent type. So after the loop, when
15048 we know the total number of template parms,
15049 end_template_parm_list computes the proper canonical types and
15050 fixes up the dependent types accordingly. */
15055 bool is_parameter_pack;
15056 location_t parm_loc;
15058 /* Parse the template-parameter. */
15059 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15060 parameter = cp_parser_template_parameter (parser,
15062 &is_parameter_pack);
15063 /* Add it to the list. */
15064 if (parameter != error_mark_node)
15065 parameter_list = process_template_parm (parameter_list,
15069 is_parameter_pack);
15072 tree err_parm = build_tree_list (parameter, parameter);
15073 parameter_list = chainon (parameter_list, err_parm);
15076 /* If the next token is not a `,', we're done. */
15077 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15079 /* Otherwise, consume the `,' token. */
15080 cp_lexer_consume_token (parser->lexer);
15083 return end_template_parm_list (parameter_list);
15086 /* Parse a introduction-list.
15089 introduced-parameter
15090 introduction-list , introduced-parameter
15092 introduced-parameter:
15093 ...[opt] identifier
15095 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15096 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15097 WILDCARD_DECL will also have DECL_NAME set and token location in
15098 DECL_SOURCE_LOCATION. */
15101 cp_parser_introduction_list (cp_parser *parser)
15103 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15107 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15109 cp_lexer_consume_token (parser->lexer);
15111 /* Build placeholder. */
15112 tree parm = build_nt (WILDCARD_DECL);
15113 DECL_SOURCE_LOCATION (parm)
15114 = cp_lexer_peek_token (parser->lexer)->location;
15115 DECL_NAME (parm) = cp_parser_identifier (parser);
15116 WILDCARD_PACK_P (parm) = is_pack;
15117 vec_safe_push (introduction_vec, parm);
15119 /* If the next token is not a `,', we're done. */
15120 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15122 /* Otherwise, consume the `,' token. */
15123 cp_lexer_consume_token (parser->lexer);
15126 /* Convert the vec into a TREE_VEC. */
15127 tree introduction_list = make_tree_vec (introduction_vec->length ());
15130 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15131 TREE_VEC_ELT (introduction_list, n) = parm;
15133 release_tree_vector (introduction_vec);
15134 return introduction_list;
15137 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15138 is an abstract declarator. */
15140 static inline cp_declarator*
15141 get_id_declarator (cp_declarator *declarator)
15143 cp_declarator *d = declarator;
15144 while (d && d->kind != cdk_id)
15149 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15150 is an abstract declarator. */
15153 get_unqualified_id (cp_declarator *declarator)
15155 declarator = get_id_declarator (declarator);
15157 return declarator->u.id.unqualified_name;
15162 /* Returns true if DECL represents a constrained-parameter. */
15165 is_constrained_parameter (tree decl)
15168 && TREE_CODE (decl) == TYPE_DECL
15169 && CONSTRAINED_PARM_CONCEPT (decl)
15170 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15173 /* Returns true if PARM declares a constrained-parameter. */
15176 is_constrained_parameter (cp_parameter_declarator *parm)
15178 return is_constrained_parameter (parm->decl_specifiers.type);
15181 /* Check that the type parameter is only a declarator-id, and that its
15182 type is not cv-qualified. */
15185 cp_parser_check_constrained_type_parm (cp_parser *parser,
15186 cp_parameter_declarator *parm)
15188 if (!parm->declarator)
15191 if (parm->declarator->kind != cdk_id)
15193 cp_parser_error (parser, "invalid constrained type parameter");
15197 /* Don't allow cv-qualified type parameters. */
15198 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15199 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15201 cp_parser_error (parser, "cv-qualified type parameter");
15208 /* Finish parsing/processing a template type parameter and checking
15209 various restrictions. */
15212 cp_parser_constrained_type_template_parm (cp_parser *parser,
15214 cp_parameter_declarator* parmdecl)
15216 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15217 return finish_template_type_parm (class_type_node, id);
15219 return error_mark_node;
15223 finish_constrained_template_template_parm (tree proto, tree id)
15225 /* FIXME: This should probably be copied, and we may need to adjust
15226 the template parameter depths. */
15227 tree saved_parms = current_template_parms;
15228 begin_template_parm_list ();
15229 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15230 end_template_parm_list ();
15232 tree parm = finish_template_template_parm (class_type_node, id);
15233 current_template_parms = saved_parms;
15238 /* Finish parsing/processing a template template parameter by borrowing
15239 the template parameter list from the prototype parameter. */
15242 cp_parser_constrained_template_template_parm (cp_parser *parser,
15245 cp_parameter_declarator *parmdecl)
15247 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15248 return error_mark_node;
15249 return finish_constrained_template_template_parm (proto, id);
15252 /* Create a new non-type template parameter from the given PARM
15256 constrained_non_type_template_parm (bool *is_non_type,
15257 cp_parameter_declarator *parm)
15259 *is_non_type = true;
15260 cp_declarator *decl = parm->declarator;
15261 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15262 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15263 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15266 /* Build a constrained template parameter based on the PARMDECL
15267 declarator. The type of PARMDECL is the constrained type, which
15268 refers to the prototype template parameter that ultimately
15269 specifies the type of the declared parameter. */
15272 finish_constrained_parameter (cp_parser *parser,
15273 cp_parameter_declarator *parmdecl,
15275 bool *is_parameter_pack)
15277 tree decl = parmdecl->decl_specifiers.type;
15278 tree id = get_unqualified_id (parmdecl->declarator);
15279 tree def = parmdecl->default_argument;
15280 tree proto = DECL_INITIAL (decl);
15282 /* A template parameter constrained by a variadic concept shall also
15283 be declared as a template parameter pack. */
15284 bool is_variadic = template_parameter_pack_p (proto);
15285 if (is_variadic && !*is_parameter_pack)
15286 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15288 /* Build the parameter. Return an error if the declarator was invalid. */
15290 if (TREE_CODE (proto) == TYPE_DECL)
15291 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15292 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15293 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15296 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15297 if (parm == error_mark_node)
15298 return error_mark_node;
15300 /* Finish the parameter decl and create a node attaching the
15301 default argument and constraint. */
15302 parm = build_tree_list (def, parm);
15303 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15308 /* Returns true if the parsed type actually represents the declaration
15309 of a type template-parameter. */
15312 declares_constrained_type_template_parameter (tree type)
15314 return (is_constrained_parameter (type)
15315 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15319 /* Returns true if the parsed type actually represents the declaration of
15320 a template template-parameter. */
15323 declares_constrained_template_template_parameter (tree type)
15325 return (is_constrained_parameter (type)
15326 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15329 /* Parse a default argument for a type template-parameter.
15330 Note that diagnostics are handled in cp_parser_template_parameter. */
15333 cp_parser_default_type_template_argument (cp_parser *parser)
15335 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15337 /* Consume the `=' token. */
15338 cp_lexer_consume_token (parser->lexer);
15340 cp_token *token = cp_lexer_peek_token (parser->lexer);
15342 /* Parse the default-argument. */
15343 push_deferring_access_checks (dk_no_deferred);
15344 tree default_argument = cp_parser_type_id (parser);
15345 pop_deferring_access_checks ();
15347 if (flag_concepts && type_uses_auto (default_argument))
15349 error_at (token->location,
15350 "invalid use of %<auto%> in default template argument");
15351 return error_mark_node;
15354 return default_argument;
15357 /* Parse a default argument for a template template-parameter. */
15360 cp_parser_default_template_template_argument (cp_parser *parser)
15362 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15366 /* Consume the `='. */
15367 cp_lexer_consume_token (parser->lexer);
15368 /* Parse the id-expression. */
15369 push_deferring_access_checks (dk_no_deferred);
15370 /* save token before parsing the id-expression, for error
15372 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15373 tree default_argument
15374 = cp_parser_id_expression (parser,
15375 /*template_keyword_p=*/false,
15376 /*check_dependency_p=*/true,
15377 /*template_p=*/&is_template,
15378 /*declarator_p=*/false,
15379 /*optional_p=*/false);
15380 if (TREE_CODE (default_argument) == TYPE_DECL)
15381 /* If the id-expression was a template-id that refers to
15382 a template-class, we already have the declaration here,
15383 so no further lookup is needed. */
15386 /* Look up the name. */
15388 = cp_parser_lookup_name (parser, default_argument,
15390 /*is_template=*/is_template,
15391 /*is_namespace=*/false,
15392 /*check_dependency=*/true,
15393 /*ambiguous_decls=*/NULL,
15395 /* See if the default argument is valid. */
15396 default_argument = check_template_template_default_arg (default_argument);
15397 pop_deferring_access_checks ();
15398 return default_argument;
15401 /* Parse a template-parameter.
15403 template-parameter:
15405 parameter-declaration
15407 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15408 the parameter. The TREE_PURPOSE is the default value, if any.
15409 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15410 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15411 set to true iff this parameter is a parameter pack. */
15414 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15415 bool *is_parameter_pack)
15418 cp_parameter_declarator *parameter_declarator;
15421 /* Assume it is a type parameter or a template parameter. */
15422 *is_non_type = false;
15423 /* Assume it not a parameter pack. */
15424 *is_parameter_pack = false;
15425 /* Peek at the next token. */
15426 token = cp_lexer_peek_token (parser->lexer);
15427 /* If it is `template', we have a type-parameter. */
15428 if (token->keyword == RID_TEMPLATE)
15429 return cp_parser_type_parameter (parser, is_parameter_pack);
15430 /* If it is `class' or `typename' we do not know yet whether it is a
15431 type parameter or a non-type parameter. Consider:
15433 template <typename T, typename T::X X> ...
15437 template <class C, class D*> ...
15439 Here, the first parameter is a type parameter, and the second is
15440 a non-type parameter. We can tell by looking at the token after
15441 the identifier -- if it is a `,', `=', or `>' then we have a type
15443 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15445 /* Peek at the token after `class' or `typename'. */
15446 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15447 /* If it's an ellipsis, we have a template type parameter
15449 if (token->type == CPP_ELLIPSIS)
15450 return cp_parser_type_parameter (parser, is_parameter_pack);
15451 /* If it's an identifier, skip it. */
15452 if (token->type == CPP_NAME)
15453 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15454 /* Now, see if the token looks like the end of a template
15456 if (token->type == CPP_COMMA
15457 || token->type == CPP_EQ
15458 || token->type == CPP_GREATER)
15459 return cp_parser_type_parameter (parser, is_parameter_pack);
15462 /* Otherwise, it is a non-type parameter or a constrained parameter.
15466 When parsing a default template-argument for a non-type
15467 template-parameter, the first non-nested `>' is taken as the end
15468 of the template parameter-list rather than a greater-than
15470 parameter_declarator
15471 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15472 /*parenthesized_p=*/NULL);
15474 if (!parameter_declarator)
15475 return error_mark_node;
15477 /* If the parameter declaration is marked as a parameter pack, set
15478 *IS_PARAMETER_PACK to notify the caller. */
15479 if (parameter_declarator->template_parameter_pack_p)
15480 *is_parameter_pack = true;
15482 if (parameter_declarator->default_argument)
15484 /* Can happen in some cases of erroneous input (c++/34892). */
15485 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15486 /* Consume the `...' for better error recovery. */
15487 cp_lexer_consume_token (parser->lexer);
15490 // The parameter may have been constrained.
15491 if (is_constrained_parameter (parameter_declarator))
15492 return finish_constrained_parameter (parser,
15493 parameter_declarator,
15495 is_parameter_pack);
15497 // Now we're sure that the parameter is a non-type parameter.
15498 *is_non_type = true;
15500 parm = grokdeclarator (parameter_declarator->declarator,
15501 ¶meter_declarator->decl_specifiers,
15502 TPARM, /*initialized=*/0,
15503 /*attrlist=*/NULL);
15504 if (parm == error_mark_node)
15505 return error_mark_node;
15507 return build_tree_list (parameter_declarator->default_argument, parm);
15510 /* Parse a type-parameter.
15513 class identifier [opt]
15514 class identifier [opt] = type-id
15515 typename identifier [opt]
15516 typename identifier [opt] = type-id
15517 template < template-parameter-list > class identifier [opt]
15518 template < template-parameter-list > class identifier [opt]
15521 GNU Extension (variadic templates):
15524 class ... identifier [opt]
15525 typename ... identifier [opt]
15527 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15528 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15529 the declaration of the parameter.
15531 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15534 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15539 /* Look for a keyword to tell us what kind of parameter this is. */
15540 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15542 return error_mark_node;
15544 switch (token->keyword)
15550 tree default_argument;
15552 /* If the next token is an ellipsis, we have a template
15554 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15556 /* Consume the `...' token. */
15557 cp_lexer_consume_token (parser->lexer);
15558 maybe_warn_variadic_templates ();
15560 *is_parameter_pack = true;
15563 /* If the next token is an identifier, then it names the
15565 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15566 identifier = cp_parser_identifier (parser);
15568 identifier = NULL_TREE;
15570 /* Create the parameter. */
15571 parameter = finish_template_type_parm (class_type_node, identifier);
15573 /* If the next token is an `=', we have a default argument. */
15574 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15577 = cp_parser_default_type_template_argument (parser);
15579 /* Template parameter packs cannot have default
15581 if (*is_parameter_pack)
15584 error_at (token->location,
15585 "template parameter pack %qD cannot have a "
15586 "default argument", identifier);
15588 error_at (token->location,
15589 "template parameter packs cannot have "
15590 "default arguments");
15591 default_argument = NULL_TREE;
15593 else if (check_for_bare_parameter_packs (default_argument))
15594 default_argument = error_mark_node;
15597 default_argument = NULL_TREE;
15599 /* Create the combined representation of the parameter and the
15600 default argument. */
15601 parameter = build_tree_list (default_argument, parameter);
15608 tree default_argument;
15610 /* Look for the `<'. */
15611 cp_parser_require (parser, CPP_LESS, RT_LESS);
15612 /* Parse the template-parameter-list. */
15613 cp_parser_template_parameter_list (parser);
15614 /* Look for the `>'. */
15615 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15617 // If template requirements are present, parse them.
15620 tree reqs = get_shorthand_constraints (current_template_parms);
15621 if (tree r = cp_parser_requires_clause_opt (parser))
15622 reqs = conjoin_constraints (reqs, normalize_expression (r));
15623 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15626 /* Look for the `class' or 'typename' keywords. */
15627 cp_parser_type_parameter_key (parser);
15628 /* If the next token is an ellipsis, we have a template
15630 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15632 /* Consume the `...' token. */
15633 cp_lexer_consume_token (parser->lexer);
15634 maybe_warn_variadic_templates ();
15636 *is_parameter_pack = true;
15638 /* If the next token is an `=', then there is a
15639 default-argument. If the next token is a `>', we are at
15640 the end of the parameter-list. If the next token is a `,',
15641 then we are at the end of this parameter. */
15642 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15643 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15644 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15646 identifier = cp_parser_identifier (parser);
15647 /* Treat invalid names as if the parameter were nameless. */
15648 if (identifier == error_mark_node)
15649 identifier = NULL_TREE;
15652 identifier = NULL_TREE;
15654 /* Create the template parameter. */
15655 parameter = finish_template_template_parm (class_type_node,
15658 /* If the next token is an `=', then there is a
15659 default-argument. */
15660 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15663 = cp_parser_default_template_template_argument (parser);
15665 /* Template parameter packs cannot have default
15667 if (*is_parameter_pack)
15670 error_at (token->location,
15671 "template parameter pack %qD cannot "
15672 "have a default argument",
15675 error_at (token->location, "template parameter packs cannot "
15676 "have default arguments");
15677 default_argument = NULL_TREE;
15681 default_argument = NULL_TREE;
15683 /* Create the combined representation of the parameter and the
15684 default argument. */
15685 parameter = build_tree_list (default_argument, parameter);
15690 gcc_unreachable ();
15697 /* Parse a template-id.
15700 template-name < template-argument-list [opt] >
15702 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15703 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15704 returned. Otherwise, if the template-name names a function, or set
15705 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15706 names a class, returns a TYPE_DECL for the specialization.
15708 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15709 uninstantiated templates. */
15712 cp_parser_template_id (cp_parser *parser,
15713 bool template_keyword_p,
15714 bool check_dependency_p,
15715 enum tag_types tag_type,
15716 bool is_declaration)
15721 cp_token_position start_of_id = 0;
15722 cp_token *next_token = NULL, *next_token_2 = NULL;
15723 bool is_identifier;
15725 /* If the next token corresponds to a template-id, there is no need
15727 cp_token *token = cp_lexer_peek_token (parser->lexer);
15728 if (token->type == CPP_TEMPLATE_ID)
15730 cp_lexer_consume_token (parser->lexer);
15731 return saved_checks_value (token->u.tree_check_value);
15734 /* Avoid performing name lookup if there is no possibility of
15735 finding a template-id. */
15736 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15737 || (token->type == CPP_NAME
15738 && !cp_parser_nth_token_starts_template_argument_list_p
15741 cp_parser_error (parser, "expected template-id");
15742 return error_mark_node;
15745 /* Remember where the template-id starts. */
15746 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15747 start_of_id = cp_lexer_token_position (parser->lexer, false);
15749 push_deferring_access_checks (dk_deferred);
15751 /* Parse the template-name. */
15752 is_identifier = false;
15753 templ = cp_parser_template_name (parser, template_keyword_p,
15754 check_dependency_p,
15758 if (templ == error_mark_node || is_identifier)
15760 pop_deferring_access_checks ();
15764 /* Since we're going to preserve any side-effects from this parse, set up a
15765 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15766 in the template arguments. */
15767 tentative_firewall firewall (parser);
15769 /* If we find the sequence `[:' after a template-name, it's probably
15770 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15771 parse correctly the argument list. */
15772 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15773 == CPP_OPEN_SQUARE)
15774 && next_token->flags & DIGRAPH
15775 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15777 && !(next_token_2->flags & PREV_WHITE))
15779 cp_parser_parse_tentatively (parser);
15780 /* Change `:' into `::'. */
15781 next_token_2->type = CPP_SCOPE;
15782 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15784 cp_lexer_consume_token (parser->lexer);
15786 /* Parse the arguments. */
15787 arguments = cp_parser_enclosed_template_argument_list (parser);
15788 if (!cp_parser_parse_definitely (parser))
15790 /* If we couldn't parse an argument list, then we revert our changes
15791 and return simply an error. Maybe this is not a template-id
15793 next_token_2->type = CPP_COLON;
15794 cp_parser_error (parser, "expected %<<%>");
15795 pop_deferring_access_checks ();
15796 return error_mark_node;
15798 /* Otherwise, emit an error about the invalid digraph, but continue
15799 parsing because we got our argument list. */
15800 if (permerror (next_token->location,
15801 "%<<::%> cannot begin a template-argument list"))
15803 static bool hint = false;
15804 inform (next_token->location,
15805 "%<<:%> is an alternate spelling for %<[%>."
15806 " Insert whitespace between %<<%> and %<::%>");
15807 if (!hint && !flag_permissive)
15809 inform (next_token->location, "(if you use %<-fpermissive%> "
15810 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15811 "accept your code)");
15818 /* Look for the `<' that starts the template-argument-list. */
15819 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15821 pop_deferring_access_checks ();
15822 return error_mark_node;
15824 /* Parse the arguments. */
15825 arguments = cp_parser_enclosed_template_argument_list (parser);
15828 /* Set the location to be of the form:
15829 template-name < template-argument-list [opt] >
15830 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15831 with caret == start at the start of the template-name,
15832 ranging until the closing '>'. */
15833 location_t finish_loc
15834 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15835 location_t combined_loc
15836 = make_location (token->location, token->location, finish_loc);
15838 /* Check for concepts autos where they don't belong. We could
15839 identify types in some cases of idnetifier TEMPL, looking ahead
15840 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15841 types. We reject them in functions, but if what we have is an
15842 identifier, even with none_type we can't conclude it's NOT a
15843 type, we have to wait for template substitution. */
15844 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15845 template_id = error_mark_node;
15846 /* Build a representation of the specialization. */
15847 else if (identifier_p (templ))
15848 template_id = build_min_nt_loc (combined_loc,
15851 else if (DECL_TYPE_TEMPLATE_P (templ)
15852 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15854 bool entering_scope;
15855 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15856 template (rather than some instantiation thereof) only if
15857 is not nested within some other construct. For example, in
15858 "template <typename T> void f(T) { A<T>::", A<T> is just an
15859 instantiation of A. */
15860 entering_scope = (template_parm_scope_p ()
15861 && cp_lexer_next_token_is (parser->lexer,
15864 = finish_template_type (templ, arguments, entering_scope);
15866 /* A template-like identifier may be a partial concept id. */
15867 else if (flag_concepts
15868 && (template_id = (cp_parser_maybe_partial_concept_id
15869 (parser, templ, arguments))))
15870 return template_id;
15871 else if (variable_template_p (templ))
15873 template_id = lookup_template_variable (templ, arguments);
15874 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15875 SET_EXPR_LOCATION (template_id, combined_loc);
15879 /* If it's not a class-template or a template-template, it should be
15880 a function-template. */
15881 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15882 || TREE_CODE (templ) == OVERLOAD
15883 || BASELINK_P (templ)));
15885 template_id = lookup_template_function (templ, arguments);
15886 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15887 SET_EXPR_LOCATION (template_id, combined_loc);
15890 /* If parsing tentatively, replace the sequence of tokens that makes
15891 up the template-id with a CPP_TEMPLATE_ID token. That way,
15892 should we re-parse the token stream, we will not have to repeat
15893 the effort required to do the parse, nor will we issue duplicate
15894 error messages about problems during instantiation of the
15897 /* Don't do this if we had a parse error in a declarator; re-parsing
15898 might succeed if a name changes meaning (60361). */
15899 && !(cp_parser_error_occurred (parser)
15900 && cp_parser_parsing_tentatively (parser)
15901 && parser->in_declarator_p))
15903 /* Reset the contents of the START_OF_ID token. */
15904 token->type = CPP_TEMPLATE_ID;
15905 token->location = combined_loc;
15907 /* We must mark the lookup as kept, so we don't throw it away on
15908 the first parse. */
15909 if (is_overloaded_fn (template_id))
15910 lookup_keep (get_fns (template_id), true);
15912 /* Retrieve any deferred checks. Do not pop this access checks yet
15913 so the memory will not be reclaimed during token replacing below. */
15914 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15915 token->u.tree_check_value->value = template_id;
15916 token->u.tree_check_value->checks = get_deferred_access_checks ();
15917 token->keyword = RID_MAX;
15919 /* Purge all subsequent tokens. */
15920 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15922 /* ??? Can we actually assume that, if template_id ==
15923 error_mark_node, we will have issued a diagnostic to the
15924 user, as opposed to simply marking the tentative parse as
15926 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15927 error_at (token->location, "parse error in template argument list");
15930 pop_to_parent_deferring_access_checks ();
15931 return template_id;
15934 /* Parse a template-name.
15939 The standard should actually say:
15943 operator-function-id
15945 A defect report has been filed about this issue.
15947 A conversion-function-id cannot be a template name because they cannot
15948 be part of a template-id. In fact, looking at this code:
15950 a.operator K<int>()
15952 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15953 It is impossible to call a templated conversion-function-id with an
15954 explicit argument list, since the only allowed template parameter is
15955 the type to which it is converting.
15957 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15958 `template' keyword, in a construction like:
15962 In that case `f' is taken to be a template-name, even though there
15963 is no way of knowing for sure.
15965 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15966 name refers to a set of overloaded functions, at least one of which
15967 is a template, or an IDENTIFIER_NODE with the name of the template,
15968 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15969 names are looked up inside uninstantiated templates. */
15972 cp_parser_template_name (cp_parser* parser,
15973 bool template_keyword_p,
15974 bool check_dependency_p,
15975 bool is_declaration,
15976 enum tag_types tag_type,
15977 bool *is_identifier)
15981 cp_token *token = cp_lexer_peek_token (parser->lexer);
15983 /* If the next token is `operator', then we have either an
15984 operator-function-id or a conversion-function-id. */
15985 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15987 /* We don't know whether we're looking at an
15988 operator-function-id or a conversion-function-id. */
15989 cp_parser_parse_tentatively (parser);
15990 /* Try an operator-function-id. */
15991 identifier = cp_parser_operator_function_id (parser);
15992 /* If that didn't work, try a conversion-function-id. */
15993 if (!cp_parser_parse_definitely (parser))
15995 cp_parser_error (parser, "expected template-name");
15996 return error_mark_node;
15999 /* Look for the identifier. */
16001 identifier = cp_parser_identifier (parser);
16003 /* If we didn't find an identifier, we don't have a template-id. */
16004 if (identifier == error_mark_node)
16005 return error_mark_node;
16007 /* If the name immediately followed the `template' keyword, then it
16008 is a template-name. However, if the next token is not `<', then
16009 we do not treat it as a template-name, since it is not being used
16010 as part of a template-id. This enables us to handle constructs
16013 template <typename T> struct S { S(); };
16014 template <typename T> S<T>::S();
16016 correctly. We would treat `S' as a template -- if it were `S<T>'
16017 -- but we do not if there is no `<'. */
16019 if (processing_template_decl
16020 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16022 /* In a declaration, in a dependent context, we pretend that the
16023 "template" keyword was present in order to improve error
16024 recovery. For example, given:
16026 template <typename T> void f(T::X<int>);
16028 we want to treat "X<int>" as a template-id. */
16030 && !template_keyword_p
16031 && parser->scope && TYPE_P (parser->scope)
16032 && check_dependency_p
16033 && dependent_scope_p (parser->scope)
16034 /* Do not do this for dtors (or ctors), since they never
16035 need the template keyword before their name. */
16036 && !constructor_name_p (identifier, parser->scope))
16038 cp_token_position start = 0;
16040 /* Explain what went wrong. */
16041 error_at (token->location, "non-template %qD used as template",
16043 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16044 parser->scope, identifier);
16045 /* If parsing tentatively, find the location of the "<" token. */
16046 if (cp_parser_simulate_error (parser))
16047 start = cp_lexer_token_position (parser->lexer, true);
16048 /* Parse the template arguments so that we can issue error
16049 messages about them. */
16050 cp_lexer_consume_token (parser->lexer);
16051 cp_parser_enclosed_template_argument_list (parser);
16052 /* Skip tokens until we find a good place from which to
16053 continue parsing. */
16054 cp_parser_skip_to_closing_parenthesis (parser,
16055 /*recovering=*/true,
16057 /*consume_paren=*/false);
16058 /* If parsing tentatively, permanently remove the
16059 template argument list. That will prevent duplicate
16060 error messages from being issued about the missing
16061 "template" keyword. */
16063 cp_lexer_purge_tokens_after (parser->lexer, start);
16065 *is_identifier = true;
16066 parser->context->object_type = NULL_TREE;
16070 /* If the "template" keyword is present, then there is generally
16071 no point in doing name-lookup, so we just return IDENTIFIER.
16072 But, if the qualifying scope is non-dependent then we can
16073 (and must) do name-lookup normally. */
16074 if (template_keyword_p)
16076 tree scope = (parser->scope ? parser->scope
16077 : parser->context->object_type);
16078 if (scope && TYPE_P (scope)
16079 && (!CLASS_TYPE_P (scope)
16080 || (check_dependency_p && dependent_type_p (scope))))
16082 /* We're optimizing away the call to cp_parser_lookup_name, but
16083 we still need to do this. */
16084 parser->context->object_type = NULL_TREE;
16090 /* Look up the name. */
16091 decl = cp_parser_lookup_name (parser, identifier,
16093 /*is_template=*/true,
16094 /*is_namespace=*/false,
16095 check_dependency_p,
16096 /*ambiguous_decls=*/NULL,
16099 decl = strip_using_decl (decl);
16101 /* If DECL is a template, then the name was a template-name. */
16102 if (TREE_CODE (decl) == TEMPLATE_DECL)
16104 if (TREE_DEPRECATED (decl)
16105 && deprecated_state != DEPRECATED_SUPPRESS)
16106 warn_deprecated_use (decl, NULL_TREE);
16110 /* The standard does not explicitly indicate whether a name that
16111 names a set of overloaded declarations, some of which are
16112 templates, is a template-name. However, such a name should
16113 be a template-name; otherwise, there is no way to form a
16114 template-id for the overloaded templates. */
16115 bool found = false;
16117 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16118 !found && iter; ++iter)
16119 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16124 /* The name does not name a template. */
16125 cp_parser_error (parser, "expected template-name");
16126 return error_mark_node;
16130 /* If DECL is dependent, and refers to a function, then just return
16131 its name; we will look it up again during template instantiation. */
16132 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16134 tree scope = ovl_scope (decl);
16135 if (TYPE_P (scope) && dependent_type_p (scope))
16142 /* Parse a template-argument-list.
16144 template-argument-list:
16145 template-argument ... [opt]
16146 template-argument-list , template-argument ... [opt]
16148 Returns a TREE_VEC containing the arguments. */
16151 cp_parser_template_argument_list (cp_parser* parser)
16153 tree fixed_args[10];
16154 unsigned n_args = 0;
16155 unsigned alloced = 10;
16156 tree *arg_ary = fixed_args;
16158 bool saved_in_template_argument_list_p;
16160 bool saved_non_ice_p;
16162 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16163 parser->in_template_argument_list_p = true;
16164 /* Even if the template-id appears in an integral
16165 constant-expression, the contents of the argument list do
16167 saved_ice_p = parser->integral_constant_expression_p;
16168 parser->integral_constant_expression_p = false;
16169 saved_non_ice_p = parser->non_integral_constant_expression_p;
16170 parser->non_integral_constant_expression_p = false;
16172 /* Parse the arguments. */
16178 /* Consume the comma. */
16179 cp_lexer_consume_token (parser->lexer);
16181 /* Parse the template-argument. */
16182 argument = cp_parser_template_argument (parser);
16184 /* If the next token is an ellipsis, we're expanding a template
16186 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16188 if (argument == error_mark_node)
16190 cp_token *token = cp_lexer_peek_token (parser->lexer);
16191 error_at (token->location,
16192 "expected parameter pack before %<...%>");
16194 /* Consume the `...' token. */
16195 cp_lexer_consume_token (parser->lexer);
16197 /* Make the argument into a TYPE_PACK_EXPANSION or
16198 EXPR_PACK_EXPANSION. */
16199 argument = make_pack_expansion (argument);
16202 if (n_args == alloced)
16206 if (arg_ary == fixed_args)
16208 arg_ary = XNEWVEC (tree, alloced);
16209 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16212 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16214 arg_ary[n_args++] = argument;
16216 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16218 vec = make_tree_vec (n_args);
16221 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16223 if (arg_ary != fixed_args)
16225 parser->non_integral_constant_expression_p = saved_non_ice_p;
16226 parser->integral_constant_expression_p = saved_ice_p;
16227 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16229 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16233 /* Parse a template-argument.
16236 assignment-expression
16240 The representation is that of an assignment-expression, type-id, or
16241 id-expression -- except that the qualified id-expression is
16242 evaluated, so that the value returned is either a DECL or an
16245 Although the standard says "assignment-expression", it forbids
16246 throw-expressions or assignments in the template argument.
16247 Therefore, we use "conditional-expression" instead. */
16250 cp_parser_template_argument (cp_parser* parser)
16255 bool maybe_type_id = false;
16256 cp_token *token = NULL, *argument_start_token = NULL;
16257 location_t loc = 0;
16260 /* There's really no way to know what we're looking at, so we just
16261 try each alternative in order.
16265 In a template-argument, an ambiguity between a type-id and an
16266 expression is resolved to a type-id, regardless of the form of
16267 the corresponding template-parameter.
16269 Therefore, we try a type-id first. */
16270 cp_parser_parse_tentatively (parser);
16271 argument = cp_parser_template_type_arg (parser);
16272 /* If there was no error parsing the type-id but the next token is a
16273 '>>', our behavior depends on which dialect of C++ we're
16274 parsing. In C++98, we probably found a typo for '> >'. But there
16275 are type-id which are also valid expressions. For instance:
16277 struct X { int operator >> (int); };
16278 template <int V> struct Foo {};
16281 Here 'X()' is a valid type-id of a function type, but the user just
16282 wanted to write the expression "X() >> 5". Thus, we remember that we
16283 found a valid type-id, but we still try to parse the argument as an
16284 expression to see what happens.
16286 In C++0x, the '>>' will be considered two separate '>'
16288 if (!cp_parser_error_occurred (parser)
16289 && cxx_dialect == cxx98
16290 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16292 maybe_type_id = true;
16293 cp_parser_abort_tentative_parse (parser);
16297 /* If the next token isn't a `,' or a `>', then this argument wasn't
16298 really finished. This means that the argument is not a valid
16300 if (!cp_parser_next_token_ends_template_argument_p (parser))
16301 cp_parser_error (parser, "expected template-argument");
16302 /* If that worked, we're done. */
16303 if (cp_parser_parse_definitely (parser))
16306 /* We're still not sure what the argument will be. */
16307 cp_parser_parse_tentatively (parser);
16308 /* Try a template. */
16309 argument_start_token = cp_lexer_peek_token (parser->lexer);
16310 argument = cp_parser_id_expression (parser,
16311 /*template_keyword_p=*/false,
16312 /*check_dependency_p=*/true,
16314 /*declarator_p=*/false,
16315 /*optional_p=*/false);
16316 /* If the next token isn't a `,' or a `>', then this argument wasn't
16317 really finished. */
16318 if (!cp_parser_next_token_ends_template_argument_p (parser))
16319 cp_parser_error (parser, "expected template-argument");
16320 if (!cp_parser_error_occurred (parser))
16322 /* Figure out what is being referred to. If the id-expression
16323 was for a class template specialization, then we will have a
16324 TYPE_DECL at this point. There is no need to do name lookup
16325 at this point in that case. */
16326 if (TREE_CODE (argument) != TYPE_DECL)
16327 argument = cp_parser_lookup_name (parser, argument,
16329 /*is_template=*/template_p,
16330 /*is_namespace=*/false,
16331 /*check_dependency=*/true,
16332 /*ambiguous_decls=*/NULL,
16333 argument_start_token->location);
16334 /* Handle a constrained-type-specifier for a non-type template
16336 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16338 else if (TREE_CODE (argument) != TEMPLATE_DECL
16339 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16340 cp_parser_error (parser, "expected template-name");
16342 if (cp_parser_parse_definitely (parser))
16344 if (TREE_DEPRECATED (argument))
16345 warn_deprecated_use (argument, NULL_TREE);
16348 /* It must be a non-type argument. In C++17 any constant-expression is
16350 if (cxx_dialect > cxx14)
16353 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16355 -- an integral constant-expression of integral or enumeration
16358 -- the name of a non-type template-parameter; or
16360 -- the name of an object or function with external linkage...
16362 -- the address of an object or function with external linkage...
16364 -- a pointer to member... */
16365 /* Look for a non-type template parameter. */
16366 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16368 cp_parser_parse_tentatively (parser);
16369 argument = cp_parser_primary_expression (parser,
16370 /*address_p=*/false,
16372 /*template_arg_p=*/true,
16374 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16375 || !cp_parser_next_token_ends_template_argument_p (parser))
16376 cp_parser_simulate_error (parser);
16377 if (cp_parser_parse_definitely (parser))
16381 /* If the next token is "&", the argument must be the address of an
16382 object or function with external linkage. */
16383 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16386 loc = cp_lexer_peek_token (parser->lexer)->location;
16387 cp_lexer_consume_token (parser->lexer);
16389 /* See if we might have an id-expression. */
16390 token = cp_lexer_peek_token (parser->lexer);
16391 if (token->type == CPP_NAME
16392 || token->keyword == RID_OPERATOR
16393 || token->type == CPP_SCOPE
16394 || token->type == CPP_TEMPLATE_ID
16395 || token->type == CPP_NESTED_NAME_SPECIFIER)
16397 cp_parser_parse_tentatively (parser);
16398 argument = cp_parser_primary_expression (parser,
16401 /*template_arg_p=*/true,
16403 if (cp_parser_error_occurred (parser)
16404 || !cp_parser_next_token_ends_template_argument_p (parser))
16405 cp_parser_abort_tentative_parse (parser);
16410 if (INDIRECT_REF_P (argument))
16412 /* Strip the dereference temporarily. */
16413 gcc_assert (REFERENCE_REF_P (argument));
16414 argument = TREE_OPERAND (argument, 0);
16417 /* If we're in a template, we represent a qualified-id referring
16418 to a static data member as a SCOPE_REF even if the scope isn't
16419 dependent so that we can check access control later. */
16421 if (TREE_CODE (probe) == SCOPE_REF)
16422 probe = TREE_OPERAND (probe, 1);
16425 /* A variable without external linkage might still be a
16426 valid constant-expression, so no error is issued here
16427 if the external-linkage check fails. */
16428 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16429 cp_parser_simulate_error (parser);
16431 else if (is_overloaded_fn (argument))
16432 /* All overloaded functions are allowed; if the external
16433 linkage test does not pass, an error will be issued
16437 && (TREE_CODE (argument) == OFFSET_REF
16438 || TREE_CODE (argument) == SCOPE_REF))
16439 /* A pointer-to-member. */
16441 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16444 cp_parser_simulate_error (parser);
16446 if (cp_parser_parse_definitely (parser))
16449 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16450 tf_warning_or_error);
16452 argument = convert_from_reference (argument);
16457 /* If the argument started with "&", there are no other valid
16458 alternatives at this point. */
16461 cp_parser_error (parser, "invalid non-type template argument");
16462 return error_mark_node;
16466 /* If the argument wasn't successfully parsed as a type-id followed
16467 by '>>', the argument can only be a constant expression now.
16468 Otherwise, we try parsing the constant-expression tentatively,
16469 because the argument could really be a type-id. */
16471 cp_parser_parse_tentatively (parser);
16473 if (cxx_dialect <= cxx14)
16474 argument = cp_parser_constant_expression (parser);
16477 /* With C++17 generalized non-type template arguments we need to handle
16478 lvalue constant expressions, too. */
16479 argument = cp_parser_assignment_expression (parser);
16480 require_potential_constant_expression (argument);
16483 if (!maybe_type_id)
16485 if (!cp_parser_next_token_ends_template_argument_p (parser))
16486 cp_parser_error (parser, "expected template-argument");
16487 if (cp_parser_parse_definitely (parser))
16489 /* We did our best to parse the argument as a non type-id, but that
16490 was the only alternative that matched (albeit with a '>' after
16491 it). We can assume it's just a typo from the user, and a
16492 diagnostic will then be issued. */
16493 return cp_parser_template_type_arg (parser);
16496 /* Parse an explicit-instantiation.
16498 explicit-instantiation:
16499 template declaration
16501 Although the standard says `declaration', what it really means is:
16503 explicit-instantiation:
16504 template decl-specifier-seq [opt] declarator [opt] ;
16506 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16507 supposed to be allowed. A defect report has been filed about this
16512 explicit-instantiation:
16513 storage-class-specifier template
16514 decl-specifier-seq [opt] declarator [opt] ;
16515 function-specifier template
16516 decl-specifier-seq [opt] declarator [opt] ; */
16519 cp_parser_explicit_instantiation (cp_parser* parser)
16521 int declares_class_or_enum;
16522 cp_decl_specifier_seq decl_specifiers;
16523 tree extension_specifier = NULL_TREE;
16525 timevar_push (TV_TEMPLATE_INST);
16527 /* Look for an (optional) storage-class-specifier or
16528 function-specifier. */
16529 if (cp_parser_allow_gnu_extensions_p (parser))
16531 extension_specifier
16532 = cp_parser_storage_class_specifier_opt (parser);
16533 if (!extension_specifier)
16534 extension_specifier
16535 = cp_parser_function_specifier_opt (parser,
16536 /*decl_specs=*/NULL);
16539 /* Look for the `template' keyword. */
16540 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16541 /* Let the front end know that we are processing an explicit
16543 begin_explicit_instantiation ();
16544 /* [temp.explicit] says that we are supposed to ignore access
16545 control while processing explicit instantiation directives. */
16546 push_deferring_access_checks (dk_no_check);
16547 /* Parse a decl-specifier-seq. */
16548 cp_parser_decl_specifier_seq (parser,
16549 CP_PARSER_FLAGS_OPTIONAL,
16551 &declares_class_or_enum);
16552 /* If there was exactly one decl-specifier, and it declared a class,
16553 and there's no declarator, then we have an explicit type
16555 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16559 type = check_tag_decl (&decl_specifiers,
16560 /*explicit_type_instantiation_p=*/true);
16561 /* Turn access control back on for names used during
16562 template instantiation. */
16563 pop_deferring_access_checks ();
16565 do_type_instantiation (type, extension_specifier,
16566 /*complain=*/tf_error);
16570 cp_declarator *declarator;
16573 /* Parse the declarator. */
16575 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16576 /*ctor_dtor_or_conv_p=*/NULL,
16577 /*parenthesized_p=*/NULL,
16578 /*member_p=*/false,
16579 /*friend_p=*/false);
16580 if (declares_class_or_enum & 2)
16581 cp_parser_check_for_definition_in_return_type (declarator,
16582 decl_specifiers.type,
16583 decl_specifiers.locations[ds_type_spec]);
16584 if (declarator != cp_error_declarator)
16586 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16587 permerror (decl_specifiers.locations[ds_inline],
16588 "explicit instantiation shall not use"
16589 " %<inline%> specifier");
16590 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16591 permerror (decl_specifiers.locations[ds_constexpr],
16592 "explicit instantiation shall not use"
16593 " %<constexpr%> specifier");
16595 decl = grokdeclarator (declarator, &decl_specifiers,
16596 NORMAL, 0, &decl_specifiers.attributes);
16597 /* Turn access control back on for names used during
16598 template instantiation. */
16599 pop_deferring_access_checks ();
16600 /* Do the explicit instantiation. */
16601 do_decl_instantiation (decl, extension_specifier);
16605 pop_deferring_access_checks ();
16606 /* Skip the body of the explicit instantiation. */
16607 cp_parser_skip_to_end_of_statement (parser);
16610 /* We're done with the instantiation. */
16611 end_explicit_instantiation ();
16613 cp_parser_consume_semicolon_at_end_of_statement (parser);
16615 timevar_pop (TV_TEMPLATE_INST);
16618 /* Parse an explicit-specialization.
16620 explicit-specialization:
16621 template < > declaration
16623 Although the standard says `declaration', what it really means is:
16625 explicit-specialization:
16626 template <> decl-specifier [opt] init-declarator [opt] ;
16627 template <> function-definition
16628 template <> explicit-specialization
16629 template <> template-declaration */
16632 cp_parser_explicit_specialization (cp_parser* parser)
16634 bool need_lang_pop;
16635 cp_token *token = cp_lexer_peek_token (parser->lexer);
16637 /* Look for the `template' keyword. */
16638 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16639 /* Look for the `<'. */
16640 cp_parser_require (parser, CPP_LESS, RT_LESS);
16641 /* Look for the `>'. */
16642 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16643 /* We have processed another parameter list. */
16644 ++parser->num_template_parameter_lists;
16647 A template ... explicit specialization ... shall not have C
16649 if (current_lang_name == lang_name_c)
16651 error_at (token->location, "template specialization with C linkage");
16652 maybe_show_extern_c_location ();
16653 /* Give it C++ linkage to avoid confusing other parts of the
16655 push_lang_context (lang_name_cplusplus);
16656 need_lang_pop = true;
16659 need_lang_pop = false;
16660 /* Let the front end know that we are beginning a specialization. */
16661 if (!begin_specialization ())
16663 end_specialization ();
16667 /* If the next keyword is `template', we need to figure out whether
16668 or not we're looking a template-declaration. */
16669 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16671 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16672 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16673 cp_parser_template_declaration_after_export (parser,
16674 /*member_p=*/false);
16676 cp_parser_explicit_specialization (parser);
16679 /* Parse the dependent declaration. */
16680 cp_parser_single_declaration (parser,
16682 /*member_p=*/false,
16683 /*explicit_specialization_p=*/true,
16684 /*friend_p=*/NULL);
16685 /* We're done with the specialization. */
16686 end_specialization ();
16687 /* For the erroneous case of a template with C linkage, we pushed an
16688 implicit C++ linkage scope; exit that scope now. */
16690 pop_lang_context ();
16691 /* We're done with this parameter list. */
16692 --parser->num_template_parameter_lists;
16695 /* Parse a type-specifier.
16698 simple-type-specifier
16701 elaborated-type-specifier
16709 Returns a representation of the type-specifier. For a
16710 class-specifier, enum-specifier, or elaborated-type-specifier, a
16711 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16713 The parser flags FLAGS is used to control type-specifier parsing.
16715 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16716 in a decl-specifier-seq.
16718 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16719 class-specifier, enum-specifier, or elaborated-type-specifier, then
16720 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16721 if a type is declared; 2 if it is defined. Otherwise, it is set to
16724 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16725 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16726 is set to FALSE. */
16729 cp_parser_type_specifier (cp_parser* parser,
16730 cp_parser_flags flags,
16731 cp_decl_specifier_seq *decl_specs,
16732 bool is_declaration,
16733 int* declares_class_or_enum,
16734 bool* is_cv_qualifier)
16736 tree type_spec = NULL_TREE;
16739 cp_decl_spec ds = ds_last;
16741 /* Assume this type-specifier does not declare a new type. */
16742 if (declares_class_or_enum)
16743 *declares_class_or_enum = 0;
16744 /* And that it does not specify a cv-qualifier. */
16745 if (is_cv_qualifier)
16746 *is_cv_qualifier = false;
16747 /* Peek at the next token. */
16748 token = cp_lexer_peek_token (parser->lexer);
16750 /* If we're looking at a keyword, we can use that to guide the
16751 production we choose. */
16752 keyword = token->keyword;
16756 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16757 goto elaborated_type_specifier;
16759 /* Look for the enum-specifier. */
16760 type_spec = cp_parser_enum_specifier (parser);
16761 /* If that worked, we're done. */
16764 if (declares_class_or_enum)
16765 *declares_class_or_enum = 2;
16767 cp_parser_set_decl_spec_type (decl_specs,
16770 /*type_definition_p=*/true);
16774 goto elaborated_type_specifier;
16776 /* Any of these indicate either a class-specifier, or an
16777 elaborated-type-specifier. */
16781 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16782 goto elaborated_type_specifier;
16784 /* Parse tentatively so that we can back up if we don't find a
16785 class-specifier. */
16786 cp_parser_parse_tentatively (parser);
16787 /* Look for the class-specifier. */
16788 type_spec = cp_parser_class_specifier (parser);
16789 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16790 /* If that worked, we're done. */
16791 if (cp_parser_parse_definitely (parser))
16793 if (declares_class_or_enum)
16794 *declares_class_or_enum = 2;
16796 cp_parser_set_decl_spec_type (decl_specs,
16799 /*type_definition_p=*/true);
16803 /* Fall through. */
16804 elaborated_type_specifier:
16805 /* We're declaring (not defining) a class or enum. */
16806 if (declares_class_or_enum)
16807 *declares_class_or_enum = 1;
16809 /* Fall through. */
16811 /* Look for an elaborated-type-specifier. */
16813 = (cp_parser_elaborated_type_specifier
16815 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16818 cp_parser_set_decl_spec_type (decl_specs,
16821 /*type_definition_p=*/false);
16826 if (is_cv_qualifier)
16827 *is_cv_qualifier = true;
16832 if (is_cv_qualifier)
16833 *is_cv_qualifier = true;
16838 if (is_cv_qualifier)
16839 *is_cv_qualifier = true;
16843 /* The `__complex__' keyword is a GNU extension. */
16851 /* Handle simple keywords. */
16856 set_and_check_decl_spec_loc (decl_specs, ds, token);
16857 decl_specs->any_specifiers_p = true;
16859 return cp_lexer_consume_token (parser->lexer)->u.value;
16862 /* If we do not already have a type-specifier, assume we are looking
16863 at a simple-type-specifier. */
16864 type_spec = cp_parser_simple_type_specifier (parser,
16868 /* If we didn't find a type-specifier, and a type-specifier was not
16869 optional in this context, issue an error message. */
16870 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16872 cp_parser_error (parser, "expected type specifier");
16873 return error_mark_node;
16879 /* Parse a simple-type-specifier.
16881 simple-type-specifier:
16882 :: [opt] nested-name-specifier [opt] type-name
16883 :: [opt] nested-name-specifier template template-id
16898 simple-type-specifier:
16900 decltype ( expression )
16903 __underlying_type ( type-id )
16907 nested-name-specifier(opt) template-name
16911 simple-type-specifier:
16913 __typeof__ unary-expression
16914 __typeof__ ( type-id )
16915 __typeof__ ( type-id ) { initializer-list , [opt] }
16917 Concepts Extension:
16919 simple-type-specifier:
16920 constrained-type-specifier
16922 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16923 appropriately updated. */
16926 cp_parser_simple_type_specifier (cp_parser* parser,
16927 cp_decl_specifier_seq *decl_specs,
16928 cp_parser_flags flags)
16930 tree type = NULL_TREE;
16934 /* Peek at the next token. */
16935 token = cp_lexer_peek_token (parser->lexer);
16937 /* If we're looking at a keyword, things are easy. */
16938 switch (token->keyword)
16942 decl_specs->explicit_char_p = true;
16943 type = char_type_node;
16946 type = char16_type_node;
16949 type = char32_type_node;
16952 type = wchar_type_node;
16955 type = boolean_type_node;
16958 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16959 type = short_integer_type_node;
16963 decl_specs->explicit_int_p = true;
16964 type = integer_type_node;
16970 idx = token->keyword - RID_INT_N_0;
16971 if (! int_n_enabled_p [idx])
16975 decl_specs->explicit_intN_p = true;
16976 decl_specs->int_n_idx = idx;
16978 type = int_n_trees [idx].signed_type;
16982 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16983 type = long_integer_type_node;
16986 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16987 type = integer_type_node;
16990 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16991 type = unsigned_type_node;
16994 type = float_type_node;
16997 type = double_type_node;
17000 type = void_type_node;
17004 maybe_warn_cpp0x (CPP0X_AUTO);
17005 if (parser->auto_is_implicit_function_template_parm_p)
17007 /* The 'auto' might be the placeholder return type for a function decl
17008 with trailing return type. */
17009 bool have_trailing_return_fn_decl = false;
17011 cp_parser_parse_tentatively (parser);
17012 cp_lexer_consume_token (parser->lexer);
17013 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17014 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17015 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17016 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17018 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17020 cp_lexer_consume_token (parser->lexer);
17021 cp_parser_skip_to_closing_parenthesis (parser,
17022 /*recovering*/false,
17024 /*consume_paren*/true);
17028 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17030 have_trailing_return_fn_decl = true;
17034 cp_lexer_consume_token (parser->lexer);
17036 cp_parser_abort_tentative_parse (parser);
17038 if (have_trailing_return_fn_decl)
17040 type = make_auto ();
17044 if (cxx_dialect >= cxx14)
17046 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17047 type = TREE_TYPE (type);
17050 type = error_mark_node;
17052 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17054 if (cxx_dialect < cxx14)
17055 error_at (token->location,
17056 "use of %<auto%> in lambda parameter declaration "
17057 "only available with "
17058 "-std=c++14 or -std=gnu++14");
17060 else if (cxx_dialect < cxx14)
17061 error_at (token->location,
17062 "use of %<auto%> in parameter declaration "
17063 "only available with "
17064 "-std=c++14 or -std=gnu++14");
17065 else if (!flag_concepts)
17066 pedwarn (token->location, 0,
17067 "use of %<auto%> in parameter declaration "
17068 "only available with -fconcepts");
17071 type = make_auto ();
17075 /* Since DR 743, decltype can either be a simple-type-specifier by
17076 itself or begin a nested-name-specifier. Parsing it will replace
17077 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17078 handling below decide what to do. */
17079 cp_parser_decltype (parser);
17080 cp_lexer_set_token_position (parser->lexer, token);
17084 /* Consume the `typeof' token. */
17085 cp_lexer_consume_token (parser->lexer);
17086 /* Parse the operand to `typeof'. */
17087 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17088 /* If it is not already a TYPE, take its type. */
17089 if (!TYPE_P (type))
17090 type = finish_typeof (type);
17093 cp_parser_set_decl_spec_type (decl_specs, type,
17095 /*type_definition_p=*/false);
17099 case RID_UNDERLYING_TYPE:
17100 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17102 cp_parser_set_decl_spec_type (decl_specs, type,
17104 /*type_definition_p=*/false);
17109 case RID_DIRECT_BASES:
17110 type = cp_parser_trait_expr (parser, token->keyword);
17112 cp_parser_set_decl_spec_type (decl_specs, type,
17114 /*type_definition_p=*/false);
17120 /* If token is an already-parsed decltype not followed by ::,
17121 it's a simple-type-specifier. */
17122 if (token->type == CPP_DECLTYPE
17123 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17125 type = saved_checks_value (token->u.tree_check_value);
17128 cp_parser_set_decl_spec_type (decl_specs, type,
17130 /*type_definition_p=*/false);
17131 /* Remember that we are handling a decltype in order to
17132 implement the resolution of DR 1510 when the argument
17133 isn't instantiation dependent. */
17134 decl_specs->decltype_p = true;
17136 cp_lexer_consume_token (parser->lexer);
17140 /* If the type-specifier was for a built-in type, we're done. */
17143 /* Record the type. */
17145 && (token->keyword != RID_SIGNED
17146 && token->keyword != RID_UNSIGNED
17147 && token->keyword != RID_SHORT
17148 && token->keyword != RID_LONG))
17149 cp_parser_set_decl_spec_type (decl_specs,
17152 /*type_definition_p=*/false);
17154 decl_specs->any_specifiers_p = true;
17156 /* Consume the token. */
17157 cp_lexer_consume_token (parser->lexer);
17159 if (type == error_mark_node)
17160 return error_mark_node;
17162 /* There is no valid C++ program where a non-template type is
17163 followed by a "<". That usually indicates that the user thought
17164 that the type was a template. */
17165 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17168 return TYPE_NAME (type);
17171 /* The type-specifier must be a user-defined type. */
17172 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17177 /* Don't gobble tokens or issue error messages if this is an
17178 optional type-specifier. */
17179 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17180 cp_parser_parse_tentatively (parser);
17182 token = cp_lexer_peek_token (parser->lexer);
17184 /* Look for the optional `::' operator. */
17186 = (cp_parser_global_scope_opt (parser,
17187 /*current_scope_valid_p=*/false)
17189 /* Look for the nested-name specifier. */
17191 = (cp_parser_nested_name_specifier_opt (parser,
17192 /*typename_keyword_p=*/false,
17193 /*check_dependency_p=*/true,
17195 /*is_declaration=*/false)
17197 /* If we have seen a nested-name-specifier, and the next token
17198 is `template', then we are using the template-id production. */
17200 && cp_parser_optional_template_keyword (parser))
17202 /* Look for the template-id. */
17203 type = cp_parser_template_id (parser,
17204 /*template_keyword_p=*/true,
17205 /*check_dependency_p=*/true,
17207 /*is_declaration=*/false);
17208 /* If the template-id did not name a type, we are out of
17210 if (TREE_CODE (type) != TYPE_DECL)
17212 cp_parser_error (parser, "expected template-id for type");
17216 /* Otherwise, look for a type-name. */
17218 type = cp_parser_type_name (parser);
17219 /* Keep track of all name-lookups performed in class scopes. */
17223 && TREE_CODE (type) == TYPE_DECL
17224 && identifier_p (DECL_NAME (type)))
17225 maybe_note_name_used_in_class (DECL_NAME (type), type);
17226 /* If it didn't work out, we don't have a TYPE. */
17227 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17228 && !cp_parser_parse_definitely (parser))
17230 if (!type && cxx_dialect >= cxx17)
17232 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17233 cp_parser_parse_tentatively (parser);
17235 cp_parser_global_scope_opt (parser,
17236 /*current_scope_valid_p=*/false);
17237 cp_parser_nested_name_specifier_opt (parser,
17238 /*typename_keyword_p=*/false,
17239 /*check_dependency_p=*/true,
17241 /*is_declaration=*/false);
17242 tree name = cp_parser_identifier (parser);
17243 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17244 && parser->scope != error_mark_node)
17246 tree tmpl = cp_parser_lookup_name (parser, name,
17248 /*is_template=*/false,
17249 /*is_namespace=*/false,
17250 /*check_dependency=*/true,
17251 /*ambiguous_decls=*/NULL,
17253 if (tmpl && tmpl != error_mark_node
17254 && (DECL_CLASS_TEMPLATE_P (tmpl)
17255 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17256 type = make_template_placeholder (tmpl);
17259 type = error_mark_node;
17260 if (!cp_parser_simulate_error (parser))
17261 cp_parser_name_lookup_error (parser, name, tmpl,
17262 NLE_TYPE, token->location);
17266 type = error_mark_node;
17268 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17269 && !cp_parser_parse_definitely (parser))
17272 if (type && decl_specs)
17273 cp_parser_set_decl_spec_type (decl_specs, type,
17275 /*type_definition_p=*/false);
17278 /* If we didn't get a type-name, issue an error message. */
17279 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17281 cp_parser_error (parser, "expected type-name");
17282 return error_mark_node;
17285 if (type && type != error_mark_node)
17287 /* See if TYPE is an Objective-C type, and if so, parse and
17288 accept any protocol references following it. Do this before
17289 the cp_parser_check_for_invalid_template_id() call, because
17290 Objective-C types can be followed by '<...>' which would
17291 enclose protocol names rather than template arguments, and so
17292 everything is fine. */
17293 if (c_dialect_objc () && !parser->scope
17294 && (objc_is_id (type) || objc_is_class_name (type)))
17296 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17297 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17299 /* Clobber the "unqualified" type previously entered into
17300 DECL_SPECS with the new, improved protocol-qualified version. */
17302 decl_specs->type = qual_type;
17307 /* There is no valid C++ program where a non-template type is
17308 followed by a "<". That usually indicates that the user
17309 thought that the type was a template. */
17310 cp_parser_check_for_invalid_template_id (parser, type,
17318 /* Parse a type-name.
17324 simple-template-id [in c++0x]
17341 Returns a TYPE_DECL for the type. */
17344 cp_parser_type_name (cp_parser* parser)
17346 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17351 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17355 /* We can't know yet whether it is a class-name or not. */
17356 cp_parser_parse_tentatively (parser);
17357 /* Try a class-name. */
17358 type_decl = cp_parser_class_name (parser,
17359 typename_keyword_p,
17360 /*template_keyword_p=*/false,
17362 /*check_dependency_p=*/true,
17363 /*class_head_p=*/false,
17364 /*is_declaration=*/false);
17365 /* If it's not a class-name, keep looking. */
17366 if (!cp_parser_parse_definitely (parser))
17368 if (cxx_dialect < cxx11)
17369 /* It must be a typedef-name or an enum-name. */
17370 return cp_parser_nonclass_name (parser);
17372 cp_parser_parse_tentatively (parser);
17373 /* It is either a simple-template-id representing an
17374 instantiation of an alias template... */
17375 type_decl = cp_parser_template_id (parser,
17376 /*template_keyword_p=*/false,
17377 /*check_dependency_p=*/true,
17379 /*is_declaration=*/false);
17380 /* Note that this must be an instantiation of an alias template
17381 because [temp.names]/6 says:
17383 A template-id that names an alias template specialization
17386 Whereas [temp.names]/7 says:
17388 A simple-template-id that names a class template
17389 specialization is a class-name.
17391 With concepts, this could also be a partial-concept-id that
17392 declares a non-type template parameter. */
17393 if (type_decl != NULL_TREE
17394 && TREE_CODE (type_decl) == TYPE_DECL
17395 && TYPE_DECL_ALIAS_P (type_decl))
17396 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17397 else if (is_constrained_parameter (type_decl))
17398 /* Don't do anything. */ ;
17400 cp_parser_simulate_error (parser);
17402 if (!cp_parser_parse_definitely (parser))
17403 /* ... Or a typedef-name or an enum-name. */
17404 return cp_parser_nonclass_name (parser);
17410 /* Check if DECL and ARGS can form a constrained-type-specifier.
17411 If ARGS is non-null, we try to form a concept check of the
17412 form DECL<?, ARGS> where ? is a wildcard that matches any
17413 kind of template argument. If ARGS is NULL, then we try to
17414 form a concept check of the form DECL<?>. */
17417 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17418 tree decl, tree args)
17420 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17422 /* If we a constrained-type-specifier cannot be deduced. */
17423 if (parser->prevent_constrained_type_specifiers)
17426 /* A constrained type specifier can only be found in an
17427 overload set or as a reference to a template declaration.
17429 FIXME: This might be masking a bug. It's possible that
17430 that the deduction below is causing template specializations
17431 to be formed with the wildcard as an argument. */
17432 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17435 /* Try to build a call expression that evaluates the
17436 concept. This can fail if the overload set refers
17437 only to non-templates. */
17438 tree placeholder = build_nt (WILDCARD_DECL);
17439 tree check = build_concept_check (decl, placeholder, args);
17440 if (check == error_mark_node)
17443 /* Deduce the checked constraint and the prototype parameter.
17445 FIXME: In certain cases, failure to deduce should be a
17446 diagnosable error. */
17449 if (!deduce_constrained_parameter (check, conc, proto))
17452 /* In template parameter scope, this results in a constrained
17453 parameter. Return a descriptor of that parm. */
17454 if (processing_template_parmlist)
17455 return build_constrained_parameter (conc, proto, args);
17457 /* In a parameter-declaration-clause, constrained-type
17458 specifiers result in invented template parameters. */
17459 if (parser->auto_is_implicit_function_template_parm_p)
17461 tree x = build_constrained_parameter (conc, proto, args);
17462 return synthesize_implicit_template_parm (parser, x);
17466 /* Otherwise, we're in a context where the constrained
17467 type name is deduced and the constraint applies
17468 after deduction. */
17469 return make_constrained_auto (conc, args);
17475 /* If DECL refers to a concept, return a TYPE_DECL representing
17476 the result of using the constrained type specifier in the
17477 current context. DECL refers to a concept if
17479 - it is an overload set containing a function concept taking a single
17482 - it is a variable concept taking a single type argument. */
17485 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17488 && (TREE_CODE (decl) == OVERLOAD
17489 || BASELINK_P (decl)
17490 || variable_concept_p (decl)))
17491 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17496 /* Check if DECL and ARGS form a partial-concept-id. If so,
17497 assign ID to the resulting constrained placeholder.
17499 Returns true if the partial-concept-id designates a placeholder
17500 and false otherwise. Note that *id is set to NULL_TREE in
17504 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17506 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17509 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17521 Returns a TYPE_DECL for the type. */
17524 cp_parser_nonclass_name (cp_parser* parser)
17529 cp_token *token = cp_lexer_peek_token (parser->lexer);
17530 identifier = cp_parser_identifier (parser);
17531 if (identifier == error_mark_node)
17532 return error_mark_node;
17534 /* Look up the type-name. */
17535 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17537 type_decl = strip_using_decl (type_decl);
17539 /* If we found an overload set, then it may refer to a concept-name. */
17540 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17543 if (TREE_CODE (type_decl) != TYPE_DECL
17544 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17546 /* See if this is an Objective-C type. */
17547 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17548 tree type = objc_get_protocol_qualified_type (identifier, protos);
17550 type_decl = TYPE_NAME (type);
17553 /* Issue an error if we did not find a type-name. */
17554 if (TREE_CODE (type_decl) != TYPE_DECL
17555 /* In Objective-C, we have the complication that class names are
17556 normally type names and start declarations (eg, the
17557 "NSObject" in "NSObject *object;"), but can be used in an
17558 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17559 is an expression. So, a classname followed by a dot is not a
17560 valid type-name. */
17561 || (objc_is_class_name (TREE_TYPE (type_decl))
17562 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17564 if (!cp_parser_simulate_error (parser))
17565 cp_parser_name_lookup_error (parser, identifier, type_decl,
17566 NLE_TYPE, token->location);
17567 return error_mark_node;
17569 /* Remember that the name was used in the definition of the
17570 current class so that we can check later to see if the
17571 meaning would have been different after the class was
17572 entirely defined. */
17573 else if (type_decl != error_mark_node
17575 maybe_note_name_used_in_class (identifier, type_decl);
17580 /* Parse an elaborated-type-specifier. Note that the grammar given
17581 here incorporates the resolution to DR68.
17583 elaborated-type-specifier:
17584 class-key :: [opt] nested-name-specifier [opt] identifier
17585 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17586 enum-key :: [opt] nested-name-specifier [opt] identifier
17587 typename :: [opt] nested-name-specifier identifier
17588 typename :: [opt] nested-name-specifier template [opt]
17593 elaborated-type-specifier:
17594 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17595 class-key attributes :: [opt] nested-name-specifier [opt]
17596 template [opt] template-id
17597 enum attributes :: [opt] nested-name-specifier [opt] identifier
17599 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17600 declared `friend'. If IS_DECLARATION is TRUE, then this
17601 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17602 something is being declared.
17604 Returns the TYPE specified. */
17607 cp_parser_elaborated_type_specifier (cp_parser* parser,
17609 bool is_declaration)
17611 enum tag_types tag_type;
17613 tree type = NULL_TREE;
17614 tree attributes = NULL_TREE;
17616 cp_token *token = NULL;
17618 /* See if we're looking at the `enum' keyword. */
17619 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17621 /* Consume the `enum' token. */
17622 cp_lexer_consume_token (parser->lexer);
17623 /* Remember that it's an enumeration type. */
17624 tag_type = enum_type;
17625 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17626 enums) is used here. */
17627 cp_token *token = cp_lexer_peek_token (parser->lexer);
17628 if (cp_parser_is_keyword (token, RID_CLASS)
17629 || cp_parser_is_keyword (token, RID_STRUCT))
17631 gcc_rich_location richloc (token->location);
17632 richloc.add_range (input_location, false);
17633 richloc.add_fixit_remove ();
17634 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17635 "a scoped enum must not use the %qD keyword",
17637 /* Consume the `struct' or `class' and parse it anyway. */
17638 cp_lexer_consume_token (parser->lexer);
17640 /* Parse the attributes. */
17641 attributes = cp_parser_attributes_opt (parser);
17643 /* Or, it might be `typename'. */
17644 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17647 /* Consume the `typename' token. */
17648 cp_lexer_consume_token (parser->lexer);
17649 /* Remember that it's a `typename' type. */
17650 tag_type = typename_type;
17652 /* Otherwise it must be a class-key. */
17655 tag_type = cp_parser_class_key (parser);
17656 if (tag_type == none_type)
17657 return error_mark_node;
17658 /* Parse the attributes. */
17659 attributes = cp_parser_attributes_opt (parser);
17662 /* Look for the `::' operator. */
17663 globalscope = cp_parser_global_scope_opt (parser,
17664 /*current_scope_valid_p=*/false);
17665 /* Look for the nested-name-specifier. */
17666 tree nested_name_specifier;
17667 if (tag_type == typename_type && !globalscope)
17669 nested_name_specifier
17670 = cp_parser_nested_name_specifier (parser,
17671 /*typename_keyword_p=*/true,
17672 /*check_dependency_p=*/true,
17675 if (!nested_name_specifier)
17676 return error_mark_node;
17679 /* Even though `typename' is not present, the proposed resolution
17680 to Core Issue 180 says that in `class A<T>::B', `B' should be
17681 considered a type-name, even if `A<T>' is dependent. */
17682 nested_name_specifier
17683 = cp_parser_nested_name_specifier_opt (parser,
17684 /*typename_keyword_p=*/true,
17685 /*check_dependency_p=*/true,
17688 /* For everything but enumeration types, consider a template-id.
17689 For an enumeration type, consider only a plain identifier. */
17690 if (tag_type != enum_type)
17692 bool template_p = false;
17695 /* Allow the `template' keyword. */
17696 template_p = cp_parser_optional_template_keyword (parser);
17697 /* If we didn't see `template', we don't know if there's a
17698 template-id or not. */
17700 cp_parser_parse_tentatively (parser);
17701 /* Parse the template-id. */
17702 token = cp_lexer_peek_token (parser->lexer);
17703 decl = cp_parser_template_id (parser, template_p,
17704 /*check_dependency_p=*/true,
17707 /* If we didn't find a template-id, look for an ordinary
17709 if (!template_p && !cp_parser_parse_definitely (parser))
17711 /* We can get here when cp_parser_template_id, called by
17712 cp_parser_class_name with tag_type == none_type, succeeds
17713 and caches a BASELINK. Then, when called again here,
17714 instead of failing and returning an error_mark_node
17715 returns it (see template/typename17.C in C++11).
17716 ??? Could we diagnose this earlier? */
17717 else if (tag_type == typename_type && BASELINK_P (decl))
17719 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17720 type = error_mark_node;
17722 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17723 in effect, then we must assume that, upon instantiation, the
17724 template will correspond to a class. */
17725 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17726 && tag_type == typename_type)
17727 type = make_typename_type (parser->scope, decl,
17729 /*complain=*/tf_error);
17730 /* If the `typename' keyword is in effect and DECL is not a type
17731 decl, then type is non existent. */
17732 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17734 else if (TREE_CODE (decl) == TYPE_DECL)
17736 type = check_elaborated_type_specifier (tag_type, decl,
17737 /*allow_template_p=*/true);
17739 /* If the next token is a semicolon, this must be a specialization,
17740 instantiation, or friend declaration. Check the scope while we
17741 still know whether or not we had a nested-name-specifier. */
17742 if (type != error_mark_node
17743 && !nested_name_specifier && !is_friend
17744 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17745 check_unqualified_spec_or_inst (type, token->location);
17747 else if (decl == error_mark_node)
17748 type = error_mark_node;
17753 token = cp_lexer_peek_token (parser->lexer);
17754 identifier = cp_parser_identifier (parser);
17756 if (identifier == error_mark_node)
17758 parser->scope = NULL_TREE;
17759 return error_mark_node;
17762 /* For a `typename', we needn't call xref_tag. */
17763 if (tag_type == typename_type
17764 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17765 return cp_parser_make_typename_type (parser, identifier,
17768 /* Template parameter lists apply only if we are not within a
17769 function parameter list. */
17770 bool template_parm_lists_apply
17771 = parser->num_template_parameter_lists;
17772 if (template_parm_lists_apply)
17773 for (cp_binding_level *s = current_binding_level;
17774 s && s->kind != sk_template_parms;
17775 s = s->level_chain)
17776 if (s->kind == sk_function_parms)
17777 template_parm_lists_apply = false;
17779 /* Look up a qualified name in the usual way. */
17783 tree ambiguous_decls;
17785 decl = cp_parser_lookup_name (parser, identifier,
17787 /*is_template=*/false,
17788 /*is_namespace=*/false,
17789 /*check_dependency=*/true,
17793 /* If the lookup was ambiguous, an error will already have been
17795 if (ambiguous_decls)
17796 return error_mark_node;
17798 /* If we are parsing friend declaration, DECL may be a
17799 TEMPLATE_DECL tree node here. However, we need to check
17800 whether this TEMPLATE_DECL results in valid code. Consider
17801 the following example:
17804 template <class T> class C {};
17807 template <class T> friend class N::C; // #1, valid code
17809 template <class T> class Y {
17810 friend class N::C; // #2, invalid code
17813 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17814 name lookup of `N::C'. We see that friend declaration must
17815 be template for the code to be valid. Note that
17816 processing_template_decl does not work here since it is
17817 always 1 for the above two cases. */
17819 decl = (cp_parser_maybe_treat_template_as_class
17820 (decl, /*tag_name_p=*/is_friend
17821 && template_parm_lists_apply));
17823 if (TREE_CODE (decl) != TYPE_DECL)
17825 cp_parser_diagnose_invalid_type_name (parser,
17828 return error_mark_node;
17831 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17833 bool allow_template = (template_parm_lists_apply
17834 || DECL_SELF_REFERENCE_P (decl));
17835 type = check_elaborated_type_specifier (tag_type, decl,
17838 if (type == error_mark_node)
17839 return error_mark_node;
17842 /* Forward declarations of nested types, such as
17847 are invalid unless all components preceding the final '::'
17848 are complete. If all enclosing types are complete, these
17849 declarations become merely pointless.
17851 Invalid forward declarations of nested types are errors
17852 caught elsewhere in parsing. Those that are pointless arrive
17855 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17856 && !is_friend && !processing_explicit_instantiation)
17857 warning (0, "declaration %qD does not declare anything", decl);
17859 type = TREE_TYPE (decl);
17863 /* An elaborated-type-specifier sometimes introduces a new type and
17864 sometimes names an existing type. Normally, the rule is that it
17865 introduces a new type only if there is not an existing type of
17866 the same name already in scope. For example, given:
17869 void f() { struct S s; }
17871 the `struct S' in the body of `f' is the same `struct S' as in
17872 the global scope; the existing definition is used. However, if
17873 there were no global declaration, this would introduce a new
17874 local class named `S'.
17876 An exception to this rule applies to the following code:
17878 namespace N { struct S; }
17880 Here, the elaborated-type-specifier names a new type
17881 unconditionally; even if there is already an `S' in the
17882 containing scope this declaration names a new type.
17883 This exception only applies if the elaborated-type-specifier
17884 forms the complete declaration:
17888 A declaration consisting solely of `class-key identifier ;' is
17889 either a redeclaration of the name in the current scope or a
17890 forward declaration of the identifier as a class name. It
17891 introduces the name into the current scope.
17893 We are in this situation precisely when the next token is a `;'.
17895 An exception to the exception is that a `friend' declaration does
17896 *not* name a new type; i.e., given:
17898 struct S { friend struct T; };
17900 `T' is not a new type in the scope of `S'.
17902 Also, `new struct S' or `sizeof (struct S)' never results in the
17903 definition of a new type; a new type can only be declared in a
17904 declaration context. */
17910 /* Friends have special name lookup rules. */
17911 ts = ts_within_enclosing_non_class;
17912 else if (is_declaration
17913 && cp_lexer_next_token_is (parser->lexer,
17915 /* This is a `class-key identifier ;' */
17921 (template_parm_lists_apply
17922 && (cp_parser_next_token_starts_class_definition_p (parser)
17923 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17924 /* An unqualified name was used to reference this type, so
17925 there were no qualifying templates. */
17926 if (template_parm_lists_apply
17927 && !cp_parser_check_template_parameters (parser,
17928 /*num_templates=*/0,
17929 /*template_id*/false,
17931 /*declarator=*/NULL))
17932 return error_mark_node;
17933 type = xref_tag (tag_type, identifier, ts, template_p);
17937 if (type == error_mark_node)
17938 return error_mark_node;
17940 /* Allow attributes on forward declarations of classes. */
17943 if (TREE_CODE (type) == TYPENAME_TYPE)
17944 warning (OPT_Wattributes,
17945 "attributes ignored on uninstantiated type");
17946 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17947 && ! processing_explicit_instantiation)
17948 warning (OPT_Wattributes,
17949 "attributes ignored on template instantiation");
17950 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17951 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17953 warning (OPT_Wattributes,
17954 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17957 if (tag_type != enum_type)
17959 /* Indicate whether this class was declared as a `class' or as a
17961 if (CLASS_TYPE_P (type))
17962 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17963 cp_parser_check_class_key (tag_type, type);
17966 /* A "<" cannot follow an elaborated type specifier. If that
17967 happens, the user was probably trying to form a template-id. */
17968 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17974 /* Parse an enum-specifier.
17977 enum-head { enumerator-list [opt] }
17978 enum-head { enumerator-list , } [C++0x]
17981 enum-key identifier [opt] enum-base [opt]
17982 enum-key nested-name-specifier identifier enum-base [opt]
17987 enum struct [C++0x]
17990 : type-specifier-seq
17992 opaque-enum-specifier:
17993 enum-key identifier enum-base [opt] ;
17996 enum-key attributes[opt] identifier [opt] enum-base [opt]
17997 { enumerator-list [opt] }attributes[opt]
17998 enum-key attributes[opt] identifier [opt] enum-base [opt]
17999 { enumerator-list, }attributes[opt] [C++0x]
18001 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18002 if the token stream isn't an enum-specifier after all. */
18005 cp_parser_enum_specifier (cp_parser* parser)
18008 tree type = NULL_TREE;
18010 tree nested_name_specifier = NULL_TREE;
18012 bool scoped_enum_p = false;
18013 bool has_underlying_type = false;
18014 bool nested_being_defined = false;
18015 bool new_value_list = false;
18016 bool is_new_type = false;
18017 bool is_unnamed = false;
18018 tree underlying_type = NULL_TREE;
18019 cp_token *type_start_token = NULL;
18020 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18022 parser->colon_corrects_to_scope_p = false;
18024 /* Parse tentatively so that we can back up if we don't find a
18026 cp_parser_parse_tentatively (parser);
18028 /* Caller guarantees that the current token is 'enum', an identifier
18029 possibly follows, and the token after that is an opening brace.
18030 If we don't have an identifier, fabricate an anonymous name for
18031 the enumeration being defined. */
18032 cp_lexer_consume_token (parser->lexer);
18034 /* Parse the "class" or "struct", which indicates a scoped
18035 enumeration type in C++0x. */
18036 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18037 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18039 if (cxx_dialect < cxx11)
18040 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18042 /* Consume the `struct' or `class' token. */
18043 cp_lexer_consume_token (parser->lexer);
18045 scoped_enum_p = true;
18048 attributes = cp_parser_attributes_opt (parser);
18050 /* Clear the qualification. */
18051 parser->scope = NULL_TREE;
18052 parser->qualifying_scope = NULL_TREE;
18053 parser->object_scope = NULL_TREE;
18055 /* Figure out in what scope the declaration is being placed. */
18056 prev_scope = current_scope ();
18058 type_start_token = cp_lexer_peek_token (parser->lexer);
18060 push_deferring_access_checks (dk_no_check);
18061 nested_name_specifier
18062 = cp_parser_nested_name_specifier_opt (parser,
18063 /*typename_keyword_p=*/true,
18064 /*check_dependency_p=*/false,
18066 /*is_declaration=*/false);
18068 if (nested_name_specifier)
18072 identifier = cp_parser_identifier (parser);
18073 name = cp_parser_lookup_name (parser, identifier,
18075 /*is_template=*/false,
18076 /*is_namespace=*/false,
18077 /*check_dependency=*/true,
18078 /*ambiguous_decls=*/NULL,
18080 if (name && name != error_mark_node)
18082 type = TREE_TYPE (name);
18083 if (TREE_CODE (type) == TYPENAME_TYPE)
18085 /* Are template enums allowed in ISO? */
18086 if (template_parm_scope_p ())
18087 pedwarn (type_start_token->location, OPT_Wpedantic,
18088 "%qD is an enumeration template", name);
18089 /* ignore a typename reference, for it will be solved by name
18094 else if (nested_name_specifier == error_mark_node)
18095 /* We already issued an error. */;
18098 error_at (type_start_token->location,
18099 "%qD does not name an enumeration in %qT",
18100 identifier, nested_name_specifier);
18101 nested_name_specifier = error_mark_node;
18106 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18107 identifier = cp_parser_identifier (parser);
18110 identifier = make_anon_name ();
18113 error_at (type_start_token->location,
18114 "unnamed scoped enum is not allowed");
18117 pop_deferring_access_checks ();
18119 /* Check for the `:' that denotes a specified underlying type in C++0x.
18120 Note that a ':' could also indicate a bitfield width, however. */
18121 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18123 cp_decl_specifier_seq type_specifiers;
18125 /* Consume the `:'. */
18126 cp_lexer_consume_token (parser->lexer);
18128 /* Parse the type-specifier-seq. */
18129 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18130 /*is_trailing_return=*/false,
18133 /* At this point this is surely not elaborated type specifier. */
18134 if (!cp_parser_parse_definitely (parser))
18137 if (cxx_dialect < cxx11)
18138 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18140 has_underlying_type = true;
18142 /* If that didn't work, stop. */
18143 if (type_specifiers.type != error_mark_node)
18145 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18146 /*initialized=*/0, NULL);
18147 if (underlying_type == error_mark_node
18148 || check_for_bare_parameter_packs (underlying_type))
18149 underlying_type = NULL_TREE;
18153 /* Look for the `{' but don't consume it yet. */
18154 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18156 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18158 cp_parser_error (parser, "expected %<{%>");
18159 if (has_underlying_type)
18165 /* An opaque-enum-specifier must have a ';' here. */
18166 if ((scoped_enum_p || underlying_type)
18167 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18169 cp_parser_error (parser, "expected %<;%> or %<{%>");
18170 if (has_underlying_type)
18178 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18181 if (nested_name_specifier)
18183 if (CLASS_TYPE_P (nested_name_specifier))
18185 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18186 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18187 push_scope (nested_name_specifier);
18189 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18191 push_nested_namespace (nested_name_specifier);
18195 /* Issue an error message if type-definitions are forbidden here. */
18196 if (!cp_parser_check_type_definition (parser))
18197 type = error_mark_node;
18199 /* Create the new type. We do this before consuming the opening
18200 brace so the enum will be recorded as being on the line of its
18201 tag (or the 'enum' keyword, if there is no tag). */
18202 type = start_enum (identifier, type, underlying_type,
18203 attributes, scoped_enum_p, &is_new_type);
18205 /* If the next token is not '{' it is an opaque-enum-specifier or an
18206 elaborated-type-specifier. */
18207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18209 timevar_push (TV_PARSE_ENUM);
18210 if (nested_name_specifier
18211 && nested_name_specifier != error_mark_node)
18213 /* The following catches invalid code such as:
18214 enum class S<int>::E { A, B, C }; */
18215 if (!processing_specialization
18216 && CLASS_TYPE_P (nested_name_specifier)
18217 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18218 error_at (type_start_token->location, "cannot add an enumerator "
18219 "list to a template instantiation");
18221 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18223 error_at (type_start_token->location,
18224 "%<%T::%E%> has not been declared",
18225 TYPE_CONTEXT (nested_name_specifier),
18226 nested_name_specifier);
18227 type = error_mark_node;
18229 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18230 && !CLASS_TYPE_P (nested_name_specifier))
18232 error_at (type_start_token->location, "nested name specifier "
18233 "%qT for enum declaration does not name a class "
18234 "or namespace", nested_name_specifier);
18235 type = error_mark_node;
18237 /* If that scope does not contain the scope in which the
18238 class was originally declared, the program is invalid. */
18239 else if (prev_scope && !is_ancestor (prev_scope,
18240 nested_name_specifier))
18242 if (at_namespace_scope_p ())
18243 error_at (type_start_token->location,
18244 "declaration of %qD in namespace %qD which does not "
18246 type, prev_scope, nested_name_specifier);
18248 error_at (type_start_token->location,
18249 "declaration of %qD in %qD which does not "
18251 type, prev_scope, nested_name_specifier);
18252 type = error_mark_node;
18254 /* If that scope is the scope where the declaration is being placed
18255 the program is invalid. */
18256 else if (CLASS_TYPE_P (nested_name_specifier)
18257 && CLASS_TYPE_P (prev_scope)
18258 && same_type_p (nested_name_specifier, prev_scope))
18260 permerror (type_start_token->location,
18261 "extra qualification not allowed");
18262 nested_name_specifier = NULL_TREE;
18267 begin_scope (sk_scoped_enum, type);
18269 /* Consume the opening brace. */
18270 matching_braces braces;
18271 braces.consume_open (parser);
18273 if (type == error_mark_node)
18274 ; /* Nothing to add */
18275 else if (OPAQUE_ENUM_P (type)
18276 || (cxx_dialect > cxx98 && processing_specialization))
18278 new_value_list = true;
18279 SET_OPAQUE_ENUM_P (type, false);
18280 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18284 error_at (type_start_token->location,
18285 "multiple definition of %q#T", type);
18286 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18287 "previous definition here");
18288 type = error_mark_node;
18291 if (type == error_mark_node)
18292 cp_parser_skip_to_end_of_block_or_statement (parser);
18293 /* If the next token is not '}', then there are some enumerators. */
18294 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18296 if (is_unnamed && !scoped_enum_p)
18297 pedwarn (type_start_token->location, OPT_Wpedantic,
18298 "ISO C++ forbids empty unnamed enum");
18301 cp_parser_enumerator_list (parser, type);
18303 /* Consume the final '}'. */
18304 braces.require_close (parser);
18308 timevar_pop (TV_PARSE_ENUM);
18312 /* If a ';' follows, then it is an opaque-enum-specifier
18313 and additional restrictions apply. */
18314 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18317 error_at (type_start_token->location,
18318 "opaque-enum-specifier without name");
18319 else if (nested_name_specifier)
18320 error_at (type_start_token->location,
18321 "opaque-enum-specifier must use a simple identifier");
18325 /* Look for trailing attributes to apply to this enumeration, and
18326 apply them if appropriate. */
18327 if (cp_parser_allow_gnu_extensions_p (parser))
18329 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18330 cplus_decl_attributes (&type,
18332 (int) ATTR_FLAG_TYPE_IN_PLACE);
18335 /* Finish up the enumeration. */
18336 if (type != error_mark_node)
18338 if (new_value_list)
18339 finish_enum_value_list (type);
18341 finish_enum (type);
18344 if (nested_name_specifier)
18346 if (CLASS_TYPE_P (nested_name_specifier))
18348 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18349 pop_scope (nested_name_specifier);
18351 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18353 pop_nested_namespace (nested_name_specifier);
18357 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18361 /* Parse an enumerator-list. The enumerators all have the indicated
18365 enumerator-definition
18366 enumerator-list , enumerator-definition */
18369 cp_parser_enumerator_list (cp_parser* parser, tree type)
18373 /* Parse an enumerator-definition. */
18374 cp_parser_enumerator_definition (parser, type);
18376 /* If the next token is not a ',', we've reached the end of
18378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18380 /* Otherwise, consume the `,' and keep going. */
18381 cp_lexer_consume_token (parser->lexer);
18382 /* If the next token is a `}', there is a trailing comma. */
18383 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18385 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18386 pedwarn (input_location, OPT_Wpedantic,
18387 "comma at end of enumerator list");
18393 /* Parse an enumerator-definition. The enumerator has the indicated
18396 enumerator-definition:
18398 enumerator = constant-expression
18405 enumerator-definition:
18406 enumerator attributes [opt]
18407 enumerator attributes [opt] = constant-expression */
18410 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18416 /* Save the input location because we are interested in the location
18417 of the identifier and not the location of the explicit value. */
18418 loc = cp_lexer_peek_token (parser->lexer)->location;
18420 /* Look for the identifier. */
18421 identifier = cp_parser_identifier (parser);
18422 if (identifier == error_mark_node)
18425 /* Parse any specified attributes. */
18426 tree attrs = cp_parser_attributes_opt (parser);
18428 /* If the next token is an '=', then there is an explicit value. */
18429 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18431 /* Consume the `=' token. */
18432 cp_lexer_consume_token (parser->lexer);
18433 /* Parse the value. */
18434 value = cp_parser_constant_expression (parser);
18439 /* If we are processing a template, make sure the initializer of the
18440 enumerator doesn't contain any bare template parameter pack. */
18441 if (check_for_bare_parameter_packs (value))
18442 value = error_mark_node;
18444 /* Create the enumerator. */
18445 build_enumerator (identifier, value, type, attrs, loc);
18448 /* Parse a namespace-name.
18451 original-namespace-name
18454 Returns the NAMESPACE_DECL for the namespace. */
18457 cp_parser_namespace_name (cp_parser* parser)
18460 tree namespace_decl;
18462 cp_token *token = cp_lexer_peek_token (parser->lexer);
18464 /* Get the name of the namespace. */
18465 identifier = cp_parser_identifier (parser);
18466 if (identifier == error_mark_node)
18467 return error_mark_node;
18469 /* Look up the identifier in the currently active scope. Look only
18470 for namespaces, due to:
18472 [basic.lookup.udir]
18474 When looking up a namespace-name in a using-directive or alias
18475 definition, only namespace names are considered.
18479 [basic.lookup.qual]
18481 During the lookup of a name preceding the :: scope resolution
18482 operator, object, function, and enumerator names are ignored.
18484 (Note that cp_parser_qualifying_entity only calls this
18485 function if the token after the name is the scope resolution
18487 namespace_decl = cp_parser_lookup_name (parser, identifier,
18489 /*is_template=*/false,
18490 /*is_namespace=*/true,
18491 /*check_dependency=*/true,
18492 /*ambiguous_decls=*/NULL,
18494 /* If it's not a namespace, issue an error. */
18495 if (namespace_decl == error_mark_node
18496 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18498 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18500 error_at (token->location, "%qD is not a namespace-name", identifier);
18501 if (namespace_decl == error_mark_node
18502 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18503 suggest_alternative_in_explicit_scope (token->location, identifier,
18506 cp_parser_error (parser, "expected namespace-name");
18507 namespace_decl = error_mark_node;
18510 return namespace_decl;
18513 /* Parse a namespace-definition.
18515 namespace-definition:
18516 named-namespace-definition
18517 unnamed-namespace-definition
18519 named-namespace-definition:
18520 original-namespace-definition
18521 extension-namespace-definition
18523 original-namespace-definition:
18524 namespace identifier { namespace-body }
18526 extension-namespace-definition:
18527 namespace original-namespace-name { namespace-body }
18529 unnamed-namespace-definition:
18530 namespace { namespace-body } */
18533 cp_parser_namespace_definition (cp_parser* parser)
18536 int nested_definition_count = 0;
18538 cp_ensure_no_omp_declare_simd (parser);
18539 cp_ensure_no_oacc_routine (parser);
18541 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18545 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18546 cp_lexer_consume_token (parser->lexer);
18549 /* Look for the `namespace' keyword. */
18551 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18553 /* Parse any specified attributes before the identifier. */
18554 tree attribs = cp_parser_attributes_opt (parser);
18558 identifier = NULL_TREE;
18560 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18562 identifier = cp_parser_identifier (parser);
18564 /* Parse any attributes specified after the identifier. */
18565 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18571 if (!nested_definition_count && cxx_dialect < cxx17)
18572 pedwarn (input_location, OPT_Wpedantic,
18573 "nested namespace definitions only available with "
18574 "-std=c++17 or -std=gnu++17");
18576 /* Nested namespace names can create new namespaces (unlike
18577 other qualified-ids). */
18578 if (int count = identifier ? push_namespace (identifier) : 0)
18579 nested_definition_count += count;
18581 cp_parser_error (parser, "nested namespace name required");
18582 cp_lexer_consume_token (parser->lexer);
18585 if (nested_definition_count && !identifier)
18586 cp_parser_error (parser, "namespace name required");
18588 if (nested_definition_count && attribs)
18589 error_at (token->location,
18590 "a nested namespace definition cannot have attributes");
18591 if (nested_definition_count && is_inline)
18592 error_at (token->location,
18593 "a nested namespace definition cannot be inline");
18595 /* Start the namespace. */
18596 nested_definition_count += push_namespace (identifier, is_inline);
18598 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18600 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18602 /* Look for the `{' to validate starting the namespace. */
18603 matching_braces braces;
18604 if (braces.require_open (parser))
18606 /* Parse the body of the namespace. */
18607 cp_parser_namespace_body (parser);
18609 /* Look for the final `}'. */
18610 braces.require_close (parser);
18613 if (has_visibility)
18614 pop_visibility (1);
18616 /* Pop the nested namespace definitions. */
18617 while (nested_definition_count--)
18621 /* Parse a namespace-body.
18624 declaration-seq [opt] */
18627 cp_parser_namespace_body (cp_parser* parser)
18629 cp_parser_declaration_seq_opt (parser);
18632 /* Parse a namespace-alias-definition.
18634 namespace-alias-definition:
18635 namespace identifier = qualified-namespace-specifier ; */
18638 cp_parser_namespace_alias_definition (cp_parser* parser)
18641 tree namespace_specifier;
18643 cp_token *token = cp_lexer_peek_token (parser->lexer);
18645 /* Look for the `namespace' keyword. */
18646 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18647 /* Look for the identifier. */
18648 identifier = cp_parser_identifier (parser);
18649 if (identifier == error_mark_node)
18651 /* Look for the `=' token. */
18652 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18653 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18655 error_at (token->location, "%<namespace%> definition is not allowed here");
18656 /* Skip the definition. */
18657 cp_lexer_consume_token (parser->lexer);
18658 if (cp_parser_skip_to_closing_brace (parser))
18659 cp_lexer_consume_token (parser->lexer);
18662 cp_parser_require (parser, CPP_EQ, RT_EQ);
18663 /* Look for the qualified-namespace-specifier. */
18664 namespace_specifier
18665 = cp_parser_qualified_namespace_specifier (parser);
18666 /* Look for the `;' token. */
18667 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18669 /* Register the alias in the symbol table. */
18670 do_namespace_alias (identifier, namespace_specifier);
18673 /* Parse a qualified-namespace-specifier.
18675 qualified-namespace-specifier:
18676 :: [opt] nested-name-specifier [opt] namespace-name
18678 Returns a NAMESPACE_DECL corresponding to the specified
18682 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18684 /* Look for the optional `::'. */
18685 cp_parser_global_scope_opt (parser,
18686 /*current_scope_valid_p=*/false);
18688 /* Look for the optional nested-name-specifier. */
18689 cp_parser_nested_name_specifier_opt (parser,
18690 /*typename_keyword_p=*/false,
18691 /*check_dependency_p=*/true,
18693 /*is_declaration=*/true);
18695 return cp_parser_namespace_name (parser);
18698 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18699 access declaration.
18702 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18703 using :: unqualified-id ;
18705 access-declaration:
18711 cp_parser_using_declaration (cp_parser* parser,
18712 bool access_declaration_p)
18715 bool typename_p = false;
18716 bool global_scope_p;
18720 int oldcount = errorcount;
18721 cp_token *diag_token = NULL;
18723 if (access_declaration_p)
18725 diag_token = cp_lexer_peek_token (parser->lexer);
18726 cp_parser_parse_tentatively (parser);
18730 /* Look for the `using' keyword. */
18731 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18734 /* Peek at the next token. */
18735 token = cp_lexer_peek_token (parser->lexer);
18736 /* See if it's `typename'. */
18737 if (token->keyword == RID_TYPENAME)
18739 /* Remember that we've seen it. */
18741 /* Consume the `typename' token. */
18742 cp_lexer_consume_token (parser->lexer);
18746 /* Look for the optional global scope qualification. */
18748 = (cp_parser_global_scope_opt (parser,
18749 /*current_scope_valid_p=*/false)
18752 /* If we saw `typename', or didn't see `::', then there must be a
18753 nested-name-specifier present. */
18754 if (typename_p || !global_scope_p)
18756 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18757 /*check_dependency_p=*/true,
18759 /*is_declaration=*/true);
18760 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18762 cp_parser_skip_to_end_of_block_or_statement (parser);
18766 /* Otherwise, we could be in either of the two productions. In that
18767 case, treat the nested-name-specifier as optional. */
18769 qscope = cp_parser_nested_name_specifier_opt (parser,
18770 /*typename_keyword_p=*/false,
18771 /*check_dependency_p=*/true,
18773 /*is_declaration=*/true);
18775 qscope = global_namespace;
18776 else if (UNSCOPED_ENUM_P (qscope))
18777 qscope = CP_TYPE_CONTEXT (qscope);
18779 if (access_declaration_p && cp_parser_error_occurred (parser))
18780 /* Something has already gone wrong; there's no need to parse
18781 further. Since an error has occurred, the return value of
18782 cp_parser_parse_definitely will be false, as required. */
18783 return cp_parser_parse_definitely (parser);
18785 token = cp_lexer_peek_token (parser->lexer);
18786 /* Parse the unqualified-id. */
18787 identifier = cp_parser_unqualified_id (parser,
18788 /*template_keyword_p=*/false,
18789 /*check_dependency_p=*/true,
18790 /*declarator_p=*/true,
18791 /*optional_p=*/false);
18793 if (access_declaration_p)
18795 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18796 cp_parser_simulate_error (parser);
18797 if (!cp_parser_parse_definitely (parser))
18800 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18802 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18803 if (cxx_dialect < cxx17
18804 && !in_system_header_at (ell->location))
18805 pedwarn (ell->location, 0,
18806 "pack expansion in using-declaration only available "
18807 "with -std=c++17 or -std=gnu++17");
18808 qscope = make_pack_expansion (qscope);
18811 /* The function we call to handle a using-declaration is different
18812 depending on what scope we are in. */
18813 if (qscope == error_mark_node || identifier == error_mark_node)
18815 else if (!identifier_p (identifier)
18816 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18817 /* [namespace.udecl]
18819 A using declaration shall not name a template-id. */
18820 error_at (token->location,
18821 "a template-id may not appear in a using-declaration");
18824 if (at_class_scope_p ())
18826 /* Create the USING_DECL. */
18827 decl = do_class_using_decl (qscope, identifier);
18829 if (decl && typename_p)
18830 USING_DECL_TYPENAME_P (decl) = 1;
18832 if (check_for_bare_parameter_packs (decl))
18834 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18838 /* Add it to the list of members in this class. */
18839 finish_member_declaration (decl);
18843 decl = cp_parser_lookup_name_simple (parser,
18846 if (decl == error_mark_node)
18847 cp_parser_name_lookup_error (parser, identifier,
18850 else if (check_for_bare_parameter_packs (decl))
18852 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18855 else if (!at_namespace_scope_p ())
18856 finish_local_using_decl (decl, qscope, identifier);
18858 finish_namespace_using_decl (decl, qscope, identifier);
18862 if (!access_declaration_p
18863 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18865 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18866 if (cxx_dialect < cxx17)
18867 pedwarn (comma->location, 0,
18868 "comma-separated list in using-declaration only available "
18869 "with -std=c++17 or -std=gnu++17");
18873 /* Look for the final `;'. */
18874 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18876 if (access_declaration_p && errorcount == oldcount)
18877 warning_at (diag_token->location, OPT_Wdeprecated,
18878 "access declarations are deprecated "
18879 "in favour of using-declarations; "
18880 "suggestion: add the %<using%> keyword");
18885 /* Parse an alias-declaration.
18888 using identifier attribute-specifier-seq [opt] = type-id */
18891 cp_parser_alias_declaration (cp_parser* parser)
18893 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18894 location_t id_location;
18895 cp_declarator *declarator;
18896 cp_decl_specifier_seq decl_specs;
18898 const char *saved_message = NULL;
18900 /* Look for the `using' keyword. */
18901 cp_token *using_token
18902 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18903 if (using_token == NULL)
18904 return error_mark_node;
18906 id_location = cp_lexer_peek_token (parser->lexer)->location;
18907 id = cp_parser_identifier (parser);
18908 if (id == error_mark_node)
18909 return error_mark_node;
18911 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18912 attributes = cp_parser_attributes_opt (parser);
18913 if (attributes == error_mark_node)
18914 return error_mark_node;
18916 cp_parser_require (parser, CPP_EQ, RT_EQ);
18918 if (cp_parser_error_occurred (parser))
18919 return error_mark_node;
18921 cp_parser_commit_to_tentative_parse (parser);
18923 /* Now we are going to parse the type-id of the declaration. */
18928 "A type-specifier-seq shall not define a class or enumeration
18929 unless it appears in the type-id of an alias-declaration (7.1.3) that
18930 is not the declaration of a template-declaration."
18932 In other words, if we currently are in an alias template, the
18933 type-id should not define a type.
18935 So let's set parser->type_definition_forbidden_message in that
18936 case; cp_parser_check_type_definition (called by
18937 cp_parser_class_specifier) will then emit an error if a type is
18938 defined in the type-id. */
18939 if (parser->num_template_parameter_lists)
18941 saved_message = parser->type_definition_forbidden_message;
18942 parser->type_definition_forbidden_message =
18943 G_("types may not be defined in alias template declarations");
18946 type = cp_parser_type_id (parser);
18948 /* Restore the error message if need be. */
18949 if (parser->num_template_parameter_lists)
18950 parser->type_definition_forbidden_message = saved_message;
18952 if (type == error_mark_node
18953 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18955 cp_parser_skip_to_end_of_block_or_statement (parser);
18956 return error_mark_node;
18959 /* A typedef-name can also be introduced by an alias-declaration. The
18960 identifier following the using keyword becomes a typedef-name. It has
18961 the same semantics as if it were introduced by the typedef
18962 specifier. In particular, it does not define a new type and it shall
18963 not appear in the type-id. */
18965 clear_decl_specs (&decl_specs);
18966 decl_specs.type = type;
18967 if (attributes != NULL_TREE)
18969 decl_specs.attributes = attributes;
18970 set_and_check_decl_spec_loc (&decl_specs,
18974 set_and_check_decl_spec_loc (&decl_specs,
18977 set_and_check_decl_spec_loc (&decl_specs,
18981 if (parser->num_template_parameter_lists
18982 && !cp_parser_check_template_parameters (parser,
18983 /*num_templates=*/0,
18984 /*template_id*/false,
18986 /*declarator=*/NULL))
18987 return error_mark_node;
18989 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18990 declarator->id_loc = id_location;
18992 member_p = at_class_scope_p ();
18994 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18995 NULL_TREE, attributes);
18997 decl = start_decl (declarator, &decl_specs, 0,
18998 attributes, NULL_TREE, &pushed_scope);
18999 if (decl == error_mark_node)
19002 // Attach constraints to the alias declaration.
19003 if (flag_concepts && current_template_parms)
19005 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19006 tree constr = build_constraints (reqs, NULL_TREE);
19007 set_constraints (decl, constr);
19010 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19013 pop_scope (pushed_scope);
19015 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19016 added into the symbol table; otherwise, return the TYPE_DECL. */
19017 if (DECL_LANG_SPECIFIC (decl)
19018 && DECL_TEMPLATE_INFO (decl)
19019 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19021 decl = DECL_TI_TEMPLATE (decl);
19023 check_member_template (decl);
19029 /* Parse a using-directive.
19032 using namespace :: [opt] nested-name-specifier [opt]
19033 namespace-name ; */
19036 cp_parser_using_directive (cp_parser* parser)
19038 tree namespace_decl;
19041 /* Look for the `using' keyword. */
19042 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19043 /* And the `namespace' keyword. */
19044 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19045 /* Look for the optional `::' operator. */
19046 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19047 /* And the optional nested-name-specifier. */
19048 cp_parser_nested_name_specifier_opt (parser,
19049 /*typename_keyword_p=*/false,
19050 /*check_dependency_p=*/true,
19052 /*is_declaration=*/true);
19053 /* Get the namespace being used. */
19054 namespace_decl = cp_parser_namespace_name (parser);
19055 /* And any specified attributes. */
19056 attribs = cp_parser_attributes_opt (parser);
19058 /* Update the symbol table. */
19059 if (namespace_bindings_p ())
19060 finish_namespace_using_directive (namespace_decl, attribs);
19062 finish_local_using_directive (namespace_decl, attribs);
19064 /* Look for the final `;'. */
19065 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19068 /* Parse an asm-definition.
19075 asm-qualifier-list:
19077 asm-qualifier-list asm-qualifier
19080 asm ( string-literal ) ;
19085 asm asm-qualifier-list [opt] ( string-literal ) ;
19086 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19087 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19088 : asm-operand-list [opt] ) ;
19089 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19090 : asm-operand-list [opt]
19091 : asm-clobber-list [opt] ) ;
19092 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19093 : asm-clobber-list [opt]
19094 : asm-goto-list ) ;
19096 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19097 contains goto, and is the only allowed form in that case. No duplicates are
19098 allowed in an asm-qualifier-list. */
19101 cp_parser_asm_definition (cp_parser* parser)
19104 tree outputs = NULL_TREE;
19105 tree inputs = NULL_TREE;
19106 tree clobbers = NULL_TREE;
19107 tree labels = NULL_TREE;
19109 bool extended_p = false;
19110 bool invalid_inputs_p = false;
19111 bool invalid_outputs_p = false;
19112 required_token missing = RT_NONE;
19114 /* Look for the `asm' keyword. */
19115 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19117 if (parser->in_function_body
19118 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19120 error ("%<asm%> in %<constexpr%> function");
19121 cp_function_chain->invalid_constexpr = true;
19124 /* Handle the asm-qualifier-list. */
19125 location_t volatile_loc = UNKNOWN_LOCATION;
19126 location_t inline_loc = UNKNOWN_LOCATION;
19127 location_t goto_loc = UNKNOWN_LOCATION;
19128 location_t first_loc = UNKNOWN_LOCATION;
19130 if (cp_parser_allow_gnu_extensions_p (parser))
19133 cp_token *token = cp_lexer_peek_token (parser->lexer);
19134 location_t loc = token->location;
19135 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19140 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19141 inform (volatile_loc, "first seen here");
19144 volatile_loc = loc;
19145 cp_lexer_consume_token (parser->lexer);
19151 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19152 inform (inline_loc, "first seen here");
19158 cp_lexer_consume_token (parser->lexer);
19164 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19165 inform (goto_loc, "first seen here");
19171 cp_lexer_consume_token (parser->lexer);
19176 error_at (loc, "%qT is not an asm qualifier", token->u.value);
19177 cp_lexer_consume_token (parser->lexer);
19186 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19187 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19188 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19190 if (!parser->in_function_body && (inline_p || goto_p))
19192 error_at (first_loc, "asm qualifier outside of function body");
19193 inline_p = goto_p = false;
19196 /* Look for the opening `('. */
19197 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19199 /* Look for the string. */
19200 string = cp_parser_string_literal (parser, false, false);
19201 if (string == error_mark_node)
19203 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19204 /*consume_paren=*/true);
19208 /* If we're allowing GNU extensions, check for the extended assembly
19209 syntax. Unfortunately, the `:' tokens need not be separated by
19210 a space in C, and so, for compatibility, we tolerate that here
19211 too. Doing that means that we have to treat the `::' operator as
19213 if (cp_parser_allow_gnu_extensions_p (parser)
19214 && parser->in_function_body
19215 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19216 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19218 bool inputs_p = false;
19219 bool clobbers_p = false;
19220 bool labels_p = false;
19222 /* The extended syntax was used. */
19225 /* Look for outputs. */
19226 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19228 /* Consume the `:'. */
19229 cp_lexer_consume_token (parser->lexer);
19230 /* Parse the output-operands. */
19231 if (cp_lexer_next_token_is_not (parser->lexer,
19233 && cp_lexer_next_token_is_not (parser->lexer,
19235 && cp_lexer_next_token_is_not (parser->lexer,
19239 outputs = cp_parser_asm_operand_list (parser);
19240 if (outputs == error_mark_node)
19241 invalid_outputs_p = true;
19244 /* If the next token is `::', there are no outputs, and the
19245 next token is the beginning of the inputs. */
19246 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19247 /* The inputs are coming next. */
19250 /* Look for inputs. */
19252 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19254 /* Consume the `:' or `::'. */
19255 cp_lexer_consume_token (parser->lexer);
19256 /* Parse the output-operands. */
19257 if (cp_lexer_next_token_is_not (parser->lexer,
19259 && cp_lexer_next_token_is_not (parser->lexer,
19261 && cp_lexer_next_token_is_not (parser->lexer,
19264 inputs = cp_parser_asm_operand_list (parser);
19265 if (inputs == error_mark_node)
19266 invalid_inputs_p = true;
19269 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19270 /* The clobbers are coming next. */
19273 /* Look for clobbers. */
19275 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19278 /* Consume the `:' or `::'. */
19279 cp_lexer_consume_token (parser->lexer);
19280 /* Parse the clobbers. */
19281 if (cp_lexer_next_token_is_not (parser->lexer,
19283 && cp_lexer_next_token_is_not (parser->lexer,
19285 clobbers = cp_parser_asm_clobber_list (parser);
19287 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19288 /* The labels are coming next. */
19291 /* Look for labels. */
19293 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19296 /* Consume the `:' or `::'. */
19297 cp_lexer_consume_token (parser->lexer);
19298 /* Parse the labels. */
19299 labels = cp_parser_asm_label_list (parser);
19302 if (goto_p && !labels_p)
19303 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19306 missing = RT_COLON_SCOPE;
19308 /* Look for the closing `)'. */
19309 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19310 missing ? missing : RT_CLOSE_PAREN))
19311 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19312 /*consume_paren=*/true);
19313 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19315 if (!invalid_inputs_p && !invalid_outputs_p)
19317 /* Create the ASM_EXPR. */
19318 if (parser->in_function_body)
19320 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19321 inputs, clobbers, labels, inline_p);
19322 /* If the extended syntax was not used, mark the ASM_EXPR. */
19325 tree temp = asm_stmt;
19326 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19327 temp = TREE_OPERAND (temp, 0);
19329 ASM_INPUT_P (temp) = 1;
19333 symtab->finalize_toplevel_asm (string);
19337 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19338 type that comes from the decl-specifier-seq. */
19341 strip_declarator_types (tree type, cp_declarator *declarator)
19343 for (cp_declarator *d = declarator; d;)
19353 if (TYPE_PTRMEMFUNC_P (type))
19354 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19355 type = TREE_TYPE (type);
19363 /* Declarators [gram.dcl.decl] */
19365 /* Parse an init-declarator.
19368 declarator initializer [opt]
19373 declarator asm-specification [opt] attributes [opt] initializer [opt]
19375 function-definition:
19376 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19378 decl-specifier-seq [opt] declarator function-try-block
19382 function-definition:
19383 __extension__ function-definition
19387 function-definition:
19388 decl-specifier-seq [opt] declarator function-transaction-block
19390 The DECL_SPECIFIERS apply to this declarator. Returns a
19391 representation of the entity declared. If MEMBER_P is TRUE, then
19392 this declarator appears in a class scope. The new DECL created by
19393 this declarator is returned.
19395 The CHECKS are access checks that should be performed once we know
19396 what entity is being declared (and, therefore, what classes have
19399 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19400 for a function-definition here as well. If the declarator is a
19401 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19402 be TRUE upon return. By that point, the function-definition will
19403 have been completely parsed.
19405 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19408 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19409 parsed declaration if it is an uninitialized single declarator not followed
19410 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19411 if present, will not be consumed. If returned, this declarator will be
19412 created with SD_INITIALIZED but will not call cp_finish_decl.
19414 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19415 and there is an initializer, the pointed location_t is set to the
19416 location of the '=' or `(', or '{' in C++11 token introducing the
19420 cp_parser_init_declarator (cp_parser* parser,
19421 cp_decl_specifier_seq *decl_specifiers,
19422 vec<deferred_access_check, va_gc> *checks,
19423 bool function_definition_allowed_p,
19425 int declares_class_or_enum,
19426 bool* function_definition_p,
19427 tree* maybe_range_for_decl,
19428 location_t* init_loc,
19431 cp_token *token = NULL, *asm_spec_start_token = NULL,
19432 *attributes_start_token = NULL;
19433 cp_declarator *declarator;
19434 tree prefix_attributes;
19435 tree attributes = NULL;
19436 tree asm_specification;
19438 tree decl = NULL_TREE;
19440 int is_initialized;
19441 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19442 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19444 enum cpp_ttype initialization_kind;
19445 bool is_direct_init = false;
19446 bool is_non_constant_init;
19447 int ctor_dtor_or_conv_p;
19448 bool friend_p = cp_parser_friend_p (decl_specifiers);
19449 tree pushed_scope = NULL_TREE;
19450 bool range_for_decl_p = false;
19451 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19452 location_t tmp_init_loc = UNKNOWN_LOCATION;
19454 /* Gather the attributes that were provided with the
19455 decl-specifiers. */
19456 prefix_attributes = decl_specifiers->attributes;
19458 /* Assume that this is not the declarator for a function
19460 if (function_definition_p)
19461 *function_definition_p = false;
19463 /* Default arguments are only permitted for function parameters. */
19464 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19465 parser->default_arg_ok_p = false;
19467 /* Defer access checks while parsing the declarator; we cannot know
19468 what names are accessible until we know what is being
19470 resume_deferring_access_checks ();
19472 token = cp_lexer_peek_token (parser->lexer);
19474 /* Parse the declarator. */
19476 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19477 &ctor_dtor_or_conv_p,
19478 /*parenthesized_p=*/NULL,
19479 member_p, friend_p);
19480 /* Gather up the deferred checks. */
19481 stop_deferring_access_checks ();
19483 parser->default_arg_ok_p = saved_default_arg_ok_p;
19485 /* If the DECLARATOR was erroneous, there's no need to go
19487 if (declarator == cp_error_declarator)
19488 return error_mark_node;
19490 /* Check that the number of template-parameter-lists is OK. */
19491 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19493 return error_mark_node;
19495 if (declares_class_or_enum & 2)
19496 cp_parser_check_for_definition_in_return_type (declarator,
19497 decl_specifiers->type,
19498 decl_specifiers->locations[ds_type_spec]);
19500 /* Figure out what scope the entity declared by the DECLARATOR is
19501 located in. `grokdeclarator' sometimes changes the scope, so
19502 we compute it now. */
19503 scope = get_scope_of_declarator (declarator);
19505 /* Perform any lookups in the declared type which were thought to be
19506 dependent, but are not in the scope of the declarator. */
19507 decl_specifiers->type
19508 = maybe_update_decl_type (decl_specifiers->type, scope);
19510 /* If we're allowing GNU extensions, look for an
19511 asm-specification. */
19512 if (cp_parser_allow_gnu_extensions_p (parser))
19514 /* Look for an asm-specification. */
19515 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19516 asm_specification = cp_parser_asm_specification_opt (parser);
19519 asm_specification = NULL_TREE;
19521 /* Look for attributes. */
19522 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19523 attributes = cp_parser_attributes_opt (parser);
19525 /* Peek at the next token. */
19526 token = cp_lexer_peek_token (parser->lexer);
19528 bool bogus_implicit_tmpl = false;
19530 if (function_declarator_p (declarator))
19532 /* Handle C++17 deduction guides. */
19533 if (!decl_specifiers->type
19534 && ctor_dtor_or_conv_p <= 0
19535 && cxx_dialect >= cxx17)
19537 cp_declarator *id = get_id_declarator (declarator);
19538 tree name = id->u.id.unqualified_name;
19539 parser->scope = id->u.id.qualifying_scope;
19540 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19542 && (DECL_CLASS_TEMPLATE_P (tmpl)
19543 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19545 id->u.id.unqualified_name = dguide_name (tmpl);
19546 id->u.id.sfk = sfk_deduction_guide;
19547 ctor_dtor_or_conv_p = 1;
19551 /* Check to see if the token indicates the start of a
19552 function-definition. */
19553 if (cp_parser_token_starts_function_definition_p (token))
19555 if (!function_definition_allowed_p)
19557 /* If a function-definition should not appear here, issue an
19559 cp_parser_error (parser,
19560 "a function-definition is not allowed here");
19561 return error_mark_node;
19564 location_t func_brace_location
19565 = cp_lexer_peek_token (parser->lexer)->location;
19567 /* Neither attributes nor an asm-specification are allowed
19568 on a function-definition. */
19569 if (asm_specification)
19570 error_at (asm_spec_start_token->location,
19571 "an asm-specification is not allowed "
19572 "on a function-definition");
19574 error_at (attributes_start_token->location,
19575 "attributes are not allowed "
19576 "on a function-definition");
19577 /* This is a function-definition. */
19578 *function_definition_p = true;
19580 /* Parse the function definition. */
19582 decl = cp_parser_save_member_function_body (parser,
19585 prefix_attributes);
19588 (cp_parser_function_definition_from_specifiers_and_declarator
19589 (parser, decl_specifiers, prefix_attributes, declarator));
19591 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19593 /* This is where the prologue starts... */
19594 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19595 = func_brace_location;
19601 else if (parser->fully_implicit_function_template_p)
19603 /* A non-template declaration involving a function parameter list
19604 containing an implicit template parameter will be made into a
19605 template. If the resulting declaration is not going to be an
19606 actual function then finish the template scope here to prevent it.
19607 An error message will be issued once we have a decl to talk about.
19609 FIXME probably we should do type deduction rather than create an
19610 implicit template, but the standard currently doesn't allow it. */
19611 bogus_implicit_tmpl = true;
19612 finish_fully_implicit_template (parser, NULL_TREE);
19617 Only in function declarations for constructors, destructors, type
19618 conversions, and deduction guides can the decl-specifier-seq be omitted.
19620 We explicitly postpone this check past the point where we handle
19621 function-definitions because we tolerate function-definitions
19622 that are missing their return types in some modes. */
19623 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19625 cp_parser_error (parser,
19626 "expected constructor, destructor, or type conversion");
19627 return error_mark_node;
19630 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19631 if (token->type == CPP_EQ
19632 || token->type == CPP_OPEN_PAREN
19633 || token->type == CPP_OPEN_BRACE)
19635 is_initialized = SD_INITIALIZED;
19636 initialization_kind = token->type;
19637 if (maybe_range_for_decl)
19638 *maybe_range_for_decl = error_mark_node;
19639 tmp_init_loc = token->location;
19640 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19641 *init_loc = tmp_init_loc;
19643 if (token->type == CPP_EQ
19644 && function_declarator_p (declarator))
19646 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19647 if (t2->keyword == RID_DEFAULT)
19648 is_initialized = SD_DEFAULTED;
19649 else if (t2->keyword == RID_DELETE)
19650 is_initialized = SD_DELETED;
19655 /* If the init-declarator isn't initialized and isn't followed by a
19656 `,' or `;', it's not a valid init-declarator. */
19657 if (token->type != CPP_COMMA
19658 && token->type != CPP_SEMICOLON)
19660 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19661 range_for_decl_p = true;
19664 if (!maybe_range_for_decl)
19665 cp_parser_error (parser, "expected initializer");
19666 return error_mark_node;
19669 is_initialized = SD_UNINITIALIZED;
19670 initialization_kind = CPP_EOF;
19673 /* Because start_decl has side-effects, we should only call it if we
19674 know we're going ahead. By this point, we know that we cannot
19675 possibly be looking at any other construct. */
19676 cp_parser_commit_to_tentative_parse (parser);
19678 /* Enter the newly declared entry in the symbol table. If we're
19679 processing a declaration in a class-specifier, we wait until
19680 after processing the initializer. */
19683 if (parser->in_unbraced_linkage_specification_p)
19684 decl_specifiers->storage_class = sc_extern;
19685 decl = start_decl (declarator, decl_specifiers,
19686 range_for_decl_p? SD_INITIALIZED : is_initialized,
19687 attributes, prefix_attributes, &pushed_scope);
19688 cp_finalize_omp_declare_simd (parser, decl);
19689 cp_finalize_oacc_routine (parser, decl, false);
19690 /* Adjust location of decl if declarator->id_loc is more appropriate:
19691 set, and decl wasn't merged with another decl, in which case its
19692 location would be different from input_location, and more accurate. */
19694 && declarator->id_loc != UNKNOWN_LOCATION
19695 && DECL_SOURCE_LOCATION (decl) == input_location)
19696 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19699 /* Enter the SCOPE. That way unqualified names appearing in the
19700 initializer will be looked up in SCOPE. */
19701 pushed_scope = push_scope (scope);
19703 /* Perform deferred access control checks, now that we know in which
19704 SCOPE the declared entity resides. */
19705 if (!member_p && decl)
19707 tree saved_current_function_decl = NULL_TREE;
19709 /* If the entity being declared is a function, pretend that we
19710 are in its scope. If it is a `friend', it may have access to
19711 things that would not otherwise be accessible. */
19712 if (TREE_CODE (decl) == FUNCTION_DECL)
19714 saved_current_function_decl = current_function_decl;
19715 current_function_decl = decl;
19718 /* Perform access checks for template parameters. */
19719 cp_parser_perform_template_parameter_access_checks (checks);
19721 /* Perform the access control checks for the declarator and the
19722 decl-specifiers. */
19723 perform_deferred_access_checks (tf_warning_or_error);
19725 /* Restore the saved value. */
19726 if (TREE_CODE (decl) == FUNCTION_DECL)
19727 current_function_decl = saved_current_function_decl;
19730 /* Parse the initializer. */
19731 initializer = NULL_TREE;
19732 is_direct_init = false;
19733 is_non_constant_init = true;
19734 if (is_initialized)
19736 if (function_declarator_p (declarator))
19738 if (initialization_kind == CPP_EQ)
19739 initializer = cp_parser_pure_specifier (parser);
19742 /* If the declaration was erroneous, we don't really
19743 know what the user intended, so just silently
19744 consume the initializer. */
19745 if (decl != error_mark_node)
19746 error_at (tmp_init_loc, "initializer provided for function");
19747 cp_parser_skip_to_closing_parenthesis (parser,
19748 /*recovering=*/true,
19749 /*or_comma=*/false,
19750 /*consume_paren=*/true);
19755 /* We want to record the extra mangling scope for in-class
19756 initializers of class members and initializers of static data
19757 member templates. The former involves deferring
19758 parsing of the initializer until end of class as with default
19759 arguments. So right here we only handle the latter. */
19760 if (!member_p && processing_template_decl && decl != error_mark_node)
19761 start_lambda_scope (decl);
19762 initializer = cp_parser_initializer (parser,
19764 &is_non_constant_init);
19765 if (!member_p && processing_template_decl && decl != error_mark_node)
19766 finish_lambda_scope ();
19767 if (initializer == error_mark_node)
19768 cp_parser_skip_to_end_of_statement (parser);
19772 /* The old parser allows attributes to appear after a parenthesized
19773 initializer. Mark Mitchell proposed removing this functionality
19774 on the GCC mailing lists on 2002-08-13. This parser accepts the
19775 attributes -- but ignores them. Made a permerror in GCC 8. */
19776 if (cp_parser_allow_gnu_extensions_p (parser)
19777 && initialization_kind == CPP_OPEN_PAREN
19778 && cp_parser_attributes_opt (parser)
19779 && permerror (input_location,
19780 "attributes after parenthesized initializer ignored"))
19783 if (flag_permissive && !hint)
19786 inform (input_location,
19787 "this flexibility is deprecated and will be removed");
19791 /* And now complain about a non-function implicit template. */
19792 if (bogus_implicit_tmpl && decl != error_mark_node)
19793 error_at (DECL_SOURCE_LOCATION (decl),
19794 "non-function %qD declared as implicit template", decl);
19796 /* For an in-class declaration, use `grokfield' to create the
19802 pop_scope (pushed_scope);
19803 pushed_scope = NULL_TREE;
19805 decl = grokfield (declarator, decl_specifiers,
19806 initializer, !is_non_constant_init,
19807 /*asmspec=*/NULL_TREE,
19808 attr_chainon (attributes, prefix_attributes));
19809 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19810 cp_parser_save_default_args (parser, decl);
19811 cp_finalize_omp_declare_simd (parser, decl);
19812 cp_finalize_oacc_routine (parser, decl, false);
19815 /* Finish processing the declaration. But, skip member
19817 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19819 cp_finish_decl (decl,
19820 initializer, !is_non_constant_init,
19822 /* If the initializer is in parentheses, then this is
19823 a direct-initialization, which means that an
19824 `explicit' constructor is OK. Otherwise, an
19825 `explicit' constructor cannot be used. */
19826 ((is_direct_init || !is_initialized)
19827 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19829 else if ((cxx_dialect != cxx98) && friend_p
19830 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19831 /* Core issue #226 (C++0x only): A default template-argument
19832 shall not be specified in a friend class template
19834 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19835 /*is_partial=*/false, /*is_friend_decl=*/1);
19837 if (!friend_p && pushed_scope)
19838 pop_scope (pushed_scope);
19840 if (function_declarator_p (declarator)
19841 && parser->fully_implicit_function_template_p)
19844 decl = finish_fully_implicit_template (parser, decl);
19846 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19849 if (auto_result && is_initialized && decl_specifiers->type
19850 && type_uses_auto (decl_specifiers->type))
19851 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19856 /* Parse a declarator.
19860 ptr-operator declarator
19862 abstract-declarator:
19863 ptr-operator abstract-declarator [opt]
19864 direct-abstract-declarator
19869 attributes [opt] direct-declarator
19870 attributes [opt] ptr-operator declarator
19872 abstract-declarator:
19873 attributes [opt] ptr-operator abstract-declarator [opt]
19874 attributes [opt] direct-abstract-declarator
19876 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19877 detect constructors, destructors, deduction guides, or conversion operators.
19878 It is set to -1 if the declarator is a name, and +1 if it is a
19879 function. Otherwise it is set to zero. Usually you just want to
19880 test for >0, but internally the negative value is used.
19882 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19883 a decl-specifier-seq unless it declares a constructor, destructor,
19884 or conversion. It might seem that we could check this condition in
19885 semantic analysis, rather than parsing, but that makes it difficult
19886 to handle something like `f()'. We want to notice that there are
19887 no decl-specifiers, and therefore realize that this is an
19888 expression, not a declaration.)
19890 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19891 the declarator is a direct-declarator of the form "(...)".
19893 MEMBER_P is true iff this declarator is a member-declarator.
19895 FRIEND_P is true iff this declarator is a friend. */
19897 static cp_declarator *
19898 cp_parser_declarator (cp_parser* parser,
19899 cp_parser_declarator_kind dcl_kind,
19900 int* ctor_dtor_or_conv_p,
19901 bool* parenthesized_p,
19902 bool member_p, bool friend_p)
19904 cp_declarator *declarator;
19905 enum tree_code code;
19906 cp_cv_quals cv_quals;
19908 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19910 /* Assume this is not a constructor, destructor, or type-conversion
19912 if (ctor_dtor_or_conv_p)
19913 *ctor_dtor_or_conv_p = 0;
19915 if (cp_parser_allow_gnu_extensions_p (parser))
19916 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19918 /* Check for the ptr-operator production. */
19919 cp_parser_parse_tentatively (parser);
19920 /* Parse the ptr-operator. */
19921 code = cp_parser_ptr_operator (parser,
19926 /* If that worked, then we have a ptr-operator. */
19927 if (cp_parser_parse_definitely (parser))
19929 /* If a ptr-operator was found, then this declarator was not
19931 if (parenthesized_p)
19932 *parenthesized_p = true;
19933 /* The dependent declarator is optional if we are parsing an
19934 abstract-declarator. */
19935 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19936 cp_parser_parse_tentatively (parser);
19938 /* Parse the dependent declarator. */
19939 declarator = cp_parser_declarator (parser, dcl_kind,
19940 /*ctor_dtor_or_conv_p=*/NULL,
19941 /*parenthesized_p=*/NULL,
19942 /*member_p=*/false,
19945 /* If we are parsing an abstract-declarator, we must handle the
19946 case where the dependent declarator is absent. */
19947 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19948 && !cp_parser_parse_definitely (parser))
19951 declarator = cp_parser_make_indirect_declarator
19952 (code, class_type, cv_quals, declarator, std_attributes);
19954 /* Everything else is a direct-declarator. */
19957 if (parenthesized_p)
19958 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19960 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19961 ctor_dtor_or_conv_p,
19962 member_p, friend_p);
19965 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19966 declarator->attributes = gnu_attributes;
19970 /* Parse a direct-declarator or direct-abstract-declarator.
19974 direct-declarator ( parameter-declaration-clause )
19975 cv-qualifier-seq [opt]
19976 ref-qualifier [opt]
19977 exception-specification [opt]
19978 direct-declarator [ constant-expression [opt] ]
19981 direct-abstract-declarator:
19982 direct-abstract-declarator [opt]
19983 ( parameter-declaration-clause )
19984 cv-qualifier-seq [opt]
19985 ref-qualifier [opt]
19986 exception-specification [opt]
19987 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19988 ( abstract-declarator )
19990 Returns a representation of the declarator. DCL_KIND is
19991 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19992 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19993 we are parsing a direct-declarator. It is
19994 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19995 of ambiguity we prefer an abstract declarator, as per
19996 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19997 as for cp_parser_declarator. */
19999 static cp_declarator *
20000 cp_parser_direct_declarator (cp_parser* parser,
20001 cp_parser_declarator_kind dcl_kind,
20002 int* ctor_dtor_or_conv_p,
20003 bool member_p, bool friend_p)
20006 cp_declarator *declarator = NULL;
20007 tree scope = NULL_TREE;
20008 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20009 bool saved_in_declarator_p = parser->in_declarator_p;
20011 tree pushed_scope = NULL_TREE;
20012 cp_token *open_paren = NULL, *close_paren = NULL;
20016 /* Peek at the next token. */
20017 token = cp_lexer_peek_token (parser->lexer);
20018 if (token->type == CPP_OPEN_PAREN)
20020 /* This is either a parameter-declaration-clause, or a
20021 parenthesized declarator. When we know we are parsing a
20022 named declarator, it must be a parenthesized declarator
20023 if FIRST is true. For instance, `(int)' is a
20024 parameter-declaration-clause, with an omitted
20025 direct-abstract-declarator. But `((*))', is a
20026 parenthesized abstract declarator. Finally, when T is a
20027 template parameter `(T)' is a
20028 parameter-declaration-clause, and not a parenthesized
20031 We first try and parse a parameter-declaration-clause,
20032 and then try a nested declarator (if FIRST is true).
20034 It is not an error for it not to be a
20035 parameter-declaration-clause, even when FIRST is
20041 The first is the declaration of a function while the
20042 second is the definition of a variable, including its
20045 Having seen only the parenthesis, we cannot know which of
20046 these two alternatives should be selected. Even more
20047 complex are examples like:
20052 The former is a function-declaration; the latter is a
20053 variable initialization.
20055 Thus again, we try a parameter-declaration-clause, and if
20056 that fails, we back out and return. */
20058 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20061 bool is_declarator = false;
20065 /* In a member-declarator, the only valid interpretation
20066 of a parenthesis is the start of a
20067 parameter-declaration-clause. (It is invalid to
20068 initialize a static data member with a parenthesized
20069 initializer; only the "=" form of initialization is
20072 cp_parser_parse_tentatively (parser);
20074 /* Consume the `('. */
20075 matching_parens parens;
20076 parens.consume_open (parser);
20079 /* If this is going to be an abstract declarator, we're
20080 in a declarator and we can't have default args. */
20081 parser->default_arg_ok_p = false;
20082 parser->in_declarator_p = true;
20085 begin_scope (sk_function_parms, NULL_TREE);
20087 /* Parse the parameter-declaration-clause. */
20088 params = cp_parser_parameter_declaration_clause (parser);
20090 /* Consume the `)'. */
20091 parens.require_close (parser);
20093 /* If all went well, parse the cv-qualifier-seq,
20094 ref-qualifier and the exception-specification. */
20095 if (member_p || cp_parser_parse_definitely (parser))
20097 cp_cv_quals cv_quals;
20098 cp_virt_specifiers virt_specifiers;
20099 cp_ref_qualifier ref_qual;
20100 tree exception_specification;
20103 bool memfn = (member_p || (pushed_scope
20104 && CLASS_TYPE_P (pushed_scope)));
20106 is_declarator = true;
20108 if (ctor_dtor_or_conv_p)
20109 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20112 /* Parse the cv-qualifier-seq. */
20113 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20114 /* Parse the ref-qualifier. */
20115 ref_qual = cp_parser_ref_qualifier_opt (parser);
20116 /* Parse the tx-qualifier. */
20117 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20118 /* And the exception-specification. */
20119 exception_specification
20120 = cp_parser_exception_specification_opt (parser);
20122 attrs = cp_parser_std_attribute_spec_seq (parser);
20124 /* In here, we handle cases where attribute is used after
20125 the function declaration. For example:
20126 void func (int x) __attribute__((vector(..))); */
20127 tree gnu_attrs = NULL_TREE;
20128 tree requires_clause = NULL_TREE;
20129 late_return = (cp_parser_late_return_type_opt
20130 (parser, declarator, requires_clause,
20131 memfn ? cv_quals : -1));
20133 /* Parse the virt-specifier-seq. */
20134 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20136 /* Create the function-declarator. */
20137 declarator = make_call_declarator (declarator,
20143 exception_specification,
20146 declarator->std_attributes = attrs;
20147 declarator->attributes = gnu_attrs;
20148 /* Any subsequent parameter lists are to do with
20149 return type, so are not those of the declared
20151 parser->default_arg_ok_p = false;
20154 /* Remove the function parms from scope. */
20155 pop_bindings_and_leave_scope ();
20158 /* Repeat the main loop. */
20162 /* If this is the first, we can try a parenthesized
20166 bool saved_in_type_id_in_expr_p;
20168 parser->default_arg_ok_p = saved_default_arg_ok_p;
20169 parser->in_declarator_p = saved_in_declarator_p;
20171 open_paren = token;
20172 /* Consume the `('. */
20173 matching_parens parens;
20174 parens.consume_open (parser);
20175 /* Parse the nested declarator. */
20176 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20177 parser->in_type_id_in_expr_p = true;
20179 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20180 /*parenthesized_p=*/NULL,
20181 member_p, friend_p);
20182 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20184 /* Expect a `)'. */
20185 close_paren = cp_lexer_peek_token (parser->lexer);
20186 if (!parens.require_close (parser))
20187 declarator = cp_error_declarator;
20188 if (declarator == cp_error_declarator)
20191 goto handle_declarator;
20193 /* Otherwise, we must be done. */
20197 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20198 && token->type == CPP_OPEN_SQUARE
20199 && !cp_next_tokens_can_be_attribute_p (parser))
20201 /* Parse an array-declarator. */
20202 tree bounds, attrs;
20204 if (ctor_dtor_or_conv_p)
20205 *ctor_dtor_or_conv_p = 0;
20209 parser->default_arg_ok_p = false;
20210 parser->in_declarator_p = true;
20211 /* Consume the `['. */
20212 cp_lexer_consume_token (parser->lexer);
20213 /* Peek at the next token. */
20214 token = cp_lexer_peek_token (parser->lexer);
20215 /* If the next token is `]', then there is no
20216 constant-expression. */
20217 if (token->type != CPP_CLOSE_SQUARE)
20219 bool non_constant_p;
20221 = cp_parser_constant_expression (parser,
20222 /*allow_non_constant=*/true,
20224 if (!non_constant_p)
20226 else if (error_operand_p (bounds))
20227 /* Already gave an error. */;
20228 else if (!parser->in_function_body
20229 || current_binding_level->kind == sk_function_parms)
20231 /* Normally, the array bound must be an integral constant
20232 expression. However, as an extension, we allow VLAs
20233 in function scopes as long as they aren't part of a
20234 parameter declaration. */
20235 cp_parser_error (parser,
20236 "array bound is not an integer constant");
20237 bounds = error_mark_node;
20239 else if (processing_template_decl
20240 && !type_dependent_expression_p (bounds))
20242 /* Remember this wasn't a constant-expression. */
20243 bounds = build_nop (TREE_TYPE (bounds), bounds);
20244 TREE_SIDE_EFFECTS (bounds) = 1;
20248 bounds = NULL_TREE;
20249 /* Look for the closing `]'. */
20250 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20252 declarator = cp_error_declarator;
20256 attrs = cp_parser_std_attribute_spec_seq (parser);
20257 declarator = make_array_declarator (declarator, bounds);
20258 declarator->std_attributes = attrs;
20260 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20263 tree qualifying_scope;
20264 tree unqualified_name;
20266 special_function_kind sfk;
20268 bool pack_expansion_p = false;
20269 cp_token *declarator_id_start_token;
20271 /* Parse a declarator-id */
20272 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20275 cp_parser_parse_tentatively (parser);
20277 /* If we see an ellipsis, we should be looking at a
20279 if (token->type == CPP_ELLIPSIS)
20281 /* Consume the `...' */
20282 cp_lexer_consume_token (parser->lexer);
20284 pack_expansion_p = true;
20288 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20290 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20291 qualifying_scope = parser->scope;
20296 if (!unqualified_name && pack_expansion_p)
20298 /* Check whether an error occurred. */
20299 okay = !cp_parser_error_occurred (parser);
20301 /* We already consumed the ellipsis to mark a
20302 parameter pack, but we have no way to report it,
20303 so abort the tentative parse. We will be exiting
20304 immediately anyway. */
20305 cp_parser_abort_tentative_parse (parser);
20308 okay = cp_parser_parse_definitely (parser);
20311 unqualified_name = error_mark_node;
20312 else if (unqualified_name
20313 && (qualifying_scope
20314 || (!identifier_p (unqualified_name))))
20316 cp_parser_error (parser, "expected unqualified-id");
20317 unqualified_name = error_mark_node;
20321 if (!unqualified_name)
20323 if (unqualified_name == error_mark_node)
20325 declarator = cp_error_declarator;
20326 pack_expansion_p = false;
20327 declarator->parameter_pack_p = false;
20331 attrs = cp_parser_std_attribute_spec_seq (parser);
20333 if (qualifying_scope && at_namespace_scope_p ()
20334 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20336 /* In the declaration of a member of a template class
20337 outside of the class itself, the SCOPE will sometimes
20338 be a TYPENAME_TYPE. For example, given:
20340 template <typename T>
20341 int S<T>::R::i = 3;
20343 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20344 this context, we must resolve S<T>::R to an ordinary
20345 type, rather than a typename type.
20347 The reason we normally avoid resolving TYPENAME_TYPEs
20348 is that a specialization of `S' might render
20349 `S<T>::R' not a type. However, if `S' is
20350 specialized, then this `i' will not be used, so there
20351 is no harm in resolving the types here. */
20354 /* Resolve the TYPENAME_TYPE. */
20355 type = resolve_typename_type (qualifying_scope,
20356 /*only_current_p=*/false);
20357 /* If that failed, the declarator is invalid. */
20358 if (TREE_CODE (type) == TYPENAME_TYPE)
20360 if (typedef_variant_p (type))
20361 error_at (declarator_id_start_token->location,
20362 "cannot define member of dependent typedef "
20365 error_at (declarator_id_start_token->location,
20366 "%<%T::%E%> is not a type",
20367 TYPE_CONTEXT (qualifying_scope),
20368 TYPE_IDENTIFIER (qualifying_scope));
20370 qualifying_scope = type;
20375 if (unqualified_name)
20379 if (qualifying_scope
20380 && CLASS_TYPE_P (qualifying_scope))
20381 class_type = qualifying_scope;
20383 class_type = current_class_type;
20385 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20387 tree name_type = TREE_TYPE (unqualified_name);
20389 if (!class_type || !same_type_p (name_type, class_type))
20391 /* We do not attempt to print the declarator
20392 here because we do not have enough
20393 information about its original syntactic
20395 cp_parser_error (parser, "invalid declarator");
20396 declarator = cp_error_declarator;
20399 else if (qualifying_scope
20400 && CLASSTYPE_USE_TEMPLATE (name_type))
20402 error_at (declarator_id_start_token->location,
20403 "invalid use of constructor as a template");
20404 inform (declarator_id_start_token->location,
20405 "use %<%T::%D%> instead of %<%T::%D%> to "
20406 "name the constructor in a qualified name",
20408 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20409 class_type, name_type);
20410 declarator = cp_error_declarator;
20413 unqualified_name = constructor_name (class_type);
20418 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20419 sfk = sfk_destructor;
20420 else if (identifier_p (unqualified_name)
20421 && IDENTIFIER_CONV_OP_P (unqualified_name))
20422 sfk = sfk_conversion;
20423 else if (/* There's no way to declare a constructor
20424 for an unnamed type, even if the type
20425 got a name for linkage purposes. */
20426 !TYPE_WAS_UNNAMED (class_type)
20427 /* Handle correctly (c++/19200):
20441 friend void N::S();
20443 && (!friend_p || class_type == qualifying_scope)
20444 && constructor_name_p (unqualified_name,
20446 sfk = sfk_constructor;
20447 else if (is_overloaded_fn (unqualified_name)
20448 && DECL_CONSTRUCTOR_P (get_first_fn
20449 (unqualified_name)))
20450 sfk = sfk_constructor;
20452 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20453 *ctor_dtor_or_conv_p = -1;
20456 declarator = make_id_declarator (qualifying_scope,
20459 declarator->std_attributes = attrs;
20460 declarator->id_loc = token->location;
20461 declarator->parameter_pack_p = pack_expansion_p;
20463 if (pack_expansion_p)
20464 maybe_warn_variadic_templates ();
20467 handle_declarator:;
20468 scope = get_scope_of_declarator (declarator);
20471 /* Any names that appear after the declarator-id for a
20472 member are looked up in the containing scope. */
20473 if (at_function_scope_p ())
20475 /* But declarations with qualified-ids can't appear in a
20477 cp_parser_error (parser, "qualified-id in declaration");
20478 declarator = cp_error_declarator;
20481 pushed_scope = push_scope (scope);
20483 parser->in_declarator_p = true;
20484 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20485 || (declarator && declarator->kind == cdk_id))
20486 /* Default args are only allowed on function
20488 parser->default_arg_ok_p = saved_default_arg_ok_p;
20490 parser->default_arg_ok_p = false;
20499 /* For an abstract declarator, we might wind up with nothing at this
20500 point. That's an error; the declarator is not optional. */
20502 cp_parser_error (parser, "expected declarator");
20503 else if (open_paren)
20505 /* Record overly parenthesized declarator so we can give a
20506 diagnostic about confusing decl/expr disambiguation. */
20507 if (declarator->kind == cdk_array)
20509 /* If the open and close parens are on different lines, this
20510 is probably a formatting thing, so ignore. */
20511 expanded_location open = expand_location (open_paren->location);
20512 expanded_location close = expand_location (close_paren->location);
20513 if (open.line != close.line || open.file != close.file)
20517 declarator->parenthesized = open_paren->location;
20520 /* If we entered a scope, we must exit it now. */
20522 pop_scope (pushed_scope);
20524 parser->default_arg_ok_p = saved_default_arg_ok_p;
20525 parser->in_declarator_p = saved_in_declarator_p;
20530 /* Parse a ptr-operator.
20533 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20534 * cv-qualifier-seq [opt]
20536 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20537 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20542 & cv-qualifier-seq [opt]
20544 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20545 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20546 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20547 filled in with the TYPE containing the member. *CV_QUALS is
20548 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20549 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20550 Note that the tree codes returned by this function have nothing
20551 to do with the types of trees that will be eventually be created
20552 to represent the pointer or reference type being parsed. They are
20553 just constants with suggestive names. */
20554 static enum tree_code
20555 cp_parser_ptr_operator (cp_parser* parser,
20557 cp_cv_quals *cv_quals,
20560 enum tree_code code = ERROR_MARK;
20562 tree attrs = NULL_TREE;
20564 /* Assume that it's not a pointer-to-member. */
20566 /* And that there are no cv-qualifiers. */
20567 *cv_quals = TYPE_UNQUALIFIED;
20569 /* Peek at the next token. */
20570 token = cp_lexer_peek_token (parser->lexer);
20572 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20573 if (token->type == CPP_MULT)
20574 code = INDIRECT_REF;
20575 else if (token->type == CPP_AND)
20577 else if ((cxx_dialect != cxx98) &&
20578 token->type == CPP_AND_AND) /* C++0x only */
20579 code = NON_LVALUE_EXPR;
20581 if (code != ERROR_MARK)
20583 /* Consume the `*', `&' or `&&'. */
20584 cp_lexer_consume_token (parser->lexer);
20586 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20587 `&', if we are allowing GNU extensions. (The only qualifier
20588 that can legally appear after `&' is `restrict', but that is
20589 enforced during semantic analysis. */
20590 if (code == INDIRECT_REF
20591 || cp_parser_allow_gnu_extensions_p (parser))
20592 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20594 attrs = cp_parser_std_attribute_spec_seq (parser);
20595 if (attributes != NULL)
20596 *attributes = attrs;
20600 /* Try the pointer-to-member case. */
20601 cp_parser_parse_tentatively (parser);
20602 /* Look for the optional `::' operator. */
20603 cp_parser_global_scope_opt (parser,
20604 /*current_scope_valid_p=*/false);
20605 /* Look for the nested-name specifier. */
20606 token = cp_lexer_peek_token (parser->lexer);
20607 cp_parser_nested_name_specifier (parser,
20608 /*typename_keyword_p=*/false,
20609 /*check_dependency_p=*/true,
20611 /*is_declaration=*/false);
20612 /* If we found it, and the next token is a `*', then we are
20613 indeed looking at a pointer-to-member operator. */
20614 if (!cp_parser_error_occurred (parser)
20615 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20617 /* Indicate that the `*' operator was used. */
20618 code = INDIRECT_REF;
20620 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20621 error_at (token->location, "%qD is a namespace", parser->scope);
20622 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20623 error_at (token->location, "cannot form pointer to member of "
20624 "non-class %q#T", parser->scope);
20627 /* The type of which the member is a member is given by the
20629 *type = parser->scope;
20630 /* The next name will not be qualified. */
20631 parser->scope = NULL_TREE;
20632 parser->qualifying_scope = NULL_TREE;
20633 parser->object_scope = NULL_TREE;
20634 /* Look for optional c++11 attributes. */
20635 attrs = cp_parser_std_attribute_spec_seq (parser);
20636 if (attributes != NULL)
20637 *attributes = attrs;
20638 /* Look for the optional cv-qualifier-seq. */
20639 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20642 /* If that didn't work we don't have a ptr-operator. */
20643 if (!cp_parser_parse_definitely (parser))
20644 cp_parser_error (parser, "expected ptr-operator");
20650 /* Parse an (optional) cv-qualifier-seq.
20653 cv-qualifier cv-qualifier-seq [opt]
20664 Returns a bitmask representing the cv-qualifiers. */
20667 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20669 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20674 cp_cv_quals cv_qualifier;
20676 /* Peek at the next token. */
20677 token = cp_lexer_peek_token (parser->lexer);
20678 /* See if it's a cv-qualifier. */
20679 switch (token->keyword)
20682 cv_qualifier = TYPE_QUAL_CONST;
20686 cv_qualifier = TYPE_QUAL_VOLATILE;
20690 cv_qualifier = TYPE_QUAL_RESTRICT;
20694 cv_qualifier = TYPE_UNQUALIFIED;
20701 if (cv_quals & cv_qualifier)
20703 gcc_rich_location richloc (token->location);
20704 richloc.add_fixit_remove ();
20705 error_at (&richloc, "duplicate cv-qualifier");
20706 cp_lexer_purge_token (parser->lexer);
20710 cp_lexer_consume_token (parser->lexer);
20711 cv_quals |= cv_qualifier;
20718 /* Parse an (optional) ref-qualifier
20724 Returns cp_ref_qualifier representing ref-qualifier. */
20726 static cp_ref_qualifier
20727 cp_parser_ref_qualifier_opt (cp_parser* parser)
20729 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20731 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20732 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20737 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20738 cp_token *token = cp_lexer_peek_token (parser->lexer);
20740 switch (token->type)
20743 curr_ref_qual = REF_QUAL_LVALUE;
20747 curr_ref_qual = REF_QUAL_RVALUE;
20751 curr_ref_qual = REF_QUAL_NONE;
20755 if (!curr_ref_qual)
20759 error_at (token->location, "multiple ref-qualifiers");
20760 cp_lexer_purge_token (parser->lexer);
20764 ref_qual = curr_ref_qual;
20765 cp_lexer_consume_token (parser->lexer);
20772 /* Parse an optional tx-qualifier.
20776 transaction_safe_dynamic */
20779 cp_parser_tx_qualifier_opt (cp_parser *parser)
20781 cp_token *token = cp_lexer_peek_token (parser->lexer);
20782 if (token->type == CPP_NAME)
20784 tree name = token->u.value;
20785 const char *p = IDENTIFIER_POINTER (name);
20786 const int len = strlen ("transaction_safe");
20787 if (!strncmp (p, "transaction_safe", len))
20791 || !strcmp (p, "_dynamic"))
20793 cp_lexer_consume_token (parser->lexer);
20796 error ("%qE requires %<-fgnu-tm%>", name);
20807 /* Parse an (optional) virt-specifier-seq.
20809 virt-specifier-seq:
20810 virt-specifier virt-specifier-seq [opt]
20816 Returns a bitmask representing the virt-specifiers. */
20818 static cp_virt_specifiers
20819 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20821 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20826 cp_virt_specifiers virt_specifier;
20828 /* Peek at the next token. */
20829 token = cp_lexer_peek_token (parser->lexer);
20830 /* See if it's a virt-specifier-qualifier. */
20831 if (token->type != CPP_NAME)
20833 if (id_equal (token->u.value, "override"))
20835 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20836 virt_specifier = VIRT_SPEC_OVERRIDE;
20838 else if (id_equal (token->u.value, "final"))
20840 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20841 virt_specifier = VIRT_SPEC_FINAL;
20843 else if (id_equal (token->u.value, "__final"))
20845 virt_specifier = VIRT_SPEC_FINAL;
20850 if (virt_specifiers & virt_specifier)
20852 gcc_rich_location richloc (token->location);
20853 richloc.add_fixit_remove ();
20854 error_at (&richloc, "duplicate virt-specifier");
20855 cp_lexer_purge_token (parser->lexer);
20859 cp_lexer_consume_token (parser->lexer);
20860 virt_specifiers |= virt_specifier;
20863 return virt_specifiers;
20866 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20867 is in scope even though it isn't real. */
20870 inject_this_parameter (tree ctype, cp_cv_quals quals)
20874 if (current_class_ptr)
20876 /* We don't clear this between NSDMIs. Is it already what we want? */
20877 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20878 if (DECL_P (current_class_ptr)
20879 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20880 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20881 && cp_type_quals (type) == quals)
20885 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20886 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20887 current_class_ptr = NULL_TREE;
20889 = cp_build_fold_indirect_ref (this_parm);
20890 current_class_ptr = this_parm;
20893 /* Return true iff our current scope is a non-static data member
20897 parsing_nsdmi (void)
20899 /* We recognize NSDMI context by the context-less 'this' pointer set up
20900 by the function above. */
20901 if (current_class_ptr
20902 && TREE_CODE (current_class_ptr) == PARM_DECL
20903 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20908 /* Parse a late-specified return type, if any. This is not a separate
20909 non-terminal, but part of a function declarator, which looks like
20911 -> trailing-type-specifier-seq abstract-declarator(opt)
20913 Returns the type indicated by the type-id.
20915 In addition to this, parse any queued up #pragma omp declare simd
20916 clauses, and #pragma acc routine clauses.
20918 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20922 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20923 tree& requires_clause, cp_cv_quals quals)
20926 tree type = NULL_TREE;
20927 bool declare_simd_p = (parser->omp_declare_simd
20929 && declarator->kind == cdk_id);
20931 bool oacc_routine_p = (parser->oacc_routine
20933 && declarator->kind == cdk_id);
20935 /* Peek at the next token. */
20936 token = cp_lexer_peek_token (parser->lexer);
20937 /* A late-specified return type is indicated by an initial '->'. */
20938 if (token->type != CPP_DEREF
20939 && token->keyword != RID_REQUIRES
20940 && !(token->type == CPP_NAME
20941 && token->u.value == ridpointers[RID_REQUIRES])
20942 && !(declare_simd_p || oacc_routine_p))
20945 tree save_ccp = current_class_ptr;
20946 tree save_ccr = current_class_ref;
20949 /* DR 1207: 'this' is in scope in the trailing return type. */
20950 inject_this_parameter (current_class_type, quals);
20953 if (token->type == CPP_DEREF)
20955 /* Consume the ->. */
20956 cp_lexer_consume_token (parser->lexer);
20958 type = cp_parser_trailing_type_id (parser);
20961 /* Function declarations may be followed by a trailing
20962 requires-clause. */
20963 requires_clause = cp_parser_requires_clause_opt (parser);
20965 if (declare_simd_p)
20966 declarator->attributes
20967 = cp_parser_late_parsing_omp_declare_simd (parser,
20968 declarator->attributes);
20969 if (oacc_routine_p)
20970 declarator->attributes
20971 = cp_parser_late_parsing_oacc_routine (parser,
20972 declarator->attributes);
20976 current_class_ptr = save_ccp;
20977 current_class_ref = save_ccr;
20983 /* Parse a declarator-id.
20987 :: [opt] nested-name-specifier [opt] type-name
20989 In the `id-expression' case, the value returned is as for
20990 cp_parser_id_expression if the id-expression was an unqualified-id.
20991 If the id-expression was a qualified-id, then a SCOPE_REF is
20992 returned. The first operand is the scope (either a NAMESPACE_DECL
20993 or TREE_TYPE), but the second is still just a representation of an
20997 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21000 /* The expression must be an id-expression. Assume that qualified
21001 names are the names of types so that:
21004 int S<T>::R::i = 3;
21006 will work; we must treat `S<T>::R' as the name of a type.
21007 Similarly, assume that qualified names are templates, where
21011 int S<T>::R<T>::i = 3;
21014 id = cp_parser_id_expression (parser,
21015 /*template_keyword_p=*/false,
21016 /*check_dependency_p=*/false,
21017 /*template_p=*/NULL,
21018 /*declarator_p=*/true,
21020 if (id && BASELINK_P (id))
21021 id = BASELINK_FUNCTIONS (id);
21025 /* Parse a type-id.
21028 type-specifier-seq abstract-declarator [opt]
21030 Returns the TYPE specified. */
21033 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21034 bool is_trailing_return)
21036 cp_decl_specifier_seq type_specifier_seq;
21037 cp_declarator *abstract_declarator;
21039 /* Parse the type-specifier-seq. */
21040 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21041 is_trailing_return,
21042 &type_specifier_seq);
21043 if (is_template_arg && type_specifier_seq.type
21044 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21045 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21046 /* A bare template name as a template argument is a template template
21047 argument, not a placeholder, so fail parsing it as a type argument. */
21049 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21050 cp_parser_simulate_error (parser);
21051 return error_mark_node;
21053 if (type_specifier_seq.type == error_mark_node)
21054 return error_mark_node;
21056 /* There might or might not be an abstract declarator. */
21057 cp_parser_parse_tentatively (parser);
21058 /* Look for the declarator. */
21059 abstract_declarator
21060 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21061 /*parenthesized_p=*/NULL,
21062 /*member_p=*/false,
21063 /*friend_p=*/false);
21064 /* Check to see if there really was a declarator. */
21065 if (!cp_parser_parse_definitely (parser))
21066 abstract_declarator = NULL;
21068 if (type_specifier_seq.type
21069 /* The concepts TS allows 'auto' as a type-id. */
21070 && (!flag_concepts || parser->in_type_id_in_expr_p)
21071 /* None of the valid uses of 'auto' in C++14 involve the type-id
21072 nonterminal, but it is valid in a trailing-return-type. */
21073 && !(cxx_dialect >= cxx14 && is_trailing_return))
21074 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21076 /* A type-id with type 'auto' is only ok if the abstract declarator
21077 is a function declarator with a late-specified return type.
21079 A type-id with 'auto' is also valid in a trailing-return-type
21080 in a compound-requirement. */
21081 if (abstract_declarator
21082 && abstract_declarator->kind == cdk_function
21083 && abstract_declarator->u.function.late_return_type)
21085 else if (parser->in_result_type_constraint_p)
21089 location_t loc = type_specifier_seq.locations[ds_type_spec];
21090 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21092 error_at (loc, "missing template arguments after %qT",
21094 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21098 error_at (loc, "invalid use of %qT", auto_node);
21099 return error_mark_node;
21103 return groktypename (&type_specifier_seq, abstract_declarator,
21108 cp_parser_type_id (cp_parser *parser)
21110 return cp_parser_type_id_1 (parser, false, false);
21114 cp_parser_template_type_arg (cp_parser *parser)
21117 const char *saved_message = parser->type_definition_forbidden_message;
21118 parser->type_definition_forbidden_message
21119 = G_("types may not be defined in template arguments");
21120 r = cp_parser_type_id_1 (parser, true, false);
21121 parser->type_definition_forbidden_message = saved_message;
21122 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21124 error ("invalid use of %<auto%> in template argument");
21125 r = error_mark_node;
21131 cp_parser_trailing_type_id (cp_parser *parser)
21133 return cp_parser_type_id_1 (parser, false, true);
21136 /* Parse a type-specifier-seq.
21138 type-specifier-seq:
21139 type-specifier type-specifier-seq [opt]
21143 type-specifier-seq:
21144 attributes type-specifier-seq [opt]
21146 If IS_DECLARATION is true, we are at the start of a "condition" or
21147 exception-declaration, so we might be followed by a declarator-id.
21149 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21150 i.e. we've just seen "->".
21152 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21155 cp_parser_type_specifier_seq (cp_parser* parser,
21156 bool is_declaration,
21157 bool is_trailing_return,
21158 cp_decl_specifier_seq *type_specifier_seq)
21160 bool seen_type_specifier = false;
21161 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21162 cp_token *start_token = NULL;
21164 /* Clear the TYPE_SPECIFIER_SEQ. */
21165 clear_decl_specs (type_specifier_seq);
21167 /* In the context of a trailing return type, enum E { } is an
21168 elaborated-type-specifier followed by a function-body, not an
21170 if (is_trailing_return)
21171 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21173 /* Parse the type-specifiers and attributes. */
21176 tree type_specifier;
21177 bool is_cv_qualifier;
21179 /* Check for attributes first. */
21180 if (cp_next_tokens_can_be_attribute_p (parser))
21182 type_specifier_seq->attributes
21183 = attr_chainon (type_specifier_seq->attributes,
21184 cp_parser_attributes_opt (parser));
21188 /* record the token of the beginning of the type specifier seq,
21189 for error reporting purposes*/
21191 start_token = cp_lexer_peek_token (parser->lexer);
21193 /* Look for the type-specifier. */
21194 type_specifier = cp_parser_type_specifier (parser,
21196 type_specifier_seq,
21197 /*is_declaration=*/false,
21200 if (!type_specifier)
21202 /* If the first type-specifier could not be found, this is not a
21203 type-specifier-seq at all. */
21204 if (!seen_type_specifier)
21206 /* Set in_declarator_p to avoid skipping to the semicolon. */
21207 int in_decl = parser->in_declarator_p;
21208 parser->in_declarator_p = true;
21210 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21211 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21212 cp_parser_error (parser, "expected type-specifier");
21214 parser->in_declarator_p = in_decl;
21216 type_specifier_seq->type = error_mark_node;
21219 /* If subsequent type-specifiers could not be found, the
21220 type-specifier-seq is complete. */
21224 seen_type_specifier = true;
21225 /* The standard says that a condition can be:
21227 type-specifier-seq declarator = assignment-expression
21234 we should treat the "S" as a declarator, not as a
21235 type-specifier. The standard doesn't say that explicitly for
21236 type-specifier-seq, but it does say that for
21237 decl-specifier-seq in an ordinary declaration. Perhaps it
21238 would be clearer just to allow a decl-specifier-seq here, and
21239 then add a semantic restriction that if any decl-specifiers
21240 that are not type-specifiers appear, the program is invalid. */
21241 if (is_declaration && !is_cv_qualifier)
21242 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21246 /* Return whether the function currently being declared has an associated
21247 template parameter list. */
21250 function_being_declared_is_template_p (cp_parser* parser)
21252 if (!current_template_parms || processing_template_parmlist)
21255 if (parser->implicit_template_scope)
21258 if (at_class_scope_p ()
21259 && TYPE_BEING_DEFINED (current_class_type))
21260 return parser->num_template_parameter_lists != 0;
21262 return ((int) parser->num_template_parameter_lists > template_class_depth
21263 (current_class_type));
21266 /* Parse a parameter-declaration-clause.
21268 parameter-declaration-clause:
21269 parameter-declaration-list [opt] ... [opt]
21270 parameter-declaration-list , ...
21272 Returns a representation for the parameter declarations. A return
21273 value of NULL indicates a parameter-declaration-clause consisting
21274 only of an ellipsis. */
21277 cp_parser_parameter_declaration_clause (cp_parser* parser)
21284 temp_override<bool> cleanup
21285 (parser->auto_is_implicit_function_template_parm_p);
21287 if (!processing_specialization
21288 && !processing_template_parmlist
21289 && !processing_explicit_instantiation
21290 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21291 actual function or a random abstract declarator. */
21292 && parser->default_arg_ok_p)
21293 if (!current_function_decl
21294 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21295 parser->auto_is_implicit_function_template_parm_p = true;
21297 /* Peek at the next token. */
21298 token = cp_lexer_peek_token (parser->lexer);
21299 /* Check for trivial parameter-declaration-clauses. */
21300 if (token->type == CPP_ELLIPSIS)
21302 /* Consume the `...' token. */
21303 cp_lexer_consume_token (parser->lexer);
21306 else if (token->type == CPP_CLOSE_PAREN)
21307 /* There are no parameters. */
21309 #ifndef NO_IMPLICIT_EXTERN_C
21310 if (in_system_header_at (input_location)
21311 && current_class_type == NULL
21312 && current_lang_name == lang_name_c)
21316 return void_list_node;
21318 /* Check for `(void)', too, which is a special case. */
21319 else if (token->keyword == RID_VOID
21320 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21321 == CPP_CLOSE_PAREN))
21323 /* Consume the `void' token. */
21324 cp_lexer_consume_token (parser->lexer);
21325 /* There are no parameters. */
21326 return void_list_node;
21329 /* Parse the parameter-declaration-list. */
21330 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21331 /* If a parse error occurred while parsing the
21332 parameter-declaration-list, then the entire
21333 parameter-declaration-clause is erroneous. */
21337 /* Peek at the next token. */
21338 token = cp_lexer_peek_token (parser->lexer);
21339 /* If it's a `,', the clause should terminate with an ellipsis. */
21340 if (token->type == CPP_COMMA)
21342 /* Consume the `,'. */
21343 cp_lexer_consume_token (parser->lexer);
21344 /* Expect an ellipsis. */
21346 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21348 /* It might also be `...' if the optional trailing `,' was
21350 else if (token->type == CPP_ELLIPSIS)
21352 /* Consume the `...' token. */
21353 cp_lexer_consume_token (parser->lexer);
21354 /* And remember that we saw it. */
21358 ellipsis_p = false;
21360 /* Finish the parameter list. */
21362 parameters = chainon (parameters, void_list_node);
21367 /* Parse a parameter-declaration-list.
21369 parameter-declaration-list:
21370 parameter-declaration
21371 parameter-declaration-list , parameter-declaration
21373 Returns a representation of the parameter-declaration-list, as for
21374 cp_parser_parameter_declaration_clause. However, the
21375 `void_list_node' is never appended to the list. Upon return,
21376 *IS_ERROR will be true iff an error occurred. */
21379 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21381 tree parameters = NULL_TREE;
21382 tree *tail = ¶meters;
21383 bool saved_in_unbraced_linkage_specification_p;
21386 /* Assume all will go well. */
21388 /* The special considerations that apply to a function within an
21389 unbraced linkage specifications do not apply to the parameters
21390 to the function. */
21391 saved_in_unbraced_linkage_specification_p
21392 = parser->in_unbraced_linkage_specification_p;
21393 parser->in_unbraced_linkage_specification_p = false;
21395 /* Look for more parameters. */
21398 cp_parameter_declarator *parameter;
21399 tree decl = error_mark_node;
21400 bool parenthesized_p = false;
21402 /* Parse the parameter. */
21404 = cp_parser_parameter_declaration (parser,
21405 /*template_parm_p=*/false,
21408 /* We don't know yet if the enclosing context is deprecated, so wait
21409 and warn in grokparms if appropriate. */
21410 deprecated_state = DEPRECATED_SUPPRESS;
21414 decl = grokdeclarator (parameter->declarator,
21415 ¶meter->decl_specifiers,
21417 parameter->default_argument != NULL_TREE,
21418 ¶meter->decl_specifiers.attributes);
21419 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21420 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21423 deprecated_state = DEPRECATED_NORMAL;
21425 /* If a parse error occurred parsing the parameter declaration,
21426 then the entire parameter-declaration-list is erroneous. */
21427 if (decl == error_mark_node)
21430 parameters = error_mark_node;
21434 if (parameter->decl_specifiers.attributes)
21435 cplus_decl_attributes (&decl,
21436 parameter->decl_specifiers.attributes,
21438 if (DECL_NAME (decl))
21439 decl = pushdecl (decl);
21441 if (decl != error_mark_node)
21443 retrofit_lang_decl (decl);
21444 DECL_PARM_INDEX (decl) = ++index;
21445 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21448 /* Add the new parameter to the list. */
21449 *tail = build_tree_list (parameter->default_argument, decl);
21450 tail = &TREE_CHAIN (*tail);
21452 /* Peek at the next token. */
21453 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21454 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21455 /* These are for Objective-C++ */
21456 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21457 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21458 /* The parameter-declaration-list is complete. */
21460 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21464 /* Peek at the next token. */
21465 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21466 /* If it's an ellipsis, then the list is complete. */
21467 if (token->type == CPP_ELLIPSIS)
21469 /* Otherwise, there must be more parameters. Consume the
21471 cp_lexer_consume_token (parser->lexer);
21472 /* When parsing something like:
21474 int i(float f, double d)
21476 we can tell after seeing the declaration for "f" that we
21477 are not looking at an initialization of a variable "i",
21478 but rather at the declaration of a function "i".
21480 Due to the fact that the parsing of template arguments
21481 (as specified to a template-id) requires backtracking we
21482 cannot use this technique when inside a template argument
21484 if (!parser->in_template_argument_list_p
21485 && !parser->in_type_id_in_expr_p
21486 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21487 /* However, a parameter-declaration of the form
21488 "float(f)" (which is a valid declaration of a
21489 parameter "f") can also be interpreted as an
21490 expression (the conversion of "f" to "float"). */
21491 && !parenthesized_p)
21492 cp_parser_commit_to_tentative_parse (parser);
21496 cp_parser_error (parser, "expected %<,%> or %<...%>");
21497 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21498 cp_parser_skip_to_closing_parenthesis (parser,
21499 /*recovering=*/true,
21500 /*or_comma=*/false,
21501 /*consume_paren=*/false);
21506 parser->in_unbraced_linkage_specification_p
21507 = saved_in_unbraced_linkage_specification_p;
21509 /* Reset implicit_template_scope if we are about to leave the function
21510 parameter list that introduced it. Note that for out-of-line member
21511 definitions, there will be one or more class scopes before we get to
21512 the template parameter scope. */
21514 if (cp_binding_level *its = parser->implicit_template_scope)
21515 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21517 while (maybe_its->kind == sk_class)
21518 maybe_its = maybe_its->level_chain;
21519 if (maybe_its == its)
21521 parser->implicit_template_parms = 0;
21522 parser->implicit_template_scope = 0;
21529 /* Parse a parameter declaration.
21531 parameter-declaration:
21532 decl-specifier-seq ... [opt] declarator
21533 decl-specifier-seq declarator = assignment-expression
21534 decl-specifier-seq ... [opt] abstract-declarator [opt]
21535 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21537 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21538 declares a template parameter. (In that case, a non-nested `>'
21539 token encountered during the parsing of the assignment-expression
21540 is not interpreted as a greater-than operator.)
21542 Returns a representation of the parameter, or NULL if an error
21543 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21544 true iff the declarator is of the form "(p)". */
21546 static cp_parameter_declarator *
21547 cp_parser_parameter_declaration (cp_parser *parser,
21548 bool template_parm_p,
21549 bool *parenthesized_p)
21551 int declares_class_or_enum;
21552 cp_decl_specifier_seq decl_specifiers;
21553 cp_declarator *declarator;
21554 tree default_argument;
21555 cp_token *token = NULL, *declarator_token_start = NULL;
21556 const char *saved_message;
21557 bool template_parameter_pack_p = false;
21559 /* In a template parameter, `>' is not an operator.
21563 When parsing a default template-argument for a non-type
21564 template-parameter, the first non-nested `>' is taken as the end
21565 of the template parameter-list rather than a greater-than
21568 /* Type definitions may not appear in parameter types. */
21569 saved_message = parser->type_definition_forbidden_message;
21570 parser->type_definition_forbidden_message
21571 = G_("types may not be defined in parameter types");
21573 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21574 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21575 (current_template_parms)) : 0);
21577 /* Parse the declaration-specifiers. */
21578 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21579 cp_parser_decl_specifier_seq (parser,
21580 CP_PARSER_FLAGS_NONE,
21582 &declares_class_or_enum);
21584 /* Complain about missing 'typename' or other invalid type names. */
21585 if (!decl_specifiers.any_type_specifiers_p
21586 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21587 decl_specifiers.type = error_mark_node;
21589 /* If an error occurred, there's no reason to attempt to parse the
21590 rest of the declaration. */
21591 if (cp_parser_error_occurred (parser))
21593 parser->type_definition_forbidden_message = saved_message;
21597 /* Peek at the next token. */
21598 token = cp_lexer_peek_token (parser->lexer);
21600 /* If the next token is a `)', `,', `=', `>', or `...', then there
21601 is no declarator. However, when variadic templates are enabled,
21602 there may be a declarator following `...'. */
21603 if (token->type == CPP_CLOSE_PAREN
21604 || token->type == CPP_COMMA
21605 || token->type == CPP_EQ
21606 || token->type == CPP_GREATER)
21609 if (parenthesized_p)
21610 *parenthesized_p = false;
21612 /* Otherwise, there should be a declarator. */
21615 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21616 parser->default_arg_ok_p = false;
21618 /* After seeing a decl-specifier-seq, if the next token is not a
21619 "(", there is no possibility that the code is a valid
21620 expression. Therefore, if parsing tentatively, we commit at
21622 if (!parser->in_template_argument_list_p
21623 /* In an expression context, having seen:
21627 we cannot be sure whether we are looking at a
21628 function-type (taking a "char" as a parameter) or a cast
21629 of some object of type "char" to "int". */
21630 && !parser->in_type_id_in_expr_p
21631 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21632 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21633 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21634 cp_parser_commit_to_tentative_parse (parser);
21635 /* Parse the declarator. */
21636 declarator_token_start = token;
21637 declarator = cp_parser_declarator (parser,
21638 CP_PARSER_DECLARATOR_EITHER,
21639 /*ctor_dtor_or_conv_p=*/NULL,
21641 /*member_p=*/false,
21642 /*friend_p=*/false);
21643 parser->default_arg_ok_p = saved_default_arg_ok_p;
21644 /* After the declarator, allow more attributes. */
21645 decl_specifiers.attributes
21646 = attr_chainon (decl_specifiers.attributes,
21647 cp_parser_attributes_opt (parser));
21649 /* If the declarator is a template parameter pack, remember that and
21650 clear the flag in the declarator itself so we don't get errors
21651 from grokdeclarator. */
21652 if (template_parm_p && declarator && declarator->parameter_pack_p)
21654 declarator->parameter_pack_p = false;
21655 template_parameter_pack_p = true;
21659 /* If the next token is an ellipsis, and we have not seen a declarator
21660 name, and if either the type of the declarator contains parameter
21661 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21662 for, eg, abbreviated integral type names), then we actually have a
21663 parameter pack expansion expression. Otherwise, leave the ellipsis
21664 for a C-style variadic function. */
21665 token = cp_lexer_peek_token (parser->lexer);
21667 /* If a function parameter pack was specified and an implicit template
21668 parameter was introduced during cp_parser_parameter_declaration,
21669 change any implicit parameters introduced into packs. */
21670 if (parser->implicit_template_parms
21671 && ((token->type == CPP_ELLIPSIS
21672 && declarator_can_be_parameter_pack (declarator))
21673 || (declarator && declarator->parameter_pack_p)))
21675 int latest_template_parm_idx = TREE_VEC_LENGTH
21676 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21678 if (latest_template_parm_idx != template_parm_idx)
21679 decl_specifiers.type = convert_generic_types_to_packs
21680 (decl_specifiers.type,
21681 template_parm_idx, latest_template_parm_idx);
21684 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21686 tree type = decl_specifiers.type;
21688 if (type && DECL_P (type))
21689 type = TREE_TYPE (type);
21692 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21693 && (template_parm_p || uses_parameter_packs (type)))
21694 || (!type && template_parm_p))
21695 && declarator_can_be_parameter_pack (declarator))
21697 /* Consume the `...'. */
21698 cp_lexer_consume_token (parser->lexer);
21699 maybe_warn_variadic_templates ();
21701 /* Build a pack expansion type */
21702 if (template_parm_p)
21703 template_parameter_pack_p = true;
21704 else if (declarator)
21705 declarator->parameter_pack_p = true;
21707 decl_specifiers.type = make_pack_expansion (type);
21711 /* The restriction on defining new types applies only to the type
21712 of the parameter, not to the default argument. */
21713 parser->type_definition_forbidden_message = saved_message;
21715 /* If the next token is `=', then process a default argument. */
21716 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21718 tree type = decl_specifiers.type;
21719 token = cp_lexer_peek_token (parser->lexer);
21720 /* If we are defining a class, then the tokens that make up the
21721 default argument must be saved and processed later. */
21722 if (!template_parm_p && at_class_scope_p ()
21723 && TYPE_BEING_DEFINED (current_class_type)
21724 && !LAMBDA_TYPE_P (current_class_type))
21725 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21727 // A constrained-type-specifier may declare a type template-parameter.
21728 else if (declares_constrained_type_template_parameter (type))
21730 = cp_parser_default_type_template_argument (parser);
21732 // A constrained-type-specifier may declare a template-template-parameter.
21733 else if (declares_constrained_template_template_parameter (type))
21735 = cp_parser_default_template_template_argument (parser);
21737 /* Outside of a class definition, we can just parse the
21738 assignment-expression. */
21741 = cp_parser_default_argument (parser, template_parm_p);
21743 if (!parser->default_arg_ok_p)
21745 permerror (token->location,
21746 "default arguments are only "
21747 "permitted for function parameters");
21749 else if ((declarator && declarator->parameter_pack_p)
21750 || template_parameter_pack_p
21751 || (decl_specifiers.type
21752 && PACK_EXPANSION_P (decl_specifiers.type)))
21754 /* Find the name of the parameter pack. */
21755 cp_declarator *id_declarator = declarator;
21756 while (id_declarator && id_declarator->kind != cdk_id)
21757 id_declarator = id_declarator->declarator;
21759 if (id_declarator && id_declarator->kind == cdk_id)
21760 error_at (declarator_token_start->location,
21762 ? G_("template parameter pack %qD "
21763 "cannot have a default argument")
21764 : G_("parameter pack %qD cannot have "
21765 "a default argument"),
21766 id_declarator->u.id.unqualified_name);
21768 error_at (declarator_token_start->location,
21770 ? G_("template parameter pack cannot have "
21771 "a default argument")
21772 : G_("parameter pack cannot have a "
21773 "default argument"));
21775 default_argument = NULL_TREE;
21779 default_argument = NULL_TREE;
21781 /* Generate a location for the parameter, ranging from the start of the
21782 initial token to the end of the final token (using input_location for
21783 the latter, set up by cp_lexer_set_source_position_from_token when
21786 If we have a identifier, then use it for the caret location, e.g.
21788 extern int callee (int one, int (*two)(int, int), float three);
21789 ~~~~~~^~~~~~~~~~~~~~
21791 otherwise, reuse the start location for the caret location e.g.:
21793 extern int callee (int one, int (*)(int, int), float three);
21797 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21798 ? declarator->id_loc
21799 : decl_spec_token_start->location);
21800 location_t param_loc = make_location (caret_loc,
21801 decl_spec_token_start->location,
21804 return make_parameter_declarator (&decl_specifiers,
21808 template_parameter_pack_p);
21811 /* Parse a default argument and return it.
21813 TEMPLATE_PARM_P is true if this is a default argument for a
21814 non-type template parameter. */
21816 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21818 tree default_argument = NULL_TREE;
21819 bool saved_greater_than_is_operator_p;
21820 bool saved_local_variables_forbidden_p;
21821 bool non_constant_p, is_direct_init;
21823 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21825 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21826 parser->greater_than_is_operator_p = !template_parm_p;
21827 /* Local variable names (and the `this' keyword) may not
21828 appear in a default argument. */
21829 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21830 parser->local_variables_forbidden_p = true;
21831 /* Parse the assignment-expression. */
21832 if (template_parm_p)
21833 push_deferring_access_checks (dk_no_deferred);
21834 tree saved_class_ptr = NULL_TREE;
21835 tree saved_class_ref = NULL_TREE;
21836 /* The "this" pointer is not valid in a default argument. */
21839 saved_class_ptr = current_class_ptr;
21840 cp_function_chain->x_current_class_ptr = NULL_TREE;
21841 saved_class_ref = current_class_ref;
21842 cp_function_chain->x_current_class_ref = NULL_TREE;
21845 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21846 /* Restore the "this" pointer. */
21849 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21850 cp_function_chain->x_current_class_ref = saved_class_ref;
21852 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21853 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21854 if (template_parm_p)
21855 pop_deferring_access_checks ();
21856 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21857 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21859 return default_argument;
21862 /* Parse a function-body.
21865 compound_statement */
21868 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21870 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21871 ? BCS_TRY_BLOCK : BCS_NORMAL),
21875 /* Parse a ctor-initializer-opt followed by a function-body. Return
21876 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21877 is true we are parsing a function-try-block. */
21880 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21881 bool in_function_try_block)
21884 const bool check_body_p =
21885 DECL_CONSTRUCTOR_P (current_function_decl)
21886 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21889 /* Begin the function body. */
21890 body = begin_function_body ();
21891 /* Parse the optional ctor-initializer. */
21892 cp_parser_ctor_initializer_opt (parser);
21894 /* If we're parsing a constexpr constructor definition, we need
21895 to check that the constructor body is indeed empty. However,
21896 before we get to cp_parser_function_body lot of junk has been
21897 generated, so we can't just check that we have an empty block.
21898 Rather we take a snapshot of the outermost block, and check whether
21899 cp_parser_function_body changed its state. */
21902 list = cur_stmt_list;
21903 if (STATEMENT_LIST_TAIL (list))
21904 last = STATEMENT_LIST_TAIL (list)->stmt;
21906 /* Parse the function-body. */
21907 cp_parser_function_body (parser, in_function_try_block);
21909 check_constexpr_ctor_body (last, list, /*complain=*/true);
21910 /* Finish the function body. */
21911 finish_function_body (body);
21914 /* Parse an initializer.
21917 = initializer-clause
21918 ( expression-list )
21920 Returns an expression representing the initializer. If no
21921 initializer is present, NULL_TREE is returned.
21923 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21924 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21925 set to TRUE if there is no initializer present. If there is an
21926 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21927 is set to true; otherwise it is set to false. */
21930 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21931 bool* non_constant_p, bool subexpression_p)
21936 /* Peek at the next token. */
21937 token = cp_lexer_peek_token (parser->lexer);
21939 /* Let our caller know whether or not this initializer was
21941 *is_direct_init = (token->type != CPP_EQ);
21942 /* Assume that the initializer is constant. */
21943 *non_constant_p = false;
21945 if (token->type == CPP_EQ)
21947 /* Consume the `='. */
21948 cp_lexer_consume_token (parser->lexer);
21949 /* Parse the initializer-clause. */
21950 init = cp_parser_initializer_clause (parser, non_constant_p);
21952 else if (token->type == CPP_OPEN_PAREN)
21954 vec<tree, va_gc> *vec;
21955 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21957 /*allow_expansion_p=*/true,
21960 return error_mark_node;
21961 init = build_tree_list_vec (vec);
21962 release_tree_vector (vec);
21964 else if (token->type == CPP_OPEN_BRACE)
21966 cp_lexer_set_source_position (parser->lexer);
21967 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21968 init = cp_parser_braced_list (parser, non_constant_p);
21969 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21973 /* Anything else is an error. */
21974 cp_parser_error (parser, "expected initializer");
21975 init = error_mark_node;
21978 if (!subexpression_p && check_for_bare_parameter_packs (init))
21979 init = error_mark_node;
21984 /* Parse an initializer-clause.
21986 initializer-clause:
21987 assignment-expression
21990 Returns an expression representing the initializer.
21992 If the `assignment-expression' production is used the value
21993 returned is simply a representation for the expression.
21995 Otherwise, calls cp_parser_braced_list. */
21998 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22000 cp_expr initializer;
22002 /* Assume the expression is constant. */
22003 *non_constant_p = false;
22005 /* If it is not a `{', then we are looking at an
22006 assignment-expression. */
22007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22010 = cp_parser_constant_expression (parser,
22011 /*allow_non_constant_p=*/true,
22015 initializer = cp_parser_braced_list (parser, non_constant_p);
22017 return initializer;
22020 /* Parse a brace-enclosed initializer list.
22023 { initializer-list , [opt] }
22024 { designated-initializer-list , [opt] }
22027 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22028 the elements of the initializer-list (or NULL, if the last
22029 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22030 NULL_TREE. There is no way to detect whether or not the optional
22031 trailing `,' was provided. NON_CONSTANT_P is as for
22032 cp_parser_initializer. */
22035 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22038 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22040 /* Consume the `{' token. */
22041 matching_braces braces;
22042 braces.require_open (parser);
22043 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22044 initializer = make_node (CONSTRUCTOR);
22045 /* If it's not a `}', then there is a non-trivial initializer. */
22046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22048 /* Parse the initializer list. */
22049 CONSTRUCTOR_ELTS (initializer)
22050 = cp_parser_initializer_list (parser, non_constant_p);
22051 /* A trailing `,' token is allowed. */
22052 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22053 cp_lexer_consume_token (parser->lexer);
22056 *non_constant_p = false;
22057 /* Now, there should be a trailing `}'. */
22058 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22059 braces.require_close (parser);
22060 TREE_TYPE (initializer) = init_list_type_node;
22062 cp_expr result (initializer);
22063 /* Build a location of the form:
22066 with caret==start at the open brace, finish at the close brace. */
22067 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22068 result.set_location (combined_loc);
22072 /* Consume tokens up to, and including, the next non-nested closing `]'.
22073 Returns true iff we found a closing `]'. */
22076 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22078 unsigned square_depth = 0;
22082 cp_token * token = cp_lexer_peek_token (parser->lexer);
22084 switch (token->type)
22087 case CPP_PRAGMA_EOL:
22088 /* If we've run out of tokens, then there is no closing `]'. */
22091 case CPP_OPEN_SQUARE:
22095 case CPP_CLOSE_SQUARE:
22096 if (!square_depth--)
22098 cp_lexer_consume_token (parser->lexer);
22107 /* Consume the token. */
22108 cp_lexer_consume_token (parser->lexer);
22112 /* Return true if we are looking at an array-designator, false otherwise. */
22115 cp_parser_array_designator_p (cp_parser *parser)
22117 /* Consume the `['. */
22118 cp_lexer_consume_token (parser->lexer);
22120 cp_lexer_save_tokens (parser->lexer);
22122 /* Skip tokens until the next token is a closing square bracket.
22123 If we find the closing `]', and the next token is a `=', then
22124 we are looking at an array designator. */
22125 bool array_designator_p
22126 = (cp_parser_skip_to_closing_square_bracket (parser)
22127 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22129 /* Roll back the tokens we skipped. */
22130 cp_lexer_rollback_tokens (parser->lexer);
22132 return array_designator_p;
22135 /* Parse an initializer-list.
22138 initializer-clause ... [opt]
22139 initializer-list , initializer-clause ... [opt]
22143 designated-initializer-list:
22144 designated-initializer-clause
22145 designated-initializer-list , designated-initializer-clause
22147 designated-initializer-clause:
22148 designator brace-or-equal-initializer
22156 designation initializer-clause ...[opt]
22157 initializer-list , designation initializer-clause ...[opt]
22162 [ constant-expression ] =
22164 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22165 for the initializer. If the INDEX of the elt is non-NULL, it is the
22166 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22167 as for cp_parser_initializer. */
22169 static vec<constructor_elt, va_gc> *
22170 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22172 vec<constructor_elt, va_gc> *v = NULL;
22173 bool first_p = true;
22174 tree first_designator = NULL_TREE;
22176 /* Assume all of the expressions are constant. */
22177 *non_constant_p = false;
22179 /* Parse the rest of the list. */
22185 bool clause_non_constant_p;
22186 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22188 /* Handle the C++2A syntax, '. id ='. */
22189 if ((cxx_dialect >= cxx2a
22190 || cp_parser_allow_gnu_extensions_p (parser))
22191 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22192 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22193 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22194 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22195 == CPP_OPEN_BRACE)))
22197 if (cxx_dialect < cxx2a)
22198 pedwarn (loc, OPT_Wpedantic,
22199 "C++ designated initializers only available with "
22200 "-std=c++2a or -std=gnu++2a");
22201 /* Consume the `.'. */
22202 cp_lexer_consume_token (parser->lexer);
22203 /* Consume the identifier. */
22204 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22205 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22206 /* Consume the `='. */
22207 cp_lexer_consume_token (parser->lexer);
22209 /* Also, if the next token is an identifier and the following one is a
22210 colon, we are looking at the GNU designated-initializer
22212 else if (cp_parser_allow_gnu_extensions_p (parser)
22213 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22214 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22217 /* Warn the user that they are using an extension. */
22218 pedwarn (loc, OPT_Wpedantic,
22219 "ISO C++ does not allow GNU designated initializers");
22220 /* Consume the identifier. */
22221 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22222 /* Consume the `:'. */
22223 cp_lexer_consume_token (parser->lexer);
22225 /* Also handle C99 array designators, '[ const ] ='. */
22226 else if (cp_parser_allow_gnu_extensions_p (parser)
22227 && !c_dialect_objc ()
22228 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22230 /* In C++11, [ could start a lambda-introducer. */
22231 bool non_const = false;
22233 cp_parser_parse_tentatively (parser);
22235 if (!cp_parser_array_designator_p (parser))
22237 cp_parser_simulate_error (parser);
22238 designator = NULL_TREE;
22242 designator = cp_parser_constant_expression (parser, true,
22244 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22245 cp_parser_require (parser, CPP_EQ, RT_EQ);
22248 if (!cp_parser_parse_definitely (parser))
22249 designator = NULL_TREE;
22251 && (!require_potential_rvalue_constant_expression
22253 designator = NULL_TREE;
22255 /* Warn the user that they are using an extension. */
22256 pedwarn (loc, OPT_Wpedantic,
22257 "ISO C++ does not allow C99 designated initializers");
22260 designator = NULL_TREE;
22264 first_designator = designator;
22267 else if (cxx_dialect >= cxx2a
22268 && first_designator != error_mark_node
22269 && (!first_designator != !designator))
22271 error_at (loc, "either all initializer clauses should be designated "
22272 "or none of them should be");
22273 first_designator = error_mark_node;
22275 else if (cxx_dialect < cxx2a && !first_designator)
22276 first_designator = designator;
22278 /* Parse the initializer. */
22279 initializer = cp_parser_initializer_clause (parser,
22280 &clause_non_constant_p);
22281 /* If any clause is non-constant, so is the entire initializer. */
22282 if (clause_non_constant_p)
22283 *non_constant_p = true;
22285 /* If we have an ellipsis, this is an initializer pack
22287 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22289 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22291 /* Consume the `...'. */
22292 cp_lexer_consume_token (parser->lexer);
22294 if (designator && cxx_dialect >= cxx2a)
22296 "%<...%> not allowed in designated initializer list");
22298 /* Turn the initializer into an initializer expansion. */
22299 initializer = make_pack_expansion (initializer);
22302 /* Add it to the vector. */
22303 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22305 /* If the next token is not a comma, we have reached the end of
22307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22310 /* Peek at the next token. */
22311 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22312 /* If the next token is a `}', then we're still done. An
22313 initializer-clause can have a trailing `,' after the
22314 initializer-list and before the closing `}'. */
22315 if (token->type == CPP_CLOSE_BRACE)
22318 /* Consume the `,' token. */
22319 cp_lexer_consume_token (parser->lexer);
22322 /* The same identifier shall not appear in multiple designators
22323 of a designated-initializer-list. */
22324 if (first_designator)
22327 tree designator, val;
22328 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22329 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22331 if (IDENTIFIER_MARKED (designator))
22333 error_at (EXPR_LOC_OR_LOC (val, input_location),
22334 "%<.%s%> designator used multiple times in "
22335 "the same initializer list",
22336 IDENTIFIER_POINTER (designator));
22337 (*v)[i].index = NULL_TREE;
22340 IDENTIFIER_MARKED (designator) = 1;
22342 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22343 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22344 IDENTIFIER_MARKED (designator) = 0;
22350 /* Classes [gram.class] */
22352 /* Parse a class-name.
22358 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22359 to indicate that names looked up in dependent types should be
22360 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22361 keyword has been used to indicate that the name that appears next
22362 is a template. TAG_TYPE indicates the explicit tag given before
22363 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22364 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22365 is the class being defined in a class-head. If ENUM_OK is TRUE,
22366 enum-names are also accepted.
22368 Returns the TYPE_DECL representing the class. */
22371 cp_parser_class_name (cp_parser *parser,
22372 bool typename_keyword_p,
22373 bool template_keyword_p,
22374 enum tag_types tag_type,
22375 bool check_dependency_p,
22377 bool is_declaration,
22384 tree identifier = NULL_TREE;
22386 /* All class-names start with an identifier. */
22387 token = cp_lexer_peek_token (parser->lexer);
22388 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22390 cp_parser_error (parser, "expected class-name");
22391 return error_mark_node;
22394 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22395 to a template-id, so we save it here. */
22396 scope = parser->scope;
22397 if (scope == error_mark_node)
22398 return error_mark_node;
22400 /* Any name names a type if we're following the `typename' keyword
22401 in a qualified name where the enclosing scope is type-dependent. */
22402 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22403 && dependent_type_p (scope));
22404 /* Handle the common case (an identifier, but not a template-id)
22406 if (token->type == CPP_NAME
22407 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22409 cp_token *identifier_token;
22412 /* Look for the identifier. */
22413 identifier_token = cp_lexer_peek_token (parser->lexer);
22414 ambiguous_p = identifier_token->error_reported;
22415 identifier = cp_parser_identifier (parser);
22416 /* If the next token isn't an identifier, we are certainly not
22417 looking at a class-name. */
22418 if (identifier == error_mark_node)
22419 decl = error_mark_node;
22420 /* If we know this is a type-name, there's no need to look it
22422 else if (typename_p)
22426 tree ambiguous_decls;
22427 /* If we already know that this lookup is ambiguous, then
22428 we've already issued an error message; there's no reason
22432 cp_parser_simulate_error (parser);
22433 return error_mark_node;
22435 /* If the next token is a `::', then the name must be a type
22438 [basic.lookup.qual]
22440 During the lookup for a name preceding the :: scope
22441 resolution operator, object, function, and enumerator
22442 names are ignored. */
22443 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22444 tag_type = scope_type;
22445 /* Look up the name. */
22446 decl = cp_parser_lookup_name (parser, identifier,
22448 /*is_template=*/false,
22449 /*is_namespace=*/false,
22450 check_dependency_p,
22452 identifier_token->location);
22453 if (ambiguous_decls)
22455 if (cp_parser_parsing_tentatively (parser))
22456 cp_parser_simulate_error (parser);
22457 return error_mark_node;
22463 /* Try a template-id. */
22464 decl = cp_parser_template_id (parser, template_keyword_p,
22465 check_dependency_p,
22468 if (decl == error_mark_node)
22469 return error_mark_node;
22472 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22474 /* If this is a typename, create a TYPENAME_TYPE. */
22475 if (typename_p && decl != error_mark_node)
22477 decl = make_typename_type (scope, decl, typename_type,
22478 /*complain=*/tf_error);
22479 if (decl != error_mark_node)
22480 decl = TYPE_NAME (decl);
22483 decl = strip_using_decl (decl);
22485 /* Check to see that it is really the name of a class. */
22486 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22487 && identifier_p (TREE_OPERAND (decl, 0))
22488 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22489 /* Situations like this:
22491 template <typename T> struct A {
22492 typename T::template X<int>::I i;
22495 are problematic. Is `T::template X<int>' a class-name? The
22496 standard does not seem to be definitive, but there is no other
22497 valid interpretation of the following `::'. Therefore, those
22498 names are considered class-names. */
22500 decl = make_typename_type (scope, decl, tag_type, tf_error);
22501 if (decl != error_mark_node)
22502 decl = TYPE_NAME (decl);
22504 else if (TREE_CODE (decl) != TYPE_DECL
22505 || TREE_TYPE (decl) == error_mark_node
22506 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22507 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22508 /* In Objective-C 2.0, a classname followed by '.' starts a
22509 dot-syntax expression, and it's not a type-name. */
22510 || (c_dialect_objc ()
22511 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22512 && objc_is_class_name (decl)))
22513 decl = error_mark_node;
22515 if (decl == error_mark_node)
22516 cp_parser_error (parser, "expected class-name");
22517 else if (identifier && !parser->scope)
22518 maybe_note_name_used_in_class (identifier, decl);
22523 /* Parse a class-specifier.
22526 class-head { member-specification [opt] }
22528 Returns the TREE_TYPE representing the class. */
22531 cp_parser_class_specifier_1 (cp_parser* parser)
22534 tree attributes = NULL_TREE;
22535 bool nested_name_specifier_p;
22536 unsigned saved_num_template_parameter_lists;
22537 bool saved_in_function_body;
22538 unsigned char in_statement;
22539 bool in_switch_statement_p;
22540 bool saved_in_unbraced_linkage_specification_p;
22541 tree old_scope = NULL_TREE;
22542 tree scope = NULL_TREE;
22543 cp_token *closing_brace;
22545 push_deferring_access_checks (dk_no_deferred);
22547 /* Parse the class-head. */
22548 type = cp_parser_class_head (parser,
22549 &nested_name_specifier_p);
22550 /* If the class-head was a semantic disaster, skip the entire body
22554 cp_parser_skip_to_end_of_block_or_statement (parser);
22555 pop_deferring_access_checks ();
22556 return error_mark_node;
22559 /* Look for the `{'. */
22560 matching_braces braces;
22561 if (!braces.require_open (parser))
22563 pop_deferring_access_checks ();
22564 return error_mark_node;
22567 cp_ensure_no_omp_declare_simd (parser);
22568 cp_ensure_no_oacc_routine (parser);
22570 /* Issue an error message if type-definitions are forbidden here. */
22571 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
22572 /* Remember that we are defining one more class. */
22573 ++parser->num_classes_being_defined;
22574 /* Inside the class, surrounding template-parameter-lists do not
22576 saved_num_template_parameter_lists
22577 = parser->num_template_parameter_lists;
22578 parser->num_template_parameter_lists = 0;
22579 /* We are not in a function body. */
22580 saved_in_function_body = parser->in_function_body;
22581 parser->in_function_body = false;
22582 /* Or in a loop. */
22583 in_statement = parser->in_statement;
22584 parser->in_statement = 0;
22585 /* Or in a switch. */
22586 in_switch_statement_p = parser->in_switch_statement_p;
22587 parser->in_switch_statement_p = false;
22588 /* We are not immediately inside an extern "lang" block. */
22589 saved_in_unbraced_linkage_specification_p
22590 = parser->in_unbraced_linkage_specification_p;
22591 parser->in_unbraced_linkage_specification_p = false;
22593 // Associate constraints with the type.
22595 type = associate_classtype_constraints (type);
22597 /* Start the class. */
22598 if (nested_name_specifier_p)
22600 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22601 old_scope = push_inner_scope (scope);
22603 type = begin_class_definition (type);
22605 if (type == error_mark_node)
22606 /* If the type is erroneous, skip the entire body of the class. */
22607 cp_parser_skip_to_closing_brace (parser);
22609 /* Parse the member-specification. */
22610 cp_parser_member_specification_opt (parser);
22612 /* Look for the trailing `}'. */
22613 closing_brace = braces.require_close (parser);
22614 /* Look for trailing attributes to apply to this class. */
22615 if (cp_parser_allow_gnu_extensions_p (parser))
22616 attributes = cp_parser_gnu_attributes_opt (parser);
22617 if (type != error_mark_node)
22618 type = finish_struct (type, attributes);
22619 if (nested_name_specifier_p)
22620 pop_inner_scope (old_scope, scope);
22622 /* We've finished a type definition. Check for the common syntax
22623 error of forgetting a semicolon after the definition. We need to
22624 be careful, as we can't just check for not-a-semicolon and be done
22625 with it; the user might have typed:
22627 class X { } c = ...;
22628 class X { } *p = ...;
22630 and so forth. Instead, enumerate all the possible tokens that
22631 might follow this production; if we don't see one of them, then
22632 complain and silently insert the semicolon. */
22634 cp_token *token = cp_lexer_peek_token (parser->lexer);
22635 bool want_semicolon = true;
22637 if (cp_next_tokens_can_be_std_attribute_p (parser))
22638 /* Don't try to parse c++11 attributes here. As per the
22639 grammar, that should be a task for
22640 cp_parser_decl_specifier_seq. */
22641 want_semicolon = false;
22643 switch (token->type)
22646 case CPP_SEMICOLON:
22649 case CPP_OPEN_PAREN:
22650 case CPP_CLOSE_PAREN:
22652 want_semicolon = false;
22655 /* While it's legal for type qualifiers and storage class
22656 specifiers to follow type definitions in the grammar, only
22657 compiler testsuites contain code like that. Assume that if
22658 we see such code, then what we're really seeing is a case
22662 const <type> var = ...;
22667 static <type> func (...) ...
22669 i.e. the qualifier or specifier applies to the next
22670 declaration. To do so, however, we need to look ahead one
22671 more token to see if *that* token is a type specifier.
22673 This code could be improved to handle:
22676 static const <type> var = ...; */
22678 if (keyword_is_decl_specifier (token->keyword))
22680 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22682 /* Handling user-defined types here would be nice, but very
22685 = (lookahead->type == CPP_KEYWORD
22686 && keyword_begins_type_specifier (lookahead->keyword));
22693 /* If we don't have a type, then something is very wrong and we
22694 shouldn't try to do anything clever. Likewise for not seeing the
22696 if (closing_brace && TYPE_P (type) && want_semicolon)
22698 /* Locate the closing brace. */
22699 cp_token_position prev
22700 = cp_lexer_previous_token_position (parser->lexer);
22701 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22702 location_t loc = prev_token->location;
22704 /* We want to suggest insertion of a ';' immediately *after* the
22705 closing brace, so, if we can, offset the location by 1 column. */
22706 location_t next_loc = loc;
22707 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22708 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22710 rich_location richloc (line_table, next_loc);
22712 /* If we successfully offset the location, suggest the fix-it. */
22713 if (next_loc != loc)
22714 richloc.add_fixit_insert_before (next_loc, ";");
22716 if (CLASSTYPE_DECLARED_CLASS (type))
22717 error_at (&richloc,
22718 "expected %<;%> after class definition");
22719 else if (TREE_CODE (type) == RECORD_TYPE)
22720 error_at (&richloc,
22721 "expected %<;%> after struct definition");
22722 else if (TREE_CODE (type) == UNION_TYPE)
22723 error_at (&richloc,
22724 "expected %<;%> after union definition");
22726 gcc_unreachable ();
22728 /* Unget one token and smash it to look as though we encountered
22729 a semicolon in the input stream. */
22730 cp_lexer_set_token_position (parser->lexer, prev);
22731 token = cp_lexer_peek_token (parser->lexer);
22732 token->type = CPP_SEMICOLON;
22733 token->keyword = RID_MAX;
22737 /* If this class is not itself within the scope of another class,
22738 then we need to parse the bodies of all of the queued function
22739 definitions. Note that the queued functions defined in a class
22740 are not always processed immediately following the
22741 class-specifier for that class. Consider:
22744 struct B { void f() { sizeof (A); } };
22747 If `f' were processed before the processing of `A' were
22748 completed, there would be no way to compute the size of `A'.
22749 Note that the nesting we are interested in here is lexical --
22750 not the semantic nesting given by TYPE_CONTEXT. In particular,
22753 struct A { struct B; };
22754 struct A::B { void f() { } };
22756 there is no need to delay the parsing of `A::B::f'. */
22757 if (--parser->num_classes_being_defined == 0)
22760 tree class_type = NULL_TREE;
22761 tree pushed_scope = NULL_TREE;
22763 cp_default_arg_entry *e;
22764 tree save_ccp, save_ccr;
22766 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
22768 /* Skip default arguments, NSDMIs, etc, in order to improve
22769 error recovery (c++/71169, c++/71832). */
22770 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22771 vec_safe_truncate (unparsed_nsdmis, 0);
22772 vec_safe_truncate (unparsed_classes, 0);
22773 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22776 /* In a first pass, parse default arguments to the functions.
22777 Then, in a second pass, parse the bodies of the functions.
22778 This two-phased approach handles cases like:
22786 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22789 /* If there are default arguments that have not yet been processed,
22790 take care of them now. */
22791 if (class_type != e->class_type)
22794 pop_scope (pushed_scope);
22795 class_type = e->class_type;
22796 pushed_scope = push_scope (class_type);
22798 /* Make sure that any template parameters are in scope. */
22799 maybe_begin_member_template_processing (decl);
22800 /* Parse the default argument expressions. */
22801 cp_parser_late_parsing_default_args (parser, decl);
22802 /* Remove any template parameters from the symbol table. */
22803 maybe_end_member_template_processing ();
22805 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22806 /* Now parse any NSDMIs. */
22807 save_ccp = current_class_ptr;
22808 save_ccr = current_class_ref;
22809 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22811 if (class_type != DECL_CONTEXT (decl))
22814 pop_scope (pushed_scope);
22815 class_type = DECL_CONTEXT (decl);
22816 pushed_scope = push_scope (class_type);
22818 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22819 cp_parser_late_parsing_nsdmi (parser, decl);
22821 vec_safe_truncate (unparsed_nsdmis, 0);
22822 current_class_ptr = save_ccp;
22823 current_class_ref = save_ccr;
22825 pop_scope (pushed_scope);
22827 /* Now do some post-NSDMI bookkeeping. */
22828 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22829 after_nsdmi_defaulted_late_checks (class_type);
22830 vec_safe_truncate (unparsed_classes, 0);
22831 after_nsdmi_defaulted_late_checks (type);
22833 /* Now parse the body of the functions. */
22836 /* OpenMP UDRs need to be parsed before all other functions. */
22837 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22838 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22839 cp_parser_late_parsing_for_member (parser, decl);
22840 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22841 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22842 cp_parser_late_parsing_for_member (parser, decl);
22845 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22846 cp_parser_late_parsing_for_member (parser, decl);
22847 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22850 vec_safe_push (unparsed_classes, type);
22852 /* Put back any saved access checks. */
22853 pop_deferring_access_checks ();
22855 /* Restore saved state. */
22856 parser->in_switch_statement_p = in_switch_statement_p;
22857 parser->in_statement = in_statement;
22858 parser->in_function_body = saved_in_function_body;
22859 parser->num_template_parameter_lists
22860 = saved_num_template_parameter_lists;
22861 parser->in_unbraced_linkage_specification_p
22862 = saved_in_unbraced_linkage_specification_p;
22868 cp_parser_class_specifier (cp_parser* parser)
22871 timevar_push (TV_PARSE_STRUCT);
22872 ret = cp_parser_class_specifier_1 (parser);
22873 timevar_pop (TV_PARSE_STRUCT);
22877 /* Parse a class-head.
22880 class-key identifier [opt] base-clause [opt]
22881 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22882 class-key nested-name-specifier [opt] template-id
22885 class-virt-specifier:
22889 class-key attributes identifier [opt] base-clause [opt]
22890 class-key attributes nested-name-specifier identifier base-clause [opt]
22891 class-key attributes nested-name-specifier [opt] template-id
22894 Upon return BASES is initialized to the list of base classes (or
22895 NULL, if there are none) in the same form returned by
22896 cp_parser_base_clause.
22898 Returns the TYPE of the indicated class. Sets
22899 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22900 involving a nested-name-specifier was used, and FALSE otherwise.
22902 Returns error_mark_node if this is not a class-head.
22904 Returns NULL_TREE if the class-head is syntactically valid, but
22905 semantically invalid in a way that means we should skip the entire
22906 body of the class. */
22909 cp_parser_class_head (cp_parser* parser,
22910 bool* nested_name_specifier_p)
22912 tree nested_name_specifier;
22913 enum tag_types class_key;
22914 tree id = NULL_TREE;
22915 tree type = NULL_TREE;
22918 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22919 bool template_id_p = false;
22920 bool qualified_p = false;
22921 bool invalid_nested_name_p = false;
22922 bool invalid_explicit_specialization_p = false;
22923 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22924 tree pushed_scope = NULL_TREE;
22925 unsigned num_templates;
22926 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22927 /* Assume no nested-name-specifier will be present. */
22928 *nested_name_specifier_p = false;
22929 /* Assume no template parameter lists will be used in defining the
22932 parser->colon_corrects_to_scope_p = false;
22934 /* Look for the class-key. */
22935 class_key = cp_parser_class_key (parser);
22936 if (class_key == none_type)
22937 return error_mark_node;
22939 location_t class_head_start_location = input_location;
22941 /* Parse the attributes. */
22942 attributes = cp_parser_attributes_opt (parser);
22944 /* If the next token is `::', that is invalid -- but sometimes
22945 people do try to write:
22949 Handle this gracefully by accepting the extra qualifier, and then
22950 issuing an error about it later if this really is a
22951 class-head. If it turns out just to be an elaborated type
22952 specifier, remain silent. */
22953 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22954 qualified_p = true;
22956 push_deferring_access_checks (dk_no_check);
22958 /* Determine the name of the class. Begin by looking for an
22959 optional nested-name-specifier. */
22960 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22961 nested_name_specifier
22962 = cp_parser_nested_name_specifier_opt (parser,
22963 /*typename_keyword_p=*/false,
22964 /*check_dependency_p=*/false,
22966 /*is_declaration=*/false);
22967 /* If there was a nested-name-specifier, then there *must* be an
22970 cp_token *bad_template_keyword = NULL;
22972 if (nested_name_specifier)
22974 type_start_token = cp_lexer_peek_token (parser->lexer);
22975 /* Although the grammar says `identifier', it really means
22976 `class-name' or `template-name'. You are only allowed to
22977 define a class that has already been declared with this
22980 The proposed resolution for Core Issue 180 says that wherever
22981 you see `class T::X' you should treat `X' as a type-name.
22983 It is OK to define an inaccessible class; for example:
22985 class A { class B; };
22988 We do not know if we will see a class-name, or a
22989 template-name. We look for a class-name first, in case the
22990 class-name is a template-id; if we looked for the
22991 template-name first we would stop after the template-name. */
22992 cp_parser_parse_tentatively (parser);
22993 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22994 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22995 type = cp_parser_class_name (parser,
22996 /*typename_keyword_p=*/false,
22997 /*template_keyword_p=*/false,
22999 /*check_dependency_p=*/false,
23000 /*class_head_p=*/true,
23001 /*is_declaration=*/false);
23002 /* If that didn't work, ignore the nested-name-specifier. */
23003 if (!cp_parser_parse_definitely (parser))
23005 invalid_nested_name_p = true;
23006 type_start_token = cp_lexer_peek_token (parser->lexer);
23007 id = cp_parser_identifier (parser);
23008 if (id == error_mark_node)
23011 /* If we could not find a corresponding TYPE, treat this
23012 declaration like an unqualified declaration. */
23013 if (type == error_mark_node)
23014 nested_name_specifier = NULL_TREE;
23015 /* Otherwise, count the number of templates used in TYPE and its
23016 containing scopes. */
23021 for (scope = TREE_TYPE (type);
23022 scope && TREE_CODE (scope) != NAMESPACE_DECL;
23023 scope = get_containing_scope (scope))
23025 && CLASS_TYPE_P (scope)
23026 && CLASSTYPE_TEMPLATE_INFO (scope)
23027 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
23028 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
23029 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
23033 /* Otherwise, the identifier is optional. */
23036 /* We don't know whether what comes next is a template-id,
23037 an identifier, or nothing at all. */
23038 cp_parser_parse_tentatively (parser);
23039 /* Check for a template-id. */
23040 type_start_token = cp_lexer_peek_token (parser->lexer);
23041 id = cp_parser_template_id (parser,
23042 /*template_keyword_p=*/false,
23043 /*check_dependency_p=*/true,
23045 /*is_declaration=*/true);
23046 /* If that didn't work, it could still be an identifier. */
23047 if (!cp_parser_parse_definitely (parser))
23049 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23051 type_start_token = cp_lexer_peek_token (parser->lexer);
23052 id = cp_parser_identifier (parser);
23059 template_id_p = true;
23064 pop_deferring_access_checks ();
23068 cp_parser_check_for_invalid_template_id (parser, id,
23070 type_start_token->location);
23072 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23074 /* If it's not a `:' or a `{' then we can't really be looking at a
23075 class-head, since a class-head only appears as part of a
23076 class-specifier. We have to detect this situation before calling
23077 xref_tag, since that has irreversible side-effects. */
23078 if (!cp_parser_next_token_starts_class_definition_p (parser))
23080 cp_parser_error (parser, "expected %<{%> or %<:%>");
23081 type = error_mark_node;
23085 /* At this point, we're going ahead with the class-specifier, even
23086 if some other problem occurs. */
23087 cp_parser_commit_to_tentative_parse (parser);
23088 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23090 cp_parser_error (parser,
23091 "cannot specify %<override%> for a class");
23092 type = error_mark_node;
23095 /* Issue the error about the overly-qualified name now. */
23098 cp_parser_error (parser,
23099 "global qualification of class name is invalid");
23100 type = error_mark_node;
23103 else if (invalid_nested_name_p)
23105 cp_parser_error (parser,
23106 "qualified name does not name a class");
23107 type = error_mark_node;
23110 else if (nested_name_specifier)
23114 if (bad_template_keyword)
23115 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23116 keyword template shall not appear at the top level. */
23117 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23118 "keyword %<template%> not allowed in class-head-name");
23120 /* Reject typedef-names in class heads. */
23121 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23123 error_at (type_start_token->location,
23124 "invalid class name in declaration of %qD",
23130 /* Figure out in what scope the declaration is being placed. */
23131 scope = current_scope ();
23132 /* If that scope does not contain the scope in which the
23133 class was originally declared, the program is invalid. */
23134 if (scope && !is_ancestor (scope, nested_name_specifier))
23136 if (at_namespace_scope_p ())
23137 error_at (type_start_token->location,
23138 "declaration of %qD in namespace %qD which does not "
23140 type, scope, nested_name_specifier);
23142 error_at (type_start_token->location,
23143 "declaration of %qD in %qD which does not enclose %qD",
23144 type, scope, nested_name_specifier);
23150 A declarator-id shall not be qualified except for the
23151 definition of a ... nested class outside of its class
23152 ... [or] the definition or explicit instantiation of a
23153 class member of a namespace outside of its namespace. */
23154 if (scope == nested_name_specifier)
23156 permerror (nested_name_specifier_token_start->location,
23157 "extra qualification not allowed");
23158 nested_name_specifier = NULL_TREE;
23162 /* An explicit-specialization must be preceded by "template <>". If
23163 it is not, try to recover gracefully. */
23164 if (at_namespace_scope_p ()
23165 && parser->num_template_parameter_lists == 0
23166 && !processing_template_parmlist
23169 /* Build a location of this form:
23170 struct typename <ARGS>
23171 ^~~~~~~~~~~~~~~~~~~~~~
23172 with caret==start at the start token, and
23173 finishing at the end of the type. */
23174 location_t reported_loc
23175 = make_location (class_head_start_location,
23176 class_head_start_location,
23177 get_finish (type_start_token->location));
23178 rich_location richloc (line_table, reported_loc);
23179 richloc.add_fixit_insert_before (class_head_start_location,
23181 error_at (&richloc,
23182 "an explicit specialization must be preceded by"
23183 " %<template <>%>");
23184 invalid_explicit_specialization_p = true;
23185 /* Take the same action that would have been taken by
23186 cp_parser_explicit_specialization. */
23187 ++parser->num_template_parameter_lists;
23188 begin_specialization ();
23190 /* There must be no "return" statements between this point and the
23191 end of this function; set "type "to the correct return value and
23192 use "goto done;" to return. */
23193 /* Make sure that the right number of template parameters were
23195 if (!cp_parser_check_template_parameters (parser, num_templates,
23197 type_start_token->location,
23198 /*declarator=*/NULL))
23200 /* If something went wrong, there is no point in even trying to
23201 process the class-definition. */
23206 /* Look up the type. */
23209 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23210 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23211 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23213 error_at (type_start_token->location,
23214 "function template %qD redeclared as a class template", id);
23215 type = error_mark_node;
23219 type = TREE_TYPE (id);
23220 type = maybe_process_partial_specialization (type);
23222 /* Check the scope while we still know whether or not we had a
23223 nested-name-specifier. */
23224 if (type != error_mark_node)
23225 check_unqualified_spec_or_inst (type, type_start_token->location);
23227 if (nested_name_specifier)
23228 pushed_scope = push_scope (nested_name_specifier);
23230 else if (nested_name_specifier)
23236 template <typename T> struct S { struct T };
23237 template <typename T> struct S<T>::T { };
23239 we will get a TYPENAME_TYPE when processing the definition of
23240 `S::T'. We need to resolve it to the actual type before we
23241 try to define it. */
23242 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23244 class_type = resolve_typename_type (TREE_TYPE (type),
23245 /*only_current_p=*/false);
23246 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23247 type = TYPE_NAME (class_type);
23250 cp_parser_error (parser, "could not resolve typename type");
23251 type = error_mark_node;
23255 if (maybe_process_partial_specialization (TREE_TYPE (type))
23256 == error_mark_node)
23262 class_type = current_class_type;
23263 /* Enter the scope indicated by the nested-name-specifier. */
23264 pushed_scope = push_scope (nested_name_specifier);
23265 /* Get the canonical version of this type. */
23266 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23267 /* Call push_template_decl if it seems like we should be defining a
23268 template either from the template headers or the type we're
23269 defining, so that we diagnose both extra and missing headers. */
23270 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23271 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23272 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23274 type = push_template_decl (type);
23275 if (type == error_mark_node)
23282 type = TREE_TYPE (type);
23283 *nested_name_specifier_p = true;
23285 else /* The name is not a nested name. */
23287 /* If the class was unnamed, create a dummy name. */
23289 id = make_anon_name ();
23290 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23291 ? ts_within_enclosing_non_class
23293 type = xref_tag (class_key, id, tag_scope,
23294 parser->num_template_parameter_lists);
23297 /* Indicate whether this class was declared as a `class' or as a
23299 if (TREE_CODE (type) == RECORD_TYPE)
23300 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23301 cp_parser_check_class_key (class_key, type);
23303 /* If this type was already complete, and we see another definition,
23304 that's an error. */
23305 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23307 error_at (type_start_token->location, "redefinition of %q#T",
23309 inform (location_of (type), "previous definition of %q#T",
23314 else if (type == error_mark_node)
23319 /* Apply attributes now, before any use of the class as a template
23320 argument in its base list. */
23321 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23322 fixup_attribute_variants (type);
23325 /* We will have entered the scope containing the class; the names of
23326 base classes should be looked up in that context. For example:
23328 struct A { struct B {}; struct C; };
23329 struct A::C : B {};
23333 /* Get the list of base-classes, if there is one. */
23334 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23336 /* PR59482: enter the class scope so that base-specifiers are looked
23340 bases = cp_parser_base_clause (parser);
23341 /* PR59482: get out of the previously pushed class scope so that the
23342 subsequent pops pop the right thing. */
23349 /* If we're really defining a class, process the base classes.
23350 If they're invalid, fail. */
23351 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23352 xref_basetypes (type, bases);
23355 /* Leave the scope given by the nested-name-specifier. We will
23356 enter the class scope itself while processing the members. */
23358 pop_scope (pushed_scope);
23360 if (invalid_explicit_specialization_p)
23362 end_specialization ();
23363 --parser->num_template_parameter_lists;
23367 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23368 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23369 CLASSTYPE_FINAL (type) = 1;
23371 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23375 /* Parse a class-key.
23382 Returns the kind of class-key specified, or none_type to indicate
23385 static enum tag_types
23386 cp_parser_class_key (cp_parser* parser)
23389 enum tag_types tag_type;
23391 /* Look for the class-key. */
23392 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23396 /* Check to see if the TOKEN is a class-key. */
23397 tag_type = cp_parser_token_is_class_key (token);
23399 cp_parser_error (parser, "expected class-key");
23403 /* Parse a type-parameter-key.
23405 type-parameter-key:
23411 cp_parser_type_parameter_key (cp_parser* parser)
23413 /* Look for the type-parameter-key. */
23414 enum tag_types tag_type = none_type;
23415 cp_token *token = cp_lexer_peek_token (parser->lexer);
23416 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23418 cp_lexer_consume_token (parser->lexer);
23419 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23420 /* typename is not allowed in a template template parameter
23421 by the standard until C++17. */
23422 pedwarn (token->location, OPT_Wpedantic,
23423 "ISO C++ forbids typename key in template template parameter;"
23424 " use -std=c++17 or -std=gnu++17");
23427 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23432 /* Parse an (optional) member-specification.
23434 member-specification:
23435 member-declaration member-specification [opt]
23436 access-specifier : member-specification [opt] */
23439 cp_parser_member_specification_opt (cp_parser* parser)
23446 /* Peek at the next token. */
23447 token = cp_lexer_peek_token (parser->lexer);
23448 /* If it's a `}', or EOF then we've seen all the members. */
23449 if (token->type == CPP_CLOSE_BRACE
23450 || token->type == CPP_EOF
23451 || token->type == CPP_PRAGMA_EOL)
23454 /* See if this token is a keyword. */
23455 keyword = token->keyword;
23459 case RID_PROTECTED:
23461 /* Consume the access-specifier. */
23462 cp_lexer_consume_token (parser->lexer);
23463 /* Remember which access-specifier is active. */
23464 current_access_specifier = token->u.value;
23465 /* Look for the `:'. */
23466 cp_parser_require (parser, CPP_COLON, RT_COLON);
23470 /* Accept #pragmas at class scope. */
23471 if (token->type == CPP_PRAGMA)
23473 cp_parser_pragma (parser, pragma_member, NULL);
23477 /* Otherwise, the next construction must be a
23478 member-declaration. */
23479 cp_parser_member_declaration (parser);
23484 /* Parse a member-declaration.
23486 member-declaration:
23487 decl-specifier-seq [opt] member-declarator-list [opt] ;
23488 function-definition ; [opt]
23489 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23491 template-declaration
23494 member-declarator-list:
23496 member-declarator-list , member-declarator
23499 declarator pure-specifier [opt]
23500 declarator constant-initializer [opt]
23501 identifier [opt] : constant-expression
23505 member-declaration:
23506 __extension__ member-declaration
23509 declarator attributes [opt] pure-specifier [opt]
23510 declarator attributes [opt] constant-initializer [opt]
23511 identifier [opt] attributes [opt] : constant-expression
23515 member-declaration:
23516 static_assert-declaration */
23519 cp_parser_member_declaration (cp_parser* parser)
23521 cp_decl_specifier_seq decl_specifiers;
23522 tree prefix_attributes;
23524 int declares_class_or_enum;
23526 cp_token *token = NULL;
23527 cp_token *decl_spec_token_start = NULL;
23528 cp_token *initializer_token_start = NULL;
23529 int saved_pedantic;
23530 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23532 /* Check for the `__extension__' keyword. */
23533 if (cp_parser_extension_opt (parser, &saved_pedantic))
23536 cp_parser_member_declaration (parser);
23537 /* Restore the old value of the PEDANTIC flag. */
23538 pedantic = saved_pedantic;
23543 /* Check for a template-declaration. */
23544 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23546 /* An explicit specialization here is an error condition, and we
23547 expect the specialization handler to detect and report this. */
23548 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23549 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23550 cp_parser_explicit_specialization (parser);
23552 cp_parser_template_declaration (parser, /*member_p=*/true);
23556 /* Check for a template introduction. */
23557 else if (cp_parser_template_declaration_after_export (parser, true))
23560 /* Check for a using-declaration. */
23561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23563 if (cxx_dialect < cxx11)
23565 /* Parse the using-declaration. */
23566 cp_parser_using_declaration (parser,
23567 /*access_declaration_p=*/false);
23573 bool alias_decl_expected;
23574 cp_parser_parse_tentatively (parser);
23575 decl = cp_parser_alias_declaration (parser);
23576 /* Note that if we actually see the '=' token after the
23577 identifier, cp_parser_alias_declaration commits the
23578 tentative parse. In that case, we really expect an
23579 alias-declaration. Otherwise, we expect a using
23581 alias_decl_expected =
23582 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23583 cp_parser_parse_definitely (parser);
23585 if (alias_decl_expected)
23586 finish_member_declaration (decl);
23588 cp_parser_using_declaration (parser,
23589 /*access_declaration_p=*/false);
23594 /* Check for @defs. */
23595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23598 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23599 ivar = ivar_chains;
23603 ivar = TREE_CHAIN (member);
23604 TREE_CHAIN (member) = NULL_TREE;
23605 finish_member_declaration (member);
23610 /* If the next token is `static_assert' we have a static assertion. */
23611 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23613 cp_parser_static_assert (parser, /*member_p=*/true);
23617 parser->colon_corrects_to_scope_p = false;
23619 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23622 /* Parse the decl-specifier-seq. */
23623 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23624 cp_parser_decl_specifier_seq (parser,
23625 CP_PARSER_FLAGS_OPTIONAL,
23627 &declares_class_or_enum);
23628 /* Check for an invalid type-name. */
23629 if (!decl_specifiers.any_type_specifiers_p
23630 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23632 /* If there is no declarator, then the decl-specifier-seq should
23634 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23636 /* If there was no decl-specifier-seq, and the next token is a
23637 `;', then we have something like:
23643 Each member-declaration shall declare at least one member
23644 name of the class. */
23645 if (!decl_specifiers.any_specifiers_p)
23647 cp_token *token = cp_lexer_peek_token (parser->lexer);
23648 if (!in_system_header_at (token->location))
23650 gcc_rich_location richloc (token->location);
23651 richloc.add_fixit_remove ();
23652 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23659 /* See if this declaration is a friend. */
23660 friend_p = cp_parser_friend_p (&decl_specifiers);
23661 /* If there were decl-specifiers, check to see if there was
23662 a class-declaration. */
23663 type = check_tag_decl (&decl_specifiers,
23664 /*explicit_type_instantiation_p=*/false);
23665 /* Nested classes have already been added to the class, but
23666 a `friend' needs to be explicitly registered. */
23669 /* If the `friend' keyword was present, the friend must
23670 be introduced with a class-key. */
23671 if (!declares_class_or_enum && cxx_dialect < cxx11)
23672 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23673 "in C++03 a class-key must be used "
23674 "when declaring a friend");
23677 template <typename T> struct A {
23678 friend struct A<T>::B;
23681 A<T>::B will be represented by a TYPENAME_TYPE, and
23682 therefore not recognized by check_tag_decl. */
23685 type = decl_specifiers.type;
23686 if (type && TREE_CODE (type) == TYPE_DECL)
23687 type = TREE_TYPE (type);
23689 if (!type || !TYPE_P (type))
23690 error_at (decl_spec_token_start->location,
23691 "friend declaration does not name a class or "
23694 make_friend_class (current_class_type, type,
23695 /*complain=*/true);
23697 /* If there is no TYPE, an error message will already have
23699 else if (!type || type == error_mark_node)
23701 /* An anonymous aggregate has to be handled specially; such
23702 a declaration really declares a data member (with a
23703 particular type), as opposed to a nested class. */
23704 else if (ANON_AGGR_TYPE_P (type))
23707 if (decl_specifiers.storage_class != sc_none)
23708 error_at (decl_spec_token_start->location,
23709 "a storage class on an anonymous aggregate "
23710 "in class scope is not allowed");
23712 /* Remove constructors and such from TYPE, now that we
23713 know it is an anonymous aggregate. */
23714 fixup_anonymous_aggr (type);
23715 /* And make the corresponding data member. */
23716 decl = build_decl (decl_spec_token_start->location,
23717 FIELD_DECL, NULL_TREE, type);
23718 /* Add it to the class. */
23719 finish_member_declaration (decl);
23722 cp_parser_check_access_in_redeclaration
23724 decl_spec_token_start->location);
23729 bool assume_semicolon = false;
23731 /* Clear attributes from the decl_specifiers but keep them
23732 around as prefix attributes that apply them to the entity
23734 prefix_attributes = decl_specifiers.attributes;
23735 decl_specifiers.attributes = NULL_TREE;
23737 /* See if these declarations will be friends. */
23738 friend_p = cp_parser_friend_p (&decl_specifiers);
23740 /* Keep going until we hit the `;' at the end of the
23742 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23744 tree attributes = NULL_TREE;
23745 tree first_attribute;
23747 bool named_bitfld = false;
23749 /* Peek at the next token. */
23750 token = cp_lexer_peek_token (parser->lexer);
23752 /* The following code wants to know early if it is a bit-field
23753 or some other declaration. Attributes can appear before
23754 the `:' token. Skip over them without consuming any tokens
23755 to peek if they are followed by `:'. */
23756 if (cp_next_tokens_can_be_attribute_p (parser)
23757 || (token->type == CPP_NAME
23758 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23759 && (named_bitfld = true)))
23762 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23763 token = cp_lexer_peek_nth_token (parser->lexer, n);
23766 /* Check for a bitfield declaration. */
23767 if (token->type == CPP_COLON
23768 || (token->type == CPP_NAME
23769 && token == cp_lexer_peek_token (parser->lexer)
23770 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23771 && (named_bitfld = true)))
23775 tree late_attributes = NULL_TREE;
23778 identifier = cp_parser_identifier (parser);
23780 identifier = NULL_TREE;
23782 /* Look for attributes that apply to the bitfield. */
23783 attributes = cp_parser_attributes_opt (parser);
23785 /* Consume the `:' token. */
23786 cp_lexer_consume_token (parser->lexer);
23788 /* Get the width of the bitfield. */
23789 width = cp_parser_constant_expression (parser, false, NULL,
23790 cxx_dialect >= cxx11);
23792 /* In C++2A and as extension for C++11 and above we allow
23793 default member initializers for bit-fields. */
23794 initializer = NULL_TREE;
23795 if (cxx_dialect >= cxx11
23796 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23797 || cp_lexer_next_token_is (parser->lexer,
23801 = cp_lexer_peek_token (parser->lexer)->location;
23802 if (cxx_dialect < cxx2a
23803 && !in_system_header_at (loc)
23804 && identifier != NULL_TREE)
23806 "default member initializers for bit-fields "
23807 "only available with -std=c++2a or "
23810 initializer = cp_parser_save_nsdmi (parser);
23811 if (identifier == NULL_TREE)
23813 error_at (loc, "default member initializer for "
23814 "unnamed bit-field");
23815 initializer = NULL_TREE;
23820 /* Look for attributes that apply to the bitfield after
23821 the `:' token and width. This is where GCC used to
23822 parse attributes in the past, pedwarn if there is
23823 a std attribute. */
23824 if (cp_next_tokens_can_be_std_attribute_p (parser))
23825 pedwarn (input_location, OPT_Wpedantic,
23826 "ISO C++ allows bit-field attributes only "
23827 "before the %<:%> token");
23829 late_attributes = cp_parser_attributes_opt (parser);
23832 attributes = attr_chainon (attributes, late_attributes);
23834 /* Remember which attributes are prefix attributes and
23836 first_attribute = attributes;
23837 /* Combine the attributes. */
23838 attributes = attr_chainon (prefix_attributes, attributes);
23840 /* Create the bitfield declaration. */
23841 decl = grokbitfield (identifier
23842 ? make_id_declarator (NULL_TREE,
23847 width, initializer,
23852 cp_declarator *declarator;
23853 tree asm_specification;
23854 int ctor_dtor_or_conv_p;
23856 /* Parse the declarator. */
23858 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23859 &ctor_dtor_or_conv_p,
23860 /*parenthesized_p=*/NULL,
23864 /* If something went wrong parsing the declarator, make sure
23865 that we at least consume some tokens. */
23866 if (declarator == cp_error_declarator)
23868 /* Skip to the end of the statement. */
23869 cp_parser_skip_to_end_of_statement (parser);
23870 /* If the next token is not a semicolon, that is
23871 probably because we just skipped over the body of
23872 a function. So, we consume a semicolon if
23873 present, but do not issue an error message if it
23875 if (cp_lexer_next_token_is (parser->lexer,
23877 cp_lexer_consume_token (parser->lexer);
23881 if (declares_class_or_enum & 2)
23882 cp_parser_check_for_definition_in_return_type
23883 (declarator, decl_specifiers.type,
23884 decl_specifiers.locations[ds_type_spec]);
23886 /* Look for an asm-specification. */
23887 asm_specification = cp_parser_asm_specification_opt (parser);
23888 /* Look for attributes that apply to the declaration. */
23889 attributes = cp_parser_attributes_opt (parser);
23890 /* Remember which attributes are prefix attributes and
23892 first_attribute = attributes;
23893 /* Combine the attributes. */
23894 attributes = attr_chainon (prefix_attributes, attributes);
23896 /* If it's an `=', then we have a constant-initializer or a
23897 pure-specifier. It is not correct to parse the
23898 initializer before registering the member declaration
23899 since the member declaration should be in scope while
23900 its initializer is processed. However, the rest of the
23901 front end does not yet provide an interface that allows
23902 us to handle this correctly. */
23903 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23907 A pure-specifier shall be used only in the declaration of
23908 a virtual function.
23910 A member-declarator can contain a constant-initializer
23911 only if it declares a static member of integral or
23914 Therefore, if the DECLARATOR is for a function, we look
23915 for a pure-specifier; otherwise, we look for a
23916 constant-initializer. When we call `grokfield', it will
23917 perform more stringent semantics checks. */
23918 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23919 if (function_declarator_p (declarator)
23920 || (decl_specifiers.type
23921 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23922 && declarator->kind == cdk_id
23923 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23924 == FUNCTION_TYPE)))
23925 initializer = cp_parser_pure_specifier (parser);
23926 else if (decl_specifiers.storage_class != sc_static)
23927 initializer = cp_parser_save_nsdmi (parser);
23928 else if (cxx_dialect >= cxx11)
23931 /* Don't require a constant rvalue in C++11, since we
23932 might want a reference constant. We'll enforce
23933 constancy later. */
23934 cp_lexer_consume_token (parser->lexer);
23935 /* Parse the initializer. */
23936 initializer = cp_parser_initializer_clause (parser,
23940 /* Parse the initializer. */
23941 initializer = cp_parser_constant_initializer (parser);
23943 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23944 && !function_declarator_p (declarator))
23947 if (decl_specifiers.storage_class != sc_static)
23948 initializer = cp_parser_save_nsdmi (parser);
23950 initializer = cp_parser_initializer (parser, &x, &x);
23952 /* Otherwise, there is no initializer. */
23954 initializer = NULL_TREE;
23956 /* See if we are probably looking at a function
23957 definition. We are certainly not looking at a
23958 member-declarator. Calling `grokfield' has
23959 side-effects, so we must not do it unless we are sure
23960 that we are looking at a member-declarator. */
23961 if (cp_parser_token_starts_function_definition_p
23962 (cp_lexer_peek_token (parser->lexer)))
23964 /* The grammar does not allow a pure-specifier to be
23965 used when a member function is defined. (It is
23966 possible that this fact is an oversight in the
23967 standard, since a pure function may be defined
23968 outside of the class-specifier. */
23969 if (initializer && initializer_token_start)
23970 error_at (initializer_token_start->location,
23971 "pure-specifier on function-definition");
23972 decl = cp_parser_save_member_function_body (parser,
23976 if (parser->fully_implicit_function_template_p)
23977 decl = finish_fully_implicit_template (parser, decl);
23978 /* If the member was not a friend, declare it here. */
23980 finish_member_declaration (decl);
23981 /* Peek at the next token. */
23982 token = cp_lexer_peek_token (parser->lexer);
23983 /* If the next token is a semicolon, consume it. */
23984 if (token->type == CPP_SEMICOLON)
23986 location_t semicolon_loc
23987 = cp_lexer_consume_token (parser->lexer)->location;
23988 gcc_rich_location richloc (semicolon_loc);
23989 richloc.add_fixit_remove ();
23990 warning_at (&richloc, OPT_Wextra_semi,
23991 "extra %<;%> after in-class "
23992 "function definition");
23997 if (declarator->kind == cdk_function)
23998 declarator->id_loc = token->location;
23999 /* Create the declaration. */
24000 decl = grokfield (declarator, &decl_specifiers,
24001 initializer, /*init_const_expr_p=*/true,
24002 asm_specification, attributes);
24003 if (parser->fully_implicit_function_template_p)
24006 finish_fully_implicit_template (parser, 0);
24008 decl = finish_fully_implicit_template (parser, decl);
24012 cp_finalize_omp_declare_simd (parser, decl);
24013 cp_finalize_oacc_routine (parser, decl, false);
24015 /* Reset PREFIX_ATTRIBUTES. */
24016 if (attributes != error_mark_node)
24018 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24019 attributes = TREE_CHAIN (attributes);
24021 TREE_CHAIN (attributes) = NULL_TREE;
24024 /* If there is any qualification still in effect, clear it
24025 now; we will be starting fresh with the next declarator. */
24026 parser->scope = NULL_TREE;
24027 parser->qualifying_scope = NULL_TREE;
24028 parser->object_scope = NULL_TREE;
24029 /* If it's a `,', then there are more declarators. */
24030 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24032 cp_lexer_consume_token (parser->lexer);
24033 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24035 cp_token *token = cp_lexer_previous_token (parser->lexer);
24036 gcc_rich_location richloc (token->location);
24037 richloc.add_fixit_remove ();
24038 error_at (&richloc, "stray %<,%> at end of "
24039 "member declaration");
24042 /* If the next token isn't a `;', then we have a parse error. */
24043 else if (cp_lexer_next_token_is_not (parser->lexer,
24046 /* The next token might be a ways away from where the
24047 actual semicolon is missing. Find the previous token
24048 and use that for our error position. */
24049 cp_token *token = cp_lexer_previous_token (parser->lexer);
24050 gcc_rich_location richloc (token->location);
24051 richloc.add_fixit_insert_after (";");
24052 error_at (&richloc, "expected %<;%> at end of "
24053 "member declaration");
24055 /* Assume that the user meant to provide a semicolon. If
24056 we were to cp_parser_skip_to_end_of_statement, we might
24057 skip to a semicolon inside a member function definition
24058 and issue nonsensical error messages. */
24059 assume_semicolon = true;
24064 /* Add DECL to the list of members. */
24066 /* Explicitly include, eg, NSDMIs, for better error
24067 recovery (c++/58650). */
24068 || !DECL_DECLARES_FUNCTION_P (decl))
24069 finish_member_declaration (decl);
24071 if (TREE_CODE (decl) == FUNCTION_DECL)
24072 cp_parser_save_default_args (parser, decl);
24073 else if (TREE_CODE (decl) == FIELD_DECL
24074 && DECL_INITIAL (decl))
24075 /* Add DECL to the queue of NSDMI to be parsed later. */
24076 vec_safe_push (unparsed_nsdmis, decl);
24079 if (assume_semicolon)
24084 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24086 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24089 /* Parse a pure-specifier.
24094 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24095 Otherwise, ERROR_MARK_NODE is returned. */
24098 cp_parser_pure_specifier (cp_parser* parser)
24102 /* Look for the `=' token. */
24103 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24104 return error_mark_node;
24105 /* Look for the `0' token. */
24106 token = cp_lexer_peek_token (parser->lexer);
24108 if (token->type == CPP_EOF
24109 || token->type == CPP_PRAGMA_EOL)
24110 return error_mark_node;
24112 cp_lexer_consume_token (parser->lexer);
24114 /* Accept = default or = delete in c++0x mode. */
24115 if (token->keyword == RID_DEFAULT
24116 || token->keyword == RID_DELETE)
24118 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24119 return token->u.value;
24122 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24123 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24125 cp_parser_error (parser,
24126 "invalid pure specifier (only %<= 0%> is allowed)");
24127 cp_parser_skip_to_end_of_statement (parser);
24128 return error_mark_node;
24130 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24132 error_at (token->location, "templates may not be %<virtual%>");
24133 return error_mark_node;
24136 return integer_zero_node;
24139 /* Parse a constant-initializer.
24141 constant-initializer:
24142 = constant-expression
24144 Returns a representation of the constant-expression. */
24147 cp_parser_constant_initializer (cp_parser* parser)
24149 /* Look for the `=' token. */
24150 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24151 return error_mark_node;
24153 /* It is invalid to write:
24155 struct S { static const int i = { 7 }; };
24158 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24160 cp_parser_error (parser,
24161 "a brace-enclosed initializer is not allowed here");
24162 /* Consume the opening brace. */
24163 matching_braces braces;
24164 braces.consume_open (parser);
24165 /* Skip the initializer. */
24166 cp_parser_skip_to_closing_brace (parser);
24167 /* Look for the trailing `}'. */
24168 braces.require_close (parser);
24170 return error_mark_node;
24173 return cp_parser_constant_expression (parser);
24176 /* Derived classes [gram.class.derived] */
24178 /* Parse a base-clause.
24181 : base-specifier-list
24183 base-specifier-list:
24184 base-specifier ... [opt]
24185 base-specifier-list , base-specifier ... [opt]
24187 Returns a TREE_LIST representing the base-classes, in the order in
24188 which they were declared. The representation of each node is as
24189 described by cp_parser_base_specifier.
24191 In the case that no bases are specified, this function will return
24192 NULL_TREE, not ERROR_MARK_NODE. */
24195 cp_parser_base_clause (cp_parser* parser)
24197 tree bases = NULL_TREE;
24199 /* Look for the `:' that begins the list. */
24200 cp_parser_require (parser, CPP_COLON, RT_COLON);
24202 /* Scan the base-specifier-list. */
24207 bool pack_expansion_p = false;
24209 /* Look for the base-specifier. */
24210 base = cp_parser_base_specifier (parser);
24211 /* Look for the (optional) ellipsis. */
24212 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24214 /* Consume the `...'. */
24215 cp_lexer_consume_token (parser->lexer);
24217 pack_expansion_p = true;
24220 /* Add BASE to the front of the list. */
24221 if (base && base != error_mark_node)
24223 if (pack_expansion_p)
24224 /* Make this a pack expansion type. */
24225 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24227 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24229 TREE_CHAIN (base) = bases;
24233 /* Peek at the next token. */
24234 token = cp_lexer_peek_token (parser->lexer);
24235 /* If it's not a comma, then the list is complete. */
24236 if (token->type != CPP_COMMA)
24238 /* Consume the `,'. */
24239 cp_lexer_consume_token (parser->lexer);
24242 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24243 base class had a qualified name. However, the next name that
24244 appears is certainly not qualified. */
24245 parser->scope = NULL_TREE;
24246 parser->qualifying_scope = NULL_TREE;
24247 parser->object_scope = NULL_TREE;
24249 return nreverse (bases);
24252 /* Parse a base-specifier.
24255 :: [opt] nested-name-specifier [opt] class-name
24256 virtual access-specifier [opt] :: [opt] nested-name-specifier
24258 access-specifier virtual [opt] :: [opt] nested-name-specifier
24261 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24262 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24263 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24264 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24267 cp_parser_base_specifier (cp_parser* parser)
24271 bool virtual_p = false;
24272 bool duplicate_virtual_error_issued_p = false;
24273 bool duplicate_access_error_issued_p = false;
24274 bool class_scope_p, template_p;
24275 tree access = access_default_node;
24278 /* Process the optional `virtual' and `access-specifier'. */
24281 /* Peek at the next token. */
24282 token = cp_lexer_peek_token (parser->lexer);
24283 /* Process `virtual'. */
24284 switch (token->keyword)
24287 /* If `virtual' appears more than once, issue an error. */
24288 if (virtual_p && !duplicate_virtual_error_issued_p)
24290 cp_parser_error (parser,
24291 "%<virtual%> specified more than once in base-specifier");
24292 duplicate_virtual_error_issued_p = true;
24297 /* Consume the `virtual' token. */
24298 cp_lexer_consume_token (parser->lexer);
24303 case RID_PROTECTED:
24305 /* If more than one access specifier appears, issue an
24307 if (access != access_default_node
24308 && !duplicate_access_error_issued_p)
24310 cp_parser_error (parser,
24311 "more than one access specifier in base-specifier");
24312 duplicate_access_error_issued_p = true;
24315 access = ridpointers[(int) token->keyword];
24317 /* Consume the access-specifier. */
24318 cp_lexer_consume_token (parser->lexer);
24327 /* It is not uncommon to see programs mechanically, erroneously, use
24328 the 'typename' keyword to denote (dependent) qualified types
24329 as base classes. */
24330 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24332 token = cp_lexer_peek_token (parser->lexer);
24333 if (!processing_template_decl)
24334 error_at (token->location,
24335 "keyword %<typename%> not allowed outside of templates");
24337 error_at (token->location,
24338 "keyword %<typename%> not allowed in this context "
24339 "(the base class is implicitly a type)");
24340 cp_lexer_consume_token (parser->lexer);
24343 /* Look for the optional `::' operator. */
24344 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24345 /* Look for the nested-name-specifier. The simplest way to
24350 The keyword `typename' is not permitted in a base-specifier or
24351 mem-initializer; in these contexts a qualified name that
24352 depends on a template-parameter is implicitly assumed to be a
24355 is to pretend that we have seen the `typename' keyword at this
24357 cp_parser_nested_name_specifier_opt (parser,
24358 /*typename_keyword_p=*/true,
24359 /*check_dependency_p=*/true,
24361 /*is_declaration=*/true);
24362 /* If the base class is given by a qualified name, assume that names
24363 we see are type names or templates, as appropriate. */
24364 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24365 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24368 && cp_lexer_next_token_is_decltype (parser->lexer))
24369 /* DR 950 allows decltype as a base-specifier. */
24370 type = cp_parser_decltype (parser);
24373 /* Otherwise, look for the class-name. */
24374 type = cp_parser_class_name (parser,
24378 /*check_dependency_p=*/true,
24379 /*class_head_p=*/false,
24380 /*is_declaration=*/true);
24381 type = TREE_TYPE (type);
24384 if (type == error_mark_node)
24385 return error_mark_node;
24387 return finish_base_specifier (type, access, virtual_p);
24390 /* Exception handling [gram.exception] */
24392 /* Parse an (optional) noexcept-specification.
24394 noexcept-specification:
24395 noexcept ( constant-expression ) [opt]
24397 If no noexcept-specification is present, returns NULL_TREE.
24398 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24399 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24400 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24401 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24402 in which case a boolean condition is returned instead. */
24405 cp_parser_noexcept_specification_opt (cp_parser* parser,
24406 bool require_constexpr,
24407 bool* consumed_expr,
24411 const char *saved_message;
24413 /* Peek at the next token. */
24414 token = cp_lexer_peek_token (parser->lexer);
24416 /* Is it a noexcept-specification? */
24417 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24420 cp_lexer_consume_token (parser->lexer);
24422 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24424 matching_parens parens;
24425 parens.consume_open (parser);
24427 if (require_constexpr)
24429 /* Types may not be defined in an exception-specification. */
24430 saved_message = parser->type_definition_forbidden_message;
24431 parser->type_definition_forbidden_message
24432 = G_("types may not be defined in an exception-specification");
24434 expr = cp_parser_constant_expression (parser);
24436 /* Restore the saved message. */
24437 parser->type_definition_forbidden_message = saved_message;
24441 expr = cp_parser_expression (parser);
24442 *consumed_expr = true;
24445 parens.require_close (parser);
24449 expr = boolean_true_node;
24450 if (!require_constexpr)
24451 *consumed_expr = false;
24454 /* We cannot build a noexcept-spec right away because this will check
24455 that expr is a constexpr. */
24457 return build_noexcept_spec (expr, tf_warning_or_error);
24465 /* Parse an (optional) exception-specification.
24467 exception-specification:
24468 throw ( type-id-list [opt] )
24470 Returns a TREE_LIST representing the exception-specification. The
24471 TREE_VALUE of each node is a type. */
24474 cp_parser_exception_specification_opt (cp_parser* parser)
24478 const char *saved_message;
24480 /* Peek at the next token. */
24481 token = cp_lexer_peek_token (parser->lexer);
24483 /* Is it a noexcept-specification? */
24484 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24486 if (type_id_list != NULL_TREE)
24487 return type_id_list;
24489 /* If it's not `throw', then there's no exception-specification. */
24490 if (!cp_parser_is_keyword (token, RID_THROW))
24493 location_t loc = token->location;
24495 /* Consume the `throw'. */
24496 cp_lexer_consume_token (parser->lexer);
24498 /* Look for the `('. */
24499 matching_parens parens;
24500 parens.require_open (parser);
24502 /* Peek at the next token. */
24503 token = cp_lexer_peek_token (parser->lexer);
24504 /* If it's not a `)', then there is a type-id-list. */
24505 if (token->type != CPP_CLOSE_PAREN)
24507 /* Types may not be defined in an exception-specification. */
24508 saved_message = parser->type_definition_forbidden_message;
24509 parser->type_definition_forbidden_message
24510 = G_("types may not be defined in an exception-specification");
24511 /* Parse the type-id-list. */
24512 type_id_list = cp_parser_type_id_list (parser);
24513 /* Restore the saved message. */
24514 parser->type_definition_forbidden_message = saved_message;
24516 if (cxx_dialect >= cxx17)
24518 error_at (loc, "ISO C++17 does not allow dynamic exception "
24520 type_id_list = NULL_TREE;
24522 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24523 warning_at (loc, OPT_Wdeprecated,
24524 "dynamic exception specifications are deprecated in "
24527 /* In C++17, throw() is equivalent to noexcept (true). throw()
24528 is deprecated in C++11 and above as well, but is still widely used,
24529 so don't warn about it yet. */
24530 else if (cxx_dialect >= cxx17)
24531 type_id_list = noexcept_true_spec;
24533 type_id_list = empty_except_spec;
24535 /* Look for the `)'. */
24536 parens.require_close (parser);
24538 return type_id_list;
24541 /* Parse an (optional) type-id-list.
24545 type-id-list , type-id ... [opt]
24547 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24548 in the order that the types were presented. */
24551 cp_parser_type_id_list (cp_parser* parser)
24553 tree types = NULL_TREE;
24560 token = cp_lexer_peek_token (parser->lexer);
24562 /* Get the next type-id. */
24563 type = cp_parser_type_id (parser);
24564 /* Check for invalid 'auto'. */
24565 if (flag_concepts && type_uses_auto (type))
24567 error_at (token->location,
24568 "invalid use of %<auto%> in exception-specification");
24569 type = error_mark_node;
24571 /* Parse the optional ellipsis. */
24572 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24574 /* Consume the `...'. */
24575 cp_lexer_consume_token (parser->lexer);
24577 /* Turn the type into a pack expansion expression. */
24578 type = make_pack_expansion (type);
24580 /* Add it to the list. */
24581 types = add_exception_specifier (types, type, /*complain=*/1);
24582 /* Peek at the next token. */
24583 token = cp_lexer_peek_token (parser->lexer);
24584 /* If it is not a `,', we are done. */
24585 if (token->type != CPP_COMMA)
24587 /* Consume the `,'. */
24588 cp_lexer_consume_token (parser->lexer);
24591 return nreverse (types);
24594 /* Parse a try-block.
24597 try compound-statement handler-seq */
24600 cp_parser_try_block (cp_parser* parser)
24604 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24605 if (parser->in_function_body
24606 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24607 error ("%<try%> in %<constexpr%> function");
24609 try_block = begin_try_block ();
24610 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24611 finish_try_block (try_block);
24612 cp_parser_handler_seq (parser);
24613 finish_handler_sequence (try_block);
24618 /* Parse a function-try-block.
24620 function-try-block:
24621 try ctor-initializer [opt] function-body handler-seq */
24624 cp_parser_function_try_block (cp_parser* parser)
24626 tree compound_stmt;
24629 /* Look for the `try' keyword. */
24630 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24632 /* Let the rest of the front end know where we are. */
24633 try_block = begin_function_try_block (&compound_stmt);
24634 /* Parse the function-body. */
24635 cp_parser_ctor_initializer_opt_and_function_body
24636 (parser, /*in_function_try_block=*/true);
24637 /* We're done with the `try' part. */
24638 finish_function_try_block (try_block);
24639 /* Parse the handlers. */
24640 cp_parser_handler_seq (parser);
24641 /* We're done with the handlers. */
24642 finish_function_handler_sequence (try_block, compound_stmt);
24645 /* Parse a handler-seq.
24648 handler handler-seq [opt] */
24651 cp_parser_handler_seq (cp_parser* parser)
24657 /* Parse the handler. */
24658 cp_parser_handler (parser);
24659 /* Peek at the next token. */
24660 token = cp_lexer_peek_token (parser->lexer);
24661 /* If it's not `catch' then there are no more handlers. */
24662 if (!cp_parser_is_keyword (token, RID_CATCH))
24667 /* Parse a handler.
24670 catch ( exception-declaration ) compound-statement */
24673 cp_parser_handler (cp_parser* parser)
24678 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24679 handler = begin_handler ();
24680 matching_parens parens;
24681 parens.require_open (parser);
24682 declaration = cp_parser_exception_declaration (parser);
24683 finish_handler_parms (declaration, handler);
24684 parens.require_close (parser);
24685 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24686 finish_handler (handler);
24689 /* Parse an exception-declaration.
24691 exception-declaration:
24692 type-specifier-seq declarator
24693 type-specifier-seq abstract-declarator
24697 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24698 ellipsis variant is used. */
24701 cp_parser_exception_declaration (cp_parser* parser)
24703 cp_decl_specifier_seq type_specifiers;
24704 cp_declarator *declarator;
24705 const char *saved_message;
24707 /* If it's an ellipsis, it's easy to handle. */
24708 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24710 /* Consume the `...' token. */
24711 cp_lexer_consume_token (parser->lexer);
24715 /* Types may not be defined in exception-declarations. */
24716 saved_message = parser->type_definition_forbidden_message;
24717 parser->type_definition_forbidden_message
24718 = G_("types may not be defined in exception-declarations");
24720 /* Parse the type-specifier-seq. */
24721 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24722 /*is_trailing_return=*/false,
24724 /* If it's a `)', then there is no declarator. */
24725 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24728 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24729 /*ctor_dtor_or_conv_p=*/NULL,
24730 /*parenthesized_p=*/NULL,
24731 /*member_p=*/false,
24732 /*friend_p=*/false);
24734 /* Restore the saved message. */
24735 parser->type_definition_forbidden_message = saved_message;
24737 if (!type_specifiers.any_specifiers_p)
24738 return error_mark_node;
24740 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24743 /* Parse a throw-expression.
24746 throw assignment-expression [opt]
24748 Returns a THROW_EXPR representing the throw-expression. */
24751 cp_parser_throw_expression (cp_parser* parser)
24756 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24757 token = cp_lexer_peek_token (parser->lexer);
24758 /* Figure out whether or not there is an assignment-expression
24759 following the "throw" keyword. */
24760 if (token->type == CPP_COMMA
24761 || token->type == CPP_SEMICOLON
24762 || token->type == CPP_CLOSE_PAREN
24763 || token->type == CPP_CLOSE_SQUARE
24764 || token->type == CPP_CLOSE_BRACE
24765 || token->type == CPP_COLON)
24766 expression = NULL_TREE;
24768 expression = cp_parser_assignment_expression (parser);
24770 return build_throw (expression);
24773 /* GNU Extensions */
24775 /* Parse an (optional) asm-specification.
24778 asm ( string-literal )
24780 If the asm-specification is present, returns a STRING_CST
24781 corresponding to the string-literal. Otherwise, returns
24785 cp_parser_asm_specification_opt (cp_parser* parser)
24788 tree asm_specification;
24790 /* Peek at the next token. */
24791 token = cp_lexer_peek_token (parser->lexer);
24792 /* If the next token isn't the `asm' keyword, then there's no
24793 asm-specification. */
24794 if (!cp_parser_is_keyword (token, RID_ASM))
24797 /* Consume the `asm' token. */
24798 cp_lexer_consume_token (parser->lexer);
24799 /* Look for the `('. */
24800 matching_parens parens;
24801 parens.require_open (parser);
24803 /* Look for the string-literal. */
24804 asm_specification = cp_parser_string_literal (parser, false, false);
24806 /* Look for the `)'. */
24807 parens.require_close (parser);
24809 return asm_specification;
24812 /* Parse an asm-operand-list.
24816 asm-operand-list , asm-operand
24819 string-literal ( expression )
24820 [ string-literal ] string-literal ( expression )
24822 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24823 each node is the expression. The TREE_PURPOSE is itself a
24824 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24825 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24826 is a STRING_CST for the string literal before the parenthesis. Returns
24827 ERROR_MARK_NODE if any of the operands are invalid. */
24830 cp_parser_asm_operand_list (cp_parser* parser)
24832 tree asm_operands = NULL_TREE;
24833 bool invalid_operands = false;
24837 tree string_literal;
24841 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24843 /* Consume the `[' token. */
24844 cp_lexer_consume_token (parser->lexer);
24845 /* Read the operand name. */
24846 name = cp_parser_identifier (parser);
24847 if (name != error_mark_node)
24848 name = build_string (IDENTIFIER_LENGTH (name),
24849 IDENTIFIER_POINTER (name));
24850 /* Look for the closing `]'. */
24851 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24855 /* Look for the string-literal. */
24856 string_literal = cp_parser_string_literal (parser, false, false);
24858 /* Look for the `('. */
24859 matching_parens parens;
24860 parens.require_open (parser);
24861 /* Parse the expression. */
24862 expression = cp_parser_expression (parser);
24863 /* Look for the `)'. */
24864 parens.require_close (parser);
24866 if (name == error_mark_node
24867 || string_literal == error_mark_node
24868 || expression == error_mark_node)
24869 invalid_operands = true;
24871 /* Add this operand to the list. */
24872 asm_operands = tree_cons (build_tree_list (name, string_literal),
24875 /* If the next token is not a `,', there are no more
24877 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24879 /* Consume the `,'. */
24880 cp_lexer_consume_token (parser->lexer);
24883 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24886 /* Parse an asm-clobber-list.
24890 asm-clobber-list , string-literal
24892 Returns a TREE_LIST, indicating the clobbers in the order that they
24893 appeared. The TREE_VALUE of each node is a STRING_CST. */
24896 cp_parser_asm_clobber_list (cp_parser* parser)
24898 tree clobbers = NULL_TREE;
24902 tree string_literal;
24904 /* Look for the string literal. */
24905 string_literal = cp_parser_string_literal (parser, false, false);
24906 /* Add it to the list. */
24907 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24908 /* If the next token is not a `,', then the list is
24910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24912 /* Consume the `,' token. */
24913 cp_lexer_consume_token (parser->lexer);
24919 /* Parse an asm-label-list.
24923 asm-label-list , identifier
24925 Returns a TREE_LIST, indicating the labels in the order that they
24926 appeared. The TREE_VALUE of each node is a label. */
24929 cp_parser_asm_label_list (cp_parser* parser)
24931 tree labels = NULL_TREE;
24935 tree identifier, label, name;
24937 /* Look for the identifier. */
24938 identifier = cp_parser_identifier (parser);
24939 if (!error_operand_p (identifier))
24941 label = lookup_label (identifier);
24942 if (TREE_CODE (label) == LABEL_DECL)
24944 TREE_USED (label) = 1;
24945 check_goto (label);
24946 name = build_string (IDENTIFIER_LENGTH (identifier),
24947 IDENTIFIER_POINTER (identifier));
24948 labels = tree_cons (name, label, labels);
24951 /* If the next token is not a `,', then the list is
24953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24955 /* Consume the `,' token. */
24956 cp_lexer_consume_token (parser->lexer);
24959 return nreverse (labels);
24962 /* Return TRUE iff the next tokens in the stream are possibly the
24963 beginning of a GNU extension attribute. */
24966 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24968 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24971 /* Return TRUE iff the next tokens in the stream are possibly the
24972 beginning of a standard C++-11 attribute specifier. */
24975 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24977 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24980 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24981 beginning of a standard C++-11 attribute specifier. */
24984 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24986 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24988 return (cxx_dialect >= cxx11
24989 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24990 || (token->type == CPP_OPEN_SQUARE
24991 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24992 && token->type == CPP_OPEN_SQUARE)));
24995 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24996 beginning of a GNU extension attribute. */
24999 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25001 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25003 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25006 /* Return true iff the next tokens can be the beginning of either a
25007 GNU attribute list, or a standard C++11 attribute sequence. */
25010 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25012 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25013 || cp_next_tokens_can_be_std_attribute_p (parser));
25016 /* Return true iff the next Nth tokens can be the beginning of either
25017 a GNU attribute list, or a standard C++11 attribute sequence. */
25020 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25022 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25023 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25026 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25027 of GNU attributes, or return NULL. */
25030 cp_parser_attributes_opt (cp_parser *parser)
25032 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25033 return cp_parser_gnu_attributes_opt (parser);
25034 return cp_parser_std_attribute_spec_seq (parser);
25037 /* Parse an (optional) series of attributes.
25040 attributes attribute
25043 __attribute__ (( attribute-list [opt] ))
25045 The return value is as for cp_parser_gnu_attribute_list. */
25048 cp_parser_gnu_attributes_opt (cp_parser* parser)
25050 tree attributes = NULL_TREE;
25052 temp_override<bool> cleanup
25053 (parser->auto_is_implicit_function_template_parm_p, false);
25058 tree attribute_list;
25061 /* Peek at the next token. */
25062 token = cp_lexer_peek_token (parser->lexer);
25063 /* If it's not `__attribute__', then we're done. */
25064 if (token->keyword != RID_ATTRIBUTE)
25067 /* Consume the `__attribute__' keyword. */
25068 cp_lexer_consume_token (parser->lexer);
25069 /* Look for the two `(' tokens. */
25070 matching_parens outer_parens;
25071 outer_parens.require_open (parser);
25072 matching_parens inner_parens;
25073 inner_parens.require_open (parser);
25075 /* Peek at the next token. */
25076 token = cp_lexer_peek_token (parser->lexer);
25077 if (token->type != CPP_CLOSE_PAREN)
25078 /* Parse the attribute-list. */
25079 attribute_list = cp_parser_gnu_attribute_list (parser);
25081 /* If the next token is a `)', then there is no attribute
25083 attribute_list = NULL;
25085 /* Look for the two `)' tokens. */
25086 if (!inner_parens.require_close (parser))
25088 if (!outer_parens.require_close (parser))
25091 cp_parser_skip_to_end_of_statement (parser);
25093 /* Add these new attributes to the list. */
25094 attributes = attr_chainon (attributes, attribute_list);
25100 /* Parse a GNU attribute-list.
25104 attribute-list , attribute
25108 identifier ( identifier )
25109 identifier ( identifier , expression-list )
25110 identifier ( expression-list )
25112 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25113 to an attribute. The TREE_PURPOSE of each node is the identifier
25114 indicating which attribute is in use. The TREE_VALUE represents
25115 the arguments, if any. */
25118 cp_parser_gnu_attribute_list (cp_parser* parser)
25120 tree attribute_list = NULL_TREE;
25121 bool save_translate_strings_p = parser->translate_strings_p;
25123 parser->translate_strings_p = false;
25130 /* Look for the identifier. We also allow keywords here; for
25131 example `__attribute__ ((const))' is legal. */
25132 token = cp_lexer_peek_token (parser->lexer);
25133 if (token->type == CPP_NAME
25134 || token->type == CPP_KEYWORD)
25136 tree arguments = NULL_TREE;
25138 /* Consume the token, but save it since we need it for the
25139 SIMD enabled function parsing. */
25140 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25142 /* Save away the identifier that indicates which attribute
25144 identifier = (token->type == CPP_KEYWORD)
25145 /* For keywords, use the canonical spelling, not the
25146 parsed identifier. */
25147 ? ridpointers[(int) token->keyword]
25148 : id_token->u.value;
25150 identifier = canonicalize_attr_name (identifier);
25151 attribute = build_tree_list (identifier, NULL_TREE);
25153 /* Peek at the next token. */
25154 token = cp_lexer_peek_token (parser->lexer);
25155 /* If it's an `(', then parse the attribute arguments. */
25156 if (token->type == CPP_OPEN_PAREN)
25158 vec<tree, va_gc> *vec;
25159 int attr_flag = (attribute_takes_identifier_p (identifier)
25160 ? id_attr : normal_attr);
25161 vec = cp_parser_parenthesized_expression_list
25162 (parser, attr_flag, /*cast_p=*/false,
25163 /*allow_expansion_p=*/false,
25164 /*non_constant_p=*/NULL);
25166 arguments = error_mark_node;
25169 arguments = build_tree_list_vec (vec);
25170 release_tree_vector (vec);
25172 /* Save the arguments away. */
25173 TREE_VALUE (attribute) = arguments;
25176 if (arguments != error_mark_node)
25178 /* Add this attribute to the list. */
25179 TREE_CHAIN (attribute) = attribute_list;
25180 attribute_list = attribute;
25183 token = cp_lexer_peek_token (parser->lexer);
25185 /* Now, look for more attributes. If the next token isn't a
25186 `,', we're done. */
25187 if (token->type != CPP_COMMA)
25190 /* Consume the comma and keep going. */
25191 cp_lexer_consume_token (parser->lexer);
25193 parser->translate_strings_p = save_translate_strings_p;
25195 /* We built up the list in reverse order. */
25196 return nreverse (attribute_list);
25199 /* Parse a standard C++11 attribute.
25201 The returned representation is a TREE_LIST which TREE_PURPOSE is
25202 the scoped name of the attribute, and the TREE_VALUE is its
25205 Note that the scoped name of the attribute is itself a TREE_LIST
25206 which TREE_PURPOSE is the namespace of the attribute, and
25207 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25208 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25209 and which TREE_PURPOSE is directly the attribute name.
25211 Clients of the attribute code should use get_attribute_namespace
25212 and get_attribute_name to get the actual namespace and name of
25213 attributes, regardless of their being GNU or C++11 attributes.
25216 attribute-token attribute-argument-clause [opt]
25220 attribute-scoped-token
25222 attribute-scoped-token:
25223 attribute-namespace :: identifier
25225 attribute-namespace:
25228 attribute-argument-clause:
25229 ( balanced-token-seq )
25231 balanced-token-seq:
25232 balanced-token [opt]
25233 balanced-token-seq balanced-token
25236 ( balanced-token-seq )
25237 [ balanced-token-seq ]
25238 { balanced-token-seq }. */
25241 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25243 tree attribute, attr_id = NULL_TREE, arguments;
25246 temp_override<bool> cleanup
25247 (parser->auto_is_implicit_function_template_parm_p, false);
25249 /* First, parse name of the attribute, a.k.a attribute-token. */
25251 token = cp_lexer_peek_token (parser->lexer);
25252 if (token->type == CPP_NAME)
25253 attr_id = token->u.value;
25254 else if (token->type == CPP_KEYWORD)
25255 attr_id = ridpointers[(int) token->keyword];
25256 else if (token->flags & NAMED_OP)
25257 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25259 if (attr_id == NULL_TREE)
25262 cp_lexer_consume_token (parser->lexer);
25264 token = cp_lexer_peek_token (parser->lexer);
25265 if (token->type == CPP_SCOPE)
25267 /* We are seeing a scoped attribute token. */
25269 cp_lexer_consume_token (parser->lexer);
25271 error_at (token->location, "attribute using prefix used together "
25272 "with scoped attribute token");
25275 token = cp_lexer_consume_token (parser->lexer);
25276 if (token->type == CPP_NAME)
25277 attr_id = token->u.value;
25278 else if (token->type == CPP_KEYWORD)
25279 attr_id = ridpointers[(int) token->keyword];
25280 else if (token->flags & NAMED_OP)
25281 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25284 error_at (token->location,
25285 "expected an identifier for the attribute name");
25286 return error_mark_node;
25289 attr_ns = canonicalize_attr_name (attr_ns);
25290 attr_id = canonicalize_attr_name (attr_id);
25291 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25293 token = cp_lexer_peek_token (parser->lexer);
25297 attr_ns = canonicalize_attr_name (attr_ns);
25298 attr_id = canonicalize_attr_name (attr_id);
25299 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25304 attr_id = canonicalize_attr_name (attr_id);
25305 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25307 /* C++11 noreturn attribute is equivalent to GNU's. */
25308 if (is_attribute_p ("noreturn", attr_id))
25309 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25310 /* C++14 deprecated attribute is equivalent to GNU's. */
25311 else if (is_attribute_p ("deprecated", attr_id))
25312 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25313 /* C++17 fallthrough attribute is equivalent to GNU's. */
25314 else if (is_attribute_p ("fallthrough", attr_id))
25315 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25316 /* Transactional Memory TS optimize_for_synchronized attribute is
25317 equivalent to GNU transaction_callable. */
25318 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25319 TREE_PURPOSE (attribute)
25320 = get_identifier ("transaction_callable");
25321 /* Transactional Memory attributes are GNU attributes. */
25322 else if (tm_attr_to_mask (attr_id))
25323 TREE_PURPOSE (attribute) = attr_id;
25326 /* Now parse the optional argument clause of the attribute. */
25328 if (token->type != CPP_OPEN_PAREN)
25332 vec<tree, va_gc> *vec;
25333 int attr_flag = normal_attr;
25335 if (attr_ns == get_identifier ("gnu")
25336 && attribute_takes_identifier_p (attr_id))
25337 /* A GNU attribute that takes an identifier in parameter. */
25338 attr_flag = id_attr;
25340 vec = cp_parser_parenthesized_expression_list
25341 (parser, attr_flag, /*cast_p=*/false,
25342 /*allow_expansion_p=*/true,
25343 /*non_constant_p=*/NULL);
25345 arguments = error_mark_node;
25348 arguments = build_tree_list_vec (vec);
25349 release_tree_vector (vec);
25352 if (arguments == error_mark_node)
25353 attribute = error_mark_node;
25355 TREE_VALUE (attribute) = arguments;
25361 /* Check that the attribute ATTRIBUTE appears at most once in the
25362 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25363 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25364 isn't implemented yet in GCC. */
25367 cp_parser_check_std_attribute (tree attributes, tree attribute)
25371 tree name = get_attribute_name (attribute);
25372 if (is_attribute_p ("noreturn", name)
25373 && lookup_attribute ("noreturn", attributes))
25374 error ("attribute %<noreturn%> can appear at most once "
25375 "in an attribute-list");
25376 else if (is_attribute_p ("deprecated", name)
25377 && lookup_attribute ("deprecated", attributes))
25378 error ("attribute %<deprecated%> can appear at most once "
25379 "in an attribute-list");
25383 /* Parse a list of standard C++-11 attributes.
25387 attribute-list , attribute[opt]
25389 attribute-list , attribute ...
25393 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25395 tree attributes = NULL_TREE, attribute = NULL_TREE;
25396 cp_token *token = NULL;
25400 attribute = cp_parser_std_attribute (parser, attr_ns);
25401 if (attribute == error_mark_node)
25403 if (attribute != NULL_TREE)
25405 cp_parser_check_std_attribute (attributes, attribute);
25406 TREE_CHAIN (attribute) = attributes;
25407 attributes = attribute;
25409 token = cp_lexer_peek_token (parser->lexer);
25410 if (token->type == CPP_ELLIPSIS)
25412 cp_lexer_consume_token (parser->lexer);
25413 if (attribute == NULL_TREE)
25414 error_at (token->location,
25415 "expected attribute before %<...%>");
25418 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25419 if (pack == error_mark_node)
25420 return error_mark_node;
25421 TREE_VALUE (attribute) = pack;
25423 token = cp_lexer_peek_token (parser->lexer);
25425 if (token->type != CPP_COMMA)
25427 cp_lexer_consume_token (parser->lexer);
25429 attributes = nreverse (attributes);
25433 /* Parse a standard C++-11 attribute specifier.
25435 attribute-specifier:
25436 [ [ attribute-using-prefix [opt] attribute-list ] ]
25437 alignment-specifier
25439 attribute-using-prefix:
25440 using attribute-namespace :
25442 alignment-specifier:
25443 alignas ( type-id ... [opt] )
25444 alignas ( alignment-expression ... [opt] ). */
25447 cp_parser_std_attribute_spec (cp_parser *parser)
25449 tree attributes = NULL_TREE;
25450 cp_token *token = cp_lexer_peek_token (parser->lexer);
25452 if (token->type == CPP_OPEN_SQUARE
25453 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25455 tree attr_ns = NULL_TREE;
25457 cp_lexer_consume_token (parser->lexer);
25458 cp_lexer_consume_token (parser->lexer);
25460 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25462 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25463 if (token->type == CPP_NAME)
25464 attr_ns = token->u.value;
25465 else if (token->type == CPP_KEYWORD)
25466 attr_ns = ridpointers[(int) token->keyword];
25467 else if (token->flags & NAMED_OP)
25468 attr_ns = get_identifier (cpp_type2name (token->type,
25471 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25473 if (cxx_dialect < cxx17
25474 && !in_system_header_at (input_location))
25475 pedwarn (input_location, 0,
25476 "attribute using prefix only available "
25477 "with -std=c++17 or -std=gnu++17");
25479 cp_lexer_consume_token (parser->lexer);
25480 cp_lexer_consume_token (parser->lexer);
25481 cp_lexer_consume_token (parser->lexer);
25484 attr_ns = NULL_TREE;
25487 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25489 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25490 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25491 cp_parser_skip_to_end_of_statement (parser);
25493 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25494 when we are sure that we have actually parsed them. */
25495 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25501 /* Look for an alignment-specifier. */
25503 token = cp_lexer_peek_token (parser->lexer);
25505 if (token->type != CPP_KEYWORD
25506 || token->keyword != RID_ALIGNAS)
25509 cp_lexer_consume_token (parser->lexer);
25510 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25512 matching_parens parens;
25513 if (!parens.require_open (parser))
25514 return error_mark_node;
25516 cp_parser_parse_tentatively (parser);
25517 alignas_expr = cp_parser_type_id (parser);
25519 if (!cp_parser_parse_definitely (parser))
25521 alignas_expr = cp_parser_assignment_expression (parser);
25522 if (alignas_expr == error_mark_node)
25523 cp_parser_skip_to_end_of_statement (parser);
25524 if (alignas_expr == NULL_TREE
25525 || alignas_expr == error_mark_node)
25526 return alignas_expr;
25529 alignas_expr = cxx_alignas_expr (alignas_expr);
25530 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25532 /* Handle alignas (pack...). */
25533 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25535 cp_lexer_consume_token (parser->lexer);
25536 alignas_expr = make_pack_expansion (alignas_expr);
25539 /* Something went wrong, so don't build the attribute. */
25540 if (alignas_expr == error_mark_node)
25541 return error_mark_node;
25543 if (!parens.require_close (parser))
25544 return error_mark_node;
25546 /* Build the C++-11 representation of an 'aligned'
25549 build_tree_list (build_tree_list (get_identifier ("gnu"),
25550 get_identifier ("aligned")),
25557 /* Parse a standard C++-11 attribute-specifier-seq.
25559 attribute-specifier-seq:
25560 attribute-specifier-seq [opt] attribute-specifier
25564 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25566 tree attr_specs = NULL_TREE;
25567 tree attr_last = NULL_TREE;
25571 tree attr_spec = cp_parser_std_attribute_spec (parser);
25572 if (attr_spec == NULL_TREE)
25574 if (attr_spec == error_mark_node)
25575 return error_mark_node;
25578 TREE_CHAIN (attr_last) = attr_spec;
25580 attr_specs = attr_last = attr_spec;
25581 attr_last = tree_last (attr_last);
25587 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25588 return index of the first token after balanced-token, or N on failure. */
25591 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25594 int nparens = 0, nbraces = 0, nsquares = 0;
25596 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25599 case CPP_PRAGMA_EOL:
25600 /* Ran out of tokens. */
25602 case CPP_OPEN_PAREN:
25605 case CPP_OPEN_BRACE:
25608 case CPP_OPEN_SQUARE:
25611 case CPP_CLOSE_PAREN:
25614 case CPP_CLOSE_BRACE:
25617 case CPP_CLOSE_SQUARE:
25623 while (nparens || nbraces || nsquares);
25627 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25628 return index of the first token after the GNU attribute tokens, or N on
25632 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25636 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25637 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25638 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25641 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25644 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25651 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25652 next token), return index of the first token after the standard C++11
25653 attribute tokens, or N on failure. */
25656 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25660 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25661 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25663 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25666 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25670 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25671 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25673 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25684 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25685 as the next token), return index of the first token after the attribute
25686 tokens, or N on failure. */
25689 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25691 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25692 return cp_parser_skip_gnu_attributes_opt (parser, n);
25693 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25696 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25697 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25698 current value of the PEDANTIC flag, regardless of whether or not
25699 the `__extension__' keyword is present. The caller is responsible
25700 for restoring the value of the PEDANTIC flag. */
25703 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25705 /* Save the old value of the PEDANTIC flag. */
25706 *saved_pedantic = pedantic;
25708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25710 /* Consume the `__extension__' token. */
25711 cp_lexer_consume_token (parser->lexer);
25712 /* We're not being pedantic while the `__extension__' keyword is
25722 /* Parse a label declaration.
25725 __label__ label-declarator-seq ;
25727 label-declarator-seq:
25728 identifier , label-declarator-seq
25732 cp_parser_label_declaration (cp_parser* parser)
25734 /* Look for the `__label__' keyword. */
25735 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25741 /* Look for an identifier. */
25742 identifier = cp_parser_identifier (parser);
25743 /* If we failed, stop. */
25744 if (identifier == error_mark_node)
25746 /* Declare it as a label. */
25747 finish_label_decl (identifier);
25748 /* If the next token is a `;', stop. */
25749 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25751 /* Look for the `,' separating the label declarations. */
25752 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25755 /* Look for the final `;'. */
25756 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25759 // -------------------------------------------------------------------------- //
25762 // Parse a requires clause.
25764 // requires-clause:
25765 // 'requires' logical-or-expression
25767 // The required logical-or-expression must be a constant expression. Note
25768 // that we don't check that the expression is constepxr here. We defer until
25769 // we analyze constraints and then, we only check atomic constraints.
25771 cp_parser_requires_clause (cp_parser *parser)
25773 // Parse the requires clause so that it is not automatically folded.
25774 ++processing_template_decl;
25775 tree expr = cp_parser_binary_expression (parser, false, false,
25776 PREC_NOT_OPERATOR, NULL);
25777 if (check_for_bare_parameter_packs (expr))
25778 expr = error_mark_node;
25779 --processing_template_decl;
25783 // Optionally parse a requires clause:
25785 cp_parser_requires_clause_opt (cp_parser *parser)
25787 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25788 if (tok->keyword != RID_REQUIRES)
25790 if (!flag_concepts && tok->type == CPP_NAME
25791 && tok->u.value == ridpointers[RID_REQUIRES])
25793 error_at (cp_lexer_peek_token (parser->lexer)->location,
25794 "%<requires%> only available with -fconcepts");
25795 /* Parse and discard the requires-clause. */
25796 cp_lexer_consume_token (parser->lexer);
25797 cp_parser_requires_clause (parser);
25801 cp_lexer_consume_token (parser->lexer);
25802 return cp_parser_requires_clause (parser);
25806 /*---------------------------------------------------------------------------
25807 Requires expressions
25808 ---------------------------------------------------------------------------*/
25810 /* Parse a requires expression
25812 requirement-expression:
25813 'requires' requirement-parameter-list [opt] requirement-body */
25815 cp_parser_requires_expression (cp_parser *parser)
25817 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25818 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25820 /* A requires-expression shall appear only within a concept
25821 definition or a requires-clause.
25823 TODO: Implement this diagnostic correctly. */
25824 if (!processing_template_decl)
25826 error_at (loc, "a requires expression cannot appear outside a template");
25827 cp_parser_skip_to_end_of_statement (parser);
25828 return error_mark_node;
25833 /* Local parameters are delared as variables within the scope
25834 of the expression. They are not visible past the end of
25835 the expression. Expressions within the requires-expression
25836 are unevaluated. */
25837 struct scope_sentinel
25841 ++cp_unevaluated_operand;
25842 begin_scope (sk_block, NULL_TREE);
25847 pop_bindings_and_leave_scope ();
25848 --cp_unevaluated_operand;
25852 /* Parse the optional parameter list. */
25853 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25855 parms = cp_parser_requirement_parameter_list (parser);
25856 if (parms == error_mark_node)
25857 return error_mark_node;
25862 /* Parse the requirement body. */
25863 reqs = cp_parser_requirement_body (parser);
25864 if (reqs == error_mark_node)
25865 return error_mark_node;
25868 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25870 grokparms (parms, &parms);
25871 return finish_requires_expr (parms, reqs);
25874 /* Parse a parameterized requirement.
25876 requirement-parameter-list:
25877 '(' parameter-declaration-clause ')' */
25879 cp_parser_requirement_parameter_list (cp_parser *parser)
25881 matching_parens parens;
25882 if (!parens.require_open (parser))
25883 return error_mark_node;
25885 tree parms = cp_parser_parameter_declaration_clause (parser);
25887 if (!parens.require_close (parser))
25888 return error_mark_node;
25893 /* Parse the body of a requirement.
25896 '{' requirement-list '}' */
25898 cp_parser_requirement_body (cp_parser *parser)
25900 matching_braces braces;
25901 if (!braces.require_open (parser))
25902 return error_mark_node;
25904 tree reqs = cp_parser_requirement_list (parser);
25906 if (!braces.require_close (parser))
25907 return error_mark_node;
25912 /* Parse a list of requirements.
25916 requirement-list ';' requirement[opt] */
25918 cp_parser_requirement_list (cp_parser *parser)
25920 tree result = NULL_TREE;
25923 tree req = cp_parser_requirement (parser);
25924 if (req == error_mark_node)
25925 return error_mark_node;
25927 result = tree_cons (NULL_TREE, req, result);
25929 /* If we see a semi-colon, consume it. */
25930 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25931 cp_lexer_consume_token (parser->lexer);
25933 /* Stop processing at the end of the list. */
25934 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25938 /* Reverse the order of requirements so they are analyzed in
25939 declaration order. */
25940 return nreverse (result);
25943 /* Parse a syntactic requirement or type requirement.
25947 compound-requirement
25949 nested-requirement */
25951 cp_parser_requirement (cp_parser *parser)
25953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25954 return cp_parser_compound_requirement (parser);
25955 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25956 return cp_parser_type_requirement (parser);
25957 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25958 return cp_parser_nested_requirement (parser);
25960 return cp_parser_simple_requirement (parser);
25963 /* Parse a simple requirement.
25965 simple-requirement:
25968 cp_parser_simple_requirement (cp_parser *parser)
25970 tree expr = cp_parser_expression (parser, NULL, false, false);
25971 if (!expr || expr == error_mark_node)
25972 return error_mark_node;
25974 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25975 return error_mark_node;
25977 return finish_simple_requirement (expr);
25980 /* Parse a type requirement
25983 nested-name-specifier [opt] required-type-name ';'
25985 required-type-name:
25987 'template' [opt] simple-template-id */
25989 cp_parser_type_requirement (cp_parser *parser)
25991 cp_lexer_consume_token (parser->lexer);
25993 // Save the scope before parsing name specifiers.
25994 tree saved_scope = parser->scope;
25995 tree saved_object_scope = parser->object_scope;
25996 tree saved_qualifying_scope = parser->qualifying_scope;
25997 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25998 cp_parser_nested_name_specifier_opt (parser,
25999 /*typename_keyword_p=*/true,
26000 /*check_dependency_p=*/false,
26002 /*is_declaration=*/false);
26005 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26007 cp_lexer_consume_token (parser->lexer);
26008 type = cp_parser_template_id (parser,
26009 /*template_keyword_p=*/true,
26010 /*check_dependency=*/false,
26011 /*tag_type=*/none_type,
26012 /*is_declaration=*/false);
26013 type = make_typename_type (parser->scope, type, typename_type,
26014 /*complain=*/tf_error);
26017 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26019 if (TREE_CODE (type) == TYPE_DECL)
26020 type = TREE_TYPE (type);
26022 parser->scope = saved_scope;
26023 parser->object_scope = saved_object_scope;
26024 parser->qualifying_scope = saved_qualifying_scope;
26026 if (type == error_mark_node)
26027 cp_parser_skip_to_end_of_statement (parser);
26029 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26030 return error_mark_node;
26031 if (type == error_mark_node)
26032 return error_mark_node;
26034 return finish_type_requirement (type);
26037 /* Parse a compound requirement
26039 compound-requirement:
26040 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26042 cp_parser_compound_requirement (cp_parser *parser)
26044 /* Parse an expression enclosed in '{ }'s. */
26045 matching_braces braces;
26046 if (!braces.require_open (parser))
26047 return error_mark_node;
26049 tree expr = cp_parser_expression (parser, NULL, false, false);
26050 if (!expr || expr == error_mark_node)
26051 return error_mark_node;
26053 if (!braces.require_close (parser))
26054 return error_mark_node;
26056 /* Parse the optional noexcept. */
26057 bool noexcept_p = false;
26058 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26060 cp_lexer_consume_token (parser->lexer);
26064 /* Parse the optional trailing return type. */
26065 tree type = NULL_TREE;
26066 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26068 cp_lexer_consume_token (parser->lexer);
26069 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26070 parser->in_result_type_constraint_p = true;
26071 type = cp_parser_trailing_type_id (parser);
26072 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26073 if (type == error_mark_node)
26074 return error_mark_node;
26077 return finish_compound_requirement (expr, type, noexcept_p);
26080 /* Parse a nested requirement. This is the same as a requires clause.
26082 nested-requirement:
26085 cp_parser_nested_requirement (cp_parser *parser)
26087 cp_lexer_consume_token (parser->lexer);
26088 tree req = cp_parser_requires_clause (parser);
26089 if (req == error_mark_node)
26090 return error_mark_node;
26091 return finish_nested_requirement (req);
26094 /* Support Functions */
26096 /* Return the appropriate prefer_type argument for lookup_name_real based on
26097 tag_type and template_mem_access. */
26100 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26102 /* DR 141: When looking in the current enclosing context for a template-name
26103 after -> or ., only consider class templates. */
26104 if (template_mem_access)
26108 case none_type: return 0; // No preference.
26109 case scope_type: return 1; // Type or namespace.
26110 default: return 2; // Type only.
26114 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26115 NAME should have one of the representations used for an
26116 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26117 is returned. If PARSER->SCOPE is a dependent type, then a
26118 SCOPE_REF is returned.
26120 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26121 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26122 was formed. Abstractly, such entities should not be passed to this
26123 function, because they do not need to be looked up, but it is
26124 simpler to check for this special case here, rather than at the
26127 In cases not explicitly covered above, this function returns a
26128 DECL, OVERLOAD, or baselink representing the result of the lookup.
26129 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26132 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26133 (e.g., "struct") that was used. In that case bindings that do not
26134 refer to types are ignored.
26136 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26139 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26142 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26145 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26146 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26147 NULL_TREE otherwise. */
26150 cp_parser_lookup_name (cp_parser *parser, tree name,
26151 enum tag_types tag_type,
26154 bool check_dependency,
26155 tree *ambiguous_decls,
26156 location_t name_location)
26159 tree object_type = parser->context->object_type;
26161 /* Assume that the lookup will be unambiguous. */
26162 if (ambiguous_decls)
26163 *ambiguous_decls = NULL_TREE;
26165 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26166 no longer valid. Note that if we are parsing tentatively, and
26167 the parse fails, OBJECT_TYPE will be automatically restored. */
26168 parser->context->object_type = NULL_TREE;
26170 if (name == error_mark_node)
26171 return error_mark_node;
26173 /* A template-id has already been resolved; there is no lookup to
26175 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26177 if (BASELINK_P (name))
26179 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26180 == TEMPLATE_ID_EXPR);
26184 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26185 it should already have been checked to make sure that the name
26186 used matches the type being destroyed. */
26187 if (TREE_CODE (name) == BIT_NOT_EXPR)
26191 /* Figure out to which type this destructor applies. */
26193 type = parser->scope;
26194 else if (object_type)
26195 type = object_type;
26197 type = current_class_type;
26198 /* If that's not a class type, there is no destructor. */
26199 if (!type || !CLASS_TYPE_P (type))
26200 return error_mark_node;
26202 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26203 lazily_declare_fn (sfk_destructor, type);
26205 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26208 return error_mark_node;
26211 /* By this point, the NAME should be an ordinary identifier. If
26212 the id-expression was a qualified name, the qualifying scope is
26213 stored in PARSER->SCOPE at this point. */
26214 gcc_assert (identifier_p (name));
26216 /* Perform the lookup. */
26221 if (parser->scope == error_mark_node)
26222 return error_mark_node;
26224 /* If the SCOPE is dependent, the lookup must be deferred until
26225 the template is instantiated -- unless we are explicitly
26226 looking up names in uninstantiated templates. Even then, we
26227 cannot look up the name if the scope is not a class type; it
26228 might, for example, be a template type parameter. */
26229 dependent_p = (TYPE_P (parser->scope)
26230 && dependent_scope_p (parser->scope));
26231 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26233 /* Defer lookup. */
26234 decl = error_mark_node;
26237 tree pushed_scope = NULL_TREE;
26239 /* If PARSER->SCOPE is a dependent type, then it must be a
26240 class type, and we must not be checking dependencies;
26241 otherwise, we would have processed this lookup above. So
26242 that PARSER->SCOPE is not considered a dependent base by
26243 lookup_member, we must enter the scope here. */
26245 pushed_scope = push_scope (parser->scope);
26247 /* If the PARSER->SCOPE is a template specialization, it
26248 may be instantiated during name lookup. In that case,
26249 errors may be issued. Even if we rollback the current
26250 tentative parse, those errors are valid. */
26251 decl = lookup_qualified_name (parser->scope, name,
26252 prefer_type_arg (tag_type),
26253 /*complain=*/true);
26255 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26256 lookup result and the nested-name-specifier nominates a class C:
26257 * if the name specified after the nested-name-specifier, when
26258 looked up in C, is the injected-class-name of C (Clause 9), or
26259 * if the name specified after the nested-name-specifier is the
26260 same as the identifier or the simple-template-id's template-
26261 name in the last component of the nested-name-specifier,
26262 the name is instead considered to name the constructor of
26263 class C. [ Note: for example, the constructor is not an
26264 acceptable lookup result in an elaborated-type-specifier so
26265 the constructor would not be used in place of the
26266 injected-class-name. --end note ] Such a constructor name
26267 shall be used only in the declarator-id of a declaration that
26268 names a constructor or in a using-declaration. */
26269 if (tag_type == none_type
26270 && DECL_SELF_REFERENCE_P (decl)
26271 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26272 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26273 prefer_type_arg (tag_type),
26274 /*complain=*/true);
26276 /* If we have a single function from a using decl, pull it out. */
26277 if (TREE_CODE (decl) == OVERLOAD
26278 && !really_overloaded_fn (decl))
26279 decl = OVL_FUNCTION (decl);
26282 pop_scope (pushed_scope);
26285 /* If the scope is a dependent type and either we deferred lookup or
26286 we did lookup but didn't find the name, rememeber the name. */
26287 if (decl == error_mark_node && TYPE_P (parser->scope)
26288 && dependent_type_p (parser->scope))
26294 /* The resolution to Core Issue 180 says that `struct
26295 A::B' should be considered a type-name, even if `A'
26297 type = make_typename_type (parser->scope, name, tag_type,
26298 /*complain=*/tf_error);
26299 if (type != error_mark_node)
26300 decl = TYPE_NAME (type);
26302 else if (is_template
26303 && (cp_parser_next_token_ends_template_argument_p (parser)
26304 || cp_lexer_next_token_is (parser->lexer,
26306 decl = make_unbound_class_template (parser->scope,
26308 /*complain=*/tf_error);
26310 decl = build_qualified_name (/*type=*/NULL_TREE,
26311 parser->scope, name,
26314 parser->qualifying_scope = parser->scope;
26315 parser->object_scope = NULL_TREE;
26317 else if (object_type)
26319 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26320 OBJECT_TYPE is not a class. */
26321 if (CLASS_TYPE_P (object_type))
26322 /* If the OBJECT_TYPE is a template specialization, it may
26323 be instantiated during name lookup. In that case, errors
26324 may be issued. Even if we rollback the current tentative
26325 parse, those errors are valid. */
26326 decl = lookup_member (object_type,
26329 prefer_type_arg (tag_type),
26330 tf_warning_or_error);
26335 /* Look it up in the enclosing context. DR 141: When looking for a
26336 template-name after -> or ., only consider class templates. */
26337 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26339 /*block_p=*/true, is_namespace, 0);
26340 if (object_type == unknown_type_node)
26341 /* The object is type-dependent, so we can't look anything up; we used
26342 this to get the DR 141 behavior. */
26343 object_type = NULL_TREE;
26344 parser->object_scope = object_type;
26345 parser->qualifying_scope = NULL_TREE;
26349 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26351 /*block_p=*/true, is_namespace, 0);
26352 parser->qualifying_scope = NULL_TREE;
26353 parser->object_scope = NULL_TREE;
26356 /* If the lookup failed, let our caller know. */
26357 if (!decl || decl == error_mark_node)
26358 return error_mark_node;
26360 /* Pull out the template from an injected-class-name (or multiple). */
26362 decl = maybe_get_template_decl_from_type_decl (decl);
26364 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26365 if (TREE_CODE (decl) == TREE_LIST)
26367 if (ambiguous_decls)
26368 *ambiguous_decls = decl;
26369 /* The error message we have to print is too complicated for
26370 cp_parser_error, so we incorporate its actions directly. */
26371 if (!cp_parser_simulate_error (parser))
26373 error_at (name_location, "reference to %qD is ambiguous",
26375 print_candidates (decl);
26377 return error_mark_node;
26380 gcc_assert (DECL_P (decl)
26381 || TREE_CODE (decl) == OVERLOAD
26382 || TREE_CODE (decl) == SCOPE_REF
26383 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26384 || BASELINK_P (decl));
26386 /* If we have resolved the name of a member declaration, check to
26387 see if the declaration is accessible. When the name resolves to
26388 set of overloaded functions, accessibility is checked when
26389 overload resolution is done.
26391 During an explicit instantiation, access is not checked at all,
26392 as per [temp.explicit]. */
26394 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26396 maybe_record_typedef_use (decl);
26398 return cp_expr (decl, name_location);
26401 /* Like cp_parser_lookup_name, but for use in the typical case where
26402 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26403 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26406 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26408 return cp_parser_lookup_name (parser, name,
26410 /*is_template=*/false,
26411 /*is_namespace=*/false,
26412 /*check_dependency=*/true,
26413 /*ambiguous_decls=*/NULL,
26417 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26418 the current context, return the TYPE_DECL. If TAG_NAME_P is
26419 true, the DECL indicates the class being defined in a class-head,
26420 or declared in an elaborated-type-specifier.
26422 Otherwise, return DECL. */
26425 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26427 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26428 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26431 template <typename T> struct B;
26434 template <typename T> struct A::B {};
26436 Similarly, in an elaborated-type-specifier:
26438 namespace N { struct X{}; }
26441 template <typename T> friend struct N::X;
26444 However, if the DECL refers to a class type, and we are in
26445 the scope of the class, then the name lookup automatically
26446 finds the TYPE_DECL created by build_self_reference rather
26447 than a TEMPLATE_DECL. For example, in:
26449 template <class T> struct S {
26453 there is no need to handle such case. */
26455 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26456 return DECL_TEMPLATE_RESULT (decl);
26461 /* If too many, or too few, template-parameter lists apply to the
26462 declarator, issue an error message. Returns TRUE if all went well,
26463 and FALSE otherwise. */
26466 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26467 cp_declarator *declarator,
26468 location_t declarator_location)
26470 switch (declarator->kind)
26474 unsigned num_templates = 0;
26475 tree scope = declarator->u.id.qualifying_scope;
26476 bool template_id_p = false;
26479 num_templates = num_template_headers_for_class (scope);
26480 else if (TREE_CODE (declarator->u.id.unqualified_name)
26481 == TEMPLATE_ID_EXPR)
26483 /* If the DECLARATOR has the form `X<y>' then it uses one
26484 additional level of template parameters. */
26486 template_id_p = true;
26489 return cp_parser_check_template_parameters
26490 (parser, num_templates, template_id_p, declarator_location,
26497 case cdk_reference:
26499 return (cp_parser_check_declarator_template_parameters
26500 (parser, declarator->declarator, declarator_location));
26507 gcc_unreachable ();
26512 /* NUM_TEMPLATES were used in the current declaration. If that is
26513 invalid, return FALSE and issue an error messages. Otherwise,
26514 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26515 declarator and we can print more accurate diagnostics. */
26518 cp_parser_check_template_parameters (cp_parser* parser,
26519 unsigned num_templates,
26520 bool template_id_p,
26521 location_t location,
26522 cp_declarator *declarator)
26524 /* If there are the same number of template classes and parameter
26525 lists, that's OK. */
26526 if (parser->num_template_parameter_lists == num_templates)
26528 /* If there are more, but only one more, and the name ends in an identifier,
26529 then we are declaring a primary template. That's OK too. */
26531 && parser->num_template_parameter_lists == num_templates + 1)
26533 /* If there are more template classes than parameter lists, we have
26536 template <class T> void S<T>::R<T>::f (); */
26537 if (parser->num_template_parameter_lists < num_templates)
26539 if (declarator && !current_function_decl)
26540 error_at (location, "specializing member %<%T::%E%> "
26541 "requires %<template<>%> syntax",
26542 declarator->u.id.qualifying_scope,
26543 declarator->u.id.unqualified_name);
26544 else if (declarator)
26545 error_at (location, "invalid declaration of %<%T::%E%>",
26546 declarator->u.id.qualifying_scope,
26547 declarator->u.id.unqualified_name);
26549 error_at (location, "too few template-parameter-lists");
26552 /* Otherwise, there are too many template parameter lists. We have
26555 template <class T> template <class U> void S::f(); */
26556 error_at (location, "too many template-parameter-lists");
26560 /* Parse an optional `::' token indicating that the following name is
26561 from the global namespace. If so, PARSER->SCOPE is set to the
26562 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26563 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26564 Returns the new value of PARSER->SCOPE, if the `::' token is
26565 present, and NULL_TREE otherwise. */
26568 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26572 /* Peek at the next token. */
26573 token = cp_lexer_peek_token (parser->lexer);
26574 /* If we're looking at a `::' token then we're starting from the
26575 global namespace, not our current location. */
26576 if (token->type == CPP_SCOPE)
26578 /* Consume the `::' token. */
26579 cp_lexer_consume_token (parser->lexer);
26580 /* Set the SCOPE so that we know where to start the lookup. */
26581 parser->scope = global_namespace;
26582 parser->qualifying_scope = global_namespace;
26583 parser->object_scope = NULL_TREE;
26585 return parser->scope;
26587 else if (!current_scope_valid_p)
26589 parser->scope = NULL_TREE;
26590 parser->qualifying_scope = NULL_TREE;
26591 parser->object_scope = NULL_TREE;
26597 /* Returns TRUE if the upcoming token sequence is the start of a
26598 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26599 declarator is preceded by the `friend' specifier. */
26602 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26604 bool constructor_p;
26605 bool outside_class_specifier_p;
26606 tree nested_name_specifier;
26607 cp_token *next_token;
26609 /* The common case is that this is not a constructor declarator, so
26610 try to avoid doing lots of work if at all possible. It's not
26611 valid declare a constructor at function scope. */
26612 if (parser->in_function_body)
26614 /* And only certain tokens can begin a constructor declarator. */
26615 next_token = cp_lexer_peek_token (parser->lexer);
26616 if (next_token->type != CPP_NAME
26617 && next_token->type != CPP_SCOPE
26618 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26619 && next_token->type != CPP_TEMPLATE_ID)
26622 /* Parse tentatively; we are going to roll back all of the tokens
26624 cp_parser_parse_tentatively (parser);
26625 /* Assume that we are looking at a constructor declarator. */
26626 constructor_p = true;
26628 /* Look for the optional `::' operator. */
26629 cp_parser_global_scope_opt (parser,
26630 /*current_scope_valid_p=*/false);
26631 /* Look for the nested-name-specifier. */
26632 nested_name_specifier
26633 = (cp_parser_nested_name_specifier_opt (parser,
26634 /*typename_keyword_p=*/false,
26635 /*check_dependency_p=*/false,
26637 /*is_declaration=*/false));
26639 outside_class_specifier_p = (!at_class_scope_p ()
26640 || !TYPE_BEING_DEFINED (current_class_type)
26643 /* Outside of a class-specifier, there must be a
26644 nested-name-specifier. Except in C++17 mode, where we
26645 might be declaring a guiding declaration. */
26646 if (!nested_name_specifier && outside_class_specifier_p
26647 && cxx_dialect < cxx17)
26648 constructor_p = false;
26649 else if (nested_name_specifier == error_mark_node)
26650 constructor_p = false;
26652 /* If we have a class scope, this is easy; DR 147 says that S::S always
26653 names the constructor, and no other qualified name could. */
26654 if (constructor_p && nested_name_specifier
26655 && CLASS_TYPE_P (nested_name_specifier))
26657 tree id = cp_parser_unqualified_id (parser,
26658 /*template_keyword_p=*/false,
26659 /*check_dependency_p=*/false,
26660 /*declarator_p=*/true,
26661 /*optional_p=*/false);
26662 if (is_overloaded_fn (id))
26663 id = DECL_NAME (get_first_fn (id));
26664 if (!constructor_name_p (id, nested_name_specifier))
26665 constructor_p = false;
26667 /* If we still think that this might be a constructor-declarator,
26668 look for a class-name. */
26669 else if (constructor_p)
26673 template <typename T> struct S {
26677 we must recognize that the nested `S' names a class. */
26678 if (cxx_dialect >= cxx17)
26679 cp_parser_parse_tentatively (parser);
26682 type_decl = cp_parser_class_name (parser,
26683 /*typename_keyword_p=*/false,
26684 /*template_keyword_p=*/false,
26686 /*check_dependency_p=*/false,
26687 /*class_head_p=*/false,
26688 /*is_declaration=*/false);
26690 if (cxx_dialect >= cxx17
26691 && !cp_parser_parse_definitely (parser))
26693 type_decl = NULL_TREE;
26694 tree tmpl = cp_parser_template_name (parser,
26695 /*template_keyword*/false,
26696 /*check_dependency_p*/false,
26697 /*is_declaration*/false,
26699 /*is_identifier*/NULL);
26700 if (DECL_CLASS_TEMPLATE_P (tmpl)
26701 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26702 /* It's a deduction guide, return true. */;
26704 cp_parser_simulate_error (parser);
26707 /* If there was no class-name, then this is not a constructor.
26708 Otherwise, if we are in a class-specifier and we aren't
26709 handling a friend declaration, check that its type matches
26710 current_class_type (c++/38313). Note: error_mark_node
26711 is left alone for error recovery purposes. */
26712 constructor_p = (!cp_parser_error_occurred (parser)
26713 && (outside_class_specifier_p
26714 || type_decl == NULL_TREE
26715 || type_decl == error_mark_node
26716 || same_type_p (current_class_type,
26717 TREE_TYPE (type_decl))));
26719 /* If we're still considering a constructor, we have to see a `(',
26720 to begin the parameter-declaration-clause, followed by either a
26721 `)', an `...', or a decl-specifier. We need to check for a
26722 type-specifier to avoid being fooled into thinking that:
26726 is a constructor. (It is actually a function named `f' that
26727 takes one parameter (of type `int') and returns a value of type
26730 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26731 constructor_p = false;
26734 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26735 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26736 /* A parameter declaration begins with a decl-specifier,
26737 which is either the "attribute" keyword, a storage class
26738 specifier, or (usually) a type-specifier. */
26739 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26742 tree pushed_scope = NULL_TREE;
26743 unsigned saved_num_template_parameter_lists;
26745 /* Names appearing in the type-specifier should be looked up
26746 in the scope of the class. */
26747 if (current_class_type)
26749 else if (type_decl)
26751 type = TREE_TYPE (type_decl);
26752 if (TREE_CODE (type) == TYPENAME_TYPE)
26754 type = resolve_typename_type (type,
26755 /*only_current_p=*/false);
26756 if (TREE_CODE (type) == TYPENAME_TYPE)
26758 cp_parser_abort_tentative_parse (parser);
26762 pushed_scope = push_scope (type);
26765 /* Inside the constructor parameter list, surrounding
26766 template-parameter-lists do not apply. */
26767 saved_num_template_parameter_lists
26768 = parser->num_template_parameter_lists;
26769 parser->num_template_parameter_lists = 0;
26771 /* Look for the type-specifier. */
26772 cp_parser_type_specifier (parser,
26773 CP_PARSER_FLAGS_NONE,
26774 /*decl_specs=*/NULL,
26775 /*is_declarator=*/true,
26776 /*declares_class_or_enum=*/NULL,
26777 /*is_cv_qualifier=*/NULL);
26779 parser->num_template_parameter_lists
26780 = saved_num_template_parameter_lists;
26782 /* Leave the scope of the class. */
26784 pop_scope (pushed_scope);
26786 constructor_p = !cp_parser_error_occurred (parser);
26790 /* We did not really want to consume any tokens. */
26791 cp_parser_abort_tentative_parse (parser);
26793 return constructor_p;
26796 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26797 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26798 they must be performed once we are in the scope of the function.
26800 Returns the function defined. */
26803 cp_parser_function_definition_from_specifiers_and_declarator
26804 (cp_parser* parser,
26805 cp_decl_specifier_seq *decl_specifiers,
26807 const cp_declarator *declarator)
26812 /* Begin the function-definition. */
26813 success_p = start_function (decl_specifiers, declarator, attributes);
26815 /* The things we're about to see are not directly qualified by any
26816 template headers we've seen thus far. */
26817 reset_specialization ();
26819 /* If there were names looked up in the decl-specifier-seq that we
26820 did not check, check them now. We must wait until we are in the
26821 scope of the function to perform the checks, since the function
26822 might be a friend. */
26823 perform_deferred_access_checks (tf_warning_or_error);
26827 cp_finalize_omp_declare_simd (parser, current_function_decl);
26828 parser->omp_declare_simd = NULL;
26829 cp_finalize_oacc_routine (parser, current_function_decl, true);
26830 parser->oacc_routine = NULL;
26835 /* Skip the entire function. */
26836 cp_parser_skip_to_end_of_block_or_statement (parser);
26837 fn = error_mark_node;
26839 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26841 /* Seen already, skip it. An error message has already been output. */
26842 cp_parser_skip_to_end_of_block_or_statement (parser);
26843 fn = current_function_decl;
26844 current_function_decl = NULL_TREE;
26845 /* If this is a function from a class, pop the nested class. */
26846 if (current_class_name)
26847 pop_nested_class ();
26852 if (DECL_DECLARED_INLINE_P (current_function_decl))
26853 tv = TV_PARSE_INLINE;
26855 tv = TV_PARSE_FUNC;
26857 fn = cp_parser_function_definition_after_declarator (parser,
26858 /*inline_p=*/false);
26865 /* Parse the part of a function-definition that follows the
26866 declarator. INLINE_P is TRUE iff this function is an inline
26867 function defined within a class-specifier.
26869 Returns the function defined. */
26872 cp_parser_function_definition_after_declarator (cp_parser* parser,
26876 bool saved_in_unbraced_linkage_specification_p;
26877 bool saved_in_function_body;
26878 unsigned saved_num_template_parameter_lists;
26880 bool fully_implicit_function_template_p
26881 = parser->fully_implicit_function_template_p;
26882 parser->fully_implicit_function_template_p = false;
26883 tree implicit_template_parms
26884 = parser->implicit_template_parms;
26885 parser->implicit_template_parms = 0;
26886 cp_binding_level* implicit_template_scope
26887 = parser->implicit_template_scope;
26888 parser->implicit_template_scope = 0;
26890 saved_in_function_body = parser->in_function_body;
26891 parser->in_function_body = true;
26892 /* If the next token is `return', then the code may be trying to
26893 make use of the "named return value" extension that G++ used to
26895 token = cp_lexer_peek_token (parser->lexer);
26896 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26898 /* Consume the `return' keyword. */
26899 cp_lexer_consume_token (parser->lexer);
26900 /* Look for the identifier that indicates what value is to be
26902 cp_parser_identifier (parser);
26903 /* Issue an error message. */
26904 error_at (token->location,
26905 "named return values are no longer supported");
26906 /* Skip tokens until we reach the start of the function body. */
26909 cp_token *token = cp_lexer_peek_token (parser->lexer);
26910 if (token->type == CPP_OPEN_BRACE
26911 || token->type == CPP_EOF
26912 || token->type == CPP_PRAGMA_EOL)
26914 cp_lexer_consume_token (parser->lexer);
26917 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26918 anything declared inside `f'. */
26919 saved_in_unbraced_linkage_specification_p
26920 = parser->in_unbraced_linkage_specification_p;
26921 parser->in_unbraced_linkage_specification_p = false;
26922 /* Inside the function, surrounding template-parameter-lists do not
26924 saved_num_template_parameter_lists
26925 = parser->num_template_parameter_lists;
26926 parser->num_template_parameter_lists = 0;
26928 /* If the next token is `try', `__transaction_atomic', or
26929 `__transaction_relaxed`, then we are looking at either function-try-block
26930 or function-transaction-block. Note that all of these include the
26932 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26933 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26934 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26935 RID_TRANSACTION_RELAXED))
26936 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26937 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26938 cp_parser_function_try_block (parser);
26940 cp_parser_ctor_initializer_opt_and_function_body
26941 (parser, /*in_function_try_block=*/false);
26943 /* Finish the function. */
26944 fn = finish_function (inline_p);
26945 /* Generate code for it, if necessary. */
26946 expand_or_defer_fn (fn);
26947 /* Restore the saved values. */
26948 parser->in_unbraced_linkage_specification_p
26949 = saved_in_unbraced_linkage_specification_p;
26950 parser->num_template_parameter_lists
26951 = saved_num_template_parameter_lists;
26952 parser->in_function_body = saved_in_function_body;
26954 parser->fully_implicit_function_template_p
26955 = fully_implicit_function_template_p;
26956 parser->implicit_template_parms
26957 = implicit_template_parms;
26958 parser->implicit_template_scope
26959 = implicit_template_scope;
26961 if (parser->fully_implicit_function_template_p)
26962 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26967 /* Parse a template-declaration body (following argument list). */
26970 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26971 tree parameter_list,
26974 tree decl = NULL_TREE;
26975 bool friend_p = false;
26977 /* We just processed one more parameter list. */
26978 ++parser->num_template_parameter_lists;
26980 /* Get the deferred access checks from the parameter list. These
26981 will be checked once we know what is being declared, as for a
26982 member template the checks must be performed in the scope of the
26983 class containing the member. */
26984 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26986 /* Tentatively parse for a new template parameter list, which can either be
26987 the template keyword or a template introduction. */
26988 if (cp_parser_template_declaration_after_export (parser, member_p))
26990 else if (cxx_dialect >= cxx11
26991 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26992 decl = cp_parser_alias_declaration (parser);
26995 /* There are no access checks when parsing a template, as we do not
26996 know if a specialization will be a friend. */
26997 push_deferring_access_checks (dk_no_check);
26998 cp_token *token = cp_lexer_peek_token (parser->lexer);
26999 decl = cp_parser_single_declaration (parser,
27002 /*explicit_specialization_p=*/false,
27004 pop_deferring_access_checks ();
27006 /* If this is a member template declaration, let the front
27008 if (member_p && !friend_p && decl)
27010 if (TREE_CODE (decl) == TYPE_DECL)
27011 cp_parser_check_access_in_redeclaration (decl, token->location);
27013 decl = finish_member_template_decl (decl);
27015 else if (friend_p && decl
27016 && DECL_DECLARES_TYPE_P (decl))
27017 make_friend_class (current_class_type, TREE_TYPE (decl),
27018 /*complain=*/true);
27020 /* We are done with the current parameter list. */
27021 --parser->num_template_parameter_lists;
27023 pop_deferring_access_checks ();
27026 finish_template_decl (parameter_list);
27028 /* Check the template arguments for a literal operator template. */
27030 && DECL_DECLARES_FUNCTION_P (decl)
27031 && UDLIT_OPER_P (DECL_NAME (decl)))
27034 if (parameter_list == NULL_TREE)
27038 int num_parms = TREE_VEC_LENGTH (parameter_list);
27039 if (num_parms == 1)
27041 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27042 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27043 if (TREE_TYPE (parm) != char_type_node
27044 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27047 else if (num_parms == 2 && cxx_dialect >= cxx14)
27049 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27050 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27051 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27052 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27053 if (parm == error_mark_node
27054 || TREE_TYPE (parm) != TREE_TYPE (type)
27055 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27063 if (cxx_dialect >= cxx14)
27064 error ("literal operator template %qD has invalid parameter list."
27065 " Expected non-type template argument pack <char...>"
27066 " or <typename CharT, CharT...>",
27069 error ("literal operator template %qD has invalid parameter list."
27070 " Expected non-type template argument pack <char...>",
27075 /* Register member declarations. */
27076 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27077 finish_member_declaration (decl);
27078 /* If DECL is a function template, we must return to parse it later.
27079 (Even though there is no definition, there might be default
27080 arguments that need handling.) */
27081 if (member_p && decl
27082 && DECL_DECLARES_FUNCTION_P (decl))
27083 vec_safe_push (unparsed_funs_with_definitions, decl);
27086 /* Parse a template introduction header for a template-declaration. Returns
27087 false if tentative parse fails. */
27090 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27092 cp_parser_parse_tentatively (parser);
27094 tree saved_scope = parser->scope;
27095 tree saved_object_scope = parser->object_scope;
27096 tree saved_qualifying_scope = parser->qualifying_scope;
27098 /* Look for the optional `::' operator. */
27099 cp_parser_global_scope_opt (parser,
27100 /*current_scope_valid_p=*/false);
27101 /* Look for the nested-name-specifier. */
27102 cp_parser_nested_name_specifier_opt (parser,
27103 /*typename_keyword_p=*/false,
27104 /*check_dependency_p=*/true,
27106 /*is_declaration=*/false);
27108 cp_token *token = cp_lexer_peek_token (parser->lexer);
27109 tree concept_name = cp_parser_identifier (parser);
27111 /* Look up the concept for which we will be matching
27112 template parameters. */
27113 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27115 parser->scope = saved_scope;
27116 parser->object_scope = saved_object_scope;
27117 parser->qualifying_scope = saved_qualifying_scope;
27119 if (concept_name == error_mark_node)
27120 cp_parser_simulate_error (parser);
27122 /* Look for opening brace for introduction. */
27123 matching_braces braces;
27124 braces.require_open (parser);
27126 if (!cp_parser_parse_definitely (parser))
27129 push_deferring_access_checks (dk_deferred);
27131 /* Build vector of placeholder parameters and grab
27132 matching identifiers. */
27133 tree introduction_list = cp_parser_introduction_list (parser);
27135 /* The introduction-list shall not be empty. */
27136 int nargs = TREE_VEC_LENGTH (introduction_list);
27139 error ("empty introduction-list");
27143 /* Look for closing brace for introduction. */
27144 if (!braces.require_close (parser))
27147 if (tmpl_decl == error_mark_node)
27149 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27154 /* Build and associate the constraint. */
27155 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27156 if (parms && parms != error_mark_node)
27158 cp_parser_template_declaration_after_parameters (parser, parms,
27163 error_at (token->location, "no matching concept for template-introduction");
27167 /* Parse a normal template-declaration following the template keyword. */
27170 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27172 tree parameter_list;
27173 bool need_lang_pop;
27174 location_t location = input_location;
27176 /* Look for the `<' token. */
27177 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27179 if (at_class_scope_p () && current_function_decl)
27181 /* 14.5.2.2 [temp.mem]
27183 A local class shall not have member templates. */
27184 error_at (location,
27185 "invalid declaration of member template in local class");
27186 cp_parser_skip_to_end_of_block_or_statement (parser);
27191 A template ... shall not have C linkage. */
27192 if (current_lang_name == lang_name_c)
27194 error_at (location, "template with C linkage");
27195 maybe_show_extern_c_location ();
27196 /* Give it C++ linkage to avoid confusing other parts of the
27198 push_lang_context (lang_name_cplusplus);
27199 need_lang_pop = true;
27202 need_lang_pop = false;
27204 /* We cannot perform access checks on the template parameter
27205 declarations until we know what is being declared, just as we
27206 cannot check the decl-specifier list. */
27207 push_deferring_access_checks (dk_deferred);
27209 /* If the next token is `>', then we have an invalid
27210 specialization. Rather than complain about an invalid template
27211 parameter, issue an error message here. */
27212 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27214 cp_parser_error (parser, "invalid explicit specialization");
27215 begin_specialization ();
27216 parameter_list = NULL_TREE;
27220 /* Parse the template parameters. */
27221 parameter_list = cp_parser_template_parameter_list (parser);
27224 /* Look for the `>'. */
27225 cp_parser_skip_to_end_of_template_parameter_list (parser);
27227 /* Manage template requirements */
27230 tree reqs = get_shorthand_constraints (current_template_parms);
27231 if (tree r = cp_parser_requires_clause_opt (parser))
27232 reqs = conjoin_constraints (reqs, normalize_expression (r));
27233 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27236 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27239 /* For the erroneous case of a template with C linkage, we pushed an
27240 implicit C++ linkage scope; exit that scope now. */
27242 pop_lang_context ();
27245 /* Parse a template-declaration, assuming that the `export' (and
27246 `extern') keywords, if present, has already been scanned. MEMBER_P
27247 is as for cp_parser_template_declaration. */
27250 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27252 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27254 cp_lexer_consume_token (parser->lexer);
27255 cp_parser_explicit_template_declaration (parser, member_p);
27258 else if (flag_concepts)
27259 return cp_parser_template_introduction (parser, member_p);
27264 /* Perform the deferred access checks from a template-parameter-list.
27265 CHECKS is a TREE_LIST of access checks, as returned by
27266 get_deferred_access_checks. */
27269 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27271 ++processing_template_parmlist;
27272 perform_access_checks (checks, tf_warning_or_error);
27273 --processing_template_parmlist;
27276 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27277 `function-definition' sequence that follows a template header.
27278 If MEMBER_P is true, this declaration appears in a class scope.
27280 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27281 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27284 cp_parser_single_declaration (cp_parser* parser,
27285 vec<deferred_access_check, va_gc> *checks,
27287 bool explicit_specialization_p,
27290 int declares_class_or_enum;
27291 tree decl = NULL_TREE;
27292 cp_decl_specifier_seq decl_specifiers;
27293 bool function_definition_p = false;
27294 cp_token *decl_spec_token_start;
27296 /* This function is only used when processing a template
27298 gcc_assert (innermost_scope_kind () == sk_template_parms
27299 || innermost_scope_kind () == sk_template_spec);
27301 /* Defer access checks until we know what is being declared. */
27302 push_deferring_access_checks (dk_deferred);
27304 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27306 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27307 cp_parser_decl_specifier_seq (parser,
27308 CP_PARSER_FLAGS_OPTIONAL,
27310 &declares_class_or_enum);
27312 *friend_p = cp_parser_friend_p (&decl_specifiers);
27314 /* There are no template typedefs. */
27315 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27317 error_at (decl_spec_token_start->location,
27318 "template declaration of %<typedef%>");
27319 decl = error_mark_node;
27322 /* Gather up the access checks that occurred the
27323 decl-specifier-seq. */
27324 stop_deferring_access_checks ();
27326 /* Check for the declaration of a template class. */
27327 if (declares_class_or_enum)
27329 if (cp_parser_declares_only_class_p (parser)
27330 || (declares_class_or_enum & 2))
27332 // If this is a declaration, but not a definition, associate
27333 // any constraints with the type declaration. Constraints
27334 // are associated with definitions in cp_parser_class_specifier.
27335 if (declares_class_or_enum == 1)
27336 associate_classtype_constraints (decl_specifiers.type);
27338 decl = shadow_tag (&decl_specifiers);
27343 friend template <typename T> struct A<T>::B;
27346 A<T>::B will be represented by a TYPENAME_TYPE, and
27347 therefore not recognized by shadow_tag. */
27348 if (friend_p && *friend_p
27350 && decl_specifiers.type
27351 && TYPE_P (decl_specifiers.type))
27352 decl = decl_specifiers.type;
27354 if (decl && decl != error_mark_node)
27355 decl = TYPE_NAME (decl);
27357 decl = error_mark_node;
27359 /* Perform access checks for template parameters. */
27360 cp_parser_perform_template_parameter_access_checks (checks);
27362 /* Give a helpful diagnostic for
27363 template <class T> struct A { } a;
27364 if we aren't already recovering from an error. */
27365 if (!cp_parser_declares_only_class_p (parser)
27368 error_at (cp_lexer_peek_token (parser->lexer)->location,
27369 "a class template declaration must not declare "
27371 cp_parser_skip_to_end_of_block_or_statement (parser);
27377 /* Complain about missing 'typename' or other invalid type names. */
27378 if (!decl_specifiers.any_type_specifiers_p
27379 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27381 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27382 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27383 the rest of this declaration. */
27384 decl = error_mark_node;
27388 /* If it's not a template class, try for a template function. If
27389 the next token is a `;', then this declaration does not declare
27390 anything. But, if there were errors in the decl-specifiers, then
27391 the error might well have come from an attempted class-specifier.
27392 In that case, there's no need to warn about a missing declarator. */
27394 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27395 || decl_specifiers.type != error_mark_node))
27397 decl = cp_parser_init_declarator (parser,
27400 /*function_definition_allowed_p=*/true,
27402 declares_class_or_enum,
27403 &function_definition_p,
27406 /* 7.1.1-1 [dcl.stc]
27408 A storage-class-specifier shall not be specified in an explicit
27409 specialization... */
27411 && explicit_specialization_p
27412 && decl_specifiers.storage_class != sc_none)
27414 error_at (decl_spec_token_start->location,
27415 "explicit template specialization cannot have a storage class");
27416 decl = error_mark_node;
27419 if (decl && VAR_P (decl))
27420 check_template_variable (decl);
27423 /* Look for a trailing `;' after the declaration. */
27424 if (!function_definition_p
27425 && (decl == error_mark_node
27426 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27427 cp_parser_skip_to_end_of_block_or_statement (parser);
27430 pop_deferring_access_checks ();
27432 /* Clear any current qualification; whatever comes next is the start
27433 of something new. */
27434 parser->scope = NULL_TREE;
27435 parser->qualifying_scope = NULL_TREE;
27436 parser->object_scope = NULL_TREE;
27441 /* Parse a cast-expression that is not the operand of a unary "&". */
27444 cp_parser_simple_cast_expression (cp_parser *parser)
27446 return cp_parser_cast_expression (parser, /*address_p=*/false,
27447 /*cast_p=*/false, /*decltype*/false, NULL);
27450 /* Parse a functional cast to TYPE. Returns an expression
27451 representing the cast. */
27454 cp_parser_functional_cast (cp_parser* parser, tree type)
27456 vec<tree, va_gc> *vec;
27457 tree expression_list;
27461 location_t start_loc = input_location;
27464 type = error_mark_node;
27466 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27468 cp_lexer_set_source_position (parser->lexer);
27469 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27470 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27471 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27472 if (TREE_CODE (type) == TYPE_DECL)
27473 type = TREE_TYPE (type);
27475 cast = finish_compound_literal (type, expression_list,
27476 tf_warning_or_error, fcl_functional);
27477 /* Create a location of the form:
27480 with caret == start at the start of the type name,
27481 finishing at the closing brace. */
27482 location_t finish_loc
27483 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27484 location_t combined_loc = make_location (start_loc, start_loc,
27486 cast.set_location (combined_loc);
27491 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27493 /*allow_expansion_p=*/true,
27494 /*non_constant_p=*/NULL);
27496 expression_list = error_mark_node;
27499 expression_list = build_tree_list_vec (vec);
27500 release_tree_vector (vec);
27503 cast = build_functional_cast (type, expression_list,
27504 tf_warning_or_error);
27505 /* [expr.const]/1: In an integral constant expression "only type
27506 conversions to integral or enumeration type can be used". */
27507 if (TREE_CODE (type) == TYPE_DECL)
27508 type = TREE_TYPE (type);
27509 if (cast != error_mark_node
27510 && !cast_valid_in_integral_constant_expression_p (type)
27511 && cp_parser_non_integral_constant_expression (parser,
27513 return error_mark_node;
27515 /* Create a location of the form:
27518 with caret == start at the start of the type name,
27519 finishing at the closing paren. */
27520 location_t finish_loc
27521 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27522 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27523 cast.set_location (combined_loc);
27527 /* Save the tokens that make up the body of a member function defined
27528 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27529 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27530 specifiers applied to the declaration. Returns the FUNCTION_DECL
27531 for the member function. */
27534 cp_parser_save_member_function_body (cp_parser* parser,
27535 cp_decl_specifier_seq *decl_specifiers,
27536 cp_declarator *declarator,
27542 bool function_try_block = false;
27544 /* Create the FUNCTION_DECL. */
27545 fn = grokmethod (decl_specifiers, declarator, attributes);
27546 cp_finalize_omp_declare_simd (parser, fn);
27547 cp_finalize_oacc_routine (parser, fn, true);
27548 /* If something went badly wrong, bail out now. */
27549 if (fn == error_mark_node)
27551 /* If there's a function-body, skip it. */
27552 if (cp_parser_token_starts_function_definition_p
27553 (cp_lexer_peek_token (parser->lexer)))
27554 cp_parser_skip_to_end_of_block_or_statement (parser);
27555 return error_mark_node;
27558 /* Remember it, if there default args to post process. */
27559 cp_parser_save_default_args (parser, fn);
27561 /* Save away the tokens that make up the body of the
27563 first = parser->lexer->next_token;
27565 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27566 cp_lexer_consume_token (parser->lexer);
27567 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27568 RID_TRANSACTION_ATOMIC))
27570 cp_lexer_consume_token (parser->lexer);
27571 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27572 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27573 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27574 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27575 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27576 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27577 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27579 cp_lexer_consume_token (parser->lexer);
27580 cp_lexer_consume_token (parser->lexer);
27581 cp_lexer_consume_token (parser->lexer);
27582 cp_lexer_consume_token (parser->lexer);
27583 cp_lexer_consume_token (parser->lexer);
27586 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27587 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27589 cp_lexer_consume_token (parser->lexer);
27590 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27595 /* Handle function try blocks. */
27596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27598 cp_lexer_consume_token (parser->lexer);
27599 function_try_block = true;
27601 /* We can have braced-init-list mem-initializers before the fn body. */
27602 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27604 cp_lexer_consume_token (parser->lexer);
27605 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27607 /* cache_group will stop after an un-nested { } pair, too. */
27608 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27611 /* variadic mem-inits have ... after the ')'. */
27612 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27613 cp_lexer_consume_token (parser->lexer);
27616 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27617 /* Handle function try blocks. */
27618 if (function_try_block)
27619 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27620 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27621 last = parser->lexer->next_token;
27623 /* Save away the inline definition; we will process it when the
27624 class is complete. */
27625 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27626 DECL_PENDING_INLINE_P (fn) = 1;
27628 /* We need to know that this was defined in the class, so that
27629 friend templates are handled correctly. */
27630 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27632 /* Add FN to the queue of functions to be parsed later. */
27633 vec_safe_push (unparsed_funs_with_definitions, fn);
27638 /* Save the tokens that make up the in-class initializer for a non-static
27639 data member. Returns a DEFAULT_ARG. */
27642 cp_parser_save_nsdmi (cp_parser* parser)
27644 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27647 /* Parse a template-argument-list, as well as the trailing ">" (but
27648 not the opening "<"). See cp_parser_template_argument_list for the
27652 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27656 tree saved_qualifying_scope;
27657 tree saved_object_scope;
27658 bool saved_greater_than_is_operator_p;
27659 int saved_unevaluated_operand;
27660 int saved_inhibit_evaluation_warnings;
27664 When parsing a template-id, the first non-nested `>' is taken as
27665 the end of the template-argument-list rather than a greater-than
27667 saved_greater_than_is_operator_p
27668 = parser->greater_than_is_operator_p;
27669 parser->greater_than_is_operator_p = false;
27670 /* Parsing the argument list may modify SCOPE, so we save it
27672 saved_scope = parser->scope;
27673 saved_qualifying_scope = parser->qualifying_scope;
27674 saved_object_scope = parser->object_scope;
27675 /* We need to evaluate the template arguments, even though this
27676 template-id may be nested within a "sizeof". */
27677 saved_unevaluated_operand = cp_unevaluated_operand;
27678 cp_unevaluated_operand = 0;
27679 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27680 c_inhibit_evaluation_warnings = 0;
27681 /* Parse the template-argument-list itself. */
27682 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27683 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27684 arguments = NULL_TREE;
27686 arguments = cp_parser_template_argument_list (parser);
27687 /* Look for the `>' that ends the template-argument-list. If we find
27688 a '>>' instead, it's probably just a typo. */
27689 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27691 if (cxx_dialect != cxx98)
27693 /* In C++0x, a `>>' in a template argument list or cast
27694 expression is considered to be two separate `>'
27695 tokens. So, change the current token to a `>', but don't
27696 consume it: it will be consumed later when the outer
27697 template argument list (or cast expression) is parsed.
27698 Note that this replacement of `>' for `>>' is necessary
27699 even if we are parsing tentatively: in the tentative
27700 case, after calling
27701 cp_parser_enclosed_template_argument_list we will always
27702 throw away all of the template arguments and the first
27703 closing `>', either because the template argument list
27704 was erroneous or because we are replacing those tokens
27705 with a CPP_TEMPLATE_ID token. The second `>' (which will
27706 not have been thrown away) is needed either to close an
27707 outer template argument list or to complete a new-style
27709 cp_token *token = cp_lexer_peek_token (parser->lexer);
27710 token->type = CPP_GREATER;
27712 else if (!saved_greater_than_is_operator_p)
27714 /* If we're in a nested template argument list, the '>>' has
27715 to be a typo for '> >'. We emit the error message, but we
27716 continue parsing and we push a '>' as next token, so that
27717 the argument list will be parsed correctly. Note that the
27718 global source location is still on the token before the
27719 '>>', so we need to say explicitly where we want it. */
27720 cp_token *token = cp_lexer_peek_token (parser->lexer);
27721 gcc_rich_location richloc (token->location);
27722 richloc.add_fixit_replace ("> >");
27723 error_at (&richloc, "%<>>%> should be %<> >%> "
27724 "within a nested template argument list");
27726 token->type = CPP_GREATER;
27730 /* If this is not a nested template argument list, the '>>'
27731 is a typo for '>'. Emit an error message and continue.
27732 Same deal about the token location, but here we can get it
27733 right by consuming the '>>' before issuing the diagnostic. */
27734 cp_token *token = cp_lexer_consume_token (parser->lexer);
27735 error_at (token->location,
27736 "spurious %<>>%>, use %<>%> to terminate "
27737 "a template argument list");
27741 cp_parser_skip_to_end_of_template_parameter_list (parser);
27742 /* The `>' token might be a greater-than operator again now. */
27743 parser->greater_than_is_operator_p
27744 = saved_greater_than_is_operator_p;
27745 /* Restore the SAVED_SCOPE. */
27746 parser->scope = saved_scope;
27747 parser->qualifying_scope = saved_qualifying_scope;
27748 parser->object_scope = saved_object_scope;
27749 cp_unevaluated_operand = saved_unevaluated_operand;
27750 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27755 /* MEMBER_FUNCTION is a member function, or a friend. If default
27756 arguments, or the body of the function have not yet been parsed,
27760 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27762 timevar_push (TV_PARSE_INMETH);
27763 /* If this member is a template, get the underlying
27765 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27766 member_function = DECL_TEMPLATE_RESULT (member_function);
27768 /* There should not be any class definitions in progress at this
27769 point; the bodies of members are only parsed outside of all class
27771 gcc_assert (parser->num_classes_being_defined == 0);
27772 /* While we're parsing the member functions we might encounter more
27773 classes. We want to handle them right away, but we don't want
27774 them getting mixed up with functions that are currently in the
27776 push_unparsed_function_queues (parser);
27778 /* Make sure that any template parameters are in scope. */
27779 maybe_begin_member_template_processing (member_function);
27781 /* If the body of the function has not yet been parsed, parse it
27783 if (DECL_PENDING_INLINE_P (member_function))
27785 tree function_scope;
27786 cp_token_cache *tokens;
27788 /* The function is no longer pending; we are processing it. */
27789 tokens = DECL_PENDING_INLINE_INFO (member_function);
27790 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27791 DECL_PENDING_INLINE_P (member_function) = 0;
27793 /* If this is a local class, enter the scope of the containing
27795 function_scope = current_function_decl;
27796 if (function_scope)
27797 push_function_context ();
27799 /* Push the body of the function onto the lexer stack. */
27800 cp_parser_push_lexer_for_tokens (parser, tokens);
27802 /* Let the front end know that we going to be defining this
27804 start_preparsed_function (member_function, NULL_TREE,
27805 SF_PRE_PARSED | SF_INCLASS_INLINE);
27807 /* Don't do access checking if it is a templated function. */
27808 if (processing_template_decl)
27809 push_deferring_access_checks (dk_no_check);
27811 /* #pragma omp declare reduction needs special parsing. */
27812 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27814 parser->lexer->in_pragma = true;
27815 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27816 finish_function (/*inline_p=*/true);
27817 cp_check_omp_declare_reduction (member_function);
27820 /* Now, parse the body of the function. */
27821 cp_parser_function_definition_after_declarator (parser,
27822 /*inline_p=*/true);
27824 if (processing_template_decl)
27825 pop_deferring_access_checks ();
27827 /* Leave the scope of the containing function. */
27828 if (function_scope)
27829 pop_function_context ();
27830 cp_parser_pop_lexer (parser);
27833 /* Remove any template parameters from the symbol table. */
27834 maybe_end_member_template_processing ();
27836 /* Restore the queue. */
27837 pop_unparsed_function_queues (parser);
27838 timevar_pop (TV_PARSE_INMETH);
27841 /* If DECL contains any default args, remember it on the unparsed
27842 functions queue. */
27845 cp_parser_save_default_args (cp_parser* parser, tree decl)
27849 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27851 probe = TREE_CHAIN (probe))
27852 if (TREE_PURPOSE (probe))
27854 cp_default_arg_entry entry = {current_class_type, decl};
27855 vec_safe_push (unparsed_funs_with_default_args, entry);
27860 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27861 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27862 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27863 from the parameter-type-list. */
27866 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27867 tree default_arg, tree parmtype)
27869 cp_token_cache *tokens;
27873 if (default_arg == error_mark_node)
27874 return error_mark_node;
27876 /* Push the saved tokens for the default argument onto the parser's
27878 tokens = DEFARG_TOKENS (default_arg);
27879 cp_parser_push_lexer_for_tokens (parser, tokens);
27881 start_lambda_scope (decl);
27883 /* Parse the default argument. */
27884 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27885 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27886 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27888 finish_lambda_scope ();
27890 if (parsed_arg == error_mark_node)
27891 cp_parser_skip_to_end_of_statement (parser);
27893 if (!processing_template_decl)
27895 /* In a non-template class, check conversions now. In a template,
27896 we'll wait and instantiate these as needed. */
27897 if (TREE_CODE (decl) == PARM_DECL)
27898 parsed_arg = check_default_argument (parmtype, parsed_arg,
27899 tf_warning_or_error);
27900 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27901 parsed_arg = error_mark_node;
27903 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27906 /* If the token stream has not been completely used up, then
27907 there was extra junk after the end of the default
27909 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27911 if (TREE_CODE (decl) == PARM_DECL)
27912 cp_parser_error (parser, "expected %<,%>");
27914 cp_parser_error (parser, "expected %<;%>");
27917 /* Revert to the main lexer. */
27918 cp_parser_pop_lexer (parser);
27923 /* FIELD is a non-static data member with an initializer which we saved for
27924 later; parse it now. */
27927 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27931 maybe_begin_member_template_processing (field);
27933 push_unparsed_function_queues (parser);
27934 def = cp_parser_late_parse_one_default_arg (parser, field,
27935 DECL_INITIAL (field),
27937 pop_unparsed_function_queues (parser);
27939 maybe_end_member_template_processing ();
27941 DECL_INITIAL (field) = def;
27944 /* FN is a FUNCTION_DECL which may contains a parameter with an
27945 unparsed DEFAULT_ARG. Parse the default args now. This function
27946 assumes that the current scope is the scope in which the default
27947 argument should be processed. */
27950 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27952 bool saved_local_variables_forbidden_p;
27953 tree parm, parmdecl;
27955 /* While we're parsing the default args, we might (due to the
27956 statement expression extension) encounter more classes. We want
27957 to handle them right away, but we don't want them getting mixed
27958 up with default args that are currently in the queue. */
27959 push_unparsed_function_queues (parser);
27961 /* Local variable names (and the `this' keyword) may not appear
27962 in a default argument. */
27963 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27964 parser->local_variables_forbidden_p = true;
27966 push_defarg_context (fn);
27968 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27969 parmdecl = DECL_ARGUMENTS (fn);
27970 parm && parm != void_list_node;
27971 parm = TREE_CHAIN (parm),
27972 parmdecl = DECL_CHAIN (parmdecl))
27974 tree default_arg = TREE_PURPOSE (parm);
27976 vec<tree, va_gc> *insts;
27983 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27984 /* This can happen for a friend declaration for a function
27985 already declared with default arguments. */
27989 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27991 TREE_VALUE (parm));
27992 TREE_PURPOSE (parm) = parsed_arg;
27994 /* Update any instantiations we've already created. */
27995 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27996 vec_safe_iterate (insts, ix, ©); ix++)
27997 TREE_PURPOSE (copy) = parsed_arg;
28000 pop_defarg_context ();
28002 /* Make sure no default arg is missing. */
28003 check_default_args (fn);
28005 /* Restore the state of local_variables_forbidden_p. */
28006 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28008 /* Restore the queue. */
28009 pop_unparsed_function_queues (parser);
28012 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28014 sizeof ... ( identifier )
28016 where the 'sizeof' token has already been consumed. */
28019 cp_parser_sizeof_pack (cp_parser *parser)
28021 /* Consume the `...'. */
28022 cp_lexer_consume_token (parser->lexer);
28023 maybe_warn_variadic_templates ();
28025 matching_parens parens;
28026 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28028 parens.consume_open (parser);
28030 permerror (cp_lexer_peek_token (parser->lexer)->location,
28031 "%<sizeof...%> argument must be surrounded by parentheses");
28033 cp_token *token = cp_lexer_peek_token (parser->lexer);
28034 tree name = cp_parser_identifier (parser);
28035 if (name == error_mark_node)
28036 return error_mark_node;
28037 /* The name is not qualified. */
28038 parser->scope = NULL_TREE;
28039 parser->qualifying_scope = NULL_TREE;
28040 parser->object_scope = NULL_TREE;
28041 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28042 if (expr == error_mark_node)
28043 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28045 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28046 expr = TREE_TYPE (expr);
28047 else if (TREE_CODE (expr) == CONST_DECL)
28048 expr = DECL_INITIAL (expr);
28049 expr = make_pack_expansion (expr);
28050 PACK_EXPANSION_SIZEOF_P (expr) = true;
28053 parens.require_close (parser);
28058 /* Parse the operand of `sizeof' (or a similar operator). Returns
28059 either a TYPE or an expression, depending on the form of the
28060 input. The KEYWORD indicates which kind of expression we have
28064 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28066 tree expr = NULL_TREE;
28067 const char *saved_message;
28069 bool saved_integral_constant_expression_p;
28070 bool saved_non_integral_constant_expression_p;
28072 /* If it's a `...', then we are computing the length of a parameter
28074 if (keyword == RID_SIZEOF
28075 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28076 return cp_parser_sizeof_pack (parser);
28078 /* Types cannot be defined in a `sizeof' expression. Save away the
28080 saved_message = parser->type_definition_forbidden_message;
28081 /* And create the new one. */
28082 tmp = concat ("types may not be defined in %<",
28083 IDENTIFIER_POINTER (ridpointers[keyword]),
28084 "%> expressions", NULL);
28085 parser->type_definition_forbidden_message = tmp;
28087 /* The restrictions on constant-expressions do not apply inside
28088 sizeof expressions. */
28089 saved_integral_constant_expression_p
28090 = parser->integral_constant_expression_p;
28091 saved_non_integral_constant_expression_p
28092 = parser->non_integral_constant_expression_p;
28093 parser->integral_constant_expression_p = false;
28095 /* Do not actually evaluate the expression. */
28096 ++cp_unevaluated_operand;
28097 ++c_inhibit_evaluation_warnings;
28098 /* If it's a `(', then we might be looking at the type-id
28100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28102 tree type = NULL_TREE;
28104 /* We can't be sure yet whether we're looking at a type-id or an
28106 cp_parser_parse_tentatively (parser);
28108 matching_parens parens;
28109 parens.consume_open (parser);
28111 /* Note: as a GNU Extension, compound literals are considered
28112 postfix-expressions as they are in C99, so they are valid
28113 arguments to sizeof. See comment in cp_parser_cast_expression
28115 if (cp_parser_compound_literal_p (parser))
28116 cp_parser_simulate_error (parser);
28119 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28120 parser->in_type_id_in_expr_p = true;
28121 /* Look for the type-id. */
28122 type = cp_parser_type_id (parser);
28123 /* Look for the closing `)'. */
28124 parens.require_close (parser);
28125 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28128 /* If all went well, then we're done. */
28129 if (cp_parser_parse_definitely (parser))
28131 cp_decl_specifier_seq decl_specs;
28133 /* Build a trivial decl-specifier-seq. */
28134 clear_decl_specs (&decl_specs);
28135 decl_specs.type = type;
28137 /* Call grokdeclarator to figure out what type this is. */
28138 expr = grokdeclarator (NULL,
28142 /*attrlist=*/NULL);
28146 /* If the type-id production did not work out, then we must be
28147 looking at the unary-expression production. */
28149 expr = cp_parser_unary_expression (parser);
28151 /* Go back to evaluating expressions. */
28152 --cp_unevaluated_operand;
28153 --c_inhibit_evaluation_warnings;
28155 /* Free the message we created. */
28157 /* And restore the old one. */
28158 parser->type_definition_forbidden_message = saved_message;
28159 parser->integral_constant_expression_p
28160 = saved_integral_constant_expression_p;
28161 parser->non_integral_constant_expression_p
28162 = saved_non_integral_constant_expression_p;
28167 /* If the current declaration has no declarator, return true. */
28170 cp_parser_declares_only_class_p (cp_parser *parser)
28172 /* If the next token is a `;' or a `,' then there is no
28174 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28175 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28178 /* Update the DECL_SPECS to reflect the storage class indicated by
28182 cp_parser_set_storage_class (cp_parser *parser,
28183 cp_decl_specifier_seq *decl_specs,
28187 cp_storage_class storage_class;
28189 if (parser->in_unbraced_linkage_specification_p)
28191 error_at (token->location, "invalid use of %qD in linkage specification",
28192 ridpointers[keyword]);
28195 else if (decl_specs->storage_class != sc_none)
28197 decl_specs->conflicting_specifiers_p = true;
28201 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28202 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28203 && decl_specs->gnu_thread_keyword_p)
28205 pedwarn (decl_specs->locations[ds_thread], 0,
28206 "%<__thread%> before %qD", ridpointers[keyword]);
28212 storage_class = sc_auto;
28215 storage_class = sc_register;
28218 storage_class = sc_static;
28221 storage_class = sc_extern;
28224 storage_class = sc_mutable;
28227 gcc_unreachable ();
28229 decl_specs->storage_class = storage_class;
28230 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28232 /* A storage class specifier cannot be applied alongside a typedef
28233 specifier. If there is a typedef specifier present then set
28234 conflicting_specifiers_p which will trigger an error later
28235 on in grokdeclarator. */
28236 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28237 decl_specs->conflicting_specifiers_p = true;
28240 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28241 is true, the type is a class or enum definition. */
28244 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28247 bool type_definition_p)
28249 decl_specs->any_specifiers_p = true;
28251 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28252 (with, for example, in "typedef int wchar_t;") we remember that
28253 this is what happened. In system headers, we ignore these
28254 declarations so that G++ can work with system headers that are not
28256 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28257 && !type_definition_p
28258 && (type_spec == boolean_type_node
28259 || type_spec == char16_type_node
28260 || type_spec == char32_type_node
28261 || type_spec == wchar_type_node)
28262 && (decl_specs->type
28263 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28264 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28265 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28266 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28268 decl_specs->redefined_builtin_type = type_spec;
28269 set_and_check_decl_spec_loc (decl_specs,
28270 ds_redefined_builtin_type_spec,
28272 if (!decl_specs->type)
28274 decl_specs->type = type_spec;
28275 decl_specs->type_definition_p = false;
28276 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28279 else if (decl_specs->type)
28280 decl_specs->multiple_types_p = true;
28283 decl_specs->type = type_spec;
28284 decl_specs->type_definition_p = type_definition_p;
28285 decl_specs->redefined_builtin_type = NULL_TREE;
28286 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28290 /* True iff TOKEN is the GNU keyword __thread. */
28293 token_is__thread (cp_token *token)
28295 gcc_assert (token->keyword == RID_THREAD);
28296 return id_equal (token->u.value, "__thread");
28299 /* Set the location for a declarator specifier and check if it is
28302 DECL_SPECS is the sequence of declarator specifiers onto which to
28305 DS is the single declarator specifier to set which location is to
28306 be set onto the existing sequence of declarators.
28308 LOCATION is the location for the declarator specifier to
28312 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28313 cp_decl_spec ds, cp_token *token)
28315 gcc_assert (ds < ds_last);
28317 if (decl_specs == NULL)
28320 source_location location = token->location;
28322 if (decl_specs->locations[ds] == 0)
28324 decl_specs->locations[ds] = location;
28325 if (ds == ds_thread)
28326 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28332 if (decl_specs->locations[ds_long_long] != 0)
28333 error_at (location,
28334 "%<long long long%> is too long for GCC");
28337 decl_specs->locations[ds_long_long] = location;
28338 pedwarn_cxx98 (location,
28340 "ISO C++ 1998 does not support %<long long%>");
28343 else if (ds == ds_thread)
28345 bool gnu = token_is__thread (token);
28346 if (gnu != decl_specs->gnu_thread_keyword_p)
28347 error_at (location,
28348 "both %<__thread%> and %<thread_local%> specified");
28351 gcc_rich_location richloc (location);
28352 richloc.add_fixit_remove ();
28353 error_at (&richloc, "duplicate %qD", token->u.value);
28358 static const char *const decl_spec_names[] = {
28375 gcc_rich_location richloc (location);
28376 richloc.add_fixit_remove ();
28377 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28382 /* Return true iff the declarator specifier DS is present in the
28383 sequence of declarator specifiers DECL_SPECS. */
28386 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28389 gcc_assert (ds < ds_last);
28391 if (decl_specs == NULL)
28394 return decl_specs->locations[ds] != 0;
28397 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28398 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28401 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28403 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28406 /* Issue an error message indicating that TOKEN_DESC was expected.
28407 If KEYWORD is true, it indicated this function is called by
28408 cp_parser_require_keword and the required token can only be
28409 a indicated keyword.
28411 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28412 within any error as the location of an "opening" token matching
28413 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28414 RT_CLOSE_PAREN). */
28417 cp_parser_required_error (cp_parser *parser,
28418 required_token token_desc,
28420 location_t matching_location)
28422 if (cp_parser_simulate_error (parser))
28425 const char *gmsgid = NULL;
28426 switch (token_desc)
28429 gmsgid = G_("expected %<new%>");
28432 gmsgid = G_("expected %<delete%>");
28435 gmsgid = G_("expected %<return%>");
28438 gmsgid = G_("expected %<while%>");
28441 gmsgid = G_("expected %<extern%>");
28443 case RT_STATIC_ASSERT:
28444 gmsgid = G_("expected %<static_assert%>");
28447 gmsgid = G_("expected %<decltype%>");
28450 gmsgid = G_("expected %<operator%>");
28453 gmsgid = G_("expected %<class%>");
28456 gmsgid = G_("expected %<template%>");
28459 gmsgid = G_("expected %<namespace%>");
28462 gmsgid = G_("expected %<using%>");
28465 gmsgid = G_("expected %<asm%>");
28468 gmsgid = G_("expected %<try%>");
28471 gmsgid = G_("expected %<catch%>");
28474 gmsgid = G_("expected %<throw%>");
28477 gmsgid = G_("expected %<__label__%>");
28480 gmsgid = G_("expected %<@try%>");
28482 case RT_AT_SYNCHRONIZED:
28483 gmsgid = G_("expected %<@synchronized%>");
28486 gmsgid = G_("expected %<@throw%>");
28488 case RT_TRANSACTION_ATOMIC:
28489 gmsgid = G_("expected %<__transaction_atomic%>");
28491 case RT_TRANSACTION_RELAXED:
28492 gmsgid = G_("expected %<__transaction_relaxed%>");
28498 if (!gmsgid && !keyword)
28500 switch (token_desc)
28503 gmsgid = G_("expected %<;%>");
28505 case RT_OPEN_PAREN:
28506 gmsgid = G_("expected %<(%>");
28508 case RT_CLOSE_BRACE:
28509 gmsgid = G_("expected %<}%>");
28511 case RT_OPEN_BRACE:
28512 gmsgid = G_("expected %<{%>");
28514 case RT_CLOSE_SQUARE:
28515 gmsgid = G_("expected %<]%>");
28517 case RT_OPEN_SQUARE:
28518 gmsgid = G_("expected %<[%>");
28521 gmsgid = G_("expected %<,%>");
28524 gmsgid = G_("expected %<::%>");
28527 gmsgid = G_("expected %<<%>");
28530 gmsgid = G_("expected %<>%>");
28533 gmsgid = G_("expected %<=%>");
28536 gmsgid = G_("expected %<...%>");
28539 gmsgid = G_("expected %<*%>");
28542 gmsgid = G_("expected %<~%>");
28545 gmsgid = G_("expected %<:%>");
28547 case RT_COLON_SCOPE:
28548 gmsgid = G_("expected %<:%> or %<::%>");
28550 case RT_CLOSE_PAREN:
28551 gmsgid = G_("expected %<)%>");
28553 case RT_COMMA_CLOSE_PAREN:
28554 gmsgid = G_("expected %<,%> or %<)%>");
28556 case RT_PRAGMA_EOL:
28557 gmsgid = G_("expected end of line");
28560 gmsgid = G_("expected identifier");
28563 gmsgid = G_("expected selection-statement");
28566 gmsgid = G_("expected iteration-statement");
28569 gmsgid = G_("expected jump-statement");
28572 gmsgid = G_("expected class-key");
28574 case RT_CLASS_TYPENAME_TEMPLATE:
28575 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28578 gcc_unreachable ();
28583 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28587 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28588 issue an error message indicating that TOKEN_DESC was expected.
28590 Returns the token consumed, if the token had the appropriate type.
28591 Otherwise, returns NULL.
28593 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28594 within any error as the location of an "opening" token matching
28595 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28596 RT_CLOSE_PAREN). */
28599 cp_parser_require (cp_parser* parser,
28600 enum cpp_ttype type,
28601 required_token token_desc,
28602 location_t matching_location)
28604 if (cp_lexer_next_token_is (parser->lexer, type))
28605 return cp_lexer_consume_token (parser->lexer);
28608 /* Output the MESSAGE -- unless we're parsing tentatively. */
28609 if (!cp_parser_simulate_error (parser))
28610 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28611 matching_location);
28616 /* An error message is produced if the next token is not '>'.
28617 All further tokens are skipped until the desired token is
28618 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28621 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28623 /* Current level of '< ... >'. */
28624 unsigned level = 0;
28625 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28626 unsigned nesting_depth = 0;
28628 /* Are we ready, yet? If not, issue error message. */
28629 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28632 /* Skip tokens until the desired token is found. */
28635 /* Peek at the next token. */
28636 switch (cp_lexer_peek_token (parser->lexer)->type)
28639 if (!nesting_depth)
28644 if (cxx_dialect == cxx98)
28645 /* C++0x views the `>>' operator as two `>' tokens, but
28648 else if (!nesting_depth && level-- == 0)
28650 /* We've hit a `>>' where the first `>' closes the
28651 template argument list, and the second `>' is
28652 spurious. Just consume the `>>' and stop; we've
28653 already produced at least one error. */
28654 cp_lexer_consume_token (parser->lexer);
28657 /* Fall through for C++0x, so we handle the second `>' in
28659 gcc_fallthrough ();
28662 if (!nesting_depth && level-- == 0)
28664 /* We've reached the token we want, consume it and stop. */
28665 cp_lexer_consume_token (parser->lexer);
28670 case CPP_OPEN_PAREN:
28671 case CPP_OPEN_SQUARE:
28675 case CPP_CLOSE_PAREN:
28676 case CPP_CLOSE_SQUARE:
28677 if (nesting_depth-- == 0)
28682 case CPP_PRAGMA_EOL:
28683 case CPP_SEMICOLON:
28684 case CPP_OPEN_BRACE:
28685 case CPP_CLOSE_BRACE:
28686 /* The '>' was probably forgotten, don't look further. */
28693 /* Consume this token. */
28694 cp_lexer_consume_token (parser->lexer);
28698 /* If the next token is the indicated keyword, consume it. Otherwise,
28699 issue an error message indicating that TOKEN_DESC was expected.
28701 Returns the token consumed, if the token had the appropriate type.
28702 Otherwise, returns NULL. */
28705 cp_parser_require_keyword (cp_parser* parser,
28707 required_token token_desc)
28709 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28711 if (token && token->keyword != keyword)
28713 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28721 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28722 function-definition. */
28725 cp_parser_token_starts_function_definition_p (cp_token* token)
28727 return (/* An ordinary function-body begins with an `{'. */
28728 token->type == CPP_OPEN_BRACE
28729 /* A ctor-initializer begins with a `:'. */
28730 || token->type == CPP_COLON
28731 /* A function-try-block begins with `try'. */
28732 || token->keyword == RID_TRY
28733 /* A function-transaction-block begins with `__transaction_atomic'
28734 or `__transaction_relaxed'. */
28735 || token->keyword == RID_TRANSACTION_ATOMIC
28736 || token->keyword == RID_TRANSACTION_RELAXED
28737 /* The named return value extension begins with `return'. */
28738 || token->keyword == RID_RETURN);
28741 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28745 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28749 token = cp_lexer_peek_token (parser->lexer);
28750 return (token->type == CPP_OPEN_BRACE
28751 || (token->type == CPP_COLON
28752 && !parser->colon_doesnt_start_class_def_p));
28755 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28756 C++0x) ending a template-argument. */
28759 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28763 token = cp_lexer_peek_token (parser->lexer);
28764 return (token->type == CPP_COMMA
28765 || token->type == CPP_GREATER
28766 || token->type == CPP_ELLIPSIS
28767 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28770 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28771 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28774 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28779 token = cp_lexer_peek_nth_token (parser->lexer, n);
28780 if (token->type == CPP_LESS)
28782 /* Check for the sequence `<::' in the original code. It would be lexed as
28783 `[:', where `[' is a digraph, and there is no whitespace before
28785 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28788 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28789 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28795 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28796 or none_type otherwise. */
28798 static enum tag_types
28799 cp_parser_token_is_class_key (cp_token* token)
28801 switch (token->keyword)
28806 return record_type;
28815 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28816 or none_type otherwise or if the token is null. */
28818 static enum tag_types
28819 cp_parser_token_is_type_parameter_key (cp_token* token)
28824 switch (token->keyword)
28829 return typename_type;
28836 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28839 cp_parser_check_class_key (enum tag_types class_key, tree type)
28841 if (type == error_mark_node)
28843 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28845 if (permerror (input_location, "%qs tag used in naming %q#T",
28846 class_key == union_type ? "union"
28847 : class_key == record_type ? "struct" : "class",
28849 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28850 "%q#T was previously declared here", type);
28854 /* Issue an error message if DECL is redeclared with different
28855 access than its original declaration [class.access.spec/3].
28856 This applies to nested classes, nested class templates and
28857 enumerations [class.mem/1]. */
28860 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28863 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28864 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28867 if ((TREE_PRIVATE (decl)
28868 != (current_access_specifier == access_private_node))
28869 || (TREE_PROTECTED (decl)
28870 != (current_access_specifier == access_protected_node)))
28871 error_at (location, "%qD redeclared with different access", decl);
28874 /* Look for the `template' keyword, as a syntactic disambiguator.
28875 Return TRUE iff it is present, in which case it will be
28879 cp_parser_optional_template_keyword (cp_parser *parser)
28881 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28883 /* In C++98 the `template' keyword can only be used within templates;
28884 outside templates the parser can always figure out what is a
28885 template and what is not. In C++11, per the resolution of DR 468,
28886 `template' is allowed in cases where it is not strictly necessary. */
28887 if (!processing_template_decl
28888 && pedantic && cxx_dialect == cxx98)
28890 cp_token *token = cp_lexer_peek_token (parser->lexer);
28891 pedwarn (token->location, OPT_Wpedantic,
28892 "in C++98 %<template%> (as a disambiguator) is only "
28893 "allowed within templates");
28894 /* If this part of the token stream is rescanned, the same
28895 error message would be generated. So, we purge the token
28896 from the stream. */
28897 cp_lexer_purge_token (parser->lexer);
28902 /* Consume the `template' keyword. */
28903 cp_lexer_consume_token (parser->lexer);
28910 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28911 set PARSER->SCOPE, and perform other related actions. */
28914 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28916 struct tree_check *check_value;
28918 /* Get the stored value. */
28919 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28920 /* Set the scope from the stored value. */
28921 parser->scope = saved_checks_value (check_value);
28922 parser->qualifying_scope = check_value->qualifying_scope;
28923 parser->object_scope = NULL_TREE;
28926 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28927 encounter the end of a block before what we were looking for. */
28930 cp_parser_cache_group (cp_parser *parser,
28931 enum cpp_ttype end,
28936 cp_token *token = cp_lexer_peek_token (parser->lexer);
28938 /* Abort a parenthesized expression if we encounter a semicolon. */
28939 if ((end == CPP_CLOSE_PAREN || depth == 0)
28940 && token->type == CPP_SEMICOLON)
28942 /* If we've reached the end of the file, stop. */
28943 if (token->type == CPP_EOF
28944 || (end != CPP_PRAGMA_EOL
28945 && token->type == CPP_PRAGMA_EOL))
28947 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28948 /* We've hit the end of an enclosing block, so there's been some
28949 kind of syntax error. */
28952 /* Consume the token. */
28953 cp_lexer_consume_token (parser->lexer);
28954 /* See if it starts a new group. */
28955 if (token->type == CPP_OPEN_BRACE)
28957 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28958 /* In theory this should probably check end == '}', but
28959 cp_parser_save_member_function_body needs it to exit
28960 after either '}' or ')' when called with ')'. */
28964 else if (token->type == CPP_OPEN_PAREN)
28966 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28967 if (depth == 0 && end == CPP_CLOSE_PAREN)
28970 else if (token->type == CPP_PRAGMA)
28971 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28972 else if (token->type == end)
28977 /* Like above, for caching a default argument or NSDMI. Both of these are
28978 terminated by a non-nested comma, but it can be unclear whether or not a
28979 comma is nested in a template argument list unless we do more parsing.
28980 In order to handle this ambiguity, when we encounter a ',' after a '<'
28981 we try to parse what follows as a parameter-declaration-list (in the
28982 case of a default argument) or a member-declarator (in the case of an
28983 NSDMI). If that succeeds, then we stop caching. */
28986 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28988 unsigned depth = 0;
28989 int maybe_template_id = 0;
28990 cp_token *first_token;
28992 tree default_argument;
28994 /* Add tokens until we have processed the entire default
28995 argument. We add the range [first_token, token). */
28996 first_token = cp_lexer_peek_token (parser->lexer);
28997 if (first_token->type == CPP_OPEN_BRACE)
28999 /* For list-initialization, this is straightforward. */
29000 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29001 token = cp_lexer_peek_token (parser->lexer);
29007 /* Peek at the next token. */
29008 token = cp_lexer_peek_token (parser->lexer);
29009 /* What we do depends on what token we have. */
29010 switch (token->type)
29012 /* In valid code, a default argument must be
29013 immediately followed by a `,' `)', or `...'. */
29015 if (depth == 0 && maybe_template_id)
29017 /* If we've seen a '<', we might be in a
29018 template-argument-list. Until Core issue 325 is
29019 resolved, we don't know how this situation ought
29020 to be handled, so try to DTRT. We check whether
29021 what comes after the comma is a valid parameter
29022 declaration list. If it is, then the comma ends
29023 the default argument; otherwise the default
29024 argument continues. */
29025 bool error = false;
29028 /* Set ITALP so cp_parser_parameter_declaration_list
29029 doesn't decide to commit to this parse. */
29030 bool saved_italp = parser->in_template_argument_list_p;
29031 parser->in_template_argument_list_p = true;
29033 cp_parser_parse_tentatively (parser);
29037 /* Parse declarators until we reach a non-comma or
29038 somthing that cannot be an initializer.
29039 Just checking whether we're looking at a single
29040 declarator is insufficient. Consider:
29041 int var = tuple<T,U>::x;
29042 The template parameter 'U' looks exactly like a
29046 int ctor_dtor_or_conv_p;
29047 cp_lexer_consume_token (parser->lexer);
29048 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29049 &ctor_dtor_or_conv_p,
29050 /*parenthesized_p=*/NULL,
29052 /*friend_p=*/false);
29053 peek = cp_lexer_peek_token (parser->lexer);
29054 if (cp_parser_error_occurred (parser))
29057 while (peek->type == CPP_COMMA);
29058 /* If we met an '=' or ';' then the original comma
29059 was the end of the NSDMI. Otherwise assume
29060 we're still in the NSDMI. */
29061 error = (peek->type != CPP_EQ
29062 && peek->type != CPP_SEMICOLON);
29066 cp_lexer_consume_token (parser->lexer);
29067 begin_scope (sk_function_parms, NULL_TREE);
29068 cp_parser_parameter_declaration_list (parser, &error);
29069 pop_bindings_and_leave_scope ();
29071 if (!cp_parser_error_occurred (parser) && !error)
29073 cp_parser_abort_tentative_parse (parser);
29075 parser->in_template_argument_list_p = saved_italp;
29079 case CPP_CLOSE_PAREN:
29081 /* If we run into a non-nested `;', `}', or `]',
29082 then the code is invalid -- but the default
29083 argument is certainly over. */
29084 case CPP_SEMICOLON:
29085 case CPP_CLOSE_BRACE:
29086 case CPP_CLOSE_SQUARE:
29088 /* Handle correctly int n = sizeof ... ( p ); */
29089 && token->type != CPP_ELLIPSIS)
29091 /* Update DEPTH, if necessary. */
29092 else if (token->type == CPP_CLOSE_PAREN
29093 || token->type == CPP_CLOSE_BRACE
29094 || token->type == CPP_CLOSE_SQUARE)
29098 case CPP_OPEN_PAREN:
29099 case CPP_OPEN_SQUARE:
29100 case CPP_OPEN_BRACE:
29106 /* This might be the comparison operator, or it might
29107 start a template argument list. */
29108 ++maybe_template_id;
29112 if (cxx_dialect == cxx98)
29114 /* Fall through for C++0x, which treats the `>>'
29115 operator like two `>' tokens in certain
29117 gcc_fallthrough ();
29122 /* This might be an operator, or it might close a
29123 template argument list. But if a previous '<'
29124 started a template argument list, this will have
29125 closed it, so we can't be in one anymore. */
29126 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29127 if (maybe_template_id < 0)
29128 maybe_template_id = 0;
29132 /* If we run out of tokens, issue an error message. */
29134 case CPP_PRAGMA_EOL:
29135 error_at (token->location, "file ends in default argument");
29136 return error_mark_node;
29140 /* In these cases, we should look for template-ids.
29141 For example, if the default argument is
29142 `X<int, double>()', we need to do name lookup to
29143 figure out whether or not `X' is a template; if
29144 so, the `,' does not end the default argument.
29146 That is not yet done. */
29153 /* If we've reached the end, stop. */
29157 /* Add the token to the token block. */
29158 token = cp_lexer_consume_token (parser->lexer);
29161 /* Create a DEFAULT_ARG to represent the unparsed default
29163 default_argument = make_node (DEFAULT_ARG);
29164 DEFARG_TOKENS (default_argument)
29165 = cp_token_cache_new (first_token, token);
29166 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29168 return default_argument;
29171 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29174 defarg_location (tree default_argument)
29176 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29177 location_t start = tokens->first->location;
29178 location_t end = tokens->last->location;
29179 return make_location (start, start, end);
29182 /* Begin parsing tentatively. We always save tokens while parsing
29183 tentatively so that if the tentative parsing fails we can restore the
29187 cp_parser_parse_tentatively (cp_parser* parser)
29189 /* Enter a new parsing context. */
29190 parser->context = cp_parser_context_new (parser->context);
29191 /* Begin saving tokens. */
29192 cp_lexer_save_tokens (parser->lexer);
29193 /* In order to avoid repetitive access control error messages,
29194 access checks are queued up until we are no longer parsing
29196 push_deferring_access_checks (dk_deferred);
29199 /* Commit to the currently active tentative parse. */
29202 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29204 cp_parser_context *context;
29207 /* Mark all of the levels as committed. */
29208 lexer = parser->lexer;
29209 for (context = parser->context; context->next; context = context->next)
29211 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29213 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29214 while (!cp_lexer_saving_tokens (lexer))
29215 lexer = lexer->next;
29216 cp_lexer_commit_tokens (lexer);
29220 /* Commit to the topmost currently active tentative parse.
29222 Note that this function shouldn't be called when there are
29223 irreversible side-effects while in a tentative state. For
29224 example, we shouldn't create a permanent entry in the symbol
29225 table, or issue an error message that might not apply if the
29226 tentative parse is aborted. */
29229 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29231 cp_parser_context *context = parser->context;
29232 cp_lexer *lexer = parser->lexer;
29236 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29238 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29240 while (!cp_lexer_saving_tokens (lexer))
29241 lexer = lexer->next;
29242 cp_lexer_commit_tokens (lexer);
29246 /* Abort the currently active tentative parse. All consumed tokens
29247 will be rolled back, and no diagnostics will be issued. */
29250 cp_parser_abort_tentative_parse (cp_parser* parser)
29252 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29253 || errorcount > 0);
29254 cp_parser_simulate_error (parser);
29255 /* Now, pretend that we want to see if the construct was
29256 successfully parsed. */
29257 cp_parser_parse_definitely (parser);
29260 /* Stop parsing tentatively. If a parse error has occurred, restore the
29261 token stream. Otherwise, commit to the tokens we have consumed.
29262 Returns true if no error occurred; false otherwise. */
29265 cp_parser_parse_definitely (cp_parser* parser)
29267 bool error_occurred;
29268 cp_parser_context *context;
29270 /* Remember whether or not an error occurred, since we are about to
29271 destroy that information. */
29272 error_occurred = cp_parser_error_occurred (parser);
29273 /* Remove the topmost context from the stack. */
29274 context = parser->context;
29275 parser->context = context->next;
29276 /* If no parse errors occurred, commit to the tentative parse. */
29277 if (!error_occurred)
29279 /* Commit to the tokens read tentatively, unless that was
29281 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29282 cp_lexer_commit_tokens (parser->lexer);
29284 pop_to_parent_deferring_access_checks ();
29286 /* Otherwise, if errors occurred, roll back our state so that things
29287 are just as they were before we began the tentative parse. */
29290 cp_lexer_rollback_tokens (parser->lexer);
29291 pop_deferring_access_checks ();
29293 /* Add the context to the front of the free list. */
29294 context->next = cp_parser_context_free_list;
29295 cp_parser_context_free_list = context;
29297 return !error_occurred;
29300 /* Returns true if we are parsing tentatively and are not committed to
29301 this tentative parse. */
29304 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29306 return (cp_parser_parsing_tentatively (parser)
29307 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29310 /* Returns nonzero iff an error has occurred during the most recent
29311 tentative parse. */
29314 cp_parser_error_occurred (cp_parser* parser)
29316 return (cp_parser_parsing_tentatively (parser)
29317 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29320 /* Returns nonzero if GNU extensions are allowed. */
29323 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29325 return parser->allow_gnu_extensions_p;
29328 /* Objective-C++ Productions */
29331 /* Parse an Objective-C expression, which feeds into a primary-expression
29335 objc-message-expression
29336 objc-string-literal
29337 objc-encode-expression
29338 objc-protocol-expression
29339 objc-selector-expression
29341 Returns a tree representation of the expression. */
29344 cp_parser_objc_expression (cp_parser* parser)
29346 /* Try to figure out what kind of declaration is present. */
29347 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29351 case CPP_OPEN_SQUARE:
29352 return cp_parser_objc_message_expression (parser);
29354 case CPP_OBJC_STRING:
29355 kwd = cp_lexer_consume_token (parser->lexer);
29356 return objc_build_string_object (kwd->u.value);
29359 switch (kwd->keyword)
29361 case RID_AT_ENCODE:
29362 return cp_parser_objc_encode_expression (parser);
29364 case RID_AT_PROTOCOL:
29365 return cp_parser_objc_protocol_expression (parser);
29367 case RID_AT_SELECTOR:
29368 return cp_parser_objc_selector_expression (parser);
29375 error_at (kwd->location,
29376 "misplaced %<@%D%> Objective-C++ construct",
29378 cp_parser_skip_to_end_of_block_or_statement (parser);
29381 return error_mark_node;
29384 /* Parse an Objective-C message expression.
29386 objc-message-expression:
29387 [ objc-message-receiver objc-message-args ]
29389 Returns a representation of an Objective-C message. */
29392 cp_parser_objc_message_expression (cp_parser* parser)
29394 tree receiver, messageargs;
29396 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29397 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29398 receiver = cp_parser_objc_message_receiver (parser);
29399 messageargs = cp_parser_objc_message_args (parser);
29400 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29401 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29403 tree result = objc_build_message_expr (receiver, messageargs);
29405 /* Construct a location e.g.
29408 ranging from the '[' to the ']', with the caret at the start. */
29409 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29410 protected_set_expr_location (result, combined_loc);
29415 /* Parse an objc-message-receiver.
29417 objc-message-receiver:
29419 simple-type-specifier
29421 Returns a representation of the type or expression. */
29424 cp_parser_objc_message_receiver (cp_parser* parser)
29428 /* An Objective-C message receiver may be either (1) a type
29429 or (2) an expression. */
29430 cp_parser_parse_tentatively (parser);
29431 rcv = cp_parser_expression (parser);
29433 /* If that worked out, fine. */
29434 if (cp_parser_parse_definitely (parser))
29437 cp_parser_parse_tentatively (parser);
29438 rcv = cp_parser_simple_type_specifier (parser,
29439 /*decl_specs=*/NULL,
29440 CP_PARSER_FLAGS_NONE);
29442 if (cp_parser_parse_definitely (parser))
29443 return objc_get_class_reference (rcv);
29445 cp_parser_error (parser, "objective-c++ message receiver expected");
29446 return error_mark_node;
29449 /* Parse the arguments and selectors comprising an Objective-C message.
29454 objc-selector-args , objc-comma-args
29456 objc-selector-args:
29457 objc-selector [opt] : assignment-expression
29458 objc-selector-args objc-selector [opt] : assignment-expression
29461 assignment-expression
29462 objc-comma-args , assignment-expression
29464 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29465 selector arguments and TREE_VALUE containing a list of comma
29469 cp_parser_objc_message_args (cp_parser* parser)
29471 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29472 bool maybe_unary_selector_p = true;
29473 cp_token *token = cp_lexer_peek_token (parser->lexer);
29475 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29477 tree selector = NULL_TREE, arg;
29479 if (token->type != CPP_COLON)
29480 selector = cp_parser_objc_selector (parser);
29482 /* Detect if we have a unary selector. */
29483 if (maybe_unary_selector_p
29484 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29485 return build_tree_list (selector, NULL_TREE);
29487 maybe_unary_selector_p = false;
29488 cp_parser_require (parser, CPP_COLON, RT_COLON);
29489 arg = cp_parser_assignment_expression (parser);
29492 = chainon (sel_args,
29493 build_tree_list (selector, arg));
29495 token = cp_lexer_peek_token (parser->lexer);
29498 /* Handle non-selector arguments, if any. */
29499 while (token->type == CPP_COMMA)
29503 cp_lexer_consume_token (parser->lexer);
29504 arg = cp_parser_assignment_expression (parser);
29507 = chainon (addl_args,
29508 build_tree_list (NULL_TREE, arg));
29510 token = cp_lexer_peek_token (parser->lexer);
29513 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29515 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29516 return build_tree_list (error_mark_node, error_mark_node);
29519 return build_tree_list (sel_args, addl_args);
29522 /* Parse an Objective-C encode expression.
29524 objc-encode-expression:
29525 @encode objc-typename
29527 Returns an encoded representation of the type argument. */
29530 cp_parser_objc_encode_expression (cp_parser* parser)
29534 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29536 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29537 matching_parens parens;
29538 parens.require_open (parser);
29539 token = cp_lexer_peek_token (parser->lexer);
29540 type = complete_type (cp_parser_type_id (parser));
29541 parens.require_close (parser);
29545 error_at (token->location,
29546 "%<@encode%> must specify a type as an argument");
29547 return error_mark_node;
29550 /* This happens if we find @encode(T) (where T is a template
29551 typename or something dependent on a template typename) when
29552 parsing a template. In that case, we can't compile it
29553 immediately, but we rather create an AT_ENCODE_EXPR which will
29554 need to be instantiated when the template is used.
29556 if (dependent_type_p (type))
29558 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29559 TREE_READONLY (value) = 1;
29564 /* Build a location of the form:
29567 with caret==start at the @ token, finishing at the close paren. */
29568 location_t combined_loc
29569 = make_location (start_loc, start_loc,
29570 cp_lexer_previous_token (parser->lexer)->location);
29572 return cp_expr (objc_build_encode_expr (type), combined_loc);
29575 /* Parse an Objective-C @defs expression. */
29578 cp_parser_objc_defs_expression (cp_parser *parser)
29582 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29583 matching_parens parens;
29584 parens.require_open (parser);
29585 name = cp_parser_identifier (parser);
29586 parens.require_close (parser);
29588 return objc_get_class_ivars (name);
29591 /* Parse an Objective-C protocol expression.
29593 objc-protocol-expression:
29594 @protocol ( identifier )
29596 Returns a representation of the protocol expression. */
29599 cp_parser_objc_protocol_expression (cp_parser* parser)
29602 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29604 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29605 matching_parens parens;
29606 parens.require_open (parser);
29607 proto = cp_parser_identifier (parser);
29608 parens.require_close (parser);
29610 /* Build a location of the form:
29613 with caret==start at the @ token, finishing at the close paren. */
29614 location_t combined_loc
29615 = make_location (start_loc, start_loc,
29616 cp_lexer_previous_token (parser->lexer)->location);
29617 tree result = objc_build_protocol_expr (proto);
29618 protected_set_expr_location (result, combined_loc);
29622 /* Parse an Objective-C selector expression.
29624 objc-selector-expression:
29625 @selector ( objc-method-signature )
29627 objc-method-signature:
29633 objc-selector-seq objc-selector :
29635 Returns a representation of the method selector. */
29638 cp_parser_objc_selector_expression (cp_parser* parser)
29640 tree sel_seq = NULL_TREE;
29641 bool maybe_unary_selector_p = true;
29643 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29645 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29646 matching_parens parens;
29647 parens.require_open (parser);
29648 token = cp_lexer_peek_token (parser->lexer);
29650 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29651 || token->type == CPP_SCOPE)
29653 tree selector = NULL_TREE;
29655 if (token->type != CPP_COLON
29656 || token->type == CPP_SCOPE)
29657 selector = cp_parser_objc_selector (parser);
29659 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29660 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29662 /* Detect if we have a unary selector. */
29663 if (maybe_unary_selector_p)
29665 sel_seq = selector;
29666 goto finish_selector;
29670 cp_parser_error (parser, "expected %<:%>");
29673 maybe_unary_selector_p = false;
29674 token = cp_lexer_consume_token (parser->lexer);
29676 if (token->type == CPP_SCOPE)
29679 = chainon (sel_seq,
29680 build_tree_list (selector, NULL_TREE));
29682 = chainon (sel_seq,
29683 build_tree_list (NULL_TREE, NULL_TREE));
29687 = chainon (sel_seq,
29688 build_tree_list (selector, NULL_TREE));
29690 token = cp_lexer_peek_token (parser->lexer);
29694 parens.require_close (parser);
29697 /* Build a location of the form:
29700 with caret==start at the @ token, finishing at the close paren. */
29701 location_t combined_loc
29702 = make_location (loc, loc,
29703 cp_lexer_previous_token (parser->lexer)->location);
29704 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29705 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29706 protected_set_expr_location (result, combined_loc);
29710 /* Parse a list of identifiers.
29712 objc-identifier-list:
29714 objc-identifier-list , identifier
29716 Returns a TREE_LIST of identifier nodes. */
29719 cp_parser_objc_identifier_list (cp_parser* parser)
29725 identifier = cp_parser_identifier (parser);
29726 if (identifier == error_mark_node)
29727 return error_mark_node;
29729 list = build_tree_list (NULL_TREE, identifier);
29730 sep = cp_lexer_peek_token (parser->lexer);
29732 while (sep->type == CPP_COMMA)
29734 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29735 identifier = cp_parser_identifier (parser);
29736 if (identifier == error_mark_node)
29739 list = chainon (list, build_tree_list (NULL_TREE,
29741 sep = cp_lexer_peek_token (parser->lexer);
29747 /* Parse an Objective-C alias declaration.
29749 objc-alias-declaration:
29750 @compatibility_alias identifier identifier ;
29752 This function registers the alias mapping with the Objective-C front end.
29753 It returns nothing. */
29756 cp_parser_objc_alias_declaration (cp_parser* parser)
29760 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29761 alias = cp_parser_identifier (parser);
29762 orig = cp_parser_identifier (parser);
29763 objc_declare_alias (alias, orig);
29764 cp_parser_consume_semicolon_at_end_of_statement (parser);
29767 /* Parse an Objective-C class forward-declaration.
29769 objc-class-declaration:
29770 @class objc-identifier-list ;
29772 The function registers the forward declarations with the Objective-C
29773 front end. It returns nothing. */
29776 cp_parser_objc_class_declaration (cp_parser* parser)
29778 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29783 id = cp_parser_identifier (parser);
29784 if (id == error_mark_node)
29787 objc_declare_class (id);
29789 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29790 cp_lexer_consume_token (parser->lexer);
29794 cp_parser_consume_semicolon_at_end_of_statement (parser);
29797 /* Parse a list of Objective-C protocol references.
29799 objc-protocol-refs-opt:
29800 objc-protocol-refs [opt]
29802 objc-protocol-refs:
29803 < objc-identifier-list >
29805 Returns a TREE_LIST of identifiers, if any. */
29808 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29810 tree protorefs = NULL_TREE;
29812 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29814 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29815 protorefs = cp_parser_objc_identifier_list (parser);
29816 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29822 /* Parse a Objective-C visibility specification. */
29825 cp_parser_objc_visibility_spec (cp_parser* parser)
29827 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29829 switch (vis->keyword)
29831 case RID_AT_PRIVATE:
29832 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29834 case RID_AT_PROTECTED:
29835 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29837 case RID_AT_PUBLIC:
29838 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29840 case RID_AT_PACKAGE:
29841 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29847 /* Eat '@private'/'@protected'/'@public'. */
29848 cp_lexer_consume_token (parser->lexer);
29851 /* Parse an Objective-C method type. Return 'true' if it is a class
29852 (+) method, and 'false' if it is an instance (-) method. */
29855 cp_parser_objc_method_type (cp_parser* parser)
29857 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29863 /* Parse an Objective-C protocol qualifier. */
29866 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29868 tree quals = NULL_TREE, node;
29869 cp_token *token = cp_lexer_peek_token (parser->lexer);
29871 node = token->u.value;
29873 while (node && identifier_p (node)
29874 && (node == ridpointers [(int) RID_IN]
29875 || node == ridpointers [(int) RID_OUT]
29876 || node == ridpointers [(int) RID_INOUT]
29877 || node == ridpointers [(int) RID_BYCOPY]
29878 || node == ridpointers [(int) RID_BYREF]
29879 || node == ridpointers [(int) RID_ONEWAY]))
29881 quals = tree_cons (NULL_TREE, node, quals);
29882 cp_lexer_consume_token (parser->lexer);
29883 token = cp_lexer_peek_token (parser->lexer);
29884 node = token->u.value;
29890 /* Parse an Objective-C typename. */
29893 cp_parser_objc_typename (cp_parser* parser)
29895 tree type_name = NULL_TREE;
29897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29899 tree proto_quals, cp_type = NULL_TREE;
29901 matching_parens parens;
29902 parens.consume_open (parser); /* Eat '('. */
29903 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29905 /* An ObjC type name may consist of just protocol qualifiers, in which
29906 case the type shall default to 'id'. */
29907 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29909 cp_type = cp_parser_type_id (parser);
29911 /* If the type could not be parsed, an error has already
29912 been produced. For error recovery, behave as if it had
29913 not been specified, which will use the default type
29915 if (cp_type == error_mark_node)
29917 cp_type = NULL_TREE;
29918 /* We need to skip to the closing parenthesis as
29919 cp_parser_type_id() does not seem to do it for
29921 cp_parser_skip_to_closing_parenthesis (parser,
29922 /*recovering=*/true,
29923 /*or_comma=*/false,
29924 /*consume_paren=*/false);
29928 parens.require_close (parser);
29929 type_name = build_tree_list (proto_quals, cp_type);
29935 /* Check to see if TYPE refers to an Objective-C selector name. */
29938 cp_parser_objc_selector_p (enum cpp_ttype type)
29940 return (type == CPP_NAME || type == CPP_KEYWORD
29941 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29942 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29943 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29944 || type == CPP_XOR || type == CPP_XOR_EQ);
29947 /* Parse an Objective-C selector. */
29950 cp_parser_objc_selector (cp_parser* parser)
29952 cp_token *token = cp_lexer_consume_token (parser->lexer);
29954 if (!cp_parser_objc_selector_p (token->type))
29956 error_at (token->location, "invalid Objective-C++ selector name");
29957 return error_mark_node;
29960 /* C++ operator names are allowed to appear in ObjC selectors. */
29961 switch (token->type)
29963 case CPP_AND_AND: return get_identifier ("and");
29964 case CPP_AND_EQ: return get_identifier ("and_eq");
29965 case CPP_AND: return get_identifier ("bitand");
29966 case CPP_OR: return get_identifier ("bitor");
29967 case CPP_COMPL: return get_identifier ("compl");
29968 case CPP_NOT: return get_identifier ("not");
29969 case CPP_NOT_EQ: return get_identifier ("not_eq");
29970 case CPP_OR_OR: return get_identifier ("or");
29971 case CPP_OR_EQ: return get_identifier ("or_eq");
29972 case CPP_XOR: return get_identifier ("xor");
29973 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29974 default: return token->u.value;
29978 /* Parse an Objective-C params list. */
29981 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29983 tree params = NULL_TREE;
29984 bool maybe_unary_selector_p = true;
29985 cp_token *token = cp_lexer_peek_token (parser->lexer);
29987 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29989 tree selector = NULL_TREE, type_name, identifier;
29990 tree parm_attr = NULL_TREE;
29992 if (token->keyword == RID_ATTRIBUTE)
29995 if (token->type != CPP_COLON)
29996 selector = cp_parser_objc_selector (parser);
29998 /* Detect if we have a unary selector. */
29999 if (maybe_unary_selector_p
30000 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30002 params = selector; /* Might be followed by attributes. */
30006 maybe_unary_selector_p = false;
30007 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30009 /* Something went quite wrong. There should be a colon
30010 here, but there is not. Stop parsing parameters. */
30013 type_name = cp_parser_objc_typename (parser);
30014 /* New ObjC allows attributes on parameters too. */
30015 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30016 parm_attr = cp_parser_attributes_opt (parser);
30017 identifier = cp_parser_identifier (parser);
30021 objc_build_keyword_decl (selector,
30026 token = cp_lexer_peek_token (parser->lexer);
30029 if (params == NULL_TREE)
30031 cp_parser_error (parser, "objective-c++ method declaration is expected");
30032 return error_mark_node;
30035 /* We allow tail attributes for the method. */
30036 if (token->keyword == RID_ATTRIBUTE)
30038 *attributes = cp_parser_attributes_opt (parser);
30039 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30040 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30042 cp_parser_error (parser,
30043 "method attributes must be specified at the end");
30044 return error_mark_node;
30047 if (params == NULL_TREE)
30049 cp_parser_error (parser, "objective-c++ method declaration is expected");
30050 return error_mark_node;
30055 /* Parse the non-keyword Objective-C params. */
30058 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30061 tree params = make_node (TREE_LIST);
30062 cp_token *token = cp_lexer_peek_token (parser->lexer);
30063 *ellipsisp = false; /* Initially, assume no ellipsis. */
30065 while (token->type == CPP_COMMA)
30067 cp_parameter_declarator *parmdecl;
30070 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30071 token = cp_lexer_peek_token (parser->lexer);
30073 if (token->type == CPP_ELLIPSIS)
30075 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30077 token = cp_lexer_peek_token (parser->lexer);
30081 /* TODO: parse attributes for tail parameters. */
30082 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30083 parm = grokdeclarator (parmdecl->declarator,
30084 &parmdecl->decl_specifiers,
30085 PARM, /*initialized=*/0,
30086 /*attrlist=*/NULL);
30088 chainon (params, build_tree_list (NULL_TREE, parm));
30089 token = cp_lexer_peek_token (parser->lexer);
30092 /* We allow tail attributes for the method. */
30093 if (token->keyword == RID_ATTRIBUTE)
30095 if (*attributes == NULL_TREE)
30097 *attributes = cp_parser_attributes_opt (parser);
30098 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30099 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30103 /* We have an error, but parse the attributes, so that we can
30105 *attributes = cp_parser_attributes_opt (parser);
30107 cp_parser_error (parser,
30108 "method attributes must be specified at the end");
30109 return error_mark_node;
30115 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30118 cp_parser_objc_interstitial_code (cp_parser* parser)
30120 cp_token *token = cp_lexer_peek_token (parser->lexer);
30122 /* If the next token is `extern' and the following token is a string
30123 literal, then we have a linkage specification. */
30124 if (token->keyword == RID_EXTERN
30125 && cp_parser_is_pure_string_literal
30126 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30127 cp_parser_linkage_specification (parser);
30128 /* Handle #pragma, if any. */
30129 else if (token->type == CPP_PRAGMA)
30130 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30131 /* Allow stray semicolons. */
30132 else if (token->type == CPP_SEMICOLON)
30133 cp_lexer_consume_token (parser->lexer);
30134 /* Mark methods as optional or required, when building protocols. */
30135 else if (token->keyword == RID_AT_OPTIONAL)
30137 cp_lexer_consume_token (parser->lexer);
30138 objc_set_method_opt (true);
30140 else if (token->keyword == RID_AT_REQUIRED)
30142 cp_lexer_consume_token (parser->lexer);
30143 objc_set_method_opt (false);
30145 else if (token->keyword == RID_NAMESPACE)
30146 cp_parser_namespace_definition (parser);
30147 /* Other stray characters must generate errors. */
30148 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30150 cp_lexer_consume_token (parser->lexer);
30151 error ("stray %qs between Objective-C++ methods",
30152 token->type == CPP_OPEN_BRACE ? "{" : "}");
30154 /* Finally, try to parse a block-declaration, or a function-definition. */
30156 cp_parser_block_declaration (parser, /*statement_p=*/false);
30159 /* Parse a method signature. */
30162 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30164 tree rettype, kwdparms, optparms;
30165 bool ellipsis = false;
30166 bool is_class_method;
30168 is_class_method = cp_parser_objc_method_type (parser);
30169 rettype = cp_parser_objc_typename (parser);
30170 *attributes = NULL_TREE;
30171 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30172 if (kwdparms == error_mark_node)
30173 return error_mark_node;
30174 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30175 if (optparms == error_mark_node)
30176 return error_mark_node;
30178 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30182 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30185 cp_lexer_save_tokens (parser->lexer);
30186 tattr = cp_parser_attributes_opt (parser);
30187 gcc_assert (tattr) ;
30189 /* If the attributes are followed by a method introducer, this is not allowed.
30190 Dump the attributes and flag the situation. */
30191 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30192 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30195 /* Otherwise, the attributes introduce some interstitial code, possibly so
30196 rewind to allow that check. */
30197 cp_lexer_rollback_tokens (parser->lexer);
30201 /* Parse an Objective-C method prototype list. */
30204 cp_parser_objc_method_prototype_list (cp_parser* parser)
30206 cp_token *token = cp_lexer_peek_token (parser->lexer);
30208 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30210 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30212 tree attributes, sig;
30213 bool is_class_method;
30214 if (token->type == CPP_PLUS)
30215 is_class_method = true;
30217 is_class_method = false;
30218 sig = cp_parser_objc_method_signature (parser, &attributes);
30219 if (sig == error_mark_node)
30221 cp_parser_skip_to_end_of_block_or_statement (parser);
30222 token = cp_lexer_peek_token (parser->lexer);
30225 objc_add_method_declaration (is_class_method, sig, attributes);
30226 cp_parser_consume_semicolon_at_end_of_statement (parser);
30228 else if (token->keyword == RID_AT_PROPERTY)
30229 cp_parser_objc_at_property_declaration (parser);
30230 else if (token->keyword == RID_ATTRIBUTE
30231 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30232 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30234 "prefix attributes are ignored for methods");
30236 /* Allow for interspersed non-ObjC++ code. */
30237 cp_parser_objc_interstitial_code (parser);
30239 token = cp_lexer_peek_token (parser->lexer);
30242 if (token->type != CPP_EOF)
30243 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30245 cp_parser_error (parser, "expected %<@end%>");
30247 objc_finish_interface ();
30250 /* Parse an Objective-C method definition list. */
30253 cp_parser_objc_method_definition_list (cp_parser* parser)
30255 cp_token *token = cp_lexer_peek_token (parser->lexer);
30257 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30261 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30264 tree sig, attribute;
30265 bool is_class_method;
30266 if (token->type == CPP_PLUS)
30267 is_class_method = true;
30269 is_class_method = false;
30270 push_deferring_access_checks (dk_deferred);
30271 sig = cp_parser_objc_method_signature (parser, &attribute);
30272 if (sig == error_mark_node)
30274 cp_parser_skip_to_end_of_block_or_statement (parser);
30275 token = cp_lexer_peek_token (parser->lexer);
30278 objc_start_method_definition (is_class_method, sig, attribute,
30281 /* For historical reasons, we accept an optional semicolon. */
30282 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30283 cp_lexer_consume_token (parser->lexer);
30285 ptk = cp_lexer_peek_token (parser->lexer);
30286 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30287 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30289 perform_deferred_access_checks (tf_warning_or_error);
30290 stop_deferring_access_checks ();
30291 meth = cp_parser_function_definition_after_declarator (parser,
30293 pop_deferring_access_checks ();
30294 objc_finish_method_definition (meth);
30297 /* The following case will be removed once @synthesize is
30298 completely implemented. */
30299 else if (token->keyword == RID_AT_PROPERTY)
30300 cp_parser_objc_at_property_declaration (parser);
30301 else if (token->keyword == RID_AT_SYNTHESIZE)
30302 cp_parser_objc_at_synthesize_declaration (parser);
30303 else if (token->keyword == RID_AT_DYNAMIC)
30304 cp_parser_objc_at_dynamic_declaration (parser);
30305 else if (token->keyword == RID_ATTRIBUTE
30306 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30307 warning_at (token->location, OPT_Wattributes,
30308 "prefix attributes are ignored for methods");
30310 /* Allow for interspersed non-ObjC++ code. */
30311 cp_parser_objc_interstitial_code (parser);
30313 token = cp_lexer_peek_token (parser->lexer);
30316 if (token->type != CPP_EOF)
30317 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30319 cp_parser_error (parser, "expected %<@end%>");
30321 objc_finish_implementation ();
30324 /* Parse Objective-C ivars. */
30327 cp_parser_objc_class_ivars (cp_parser* parser)
30329 cp_token *token = cp_lexer_peek_token (parser->lexer);
30331 if (token->type != CPP_OPEN_BRACE)
30332 return; /* No ivars specified. */
30334 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30335 token = cp_lexer_peek_token (parser->lexer);
30337 while (token->type != CPP_CLOSE_BRACE
30338 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30340 cp_decl_specifier_seq declspecs;
30341 int decl_class_or_enum_p;
30342 tree prefix_attributes;
30344 cp_parser_objc_visibility_spec (parser);
30346 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30349 cp_parser_decl_specifier_seq (parser,
30350 CP_PARSER_FLAGS_OPTIONAL,
30352 &decl_class_or_enum_p);
30354 /* auto, register, static, extern, mutable. */
30355 if (declspecs.storage_class != sc_none)
30357 cp_parser_error (parser, "invalid type for instance variable");
30358 declspecs.storage_class = sc_none;
30361 /* thread_local. */
30362 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30364 cp_parser_error (parser, "invalid type for instance variable");
30365 declspecs.locations[ds_thread] = 0;
30369 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30371 cp_parser_error (parser, "invalid type for instance variable");
30372 declspecs.locations[ds_typedef] = 0;
30375 prefix_attributes = declspecs.attributes;
30376 declspecs.attributes = NULL_TREE;
30378 /* Keep going until we hit the `;' at the end of the
30380 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30382 tree width = NULL_TREE, attributes, first_attribute, decl;
30383 cp_declarator *declarator = NULL;
30384 int ctor_dtor_or_conv_p;
30386 /* Check for a (possibly unnamed) bitfield declaration. */
30387 token = cp_lexer_peek_token (parser->lexer);
30388 if (token->type == CPP_COLON)
30391 if (token->type == CPP_NAME
30392 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30395 /* Get the name of the bitfield. */
30396 declarator = make_id_declarator (NULL_TREE,
30397 cp_parser_identifier (parser),
30401 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30402 /* Get the width of the bitfield. */
30404 = cp_parser_constant_expression (parser);
30408 /* Parse the declarator. */
30410 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30411 &ctor_dtor_or_conv_p,
30412 /*parenthesized_p=*/NULL,
30413 /*member_p=*/false,
30414 /*friend_p=*/false);
30417 /* Look for attributes that apply to the ivar. */
30418 attributes = cp_parser_attributes_opt (parser);
30419 /* Remember which attributes are prefix attributes and
30421 first_attribute = attributes;
30422 /* Combine the attributes. */
30423 attributes = attr_chainon (prefix_attributes, attributes);
30426 /* Create the bitfield declaration. */
30427 decl = grokbitfield (declarator, &declspecs,
30428 width, NULL_TREE, attributes);
30430 decl = grokfield (declarator, &declspecs,
30431 NULL_TREE, /*init_const_expr_p=*/false,
30432 NULL_TREE, attributes);
30434 /* Add the instance variable. */
30435 if (decl != error_mark_node && decl != NULL_TREE)
30436 objc_add_instance_variable (decl);
30438 /* Reset PREFIX_ATTRIBUTES. */
30439 if (attributes != error_mark_node)
30441 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30442 attributes = TREE_CHAIN (attributes);
30444 TREE_CHAIN (attributes) = NULL_TREE;
30447 token = cp_lexer_peek_token (parser->lexer);
30449 if (token->type == CPP_COMMA)
30451 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30457 cp_parser_consume_semicolon_at_end_of_statement (parser);
30458 token = cp_lexer_peek_token (parser->lexer);
30461 if (token->keyword == RID_AT_END)
30462 cp_parser_error (parser, "expected %<}%>");
30464 /* Do not consume the RID_AT_END, so it will be read again as terminating
30465 the @interface of @implementation. */
30466 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30467 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30469 /* For historical reasons, we accept an optional semicolon. */
30470 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30471 cp_lexer_consume_token (parser->lexer);
30474 /* Parse an Objective-C protocol declaration. */
30477 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30479 tree proto, protorefs;
30482 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30483 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30485 tok = cp_lexer_peek_token (parser->lexer);
30486 error_at (tok->location, "identifier expected after %<@protocol%>");
30487 cp_parser_consume_semicolon_at_end_of_statement (parser);
30491 /* See if we have a forward declaration or a definition. */
30492 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30494 /* Try a forward declaration first. */
30495 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30501 id = cp_parser_identifier (parser);
30502 if (id == error_mark_node)
30505 objc_declare_protocol (id, attributes);
30507 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30508 cp_lexer_consume_token (parser->lexer);
30512 cp_parser_consume_semicolon_at_end_of_statement (parser);
30515 /* Ok, we got a full-fledged definition (or at least should). */
30518 proto = cp_parser_identifier (parser);
30519 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30520 objc_start_protocol (proto, protorefs, attributes);
30521 cp_parser_objc_method_prototype_list (parser);
30525 /* Parse an Objective-C superclass or category. */
30528 cp_parser_objc_superclass_or_category (cp_parser *parser,
30531 tree *categ, bool *is_class_extension)
30533 cp_token *next = cp_lexer_peek_token (parser->lexer);
30535 *super = *categ = NULL_TREE;
30536 *is_class_extension = false;
30537 if (next->type == CPP_COLON)
30539 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30540 *super = cp_parser_identifier (parser);
30542 else if (next->type == CPP_OPEN_PAREN)
30544 matching_parens parens;
30545 parens.consume_open (parser); /* Eat '('. */
30547 /* If there is no category name, and this is an @interface, we
30548 have a class extension. */
30549 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30551 *categ = NULL_TREE;
30552 *is_class_extension = true;
30555 *categ = cp_parser_identifier (parser);
30557 parens.require_close (parser);
30561 /* Parse an Objective-C class interface. */
30564 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30566 tree name, super, categ, protos;
30567 bool is_class_extension;
30569 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30570 name = cp_parser_identifier (parser);
30571 if (name == error_mark_node)
30573 /* It's hard to recover because even if valid @interface stuff
30574 is to follow, we can't compile it (or validate it) if we
30575 don't even know which class it refers to. Let's assume this
30576 was a stray '@interface' token in the stream and skip it.
30580 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30581 &is_class_extension);
30582 protos = cp_parser_objc_protocol_refs_opt (parser);
30584 /* We have either a class or a category on our hands. */
30585 if (categ || is_class_extension)
30586 objc_start_category_interface (name, categ, protos, attributes);
30589 objc_start_class_interface (name, super, protos, attributes);
30590 /* Handle instance variable declarations, if any. */
30591 cp_parser_objc_class_ivars (parser);
30592 objc_continue_interface ();
30595 cp_parser_objc_method_prototype_list (parser);
30598 /* Parse an Objective-C class implementation. */
30601 cp_parser_objc_class_implementation (cp_parser* parser)
30603 tree name, super, categ;
30604 bool is_class_extension;
30606 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30607 name = cp_parser_identifier (parser);
30608 if (name == error_mark_node)
30610 /* It's hard to recover because even if valid @implementation
30611 stuff is to follow, we can't compile it (or validate it) if
30612 we don't even know which class it refers to. Let's assume
30613 this was a stray '@implementation' token in the stream and
30618 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30619 &is_class_extension);
30621 /* We have either a class or a category on our hands. */
30623 objc_start_category_implementation (name, categ);
30626 objc_start_class_implementation (name, super);
30627 /* Handle instance variable declarations, if any. */
30628 cp_parser_objc_class_ivars (parser);
30629 objc_continue_implementation ();
30632 cp_parser_objc_method_definition_list (parser);
30635 /* Consume the @end token and finish off the implementation. */
30638 cp_parser_objc_end_implementation (cp_parser* parser)
30640 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30641 objc_finish_implementation ();
30644 /* Parse an Objective-C declaration. */
30647 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30649 /* Try to figure out what kind of declaration is present. */
30650 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30653 switch (kwd->keyword)
30658 error_at (kwd->location, "attributes may not be specified before"
30659 " the %<@%D%> Objective-C++ keyword",
30663 case RID_AT_IMPLEMENTATION:
30664 warning_at (kwd->location, OPT_Wattributes,
30665 "prefix attributes are ignored before %<@%D%>",
30672 switch (kwd->keyword)
30675 cp_parser_objc_alias_declaration (parser);
30678 cp_parser_objc_class_declaration (parser);
30680 case RID_AT_PROTOCOL:
30681 cp_parser_objc_protocol_declaration (parser, attributes);
30683 case RID_AT_INTERFACE:
30684 cp_parser_objc_class_interface (parser, attributes);
30686 case RID_AT_IMPLEMENTATION:
30687 cp_parser_objc_class_implementation (parser);
30690 cp_parser_objc_end_implementation (parser);
30693 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30695 cp_parser_skip_to_end_of_block_or_statement (parser);
30699 /* Parse an Objective-C try-catch-finally statement.
30701 objc-try-catch-finally-stmt:
30702 @try compound-statement objc-catch-clause-seq [opt]
30703 objc-finally-clause [opt]
30705 objc-catch-clause-seq:
30706 objc-catch-clause objc-catch-clause-seq [opt]
30709 @catch ( objc-exception-declaration ) compound-statement
30711 objc-finally-clause:
30712 @finally compound-statement
30714 objc-exception-declaration:
30715 parameter-declaration
30718 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30722 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30723 for C. Keep them in sync. */
30726 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30728 location_t location;
30731 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30732 location = cp_lexer_peek_token (parser->lexer)->location;
30733 objc_maybe_warn_exceptions (location);
30734 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30735 node, lest it get absorbed into the surrounding block. */
30736 stmt = push_stmt_list ();
30737 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30738 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30740 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30742 cp_parameter_declarator *parm;
30743 tree parameter_declaration = error_mark_node;
30744 bool seen_open_paren = false;
30745 matching_parens parens;
30747 cp_lexer_consume_token (parser->lexer);
30748 if (parens.require_open (parser))
30749 seen_open_paren = true;
30750 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30752 /* We have "@catch (...)" (where the '...' are literally
30753 what is in the code). Skip the '...'.
30754 parameter_declaration is set to NULL_TREE, and
30755 objc_being_catch_clauses() knows that that means
30757 cp_lexer_consume_token (parser->lexer);
30758 parameter_declaration = NULL_TREE;
30762 /* We have "@catch (NSException *exception)" or something
30763 like that. Parse the parameter declaration. */
30764 parm = cp_parser_parameter_declaration (parser, false, NULL);
30766 parameter_declaration = error_mark_node;
30768 parameter_declaration = grokdeclarator (parm->declarator,
30769 &parm->decl_specifiers,
30770 PARM, /*initialized=*/0,
30771 /*attrlist=*/NULL);
30773 if (seen_open_paren)
30774 parens.require_close (parser);
30777 /* If there was no open parenthesis, we are recovering from
30778 an error, and we are trying to figure out what mistake
30779 the user has made. */
30781 /* If there is an immediate closing parenthesis, the user
30782 probably forgot the opening one (ie, they typed "@catch
30783 NSException *e)". Parse the closing parenthesis and keep
30785 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30786 cp_lexer_consume_token (parser->lexer);
30788 /* If these is no immediate closing parenthesis, the user
30789 probably doesn't know that parenthesis are required at
30790 all (ie, they typed "@catch NSException *e"). So, just
30791 forget about the closing parenthesis and keep going. */
30793 objc_begin_catch_clause (parameter_declaration);
30794 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30795 objc_finish_catch_clause ();
30797 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30799 cp_lexer_consume_token (parser->lexer);
30800 location = cp_lexer_peek_token (parser->lexer)->location;
30801 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30802 node, lest it get absorbed into the surrounding block. */
30803 stmt = push_stmt_list ();
30804 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30805 objc_build_finally_clause (location, pop_stmt_list (stmt));
30808 return objc_finish_try_stmt ();
30811 /* Parse an Objective-C synchronized statement.
30813 objc-synchronized-stmt:
30814 @synchronized ( expression ) compound-statement
30816 Returns NULL_TREE. */
30819 cp_parser_objc_synchronized_statement (cp_parser *parser)
30821 location_t location;
30824 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30826 location = cp_lexer_peek_token (parser->lexer)->location;
30827 objc_maybe_warn_exceptions (location);
30828 matching_parens parens;
30829 parens.require_open (parser);
30830 lock = cp_parser_expression (parser);
30831 parens.require_close (parser);
30833 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30834 node, lest it get absorbed into the surrounding block. */
30835 stmt = push_stmt_list ();
30836 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30838 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30841 /* Parse an Objective-C throw statement.
30844 @throw assignment-expression [opt] ;
30846 Returns a constructed '@throw' statement. */
30849 cp_parser_objc_throw_statement (cp_parser *parser)
30851 tree expr = NULL_TREE;
30852 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30854 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30856 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30857 expr = cp_parser_expression (parser);
30859 cp_parser_consume_semicolon_at_end_of_statement (parser);
30861 return objc_build_throw_stmt (loc, expr);
30864 /* Parse an Objective-C statement. */
30867 cp_parser_objc_statement (cp_parser * parser)
30869 /* Try to figure out what kind of declaration is present. */
30870 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30872 switch (kwd->keyword)
30875 return cp_parser_objc_try_catch_finally_statement (parser);
30876 case RID_AT_SYNCHRONIZED:
30877 return cp_parser_objc_synchronized_statement (parser);
30879 return cp_parser_objc_throw_statement (parser);
30881 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30883 cp_parser_skip_to_end_of_block_or_statement (parser);
30886 return error_mark_node;
30889 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30890 look ahead to see if an objc keyword follows the attributes. This
30891 is to detect the use of prefix attributes on ObjC @interface and
30895 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30897 cp_lexer_save_tokens (parser->lexer);
30898 *attrib = cp_parser_attributes_opt (parser);
30899 gcc_assert (*attrib);
30900 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30902 cp_lexer_commit_tokens (parser->lexer);
30905 cp_lexer_rollback_tokens (parser->lexer);
30909 /* This routine is a minimal replacement for
30910 c_parser_struct_declaration () used when parsing the list of
30911 types/names or ObjC++ properties. For example, when parsing the
30914 @property (readonly) int a, b, c;
30916 this function is responsible for parsing "int a, int b, int c" and
30917 returning the declarations as CHAIN of DECLs.
30919 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30920 similar parsing. */
30922 cp_parser_objc_struct_declaration (cp_parser *parser)
30924 tree decls = NULL_TREE;
30925 cp_decl_specifier_seq declspecs;
30926 int decl_class_or_enum_p;
30927 tree prefix_attributes;
30929 cp_parser_decl_specifier_seq (parser,
30930 CP_PARSER_FLAGS_NONE,
30932 &decl_class_or_enum_p);
30934 if (declspecs.type == error_mark_node)
30935 return error_mark_node;
30937 /* auto, register, static, extern, mutable. */
30938 if (declspecs.storage_class != sc_none)
30940 cp_parser_error (parser, "invalid type for property");
30941 declspecs.storage_class = sc_none;
30944 /* thread_local. */
30945 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30947 cp_parser_error (parser, "invalid type for property");
30948 declspecs.locations[ds_thread] = 0;
30952 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30954 cp_parser_error (parser, "invalid type for property");
30955 declspecs.locations[ds_typedef] = 0;
30958 prefix_attributes = declspecs.attributes;
30959 declspecs.attributes = NULL_TREE;
30961 /* Keep going until we hit the `;' at the end of the declaration. */
30962 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30964 tree attributes, first_attribute, decl;
30965 cp_declarator *declarator;
30968 /* Parse the declarator. */
30969 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30970 NULL, NULL, false, false);
30972 /* Look for attributes that apply to the ivar. */
30973 attributes = cp_parser_attributes_opt (parser);
30974 /* Remember which attributes are prefix attributes and
30976 first_attribute = attributes;
30977 /* Combine the attributes. */
30978 attributes = attr_chainon (prefix_attributes, attributes);
30980 decl = grokfield (declarator, &declspecs,
30981 NULL_TREE, /*init_const_expr_p=*/false,
30982 NULL_TREE, attributes);
30984 if (decl == error_mark_node || decl == NULL_TREE)
30985 return error_mark_node;
30987 /* Reset PREFIX_ATTRIBUTES. */
30988 if (attributes != error_mark_node)
30990 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30991 attributes = TREE_CHAIN (attributes);
30993 TREE_CHAIN (attributes) = NULL_TREE;
30996 DECL_CHAIN (decl) = decls;
30999 token = cp_lexer_peek_token (parser->lexer);
31000 if (token->type == CPP_COMMA)
31002 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31011 /* Parse an Objective-C @property declaration. The syntax is:
31013 objc-property-declaration:
31014 '@property' objc-property-attributes[opt] struct-declaration ;
31016 objc-property-attributes:
31017 '(' objc-property-attribute-list ')'
31019 objc-property-attribute-list:
31020 objc-property-attribute
31021 objc-property-attribute-list, objc-property-attribute
31023 objc-property-attribute
31024 'getter' = identifier
31025 'setter' = identifier
31034 @property NSString *name;
31035 @property (readonly) id object;
31036 @property (retain, nonatomic, getter=getTheName) id name;
31037 @property int a, b, c;
31039 PS: This function is identical to
31040 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31042 cp_parser_objc_at_property_declaration (cp_parser *parser)
31044 /* The following variables hold the attributes of the properties as
31045 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31046 seen. When we see an attribute, we set them to 'true' (if they
31047 are boolean properties) or to the identifier (if they have an
31048 argument, ie, for getter and setter). Note that here we only
31049 parse the list of attributes, check the syntax and accumulate the
31050 attributes that we find. objc_add_property_declaration() will
31051 then process the information. */
31052 bool property_assign = false;
31053 bool property_copy = false;
31054 tree property_getter_ident = NULL_TREE;
31055 bool property_nonatomic = false;
31056 bool property_readonly = false;
31057 bool property_readwrite = false;
31058 bool property_retain = false;
31059 tree property_setter_ident = NULL_TREE;
31061 /* 'properties' is the list of properties that we read. Usually a
31062 single one, but maybe more (eg, in "@property int a, b, c;" there
31067 loc = cp_lexer_peek_token (parser->lexer)->location;
31069 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31071 /* Parse the optional attribute list... */
31072 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31075 matching_parens parens;
31076 parens.consume_open (parser);
31080 bool syntax_error = false;
31081 cp_token *token = cp_lexer_peek_token (parser->lexer);
31084 if (token->type != CPP_NAME)
31086 cp_parser_error (parser, "expected identifier");
31089 keyword = C_RID_CODE (token->u.value);
31090 cp_lexer_consume_token (parser->lexer);
31093 case RID_ASSIGN: property_assign = true; break;
31094 case RID_COPY: property_copy = true; break;
31095 case RID_NONATOMIC: property_nonatomic = true; break;
31096 case RID_READONLY: property_readonly = true; break;
31097 case RID_READWRITE: property_readwrite = true; break;
31098 case RID_RETAIN: property_retain = true; break;
31102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31104 if (keyword == RID_GETTER)
31105 cp_parser_error (parser,
31106 "missing %<=%> (after %<getter%> attribute)");
31108 cp_parser_error (parser,
31109 "missing %<=%> (after %<setter%> attribute)");
31110 syntax_error = true;
31113 cp_lexer_consume_token (parser->lexer); /* eat the = */
31114 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31116 cp_parser_error (parser, "expected identifier");
31117 syntax_error = true;
31120 if (keyword == RID_SETTER)
31122 if (property_setter_ident != NULL_TREE)
31124 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31125 cp_lexer_consume_token (parser->lexer);
31128 property_setter_ident = cp_parser_objc_selector (parser);
31129 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31130 cp_parser_error (parser, "setter name must terminate with %<:%>");
31132 cp_lexer_consume_token (parser->lexer);
31136 if (property_getter_ident != NULL_TREE)
31138 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31139 cp_lexer_consume_token (parser->lexer);
31142 property_getter_ident = cp_parser_objc_selector (parser);
31146 cp_parser_error (parser, "unknown property attribute");
31147 syntax_error = true;
31154 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31155 cp_lexer_consume_token (parser->lexer);
31160 /* FIXME: "@property (setter, assign);" will generate a spurious
31161 "error: expected ‘)’ before ‘,’ token". This is because
31162 cp_parser_require, unlike the C counterpart, will produce an
31163 error even if we are in error recovery. */
31164 if (!parens.require_close (parser))
31166 cp_parser_skip_to_closing_parenthesis (parser,
31167 /*recovering=*/true,
31168 /*or_comma=*/false,
31169 /*consume_paren=*/true);
31173 /* ... and the property declaration(s). */
31174 properties = cp_parser_objc_struct_declaration (parser);
31176 if (properties == error_mark_node)
31178 cp_parser_skip_to_end_of_statement (parser);
31179 /* If the next token is now a `;', consume it. */
31180 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31181 cp_lexer_consume_token (parser->lexer);
31185 if (properties == NULL_TREE)
31186 cp_parser_error (parser, "expected identifier");
31189 /* Comma-separated properties are chained together in
31190 reverse order; add them one by one. */
31191 properties = nreverse (properties);
31193 for (; properties; properties = TREE_CHAIN (properties))
31194 objc_add_property_declaration (loc, copy_node (properties),
31195 property_readonly, property_readwrite,
31196 property_assign, property_retain,
31197 property_copy, property_nonatomic,
31198 property_getter_ident, property_setter_ident);
31201 cp_parser_consume_semicolon_at_end_of_statement (parser);
31204 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31206 objc-synthesize-declaration:
31207 @synthesize objc-synthesize-identifier-list ;
31209 objc-synthesize-identifier-list:
31210 objc-synthesize-identifier
31211 objc-synthesize-identifier-list, objc-synthesize-identifier
31213 objc-synthesize-identifier
31215 identifier = identifier
31218 @synthesize MyProperty;
31219 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31221 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31222 for C. Keep them in sync.
31225 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31227 tree list = NULL_TREE;
31229 loc = cp_lexer_peek_token (parser->lexer)->location;
31231 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31234 tree property, ivar;
31235 property = cp_parser_identifier (parser);
31236 if (property == error_mark_node)
31238 cp_parser_consume_semicolon_at_end_of_statement (parser);
31241 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31243 cp_lexer_consume_token (parser->lexer);
31244 ivar = cp_parser_identifier (parser);
31245 if (ivar == error_mark_node)
31247 cp_parser_consume_semicolon_at_end_of_statement (parser);
31253 list = chainon (list, build_tree_list (ivar, property));
31254 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31255 cp_lexer_consume_token (parser->lexer);
31259 cp_parser_consume_semicolon_at_end_of_statement (parser);
31260 objc_add_synthesize_declaration (loc, list);
31263 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31265 objc-dynamic-declaration:
31266 @dynamic identifier-list ;
31269 @dynamic MyProperty;
31270 @dynamic MyProperty, AnotherProperty;
31272 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31273 for C. Keep them in sync.
31276 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31278 tree list = NULL_TREE;
31280 loc = cp_lexer_peek_token (parser->lexer)->location;
31282 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31286 property = cp_parser_identifier (parser);
31287 if (property == error_mark_node)
31289 cp_parser_consume_semicolon_at_end_of_statement (parser);
31292 list = chainon (list, build_tree_list (NULL, property));
31293 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31294 cp_lexer_consume_token (parser->lexer);
31298 cp_parser_consume_semicolon_at_end_of_statement (parser);
31299 objc_add_dynamic_declaration (loc, list);
31303 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31305 /* Returns name of the next clause.
31306 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31307 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31308 returned and the token is consumed. */
31310 static pragma_omp_clause
31311 cp_parser_omp_clause_name (cp_parser *parser)
31313 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31316 result = PRAGMA_OACC_CLAUSE_AUTO;
31317 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31318 result = PRAGMA_OMP_CLAUSE_IF;
31319 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31320 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31321 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31322 result = PRAGMA_OACC_CLAUSE_DELETE;
31323 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31324 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31325 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31326 result = PRAGMA_OMP_CLAUSE_FOR;
31327 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31329 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31330 const char *p = IDENTIFIER_POINTER (id);
31335 if (!strcmp ("aligned", p))
31336 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31337 else if (!strcmp ("async", p))
31338 result = PRAGMA_OACC_CLAUSE_ASYNC;
31341 if (!strcmp ("collapse", p))
31342 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31343 else if (!strcmp ("copy", p))
31344 result = PRAGMA_OACC_CLAUSE_COPY;
31345 else if (!strcmp ("copyin", p))
31346 result = PRAGMA_OMP_CLAUSE_COPYIN;
31347 else if (!strcmp ("copyout", p))
31348 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31349 else if (!strcmp ("copyprivate", p))
31350 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31351 else if (!strcmp ("create", p))
31352 result = PRAGMA_OACC_CLAUSE_CREATE;
31355 if (!strcmp ("defaultmap", p))
31356 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31357 else if (!strcmp ("depend", p))
31358 result = PRAGMA_OMP_CLAUSE_DEPEND;
31359 else if (!strcmp ("device", p))
31360 result = PRAGMA_OMP_CLAUSE_DEVICE;
31361 else if (!strcmp ("deviceptr", p))
31362 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31363 else if (!strcmp ("device_resident", p))
31364 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31365 else if (!strcmp ("dist_schedule", p))
31366 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31369 if (!strcmp ("final", p))
31370 result = PRAGMA_OMP_CLAUSE_FINAL;
31371 else if (!strcmp ("firstprivate", p))
31372 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31373 else if (!strcmp ("from", p))
31374 result = PRAGMA_OMP_CLAUSE_FROM;
31377 if (!strcmp ("gang", p))
31378 result = PRAGMA_OACC_CLAUSE_GANG;
31379 else if (!strcmp ("grainsize", p))
31380 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31383 if (!strcmp ("hint", p))
31384 result = PRAGMA_OMP_CLAUSE_HINT;
31385 else if (!strcmp ("host", p))
31386 result = PRAGMA_OACC_CLAUSE_HOST;
31389 if (!strcmp ("inbranch", p))
31390 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31391 else if (!strcmp ("independent", p))
31392 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31393 else if (!strcmp ("is_device_ptr", p))
31394 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31397 if (!strcmp ("lastprivate", p))
31398 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31399 else if (!strcmp ("linear", p))
31400 result = PRAGMA_OMP_CLAUSE_LINEAR;
31401 else if (!strcmp ("link", p))
31402 result = PRAGMA_OMP_CLAUSE_LINK;
31405 if (!strcmp ("map", p))
31406 result = PRAGMA_OMP_CLAUSE_MAP;
31407 else if (!strcmp ("mergeable", p))
31408 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31411 if (!strcmp ("nogroup", p))
31412 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31413 else if (!strcmp ("notinbranch", p))
31414 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31415 else if (!strcmp ("nowait", p))
31416 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31417 else if (!strcmp ("num_gangs", p))
31418 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31419 else if (!strcmp ("num_tasks", p))
31420 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31421 else if (!strcmp ("num_teams", p))
31422 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31423 else if (!strcmp ("num_threads", p))
31424 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31425 else if (!strcmp ("num_workers", p))
31426 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31429 if (!strcmp ("ordered", p))
31430 result = PRAGMA_OMP_CLAUSE_ORDERED;
31433 if (!strcmp ("parallel", p))
31434 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31435 else if (!strcmp ("present", p))
31436 result = PRAGMA_OACC_CLAUSE_PRESENT;
31437 else if (!strcmp ("present_or_copy", p)
31438 || !strcmp ("pcopy", p))
31439 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31440 else if (!strcmp ("present_or_copyin", p)
31441 || !strcmp ("pcopyin", p))
31442 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31443 else if (!strcmp ("present_or_copyout", p)
31444 || !strcmp ("pcopyout", p))
31445 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31446 else if (!strcmp ("present_or_create", p)
31447 || !strcmp ("pcreate", p))
31448 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31449 else if (!strcmp ("priority", p))
31450 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31451 else if (!strcmp ("proc_bind", p))
31452 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31455 if (!strcmp ("reduction", p))
31456 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31459 if (!strcmp ("safelen", p))
31460 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31461 else if (!strcmp ("schedule", p))
31462 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31463 else if (!strcmp ("sections", p))
31464 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31465 else if (!strcmp ("self", p))
31466 result = PRAGMA_OACC_CLAUSE_SELF;
31467 else if (!strcmp ("seq", p))
31468 result = PRAGMA_OACC_CLAUSE_SEQ;
31469 else if (!strcmp ("shared", p))
31470 result = PRAGMA_OMP_CLAUSE_SHARED;
31471 else if (!strcmp ("simd", p))
31472 result = PRAGMA_OMP_CLAUSE_SIMD;
31473 else if (!strcmp ("simdlen", p))
31474 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31477 if (!strcmp ("taskgroup", p))
31478 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31479 else if (!strcmp ("thread_limit", p))
31480 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31481 else if (!strcmp ("threads", p))
31482 result = PRAGMA_OMP_CLAUSE_THREADS;
31483 else if (!strcmp ("tile", p))
31484 result = PRAGMA_OACC_CLAUSE_TILE;
31485 else if (!strcmp ("to", p))
31486 result = PRAGMA_OMP_CLAUSE_TO;
31489 if (!strcmp ("uniform", p))
31490 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31491 else if (!strcmp ("untied", p))
31492 result = PRAGMA_OMP_CLAUSE_UNTIED;
31493 else if (!strcmp ("use_device", p))
31494 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31495 else if (!strcmp ("use_device_ptr", p))
31496 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31499 if (!strcmp ("vector", p))
31500 result = PRAGMA_OACC_CLAUSE_VECTOR;
31501 else if (!strcmp ("vector_length", p))
31502 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31505 if (!strcmp ("wait", p))
31506 result = PRAGMA_OACC_CLAUSE_WAIT;
31507 else if (!strcmp ("worker", p))
31508 result = PRAGMA_OACC_CLAUSE_WORKER;
31513 if (result != PRAGMA_OMP_CLAUSE_NONE)
31514 cp_lexer_consume_token (parser->lexer);
31519 /* Validate that a clause of the given type does not already exist. */
31522 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31523 const char *name, location_t location)
31527 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31528 if (OMP_CLAUSE_CODE (c) == code)
31530 error_at (location, "too many %qs clauses", name);
31538 variable-list , identifier
31540 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31541 colon). An opening parenthesis will have been consumed by the caller.
31543 If KIND is nonzero, create the appropriate node and install the decl
31544 in OMP_CLAUSE_DECL and add the node to the head of the list.
31546 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31547 return the list created.
31549 COLON can be NULL if only closing parenthesis should end the list,
31550 or pointer to bool which will receive false if the list is terminated
31551 by closing parenthesis or true if the list is terminated by colon. */
31554 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31555 tree list, bool *colon)
31558 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31561 parser->colon_corrects_to_scope_p = false;
31568 token = cp_lexer_peek_token (parser->lexer);
31570 && current_class_ptr
31571 && cp_parser_is_keyword (token, RID_THIS))
31573 decl = finish_this_expr ();
31574 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31575 || CONVERT_EXPR_P (decl))
31576 decl = TREE_OPERAND (decl, 0);
31577 cp_lexer_consume_token (parser->lexer);
31581 name = cp_parser_id_expression (parser, /*template_p=*/false,
31582 /*check_dependency_p=*/true,
31583 /*template_p=*/NULL,
31584 /*declarator_p=*/false,
31585 /*optional_p=*/false);
31586 if (name == error_mark_node)
31589 if (identifier_p (name))
31590 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31593 if (decl == error_mark_node)
31594 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31597 if (decl == error_mark_node)
31599 else if (kind != 0)
31603 case OMP_CLAUSE__CACHE_:
31604 /* The OpenACC cache directive explicitly only allows "array
31605 elements or subarrays". */
31606 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31608 error_at (token->location, "expected %<[%>");
31609 decl = error_mark_node;
31613 case OMP_CLAUSE_MAP:
31614 case OMP_CLAUSE_FROM:
31615 case OMP_CLAUSE_TO:
31616 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31619 = cp_lexer_peek_token (parser->lexer)->location;
31620 cp_id_kind idk = CP_ID_KIND_NONE;
31621 cp_lexer_consume_token (parser->lexer);
31622 decl = convert_from_reference (decl);
31624 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31629 case OMP_CLAUSE_DEPEND:
31630 case OMP_CLAUSE_REDUCTION:
31631 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31633 tree low_bound = NULL_TREE, length = NULL_TREE;
31635 parser->colon_corrects_to_scope_p = false;
31636 cp_lexer_consume_token (parser->lexer);
31637 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31638 low_bound = cp_parser_expression (parser);
31640 parser->colon_corrects_to_scope_p
31641 = saved_colon_corrects_to_scope_p;
31642 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31643 length = integer_one_node;
31646 /* Look for `:'. */
31647 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31649 if (!cp_lexer_next_token_is (parser->lexer,
31651 length = cp_parser_expression (parser);
31653 /* Look for the closing `]'. */
31654 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31658 decl = tree_cons (low_bound, length, decl);
31665 tree u = build_omp_clause (token->location, kind);
31666 OMP_CLAUSE_DECL (u) = decl;
31667 OMP_CLAUSE_CHAIN (u) = list;
31671 list = tree_cons (decl, NULL_TREE, list);
31674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31676 cp_lexer_consume_token (parser->lexer);
31680 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31682 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31685 cp_parser_require (parser, CPP_COLON, RT_COLON);
31689 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31693 /* Try to resync to an unnested comma. Copied from
31694 cp_parser_parenthesized_expression_list. */
31697 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31698 ending = cp_parser_skip_to_closing_parenthesis (parser,
31699 /*recovering=*/true,
31701 /*consume_paren=*/true);
31709 /* Similarly, but expect leading and trailing parenthesis. This is a very
31710 common case for omp clauses. */
31713 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31715 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31716 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31721 copy ( variable-list )
31722 copyin ( variable-list )
31723 copyout ( variable-list )
31724 create ( variable-list )
31725 delete ( variable-list )
31726 present ( variable-list )
31727 present_or_copy ( variable-list )
31728 pcopy ( variable-list )
31729 present_or_copyin ( variable-list )
31730 pcopyin ( variable-list )
31731 present_or_copyout ( variable-list )
31732 pcopyout ( variable-list )
31733 present_or_create ( variable-list )
31734 pcreate ( variable-list ) */
31737 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31740 enum gomp_map_kind kind;
31743 case PRAGMA_OACC_CLAUSE_COPY:
31744 kind = GOMP_MAP_FORCE_TOFROM;
31746 case PRAGMA_OACC_CLAUSE_COPYIN:
31747 kind = GOMP_MAP_FORCE_TO;
31749 case PRAGMA_OACC_CLAUSE_COPYOUT:
31750 kind = GOMP_MAP_FORCE_FROM;
31752 case PRAGMA_OACC_CLAUSE_CREATE:
31753 kind = GOMP_MAP_FORCE_ALLOC;
31755 case PRAGMA_OACC_CLAUSE_DELETE:
31756 kind = GOMP_MAP_DELETE;
31758 case PRAGMA_OACC_CLAUSE_DEVICE:
31759 kind = GOMP_MAP_FORCE_TO;
31761 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31762 kind = GOMP_MAP_DEVICE_RESIDENT;
31764 case PRAGMA_OACC_CLAUSE_HOST:
31765 case PRAGMA_OACC_CLAUSE_SELF:
31766 kind = GOMP_MAP_FORCE_FROM;
31768 case PRAGMA_OACC_CLAUSE_LINK:
31769 kind = GOMP_MAP_LINK;
31771 case PRAGMA_OACC_CLAUSE_PRESENT:
31772 kind = GOMP_MAP_FORCE_PRESENT;
31774 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31775 kind = GOMP_MAP_TOFROM;
31777 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31778 kind = GOMP_MAP_TO;
31780 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31781 kind = GOMP_MAP_FROM;
31783 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31784 kind = GOMP_MAP_ALLOC;
31787 gcc_unreachable ();
31790 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31792 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31793 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31799 deviceptr ( variable-list ) */
31802 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31804 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31807 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31808 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31809 variable-list must only allow for pointer variables. */
31810 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31811 for (t = vars; t; t = TREE_CHAIN (t))
31813 tree v = TREE_PURPOSE (t);
31814 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31815 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31816 OMP_CLAUSE_DECL (u) = v;
31817 OMP_CLAUSE_CHAIN (u) = list;
31831 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31832 enum omp_clause_code code,
31833 tree list, location_t location)
31835 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31836 tree c = build_omp_clause (location, code);
31837 OMP_CLAUSE_CHAIN (c) = list;
31842 num_gangs ( expression )
31843 num_workers ( expression )
31844 vector_length ( expression ) */
31847 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31848 const char *str, tree list)
31850 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31852 matching_parens parens;
31853 if (!parens.require_open (parser))
31856 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31858 if (t == error_mark_node
31859 || !parens.require_close (parser))
31861 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31862 /*or_comma=*/false,
31863 /*consume_paren=*/true);
31867 check_no_duplicate_clause (list, code, str, loc);
31869 tree c = build_omp_clause (loc, code);
31870 OMP_CLAUSE_OPERAND (c, 0) = t;
31871 OMP_CLAUSE_CHAIN (c) = list;
31877 gang [( gang-arg-list )]
31878 worker [( [num:] int-expr )]
31879 vector [( [length:] int-expr )]
31881 where gang-arg is one of:
31886 and size-expr may be:
31893 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31894 const char *str, tree list)
31896 const char *id = "num";
31897 cp_lexer *lexer = parser->lexer;
31898 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31899 location_t loc = cp_lexer_peek_token (lexer)->location;
31901 if (kind == OMP_CLAUSE_VECTOR)
31904 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31906 matching_parens parens;
31907 parens.consume_open (parser);
31911 cp_token *next = cp_lexer_peek_token (lexer);
31914 /* Gang static argument. */
31915 if (kind == OMP_CLAUSE_GANG
31916 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31918 cp_lexer_consume_token (lexer);
31920 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31921 goto cleanup_error;
31924 if (ops[idx] != NULL)
31926 cp_parser_error (parser, "too many %<static%> arguments");
31927 goto cleanup_error;
31930 /* Check for the '*' argument. */
31931 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31932 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31933 || cp_lexer_nth_token_is (parser->lexer, 2,
31936 cp_lexer_consume_token (lexer);
31937 ops[idx] = integer_minus_one_node;
31939 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31941 cp_lexer_consume_token (lexer);
31947 /* Worker num: argument and vector length: arguments. */
31948 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31949 && id_equal (next->u.value, id)
31950 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31952 cp_lexer_consume_token (lexer); /* id */
31953 cp_lexer_consume_token (lexer); /* ':' */
31956 /* Now collect the actual argument. */
31957 if (ops[idx] != NULL_TREE)
31959 cp_parser_error (parser, "unexpected argument");
31960 goto cleanup_error;
31963 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31965 if (expr == error_mark_node)
31966 goto cleanup_error;
31968 mark_exp_read (expr);
31971 if (kind == OMP_CLAUSE_GANG
31972 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31974 cp_lexer_consume_token (lexer);
31981 if (!parens.require_close (parser))
31982 goto cleanup_error;
31985 check_no_duplicate_clause (list, kind, str, loc);
31987 c = build_omp_clause (loc, kind);
31990 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31992 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31993 OMP_CLAUSE_CHAIN (c) = list;
31998 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32003 tile ( size-expr-list ) */
32006 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32008 tree c, expr = error_mark_node;
32009 tree tile = NULL_TREE;
32011 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32012 so, but the spec authors never considered such a case and have
32013 differing opinions on what it might mean, including 'not
32015 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32016 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32019 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32024 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32027 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32028 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32029 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32031 cp_lexer_consume_token (parser->lexer);
32032 expr = integer_zero_node;
32035 expr = cp_parser_constant_expression (parser);
32037 tile = tree_cons (NULL_TREE, expr, tile);
32039 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32041 /* Consume the trailing ')'. */
32042 cp_lexer_consume_token (parser->lexer);
32044 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32045 tile = nreverse (tile);
32046 OMP_CLAUSE_TILE_LIST (c) = tile;
32047 OMP_CLAUSE_CHAIN (c) = list;
32052 Parse wait clause or directive parameters. */
32055 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32057 vec<tree, va_gc> *args;
32060 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32062 /*allow_expansion_p=*/true,
32063 /*non_constant_p=*/NULL);
32065 if (args == NULL || args->length () == 0)
32067 cp_parser_error (parser, "expected integer expression before ')'");
32069 release_tree_vector (args);
32073 args_tree = build_tree_list_vec (args);
32075 release_tree_vector (args);
32077 for (t = args_tree; t; t = TREE_CHAIN (t))
32079 tree targ = TREE_VALUE (t);
32081 if (targ != error_mark_node)
32083 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32084 error ("%<wait%> expression must be integral");
32087 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32089 targ = mark_rvalue_use (targ);
32090 OMP_CLAUSE_DECL (c) = targ;
32091 OMP_CLAUSE_CHAIN (c) = list;
32101 wait ( int-expr-list ) */
32104 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32106 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32108 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32111 list = cp_parser_oacc_wait_list (parser, location, list);
32117 collapse ( constant-expression ) */
32120 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32126 loc = cp_lexer_peek_token (parser->lexer)->location;
32127 matching_parens parens;
32128 if (!parens.require_open (parser))
32131 num = cp_parser_constant_expression (parser);
32133 if (!parens.require_close (parser))
32134 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32135 /*or_comma=*/false,
32136 /*consume_paren=*/true);
32138 if (num == error_mark_node)
32140 num = fold_non_dependent_expr (num);
32141 if (!tree_fits_shwi_p (num)
32142 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32143 || (n = tree_to_shwi (num)) <= 0
32146 error_at (loc, "collapse argument needs positive constant integer expression");
32150 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32151 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32152 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32153 OMP_CLAUSE_CHAIN (c) = list;
32154 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32160 default ( none | shared )
32163 default ( none | present ) */
32166 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32167 location_t location, bool is_oacc)
32169 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32172 matching_parens parens;
32173 if (!parens.require_open (parser))
32175 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32177 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32178 const char *p = IDENTIFIER_POINTER (id);
32183 if (strcmp ("none", p) != 0)
32185 kind = OMP_CLAUSE_DEFAULT_NONE;
32189 if (strcmp ("present", p) != 0 || !is_oacc)
32191 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32195 if (strcmp ("shared", p) != 0 || is_oacc)
32197 kind = OMP_CLAUSE_DEFAULT_SHARED;
32204 cp_lexer_consume_token (parser->lexer);
32210 cp_parser_error (parser, "expected %<none%> or %<present%>");
32212 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32215 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32216 || !parens.require_close (parser))
32217 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32218 /*or_comma=*/false,
32219 /*consume_paren=*/true);
32221 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32224 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32225 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32226 OMP_CLAUSE_CHAIN (c) = list;
32227 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32233 final ( expression ) */
32236 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32240 matching_parens parens;
32241 if (!parens.require_open (parser))
32244 t = cp_parser_condition (parser);
32246 if (t == error_mark_node
32247 || !parens.require_close (parser))
32248 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32249 /*or_comma=*/false,
32250 /*consume_paren=*/true);
32252 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32254 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32255 OMP_CLAUSE_FINAL_EXPR (c) = t;
32256 OMP_CLAUSE_CHAIN (c) = list;
32265 if ( directive-name-modifier : expression )
32267 directive-name-modifier:
32268 parallel | task | taskloop | target data | target | target update
32269 | target enter data | target exit data */
32272 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32276 enum tree_code if_modifier = ERROR_MARK;
32278 matching_parens parens;
32279 if (!parens.require_open (parser))
32282 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32284 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32285 const char *p = IDENTIFIER_POINTER (id);
32288 if (strcmp ("parallel", p) == 0)
32289 if_modifier = OMP_PARALLEL;
32290 else if (strcmp ("task", p) == 0)
32291 if_modifier = OMP_TASK;
32292 else if (strcmp ("taskloop", p) == 0)
32293 if_modifier = OMP_TASKLOOP;
32294 else if (strcmp ("target", p) == 0)
32296 if_modifier = OMP_TARGET;
32297 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32299 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32300 p = IDENTIFIER_POINTER (id);
32301 if (strcmp ("data", p) == 0)
32302 if_modifier = OMP_TARGET_DATA;
32303 else if (strcmp ("update", p) == 0)
32304 if_modifier = OMP_TARGET_UPDATE;
32305 else if (strcmp ("enter", p) == 0)
32306 if_modifier = OMP_TARGET_ENTER_DATA;
32307 else if (strcmp ("exit", p) == 0)
32308 if_modifier = OMP_TARGET_EXIT_DATA;
32309 if (if_modifier != OMP_TARGET)
32314 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32315 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32317 if_modifier = ERROR_MARK;
32319 if (if_modifier == OMP_TARGET_ENTER_DATA
32320 || if_modifier == OMP_TARGET_EXIT_DATA)
32322 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32324 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32325 p = IDENTIFIER_POINTER (id);
32326 if (strcmp ("data", p) == 0)
32332 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32333 error_at (loc, "expected %<data%>");
32334 if_modifier = ERROR_MARK;
32339 if (if_modifier != ERROR_MARK)
32341 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32344 cp_lexer_consume_token (parser->lexer);
32351 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32352 error_at (loc, "expected %<:%>");
32354 if_modifier = ERROR_MARK;
32359 t = cp_parser_condition (parser);
32361 if (t == error_mark_node
32362 || !parens.require_close (parser))
32363 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32364 /*or_comma=*/false,
32365 /*consume_paren=*/true);
32367 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32368 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32370 if (if_modifier != ERROR_MARK
32371 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32373 const char *p = NULL;
32374 switch (if_modifier)
32376 case OMP_PARALLEL: p = "parallel"; break;
32377 case OMP_TASK: p = "task"; break;
32378 case OMP_TASKLOOP: p = "taskloop"; break;
32379 case OMP_TARGET_DATA: p = "target data"; break;
32380 case OMP_TARGET: p = "target"; break;
32381 case OMP_TARGET_UPDATE: p = "target update"; break;
32382 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32383 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32384 default: gcc_unreachable ();
32386 error_at (location, "too many %<if%> clauses with %qs modifier",
32390 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32393 error_at (location, "too many %<if%> clauses");
32395 error_at (location, "too many %<if%> clauses without modifier");
32398 else if (if_modifier == ERROR_MARK
32399 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32401 error_at (location, "if any %<if%> clause has modifier, then all "
32402 "%<if%> clauses have to use modifier");
32407 c = build_omp_clause (location, OMP_CLAUSE_IF);
32408 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32409 OMP_CLAUSE_IF_EXPR (c) = t;
32410 OMP_CLAUSE_CHAIN (c) = list;
32419 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32420 tree list, location_t location)
32424 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32427 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32428 OMP_CLAUSE_CHAIN (c) = list;
32436 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32437 tree list, location_t location)
32441 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32443 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32444 OMP_CLAUSE_CHAIN (c) = list;
32449 num_threads ( expression ) */
32452 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32453 location_t location)
32457 matching_parens parens;
32458 if (!parens.require_open (parser))
32461 t = cp_parser_expression (parser);
32463 if (t == error_mark_node
32464 || !parens.require_close (parser))
32465 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32466 /*or_comma=*/false,
32467 /*consume_paren=*/true);
32469 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32470 "num_threads", location);
32472 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32473 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32474 OMP_CLAUSE_CHAIN (c) = list;
32480 num_tasks ( expression ) */
32483 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32484 location_t location)
32488 matching_parens parens;
32489 if (!parens.require_open (parser))
32492 t = cp_parser_expression (parser);
32494 if (t == error_mark_node
32495 || !parens.require_close (parser))
32496 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32497 /*or_comma=*/false,
32498 /*consume_paren=*/true);
32500 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32501 "num_tasks", location);
32503 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32504 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32505 OMP_CLAUSE_CHAIN (c) = list;
32511 grainsize ( expression ) */
32514 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32515 location_t location)
32519 matching_parens parens;
32520 if (!parens.require_open (parser))
32523 t = cp_parser_expression (parser);
32525 if (t == error_mark_node
32526 || !parens.require_close (parser))
32527 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32528 /*or_comma=*/false,
32529 /*consume_paren=*/true);
32531 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32532 "grainsize", location);
32534 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32535 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32536 OMP_CLAUSE_CHAIN (c) = list;
32542 priority ( expression ) */
32545 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32546 location_t location)
32550 matching_parens parens;
32551 if (!parens.require_open (parser))
32554 t = cp_parser_expression (parser);
32556 if (t == error_mark_node
32557 || !parens.require_close (parser))
32558 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32559 /*or_comma=*/false,
32560 /*consume_paren=*/true);
32562 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32563 "priority", location);
32565 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32566 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32567 OMP_CLAUSE_CHAIN (c) = list;
32573 hint ( expression ) */
32576 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32577 location_t location)
32581 matching_parens parens;
32582 if (!parens.require_open (parser))
32585 t = cp_parser_expression (parser);
32587 if (t == error_mark_node
32588 || !parens.require_close (parser))
32589 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32590 /*or_comma=*/false,
32591 /*consume_paren=*/true);
32593 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32595 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32596 OMP_CLAUSE_HINT_EXPR (c) = t;
32597 OMP_CLAUSE_CHAIN (c) = list;
32603 defaultmap ( tofrom : scalar ) */
32606 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32607 location_t location)
32612 matching_parens parens;
32613 if (!parens.require_open (parser))
32616 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32618 cp_parser_error (parser, "expected %<tofrom%>");
32621 id = cp_lexer_peek_token (parser->lexer)->u.value;
32622 p = IDENTIFIER_POINTER (id);
32623 if (strcmp (p, "tofrom") != 0)
32625 cp_parser_error (parser, "expected %<tofrom%>");
32628 cp_lexer_consume_token (parser->lexer);
32629 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32632 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32634 cp_parser_error (parser, "expected %<scalar%>");
32637 id = cp_lexer_peek_token (parser->lexer)->u.value;
32638 p = IDENTIFIER_POINTER (id);
32639 if (strcmp (p, "scalar") != 0)
32641 cp_parser_error (parser, "expected %<scalar%>");
32644 cp_lexer_consume_token (parser->lexer);
32645 if (!parens.require_close (parser))
32648 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32651 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32652 OMP_CLAUSE_CHAIN (c) = list;
32656 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32657 /*or_comma=*/false,
32658 /*consume_paren=*/true);
32666 ordered ( constant-expression ) */
32669 cp_parser_omp_clause_ordered (cp_parser *parser,
32670 tree list, location_t location)
32672 tree c, num = NULL_TREE;
32675 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32676 "ordered", location);
32678 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32680 matching_parens parens;
32681 parens.consume_open (parser);
32683 num = cp_parser_constant_expression (parser);
32685 if (!parens.require_close (parser))
32686 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32687 /*or_comma=*/false,
32688 /*consume_paren=*/true);
32690 if (num == error_mark_node)
32692 num = fold_non_dependent_expr (num);
32693 if (!tree_fits_shwi_p (num)
32694 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32695 || (n = tree_to_shwi (num)) <= 0
32698 error_at (location,
32699 "ordered argument needs positive constant integer "
32705 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32706 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32707 OMP_CLAUSE_CHAIN (c) = list;
32712 reduction ( reduction-operator : variable-list )
32714 reduction-operator:
32715 One of: + * - & ^ | && ||
32719 reduction-operator:
32720 One of: + * - & ^ | && || min max
32724 reduction-operator:
32725 One of: + * - & ^ | && ||
32729 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32731 enum tree_code code = ERROR_MARK;
32732 tree nlist, c, id = NULL_TREE;
32734 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32737 switch (cp_lexer_peek_token (parser->lexer)->type)
32739 case CPP_PLUS: code = PLUS_EXPR; break;
32740 case CPP_MULT: code = MULT_EXPR; break;
32741 case CPP_MINUS: code = MINUS_EXPR; break;
32742 case CPP_AND: code = BIT_AND_EXPR; break;
32743 case CPP_XOR: code = BIT_XOR_EXPR; break;
32744 case CPP_OR: code = BIT_IOR_EXPR; break;
32745 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32746 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32750 if (code != ERROR_MARK)
32751 cp_lexer_consume_token (parser->lexer);
32754 bool saved_colon_corrects_to_scope_p;
32755 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32756 parser->colon_corrects_to_scope_p = false;
32757 id = cp_parser_id_expression (parser, /*template_p=*/false,
32758 /*check_dependency_p=*/true,
32759 /*template_p=*/NULL,
32760 /*declarator_p=*/false,
32761 /*optional_p=*/false);
32762 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32763 if (identifier_p (id))
32765 const char *p = IDENTIFIER_POINTER (id);
32767 if (strcmp (p, "min") == 0)
32769 else if (strcmp (p, "max") == 0)
32771 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32773 else if (id == ovl_op_identifier (false, MULT_EXPR))
32775 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32777 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32778 code = BIT_AND_EXPR;
32779 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32780 code = BIT_IOR_EXPR;
32781 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32782 code = BIT_XOR_EXPR;
32783 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32784 code = TRUTH_ANDIF_EXPR;
32785 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32786 code = TRUTH_ORIF_EXPR;
32787 id = omp_reduction_id (code, id, NULL_TREE);
32788 tree scope = parser->scope;
32790 id = build_qualified_name (NULL_TREE, scope, id, false);
32791 parser->scope = NULL_TREE;
32792 parser->qualifying_scope = NULL_TREE;
32793 parser->object_scope = NULL_TREE;
32797 error ("invalid reduction-identifier");
32799 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32800 /*or_comma=*/false,
32801 /*consume_paren=*/true);
32806 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32809 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32811 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32813 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32814 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32821 schedule ( schedule-kind )
32822 schedule ( schedule-kind , expression )
32825 static | dynamic | guided | runtime | auto
32828 schedule ( schedule-modifier : schedule-kind )
32829 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32837 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32840 int modifiers = 0, nmodifiers = 0;
32842 matching_parens parens;
32843 if (!parens.require_open (parser))
32846 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32848 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32850 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32851 const char *p = IDENTIFIER_POINTER (id);
32852 if (strcmp ("simd", p) == 0)
32853 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32854 else if (strcmp ("monotonic", p) == 0)
32855 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32856 else if (strcmp ("nonmonotonic", p) == 0)
32857 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32860 cp_lexer_consume_token (parser->lexer);
32861 if (nmodifiers++ == 0
32862 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32863 cp_lexer_consume_token (parser->lexer);
32866 cp_parser_require (parser, CPP_COLON, RT_COLON);
32871 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32873 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32874 const char *p = IDENTIFIER_POINTER (id);
32879 if (strcmp ("dynamic", p) != 0)
32881 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32885 if (strcmp ("guided", p) != 0)
32887 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32891 if (strcmp ("runtime", p) != 0)
32893 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32900 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32901 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32902 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32903 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32906 cp_lexer_consume_token (parser->lexer);
32908 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32909 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32910 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32911 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32913 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32918 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32921 cp_lexer_consume_token (parser->lexer);
32923 token = cp_lexer_peek_token (parser->lexer);
32924 t = cp_parser_assignment_expression (parser);
32926 if (t == error_mark_node)
32928 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32929 error_at (token->location, "schedule %<runtime%> does not take "
32930 "a %<chunk_size%> parameter");
32931 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32932 error_at (token->location, "schedule %<auto%> does not take "
32933 "a %<chunk_size%> parameter");
32935 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32937 if (!parens.require_close (parser))
32940 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32943 OMP_CLAUSE_SCHEDULE_KIND (c)
32944 = (enum omp_clause_schedule_kind)
32945 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32947 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32948 OMP_CLAUSE_CHAIN (c) = list;
32952 cp_parser_error (parser, "invalid schedule kind");
32954 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32955 /*or_comma=*/false,
32956 /*consume_paren=*/true);
32964 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32965 tree list, location_t location)
32969 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32971 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32972 OMP_CLAUSE_CHAIN (c) = list;
32981 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32982 tree list, location_t location)
32984 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32985 tree c = build_omp_clause (location, code);
32986 OMP_CLAUSE_CHAIN (c) = list;
32997 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32998 enum omp_clause_code code,
32999 tree list, location_t location)
33001 tree c = build_omp_clause (location, code);
33002 OMP_CLAUSE_CHAIN (c) = list;
33010 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33011 tree list, location_t location)
33013 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33014 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33015 OMP_CLAUSE_CHAIN (c) = list;
33024 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33025 enum omp_clause_code code,
33026 tree list, location_t location)
33028 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33029 tree c = build_omp_clause (location, code);
33030 OMP_CLAUSE_CHAIN (c) = list;
33035 num_teams ( expression ) */
33038 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33039 location_t location)
33043 matching_parens parens;
33044 if (!parens.require_open (parser))
33047 t = cp_parser_expression (parser);
33049 if (t == error_mark_node
33050 || !parens.require_close (parser))
33051 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33052 /*or_comma=*/false,
33053 /*consume_paren=*/true);
33055 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33056 "num_teams", location);
33058 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33059 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33060 OMP_CLAUSE_CHAIN (c) = list;
33066 thread_limit ( expression ) */
33069 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33070 location_t location)
33074 matching_parens parens;
33075 if (!parens.require_open (parser))
33078 t = cp_parser_expression (parser);
33080 if (t == error_mark_node
33081 || !parens.require_close (parser))
33082 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33083 /*or_comma=*/false,
33084 /*consume_paren=*/true);
33086 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33087 "thread_limit", location);
33089 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33090 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33091 OMP_CLAUSE_CHAIN (c) = list;
33097 aligned ( variable-list )
33098 aligned ( variable-list : constant-expression ) */
33101 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33103 tree nlist, c, alignment = NULL_TREE;
33106 matching_parens parens;
33107 if (!parens.require_open (parser))
33110 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33115 alignment = cp_parser_constant_expression (parser);
33117 if (!parens.require_close (parser))
33118 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33119 /*or_comma=*/false,
33120 /*consume_paren=*/true);
33122 if (alignment == error_mark_node)
33123 alignment = NULL_TREE;
33126 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33127 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33133 linear ( variable-list )
33134 linear ( variable-list : expression )
33137 linear ( modifier ( variable-list ) )
33138 linear ( modifier ( variable-list ) : expression ) */
33141 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33144 tree nlist, c, step = integer_one_node;
33146 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33148 matching_parens parens;
33149 if (!parens.require_open (parser))
33152 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33154 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33155 const char *p = IDENTIFIER_POINTER (id);
33157 if (strcmp ("ref", p) == 0)
33158 kind = OMP_CLAUSE_LINEAR_REF;
33159 else if (strcmp ("val", p) == 0)
33160 kind = OMP_CLAUSE_LINEAR_VAL;
33161 else if (strcmp ("uval", p) == 0)
33162 kind = OMP_CLAUSE_LINEAR_UVAL;
33163 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33164 cp_lexer_consume_token (parser->lexer);
33166 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33169 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33170 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33174 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33175 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33177 cp_parser_require (parser, CPP_COLON, RT_COLON);
33178 else if (!parens.require_close (parser))
33179 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33180 /*or_comma=*/false,
33181 /*consume_paren=*/true);
33188 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33189 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33191 cp_token *token = cp_lexer_peek_token (parser->lexer);
33192 cp_parser_parse_tentatively (parser);
33193 step = cp_parser_id_expression (parser, /*template_p=*/false,
33194 /*check_dependency_p=*/true,
33195 /*template_p=*/NULL,
33196 /*declarator_p=*/false,
33197 /*optional_p=*/false);
33198 if (step != error_mark_node)
33199 step = cp_parser_lookup_name_simple (parser, step, token->location);
33200 if (step == error_mark_node)
33203 cp_parser_abort_tentative_parse (parser);
33205 else if (!cp_parser_parse_definitely (parser))
33209 step = cp_parser_expression (parser);
33211 if (!parens.require_close (parser))
33212 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33213 /*or_comma=*/false,
33214 /*consume_paren=*/true);
33216 if (step == error_mark_node)
33220 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33222 OMP_CLAUSE_LINEAR_STEP (c) = step;
33223 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33230 safelen ( constant-expression ) */
33233 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33234 location_t location)
33238 matching_parens parens;
33239 if (!parens.require_open (parser))
33242 t = cp_parser_constant_expression (parser);
33244 if (t == error_mark_node
33245 || !parens.require_close (parser))
33246 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33247 /*or_comma=*/false,
33248 /*consume_paren=*/true);
33250 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33252 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33253 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33254 OMP_CLAUSE_CHAIN (c) = list;
33260 simdlen ( constant-expression ) */
33263 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33264 location_t location)
33268 matching_parens parens;
33269 if (!parens.require_open (parser))
33272 t = cp_parser_constant_expression (parser);
33274 if (t == error_mark_node
33275 || !parens.require_close (parser))
33276 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33277 /*or_comma=*/false,
33278 /*consume_paren=*/true);
33280 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33282 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33283 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33284 OMP_CLAUSE_CHAIN (c) = list;
33291 identifier [+/- integer]
33292 vec , identifier [+/- integer]
33296 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33303 cp_parser_error (parser, "expected identifier");
33307 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33309 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33310 tree t, identifier = cp_parser_identifier (parser);
33311 tree addend = NULL;
33313 if (identifier == error_mark_node)
33314 t = error_mark_node;
33317 t = cp_parser_lookup_name_simple
33318 (parser, identifier,
33319 cp_lexer_peek_token (parser->lexer)->location);
33320 if (t == error_mark_node)
33321 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33326 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33328 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33330 addend = integer_zero_node;
33331 goto add_to_vector;
33333 cp_lexer_consume_token (parser->lexer);
33335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33337 cp_parser_error (parser, "expected integer");
33341 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33342 if (TREE_CODE (addend) != INTEGER_CST)
33344 cp_parser_error (parser, "expected integer");
33347 cp_lexer_consume_token (parser->lexer);
33350 if (t != error_mark_node)
33352 vec = tree_cons (addend, t, vec);
33354 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33360 cp_lexer_consume_token (parser->lexer);
33363 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33365 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33366 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33367 OMP_CLAUSE_DECL (u) = nreverse (vec);
33368 OMP_CLAUSE_CHAIN (u) = list;
33375 depend ( depend-kind : variable-list )
33383 depend ( sink : vec ) */
33386 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33389 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33391 matching_parens parens;
33392 if (!parens.require_open (parser))
33395 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33397 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33398 const char *p = IDENTIFIER_POINTER (id);
33400 if (strcmp ("in", p) == 0)
33401 kind = OMP_CLAUSE_DEPEND_IN;
33402 else if (strcmp ("inout", p) == 0)
33403 kind = OMP_CLAUSE_DEPEND_INOUT;
33404 else if (strcmp ("out", p) == 0)
33405 kind = OMP_CLAUSE_DEPEND_OUT;
33406 else if (strcmp ("source", p) == 0)
33407 kind = OMP_CLAUSE_DEPEND_SOURCE;
33408 else if (strcmp ("sink", p) == 0)
33409 kind = OMP_CLAUSE_DEPEND_SINK;
33416 cp_lexer_consume_token (parser->lexer);
33418 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33420 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33421 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33422 OMP_CLAUSE_DECL (c) = NULL_TREE;
33423 OMP_CLAUSE_CHAIN (c) = list;
33424 if (!parens.require_close (parser))
33425 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33426 /*or_comma=*/false,
33427 /*consume_paren=*/true);
33431 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33434 if (kind == OMP_CLAUSE_DEPEND_SINK)
33435 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33438 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33441 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33442 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33447 cp_parser_error (parser, "invalid depend kind");
33449 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33450 /*or_comma=*/false,
33451 /*consume_paren=*/true);
33456 map ( map-kind : variable-list )
33457 map ( variable-list )
33460 alloc | to | from | tofrom
33464 alloc | to | from | tofrom | release | delete
33466 map ( always [,] map-kind: variable-list ) */
33469 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33472 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33473 bool always = false;
33475 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33478 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33480 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33481 const char *p = IDENTIFIER_POINTER (id);
33483 if (strcmp ("always", p) == 0)
33486 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33488 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33489 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33491 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33495 cp_lexer_consume_token (parser->lexer);
33497 cp_lexer_consume_token (parser->lexer);
33502 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33503 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33505 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33506 const char *p = IDENTIFIER_POINTER (id);
33508 if (strcmp ("alloc", p) == 0)
33509 kind = GOMP_MAP_ALLOC;
33510 else if (strcmp ("to", p) == 0)
33511 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33512 else if (strcmp ("from", p) == 0)
33513 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33514 else if (strcmp ("tofrom", p) == 0)
33515 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33516 else if (strcmp ("release", p) == 0)
33517 kind = GOMP_MAP_RELEASE;
33520 cp_parser_error (parser, "invalid map kind");
33521 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33522 /*or_comma=*/false,
33523 /*consume_paren=*/true);
33526 cp_lexer_consume_token (parser->lexer);
33527 cp_lexer_consume_token (parser->lexer);
33529 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33530 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33532 kind = GOMP_MAP_DELETE;
33533 cp_lexer_consume_token (parser->lexer);
33534 cp_lexer_consume_token (parser->lexer);
33537 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33540 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33541 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33547 device ( expression ) */
33550 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33551 location_t location)
33555 matching_parens parens;
33556 if (!parens.require_open (parser))
33559 t = cp_parser_expression (parser);
33561 if (t == error_mark_node
33562 || !parens.require_close (parser))
33563 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33564 /*or_comma=*/false,
33565 /*consume_paren=*/true);
33567 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33568 "device", location);
33570 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33571 OMP_CLAUSE_DEVICE_ID (c) = t;
33572 OMP_CLAUSE_CHAIN (c) = list;
33578 dist_schedule ( static )
33579 dist_schedule ( static , expression ) */
33582 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33583 location_t location)
33587 matching_parens parens;
33588 if (!parens.require_open (parser))
33591 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33593 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33595 cp_lexer_consume_token (parser->lexer);
33597 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33599 cp_lexer_consume_token (parser->lexer);
33601 t = cp_parser_assignment_expression (parser);
33603 if (t == error_mark_node)
33605 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33607 if (!parens.require_close (parser))
33610 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33613 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33615 OMP_CLAUSE_CHAIN (c) = list;
33619 cp_parser_error (parser, "invalid dist_schedule kind");
33621 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33622 /*or_comma=*/false,
33623 /*consume_paren=*/true);
33628 proc_bind ( proc-bind-kind )
33631 master | close | spread */
33634 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33635 location_t location)
33638 enum omp_clause_proc_bind_kind kind;
33640 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33643 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33645 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33646 const char *p = IDENTIFIER_POINTER (id);
33648 if (strcmp ("master", p) == 0)
33649 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33650 else if (strcmp ("close", p) == 0)
33651 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33652 else if (strcmp ("spread", p) == 0)
33653 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33660 cp_lexer_consume_token (parser->lexer);
33661 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33664 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33665 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33667 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33668 OMP_CLAUSE_CHAIN (c) = list;
33672 cp_parser_error (parser, "invalid depend kind");
33674 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33675 /*or_comma=*/false,
33676 /*consume_paren=*/true);
33681 async [( int-expr )] */
33684 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33687 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33689 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33691 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33693 matching_parens parens;
33694 parens.consume_open (parser);
33696 t = cp_parser_expression (parser);
33697 if (t == error_mark_node
33698 || !parens.require_close (parser))
33699 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33700 /*or_comma=*/false,
33701 /*consume_paren=*/true);
33704 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33706 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33707 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33708 OMP_CLAUSE_CHAIN (c) = list;
33714 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33715 is a bitmask in MASK. Return the list of clauses found. */
33718 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33719 const char *where, cp_token *pragma_tok,
33720 bool finish_p = true)
33722 tree clauses = NULL;
33725 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33728 pragma_omp_clause c_kind;
33729 omp_clause_code code;
33730 const char *c_name;
33731 tree prev = clauses;
33733 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33734 cp_lexer_consume_token (parser->lexer);
33736 here = cp_lexer_peek_token (parser->lexer)->location;
33737 c_kind = cp_parser_omp_clause_name (parser);
33741 case PRAGMA_OACC_CLAUSE_ASYNC:
33742 clauses = cp_parser_oacc_clause_async (parser, clauses);
33745 case PRAGMA_OACC_CLAUSE_AUTO:
33746 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33750 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33751 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33752 c_name = "collapse";
33754 case PRAGMA_OACC_CLAUSE_COPY:
33755 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33758 case PRAGMA_OACC_CLAUSE_COPYIN:
33759 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33762 case PRAGMA_OACC_CLAUSE_COPYOUT:
33763 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33764 c_name = "copyout";
33766 case PRAGMA_OACC_CLAUSE_CREATE:
33767 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33770 case PRAGMA_OACC_CLAUSE_DELETE:
33771 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33774 case PRAGMA_OMP_CLAUSE_DEFAULT:
33775 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33776 c_name = "default";
33778 case PRAGMA_OACC_CLAUSE_DEVICE:
33779 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33782 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33783 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33784 c_name = "deviceptr";
33786 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33787 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33788 c_name = "device_resident";
33790 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33791 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33793 c_name = "firstprivate";
33795 case PRAGMA_OACC_CLAUSE_GANG:
33797 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33800 case PRAGMA_OACC_CLAUSE_HOST:
33801 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33804 case PRAGMA_OACC_CLAUSE_IF:
33805 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33808 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33809 clauses = cp_parser_oacc_simple_clause (parser,
33810 OMP_CLAUSE_INDEPENDENT,
33812 c_name = "independent";
33814 case PRAGMA_OACC_CLAUSE_LINK:
33815 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33818 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33819 code = OMP_CLAUSE_NUM_GANGS;
33820 c_name = "num_gangs";
33821 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33824 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33825 c_name = "num_workers";
33826 code = OMP_CLAUSE_NUM_WORKERS;
33827 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33830 case PRAGMA_OACC_CLAUSE_PRESENT:
33831 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33832 c_name = "present";
33834 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33835 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33836 c_name = "present_or_copy";
33838 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33839 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33840 c_name = "present_or_copyin";
33842 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33843 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33844 c_name = "present_or_copyout";
33846 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33847 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33848 c_name = "present_or_create";
33850 case PRAGMA_OACC_CLAUSE_PRIVATE:
33851 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33853 c_name = "private";
33855 case PRAGMA_OACC_CLAUSE_REDUCTION:
33856 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33857 c_name = "reduction";
33859 case PRAGMA_OACC_CLAUSE_SELF:
33860 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33863 case PRAGMA_OACC_CLAUSE_SEQ:
33864 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33868 case PRAGMA_OACC_CLAUSE_TILE:
33869 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33872 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33873 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33875 c_name = "use_device";
33877 case PRAGMA_OACC_CLAUSE_VECTOR:
33879 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33882 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33883 c_name = "vector_length";
33884 code = OMP_CLAUSE_VECTOR_LENGTH;
33885 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33888 case PRAGMA_OACC_CLAUSE_WAIT:
33889 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33892 case PRAGMA_OACC_CLAUSE_WORKER:
33894 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33898 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33904 if (((mask >> c_kind) & 1) == 0)
33906 /* Remove the invalid clause(s) from the list to avoid
33907 confusing the rest of the compiler. */
33909 error_at (here, "%qs is not valid for %qs", c_name, where);
33914 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33917 return finish_omp_clauses (clauses, C_ORT_ACC);
33922 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33923 is a bitmask in MASK. Return the list of clauses found; the result
33924 of clause default goes in *pdefault. */
33927 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33928 const char *where, cp_token *pragma_tok,
33929 bool finish_p = true)
33931 tree clauses = NULL;
33933 cp_token *token = NULL;
33935 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33937 pragma_omp_clause c_kind;
33938 const char *c_name;
33939 tree prev = clauses;
33941 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33942 cp_lexer_consume_token (parser->lexer);
33944 token = cp_lexer_peek_token (parser->lexer);
33945 c_kind = cp_parser_omp_clause_name (parser);
33949 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33950 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33952 c_name = "collapse";
33954 case PRAGMA_OMP_CLAUSE_COPYIN:
33955 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33958 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33959 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33961 c_name = "copyprivate";
33963 case PRAGMA_OMP_CLAUSE_DEFAULT:
33964 clauses = cp_parser_omp_clause_default (parser, clauses,
33965 token->location, false);
33966 c_name = "default";
33968 case PRAGMA_OMP_CLAUSE_FINAL:
33969 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33972 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33973 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33975 c_name = "firstprivate";
33977 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33978 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33980 c_name = "grainsize";
33982 case PRAGMA_OMP_CLAUSE_HINT:
33983 clauses = cp_parser_omp_clause_hint (parser, clauses,
33987 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33988 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33990 c_name = "defaultmap";
33992 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33993 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33995 c_name = "use_device_ptr";
33997 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33998 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34000 c_name = "is_device_ptr";
34002 case PRAGMA_OMP_CLAUSE_IF:
34003 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34007 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34008 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34010 c_name = "lastprivate";
34012 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34013 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34015 c_name = "mergeable";
34017 case PRAGMA_OMP_CLAUSE_NOWAIT:
34018 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34021 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34022 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34024 c_name = "num_tasks";
34026 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34027 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34029 c_name = "num_threads";
34031 case PRAGMA_OMP_CLAUSE_ORDERED:
34032 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34034 c_name = "ordered";
34036 case PRAGMA_OMP_CLAUSE_PRIORITY:
34037 clauses = cp_parser_omp_clause_priority (parser, clauses,
34039 c_name = "priority";
34041 case PRAGMA_OMP_CLAUSE_PRIVATE:
34042 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34044 c_name = "private";
34046 case PRAGMA_OMP_CLAUSE_REDUCTION:
34047 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34048 c_name = "reduction";
34050 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34051 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34053 c_name = "schedule";
34055 case PRAGMA_OMP_CLAUSE_SHARED:
34056 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34060 case PRAGMA_OMP_CLAUSE_UNTIED:
34061 clauses = cp_parser_omp_clause_untied (parser, clauses,
34065 case PRAGMA_OMP_CLAUSE_INBRANCH:
34066 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34067 clauses, token->location);
34068 c_name = "inbranch";
34070 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34071 clauses = cp_parser_omp_clause_branch (parser,
34072 OMP_CLAUSE_NOTINBRANCH,
34073 clauses, token->location);
34074 c_name = "notinbranch";
34076 case PRAGMA_OMP_CLAUSE_PARALLEL:
34077 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34078 clauses, token->location);
34079 c_name = "parallel";
34083 error_at (token->location, "%qs must be the first clause of %qs",
34088 case PRAGMA_OMP_CLAUSE_FOR:
34089 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34090 clauses, token->location);
34093 goto clause_not_first;
34095 case PRAGMA_OMP_CLAUSE_SECTIONS:
34096 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34097 clauses, token->location);
34098 c_name = "sections";
34100 goto clause_not_first;
34102 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34103 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34104 clauses, token->location);
34105 c_name = "taskgroup";
34107 goto clause_not_first;
34109 case PRAGMA_OMP_CLAUSE_LINK:
34110 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34113 case PRAGMA_OMP_CLAUSE_TO:
34114 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34115 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34118 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34121 case PRAGMA_OMP_CLAUSE_FROM:
34122 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34125 case PRAGMA_OMP_CLAUSE_UNIFORM:
34126 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34128 c_name = "uniform";
34130 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34131 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34133 c_name = "num_teams";
34135 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34136 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34138 c_name = "thread_limit";
34140 case PRAGMA_OMP_CLAUSE_ALIGNED:
34141 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34142 c_name = "aligned";
34144 case PRAGMA_OMP_CLAUSE_LINEAR:
34146 bool declare_simd = false;
34147 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34148 declare_simd = true;
34149 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34153 case PRAGMA_OMP_CLAUSE_DEPEND:
34154 clauses = cp_parser_omp_clause_depend (parser, clauses,
34158 case PRAGMA_OMP_CLAUSE_MAP:
34159 clauses = cp_parser_omp_clause_map (parser, clauses);
34162 case PRAGMA_OMP_CLAUSE_DEVICE:
34163 clauses = cp_parser_omp_clause_device (parser, clauses,
34167 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34168 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34170 c_name = "dist_schedule";
34172 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34173 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34175 c_name = "proc_bind";
34177 case PRAGMA_OMP_CLAUSE_SAFELEN:
34178 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34180 c_name = "safelen";
34182 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34183 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34185 c_name = "simdlen";
34187 case PRAGMA_OMP_CLAUSE_NOGROUP:
34188 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34190 c_name = "nogroup";
34192 case PRAGMA_OMP_CLAUSE_THREADS:
34194 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34195 clauses, token->location);
34196 c_name = "threads";
34198 case PRAGMA_OMP_CLAUSE_SIMD:
34200 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34201 clauses, token->location);
34205 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34211 if (((mask >> c_kind) & 1) == 0)
34213 /* Remove the invalid clause(s) from the list to avoid
34214 confusing the rest of the compiler. */
34216 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34220 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34223 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34224 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34226 return finish_omp_clauses (clauses, C_ORT_OMP);
34235 In practice, we're also interested in adding the statement to an
34236 outer node. So it is convenient if we work around the fact that
34237 cp_parser_statement calls add_stmt. */
34240 cp_parser_begin_omp_structured_block (cp_parser *parser)
34242 unsigned save = parser->in_statement;
34244 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34245 This preserves the "not within loop or switch" style error messages
34246 for nonsense cases like
34252 if (parser->in_statement)
34253 parser->in_statement = IN_OMP_BLOCK;
34259 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34261 parser->in_statement = save;
34265 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34267 tree stmt = begin_omp_structured_block ();
34268 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34270 cp_parser_statement (parser, NULL_TREE, false, if_p);
34272 cp_parser_end_omp_structured_block (parser, save);
34273 return finish_omp_structured_block (stmt);
34277 # pragma omp atomic new-line
34281 x binop= expr | x++ | ++x | x-- | --x
34283 +, *, -, /, &, ^, |, <<, >>
34285 where x is an lvalue expression with scalar type.
34288 # pragma omp atomic new-line
34291 # pragma omp atomic read new-line
34294 # pragma omp atomic write new-line
34297 # pragma omp atomic update new-line
34300 # pragma omp atomic capture new-line
34303 # pragma omp atomic capture new-line
34311 expression-stmt | x = x binop expr
34313 v = expression-stmt
34315 { v = x; update-stmt; } | { update-stmt; v = x; }
34319 expression-stmt | x = x binop expr | x = expr binop x
34323 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34325 where x and v are lvalue expressions with scalar type. */
34328 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34330 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34331 tree rhs1 = NULL_TREE, orig_lhs;
34332 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34333 bool structured_block = false;
34334 bool seq_cst = false;
34336 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34338 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34339 const char *p = IDENTIFIER_POINTER (id);
34341 if (!strcmp (p, "seq_cst"))
34344 cp_lexer_consume_token (parser->lexer);
34345 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34346 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34347 cp_lexer_consume_token (parser->lexer);
34350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34352 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34353 const char *p = IDENTIFIER_POINTER (id);
34355 if (!strcmp (p, "read"))
34356 code = OMP_ATOMIC_READ;
34357 else if (!strcmp (p, "write"))
34359 else if (!strcmp (p, "update"))
34361 else if (!strcmp (p, "capture"))
34362 code = OMP_ATOMIC_CAPTURE_NEW;
34366 cp_lexer_consume_token (parser->lexer);
34370 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34371 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34372 cp_lexer_consume_token (parser->lexer);
34374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34376 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34377 const char *p = IDENTIFIER_POINTER (id);
34379 if (!strcmp (p, "seq_cst"))
34382 cp_lexer_consume_token (parser->lexer);
34386 cp_parser_require_pragma_eol (parser, pragma_tok);
34390 case OMP_ATOMIC_READ:
34391 case NOP_EXPR: /* atomic write */
34392 v = cp_parser_unary_expression (parser);
34393 if (v == error_mark_node)
34395 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34397 if (code == NOP_EXPR)
34398 lhs = cp_parser_expression (parser);
34400 lhs = cp_parser_unary_expression (parser);
34401 if (lhs == error_mark_node)
34403 if (code == NOP_EXPR)
34405 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34413 case OMP_ATOMIC_CAPTURE_NEW:
34414 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34416 cp_lexer_consume_token (parser->lexer);
34417 structured_block = true;
34421 v = cp_parser_unary_expression (parser);
34422 if (v == error_mark_node)
34424 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34432 lhs = cp_parser_unary_expression (parser);
34434 switch (TREE_CODE (lhs))
34439 case POSTINCREMENT_EXPR:
34440 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34441 code = OMP_ATOMIC_CAPTURE_OLD;
34443 case PREINCREMENT_EXPR:
34444 lhs = TREE_OPERAND (lhs, 0);
34445 opcode = PLUS_EXPR;
34446 rhs = integer_one_node;
34449 case POSTDECREMENT_EXPR:
34450 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34451 code = OMP_ATOMIC_CAPTURE_OLD;
34453 case PREDECREMENT_EXPR:
34454 lhs = TREE_OPERAND (lhs, 0);
34455 opcode = MINUS_EXPR;
34456 rhs = integer_one_node;
34459 case COMPOUND_EXPR:
34460 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34461 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34462 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34463 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34464 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34465 (TREE_OPERAND (lhs, 1), 0), 0)))
34467 /* Undo effects of boolean_increment for post {in,de}crement. */
34468 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34471 if (TREE_CODE (lhs) == MODIFY_EXPR
34472 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34474 /* Undo effects of boolean_increment. */
34475 if (integer_onep (TREE_OPERAND (lhs, 1)))
34477 /* This is pre or post increment. */
34478 rhs = TREE_OPERAND (lhs, 1);
34479 lhs = TREE_OPERAND (lhs, 0);
34481 if (code == OMP_ATOMIC_CAPTURE_NEW
34482 && !structured_block
34483 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34484 code = OMP_ATOMIC_CAPTURE_OLD;
34490 switch (cp_lexer_peek_token (parser->lexer)->type)
34493 opcode = MULT_EXPR;
34496 opcode = TRUNC_DIV_EXPR;
34499 opcode = PLUS_EXPR;
34502 opcode = MINUS_EXPR;
34504 case CPP_LSHIFT_EQ:
34505 opcode = LSHIFT_EXPR;
34507 case CPP_RSHIFT_EQ:
34508 opcode = RSHIFT_EXPR;
34511 opcode = BIT_AND_EXPR;
34514 opcode = BIT_IOR_EXPR;
34517 opcode = BIT_XOR_EXPR;
34520 enum cp_parser_prec oprec;
34522 cp_lexer_consume_token (parser->lexer);
34523 cp_parser_parse_tentatively (parser);
34524 rhs1 = cp_parser_simple_cast_expression (parser);
34525 if (rhs1 == error_mark_node)
34527 cp_parser_abort_tentative_parse (parser);
34528 cp_parser_simple_cast_expression (parser);
34531 token = cp_lexer_peek_token (parser->lexer);
34532 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34534 cp_parser_abort_tentative_parse (parser);
34535 cp_parser_parse_tentatively (parser);
34536 rhs = cp_parser_binary_expression (parser, false, true,
34537 PREC_NOT_OPERATOR, NULL);
34538 if (rhs == error_mark_node)
34540 cp_parser_abort_tentative_parse (parser);
34541 cp_parser_binary_expression (parser, false, true,
34542 PREC_NOT_OPERATOR, NULL);
34545 switch (TREE_CODE (rhs))
34548 case TRUNC_DIV_EXPR:
34557 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34559 if (cp_parser_parse_definitely (parser))
34561 opcode = TREE_CODE (rhs);
34562 rhs1 = TREE_OPERAND (rhs, 0);
34563 rhs = TREE_OPERAND (rhs, 1);
34573 cp_parser_abort_tentative_parse (parser);
34574 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34576 rhs = cp_parser_expression (parser);
34577 if (rhs == error_mark_node)
34583 cp_parser_error (parser,
34584 "invalid form of %<#pragma omp atomic%>");
34587 if (!cp_parser_parse_definitely (parser))
34589 switch (token->type)
34591 case CPP_SEMICOLON:
34592 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34594 code = OMP_ATOMIC_CAPTURE_OLD;
34599 cp_lexer_consume_token (parser->lexer);
34602 else if (structured_block)
34609 cp_parser_error (parser,
34610 "invalid form of %<#pragma omp atomic%>");
34613 opcode = MULT_EXPR;
34616 opcode = TRUNC_DIV_EXPR;
34619 opcode = PLUS_EXPR;
34622 opcode = MINUS_EXPR;
34625 opcode = LSHIFT_EXPR;
34628 opcode = RSHIFT_EXPR;
34631 opcode = BIT_AND_EXPR;
34634 opcode = BIT_IOR_EXPR;
34637 opcode = BIT_XOR_EXPR;
34640 cp_parser_error (parser,
34641 "invalid operator for %<#pragma omp atomic%>");
34644 oprec = TOKEN_PRECEDENCE (token);
34645 gcc_assert (oprec != PREC_NOT_OPERATOR);
34646 if (commutative_tree_code (opcode))
34647 oprec = (enum cp_parser_prec) (oprec - 1);
34648 cp_lexer_consume_token (parser->lexer);
34649 rhs = cp_parser_binary_expression (parser, false, false,
34651 if (rhs == error_mark_node)
34656 cp_parser_error (parser,
34657 "invalid operator for %<#pragma omp atomic%>");
34660 cp_lexer_consume_token (parser->lexer);
34662 rhs = cp_parser_expression (parser);
34663 if (rhs == error_mark_node)
34668 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34670 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34672 v = cp_parser_unary_expression (parser);
34673 if (v == error_mark_node)
34675 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34677 lhs1 = cp_parser_unary_expression (parser);
34678 if (lhs1 == error_mark_node)
34681 if (structured_block)
34683 cp_parser_consume_semicolon_at_end_of_statement (parser);
34684 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34687 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34688 if (!structured_block)
34689 cp_parser_consume_semicolon_at_end_of_statement (parser);
34693 cp_parser_skip_to_end_of_block_or_statement (parser);
34694 if (structured_block)
34696 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34697 cp_lexer_consume_token (parser->lexer);
34698 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34700 cp_parser_skip_to_end_of_block_or_statement (parser);
34701 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34702 cp_lexer_consume_token (parser->lexer);
34709 # pragma omp barrier new-line */
34712 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34714 cp_parser_require_pragma_eol (parser, pragma_tok);
34715 finish_omp_barrier ();
34719 # pragma omp critical [(name)] new-line
34723 # pragma omp critical [(name) [hint(expression)]] new-line
34724 structured-block */
34726 #define OMP_CRITICAL_CLAUSE_MASK \
34727 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34730 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34732 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34734 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34736 matching_parens parens;
34737 parens.consume_open (parser);
34739 name = cp_parser_identifier (parser);
34741 if (name == error_mark_node
34742 || !parens.require_close (parser))
34743 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34744 /*or_comma=*/false,
34745 /*consume_paren=*/true);
34746 if (name == error_mark_node)
34749 clauses = cp_parser_omp_all_clauses (parser,
34750 OMP_CRITICAL_CLAUSE_MASK,
34751 "#pragma omp critical", pragma_tok);
34754 cp_parser_require_pragma_eol (parser, pragma_tok);
34756 stmt = cp_parser_omp_structured_block (parser, if_p);
34757 return c_finish_omp_critical (input_location, stmt, name, clauses);
34761 # pragma omp flush flush-vars[opt] new-line
34764 ( variable-list ) */
34767 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34769 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34770 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34771 cp_parser_require_pragma_eol (parser, pragma_tok);
34773 finish_omp_flush ();
34776 /* Helper function, to parse omp for increment expression. */
34779 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34781 tree cond = cp_parser_binary_expression (parser, false, true,
34782 PREC_NOT_OPERATOR, NULL);
34783 if (cond == error_mark_node
34784 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34786 cp_parser_skip_to_end_of_statement (parser);
34787 return error_mark_node;
34790 switch (TREE_CODE (cond))
34798 /* Fall through: OpenMP disallows NE_EXPR. */
34799 gcc_fallthrough ();
34801 return error_mark_node;
34804 /* If decl is an iterator, preserve LHS and RHS of the relational
34805 expr until finish_omp_for. */
34807 && (type_dependent_expression_p (decl)
34808 || CLASS_TYPE_P (TREE_TYPE (decl))))
34811 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34813 TREE_OPERAND (cond, 0), ERROR_MARK,
34814 TREE_OPERAND (cond, 1), ERROR_MARK,
34815 /*overload=*/NULL, tf_warning_or_error);
34818 /* Helper function, to parse omp for increment expression. */
34821 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34823 cp_token *token = cp_lexer_peek_token (parser->lexer);
34829 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34831 op = (token->type == CPP_PLUS_PLUS
34832 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34833 cp_lexer_consume_token (parser->lexer);
34834 lhs = cp_parser_simple_cast_expression (parser);
34836 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34837 return error_mark_node;
34838 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34841 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34843 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34844 return error_mark_node;
34846 token = cp_lexer_peek_token (parser->lexer);
34847 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34849 op = (token->type == CPP_PLUS_PLUS
34850 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34851 cp_lexer_consume_token (parser->lexer);
34852 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34855 op = cp_parser_assignment_operator_opt (parser);
34856 if (op == ERROR_MARK)
34857 return error_mark_node;
34859 if (op != NOP_EXPR)
34861 rhs = cp_parser_assignment_expression (parser);
34862 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34863 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34866 lhs = cp_parser_binary_expression (parser, false, false,
34867 PREC_ADDITIVE_EXPRESSION, NULL);
34868 token = cp_lexer_peek_token (parser->lexer);
34869 decl_first = (lhs == decl
34870 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34873 if (token->type != CPP_PLUS
34874 && token->type != CPP_MINUS)
34875 return error_mark_node;
34879 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34880 cp_lexer_consume_token (parser->lexer);
34881 rhs = cp_parser_binary_expression (parser, false, false,
34882 PREC_ADDITIVE_EXPRESSION, NULL);
34883 token = cp_lexer_peek_token (parser->lexer);
34884 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34886 if (lhs == NULL_TREE)
34888 if (op == PLUS_EXPR)
34891 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34892 tf_warning_or_error);
34895 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34896 ERROR_MARK, NULL, tf_warning_or_error);
34899 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34904 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34905 || op == MINUS_EXPR)
34906 return error_mark_node;
34907 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34910 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34912 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34915 /* Parse the initialization statement of an OpenMP for loop.
34917 Return true if the resulting construct should have an
34918 OMP_CLAUSE_PRIVATE added to it. */
34921 cp_parser_omp_for_loop_init (cp_parser *parser,
34922 tree &this_pre_body,
34923 vec<tree, va_gc> *&for_block,
34929 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34932 tree add_private_clause = NULL_TREE;
34934 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34938 integer-type var = lb
34939 random-access-iterator-type var = lb
34940 pointer-type var = lb
34942 cp_decl_specifier_seq type_specifiers;
34944 /* First, try to parse as an initialized declaration. See
34945 cp_parser_condition, from whence the bulk of this is copied. */
34947 cp_parser_parse_tentatively (parser);
34948 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34949 /*is_trailing_return=*/false,
34951 if (cp_parser_parse_definitely (parser))
34953 /* If parsing a type specifier seq succeeded, then this
34954 MUST be a initialized declaration. */
34955 tree asm_specification, attributes;
34956 cp_declarator *declarator;
34958 declarator = cp_parser_declarator (parser,
34959 CP_PARSER_DECLARATOR_NAMED,
34960 /*ctor_dtor_or_conv_p=*/NULL,
34961 /*parenthesized_p=*/NULL,
34962 /*member_p=*/false,
34963 /*friend_p=*/false);
34964 attributes = cp_parser_attributes_opt (parser);
34965 asm_specification = cp_parser_asm_specification_opt (parser);
34967 if (declarator == cp_error_declarator)
34968 cp_parser_skip_to_end_of_statement (parser);
34972 tree pushed_scope, auto_node;
34974 decl = start_decl (declarator, &type_specifiers,
34975 SD_INITIALIZED, attributes,
34976 /*prefix_attributes=*/NULL_TREE,
34979 auto_node = type_uses_auto (TREE_TYPE (decl));
34980 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34982 if (cp_lexer_next_token_is (parser->lexer,
34984 error ("parenthesized initialization is not allowed in "
34985 "OpenMP %<for%> loop");
34987 /* Trigger an error. */
34988 cp_parser_require (parser, CPP_EQ, RT_EQ);
34990 init = error_mark_node;
34991 cp_parser_skip_to_end_of_statement (parser);
34993 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34994 || type_dependent_expression_p (decl)
34997 bool is_direct_init, is_non_constant_init;
34999 init = cp_parser_initializer (parser,
35001 &is_non_constant_init);
35006 = do_auto_deduction (TREE_TYPE (decl), init,
35009 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35010 && !type_dependent_expression_p (decl))
35014 cp_finish_decl (decl, init, !is_non_constant_init,
35016 LOOKUP_ONLYCONVERTING);
35018 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35020 vec_safe_push (for_block, this_pre_body);
35025 init = pop_stmt_list (this_pre_body);
35026 if (init && TREE_CODE (init) == STATEMENT_LIST)
35028 tree_stmt_iterator i = tsi_start (init);
35029 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35030 while (!tsi_end_p (i))
35032 tree t = tsi_stmt (i);
35033 if (TREE_CODE (t) == DECL_EXPR
35034 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35037 vec_safe_push (for_block, t);
35042 if (tsi_one_before_end_p (i))
35044 tree t = tsi_stmt (i);
35046 free_stmt_list (init);
35051 this_pre_body = NULL_TREE;
35056 cp_lexer_consume_token (parser->lexer);
35057 init = cp_parser_assignment_expression (parser);
35060 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
35061 init = error_mark_node;
35063 cp_finish_decl (decl, NULL_TREE,
35064 /*init_const_expr_p=*/false,
35066 LOOKUP_ONLYCONVERTING);
35070 pop_scope (pushed_scope);
35076 /* If parsing a type specifier sequence failed, then
35077 this MUST be a simple expression. */
35078 cp_parser_parse_tentatively (parser);
35079 decl = cp_parser_primary_expression (parser, false, false,
35081 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35082 if (!cp_parser_error_occurred (parser)
35084 && (TREE_CODE (decl) == COMPONENT_REF
35085 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35087 cp_parser_abort_tentative_parse (parser);
35088 cp_parser_parse_tentatively (parser);
35089 cp_token *token = cp_lexer_peek_token (parser->lexer);
35090 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35091 /*check_dependency_p=*/true,
35092 /*template_p=*/NULL,
35093 /*declarator_p=*/false,
35094 /*optional_p=*/false);
35095 if (name != error_mark_node
35096 && last_tok == cp_lexer_peek_token (parser->lexer))
35098 decl = cp_parser_lookup_name_simple (parser, name,
35100 if (TREE_CODE (decl) == FIELD_DECL)
35101 add_private_clause = omp_privatize_field (decl, false);
35103 cp_parser_abort_tentative_parse (parser);
35104 cp_parser_parse_tentatively (parser);
35105 decl = cp_parser_primary_expression (parser, false, false,
35108 if (!cp_parser_error_occurred (parser)
35111 && CLASS_TYPE_P (TREE_TYPE (decl)))
35115 cp_parser_parse_definitely (parser);
35116 cp_parser_require (parser, CPP_EQ, RT_EQ);
35117 rhs = cp_parser_assignment_expression (parser);
35119 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35122 tf_warning_or_error));
35123 if (!add_private_clause)
35124 add_private_clause = decl;
35129 cp_parser_abort_tentative_parse (parser);
35130 init = cp_parser_expression (parser);
35133 if (TREE_CODE (init) == MODIFY_EXPR
35134 || TREE_CODE (init) == MODOP_EXPR)
35135 real_decl = TREE_OPERAND (init, 0);
35139 return add_private_clause;
35142 /* Parse the restricted form of the for statement allowed by OpenMP. */
35145 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35146 tree *cclauses, bool *if_p)
35148 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35149 tree real_decl, initv, condv, incrv, declv;
35150 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35151 location_t loc_first;
35152 bool collapse_err = false;
35153 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35154 vec<tree, va_gc> *for_block = make_tree_vector ();
35155 auto_vec<tree, 4> orig_inits;
35156 bool tiling = false;
35158 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35159 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35160 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35161 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35164 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35166 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35167 && OMP_CLAUSE_ORDERED_EXPR (cl))
35170 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35173 if (ordered && ordered < collapse)
35175 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35176 "%<ordered%> clause parameter is less than %<collapse%>");
35177 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35178 = build_int_cst (NULL_TREE, collapse);
35179 ordered = collapse;
35183 for (tree *pc = &clauses; *pc; )
35184 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35186 error_at (OMP_CLAUSE_LOCATION (*pc),
35187 "%<linear%> clause may not be specified together "
35188 "with %<ordered%> clause with a parameter");
35189 *pc = OMP_CLAUSE_CHAIN (*pc);
35192 pc = &OMP_CLAUSE_CHAIN (*pc);
35195 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35196 count = ordered ? ordered : collapse;
35198 declv = make_tree_vec (count);
35199 initv = make_tree_vec (count);
35200 condv = make_tree_vec (count);
35201 incrv = make_tree_vec (count);
35203 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35205 for (i = 0; i < count; i++)
35207 int bracecount = 0;
35208 tree add_private_clause = NULL_TREE;
35211 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35214 cp_parser_error (parser, "for statement expected");
35217 loc = cp_lexer_consume_token (parser->lexer)->location;
35219 matching_parens parens;
35220 if (!parens.require_open (parser))
35223 init = orig_init = decl = real_decl = NULL;
35224 this_pre_body = push_stmt_list ();
35227 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35228 init, orig_init, decl, real_decl);
35230 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35233 this_pre_body = pop_stmt_list (this_pre_body);
35237 pre_body = push_stmt_list ();
35239 add_stmt (this_pre_body);
35240 pre_body = pop_stmt_list (pre_body);
35243 pre_body = this_pre_body;
35248 if (cclauses != NULL
35249 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35250 && real_decl != NULL_TREE)
35253 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35254 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35255 && OMP_CLAUSE_DECL (*c) == real_decl)
35257 error_at (loc, "iteration variable %qD"
35258 " should not be firstprivate", real_decl);
35259 *c = OMP_CLAUSE_CHAIN (*c);
35261 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35262 && OMP_CLAUSE_DECL (*c) == real_decl)
35264 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35266 *c = OMP_CLAUSE_CHAIN (*c);
35267 if (code == OMP_SIMD)
35269 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35270 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35274 OMP_CLAUSE_CHAIN (l) = clauses;
35277 add_private_clause = NULL_TREE;
35281 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35282 && OMP_CLAUSE_DECL (*c) == real_decl)
35283 add_private_clause = NULL_TREE;
35284 c = &OMP_CLAUSE_CHAIN (*c);
35288 if (add_private_clause)
35291 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35293 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35294 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35295 && OMP_CLAUSE_DECL (c) == decl)
35297 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35298 && OMP_CLAUSE_DECL (c) == decl)
35299 error_at (loc, "iteration variable %qD "
35300 "should not be firstprivate",
35302 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35303 && OMP_CLAUSE_DECL (c) == decl)
35304 error_at (loc, "iteration variable %qD should not be reduction",
35309 if (code != OMP_SIMD)
35310 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35311 else if (collapse == 1)
35312 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35314 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35315 OMP_CLAUSE_DECL (c) = add_private_clause;
35316 c = finish_omp_clauses (c, C_ORT_OMP);
35319 OMP_CLAUSE_CHAIN (c) = clauses;
35321 /* For linear, signal that we need to fill up
35322 the so far unknown linear step. */
35323 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35324 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35330 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35331 cond = cp_parser_omp_for_cond (parser, decl);
35332 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35337 /* If decl is an iterator, preserve the operator on decl
35338 until finish_omp_for. */
35340 && ((processing_template_decl
35341 && (TREE_TYPE (real_decl) == NULL_TREE
35342 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35343 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35344 incr = cp_parser_omp_for_incr (parser, real_decl);
35346 incr = cp_parser_expression (parser);
35347 if (!EXPR_HAS_LOCATION (incr))
35348 protected_set_expr_location (incr, input_location);
35351 if (!parens.require_close (parser))
35352 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35353 /*or_comma=*/false,
35354 /*consume_paren=*/true);
35356 TREE_VEC_ELT (declv, i) = decl;
35357 TREE_VEC_ELT (initv, i) = init;
35358 TREE_VEC_ELT (condv, i) = cond;
35359 TREE_VEC_ELT (incrv, i) = incr;
35362 orig_inits.safe_grow_cleared (i + 1);
35363 orig_inits[i] = orig_init;
35366 if (i == count - 1)
35369 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35370 in between the collapsed for loops to be still considered perfectly
35371 nested. Hopefully the final version clarifies this.
35372 For now handle (multiple) {'s and empty statements. */
35373 cp_parser_parse_tentatively (parser);
35376 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35378 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35380 cp_lexer_consume_token (parser->lexer);
35383 else if (bracecount
35384 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35385 cp_lexer_consume_token (parser->lexer);
35388 loc = cp_lexer_peek_token (parser->lexer)->location;
35389 error_at (loc, "not enough for loops to collapse");
35390 collapse_err = true;
35391 cp_parser_abort_tentative_parse (parser);
35399 cp_parser_parse_definitely (parser);
35400 nbraces += bracecount;
35407 /* Note that we saved the original contents of this flag when we entered
35408 the structured block, and so we don't need to re-save it here. */
35409 parser->in_statement = IN_OMP_FOR;
35411 /* Note that the grammar doesn't call for a structured block here,
35412 though the loop as a whole is a structured block. */
35413 body = push_stmt_list ();
35414 cp_parser_statement (parser, NULL_TREE, false, if_p);
35415 body = pop_stmt_list (body);
35417 if (declv == NULL_TREE)
35420 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35421 body, pre_body, &orig_inits, clauses);
35425 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35427 cp_lexer_consume_token (parser->lexer);
35430 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35431 cp_lexer_consume_token (parser->lexer);
35436 error_at (cp_lexer_peek_token (parser->lexer)->location,
35437 "collapsed loops not perfectly nested");
35439 collapse_err = true;
35440 cp_parser_statement_seq_opt (parser, NULL);
35441 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35446 while (!for_block->is_empty ())
35448 tree t = for_block->pop ();
35449 if (TREE_CODE (t) == STATEMENT_LIST)
35450 add_stmt (pop_stmt_list (t));
35454 release_tree_vector (for_block);
35459 /* Helper function for OpenMP parsing, split clauses and call
35460 finish_omp_clauses on each of the set of clauses afterwards. */
35463 cp_omp_split_clauses (location_t loc, enum tree_code code,
35464 omp_clause_mask mask, tree clauses, tree *cclauses)
35467 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35468 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35470 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35474 #pragma omp simd simd-clause[optseq] new-line
35477 #define OMP_SIMD_CLAUSE_MASK \
35478 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35488 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35489 char *p_name, omp_clause_mask mask, tree *cclauses,
35492 tree clauses, sb, ret;
35494 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35496 strcat (p_name, " simd");
35497 mask |= OMP_SIMD_CLAUSE_MASK;
35499 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35503 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35504 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35505 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35506 OMP_CLAUSE_ORDERED);
35507 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35509 error_at (OMP_CLAUSE_LOCATION (c),
35510 "%<ordered%> clause with parameter may not be specified "
35511 "on %qs construct", p_name);
35512 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35516 sb = begin_omp_structured_block ();
35517 save = cp_parser_begin_omp_structured_block (parser);
35519 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35521 cp_parser_end_omp_structured_block (parser, save);
35522 add_stmt (finish_omp_structured_block (sb));
35528 #pragma omp for for-clause[optseq] new-line
35532 #pragma omp for simd for-simd-clause[optseq] new-line
35535 #define OMP_FOR_CLAUSE_MASK \
35536 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35547 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35548 char *p_name, omp_clause_mask mask, tree *cclauses,
35551 tree clauses, sb, ret;
35553 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35555 strcat (p_name, " for");
35556 mask |= OMP_FOR_CLAUSE_MASK;
35557 /* parallel for{, simd} disallows nowait clause, but for
35558 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35559 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35560 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35561 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35562 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35563 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35565 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35567 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35568 const char *p = IDENTIFIER_POINTER (id);
35570 if (strcmp (p, "simd") == 0)
35572 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35573 if (cclauses == NULL)
35574 cclauses = cclauses_buf;
35576 cp_lexer_consume_token (parser->lexer);
35577 if (!flag_openmp) /* flag_openmp_simd */
35578 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35580 sb = begin_omp_structured_block ();
35581 save = cp_parser_begin_omp_structured_block (parser);
35582 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35584 cp_parser_end_omp_structured_block (parser, save);
35585 tree body = finish_omp_structured_block (sb);
35588 ret = make_node (OMP_FOR);
35589 TREE_TYPE (ret) = void_type_node;
35590 OMP_FOR_BODY (ret) = body;
35591 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35592 SET_EXPR_LOCATION (ret, loc);
35597 if (!flag_openmp) /* flag_openmp_simd */
35599 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35603 /* Composite distribute parallel for disallows linear clause. */
35604 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35605 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35607 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35611 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35612 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35615 sb = begin_omp_structured_block ();
35616 save = cp_parser_begin_omp_structured_block (parser);
35618 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35620 cp_parser_end_omp_structured_block (parser, save);
35621 add_stmt (finish_omp_structured_block (sb));
35627 # pragma omp master new-line
35628 structured-block */
35631 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35633 cp_parser_require_pragma_eol (parser, pragma_tok);
35634 return c_finish_omp_master (input_location,
35635 cp_parser_omp_structured_block (parser, if_p));
35639 # pragma omp ordered new-line
35643 # pragma omp ordered ordered-clauses new-line
35644 structured-block */
35646 #define OMP_ORDERED_CLAUSE_MASK \
35647 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35650 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35651 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35654 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35655 enum pragma_context context, bool *if_p)
35657 location_t loc = pragma_tok->location;
35659 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35661 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35662 const char *p = IDENTIFIER_POINTER (id);
35664 if (strcmp (p, "depend") == 0)
35666 if (!flag_openmp) /* flag_openmp_simd */
35668 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35671 if (context == pragma_stmt)
35673 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35674 "%<depend%> clause may only be used in compound "
35676 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35680 = cp_parser_omp_all_clauses (parser,
35681 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35682 "#pragma omp ordered", pragma_tok);
35683 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35689 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35690 "#pragma omp ordered", pragma_tok);
35692 if (!flag_openmp /* flag_openmp_simd */
35693 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35696 c_finish_omp_ordered (loc, clauses,
35697 cp_parser_omp_structured_block (parser, if_p));
35704 { section-sequence }
35707 section-directive[opt] structured-block
35708 section-sequence section-directive structured-block */
35711 cp_parser_omp_sections_scope (cp_parser *parser)
35713 tree stmt, substmt;
35714 bool error_suppress = false;
35717 matching_braces braces;
35718 if (!braces.require_open (parser))
35721 stmt = push_stmt_list ();
35723 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35724 != PRAGMA_OMP_SECTION)
35726 substmt = cp_parser_omp_structured_block (parser, NULL);
35727 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35728 add_stmt (substmt);
35733 tok = cp_lexer_peek_token (parser->lexer);
35734 if (tok->type == CPP_CLOSE_BRACE)
35736 if (tok->type == CPP_EOF)
35739 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35741 cp_lexer_consume_token (parser->lexer);
35742 cp_parser_require_pragma_eol (parser, tok);
35743 error_suppress = false;
35745 else if (!error_suppress)
35747 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35748 error_suppress = true;
35751 substmt = cp_parser_omp_structured_block (parser, NULL);
35752 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35753 add_stmt (substmt);
35755 braces.require_close (parser);
35757 substmt = pop_stmt_list (stmt);
35759 stmt = make_node (OMP_SECTIONS);
35760 TREE_TYPE (stmt) = void_type_node;
35761 OMP_SECTIONS_BODY (stmt) = substmt;
35768 # pragma omp sections sections-clause[optseq] newline
35771 #define OMP_SECTIONS_CLAUSE_MASK \
35772 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35779 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35780 char *p_name, omp_clause_mask mask, tree *cclauses)
35783 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35785 strcat (p_name, " sections");
35786 mask |= OMP_SECTIONS_CLAUSE_MASK;
35788 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35790 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35794 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35795 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35798 ret = cp_parser_omp_sections_scope (parser);
35800 OMP_SECTIONS_CLAUSES (ret) = clauses;
35806 # pragma omp parallel parallel-clause[optseq] new-line
35808 # pragma omp parallel for parallel-for-clause[optseq] new-line
35810 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35814 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35815 structured-block */
35817 #define OMP_PARALLEL_CLAUSE_MASK \
35818 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35829 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35830 char *p_name, omp_clause_mask mask, tree *cclauses,
35833 tree stmt, clauses, block;
35835 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35837 strcat (p_name, " parallel");
35838 mask |= OMP_PARALLEL_CLAUSE_MASK;
35839 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35840 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35841 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35842 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35844 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35846 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35847 if (cclauses == NULL)
35848 cclauses = cclauses_buf;
35850 cp_lexer_consume_token (parser->lexer);
35851 if (!flag_openmp) /* flag_openmp_simd */
35852 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35854 block = begin_omp_parallel ();
35855 save = cp_parser_begin_omp_structured_block (parser);
35856 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35858 cp_parser_end_omp_structured_block (parser, save);
35859 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35861 if (ret == NULL_TREE)
35863 OMP_PARALLEL_COMBINED (stmt) = 1;
35866 /* When combined with distribute, parallel has to be followed by for.
35867 #pragma omp target parallel is allowed though. */
35869 && (mask & (OMP_CLAUSE_MASK_1
35870 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35872 error_at (loc, "expected %<for%> after %qs", p_name);
35873 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35876 else if (!flag_openmp) /* flag_openmp_simd */
35878 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35881 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35883 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35884 const char *p = IDENTIFIER_POINTER (id);
35885 if (strcmp (p, "sections") == 0)
35887 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35888 cclauses = cclauses_buf;
35890 cp_lexer_consume_token (parser->lexer);
35891 block = begin_omp_parallel ();
35892 save = cp_parser_begin_omp_structured_block (parser);
35893 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35894 cp_parser_end_omp_structured_block (parser, save);
35895 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35897 OMP_PARALLEL_COMBINED (stmt) = 1;
35902 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35906 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35907 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35910 block = begin_omp_parallel ();
35911 save = cp_parser_begin_omp_structured_block (parser);
35912 cp_parser_statement (parser, NULL_TREE, false, if_p);
35913 cp_parser_end_omp_structured_block (parser, save);
35914 stmt = finish_omp_parallel (clauses, block);
35919 # pragma omp single single-clause[optseq] new-line
35920 structured-block */
35922 #define OMP_SINGLE_CLAUSE_MASK \
35923 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35929 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35931 tree stmt = make_node (OMP_SINGLE);
35932 TREE_TYPE (stmt) = void_type_node;
35934 OMP_SINGLE_CLAUSES (stmt)
35935 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35936 "#pragma omp single", pragma_tok);
35937 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35939 return add_stmt (stmt);
35943 # pragma omp task task-clause[optseq] new-line
35944 structured-block */
35946 #define OMP_TASK_CLAUSE_MASK \
35947 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35959 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35961 tree clauses, block;
35964 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35965 "#pragma omp task", pragma_tok);
35966 block = begin_omp_task ();
35967 save = cp_parser_begin_omp_structured_block (parser);
35968 cp_parser_statement (parser, NULL_TREE, false, if_p);
35969 cp_parser_end_omp_structured_block (parser, save);
35970 return finish_omp_task (clauses, block);
35974 # pragma omp taskwait new-line */
35977 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35979 cp_parser_require_pragma_eol (parser, pragma_tok);
35980 finish_omp_taskwait ();
35984 # pragma omp taskyield new-line */
35987 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35989 cp_parser_require_pragma_eol (parser, pragma_tok);
35990 finish_omp_taskyield ();
35994 # pragma omp taskgroup new-line
35995 structured-block */
35998 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36000 cp_parser_require_pragma_eol (parser, pragma_tok);
36001 return c_finish_omp_taskgroup (input_location,
36002 cp_parser_omp_structured_block (parser,
36008 # pragma omp threadprivate (variable-list) */
36011 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36015 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36016 cp_parser_require_pragma_eol (parser, pragma_tok);
36018 finish_omp_threadprivate (vars);
36022 # pragma omp cancel cancel-clause[optseq] new-line */
36024 #define OMP_CANCEL_CLAUSE_MASK \
36025 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36032 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36034 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36035 "#pragma omp cancel", pragma_tok);
36036 finish_omp_cancel (clauses);
36040 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36042 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36043 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36049 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36050 enum pragma_context context)
36053 bool point_seen = false;
36055 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36057 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36058 const char *p = IDENTIFIER_POINTER (id);
36060 if (strcmp (p, "point") == 0)
36062 cp_lexer_consume_token (parser->lexer);
36068 cp_parser_error (parser, "expected %<point%>");
36069 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36073 if (context != pragma_compound)
36075 if (context == pragma_stmt)
36076 error_at (pragma_tok->location,
36077 "%<#pragma %s%> may only be used in compound statements",
36078 "omp cancellation point");
36080 cp_parser_error (parser, "expected declaration specifiers");
36081 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36085 clauses = cp_parser_omp_all_clauses (parser,
36086 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36087 "#pragma omp cancellation point",
36089 finish_omp_cancellation_point (clauses);
36093 #pragma omp distribute distribute-clause[optseq] new-line
36096 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36097 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36104 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36105 char *p_name, omp_clause_mask mask, tree *cclauses,
36108 tree clauses, sb, ret;
36110 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36112 strcat (p_name, " distribute");
36113 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36115 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36117 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36118 const char *p = IDENTIFIER_POINTER (id);
36120 bool parallel = false;
36122 if (strcmp (p, "simd") == 0)
36125 parallel = strcmp (p, "parallel") == 0;
36126 if (parallel || simd)
36128 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36129 if (cclauses == NULL)
36130 cclauses = cclauses_buf;
36131 cp_lexer_consume_token (parser->lexer);
36132 if (!flag_openmp) /* flag_openmp_simd */
36135 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36138 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36141 sb = begin_omp_structured_block ();
36142 save = cp_parser_begin_omp_structured_block (parser);
36144 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36147 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36149 cp_parser_end_omp_structured_block (parser, save);
36150 tree body = finish_omp_structured_block (sb);
36153 ret = make_node (OMP_DISTRIBUTE);
36154 TREE_TYPE (ret) = void_type_node;
36155 OMP_FOR_BODY (ret) = body;
36156 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36157 SET_EXPR_LOCATION (ret, loc);
36162 if (!flag_openmp) /* flag_openmp_simd */
36164 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36168 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36172 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36173 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36176 sb = begin_omp_structured_block ();
36177 save = cp_parser_begin_omp_structured_block (parser);
36179 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36181 cp_parser_end_omp_structured_block (parser, save);
36182 add_stmt (finish_omp_structured_block (sb));
36188 # pragma omp teams teams-clause[optseq] new-line
36189 structured-block */
36191 #define OMP_TEAMS_CLAUSE_MASK \
36192 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36201 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36202 char *p_name, omp_clause_mask mask, tree *cclauses,
36205 tree clauses, sb, ret;
36207 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36209 strcat (p_name, " teams");
36210 mask |= OMP_TEAMS_CLAUSE_MASK;
36212 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36214 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36215 const char *p = IDENTIFIER_POINTER (id);
36216 if (strcmp (p, "distribute") == 0)
36218 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36219 if (cclauses == NULL)
36220 cclauses = cclauses_buf;
36222 cp_lexer_consume_token (parser->lexer);
36223 if (!flag_openmp) /* flag_openmp_simd */
36224 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36226 sb = begin_omp_structured_block ();
36227 save = cp_parser_begin_omp_structured_block (parser);
36228 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36230 cp_parser_end_omp_structured_block (parser, save);
36231 tree body = finish_omp_structured_block (sb);
36234 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36235 ret = make_node (OMP_TEAMS);
36236 TREE_TYPE (ret) = void_type_node;
36237 OMP_TEAMS_CLAUSES (ret) = clauses;
36238 OMP_TEAMS_BODY (ret) = body;
36239 OMP_TEAMS_COMBINED (ret) = 1;
36240 SET_EXPR_LOCATION (ret, loc);
36241 return add_stmt (ret);
36244 if (!flag_openmp) /* flag_openmp_simd */
36246 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36250 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36254 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36255 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36258 tree stmt = make_node (OMP_TEAMS);
36259 TREE_TYPE (stmt) = void_type_node;
36260 OMP_TEAMS_CLAUSES (stmt) = clauses;
36261 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36262 SET_EXPR_LOCATION (stmt, loc);
36264 return add_stmt (stmt);
36268 # pragma omp target data target-data-clause[optseq] new-line
36269 structured-block */
36271 #define OMP_TARGET_DATA_CLAUSE_MASK \
36272 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36278 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36281 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36282 "#pragma omp target data", pragma_tok);
36284 for (tree *pc = &clauses; *pc;)
36286 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36287 switch (OMP_CLAUSE_MAP_KIND (*pc))
36290 case GOMP_MAP_ALWAYS_TO:
36291 case GOMP_MAP_FROM:
36292 case GOMP_MAP_ALWAYS_FROM:
36293 case GOMP_MAP_TOFROM:
36294 case GOMP_MAP_ALWAYS_TOFROM:
36295 case GOMP_MAP_ALLOC:
36298 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36299 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36300 case GOMP_MAP_ALWAYS_POINTER:
36304 error_at (OMP_CLAUSE_LOCATION (*pc),
36305 "%<#pragma omp target data%> with map-type other "
36306 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36307 "on %<map%> clause");
36308 *pc = OMP_CLAUSE_CHAIN (*pc);
36311 pc = &OMP_CLAUSE_CHAIN (*pc);
36317 error_at (pragma_tok->location,
36318 "%<#pragma omp target data%> must contain at least "
36319 "one %<map%> clause");
36323 tree stmt = make_node (OMP_TARGET_DATA);
36324 TREE_TYPE (stmt) = void_type_node;
36325 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36327 keep_next_level (true);
36328 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36330 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36331 return add_stmt (stmt);
36335 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36336 structured-block */
36338 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36339 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36346 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36347 enum pragma_context context)
36349 bool data_seen = false;
36350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36352 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36353 const char *p = IDENTIFIER_POINTER (id);
36355 if (strcmp (p, "data") == 0)
36357 cp_lexer_consume_token (parser->lexer);
36363 cp_parser_error (parser, "expected %<data%>");
36364 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36368 if (context == pragma_stmt)
36370 error_at (pragma_tok->location,
36371 "%<#pragma %s%> may only be used in compound statements",
36372 "omp target enter data");
36373 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36378 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36379 "#pragma omp target enter data", pragma_tok);
36381 for (tree *pc = &clauses; *pc;)
36383 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36384 switch (OMP_CLAUSE_MAP_KIND (*pc))
36387 case GOMP_MAP_ALWAYS_TO:
36388 case GOMP_MAP_ALLOC:
36391 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36392 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36393 case GOMP_MAP_ALWAYS_POINTER:
36397 error_at (OMP_CLAUSE_LOCATION (*pc),
36398 "%<#pragma omp target enter data%> with map-type other "
36399 "than %<to%> or %<alloc%> on %<map%> clause");
36400 *pc = OMP_CLAUSE_CHAIN (*pc);
36403 pc = &OMP_CLAUSE_CHAIN (*pc);
36409 error_at (pragma_tok->location,
36410 "%<#pragma omp target enter data%> must contain at least "
36411 "one %<map%> clause");
36415 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36416 TREE_TYPE (stmt) = void_type_node;
36417 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36418 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36419 return add_stmt (stmt);
36423 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36424 structured-block */
36426 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36427 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36434 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36435 enum pragma_context context)
36437 bool data_seen = false;
36438 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36440 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36441 const char *p = IDENTIFIER_POINTER (id);
36443 if (strcmp (p, "data") == 0)
36445 cp_lexer_consume_token (parser->lexer);
36451 cp_parser_error (parser, "expected %<data%>");
36452 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36456 if (context == pragma_stmt)
36458 error_at (pragma_tok->location,
36459 "%<#pragma %s%> may only be used in compound statements",
36460 "omp target exit data");
36461 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36466 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36467 "#pragma omp target exit data", pragma_tok);
36469 for (tree *pc = &clauses; *pc;)
36471 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36472 switch (OMP_CLAUSE_MAP_KIND (*pc))
36474 case GOMP_MAP_FROM:
36475 case GOMP_MAP_ALWAYS_FROM:
36476 case GOMP_MAP_RELEASE:
36477 case GOMP_MAP_DELETE:
36480 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36481 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36482 case GOMP_MAP_ALWAYS_POINTER:
36486 error_at (OMP_CLAUSE_LOCATION (*pc),
36487 "%<#pragma omp target exit data%> with map-type other "
36488 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36490 *pc = OMP_CLAUSE_CHAIN (*pc);
36493 pc = &OMP_CLAUSE_CHAIN (*pc);
36499 error_at (pragma_tok->location,
36500 "%<#pragma omp target exit data%> must contain at least "
36501 "one %<map%> clause");
36505 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36506 TREE_TYPE (stmt) = void_type_node;
36507 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36508 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36509 return add_stmt (stmt);
36513 # pragma omp target update target-update-clause[optseq] new-line */
36515 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36516 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36524 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36525 enum pragma_context context)
36527 if (context == pragma_stmt)
36529 error_at (pragma_tok->location,
36530 "%<#pragma %s%> may only be used in compound statements",
36531 "omp target update");
36532 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36537 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36538 "#pragma omp target update", pragma_tok);
36539 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36540 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36542 error_at (pragma_tok->location,
36543 "%<#pragma omp target update%> must contain at least one "
36544 "%<from%> or %<to%> clauses");
36548 tree stmt = make_node (OMP_TARGET_UPDATE);
36549 TREE_TYPE (stmt) = void_type_node;
36550 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36551 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36557 # pragma omp target target-clause[optseq] new-line
36558 structured-block */
36560 #define OMP_TARGET_CLAUSE_MASK \
36561 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36572 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36573 enum pragma_context context, bool *if_p)
36575 tree *pc = NULL, stmt;
36577 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36579 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36580 const char *p = IDENTIFIER_POINTER (id);
36581 enum tree_code ccode = ERROR_MARK;
36583 if (strcmp (p, "teams") == 0)
36585 else if (strcmp (p, "parallel") == 0)
36586 ccode = OMP_PARALLEL;
36587 else if (strcmp (p, "simd") == 0)
36589 if (ccode != ERROR_MARK)
36591 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36592 char p_name[sizeof ("#pragma omp target teams distribute "
36593 "parallel for simd")];
36595 cp_lexer_consume_token (parser->lexer);
36596 strcpy (p_name, "#pragma omp target");
36597 if (!flag_openmp) /* flag_openmp_simd */
36603 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36604 OMP_TARGET_CLAUSE_MASK,
36608 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36609 OMP_TARGET_CLAUSE_MASK,
36613 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36614 OMP_TARGET_CLAUSE_MASK,
36618 gcc_unreachable ();
36620 return stmt != NULL_TREE;
36622 keep_next_level (true);
36623 tree sb = begin_omp_structured_block (), ret;
36624 unsigned save = cp_parser_begin_omp_structured_block (parser);
36628 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36629 OMP_TARGET_CLAUSE_MASK, cclauses,
36633 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36634 OMP_TARGET_CLAUSE_MASK, cclauses,
36638 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36639 OMP_TARGET_CLAUSE_MASK, cclauses,
36643 gcc_unreachable ();
36645 cp_parser_end_omp_structured_block (parser, save);
36646 tree body = finish_omp_structured_block (sb);
36647 if (ret == NULL_TREE)
36649 if (ccode == OMP_TEAMS && !processing_template_decl)
36651 /* For combined target teams, ensure the num_teams and
36652 thread_limit clause expressions are evaluated on the host,
36653 before entering the target construct. */
36655 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36656 c; c = OMP_CLAUSE_CHAIN (c))
36657 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36658 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36659 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36661 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36662 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36663 if (expr == error_mark_node)
36665 tree tmp = TARGET_EXPR_SLOT (expr);
36667 OMP_CLAUSE_OPERAND (c, 0) = expr;
36668 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36669 OMP_CLAUSE_FIRSTPRIVATE);
36670 OMP_CLAUSE_DECL (tc) = tmp;
36671 OMP_CLAUSE_CHAIN (tc)
36672 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36673 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36676 tree stmt = make_node (OMP_TARGET);
36677 TREE_TYPE (stmt) = void_type_node;
36678 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36679 OMP_TARGET_BODY (stmt) = body;
36680 OMP_TARGET_COMBINED (stmt) = 1;
36681 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36683 pc = &OMP_TARGET_CLAUSES (stmt);
36684 goto check_clauses;
36686 else if (!flag_openmp) /* flag_openmp_simd */
36688 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36691 else if (strcmp (p, "data") == 0)
36693 cp_lexer_consume_token (parser->lexer);
36694 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36697 else if (strcmp (p, "enter") == 0)
36699 cp_lexer_consume_token (parser->lexer);
36700 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36703 else if (strcmp (p, "exit") == 0)
36705 cp_lexer_consume_token (parser->lexer);
36706 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36709 else if (strcmp (p, "update") == 0)
36711 cp_lexer_consume_token (parser->lexer);
36712 return cp_parser_omp_target_update (parser, pragma_tok, context);
36715 if (!flag_openmp) /* flag_openmp_simd */
36717 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36721 stmt = make_node (OMP_TARGET);
36722 TREE_TYPE (stmt) = void_type_node;
36724 OMP_TARGET_CLAUSES (stmt)
36725 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36726 "#pragma omp target", pragma_tok);
36727 pc = &OMP_TARGET_CLAUSES (stmt);
36728 keep_next_level (true);
36729 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36731 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36737 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36738 switch (OMP_CLAUSE_MAP_KIND (*pc))
36741 case GOMP_MAP_ALWAYS_TO:
36742 case GOMP_MAP_FROM:
36743 case GOMP_MAP_ALWAYS_FROM:
36744 case GOMP_MAP_TOFROM:
36745 case GOMP_MAP_ALWAYS_TOFROM:
36746 case GOMP_MAP_ALLOC:
36747 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36748 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36749 case GOMP_MAP_ALWAYS_POINTER:
36752 error_at (OMP_CLAUSE_LOCATION (*pc),
36753 "%<#pragma omp target%> with map-type other "
36754 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36755 "on %<map%> clause");
36756 *pc = OMP_CLAUSE_CHAIN (*pc);
36759 pc = &OMP_CLAUSE_CHAIN (*pc);
36765 # pragma acc cache (variable-list) new-line
36769 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36771 tree stmt, clauses;
36773 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36774 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36776 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36778 stmt = make_node (OACC_CACHE);
36779 TREE_TYPE (stmt) = void_type_node;
36780 OACC_CACHE_CLAUSES (stmt) = clauses;
36781 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36788 # pragma acc data oacc-data-clause[optseq] new-line
36789 structured-block */
36791 #define OACC_DATA_CLAUSE_MASK \
36792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36805 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36807 tree stmt, clauses, block;
36810 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36811 "#pragma acc data", pragma_tok);
36813 block = begin_omp_parallel ();
36814 save = cp_parser_begin_omp_structured_block (parser);
36815 cp_parser_statement (parser, NULL_TREE, false, if_p);
36816 cp_parser_end_omp_structured_block (parser, save);
36817 stmt = finish_oacc_data (clauses, block);
36822 # pragma acc host_data <clauses> new-line
36823 structured-block */
36825 #define OACC_HOST_DATA_CLAUSE_MASK \
36826 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36829 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36831 tree stmt, clauses, block;
36834 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36835 "#pragma acc host_data", pragma_tok);
36837 block = begin_omp_parallel ();
36838 save = cp_parser_begin_omp_structured_block (parser);
36839 cp_parser_statement (parser, NULL_TREE, false, if_p);
36840 cp_parser_end_omp_structured_block (parser, save);
36841 stmt = finish_oacc_host_data (clauses, block);
36846 # pragma acc declare oacc-data-clause[optseq] new-line
36849 #define OACC_DECLARE_CLAUSE_MASK \
36850 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36864 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36866 tree clauses, stmt;
36867 bool error = false;
36869 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36870 "#pragma acc declare", pragma_tok, true);
36873 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36875 error_at (pragma_tok->location,
36876 "no valid clauses specified in %<#pragma acc declare%>");
36880 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36882 location_t loc = OMP_CLAUSE_LOCATION (t);
36883 tree decl = OMP_CLAUSE_DECL (t);
36884 if (!DECL_P (decl))
36886 error_at (loc, "array section in %<#pragma acc declare%>");
36890 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36891 switch (OMP_CLAUSE_MAP_KIND (t))
36893 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36894 case GOMP_MAP_FORCE_ALLOC:
36895 case GOMP_MAP_FORCE_TO:
36896 case GOMP_MAP_FORCE_DEVICEPTR:
36897 case GOMP_MAP_DEVICE_RESIDENT:
36900 case GOMP_MAP_LINK:
36901 if (!global_bindings_p ()
36902 && (TREE_STATIC (decl)
36903 || !DECL_EXTERNAL (decl)))
36906 "%qD must be a global variable in "
36907 "%<#pragma acc declare link%>",
36915 if (global_bindings_p ())
36917 error_at (loc, "invalid OpenACC clause at file scope");
36921 if (DECL_EXTERNAL (decl))
36924 "invalid use of %<extern%> variable %qD "
36925 "in %<#pragma acc declare%>", decl);
36929 else if (TREE_PUBLIC (decl))
36932 "invalid use of %<global%> variable %qD "
36933 "in %<#pragma acc declare%>", decl);
36940 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36941 || lookup_attribute ("omp declare target link",
36942 DECL_ATTRIBUTES (decl)))
36944 error_at (loc, "variable %qD used more than once with "
36945 "%<#pragma acc declare%>", decl);
36954 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36955 id = get_identifier ("omp declare target link");
36957 id = get_identifier ("omp declare target");
36959 DECL_ATTRIBUTES (decl)
36960 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36961 if (global_bindings_p ())
36963 symtab_node *node = symtab_node::get (decl);
36966 node->offloadable = 1;
36967 if (ENABLE_OFFLOADING)
36969 g->have_offload = true;
36970 if (is_a <varpool_node *> (node))
36971 vec_safe_push (offload_vars, decl);
36978 if (error || global_bindings_p ())
36981 stmt = make_node (OACC_DECLARE);
36982 TREE_TYPE (stmt) = void_type_node;
36983 OACC_DECLARE_CLAUSES (stmt) = clauses;
36984 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36992 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36996 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36998 LOC is the location of the #pragma token.
37001 #define OACC_ENTER_DATA_CLAUSE_MASK \
37002 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37010 #define OACC_EXIT_DATA_CLAUSE_MASK \
37011 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37018 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37021 location_t loc = pragma_tok->location;
37022 tree stmt, clauses;
37023 const char *p = "";
37025 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37026 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37028 if (strcmp (p, "data") != 0)
37030 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37031 enter ? "enter" : "exit");
37032 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37036 cp_lexer_consume_token (parser->lexer);
37039 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37040 "#pragma acc enter data", pragma_tok);
37042 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37043 "#pragma acc exit data", pragma_tok);
37045 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37047 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37048 enter ? "enter" : "exit");
37052 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37053 TREE_TYPE (stmt) = void_type_node;
37054 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37055 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37061 # pragma acc loop oacc-loop-clause[optseq] new-line
37062 structured-block */
37064 #define OACC_LOOP_CLAUSE_MASK \
37065 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37077 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37078 omp_clause_mask mask, tree *cclauses, bool *if_p)
37080 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37082 strcat (p_name, " loop");
37083 mask |= OACC_LOOP_CLAUSE_MASK;
37085 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37089 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37091 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37093 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37096 tree block = begin_omp_structured_block ();
37097 int save = cp_parser_begin_omp_structured_block (parser);
37098 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37099 cp_parser_end_omp_structured_block (parser, save);
37100 add_stmt (finish_omp_structured_block (block));
37106 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37111 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37115 #define OACC_KERNELS_CLAUSE_MASK \
37116 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37134 #define OACC_PARALLEL_CLAUSE_MASK \
37135 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37157 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37158 char *p_name, bool *if_p)
37160 omp_clause_mask mask;
37161 enum tree_code code;
37162 switch (cp_parser_pragma_kind (pragma_tok))
37164 case PRAGMA_OACC_KERNELS:
37165 strcat (p_name, " kernels");
37166 mask = OACC_KERNELS_CLAUSE_MASK;
37167 code = OACC_KERNELS;
37169 case PRAGMA_OACC_PARALLEL:
37170 strcat (p_name, " parallel");
37171 mask = OACC_PARALLEL_CLAUSE_MASK;
37172 code = OACC_PARALLEL;
37175 gcc_unreachable ();
37178 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37181 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37182 if (strcmp (p, "loop") == 0)
37184 cp_lexer_consume_token (parser->lexer);
37185 tree block = begin_omp_parallel ();
37187 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37189 return finish_omp_construct (code, block, clauses);
37193 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37195 tree block = begin_omp_parallel ();
37196 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37197 cp_parser_statement (parser, NULL_TREE, false, if_p);
37198 cp_parser_end_omp_structured_block (parser, save);
37199 return finish_omp_construct (code, block, clauses);
37203 # pragma acc update oacc-update-clause[optseq] new-line
37206 #define OACC_UPDATE_CLAUSE_MASK \
37207 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37215 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37217 tree stmt, clauses;
37219 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37220 "#pragma acc update", pragma_tok);
37222 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37224 error_at (pragma_tok->location,
37225 "%<#pragma acc update%> must contain at least one "
37226 "%<device%> or %<host%> or %<self%> clause");
37230 stmt = make_node (OACC_UPDATE);
37231 TREE_TYPE (stmt) = void_type_node;
37232 OACC_UPDATE_CLAUSES (stmt) = clauses;
37233 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37239 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37241 LOC is the location of the #pragma token.
37244 #define OACC_WAIT_CLAUSE_MASK \
37245 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37248 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37250 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37251 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37253 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37254 list = cp_parser_oacc_wait_list (parser, loc, list);
37256 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37257 "#pragma acc wait", pragma_tok);
37259 stmt = c_finish_oacc_wait (loc, list, clauses);
37260 stmt = finish_expr_stmt (stmt);
37266 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37268 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37269 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37277 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37278 enum pragma_context context)
37280 bool first_p = parser->omp_declare_simd == NULL;
37281 cp_omp_declare_simd_data data;
37284 data.error_seen = false;
37285 data.fndecl_seen = false;
37286 data.tokens = vNULL;
37287 data.clauses = NULL_TREE;
37288 /* It is safe to take the address of a local variable; it will only be
37289 used while this scope is live. */
37290 parser->omp_declare_simd = &data;
37293 /* Store away all pragma tokens. */
37294 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37295 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37296 cp_lexer_consume_token (parser->lexer);
37297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37298 parser->omp_declare_simd->error_seen = true;
37299 cp_parser_require_pragma_eol (parser, pragma_tok);
37300 struct cp_token_cache *cp
37301 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37302 parser->omp_declare_simd->tokens.safe_push (cp);
37306 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37307 cp_parser_pragma (parser, context, NULL);
37310 case pragma_external:
37311 cp_parser_declaration (parser);
37313 case pragma_member:
37314 cp_parser_member_declaration (parser);
37316 case pragma_objc_icode:
37317 cp_parser_block_declaration (parser, /*statement_p=*/false);
37320 cp_parser_declaration_statement (parser);
37323 if (parser->omp_declare_simd
37324 && !parser->omp_declare_simd->error_seen
37325 && !parser->omp_declare_simd->fndecl_seen)
37326 error_at (pragma_tok->location,
37327 "%<#pragma omp declare simd%> not immediately followed by "
37328 "function declaration or definition");
37329 data.tokens.release ();
37330 parser->omp_declare_simd = NULL;
37334 /* Finalize #pragma omp declare simd clauses after direct declarator has
37335 been parsed, and put that into "omp declare simd" attribute. */
37338 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37340 struct cp_token_cache *ce;
37341 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37344 if (!data->error_seen && data->fndecl_seen)
37346 error ("%<#pragma omp declare simd%> not immediately followed by "
37347 "a single function declaration or definition");
37348 data->error_seen = true;
37350 if (data->error_seen)
37353 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37357 cp_parser_push_lexer_for_tokens (parser, ce);
37358 parser->lexer->in_pragma = true;
37359 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37360 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37361 cp_lexer_consume_token (parser->lexer);
37362 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37363 "#pragma omp declare simd", pragma_tok);
37364 cp_parser_pop_lexer (parser);
37366 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37367 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37368 TREE_CHAIN (c) = attrs;
37369 if (processing_template_decl)
37370 ATTR_IS_DEPENDENT (c) = 1;
37374 data->fndecl_seen = true;
37380 # pragma omp declare target new-line
37381 declarations and definitions
37382 # pragma omp end declare target new-line
37385 # pragma omp declare target ( extended-list ) new-line
37387 # pragma omp declare target declare-target-clauses[seq] new-line */
37389 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37390 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37394 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37396 tree clauses = NULL_TREE;
37397 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37399 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37400 "#pragma omp declare target", pragma_tok);
37401 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37403 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37405 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37406 cp_parser_require_pragma_eol (parser, pragma_tok);
37410 cp_parser_require_pragma_eol (parser, pragma_tok);
37411 scope_chain->omp_declare_target_attribute++;
37414 if (scope_chain->omp_declare_target_attribute)
37415 error_at (pragma_tok->location,
37416 "%<#pragma omp declare target%> with clauses in between "
37417 "%<#pragma omp declare target%> without clauses and "
37418 "%<#pragma omp end declare target%>");
37419 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37421 tree t = OMP_CLAUSE_DECL (c), id;
37422 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37423 tree at2 = lookup_attribute ("omp declare target link",
37424 DECL_ATTRIBUTES (t));
37425 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37427 id = get_identifier ("omp declare target link");
37428 std::swap (at1, at2);
37431 id = get_identifier ("omp declare target");
37434 error_at (OMP_CLAUSE_LOCATION (c),
37435 "%qD specified both in declare target %<link%> and %<to%>"
37441 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37442 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37445 symtab_node *node = symtab_node::get (t);
37448 node->offloadable = 1;
37449 if (ENABLE_OFFLOADING)
37451 g->have_offload = true;
37452 if (is_a <varpool_node *> (node))
37453 vec_safe_push (offload_vars, t);
37461 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37463 const char *p = "";
37464 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37466 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37467 p = IDENTIFIER_POINTER (id);
37469 if (strcmp (p, "declare") == 0)
37471 cp_lexer_consume_token (parser->lexer);
37473 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37475 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37476 p = IDENTIFIER_POINTER (id);
37478 if (strcmp (p, "target") == 0)
37479 cp_lexer_consume_token (parser->lexer);
37482 cp_parser_error (parser, "expected %<target%>");
37483 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37489 cp_parser_error (parser, "expected %<declare%>");
37490 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37493 cp_parser_require_pragma_eol (parser, pragma_tok);
37494 if (!scope_chain->omp_declare_target_attribute)
37495 error_at (pragma_tok->location,
37496 "%<#pragma omp end declare target%> without corresponding "
37497 "%<#pragma omp declare target%>");
37499 scope_chain->omp_declare_target_attribute--;
37502 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37503 expression and optional initializer clause of
37504 #pragma omp declare reduction. We store the expression(s) as
37505 either 3, 6 or 7 special statements inside of the artificial function's
37506 body. The first two statements are DECL_EXPRs for the artificial
37507 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37508 expression that uses those variables.
37509 If there was any INITIALIZER clause, this is followed by further statements,
37510 the fourth and fifth statements are DECL_EXPRs for the artificial
37511 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37512 constructor variant (first token after open paren is not omp_priv),
37513 then the sixth statement is a statement with the function call expression
37514 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37515 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37516 to initialize the OMP_PRIV artificial variable and there is seventh
37517 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37520 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37522 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37523 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37524 type = TREE_TYPE (type);
37525 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37526 DECL_ARTIFICIAL (omp_out) = 1;
37527 pushdecl (omp_out);
37528 add_decl_expr (omp_out);
37529 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37530 DECL_ARTIFICIAL (omp_in) = 1;
37532 add_decl_expr (omp_in);
37534 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37536 keep_next_level (true);
37537 tree block = begin_omp_structured_block ();
37538 combiner = cp_parser_expression (parser);
37539 finish_expr_stmt (combiner);
37540 block = finish_omp_structured_block (block);
37543 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37546 const char *p = "";
37547 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37549 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37550 p = IDENTIFIER_POINTER (id);
37553 if (strcmp (p, "initializer") == 0)
37555 cp_lexer_consume_token (parser->lexer);
37556 matching_parens parens;
37557 if (!parens.require_open (parser))
37561 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37563 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37564 p = IDENTIFIER_POINTER (id);
37567 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37568 DECL_ARTIFICIAL (omp_priv) = 1;
37569 pushdecl (omp_priv);
37570 add_decl_expr (omp_priv);
37571 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37572 DECL_ARTIFICIAL (omp_orig) = 1;
37573 pushdecl (omp_orig);
37574 add_decl_expr (omp_orig);
37576 keep_next_level (true);
37577 block = begin_omp_structured_block ();
37580 if (strcmp (p, "omp_priv") == 0)
37582 bool is_direct_init, is_non_constant_init;
37584 cp_lexer_consume_token (parser->lexer);
37585 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37586 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37587 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37588 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37590 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37591 == CPP_CLOSE_PAREN))
37593 finish_omp_structured_block (block);
37594 error ("invalid initializer clause");
37597 initializer = cp_parser_initializer (parser, &is_direct_init,
37598 &is_non_constant_init);
37599 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37600 NULL_TREE, LOOKUP_ONLYCONVERTING);
37604 cp_parser_parse_tentatively (parser);
37605 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37606 /*check_dependency_p=*/true,
37607 /*template_p=*/NULL,
37608 /*declarator_p=*/false,
37609 /*optional_p=*/false);
37610 vec<tree, va_gc> *args;
37611 if (fn_name == error_mark_node
37612 || cp_parser_error_occurred (parser)
37613 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37614 || ((args = cp_parser_parenthesized_expression_list
37615 (parser, non_attr, /*cast_p=*/false,
37616 /*allow_expansion_p=*/true,
37617 /*non_constant_p=*/NULL)),
37618 cp_parser_error_occurred (parser)))
37620 finish_omp_structured_block (block);
37621 cp_parser_abort_tentative_parse (parser);
37622 cp_parser_error (parser, "expected id-expression (arguments)");
37627 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37628 if (arg == omp_priv
37629 || (TREE_CODE (arg) == ADDR_EXPR
37630 && TREE_OPERAND (arg, 0) == omp_priv))
37632 cp_parser_abort_tentative_parse (parser);
37633 if (arg == NULL_TREE)
37634 error ("one of the initializer call arguments should be %<omp_priv%>"
37635 " or %<&omp_priv%>");
37636 initializer = cp_parser_postfix_expression (parser, false, false, false,
37638 finish_expr_stmt (initializer);
37641 block = finish_omp_structured_block (block);
37642 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37646 add_decl_expr (omp_orig);
37648 if (!parens.require_close (parser))
37652 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37653 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37660 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37661 initializer-clause[opt] new-line
37663 initializer-clause:
37664 initializer (omp_priv initializer)
37665 initializer (function-name (argument-list)) */
37668 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37669 enum pragma_context)
37671 auto_vec<tree> types;
37672 enum tree_code reduc_code = ERROR_MARK;
37673 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37675 cp_token *first_token;
37676 cp_token_cache *cp;
37680 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37681 p = obstack_alloc (&declarator_obstack, 0);
37683 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37686 switch (cp_lexer_peek_token (parser->lexer)->type)
37689 reduc_code = PLUS_EXPR;
37692 reduc_code = MULT_EXPR;
37695 reduc_code = MINUS_EXPR;
37698 reduc_code = BIT_AND_EXPR;
37701 reduc_code = BIT_XOR_EXPR;
37704 reduc_code = BIT_IOR_EXPR;
37707 reduc_code = TRUTH_ANDIF_EXPR;
37710 reduc_code = TRUTH_ORIF_EXPR;
37713 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37716 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37717 "%<|%>, %<&&%>, %<||%> or identifier");
37721 if (reduc_code != ERROR_MARK)
37722 cp_lexer_consume_token (parser->lexer);
37724 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37725 if (reduc_id == error_mark_node)
37728 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37731 /* Types may not be defined in declare reduction type list. */
37732 const char *saved_message;
37733 saved_message = parser->type_definition_forbidden_message;
37734 parser->type_definition_forbidden_message
37735 = G_("types may not be defined in declare reduction type list");
37736 bool saved_colon_corrects_to_scope_p;
37737 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37738 parser->colon_corrects_to_scope_p = false;
37739 bool saved_colon_doesnt_start_class_def_p;
37740 saved_colon_doesnt_start_class_def_p
37741 = parser->colon_doesnt_start_class_def_p;
37742 parser->colon_doesnt_start_class_def_p = true;
37746 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37747 type = cp_parser_type_id (parser);
37748 if (type == error_mark_node)
37750 else if (ARITHMETIC_TYPE_P (type)
37751 && (orig_reduc_id == NULL_TREE
37752 || (TREE_CODE (type) != COMPLEX_TYPE
37753 && (id_equal (orig_reduc_id, "min")
37754 || id_equal (orig_reduc_id, "max")))))
37755 error_at (loc, "predeclared arithmetic type %qT in "
37756 "%<#pragma omp declare reduction%>", type);
37757 else if (TREE_CODE (type) == FUNCTION_TYPE
37758 || TREE_CODE (type) == METHOD_TYPE
37759 || TREE_CODE (type) == ARRAY_TYPE)
37760 error_at (loc, "function or array type %qT in "
37761 "%<#pragma omp declare reduction%>", type);
37762 else if (TREE_CODE (type) == REFERENCE_TYPE)
37763 error_at (loc, "reference type %qT in "
37764 "%<#pragma omp declare reduction%>", type);
37765 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37766 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37767 "%<#pragma omp declare reduction%>", type);
37769 types.safe_push (type);
37771 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37772 cp_lexer_consume_token (parser->lexer);
37777 /* Restore the saved message. */
37778 parser->type_definition_forbidden_message = saved_message;
37779 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37780 parser->colon_doesnt_start_class_def_p
37781 = saved_colon_doesnt_start_class_def_p;
37783 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37784 || types.is_empty ())
37787 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37791 first_token = cp_lexer_peek_token (parser->lexer);
37794 FOR_EACH_VEC_ELT (types, i, type)
37797 = build_function_type_list (void_type_node,
37798 cp_build_reference_type (type, false),
37800 tree this_reduc_id = reduc_id;
37801 if (!dependent_type_p (type))
37802 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37803 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37804 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37805 DECL_ARTIFICIAL (fndecl) = 1;
37806 DECL_EXTERNAL (fndecl) = 1;
37807 DECL_DECLARED_INLINE_P (fndecl) = 1;
37808 DECL_IGNORED_P (fndecl) = 1;
37809 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37810 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37811 DECL_ATTRIBUTES (fndecl)
37812 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37813 DECL_ATTRIBUTES (fndecl));
37814 if (processing_template_decl)
37815 fndecl = push_template_decl (fndecl);
37816 bool block_scope = false;
37817 tree block = NULL_TREE;
37818 if (current_function_decl)
37820 block_scope = true;
37821 DECL_CONTEXT (fndecl) = global_namespace;
37822 if (!processing_template_decl)
37825 else if (current_class_type)
37829 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37830 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37831 cp_lexer_consume_token (parser->lexer);
37832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37834 cp = cp_token_cache_new (first_token,
37835 cp_lexer_peek_nth_token (parser->lexer,
37838 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37839 finish_member_declaration (fndecl);
37840 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37841 DECL_PENDING_INLINE_P (fndecl) = 1;
37842 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37847 DECL_CONTEXT (fndecl) = current_namespace;
37851 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37853 block = begin_omp_structured_block ();
37856 cp_parser_push_lexer_for_tokens (parser, cp);
37857 parser->lexer->in_pragma = true;
37859 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37862 finish_function (/*inline_p=*/false);
37864 DECL_CONTEXT (fndecl) = current_function_decl;
37866 cp_parser_pop_lexer (parser);
37870 cp_parser_pop_lexer (parser);
37872 finish_function (/*inline_p=*/false);
37875 DECL_CONTEXT (fndecl) = current_function_decl;
37876 block = finish_omp_structured_block (block);
37877 if (TREE_CODE (block) == BIND_EXPR)
37878 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37879 else if (TREE_CODE (block) == STATEMENT_LIST)
37880 DECL_SAVED_TREE (fndecl) = block;
37881 if (processing_template_decl)
37882 add_decl_expr (fndecl);
37884 cp_check_omp_declare_reduction (fndecl);
37885 if (cp == NULL && types.length () > 1)
37886 cp = cp_token_cache_new (first_token,
37887 cp_lexer_peek_nth_token (parser->lexer, 2));
37888 if (errs != errorcount)
37892 cp_parser_require_pragma_eol (parser, pragma_tok);
37895 /* Free any declarators allocated. */
37896 obstack_free (&declarator_obstack, p);
37900 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37901 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37902 initializer-clause[opt] new-line
37903 #pragma omp declare target new-line */
37906 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37907 enum pragma_context context)
37909 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37911 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37912 const char *p = IDENTIFIER_POINTER (id);
37914 if (strcmp (p, "simd") == 0)
37916 cp_lexer_consume_token (parser->lexer);
37917 cp_parser_omp_declare_simd (parser, pragma_tok,
37921 cp_ensure_no_omp_declare_simd (parser);
37922 if (strcmp (p, "reduction") == 0)
37924 cp_lexer_consume_token (parser->lexer);
37925 cp_parser_omp_declare_reduction (parser, pragma_tok,
37929 if (!flag_openmp) /* flag_openmp_simd */
37931 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37934 if (strcmp (p, "target") == 0)
37936 cp_lexer_consume_token (parser->lexer);
37937 cp_parser_omp_declare_target (parser, pragma_tok);
37941 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37943 cp_parser_require_pragma_eol (parser, pragma_tok);
37948 #pragma omp taskloop taskloop-clause[optseq] new-line
37951 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37954 #define OMP_TASKLOOP_CLAUSE_MASK \
37955 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37971 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37972 char *p_name, omp_clause_mask mask, tree *cclauses,
37975 tree clauses, sb, ret;
37977 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37979 strcat (p_name, " taskloop");
37980 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37984 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37985 const char *p = IDENTIFIER_POINTER (id);
37987 if (strcmp (p, "simd") == 0)
37989 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37990 if (cclauses == NULL)
37991 cclauses = cclauses_buf;
37993 cp_lexer_consume_token (parser->lexer);
37994 if (!flag_openmp) /* flag_openmp_simd */
37995 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37997 sb = begin_omp_structured_block ();
37998 save = cp_parser_begin_omp_structured_block (parser);
37999 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38001 cp_parser_end_omp_structured_block (parser, save);
38002 tree body = finish_omp_structured_block (sb);
38005 ret = make_node (OMP_TASKLOOP);
38006 TREE_TYPE (ret) = void_type_node;
38007 OMP_FOR_BODY (ret) = body;
38008 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38009 SET_EXPR_LOCATION (ret, loc);
38014 if (!flag_openmp) /* flag_openmp_simd */
38016 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38020 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38024 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38025 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38028 sb = begin_omp_structured_block ();
38029 save = cp_parser_begin_omp_structured_block (parser);
38031 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38034 cp_parser_end_omp_structured_block (parser, save);
38035 add_stmt (finish_omp_structured_block (sb));
38042 # pragma acc routine oacc-routine-clause[optseq] new-line
38043 function-definition
38045 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38048 #define OACC_ROUTINE_CLAUSE_MASK \
38049 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38055 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38056 component, which must resolve to a declared namespace-scope
38057 function. The clauses are either processed directly (for a named
38058 function), or defered until the immediatley following declaration
38062 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38063 enum pragma_context context)
38065 gcc_checking_assert (context == pragma_external);
38066 /* The checking for "another pragma following this one" in the "no optional
38067 '( name )'" case makes sure that we dont re-enter. */
38068 gcc_checking_assert (parser->oacc_routine == NULL);
38070 cp_oacc_routine_data data;
38071 data.error_seen = false;
38072 data.fndecl_seen = false;
38073 data.tokens = vNULL;
38074 data.clauses = NULL_TREE;
38075 data.loc = pragma_tok->location;
38076 /* It is safe to take the address of a local variable; it will only be
38077 used while this scope is live. */
38078 parser->oacc_routine = &data;
38080 /* Look for optional '( name )'. */
38081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38083 matching_parens parens;
38084 parens.consume_open (parser); /* '(' */
38086 /* We parse the name as an id-expression. If it resolves to
38087 anything other than a non-overloaded function at namespace
38088 scope, it's an error. */
38089 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38090 tree name = cp_parser_id_expression (parser,
38091 /*template_keyword_p=*/false,
38092 /*check_dependency_p=*/false,
38093 /*template_p=*/NULL,
38094 /*declarator_p=*/false,
38095 /*optional_p=*/false);
38096 tree decl = (identifier_p (name)
38097 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38099 if (name != error_mark_node && decl == error_mark_node)
38100 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38102 if (decl == error_mark_node
38103 || !parens.require_close (parser))
38105 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38106 parser->oacc_routine = NULL;
38111 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38112 "#pragma acc routine",
38113 cp_lexer_peek_token (parser->lexer));
38115 if (decl && is_overloaded_fn (decl)
38116 && (TREE_CODE (decl) != FUNCTION_DECL
38117 || DECL_FUNCTION_TEMPLATE_P (decl)))
38119 error_at (name_loc,
38120 "%<#pragma acc routine%> names a set of overloads");
38121 parser->oacc_routine = NULL;
38125 /* Perhaps we should use the same rule as declarations in different
38127 if (!DECL_NAMESPACE_SCOPE_P (decl))
38129 error_at (name_loc,
38130 "%qD does not refer to a namespace scope function", decl);
38131 parser->oacc_routine = NULL;
38135 if (TREE_CODE (decl) != FUNCTION_DECL)
38137 error_at (name_loc, "%qD does not refer to a function", decl);
38138 parser->oacc_routine = NULL;
38142 cp_finalize_oacc_routine (parser, decl, false);
38143 parser->oacc_routine = NULL;
38145 else /* No optional '( name )'. */
38147 /* Store away all pragma tokens. */
38148 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38149 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38150 cp_lexer_consume_token (parser->lexer);
38151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38152 parser->oacc_routine->error_seen = true;
38153 cp_parser_require_pragma_eol (parser, pragma_tok);
38154 struct cp_token_cache *cp
38155 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38156 parser->oacc_routine->tokens.safe_push (cp);
38158 /* Emit a helpful diagnostic if there's another pragma following this
38160 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38162 cp_ensure_no_oacc_routine (parser);
38163 data.tokens.release ();
38164 /* ..., and then just keep going. */
38168 /* We only have to consider the pragma_external case here. */
38169 cp_parser_declaration (parser);
38170 if (parser->oacc_routine
38171 && !parser->oacc_routine->fndecl_seen)
38172 cp_ensure_no_oacc_routine (parser);
38174 parser->oacc_routine = NULL;
38175 data.tokens.release ();
38179 /* Finalize #pragma acc routine clauses after direct declarator has
38183 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38185 struct cp_token_cache *ce;
38186 cp_oacc_routine_data *data = parser->oacc_routine;
38188 if (!data->error_seen && data->fndecl_seen)
38190 error_at (data->loc,
38191 "%<#pragma acc routine%> not immediately followed by "
38192 "a single function declaration or definition");
38193 data->error_seen = true;
38195 if (data->error_seen)
38198 gcc_checking_assert (data->tokens.length () == 1);
38199 ce = data->tokens[0];
38201 cp_parser_push_lexer_for_tokens (parser, ce);
38202 parser->lexer->in_pragma = true;
38203 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38205 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38206 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38207 parser->oacc_routine->clauses
38208 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38209 "#pragma acc routine", pragma_tok);
38210 cp_parser_pop_lexer (parser);
38211 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38217 /* Apply any saved OpenACC routine clauses to a just-parsed
38221 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38223 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38225 /* Keep going if we're in error reporting mode. */
38226 if (parser->oacc_routine->error_seen
38227 || fndecl == error_mark_node)
38230 if (parser->oacc_routine->fndecl_seen)
38232 error_at (parser->oacc_routine->loc,
38233 "%<#pragma acc routine%> not immediately followed by"
38234 " a single function declaration or definition");
38235 parser->oacc_routine = NULL;
38238 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38240 cp_ensure_no_oacc_routine (parser);
38244 if (oacc_get_fn_attrib (fndecl))
38246 error_at (parser->oacc_routine->loc,
38247 "%<#pragma acc routine%> already applied to %qD", fndecl);
38248 parser->oacc_routine = NULL;
38252 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38254 error_at (parser->oacc_routine->loc,
38256 ? G_("%<#pragma acc routine%> must be applied before use")
38257 : G_("%<#pragma acc routine%> must be applied before "
38259 parser->oacc_routine = NULL;
38263 /* Process the routine's dimension clauses. */
38264 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38265 oacc_replace_fn_attrib (fndecl, dims);
38267 /* Add an "omp declare target" attribute. */
38268 DECL_ATTRIBUTES (fndecl)
38269 = tree_cons (get_identifier ("omp declare target"),
38270 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38272 /* Don't unset parser->oacc_routine here: we may still need it to
38273 diagnose wrong usage. But, remember that we've used this "#pragma acc
38275 parser->oacc_routine->fndecl_seen = true;
38279 /* Main entry point to OpenMP statement pragmas. */
38282 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38285 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38286 omp_clause_mask mask (0);
38288 switch (cp_parser_pragma_kind (pragma_tok))
38290 case PRAGMA_OACC_ATOMIC:
38291 cp_parser_omp_atomic (parser, pragma_tok);
38293 case PRAGMA_OACC_CACHE:
38294 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38296 case PRAGMA_OACC_DATA:
38297 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38299 case PRAGMA_OACC_ENTER_DATA:
38300 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38302 case PRAGMA_OACC_EXIT_DATA:
38303 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38305 case PRAGMA_OACC_HOST_DATA:
38306 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38308 case PRAGMA_OACC_KERNELS:
38309 case PRAGMA_OACC_PARALLEL:
38310 strcpy (p_name, "#pragma acc");
38311 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38314 case PRAGMA_OACC_LOOP:
38315 strcpy (p_name, "#pragma acc");
38316 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38319 case PRAGMA_OACC_UPDATE:
38320 stmt = cp_parser_oacc_update (parser, pragma_tok);
38322 case PRAGMA_OACC_WAIT:
38323 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38325 case PRAGMA_OMP_ATOMIC:
38326 cp_parser_omp_atomic (parser, pragma_tok);
38328 case PRAGMA_OMP_CRITICAL:
38329 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38331 case PRAGMA_OMP_DISTRIBUTE:
38332 strcpy (p_name, "#pragma omp");
38333 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38336 case PRAGMA_OMP_FOR:
38337 strcpy (p_name, "#pragma omp");
38338 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38341 case PRAGMA_OMP_MASTER:
38342 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38344 case PRAGMA_OMP_PARALLEL:
38345 strcpy (p_name, "#pragma omp");
38346 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38349 case PRAGMA_OMP_SECTIONS:
38350 strcpy (p_name, "#pragma omp");
38351 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38353 case PRAGMA_OMP_SIMD:
38354 strcpy (p_name, "#pragma omp");
38355 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38358 case PRAGMA_OMP_SINGLE:
38359 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38361 case PRAGMA_OMP_TASK:
38362 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38364 case PRAGMA_OMP_TASKGROUP:
38365 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38367 case PRAGMA_OMP_TASKLOOP:
38368 strcpy (p_name, "#pragma omp");
38369 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38372 case PRAGMA_OMP_TEAMS:
38373 strcpy (p_name, "#pragma omp");
38374 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38378 gcc_unreachable ();
38381 protected_set_expr_location (stmt, pragma_tok->location);
38384 /* Transactional Memory parsing routines. */
38386 /* Parse a transaction attribute.
38392 We use this instead of cp_parser_attributes_opt for transactions to avoid
38393 the pedwarn in C++98 mode. */
38396 cp_parser_txn_attribute_opt (cp_parser *parser)
38399 tree attr_name, attr = NULL;
38401 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38402 return cp_parser_attributes_opt (parser);
38404 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38406 cp_lexer_consume_token (parser->lexer);
38407 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38410 token = cp_lexer_peek_token (parser->lexer);
38411 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38413 token = cp_lexer_consume_token (parser->lexer);
38415 attr_name = (token->type == CPP_KEYWORD
38416 /* For keywords, use the canonical spelling,
38417 not the parsed identifier. */
38418 ? ridpointers[(int) token->keyword]
38420 attr = build_tree_list (attr_name, NULL_TREE);
38423 cp_parser_error (parser, "expected identifier");
38425 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38427 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38431 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38433 transaction-statement:
38434 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38436 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38440 cp_parser_transaction (cp_parser *parser, cp_token *token)
38442 unsigned char old_in = parser->in_transaction;
38443 unsigned char this_in = 1, new_in;
38444 enum rid keyword = token->keyword;
38445 tree stmt, attrs, noex;
38447 cp_lexer_consume_token (parser->lexer);
38449 if (keyword == RID_TRANSACTION_RELAXED
38450 || keyword == RID_SYNCHRONIZED)
38451 this_in |= TM_STMT_ATTR_RELAXED;
38454 attrs = cp_parser_txn_attribute_opt (parser);
38456 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38459 /* Parse a noexcept specification. */
38460 if (keyword == RID_ATOMIC_NOEXCEPT)
38461 noex = boolean_true_node;
38462 else if (keyword == RID_ATOMIC_CANCEL)
38464 /* cancel-and-throw is unimplemented. */
38465 sorry ("atomic_cancel");
38469 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38471 /* Keep track if we're in the lexical scope of an outer transaction. */
38472 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38474 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38476 parser->in_transaction = new_in;
38477 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38478 parser->in_transaction = old_in;
38480 finish_transaction_stmt (stmt, NULL, this_in, noex);
38485 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38487 transaction-expression:
38488 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38489 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38493 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38495 unsigned char old_in = parser->in_transaction;
38496 unsigned char this_in = 1;
38500 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38502 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38503 || keyword == RID_TRANSACTION_RELAXED);
38507 keyword == RID_TRANSACTION_RELAXED
38508 ? G_("%<__transaction_relaxed%> without transactional memory "
38510 : G_("%<__transaction_atomic%> without transactional memory "
38511 "support enabled"));
38513 token = cp_parser_require_keyword (parser, keyword,
38514 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38515 : RT_TRANSACTION_RELAXED));
38516 gcc_assert (token != NULL);
38518 if (keyword == RID_TRANSACTION_RELAXED)
38519 this_in |= TM_STMT_ATTR_RELAXED;
38521 /* Set this early. This might mean that we allow transaction_cancel in
38522 an expression that we find out later actually has to be a constexpr.
38523 However, we expect that cxx_constant_value will be able to deal with
38524 this; also, if the noexcept has no constexpr, then what we parse next
38525 really is a transaction's body. */
38526 parser->in_transaction = this_in;
38528 /* Parse a noexcept specification. */
38529 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38532 if (!noex || !noex_expr
38533 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38535 matching_parens parens;
38536 parens.require_open (parser);
38538 expr = cp_parser_expression (parser);
38539 expr = finish_parenthesized_expr (expr);
38541 parens.require_close (parser);
38545 /* The only expression that is available got parsed for the noexcept
38546 already. noexcept is true then. */
38548 noex = boolean_true_node;
38551 expr = build_transaction_expr (token->location, expr, this_in, noex);
38552 parser->in_transaction = old_in;
38554 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38555 return error_mark_node;
38557 return (flag_tm ? expr : error_mark_node);
38560 /* Parse a function-transaction-block.
38562 function-transaction-block:
38563 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38565 __transaction_atomic txn-attribute[opt] function-try-block
38566 __transaction_relaxed ctor-initializer[opt] function-body
38567 __transaction_relaxed function-try-block
38571 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38573 unsigned char old_in = parser->in_transaction;
38574 unsigned char new_in = 1;
38575 tree compound_stmt, stmt, attrs;
38578 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38579 || keyword == RID_TRANSACTION_RELAXED);
38580 token = cp_parser_require_keyword (parser, keyword,
38581 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38582 : RT_TRANSACTION_RELAXED));
38583 gcc_assert (token != NULL);
38585 if (keyword == RID_TRANSACTION_RELAXED)
38586 new_in |= TM_STMT_ATTR_RELAXED;
38589 attrs = cp_parser_txn_attribute_opt (parser);
38591 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38594 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38596 parser->in_transaction = new_in;
38598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38599 cp_parser_function_try_block (parser);
38601 cp_parser_ctor_initializer_opt_and_function_body
38602 (parser, /*in_function_try_block=*/false);
38604 parser->in_transaction = old_in;
38606 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38609 /* Parse a __transaction_cancel statement.
38612 __transaction_cancel txn-attribute[opt] ;
38613 __transaction_cancel txn-attribute[opt] throw-expression ;
38615 ??? Cancel and throw is not yet implemented. */
38618 cp_parser_transaction_cancel (cp_parser *parser)
38621 bool is_outer = false;
38624 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38625 RT_TRANSACTION_CANCEL);
38626 gcc_assert (token != NULL);
38628 attrs = cp_parser_txn_attribute_opt (parser);
38630 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38632 /* ??? Parse cancel-and-throw here. */
38634 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38638 error_at (token->location, "%<__transaction_cancel%> without "
38639 "transactional memory support enabled");
38640 return error_mark_node;
38642 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38644 error_at (token->location, "%<__transaction_cancel%> within a "
38645 "%<__transaction_relaxed%>");
38646 return error_mark_node;
38650 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38651 && !is_tm_may_cancel_outer (current_function_decl))
38653 error_at (token->location, "outer %<__transaction_cancel%> not "
38654 "within outer %<__transaction_atomic%>");
38655 error_at (token->location,
38656 " or a %<transaction_may_cancel_outer%> function");
38657 return error_mark_node;
38660 else if (parser->in_transaction == 0)
38662 error_at (token->location, "%<__transaction_cancel%> not within "
38663 "%<__transaction_atomic%>");
38664 return error_mark_node;
38667 stmt = build_tm_abort_call (token->location, is_outer);
38675 static GTY (()) cp_parser *the_parser;
38678 /* Special handling for the first token or line in the file. The first
38679 thing in the file might be #pragma GCC pch_preprocess, which loads a
38680 PCH file, which is a GC collection point. So we need to handle this
38681 first pragma without benefit of an existing lexer structure.
38683 Always returns one token to the caller in *FIRST_TOKEN. This is
38684 either the true first token of the file, or the first token after
38685 the initial pragma. */
38688 cp_parser_initial_pragma (cp_token *first_token)
38692 cp_lexer_get_preprocessor_token (NULL, first_token);
38693 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38696 cp_lexer_get_preprocessor_token (NULL, first_token);
38697 if (first_token->type == CPP_STRING)
38699 name = first_token->u.value;
38701 cp_lexer_get_preprocessor_token (NULL, first_token);
38702 if (first_token->type != CPP_PRAGMA_EOL)
38703 error_at (first_token->location,
38704 "junk at end of %<#pragma GCC pch_preprocess%>");
38707 error_at (first_token->location, "expected string literal");
38709 /* Skip to the end of the pragma. */
38710 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38711 cp_lexer_get_preprocessor_token (NULL, first_token);
38713 /* Now actually load the PCH file. */
38715 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38717 /* Read one more token to return to our caller. We have to do this
38718 after reading the PCH file in, since its pointers have to be
38720 cp_lexer_get_preprocessor_token (NULL, first_token);
38723 /* Parse a pragma GCC ivdep. */
38726 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38728 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38732 /* Parse a pragma GCC unroll. */
38734 static unsigned short
38735 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38737 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38738 tree expr = cp_parser_constant_expression (parser);
38739 unsigned short unroll;
38740 expr = maybe_constant_value (expr);
38741 HOST_WIDE_INT lunroll = 0;
38742 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38743 || TREE_CODE (expr) != INTEGER_CST
38744 || (lunroll = tree_to_shwi (expr)) < 0
38745 || lunroll >= USHRT_MAX)
38747 error_at (location, "%<#pragma GCC unroll%> requires an"
38748 " assignment-expression that evaluates to a non-negative"
38749 " integral constant less than %u", USHRT_MAX);
38754 unroll = (unsigned short)lunroll;
38758 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38762 /* Normal parsing of a pragma token. Here we can (and must) use the
38766 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38768 cp_token *pragma_tok;
38773 pragma_tok = cp_lexer_consume_token (parser->lexer);
38774 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38775 parser->lexer->in_pragma = true;
38777 id = cp_parser_pragma_kind (pragma_tok);
38778 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38779 cp_ensure_no_omp_declare_simd (parser);
38782 case PRAGMA_GCC_PCH_PREPROCESS:
38783 error_at (pragma_tok->location,
38784 "%<#pragma GCC pch_preprocess%> must be first");
38787 case PRAGMA_OMP_BARRIER:
38790 case pragma_compound:
38791 cp_parser_omp_barrier (parser, pragma_tok);
38794 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38795 "used in compound statements", "omp barrier");
38802 case PRAGMA_OMP_FLUSH:
38805 case pragma_compound:
38806 cp_parser_omp_flush (parser, pragma_tok);
38809 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38810 "used in compound statements", "omp flush");
38817 case PRAGMA_OMP_TASKWAIT:
38820 case pragma_compound:
38821 cp_parser_omp_taskwait (parser, pragma_tok);
38824 error_at (pragma_tok->location,
38825 "%<#pragma %s%> may only be used in compound statements",
38833 case PRAGMA_OMP_TASKYIELD:
38836 case pragma_compound:
38837 cp_parser_omp_taskyield (parser, pragma_tok);
38840 error_at (pragma_tok->location,
38841 "%<#pragma %s%> may only be used in compound statements",
38849 case PRAGMA_OMP_CANCEL:
38852 case pragma_compound:
38853 cp_parser_omp_cancel (parser, pragma_tok);
38856 error_at (pragma_tok->location,
38857 "%<#pragma %s%> may only be used in compound statements",
38865 case PRAGMA_OMP_CANCELLATION_POINT:
38866 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38869 case PRAGMA_OMP_THREADPRIVATE:
38870 cp_parser_omp_threadprivate (parser, pragma_tok);
38873 case PRAGMA_OMP_DECLARE:
38874 return cp_parser_omp_declare (parser, pragma_tok, context);
38876 case PRAGMA_OACC_DECLARE:
38877 cp_parser_oacc_declare (parser, pragma_tok);
38880 case PRAGMA_OACC_ENTER_DATA:
38881 if (context == pragma_stmt)
38883 error_at (pragma_tok->location,
38884 "%<#pragma %s%> may only be used in compound statements",
38888 else if (context != pragma_compound)
38890 cp_parser_omp_construct (parser, pragma_tok, if_p);
38893 case PRAGMA_OACC_EXIT_DATA:
38894 if (context == pragma_stmt)
38896 error_at (pragma_tok->location,
38897 "%<#pragma %s%> may only be used in compound statements",
38901 else if (context != pragma_compound)
38903 cp_parser_omp_construct (parser, pragma_tok, if_p);
38906 case PRAGMA_OACC_ROUTINE:
38907 if (context != pragma_external)
38909 error_at (pragma_tok->location,
38910 "%<#pragma acc routine%> must be at file scope");
38913 cp_parser_oacc_routine (parser, pragma_tok, context);
38916 case PRAGMA_OACC_UPDATE:
38917 if (context == pragma_stmt)
38919 error_at (pragma_tok->location,
38920 "%<#pragma %s%> may only be used in compound statements",
38924 else if (context != pragma_compound)
38926 cp_parser_omp_construct (parser, pragma_tok, if_p);
38929 case PRAGMA_OACC_WAIT:
38930 if (context == pragma_stmt)
38932 error_at (pragma_tok->location,
38933 "%<#pragma %s%> may only be used in compound statements",
38937 else if (context != pragma_compound)
38939 cp_parser_omp_construct (parser, pragma_tok, if_p);
38942 case PRAGMA_OACC_ATOMIC:
38943 case PRAGMA_OACC_CACHE:
38944 case PRAGMA_OACC_DATA:
38945 case PRAGMA_OACC_HOST_DATA:
38946 case PRAGMA_OACC_KERNELS:
38947 case PRAGMA_OACC_PARALLEL:
38948 case PRAGMA_OACC_LOOP:
38949 case PRAGMA_OMP_ATOMIC:
38950 case PRAGMA_OMP_CRITICAL:
38951 case PRAGMA_OMP_DISTRIBUTE:
38952 case PRAGMA_OMP_FOR:
38953 case PRAGMA_OMP_MASTER:
38954 case PRAGMA_OMP_PARALLEL:
38955 case PRAGMA_OMP_SECTIONS:
38956 case PRAGMA_OMP_SIMD:
38957 case PRAGMA_OMP_SINGLE:
38958 case PRAGMA_OMP_TASK:
38959 case PRAGMA_OMP_TASKGROUP:
38960 case PRAGMA_OMP_TASKLOOP:
38961 case PRAGMA_OMP_TEAMS:
38962 if (context != pragma_stmt && context != pragma_compound)
38964 stmt = push_omp_privatization_clauses (false);
38965 cp_parser_omp_construct (parser, pragma_tok, if_p);
38966 pop_omp_privatization_clauses (stmt);
38969 case PRAGMA_OMP_ORDERED:
38970 if (context != pragma_stmt && context != pragma_compound)
38972 stmt = push_omp_privatization_clauses (false);
38973 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38974 pop_omp_privatization_clauses (stmt);
38977 case PRAGMA_OMP_TARGET:
38978 if (context != pragma_stmt && context != pragma_compound)
38980 stmt = push_omp_privatization_clauses (false);
38981 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38982 pop_omp_privatization_clauses (stmt);
38985 case PRAGMA_OMP_END_DECLARE_TARGET:
38986 cp_parser_omp_end_declare_target (parser, pragma_tok);
38989 case PRAGMA_OMP_SECTION:
38990 error_at (pragma_tok->location,
38991 "%<#pragma omp section%> may only be used in "
38992 "%<#pragma omp sections%> construct");
38997 if (context == pragma_external)
38999 error_at (pragma_tok->location,
39000 "%<#pragma GCC ivdep%> must be inside a function");
39003 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39004 unsigned short unroll;
39005 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39006 if (tok->type == CPP_PRAGMA
39007 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39009 tok = cp_lexer_consume_token (parser->lexer);
39010 unroll = cp_parser_pragma_unroll (parser, tok);
39011 tok = cp_lexer_peek_token (the_parser->lexer);
39015 if (tok->type != CPP_KEYWORD
39016 || (tok->keyword != RID_FOR
39017 && tok->keyword != RID_WHILE
39018 && tok->keyword != RID_DO))
39020 cp_parser_error (parser, "for, while or do statement expected");
39023 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39027 case PRAGMA_UNROLL:
39029 if (context == pragma_external)
39031 error_at (pragma_tok->location,
39032 "%<#pragma GCC unroll%> must be inside a function");
39035 const unsigned short unroll
39036 = cp_parser_pragma_unroll (parser, pragma_tok);
39038 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39039 if (tok->type == CPP_PRAGMA
39040 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39042 tok = cp_lexer_consume_token (parser->lexer);
39043 ivdep = cp_parser_pragma_ivdep (parser, tok);
39044 tok = cp_lexer_peek_token (the_parser->lexer);
39048 if (tok->type != CPP_KEYWORD
39049 || (tok->keyword != RID_FOR
39050 && tok->keyword != RID_WHILE
39051 && tok->keyword != RID_DO))
39053 cp_parser_error (parser, "for, while or do statement expected");
39056 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39061 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39062 c_invoke_pragma_handler (id);
39066 cp_parser_error (parser, "expected declaration specifiers");
39070 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39074 /* The interface the pragma parsers have to the lexer. */
39077 pragma_lex (tree *value, location_t *loc)
39079 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39080 enum cpp_ttype ret = tok->type;
39082 *value = tok->u.value;
39084 *loc = tok->location;
39086 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39088 else if (ret == CPP_STRING)
39089 *value = cp_parser_string_literal (the_parser, false, false);
39092 if (ret == CPP_KEYWORD)
39094 cp_lexer_consume_token (the_parser->lexer);
39101 /* External interface. */
39103 /* Parse one entire translation unit. */
39106 c_parse_file (void)
39108 static bool already_called = false;
39110 if (already_called)
39111 fatal_error (input_location,
39112 "inter-module optimizations not implemented for C++");
39113 already_called = true;
39115 the_parser = cp_parser_new ();
39116 push_deferring_access_checks (flag_access_control
39117 ? dk_no_deferred : dk_no_check);
39118 cp_parser_translation_unit (the_parser);
39122 /* Create an identifier for a generic parameter type (a synthesized
39123 template parameter implied by `auto' or a concept identifier). */
39125 static GTY(()) int generic_parm_count;
39127 make_generic_type_name ()
39130 sprintf (buf, "auto:%d", ++generic_parm_count);
39131 return get_identifier (buf);
39134 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39135 (creating a new template parameter list if necessary). Returns the newly
39136 created template type parm. */
39139 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39141 gcc_assert (current_binding_level->kind == sk_function_parms);
39143 /* Before committing to modifying any scope, if we're in an
39144 implicit template scope, and we're trying to synthesize a
39145 constrained parameter, try to find a previous parameter with
39146 the same name. This is the same-type rule for abbreviated
39147 function templates.
39149 NOTE: We can generate implicit parameters when tentatively
39150 parsing a nested name specifier, only to reject that parse
39151 later. However, matching the same template-id as part of a
39152 direct-declarator should generate an identical template
39153 parameter, so this rule will merge them. */
39154 if (parser->implicit_template_scope && constr)
39156 tree t = parser->implicit_template_parms;
39159 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39161 tree d = TREE_VALUE (t);
39162 if (TREE_CODE (d) == PARM_DECL)
39163 /* Return the TEMPLATE_PARM_INDEX. */
39164 d = DECL_INITIAL (d);
39167 t = TREE_CHAIN (t);
39171 /* We are either continuing a function template that already contains implicit
39172 template parameters, creating a new fully-implicit function template, or
39173 extending an existing explicit function template with implicit template
39176 cp_binding_level *const entry_scope = current_binding_level;
39178 bool become_template = false;
39179 cp_binding_level *parent_scope = 0;
39181 if (parser->implicit_template_scope)
39183 gcc_assert (parser->implicit_template_parms);
39185 current_binding_level = parser->implicit_template_scope;
39189 /* Roll back to the existing template parameter scope (in the case of
39190 extending an explicit function template) or introduce a new template
39191 parameter scope ahead of the function parameter scope (or class scope
39192 in the case of out-of-line member definitions). The function scope is
39193 added back after template parameter synthesis below. */
39195 cp_binding_level *scope = entry_scope;
39197 while (scope->kind == sk_function_parms)
39199 parent_scope = scope;
39200 scope = scope->level_chain;
39202 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39204 /* If not defining a class, then any class scope is a scope level in
39205 an out-of-line member definition. In this case simply wind back
39206 beyond the first such scope to inject the template parameter list.
39207 Otherwise wind back to the class being defined. The latter can
39208 occur in class member friend declarations such as:
39214 friend void A::foo (auto);
39217 The template parameter list synthesized for the friend declaration
39218 must be injected in the scope of 'B'. This can also occur in
39219 erroneous cases such as:
39225 void B::foo (auto) {}
39228 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39229 but, nevertheless, the template parameter list synthesized for the
39230 declarator should be injected into the scope of 'A' as if the
39231 ill-formed template was specified explicitly. */
39233 while (scope->kind == sk_class && !scope->defining_class_p)
39235 parent_scope = scope;
39236 scope = scope->level_chain;
39240 current_binding_level = scope;
39242 if (scope->kind != sk_template_parms
39243 || !function_being_declared_is_template_p (parser))
39245 /* Introduce a new template parameter list for implicit template
39248 become_template = true;
39250 parser->implicit_template_scope
39251 = begin_scope (sk_template_parms, NULL);
39253 ++processing_template_decl;
39255 parser->fully_implicit_function_template_p = true;
39256 ++parser->num_template_parameter_lists;
39260 /* Synthesize implicit template parameters at the end of the explicit
39261 template parameter list. */
39263 gcc_assert (current_template_parms);
39265 parser->implicit_template_scope = scope;
39267 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39268 parser->implicit_template_parms
39269 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39273 /* Synthesize a new template parameter and track the current template
39274 parameter chain with implicit_template_parms. */
39276 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39277 tree synth_id = make_generic_type_name ();
39278 tree synth_tmpl_parm;
39279 bool non_type = false;
39281 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39283 = finish_template_type_parm (class_type_node, synth_id);
39284 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39286 = finish_constrained_template_template_parm (proto, synth_id);
39289 synth_tmpl_parm = copy_decl (proto);
39290 DECL_NAME (synth_tmpl_parm) = synth_id;
39294 // Attach the constraint to the parm before processing.
39295 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39296 TREE_TYPE (node) = constr;
39298 = process_template_parm (parser->implicit_template_parms,
39301 /*non_type=*/non_type,
39302 /*param_pack=*/false);
39304 // Chain the new parameter to the list of implicit parameters.
39305 if (parser->implicit_template_parms)
39306 parser->implicit_template_parms
39307 = TREE_CHAIN (parser->implicit_template_parms);
39309 parser->implicit_template_parms = new_parm;
39311 tree new_decl = get_local_decls ();
39313 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39314 new_decl = DECL_INITIAL (new_decl);
39316 /* If creating a fully implicit function template, start the new implicit
39317 template parameter list with this synthesized type, otherwise grow the
39318 current template parameter list. */
39320 if (become_template)
39322 parent_scope->level_chain = current_binding_level;
39324 tree new_parms = make_tree_vec (1);
39325 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39326 current_template_parms = tree_cons (size_int (processing_template_decl),
39327 new_parms, current_template_parms);
39331 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39332 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39333 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39334 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39337 // If the new parameter was constrained, we need to add that to the
39338 // constraints in the template parameter list.
39339 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39341 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39342 reqs = conjoin_constraints (reqs, req);
39343 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39346 current_binding_level = entry_scope;
39351 /* Finish the declaration of a fully implicit function template. Such a
39352 template has no explicit template parameter list so has not been through the
39353 normal template head and tail processing. synthesize_implicit_template_parm
39354 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39355 provided if the declaration is a class member such that its template
39356 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39357 form is returned. Otherwise NULL_TREE is returned. */
39360 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39362 gcc_assert (parser->fully_implicit_function_template_p);
39364 if (member_decl_opt && member_decl_opt != error_mark_node
39365 && DECL_VIRTUAL_P (member_decl_opt))
39367 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39368 "implicit templates may not be %<virtual%>");
39369 DECL_VIRTUAL_P (member_decl_opt) = false;
39372 if (member_decl_opt)
39373 member_decl_opt = finish_member_template_decl (member_decl_opt);
39374 end_template_decl ();
39376 parser->fully_implicit_function_template_p = false;
39377 parser->implicit_template_parms = 0;
39378 parser->implicit_template_scope = 0;
39379 --parser->num_template_parameter_lists;
39381 return member_decl_opt;
39384 /* Like finish_fully_implicit_template, but to be used in error
39385 recovery, rearranging scopes so that we restore the state we had
39386 before synthesize_implicit_template_parm inserted the implement
39387 template parms scope. */
39390 abort_fully_implicit_template (cp_parser *parser)
39392 cp_binding_level *return_to_scope = current_binding_level;
39394 if (parser->implicit_template_scope
39395 && return_to_scope != parser->implicit_template_scope)
39397 cp_binding_level *child = return_to_scope;
39398 for (cp_binding_level *scope = child->level_chain;
39399 scope != parser->implicit_template_scope;
39400 scope = child->level_chain)
39402 child->level_chain = parser->implicit_template_scope->level_chain;
39403 parser->implicit_template_scope->level_chain = return_to_scope;
39404 current_binding_level = parser->implicit_template_scope;
39407 return_to_scope = return_to_scope->level_chain;
39409 finish_fully_implicit_template (parser, NULL);
39411 gcc_assert (current_binding_level == return_to_scope);
39414 /* Helper function for diagnostics that have complained about things
39415 being used with 'extern "C"' linkage.
39417 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39420 maybe_show_extern_c_location (void)
39422 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39423 inform (the_parser->innermost_linkage_specification_location,
39424 "%<extern \"C\"%> linkage started here");
39427 #include "gt-cp-parser.h"