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;
19129 if (cp_parser_allow_gnu_extensions_p (parser) && parser->in_function_body)
19132 cp_token *token = cp_lexer_peek_token (parser->lexer);
19133 location_t loc = token->location;
19134 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19139 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19140 inform (volatile_loc, "first seen here");
19143 volatile_loc = loc;
19144 cp_lexer_consume_token (parser->lexer);
19150 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19151 inform (inline_loc, "first seen here");
19155 cp_lexer_consume_token (parser->lexer);
19161 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19162 inform (goto_loc, "first seen here");
19166 cp_lexer_consume_token (parser->lexer);
19171 error_at (loc, "%qT is not an asm qualifier", token->u.value);
19172 cp_lexer_consume_token (parser->lexer);
19181 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19182 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19183 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19185 /* Look for the opening `('. */
19186 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19188 /* Look for the string. */
19189 string = cp_parser_string_literal (parser, false, false);
19190 if (string == error_mark_node)
19192 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19193 /*consume_paren=*/true);
19197 /* If we're allowing GNU extensions, check for the extended assembly
19198 syntax. Unfortunately, the `:' tokens need not be separated by
19199 a space in C, and so, for compatibility, we tolerate that here
19200 too. Doing that means that we have to treat the `::' operator as
19202 if (cp_parser_allow_gnu_extensions_p (parser)
19203 && parser->in_function_body
19204 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19205 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19207 bool inputs_p = false;
19208 bool clobbers_p = false;
19209 bool labels_p = false;
19211 /* The extended syntax was used. */
19214 /* Look for outputs. */
19215 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19217 /* Consume the `:'. */
19218 cp_lexer_consume_token (parser->lexer);
19219 /* Parse the output-operands. */
19220 if (cp_lexer_next_token_is_not (parser->lexer,
19222 && cp_lexer_next_token_is_not (parser->lexer,
19224 && cp_lexer_next_token_is_not (parser->lexer,
19228 outputs = cp_parser_asm_operand_list (parser);
19229 if (outputs == error_mark_node)
19230 invalid_outputs_p = true;
19233 /* If the next token is `::', there are no outputs, and the
19234 next token is the beginning of the inputs. */
19235 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19236 /* The inputs are coming next. */
19239 /* Look for inputs. */
19241 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19243 /* Consume the `:' or `::'. */
19244 cp_lexer_consume_token (parser->lexer);
19245 /* Parse the output-operands. */
19246 if (cp_lexer_next_token_is_not (parser->lexer,
19248 && cp_lexer_next_token_is_not (parser->lexer,
19250 && cp_lexer_next_token_is_not (parser->lexer,
19253 inputs = cp_parser_asm_operand_list (parser);
19254 if (inputs == error_mark_node)
19255 invalid_inputs_p = true;
19258 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19259 /* The clobbers are coming next. */
19262 /* Look for clobbers. */
19264 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19267 /* Consume the `:' or `::'. */
19268 cp_lexer_consume_token (parser->lexer);
19269 /* Parse the clobbers. */
19270 if (cp_lexer_next_token_is_not (parser->lexer,
19272 && cp_lexer_next_token_is_not (parser->lexer,
19274 clobbers = cp_parser_asm_clobber_list (parser);
19276 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19277 /* The labels are coming next. */
19280 /* Look for labels. */
19282 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19285 /* Consume the `:' or `::'. */
19286 cp_lexer_consume_token (parser->lexer);
19287 /* Parse the labels. */
19288 labels = cp_parser_asm_label_list (parser);
19291 if (goto_p && !labels_p)
19292 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19295 missing = RT_COLON_SCOPE;
19297 /* Look for the closing `)'. */
19298 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19299 missing ? missing : RT_CLOSE_PAREN))
19300 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19301 /*consume_paren=*/true);
19302 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19304 if (!invalid_inputs_p && !invalid_outputs_p)
19306 /* Create the ASM_EXPR. */
19307 if (parser->in_function_body)
19309 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19310 inputs, clobbers, labels, inline_p);
19311 /* If the extended syntax was not used, mark the ASM_EXPR. */
19314 tree temp = asm_stmt;
19315 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19316 temp = TREE_OPERAND (temp, 0);
19318 ASM_INPUT_P (temp) = 1;
19322 symtab->finalize_toplevel_asm (string);
19326 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19327 type that comes from the decl-specifier-seq. */
19330 strip_declarator_types (tree type, cp_declarator *declarator)
19332 for (cp_declarator *d = declarator; d;)
19342 if (TYPE_PTRMEMFUNC_P (type))
19343 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19344 type = TREE_TYPE (type);
19352 /* Declarators [gram.dcl.decl] */
19354 /* Parse an init-declarator.
19357 declarator initializer [opt]
19362 declarator asm-specification [opt] attributes [opt] initializer [opt]
19364 function-definition:
19365 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19367 decl-specifier-seq [opt] declarator function-try-block
19371 function-definition:
19372 __extension__ function-definition
19376 function-definition:
19377 decl-specifier-seq [opt] declarator function-transaction-block
19379 The DECL_SPECIFIERS apply to this declarator. Returns a
19380 representation of the entity declared. If MEMBER_P is TRUE, then
19381 this declarator appears in a class scope. The new DECL created by
19382 this declarator is returned.
19384 The CHECKS are access checks that should be performed once we know
19385 what entity is being declared (and, therefore, what classes have
19388 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19389 for a function-definition here as well. If the declarator is a
19390 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19391 be TRUE upon return. By that point, the function-definition will
19392 have been completely parsed.
19394 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19397 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19398 parsed declaration if it is an uninitialized single declarator not followed
19399 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19400 if present, will not be consumed. If returned, this declarator will be
19401 created with SD_INITIALIZED but will not call cp_finish_decl.
19403 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19404 and there is an initializer, the pointed location_t is set to the
19405 location of the '=' or `(', or '{' in C++11 token introducing the
19409 cp_parser_init_declarator (cp_parser* parser,
19410 cp_decl_specifier_seq *decl_specifiers,
19411 vec<deferred_access_check, va_gc> *checks,
19412 bool function_definition_allowed_p,
19414 int declares_class_or_enum,
19415 bool* function_definition_p,
19416 tree* maybe_range_for_decl,
19417 location_t* init_loc,
19420 cp_token *token = NULL, *asm_spec_start_token = NULL,
19421 *attributes_start_token = NULL;
19422 cp_declarator *declarator;
19423 tree prefix_attributes;
19424 tree attributes = NULL;
19425 tree asm_specification;
19427 tree decl = NULL_TREE;
19429 int is_initialized;
19430 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19431 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19433 enum cpp_ttype initialization_kind;
19434 bool is_direct_init = false;
19435 bool is_non_constant_init;
19436 int ctor_dtor_or_conv_p;
19437 bool friend_p = cp_parser_friend_p (decl_specifiers);
19438 tree pushed_scope = NULL_TREE;
19439 bool range_for_decl_p = false;
19440 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19441 location_t tmp_init_loc = UNKNOWN_LOCATION;
19443 /* Gather the attributes that were provided with the
19444 decl-specifiers. */
19445 prefix_attributes = decl_specifiers->attributes;
19447 /* Assume that this is not the declarator for a function
19449 if (function_definition_p)
19450 *function_definition_p = false;
19452 /* Default arguments are only permitted for function parameters. */
19453 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19454 parser->default_arg_ok_p = false;
19456 /* Defer access checks while parsing the declarator; we cannot know
19457 what names are accessible until we know what is being
19459 resume_deferring_access_checks ();
19461 token = cp_lexer_peek_token (parser->lexer);
19463 /* Parse the declarator. */
19465 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19466 &ctor_dtor_or_conv_p,
19467 /*parenthesized_p=*/NULL,
19468 member_p, friend_p);
19469 /* Gather up the deferred checks. */
19470 stop_deferring_access_checks ();
19472 parser->default_arg_ok_p = saved_default_arg_ok_p;
19474 /* If the DECLARATOR was erroneous, there's no need to go
19476 if (declarator == cp_error_declarator)
19477 return error_mark_node;
19479 /* Check that the number of template-parameter-lists is OK. */
19480 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19482 return error_mark_node;
19484 if (declares_class_or_enum & 2)
19485 cp_parser_check_for_definition_in_return_type (declarator,
19486 decl_specifiers->type,
19487 decl_specifiers->locations[ds_type_spec]);
19489 /* Figure out what scope the entity declared by the DECLARATOR is
19490 located in. `grokdeclarator' sometimes changes the scope, so
19491 we compute it now. */
19492 scope = get_scope_of_declarator (declarator);
19494 /* Perform any lookups in the declared type which were thought to be
19495 dependent, but are not in the scope of the declarator. */
19496 decl_specifiers->type
19497 = maybe_update_decl_type (decl_specifiers->type, scope);
19499 /* If we're allowing GNU extensions, look for an
19500 asm-specification. */
19501 if (cp_parser_allow_gnu_extensions_p (parser))
19503 /* Look for an asm-specification. */
19504 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19505 asm_specification = cp_parser_asm_specification_opt (parser);
19508 asm_specification = NULL_TREE;
19510 /* Look for attributes. */
19511 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19512 attributes = cp_parser_attributes_opt (parser);
19514 /* Peek at the next token. */
19515 token = cp_lexer_peek_token (parser->lexer);
19517 bool bogus_implicit_tmpl = false;
19519 if (function_declarator_p (declarator))
19521 /* Handle C++17 deduction guides. */
19522 if (!decl_specifiers->type
19523 && ctor_dtor_or_conv_p <= 0
19524 && cxx_dialect >= cxx17)
19526 cp_declarator *id = get_id_declarator (declarator);
19527 tree name = id->u.id.unqualified_name;
19528 parser->scope = id->u.id.qualifying_scope;
19529 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19531 && (DECL_CLASS_TEMPLATE_P (tmpl)
19532 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19534 id->u.id.unqualified_name = dguide_name (tmpl);
19535 id->u.id.sfk = sfk_deduction_guide;
19536 ctor_dtor_or_conv_p = 1;
19540 /* Check to see if the token indicates the start of a
19541 function-definition. */
19542 if (cp_parser_token_starts_function_definition_p (token))
19544 if (!function_definition_allowed_p)
19546 /* If a function-definition should not appear here, issue an
19548 cp_parser_error (parser,
19549 "a function-definition is not allowed here");
19550 return error_mark_node;
19553 location_t func_brace_location
19554 = cp_lexer_peek_token (parser->lexer)->location;
19556 /* Neither attributes nor an asm-specification are allowed
19557 on a function-definition. */
19558 if (asm_specification)
19559 error_at (asm_spec_start_token->location,
19560 "an asm-specification is not allowed "
19561 "on a function-definition");
19563 error_at (attributes_start_token->location,
19564 "attributes are not allowed "
19565 "on a function-definition");
19566 /* This is a function-definition. */
19567 *function_definition_p = true;
19569 /* Parse the function definition. */
19571 decl = cp_parser_save_member_function_body (parser,
19574 prefix_attributes);
19577 (cp_parser_function_definition_from_specifiers_and_declarator
19578 (parser, decl_specifiers, prefix_attributes, declarator));
19580 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19582 /* This is where the prologue starts... */
19583 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19584 = func_brace_location;
19590 else if (parser->fully_implicit_function_template_p)
19592 /* A non-template declaration involving a function parameter list
19593 containing an implicit template parameter will be made into a
19594 template. If the resulting declaration is not going to be an
19595 actual function then finish the template scope here to prevent it.
19596 An error message will be issued once we have a decl to talk about.
19598 FIXME probably we should do type deduction rather than create an
19599 implicit template, but the standard currently doesn't allow it. */
19600 bogus_implicit_tmpl = true;
19601 finish_fully_implicit_template (parser, NULL_TREE);
19606 Only in function declarations for constructors, destructors, type
19607 conversions, and deduction guides can the decl-specifier-seq be omitted.
19609 We explicitly postpone this check past the point where we handle
19610 function-definitions because we tolerate function-definitions
19611 that are missing their return types in some modes. */
19612 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19614 cp_parser_error (parser,
19615 "expected constructor, destructor, or type conversion");
19616 return error_mark_node;
19619 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19620 if (token->type == CPP_EQ
19621 || token->type == CPP_OPEN_PAREN
19622 || token->type == CPP_OPEN_BRACE)
19624 is_initialized = SD_INITIALIZED;
19625 initialization_kind = token->type;
19626 if (maybe_range_for_decl)
19627 *maybe_range_for_decl = error_mark_node;
19628 tmp_init_loc = token->location;
19629 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19630 *init_loc = tmp_init_loc;
19632 if (token->type == CPP_EQ
19633 && function_declarator_p (declarator))
19635 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19636 if (t2->keyword == RID_DEFAULT)
19637 is_initialized = SD_DEFAULTED;
19638 else if (t2->keyword == RID_DELETE)
19639 is_initialized = SD_DELETED;
19644 /* If the init-declarator isn't initialized and isn't followed by a
19645 `,' or `;', it's not a valid init-declarator. */
19646 if (token->type != CPP_COMMA
19647 && token->type != CPP_SEMICOLON)
19649 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19650 range_for_decl_p = true;
19653 if (!maybe_range_for_decl)
19654 cp_parser_error (parser, "expected initializer");
19655 return error_mark_node;
19658 is_initialized = SD_UNINITIALIZED;
19659 initialization_kind = CPP_EOF;
19662 /* Because start_decl has side-effects, we should only call it if we
19663 know we're going ahead. By this point, we know that we cannot
19664 possibly be looking at any other construct. */
19665 cp_parser_commit_to_tentative_parse (parser);
19667 /* Enter the newly declared entry in the symbol table. If we're
19668 processing a declaration in a class-specifier, we wait until
19669 after processing the initializer. */
19672 if (parser->in_unbraced_linkage_specification_p)
19673 decl_specifiers->storage_class = sc_extern;
19674 decl = start_decl (declarator, decl_specifiers,
19675 range_for_decl_p? SD_INITIALIZED : is_initialized,
19676 attributes, prefix_attributes, &pushed_scope);
19677 cp_finalize_omp_declare_simd (parser, decl);
19678 cp_finalize_oacc_routine (parser, decl, false);
19679 /* Adjust location of decl if declarator->id_loc is more appropriate:
19680 set, and decl wasn't merged with another decl, in which case its
19681 location would be different from input_location, and more accurate. */
19683 && declarator->id_loc != UNKNOWN_LOCATION
19684 && DECL_SOURCE_LOCATION (decl) == input_location)
19685 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19688 /* Enter the SCOPE. That way unqualified names appearing in the
19689 initializer will be looked up in SCOPE. */
19690 pushed_scope = push_scope (scope);
19692 /* Perform deferred access control checks, now that we know in which
19693 SCOPE the declared entity resides. */
19694 if (!member_p && decl)
19696 tree saved_current_function_decl = NULL_TREE;
19698 /* If the entity being declared is a function, pretend that we
19699 are in its scope. If it is a `friend', it may have access to
19700 things that would not otherwise be accessible. */
19701 if (TREE_CODE (decl) == FUNCTION_DECL)
19703 saved_current_function_decl = current_function_decl;
19704 current_function_decl = decl;
19707 /* Perform access checks for template parameters. */
19708 cp_parser_perform_template_parameter_access_checks (checks);
19710 /* Perform the access control checks for the declarator and the
19711 decl-specifiers. */
19712 perform_deferred_access_checks (tf_warning_or_error);
19714 /* Restore the saved value. */
19715 if (TREE_CODE (decl) == FUNCTION_DECL)
19716 current_function_decl = saved_current_function_decl;
19719 /* Parse the initializer. */
19720 initializer = NULL_TREE;
19721 is_direct_init = false;
19722 is_non_constant_init = true;
19723 if (is_initialized)
19725 if (function_declarator_p (declarator))
19727 if (initialization_kind == CPP_EQ)
19728 initializer = cp_parser_pure_specifier (parser);
19731 /* If the declaration was erroneous, we don't really
19732 know what the user intended, so just silently
19733 consume the initializer. */
19734 if (decl != error_mark_node)
19735 error_at (tmp_init_loc, "initializer provided for function");
19736 cp_parser_skip_to_closing_parenthesis (parser,
19737 /*recovering=*/true,
19738 /*or_comma=*/false,
19739 /*consume_paren=*/true);
19744 /* We want to record the extra mangling scope for in-class
19745 initializers of class members and initializers of static data
19746 member templates. The former involves deferring
19747 parsing of the initializer until end of class as with default
19748 arguments. So right here we only handle the latter. */
19749 if (!member_p && processing_template_decl && decl != error_mark_node)
19750 start_lambda_scope (decl);
19751 initializer = cp_parser_initializer (parser,
19753 &is_non_constant_init);
19754 if (!member_p && processing_template_decl && decl != error_mark_node)
19755 finish_lambda_scope ();
19756 if (initializer == error_mark_node)
19757 cp_parser_skip_to_end_of_statement (parser);
19761 /* The old parser allows attributes to appear after a parenthesized
19762 initializer. Mark Mitchell proposed removing this functionality
19763 on the GCC mailing lists on 2002-08-13. This parser accepts the
19764 attributes -- but ignores them. Made a permerror in GCC 8. */
19765 if (cp_parser_allow_gnu_extensions_p (parser)
19766 && initialization_kind == CPP_OPEN_PAREN
19767 && cp_parser_attributes_opt (parser)
19768 && permerror (input_location,
19769 "attributes after parenthesized initializer ignored"))
19772 if (flag_permissive && !hint)
19775 inform (input_location,
19776 "this flexibility is deprecated and will be removed");
19780 /* And now complain about a non-function implicit template. */
19781 if (bogus_implicit_tmpl && decl != error_mark_node)
19782 error_at (DECL_SOURCE_LOCATION (decl),
19783 "non-function %qD declared as implicit template", decl);
19785 /* For an in-class declaration, use `grokfield' to create the
19791 pop_scope (pushed_scope);
19792 pushed_scope = NULL_TREE;
19794 decl = grokfield (declarator, decl_specifiers,
19795 initializer, !is_non_constant_init,
19796 /*asmspec=*/NULL_TREE,
19797 attr_chainon (attributes, prefix_attributes));
19798 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19799 cp_parser_save_default_args (parser, decl);
19800 cp_finalize_omp_declare_simd (parser, decl);
19801 cp_finalize_oacc_routine (parser, decl, false);
19804 /* Finish processing the declaration. But, skip member
19806 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19808 cp_finish_decl (decl,
19809 initializer, !is_non_constant_init,
19811 /* If the initializer is in parentheses, then this is
19812 a direct-initialization, which means that an
19813 `explicit' constructor is OK. Otherwise, an
19814 `explicit' constructor cannot be used. */
19815 ((is_direct_init || !is_initialized)
19816 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19818 else if ((cxx_dialect != cxx98) && friend_p
19819 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19820 /* Core issue #226 (C++0x only): A default template-argument
19821 shall not be specified in a friend class template
19823 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19824 /*is_partial=*/false, /*is_friend_decl=*/1);
19826 if (!friend_p && pushed_scope)
19827 pop_scope (pushed_scope);
19829 if (function_declarator_p (declarator)
19830 && parser->fully_implicit_function_template_p)
19833 decl = finish_fully_implicit_template (parser, decl);
19835 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19838 if (auto_result && is_initialized && decl_specifiers->type
19839 && type_uses_auto (decl_specifiers->type))
19840 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19845 /* Parse a declarator.
19849 ptr-operator declarator
19851 abstract-declarator:
19852 ptr-operator abstract-declarator [opt]
19853 direct-abstract-declarator
19858 attributes [opt] direct-declarator
19859 attributes [opt] ptr-operator declarator
19861 abstract-declarator:
19862 attributes [opt] ptr-operator abstract-declarator [opt]
19863 attributes [opt] direct-abstract-declarator
19865 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19866 detect constructors, destructors, deduction guides, or conversion operators.
19867 It is set to -1 if the declarator is a name, and +1 if it is a
19868 function. Otherwise it is set to zero. Usually you just want to
19869 test for >0, but internally the negative value is used.
19871 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19872 a decl-specifier-seq unless it declares a constructor, destructor,
19873 or conversion. It might seem that we could check this condition in
19874 semantic analysis, rather than parsing, but that makes it difficult
19875 to handle something like `f()'. We want to notice that there are
19876 no decl-specifiers, and therefore realize that this is an
19877 expression, not a declaration.)
19879 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19880 the declarator is a direct-declarator of the form "(...)".
19882 MEMBER_P is true iff this declarator is a member-declarator.
19884 FRIEND_P is true iff this declarator is a friend. */
19886 static cp_declarator *
19887 cp_parser_declarator (cp_parser* parser,
19888 cp_parser_declarator_kind dcl_kind,
19889 int* ctor_dtor_or_conv_p,
19890 bool* parenthesized_p,
19891 bool member_p, bool friend_p)
19893 cp_declarator *declarator;
19894 enum tree_code code;
19895 cp_cv_quals cv_quals;
19897 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19899 /* Assume this is not a constructor, destructor, or type-conversion
19901 if (ctor_dtor_or_conv_p)
19902 *ctor_dtor_or_conv_p = 0;
19904 if (cp_parser_allow_gnu_extensions_p (parser))
19905 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19907 /* Check for the ptr-operator production. */
19908 cp_parser_parse_tentatively (parser);
19909 /* Parse the ptr-operator. */
19910 code = cp_parser_ptr_operator (parser,
19915 /* If that worked, then we have a ptr-operator. */
19916 if (cp_parser_parse_definitely (parser))
19918 /* If a ptr-operator was found, then this declarator was not
19920 if (parenthesized_p)
19921 *parenthesized_p = true;
19922 /* The dependent declarator is optional if we are parsing an
19923 abstract-declarator. */
19924 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19925 cp_parser_parse_tentatively (parser);
19927 /* Parse the dependent declarator. */
19928 declarator = cp_parser_declarator (parser, dcl_kind,
19929 /*ctor_dtor_or_conv_p=*/NULL,
19930 /*parenthesized_p=*/NULL,
19931 /*member_p=*/false,
19934 /* If we are parsing an abstract-declarator, we must handle the
19935 case where the dependent declarator is absent. */
19936 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19937 && !cp_parser_parse_definitely (parser))
19940 declarator = cp_parser_make_indirect_declarator
19941 (code, class_type, cv_quals, declarator, std_attributes);
19943 /* Everything else is a direct-declarator. */
19946 if (parenthesized_p)
19947 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19949 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19950 ctor_dtor_or_conv_p,
19951 member_p, friend_p);
19954 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19955 declarator->attributes = gnu_attributes;
19959 /* Parse a direct-declarator or direct-abstract-declarator.
19963 direct-declarator ( parameter-declaration-clause )
19964 cv-qualifier-seq [opt]
19965 ref-qualifier [opt]
19966 exception-specification [opt]
19967 direct-declarator [ constant-expression [opt] ]
19970 direct-abstract-declarator:
19971 direct-abstract-declarator [opt]
19972 ( parameter-declaration-clause )
19973 cv-qualifier-seq [opt]
19974 ref-qualifier [opt]
19975 exception-specification [opt]
19976 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19977 ( abstract-declarator )
19979 Returns a representation of the declarator. DCL_KIND is
19980 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19981 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19982 we are parsing a direct-declarator. It is
19983 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19984 of ambiguity we prefer an abstract declarator, as per
19985 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19986 as for cp_parser_declarator. */
19988 static cp_declarator *
19989 cp_parser_direct_declarator (cp_parser* parser,
19990 cp_parser_declarator_kind dcl_kind,
19991 int* ctor_dtor_or_conv_p,
19992 bool member_p, bool friend_p)
19995 cp_declarator *declarator = NULL;
19996 tree scope = NULL_TREE;
19997 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19998 bool saved_in_declarator_p = parser->in_declarator_p;
20000 tree pushed_scope = NULL_TREE;
20001 cp_token *open_paren = NULL, *close_paren = NULL;
20005 /* Peek at the next token. */
20006 token = cp_lexer_peek_token (parser->lexer);
20007 if (token->type == CPP_OPEN_PAREN)
20009 /* This is either a parameter-declaration-clause, or a
20010 parenthesized declarator. When we know we are parsing a
20011 named declarator, it must be a parenthesized declarator
20012 if FIRST is true. For instance, `(int)' is a
20013 parameter-declaration-clause, with an omitted
20014 direct-abstract-declarator. But `((*))', is a
20015 parenthesized abstract declarator. Finally, when T is a
20016 template parameter `(T)' is a
20017 parameter-declaration-clause, and not a parenthesized
20020 We first try and parse a parameter-declaration-clause,
20021 and then try a nested declarator (if FIRST is true).
20023 It is not an error for it not to be a
20024 parameter-declaration-clause, even when FIRST is
20030 The first is the declaration of a function while the
20031 second is the definition of a variable, including its
20034 Having seen only the parenthesis, we cannot know which of
20035 these two alternatives should be selected. Even more
20036 complex are examples like:
20041 The former is a function-declaration; the latter is a
20042 variable initialization.
20044 Thus again, we try a parameter-declaration-clause, and if
20045 that fails, we back out and return. */
20047 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20050 bool is_declarator = false;
20054 /* In a member-declarator, the only valid interpretation
20055 of a parenthesis is the start of a
20056 parameter-declaration-clause. (It is invalid to
20057 initialize a static data member with a parenthesized
20058 initializer; only the "=" form of initialization is
20061 cp_parser_parse_tentatively (parser);
20063 /* Consume the `('. */
20064 matching_parens parens;
20065 parens.consume_open (parser);
20068 /* If this is going to be an abstract declarator, we're
20069 in a declarator and we can't have default args. */
20070 parser->default_arg_ok_p = false;
20071 parser->in_declarator_p = true;
20074 begin_scope (sk_function_parms, NULL_TREE);
20076 /* Parse the parameter-declaration-clause. */
20077 params = cp_parser_parameter_declaration_clause (parser);
20079 /* Consume the `)'. */
20080 parens.require_close (parser);
20082 /* If all went well, parse the cv-qualifier-seq,
20083 ref-qualifier and the exception-specification. */
20084 if (member_p || cp_parser_parse_definitely (parser))
20086 cp_cv_quals cv_quals;
20087 cp_virt_specifiers virt_specifiers;
20088 cp_ref_qualifier ref_qual;
20089 tree exception_specification;
20092 bool memfn = (member_p || (pushed_scope
20093 && CLASS_TYPE_P (pushed_scope)));
20095 is_declarator = true;
20097 if (ctor_dtor_or_conv_p)
20098 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20101 /* Parse the cv-qualifier-seq. */
20102 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20103 /* Parse the ref-qualifier. */
20104 ref_qual = cp_parser_ref_qualifier_opt (parser);
20105 /* Parse the tx-qualifier. */
20106 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20107 /* And the exception-specification. */
20108 exception_specification
20109 = cp_parser_exception_specification_opt (parser);
20111 attrs = cp_parser_std_attribute_spec_seq (parser);
20113 /* In here, we handle cases where attribute is used after
20114 the function declaration. For example:
20115 void func (int x) __attribute__((vector(..))); */
20116 tree gnu_attrs = NULL_TREE;
20117 tree requires_clause = NULL_TREE;
20118 late_return = (cp_parser_late_return_type_opt
20119 (parser, declarator, requires_clause,
20120 memfn ? cv_quals : -1));
20122 /* Parse the virt-specifier-seq. */
20123 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20125 /* Create the function-declarator. */
20126 declarator = make_call_declarator (declarator,
20132 exception_specification,
20135 declarator->std_attributes = attrs;
20136 declarator->attributes = gnu_attrs;
20137 /* Any subsequent parameter lists are to do with
20138 return type, so are not those of the declared
20140 parser->default_arg_ok_p = false;
20143 /* Remove the function parms from scope. */
20144 pop_bindings_and_leave_scope ();
20147 /* Repeat the main loop. */
20151 /* If this is the first, we can try a parenthesized
20155 bool saved_in_type_id_in_expr_p;
20157 parser->default_arg_ok_p = saved_default_arg_ok_p;
20158 parser->in_declarator_p = saved_in_declarator_p;
20160 open_paren = token;
20161 /* Consume the `('. */
20162 matching_parens parens;
20163 parens.consume_open (parser);
20164 /* Parse the nested declarator. */
20165 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20166 parser->in_type_id_in_expr_p = true;
20168 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20169 /*parenthesized_p=*/NULL,
20170 member_p, friend_p);
20171 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20173 /* Expect a `)'. */
20174 close_paren = cp_lexer_peek_token (parser->lexer);
20175 if (!parens.require_close (parser))
20176 declarator = cp_error_declarator;
20177 if (declarator == cp_error_declarator)
20180 goto handle_declarator;
20182 /* Otherwise, we must be done. */
20186 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20187 && token->type == CPP_OPEN_SQUARE
20188 && !cp_next_tokens_can_be_attribute_p (parser))
20190 /* Parse an array-declarator. */
20191 tree bounds, attrs;
20193 if (ctor_dtor_or_conv_p)
20194 *ctor_dtor_or_conv_p = 0;
20198 parser->default_arg_ok_p = false;
20199 parser->in_declarator_p = true;
20200 /* Consume the `['. */
20201 cp_lexer_consume_token (parser->lexer);
20202 /* Peek at the next token. */
20203 token = cp_lexer_peek_token (parser->lexer);
20204 /* If the next token is `]', then there is no
20205 constant-expression. */
20206 if (token->type != CPP_CLOSE_SQUARE)
20208 bool non_constant_p;
20210 = cp_parser_constant_expression (parser,
20211 /*allow_non_constant=*/true,
20213 if (!non_constant_p)
20215 else if (error_operand_p (bounds))
20216 /* Already gave an error. */;
20217 else if (!parser->in_function_body
20218 || current_binding_level->kind == sk_function_parms)
20220 /* Normally, the array bound must be an integral constant
20221 expression. However, as an extension, we allow VLAs
20222 in function scopes as long as they aren't part of a
20223 parameter declaration. */
20224 cp_parser_error (parser,
20225 "array bound is not an integer constant");
20226 bounds = error_mark_node;
20228 else if (processing_template_decl
20229 && !type_dependent_expression_p (bounds))
20231 /* Remember this wasn't a constant-expression. */
20232 bounds = build_nop (TREE_TYPE (bounds), bounds);
20233 TREE_SIDE_EFFECTS (bounds) = 1;
20237 bounds = NULL_TREE;
20238 /* Look for the closing `]'. */
20239 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20241 declarator = cp_error_declarator;
20245 attrs = cp_parser_std_attribute_spec_seq (parser);
20246 declarator = make_array_declarator (declarator, bounds);
20247 declarator->std_attributes = attrs;
20249 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20252 tree qualifying_scope;
20253 tree unqualified_name;
20255 special_function_kind sfk;
20257 bool pack_expansion_p = false;
20258 cp_token *declarator_id_start_token;
20260 /* Parse a declarator-id */
20261 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20264 cp_parser_parse_tentatively (parser);
20266 /* If we see an ellipsis, we should be looking at a
20268 if (token->type == CPP_ELLIPSIS)
20270 /* Consume the `...' */
20271 cp_lexer_consume_token (parser->lexer);
20273 pack_expansion_p = true;
20277 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20279 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20280 qualifying_scope = parser->scope;
20285 if (!unqualified_name && pack_expansion_p)
20287 /* Check whether an error occurred. */
20288 okay = !cp_parser_error_occurred (parser);
20290 /* We already consumed the ellipsis to mark a
20291 parameter pack, but we have no way to report it,
20292 so abort the tentative parse. We will be exiting
20293 immediately anyway. */
20294 cp_parser_abort_tentative_parse (parser);
20297 okay = cp_parser_parse_definitely (parser);
20300 unqualified_name = error_mark_node;
20301 else if (unqualified_name
20302 && (qualifying_scope
20303 || (!identifier_p (unqualified_name))))
20305 cp_parser_error (parser, "expected unqualified-id");
20306 unqualified_name = error_mark_node;
20310 if (!unqualified_name)
20312 if (unqualified_name == error_mark_node)
20314 declarator = cp_error_declarator;
20315 pack_expansion_p = false;
20316 declarator->parameter_pack_p = false;
20320 attrs = cp_parser_std_attribute_spec_seq (parser);
20322 if (qualifying_scope && at_namespace_scope_p ()
20323 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20325 /* In the declaration of a member of a template class
20326 outside of the class itself, the SCOPE will sometimes
20327 be a TYPENAME_TYPE. For example, given:
20329 template <typename T>
20330 int S<T>::R::i = 3;
20332 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20333 this context, we must resolve S<T>::R to an ordinary
20334 type, rather than a typename type.
20336 The reason we normally avoid resolving TYPENAME_TYPEs
20337 is that a specialization of `S' might render
20338 `S<T>::R' not a type. However, if `S' is
20339 specialized, then this `i' will not be used, so there
20340 is no harm in resolving the types here. */
20343 /* Resolve the TYPENAME_TYPE. */
20344 type = resolve_typename_type (qualifying_scope,
20345 /*only_current_p=*/false);
20346 /* If that failed, the declarator is invalid. */
20347 if (TREE_CODE (type) == TYPENAME_TYPE)
20349 if (typedef_variant_p (type))
20350 error_at (declarator_id_start_token->location,
20351 "cannot define member of dependent typedef "
20354 error_at (declarator_id_start_token->location,
20355 "%<%T::%E%> is not a type",
20356 TYPE_CONTEXT (qualifying_scope),
20357 TYPE_IDENTIFIER (qualifying_scope));
20359 qualifying_scope = type;
20364 if (unqualified_name)
20368 if (qualifying_scope
20369 && CLASS_TYPE_P (qualifying_scope))
20370 class_type = qualifying_scope;
20372 class_type = current_class_type;
20374 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20376 tree name_type = TREE_TYPE (unqualified_name);
20378 if (!class_type || !same_type_p (name_type, class_type))
20380 /* We do not attempt to print the declarator
20381 here because we do not have enough
20382 information about its original syntactic
20384 cp_parser_error (parser, "invalid declarator");
20385 declarator = cp_error_declarator;
20388 else if (qualifying_scope
20389 && CLASSTYPE_USE_TEMPLATE (name_type))
20391 error_at (declarator_id_start_token->location,
20392 "invalid use of constructor as a template");
20393 inform (declarator_id_start_token->location,
20394 "use %<%T::%D%> instead of %<%T::%D%> to "
20395 "name the constructor in a qualified name",
20397 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20398 class_type, name_type);
20399 declarator = cp_error_declarator;
20402 unqualified_name = constructor_name (class_type);
20407 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20408 sfk = sfk_destructor;
20409 else if (identifier_p (unqualified_name)
20410 && IDENTIFIER_CONV_OP_P (unqualified_name))
20411 sfk = sfk_conversion;
20412 else if (/* There's no way to declare a constructor
20413 for an unnamed type, even if the type
20414 got a name for linkage purposes. */
20415 !TYPE_WAS_UNNAMED (class_type)
20416 /* Handle correctly (c++/19200):
20430 friend void N::S();
20432 && (!friend_p || class_type == qualifying_scope)
20433 && constructor_name_p (unqualified_name,
20435 sfk = sfk_constructor;
20436 else if (is_overloaded_fn (unqualified_name)
20437 && DECL_CONSTRUCTOR_P (get_first_fn
20438 (unqualified_name)))
20439 sfk = sfk_constructor;
20441 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20442 *ctor_dtor_or_conv_p = -1;
20445 declarator = make_id_declarator (qualifying_scope,
20448 declarator->std_attributes = attrs;
20449 declarator->id_loc = token->location;
20450 declarator->parameter_pack_p = pack_expansion_p;
20452 if (pack_expansion_p)
20453 maybe_warn_variadic_templates ();
20456 handle_declarator:;
20457 scope = get_scope_of_declarator (declarator);
20460 /* Any names that appear after the declarator-id for a
20461 member are looked up in the containing scope. */
20462 if (at_function_scope_p ())
20464 /* But declarations with qualified-ids can't appear in a
20466 cp_parser_error (parser, "qualified-id in declaration");
20467 declarator = cp_error_declarator;
20470 pushed_scope = push_scope (scope);
20472 parser->in_declarator_p = true;
20473 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20474 || (declarator && declarator->kind == cdk_id))
20475 /* Default args are only allowed on function
20477 parser->default_arg_ok_p = saved_default_arg_ok_p;
20479 parser->default_arg_ok_p = false;
20488 /* For an abstract declarator, we might wind up with nothing at this
20489 point. That's an error; the declarator is not optional. */
20491 cp_parser_error (parser, "expected declarator");
20492 else if (open_paren)
20494 /* Record overly parenthesized declarator so we can give a
20495 diagnostic about confusing decl/expr disambiguation. */
20496 if (declarator->kind == cdk_array)
20498 /* If the open and close parens are on different lines, this
20499 is probably a formatting thing, so ignore. */
20500 expanded_location open = expand_location (open_paren->location);
20501 expanded_location close = expand_location (close_paren->location);
20502 if (open.line != close.line || open.file != close.file)
20506 declarator->parenthesized = open_paren->location;
20509 /* If we entered a scope, we must exit it now. */
20511 pop_scope (pushed_scope);
20513 parser->default_arg_ok_p = saved_default_arg_ok_p;
20514 parser->in_declarator_p = saved_in_declarator_p;
20519 /* Parse a ptr-operator.
20522 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20523 * cv-qualifier-seq [opt]
20525 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20526 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20531 & cv-qualifier-seq [opt]
20533 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20534 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20535 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20536 filled in with the TYPE containing the member. *CV_QUALS is
20537 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20538 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20539 Note that the tree codes returned by this function have nothing
20540 to do with the types of trees that will be eventually be created
20541 to represent the pointer or reference type being parsed. They are
20542 just constants with suggestive names. */
20543 static enum tree_code
20544 cp_parser_ptr_operator (cp_parser* parser,
20546 cp_cv_quals *cv_quals,
20549 enum tree_code code = ERROR_MARK;
20551 tree attrs = NULL_TREE;
20553 /* Assume that it's not a pointer-to-member. */
20555 /* And that there are no cv-qualifiers. */
20556 *cv_quals = TYPE_UNQUALIFIED;
20558 /* Peek at the next token. */
20559 token = cp_lexer_peek_token (parser->lexer);
20561 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20562 if (token->type == CPP_MULT)
20563 code = INDIRECT_REF;
20564 else if (token->type == CPP_AND)
20566 else if ((cxx_dialect != cxx98) &&
20567 token->type == CPP_AND_AND) /* C++0x only */
20568 code = NON_LVALUE_EXPR;
20570 if (code != ERROR_MARK)
20572 /* Consume the `*', `&' or `&&'. */
20573 cp_lexer_consume_token (parser->lexer);
20575 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20576 `&', if we are allowing GNU extensions. (The only qualifier
20577 that can legally appear after `&' is `restrict', but that is
20578 enforced during semantic analysis. */
20579 if (code == INDIRECT_REF
20580 || cp_parser_allow_gnu_extensions_p (parser))
20581 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20583 attrs = cp_parser_std_attribute_spec_seq (parser);
20584 if (attributes != NULL)
20585 *attributes = attrs;
20589 /* Try the pointer-to-member case. */
20590 cp_parser_parse_tentatively (parser);
20591 /* Look for the optional `::' operator. */
20592 cp_parser_global_scope_opt (parser,
20593 /*current_scope_valid_p=*/false);
20594 /* Look for the nested-name specifier. */
20595 token = cp_lexer_peek_token (parser->lexer);
20596 cp_parser_nested_name_specifier (parser,
20597 /*typename_keyword_p=*/false,
20598 /*check_dependency_p=*/true,
20600 /*is_declaration=*/false);
20601 /* If we found it, and the next token is a `*', then we are
20602 indeed looking at a pointer-to-member operator. */
20603 if (!cp_parser_error_occurred (parser)
20604 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20606 /* Indicate that the `*' operator was used. */
20607 code = INDIRECT_REF;
20609 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20610 error_at (token->location, "%qD is a namespace", parser->scope);
20611 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20612 error_at (token->location, "cannot form pointer to member of "
20613 "non-class %q#T", parser->scope);
20616 /* The type of which the member is a member is given by the
20618 *type = parser->scope;
20619 /* The next name will not be qualified. */
20620 parser->scope = NULL_TREE;
20621 parser->qualifying_scope = NULL_TREE;
20622 parser->object_scope = NULL_TREE;
20623 /* Look for optional c++11 attributes. */
20624 attrs = cp_parser_std_attribute_spec_seq (parser);
20625 if (attributes != NULL)
20626 *attributes = attrs;
20627 /* Look for the optional cv-qualifier-seq. */
20628 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20631 /* If that didn't work we don't have a ptr-operator. */
20632 if (!cp_parser_parse_definitely (parser))
20633 cp_parser_error (parser, "expected ptr-operator");
20639 /* Parse an (optional) cv-qualifier-seq.
20642 cv-qualifier cv-qualifier-seq [opt]
20653 Returns a bitmask representing the cv-qualifiers. */
20656 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20658 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20663 cp_cv_quals cv_qualifier;
20665 /* Peek at the next token. */
20666 token = cp_lexer_peek_token (parser->lexer);
20667 /* See if it's a cv-qualifier. */
20668 switch (token->keyword)
20671 cv_qualifier = TYPE_QUAL_CONST;
20675 cv_qualifier = TYPE_QUAL_VOLATILE;
20679 cv_qualifier = TYPE_QUAL_RESTRICT;
20683 cv_qualifier = TYPE_UNQUALIFIED;
20690 if (cv_quals & cv_qualifier)
20692 gcc_rich_location richloc (token->location);
20693 richloc.add_fixit_remove ();
20694 error_at (&richloc, "duplicate cv-qualifier");
20695 cp_lexer_purge_token (parser->lexer);
20699 cp_lexer_consume_token (parser->lexer);
20700 cv_quals |= cv_qualifier;
20707 /* Parse an (optional) ref-qualifier
20713 Returns cp_ref_qualifier representing ref-qualifier. */
20715 static cp_ref_qualifier
20716 cp_parser_ref_qualifier_opt (cp_parser* parser)
20718 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20720 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20721 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20726 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20727 cp_token *token = cp_lexer_peek_token (parser->lexer);
20729 switch (token->type)
20732 curr_ref_qual = REF_QUAL_LVALUE;
20736 curr_ref_qual = REF_QUAL_RVALUE;
20740 curr_ref_qual = REF_QUAL_NONE;
20744 if (!curr_ref_qual)
20748 error_at (token->location, "multiple ref-qualifiers");
20749 cp_lexer_purge_token (parser->lexer);
20753 ref_qual = curr_ref_qual;
20754 cp_lexer_consume_token (parser->lexer);
20761 /* Parse an optional tx-qualifier.
20765 transaction_safe_dynamic */
20768 cp_parser_tx_qualifier_opt (cp_parser *parser)
20770 cp_token *token = cp_lexer_peek_token (parser->lexer);
20771 if (token->type == CPP_NAME)
20773 tree name = token->u.value;
20774 const char *p = IDENTIFIER_POINTER (name);
20775 const int len = strlen ("transaction_safe");
20776 if (!strncmp (p, "transaction_safe", len))
20780 || !strcmp (p, "_dynamic"))
20782 cp_lexer_consume_token (parser->lexer);
20785 error ("%qE requires %<-fgnu-tm%>", name);
20796 /* Parse an (optional) virt-specifier-seq.
20798 virt-specifier-seq:
20799 virt-specifier virt-specifier-seq [opt]
20805 Returns a bitmask representing the virt-specifiers. */
20807 static cp_virt_specifiers
20808 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20810 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20815 cp_virt_specifiers virt_specifier;
20817 /* Peek at the next token. */
20818 token = cp_lexer_peek_token (parser->lexer);
20819 /* See if it's a virt-specifier-qualifier. */
20820 if (token->type != CPP_NAME)
20822 if (id_equal (token->u.value, "override"))
20824 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20825 virt_specifier = VIRT_SPEC_OVERRIDE;
20827 else if (id_equal (token->u.value, "final"))
20829 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20830 virt_specifier = VIRT_SPEC_FINAL;
20832 else if (id_equal (token->u.value, "__final"))
20834 virt_specifier = VIRT_SPEC_FINAL;
20839 if (virt_specifiers & virt_specifier)
20841 gcc_rich_location richloc (token->location);
20842 richloc.add_fixit_remove ();
20843 error_at (&richloc, "duplicate virt-specifier");
20844 cp_lexer_purge_token (parser->lexer);
20848 cp_lexer_consume_token (parser->lexer);
20849 virt_specifiers |= virt_specifier;
20852 return virt_specifiers;
20855 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20856 is in scope even though it isn't real. */
20859 inject_this_parameter (tree ctype, cp_cv_quals quals)
20863 if (current_class_ptr)
20865 /* We don't clear this between NSDMIs. Is it already what we want? */
20866 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20867 if (DECL_P (current_class_ptr)
20868 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20869 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20870 && cp_type_quals (type) == quals)
20874 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20875 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20876 current_class_ptr = NULL_TREE;
20878 = cp_build_fold_indirect_ref (this_parm);
20879 current_class_ptr = this_parm;
20882 /* Return true iff our current scope is a non-static data member
20886 parsing_nsdmi (void)
20888 /* We recognize NSDMI context by the context-less 'this' pointer set up
20889 by the function above. */
20890 if (current_class_ptr
20891 && TREE_CODE (current_class_ptr) == PARM_DECL
20892 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20897 /* Parse a late-specified return type, if any. This is not a separate
20898 non-terminal, but part of a function declarator, which looks like
20900 -> trailing-type-specifier-seq abstract-declarator(opt)
20902 Returns the type indicated by the type-id.
20904 In addition to this, parse any queued up #pragma omp declare simd
20905 clauses, and #pragma acc routine clauses.
20907 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20911 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20912 tree& requires_clause, cp_cv_quals quals)
20915 tree type = NULL_TREE;
20916 bool declare_simd_p = (parser->omp_declare_simd
20918 && declarator->kind == cdk_id);
20920 bool oacc_routine_p = (parser->oacc_routine
20922 && declarator->kind == cdk_id);
20924 /* Peek at the next token. */
20925 token = cp_lexer_peek_token (parser->lexer);
20926 /* A late-specified return type is indicated by an initial '->'. */
20927 if (token->type != CPP_DEREF
20928 && token->keyword != RID_REQUIRES
20929 && !(token->type == CPP_NAME
20930 && token->u.value == ridpointers[RID_REQUIRES])
20931 && !(declare_simd_p || oacc_routine_p))
20934 tree save_ccp = current_class_ptr;
20935 tree save_ccr = current_class_ref;
20938 /* DR 1207: 'this' is in scope in the trailing return type. */
20939 inject_this_parameter (current_class_type, quals);
20942 if (token->type == CPP_DEREF)
20944 /* Consume the ->. */
20945 cp_lexer_consume_token (parser->lexer);
20947 type = cp_parser_trailing_type_id (parser);
20950 /* Function declarations may be followed by a trailing
20951 requires-clause. */
20952 requires_clause = cp_parser_requires_clause_opt (parser);
20954 if (declare_simd_p)
20955 declarator->attributes
20956 = cp_parser_late_parsing_omp_declare_simd (parser,
20957 declarator->attributes);
20958 if (oacc_routine_p)
20959 declarator->attributes
20960 = cp_parser_late_parsing_oacc_routine (parser,
20961 declarator->attributes);
20965 current_class_ptr = save_ccp;
20966 current_class_ref = save_ccr;
20972 /* Parse a declarator-id.
20976 :: [opt] nested-name-specifier [opt] type-name
20978 In the `id-expression' case, the value returned is as for
20979 cp_parser_id_expression if the id-expression was an unqualified-id.
20980 If the id-expression was a qualified-id, then a SCOPE_REF is
20981 returned. The first operand is the scope (either a NAMESPACE_DECL
20982 or TREE_TYPE), but the second is still just a representation of an
20986 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20989 /* The expression must be an id-expression. Assume that qualified
20990 names are the names of types so that:
20993 int S<T>::R::i = 3;
20995 will work; we must treat `S<T>::R' as the name of a type.
20996 Similarly, assume that qualified names are templates, where
21000 int S<T>::R<T>::i = 3;
21003 id = cp_parser_id_expression (parser,
21004 /*template_keyword_p=*/false,
21005 /*check_dependency_p=*/false,
21006 /*template_p=*/NULL,
21007 /*declarator_p=*/true,
21009 if (id && BASELINK_P (id))
21010 id = BASELINK_FUNCTIONS (id);
21014 /* Parse a type-id.
21017 type-specifier-seq abstract-declarator [opt]
21019 Returns the TYPE specified. */
21022 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21023 bool is_trailing_return)
21025 cp_decl_specifier_seq type_specifier_seq;
21026 cp_declarator *abstract_declarator;
21028 /* Parse the type-specifier-seq. */
21029 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21030 is_trailing_return,
21031 &type_specifier_seq);
21032 if (is_template_arg && type_specifier_seq.type
21033 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21034 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21035 /* A bare template name as a template argument is a template template
21036 argument, not a placeholder, so fail parsing it as a type argument. */
21038 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21039 cp_parser_simulate_error (parser);
21040 return error_mark_node;
21042 if (type_specifier_seq.type == error_mark_node)
21043 return error_mark_node;
21045 /* There might or might not be an abstract declarator. */
21046 cp_parser_parse_tentatively (parser);
21047 /* Look for the declarator. */
21048 abstract_declarator
21049 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21050 /*parenthesized_p=*/NULL,
21051 /*member_p=*/false,
21052 /*friend_p=*/false);
21053 /* Check to see if there really was a declarator. */
21054 if (!cp_parser_parse_definitely (parser))
21055 abstract_declarator = NULL;
21057 if (type_specifier_seq.type
21058 /* The concepts TS allows 'auto' as a type-id. */
21059 && (!flag_concepts || parser->in_type_id_in_expr_p)
21060 /* None of the valid uses of 'auto' in C++14 involve the type-id
21061 nonterminal, but it is valid in a trailing-return-type. */
21062 && !(cxx_dialect >= cxx14 && is_trailing_return))
21063 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21065 /* A type-id with type 'auto' is only ok if the abstract declarator
21066 is a function declarator with a late-specified return type.
21068 A type-id with 'auto' is also valid in a trailing-return-type
21069 in a compound-requirement. */
21070 if (abstract_declarator
21071 && abstract_declarator->kind == cdk_function
21072 && abstract_declarator->u.function.late_return_type)
21074 else if (parser->in_result_type_constraint_p)
21078 location_t loc = type_specifier_seq.locations[ds_type_spec];
21079 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21081 error_at (loc, "missing template arguments after %qT",
21083 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21087 error_at (loc, "invalid use of %qT", auto_node);
21088 return error_mark_node;
21092 return groktypename (&type_specifier_seq, abstract_declarator,
21097 cp_parser_type_id (cp_parser *parser)
21099 return cp_parser_type_id_1 (parser, false, false);
21103 cp_parser_template_type_arg (cp_parser *parser)
21106 const char *saved_message = parser->type_definition_forbidden_message;
21107 parser->type_definition_forbidden_message
21108 = G_("types may not be defined in template arguments");
21109 r = cp_parser_type_id_1 (parser, true, false);
21110 parser->type_definition_forbidden_message = saved_message;
21111 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21113 error ("invalid use of %<auto%> in template argument");
21114 r = error_mark_node;
21120 cp_parser_trailing_type_id (cp_parser *parser)
21122 return cp_parser_type_id_1 (parser, false, true);
21125 /* Parse a type-specifier-seq.
21127 type-specifier-seq:
21128 type-specifier type-specifier-seq [opt]
21132 type-specifier-seq:
21133 attributes type-specifier-seq [opt]
21135 If IS_DECLARATION is true, we are at the start of a "condition" or
21136 exception-declaration, so we might be followed by a declarator-id.
21138 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21139 i.e. we've just seen "->".
21141 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21144 cp_parser_type_specifier_seq (cp_parser* parser,
21145 bool is_declaration,
21146 bool is_trailing_return,
21147 cp_decl_specifier_seq *type_specifier_seq)
21149 bool seen_type_specifier = false;
21150 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21151 cp_token *start_token = NULL;
21153 /* Clear the TYPE_SPECIFIER_SEQ. */
21154 clear_decl_specs (type_specifier_seq);
21156 /* In the context of a trailing return type, enum E { } is an
21157 elaborated-type-specifier followed by a function-body, not an
21159 if (is_trailing_return)
21160 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21162 /* Parse the type-specifiers and attributes. */
21165 tree type_specifier;
21166 bool is_cv_qualifier;
21168 /* Check for attributes first. */
21169 if (cp_next_tokens_can_be_attribute_p (parser))
21171 type_specifier_seq->attributes
21172 = attr_chainon (type_specifier_seq->attributes,
21173 cp_parser_attributes_opt (parser));
21177 /* record the token of the beginning of the type specifier seq,
21178 for error reporting purposes*/
21180 start_token = cp_lexer_peek_token (parser->lexer);
21182 /* Look for the type-specifier. */
21183 type_specifier = cp_parser_type_specifier (parser,
21185 type_specifier_seq,
21186 /*is_declaration=*/false,
21189 if (!type_specifier)
21191 /* If the first type-specifier could not be found, this is not a
21192 type-specifier-seq at all. */
21193 if (!seen_type_specifier)
21195 /* Set in_declarator_p to avoid skipping to the semicolon. */
21196 int in_decl = parser->in_declarator_p;
21197 parser->in_declarator_p = true;
21199 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21200 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21201 cp_parser_error (parser, "expected type-specifier");
21203 parser->in_declarator_p = in_decl;
21205 type_specifier_seq->type = error_mark_node;
21208 /* If subsequent type-specifiers could not be found, the
21209 type-specifier-seq is complete. */
21213 seen_type_specifier = true;
21214 /* The standard says that a condition can be:
21216 type-specifier-seq declarator = assignment-expression
21223 we should treat the "S" as a declarator, not as a
21224 type-specifier. The standard doesn't say that explicitly for
21225 type-specifier-seq, but it does say that for
21226 decl-specifier-seq in an ordinary declaration. Perhaps it
21227 would be clearer just to allow a decl-specifier-seq here, and
21228 then add a semantic restriction that if any decl-specifiers
21229 that are not type-specifiers appear, the program is invalid. */
21230 if (is_declaration && !is_cv_qualifier)
21231 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21235 /* Return whether the function currently being declared has an associated
21236 template parameter list. */
21239 function_being_declared_is_template_p (cp_parser* parser)
21241 if (!current_template_parms || processing_template_parmlist)
21244 if (parser->implicit_template_scope)
21247 if (at_class_scope_p ()
21248 && TYPE_BEING_DEFINED (current_class_type))
21249 return parser->num_template_parameter_lists != 0;
21251 return ((int) parser->num_template_parameter_lists > template_class_depth
21252 (current_class_type));
21255 /* Parse a parameter-declaration-clause.
21257 parameter-declaration-clause:
21258 parameter-declaration-list [opt] ... [opt]
21259 parameter-declaration-list , ...
21261 Returns a representation for the parameter declarations. A return
21262 value of NULL indicates a parameter-declaration-clause consisting
21263 only of an ellipsis. */
21266 cp_parser_parameter_declaration_clause (cp_parser* parser)
21273 temp_override<bool> cleanup
21274 (parser->auto_is_implicit_function_template_parm_p);
21276 if (!processing_specialization
21277 && !processing_template_parmlist
21278 && !processing_explicit_instantiation
21279 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21280 actual function or a random abstract declarator. */
21281 && parser->default_arg_ok_p)
21282 if (!current_function_decl
21283 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21284 parser->auto_is_implicit_function_template_parm_p = true;
21286 /* Peek at the next token. */
21287 token = cp_lexer_peek_token (parser->lexer);
21288 /* Check for trivial parameter-declaration-clauses. */
21289 if (token->type == CPP_ELLIPSIS)
21291 /* Consume the `...' token. */
21292 cp_lexer_consume_token (parser->lexer);
21295 else if (token->type == CPP_CLOSE_PAREN)
21296 /* There are no parameters. */
21298 #ifndef NO_IMPLICIT_EXTERN_C
21299 if (in_system_header_at (input_location)
21300 && current_class_type == NULL
21301 && current_lang_name == lang_name_c)
21305 return void_list_node;
21307 /* Check for `(void)', too, which is a special case. */
21308 else if (token->keyword == RID_VOID
21309 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21310 == CPP_CLOSE_PAREN))
21312 /* Consume the `void' token. */
21313 cp_lexer_consume_token (parser->lexer);
21314 /* There are no parameters. */
21315 return void_list_node;
21318 /* Parse the parameter-declaration-list. */
21319 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21320 /* If a parse error occurred while parsing the
21321 parameter-declaration-list, then the entire
21322 parameter-declaration-clause is erroneous. */
21326 /* Peek at the next token. */
21327 token = cp_lexer_peek_token (parser->lexer);
21328 /* If it's a `,', the clause should terminate with an ellipsis. */
21329 if (token->type == CPP_COMMA)
21331 /* Consume the `,'. */
21332 cp_lexer_consume_token (parser->lexer);
21333 /* Expect an ellipsis. */
21335 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21337 /* It might also be `...' if the optional trailing `,' was
21339 else if (token->type == CPP_ELLIPSIS)
21341 /* Consume the `...' token. */
21342 cp_lexer_consume_token (parser->lexer);
21343 /* And remember that we saw it. */
21347 ellipsis_p = false;
21349 /* Finish the parameter list. */
21351 parameters = chainon (parameters, void_list_node);
21356 /* Parse a parameter-declaration-list.
21358 parameter-declaration-list:
21359 parameter-declaration
21360 parameter-declaration-list , parameter-declaration
21362 Returns a representation of the parameter-declaration-list, as for
21363 cp_parser_parameter_declaration_clause. However, the
21364 `void_list_node' is never appended to the list. Upon return,
21365 *IS_ERROR will be true iff an error occurred. */
21368 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21370 tree parameters = NULL_TREE;
21371 tree *tail = ¶meters;
21372 bool saved_in_unbraced_linkage_specification_p;
21375 /* Assume all will go well. */
21377 /* The special considerations that apply to a function within an
21378 unbraced linkage specifications do not apply to the parameters
21379 to the function. */
21380 saved_in_unbraced_linkage_specification_p
21381 = parser->in_unbraced_linkage_specification_p;
21382 parser->in_unbraced_linkage_specification_p = false;
21384 /* Look for more parameters. */
21387 cp_parameter_declarator *parameter;
21388 tree decl = error_mark_node;
21389 bool parenthesized_p = false;
21391 /* Parse the parameter. */
21393 = cp_parser_parameter_declaration (parser,
21394 /*template_parm_p=*/false,
21397 /* We don't know yet if the enclosing context is deprecated, so wait
21398 and warn in grokparms if appropriate. */
21399 deprecated_state = DEPRECATED_SUPPRESS;
21403 decl = grokdeclarator (parameter->declarator,
21404 ¶meter->decl_specifiers,
21406 parameter->default_argument != NULL_TREE,
21407 ¶meter->decl_specifiers.attributes);
21408 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21409 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21412 deprecated_state = DEPRECATED_NORMAL;
21414 /* If a parse error occurred parsing the parameter declaration,
21415 then the entire parameter-declaration-list is erroneous. */
21416 if (decl == error_mark_node)
21419 parameters = error_mark_node;
21423 if (parameter->decl_specifiers.attributes)
21424 cplus_decl_attributes (&decl,
21425 parameter->decl_specifiers.attributes,
21427 if (DECL_NAME (decl))
21428 decl = pushdecl (decl);
21430 if (decl != error_mark_node)
21432 retrofit_lang_decl (decl);
21433 DECL_PARM_INDEX (decl) = ++index;
21434 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21437 /* Add the new parameter to the list. */
21438 *tail = build_tree_list (parameter->default_argument, decl);
21439 tail = &TREE_CHAIN (*tail);
21441 /* Peek at the next token. */
21442 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21443 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21444 /* These are for Objective-C++ */
21445 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21446 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21447 /* The parameter-declaration-list is complete. */
21449 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21453 /* Peek at the next token. */
21454 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21455 /* If it's an ellipsis, then the list is complete. */
21456 if (token->type == CPP_ELLIPSIS)
21458 /* Otherwise, there must be more parameters. Consume the
21460 cp_lexer_consume_token (parser->lexer);
21461 /* When parsing something like:
21463 int i(float f, double d)
21465 we can tell after seeing the declaration for "f" that we
21466 are not looking at an initialization of a variable "i",
21467 but rather at the declaration of a function "i".
21469 Due to the fact that the parsing of template arguments
21470 (as specified to a template-id) requires backtracking we
21471 cannot use this technique when inside a template argument
21473 if (!parser->in_template_argument_list_p
21474 && !parser->in_type_id_in_expr_p
21475 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21476 /* However, a parameter-declaration of the form
21477 "float(f)" (which is a valid declaration of a
21478 parameter "f") can also be interpreted as an
21479 expression (the conversion of "f" to "float"). */
21480 && !parenthesized_p)
21481 cp_parser_commit_to_tentative_parse (parser);
21485 cp_parser_error (parser, "expected %<,%> or %<...%>");
21486 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21487 cp_parser_skip_to_closing_parenthesis (parser,
21488 /*recovering=*/true,
21489 /*or_comma=*/false,
21490 /*consume_paren=*/false);
21495 parser->in_unbraced_linkage_specification_p
21496 = saved_in_unbraced_linkage_specification_p;
21498 /* Reset implicit_template_scope if we are about to leave the function
21499 parameter list that introduced it. Note that for out-of-line member
21500 definitions, there will be one or more class scopes before we get to
21501 the template parameter scope. */
21503 if (cp_binding_level *its = parser->implicit_template_scope)
21504 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21506 while (maybe_its->kind == sk_class)
21507 maybe_its = maybe_its->level_chain;
21508 if (maybe_its == its)
21510 parser->implicit_template_parms = 0;
21511 parser->implicit_template_scope = 0;
21518 /* Parse a parameter declaration.
21520 parameter-declaration:
21521 decl-specifier-seq ... [opt] declarator
21522 decl-specifier-seq declarator = assignment-expression
21523 decl-specifier-seq ... [opt] abstract-declarator [opt]
21524 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21526 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21527 declares a template parameter. (In that case, a non-nested `>'
21528 token encountered during the parsing of the assignment-expression
21529 is not interpreted as a greater-than operator.)
21531 Returns a representation of the parameter, or NULL if an error
21532 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21533 true iff the declarator is of the form "(p)". */
21535 static cp_parameter_declarator *
21536 cp_parser_parameter_declaration (cp_parser *parser,
21537 bool template_parm_p,
21538 bool *parenthesized_p)
21540 int declares_class_or_enum;
21541 cp_decl_specifier_seq decl_specifiers;
21542 cp_declarator *declarator;
21543 tree default_argument;
21544 cp_token *token = NULL, *declarator_token_start = NULL;
21545 const char *saved_message;
21546 bool template_parameter_pack_p = false;
21548 /* In a template parameter, `>' is not an operator.
21552 When parsing a default template-argument for a non-type
21553 template-parameter, the first non-nested `>' is taken as the end
21554 of the template parameter-list rather than a greater-than
21557 /* Type definitions may not appear in parameter types. */
21558 saved_message = parser->type_definition_forbidden_message;
21559 parser->type_definition_forbidden_message
21560 = G_("types may not be defined in parameter types");
21562 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21563 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21564 (current_template_parms)) : 0);
21566 /* Parse the declaration-specifiers. */
21567 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21568 cp_parser_decl_specifier_seq (parser,
21569 CP_PARSER_FLAGS_NONE,
21571 &declares_class_or_enum);
21573 /* Complain about missing 'typename' or other invalid type names. */
21574 if (!decl_specifiers.any_type_specifiers_p
21575 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21576 decl_specifiers.type = error_mark_node;
21578 /* If an error occurred, there's no reason to attempt to parse the
21579 rest of the declaration. */
21580 if (cp_parser_error_occurred (parser))
21582 parser->type_definition_forbidden_message = saved_message;
21586 /* Peek at the next token. */
21587 token = cp_lexer_peek_token (parser->lexer);
21589 /* If the next token is a `)', `,', `=', `>', or `...', then there
21590 is no declarator. However, when variadic templates are enabled,
21591 there may be a declarator following `...'. */
21592 if (token->type == CPP_CLOSE_PAREN
21593 || token->type == CPP_COMMA
21594 || token->type == CPP_EQ
21595 || token->type == CPP_GREATER)
21598 if (parenthesized_p)
21599 *parenthesized_p = false;
21601 /* Otherwise, there should be a declarator. */
21604 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21605 parser->default_arg_ok_p = false;
21607 /* After seeing a decl-specifier-seq, if the next token is not a
21608 "(", there is no possibility that the code is a valid
21609 expression. Therefore, if parsing tentatively, we commit at
21611 if (!parser->in_template_argument_list_p
21612 /* In an expression context, having seen:
21616 we cannot be sure whether we are looking at a
21617 function-type (taking a "char" as a parameter) or a cast
21618 of some object of type "char" to "int". */
21619 && !parser->in_type_id_in_expr_p
21620 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21621 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21622 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21623 cp_parser_commit_to_tentative_parse (parser);
21624 /* Parse the declarator. */
21625 declarator_token_start = token;
21626 declarator = cp_parser_declarator (parser,
21627 CP_PARSER_DECLARATOR_EITHER,
21628 /*ctor_dtor_or_conv_p=*/NULL,
21630 /*member_p=*/false,
21631 /*friend_p=*/false);
21632 parser->default_arg_ok_p = saved_default_arg_ok_p;
21633 /* After the declarator, allow more attributes. */
21634 decl_specifiers.attributes
21635 = attr_chainon (decl_specifiers.attributes,
21636 cp_parser_attributes_opt (parser));
21638 /* If the declarator is a template parameter pack, remember that and
21639 clear the flag in the declarator itself so we don't get errors
21640 from grokdeclarator. */
21641 if (template_parm_p && declarator && declarator->parameter_pack_p)
21643 declarator->parameter_pack_p = false;
21644 template_parameter_pack_p = true;
21648 /* If the next token is an ellipsis, and we have not seen a declarator
21649 name, and if either the type of the declarator contains parameter
21650 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21651 for, eg, abbreviated integral type names), then we actually have a
21652 parameter pack expansion expression. Otherwise, leave the ellipsis
21653 for a C-style variadic function. */
21654 token = cp_lexer_peek_token (parser->lexer);
21656 /* If a function parameter pack was specified and an implicit template
21657 parameter was introduced during cp_parser_parameter_declaration,
21658 change any implicit parameters introduced into packs. */
21659 if (parser->implicit_template_parms
21660 && ((token->type == CPP_ELLIPSIS
21661 && declarator_can_be_parameter_pack (declarator))
21662 || (declarator && declarator->parameter_pack_p)))
21664 int latest_template_parm_idx = TREE_VEC_LENGTH
21665 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21667 if (latest_template_parm_idx != template_parm_idx)
21668 decl_specifiers.type = convert_generic_types_to_packs
21669 (decl_specifiers.type,
21670 template_parm_idx, latest_template_parm_idx);
21673 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21675 tree type = decl_specifiers.type;
21677 if (type && DECL_P (type))
21678 type = TREE_TYPE (type);
21681 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21682 && (template_parm_p || uses_parameter_packs (type)))
21683 || (!type && template_parm_p))
21684 && declarator_can_be_parameter_pack (declarator))
21686 /* Consume the `...'. */
21687 cp_lexer_consume_token (parser->lexer);
21688 maybe_warn_variadic_templates ();
21690 /* Build a pack expansion type */
21691 if (template_parm_p)
21692 template_parameter_pack_p = true;
21693 else if (declarator)
21694 declarator->parameter_pack_p = true;
21696 decl_specifiers.type = make_pack_expansion (type);
21700 /* The restriction on defining new types applies only to the type
21701 of the parameter, not to the default argument. */
21702 parser->type_definition_forbidden_message = saved_message;
21704 /* If the next token is `=', then process a default argument. */
21705 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21707 tree type = decl_specifiers.type;
21708 token = cp_lexer_peek_token (parser->lexer);
21709 /* If we are defining a class, then the tokens that make up the
21710 default argument must be saved and processed later. */
21711 if (!template_parm_p && at_class_scope_p ()
21712 && TYPE_BEING_DEFINED (current_class_type)
21713 && !LAMBDA_TYPE_P (current_class_type))
21714 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21716 // A constrained-type-specifier may declare a type template-parameter.
21717 else if (declares_constrained_type_template_parameter (type))
21719 = cp_parser_default_type_template_argument (parser);
21721 // A constrained-type-specifier may declare a template-template-parameter.
21722 else if (declares_constrained_template_template_parameter (type))
21724 = cp_parser_default_template_template_argument (parser);
21726 /* Outside of a class definition, we can just parse the
21727 assignment-expression. */
21730 = cp_parser_default_argument (parser, template_parm_p);
21732 if (!parser->default_arg_ok_p)
21734 permerror (token->location,
21735 "default arguments are only "
21736 "permitted for function parameters");
21738 else if ((declarator && declarator->parameter_pack_p)
21739 || template_parameter_pack_p
21740 || (decl_specifiers.type
21741 && PACK_EXPANSION_P (decl_specifiers.type)))
21743 /* Find the name of the parameter pack. */
21744 cp_declarator *id_declarator = declarator;
21745 while (id_declarator && id_declarator->kind != cdk_id)
21746 id_declarator = id_declarator->declarator;
21748 if (id_declarator && id_declarator->kind == cdk_id)
21749 error_at (declarator_token_start->location,
21751 ? G_("template parameter pack %qD "
21752 "cannot have a default argument")
21753 : G_("parameter pack %qD cannot have "
21754 "a default argument"),
21755 id_declarator->u.id.unqualified_name);
21757 error_at (declarator_token_start->location,
21759 ? G_("template parameter pack cannot have "
21760 "a default argument")
21761 : G_("parameter pack cannot have a "
21762 "default argument"));
21764 default_argument = NULL_TREE;
21768 default_argument = NULL_TREE;
21770 /* Generate a location for the parameter, ranging from the start of the
21771 initial token to the end of the final token (using input_location for
21772 the latter, set up by cp_lexer_set_source_position_from_token when
21775 If we have a identifier, then use it for the caret location, e.g.
21777 extern int callee (int one, int (*two)(int, int), float three);
21778 ~~~~~~^~~~~~~~~~~~~~
21780 otherwise, reuse the start location for the caret location e.g.:
21782 extern int callee (int one, int (*)(int, int), float three);
21786 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21787 ? declarator->id_loc
21788 : decl_spec_token_start->location);
21789 location_t param_loc = make_location (caret_loc,
21790 decl_spec_token_start->location,
21793 return make_parameter_declarator (&decl_specifiers,
21797 template_parameter_pack_p);
21800 /* Parse a default argument and return it.
21802 TEMPLATE_PARM_P is true if this is a default argument for a
21803 non-type template parameter. */
21805 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21807 tree default_argument = NULL_TREE;
21808 bool saved_greater_than_is_operator_p;
21809 bool saved_local_variables_forbidden_p;
21810 bool non_constant_p, is_direct_init;
21812 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21814 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21815 parser->greater_than_is_operator_p = !template_parm_p;
21816 /* Local variable names (and the `this' keyword) may not
21817 appear in a default argument. */
21818 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21819 parser->local_variables_forbidden_p = true;
21820 /* Parse the assignment-expression. */
21821 if (template_parm_p)
21822 push_deferring_access_checks (dk_no_deferred);
21823 tree saved_class_ptr = NULL_TREE;
21824 tree saved_class_ref = NULL_TREE;
21825 /* The "this" pointer is not valid in a default argument. */
21828 saved_class_ptr = current_class_ptr;
21829 cp_function_chain->x_current_class_ptr = NULL_TREE;
21830 saved_class_ref = current_class_ref;
21831 cp_function_chain->x_current_class_ref = NULL_TREE;
21834 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21835 /* Restore the "this" pointer. */
21838 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21839 cp_function_chain->x_current_class_ref = saved_class_ref;
21841 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21842 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21843 if (template_parm_p)
21844 pop_deferring_access_checks ();
21845 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21846 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21848 return default_argument;
21851 /* Parse a function-body.
21854 compound_statement */
21857 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21859 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21860 ? BCS_TRY_BLOCK : BCS_NORMAL),
21864 /* Parse a ctor-initializer-opt followed by a function-body. Return
21865 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21866 is true we are parsing a function-try-block. */
21869 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21870 bool in_function_try_block)
21873 const bool check_body_p =
21874 DECL_CONSTRUCTOR_P (current_function_decl)
21875 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21878 /* Begin the function body. */
21879 body = begin_function_body ();
21880 /* Parse the optional ctor-initializer. */
21881 cp_parser_ctor_initializer_opt (parser);
21883 /* If we're parsing a constexpr constructor definition, we need
21884 to check that the constructor body is indeed empty. However,
21885 before we get to cp_parser_function_body lot of junk has been
21886 generated, so we can't just check that we have an empty block.
21887 Rather we take a snapshot of the outermost block, and check whether
21888 cp_parser_function_body changed its state. */
21891 list = cur_stmt_list;
21892 if (STATEMENT_LIST_TAIL (list))
21893 last = STATEMENT_LIST_TAIL (list)->stmt;
21895 /* Parse the function-body. */
21896 cp_parser_function_body (parser, in_function_try_block);
21898 check_constexpr_ctor_body (last, list, /*complain=*/true);
21899 /* Finish the function body. */
21900 finish_function_body (body);
21903 /* Parse an initializer.
21906 = initializer-clause
21907 ( expression-list )
21909 Returns an expression representing the initializer. If no
21910 initializer is present, NULL_TREE is returned.
21912 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21913 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21914 set to TRUE if there is no initializer present. If there is an
21915 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21916 is set to true; otherwise it is set to false. */
21919 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21920 bool* non_constant_p, bool subexpression_p)
21925 /* Peek at the next token. */
21926 token = cp_lexer_peek_token (parser->lexer);
21928 /* Let our caller know whether or not this initializer was
21930 *is_direct_init = (token->type != CPP_EQ);
21931 /* Assume that the initializer is constant. */
21932 *non_constant_p = false;
21934 if (token->type == CPP_EQ)
21936 /* Consume the `='. */
21937 cp_lexer_consume_token (parser->lexer);
21938 /* Parse the initializer-clause. */
21939 init = cp_parser_initializer_clause (parser, non_constant_p);
21941 else if (token->type == CPP_OPEN_PAREN)
21943 vec<tree, va_gc> *vec;
21944 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21946 /*allow_expansion_p=*/true,
21949 return error_mark_node;
21950 init = build_tree_list_vec (vec);
21951 release_tree_vector (vec);
21953 else if (token->type == CPP_OPEN_BRACE)
21955 cp_lexer_set_source_position (parser->lexer);
21956 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21957 init = cp_parser_braced_list (parser, non_constant_p);
21958 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21962 /* Anything else is an error. */
21963 cp_parser_error (parser, "expected initializer");
21964 init = error_mark_node;
21967 if (!subexpression_p && check_for_bare_parameter_packs (init))
21968 init = error_mark_node;
21973 /* Parse an initializer-clause.
21975 initializer-clause:
21976 assignment-expression
21979 Returns an expression representing the initializer.
21981 If the `assignment-expression' production is used the value
21982 returned is simply a representation for the expression.
21984 Otherwise, calls cp_parser_braced_list. */
21987 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21989 cp_expr initializer;
21991 /* Assume the expression is constant. */
21992 *non_constant_p = false;
21994 /* If it is not a `{', then we are looking at an
21995 assignment-expression. */
21996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21999 = cp_parser_constant_expression (parser,
22000 /*allow_non_constant_p=*/true,
22004 initializer = cp_parser_braced_list (parser, non_constant_p);
22006 return initializer;
22009 /* Parse a brace-enclosed initializer list.
22012 { initializer-list , [opt] }
22013 { designated-initializer-list , [opt] }
22016 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22017 the elements of the initializer-list (or NULL, if the last
22018 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22019 NULL_TREE. There is no way to detect whether or not the optional
22020 trailing `,' was provided. NON_CONSTANT_P is as for
22021 cp_parser_initializer. */
22024 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22027 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22029 /* Consume the `{' token. */
22030 matching_braces braces;
22031 braces.require_open (parser);
22032 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22033 initializer = make_node (CONSTRUCTOR);
22034 /* If it's not a `}', then there is a non-trivial initializer. */
22035 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22037 /* Parse the initializer list. */
22038 CONSTRUCTOR_ELTS (initializer)
22039 = cp_parser_initializer_list (parser, non_constant_p);
22040 /* A trailing `,' token is allowed. */
22041 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22042 cp_lexer_consume_token (parser->lexer);
22045 *non_constant_p = false;
22046 /* Now, there should be a trailing `}'. */
22047 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22048 braces.require_close (parser);
22049 TREE_TYPE (initializer) = init_list_type_node;
22051 cp_expr result (initializer);
22052 /* Build a location of the form:
22055 with caret==start at the open brace, finish at the close brace. */
22056 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22057 result.set_location (combined_loc);
22061 /* Consume tokens up to, and including, the next non-nested closing `]'.
22062 Returns true iff we found a closing `]'. */
22065 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22067 unsigned square_depth = 0;
22071 cp_token * token = cp_lexer_peek_token (parser->lexer);
22073 switch (token->type)
22076 case CPP_PRAGMA_EOL:
22077 /* If we've run out of tokens, then there is no closing `]'. */
22080 case CPP_OPEN_SQUARE:
22084 case CPP_CLOSE_SQUARE:
22085 if (!square_depth--)
22087 cp_lexer_consume_token (parser->lexer);
22096 /* Consume the token. */
22097 cp_lexer_consume_token (parser->lexer);
22101 /* Return true if we are looking at an array-designator, false otherwise. */
22104 cp_parser_array_designator_p (cp_parser *parser)
22106 /* Consume the `['. */
22107 cp_lexer_consume_token (parser->lexer);
22109 cp_lexer_save_tokens (parser->lexer);
22111 /* Skip tokens until the next token is a closing square bracket.
22112 If we find the closing `]', and the next token is a `=', then
22113 we are looking at an array designator. */
22114 bool array_designator_p
22115 = (cp_parser_skip_to_closing_square_bracket (parser)
22116 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22118 /* Roll back the tokens we skipped. */
22119 cp_lexer_rollback_tokens (parser->lexer);
22121 return array_designator_p;
22124 /* Parse an initializer-list.
22127 initializer-clause ... [opt]
22128 initializer-list , initializer-clause ... [opt]
22132 designated-initializer-list:
22133 designated-initializer-clause
22134 designated-initializer-list , designated-initializer-clause
22136 designated-initializer-clause:
22137 designator brace-or-equal-initializer
22145 designation initializer-clause ...[opt]
22146 initializer-list , designation initializer-clause ...[opt]
22151 [ constant-expression ] =
22153 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22154 for the initializer. If the INDEX of the elt is non-NULL, it is the
22155 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22156 as for cp_parser_initializer. */
22158 static vec<constructor_elt, va_gc> *
22159 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22161 vec<constructor_elt, va_gc> *v = NULL;
22162 bool first_p = true;
22163 tree first_designator = NULL_TREE;
22165 /* Assume all of the expressions are constant. */
22166 *non_constant_p = false;
22168 /* Parse the rest of the list. */
22174 bool clause_non_constant_p;
22175 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22177 /* Handle the C++2A syntax, '. id ='. */
22178 if ((cxx_dialect >= cxx2a
22179 || cp_parser_allow_gnu_extensions_p (parser))
22180 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22181 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22182 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22183 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22184 == CPP_OPEN_BRACE)))
22186 if (cxx_dialect < cxx2a)
22187 pedwarn (loc, OPT_Wpedantic,
22188 "C++ designated initializers only available with "
22189 "-std=c++2a or -std=gnu++2a");
22190 /* Consume the `.'. */
22191 cp_lexer_consume_token (parser->lexer);
22192 /* Consume the identifier. */
22193 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22194 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22195 /* Consume the `='. */
22196 cp_lexer_consume_token (parser->lexer);
22198 /* Also, if the next token is an identifier and the following one is a
22199 colon, we are looking at the GNU designated-initializer
22201 else if (cp_parser_allow_gnu_extensions_p (parser)
22202 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22203 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22206 /* Warn the user that they are using an extension. */
22207 pedwarn (loc, OPT_Wpedantic,
22208 "ISO C++ does not allow GNU designated initializers");
22209 /* Consume the identifier. */
22210 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22211 /* Consume the `:'. */
22212 cp_lexer_consume_token (parser->lexer);
22214 /* Also handle C99 array designators, '[ const ] ='. */
22215 else if (cp_parser_allow_gnu_extensions_p (parser)
22216 && !c_dialect_objc ()
22217 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22219 /* In C++11, [ could start a lambda-introducer. */
22220 bool non_const = false;
22222 cp_parser_parse_tentatively (parser);
22224 if (!cp_parser_array_designator_p (parser))
22226 cp_parser_simulate_error (parser);
22227 designator = NULL_TREE;
22231 designator = cp_parser_constant_expression (parser, true,
22233 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22234 cp_parser_require (parser, CPP_EQ, RT_EQ);
22237 if (!cp_parser_parse_definitely (parser))
22238 designator = NULL_TREE;
22240 && (!require_potential_rvalue_constant_expression
22242 designator = NULL_TREE;
22244 /* Warn the user that they are using an extension. */
22245 pedwarn (loc, OPT_Wpedantic,
22246 "ISO C++ does not allow C99 designated initializers");
22249 designator = NULL_TREE;
22253 first_designator = designator;
22256 else if (cxx_dialect >= cxx2a
22257 && first_designator != error_mark_node
22258 && (!first_designator != !designator))
22260 error_at (loc, "either all initializer clauses should be designated "
22261 "or none of them should be");
22262 first_designator = error_mark_node;
22264 else if (cxx_dialect < cxx2a && !first_designator)
22265 first_designator = designator;
22267 /* Parse the initializer. */
22268 initializer = cp_parser_initializer_clause (parser,
22269 &clause_non_constant_p);
22270 /* If any clause is non-constant, so is the entire initializer. */
22271 if (clause_non_constant_p)
22272 *non_constant_p = true;
22274 /* If we have an ellipsis, this is an initializer pack
22276 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22278 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22280 /* Consume the `...'. */
22281 cp_lexer_consume_token (parser->lexer);
22283 if (designator && cxx_dialect >= cxx2a)
22285 "%<...%> not allowed in designated initializer list");
22287 /* Turn the initializer into an initializer expansion. */
22288 initializer = make_pack_expansion (initializer);
22291 /* Add it to the vector. */
22292 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22294 /* If the next token is not a comma, we have reached the end of
22296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22299 /* Peek at the next token. */
22300 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22301 /* If the next token is a `}', then we're still done. An
22302 initializer-clause can have a trailing `,' after the
22303 initializer-list and before the closing `}'. */
22304 if (token->type == CPP_CLOSE_BRACE)
22307 /* Consume the `,' token. */
22308 cp_lexer_consume_token (parser->lexer);
22311 /* The same identifier shall not appear in multiple designators
22312 of a designated-initializer-list. */
22313 if (first_designator)
22316 tree designator, val;
22317 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22318 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22320 if (IDENTIFIER_MARKED (designator))
22322 error_at (EXPR_LOC_OR_LOC (val, input_location),
22323 "%<.%s%> designator used multiple times in "
22324 "the same initializer list",
22325 IDENTIFIER_POINTER (designator));
22326 (*v)[i].index = NULL_TREE;
22329 IDENTIFIER_MARKED (designator) = 1;
22331 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22332 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22333 IDENTIFIER_MARKED (designator) = 0;
22339 /* Classes [gram.class] */
22341 /* Parse a class-name.
22347 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22348 to indicate that names looked up in dependent types should be
22349 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22350 keyword has been used to indicate that the name that appears next
22351 is a template. TAG_TYPE indicates the explicit tag given before
22352 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22353 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22354 is the class being defined in a class-head. If ENUM_OK is TRUE,
22355 enum-names are also accepted.
22357 Returns the TYPE_DECL representing the class. */
22360 cp_parser_class_name (cp_parser *parser,
22361 bool typename_keyword_p,
22362 bool template_keyword_p,
22363 enum tag_types tag_type,
22364 bool check_dependency_p,
22366 bool is_declaration,
22373 tree identifier = NULL_TREE;
22375 /* All class-names start with an identifier. */
22376 token = cp_lexer_peek_token (parser->lexer);
22377 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22379 cp_parser_error (parser, "expected class-name");
22380 return error_mark_node;
22383 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22384 to a template-id, so we save it here. */
22385 scope = parser->scope;
22386 if (scope == error_mark_node)
22387 return error_mark_node;
22389 /* Any name names a type if we're following the `typename' keyword
22390 in a qualified name where the enclosing scope is type-dependent. */
22391 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22392 && dependent_type_p (scope));
22393 /* Handle the common case (an identifier, but not a template-id)
22395 if (token->type == CPP_NAME
22396 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22398 cp_token *identifier_token;
22401 /* Look for the identifier. */
22402 identifier_token = cp_lexer_peek_token (parser->lexer);
22403 ambiguous_p = identifier_token->error_reported;
22404 identifier = cp_parser_identifier (parser);
22405 /* If the next token isn't an identifier, we are certainly not
22406 looking at a class-name. */
22407 if (identifier == error_mark_node)
22408 decl = error_mark_node;
22409 /* If we know this is a type-name, there's no need to look it
22411 else if (typename_p)
22415 tree ambiguous_decls;
22416 /* If we already know that this lookup is ambiguous, then
22417 we've already issued an error message; there's no reason
22421 cp_parser_simulate_error (parser);
22422 return error_mark_node;
22424 /* If the next token is a `::', then the name must be a type
22427 [basic.lookup.qual]
22429 During the lookup for a name preceding the :: scope
22430 resolution operator, object, function, and enumerator
22431 names are ignored. */
22432 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22433 tag_type = scope_type;
22434 /* Look up the name. */
22435 decl = cp_parser_lookup_name (parser, identifier,
22437 /*is_template=*/false,
22438 /*is_namespace=*/false,
22439 check_dependency_p,
22441 identifier_token->location);
22442 if (ambiguous_decls)
22444 if (cp_parser_parsing_tentatively (parser))
22445 cp_parser_simulate_error (parser);
22446 return error_mark_node;
22452 /* Try a template-id. */
22453 decl = cp_parser_template_id (parser, template_keyword_p,
22454 check_dependency_p,
22457 if (decl == error_mark_node)
22458 return error_mark_node;
22461 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22463 /* If this is a typename, create a TYPENAME_TYPE. */
22464 if (typename_p && decl != error_mark_node)
22466 decl = make_typename_type (scope, decl, typename_type,
22467 /*complain=*/tf_error);
22468 if (decl != error_mark_node)
22469 decl = TYPE_NAME (decl);
22472 decl = strip_using_decl (decl);
22474 /* Check to see that it is really the name of a class. */
22475 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22476 && identifier_p (TREE_OPERAND (decl, 0))
22477 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22478 /* Situations like this:
22480 template <typename T> struct A {
22481 typename T::template X<int>::I i;
22484 are problematic. Is `T::template X<int>' a class-name? The
22485 standard does not seem to be definitive, but there is no other
22486 valid interpretation of the following `::'. Therefore, those
22487 names are considered class-names. */
22489 decl = make_typename_type (scope, decl, tag_type, tf_error);
22490 if (decl != error_mark_node)
22491 decl = TYPE_NAME (decl);
22493 else if (TREE_CODE (decl) != TYPE_DECL
22494 || TREE_TYPE (decl) == error_mark_node
22495 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22496 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22497 /* In Objective-C 2.0, a classname followed by '.' starts a
22498 dot-syntax expression, and it's not a type-name. */
22499 || (c_dialect_objc ()
22500 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22501 && objc_is_class_name (decl)))
22502 decl = error_mark_node;
22504 if (decl == error_mark_node)
22505 cp_parser_error (parser, "expected class-name");
22506 else if (identifier && !parser->scope)
22507 maybe_note_name_used_in_class (identifier, decl);
22512 /* Parse a class-specifier.
22515 class-head { member-specification [opt] }
22517 Returns the TREE_TYPE representing the class. */
22520 cp_parser_class_specifier_1 (cp_parser* parser)
22523 tree attributes = NULL_TREE;
22524 bool nested_name_specifier_p;
22525 unsigned saved_num_template_parameter_lists;
22526 bool saved_in_function_body;
22527 unsigned char in_statement;
22528 bool in_switch_statement_p;
22529 bool saved_in_unbraced_linkage_specification_p;
22530 tree old_scope = NULL_TREE;
22531 tree scope = NULL_TREE;
22532 cp_token *closing_brace;
22534 push_deferring_access_checks (dk_no_deferred);
22536 /* Parse the class-head. */
22537 type = cp_parser_class_head (parser,
22538 &nested_name_specifier_p);
22539 /* If the class-head was a semantic disaster, skip the entire body
22543 cp_parser_skip_to_end_of_block_or_statement (parser);
22544 pop_deferring_access_checks ();
22545 return error_mark_node;
22548 /* Look for the `{'. */
22549 matching_braces braces;
22550 if (!braces.require_open (parser))
22552 pop_deferring_access_checks ();
22553 return error_mark_node;
22556 cp_ensure_no_omp_declare_simd (parser);
22557 cp_ensure_no_oacc_routine (parser);
22559 /* Issue an error message if type-definitions are forbidden here. */
22560 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
22561 /* Remember that we are defining one more class. */
22562 ++parser->num_classes_being_defined;
22563 /* Inside the class, surrounding template-parameter-lists do not
22565 saved_num_template_parameter_lists
22566 = parser->num_template_parameter_lists;
22567 parser->num_template_parameter_lists = 0;
22568 /* We are not in a function body. */
22569 saved_in_function_body = parser->in_function_body;
22570 parser->in_function_body = false;
22571 /* Or in a loop. */
22572 in_statement = parser->in_statement;
22573 parser->in_statement = 0;
22574 /* Or in a switch. */
22575 in_switch_statement_p = parser->in_switch_statement_p;
22576 parser->in_switch_statement_p = false;
22577 /* We are not immediately inside an extern "lang" block. */
22578 saved_in_unbraced_linkage_specification_p
22579 = parser->in_unbraced_linkage_specification_p;
22580 parser->in_unbraced_linkage_specification_p = false;
22582 // Associate constraints with the type.
22584 type = associate_classtype_constraints (type);
22586 /* Start the class. */
22587 if (nested_name_specifier_p)
22589 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22590 old_scope = push_inner_scope (scope);
22592 type = begin_class_definition (type);
22594 if (type == error_mark_node)
22595 /* If the type is erroneous, skip the entire body of the class. */
22596 cp_parser_skip_to_closing_brace (parser);
22598 /* Parse the member-specification. */
22599 cp_parser_member_specification_opt (parser);
22601 /* Look for the trailing `}'. */
22602 closing_brace = braces.require_close (parser);
22603 /* Look for trailing attributes to apply to this class. */
22604 if (cp_parser_allow_gnu_extensions_p (parser))
22605 attributes = cp_parser_gnu_attributes_opt (parser);
22606 if (type != error_mark_node)
22607 type = finish_struct (type, attributes);
22608 if (nested_name_specifier_p)
22609 pop_inner_scope (old_scope, scope);
22611 /* We've finished a type definition. Check for the common syntax
22612 error of forgetting a semicolon after the definition. We need to
22613 be careful, as we can't just check for not-a-semicolon and be done
22614 with it; the user might have typed:
22616 class X { } c = ...;
22617 class X { } *p = ...;
22619 and so forth. Instead, enumerate all the possible tokens that
22620 might follow this production; if we don't see one of them, then
22621 complain and silently insert the semicolon. */
22623 cp_token *token = cp_lexer_peek_token (parser->lexer);
22624 bool want_semicolon = true;
22626 if (cp_next_tokens_can_be_std_attribute_p (parser))
22627 /* Don't try to parse c++11 attributes here. As per the
22628 grammar, that should be a task for
22629 cp_parser_decl_specifier_seq. */
22630 want_semicolon = false;
22632 switch (token->type)
22635 case CPP_SEMICOLON:
22638 case CPP_OPEN_PAREN:
22639 case CPP_CLOSE_PAREN:
22641 want_semicolon = false;
22644 /* While it's legal for type qualifiers and storage class
22645 specifiers to follow type definitions in the grammar, only
22646 compiler testsuites contain code like that. Assume that if
22647 we see such code, then what we're really seeing is a case
22651 const <type> var = ...;
22656 static <type> func (...) ...
22658 i.e. the qualifier or specifier applies to the next
22659 declaration. To do so, however, we need to look ahead one
22660 more token to see if *that* token is a type specifier.
22662 This code could be improved to handle:
22665 static const <type> var = ...; */
22667 if (keyword_is_decl_specifier (token->keyword))
22669 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22671 /* Handling user-defined types here would be nice, but very
22674 = (lookahead->type == CPP_KEYWORD
22675 && keyword_begins_type_specifier (lookahead->keyword));
22682 /* If we don't have a type, then something is very wrong and we
22683 shouldn't try to do anything clever. Likewise for not seeing the
22685 if (closing_brace && TYPE_P (type) && want_semicolon)
22687 /* Locate the closing brace. */
22688 cp_token_position prev
22689 = cp_lexer_previous_token_position (parser->lexer);
22690 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22691 location_t loc = prev_token->location;
22693 /* We want to suggest insertion of a ';' immediately *after* the
22694 closing brace, so, if we can, offset the location by 1 column. */
22695 location_t next_loc = loc;
22696 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22697 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22699 rich_location richloc (line_table, next_loc);
22701 /* If we successfully offset the location, suggest the fix-it. */
22702 if (next_loc != loc)
22703 richloc.add_fixit_insert_before (next_loc, ";");
22705 if (CLASSTYPE_DECLARED_CLASS (type))
22706 error_at (&richloc,
22707 "expected %<;%> after class definition");
22708 else if (TREE_CODE (type) == RECORD_TYPE)
22709 error_at (&richloc,
22710 "expected %<;%> after struct definition");
22711 else if (TREE_CODE (type) == UNION_TYPE)
22712 error_at (&richloc,
22713 "expected %<;%> after union definition");
22715 gcc_unreachable ();
22717 /* Unget one token and smash it to look as though we encountered
22718 a semicolon in the input stream. */
22719 cp_lexer_set_token_position (parser->lexer, prev);
22720 token = cp_lexer_peek_token (parser->lexer);
22721 token->type = CPP_SEMICOLON;
22722 token->keyword = RID_MAX;
22726 /* If this class is not itself within the scope of another class,
22727 then we need to parse the bodies of all of the queued function
22728 definitions. Note that the queued functions defined in a class
22729 are not always processed immediately following the
22730 class-specifier for that class. Consider:
22733 struct B { void f() { sizeof (A); } };
22736 If `f' were processed before the processing of `A' were
22737 completed, there would be no way to compute the size of `A'.
22738 Note that the nesting we are interested in here is lexical --
22739 not the semantic nesting given by TYPE_CONTEXT. In particular,
22742 struct A { struct B; };
22743 struct A::B { void f() { } };
22745 there is no need to delay the parsing of `A::B::f'. */
22746 if (--parser->num_classes_being_defined == 0)
22749 tree class_type = NULL_TREE;
22750 tree pushed_scope = NULL_TREE;
22752 cp_default_arg_entry *e;
22753 tree save_ccp, save_ccr;
22755 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
22757 /* Skip default arguments, NSDMIs, etc, in order to improve
22758 error recovery (c++/71169, c++/71832). */
22759 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22760 vec_safe_truncate (unparsed_nsdmis, 0);
22761 vec_safe_truncate (unparsed_classes, 0);
22762 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22765 /* In a first pass, parse default arguments to the functions.
22766 Then, in a second pass, parse the bodies of the functions.
22767 This two-phased approach handles cases like:
22775 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22778 /* If there are default arguments that have not yet been processed,
22779 take care of them now. */
22780 if (class_type != e->class_type)
22783 pop_scope (pushed_scope);
22784 class_type = e->class_type;
22785 pushed_scope = push_scope (class_type);
22787 /* Make sure that any template parameters are in scope. */
22788 maybe_begin_member_template_processing (decl);
22789 /* Parse the default argument expressions. */
22790 cp_parser_late_parsing_default_args (parser, decl);
22791 /* Remove any template parameters from the symbol table. */
22792 maybe_end_member_template_processing ();
22794 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22795 /* Now parse any NSDMIs. */
22796 save_ccp = current_class_ptr;
22797 save_ccr = current_class_ref;
22798 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22800 if (class_type != DECL_CONTEXT (decl))
22803 pop_scope (pushed_scope);
22804 class_type = DECL_CONTEXT (decl);
22805 pushed_scope = push_scope (class_type);
22807 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22808 cp_parser_late_parsing_nsdmi (parser, decl);
22810 vec_safe_truncate (unparsed_nsdmis, 0);
22811 current_class_ptr = save_ccp;
22812 current_class_ref = save_ccr;
22814 pop_scope (pushed_scope);
22816 /* Now do some post-NSDMI bookkeeping. */
22817 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22818 after_nsdmi_defaulted_late_checks (class_type);
22819 vec_safe_truncate (unparsed_classes, 0);
22820 after_nsdmi_defaulted_late_checks (type);
22822 /* Now parse the body of the functions. */
22825 /* OpenMP UDRs need to be parsed before all other functions. */
22826 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22827 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22828 cp_parser_late_parsing_for_member (parser, decl);
22829 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22830 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22831 cp_parser_late_parsing_for_member (parser, decl);
22834 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22835 cp_parser_late_parsing_for_member (parser, decl);
22836 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22839 vec_safe_push (unparsed_classes, type);
22841 /* Put back any saved access checks. */
22842 pop_deferring_access_checks ();
22844 /* Restore saved state. */
22845 parser->in_switch_statement_p = in_switch_statement_p;
22846 parser->in_statement = in_statement;
22847 parser->in_function_body = saved_in_function_body;
22848 parser->num_template_parameter_lists
22849 = saved_num_template_parameter_lists;
22850 parser->in_unbraced_linkage_specification_p
22851 = saved_in_unbraced_linkage_specification_p;
22857 cp_parser_class_specifier (cp_parser* parser)
22860 timevar_push (TV_PARSE_STRUCT);
22861 ret = cp_parser_class_specifier_1 (parser);
22862 timevar_pop (TV_PARSE_STRUCT);
22866 /* Parse a class-head.
22869 class-key identifier [opt] base-clause [opt]
22870 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22871 class-key nested-name-specifier [opt] template-id
22874 class-virt-specifier:
22878 class-key attributes identifier [opt] base-clause [opt]
22879 class-key attributes nested-name-specifier identifier base-clause [opt]
22880 class-key attributes nested-name-specifier [opt] template-id
22883 Upon return BASES is initialized to the list of base classes (or
22884 NULL, if there are none) in the same form returned by
22885 cp_parser_base_clause.
22887 Returns the TYPE of the indicated class. Sets
22888 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22889 involving a nested-name-specifier was used, and FALSE otherwise.
22891 Returns error_mark_node if this is not a class-head.
22893 Returns NULL_TREE if the class-head is syntactically valid, but
22894 semantically invalid in a way that means we should skip the entire
22895 body of the class. */
22898 cp_parser_class_head (cp_parser* parser,
22899 bool* nested_name_specifier_p)
22901 tree nested_name_specifier;
22902 enum tag_types class_key;
22903 tree id = NULL_TREE;
22904 tree type = NULL_TREE;
22907 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22908 bool template_id_p = false;
22909 bool qualified_p = false;
22910 bool invalid_nested_name_p = false;
22911 bool invalid_explicit_specialization_p = false;
22912 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22913 tree pushed_scope = NULL_TREE;
22914 unsigned num_templates;
22915 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22916 /* Assume no nested-name-specifier will be present. */
22917 *nested_name_specifier_p = false;
22918 /* Assume no template parameter lists will be used in defining the
22921 parser->colon_corrects_to_scope_p = false;
22923 /* Look for the class-key. */
22924 class_key = cp_parser_class_key (parser);
22925 if (class_key == none_type)
22926 return error_mark_node;
22928 location_t class_head_start_location = input_location;
22930 /* Parse the attributes. */
22931 attributes = cp_parser_attributes_opt (parser);
22933 /* If the next token is `::', that is invalid -- but sometimes
22934 people do try to write:
22938 Handle this gracefully by accepting the extra qualifier, and then
22939 issuing an error about it later if this really is a
22940 class-head. If it turns out just to be an elaborated type
22941 specifier, remain silent. */
22942 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22943 qualified_p = true;
22945 push_deferring_access_checks (dk_no_check);
22947 /* Determine the name of the class. Begin by looking for an
22948 optional nested-name-specifier. */
22949 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22950 nested_name_specifier
22951 = cp_parser_nested_name_specifier_opt (parser,
22952 /*typename_keyword_p=*/false,
22953 /*check_dependency_p=*/false,
22955 /*is_declaration=*/false);
22956 /* If there was a nested-name-specifier, then there *must* be an
22959 cp_token *bad_template_keyword = NULL;
22961 if (nested_name_specifier)
22963 type_start_token = cp_lexer_peek_token (parser->lexer);
22964 /* Although the grammar says `identifier', it really means
22965 `class-name' or `template-name'. You are only allowed to
22966 define a class that has already been declared with this
22969 The proposed resolution for Core Issue 180 says that wherever
22970 you see `class T::X' you should treat `X' as a type-name.
22972 It is OK to define an inaccessible class; for example:
22974 class A { class B; };
22977 We do not know if we will see a class-name, or a
22978 template-name. We look for a class-name first, in case the
22979 class-name is a template-id; if we looked for the
22980 template-name first we would stop after the template-name. */
22981 cp_parser_parse_tentatively (parser);
22982 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22983 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22984 type = cp_parser_class_name (parser,
22985 /*typename_keyword_p=*/false,
22986 /*template_keyword_p=*/false,
22988 /*check_dependency_p=*/false,
22989 /*class_head_p=*/true,
22990 /*is_declaration=*/false);
22991 /* If that didn't work, ignore the nested-name-specifier. */
22992 if (!cp_parser_parse_definitely (parser))
22994 invalid_nested_name_p = true;
22995 type_start_token = cp_lexer_peek_token (parser->lexer);
22996 id = cp_parser_identifier (parser);
22997 if (id == error_mark_node)
23000 /* If we could not find a corresponding TYPE, treat this
23001 declaration like an unqualified declaration. */
23002 if (type == error_mark_node)
23003 nested_name_specifier = NULL_TREE;
23004 /* Otherwise, count the number of templates used in TYPE and its
23005 containing scopes. */
23010 for (scope = TREE_TYPE (type);
23011 scope && TREE_CODE (scope) != NAMESPACE_DECL;
23012 scope = get_containing_scope (scope))
23014 && CLASS_TYPE_P (scope)
23015 && CLASSTYPE_TEMPLATE_INFO (scope)
23016 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
23017 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
23018 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
23022 /* Otherwise, the identifier is optional. */
23025 /* We don't know whether what comes next is a template-id,
23026 an identifier, or nothing at all. */
23027 cp_parser_parse_tentatively (parser);
23028 /* Check for a template-id. */
23029 type_start_token = cp_lexer_peek_token (parser->lexer);
23030 id = cp_parser_template_id (parser,
23031 /*template_keyword_p=*/false,
23032 /*check_dependency_p=*/true,
23034 /*is_declaration=*/true);
23035 /* If that didn't work, it could still be an identifier. */
23036 if (!cp_parser_parse_definitely (parser))
23038 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23040 type_start_token = cp_lexer_peek_token (parser->lexer);
23041 id = cp_parser_identifier (parser);
23048 template_id_p = true;
23053 pop_deferring_access_checks ();
23057 cp_parser_check_for_invalid_template_id (parser, id,
23059 type_start_token->location);
23061 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23063 /* If it's not a `:' or a `{' then we can't really be looking at a
23064 class-head, since a class-head only appears as part of a
23065 class-specifier. We have to detect this situation before calling
23066 xref_tag, since that has irreversible side-effects. */
23067 if (!cp_parser_next_token_starts_class_definition_p (parser))
23069 cp_parser_error (parser, "expected %<{%> or %<:%>");
23070 type = error_mark_node;
23074 /* At this point, we're going ahead with the class-specifier, even
23075 if some other problem occurs. */
23076 cp_parser_commit_to_tentative_parse (parser);
23077 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23079 cp_parser_error (parser,
23080 "cannot specify %<override%> for a class");
23081 type = error_mark_node;
23084 /* Issue the error about the overly-qualified name now. */
23087 cp_parser_error (parser,
23088 "global qualification of class name is invalid");
23089 type = error_mark_node;
23092 else if (invalid_nested_name_p)
23094 cp_parser_error (parser,
23095 "qualified name does not name a class");
23096 type = error_mark_node;
23099 else if (nested_name_specifier)
23103 if (bad_template_keyword)
23104 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23105 keyword template shall not appear at the top level. */
23106 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23107 "keyword %<template%> not allowed in class-head-name");
23109 /* Reject typedef-names in class heads. */
23110 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23112 error_at (type_start_token->location,
23113 "invalid class name in declaration of %qD",
23119 /* Figure out in what scope the declaration is being placed. */
23120 scope = current_scope ();
23121 /* If that scope does not contain the scope in which the
23122 class was originally declared, the program is invalid. */
23123 if (scope && !is_ancestor (scope, nested_name_specifier))
23125 if (at_namespace_scope_p ())
23126 error_at (type_start_token->location,
23127 "declaration of %qD in namespace %qD which does not "
23129 type, scope, nested_name_specifier);
23131 error_at (type_start_token->location,
23132 "declaration of %qD in %qD which does not enclose %qD",
23133 type, scope, nested_name_specifier);
23139 A declarator-id shall not be qualified except for the
23140 definition of a ... nested class outside of its class
23141 ... [or] the definition or explicit instantiation of a
23142 class member of a namespace outside of its namespace. */
23143 if (scope == nested_name_specifier)
23145 permerror (nested_name_specifier_token_start->location,
23146 "extra qualification not allowed");
23147 nested_name_specifier = NULL_TREE;
23151 /* An explicit-specialization must be preceded by "template <>". If
23152 it is not, try to recover gracefully. */
23153 if (at_namespace_scope_p ()
23154 && parser->num_template_parameter_lists == 0
23155 && !processing_template_parmlist
23158 /* Build a location of this form:
23159 struct typename <ARGS>
23160 ^~~~~~~~~~~~~~~~~~~~~~
23161 with caret==start at the start token, and
23162 finishing at the end of the type. */
23163 location_t reported_loc
23164 = make_location (class_head_start_location,
23165 class_head_start_location,
23166 get_finish (type_start_token->location));
23167 rich_location richloc (line_table, reported_loc);
23168 richloc.add_fixit_insert_before (class_head_start_location,
23170 error_at (&richloc,
23171 "an explicit specialization must be preceded by"
23172 " %<template <>%>");
23173 invalid_explicit_specialization_p = true;
23174 /* Take the same action that would have been taken by
23175 cp_parser_explicit_specialization. */
23176 ++parser->num_template_parameter_lists;
23177 begin_specialization ();
23179 /* There must be no "return" statements between this point and the
23180 end of this function; set "type "to the correct return value and
23181 use "goto done;" to return. */
23182 /* Make sure that the right number of template parameters were
23184 if (!cp_parser_check_template_parameters (parser, num_templates,
23186 type_start_token->location,
23187 /*declarator=*/NULL))
23189 /* If something went wrong, there is no point in even trying to
23190 process the class-definition. */
23195 /* Look up the type. */
23198 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23199 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23200 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23202 error_at (type_start_token->location,
23203 "function template %qD redeclared as a class template", id);
23204 type = error_mark_node;
23208 type = TREE_TYPE (id);
23209 type = maybe_process_partial_specialization (type);
23211 /* Check the scope while we still know whether or not we had a
23212 nested-name-specifier. */
23213 if (type != error_mark_node)
23214 check_unqualified_spec_or_inst (type, type_start_token->location);
23216 if (nested_name_specifier)
23217 pushed_scope = push_scope (nested_name_specifier);
23219 else if (nested_name_specifier)
23225 template <typename T> struct S { struct T };
23226 template <typename T> struct S<T>::T { };
23228 we will get a TYPENAME_TYPE when processing the definition of
23229 `S::T'. We need to resolve it to the actual type before we
23230 try to define it. */
23231 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23233 class_type = resolve_typename_type (TREE_TYPE (type),
23234 /*only_current_p=*/false);
23235 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23236 type = TYPE_NAME (class_type);
23239 cp_parser_error (parser, "could not resolve typename type");
23240 type = error_mark_node;
23244 if (maybe_process_partial_specialization (TREE_TYPE (type))
23245 == error_mark_node)
23251 class_type = current_class_type;
23252 /* Enter the scope indicated by the nested-name-specifier. */
23253 pushed_scope = push_scope (nested_name_specifier);
23254 /* Get the canonical version of this type. */
23255 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23256 /* Call push_template_decl if it seems like we should be defining a
23257 template either from the template headers or the type we're
23258 defining, so that we diagnose both extra and missing headers. */
23259 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23260 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23261 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23263 type = push_template_decl (type);
23264 if (type == error_mark_node)
23271 type = TREE_TYPE (type);
23272 *nested_name_specifier_p = true;
23274 else /* The name is not a nested name. */
23276 /* If the class was unnamed, create a dummy name. */
23278 id = make_anon_name ();
23279 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23280 ? ts_within_enclosing_non_class
23282 type = xref_tag (class_key, id, tag_scope,
23283 parser->num_template_parameter_lists);
23286 /* Indicate whether this class was declared as a `class' or as a
23288 if (TREE_CODE (type) == RECORD_TYPE)
23289 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23290 cp_parser_check_class_key (class_key, type);
23292 /* If this type was already complete, and we see another definition,
23293 that's an error. */
23294 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23296 error_at (type_start_token->location, "redefinition of %q#T",
23298 inform (location_of (type), "previous definition of %q#T",
23303 else if (type == error_mark_node)
23308 /* Apply attributes now, before any use of the class as a template
23309 argument in its base list. */
23310 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23311 fixup_attribute_variants (type);
23314 /* We will have entered the scope containing the class; the names of
23315 base classes should be looked up in that context. For example:
23317 struct A { struct B {}; struct C; };
23318 struct A::C : B {};
23322 /* Get the list of base-classes, if there is one. */
23323 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23325 /* PR59482: enter the class scope so that base-specifiers are looked
23329 bases = cp_parser_base_clause (parser);
23330 /* PR59482: get out of the previously pushed class scope so that the
23331 subsequent pops pop the right thing. */
23338 /* If we're really defining a class, process the base classes.
23339 If they're invalid, fail. */
23340 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23341 xref_basetypes (type, bases);
23344 /* Leave the scope given by the nested-name-specifier. We will
23345 enter the class scope itself while processing the members. */
23347 pop_scope (pushed_scope);
23349 if (invalid_explicit_specialization_p)
23351 end_specialization ();
23352 --parser->num_template_parameter_lists;
23356 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23357 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23358 CLASSTYPE_FINAL (type) = 1;
23360 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23364 /* Parse a class-key.
23371 Returns the kind of class-key specified, or none_type to indicate
23374 static enum tag_types
23375 cp_parser_class_key (cp_parser* parser)
23378 enum tag_types tag_type;
23380 /* Look for the class-key. */
23381 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23385 /* Check to see if the TOKEN is a class-key. */
23386 tag_type = cp_parser_token_is_class_key (token);
23388 cp_parser_error (parser, "expected class-key");
23392 /* Parse a type-parameter-key.
23394 type-parameter-key:
23400 cp_parser_type_parameter_key (cp_parser* parser)
23402 /* Look for the type-parameter-key. */
23403 enum tag_types tag_type = none_type;
23404 cp_token *token = cp_lexer_peek_token (parser->lexer);
23405 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23407 cp_lexer_consume_token (parser->lexer);
23408 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23409 /* typename is not allowed in a template template parameter
23410 by the standard until C++17. */
23411 pedwarn (token->location, OPT_Wpedantic,
23412 "ISO C++ forbids typename key in template template parameter;"
23413 " use -std=c++17 or -std=gnu++17");
23416 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23421 /* Parse an (optional) member-specification.
23423 member-specification:
23424 member-declaration member-specification [opt]
23425 access-specifier : member-specification [opt] */
23428 cp_parser_member_specification_opt (cp_parser* parser)
23435 /* Peek at the next token. */
23436 token = cp_lexer_peek_token (parser->lexer);
23437 /* If it's a `}', or EOF then we've seen all the members. */
23438 if (token->type == CPP_CLOSE_BRACE
23439 || token->type == CPP_EOF
23440 || token->type == CPP_PRAGMA_EOL)
23443 /* See if this token is a keyword. */
23444 keyword = token->keyword;
23448 case RID_PROTECTED:
23450 /* Consume the access-specifier. */
23451 cp_lexer_consume_token (parser->lexer);
23452 /* Remember which access-specifier is active. */
23453 current_access_specifier = token->u.value;
23454 /* Look for the `:'. */
23455 cp_parser_require (parser, CPP_COLON, RT_COLON);
23459 /* Accept #pragmas at class scope. */
23460 if (token->type == CPP_PRAGMA)
23462 cp_parser_pragma (parser, pragma_member, NULL);
23466 /* Otherwise, the next construction must be a
23467 member-declaration. */
23468 cp_parser_member_declaration (parser);
23473 /* Parse a member-declaration.
23475 member-declaration:
23476 decl-specifier-seq [opt] member-declarator-list [opt] ;
23477 function-definition ; [opt]
23478 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23480 template-declaration
23483 member-declarator-list:
23485 member-declarator-list , member-declarator
23488 declarator pure-specifier [opt]
23489 declarator constant-initializer [opt]
23490 identifier [opt] : constant-expression
23494 member-declaration:
23495 __extension__ member-declaration
23498 declarator attributes [opt] pure-specifier [opt]
23499 declarator attributes [opt] constant-initializer [opt]
23500 identifier [opt] attributes [opt] : constant-expression
23504 member-declaration:
23505 static_assert-declaration */
23508 cp_parser_member_declaration (cp_parser* parser)
23510 cp_decl_specifier_seq decl_specifiers;
23511 tree prefix_attributes;
23513 int declares_class_or_enum;
23515 cp_token *token = NULL;
23516 cp_token *decl_spec_token_start = NULL;
23517 cp_token *initializer_token_start = NULL;
23518 int saved_pedantic;
23519 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23521 /* Check for the `__extension__' keyword. */
23522 if (cp_parser_extension_opt (parser, &saved_pedantic))
23525 cp_parser_member_declaration (parser);
23526 /* Restore the old value of the PEDANTIC flag. */
23527 pedantic = saved_pedantic;
23532 /* Check for a template-declaration. */
23533 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23535 /* An explicit specialization here is an error condition, and we
23536 expect the specialization handler to detect and report this. */
23537 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23538 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23539 cp_parser_explicit_specialization (parser);
23541 cp_parser_template_declaration (parser, /*member_p=*/true);
23545 /* Check for a template introduction. */
23546 else if (cp_parser_template_declaration_after_export (parser, true))
23549 /* Check for a using-declaration. */
23550 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23552 if (cxx_dialect < cxx11)
23554 /* Parse the using-declaration. */
23555 cp_parser_using_declaration (parser,
23556 /*access_declaration_p=*/false);
23562 bool alias_decl_expected;
23563 cp_parser_parse_tentatively (parser);
23564 decl = cp_parser_alias_declaration (parser);
23565 /* Note that if we actually see the '=' token after the
23566 identifier, cp_parser_alias_declaration commits the
23567 tentative parse. In that case, we really expect an
23568 alias-declaration. Otherwise, we expect a using
23570 alias_decl_expected =
23571 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23572 cp_parser_parse_definitely (parser);
23574 if (alias_decl_expected)
23575 finish_member_declaration (decl);
23577 cp_parser_using_declaration (parser,
23578 /*access_declaration_p=*/false);
23583 /* Check for @defs. */
23584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23587 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23588 ivar = ivar_chains;
23592 ivar = TREE_CHAIN (member);
23593 TREE_CHAIN (member) = NULL_TREE;
23594 finish_member_declaration (member);
23599 /* If the next token is `static_assert' we have a static assertion. */
23600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23602 cp_parser_static_assert (parser, /*member_p=*/true);
23606 parser->colon_corrects_to_scope_p = false;
23608 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23611 /* Parse the decl-specifier-seq. */
23612 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23613 cp_parser_decl_specifier_seq (parser,
23614 CP_PARSER_FLAGS_OPTIONAL,
23616 &declares_class_or_enum);
23617 /* Check for an invalid type-name. */
23618 if (!decl_specifiers.any_type_specifiers_p
23619 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23621 /* If there is no declarator, then the decl-specifier-seq should
23623 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23625 /* If there was no decl-specifier-seq, and the next token is a
23626 `;', then we have something like:
23632 Each member-declaration shall declare at least one member
23633 name of the class. */
23634 if (!decl_specifiers.any_specifiers_p)
23636 cp_token *token = cp_lexer_peek_token (parser->lexer);
23637 if (!in_system_header_at (token->location))
23639 gcc_rich_location richloc (token->location);
23640 richloc.add_fixit_remove ();
23641 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23648 /* See if this declaration is a friend. */
23649 friend_p = cp_parser_friend_p (&decl_specifiers);
23650 /* If there were decl-specifiers, check to see if there was
23651 a class-declaration. */
23652 type = check_tag_decl (&decl_specifiers,
23653 /*explicit_type_instantiation_p=*/false);
23654 /* Nested classes have already been added to the class, but
23655 a `friend' needs to be explicitly registered. */
23658 /* If the `friend' keyword was present, the friend must
23659 be introduced with a class-key. */
23660 if (!declares_class_or_enum && cxx_dialect < cxx11)
23661 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23662 "in C++03 a class-key must be used "
23663 "when declaring a friend");
23666 template <typename T> struct A {
23667 friend struct A<T>::B;
23670 A<T>::B will be represented by a TYPENAME_TYPE, and
23671 therefore not recognized by check_tag_decl. */
23674 type = decl_specifiers.type;
23675 if (type && TREE_CODE (type) == TYPE_DECL)
23676 type = TREE_TYPE (type);
23678 if (!type || !TYPE_P (type))
23679 error_at (decl_spec_token_start->location,
23680 "friend declaration does not name a class or "
23683 make_friend_class (current_class_type, type,
23684 /*complain=*/true);
23686 /* If there is no TYPE, an error message will already have
23688 else if (!type || type == error_mark_node)
23690 /* An anonymous aggregate has to be handled specially; such
23691 a declaration really declares a data member (with a
23692 particular type), as opposed to a nested class. */
23693 else if (ANON_AGGR_TYPE_P (type))
23696 if (decl_specifiers.storage_class != sc_none)
23697 error_at (decl_spec_token_start->location,
23698 "a storage class on an anonymous aggregate "
23699 "in class scope is not allowed");
23701 /* Remove constructors and such from TYPE, now that we
23702 know it is an anonymous aggregate. */
23703 fixup_anonymous_aggr (type);
23704 /* And make the corresponding data member. */
23705 decl = build_decl (decl_spec_token_start->location,
23706 FIELD_DECL, NULL_TREE, type);
23707 /* Add it to the class. */
23708 finish_member_declaration (decl);
23711 cp_parser_check_access_in_redeclaration
23713 decl_spec_token_start->location);
23718 bool assume_semicolon = false;
23720 /* Clear attributes from the decl_specifiers but keep them
23721 around as prefix attributes that apply them to the entity
23723 prefix_attributes = decl_specifiers.attributes;
23724 decl_specifiers.attributes = NULL_TREE;
23726 /* See if these declarations will be friends. */
23727 friend_p = cp_parser_friend_p (&decl_specifiers);
23729 /* Keep going until we hit the `;' at the end of the
23731 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23733 tree attributes = NULL_TREE;
23734 tree first_attribute;
23736 bool named_bitfld = false;
23738 /* Peek at the next token. */
23739 token = cp_lexer_peek_token (parser->lexer);
23741 /* The following code wants to know early if it is a bit-field
23742 or some other declaration. Attributes can appear before
23743 the `:' token. Skip over them without consuming any tokens
23744 to peek if they are followed by `:'. */
23745 if (cp_next_tokens_can_be_attribute_p (parser)
23746 || (token->type == CPP_NAME
23747 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23748 && (named_bitfld = true)))
23751 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23752 token = cp_lexer_peek_nth_token (parser->lexer, n);
23755 /* Check for a bitfield declaration. */
23756 if (token->type == CPP_COLON
23757 || (token->type == CPP_NAME
23758 && token == cp_lexer_peek_token (parser->lexer)
23759 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23760 && (named_bitfld = true)))
23764 tree late_attributes = NULL_TREE;
23767 identifier = cp_parser_identifier (parser);
23769 identifier = NULL_TREE;
23771 /* Look for attributes that apply to the bitfield. */
23772 attributes = cp_parser_attributes_opt (parser);
23774 /* Consume the `:' token. */
23775 cp_lexer_consume_token (parser->lexer);
23777 /* Get the width of the bitfield. */
23778 width = cp_parser_constant_expression (parser, false, NULL,
23779 cxx_dialect >= cxx11);
23781 /* In C++2A and as extension for C++11 and above we allow
23782 default member initializers for bit-fields. */
23783 initializer = NULL_TREE;
23784 if (cxx_dialect >= cxx11
23785 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23786 || cp_lexer_next_token_is (parser->lexer,
23790 = cp_lexer_peek_token (parser->lexer)->location;
23791 if (cxx_dialect < cxx2a
23792 && !in_system_header_at (loc)
23793 && identifier != NULL_TREE)
23795 "default member initializers for bit-fields "
23796 "only available with -std=c++2a or "
23799 initializer = cp_parser_save_nsdmi (parser);
23800 if (identifier == NULL_TREE)
23802 error_at (loc, "default member initializer for "
23803 "unnamed bit-field");
23804 initializer = NULL_TREE;
23809 /* Look for attributes that apply to the bitfield after
23810 the `:' token and width. This is where GCC used to
23811 parse attributes in the past, pedwarn if there is
23812 a std attribute. */
23813 if (cp_next_tokens_can_be_std_attribute_p (parser))
23814 pedwarn (input_location, OPT_Wpedantic,
23815 "ISO C++ allows bit-field attributes only "
23816 "before the %<:%> token");
23818 late_attributes = cp_parser_attributes_opt (parser);
23821 attributes = attr_chainon (attributes, late_attributes);
23823 /* Remember which attributes are prefix attributes and
23825 first_attribute = attributes;
23826 /* Combine the attributes. */
23827 attributes = attr_chainon (prefix_attributes, attributes);
23829 /* Create the bitfield declaration. */
23830 decl = grokbitfield (identifier
23831 ? make_id_declarator (NULL_TREE,
23836 width, initializer,
23841 cp_declarator *declarator;
23842 tree asm_specification;
23843 int ctor_dtor_or_conv_p;
23845 /* Parse the declarator. */
23847 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23848 &ctor_dtor_or_conv_p,
23849 /*parenthesized_p=*/NULL,
23853 /* If something went wrong parsing the declarator, make sure
23854 that we at least consume some tokens. */
23855 if (declarator == cp_error_declarator)
23857 /* Skip to the end of the statement. */
23858 cp_parser_skip_to_end_of_statement (parser);
23859 /* If the next token is not a semicolon, that is
23860 probably because we just skipped over the body of
23861 a function. So, we consume a semicolon if
23862 present, but do not issue an error message if it
23864 if (cp_lexer_next_token_is (parser->lexer,
23866 cp_lexer_consume_token (parser->lexer);
23870 if (declares_class_or_enum & 2)
23871 cp_parser_check_for_definition_in_return_type
23872 (declarator, decl_specifiers.type,
23873 decl_specifiers.locations[ds_type_spec]);
23875 /* Look for an asm-specification. */
23876 asm_specification = cp_parser_asm_specification_opt (parser);
23877 /* Look for attributes that apply to the declaration. */
23878 attributes = cp_parser_attributes_opt (parser);
23879 /* Remember which attributes are prefix attributes and
23881 first_attribute = attributes;
23882 /* Combine the attributes. */
23883 attributes = attr_chainon (prefix_attributes, attributes);
23885 /* If it's an `=', then we have a constant-initializer or a
23886 pure-specifier. It is not correct to parse the
23887 initializer before registering the member declaration
23888 since the member declaration should be in scope while
23889 its initializer is processed. However, the rest of the
23890 front end does not yet provide an interface that allows
23891 us to handle this correctly. */
23892 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23896 A pure-specifier shall be used only in the declaration of
23897 a virtual function.
23899 A member-declarator can contain a constant-initializer
23900 only if it declares a static member of integral or
23903 Therefore, if the DECLARATOR is for a function, we look
23904 for a pure-specifier; otherwise, we look for a
23905 constant-initializer. When we call `grokfield', it will
23906 perform more stringent semantics checks. */
23907 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23908 if (function_declarator_p (declarator)
23909 || (decl_specifiers.type
23910 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23911 && declarator->kind == cdk_id
23912 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23913 == FUNCTION_TYPE)))
23914 initializer = cp_parser_pure_specifier (parser);
23915 else if (decl_specifiers.storage_class != sc_static)
23916 initializer = cp_parser_save_nsdmi (parser);
23917 else if (cxx_dialect >= cxx11)
23920 /* Don't require a constant rvalue in C++11, since we
23921 might want a reference constant. We'll enforce
23922 constancy later. */
23923 cp_lexer_consume_token (parser->lexer);
23924 /* Parse the initializer. */
23925 initializer = cp_parser_initializer_clause (parser,
23929 /* Parse the initializer. */
23930 initializer = cp_parser_constant_initializer (parser);
23932 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23933 && !function_declarator_p (declarator))
23936 if (decl_specifiers.storage_class != sc_static)
23937 initializer = cp_parser_save_nsdmi (parser);
23939 initializer = cp_parser_initializer (parser, &x, &x);
23941 /* Otherwise, there is no initializer. */
23943 initializer = NULL_TREE;
23945 /* See if we are probably looking at a function
23946 definition. We are certainly not looking at a
23947 member-declarator. Calling `grokfield' has
23948 side-effects, so we must not do it unless we are sure
23949 that we are looking at a member-declarator. */
23950 if (cp_parser_token_starts_function_definition_p
23951 (cp_lexer_peek_token (parser->lexer)))
23953 /* The grammar does not allow a pure-specifier to be
23954 used when a member function is defined. (It is
23955 possible that this fact is an oversight in the
23956 standard, since a pure function may be defined
23957 outside of the class-specifier. */
23958 if (initializer && initializer_token_start)
23959 error_at (initializer_token_start->location,
23960 "pure-specifier on function-definition");
23961 decl = cp_parser_save_member_function_body (parser,
23965 if (parser->fully_implicit_function_template_p)
23966 decl = finish_fully_implicit_template (parser, decl);
23967 /* If the member was not a friend, declare it here. */
23969 finish_member_declaration (decl);
23970 /* Peek at the next token. */
23971 token = cp_lexer_peek_token (parser->lexer);
23972 /* If the next token is a semicolon, consume it. */
23973 if (token->type == CPP_SEMICOLON)
23975 location_t semicolon_loc
23976 = cp_lexer_consume_token (parser->lexer)->location;
23977 gcc_rich_location richloc (semicolon_loc);
23978 richloc.add_fixit_remove ();
23979 warning_at (&richloc, OPT_Wextra_semi,
23980 "extra %<;%> after in-class "
23981 "function definition");
23986 if (declarator->kind == cdk_function)
23987 declarator->id_loc = token->location;
23988 /* Create the declaration. */
23989 decl = grokfield (declarator, &decl_specifiers,
23990 initializer, /*init_const_expr_p=*/true,
23991 asm_specification, attributes);
23992 if (parser->fully_implicit_function_template_p)
23995 finish_fully_implicit_template (parser, 0);
23997 decl = finish_fully_implicit_template (parser, decl);
24001 cp_finalize_omp_declare_simd (parser, decl);
24002 cp_finalize_oacc_routine (parser, decl, false);
24004 /* Reset PREFIX_ATTRIBUTES. */
24005 if (attributes != error_mark_node)
24007 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24008 attributes = TREE_CHAIN (attributes);
24010 TREE_CHAIN (attributes) = NULL_TREE;
24013 /* If there is any qualification still in effect, clear it
24014 now; we will be starting fresh with the next declarator. */
24015 parser->scope = NULL_TREE;
24016 parser->qualifying_scope = NULL_TREE;
24017 parser->object_scope = NULL_TREE;
24018 /* If it's a `,', then there are more declarators. */
24019 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24021 cp_lexer_consume_token (parser->lexer);
24022 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24024 cp_token *token = cp_lexer_previous_token (parser->lexer);
24025 gcc_rich_location richloc (token->location);
24026 richloc.add_fixit_remove ();
24027 error_at (&richloc, "stray %<,%> at end of "
24028 "member declaration");
24031 /* If the next token isn't a `;', then we have a parse error. */
24032 else if (cp_lexer_next_token_is_not (parser->lexer,
24035 /* The next token might be a ways away from where the
24036 actual semicolon is missing. Find the previous token
24037 and use that for our error position. */
24038 cp_token *token = cp_lexer_previous_token (parser->lexer);
24039 gcc_rich_location richloc (token->location);
24040 richloc.add_fixit_insert_after (";");
24041 error_at (&richloc, "expected %<;%> at end of "
24042 "member declaration");
24044 /* Assume that the user meant to provide a semicolon. If
24045 we were to cp_parser_skip_to_end_of_statement, we might
24046 skip to a semicolon inside a member function definition
24047 and issue nonsensical error messages. */
24048 assume_semicolon = true;
24053 /* Add DECL to the list of members. */
24055 /* Explicitly include, eg, NSDMIs, for better error
24056 recovery (c++/58650). */
24057 || !DECL_DECLARES_FUNCTION_P (decl))
24058 finish_member_declaration (decl);
24060 if (TREE_CODE (decl) == FUNCTION_DECL)
24061 cp_parser_save_default_args (parser, decl);
24062 else if (TREE_CODE (decl) == FIELD_DECL
24063 && DECL_INITIAL (decl))
24064 /* Add DECL to the queue of NSDMI to be parsed later. */
24065 vec_safe_push (unparsed_nsdmis, decl);
24068 if (assume_semicolon)
24073 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24075 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24078 /* Parse a pure-specifier.
24083 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24084 Otherwise, ERROR_MARK_NODE is returned. */
24087 cp_parser_pure_specifier (cp_parser* parser)
24091 /* Look for the `=' token. */
24092 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24093 return error_mark_node;
24094 /* Look for the `0' token. */
24095 token = cp_lexer_peek_token (parser->lexer);
24097 if (token->type == CPP_EOF
24098 || token->type == CPP_PRAGMA_EOL)
24099 return error_mark_node;
24101 cp_lexer_consume_token (parser->lexer);
24103 /* Accept = default or = delete in c++0x mode. */
24104 if (token->keyword == RID_DEFAULT
24105 || token->keyword == RID_DELETE)
24107 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24108 return token->u.value;
24111 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24112 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24114 cp_parser_error (parser,
24115 "invalid pure specifier (only %<= 0%> is allowed)");
24116 cp_parser_skip_to_end_of_statement (parser);
24117 return error_mark_node;
24119 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24121 error_at (token->location, "templates may not be %<virtual%>");
24122 return error_mark_node;
24125 return integer_zero_node;
24128 /* Parse a constant-initializer.
24130 constant-initializer:
24131 = constant-expression
24133 Returns a representation of the constant-expression. */
24136 cp_parser_constant_initializer (cp_parser* parser)
24138 /* Look for the `=' token. */
24139 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24140 return error_mark_node;
24142 /* It is invalid to write:
24144 struct S { static const int i = { 7 }; };
24147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24149 cp_parser_error (parser,
24150 "a brace-enclosed initializer is not allowed here");
24151 /* Consume the opening brace. */
24152 matching_braces braces;
24153 braces.consume_open (parser);
24154 /* Skip the initializer. */
24155 cp_parser_skip_to_closing_brace (parser);
24156 /* Look for the trailing `}'. */
24157 braces.require_close (parser);
24159 return error_mark_node;
24162 return cp_parser_constant_expression (parser);
24165 /* Derived classes [gram.class.derived] */
24167 /* Parse a base-clause.
24170 : base-specifier-list
24172 base-specifier-list:
24173 base-specifier ... [opt]
24174 base-specifier-list , base-specifier ... [opt]
24176 Returns a TREE_LIST representing the base-classes, in the order in
24177 which they were declared. The representation of each node is as
24178 described by cp_parser_base_specifier.
24180 In the case that no bases are specified, this function will return
24181 NULL_TREE, not ERROR_MARK_NODE. */
24184 cp_parser_base_clause (cp_parser* parser)
24186 tree bases = NULL_TREE;
24188 /* Look for the `:' that begins the list. */
24189 cp_parser_require (parser, CPP_COLON, RT_COLON);
24191 /* Scan the base-specifier-list. */
24196 bool pack_expansion_p = false;
24198 /* Look for the base-specifier. */
24199 base = cp_parser_base_specifier (parser);
24200 /* Look for the (optional) ellipsis. */
24201 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24203 /* Consume the `...'. */
24204 cp_lexer_consume_token (parser->lexer);
24206 pack_expansion_p = true;
24209 /* Add BASE to the front of the list. */
24210 if (base && base != error_mark_node)
24212 if (pack_expansion_p)
24213 /* Make this a pack expansion type. */
24214 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24216 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24218 TREE_CHAIN (base) = bases;
24222 /* Peek at the next token. */
24223 token = cp_lexer_peek_token (parser->lexer);
24224 /* If it's not a comma, then the list is complete. */
24225 if (token->type != CPP_COMMA)
24227 /* Consume the `,'. */
24228 cp_lexer_consume_token (parser->lexer);
24231 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24232 base class had a qualified name. However, the next name that
24233 appears is certainly not qualified. */
24234 parser->scope = NULL_TREE;
24235 parser->qualifying_scope = NULL_TREE;
24236 parser->object_scope = NULL_TREE;
24238 return nreverse (bases);
24241 /* Parse a base-specifier.
24244 :: [opt] nested-name-specifier [opt] class-name
24245 virtual access-specifier [opt] :: [opt] nested-name-specifier
24247 access-specifier virtual [opt] :: [opt] nested-name-specifier
24250 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24251 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24252 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24253 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24256 cp_parser_base_specifier (cp_parser* parser)
24260 bool virtual_p = false;
24261 bool duplicate_virtual_error_issued_p = false;
24262 bool duplicate_access_error_issued_p = false;
24263 bool class_scope_p, template_p;
24264 tree access = access_default_node;
24267 /* Process the optional `virtual' and `access-specifier'. */
24270 /* Peek at the next token. */
24271 token = cp_lexer_peek_token (parser->lexer);
24272 /* Process `virtual'. */
24273 switch (token->keyword)
24276 /* If `virtual' appears more than once, issue an error. */
24277 if (virtual_p && !duplicate_virtual_error_issued_p)
24279 cp_parser_error (parser,
24280 "%<virtual%> specified more than once in base-specifier");
24281 duplicate_virtual_error_issued_p = true;
24286 /* Consume the `virtual' token. */
24287 cp_lexer_consume_token (parser->lexer);
24292 case RID_PROTECTED:
24294 /* If more than one access specifier appears, issue an
24296 if (access != access_default_node
24297 && !duplicate_access_error_issued_p)
24299 cp_parser_error (parser,
24300 "more than one access specifier in base-specifier");
24301 duplicate_access_error_issued_p = true;
24304 access = ridpointers[(int) token->keyword];
24306 /* Consume the access-specifier. */
24307 cp_lexer_consume_token (parser->lexer);
24316 /* It is not uncommon to see programs mechanically, erroneously, use
24317 the 'typename' keyword to denote (dependent) qualified types
24318 as base classes. */
24319 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24321 token = cp_lexer_peek_token (parser->lexer);
24322 if (!processing_template_decl)
24323 error_at (token->location,
24324 "keyword %<typename%> not allowed outside of templates");
24326 error_at (token->location,
24327 "keyword %<typename%> not allowed in this context "
24328 "(the base class is implicitly a type)");
24329 cp_lexer_consume_token (parser->lexer);
24332 /* Look for the optional `::' operator. */
24333 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24334 /* Look for the nested-name-specifier. The simplest way to
24339 The keyword `typename' is not permitted in a base-specifier or
24340 mem-initializer; in these contexts a qualified name that
24341 depends on a template-parameter is implicitly assumed to be a
24344 is to pretend that we have seen the `typename' keyword at this
24346 cp_parser_nested_name_specifier_opt (parser,
24347 /*typename_keyword_p=*/true,
24348 /*check_dependency_p=*/true,
24350 /*is_declaration=*/true);
24351 /* If the base class is given by a qualified name, assume that names
24352 we see are type names or templates, as appropriate. */
24353 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24354 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24357 && cp_lexer_next_token_is_decltype (parser->lexer))
24358 /* DR 950 allows decltype as a base-specifier. */
24359 type = cp_parser_decltype (parser);
24362 /* Otherwise, look for the class-name. */
24363 type = cp_parser_class_name (parser,
24367 /*check_dependency_p=*/true,
24368 /*class_head_p=*/false,
24369 /*is_declaration=*/true);
24370 type = TREE_TYPE (type);
24373 if (type == error_mark_node)
24374 return error_mark_node;
24376 return finish_base_specifier (type, access, virtual_p);
24379 /* Exception handling [gram.exception] */
24381 /* Parse an (optional) noexcept-specification.
24383 noexcept-specification:
24384 noexcept ( constant-expression ) [opt]
24386 If no noexcept-specification is present, returns NULL_TREE.
24387 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24388 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24389 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24390 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24391 in which case a boolean condition is returned instead. */
24394 cp_parser_noexcept_specification_opt (cp_parser* parser,
24395 bool require_constexpr,
24396 bool* consumed_expr,
24400 const char *saved_message;
24402 /* Peek at the next token. */
24403 token = cp_lexer_peek_token (parser->lexer);
24405 /* Is it a noexcept-specification? */
24406 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24409 cp_lexer_consume_token (parser->lexer);
24411 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24413 matching_parens parens;
24414 parens.consume_open (parser);
24416 if (require_constexpr)
24418 /* Types may not be defined in an exception-specification. */
24419 saved_message = parser->type_definition_forbidden_message;
24420 parser->type_definition_forbidden_message
24421 = G_("types may not be defined in an exception-specification");
24423 expr = cp_parser_constant_expression (parser);
24425 /* Restore the saved message. */
24426 parser->type_definition_forbidden_message = saved_message;
24430 expr = cp_parser_expression (parser);
24431 *consumed_expr = true;
24434 parens.require_close (parser);
24438 expr = boolean_true_node;
24439 if (!require_constexpr)
24440 *consumed_expr = false;
24443 /* We cannot build a noexcept-spec right away because this will check
24444 that expr is a constexpr. */
24446 return build_noexcept_spec (expr, tf_warning_or_error);
24454 /* Parse an (optional) exception-specification.
24456 exception-specification:
24457 throw ( type-id-list [opt] )
24459 Returns a TREE_LIST representing the exception-specification. The
24460 TREE_VALUE of each node is a type. */
24463 cp_parser_exception_specification_opt (cp_parser* parser)
24467 const char *saved_message;
24469 /* Peek at the next token. */
24470 token = cp_lexer_peek_token (parser->lexer);
24472 /* Is it a noexcept-specification? */
24473 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24475 if (type_id_list != NULL_TREE)
24476 return type_id_list;
24478 /* If it's not `throw', then there's no exception-specification. */
24479 if (!cp_parser_is_keyword (token, RID_THROW))
24482 location_t loc = token->location;
24484 /* Consume the `throw'. */
24485 cp_lexer_consume_token (parser->lexer);
24487 /* Look for the `('. */
24488 matching_parens parens;
24489 parens.require_open (parser);
24491 /* Peek at the next token. */
24492 token = cp_lexer_peek_token (parser->lexer);
24493 /* If it's not a `)', then there is a type-id-list. */
24494 if (token->type != CPP_CLOSE_PAREN)
24496 /* Types may not be defined in an exception-specification. */
24497 saved_message = parser->type_definition_forbidden_message;
24498 parser->type_definition_forbidden_message
24499 = G_("types may not be defined in an exception-specification");
24500 /* Parse the type-id-list. */
24501 type_id_list = cp_parser_type_id_list (parser);
24502 /* Restore the saved message. */
24503 parser->type_definition_forbidden_message = saved_message;
24505 if (cxx_dialect >= cxx17)
24507 error_at (loc, "ISO C++17 does not allow dynamic exception "
24509 type_id_list = NULL_TREE;
24511 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24512 warning_at (loc, OPT_Wdeprecated,
24513 "dynamic exception specifications are deprecated in "
24516 /* In C++17, throw() is equivalent to noexcept (true). throw()
24517 is deprecated in C++11 and above as well, but is still widely used,
24518 so don't warn about it yet. */
24519 else if (cxx_dialect >= cxx17)
24520 type_id_list = noexcept_true_spec;
24522 type_id_list = empty_except_spec;
24524 /* Look for the `)'. */
24525 parens.require_close (parser);
24527 return type_id_list;
24530 /* Parse an (optional) type-id-list.
24534 type-id-list , type-id ... [opt]
24536 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24537 in the order that the types were presented. */
24540 cp_parser_type_id_list (cp_parser* parser)
24542 tree types = NULL_TREE;
24549 token = cp_lexer_peek_token (parser->lexer);
24551 /* Get the next type-id. */
24552 type = cp_parser_type_id (parser);
24553 /* Check for invalid 'auto'. */
24554 if (flag_concepts && type_uses_auto (type))
24556 error_at (token->location,
24557 "invalid use of %<auto%> in exception-specification");
24558 type = error_mark_node;
24560 /* Parse the optional ellipsis. */
24561 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24563 /* Consume the `...'. */
24564 cp_lexer_consume_token (parser->lexer);
24566 /* Turn the type into a pack expansion expression. */
24567 type = make_pack_expansion (type);
24569 /* Add it to the list. */
24570 types = add_exception_specifier (types, type, /*complain=*/1);
24571 /* Peek at the next token. */
24572 token = cp_lexer_peek_token (parser->lexer);
24573 /* If it is not a `,', we are done. */
24574 if (token->type != CPP_COMMA)
24576 /* Consume the `,'. */
24577 cp_lexer_consume_token (parser->lexer);
24580 return nreverse (types);
24583 /* Parse a try-block.
24586 try compound-statement handler-seq */
24589 cp_parser_try_block (cp_parser* parser)
24593 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24594 if (parser->in_function_body
24595 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24596 error ("%<try%> in %<constexpr%> function");
24598 try_block = begin_try_block ();
24599 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24600 finish_try_block (try_block);
24601 cp_parser_handler_seq (parser);
24602 finish_handler_sequence (try_block);
24607 /* Parse a function-try-block.
24609 function-try-block:
24610 try ctor-initializer [opt] function-body handler-seq */
24613 cp_parser_function_try_block (cp_parser* parser)
24615 tree compound_stmt;
24618 /* Look for the `try' keyword. */
24619 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24621 /* Let the rest of the front end know where we are. */
24622 try_block = begin_function_try_block (&compound_stmt);
24623 /* Parse the function-body. */
24624 cp_parser_ctor_initializer_opt_and_function_body
24625 (parser, /*in_function_try_block=*/true);
24626 /* We're done with the `try' part. */
24627 finish_function_try_block (try_block);
24628 /* Parse the handlers. */
24629 cp_parser_handler_seq (parser);
24630 /* We're done with the handlers. */
24631 finish_function_handler_sequence (try_block, compound_stmt);
24634 /* Parse a handler-seq.
24637 handler handler-seq [opt] */
24640 cp_parser_handler_seq (cp_parser* parser)
24646 /* Parse the handler. */
24647 cp_parser_handler (parser);
24648 /* Peek at the next token. */
24649 token = cp_lexer_peek_token (parser->lexer);
24650 /* If it's not `catch' then there are no more handlers. */
24651 if (!cp_parser_is_keyword (token, RID_CATCH))
24656 /* Parse a handler.
24659 catch ( exception-declaration ) compound-statement */
24662 cp_parser_handler (cp_parser* parser)
24667 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24668 handler = begin_handler ();
24669 matching_parens parens;
24670 parens.require_open (parser);
24671 declaration = cp_parser_exception_declaration (parser);
24672 finish_handler_parms (declaration, handler);
24673 parens.require_close (parser);
24674 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24675 finish_handler (handler);
24678 /* Parse an exception-declaration.
24680 exception-declaration:
24681 type-specifier-seq declarator
24682 type-specifier-seq abstract-declarator
24686 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24687 ellipsis variant is used. */
24690 cp_parser_exception_declaration (cp_parser* parser)
24692 cp_decl_specifier_seq type_specifiers;
24693 cp_declarator *declarator;
24694 const char *saved_message;
24696 /* If it's an ellipsis, it's easy to handle. */
24697 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24699 /* Consume the `...' token. */
24700 cp_lexer_consume_token (parser->lexer);
24704 /* Types may not be defined in exception-declarations. */
24705 saved_message = parser->type_definition_forbidden_message;
24706 parser->type_definition_forbidden_message
24707 = G_("types may not be defined in exception-declarations");
24709 /* Parse the type-specifier-seq. */
24710 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24711 /*is_trailing_return=*/false,
24713 /* If it's a `)', then there is no declarator. */
24714 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24717 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24718 /*ctor_dtor_or_conv_p=*/NULL,
24719 /*parenthesized_p=*/NULL,
24720 /*member_p=*/false,
24721 /*friend_p=*/false);
24723 /* Restore the saved message. */
24724 parser->type_definition_forbidden_message = saved_message;
24726 if (!type_specifiers.any_specifiers_p)
24727 return error_mark_node;
24729 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24732 /* Parse a throw-expression.
24735 throw assignment-expression [opt]
24737 Returns a THROW_EXPR representing the throw-expression. */
24740 cp_parser_throw_expression (cp_parser* parser)
24745 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24746 token = cp_lexer_peek_token (parser->lexer);
24747 /* Figure out whether or not there is an assignment-expression
24748 following the "throw" keyword. */
24749 if (token->type == CPP_COMMA
24750 || token->type == CPP_SEMICOLON
24751 || token->type == CPP_CLOSE_PAREN
24752 || token->type == CPP_CLOSE_SQUARE
24753 || token->type == CPP_CLOSE_BRACE
24754 || token->type == CPP_COLON)
24755 expression = NULL_TREE;
24757 expression = cp_parser_assignment_expression (parser);
24759 return build_throw (expression);
24762 /* GNU Extensions */
24764 /* Parse an (optional) asm-specification.
24767 asm ( string-literal )
24769 If the asm-specification is present, returns a STRING_CST
24770 corresponding to the string-literal. Otherwise, returns
24774 cp_parser_asm_specification_opt (cp_parser* parser)
24777 tree asm_specification;
24779 /* Peek at the next token. */
24780 token = cp_lexer_peek_token (parser->lexer);
24781 /* If the next token isn't the `asm' keyword, then there's no
24782 asm-specification. */
24783 if (!cp_parser_is_keyword (token, RID_ASM))
24786 /* Consume the `asm' token. */
24787 cp_lexer_consume_token (parser->lexer);
24788 /* Look for the `('. */
24789 matching_parens parens;
24790 parens.require_open (parser);
24792 /* Look for the string-literal. */
24793 asm_specification = cp_parser_string_literal (parser, false, false);
24795 /* Look for the `)'. */
24796 parens.require_close (parser);
24798 return asm_specification;
24801 /* Parse an asm-operand-list.
24805 asm-operand-list , asm-operand
24808 string-literal ( expression )
24809 [ string-literal ] string-literal ( expression )
24811 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24812 each node is the expression. The TREE_PURPOSE is itself a
24813 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24814 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24815 is a STRING_CST for the string literal before the parenthesis. Returns
24816 ERROR_MARK_NODE if any of the operands are invalid. */
24819 cp_parser_asm_operand_list (cp_parser* parser)
24821 tree asm_operands = NULL_TREE;
24822 bool invalid_operands = false;
24826 tree string_literal;
24830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24832 /* Consume the `[' token. */
24833 cp_lexer_consume_token (parser->lexer);
24834 /* Read the operand name. */
24835 name = cp_parser_identifier (parser);
24836 if (name != error_mark_node)
24837 name = build_string (IDENTIFIER_LENGTH (name),
24838 IDENTIFIER_POINTER (name));
24839 /* Look for the closing `]'. */
24840 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24844 /* Look for the string-literal. */
24845 string_literal = cp_parser_string_literal (parser, false, false);
24847 /* Look for the `('. */
24848 matching_parens parens;
24849 parens.require_open (parser);
24850 /* Parse the expression. */
24851 expression = cp_parser_expression (parser);
24852 /* Look for the `)'. */
24853 parens.require_close (parser);
24855 if (name == error_mark_node
24856 || string_literal == error_mark_node
24857 || expression == error_mark_node)
24858 invalid_operands = true;
24860 /* Add this operand to the list. */
24861 asm_operands = tree_cons (build_tree_list (name, string_literal),
24864 /* If the next token is not a `,', there are no more
24866 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24868 /* Consume the `,'. */
24869 cp_lexer_consume_token (parser->lexer);
24872 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24875 /* Parse an asm-clobber-list.
24879 asm-clobber-list , string-literal
24881 Returns a TREE_LIST, indicating the clobbers in the order that they
24882 appeared. The TREE_VALUE of each node is a STRING_CST. */
24885 cp_parser_asm_clobber_list (cp_parser* parser)
24887 tree clobbers = NULL_TREE;
24891 tree string_literal;
24893 /* Look for the string literal. */
24894 string_literal = cp_parser_string_literal (parser, false, false);
24895 /* Add it to the list. */
24896 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24897 /* If the next token is not a `,', then the list is
24899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24901 /* Consume the `,' token. */
24902 cp_lexer_consume_token (parser->lexer);
24908 /* Parse an asm-label-list.
24912 asm-label-list , identifier
24914 Returns a TREE_LIST, indicating the labels in the order that they
24915 appeared. The TREE_VALUE of each node is a label. */
24918 cp_parser_asm_label_list (cp_parser* parser)
24920 tree labels = NULL_TREE;
24924 tree identifier, label, name;
24926 /* Look for the identifier. */
24927 identifier = cp_parser_identifier (parser);
24928 if (!error_operand_p (identifier))
24930 label = lookup_label (identifier);
24931 if (TREE_CODE (label) == LABEL_DECL)
24933 TREE_USED (label) = 1;
24934 check_goto (label);
24935 name = build_string (IDENTIFIER_LENGTH (identifier),
24936 IDENTIFIER_POINTER (identifier));
24937 labels = tree_cons (name, label, labels);
24940 /* If the next token is not a `,', then the list is
24942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24944 /* Consume the `,' token. */
24945 cp_lexer_consume_token (parser->lexer);
24948 return nreverse (labels);
24951 /* Return TRUE iff the next tokens in the stream are possibly the
24952 beginning of a GNU extension attribute. */
24955 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24957 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24960 /* Return TRUE iff the next tokens in the stream are possibly the
24961 beginning of a standard C++-11 attribute specifier. */
24964 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24966 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24969 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24970 beginning of a standard C++-11 attribute specifier. */
24973 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24975 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24977 return (cxx_dialect >= cxx11
24978 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24979 || (token->type == CPP_OPEN_SQUARE
24980 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24981 && token->type == CPP_OPEN_SQUARE)));
24984 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24985 beginning of a GNU extension attribute. */
24988 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24990 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24992 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24995 /* Return true iff the next tokens can be the beginning of either a
24996 GNU attribute list, or a standard C++11 attribute sequence. */
24999 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25001 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25002 || cp_next_tokens_can_be_std_attribute_p (parser));
25005 /* Return true iff the next Nth tokens can be the beginning of either
25006 a GNU attribute list, or a standard C++11 attribute sequence. */
25009 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25011 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25012 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25015 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25016 of GNU attributes, or return NULL. */
25019 cp_parser_attributes_opt (cp_parser *parser)
25021 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25022 return cp_parser_gnu_attributes_opt (parser);
25023 return cp_parser_std_attribute_spec_seq (parser);
25026 /* Parse an (optional) series of attributes.
25029 attributes attribute
25032 __attribute__ (( attribute-list [opt] ))
25034 The return value is as for cp_parser_gnu_attribute_list. */
25037 cp_parser_gnu_attributes_opt (cp_parser* parser)
25039 tree attributes = NULL_TREE;
25041 temp_override<bool> cleanup
25042 (parser->auto_is_implicit_function_template_parm_p, false);
25047 tree attribute_list;
25050 /* Peek at the next token. */
25051 token = cp_lexer_peek_token (parser->lexer);
25052 /* If it's not `__attribute__', then we're done. */
25053 if (token->keyword != RID_ATTRIBUTE)
25056 /* Consume the `__attribute__' keyword. */
25057 cp_lexer_consume_token (parser->lexer);
25058 /* Look for the two `(' tokens. */
25059 matching_parens outer_parens;
25060 outer_parens.require_open (parser);
25061 matching_parens inner_parens;
25062 inner_parens.require_open (parser);
25064 /* Peek at the next token. */
25065 token = cp_lexer_peek_token (parser->lexer);
25066 if (token->type != CPP_CLOSE_PAREN)
25067 /* Parse the attribute-list. */
25068 attribute_list = cp_parser_gnu_attribute_list (parser);
25070 /* If the next token is a `)', then there is no attribute
25072 attribute_list = NULL;
25074 /* Look for the two `)' tokens. */
25075 if (!inner_parens.require_close (parser))
25077 if (!outer_parens.require_close (parser))
25080 cp_parser_skip_to_end_of_statement (parser);
25082 /* Add these new attributes to the list. */
25083 attributes = attr_chainon (attributes, attribute_list);
25089 /* Parse a GNU attribute-list.
25093 attribute-list , attribute
25097 identifier ( identifier )
25098 identifier ( identifier , expression-list )
25099 identifier ( expression-list )
25101 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25102 to an attribute. The TREE_PURPOSE of each node is the identifier
25103 indicating which attribute is in use. The TREE_VALUE represents
25104 the arguments, if any. */
25107 cp_parser_gnu_attribute_list (cp_parser* parser)
25109 tree attribute_list = NULL_TREE;
25110 bool save_translate_strings_p = parser->translate_strings_p;
25112 parser->translate_strings_p = false;
25119 /* Look for the identifier. We also allow keywords here; for
25120 example `__attribute__ ((const))' is legal. */
25121 token = cp_lexer_peek_token (parser->lexer);
25122 if (token->type == CPP_NAME
25123 || token->type == CPP_KEYWORD)
25125 tree arguments = NULL_TREE;
25127 /* Consume the token, but save it since we need it for the
25128 SIMD enabled function parsing. */
25129 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25131 /* Save away the identifier that indicates which attribute
25133 identifier = (token->type == CPP_KEYWORD)
25134 /* For keywords, use the canonical spelling, not the
25135 parsed identifier. */
25136 ? ridpointers[(int) token->keyword]
25137 : id_token->u.value;
25139 identifier = canonicalize_attr_name (identifier);
25140 attribute = build_tree_list (identifier, NULL_TREE);
25142 /* Peek at the next token. */
25143 token = cp_lexer_peek_token (parser->lexer);
25144 /* If it's an `(', then parse the attribute arguments. */
25145 if (token->type == CPP_OPEN_PAREN)
25147 vec<tree, va_gc> *vec;
25148 int attr_flag = (attribute_takes_identifier_p (identifier)
25149 ? id_attr : normal_attr);
25150 vec = cp_parser_parenthesized_expression_list
25151 (parser, attr_flag, /*cast_p=*/false,
25152 /*allow_expansion_p=*/false,
25153 /*non_constant_p=*/NULL);
25155 arguments = error_mark_node;
25158 arguments = build_tree_list_vec (vec);
25159 release_tree_vector (vec);
25161 /* Save the arguments away. */
25162 TREE_VALUE (attribute) = arguments;
25165 if (arguments != error_mark_node)
25167 /* Add this attribute to the list. */
25168 TREE_CHAIN (attribute) = attribute_list;
25169 attribute_list = attribute;
25172 token = cp_lexer_peek_token (parser->lexer);
25174 /* Now, look for more attributes. If the next token isn't a
25175 `,', we're done. */
25176 if (token->type != CPP_COMMA)
25179 /* Consume the comma and keep going. */
25180 cp_lexer_consume_token (parser->lexer);
25182 parser->translate_strings_p = save_translate_strings_p;
25184 /* We built up the list in reverse order. */
25185 return nreverse (attribute_list);
25188 /* Parse a standard C++11 attribute.
25190 The returned representation is a TREE_LIST which TREE_PURPOSE is
25191 the scoped name of the attribute, and the TREE_VALUE is its
25194 Note that the scoped name of the attribute is itself a TREE_LIST
25195 which TREE_PURPOSE is the namespace of the attribute, and
25196 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25197 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25198 and which TREE_PURPOSE is directly the attribute name.
25200 Clients of the attribute code should use get_attribute_namespace
25201 and get_attribute_name to get the actual namespace and name of
25202 attributes, regardless of their being GNU or C++11 attributes.
25205 attribute-token attribute-argument-clause [opt]
25209 attribute-scoped-token
25211 attribute-scoped-token:
25212 attribute-namespace :: identifier
25214 attribute-namespace:
25217 attribute-argument-clause:
25218 ( balanced-token-seq )
25220 balanced-token-seq:
25221 balanced-token [opt]
25222 balanced-token-seq balanced-token
25225 ( balanced-token-seq )
25226 [ balanced-token-seq ]
25227 { balanced-token-seq }. */
25230 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25232 tree attribute, attr_id = NULL_TREE, arguments;
25235 temp_override<bool> cleanup
25236 (parser->auto_is_implicit_function_template_parm_p, false);
25238 /* First, parse name of the attribute, a.k.a attribute-token. */
25240 token = cp_lexer_peek_token (parser->lexer);
25241 if (token->type == CPP_NAME)
25242 attr_id = token->u.value;
25243 else if (token->type == CPP_KEYWORD)
25244 attr_id = ridpointers[(int) token->keyword];
25245 else if (token->flags & NAMED_OP)
25246 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25248 if (attr_id == NULL_TREE)
25251 cp_lexer_consume_token (parser->lexer);
25253 token = cp_lexer_peek_token (parser->lexer);
25254 if (token->type == CPP_SCOPE)
25256 /* We are seeing a scoped attribute token. */
25258 cp_lexer_consume_token (parser->lexer);
25260 error_at (token->location, "attribute using prefix used together "
25261 "with scoped attribute token");
25264 token = cp_lexer_consume_token (parser->lexer);
25265 if (token->type == CPP_NAME)
25266 attr_id = token->u.value;
25267 else if (token->type == CPP_KEYWORD)
25268 attr_id = ridpointers[(int) token->keyword];
25269 else if (token->flags & NAMED_OP)
25270 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25273 error_at (token->location,
25274 "expected an identifier for the attribute name");
25275 return error_mark_node;
25278 attr_ns = canonicalize_attr_name (attr_ns);
25279 attr_id = canonicalize_attr_name (attr_id);
25280 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25282 token = cp_lexer_peek_token (parser->lexer);
25286 attr_ns = canonicalize_attr_name (attr_ns);
25287 attr_id = canonicalize_attr_name (attr_id);
25288 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25293 attr_id = canonicalize_attr_name (attr_id);
25294 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25296 /* C++11 noreturn attribute is equivalent to GNU's. */
25297 if (is_attribute_p ("noreturn", attr_id))
25298 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25299 /* C++14 deprecated attribute is equivalent to GNU's. */
25300 else if (is_attribute_p ("deprecated", attr_id))
25301 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25302 /* C++17 fallthrough attribute is equivalent to GNU's. */
25303 else if (is_attribute_p ("fallthrough", attr_id))
25304 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25305 /* Transactional Memory TS optimize_for_synchronized attribute is
25306 equivalent to GNU transaction_callable. */
25307 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25308 TREE_PURPOSE (attribute)
25309 = get_identifier ("transaction_callable");
25310 /* Transactional Memory attributes are GNU attributes. */
25311 else if (tm_attr_to_mask (attr_id))
25312 TREE_PURPOSE (attribute) = attr_id;
25315 /* Now parse the optional argument clause of the attribute. */
25317 if (token->type != CPP_OPEN_PAREN)
25321 vec<tree, va_gc> *vec;
25322 int attr_flag = normal_attr;
25324 if (attr_ns == get_identifier ("gnu")
25325 && attribute_takes_identifier_p (attr_id))
25326 /* A GNU attribute that takes an identifier in parameter. */
25327 attr_flag = id_attr;
25329 vec = cp_parser_parenthesized_expression_list
25330 (parser, attr_flag, /*cast_p=*/false,
25331 /*allow_expansion_p=*/true,
25332 /*non_constant_p=*/NULL);
25334 arguments = error_mark_node;
25337 arguments = build_tree_list_vec (vec);
25338 release_tree_vector (vec);
25341 if (arguments == error_mark_node)
25342 attribute = error_mark_node;
25344 TREE_VALUE (attribute) = arguments;
25350 /* Check that the attribute ATTRIBUTE appears at most once in the
25351 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25352 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25353 isn't implemented yet in GCC. */
25356 cp_parser_check_std_attribute (tree attributes, tree attribute)
25360 tree name = get_attribute_name (attribute);
25361 if (is_attribute_p ("noreturn", name)
25362 && lookup_attribute ("noreturn", attributes))
25363 error ("attribute %<noreturn%> can appear at most once "
25364 "in an attribute-list");
25365 else if (is_attribute_p ("deprecated", name)
25366 && lookup_attribute ("deprecated", attributes))
25367 error ("attribute %<deprecated%> can appear at most once "
25368 "in an attribute-list");
25372 /* Parse a list of standard C++-11 attributes.
25376 attribute-list , attribute[opt]
25378 attribute-list , attribute ...
25382 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25384 tree attributes = NULL_TREE, attribute = NULL_TREE;
25385 cp_token *token = NULL;
25389 attribute = cp_parser_std_attribute (parser, attr_ns);
25390 if (attribute == error_mark_node)
25392 if (attribute != NULL_TREE)
25394 cp_parser_check_std_attribute (attributes, attribute);
25395 TREE_CHAIN (attribute) = attributes;
25396 attributes = attribute;
25398 token = cp_lexer_peek_token (parser->lexer);
25399 if (token->type == CPP_ELLIPSIS)
25401 cp_lexer_consume_token (parser->lexer);
25402 if (attribute == NULL_TREE)
25403 error_at (token->location,
25404 "expected attribute before %<...%>");
25407 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25408 if (pack == error_mark_node)
25409 return error_mark_node;
25410 TREE_VALUE (attribute) = pack;
25412 token = cp_lexer_peek_token (parser->lexer);
25414 if (token->type != CPP_COMMA)
25416 cp_lexer_consume_token (parser->lexer);
25418 attributes = nreverse (attributes);
25422 /* Parse a standard C++-11 attribute specifier.
25424 attribute-specifier:
25425 [ [ attribute-using-prefix [opt] attribute-list ] ]
25426 alignment-specifier
25428 attribute-using-prefix:
25429 using attribute-namespace :
25431 alignment-specifier:
25432 alignas ( type-id ... [opt] )
25433 alignas ( alignment-expression ... [opt] ). */
25436 cp_parser_std_attribute_spec (cp_parser *parser)
25438 tree attributes = NULL_TREE;
25439 cp_token *token = cp_lexer_peek_token (parser->lexer);
25441 if (token->type == CPP_OPEN_SQUARE
25442 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25444 tree attr_ns = NULL_TREE;
25446 cp_lexer_consume_token (parser->lexer);
25447 cp_lexer_consume_token (parser->lexer);
25449 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25451 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25452 if (token->type == CPP_NAME)
25453 attr_ns = token->u.value;
25454 else if (token->type == CPP_KEYWORD)
25455 attr_ns = ridpointers[(int) token->keyword];
25456 else if (token->flags & NAMED_OP)
25457 attr_ns = get_identifier (cpp_type2name (token->type,
25460 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25462 if (cxx_dialect < cxx17
25463 && !in_system_header_at (input_location))
25464 pedwarn (input_location, 0,
25465 "attribute using prefix only available "
25466 "with -std=c++17 or -std=gnu++17");
25468 cp_lexer_consume_token (parser->lexer);
25469 cp_lexer_consume_token (parser->lexer);
25470 cp_lexer_consume_token (parser->lexer);
25473 attr_ns = NULL_TREE;
25476 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25478 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25479 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25480 cp_parser_skip_to_end_of_statement (parser);
25482 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25483 when we are sure that we have actually parsed them. */
25484 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25490 /* Look for an alignment-specifier. */
25492 token = cp_lexer_peek_token (parser->lexer);
25494 if (token->type != CPP_KEYWORD
25495 || token->keyword != RID_ALIGNAS)
25498 cp_lexer_consume_token (parser->lexer);
25499 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25501 matching_parens parens;
25502 if (!parens.require_open (parser))
25503 return error_mark_node;
25505 cp_parser_parse_tentatively (parser);
25506 alignas_expr = cp_parser_type_id (parser);
25508 if (!cp_parser_parse_definitely (parser))
25510 alignas_expr = cp_parser_assignment_expression (parser);
25511 if (alignas_expr == error_mark_node)
25512 cp_parser_skip_to_end_of_statement (parser);
25513 if (alignas_expr == NULL_TREE
25514 || alignas_expr == error_mark_node)
25515 return alignas_expr;
25518 alignas_expr = cxx_alignas_expr (alignas_expr);
25519 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25521 /* Handle alignas (pack...). */
25522 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25524 cp_lexer_consume_token (parser->lexer);
25525 alignas_expr = make_pack_expansion (alignas_expr);
25528 /* Something went wrong, so don't build the attribute. */
25529 if (alignas_expr == error_mark_node)
25530 return error_mark_node;
25532 if (!parens.require_close (parser))
25533 return error_mark_node;
25535 /* Build the C++-11 representation of an 'aligned'
25538 build_tree_list (build_tree_list (get_identifier ("gnu"),
25539 get_identifier ("aligned")),
25546 /* Parse a standard C++-11 attribute-specifier-seq.
25548 attribute-specifier-seq:
25549 attribute-specifier-seq [opt] attribute-specifier
25553 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25555 tree attr_specs = NULL_TREE;
25556 tree attr_last = NULL_TREE;
25560 tree attr_spec = cp_parser_std_attribute_spec (parser);
25561 if (attr_spec == NULL_TREE)
25563 if (attr_spec == error_mark_node)
25564 return error_mark_node;
25567 TREE_CHAIN (attr_last) = attr_spec;
25569 attr_specs = attr_last = attr_spec;
25570 attr_last = tree_last (attr_last);
25576 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25577 return index of the first token after balanced-token, or N on failure. */
25580 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25583 int nparens = 0, nbraces = 0, nsquares = 0;
25585 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25588 case CPP_PRAGMA_EOL:
25589 /* Ran out of tokens. */
25591 case CPP_OPEN_PAREN:
25594 case CPP_OPEN_BRACE:
25597 case CPP_OPEN_SQUARE:
25600 case CPP_CLOSE_PAREN:
25603 case CPP_CLOSE_BRACE:
25606 case CPP_CLOSE_SQUARE:
25612 while (nparens || nbraces || nsquares);
25616 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25617 return index of the first token after the GNU attribute tokens, or N on
25621 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25625 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25626 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25627 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25630 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25633 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25640 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25641 next token), return index of the first token after the standard C++11
25642 attribute tokens, or N on failure. */
25645 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25649 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25650 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25652 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25655 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25659 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25660 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25662 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25673 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25674 as the next token), return index of the first token after the attribute
25675 tokens, or N on failure. */
25678 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25680 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25681 return cp_parser_skip_gnu_attributes_opt (parser, n);
25682 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25685 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25686 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25687 current value of the PEDANTIC flag, regardless of whether or not
25688 the `__extension__' keyword is present. The caller is responsible
25689 for restoring the value of the PEDANTIC flag. */
25692 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25694 /* Save the old value of the PEDANTIC flag. */
25695 *saved_pedantic = pedantic;
25697 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25699 /* Consume the `__extension__' token. */
25700 cp_lexer_consume_token (parser->lexer);
25701 /* We're not being pedantic while the `__extension__' keyword is
25711 /* Parse a label declaration.
25714 __label__ label-declarator-seq ;
25716 label-declarator-seq:
25717 identifier , label-declarator-seq
25721 cp_parser_label_declaration (cp_parser* parser)
25723 /* Look for the `__label__' keyword. */
25724 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25730 /* Look for an identifier. */
25731 identifier = cp_parser_identifier (parser);
25732 /* If we failed, stop. */
25733 if (identifier == error_mark_node)
25735 /* Declare it as a label. */
25736 finish_label_decl (identifier);
25737 /* If the next token is a `;', stop. */
25738 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25740 /* Look for the `,' separating the label declarations. */
25741 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25744 /* Look for the final `;'. */
25745 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25748 // -------------------------------------------------------------------------- //
25751 // Parse a requires clause.
25753 // requires-clause:
25754 // 'requires' logical-or-expression
25756 // The required logical-or-expression must be a constant expression. Note
25757 // that we don't check that the expression is constepxr here. We defer until
25758 // we analyze constraints and then, we only check atomic constraints.
25760 cp_parser_requires_clause (cp_parser *parser)
25762 // Parse the requires clause so that it is not automatically folded.
25763 ++processing_template_decl;
25764 tree expr = cp_parser_binary_expression (parser, false, false,
25765 PREC_NOT_OPERATOR, NULL);
25766 if (check_for_bare_parameter_packs (expr))
25767 expr = error_mark_node;
25768 --processing_template_decl;
25772 // Optionally parse a requires clause:
25774 cp_parser_requires_clause_opt (cp_parser *parser)
25776 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25777 if (tok->keyword != RID_REQUIRES)
25779 if (!flag_concepts && tok->type == CPP_NAME
25780 && tok->u.value == ridpointers[RID_REQUIRES])
25782 error_at (cp_lexer_peek_token (parser->lexer)->location,
25783 "%<requires%> only available with -fconcepts");
25784 /* Parse and discard the requires-clause. */
25785 cp_lexer_consume_token (parser->lexer);
25786 cp_parser_requires_clause (parser);
25790 cp_lexer_consume_token (parser->lexer);
25791 return cp_parser_requires_clause (parser);
25795 /*---------------------------------------------------------------------------
25796 Requires expressions
25797 ---------------------------------------------------------------------------*/
25799 /* Parse a requires expression
25801 requirement-expression:
25802 'requires' requirement-parameter-list [opt] requirement-body */
25804 cp_parser_requires_expression (cp_parser *parser)
25806 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25807 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25809 /* A requires-expression shall appear only within a concept
25810 definition or a requires-clause.
25812 TODO: Implement this diagnostic correctly. */
25813 if (!processing_template_decl)
25815 error_at (loc, "a requires expression cannot appear outside a template");
25816 cp_parser_skip_to_end_of_statement (parser);
25817 return error_mark_node;
25822 /* Local parameters are delared as variables within the scope
25823 of the expression. They are not visible past the end of
25824 the expression. Expressions within the requires-expression
25825 are unevaluated. */
25826 struct scope_sentinel
25830 ++cp_unevaluated_operand;
25831 begin_scope (sk_block, NULL_TREE);
25836 pop_bindings_and_leave_scope ();
25837 --cp_unevaluated_operand;
25841 /* Parse the optional parameter list. */
25842 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25844 parms = cp_parser_requirement_parameter_list (parser);
25845 if (parms == error_mark_node)
25846 return error_mark_node;
25851 /* Parse the requirement body. */
25852 reqs = cp_parser_requirement_body (parser);
25853 if (reqs == error_mark_node)
25854 return error_mark_node;
25857 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25859 grokparms (parms, &parms);
25860 return finish_requires_expr (parms, reqs);
25863 /* Parse a parameterized requirement.
25865 requirement-parameter-list:
25866 '(' parameter-declaration-clause ')' */
25868 cp_parser_requirement_parameter_list (cp_parser *parser)
25870 matching_parens parens;
25871 if (!parens.require_open (parser))
25872 return error_mark_node;
25874 tree parms = cp_parser_parameter_declaration_clause (parser);
25876 if (!parens.require_close (parser))
25877 return error_mark_node;
25882 /* Parse the body of a requirement.
25885 '{' requirement-list '}' */
25887 cp_parser_requirement_body (cp_parser *parser)
25889 matching_braces braces;
25890 if (!braces.require_open (parser))
25891 return error_mark_node;
25893 tree reqs = cp_parser_requirement_list (parser);
25895 if (!braces.require_close (parser))
25896 return error_mark_node;
25901 /* Parse a list of requirements.
25905 requirement-list ';' requirement[opt] */
25907 cp_parser_requirement_list (cp_parser *parser)
25909 tree result = NULL_TREE;
25912 tree req = cp_parser_requirement (parser);
25913 if (req == error_mark_node)
25914 return error_mark_node;
25916 result = tree_cons (NULL_TREE, req, result);
25918 /* If we see a semi-colon, consume it. */
25919 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25920 cp_lexer_consume_token (parser->lexer);
25922 /* Stop processing at the end of the list. */
25923 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25927 /* Reverse the order of requirements so they are analyzed in
25928 declaration order. */
25929 return nreverse (result);
25932 /* Parse a syntactic requirement or type requirement.
25936 compound-requirement
25938 nested-requirement */
25940 cp_parser_requirement (cp_parser *parser)
25942 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25943 return cp_parser_compound_requirement (parser);
25944 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25945 return cp_parser_type_requirement (parser);
25946 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25947 return cp_parser_nested_requirement (parser);
25949 return cp_parser_simple_requirement (parser);
25952 /* Parse a simple requirement.
25954 simple-requirement:
25957 cp_parser_simple_requirement (cp_parser *parser)
25959 tree expr = cp_parser_expression (parser, NULL, false, false);
25960 if (!expr || expr == error_mark_node)
25961 return error_mark_node;
25963 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25964 return error_mark_node;
25966 return finish_simple_requirement (expr);
25969 /* Parse a type requirement
25972 nested-name-specifier [opt] required-type-name ';'
25974 required-type-name:
25976 'template' [opt] simple-template-id */
25978 cp_parser_type_requirement (cp_parser *parser)
25980 cp_lexer_consume_token (parser->lexer);
25982 // Save the scope before parsing name specifiers.
25983 tree saved_scope = parser->scope;
25984 tree saved_object_scope = parser->object_scope;
25985 tree saved_qualifying_scope = parser->qualifying_scope;
25986 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25987 cp_parser_nested_name_specifier_opt (parser,
25988 /*typename_keyword_p=*/true,
25989 /*check_dependency_p=*/false,
25991 /*is_declaration=*/false);
25994 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25996 cp_lexer_consume_token (parser->lexer);
25997 type = cp_parser_template_id (parser,
25998 /*template_keyword_p=*/true,
25999 /*check_dependency=*/false,
26000 /*tag_type=*/none_type,
26001 /*is_declaration=*/false);
26002 type = make_typename_type (parser->scope, type, typename_type,
26003 /*complain=*/tf_error);
26006 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26008 if (TREE_CODE (type) == TYPE_DECL)
26009 type = TREE_TYPE (type);
26011 parser->scope = saved_scope;
26012 parser->object_scope = saved_object_scope;
26013 parser->qualifying_scope = saved_qualifying_scope;
26015 if (type == error_mark_node)
26016 cp_parser_skip_to_end_of_statement (parser);
26018 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26019 return error_mark_node;
26020 if (type == error_mark_node)
26021 return error_mark_node;
26023 return finish_type_requirement (type);
26026 /* Parse a compound requirement
26028 compound-requirement:
26029 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26031 cp_parser_compound_requirement (cp_parser *parser)
26033 /* Parse an expression enclosed in '{ }'s. */
26034 matching_braces braces;
26035 if (!braces.require_open (parser))
26036 return error_mark_node;
26038 tree expr = cp_parser_expression (parser, NULL, false, false);
26039 if (!expr || expr == error_mark_node)
26040 return error_mark_node;
26042 if (!braces.require_close (parser))
26043 return error_mark_node;
26045 /* Parse the optional noexcept. */
26046 bool noexcept_p = false;
26047 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26049 cp_lexer_consume_token (parser->lexer);
26053 /* Parse the optional trailing return type. */
26054 tree type = NULL_TREE;
26055 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26057 cp_lexer_consume_token (parser->lexer);
26058 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26059 parser->in_result_type_constraint_p = true;
26060 type = cp_parser_trailing_type_id (parser);
26061 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26062 if (type == error_mark_node)
26063 return error_mark_node;
26066 return finish_compound_requirement (expr, type, noexcept_p);
26069 /* Parse a nested requirement. This is the same as a requires clause.
26071 nested-requirement:
26074 cp_parser_nested_requirement (cp_parser *parser)
26076 cp_lexer_consume_token (parser->lexer);
26077 tree req = cp_parser_requires_clause (parser);
26078 if (req == error_mark_node)
26079 return error_mark_node;
26080 return finish_nested_requirement (req);
26083 /* Support Functions */
26085 /* Return the appropriate prefer_type argument for lookup_name_real based on
26086 tag_type and template_mem_access. */
26089 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26091 /* DR 141: When looking in the current enclosing context for a template-name
26092 after -> or ., only consider class templates. */
26093 if (template_mem_access)
26097 case none_type: return 0; // No preference.
26098 case scope_type: return 1; // Type or namespace.
26099 default: return 2; // Type only.
26103 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26104 NAME should have one of the representations used for an
26105 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26106 is returned. If PARSER->SCOPE is a dependent type, then a
26107 SCOPE_REF is returned.
26109 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26110 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26111 was formed. Abstractly, such entities should not be passed to this
26112 function, because they do not need to be looked up, but it is
26113 simpler to check for this special case here, rather than at the
26116 In cases not explicitly covered above, this function returns a
26117 DECL, OVERLOAD, or baselink representing the result of the lookup.
26118 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26121 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26122 (e.g., "struct") that was used. In that case bindings that do not
26123 refer to types are ignored.
26125 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26128 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26131 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26134 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26135 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26136 NULL_TREE otherwise. */
26139 cp_parser_lookup_name (cp_parser *parser, tree name,
26140 enum tag_types tag_type,
26143 bool check_dependency,
26144 tree *ambiguous_decls,
26145 location_t name_location)
26148 tree object_type = parser->context->object_type;
26150 /* Assume that the lookup will be unambiguous. */
26151 if (ambiguous_decls)
26152 *ambiguous_decls = NULL_TREE;
26154 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26155 no longer valid. Note that if we are parsing tentatively, and
26156 the parse fails, OBJECT_TYPE will be automatically restored. */
26157 parser->context->object_type = NULL_TREE;
26159 if (name == error_mark_node)
26160 return error_mark_node;
26162 /* A template-id has already been resolved; there is no lookup to
26164 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26166 if (BASELINK_P (name))
26168 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26169 == TEMPLATE_ID_EXPR);
26173 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26174 it should already have been checked to make sure that the name
26175 used matches the type being destroyed. */
26176 if (TREE_CODE (name) == BIT_NOT_EXPR)
26180 /* Figure out to which type this destructor applies. */
26182 type = parser->scope;
26183 else if (object_type)
26184 type = object_type;
26186 type = current_class_type;
26187 /* If that's not a class type, there is no destructor. */
26188 if (!type || !CLASS_TYPE_P (type))
26189 return error_mark_node;
26191 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26192 lazily_declare_fn (sfk_destructor, type);
26194 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26197 return error_mark_node;
26200 /* By this point, the NAME should be an ordinary identifier. If
26201 the id-expression was a qualified name, the qualifying scope is
26202 stored in PARSER->SCOPE at this point. */
26203 gcc_assert (identifier_p (name));
26205 /* Perform the lookup. */
26210 if (parser->scope == error_mark_node)
26211 return error_mark_node;
26213 /* If the SCOPE is dependent, the lookup must be deferred until
26214 the template is instantiated -- unless we are explicitly
26215 looking up names in uninstantiated templates. Even then, we
26216 cannot look up the name if the scope is not a class type; it
26217 might, for example, be a template type parameter. */
26218 dependent_p = (TYPE_P (parser->scope)
26219 && dependent_scope_p (parser->scope));
26220 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26222 /* Defer lookup. */
26223 decl = error_mark_node;
26226 tree pushed_scope = NULL_TREE;
26228 /* If PARSER->SCOPE is a dependent type, then it must be a
26229 class type, and we must not be checking dependencies;
26230 otherwise, we would have processed this lookup above. So
26231 that PARSER->SCOPE is not considered a dependent base by
26232 lookup_member, we must enter the scope here. */
26234 pushed_scope = push_scope (parser->scope);
26236 /* If the PARSER->SCOPE is a template specialization, it
26237 may be instantiated during name lookup. In that case,
26238 errors may be issued. Even if we rollback the current
26239 tentative parse, those errors are valid. */
26240 decl = lookup_qualified_name (parser->scope, name,
26241 prefer_type_arg (tag_type),
26242 /*complain=*/true);
26244 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26245 lookup result and the nested-name-specifier nominates a class C:
26246 * if the name specified after the nested-name-specifier, when
26247 looked up in C, is the injected-class-name of C (Clause 9), or
26248 * if the name specified after the nested-name-specifier is the
26249 same as the identifier or the simple-template-id's template-
26250 name in the last component of the nested-name-specifier,
26251 the name is instead considered to name the constructor of
26252 class C. [ Note: for example, the constructor is not an
26253 acceptable lookup result in an elaborated-type-specifier so
26254 the constructor would not be used in place of the
26255 injected-class-name. --end note ] Such a constructor name
26256 shall be used only in the declarator-id of a declaration that
26257 names a constructor or in a using-declaration. */
26258 if (tag_type == none_type
26259 && DECL_SELF_REFERENCE_P (decl)
26260 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26261 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26262 prefer_type_arg (tag_type),
26263 /*complain=*/true);
26265 /* If we have a single function from a using decl, pull it out. */
26266 if (TREE_CODE (decl) == OVERLOAD
26267 && !really_overloaded_fn (decl))
26268 decl = OVL_FUNCTION (decl);
26271 pop_scope (pushed_scope);
26274 /* If the scope is a dependent type and either we deferred lookup or
26275 we did lookup but didn't find the name, rememeber the name. */
26276 if (decl == error_mark_node && TYPE_P (parser->scope)
26277 && dependent_type_p (parser->scope))
26283 /* The resolution to Core Issue 180 says that `struct
26284 A::B' should be considered a type-name, even if `A'
26286 type = make_typename_type (parser->scope, name, tag_type,
26287 /*complain=*/tf_error);
26288 if (type != error_mark_node)
26289 decl = TYPE_NAME (type);
26291 else if (is_template
26292 && (cp_parser_next_token_ends_template_argument_p (parser)
26293 || cp_lexer_next_token_is (parser->lexer,
26295 decl = make_unbound_class_template (parser->scope,
26297 /*complain=*/tf_error);
26299 decl = build_qualified_name (/*type=*/NULL_TREE,
26300 parser->scope, name,
26303 parser->qualifying_scope = parser->scope;
26304 parser->object_scope = NULL_TREE;
26306 else if (object_type)
26308 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26309 OBJECT_TYPE is not a class. */
26310 if (CLASS_TYPE_P (object_type))
26311 /* If the OBJECT_TYPE is a template specialization, it may
26312 be instantiated during name lookup. In that case, errors
26313 may be issued. Even if we rollback the current tentative
26314 parse, those errors are valid. */
26315 decl = lookup_member (object_type,
26318 prefer_type_arg (tag_type),
26319 tf_warning_or_error);
26324 /* Look it up in the enclosing context. DR 141: When looking for a
26325 template-name after -> or ., only consider class templates. */
26326 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26328 /*block_p=*/true, is_namespace, 0);
26329 if (object_type == unknown_type_node)
26330 /* The object is type-dependent, so we can't look anything up; we used
26331 this to get the DR 141 behavior. */
26332 object_type = NULL_TREE;
26333 parser->object_scope = object_type;
26334 parser->qualifying_scope = NULL_TREE;
26338 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26340 /*block_p=*/true, is_namespace, 0);
26341 parser->qualifying_scope = NULL_TREE;
26342 parser->object_scope = NULL_TREE;
26345 /* If the lookup failed, let our caller know. */
26346 if (!decl || decl == error_mark_node)
26347 return error_mark_node;
26349 /* Pull out the template from an injected-class-name (or multiple). */
26351 decl = maybe_get_template_decl_from_type_decl (decl);
26353 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26354 if (TREE_CODE (decl) == TREE_LIST)
26356 if (ambiguous_decls)
26357 *ambiguous_decls = decl;
26358 /* The error message we have to print is too complicated for
26359 cp_parser_error, so we incorporate its actions directly. */
26360 if (!cp_parser_simulate_error (parser))
26362 error_at (name_location, "reference to %qD is ambiguous",
26364 print_candidates (decl);
26366 return error_mark_node;
26369 gcc_assert (DECL_P (decl)
26370 || TREE_CODE (decl) == OVERLOAD
26371 || TREE_CODE (decl) == SCOPE_REF
26372 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26373 || BASELINK_P (decl));
26375 /* If we have resolved the name of a member declaration, check to
26376 see if the declaration is accessible. When the name resolves to
26377 set of overloaded functions, accessibility is checked when
26378 overload resolution is done.
26380 During an explicit instantiation, access is not checked at all,
26381 as per [temp.explicit]. */
26383 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26385 maybe_record_typedef_use (decl);
26387 return cp_expr (decl, name_location);
26390 /* Like cp_parser_lookup_name, but for use in the typical case where
26391 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26392 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26395 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26397 return cp_parser_lookup_name (parser, name,
26399 /*is_template=*/false,
26400 /*is_namespace=*/false,
26401 /*check_dependency=*/true,
26402 /*ambiguous_decls=*/NULL,
26406 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26407 the current context, return the TYPE_DECL. If TAG_NAME_P is
26408 true, the DECL indicates the class being defined in a class-head,
26409 or declared in an elaborated-type-specifier.
26411 Otherwise, return DECL. */
26414 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26416 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26417 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26420 template <typename T> struct B;
26423 template <typename T> struct A::B {};
26425 Similarly, in an elaborated-type-specifier:
26427 namespace N { struct X{}; }
26430 template <typename T> friend struct N::X;
26433 However, if the DECL refers to a class type, and we are in
26434 the scope of the class, then the name lookup automatically
26435 finds the TYPE_DECL created by build_self_reference rather
26436 than a TEMPLATE_DECL. For example, in:
26438 template <class T> struct S {
26442 there is no need to handle such case. */
26444 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26445 return DECL_TEMPLATE_RESULT (decl);
26450 /* If too many, or too few, template-parameter lists apply to the
26451 declarator, issue an error message. Returns TRUE if all went well,
26452 and FALSE otherwise. */
26455 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26456 cp_declarator *declarator,
26457 location_t declarator_location)
26459 switch (declarator->kind)
26463 unsigned num_templates = 0;
26464 tree scope = declarator->u.id.qualifying_scope;
26465 bool template_id_p = false;
26468 num_templates = num_template_headers_for_class (scope);
26469 else if (TREE_CODE (declarator->u.id.unqualified_name)
26470 == TEMPLATE_ID_EXPR)
26472 /* If the DECLARATOR has the form `X<y>' then it uses one
26473 additional level of template parameters. */
26475 template_id_p = true;
26478 return cp_parser_check_template_parameters
26479 (parser, num_templates, template_id_p, declarator_location,
26486 case cdk_reference:
26488 return (cp_parser_check_declarator_template_parameters
26489 (parser, declarator->declarator, declarator_location));
26496 gcc_unreachable ();
26501 /* NUM_TEMPLATES were used in the current declaration. If that is
26502 invalid, return FALSE and issue an error messages. Otherwise,
26503 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26504 declarator and we can print more accurate diagnostics. */
26507 cp_parser_check_template_parameters (cp_parser* parser,
26508 unsigned num_templates,
26509 bool template_id_p,
26510 location_t location,
26511 cp_declarator *declarator)
26513 /* If there are the same number of template classes and parameter
26514 lists, that's OK. */
26515 if (parser->num_template_parameter_lists == num_templates)
26517 /* If there are more, but only one more, and the name ends in an identifier,
26518 then we are declaring a primary template. That's OK too. */
26520 && parser->num_template_parameter_lists == num_templates + 1)
26522 /* If there are more template classes than parameter lists, we have
26525 template <class T> void S<T>::R<T>::f (); */
26526 if (parser->num_template_parameter_lists < num_templates)
26528 if (declarator && !current_function_decl)
26529 error_at (location, "specializing member %<%T::%E%> "
26530 "requires %<template<>%> syntax",
26531 declarator->u.id.qualifying_scope,
26532 declarator->u.id.unqualified_name);
26533 else if (declarator)
26534 error_at (location, "invalid declaration of %<%T::%E%>",
26535 declarator->u.id.qualifying_scope,
26536 declarator->u.id.unqualified_name);
26538 error_at (location, "too few template-parameter-lists");
26541 /* Otherwise, there are too many template parameter lists. We have
26544 template <class T> template <class U> void S::f(); */
26545 error_at (location, "too many template-parameter-lists");
26549 /* Parse an optional `::' token indicating that the following name is
26550 from the global namespace. If so, PARSER->SCOPE is set to the
26551 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26552 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26553 Returns the new value of PARSER->SCOPE, if the `::' token is
26554 present, and NULL_TREE otherwise. */
26557 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26561 /* Peek at the next token. */
26562 token = cp_lexer_peek_token (parser->lexer);
26563 /* If we're looking at a `::' token then we're starting from the
26564 global namespace, not our current location. */
26565 if (token->type == CPP_SCOPE)
26567 /* Consume the `::' token. */
26568 cp_lexer_consume_token (parser->lexer);
26569 /* Set the SCOPE so that we know where to start the lookup. */
26570 parser->scope = global_namespace;
26571 parser->qualifying_scope = global_namespace;
26572 parser->object_scope = NULL_TREE;
26574 return parser->scope;
26576 else if (!current_scope_valid_p)
26578 parser->scope = NULL_TREE;
26579 parser->qualifying_scope = NULL_TREE;
26580 parser->object_scope = NULL_TREE;
26586 /* Returns TRUE if the upcoming token sequence is the start of a
26587 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26588 declarator is preceded by the `friend' specifier. */
26591 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26593 bool constructor_p;
26594 bool outside_class_specifier_p;
26595 tree nested_name_specifier;
26596 cp_token *next_token;
26598 /* The common case is that this is not a constructor declarator, so
26599 try to avoid doing lots of work if at all possible. It's not
26600 valid declare a constructor at function scope. */
26601 if (parser->in_function_body)
26603 /* And only certain tokens can begin a constructor declarator. */
26604 next_token = cp_lexer_peek_token (parser->lexer);
26605 if (next_token->type != CPP_NAME
26606 && next_token->type != CPP_SCOPE
26607 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26608 && next_token->type != CPP_TEMPLATE_ID)
26611 /* Parse tentatively; we are going to roll back all of the tokens
26613 cp_parser_parse_tentatively (parser);
26614 /* Assume that we are looking at a constructor declarator. */
26615 constructor_p = true;
26617 /* Look for the optional `::' operator. */
26618 cp_parser_global_scope_opt (parser,
26619 /*current_scope_valid_p=*/false);
26620 /* Look for the nested-name-specifier. */
26621 nested_name_specifier
26622 = (cp_parser_nested_name_specifier_opt (parser,
26623 /*typename_keyword_p=*/false,
26624 /*check_dependency_p=*/false,
26626 /*is_declaration=*/false));
26628 outside_class_specifier_p = (!at_class_scope_p ()
26629 || !TYPE_BEING_DEFINED (current_class_type)
26632 /* Outside of a class-specifier, there must be a
26633 nested-name-specifier. Except in C++17 mode, where we
26634 might be declaring a guiding declaration. */
26635 if (!nested_name_specifier && outside_class_specifier_p
26636 && cxx_dialect < cxx17)
26637 constructor_p = false;
26638 else if (nested_name_specifier == error_mark_node)
26639 constructor_p = false;
26641 /* If we have a class scope, this is easy; DR 147 says that S::S always
26642 names the constructor, and no other qualified name could. */
26643 if (constructor_p && nested_name_specifier
26644 && CLASS_TYPE_P (nested_name_specifier))
26646 tree id = cp_parser_unqualified_id (parser,
26647 /*template_keyword_p=*/false,
26648 /*check_dependency_p=*/false,
26649 /*declarator_p=*/true,
26650 /*optional_p=*/false);
26651 if (is_overloaded_fn (id))
26652 id = DECL_NAME (get_first_fn (id));
26653 if (!constructor_name_p (id, nested_name_specifier))
26654 constructor_p = false;
26656 /* If we still think that this might be a constructor-declarator,
26657 look for a class-name. */
26658 else if (constructor_p)
26662 template <typename T> struct S {
26666 we must recognize that the nested `S' names a class. */
26667 if (cxx_dialect >= cxx17)
26668 cp_parser_parse_tentatively (parser);
26671 type_decl = cp_parser_class_name (parser,
26672 /*typename_keyword_p=*/false,
26673 /*template_keyword_p=*/false,
26675 /*check_dependency_p=*/false,
26676 /*class_head_p=*/false,
26677 /*is_declaration=*/false);
26679 if (cxx_dialect >= cxx17
26680 && !cp_parser_parse_definitely (parser))
26682 type_decl = NULL_TREE;
26683 tree tmpl = cp_parser_template_name (parser,
26684 /*template_keyword*/false,
26685 /*check_dependency_p*/false,
26686 /*is_declaration*/false,
26688 /*is_identifier*/NULL);
26689 if (DECL_CLASS_TEMPLATE_P (tmpl)
26690 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26691 /* It's a deduction guide, return true. */;
26693 cp_parser_simulate_error (parser);
26696 /* If there was no class-name, then this is not a constructor.
26697 Otherwise, if we are in a class-specifier and we aren't
26698 handling a friend declaration, check that its type matches
26699 current_class_type (c++/38313). Note: error_mark_node
26700 is left alone for error recovery purposes. */
26701 constructor_p = (!cp_parser_error_occurred (parser)
26702 && (outside_class_specifier_p
26703 || type_decl == NULL_TREE
26704 || type_decl == error_mark_node
26705 || same_type_p (current_class_type,
26706 TREE_TYPE (type_decl))));
26708 /* If we're still considering a constructor, we have to see a `(',
26709 to begin the parameter-declaration-clause, followed by either a
26710 `)', an `...', or a decl-specifier. We need to check for a
26711 type-specifier to avoid being fooled into thinking that:
26715 is a constructor. (It is actually a function named `f' that
26716 takes one parameter (of type `int') and returns a value of type
26719 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26720 constructor_p = false;
26723 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26724 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26725 /* A parameter declaration begins with a decl-specifier,
26726 which is either the "attribute" keyword, a storage class
26727 specifier, or (usually) a type-specifier. */
26728 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26731 tree pushed_scope = NULL_TREE;
26732 unsigned saved_num_template_parameter_lists;
26734 /* Names appearing in the type-specifier should be looked up
26735 in the scope of the class. */
26736 if (current_class_type)
26738 else if (type_decl)
26740 type = TREE_TYPE (type_decl);
26741 if (TREE_CODE (type) == TYPENAME_TYPE)
26743 type = resolve_typename_type (type,
26744 /*only_current_p=*/false);
26745 if (TREE_CODE (type) == TYPENAME_TYPE)
26747 cp_parser_abort_tentative_parse (parser);
26751 pushed_scope = push_scope (type);
26754 /* Inside the constructor parameter list, surrounding
26755 template-parameter-lists do not apply. */
26756 saved_num_template_parameter_lists
26757 = parser->num_template_parameter_lists;
26758 parser->num_template_parameter_lists = 0;
26760 /* Look for the type-specifier. */
26761 cp_parser_type_specifier (parser,
26762 CP_PARSER_FLAGS_NONE,
26763 /*decl_specs=*/NULL,
26764 /*is_declarator=*/true,
26765 /*declares_class_or_enum=*/NULL,
26766 /*is_cv_qualifier=*/NULL);
26768 parser->num_template_parameter_lists
26769 = saved_num_template_parameter_lists;
26771 /* Leave the scope of the class. */
26773 pop_scope (pushed_scope);
26775 constructor_p = !cp_parser_error_occurred (parser);
26779 /* We did not really want to consume any tokens. */
26780 cp_parser_abort_tentative_parse (parser);
26782 return constructor_p;
26785 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26786 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26787 they must be performed once we are in the scope of the function.
26789 Returns the function defined. */
26792 cp_parser_function_definition_from_specifiers_and_declarator
26793 (cp_parser* parser,
26794 cp_decl_specifier_seq *decl_specifiers,
26796 const cp_declarator *declarator)
26801 /* Begin the function-definition. */
26802 success_p = start_function (decl_specifiers, declarator, attributes);
26804 /* The things we're about to see are not directly qualified by any
26805 template headers we've seen thus far. */
26806 reset_specialization ();
26808 /* If there were names looked up in the decl-specifier-seq that we
26809 did not check, check them now. We must wait until we are in the
26810 scope of the function to perform the checks, since the function
26811 might be a friend. */
26812 perform_deferred_access_checks (tf_warning_or_error);
26816 cp_finalize_omp_declare_simd (parser, current_function_decl);
26817 parser->omp_declare_simd = NULL;
26818 cp_finalize_oacc_routine (parser, current_function_decl, true);
26819 parser->oacc_routine = NULL;
26824 /* Skip the entire function. */
26825 cp_parser_skip_to_end_of_block_or_statement (parser);
26826 fn = error_mark_node;
26828 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26830 /* Seen already, skip it. An error message has already been output. */
26831 cp_parser_skip_to_end_of_block_or_statement (parser);
26832 fn = current_function_decl;
26833 current_function_decl = NULL_TREE;
26834 /* If this is a function from a class, pop the nested class. */
26835 if (current_class_name)
26836 pop_nested_class ();
26841 if (DECL_DECLARED_INLINE_P (current_function_decl))
26842 tv = TV_PARSE_INLINE;
26844 tv = TV_PARSE_FUNC;
26846 fn = cp_parser_function_definition_after_declarator (parser,
26847 /*inline_p=*/false);
26854 /* Parse the part of a function-definition that follows the
26855 declarator. INLINE_P is TRUE iff this function is an inline
26856 function defined within a class-specifier.
26858 Returns the function defined. */
26861 cp_parser_function_definition_after_declarator (cp_parser* parser,
26865 bool saved_in_unbraced_linkage_specification_p;
26866 bool saved_in_function_body;
26867 unsigned saved_num_template_parameter_lists;
26869 bool fully_implicit_function_template_p
26870 = parser->fully_implicit_function_template_p;
26871 parser->fully_implicit_function_template_p = false;
26872 tree implicit_template_parms
26873 = parser->implicit_template_parms;
26874 parser->implicit_template_parms = 0;
26875 cp_binding_level* implicit_template_scope
26876 = parser->implicit_template_scope;
26877 parser->implicit_template_scope = 0;
26879 saved_in_function_body = parser->in_function_body;
26880 parser->in_function_body = true;
26881 /* If the next token is `return', then the code may be trying to
26882 make use of the "named return value" extension that G++ used to
26884 token = cp_lexer_peek_token (parser->lexer);
26885 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26887 /* Consume the `return' keyword. */
26888 cp_lexer_consume_token (parser->lexer);
26889 /* Look for the identifier that indicates what value is to be
26891 cp_parser_identifier (parser);
26892 /* Issue an error message. */
26893 error_at (token->location,
26894 "named return values are no longer supported");
26895 /* Skip tokens until we reach the start of the function body. */
26898 cp_token *token = cp_lexer_peek_token (parser->lexer);
26899 if (token->type == CPP_OPEN_BRACE
26900 || token->type == CPP_EOF
26901 || token->type == CPP_PRAGMA_EOL)
26903 cp_lexer_consume_token (parser->lexer);
26906 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26907 anything declared inside `f'. */
26908 saved_in_unbraced_linkage_specification_p
26909 = parser->in_unbraced_linkage_specification_p;
26910 parser->in_unbraced_linkage_specification_p = false;
26911 /* Inside the function, surrounding template-parameter-lists do not
26913 saved_num_template_parameter_lists
26914 = parser->num_template_parameter_lists;
26915 parser->num_template_parameter_lists = 0;
26917 /* If the next token is `try', `__transaction_atomic', or
26918 `__transaction_relaxed`, then we are looking at either function-try-block
26919 or function-transaction-block. Note that all of these include the
26921 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26922 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26923 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26924 RID_TRANSACTION_RELAXED))
26925 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26926 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26927 cp_parser_function_try_block (parser);
26929 cp_parser_ctor_initializer_opt_and_function_body
26930 (parser, /*in_function_try_block=*/false);
26932 /* Finish the function. */
26933 fn = finish_function (inline_p);
26934 /* Generate code for it, if necessary. */
26935 expand_or_defer_fn (fn);
26936 /* Restore the saved values. */
26937 parser->in_unbraced_linkage_specification_p
26938 = saved_in_unbraced_linkage_specification_p;
26939 parser->num_template_parameter_lists
26940 = saved_num_template_parameter_lists;
26941 parser->in_function_body = saved_in_function_body;
26943 parser->fully_implicit_function_template_p
26944 = fully_implicit_function_template_p;
26945 parser->implicit_template_parms
26946 = implicit_template_parms;
26947 parser->implicit_template_scope
26948 = implicit_template_scope;
26950 if (parser->fully_implicit_function_template_p)
26951 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26956 /* Parse a template-declaration body (following argument list). */
26959 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26960 tree parameter_list,
26963 tree decl = NULL_TREE;
26964 bool friend_p = false;
26966 /* We just processed one more parameter list. */
26967 ++parser->num_template_parameter_lists;
26969 /* Get the deferred access checks from the parameter list. These
26970 will be checked once we know what is being declared, as for a
26971 member template the checks must be performed in the scope of the
26972 class containing the member. */
26973 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26975 /* Tentatively parse for a new template parameter list, which can either be
26976 the template keyword or a template introduction. */
26977 if (cp_parser_template_declaration_after_export (parser, member_p))
26979 else if (cxx_dialect >= cxx11
26980 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26981 decl = cp_parser_alias_declaration (parser);
26984 /* There are no access checks when parsing a template, as we do not
26985 know if a specialization will be a friend. */
26986 push_deferring_access_checks (dk_no_check);
26987 cp_token *token = cp_lexer_peek_token (parser->lexer);
26988 decl = cp_parser_single_declaration (parser,
26991 /*explicit_specialization_p=*/false,
26993 pop_deferring_access_checks ();
26995 /* If this is a member template declaration, let the front
26997 if (member_p && !friend_p && decl)
26999 if (TREE_CODE (decl) == TYPE_DECL)
27000 cp_parser_check_access_in_redeclaration (decl, token->location);
27002 decl = finish_member_template_decl (decl);
27004 else if (friend_p && decl
27005 && DECL_DECLARES_TYPE_P (decl))
27006 make_friend_class (current_class_type, TREE_TYPE (decl),
27007 /*complain=*/true);
27009 /* We are done with the current parameter list. */
27010 --parser->num_template_parameter_lists;
27012 pop_deferring_access_checks ();
27015 finish_template_decl (parameter_list);
27017 /* Check the template arguments for a literal operator template. */
27019 && DECL_DECLARES_FUNCTION_P (decl)
27020 && UDLIT_OPER_P (DECL_NAME (decl)))
27023 if (parameter_list == NULL_TREE)
27027 int num_parms = TREE_VEC_LENGTH (parameter_list);
27028 if (num_parms == 1)
27030 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27031 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27032 if (TREE_TYPE (parm) != char_type_node
27033 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27036 else if (num_parms == 2 && cxx_dialect >= cxx14)
27038 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27039 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27040 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27041 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27042 if (parm == error_mark_node
27043 || TREE_TYPE (parm) != TREE_TYPE (type)
27044 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27052 if (cxx_dialect >= cxx14)
27053 error ("literal operator template %qD has invalid parameter list."
27054 " Expected non-type template argument pack <char...>"
27055 " or <typename CharT, CharT...>",
27058 error ("literal operator template %qD has invalid parameter list."
27059 " Expected non-type template argument pack <char...>",
27064 /* Register member declarations. */
27065 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27066 finish_member_declaration (decl);
27067 /* If DECL is a function template, we must return to parse it later.
27068 (Even though there is no definition, there might be default
27069 arguments that need handling.) */
27070 if (member_p && decl
27071 && DECL_DECLARES_FUNCTION_P (decl))
27072 vec_safe_push (unparsed_funs_with_definitions, decl);
27075 /* Parse a template introduction header for a template-declaration. Returns
27076 false if tentative parse fails. */
27079 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27081 cp_parser_parse_tentatively (parser);
27083 tree saved_scope = parser->scope;
27084 tree saved_object_scope = parser->object_scope;
27085 tree saved_qualifying_scope = parser->qualifying_scope;
27087 /* Look for the optional `::' operator. */
27088 cp_parser_global_scope_opt (parser,
27089 /*current_scope_valid_p=*/false);
27090 /* Look for the nested-name-specifier. */
27091 cp_parser_nested_name_specifier_opt (parser,
27092 /*typename_keyword_p=*/false,
27093 /*check_dependency_p=*/true,
27095 /*is_declaration=*/false);
27097 cp_token *token = cp_lexer_peek_token (parser->lexer);
27098 tree concept_name = cp_parser_identifier (parser);
27100 /* Look up the concept for which we will be matching
27101 template parameters. */
27102 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27104 parser->scope = saved_scope;
27105 parser->object_scope = saved_object_scope;
27106 parser->qualifying_scope = saved_qualifying_scope;
27108 if (concept_name == error_mark_node)
27109 cp_parser_simulate_error (parser);
27111 /* Look for opening brace for introduction. */
27112 matching_braces braces;
27113 braces.require_open (parser);
27115 if (!cp_parser_parse_definitely (parser))
27118 push_deferring_access_checks (dk_deferred);
27120 /* Build vector of placeholder parameters and grab
27121 matching identifiers. */
27122 tree introduction_list = cp_parser_introduction_list (parser);
27124 /* The introduction-list shall not be empty. */
27125 int nargs = TREE_VEC_LENGTH (introduction_list);
27128 error ("empty introduction-list");
27132 /* Look for closing brace for introduction. */
27133 if (!braces.require_close (parser))
27136 if (tmpl_decl == error_mark_node)
27138 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27143 /* Build and associate the constraint. */
27144 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27145 if (parms && parms != error_mark_node)
27147 cp_parser_template_declaration_after_parameters (parser, parms,
27152 error_at (token->location, "no matching concept for template-introduction");
27156 /* Parse a normal template-declaration following the template keyword. */
27159 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27161 tree parameter_list;
27162 bool need_lang_pop;
27163 location_t location = input_location;
27165 /* Look for the `<' token. */
27166 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27168 if (at_class_scope_p () && current_function_decl)
27170 /* 14.5.2.2 [temp.mem]
27172 A local class shall not have member templates. */
27173 error_at (location,
27174 "invalid declaration of member template in local class");
27175 cp_parser_skip_to_end_of_block_or_statement (parser);
27180 A template ... shall not have C linkage. */
27181 if (current_lang_name == lang_name_c)
27183 error_at (location, "template with C linkage");
27184 maybe_show_extern_c_location ();
27185 /* Give it C++ linkage to avoid confusing other parts of the
27187 push_lang_context (lang_name_cplusplus);
27188 need_lang_pop = true;
27191 need_lang_pop = false;
27193 /* We cannot perform access checks on the template parameter
27194 declarations until we know what is being declared, just as we
27195 cannot check the decl-specifier list. */
27196 push_deferring_access_checks (dk_deferred);
27198 /* If the next token is `>', then we have an invalid
27199 specialization. Rather than complain about an invalid template
27200 parameter, issue an error message here. */
27201 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27203 cp_parser_error (parser, "invalid explicit specialization");
27204 begin_specialization ();
27205 parameter_list = NULL_TREE;
27209 /* Parse the template parameters. */
27210 parameter_list = cp_parser_template_parameter_list (parser);
27213 /* Look for the `>'. */
27214 cp_parser_skip_to_end_of_template_parameter_list (parser);
27216 /* Manage template requirements */
27219 tree reqs = get_shorthand_constraints (current_template_parms);
27220 if (tree r = cp_parser_requires_clause_opt (parser))
27221 reqs = conjoin_constraints (reqs, normalize_expression (r));
27222 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27225 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27228 /* For the erroneous case of a template with C linkage, we pushed an
27229 implicit C++ linkage scope; exit that scope now. */
27231 pop_lang_context ();
27234 /* Parse a template-declaration, assuming that the `export' (and
27235 `extern') keywords, if present, has already been scanned. MEMBER_P
27236 is as for cp_parser_template_declaration. */
27239 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27241 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27243 cp_lexer_consume_token (parser->lexer);
27244 cp_parser_explicit_template_declaration (parser, member_p);
27247 else if (flag_concepts)
27248 return cp_parser_template_introduction (parser, member_p);
27253 /* Perform the deferred access checks from a template-parameter-list.
27254 CHECKS is a TREE_LIST of access checks, as returned by
27255 get_deferred_access_checks. */
27258 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27260 ++processing_template_parmlist;
27261 perform_access_checks (checks, tf_warning_or_error);
27262 --processing_template_parmlist;
27265 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27266 `function-definition' sequence that follows a template header.
27267 If MEMBER_P is true, this declaration appears in a class scope.
27269 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27270 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27273 cp_parser_single_declaration (cp_parser* parser,
27274 vec<deferred_access_check, va_gc> *checks,
27276 bool explicit_specialization_p,
27279 int declares_class_or_enum;
27280 tree decl = NULL_TREE;
27281 cp_decl_specifier_seq decl_specifiers;
27282 bool function_definition_p = false;
27283 cp_token *decl_spec_token_start;
27285 /* This function is only used when processing a template
27287 gcc_assert (innermost_scope_kind () == sk_template_parms
27288 || innermost_scope_kind () == sk_template_spec);
27290 /* Defer access checks until we know what is being declared. */
27291 push_deferring_access_checks (dk_deferred);
27293 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27295 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27296 cp_parser_decl_specifier_seq (parser,
27297 CP_PARSER_FLAGS_OPTIONAL,
27299 &declares_class_or_enum);
27301 *friend_p = cp_parser_friend_p (&decl_specifiers);
27303 /* There are no template typedefs. */
27304 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27306 error_at (decl_spec_token_start->location,
27307 "template declaration of %<typedef%>");
27308 decl = error_mark_node;
27311 /* Gather up the access checks that occurred the
27312 decl-specifier-seq. */
27313 stop_deferring_access_checks ();
27315 /* Check for the declaration of a template class. */
27316 if (declares_class_or_enum)
27318 if (cp_parser_declares_only_class_p (parser)
27319 || (declares_class_or_enum & 2))
27321 // If this is a declaration, but not a definition, associate
27322 // any constraints with the type declaration. Constraints
27323 // are associated with definitions in cp_parser_class_specifier.
27324 if (declares_class_or_enum == 1)
27325 associate_classtype_constraints (decl_specifiers.type);
27327 decl = shadow_tag (&decl_specifiers);
27332 friend template <typename T> struct A<T>::B;
27335 A<T>::B will be represented by a TYPENAME_TYPE, and
27336 therefore not recognized by shadow_tag. */
27337 if (friend_p && *friend_p
27339 && decl_specifiers.type
27340 && TYPE_P (decl_specifiers.type))
27341 decl = decl_specifiers.type;
27343 if (decl && decl != error_mark_node)
27344 decl = TYPE_NAME (decl);
27346 decl = error_mark_node;
27348 /* Perform access checks for template parameters. */
27349 cp_parser_perform_template_parameter_access_checks (checks);
27351 /* Give a helpful diagnostic for
27352 template <class T> struct A { } a;
27353 if we aren't already recovering from an error. */
27354 if (!cp_parser_declares_only_class_p (parser)
27357 error_at (cp_lexer_peek_token (parser->lexer)->location,
27358 "a class template declaration must not declare "
27360 cp_parser_skip_to_end_of_block_or_statement (parser);
27366 /* Complain about missing 'typename' or other invalid type names. */
27367 if (!decl_specifiers.any_type_specifiers_p
27368 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27370 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27371 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27372 the rest of this declaration. */
27373 decl = error_mark_node;
27377 /* If it's not a template class, try for a template function. If
27378 the next token is a `;', then this declaration does not declare
27379 anything. But, if there were errors in the decl-specifiers, then
27380 the error might well have come from an attempted class-specifier.
27381 In that case, there's no need to warn about a missing declarator. */
27383 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27384 || decl_specifiers.type != error_mark_node))
27386 decl = cp_parser_init_declarator (parser,
27389 /*function_definition_allowed_p=*/true,
27391 declares_class_or_enum,
27392 &function_definition_p,
27395 /* 7.1.1-1 [dcl.stc]
27397 A storage-class-specifier shall not be specified in an explicit
27398 specialization... */
27400 && explicit_specialization_p
27401 && decl_specifiers.storage_class != sc_none)
27403 error_at (decl_spec_token_start->location,
27404 "explicit template specialization cannot have a storage class");
27405 decl = error_mark_node;
27408 if (decl && VAR_P (decl))
27409 check_template_variable (decl);
27412 /* Look for a trailing `;' after the declaration. */
27413 if (!function_definition_p
27414 && (decl == error_mark_node
27415 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27416 cp_parser_skip_to_end_of_block_or_statement (parser);
27419 pop_deferring_access_checks ();
27421 /* Clear any current qualification; whatever comes next is the start
27422 of something new. */
27423 parser->scope = NULL_TREE;
27424 parser->qualifying_scope = NULL_TREE;
27425 parser->object_scope = NULL_TREE;
27430 /* Parse a cast-expression that is not the operand of a unary "&". */
27433 cp_parser_simple_cast_expression (cp_parser *parser)
27435 return cp_parser_cast_expression (parser, /*address_p=*/false,
27436 /*cast_p=*/false, /*decltype*/false, NULL);
27439 /* Parse a functional cast to TYPE. Returns an expression
27440 representing the cast. */
27443 cp_parser_functional_cast (cp_parser* parser, tree type)
27445 vec<tree, va_gc> *vec;
27446 tree expression_list;
27450 location_t start_loc = input_location;
27453 type = error_mark_node;
27455 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27457 cp_lexer_set_source_position (parser->lexer);
27458 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27459 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27460 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27461 if (TREE_CODE (type) == TYPE_DECL)
27462 type = TREE_TYPE (type);
27464 cast = finish_compound_literal (type, expression_list,
27465 tf_warning_or_error, fcl_functional);
27466 /* Create a location of the form:
27469 with caret == start at the start of the type name,
27470 finishing at the closing brace. */
27471 location_t finish_loc
27472 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27473 location_t combined_loc = make_location (start_loc, start_loc,
27475 cast.set_location (combined_loc);
27480 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27482 /*allow_expansion_p=*/true,
27483 /*non_constant_p=*/NULL);
27485 expression_list = error_mark_node;
27488 expression_list = build_tree_list_vec (vec);
27489 release_tree_vector (vec);
27492 cast = build_functional_cast (type, expression_list,
27493 tf_warning_or_error);
27494 /* [expr.const]/1: In an integral constant expression "only type
27495 conversions to integral or enumeration type can be used". */
27496 if (TREE_CODE (type) == TYPE_DECL)
27497 type = TREE_TYPE (type);
27498 if (cast != error_mark_node
27499 && !cast_valid_in_integral_constant_expression_p (type)
27500 && cp_parser_non_integral_constant_expression (parser,
27502 return error_mark_node;
27504 /* Create a location of the form:
27507 with caret == start at the start of the type name,
27508 finishing at the closing paren. */
27509 location_t finish_loc
27510 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27511 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27512 cast.set_location (combined_loc);
27516 /* Save the tokens that make up the body of a member function defined
27517 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27518 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27519 specifiers applied to the declaration. Returns the FUNCTION_DECL
27520 for the member function. */
27523 cp_parser_save_member_function_body (cp_parser* parser,
27524 cp_decl_specifier_seq *decl_specifiers,
27525 cp_declarator *declarator,
27531 bool function_try_block = false;
27533 /* Create the FUNCTION_DECL. */
27534 fn = grokmethod (decl_specifiers, declarator, attributes);
27535 cp_finalize_omp_declare_simd (parser, fn);
27536 cp_finalize_oacc_routine (parser, fn, true);
27537 /* If something went badly wrong, bail out now. */
27538 if (fn == error_mark_node)
27540 /* If there's a function-body, skip it. */
27541 if (cp_parser_token_starts_function_definition_p
27542 (cp_lexer_peek_token (parser->lexer)))
27543 cp_parser_skip_to_end_of_block_or_statement (parser);
27544 return error_mark_node;
27547 /* Remember it, if there default args to post process. */
27548 cp_parser_save_default_args (parser, fn);
27550 /* Save away the tokens that make up the body of the
27552 first = parser->lexer->next_token;
27554 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27555 cp_lexer_consume_token (parser->lexer);
27556 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27557 RID_TRANSACTION_ATOMIC))
27559 cp_lexer_consume_token (parser->lexer);
27560 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27561 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27562 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27563 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27564 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27565 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27566 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27568 cp_lexer_consume_token (parser->lexer);
27569 cp_lexer_consume_token (parser->lexer);
27570 cp_lexer_consume_token (parser->lexer);
27571 cp_lexer_consume_token (parser->lexer);
27572 cp_lexer_consume_token (parser->lexer);
27575 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27576 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27578 cp_lexer_consume_token (parser->lexer);
27579 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27584 /* Handle function try blocks. */
27585 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27587 cp_lexer_consume_token (parser->lexer);
27588 function_try_block = true;
27590 /* We can have braced-init-list mem-initializers before the fn body. */
27591 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27593 cp_lexer_consume_token (parser->lexer);
27594 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27596 /* cache_group will stop after an un-nested { } pair, too. */
27597 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27600 /* variadic mem-inits have ... after the ')'. */
27601 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27602 cp_lexer_consume_token (parser->lexer);
27605 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27606 /* Handle function try blocks. */
27607 if (function_try_block)
27608 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27609 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27610 last = parser->lexer->next_token;
27612 /* Save away the inline definition; we will process it when the
27613 class is complete. */
27614 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27615 DECL_PENDING_INLINE_P (fn) = 1;
27617 /* We need to know that this was defined in the class, so that
27618 friend templates are handled correctly. */
27619 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27621 /* Add FN to the queue of functions to be parsed later. */
27622 vec_safe_push (unparsed_funs_with_definitions, fn);
27627 /* Save the tokens that make up the in-class initializer for a non-static
27628 data member. Returns a DEFAULT_ARG. */
27631 cp_parser_save_nsdmi (cp_parser* parser)
27633 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27636 /* Parse a template-argument-list, as well as the trailing ">" (but
27637 not the opening "<"). See cp_parser_template_argument_list for the
27641 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27645 tree saved_qualifying_scope;
27646 tree saved_object_scope;
27647 bool saved_greater_than_is_operator_p;
27648 int saved_unevaluated_operand;
27649 int saved_inhibit_evaluation_warnings;
27653 When parsing a template-id, the first non-nested `>' is taken as
27654 the end of the template-argument-list rather than a greater-than
27656 saved_greater_than_is_operator_p
27657 = parser->greater_than_is_operator_p;
27658 parser->greater_than_is_operator_p = false;
27659 /* Parsing the argument list may modify SCOPE, so we save it
27661 saved_scope = parser->scope;
27662 saved_qualifying_scope = parser->qualifying_scope;
27663 saved_object_scope = parser->object_scope;
27664 /* We need to evaluate the template arguments, even though this
27665 template-id may be nested within a "sizeof". */
27666 saved_unevaluated_operand = cp_unevaluated_operand;
27667 cp_unevaluated_operand = 0;
27668 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27669 c_inhibit_evaluation_warnings = 0;
27670 /* Parse the template-argument-list itself. */
27671 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27672 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27673 arguments = NULL_TREE;
27675 arguments = cp_parser_template_argument_list (parser);
27676 /* Look for the `>' that ends the template-argument-list. If we find
27677 a '>>' instead, it's probably just a typo. */
27678 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27680 if (cxx_dialect != cxx98)
27682 /* In C++0x, a `>>' in a template argument list or cast
27683 expression is considered to be two separate `>'
27684 tokens. So, change the current token to a `>', but don't
27685 consume it: it will be consumed later when the outer
27686 template argument list (or cast expression) is parsed.
27687 Note that this replacement of `>' for `>>' is necessary
27688 even if we are parsing tentatively: in the tentative
27689 case, after calling
27690 cp_parser_enclosed_template_argument_list we will always
27691 throw away all of the template arguments and the first
27692 closing `>', either because the template argument list
27693 was erroneous or because we are replacing those tokens
27694 with a CPP_TEMPLATE_ID token. The second `>' (which will
27695 not have been thrown away) is needed either to close an
27696 outer template argument list or to complete a new-style
27698 cp_token *token = cp_lexer_peek_token (parser->lexer);
27699 token->type = CPP_GREATER;
27701 else if (!saved_greater_than_is_operator_p)
27703 /* If we're in a nested template argument list, the '>>' has
27704 to be a typo for '> >'. We emit the error message, but we
27705 continue parsing and we push a '>' as next token, so that
27706 the argument list will be parsed correctly. Note that the
27707 global source location is still on the token before the
27708 '>>', so we need to say explicitly where we want it. */
27709 cp_token *token = cp_lexer_peek_token (parser->lexer);
27710 gcc_rich_location richloc (token->location);
27711 richloc.add_fixit_replace ("> >");
27712 error_at (&richloc, "%<>>%> should be %<> >%> "
27713 "within a nested template argument list");
27715 token->type = CPP_GREATER;
27719 /* If this is not a nested template argument list, the '>>'
27720 is a typo for '>'. Emit an error message and continue.
27721 Same deal about the token location, but here we can get it
27722 right by consuming the '>>' before issuing the diagnostic. */
27723 cp_token *token = cp_lexer_consume_token (parser->lexer);
27724 error_at (token->location,
27725 "spurious %<>>%>, use %<>%> to terminate "
27726 "a template argument list");
27730 cp_parser_skip_to_end_of_template_parameter_list (parser);
27731 /* The `>' token might be a greater-than operator again now. */
27732 parser->greater_than_is_operator_p
27733 = saved_greater_than_is_operator_p;
27734 /* Restore the SAVED_SCOPE. */
27735 parser->scope = saved_scope;
27736 parser->qualifying_scope = saved_qualifying_scope;
27737 parser->object_scope = saved_object_scope;
27738 cp_unevaluated_operand = saved_unevaluated_operand;
27739 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27744 /* MEMBER_FUNCTION is a member function, or a friend. If default
27745 arguments, or the body of the function have not yet been parsed,
27749 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27751 timevar_push (TV_PARSE_INMETH);
27752 /* If this member is a template, get the underlying
27754 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27755 member_function = DECL_TEMPLATE_RESULT (member_function);
27757 /* There should not be any class definitions in progress at this
27758 point; the bodies of members are only parsed outside of all class
27760 gcc_assert (parser->num_classes_being_defined == 0);
27761 /* While we're parsing the member functions we might encounter more
27762 classes. We want to handle them right away, but we don't want
27763 them getting mixed up with functions that are currently in the
27765 push_unparsed_function_queues (parser);
27767 /* Make sure that any template parameters are in scope. */
27768 maybe_begin_member_template_processing (member_function);
27770 /* If the body of the function has not yet been parsed, parse it
27772 if (DECL_PENDING_INLINE_P (member_function))
27774 tree function_scope;
27775 cp_token_cache *tokens;
27777 /* The function is no longer pending; we are processing it. */
27778 tokens = DECL_PENDING_INLINE_INFO (member_function);
27779 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27780 DECL_PENDING_INLINE_P (member_function) = 0;
27782 /* If this is a local class, enter the scope of the containing
27784 function_scope = current_function_decl;
27785 if (function_scope)
27786 push_function_context ();
27788 /* Push the body of the function onto the lexer stack. */
27789 cp_parser_push_lexer_for_tokens (parser, tokens);
27791 /* Let the front end know that we going to be defining this
27793 start_preparsed_function (member_function, NULL_TREE,
27794 SF_PRE_PARSED | SF_INCLASS_INLINE);
27796 /* Don't do access checking if it is a templated function. */
27797 if (processing_template_decl)
27798 push_deferring_access_checks (dk_no_check);
27800 /* #pragma omp declare reduction needs special parsing. */
27801 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27803 parser->lexer->in_pragma = true;
27804 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27805 finish_function (/*inline_p=*/true);
27806 cp_check_omp_declare_reduction (member_function);
27809 /* Now, parse the body of the function. */
27810 cp_parser_function_definition_after_declarator (parser,
27811 /*inline_p=*/true);
27813 if (processing_template_decl)
27814 pop_deferring_access_checks ();
27816 /* Leave the scope of the containing function. */
27817 if (function_scope)
27818 pop_function_context ();
27819 cp_parser_pop_lexer (parser);
27822 /* Remove any template parameters from the symbol table. */
27823 maybe_end_member_template_processing ();
27825 /* Restore the queue. */
27826 pop_unparsed_function_queues (parser);
27827 timevar_pop (TV_PARSE_INMETH);
27830 /* If DECL contains any default args, remember it on the unparsed
27831 functions queue. */
27834 cp_parser_save_default_args (cp_parser* parser, tree decl)
27838 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27840 probe = TREE_CHAIN (probe))
27841 if (TREE_PURPOSE (probe))
27843 cp_default_arg_entry entry = {current_class_type, decl};
27844 vec_safe_push (unparsed_funs_with_default_args, entry);
27849 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27850 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27851 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27852 from the parameter-type-list. */
27855 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27856 tree default_arg, tree parmtype)
27858 cp_token_cache *tokens;
27862 if (default_arg == error_mark_node)
27863 return error_mark_node;
27865 /* Push the saved tokens for the default argument onto the parser's
27867 tokens = DEFARG_TOKENS (default_arg);
27868 cp_parser_push_lexer_for_tokens (parser, tokens);
27870 start_lambda_scope (decl);
27872 /* Parse the default argument. */
27873 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27874 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27875 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27877 finish_lambda_scope ();
27879 if (parsed_arg == error_mark_node)
27880 cp_parser_skip_to_end_of_statement (parser);
27882 if (!processing_template_decl)
27884 /* In a non-template class, check conversions now. In a template,
27885 we'll wait and instantiate these as needed. */
27886 if (TREE_CODE (decl) == PARM_DECL)
27887 parsed_arg = check_default_argument (parmtype, parsed_arg,
27888 tf_warning_or_error);
27889 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27890 parsed_arg = error_mark_node;
27892 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27895 /* If the token stream has not been completely used up, then
27896 there was extra junk after the end of the default
27898 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27900 if (TREE_CODE (decl) == PARM_DECL)
27901 cp_parser_error (parser, "expected %<,%>");
27903 cp_parser_error (parser, "expected %<;%>");
27906 /* Revert to the main lexer. */
27907 cp_parser_pop_lexer (parser);
27912 /* FIELD is a non-static data member with an initializer which we saved for
27913 later; parse it now. */
27916 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27920 maybe_begin_member_template_processing (field);
27922 push_unparsed_function_queues (parser);
27923 def = cp_parser_late_parse_one_default_arg (parser, field,
27924 DECL_INITIAL (field),
27926 pop_unparsed_function_queues (parser);
27928 maybe_end_member_template_processing ();
27930 DECL_INITIAL (field) = def;
27933 /* FN is a FUNCTION_DECL which may contains a parameter with an
27934 unparsed DEFAULT_ARG. Parse the default args now. This function
27935 assumes that the current scope is the scope in which the default
27936 argument should be processed. */
27939 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27941 bool saved_local_variables_forbidden_p;
27942 tree parm, parmdecl;
27944 /* While we're parsing the default args, we might (due to the
27945 statement expression extension) encounter more classes. We want
27946 to handle them right away, but we don't want them getting mixed
27947 up with default args that are currently in the queue. */
27948 push_unparsed_function_queues (parser);
27950 /* Local variable names (and the `this' keyword) may not appear
27951 in a default argument. */
27952 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27953 parser->local_variables_forbidden_p = true;
27955 push_defarg_context (fn);
27957 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27958 parmdecl = DECL_ARGUMENTS (fn);
27959 parm && parm != void_list_node;
27960 parm = TREE_CHAIN (parm),
27961 parmdecl = DECL_CHAIN (parmdecl))
27963 tree default_arg = TREE_PURPOSE (parm);
27965 vec<tree, va_gc> *insts;
27972 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27973 /* This can happen for a friend declaration for a function
27974 already declared with default arguments. */
27978 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27980 TREE_VALUE (parm));
27981 TREE_PURPOSE (parm) = parsed_arg;
27983 /* Update any instantiations we've already created. */
27984 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27985 vec_safe_iterate (insts, ix, ©); ix++)
27986 TREE_PURPOSE (copy) = parsed_arg;
27989 pop_defarg_context ();
27991 /* Make sure no default arg is missing. */
27992 check_default_args (fn);
27994 /* Restore the state of local_variables_forbidden_p. */
27995 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27997 /* Restore the queue. */
27998 pop_unparsed_function_queues (parser);
28001 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28003 sizeof ... ( identifier )
28005 where the 'sizeof' token has already been consumed. */
28008 cp_parser_sizeof_pack (cp_parser *parser)
28010 /* Consume the `...'. */
28011 cp_lexer_consume_token (parser->lexer);
28012 maybe_warn_variadic_templates ();
28014 matching_parens parens;
28015 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28017 parens.consume_open (parser);
28019 permerror (cp_lexer_peek_token (parser->lexer)->location,
28020 "%<sizeof...%> argument must be surrounded by parentheses");
28022 cp_token *token = cp_lexer_peek_token (parser->lexer);
28023 tree name = cp_parser_identifier (parser);
28024 if (name == error_mark_node)
28025 return error_mark_node;
28026 /* The name is not qualified. */
28027 parser->scope = NULL_TREE;
28028 parser->qualifying_scope = NULL_TREE;
28029 parser->object_scope = NULL_TREE;
28030 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28031 if (expr == error_mark_node)
28032 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28034 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28035 expr = TREE_TYPE (expr);
28036 else if (TREE_CODE (expr) == CONST_DECL)
28037 expr = DECL_INITIAL (expr);
28038 expr = make_pack_expansion (expr);
28039 PACK_EXPANSION_SIZEOF_P (expr) = true;
28042 parens.require_close (parser);
28047 /* Parse the operand of `sizeof' (or a similar operator). Returns
28048 either a TYPE or an expression, depending on the form of the
28049 input. The KEYWORD indicates which kind of expression we have
28053 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28055 tree expr = NULL_TREE;
28056 const char *saved_message;
28058 bool saved_integral_constant_expression_p;
28059 bool saved_non_integral_constant_expression_p;
28061 /* If it's a `...', then we are computing the length of a parameter
28063 if (keyword == RID_SIZEOF
28064 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28065 return cp_parser_sizeof_pack (parser);
28067 /* Types cannot be defined in a `sizeof' expression. Save away the
28069 saved_message = parser->type_definition_forbidden_message;
28070 /* And create the new one. */
28071 tmp = concat ("types may not be defined in %<",
28072 IDENTIFIER_POINTER (ridpointers[keyword]),
28073 "%> expressions", NULL);
28074 parser->type_definition_forbidden_message = tmp;
28076 /* The restrictions on constant-expressions do not apply inside
28077 sizeof expressions. */
28078 saved_integral_constant_expression_p
28079 = parser->integral_constant_expression_p;
28080 saved_non_integral_constant_expression_p
28081 = parser->non_integral_constant_expression_p;
28082 parser->integral_constant_expression_p = false;
28084 /* Do not actually evaluate the expression. */
28085 ++cp_unevaluated_operand;
28086 ++c_inhibit_evaluation_warnings;
28087 /* If it's a `(', then we might be looking at the type-id
28089 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28091 tree type = NULL_TREE;
28093 /* We can't be sure yet whether we're looking at a type-id or an
28095 cp_parser_parse_tentatively (parser);
28097 matching_parens parens;
28098 parens.consume_open (parser);
28100 /* Note: as a GNU Extension, compound literals are considered
28101 postfix-expressions as they are in C99, so they are valid
28102 arguments to sizeof. See comment in cp_parser_cast_expression
28104 if (cp_parser_compound_literal_p (parser))
28105 cp_parser_simulate_error (parser);
28108 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28109 parser->in_type_id_in_expr_p = true;
28110 /* Look for the type-id. */
28111 type = cp_parser_type_id (parser);
28112 /* Look for the closing `)'. */
28113 parens.require_close (parser);
28114 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28117 /* If all went well, then we're done. */
28118 if (cp_parser_parse_definitely (parser))
28120 cp_decl_specifier_seq decl_specs;
28122 /* Build a trivial decl-specifier-seq. */
28123 clear_decl_specs (&decl_specs);
28124 decl_specs.type = type;
28126 /* Call grokdeclarator to figure out what type this is. */
28127 expr = grokdeclarator (NULL,
28131 /*attrlist=*/NULL);
28135 /* If the type-id production did not work out, then we must be
28136 looking at the unary-expression production. */
28138 expr = cp_parser_unary_expression (parser);
28140 /* Go back to evaluating expressions. */
28141 --cp_unevaluated_operand;
28142 --c_inhibit_evaluation_warnings;
28144 /* Free the message we created. */
28146 /* And restore the old one. */
28147 parser->type_definition_forbidden_message = saved_message;
28148 parser->integral_constant_expression_p
28149 = saved_integral_constant_expression_p;
28150 parser->non_integral_constant_expression_p
28151 = saved_non_integral_constant_expression_p;
28156 /* If the current declaration has no declarator, return true. */
28159 cp_parser_declares_only_class_p (cp_parser *parser)
28161 /* If the next token is a `;' or a `,' then there is no
28163 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28164 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28167 /* Update the DECL_SPECS to reflect the storage class indicated by
28171 cp_parser_set_storage_class (cp_parser *parser,
28172 cp_decl_specifier_seq *decl_specs,
28176 cp_storage_class storage_class;
28178 if (parser->in_unbraced_linkage_specification_p)
28180 error_at (token->location, "invalid use of %qD in linkage specification",
28181 ridpointers[keyword]);
28184 else if (decl_specs->storage_class != sc_none)
28186 decl_specs->conflicting_specifiers_p = true;
28190 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28191 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28192 && decl_specs->gnu_thread_keyword_p)
28194 pedwarn (decl_specs->locations[ds_thread], 0,
28195 "%<__thread%> before %qD", ridpointers[keyword]);
28201 storage_class = sc_auto;
28204 storage_class = sc_register;
28207 storage_class = sc_static;
28210 storage_class = sc_extern;
28213 storage_class = sc_mutable;
28216 gcc_unreachable ();
28218 decl_specs->storage_class = storage_class;
28219 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28221 /* A storage class specifier cannot be applied alongside a typedef
28222 specifier. If there is a typedef specifier present then set
28223 conflicting_specifiers_p which will trigger an error later
28224 on in grokdeclarator. */
28225 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28226 decl_specs->conflicting_specifiers_p = true;
28229 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28230 is true, the type is a class or enum definition. */
28233 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28236 bool type_definition_p)
28238 decl_specs->any_specifiers_p = true;
28240 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28241 (with, for example, in "typedef int wchar_t;") we remember that
28242 this is what happened. In system headers, we ignore these
28243 declarations so that G++ can work with system headers that are not
28245 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28246 && !type_definition_p
28247 && (type_spec == boolean_type_node
28248 || type_spec == char16_type_node
28249 || type_spec == char32_type_node
28250 || type_spec == wchar_type_node)
28251 && (decl_specs->type
28252 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28253 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28254 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28255 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28257 decl_specs->redefined_builtin_type = type_spec;
28258 set_and_check_decl_spec_loc (decl_specs,
28259 ds_redefined_builtin_type_spec,
28261 if (!decl_specs->type)
28263 decl_specs->type = type_spec;
28264 decl_specs->type_definition_p = false;
28265 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28268 else if (decl_specs->type)
28269 decl_specs->multiple_types_p = true;
28272 decl_specs->type = type_spec;
28273 decl_specs->type_definition_p = type_definition_p;
28274 decl_specs->redefined_builtin_type = NULL_TREE;
28275 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28279 /* True iff TOKEN is the GNU keyword __thread. */
28282 token_is__thread (cp_token *token)
28284 gcc_assert (token->keyword == RID_THREAD);
28285 return id_equal (token->u.value, "__thread");
28288 /* Set the location for a declarator specifier and check if it is
28291 DECL_SPECS is the sequence of declarator specifiers onto which to
28294 DS is the single declarator specifier to set which location is to
28295 be set onto the existing sequence of declarators.
28297 LOCATION is the location for the declarator specifier to
28301 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28302 cp_decl_spec ds, cp_token *token)
28304 gcc_assert (ds < ds_last);
28306 if (decl_specs == NULL)
28309 source_location location = token->location;
28311 if (decl_specs->locations[ds] == 0)
28313 decl_specs->locations[ds] = location;
28314 if (ds == ds_thread)
28315 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28321 if (decl_specs->locations[ds_long_long] != 0)
28322 error_at (location,
28323 "%<long long long%> is too long for GCC");
28326 decl_specs->locations[ds_long_long] = location;
28327 pedwarn_cxx98 (location,
28329 "ISO C++ 1998 does not support %<long long%>");
28332 else if (ds == ds_thread)
28334 bool gnu = token_is__thread (token);
28335 if (gnu != decl_specs->gnu_thread_keyword_p)
28336 error_at (location,
28337 "both %<__thread%> and %<thread_local%> specified");
28340 gcc_rich_location richloc (location);
28341 richloc.add_fixit_remove ();
28342 error_at (&richloc, "duplicate %qD", token->u.value);
28347 static const char *const decl_spec_names[] = {
28364 gcc_rich_location richloc (location);
28365 richloc.add_fixit_remove ();
28366 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28371 /* Return true iff the declarator specifier DS is present in the
28372 sequence of declarator specifiers DECL_SPECS. */
28375 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28378 gcc_assert (ds < ds_last);
28380 if (decl_specs == NULL)
28383 return decl_specs->locations[ds] != 0;
28386 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28387 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28390 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28392 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28395 /* Issue an error message indicating that TOKEN_DESC was expected.
28396 If KEYWORD is true, it indicated this function is called by
28397 cp_parser_require_keword and the required token can only be
28398 a indicated keyword.
28400 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28401 within any error as the location of an "opening" token matching
28402 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28403 RT_CLOSE_PAREN). */
28406 cp_parser_required_error (cp_parser *parser,
28407 required_token token_desc,
28409 location_t matching_location)
28411 if (cp_parser_simulate_error (parser))
28414 const char *gmsgid = NULL;
28415 switch (token_desc)
28418 gmsgid = G_("expected %<new%>");
28421 gmsgid = G_("expected %<delete%>");
28424 gmsgid = G_("expected %<return%>");
28427 gmsgid = G_("expected %<while%>");
28430 gmsgid = G_("expected %<extern%>");
28432 case RT_STATIC_ASSERT:
28433 gmsgid = G_("expected %<static_assert%>");
28436 gmsgid = G_("expected %<decltype%>");
28439 gmsgid = G_("expected %<operator%>");
28442 gmsgid = G_("expected %<class%>");
28445 gmsgid = G_("expected %<template%>");
28448 gmsgid = G_("expected %<namespace%>");
28451 gmsgid = G_("expected %<using%>");
28454 gmsgid = G_("expected %<asm%>");
28457 gmsgid = G_("expected %<try%>");
28460 gmsgid = G_("expected %<catch%>");
28463 gmsgid = G_("expected %<throw%>");
28466 gmsgid = G_("expected %<__label__%>");
28469 gmsgid = G_("expected %<@try%>");
28471 case RT_AT_SYNCHRONIZED:
28472 gmsgid = G_("expected %<@synchronized%>");
28475 gmsgid = G_("expected %<@throw%>");
28477 case RT_TRANSACTION_ATOMIC:
28478 gmsgid = G_("expected %<__transaction_atomic%>");
28480 case RT_TRANSACTION_RELAXED:
28481 gmsgid = G_("expected %<__transaction_relaxed%>");
28487 if (!gmsgid && !keyword)
28489 switch (token_desc)
28492 gmsgid = G_("expected %<;%>");
28494 case RT_OPEN_PAREN:
28495 gmsgid = G_("expected %<(%>");
28497 case RT_CLOSE_BRACE:
28498 gmsgid = G_("expected %<}%>");
28500 case RT_OPEN_BRACE:
28501 gmsgid = G_("expected %<{%>");
28503 case RT_CLOSE_SQUARE:
28504 gmsgid = G_("expected %<]%>");
28506 case RT_OPEN_SQUARE:
28507 gmsgid = G_("expected %<[%>");
28510 gmsgid = G_("expected %<,%>");
28513 gmsgid = G_("expected %<::%>");
28516 gmsgid = G_("expected %<<%>");
28519 gmsgid = G_("expected %<>%>");
28522 gmsgid = G_("expected %<=%>");
28525 gmsgid = G_("expected %<...%>");
28528 gmsgid = G_("expected %<*%>");
28531 gmsgid = G_("expected %<~%>");
28534 gmsgid = G_("expected %<:%>");
28536 case RT_COLON_SCOPE:
28537 gmsgid = G_("expected %<:%> or %<::%>");
28539 case RT_CLOSE_PAREN:
28540 gmsgid = G_("expected %<)%>");
28542 case RT_COMMA_CLOSE_PAREN:
28543 gmsgid = G_("expected %<,%> or %<)%>");
28545 case RT_PRAGMA_EOL:
28546 gmsgid = G_("expected end of line");
28549 gmsgid = G_("expected identifier");
28552 gmsgid = G_("expected selection-statement");
28555 gmsgid = G_("expected iteration-statement");
28558 gmsgid = G_("expected jump-statement");
28561 gmsgid = G_("expected class-key");
28563 case RT_CLASS_TYPENAME_TEMPLATE:
28564 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28567 gcc_unreachable ();
28572 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28576 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28577 issue an error message indicating that TOKEN_DESC was expected.
28579 Returns the token consumed, if the token had the appropriate type.
28580 Otherwise, returns NULL.
28582 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28583 within any error as the location of an "opening" token matching
28584 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28585 RT_CLOSE_PAREN). */
28588 cp_parser_require (cp_parser* parser,
28589 enum cpp_ttype type,
28590 required_token token_desc,
28591 location_t matching_location)
28593 if (cp_lexer_next_token_is (parser->lexer, type))
28594 return cp_lexer_consume_token (parser->lexer);
28597 /* Output the MESSAGE -- unless we're parsing tentatively. */
28598 if (!cp_parser_simulate_error (parser))
28599 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28600 matching_location);
28605 /* An error message is produced if the next token is not '>'.
28606 All further tokens are skipped until the desired token is
28607 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28610 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28612 /* Current level of '< ... >'. */
28613 unsigned level = 0;
28614 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28615 unsigned nesting_depth = 0;
28617 /* Are we ready, yet? If not, issue error message. */
28618 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28621 /* Skip tokens until the desired token is found. */
28624 /* Peek at the next token. */
28625 switch (cp_lexer_peek_token (parser->lexer)->type)
28628 if (!nesting_depth)
28633 if (cxx_dialect == cxx98)
28634 /* C++0x views the `>>' operator as two `>' tokens, but
28637 else if (!nesting_depth && level-- == 0)
28639 /* We've hit a `>>' where the first `>' closes the
28640 template argument list, and the second `>' is
28641 spurious. Just consume the `>>' and stop; we've
28642 already produced at least one error. */
28643 cp_lexer_consume_token (parser->lexer);
28646 /* Fall through for C++0x, so we handle the second `>' in
28648 gcc_fallthrough ();
28651 if (!nesting_depth && level-- == 0)
28653 /* We've reached the token we want, consume it and stop. */
28654 cp_lexer_consume_token (parser->lexer);
28659 case CPP_OPEN_PAREN:
28660 case CPP_OPEN_SQUARE:
28664 case CPP_CLOSE_PAREN:
28665 case CPP_CLOSE_SQUARE:
28666 if (nesting_depth-- == 0)
28671 case CPP_PRAGMA_EOL:
28672 case CPP_SEMICOLON:
28673 case CPP_OPEN_BRACE:
28674 case CPP_CLOSE_BRACE:
28675 /* The '>' was probably forgotten, don't look further. */
28682 /* Consume this token. */
28683 cp_lexer_consume_token (parser->lexer);
28687 /* If the next token is the indicated keyword, consume it. Otherwise,
28688 issue an error message indicating that TOKEN_DESC was expected.
28690 Returns the token consumed, if the token had the appropriate type.
28691 Otherwise, returns NULL. */
28694 cp_parser_require_keyword (cp_parser* parser,
28696 required_token token_desc)
28698 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28700 if (token && token->keyword != keyword)
28702 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28710 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28711 function-definition. */
28714 cp_parser_token_starts_function_definition_p (cp_token* token)
28716 return (/* An ordinary function-body begins with an `{'. */
28717 token->type == CPP_OPEN_BRACE
28718 /* A ctor-initializer begins with a `:'. */
28719 || token->type == CPP_COLON
28720 /* A function-try-block begins with `try'. */
28721 || token->keyword == RID_TRY
28722 /* A function-transaction-block begins with `__transaction_atomic'
28723 or `__transaction_relaxed'. */
28724 || token->keyword == RID_TRANSACTION_ATOMIC
28725 || token->keyword == RID_TRANSACTION_RELAXED
28726 /* The named return value extension begins with `return'. */
28727 || token->keyword == RID_RETURN);
28730 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28734 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28738 token = cp_lexer_peek_token (parser->lexer);
28739 return (token->type == CPP_OPEN_BRACE
28740 || (token->type == CPP_COLON
28741 && !parser->colon_doesnt_start_class_def_p));
28744 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28745 C++0x) ending a template-argument. */
28748 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28752 token = cp_lexer_peek_token (parser->lexer);
28753 return (token->type == CPP_COMMA
28754 || token->type == CPP_GREATER
28755 || token->type == CPP_ELLIPSIS
28756 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28759 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28760 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28763 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28768 token = cp_lexer_peek_nth_token (parser->lexer, n);
28769 if (token->type == CPP_LESS)
28771 /* Check for the sequence `<::' in the original code. It would be lexed as
28772 `[:', where `[' is a digraph, and there is no whitespace before
28774 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28777 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28778 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28784 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28785 or none_type otherwise. */
28787 static enum tag_types
28788 cp_parser_token_is_class_key (cp_token* token)
28790 switch (token->keyword)
28795 return record_type;
28804 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28805 or none_type otherwise or if the token is null. */
28807 static enum tag_types
28808 cp_parser_token_is_type_parameter_key (cp_token* token)
28813 switch (token->keyword)
28818 return typename_type;
28825 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28828 cp_parser_check_class_key (enum tag_types class_key, tree type)
28830 if (type == error_mark_node)
28832 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28834 if (permerror (input_location, "%qs tag used in naming %q#T",
28835 class_key == union_type ? "union"
28836 : class_key == record_type ? "struct" : "class",
28838 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28839 "%q#T was previously declared here", type);
28843 /* Issue an error message if DECL is redeclared with different
28844 access than its original declaration [class.access.spec/3].
28845 This applies to nested classes, nested class templates and
28846 enumerations [class.mem/1]. */
28849 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28852 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28853 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28856 if ((TREE_PRIVATE (decl)
28857 != (current_access_specifier == access_private_node))
28858 || (TREE_PROTECTED (decl)
28859 != (current_access_specifier == access_protected_node)))
28860 error_at (location, "%qD redeclared with different access", decl);
28863 /* Look for the `template' keyword, as a syntactic disambiguator.
28864 Return TRUE iff it is present, in which case it will be
28868 cp_parser_optional_template_keyword (cp_parser *parser)
28870 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28872 /* In C++98 the `template' keyword can only be used within templates;
28873 outside templates the parser can always figure out what is a
28874 template and what is not. In C++11, per the resolution of DR 468,
28875 `template' is allowed in cases where it is not strictly necessary. */
28876 if (!processing_template_decl
28877 && pedantic && cxx_dialect == cxx98)
28879 cp_token *token = cp_lexer_peek_token (parser->lexer);
28880 pedwarn (token->location, OPT_Wpedantic,
28881 "in C++98 %<template%> (as a disambiguator) is only "
28882 "allowed within templates");
28883 /* If this part of the token stream is rescanned, the same
28884 error message would be generated. So, we purge the token
28885 from the stream. */
28886 cp_lexer_purge_token (parser->lexer);
28891 /* Consume the `template' keyword. */
28892 cp_lexer_consume_token (parser->lexer);
28899 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28900 set PARSER->SCOPE, and perform other related actions. */
28903 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28905 struct tree_check *check_value;
28907 /* Get the stored value. */
28908 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28909 /* Set the scope from the stored value. */
28910 parser->scope = saved_checks_value (check_value);
28911 parser->qualifying_scope = check_value->qualifying_scope;
28912 parser->object_scope = NULL_TREE;
28915 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28916 encounter the end of a block before what we were looking for. */
28919 cp_parser_cache_group (cp_parser *parser,
28920 enum cpp_ttype end,
28925 cp_token *token = cp_lexer_peek_token (parser->lexer);
28927 /* Abort a parenthesized expression if we encounter a semicolon. */
28928 if ((end == CPP_CLOSE_PAREN || depth == 0)
28929 && token->type == CPP_SEMICOLON)
28931 /* If we've reached the end of the file, stop. */
28932 if (token->type == CPP_EOF
28933 || (end != CPP_PRAGMA_EOL
28934 && token->type == CPP_PRAGMA_EOL))
28936 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28937 /* We've hit the end of an enclosing block, so there's been some
28938 kind of syntax error. */
28941 /* Consume the token. */
28942 cp_lexer_consume_token (parser->lexer);
28943 /* See if it starts a new group. */
28944 if (token->type == CPP_OPEN_BRACE)
28946 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28947 /* In theory this should probably check end == '}', but
28948 cp_parser_save_member_function_body needs it to exit
28949 after either '}' or ')' when called with ')'. */
28953 else if (token->type == CPP_OPEN_PAREN)
28955 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28956 if (depth == 0 && end == CPP_CLOSE_PAREN)
28959 else if (token->type == CPP_PRAGMA)
28960 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28961 else if (token->type == end)
28966 /* Like above, for caching a default argument or NSDMI. Both of these are
28967 terminated by a non-nested comma, but it can be unclear whether or not a
28968 comma is nested in a template argument list unless we do more parsing.
28969 In order to handle this ambiguity, when we encounter a ',' after a '<'
28970 we try to parse what follows as a parameter-declaration-list (in the
28971 case of a default argument) or a member-declarator (in the case of an
28972 NSDMI). If that succeeds, then we stop caching. */
28975 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28977 unsigned depth = 0;
28978 int maybe_template_id = 0;
28979 cp_token *first_token;
28981 tree default_argument;
28983 /* Add tokens until we have processed the entire default
28984 argument. We add the range [first_token, token). */
28985 first_token = cp_lexer_peek_token (parser->lexer);
28986 if (first_token->type == CPP_OPEN_BRACE)
28988 /* For list-initialization, this is straightforward. */
28989 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28990 token = cp_lexer_peek_token (parser->lexer);
28996 /* Peek at the next token. */
28997 token = cp_lexer_peek_token (parser->lexer);
28998 /* What we do depends on what token we have. */
28999 switch (token->type)
29001 /* In valid code, a default argument must be
29002 immediately followed by a `,' `)', or `...'. */
29004 if (depth == 0 && maybe_template_id)
29006 /* If we've seen a '<', we might be in a
29007 template-argument-list. Until Core issue 325 is
29008 resolved, we don't know how this situation ought
29009 to be handled, so try to DTRT. We check whether
29010 what comes after the comma is a valid parameter
29011 declaration list. If it is, then the comma ends
29012 the default argument; otherwise the default
29013 argument continues. */
29014 bool error = false;
29017 /* Set ITALP so cp_parser_parameter_declaration_list
29018 doesn't decide to commit to this parse. */
29019 bool saved_italp = parser->in_template_argument_list_p;
29020 parser->in_template_argument_list_p = true;
29022 cp_parser_parse_tentatively (parser);
29026 /* Parse declarators until we reach a non-comma or
29027 somthing that cannot be an initializer.
29028 Just checking whether we're looking at a single
29029 declarator is insufficient. Consider:
29030 int var = tuple<T,U>::x;
29031 The template parameter 'U' looks exactly like a
29035 int ctor_dtor_or_conv_p;
29036 cp_lexer_consume_token (parser->lexer);
29037 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29038 &ctor_dtor_or_conv_p,
29039 /*parenthesized_p=*/NULL,
29041 /*friend_p=*/false);
29042 peek = cp_lexer_peek_token (parser->lexer);
29043 if (cp_parser_error_occurred (parser))
29046 while (peek->type == CPP_COMMA);
29047 /* If we met an '=' or ';' then the original comma
29048 was the end of the NSDMI. Otherwise assume
29049 we're still in the NSDMI. */
29050 error = (peek->type != CPP_EQ
29051 && peek->type != CPP_SEMICOLON);
29055 cp_lexer_consume_token (parser->lexer);
29056 begin_scope (sk_function_parms, NULL_TREE);
29057 cp_parser_parameter_declaration_list (parser, &error);
29058 pop_bindings_and_leave_scope ();
29060 if (!cp_parser_error_occurred (parser) && !error)
29062 cp_parser_abort_tentative_parse (parser);
29064 parser->in_template_argument_list_p = saved_italp;
29068 case CPP_CLOSE_PAREN:
29070 /* If we run into a non-nested `;', `}', or `]',
29071 then the code is invalid -- but the default
29072 argument is certainly over. */
29073 case CPP_SEMICOLON:
29074 case CPP_CLOSE_BRACE:
29075 case CPP_CLOSE_SQUARE:
29077 /* Handle correctly int n = sizeof ... ( p ); */
29078 && token->type != CPP_ELLIPSIS)
29080 /* Update DEPTH, if necessary. */
29081 else if (token->type == CPP_CLOSE_PAREN
29082 || token->type == CPP_CLOSE_BRACE
29083 || token->type == CPP_CLOSE_SQUARE)
29087 case CPP_OPEN_PAREN:
29088 case CPP_OPEN_SQUARE:
29089 case CPP_OPEN_BRACE:
29095 /* This might be the comparison operator, or it might
29096 start a template argument list. */
29097 ++maybe_template_id;
29101 if (cxx_dialect == cxx98)
29103 /* Fall through for C++0x, which treats the `>>'
29104 operator like two `>' tokens in certain
29106 gcc_fallthrough ();
29111 /* This might be an operator, or it might close a
29112 template argument list. But if a previous '<'
29113 started a template argument list, this will have
29114 closed it, so we can't be in one anymore. */
29115 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29116 if (maybe_template_id < 0)
29117 maybe_template_id = 0;
29121 /* If we run out of tokens, issue an error message. */
29123 case CPP_PRAGMA_EOL:
29124 error_at (token->location, "file ends in default argument");
29125 return error_mark_node;
29129 /* In these cases, we should look for template-ids.
29130 For example, if the default argument is
29131 `X<int, double>()', we need to do name lookup to
29132 figure out whether or not `X' is a template; if
29133 so, the `,' does not end the default argument.
29135 That is not yet done. */
29142 /* If we've reached the end, stop. */
29146 /* Add the token to the token block. */
29147 token = cp_lexer_consume_token (parser->lexer);
29150 /* Create a DEFAULT_ARG to represent the unparsed default
29152 default_argument = make_node (DEFAULT_ARG);
29153 DEFARG_TOKENS (default_argument)
29154 = cp_token_cache_new (first_token, token);
29155 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29157 return default_argument;
29160 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29163 defarg_location (tree default_argument)
29165 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29166 location_t start = tokens->first->location;
29167 location_t end = tokens->last->location;
29168 return make_location (start, start, end);
29171 /* Begin parsing tentatively. We always save tokens while parsing
29172 tentatively so that if the tentative parsing fails we can restore the
29176 cp_parser_parse_tentatively (cp_parser* parser)
29178 /* Enter a new parsing context. */
29179 parser->context = cp_parser_context_new (parser->context);
29180 /* Begin saving tokens. */
29181 cp_lexer_save_tokens (parser->lexer);
29182 /* In order to avoid repetitive access control error messages,
29183 access checks are queued up until we are no longer parsing
29185 push_deferring_access_checks (dk_deferred);
29188 /* Commit to the currently active tentative parse. */
29191 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29193 cp_parser_context *context;
29196 /* Mark all of the levels as committed. */
29197 lexer = parser->lexer;
29198 for (context = parser->context; context->next; context = context->next)
29200 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29202 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29203 while (!cp_lexer_saving_tokens (lexer))
29204 lexer = lexer->next;
29205 cp_lexer_commit_tokens (lexer);
29209 /* Commit to the topmost currently active tentative parse.
29211 Note that this function shouldn't be called when there are
29212 irreversible side-effects while in a tentative state. For
29213 example, we shouldn't create a permanent entry in the symbol
29214 table, or issue an error message that might not apply if the
29215 tentative parse is aborted. */
29218 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29220 cp_parser_context *context = parser->context;
29221 cp_lexer *lexer = parser->lexer;
29225 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29227 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29229 while (!cp_lexer_saving_tokens (lexer))
29230 lexer = lexer->next;
29231 cp_lexer_commit_tokens (lexer);
29235 /* Abort the currently active tentative parse. All consumed tokens
29236 will be rolled back, and no diagnostics will be issued. */
29239 cp_parser_abort_tentative_parse (cp_parser* parser)
29241 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29242 || errorcount > 0);
29243 cp_parser_simulate_error (parser);
29244 /* Now, pretend that we want to see if the construct was
29245 successfully parsed. */
29246 cp_parser_parse_definitely (parser);
29249 /* Stop parsing tentatively. If a parse error has occurred, restore the
29250 token stream. Otherwise, commit to the tokens we have consumed.
29251 Returns true if no error occurred; false otherwise. */
29254 cp_parser_parse_definitely (cp_parser* parser)
29256 bool error_occurred;
29257 cp_parser_context *context;
29259 /* Remember whether or not an error occurred, since we are about to
29260 destroy that information. */
29261 error_occurred = cp_parser_error_occurred (parser);
29262 /* Remove the topmost context from the stack. */
29263 context = parser->context;
29264 parser->context = context->next;
29265 /* If no parse errors occurred, commit to the tentative parse. */
29266 if (!error_occurred)
29268 /* Commit to the tokens read tentatively, unless that was
29270 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29271 cp_lexer_commit_tokens (parser->lexer);
29273 pop_to_parent_deferring_access_checks ();
29275 /* Otherwise, if errors occurred, roll back our state so that things
29276 are just as they were before we began the tentative parse. */
29279 cp_lexer_rollback_tokens (parser->lexer);
29280 pop_deferring_access_checks ();
29282 /* Add the context to the front of the free list. */
29283 context->next = cp_parser_context_free_list;
29284 cp_parser_context_free_list = context;
29286 return !error_occurred;
29289 /* Returns true if we are parsing tentatively and are not committed to
29290 this tentative parse. */
29293 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29295 return (cp_parser_parsing_tentatively (parser)
29296 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29299 /* Returns nonzero iff an error has occurred during the most recent
29300 tentative parse. */
29303 cp_parser_error_occurred (cp_parser* parser)
29305 return (cp_parser_parsing_tentatively (parser)
29306 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29309 /* Returns nonzero if GNU extensions are allowed. */
29312 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29314 return parser->allow_gnu_extensions_p;
29317 /* Objective-C++ Productions */
29320 /* Parse an Objective-C expression, which feeds into a primary-expression
29324 objc-message-expression
29325 objc-string-literal
29326 objc-encode-expression
29327 objc-protocol-expression
29328 objc-selector-expression
29330 Returns a tree representation of the expression. */
29333 cp_parser_objc_expression (cp_parser* parser)
29335 /* Try to figure out what kind of declaration is present. */
29336 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29340 case CPP_OPEN_SQUARE:
29341 return cp_parser_objc_message_expression (parser);
29343 case CPP_OBJC_STRING:
29344 kwd = cp_lexer_consume_token (parser->lexer);
29345 return objc_build_string_object (kwd->u.value);
29348 switch (kwd->keyword)
29350 case RID_AT_ENCODE:
29351 return cp_parser_objc_encode_expression (parser);
29353 case RID_AT_PROTOCOL:
29354 return cp_parser_objc_protocol_expression (parser);
29356 case RID_AT_SELECTOR:
29357 return cp_parser_objc_selector_expression (parser);
29364 error_at (kwd->location,
29365 "misplaced %<@%D%> Objective-C++ construct",
29367 cp_parser_skip_to_end_of_block_or_statement (parser);
29370 return error_mark_node;
29373 /* Parse an Objective-C message expression.
29375 objc-message-expression:
29376 [ objc-message-receiver objc-message-args ]
29378 Returns a representation of an Objective-C message. */
29381 cp_parser_objc_message_expression (cp_parser* parser)
29383 tree receiver, messageargs;
29385 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29386 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29387 receiver = cp_parser_objc_message_receiver (parser);
29388 messageargs = cp_parser_objc_message_args (parser);
29389 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29390 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29392 tree result = objc_build_message_expr (receiver, messageargs);
29394 /* Construct a location e.g.
29397 ranging from the '[' to the ']', with the caret at the start. */
29398 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29399 protected_set_expr_location (result, combined_loc);
29404 /* Parse an objc-message-receiver.
29406 objc-message-receiver:
29408 simple-type-specifier
29410 Returns a representation of the type or expression. */
29413 cp_parser_objc_message_receiver (cp_parser* parser)
29417 /* An Objective-C message receiver may be either (1) a type
29418 or (2) an expression. */
29419 cp_parser_parse_tentatively (parser);
29420 rcv = cp_parser_expression (parser);
29422 /* If that worked out, fine. */
29423 if (cp_parser_parse_definitely (parser))
29426 cp_parser_parse_tentatively (parser);
29427 rcv = cp_parser_simple_type_specifier (parser,
29428 /*decl_specs=*/NULL,
29429 CP_PARSER_FLAGS_NONE);
29431 if (cp_parser_parse_definitely (parser))
29432 return objc_get_class_reference (rcv);
29434 cp_parser_error (parser, "objective-c++ message receiver expected");
29435 return error_mark_node;
29438 /* Parse the arguments and selectors comprising an Objective-C message.
29443 objc-selector-args , objc-comma-args
29445 objc-selector-args:
29446 objc-selector [opt] : assignment-expression
29447 objc-selector-args objc-selector [opt] : assignment-expression
29450 assignment-expression
29451 objc-comma-args , assignment-expression
29453 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29454 selector arguments and TREE_VALUE containing a list of comma
29458 cp_parser_objc_message_args (cp_parser* parser)
29460 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29461 bool maybe_unary_selector_p = true;
29462 cp_token *token = cp_lexer_peek_token (parser->lexer);
29464 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29466 tree selector = NULL_TREE, arg;
29468 if (token->type != CPP_COLON)
29469 selector = cp_parser_objc_selector (parser);
29471 /* Detect if we have a unary selector. */
29472 if (maybe_unary_selector_p
29473 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29474 return build_tree_list (selector, NULL_TREE);
29476 maybe_unary_selector_p = false;
29477 cp_parser_require (parser, CPP_COLON, RT_COLON);
29478 arg = cp_parser_assignment_expression (parser);
29481 = chainon (sel_args,
29482 build_tree_list (selector, arg));
29484 token = cp_lexer_peek_token (parser->lexer);
29487 /* Handle non-selector arguments, if any. */
29488 while (token->type == CPP_COMMA)
29492 cp_lexer_consume_token (parser->lexer);
29493 arg = cp_parser_assignment_expression (parser);
29496 = chainon (addl_args,
29497 build_tree_list (NULL_TREE, arg));
29499 token = cp_lexer_peek_token (parser->lexer);
29502 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29504 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29505 return build_tree_list (error_mark_node, error_mark_node);
29508 return build_tree_list (sel_args, addl_args);
29511 /* Parse an Objective-C encode expression.
29513 objc-encode-expression:
29514 @encode objc-typename
29516 Returns an encoded representation of the type argument. */
29519 cp_parser_objc_encode_expression (cp_parser* parser)
29523 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29525 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29526 matching_parens parens;
29527 parens.require_open (parser);
29528 token = cp_lexer_peek_token (parser->lexer);
29529 type = complete_type (cp_parser_type_id (parser));
29530 parens.require_close (parser);
29534 error_at (token->location,
29535 "%<@encode%> must specify a type as an argument");
29536 return error_mark_node;
29539 /* This happens if we find @encode(T) (where T is a template
29540 typename or something dependent on a template typename) when
29541 parsing a template. In that case, we can't compile it
29542 immediately, but we rather create an AT_ENCODE_EXPR which will
29543 need to be instantiated when the template is used.
29545 if (dependent_type_p (type))
29547 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29548 TREE_READONLY (value) = 1;
29553 /* Build a location of the form:
29556 with caret==start at the @ token, finishing at the close paren. */
29557 location_t combined_loc
29558 = make_location (start_loc, start_loc,
29559 cp_lexer_previous_token (parser->lexer)->location);
29561 return cp_expr (objc_build_encode_expr (type), combined_loc);
29564 /* Parse an Objective-C @defs expression. */
29567 cp_parser_objc_defs_expression (cp_parser *parser)
29571 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29572 matching_parens parens;
29573 parens.require_open (parser);
29574 name = cp_parser_identifier (parser);
29575 parens.require_close (parser);
29577 return objc_get_class_ivars (name);
29580 /* Parse an Objective-C protocol expression.
29582 objc-protocol-expression:
29583 @protocol ( identifier )
29585 Returns a representation of the protocol expression. */
29588 cp_parser_objc_protocol_expression (cp_parser* parser)
29591 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29593 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29594 matching_parens parens;
29595 parens.require_open (parser);
29596 proto = cp_parser_identifier (parser);
29597 parens.require_close (parser);
29599 /* Build a location of the form:
29602 with caret==start at the @ token, finishing at the close paren. */
29603 location_t combined_loc
29604 = make_location (start_loc, start_loc,
29605 cp_lexer_previous_token (parser->lexer)->location);
29606 tree result = objc_build_protocol_expr (proto);
29607 protected_set_expr_location (result, combined_loc);
29611 /* Parse an Objective-C selector expression.
29613 objc-selector-expression:
29614 @selector ( objc-method-signature )
29616 objc-method-signature:
29622 objc-selector-seq objc-selector :
29624 Returns a representation of the method selector. */
29627 cp_parser_objc_selector_expression (cp_parser* parser)
29629 tree sel_seq = NULL_TREE;
29630 bool maybe_unary_selector_p = true;
29632 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29634 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29635 matching_parens parens;
29636 parens.require_open (parser);
29637 token = cp_lexer_peek_token (parser->lexer);
29639 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29640 || token->type == CPP_SCOPE)
29642 tree selector = NULL_TREE;
29644 if (token->type != CPP_COLON
29645 || token->type == CPP_SCOPE)
29646 selector = cp_parser_objc_selector (parser);
29648 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29649 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29651 /* Detect if we have a unary selector. */
29652 if (maybe_unary_selector_p)
29654 sel_seq = selector;
29655 goto finish_selector;
29659 cp_parser_error (parser, "expected %<:%>");
29662 maybe_unary_selector_p = false;
29663 token = cp_lexer_consume_token (parser->lexer);
29665 if (token->type == CPP_SCOPE)
29668 = chainon (sel_seq,
29669 build_tree_list (selector, NULL_TREE));
29671 = chainon (sel_seq,
29672 build_tree_list (NULL_TREE, NULL_TREE));
29676 = chainon (sel_seq,
29677 build_tree_list (selector, NULL_TREE));
29679 token = cp_lexer_peek_token (parser->lexer);
29683 parens.require_close (parser);
29686 /* Build a location of the form:
29689 with caret==start at the @ token, finishing at the close paren. */
29690 location_t combined_loc
29691 = make_location (loc, loc,
29692 cp_lexer_previous_token (parser->lexer)->location);
29693 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29694 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29695 protected_set_expr_location (result, combined_loc);
29699 /* Parse a list of identifiers.
29701 objc-identifier-list:
29703 objc-identifier-list , identifier
29705 Returns a TREE_LIST of identifier nodes. */
29708 cp_parser_objc_identifier_list (cp_parser* parser)
29714 identifier = cp_parser_identifier (parser);
29715 if (identifier == error_mark_node)
29716 return error_mark_node;
29718 list = build_tree_list (NULL_TREE, identifier);
29719 sep = cp_lexer_peek_token (parser->lexer);
29721 while (sep->type == CPP_COMMA)
29723 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29724 identifier = cp_parser_identifier (parser);
29725 if (identifier == error_mark_node)
29728 list = chainon (list, build_tree_list (NULL_TREE,
29730 sep = cp_lexer_peek_token (parser->lexer);
29736 /* Parse an Objective-C alias declaration.
29738 objc-alias-declaration:
29739 @compatibility_alias identifier identifier ;
29741 This function registers the alias mapping with the Objective-C front end.
29742 It returns nothing. */
29745 cp_parser_objc_alias_declaration (cp_parser* parser)
29749 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29750 alias = cp_parser_identifier (parser);
29751 orig = cp_parser_identifier (parser);
29752 objc_declare_alias (alias, orig);
29753 cp_parser_consume_semicolon_at_end_of_statement (parser);
29756 /* Parse an Objective-C class forward-declaration.
29758 objc-class-declaration:
29759 @class objc-identifier-list ;
29761 The function registers the forward declarations with the Objective-C
29762 front end. It returns nothing. */
29765 cp_parser_objc_class_declaration (cp_parser* parser)
29767 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29772 id = cp_parser_identifier (parser);
29773 if (id == error_mark_node)
29776 objc_declare_class (id);
29778 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29779 cp_lexer_consume_token (parser->lexer);
29783 cp_parser_consume_semicolon_at_end_of_statement (parser);
29786 /* Parse a list of Objective-C protocol references.
29788 objc-protocol-refs-opt:
29789 objc-protocol-refs [opt]
29791 objc-protocol-refs:
29792 < objc-identifier-list >
29794 Returns a TREE_LIST of identifiers, if any. */
29797 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29799 tree protorefs = NULL_TREE;
29801 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29803 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29804 protorefs = cp_parser_objc_identifier_list (parser);
29805 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29811 /* Parse a Objective-C visibility specification. */
29814 cp_parser_objc_visibility_spec (cp_parser* parser)
29816 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29818 switch (vis->keyword)
29820 case RID_AT_PRIVATE:
29821 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29823 case RID_AT_PROTECTED:
29824 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29826 case RID_AT_PUBLIC:
29827 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29829 case RID_AT_PACKAGE:
29830 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29836 /* Eat '@private'/'@protected'/'@public'. */
29837 cp_lexer_consume_token (parser->lexer);
29840 /* Parse an Objective-C method type. Return 'true' if it is a class
29841 (+) method, and 'false' if it is an instance (-) method. */
29844 cp_parser_objc_method_type (cp_parser* parser)
29846 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29852 /* Parse an Objective-C protocol qualifier. */
29855 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29857 tree quals = NULL_TREE, node;
29858 cp_token *token = cp_lexer_peek_token (parser->lexer);
29860 node = token->u.value;
29862 while (node && identifier_p (node)
29863 && (node == ridpointers [(int) RID_IN]
29864 || node == ridpointers [(int) RID_OUT]
29865 || node == ridpointers [(int) RID_INOUT]
29866 || node == ridpointers [(int) RID_BYCOPY]
29867 || node == ridpointers [(int) RID_BYREF]
29868 || node == ridpointers [(int) RID_ONEWAY]))
29870 quals = tree_cons (NULL_TREE, node, quals);
29871 cp_lexer_consume_token (parser->lexer);
29872 token = cp_lexer_peek_token (parser->lexer);
29873 node = token->u.value;
29879 /* Parse an Objective-C typename. */
29882 cp_parser_objc_typename (cp_parser* parser)
29884 tree type_name = NULL_TREE;
29886 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29888 tree proto_quals, cp_type = NULL_TREE;
29890 matching_parens parens;
29891 parens.consume_open (parser); /* Eat '('. */
29892 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29894 /* An ObjC type name may consist of just protocol qualifiers, in which
29895 case the type shall default to 'id'. */
29896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29898 cp_type = cp_parser_type_id (parser);
29900 /* If the type could not be parsed, an error has already
29901 been produced. For error recovery, behave as if it had
29902 not been specified, which will use the default type
29904 if (cp_type == error_mark_node)
29906 cp_type = NULL_TREE;
29907 /* We need to skip to the closing parenthesis as
29908 cp_parser_type_id() does not seem to do it for
29910 cp_parser_skip_to_closing_parenthesis (parser,
29911 /*recovering=*/true,
29912 /*or_comma=*/false,
29913 /*consume_paren=*/false);
29917 parens.require_close (parser);
29918 type_name = build_tree_list (proto_quals, cp_type);
29924 /* Check to see if TYPE refers to an Objective-C selector name. */
29927 cp_parser_objc_selector_p (enum cpp_ttype type)
29929 return (type == CPP_NAME || type == CPP_KEYWORD
29930 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29931 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29932 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29933 || type == CPP_XOR || type == CPP_XOR_EQ);
29936 /* Parse an Objective-C selector. */
29939 cp_parser_objc_selector (cp_parser* parser)
29941 cp_token *token = cp_lexer_consume_token (parser->lexer);
29943 if (!cp_parser_objc_selector_p (token->type))
29945 error_at (token->location, "invalid Objective-C++ selector name");
29946 return error_mark_node;
29949 /* C++ operator names are allowed to appear in ObjC selectors. */
29950 switch (token->type)
29952 case CPP_AND_AND: return get_identifier ("and");
29953 case CPP_AND_EQ: return get_identifier ("and_eq");
29954 case CPP_AND: return get_identifier ("bitand");
29955 case CPP_OR: return get_identifier ("bitor");
29956 case CPP_COMPL: return get_identifier ("compl");
29957 case CPP_NOT: return get_identifier ("not");
29958 case CPP_NOT_EQ: return get_identifier ("not_eq");
29959 case CPP_OR_OR: return get_identifier ("or");
29960 case CPP_OR_EQ: return get_identifier ("or_eq");
29961 case CPP_XOR: return get_identifier ("xor");
29962 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29963 default: return token->u.value;
29967 /* Parse an Objective-C params list. */
29970 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29972 tree params = NULL_TREE;
29973 bool maybe_unary_selector_p = true;
29974 cp_token *token = cp_lexer_peek_token (parser->lexer);
29976 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29978 tree selector = NULL_TREE, type_name, identifier;
29979 tree parm_attr = NULL_TREE;
29981 if (token->keyword == RID_ATTRIBUTE)
29984 if (token->type != CPP_COLON)
29985 selector = cp_parser_objc_selector (parser);
29987 /* Detect if we have a unary selector. */
29988 if (maybe_unary_selector_p
29989 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29991 params = selector; /* Might be followed by attributes. */
29995 maybe_unary_selector_p = false;
29996 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29998 /* Something went quite wrong. There should be a colon
29999 here, but there is not. Stop parsing parameters. */
30002 type_name = cp_parser_objc_typename (parser);
30003 /* New ObjC allows attributes on parameters too. */
30004 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30005 parm_attr = cp_parser_attributes_opt (parser);
30006 identifier = cp_parser_identifier (parser);
30010 objc_build_keyword_decl (selector,
30015 token = cp_lexer_peek_token (parser->lexer);
30018 if (params == NULL_TREE)
30020 cp_parser_error (parser, "objective-c++ method declaration is expected");
30021 return error_mark_node;
30024 /* We allow tail attributes for the method. */
30025 if (token->keyword == RID_ATTRIBUTE)
30027 *attributes = cp_parser_attributes_opt (parser);
30028 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30029 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30031 cp_parser_error (parser,
30032 "method attributes must be specified at the end");
30033 return error_mark_node;
30036 if (params == NULL_TREE)
30038 cp_parser_error (parser, "objective-c++ method declaration is expected");
30039 return error_mark_node;
30044 /* Parse the non-keyword Objective-C params. */
30047 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30050 tree params = make_node (TREE_LIST);
30051 cp_token *token = cp_lexer_peek_token (parser->lexer);
30052 *ellipsisp = false; /* Initially, assume no ellipsis. */
30054 while (token->type == CPP_COMMA)
30056 cp_parameter_declarator *parmdecl;
30059 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30060 token = cp_lexer_peek_token (parser->lexer);
30062 if (token->type == CPP_ELLIPSIS)
30064 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30066 token = cp_lexer_peek_token (parser->lexer);
30070 /* TODO: parse attributes for tail parameters. */
30071 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30072 parm = grokdeclarator (parmdecl->declarator,
30073 &parmdecl->decl_specifiers,
30074 PARM, /*initialized=*/0,
30075 /*attrlist=*/NULL);
30077 chainon (params, build_tree_list (NULL_TREE, parm));
30078 token = cp_lexer_peek_token (parser->lexer);
30081 /* We allow tail attributes for the method. */
30082 if (token->keyword == RID_ATTRIBUTE)
30084 if (*attributes == NULL_TREE)
30086 *attributes = cp_parser_attributes_opt (parser);
30087 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30088 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30092 /* We have an error, but parse the attributes, so that we can
30094 *attributes = cp_parser_attributes_opt (parser);
30096 cp_parser_error (parser,
30097 "method attributes must be specified at the end");
30098 return error_mark_node;
30104 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30107 cp_parser_objc_interstitial_code (cp_parser* parser)
30109 cp_token *token = cp_lexer_peek_token (parser->lexer);
30111 /* If the next token is `extern' and the following token is a string
30112 literal, then we have a linkage specification. */
30113 if (token->keyword == RID_EXTERN
30114 && cp_parser_is_pure_string_literal
30115 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30116 cp_parser_linkage_specification (parser);
30117 /* Handle #pragma, if any. */
30118 else if (token->type == CPP_PRAGMA)
30119 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30120 /* Allow stray semicolons. */
30121 else if (token->type == CPP_SEMICOLON)
30122 cp_lexer_consume_token (parser->lexer);
30123 /* Mark methods as optional or required, when building protocols. */
30124 else if (token->keyword == RID_AT_OPTIONAL)
30126 cp_lexer_consume_token (parser->lexer);
30127 objc_set_method_opt (true);
30129 else if (token->keyword == RID_AT_REQUIRED)
30131 cp_lexer_consume_token (parser->lexer);
30132 objc_set_method_opt (false);
30134 else if (token->keyword == RID_NAMESPACE)
30135 cp_parser_namespace_definition (parser);
30136 /* Other stray characters must generate errors. */
30137 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30139 cp_lexer_consume_token (parser->lexer);
30140 error ("stray %qs between Objective-C++ methods",
30141 token->type == CPP_OPEN_BRACE ? "{" : "}");
30143 /* Finally, try to parse a block-declaration, or a function-definition. */
30145 cp_parser_block_declaration (parser, /*statement_p=*/false);
30148 /* Parse a method signature. */
30151 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30153 tree rettype, kwdparms, optparms;
30154 bool ellipsis = false;
30155 bool is_class_method;
30157 is_class_method = cp_parser_objc_method_type (parser);
30158 rettype = cp_parser_objc_typename (parser);
30159 *attributes = NULL_TREE;
30160 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30161 if (kwdparms == error_mark_node)
30162 return error_mark_node;
30163 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30164 if (optparms == error_mark_node)
30165 return error_mark_node;
30167 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30171 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30174 cp_lexer_save_tokens (parser->lexer);
30175 tattr = cp_parser_attributes_opt (parser);
30176 gcc_assert (tattr) ;
30178 /* If the attributes are followed by a method introducer, this is not allowed.
30179 Dump the attributes and flag the situation. */
30180 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30181 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30184 /* Otherwise, the attributes introduce some interstitial code, possibly so
30185 rewind to allow that check. */
30186 cp_lexer_rollback_tokens (parser->lexer);
30190 /* Parse an Objective-C method prototype list. */
30193 cp_parser_objc_method_prototype_list (cp_parser* parser)
30195 cp_token *token = cp_lexer_peek_token (parser->lexer);
30197 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30199 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30201 tree attributes, sig;
30202 bool is_class_method;
30203 if (token->type == CPP_PLUS)
30204 is_class_method = true;
30206 is_class_method = false;
30207 sig = cp_parser_objc_method_signature (parser, &attributes);
30208 if (sig == error_mark_node)
30210 cp_parser_skip_to_end_of_block_or_statement (parser);
30211 token = cp_lexer_peek_token (parser->lexer);
30214 objc_add_method_declaration (is_class_method, sig, attributes);
30215 cp_parser_consume_semicolon_at_end_of_statement (parser);
30217 else if (token->keyword == RID_AT_PROPERTY)
30218 cp_parser_objc_at_property_declaration (parser);
30219 else if (token->keyword == RID_ATTRIBUTE
30220 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30221 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30223 "prefix attributes are ignored for methods");
30225 /* Allow for interspersed non-ObjC++ code. */
30226 cp_parser_objc_interstitial_code (parser);
30228 token = cp_lexer_peek_token (parser->lexer);
30231 if (token->type != CPP_EOF)
30232 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30234 cp_parser_error (parser, "expected %<@end%>");
30236 objc_finish_interface ();
30239 /* Parse an Objective-C method definition list. */
30242 cp_parser_objc_method_definition_list (cp_parser* parser)
30244 cp_token *token = cp_lexer_peek_token (parser->lexer);
30246 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30250 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30253 tree sig, attribute;
30254 bool is_class_method;
30255 if (token->type == CPP_PLUS)
30256 is_class_method = true;
30258 is_class_method = false;
30259 push_deferring_access_checks (dk_deferred);
30260 sig = cp_parser_objc_method_signature (parser, &attribute);
30261 if (sig == error_mark_node)
30263 cp_parser_skip_to_end_of_block_or_statement (parser);
30264 token = cp_lexer_peek_token (parser->lexer);
30267 objc_start_method_definition (is_class_method, sig, attribute,
30270 /* For historical reasons, we accept an optional semicolon. */
30271 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30272 cp_lexer_consume_token (parser->lexer);
30274 ptk = cp_lexer_peek_token (parser->lexer);
30275 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30276 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30278 perform_deferred_access_checks (tf_warning_or_error);
30279 stop_deferring_access_checks ();
30280 meth = cp_parser_function_definition_after_declarator (parser,
30282 pop_deferring_access_checks ();
30283 objc_finish_method_definition (meth);
30286 /* The following case will be removed once @synthesize is
30287 completely implemented. */
30288 else if (token->keyword == RID_AT_PROPERTY)
30289 cp_parser_objc_at_property_declaration (parser);
30290 else if (token->keyword == RID_AT_SYNTHESIZE)
30291 cp_parser_objc_at_synthesize_declaration (parser);
30292 else if (token->keyword == RID_AT_DYNAMIC)
30293 cp_parser_objc_at_dynamic_declaration (parser);
30294 else if (token->keyword == RID_ATTRIBUTE
30295 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30296 warning_at (token->location, OPT_Wattributes,
30297 "prefix attributes are ignored for methods");
30299 /* Allow for interspersed non-ObjC++ code. */
30300 cp_parser_objc_interstitial_code (parser);
30302 token = cp_lexer_peek_token (parser->lexer);
30305 if (token->type != CPP_EOF)
30306 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30308 cp_parser_error (parser, "expected %<@end%>");
30310 objc_finish_implementation ();
30313 /* Parse Objective-C ivars. */
30316 cp_parser_objc_class_ivars (cp_parser* parser)
30318 cp_token *token = cp_lexer_peek_token (parser->lexer);
30320 if (token->type != CPP_OPEN_BRACE)
30321 return; /* No ivars specified. */
30323 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30324 token = cp_lexer_peek_token (parser->lexer);
30326 while (token->type != CPP_CLOSE_BRACE
30327 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30329 cp_decl_specifier_seq declspecs;
30330 int decl_class_or_enum_p;
30331 tree prefix_attributes;
30333 cp_parser_objc_visibility_spec (parser);
30335 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30338 cp_parser_decl_specifier_seq (parser,
30339 CP_PARSER_FLAGS_OPTIONAL,
30341 &decl_class_or_enum_p);
30343 /* auto, register, static, extern, mutable. */
30344 if (declspecs.storage_class != sc_none)
30346 cp_parser_error (parser, "invalid type for instance variable");
30347 declspecs.storage_class = sc_none;
30350 /* thread_local. */
30351 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30353 cp_parser_error (parser, "invalid type for instance variable");
30354 declspecs.locations[ds_thread] = 0;
30358 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30360 cp_parser_error (parser, "invalid type for instance variable");
30361 declspecs.locations[ds_typedef] = 0;
30364 prefix_attributes = declspecs.attributes;
30365 declspecs.attributes = NULL_TREE;
30367 /* Keep going until we hit the `;' at the end of the
30369 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30371 tree width = NULL_TREE, attributes, first_attribute, decl;
30372 cp_declarator *declarator = NULL;
30373 int ctor_dtor_or_conv_p;
30375 /* Check for a (possibly unnamed) bitfield declaration. */
30376 token = cp_lexer_peek_token (parser->lexer);
30377 if (token->type == CPP_COLON)
30380 if (token->type == CPP_NAME
30381 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30384 /* Get the name of the bitfield. */
30385 declarator = make_id_declarator (NULL_TREE,
30386 cp_parser_identifier (parser),
30390 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30391 /* Get the width of the bitfield. */
30393 = cp_parser_constant_expression (parser);
30397 /* Parse the declarator. */
30399 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30400 &ctor_dtor_or_conv_p,
30401 /*parenthesized_p=*/NULL,
30402 /*member_p=*/false,
30403 /*friend_p=*/false);
30406 /* Look for attributes that apply to the ivar. */
30407 attributes = cp_parser_attributes_opt (parser);
30408 /* Remember which attributes are prefix attributes and
30410 first_attribute = attributes;
30411 /* Combine the attributes. */
30412 attributes = attr_chainon (prefix_attributes, attributes);
30415 /* Create the bitfield declaration. */
30416 decl = grokbitfield (declarator, &declspecs,
30417 width, NULL_TREE, attributes);
30419 decl = grokfield (declarator, &declspecs,
30420 NULL_TREE, /*init_const_expr_p=*/false,
30421 NULL_TREE, attributes);
30423 /* Add the instance variable. */
30424 if (decl != error_mark_node && decl != NULL_TREE)
30425 objc_add_instance_variable (decl);
30427 /* Reset PREFIX_ATTRIBUTES. */
30428 if (attributes != error_mark_node)
30430 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30431 attributes = TREE_CHAIN (attributes);
30433 TREE_CHAIN (attributes) = NULL_TREE;
30436 token = cp_lexer_peek_token (parser->lexer);
30438 if (token->type == CPP_COMMA)
30440 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30446 cp_parser_consume_semicolon_at_end_of_statement (parser);
30447 token = cp_lexer_peek_token (parser->lexer);
30450 if (token->keyword == RID_AT_END)
30451 cp_parser_error (parser, "expected %<}%>");
30453 /* Do not consume the RID_AT_END, so it will be read again as terminating
30454 the @interface of @implementation. */
30455 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30456 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30458 /* For historical reasons, we accept an optional semicolon. */
30459 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30460 cp_lexer_consume_token (parser->lexer);
30463 /* Parse an Objective-C protocol declaration. */
30466 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30468 tree proto, protorefs;
30471 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30472 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30474 tok = cp_lexer_peek_token (parser->lexer);
30475 error_at (tok->location, "identifier expected after %<@protocol%>");
30476 cp_parser_consume_semicolon_at_end_of_statement (parser);
30480 /* See if we have a forward declaration or a definition. */
30481 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30483 /* Try a forward declaration first. */
30484 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30490 id = cp_parser_identifier (parser);
30491 if (id == error_mark_node)
30494 objc_declare_protocol (id, attributes);
30496 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30497 cp_lexer_consume_token (parser->lexer);
30501 cp_parser_consume_semicolon_at_end_of_statement (parser);
30504 /* Ok, we got a full-fledged definition (or at least should). */
30507 proto = cp_parser_identifier (parser);
30508 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30509 objc_start_protocol (proto, protorefs, attributes);
30510 cp_parser_objc_method_prototype_list (parser);
30514 /* Parse an Objective-C superclass or category. */
30517 cp_parser_objc_superclass_or_category (cp_parser *parser,
30520 tree *categ, bool *is_class_extension)
30522 cp_token *next = cp_lexer_peek_token (parser->lexer);
30524 *super = *categ = NULL_TREE;
30525 *is_class_extension = false;
30526 if (next->type == CPP_COLON)
30528 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30529 *super = cp_parser_identifier (parser);
30531 else if (next->type == CPP_OPEN_PAREN)
30533 matching_parens parens;
30534 parens.consume_open (parser); /* Eat '('. */
30536 /* If there is no category name, and this is an @interface, we
30537 have a class extension. */
30538 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30540 *categ = NULL_TREE;
30541 *is_class_extension = true;
30544 *categ = cp_parser_identifier (parser);
30546 parens.require_close (parser);
30550 /* Parse an Objective-C class interface. */
30553 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30555 tree name, super, categ, protos;
30556 bool is_class_extension;
30558 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30559 name = cp_parser_identifier (parser);
30560 if (name == error_mark_node)
30562 /* It's hard to recover because even if valid @interface stuff
30563 is to follow, we can't compile it (or validate it) if we
30564 don't even know which class it refers to. Let's assume this
30565 was a stray '@interface' token in the stream and skip it.
30569 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30570 &is_class_extension);
30571 protos = cp_parser_objc_protocol_refs_opt (parser);
30573 /* We have either a class or a category on our hands. */
30574 if (categ || is_class_extension)
30575 objc_start_category_interface (name, categ, protos, attributes);
30578 objc_start_class_interface (name, super, protos, attributes);
30579 /* Handle instance variable declarations, if any. */
30580 cp_parser_objc_class_ivars (parser);
30581 objc_continue_interface ();
30584 cp_parser_objc_method_prototype_list (parser);
30587 /* Parse an Objective-C class implementation. */
30590 cp_parser_objc_class_implementation (cp_parser* parser)
30592 tree name, super, categ;
30593 bool is_class_extension;
30595 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30596 name = cp_parser_identifier (parser);
30597 if (name == error_mark_node)
30599 /* It's hard to recover because even if valid @implementation
30600 stuff is to follow, we can't compile it (or validate it) if
30601 we don't even know which class it refers to. Let's assume
30602 this was a stray '@implementation' token in the stream and
30607 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30608 &is_class_extension);
30610 /* We have either a class or a category on our hands. */
30612 objc_start_category_implementation (name, categ);
30615 objc_start_class_implementation (name, super);
30616 /* Handle instance variable declarations, if any. */
30617 cp_parser_objc_class_ivars (parser);
30618 objc_continue_implementation ();
30621 cp_parser_objc_method_definition_list (parser);
30624 /* Consume the @end token and finish off the implementation. */
30627 cp_parser_objc_end_implementation (cp_parser* parser)
30629 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30630 objc_finish_implementation ();
30633 /* Parse an Objective-C declaration. */
30636 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30638 /* Try to figure out what kind of declaration is present. */
30639 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30642 switch (kwd->keyword)
30647 error_at (kwd->location, "attributes may not be specified before"
30648 " the %<@%D%> Objective-C++ keyword",
30652 case RID_AT_IMPLEMENTATION:
30653 warning_at (kwd->location, OPT_Wattributes,
30654 "prefix attributes are ignored before %<@%D%>",
30661 switch (kwd->keyword)
30664 cp_parser_objc_alias_declaration (parser);
30667 cp_parser_objc_class_declaration (parser);
30669 case RID_AT_PROTOCOL:
30670 cp_parser_objc_protocol_declaration (parser, attributes);
30672 case RID_AT_INTERFACE:
30673 cp_parser_objc_class_interface (parser, attributes);
30675 case RID_AT_IMPLEMENTATION:
30676 cp_parser_objc_class_implementation (parser);
30679 cp_parser_objc_end_implementation (parser);
30682 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30684 cp_parser_skip_to_end_of_block_or_statement (parser);
30688 /* Parse an Objective-C try-catch-finally statement.
30690 objc-try-catch-finally-stmt:
30691 @try compound-statement objc-catch-clause-seq [opt]
30692 objc-finally-clause [opt]
30694 objc-catch-clause-seq:
30695 objc-catch-clause objc-catch-clause-seq [opt]
30698 @catch ( objc-exception-declaration ) compound-statement
30700 objc-finally-clause:
30701 @finally compound-statement
30703 objc-exception-declaration:
30704 parameter-declaration
30707 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30711 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30712 for C. Keep them in sync. */
30715 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30717 location_t location;
30720 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30721 location = cp_lexer_peek_token (parser->lexer)->location;
30722 objc_maybe_warn_exceptions (location);
30723 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30724 node, lest it get absorbed into the surrounding block. */
30725 stmt = push_stmt_list ();
30726 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30727 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30729 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30731 cp_parameter_declarator *parm;
30732 tree parameter_declaration = error_mark_node;
30733 bool seen_open_paren = false;
30734 matching_parens parens;
30736 cp_lexer_consume_token (parser->lexer);
30737 if (parens.require_open (parser))
30738 seen_open_paren = true;
30739 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30741 /* We have "@catch (...)" (where the '...' are literally
30742 what is in the code). Skip the '...'.
30743 parameter_declaration is set to NULL_TREE, and
30744 objc_being_catch_clauses() knows that that means
30746 cp_lexer_consume_token (parser->lexer);
30747 parameter_declaration = NULL_TREE;
30751 /* We have "@catch (NSException *exception)" or something
30752 like that. Parse the parameter declaration. */
30753 parm = cp_parser_parameter_declaration (parser, false, NULL);
30755 parameter_declaration = error_mark_node;
30757 parameter_declaration = grokdeclarator (parm->declarator,
30758 &parm->decl_specifiers,
30759 PARM, /*initialized=*/0,
30760 /*attrlist=*/NULL);
30762 if (seen_open_paren)
30763 parens.require_close (parser);
30766 /* If there was no open parenthesis, we are recovering from
30767 an error, and we are trying to figure out what mistake
30768 the user has made. */
30770 /* If there is an immediate closing parenthesis, the user
30771 probably forgot the opening one (ie, they typed "@catch
30772 NSException *e)". Parse the closing parenthesis and keep
30774 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30775 cp_lexer_consume_token (parser->lexer);
30777 /* If these is no immediate closing parenthesis, the user
30778 probably doesn't know that parenthesis are required at
30779 all (ie, they typed "@catch NSException *e"). So, just
30780 forget about the closing parenthesis and keep going. */
30782 objc_begin_catch_clause (parameter_declaration);
30783 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30784 objc_finish_catch_clause ();
30786 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30788 cp_lexer_consume_token (parser->lexer);
30789 location = cp_lexer_peek_token (parser->lexer)->location;
30790 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30791 node, lest it get absorbed into the surrounding block. */
30792 stmt = push_stmt_list ();
30793 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30794 objc_build_finally_clause (location, pop_stmt_list (stmt));
30797 return objc_finish_try_stmt ();
30800 /* Parse an Objective-C synchronized statement.
30802 objc-synchronized-stmt:
30803 @synchronized ( expression ) compound-statement
30805 Returns NULL_TREE. */
30808 cp_parser_objc_synchronized_statement (cp_parser *parser)
30810 location_t location;
30813 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30815 location = cp_lexer_peek_token (parser->lexer)->location;
30816 objc_maybe_warn_exceptions (location);
30817 matching_parens parens;
30818 parens.require_open (parser);
30819 lock = cp_parser_expression (parser);
30820 parens.require_close (parser);
30822 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30823 node, lest it get absorbed into the surrounding block. */
30824 stmt = push_stmt_list ();
30825 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30827 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30830 /* Parse an Objective-C throw statement.
30833 @throw assignment-expression [opt] ;
30835 Returns a constructed '@throw' statement. */
30838 cp_parser_objc_throw_statement (cp_parser *parser)
30840 tree expr = NULL_TREE;
30841 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30843 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30846 expr = cp_parser_expression (parser);
30848 cp_parser_consume_semicolon_at_end_of_statement (parser);
30850 return objc_build_throw_stmt (loc, expr);
30853 /* Parse an Objective-C statement. */
30856 cp_parser_objc_statement (cp_parser * parser)
30858 /* Try to figure out what kind of declaration is present. */
30859 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30861 switch (kwd->keyword)
30864 return cp_parser_objc_try_catch_finally_statement (parser);
30865 case RID_AT_SYNCHRONIZED:
30866 return cp_parser_objc_synchronized_statement (parser);
30868 return cp_parser_objc_throw_statement (parser);
30870 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30872 cp_parser_skip_to_end_of_block_or_statement (parser);
30875 return error_mark_node;
30878 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30879 look ahead to see if an objc keyword follows the attributes. This
30880 is to detect the use of prefix attributes on ObjC @interface and
30884 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30886 cp_lexer_save_tokens (parser->lexer);
30887 *attrib = cp_parser_attributes_opt (parser);
30888 gcc_assert (*attrib);
30889 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30891 cp_lexer_commit_tokens (parser->lexer);
30894 cp_lexer_rollback_tokens (parser->lexer);
30898 /* This routine is a minimal replacement for
30899 c_parser_struct_declaration () used when parsing the list of
30900 types/names or ObjC++ properties. For example, when parsing the
30903 @property (readonly) int a, b, c;
30905 this function is responsible for parsing "int a, int b, int c" and
30906 returning the declarations as CHAIN of DECLs.
30908 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30909 similar parsing. */
30911 cp_parser_objc_struct_declaration (cp_parser *parser)
30913 tree decls = NULL_TREE;
30914 cp_decl_specifier_seq declspecs;
30915 int decl_class_or_enum_p;
30916 tree prefix_attributes;
30918 cp_parser_decl_specifier_seq (parser,
30919 CP_PARSER_FLAGS_NONE,
30921 &decl_class_or_enum_p);
30923 if (declspecs.type == error_mark_node)
30924 return error_mark_node;
30926 /* auto, register, static, extern, mutable. */
30927 if (declspecs.storage_class != sc_none)
30929 cp_parser_error (parser, "invalid type for property");
30930 declspecs.storage_class = sc_none;
30933 /* thread_local. */
30934 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30936 cp_parser_error (parser, "invalid type for property");
30937 declspecs.locations[ds_thread] = 0;
30941 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30943 cp_parser_error (parser, "invalid type for property");
30944 declspecs.locations[ds_typedef] = 0;
30947 prefix_attributes = declspecs.attributes;
30948 declspecs.attributes = NULL_TREE;
30950 /* Keep going until we hit the `;' at the end of the declaration. */
30951 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30953 tree attributes, first_attribute, decl;
30954 cp_declarator *declarator;
30957 /* Parse the declarator. */
30958 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30959 NULL, NULL, false, false);
30961 /* Look for attributes that apply to the ivar. */
30962 attributes = cp_parser_attributes_opt (parser);
30963 /* Remember which attributes are prefix attributes and
30965 first_attribute = attributes;
30966 /* Combine the attributes. */
30967 attributes = attr_chainon (prefix_attributes, attributes);
30969 decl = grokfield (declarator, &declspecs,
30970 NULL_TREE, /*init_const_expr_p=*/false,
30971 NULL_TREE, attributes);
30973 if (decl == error_mark_node || decl == NULL_TREE)
30974 return error_mark_node;
30976 /* Reset PREFIX_ATTRIBUTES. */
30977 if (attributes != error_mark_node)
30979 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30980 attributes = TREE_CHAIN (attributes);
30982 TREE_CHAIN (attributes) = NULL_TREE;
30985 DECL_CHAIN (decl) = decls;
30988 token = cp_lexer_peek_token (parser->lexer);
30989 if (token->type == CPP_COMMA)
30991 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31000 /* Parse an Objective-C @property declaration. The syntax is:
31002 objc-property-declaration:
31003 '@property' objc-property-attributes[opt] struct-declaration ;
31005 objc-property-attributes:
31006 '(' objc-property-attribute-list ')'
31008 objc-property-attribute-list:
31009 objc-property-attribute
31010 objc-property-attribute-list, objc-property-attribute
31012 objc-property-attribute
31013 'getter' = identifier
31014 'setter' = identifier
31023 @property NSString *name;
31024 @property (readonly) id object;
31025 @property (retain, nonatomic, getter=getTheName) id name;
31026 @property int a, b, c;
31028 PS: This function is identical to
31029 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31031 cp_parser_objc_at_property_declaration (cp_parser *parser)
31033 /* The following variables hold the attributes of the properties as
31034 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31035 seen. When we see an attribute, we set them to 'true' (if they
31036 are boolean properties) or to the identifier (if they have an
31037 argument, ie, for getter and setter). Note that here we only
31038 parse the list of attributes, check the syntax and accumulate the
31039 attributes that we find. objc_add_property_declaration() will
31040 then process the information. */
31041 bool property_assign = false;
31042 bool property_copy = false;
31043 tree property_getter_ident = NULL_TREE;
31044 bool property_nonatomic = false;
31045 bool property_readonly = false;
31046 bool property_readwrite = false;
31047 bool property_retain = false;
31048 tree property_setter_ident = NULL_TREE;
31050 /* 'properties' is the list of properties that we read. Usually a
31051 single one, but maybe more (eg, in "@property int a, b, c;" there
31056 loc = cp_lexer_peek_token (parser->lexer)->location;
31058 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31060 /* Parse the optional attribute list... */
31061 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31064 matching_parens parens;
31065 parens.consume_open (parser);
31069 bool syntax_error = false;
31070 cp_token *token = cp_lexer_peek_token (parser->lexer);
31073 if (token->type != CPP_NAME)
31075 cp_parser_error (parser, "expected identifier");
31078 keyword = C_RID_CODE (token->u.value);
31079 cp_lexer_consume_token (parser->lexer);
31082 case RID_ASSIGN: property_assign = true; break;
31083 case RID_COPY: property_copy = true; break;
31084 case RID_NONATOMIC: property_nonatomic = true; break;
31085 case RID_READONLY: property_readonly = true; break;
31086 case RID_READWRITE: property_readwrite = true; break;
31087 case RID_RETAIN: property_retain = true; break;
31091 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31093 if (keyword == RID_GETTER)
31094 cp_parser_error (parser,
31095 "missing %<=%> (after %<getter%> attribute)");
31097 cp_parser_error (parser,
31098 "missing %<=%> (after %<setter%> attribute)");
31099 syntax_error = true;
31102 cp_lexer_consume_token (parser->lexer); /* eat the = */
31103 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31105 cp_parser_error (parser, "expected identifier");
31106 syntax_error = true;
31109 if (keyword == RID_SETTER)
31111 if (property_setter_ident != NULL_TREE)
31113 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31114 cp_lexer_consume_token (parser->lexer);
31117 property_setter_ident = cp_parser_objc_selector (parser);
31118 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31119 cp_parser_error (parser, "setter name must terminate with %<:%>");
31121 cp_lexer_consume_token (parser->lexer);
31125 if (property_getter_ident != NULL_TREE)
31127 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31128 cp_lexer_consume_token (parser->lexer);
31131 property_getter_ident = cp_parser_objc_selector (parser);
31135 cp_parser_error (parser, "unknown property attribute");
31136 syntax_error = true;
31143 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31144 cp_lexer_consume_token (parser->lexer);
31149 /* FIXME: "@property (setter, assign);" will generate a spurious
31150 "error: expected ‘)’ before ‘,’ token". This is because
31151 cp_parser_require, unlike the C counterpart, will produce an
31152 error even if we are in error recovery. */
31153 if (!parens.require_close (parser))
31155 cp_parser_skip_to_closing_parenthesis (parser,
31156 /*recovering=*/true,
31157 /*or_comma=*/false,
31158 /*consume_paren=*/true);
31162 /* ... and the property declaration(s). */
31163 properties = cp_parser_objc_struct_declaration (parser);
31165 if (properties == error_mark_node)
31167 cp_parser_skip_to_end_of_statement (parser);
31168 /* If the next token is now a `;', consume it. */
31169 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31170 cp_lexer_consume_token (parser->lexer);
31174 if (properties == NULL_TREE)
31175 cp_parser_error (parser, "expected identifier");
31178 /* Comma-separated properties are chained together in
31179 reverse order; add them one by one. */
31180 properties = nreverse (properties);
31182 for (; properties; properties = TREE_CHAIN (properties))
31183 objc_add_property_declaration (loc, copy_node (properties),
31184 property_readonly, property_readwrite,
31185 property_assign, property_retain,
31186 property_copy, property_nonatomic,
31187 property_getter_ident, property_setter_ident);
31190 cp_parser_consume_semicolon_at_end_of_statement (parser);
31193 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31195 objc-synthesize-declaration:
31196 @synthesize objc-synthesize-identifier-list ;
31198 objc-synthesize-identifier-list:
31199 objc-synthesize-identifier
31200 objc-synthesize-identifier-list, objc-synthesize-identifier
31202 objc-synthesize-identifier
31204 identifier = identifier
31207 @synthesize MyProperty;
31208 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31210 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31211 for C. Keep them in sync.
31214 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31216 tree list = NULL_TREE;
31218 loc = cp_lexer_peek_token (parser->lexer)->location;
31220 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31223 tree property, ivar;
31224 property = cp_parser_identifier (parser);
31225 if (property == error_mark_node)
31227 cp_parser_consume_semicolon_at_end_of_statement (parser);
31230 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31232 cp_lexer_consume_token (parser->lexer);
31233 ivar = cp_parser_identifier (parser);
31234 if (ivar == error_mark_node)
31236 cp_parser_consume_semicolon_at_end_of_statement (parser);
31242 list = chainon (list, build_tree_list (ivar, property));
31243 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31244 cp_lexer_consume_token (parser->lexer);
31248 cp_parser_consume_semicolon_at_end_of_statement (parser);
31249 objc_add_synthesize_declaration (loc, list);
31252 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31254 objc-dynamic-declaration:
31255 @dynamic identifier-list ;
31258 @dynamic MyProperty;
31259 @dynamic MyProperty, AnotherProperty;
31261 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31262 for C. Keep them in sync.
31265 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31267 tree list = NULL_TREE;
31269 loc = cp_lexer_peek_token (parser->lexer)->location;
31271 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31275 property = cp_parser_identifier (parser);
31276 if (property == error_mark_node)
31278 cp_parser_consume_semicolon_at_end_of_statement (parser);
31281 list = chainon (list, build_tree_list (NULL, property));
31282 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31283 cp_lexer_consume_token (parser->lexer);
31287 cp_parser_consume_semicolon_at_end_of_statement (parser);
31288 objc_add_dynamic_declaration (loc, list);
31292 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31294 /* Returns name of the next clause.
31295 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31296 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31297 returned and the token is consumed. */
31299 static pragma_omp_clause
31300 cp_parser_omp_clause_name (cp_parser *parser)
31302 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31305 result = PRAGMA_OACC_CLAUSE_AUTO;
31306 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31307 result = PRAGMA_OMP_CLAUSE_IF;
31308 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31309 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31310 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31311 result = PRAGMA_OACC_CLAUSE_DELETE;
31312 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31313 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31314 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31315 result = PRAGMA_OMP_CLAUSE_FOR;
31316 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31318 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31319 const char *p = IDENTIFIER_POINTER (id);
31324 if (!strcmp ("aligned", p))
31325 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31326 else if (!strcmp ("async", p))
31327 result = PRAGMA_OACC_CLAUSE_ASYNC;
31330 if (!strcmp ("collapse", p))
31331 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31332 else if (!strcmp ("copy", p))
31333 result = PRAGMA_OACC_CLAUSE_COPY;
31334 else if (!strcmp ("copyin", p))
31335 result = PRAGMA_OMP_CLAUSE_COPYIN;
31336 else if (!strcmp ("copyout", p))
31337 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31338 else if (!strcmp ("copyprivate", p))
31339 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31340 else if (!strcmp ("create", p))
31341 result = PRAGMA_OACC_CLAUSE_CREATE;
31344 if (!strcmp ("defaultmap", p))
31345 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31346 else if (!strcmp ("depend", p))
31347 result = PRAGMA_OMP_CLAUSE_DEPEND;
31348 else if (!strcmp ("device", p))
31349 result = PRAGMA_OMP_CLAUSE_DEVICE;
31350 else if (!strcmp ("deviceptr", p))
31351 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31352 else if (!strcmp ("device_resident", p))
31353 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31354 else if (!strcmp ("dist_schedule", p))
31355 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31358 if (!strcmp ("final", p))
31359 result = PRAGMA_OMP_CLAUSE_FINAL;
31360 else if (!strcmp ("firstprivate", p))
31361 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31362 else if (!strcmp ("from", p))
31363 result = PRAGMA_OMP_CLAUSE_FROM;
31366 if (!strcmp ("gang", p))
31367 result = PRAGMA_OACC_CLAUSE_GANG;
31368 else if (!strcmp ("grainsize", p))
31369 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31372 if (!strcmp ("hint", p))
31373 result = PRAGMA_OMP_CLAUSE_HINT;
31374 else if (!strcmp ("host", p))
31375 result = PRAGMA_OACC_CLAUSE_HOST;
31378 if (!strcmp ("inbranch", p))
31379 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31380 else if (!strcmp ("independent", p))
31381 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31382 else if (!strcmp ("is_device_ptr", p))
31383 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31386 if (!strcmp ("lastprivate", p))
31387 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31388 else if (!strcmp ("linear", p))
31389 result = PRAGMA_OMP_CLAUSE_LINEAR;
31390 else if (!strcmp ("link", p))
31391 result = PRAGMA_OMP_CLAUSE_LINK;
31394 if (!strcmp ("map", p))
31395 result = PRAGMA_OMP_CLAUSE_MAP;
31396 else if (!strcmp ("mergeable", p))
31397 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31400 if (!strcmp ("nogroup", p))
31401 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31402 else if (!strcmp ("notinbranch", p))
31403 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31404 else if (!strcmp ("nowait", p))
31405 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31406 else if (!strcmp ("num_gangs", p))
31407 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31408 else if (!strcmp ("num_tasks", p))
31409 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31410 else if (!strcmp ("num_teams", p))
31411 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31412 else if (!strcmp ("num_threads", p))
31413 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31414 else if (!strcmp ("num_workers", p))
31415 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31418 if (!strcmp ("ordered", p))
31419 result = PRAGMA_OMP_CLAUSE_ORDERED;
31422 if (!strcmp ("parallel", p))
31423 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31424 else if (!strcmp ("present", p))
31425 result = PRAGMA_OACC_CLAUSE_PRESENT;
31426 else if (!strcmp ("present_or_copy", p)
31427 || !strcmp ("pcopy", p))
31428 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31429 else if (!strcmp ("present_or_copyin", p)
31430 || !strcmp ("pcopyin", p))
31431 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31432 else if (!strcmp ("present_or_copyout", p)
31433 || !strcmp ("pcopyout", p))
31434 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31435 else if (!strcmp ("present_or_create", p)
31436 || !strcmp ("pcreate", p))
31437 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31438 else if (!strcmp ("priority", p))
31439 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31440 else if (!strcmp ("proc_bind", p))
31441 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31444 if (!strcmp ("reduction", p))
31445 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31448 if (!strcmp ("safelen", p))
31449 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31450 else if (!strcmp ("schedule", p))
31451 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31452 else if (!strcmp ("sections", p))
31453 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31454 else if (!strcmp ("self", p))
31455 result = PRAGMA_OACC_CLAUSE_SELF;
31456 else if (!strcmp ("seq", p))
31457 result = PRAGMA_OACC_CLAUSE_SEQ;
31458 else if (!strcmp ("shared", p))
31459 result = PRAGMA_OMP_CLAUSE_SHARED;
31460 else if (!strcmp ("simd", p))
31461 result = PRAGMA_OMP_CLAUSE_SIMD;
31462 else if (!strcmp ("simdlen", p))
31463 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31466 if (!strcmp ("taskgroup", p))
31467 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31468 else if (!strcmp ("thread_limit", p))
31469 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31470 else if (!strcmp ("threads", p))
31471 result = PRAGMA_OMP_CLAUSE_THREADS;
31472 else if (!strcmp ("tile", p))
31473 result = PRAGMA_OACC_CLAUSE_TILE;
31474 else if (!strcmp ("to", p))
31475 result = PRAGMA_OMP_CLAUSE_TO;
31478 if (!strcmp ("uniform", p))
31479 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31480 else if (!strcmp ("untied", p))
31481 result = PRAGMA_OMP_CLAUSE_UNTIED;
31482 else if (!strcmp ("use_device", p))
31483 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31484 else if (!strcmp ("use_device_ptr", p))
31485 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31488 if (!strcmp ("vector", p))
31489 result = PRAGMA_OACC_CLAUSE_VECTOR;
31490 else if (!strcmp ("vector_length", p))
31491 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31494 if (!strcmp ("wait", p))
31495 result = PRAGMA_OACC_CLAUSE_WAIT;
31496 else if (!strcmp ("worker", p))
31497 result = PRAGMA_OACC_CLAUSE_WORKER;
31502 if (result != PRAGMA_OMP_CLAUSE_NONE)
31503 cp_lexer_consume_token (parser->lexer);
31508 /* Validate that a clause of the given type does not already exist. */
31511 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31512 const char *name, location_t location)
31516 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31517 if (OMP_CLAUSE_CODE (c) == code)
31519 error_at (location, "too many %qs clauses", name);
31527 variable-list , identifier
31529 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31530 colon). An opening parenthesis will have been consumed by the caller.
31532 If KIND is nonzero, create the appropriate node and install the decl
31533 in OMP_CLAUSE_DECL and add the node to the head of the list.
31535 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31536 return the list created.
31538 COLON can be NULL if only closing parenthesis should end the list,
31539 or pointer to bool which will receive false if the list is terminated
31540 by closing parenthesis or true if the list is terminated by colon. */
31543 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31544 tree list, bool *colon)
31547 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31550 parser->colon_corrects_to_scope_p = false;
31557 token = cp_lexer_peek_token (parser->lexer);
31559 && current_class_ptr
31560 && cp_parser_is_keyword (token, RID_THIS))
31562 decl = finish_this_expr ();
31563 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31564 || CONVERT_EXPR_P (decl))
31565 decl = TREE_OPERAND (decl, 0);
31566 cp_lexer_consume_token (parser->lexer);
31570 name = cp_parser_id_expression (parser, /*template_p=*/false,
31571 /*check_dependency_p=*/true,
31572 /*template_p=*/NULL,
31573 /*declarator_p=*/false,
31574 /*optional_p=*/false);
31575 if (name == error_mark_node)
31578 if (identifier_p (name))
31579 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31582 if (decl == error_mark_node)
31583 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31586 if (decl == error_mark_node)
31588 else if (kind != 0)
31592 case OMP_CLAUSE__CACHE_:
31593 /* The OpenACC cache directive explicitly only allows "array
31594 elements or subarrays". */
31595 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31597 error_at (token->location, "expected %<[%>");
31598 decl = error_mark_node;
31602 case OMP_CLAUSE_MAP:
31603 case OMP_CLAUSE_FROM:
31604 case OMP_CLAUSE_TO:
31605 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31608 = cp_lexer_peek_token (parser->lexer)->location;
31609 cp_id_kind idk = CP_ID_KIND_NONE;
31610 cp_lexer_consume_token (parser->lexer);
31611 decl = convert_from_reference (decl);
31613 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31618 case OMP_CLAUSE_DEPEND:
31619 case OMP_CLAUSE_REDUCTION:
31620 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31622 tree low_bound = NULL_TREE, length = NULL_TREE;
31624 parser->colon_corrects_to_scope_p = false;
31625 cp_lexer_consume_token (parser->lexer);
31626 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31627 low_bound = cp_parser_expression (parser);
31629 parser->colon_corrects_to_scope_p
31630 = saved_colon_corrects_to_scope_p;
31631 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31632 length = integer_one_node;
31635 /* Look for `:'. */
31636 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31638 if (!cp_lexer_next_token_is (parser->lexer,
31640 length = cp_parser_expression (parser);
31642 /* Look for the closing `]'. */
31643 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31647 decl = tree_cons (low_bound, length, decl);
31654 tree u = build_omp_clause (token->location, kind);
31655 OMP_CLAUSE_DECL (u) = decl;
31656 OMP_CLAUSE_CHAIN (u) = list;
31660 list = tree_cons (decl, NULL_TREE, list);
31663 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31665 cp_lexer_consume_token (parser->lexer);
31669 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31671 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31674 cp_parser_require (parser, CPP_COLON, RT_COLON);
31678 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31682 /* Try to resync to an unnested comma. Copied from
31683 cp_parser_parenthesized_expression_list. */
31686 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31687 ending = cp_parser_skip_to_closing_parenthesis (parser,
31688 /*recovering=*/true,
31690 /*consume_paren=*/true);
31698 /* Similarly, but expect leading and trailing parenthesis. This is a very
31699 common case for omp clauses. */
31702 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31704 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31705 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31710 copy ( variable-list )
31711 copyin ( variable-list )
31712 copyout ( variable-list )
31713 create ( variable-list )
31714 delete ( variable-list )
31715 present ( variable-list )
31716 present_or_copy ( variable-list )
31717 pcopy ( variable-list )
31718 present_or_copyin ( variable-list )
31719 pcopyin ( variable-list )
31720 present_or_copyout ( variable-list )
31721 pcopyout ( variable-list )
31722 present_or_create ( variable-list )
31723 pcreate ( variable-list ) */
31726 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31729 enum gomp_map_kind kind;
31732 case PRAGMA_OACC_CLAUSE_COPY:
31733 kind = GOMP_MAP_FORCE_TOFROM;
31735 case PRAGMA_OACC_CLAUSE_COPYIN:
31736 kind = GOMP_MAP_FORCE_TO;
31738 case PRAGMA_OACC_CLAUSE_COPYOUT:
31739 kind = GOMP_MAP_FORCE_FROM;
31741 case PRAGMA_OACC_CLAUSE_CREATE:
31742 kind = GOMP_MAP_FORCE_ALLOC;
31744 case PRAGMA_OACC_CLAUSE_DELETE:
31745 kind = GOMP_MAP_DELETE;
31747 case PRAGMA_OACC_CLAUSE_DEVICE:
31748 kind = GOMP_MAP_FORCE_TO;
31750 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31751 kind = GOMP_MAP_DEVICE_RESIDENT;
31753 case PRAGMA_OACC_CLAUSE_HOST:
31754 case PRAGMA_OACC_CLAUSE_SELF:
31755 kind = GOMP_MAP_FORCE_FROM;
31757 case PRAGMA_OACC_CLAUSE_LINK:
31758 kind = GOMP_MAP_LINK;
31760 case PRAGMA_OACC_CLAUSE_PRESENT:
31761 kind = GOMP_MAP_FORCE_PRESENT;
31763 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31764 kind = GOMP_MAP_TOFROM;
31766 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31767 kind = GOMP_MAP_TO;
31769 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31770 kind = GOMP_MAP_FROM;
31772 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31773 kind = GOMP_MAP_ALLOC;
31776 gcc_unreachable ();
31779 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31781 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31782 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31788 deviceptr ( variable-list ) */
31791 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31793 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31796 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31797 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31798 variable-list must only allow for pointer variables. */
31799 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31800 for (t = vars; t; t = TREE_CHAIN (t))
31802 tree v = TREE_PURPOSE (t);
31803 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31804 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31805 OMP_CLAUSE_DECL (u) = v;
31806 OMP_CLAUSE_CHAIN (u) = list;
31820 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31821 enum omp_clause_code code,
31822 tree list, location_t location)
31824 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31825 tree c = build_omp_clause (location, code);
31826 OMP_CLAUSE_CHAIN (c) = list;
31831 num_gangs ( expression )
31832 num_workers ( expression )
31833 vector_length ( expression ) */
31836 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31837 const char *str, tree list)
31839 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31841 matching_parens parens;
31842 if (!parens.require_open (parser))
31845 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31847 if (t == error_mark_node
31848 || !parens.require_close (parser))
31850 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31851 /*or_comma=*/false,
31852 /*consume_paren=*/true);
31856 check_no_duplicate_clause (list, code, str, loc);
31858 tree c = build_omp_clause (loc, code);
31859 OMP_CLAUSE_OPERAND (c, 0) = t;
31860 OMP_CLAUSE_CHAIN (c) = list;
31866 gang [( gang-arg-list )]
31867 worker [( [num:] int-expr )]
31868 vector [( [length:] int-expr )]
31870 where gang-arg is one of:
31875 and size-expr may be:
31882 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31883 const char *str, tree list)
31885 const char *id = "num";
31886 cp_lexer *lexer = parser->lexer;
31887 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31888 location_t loc = cp_lexer_peek_token (lexer)->location;
31890 if (kind == OMP_CLAUSE_VECTOR)
31893 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31895 matching_parens parens;
31896 parens.consume_open (parser);
31900 cp_token *next = cp_lexer_peek_token (lexer);
31903 /* Gang static argument. */
31904 if (kind == OMP_CLAUSE_GANG
31905 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31907 cp_lexer_consume_token (lexer);
31909 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31910 goto cleanup_error;
31913 if (ops[idx] != NULL)
31915 cp_parser_error (parser, "too many %<static%> arguments");
31916 goto cleanup_error;
31919 /* Check for the '*' argument. */
31920 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31921 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31922 || cp_lexer_nth_token_is (parser->lexer, 2,
31925 cp_lexer_consume_token (lexer);
31926 ops[idx] = integer_minus_one_node;
31928 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31930 cp_lexer_consume_token (lexer);
31936 /* Worker num: argument and vector length: arguments. */
31937 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31938 && id_equal (next->u.value, id)
31939 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31941 cp_lexer_consume_token (lexer); /* id */
31942 cp_lexer_consume_token (lexer); /* ':' */
31945 /* Now collect the actual argument. */
31946 if (ops[idx] != NULL_TREE)
31948 cp_parser_error (parser, "unexpected argument");
31949 goto cleanup_error;
31952 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31954 if (expr == error_mark_node)
31955 goto cleanup_error;
31957 mark_exp_read (expr);
31960 if (kind == OMP_CLAUSE_GANG
31961 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31963 cp_lexer_consume_token (lexer);
31970 if (!parens.require_close (parser))
31971 goto cleanup_error;
31974 check_no_duplicate_clause (list, kind, str, loc);
31976 c = build_omp_clause (loc, kind);
31979 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31981 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31982 OMP_CLAUSE_CHAIN (c) = list;
31987 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31992 tile ( size-expr-list ) */
31995 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31997 tree c, expr = error_mark_node;
31998 tree tile = NULL_TREE;
32000 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32001 so, but the spec authors never considered such a case and have
32002 differing opinions on what it might mean, including 'not
32004 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32005 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32008 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32013 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32016 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32017 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32018 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32020 cp_lexer_consume_token (parser->lexer);
32021 expr = integer_zero_node;
32024 expr = cp_parser_constant_expression (parser);
32026 tile = tree_cons (NULL_TREE, expr, tile);
32028 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32030 /* Consume the trailing ')'. */
32031 cp_lexer_consume_token (parser->lexer);
32033 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32034 tile = nreverse (tile);
32035 OMP_CLAUSE_TILE_LIST (c) = tile;
32036 OMP_CLAUSE_CHAIN (c) = list;
32041 Parse wait clause or directive parameters. */
32044 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32046 vec<tree, va_gc> *args;
32049 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32051 /*allow_expansion_p=*/true,
32052 /*non_constant_p=*/NULL);
32054 if (args == NULL || args->length () == 0)
32056 cp_parser_error (parser, "expected integer expression before ')'");
32058 release_tree_vector (args);
32062 args_tree = build_tree_list_vec (args);
32064 release_tree_vector (args);
32066 for (t = args_tree; t; t = TREE_CHAIN (t))
32068 tree targ = TREE_VALUE (t);
32070 if (targ != error_mark_node)
32072 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32073 error ("%<wait%> expression must be integral");
32076 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32078 targ = mark_rvalue_use (targ);
32079 OMP_CLAUSE_DECL (c) = targ;
32080 OMP_CLAUSE_CHAIN (c) = list;
32090 wait ( int-expr-list ) */
32093 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32095 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32097 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32100 list = cp_parser_oacc_wait_list (parser, location, list);
32106 collapse ( constant-expression ) */
32109 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32115 loc = cp_lexer_peek_token (parser->lexer)->location;
32116 matching_parens parens;
32117 if (!parens.require_open (parser))
32120 num = cp_parser_constant_expression (parser);
32122 if (!parens.require_close (parser))
32123 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32124 /*or_comma=*/false,
32125 /*consume_paren=*/true);
32127 if (num == error_mark_node)
32129 num = fold_non_dependent_expr (num);
32130 if (!tree_fits_shwi_p (num)
32131 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32132 || (n = tree_to_shwi (num)) <= 0
32135 error_at (loc, "collapse argument needs positive constant integer expression");
32139 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32140 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32141 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32142 OMP_CLAUSE_CHAIN (c) = list;
32143 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32149 default ( none | shared )
32152 default ( none | present ) */
32155 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32156 location_t location, bool is_oacc)
32158 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32161 matching_parens parens;
32162 if (!parens.require_open (parser))
32164 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32166 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32167 const char *p = IDENTIFIER_POINTER (id);
32172 if (strcmp ("none", p) != 0)
32174 kind = OMP_CLAUSE_DEFAULT_NONE;
32178 if (strcmp ("present", p) != 0 || !is_oacc)
32180 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32184 if (strcmp ("shared", p) != 0 || is_oacc)
32186 kind = OMP_CLAUSE_DEFAULT_SHARED;
32193 cp_lexer_consume_token (parser->lexer);
32199 cp_parser_error (parser, "expected %<none%> or %<present%>");
32201 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32204 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32205 || !parens.require_close (parser))
32206 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32207 /*or_comma=*/false,
32208 /*consume_paren=*/true);
32210 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32213 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32214 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32215 OMP_CLAUSE_CHAIN (c) = list;
32216 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32222 final ( expression ) */
32225 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32229 matching_parens parens;
32230 if (!parens.require_open (parser))
32233 t = cp_parser_condition (parser);
32235 if (t == error_mark_node
32236 || !parens.require_close (parser))
32237 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32238 /*or_comma=*/false,
32239 /*consume_paren=*/true);
32241 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32243 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32244 OMP_CLAUSE_FINAL_EXPR (c) = t;
32245 OMP_CLAUSE_CHAIN (c) = list;
32254 if ( directive-name-modifier : expression )
32256 directive-name-modifier:
32257 parallel | task | taskloop | target data | target | target update
32258 | target enter data | target exit data */
32261 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32265 enum tree_code if_modifier = ERROR_MARK;
32267 matching_parens parens;
32268 if (!parens.require_open (parser))
32271 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32273 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32274 const char *p = IDENTIFIER_POINTER (id);
32277 if (strcmp ("parallel", p) == 0)
32278 if_modifier = OMP_PARALLEL;
32279 else if (strcmp ("task", p) == 0)
32280 if_modifier = OMP_TASK;
32281 else if (strcmp ("taskloop", p) == 0)
32282 if_modifier = OMP_TASKLOOP;
32283 else if (strcmp ("target", p) == 0)
32285 if_modifier = OMP_TARGET;
32286 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32288 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32289 p = IDENTIFIER_POINTER (id);
32290 if (strcmp ("data", p) == 0)
32291 if_modifier = OMP_TARGET_DATA;
32292 else if (strcmp ("update", p) == 0)
32293 if_modifier = OMP_TARGET_UPDATE;
32294 else if (strcmp ("enter", p) == 0)
32295 if_modifier = OMP_TARGET_ENTER_DATA;
32296 else if (strcmp ("exit", p) == 0)
32297 if_modifier = OMP_TARGET_EXIT_DATA;
32298 if (if_modifier != OMP_TARGET)
32303 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32304 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32306 if_modifier = ERROR_MARK;
32308 if (if_modifier == OMP_TARGET_ENTER_DATA
32309 || if_modifier == OMP_TARGET_EXIT_DATA)
32311 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32313 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32314 p = IDENTIFIER_POINTER (id);
32315 if (strcmp ("data", p) == 0)
32321 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32322 error_at (loc, "expected %<data%>");
32323 if_modifier = ERROR_MARK;
32328 if (if_modifier != ERROR_MARK)
32330 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32333 cp_lexer_consume_token (parser->lexer);
32340 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32341 error_at (loc, "expected %<:%>");
32343 if_modifier = ERROR_MARK;
32348 t = cp_parser_condition (parser);
32350 if (t == error_mark_node
32351 || !parens.require_close (parser))
32352 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32353 /*or_comma=*/false,
32354 /*consume_paren=*/true);
32356 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32357 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32359 if (if_modifier != ERROR_MARK
32360 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32362 const char *p = NULL;
32363 switch (if_modifier)
32365 case OMP_PARALLEL: p = "parallel"; break;
32366 case OMP_TASK: p = "task"; break;
32367 case OMP_TASKLOOP: p = "taskloop"; break;
32368 case OMP_TARGET_DATA: p = "target data"; break;
32369 case OMP_TARGET: p = "target"; break;
32370 case OMP_TARGET_UPDATE: p = "target update"; break;
32371 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32372 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32373 default: gcc_unreachable ();
32375 error_at (location, "too many %<if%> clauses with %qs modifier",
32379 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32382 error_at (location, "too many %<if%> clauses");
32384 error_at (location, "too many %<if%> clauses without modifier");
32387 else if (if_modifier == ERROR_MARK
32388 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32390 error_at (location, "if any %<if%> clause has modifier, then all "
32391 "%<if%> clauses have to use modifier");
32396 c = build_omp_clause (location, OMP_CLAUSE_IF);
32397 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32398 OMP_CLAUSE_IF_EXPR (c) = t;
32399 OMP_CLAUSE_CHAIN (c) = list;
32408 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32409 tree list, location_t location)
32413 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32416 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32417 OMP_CLAUSE_CHAIN (c) = list;
32425 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32426 tree list, location_t location)
32430 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32432 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32433 OMP_CLAUSE_CHAIN (c) = list;
32438 num_threads ( expression ) */
32441 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32442 location_t location)
32446 matching_parens parens;
32447 if (!parens.require_open (parser))
32450 t = cp_parser_expression (parser);
32452 if (t == error_mark_node
32453 || !parens.require_close (parser))
32454 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32455 /*or_comma=*/false,
32456 /*consume_paren=*/true);
32458 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32459 "num_threads", location);
32461 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32462 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32463 OMP_CLAUSE_CHAIN (c) = list;
32469 num_tasks ( expression ) */
32472 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32473 location_t location)
32477 matching_parens parens;
32478 if (!parens.require_open (parser))
32481 t = cp_parser_expression (parser);
32483 if (t == error_mark_node
32484 || !parens.require_close (parser))
32485 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32486 /*or_comma=*/false,
32487 /*consume_paren=*/true);
32489 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32490 "num_tasks", location);
32492 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32493 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32494 OMP_CLAUSE_CHAIN (c) = list;
32500 grainsize ( expression ) */
32503 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32504 location_t location)
32508 matching_parens parens;
32509 if (!parens.require_open (parser))
32512 t = cp_parser_expression (parser);
32514 if (t == error_mark_node
32515 || !parens.require_close (parser))
32516 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32517 /*or_comma=*/false,
32518 /*consume_paren=*/true);
32520 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32521 "grainsize", location);
32523 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32524 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32525 OMP_CLAUSE_CHAIN (c) = list;
32531 priority ( expression ) */
32534 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32535 location_t location)
32539 matching_parens parens;
32540 if (!parens.require_open (parser))
32543 t = cp_parser_expression (parser);
32545 if (t == error_mark_node
32546 || !parens.require_close (parser))
32547 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32548 /*or_comma=*/false,
32549 /*consume_paren=*/true);
32551 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32552 "priority", location);
32554 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32555 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32556 OMP_CLAUSE_CHAIN (c) = list;
32562 hint ( expression ) */
32565 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32566 location_t location)
32570 matching_parens parens;
32571 if (!parens.require_open (parser))
32574 t = cp_parser_expression (parser);
32576 if (t == error_mark_node
32577 || !parens.require_close (parser))
32578 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32579 /*or_comma=*/false,
32580 /*consume_paren=*/true);
32582 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32584 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32585 OMP_CLAUSE_HINT_EXPR (c) = t;
32586 OMP_CLAUSE_CHAIN (c) = list;
32592 defaultmap ( tofrom : scalar ) */
32595 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32596 location_t location)
32601 matching_parens parens;
32602 if (!parens.require_open (parser))
32605 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32607 cp_parser_error (parser, "expected %<tofrom%>");
32610 id = cp_lexer_peek_token (parser->lexer)->u.value;
32611 p = IDENTIFIER_POINTER (id);
32612 if (strcmp (p, "tofrom") != 0)
32614 cp_parser_error (parser, "expected %<tofrom%>");
32617 cp_lexer_consume_token (parser->lexer);
32618 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32621 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32623 cp_parser_error (parser, "expected %<scalar%>");
32626 id = cp_lexer_peek_token (parser->lexer)->u.value;
32627 p = IDENTIFIER_POINTER (id);
32628 if (strcmp (p, "scalar") != 0)
32630 cp_parser_error (parser, "expected %<scalar%>");
32633 cp_lexer_consume_token (parser->lexer);
32634 if (!parens.require_close (parser))
32637 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32640 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32641 OMP_CLAUSE_CHAIN (c) = list;
32645 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32646 /*or_comma=*/false,
32647 /*consume_paren=*/true);
32655 ordered ( constant-expression ) */
32658 cp_parser_omp_clause_ordered (cp_parser *parser,
32659 tree list, location_t location)
32661 tree c, num = NULL_TREE;
32664 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32665 "ordered", location);
32667 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32669 matching_parens parens;
32670 parens.consume_open (parser);
32672 num = cp_parser_constant_expression (parser);
32674 if (!parens.require_close (parser))
32675 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32676 /*or_comma=*/false,
32677 /*consume_paren=*/true);
32679 if (num == error_mark_node)
32681 num = fold_non_dependent_expr (num);
32682 if (!tree_fits_shwi_p (num)
32683 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32684 || (n = tree_to_shwi (num)) <= 0
32687 error_at (location,
32688 "ordered argument needs positive constant integer "
32694 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32695 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32696 OMP_CLAUSE_CHAIN (c) = list;
32701 reduction ( reduction-operator : variable-list )
32703 reduction-operator:
32704 One of: + * - & ^ | && ||
32708 reduction-operator:
32709 One of: + * - & ^ | && || min max
32713 reduction-operator:
32714 One of: + * - & ^ | && ||
32718 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32720 enum tree_code code = ERROR_MARK;
32721 tree nlist, c, id = NULL_TREE;
32723 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32726 switch (cp_lexer_peek_token (parser->lexer)->type)
32728 case CPP_PLUS: code = PLUS_EXPR; break;
32729 case CPP_MULT: code = MULT_EXPR; break;
32730 case CPP_MINUS: code = MINUS_EXPR; break;
32731 case CPP_AND: code = BIT_AND_EXPR; break;
32732 case CPP_XOR: code = BIT_XOR_EXPR; break;
32733 case CPP_OR: code = BIT_IOR_EXPR; break;
32734 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32735 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32739 if (code != ERROR_MARK)
32740 cp_lexer_consume_token (parser->lexer);
32743 bool saved_colon_corrects_to_scope_p;
32744 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32745 parser->colon_corrects_to_scope_p = false;
32746 id = cp_parser_id_expression (parser, /*template_p=*/false,
32747 /*check_dependency_p=*/true,
32748 /*template_p=*/NULL,
32749 /*declarator_p=*/false,
32750 /*optional_p=*/false);
32751 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32752 if (identifier_p (id))
32754 const char *p = IDENTIFIER_POINTER (id);
32756 if (strcmp (p, "min") == 0)
32758 else if (strcmp (p, "max") == 0)
32760 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32762 else if (id == ovl_op_identifier (false, MULT_EXPR))
32764 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32766 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32767 code = BIT_AND_EXPR;
32768 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32769 code = BIT_IOR_EXPR;
32770 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32771 code = BIT_XOR_EXPR;
32772 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32773 code = TRUTH_ANDIF_EXPR;
32774 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32775 code = TRUTH_ORIF_EXPR;
32776 id = omp_reduction_id (code, id, NULL_TREE);
32777 tree scope = parser->scope;
32779 id = build_qualified_name (NULL_TREE, scope, id, false);
32780 parser->scope = NULL_TREE;
32781 parser->qualifying_scope = NULL_TREE;
32782 parser->object_scope = NULL_TREE;
32786 error ("invalid reduction-identifier");
32788 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32789 /*or_comma=*/false,
32790 /*consume_paren=*/true);
32795 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32798 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32800 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32802 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32803 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32810 schedule ( schedule-kind )
32811 schedule ( schedule-kind , expression )
32814 static | dynamic | guided | runtime | auto
32817 schedule ( schedule-modifier : schedule-kind )
32818 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32826 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32829 int modifiers = 0, nmodifiers = 0;
32831 matching_parens parens;
32832 if (!parens.require_open (parser))
32835 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32837 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32839 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32840 const char *p = IDENTIFIER_POINTER (id);
32841 if (strcmp ("simd", p) == 0)
32842 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32843 else if (strcmp ("monotonic", p) == 0)
32844 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32845 else if (strcmp ("nonmonotonic", p) == 0)
32846 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32849 cp_lexer_consume_token (parser->lexer);
32850 if (nmodifiers++ == 0
32851 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32852 cp_lexer_consume_token (parser->lexer);
32855 cp_parser_require (parser, CPP_COLON, RT_COLON);
32860 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32862 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32863 const char *p = IDENTIFIER_POINTER (id);
32868 if (strcmp ("dynamic", p) != 0)
32870 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32874 if (strcmp ("guided", p) != 0)
32876 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32880 if (strcmp ("runtime", p) != 0)
32882 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32889 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32890 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32891 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32892 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32895 cp_lexer_consume_token (parser->lexer);
32897 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32898 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32899 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32900 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32902 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32907 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32910 cp_lexer_consume_token (parser->lexer);
32912 token = cp_lexer_peek_token (parser->lexer);
32913 t = cp_parser_assignment_expression (parser);
32915 if (t == error_mark_node)
32917 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32918 error_at (token->location, "schedule %<runtime%> does not take "
32919 "a %<chunk_size%> parameter");
32920 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32921 error_at (token->location, "schedule %<auto%> does not take "
32922 "a %<chunk_size%> parameter");
32924 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32926 if (!parens.require_close (parser))
32929 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32932 OMP_CLAUSE_SCHEDULE_KIND (c)
32933 = (enum omp_clause_schedule_kind)
32934 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32936 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32937 OMP_CLAUSE_CHAIN (c) = list;
32941 cp_parser_error (parser, "invalid schedule kind");
32943 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32944 /*or_comma=*/false,
32945 /*consume_paren=*/true);
32953 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32954 tree list, location_t location)
32958 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32960 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32961 OMP_CLAUSE_CHAIN (c) = list;
32970 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32971 tree list, location_t location)
32973 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32974 tree c = build_omp_clause (location, code);
32975 OMP_CLAUSE_CHAIN (c) = list;
32986 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32987 enum omp_clause_code code,
32988 tree list, location_t location)
32990 tree c = build_omp_clause (location, code);
32991 OMP_CLAUSE_CHAIN (c) = list;
32999 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33000 tree list, location_t location)
33002 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33003 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33004 OMP_CLAUSE_CHAIN (c) = list;
33013 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33014 enum omp_clause_code code,
33015 tree list, location_t location)
33017 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33018 tree c = build_omp_clause (location, code);
33019 OMP_CLAUSE_CHAIN (c) = list;
33024 num_teams ( expression ) */
33027 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33028 location_t location)
33032 matching_parens parens;
33033 if (!parens.require_open (parser))
33036 t = cp_parser_expression (parser);
33038 if (t == error_mark_node
33039 || !parens.require_close (parser))
33040 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33041 /*or_comma=*/false,
33042 /*consume_paren=*/true);
33044 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33045 "num_teams", location);
33047 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33048 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33049 OMP_CLAUSE_CHAIN (c) = list;
33055 thread_limit ( expression ) */
33058 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33059 location_t location)
33063 matching_parens parens;
33064 if (!parens.require_open (parser))
33067 t = cp_parser_expression (parser);
33069 if (t == error_mark_node
33070 || !parens.require_close (parser))
33071 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33072 /*or_comma=*/false,
33073 /*consume_paren=*/true);
33075 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33076 "thread_limit", location);
33078 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33079 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33080 OMP_CLAUSE_CHAIN (c) = list;
33086 aligned ( variable-list )
33087 aligned ( variable-list : constant-expression ) */
33090 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33092 tree nlist, c, alignment = NULL_TREE;
33095 matching_parens parens;
33096 if (!parens.require_open (parser))
33099 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33104 alignment = cp_parser_constant_expression (parser);
33106 if (!parens.require_close (parser))
33107 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33108 /*or_comma=*/false,
33109 /*consume_paren=*/true);
33111 if (alignment == error_mark_node)
33112 alignment = NULL_TREE;
33115 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33116 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33122 linear ( variable-list )
33123 linear ( variable-list : expression )
33126 linear ( modifier ( variable-list ) )
33127 linear ( modifier ( variable-list ) : expression ) */
33130 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33133 tree nlist, c, step = integer_one_node;
33135 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33137 matching_parens parens;
33138 if (!parens.require_open (parser))
33141 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33143 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33144 const char *p = IDENTIFIER_POINTER (id);
33146 if (strcmp ("ref", p) == 0)
33147 kind = OMP_CLAUSE_LINEAR_REF;
33148 else if (strcmp ("val", p) == 0)
33149 kind = OMP_CLAUSE_LINEAR_VAL;
33150 else if (strcmp ("uval", p) == 0)
33151 kind = OMP_CLAUSE_LINEAR_UVAL;
33152 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33153 cp_lexer_consume_token (parser->lexer);
33155 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33158 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33159 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33163 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33164 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33166 cp_parser_require (parser, CPP_COLON, RT_COLON);
33167 else if (!parens.require_close (parser))
33168 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33169 /*or_comma=*/false,
33170 /*consume_paren=*/true);
33177 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33178 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33180 cp_token *token = cp_lexer_peek_token (parser->lexer);
33181 cp_parser_parse_tentatively (parser);
33182 step = cp_parser_id_expression (parser, /*template_p=*/false,
33183 /*check_dependency_p=*/true,
33184 /*template_p=*/NULL,
33185 /*declarator_p=*/false,
33186 /*optional_p=*/false);
33187 if (step != error_mark_node)
33188 step = cp_parser_lookup_name_simple (parser, step, token->location);
33189 if (step == error_mark_node)
33192 cp_parser_abort_tentative_parse (parser);
33194 else if (!cp_parser_parse_definitely (parser))
33198 step = cp_parser_expression (parser);
33200 if (!parens.require_close (parser))
33201 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33202 /*or_comma=*/false,
33203 /*consume_paren=*/true);
33205 if (step == error_mark_node)
33209 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33211 OMP_CLAUSE_LINEAR_STEP (c) = step;
33212 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33219 safelen ( constant-expression ) */
33222 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33223 location_t location)
33227 matching_parens parens;
33228 if (!parens.require_open (parser))
33231 t = cp_parser_constant_expression (parser);
33233 if (t == error_mark_node
33234 || !parens.require_close (parser))
33235 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33236 /*or_comma=*/false,
33237 /*consume_paren=*/true);
33239 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33241 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33242 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33243 OMP_CLAUSE_CHAIN (c) = list;
33249 simdlen ( constant-expression ) */
33252 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33253 location_t location)
33257 matching_parens parens;
33258 if (!parens.require_open (parser))
33261 t = cp_parser_constant_expression (parser);
33263 if (t == error_mark_node
33264 || !parens.require_close (parser))
33265 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33266 /*or_comma=*/false,
33267 /*consume_paren=*/true);
33269 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33271 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33272 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33273 OMP_CLAUSE_CHAIN (c) = list;
33280 identifier [+/- integer]
33281 vec , identifier [+/- integer]
33285 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33292 cp_parser_error (parser, "expected identifier");
33296 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33298 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33299 tree t, identifier = cp_parser_identifier (parser);
33300 tree addend = NULL;
33302 if (identifier == error_mark_node)
33303 t = error_mark_node;
33306 t = cp_parser_lookup_name_simple
33307 (parser, identifier,
33308 cp_lexer_peek_token (parser->lexer)->location);
33309 if (t == error_mark_node)
33310 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33315 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33317 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33319 addend = integer_zero_node;
33320 goto add_to_vector;
33322 cp_lexer_consume_token (parser->lexer);
33324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33326 cp_parser_error (parser, "expected integer");
33330 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33331 if (TREE_CODE (addend) != INTEGER_CST)
33333 cp_parser_error (parser, "expected integer");
33336 cp_lexer_consume_token (parser->lexer);
33339 if (t != error_mark_node)
33341 vec = tree_cons (addend, t, vec);
33343 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33349 cp_lexer_consume_token (parser->lexer);
33352 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33354 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33355 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33356 OMP_CLAUSE_DECL (u) = nreverse (vec);
33357 OMP_CLAUSE_CHAIN (u) = list;
33364 depend ( depend-kind : variable-list )
33372 depend ( sink : vec ) */
33375 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33378 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33380 matching_parens parens;
33381 if (!parens.require_open (parser))
33384 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33386 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33387 const char *p = IDENTIFIER_POINTER (id);
33389 if (strcmp ("in", p) == 0)
33390 kind = OMP_CLAUSE_DEPEND_IN;
33391 else if (strcmp ("inout", p) == 0)
33392 kind = OMP_CLAUSE_DEPEND_INOUT;
33393 else if (strcmp ("out", p) == 0)
33394 kind = OMP_CLAUSE_DEPEND_OUT;
33395 else if (strcmp ("source", p) == 0)
33396 kind = OMP_CLAUSE_DEPEND_SOURCE;
33397 else if (strcmp ("sink", p) == 0)
33398 kind = OMP_CLAUSE_DEPEND_SINK;
33405 cp_lexer_consume_token (parser->lexer);
33407 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33409 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33410 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33411 OMP_CLAUSE_DECL (c) = NULL_TREE;
33412 OMP_CLAUSE_CHAIN (c) = list;
33413 if (!parens.require_close (parser))
33414 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33415 /*or_comma=*/false,
33416 /*consume_paren=*/true);
33420 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33423 if (kind == OMP_CLAUSE_DEPEND_SINK)
33424 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33427 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33430 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33431 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33436 cp_parser_error (parser, "invalid depend kind");
33438 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33439 /*or_comma=*/false,
33440 /*consume_paren=*/true);
33445 map ( map-kind : variable-list )
33446 map ( variable-list )
33449 alloc | to | from | tofrom
33453 alloc | to | from | tofrom | release | delete
33455 map ( always [,] map-kind: variable-list ) */
33458 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33461 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33462 bool always = false;
33464 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33467 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33469 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33470 const char *p = IDENTIFIER_POINTER (id);
33472 if (strcmp ("always", p) == 0)
33475 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33477 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33478 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33480 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33484 cp_lexer_consume_token (parser->lexer);
33486 cp_lexer_consume_token (parser->lexer);
33491 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33492 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33494 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33495 const char *p = IDENTIFIER_POINTER (id);
33497 if (strcmp ("alloc", p) == 0)
33498 kind = GOMP_MAP_ALLOC;
33499 else if (strcmp ("to", p) == 0)
33500 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33501 else if (strcmp ("from", p) == 0)
33502 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33503 else if (strcmp ("tofrom", p) == 0)
33504 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33505 else if (strcmp ("release", p) == 0)
33506 kind = GOMP_MAP_RELEASE;
33509 cp_parser_error (parser, "invalid map kind");
33510 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33511 /*or_comma=*/false,
33512 /*consume_paren=*/true);
33515 cp_lexer_consume_token (parser->lexer);
33516 cp_lexer_consume_token (parser->lexer);
33518 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33519 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33521 kind = GOMP_MAP_DELETE;
33522 cp_lexer_consume_token (parser->lexer);
33523 cp_lexer_consume_token (parser->lexer);
33526 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33529 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33530 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33536 device ( expression ) */
33539 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33540 location_t location)
33544 matching_parens parens;
33545 if (!parens.require_open (parser))
33548 t = cp_parser_expression (parser);
33550 if (t == error_mark_node
33551 || !parens.require_close (parser))
33552 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33553 /*or_comma=*/false,
33554 /*consume_paren=*/true);
33556 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33557 "device", location);
33559 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33560 OMP_CLAUSE_DEVICE_ID (c) = t;
33561 OMP_CLAUSE_CHAIN (c) = list;
33567 dist_schedule ( static )
33568 dist_schedule ( static , expression ) */
33571 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33572 location_t location)
33576 matching_parens parens;
33577 if (!parens.require_open (parser))
33580 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33582 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33584 cp_lexer_consume_token (parser->lexer);
33586 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33588 cp_lexer_consume_token (parser->lexer);
33590 t = cp_parser_assignment_expression (parser);
33592 if (t == error_mark_node)
33594 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33596 if (!parens.require_close (parser))
33599 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33602 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33604 OMP_CLAUSE_CHAIN (c) = list;
33608 cp_parser_error (parser, "invalid dist_schedule kind");
33610 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33611 /*or_comma=*/false,
33612 /*consume_paren=*/true);
33617 proc_bind ( proc-bind-kind )
33620 master | close | spread */
33623 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33624 location_t location)
33627 enum omp_clause_proc_bind_kind kind;
33629 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33632 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33634 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33635 const char *p = IDENTIFIER_POINTER (id);
33637 if (strcmp ("master", p) == 0)
33638 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33639 else if (strcmp ("close", p) == 0)
33640 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33641 else if (strcmp ("spread", p) == 0)
33642 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33649 cp_lexer_consume_token (parser->lexer);
33650 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33653 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33654 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33656 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33657 OMP_CLAUSE_CHAIN (c) = list;
33661 cp_parser_error (parser, "invalid depend kind");
33663 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33664 /*or_comma=*/false,
33665 /*consume_paren=*/true);
33670 async [( int-expr )] */
33673 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33676 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33678 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33680 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33682 matching_parens parens;
33683 parens.consume_open (parser);
33685 t = cp_parser_expression (parser);
33686 if (t == error_mark_node
33687 || !parens.require_close (parser))
33688 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33689 /*or_comma=*/false,
33690 /*consume_paren=*/true);
33693 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33695 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33696 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33697 OMP_CLAUSE_CHAIN (c) = list;
33703 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33704 is a bitmask in MASK. Return the list of clauses found. */
33707 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33708 const char *where, cp_token *pragma_tok,
33709 bool finish_p = true)
33711 tree clauses = NULL;
33714 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33717 pragma_omp_clause c_kind;
33718 omp_clause_code code;
33719 const char *c_name;
33720 tree prev = clauses;
33722 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33723 cp_lexer_consume_token (parser->lexer);
33725 here = cp_lexer_peek_token (parser->lexer)->location;
33726 c_kind = cp_parser_omp_clause_name (parser);
33730 case PRAGMA_OACC_CLAUSE_ASYNC:
33731 clauses = cp_parser_oacc_clause_async (parser, clauses);
33734 case PRAGMA_OACC_CLAUSE_AUTO:
33735 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33739 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33740 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33741 c_name = "collapse";
33743 case PRAGMA_OACC_CLAUSE_COPY:
33744 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33747 case PRAGMA_OACC_CLAUSE_COPYIN:
33748 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33751 case PRAGMA_OACC_CLAUSE_COPYOUT:
33752 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33753 c_name = "copyout";
33755 case PRAGMA_OACC_CLAUSE_CREATE:
33756 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33759 case PRAGMA_OACC_CLAUSE_DELETE:
33760 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33763 case PRAGMA_OMP_CLAUSE_DEFAULT:
33764 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33765 c_name = "default";
33767 case PRAGMA_OACC_CLAUSE_DEVICE:
33768 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33771 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33772 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33773 c_name = "deviceptr";
33775 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33776 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33777 c_name = "device_resident";
33779 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33780 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33782 c_name = "firstprivate";
33784 case PRAGMA_OACC_CLAUSE_GANG:
33786 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33789 case PRAGMA_OACC_CLAUSE_HOST:
33790 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33793 case PRAGMA_OACC_CLAUSE_IF:
33794 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33797 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33798 clauses = cp_parser_oacc_simple_clause (parser,
33799 OMP_CLAUSE_INDEPENDENT,
33801 c_name = "independent";
33803 case PRAGMA_OACC_CLAUSE_LINK:
33804 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33807 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33808 code = OMP_CLAUSE_NUM_GANGS;
33809 c_name = "num_gangs";
33810 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33813 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33814 c_name = "num_workers";
33815 code = OMP_CLAUSE_NUM_WORKERS;
33816 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33819 case PRAGMA_OACC_CLAUSE_PRESENT:
33820 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33821 c_name = "present";
33823 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33824 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33825 c_name = "present_or_copy";
33827 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33828 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33829 c_name = "present_or_copyin";
33831 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33832 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33833 c_name = "present_or_copyout";
33835 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33836 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33837 c_name = "present_or_create";
33839 case PRAGMA_OACC_CLAUSE_PRIVATE:
33840 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33842 c_name = "private";
33844 case PRAGMA_OACC_CLAUSE_REDUCTION:
33845 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33846 c_name = "reduction";
33848 case PRAGMA_OACC_CLAUSE_SELF:
33849 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33852 case PRAGMA_OACC_CLAUSE_SEQ:
33853 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33857 case PRAGMA_OACC_CLAUSE_TILE:
33858 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33861 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33862 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33864 c_name = "use_device";
33866 case PRAGMA_OACC_CLAUSE_VECTOR:
33868 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33871 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33872 c_name = "vector_length";
33873 code = OMP_CLAUSE_VECTOR_LENGTH;
33874 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33877 case PRAGMA_OACC_CLAUSE_WAIT:
33878 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33881 case PRAGMA_OACC_CLAUSE_WORKER:
33883 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33887 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33893 if (((mask >> c_kind) & 1) == 0)
33895 /* Remove the invalid clause(s) from the list to avoid
33896 confusing the rest of the compiler. */
33898 error_at (here, "%qs is not valid for %qs", c_name, where);
33903 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33906 return finish_omp_clauses (clauses, C_ORT_ACC);
33911 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33912 is a bitmask in MASK. Return the list of clauses found; the result
33913 of clause default goes in *pdefault. */
33916 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33917 const char *where, cp_token *pragma_tok,
33918 bool finish_p = true)
33920 tree clauses = NULL;
33922 cp_token *token = NULL;
33924 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33926 pragma_omp_clause c_kind;
33927 const char *c_name;
33928 tree prev = clauses;
33930 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33931 cp_lexer_consume_token (parser->lexer);
33933 token = cp_lexer_peek_token (parser->lexer);
33934 c_kind = cp_parser_omp_clause_name (parser);
33938 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33939 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33941 c_name = "collapse";
33943 case PRAGMA_OMP_CLAUSE_COPYIN:
33944 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33947 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33948 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33950 c_name = "copyprivate";
33952 case PRAGMA_OMP_CLAUSE_DEFAULT:
33953 clauses = cp_parser_omp_clause_default (parser, clauses,
33954 token->location, false);
33955 c_name = "default";
33957 case PRAGMA_OMP_CLAUSE_FINAL:
33958 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33961 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33962 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33964 c_name = "firstprivate";
33966 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33967 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33969 c_name = "grainsize";
33971 case PRAGMA_OMP_CLAUSE_HINT:
33972 clauses = cp_parser_omp_clause_hint (parser, clauses,
33976 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33977 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33979 c_name = "defaultmap";
33981 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33982 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33984 c_name = "use_device_ptr";
33986 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33987 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33989 c_name = "is_device_ptr";
33991 case PRAGMA_OMP_CLAUSE_IF:
33992 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33996 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33997 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33999 c_name = "lastprivate";
34001 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34002 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34004 c_name = "mergeable";
34006 case PRAGMA_OMP_CLAUSE_NOWAIT:
34007 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34010 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34011 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34013 c_name = "num_tasks";
34015 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34016 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34018 c_name = "num_threads";
34020 case PRAGMA_OMP_CLAUSE_ORDERED:
34021 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34023 c_name = "ordered";
34025 case PRAGMA_OMP_CLAUSE_PRIORITY:
34026 clauses = cp_parser_omp_clause_priority (parser, clauses,
34028 c_name = "priority";
34030 case PRAGMA_OMP_CLAUSE_PRIVATE:
34031 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34033 c_name = "private";
34035 case PRAGMA_OMP_CLAUSE_REDUCTION:
34036 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34037 c_name = "reduction";
34039 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34040 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34042 c_name = "schedule";
34044 case PRAGMA_OMP_CLAUSE_SHARED:
34045 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34049 case PRAGMA_OMP_CLAUSE_UNTIED:
34050 clauses = cp_parser_omp_clause_untied (parser, clauses,
34054 case PRAGMA_OMP_CLAUSE_INBRANCH:
34055 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34056 clauses, token->location);
34057 c_name = "inbranch";
34059 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34060 clauses = cp_parser_omp_clause_branch (parser,
34061 OMP_CLAUSE_NOTINBRANCH,
34062 clauses, token->location);
34063 c_name = "notinbranch";
34065 case PRAGMA_OMP_CLAUSE_PARALLEL:
34066 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34067 clauses, token->location);
34068 c_name = "parallel";
34072 error_at (token->location, "%qs must be the first clause of %qs",
34077 case PRAGMA_OMP_CLAUSE_FOR:
34078 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34079 clauses, token->location);
34082 goto clause_not_first;
34084 case PRAGMA_OMP_CLAUSE_SECTIONS:
34085 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34086 clauses, token->location);
34087 c_name = "sections";
34089 goto clause_not_first;
34091 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34092 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34093 clauses, token->location);
34094 c_name = "taskgroup";
34096 goto clause_not_first;
34098 case PRAGMA_OMP_CLAUSE_LINK:
34099 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34102 case PRAGMA_OMP_CLAUSE_TO:
34103 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34104 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34107 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34110 case PRAGMA_OMP_CLAUSE_FROM:
34111 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34114 case PRAGMA_OMP_CLAUSE_UNIFORM:
34115 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34117 c_name = "uniform";
34119 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34120 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34122 c_name = "num_teams";
34124 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34125 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34127 c_name = "thread_limit";
34129 case PRAGMA_OMP_CLAUSE_ALIGNED:
34130 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34131 c_name = "aligned";
34133 case PRAGMA_OMP_CLAUSE_LINEAR:
34135 bool declare_simd = false;
34136 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34137 declare_simd = true;
34138 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34142 case PRAGMA_OMP_CLAUSE_DEPEND:
34143 clauses = cp_parser_omp_clause_depend (parser, clauses,
34147 case PRAGMA_OMP_CLAUSE_MAP:
34148 clauses = cp_parser_omp_clause_map (parser, clauses);
34151 case PRAGMA_OMP_CLAUSE_DEVICE:
34152 clauses = cp_parser_omp_clause_device (parser, clauses,
34156 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34157 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34159 c_name = "dist_schedule";
34161 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34162 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34164 c_name = "proc_bind";
34166 case PRAGMA_OMP_CLAUSE_SAFELEN:
34167 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34169 c_name = "safelen";
34171 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34172 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34174 c_name = "simdlen";
34176 case PRAGMA_OMP_CLAUSE_NOGROUP:
34177 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34179 c_name = "nogroup";
34181 case PRAGMA_OMP_CLAUSE_THREADS:
34183 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34184 clauses, token->location);
34185 c_name = "threads";
34187 case PRAGMA_OMP_CLAUSE_SIMD:
34189 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34190 clauses, token->location);
34194 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34200 if (((mask >> c_kind) & 1) == 0)
34202 /* Remove the invalid clause(s) from the list to avoid
34203 confusing the rest of the compiler. */
34205 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34209 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34212 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34213 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34215 return finish_omp_clauses (clauses, C_ORT_OMP);
34224 In practice, we're also interested in adding the statement to an
34225 outer node. So it is convenient if we work around the fact that
34226 cp_parser_statement calls add_stmt. */
34229 cp_parser_begin_omp_structured_block (cp_parser *parser)
34231 unsigned save = parser->in_statement;
34233 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34234 This preserves the "not within loop or switch" style error messages
34235 for nonsense cases like
34241 if (parser->in_statement)
34242 parser->in_statement = IN_OMP_BLOCK;
34248 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34250 parser->in_statement = save;
34254 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34256 tree stmt = begin_omp_structured_block ();
34257 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34259 cp_parser_statement (parser, NULL_TREE, false, if_p);
34261 cp_parser_end_omp_structured_block (parser, save);
34262 return finish_omp_structured_block (stmt);
34266 # pragma omp atomic new-line
34270 x binop= expr | x++ | ++x | x-- | --x
34272 +, *, -, /, &, ^, |, <<, >>
34274 where x is an lvalue expression with scalar type.
34277 # pragma omp atomic new-line
34280 # pragma omp atomic read new-line
34283 # pragma omp atomic write new-line
34286 # pragma omp atomic update new-line
34289 # pragma omp atomic capture new-line
34292 # pragma omp atomic capture new-line
34300 expression-stmt | x = x binop expr
34302 v = expression-stmt
34304 { v = x; update-stmt; } | { update-stmt; v = x; }
34308 expression-stmt | x = x binop expr | x = expr binop x
34312 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34314 where x and v are lvalue expressions with scalar type. */
34317 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34319 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34320 tree rhs1 = NULL_TREE, orig_lhs;
34321 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34322 bool structured_block = false;
34323 bool seq_cst = false;
34325 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34327 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34328 const char *p = IDENTIFIER_POINTER (id);
34330 if (!strcmp (p, "seq_cst"))
34333 cp_lexer_consume_token (parser->lexer);
34334 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34335 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34336 cp_lexer_consume_token (parser->lexer);
34339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34341 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34342 const char *p = IDENTIFIER_POINTER (id);
34344 if (!strcmp (p, "read"))
34345 code = OMP_ATOMIC_READ;
34346 else if (!strcmp (p, "write"))
34348 else if (!strcmp (p, "update"))
34350 else if (!strcmp (p, "capture"))
34351 code = OMP_ATOMIC_CAPTURE_NEW;
34355 cp_lexer_consume_token (parser->lexer);
34359 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34360 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34361 cp_lexer_consume_token (parser->lexer);
34363 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34365 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34366 const char *p = IDENTIFIER_POINTER (id);
34368 if (!strcmp (p, "seq_cst"))
34371 cp_lexer_consume_token (parser->lexer);
34375 cp_parser_require_pragma_eol (parser, pragma_tok);
34379 case OMP_ATOMIC_READ:
34380 case NOP_EXPR: /* atomic write */
34381 v = cp_parser_unary_expression (parser);
34382 if (v == error_mark_node)
34384 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34386 if (code == NOP_EXPR)
34387 lhs = cp_parser_expression (parser);
34389 lhs = cp_parser_unary_expression (parser);
34390 if (lhs == error_mark_node)
34392 if (code == NOP_EXPR)
34394 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34402 case OMP_ATOMIC_CAPTURE_NEW:
34403 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34405 cp_lexer_consume_token (parser->lexer);
34406 structured_block = true;
34410 v = cp_parser_unary_expression (parser);
34411 if (v == error_mark_node)
34413 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34421 lhs = cp_parser_unary_expression (parser);
34423 switch (TREE_CODE (lhs))
34428 case POSTINCREMENT_EXPR:
34429 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34430 code = OMP_ATOMIC_CAPTURE_OLD;
34432 case PREINCREMENT_EXPR:
34433 lhs = TREE_OPERAND (lhs, 0);
34434 opcode = PLUS_EXPR;
34435 rhs = integer_one_node;
34438 case POSTDECREMENT_EXPR:
34439 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34440 code = OMP_ATOMIC_CAPTURE_OLD;
34442 case PREDECREMENT_EXPR:
34443 lhs = TREE_OPERAND (lhs, 0);
34444 opcode = MINUS_EXPR;
34445 rhs = integer_one_node;
34448 case COMPOUND_EXPR:
34449 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34450 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34451 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34452 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34453 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34454 (TREE_OPERAND (lhs, 1), 0), 0)))
34456 /* Undo effects of boolean_increment for post {in,de}crement. */
34457 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34460 if (TREE_CODE (lhs) == MODIFY_EXPR
34461 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34463 /* Undo effects of boolean_increment. */
34464 if (integer_onep (TREE_OPERAND (lhs, 1)))
34466 /* This is pre or post increment. */
34467 rhs = TREE_OPERAND (lhs, 1);
34468 lhs = TREE_OPERAND (lhs, 0);
34470 if (code == OMP_ATOMIC_CAPTURE_NEW
34471 && !structured_block
34472 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34473 code = OMP_ATOMIC_CAPTURE_OLD;
34479 switch (cp_lexer_peek_token (parser->lexer)->type)
34482 opcode = MULT_EXPR;
34485 opcode = TRUNC_DIV_EXPR;
34488 opcode = PLUS_EXPR;
34491 opcode = MINUS_EXPR;
34493 case CPP_LSHIFT_EQ:
34494 opcode = LSHIFT_EXPR;
34496 case CPP_RSHIFT_EQ:
34497 opcode = RSHIFT_EXPR;
34500 opcode = BIT_AND_EXPR;
34503 opcode = BIT_IOR_EXPR;
34506 opcode = BIT_XOR_EXPR;
34509 enum cp_parser_prec oprec;
34511 cp_lexer_consume_token (parser->lexer);
34512 cp_parser_parse_tentatively (parser);
34513 rhs1 = cp_parser_simple_cast_expression (parser);
34514 if (rhs1 == error_mark_node)
34516 cp_parser_abort_tentative_parse (parser);
34517 cp_parser_simple_cast_expression (parser);
34520 token = cp_lexer_peek_token (parser->lexer);
34521 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34523 cp_parser_abort_tentative_parse (parser);
34524 cp_parser_parse_tentatively (parser);
34525 rhs = cp_parser_binary_expression (parser, false, true,
34526 PREC_NOT_OPERATOR, NULL);
34527 if (rhs == error_mark_node)
34529 cp_parser_abort_tentative_parse (parser);
34530 cp_parser_binary_expression (parser, false, true,
34531 PREC_NOT_OPERATOR, NULL);
34534 switch (TREE_CODE (rhs))
34537 case TRUNC_DIV_EXPR:
34546 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34548 if (cp_parser_parse_definitely (parser))
34550 opcode = TREE_CODE (rhs);
34551 rhs1 = TREE_OPERAND (rhs, 0);
34552 rhs = TREE_OPERAND (rhs, 1);
34562 cp_parser_abort_tentative_parse (parser);
34563 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34565 rhs = cp_parser_expression (parser);
34566 if (rhs == error_mark_node)
34572 cp_parser_error (parser,
34573 "invalid form of %<#pragma omp atomic%>");
34576 if (!cp_parser_parse_definitely (parser))
34578 switch (token->type)
34580 case CPP_SEMICOLON:
34581 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34583 code = OMP_ATOMIC_CAPTURE_OLD;
34588 cp_lexer_consume_token (parser->lexer);
34591 else if (structured_block)
34598 cp_parser_error (parser,
34599 "invalid form of %<#pragma omp atomic%>");
34602 opcode = MULT_EXPR;
34605 opcode = TRUNC_DIV_EXPR;
34608 opcode = PLUS_EXPR;
34611 opcode = MINUS_EXPR;
34614 opcode = LSHIFT_EXPR;
34617 opcode = RSHIFT_EXPR;
34620 opcode = BIT_AND_EXPR;
34623 opcode = BIT_IOR_EXPR;
34626 opcode = BIT_XOR_EXPR;
34629 cp_parser_error (parser,
34630 "invalid operator for %<#pragma omp atomic%>");
34633 oprec = TOKEN_PRECEDENCE (token);
34634 gcc_assert (oprec != PREC_NOT_OPERATOR);
34635 if (commutative_tree_code (opcode))
34636 oprec = (enum cp_parser_prec) (oprec - 1);
34637 cp_lexer_consume_token (parser->lexer);
34638 rhs = cp_parser_binary_expression (parser, false, false,
34640 if (rhs == error_mark_node)
34645 cp_parser_error (parser,
34646 "invalid operator for %<#pragma omp atomic%>");
34649 cp_lexer_consume_token (parser->lexer);
34651 rhs = cp_parser_expression (parser);
34652 if (rhs == error_mark_node)
34657 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34659 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34661 v = cp_parser_unary_expression (parser);
34662 if (v == error_mark_node)
34664 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34666 lhs1 = cp_parser_unary_expression (parser);
34667 if (lhs1 == error_mark_node)
34670 if (structured_block)
34672 cp_parser_consume_semicolon_at_end_of_statement (parser);
34673 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34676 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34677 if (!structured_block)
34678 cp_parser_consume_semicolon_at_end_of_statement (parser);
34682 cp_parser_skip_to_end_of_block_or_statement (parser);
34683 if (structured_block)
34685 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34686 cp_lexer_consume_token (parser->lexer);
34687 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34689 cp_parser_skip_to_end_of_block_or_statement (parser);
34690 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34691 cp_lexer_consume_token (parser->lexer);
34698 # pragma omp barrier new-line */
34701 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34703 cp_parser_require_pragma_eol (parser, pragma_tok);
34704 finish_omp_barrier ();
34708 # pragma omp critical [(name)] new-line
34712 # pragma omp critical [(name) [hint(expression)]] new-line
34713 structured-block */
34715 #define OMP_CRITICAL_CLAUSE_MASK \
34716 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34719 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34721 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34723 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34725 matching_parens parens;
34726 parens.consume_open (parser);
34728 name = cp_parser_identifier (parser);
34730 if (name == error_mark_node
34731 || !parens.require_close (parser))
34732 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34733 /*or_comma=*/false,
34734 /*consume_paren=*/true);
34735 if (name == error_mark_node)
34738 clauses = cp_parser_omp_all_clauses (parser,
34739 OMP_CRITICAL_CLAUSE_MASK,
34740 "#pragma omp critical", pragma_tok);
34743 cp_parser_require_pragma_eol (parser, pragma_tok);
34745 stmt = cp_parser_omp_structured_block (parser, if_p);
34746 return c_finish_omp_critical (input_location, stmt, name, clauses);
34750 # pragma omp flush flush-vars[opt] new-line
34753 ( variable-list ) */
34756 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34758 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34759 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34760 cp_parser_require_pragma_eol (parser, pragma_tok);
34762 finish_omp_flush ();
34765 /* Helper function, to parse omp for increment expression. */
34768 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34770 tree cond = cp_parser_binary_expression (parser, false, true,
34771 PREC_NOT_OPERATOR, NULL);
34772 if (cond == error_mark_node
34773 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34775 cp_parser_skip_to_end_of_statement (parser);
34776 return error_mark_node;
34779 switch (TREE_CODE (cond))
34787 /* Fall through: OpenMP disallows NE_EXPR. */
34788 gcc_fallthrough ();
34790 return error_mark_node;
34793 /* If decl is an iterator, preserve LHS and RHS of the relational
34794 expr until finish_omp_for. */
34796 && (type_dependent_expression_p (decl)
34797 || CLASS_TYPE_P (TREE_TYPE (decl))))
34800 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34802 TREE_OPERAND (cond, 0), ERROR_MARK,
34803 TREE_OPERAND (cond, 1), ERROR_MARK,
34804 /*overload=*/NULL, tf_warning_or_error);
34807 /* Helper function, to parse omp for increment expression. */
34810 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34812 cp_token *token = cp_lexer_peek_token (parser->lexer);
34818 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34820 op = (token->type == CPP_PLUS_PLUS
34821 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34822 cp_lexer_consume_token (parser->lexer);
34823 lhs = cp_parser_simple_cast_expression (parser);
34825 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34826 return error_mark_node;
34827 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34830 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34832 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34833 return error_mark_node;
34835 token = cp_lexer_peek_token (parser->lexer);
34836 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34838 op = (token->type == CPP_PLUS_PLUS
34839 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34840 cp_lexer_consume_token (parser->lexer);
34841 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34844 op = cp_parser_assignment_operator_opt (parser);
34845 if (op == ERROR_MARK)
34846 return error_mark_node;
34848 if (op != NOP_EXPR)
34850 rhs = cp_parser_assignment_expression (parser);
34851 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34852 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34855 lhs = cp_parser_binary_expression (parser, false, false,
34856 PREC_ADDITIVE_EXPRESSION, NULL);
34857 token = cp_lexer_peek_token (parser->lexer);
34858 decl_first = (lhs == decl
34859 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34862 if (token->type != CPP_PLUS
34863 && token->type != CPP_MINUS)
34864 return error_mark_node;
34868 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34869 cp_lexer_consume_token (parser->lexer);
34870 rhs = cp_parser_binary_expression (parser, false, false,
34871 PREC_ADDITIVE_EXPRESSION, NULL);
34872 token = cp_lexer_peek_token (parser->lexer);
34873 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34875 if (lhs == NULL_TREE)
34877 if (op == PLUS_EXPR)
34880 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34881 tf_warning_or_error);
34884 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34885 ERROR_MARK, NULL, tf_warning_or_error);
34888 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34893 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34894 || op == MINUS_EXPR)
34895 return error_mark_node;
34896 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34899 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34901 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34904 /* Parse the initialization statement of an OpenMP for loop.
34906 Return true if the resulting construct should have an
34907 OMP_CLAUSE_PRIVATE added to it. */
34910 cp_parser_omp_for_loop_init (cp_parser *parser,
34911 tree &this_pre_body,
34912 vec<tree, va_gc> *&for_block,
34918 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34921 tree add_private_clause = NULL_TREE;
34923 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34927 integer-type var = lb
34928 random-access-iterator-type var = lb
34929 pointer-type var = lb
34931 cp_decl_specifier_seq type_specifiers;
34933 /* First, try to parse as an initialized declaration. See
34934 cp_parser_condition, from whence the bulk of this is copied. */
34936 cp_parser_parse_tentatively (parser);
34937 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34938 /*is_trailing_return=*/false,
34940 if (cp_parser_parse_definitely (parser))
34942 /* If parsing a type specifier seq succeeded, then this
34943 MUST be a initialized declaration. */
34944 tree asm_specification, attributes;
34945 cp_declarator *declarator;
34947 declarator = cp_parser_declarator (parser,
34948 CP_PARSER_DECLARATOR_NAMED,
34949 /*ctor_dtor_or_conv_p=*/NULL,
34950 /*parenthesized_p=*/NULL,
34951 /*member_p=*/false,
34952 /*friend_p=*/false);
34953 attributes = cp_parser_attributes_opt (parser);
34954 asm_specification = cp_parser_asm_specification_opt (parser);
34956 if (declarator == cp_error_declarator)
34957 cp_parser_skip_to_end_of_statement (parser);
34961 tree pushed_scope, auto_node;
34963 decl = start_decl (declarator, &type_specifiers,
34964 SD_INITIALIZED, attributes,
34965 /*prefix_attributes=*/NULL_TREE,
34968 auto_node = type_uses_auto (TREE_TYPE (decl));
34969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34971 if (cp_lexer_next_token_is (parser->lexer,
34973 error ("parenthesized initialization is not allowed in "
34974 "OpenMP %<for%> loop");
34976 /* Trigger an error. */
34977 cp_parser_require (parser, CPP_EQ, RT_EQ);
34979 init = error_mark_node;
34980 cp_parser_skip_to_end_of_statement (parser);
34982 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34983 || type_dependent_expression_p (decl)
34986 bool is_direct_init, is_non_constant_init;
34988 init = cp_parser_initializer (parser,
34990 &is_non_constant_init);
34995 = do_auto_deduction (TREE_TYPE (decl), init,
34998 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34999 && !type_dependent_expression_p (decl))
35003 cp_finish_decl (decl, init, !is_non_constant_init,
35005 LOOKUP_ONLYCONVERTING);
35007 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35009 vec_safe_push (for_block, this_pre_body);
35014 init = pop_stmt_list (this_pre_body);
35015 if (init && TREE_CODE (init) == STATEMENT_LIST)
35017 tree_stmt_iterator i = tsi_start (init);
35018 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35019 while (!tsi_end_p (i))
35021 tree t = tsi_stmt (i);
35022 if (TREE_CODE (t) == DECL_EXPR
35023 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35026 vec_safe_push (for_block, t);
35031 if (tsi_one_before_end_p (i))
35033 tree t = tsi_stmt (i);
35035 free_stmt_list (init);
35040 this_pre_body = NULL_TREE;
35045 cp_lexer_consume_token (parser->lexer);
35046 init = cp_parser_assignment_expression (parser);
35049 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
35050 init = error_mark_node;
35052 cp_finish_decl (decl, NULL_TREE,
35053 /*init_const_expr_p=*/false,
35055 LOOKUP_ONLYCONVERTING);
35059 pop_scope (pushed_scope);
35065 /* If parsing a type specifier sequence failed, then
35066 this MUST be a simple expression. */
35067 cp_parser_parse_tentatively (parser);
35068 decl = cp_parser_primary_expression (parser, false, false,
35070 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35071 if (!cp_parser_error_occurred (parser)
35073 && (TREE_CODE (decl) == COMPONENT_REF
35074 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35076 cp_parser_abort_tentative_parse (parser);
35077 cp_parser_parse_tentatively (parser);
35078 cp_token *token = cp_lexer_peek_token (parser->lexer);
35079 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35080 /*check_dependency_p=*/true,
35081 /*template_p=*/NULL,
35082 /*declarator_p=*/false,
35083 /*optional_p=*/false);
35084 if (name != error_mark_node
35085 && last_tok == cp_lexer_peek_token (parser->lexer))
35087 decl = cp_parser_lookup_name_simple (parser, name,
35089 if (TREE_CODE (decl) == FIELD_DECL)
35090 add_private_clause = omp_privatize_field (decl, false);
35092 cp_parser_abort_tentative_parse (parser);
35093 cp_parser_parse_tentatively (parser);
35094 decl = cp_parser_primary_expression (parser, false, false,
35097 if (!cp_parser_error_occurred (parser)
35100 && CLASS_TYPE_P (TREE_TYPE (decl)))
35104 cp_parser_parse_definitely (parser);
35105 cp_parser_require (parser, CPP_EQ, RT_EQ);
35106 rhs = cp_parser_assignment_expression (parser);
35108 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35111 tf_warning_or_error));
35112 if (!add_private_clause)
35113 add_private_clause = decl;
35118 cp_parser_abort_tentative_parse (parser);
35119 init = cp_parser_expression (parser);
35122 if (TREE_CODE (init) == MODIFY_EXPR
35123 || TREE_CODE (init) == MODOP_EXPR)
35124 real_decl = TREE_OPERAND (init, 0);
35128 return add_private_clause;
35131 /* Parse the restricted form of the for statement allowed by OpenMP. */
35134 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35135 tree *cclauses, bool *if_p)
35137 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35138 tree real_decl, initv, condv, incrv, declv;
35139 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35140 location_t loc_first;
35141 bool collapse_err = false;
35142 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35143 vec<tree, va_gc> *for_block = make_tree_vector ();
35144 auto_vec<tree, 4> orig_inits;
35145 bool tiling = false;
35147 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35148 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35149 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35150 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35153 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35155 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35156 && OMP_CLAUSE_ORDERED_EXPR (cl))
35159 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35162 if (ordered && ordered < collapse)
35164 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35165 "%<ordered%> clause parameter is less than %<collapse%>");
35166 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35167 = build_int_cst (NULL_TREE, collapse);
35168 ordered = collapse;
35172 for (tree *pc = &clauses; *pc; )
35173 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35175 error_at (OMP_CLAUSE_LOCATION (*pc),
35176 "%<linear%> clause may not be specified together "
35177 "with %<ordered%> clause with a parameter");
35178 *pc = OMP_CLAUSE_CHAIN (*pc);
35181 pc = &OMP_CLAUSE_CHAIN (*pc);
35184 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35185 count = ordered ? ordered : collapse;
35187 declv = make_tree_vec (count);
35188 initv = make_tree_vec (count);
35189 condv = make_tree_vec (count);
35190 incrv = make_tree_vec (count);
35192 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35194 for (i = 0; i < count; i++)
35196 int bracecount = 0;
35197 tree add_private_clause = NULL_TREE;
35200 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35203 cp_parser_error (parser, "for statement expected");
35206 loc = cp_lexer_consume_token (parser->lexer)->location;
35208 matching_parens parens;
35209 if (!parens.require_open (parser))
35212 init = orig_init = decl = real_decl = NULL;
35213 this_pre_body = push_stmt_list ();
35216 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35217 init, orig_init, decl, real_decl);
35219 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35222 this_pre_body = pop_stmt_list (this_pre_body);
35226 pre_body = push_stmt_list ();
35228 add_stmt (this_pre_body);
35229 pre_body = pop_stmt_list (pre_body);
35232 pre_body = this_pre_body;
35237 if (cclauses != NULL
35238 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35239 && real_decl != NULL_TREE)
35242 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35243 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35244 && OMP_CLAUSE_DECL (*c) == real_decl)
35246 error_at (loc, "iteration variable %qD"
35247 " should not be firstprivate", real_decl);
35248 *c = OMP_CLAUSE_CHAIN (*c);
35250 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35251 && OMP_CLAUSE_DECL (*c) == real_decl)
35253 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35255 *c = OMP_CLAUSE_CHAIN (*c);
35256 if (code == OMP_SIMD)
35258 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35259 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35263 OMP_CLAUSE_CHAIN (l) = clauses;
35266 add_private_clause = NULL_TREE;
35270 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35271 && OMP_CLAUSE_DECL (*c) == real_decl)
35272 add_private_clause = NULL_TREE;
35273 c = &OMP_CLAUSE_CHAIN (*c);
35277 if (add_private_clause)
35280 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35282 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35283 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35284 && OMP_CLAUSE_DECL (c) == decl)
35286 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35287 && OMP_CLAUSE_DECL (c) == decl)
35288 error_at (loc, "iteration variable %qD "
35289 "should not be firstprivate",
35291 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35292 && OMP_CLAUSE_DECL (c) == decl)
35293 error_at (loc, "iteration variable %qD should not be reduction",
35298 if (code != OMP_SIMD)
35299 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35300 else if (collapse == 1)
35301 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35303 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35304 OMP_CLAUSE_DECL (c) = add_private_clause;
35305 c = finish_omp_clauses (c, C_ORT_OMP);
35308 OMP_CLAUSE_CHAIN (c) = clauses;
35310 /* For linear, signal that we need to fill up
35311 the so far unknown linear step. */
35312 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35313 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35320 cond = cp_parser_omp_for_cond (parser, decl);
35321 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35326 /* If decl is an iterator, preserve the operator on decl
35327 until finish_omp_for. */
35329 && ((processing_template_decl
35330 && (TREE_TYPE (real_decl) == NULL_TREE
35331 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35332 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35333 incr = cp_parser_omp_for_incr (parser, real_decl);
35335 incr = cp_parser_expression (parser);
35336 if (!EXPR_HAS_LOCATION (incr))
35337 protected_set_expr_location (incr, input_location);
35340 if (!parens.require_close (parser))
35341 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35342 /*or_comma=*/false,
35343 /*consume_paren=*/true);
35345 TREE_VEC_ELT (declv, i) = decl;
35346 TREE_VEC_ELT (initv, i) = init;
35347 TREE_VEC_ELT (condv, i) = cond;
35348 TREE_VEC_ELT (incrv, i) = incr;
35351 orig_inits.safe_grow_cleared (i + 1);
35352 orig_inits[i] = orig_init;
35355 if (i == count - 1)
35358 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35359 in between the collapsed for loops to be still considered perfectly
35360 nested. Hopefully the final version clarifies this.
35361 For now handle (multiple) {'s and empty statements. */
35362 cp_parser_parse_tentatively (parser);
35365 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35367 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35369 cp_lexer_consume_token (parser->lexer);
35372 else if (bracecount
35373 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35374 cp_lexer_consume_token (parser->lexer);
35377 loc = cp_lexer_peek_token (parser->lexer)->location;
35378 error_at (loc, "not enough for loops to collapse");
35379 collapse_err = true;
35380 cp_parser_abort_tentative_parse (parser);
35388 cp_parser_parse_definitely (parser);
35389 nbraces += bracecount;
35396 /* Note that we saved the original contents of this flag when we entered
35397 the structured block, and so we don't need to re-save it here. */
35398 parser->in_statement = IN_OMP_FOR;
35400 /* Note that the grammar doesn't call for a structured block here,
35401 though the loop as a whole is a structured block. */
35402 body = push_stmt_list ();
35403 cp_parser_statement (parser, NULL_TREE, false, if_p);
35404 body = pop_stmt_list (body);
35406 if (declv == NULL_TREE)
35409 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35410 body, pre_body, &orig_inits, clauses);
35414 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35416 cp_lexer_consume_token (parser->lexer);
35419 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35420 cp_lexer_consume_token (parser->lexer);
35425 error_at (cp_lexer_peek_token (parser->lexer)->location,
35426 "collapsed loops not perfectly nested");
35428 collapse_err = true;
35429 cp_parser_statement_seq_opt (parser, NULL);
35430 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35435 while (!for_block->is_empty ())
35437 tree t = for_block->pop ();
35438 if (TREE_CODE (t) == STATEMENT_LIST)
35439 add_stmt (pop_stmt_list (t));
35443 release_tree_vector (for_block);
35448 /* Helper function for OpenMP parsing, split clauses and call
35449 finish_omp_clauses on each of the set of clauses afterwards. */
35452 cp_omp_split_clauses (location_t loc, enum tree_code code,
35453 omp_clause_mask mask, tree clauses, tree *cclauses)
35456 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35457 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35459 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35463 #pragma omp simd simd-clause[optseq] new-line
35466 #define OMP_SIMD_CLAUSE_MASK \
35467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35477 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35478 char *p_name, omp_clause_mask mask, tree *cclauses,
35481 tree clauses, sb, ret;
35483 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35485 strcat (p_name, " simd");
35486 mask |= OMP_SIMD_CLAUSE_MASK;
35488 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35492 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35493 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35494 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35495 OMP_CLAUSE_ORDERED);
35496 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35498 error_at (OMP_CLAUSE_LOCATION (c),
35499 "%<ordered%> clause with parameter may not be specified "
35500 "on %qs construct", p_name);
35501 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35505 sb = begin_omp_structured_block ();
35506 save = cp_parser_begin_omp_structured_block (parser);
35508 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35510 cp_parser_end_omp_structured_block (parser, save);
35511 add_stmt (finish_omp_structured_block (sb));
35517 #pragma omp for for-clause[optseq] new-line
35521 #pragma omp for simd for-simd-clause[optseq] new-line
35524 #define OMP_FOR_CLAUSE_MASK \
35525 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35536 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35537 char *p_name, omp_clause_mask mask, tree *cclauses,
35540 tree clauses, sb, ret;
35542 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35544 strcat (p_name, " for");
35545 mask |= OMP_FOR_CLAUSE_MASK;
35546 /* parallel for{, simd} disallows nowait clause, but for
35547 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35548 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35549 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35550 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35551 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35552 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35554 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35556 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35557 const char *p = IDENTIFIER_POINTER (id);
35559 if (strcmp (p, "simd") == 0)
35561 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35562 if (cclauses == NULL)
35563 cclauses = cclauses_buf;
35565 cp_lexer_consume_token (parser->lexer);
35566 if (!flag_openmp) /* flag_openmp_simd */
35567 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35569 sb = begin_omp_structured_block ();
35570 save = cp_parser_begin_omp_structured_block (parser);
35571 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35573 cp_parser_end_omp_structured_block (parser, save);
35574 tree body = finish_omp_structured_block (sb);
35577 ret = make_node (OMP_FOR);
35578 TREE_TYPE (ret) = void_type_node;
35579 OMP_FOR_BODY (ret) = body;
35580 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35581 SET_EXPR_LOCATION (ret, loc);
35586 if (!flag_openmp) /* flag_openmp_simd */
35588 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35592 /* Composite distribute parallel for disallows linear clause. */
35593 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35594 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35596 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35600 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35601 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35604 sb = begin_omp_structured_block ();
35605 save = cp_parser_begin_omp_structured_block (parser);
35607 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35609 cp_parser_end_omp_structured_block (parser, save);
35610 add_stmt (finish_omp_structured_block (sb));
35616 # pragma omp master new-line
35617 structured-block */
35620 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35622 cp_parser_require_pragma_eol (parser, pragma_tok);
35623 return c_finish_omp_master (input_location,
35624 cp_parser_omp_structured_block (parser, if_p));
35628 # pragma omp ordered new-line
35632 # pragma omp ordered ordered-clauses new-line
35633 structured-block */
35635 #define OMP_ORDERED_CLAUSE_MASK \
35636 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35639 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35640 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35643 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35644 enum pragma_context context, bool *if_p)
35646 location_t loc = pragma_tok->location;
35648 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35650 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35651 const char *p = IDENTIFIER_POINTER (id);
35653 if (strcmp (p, "depend") == 0)
35655 if (!flag_openmp) /* flag_openmp_simd */
35657 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35660 if (context == pragma_stmt)
35662 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35663 "%<depend%> clause may only be used in compound "
35665 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35669 = cp_parser_omp_all_clauses (parser,
35670 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35671 "#pragma omp ordered", pragma_tok);
35672 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35678 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35679 "#pragma omp ordered", pragma_tok);
35681 if (!flag_openmp /* flag_openmp_simd */
35682 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35685 c_finish_omp_ordered (loc, clauses,
35686 cp_parser_omp_structured_block (parser, if_p));
35693 { section-sequence }
35696 section-directive[opt] structured-block
35697 section-sequence section-directive structured-block */
35700 cp_parser_omp_sections_scope (cp_parser *parser)
35702 tree stmt, substmt;
35703 bool error_suppress = false;
35706 matching_braces braces;
35707 if (!braces.require_open (parser))
35710 stmt = push_stmt_list ();
35712 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35713 != PRAGMA_OMP_SECTION)
35715 substmt = cp_parser_omp_structured_block (parser, NULL);
35716 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35717 add_stmt (substmt);
35722 tok = cp_lexer_peek_token (parser->lexer);
35723 if (tok->type == CPP_CLOSE_BRACE)
35725 if (tok->type == CPP_EOF)
35728 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35730 cp_lexer_consume_token (parser->lexer);
35731 cp_parser_require_pragma_eol (parser, tok);
35732 error_suppress = false;
35734 else if (!error_suppress)
35736 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35737 error_suppress = true;
35740 substmt = cp_parser_omp_structured_block (parser, NULL);
35741 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35742 add_stmt (substmt);
35744 braces.require_close (parser);
35746 substmt = pop_stmt_list (stmt);
35748 stmt = make_node (OMP_SECTIONS);
35749 TREE_TYPE (stmt) = void_type_node;
35750 OMP_SECTIONS_BODY (stmt) = substmt;
35757 # pragma omp sections sections-clause[optseq] newline
35760 #define OMP_SECTIONS_CLAUSE_MASK \
35761 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35768 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35769 char *p_name, omp_clause_mask mask, tree *cclauses)
35772 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35774 strcat (p_name, " sections");
35775 mask |= OMP_SECTIONS_CLAUSE_MASK;
35777 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35779 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35783 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35784 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35787 ret = cp_parser_omp_sections_scope (parser);
35789 OMP_SECTIONS_CLAUSES (ret) = clauses;
35795 # pragma omp parallel parallel-clause[optseq] new-line
35797 # pragma omp parallel for parallel-for-clause[optseq] new-line
35799 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35803 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35804 structured-block */
35806 #define OMP_PARALLEL_CLAUSE_MASK \
35807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35818 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35819 char *p_name, omp_clause_mask mask, tree *cclauses,
35822 tree stmt, clauses, block;
35824 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35826 strcat (p_name, " parallel");
35827 mask |= OMP_PARALLEL_CLAUSE_MASK;
35828 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35829 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35830 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35831 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35833 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35835 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35836 if (cclauses == NULL)
35837 cclauses = cclauses_buf;
35839 cp_lexer_consume_token (parser->lexer);
35840 if (!flag_openmp) /* flag_openmp_simd */
35841 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35843 block = begin_omp_parallel ();
35844 save = cp_parser_begin_omp_structured_block (parser);
35845 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35847 cp_parser_end_omp_structured_block (parser, save);
35848 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35850 if (ret == NULL_TREE)
35852 OMP_PARALLEL_COMBINED (stmt) = 1;
35855 /* When combined with distribute, parallel has to be followed by for.
35856 #pragma omp target parallel is allowed though. */
35858 && (mask & (OMP_CLAUSE_MASK_1
35859 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35861 error_at (loc, "expected %<for%> after %qs", p_name);
35862 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35865 else if (!flag_openmp) /* flag_openmp_simd */
35867 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35870 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35872 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35873 const char *p = IDENTIFIER_POINTER (id);
35874 if (strcmp (p, "sections") == 0)
35876 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35877 cclauses = cclauses_buf;
35879 cp_lexer_consume_token (parser->lexer);
35880 block = begin_omp_parallel ();
35881 save = cp_parser_begin_omp_structured_block (parser);
35882 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35883 cp_parser_end_omp_structured_block (parser, save);
35884 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35886 OMP_PARALLEL_COMBINED (stmt) = 1;
35891 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35895 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35896 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35899 block = begin_omp_parallel ();
35900 save = cp_parser_begin_omp_structured_block (parser);
35901 cp_parser_statement (parser, NULL_TREE, false, if_p);
35902 cp_parser_end_omp_structured_block (parser, save);
35903 stmt = finish_omp_parallel (clauses, block);
35908 # pragma omp single single-clause[optseq] new-line
35909 structured-block */
35911 #define OMP_SINGLE_CLAUSE_MASK \
35912 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35918 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35920 tree stmt = make_node (OMP_SINGLE);
35921 TREE_TYPE (stmt) = void_type_node;
35923 OMP_SINGLE_CLAUSES (stmt)
35924 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35925 "#pragma omp single", pragma_tok);
35926 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35928 return add_stmt (stmt);
35932 # pragma omp task task-clause[optseq] new-line
35933 structured-block */
35935 #define OMP_TASK_CLAUSE_MASK \
35936 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35948 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35950 tree clauses, block;
35953 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35954 "#pragma omp task", pragma_tok);
35955 block = begin_omp_task ();
35956 save = cp_parser_begin_omp_structured_block (parser);
35957 cp_parser_statement (parser, NULL_TREE, false, if_p);
35958 cp_parser_end_omp_structured_block (parser, save);
35959 return finish_omp_task (clauses, block);
35963 # pragma omp taskwait new-line */
35966 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35968 cp_parser_require_pragma_eol (parser, pragma_tok);
35969 finish_omp_taskwait ();
35973 # pragma omp taskyield new-line */
35976 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35978 cp_parser_require_pragma_eol (parser, pragma_tok);
35979 finish_omp_taskyield ();
35983 # pragma omp taskgroup new-line
35984 structured-block */
35987 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35989 cp_parser_require_pragma_eol (parser, pragma_tok);
35990 return c_finish_omp_taskgroup (input_location,
35991 cp_parser_omp_structured_block (parser,
35997 # pragma omp threadprivate (variable-list) */
36000 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36004 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36005 cp_parser_require_pragma_eol (parser, pragma_tok);
36007 finish_omp_threadprivate (vars);
36011 # pragma omp cancel cancel-clause[optseq] new-line */
36013 #define OMP_CANCEL_CLAUSE_MASK \
36014 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36021 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36023 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36024 "#pragma omp cancel", pragma_tok);
36025 finish_omp_cancel (clauses);
36029 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36031 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36032 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36038 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36039 enum pragma_context context)
36042 bool point_seen = false;
36044 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36046 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36047 const char *p = IDENTIFIER_POINTER (id);
36049 if (strcmp (p, "point") == 0)
36051 cp_lexer_consume_token (parser->lexer);
36057 cp_parser_error (parser, "expected %<point%>");
36058 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36062 if (context != pragma_compound)
36064 if (context == pragma_stmt)
36065 error_at (pragma_tok->location,
36066 "%<#pragma %s%> may only be used in compound statements",
36067 "omp cancellation point");
36069 cp_parser_error (parser, "expected declaration specifiers");
36070 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36074 clauses = cp_parser_omp_all_clauses (parser,
36075 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36076 "#pragma omp cancellation point",
36078 finish_omp_cancellation_point (clauses);
36082 #pragma omp distribute distribute-clause[optseq] new-line
36085 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36086 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36093 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36094 char *p_name, omp_clause_mask mask, tree *cclauses,
36097 tree clauses, sb, ret;
36099 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36101 strcat (p_name, " distribute");
36102 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36104 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36106 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36107 const char *p = IDENTIFIER_POINTER (id);
36109 bool parallel = false;
36111 if (strcmp (p, "simd") == 0)
36114 parallel = strcmp (p, "parallel") == 0;
36115 if (parallel || simd)
36117 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36118 if (cclauses == NULL)
36119 cclauses = cclauses_buf;
36120 cp_lexer_consume_token (parser->lexer);
36121 if (!flag_openmp) /* flag_openmp_simd */
36124 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36127 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36130 sb = begin_omp_structured_block ();
36131 save = cp_parser_begin_omp_structured_block (parser);
36133 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36136 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36138 cp_parser_end_omp_structured_block (parser, save);
36139 tree body = finish_omp_structured_block (sb);
36142 ret = make_node (OMP_DISTRIBUTE);
36143 TREE_TYPE (ret) = void_type_node;
36144 OMP_FOR_BODY (ret) = body;
36145 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36146 SET_EXPR_LOCATION (ret, loc);
36151 if (!flag_openmp) /* flag_openmp_simd */
36153 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36157 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36161 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36162 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36165 sb = begin_omp_structured_block ();
36166 save = cp_parser_begin_omp_structured_block (parser);
36168 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36170 cp_parser_end_omp_structured_block (parser, save);
36171 add_stmt (finish_omp_structured_block (sb));
36177 # pragma omp teams teams-clause[optseq] new-line
36178 structured-block */
36180 #define OMP_TEAMS_CLAUSE_MASK \
36181 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36190 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36191 char *p_name, omp_clause_mask mask, tree *cclauses,
36194 tree clauses, sb, ret;
36196 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36198 strcat (p_name, " teams");
36199 mask |= OMP_TEAMS_CLAUSE_MASK;
36201 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36203 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36204 const char *p = IDENTIFIER_POINTER (id);
36205 if (strcmp (p, "distribute") == 0)
36207 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36208 if (cclauses == NULL)
36209 cclauses = cclauses_buf;
36211 cp_lexer_consume_token (parser->lexer);
36212 if (!flag_openmp) /* flag_openmp_simd */
36213 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36215 sb = begin_omp_structured_block ();
36216 save = cp_parser_begin_omp_structured_block (parser);
36217 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36219 cp_parser_end_omp_structured_block (parser, save);
36220 tree body = finish_omp_structured_block (sb);
36223 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36224 ret = make_node (OMP_TEAMS);
36225 TREE_TYPE (ret) = void_type_node;
36226 OMP_TEAMS_CLAUSES (ret) = clauses;
36227 OMP_TEAMS_BODY (ret) = body;
36228 OMP_TEAMS_COMBINED (ret) = 1;
36229 SET_EXPR_LOCATION (ret, loc);
36230 return add_stmt (ret);
36233 if (!flag_openmp) /* flag_openmp_simd */
36235 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36239 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36243 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36244 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36247 tree stmt = make_node (OMP_TEAMS);
36248 TREE_TYPE (stmt) = void_type_node;
36249 OMP_TEAMS_CLAUSES (stmt) = clauses;
36250 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36251 SET_EXPR_LOCATION (stmt, loc);
36253 return add_stmt (stmt);
36257 # pragma omp target data target-data-clause[optseq] new-line
36258 structured-block */
36260 #define OMP_TARGET_DATA_CLAUSE_MASK \
36261 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36267 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36270 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36271 "#pragma omp target data", pragma_tok);
36273 for (tree *pc = &clauses; *pc;)
36275 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36276 switch (OMP_CLAUSE_MAP_KIND (*pc))
36279 case GOMP_MAP_ALWAYS_TO:
36280 case GOMP_MAP_FROM:
36281 case GOMP_MAP_ALWAYS_FROM:
36282 case GOMP_MAP_TOFROM:
36283 case GOMP_MAP_ALWAYS_TOFROM:
36284 case GOMP_MAP_ALLOC:
36287 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36288 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36289 case GOMP_MAP_ALWAYS_POINTER:
36293 error_at (OMP_CLAUSE_LOCATION (*pc),
36294 "%<#pragma omp target data%> with map-type other "
36295 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36296 "on %<map%> clause");
36297 *pc = OMP_CLAUSE_CHAIN (*pc);
36300 pc = &OMP_CLAUSE_CHAIN (*pc);
36306 error_at (pragma_tok->location,
36307 "%<#pragma omp target data%> must contain at least "
36308 "one %<map%> clause");
36312 tree stmt = make_node (OMP_TARGET_DATA);
36313 TREE_TYPE (stmt) = void_type_node;
36314 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36316 keep_next_level (true);
36317 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36319 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36320 return add_stmt (stmt);
36324 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36325 structured-block */
36327 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36328 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36335 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36336 enum pragma_context context)
36338 bool data_seen = false;
36339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36341 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36342 const char *p = IDENTIFIER_POINTER (id);
36344 if (strcmp (p, "data") == 0)
36346 cp_lexer_consume_token (parser->lexer);
36352 cp_parser_error (parser, "expected %<data%>");
36353 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36357 if (context == pragma_stmt)
36359 error_at (pragma_tok->location,
36360 "%<#pragma %s%> may only be used in compound statements",
36361 "omp target enter data");
36362 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36367 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36368 "#pragma omp target enter data", pragma_tok);
36370 for (tree *pc = &clauses; *pc;)
36372 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36373 switch (OMP_CLAUSE_MAP_KIND (*pc))
36376 case GOMP_MAP_ALWAYS_TO:
36377 case GOMP_MAP_ALLOC:
36380 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36381 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36382 case GOMP_MAP_ALWAYS_POINTER:
36386 error_at (OMP_CLAUSE_LOCATION (*pc),
36387 "%<#pragma omp target enter data%> with map-type other "
36388 "than %<to%> or %<alloc%> on %<map%> clause");
36389 *pc = OMP_CLAUSE_CHAIN (*pc);
36392 pc = &OMP_CLAUSE_CHAIN (*pc);
36398 error_at (pragma_tok->location,
36399 "%<#pragma omp target enter data%> must contain at least "
36400 "one %<map%> clause");
36404 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36405 TREE_TYPE (stmt) = void_type_node;
36406 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36407 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36408 return add_stmt (stmt);
36412 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36413 structured-block */
36415 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36416 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36423 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36424 enum pragma_context context)
36426 bool data_seen = false;
36427 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36429 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36430 const char *p = IDENTIFIER_POINTER (id);
36432 if (strcmp (p, "data") == 0)
36434 cp_lexer_consume_token (parser->lexer);
36440 cp_parser_error (parser, "expected %<data%>");
36441 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36445 if (context == pragma_stmt)
36447 error_at (pragma_tok->location,
36448 "%<#pragma %s%> may only be used in compound statements",
36449 "omp target exit data");
36450 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36455 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36456 "#pragma omp target exit data", pragma_tok);
36458 for (tree *pc = &clauses; *pc;)
36460 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36461 switch (OMP_CLAUSE_MAP_KIND (*pc))
36463 case GOMP_MAP_FROM:
36464 case GOMP_MAP_ALWAYS_FROM:
36465 case GOMP_MAP_RELEASE:
36466 case GOMP_MAP_DELETE:
36469 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36470 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36471 case GOMP_MAP_ALWAYS_POINTER:
36475 error_at (OMP_CLAUSE_LOCATION (*pc),
36476 "%<#pragma omp target exit data%> with map-type other "
36477 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36479 *pc = OMP_CLAUSE_CHAIN (*pc);
36482 pc = &OMP_CLAUSE_CHAIN (*pc);
36488 error_at (pragma_tok->location,
36489 "%<#pragma omp target exit data%> must contain at least "
36490 "one %<map%> clause");
36494 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36495 TREE_TYPE (stmt) = void_type_node;
36496 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36497 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36498 return add_stmt (stmt);
36502 # pragma omp target update target-update-clause[optseq] new-line */
36504 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36505 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36513 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36514 enum pragma_context context)
36516 if (context == pragma_stmt)
36518 error_at (pragma_tok->location,
36519 "%<#pragma %s%> may only be used in compound statements",
36520 "omp target update");
36521 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36526 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36527 "#pragma omp target update", pragma_tok);
36528 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36529 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36531 error_at (pragma_tok->location,
36532 "%<#pragma omp target update%> must contain at least one "
36533 "%<from%> or %<to%> clauses");
36537 tree stmt = make_node (OMP_TARGET_UPDATE);
36538 TREE_TYPE (stmt) = void_type_node;
36539 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36540 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36546 # pragma omp target target-clause[optseq] new-line
36547 structured-block */
36549 #define OMP_TARGET_CLAUSE_MASK \
36550 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36561 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36562 enum pragma_context context, bool *if_p)
36564 tree *pc = NULL, stmt;
36566 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36568 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36569 const char *p = IDENTIFIER_POINTER (id);
36570 enum tree_code ccode = ERROR_MARK;
36572 if (strcmp (p, "teams") == 0)
36574 else if (strcmp (p, "parallel") == 0)
36575 ccode = OMP_PARALLEL;
36576 else if (strcmp (p, "simd") == 0)
36578 if (ccode != ERROR_MARK)
36580 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36581 char p_name[sizeof ("#pragma omp target teams distribute "
36582 "parallel for simd")];
36584 cp_lexer_consume_token (parser->lexer);
36585 strcpy (p_name, "#pragma omp target");
36586 if (!flag_openmp) /* flag_openmp_simd */
36592 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36593 OMP_TARGET_CLAUSE_MASK,
36597 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36598 OMP_TARGET_CLAUSE_MASK,
36602 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36603 OMP_TARGET_CLAUSE_MASK,
36607 gcc_unreachable ();
36609 return stmt != NULL_TREE;
36611 keep_next_level (true);
36612 tree sb = begin_omp_structured_block (), ret;
36613 unsigned save = cp_parser_begin_omp_structured_block (parser);
36617 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36618 OMP_TARGET_CLAUSE_MASK, cclauses,
36622 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36623 OMP_TARGET_CLAUSE_MASK, cclauses,
36627 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36628 OMP_TARGET_CLAUSE_MASK, cclauses,
36632 gcc_unreachable ();
36634 cp_parser_end_omp_structured_block (parser, save);
36635 tree body = finish_omp_structured_block (sb);
36636 if (ret == NULL_TREE)
36638 if (ccode == OMP_TEAMS && !processing_template_decl)
36640 /* For combined target teams, ensure the num_teams and
36641 thread_limit clause expressions are evaluated on the host,
36642 before entering the target construct. */
36644 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36645 c; c = OMP_CLAUSE_CHAIN (c))
36646 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36647 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36648 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36650 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36651 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36652 if (expr == error_mark_node)
36654 tree tmp = TARGET_EXPR_SLOT (expr);
36656 OMP_CLAUSE_OPERAND (c, 0) = expr;
36657 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36658 OMP_CLAUSE_FIRSTPRIVATE);
36659 OMP_CLAUSE_DECL (tc) = tmp;
36660 OMP_CLAUSE_CHAIN (tc)
36661 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36662 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36665 tree stmt = make_node (OMP_TARGET);
36666 TREE_TYPE (stmt) = void_type_node;
36667 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36668 OMP_TARGET_BODY (stmt) = body;
36669 OMP_TARGET_COMBINED (stmt) = 1;
36670 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36672 pc = &OMP_TARGET_CLAUSES (stmt);
36673 goto check_clauses;
36675 else if (!flag_openmp) /* flag_openmp_simd */
36677 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36680 else if (strcmp (p, "data") == 0)
36682 cp_lexer_consume_token (parser->lexer);
36683 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36686 else if (strcmp (p, "enter") == 0)
36688 cp_lexer_consume_token (parser->lexer);
36689 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36692 else if (strcmp (p, "exit") == 0)
36694 cp_lexer_consume_token (parser->lexer);
36695 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36698 else if (strcmp (p, "update") == 0)
36700 cp_lexer_consume_token (parser->lexer);
36701 return cp_parser_omp_target_update (parser, pragma_tok, context);
36704 if (!flag_openmp) /* flag_openmp_simd */
36706 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36710 stmt = make_node (OMP_TARGET);
36711 TREE_TYPE (stmt) = void_type_node;
36713 OMP_TARGET_CLAUSES (stmt)
36714 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36715 "#pragma omp target", pragma_tok);
36716 pc = &OMP_TARGET_CLAUSES (stmt);
36717 keep_next_level (true);
36718 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36720 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36726 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36727 switch (OMP_CLAUSE_MAP_KIND (*pc))
36730 case GOMP_MAP_ALWAYS_TO:
36731 case GOMP_MAP_FROM:
36732 case GOMP_MAP_ALWAYS_FROM:
36733 case GOMP_MAP_TOFROM:
36734 case GOMP_MAP_ALWAYS_TOFROM:
36735 case GOMP_MAP_ALLOC:
36736 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36737 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36738 case GOMP_MAP_ALWAYS_POINTER:
36741 error_at (OMP_CLAUSE_LOCATION (*pc),
36742 "%<#pragma omp target%> with map-type other "
36743 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36744 "on %<map%> clause");
36745 *pc = OMP_CLAUSE_CHAIN (*pc);
36748 pc = &OMP_CLAUSE_CHAIN (*pc);
36754 # pragma acc cache (variable-list) new-line
36758 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36760 tree stmt, clauses;
36762 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36763 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36765 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36767 stmt = make_node (OACC_CACHE);
36768 TREE_TYPE (stmt) = void_type_node;
36769 OACC_CACHE_CLAUSES (stmt) = clauses;
36770 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36777 # pragma acc data oacc-data-clause[optseq] new-line
36778 structured-block */
36780 #define OACC_DATA_CLAUSE_MASK \
36781 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36794 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36796 tree stmt, clauses, block;
36799 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36800 "#pragma acc data", pragma_tok);
36802 block = begin_omp_parallel ();
36803 save = cp_parser_begin_omp_structured_block (parser);
36804 cp_parser_statement (parser, NULL_TREE, false, if_p);
36805 cp_parser_end_omp_structured_block (parser, save);
36806 stmt = finish_oacc_data (clauses, block);
36811 # pragma acc host_data <clauses> new-line
36812 structured-block */
36814 #define OACC_HOST_DATA_CLAUSE_MASK \
36815 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36818 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36820 tree stmt, clauses, block;
36823 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36824 "#pragma acc host_data", pragma_tok);
36826 block = begin_omp_parallel ();
36827 save = cp_parser_begin_omp_structured_block (parser);
36828 cp_parser_statement (parser, NULL_TREE, false, if_p);
36829 cp_parser_end_omp_structured_block (parser, save);
36830 stmt = finish_oacc_host_data (clauses, block);
36835 # pragma acc declare oacc-data-clause[optseq] new-line
36838 #define OACC_DECLARE_CLAUSE_MASK \
36839 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36853 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36855 tree clauses, stmt;
36856 bool error = false;
36858 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36859 "#pragma acc declare", pragma_tok, true);
36862 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36864 error_at (pragma_tok->location,
36865 "no valid clauses specified in %<#pragma acc declare%>");
36869 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36871 location_t loc = OMP_CLAUSE_LOCATION (t);
36872 tree decl = OMP_CLAUSE_DECL (t);
36873 if (!DECL_P (decl))
36875 error_at (loc, "array section in %<#pragma acc declare%>");
36879 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36880 switch (OMP_CLAUSE_MAP_KIND (t))
36882 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36883 case GOMP_MAP_FORCE_ALLOC:
36884 case GOMP_MAP_FORCE_TO:
36885 case GOMP_MAP_FORCE_DEVICEPTR:
36886 case GOMP_MAP_DEVICE_RESIDENT:
36889 case GOMP_MAP_LINK:
36890 if (!global_bindings_p ()
36891 && (TREE_STATIC (decl)
36892 || !DECL_EXTERNAL (decl)))
36895 "%qD must be a global variable in "
36896 "%<#pragma acc declare link%>",
36904 if (global_bindings_p ())
36906 error_at (loc, "invalid OpenACC clause at file scope");
36910 if (DECL_EXTERNAL (decl))
36913 "invalid use of %<extern%> variable %qD "
36914 "in %<#pragma acc declare%>", decl);
36918 else if (TREE_PUBLIC (decl))
36921 "invalid use of %<global%> variable %qD "
36922 "in %<#pragma acc declare%>", decl);
36929 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36930 || lookup_attribute ("omp declare target link",
36931 DECL_ATTRIBUTES (decl)))
36933 error_at (loc, "variable %qD used more than once with "
36934 "%<#pragma acc declare%>", decl);
36943 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36944 id = get_identifier ("omp declare target link");
36946 id = get_identifier ("omp declare target");
36948 DECL_ATTRIBUTES (decl)
36949 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36950 if (global_bindings_p ())
36952 symtab_node *node = symtab_node::get (decl);
36955 node->offloadable = 1;
36956 if (ENABLE_OFFLOADING)
36958 g->have_offload = true;
36959 if (is_a <varpool_node *> (node))
36960 vec_safe_push (offload_vars, decl);
36967 if (error || global_bindings_p ())
36970 stmt = make_node (OACC_DECLARE);
36971 TREE_TYPE (stmt) = void_type_node;
36972 OACC_DECLARE_CLAUSES (stmt) = clauses;
36973 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36981 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36985 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36987 LOC is the location of the #pragma token.
36990 #define OACC_ENTER_DATA_CLAUSE_MASK \
36991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36999 #define OACC_EXIT_DATA_CLAUSE_MASK \
37000 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37007 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37010 location_t loc = pragma_tok->location;
37011 tree stmt, clauses;
37012 const char *p = "";
37014 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37015 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37017 if (strcmp (p, "data") != 0)
37019 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37020 enter ? "enter" : "exit");
37021 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37025 cp_lexer_consume_token (parser->lexer);
37028 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37029 "#pragma acc enter data", pragma_tok);
37031 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37032 "#pragma acc exit data", pragma_tok);
37034 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37036 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37037 enter ? "enter" : "exit");
37041 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37042 TREE_TYPE (stmt) = void_type_node;
37043 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37044 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37050 # pragma acc loop oacc-loop-clause[optseq] new-line
37051 structured-block */
37053 #define OACC_LOOP_CLAUSE_MASK \
37054 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37066 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37067 omp_clause_mask mask, tree *cclauses, bool *if_p)
37069 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37071 strcat (p_name, " loop");
37072 mask |= OACC_LOOP_CLAUSE_MASK;
37074 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37078 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37080 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37082 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37085 tree block = begin_omp_structured_block ();
37086 int save = cp_parser_begin_omp_structured_block (parser);
37087 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37088 cp_parser_end_omp_structured_block (parser, save);
37089 add_stmt (finish_omp_structured_block (block));
37095 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37100 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37104 #define OACC_KERNELS_CLAUSE_MASK \
37105 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37123 #define OACC_PARALLEL_CLAUSE_MASK \
37124 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37146 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37147 char *p_name, bool *if_p)
37149 omp_clause_mask mask;
37150 enum tree_code code;
37151 switch (cp_parser_pragma_kind (pragma_tok))
37153 case PRAGMA_OACC_KERNELS:
37154 strcat (p_name, " kernels");
37155 mask = OACC_KERNELS_CLAUSE_MASK;
37156 code = OACC_KERNELS;
37158 case PRAGMA_OACC_PARALLEL:
37159 strcat (p_name, " parallel");
37160 mask = OACC_PARALLEL_CLAUSE_MASK;
37161 code = OACC_PARALLEL;
37164 gcc_unreachable ();
37167 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37170 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37171 if (strcmp (p, "loop") == 0)
37173 cp_lexer_consume_token (parser->lexer);
37174 tree block = begin_omp_parallel ();
37176 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37178 return finish_omp_construct (code, block, clauses);
37182 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37184 tree block = begin_omp_parallel ();
37185 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37186 cp_parser_statement (parser, NULL_TREE, false, if_p);
37187 cp_parser_end_omp_structured_block (parser, save);
37188 return finish_omp_construct (code, block, clauses);
37192 # pragma acc update oacc-update-clause[optseq] new-line
37195 #define OACC_UPDATE_CLAUSE_MASK \
37196 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37204 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37206 tree stmt, clauses;
37208 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37209 "#pragma acc update", pragma_tok);
37211 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37213 error_at (pragma_tok->location,
37214 "%<#pragma acc update%> must contain at least one "
37215 "%<device%> or %<host%> or %<self%> clause");
37219 stmt = make_node (OACC_UPDATE);
37220 TREE_TYPE (stmt) = void_type_node;
37221 OACC_UPDATE_CLAUSES (stmt) = clauses;
37222 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37228 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37230 LOC is the location of the #pragma token.
37233 #define OACC_WAIT_CLAUSE_MASK \
37234 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37237 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37239 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37242 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37243 list = cp_parser_oacc_wait_list (parser, loc, list);
37245 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37246 "#pragma acc wait", pragma_tok);
37248 stmt = c_finish_oacc_wait (loc, list, clauses);
37249 stmt = finish_expr_stmt (stmt);
37255 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37257 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37258 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37266 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37267 enum pragma_context context)
37269 bool first_p = parser->omp_declare_simd == NULL;
37270 cp_omp_declare_simd_data data;
37273 data.error_seen = false;
37274 data.fndecl_seen = false;
37275 data.tokens = vNULL;
37276 data.clauses = NULL_TREE;
37277 /* It is safe to take the address of a local variable; it will only be
37278 used while this scope is live. */
37279 parser->omp_declare_simd = &data;
37282 /* Store away all pragma tokens. */
37283 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37284 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37285 cp_lexer_consume_token (parser->lexer);
37286 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37287 parser->omp_declare_simd->error_seen = true;
37288 cp_parser_require_pragma_eol (parser, pragma_tok);
37289 struct cp_token_cache *cp
37290 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37291 parser->omp_declare_simd->tokens.safe_push (cp);
37295 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37296 cp_parser_pragma (parser, context, NULL);
37299 case pragma_external:
37300 cp_parser_declaration (parser);
37302 case pragma_member:
37303 cp_parser_member_declaration (parser);
37305 case pragma_objc_icode:
37306 cp_parser_block_declaration (parser, /*statement_p=*/false);
37309 cp_parser_declaration_statement (parser);
37312 if (parser->omp_declare_simd
37313 && !parser->omp_declare_simd->error_seen
37314 && !parser->omp_declare_simd->fndecl_seen)
37315 error_at (pragma_tok->location,
37316 "%<#pragma omp declare simd%> not immediately followed by "
37317 "function declaration or definition");
37318 data.tokens.release ();
37319 parser->omp_declare_simd = NULL;
37323 /* Finalize #pragma omp declare simd clauses after direct declarator has
37324 been parsed, and put that into "omp declare simd" attribute. */
37327 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37329 struct cp_token_cache *ce;
37330 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37333 if (!data->error_seen && data->fndecl_seen)
37335 error ("%<#pragma omp declare simd%> not immediately followed by "
37336 "a single function declaration or definition");
37337 data->error_seen = true;
37339 if (data->error_seen)
37342 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37346 cp_parser_push_lexer_for_tokens (parser, ce);
37347 parser->lexer->in_pragma = true;
37348 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37349 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37350 cp_lexer_consume_token (parser->lexer);
37351 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37352 "#pragma omp declare simd", pragma_tok);
37353 cp_parser_pop_lexer (parser);
37355 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37356 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37357 TREE_CHAIN (c) = attrs;
37358 if (processing_template_decl)
37359 ATTR_IS_DEPENDENT (c) = 1;
37363 data->fndecl_seen = true;
37369 # pragma omp declare target new-line
37370 declarations and definitions
37371 # pragma omp end declare target new-line
37374 # pragma omp declare target ( extended-list ) new-line
37376 # pragma omp declare target declare-target-clauses[seq] new-line */
37378 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37379 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37383 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37385 tree clauses = NULL_TREE;
37386 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37388 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37389 "#pragma omp declare target", pragma_tok);
37390 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37392 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37394 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37395 cp_parser_require_pragma_eol (parser, pragma_tok);
37399 cp_parser_require_pragma_eol (parser, pragma_tok);
37400 scope_chain->omp_declare_target_attribute++;
37403 if (scope_chain->omp_declare_target_attribute)
37404 error_at (pragma_tok->location,
37405 "%<#pragma omp declare target%> with clauses in between "
37406 "%<#pragma omp declare target%> without clauses and "
37407 "%<#pragma omp end declare target%>");
37408 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37410 tree t = OMP_CLAUSE_DECL (c), id;
37411 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37412 tree at2 = lookup_attribute ("omp declare target link",
37413 DECL_ATTRIBUTES (t));
37414 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37416 id = get_identifier ("omp declare target link");
37417 std::swap (at1, at2);
37420 id = get_identifier ("omp declare target");
37423 error_at (OMP_CLAUSE_LOCATION (c),
37424 "%qD specified both in declare target %<link%> and %<to%>"
37430 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37431 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37434 symtab_node *node = symtab_node::get (t);
37437 node->offloadable = 1;
37438 if (ENABLE_OFFLOADING)
37440 g->have_offload = true;
37441 if (is_a <varpool_node *> (node))
37442 vec_safe_push (offload_vars, t);
37450 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37452 const char *p = "";
37453 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37455 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37456 p = IDENTIFIER_POINTER (id);
37458 if (strcmp (p, "declare") == 0)
37460 cp_lexer_consume_token (parser->lexer);
37462 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37464 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37465 p = IDENTIFIER_POINTER (id);
37467 if (strcmp (p, "target") == 0)
37468 cp_lexer_consume_token (parser->lexer);
37471 cp_parser_error (parser, "expected %<target%>");
37472 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37478 cp_parser_error (parser, "expected %<declare%>");
37479 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37482 cp_parser_require_pragma_eol (parser, pragma_tok);
37483 if (!scope_chain->omp_declare_target_attribute)
37484 error_at (pragma_tok->location,
37485 "%<#pragma omp end declare target%> without corresponding "
37486 "%<#pragma omp declare target%>");
37488 scope_chain->omp_declare_target_attribute--;
37491 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37492 expression and optional initializer clause of
37493 #pragma omp declare reduction. We store the expression(s) as
37494 either 3, 6 or 7 special statements inside of the artificial function's
37495 body. The first two statements are DECL_EXPRs for the artificial
37496 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37497 expression that uses those variables.
37498 If there was any INITIALIZER clause, this is followed by further statements,
37499 the fourth and fifth statements are DECL_EXPRs for the artificial
37500 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37501 constructor variant (first token after open paren is not omp_priv),
37502 then the sixth statement is a statement with the function call expression
37503 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37504 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37505 to initialize the OMP_PRIV artificial variable and there is seventh
37506 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37509 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37511 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37512 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37513 type = TREE_TYPE (type);
37514 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37515 DECL_ARTIFICIAL (omp_out) = 1;
37516 pushdecl (omp_out);
37517 add_decl_expr (omp_out);
37518 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37519 DECL_ARTIFICIAL (omp_in) = 1;
37521 add_decl_expr (omp_in);
37523 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37525 keep_next_level (true);
37526 tree block = begin_omp_structured_block ();
37527 combiner = cp_parser_expression (parser);
37528 finish_expr_stmt (combiner);
37529 block = finish_omp_structured_block (block);
37532 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37535 const char *p = "";
37536 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37538 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37539 p = IDENTIFIER_POINTER (id);
37542 if (strcmp (p, "initializer") == 0)
37544 cp_lexer_consume_token (parser->lexer);
37545 matching_parens parens;
37546 if (!parens.require_open (parser))
37550 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37552 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37553 p = IDENTIFIER_POINTER (id);
37556 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37557 DECL_ARTIFICIAL (omp_priv) = 1;
37558 pushdecl (omp_priv);
37559 add_decl_expr (omp_priv);
37560 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37561 DECL_ARTIFICIAL (omp_orig) = 1;
37562 pushdecl (omp_orig);
37563 add_decl_expr (omp_orig);
37565 keep_next_level (true);
37566 block = begin_omp_structured_block ();
37569 if (strcmp (p, "omp_priv") == 0)
37571 bool is_direct_init, is_non_constant_init;
37573 cp_lexer_consume_token (parser->lexer);
37574 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37575 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37576 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37577 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37579 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37580 == CPP_CLOSE_PAREN))
37582 finish_omp_structured_block (block);
37583 error ("invalid initializer clause");
37586 initializer = cp_parser_initializer (parser, &is_direct_init,
37587 &is_non_constant_init);
37588 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37589 NULL_TREE, LOOKUP_ONLYCONVERTING);
37593 cp_parser_parse_tentatively (parser);
37594 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37595 /*check_dependency_p=*/true,
37596 /*template_p=*/NULL,
37597 /*declarator_p=*/false,
37598 /*optional_p=*/false);
37599 vec<tree, va_gc> *args;
37600 if (fn_name == error_mark_node
37601 || cp_parser_error_occurred (parser)
37602 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37603 || ((args = cp_parser_parenthesized_expression_list
37604 (parser, non_attr, /*cast_p=*/false,
37605 /*allow_expansion_p=*/true,
37606 /*non_constant_p=*/NULL)),
37607 cp_parser_error_occurred (parser)))
37609 finish_omp_structured_block (block);
37610 cp_parser_abort_tentative_parse (parser);
37611 cp_parser_error (parser, "expected id-expression (arguments)");
37616 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37617 if (arg == omp_priv
37618 || (TREE_CODE (arg) == ADDR_EXPR
37619 && TREE_OPERAND (arg, 0) == omp_priv))
37621 cp_parser_abort_tentative_parse (parser);
37622 if (arg == NULL_TREE)
37623 error ("one of the initializer call arguments should be %<omp_priv%>"
37624 " or %<&omp_priv%>");
37625 initializer = cp_parser_postfix_expression (parser, false, false, false,
37627 finish_expr_stmt (initializer);
37630 block = finish_omp_structured_block (block);
37631 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37635 add_decl_expr (omp_orig);
37637 if (!parens.require_close (parser))
37641 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37642 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37649 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37650 initializer-clause[opt] new-line
37652 initializer-clause:
37653 initializer (omp_priv initializer)
37654 initializer (function-name (argument-list)) */
37657 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37658 enum pragma_context)
37660 auto_vec<tree> types;
37661 enum tree_code reduc_code = ERROR_MARK;
37662 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37664 cp_token *first_token;
37665 cp_token_cache *cp;
37669 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37670 p = obstack_alloc (&declarator_obstack, 0);
37672 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37675 switch (cp_lexer_peek_token (parser->lexer)->type)
37678 reduc_code = PLUS_EXPR;
37681 reduc_code = MULT_EXPR;
37684 reduc_code = MINUS_EXPR;
37687 reduc_code = BIT_AND_EXPR;
37690 reduc_code = BIT_XOR_EXPR;
37693 reduc_code = BIT_IOR_EXPR;
37696 reduc_code = TRUTH_ANDIF_EXPR;
37699 reduc_code = TRUTH_ORIF_EXPR;
37702 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37705 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37706 "%<|%>, %<&&%>, %<||%> or identifier");
37710 if (reduc_code != ERROR_MARK)
37711 cp_lexer_consume_token (parser->lexer);
37713 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37714 if (reduc_id == error_mark_node)
37717 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37720 /* Types may not be defined in declare reduction type list. */
37721 const char *saved_message;
37722 saved_message = parser->type_definition_forbidden_message;
37723 parser->type_definition_forbidden_message
37724 = G_("types may not be defined in declare reduction type list");
37725 bool saved_colon_corrects_to_scope_p;
37726 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37727 parser->colon_corrects_to_scope_p = false;
37728 bool saved_colon_doesnt_start_class_def_p;
37729 saved_colon_doesnt_start_class_def_p
37730 = parser->colon_doesnt_start_class_def_p;
37731 parser->colon_doesnt_start_class_def_p = true;
37735 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37736 type = cp_parser_type_id (parser);
37737 if (type == error_mark_node)
37739 else if (ARITHMETIC_TYPE_P (type)
37740 && (orig_reduc_id == NULL_TREE
37741 || (TREE_CODE (type) != COMPLEX_TYPE
37742 && (id_equal (orig_reduc_id, "min")
37743 || id_equal (orig_reduc_id, "max")))))
37744 error_at (loc, "predeclared arithmetic type %qT in "
37745 "%<#pragma omp declare reduction%>", type);
37746 else if (TREE_CODE (type) == FUNCTION_TYPE
37747 || TREE_CODE (type) == METHOD_TYPE
37748 || TREE_CODE (type) == ARRAY_TYPE)
37749 error_at (loc, "function or array type %qT in "
37750 "%<#pragma omp declare reduction%>", type);
37751 else if (TREE_CODE (type) == REFERENCE_TYPE)
37752 error_at (loc, "reference type %qT in "
37753 "%<#pragma omp declare reduction%>", type);
37754 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37755 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37756 "%<#pragma omp declare reduction%>", type);
37758 types.safe_push (type);
37760 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37761 cp_lexer_consume_token (parser->lexer);
37766 /* Restore the saved message. */
37767 parser->type_definition_forbidden_message = saved_message;
37768 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37769 parser->colon_doesnt_start_class_def_p
37770 = saved_colon_doesnt_start_class_def_p;
37772 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37773 || types.is_empty ())
37776 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37780 first_token = cp_lexer_peek_token (parser->lexer);
37783 FOR_EACH_VEC_ELT (types, i, type)
37786 = build_function_type_list (void_type_node,
37787 cp_build_reference_type (type, false),
37789 tree this_reduc_id = reduc_id;
37790 if (!dependent_type_p (type))
37791 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37792 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37793 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37794 DECL_ARTIFICIAL (fndecl) = 1;
37795 DECL_EXTERNAL (fndecl) = 1;
37796 DECL_DECLARED_INLINE_P (fndecl) = 1;
37797 DECL_IGNORED_P (fndecl) = 1;
37798 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37799 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37800 DECL_ATTRIBUTES (fndecl)
37801 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37802 DECL_ATTRIBUTES (fndecl));
37803 if (processing_template_decl)
37804 fndecl = push_template_decl (fndecl);
37805 bool block_scope = false;
37806 tree block = NULL_TREE;
37807 if (current_function_decl)
37809 block_scope = true;
37810 DECL_CONTEXT (fndecl) = global_namespace;
37811 if (!processing_template_decl)
37814 else if (current_class_type)
37818 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37819 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37820 cp_lexer_consume_token (parser->lexer);
37821 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37823 cp = cp_token_cache_new (first_token,
37824 cp_lexer_peek_nth_token (parser->lexer,
37827 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37828 finish_member_declaration (fndecl);
37829 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37830 DECL_PENDING_INLINE_P (fndecl) = 1;
37831 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37836 DECL_CONTEXT (fndecl) = current_namespace;
37840 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37842 block = begin_omp_structured_block ();
37845 cp_parser_push_lexer_for_tokens (parser, cp);
37846 parser->lexer->in_pragma = true;
37848 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37851 finish_function (/*inline_p=*/false);
37853 DECL_CONTEXT (fndecl) = current_function_decl;
37855 cp_parser_pop_lexer (parser);
37859 cp_parser_pop_lexer (parser);
37861 finish_function (/*inline_p=*/false);
37864 DECL_CONTEXT (fndecl) = current_function_decl;
37865 block = finish_omp_structured_block (block);
37866 if (TREE_CODE (block) == BIND_EXPR)
37867 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37868 else if (TREE_CODE (block) == STATEMENT_LIST)
37869 DECL_SAVED_TREE (fndecl) = block;
37870 if (processing_template_decl)
37871 add_decl_expr (fndecl);
37873 cp_check_omp_declare_reduction (fndecl);
37874 if (cp == NULL && types.length () > 1)
37875 cp = cp_token_cache_new (first_token,
37876 cp_lexer_peek_nth_token (parser->lexer, 2));
37877 if (errs != errorcount)
37881 cp_parser_require_pragma_eol (parser, pragma_tok);
37884 /* Free any declarators allocated. */
37885 obstack_free (&declarator_obstack, p);
37889 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37890 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37891 initializer-clause[opt] new-line
37892 #pragma omp declare target new-line */
37895 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37896 enum pragma_context context)
37898 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37900 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37901 const char *p = IDENTIFIER_POINTER (id);
37903 if (strcmp (p, "simd") == 0)
37905 cp_lexer_consume_token (parser->lexer);
37906 cp_parser_omp_declare_simd (parser, pragma_tok,
37910 cp_ensure_no_omp_declare_simd (parser);
37911 if (strcmp (p, "reduction") == 0)
37913 cp_lexer_consume_token (parser->lexer);
37914 cp_parser_omp_declare_reduction (parser, pragma_tok,
37918 if (!flag_openmp) /* flag_openmp_simd */
37920 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37923 if (strcmp (p, "target") == 0)
37925 cp_lexer_consume_token (parser->lexer);
37926 cp_parser_omp_declare_target (parser, pragma_tok);
37930 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37932 cp_parser_require_pragma_eol (parser, pragma_tok);
37937 #pragma omp taskloop taskloop-clause[optseq] new-line
37940 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37943 #define OMP_TASKLOOP_CLAUSE_MASK \
37944 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37960 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37961 char *p_name, omp_clause_mask mask, tree *cclauses,
37964 tree clauses, sb, ret;
37966 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37968 strcat (p_name, " taskloop");
37969 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37971 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37973 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37974 const char *p = IDENTIFIER_POINTER (id);
37976 if (strcmp (p, "simd") == 0)
37978 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37979 if (cclauses == NULL)
37980 cclauses = cclauses_buf;
37982 cp_lexer_consume_token (parser->lexer);
37983 if (!flag_openmp) /* flag_openmp_simd */
37984 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37986 sb = begin_omp_structured_block ();
37987 save = cp_parser_begin_omp_structured_block (parser);
37988 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37990 cp_parser_end_omp_structured_block (parser, save);
37991 tree body = finish_omp_structured_block (sb);
37994 ret = make_node (OMP_TASKLOOP);
37995 TREE_TYPE (ret) = void_type_node;
37996 OMP_FOR_BODY (ret) = body;
37997 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37998 SET_EXPR_LOCATION (ret, loc);
38003 if (!flag_openmp) /* flag_openmp_simd */
38005 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38009 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38013 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38014 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38017 sb = begin_omp_structured_block ();
38018 save = cp_parser_begin_omp_structured_block (parser);
38020 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38023 cp_parser_end_omp_structured_block (parser, save);
38024 add_stmt (finish_omp_structured_block (sb));
38031 # pragma acc routine oacc-routine-clause[optseq] new-line
38032 function-definition
38034 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38037 #define OACC_ROUTINE_CLAUSE_MASK \
38038 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38044 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38045 component, which must resolve to a declared namespace-scope
38046 function. The clauses are either processed directly (for a named
38047 function), or defered until the immediatley following declaration
38051 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38052 enum pragma_context context)
38054 gcc_checking_assert (context == pragma_external);
38055 /* The checking for "another pragma following this one" in the "no optional
38056 '( name )'" case makes sure that we dont re-enter. */
38057 gcc_checking_assert (parser->oacc_routine == NULL);
38059 cp_oacc_routine_data data;
38060 data.error_seen = false;
38061 data.fndecl_seen = false;
38062 data.tokens = vNULL;
38063 data.clauses = NULL_TREE;
38064 data.loc = pragma_tok->location;
38065 /* It is safe to take the address of a local variable; it will only be
38066 used while this scope is live. */
38067 parser->oacc_routine = &data;
38069 /* Look for optional '( name )'. */
38070 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38072 matching_parens parens;
38073 parens.consume_open (parser); /* '(' */
38075 /* We parse the name as an id-expression. If it resolves to
38076 anything other than a non-overloaded function at namespace
38077 scope, it's an error. */
38078 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38079 tree name = cp_parser_id_expression (parser,
38080 /*template_keyword_p=*/false,
38081 /*check_dependency_p=*/false,
38082 /*template_p=*/NULL,
38083 /*declarator_p=*/false,
38084 /*optional_p=*/false);
38085 tree decl = (identifier_p (name)
38086 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38088 if (name != error_mark_node && decl == error_mark_node)
38089 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38091 if (decl == error_mark_node
38092 || !parens.require_close (parser))
38094 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38095 parser->oacc_routine = NULL;
38100 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38101 "#pragma acc routine",
38102 cp_lexer_peek_token (parser->lexer));
38104 if (decl && is_overloaded_fn (decl)
38105 && (TREE_CODE (decl) != FUNCTION_DECL
38106 || DECL_FUNCTION_TEMPLATE_P (decl)))
38108 error_at (name_loc,
38109 "%<#pragma acc routine%> names a set of overloads");
38110 parser->oacc_routine = NULL;
38114 /* Perhaps we should use the same rule as declarations in different
38116 if (!DECL_NAMESPACE_SCOPE_P (decl))
38118 error_at (name_loc,
38119 "%qD does not refer to a namespace scope function", decl);
38120 parser->oacc_routine = NULL;
38124 if (TREE_CODE (decl) != FUNCTION_DECL)
38126 error_at (name_loc, "%qD does not refer to a function", decl);
38127 parser->oacc_routine = NULL;
38131 cp_finalize_oacc_routine (parser, decl, false);
38132 parser->oacc_routine = NULL;
38134 else /* No optional '( name )'. */
38136 /* Store away all pragma tokens. */
38137 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38138 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38139 cp_lexer_consume_token (parser->lexer);
38140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38141 parser->oacc_routine->error_seen = true;
38142 cp_parser_require_pragma_eol (parser, pragma_tok);
38143 struct cp_token_cache *cp
38144 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38145 parser->oacc_routine->tokens.safe_push (cp);
38147 /* Emit a helpful diagnostic if there's another pragma following this
38149 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38151 cp_ensure_no_oacc_routine (parser);
38152 data.tokens.release ();
38153 /* ..., and then just keep going. */
38157 /* We only have to consider the pragma_external case here. */
38158 cp_parser_declaration (parser);
38159 if (parser->oacc_routine
38160 && !parser->oacc_routine->fndecl_seen)
38161 cp_ensure_no_oacc_routine (parser);
38163 parser->oacc_routine = NULL;
38164 data.tokens.release ();
38168 /* Finalize #pragma acc routine clauses after direct declarator has
38172 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38174 struct cp_token_cache *ce;
38175 cp_oacc_routine_data *data = parser->oacc_routine;
38177 if (!data->error_seen && data->fndecl_seen)
38179 error_at (data->loc,
38180 "%<#pragma acc routine%> not immediately followed by "
38181 "a single function declaration or definition");
38182 data->error_seen = true;
38184 if (data->error_seen)
38187 gcc_checking_assert (data->tokens.length () == 1);
38188 ce = data->tokens[0];
38190 cp_parser_push_lexer_for_tokens (parser, ce);
38191 parser->lexer->in_pragma = true;
38192 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38194 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38195 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38196 parser->oacc_routine->clauses
38197 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38198 "#pragma acc routine", pragma_tok);
38199 cp_parser_pop_lexer (parser);
38200 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38206 /* Apply any saved OpenACC routine clauses to a just-parsed
38210 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38212 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38214 /* Keep going if we're in error reporting mode. */
38215 if (parser->oacc_routine->error_seen
38216 || fndecl == error_mark_node)
38219 if (parser->oacc_routine->fndecl_seen)
38221 error_at (parser->oacc_routine->loc,
38222 "%<#pragma acc routine%> not immediately followed by"
38223 " a single function declaration or definition");
38224 parser->oacc_routine = NULL;
38227 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38229 cp_ensure_no_oacc_routine (parser);
38233 if (oacc_get_fn_attrib (fndecl))
38235 error_at (parser->oacc_routine->loc,
38236 "%<#pragma acc routine%> already applied to %qD", fndecl);
38237 parser->oacc_routine = NULL;
38241 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38243 error_at (parser->oacc_routine->loc,
38245 ? G_("%<#pragma acc routine%> must be applied before use")
38246 : G_("%<#pragma acc routine%> must be applied before "
38248 parser->oacc_routine = NULL;
38252 /* Process the routine's dimension clauses. */
38253 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38254 oacc_replace_fn_attrib (fndecl, dims);
38256 /* Add an "omp declare target" attribute. */
38257 DECL_ATTRIBUTES (fndecl)
38258 = tree_cons (get_identifier ("omp declare target"),
38259 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38261 /* Don't unset parser->oacc_routine here: we may still need it to
38262 diagnose wrong usage. But, remember that we've used this "#pragma acc
38264 parser->oacc_routine->fndecl_seen = true;
38268 /* Main entry point to OpenMP statement pragmas. */
38271 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38274 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38275 omp_clause_mask mask (0);
38277 switch (cp_parser_pragma_kind (pragma_tok))
38279 case PRAGMA_OACC_ATOMIC:
38280 cp_parser_omp_atomic (parser, pragma_tok);
38282 case PRAGMA_OACC_CACHE:
38283 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38285 case PRAGMA_OACC_DATA:
38286 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38288 case PRAGMA_OACC_ENTER_DATA:
38289 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38291 case PRAGMA_OACC_EXIT_DATA:
38292 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38294 case PRAGMA_OACC_HOST_DATA:
38295 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38297 case PRAGMA_OACC_KERNELS:
38298 case PRAGMA_OACC_PARALLEL:
38299 strcpy (p_name, "#pragma acc");
38300 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38303 case PRAGMA_OACC_LOOP:
38304 strcpy (p_name, "#pragma acc");
38305 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38308 case PRAGMA_OACC_UPDATE:
38309 stmt = cp_parser_oacc_update (parser, pragma_tok);
38311 case PRAGMA_OACC_WAIT:
38312 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38314 case PRAGMA_OMP_ATOMIC:
38315 cp_parser_omp_atomic (parser, pragma_tok);
38317 case PRAGMA_OMP_CRITICAL:
38318 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38320 case PRAGMA_OMP_DISTRIBUTE:
38321 strcpy (p_name, "#pragma omp");
38322 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38325 case PRAGMA_OMP_FOR:
38326 strcpy (p_name, "#pragma omp");
38327 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38330 case PRAGMA_OMP_MASTER:
38331 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38333 case PRAGMA_OMP_PARALLEL:
38334 strcpy (p_name, "#pragma omp");
38335 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38338 case PRAGMA_OMP_SECTIONS:
38339 strcpy (p_name, "#pragma omp");
38340 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38342 case PRAGMA_OMP_SIMD:
38343 strcpy (p_name, "#pragma omp");
38344 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38347 case PRAGMA_OMP_SINGLE:
38348 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38350 case PRAGMA_OMP_TASK:
38351 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38353 case PRAGMA_OMP_TASKGROUP:
38354 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38356 case PRAGMA_OMP_TASKLOOP:
38357 strcpy (p_name, "#pragma omp");
38358 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38361 case PRAGMA_OMP_TEAMS:
38362 strcpy (p_name, "#pragma omp");
38363 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38367 gcc_unreachable ();
38370 protected_set_expr_location (stmt, pragma_tok->location);
38373 /* Transactional Memory parsing routines. */
38375 /* Parse a transaction attribute.
38381 We use this instead of cp_parser_attributes_opt for transactions to avoid
38382 the pedwarn in C++98 mode. */
38385 cp_parser_txn_attribute_opt (cp_parser *parser)
38388 tree attr_name, attr = NULL;
38390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38391 return cp_parser_attributes_opt (parser);
38393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38395 cp_lexer_consume_token (parser->lexer);
38396 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38399 token = cp_lexer_peek_token (parser->lexer);
38400 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38402 token = cp_lexer_consume_token (parser->lexer);
38404 attr_name = (token->type == CPP_KEYWORD
38405 /* For keywords, use the canonical spelling,
38406 not the parsed identifier. */
38407 ? ridpointers[(int) token->keyword]
38409 attr = build_tree_list (attr_name, NULL_TREE);
38412 cp_parser_error (parser, "expected identifier");
38414 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38416 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38420 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38422 transaction-statement:
38423 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38425 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38429 cp_parser_transaction (cp_parser *parser, cp_token *token)
38431 unsigned char old_in = parser->in_transaction;
38432 unsigned char this_in = 1, new_in;
38433 enum rid keyword = token->keyword;
38434 tree stmt, attrs, noex;
38436 cp_lexer_consume_token (parser->lexer);
38438 if (keyword == RID_TRANSACTION_RELAXED
38439 || keyword == RID_SYNCHRONIZED)
38440 this_in |= TM_STMT_ATTR_RELAXED;
38443 attrs = cp_parser_txn_attribute_opt (parser);
38445 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38448 /* Parse a noexcept specification. */
38449 if (keyword == RID_ATOMIC_NOEXCEPT)
38450 noex = boolean_true_node;
38451 else if (keyword == RID_ATOMIC_CANCEL)
38453 /* cancel-and-throw is unimplemented. */
38454 sorry ("atomic_cancel");
38458 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38460 /* Keep track if we're in the lexical scope of an outer transaction. */
38461 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38463 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38465 parser->in_transaction = new_in;
38466 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38467 parser->in_transaction = old_in;
38469 finish_transaction_stmt (stmt, NULL, this_in, noex);
38474 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38476 transaction-expression:
38477 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38478 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38482 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38484 unsigned char old_in = parser->in_transaction;
38485 unsigned char this_in = 1;
38489 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38491 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38492 || keyword == RID_TRANSACTION_RELAXED);
38496 keyword == RID_TRANSACTION_RELAXED
38497 ? G_("%<__transaction_relaxed%> without transactional memory "
38499 : G_("%<__transaction_atomic%> without transactional memory "
38500 "support enabled"));
38502 token = cp_parser_require_keyword (parser, keyword,
38503 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38504 : RT_TRANSACTION_RELAXED));
38505 gcc_assert (token != NULL);
38507 if (keyword == RID_TRANSACTION_RELAXED)
38508 this_in |= TM_STMT_ATTR_RELAXED;
38510 /* Set this early. This might mean that we allow transaction_cancel in
38511 an expression that we find out later actually has to be a constexpr.
38512 However, we expect that cxx_constant_value will be able to deal with
38513 this; also, if the noexcept has no constexpr, then what we parse next
38514 really is a transaction's body. */
38515 parser->in_transaction = this_in;
38517 /* Parse a noexcept specification. */
38518 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38521 if (!noex || !noex_expr
38522 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38524 matching_parens parens;
38525 parens.require_open (parser);
38527 expr = cp_parser_expression (parser);
38528 expr = finish_parenthesized_expr (expr);
38530 parens.require_close (parser);
38534 /* The only expression that is available got parsed for the noexcept
38535 already. noexcept is true then. */
38537 noex = boolean_true_node;
38540 expr = build_transaction_expr (token->location, expr, this_in, noex);
38541 parser->in_transaction = old_in;
38543 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38544 return error_mark_node;
38546 return (flag_tm ? expr : error_mark_node);
38549 /* Parse a function-transaction-block.
38551 function-transaction-block:
38552 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38554 __transaction_atomic txn-attribute[opt] function-try-block
38555 __transaction_relaxed ctor-initializer[opt] function-body
38556 __transaction_relaxed function-try-block
38560 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38562 unsigned char old_in = parser->in_transaction;
38563 unsigned char new_in = 1;
38564 tree compound_stmt, stmt, attrs;
38567 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38568 || keyword == RID_TRANSACTION_RELAXED);
38569 token = cp_parser_require_keyword (parser, keyword,
38570 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38571 : RT_TRANSACTION_RELAXED));
38572 gcc_assert (token != NULL);
38574 if (keyword == RID_TRANSACTION_RELAXED)
38575 new_in |= TM_STMT_ATTR_RELAXED;
38578 attrs = cp_parser_txn_attribute_opt (parser);
38580 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38583 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38585 parser->in_transaction = new_in;
38587 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38588 cp_parser_function_try_block (parser);
38590 cp_parser_ctor_initializer_opt_and_function_body
38591 (parser, /*in_function_try_block=*/false);
38593 parser->in_transaction = old_in;
38595 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38598 /* Parse a __transaction_cancel statement.
38601 __transaction_cancel txn-attribute[opt] ;
38602 __transaction_cancel txn-attribute[opt] throw-expression ;
38604 ??? Cancel and throw is not yet implemented. */
38607 cp_parser_transaction_cancel (cp_parser *parser)
38610 bool is_outer = false;
38613 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38614 RT_TRANSACTION_CANCEL);
38615 gcc_assert (token != NULL);
38617 attrs = cp_parser_txn_attribute_opt (parser);
38619 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38621 /* ??? Parse cancel-and-throw here. */
38623 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38627 error_at (token->location, "%<__transaction_cancel%> without "
38628 "transactional memory support enabled");
38629 return error_mark_node;
38631 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38633 error_at (token->location, "%<__transaction_cancel%> within a "
38634 "%<__transaction_relaxed%>");
38635 return error_mark_node;
38639 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38640 && !is_tm_may_cancel_outer (current_function_decl))
38642 error_at (token->location, "outer %<__transaction_cancel%> not "
38643 "within outer %<__transaction_atomic%>");
38644 error_at (token->location,
38645 " or a %<transaction_may_cancel_outer%> function");
38646 return error_mark_node;
38649 else if (parser->in_transaction == 0)
38651 error_at (token->location, "%<__transaction_cancel%> not within "
38652 "%<__transaction_atomic%>");
38653 return error_mark_node;
38656 stmt = build_tm_abort_call (token->location, is_outer);
38664 static GTY (()) cp_parser *the_parser;
38667 /* Special handling for the first token or line in the file. The first
38668 thing in the file might be #pragma GCC pch_preprocess, which loads a
38669 PCH file, which is a GC collection point. So we need to handle this
38670 first pragma without benefit of an existing lexer structure.
38672 Always returns one token to the caller in *FIRST_TOKEN. This is
38673 either the true first token of the file, or the first token after
38674 the initial pragma. */
38677 cp_parser_initial_pragma (cp_token *first_token)
38681 cp_lexer_get_preprocessor_token (NULL, first_token);
38682 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38685 cp_lexer_get_preprocessor_token (NULL, first_token);
38686 if (first_token->type == CPP_STRING)
38688 name = first_token->u.value;
38690 cp_lexer_get_preprocessor_token (NULL, first_token);
38691 if (first_token->type != CPP_PRAGMA_EOL)
38692 error_at (first_token->location,
38693 "junk at end of %<#pragma GCC pch_preprocess%>");
38696 error_at (first_token->location, "expected string literal");
38698 /* Skip to the end of the pragma. */
38699 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38700 cp_lexer_get_preprocessor_token (NULL, first_token);
38702 /* Now actually load the PCH file. */
38704 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38706 /* Read one more token to return to our caller. We have to do this
38707 after reading the PCH file in, since its pointers have to be
38709 cp_lexer_get_preprocessor_token (NULL, first_token);
38712 /* Parse a pragma GCC ivdep. */
38715 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38717 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38721 /* Parse a pragma GCC unroll. */
38723 static unsigned short
38724 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38726 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38727 tree expr = cp_parser_constant_expression (parser);
38728 unsigned short unroll;
38729 expr = maybe_constant_value (expr);
38730 HOST_WIDE_INT lunroll = 0;
38731 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38732 || TREE_CODE (expr) != INTEGER_CST
38733 || (lunroll = tree_to_shwi (expr)) < 0
38734 || lunroll >= USHRT_MAX)
38736 error_at (location, "%<#pragma GCC unroll%> requires an"
38737 " assignment-expression that evaluates to a non-negative"
38738 " integral constant less than %u", USHRT_MAX);
38743 unroll = (unsigned short)lunroll;
38747 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38751 /* Normal parsing of a pragma token. Here we can (and must) use the
38755 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38757 cp_token *pragma_tok;
38762 pragma_tok = cp_lexer_consume_token (parser->lexer);
38763 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38764 parser->lexer->in_pragma = true;
38766 id = cp_parser_pragma_kind (pragma_tok);
38767 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38768 cp_ensure_no_omp_declare_simd (parser);
38771 case PRAGMA_GCC_PCH_PREPROCESS:
38772 error_at (pragma_tok->location,
38773 "%<#pragma GCC pch_preprocess%> must be first");
38776 case PRAGMA_OMP_BARRIER:
38779 case pragma_compound:
38780 cp_parser_omp_barrier (parser, pragma_tok);
38783 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38784 "used in compound statements", "omp barrier");
38791 case PRAGMA_OMP_FLUSH:
38794 case pragma_compound:
38795 cp_parser_omp_flush (parser, pragma_tok);
38798 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38799 "used in compound statements", "omp flush");
38806 case PRAGMA_OMP_TASKWAIT:
38809 case pragma_compound:
38810 cp_parser_omp_taskwait (parser, pragma_tok);
38813 error_at (pragma_tok->location,
38814 "%<#pragma %s%> may only be used in compound statements",
38822 case PRAGMA_OMP_TASKYIELD:
38825 case pragma_compound:
38826 cp_parser_omp_taskyield (parser, pragma_tok);
38829 error_at (pragma_tok->location,
38830 "%<#pragma %s%> may only be used in compound statements",
38838 case PRAGMA_OMP_CANCEL:
38841 case pragma_compound:
38842 cp_parser_omp_cancel (parser, pragma_tok);
38845 error_at (pragma_tok->location,
38846 "%<#pragma %s%> may only be used in compound statements",
38854 case PRAGMA_OMP_CANCELLATION_POINT:
38855 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38858 case PRAGMA_OMP_THREADPRIVATE:
38859 cp_parser_omp_threadprivate (parser, pragma_tok);
38862 case PRAGMA_OMP_DECLARE:
38863 return cp_parser_omp_declare (parser, pragma_tok, context);
38865 case PRAGMA_OACC_DECLARE:
38866 cp_parser_oacc_declare (parser, pragma_tok);
38869 case PRAGMA_OACC_ENTER_DATA:
38870 if (context == pragma_stmt)
38872 error_at (pragma_tok->location,
38873 "%<#pragma %s%> may only be used in compound statements",
38877 else if (context != pragma_compound)
38879 cp_parser_omp_construct (parser, pragma_tok, if_p);
38882 case PRAGMA_OACC_EXIT_DATA:
38883 if (context == pragma_stmt)
38885 error_at (pragma_tok->location,
38886 "%<#pragma %s%> may only be used in compound statements",
38890 else if (context != pragma_compound)
38892 cp_parser_omp_construct (parser, pragma_tok, if_p);
38895 case PRAGMA_OACC_ROUTINE:
38896 if (context != pragma_external)
38898 error_at (pragma_tok->location,
38899 "%<#pragma acc routine%> must be at file scope");
38902 cp_parser_oacc_routine (parser, pragma_tok, context);
38905 case PRAGMA_OACC_UPDATE:
38906 if (context == pragma_stmt)
38908 error_at (pragma_tok->location,
38909 "%<#pragma %s%> may only be used in compound statements",
38913 else if (context != pragma_compound)
38915 cp_parser_omp_construct (parser, pragma_tok, if_p);
38918 case PRAGMA_OACC_WAIT:
38919 if (context == pragma_stmt)
38921 error_at (pragma_tok->location,
38922 "%<#pragma %s%> may only be used in compound statements",
38926 else if (context != pragma_compound)
38928 cp_parser_omp_construct (parser, pragma_tok, if_p);
38931 case PRAGMA_OACC_ATOMIC:
38932 case PRAGMA_OACC_CACHE:
38933 case PRAGMA_OACC_DATA:
38934 case PRAGMA_OACC_HOST_DATA:
38935 case PRAGMA_OACC_KERNELS:
38936 case PRAGMA_OACC_PARALLEL:
38937 case PRAGMA_OACC_LOOP:
38938 case PRAGMA_OMP_ATOMIC:
38939 case PRAGMA_OMP_CRITICAL:
38940 case PRAGMA_OMP_DISTRIBUTE:
38941 case PRAGMA_OMP_FOR:
38942 case PRAGMA_OMP_MASTER:
38943 case PRAGMA_OMP_PARALLEL:
38944 case PRAGMA_OMP_SECTIONS:
38945 case PRAGMA_OMP_SIMD:
38946 case PRAGMA_OMP_SINGLE:
38947 case PRAGMA_OMP_TASK:
38948 case PRAGMA_OMP_TASKGROUP:
38949 case PRAGMA_OMP_TASKLOOP:
38950 case PRAGMA_OMP_TEAMS:
38951 if (context != pragma_stmt && context != pragma_compound)
38953 stmt = push_omp_privatization_clauses (false);
38954 cp_parser_omp_construct (parser, pragma_tok, if_p);
38955 pop_omp_privatization_clauses (stmt);
38958 case PRAGMA_OMP_ORDERED:
38959 if (context != pragma_stmt && context != pragma_compound)
38961 stmt = push_omp_privatization_clauses (false);
38962 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38963 pop_omp_privatization_clauses (stmt);
38966 case PRAGMA_OMP_TARGET:
38967 if (context != pragma_stmt && context != pragma_compound)
38969 stmt = push_omp_privatization_clauses (false);
38970 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38971 pop_omp_privatization_clauses (stmt);
38974 case PRAGMA_OMP_END_DECLARE_TARGET:
38975 cp_parser_omp_end_declare_target (parser, pragma_tok);
38978 case PRAGMA_OMP_SECTION:
38979 error_at (pragma_tok->location,
38980 "%<#pragma omp section%> may only be used in "
38981 "%<#pragma omp sections%> construct");
38986 if (context == pragma_external)
38988 error_at (pragma_tok->location,
38989 "%<#pragma GCC ivdep%> must be inside a function");
38992 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38993 unsigned short unroll;
38994 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38995 if (tok->type == CPP_PRAGMA
38996 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38998 tok = cp_lexer_consume_token (parser->lexer);
38999 unroll = cp_parser_pragma_unroll (parser, tok);
39000 tok = cp_lexer_peek_token (the_parser->lexer);
39004 if (tok->type != CPP_KEYWORD
39005 || (tok->keyword != RID_FOR
39006 && tok->keyword != RID_WHILE
39007 && tok->keyword != RID_DO))
39009 cp_parser_error (parser, "for, while or do statement expected");
39012 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39016 case PRAGMA_UNROLL:
39018 if (context == pragma_external)
39020 error_at (pragma_tok->location,
39021 "%<#pragma GCC unroll%> must be inside a function");
39024 const unsigned short unroll
39025 = cp_parser_pragma_unroll (parser, pragma_tok);
39027 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39028 if (tok->type == CPP_PRAGMA
39029 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39031 tok = cp_lexer_consume_token (parser->lexer);
39032 ivdep = cp_parser_pragma_ivdep (parser, tok);
39033 tok = cp_lexer_peek_token (the_parser->lexer);
39037 if (tok->type != CPP_KEYWORD
39038 || (tok->keyword != RID_FOR
39039 && tok->keyword != RID_WHILE
39040 && tok->keyword != RID_DO))
39042 cp_parser_error (parser, "for, while or do statement expected");
39045 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39050 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39051 c_invoke_pragma_handler (id);
39055 cp_parser_error (parser, "expected declaration specifiers");
39059 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39063 /* The interface the pragma parsers have to the lexer. */
39066 pragma_lex (tree *value, location_t *loc)
39068 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39069 enum cpp_ttype ret = tok->type;
39071 *value = tok->u.value;
39073 *loc = tok->location;
39075 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39077 else if (ret == CPP_STRING)
39078 *value = cp_parser_string_literal (the_parser, false, false);
39081 if (ret == CPP_KEYWORD)
39083 cp_lexer_consume_token (the_parser->lexer);
39090 /* External interface. */
39092 /* Parse one entire translation unit. */
39095 c_parse_file (void)
39097 static bool already_called = false;
39099 if (already_called)
39100 fatal_error (input_location,
39101 "inter-module optimizations not implemented for C++");
39102 already_called = true;
39104 the_parser = cp_parser_new ();
39105 push_deferring_access_checks (flag_access_control
39106 ? dk_no_deferred : dk_no_check);
39107 cp_parser_translation_unit (the_parser);
39111 /* Create an identifier for a generic parameter type (a synthesized
39112 template parameter implied by `auto' or a concept identifier). */
39114 static GTY(()) int generic_parm_count;
39116 make_generic_type_name ()
39119 sprintf (buf, "auto:%d", ++generic_parm_count);
39120 return get_identifier (buf);
39123 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39124 (creating a new template parameter list if necessary). Returns the newly
39125 created template type parm. */
39128 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39130 gcc_assert (current_binding_level->kind == sk_function_parms);
39132 /* Before committing to modifying any scope, if we're in an
39133 implicit template scope, and we're trying to synthesize a
39134 constrained parameter, try to find a previous parameter with
39135 the same name. This is the same-type rule for abbreviated
39136 function templates.
39138 NOTE: We can generate implicit parameters when tentatively
39139 parsing a nested name specifier, only to reject that parse
39140 later. However, matching the same template-id as part of a
39141 direct-declarator should generate an identical template
39142 parameter, so this rule will merge them. */
39143 if (parser->implicit_template_scope && constr)
39145 tree t = parser->implicit_template_parms;
39148 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39150 tree d = TREE_VALUE (t);
39151 if (TREE_CODE (d) == PARM_DECL)
39152 /* Return the TEMPLATE_PARM_INDEX. */
39153 d = DECL_INITIAL (d);
39156 t = TREE_CHAIN (t);
39160 /* We are either continuing a function template that already contains implicit
39161 template parameters, creating a new fully-implicit function template, or
39162 extending an existing explicit function template with implicit template
39165 cp_binding_level *const entry_scope = current_binding_level;
39167 bool become_template = false;
39168 cp_binding_level *parent_scope = 0;
39170 if (parser->implicit_template_scope)
39172 gcc_assert (parser->implicit_template_parms);
39174 current_binding_level = parser->implicit_template_scope;
39178 /* Roll back to the existing template parameter scope (in the case of
39179 extending an explicit function template) or introduce a new template
39180 parameter scope ahead of the function parameter scope (or class scope
39181 in the case of out-of-line member definitions). The function scope is
39182 added back after template parameter synthesis below. */
39184 cp_binding_level *scope = entry_scope;
39186 while (scope->kind == sk_function_parms)
39188 parent_scope = scope;
39189 scope = scope->level_chain;
39191 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39193 /* If not defining a class, then any class scope is a scope level in
39194 an out-of-line member definition. In this case simply wind back
39195 beyond the first such scope to inject the template parameter list.
39196 Otherwise wind back to the class being defined. The latter can
39197 occur in class member friend declarations such as:
39203 friend void A::foo (auto);
39206 The template parameter list synthesized for the friend declaration
39207 must be injected in the scope of 'B'. This can also occur in
39208 erroneous cases such as:
39214 void B::foo (auto) {}
39217 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39218 but, nevertheless, the template parameter list synthesized for the
39219 declarator should be injected into the scope of 'A' as if the
39220 ill-formed template was specified explicitly. */
39222 while (scope->kind == sk_class && !scope->defining_class_p)
39224 parent_scope = scope;
39225 scope = scope->level_chain;
39229 current_binding_level = scope;
39231 if (scope->kind != sk_template_parms
39232 || !function_being_declared_is_template_p (parser))
39234 /* Introduce a new template parameter list for implicit template
39237 become_template = true;
39239 parser->implicit_template_scope
39240 = begin_scope (sk_template_parms, NULL);
39242 ++processing_template_decl;
39244 parser->fully_implicit_function_template_p = true;
39245 ++parser->num_template_parameter_lists;
39249 /* Synthesize implicit template parameters at the end of the explicit
39250 template parameter list. */
39252 gcc_assert (current_template_parms);
39254 parser->implicit_template_scope = scope;
39256 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39257 parser->implicit_template_parms
39258 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39262 /* Synthesize a new template parameter and track the current template
39263 parameter chain with implicit_template_parms. */
39265 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39266 tree synth_id = make_generic_type_name ();
39267 tree synth_tmpl_parm;
39268 bool non_type = false;
39270 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39272 = finish_template_type_parm (class_type_node, synth_id);
39273 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39275 = finish_constrained_template_template_parm (proto, synth_id);
39278 synth_tmpl_parm = copy_decl (proto);
39279 DECL_NAME (synth_tmpl_parm) = synth_id;
39283 // Attach the constraint to the parm before processing.
39284 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39285 TREE_TYPE (node) = constr;
39287 = process_template_parm (parser->implicit_template_parms,
39290 /*non_type=*/non_type,
39291 /*param_pack=*/false);
39293 // Chain the new parameter to the list of implicit parameters.
39294 if (parser->implicit_template_parms)
39295 parser->implicit_template_parms
39296 = TREE_CHAIN (parser->implicit_template_parms);
39298 parser->implicit_template_parms = new_parm;
39300 tree new_decl = get_local_decls ();
39302 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39303 new_decl = DECL_INITIAL (new_decl);
39305 /* If creating a fully implicit function template, start the new implicit
39306 template parameter list with this synthesized type, otherwise grow the
39307 current template parameter list. */
39309 if (become_template)
39311 parent_scope->level_chain = current_binding_level;
39313 tree new_parms = make_tree_vec (1);
39314 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39315 current_template_parms = tree_cons (size_int (processing_template_decl),
39316 new_parms, current_template_parms);
39320 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39321 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39322 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39323 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39326 // If the new parameter was constrained, we need to add that to the
39327 // constraints in the template parameter list.
39328 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39330 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39331 reqs = conjoin_constraints (reqs, req);
39332 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39335 current_binding_level = entry_scope;
39340 /* Finish the declaration of a fully implicit function template. Such a
39341 template has no explicit template parameter list so has not been through the
39342 normal template head and tail processing. synthesize_implicit_template_parm
39343 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39344 provided if the declaration is a class member such that its template
39345 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39346 form is returned. Otherwise NULL_TREE is returned. */
39349 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39351 gcc_assert (parser->fully_implicit_function_template_p);
39353 if (member_decl_opt && member_decl_opt != error_mark_node
39354 && DECL_VIRTUAL_P (member_decl_opt))
39356 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39357 "implicit templates may not be %<virtual%>");
39358 DECL_VIRTUAL_P (member_decl_opt) = false;
39361 if (member_decl_opt)
39362 member_decl_opt = finish_member_template_decl (member_decl_opt);
39363 end_template_decl ();
39365 parser->fully_implicit_function_template_p = false;
39366 parser->implicit_template_parms = 0;
39367 parser->implicit_template_scope = 0;
39368 --parser->num_template_parameter_lists;
39370 return member_decl_opt;
39373 /* Like finish_fully_implicit_template, but to be used in error
39374 recovery, rearranging scopes so that we restore the state we had
39375 before synthesize_implicit_template_parm inserted the implement
39376 template parms scope. */
39379 abort_fully_implicit_template (cp_parser *parser)
39381 cp_binding_level *return_to_scope = current_binding_level;
39383 if (parser->implicit_template_scope
39384 && return_to_scope != parser->implicit_template_scope)
39386 cp_binding_level *child = return_to_scope;
39387 for (cp_binding_level *scope = child->level_chain;
39388 scope != parser->implicit_template_scope;
39389 scope = child->level_chain)
39391 child->level_chain = parser->implicit_template_scope->level_chain;
39392 parser->implicit_template_scope->level_chain = return_to_scope;
39393 current_binding_level = parser->implicit_template_scope;
39396 return_to_scope = return_to_scope->level_chain;
39398 finish_fully_implicit_template (parser, NULL);
39400 gcc_assert (current_binding_level == return_to_scope);
39403 /* Helper function for diagnostics that have complained about things
39404 being used with 'extern "C"' linkage.
39406 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39409 maybe_show_extern_c_location (void)
39411 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39412 inform (the_parser->innermost_linkage_specification_location,
39413 "%<extern \"C\"%> linkage started here");
39416 #include "gt-cp-parser.h"