2 Copyright (C) 2000-2016 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/>. */
23 #include "coretypes.h"
25 #include "c-family/c-common.h"
27 #include "stringpool.h"
29 #include "print-tree.h"
31 #include "trans-mem.h"
34 #include "c-family/c-objc.h"
36 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "c-family/c-indentation.h"
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token =
51 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
54 /* The various kinds of non integral constant we encounter. */
55 enum non_integral_constant {
57 /* floating-point literal */
61 /* %<__FUNCTION__%> */
63 /* %<__PRETTY_FUNCTION__%> */
71 /* %<typeid%> operator */
73 /* non-constant compound literals */
81 /* an array reference */
87 /* the address of a label */
101 /* calls to overloaded operators */
105 /* a comma operator */
107 /* a call to a constructor */
109 /* a transaction expression */
113 /* The various kinds of errors about name-lookup failing. */
114 enum name_lookup_error {
119 /* is not a class or namespace */
121 /* is not a class, namespace, or enumeration */
125 /* The various kinds of required token */
126 enum required_token {
128 RT_SEMICOLON, /* ';' */
129 RT_OPEN_PAREN, /* '(' */
130 RT_CLOSE_BRACE, /* '}' */
131 RT_OPEN_BRACE, /* '{' */
132 RT_CLOSE_SQUARE, /* ']' */
133 RT_OPEN_SQUARE, /* '[' */
137 RT_GREATER, /* '>' */
139 RT_ELLIPSIS, /* '...' */
143 RT_COLON_SCOPE, /* ':' or '::' */
144 RT_CLOSE_PAREN, /* ')' */
145 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146 RT_PRAGMA_EOL, /* end of line */
147 RT_NAME, /* identifier */
149 /* The type is CPP_KEYWORD */
151 RT_DELETE, /* delete */
152 RT_RETURN, /* return */
153 RT_WHILE, /* while */
154 RT_EXTERN, /* extern */
155 RT_STATIC_ASSERT, /* static_assert */
156 RT_DECLTYPE, /* decltype */
157 RT_OPERATOR, /* operator */
158 RT_CLASS, /* class */
159 RT_TEMPLATE, /* template */
160 RT_NAMESPACE, /* namespace */
161 RT_USING, /* using */
164 RT_CATCH, /* catch */
165 RT_THROW, /* throw */
166 RT_LABEL, /* __label__ */
167 RT_AT_TRY, /* @try */
168 RT_AT_SYNCHRONIZED, /* @synchronized */
169 RT_AT_THROW, /* @throw */
171 RT_SELECT, /* selection-statement */
172 RT_INTERATION, /* iteration-statement */
173 RT_JUMP, /* jump-statement */
174 RT_CLASS_KEY, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL /* __transaction_cancel */
181 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
182 reverting it on destruction. */
184 class type_id_in_expr_sentinel
189 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
191 saved (parser->in_type_id_in_expr_p)
192 { parser->in_type_id_in_expr_p = set; }
193 ~type_id_in_expr_sentinel ()
194 { parser->in_type_id_in_expr_p = saved; }
199 static cp_lexer *cp_lexer_new_main
201 static cp_lexer *cp_lexer_new_from_tokens
202 (cp_token_cache *tokens);
203 static void cp_lexer_destroy
205 static int cp_lexer_saving_tokens
207 static cp_token *cp_lexer_token_at
208 (cp_lexer *, cp_token_position);
209 static void cp_lexer_get_preprocessor_token
210 (cp_lexer *, cp_token *);
211 static inline cp_token *cp_lexer_peek_token
213 static cp_token *cp_lexer_peek_nth_token
214 (cp_lexer *, size_t);
215 static inline bool cp_lexer_next_token_is
216 (cp_lexer *, enum cpp_ttype);
217 static bool cp_lexer_next_token_is_not
218 (cp_lexer *, enum cpp_ttype);
219 static bool cp_lexer_next_token_is_keyword
220 (cp_lexer *, enum rid);
221 static cp_token *cp_lexer_consume_token
223 static void cp_lexer_purge_token
225 static void cp_lexer_purge_tokens_after
226 (cp_lexer *, cp_token_position);
227 static void cp_lexer_save_tokens
229 static void cp_lexer_commit_tokens
231 static void cp_lexer_rollback_tokens
233 static void cp_lexer_print_token
234 (FILE *, cp_token *);
235 static inline bool cp_lexer_debugging_p
237 static void cp_lexer_start_debugging
238 (cp_lexer *) ATTRIBUTE_UNUSED;
239 static void cp_lexer_stop_debugging
240 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static cp_token_cache *cp_token_cache_new
243 (cp_token *, cp_token *);
245 static void cp_parser_initial_pragma
248 static tree cp_literal_operator_id
251 static void cp_parser_cilk_simd
252 (cp_parser *, cp_token *, bool *);
253 static tree cp_parser_cilk_for
254 (cp_parser *, tree, bool *);
255 static bool cp_parser_omp_declare_reduction_exprs
257 static tree cp_parser_cilk_simd_vectorlength
258 (cp_parser *, tree, bool);
259 static void cp_finalize_oacc_routine
260 (cp_parser *, tree, bool);
262 /* Manifest constants. */
263 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
264 #define CP_SAVED_TOKEN_STACK 5
268 /* The stream to which debugging output should be written. */
269 static FILE *cp_lexer_debug_stream;
271 /* Nonzero if we are parsing an unevaluated operand: an operand to
272 sizeof, typeof, or alignof. */
273 int cp_unevaluated_operand;
275 /* Dump up to NUM tokens in BUFFER to FILE starting with token
276 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
277 first token in BUFFER. If NUM is 0, dump all the tokens. If
278 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
279 highlighted by surrounding it in [[ ]]. */
282 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
283 cp_token *start_token, unsigned num,
284 cp_token *curr_token)
286 unsigned i, nprinted;
290 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
296 num = buffer->length ();
298 if (start_token == NULL)
299 start_token = buffer->address ();
301 if (start_token > buffer->address ())
303 cp_lexer_print_token (file, &(*buffer)[0]);
304 fprintf (file, " ... ");
309 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
311 if (token == start_token)
318 if (token == curr_token)
319 fprintf (file, "[[");
321 cp_lexer_print_token (file, token);
323 if (token == curr_token)
324 fprintf (file, "]]");
330 case CPP_CLOSE_BRACE:
340 if (i == num && i < buffer->length ())
342 fprintf (file, " ... ");
343 cp_lexer_print_token (file, &buffer->last ());
346 fprintf (file, "\n");
350 /* Dump all tokens in BUFFER to stderr. */
353 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
355 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
359 debug (vec<cp_token, va_gc> &ref)
361 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
365 debug (vec<cp_token, va_gc> *ptr)
370 fprintf (stderr, "<nil>\n");
374 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
375 description for T. */
378 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
382 fprintf (file, "%s: ", desc);
383 print_node_brief (file, "", t, 0);
388 /* Dump parser context C to FILE. */
391 cp_debug_print_context (FILE *file, cp_parser_context *c)
393 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
394 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
395 print_node_brief (file, "", c->object_type, 0);
396 fprintf (file, "}\n");
400 /* Print the stack of parsing contexts to FILE starting with FIRST. */
403 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
406 cp_parser_context *c;
408 fprintf (file, "Parsing context stack:\n");
409 for (i = 0, c = first; c; c = c->next, i++)
411 fprintf (file, "\t#%u: ", i);
412 cp_debug_print_context (file, c);
417 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
420 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
423 fprintf (file, "%s: true\n", desc);
427 /* Print an unparsed function entry UF to FILE. */
430 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
433 cp_default_arg_entry *default_arg_fn;
436 fprintf (file, "\tFunctions with default args:\n");
438 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
441 fprintf (file, "\t\tClass type: ");
442 print_node_brief (file, "", default_arg_fn->class_type, 0);
443 fprintf (file, "\t\tDeclaration: ");
444 print_node_brief (file, "", default_arg_fn->decl, 0);
445 fprintf (file, "\n");
448 fprintf (file, "\n\tFunctions with definitions that require "
449 "post-processing\n\t\t");
450 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
452 print_node_brief (file, "", fn, 0);
455 fprintf (file, "\n");
457 fprintf (file, "\n\tNon-static data members with initializers that require "
458 "post-processing\n\t\t");
459 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
461 print_node_brief (file, "", fn, 0);
464 fprintf (file, "\n");
468 /* Print the stack of unparsed member functions S to FILE. */
471 cp_debug_print_unparsed_queues (FILE *file,
472 vec<cp_unparsed_functions_entry, va_gc> *s)
475 cp_unparsed_functions_entry *uf;
477 fprintf (file, "Unparsed functions\n");
478 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
480 fprintf (file, "#%u:\n", i);
481 cp_debug_print_unparsed_function (file, uf);
486 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
487 the given PARSER. If FILE is NULL, the output is printed on stderr. */
490 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
492 cp_token *next_token, *first_token, *start_token;
497 next_token = parser->lexer->next_token;
498 first_token = parser->lexer->buffer->address ();
499 start_token = (next_token > first_token + window_size / 2)
500 ? next_token - window_size / 2
502 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
507 /* Dump debugging information for the given PARSER. If FILE is NULL,
508 the output is printed on stderr. */
511 cp_debug_parser (FILE *file, cp_parser *parser)
513 const size_t window_size = 20;
515 expanded_location eloc;
520 fprintf (file, "Parser state\n\n");
521 fprintf (file, "Number of tokens: %u\n",
522 vec_safe_length (parser->lexer->buffer));
523 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
524 cp_debug_print_tree_if_set (file, "Object scope",
525 parser->object_scope);
526 cp_debug_print_tree_if_set (file, "Qualifying scope",
527 parser->qualifying_scope);
528 cp_debug_print_context_stack (file, parser->context);
529 cp_debug_print_flag (file, "Allow GNU extensions",
530 parser->allow_gnu_extensions_p);
531 cp_debug_print_flag (file, "'>' token is greater-than",
532 parser->greater_than_is_operator_p);
533 cp_debug_print_flag (file, "Default args allowed in current "
534 "parameter list", parser->default_arg_ok_p);
535 cp_debug_print_flag (file, "Parsing integral constant-expression",
536 parser->integral_constant_expression_p);
537 cp_debug_print_flag (file, "Allow non-constant expression in current "
538 "constant-expression",
539 parser->allow_non_integral_constant_expression_p);
540 cp_debug_print_flag (file, "Seen non-constant expression",
541 parser->non_integral_constant_expression_p);
542 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544 parser->local_variables_forbidden_p);
545 cp_debug_print_flag (file, "In unbraced linkage specification",
546 parser->in_unbraced_linkage_specification_p);
547 cp_debug_print_flag (file, "Parsing a declarator",
548 parser->in_declarator_p);
549 cp_debug_print_flag (file, "In template argument list",
550 parser->in_template_argument_list_p);
551 cp_debug_print_flag (file, "Parsing an iteration statement",
552 parser->in_statement & IN_ITERATION_STMT);
553 cp_debug_print_flag (file, "Parsing a switch statement",
554 parser->in_statement & IN_SWITCH_STMT);
555 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
556 parser->in_statement & IN_OMP_BLOCK);
557 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
558 parser->in_statement & IN_CILK_SIMD_FOR);
559 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
560 parser->in_statement & IN_OMP_FOR);
561 cp_debug_print_flag (file, "Parsing an if statement",
562 parser->in_statement & IN_IF_STMT);
563 cp_debug_print_flag (file, "Parsing a type-id in an expression "
564 "context", parser->in_type_id_in_expr_p);
565 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
566 parser->implicit_extern_c);
567 cp_debug_print_flag (file, "String expressions should be translated "
568 "to execution character set",
569 parser->translate_strings_p);
570 cp_debug_print_flag (file, "Parsing function body outside of a "
571 "local class", parser->in_function_body);
572 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
573 parser->colon_corrects_to_scope_p);
574 cp_debug_print_flag (file, "Colon doesn't start a class definition",
575 parser->colon_doesnt_start_class_def_p);
576 if (parser->type_definition_forbidden_message)
577 fprintf (file, "Error message for forbidden type definitions: %s\n",
578 parser->type_definition_forbidden_message);
579 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
580 fprintf (file, "Number of class definitions in progress: %u\n",
581 parser->num_classes_being_defined);
582 fprintf (file, "Number of template parameter lists for the current "
583 "declaration: %u\n", parser->num_template_parameter_lists);
584 cp_debug_parser_tokens (file, parser, window_size);
585 token = parser->lexer->next_token;
586 fprintf (file, "Next token to parse:\n");
587 fprintf (file, "\tToken: ");
588 cp_lexer_print_token (file, token);
589 eloc = expand_location (token->location);
590 fprintf (file, "\n\tFile: %s\n", eloc.file);
591 fprintf (file, "\tLine: %d\n", eloc.line);
592 fprintf (file, "\tColumn: %d\n", eloc.column);
596 debug (cp_parser &ref)
598 cp_debug_parser (stderr, &ref);
602 debug (cp_parser *ptr)
607 fprintf (stderr, "<nil>\n");
610 /* Allocate memory for a new lexer object and return it. */
613 cp_lexer_alloc (void)
617 c_common_no_more_pch ();
619 /* Allocate the memory. */
620 lexer = ggc_cleared_alloc<cp_lexer> ();
622 /* Initially we are not debugging. */
623 lexer->debugging_p = false;
625 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
627 /* Create the buffer. */
628 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
634 /* Create a new main C++ lexer, the lexer that gets tokens from the
638 cp_lexer_new_main (void)
643 /* It's possible that parsing the first pragma will load a PCH file,
644 which is a GC collection point. So we have to do that before
645 allocating any memory. */
646 cp_parser_initial_pragma (&token);
648 lexer = cp_lexer_alloc ();
650 /* Put the first token in the buffer. */
651 lexer->buffer->quick_push (token);
653 /* Get the remaining tokens from the preprocessor. */
654 while (token.type != CPP_EOF)
656 cp_lexer_get_preprocessor_token (lexer, &token);
657 vec_safe_push (lexer->buffer, token);
660 lexer->last_token = lexer->buffer->address ()
661 + lexer->buffer->length ()
663 lexer->next_token = lexer->buffer->length ()
664 ? lexer->buffer->address ()
667 /* Subsequent preprocessor diagnostics should use compiler
668 diagnostic functions to get the compiler source location. */
671 gcc_assert (!lexer->next_token->purged_p);
675 /* Create a new lexer whose token stream is primed with the tokens in
676 CACHE. When these tokens are exhausted, no new tokens will be read. */
679 cp_lexer_new_from_tokens (cp_token_cache *cache)
681 cp_token *first = cache->first;
682 cp_token *last = cache->last;
683 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
685 /* We do not own the buffer. */
686 lexer->buffer = NULL;
687 lexer->next_token = first == last ? &eof_token : first;
688 lexer->last_token = last;
690 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
692 /* Initially we are not debugging. */
693 lexer->debugging_p = false;
695 gcc_assert (!lexer->next_token->purged_p);
699 /* Frees all resources associated with LEXER. */
702 cp_lexer_destroy (cp_lexer *lexer)
704 vec_free (lexer->buffer);
705 lexer->saved_tokens.release ();
709 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
710 be used. The point of this flag is to help the compiler to fold away calls
711 to cp_lexer_debugging_p within this source file at compile time, when the
712 lexer is not being debugged. */
714 #define LEXER_DEBUGGING_ENABLED_P false
716 /* Returns nonzero if debugging information should be output. */
719 cp_lexer_debugging_p (cp_lexer *lexer)
721 if (!LEXER_DEBUGGING_ENABLED_P)
724 return lexer->debugging_p;
728 static inline cp_token_position
729 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
731 gcc_assert (!previous_p || lexer->next_token != &eof_token);
733 return lexer->next_token - previous_p;
736 static inline cp_token *
737 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
743 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
745 lexer->next_token = cp_lexer_token_at (lexer, pos);
748 static inline cp_token_position
749 cp_lexer_previous_token_position (cp_lexer *lexer)
751 if (lexer->next_token == &eof_token)
752 return lexer->last_token - 1;
754 return cp_lexer_token_position (lexer, true);
757 static inline cp_token *
758 cp_lexer_previous_token (cp_lexer *lexer)
760 cp_token_position tp = cp_lexer_previous_token_position (lexer);
762 /* Skip past purged tokens. */
765 gcc_assert (tp != vec_safe_address (lexer->buffer));
769 return cp_lexer_token_at (lexer, tp);
772 /* nonzero if we are presently saving tokens. */
775 cp_lexer_saving_tokens (const cp_lexer* lexer)
777 return lexer->saved_tokens.length () != 0;
780 /* Store the next token from the preprocessor in *TOKEN. Return true
781 if we reach EOF. If LEXER is NULL, assume we are handling an
782 initial #pragma pch_preprocess, and thus want the lexer to return
783 processed strings. */
786 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
788 static int is_extern_c = 0;
790 /* Get a new token from the preprocessor. */
792 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
793 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
794 token->keyword = RID_MAX;
795 token->purged_p = false;
796 token->error_reported = false;
798 /* On some systems, some header files are surrounded by an
799 implicit extern "C" block. Set a flag in the token if it
800 comes from such a header. */
801 is_extern_c += pending_lang_change;
802 pending_lang_change = 0;
803 token->implicit_extern_c = is_extern_c > 0;
805 /* Check to see if this token is a keyword. */
806 if (token->type == CPP_NAME)
808 if (C_IS_RESERVED_WORD (token->u.value))
810 /* Mark this token as a keyword. */
811 token->type = CPP_KEYWORD;
812 /* Record which keyword. */
813 token->keyword = C_RID_CODE (token->u.value);
817 if (warn_cxx11_compat
818 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
819 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
821 /* Warn about the C++0x keyword (but still treat it as
823 warning (OPT_Wc__11_compat,
824 "identifier %qE is a keyword in C++11",
827 /* Clear out the C_RID_CODE so we don't warn about this
828 particular identifier-turned-keyword again. */
829 C_SET_RID_CODE (token->u.value, RID_MAX);
832 token->keyword = RID_MAX;
835 else if (token->type == CPP_AT_NAME)
837 /* This only happens in Objective-C++; it must be a keyword. */
838 token->type = CPP_KEYWORD;
839 switch (C_RID_CODE (token->u.value))
841 /* Replace 'class' with '@class', 'private' with '@private',
842 etc. This prevents confusion with the C++ keyword
843 'class', and makes the tokens consistent with other
844 Objective-C 'AT' keywords. For example '@class' is
845 reported as RID_AT_CLASS which is consistent with
846 '@synchronized', which is reported as
849 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
850 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
851 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
852 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
853 case RID_THROW: token->keyword = RID_AT_THROW; break;
854 case RID_TRY: token->keyword = RID_AT_TRY; break;
855 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
856 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
857 default: token->keyword = C_RID_CODE (token->u.value);
862 /* Update the globals input_location and the input file stack from TOKEN. */
864 cp_lexer_set_source_position_from_token (cp_token *token)
866 if (token->type != CPP_EOF)
868 input_location = token->location;
872 /* Update the globals input_location and the input file stack from LEXER. */
874 cp_lexer_set_source_position (cp_lexer *lexer)
876 cp_token *token = cp_lexer_peek_token (lexer);
877 cp_lexer_set_source_position_from_token (token);
880 /* Return a pointer to the next token in the token stream, but do not
883 static inline cp_token *
884 cp_lexer_peek_token (cp_lexer *lexer)
886 if (cp_lexer_debugging_p (lexer))
888 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
889 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
890 putc ('\n', cp_lexer_debug_stream);
892 return lexer->next_token;
895 /* Return true if the next token has the indicated TYPE. */
898 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
900 return cp_lexer_peek_token (lexer)->type == type;
903 /* Return true if the next token does not have the indicated TYPE. */
906 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
908 return !cp_lexer_next_token_is (lexer, type);
911 /* Return true if the next token is the indicated KEYWORD. */
914 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
916 return cp_lexer_peek_token (lexer)->keyword == keyword;
920 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
922 return cp_lexer_peek_nth_token (lexer, n)->type == type;
926 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
928 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
931 /* Return true if the next token is not the indicated KEYWORD. */
934 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
936 return cp_lexer_peek_token (lexer)->keyword != keyword;
939 /* Return true if the next token is a keyword for a decl-specifier. */
942 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
946 token = cp_lexer_peek_token (lexer);
947 switch (token->keyword)
949 /* auto specifier: storage-class-specifier in C++,
950 simple-type-specifier in C++0x. */
952 /* Storage classes. */
958 /* Elaborated type specifiers. */
964 /* Simple type specifiers. */
978 /* GNU extensions. */
981 /* C++0x extensions. */
983 case RID_UNDERLYING_TYPE:
987 if (token->keyword >= RID_FIRST_INT_N
988 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
989 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
995 /* Returns TRUE iff the token T begins a decltype type. */
998 token_is_decltype (cp_token *t)
1000 return (t->keyword == RID_DECLTYPE
1001 || t->type == CPP_DECLTYPE);
1004 /* Returns TRUE iff the next token begins a decltype type. */
1007 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1009 cp_token *t = cp_lexer_peek_token (lexer);
1010 return token_is_decltype (t);
1013 /* Called when processing a token with tree_check_value; perform or defer the
1014 associated checks and return the value. */
1017 saved_checks_value (struct tree_check *check_value)
1019 /* Perform any access checks that were deferred. */
1020 vec<deferred_access_check, va_gc> *checks;
1021 deferred_access_check *chk;
1022 checks = check_value->checks;
1026 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1027 perform_or_defer_access_check (chk->binfo,
1029 chk->diag_decl, tf_warning_or_error);
1031 /* Return the stored value. */
1032 return check_value->value;
1035 /* Return a pointer to the Nth token in the token stream. If N is 1,
1036 then this is precisely equivalent to cp_lexer_peek_token (except
1037 that it is not inline). One would like to disallow that case, but
1038 there is one case (cp_parser_nth_token_starts_template_id) where
1039 the caller passes a variable for N and it might be 1. */
1042 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1046 /* N is 1-based, not zero-based. */
1049 if (cp_lexer_debugging_p (lexer))
1050 fprintf (cp_lexer_debug_stream,
1051 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1054 token = lexer->next_token;
1055 gcc_assert (!n || token != &eof_token);
1059 if (token == lexer->last_token)
1065 if (!token->purged_p)
1069 if (cp_lexer_debugging_p (lexer))
1071 cp_lexer_print_token (cp_lexer_debug_stream, token);
1072 putc ('\n', cp_lexer_debug_stream);
1078 /* Return the next token, and advance the lexer's next_token pointer
1079 to point to the next non-purged token. */
1082 cp_lexer_consume_token (cp_lexer* lexer)
1084 cp_token *token = lexer->next_token;
1086 gcc_assert (token != &eof_token);
1087 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1091 lexer->next_token++;
1092 if (lexer->next_token == lexer->last_token)
1094 lexer->next_token = &eof_token;
1099 while (lexer->next_token->purged_p);
1101 cp_lexer_set_source_position_from_token (token);
1103 /* Provide debugging output. */
1104 if (cp_lexer_debugging_p (lexer))
1106 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1107 cp_lexer_print_token (cp_lexer_debug_stream, token);
1108 putc ('\n', cp_lexer_debug_stream);
1114 /* Permanently remove the next token from the token stream, and
1115 advance the next_token pointer to refer to the next non-purged
1119 cp_lexer_purge_token (cp_lexer *lexer)
1121 cp_token *tok = lexer->next_token;
1123 gcc_assert (tok != &eof_token);
1124 tok->purged_p = true;
1125 tok->location = UNKNOWN_LOCATION;
1126 tok->u.value = NULL_TREE;
1127 tok->keyword = RID_MAX;
1132 if (tok == lexer->last_token)
1138 while (tok->purged_p);
1139 lexer->next_token = tok;
1142 /* Permanently remove all tokens after TOK, up to, but not
1143 including, the token that will be returned next by
1144 cp_lexer_peek_token. */
1147 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1149 cp_token *peek = lexer->next_token;
1151 if (peek == &eof_token)
1152 peek = lexer->last_token;
1154 gcc_assert (tok < peek);
1156 for ( tok += 1; tok != peek; tok += 1)
1158 tok->purged_p = true;
1159 tok->location = UNKNOWN_LOCATION;
1160 tok->u.value = NULL_TREE;
1161 tok->keyword = RID_MAX;
1165 /* Begin saving tokens. All tokens consumed after this point will be
1169 cp_lexer_save_tokens (cp_lexer* lexer)
1171 /* Provide debugging output. */
1172 if (cp_lexer_debugging_p (lexer))
1173 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1175 lexer->saved_tokens.safe_push (lexer->next_token);
1178 /* Commit to the portion of the token stream most recently saved. */
1181 cp_lexer_commit_tokens (cp_lexer* lexer)
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer))
1185 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1187 lexer->saved_tokens.pop ();
1190 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1191 to the token stream. Stop saving tokens. */
1194 cp_lexer_rollback_tokens (cp_lexer* lexer)
1196 /* Provide debugging output. */
1197 if (cp_lexer_debugging_p (lexer))
1198 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1200 lexer->next_token = lexer->saved_tokens.pop ();
1203 /* RAII wrapper around the above functions, with sanity checking. Creating
1204 a variable saves tokens, which are committed when the variable is
1205 destroyed unless they are explicitly rolled back by calling the rollback
1208 struct saved_token_sentinel
1213 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1215 len = lexer->saved_tokens.length ();
1216 cp_lexer_save_tokens (lexer);
1220 cp_lexer_rollback_tokens (lexer);
1223 ~saved_token_sentinel()
1226 cp_lexer_commit_tokens (lexer);
1227 gcc_assert (lexer->saved_tokens.length () == len);
1231 /* Print a representation of the TOKEN on the STREAM. */
1234 cp_lexer_print_token (FILE * stream, cp_token *token)
1236 /* We don't use cpp_type2name here because the parser defines
1237 a few tokens of its own. */
1238 static const char *const token_names[] = {
1239 /* cpplib-defined token types */
1240 #define OP(e, s) #e,
1241 #define TK(e, s) #e,
1245 /* C++ parser token types - see "Manifest constants", above. */
1248 "NESTED_NAME_SPECIFIER",
1251 /* For some tokens, print the associated data. */
1252 switch (token->type)
1255 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1256 For example, `struct' is mapped to an INTEGER_CST. */
1257 if (!identifier_p (token->u.value))
1259 /* else fall through */
1261 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1268 case CPP_UTF8STRING:
1269 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 print_generic_expr (stream, token->u.value, 0);
1277 /* If we have a name for the token, print it out. Otherwise, we
1278 simply give the numeric code. */
1279 if (token->type < ARRAY_SIZE(token_names))
1280 fputs (token_names[token->type], stream);
1282 fprintf (stream, "[%d]", token->type);
1288 debug (cp_token &ref)
1290 cp_lexer_print_token (stderr, &ref);
1291 fprintf (stderr, "\n");
1295 debug (cp_token *ptr)
1300 fprintf (stderr, "<nil>\n");
1304 /* Start emitting debugging information. */
1307 cp_lexer_start_debugging (cp_lexer* lexer)
1309 if (!LEXER_DEBUGGING_ENABLED_P)
1310 fatal_error (input_location,
1311 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1313 lexer->debugging_p = true;
1314 cp_lexer_debug_stream = stderr;
1317 /* Stop emitting debugging information. */
1320 cp_lexer_stop_debugging (cp_lexer* lexer)
1322 if (!LEXER_DEBUGGING_ENABLED_P)
1323 fatal_error (input_location,
1324 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1326 lexer->debugging_p = false;
1327 cp_lexer_debug_stream = NULL;
1330 /* Create a new cp_token_cache, representing a range of tokens. */
1332 static cp_token_cache *
1333 cp_token_cache_new (cp_token *first, cp_token *last)
1335 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1336 cache->first = first;
1341 /* Diagnose if #pragma omp declare simd isn't followed immediately
1342 by function declaration or definition. */
1345 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1347 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1349 error ("%<#pragma omp declare simd%> not immediately followed by "
1350 "function declaration or definition");
1351 parser->omp_declare_simd = NULL;
1355 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1356 and put that into "omp declare simd" attribute. */
1359 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1361 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1363 if (fndecl == error_mark_node)
1365 parser->omp_declare_simd = NULL;
1368 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1370 cp_ensure_no_omp_declare_simd (parser);
1376 /* Diagnose if #pragma acc routine isn't followed immediately by function
1377 declaration or definition. */
1380 cp_ensure_no_oacc_routine (cp_parser *parser)
1382 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1384 tree clauses = parser->oacc_routine->clauses;
1385 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
1387 error_at (loc, "%<#pragma acc routine%> not followed by a function "
1388 "declaration or definition");
1389 parser->oacc_routine = NULL;
1393 /* Decl-specifiers. */
1395 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1400 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1405 /* Nothing other than the parser should be creating declarators;
1406 declarators are a semi-syntactic representation of C++ entities.
1407 Other parts of the front end that need to create entities (like
1408 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1410 static cp_declarator *make_call_declarator
1411 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1412 static cp_declarator *make_array_declarator
1413 (cp_declarator *, tree);
1414 static cp_declarator *make_pointer_declarator
1415 (cp_cv_quals, cp_declarator *, tree);
1416 static cp_declarator *make_reference_declarator
1417 (cp_cv_quals, cp_declarator *, bool, tree);
1418 static cp_declarator *make_ptrmem_declarator
1419 (cp_cv_quals, tree, cp_declarator *, tree);
1421 /* An erroneous declarator. */
1422 static cp_declarator *cp_error_declarator;
1424 /* The obstack on which declarators and related data structures are
1426 static struct obstack declarator_obstack;
1428 /* Alloc BYTES from the declarator memory pool. */
1430 static inline void *
1431 alloc_declarator (size_t bytes)
1433 return obstack_alloc (&declarator_obstack, bytes);
1436 /* Allocate a declarator of the indicated KIND. Clear fields that are
1437 common to all declarators. */
1439 static cp_declarator *
1440 make_declarator (cp_declarator_kind kind)
1442 cp_declarator *declarator;
1444 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1445 declarator->kind = kind;
1446 declarator->attributes = NULL_TREE;
1447 declarator->std_attributes = NULL_TREE;
1448 declarator->declarator = NULL;
1449 declarator->parameter_pack_p = false;
1450 declarator->id_loc = UNKNOWN_LOCATION;
1455 /* Make a declarator for a generalized identifier. If
1456 QUALIFYING_SCOPE is non-NULL, the identifier is
1457 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1458 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 static cp_declarator *
1462 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1463 special_function_kind sfk)
1465 cp_declarator *declarator;
1467 /* It is valid to write:
1469 class C { void f(); };
1473 The standard is not clear about whether `typedef const C D' is
1474 legal; as of 2002-09-15 the committee is considering that
1475 question. EDG 3.0 allows that syntax. Therefore, we do as
1477 if (qualifying_scope && TYPE_P (qualifying_scope))
1478 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1480 gcc_assert (identifier_p (unqualified_name)
1481 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1482 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1484 declarator = make_declarator (cdk_id);
1485 declarator->u.id.qualifying_scope = qualifying_scope;
1486 declarator->u.id.unqualified_name = unqualified_name;
1487 declarator->u.id.sfk = sfk;
1492 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1493 of modifiers such as const or volatile to apply to the pointer
1494 type, represented as identifiers. ATTRIBUTES represent the attributes that
1495 appertain to the pointer or reference. */
1498 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 cp_declarator *declarator;
1503 declarator = make_declarator (cdk_pointer);
1504 declarator->declarator = target;
1505 declarator->u.pointer.qualifiers = cv_qualifiers;
1506 declarator->u.pointer.class_type = NULL_TREE;
1509 declarator->id_loc = target->id_loc;
1510 declarator->parameter_pack_p = target->parameter_pack_p;
1511 target->parameter_pack_p = false;
1514 declarator->parameter_pack_p = false;
1516 declarator->std_attributes = attributes;
1521 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1522 represent the attributes that appertain to the pointer or
1526 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1527 bool rvalue_ref, tree attributes)
1529 cp_declarator *declarator;
1531 declarator = make_declarator (cdk_reference);
1532 declarator->declarator = target;
1533 declarator->u.reference.qualifiers = cv_qualifiers;
1534 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 declarator->id_loc = target->id_loc;
1538 declarator->parameter_pack_p = target->parameter_pack_p;
1539 target->parameter_pack_p = false;
1542 declarator->parameter_pack_p = false;
1544 declarator->std_attributes = attributes;
1549 /* Like make_pointer_declarator -- but for a pointer to a non-static
1550 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1551 appertain to the pointer or reference. */
1554 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1555 cp_declarator *pointee,
1558 cp_declarator *declarator;
1560 declarator = make_declarator (cdk_ptrmem);
1561 declarator->declarator = pointee;
1562 declarator->u.pointer.qualifiers = cv_qualifiers;
1563 declarator->u.pointer.class_type = class_type;
1567 declarator->parameter_pack_p = pointee->parameter_pack_p;
1568 pointee->parameter_pack_p = false;
1571 declarator->parameter_pack_p = false;
1573 declarator->std_attributes = attributes;
1578 /* Make a declarator for the function given by TARGET, with the
1579 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1580 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1581 indicates what exceptions can be thrown. */
1584 make_call_declarator (cp_declarator *target,
1586 cp_cv_quals cv_qualifiers,
1587 cp_virt_specifiers virt_specifiers,
1588 cp_ref_qualifier ref_qualifier,
1590 tree exception_specification,
1591 tree late_return_type,
1592 tree requires_clause)
1594 cp_declarator *declarator;
1596 declarator = make_declarator (cdk_function);
1597 declarator->declarator = target;
1598 declarator->u.function.parameters = parms;
1599 declarator->u.function.qualifiers = cv_qualifiers;
1600 declarator->u.function.virt_specifiers = virt_specifiers;
1601 declarator->u.function.ref_qualifier = ref_qualifier;
1602 declarator->u.function.tx_qualifier = tx_qualifier;
1603 declarator->u.function.exception_specification = exception_specification;
1604 declarator->u.function.late_return_type = late_return_type;
1605 declarator->u.function.requires_clause = requires_clause;
1608 declarator->id_loc = target->id_loc;
1609 declarator->parameter_pack_p = target->parameter_pack_p;
1610 target->parameter_pack_p = false;
1613 declarator->parameter_pack_p = false;
1618 /* Make a declarator for an array of BOUNDS elements, each of which is
1619 defined by ELEMENT. */
1622 make_array_declarator (cp_declarator *element, tree bounds)
1624 cp_declarator *declarator;
1626 declarator = make_declarator (cdk_array);
1627 declarator->declarator = element;
1628 declarator->u.array.bounds = bounds;
1631 declarator->id_loc = element->id_loc;
1632 declarator->parameter_pack_p = element->parameter_pack_p;
1633 element->parameter_pack_p = false;
1636 declarator->parameter_pack_p = false;
1641 /* Determine whether the declarator we've seen so far can be a
1642 parameter pack, when followed by an ellipsis. */
1644 declarator_can_be_parameter_pack (cp_declarator *declarator)
1646 if (declarator && declarator->parameter_pack_p)
1647 /* We already saw an ellipsis. */
1650 /* Search for a declarator name, or any other declarator that goes
1651 after the point where the ellipsis could appear in a parameter
1652 pack. If we find any of these, then this declarator can not be
1653 made into a parameter pack. */
1655 while (declarator && !found)
1657 switch ((int)declarator->kind)
1668 declarator = declarator->declarator;
1676 cp_parameter_declarator *no_parameters;
1678 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1679 DECLARATOR and DEFAULT_ARGUMENT. */
1681 cp_parameter_declarator *
1682 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1683 cp_declarator *declarator,
1684 tree default_argument,
1685 bool template_parameter_pack_p = false)
1687 cp_parameter_declarator *parameter;
1689 parameter = ((cp_parameter_declarator *)
1690 alloc_declarator (sizeof (cp_parameter_declarator)));
1691 parameter->next = NULL;
1692 if (decl_specifiers)
1693 parameter->decl_specifiers = *decl_specifiers;
1695 clear_decl_specs (¶meter->decl_specifiers);
1696 parameter->declarator = declarator;
1697 parameter->default_argument = default_argument;
1698 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 /* Returns true iff DECLARATOR is a declaration for a function. */
1706 function_declarator_p (const cp_declarator *declarator)
1710 if (declarator->kind == cdk_function
1711 && declarator->declarator->kind == cdk_id)
1713 if (declarator->kind == cdk_id
1714 || declarator->kind == cdk_error)
1716 declarator = declarator->declarator;
1726 A cp_parser parses the token stream as specified by the C++
1727 grammar. Its job is purely parsing, not semantic analysis. For
1728 example, the parser breaks the token stream into declarators,
1729 expressions, statements, and other similar syntactic constructs.
1730 It does not check that the types of the expressions on either side
1731 of an assignment-statement are compatible, or that a function is
1732 not declared with a parameter of type `void'.
1734 The parser invokes routines elsewhere in the compiler to perform
1735 semantic analysis and to build up the abstract syntax tree for the
1738 The parser (and the template instantiation code, which is, in a
1739 way, a close relative of parsing) are the only parts of the
1740 compiler that should be calling push_scope and pop_scope, or
1741 related functions. The parser (and template instantiation code)
1742 keeps track of what scope is presently active; everything else
1743 should simply honor that. (The code that generates static
1744 initializers may also need to set the scope, in order to check
1745 access control correctly when emitting the initializers.)
1750 The parser is of the standard recursive-descent variety. Upcoming
1751 tokens in the token stream are examined in order to determine which
1752 production to use when parsing a non-terminal. Some C++ constructs
1753 require arbitrary look ahead to disambiguate. For example, it is
1754 impossible, in the general case, to tell whether a statement is an
1755 expression or declaration without scanning the entire statement.
1756 Therefore, the parser is capable of "parsing tentatively." When the
1757 parser is not sure what construct comes next, it enters this mode.
1758 Then, while we attempt to parse the construct, the parser queues up
1759 error messages, rather than issuing them immediately, and saves the
1760 tokens it consumes. If the construct is parsed successfully, the
1761 parser "commits", i.e., it issues any queued error messages and
1762 the tokens that were being preserved are permanently discarded.
1763 If, however, the construct is not parsed successfully, the parser
1764 rolls back its state completely so that it can resume parsing using
1765 a different alternative.
1770 The performance of the parser could probably be improved substantially.
1771 We could often eliminate the need to parse tentatively by looking ahead
1772 a little bit. In some places, this approach might not entirely eliminate
1773 the need to parse tentatively, but it might still speed up the average
1776 /* Flags that are passed to some parsing functions. These values can
1777 be bitwise-ored together. */
1782 CP_PARSER_FLAGS_NONE = 0x0,
1783 /* The construct is optional. If it is not present, then no error
1784 should be issued. */
1785 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1786 /* When parsing a type-specifier, treat user-defined type-names
1787 as non-type identifiers. */
1788 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1789 /* When parsing a type-specifier, do not try to parse a class-specifier
1790 or enum-specifier. */
1791 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1792 /* When parsing a decl-specifier-seq, only allow type-specifier or
1794 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1797 /* This type is used for parameters and variables which hold
1798 combinations of the above flags. */
1799 typedef int cp_parser_flags;
1801 /* The different kinds of declarators we want to parse. */
1803 enum cp_parser_declarator_kind
1805 /* We want an abstract declarator. */
1806 CP_PARSER_DECLARATOR_ABSTRACT,
1807 /* We want a named declarator. */
1808 CP_PARSER_DECLARATOR_NAMED,
1809 /* We don't mind, but the name must be an unqualified-id. */
1810 CP_PARSER_DECLARATOR_EITHER
1813 /* The precedence values used to parse binary expressions. The minimum value
1814 of PREC must be 1, because zero is reserved to quickly discriminate
1815 binary operators from other tokens. */
1820 PREC_LOGICAL_OR_EXPRESSION,
1821 PREC_LOGICAL_AND_EXPRESSION,
1822 PREC_INCLUSIVE_OR_EXPRESSION,
1823 PREC_EXCLUSIVE_OR_EXPRESSION,
1824 PREC_AND_EXPRESSION,
1825 PREC_EQUALITY_EXPRESSION,
1826 PREC_RELATIONAL_EXPRESSION,
1827 PREC_SHIFT_EXPRESSION,
1828 PREC_ADDITIVE_EXPRESSION,
1829 PREC_MULTIPLICATIVE_EXPRESSION,
1831 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1834 /* A mapping from a token type to a corresponding tree node type, with a
1835 precedence value. */
1837 struct cp_parser_binary_operations_map_node
1839 /* The token type. */
1840 enum cpp_ttype token_type;
1841 /* The corresponding tree code. */
1842 enum tree_code tree_type;
1843 /* The precedence of this operator. */
1844 enum cp_parser_prec prec;
1847 struct cp_parser_expression_stack_entry
1849 /* Left hand side of the binary operation we are currently
1852 /* Original tree code for left hand side, if it was a binary
1853 expression itself (used for -Wparentheses). */
1854 enum tree_code lhs_type;
1855 /* Tree code for the binary operation we are parsing. */
1856 enum tree_code tree_type;
1857 /* Precedence of the binary operation we are parsing. */
1858 enum cp_parser_prec prec;
1859 /* Location of the binary operation we are parsing. */
1863 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1864 entries because precedence levels on the stack are monotonically
1866 typedef struct cp_parser_expression_stack_entry
1867 cp_parser_expression_stack[NUM_PREC_VALUES];
1871 /* Constructors and destructors. */
1873 static cp_parser_context *cp_parser_context_new
1874 (cp_parser_context *);
1876 /* Class variables. */
1878 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1880 /* The operator-precedence table used by cp_parser_binary_expression.
1881 Transformed into an associative array (binops_by_token) by
1884 static const cp_parser_binary_operations_map_node binops[] = {
1885 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1886 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1888 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1889 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1892 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1893 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1895 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1896 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1898 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1899 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1900 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1901 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1903 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1904 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1906 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1908 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1910 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1912 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1914 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1917 /* The same as binops, but initialized by cp_parser_new so that
1918 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1920 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1922 /* Constructors and destructors. */
1924 /* Construct a new context. The context below this one on the stack
1925 is given by NEXT. */
1927 static cp_parser_context *
1928 cp_parser_context_new (cp_parser_context* next)
1930 cp_parser_context *context;
1932 /* Allocate the storage. */
1933 if (cp_parser_context_free_list != NULL)
1935 /* Pull the first entry from the free list. */
1936 context = cp_parser_context_free_list;
1937 cp_parser_context_free_list = context->next;
1938 memset (context, 0, sizeof (*context));
1941 context = ggc_cleared_alloc<cp_parser_context> ();
1943 /* No errors have occurred yet in this context. */
1944 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1945 /* If this is not the bottommost context, copy information that we
1946 need from the previous context. */
1949 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1950 expression, then we are parsing one in this context, too. */
1951 context->object_type = next->object_type;
1952 /* Thread the stack. */
1953 context->next = next;
1959 /* Managing the unparsed function queues. */
1961 #define unparsed_funs_with_default_args \
1962 parser->unparsed_queues->last ().funs_with_default_args
1963 #define unparsed_funs_with_definitions \
1964 parser->unparsed_queues->last ().funs_with_definitions
1965 #define unparsed_nsdmis \
1966 parser->unparsed_queues->last ().nsdmis
1967 #define unparsed_classes \
1968 parser->unparsed_queues->last ().classes
1971 push_unparsed_function_queues (cp_parser *parser)
1973 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1974 vec_safe_push (parser->unparsed_queues, e);
1978 pop_unparsed_function_queues (cp_parser *parser)
1980 release_tree_vector (unparsed_funs_with_definitions);
1981 parser->unparsed_queues->pop ();
1986 /* Constructors and destructors. */
1988 static cp_parser *cp_parser_new
1991 /* Routines to parse various constructs.
1993 Those that return `tree' will return the error_mark_node (rather
1994 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1995 Sometimes, they will return an ordinary node if error-recovery was
1996 attempted, even though a parse error occurred. So, to check
1997 whether or not a parse error occurred, you should always use
1998 cp_parser_error_occurred. If the construct is optional (indicated
1999 either by an `_opt' in the name of the function that does the
2000 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2001 the construct is not present. */
2003 /* Lexical conventions [gram.lex] */
2005 static cp_expr cp_parser_identifier
2007 static cp_expr cp_parser_string_literal
2008 (cp_parser *, bool, bool, bool);
2009 static cp_expr cp_parser_userdef_char_literal
2011 static tree cp_parser_userdef_string_literal
2013 static cp_expr cp_parser_userdef_numeric_literal
2016 /* Basic concepts [gram.basic] */
2018 static bool cp_parser_translation_unit
2021 /* Expressions [gram.expr] */
2023 static cp_expr cp_parser_primary_expression
2024 (cp_parser *, bool, bool, bool, cp_id_kind *);
2025 static cp_expr cp_parser_id_expression
2026 (cp_parser *, bool, bool, bool *, bool, bool);
2027 static cp_expr cp_parser_unqualified_id
2028 (cp_parser *, bool, bool, bool, bool);
2029 static tree cp_parser_nested_name_specifier_opt
2030 (cp_parser *, bool, bool, bool, bool);
2031 static tree cp_parser_nested_name_specifier
2032 (cp_parser *, bool, bool, bool, bool);
2033 static tree cp_parser_qualifying_entity
2034 (cp_parser *, bool, bool, bool, bool, bool);
2035 static cp_expr cp_parser_postfix_expression
2036 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2037 static tree cp_parser_postfix_open_square_expression
2038 (cp_parser *, tree, bool, bool);
2039 static tree cp_parser_postfix_dot_deref_expression
2040 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2041 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2042 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2043 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2044 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2045 static void cp_parser_pseudo_destructor_name
2046 (cp_parser *, tree, tree *, tree *);
2047 static cp_expr cp_parser_unary_expression
2048 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2049 static enum tree_code cp_parser_unary_operator
2051 static tree cp_parser_new_expression
2053 static vec<tree, va_gc> *cp_parser_new_placement
2055 static tree cp_parser_new_type_id
2056 (cp_parser *, tree *);
2057 static cp_declarator *cp_parser_new_declarator_opt
2059 static cp_declarator *cp_parser_direct_new_declarator
2061 static vec<tree, va_gc> *cp_parser_new_initializer
2063 static tree cp_parser_delete_expression
2065 static cp_expr cp_parser_cast_expression
2066 (cp_parser *, bool, bool, bool, cp_id_kind *);
2067 static cp_expr cp_parser_binary_expression
2068 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2069 static tree cp_parser_question_colon_clause
2070 (cp_parser *, cp_expr);
2071 static cp_expr cp_parser_assignment_expression
2072 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2073 static enum tree_code cp_parser_assignment_operator_opt
2075 static cp_expr cp_parser_expression
2076 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2077 static cp_expr cp_parser_constant_expression
2078 (cp_parser *, bool = false, bool * = NULL);
2079 static cp_expr cp_parser_builtin_offsetof
2081 static cp_expr cp_parser_lambda_expression
2083 static void cp_parser_lambda_introducer
2084 (cp_parser *, tree);
2085 static bool cp_parser_lambda_declarator_opt
2086 (cp_parser *, tree);
2087 static void cp_parser_lambda_body
2088 (cp_parser *, tree);
2090 /* Statements [gram.stmt.stmt] */
2092 static void cp_parser_statement
2093 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2094 static void cp_parser_label_for_labeled_statement
2095 (cp_parser *, tree);
2096 static tree cp_parser_expression_statement
2097 (cp_parser *, tree);
2098 static tree cp_parser_compound_statement
2099 (cp_parser *, tree, int, bool);
2100 static void cp_parser_statement_seq_opt
2101 (cp_parser *, tree);
2102 static tree cp_parser_selection_statement
2103 (cp_parser *, bool *, vec<tree> *);
2104 static tree cp_parser_condition
2106 static tree cp_parser_iteration_statement
2107 (cp_parser *, bool *, bool);
2108 static bool cp_parser_for_init_statement
2109 (cp_parser *, tree *decl);
2110 static tree cp_parser_for
2111 (cp_parser *, bool);
2112 static tree cp_parser_c_for
2113 (cp_parser *, tree, tree, bool);
2114 static tree cp_parser_range_for
2115 (cp_parser *, tree, tree, tree, bool);
2116 static void do_range_for_auto_deduction
2118 static tree cp_parser_perform_range_for_lookup
2119 (tree, tree *, tree *);
2120 static tree cp_parser_range_for_member_function
2122 static tree cp_parser_jump_statement
2124 static void cp_parser_declaration_statement
2127 static tree cp_parser_implicitly_scoped_statement
2128 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2129 static void cp_parser_already_scoped_statement
2130 (cp_parser *, bool *, const token_indent_info &);
2132 /* Declarations [gram.dcl.dcl] */
2134 static void cp_parser_declaration_seq_opt
2136 static void cp_parser_declaration
2138 static void cp_parser_block_declaration
2139 (cp_parser *, bool);
2140 static void cp_parser_simple_declaration
2141 (cp_parser *, bool, tree *);
2142 static void cp_parser_decl_specifier_seq
2143 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2144 static tree cp_parser_storage_class_specifier_opt
2146 static tree cp_parser_function_specifier_opt
2147 (cp_parser *, cp_decl_specifier_seq *);
2148 static tree cp_parser_type_specifier
2149 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2151 static tree cp_parser_simple_type_specifier
2152 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2153 static tree cp_parser_type_name
2154 (cp_parser *, bool);
2155 static tree cp_parser_type_name
2157 static tree cp_parser_nonclass_name
2158 (cp_parser* parser);
2159 static tree cp_parser_elaborated_type_specifier
2160 (cp_parser *, bool, bool);
2161 static tree cp_parser_enum_specifier
2163 static void cp_parser_enumerator_list
2164 (cp_parser *, tree);
2165 static void cp_parser_enumerator_definition
2166 (cp_parser *, tree);
2167 static tree cp_parser_namespace_name
2169 static void cp_parser_namespace_definition
2171 static void cp_parser_namespace_body
2173 static tree cp_parser_qualified_namespace_specifier
2175 static void cp_parser_namespace_alias_definition
2177 static bool cp_parser_using_declaration
2178 (cp_parser *, bool);
2179 static void cp_parser_using_directive
2181 static tree cp_parser_alias_declaration
2183 static void cp_parser_asm_definition
2185 static void cp_parser_linkage_specification
2187 static void cp_parser_static_assert
2188 (cp_parser *, bool);
2189 static tree cp_parser_decltype
2192 /* Declarators [gram.dcl.decl] */
2194 static tree cp_parser_init_declarator
2195 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2196 bool, bool, int, bool *, tree *, location_t *, tree *);
2197 static cp_declarator *cp_parser_declarator
2198 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2199 static cp_declarator *cp_parser_direct_declarator
2200 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2201 static enum tree_code cp_parser_ptr_operator
2202 (cp_parser *, tree *, cp_cv_quals *, tree *);
2203 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2205 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2207 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2209 static tree cp_parser_tx_qualifier_opt
2211 static tree cp_parser_late_return_type_opt
2212 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2213 static tree cp_parser_declarator_id
2214 (cp_parser *, bool);
2215 static tree cp_parser_type_id
2217 static tree cp_parser_template_type_arg
2219 static tree cp_parser_trailing_type_id (cp_parser *);
2220 static tree cp_parser_type_id_1
2221 (cp_parser *, bool, bool);
2222 static void cp_parser_type_specifier_seq
2223 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2224 static tree cp_parser_parameter_declaration_clause
2226 static tree cp_parser_parameter_declaration_list
2227 (cp_parser *, bool *);
2228 static cp_parameter_declarator *cp_parser_parameter_declaration
2229 (cp_parser *, bool, bool *);
2230 static tree cp_parser_default_argument
2231 (cp_parser *, bool);
2232 static void cp_parser_function_body
2233 (cp_parser *, bool);
2234 static tree cp_parser_initializer
2235 (cp_parser *, bool *, bool *);
2236 static cp_expr cp_parser_initializer_clause
2237 (cp_parser *, bool *);
2238 static cp_expr cp_parser_braced_list
2239 (cp_parser*, bool*);
2240 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2241 (cp_parser *, bool *);
2243 static bool cp_parser_ctor_initializer_opt_and_function_body
2244 (cp_parser *, bool);
2246 static tree cp_parser_late_parsing_omp_declare_simd
2247 (cp_parser *, tree);
2249 static tree cp_parser_late_parsing_cilk_simd_fn_info
2250 (cp_parser *, tree);
2252 static tree cp_parser_late_parsing_oacc_routine
2253 (cp_parser *, tree);
2255 static tree synthesize_implicit_template_parm
2256 (cp_parser *, tree);
2257 static tree finish_fully_implicit_template
2258 (cp_parser *, tree);
2260 /* Classes [gram.class] */
2262 static tree cp_parser_class_name
2263 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2264 static tree cp_parser_class_specifier
2266 static tree cp_parser_class_head
2267 (cp_parser *, bool *);
2268 static enum tag_types cp_parser_class_key
2270 static void cp_parser_type_parameter_key
2271 (cp_parser* parser);
2272 static void cp_parser_member_specification_opt
2274 static void cp_parser_member_declaration
2276 static tree cp_parser_pure_specifier
2278 static tree cp_parser_constant_initializer
2281 /* Derived classes [gram.class.derived] */
2283 static tree cp_parser_base_clause
2285 static tree cp_parser_base_specifier
2288 /* Special member functions [gram.special] */
2290 static tree cp_parser_conversion_function_id
2292 static tree cp_parser_conversion_type_id
2294 static cp_declarator *cp_parser_conversion_declarator_opt
2296 static bool cp_parser_ctor_initializer_opt
2298 static void cp_parser_mem_initializer_list
2300 static tree cp_parser_mem_initializer
2302 static tree cp_parser_mem_initializer_id
2305 /* Overloading [gram.over] */
2307 static cp_expr cp_parser_operator_function_id
2309 static cp_expr cp_parser_operator
2312 /* Templates [gram.temp] */
2314 static void cp_parser_template_declaration
2315 (cp_parser *, bool);
2316 static tree cp_parser_template_parameter_list
2318 static tree cp_parser_template_parameter
2319 (cp_parser *, bool *, bool *);
2320 static tree cp_parser_type_parameter
2321 (cp_parser *, bool *);
2322 static tree cp_parser_template_id
2323 (cp_parser *, bool, bool, enum tag_types, bool);
2324 static tree cp_parser_template_name
2325 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2326 static tree cp_parser_template_argument_list
2328 static tree cp_parser_template_argument
2330 static void cp_parser_explicit_instantiation
2332 static void cp_parser_explicit_specialization
2335 /* Exception handling [gram.exception] */
2337 static tree cp_parser_try_block
2339 static bool cp_parser_function_try_block
2341 static void cp_parser_handler_seq
2343 static void cp_parser_handler
2345 static tree cp_parser_exception_declaration
2347 static tree cp_parser_throw_expression
2349 static tree cp_parser_exception_specification_opt
2351 static tree cp_parser_type_id_list
2354 /* GNU Extensions */
2356 static tree cp_parser_asm_specification_opt
2358 static tree cp_parser_asm_operand_list
2360 static tree cp_parser_asm_clobber_list
2362 static tree cp_parser_asm_label_list
2364 static bool cp_next_tokens_can_be_attribute_p
2366 static bool cp_next_tokens_can_be_gnu_attribute_p
2368 static bool cp_next_tokens_can_be_std_attribute_p
2370 static bool cp_nth_tokens_can_be_std_attribute_p
2371 (cp_parser *, size_t);
2372 static bool cp_nth_tokens_can_be_gnu_attribute_p
2373 (cp_parser *, size_t);
2374 static bool cp_nth_tokens_can_be_attribute_p
2375 (cp_parser *, size_t);
2376 static tree cp_parser_attributes_opt
2378 static tree cp_parser_gnu_attributes_opt
2380 static tree cp_parser_gnu_attribute_list
2382 static tree cp_parser_std_attribute
2384 static tree cp_parser_std_attribute_spec
2386 static tree cp_parser_std_attribute_spec_seq
2388 static bool cp_parser_extension_opt
2389 (cp_parser *, int *);
2390 static void cp_parser_label_declaration
2393 /* Concept Extensions */
2395 static tree cp_parser_requires_clause
2397 static tree cp_parser_requires_clause_opt
2399 static tree cp_parser_requires_expression
2401 static tree cp_parser_requirement_parameter_list
2403 static tree cp_parser_requirement_body
2405 static tree cp_parser_requirement_list
2407 static tree cp_parser_requirement
2409 static tree cp_parser_simple_requirement
2411 static tree cp_parser_compound_requirement
2413 static tree cp_parser_type_requirement
2415 static tree cp_parser_nested_requirement
2418 /* Transactional Memory Extensions */
2420 static tree cp_parser_transaction
2421 (cp_parser *, cp_token *);
2422 static tree cp_parser_transaction_expression
2423 (cp_parser *, enum rid);
2424 static bool cp_parser_function_transaction
2425 (cp_parser *, enum rid);
2426 static tree cp_parser_transaction_cancel
2429 enum pragma_context {
2436 static bool cp_parser_pragma
2437 (cp_parser *, enum pragma_context, bool *);
2439 /* Objective-C++ Productions */
2441 static tree cp_parser_objc_message_receiver
2443 static tree cp_parser_objc_message_args
2445 static tree cp_parser_objc_message_expression
2447 static cp_expr cp_parser_objc_encode_expression
2449 static tree cp_parser_objc_defs_expression
2451 static tree cp_parser_objc_protocol_expression
2453 static tree cp_parser_objc_selector_expression
2455 static cp_expr cp_parser_objc_expression
2457 static bool cp_parser_objc_selector_p
2459 static tree cp_parser_objc_selector
2461 static tree cp_parser_objc_protocol_refs_opt
2463 static void cp_parser_objc_declaration
2464 (cp_parser *, tree);
2465 static tree cp_parser_objc_statement
2467 static bool cp_parser_objc_valid_prefix_attributes
2468 (cp_parser *, tree *);
2469 static void cp_parser_objc_at_property_declaration
2471 static void cp_parser_objc_at_synthesize_declaration
2473 static void cp_parser_objc_at_dynamic_declaration
2475 static tree cp_parser_objc_struct_declaration
2478 /* Utility Routines */
2480 static cp_expr cp_parser_lookup_name
2481 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2482 static tree cp_parser_lookup_name_simple
2483 (cp_parser *, tree, location_t);
2484 static tree cp_parser_maybe_treat_template_as_class
2486 static bool cp_parser_check_declarator_template_parameters
2487 (cp_parser *, cp_declarator *, location_t);
2488 static bool cp_parser_check_template_parameters
2489 (cp_parser *, unsigned, location_t, cp_declarator *);
2490 static cp_expr cp_parser_simple_cast_expression
2492 static tree cp_parser_global_scope_opt
2493 (cp_parser *, bool);
2494 static bool cp_parser_constructor_declarator_p
2495 (cp_parser *, bool);
2496 static tree cp_parser_function_definition_from_specifiers_and_declarator
2497 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2498 static tree cp_parser_function_definition_after_declarator
2499 (cp_parser *, bool);
2500 static bool cp_parser_template_declaration_after_export
2501 (cp_parser *, bool);
2502 static void cp_parser_perform_template_parameter_access_checks
2503 (vec<deferred_access_check, va_gc> *);
2504 static tree cp_parser_single_declaration
2505 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2506 static cp_expr cp_parser_functional_cast
2507 (cp_parser *, tree);
2508 static tree cp_parser_save_member_function_body
2509 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2510 static tree cp_parser_save_nsdmi
2512 static tree cp_parser_enclosed_template_argument_list
2514 static void cp_parser_save_default_args
2515 (cp_parser *, tree);
2516 static void cp_parser_late_parsing_for_member
2517 (cp_parser *, tree);
2518 static tree cp_parser_late_parse_one_default_arg
2519 (cp_parser *, tree, tree, tree);
2520 static void cp_parser_late_parsing_nsdmi
2521 (cp_parser *, tree);
2522 static void cp_parser_late_parsing_default_args
2523 (cp_parser *, tree);
2524 static tree cp_parser_sizeof_operand
2525 (cp_parser *, enum rid);
2526 static tree cp_parser_trait_expr
2527 (cp_parser *, enum rid);
2528 static bool cp_parser_declares_only_class_p
2530 static void cp_parser_set_storage_class
2531 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2532 static void cp_parser_set_decl_spec_type
2533 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2534 static void set_and_check_decl_spec_loc
2535 (cp_decl_specifier_seq *decl_specs,
2536 cp_decl_spec ds, cp_token *);
2537 static bool cp_parser_friend_p
2538 (const cp_decl_specifier_seq *);
2539 static void cp_parser_required_error
2540 (cp_parser *, required_token, bool);
2541 static cp_token *cp_parser_require
2542 (cp_parser *, enum cpp_ttype, required_token);
2543 static cp_token *cp_parser_require_keyword
2544 (cp_parser *, enum rid, required_token);
2545 static bool cp_parser_token_starts_function_definition_p
2547 static bool cp_parser_next_token_starts_class_definition_p
2549 static bool cp_parser_next_token_ends_template_argument_p
2551 static bool cp_parser_nth_token_starts_template_argument_list_p
2552 (cp_parser *, size_t);
2553 static enum tag_types cp_parser_token_is_class_key
2555 static enum tag_types cp_parser_token_is_type_parameter_key
2557 static void cp_parser_check_class_key
2558 (enum tag_types, tree type);
2559 static void cp_parser_check_access_in_redeclaration
2560 (tree type, location_t location);
2561 static bool cp_parser_optional_template_keyword
2563 static void cp_parser_pre_parsed_nested_name_specifier
2565 static bool cp_parser_cache_group
2566 (cp_parser *, enum cpp_ttype, unsigned);
2567 static tree cp_parser_cache_defarg
2568 (cp_parser *parser, bool nsdmi);
2569 static void cp_parser_parse_tentatively
2571 static void cp_parser_commit_to_tentative_parse
2573 static void cp_parser_commit_to_topmost_tentative_parse
2575 static void cp_parser_abort_tentative_parse
2577 static bool cp_parser_parse_definitely
2579 static inline bool cp_parser_parsing_tentatively
2581 static bool cp_parser_uncommitted_to_tentative_parse_p
2583 static void cp_parser_error
2584 (cp_parser *, const char *);
2585 static void cp_parser_name_lookup_error
2586 (cp_parser *, tree, tree, name_lookup_error, location_t);
2587 static bool cp_parser_simulate_error
2589 static bool cp_parser_check_type_definition
2591 static void cp_parser_check_for_definition_in_return_type
2592 (cp_declarator *, tree, location_t type_location);
2593 static void cp_parser_check_for_invalid_template_id
2594 (cp_parser *, tree, enum tag_types, location_t location);
2595 static bool cp_parser_non_integral_constant_expression
2596 (cp_parser *, non_integral_constant);
2597 static void cp_parser_diagnose_invalid_type_name
2598 (cp_parser *, tree, location_t);
2599 static bool cp_parser_parse_and_diagnose_invalid_type_name
2601 static int cp_parser_skip_to_closing_parenthesis
2602 (cp_parser *, bool, bool, bool);
2603 static void cp_parser_skip_to_end_of_statement
2605 static void cp_parser_consume_semicolon_at_end_of_statement
2607 static void cp_parser_skip_to_end_of_block_or_statement
2609 static bool cp_parser_skip_to_closing_brace
2611 static void cp_parser_skip_to_end_of_template_parameter_list
2613 static void cp_parser_skip_to_pragma_eol
2614 (cp_parser*, cp_token *);
2615 static bool cp_parser_error_occurred
2617 static bool cp_parser_allow_gnu_extensions_p
2619 static bool cp_parser_is_pure_string_literal
2621 static bool cp_parser_is_string_literal
2623 static bool cp_parser_is_keyword
2624 (cp_token *, enum rid);
2625 static tree cp_parser_make_typename_type
2626 (cp_parser *, tree, location_t location);
2627 static cp_declarator * cp_parser_make_indirect_declarator
2628 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2629 static bool cp_parser_compound_literal_p
2631 static bool cp_parser_array_designator_p
2633 static bool cp_parser_skip_to_closing_square_bracket
2636 /* Concept-related syntactic transformations */
2638 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2639 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2641 // -------------------------------------------------------------------------- //
2642 // Unevaluated Operand Guard
2644 // Implementation of an RAII helper for unevaluated operand parsing.
2645 cp_unevaluated::cp_unevaluated ()
2647 ++cp_unevaluated_operand;
2648 ++c_inhibit_evaluation_warnings;
2651 cp_unevaluated::~cp_unevaluated ()
2653 --c_inhibit_evaluation_warnings;
2654 --cp_unevaluated_operand;
2657 // -------------------------------------------------------------------------- //
2658 // Tentative Parsing
2660 /* Returns nonzero if we are parsing tentatively. */
2663 cp_parser_parsing_tentatively (cp_parser* parser)
2665 return parser->context->next != NULL;
2668 /* Returns nonzero if TOKEN is a string literal. */
2671 cp_parser_is_pure_string_literal (cp_token* token)
2673 return (token->type == CPP_STRING ||
2674 token->type == CPP_STRING16 ||
2675 token->type == CPP_STRING32 ||
2676 token->type == CPP_WSTRING ||
2677 token->type == CPP_UTF8STRING);
2680 /* Returns nonzero if TOKEN is a string literal
2681 of a user-defined string literal. */
2684 cp_parser_is_string_literal (cp_token* token)
2686 return (cp_parser_is_pure_string_literal (token) ||
2687 token->type == CPP_STRING_USERDEF ||
2688 token->type == CPP_STRING16_USERDEF ||
2689 token->type == CPP_STRING32_USERDEF ||
2690 token->type == CPP_WSTRING_USERDEF ||
2691 token->type == CPP_UTF8STRING_USERDEF);
2694 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2697 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2699 return token->keyword == keyword;
2702 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2705 static enum pragma_kind
2706 cp_parser_pragma_kind (cp_token *token)
2708 if (token->type != CPP_PRAGMA)
2710 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2711 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2714 /* Helper function for cp_parser_error.
2715 Having peeked a token of kind TOK1_KIND that might signify
2716 a conflict marker, peek successor tokens to determine
2717 if we actually do have a conflict marker.
2718 Specifically, we consider a run of 7 '<', '=' or '>' characters
2719 at the start of a line as a conflict marker.
2720 These come through the lexer as three pairs and a single,
2721 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2722 If it returns true, *OUT_LOC is written to with the location/range
2726 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2727 location_t *out_loc)
2729 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2730 if (token2->type != tok1_kind)
2732 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2733 if (token3->type != tok1_kind)
2735 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2736 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2739 /* It must be at the start of the line. */
2740 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2741 if (LOCATION_COLUMN (start_loc) != 1)
2744 /* We have a conflict marker. Construct a location of the form:
2747 with start == caret, finishing at the end of the marker. */
2748 location_t finish_loc = get_finish (token4->location);
2749 *out_loc = make_location (start_loc, start_loc, finish_loc);
2754 /* If not parsing tentatively, issue a diagnostic of the form
2755 FILE:LINE: MESSAGE before TOKEN
2756 where TOKEN is the next token in the input stream. MESSAGE
2757 (specified by the caller) is usually of the form "expected
2761 cp_parser_error (cp_parser* parser, const char* gmsgid)
2763 if (!cp_parser_simulate_error (parser))
2765 cp_token *token = cp_lexer_peek_token (parser->lexer);
2766 /* This diagnostic makes more sense if it is tagged to the line
2767 of the token we just peeked at. */
2768 cp_lexer_set_source_position_from_token (token);
2770 if (token->type == CPP_PRAGMA)
2772 error_at (token->location,
2773 "%<#pragma%> is not allowed here");
2774 cp_parser_skip_to_pragma_eol (parser, token);
2778 /* If this is actually a conflict marker, report it as such. */
2779 if (token->type == CPP_LSHIFT
2780 || token->type == CPP_RSHIFT
2781 || token->type == CPP_EQ_EQ)
2784 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2786 error_at (loc, "version control conflict marker in file");
2791 c_parse_error (gmsgid,
2792 /* Because c_parser_error does not understand
2793 CPP_KEYWORD, keywords are treated like
2795 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2796 token->u.value, token->flags);
2800 /* Issue an error about name-lookup failing. NAME is the
2801 IDENTIFIER_NODE DECL is the result of
2802 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2803 the thing that we hoped to find. */
2806 cp_parser_name_lookup_error (cp_parser* parser,
2809 name_lookup_error desired,
2810 location_t location)
2812 /* If name lookup completely failed, tell the user that NAME was not
2814 if (decl == error_mark_node)
2816 if (parser->scope && parser->scope != global_namespace)
2817 error_at (location, "%<%E::%E%> has not been declared",
2818 parser->scope, name);
2819 else if (parser->scope == global_namespace)
2820 error_at (location, "%<::%E%> has not been declared", name);
2821 else if (parser->object_scope
2822 && !CLASS_TYPE_P (parser->object_scope))
2823 error_at (location, "request for member %qE in non-class type %qT",
2824 name, parser->object_scope);
2825 else if (parser->object_scope)
2826 error_at (location, "%<%T::%E%> has not been declared",
2827 parser->object_scope, name);
2829 error_at (location, "%qE has not been declared", name);
2831 else if (parser->scope && parser->scope != global_namespace)
2836 error_at (location, "%<%E::%E%> is not a type",
2837 parser->scope, name);
2840 error_at (location, "%<%E::%E%> is not a class or namespace",
2841 parser->scope, name);
2845 "%<%E::%E%> is not a class, namespace, or enumeration",
2846 parser->scope, name);
2853 else if (parser->scope == global_namespace)
2858 error_at (location, "%<::%E%> is not a type", name);
2861 error_at (location, "%<::%E%> is not a class or namespace", name);
2865 "%<::%E%> is not a class, namespace, or enumeration",
2877 error_at (location, "%qE is not a type", name);
2880 error_at (location, "%qE is not a class or namespace", name);
2884 "%qE is not a class, namespace, or enumeration", name);
2892 /* If we are parsing tentatively, remember that an error has occurred
2893 during this tentative parse. Returns true if the error was
2894 simulated; false if a message should be issued by the caller. */
2897 cp_parser_simulate_error (cp_parser* parser)
2899 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2901 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2907 /* This function is called when a type is defined. If type
2908 definitions are forbidden at this point, an error message is
2912 cp_parser_check_type_definition (cp_parser* parser)
2914 /* If types are forbidden here, issue a message. */
2915 if (parser->type_definition_forbidden_message)
2917 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2918 in the message need to be interpreted. */
2919 error (parser->type_definition_forbidden_message);
2925 /* This function is called when the DECLARATOR is processed. The TYPE
2926 was a type defined in the decl-specifiers. If it is invalid to
2927 define a type in the decl-specifiers for DECLARATOR, an error is
2928 issued. TYPE_LOCATION is the location of TYPE and is used
2929 for error reporting. */
2932 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2933 tree type, location_t type_location)
2935 /* [dcl.fct] forbids type definitions in return types.
2936 Unfortunately, it's not easy to know whether or not we are
2937 processing a return type until after the fact. */
2939 && (declarator->kind == cdk_pointer
2940 || declarator->kind == cdk_reference
2941 || declarator->kind == cdk_ptrmem))
2942 declarator = declarator->declarator;
2944 && declarator->kind == cdk_function)
2946 error_at (type_location,
2947 "new types may not be defined in a return type");
2948 inform (type_location,
2949 "(perhaps a semicolon is missing after the definition of %qT)",
2954 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2955 "<" in any valid C++ program. If the next token is indeed "<",
2956 issue a message warning the user about what appears to be an
2957 invalid attempt to form a template-id. LOCATION is the location
2958 of the type-specifier (TYPE) */
2961 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2963 enum tag_types tag_type,
2964 location_t location)
2966 cp_token_position start = 0;
2968 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2971 error_at (location, "%qT is not a template", type);
2972 else if (identifier_p (type))
2974 if (tag_type != none_type)
2975 error_at (location, "%qE is not a class template", type);
2977 error_at (location, "%qE is not a template", type);
2980 error_at (location, "invalid template-id");
2981 /* Remember the location of the invalid "<". */
2982 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2983 start = cp_lexer_token_position (parser->lexer, true);
2984 /* Consume the "<". */
2985 cp_lexer_consume_token (parser->lexer);
2986 /* Parse the template arguments. */
2987 cp_parser_enclosed_template_argument_list (parser);
2988 /* Permanently remove the invalid template arguments so that
2989 this error message is not issued again. */
2991 cp_lexer_purge_tokens_after (parser->lexer, start);
2995 /* If parsing an integral constant-expression, issue an error message
2996 about the fact that THING appeared and return true. Otherwise,
2997 return false. In either case, set
2998 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3001 cp_parser_non_integral_constant_expression (cp_parser *parser,
3002 non_integral_constant thing)
3004 parser->non_integral_constant_expression_p = true;
3005 if (parser->integral_constant_expression_p)
3007 if (!parser->allow_non_integral_constant_expression_p)
3009 const char *msg = NULL;
3013 error ("floating-point literal "
3014 "cannot appear in a constant-expression");
3017 error ("a cast to a type other than an integral or "
3018 "enumeration type cannot appear in a "
3019 "constant-expression");
3022 error ("%<typeid%> operator "
3023 "cannot appear in a constant-expression");
3026 error ("non-constant compound literals "
3027 "cannot appear in a constant-expression");
3030 error ("a function call "
3031 "cannot appear in a constant-expression");
3034 error ("an increment "
3035 "cannot appear in a constant-expression");
3038 error ("an decrement "
3039 "cannot appear in a constant-expression");
3042 error ("an array reference "
3043 "cannot appear in a constant-expression");
3045 case NIC_ADDR_LABEL:
3046 error ("the address of a label "
3047 "cannot appear in a constant-expression");
3049 case NIC_OVERLOADED:
3050 error ("calls to overloaded operators "
3051 "cannot appear in a constant-expression");
3053 case NIC_ASSIGNMENT:
3054 error ("an assignment cannot appear in a constant-expression");
3057 error ("a comma operator "
3058 "cannot appear in a constant-expression");
3060 case NIC_CONSTRUCTOR:
3061 error ("a call to a constructor "
3062 "cannot appear in a constant-expression");
3064 case NIC_TRANSACTION:
3065 error ("a transaction expression "
3066 "cannot appear in a constant-expression");
3072 msg = "__FUNCTION__";
3074 case NIC_PRETTY_FUNC:
3075 msg = "__PRETTY_FUNCTION__";
3095 case NIC_PREINCREMENT:
3098 case NIC_PREDECREMENT:
3111 error ("%qs cannot appear in a constant-expression", msg);
3118 /* Emit a diagnostic for an invalid type name. This function commits
3119 to the current active tentative parse, if any. (Otherwise, the
3120 problematic construct might be encountered again later, resulting
3121 in duplicate error messages.) LOCATION is the location of ID. */
3124 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3125 location_t location)
3127 tree decl, ambiguous_decls;
3128 cp_parser_commit_to_tentative_parse (parser);
3129 /* Try to lookup the identifier. */
3130 decl = cp_parser_lookup_name (parser, id, none_type,
3131 /*is_template=*/false,
3132 /*is_namespace=*/false,
3133 /*check_dependency=*/true,
3134 &ambiguous_decls, location);
3135 if (ambiguous_decls)
3136 /* If the lookup was ambiguous, an error will already have
3139 /* If the lookup found a template-name, it means that the user forgot
3140 to specify an argument list. Emit a useful error message. */
3141 if (DECL_TYPE_TEMPLATE_P (decl))
3144 "invalid use of template-name %qE without an argument list",
3146 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3148 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3149 error_at (location, "invalid use of destructor %qD as a type", id);
3150 else if (TREE_CODE (decl) == TYPE_DECL)
3151 /* Something like 'unsigned A a;' */
3152 error_at (location, "invalid combination of multiple type-specifiers");
3153 else if (!parser->scope)
3155 /* Issue an error message. */
3156 error_at (location, "%qE does not name a type", id);
3157 /* If we're in a template class, it's possible that the user was
3158 referring to a type from a base class. For example:
3160 template <typename T> struct A { typedef T X; };
3161 template <typename T> struct B : public A<T> { X x; };
3163 The user should have said "typename A<T>::X". */
3164 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3165 inform (location, "C++11 %<constexpr%> only available with "
3166 "-std=c++11 or -std=gnu++11");
3167 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3168 inform (location, "C++11 %<noexcept%> only available with "
3169 "-std=c++11 or -std=gnu++11");
3170 else if (cxx_dialect < cxx11
3171 && TREE_CODE (id) == IDENTIFIER_NODE
3172 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3173 inform (location, "C++11 %<thread_local%> only available with "
3174 "-std=c++11 or -std=gnu++11");
3175 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3176 inform (location, "%<concept%> only available with -fconcepts");
3177 else if (processing_template_decl && current_class_type
3178 && TYPE_BINFO (current_class_type))
3182 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3186 tree base_type = BINFO_TYPE (b);
3187 if (CLASS_TYPE_P (base_type)
3188 && dependent_type_p (base_type))
3191 /* Go from a particular instantiation of the
3192 template (which will have an empty TYPE_FIELDs),
3193 to the main version. */
3194 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3195 for (field = TYPE_FIELDS (base_type);
3197 field = DECL_CHAIN (field))
3198 if (TREE_CODE (field) == TYPE_DECL
3199 && DECL_NAME (field) == id)
3202 "(perhaps %<typename %T::%E%> was intended)",
3203 BINFO_TYPE (b), id);
3212 /* Here we diagnose qualified-ids where the scope is actually correct,
3213 but the identifier does not resolve to a valid type name. */
3214 else if (parser->scope != error_mark_node)
3216 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3218 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3219 error_at (location_of (id),
3220 "%qE in namespace %qE does not name a template type",
3223 error_at (location_of (id),
3224 "%qE in namespace %qE does not name a type",
3227 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3229 else if (CLASS_TYPE_P (parser->scope)
3230 && constructor_name_p (id, parser->scope))
3233 error_at (location, "%<%T::%E%> names the constructor, not"
3234 " the type", parser->scope, id);
3235 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3236 error_at (location, "and %qT has no template constructors",
3239 else if (TYPE_P (parser->scope)
3240 && dependent_scope_p (parser->scope))
3241 error_at (location, "need %<typename%> before %<%T::%E%> because "
3242 "%qT is a dependent scope",
3243 parser->scope, id, parser->scope);
3244 else if (TYPE_P (parser->scope))
3246 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3247 error_at (location_of (id),
3248 "%qE in %q#T does not name a template type",
3251 error_at (location_of (id),
3252 "%qE in %q#T does not name a type",
3255 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3262 /* Check for a common situation where a type-name should be present,
3263 but is not, and issue a sensible error message. Returns true if an
3264 invalid type-name was detected.
3266 The situation handled by this function are variable declarations of the
3267 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3268 Usually, `ID' should name a type, but if we got here it means that it
3269 does not. We try to emit the best possible error message depending on
3270 how exactly the id-expression looks like. */
3273 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3276 cp_token *token = cp_lexer_peek_token (parser->lexer);
3278 /* Avoid duplicate error about ambiguous lookup. */
3279 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3281 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3282 if (next->type == CPP_NAME && next->error_reported)
3286 cp_parser_parse_tentatively (parser);
3287 id = cp_parser_id_expression (parser,
3288 /*template_keyword_p=*/false,
3289 /*check_dependency_p=*/true,
3290 /*template_p=*/NULL,
3291 /*declarator_p=*/true,
3292 /*optional_p=*/false);
3293 /* If the next token is a (, this is a function with no explicit return
3294 type, i.e. constructor, destructor or conversion op. */
3295 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3296 || TREE_CODE (id) == TYPE_DECL)
3298 cp_parser_abort_tentative_parse (parser);
3301 if (!cp_parser_parse_definitely (parser))
3304 /* Emit a diagnostic for the invalid type. */
3305 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3307 /* If we aren't in the middle of a declarator (i.e. in a
3308 parameter-declaration-clause), skip to the end of the declaration;
3309 there's no point in trying to process it. */
3310 if (!parser->in_declarator_p)
3311 cp_parser_skip_to_end_of_block_or_statement (parser);
3315 /* Consume tokens up to, and including, the next non-nested closing `)'.
3316 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3317 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3318 found an unnested token of that type. */
3321 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3326 unsigned paren_depth = 0;
3327 unsigned brace_depth = 0;
3328 unsigned square_depth = 0;
3330 if (recovering && or_ttype == CPP_EOF
3331 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3336 cp_token * token = cp_lexer_peek_token (parser->lexer);
3338 /* Have we found what we're looking for before the closing paren? */
3339 if (token->type == or_ttype && or_ttype != CPP_EOF
3340 && !brace_depth && !paren_depth && !square_depth)
3343 switch (token->type)
3346 case CPP_PRAGMA_EOL:
3347 /* If we've run out of tokens, then there is no closing `)'. */
3350 /* This is good for lambda expression capture-lists. */
3351 case CPP_OPEN_SQUARE:
3354 case CPP_CLOSE_SQUARE:
3355 if (!square_depth--)
3360 /* This matches the processing in skip_to_end_of_statement. */
3365 case CPP_OPEN_BRACE:
3368 case CPP_CLOSE_BRACE:
3373 case CPP_OPEN_PAREN:
3378 case CPP_CLOSE_PAREN:
3379 if (!brace_depth && !paren_depth--)
3382 cp_lexer_consume_token (parser->lexer);
3391 /* Consume the token. */
3392 cp_lexer_consume_token (parser->lexer);
3396 /* Consume tokens up to, and including, the next non-nested closing `)'.
3397 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3398 are doing error recovery. Returns -1 if OR_COMMA is true and we
3399 found an unnested token of that type. */
3402 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3407 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3408 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3409 ttype, consume_paren);
3412 /* Consume tokens until we reach the end of the current statement.
3413 Normally, that will be just before consuming a `;'. However, if a
3414 non-nested `}' comes first, then we stop before consuming that. */
3417 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3419 unsigned nesting_depth = 0;
3421 /* Unwind generic function template scope if necessary. */
3422 if (parser->fully_implicit_function_template_p)
3423 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3427 cp_token *token = cp_lexer_peek_token (parser->lexer);
3429 switch (token->type)
3432 case CPP_PRAGMA_EOL:
3433 /* If we've run out of tokens, stop. */
3437 /* If the next token is a `;', we have reached the end of the
3443 case CPP_CLOSE_BRACE:
3444 /* If this is a non-nested '}', stop before consuming it.
3445 That way, when confronted with something like:
3449 we stop before consuming the closing '}', even though we
3450 have not yet reached a `;'. */
3451 if (nesting_depth == 0)
3454 /* If it is the closing '}' for a block that we have
3455 scanned, stop -- but only after consuming the token.
3461 we will stop after the body of the erroneously declared
3462 function, but before consuming the following `typedef'
3464 if (--nesting_depth == 0)
3466 cp_lexer_consume_token (parser->lexer);
3470 case CPP_OPEN_BRACE:
3478 /* Consume the token. */
3479 cp_lexer_consume_token (parser->lexer);
3483 /* This function is called at the end of a statement or declaration.
3484 If the next token is a semicolon, it is consumed; otherwise, error
3485 recovery is attempted. */
3488 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3490 /* Look for the trailing `;'. */
3491 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3493 /* If there is additional (erroneous) input, skip to the end of
3495 cp_parser_skip_to_end_of_statement (parser);
3496 /* If the next token is now a `;', consume it. */
3497 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3498 cp_lexer_consume_token (parser->lexer);
3502 /* Skip tokens until we have consumed an entire block, or until we
3503 have consumed a non-nested `;'. */
3506 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3508 int nesting_depth = 0;
3510 /* Unwind generic function template scope if necessary. */
3511 if (parser->fully_implicit_function_template_p)
3512 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3514 while (nesting_depth >= 0)
3516 cp_token *token = cp_lexer_peek_token (parser->lexer);
3518 switch (token->type)
3521 case CPP_PRAGMA_EOL:
3522 /* If we've run out of tokens, stop. */
3526 /* Stop if this is an unnested ';'. */
3531 case CPP_CLOSE_BRACE:
3532 /* Stop if this is an unnested '}', or closes the outermost
3535 if (nesting_depth < 0)
3541 case CPP_OPEN_BRACE:
3550 /* Consume the token. */
3551 cp_lexer_consume_token (parser->lexer);
3555 /* Skip tokens until a non-nested closing curly brace is the next
3556 token, or there are no more tokens. Return true in the first case,
3560 cp_parser_skip_to_closing_brace (cp_parser *parser)
3562 unsigned nesting_depth = 0;
3566 cp_token *token = cp_lexer_peek_token (parser->lexer);
3568 switch (token->type)
3571 case CPP_PRAGMA_EOL:
3572 /* If we've run out of tokens, stop. */
3575 case CPP_CLOSE_BRACE:
3576 /* If the next token is a non-nested `}', then we have reached
3577 the end of the current block. */
3578 if (nesting_depth-- == 0)
3582 case CPP_OPEN_BRACE:
3583 /* If it the next token is a `{', then we are entering a new
3584 block. Consume the entire block. */
3592 /* Consume the token. */
3593 cp_lexer_consume_token (parser->lexer);
3597 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3598 parameter is the PRAGMA token, allowing us to purge the entire pragma
3602 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3606 parser->lexer->in_pragma = false;
3609 token = cp_lexer_consume_token (parser->lexer);
3610 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3612 /* Ensure that the pragma is not parsed again. */
3613 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3616 /* Require pragma end of line, resyncing with it as necessary. The
3617 arguments are as for cp_parser_skip_to_pragma_eol. */
3620 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3622 parser->lexer->in_pragma = false;
3623 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3624 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3627 /* This is a simple wrapper around make_typename_type. When the id is
3628 an unresolved identifier node, we can provide a superior diagnostic
3629 using cp_parser_diagnose_invalid_type_name. */
3632 cp_parser_make_typename_type (cp_parser *parser, tree id,
3633 location_t id_location)
3636 if (identifier_p (id))
3638 result = make_typename_type (parser->scope, id, typename_type,
3639 /*complain=*/tf_none);
3640 if (result == error_mark_node)
3641 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3644 return make_typename_type (parser->scope, id, typename_type, tf_error);
3647 /* This is a wrapper around the
3648 make_{pointer,ptrmem,reference}_declarator functions that decides
3649 which one to call based on the CODE and CLASS_TYPE arguments. The
3650 CODE argument should be one of the values returned by
3651 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3652 appertain to the pointer or reference. */
3654 static cp_declarator *
3655 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3656 cp_cv_quals cv_qualifiers,
3657 cp_declarator *target,
3660 if (code == ERROR_MARK)
3661 return cp_error_declarator;
3663 if (code == INDIRECT_REF)
3664 if (class_type == NULL_TREE)
3665 return make_pointer_declarator (cv_qualifiers, target, attributes);
3667 return make_ptrmem_declarator (cv_qualifiers, class_type,
3668 target, attributes);
3669 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3670 return make_reference_declarator (cv_qualifiers, target,
3672 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3673 return make_reference_declarator (cv_qualifiers, target,
3678 /* Create a new C++ parser. */
3681 cp_parser_new (void)
3687 /* cp_lexer_new_main is called before doing GC allocation because
3688 cp_lexer_new_main might load a PCH file. */
3689 lexer = cp_lexer_new_main ();
3691 /* Initialize the binops_by_token so that we can get the tree
3692 directly from the token. */
3693 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3694 binops_by_token[binops[i].token_type] = binops[i];
3696 parser = ggc_cleared_alloc<cp_parser> ();
3697 parser->lexer = lexer;
3698 parser->context = cp_parser_context_new (NULL);
3700 /* For now, we always accept GNU extensions. */
3701 parser->allow_gnu_extensions_p = 1;
3703 /* The `>' token is a greater-than operator, not the end of a
3705 parser->greater_than_is_operator_p = true;
3707 parser->default_arg_ok_p = true;
3709 /* We are not parsing a constant-expression. */
3710 parser->integral_constant_expression_p = false;
3711 parser->allow_non_integral_constant_expression_p = false;
3712 parser->non_integral_constant_expression_p = false;
3714 /* Local variable names are not forbidden. */
3715 parser->local_variables_forbidden_p = false;
3717 /* We are not processing an `extern "C"' declaration. */
3718 parser->in_unbraced_linkage_specification_p = false;
3720 /* We are not processing a declarator. */
3721 parser->in_declarator_p = false;
3723 /* We are not processing a template-argument-list. */
3724 parser->in_template_argument_list_p = false;
3726 /* We are not in an iteration statement. */
3727 parser->in_statement = 0;
3729 /* We are not in a switch statement. */
3730 parser->in_switch_statement_p = false;
3732 /* We are not parsing a type-id inside an expression. */
3733 parser->in_type_id_in_expr_p = false;
3735 /* Declarations aren't implicitly extern "C". */
3736 parser->implicit_extern_c = false;
3738 /* String literals should be translated to the execution character set. */
3739 parser->translate_strings_p = true;
3741 /* We are not parsing a function body. */
3742 parser->in_function_body = false;
3744 /* We can correct until told otherwise. */
3745 parser->colon_corrects_to_scope_p = true;
3747 /* The unparsed function queue is empty. */
3748 push_unparsed_function_queues (parser);
3750 /* There are no classes being defined. */
3751 parser->num_classes_being_defined = 0;
3753 /* No template parameters apply. */
3754 parser->num_template_parameter_lists = 0;
3756 /* Not declaring an implicit function template. */
3757 parser->auto_is_implicit_function_template_parm_p = false;
3758 parser->fully_implicit_function_template_p = false;
3759 parser->implicit_template_parms = 0;
3760 parser->implicit_template_scope = 0;
3762 /* Active OpenACC routine clauses. */
3763 parser->oacc_routine = NULL;
3765 /* Allow constrained-type-specifiers. */
3766 parser->prevent_constrained_type_specifiers = 0;
3771 /* Create a cp_lexer structure which will emit the tokens in CACHE
3772 and push it onto the parser's lexer stack. This is used for delayed
3773 parsing of in-class method bodies and default arguments, and should
3774 not be confused with tentative parsing. */
3776 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3778 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3779 lexer->next = parser->lexer;
3780 parser->lexer = lexer;
3782 /* Move the current source position to that of the first token in the
3784 cp_lexer_set_source_position_from_token (lexer->next_token);
3787 /* Pop the top lexer off the parser stack. This is never used for the
3788 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3790 cp_parser_pop_lexer (cp_parser *parser)
3792 cp_lexer *lexer = parser->lexer;
3793 parser->lexer = lexer->next;
3794 cp_lexer_destroy (lexer);
3796 /* Put the current source position back where it was before this
3797 lexer was pushed. */
3798 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3801 /* Lexical conventions [gram.lex] */
3803 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3807 cp_parser_identifier (cp_parser* parser)
3811 /* Look for the identifier. */
3812 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3813 /* Return the value. */
3815 return cp_expr (token->u.value, token->location);
3817 return error_mark_node;
3820 /* Parse a sequence of adjacent string constants. Returns a
3821 TREE_STRING representing the combined, nul-terminated string
3822 constant. If TRANSLATE is true, translate the string to the
3823 execution character set. If WIDE_OK is true, a wide string is
3826 C++98 [lex.string] says that if a narrow string literal token is
3827 adjacent to a wide string literal token, the behavior is undefined.
3828 However, C99 6.4.5p4 says that this results in a wide string literal.
3829 We follow C99 here, for consistency with the C front end.
3831 This code is largely lifted from lex_string() in c-lex.c.
3833 FUTURE: ObjC++ will need to handle @-strings here. */
3835 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3836 bool lookup_udlit = true)
3840 struct obstack str_ob;
3841 cpp_string str, istr, *strs;
3843 enum cpp_ttype type, curr_type;
3844 int have_suffix_p = 0;
3846 tree suffix_id = NULL_TREE;
3847 bool curr_tok_is_userdef_p = false;
3849 tok = cp_lexer_peek_token (parser->lexer);
3850 if (!cp_parser_is_string_literal (tok))
3852 cp_parser_error (parser, "expected string-literal");
3853 return error_mark_node;
3856 location_t loc = tok->location;
3858 if (cpp_userdef_string_p (tok->type))
3860 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3861 curr_type = cpp_userdef_string_remove_type (tok->type);
3862 curr_tok_is_userdef_p = true;
3866 string_tree = tok->u.value;
3867 curr_type = tok->type;
3871 /* Try to avoid the overhead of creating and destroying an obstack
3872 for the common case of just one string. */
3873 if (!cp_parser_is_string_literal
3874 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3876 cp_lexer_consume_token (parser->lexer);
3878 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3879 str.len = TREE_STRING_LENGTH (string_tree);
3882 if (curr_tok_is_userdef_p)
3884 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3886 curr_type = cpp_userdef_string_remove_type (tok->type);
3889 curr_type = tok->type;
3895 location_t last_tok_loc;
3896 gcc_obstack_init (&str_ob);
3901 last_tok_loc = tok->location;
3902 cp_lexer_consume_token (parser->lexer);
3904 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3905 str.len = TREE_STRING_LENGTH (string_tree);
3907 if (curr_tok_is_userdef_p)
3909 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3910 if (have_suffix_p == 0)
3912 suffix_id = curr_suffix_id;
3915 else if (have_suffix_p == 1
3916 && curr_suffix_id != suffix_id)
3918 error ("inconsistent user-defined literal suffixes"
3919 " %qD and %qD in string literal",
3920 suffix_id, curr_suffix_id);
3923 curr_type = cpp_userdef_string_remove_type (tok->type);
3926 curr_type = tok->type;
3928 if (type != curr_type)
3930 if (type == CPP_STRING)
3932 else if (curr_type != CPP_STRING)
3933 error_at (tok->location,
3934 "unsupported non-standard concatenation "
3935 "of string literals");
3938 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3940 tok = cp_lexer_peek_token (parser->lexer);
3941 if (cpp_userdef_string_p (tok->type))
3943 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3944 curr_type = cpp_userdef_string_remove_type (tok->type);
3945 curr_tok_is_userdef_p = true;
3949 string_tree = tok->u.value;
3950 curr_type = tok->type;
3951 curr_tok_is_userdef_p = false;
3954 while (cp_parser_is_string_literal (tok));
3956 /* A string literal built by concatenation has its caret=start at
3957 the start of the initial string, and its finish at the finish of
3958 the final string literal. */
3959 loc = make_location (loc, loc, get_finish (last_tok_loc));
3961 strs = (cpp_string *) obstack_finish (&str_ob);
3964 if (type != CPP_STRING && !wide_ok)
3966 cp_parser_error (parser, "a wide string is invalid in this context");
3970 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3971 (parse_in, strs, count, &istr, type))
3973 value = build_string (istr.len, (const char *)istr.text);
3974 free (CONST_CAST (unsigned char *, istr.text));
3980 case CPP_UTF8STRING:
3981 TREE_TYPE (value) = char_array_type_node;
3984 TREE_TYPE (value) = char16_array_type_node;
3987 TREE_TYPE (value) = char32_array_type_node;
3990 TREE_TYPE (value) = wchar_array_type_node;
3994 value = fix_string_type (value);
3998 tree literal = build_userdef_literal (suffix_id, value,
3999 OT_NONE, NULL_TREE);
4001 value = cp_parser_userdef_string_literal (literal);
4007 /* cpp_interpret_string has issued an error. */
4008 value = error_mark_node;
4011 obstack_free (&str_ob, 0);
4013 return cp_expr (value, loc);
4016 /* Look up a literal operator with the name and the exact arguments. */
4019 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4022 decl = lookup_name (name);
4023 if (!decl || !is_overloaded_fn (decl))
4024 return error_mark_node;
4026 for (fns = decl; fns; fns = OVL_NEXT (fns))
4030 tree fn = OVL_CURRENT (fns);
4031 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4032 if (parmtypes != NULL_TREE)
4034 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4035 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4037 tree tparm = TREE_VALUE (parmtypes);
4038 tree targ = TREE_TYPE ((*args)[ix]);
4039 bool ptr = TYPE_PTR_P (tparm);
4040 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4041 if ((ptr || arr || !same_type_p (tparm, targ))
4043 || !same_type_p (TREE_TYPE (tparm),
4048 && ix == vec_safe_length (args)
4049 /* May be this should be sufficient_parms_p instead,
4050 depending on how exactly should user-defined literals
4051 work in presence of default arguments on the literal
4052 operator parameters. */
4053 && parmtypes == void_list_node)
4058 return error_mark_node;
4061 /* Parse a user-defined char constant. Returns a call to a user-defined
4062 literal operator taking the character as an argument. */
4065 cp_parser_userdef_char_literal (cp_parser *parser)
4067 cp_token *token = cp_lexer_consume_token (parser->lexer);
4068 tree literal = token->u.value;
4069 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4070 tree value = USERDEF_LITERAL_VALUE (literal);
4071 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4074 /* Build up a call to the user-defined operator */
4075 /* Lookup the name we got back from the id-expression. */
4076 vec<tree, va_gc> *args = make_tree_vector ();
4077 vec_safe_push (args, value);
4078 decl = lookup_literal_operator (name, args);
4079 if (!decl || decl == error_mark_node)
4081 error ("unable to find character literal operator %qD with %qT argument",
4082 name, TREE_TYPE (value));
4083 release_tree_vector (args);
4084 return error_mark_node;
4086 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4087 release_tree_vector (args);
4091 /* A subroutine of cp_parser_userdef_numeric_literal to
4092 create a char... template parameter pack from a string node. */
4095 make_char_string_pack (tree value)
4098 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4099 const char *str = TREE_STRING_POINTER (value);
4100 int i, len = TREE_STRING_LENGTH (value) - 1;
4101 tree argvec = make_tree_vec (1);
4103 /* Fill in CHARVEC with all of the parameters. */
4104 charvec = make_tree_vec (len);
4105 for (i = 0; i < len; ++i)
4106 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4108 /* Build the argument packs. */
4109 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4110 TREE_TYPE (argpack) = char_type_node;
4112 TREE_VEC_ELT (argvec, 0) = argpack;
4117 /* A subroutine of cp_parser_userdef_numeric_literal to
4118 create a char... template parameter pack from a string node. */
4121 make_string_pack (tree value)
4124 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4125 const unsigned char *str
4126 = (const unsigned char *) TREE_STRING_POINTER (value);
4127 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4128 int len = TREE_STRING_LENGTH (value) / sz - 1;
4129 tree argvec = make_tree_vec (2);
4131 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4132 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4134 /* First template parm is character type. */
4135 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4137 /* Fill in CHARVEC with all of the parameters. */
4138 charvec = make_tree_vec (len);
4139 for (int i = 0; i < len; ++i)
4140 TREE_VEC_ELT (charvec, i)
4141 = double_int_to_tree (str_char_type_node,
4142 double_int::from_buffer (str + i * sz, sz));
4144 /* Build the argument packs. */
4145 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4146 TREE_TYPE (argpack) = str_char_type_node;
4148 TREE_VEC_ELT (argvec, 1) = argpack;
4153 /* Parse a user-defined numeric constant. returns a call to a user-defined
4154 literal operator. */
4157 cp_parser_userdef_numeric_literal (cp_parser *parser)
4159 cp_token *token = cp_lexer_consume_token (parser->lexer);
4160 tree literal = token->u.value;
4161 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4162 tree value = USERDEF_LITERAL_VALUE (literal);
4163 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4164 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4165 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4167 vec<tree, va_gc> *args;
4169 /* Look for a literal operator taking the exact type of numeric argument
4170 as the literal value. */
4171 args = make_tree_vector ();
4172 vec_safe_push (args, value);
4173 decl = lookup_literal_operator (name, args);
4174 if (decl && decl != error_mark_node)
4176 result = finish_call_expr (decl, &args, false, true,
4177 tf_warning_or_error);
4179 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4181 warning_at (token->location, OPT_Woverflow,
4182 "integer literal exceeds range of %qT type",
4183 long_long_unsigned_type_node);
4188 warning_at (token->location, OPT_Woverflow,
4189 "floating literal exceeds range of %qT type",
4190 long_double_type_node);
4191 else if (overflow < 0)
4192 warning_at (token->location, OPT_Woverflow,
4193 "floating literal truncated to zero");
4196 release_tree_vector (args);
4199 release_tree_vector (args);
4201 /* If the numeric argument didn't work, look for a raw literal
4202 operator taking a const char* argument consisting of the number
4203 in string format. */
4204 args = make_tree_vector ();
4205 vec_safe_push (args, num_string);
4206 decl = lookup_literal_operator (name, args);
4207 if (decl && decl != error_mark_node)
4209 result = finish_call_expr (decl, &args, false, true,
4210 tf_warning_or_error);
4211 release_tree_vector (args);
4214 release_tree_vector (args);
4216 /* If the raw literal didn't work, look for a non-type template
4217 function with parameter pack char.... Call the function with
4218 template parameter characters representing the number. */
4219 args = make_tree_vector ();
4220 decl = lookup_literal_operator (name, args);
4221 if (decl && decl != error_mark_node)
4223 tree tmpl_args = make_char_string_pack (num_string);
4224 decl = lookup_template_function (decl, tmpl_args);
4225 result = finish_call_expr (decl, &args, false, true,
4226 tf_warning_or_error);
4227 release_tree_vector (args);
4231 release_tree_vector (args);
4233 error ("unable to find numeric literal operator %qD", name);
4234 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4235 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4236 "to enable more built-in suffixes");
4237 return error_mark_node;
4240 /* Parse a user-defined string constant. Returns a call to a user-defined
4241 literal operator taking a character pointer and the length of the string
4245 cp_parser_userdef_string_literal (tree literal)
4247 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4248 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4249 tree value = USERDEF_LITERAL_VALUE (literal);
4250 int len = TREE_STRING_LENGTH (value)
4251 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4253 vec<tree, va_gc> *args;
4255 /* Build up a call to the user-defined operator. */
4256 /* Lookup the name we got back from the id-expression. */
4257 args = make_tree_vector ();
4258 vec_safe_push (args, value);
4259 vec_safe_push (args, build_int_cst (size_type_node, len));
4260 decl = lookup_literal_operator (name, args);
4262 if (decl && decl != error_mark_node)
4264 result = finish_call_expr (decl, &args, false, true,
4265 tf_warning_or_error);
4266 release_tree_vector (args);
4269 release_tree_vector (args);
4271 /* Look for a template function with typename parameter CharT
4272 and parameter pack CharT... Call the function with
4273 template parameter characters representing the string. */
4274 args = make_tree_vector ();
4275 decl = lookup_literal_operator (name, args);
4276 if (decl && decl != error_mark_node)
4278 tree tmpl_args = make_string_pack (value);
4279 decl = lookup_template_function (decl, tmpl_args);
4280 result = finish_call_expr (decl, &args, false, true,
4281 tf_warning_or_error);
4282 release_tree_vector (args);
4285 release_tree_vector (args);
4287 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4288 name, TREE_TYPE (value), size_type_node);
4289 return error_mark_node;
4293 /* Basic concepts [gram.basic] */
4295 /* Parse a translation-unit.
4298 declaration-seq [opt]
4300 Returns TRUE if all went well. */
4303 cp_parser_translation_unit (cp_parser* parser)
4305 /* The address of the first non-permanent object on the declarator
4307 static void *declarator_obstack_base;
4311 /* Create the declarator obstack, if necessary. */
4312 if (!cp_error_declarator)
4314 gcc_obstack_init (&declarator_obstack);
4315 /* Create the error declarator. */
4316 cp_error_declarator = make_declarator (cdk_error);
4317 /* Create the empty parameter list. */
4318 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4319 /* Remember where the base of the declarator obstack lies. */
4320 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4323 cp_parser_declaration_seq_opt (parser);
4325 /* If there are no tokens left then all went well. */
4326 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4328 /* Get rid of the token array; we don't need it any more. */
4329 cp_lexer_destroy (parser->lexer);
4330 parser->lexer = NULL;
4332 /* This file might have been a context that's implicitly extern
4333 "C". If so, pop the lang context. (Only relevant for PCH.) */
4334 if (parser->implicit_extern_c)
4336 pop_lang_context ();
4337 parser->implicit_extern_c = false;
4341 finish_translation_unit ();
4347 cp_parser_error (parser, "expected declaration");
4351 /* Make sure the declarator obstack was fully cleaned up. */
4352 gcc_assert (obstack_next_free (&declarator_obstack)
4353 == declarator_obstack_base);
4355 /* All went well. */
4359 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4360 decltype context. */
4362 static inline tsubst_flags_t
4363 complain_flags (bool decltype_p)
4365 tsubst_flags_t complain = tf_warning_or_error;
4367 complain |= tf_decltype;
4371 /* We're about to parse a collection of statements. If we're currently
4372 parsing tentatively, set up a firewall so that any nested
4373 cp_parser_commit_to_tentative_parse won't affect the current context. */
4375 static cp_token_position
4376 cp_parser_start_tentative_firewall (cp_parser *parser)
4378 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4381 cp_parser_parse_tentatively (parser);
4382 cp_parser_commit_to_topmost_tentative_parse (parser);
4383 return cp_lexer_token_position (parser->lexer, false);
4386 /* We've finished parsing the collection of statements. Wrap up the
4387 firewall and replace the relevant tokens with the parsed form. */
4390 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4396 /* Finish the firewall level. */
4397 cp_parser_parse_definitely (parser);
4398 /* And remember the result of the parse for when we try again. */
4399 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4400 token->type = CPP_PREPARSED_EXPR;
4401 token->u.value = expr;
4402 token->keyword = RID_MAX;
4403 cp_lexer_purge_tokens_after (parser->lexer, start);
4406 /* Like the above functions, but let the user modify the tokens. Used by
4407 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4408 later parses, so it makes sense to localize the effects of
4409 cp_parser_commit_to_tentative_parse. */
4411 struct tentative_firewall
4416 tentative_firewall (cp_parser *p): parser(p)
4418 /* If we're currently parsing tentatively, start a committed level as a
4419 firewall and then an inner tentative parse. */
4420 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4422 cp_parser_parse_tentatively (parser);
4423 cp_parser_commit_to_topmost_tentative_parse (parser);
4424 cp_parser_parse_tentatively (parser);
4428 ~tentative_firewall()
4432 /* Finish the inner tentative parse and the firewall, propagating any
4433 uncommitted error state to the outer tentative parse. */
4434 bool err = cp_parser_error_occurred (parser);
4435 cp_parser_parse_definitely (parser);
4436 cp_parser_parse_definitely (parser);
4438 cp_parser_simulate_error (parser);
4443 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4444 enclosing parentheses. */
4447 cp_parser_statement_expr (cp_parser *parser)
4449 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4451 /* Consume the '('. */
4452 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4453 cp_lexer_consume_token (parser->lexer);
4454 /* Start the statement-expression. */
4455 tree expr = begin_stmt_expr ();
4456 /* Parse the compound-statement. */
4457 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4459 expr = finish_stmt_expr (expr, false);
4460 /* Consume the ')'. */
4461 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4462 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4463 cp_parser_skip_to_end_of_statement (parser);
4465 cp_parser_end_tentative_firewall (parser, start, expr);
4466 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4467 return cp_expr (expr, combined_loc);
4470 /* Expressions [gram.expr] */
4472 /* Parse a fold-operator.
4475 - * / % ^ & | = < > << >>
4476 = -= *= /= %= ^= &= |= <<= >>=
4477 == != <= >= && || , .* ->*
4479 This returns the tree code corresponding to the matched operator
4480 as an int. When the current token matches a compound assignment
4481 opertor, the resulting tree code is the negative value of the
4482 non-assignment operator. */
4485 cp_parser_fold_operator (cp_token *token)
4487 switch (token->type)
4489 case CPP_PLUS: return PLUS_EXPR;
4490 case CPP_MINUS: return MINUS_EXPR;
4491 case CPP_MULT: return MULT_EXPR;
4492 case CPP_DIV: return TRUNC_DIV_EXPR;
4493 case CPP_MOD: return TRUNC_MOD_EXPR;
4494 case CPP_XOR: return BIT_XOR_EXPR;
4495 case CPP_AND: return BIT_AND_EXPR;
4496 case CPP_OR: return BIT_IOR_EXPR;
4497 case CPP_LSHIFT: return LSHIFT_EXPR;
4498 case CPP_RSHIFT: return RSHIFT_EXPR;
4500 case CPP_EQ: return -NOP_EXPR;
4501 case CPP_PLUS_EQ: return -PLUS_EXPR;
4502 case CPP_MINUS_EQ: return -MINUS_EXPR;
4503 case CPP_MULT_EQ: return -MULT_EXPR;
4504 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4505 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4506 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4507 case CPP_AND_EQ: return -BIT_AND_EXPR;
4508 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4509 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4510 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4512 case CPP_EQ_EQ: return EQ_EXPR;
4513 case CPP_NOT_EQ: return NE_EXPR;
4514 case CPP_LESS: return LT_EXPR;
4515 case CPP_GREATER: return GT_EXPR;
4516 case CPP_LESS_EQ: return LE_EXPR;
4517 case CPP_GREATER_EQ: return GE_EXPR;
4519 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4520 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4522 case CPP_COMMA: return COMPOUND_EXPR;
4524 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4525 case CPP_DEREF_STAR: return MEMBER_REF;
4527 default: return ERROR_MARK;
4531 /* Returns true if CODE indicates a binary expression, which is not allowed in
4532 the LHS of a fold-expression. More codes will need to be added to use this
4533 function in other contexts. */
4536 is_binary_op (tree_code code)
4541 case POINTER_PLUS_EXPR:
4544 case TRUNC_DIV_EXPR:
4545 case TRUNC_MOD_EXPR:
4561 case TRUTH_ANDIF_EXPR:
4562 case TRUTH_ORIF_EXPR:
4575 /* If the next token is a suitable fold operator, consume it and return as
4576 the function above. */
4579 cp_parser_fold_operator (cp_parser *parser)
4581 cp_token* token = cp_lexer_peek_token (parser->lexer);
4582 int code = cp_parser_fold_operator (token);
4583 if (code != ERROR_MARK)
4584 cp_lexer_consume_token (parser->lexer);
4588 /* Parse a fold-expression.
4591 ( ... folding-operator cast-expression)
4592 ( cast-expression folding-operator ... )
4593 ( cast-expression folding operator ... folding-operator cast-expression)
4595 Note that the '(' and ')' are matched in primary expression. */
4598 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4603 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4605 cp_lexer_consume_token (parser->lexer);
4606 int op = cp_parser_fold_operator (parser);
4607 if (op == ERROR_MARK)
4609 cp_parser_error (parser, "expected binary operator");
4610 return error_mark_node;
4613 tree expr = cp_parser_cast_expression (parser, false, false,
4615 if (expr == error_mark_node)
4616 return error_mark_node;
4617 return finish_left_unary_fold_expr (expr, op);
4620 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4621 int op = cp_parser_fold_operator (parser);
4622 if (op == ERROR_MARK)
4624 cp_parser_error (parser, "expected binary operator");
4625 return error_mark_node;
4628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4630 cp_parser_error (parser, "expected ...");
4631 return error_mark_node;
4633 cp_lexer_consume_token (parser->lexer);
4635 /* The operands of a fold-expression are cast-expressions, so binary or
4636 conditional expressions are not allowed. We check this here to avoid
4637 tentative parsing. */
4638 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4639 /* OK, the expression was parenthesized. */;
4640 else if (is_binary_op (TREE_CODE (expr1)))
4641 error_at (location_of (expr1),
4642 "binary expression in operand of fold-expression");
4643 else if (TREE_CODE (expr1) == COND_EXPR)
4644 error_at (location_of (expr1),
4645 "conditional expression in operand of fold-expression");
4648 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4649 return finish_right_unary_fold_expr (expr1, op);
4651 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4653 cp_parser_error (parser, "mismatched operator in fold-expression");
4654 return error_mark_node;
4656 cp_lexer_consume_token (parser->lexer);
4658 // Binary left or right fold.
4659 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4660 if (expr2 == error_mark_node)
4661 return error_mark_node;
4662 return finish_binary_fold_expr (expr1, expr2, op);
4665 /* Parse a primary-expression.
4672 lambda-expression (C++11)
4677 ( compound-statement )
4678 __builtin_va_arg ( assignment-expression , type-id )
4679 __builtin_offsetof ( type-id , offsetof-expression )
4682 __has_nothrow_assign ( type-id )
4683 __has_nothrow_constructor ( type-id )
4684 __has_nothrow_copy ( type-id )
4685 __has_trivial_assign ( type-id )
4686 __has_trivial_constructor ( type-id )
4687 __has_trivial_copy ( type-id )
4688 __has_trivial_destructor ( type-id )
4689 __has_virtual_destructor ( type-id )
4690 __is_abstract ( type-id )
4691 __is_base_of ( type-id , type-id )
4692 __is_class ( type-id )
4693 __is_empty ( type-id )
4694 __is_enum ( type-id )
4695 __is_final ( type-id )
4696 __is_literal_type ( type-id )
4697 __is_pod ( type-id )
4698 __is_polymorphic ( type-id )
4699 __is_std_layout ( type-id )
4700 __is_trivial ( type-id )
4701 __is_union ( type-id )
4703 Objective-C++ Extension:
4711 ADDRESS_P is true iff this expression was immediately preceded by
4712 "&" and therefore might denote a pointer-to-member. CAST_P is true
4713 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4714 true iff this expression is a template argument.
4716 Returns a representation of the expression. Upon return, *IDK
4717 indicates what kind of id-expression (if any) was present. */
4720 cp_parser_primary_expression (cp_parser *parser,
4723 bool template_arg_p,
4727 cp_token *token = NULL;
4729 /* Assume the primary expression is not an id-expression. */
4730 *idk = CP_ID_KIND_NONE;
4732 /* Peek at the next token. */
4733 token = cp_lexer_peek_token (parser->lexer);
4734 switch ((int) token->type)
4743 user-defined-literal */
4750 case CPP_PREPARSED_EXPR:
4751 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4752 return cp_parser_userdef_numeric_literal (parser);
4753 token = cp_lexer_consume_token (parser->lexer);
4754 if (TREE_CODE (token->u.value) == FIXED_CST)
4756 error_at (token->location,
4757 "fixed-point types not supported in C++");
4758 return error_mark_node;
4760 /* Floating-point literals are only allowed in an integral
4761 constant expression if they are cast to an integral or
4762 enumeration type. */
4763 if (TREE_CODE (token->u.value) == REAL_CST
4764 && parser->integral_constant_expression_p
4767 /* CAST_P will be set even in invalid code like "int(2.7 +
4768 ...)". Therefore, we have to check that the next token
4769 is sure to end the cast. */
4772 cp_token *next_token;
4774 next_token = cp_lexer_peek_token (parser->lexer);
4775 if (/* The comma at the end of an
4776 enumerator-definition. */
4777 next_token->type != CPP_COMMA
4778 /* The curly brace at the end of an enum-specifier. */
4779 && next_token->type != CPP_CLOSE_BRACE
4780 /* The end of a statement. */
4781 && next_token->type != CPP_SEMICOLON
4782 /* The end of the cast-expression. */
4783 && next_token->type != CPP_CLOSE_PAREN
4784 /* The end of an array bound. */
4785 && next_token->type != CPP_CLOSE_SQUARE
4786 /* The closing ">" in a template-argument-list. */
4787 && (next_token->type != CPP_GREATER
4788 || parser->greater_than_is_operator_p)
4789 /* C++0x only: A ">>" treated like two ">" tokens,
4790 in a template-argument-list. */
4791 && (next_token->type != CPP_RSHIFT
4792 || (cxx_dialect == cxx98)
4793 || parser->greater_than_is_operator_p))
4797 /* If we are within a cast, then the constraint that the
4798 cast is to an integral or enumeration type will be
4799 checked at that point. If we are not within a cast, then
4800 this code is invalid. */
4802 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4804 return cp_expr (token->u.value, token->location);
4806 case CPP_CHAR_USERDEF:
4807 case CPP_CHAR16_USERDEF:
4808 case CPP_CHAR32_USERDEF:
4809 case CPP_WCHAR_USERDEF:
4810 case CPP_UTF8CHAR_USERDEF:
4811 return cp_parser_userdef_char_literal (parser);
4817 case CPP_UTF8STRING:
4818 case CPP_STRING_USERDEF:
4819 case CPP_STRING16_USERDEF:
4820 case CPP_STRING32_USERDEF:
4821 case CPP_WSTRING_USERDEF:
4822 case CPP_UTF8STRING_USERDEF:
4823 /* ??? Should wide strings be allowed when parser->translate_strings_p
4824 is false (i.e. in attributes)? If not, we can kill the third
4825 argument to cp_parser_string_literal. */
4826 return cp_parser_string_literal (parser,
4827 parser->translate_strings_p,
4830 case CPP_OPEN_PAREN:
4831 /* If we see `( { ' then we are looking at the beginning of
4832 a GNU statement-expression. */
4833 if (cp_parser_allow_gnu_extensions_p (parser)
4834 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4836 /* Statement-expressions are not allowed by the standard. */
4837 pedwarn (token->location, OPT_Wpedantic,
4838 "ISO C++ forbids braced-groups within expressions");
4840 /* And they're not allowed outside of a function-body; you
4841 cannot, for example, write:
4843 int i = ({ int j = 3; j + 1; });
4845 at class or namespace scope. */
4846 if (!parser->in_function_body
4847 || parser->in_template_argument_list_p)
4849 error_at (token->location,
4850 "statement-expressions are not allowed outside "
4851 "functions nor in template-argument lists");
4852 cp_parser_skip_to_end_of_block_or_statement (parser);
4853 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4854 cp_lexer_consume_token (parser->lexer);
4855 return error_mark_node;
4858 return cp_parser_statement_expr (parser);
4860 /* Otherwise it's a normal parenthesized expression. */
4863 bool saved_greater_than_is_operator_p;
4865 location_t open_paren_loc = token->location;
4867 /* Consume the `('. */
4868 cp_lexer_consume_token (parser->lexer);
4869 /* Within a parenthesized expression, a `>' token is always
4870 the greater-than operator. */
4871 saved_greater_than_is_operator_p
4872 = parser->greater_than_is_operator_p;
4873 parser->greater_than_is_operator_p = true;
4875 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4876 /* Left fold expression. */
4879 /* Parse the parenthesized expression. */
4880 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4882 token = cp_lexer_peek_token (parser->lexer);
4883 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4885 expr = cp_parser_fold_expression (parser, expr);
4886 if (expr != error_mark_node
4887 && cxx_dialect < cxx1z
4888 && !in_system_header_at (input_location))
4889 pedwarn (input_location, 0, "fold-expressions only available "
4890 "with -std=c++1z or -std=gnu++1z");
4893 /* Let the front end know that this expression was
4894 enclosed in parentheses. This matters in case, for
4895 example, the expression is of the form `A::B', since
4896 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4898 expr = finish_parenthesized_expr (expr);
4900 /* DR 705: Wrapping an unqualified name in parentheses
4901 suppresses arg-dependent lookup. We want to pass back
4902 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4903 (c++/37862), but none of the others. */
4904 if (*idk != CP_ID_KIND_QUALIFIED)
4905 *idk = CP_ID_KIND_NONE;
4907 /* The `>' token might be the end of a template-id or
4908 template-parameter-list now. */
4909 parser->greater_than_is_operator_p
4910 = saved_greater_than_is_operator_p;
4912 /* Consume the `)'. */
4913 token = cp_lexer_peek_token (parser->lexer);
4914 location_t close_paren_loc = token->location;
4915 expr.set_range (open_paren_loc, close_paren_loc);
4916 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4917 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4918 cp_parser_skip_to_end_of_statement (parser);
4923 case CPP_OPEN_SQUARE:
4925 if (c_dialect_objc ())
4927 /* We might have an Objective-C++ message. */
4928 cp_parser_parse_tentatively (parser);
4929 tree msg = cp_parser_objc_message_expression (parser);
4930 /* If that works out, we're done ... */
4931 if (cp_parser_parse_definitely (parser))
4933 /* ... else, fall though to see if it's a lambda. */
4935 cp_expr lam = cp_parser_lambda_expression (parser);
4936 /* Don't warn about a failed tentative parse. */
4937 if (cp_parser_error_occurred (parser))
4938 return error_mark_node;
4939 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4943 case CPP_OBJC_STRING:
4944 if (c_dialect_objc ())
4945 /* We have an Objective-C++ string literal. */
4946 return cp_parser_objc_expression (parser);
4947 cp_parser_error (parser, "expected primary-expression");
4948 return error_mark_node;
4951 switch (token->keyword)
4953 /* These two are the boolean literals. */
4955 cp_lexer_consume_token (parser->lexer);
4956 return cp_expr (boolean_true_node, token->location);
4958 cp_lexer_consume_token (parser->lexer);
4959 return cp_expr (boolean_false_node, token->location);
4961 /* The `__null' literal. */
4963 cp_lexer_consume_token (parser->lexer);
4964 return cp_expr (null_node, token->location);
4966 /* The `nullptr' literal. */
4968 cp_lexer_consume_token (parser->lexer);
4969 return cp_expr (nullptr_node, token->location);
4971 /* Recognize the `this' keyword. */
4973 cp_lexer_consume_token (parser->lexer);
4974 if (parser->local_variables_forbidden_p)
4976 error_at (token->location,
4977 "%<this%> may not be used in this context");
4978 return error_mark_node;
4980 /* Pointers cannot appear in constant-expressions. */
4981 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4982 return error_mark_node;
4983 return cp_expr (finish_this_expr (), token->location);
4985 /* The `operator' keyword can be the beginning of an
4990 case RID_FUNCTION_NAME:
4991 case RID_PRETTY_FUNCTION_NAME:
4992 case RID_C99_FUNCTION_NAME:
4994 non_integral_constant name;
4996 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4997 __func__ are the names of variables -- but they are
4998 treated specially. Therefore, they are handled here,
4999 rather than relying on the generic id-expression logic
5000 below. Grammatically, these names are id-expressions.
5002 Consume the token. */
5003 token = cp_lexer_consume_token (parser->lexer);
5005 switch (token->keyword)
5007 case RID_FUNCTION_NAME:
5008 name = NIC_FUNC_NAME;
5010 case RID_PRETTY_FUNCTION_NAME:
5011 name = NIC_PRETTY_FUNC;
5013 case RID_C99_FUNCTION_NAME:
5014 name = NIC_C99_FUNC;
5020 if (cp_parser_non_integral_constant_expression (parser, name))
5021 return error_mark_node;
5023 /* Look up the name. */
5024 return finish_fname (token->u.value);
5031 source_location type_location;
5032 location_t start_loc
5033 = cp_lexer_peek_token (parser->lexer)->location;
5034 /* The `__builtin_va_arg' construct is used to handle
5035 `va_arg'. Consume the `__builtin_va_arg' token. */
5036 cp_lexer_consume_token (parser->lexer);
5037 /* Look for the opening `('. */
5038 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5039 /* Now, parse the assignment-expression. */
5040 expression = cp_parser_assignment_expression (parser);
5041 /* Look for the `,'. */
5042 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5043 type_location = cp_lexer_peek_token (parser->lexer)->location;
5044 /* Parse the type-id. */
5046 type_id_in_expr_sentinel s (parser);
5047 type = cp_parser_type_id (parser);
5049 /* Look for the closing `)'. */
5050 location_t finish_loc
5051 = cp_lexer_peek_token (parser->lexer)->location;
5052 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5053 /* Using `va_arg' in a constant-expression is not
5055 if (cp_parser_non_integral_constant_expression (parser,
5057 return error_mark_node;
5058 /* Construct a location of the form:
5059 __builtin_va_arg (v, int)
5060 ~~~~~~~~~~~~~~~~~~~~~^~~~
5061 with the caret at the type, ranging from the start of the
5062 "__builtin_va_arg" token to the close paren. */
5063 location_t combined_loc
5064 = make_location (type_location, start_loc, finish_loc);
5065 return build_x_va_arg (combined_loc, expression, type);
5069 return cp_parser_builtin_offsetof (parser);
5071 case RID_HAS_NOTHROW_ASSIGN:
5072 case RID_HAS_NOTHROW_CONSTRUCTOR:
5073 case RID_HAS_NOTHROW_COPY:
5074 case RID_HAS_TRIVIAL_ASSIGN:
5075 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5076 case RID_HAS_TRIVIAL_COPY:
5077 case RID_HAS_TRIVIAL_DESTRUCTOR:
5078 case RID_HAS_VIRTUAL_DESTRUCTOR:
5079 case RID_IS_ABSTRACT:
5080 case RID_IS_BASE_OF:
5085 case RID_IS_LITERAL_TYPE:
5087 case RID_IS_POLYMORPHIC:
5088 case RID_IS_SAME_AS:
5089 case RID_IS_STD_LAYOUT:
5090 case RID_IS_TRIVIAL:
5091 case RID_IS_TRIVIALLY_ASSIGNABLE:
5092 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5093 case RID_IS_TRIVIALLY_COPYABLE:
5095 return cp_parser_trait_expr (parser, token->keyword);
5099 return cp_parser_requires_expression (parser);
5101 /* Objective-C++ expressions. */
5103 case RID_AT_PROTOCOL:
5104 case RID_AT_SELECTOR:
5105 return cp_parser_objc_expression (parser);
5108 if (parser->in_function_body
5109 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5112 error_at (token->location,
5113 "a template declaration cannot appear at block scope");
5114 cp_parser_skip_to_end_of_block_or_statement (parser);
5115 return error_mark_node;
5118 cp_parser_error (parser, "expected primary-expression");
5119 return error_mark_node;
5122 /* An id-expression can start with either an identifier, a
5123 `::' as the beginning of a qualified-id, or the "operator"
5127 case CPP_TEMPLATE_ID:
5128 case CPP_NESTED_NAME_SPECIFIER:
5131 cp_expr id_expression;
5133 const char *error_msg;
5136 cp_token *id_expr_token;
5138 /* Parse the id-expression. */
5140 = cp_parser_id_expression (parser,
5141 /*template_keyword_p=*/false,
5142 /*check_dependency_p=*/true,
5144 /*declarator_p=*/false,
5145 /*optional_p=*/false);
5146 if (id_expression == error_mark_node)
5147 return error_mark_node;
5148 id_expr_token = token;
5149 token = cp_lexer_peek_token (parser->lexer);
5150 done = (token->type != CPP_OPEN_SQUARE
5151 && token->type != CPP_OPEN_PAREN
5152 && token->type != CPP_DOT
5153 && token->type != CPP_DEREF
5154 && token->type != CPP_PLUS_PLUS
5155 && token->type != CPP_MINUS_MINUS);
5156 /* If we have a template-id, then no further lookup is
5157 required. If the template-id was for a template-class, we
5158 will sometimes have a TYPE_DECL at this point. */
5159 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5160 || TREE_CODE (id_expression) == TYPE_DECL)
5161 decl = id_expression;
5162 /* Look up the name. */
5165 tree ambiguous_decls;
5167 /* If we already know that this lookup is ambiguous, then
5168 we've already issued an error message; there's no reason
5170 if (id_expr_token->type == CPP_NAME
5171 && id_expr_token->error_reported)
5173 cp_parser_simulate_error (parser);
5174 return error_mark_node;
5177 decl = cp_parser_lookup_name (parser, id_expression,
5180 /*is_namespace=*/false,
5181 /*check_dependency=*/true,
5183 id_expr_token->location);
5184 /* If the lookup was ambiguous, an error will already have
5186 if (ambiguous_decls)
5187 return error_mark_node;
5189 /* In Objective-C++, we may have an Objective-C 2.0
5190 dot-syntax for classes here. */
5191 if (c_dialect_objc ()
5192 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5193 && TREE_CODE (decl) == TYPE_DECL
5194 && objc_is_class_name (decl))
5197 cp_lexer_consume_token (parser->lexer);
5198 component = cp_parser_identifier (parser);
5199 if (component == error_mark_node)
5200 return error_mark_node;
5202 tree result = objc_build_class_component_ref (id_expression,
5204 /* Build a location of the form:
5207 with caret at the start of the component name (at
5208 input_location), ranging from the start of the id_expression
5209 to the end of the component name. */
5210 location_t combined_loc
5211 = make_location (input_location, id_expression.get_start (),
5212 get_finish (input_location));
5213 protected_set_expr_location (result, combined_loc);
5217 /* In Objective-C++, an instance variable (ivar) may be preferred
5218 to whatever cp_parser_lookup_name() found.
5219 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5220 rest of c-family, we have to do a little extra work to preserve
5221 any location information in cp_expr "decl". Given that
5222 objc_lookup_ivar is implemented in "c-family" and "objc", we
5223 have a trip through the pure "tree" type, rather than cp_expr.
5224 Naively copying it back to "decl" would implicitly give the
5225 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5226 store an EXPR_LOCATION. Hence we only update "decl" (and
5227 hence its location_t) if we get back a different tree node. */
5228 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5230 if (decl_tree != decl.get_value ())
5231 decl = cp_expr (decl_tree);
5233 /* If name lookup gives us a SCOPE_REF, then the
5234 qualifying scope was dependent. */
5235 if (TREE_CODE (decl) == SCOPE_REF)
5237 /* At this point, we do not know if DECL is a valid
5238 integral constant expression. We assume that it is
5239 in fact such an expression, so that code like:
5241 template <int N> struct A {
5245 is accepted. At template-instantiation time, we
5246 will check that B<N>::i is actually a constant. */
5249 /* Check to see if DECL is a local variable in a context
5250 where that is forbidden. */
5251 if (parser->local_variables_forbidden_p
5252 && local_variable_p (decl))
5254 /* It might be that we only found DECL because we are
5255 trying to be generous with pre-ISO scoping rules.
5256 For example, consider:
5260 for (int i = 0; i < 10; ++i) {}
5261 extern void f(int j = i);
5264 Here, name look up will originally find the out
5265 of scope `i'. We need to issue a warning message,
5266 but then use the global `i'. */
5267 decl = check_for_out_of_scope_variable (decl);
5268 if (local_variable_p (decl))
5270 error_at (id_expr_token->location,
5271 "local variable %qD may not appear in this context",
5273 return error_mark_node;
5278 decl = (finish_id_expression
5279 (id_expression, decl, parser->scope,
5281 parser->integral_constant_expression_p,
5282 parser->allow_non_integral_constant_expression_p,
5283 &parser->non_integral_constant_expression_p,
5284 template_p, done, address_p,
5287 id_expr_token->location));
5289 cp_parser_error (parser, error_msg);
5290 decl.set_location (id_expr_token->location);
5294 /* Anything else is an error. */
5296 cp_parser_error (parser, "expected primary-expression");
5297 return error_mark_node;
5301 static inline cp_expr
5302 cp_parser_primary_expression (cp_parser *parser,
5305 bool template_arg_p,
5308 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5309 /*decltype*/false, idk);
5312 /* Parse an id-expression.
5319 :: [opt] nested-name-specifier template [opt] unqualified-id
5321 :: operator-function-id
5324 Return a representation of the unqualified portion of the
5325 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5326 a `::' or nested-name-specifier.
5328 Often, if the id-expression was a qualified-id, the caller will
5329 want to make a SCOPE_REF to represent the qualified-id. This
5330 function does not do this in order to avoid wastefully creating
5331 SCOPE_REFs when they are not required.
5333 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5336 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5337 uninstantiated templates.
5339 If *TEMPLATE_P is non-NULL, it is set to true iff the
5340 `template' keyword is used to explicitly indicate that the entity
5341 named is a template.
5343 If DECLARATOR_P is true, the id-expression is appearing as part of
5344 a declarator, rather than as part of an expression. */
5347 cp_parser_id_expression (cp_parser *parser,
5348 bool template_keyword_p,
5349 bool check_dependency_p,
5354 bool global_scope_p;
5355 bool nested_name_specifier_p;
5357 /* Assume the `template' keyword was not used. */
5359 *template_p = template_keyword_p;
5361 /* Look for the optional `::' operator. */
5363 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5365 /* Look for the optional nested-name-specifier. */
5366 nested_name_specifier_p
5367 = (cp_parser_nested_name_specifier_opt (parser,
5368 /*typename_keyword_p=*/false,
5373 /* If there is a nested-name-specifier, then we are looking at
5374 the first qualified-id production. */
5375 if (nested_name_specifier_p)
5378 tree saved_object_scope;
5379 tree saved_qualifying_scope;
5380 tree unqualified_id;
5383 /* See if the next token is the `template' keyword. */
5385 template_p = &is_template;
5386 *template_p = cp_parser_optional_template_keyword (parser);
5387 /* Name lookup we do during the processing of the
5388 unqualified-id might obliterate SCOPE. */
5389 saved_scope = parser->scope;
5390 saved_object_scope = parser->object_scope;
5391 saved_qualifying_scope = parser->qualifying_scope;
5392 /* Process the final unqualified-id. */
5393 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5396 /*optional_p=*/false);
5397 /* Restore the SAVED_SCOPE for our caller. */
5398 parser->scope = saved_scope;
5399 parser->object_scope = saved_object_scope;
5400 parser->qualifying_scope = saved_qualifying_scope;
5402 return unqualified_id;
5404 /* Otherwise, if we are in global scope, then we are looking at one
5405 of the other qualified-id productions. */
5406 else if (global_scope_p)
5411 /* Peek at the next token. */
5412 token = cp_lexer_peek_token (parser->lexer);
5414 /* If it's an identifier, and the next token is not a "<", then
5415 we can avoid the template-id case. This is an optimization
5416 for this common case. */
5417 if (token->type == CPP_NAME
5418 && !cp_parser_nth_token_starts_template_argument_list_p
5420 return cp_parser_identifier (parser);
5422 cp_parser_parse_tentatively (parser);
5423 /* Try a template-id. */
5424 id = cp_parser_template_id (parser,
5425 /*template_keyword_p=*/false,
5426 /*check_dependency_p=*/true,
5429 /* If that worked, we're done. */
5430 if (cp_parser_parse_definitely (parser))
5433 /* Peek at the next token. (Changes in the token buffer may
5434 have invalidated the pointer obtained above.) */
5435 token = cp_lexer_peek_token (parser->lexer);
5437 switch (token->type)
5440 return cp_parser_identifier (parser);
5443 if (token->keyword == RID_OPERATOR)
5444 return cp_parser_operator_function_id (parser);
5448 cp_parser_error (parser, "expected id-expression");
5449 return error_mark_node;
5453 return cp_parser_unqualified_id (parser, template_keyword_p,
5454 /*check_dependency_p=*/true,
5459 /* Parse an unqualified-id.
5463 operator-function-id
5464 conversion-function-id
5468 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5469 keyword, in a construct like `A::template ...'.
5471 Returns a representation of unqualified-id. For the `identifier'
5472 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5473 production a BIT_NOT_EXPR is returned; the operand of the
5474 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5475 other productions, see the documentation accompanying the
5476 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5477 names are looked up in uninstantiated templates. If DECLARATOR_P
5478 is true, the unqualified-id is appearing as part of a declarator,
5479 rather than as part of an expression. */
5482 cp_parser_unqualified_id (cp_parser* parser,
5483 bool template_keyword_p,
5484 bool check_dependency_p,
5490 /* Peek at the next token. */
5491 token = cp_lexer_peek_token (parser->lexer);
5493 switch ((int) token->type)
5499 /* We don't know yet whether or not this will be a
5501 cp_parser_parse_tentatively (parser);
5502 /* Try a template-id. */
5503 id = cp_parser_template_id (parser, template_keyword_p,
5507 /* If it worked, we're done. */
5508 if (cp_parser_parse_definitely (parser))
5510 /* Otherwise, it's an ordinary identifier. */
5511 return cp_parser_identifier (parser);
5514 case CPP_TEMPLATE_ID:
5515 return cp_parser_template_id (parser, template_keyword_p,
5523 tree qualifying_scope;
5528 /* Consume the `~' token. */
5529 cp_lexer_consume_token (parser->lexer);
5530 /* Parse the class-name. The standard, as written, seems to
5533 template <typename T> struct S { ~S (); };
5534 template <typename T> S<T>::~S() {}
5536 is invalid, since `~' must be followed by a class-name, but
5537 `S<T>' is dependent, and so not known to be a class.
5538 That's not right; we need to look in uninstantiated
5539 templates. A further complication arises from:
5541 template <typename T> void f(T t) {
5545 Here, it is not possible to look up `T' in the scope of `T'
5546 itself. We must look in both the current scope, and the
5547 scope of the containing complete expression.
5549 Yet another issue is:
5558 The standard does not seem to say that the `S' in `~S'
5559 should refer to the type `S' and not the data member
5562 /* DR 244 says that we look up the name after the "~" in the
5563 same scope as we looked up the qualifying name. That idea
5564 isn't fully worked out; it's more complicated than that. */
5565 scope = parser->scope;
5566 object_scope = parser->object_scope;
5567 qualifying_scope = parser->qualifying_scope;
5569 /* Check for invalid scopes. */
5570 if (scope == error_mark_node)
5572 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5573 cp_lexer_consume_token (parser->lexer);
5574 return error_mark_node;
5576 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5578 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5579 error_at (token->location,
5580 "scope %qT before %<~%> is not a class-name",
5582 cp_parser_simulate_error (parser);
5583 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5584 cp_lexer_consume_token (parser->lexer);
5585 return error_mark_node;
5587 gcc_assert (!scope || TYPE_P (scope));
5589 /* If the name is of the form "X::~X" it's OK even if X is a
5591 token = cp_lexer_peek_token (parser->lexer);
5593 && token->type == CPP_NAME
5594 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5596 && (token->u.value == TYPE_IDENTIFIER (scope)
5597 || (CLASS_TYPE_P (scope)
5598 && constructor_name_p (token->u.value, scope))))
5600 cp_lexer_consume_token (parser->lexer);
5601 return build_nt (BIT_NOT_EXPR, scope);
5604 /* ~auto means the destructor of whatever the object is. */
5605 if (cp_parser_is_keyword (token, RID_AUTO))
5607 if (cxx_dialect < cxx14)
5608 pedwarn (input_location, 0,
5609 "%<~auto%> only available with "
5610 "-std=c++14 or -std=gnu++14");
5611 cp_lexer_consume_token (parser->lexer);
5612 return build_nt (BIT_NOT_EXPR, make_auto ());
5615 /* If there was an explicit qualification (S::~T), first look
5616 in the scope given by the qualification (i.e., S).
5618 Note: in the calls to cp_parser_class_name below we pass
5619 typename_type so that lookup finds the injected-class-name
5620 rather than the constructor. */
5622 type_decl = NULL_TREE;
5625 cp_parser_parse_tentatively (parser);
5626 type_decl = cp_parser_class_name (parser,
5627 /*typename_keyword_p=*/false,
5628 /*template_keyword_p=*/false,
5630 /*check_dependency=*/false,
5631 /*class_head_p=*/false,
5633 if (cp_parser_parse_definitely (parser))
5636 /* In "N::S::~S", look in "N" as well. */
5637 if (!done && scope && qualifying_scope)
5639 cp_parser_parse_tentatively (parser);
5640 parser->scope = qualifying_scope;
5641 parser->object_scope = NULL_TREE;
5642 parser->qualifying_scope = NULL_TREE;
5644 = cp_parser_class_name (parser,
5645 /*typename_keyword_p=*/false,
5646 /*template_keyword_p=*/false,
5648 /*check_dependency=*/false,
5649 /*class_head_p=*/false,
5651 if (cp_parser_parse_definitely (parser))
5654 /* In "p->S::~T", look in the scope given by "*p" as well. */
5655 else if (!done && object_scope)
5657 cp_parser_parse_tentatively (parser);
5658 parser->scope = object_scope;
5659 parser->object_scope = NULL_TREE;
5660 parser->qualifying_scope = NULL_TREE;
5662 = cp_parser_class_name (parser,
5663 /*typename_keyword_p=*/false,
5664 /*template_keyword_p=*/false,
5666 /*check_dependency=*/false,
5667 /*class_head_p=*/false,
5669 if (cp_parser_parse_definitely (parser))
5672 /* Look in the surrounding context. */
5675 parser->scope = NULL_TREE;
5676 parser->object_scope = NULL_TREE;
5677 parser->qualifying_scope = NULL_TREE;
5678 if (processing_template_decl)
5679 cp_parser_parse_tentatively (parser);
5681 = cp_parser_class_name (parser,
5682 /*typename_keyword_p=*/false,
5683 /*template_keyword_p=*/false,
5685 /*check_dependency=*/false,
5686 /*class_head_p=*/false,
5688 if (processing_template_decl
5689 && ! cp_parser_parse_definitely (parser))
5691 /* We couldn't find a type with this name. If we're parsing
5692 tentatively, fail and try something else. */
5693 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5695 cp_parser_simulate_error (parser);
5696 return error_mark_node;
5698 /* Otherwise, accept it and check for a match at instantiation
5700 type_decl = cp_parser_identifier (parser);
5701 if (type_decl != error_mark_node)
5702 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5706 /* If an error occurred, assume that the name of the
5707 destructor is the same as the name of the qualifying
5708 class. That allows us to keep parsing after running
5709 into ill-formed destructor names. */
5710 if (type_decl == error_mark_node && scope)
5711 return build_nt (BIT_NOT_EXPR, scope);
5712 else if (type_decl == error_mark_node)
5713 return error_mark_node;
5715 /* Check that destructor name and scope match. */
5716 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5718 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5719 error_at (token->location,
5720 "declaration of %<~%T%> as member of %qT",
5722 cp_parser_simulate_error (parser);
5723 return error_mark_node;
5728 A typedef-name that names a class shall not be used as the
5729 identifier in the declarator for a destructor declaration. */
5731 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5732 && !DECL_SELF_REFERENCE_P (type_decl)
5733 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5734 error_at (token->location,
5735 "typedef-name %qD used as destructor declarator",
5738 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5742 if (token->keyword == RID_OPERATOR)
5746 /* This could be a template-id, so we try that first. */
5747 cp_parser_parse_tentatively (parser);
5748 /* Try a template-id. */
5749 id = cp_parser_template_id (parser, template_keyword_p,
5750 /*check_dependency_p=*/true,
5753 /* If that worked, we're done. */
5754 if (cp_parser_parse_definitely (parser))
5756 /* We still don't know whether we're looking at an
5757 operator-function-id or a conversion-function-id. */
5758 cp_parser_parse_tentatively (parser);
5759 /* Try an operator-function-id. */
5760 id = cp_parser_operator_function_id (parser);
5761 /* If that didn't work, try a conversion-function-id. */
5762 if (!cp_parser_parse_definitely (parser))
5763 id = cp_parser_conversion_function_id (parser);
5764 else if (UDLIT_OPER_P (id))
5767 const char *name = UDLIT_OP_SUFFIX (id);
5768 if (name[0] != '_' && !in_system_header_at (input_location)
5770 warning (0, "literal operator suffixes not preceded by %<_%>"
5771 " are reserved for future standardization");
5781 cp_parser_error (parser, "expected unqualified-id");
5782 return error_mark_node;
5786 /* Parse an (optional) nested-name-specifier.
5788 nested-name-specifier: [C++98]
5789 class-or-namespace-name :: nested-name-specifier [opt]
5790 class-or-namespace-name :: template nested-name-specifier [opt]
5792 nested-name-specifier: [C++0x]
5795 nested-name-specifier identifier ::
5796 nested-name-specifier template [opt] simple-template-id ::
5798 PARSER->SCOPE should be set appropriately before this function is
5799 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5800 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5803 Sets PARSER->SCOPE to the class (TYPE) or namespace
5804 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5805 it unchanged if there is no nested-name-specifier. Returns the new
5806 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5808 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5809 part of a declaration and/or decl-specifier. */
5812 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5813 bool typename_keyword_p,
5814 bool check_dependency_p,
5816 bool is_declaration)
5818 bool success = false;
5819 cp_token_position start = 0;
5822 /* Remember where the nested-name-specifier starts. */
5823 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5825 start = cp_lexer_token_position (parser->lexer, false);
5826 push_deferring_access_checks (dk_deferred);
5833 tree saved_qualifying_scope;
5834 bool template_keyword_p;
5836 /* Spot cases that cannot be the beginning of a
5837 nested-name-specifier. */
5838 token = cp_lexer_peek_token (parser->lexer);
5840 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5841 the already parsed nested-name-specifier. */
5842 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5844 /* Grab the nested-name-specifier and continue the loop. */
5845 cp_parser_pre_parsed_nested_name_specifier (parser);
5846 /* If we originally encountered this nested-name-specifier
5847 with IS_DECLARATION set to false, we will not have
5848 resolved TYPENAME_TYPEs, so we must do so here. */
5850 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5852 new_scope = resolve_typename_type (parser->scope,
5853 /*only_current_p=*/false);
5854 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5855 parser->scope = new_scope;
5861 /* Spot cases that cannot be the beginning of a
5862 nested-name-specifier. On the second and subsequent times
5863 through the loop, we look for the `template' keyword. */
5864 if (success && token->keyword == RID_TEMPLATE)
5866 /* A template-id can start a nested-name-specifier. */
5867 else if (token->type == CPP_TEMPLATE_ID)
5869 /* DR 743: decltype can be used in a nested-name-specifier. */
5870 else if (token_is_decltype (token))
5874 /* If the next token is not an identifier, then it is
5875 definitely not a type-name or namespace-name. */
5876 if (token->type != CPP_NAME)
5878 /* If the following token is neither a `<' (to begin a
5879 template-id), nor a `::', then we are not looking at a
5880 nested-name-specifier. */
5881 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5883 if (token->type == CPP_COLON
5884 && parser->colon_corrects_to_scope_p
5885 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5887 error_at (token->location,
5888 "found %<:%> in nested-name-specifier, expected %<::%>");
5889 token->type = CPP_SCOPE;
5892 if (token->type != CPP_SCOPE
5893 && !cp_parser_nth_token_starts_template_argument_list_p
5898 /* The nested-name-specifier is optional, so we parse
5900 cp_parser_parse_tentatively (parser);
5902 /* Look for the optional `template' keyword, if this isn't the
5903 first time through the loop. */
5905 template_keyword_p = cp_parser_optional_template_keyword (parser);
5907 template_keyword_p = false;
5909 /* Save the old scope since the name lookup we are about to do
5910 might destroy it. */
5911 old_scope = parser->scope;
5912 saved_qualifying_scope = parser->qualifying_scope;
5913 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5914 look up names in "X<T>::I" in order to determine that "Y" is
5915 a template. So, if we have a typename at this point, we make
5916 an effort to look through it. */
5918 && !typename_keyword_p
5920 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5921 parser->scope = resolve_typename_type (parser->scope,
5922 /*only_current_p=*/false);
5923 /* Parse the qualifying entity. */
5925 = cp_parser_qualifying_entity (parser,
5931 /* Look for the `::' token. */
5932 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5934 /* If we found what we wanted, we keep going; otherwise, we're
5936 if (!cp_parser_parse_definitely (parser))
5938 bool error_p = false;
5940 /* Restore the OLD_SCOPE since it was valid before the
5941 failed attempt at finding the last
5942 class-or-namespace-name. */
5943 parser->scope = old_scope;
5944 parser->qualifying_scope = saved_qualifying_scope;
5946 /* If the next token is a decltype, and the one after that is a
5947 `::', then the decltype has failed to resolve to a class or
5948 enumeration type. Give this error even when parsing
5949 tentatively since it can't possibly be valid--and we're going
5950 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5951 won't get another chance.*/
5952 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5953 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5956 token = cp_lexer_consume_token (parser->lexer);
5957 error_at (token->location, "decltype evaluates to %qT, "
5958 "which is not a class or enumeration type",
5959 token->u.tree_check_value->value);
5960 parser->scope = error_mark_node;
5964 cp_lexer_consume_token (parser->lexer);
5967 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5968 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5970 /* If we have a non-type template-id followed by ::, it can't
5971 possibly be valid. */
5972 token = cp_lexer_peek_token (parser->lexer);
5973 tree tid = token->u.tree_check_value->value;
5974 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5975 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5977 tree tmpl = NULL_TREE;
5978 if (is_overloaded_fn (tid))
5980 tree fns = get_fns (tid);
5981 if (!OVL_CHAIN (fns))
5982 tmpl = OVL_CURRENT (fns);
5983 error_at (token->location, "function template-id %qD "
5984 "in nested-name-specifier", tid);
5988 /* Variable template. */
5989 tmpl = TREE_OPERAND (tid, 0);
5990 gcc_assert (variable_template_p (tmpl));
5991 error_at (token->location, "variable template-id %qD "
5992 "in nested-name-specifier", tid);
5995 inform (DECL_SOURCE_LOCATION (tmpl),
5996 "%qD declared here", tmpl);
5998 parser->scope = error_mark_node;
6002 cp_lexer_consume_token (parser->lexer);
6003 cp_lexer_consume_token (parser->lexer);
6007 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6009 /* If the next token is an identifier, and the one after
6010 that is a `::', then any valid interpretation would have
6011 found a class-or-namespace-name. */
6012 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6013 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6015 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6018 token = cp_lexer_consume_token (parser->lexer);
6021 if (!token->error_reported)
6024 tree ambiguous_decls;
6026 decl = cp_parser_lookup_name (parser, token->u.value,
6028 /*is_template=*/false,
6029 /*is_namespace=*/false,
6030 /*check_dependency=*/true,
6033 if (TREE_CODE (decl) == TEMPLATE_DECL)
6034 error_at (token->location,
6035 "%qD used without template parameters",
6037 else if (ambiguous_decls)
6039 // cp_parser_lookup_name has the same diagnostic,
6040 // thus make sure to emit it at most once.
6041 if (cp_parser_uncommitted_to_tentative_parse_p
6044 error_at (token->location,
6045 "reference to %qD is ambiguous",
6047 print_candidates (ambiguous_decls);
6049 decl = error_mark_node;
6053 if (cxx_dialect != cxx98)
6054 cp_parser_name_lookup_error
6055 (parser, token->u.value, decl, NLE_NOT_CXX98,
6058 cp_parser_name_lookup_error
6059 (parser, token->u.value, decl, NLE_CXX98,
6063 parser->scope = error_mark_node;
6065 /* Treat this as a successful nested-name-specifier
6070 If the name found is not a class-name (clause
6071 _class_) or namespace-name (_namespace.def_), the
6072 program is ill-formed. */
6075 cp_lexer_consume_token (parser->lexer);
6079 /* We've found one valid nested-name-specifier. */
6081 /* Name lookup always gives us a DECL. */
6082 if (TREE_CODE (new_scope) == TYPE_DECL)
6083 new_scope = TREE_TYPE (new_scope);
6084 /* Uses of "template" must be followed by actual templates. */
6085 if (template_keyword_p
6086 && !(CLASS_TYPE_P (new_scope)
6087 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6088 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6089 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6090 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6091 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6092 == TEMPLATE_ID_EXPR)))
6093 permerror (input_location, TYPE_P (new_scope)
6094 ? G_("%qT is not a template")
6095 : G_("%qD is not a template"),
6097 /* If it is a class scope, try to complete it; we are about to
6098 be looking up names inside the class. */
6099 if (TYPE_P (new_scope)
6100 /* Since checking types for dependency can be expensive,
6101 avoid doing it if the type is already complete. */
6102 && !COMPLETE_TYPE_P (new_scope)
6103 /* Do not try to complete dependent types. */
6104 && !dependent_type_p (new_scope))
6106 new_scope = complete_type (new_scope);
6107 /* If it is a typedef to current class, use the current
6108 class instead, as the typedef won't have any names inside
6110 if (!COMPLETE_TYPE_P (new_scope)
6111 && currently_open_class (new_scope))
6112 new_scope = TYPE_MAIN_VARIANT (new_scope);
6114 /* Make sure we look in the right scope the next time through
6116 parser->scope = new_scope;
6119 /* If parsing tentatively, replace the sequence of tokens that makes
6120 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6121 token. That way, should we re-parse the token stream, we will
6122 not have to repeat the effort required to do the parse, nor will
6123 we issue duplicate error messages. */
6124 if (success && start)
6128 token = cp_lexer_token_at (parser->lexer, start);
6129 /* Reset the contents of the START token. */
6130 token->type = CPP_NESTED_NAME_SPECIFIER;
6131 /* Retrieve any deferred checks. Do not pop this access checks yet
6132 so the memory will not be reclaimed during token replacing below. */
6133 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6134 token->u.tree_check_value->value = parser->scope;
6135 token->u.tree_check_value->checks = get_deferred_access_checks ();
6136 token->u.tree_check_value->qualifying_scope =
6137 parser->qualifying_scope;
6138 token->keyword = RID_MAX;
6140 /* Purge all subsequent tokens. */
6141 cp_lexer_purge_tokens_after (parser->lexer, start);
6145 pop_to_parent_deferring_access_checks ();
6147 return success ? parser->scope : NULL_TREE;
6150 /* Parse a nested-name-specifier. See
6151 cp_parser_nested_name_specifier_opt for details. This function
6152 behaves identically, except that it will an issue an error if no
6153 nested-name-specifier is present. */
6156 cp_parser_nested_name_specifier (cp_parser *parser,
6157 bool typename_keyword_p,
6158 bool check_dependency_p,
6160 bool is_declaration)
6164 /* Look for the nested-name-specifier. */
6165 scope = cp_parser_nested_name_specifier_opt (parser,
6170 /* If it was not present, issue an error message. */
6173 cp_parser_error (parser, "expected nested-name-specifier");
6174 parser->scope = NULL_TREE;
6180 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6181 this is either a class-name or a namespace-name (which corresponds
6182 to the class-or-namespace-name production in the grammar). For
6183 C++0x, it can also be a type-name that refers to an enumeration
6184 type or a simple-template-id.
6186 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6187 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6188 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6189 TYPE_P is TRUE iff the next name should be taken as a class-name,
6190 even the same name is declared to be another entity in the same
6193 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6194 specified by the class-or-namespace-name. If neither is found the
6195 ERROR_MARK_NODE is returned. */
6198 cp_parser_qualifying_entity (cp_parser *parser,
6199 bool typename_keyword_p,
6200 bool template_keyword_p,
6201 bool check_dependency_p,
6203 bool is_declaration)
6206 tree saved_qualifying_scope;
6207 tree saved_object_scope;
6210 bool successful_parse_p;
6212 /* DR 743: decltype can appear in a nested-name-specifier. */
6213 if (cp_lexer_next_token_is_decltype (parser->lexer))
6215 scope = cp_parser_decltype (parser);
6216 if (TREE_CODE (scope) != ENUMERAL_TYPE
6217 && !MAYBE_CLASS_TYPE_P (scope))
6219 cp_parser_simulate_error (parser);
6220 return error_mark_node;
6222 if (TYPE_NAME (scope))
6223 scope = TYPE_NAME (scope);
6227 /* Before we try to parse the class-name, we must save away the
6228 current PARSER->SCOPE since cp_parser_class_name will destroy
6230 saved_scope = parser->scope;
6231 saved_qualifying_scope = parser->qualifying_scope;
6232 saved_object_scope = parser->object_scope;
6233 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6234 there is no need to look for a namespace-name. */
6235 only_class_p = template_keyword_p
6236 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6238 cp_parser_parse_tentatively (parser);
6239 scope = cp_parser_class_name (parser,
6242 type_p ? class_type : none_type,
6244 /*class_head_p=*/false,
6246 /*enum_ok=*/cxx_dialect > cxx98);
6247 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6248 /* If that didn't work, try for a namespace-name. */
6249 if (!only_class_p && !successful_parse_p)
6251 /* Restore the saved scope. */
6252 parser->scope = saved_scope;
6253 parser->qualifying_scope = saved_qualifying_scope;
6254 parser->object_scope = saved_object_scope;
6255 /* If we are not looking at an identifier followed by the scope
6256 resolution operator, then this is not part of a
6257 nested-name-specifier. (Note that this function is only used
6258 to parse the components of a nested-name-specifier.) */
6259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6260 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6261 return error_mark_node;
6262 scope = cp_parser_namespace_name (parser);
6268 /* Return true if we are looking at a compound-literal, false otherwise. */
6271 cp_parser_compound_literal_p (cp_parser *parser)
6273 /* Consume the `('. */
6274 cp_lexer_consume_token (parser->lexer);
6276 cp_lexer_save_tokens (parser->lexer);
6278 /* Skip tokens until the next token is a closing parenthesis.
6279 If we find the closing `)', and the next token is a `{', then
6280 we are looking at a compound-literal. */
6281 bool compound_literal_p
6282 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6283 /*consume_paren=*/true)
6284 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6286 /* Roll back the tokens we skipped. */
6287 cp_lexer_rollback_tokens (parser->lexer);
6289 return compound_literal_p;
6292 /* Parse a postfix-expression.
6296 postfix-expression [ expression ]
6297 postfix-expression ( expression-list [opt] )
6298 simple-type-specifier ( expression-list [opt] )
6299 typename :: [opt] nested-name-specifier identifier
6300 ( expression-list [opt] )
6301 typename :: [opt] nested-name-specifier template [opt] template-id
6302 ( expression-list [opt] )
6303 postfix-expression . template [opt] id-expression
6304 postfix-expression -> template [opt] id-expression
6305 postfix-expression . pseudo-destructor-name
6306 postfix-expression -> pseudo-destructor-name
6307 postfix-expression ++
6308 postfix-expression --
6309 dynamic_cast < type-id > ( expression )
6310 static_cast < type-id > ( expression )
6311 reinterpret_cast < type-id > ( expression )
6312 const_cast < type-id > ( expression )
6313 typeid ( expression )
6319 ( type-id ) { initializer-list , [opt] }
6321 This extension is a GNU version of the C99 compound-literal
6322 construct. (The C99 grammar uses `type-name' instead of `type-id',
6323 but they are essentially the same concept.)
6325 If ADDRESS_P is true, the postfix expression is the operand of the
6326 `&' operator. CAST_P is true if this expression is the target of a
6329 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6330 class member access expressions [expr.ref].
6332 Returns a representation of the expression. */
6335 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6336 bool member_access_only_p, bool decltype_p,
6337 cp_id_kind * pidk_return)
6342 cp_id_kind idk = CP_ID_KIND_NONE;
6343 cp_expr postfix_expression = NULL_TREE;
6344 bool is_member_access = false;
6345 int saved_in_statement = -1;
6347 /* Peek at the next token. */
6348 token = cp_lexer_peek_token (parser->lexer);
6349 loc = token->location;
6350 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6352 /* Some of the productions are determined by keywords. */
6353 keyword = token->keyword;
6363 const char *saved_message;
6364 bool saved_in_type_id_in_expr_p;
6366 /* All of these can be handled in the same way from the point
6367 of view of parsing. Begin by consuming the token
6368 identifying the cast. */
6369 cp_lexer_consume_token (parser->lexer);
6371 /* New types cannot be defined in the cast. */
6372 saved_message = parser->type_definition_forbidden_message;
6373 parser->type_definition_forbidden_message
6374 = G_("types may not be defined in casts");
6376 /* Look for the opening `<'. */
6377 cp_parser_require (parser, CPP_LESS, RT_LESS);
6378 /* Parse the type to which we are casting. */
6379 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6380 parser->in_type_id_in_expr_p = true;
6381 type = cp_parser_type_id (parser);
6382 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6383 /* Look for the closing `>'. */
6384 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6385 /* Restore the old message. */
6386 parser->type_definition_forbidden_message = saved_message;
6388 bool saved_greater_than_is_operator_p
6389 = parser->greater_than_is_operator_p;
6390 parser->greater_than_is_operator_p = true;
6392 /* And the expression which is being cast. */
6393 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6394 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6395 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6397 location_t end_loc = close_paren ?
6398 close_paren->location : UNKNOWN_LOCATION;
6400 parser->greater_than_is_operator_p
6401 = saved_greater_than_is_operator_p;
6403 /* Only type conversions to integral or enumeration types
6404 can be used in constant-expressions. */
6405 if (!cast_valid_in_integral_constant_expression_p (type)
6406 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6408 postfix_expression = error_mark_node;
6416 = build_dynamic_cast (type, expression, tf_warning_or_error);
6420 = build_static_cast (type, expression, tf_warning_or_error);
6424 = build_reinterpret_cast (type, expression,
6425 tf_warning_or_error);
6429 = build_const_cast (type, expression, tf_warning_or_error);
6435 /* Construct a location e.g. :
6436 reinterpret_cast <int *> (expr)
6437 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6438 ranging from the start of the "*_cast" token to the final closing
6439 paren, with the caret at the start. */
6440 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6441 postfix_expression.set_location (cp_cast_loc);
6448 const char *saved_message;
6449 bool saved_in_type_id_in_expr_p;
6451 /* Consume the `typeid' token. */
6452 cp_lexer_consume_token (parser->lexer);
6453 /* Look for the `(' token. */
6454 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6455 /* Types cannot be defined in a `typeid' expression. */
6456 saved_message = parser->type_definition_forbidden_message;
6457 parser->type_definition_forbidden_message
6458 = G_("types may not be defined in a %<typeid%> expression");
6459 /* We can't be sure yet whether we're looking at a type-id or an
6461 cp_parser_parse_tentatively (parser);
6462 /* Try a type-id first. */
6463 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6464 parser->in_type_id_in_expr_p = true;
6465 type = cp_parser_type_id (parser);
6466 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6467 /* Look for the `)' token. Otherwise, we can't be sure that
6468 we're not looking at an expression: consider `typeid (int
6469 (3))', for example. */
6470 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6471 /* If all went well, simply lookup the type-id. */
6472 if (cp_parser_parse_definitely (parser))
6473 postfix_expression = get_typeid (type, tf_warning_or_error);
6474 /* Otherwise, fall back to the expression variant. */
6479 /* Look for an expression. */
6480 expression = cp_parser_expression (parser, & idk);
6481 /* Compute its typeid. */
6482 postfix_expression = build_typeid (expression, tf_warning_or_error);
6483 /* Look for the `)' token. */
6484 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6486 /* Restore the saved message. */
6487 parser->type_definition_forbidden_message = saved_message;
6488 /* `typeid' may not appear in an integral constant expression. */
6489 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6490 postfix_expression = error_mark_node;
6497 /* The syntax permitted here is the same permitted for an
6498 elaborated-type-specifier. */
6499 ++parser->prevent_constrained_type_specifiers;
6500 type = cp_parser_elaborated_type_specifier (parser,
6501 /*is_friend=*/false,
6502 /*is_declaration=*/false);
6503 --parser->prevent_constrained_type_specifiers;
6504 postfix_expression = cp_parser_functional_cast (parser, type);
6508 case RID_CILK_SPAWN:
6510 location_t cilk_spawn_loc
6511 = cp_lexer_peek_token (parser->lexer)->location;
6512 cp_lexer_consume_token (parser->lexer);
6513 token = cp_lexer_peek_token (parser->lexer);
6514 if (token->type == CPP_SEMICOLON)
6516 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6518 postfix_expression = error_mark_node;
6521 else if (!current_function_decl)
6523 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6524 "inside a function");
6525 postfix_expression = error_mark_node;
6530 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6531 saved_in_statement = parser->in_statement;
6532 parser->in_statement |= IN_CILK_SPAWN;
6534 cfun->calls_cilk_spawn = 1;
6535 postfix_expression =
6536 cp_parser_postfix_expression (parser, false, false,
6537 false, false, &idk);
6540 error_at (token->location, "-fcilkplus must be enabled to use"
6541 " %<_Cilk_spawn%>");
6542 cfun->calls_cilk_spawn = 0;
6544 else if (saved_in_statement & IN_CILK_SPAWN)
6546 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6547 "are not permitted");
6548 postfix_expression = error_mark_node;
6549 cfun->calls_cilk_spawn = 0;
6553 location_t loc = postfix_expression.get_location ();
6554 postfix_expression = build_cilk_spawn (token->location,
6555 postfix_expression);
6556 /* Build a location of the form:
6559 with caret at the expr, ranging from the start of the
6560 _Cilk_spawn token to the end of the expression. */
6561 location_t combined_loc =
6562 make_location (loc, cilk_spawn_loc, get_finish (loc));
6563 postfix_expression.set_location (combined_loc);
6564 if (postfix_expression != error_mark_node)
6565 SET_EXPR_LOCATION (postfix_expression, input_location);
6566 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6571 case RID_BUILTIN_SHUFFLE:
6573 vec<tree, va_gc> *vec;
6577 cp_lexer_consume_token (parser->lexer);
6578 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6579 /*cast_p=*/false, /*allow_expansion_p=*/true,
6580 /*non_constant_p=*/NULL);
6583 postfix_expression = error_mark_node;
6587 FOR_EACH_VEC_ELT (*vec, i, p)
6590 if (vec->length () == 2)
6592 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6593 tf_warning_or_error);
6594 else if (vec->length () == 3)
6596 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6597 tf_warning_or_error);
6600 error_at (loc, "wrong number of arguments to "
6601 "%<__builtin_shuffle%>");
6602 postfix_expression = error_mark_node;
6611 /* If the next thing is a simple-type-specifier, we may be
6612 looking at a functional cast. We could also be looking at
6613 an id-expression. So, we try the functional cast, and if
6614 that doesn't work we fall back to the primary-expression. */
6615 cp_parser_parse_tentatively (parser);
6616 /* Look for the simple-type-specifier. */
6617 ++parser->prevent_constrained_type_specifiers;
6618 type = cp_parser_simple_type_specifier (parser,
6619 /*decl_specs=*/NULL,
6620 CP_PARSER_FLAGS_NONE);
6621 --parser->prevent_constrained_type_specifiers;
6622 /* Parse the cast itself. */
6623 if (!cp_parser_error_occurred (parser))
6625 = cp_parser_functional_cast (parser, type);
6626 /* If that worked, we're done. */
6627 if (cp_parser_parse_definitely (parser))
6630 /* If the functional-cast didn't work out, try a
6631 compound-literal. */
6632 if (cp_parser_allow_gnu_extensions_p (parser)
6633 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6635 cp_expr initializer = NULL_TREE;
6637 cp_parser_parse_tentatively (parser);
6639 /* Avoid calling cp_parser_type_id pointlessly, see comment
6640 in cp_parser_cast_expression about c++/29234. */
6641 if (!cp_parser_compound_literal_p (parser))
6642 cp_parser_simulate_error (parser);
6645 /* Parse the type. */
6646 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6647 parser->in_type_id_in_expr_p = true;
6648 type = cp_parser_type_id (parser);
6649 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6650 /* Look for the `)'. */
6651 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6654 /* If things aren't going well, there's no need to
6656 if (!cp_parser_error_occurred (parser))
6658 bool non_constant_p;
6659 /* Parse the brace-enclosed initializer list. */
6660 initializer = cp_parser_braced_list (parser,
6663 /* If that worked, we're definitely looking at a
6664 compound-literal expression. */
6665 if (cp_parser_parse_definitely (parser))
6667 /* Warn the user that a compound literal is not
6668 allowed in standard C++. */
6669 pedwarn (input_location, OPT_Wpedantic,
6670 "ISO C++ forbids compound-literals");
6671 /* For simplicity, we disallow compound literals in
6672 constant-expressions. We could
6673 allow compound literals of integer type, whose
6674 initializer was a constant, in constant
6675 expressions. Permitting that usage, as a further
6676 extension, would not change the meaning of any
6677 currently accepted programs. (Of course, as
6678 compound literals are not part of ISO C++, the
6679 standard has nothing to say.) */
6680 if (cp_parser_non_integral_constant_expression (parser,
6683 postfix_expression = error_mark_node;
6686 /* Form the representation of the compound-literal. */
6688 = finish_compound_literal (type, initializer,
6689 tf_warning_or_error);
6690 postfix_expression.set_location (initializer.get_location ());
6695 /* It must be a primary-expression. */
6697 = cp_parser_primary_expression (parser, address_p, cast_p,
6698 /*template_arg_p=*/false,
6705 /* Note that we don't need to worry about calling build_cplus_new on a
6706 class-valued CALL_EXPR in decltype when it isn't the end of the
6707 postfix-expression; unary_complex_lvalue will take care of that for
6710 /* Keep looping until the postfix-expression is complete. */
6713 if (idk == CP_ID_KIND_UNQUALIFIED
6714 && identifier_p (postfix_expression)
6715 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6716 /* It is not a Koenig lookup function call. */
6718 = unqualified_name_lookup_error (postfix_expression);
6720 /* Peek at the next token. */
6721 token = cp_lexer_peek_token (parser->lexer);
6723 switch (token->type)
6725 case CPP_OPEN_SQUARE:
6726 if (cp_next_tokens_can_be_std_attribute_p (parser))
6728 cp_parser_error (parser,
6729 "two consecutive %<[%> shall "
6730 "only introduce an attribute");
6731 return error_mark_node;
6734 = cp_parser_postfix_open_square_expression (parser,
6738 postfix_expression.set_range (start_loc,
6739 postfix_expression.get_location ());
6741 idk = CP_ID_KIND_NONE;
6742 is_member_access = false;
6745 case CPP_OPEN_PAREN:
6746 /* postfix-expression ( expression-list [opt] ) */
6749 bool is_builtin_constant_p;
6750 bool saved_integral_constant_expression_p = false;
6751 bool saved_non_integral_constant_expression_p = false;
6752 tsubst_flags_t complain = complain_flags (decltype_p);
6753 vec<tree, va_gc> *args;
6754 location_t close_paren_loc = UNKNOWN_LOCATION;
6756 is_member_access = false;
6758 is_builtin_constant_p
6759 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6760 if (is_builtin_constant_p)
6762 /* The whole point of __builtin_constant_p is to allow
6763 non-constant expressions to appear as arguments. */
6764 saved_integral_constant_expression_p
6765 = parser->integral_constant_expression_p;
6766 saved_non_integral_constant_expression_p
6767 = parser->non_integral_constant_expression_p;
6768 parser->integral_constant_expression_p = false;
6770 args = (cp_parser_parenthesized_expression_list
6772 /*cast_p=*/false, /*allow_expansion_p=*/true,
6773 /*non_constant_p=*/NULL,
6774 /*close_paren_loc=*/&close_paren_loc));
6775 if (is_builtin_constant_p)
6777 parser->integral_constant_expression_p
6778 = saved_integral_constant_expression_p;
6779 parser->non_integral_constant_expression_p
6780 = saved_non_integral_constant_expression_p;
6785 postfix_expression = error_mark_node;
6789 /* Function calls are not permitted in
6790 constant-expressions. */
6791 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6792 && cp_parser_non_integral_constant_expression (parser,
6795 postfix_expression = error_mark_node;
6796 release_tree_vector (args);
6801 if (idk == CP_ID_KIND_UNQUALIFIED
6802 || idk == CP_ID_KIND_TEMPLATE_ID)
6804 if (identifier_p (postfix_expression))
6806 if (!args->is_empty ())
6809 if (!any_type_dependent_arguments_p (args))
6811 = perform_koenig_lookup (postfix_expression, args,
6816 = unqualified_fn_lookup_error (postfix_expression);
6818 /* We do not perform argument-dependent lookup if
6819 normal lookup finds a non-function, in accordance
6820 with the expected resolution of DR 218. */
6821 else if (!args->is_empty ()
6822 && is_overloaded_fn (postfix_expression))
6824 tree fn = get_first_fn (postfix_expression);
6825 fn = STRIP_TEMPLATE (fn);
6827 /* Do not do argument dependent lookup if regular
6828 lookup finds a member function or a block-scope
6829 function declaration. [basic.lookup.argdep]/3 */
6830 if (!DECL_FUNCTION_MEMBER_P (fn)
6831 && !DECL_LOCAL_FUNCTION_P (fn))
6834 if (!any_type_dependent_arguments_p (args))
6836 = perform_koenig_lookup (postfix_expression, args,
6842 if (warn_memset_transposed_args)
6844 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6845 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6846 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6847 && vec_safe_length (args) == 3
6848 && TREE_CODE ((*args)[2]) == INTEGER_CST
6849 && integer_zerop ((*args)[2])
6850 && !(TREE_CODE ((*args)[1]) == INTEGER_CST
6851 && integer_zerop ((*args)[1])))
6852 warning (OPT_Wmemset_transposed_args,
6853 "%<memset%> used with constant zero length "
6854 "parameter; this could be due to transposed "
6858 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6860 tree instance = TREE_OPERAND (postfix_expression, 0);
6861 tree fn = TREE_OPERAND (postfix_expression, 1);
6863 if (processing_template_decl
6864 && (type_dependent_expression_p (instance)
6865 || (!BASELINK_P (fn)
6866 && TREE_CODE (fn) != FIELD_DECL)
6867 || type_dependent_expression_p (fn)
6868 || any_type_dependent_arguments_p (args)))
6871 = build_nt_call_vec (postfix_expression, args);
6872 release_tree_vector (args);
6876 if (BASELINK_P (fn))
6879 = (build_new_method_call
6880 (instance, fn, &args, NULL_TREE,
6881 (idk == CP_ID_KIND_QUALIFIED
6882 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6889 = finish_call_expr (postfix_expression, &args,
6890 /*disallow_virtual=*/false,
6894 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6895 || TREE_CODE (postfix_expression) == MEMBER_REF
6896 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6897 postfix_expression = (build_offset_ref_call_from_tree
6898 (postfix_expression, &args,
6900 else if (idk == CP_ID_KIND_QUALIFIED)
6901 /* A call to a static class member, or a namespace-scope
6904 = finish_call_expr (postfix_expression, &args,
6905 /*disallow_virtual=*/true,
6909 /* All other function calls. */
6911 = finish_call_expr (postfix_expression, &args,
6912 /*disallow_virtual=*/false,
6916 if (close_paren_loc != UNKNOWN_LOCATION)
6918 location_t combined_loc = make_location (token->location,
6921 postfix_expression.set_location (combined_loc);
6924 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6925 idk = CP_ID_KIND_NONE;
6927 release_tree_vector (args);
6933 /* postfix-expression . template [opt] id-expression
6934 postfix-expression . pseudo-destructor-name
6935 postfix-expression -> template [opt] id-expression
6936 postfix-expression -> pseudo-destructor-name */
6938 /* Consume the `.' or `->' operator. */
6939 cp_lexer_consume_token (parser->lexer);
6942 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6946 is_member_access = true;
6950 /* postfix-expression ++ */
6951 /* Consume the `++' token. */
6952 cp_lexer_consume_token (parser->lexer);
6953 /* Generate a representation for the complete expression. */
6955 = finish_increment_expr (postfix_expression,
6956 POSTINCREMENT_EXPR);
6957 /* Increments may not appear in constant-expressions. */
6958 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6959 postfix_expression = error_mark_node;
6960 idk = CP_ID_KIND_NONE;
6961 is_member_access = false;
6964 case CPP_MINUS_MINUS:
6965 /* postfix-expression -- */
6966 /* Consume the `--' token. */
6967 cp_lexer_consume_token (parser->lexer);
6968 /* Generate a representation for the complete expression. */
6970 = finish_increment_expr (postfix_expression,
6971 POSTDECREMENT_EXPR);
6972 /* Decrements may not appear in constant-expressions. */
6973 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6974 postfix_expression = error_mark_node;
6975 idk = CP_ID_KIND_NONE;
6976 is_member_access = false;
6980 if (pidk_return != NULL)
6981 * pidk_return = idk;
6982 if (member_access_only_p)
6983 return is_member_access
6984 ? postfix_expression
6985 : cp_expr (error_mark_node);
6987 return postfix_expression;
6991 /* We should never get here. */
6993 return error_mark_node;
6996 /* This function parses Cilk Plus array notations. If a normal array expr. is
6997 parsed then the array index is passed back to the caller through *INIT_INDEX
6998 and the function returns a NULL_TREE. If array notation expr. is parsed,
6999 then *INIT_INDEX is ignored by the caller and the function returns
7000 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7004 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7007 cp_token *token = NULL;
7008 tree length_index, stride = NULL_TREE, value_tree, array_type;
7009 if (!array_value || array_value == error_mark_node)
7011 cp_parser_skip_to_end_of_statement (parser);
7012 return error_mark_node;
7015 array_type = TREE_TYPE (array_value);
7017 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7018 parser->colon_corrects_to_scope_p = false;
7019 token = cp_lexer_peek_token (parser->lexer);
7023 cp_parser_error (parser, "expected %<:%> or numeral");
7024 return error_mark_node;
7026 else if (token->type == CPP_COLON)
7028 /* Consume the ':'. */
7029 cp_lexer_consume_token (parser->lexer);
7031 /* If we are here, then we have a case like this A[:]. */
7032 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7034 cp_parser_error (parser, "expected %<]%>");
7035 cp_parser_skip_to_end_of_statement (parser);
7036 return error_mark_node;
7038 *init_index = NULL_TREE;
7040 length_index = NULL_TREE;
7044 /* If we are here, then there are three valid possibilities:
7046 2. ARRAY [ EXP : EXP ]
7047 3. ARRAY [ EXP : EXP : EXP ] */
7049 *init_index = cp_parser_expression (parser);
7050 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7052 /* This indicates that we have a normal array expression. */
7053 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7057 /* Consume the ':'. */
7058 cp_lexer_consume_token (parser->lexer);
7059 length_index = cp_parser_expression (parser);
7060 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7062 cp_lexer_consume_token (parser->lexer);
7063 stride = cp_parser_expression (parser);
7066 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7068 if (*init_index == error_mark_node || length_index == error_mark_node
7069 || stride == error_mark_node || array_type == error_mark_node)
7071 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7072 cp_lexer_consume_token (parser->lexer);
7073 return error_mark_node;
7075 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7077 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7078 length_index, stride, array_type);
7082 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7083 by cp_parser_builtin_offsetof. We're looking for
7085 postfix-expression [ expression ]
7086 postfix-expression [ braced-init-list ] (C++11)
7088 FOR_OFFSETOF is set if we're being called in that context, which
7089 changes how we deal with integer constant expressions. */
7092 cp_parser_postfix_open_square_expression (cp_parser *parser,
7093 tree postfix_expression,
7097 tree index = NULL_TREE;
7098 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7099 bool saved_greater_than_is_operator_p;
7101 /* Consume the `[' token. */
7102 cp_lexer_consume_token (parser->lexer);
7104 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7105 parser->greater_than_is_operator_p = true;
7107 /* Parse the index expression. */
7108 /* ??? For offsetof, there is a question of what to allow here. If
7109 offsetof is not being used in an integral constant expression context,
7110 then we *could* get the right answer by computing the value at runtime.
7111 If we are in an integral constant expression context, then we might
7112 could accept any constant expression; hard to say without analysis.
7113 Rather than open the barn door too wide right away, allow only integer
7114 constant expressions here. */
7116 index = cp_parser_constant_expression (parser);
7119 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7121 bool expr_nonconst_p;
7122 cp_lexer_set_source_position (parser->lexer);
7123 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7124 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7126 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7128 error_at (cp_lexer_peek_token (parser->lexer)->location,
7129 "braced list index is not allowed with array "
7131 cp_parser_skip_to_end_of_statement (parser);
7132 return error_mark_node;
7135 else if (flag_cilkplus)
7137 /* Here are have these two options:
7138 ARRAY[EXP : EXP] - Array notation expr with default
7140 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7142 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7143 postfix_expression);
7148 index = cp_parser_expression (parser);
7151 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7153 /* Look for the closing `]'. */
7154 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7156 /* Build the ARRAY_REF. */
7157 postfix_expression = grok_array_decl (loc, postfix_expression,
7160 /* When not doing offsetof, array references are not permitted in
7161 constant-expressions. */
7163 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7164 postfix_expression = error_mark_node;
7166 return postfix_expression;
7169 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7170 by cp_parser_builtin_offsetof. We're looking for
7172 postfix-expression . template [opt] id-expression
7173 postfix-expression . pseudo-destructor-name
7174 postfix-expression -> template [opt] id-expression
7175 postfix-expression -> pseudo-destructor-name
7177 FOR_OFFSETOF is set if we're being called in that context. That sorta
7178 limits what of the above we'll actually accept, but nevermind.
7179 TOKEN_TYPE is the "." or "->" token, which will already have been
7180 removed from the stream. */
7183 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7184 enum cpp_ttype token_type,
7185 cp_expr postfix_expression,
7186 bool for_offsetof, cp_id_kind *idk,
7187 location_t location)
7191 bool pseudo_destructor_p;
7192 tree scope = NULL_TREE;
7193 location_t start_loc = postfix_expression.get_start ();
7195 /* If this is a `->' operator, dereference the pointer. */
7196 if (token_type == CPP_DEREF)
7197 postfix_expression = build_x_arrow (location, postfix_expression,
7198 tf_warning_or_error);
7199 /* Check to see whether or not the expression is type-dependent. */
7200 dependent_p = type_dependent_expression_p (postfix_expression);
7201 /* The identifier following the `->' or `.' is not qualified. */
7202 parser->scope = NULL_TREE;
7203 parser->qualifying_scope = NULL_TREE;
7204 parser->object_scope = NULL_TREE;
7205 *idk = CP_ID_KIND_NONE;
7207 /* Enter the scope corresponding to the type of the object
7208 given by the POSTFIX_EXPRESSION. */
7209 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7211 scope = TREE_TYPE (postfix_expression);
7212 /* According to the standard, no expression should ever have
7213 reference type. Unfortunately, we do not currently match
7214 the standard in this respect in that our internal representation
7215 of an expression may have reference type even when the standard
7216 says it does not. Therefore, we have to manually obtain the
7217 underlying type here. */
7218 scope = non_reference (scope);
7219 /* The type of the POSTFIX_EXPRESSION must be complete. */
7220 if (scope == unknown_type_node)
7222 error_at (location, "%qE does not have class type",
7223 postfix_expression.get_value ());
7226 /* Unlike the object expression in other contexts, *this is not
7227 required to be of complete type for purposes of class member
7228 access (5.2.5) outside the member function body. */
7229 else if (postfix_expression != current_class_ref
7230 && !(processing_template_decl && scope == current_class_type))
7231 scope = complete_type_or_else (scope, NULL_TREE);
7232 /* Let the name lookup machinery know that we are processing a
7233 class member access expression. */
7234 parser->context->object_type = scope;
7235 /* If something went wrong, we want to be able to discern that case,
7236 as opposed to the case where there was no SCOPE due to the type
7237 of expression being dependent. */
7239 scope = error_mark_node;
7240 /* If the SCOPE was erroneous, make the various semantic analysis
7241 functions exit quickly -- and without issuing additional error
7243 if (scope == error_mark_node)
7244 postfix_expression = error_mark_node;
7247 /* Tell cp_parser_lookup_name that there was an object, even though it's
7249 parser->context->object_type = unknown_type_node;
7251 /* Assume this expression is not a pseudo-destructor access. */
7252 pseudo_destructor_p = false;
7254 /* If the SCOPE is a scalar type, then, if this is a valid program,
7255 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7256 is type dependent, it can be pseudo-destructor-name or something else.
7257 Try to parse it as pseudo-destructor-name first. */
7258 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7263 cp_parser_parse_tentatively (parser);
7264 /* Parse the pseudo-destructor-name. */
7266 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7269 && (cp_parser_error_occurred (parser)
7270 || !SCALAR_TYPE_P (type)))
7271 cp_parser_abort_tentative_parse (parser);
7272 else if (cp_parser_parse_definitely (parser))
7274 pseudo_destructor_p = true;
7276 = finish_pseudo_destructor_expr (postfix_expression,
7281 if (!pseudo_destructor_p)
7283 /* If the SCOPE is not a scalar type, we are looking at an
7284 ordinary class member access expression, rather than a
7285 pseudo-destructor-name. */
7287 cp_token *token = cp_lexer_peek_token (parser->lexer);
7288 /* Parse the id-expression. */
7289 name = (cp_parser_id_expression
7291 cp_parser_optional_template_keyword (parser),
7292 /*check_dependency_p=*/true,
7294 /*declarator_p=*/false,
7295 /*optional_p=*/false));
7296 /* In general, build a SCOPE_REF if the member name is qualified.
7297 However, if the name was not dependent and has already been
7298 resolved; there is no need to build the SCOPE_REF. For example;
7300 struct X { void f(); };
7301 template <typename T> void f(T* t) { t->X::f(); }
7303 Even though "t" is dependent, "X::f" is not and has been resolved
7304 to a BASELINK; there is no need to include scope information. */
7306 /* But we do need to remember that there was an explicit scope for
7307 virtual function calls. */
7309 *idk = CP_ID_KIND_QUALIFIED;
7311 /* If the name is a template-id that names a type, we will get a
7312 TYPE_DECL here. That is invalid code. */
7313 if (TREE_CODE (name) == TYPE_DECL)
7315 error_at (token->location, "invalid use of %qD", name);
7316 postfix_expression = error_mark_node;
7320 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7322 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7324 error_at (token->location, "%<%D::%D%> is not a class member",
7325 parser->scope, name);
7326 postfix_expression = error_mark_node;
7329 name = build_qualified_name (/*type=*/NULL_TREE,
7333 parser->scope = NULL_TREE;
7334 parser->qualifying_scope = NULL_TREE;
7335 parser->object_scope = NULL_TREE;
7337 if (parser->scope && name && BASELINK_P (name))
7338 adjust_result_of_qualified_name_lookup
7339 (name, parser->scope, scope);
7341 = finish_class_member_access_expr (postfix_expression, name,
7343 tf_warning_or_error);
7344 /* Build a location e.g.:
7347 where the caret is at the deref token, ranging from
7348 the start of postfix_expression to the end of the access expr. */
7350 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7351 location_t combined_loc
7352 = make_location (input_location, start_loc, end_loc);
7353 protected_set_expr_location (postfix_expression, combined_loc);
7357 /* We no longer need to look up names in the scope of the object on
7358 the left-hand side of the `.' or `->' operator. */
7359 parser->context->object_type = NULL_TREE;
7361 /* Outside of offsetof, these operators may not appear in
7362 constant-expressions. */
7364 && (cp_parser_non_integral_constant_expression
7365 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7366 postfix_expression = error_mark_node;
7368 return postfix_expression;
7371 /* Parse a parenthesized expression-list.
7374 assignment-expression
7375 expression-list, assignment-expression
7380 identifier, expression-list
7382 CAST_P is true if this expression is the target of a cast.
7384 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7387 Returns a vector of trees. Each element is a representation of an
7388 assignment-expression. NULL is returned if the ( and or ) are
7389 missing. An empty, but allocated, vector is returned on no
7390 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7391 if we are parsing an attribute list for an attribute that wants a
7392 plain identifier argument, normal_attr for an attribute that wants
7393 an expression, or non_attr if we aren't parsing an attribute list. If
7394 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7395 not all of the expressions in the list were constant.
7396 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7397 will be written to with the location of the closing parenthesis. If
7398 an error occurs, it may or may not be written to. */
7400 static vec<tree, va_gc> *
7401 cp_parser_parenthesized_expression_list (cp_parser* parser,
7402 int is_attribute_list,
7404 bool allow_expansion_p,
7405 bool *non_constant_p,
7406 location_t *close_paren_loc)
7408 vec<tree, va_gc> *expression_list;
7409 bool fold_expr_p = is_attribute_list != non_attr;
7410 tree identifier = NULL_TREE;
7411 bool saved_greater_than_is_operator_p;
7413 /* Assume all the expressions will be constant. */
7415 *non_constant_p = false;
7417 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7420 expression_list = make_tree_vector ();
7422 /* Within a parenthesized expression, a `>' token is always
7423 the greater-than operator. */
7424 saved_greater_than_is_operator_p
7425 = parser->greater_than_is_operator_p;
7426 parser->greater_than_is_operator_p = true;
7428 /* Consume expressions until there are no more. */
7429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7434 /* At the beginning of attribute lists, check to see if the
7435 next token is an identifier. */
7436 if (is_attribute_list == id_attr
7437 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7441 /* Consume the identifier. */
7442 token = cp_lexer_consume_token (parser->lexer);
7443 /* Save the identifier. */
7444 identifier = token->u.value;
7448 bool expr_non_constant_p;
7450 /* Parse the next assignment-expression. */
7451 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7453 /* A braced-init-list. */
7454 cp_lexer_set_source_position (parser->lexer);
7455 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7456 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7457 if (non_constant_p && expr_non_constant_p)
7458 *non_constant_p = true;
7460 else if (non_constant_p)
7462 expr = (cp_parser_constant_expression
7463 (parser, /*allow_non_constant_p=*/true,
7464 &expr_non_constant_p));
7465 if (expr_non_constant_p)
7466 *non_constant_p = true;
7469 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7473 expr = instantiate_non_dependent_expr (expr);
7475 /* If we have an ellipsis, then this is an expression
7477 if (allow_expansion_p
7478 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7480 /* Consume the `...'. */
7481 cp_lexer_consume_token (parser->lexer);
7483 /* Build the argument pack. */
7484 expr = make_pack_expansion (expr);
7487 /* Add it to the list. We add error_mark_node
7488 expressions to the list, so that we can still tell if
7489 the correct form for a parenthesized expression-list
7490 is found. That gives better errors. */
7491 vec_safe_push (expression_list, expr);
7493 if (expr == error_mark_node)
7497 /* After the first item, attribute lists look the same as
7498 expression lists. */
7499 is_attribute_list = non_attr;
7502 /* If the next token isn't a `,', then we are done. */
7503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7506 /* Otherwise, consume the `,' and keep going. */
7507 cp_lexer_consume_token (parser->lexer);
7510 if (close_paren_loc)
7511 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7513 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7518 /* We try and resync to an unnested comma, as that will give the
7519 user better diagnostics. */
7520 ending = cp_parser_skip_to_closing_parenthesis (parser,
7521 /*recovering=*/true,
7523 /*consume_paren=*/true);
7528 parser->greater_than_is_operator_p
7529 = saved_greater_than_is_operator_p;
7534 parser->greater_than_is_operator_p
7535 = saved_greater_than_is_operator_p;
7538 vec_safe_insert (expression_list, 0, identifier);
7540 return expression_list;
7543 /* Parse a pseudo-destructor-name.
7545 pseudo-destructor-name:
7546 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7547 :: [opt] nested-name-specifier template template-id :: ~ type-name
7548 :: [opt] nested-name-specifier [opt] ~ type-name
7550 If either of the first two productions is used, sets *SCOPE to the
7551 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7552 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7553 or ERROR_MARK_NODE if the parse fails. */
7556 cp_parser_pseudo_destructor_name (cp_parser* parser,
7561 bool nested_name_specifier_p;
7564 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7565 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7566 && !type_dependent_expression_p (object))
7568 if (cxx_dialect < cxx14)
7569 pedwarn (input_location, 0,
7570 "%<~auto%> only available with "
7571 "-std=c++14 or -std=gnu++14");
7572 cp_lexer_consume_token (parser->lexer);
7573 cp_lexer_consume_token (parser->lexer);
7575 *type = TREE_TYPE (object);
7579 /* Assume that things will not work out. */
7580 *type = error_mark_node;
7582 /* Look for the optional `::' operator. */
7583 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7584 /* Look for the optional nested-name-specifier. */
7585 nested_name_specifier_p
7586 = (cp_parser_nested_name_specifier_opt (parser,
7587 /*typename_keyword_p=*/false,
7588 /*check_dependency_p=*/true,
7590 /*is_declaration=*/false)
7592 /* Now, if we saw a nested-name-specifier, we might be doing the
7593 second production. */
7594 if (nested_name_specifier_p
7595 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7597 /* Consume the `template' keyword. */
7598 cp_lexer_consume_token (parser->lexer);
7599 /* Parse the template-id. */
7600 cp_parser_template_id (parser,
7601 /*template_keyword_p=*/true,
7602 /*check_dependency_p=*/false,
7604 /*is_declaration=*/true);
7605 /* Look for the `::' token. */
7606 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7608 /* If the next token is not a `~', then there might be some
7609 additional qualification. */
7610 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7612 /* At this point, we're looking for "type-name :: ~". The type-name
7613 must not be a class-name, since this is a pseudo-destructor. So,
7614 it must be either an enum-name, or a typedef-name -- both of which
7615 are just identifiers. So, we peek ahead to check that the "::"
7616 and "~" tokens are present; if they are not, then we can avoid
7617 calling type_name. */
7618 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7619 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7620 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7622 cp_parser_error (parser, "non-scalar type");
7626 /* Look for the type-name. */
7627 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7628 if (*scope == error_mark_node)
7631 /* Look for the `::' token. */
7632 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7637 /* Look for the `~'. */
7638 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7640 /* Once we see the ~, this has to be a pseudo-destructor. */
7641 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7642 cp_parser_commit_to_topmost_tentative_parse (parser);
7644 /* Look for the type-name again. We are not responsible for
7645 checking that it matches the first type-name. */
7646 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7649 /* Parse a unary-expression.
7655 unary-operator cast-expression
7656 sizeof unary-expression
7658 alignof ( type-id ) [C++0x]
7665 __extension__ cast-expression
7666 __alignof__ unary-expression
7667 __alignof__ ( type-id )
7668 alignof unary-expression [C++0x]
7669 __real__ cast-expression
7670 __imag__ cast-expression
7672 sizeof ( type-id ) { initializer-list , [opt] }
7673 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7674 __alignof__ ( type-id ) { initializer-list , [opt] }
7676 ADDRESS_P is true iff the unary-expression is appearing as the
7677 operand of the `&' operator. CAST_P is true if this expression is
7678 the target of a cast.
7680 Returns a representation of the expression. */
7683 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7684 bool address_p, bool cast_p, bool decltype_p)
7687 enum tree_code unary_operator;
7689 /* Peek at the next token. */
7690 token = cp_lexer_peek_token (parser->lexer);
7691 /* Some keywords give away the kind of expression. */
7692 if (token->type == CPP_KEYWORD)
7694 enum rid keyword = token->keyword;
7703 location_t first_loc;
7705 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7706 /* Consume the token. */
7707 cp_lexer_consume_token (parser->lexer);
7708 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7709 /* Parse the operand. */
7710 operand = cp_parser_sizeof_operand (parser, keyword);
7712 if (TYPE_P (operand))
7713 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7716 /* ISO C++ defines alignof only with types, not with
7717 expressions. So pedwarn if alignof is used with a non-
7718 type expression. However, __alignof__ is ok. */
7719 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7720 pedwarn (token->location, OPT_Wpedantic,
7721 "ISO C++ does not allow %<alignof%> "
7724 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7726 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7727 SIZEOF_EXPR with the original operand. */
7728 if (op == SIZEOF_EXPR && ret != error_mark_node)
7730 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7732 if (!processing_template_decl && TYPE_P (operand))
7734 ret = build_min (SIZEOF_EXPR, size_type_node,
7735 build1 (NOP_EXPR, operand,
7737 SIZEOF_EXPR_TYPE_P (ret) = 1;
7740 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7741 TREE_SIDE_EFFECTS (ret) = 0;
7742 TREE_READONLY (ret) = 1;
7744 SET_EXPR_LOCATION (ret, first_loc);
7750 return cp_parser_new_expression (parser);
7753 return cp_parser_delete_expression (parser);
7757 /* The saved value of the PEDANTIC flag. */
7761 /* Save away the PEDANTIC flag. */
7762 cp_parser_extension_opt (parser, &saved_pedantic);
7763 /* Parse the cast-expression. */
7764 expr = cp_parser_simple_cast_expression (parser);
7765 /* Restore the PEDANTIC flag. */
7766 pedantic = saved_pedantic;
7776 /* Consume the `__real__' or `__imag__' token. */
7777 cp_lexer_consume_token (parser->lexer);
7778 /* Parse the cast-expression. */
7779 expression = cp_parser_simple_cast_expression (parser);
7780 /* Create the complete representation. */
7781 return build_x_unary_op (token->location,
7782 (keyword == RID_REALPART
7783 ? REALPART_EXPR : IMAGPART_EXPR),
7785 tf_warning_or_error);
7789 case RID_TRANSACTION_ATOMIC:
7790 case RID_TRANSACTION_RELAXED:
7791 return cp_parser_transaction_expression (parser, keyword);
7796 const char *saved_message;
7797 bool saved_integral_constant_expression_p;
7798 bool saved_non_integral_constant_expression_p;
7799 bool saved_greater_than_is_operator_p;
7801 cp_lexer_consume_token (parser->lexer);
7802 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7804 saved_message = parser->type_definition_forbidden_message;
7805 parser->type_definition_forbidden_message
7806 = G_("types may not be defined in %<noexcept%> expressions");
7808 saved_integral_constant_expression_p
7809 = parser->integral_constant_expression_p;
7810 saved_non_integral_constant_expression_p
7811 = parser->non_integral_constant_expression_p;
7812 parser->integral_constant_expression_p = false;
7814 saved_greater_than_is_operator_p
7815 = parser->greater_than_is_operator_p;
7816 parser->greater_than_is_operator_p = true;
7818 ++cp_unevaluated_operand;
7819 ++c_inhibit_evaluation_warnings;
7820 ++cp_noexcept_operand;
7821 expr = cp_parser_expression (parser);
7822 --cp_noexcept_operand;
7823 --c_inhibit_evaluation_warnings;
7824 --cp_unevaluated_operand;
7826 parser->greater_than_is_operator_p
7827 = saved_greater_than_is_operator_p;
7829 parser->integral_constant_expression_p
7830 = saved_integral_constant_expression_p;
7831 parser->non_integral_constant_expression_p
7832 = saved_non_integral_constant_expression_p;
7834 parser->type_definition_forbidden_message = saved_message;
7836 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7837 return finish_noexcept_expr (expr, tf_warning_or_error);
7845 /* Look for the `:: new' and `:: delete', which also signal the
7846 beginning of a new-expression, or delete-expression,
7847 respectively. If the next token is `::', then it might be one of
7849 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7853 /* See if the token after the `::' is one of the keywords in
7854 which we're interested. */
7855 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7856 /* If it's `new', we have a new-expression. */
7857 if (keyword == RID_NEW)
7858 return cp_parser_new_expression (parser);
7859 /* Similarly, for `delete'. */
7860 else if (keyword == RID_DELETE)
7861 return cp_parser_delete_expression (parser);
7864 /* Look for a unary operator. */
7865 unary_operator = cp_parser_unary_operator (token);
7866 /* The `++' and `--' operators can be handled similarly, even though
7867 they are not technically unary-operators in the grammar. */
7868 if (unary_operator == ERROR_MARK)
7870 if (token->type == CPP_PLUS_PLUS)
7871 unary_operator = PREINCREMENT_EXPR;
7872 else if (token->type == CPP_MINUS_MINUS)
7873 unary_operator = PREDECREMENT_EXPR;
7874 /* Handle the GNU address-of-label extension. */
7875 else if (cp_parser_allow_gnu_extensions_p (parser)
7876 && token->type == CPP_AND_AND)
7880 location_t start_loc = token->location;
7882 /* Consume the '&&' token. */
7883 cp_lexer_consume_token (parser->lexer);
7884 /* Look for the identifier. */
7885 location_t finish_loc
7886 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7887 identifier = cp_parser_identifier (parser);
7888 /* Construct a location of the form:
7891 with caret==start at the "&&", finish at the end of the label. */
7892 location_t combined_loc
7893 = make_location (start_loc, start_loc, finish_loc);
7894 /* Create an expression representing the address. */
7895 expression = finish_label_address_expr (identifier, combined_loc);
7896 if (cp_parser_non_integral_constant_expression (parser,
7898 expression = error_mark_node;
7902 if (unary_operator != ERROR_MARK)
7904 cp_expr cast_expression;
7905 cp_expr expression = error_mark_node;
7906 non_integral_constant non_constant_p = NIC_NONE;
7907 location_t loc = token->location;
7908 tsubst_flags_t complain = complain_flags (decltype_p);
7910 /* Consume the operator token. */
7911 token = cp_lexer_consume_token (parser->lexer);
7912 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7914 /* Parse the cast-expression. */
7916 = cp_parser_cast_expression (parser,
7917 unary_operator == ADDR_EXPR,
7923 OP_TOKEN CAST_EXPRESSION
7924 ^~~~~~~~~~~~~~~~~~~~~~~~~
7925 with start==caret at the operator token, and
7926 extending to the end of the cast_expression. */
7927 loc = make_location (loc, loc, cast_expression.get_finish ());
7929 /* Now, build an appropriate representation. */
7930 switch (unary_operator)
7933 non_constant_p = NIC_STAR;
7934 expression = build_x_indirect_ref (loc, cast_expression,
7937 /* TODO: build_x_indirect_ref does not always honor the
7938 location, so ensure it is set. */
7939 expression.set_location (loc);
7943 non_constant_p = NIC_ADDR;
7946 expression = build_x_unary_op (loc, unary_operator,
7949 /* TODO: build_x_unary_op does not always honor the location,
7950 so ensure it is set. */
7951 expression.set_location (loc);
7954 case PREINCREMENT_EXPR:
7955 case PREDECREMENT_EXPR:
7956 non_constant_p = unary_operator == PREINCREMENT_EXPR
7957 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7960 /* Immediately fold negation of a constant, unless the constant is 0
7961 (since -0 == 0) or it would overflow. */
7962 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7963 && CONSTANT_CLASS_P (cast_expression)
7964 && !integer_zerop (cast_expression)
7965 && !TREE_OVERFLOW (cast_expression))
7967 tree folded = fold_build1 (unary_operator,
7968 TREE_TYPE (cast_expression),
7970 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7972 expression = cp_expr (folded, loc);
7977 case UNARY_PLUS_EXPR:
7978 case TRUTH_NOT_EXPR:
7979 expression = finish_unary_op_expr (loc, unary_operator,
7980 cast_expression, complain);
7987 if (non_constant_p != NIC_NONE
7988 && cp_parser_non_integral_constant_expression (parser,
7990 expression = error_mark_node;
7995 return cp_parser_postfix_expression (parser, address_p, cast_p,
7996 /*member_access_only_p=*/false,
8001 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8002 unary-operator, the corresponding tree code is returned. */
8004 static enum tree_code
8005 cp_parser_unary_operator (cp_token* token)
8007 switch (token->type)
8010 return INDIRECT_REF;
8016 return UNARY_PLUS_EXPR;
8022 return TRUTH_NOT_EXPR;
8025 return BIT_NOT_EXPR;
8032 /* Parse a new-expression.
8035 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8036 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8038 Returns a representation of the expression. */
8041 cp_parser_new_expression (cp_parser* parser)
8043 bool global_scope_p;
8044 vec<tree, va_gc> *placement;
8046 vec<tree, va_gc> *initializer;
8047 tree nelts = NULL_TREE;
8050 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8052 /* Look for the optional `::' operator. */
8054 = (cp_parser_global_scope_opt (parser,
8055 /*current_scope_valid_p=*/false)
8057 /* Look for the `new' operator. */
8058 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8059 /* There's no easy way to tell a new-placement from the
8060 `( type-id )' construct. */
8061 cp_parser_parse_tentatively (parser);
8062 /* Look for a new-placement. */
8063 placement = cp_parser_new_placement (parser);
8064 /* If that didn't work out, there's no new-placement. */
8065 if (!cp_parser_parse_definitely (parser))
8067 if (placement != NULL)
8068 release_tree_vector (placement);
8072 /* If the next token is a `(', then we have a parenthesized
8074 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8077 const char *saved_message = parser->type_definition_forbidden_message;
8079 /* Consume the `('. */
8080 cp_lexer_consume_token (parser->lexer);
8082 /* Parse the type-id. */
8083 parser->type_definition_forbidden_message
8084 = G_("types may not be defined in a new-expression");
8086 type_id_in_expr_sentinel s (parser);
8087 type = cp_parser_type_id (parser);
8089 parser->type_definition_forbidden_message = saved_message;
8091 /* Look for the closing `)'. */
8092 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8093 token = cp_lexer_peek_token (parser->lexer);
8094 /* There should not be a direct-new-declarator in this production,
8095 but GCC used to allowed this, so we check and emit a sensible error
8096 message for this case. */
8097 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8099 error_at (token->location,
8100 "array bound forbidden after parenthesized type-id");
8101 inform (token->location,
8102 "try removing the parentheses around the type-id");
8103 cp_parser_direct_new_declarator (parser);
8106 /* Otherwise, there must be a new-type-id. */
8108 type = cp_parser_new_type_id (parser, &nelts);
8110 /* If the next token is a `(' or '{', then we have a new-initializer. */
8111 cp_token *token = cp_lexer_peek_token (parser->lexer);
8112 if (token->type == CPP_OPEN_PAREN
8113 || token->type == CPP_OPEN_BRACE)
8114 initializer = cp_parser_new_initializer (parser);
8118 /* A new-expression may not appear in an integral constant
8120 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8121 ret = error_mark_node;
8122 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8123 of a new-type-id or type-id of a new-expression, the new-expression shall
8124 contain a new-initializer of the form ( assignment-expression )".
8125 Additionally, consistently with the spirit of DR 1467, we want to accept
8126 'new auto { 2 }' too. */
8127 else if (type_uses_auto (type)
8128 && (vec_safe_length (initializer) != 1
8129 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8130 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8132 error_at (token->location,
8133 "initialization of new-expression for type %<auto%> "
8134 "requires exactly one element");
8135 ret = error_mark_node;
8139 /* Construct a location e.g.:
8142 with caret == start at the start of the "new" token, and the end
8143 at the end of the final token we consumed. */
8144 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8145 location_t end_loc = get_finish (end_tok->location);
8146 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8148 /* Create a representation of the new-expression. */
8149 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8150 tf_warning_or_error);
8151 protected_set_expr_location (ret, combined_loc);
8154 if (placement != NULL)
8155 release_tree_vector (placement);
8156 if (initializer != NULL)
8157 release_tree_vector (initializer);
8162 /* Parse a new-placement.
8167 Returns the same representation as for an expression-list. */
8169 static vec<tree, va_gc> *
8170 cp_parser_new_placement (cp_parser* parser)
8172 vec<tree, va_gc> *expression_list;
8174 /* Parse the expression-list. */
8175 expression_list = (cp_parser_parenthesized_expression_list
8176 (parser, non_attr, /*cast_p=*/false,
8177 /*allow_expansion_p=*/true,
8178 /*non_constant_p=*/NULL));
8180 if (expression_list && expression_list->is_empty ())
8181 error ("expected expression-list or type-id");
8183 return expression_list;
8186 /* Parse a new-type-id.
8189 type-specifier-seq new-declarator [opt]
8191 Returns the TYPE allocated. If the new-type-id indicates an array
8192 type, *NELTS is set to the number of elements in the last array
8193 bound; the TYPE will not include the last array bound. */
8196 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8198 cp_decl_specifier_seq type_specifier_seq;
8199 cp_declarator *new_declarator;
8200 cp_declarator *declarator;
8201 cp_declarator *outer_declarator;
8202 const char *saved_message;
8204 /* The type-specifier sequence must not contain type definitions.
8205 (It cannot contain declarations of new types either, but if they
8206 are not definitions we will catch that because they are not
8208 saved_message = parser->type_definition_forbidden_message;
8209 parser->type_definition_forbidden_message
8210 = G_("types may not be defined in a new-type-id");
8211 /* Parse the type-specifier-seq. */
8212 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8213 /*is_trailing_return=*/false,
8214 &type_specifier_seq);
8215 /* Restore the old message. */
8216 parser->type_definition_forbidden_message = saved_message;
8218 if (type_specifier_seq.type == error_mark_node)
8219 return error_mark_node;
8221 /* Parse the new-declarator. */
8222 new_declarator = cp_parser_new_declarator_opt (parser);
8224 /* Determine the number of elements in the last array dimension, if
8227 /* Skip down to the last array dimension. */
8228 declarator = new_declarator;
8229 outer_declarator = NULL;
8230 while (declarator && (declarator->kind == cdk_pointer
8231 || declarator->kind == cdk_ptrmem))
8233 outer_declarator = declarator;
8234 declarator = declarator->declarator;
8237 && declarator->kind == cdk_array
8238 && declarator->declarator
8239 && declarator->declarator->kind == cdk_array)
8241 outer_declarator = declarator;
8242 declarator = declarator->declarator;
8245 if (declarator && declarator->kind == cdk_array)
8247 *nelts = declarator->u.array.bounds;
8248 if (*nelts == error_mark_node)
8249 *nelts = integer_one_node;
8251 if (outer_declarator)
8252 outer_declarator->declarator = declarator->declarator;
8254 new_declarator = NULL;
8257 return groktypename (&type_specifier_seq, new_declarator, false);
8260 /* Parse an (optional) new-declarator.
8263 ptr-operator new-declarator [opt]
8264 direct-new-declarator
8266 Returns the declarator. */
8268 static cp_declarator *
8269 cp_parser_new_declarator_opt (cp_parser* parser)
8271 enum tree_code code;
8272 tree type, std_attributes = NULL_TREE;
8273 cp_cv_quals cv_quals;
8275 /* We don't know if there's a ptr-operator next, or not. */
8276 cp_parser_parse_tentatively (parser);
8277 /* Look for a ptr-operator. */
8278 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8279 /* If that worked, look for more new-declarators. */
8280 if (cp_parser_parse_definitely (parser))
8282 cp_declarator *declarator;
8284 /* Parse another optional declarator. */
8285 declarator = cp_parser_new_declarator_opt (parser);
8287 declarator = cp_parser_make_indirect_declarator
8288 (code, type, cv_quals, declarator, std_attributes);
8293 /* If the next token is a `[', there is a direct-new-declarator. */
8294 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8295 return cp_parser_direct_new_declarator (parser);
8300 /* Parse a direct-new-declarator.
8302 direct-new-declarator:
8304 direct-new-declarator [constant-expression]
8308 static cp_declarator *
8309 cp_parser_direct_new_declarator (cp_parser* parser)
8311 cp_declarator *declarator = NULL;
8318 /* Look for the opening `['. */
8319 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8321 token = cp_lexer_peek_token (parser->lexer);
8322 expression = cp_parser_expression (parser);
8323 /* The standard requires that the expression have integral
8324 type. DR 74 adds enumeration types. We believe that the
8325 real intent is that these expressions be handled like the
8326 expression in a `switch' condition, which also allows
8327 classes with a single conversion to integral or
8328 enumeration type. */
8329 if (!processing_template_decl)
8332 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8337 error_at (token->location,
8338 "expression in new-declarator must have integral "
8339 "or enumeration type");
8340 expression = error_mark_node;
8344 /* Look for the closing `]'. */
8345 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8347 /* Add this bound to the declarator. */
8348 declarator = make_array_declarator (declarator, expression);
8350 /* If the next token is not a `[', then there are no more
8352 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8359 /* Parse a new-initializer.
8362 ( expression-list [opt] )
8365 Returns a representation of the expression-list. */
8367 static vec<tree, va_gc> *
8368 cp_parser_new_initializer (cp_parser* parser)
8370 vec<tree, va_gc> *expression_list;
8372 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8375 bool expr_non_constant_p;
8376 cp_lexer_set_source_position (parser->lexer);
8377 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8378 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8379 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8380 expression_list = make_tree_vector_single (t);
8383 expression_list = (cp_parser_parenthesized_expression_list
8384 (parser, non_attr, /*cast_p=*/false,
8385 /*allow_expansion_p=*/true,
8386 /*non_constant_p=*/NULL));
8388 return expression_list;
8391 /* Parse a delete-expression.
8394 :: [opt] delete cast-expression
8395 :: [opt] delete [ ] cast-expression
8397 Returns a representation of the expression. */
8400 cp_parser_delete_expression (cp_parser* parser)
8402 bool global_scope_p;
8406 /* Look for the optional `::' operator. */
8408 = (cp_parser_global_scope_opt (parser,
8409 /*current_scope_valid_p=*/false)
8411 /* Look for the `delete' keyword. */
8412 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8413 /* See if the array syntax is in use. */
8414 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8416 /* Consume the `[' token. */
8417 cp_lexer_consume_token (parser->lexer);
8418 /* Look for the `]' token. */
8419 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8420 /* Remember that this is the `[]' construct. */
8426 /* Parse the cast-expression. */
8427 expression = cp_parser_simple_cast_expression (parser);
8429 /* A delete-expression may not appear in an integral constant
8431 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8432 return error_mark_node;
8434 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8435 tf_warning_or_error);
8438 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8439 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8443 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8445 cp_token *token = cp_lexer_peek_token (parser->lexer);
8446 switch (token->type)
8452 case CPP_CLOSE_SQUARE:
8453 case CPP_CLOSE_PAREN:
8454 case CPP_CLOSE_BRACE:
8455 case CPP_OPEN_BRACE:
8459 case CPP_DEREF_STAR:
8467 case CPP_GREATER_EQ:
8488 case CPP_OPEN_PAREN:
8489 /* In ((type ()) () the last () isn't a valid cast-expression,
8490 so the whole must be parsed as postfix-expression. */
8491 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8494 case CPP_OPEN_SQUARE:
8495 /* '[' may start a primary-expression in obj-c++ and in C++11,
8496 as a lambda-expression, eg, '(void)[]{}'. */
8497 if (cxx_dialect >= cxx11)
8499 return c_dialect_objc ();
8502 case CPP_MINUS_MINUS:
8503 /* '++' and '--' may or may not start a cast-expression:
8505 struct T { void operator++(int); };
8506 void f() { (T())++; }
8519 /* Parse a cast-expression.
8523 ( type-id ) cast-expression
8525 ADDRESS_P is true iff the unary-expression is appearing as the
8526 operand of the `&' operator. CAST_P is true if this expression is
8527 the target of a cast.
8529 Returns a representation of the expression. */
8532 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8533 bool decltype_p, cp_id_kind * pidk)
8535 /* If it's a `(', then we might be looking at a cast. */
8536 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8538 tree type = NULL_TREE;
8539 cp_expr expr (NULL_TREE);
8540 int cast_expression = 0;
8541 const char *saved_message;
8543 /* There's no way to know yet whether or not this is a cast.
8544 For example, `(int (3))' is a unary-expression, while `(int)
8545 3' is a cast. So, we resort to parsing tentatively. */
8546 cp_parser_parse_tentatively (parser);
8547 /* Types may not be defined in a cast. */
8548 saved_message = parser->type_definition_forbidden_message;
8549 parser->type_definition_forbidden_message
8550 = G_("types may not be defined in casts");
8551 /* Consume the `('. */
8552 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8553 location_t open_paren_loc = open_paren->location;
8555 /* A very tricky bit is that `(struct S) { 3 }' is a
8556 compound-literal (which we permit in C++ as an extension).
8557 But, that construct is not a cast-expression -- it is a
8558 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8559 is legal; if the compound-literal were a cast-expression,
8560 you'd need an extra set of parentheses.) But, if we parse
8561 the type-id, and it happens to be a class-specifier, then we
8562 will commit to the parse at that point, because we cannot
8563 undo the action that is done when creating a new class. So,
8564 then we cannot back up and do a postfix-expression.
8566 Another tricky case is the following (c++/29234):
8568 struct S { void operator () (); };
8575 As a type-id we parse the parenthesized S()() as a function
8576 returning a function, groktypename complains and we cannot
8577 back up in this case either.
8579 Therefore, we scan ahead to the closing `)', and check to see
8580 if the tokens after the `)' can start a cast-expression. Otherwise
8581 we are dealing with an unary-expression, a postfix-expression
8584 Yet another tricky case, in C++11, is the following (c++/54891):
8588 The issue is that usually, besides the case of lambda-expressions,
8589 the parenthesized type-id cannot be followed by '[', and, eg, we
8590 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8591 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8592 we don't commit, we try a cast-expression, then an unary-expression.
8594 Save tokens so that we can put them back. */
8595 cp_lexer_save_tokens (parser->lexer);
8597 /* We may be looking at a cast-expression. */
8598 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8599 /*consume_paren=*/true))
8601 = cp_parser_tokens_start_cast_expression (parser);
8603 /* Roll back the tokens we skipped. */
8604 cp_lexer_rollback_tokens (parser->lexer);
8605 /* If we aren't looking at a cast-expression, simulate an error so
8606 that the call to cp_parser_error_occurred below returns true. */
8607 if (!cast_expression)
8608 cp_parser_simulate_error (parser);
8611 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8612 parser->in_type_id_in_expr_p = true;
8613 /* Look for the type-id. */
8614 type = cp_parser_type_id (parser);
8615 /* Look for the closing `)'. */
8616 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8617 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8620 /* Restore the saved message. */
8621 parser->type_definition_forbidden_message = saved_message;
8623 /* At this point this can only be either a cast or a
8624 parenthesized ctor such as `(T ())' that looks like a cast to
8625 function returning T. */
8626 if (!cp_parser_error_occurred (parser))
8628 /* Only commit if the cast-expression doesn't start with
8629 '++', '--', or '[' in C++11. */
8630 if (cast_expression > 0)
8631 cp_parser_commit_to_topmost_tentative_parse (parser);
8633 expr = cp_parser_cast_expression (parser,
8634 /*address_p=*/false,
8636 /*decltype_p=*/false,
8639 if (cp_parser_parse_definitely (parser))
8641 /* Warn about old-style casts, if so requested. */
8642 if (warn_old_style_cast
8643 && !in_system_header_at (input_location)
8644 && !VOID_TYPE_P (type)
8645 && current_lang_name != lang_name_c)
8646 warning (OPT_Wold_style_cast, "use of old-style cast");
8648 /* Only type conversions to integral or enumeration types
8649 can be used in constant-expressions. */
8650 if (!cast_valid_in_integral_constant_expression_p (type)
8651 && cp_parser_non_integral_constant_expression (parser,
8653 return error_mark_node;
8655 /* Perform the cast. */
8659 with start==caret at the open paren, extending to the
8661 location_t cast_loc = make_location (open_paren_loc,
8663 expr.get_finish ());
8664 expr = build_c_cast (cast_loc, type, expr);
8669 cp_parser_abort_tentative_parse (parser);
8672 /* If we get here, then it's not a cast, so it must be a
8673 unary-expression. */
8674 return cp_parser_unary_expression (parser, pidk, address_p,
8675 cast_p, decltype_p);
8678 /* Parse a binary expression of the general form:
8682 pm-expression .* cast-expression
8683 pm-expression ->* cast-expression
8685 multiplicative-expression:
8687 multiplicative-expression * pm-expression
8688 multiplicative-expression / pm-expression
8689 multiplicative-expression % pm-expression
8691 additive-expression:
8692 multiplicative-expression
8693 additive-expression + multiplicative-expression
8694 additive-expression - multiplicative-expression
8698 shift-expression << additive-expression
8699 shift-expression >> additive-expression
8701 relational-expression:
8703 relational-expression < shift-expression
8704 relational-expression > shift-expression
8705 relational-expression <= shift-expression
8706 relational-expression >= shift-expression
8710 relational-expression:
8711 relational-expression <? shift-expression
8712 relational-expression >? shift-expression
8714 equality-expression:
8715 relational-expression
8716 equality-expression == relational-expression
8717 equality-expression != relational-expression
8721 and-expression & equality-expression
8723 exclusive-or-expression:
8725 exclusive-or-expression ^ and-expression
8727 inclusive-or-expression:
8728 exclusive-or-expression
8729 inclusive-or-expression | exclusive-or-expression
8731 logical-and-expression:
8732 inclusive-or-expression
8733 logical-and-expression && inclusive-or-expression
8735 logical-or-expression:
8736 logical-and-expression
8737 logical-or-expression || logical-and-expression
8739 All these are implemented with a single function like:
8742 simple-cast-expression
8743 binary-expression <token> binary-expression
8745 CAST_P is true if this expression is the target of a cast.
8747 The binops_by_token map is used to get the tree codes for each <token> type.
8748 binary-expressions are associated according to a precedence table. */
8750 #define TOKEN_PRECEDENCE(token) \
8751 (((token->type == CPP_GREATER \
8752 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8753 && !parser->greater_than_is_operator_p) \
8754 ? PREC_NOT_OPERATOR \
8755 : binops_by_token[token->type].prec)
8758 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8759 bool no_toplevel_fold_p,
8761 enum cp_parser_prec prec,
8764 cp_parser_expression_stack stack;
8765 cp_parser_expression_stack_entry *sp = &stack[0];
8766 cp_parser_expression_stack_entry current;
8769 enum tree_code rhs_type;
8770 enum cp_parser_prec new_prec, lookahead_prec;
8773 /* Parse the first expression. */
8774 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8775 ? TRUTH_NOT_EXPR : ERROR_MARK);
8776 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8777 cast_p, decltype_p, pidk);
8778 current.prec = prec;
8780 if (cp_parser_error_occurred (parser))
8781 return error_mark_node;
8785 /* Get an operator token. */
8786 token = cp_lexer_peek_token (parser->lexer);
8788 if (warn_cxx11_compat
8789 && token->type == CPP_RSHIFT
8790 && !parser->greater_than_is_operator_p)
8792 if (warning_at (token->location, OPT_Wc__11_compat,
8793 "%<>>%> operator is treated"
8794 " as two right angle brackets in C++11"))
8795 inform (token->location,
8796 "suggest parentheses around %<>>%> expression");
8799 new_prec = TOKEN_PRECEDENCE (token);
8800 if (new_prec != PREC_NOT_OPERATOR
8801 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8802 /* This is a fold-expression; handle it later. */
8803 new_prec = PREC_NOT_OPERATOR;
8805 /* Popping an entry off the stack means we completed a subexpression:
8806 - either we found a token which is not an operator (`>' where it is not
8807 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8808 will happen repeatedly;
8809 - or, we found an operator which has lower priority. This is the case
8810 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8812 if (new_prec <= current.prec)
8821 current.tree_type = binops_by_token[token->type].tree_type;
8822 current.loc = token->location;
8824 /* We used the operator token. */
8825 cp_lexer_consume_token (parser->lexer);
8827 /* For "false && x" or "true || x", x will never be executed;
8828 disable warnings while evaluating it. */
8829 if (current.tree_type == TRUTH_ANDIF_EXPR)
8830 c_inhibit_evaluation_warnings +=
8831 cp_fully_fold (current.lhs) == truthvalue_false_node;
8832 else if (current.tree_type == TRUTH_ORIF_EXPR)
8833 c_inhibit_evaluation_warnings +=
8834 cp_fully_fold (current.lhs) == truthvalue_true_node;
8836 /* Extract another operand. It may be the RHS of this expression
8837 or the LHS of a new, higher priority expression. */
8838 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8839 ? TRUTH_NOT_EXPR : ERROR_MARK);
8840 rhs = cp_parser_simple_cast_expression (parser);
8842 /* Get another operator token. Look up its precedence to avoid
8843 building a useless (immediately popped) stack entry for common
8844 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8845 token = cp_lexer_peek_token (parser->lexer);
8846 lookahead_prec = TOKEN_PRECEDENCE (token);
8847 if (lookahead_prec != PREC_NOT_OPERATOR
8848 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8849 lookahead_prec = PREC_NOT_OPERATOR;
8850 if (lookahead_prec > new_prec)
8852 /* ... and prepare to parse the RHS of the new, higher priority
8853 expression. Since precedence levels on the stack are
8854 monotonically increasing, we do not have to care about
8859 current.lhs_type = rhs_type;
8860 current.prec = new_prec;
8861 new_prec = lookahead_prec;
8865 lookahead_prec = new_prec;
8866 /* If the stack is not empty, we have parsed into LHS the right side
8867 (`4' in the example above) of an expression we had suspended.
8868 We can use the information on the stack to recover the LHS (`3')
8869 from the stack together with the tree code (`MULT_EXPR'), and
8870 the precedence of the higher level subexpression
8871 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8872 which will be used to actually build the additive expression. */
8874 rhs_type = current.lhs_type;
8879 /* Undo the disabling of warnings done above. */
8880 if (current.tree_type == TRUTH_ANDIF_EXPR)
8881 c_inhibit_evaluation_warnings -=
8882 cp_fully_fold (current.lhs) == truthvalue_false_node;
8883 else if (current.tree_type == TRUTH_ORIF_EXPR)
8884 c_inhibit_evaluation_warnings -=
8885 cp_fully_fold (current.lhs) == truthvalue_true_node;
8887 if (warn_logical_not_paren
8888 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8889 && current.lhs_type == TRUTH_NOT_EXPR
8890 /* Avoid warning for !!x == y. */
8891 && (TREE_CODE (current.lhs) != NE_EXPR
8892 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8893 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8894 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8895 /* Avoid warning for !b == y where b is boolean. */
8896 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8897 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8899 /* Avoid warning for !!b == y where b is boolean. */
8900 && (!DECL_P (current.lhs)
8901 || TREE_TYPE (current.lhs) == NULL_TREE
8902 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8903 warn_logical_not_parentheses (current.loc, current.tree_type,
8904 maybe_constant_value (rhs));
8908 location_t combined_loc = make_location (current.loc,
8909 current.lhs.get_start (),
8912 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8913 ERROR_MARK for everything that is not a binary expression.
8914 This makes warn_about_parentheses miss some warnings that
8915 involve unary operators. For unary expressions we should
8916 pass the correct tree_code unless the unary expression was
8917 surrounded by parentheses.
8919 if (no_toplevel_fold_p
8920 && lookahead_prec <= current.prec
8922 current.lhs = build2_loc (combined_loc,
8924 TREE_CODE_CLASS (current.tree_type)
8926 ? boolean_type_node : TREE_TYPE (current.lhs),
8930 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8931 current.lhs, current.lhs_type,
8932 rhs, rhs_type, &overload,
8933 complain_flags (decltype_p));
8934 /* TODO: build_x_binary_op doesn't always honor the location. */
8935 current.lhs.set_location (combined_loc);
8937 current.lhs_type = current.tree_type;
8939 /* If the binary operator required the use of an overloaded operator,
8940 then this expression cannot be an integral constant-expression.
8941 An overloaded operator can be used even if both operands are
8942 otherwise permissible in an integral constant-expression if at
8943 least one of the operands is of enumeration type. */
8946 && cp_parser_non_integral_constant_expression (parser,
8948 return error_mark_node;
8955 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8956 bool no_toplevel_fold_p,
8957 enum cp_parser_prec prec,
8960 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8961 /*decltype*/false, prec, pidk);
8964 /* Parse the `? expression : assignment-expression' part of a
8965 conditional-expression. The LOGICAL_OR_EXPR is the
8966 logical-or-expression that started the conditional-expression.
8967 Returns a representation of the entire conditional-expression.
8969 This routine is used by cp_parser_assignment_expression.
8971 ? expression : assignment-expression
8975 ? : assignment-expression */
8978 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
8980 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8981 cp_expr assignment_expr;
8982 struct cp_token *token;
8983 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8985 /* Consume the `?' token. */
8986 cp_lexer_consume_token (parser->lexer);
8987 token = cp_lexer_peek_token (parser->lexer);
8988 if (cp_parser_allow_gnu_extensions_p (parser)
8989 && token->type == CPP_COLON)
8991 pedwarn (token->location, OPT_Wpedantic,
8992 "ISO C++ does not allow ?: with omitted middle operand");
8993 /* Implicit true clause. */
8995 c_inhibit_evaluation_warnings +=
8996 folded_logical_or_expr == truthvalue_true_node;
8997 warn_for_omitted_condop (token->location, logical_or_expr);
9001 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9002 parser->colon_corrects_to_scope_p = false;
9003 /* Parse the expression. */
9004 c_inhibit_evaluation_warnings +=
9005 folded_logical_or_expr == truthvalue_false_node;
9006 expr = cp_parser_expression (parser);
9007 c_inhibit_evaluation_warnings +=
9008 ((folded_logical_or_expr == truthvalue_true_node)
9009 - (folded_logical_or_expr == truthvalue_false_node));
9010 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9013 /* The next token should be a `:'. */
9014 cp_parser_require (parser, CPP_COLON, RT_COLON);
9015 /* Parse the assignment-expression. */
9016 assignment_expr = cp_parser_assignment_expression (parser);
9017 c_inhibit_evaluation_warnings -=
9018 folded_logical_or_expr == truthvalue_true_node;
9021 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9022 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9023 with the caret at the "?", ranging from the start of
9024 the logical_or_expr to the end of the assignment_expr. */
9025 loc = make_location (loc,
9026 logical_or_expr.get_start (),
9027 assignment_expr.get_finish ());
9029 /* Build the conditional-expression. */
9030 return build_x_conditional_expr (loc, logical_or_expr,
9033 tf_warning_or_error);
9036 /* Parse an assignment-expression.
9038 assignment-expression:
9039 conditional-expression
9040 logical-or-expression assignment-operator assignment_expression
9043 CAST_P is true if this expression is the target of a cast.
9044 DECLTYPE_P is true if this expression is the operand of decltype.
9046 Returns a representation for the expression. */
9049 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9050 bool cast_p, bool decltype_p)
9054 /* If the next token is the `throw' keyword, then we're looking at
9055 a throw-expression. */
9056 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9057 expr = cp_parser_throw_expression (parser);
9058 /* Otherwise, it must be that we are looking at a
9059 logical-or-expression. */
9062 /* Parse the binary expressions (logical-or-expression). */
9063 expr = cp_parser_binary_expression (parser, cast_p, false,
9065 PREC_NOT_OPERATOR, pidk);
9066 /* If the next token is a `?' then we're actually looking at a
9067 conditional-expression. */
9068 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9069 return cp_parser_question_colon_clause (parser, expr);
9072 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9074 /* If it's an assignment-operator, we're using the second
9076 enum tree_code assignment_operator
9077 = cp_parser_assignment_operator_opt (parser);
9078 if (assignment_operator != ERROR_MARK)
9080 bool non_constant_p;
9082 /* Parse the right-hand side of the assignment. */
9083 cp_expr rhs = cp_parser_initializer_clause (parser,
9086 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9087 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9089 /* An assignment may not appear in a
9090 constant-expression. */
9091 if (cp_parser_non_integral_constant_expression (parser,
9093 return error_mark_node;
9094 /* Build the assignment expression. Its default
9098 is the location of the '=' token as the
9099 caret, ranging from the start of the lhs to the
9101 loc = make_location (loc,
9104 expr = build_x_modify_expr (loc, expr,
9105 assignment_operator,
9107 complain_flags (decltype_p));
9108 /* TODO: build_x_modify_expr doesn't honor the location,
9109 so we must set it here. */
9110 expr.set_location (loc);
9118 /* Parse an (optional) assignment-operator.
9120 assignment-operator: one of
9121 = *= /= %= += -= >>= <<= &= ^= |=
9125 assignment-operator: one of
9128 If the next token is an assignment operator, the corresponding tree
9129 code is returned, and the token is consumed. For example, for
9130 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9131 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9132 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9133 operator, ERROR_MARK is returned. */
9135 static enum tree_code
9136 cp_parser_assignment_operator_opt (cp_parser* parser)
9141 /* Peek at the next token. */
9142 token = cp_lexer_peek_token (parser->lexer);
9144 switch (token->type)
9155 op = TRUNC_DIV_EXPR;
9159 op = TRUNC_MOD_EXPR;
9191 /* Nothing else is an assignment operator. */
9195 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9196 if (op != ERROR_MARK
9197 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9200 /* If it was an assignment operator, consume it. */
9201 if (op != ERROR_MARK)
9202 cp_lexer_consume_token (parser->lexer);
9207 /* Parse an expression.
9210 assignment-expression
9211 expression , assignment-expression
9213 CAST_P is true if this expression is the target of a cast.
9214 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9215 except possibly parenthesized or on the RHS of a comma (N3276).
9217 Returns a representation of the expression. */
9220 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9221 bool cast_p, bool decltype_p)
9223 cp_expr expression = NULL_TREE;
9224 location_t loc = UNKNOWN_LOCATION;
9228 cp_expr assignment_expression;
9230 /* Parse the next assignment-expression. */
9231 assignment_expression
9232 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9234 /* We don't create a temporary for a call that is the immediate operand
9235 of decltype or on the RHS of a comma. But when we see a comma, we
9236 need to create a temporary for a call on the LHS. */
9237 if (decltype_p && !processing_template_decl
9238 && TREE_CODE (assignment_expression) == CALL_EXPR
9239 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9240 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9241 assignment_expression
9242 = build_cplus_new (TREE_TYPE (assignment_expression),
9243 assignment_expression, tf_warning_or_error);
9245 /* If this is the first assignment-expression, we can just
9248 expression = assignment_expression;
9251 /* Create a location with caret at the comma, ranging
9252 from the start of the LHS to the end of the RHS. */
9253 loc = make_location (loc,
9254 expression.get_start (),
9255 assignment_expression.get_finish ());
9256 expression = build_x_compound_expr (loc, expression,
9257 assignment_expression,
9258 complain_flags (decltype_p));
9259 expression.set_location (loc);
9261 /* If the next token is not a comma, or we're in a fold-expression, then
9262 we are done with the expression. */
9263 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9264 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9266 /* Consume the `,'. */
9267 loc = cp_lexer_peek_token (parser->lexer)->location;
9268 cp_lexer_consume_token (parser->lexer);
9269 /* A comma operator cannot appear in a constant-expression. */
9270 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9271 expression = error_mark_node;
9277 /* Parse a constant-expression.
9279 constant-expression:
9280 conditional-expression
9282 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9283 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9284 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9285 is false, NON_CONSTANT_P should be NULL. */
9288 cp_parser_constant_expression (cp_parser* parser,
9289 bool allow_non_constant_p,
9290 bool *non_constant_p)
9292 bool saved_integral_constant_expression_p;
9293 bool saved_allow_non_integral_constant_expression_p;
9294 bool saved_non_integral_constant_expression_p;
9297 /* It might seem that we could simply parse the
9298 conditional-expression, and then check to see if it were
9299 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9300 one that the compiler can figure out is constant, possibly after
9301 doing some simplifications or optimizations. The standard has a
9302 precise definition of constant-expression, and we must honor
9303 that, even though it is somewhat more restrictive.
9309 is not a legal declaration, because `(2, 3)' is not a
9310 constant-expression. The `,' operator is forbidden in a
9311 constant-expression. However, GCC's constant-folding machinery
9312 will fold this operation to an INTEGER_CST for `3'. */
9314 /* Save the old settings. */
9315 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9316 saved_allow_non_integral_constant_expression_p
9317 = parser->allow_non_integral_constant_expression_p;
9318 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9319 /* We are now parsing a constant-expression. */
9320 parser->integral_constant_expression_p = true;
9321 parser->allow_non_integral_constant_expression_p
9322 = (allow_non_constant_p || cxx_dialect >= cxx11);
9323 parser->non_integral_constant_expression_p = false;
9324 /* Although the grammar says "conditional-expression", we parse an
9325 "assignment-expression", which also permits "throw-expression"
9326 and the use of assignment operators. In the case that
9327 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9328 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9329 actually essential that we look for an assignment-expression.
9330 For example, cp_parser_initializer_clauses uses this function to
9331 determine whether a particular assignment-expression is in fact
9333 expression = cp_parser_assignment_expression (parser);
9334 /* Restore the old settings. */
9335 parser->integral_constant_expression_p
9336 = saved_integral_constant_expression_p;
9337 parser->allow_non_integral_constant_expression_p
9338 = saved_allow_non_integral_constant_expression_p;
9339 if (cxx_dialect >= cxx11)
9341 /* Require an rvalue constant expression here; that's what our
9342 callers expect. Reference constant expressions are handled
9343 separately in e.g. cp_parser_template_argument. */
9344 bool is_const = potential_rvalue_constant_expression (expression);
9345 parser->non_integral_constant_expression_p = !is_const;
9346 if (!is_const && !allow_non_constant_p)
9347 require_potential_rvalue_constant_expression (expression);
9349 if (allow_non_constant_p)
9350 *non_constant_p = parser->non_integral_constant_expression_p;
9351 parser->non_integral_constant_expression_p
9352 = saved_non_integral_constant_expression_p;
9357 /* Parse __builtin_offsetof.
9359 offsetof-expression:
9360 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9362 offsetof-member-designator:
9364 | offsetof-member-designator "." id-expression
9365 | offsetof-member-designator "[" expression "]"
9366 | offsetof-member-designator "->" id-expression */
9369 cp_parser_builtin_offsetof (cp_parser *parser)
9371 int save_ice_p, save_non_ice_p;
9376 location_t finish_loc;
9378 /* We're about to accept non-integral-constant things, but will
9379 definitely yield an integral constant expression. Save and
9380 restore these values around our local parsing. */
9381 save_ice_p = parser->integral_constant_expression_p;
9382 save_non_ice_p = parser->non_integral_constant_expression_p;
9384 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9386 /* Consume the "__builtin_offsetof" token. */
9387 cp_lexer_consume_token (parser->lexer);
9388 /* Consume the opening `('. */
9389 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9390 /* Parse the type-id. */
9391 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9392 type = cp_parser_type_id (parser);
9393 /* Look for the `,'. */
9394 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9395 token = cp_lexer_peek_token (parser->lexer);
9397 /* Build the (type *)null that begins the traditional offsetof macro. */
9398 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9399 tf_warning_or_error);
9401 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9402 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9403 true, &dummy, token->location);
9406 token = cp_lexer_peek_token (parser->lexer);
9407 switch (token->type)
9409 case CPP_OPEN_SQUARE:
9410 /* offsetof-member-designator "[" expression "]" */
9411 expr = cp_parser_postfix_open_square_expression (parser, expr,
9416 /* offsetof-member-designator "->" identifier */
9417 expr = grok_array_decl (token->location, expr,
9418 integer_zero_node, false);
9422 /* offsetof-member-designator "." identifier */
9423 cp_lexer_consume_token (parser->lexer);
9424 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9429 case CPP_CLOSE_PAREN:
9430 /* Consume the ")" token. */
9431 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9432 cp_lexer_consume_token (parser->lexer);
9436 /* Error. We know the following require will fail, but
9437 that gives the proper error message. */
9438 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9439 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9440 expr = error_mark_node;
9446 /* Make a location of the form:
9447 __builtin_offsetof (struct s, f)
9448 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9449 with caret at the type-id, ranging from the start of the
9450 "_builtin_offsetof" token to the close paren. */
9451 loc = make_location (loc, start_loc, finish_loc);
9452 /* The result will be an INTEGER_CST, so we need to explicitly
9453 preserve the location. */
9454 expr = cp_expr (finish_offsetof (expr, loc), loc);
9457 parser->integral_constant_expression_p = save_ice_p;
9458 parser->non_integral_constant_expression_p = save_non_ice_p;
9463 /* Parse a trait expression.
9465 Returns a representation of the expression, the underlying type
9466 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9469 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9472 tree type1, type2 = NULL_TREE;
9473 bool binary = false;
9474 bool variadic = false;
9478 case RID_HAS_NOTHROW_ASSIGN:
9479 kind = CPTK_HAS_NOTHROW_ASSIGN;
9481 case RID_HAS_NOTHROW_CONSTRUCTOR:
9482 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9484 case RID_HAS_NOTHROW_COPY:
9485 kind = CPTK_HAS_NOTHROW_COPY;
9487 case RID_HAS_TRIVIAL_ASSIGN:
9488 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9490 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9491 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9493 case RID_HAS_TRIVIAL_COPY:
9494 kind = CPTK_HAS_TRIVIAL_COPY;
9496 case RID_HAS_TRIVIAL_DESTRUCTOR:
9497 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9499 case RID_HAS_VIRTUAL_DESTRUCTOR:
9500 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9502 case RID_IS_ABSTRACT:
9503 kind = CPTK_IS_ABSTRACT;
9505 case RID_IS_BASE_OF:
9506 kind = CPTK_IS_BASE_OF;
9510 kind = CPTK_IS_CLASS;
9513 kind = CPTK_IS_EMPTY;
9516 kind = CPTK_IS_ENUM;
9519 kind = CPTK_IS_FINAL;
9521 case RID_IS_LITERAL_TYPE:
9522 kind = CPTK_IS_LITERAL_TYPE;
9527 case RID_IS_POLYMORPHIC:
9528 kind = CPTK_IS_POLYMORPHIC;
9530 case RID_IS_SAME_AS:
9531 kind = CPTK_IS_SAME_AS;
9534 case RID_IS_STD_LAYOUT:
9535 kind = CPTK_IS_STD_LAYOUT;
9537 case RID_IS_TRIVIAL:
9538 kind = CPTK_IS_TRIVIAL;
9540 case RID_IS_TRIVIALLY_ASSIGNABLE:
9541 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9544 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9545 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9548 case RID_IS_TRIVIALLY_COPYABLE:
9549 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9552 kind = CPTK_IS_UNION;
9554 case RID_UNDERLYING_TYPE:
9555 kind = CPTK_UNDERLYING_TYPE;
9560 case RID_DIRECT_BASES:
9561 kind = CPTK_DIRECT_BASES;
9567 /* Consume the token. */
9568 cp_lexer_consume_token (parser->lexer);
9570 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9573 type_id_in_expr_sentinel s (parser);
9574 type1 = cp_parser_type_id (parser);
9577 if (type1 == error_mark_node)
9578 return error_mark_node;
9582 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9585 type_id_in_expr_sentinel s (parser);
9586 type2 = cp_parser_type_id (parser);
9589 if (type2 == error_mark_node)
9590 return error_mark_node;
9594 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9596 cp_lexer_consume_token (parser->lexer);
9597 tree elt = cp_parser_type_id (parser);
9598 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9600 cp_lexer_consume_token (parser->lexer);
9601 elt = make_pack_expansion (elt);
9603 if (elt == error_mark_node)
9604 return error_mark_node;
9605 type2 = tree_cons (NULL_TREE, elt, type2);
9609 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9611 /* Complete the trait expression, which may mean either processing
9612 the trait expr now or saving it for template instantiation. */
9615 case CPTK_UNDERLYING_TYPE:
9616 return finish_underlying_type (type1);
9618 return finish_bases (type1, false);
9619 case CPTK_DIRECT_BASES:
9620 return finish_bases (type1, true);
9622 return finish_trait_expr (kind, type1, type2);
9626 /* Lambdas that appear in variable initializer or default argument scope
9627 get that in their mangling, so we need to record it. We might as well
9628 use the count for function and namespace scopes as well. */
9629 static GTY(()) tree lambda_scope;
9630 static GTY(()) int lambda_count;
9631 struct GTY(()) tree_int
9636 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9639 start_lambda_scope (tree decl)
9643 /* Once we're inside a function, we ignore other scopes and just push
9644 the function again so that popping works properly. */
9645 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9646 decl = current_function_decl;
9647 ti.t = lambda_scope;
9648 ti.i = lambda_count;
9649 vec_safe_push (lambda_scope_stack, ti);
9650 if (lambda_scope != decl)
9652 /* Don't reset the count if we're still in the same function. */
9653 lambda_scope = decl;
9659 record_lambda_scope (tree lambda)
9661 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9662 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9666 finish_lambda_scope (void)
9668 tree_int *p = &lambda_scope_stack->last ();
9669 if (lambda_scope != p->t)
9671 lambda_scope = p->t;
9672 lambda_count = p->i;
9674 lambda_scope_stack->pop ();
9677 /* Parse a lambda expression.
9680 lambda-introducer lambda-declarator [opt] compound-statement
9682 Returns a representation of the expression. */
9685 cp_parser_lambda_expression (cp_parser* parser)
9687 tree lambda_expr = build_lambda_expr ();
9690 cp_token *token = cp_lexer_peek_token (parser->lexer);
9691 cp_token_position start = 0;
9693 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9695 if (cp_unevaluated_operand)
9697 if (!token->error_reported)
9699 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9700 "lambda-expression in unevaluated context");
9701 token->error_reported = true;
9705 else if (parser->in_template_argument_list_p)
9707 if (!token->error_reported)
9709 error_at (token->location, "lambda-expression in template-argument");
9710 token->error_reported = true;
9715 /* We may be in the middle of deferred access check. Disable
9717 push_deferring_access_checks (dk_no_deferred);
9719 cp_parser_lambda_introducer (parser, lambda_expr);
9721 type = begin_lambda_type (lambda_expr);
9722 if (type == error_mark_node)
9723 return error_mark_node;
9725 record_lambda_scope (lambda_expr);
9727 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9728 determine_visibility (TYPE_NAME (type));
9730 /* Now that we've started the type, add the capture fields for any
9731 explicit captures. */
9732 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9735 /* Inside the class, surrounding template-parameter-lists do not apply. */
9736 unsigned int saved_num_template_parameter_lists
9737 = parser->num_template_parameter_lists;
9738 unsigned char in_statement = parser->in_statement;
9739 bool in_switch_statement_p = parser->in_switch_statement_p;
9740 bool fully_implicit_function_template_p
9741 = parser->fully_implicit_function_template_p;
9742 tree implicit_template_parms = parser->implicit_template_parms;
9743 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9744 bool auto_is_implicit_function_template_parm_p
9745 = parser->auto_is_implicit_function_template_parm_p;
9747 parser->num_template_parameter_lists = 0;
9748 parser->in_statement = 0;
9749 parser->in_switch_statement_p = false;
9750 parser->fully_implicit_function_template_p = false;
9751 parser->implicit_template_parms = 0;
9752 parser->implicit_template_scope = 0;
9753 parser->auto_is_implicit_function_template_parm_p = false;
9755 /* By virtue of defining a local class, a lambda expression has access to
9756 the private variables of enclosing classes. */
9758 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9760 if (ok && cp_parser_error_occurred (parser))
9765 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9766 && cp_parser_start_tentative_firewall (parser))
9768 cp_parser_lambda_body (parser, lambda_expr);
9770 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9772 if (cp_parser_skip_to_closing_brace (parser))
9773 cp_lexer_consume_token (parser->lexer);
9776 /* The capture list was built up in reverse order; fix that now. */
9777 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9778 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9781 maybe_add_lambda_conv_op (type);
9783 type = finish_struct (type, /*attributes=*/NULL_TREE);
9785 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9786 parser->in_statement = in_statement;
9787 parser->in_switch_statement_p = in_switch_statement_p;
9788 parser->fully_implicit_function_template_p
9789 = fully_implicit_function_template_p;
9790 parser->implicit_template_parms = implicit_template_parms;
9791 parser->implicit_template_scope = implicit_template_scope;
9792 parser->auto_is_implicit_function_template_parm_p
9793 = auto_is_implicit_function_template_parm_p;
9796 /* This field is only used during parsing of the lambda. */
9797 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9799 /* This lambda shouldn't have any proxies left at this point. */
9800 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9801 /* And now that we're done, push proxies for an enclosing lambda. */
9802 insert_pending_capture_proxies ();
9805 lambda_expr = build_lambda_object (lambda_expr);
9807 lambda_expr = error_mark_node;
9809 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9811 pop_deferring_access_checks ();
9816 /* Parse the beginning of a lambda expression.
9819 [ lambda-capture [opt] ]
9821 LAMBDA_EXPR is the current representation of the lambda expression. */
9824 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9826 /* Need commas after the first capture. */
9829 /* Eat the leading `['. */
9830 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9832 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9833 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9834 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9835 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9836 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9837 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9839 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9841 cp_lexer_consume_token (parser->lexer);
9845 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9847 cp_token* capture_token;
9849 tree capture_init_expr;
9850 cp_id_kind idk = CP_ID_KIND_NONE;
9851 bool explicit_init_p = false;
9853 enum capture_kind_type
9858 enum capture_kind_type capture_kind = BY_COPY;
9860 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9862 error ("expected end of capture-list");
9869 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9871 /* Possibly capture `this'. */
9872 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9874 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9875 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9876 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9877 "with by-copy capture default");
9878 cp_lexer_consume_token (parser->lexer);
9879 add_capture (lambda_expr,
9880 /*id=*/this_identifier,
9881 /*initializer=*/finish_this_expr(),
9882 /*by_reference_p=*/false,
9887 /* Remember whether we want to capture as a reference or not. */
9888 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9890 capture_kind = BY_REFERENCE;
9891 cp_lexer_consume_token (parser->lexer);
9894 /* Get the identifier. */
9895 capture_token = cp_lexer_peek_token (parser->lexer);
9896 capture_id = cp_parser_identifier (parser);
9898 if (capture_id == error_mark_node)
9899 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9900 delimiters, but I modified this to stop on unnested ']' as well. It
9901 was already changed to stop on unnested '}', so the
9902 "closing_parenthesis" name is no more misleading with my change. */
9904 cp_parser_skip_to_closing_parenthesis (parser,
9905 /*recovering=*/true,
9907 /*consume_paren=*/true);
9911 /* Find the initializer for this capture. */
9912 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9913 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9914 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9916 bool direct, non_constant;
9917 /* An explicit initializer exists. */
9918 if (cxx_dialect < cxx14)
9919 pedwarn (input_location, 0,
9920 "lambda capture initializers "
9921 "only available with -std=c++14 or -std=gnu++14");
9922 capture_init_expr = cp_parser_initializer (parser, &direct,
9924 explicit_init_p = true;
9925 if (capture_init_expr == NULL_TREE)
9927 error ("empty initializer for lambda init-capture");
9928 capture_init_expr = error_mark_node;
9933 const char* error_msg;
9935 /* Turn the identifier into an id-expression. */
9937 = cp_parser_lookup_name_simple (parser, capture_id,
9938 capture_token->location);
9940 if (capture_init_expr == error_mark_node)
9942 unqualified_name_lookup_error (capture_id);
9945 else if (DECL_P (capture_init_expr)
9946 && (!VAR_P (capture_init_expr)
9947 && TREE_CODE (capture_init_expr) != PARM_DECL))
9949 error_at (capture_token->location,
9950 "capture of non-variable %qD ",
9952 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9953 "%q#D declared here", capture_init_expr);
9956 if (VAR_P (capture_init_expr)
9957 && decl_storage_duration (capture_init_expr) != dk_auto)
9959 if (pedwarn (capture_token->location, 0, "capture of variable "
9960 "%qD with non-automatic storage duration",
9962 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9963 "%q#D declared here", capture_init_expr);
9968 = finish_id_expression
9973 /*integral_constant_expression_p=*/false,
9974 /*allow_non_integral_constant_expression_p=*/false,
9975 /*non_integral_constant_expression_p=*/NULL,
9976 /*template_p=*/false,
9978 /*address_p=*/false,
9979 /*template_arg_p=*/false,
9981 capture_token->location);
9983 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9985 cp_lexer_consume_token (parser->lexer);
9986 capture_init_expr = make_pack_expansion (capture_init_expr);
9989 check_for_bare_parameter_packs (capture_init_expr);
9992 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9993 && !explicit_init_p)
9995 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9996 && capture_kind == BY_COPY)
9997 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9998 "of %qD redundant with by-copy capture default",
10000 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10001 && capture_kind == BY_REFERENCE)
10002 pedwarn (capture_token->location, 0, "explicit by-reference "
10003 "capture of %qD redundant with by-reference capture "
10004 "default", capture_id);
10007 add_capture (lambda_expr,
10010 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10014 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10017 /* Parse the (optional) middle of a lambda expression.
10020 < template-parameter-list [opt] >
10021 ( parameter-declaration-clause [opt] )
10022 attribute-specifier [opt]
10024 exception-specification [opt]
10025 lambda-return-type-clause [opt]
10027 LAMBDA_EXPR is the current representation of the lambda expression. */
10030 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10032 /* 5.1.1.4 of the standard says:
10033 If a lambda-expression does not include a lambda-declarator, it is as if
10034 the lambda-declarator were ().
10035 This means an empty parameter list, no attributes, and no exception
10037 tree param_list = void_list_node;
10038 tree attributes = NULL_TREE;
10039 tree exception_spec = NULL_TREE;
10040 tree template_param_list = NULL_TREE;
10041 tree tx_qual = NULL_TREE;
10043 /* The template-parameter-list is optional, but must begin with
10044 an opening angle if present. */
10045 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10047 if (cxx_dialect < cxx14)
10048 pedwarn (parser->lexer->next_token->location, 0,
10049 "lambda templates are only available with "
10050 "-std=c++14 or -std=gnu++14");
10052 cp_lexer_consume_token (parser->lexer);
10054 template_param_list = cp_parser_template_parameter_list (parser);
10056 cp_parser_skip_to_end_of_template_parameter_list (parser);
10058 /* We just processed one more parameter list. */
10059 ++parser->num_template_parameter_lists;
10062 /* The parameter-declaration-clause is optional (unless
10063 template-parameter-list was given), but must begin with an
10064 opening parenthesis if present. */
10065 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10067 cp_lexer_consume_token (parser->lexer);
10069 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10071 /* Parse parameters. */
10072 param_list = cp_parser_parameter_declaration_clause (parser);
10074 /* Default arguments shall not be specified in the
10075 parameter-declaration-clause of a lambda-declarator. */
10076 for (tree t = param_list; t; t = TREE_CHAIN (t))
10077 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10078 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10079 "default argument specified for lambda parameter");
10081 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10083 attributes = cp_parser_attributes_opt (parser);
10085 /* Parse optional `mutable' keyword. */
10086 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
10088 cp_lexer_consume_token (parser->lexer);
10089 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10092 tx_qual = cp_parser_tx_qualifier_opt (parser);
10094 /* Parse optional exception specification. */
10095 exception_spec = cp_parser_exception_specification_opt (parser);
10097 /* Parse optional trailing return type. */
10098 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10100 cp_lexer_consume_token (parser->lexer);
10101 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10102 = cp_parser_trailing_type_id (parser);
10105 /* The function parameters must be in scope all the way until after the
10106 trailing-return-type in case of decltype. */
10107 pop_bindings_and_leave_scope ();
10109 else if (template_param_list != NULL_TREE) // generate diagnostic
10110 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10112 /* Create the function call operator.
10114 Messing with declarators like this is no uglier than building up the
10115 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10118 cp_decl_specifier_seq return_type_specs;
10119 cp_declarator* declarator;
10124 clear_decl_specs (&return_type_specs);
10125 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10126 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10128 /* Maybe we will deduce the return type later. */
10129 return_type_specs.type = make_auto ();
10131 p = obstack_alloc (&declarator_obstack, 0);
10133 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10136 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10137 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10138 declarator = make_call_declarator (declarator, param_list, quals,
10139 VIRT_SPEC_UNSPECIFIED,
10143 /*late_return_type=*/NULL_TREE,
10144 /*requires_clause*/NULL_TREE);
10145 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10147 fco = grokmethod (&return_type_specs,
10150 if (fco != error_mark_node)
10152 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10153 DECL_ARTIFICIAL (fco) = 1;
10154 /* Give the object parameter a different name. */
10155 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10156 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10157 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10159 if (template_param_list)
10161 fco = finish_member_template_decl (fco);
10162 finish_template_decl (template_param_list);
10163 --parser->num_template_parameter_lists;
10165 else if (parser->fully_implicit_function_template_p)
10166 fco = finish_fully_implicit_template (parser, fco);
10168 finish_member_declaration (fco);
10170 obstack_free (&declarator_obstack, p);
10172 return (fco != error_mark_node);
10176 /* Parse the body of a lambda expression, which is simply
10180 but which requires special handling.
10181 LAMBDA_EXPR is the current representation of the lambda expression. */
10184 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10186 bool nested = (current_function_decl != NULL_TREE);
10187 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10189 push_function_context ();
10191 /* Still increment function_depth so that we don't GC in the
10192 middle of an expression. */
10194 vec<tree> omp_privatization_save;
10195 save_omp_privatization_clauses (omp_privatization_save);
10196 /* Clear this in case we're in the middle of a default argument. */
10197 parser->local_variables_forbidden_p = false;
10199 /* Finish the function call operator
10201 + late_parsing_for_member
10202 + function_definition_after_declarator
10203 + ctor_initializer_opt_and_function_body */
10205 tree fco = lambda_function (lambda_expr);
10208 tree compound_stmt;
10211 /* Let the front end know that we are going to be defining this
10213 start_preparsed_function (fco,
10215 SF_PRE_PARSED | SF_INCLASS_INLINE);
10217 start_lambda_scope (fco);
10218 body = begin_function_body ();
10220 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10223 /* Push the proxies for any explicit captures. */
10224 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10225 cap = TREE_CHAIN (cap))
10226 build_capture_proxy (TREE_PURPOSE (cap));
10228 compound_stmt = begin_compound_stmt (0);
10230 /* 5.1.1.4 of the standard says:
10231 If a lambda-expression does not include a trailing-return-type, it
10232 is as if the trailing-return-type denotes the following type:
10233 * if the compound-statement is of the form
10234 { return attribute-specifier [opt] expression ; }
10235 the type of the returned expression after lvalue-to-rvalue
10236 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10237 (_conv.array_ 4.2), and function-to-pointer conversion
10239 * otherwise, void. */
10241 /* In a lambda that has neither a lambda-return-type-clause
10242 nor a deducible form, errors should be reported for return statements
10243 in the body. Since we used void as the placeholder return type, parsing
10244 the body as usual will give such desired behavior. */
10245 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10246 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10247 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10249 tree expr = NULL_TREE;
10250 cp_id_kind idk = CP_ID_KIND_NONE;
10252 /* Parse tentatively in case there's more after the initial return
10254 cp_parser_parse_tentatively (parser);
10256 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10258 expr = cp_parser_expression (parser, &idk);
10260 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10261 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10263 if (cp_parser_parse_definitely (parser))
10265 if (!processing_template_decl)
10267 tree type = lambda_return_type (expr);
10268 apply_deduced_return_type (fco, type);
10269 if (type == error_mark_node)
10270 expr = error_mark_node;
10273 /* Will get error here if type not deduced yet. */
10274 finish_return_stmt (expr);
10282 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10283 cp_parser_label_declaration (parser);
10284 cp_parser_statement_seq_opt (parser, NULL_TREE);
10285 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10288 finish_compound_stmt (compound_stmt);
10291 finish_function_body (body);
10292 finish_lambda_scope ();
10294 /* Finish the function and generate code for it if necessary. */
10295 tree fn = finish_function (/*inline*/2);
10297 /* Only expand if the call op is not a template. */
10298 if (!DECL_TEMPLATE_INFO (fco))
10299 expand_or_defer_fn (fn);
10302 restore_omp_privatization_clauses (omp_privatization_save);
10303 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10305 pop_function_context();
10310 /* Statements [gram.stmt.stmt] */
10312 /* Parse a statement.
10316 expression-statement
10318 selection-statement
10319 iteration-statement
10321 declaration-statement
10328 attribute-specifier-seq (opt) expression-statement
10329 attribute-specifier-seq (opt) compound-statement
10330 attribute-specifier-seq (opt) selection-statement
10331 attribute-specifier-seq (opt) iteration-statement
10332 attribute-specifier-seq (opt) jump-statement
10333 declaration-statement
10334 attribute-specifier-seq (opt) try-block
10341 IN_COMPOUND is true when the statement is nested inside a
10342 cp_parser_compound_statement; this matters for certain pragmas.
10344 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10345 is a (possibly labeled) if statement which is not enclosed in braces
10346 and has an else clause. This is used to implement -Wparentheses.
10348 CHAIN is a vector of if-else-if conditions. */
10351 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10352 bool in_compound, bool *if_p, vec<tree> *chain)
10354 tree statement, std_attrs = NULL_TREE;
10356 location_t statement_location, attrs_location;
10361 /* There is no statement yet. */
10362 statement = NULL_TREE;
10364 saved_token_sentinel saved_tokens (parser->lexer);
10365 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10366 if (c_dialect_objc ())
10367 /* In obj-c++, seeing '[[' might be the either the beginning of
10368 c++11 attributes, or a nested objc-message-expression. So
10369 let's parse the c++11 attributes tentatively. */
10370 cp_parser_parse_tentatively (parser);
10371 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10372 if (c_dialect_objc ())
10374 if (!cp_parser_parse_definitely (parser))
10375 std_attrs = NULL_TREE;
10378 /* Peek at the next token. */
10379 token = cp_lexer_peek_token (parser->lexer);
10380 /* Remember the location of the first token in the statement. */
10381 statement_location = token->location;
10382 /* If this is a keyword, then that will often determine what kind of
10383 statement we have. */
10384 if (token->type == CPP_KEYWORD)
10386 enum rid keyword = token->keyword;
10392 /* Looks like a labeled-statement with a case label.
10393 Parse the label, and then use tail recursion to parse
10395 cp_parser_label_for_labeled_statement (parser, std_attrs);
10396 in_compound = false;
10401 statement = cp_parser_selection_statement (parser, if_p, chain);
10407 statement = cp_parser_iteration_statement (parser, if_p, false);
10411 if (!flag_cilkplus)
10413 error_at (cp_lexer_peek_token (parser->lexer)->location,
10414 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10415 cp_lexer_consume_token (parser->lexer);
10416 statement = error_mark_node;
10419 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10426 statement = cp_parser_jump_statement (parser);
10429 case RID_CILK_SYNC:
10430 cp_lexer_consume_token (parser->lexer);
10433 tree sync_expr = build_cilk_sync ();
10434 SET_EXPR_LOCATION (sync_expr,
10436 statement = finish_expr_stmt (sync_expr);
10440 error_at (token->location, "-fcilkplus must be enabled to use"
10441 " %<_Cilk_sync%>");
10442 statement = error_mark_node;
10444 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10447 /* Objective-C++ exception-handling constructs. */
10450 case RID_AT_FINALLY:
10451 case RID_AT_SYNCHRONIZED:
10453 statement = cp_parser_objc_statement (parser);
10457 statement = cp_parser_try_block (parser);
10460 case RID_NAMESPACE:
10461 /* This must be a namespace alias definition. */
10462 cp_parser_declaration_statement (parser);
10465 case RID_TRANSACTION_ATOMIC:
10466 case RID_TRANSACTION_RELAXED:
10467 case RID_SYNCHRONIZED:
10468 case RID_ATOMIC_NOEXCEPT:
10469 case RID_ATOMIC_CANCEL:
10470 statement = cp_parser_transaction (parser, token);
10472 case RID_TRANSACTION_CANCEL:
10473 statement = cp_parser_transaction_cancel (parser);
10477 /* It might be a keyword like `int' that can start a
10478 declaration-statement. */
10482 else if (token->type == CPP_NAME)
10484 /* If the next token is a `:', then we are looking at a
10485 labeled-statement. */
10486 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10487 if (token->type == CPP_COLON)
10489 /* Looks like a labeled-statement with an ordinary label.
10490 Parse the label, and then use tail recursion to parse
10493 cp_parser_label_for_labeled_statement (parser, std_attrs);
10494 in_compound = false;
10498 /* Anything that starts with a `{' must be a compound-statement. */
10499 else if (token->type == CPP_OPEN_BRACE)
10500 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10501 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10502 a statement all its own. */
10503 else if (token->type == CPP_PRAGMA)
10505 /* Only certain OpenMP pragmas are attached to statements, and thus
10506 are considered statements themselves. All others are not. In
10507 the context of a compound, accept the pragma as a "statement" and
10508 return so that we can check for a close brace. Otherwise we
10509 require a real statement and must go back and read one. */
10511 cp_parser_pragma (parser, pragma_compound, if_p);
10512 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10516 else if (token->type == CPP_EOF)
10518 cp_parser_error (parser, "expected statement");
10522 /* Everything else must be a declaration-statement or an
10523 expression-statement. Try for the declaration-statement
10524 first, unless we are looking at a `;', in which case we know that
10525 we have an expression-statement. */
10528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10530 if (std_attrs != NULL_TREE)
10532 /* Attributes should be parsed as part of the the
10533 declaration, so let's un-parse them. */
10534 saved_tokens.rollback();
10535 std_attrs = NULL_TREE;
10538 cp_parser_parse_tentatively (parser);
10539 /* Try to parse the declaration-statement. */
10540 cp_parser_declaration_statement (parser);
10541 /* If that worked, we're done. */
10542 if (cp_parser_parse_definitely (parser))
10545 /* Look for an expression-statement instead. */
10546 statement = cp_parser_expression_statement (parser, in_statement_expr);
10549 /* Set the line number for the statement. */
10550 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10551 SET_EXPR_LOCATION (statement, statement_location);
10553 /* Note that for now, we don't do anything with c++11 statements
10554 parsed at this level. */
10555 if (std_attrs != NULL_TREE)
10556 warning_at (attrs_location,
10558 "attributes at the beginning of statement are ignored");
10561 /* Parse the label for a labeled-statement, i.e.
10564 case constant-expression :
10568 case constant-expression ... constant-expression : statement
10570 When a label is parsed without errors, the label is added to the
10571 parse tree by the finish_* functions, so this function doesn't
10572 have to return the label. */
10575 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10578 tree label = NULL_TREE;
10579 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10581 /* The next token should be an identifier. */
10582 token = cp_lexer_peek_token (parser->lexer);
10583 if (token->type != CPP_NAME
10584 && token->type != CPP_KEYWORD)
10586 cp_parser_error (parser, "expected labeled-statement");
10590 parser->colon_corrects_to_scope_p = false;
10591 switch (token->keyword)
10595 tree expr, expr_hi;
10596 cp_token *ellipsis;
10598 /* Consume the `case' token. */
10599 cp_lexer_consume_token (parser->lexer);
10600 /* Parse the constant-expression. */
10601 expr = cp_parser_constant_expression (parser);
10602 if (check_for_bare_parameter_packs (expr))
10603 expr = error_mark_node;
10605 ellipsis = cp_lexer_peek_token (parser->lexer);
10606 if (ellipsis->type == CPP_ELLIPSIS)
10608 /* Consume the `...' token. */
10609 cp_lexer_consume_token (parser->lexer);
10610 expr_hi = cp_parser_constant_expression (parser);
10611 if (check_for_bare_parameter_packs (expr_hi))
10612 expr_hi = error_mark_node;
10614 /* We don't need to emit warnings here, as the common code
10615 will do this for us. */
10618 expr_hi = NULL_TREE;
10620 if (parser->in_switch_statement_p)
10621 finish_case_label (token->location, expr, expr_hi);
10623 error_at (token->location,
10624 "case label %qE not within a switch statement",
10630 /* Consume the `default' token. */
10631 cp_lexer_consume_token (parser->lexer);
10633 if (parser->in_switch_statement_p)
10634 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10636 error_at (token->location, "case label not within a switch statement");
10640 /* Anything else must be an ordinary label. */
10641 label = finish_label_stmt (cp_parser_identifier (parser));
10645 /* Require the `:' token. */
10646 cp_parser_require (parser, CPP_COLON, RT_COLON);
10648 /* An ordinary label may optionally be followed by attributes.
10649 However, this is only permitted if the attributes are then
10650 followed by a semicolon. This is because, for backward
10651 compatibility, when parsing
10652 lab: __attribute__ ((unused)) int i;
10653 we want the attribute to attach to "i", not "lab". */
10654 if (label != NULL_TREE
10655 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10658 cp_parser_parse_tentatively (parser);
10659 attrs = cp_parser_gnu_attributes_opt (parser);
10660 if (attrs == NULL_TREE
10661 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10662 cp_parser_abort_tentative_parse (parser);
10663 else if (!cp_parser_parse_definitely (parser))
10666 attributes = chainon (attributes, attrs);
10669 if (attributes != NULL_TREE)
10670 cplus_decl_attributes (&label, attributes, 0);
10672 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10675 /* Parse an expression-statement.
10677 expression-statement:
10680 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10681 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10682 indicates whether this expression-statement is part of an
10683 expression statement. */
10686 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10688 tree statement = NULL_TREE;
10689 cp_token *token = cp_lexer_peek_token (parser->lexer);
10691 /* If the next token is a ';', then there is no expression
10693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10695 statement = cp_parser_expression (parser);
10696 if (statement == error_mark_node
10697 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10699 cp_parser_skip_to_end_of_block_or_statement (parser);
10700 return error_mark_node;
10704 /* Give a helpful message for "A<T>::type t;" and the like. */
10705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10706 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10708 if (TREE_CODE (statement) == SCOPE_REF)
10709 error_at (token->location, "need %<typename%> before %qE because "
10710 "%qT is a dependent scope",
10711 statement, TREE_OPERAND (statement, 0));
10712 else if (is_overloaded_fn (statement)
10713 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10716 tree fn = get_first_fn (statement);
10717 error_at (token->location,
10718 "%<%T::%D%> names the constructor, not the type",
10719 DECL_CONTEXT (fn), DECL_NAME (fn));
10723 /* Consume the final `;'. */
10724 cp_parser_consume_semicolon_at_end_of_statement (parser);
10726 if (in_statement_expr
10727 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10728 /* This is the final expression statement of a statement
10730 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10731 else if (statement)
10732 statement = finish_expr_stmt (statement);
10737 /* Parse a compound-statement.
10739 compound-statement:
10740 { statement-seq [opt] }
10744 compound-statement:
10745 { label-declaration-seq [opt] statement-seq [opt] }
10747 label-declaration-seq:
10749 label-declaration-seq label-declaration
10751 Returns a tree representing the statement. */
10754 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10755 int bcs_flags, bool function_body)
10757 tree compound_stmt;
10759 /* Consume the `{'. */
10760 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10761 return error_mark_node;
10762 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10763 && !function_body && cxx_dialect < cxx14)
10764 pedwarn (input_location, OPT_Wpedantic,
10765 "compound-statement in constexpr function");
10766 /* Begin the compound-statement. */
10767 compound_stmt = begin_compound_stmt (bcs_flags);
10768 /* If the next keyword is `__label__' we have a label declaration. */
10769 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10770 cp_parser_label_declaration (parser);
10771 /* Parse an (optional) statement-seq. */
10772 cp_parser_statement_seq_opt (parser, in_statement_expr);
10773 /* Consume the `}' and keep location, if there is a `}' and compound_stmt is
10775 cp_token *token = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10777 TREE_CODE(compound_stmt) == STATEMENT_LIST &&
10778 STATEMENT_LIST_HEAD (compound_stmt) &&
10779 STATEMENT_LIST_TAIL (compound_stmt))
10781 tree brace_stmt = build0(STATEMENT_LIST_END, void_type_node);
10782 SET_EXPR_LOCATION(brace_stmt, token->location);
10783 add_stmt(brace_stmt);
10785 /* Finish the compound-statement. */
10786 finish_compound_stmt (compound_stmt);
10787 return compound_stmt;
10790 /* Parse an (optional) statement-seq.
10794 statement-seq [opt] statement */
10797 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10799 /* Scan statements until there aren't any more. */
10802 cp_token *token = cp_lexer_peek_token (parser->lexer);
10804 /* If we are looking at a `}', then we have run out of
10805 statements; the same is true if we have reached the end
10806 of file, or have stumbled upon a stray '@end'. */
10807 if (token->type == CPP_CLOSE_BRACE
10808 || token->type == CPP_EOF
10809 || token->type == CPP_PRAGMA_EOL
10810 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10813 /* If we are in a compound statement and find 'else' then
10814 something went wrong. */
10815 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10817 if (parser->in_statement & IN_IF_STMT)
10821 token = cp_lexer_consume_token (parser->lexer);
10822 error_at (token->location, "%<else%> without a previous %<if%>");
10826 /* Parse the statement. */
10827 cp_parser_statement (parser, in_statement_expr, true, NULL);
10831 /* Parse a selection-statement.
10833 selection-statement:
10834 if ( condition ) statement
10835 if ( condition ) statement else statement
10836 switch ( condition ) statement
10838 Returns the new IF_STMT or SWITCH_STMT.
10840 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10841 is a (possibly labeled) if statement which is not enclosed in
10842 braces and has an else clause. This is used to implement
10845 CHAIN is a vector of if-else-if conditions. This is used to implement
10846 -Wduplicated-cond. */
10849 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10854 token_indent_info guard_tinfo;
10859 /* Peek at the next token. */
10860 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10861 guard_tinfo = get_token_indent_info (token);
10863 /* See what kind of keyword it is. */
10864 keyword = token->keyword;
10873 /* Look for the `('. */
10874 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10876 cp_parser_skip_to_end_of_statement (parser);
10877 return error_mark_node;
10880 /* Begin the selection-statement. */
10881 if (keyword == RID_IF)
10882 statement = begin_if_stmt ();
10884 statement = begin_switch_stmt ();
10886 /* Parse the condition. */
10887 condition = cp_parser_condition (parser);
10888 /* Look for the `)'. */
10889 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10890 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10891 /*consume_paren=*/true);
10893 if (keyword == RID_IF)
10896 unsigned char in_statement;
10898 /* Add the condition. */
10899 finish_if_stmt_cond (condition, statement);
10901 if (warn_duplicated_cond)
10902 warn_duplicated_cond_add_or_warn (token->location, condition,
10905 /* Parse the then-clause. */
10906 in_statement = parser->in_statement;
10907 parser->in_statement |= IN_IF_STMT;
10908 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10910 parser->in_statement = in_statement;
10912 finish_then_clause (statement);
10914 /* If the next token is `else', parse the else-clause. */
10915 if (cp_lexer_next_token_is_keyword (parser->lexer,
10919 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10920 /* Consume the `else' keyword. */
10921 cp_lexer_consume_token (parser->lexer);
10922 if (warn_duplicated_cond)
10924 if (cp_lexer_next_token_is_keyword (parser->lexer,
10928 /* We've got "if (COND) else if (COND2)". Start
10929 the condition chain and add COND as the first
10931 chain = new vec<tree> ();
10932 if (!CONSTANT_CLASS_P (condition)
10933 && !TREE_SIDE_EFFECTS (condition))
10935 /* Wrap it in a NOP_EXPR so that we can set the
10936 location of the condition. */
10937 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10939 SET_EXPR_LOCATION (e, token->location);
10940 chain->safe_push (e);
10943 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10946 /* This is if-else without subsequent if. Zap the
10947 condition chain; we would have already warned at
10953 begin_else_clause (statement);
10954 /* Parse the else-clause. */
10955 cp_parser_implicitly_scoped_statement (parser, NULL,
10956 guard_tinfo, chain);
10958 finish_else_clause (statement);
10960 /* If we are currently parsing a then-clause, then
10961 IF_P will not be NULL. We set it to true to
10962 indicate that this if statement has an else clause.
10963 This may trigger the Wparentheses warning below
10964 when we get back up to the parent if statement. */
10970 /* This if statement does not have an else clause. If
10971 NESTED_IF is true, then the then-clause has an if
10972 statement which does have an else clause. We warn
10973 about the potential ambiguity. */
10975 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10976 "suggest explicit braces to avoid ambiguous"
10978 if (warn_duplicated_cond)
10980 /* We don't need the condition chain anymore. */
10986 /* Now we're all done with the if-statement. */
10987 finish_if_stmt (statement);
10991 bool in_switch_statement_p;
10992 unsigned char in_statement;
10994 /* Add the condition. */
10995 finish_switch_cond (condition, statement);
10997 /* Parse the body of the switch-statement. */
10998 in_switch_statement_p = parser->in_switch_statement_p;
10999 in_statement = parser->in_statement;
11000 parser->in_switch_statement_p = true;
11001 parser->in_statement |= IN_SWITCH_STMT;
11002 cp_parser_implicitly_scoped_statement (parser, NULL,
11004 parser->in_switch_statement_p = in_switch_statement_p;
11005 parser->in_statement = in_statement;
11007 /* Now we're all done with the switch-statement. */
11008 finish_switch_stmt (statement);
11016 cp_parser_error (parser, "expected selection-statement");
11017 return error_mark_node;
11021 /* Parse a condition.
11025 type-specifier-seq declarator = initializer-clause
11026 type-specifier-seq declarator braced-init-list
11031 type-specifier-seq declarator asm-specification [opt]
11032 attributes [opt] = assignment-expression
11034 Returns the expression that should be tested. */
11037 cp_parser_condition (cp_parser* parser)
11039 cp_decl_specifier_seq type_specifiers;
11040 const char *saved_message;
11041 int declares_class_or_enum;
11043 /* Try the declaration first. */
11044 cp_parser_parse_tentatively (parser);
11045 /* New types are not allowed in the type-specifier-seq for a
11047 saved_message = parser->type_definition_forbidden_message;
11048 parser->type_definition_forbidden_message
11049 = G_("types may not be defined in conditions");
11050 /* Parse the type-specifier-seq. */
11051 cp_parser_decl_specifier_seq (parser,
11052 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11054 &declares_class_or_enum);
11055 /* Restore the saved message. */
11056 parser->type_definition_forbidden_message = saved_message;
11057 /* If all is well, we might be looking at a declaration. */
11058 if (!cp_parser_error_occurred (parser))
11061 tree asm_specification;
11063 cp_declarator *declarator;
11064 tree initializer = NULL_TREE;
11066 /* Parse the declarator. */
11067 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11068 /*ctor_dtor_or_conv_p=*/NULL,
11069 /*parenthesized_p=*/NULL,
11070 /*member_p=*/false,
11071 /*friend_p=*/false);
11072 /* Parse the attributes. */
11073 attributes = cp_parser_attributes_opt (parser);
11074 /* Parse the asm-specification. */
11075 asm_specification = cp_parser_asm_specification_opt (parser);
11076 /* If the next token is not an `=' or '{', then we might still be
11077 looking at an expression. For example:
11081 looks like a decl-specifier-seq and a declarator -- but then
11082 there is no `=', so this is an expression. */
11083 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11084 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11085 cp_parser_simulate_error (parser);
11087 /* If we did see an `=' or '{', then we are looking at a declaration
11089 if (cp_parser_parse_definitely (parser))
11092 bool non_constant_p;
11093 bool flags = LOOKUP_ONLYCONVERTING;
11095 /* Create the declaration. */
11096 decl = start_decl (declarator, &type_specifiers,
11097 /*initialized_p=*/true,
11098 attributes, /*prefix_attributes=*/NULL_TREE,
11101 /* Parse the initializer. */
11102 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11104 initializer = cp_parser_braced_list (parser, &non_constant_p);
11105 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11110 /* Consume the `='. */
11111 cp_parser_require (parser, CPP_EQ, RT_EQ);
11112 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11114 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11115 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11117 /* Process the initializer. */
11118 cp_finish_decl (decl,
11119 initializer, !non_constant_p,
11124 pop_scope (pushed_scope);
11126 return convert_from_reference (decl);
11129 /* If we didn't even get past the declarator successfully, we are
11130 definitely not looking at a declaration. */
11132 cp_parser_abort_tentative_parse (parser);
11134 /* Otherwise, we are looking at an expression. */
11135 return cp_parser_expression (parser);
11138 /* Parses a for-statement or range-for-statement until the closing ')',
11142 cp_parser_for (cp_parser *parser, bool ivdep)
11144 tree init, scope, decl;
11147 /* Begin the for-statement. */
11148 scope = begin_for_scope (&init);
11150 /* Parse the initialization. */
11151 is_range_for = cp_parser_for_init_statement (parser, &decl);
11154 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11156 return cp_parser_c_for (parser, scope, init, ivdep);
11160 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11162 /* Normal for loop */
11163 tree condition = NULL_TREE;
11164 tree expression = NULL_TREE;
11167 stmt = begin_for_stmt (scope, init);
11168 /* The for-init-statement has already been parsed in
11169 cp_parser_for_init_statement, so no work is needed here. */
11170 finish_for_init_stmt (stmt);
11172 /* If there's a condition, process it. */
11173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11174 condition = cp_parser_condition (parser);
11177 cp_parser_error (parser, "missing loop condition in loop with "
11178 "%<GCC ivdep%> pragma");
11179 condition = error_mark_node;
11181 finish_for_cond (condition, stmt, ivdep);
11182 /* Look for the `;'. */
11183 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11185 /* If there's an expression, process it. */
11186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11187 expression = cp_parser_expression (parser);
11188 finish_for_expr (expression, stmt);
11193 /* Tries to parse a range-based for-statement:
11196 decl-specifier-seq declarator : expression
11198 The decl-specifier-seq declarator and the `:' are already parsed by
11199 cp_parser_for_init_statement. If processing_template_decl it returns a
11200 newly created RANGE_FOR_STMT; if not, it is converted to a
11201 regular FOR_STMT. */
11204 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11207 tree stmt, range_expr;
11208 cxx_binding *binding = NULL;
11209 tree name = NULL_TREE;
11211 /* Get the range declaration momentarily out of the way so that
11212 the range expression doesn't clash with it. */
11213 if (range_decl != error_mark_node)
11215 name = DECL_NAME (range_decl);
11216 binding = IDENTIFIER_BINDING (name);
11217 IDENTIFIER_BINDING (name) = binding->previous;
11220 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11222 bool expr_non_constant_p;
11223 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11226 range_expr = cp_parser_expression (parser);
11228 /* Put the range declaration back into scope. */
11229 if (range_decl != error_mark_node)
11231 binding->previous = IDENTIFIER_BINDING (name);
11232 IDENTIFIER_BINDING (name) = binding;
11235 /* If in template, STMT is converted to a normal for-statement
11236 at instantiation. If not, it is done just ahead. */
11237 if (processing_template_decl)
11239 if (check_for_bare_parameter_packs (range_expr))
11240 range_expr = error_mark_node;
11241 stmt = begin_range_for_stmt (scope, init);
11243 RANGE_FOR_IVDEP (stmt) = 1;
11244 finish_range_for_decl (stmt, range_decl, range_expr);
11245 if (!type_dependent_expression_p (range_expr)
11246 /* do_auto_deduction doesn't mess with template init-lists. */
11247 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11248 do_range_for_auto_deduction (range_decl, range_expr);
11252 stmt = begin_for_stmt (scope, init);
11253 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11258 /* Subroutine of cp_convert_range_for: given the initializer expression,
11259 builds up the range temporary. */
11262 build_range_temp (tree range_expr)
11264 tree range_type, range_temp;
11266 /* Find out the type deduced by the declaration
11267 `auto &&__range = range_expr'. */
11268 range_type = cp_build_reference_type (make_auto (), true);
11269 range_type = do_auto_deduction (range_type, range_expr,
11270 type_uses_auto (range_type));
11272 /* Create the __range variable. */
11273 range_temp = build_decl (input_location, VAR_DECL,
11274 get_identifier ("__for_range"), range_type);
11275 TREE_USED (range_temp) = 1;
11276 DECL_ARTIFICIAL (range_temp) = 1;
11281 /* Used by cp_parser_range_for in template context: we aren't going to
11282 do a full conversion yet, but we still need to resolve auto in the
11283 type of the for-range-declaration if present. This is basically
11284 a shortcut version of cp_convert_range_for. */
11287 do_range_for_auto_deduction (tree decl, tree range_expr)
11289 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11292 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11293 range_temp = convert_from_reference (build_range_temp (range_expr));
11294 iter_type = (cp_parser_perform_range_for_lookup
11295 (range_temp, &begin_dummy, &end_dummy));
11298 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11300 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11301 tf_warning_or_error);
11302 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11303 iter_decl, auto_node);
11308 /* Converts a range-based for-statement into a normal
11309 for-statement, as per the definition.
11311 for (RANGE_DECL : RANGE_EXPR)
11314 should be equivalent to:
11317 auto &&__range = RANGE_EXPR;
11318 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11322 RANGE_DECL = *__begin;
11327 If RANGE_EXPR is an array:
11328 BEGIN_EXPR = __range
11329 END_EXPR = __range + ARRAY_SIZE(__range)
11330 Else if RANGE_EXPR has a member 'begin' or 'end':
11331 BEGIN_EXPR = __range.begin()
11332 END_EXPR = __range.end()
11334 BEGIN_EXPR = begin(__range)
11335 END_EXPR = end(__range);
11337 If __range has a member 'begin' but not 'end', or vice versa, we must
11338 still use the second alternative (it will surely fail, however).
11339 When calling begin()/end() in the third alternative we must use
11340 argument dependent lookup, but always considering 'std' as an associated
11344 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11348 tree iter_type, begin_expr, end_expr;
11349 tree condition, expression;
11351 if (range_decl == error_mark_node || range_expr == error_mark_node)
11352 /* If an error happened previously do nothing or else a lot of
11353 unhelpful errors would be issued. */
11354 begin_expr = end_expr = iter_type = error_mark_node;
11359 if (VAR_P (range_expr)
11360 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11361 /* Can't bind a reference to an array of runtime bound. */
11362 range_temp = range_expr;
11365 range_temp = build_range_temp (range_expr);
11366 pushdecl (range_temp);
11367 cp_finish_decl (range_temp, range_expr,
11368 /*is_constant_init*/false, NULL_TREE,
11369 LOOKUP_ONLYCONVERTING);
11370 range_temp = convert_from_reference (range_temp);
11372 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11373 &begin_expr, &end_expr);
11376 /* The new for initialization statement. */
11377 begin = build_decl (input_location, VAR_DECL,
11378 get_identifier ("__for_begin"), iter_type);
11379 TREE_USED (begin) = 1;
11380 DECL_ARTIFICIAL (begin) = 1;
11382 cp_finish_decl (begin, begin_expr,
11383 /*is_constant_init*/false, NULL_TREE,
11384 LOOKUP_ONLYCONVERTING);
11386 if (cxx_dialect >= cxx1z)
11387 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11388 end = build_decl (input_location, VAR_DECL,
11389 get_identifier ("__for_end"), iter_type);
11390 TREE_USED (end) = 1;
11391 DECL_ARTIFICIAL (end) = 1;
11393 cp_finish_decl (end, end_expr,
11394 /*is_constant_init*/false, NULL_TREE,
11395 LOOKUP_ONLYCONVERTING);
11397 finish_for_init_stmt (statement);
11399 /* The new for condition. */
11400 condition = build_x_binary_op (input_location, NE_EXPR,
11403 NULL, tf_warning_or_error);
11404 finish_for_cond (condition, statement, ivdep);
11406 /* The new increment expression. */
11407 expression = finish_unary_op_expr (input_location,
11408 PREINCREMENT_EXPR, begin,
11409 tf_warning_or_error);
11410 finish_for_expr (expression, statement);
11412 /* The declaration is initialized with *__begin inside the loop body. */
11413 cp_finish_decl (range_decl,
11414 build_x_indirect_ref (input_location, begin, RO_NULL,
11415 tf_warning_or_error),
11416 /*is_constant_init*/false, NULL_TREE,
11417 LOOKUP_ONLYCONVERTING);
11422 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11423 We need to solve both at the same time because the method used
11424 depends on the existence of members begin or end.
11425 Returns the type deduced for the iterator expression. */
11428 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11430 if (error_operand_p (range))
11432 *begin = *end = error_mark_node;
11433 return error_mark_node;
11436 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11438 error ("range-based %<for%> expression of type %qT "
11439 "has incomplete type", TREE_TYPE (range));
11440 *begin = *end = error_mark_node;
11441 return error_mark_node;
11443 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11445 /* If RANGE is an array, we will use pointer arithmetic. */
11447 *end = build_binary_op (input_location, PLUS_EXPR,
11449 array_type_nelts_top (TREE_TYPE (range)),
11451 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
11455 /* If it is not an array, we must do a bit of magic. */
11456 tree id_begin, id_end;
11457 tree member_begin, member_end;
11459 *begin = *end = error_mark_node;
11461 id_begin = get_identifier ("begin");
11462 id_end = get_identifier ("end");
11463 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11464 /*protect=*/2, /*want_type=*/false,
11465 tf_warning_or_error);
11466 member_end = lookup_member (TREE_TYPE (range), id_end,
11467 /*protect=*/2, /*want_type=*/false,
11468 tf_warning_or_error);
11470 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11472 /* Use the member functions. */
11473 if (member_begin != NULL_TREE)
11474 *begin = cp_parser_range_for_member_function (range, id_begin);
11476 error ("range-based %<for%> expression of type %qT has an "
11477 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11479 if (member_end != NULL_TREE)
11480 *end = cp_parser_range_for_member_function (range, id_end);
11482 error ("range-based %<for%> expression of type %qT has a "
11483 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11487 /* Use global functions with ADL. */
11488 vec<tree, va_gc> *vec;
11489 vec = make_tree_vector ();
11491 vec_safe_push (vec, range);
11493 member_begin = perform_koenig_lookup (id_begin, vec,
11494 tf_warning_or_error);
11495 *begin = finish_call_expr (member_begin, &vec, false, true,
11496 tf_warning_or_error);
11497 member_end = perform_koenig_lookup (id_end, vec,
11498 tf_warning_or_error);
11499 *end = finish_call_expr (member_end, &vec, false, true,
11500 tf_warning_or_error);
11502 release_tree_vector (vec);
11505 /* Last common checks. */
11506 if (*begin == error_mark_node || *end == error_mark_node)
11508 /* If one of the expressions is an error do no more checks. */
11509 *begin = *end = error_mark_node;
11510 return error_mark_node;
11512 else if (type_dependent_expression_p (*begin)
11513 || type_dependent_expression_p (*end))
11514 /* Can happen, when, eg, in a template context, Koenig lookup
11515 can't resolve begin/end (c++/58503). */
11519 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11520 /* The unqualified type of the __begin and __end temporaries should
11521 be the same, as required by the multiple auto declaration. */
11522 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11524 if (cxx_dialect >= cxx1z
11525 && (build_x_binary_op (input_location, NE_EXPR,
11526 *begin, ERROR_MARK,
11529 != error_mark_node))
11530 /* P0184R0 allows __begin and __end to have different types,
11531 but make sure they are comparable so we can give a better
11534 error ("inconsistent begin/end types in range-based %<for%> "
11535 "statement: %qT and %qT",
11536 TREE_TYPE (*begin), TREE_TYPE (*end));
11543 /* Helper function for cp_parser_perform_range_for_lookup.
11544 Builds a tree for RANGE.IDENTIFIER(). */
11547 cp_parser_range_for_member_function (tree range, tree identifier)
11550 vec<tree, va_gc> *vec;
11552 member = finish_class_member_access_expr (range, identifier,
11553 false, tf_warning_or_error);
11554 if (member == error_mark_node)
11555 return error_mark_node;
11557 vec = make_tree_vector ();
11558 res = finish_call_expr (member, &vec,
11559 /*disallow_virtual=*/false,
11560 /*koenig_p=*/false,
11561 tf_warning_or_error);
11562 release_tree_vector (vec);
11566 /* Parse an iteration-statement.
11568 iteration-statement:
11569 while ( condition ) statement
11570 do statement while ( expression ) ;
11571 for ( for-init-statement condition [opt] ; expression [opt] )
11574 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11577 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11582 unsigned char in_statement;
11583 token_indent_info guard_tinfo;
11585 /* Peek at the next token. */
11586 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11588 return error_mark_node;
11590 guard_tinfo = get_token_indent_info (token);
11592 /* Remember whether or not we are already within an iteration
11594 in_statement = parser->in_statement;
11596 /* See what kind of keyword it is. */
11597 keyword = token->keyword;
11604 /* Begin the while-statement. */
11605 statement = begin_while_stmt ();
11606 /* Look for the `('. */
11607 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11608 /* Parse the condition. */
11609 condition = cp_parser_condition (parser);
11610 finish_while_stmt_cond (condition, statement, ivdep);
11611 /* Look for the `)'. */
11612 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11613 /* Parse the dependent statement. */
11614 parser->in_statement = IN_ITERATION_STMT;
11615 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11616 parser->in_statement = in_statement;
11617 /* We're done with the while-statement. */
11618 finish_while_stmt (statement);
11626 /* Begin the do-statement. */
11627 statement = begin_do_stmt ();
11628 /* Parse the body of the do-statement. */
11629 parser->in_statement = IN_ITERATION_STMT;
11630 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11631 parser->in_statement = in_statement;
11632 finish_do_body (statement);
11633 /* Look for the `while' keyword. */
11634 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11635 /* Look for the `('. */
11636 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11637 /* Parse the expression. */
11638 expression = cp_parser_expression (parser);
11639 /* We're done with the do-statement. */
11640 finish_do_stmt (expression, statement, ivdep);
11641 /* Look for the `)'. */
11642 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11643 /* Look for the `;'. */
11644 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11650 /* Look for the `('. */
11651 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11653 statement = cp_parser_for (parser, ivdep);
11655 /* Look for the `)'. */
11656 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11658 /* Parse the body of the for-statement. */
11659 parser->in_statement = IN_ITERATION_STMT;
11660 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11661 parser->in_statement = in_statement;
11663 /* We're done with the for-statement. */
11664 finish_for_stmt (statement);
11669 cp_parser_error (parser, "expected iteration-statement");
11670 statement = error_mark_node;
11677 /* Parse a for-init-statement or the declarator of a range-based-for.
11678 Returns true if a range-based-for declaration is seen.
11680 for-init-statement:
11681 expression-statement
11682 simple-declaration */
11685 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11687 /* If the next token is a `;', then we have an empty
11688 expression-statement. Grammatically, this is also a
11689 simple-declaration, but an invalid one, because it does not
11690 declare anything. Therefore, if we did not handle this case
11691 specially, we would issue an error message about an invalid
11693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11695 bool is_range_for = false;
11696 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11698 /* A colon is used in range-based for. */
11699 parser->colon_corrects_to_scope_p = false;
11701 /* We're going to speculatively look for a declaration, falling back
11702 to an expression, if necessary. */
11703 cp_parser_parse_tentatively (parser);
11704 /* Parse the declaration. */
11705 cp_parser_simple_declaration (parser,
11706 /*function_definition_allowed_p=*/false,
11708 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11709 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11711 /* It is a range-for, consume the ':' */
11712 cp_lexer_consume_token (parser->lexer);
11713 is_range_for = true;
11714 if (cxx_dialect < cxx11)
11716 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11717 "range-based %<for%> loops only available with "
11718 "-std=c++11 or -std=gnu++11");
11719 *decl = error_mark_node;
11723 /* The ';' is not consumed yet because we told
11724 cp_parser_simple_declaration not to. */
11725 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11727 if (cp_parser_parse_definitely (parser))
11728 return is_range_for;
11729 /* If the tentative parse failed, then we shall need to look for an
11730 expression-statement. */
11732 /* If we are here, it is an expression-statement. */
11733 cp_parser_expression_statement (parser, NULL_TREE);
11737 /* Parse a jump-statement.
11742 return expression [opt] ;
11743 return braced-init-list ;
11749 goto * expression ;
11751 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11754 cp_parser_jump_statement (cp_parser* parser)
11756 tree statement = error_mark_node;
11759 unsigned char in_statement;
11761 /* Peek at the next token. */
11762 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11764 return error_mark_node;
11766 /* See what kind of keyword it is. */
11767 keyword = token->keyword;
11771 in_statement = parser->in_statement & ~IN_IF_STMT;
11772 switch (in_statement)
11775 error_at (token->location, "break statement not within loop or switch");
11778 gcc_assert ((in_statement & IN_SWITCH_STMT)
11779 || in_statement == IN_ITERATION_STMT);
11780 statement = finish_break_stmt ();
11781 if (in_statement == IN_ITERATION_STMT)
11782 break_maybe_infinite_loop ();
11785 error_at (token->location, "invalid exit from OpenMP structured block");
11788 error_at (token->location, "break statement used with OpenMP for loop");
11790 case IN_CILK_SIMD_FOR:
11791 error_at (token->location, "break statement used with Cilk Plus for loop");
11794 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11798 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11801 error_at (token->location, "continue statement not within a loop");
11803 case IN_CILK_SIMD_FOR:
11804 error_at (token->location,
11805 "continue statement within %<#pragma simd%> loop body");
11806 /* Fall through. */
11807 case IN_ITERATION_STMT:
11809 statement = finish_continue_stmt ();
11812 error_at (token->location, "invalid exit from OpenMP structured block");
11815 gcc_unreachable ();
11817 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11823 bool expr_non_constant_p;
11825 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11827 cp_lexer_set_source_position (parser->lexer);
11828 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11829 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11831 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11832 expr = cp_parser_expression (parser);
11834 /* If the next token is a `;', then there is no
11837 /* Build the return-statement. */
11838 statement = finish_return_stmt (expr);
11839 /* Look for the final `;'. */
11840 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11845 if (parser->in_function_body
11846 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11848 error ("%<goto%> in %<constexpr%> function");
11849 cp_function_chain->invalid_constexpr = true;
11852 /* Create the goto-statement. */
11853 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11855 /* Issue a warning about this use of a GNU extension. */
11856 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11857 /* Consume the '*' token. */
11858 cp_lexer_consume_token (parser->lexer);
11859 /* Parse the dependent expression. */
11860 finish_goto_stmt (cp_parser_expression (parser));
11863 finish_goto_stmt (cp_parser_identifier (parser));
11864 /* Look for the final `;'. */
11865 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11869 cp_parser_error (parser, "expected jump-statement");
11876 /* Parse a declaration-statement.
11878 declaration-statement:
11879 block-declaration */
11882 cp_parser_declaration_statement (cp_parser* parser)
11886 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11887 p = obstack_alloc (&declarator_obstack, 0);
11889 /* Parse the block-declaration. */
11890 cp_parser_block_declaration (parser, /*statement_p=*/true);
11892 /* Free any declarators allocated. */
11893 obstack_free (&declarator_obstack, p);
11896 /* Some dependent statements (like `if (cond) statement'), are
11897 implicitly in their own scope. In other words, if the statement is
11898 a single statement (as opposed to a compound-statement), it is
11899 none-the-less treated as if it were enclosed in braces. Any
11900 declarations appearing in the dependent statement are out of scope
11901 after control passes that point. This function parses a statement,
11902 but ensures that is in its own scope, even if it is not a
11903 compound-statement.
11905 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11906 is a (possibly labeled) if statement which is not enclosed in
11907 braces and has an else clause. This is used to implement
11910 CHAIN is a vector of if-else-if conditions. This is used to implement
11913 Returns the new statement. */
11916 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11917 const token_indent_info &guard_tinfo,
11921 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11922 token_indent_info body_tinfo
11923 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11928 /* Mark if () ; with a special NOP_EXPR. */
11929 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11931 cp_lexer_consume_token (parser->lexer);
11932 statement = add_stmt (build_empty_stmt (body_loc));
11934 if (guard_tinfo.keyword == RID_IF
11935 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11936 warning_at (body_loc, OPT_Wempty_body,
11937 "suggest braces around empty body in an %<if%> statement");
11938 else if (guard_tinfo.keyword == RID_ELSE)
11939 warning_at (body_loc, OPT_Wempty_body,
11940 "suggest braces around empty body in an %<else%> statement");
11942 /* if a compound is opened, we simply parse the statement directly. */
11943 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11944 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11945 /* If the token is not a `{', then we must take special action. */
11948 /* Create a compound-statement. */
11949 statement = begin_compound_stmt (0);
11950 /* Parse the dependent-statement. */
11951 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11952 /* Finish the dummy compound-statement. */
11953 finish_compound_stmt (statement);
11956 token_indent_info next_tinfo
11957 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11958 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11960 /* Return the statement. */
11964 /* For some dependent statements (like `while (cond) statement'), we
11965 have already created a scope. Therefore, even if the dependent
11966 statement is a compound-statement, we do not want to create another
11970 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
11971 const token_indent_info &guard_tinfo)
11973 /* If the token is a `{', then we must take special action. */
11974 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11976 token_indent_info body_tinfo
11977 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11979 cp_parser_statement (parser, NULL_TREE, false, if_p);
11980 token_indent_info next_tinfo
11981 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11982 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11986 /* Avoid calling cp_parser_compound_statement, so that we
11987 don't create a new scope. Do everything else by hand. */
11988 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11989 /* If the next keyword is `__label__' we have a label declaration. */
11990 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11991 cp_parser_label_declaration (parser);
11992 /* Parse an (optional) statement-seq. */
11993 cp_parser_statement_seq_opt (parser, NULL_TREE);
11994 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11998 /* Declarations [gram.dcl.dcl] */
12000 /* Parse an optional declaration-sequence.
12004 declaration-seq declaration */
12007 cp_parser_declaration_seq_opt (cp_parser* parser)
12013 token = cp_lexer_peek_token (parser->lexer);
12015 if (token->type == CPP_CLOSE_BRACE
12016 || token->type == CPP_EOF
12017 || token->type == CPP_PRAGMA_EOL)
12020 if (token->type == CPP_SEMICOLON)
12022 /* A declaration consisting of a single semicolon is
12023 invalid. Allow it unless we're being pedantic. */
12024 cp_lexer_consume_token (parser->lexer);
12025 if (!in_system_header_at (input_location))
12026 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12030 /* If we're entering or exiting a region that's implicitly
12031 extern "C", modify the lang context appropriately. */
12032 if (!parser->implicit_extern_c && token->implicit_extern_c)
12034 push_lang_context (lang_name_c);
12035 parser->implicit_extern_c = true;
12037 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12039 pop_lang_context ();
12040 parser->implicit_extern_c = false;
12043 if (token->type == CPP_PRAGMA)
12045 /* A top-level declaration can consist solely of a #pragma.
12046 A nested declaration cannot, so this is done here and not
12047 in cp_parser_declaration. (A #pragma at block scope is
12048 handled in cp_parser_statement.) */
12049 cp_parser_pragma (parser, pragma_external, NULL);
12053 /* Parse the declaration itself. */
12054 cp_parser_declaration (parser);
12058 /* Parse a declaration.
12062 function-definition
12063 template-declaration
12064 explicit-instantiation
12065 explicit-specialization
12066 linkage-specification
12067 namespace-definition
12072 __extension__ declaration */
12075 cp_parser_declaration (cp_parser* parser)
12079 int saved_pedantic;
12081 tree attributes = NULL_TREE;
12083 /* Check for the `__extension__' keyword. */
12084 if (cp_parser_extension_opt (parser, &saved_pedantic))
12086 /* Parse the qualified declaration. */
12087 cp_parser_declaration (parser);
12088 /* Restore the PEDANTIC flag. */
12089 pedantic = saved_pedantic;
12094 /* Try to figure out what kind of declaration is present. */
12095 token1 = *cp_lexer_peek_token (parser->lexer);
12097 if (token1.type != CPP_EOF)
12098 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12101 token2.type = CPP_EOF;
12102 token2.keyword = RID_MAX;
12105 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12106 p = obstack_alloc (&declarator_obstack, 0);
12108 /* If the next token is `extern' and the following token is a string
12109 literal, then we have a linkage specification. */
12110 if (token1.keyword == RID_EXTERN
12111 && cp_parser_is_pure_string_literal (&token2))
12112 cp_parser_linkage_specification (parser);
12113 /* If the next token is `template', then we have either a template
12114 declaration, an explicit instantiation, or an explicit
12116 else if (token1.keyword == RID_TEMPLATE)
12118 /* `template <>' indicates a template specialization. */
12119 if (token2.type == CPP_LESS
12120 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12121 cp_parser_explicit_specialization (parser);
12122 /* `template <' indicates a template declaration. */
12123 else if (token2.type == CPP_LESS)
12124 cp_parser_template_declaration (parser, /*member_p=*/false);
12125 /* Anything else must be an explicit instantiation. */
12127 cp_parser_explicit_instantiation (parser);
12129 /* If the next token is `export', then we have a template
12131 else if (token1.keyword == RID_EXPORT)
12132 cp_parser_template_declaration (parser, /*member_p=*/false);
12133 /* If the next token is `extern', 'static' or 'inline' and the one
12134 after that is `template', we have a GNU extended explicit
12135 instantiation directive. */
12136 else if (cp_parser_allow_gnu_extensions_p (parser)
12137 && (token1.keyword == RID_EXTERN
12138 || token1.keyword == RID_STATIC
12139 || token1.keyword == RID_INLINE)
12140 && token2.keyword == RID_TEMPLATE)
12141 cp_parser_explicit_instantiation (parser);
12142 /* If the next token is `namespace', check for a named or unnamed
12143 namespace definition. */
12144 else if (token1.keyword == RID_NAMESPACE
12145 && (/* A named namespace definition. */
12146 (token2.type == CPP_NAME
12147 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12149 || (token2.type == CPP_OPEN_SQUARE
12150 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12151 == CPP_OPEN_SQUARE)
12152 /* An unnamed namespace definition. */
12153 || token2.type == CPP_OPEN_BRACE
12154 || token2.keyword == RID_ATTRIBUTE))
12155 cp_parser_namespace_definition (parser);
12156 /* An inline (associated) namespace definition. */
12157 else if (token1.keyword == RID_INLINE
12158 && token2.keyword == RID_NAMESPACE)
12159 cp_parser_namespace_definition (parser);
12160 /* Objective-C++ declaration/definition. */
12161 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12162 cp_parser_objc_declaration (parser, NULL_TREE);
12163 else if (c_dialect_objc ()
12164 && token1.keyword == RID_ATTRIBUTE
12165 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12166 cp_parser_objc_declaration (parser, attributes);
12167 /* At this point we may have a template declared by a concept
12169 else if (flag_concepts
12170 && cp_parser_template_declaration_after_export (parser,
12171 /*member_p=*/false))
12174 /* Try to parse a block-declaration, or a function-definition. */
12175 cp_parser_block_declaration (parser, /*statement_p=*/false);
12177 /* Free any declarators allocated. */
12178 obstack_free (&declarator_obstack, p);
12181 /* Parse a block-declaration.
12186 namespace-alias-definition
12193 __extension__ block-declaration
12198 static_assert-declaration
12200 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12201 part of a declaration-statement. */
12204 cp_parser_block_declaration (cp_parser *parser,
12208 int saved_pedantic;
12210 /* Check for the `__extension__' keyword. */
12211 if (cp_parser_extension_opt (parser, &saved_pedantic))
12213 /* Parse the qualified declaration. */
12214 cp_parser_block_declaration (parser, statement_p);
12215 /* Restore the PEDANTIC flag. */
12216 pedantic = saved_pedantic;
12221 /* Peek at the next token to figure out which kind of declaration is
12223 token1 = cp_lexer_peek_token (parser->lexer);
12225 /* If the next keyword is `asm', we have an asm-definition. */
12226 if (token1->keyword == RID_ASM)
12229 cp_parser_commit_to_tentative_parse (parser);
12230 cp_parser_asm_definition (parser);
12232 /* If the next keyword is `namespace', we have a
12233 namespace-alias-definition. */
12234 else if (token1->keyword == RID_NAMESPACE)
12235 cp_parser_namespace_alias_definition (parser);
12236 /* If the next keyword is `using', we have a
12237 using-declaration, a using-directive, or an alias-declaration. */
12238 else if (token1->keyword == RID_USING)
12243 cp_parser_commit_to_tentative_parse (parser);
12244 /* If the token after `using' is `namespace', then we have a
12245 using-directive. */
12246 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12247 if (token2->keyword == RID_NAMESPACE)
12248 cp_parser_using_directive (parser);
12249 /* If the second token after 'using' is '=', then we have an
12250 alias-declaration. */
12251 else if (cxx_dialect >= cxx11
12252 && token2->type == CPP_NAME
12253 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12254 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12255 cp_parser_alias_declaration (parser);
12256 /* Otherwise, it's a using-declaration. */
12258 cp_parser_using_declaration (parser,
12259 /*access_declaration_p=*/false);
12261 /* If the next keyword is `__label__' we have a misplaced label
12263 else if (token1->keyword == RID_LABEL)
12265 cp_lexer_consume_token (parser->lexer);
12266 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12267 cp_parser_skip_to_end_of_statement (parser);
12268 /* If the next token is now a `;', consume it. */
12269 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12270 cp_lexer_consume_token (parser->lexer);
12272 /* If the next token is `static_assert' we have a static assertion. */
12273 else if (token1->keyword == RID_STATIC_ASSERT)
12274 cp_parser_static_assert (parser, /*member_p=*/false);
12275 /* Anything else must be a simple-declaration. */
12277 cp_parser_simple_declaration (parser, !statement_p,
12278 /*maybe_range_for_decl*/NULL);
12281 /* Parse a simple-declaration.
12283 simple-declaration:
12284 decl-specifier-seq [opt] init-declarator-list [opt] ;
12286 init-declarator-list:
12288 init-declarator-list , init-declarator
12290 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12291 function-definition as a simple-declaration.
12293 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12294 parsed declaration if it is an uninitialized single declarator not followed
12295 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12296 if present, will not be consumed. */
12299 cp_parser_simple_declaration (cp_parser* parser,
12300 bool function_definition_allowed_p,
12301 tree *maybe_range_for_decl)
12303 cp_decl_specifier_seq decl_specifiers;
12304 int declares_class_or_enum;
12305 bool saw_declarator;
12306 location_t comma_loc = UNKNOWN_LOCATION;
12307 location_t init_loc = UNKNOWN_LOCATION;
12309 if (maybe_range_for_decl)
12310 *maybe_range_for_decl = NULL_TREE;
12312 /* Defer access checks until we know what is being declared; the
12313 checks for names appearing in the decl-specifier-seq should be
12314 done as if we were in the scope of the thing being declared. */
12315 push_deferring_access_checks (dk_deferred);
12317 /* Parse the decl-specifier-seq. We have to keep track of whether
12318 or not the decl-specifier-seq declares a named class or
12319 enumeration type, since that is the only case in which the
12320 init-declarator-list is allowed to be empty.
12324 In a simple-declaration, the optional init-declarator-list can be
12325 omitted only when declaring a class or enumeration, that is when
12326 the decl-specifier-seq contains either a class-specifier, an
12327 elaborated-type-specifier, or an enum-specifier. */
12328 cp_parser_decl_specifier_seq (parser,
12329 CP_PARSER_FLAGS_OPTIONAL,
12331 &declares_class_or_enum);
12332 /* We no longer need to defer access checks. */
12333 stop_deferring_access_checks ();
12335 /* In a block scope, a valid declaration must always have a
12336 decl-specifier-seq. By not trying to parse declarators, we can
12337 resolve the declaration/expression ambiguity more quickly. */
12338 if (!function_definition_allowed_p
12339 && !decl_specifiers.any_specifiers_p)
12341 cp_parser_error (parser, "expected declaration");
12345 /* If the next two tokens are both identifiers, the code is
12346 erroneous. The usual cause of this situation is code like:
12350 where "T" should name a type -- but does not. */
12351 if (!decl_specifiers.any_type_specifiers_p
12352 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12354 /* If parsing tentatively, we should commit; we really are
12355 looking at a declaration. */
12356 cp_parser_commit_to_tentative_parse (parser);
12361 /* If we have seen at least one decl-specifier, and the next token
12362 is not a parenthesis, then we must be looking at a declaration.
12363 (After "int (" we might be looking at a functional cast.) */
12364 if (decl_specifiers.any_specifiers_p
12365 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12366 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12367 && !cp_parser_error_occurred (parser))
12368 cp_parser_commit_to_tentative_parse (parser);
12372 last_type = NULL_TREE;
12374 /* Keep going until we hit the `;' at the end of the simple
12376 saw_declarator = false;
12377 while (cp_lexer_next_token_is_not (parser->lexer,
12381 bool function_definition_p;
12383 tree auto_result = NULL_TREE;
12385 if (saw_declarator)
12387 /* If we are processing next declarator, comma is expected */
12388 token = cp_lexer_peek_token (parser->lexer);
12389 gcc_assert (token->type == CPP_COMMA);
12390 cp_lexer_consume_token (parser->lexer);
12391 if (maybe_range_for_decl)
12393 *maybe_range_for_decl = error_mark_node;
12394 if (comma_loc == UNKNOWN_LOCATION)
12395 comma_loc = token->location;
12399 saw_declarator = true;
12401 /* Parse the init-declarator. */
12402 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12404 function_definition_allowed_p,
12405 /*member_p=*/false,
12406 declares_class_or_enum,
12407 &function_definition_p,
12408 maybe_range_for_decl,
12411 /* If an error occurred while parsing tentatively, exit quickly.
12412 (That usually happens when in the body of a function; each
12413 statement is treated as a declaration-statement until proven
12415 if (cp_parser_error_occurred (parser))
12419 && (!processing_template_decl || !type_uses_auto (auto_result)))
12422 && last_type != error_mark_node
12423 && !same_type_p (auto_result, last_type))
12425 /* If the list of declarators contains more than one declarator,
12426 the type of each declared variable is determined as described
12427 above. If the type deduced for the template parameter U is not
12428 the same in each deduction, the program is ill-formed. */
12429 error_at (decl_specifiers.locations[ds_type_spec],
12430 "inconsistent deduction for %qT: %qT and then %qT",
12431 decl_specifiers.type, last_type, auto_result);
12432 last_type = error_mark_node;
12435 last_type = auto_result;
12438 /* Handle function definitions specially. */
12439 if (function_definition_p)
12441 /* If the next token is a `,', then we are probably
12442 processing something like:
12446 which is erroneous. */
12447 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12449 cp_token *token = cp_lexer_peek_token (parser->lexer);
12450 error_at (token->location,
12452 " declarations and function-definitions is forbidden");
12454 /* Otherwise, we're done with the list of declarators. */
12457 pop_deferring_access_checks ();
12461 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12462 *maybe_range_for_decl = decl;
12463 /* The next token should be either a `,' or a `;'. */
12464 token = cp_lexer_peek_token (parser->lexer);
12465 /* If it's a `,', there are more declarators to come. */
12466 if (token->type == CPP_COMMA)
12467 /* will be consumed next time around */;
12468 /* If it's a `;', we are done. */
12469 else if (token->type == CPP_SEMICOLON)
12471 else if (maybe_range_for_decl)
12473 if (declares_class_or_enum && token->type == CPP_COLON)
12474 pedwarn (decl_specifiers.locations[ds_type_spec], 0,
12475 "types may not be defined in a for-range-declaration");
12478 /* Anything else is an error. */
12481 /* If we have already issued an error message we don't need
12482 to issue another one. */
12483 if ((decl != error_mark_node
12484 && DECL_INITIAL (decl) != error_mark_node)
12485 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12486 cp_parser_error (parser, "expected %<,%> or %<;%>");
12487 /* Skip tokens until we reach the end of the statement. */
12488 cp_parser_skip_to_end_of_statement (parser);
12489 /* If the next token is now a `;', consume it. */
12490 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12491 cp_lexer_consume_token (parser->lexer);
12494 /* After the first time around, a function-definition is not
12495 allowed -- even if it was OK at first. For example:
12500 function_definition_allowed_p = false;
12503 /* Issue an error message if no declarators are present, and the
12504 decl-specifier-seq does not itself declare a class or
12505 enumeration: [dcl.dcl]/3. */
12506 if (!saw_declarator)
12508 if (cp_parser_declares_only_class_p (parser))
12510 if (!declares_class_or_enum
12511 && decl_specifiers.type
12512 && OVERLOAD_TYPE_P (decl_specifiers.type))
12513 /* Ensure an error is issued anyway when finish_decltype_type,
12514 called via cp_parser_decl_specifier_seq, returns a class or
12515 an enumeration (c++/51786). */
12516 decl_specifiers.type = NULL_TREE;
12517 shadow_tag (&decl_specifiers);
12519 /* Perform any deferred access checks. */
12520 perform_deferred_access_checks (tf_warning_or_error);
12523 /* Consume the `;'. */
12524 if (!maybe_range_for_decl)
12525 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12526 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12528 if (init_loc != UNKNOWN_LOCATION)
12529 error_at (init_loc, "initializer in range-based %<for%> loop");
12530 if (comma_loc != UNKNOWN_LOCATION)
12531 error_at (comma_loc,
12532 "multiple declarations in range-based %<for%> loop");
12536 pop_deferring_access_checks ();
12539 /* Parse a decl-specifier-seq.
12541 decl-specifier-seq:
12542 decl-specifier-seq [opt] decl-specifier
12543 decl-specifier attribute-specifier-seq [opt] (C++11)
12546 storage-class-specifier
12557 Concepts Extension:
12562 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12564 The parser flags FLAGS is used to control type-specifier parsing.
12566 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12569 1: one of the decl-specifiers is an elaborated-type-specifier
12570 (i.e., a type declaration)
12571 2: one of the decl-specifiers is an enum-specifier or a
12572 class-specifier (i.e., a type definition)
12577 cp_parser_decl_specifier_seq (cp_parser* parser,
12578 cp_parser_flags flags,
12579 cp_decl_specifier_seq *decl_specs,
12580 int* declares_class_or_enum)
12582 bool constructor_possible_p = !parser->in_declarator_p;
12583 bool found_decl_spec = false;
12584 cp_token *start_token = NULL;
12587 /* Clear DECL_SPECS. */
12588 clear_decl_specs (decl_specs);
12590 /* Assume no class or enumeration type is declared. */
12591 *declares_class_or_enum = 0;
12593 /* Keep reading specifiers until there are no more to read. */
12596 bool constructor_p;
12600 /* Peek at the next token. */
12601 token = cp_lexer_peek_token (parser->lexer);
12603 /* Save the first token of the decl spec list for error
12606 start_token = token;
12607 /* Handle attributes. */
12608 if (cp_next_tokens_can_be_attribute_p (parser))
12610 /* Parse the attributes. */
12611 tree attrs = cp_parser_attributes_opt (parser);
12613 /* In a sequence of declaration specifiers, c++11 attributes
12614 appertain to the type that precede them. In that case
12617 The attribute-specifier-seq affects the type only for
12618 the declaration it appears in, not other declarations
12619 involving the same type.
12621 But for now let's force the user to position the
12622 attribute either at the beginning of the declaration or
12623 after the declarator-id, which would clearly mean that it
12624 applies to the declarator. */
12625 if (cxx11_attribute_p (attrs))
12627 if (!found_decl_spec)
12628 /* The c++11 attribute is at the beginning of the
12629 declaration. It appertains to the entity being
12633 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12635 /* This is an attribute following a
12636 class-specifier. */
12637 if (decl_specs->type_definition_p)
12638 warn_misplaced_attr_for_class_type (token->location,
12644 decl_specs->std_attributes
12645 = chainon (decl_specs->std_attributes,
12647 if (decl_specs->locations[ds_std_attribute] == 0)
12648 decl_specs->locations[ds_std_attribute] = token->location;
12654 decl_specs->attributes
12655 = chainon (decl_specs->attributes,
12657 if (decl_specs->locations[ds_attribute] == 0)
12658 decl_specs->locations[ds_attribute] = token->location;
12661 /* Assume we will find a decl-specifier keyword. */
12662 found_decl_spec = true;
12663 /* If the next token is an appropriate keyword, we can simply
12664 add it to the list. */
12665 switch (token->keyword)
12671 if (!at_class_scope_p ())
12673 error_at (token->location, "%<friend%> used outside of class");
12674 cp_lexer_purge_token (parser->lexer);
12679 /* Consume the token. */
12680 cp_lexer_consume_token (parser->lexer);
12684 case RID_CONSTEXPR:
12686 cp_lexer_consume_token (parser->lexer);
12691 cp_lexer_consume_token (parser->lexer);
12694 /* function-specifier:
12701 cp_parser_function_specifier_opt (parser, decl_specs);
12708 /* Consume the token. */
12709 cp_lexer_consume_token (parser->lexer);
12710 /* A constructor declarator cannot appear in a typedef. */
12711 constructor_possible_p = false;
12712 /* The "typedef" keyword can only occur in a declaration; we
12713 may as well commit at this point. */
12714 cp_parser_commit_to_tentative_parse (parser);
12716 if (decl_specs->storage_class != sc_none)
12717 decl_specs->conflicting_specifiers_p = true;
12720 /* storage-class-specifier:
12730 if (cxx_dialect == cxx98)
12732 /* Consume the token. */
12733 cp_lexer_consume_token (parser->lexer);
12735 /* Complain about `auto' as a storage specifier, if
12736 we're complaining about C++0x compatibility. */
12737 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12738 " changes meaning in C++11; please remove it");
12740 /* Set the storage class anyway. */
12741 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12745 /* C++0x auto type-specifier. */
12746 found_decl_spec = false;
12753 /* Consume the token. */
12754 cp_lexer_consume_token (parser->lexer);
12755 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12759 /* Consume the token. */
12761 cp_lexer_consume_token (parser->lexer);
12765 /* We did not yet find a decl-specifier yet. */
12766 found_decl_spec = false;
12770 if (found_decl_spec
12771 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12772 && token->keyword != RID_CONSTEXPR)
12773 error ("decl-specifier invalid in condition");
12776 set_and_check_decl_spec_loc (decl_specs, ds, token);
12778 /* Constructors are a special case. The `S' in `S()' is not a
12779 decl-specifier; it is the beginning of the declarator. */
12781 = (!found_decl_spec
12782 && constructor_possible_p
12783 && (cp_parser_constructor_declarator_p
12784 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12786 /* If we don't have a DECL_SPEC yet, then we must be looking at
12787 a type-specifier. */
12788 if (!found_decl_spec && !constructor_p)
12790 int decl_spec_declares_class_or_enum;
12791 bool is_cv_qualifier;
12795 = cp_parser_type_specifier (parser, flags,
12797 /*is_declaration=*/true,
12798 &decl_spec_declares_class_or_enum,
12800 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12802 /* If this type-specifier referenced a user-defined type
12803 (a typedef, class-name, etc.), then we can't allow any
12804 more such type-specifiers henceforth.
12808 The longest sequence of decl-specifiers that could
12809 possibly be a type name is taken as the
12810 decl-specifier-seq of a declaration. The sequence shall
12811 be self-consistent as described below.
12815 As a general rule, at most one type-specifier is allowed
12816 in the complete decl-specifier-seq of a declaration. The
12817 only exceptions are the following:
12819 -- const or volatile can be combined with any other
12822 -- signed or unsigned can be combined with char, long,
12830 void g (const int Pc);
12832 Here, Pc is *not* part of the decl-specifier seq; it's
12833 the declarator. Therefore, once we see a type-specifier
12834 (other than a cv-qualifier), we forbid any additional
12835 user-defined types. We *do* still allow things like `int
12836 int' to be considered a decl-specifier-seq, and issue the
12837 error message later. */
12838 if (type_spec && !is_cv_qualifier)
12839 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12840 /* A constructor declarator cannot follow a type-specifier. */
12843 constructor_possible_p = false;
12844 found_decl_spec = true;
12845 if (!is_cv_qualifier)
12846 decl_specs->any_type_specifiers_p = true;
12850 /* If we still do not have a DECL_SPEC, then there are no more
12851 decl-specifiers. */
12852 if (!found_decl_spec)
12855 decl_specs->any_specifiers_p = true;
12856 /* After we see one decl-specifier, further decl-specifiers are
12857 always optional. */
12858 flags |= CP_PARSER_FLAGS_OPTIONAL;
12861 /* Don't allow a friend specifier with a class definition. */
12862 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12863 && (*declares_class_or_enum & 2))
12864 error_at (decl_specs->locations[ds_friend],
12865 "class definition may not be declared a friend");
12868 /* Parse an (optional) storage-class-specifier.
12870 storage-class-specifier:
12879 storage-class-specifier:
12882 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12885 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12887 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12890 if (cxx_dialect != cxx98)
12892 /* Fall through for C++98. */
12899 /* Consume the token. */
12900 return cp_lexer_consume_token (parser->lexer)->u.value;
12907 /* Parse an (optional) function-specifier.
12909 function-specifier:
12914 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12915 Updates DECL_SPECS, if it is non-NULL. */
12918 cp_parser_function_specifier_opt (cp_parser* parser,
12919 cp_decl_specifier_seq *decl_specs)
12921 cp_token *token = cp_lexer_peek_token (parser->lexer);
12922 switch (token->keyword)
12925 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12929 /* 14.5.2.3 [temp.mem]
12931 A member function template shall not be virtual. */
12932 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12933 error_at (token->location, "templates may not be %<virtual%>");
12935 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12939 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12946 /* Consume the token. */
12947 return cp_lexer_consume_token (parser->lexer)->u.value;
12950 /* Parse a linkage-specification.
12952 linkage-specification:
12953 extern string-literal { declaration-seq [opt] }
12954 extern string-literal declaration */
12957 cp_parser_linkage_specification (cp_parser* parser)
12961 /* Look for the `extern' keyword. */
12962 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12964 /* Look for the string-literal. */
12965 linkage = cp_parser_string_literal (parser, false, false);
12967 /* Transform the literal into an identifier. If the literal is a
12968 wide-character string, or contains embedded NULs, then we can't
12969 handle it as the user wants. */
12970 if (strlen (TREE_STRING_POINTER (linkage))
12971 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12973 cp_parser_error (parser, "invalid linkage-specification");
12974 /* Assume C++ linkage. */
12975 linkage = lang_name_cplusplus;
12978 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12980 /* We're now using the new linkage. */
12981 push_lang_context (linkage);
12983 /* If the next token is a `{', then we're using the first
12985 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12987 cp_ensure_no_omp_declare_simd (parser);
12988 cp_ensure_no_oacc_routine (parser);
12990 /* Consume the `{' token. */
12991 cp_lexer_consume_token (parser->lexer);
12992 /* Parse the declarations. */
12993 cp_parser_declaration_seq_opt (parser);
12994 /* Look for the closing `}'. */
12995 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12997 /* Otherwise, there's just one declaration. */
13000 bool saved_in_unbraced_linkage_specification_p;
13002 saved_in_unbraced_linkage_specification_p
13003 = parser->in_unbraced_linkage_specification_p;
13004 parser->in_unbraced_linkage_specification_p = true;
13005 cp_parser_declaration (parser);
13006 parser->in_unbraced_linkage_specification_p
13007 = saved_in_unbraced_linkage_specification_p;
13010 /* We're done with the linkage-specification. */
13011 pop_lang_context ();
13014 /* Parse a static_assert-declaration.
13016 static_assert-declaration:
13017 static_assert ( constant-expression , string-literal ) ;
13018 static_assert ( constant-expression ) ; (C++1Z)
13020 If MEMBER_P, this static_assert is a class member. */
13023 cp_parser_static_assert(cp_parser *parser, bool member_p)
13028 location_t saved_loc;
13031 /* Peek at the `static_assert' token so we can keep track of exactly
13032 where the static assertion started. */
13033 token = cp_lexer_peek_token (parser->lexer);
13034 saved_loc = token->location;
13036 /* Look for the `static_assert' keyword. */
13037 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13041 /* We know we are in a static assertion; commit to any tentative
13043 if (cp_parser_parsing_tentatively (parser))
13044 cp_parser_commit_to_tentative_parse (parser);
13046 /* Parse the `(' starting the static assertion condition. */
13047 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13049 /* Parse the constant-expression. Allow a non-constant expression
13050 here in order to give better diagnostics in finish_static_assert. */
13052 cp_parser_constant_expression (parser,
13053 /*allow_non_constant_p=*/true,
13054 /*non_constant_p=*/&dummy);
13056 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13058 if (cxx_dialect < cxx1z)
13059 pedwarn (input_location, OPT_Wpedantic,
13060 "static_assert without a message "
13061 "only available with -std=c++1z or -std=gnu++1z");
13063 cp_lexer_consume_token (parser->lexer);
13064 message = build_string (1, "");
13065 TREE_TYPE (message) = char_array_type_node;
13066 fix_string_type (message);
13070 /* Parse the separating `,'. */
13071 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13073 /* Parse the string-literal message. */
13074 message = cp_parser_string_literal (parser,
13075 /*translate=*/false,
13078 /* A `)' completes the static assertion. */
13079 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13080 cp_parser_skip_to_closing_parenthesis (parser,
13081 /*recovering=*/true,
13082 /*or_comma=*/false,
13083 /*consume_paren=*/true);
13086 /* A semicolon terminates the declaration. */
13087 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13089 /* Complete the static assertion, which may mean either processing
13090 the static assert now or saving it for template instantiation. */
13091 finish_static_assert (condition, message, saved_loc, member_p);
13094 /* Parse the expression in decltype ( expression ). */
13097 cp_parser_decltype_expr (cp_parser *parser,
13098 bool &id_expression_or_member_access_p)
13100 cp_token *id_expr_start_token;
13103 /* Since we're going to preserve any side-effects from this parse, set up a
13104 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13105 in the expression. */
13106 tentative_firewall firewall (parser);
13108 /* First, try parsing an id-expression. */
13109 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13110 cp_parser_parse_tentatively (parser);
13111 expr = cp_parser_id_expression (parser,
13112 /*template_keyword_p=*/false,
13113 /*check_dependency_p=*/true,
13114 /*template_p=*/NULL,
13115 /*declarator_p=*/false,
13116 /*optional_p=*/false);
13118 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13120 bool non_integral_constant_expression_p = false;
13121 tree id_expression = expr;
13123 const char *error_msg;
13125 if (identifier_p (expr))
13126 /* Lookup the name we got back from the id-expression. */
13127 expr = cp_parser_lookup_name_simple (parser, expr,
13128 id_expr_start_token->location);
13131 && expr != error_mark_node
13132 && TREE_CODE (expr) != TYPE_DECL
13133 && (TREE_CODE (expr) != BIT_NOT_EXPR
13134 || !TYPE_P (TREE_OPERAND (expr, 0)))
13135 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13137 /* Complete lookup of the id-expression. */
13138 expr = (finish_id_expression
13139 (id_expression, expr, parser->scope, &idk,
13140 /*integral_constant_expression_p=*/false,
13141 /*allow_non_integral_constant_expression_p=*/true,
13142 &non_integral_constant_expression_p,
13143 /*template_p=*/false,
13145 /*address_p=*/false,
13146 /*template_arg_p=*/false,
13148 id_expr_start_token->location));
13150 if (expr == error_mark_node)
13151 /* We found an id-expression, but it was something that we
13152 should not have found. This is an error, not something
13153 we can recover from, so note that we found an
13154 id-expression and we'll recover as gracefully as
13156 id_expression_or_member_access_p = true;
13160 && expr != error_mark_node
13161 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13162 /* We have an id-expression. */
13163 id_expression_or_member_access_p = true;
13166 if (!id_expression_or_member_access_p)
13168 /* Abort the id-expression parse. */
13169 cp_parser_abort_tentative_parse (parser);
13171 /* Parsing tentatively, again. */
13172 cp_parser_parse_tentatively (parser);
13174 /* Parse a class member access. */
13175 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13176 /*cast_p=*/false, /*decltype*/true,
13177 /*member_access_only_p=*/true, NULL);
13180 && expr != error_mark_node
13181 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13182 /* We have an id-expression. */
13183 id_expression_or_member_access_p = true;
13186 if (id_expression_or_member_access_p)
13187 /* We have parsed the complete id-expression or member access. */
13188 cp_parser_parse_definitely (parser);
13191 /* Abort our attempt to parse an id-expression or member access
13193 cp_parser_abort_tentative_parse (parser);
13195 /* Parse a full expression. */
13196 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13197 /*decltype_p=*/true);
13203 /* Parse a `decltype' type. Returns the type.
13205 simple-type-specifier:
13206 decltype ( expression )
13208 decltype ( auto ) */
13211 cp_parser_decltype (cp_parser *parser)
13214 bool id_expression_or_member_access_p = false;
13215 const char *saved_message;
13216 bool saved_integral_constant_expression_p;
13217 bool saved_non_integral_constant_expression_p;
13218 bool saved_greater_than_is_operator_p;
13219 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13221 if (start_token->type == CPP_DECLTYPE)
13223 /* Already parsed. */
13224 cp_lexer_consume_token (parser->lexer);
13225 return saved_checks_value (start_token->u.tree_check_value);
13228 /* Look for the `decltype' token. */
13229 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13230 return error_mark_node;
13232 /* Parse the opening `('. */
13233 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13234 return error_mark_node;
13236 /* decltype (auto) */
13237 if (cxx_dialect >= cxx14
13238 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13240 cp_lexer_consume_token (parser->lexer);
13241 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13242 return error_mark_node;
13243 expr = make_decltype_auto ();
13244 AUTO_IS_DECLTYPE (expr) = true;
13248 /* Types cannot be defined in a `decltype' expression. Save away the
13250 saved_message = parser->type_definition_forbidden_message;
13252 /* And create the new one. */
13253 parser->type_definition_forbidden_message
13254 = G_("types may not be defined in %<decltype%> expressions");
13256 /* The restrictions on constant-expressions do not apply inside
13257 decltype expressions. */
13258 saved_integral_constant_expression_p
13259 = parser->integral_constant_expression_p;
13260 saved_non_integral_constant_expression_p
13261 = parser->non_integral_constant_expression_p;
13262 parser->integral_constant_expression_p = false;
13264 /* Within a parenthesized expression, a `>' token is always
13265 the greater-than operator. */
13266 saved_greater_than_is_operator_p
13267 = parser->greater_than_is_operator_p;
13268 parser->greater_than_is_operator_p = true;
13270 /* Do not actually evaluate the expression. */
13271 ++cp_unevaluated_operand;
13273 /* Do not warn about problems with the expression. */
13274 ++c_inhibit_evaluation_warnings;
13276 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13278 /* Go back to evaluating expressions. */
13279 --cp_unevaluated_operand;
13280 --c_inhibit_evaluation_warnings;
13282 /* The `>' token might be the end of a template-id or
13283 template-parameter-list now. */
13284 parser->greater_than_is_operator_p
13285 = saved_greater_than_is_operator_p;
13287 /* Restore the old message and the integral constant expression
13289 parser->type_definition_forbidden_message = saved_message;
13290 parser->integral_constant_expression_p
13291 = saved_integral_constant_expression_p;
13292 parser->non_integral_constant_expression_p
13293 = saved_non_integral_constant_expression_p;
13295 /* Parse to the closing `)'. */
13296 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13298 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13299 /*consume_paren=*/true);
13300 return error_mark_node;
13303 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13304 tf_warning_or_error);
13307 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13309 start_token->type = CPP_DECLTYPE;
13310 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13311 start_token->u.tree_check_value->value = expr;
13312 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13313 start_token->keyword = RID_MAX;
13314 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13319 /* Special member functions [gram.special] */
13321 /* Parse a conversion-function-id.
13323 conversion-function-id:
13324 operator conversion-type-id
13326 Returns an IDENTIFIER_NODE representing the operator. */
13329 cp_parser_conversion_function_id (cp_parser* parser)
13333 tree saved_qualifying_scope;
13334 tree saved_object_scope;
13335 tree pushed_scope = NULL_TREE;
13337 /* Look for the `operator' token. */
13338 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13339 return error_mark_node;
13340 /* When we parse the conversion-type-id, the current scope will be
13341 reset. However, we need that information in able to look up the
13342 conversion function later, so we save it here. */
13343 saved_scope = parser->scope;
13344 saved_qualifying_scope = parser->qualifying_scope;
13345 saved_object_scope = parser->object_scope;
13346 /* We must enter the scope of the class so that the names of
13347 entities declared within the class are available in the
13348 conversion-type-id. For example, consider:
13355 S::operator I() { ... }
13357 In order to see that `I' is a type-name in the definition, we
13358 must be in the scope of `S'. */
13360 pushed_scope = push_scope (saved_scope);
13361 /* Parse the conversion-type-id. */
13362 type = cp_parser_conversion_type_id (parser);
13363 /* Leave the scope of the class, if any. */
13365 pop_scope (pushed_scope);
13366 /* Restore the saved scope. */
13367 parser->scope = saved_scope;
13368 parser->qualifying_scope = saved_qualifying_scope;
13369 parser->object_scope = saved_object_scope;
13370 /* If the TYPE is invalid, indicate failure. */
13371 if (type == error_mark_node)
13372 return error_mark_node;
13373 return mangle_conv_op_name_for_type (type);
13376 /* Parse a conversion-type-id:
13378 conversion-type-id:
13379 type-specifier-seq conversion-declarator [opt]
13381 Returns the TYPE specified. */
13384 cp_parser_conversion_type_id (cp_parser* parser)
13387 cp_decl_specifier_seq type_specifiers;
13388 cp_declarator *declarator;
13389 tree type_specified;
13390 const char *saved_message;
13392 /* Parse the attributes. */
13393 attributes = cp_parser_attributes_opt (parser);
13395 saved_message = parser->type_definition_forbidden_message;
13396 parser->type_definition_forbidden_message
13397 = G_("types may not be defined in a conversion-type-id");
13399 /* Parse the type-specifiers. */
13400 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13401 /*is_trailing_return=*/false,
13404 parser->type_definition_forbidden_message = saved_message;
13406 /* If that didn't work, stop. */
13407 if (type_specifiers.type == error_mark_node)
13408 return error_mark_node;
13409 /* Parse the conversion-declarator. */
13410 declarator = cp_parser_conversion_declarator_opt (parser);
13412 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13413 /*initialized=*/0, &attributes);
13415 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13417 /* Don't give this error when parsing tentatively. This happens to
13418 work because we always parse this definitively once. */
13419 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13420 && type_uses_auto (type_specified))
13422 if (cxx_dialect < cxx14)
13424 error ("invalid use of %<auto%> in conversion operator");
13425 return error_mark_node;
13427 else if (template_parm_scope_p ())
13428 warning (0, "use of %<auto%> in member template "
13429 "conversion operator can never be deduced");
13432 return type_specified;
13435 /* Parse an (optional) conversion-declarator.
13437 conversion-declarator:
13438 ptr-operator conversion-declarator [opt]
13442 static cp_declarator *
13443 cp_parser_conversion_declarator_opt (cp_parser* parser)
13445 enum tree_code code;
13446 tree class_type, std_attributes = NULL_TREE;
13447 cp_cv_quals cv_quals;
13449 /* We don't know if there's a ptr-operator next, or not. */
13450 cp_parser_parse_tentatively (parser);
13451 /* Try the ptr-operator. */
13452 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13454 /* If it worked, look for more conversion-declarators. */
13455 if (cp_parser_parse_definitely (parser))
13457 cp_declarator *declarator;
13459 /* Parse another optional declarator. */
13460 declarator = cp_parser_conversion_declarator_opt (parser);
13462 declarator = cp_parser_make_indirect_declarator
13463 (code, class_type, cv_quals, declarator, std_attributes);
13471 /* Parse an (optional) ctor-initializer.
13474 : mem-initializer-list
13476 Returns TRUE iff the ctor-initializer was actually present. */
13479 cp_parser_ctor_initializer_opt (cp_parser* parser)
13481 /* If the next token is not a `:', then there is no
13482 ctor-initializer. */
13483 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13485 /* Do default initialization of any bases and members. */
13486 if (DECL_CONSTRUCTOR_P (current_function_decl))
13487 finish_mem_initializers (NULL_TREE);
13492 /* Consume the `:' token. */
13493 cp_lexer_consume_token (parser->lexer);
13494 /* And the mem-initializer-list. */
13495 cp_parser_mem_initializer_list (parser);
13500 /* Parse a mem-initializer-list.
13502 mem-initializer-list:
13503 mem-initializer ... [opt]
13504 mem-initializer ... [opt] , mem-initializer-list */
13507 cp_parser_mem_initializer_list (cp_parser* parser)
13509 tree mem_initializer_list = NULL_TREE;
13510 tree target_ctor = error_mark_node;
13511 cp_token *token = cp_lexer_peek_token (parser->lexer);
13513 /* Let the semantic analysis code know that we are starting the
13514 mem-initializer-list. */
13515 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13516 error_at (token->location,
13517 "only constructors take member initializers");
13519 /* Loop through the list. */
13522 tree mem_initializer;
13524 token = cp_lexer_peek_token (parser->lexer);
13525 /* Parse the mem-initializer. */
13526 mem_initializer = cp_parser_mem_initializer (parser);
13527 /* If the next token is a `...', we're expanding member initializers. */
13528 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13530 /* Consume the `...'. */
13531 cp_lexer_consume_token (parser->lexer);
13533 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13534 can be expanded but members cannot. */
13535 if (mem_initializer != error_mark_node
13536 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13538 error_at (token->location,
13539 "cannot expand initializer for member %<%D%>",
13540 TREE_PURPOSE (mem_initializer));
13541 mem_initializer = error_mark_node;
13544 /* Construct the pack expansion type. */
13545 if (mem_initializer != error_mark_node)
13546 mem_initializer = make_pack_expansion (mem_initializer);
13548 if (target_ctor != error_mark_node
13549 && mem_initializer != error_mark_node)
13551 error ("mem-initializer for %qD follows constructor delegation",
13552 TREE_PURPOSE (mem_initializer));
13553 mem_initializer = error_mark_node;
13555 /* Look for a target constructor. */
13556 if (mem_initializer != error_mark_node
13557 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13558 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13560 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13561 if (mem_initializer_list)
13563 error ("constructor delegation follows mem-initializer for %qD",
13564 TREE_PURPOSE (mem_initializer_list));
13565 mem_initializer = error_mark_node;
13567 target_ctor = mem_initializer;
13569 /* Add it to the list, unless it was erroneous. */
13570 if (mem_initializer != error_mark_node)
13572 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13573 mem_initializer_list = mem_initializer;
13575 /* If the next token is not a `,', we're done. */
13576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13578 /* Consume the `,' token. */
13579 cp_lexer_consume_token (parser->lexer);
13582 /* Perform semantic analysis. */
13583 if (DECL_CONSTRUCTOR_P (current_function_decl))
13584 finish_mem_initializers (mem_initializer_list);
13587 /* Parse a mem-initializer.
13590 mem-initializer-id ( expression-list [opt] )
13591 mem-initializer-id braced-init-list
13596 ( expression-list [opt] )
13598 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13599 class) or FIELD_DECL (for a non-static data member) to initialize;
13600 the TREE_VALUE is the expression-list. An empty initialization
13601 list is represented by void_list_node. */
13604 cp_parser_mem_initializer (cp_parser* parser)
13606 tree mem_initializer_id;
13607 tree expression_list;
13609 cp_token *token = cp_lexer_peek_token (parser->lexer);
13611 /* Find out what is being initialized. */
13612 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13614 permerror (token->location,
13615 "anachronistic old-style base class initializer");
13616 mem_initializer_id = NULL_TREE;
13620 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13621 if (mem_initializer_id == error_mark_node)
13622 return mem_initializer_id;
13624 member = expand_member_init (mem_initializer_id);
13625 if (member && !DECL_P (member))
13626 in_base_initializer = 1;
13628 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13630 bool expr_non_constant_p;
13631 cp_lexer_set_source_position (parser->lexer);
13632 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13633 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13634 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13635 expression_list = build_tree_list (NULL_TREE, expression_list);
13639 vec<tree, va_gc> *vec;
13640 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13642 /*allow_expansion_p=*/true,
13643 /*non_constant_p=*/NULL);
13645 return error_mark_node;
13646 expression_list = build_tree_list_vec (vec);
13647 release_tree_vector (vec);
13650 if (expression_list == error_mark_node)
13651 return error_mark_node;
13652 if (!expression_list)
13653 expression_list = void_type_node;
13655 in_base_initializer = 0;
13657 return member ? build_tree_list (member, expression_list) : error_mark_node;
13660 /* Parse a mem-initializer-id.
13662 mem-initializer-id:
13663 :: [opt] nested-name-specifier [opt] class-name
13664 decltype-specifier (C++11)
13667 Returns a TYPE indicating the class to be initialized for the first
13668 production (and the second in C++11). Returns an IDENTIFIER_NODE
13669 indicating the data member to be initialized for the last production. */
13672 cp_parser_mem_initializer_id (cp_parser* parser)
13674 bool global_scope_p;
13675 bool nested_name_specifier_p;
13676 bool template_p = false;
13679 cp_token *token = cp_lexer_peek_token (parser->lexer);
13681 /* `typename' is not allowed in this context ([temp.res]). */
13682 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13684 error_at (token->location,
13685 "keyword %<typename%> not allowed in this context (a qualified "
13686 "member initializer is implicitly a type)");
13687 cp_lexer_consume_token (parser->lexer);
13689 /* Look for the optional `::' operator. */
13691 = (cp_parser_global_scope_opt (parser,
13692 /*current_scope_valid_p=*/false)
13694 /* Look for the optional nested-name-specifier. The simplest way to
13699 The keyword `typename' is not permitted in a base-specifier or
13700 mem-initializer; in these contexts a qualified name that
13701 depends on a template-parameter is implicitly assumed to be a
13704 is to assume that we have seen the `typename' keyword at this
13706 nested_name_specifier_p
13707 = (cp_parser_nested_name_specifier_opt (parser,
13708 /*typename_keyword_p=*/true,
13709 /*check_dependency_p=*/true,
13711 /*is_declaration=*/true)
13713 if (nested_name_specifier_p)
13714 template_p = cp_parser_optional_template_keyword (parser);
13715 /* If there is a `::' operator or a nested-name-specifier, then we
13716 are definitely looking for a class-name. */
13717 if (global_scope_p || nested_name_specifier_p)
13718 return cp_parser_class_name (parser,
13719 /*typename_keyword_p=*/true,
13720 /*template_keyword_p=*/template_p,
13722 /*check_dependency_p=*/true,
13723 /*class_head_p=*/false,
13724 /*is_declaration=*/true);
13725 /* Otherwise, we could also be looking for an ordinary identifier. */
13726 cp_parser_parse_tentatively (parser);
13727 if (cp_lexer_next_token_is_decltype (parser->lexer))
13728 /* Try a decltype-specifier. */
13729 id = cp_parser_decltype (parser);
13731 /* Otherwise, try a class-name. */
13732 id = cp_parser_class_name (parser,
13733 /*typename_keyword_p=*/true,
13734 /*template_keyword_p=*/false,
13736 /*check_dependency_p=*/true,
13737 /*class_head_p=*/false,
13738 /*is_declaration=*/true);
13739 /* If we found one, we're done. */
13740 if (cp_parser_parse_definitely (parser))
13742 /* Otherwise, look for an ordinary identifier. */
13743 return cp_parser_identifier (parser);
13746 /* Overloading [gram.over] */
13748 /* Parse an operator-function-id.
13750 operator-function-id:
13753 Returns an IDENTIFIER_NODE for the operator which is a
13754 human-readable spelling of the identifier, e.g., `operator +'. */
13757 cp_parser_operator_function_id (cp_parser* parser)
13759 /* Look for the `operator' keyword. */
13760 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13761 return error_mark_node;
13762 /* And then the name of the operator itself. */
13763 return cp_parser_operator (parser);
13766 /* Return an identifier node for a user-defined literal operator.
13767 The suffix identifier is chained to the operator name identifier. */
13770 cp_literal_operator_id (const char* name)
13773 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13774 + strlen (name) + 10);
13775 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13776 identifier = get_identifier (buffer);
13781 /* Parse an operator.
13784 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13785 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13786 || ++ -- , ->* -> () []
13793 Returns an IDENTIFIER_NODE for the operator which is a
13794 human-readable spelling of the identifier, e.g., `operator +'. */
13797 cp_parser_operator (cp_parser* parser)
13799 tree id = NULL_TREE;
13803 /* Peek at the next token. */
13804 token = cp_lexer_peek_token (parser->lexer);
13806 location_t start_loc = token->location;
13808 /* Figure out which operator we have. */
13809 switch (token->type)
13815 /* The keyword should be either `new' or `delete'. */
13816 if (token->keyword == RID_NEW)
13818 else if (token->keyword == RID_DELETE)
13823 /* Consume the `new' or `delete' token. */
13824 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13826 /* Peek at the next token. */
13827 token = cp_lexer_peek_token (parser->lexer);
13828 /* If it's a `[' token then this is the array variant of the
13830 if (token->type == CPP_OPEN_SQUARE)
13832 /* Consume the `[' token. */
13833 cp_lexer_consume_token (parser->lexer);
13834 /* Look for the `]' token. */
13835 if (cp_token *close_token
13836 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13837 end_loc = close_token->location;
13838 id = ansi_opname (op == NEW_EXPR
13839 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13841 /* Otherwise, we have the non-array variant. */
13843 id = ansi_opname (op);
13845 location_t loc = make_location (start_loc, start_loc, end_loc);
13847 return cp_expr (id, loc);
13851 id = ansi_opname (PLUS_EXPR);
13855 id = ansi_opname (MINUS_EXPR);
13859 id = ansi_opname (MULT_EXPR);
13863 id = ansi_opname (TRUNC_DIV_EXPR);
13867 id = ansi_opname (TRUNC_MOD_EXPR);
13871 id = ansi_opname (BIT_XOR_EXPR);
13875 id = ansi_opname (BIT_AND_EXPR);
13879 id = ansi_opname (BIT_IOR_EXPR);
13883 id = ansi_opname (BIT_NOT_EXPR);
13887 id = ansi_opname (TRUTH_NOT_EXPR);
13891 id = ansi_assopname (NOP_EXPR);
13895 id = ansi_opname (LT_EXPR);
13899 id = ansi_opname (GT_EXPR);
13903 id = ansi_assopname (PLUS_EXPR);
13907 id = ansi_assopname (MINUS_EXPR);
13911 id = ansi_assopname (MULT_EXPR);
13915 id = ansi_assopname (TRUNC_DIV_EXPR);
13919 id = ansi_assopname (TRUNC_MOD_EXPR);
13923 id = ansi_assopname (BIT_XOR_EXPR);
13927 id = ansi_assopname (BIT_AND_EXPR);
13931 id = ansi_assopname (BIT_IOR_EXPR);
13935 id = ansi_opname (LSHIFT_EXPR);
13939 id = ansi_opname (RSHIFT_EXPR);
13942 case CPP_LSHIFT_EQ:
13943 id = ansi_assopname (LSHIFT_EXPR);
13946 case CPP_RSHIFT_EQ:
13947 id = ansi_assopname (RSHIFT_EXPR);
13951 id = ansi_opname (EQ_EXPR);
13955 id = ansi_opname (NE_EXPR);
13959 id = ansi_opname (LE_EXPR);
13962 case CPP_GREATER_EQ:
13963 id = ansi_opname (GE_EXPR);
13967 id = ansi_opname (TRUTH_ANDIF_EXPR);
13971 id = ansi_opname (TRUTH_ORIF_EXPR);
13974 case CPP_PLUS_PLUS:
13975 id = ansi_opname (POSTINCREMENT_EXPR);
13978 case CPP_MINUS_MINUS:
13979 id = ansi_opname (PREDECREMENT_EXPR);
13983 id = ansi_opname (COMPOUND_EXPR);
13986 case CPP_DEREF_STAR:
13987 id = ansi_opname (MEMBER_REF);
13991 id = ansi_opname (COMPONENT_REF);
13994 case CPP_OPEN_PAREN:
13995 /* Consume the `('. */
13996 cp_lexer_consume_token (parser->lexer);
13997 /* Look for the matching `)'. */
13998 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13999 return ansi_opname (CALL_EXPR);
14001 case CPP_OPEN_SQUARE:
14002 /* Consume the `['. */
14003 cp_lexer_consume_token (parser->lexer);
14004 /* Look for the matching `]'. */
14005 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14006 return ansi_opname (ARRAY_REF);
14008 case CPP_UTF8STRING:
14009 case CPP_UTF8STRING_USERDEF:
14015 case CPP_STRING_USERDEF:
14016 case CPP_WSTRING_USERDEF:
14017 case CPP_STRING16_USERDEF:
14018 case CPP_STRING32_USERDEF:
14020 tree str, string_tree;
14023 if (cxx_dialect == cxx98)
14024 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14026 /* Consume the string. */
14027 str = cp_parser_string_literal (parser, /*translate=*/true,
14028 /*wide_ok=*/true, /*lookup_udlit=*/false);
14029 if (str == error_mark_node)
14030 return error_mark_node;
14031 else if (TREE_CODE (str) == USERDEF_LITERAL)
14033 string_tree = USERDEF_LITERAL_VALUE (str);
14034 id = USERDEF_LITERAL_SUFFIX_ID (str);
14039 /* Look for the suffix identifier. */
14040 token = cp_lexer_peek_token (parser->lexer);
14041 if (token->type == CPP_NAME)
14042 id = cp_parser_identifier (parser);
14043 else if (token->type == CPP_KEYWORD)
14045 error ("unexpected keyword;"
14046 " remove space between quotes and suffix identifier");
14047 return error_mark_node;
14051 error ("expected suffix identifier");
14052 return error_mark_node;
14055 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14056 (TREE_TYPE (TREE_TYPE (string_tree))));
14057 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14060 error ("expected empty string after %<operator%> keyword");
14061 return error_mark_node;
14063 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14066 error ("invalid encoding prefix in literal operator");
14067 return error_mark_node;
14069 if (id != error_mark_node)
14071 const char *name = IDENTIFIER_POINTER (id);
14072 id = cp_literal_operator_id (name);
14078 /* Anything else is an error. */
14082 /* If we have selected an identifier, we need to consume the
14085 cp_lexer_consume_token (parser->lexer);
14086 /* Otherwise, no valid operator name was present. */
14089 cp_parser_error (parser, "expected operator");
14090 id = error_mark_node;
14093 return cp_expr (id, start_loc);
14096 /* Parse a template-declaration.
14098 template-declaration:
14099 export [opt] template < template-parameter-list > declaration
14101 If MEMBER_P is TRUE, this template-declaration occurs within a
14104 The grammar rule given by the standard isn't correct. What
14105 is really meant is:
14107 template-declaration:
14108 export [opt] template-parameter-list-seq
14109 decl-specifier-seq [opt] init-declarator [opt] ;
14110 export [opt] template-parameter-list-seq
14111 function-definition
14113 template-parameter-list-seq:
14114 template-parameter-list-seq [opt]
14115 template < template-parameter-list >
14117 Concept Extensions:
14119 template-parameter-list-seq:
14120 template < template-parameter-list > requires-clause [opt]
14123 requires logical-or-expression */
14126 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14128 /* Check for `export'. */
14129 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14131 /* Consume the `export' token. */
14132 cp_lexer_consume_token (parser->lexer);
14133 /* Warn that we do not support `export'. */
14134 warning (0, "keyword %<export%> not implemented, and will be ignored");
14137 cp_parser_template_declaration_after_export (parser, member_p);
14140 /* Parse a template-parameter-list.
14142 template-parameter-list:
14144 template-parameter-list , template-parameter
14146 Returns a TREE_LIST. Each node represents a template parameter.
14147 The nodes are connected via their TREE_CHAINs. */
14150 cp_parser_template_parameter_list (cp_parser* parser)
14152 tree parameter_list = NULL_TREE;
14154 begin_template_parm_list ();
14156 /* The loop below parses the template parms. We first need to know
14157 the total number of template parms to be able to compute proper
14158 canonical types of each dependent type. So after the loop, when
14159 we know the total number of template parms,
14160 end_template_parm_list computes the proper canonical types and
14161 fixes up the dependent types accordingly. */
14166 bool is_parameter_pack;
14167 location_t parm_loc;
14169 /* Parse the template-parameter. */
14170 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14171 parameter = cp_parser_template_parameter (parser,
14173 &is_parameter_pack);
14174 /* Add it to the list. */
14175 if (parameter != error_mark_node)
14176 parameter_list = process_template_parm (parameter_list,
14180 is_parameter_pack);
14183 tree err_parm = build_tree_list (parameter, parameter);
14184 parameter_list = chainon (parameter_list, err_parm);
14187 /* If the next token is not a `,', we're done. */
14188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14190 /* Otherwise, consume the `,' token. */
14191 cp_lexer_consume_token (parser->lexer);
14194 return end_template_parm_list (parameter_list);
14197 /* Parse a introduction-list.
14200 introduced-parameter
14201 introduction-list , introduced-parameter
14203 introduced-parameter:
14204 ...[opt] identifier
14206 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14207 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14208 WILDCARD_DECL will also have DECL_NAME set and token location in
14209 DECL_SOURCE_LOCATION. */
14212 cp_parser_introduction_list (cp_parser *parser)
14214 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14218 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14220 cp_lexer_consume_token (parser->lexer);
14222 /* Build placeholder. */
14223 tree parm = build_nt (WILDCARD_DECL);
14224 DECL_SOURCE_LOCATION (parm)
14225 = cp_lexer_peek_token (parser->lexer)->location;
14226 DECL_NAME (parm) = cp_parser_identifier (parser);
14227 WILDCARD_PACK_P (parm) = is_pack;
14228 vec_safe_push (introduction_vec, parm);
14230 /* If the next token is not a `,', we're done. */
14231 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14233 /* Otherwise, consume the `,' token. */
14234 cp_lexer_consume_token (parser->lexer);
14237 /* Convert the vec into a TREE_VEC. */
14238 tree introduction_list = make_tree_vec (introduction_vec->length ());
14241 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14242 TREE_VEC_ELT (introduction_list, n) = parm;
14244 release_tree_vector (introduction_vec);
14245 return introduction_list;
14248 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14249 is an abstract declarator. */
14251 static inline cp_declarator*
14252 get_id_declarator (cp_declarator *declarator)
14254 cp_declarator *d = declarator;
14255 while (d && d->kind != cdk_id)
14260 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14261 is an abstract declarator. */
14264 get_unqualified_id (cp_declarator *declarator)
14266 declarator = get_id_declarator (declarator);
14268 return declarator->u.id.unqualified_name;
14273 /* Returns true if DECL represents a constrained-parameter. */
14276 is_constrained_parameter (tree decl)
14279 && TREE_CODE (decl) == TYPE_DECL
14280 && CONSTRAINED_PARM_CONCEPT (decl)
14281 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14284 /* Returns true if PARM declares a constrained-parameter. */
14287 is_constrained_parameter (cp_parameter_declarator *parm)
14289 return is_constrained_parameter (parm->decl_specifiers.type);
14292 /* Check that the type parameter is only a declarator-id, and that its
14293 type is not cv-qualified. */
14296 cp_parser_check_constrained_type_parm (cp_parser *parser,
14297 cp_parameter_declarator *parm)
14299 if (!parm->declarator)
14302 if (parm->declarator->kind != cdk_id)
14304 cp_parser_error (parser, "invalid constrained type parameter");
14308 /* Don't allow cv-qualified type parameters. */
14309 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14310 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14312 cp_parser_error (parser, "cv-qualified type parameter");
14319 /* Finish parsing/processing a template type parameter and checking
14320 various restrictions. */
14323 cp_parser_constrained_type_template_parm (cp_parser *parser,
14325 cp_parameter_declarator* parmdecl)
14327 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14328 return finish_template_type_parm (class_type_node, id);
14330 return error_mark_node;
14334 finish_constrained_template_template_parm (tree proto, tree id)
14336 /* FIXME: This should probably be copied, and we may need to adjust
14337 the template parameter depths. */
14338 tree saved_parms = current_template_parms;
14339 begin_template_parm_list ();
14340 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14341 end_template_parm_list ();
14343 tree parm = finish_template_template_parm (class_type_node, id);
14344 current_template_parms = saved_parms;
14349 /* Finish parsing/processing a template template parameter by borrowing
14350 the template parameter list from the prototype parameter. */
14353 cp_parser_constrained_template_template_parm (cp_parser *parser,
14356 cp_parameter_declarator *parmdecl)
14358 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14359 return error_mark_node;
14360 return finish_constrained_template_template_parm (proto, id);
14363 /* Create a new non-type template parameter from the given PARM
14367 constrained_non_type_template_parm (bool *is_non_type,
14368 cp_parameter_declarator *parm)
14370 *is_non_type = true;
14371 cp_declarator *decl = parm->declarator;
14372 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14373 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14374 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14377 /* Build a constrained template parameter based on the PARMDECL
14378 declarator. The type of PARMDECL is the constrained type, which
14379 refers to the prototype template parameter that ultimately
14380 specifies the type of the declared parameter. */
14383 finish_constrained_parameter (cp_parser *parser,
14384 cp_parameter_declarator *parmdecl,
14386 bool *is_parameter_pack)
14388 tree decl = parmdecl->decl_specifiers.type;
14389 tree id = get_unqualified_id (parmdecl->declarator);
14390 tree def = parmdecl->default_argument;
14391 tree proto = DECL_INITIAL (decl);
14393 /* A template parameter constrained by a variadic concept shall also
14394 be declared as a template parameter pack. */
14395 bool is_variadic = template_parameter_pack_p (proto);
14396 if (is_variadic && !*is_parameter_pack)
14397 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14399 /* Build the parameter. Return an error if the declarator was invalid. */
14401 if (TREE_CODE (proto) == TYPE_DECL)
14402 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14403 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14404 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14407 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14408 if (parm == error_mark_node)
14409 return error_mark_node;
14411 /* Finish the parameter decl and create a node attaching the
14412 default argument and constraint. */
14413 parm = build_tree_list (def, parm);
14414 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14419 /* Returns true if the parsed type actually represents the declaration
14420 of a type template-parameter. */
14423 declares_constrained_type_template_parameter (tree type)
14425 return (is_constrained_parameter (type)
14426 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14430 /* Returns true if the parsed type actually represents the declaration of
14431 a template template-parameter. */
14434 declares_constrained_template_template_parameter (tree type)
14436 return (is_constrained_parameter (type)
14437 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14440 /* Parse a default argument for a type template-parameter.
14441 Note that diagnostics are handled in cp_parser_template_parameter. */
14444 cp_parser_default_type_template_argument (cp_parser *parser)
14446 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14448 /* Consume the `=' token. */
14449 cp_lexer_consume_token (parser->lexer);
14451 cp_token *token = cp_lexer_peek_token (parser->lexer);
14453 /* Parse the default-argument. */
14454 push_deferring_access_checks (dk_no_deferred);
14455 tree default_argument = cp_parser_type_id (parser);
14456 pop_deferring_access_checks ();
14458 if (flag_concepts && type_uses_auto (default_argument))
14460 error_at (token->location,
14461 "invalid use of %<auto%> in default template argument");
14462 return error_mark_node;
14465 return default_argument;
14468 /* Parse a default argument for a template template-parameter. */
14471 cp_parser_default_template_template_argument (cp_parser *parser)
14473 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14477 /* Consume the `='. */
14478 cp_lexer_consume_token (parser->lexer);
14479 /* Parse the id-expression. */
14480 push_deferring_access_checks (dk_no_deferred);
14481 /* save token before parsing the id-expression, for error
14483 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14484 tree default_argument
14485 = cp_parser_id_expression (parser,
14486 /*template_keyword_p=*/false,
14487 /*check_dependency_p=*/true,
14488 /*template_p=*/&is_template,
14489 /*declarator_p=*/false,
14490 /*optional_p=*/false);
14491 if (TREE_CODE (default_argument) == TYPE_DECL)
14492 /* If the id-expression was a template-id that refers to
14493 a template-class, we already have the declaration here,
14494 so no further lookup is needed. */
14497 /* Look up the name. */
14499 = cp_parser_lookup_name (parser, default_argument,
14501 /*is_template=*/is_template,
14502 /*is_namespace=*/false,
14503 /*check_dependency=*/true,
14504 /*ambiguous_decls=*/NULL,
14506 /* See if the default argument is valid. */
14507 default_argument = check_template_template_default_arg (default_argument);
14508 pop_deferring_access_checks ();
14509 return default_argument;
14512 /* Parse a template-parameter.
14514 template-parameter:
14516 parameter-declaration
14518 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14519 the parameter. The TREE_PURPOSE is the default value, if any.
14520 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14521 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14522 set to true iff this parameter is a parameter pack. */
14525 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14526 bool *is_parameter_pack)
14529 cp_parameter_declarator *parameter_declarator;
14532 /* Assume it is a type parameter or a template parameter. */
14533 *is_non_type = false;
14534 /* Assume it not a parameter pack. */
14535 *is_parameter_pack = false;
14536 /* Peek at the next token. */
14537 token = cp_lexer_peek_token (parser->lexer);
14538 /* If it is `class' or `template', we have a type-parameter. */
14539 if (token->keyword == RID_TEMPLATE)
14540 return cp_parser_type_parameter (parser, is_parameter_pack);
14541 /* If it is `class' or `typename' we do not know yet whether it is a
14542 type parameter or a non-type parameter. Consider:
14544 template <typename T, typename T::X X> ...
14548 template <class C, class D*> ...
14550 Here, the first parameter is a type parameter, and the second is
14551 a non-type parameter. We can tell by looking at the token after
14552 the identifier -- if it is a `,', `=', or `>' then we have a type
14554 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14556 /* Peek at the token after `class' or `typename'. */
14557 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14558 /* If it's an ellipsis, we have a template type parameter
14560 if (token->type == CPP_ELLIPSIS)
14561 return cp_parser_type_parameter (parser, is_parameter_pack);
14562 /* If it's an identifier, skip it. */
14563 if (token->type == CPP_NAME)
14564 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14565 /* Now, see if the token looks like the end of a template
14567 if (token->type == CPP_COMMA
14568 || token->type == CPP_EQ
14569 || token->type == CPP_GREATER)
14570 return cp_parser_type_parameter (parser, is_parameter_pack);
14573 /* Otherwise, it is a non-type parameter or a constrained parameter.
14577 When parsing a default template-argument for a non-type
14578 template-parameter, the first non-nested `>' is taken as the end
14579 of the template parameter-list rather than a greater-than
14581 parameter_declarator
14582 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14583 /*parenthesized_p=*/NULL);
14585 if (!parameter_declarator)
14586 return error_mark_node;
14588 /* If the parameter declaration is marked as a parameter pack, set
14589 *IS_PARAMETER_PACK to notify the caller. */
14590 if (parameter_declarator->template_parameter_pack_p)
14591 *is_parameter_pack = true;
14593 if (parameter_declarator->default_argument)
14595 /* Can happen in some cases of erroneous input (c++/34892). */
14596 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14597 /* Consume the `...' for better error recovery. */
14598 cp_lexer_consume_token (parser->lexer);
14601 // The parameter may have been constrained.
14602 if (is_constrained_parameter (parameter_declarator))
14603 return finish_constrained_parameter (parser,
14604 parameter_declarator,
14606 is_parameter_pack);
14608 // Now we're sure that the parameter is a non-type parameter.
14609 *is_non_type = true;
14611 parm = grokdeclarator (parameter_declarator->declarator,
14612 ¶meter_declarator->decl_specifiers,
14613 TPARM, /*initialized=*/0,
14614 /*attrlist=*/NULL);
14615 if (parm == error_mark_node)
14616 return error_mark_node;
14618 return build_tree_list (parameter_declarator->default_argument, parm);
14621 /* Parse a type-parameter.
14624 class identifier [opt]
14625 class identifier [opt] = type-id
14626 typename identifier [opt]
14627 typename identifier [opt] = type-id
14628 template < template-parameter-list > class identifier [opt]
14629 template < template-parameter-list > class identifier [opt]
14632 GNU Extension (variadic templates):
14635 class ... identifier [opt]
14636 typename ... identifier [opt]
14638 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14639 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14640 the declaration of the parameter.
14642 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14645 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14650 /* Look for a keyword to tell us what kind of parameter this is. */
14651 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14653 return error_mark_node;
14655 switch (token->keyword)
14661 tree default_argument;
14663 /* If the next token is an ellipsis, we have a template
14665 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14667 /* Consume the `...' token. */
14668 cp_lexer_consume_token (parser->lexer);
14669 maybe_warn_variadic_templates ();
14671 *is_parameter_pack = true;
14674 /* If the next token is an identifier, then it names the
14676 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14677 identifier = cp_parser_identifier (parser);
14679 identifier = NULL_TREE;
14681 /* Create the parameter. */
14682 parameter = finish_template_type_parm (class_type_node, identifier);
14684 /* If the next token is an `=', we have a default argument. */
14685 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14688 = cp_parser_default_type_template_argument (parser);
14690 /* Template parameter packs cannot have default
14692 if (*is_parameter_pack)
14695 error_at (token->location,
14696 "template parameter pack %qD cannot have a "
14697 "default argument", identifier);
14699 error_at (token->location,
14700 "template parameter packs cannot have "
14701 "default arguments");
14702 default_argument = NULL_TREE;
14704 else if (check_for_bare_parameter_packs (default_argument))
14705 default_argument = error_mark_node;
14708 default_argument = NULL_TREE;
14710 /* Create the combined representation of the parameter and the
14711 default argument. */
14712 parameter = build_tree_list (default_argument, parameter);
14719 tree default_argument;
14721 /* Look for the `<'. */
14722 cp_parser_require (parser, CPP_LESS, RT_LESS);
14723 /* Parse the template-parameter-list. */
14724 cp_parser_template_parameter_list (parser);
14725 /* Look for the `>'. */
14726 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14728 // If template requirements are present, parse them.
14731 tree reqs = get_shorthand_constraints (current_template_parms);
14732 if (tree r = cp_parser_requires_clause_opt (parser))
14733 reqs = conjoin_constraints (reqs, normalize_expression (r));
14734 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14737 /* Look for the `class' or 'typename' keywords. */
14738 cp_parser_type_parameter_key (parser);
14739 /* If the next token is an ellipsis, we have a template
14741 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14743 /* Consume the `...' token. */
14744 cp_lexer_consume_token (parser->lexer);
14745 maybe_warn_variadic_templates ();
14747 *is_parameter_pack = true;
14749 /* If the next token is an `=', then there is a
14750 default-argument. If the next token is a `>', we are at
14751 the end of the parameter-list. If the next token is a `,',
14752 then we are at the end of this parameter. */
14753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14754 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14755 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14757 identifier = cp_parser_identifier (parser);
14758 /* Treat invalid names as if the parameter were nameless. */
14759 if (identifier == error_mark_node)
14760 identifier = NULL_TREE;
14763 identifier = NULL_TREE;
14765 /* Create the template parameter. */
14766 parameter = finish_template_template_parm (class_type_node,
14769 /* If the next token is an `=', then there is a
14770 default-argument. */
14771 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14774 = cp_parser_default_template_template_argument (parser);
14776 /* Template parameter packs cannot have default
14778 if (*is_parameter_pack)
14781 error_at (token->location,
14782 "template parameter pack %qD cannot "
14783 "have a default argument",
14786 error_at (token->location, "template parameter packs cannot "
14787 "have default arguments");
14788 default_argument = NULL_TREE;
14792 default_argument = NULL_TREE;
14794 /* Create the combined representation of the parameter and the
14795 default argument. */
14796 parameter = build_tree_list (default_argument, parameter);
14801 gcc_unreachable ();
14808 /* Parse a template-id.
14811 template-name < template-argument-list [opt] >
14813 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14814 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14815 returned. Otherwise, if the template-name names a function, or set
14816 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14817 names a class, returns a TYPE_DECL for the specialization.
14819 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14820 uninstantiated templates. */
14823 cp_parser_template_id (cp_parser *parser,
14824 bool template_keyword_p,
14825 bool check_dependency_p,
14826 enum tag_types tag_type,
14827 bool is_declaration)
14832 cp_token_position start_of_id = 0;
14833 cp_token *next_token = NULL, *next_token_2 = NULL;
14834 bool is_identifier;
14836 /* If the next token corresponds to a template-id, there is no need
14838 next_token = cp_lexer_peek_token (parser->lexer);
14839 if (next_token->type == CPP_TEMPLATE_ID)
14841 cp_lexer_consume_token (parser->lexer);
14842 return saved_checks_value (next_token->u.tree_check_value);
14845 /* Avoid performing name lookup if there is no possibility of
14846 finding a template-id. */
14847 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14848 || (next_token->type == CPP_NAME
14849 && !cp_parser_nth_token_starts_template_argument_list_p
14852 cp_parser_error (parser, "expected template-id");
14853 return error_mark_node;
14856 /* Remember where the template-id starts. */
14857 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14858 start_of_id = cp_lexer_token_position (parser->lexer, false);
14860 push_deferring_access_checks (dk_deferred);
14862 /* Parse the template-name. */
14863 is_identifier = false;
14864 templ = cp_parser_template_name (parser, template_keyword_p,
14865 check_dependency_p,
14869 if (templ == error_mark_node || is_identifier)
14871 pop_deferring_access_checks ();
14875 /* Since we're going to preserve any side-effects from this parse, set up a
14876 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14877 in the template arguments. */
14878 tentative_firewall firewall (parser);
14880 /* If we find the sequence `[:' after a template-name, it's probably
14881 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14882 parse correctly the argument list. */
14883 next_token = cp_lexer_peek_token (parser->lexer);
14884 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14885 if (next_token->type == CPP_OPEN_SQUARE
14886 && next_token->flags & DIGRAPH
14887 && next_token_2->type == CPP_COLON
14888 && !(next_token_2->flags & PREV_WHITE))
14890 cp_parser_parse_tentatively (parser);
14891 /* Change `:' into `::'. */
14892 next_token_2->type = CPP_SCOPE;
14893 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14895 cp_lexer_consume_token (parser->lexer);
14897 /* Parse the arguments. */
14898 arguments = cp_parser_enclosed_template_argument_list (parser);
14899 if (!cp_parser_parse_definitely (parser))
14901 /* If we couldn't parse an argument list, then we revert our changes
14902 and return simply an error. Maybe this is not a template-id
14904 next_token_2->type = CPP_COLON;
14905 cp_parser_error (parser, "expected %<<%>");
14906 pop_deferring_access_checks ();
14907 return error_mark_node;
14909 /* Otherwise, emit an error about the invalid digraph, but continue
14910 parsing because we got our argument list. */
14911 if (permerror (next_token->location,
14912 "%<<::%> cannot begin a template-argument list"))
14914 static bool hint = false;
14915 inform (next_token->location,
14916 "%<<:%> is an alternate spelling for %<[%>."
14917 " Insert whitespace between %<<%> and %<::%>");
14918 if (!hint && !flag_permissive)
14920 inform (next_token->location, "(if you use %<-fpermissive%> "
14921 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14922 "accept your code)");
14929 /* Look for the `<' that starts the template-argument-list. */
14930 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14932 pop_deferring_access_checks ();
14933 return error_mark_node;
14935 /* Parse the arguments. */
14936 arguments = cp_parser_enclosed_template_argument_list (parser);
14939 /* Build a representation of the specialization. */
14940 if (identifier_p (templ))
14941 template_id = build_min_nt_loc (next_token->location,
14944 else if (DECL_TYPE_TEMPLATE_P (templ)
14945 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14947 bool entering_scope;
14948 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14949 template (rather than some instantiation thereof) only if
14950 is not nested within some other construct. For example, in
14951 "template <typename T> void f(T) { A<T>::", A<T> is just an
14952 instantiation of A. */
14953 entering_scope = (template_parm_scope_p ()
14954 && cp_lexer_next_token_is (parser->lexer,
14957 = finish_template_type (templ, arguments, entering_scope);
14959 /* A template-like identifier may be a partial concept id. */
14960 else if (flag_concepts
14961 && (template_id = (cp_parser_maybe_partial_concept_id
14962 (parser, templ, arguments))))
14963 return template_id;
14964 else if (variable_template_p (templ))
14966 template_id = lookup_template_variable (templ, arguments);
14967 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14968 SET_EXPR_LOCATION (template_id, next_token->location);
14972 /* If it's not a class-template or a template-template, it should be
14973 a function-template. */
14974 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14975 || TREE_CODE (templ) == OVERLOAD
14976 || BASELINK_P (templ)));
14978 template_id = lookup_template_function (templ, arguments);
14979 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14980 SET_EXPR_LOCATION (template_id, next_token->location);
14983 /* If parsing tentatively, replace the sequence of tokens that makes
14984 up the template-id with a CPP_TEMPLATE_ID token. That way,
14985 should we re-parse the token stream, we will not have to repeat
14986 the effort required to do the parse, nor will we issue duplicate
14987 error messages about problems during instantiation of the
14990 /* Don't do this if we had a parse error in a declarator; re-parsing
14991 might succeed if a name changes meaning (60361). */
14992 && !(cp_parser_error_occurred (parser)
14993 && cp_parser_parsing_tentatively (parser)
14994 && parser->in_declarator_p))
14996 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14998 /* Reset the contents of the START_OF_ID token. */
14999 token->type = CPP_TEMPLATE_ID;
15001 /* Update the location to be of the form:
15002 template-name < template-argument-list [opt] >
15003 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15004 with caret == start at the start of the template-name,
15005 ranging until the closing '>'. */
15006 location_t finish_loc
15007 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15008 location_t combined_loc
15009 = make_location (token->location, token->location, finish_loc);
15010 token->location = combined_loc;
15012 /* Retrieve any deferred checks. Do not pop this access checks yet
15013 so the memory will not be reclaimed during token replacing below. */
15014 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15015 token->u.tree_check_value->value = template_id;
15016 token->u.tree_check_value->checks = get_deferred_access_checks ();
15017 token->keyword = RID_MAX;
15019 /* Purge all subsequent tokens. */
15020 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15022 /* ??? Can we actually assume that, if template_id ==
15023 error_mark_node, we will have issued a diagnostic to the
15024 user, as opposed to simply marking the tentative parse as
15026 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15027 error_at (token->location, "parse error in template argument list");
15030 pop_to_parent_deferring_access_checks ();
15031 return template_id;
15034 /* Parse a template-name.
15039 The standard should actually say:
15043 operator-function-id
15045 A defect report has been filed about this issue.
15047 A conversion-function-id cannot be a template name because they cannot
15048 be part of a template-id. In fact, looking at this code:
15050 a.operator K<int>()
15052 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15053 It is impossible to call a templated conversion-function-id with an
15054 explicit argument list, since the only allowed template parameter is
15055 the type to which it is converting.
15057 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15058 `template' keyword, in a construction like:
15062 In that case `f' is taken to be a template-name, even though there
15063 is no way of knowing for sure.
15065 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15066 name refers to a set of overloaded functions, at least one of which
15067 is a template, or an IDENTIFIER_NODE with the name of the template,
15068 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15069 names are looked up inside uninstantiated templates. */
15072 cp_parser_template_name (cp_parser* parser,
15073 bool template_keyword_p,
15074 bool check_dependency_p,
15075 bool is_declaration,
15076 enum tag_types tag_type,
15077 bool *is_identifier)
15082 cp_token *token = cp_lexer_peek_token (parser->lexer);
15084 /* If the next token is `operator', then we have either an
15085 operator-function-id or a conversion-function-id. */
15086 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15088 /* We don't know whether we're looking at an
15089 operator-function-id or a conversion-function-id. */
15090 cp_parser_parse_tentatively (parser);
15091 /* Try an operator-function-id. */
15092 identifier = cp_parser_operator_function_id (parser);
15093 /* If that didn't work, try a conversion-function-id. */
15094 if (!cp_parser_parse_definitely (parser))
15096 cp_parser_error (parser, "expected template-name");
15097 return error_mark_node;
15100 /* Look for the identifier. */
15102 identifier = cp_parser_identifier (parser);
15104 /* If we didn't find an identifier, we don't have a template-id. */
15105 if (identifier == error_mark_node)
15106 return error_mark_node;
15108 /* If the name immediately followed the `template' keyword, then it
15109 is a template-name. However, if the next token is not `<', then
15110 we do not treat it as a template-name, since it is not being used
15111 as part of a template-id. This enables us to handle constructs
15114 template <typename T> struct S { S(); };
15115 template <typename T> S<T>::S();
15117 correctly. We would treat `S' as a template -- if it were `S<T>'
15118 -- but we do not if there is no `<'. */
15120 if (processing_template_decl
15121 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15123 /* In a declaration, in a dependent context, we pretend that the
15124 "template" keyword was present in order to improve error
15125 recovery. For example, given:
15127 template <typename T> void f(T::X<int>);
15129 we want to treat "X<int>" as a template-id. */
15131 && !template_keyword_p
15132 && parser->scope && TYPE_P (parser->scope)
15133 && check_dependency_p
15134 && dependent_scope_p (parser->scope)
15135 /* Do not do this for dtors (or ctors), since they never
15136 need the template keyword before their name. */
15137 && !constructor_name_p (identifier, parser->scope))
15139 cp_token_position start = 0;
15141 /* Explain what went wrong. */
15142 error_at (token->location, "non-template %qD used as template",
15144 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15145 parser->scope, identifier);
15146 /* If parsing tentatively, find the location of the "<" token. */
15147 if (cp_parser_simulate_error (parser))
15148 start = cp_lexer_token_position (parser->lexer, true);
15149 /* Parse the template arguments so that we can issue error
15150 messages about them. */
15151 cp_lexer_consume_token (parser->lexer);
15152 cp_parser_enclosed_template_argument_list (parser);
15153 /* Skip tokens until we find a good place from which to
15154 continue parsing. */
15155 cp_parser_skip_to_closing_parenthesis (parser,
15156 /*recovering=*/true,
15158 /*consume_paren=*/false);
15159 /* If parsing tentatively, permanently remove the
15160 template argument list. That will prevent duplicate
15161 error messages from being issued about the missing
15162 "template" keyword. */
15164 cp_lexer_purge_tokens_after (parser->lexer, start);
15166 *is_identifier = true;
15170 /* If the "template" keyword is present, then there is generally
15171 no point in doing name-lookup, so we just return IDENTIFIER.
15172 But, if the qualifying scope is non-dependent then we can
15173 (and must) do name-lookup normally. */
15174 if (template_keyword_p
15176 || (TYPE_P (parser->scope)
15177 && dependent_type_p (parser->scope))))
15181 /* Look up the name. */
15182 decl = cp_parser_lookup_name (parser, identifier,
15184 /*is_template=*/true,
15185 /*is_namespace=*/false,
15186 check_dependency_p,
15187 /*ambiguous_decls=*/NULL,
15190 decl = strip_using_decl (decl);
15192 /* If DECL is a template, then the name was a template-name. */
15193 if (TREE_CODE (decl) == TEMPLATE_DECL)
15195 if (TREE_DEPRECATED (decl)
15196 && deprecated_state != DEPRECATED_SUPPRESS)
15197 warn_deprecated_use (decl, NULL_TREE);
15201 tree fn = NULL_TREE;
15203 /* The standard does not explicitly indicate whether a name that
15204 names a set of overloaded declarations, some of which are
15205 templates, is a template-name. However, such a name should
15206 be a template-name; otherwise, there is no way to form a
15207 template-id for the overloaded templates. */
15208 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15209 if (TREE_CODE (fns) == OVERLOAD)
15210 for (fn = fns; fn; fn = OVL_NEXT (fn))
15211 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15216 /* The name does not name a template. */
15217 cp_parser_error (parser, "expected template-name");
15218 return error_mark_node;
15222 /* If DECL is dependent, and refers to a function, then just return
15223 its name; we will look it up again during template instantiation. */
15224 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15226 tree scope = ovl_scope (decl);
15227 if (TYPE_P (scope) && dependent_type_p (scope))
15234 /* Parse a template-argument-list.
15236 template-argument-list:
15237 template-argument ... [opt]
15238 template-argument-list , template-argument ... [opt]
15240 Returns a TREE_VEC containing the arguments. */
15243 cp_parser_template_argument_list (cp_parser* parser)
15245 tree fixed_args[10];
15246 unsigned n_args = 0;
15247 unsigned alloced = 10;
15248 tree *arg_ary = fixed_args;
15250 bool saved_in_template_argument_list_p;
15252 bool saved_non_ice_p;
15254 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15255 parser->in_template_argument_list_p = true;
15256 /* Even if the template-id appears in an integral
15257 constant-expression, the contents of the argument list do
15259 saved_ice_p = parser->integral_constant_expression_p;
15260 parser->integral_constant_expression_p = false;
15261 saved_non_ice_p = parser->non_integral_constant_expression_p;
15262 parser->non_integral_constant_expression_p = false;
15264 /* Parse the arguments. */
15270 /* Consume the comma. */
15271 cp_lexer_consume_token (parser->lexer);
15273 /* Parse the template-argument. */
15274 argument = cp_parser_template_argument (parser);
15276 /* If the next token is an ellipsis, we're expanding a template
15278 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15280 if (argument == error_mark_node)
15282 cp_token *token = cp_lexer_peek_token (parser->lexer);
15283 error_at (token->location,
15284 "expected parameter pack before %<...%>");
15286 /* Consume the `...' token. */
15287 cp_lexer_consume_token (parser->lexer);
15289 /* Make the argument into a TYPE_PACK_EXPANSION or
15290 EXPR_PACK_EXPANSION. */
15291 argument = make_pack_expansion (argument);
15294 if (n_args == alloced)
15298 if (arg_ary == fixed_args)
15300 arg_ary = XNEWVEC (tree, alloced);
15301 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15304 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15306 arg_ary[n_args++] = argument;
15308 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15310 vec = make_tree_vec (n_args);
15313 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15315 if (arg_ary != fixed_args)
15317 parser->non_integral_constant_expression_p = saved_non_ice_p;
15318 parser->integral_constant_expression_p = saved_ice_p;
15319 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15321 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15325 /* Parse a template-argument.
15328 assignment-expression
15332 The representation is that of an assignment-expression, type-id, or
15333 id-expression -- except that the qualified id-expression is
15334 evaluated, so that the value returned is either a DECL or an
15337 Although the standard says "assignment-expression", it forbids
15338 throw-expressions or assignments in the template argument.
15339 Therefore, we use "conditional-expression" instead. */
15342 cp_parser_template_argument (cp_parser* parser)
15347 bool maybe_type_id = false;
15348 cp_token *token = NULL, *argument_start_token = NULL;
15349 location_t loc = 0;
15352 /* There's really no way to know what we're looking at, so we just
15353 try each alternative in order.
15357 In a template-argument, an ambiguity between a type-id and an
15358 expression is resolved to a type-id, regardless of the form of
15359 the corresponding template-parameter.
15361 Therefore, we try a type-id first. */
15362 cp_parser_parse_tentatively (parser);
15363 argument = cp_parser_template_type_arg (parser);
15364 /* If there was no error parsing the type-id but the next token is a
15365 '>>', our behavior depends on which dialect of C++ we're
15366 parsing. In C++98, we probably found a typo for '> >'. But there
15367 are type-id which are also valid expressions. For instance:
15369 struct X { int operator >> (int); };
15370 template <int V> struct Foo {};
15373 Here 'X()' is a valid type-id of a function type, but the user just
15374 wanted to write the expression "X() >> 5". Thus, we remember that we
15375 found a valid type-id, but we still try to parse the argument as an
15376 expression to see what happens.
15378 In C++0x, the '>>' will be considered two separate '>'
15380 if (!cp_parser_error_occurred (parser)
15381 && cxx_dialect == cxx98
15382 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15384 maybe_type_id = true;
15385 cp_parser_abort_tentative_parse (parser);
15389 /* If the next token isn't a `,' or a `>', then this argument wasn't
15390 really finished. This means that the argument is not a valid
15392 if (!cp_parser_next_token_ends_template_argument_p (parser))
15393 cp_parser_error (parser, "expected template-argument");
15394 /* If that worked, we're done. */
15395 if (cp_parser_parse_definitely (parser))
15398 /* We're still not sure what the argument will be. */
15399 cp_parser_parse_tentatively (parser);
15400 /* Try a template. */
15401 argument_start_token = cp_lexer_peek_token (parser->lexer);
15402 argument = cp_parser_id_expression (parser,
15403 /*template_keyword_p=*/false,
15404 /*check_dependency_p=*/true,
15406 /*declarator_p=*/false,
15407 /*optional_p=*/false);
15408 /* If the next token isn't a `,' or a `>', then this argument wasn't
15409 really finished. */
15410 if (!cp_parser_next_token_ends_template_argument_p (parser))
15411 cp_parser_error (parser, "expected template-argument");
15412 if (!cp_parser_error_occurred (parser))
15414 /* Figure out what is being referred to. If the id-expression
15415 was for a class template specialization, then we will have a
15416 TYPE_DECL at this point. There is no need to do name lookup
15417 at this point in that case. */
15418 if (TREE_CODE (argument) != TYPE_DECL)
15419 argument = cp_parser_lookup_name (parser, argument,
15421 /*is_template=*/template_p,
15422 /*is_namespace=*/false,
15423 /*check_dependency=*/true,
15424 /*ambiguous_decls=*/NULL,
15425 argument_start_token->location);
15426 /* Handle a constrained-type-specifier for a non-type template
15428 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15430 else if (TREE_CODE (argument) != TEMPLATE_DECL
15431 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15432 cp_parser_error (parser, "expected template-name");
15434 if (cp_parser_parse_definitely (parser))
15436 if (TREE_DEPRECATED (argument))
15437 warn_deprecated_use (argument, NULL_TREE);
15440 /* It must be a non-type argument. In C++17 any constant-expression is
15442 if (cxx_dialect > cxx14)
15445 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15447 -- an integral constant-expression of integral or enumeration
15450 -- the name of a non-type template-parameter; or
15452 -- the name of an object or function with external linkage...
15454 -- the address of an object or function with external linkage...
15456 -- a pointer to member... */
15457 /* Look for a non-type template parameter. */
15458 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15460 cp_parser_parse_tentatively (parser);
15461 argument = cp_parser_primary_expression (parser,
15462 /*address_p=*/false,
15464 /*template_arg_p=*/true,
15466 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15467 || !cp_parser_next_token_ends_template_argument_p (parser))
15468 cp_parser_simulate_error (parser);
15469 if (cp_parser_parse_definitely (parser))
15473 /* If the next token is "&", the argument must be the address of an
15474 object or function with external linkage. */
15475 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15478 loc = cp_lexer_peek_token (parser->lexer)->location;
15479 cp_lexer_consume_token (parser->lexer);
15481 /* See if we might have an id-expression. */
15482 token = cp_lexer_peek_token (parser->lexer);
15483 if (token->type == CPP_NAME
15484 || token->keyword == RID_OPERATOR
15485 || token->type == CPP_SCOPE
15486 || token->type == CPP_TEMPLATE_ID
15487 || token->type == CPP_NESTED_NAME_SPECIFIER)
15489 cp_parser_parse_tentatively (parser);
15490 argument = cp_parser_primary_expression (parser,
15493 /*template_arg_p=*/true,
15495 if (cp_parser_error_occurred (parser)
15496 || !cp_parser_next_token_ends_template_argument_p (parser))
15497 cp_parser_abort_tentative_parse (parser);
15502 if (INDIRECT_REF_P (argument))
15504 /* Strip the dereference temporarily. */
15505 gcc_assert (REFERENCE_REF_P (argument));
15506 argument = TREE_OPERAND (argument, 0);
15509 /* If we're in a template, we represent a qualified-id referring
15510 to a static data member as a SCOPE_REF even if the scope isn't
15511 dependent so that we can check access control later. */
15513 if (TREE_CODE (probe) == SCOPE_REF)
15514 probe = TREE_OPERAND (probe, 1);
15517 /* A variable without external linkage might still be a
15518 valid constant-expression, so no error is issued here
15519 if the external-linkage check fails. */
15520 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15521 cp_parser_simulate_error (parser);
15523 else if (is_overloaded_fn (argument))
15524 /* All overloaded functions are allowed; if the external
15525 linkage test does not pass, an error will be issued
15529 && (TREE_CODE (argument) == OFFSET_REF
15530 || TREE_CODE (argument) == SCOPE_REF))
15531 /* A pointer-to-member. */
15533 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15536 cp_parser_simulate_error (parser);
15538 if (cp_parser_parse_definitely (parser))
15541 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15542 tf_warning_or_error);
15544 argument = convert_from_reference (argument);
15549 /* If the argument started with "&", there are no other valid
15550 alternatives at this point. */
15553 cp_parser_error (parser, "invalid non-type template argument");
15554 return error_mark_node;
15558 /* If the argument wasn't successfully parsed as a type-id followed
15559 by '>>', the argument can only be a constant expression now.
15560 Otherwise, we try parsing the constant-expression tentatively,
15561 because the argument could really be a type-id. */
15563 cp_parser_parse_tentatively (parser);
15565 if (cxx_dialect <= cxx14)
15566 argument = cp_parser_constant_expression (parser);
15569 /* With C++17 generalized non-type template arguments we need to handle
15570 lvalue constant expressions, too. */
15571 argument = cp_parser_assignment_expression (parser);
15572 require_potential_constant_expression (argument);
15575 if (!maybe_type_id)
15577 if (!cp_parser_next_token_ends_template_argument_p (parser))
15578 cp_parser_error (parser, "expected template-argument");
15579 if (cp_parser_parse_definitely (parser))
15581 /* We did our best to parse the argument as a non type-id, but that
15582 was the only alternative that matched (albeit with a '>' after
15583 it). We can assume it's just a typo from the user, and a
15584 diagnostic will then be issued. */
15585 return cp_parser_template_type_arg (parser);
15588 /* Parse an explicit-instantiation.
15590 explicit-instantiation:
15591 template declaration
15593 Although the standard says `declaration', what it really means is:
15595 explicit-instantiation:
15596 template decl-specifier-seq [opt] declarator [opt] ;
15598 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15599 supposed to be allowed. A defect report has been filed about this
15604 explicit-instantiation:
15605 storage-class-specifier template
15606 decl-specifier-seq [opt] declarator [opt] ;
15607 function-specifier template
15608 decl-specifier-seq [opt] declarator [opt] ; */
15611 cp_parser_explicit_instantiation (cp_parser* parser)
15613 int declares_class_or_enum;
15614 cp_decl_specifier_seq decl_specifiers;
15615 tree extension_specifier = NULL_TREE;
15617 timevar_push (TV_TEMPLATE_INST);
15619 /* Look for an (optional) storage-class-specifier or
15620 function-specifier. */
15621 if (cp_parser_allow_gnu_extensions_p (parser))
15623 extension_specifier
15624 = cp_parser_storage_class_specifier_opt (parser);
15625 if (!extension_specifier)
15626 extension_specifier
15627 = cp_parser_function_specifier_opt (parser,
15628 /*decl_specs=*/NULL);
15631 /* Look for the `template' keyword. */
15632 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15633 /* Let the front end know that we are processing an explicit
15635 begin_explicit_instantiation ();
15636 /* [temp.explicit] says that we are supposed to ignore access
15637 control while processing explicit instantiation directives. */
15638 push_deferring_access_checks (dk_no_check);
15639 /* Parse a decl-specifier-seq. */
15640 cp_parser_decl_specifier_seq (parser,
15641 CP_PARSER_FLAGS_OPTIONAL,
15643 &declares_class_or_enum);
15644 /* If there was exactly one decl-specifier, and it declared a class,
15645 and there's no declarator, then we have an explicit type
15647 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15651 type = check_tag_decl (&decl_specifiers,
15652 /*explicit_type_instantiation_p=*/true);
15653 /* Turn access control back on for names used during
15654 template instantiation. */
15655 pop_deferring_access_checks ();
15657 do_type_instantiation (type, extension_specifier,
15658 /*complain=*/tf_error);
15662 cp_declarator *declarator;
15665 /* Parse the declarator. */
15667 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15668 /*ctor_dtor_or_conv_p=*/NULL,
15669 /*parenthesized_p=*/NULL,
15670 /*member_p=*/false,
15671 /*friend_p=*/false);
15672 if (declares_class_or_enum & 2)
15673 cp_parser_check_for_definition_in_return_type (declarator,
15674 decl_specifiers.type,
15675 decl_specifiers.locations[ds_type_spec]);
15676 if (declarator != cp_error_declarator)
15678 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15679 permerror (decl_specifiers.locations[ds_inline],
15680 "explicit instantiation shall not use"
15681 " %<inline%> specifier");
15682 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15683 permerror (decl_specifiers.locations[ds_constexpr],
15684 "explicit instantiation shall not use"
15685 " %<constexpr%> specifier");
15687 decl = grokdeclarator (declarator, &decl_specifiers,
15688 NORMAL, 0, &decl_specifiers.attributes);
15689 /* Turn access control back on for names used during
15690 template instantiation. */
15691 pop_deferring_access_checks ();
15692 /* Do the explicit instantiation. */
15693 do_decl_instantiation (decl, extension_specifier);
15697 pop_deferring_access_checks ();
15698 /* Skip the body of the explicit instantiation. */
15699 cp_parser_skip_to_end_of_statement (parser);
15702 /* We're done with the instantiation. */
15703 end_explicit_instantiation ();
15705 cp_parser_consume_semicolon_at_end_of_statement (parser);
15707 timevar_pop (TV_TEMPLATE_INST);
15710 /* Parse an explicit-specialization.
15712 explicit-specialization:
15713 template < > declaration
15715 Although the standard says `declaration', what it really means is:
15717 explicit-specialization:
15718 template <> decl-specifier [opt] init-declarator [opt] ;
15719 template <> function-definition
15720 template <> explicit-specialization
15721 template <> template-declaration */
15724 cp_parser_explicit_specialization (cp_parser* parser)
15726 bool need_lang_pop;
15727 cp_token *token = cp_lexer_peek_token (parser->lexer);
15729 /* Look for the `template' keyword. */
15730 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15731 /* Look for the `<'. */
15732 cp_parser_require (parser, CPP_LESS, RT_LESS);
15733 /* Look for the `>'. */
15734 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15735 /* We have processed another parameter list. */
15736 ++parser->num_template_parameter_lists;
15739 A template ... explicit specialization ... shall not have C
15741 if (current_lang_name == lang_name_c)
15743 error_at (token->location, "template specialization with C linkage");
15744 /* Give it C++ linkage to avoid confusing other parts of the
15746 push_lang_context (lang_name_cplusplus);
15747 need_lang_pop = true;
15750 need_lang_pop = false;
15751 /* Let the front end know that we are beginning a specialization. */
15752 if (!begin_specialization ())
15754 end_specialization ();
15758 /* If the next keyword is `template', we need to figure out whether
15759 or not we're looking a template-declaration. */
15760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15762 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15763 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15764 cp_parser_template_declaration_after_export (parser,
15765 /*member_p=*/false);
15767 cp_parser_explicit_specialization (parser);
15770 /* Parse the dependent declaration. */
15771 cp_parser_single_declaration (parser,
15773 /*member_p=*/false,
15774 /*explicit_specialization_p=*/true,
15775 /*friend_p=*/NULL);
15776 /* We're done with the specialization. */
15777 end_specialization ();
15778 /* For the erroneous case of a template with C linkage, we pushed an
15779 implicit C++ linkage scope; exit that scope now. */
15781 pop_lang_context ();
15782 /* We're done with this parameter list. */
15783 --parser->num_template_parameter_lists;
15786 /* Parse a type-specifier.
15789 simple-type-specifier
15792 elaborated-type-specifier
15800 Returns a representation of the type-specifier. For a
15801 class-specifier, enum-specifier, or elaborated-type-specifier, a
15802 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15804 The parser flags FLAGS is used to control type-specifier parsing.
15806 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15807 in a decl-specifier-seq.
15809 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15810 class-specifier, enum-specifier, or elaborated-type-specifier, then
15811 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15812 if a type is declared; 2 if it is defined. Otherwise, it is set to
15815 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15816 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15817 is set to FALSE. */
15820 cp_parser_type_specifier (cp_parser* parser,
15821 cp_parser_flags flags,
15822 cp_decl_specifier_seq *decl_specs,
15823 bool is_declaration,
15824 int* declares_class_or_enum,
15825 bool* is_cv_qualifier)
15827 tree type_spec = NULL_TREE;
15830 cp_decl_spec ds = ds_last;
15832 /* Assume this type-specifier does not declare a new type. */
15833 if (declares_class_or_enum)
15834 *declares_class_or_enum = 0;
15835 /* And that it does not specify a cv-qualifier. */
15836 if (is_cv_qualifier)
15837 *is_cv_qualifier = false;
15838 /* Peek at the next token. */
15839 token = cp_lexer_peek_token (parser->lexer);
15841 /* If we're looking at a keyword, we can use that to guide the
15842 production we choose. */
15843 keyword = token->keyword;
15847 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15848 goto elaborated_type_specifier;
15850 /* Look for the enum-specifier. */
15851 type_spec = cp_parser_enum_specifier (parser);
15852 /* If that worked, we're done. */
15855 if (declares_class_or_enum)
15856 *declares_class_or_enum = 2;
15858 cp_parser_set_decl_spec_type (decl_specs,
15861 /*type_definition_p=*/true);
15865 goto elaborated_type_specifier;
15867 /* Any of these indicate either a class-specifier, or an
15868 elaborated-type-specifier. */
15872 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15873 goto elaborated_type_specifier;
15875 /* Parse tentatively so that we can back up if we don't find a
15876 class-specifier. */
15877 cp_parser_parse_tentatively (parser);
15878 /* Look for the class-specifier. */
15879 type_spec = cp_parser_class_specifier (parser);
15880 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15881 /* If that worked, we're done. */
15882 if (cp_parser_parse_definitely (parser))
15884 if (declares_class_or_enum)
15885 *declares_class_or_enum = 2;
15887 cp_parser_set_decl_spec_type (decl_specs,
15890 /*type_definition_p=*/true);
15894 /* Fall through. */
15895 elaborated_type_specifier:
15896 /* We're declaring (not defining) a class or enum. */
15897 if (declares_class_or_enum)
15898 *declares_class_or_enum = 1;
15900 /* Fall through. */
15902 /* Look for an elaborated-type-specifier. */
15904 = (cp_parser_elaborated_type_specifier
15906 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15909 cp_parser_set_decl_spec_type (decl_specs,
15912 /*type_definition_p=*/false);
15917 if (is_cv_qualifier)
15918 *is_cv_qualifier = true;
15923 if (is_cv_qualifier)
15924 *is_cv_qualifier = true;
15929 if (is_cv_qualifier)
15930 *is_cv_qualifier = true;
15934 /* The `__complex__' keyword is a GNU extension. */
15942 /* Handle simple keywords. */
15947 set_and_check_decl_spec_loc (decl_specs, ds, token);
15948 decl_specs->any_specifiers_p = true;
15950 return cp_lexer_consume_token (parser->lexer)->u.value;
15953 /* If we do not already have a type-specifier, assume we are looking
15954 at a simple-type-specifier. */
15955 type_spec = cp_parser_simple_type_specifier (parser,
15959 /* If we didn't find a type-specifier, and a type-specifier was not
15960 optional in this context, issue an error message. */
15961 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15963 cp_parser_error (parser, "expected type specifier");
15964 return error_mark_node;
15970 /* Parse a simple-type-specifier.
15972 simple-type-specifier:
15973 :: [opt] nested-name-specifier [opt] type-name
15974 :: [opt] nested-name-specifier template template-id
15989 simple-type-specifier:
15991 decltype ( expression )
15994 __underlying_type ( type-id )
15998 simple-type-specifier:
16000 __typeof__ unary-expression
16001 __typeof__ ( type-id )
16002 __typeof__ ( type-id ) { initializer-list , [opt] }
16004 Concepts Extension:
16006 simple-type-specifier:
16007 constrained-type-specifier
16009 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16010 appropriately updated. */
16013 cp_parser_simple_type_specifier (cp_parser* parser,
16014 cp_decl_specifier_seq *decl_specs,
16015 cp_parser_flags flags)
16017 tree type = NULL_TREE;
16021 /* Peek at the next token. */
16022 token = cp_lexer_peek_token (parser->lexer);
16024 /* If we're looking at a keyword, things are easy. */
16025 switch (token->keyword)
16029 decl_specs->explicit_char_p = true;
16030 type = char_type_node;
16033 type = char16_type_node;
16036 type = char32_type_node;
16039 type = wchar_type_node;
16042 type = boolean_type_node;
16045 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16046 type = short_integer_type_node;
16050 decl_specs->explicit_int_p = true;
16051 type = integer_type_node;
16057 idx = token->keyword - RID_INT_N_0;
16058 if (! int_n_enabled_p [idx])
16062 decl_specs->explicit_intN_p = true;
16063 decl_specs->int_n_idx = idx;
16065 type = int_n_trees [idx].signed_type;
16069 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16070 type = long_integer_type_node;
16073 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16074 type = integer_type_node;
16077 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16078 type = unsigned_type_node;
16081 type = float_type_node;
16084 type = double_type_node;
16087 type = void_type_node;
16091 maybe_warn_cpp0x (CPP0X_AUTO);
16092 if (parser->auto_is_implicit_function_template_parm_p)
16094 /* The 'auto' might be the placeholder return type for a function decl
16095 with trailing return type. */
16096 bool have_trailing_return_fn_decl = false;
16098 cp_parser_parse_tentatively (parser);
16099 cp_lexer_consume_token (parser->lexer);
16100 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16101 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16102 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16103 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16105 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16107 cp_lexer_consume_token (parser->lexer);
16108 cp_parser_skip_to_closing_parenthesis (parser,
16109 /*recovering*/false,
16111 /*consume_paren*/true);
16115 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16117 have_trailing_return_fn_decl = true;
16121 cp_lexer_consume_token (parser->lexer);
16123 cp_parser_abort_tentative_parse (parser);
16125 if (have_trailing_return_fn_decl)
16127 type = make_auto ();
16131 if (cxx_dialect >= cxx14)
16133 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16134 type = TREE_TYPE (type);
16137 type = error_mark_node;
16139 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16141 if (cxx_dialect < cxx14)
16142 error_at (token->location,
16143 "use of %<auto%> in lambda parameter declaration "
16144 "only available with "
16145 "-std=c++14 or -std=gnu++14");
16147 else if (cxx_dialect < cxx14)
16148 error_at (token->location,
16149 "use of %<auto%> in parameter declaration "
16150 "only available with "
16151 "-std=c++14 or -std=gnu++14");
16152 else if (!flag_concepts)
16153 pedwarn (token->location, OPT_Wpedantic,
16154 "ISO C++ forbids use of %<auto%> in parameter "
16158 type = make_auto ();
16162 /* Since DR 743, decltype can either be a simple-type-specifier by
16163 itself or begin a nested-name-specifier. Parsing it will replace
16164 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16165 handling below decide what to do. */
16166 cp_parser_decltype (parser);
16167 cp_lexer_set_token_position (parser->lexer, token);
16171 /* Consume the `typeof' token. */
16172 cp_lexer_consume_token (parser->lexer);
16173 /* Parse the operand to `typeof'. */
16174 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16175 /* If it is not already a TYPE, take its type. */
16176 if (!TYPE_P (type))
16177 type = finish_typeof (type);
16180 cp_parser_set_decl_spec_type (decl_specs, type,
16182 /*type_definition_p=*/false);
16186 case RID_UNDERLYING_TYPE:
16187 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16189 cp_parser_set_decl_spec_type (decl_specs, type,
16191 /*type_definition_p=*/false);
16196 case RID_DIRECT_BASES:
16197 type = cp_parser_trait_expr (parser, token->keyword);
16199 cp_parser_set_decl_spec_type (decl_specs, type,
16201 /*type_definition_p=*/false);
16207 /* If token is an already-parsed decltype not followed by ::,
16208 it's a simple-type-specifier. */
16209 if (token->type == CPP_DECLTYPE
16210 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16212 type = saved_checks_value (token->u.tree_check_value);
16215 cp_parser_set_decl_spec_type (decl_specs, type,
16217 /*type_definition_p=*/false);
16218 /* Remember that we are handling a decltype in order to
16219 implement the resolution of DR 1510 when the argument
16220 isn't instantiation dependent. */
16221 decl_specs->decltype_p = true;
16223 cp_lexer_consume_token (parser->lexer);
16227 /* If the type-specifier was for a built-in type, we're done. */
16230 /* Record the type. */
16232 && (token->keyword != RID_SIGNED
16233 && token->keyword != RID_UNSIGNED
16234 && token->keyword != RID_SHORT
16235 && token->keyword != RID_LONG))
16236 cp_parser_set_decl_spec_type (decl_specs,
16239 /*type_definition_p=*/false);
16241 decl_specs->any_specifiers_p = true;
16243 /* Consume the token. */
16244 cp_lexer_consume_token (parser->lexer);
16246 if (type == error_mark_node)
16247 return error_mark_node;
16249 /* There is no valid C++ program where a non-template type is
16250 followed by a "<". That usually indicates that the user thought
16251 that the type was a template. */
16252 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16255 return TYPE_NAME (type);
16258 /* The type-specifier must be a user-defined type. */
16259 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16264 /* Don't gobble tokens or issue error messages if this is an
16265 optional type-specifier. */
16266 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16267 cp_parser_parse_tentatively (parser);
16269 /* Look for the optional `::' operator. */
16271 = (cp_parser_global_scope_opt (parser,
16272 /*current_scope_valid_p=*/false)
16274 /* Look for the nested-name specifier. */
16276 = (cp_parser_nested_name_specifier_opt (parser,
16277 /*typename_keyword_p=*/false,
16278 /*check_dependency_p=*/true,
16280 /*is_declaration=*/false)
16282 token = cp_lexer_peek_token (parser->lexer);
16283 /* If we have seen a nested-name-specifier, and the next token
16284 is `template', then we are using the template-id production. */
16286 && cp_parser_optional_template_keyword (parser))
16288 /* Look for the template-id. */
16289 type = cp_parser_template_id (parser,
16290 /*template_keyword_p=*/true,
16291 /*check_dependency_p=*/true,
16293 /*is_declaration=*/false);
16294 /* If the template-id did not name a type, we are out of
16296 if (TREE_CODE (type) != TYPE_DECL)
16298 cp_parser_error (parser, "expected template-id for type");
16302 /* Otherwise, look for a type-name. */
16304 type = cp_parser_type_name (parser);
16305 /* Keep track of all name-lookups performed in class scopes. */
16309 && TREE_CODE (type) == TYPE_DECL
16310 && identifier_p (DECL_NAME (type)))
16311 maybe_note_name_used_in_class (DECL_NAME (type), type);
16312 /* If it didn't work out, we don't have a TYPE. */
16313 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16314 && !cp_parser_parse_definitely (parser))
16316 if (type && decl_specs)
16317 cp_parser_set_decl_spec_type (decl_specs, type,
16319 /*type_definition_p=*/false);
16322 /* If we didn't get a type-name, issue an error message. */
16323 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16325 cp_parser_error (parser, "expected type-name");
16326 return error_mark_node;
16329 if (type && type != error_mark_node)
16331 /* See if TYPE is an Objective-C type, and if so, parse and
16332 accept any protocol references following it. Do this before
16333 the cp_parser_check_for_invalid_template_id() call, because
16334 Objective-C types can be followed by '<...>' which would
16335 enclose protocol names rather than template arguments, and so
16336 everything is fine. */
16337 if (c_dialect_objc () && !parser->scope
16338 && (objc_is_id (type) || objc_is_class_name (type)))
16340 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16341 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16343 /* Clobber the "unqualified" type previously entered into
16344 DECL_SPECS with the new, improved protocol-qualified version. */
16346 decl_specs->type = qual_type;
16351 /* There is no valid C++ program where a non-template type is
16352 followed by a "<". That usually indicates that the user
16353 thought that the type was a template. */
16354 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16362 /* Parse a type-name.
16368 simple-template-id [in c++0x]
16385 Returns a TYPE_DECL for the type. */
16388 cp_parser_type_name (cp_parser* parser)
16390 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16395 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16399 /* We can't know yet whether it is a class-name or not. */
16400 cp_parser_parse_tentatively (parser);
16401 /* Try a class-name. */
16402 type_decl = cp_parser_class_name (parser,
16403 typename_keyword_p,
16404 /*template_keyword_p=*/false,
16406 /*check_dependency_p=*/true,
16407 /*class_head_p=*/false,
16408 /*is_declaration=*/false);
16409 /* If it's not a class-name, keep looking. */
16410 if (!cp_parser_parse_definitely (parser))
16412 if (cxx_dialect < cxx11)
16413 /* It must be a typedef-name or an enum-name. */
16414 return cp_parser_nonclass_name (parser);
16416 cp_parser_parse_tentatively (parser);
16417 /* It is either a simple-template-id representing an
16418 instantiation of an alias template... */
16419 type_decl = cp_parser_template_id (parser,
16420 /*template_keyword_p=*/false,
16421 /*check_dependency_p=*/true,
16423 /*is_declaration=*/false);
16424 /* Note that this must be an instantiation of an alias template
16425 because [temp.names]/6 says:
16427 A template-id that names an alias template specialization
16430 Whereas [temp.names]/7 says:
16432 A simple-template-id that names a class template
16433 specialization is a class-name.
16435 With concepts, this could also be a partial-concept-id that
16436 declares a non-type template parameter. */
16437 if (type_decl != NULL_TREE
16438 && TREE_CODE (type_decl) == TYPE_DECL
16439 && TYPE_DECL_ALIAS_P (type_decl))
16440 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16441 else if (is_constrained_parameter (type_decl))
16442 /* Don't do anything. */ ;
16444 cp_parser_simulate_error (parser);
16446 if (!cp_parser_parse_definitely (parser))
16447 /* ... Or a typedef-name or an enum-name. */
16448 return cp_parser_nonclass_name (parser);
16454 /* Check if DECL and ARGS can form a constrained-type-specifier.
16455 If ARGS is non-null, we try to form a concept check of the
16456 form DECL<?, ARGS> where ? is a wildcard that matches any
16457 kind of template argument. If ARGS is NULL, then we try to
16458 form a concept check of the form DECL<?>. */
16461 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16462 tree decl, tree args)
16464 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16466 /* If we a constrained-type-specifier cannot be deduced. */
16467 if (parser->prevent_constrained_type_specifiers)
16470 /* A constrained type specifier can only be found in an
16471 overload set or as a reference to a template declaration.
16473 FIXME: This might be masking a bug. It's possible that
16474 that the deduction below is causing template specializations
16475 to be formed with the wildcard as an argument. */
16476 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16479 /* Try to build a call expression that evaluates the
16480 concept. This can fail if the overload set refers
16481 only to non-templates. */
16482 tree placeholder = build_nt (WILDCARD_DECL);
16483 tree check = build_concept_check (decl, placeholder, args);
16484 if (check == error_mark_node)
16487 /* Deduce the checked constraint and the prototype parameter.
16489 FIXME: In certain cases, failure to deduce should be a
16490 diagnosable error. */
16493 if (!deduce_constrained_parameter (check, conc, proto))
16496 /* In template parameter scope, this results in a constrained
16497 parameter. Return a descriptor of that parm. */
16498 if (processing_template_parmlist)
16499 return build_constrained_parameter (conc, proto, args);
16501 /* In a parameter-declaration-clause, constrained-type
16502 specifiers result in invented template parameters. */
16503 if (parser->auto_is_implicit_function_template_parm_p)
16505 tree x = build_constrained_parameter (conc, proto, args);
16506 return synthesize_implicit_template_parm (parser, x);
16510 /* Otherwise, we're in a context where the constrained
16511 type name is deduced and the constraint applies
16512 after deduction. */
16513 return make_constrained_auto (conc, args);
16519 /* If DECL refers to a concept, return a TYPE_DECL representing
16520 the result of using the constrained type specifier in the
16521 current context. DECL refers to a concept if
16523 - it is an overload set containing a function concept taking a single
16526 - it is a variable concept taking a single type argument. */
16529 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16532 && (TREE_CODE (decl) == OVERLOAD
16533 || BASELINK_P (decl)
16534 || variable_concept_p (decl)))
16535 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16540 /* Check if DECL and ARGS form a partial-concept-id. If so,
16541 assign ID to the resulting constrained placeholder.
16543 Returns true if the partial-concept-id designates a placeholder
16544 and false otherwise. Note that *id is set to NULL_TREE in
16548 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16550 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16553 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16565 Returns a TYPE_DECL for the type. */
16568 cp_parser_nonclass_name (cp_parser* parser)
16573 cp_token *token = cp_lexer_peek_token (parser->lexer);
16574 identifier = cp_parser_identifier (parser);
16575 if (identifier == error_mark_node)
16576 return error_mark_node;
16578 /* Look up the type-name. */
16579 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16581 type_decl = strip_using_decl (type_decl);
16583 /* If we found an overload set, then it may refer to a concept-name. */
16584 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16587 if (TREE_CODE (type_decl) != TYPE_DECL
16588 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16590 /* See if this is an Objective-C type. */
16591 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16592 tree type = objc_get_protocol_qualified_type (identifier, protos);
16594 type_decl = TYPE_NAME (type);
16597 /* Issue an error if we did not find a type-name. */
16598 if (TREE_CODE (type_decl) != TYPE_DECL
16599 /* In Objective-C, we have the complication that class names are
16600 normally type names and start declarations (eg, the
16601 "NSObject" in "NSObject *object;"), but can be used in an
16602 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16603 is an expression. So, a classname followed by a dot is not a
16604 valid type-name. */
16605 || (objc_is_class_name (TREE_TYPE (type_decl))
16606 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16608 if (!cp_parser_simulate_error (parser))
16609 cp_parser_name_lookup_error (parser, identifier, type_decl,
16610 NLE_TYPE, token->location);
16611 return error_mark_node;
16613 /* Remember that the name was used in the definition of the
16614 current class so that we can check later to see if the
16615 meaning would have been different after the class was
16616 entirely defined. */
16617 else if (type_decl != error_mark_node
16619 maybe_note_name_used_in_class (identifier, type_decl);
16624 /* Parse an elaborated-type-specifier. Note that the grammar given
16625 here incorporates the resolution to DR68.
16627 elaborated-type-specifier:
16628 class-key :: [opt] nested-name-specifier [opt] identifier
16629 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16630 enum-key :: [opt] nested-name-specifier [opt] identifier
16631 typename :: [opt] nested-name-specifier identifier
16632 typename :: [opt] nested-name-specifier template [opt]
16637 elaborated-type-specifier:
16638 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16639 class-key attributes :: [opt] nested-name-specifier [opt]
16640 template [opt] template-id
16641 enum attributes :: [opt] nested-name-specifier [opt] identifier
16643 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16644 declared `friend'. If IS_DECLARATION is TRUE, then this
16645 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16646 something is being declared.
16648 Returns the TYPE specified. */
16651 cp_parser_elaborated_type_specifier (cp_parser* parser,
16653 bool is_declaration)
16655 enum tag_types tag_type;
16657 tree type = NULL_TREE;
16658 tree attributes = NULL_TREE;
16660 cp_token *token = NULL;
16662 /* See if we're looking at the `enum' keyword. */
16663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16665 /* Consume the `enum' token. */
16666 cp_lexer_consume_token (parser->lexer);
16667 /* Remember that it's an enumeration type. */
16668 tag_type = enum_type;
16669 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16670 enums) is used here. */
16671 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16672 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16674 pedwarn (input_location, 0, "elaborated-type-specifier "
16675 "for a scoped enum must not use the %<%D%> keyword",
16676 cp_lexer_peek_token (parser->lexer)->u.value);
16677 /* Consume the `struct' or `class' and parse it anyway. */
16678 cp_lexer_consume_token (parser->lexer);
16680 /* Parse the attributes. */
16681 attributes = cp_parser_attributes_opt (parser);
16683 /* Or, it might be `typename'. */
16684 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16687 /* Consume the `typename' token. */
16688 cp_lexer_consume_token (parser->lexer);
16689 /* Remember that it's a `typename' type. */
16690 tag_type = typename_type;
16692 /* Otherwise it must be a class-key. */
16695 tag_type = cp_parser_class_key (parser);
16696 if (tag_type == none_type)
16697 return error_mark_node;
16698 /* Parse the attributes. */
16699 attributes = cp_parser_attributes_opt (parser);
16702 /* Look for the `::' operator. */
16703 globalscope = cp_parser_global_scope_opt (parser,
16704 /*current_scope_valid_p=*/false);
16705 /* Look for the nested-name-specifier. */
16706 if (tag_type == typename_type && !globalscope)
16708 if (!cp_parser_nested_name_specifier (parser,
16709 /*typename_keyword_p=*/true,
16710 /*check_dependency_p=*/true,
16713 return error_mark_node;
16716 /* Even though `typename' is not present, the proposed resolution
16717 to Core Issue 180 says that in `class A<T>::B', `B' should be
16718 considered a type-name, even if `A<T>' is dependent. */
16719 cp_parser_nested_name_specifier_opt (parser,
16720 /*typename_keyword_p=*/true,
16721 /*check_dependency_p=*/true,
16724 /* For everything but enumeration types, consider a template-id.
16725 For an enumeration type, consider only a plain identifier. */
16726 if (tag_type != enum_type)
16728 bool template_p = false;
16731 /* Allow the `template' keyword. */
16732 template_p = cp_parser_optional_template_keyword (parser);
16733 /* If we didn't see `template', we don't know if there's a
16734 template-id or not. */
16736 cp_parser_parse_tentatively (parser);
16737 /* Parse the template-id. */
16738 token = cp_lexer_peek_token (parser->lexer);
16739 decl = cp_parser_template_id (parser, template_p,
16740 /*check_dependency_p=*/true,
16743 /* If we didn't find a template-id, look for an ordinary
16745 if (!template_p && !cp_parser_parse_definitely (parser))
16747 /* We can get here when cp_parser_template_id, called by
16748 cp_parser_class_name with tag_type == none_type, succeeds
16749 and caches a BASELINK. Then, when called again here,
16750 instead of failing and returning an error_mark_node
16751 returns it (see template/typename17.C in C++11).
16752 ??? Could we diagnose this earlier? */
16753 else if (tag_type == typename_type && BASELINK_P (decl))
16755 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16756 type = error_mark_node;
16758 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16759 in effect, then we must assume that, upon instantiation, the
16760 template will correspond to a class. */
16761 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16762 && tag_type == typename_type)
16763 type = make_typename_type (parser->scope, decl,
16765 /*complain=*/tf_error);
16766 /* If the `typename' keyword is in effect and DECL is not a type
16767 decl, then type is non existent. */
16768 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16770 else if (TREE_CODE (decl) == TYPE_DECL)
16771 type = check_elaborated_type_specifier (tag_type, decl,
16772 /*allow_template_p=*/true);
16773 else if (decl == error_mark_node)
16774 type = error_mark_node;
16779 token = cp_lexer_peek_token (parser->lexer);
16780 identifier = cp_parser_identifier (parser);
16782 if (identifier == error_mark_node)
16784 parser->scope = NULL_TREE;
16785 return error_mark_node;
16788 /* For a `typename', we needn't call xref_tag. */
16789 if (tag_type == typename_type
16790 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16791 return cp_parser_make_typename_type (parser, identifier,
16794 /* Template parameter lists apply only if we are not within a
16795 function parameter list. */
16796 bool template_parm_lists_apply
16797 = parser->num_template_parameter_lists;
16798 if (template_parm_lists_apply)
16799 for (cp_binding_level *s = current_binding_level;
16800 s && s->kind != sk_template_parms;
16801 s = s->level_chain)
16802 if (s->kind == sk_function_parms)
16803 template_parm_lists_apply = false;
16805 /* Look up a qualified name in the usual way. */
16809 tree ambiguous_decls;
16811 decl = cp_parser_lookup_name (parser, identifier,
16813 /*is_template=*/false,
16814 /*is_namespace=*/false,
16815 /*check_dependency=*/true,
16819 /* If the lookup was ambiguous, an error will already have been
16821 if (ambiguous_decls)
16822 return error_mark_node;
16824 /* If we are parsing friend declaration, DECL may be a
16825 TEMPLATE_DECL tree node here. However, we need to check
16826 whether this TEMPLATE_DECL results in valid code. Consider
16827 the following example:
16830 template <class T> class C {};
16833 template <class T> friend class N::C; // #1, valid code
16835 template <class T> class Y {
16836 friend class N::C; // #2, invalid code
16839 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16840 name lookup of `N::C'. We see that friend declaration must
16841 be template for the code to be valid. Note that
16842 processing_template_decl does not work here since it is
16843 always 1 for the above two cases. */
16845 decl = (cp_parser_maybe_treat_template_as_class
16846 (decl, /*tag_name_p=*/is_friend
16847 && template_parm_lists_apply));
16849 if (TREE_CODE (decl) != TYPE_DECL)
16851 cp_parser_diagnose_invalid_type_name (parser,
16854 return error_mark_node;
16857 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16859 bool allow_template = (template_parm_lists_apply
16860 || DECL_SELF_REFERENCE_P (decl));
16861 type = check_elaborated_type_specifier (tag_type, decl,
16864 if (type == error_mark_node)
16865 return error_mark_node;
16868 /* Forward declarations of nested types, such as
16873 are invalid unless all components preceding the final '::'
16874 are complete. If all enclosing types are complete, these
16875 declarations become merely pointless.
16877 Invalid forward declarations of nested types are errors
16878 caught elsewhere in parsing. Those that are pointless arrive
16881 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16882 && !is_friend && !processing_explicit_instantiation)
16883 warning (0, "declaration %qD does not declare anything", decl);
16885 type = TREE_TYPE (decl);
16889 /* An elaborated-type-specifier sometimes introduces a new type and
16890 sometimes names an existing type. Normally, the rule is that it
16891 introduces a new type only if there is not an existing type of
16892 the same name already in scope. For example, given:
16895 void f() { struct S s; }
16897 the `struct S' in the body of `f' is the same `struct S' as in
16898 the global scope; the existing definition is used. However, if
16899 there were no global declaration, this would introduce a new
16900 local class named `S'.
16902 An exception to this rule applies to the following code:
16904 namespace N { struct S; }
16906 Here, the elaborated-type-specifier names a new type
16907 unconditionally; even if there is already an `S' in the
16908 containing scope this declaration names a new type.
16909 This exception only applies if the elaborated-type-specifier
16910 forms the complete declaration:
16914 A declaration consisting solely of `class-key identifier ;' is
16915 either a redeclaration of the name in the current scope or a
16916 forward declaration of the identifier as a class name. It
16917 introduces the name into the current scope.
16919 We are in this situation precisely when the next token is a `;'.
16921 An exception to the exception is that a `friend' declaration does
16922 *not* name a new type; i.e., given:
16924 struct S { friend struct T; };
16926 `T' is not a new type in the scope of `S'.
16928 Also, `new struct S' or `sizeof (struct S)' never results in the
16929 definition of a new type; a new type can only be declared in a
16930 declaration context. */
16936 /* Friends have special name lookup rules. */
16937 ts = ts_within_enclosing_non_class;
16938 else if (is_declaration
16939 && cp_lexer_next_token_is (parser->lexer,
16941 /* This is a `class-key identifier ;' */
16947 (template_parm_lists_apply
16948 && (cp_parser_next_token_starts_class_definition_p (parser)
16949 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16950 /* An unqualified name was used to reference this type, so
16951 there were no qualifying templates. */
16952 if (template_parm_lists_apply
16953 && !cp_parser_check_template_parameters (parser,
16954 /*num_templates=*/0,
16956 /*declarator=*/NULL))
16957 return error_mark_node;
16958 type = xref_tag (tag_type, identifier, ts, template_p);
16962 if (type == error_mark_node)
16963 return error_mark_node;
16965 /* Allow attributes on forward declarations of classes. */
16968 if (TREE_CODE (type) == TYPENAME_TYPE)
16969 warning (OPT_Wattributes,
16970 "attributes ignored on uninstantiated type");
16971 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16972 && ! processing_explicit_instantiation)
16973 warning (OPT_Wattributes,
16974 "attributes ignored on template instantiation");
16975 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16976 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16978 warning (OPT_Wattributes,
16979 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16982 if (tag_type != enum_type)
16984 /* Indicate whether this class was declared as a `class' or as a
16986 if (CLASS_TYPE_P (type))
16987 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16988 cp_parser_check_class_key (tag_type, type);
16991 /* A "<" cannot follow an elaborated type specifier. If that
16992 happens, the user was probably trying to form a template-id. */
16993 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16999 /* Parse an enum-specifier.
17002 enum-head { enumerator-list [opt] }
17003 enum-head { enumerator-list , } [C++0x]
17006 enum-key identifier [opt] enum-base [opt]
17007 enum-key nested-name-specifier identifier enum-base [opt]
17012 enum struct [C++0x]
17015 : type-specifier-seq
17017 opaque-enum-specifier:
17018 enum-key identifier enum-base [opt] ;
17021 enum-key attributes[opt] identifier [opt] enum-base [opt]
17022 { enumerator-list [opt] }attributes[opt]
17023 enum-key attributes[opt] identifier [opt] enum-base [opt]
17024 { enumerator-list, }attributes[opt] [C++0x]
17026 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17027 if the token stream isn't an enum-specifier after all. */
17030 cp_parser_enum_specifier (cp_parser* parser)
17033 tree type = NULL_TREE;
17035 tree nested_name_specifier = NULL_TREE;
17037 bool scoped_enum_p = false;
17038 bool has_underlying_type = false;
17039 bool nested_being_defined = false;
17040 bool new_value_list = false;
17041 bool is_new_type = false;
17042 bool is_anonymous = false;
17043 tree underlying_type = NULL_TREE;
17044 cp_token *type_start_token = NULL;
17045 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17047 parser->colon_corrects_to_scope_p = false;
17049 /* Parse tentatively so that we can back up if we don't find a
17051 cp_parser_parse_tentatively (parser);
17053 /* Caller guarantees that the current token is 'enum', an identifier
17054 possibly follows, and the token after that is an opening brace.
17055 If we don't have an identifier, fabricate an anonymous name for
17056 the enumeration being defined. */
17057 cp_lexer_consume_token (parser->lexer);
17059 /* Parse the "class" or "struct", which indicates a scoped
17060 enumeration type in C++0x. */
17061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17062 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17064 if (cxx_dialect < cxx11)
17065 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17067 /* Consume the `struct' or `class' token. */
17068 cp_lexer_consume_token (parser->lexer);
17070 scoped_enum_p = true;
17073 attributes = cp_parser_attributes_opt (parser);
17075 /* Clear the qualification. */
17076 parser->scope = NULL_TREE;
17077 parser->qualifying_scope = NULL_TREE;
17078 parser->object_scope = NULL_TREE;
17080 /* Figure out in what scope the declaration is being placed. */
17081 prev_scope = current_scope ();
17083 type_start_token = cp_lexer_peek_token (parser->lexer);
17085 push_deferring_access_checks (dk_no_check);
17086 nested_name_specifier
17087 = cp_parser_nested_name_specifier_opt (parser,
17088 /*typename_keyword_p=*/true,
17089 /*check_dependency_p=*/false,
17091 /*is_declaration=*/false);
17093 if (nested_name_specifier)
17097 identifier = cp_parser_identifier (parser);
17098 name = cp_parser_lookup_name (parser, identifier,
17100 /*is_template=*/false,
17101 /*is_namespace=*/false,
17102 /*check_dependency=*/true,
17103 /*ambiguous_decls=*/NULL,
17105 if (name && name != error_mark_node)
17107 type = TREE_TYPE (name);
17108 if (TREE_CODE (type) == TYPENAME_TYPE)
17110 /* Are template enums allowed in ISO? */
17111 if (template_parm_scope_p ())
17112 pedwarn (type_start_token->location, OPT_Wpedantic,
17113 "%qD is an enumeration template", name);
17114 /* ignore a typename reference, for it will be solved by name
17119 else if (nested_name_specifier == error_mark_node)
17120 /* We already issued an error. */;
17123 error_at (type_start_token->location,
17124 "%qD does not name an enumeration in %qT",
17125 identifier, nested_name_specifier);
17126 nested_name_specifier = error_mark_node;
17131 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17132 identifier = cp_parser_identifier (parser);
17135 identifier = make_anon_name ();
17136 is_anonymous = true;
17138 error_at (type_start_token->location,
17139 "anonymous scoped enum is not allowed");
17142 pop_deferring_access_checks ();
17144 /* Check for the `:' that denotes a specified underlying type in C++0x.
17145 Note that a ':' could also indicate a bitfield width, however. */
17146 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17148 cp_decl_specifier_seq type_specifiers;
17150 /* Consume the `:'. */
17151 cp_lexer_consume_token (parser->lexer);
17153 /* Parse the type-specifier-seq. */
17154 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17155 /*is_trailing_return=*/false,
17158 /* At this point this is surely not elaborated type specifier. */
17159 if (!cp_parser_parse_definitely (parser))
17162 if (cxx_dialect < cxx11)
17163 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17165 has_underlying_type = true;
17167 /* If that didn't work, stop. */
17168 if (type_specifiers.type != error_mark_node)
17170 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17171 /*initialized=*/0, NULL);
17172 if (underlying_type == error_mark_node
17173 || check_for_bare_parameter_packs (underlying_type))
17174 underlying_type = NULL_TREE;
17178 /* Look for the `{' but don't consume it yet. */
17179 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17181 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17183 cp_parser_error (parser, "expected %<{%>");
17184 if (has_underlying_type)
17190 /* An opaque-enum-specifier must have a ';' here. */
17191 if ((scoped_enum_p || underlying_type)
17192 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17194 cp_parser_error (parser, "expected %<;%> or %<{%>");
17195 if (has_underlying_type)
17203 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17206 if (nested_name_specifier)
17208 if (CLASS_TYPE_P (nested_name_specifier))
17210 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17211 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17212 push_scope (nested_name_specifier);
17214 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17216 push_nested_namespace (nested_name_specifier);
17220 /* Issue an error message if type-definitions are forbidden here. */
17221 if (!cp_parser_check_type_definition (parser))
17222 type = error_mark_node;
17224 /* Create the new type. We do this before consuming the opening
17225 brace so the enum will be recorded as being on the line of its
17226 tag (or the 'enum' keyword, if there is no tag). */
17227 type = start_enum (identifier, type, underlying_type,
17228 attributes, scoped_enum_p, &is_new_type);
17230 /* If the next token is not '{' it is an opaque-enum-specifier or an
17231 elaborated-type-specifier. */
17232 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17234 timevar_push (TV_PARSE_ENUM);
17235 if (nested_name_specifier
17236 && nested_name_specifier != error_mark_node)
17238 /* The following catches invalid code such as:
17239 enum class S<int>::E { A, B, C }; */
17240 if (!processing_specialization
17241 && CLASS_TYPE_P (nested_name_specifier)
17242 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17243 error_at (type_start_token->location, "cannot add an enumerator "
17244 "list to a template instantiation");
17246 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17248 error_at (type_start_token->location,
17249 "%<%T::%E%> has not been declared",
17250 TYPE_CONTEXT (nested_name_specifier),
17251 nested_name_specifier);
17252 type = error_mark_node;
17254 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17255 && !CLASS_TYPE_P (nested_name_specifier))
17257 error_at (type_start_token->location, "nested name specifier "
17258 "%qT for enum declaration does not name a class "
17259 "or namespace", nested_name_specifier);
17260 type = error_mark_node;
17262 /* If that scope does not contain the scope in which the
17263 class was originally declared, the program is invalid. */
17264 else if (prev_scope && !is_ancestor (prev_scope,
17265 nested_name_specifier))
17267 if (at_namespace_scope_p ())
17268 error_at (type_start_token->location,
17269 "declaration of %qD in namespace %qD which does not "
17271 type, prev_scope, nested_name_specifier);
17273 error_at (type_start_token->location,
17274 "declaration of %qD in %qD which does not "
17276 type, prev_scope, nested_name_specifier);
17277 type = error_mark_node;
17282 begin_scope (sk_scoped_enum, type);
17284 /* Consume the opening brace. */
17285 cp_lexer_consume_token (parser->lexer);
17287 if (type == error_mark_node)
17288 ; /* Nothing to add */
17289 else if (OPAQUE_ENUM_P (type)
17290 || (cxx_dialect > cxx98 && processing_specialization))
17292 new_value_list = true;
17293 SET_OPAQUE_ENUM_P (type, false);
17294 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17298 error_at (type_start_token->location,
17299 "multiple definition of %q#T", type);
17300 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17301 "previous definition here");
17302 type = error_mark_node;
17305 if (type == error_mark_node)
17306 cp_parser_skip_to_end_of_block_or_statement (parser);
17307 /* If the next token is not '}', then there are some enumerators. */
17308 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17310 if (is_anonymous && !scoped_enum_p)
17311 pedwarn (type_start_token->location, OPT_Wpedantic,
17312 "ISO C++ forbids empty anonymous enum");
17315 cp_parser_enumerator_list (parser, type);
17317 /* Consume the final '}'. */
17318 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17322 timevar_pop (TV_PARSE_ENUM);
17326 /* If a ';' follows, then it is an opaque-enum-specifier
17327 and additional restrictions apply. */
17328 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17331 error_at (type_start_token->location,
17332 "opaque-enum-specifier without name");
17333 else if (nested_name_specifier)
17334 error_at (type_start_token->location,
17335 "opaque-enum-specifier must use a simple identifier");
17339 /* Look for trailing attributes to apply to this enumeration, and
17340 apply them if appropriate. */
17341 if (cp_parser_allow_gnu_extensions_p (parser))
17343 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17344 cplus_decl_attributes (&type,
17346 (int) ATTR_FLAG_TYPE_IN_PLACE);
17349 /* Finish up the enumeration. */
17350 if (type != error_mark_node)
17352 if (new_value_list)
17353 finish_enum_value_list (type);
17355 finish_enum (type);
17358 if (nested_name_specifier)
17360 if (CLASS_TYPE_P (nested_name_specifier))
17362 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17363 pop_scope (nested_name_specifier);
17365 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17367 pop_nested_namespace (nested_name_specifier);
17371 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17375 /* Parse an enumerator-list. The enumerators all have the indicated
17379 enumerator-definition
17380 enumerator-list , enumerator-definition */
17383 cp_parser_enumerator_list (cp_parser* parser, tree type)
17387 /* Parse an enumerator-definition. */
17388 cp_parser_enumerator_definition (parser, type);
17390 /* If the next token is not a ',', we've reached the end of
17392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17394 /* Otherwise, consume the `,' and keep going. */
17395 cp_lexer_consume_token (parser->lexer);
17396 /* If the next token is a `}', there is a trailing comma. */
17397 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17399 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17400 pedwarn (input_location, OPT_Wpedantic,
17401 "comma at end of enumerator list");
17407 /* Parse an enumerator-definition. The enumerator has the indicated
17410 enumerator-definition:
17412 enumerator = constant-expression
17419 enumerator-definition:
17420 enumerator attributes [opt]
17421 enumerator attributes [opt] = constant-expression */
17424 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17430 /* Save the input location because we are interested in the location
17431 of the identifier and not the location of the explicit value. */
17432 loc = cp_lexer_peek_token (parser->lexer)->location;
17434 /* Look for the identifier. */
17435 identifier = cp_parser_identifier (parser);
17436 if (identifier == error_mark_node)
17439 /* Parse any specified attributes. */
17440 tree attrs = cp_parser_attributes_opt (parser);
17442 /* If the next token is an '=', then there is an explicit value. */
17443 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17445 /* Consume the `=' token. */
17446 cp_lexer_consume_token (parser->lexer);
17447 /* Parse the value. */
17448 value = cp_parser_constant_expression (parser);
17453 /* If we are processing a template, make sure the initializer of the
17454 enumerator doesn't contain any bare template parameter pack. */
17455 if (check_for_bare_parameter_packs (value))
17456 value = error_mark_node;
17458 /* Create the enumerator. */
17459 build_enumerator (identifier, value, type, attrs, loc);
17462 /* Parse a namespace-name.
17465 original-namespace-name
17468 Returns the NAMESPACE_DECL for the namespace. */
17471 cp_parser_namespace_name (cp_parser* parser)
17474 tree namespace_decl;
17476 cp_token *token = cp_lexer_peek_token (parser->lexer);
17478 /* Get the name of the namespace. */
17479 identifier = cp_parser_identifier (parser);
17480 if (identifier == error_mark_node)
17481 return error_mark_node;
17483 /* Look up the identifier in the currently active scope. Look only
17484 for namespaces, due to:
17486 [basic.lookup.udir]
17488 When looking up a namespace-name in a using-directive or alias
17489 definition, only namespace names are considered.
17493 [basic.lookup.qual]
17495 During the lookup of a name preceding the :: scope resolution
17496 operator, object, function, and enumerator names are ignored.
17498 (Note that cp_parser_qualifying_entity only calls this
17499 function if the token after the name is the scope resolution
17501 namespace_decl = cp_parser_lookup_name (parser, identifier,
17503 /*is_template=*/false,
17504 /*is_namespace=*/true,
17505 /*check_dependency=*/true,
17506 /*ambiguous_decls=*/NULL,
17508 /* If it's not a namespace, issue an error. */
17509 if (namespace_decl == error_mark_node
17510 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17512 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17513 error_at (token->location, "%qD is not a namespace-name", identifier);
17514 cp_parser_error (parser, "expected namespace-name");
17515 namespace_decl = error_mark_node;
17518 return namespace_decl;
17521 /* Parse a namespace-definition.
17523 namespace-definition:
17524 named-namespace-definition
17525 unnamed-namespace-definition
17527 named-namespace-definition:
17528 original-namespace-definition
17529 extension-namespace-definition
17531 original-namespace-definition:
17532 namespace identifier { namespace-body }
17534 extension-namespace-definition:
17535 namespace original-namespace-name { namespace-body }
17537 unnamed-namespace-definition:
17538 namespace { namespace-body } */
17541 cp_parser_namespace_definition (cp_parser* parser)
17543 tree identifier, attribs;
17544 bool has_visibility;
17547 int nested_definition_count = 0;
17549 cp_ensure_no_omp_declare_simd (parser);
17550 cp_ensure_no_oacc_routine (parser);
17551 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17553 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17555 cp_lexer_consume_token (parser->lexer);
17560 /* Look for the `namespace' keyword. */
17561 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17563 /* Parse any specified attributes before the identifier. */
17564 attribs = cp_parser_attributes_opt (parser);
17566 /* Get the name of the namespace. We do not attempt to distinguish
17567 between an original-namespace-definition and an
17568 extension-namespace-definition at this point. The semantic
17569 analysis routines are responsible for that. */
17570 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17571 identifier = cp_parser_identifier (parser);
17573 identifier = NULL_TREE;
17575 /* Parse any specified attributes after the identifier. */
17576 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17577 if (post_ident_attribs)
17580 attribs = chainon (attribs, post_ident_attribs);
17582 attribs = post_ident_attribs;
17585 /* Start the namespace. */
17586 push_namespace (identifier);
17588 /* Parse any nested namespace definition. */
17589 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17592 error_at (token->location, "a nested namespace definition cannot have attributes");
17593 if (cxx_dialect < cxx1z)
17594 pedwarn (input_location, OPT_Wpedantic,
17595 "nested namespace definitions only available with "
17596 "-std=c++1z or -std=gnu++1z");
17598 error_at (token->location, "a nested namespace definition cannot be inline");
17599 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17601 cp_lexer_consume_token (parser->lexer);
17602 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17603 identifier = cp_parser_identifier (parser);
17606 cp_parser_error (parser, "nested identifier required");
17609 ++nested_definition_count;
17610 push_namespace (identifier);
17614 /* Look for the `{' to validate starting the namespace. */
17615 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17617 /* "inline namespace" is equivalent to a stub namespace definition
17618 followed by a strong using directive. */
17621 tree name_space = current_namespace;
17622 /* Set up namespace association. */
17623 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17624 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17625 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17626 /* Import the contents of the inline namespace. */
17628 do_using_directive (name_space);
17629 push_namespace (identifier);
17632 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17634 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17636 /* Parse the body of the namespace. */
17637 cp_parser_namespace_body (parser);
17639 if (has_visibility)
17640 pop_visibility (1);
17642 /* Finish the nested namespace definitions. */
17643 while (nested_definition_count--)
17646 /* Finish the namespace. */
17648 /* Look for the final `}'. */
17649 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17652 /* Parse a namespace-body.
17655 declaration-seq [opt] */
17658 cp_parser_namespace_body (cp_parser* parser)
17660 cp_parser_declaration_seq_opt (parser);
17663 /* Parse a namespace-alias-definition.
17665 namespace-alias-definition:
17666 namespace identifier = qualified-namespace-specifier ; */
17669 cp_parser_namespace_alias_definition (cp_parser* parser)
17672 tree namespace_specifier;
17674 cp_token *token = cp_lexer_peek_token (parser->lexer);
17676 /* Look for the `namespace' keyword. */
17677 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17678 /* Look for the identifier. */
17679 identifier = cp_parser_identifier (parser);
17680 if (identifier == error_mark_node)
17682 /* Look for the `=' token. */
17683 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17684 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17686 error_at (token->location, "%<namespace%> definition is not allowed here");
17687 /* Skip the definition. */
17688 cp_lexer_consume_token (parser->lexer);
17689 if (cp_parser_skip_to_closing_brace (parser))
17690 cp_lexer_consume_token (parser->lexer);
17693 cp_parser_require (parser, CPP_EQ, RT_EQ);
17694 /* Look for the qualified-namespace-specifier. */
17695 namespace_specifier
17696 = cp_parser_qualified_namespace_specifier (parser);
17697 /* Look for the `;' token. */
17698 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17700 /* Register the alias in the symbol table. */
17701 do_namespace_alias (identifier, namespace_specifier);
17704 /* Parse a qualified-namespace-specifier.
17706 qualified-namespace-specifier:
17707 :: [opt] nested-name-specifier [opt] namespace-name
17709 Returns a NAMESPACE_DECL corresponding to the specified
17713 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17715 /* Look for the optional `::'. */
17716 cp_parser_global_scope_opt (parser,
17717 /*current_scope_valid_p=*/false);
17719 /* Look for the optional nested-name-specifier. */
17720 cp_parser_nested_name_specifier_opt (parser,
17721 /*typename_keyword_p=*/false,
17722 /*check_dependency_p=*/true,
17724 /*is_declaration=*/true);
17726 return cp_parser_namespace_name (parser);
17729 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17730 access declaration.
17733 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17734 using :: unqualified-id ;
17736 access-declaration:
17742 cp_parser_using_declaration (cp_parser* parser,
17743 bool access_declaration_p)
17746 bool typename_p = false;
17747 bool global_scope_p;
17751 int oldcount = errorcount;
17752 cp_token *diag_token = NULL;
17754 if (access_declaration_p)
17756 diag_token = cp_lexer_peek_token (parser->lexer);
17757 cp_parser_parse_tentatively (parser);
17761 /* Look for the `using' keyword. */
17762 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17764 /* Peek at the next token. */
17765 token = cp_lexer_peek_token (parser->lexer);
17766 /* See if it's `typename'. */
17767 if (token->keyword == RID_TYPENAME)
17769 /* Remember that we've seen it. */
17771 /* Consume the `typename' token. */
17772 cp_lexer_consume_token (parser->lexer);
17776 /* Look for the optional global scope qualification. */
17778 = (cp_parser_global_scope_opt (parser,
17779 /*current_scope_valid_p=*/false)
17782 /* If we saw `typename', or didn't see `::', then there must be a
17783 nested-name-specifier present. */
17784 if (typename_p || !global_scope_p)
17786 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17787 /*check_dependency_p=*/true,
17789 /*is_declaration=*/true);
17790 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17792 cp_parser_skip_to_end_of_block_or_statement (parser);
17796 /* Otherwise, we could be in either of the two productions. In that
17797 case, treat the nested-name-specifier as optional. */
17799 qscope = cp_parser_nested_name_specifier_opt (parser,
17800 /*typename_keyword_p=*/false,
17801 /*check_dependency_p=*/true,
17803 /*is_declaration=*/true);
17805 qscope = global_namespace;
17806 else if (UNSCOPED_ENUM_P (qscope))
17807 qscope = CP_TYPE_CONTEXT (qscope);
17809 if (access_declaration_p && cp_parser_error_occurred (parser))
17810 /* Something has already gone wrong; there's no need to parse
17811 further. Since an error has occurred, the return value of
17812 cp_parser_parse_definitely will be false, as required. */
17813 return cp_parser_parse_definitely (parser);
17815 token = cp_lexer_peek_token (parser->lexer);
17816 /* Parse the unqualified-id. */
17817 identifier = cp_parser_unqualified_id (parser,
17818 /*template_keyword_p=*/false,
17819 /*check_dependency_p=*/true,
17820 /*declarator_p=*/true,
17821 /*optional_p=*/false);
17823 if (access_declaration_p)
17825 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17826 cp_parser_simulate_error (parser);
17827 if (!cp_parser_parse_definitely (parser))
17831 /* The function we call to handle a using-declaration is different
17832 depending on what scope we are in. */
17833 if (qscope == error_mark_node || identifier == error_mark_node)
17835 else if (!identifier_p (identifier)
17836 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17837 /* [namespace.udecl]
17839 A using declaration shall not name a template-id. */
17840 error_at (token->location,
17841 "a template-id may not appear in a using-declaration");
17844 if (at_class_scope_p ())
17846 /* Create the USING_DECL. */
17847 decl = do_class_using_decl (parser->scope, identifier);
17849 if (decl && typename_p)
17850 USING_DECL_TYPENAME_P (decl) = 1;
17852 if (check_for_bare_parameter_packs (decl))
17854 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17858 /* Add it to the list of members in this class. */
17859 finish_member_declaration (decl);
17863 decl = cp_parser_lookup_name_simple (parser,
17866 if (decl == error_mark_node)
17867 cp_parser_name_lookup_error (parser, identifier,
17870 else if (check_for_bare_parameter_packs (decl))
17872 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17875 else if (!at_namespace_scope_p ())
17876 do_local_using_decl (decl, qscope, identifier);
17878 do_toplevel_using_decl (decl, qscope, identifier);
17882 /* Look for the final `;'. */
17883 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17885 if (access_declaration_p && errorcount == oldcount)
17886 warning_at (diag_token->location, OPT_Wdeprecated,
17887 "access declarations are deprecated "
17888 "in favour of using-declarations; "
17889 "suggestion: add the %<using%> keyword");
17894 /* Parse an alias-declaration.
17897 using identifier attribute-specifier-seq [opt] = type-id */
17900 cp_parser_alias_declaration (cp_parser* parser)
17902 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17903 location_t id_location;
17904 cp_declarator *declarator;
17905 cp_decl_specifier_seq decl_specs;
17907 const char *saved_message = NULL;
17909 /* Look for the `using' keyword. */
17910 cp_token *using_token
17911 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17912 if (using_token == NULL)
17913 return error_mark_node;
17915 id_location = cp_lexer_peek_token (parser->lexer)->location;
17916 id = cp_parser_identifier (parser);
17917 if (id == error_mark_node)
17918 return error_mark_node;
17920 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17921 attributes = cp_parser_attributes_opt (parser);
17922 if (attributes == error_mark_node)
17923 return error_mark_node;
17925 cp_parser_require (parser, CPP_EQ, RT_EQ);
17927 if (cp_parser_error_occurred (parser))
17928 return error_mark_node;
17930 cp_parser_commit_to_tentative_parse (parser);
17932 /* Now we are going to parse the type-id of the declaration. */
17937 "A type-specifier-seq shall not define a class or enumeration
17938 unless it appears in the type-id of an alias-declaration (7.1.3) that
17939 is not the declaration of a template-declaration."
17941 In other words, if we currently are in an alias template, the
17942 type-id should not define a type.
17944 So let's set parser->type_definition_forbidden_message in that
17945 case; cp_parser_check_type_definition (called by
17946 cp_parser_class_specifier) will then emit an error if a type is
17947 defined in the type-id. */
17948 if (parser->num_template_parameter_lists)
17950 saved_message = parser->type_definition_forbidden_message;
17951 parser->type_definition_forbidden_message =
17952 G_("types may not be defined in alias template declarations");
17955 type = cp_parser_type_id (parser);
17957 /* Restore the error message if need be. */
17958 if (parser->num_template_parameter_lists)
17959 parser->type_definition_forbidden_message = saved_message;
17961 if (type == error_mark_node
17962 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17964 cp_parser_skip_to_end_of_block_or_statement (parser);
17965 return error_mark_node;
17968 /* A typedef-name can also be introduced by an alias-declaration. The
17969 identifier following the using keyword becomes a typedef-name. It has
17970 the same semantics as if it were introduced by the typedef
17971 specifier. In particular, it does not define a new type and it shall
17972 not appear in the type-id. */
17974 clear_decl_specs (&decl_specs);
17975 decl_specs.type = type;
17976 if (attributes != NULL_TREE)
17978 decl_specs.attributes = attributes;
17979 set_and_check_decl_spec_loc (&decl_specs,
17983 set_and_check_decl_spec_loc (&decl_specs,
17986 set_and_check_decl_spec_loc (&decl_specs,
17990 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17991 declarator->id_loc = id_location;
17993 member_p = at_class_scope_p ();
17995 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17996 NULL_TREE, attributes);
17998 decl = start_decl (declarator, &decl_specs, 0,
17999 attributes, NULL_TREE, &pushed_scope);
18000 if (decl == error_mark_node)
18003 // Attach constraints to the alias declaration.
18004 if (flag_concepts && current_template_parms)
18006 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18007 tree constr = build_constraints (reqs, NULL_TREE);
18008 set_constraints (decl, constr);
18011 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18014 pop_scope (pushed_scope);
18016 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18017 added into the symbol table; otherwise, return the TYPE_DECL. */
18018 if (DECL_LANG_SPECIFIC (decl)
18019 && DECL_TEMPLATE_INFO (decl)
18020 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18022 decl = DECL_TI_TEMPLATE (decl);
18024 check_member_template (decl);
18030 /* Parse a using-directive.
18033 using namespace :: [opt] nested-name-specifier [opt]
18034 namespace-name ; */
18037 cp_parser_using_directive (cp_parser* parser)
18039 tree namespace_decl;
18042 /* Look for the `using' keyword. */
18043 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18044 /* And the `namespace' keyword. */
18045 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18046 /* Look for the optional `::' operator. */
18047 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18048 /* And the optional nested-name-specifier. */
18049 cp_parser_nested_name_specifier_opt (parser,
18050 /*typename_keyword_p=*/false,
18051 /*check_dependency_p=*/true,
18053 /*is_declaration=*/true);
18054 /* Get the namespace being used. */
18055 namespace_decl = cp_parser_namespace_name (parser);
18056 /* And any specified attributes. */
18057 attribs = cp_parser_attributes_opt (parser);
18058 /* Update the symbol table. */
18059 parse_using_directive (namespace_decl, attribs);
18060 /* Look for the final `;'. */
18061 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18064 /* Parse an asm-definition.
18067 asm ( string-literal ) ;
18072 asm volatile [opt] ( string-literal ) ;
18073 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18074 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18075 : asm-operand-list [opt] ) ;
18076 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18077 : asm-operand-list [opt]
18078 : asm-clobber-list [opt] ) ;
18079 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18080 : asm-clobber-list [opt]
18081 : asm-goto-list ) ; */
18084 cp_parser_asm_definition (cp_parser* parser)
18087 tree outputs = NULL_TREE;
18088 tree inputs = NULL_TREE;
18089 tree clobbers = NULL_TREE;
18090 tree labels = NULL_TREE;
18092 bool volatile_p = false;
18093 bool extended_p = false;
18094 bool invalid_inputs_p = false;
18095 bool invalid_outputs_p = false;
18096 bool goto_p = false;
18097 required_token missing = RT_NONE;
18099 /* Look for the `asm' keyword. */
18100 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18102 if (parser->in_function_body
18103 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18105 error ("%<asm%> in %<constexpr%> function");
18106 cp_function_chain->invalid_constexpr = true;
18109 /* See if the next token is `volatile'. */
18110 if (cp_parser_allow_gnu_extensions_p (parser)
18111 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18113 /* Remember that we saw the `volatile' keyword. */
18115 /* Consume the token. */
18116 cp_lexer_consume_token (parser->lexer);
18118 if (cp_parser_allow_gnu_extensions_p (parser)
18119 && parser->in_function_body
18120 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18122 /* Remember that we saw the `goto' keyword. */
18124 /* Consume the token. */
18125 cp_lexer_consume_token (parser->lexer);
18127 /* Look for the opening `('. */
18128 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18130 /* Look for the string. */
18131 string = cp_parser_string_literal (parser, false, false);
18132 if (string == error_mark_node)
18134 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18135 /*consume_paren=*/true);
18139 /* If we're allowing GNU extensions, check for the extended assembly
18140 syntax. Unfortunately, the `:' tokens need not be separated by
18141 a space in C, and so, for compatibility, we tolerate that here
18142 too. Doing that means that we have to treat the `::' operator as
18144 if (cp_parser_allow_gnu_extensions_p (parser)
18145 && parser->in_function_body
18146 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18147 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18149 bool inputs_p = false;
18150 bool clobbers_p = false;
18151 bool labels_p = false;
18153 /* The extended syntax was used. */
18156 /* Look for outputs. */
18157 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18159 /* Consume the `:'. */
18160 cp_lexer_consume_token (parser->lexer);
18161 /* Parse the output-operands. */
18162 if (cp_lexer_next_token_is_not (parser->lexer,
18164 && cp_lexer_next_token_is_not (parser->lexer,
18166 && cp_lexer_next_token_is_not (parser->lexer,
18170 outputs = cp_parser_asm_operand_list (parser);
18171 if (outputs == error_mark_node)
18172 invalid_outputs_p = true;
18175 /* If the next token is `::', there are no outputs, and the
18176 next token is the beginning of the inputs. */
18177 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18178 /* The inputs are coming next. */
18181 /* Look for inputs. */
18183 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18185 /* Consume the `:' or `::'. */
18186 cp_lexer_consume_token (parser->lexer);
18187 /* Parse the output-operands. */
18188 if (cp_lexer_next_token_is_not (parser->lexer,
18190 && cp_lexer_next_token_is_not (parser->lexer,
18192 && cp_lexer_next_token_is_not (parser->lexer,
18195 inputs = cp_parser_asm_operand_list (parser);
18196 if (inputs == error_mark_node)
18197 invalid_inputs_p = true;
18200 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18201 /* The clobbers are coming next. */
18204 /* Look for clobbers. */
18206 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18209 /* Consume the `:' or `::'. */
18210 cp_lexer_consume_token (parser->lexer);
18211 /* Parse the clobbers. */
18212 if (cp_lexer_next_token_is_not (parser->lexer,
18214 && cp_lexer_next_token_is_not (parser->lexer,
18216 clobbers = cp_parser_asm_clobber_list (parser);
18219 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18220 /* The labels are coming next. */
18223 /* Look for labels. */
18225 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18228 /* Consume the `:' or `::'. */
18229 cp_lexer_consume_token (parser->lexer);
18230 /* Parse the labels. */
18231 labels = cp_parser_asm_label_list (parser);
18234 if (goto_p && !labels_p)
18235 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18238 missing = RT_COLON_SCOPE;
18240 /* Look for the closing `)'. */
18241 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18242 missing ? missing : RT_CLOSE_PAREN))
18243 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18244 /*consume_paren=*/true);
18245 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18247 if (!invalid_inputs_p && !invalid_outputs_p)
18249 /* Create the ASM_EXPR. */
18250 if (parser->in_function_body)
18252 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18253 inputs, clobbers, labels);
18254 /* If the extended syntax was not used, mark the ASM_EXPR. */
18257 tree temp = asm_stmt;
18258 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18259 temp = TREE_OPERAND (temp, 0);
18261 ASM_INPUT_P (temp) = 1;
18265 symtab->finalize_toplevel_asm (string);
18269 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18270 type that comes from the decl-specifier-seq. */
18273 strip_declarator_types (tree type, cp_declarator *declarator)
18275 for (cp_declarator *d = declarator; d;)
18284 if (TYPE_PTRMEMFUNC_P (type))
18285 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18286 type = TREE_TYPE (type);
18294 /* Declarators [gram.dcl.decl] */
18296 /* Parse an init-declarator.
18299 declarator initializer [opt]
18304 declarator asm-specification [opt] attributes [opt] initializer [opt]
18306 function-definition:
18307 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18309 decl-specifier-seq [opt] declarator function-try-block
18313 function-definition:
18314 __extension__ function-definition
18318 function-definition:
18319 decl-specifier-seq [opt] declarator function-transaction-block
18321 The DECL_SPECIFIERS apply to this declarator. Returns a
18322 representation of the entity declared. If MEMBER_P is TRUE, then
18323 this declarator appears in a class scope. The new DECL created by
18324 this declarator is returned.
18326 The CHECKS are access checks that should be performed once we know
18327 what entity is being declared (and, therefore, what classes have
18330 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18331 for a function-definition here as well. If the declarator is a
18332 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18333 be TRUE upon return. By that point, the function-definition will
18334 have been completely parsed.
18336 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18339 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18340 parsed declaration if it is an uninitialized single declarator not followed
18341 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18342 if present, will not be consumed. If returned, this declarator will be
18343 created with SD_INITIALIZED but will not call cp_finish_decl.
18345 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18346 and there is an initializer, the pointed location_t is set to the
18347 location of the '=' or `(', or '{' in C++11 token introducing the
18351 cp_parser_init_declarator (cp_parser* parser,
18352 cp_decl_specifier_seq *decl_specifiers,
18353 vec<deferred_access_check, va_gc> *checks,
18354 bool function_definition_allowed_p,
18356 int declares_class_or_enum,
18357 bool* function_definition_p,
18358 tree* maybe_range_for_decl,
18359 location_t* init_loc,
18362 cp_token *token = NULL, *asm_spec_start_token = NULL,
18363 *attributes_start_token = NULL;
18364 cp_declarator *declarator;
18365 tree prefix_attributes;
18366 tree attributes = NULL;
18367 tree asm_specification;
18369 tree decl = NULL_TREE;
18371 int is_initialized;
18372 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18373 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18375 enum cpp_ttype initialization_kind;
18376 bool is_direct_init = false;
18377 bool is_non_constant_init;
18378 int ctor_dtor_or_conv_p;
18379 bool friend_p = cp_parser_friend_p (decl_specifiers);
18380 tree pushed_scope = NULL_TREE;
18381 bool range_for_decl_p = false;
18382 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18383 location_t tmp_init_loc = UNKNOWN_LOCATION;
18385 /* Gather the attributes that were provided with the
18386 decl-specifiers. */
18387 prefix_attributes = decl_specifiers->attributes;
18389 /* Assume that this is not the declarator for a function
18391 if (function_definition_p)
18392 *function_definition_p = false;
18394 /* Default arguments are only permitted for function parameters. */
18395 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18396 parser->default_arg_ok_p = false;
18398 /* Defer access checks while parsing the declarator; we cannot know
18399 what names are accessible until we know what is being
18401 resume_deferring_access_checks ();
18403 /* Parse the declarator. */
18404 token = cp_lexer_peek_token (parser->lexer);
18406 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18407 &ctor_dtor_or_conv_p,
18408 /*parenthesized_p=*/NULL,
18409 member_p, friend_p);
18410 /* Gather up the deferred checks. */
18411 stop_deferring_access_checks ();
18413 parser->default_arg_ok_p = saved_default_arg_ok_p;
18415 /* If the DECLARATOR was erroneous, there's no need to go
18417 if (declarator == cp_error_declarator)
18418 return error_mark_node;
18420 /* Check that the number of template-parameter-lists is OK. */
18421 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18423 return error_mark_node;
18425 if (declares_class_or_enum & 2)
18426 cp_parser_check_for_definition_in_return_type (declarator,
18427 decl_specifiers->type,
18428 decl_specifiers->locations[ds_type_spec]);
18430 /* Figure out what scope the entity declared by the DECLARATOR is
18431 located in. `grokdeclarator' sometimes changes the scope, so
18432 we compute it now. */
18433 scope = get_scope_of_declarator (declarator);
18435 /* Perform any lookups in the declared type which were thought to be
18436 dependent, but are not in the scope of the declarator. */
18437 decl_specifiers->type
18438 = maybe_update_decl_type (decl_specifiers->type, scope);
18440 /* If we're allowing GNU extensions, look for an
18441 asm-specification. */
18442 if (cp_parser_allow_gnu_extensions_p (parser))
18444 /* Look for an asm-specification. */
18445 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18446 asm_specification = cp_parser_asm_specification_opt (parser);
18449 asm_specification = NULL_TREE;
18451 /* Look for attributes. */
18452 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18453 attributes = cp_parser_attributes_opt (parser);
18455 /* Peek at the next token. */
18456 token = cp_lexer_peek_token (parser->lexer);
18458 bool bogus_implicit_tmpl = false;
18460 if (function_declarator_p (declarator))
18462 /* Check to see if the token indicates the start of a
18463 function-definition. */
18464 if (cp_parser_token_starts_function_definition_p (token))
18466 if (!function_definition_allowed_p)
18468 /* If a function-definition should not appear here, issue an
18470 cp_parser_error (parser,
18471 "a function-definition is not allowed here");
18472 return error_mark_node;
18475 location_t func_brace_location
18476 = cp_lexer_peek_token (parser->lexer)->location;
18478 /* Neither attributes nor an asm-specification are allowed
18479 on a function-definition. */
18480 if (asm_specification)
18481 error_at (asm_spec_start_token->location,
18482 "an asm-specification is not allowed "
18483 "on a function-definition");
18485 error_at (attributes_start_token->location,
18486 "attributes are not allowed "
18487 "on a function-definition");
18488 /* This is a function-definition. */
18489 *function_definition_p = true;
18491 /* Parse the function definition. */
18493 decl = cp_parser_save_member_function_body (parser,
18496 prefix_attributes);
18499 (cp_parser_function_definition_from_specifiers_and_declarator
18500 (parser, decl_specifiers, prefix_attributes, declarator));
18502 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18504 /* This is where the prologue starts... */
18505 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18506 = func_brace_location;
18512 else if (parser->fully_implicit_function_template_p)
18514 /* A non-template declaration involving a function parameter list
18515 containing an implicit template parameter will be made into a
18516 template. If the resulting declaration is not going to be an
18517 actual function then finish the template scope here to prevent it.
18518 An error message will be issued once we have a decl to talk about.
18520 FIXME probably we should do type deduction rather than create an
18521 implicit template, but the standard currently doesn't allow it. */
18522 bogus_implicit_tmpl = true;
18523 finish_fully_implicit_template (parser, NULL_TREE);
18528 Only in function declarations for constructors, destructors, and
18529 type conversions can the decl-specifier-seq be omitted.
18531 We explicitly postpone this check past the point where we handle
18532 function-definitions because we tolerate function-definitions
18533 that are missing their return types in some modes. */
18534 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18536 cp_parser_error (parser,
18537 "expected constructor, destructor, or type conversion");
18538 return error_mark_node;
18541 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18542 if (token->type == CPP_EQ
18543 || token->type == CPP_OPEN_PAREN
18544 || token->type == CPP_OPEN_BRACE)
18546 is_initialized = SD_INITIALIZED;
18547 initialization_kind = token->type;
18548 if (maybe_range_for_decl)
18549 *maybe_range_for_decl = error_mark_node;
18550 tmp_init_loc = token->location;
18551 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18552 *init_loc = tmp_init_loc;
18554 if (token->type == CPP_EQ
18555 && function_declarator_p (declarator))
18557 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18558 if (t2->keyword == RID_DEFAULT)
18559 is_initialized = SD_DEFAULTED;
18560 else if (t2->keyword == RID_DELETE)
18561 is_initialized = SD_DELETED;
18566 /* If the init-declarator isn't initialized and isn't followed by a
18567 `,' or `;', it's not a valid init-declarator. */
18568 if (token->type != CPP_COMMA
18569 && token->type != CPP_SEMICOLON)
18571 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18572 range_for_decl_p = true;
18575 if (!maybe_range_for_decl)
18576 cp_parser_error (parser, "expected initializer");
18577 return error_mark_node;
18580 is_initialized = SD_UNINITIALIZED;
18581 initialization_kind = CPP_EOF;
18584 /* Because start_decl has side-effects, we should only call it if we
18585 know we're going ahead. By this point, we know that we cannot
18586 possibly be looking at any other construct. */
18587 cp_parser_commit_to_tentative_parse (parser);
18589 /* Enter the newly declared entry in the symbol table. If we're
18590 processing a declaration in a class-specifier, we wait until
18591 after processing the initializer. */
18594 if (parser->in_unbraced_linkage_specification_p)
18595 decl_specifiers->storage_class = sc_extern;
18596 decl = start_decl (declarator, decl_specifiers,
18597 range_for_decl_p? SD_INITIALIZED : is_initialized,
18598 attributes, prefix_attributes, &pushed_scope);
18599 cp_finalize_omp_declare_simd (parser, decl);
18600 cp_finalize_oacc_routine (parser, decl, false);
18601 /* Adjust location of decl if declarator->id_loc is more appropriate:
18602 set, and decl wasn't merged with another decl, in which case its
18603 location would be different from input_location, and more accurate. */
18605 && declarator->id_loc != UNKNOWN_LOCATION
18606 && DECL_SOURCE_LOCATION (decl) == input_location)
18607 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18610 /* Enter the SCOPE. That way unqualified names appearing in the
18611 initializer will be looked up in SCOPE. */
18612 pushed_scope = push_scope (scope);
18614 /* Perform deferred access control checks, now that we know in which
18615 SCOPE the declared entity resides. */
18616 if (!member_p && decl)
18618 tree saved_current_function_decl = NULL_TREE;
18620 /* If the entity being declared is a function, pretend that we
18621 are in its scope. If it is a `friend', it may have access to
18622 things that would not otherwise be accessible. */
18623 if (TREE_CODE (decl) == FUNCTION_DECL)
18625 saved_current_function_decl = current_function_decl;
18626 current_function_decl = decl;
18629 /* Perform access checks for template parameters. */
18630 cp_parser_perform_template_parameter_access_checks (checks);
18632 /* Perform the access control checks for the declarator and the
18633 decl-specifiers. */
18634 perform_deferred_access_checks (tf_warning_or_error);
18636 /* Restore the saved value. */
18637 if (TREE_CODE (decl) == FUNCTION_DECL)
18638 current_function_decl = saved_current_function_decl;
18641 /* Parse the initializer. */
18642 initializer = NULL_TREE;
18643 is_direct_init = false;
18644 is_non_constant_init = true;
18645 if (is_initialized)
18647 if (function_declarator_p (declarator))
18649 if (initialization_kind == CPP_EQ)
18650 initializer = cp_parser_pure_specifier (parser);
18653 /* If the declaration was erroneous, we don't really
18654 know what the user intended, so just silently
18655 consume the initializer. */
18656 if (decl != error_mark_node)
18657 error_at (tmp_init_loc, "initializer provided for function");
18658 cp_parser_skip_to_closing_parenthesis (parser,
18659 /*recovering=*/true,
18660 /*or_comma=*/false,
18661 /*consume_paren=*/true);
18666 /* We want to record the extra mangling scope for in-class
18667 initializers of class members and initializers of static data
18668 member templates. The former involves deferring
18669 parsing of the initializer until end of class as with default
18670 arguments. So right here we only handle the latter. */
18671 if (!member_p && processing_template_decl)
18672 start_lambda_scope (decl);
18673 initializer = cp_parser_initializer (parser,
18675 &is_non_constant_init);
18676 if (!member_p && processing_template_decl)
18677 finish_lambda_scope ();
18678 if (initializer == error_mark_node)
18679 cp_parser_skip_to_end_of_statement (parser);
18683 /* The old parser allows attributes to appear after a parenthesized
18684 initializer. Mark Mitchell proposed removing this functionality
18685 on the GCC mailing lists on 2002-08-13. This parser accepts the
18686 attributes -- but ignores them. */
18687 if (cp_parser_allow_gnu_extensions_p (parser)
18688 && initialization_kind == CPP_OPEN_PAREN)
18689 if (cp_parser_attributes_opt (parser))
18690 warning (OPT_Wattributes,
18691 "attributes after parenthesized initializer ignored");
18693 /* And now complain about a non-function implicit template. */
18694 if (bogus_implicit_tmpl && decl != error_mark_node)
18695 error_at (DECL_SOURCE_LOCATION (decl),
18696 "non-function %qD declared as implicit template", decl);
18698 /* For an in-class declaration, use `grokfield' to create the
18704 pop_scope (pushed_scope);
18705 pushed_scope = NULL_TREE;
18707 decl = grokfield (declarator, decl_specifiers,
18708 initializer, !is_non_constant_init,
18709 /*asmspec=*/NULL_TREE,
18710 chainon (attributes, prefix_attributes));
18711 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18712 cp_parser_save_default_args (parser, decl);
18713 cp_finalize_omp_declare_simd (parser, decl);
18714 cp_finalize_oacc_routine (parser, decl, false);
18717 /* Finish processing the declaration. But, skip member
18719 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18721 cp_finish_decl (decl,
18722 initializer, !is_non_constant_init,
18724 /* If the initializer is in parentheses, then this is
18725 a direct-initialization, which means that an
18726 `explicit' constructor is OK. Otherwise, an
18727 `explicit' constructor cannot be used. */
18728 ((is_direct_init || !is_initialized)
18729 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18731 else if ((cxx_dialect != cxx98) && friend_p
18732 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18733 /* Core issue #226 (C++0x only): A default template-argument
18734 shall not be specified in a friend class template
18736 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18737 /*is_partial=*/false, /*is_friend_decl=*/1);
18739 if (!friend_p && pushed_scope)
18740 pop_scope (pushed_scope);
18742 if (function_declarator_p (declarator)
18743 && parser->fully_implicit_function_template_p)
18746 decl = finish_fully_implicit_template (parser, decl);
18748 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18751 if (auto_result && is_initialized && decl_specifiers->type
18752 && type_uses_auto (decl_specifiers->type))
18753 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
18758 /* Parse a declarator.
18762 ptr-operator declarator
18764 abstract-declarator:
18765 ptr-operator abstract-declarator [opt]
18766 direct-abstract-declarator
18771 attributes [opt] direct-declarator
18772 attributes [opt] ptr-operator declarator
18774 abstract-declarator:
18775 attributes [opt] ptr-operator abstract-declarator [opt]
18776 attributes [opt] direct-abstract-declarator
18778 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18779 detect constructor, destructor or conversion operators. It is set
18780 to -1 if the declarator is a name, and +1 if it is a
18781 function. Otherwise it is set to zero. Usually you just want to
18782 test for >0, but internally the negative value is used.
18784 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18785 a decl-specifier-seq unless it declares a constructor, destructor,
18786 or conversion. It might seem that we could check this condition in
18787 semantic analysis, rather than parsing, but that makes it difficult
18788 to handle something like `f()'. We want to notice that there are
18789 no decl-specifiers, and therefore realize that this is an
18790 expression, not a declaration.)
18792 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18793 the declarator is a direct-declarator of the form "(...)".
18795 MEMBER_P is true iff this declarator is a member-declarator.
18797 FRIEND_P is true iff this declarator is a friend. */
18799 static cp_declarator *
18800 cp_parser_declarator (cp_parser* parser,
18801 cp_parser_declarator_kind dcl_kind,
18802 int* ctor_dtor_or_conv_p,
18803 bool* parenthesized_p,
18804 bool member_p, bool friend_p)
18806 cp_declarator *declarator;
18807 enum tree_code code;
18808 cp_cv_quals cv_quals;
18810 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18812 /* Assume this is not a constructor, destructor, or type-conversion
18814 if (ctor_dtor_or_conv_p)
18815 *ctor_dtor_or_conv_p = 0;
18817 if (cp_parser_allow_gnu_extensions_p (parser))
18818 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18820 /* Check for the ptr-operator production. */
18821 cp_parser_parse_tentatively (parser);
18822 /* Parse the ptr-operator. */
18823 code = cp_parser_ptr_operator (parser,
18828 /* If that worked, then we have a ptr-operator. */
18829 if (cp_parser_parse_definitely (parser))
18831 /* If a ptr-operator was found, then this declarator was not
18833 if (parenthesized_p)
18834 *parenthesized_p = true;
18835 /* The dependent declarator is optional if we are parsing an
18836 abstract-declarator. */
18837 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18838 cp_parser_parse_tentatively (parser);
18840 /* Parse the dependent declarator. */
18841 declarator = cp_parser_declarator (parser, dcl_kind,
18842 /*ctor_dtor_or_conv_p=*/NULL,
18843 /*parenthesized_p=*/NULL,
18844 /*member_p=*/false,
18847 /* If we are parsing an abstract-declarator, we must handle the
18848 case where the dependent declarator is absent. */
18849 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18850 && !cp_parser_parse_definitely (parser))
18853 declarator = cp_parser_make_indirect_declarator
18854 (code, class_type, cv_quals, declarator, std_attributes);
18856 /* Everything else is a direct-declarator. */
18859 if (parenthesized_p)
18860 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18862 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18863 ctor_dtor_or_conv_p,
18864 member_p, friend_p);
18867 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18868 declarator->attributes = gnu_attributes;
18872 /* Parse a direct-declarator or direct-abstract-declarator.
18876 direct-declarator ( parameter-declaration-clause )
18877 cv-qualifier-seq [opt]
18878 ref-qualifier [opt]
18879 exception-specification [opt]
18880 direct-declarator [ constant-expression [opt] ]
18883 direct-abstract-declarator:
18884 direct-abstract-declarator [opt]
18885 ( parameter-declaration-clause )
18886 cv-qualifier-seq [opt]
18887 ref-qualifier [opt]
18888 exception-specification [opt]
18889 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18890 ( abstract-declarator )
18892 Returns a representation of the declarator. DCL_KIND is
18893 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18894 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18895 we are parsing a direct-declarator. It is
18896 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18897 of ambiguity we prefer an abstract declarator, as per
18898 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18899 as for cp_parser_declarator. */
18901 static cp_declarator *
18902 cp_parser_direct_declarator (cp_parser* parser,
18903 cp_parser_declarator_kind dcl_kind,
18904 int* ctor_dtor_or_conv_p,
18905 bool member_p, bool friend_p)
18908 cp_declarator *declarator = NULL;
18909 tree scope = NULL_TREE;
18910 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18911 bool saved_in_declarator_p = parser->in_declarator_p;
18913 tree pushed_scope = NULL_TREE;
18917 /* Peek at the next token. */
18918 token = cp_lexer_peek_token (parser->lexer);
18919 if (token->type == CPP_OPEN_PAREN)
18921 /* This is either a parameter-declaration-clause, or a
18922 parenthesized declarator. When we know we are parsing a
18923 named declarator, it must be a parenthesized declarator
18924 if FIRST is true. For instance, `(int)' is a
18925 parameter-declaration-clause, with an omitted
18926 direct-abstract-declarator. But `((*))', is a
18927 parenthesized abstract declarator. Finally, when T is a
18928 template parameter `(T)' is a
18929 parameter-declaration-clause, and not a parenthesized
18932 We first try and parse a parameter-declaration-clause,
18933 and then try a nested declarator (if FIRST is true).
18935 It is not an error for it not to be a
18936 parameter-declaration-clause, even when FIRST is
18942 The first is the declaration of a function while the
18943 second is the definition of a variable, including its
18946 Having seen only the parenthesis, we cannot know which of
18947 these two alternatives should be selected. Even more
18948 complex are examples like:
18953 The former is a function-declaration; the latter is a
18954 variable initialization.
18956 Thus again, we try a parameter-declaration-clause, and if
18957 that fails, we back out and return. */
18959 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18962 bool is_declarator = false;
18964 /* In a member-declarator, the only valid interpretation
18965 of a parenthesis is the start of a
18966 parameter-declaration-clause. (It is invalid to
18967 initialize a static data member with a parenthesized
18968 initializer; only the "=" form of initialization is
18971 cp_parser_parse_tentatively (parser);
18973 /* Consume the `('. */
18974 cp_lexer_consume_token (parser->lexer);
18977 /* If this is going to be an abstract declarator, we're
18978 in a declarator and we can't have default args. */
18979 parser->default_arg_ok_p = false;
18980 parser->in_declarator_p = true;
18983 begin_scope (sk_function_parms, NULL_TREE);
18985 /* Parse the parameter-declaration-clause. */
18986 params = cp_parser_parameter_declaration_clause (parser);
18988 /* Consume the `)'. */
18989 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18991 /* If all went well, parse the cv-qualifier-seq,
18992 ref-qualifier and the exception-specification. */
18993 if (member_p || cp_parser_parse_definitely (parser))
18995 cp_cv_quals cv_quals;
18996 cp_virt_specifiers virt_specifiers;
18997 cp_ref_qualifier ref_qual;
18998 tree exception_specification;
19001 bool memfn = (member_p || (pushed_scope
19002 && CLASS_TYPE_P (pushed_scope)));
19004 is_declarator = true;
19006 if (ctor_dtor_or_conv_p)
19007 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19010 /* Parse the cv-qualifier-seq. */
19011 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19012 /* Parse the ref-qualifier. */
19013 ref_qual = cp_parser_ref_qualifier_opt (parser);
19014 /* Parse the tx-qualifier. */
19015 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19016 /* And the exception-specification. */
19017 exception_specification
19018 = cp_parser_exception_specification_opt (parser);
19020 attrs = cp_parser_std_attribute_spec_seq (parser);
19022 /* In here, we handle cases where attribute is used after
19023 the function declaration. For example:
19024 void func (int x) __attribute__((vector(..))); */
19025 tree gnu_attrs = NULL_TREE;
19027 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19029 cp_parser_parse_tentatively (parser);
19030 tree attr = cp_parser_gnu_attributes_opt (parser);
19031 if (cp_lexer_next_token_is_not (parser->lexer,
19033 && cp_lexer_next_token_is_not (parser->lexer,
19035 cp_parser_abort_tentative_parse (parser);
19036 else if (!cp_parser_parse_definitely (parser))
19041 tree requires_clause = NULL_TREE;
19042 late_return = (cp_parser_late_return_type_opt
19043 (parser, declarator, requires_clause,
19044 memfn ? cv_quals : -1));
19046 /* Parse the virt-specifier-seq. */
19047 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19049 /* Create the function-declarator. */
19050 declarator = make_call_declarator (declarator,
19056 exception_specification,
19059 declarator->std_attributes = attrs;
19060 declarator->attributes = gnu_attrs;
19061 /* Any subsequent parameter lists are to do with
19062 return type, so are not those of the declared
19064 parser->default_arg_ok_p = false;
19067 /* Remove the function parms from scope. */
19068 pop_bindings_and_leave_scope ();
19071 /* Repeat the main loop. */
19075 /* If this is the first, we can try a parenthesized
19079 bool saved_in_type_id_in_expr_p;
19081 parser->default_arg_ok_p = saved_default_arg_ok_p;
19082 parser->in_declarator_p = saved_in_declarator_p;
19084 /* Consume the `('. */
19085 cp_lexer_consume_token (parser->lexer);
19086 /* Parse the nested declarator. */
19087 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19088 parser->in_type_id_in_expr_p = true;
19090 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19091 /*parenthesized_p=*/NULL,
19092 member_p, friend_p);
19093 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19095 /* Expect a `)'. */
19096 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19097 declarator = cp_error_declarator;
19098 if (declarator == cp_error_declarator)
19101 goto handle_declarator;
19103 /* Otherwise, we must be done. */
19107 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19108 && token->type == CPP_OPEN_SQUARE
19109 && !cp_next_tokens_can_be_attribute_p (parser))
19111 /* Parse an array-declarator. */
19112 tree bounds, attrs;
19114 if (ctor_dtor_or_conv_p)
19115 *ctor_dtor_or_conv_p = 0;
19118 parser->default_arg_ok_p = false;
19119 parser->in_declarator_p = true;
19120 /* Consume the `['. */
19121 cp_lexer_consume_token (parser->lexer);
19122 /* Peek at the next token. */
19123 token = cp_lexer_peek_token (parser->lexer);
19124 /* If the next token is `]', then there is no
19125 constant-expression. */
19126 if (token->type != CPP_CLOSE_SQUARE)
19128 bool non_constant_p;
19130 = cp_parser_constant_expression (parser,
19131 /*allow_non_constant=*/true,
19133 if (!non_constant_p)
19135 else if (error_operand_p (bounds))
19136 /* Already gave an error. */;
19137 else if (!parser->in_function_body
19138 || current_binding_level->kind == sk_function_parms)
19140 /* Normally, the array bound must be an integral constant
19141 expression. However, as an extension, we allow VLAs
19142 in function scopes as long as they aren't part of a
19143 parameter declaration. */
19144 cp_parser_error (parser,
19145 "array bound is not an integer constant");
19146 bounds = error_mark_node;
19148 else if (processing_template_decl
19149 && !type_dependent_expression_p (bounds))
19151 /* Remember this wasn't a constant-expression. */
19152 bounds = build_nop (TREE_TYPE (bounds), bounds);
19153 TREE_SIDE_EFFECTS (bounds) = 1;
19157 bounds = NULL_TREE;
19158 /* Look for the closing `]'. */
19159 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19161 declarator = cp_error_declarator;
19165 attrs = cp_parser_std_attribute_spec_seq (parser);
19166 declarator = make_array_declarator (declarator, bounds);
19167 declarator->std_attributes = attrs;
19169 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19172 tree qualifying_scope;
19173 tree unqualified_name;
19175 special_function_kind sfk;
19177 bool pack_expansion_p = false;
19178 cp_token *declarator_id_start_token;
19180 /* Parse a declarator-id */
19181 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19184 cp_parser_parse_tentatively (parser);
19186 /* If we see an ellipsis, we should be looking at a
19188 if (token->type == CPP_ELLIPSIS)
19190 /* Consume the `...' */
19191 cp_lexer_consume_token (parser->lexer);
19193 pack_expansion_p = true;
19197 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19199 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19200 qualifying_scope = parser->scope;
19205 if (!unqualified_name && pack_expansion_p)
19207 /* Check whether an error occurred. */
19208 okay = !cp_parser_error_occurred (parser);
19210 /* We already consumed the ellipsis to mark a
19211 parameter pack, but we have no way to report it,
19212 so abort the tentative parse. We will be exiting
19213 immediately anyway. */
19214 cp_parser_abort_tentative_parse (parser);
19217 okay = cp_parser_parse_definitely (parser);
19220 unqualified_name = error_mark_node;
19221 else if (unqualified_name
19222 && (qualifying_scope
19223 || (!identifier_p (unqualified_name))))
19225 cp_parser_error (parser, "expected unqualified-id");
19226 unqualified_name = error_mark_node;
19230 if (!unqualified_name)
19232 if (unqualified_name == error_mark_node)
19234 declarator = cp_error_declarator;
19235 pack_expansion_p = false;
19236 declarator->parameter_pack_p = false;
19240 attrs = cp_parser_std_attribute_spec_seq (parser);
19242 if (qualifying_scope && at_namespace_scope_p ()
19243 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19245 /* In the declaration of a member of a template class
19246 outside of the class itself, the SCOPE will sometimes
19247 be a TYPENAME_TYPE. For example, given:
19249 template <typename T>
19250 int S<T>::R::i = 3;
19252 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19253 this context, we must resolve S<T>::R to an ordinary
19254 type, rather than a typename type.
19256 The reason we normally avoid resolving TYPENAME_TYPEs
19257 is that a specialization of `S' might render
19258 `S<T>::R' not a type. However, if `S' is
19259 specialized, then this `i' will not be used, so there
19260 is no harm in resolving the types here. */
19263 /* Resolve the TYPENAME_TYPE. */
19264 type = resolve_typename_type (qualifying_scope,
19265 /*only_current_p=*/false);
19266 /* If that failed, the declarator is invalid. */
19267 if (TREE_CODE (type) == TYPENAME_TYPE)
19269 if (typedef_variant_p (type))
19270 error_at (declarator_id_start_token->location,
19271 "cannot define member of dependent typedef "
19274 error_at (declarator_id_start_token->location,
19275 "%<%T::%E%> is not a type",
19276 TYPE_CONTEXT (qualifying_scope),
19277 TYPE_IDENTIFIER (qualifying_scope));
19279 qualifying_scope = type;
19284 if (unqualified_name)
19288 if (qualifying_scope
19289 && CLASS_TYPE_P (qualifying_scope))
19290 class_type = qualifying_scope;
19292 class_type = current_class_type;
19294 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19296 tree name_type = TREE_TYPE (unqualified_name);
19297 if (class_type && same_type_p (name_type, class_type))
19299 if (qualifying_scope
19300 && CLASSTYPE_USE_TEMPLATE (name_type))
19302 error_at (declarator_id_start_token->location,
19303 "invalid use of constructor as a template");
19304 inform (declarator_id_start_token->location,
19305 "use %<%T::%D%> instead of %<%T::%D%> to "
19306 "name the constructor in a qualified name",
19308 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19309 class_type, name_type);
19310 declarator = cp_error_declarator;
19314 unqualified_name = constructor_name (class_type);
19318 /* We do not attempt to print the declarator
19319 here because we do not have enough
19320 information about its original syntactic
19322 cp_parser_error (parser, "invalid declarator");
19323 declarator = cp_error_declarator;
19330 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19331 sfk = sfk_destructor;
19332 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19333 sfk = sfk_conversion;
19334 else if (/* There's no way to declare a constructor
19335 for an anonymous type, even if the type
19336 got a name for linkage purposes. */
19337 !TYPE_WAS_ANONYMOUS (class_type)
19338 /* Handle correctly (c++/19200):
19352 friend void N::S();
19355 && class_type != qualifying_scope)
19356 && constructor_name_p (unqualified_name,
19359 unqualified_name = constructor_name (class_type);
19360 sfk = sfk_constructor;
19362 else if (is_overloaded_fn (unqualified_name)
19363 && DECL_CONSTRUCTOR_P (get_first_fn
19364 (unqualified_name)))
19365 sfk = sfk_constructor;
19367 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19368 *ctor_dtor_or_conv_p = -1;
19371 declarator = make_id_declarator (qualifying_scope,
19374 declarator->std_attributes = attrs;
19375 declarator->id_loc = token->location;
19376 declarator->parameter_pack_p = pack_expansion_p;
19378 if (pack_expansion_p)
19379 maybe_warn_variadic_templates ();
19382 handle_declarator:;
19383 scope = get_scope_of_declarator (declarator);
19386 /* Any names that appear after the declarator-id for a
19387 member are looked up in the containing scope. */
19388 if (at_function_scope_p ())
19390 /* But declarations with qualified-ids can't appear in a
19392 cp_parser_error (parser, "qualified-id in declaration");
19393 declarator = cp_error_declarator;
19396 pushed_scope = push_scope (scope);
19398 parser->in_declarator_p = true;
19399 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19400 || (declarator && declarator->kind == cdk_id))
19401 /* Default args are only allowed on function
19403 parser->default_arg_ok_p = saved_default_arg_ok_p;
19405 parser->default_arg_ok_p = false;
19414 /* For an abstract declarator, we might wind up with nothing at this
19415 point. That's an error; the declarator is not optional. */
19417 cp_parser_error (parser, "expected declarator");
19419 /* If we entered a scope, we must exit it now. */
19421 pop_scope (pushed_scope);
19423 parser->default_arg_ok_p = saved_default_arg_ok_p;
19424 parser->in_declarator_p = saved_in_declarator_p;
19429 /* Parse a ptr-operator.
19432 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19433 * cv-qualifier-seq [opt]
19435 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19436 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19441 & cv-qualifier-seq [opt]
19443 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19444 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19445 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19446 filled in with the TYPE containing the member. *CV_QUALS is
19447 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19448 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19449 Note that the tree codes returned by this function have nothing
19450 to do with the types of trees that will be eventually be created
19451 to represent the pointer or reference type being parsed. They are
19452 just constants with suggestive names. */
19453 static enum tree_code
19454 cp_parser_ptr_operator (cp_parser* parser,
19456 cp_cv_quals *cv_quals,
19459 enum tree_code code = ERROR_MARK;
19461 tree attrs = NULL_TREE;
19463 /* Assume that it's not a pointer-to-member. */
19465 /* And that there are no cv-qualifiers. */
19466 *cv_quals = TYPE_UNQUALIFIED;
19468 /* Peek at the next token. */
19469 token = cp_lexer_peek_token (parser->lexer);
19471 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19472 if (token->type == CPP_MULT)
19473 code = INDIRECT_REF;
19474 else if (token->type == CPP_AND)
19476 else if ((cxx_dialect != cxx98) &&
19477 token->type == CPP_AND_AND) /* C++0x only */
19478 code = NON_LVALUE_EXPR;
19480 if (code != ERROR_MARK)
19482 /* Consume the `*', `&' or `&&'. */
19483 cp_lexer_consume_token (parser->lexer);
19485 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19486 `&', if we are allowing GNU extensions. (The only qualifier
19487 that can legally appear after `&' is `restrict', but that is
19488 enforced during semantic analysis. */
19489 if (code == INDIRECT_REF
19490 || cp_parser_allow_gnu_extensions_p (parser))
19491 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19493 attrs = cp_parser_std_attribute_spec_seq (parser);
19494 if (attributes != NULL)
19495 *attributes = attrs;
19499 /* Try the pointer-to-member case. */
19500 cp_parser_parse_tentatively (parser);
19501 /* Look for the optional `::' operator. */
19502 cp_parser_global_scope_opt (parser,
19503 /*current_scope_valid_p=*/false);
19504 /* Look for the nested-name specifier. */
19505 token = cp_lexer_peek_token (parser->lexer);
19506 cp_parser_nested_name_specifier (parser,
19507 /*typename_keyword_p=*/false,
19508 /*check_dependency_p=*/true,
19510 /*is_declaration=*/false);
19511 /* If we found it, and the next token is a `*', then we are
19512 indeed looking at a pointer-to-member operator. */
19513 if (!cp_parser_error_occurred (parser)
19514 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19516 /* Indicate that the `*' operator was used. */
19517 code = INDIRECT_REF;
19519 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19520 error_at (token->location, "%qD is a namespace", parser->scope);
19521 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19522 error_at (token->location, "cannot form pointer to member of "
19523 "non-class %q#T", parser->scope);
19526 /* The type of which the member is a member is given by the
19528 *type = parser->scope;
19529 /* The next name will not be qualified. */
19530 parser->scope = NULL_TREE;
19531 parser->qualifying_scope = NULL_TREE;
19532 parser->object_scope = NULL_TREE;
19533 /* Look for optional c++11 attributes. */
19534 attrs = cp_parser_std_attribute_spec_seq (parser);
19535 if (attributes != NULL)
19536 *attributes = attrs;
19537 /* Look for the optional cv-qualifier-seq. */
19538 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19541 /* If that didn't work we don't have a ptr-operator. */
19542 if (!cp_parser_parse_definitely (parser))
19543 cp_parser_error (parser, "expected ptr-operator");
19549 /* Parse an (optional) cv-qualifier-seq.
19552 cv-qualifier cv-qualifier-seq [opt]
19563 Returns a bitmask representing the cv-qualifiers. */
19566 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19568 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19573 cp_cv_quals cv_qualifier;
19575 /* Peek at the next token. */
19576 token = cp_lexer_peek_token (parser->lexer);
19577 /* See if it's a cv-qualifier. */
19578 switch (token->keyword)
19581 cv_qualifier = TYPE_QUAL_CONST;
19585 cv_qualifier = TYPE_QUAL_VOLATILE;
19589 cv_qualifier = TYPE_QUAL_RESTRICT;
19593 cv_qualifier = TYPE_UNQUALIFIED;
19600 if (cv_quals & cv_qualifier)
19602 error_at (token->location, "duplicate cv-qualifier");
19603 cp_lexer_purge_token (parser->lexer);
19607 cp_lexer_consume_token (parser->lexer);
19608 cv_quals |= cv_qualifier;
19615 /* Parse an (optional) ref-qualifier
19621 Returns cp_ref_qualifier representing ref-qualifier. */
19623 static cp_ref_qualifier
19624 cp_parser_ref_qualifier_opt (cp_parser* parser)
19626 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19628 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19629 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19634 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19635 cp_token *token = cp_lexer_peek_token (parser->lexer);
19637 switch (token->type)
19640 curr_ref_qual = REF_QUAL_LVALUE;
19644 curr_ref_qual = REF_QUAL_RVALUE;
19648 curr_ref_qual = REF_QUAL_NONE;
19652 if (!curr_ref_qual)
19656 error_at (token->location, "multiple ref-qualifiers");
19657 cp_lexer_purge_token (parser->lexer);
19661 ref_qual = curr_ref_qual;
19662 cp_lexer_consume_token (parser->lexer);
19669 /* Parse an optional tx-qualifier.
19673 transaction_safe_dynamic */
19676 cp_parser_tx_qualifier_opt (cp_parser *parser)
19678 cp_token *token = cp_lexer_peek_token (parser->lexer);
19679 if (token->type == CPP_NAME)
19681 tree name = token->u.value;
19682 const char *p = IDENTIFIER_POINTER (name);
19683 const int len = strlen ("transaction_safe");
19684 if (!strncmp (p, "transaction_safe", len))
19688 || !strcmp (p, "_dynamic"))
19690 cp_lexer_consume_token (parser->lexer);
19693 error ("%E requires %<-fgnu-tm%>", name);
19704 /* Parse an (optional) virt-specifier-seq.
19706 virt-specifier-seq:
19707 virt-specifier virt-specifier-seq [opt]
19713 Returns a bitmask representing the virt-specifiers. */
19715 static cp_virt_specifiers
19716 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19718 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19723 cp_virt_specifiers virt_specifier;
19725 /* Peek at the next token. */
19726 token = cp_lexer_peek_token (parser->lexer);
19727 /* See if it's a virt-specifier-qualifier. */
19728 if (token->type != CPP_NAME)
19730 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19732 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19733 virt_specifier = VIRT_SPEC_OVERRIDE;
19735 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19737 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19738 virt_specifier = VIRT_SPEC_FINAL;
19740 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19742 virt_specifier = VIRT_SPEC_FINAL;
19747 if (virt_specifiers & virt_specifier)
19749 error_at (token->location, "duplicate virt-specifier");
19750 cp_lexer_purge_token (parser->lexer);
19754 cp_lexer_consume_token (parser->lexer);
19755 virt_specifiers |= virt_specifier;
19758 return virt_specifiers;
19761 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19762 is in scope even though it isn't real. */
19765 inject_this_parameter (tree ctype, cp_cv_quals quals)
19769 if (current_class_ptr)
19771 /* We don't clear this between NSDMIs. Is it already what we want? */
19772 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19773 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19774 && cp_type_quals (type) == quals)
19778 this_parm = build_this_parm (ctype, quals);
19779 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19780 current_class_ptr = NULL_TREE;
19782 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19783 current_class_ptr = this_parm;
19786 /* Return true iff our current scope is a non-static data member
19790 parsing_nsdmi (void)
19792 /* We recognize NSDMI context by the context-less 'this' pointer set up
19793 by the function above. */
19794 if (current_class_ptr
19795 && TREE_CODE (current_class_ptr) == PARM_DECL
19796 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19801 /* Parse a late-specified return type, if any. This is not a separate
19802 non-terminal, but part of a function declarator, which looks like
19804 -> trailing-type-specifier-seq abstract-declarator(opt)
19806 Returns the type indicated by the type-id.
19808 In addition to this, parse any queued up omp declare simd
19809 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19811 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19815 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19816 tree& requires_clause, cp_cv_quals quals)
19819 tree type = NULL_TREE;
19820 bool declare_simd_p = (parser->omp_declare_simd
19822 && declarator->kind == cdk_id);
19824 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19825 && declarator && declarator->kind == cdk_id);
19827 bool oacc_routine_p = (parser->oacc_routine
19829 && declarator->kind == cdk_id);
19831 /* Peek at the next token. */
19832 token = cp_lexer_peek_token (parser->lexer);
19833 /* A late-specified return type is indicated by an initial '->'. */
19834 if (token->type != CPP_DEREF
19835 && token->keyword != RID_REQUIRES
19836 && !(token->type == CPP_NAME
19837 && token->u.value == ridpointers[RID_REQUIRES])
19838 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19841 tree save_ccp = current_class_ptr;
19842 tree save_ccr = current_class_ref;
19845 /* DR 1207: 'this' is in scope in the trailing return type. */
19846 inject_this_parameter (current_class_type, quals);
19849 if (token->type == CPP_DEREF)
19851 /* Consume the ->. */
19852 cp_lexer_consume_token (parser->lexer);
19854 type = cp_parser_trailing_type_id (parser);
19857 /* Function declarations may be followed by a trailing
19858 requires-clause. */
19859 requires_clause = cp_parser_requires_clause_opt (parser);
19861 if (cilk_simd_fn_vector_p)
19862 declarator->attributes
19863 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19864 declarator->attributes);
19865 if (declare_simd_p)
19866 declarator->attributes
19867 = cp_parser_late_parsing_omp_declare_simd (parser,
19868 declarator->attributes);
19869 if (oacc_routine_p)
19870 declarator->attributes
19871 = cp_parser_late_parsing_oacc_routine (parser,
19872 declarator->attributes);
19876 current_class_ptr = save_ccp;
19877 current_class_ref = save_ccr;
19883 /* Parse a declarator-id.
19887 :: [opt] nested-name-specifier [opt] type-name
19889 In the `id-expression' case, the value returned is as for
19890 cp_parser_id_expression if the id-expression was an unqualified-id.
19891 If the id-expression was a qualified-id, then a SCOPE_REF is
19892 returned. The first operand is the scope (either a NAMESPACE_DECL
19893 or TREE_TYPE), but the second is still just a representation of an
19897 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19900 /* The expression must be an id-expression. Assume that qualified
19901 names are the names of types so that:
19904 int S<T>::R::i = 3;
19906 will work; we must treat `S<T>::R' as the name of a type.
19907 Similarly, assume that qualified names are templates, where
19911 int S<T>::R<T>::i = 3;
19914 id = cp_parser_id_expression (parser,
19915 /*template_keyword_p=*/false,
19916 /*check_dependency_p=*/false,
19917 /*template_p=*/NULL,
19918 /*declarator_p=*/true,
19920 if (id && BASELINK_P (id))
19921 id = BASELINK_FUNCTIONS (id);
19925 /* Parse a type-id.
19928 type-specifier-seq abstract-declarator [opt]
19930 Returns the TYPE specified. */
19933 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19934 bool is_trailing_return)
19936 cp_decl_specifier_seq type_specifier_seq;
19937 cp_declarator *abstract_declarator;
19939 /* Parse the type-specifier-seq. */
19940 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19941 is_trailing_return,
19942 &type_specifier_seq);
19943 if (type_specifier_seq.type == error_mark_node)
19944 return error_mark_node;
19946 /* There might or might not be an abstract declarator. */
19947 cp_parser_parse_tentatively (parser);
19948 /* Look for the declarator. */
19949 abstract_declarator
19950 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19951 /*parenthesized_p=*/NULL,
19952 /*member_p=*/false,
19953 /*friend_p=*/false);
19954 /* Check to see if there really was a declarator. */
19955 if (!cp_parser_parse_definitely (parser))
19956 abstract_declarator = NULL;
19958 if (type_specifier_seq.type
19959 /* The concepts TS allows 'auto' as a type-id. */
19960 && (!flag_concepts || parser->in_type_id_in_expr_p)
19961 /* None of the valid uses of 'auto' in C++14 involve the type-id
19962 nonterminal, but it is valid in a trailing-return-type. */
19963 && !(cxx_dialect >= cxx14 && is_trailing_return)
19964 && type_uses_auto (type_specifier_seq.type))
19966 /* A type-id with type 'auto' is only ok if the abstract declarator
19967 is a function declarator with a late-specified return type.
19969 A type-id with 'auto' is also valid in a trailing-return-type
19970 in a compound-requirement. */
19971 if (abstract_declarator
19972 && abstract_declarator->kind == cdk_function
19973 && abstract_declarator->u.function.late_return_type)
19975 else if (parser->in_result_type_constraint_p)
19979 error ("invalid use of %<auto%>");
19980 return error_mark_node;
19984 return groktypename (&type_specifier_seq, abstract_declarator,
19989 cp_parser_type_id (cp_parser *parser)
19991 return cp_parser_type_id_1 (parser, false, false);
19995 cp_parser_template_type_arg (cp_parser *parser)
19998 const char *saved_message = parser->type_definition_forbidden_message;
19999 parser->type_definition_forbidden_message
20000 = G_("types may not be defined in template arguments");
20001 r = cp_parser_type_id_1 (parser, true, false);
20002 parser->type_definition_forbidden_message = saved_message;
20003 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20005 error ("invalid use of %<auto%> in template argument");
20006 r = error_mark_node;
20012 cp_parser_trailing_type_id (cp_parser *parser)
20014 return cp_parser_type_id_1 (parser, false, true);
20017 /* Parse a type-specifier-seq.
20019 type-specifier-seq:
20020 type-specifier type-specifier-seq [opt]
20024 type-specifier-seq:
20025 attributes type-specifier-seq [opt]
20027 If IS_DECLARATION is true, we are at the start of a "condition" or
20028 exception-declaration, so we might be followed by a declarator-id.
20030 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20031 i.e. we've just seen "->".
20033 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20036 cp_parser_type_specifier_seq (cp_parser* parser,
20037 bool is_declaration,
20038 bool is_trailing_return,
20039 cp_decl_specifier_seq *type_specifier_seq)
20041 bool seen_type_specifier = false;
20042 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20043 cp_token *start_token = NULL;
20045 /* Clear the TYPE_SPECIFIER_SEQ. */
20046 clear_decl_specs (type_specifier_seq);
20048 /* In the context of a trailing return type, enum E { } is an
20049 elaborated-type-specifier followed by a function-body, not an
20051 if (is_trailing_return)
20052 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20054 /* Parse the type-specifiers and attributes. */
20057 tree type_specifier;
20058 bool is_cv_qualifier;
20060 /* Check for attributes first. */
20061 if (cp_next_tokens_can_be_attribute_p (parser))
20063 type_specifier_seq->attributes =
20064 chainon (type_specifier_seq->attributes,
20065 cp_parser_attributes_opt (parser));
20069 /* record the token of the beginning of the type specifier seq,
20070 for error reporting purposes*/
20072 start_token = cp_lexer_peek_token (parser->lexer);
20074 /* Look for the type-specifier. */
20075 type_specifier = cp_parser_type_specifier (parser,
20077 type_specifier_seq,
20078 /*is_declaration=*/false,
20081 if (!type_specifier)
20083 /* If the first type-specifier could not be found, this is not a
20084 type-specifier-seq at all. */
20085 if (!seen_type_specifier)
20087 /* Set in_declarator_p to avoid skipping to the semicolon. */
20088 int in_decl = parser->in_declarator_p;
20089 parser->in_declarator_p = true;
20091 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20092 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20093 cp_parser_error (parser, "expected type-specifier");
20095 parser->in_declarator_p = in_decl;
20097 type_specifier_seq->type = error_mark_node;
20100 /* If subsequent type-specifiers could not be found, the
20101 type-specifier-seq is complete. */
20105 seen_type_specifier = true;
20106 /* The standard says that a condition can be:
20108 type-specifier-seq declarator = assignment-expression
20115 we should treat the "S" as a declarator, not as a
20116 type-specifier. The standard doesn't say that explicitly for
20117 type-specifier-seq, but it does say that for
20118 decl-specifier-seq in an ordinary declaration. Perhaps it
20119 would be clearer just to allow a decl-specifier-seq here, and
20120 then add a semantic restriction that if any decl-specifiers
20121 that are not type-specifiers appear, the program is invalid. */
20122 if (is_declaration && !is_cv_qualifier)
20123 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20127 /* Return whether the function currently being declared has an associated
20128 template parameter list. */
20131 function_being_declared_is_template_p (cp_parser* parser)
20133 if (!current_template_parms || processing_template_parmlist)
20136 if (parser->implicit_template_scope)
20139 if (at_class_scope_p ()
20140 && TYPE_BEING_DEFINED (current_class_type))
20141 return parser->num_template_parameter_lists != 0;
20143 return ((int) parser->num_template_parameter_lists > template_class_depth
20144 (current_class_type));
20147 /* Parse a parameter-declaration-clause.
20149 parameter-declaration-clause:
20150 parameter-declaration-list [opt] ... [opt]
20151 parameter-declaration-list , ...
20153 Returns a representation for the parameter declarations. A return
20154 value of NULL indicates a parameter-declaration-clause consisting
20155 only of an ellipsis. */
20158 cp_parser_parameter_declaration_clause (cp_parser* parser)
20167 int auto_is_implicit_function_template_parm_p;
20169 parser->auto_is_implicit_function_template_parm_p
20170 = auto_is_implicit_function_template_parm_p;
20172 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20176 if (!processing_specialization
20177 && !processing_template_parmlist
20178 && !processing_explicit_instantiation)
20179 if (!current_function_decl
20180 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20181 parser->auto_is_implicit_function_template_parm_p = true;
20183 /* Peek at the next token. */
20184 token = cp_lexer_peek_token (parser->lexer);
20185 /* Check for trivial parameter-declaration-clauses. */
20186 if (token->type == CPP_ELLIPSIS)
20188 /* Consume the `...' token. */
20189 cp_lexer_consume_token (parser->lexer);
20192 else if (token->type == CPP_CLOSE_PAREN)
20193 /* There are no parameters. */
20195 #ifndef NO_IMPLICIT_EXTERN_C
20196 if (in_system_header_at (input_location)
20197 && current_class_type == NULL
20198 && current_lang_name == lang_name_c)
20202 return void_list_node;
20204 /* Check for `(void)', too, which is a special case. */
20205 else if (token->keyword == RID_VOID
20206 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20207 == CPP_CLOSE_PAREN))
20209 /* Consume the `void' token. */
20210 cp_lexer_consume_token (parser->lexer);
20211 /* There are no parameters. */
20212 return void_list_node;
20215 /* Parse the parameter-declaration-list. */
20216 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20217 /* If a parse error occurred while parsing the
20218 parameter-declaration-list, then the entire
20219 parameter-declaration-clause is erroneous. */
20223 /* Peek at the next token. */
20224 token = cp_lexer_peek_token (parser->lexer);
20225 /* If it's a `,', the clause should terminate with an ellipsis. */
20226 if (token->type == CPP_COMMA)
20228 /* Consume the `,'. */
20229 cp_lexer_consume_token (parser->lexer);
20230 /* Expect an ellipsis. */
20232 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20234 /* It might also be `...' if the optional trailing `,' was
20236 else if (token->type == CPP_ELLIPSIS)
20238 /* Consume the `...' token. */
20239 cp_lexer_consume_token (parser->lexer);
20240 /* And remember that we saw it. */
20244 ellipsis_p = false;
20246 /* Finish the parameter list. */
20248 parameters = chainon (parameters, void_list_node);
20253 /* Parse a parameter-declaration-list.
20255 parameter-declaration-list:
20256 parameter-declaration
20257 parameter-declaration-list , parameter-declaration
20259 Returns a representation of the parameter-declaration-list, as for
20260 cp_parser_parameter_declaration_clause. However, the
20261 `void_list_node' is never appended to the list. Upon return,
20262 *IS_ERROR will be true iff an error occurred. */
20265 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20267 tree parameters = NULL_TREE;
20268 tree *tail = ¶meters;
20269 bool saved_in_unbraced_linkage_specification_p;
20272 /* Assume all will go well. */
20274 /* The special considerations that apply to a function within an
20275 unbraced linkage specifications do not apply to the parameters
20276 to the function. */
20277 saved_in_unbraced_linkage_specification_p
20278 = parser->in_unbraced_linkage_specification_p;
20279 parser->in_unbraced_linkage_specification_p = false;
20281 /* Look for more parameters. */
20284 cp_parameter_declarator *parameter;
20285 tree decl = error_mark_node;
20286 bool parenthesized_p = false;
20287 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20288 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20289 (current_template_parms)) : 0);
20291 /* Parse the parameter. */
20293 = cp_parser_parameter_declaration (parser,
20294 /*template_parm_p=*/false,
20297 /* We don't know yet if the enclosing context is deprecated, so wait
20298 and warn in grokparms if appropriate. */
20299 deprecated_state = DEPRECATED_SUPPRESS;
20303 /* If a function parameter pack was specified and an implicit template
20304 parameter was introduced during cp_parser_parameter_declaration,
20305 change any implicit parameters introduced into packs. */
20306 if (parser->implicit_template_parms
20307 && parameter->declarator
20308 && parameter->declarator->parameter_pack_p)
20310 int latest_template_parm_idx = TREE_VEC_LENGTH
20311 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20313 if (latest_template_parm_idx != template_parm_idx)
20314 parameter->decl_specifiers.type = convert_generic_types_to_packs
20315 (parameter->decl_specifiers.type,
20316 template_parm_idx, latest_template_parm_idx);
20319 decl = grokdeclarator (parameter->declarator,
20320 ¶meter->decl_specifiers,
20322 parameter->default_argument != NULL_TREE,
20323 ¶meter->decl_specifiers.attributes);
20326 deprecated_state = DEPRECATED_NORMAL;
20328 /* If a parse error occurred parsing the parameter declaration,
20329 then the entire parameter-declaration-list is erroneous. */
20330 if (decl == error_mark_node)
20333 parameters = error_mark_node;
20337 if (parameter->decl_specifiers.attributes)
20338 cplus_decl_attributes (&decl,
20339 parameter->decl_specifiers.attributes,
20341 if (DECL_NAME (decl))
20342 decl = pushdecl (decl);
20344 if (decl != error_mark_node)
20346 retrofit_lang_decl (decl);
20347 DECL_PARM_INDEX (decl) = ++index;
20348 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20351 /* Add the new parameter to the list. */
20352 *tail = build_tree_list (parameter->default_argument, decl);
20353 tail = &TREE_CHAIN (*tail);
20355 /* Peek at the next token. */
20356 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20357 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20358 /* These are for Objective-C++ */
20359 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20360 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20361 /* The parameter-declaration-list is complete. */
20363 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20367 /* Peek at the next token. */
20368 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20369 /* If it's an ellipsis, then the list is complete. */
20370 if (token->type == CPP_ELLIPSIS)
20372 /* Otherwise, there must be more parameters. Consume the
20374 cp_lexer_consume_token (parser->lexer);
20375 /* When parsing something like:
20377 int i(float f, double d)
20379 we can tell after seeing the declaration for "f" that we
20380 are not looking at an initialization of a variable "i",
20381 but rather at the declaration of a function "i".
20383 Due to the fact that the parsing of template arguments
20384 (as specified to a template-id) requires backtracking we
20385 cannot use this technique when inside a template argument
20387 if (!parser->in_template_argument_list_p
20388 && !parser->in_type_id_in_expr_p
20389 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20390 /* However, a parameter-declaration of the form
20391 "float(f)" (which is a valid declaration of a
20392 parameter "f") can also be interpreted as an
20393 expression (the conversion of "f" to "float"). */
20394 && !parenthesized_p)
20395 cp_parser_commit_to_tentative_parse (parser);
20399 cp_parser_error (parser, "expected %<,%> or %<...%>");
20400 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20401 cp_parser_skip_to_closing_parenthesis (parser,
20402 /*recovering=*/true,
20403 /*or_comma=*/false,
20404 /*consume_paren=*/false);
20409 parser->in_unbraced_linkage_specification_p
20410 = saved_in_unbraced_linkage_specification_p;
20412 /* Reset implicit_template_scope if we are about to leave the function
20413 parameter list that introduced it. Note that for out-of-line member
20414 definitions, there will be one or more class scopes before we get to
20415 the template parameter scope. */
20417 if (cp_binding_level *its = parser->implicit_template_scope)
20418 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20420 while (maybe_its->kind == sk_class)
20421 maybe_its = maybe_its->level_chain;
20422 if (maybe_its == its)
20424 parser->implicit_template_parms = 0;
20425 parser->implicit_template_scope = 0;
20432 /* Parse a parameter declaration.
20434 parameter-declaration:
20435 decl-specifier-seq ... [opt] declarator
20436 decl-specifier-seq declarator = assignment-expression
20437 decl-specifier-seq ... [opt] abstract-declarator [opt]
20438 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20440 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20441 declares a template parameter. (In that case, a non-nested `>'
20442 token encountered during the parsing of the assignment-expression
20443 is not interpreted as a greater-than operator.)
20445 Returns a representation of the parameter, or NULL if an error
20446 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20447 true iff the declarator is of the form "(p)". */
20449 static cp_parameter_declarator *
20450 cp_parser_parameter_declaration (cp_parser *parser,
20451 bool template_parm_p,
20452 bool *parenthesized_p)
20454 int declares_class_or_enum;
20455 cp_decl_specifier_seq decl_specifiers;
20456 cp_declarator *declarator;
20457 tree default_argument;
20458 cp_token *token = NULL, *declarator_token_start = NULL;
20459 const char *saved_message;
20460 bool template_parameter_pack_p = false;
20462 /* In a template parameter, `>' is not an operator.
20466 When parsing a default template-argument for a non-type
20467 template-parameter, the first non-nested `>' is taken as the end
20468 of the template parameter-list rather than a greater-than
20471 /* Type definitions may not appear in parameter types. */
20472 saved_message = parser->type_definition_forbidden_message;
20473 parser->type_definition_forbidden_message
20474 = G_("types may not be defined in parameter types");
20476 /* Parse the declaration-specifiers. */
20477 cp_parser_decl_specifier_seq (parser,
20478 CP_PARSER_FLAGS_NONE,
20480 &declares_class_or_enum);
20482 /* Complain about missing 'typename' or other invalid type names. */
20483 if (!decl_specifiers.any_type_specifiers_p
20484 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20485 decl_specifiers.type = error_mark_node;
20487 /* If an error occurred, there's no reason to attempt to parse the
20488 rest of the declaration. */
20489 if (cp_parser_error_occurred (parser))
20491 parser->type_definition_forbidden_message = saved_message;
20495 /* Peek at the next token. */
20496 token = cp_lexer_peek_token (parser->lexer);
20498 /* If the next token is a `)', `,', `=', `>', or `...', then there
20499 is no declarator. However, when variadic templates are enabled,
20500 there may be a declarator following `...'. */
20501 if (token->type == CPP_CLOSE_PAREN
20502 || token->type == CPP_COMMA
20503 || token->type == CPP_EQ
20504 || token->type == CPP_GREATER)
20507 if (parenthesized_p)
20508 *parenthesized_p = false;
20510 /* Otherwise, there should be a declarator. */
20513 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20514 parser->default_arg_ok_p = false;
20516 /* After seeing a decl-specifier-seq, if the next token is not a
20517 "(", there is no possibility that the code is a valid
20518 expression. Therefore, if parsing tentatively, we commit at
20520 if (!parser->in_template_argument_list_p
20521 /* In an expression context, having seen:
20525 we cannot be sure whether we are looking at a
20526 function-type (taking a "char" as a parameter) or a cast
20527 of some object of type "char" to "int". */
20528 && !parser->in_type_id_in_expr_p
20529 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20530 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20531 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20532 cp_parser_commit_to_tentative_parse (parser);
20533 /* Parse the declarator. */
20534 declarator_token_start = token;
20535 declarator = cp_parser_declarator (parser,
20536 CP_PARSER_DECLARATOR_EITHER,
20537 /*ctor_dtor_or_conv_p=*/NULL,
20539 /*member_p=*/false,
20540 /*friend_p=*/false);
20541 parser->default_arg_ok_p = saved_default_arg_ok_p;
20542 /* After the declarator, allow more attributes. */
20543 decl_specifiers.attributes
20544 = chainon (decl_specifiers.attributes,
20545 cp_parser_attributes_opt (parser));
20547 /* If the declarator is a template parameter pack, remember that and
20548 clear the flag in the declarator itself so we don't get errors
20549 from grokdeclarator. */
20550 if (template_parm_p && declarator && declarator->parameter_pack_p)
20552 declarator->parameter_pack_p = false;
20553 template_parameter_pack_p = true;
20557 /* If the next token is an ellipsis, and we have not seen a declarator
20558 name, and if either the type of the declarator contains parameter
20559 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20560 for, eg, abbreviated integral type names), then we actually have a
20561 parameter pack expansion expression. Otherwise, leave the ellipsis
20562 for a C-style variadic function. */
20563 token = cp_lexer_peek_token (parser->lexer);
20564 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20566 tree type = decl_specifiers.type;
20568 if (type && DECL_P (type))
20569 type = TREE_TYPE (type);
20572 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20573 && (template_parm_p || uses_parameter_packs (type)))
20574 || (!type && template_parm_p))
20575 && declarator_can_be_parameter_pack (declarator))
20577 /* Consume the `...'. */
20578 cp_lexer_consume_token (parser->lexer);
20579 maybe_warn_variadic_templates ();
20581 /* Build a pack expansion type */
20582 if (template_parm_p)
20583 template_parameter_pack_p = true;
20584 else if (declarator)
20585 declarator->parameter_pack_p = true;
20587 decl_specifiers.type = make_pack_expansion (type);
20591 /* The restriction on defining new types applies only to the type
20592 of the parameter, not to the default argument. */
20593 parser->type_definition_forbidden_message = saved_message;
20595 /* If the next token is `=', then process a default argument. */
20596 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20598 tree type = decl_specifiers.type;
20599 token = cp_lexer_peek_token (parser->lexer);
20600 /* If we are defining a class, then the tokens that make up the
20601 default argument must be saved and processed later. */
20602 if (!template_parm_p && at_class_scope_p ()
20603 && TYPE_BEING_DEFINED (current_class_type)
20604 && !LAMBDA_TYPE_P (current_class_type))
20605 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20607 // A constrained-type-specifier may declare a type template-parameter.
20608 else if (declares_constrained_type_template_parameter (type))
20610 = cp_parser_default_type_template_argument (parser);
20612 // A constrained-type-specifier may declare a template-template-parameter.
20613 else if (declares_constrained_template_template_parameter (type))
20615 = cp_parser_default_template_template_argument (parser);
20617 /* Outside of a class definition, we can just parse the
20618 assignment-expression. */
20621 = cp_parser_default_argument (parser, template_parm_p);
20623 if (!parser->default_arg_ok_p)
20625 permerror (token->location,
20626 "default arguments are only "
20627 "permitted for function parameters");
20629 else if ((declarator && declarator->parameter_pack_p)
20630 || template_parameter_pack_p
20631 || (decl_specifiers.type
20632 && PACK_EXPANSION_P (decl_specifiers.type)))
20634 /* Find the name of the parameter pack. */
20635 cp_declarator *id_declarator = declarator;
20636 while (id_declarator && id_declarator->kind != cdk_id)
20637 id_declarator = id_declarator->declarator;
20639 if (id_declarator && id_declarator->kind == cdk_id)
20640 error_at (declarator_token_start->location,
20642 ? G_("template parameter pack %qD "
20643 "cannot have a default argument")
20644 : G_("parameter pack %qD cannot have "
20645 "a default argument"),
20646 id_declarator->u.id.unqualified_name);
20648 error_at (declarator_token_start->location,
20650 ? G_("template parameter pack cannot have "
20651 "a default argument")
20652 : G_("parameter pack cannot have a "
20653 "default argument"));
20655 default_argument = NULL_TREE;
20659 default_argument = NULL_TREE;
20661 return make_parameter_declarator (&decl_specifiers,
20664 template_parameter_pack_p);
20667 /* Parse a default argument and return it.
20669 TEMPLATE_PARM_P is true if this is a default argument for a
20670 non-type template parameter. */
20672 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20674 tree default_argument = NULL_TREE;
20675 bool saved_greater_than_is_operator_p;
20676 bool saved_local_variables_forbidden_p;
20677 bool non_constant_p, is_direct_init;
20679 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20681 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20682 parser->greater_than_is_operator_p = !template_parm_p;
20683 /* Local variable names (and the `this' keyword) may not
20684 appear in a default argument. */
20685 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20686 parser->local_variables_forbidden_p = true;
20687 /* Parse the assignment-expression. */
20688 if (template_parm_p)
20689 push_deferring_access_checks (dk_no_deferred);
20690 tree saved_class_ptr = NULL_TREE;
20691 tree saved_class_ref = NULL_TREE;
20692 /* The "this" pointer is not valid in a default argument. */
20695 saved_class_ptr = current_class_ptr;
20696 cp_function_chain->x_current_class_ptr = NULL_TREE;
20697 saved_class_ref = current_class_ref;
20698 cp_function_chain->x_current_class_ref = NULL_TREE;
20701 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20702 /* Restore the "this" pointer. */
20705 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20706 cp_function_chain->x_current_class_ref = saved_class_ref;
20708 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20709 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20710 if (template_parm_p)
20711 pop_deferring_access_checks ();
20712 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20713 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20715 return default_argument;
20718 /* Parse a function-body.
20721 compound_statement */
20724 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20726 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20727 ? BCS_TRY_BLOCK : BCS_NORMAL),
20731 /* Parse a ctor-initializer-opt followed by a function-body. Return
20732 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20733 is true we are parsing a function-try-block. */
20736 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20737 bool in_function_try_block)
20740 bool ctor_initializer_p;
20741 const bool check_body_p =
20742 DECL_CONSTRUCTOR_P (current_function_decl)
20743 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20746 /* Begin the function body. */
20747 body = begin_function_body ();
20748 /* Parse the optional ctor-initializer. */
20749 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20751 /* If we're parsing a constexpr constructor definition, we need
20752 to check that the constructor body is indeed empty. However,
20753 before we get to cp_parser_function_body lot of junk has been
20754 generated, so we can't just check that we have an empty block.
20755 Rather we take a snapshot of the outermost block, and check whether
20756 cp_parser_function_body changed its state. */
20759 list = cur_stmt_list;
20760 if (STATEMENT_LIST_TAIL (list))
20761 last = STATEMENT_LIST_TAIL (list)->stmt;
20763 /* Parse the function-body. */
20764 cp_parser_function_body (parser, in_function_try_block);
20766 check_constexpr_ctor_body (last, list, /*complain=*/true);
20767 /* Finish the function body. */
20768 finish_function_body (body);
20770 return ctor_initializer_p;
20773 /* Parse an initializer.
20776 = initializer-clause
20777 ( expression-list )
20779 Returns an expression representing the initializer. If no
20780 initializer is present, NULL_TREE is returned.
20782 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20783 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20784 set to TRUE if there is no initializer present. If there is an
20785 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20786 is set to true; otherwise it is set to false. */
20789 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20790 bool* non_constant_p)
20795 /* Peek at the next token. */
20796 token = cp_lexer_peek_token (parser->lexer);
20798 /* Let our caller know whether or not this initializer was
20800 *is_direct_init = (token->type != CPP_EQ);
20801 /* Assume that the initializer is constant. */
20802 *non_constant_p = false;
20804 if (token->type == CPP_EQ)
20806 /* Consume the `='. */
20807 cp_lexer_consume_token (parser->lexer);
20808 /* Parse the initializer-clause. */
20809 init = cp_parser_initializer_clause (parser, non_constant_p);
20811 else if (token->type == CPP_OPEN_PAREN)
20813 vec<tree, va_gc> *vec;
20814 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20816 /*allow_expansion_p=*/true,
20819 return error_mark_node;
20820 init = build_tree_list_vec (vec);
20821 release_tree_vector (vec);
20823 else if (token->type == CPP_OPEN_BRACE)
20825 cp_lexer_set_source_position (parser->lexer);
20826 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20827 init = cp_parser_braced_list (parser, non_constant_p);
20828 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20832 /* Anything else is an error. */
20833 cp_parser_error (parser, "expected initializer");
20834 init = error_mark_node;
20840 /* Parse an initializer-clause.
20842 initializer-clause:
20843 assignment-expression
20846 Returns an expression representing the initializer.
20848 If the `assignment-expression' production is used the value
20849 returned is simply a representation for the expression.
20851 Otherwise, calls cp_parser_braced_list. */
20854 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20856 cp_expr initializer;
20858 /* Assume the expression is constant. */
20859 *non_constant_p = false;
20861 /* If it is not a `{', then we are looking at an
20862 assignment-expression. */
20863 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20866 = cp_parser_constant_expression (parser,
20867 /*allow_non_constant_p=*/true,
20871 initializer = cp_parser_braced_list (parser, non_constant_p);
20873 return initializer;
20876 /* Parse a brace-enclosed initializer list.
20879 { initializer-list , [opt] }
20882 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20883 the elements of the initializer-list (or NULL, if the last
20884 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20885 NULL_TREE. There is no way to detect whether or not the optional
20886 trailing `,' was provided. NON_CONSTANT_P is as for
20887 cp_parser_initializer. */
20890 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20893 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20895 /* Consume the `{' token. */
20896 cp_lexer_consume_token (parser->lexer);
20897 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20898 initializer = make_node (CONSTRUCTOR);
20899 /* If it's not a `}', then there is a non-trivial initializer. */
20900 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20902 /* Parse the initializer list. */
20903 CONSTRUCTOR_ELTS (initializer)
20904 = cp_parser_initializer_list (parser, non_constant_p);
20905 /* A trailing `,' token is allowed. */
20906 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20907 cp_lexer_consume_token (parser->lexer);
20910 *non_constant_p = false;
20911 /* Now, there should be a trailing `}'. */
20912 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20913 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20914 TREE_TYPE (initializer) = init_list_type_node;
20916 cp_expr result (initializer);
20917 /* Build a location of the form:
20920 with caret==start at the open brace, finish at the close brace. */
20921 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20922 result.set_location (combined_loc);
20926 /* Consume tokens up to, and including, the next non-nested closing `]'.
20927 Returns true iff we found a closing `]'. */
20930 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20932 unsigned square_depth = 0;
20936 cp_token * token = cp_lexer_peek_token (parser->lexer);
20938 switch (token->type)
20941 case CPP_PRAGMA_EOL:
20942 /* If we've run out of tokens, then there is no closing `]'. */
20945 case CPP_OPEN_SQUARE:
20949 case CPP_CLOSE_SQUARE:
20950 if (!square_depth--)
20952 cp_lexer_consume_token (parser->lexer);
20961 /* Consume the token. */
20962 cp_lexer_consume_token (parser->lexer);
20966 /* Return true if we are looking at an array-designator, false otherwise. */
20969 cp_parser_array_designator_p (cp_parser *parser)
20971 /* Consume the `['. */
20972 cp_lexer_consume_token (parser->lexer);
20974 cp_lexer_save_tokens (parser->lexer);
20976 /* Skip tokens until the next token is a closing square bracket.
20977 If we find the closing `]', and the next token is a `=', then
20978 we are looking at an array designator. */
20979 bool array_designator_p
20980 = (cp_parser_skip_to_closing_square_bracket (parser)
20981 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20983 /* Roll back the tokens we skipped. */
20984 cp_lexer_rollback_tokens (parser->lexer);
20986 return array_designator_p;
20989 /* Parse an initializer-list.
20992 initializer-clause ... [opt]
20993 initializer-list , initializer-clause ... [opt]
20998 designation initializer-clause ...[opt]
20999 initializer-list , designation initializer-clause ...[opt]
21004 [ constant-expression ] =
21006 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21007 for the initializer. If the INDEX of the elt is non-NULL, it is the
21008 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21009 as for cp_parser_initializer. */
21011 static vec<constructor_elt, va_gc> *
21012 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21014 vec<constructor_elt, va_gc> *v = NULL;
21016 /* Assume all of the expressions are constant. */
21017 *non_constant_p = false;
21019 /* Parse the rest of the list. */
21025 bool clause_non_constant_p;
21027 /* If the next token is an identifier and the following one is a
21028 colon, we are looking at the GNU designated-initializer
21030 if (cp_parser_allow_gnu_extensions_p (parser)
21031 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21032 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21034 /* Warn the user that they are using an extension. */
21035 pedwarn (input_location, OPT_Wpedantic,
21036 "ISO C++ does not allow designated initializers");
21037 /* Consume the identifier. */
21038 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21039 /* Consume the `:'. */
21040 cp_lexer_consume_token (parser->lexer);
21042 /* Also handle the C99 syntax, '. id ='. */
21043 else if (cp_parser_allow_gnu_extensions_p (parser)
21044 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21045 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21046 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21048 /* Warn the user that they are using an extension. */
21049 pedwarn (input_location, OPT_Wpedantic,
21050 "ISO C++ does not allow C99 designated initializers");
21051 /* Consume the `.'. */
21052 cp_lexer_consume_token (parser->lexer);
21053 /* Consume the identifier. */
21054 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21055 /* Consume the `='. */
21056 cp_lexer_consume_token (parser->lexer);
21058 /* Also handle C99 array designators, '[ const ] ='. */
21059 else if (cp_parser_allow_gnu_extensions_p (parser)
21060 && !c_dialect_objc ()
21061 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21063 /* In C++11, [ could start a lambda-introducer. */
21064 bool non_const = false;
21066 cp_parser_parse_tentatively (parser);
21068 if (!cp_parser_array_designator_p (parser))
21070 cp_parser_simulate_error (parser);
21071 designator = NULL_TREE;
21075 designator = cp_parser_constant_expression (parser, true,
21077 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21078 cp_parser_require (parser, CPP_EQ, RT_EQ);
21081 if (!cp_parser_parse_definitely (parser))
21082 designator = NULL_TREE;
21083 else if (non_const)
21084 require_potential_rvalue_constant_expression (designator);
21087 designator = NULL_TREE;
21089 /* Parse the initializer. */
21090 initializer = cp_parser_initializer_clause (parser,
21091 &clause_non_constant_p);
21092 /* If any clause is non-constant, so is the entire initializer. */
21093 if (clause_non_constant_p)
21094 *non_constant_p = true;
21096 /* If we have an ellipsis, this is an initializer pack
21098 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21100 /* Consume the `...'. */
21101 cp_lexer_consume_token (parser->lexer);
21103 /* Turn the initializer into an initializer expansion. */
21104 initializer = make_pack_expansion (initializer);
21107 /* Add it to the vector. */
21108 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21110 /* If the next token is not a comma, we have reached the end of
21112 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21115 /* Peek at the next token. */
21116 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21117 /* If the next token is a `}', then we're still done. An
21118 initializer-clause can have a trailing `,' after the
21119 initializer-list and before the closing `}'. */
21120 if (token->type == CPP_CLOSE_BRACE)
21123 /* Consume the `,' token. */
21124 cp_lexer_consume_token (parser->lexer);
21130 /* Classes [gram.class] */
21132 /* Parse a class-name.
21138 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21139 to indicate that names looked up in dependent types should be
21140 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21141 keyword has been used to indicate that the name that appears next
21142 is a template. TAG_TYPE indicates the explicit tag given before
21143 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21144 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21145 is the class being defined in a class-head. If ENUM_OK is TRUE,
21146 enum-names are also accepted.
21148 Returns the TYPE_DECL representing the class. */
21151 cp_parser_class_name (cp_parser *parser,
21152 bool typename_keyword_p,
21153 bool template_keyword_p,
21154 enum tag_types tag_type,
21155 bool check_dependency_p,
21157 bool is_declaration,
21164 tree identifier = NULL_TREE;
21166 /* All class-names start with an identifier. */
21167 token = cp_lexer_peek_token (parser->lexer);
21168 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21170 cp_parser_error (parser, "expected class-name");
21171 return error_mark_node;
21174 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21175 to a template-id, so we save it here. */
21176 scope = parser->scope;
21177 if (scope == error_mark_node)
21178 return error_mark_node;
21180 /* Any name names a type if we're following the `typename' keyword
21181 in a qualified name where the enclosing scope is type-dependent. */
21182 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21183 && dependent_type_p (scope));
21184 /* Handle the common case (an identifier, but not a template-id)
21186 if (token->type == CPP_NAME
21187 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21189 cp_token *identifier_token;
21192 /* Look for the identifier. */
21193 identifier_token = cp_lexer_peek_token (parser->lexer);
21194 ambiguous_p = identifier_token->error_reported;
21195 identifier = cp_parser_identifier (parser);
21196 /* If the next token isn't an identifier, we are certainly not
21197 looking at a class-name. */
21198 if (identifier == error_mark_node)
21199 decl = error_mark_node;
21200 /* If we know this is a type-name, there's no need to look it
21202 else if (typename_p)
21206 tree ambiguous_decls;
21207 /* If we already know that this lookup is ambiguous, then
21208 we've already issued an error message; there's no reason
21212 cp_parser_simulate_error (parser);
21213 return error_mark_node;
21215 /* If the next token is a `::', then the name must be a type
21218 [basic.lookup.qual]
21220 During the lookup for a name preceding the :: scope
21221 resolution operator, object, function, and enumerator
21222 names are ignored. */
21223 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21224 tag_type = scope_type;
21225 /* Look up the name. */
21226 decl = cp_parser_lookup_name (parser, identifier,
21228 /*is_template=*/false,
21229 /*is_namespace=*/false,
21230 check_dependency_p,
21232 identifier_token->location);
21233 if (ambiguous_decls)
21235 if (cp_parser_parsing_tentatively (parser))
21236 cp_parser_simulate_error (parser);
21237 return error_mark_node;
21243 /* Try a template-id. */
21244 decl = cp_parser_template_id (parser, template_keyword_p,
21245 check_dependency_p,
21248 if (decl == error_mark_node)
21249 return error_mark_node;
21252 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21254 /* If this is a typename, create a TYPENAME_TYPE. */
21255 if (typename_p && decl != error_mark_node)
21257 decl = make_typename_type (scope, decl, typename_type,
21258 /*complain=*/tf_error);
21259 if (decl != error_mark_node)
21260 decl = TYPE_NAME (decl);
21263 decl = strip_using_decl (decl);
21265 /* Check to see that it is really the name of a class. */
21266 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21267 && identifier_p (TREE_OPERAND (decl, 0))
21268 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21269 /* Situations like this:
21271 template <typename T> struct A {
21272 typename T::template X<int>::I i;
21275 are problematic. Is `T::template X<int>' a class-name? The
21276 standard does not seem to be definitive, but there is no other
21277 valid interpretation of the following `::'. Therefore, those
21278 names are considered class-names. */
21280 decl = make_typename_type (scope, decl, tag_type, tf_error);
21281 if (decl != error_mark_node)
21282 decl = TYPE_NAME (decl);
21284 else if (TREE_CODE (decl) != TYPE_DECL
21285 || TREE_TYPE (decl) == error_mark_node
21286 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21287 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21288 /* In Objective-C 2.0, a classname followed by '.' starts a
21289 dot-syntax expression, and it's not a type-name. */
21290 || (c_dialect_objc ()
21291 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21292 && objc_is_class_name (decl)))
21293 decl = error_mark_node;
21295 if (decl == error_mark_node)
21296 cp_parser_error (parser, "expected class-name");
21297 else if (identifier && !parser->scope)
21298 maybe_note_name_used_in_class (identifier, decl);
21303 /* Parse a class-specifier.
21306 class-head { member-specification [opt] }
21308 Returns the TREE_TYPE representing the class. */
21311 cp_parser_class_specifier_1 (cp_parser* parser)
21314 tree attributes = NULL_TREE;
21315 bool nested_name_specifier_p;
21316 unsigned saved_num_template_parameter_lists;
21317 bool saved_in_function_body;
21318 unsigned char in_statement;
21319 bool in_switch_statement_p;
21320 bool saved_in_unbraced_linkage_specification_p;
21321 tree old_scope = NULL_TREE;
21322 tree scope = NULL_TREE;
21323 cp_token *closing_brace;
21325 push_deferring_access_checks (dk_no_deferred);
21327 /* Parse the class-head. */
21328 type = cp_parser_class_head (parser,
21329 &nested_name_specifier_p);
21330 /* If the class-head was a semantic disaster, skip the entire body
21334 cp_parser_skip_to_end_of_block_or_statement (parser);
21335 pop_deferring_access_checks ();
21336 return error_mark_node;
21339 /* Look for the `{'. */
21340 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21342 pop_deferring_access_checks ();
21343 return error_mark_node;
21346 cp_ensure_no_omp_declare_simd (parser);
21347 cp_ensure_no_oacc_routine (parser);
21349 /* Issue an error message if type-definitions are forbidden here. */
21350 cp_parser_check_type_definition (parser);
21351 /* Remember that we are defining one more class. */
21352 ++parser->num_classes_being_defined;
21353 /* Inside the class, surrounding template-parameter-lists do not
21355 saved_num_template_parameter_lists
21356 = parser->num_template_parameter_lists;
21357 parser->num_template_parameter_lists = 0;
21358 /* We are not in a function body. */
21359 saved_in_function_body = parser->in_function_body;
21360 parser->in_function_body = false;
21361 /* Or in a loop. */
21362 in_statement = parser->in_statement;
21363 parser->in_statement = 0;
21364 /* Or in a switch. */
21365 in_switch_statement_p = parser->in_switch_statement_p;
21366 parser->in_switch_statement_p = false;
21367 /* We are not immediately inside an extern "lang" block. */
21368 saved_in_unbraced_linkage_specification_p
21369 = parser->in_unbraced_linkage_specification_p;
21370 parser->in_unbraced_linkage_specification_p = false;
21372 // Associate constraints with the type.
21374 type = associate_classtype_constraints (type);
21376 /* Start the class. */
21377 if (nested_name_specifier_p)
21379 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21380 old_scope = push_inner_scope (scope);
21382 type = begin_class_definition (type);
21384 if (type == error_mark_node)
21385 /* If the type is erroneous, skip the entire body of the class. */
21386 cp_parser_skip_to_closing_brace (parser);
21388 /* Parse the member-specification. */
21389 cp_parser_member_specification_opt (parser);
21391 /* Look for the trailing `}'. */
21392 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21393 /* Look for trailing attributes to apply to this class. */
21394 if (cp_parser_allow_gnu_extensions_p (parser))
21395 attributes = cp_parser_gnu_attributes_opt (parser);
21396 if (type != error_mark_node)
21397 type = finish_struct (type, attributes);
21398 if (nested_name_specifier_p)
21399 pop_inner_scope (old_scope, scope);
21401 /* We've finished a type definition. Check for the common syntax
21402 error of forgetting a semicolon after the definition. We need to
21403 be careful, as we can't just check for not-a-semicolon and be done
21404 with it; the user might have typed:
21406 class X { } c = ...;
21407 class X { } *p = ...;
21409 and so forth. Instead, enumerate all the possible tokens that
21410 might follow this production; if we don't see one of them, then
21411 complain and silently insert the semicolon. */
21413 cp_token *token = cp_lexer_peek_token (parser->lexer);
21414 bool want_semicolon = true;
21416 if (cp_next_tokens_can_be_std_attribute_p (parser))
21417 /* Don't try to parse c++11 attributes here. As per the
21418 grammar, that should be a task for
21419 cp_parser_decl_specifier_seq. */
21420 want_semicolon = false;
21422 switch (token->type)
21425 case CPP_SEMICOLON:
21428 case CPP_OPEN_PAREN:
21429 case CPP_CLOSE_PAREN:
21431 want_semicolon = false;
21434 /* While it's legal for type qualifiers and storage class
21435 specifiers to follow type definitions in the grammar, only
21436 compiler testsuites contain code like that. Assume that if
21437 we see such code, then what we're really seeing is a case
21441 const <type> var = ...;
21446 static <type> func (...) ...
21448 i.e. the qualifier or specifier applies to the next
21449 declaration. To do so, however, we need to look ahead one
21450 more token to see if *that* token is a type specifier.
21452 This code could be improved to handle:
21455 static const <type> var = ...; */
21457 if (keyword_is_decl_specifier (token->keyword))
21459 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21461 /* Handling user-defined types here would be nice, but very
21464 = (lookahead->type == CPP_KEYWORD
21465 && keyword_begins_type_specifier (lookahead->keyword));
21472 /* If we don't have a type, then something is very wrong and we
21473 shouldn't try to do anything clever. Likewise for not seeing the
21475 if (closing_brace && TYPE_P (type) && want_semicolon)
21477 cp_token_position prev
21478 = cp_lexer_previous_token_position (parser->lexer);
21479 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21480 location_t loc = prev_token->location;
21482 if (CLASSTYPE_DECLARED_CLASS (type))
21483 error_at (loc, "expected %<;%> after class definition");
21484 else if (TREE_CODE (type) == RECORD_TYPE)
21485 error_at (loc, "expected %<;%> after struct definition");
21486 else if (TREE_CODE (type) == UNION_TYPE)
21487 error_at (loc, "expected %<;%> after union definition");
21489 gcc_unreachable ();
21491 /* Unget one token and smash it to look as though we encountered
21492 a semicolon in the input stream. */
21493 cp_lexer_set_token_position (parser->lexer, prev);
21494 token = cp_lexer_peek_token (parser->lexer);
21495 token->type = CPP_SEMICOLON;
21496 token->keyword = RID_MAX;
21500 /* If this class is not itself within the scope of another class,
21501 then we need to parse the bodies of all of the queued function
21502 definitions. Note that the queued functions defined in a class
21503 are not always processed immediately following the
21504 class-specifier for that class. Consider:
21507 struct B { void f() { sizeof (A); } };
21510 If `f' were processed before the processing of `A' were
21511 completed, there would be no way to compute the size of `A'.
21512 Note that the nesting we are interested in here is lexical --
21513 not the semantic nesting given by TYPE_CONTEXT. In particular,
21516 struct A { struct B; };
21517 struct A::B { void f() { } };
21519 there is no need to delay the parsing of `A::B::f'. */
21520 if (--parser->num_classes_being_defined == 0)
21523 tree class_type = NULL_TREE;
21524 tree pushed_scope = NULL_TREE;
21526 cp_default_arg_entry *e;
21527 tree save_ccp, save_ccr;
21529 /* In a first pass, parse default arguments to the functions.
21530 Then, in a second pass, parse the bodies of the functions.
21531 This two-phased approach handles cases like:
21539 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21542 /* If there are default arguments that have not yet been processed,
21543 take care of them now. */
21544 if (class_type != e->class_type)
21547 pop_scope (pushed_scope);
21548 class_type = e->class_type;
21549 pushed_scope = push_scope (class_type);
21551 /* Make sure that any template parameters are in scope. */
21552 maybe_begin_member_template_processing (decl);
21553 /* Parse the default argument expressions. */
21554 cp_parser_late_parsing_default_args (parser, decl);
21555 /* Remove any template parameters from the symbol table. */
21556 maybe_end_member_template_processing ();
21558 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21559 /* Now parse any NSDMIs. */
21560 save_ccp = current_class_ptr;
21561 save_ccr = current_class_ref;
21562 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21564 if (class_type != DECL_CONTEXT (decl))
21567 pop_scope (pushed_scope);
21568 class_type = DECL_CONTEXT (decl);
21569 pushed_scope = push_scope (class_type);
21571 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21572 cp_parser_late_parsing_nsdmi (parser, decl);
21574 vec_safe_truncate (unparsed_nsdmis, 0);
21575 current_class_ptr = save_ccp;
21576 current_class_ref = save_ccr;
21578 pop_scope (pushed_scope);
21580 /* Now do some post-NSDMI bookkeeping. */
21581 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21582 after_nsdmi_defaulted_late_checks (class_type);
21583 vec_safe_truncate (unparsed_classes, 0);
21584 after_nsdmi_defaulted_late_checks (type);
21586 /* Now parse the body of the functions. */
21589 /* OpenMP UDRs need to be parsed before all other functions. */
21590 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21591 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21592 cp_parser_late_parsing_for_member (parser, decl);
21593 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21594 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21595 cp_parser_late_parsing_for_member (parser, decl);
21598 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21599 cp_parser_late_parsing_for_member (parser, decl);
21600 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21603 vec_safe_push (unparsed_classes, type);
21605 /* Put back any saved access checks. */
21606 pop_deferring_access_checks ();
21608 /* Restore saved state. */
21609 parser->in_switch_statement_p = in_switch_statement_p;
21610 parser->in_statement = in_statement;
21611 parser->in_function_body = saved_in_function_body;
21612 parser->num_template_parameter_lists
21613 = saved_num_template_parameter_lists;
21614 parser->in_unbraced_linkage_specification_p
21615 = saved_in_unbraced_linkage_specification_p;
21621 cp_parser_class_specifier (cp_parser* parser)
21624 timevar_push (TV_PARSE_STRUCT);
21625 ret = cp_parser_class_specifier_1 (parser);
21626 timevar_pop (TV_PARSE_STRUCT);
21630 /* Parse a class-head.
21633 class-key identifier [opt] base-clause [opt]
21634 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21635 class-key nested-name-specifier [opt] template-id
21638 class-virt-specifier:
21642 class-key attributes identifier [opt] base-clause [opt]
21643 class-key attributes nested-name-specifier identifier base-clause [opt]
21644 class-key attributes nested-name-specifier [opt] template-id
21647 Upon return BASES is initialized to the list of base classes (or
21648 NULL, if there are none) in the same form returned by
21649 cp_parser_base_clause.
21651 Returns the TYPE of the indicated class. Sets
21652 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21653 involving a nested-name-specifier was used, and FALSE otherwise.
21655 Returns error_mark_node if this is not a class-head.
21657 Returns NULL_TREE if the class-head is syntactically valid, but
21658 semantically invalid in a way that means we should skip the entire
21659 body of the class. */
21662 cp_parser_class_head (cp_parser* parser,
21663 bool* nested_name_specifier_p)
21665 tree nested_name_specifier;
21666 enum tag_types class_key;
21667 tree id = NULL_TREE;
21668 tree type = NULL_TREE;
21671 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21672 bool template_id_p = false;
21673 bool qualified_p = false;
21674 bool invalid_nested_name_p = false;
21675 bool invalid_explicit_specialization_p = false;
21676 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21677 tree pushed_scope = NULL_TREE;
21678 unsigned num_templates;
21679 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21680 /* Assume no nested-name-specifier will be present. */
21681 *nested_name_specifier_p = false;
21682 /* Assume no template parameter lists will be used in defining the
21685 parser->colon_corrects_to_scope_p = false;
21687 /* Look for the class-key. */
21688 class_key = cp_parser_class_key (parser);
21689 if (class_key == none_type)
21690 return error_mark_node;
21692 /* Parse the attributes. */
21693 attributes = cp_parser_attributes_opt (parser);
21695 /* If the next token is `::', that is invalid -- but sometimes
21696 people do try to write:
21700 Handle this gracefully by accepting the extra qualifier, and then
21701 issuing an error about it later if this really is a
21702 class-head. If it turns out just to be an elaborated type
21703 specifier, remain silent. */
21704 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21705 qualified_p = true;
21707 push_deferring_access_checks (dk_no_check);
21709 /* Determine the name of the class. Begin by looking for an
21710 optional nested-name-specifier. */
21711 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21712 nested_name_specifier
21713 = cp_parser_nested_name_specifier_opt (parser,
21714 /*typename_keyword_p=*/false,
21715 /*check_dependency_p=*/false,
21717 /*is_declaration=*/false);
21718 /* If there was a nested-name-specifier, then there *must* be an
21720 if (nested_name_specifier)
21722 type_start_token = cp_lexer_peek_token (parser->lexer);
21723 /* Although the grammar says `identifier', it really means
21724 `class-name' or `template-name'. You are only allowed to
21725 define a class that has already been declared with this
21728 The proposed resolution for Core Issue 180 says that wherever
21729 you see `class T::X' you should treat `X' as a type-name.
21731 It is OK to define an inaccessible class; for example:
21733 class A { class B; };
21736 We do not know if we will see a class-name, or a
21737 template-name. We look for a class-name first, in case the
21738 class-name is a template-id; if we looked for the
21739 template-name first we would stop after the template-name. */
21740 cp_parser_parse_tentatively (parser);
21741 type = cp_parser_class_name (parser,
21742 /*typename_keyword_p=*/false,
21743 /*template_keyword_p=*/false,
21745 /*check_dependency_p=*/false,
21746 /*class_head_p=*/true,
21747 /*is_declaration=*/false);
21748 /* If that didn't work, ignore the nested-name-specifier. */
21749 if (!cp_parser_parse_definitely (parser))
21751 invalid_nested_name_p = true;
21752 type_start_token = cp_lexer_peek_token (parser->lexer);
21753 id = cp_parser_identifier (parser);
21754 if (id == error_mark_node)
21757 /* If we could not find a corresponding TYPE, treat this
21758 declaration like an unqualified declaration. */
21759 if (type == error_mark_node)
21760 nested_name_specifier = NULL_TREE;
21761 /* Otherwise, count the number of templates used in TYPE and its
21762 containing scopes. */
21767 for (scope = TREE_TYPE (type);
21768 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21769 scope = get_containing_scope (scope))
21771 && CLASS_TYPE_P (scope)
21772 && CLASSTYPE_TEMPLATE_INFO (scope)
21773 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21774 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21775 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21779 /* Otherwise, the identifier is optional. */
21782 /* We don't know whether what comes next is a template-id,
21783 an identifier, or nothing at all. */
21784 cp_parser_parse_tentatively (parser);
21785 /* Check for a template-id. */
21786 type_start_token = cp_lexer_peek_token (parser->lexer);
21787 id = cp_parser_template_id (parser,
21788 /*template_keyword_p=*/false,
21789 /*check_dependency_p=*/true,
21791 /*is_declaration=*/true);
21792 /* If that didn't work, it could still be an identifier. */
21793 if (!cp_parser_parse_definitely (parser))
21795 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21797 type_start_token = cp_lexer_peek_token (parser->lexer);
21798 id = cp_parser_identifier (parser);
21805 template_id_p = true;
21810 pop_deferring_access_checks ();
21814 cp_parser_check_for_invalid_template_id (parser, id,
21816 type_start_token->location);
21818 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21820 /* If it's not a `:' or a `{' then we can't really be looking at a
21821 class-head, since a class-head only appears as part of a
21822 class-specifier. We have to detect this situation before calling
21823 xref_tag, since that has irreversible side-effects. */
21824 if (!cp_parser_next_token_starts_class_definition_p (parser))
21826 cp_parser_error (parser, "expected %<{%> or %<:%>");
21827 type = error_mark_node;
21831 /* At this point, we're going ahead with the class-specifier, even
21832 if some other problem occurs. */
21833 cp_parser_commit_to_tentative_parse (parser);
21834 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21836 cp_parser_error (parser,
21837 "cannot specify %<override%> for a class");
21838 type = error_mark_node;
21841 /* Issue the error about the overly-qualified name now. */
21844 cp_parser_error (parser,
21845 "global qualification of class name is invalid");
21846 type = error_mark_node;
21849 else if (invalid_nested_name_p)
21851 cp_parser_error (parser,
21852 "qualified name does not name a class");
21853 type = error_mark_node;
21856 else if (nested_name_specifier)
21860 /* Reject typedef-names in class heads. */
21861 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21863 error_at (type_start_token->location,
21864 "invalid class name in declaration of %qD",
21870 /* Figure out in what scope the declaration is being placed. */
21871 scope = current_scope ();
21872 /* If that scope does not contain the scope in which the
21873 class was originally declared, the program is invalid. */
21874 if (scope && !is_ancestor (scope, nested_name_specifier))
21876 if (at_namespace_scope_p ())
21877 error_at (type_start_token->location,
21878 "declaration of %qD in namespace %qD which does not "
21880 type, scope, nested_name_specifier);
21882 error_at (type_start_token->location,
21883 "declaration of %qD in %qD which does not enclose %qD",
21884 type, scope, nested_name_specifier);
21890 A declarator-id shall not be qualified except for the
21891 definition of a ... nested class outside of its class
21892 ... [or] the definition or explicit instantiation of a
21893 class member of a namespace outside of its namespace. */
21894 if (scope == nested_name_specifier)
21896 permerror (nested_name_specifier_token_start->location,
21897 "extra qualification not allowed");
21898 nested_name_specifier = NULL_TREE;
21902 /* An explicit-specialization must be preceded by "template <>". If
21903 it is not, try to recover gracefully. */
21904 if (at_namespace_scope_p ()
21905 && parser->num_template_parameter_lists == 0
21908 error_at (type_start_token->location,
21909 "an explicit specialization must be preceded by %<template <>%>");
21910 invalid_explicit_specialization_p = true;
21911 /* Take the same action that would have been taken by
21912 cp_parser_explicit_specialization. */
21913 ++parser->num_template_parameter_lists;
21914 begin_specialization ();
21916 /* There must be no "return" statements between this point and the
21917 end of this function; set "type "to the correct return value and
21918 use "goto done;" to return. */
21919 /* Make sure that the right number of template parameters were
21921 if (!cp_parser_check_template_parameters (parser, num_templates,
21922 type_start_token->location,
21923 /*declarator=*/NULL))
21925 /* If something went wrong, there is no point in even trying to
21926 process the class-definition. */
21931 /* Look up the type. */
21934 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21935 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21936 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21938 error_at (type_start_token->location,
21939 "function template %qD redeclared as a class template", id);
21940 type = error_mark_node;
21944 type = TREE_TYPE (id);
21945 type = maybe_process_partial_specialization (type);
21947 if (nested_name_specifier)
21948 pushed_scope = push_scope (nested_name_specifier);
21950 else if (nested_name_specifier)
21956 template <typename T> struct S { struct T };
21957 template <typename T> struct S<T>::T { };
21959 we will get a TYPENAME_TYPE when processing the definition of
21960 `S::T'. We need to resolve it to the actual type before we
21961 try to define it. */
21962 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21964 class_type = resolve_typename_type (TREE_TYPE (type),
21965 /*only_current_p=*/false);
21966 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21967 type = TYPE_NAME (class_type);
21970 cp_parser_error (parser, "could not resolve typename type");
21971 type = error_mark_node;
21975 if (maybe_process_partial_specialization (TREE_TYPE (type))
21976 == error_mark_node)
21982 class_type = current_class_type;
21983 /* Enter the scope indicated by the nested-name-specifier. */
21984 pushed_scope = push_scope (nested_name_specifier);
21985 /* Get the canonical version of this type. */
21986 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21987 /* Call push_template_decl if it seems like we should be defining a
21988 template either from the template headers or the type we're
21989 defining, so that we diagnose both extra and missing headers. */
21990 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21991 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21992 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21994 type = push_template_decl (type);
21995 if (type == error_mark_node)
22002 type = TREE_TYPE (type);
22003 *nested_name_specifier_p = true;
22005 else /* The name is not a nested name. */
22007 /* If the class was unnamed, create a dummy name. */
22009 id = make_anon_name ();
22010 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22011 parser->num_template_parameter_lists);
22014 /* Indicate whether this class was declared as a `class' or as a
22016 if (TREE_CODE (type) == RECORD_TYPE)
22017 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22018 cp_parser_check_class_key (class_key, type);
22020 /* If this type was already complete, and we see another definition,
22021 that's an error. */
22022 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22024 error_at (type_start_token->location, "redefinition of %q#T",
22026 error_at (type_start_token->location, "previous definition of %q+#T",
22031 else if (type == error_mark_node)
22036 /* Apply attributes now, before any use of the class as a template
22037 argument in its base list. */
22038 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22039 fixup_attribute_variants (type);
22042 /* We will have entered the scope containing the class; the names of
22043 base classes should be looked up in that context. For example:
22045 struct A { struct B {}; struct C; };
22046 struct A::C : B {};
22050 /* Get the list of base-classes, if there is one. */
22051 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22053 /* PR59482: enter the class scope so that base-specifiers are looked
22057 bases = cp_parser_base_clause (parser);
22058 /* PR59482: get out of the previously pushed class scope so that the
22059 subsequent pops pop the right thing. */
22066 /* If we're really defining a class, process the base classes.
22067 If they're invalid, fail. */
22068 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22069 && !xref_basetypes (type, bases))
22073 /* Leave the scope given by the nested-name-specifier. We will
22074 enter the class scope itself while processing the members. */
22076 pop_scope (pushed_scope);
22078 if (invalid_explicit_specialization_p)
22080 end_specialization ();
22081 --parser->num_template_parameter_lists;
22085 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22086 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22087 CLASSTYPE_FINAL (type) = 1;
22089 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22093 /* Parse a class-key.
22100 Returns the kind of class-key specified, or none_type to indicate
22103 static enum tag_types
22104 cp_parser_class_key (cp_parser* parser)
22107 enum tag_types tag_type;
22109 /* Look for the class-key. */
22110 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22114 /* Check to see if the TOKEN is a class-key. */
22115 tag_type = cp_parser_token_is_class_key (token);
22117 cp_parser_error (parser, "expected class-key");
22121 /* Parse a type-parameter-key.
22123 type-parameter-key:
22129 cp_parser_type_parameter_key (cp_parser* parser)
22131 /* Look for the type-parameter-key. */
22132 enum tag_types tag_type = none_type;
22133 cp_token *token = cp_lexer_peek_token (parser->lexer);
22134 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22136 cp_lexer_consume_token (parser->lexer);
22137 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22138 /* typename is not allowed in a template template parameter
22139 by the standard until C++1Z. */
22140 pedwarn (token->location, OPT_Wpedantic,
22141 "ISO C++ forbids typename key in template template parameter;"
22142 " use -std=c++1z or -std=gnu++1z");
22145 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22150 /* Parse an (optional) member-specification.
22152 member-specification:
22153 member-declaration member-specification [opt]
22154 access-specifier : member-specification [opt] */
22157 cp_parser_member_specification_opt (cp_parser* parser)
22164 /* Peek at the next token. */
22165 token = cp_lexer_peek_token (parser->lexer);
22166 /* If it's a `}', or EOF then we've seen all the members. */
22167 if (token->type == CPP_CLOSE_BRACE
22168 || token->type == CPP_EOF
22169 || token->type == CPP_PRAGMA_EOL)
22172 /* See if this token is a keyword. */
22173 keyword = token->keyword;
22177 case RID_PROTECTED:
22179 /* Consume the access-specifier. */
22180 cp_lexer_consume_token (parser->lexer);
22181 /* Remember which access-specifier is active. */
22182 current_access_specifier = token->u.value;
22183 /* Look for the `:'. */
22184 cp_parser_require (parser, CPP_COLON, RT_COLON);
22188 /* Accept #pragmas at class scope. */
22189 if (token->type == CPP_PRAGMA)
22191 cp_parser_pragma (parser, pragma_member, NULL);
22195 /* Otherwise, the next construction must be a
22196 member-declaration. */
22197 cp_parser_member_declaration (parser);
22202 /* Parse a member-declaration.
22204 member-declaration:
22205 decl-specifier-seq [opt] member-declarator-list [opt] ;
22206 function-definition ; [opt]
22207 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22209 template-declaration
22212 member-declarator-list:
22214 member-declarator-list , member-declarator
22217 declarator pure-specifier [opt]
22218 declarator constant-initializer [opt]
22219 identifier [opt] : constant-expression
22223 member-declaration:
22224 __extension__ member-declaration
22227 declarator attributes [opt] pure-specifier [opt]
22228 declarator attributes [opt] constant-initializer [opt]
22229 identifier [opt] attributes [opt] : constant-expression
22233 member-declaration:
22234 static_assert-declaration */
22237 cp_parser_member_declaration (cp_parser* parser)
22239 cp_decl_specifier_seq decl_specifiers;
22240 tree prefix_attributes;
22242 int declares_class_or_enum;
22244 cp_token *token = NULL;
22245 cp_token *decl_spec_token_start = NULL;
22246 cp_token *initializer_token_start = NULL;
22247 int saved_pedantic;
22248 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22250 /* Check for the `__extension__' keyword. */
22251 if (cp_parser_extension_opt (parser, &saved_pedantic))
22254 cp_parser_member_declaration (parser);
22255 /* Restore the old value of the PEDANTIC flag. */
22256 pedantic = saved_pedantic;
22261 /* Check for a template-declaration. */
22262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22264 /* An explicit specialization here is an error condition, and we
22265 expect the specialization handler to detect and report this. */
22266 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22267 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22268 cp_parser_explicit_specialization (parser);
22270 cp_parser_template_declaration (parser, /*member_p=*/true);
22274 /* Check for a template introduction. */
22275 else if (cp_parser_template_declaration_after_export (parser, true))
22278 /* Check for a using-declaration. */
22279 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22281 if (cxx_dialect < cxx11)
22283 /* Parse the using-declaration. */
22284 cp_parser_using_declaration (parser,
22285 /*access_declaration_p=*/false);
22291 bool alias_decl_expected;
22292 cp_parser_parse_tentatively (parser);
22293 decl = cp_parser_alias_declaration (parser);
22294 /* Note that if we actually see the '=' token after the
22295 identifier, cp_parser_alias_declaration commits the
22296 tentative parse. In that case, we really expect an
22297 alias-declaration. Otherwise, we expect a using
22299 alias_decl_expected =
22300 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22301 cp_parser_parse_definitely (parser);
22303 if (alias_decl_expected)
22304 finish_member_declaration (decl);
22306 cp_parser_using_declaration (parser,
22307 /*access_declaration_p=*/false);
22312 /* Check for @defs. */
22313 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22316 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22317 ivar = ivar_chains;
22321 ivar = TREE_CHAIN (member);
22322 TREE_CHAIN (member) = NULL_TREE;
22323 finish_member_declaration (member);
22328 /* If the next token is `static_assert' we have a static assertion. */
22329 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22331 cp_parser_static_assert (parser, /*member_p=*/true);
22335 parser->colon_corrects_to_scope_p = false;
22337 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22340 /* Parse the decl-specifier-seq. */
22341 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22342 cp_parser_decl_specifier_seq (parser,
22343 CP_PARSER_FLAGS_OPTIONAL,
22345 &declares_class_or_enum);
22346 /* Check for an invalid type-name. */
22347 if (!decl_specifiers.any_type_specifiers_p
22348 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22350 /* If there is no declarator, then the decl-specifier-seq should
22352 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22354 /* If there was no decl-specifier-seq, and the next token is a
22355 `;', then we have something like:
22361 Each member-declaration shall declare at least one member
22362 name of the class. */
22363 if (!decl_specifiers.any_specifiers_p)
22365 cp_token *token = cp_lexer_peek_token (parser->lexer);
22366 if (!in_system_header_at (token->location))
22367 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22373 /* See if this declaration is a friend. */
22374 friend_p = cp_parser_friend_p (&decl_specifiers);
22375 /* If there were decl-specifiers, check to see if there was
22376 a class-declaration. */
22377 type = check_tag_decl (&decl_specifiers,
22378 /*explicit_type_instantiation_p=*/false);
22379 /* Nested classes have already been added to the class, but
22380 a `friend' needs to be explicitly registered. */
22383 /* If the `friend' keyword was present, the friend must
22384 be introduced with a class-key. */
22385 if (!declares_class_or_enum && cxx_dialect < cxx11)
22386 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22387 "in C++03 a class-key must be used "
22388 "when declaring a friend");
22391 template <typename T> struct A {
22392 friend struct A<T>::B;
22395 A<T>::B will be represented by a TYPENAME_TYPE, and
22396 therefore not recognized by check_tag_decl. */
22399 type = decl_specifiers.type;
22400 if (type && TREE_CODE (type) == TYPE_DECL)
22401 type = TREE_TYPE (type);
22403 if (!type || !TYPE_P (type))
22404 error_at (decl_spec_token_start->location,
22405 "friend declaration does not name a class or "
22408 make_friend_class (current_class_type, type,
22409 /*complain=*/true);
22411 /* If there is no TYPE, an error message will already have
22413 else if (!type || type == error_mark_node)
22415 /* An anonymous aggregate has to be handled specially; such
22416 a declaration really declares a data member (with a
22417 particular type), as opposed to a nested class. */
22418 else if (ANON_AGGR_TYPE_P (type))
22421 if (decl_specifiers.storage_class != sc_none)
22422 error_at (decl_spec_token_start->location,
22423 "a storage class on an anonymous aggregate "
22424 "in class scope is not allowed");
22426 /* Remove constructors and such from TYPE, now that we
22427 know it is an anonymous aggregate. */
22428 fixup_anonymous_aggr (type);
22429 /* And make the corresponding data member. */
22430 decl = build_decl (decl_spec_token_start->location,
22431 FIELD_DECL, NULL_TREE, type);
22432 /* Add it to the class. */
22433 finish_member_declaration (decl);
22436 cp_parser_check_access_in_redeclaration
22438 decl_spec_token_start->location);
22443 bool assume_semicolon = false;
22445 /* Clear attributes from the decl_specifiers but keep them
22446 around as prefix attributes that apply them to the entity
22448 prefix_attributes = decl_specifiers.attributes;
22449 decl_specifiers.attributes = NULL_TREE;
22451 /* See if these declarations will be friends. */
22452 friend_p = cp_parser_friend_p (&decl_specifiers);
22454 /* Keep going until we hit the `;' at the end of the
22456 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22458 tree attributes = NULL_TREE;
22459 tree first_attribute;
22461 /* Peek at the next token. */
22462 token = cp_lexer_peek_token (parser->lexer);
22464 /* Check for a bitfield declaration. */
22465 if (token->type == CPP_COLON
22466 || (token->type == CPP_NAME
22467 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22473 /* Get the name of the bitfield. Note that we cannot just
22474 check TOKEN here because it may have been invalidated by
22475 the call to cp_lexer_peek_nth_token above. */
22476 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22477 identifier = cp_parser_identifier (parser);
22479 identifier = NULL_TREE;
22481 /* Consume the `:' token. */
22482 cp_lexer_consume_token (parser->lexer);
22483 /* Get the width of the bitfield. */
22485 = cp_parser_constant_expression (parser);
22487 /* Look for attributes that apply to the bitfield. */
22488 attributes = cp_parser_attributes_opt (parser);
22489 /* Remember which attributes are prefix attributes and
22491 first_attribute = attributes;
22492 /* Combine the attributes. */
22493 attributes = chainon (prefix_attributes, attributes);
22495 /* Create the bitfield declaration. */
22496 decl = grokbitfield (identifier
22497 ? make_id_declarator (NULL_TREE,
22507 cp_declarator *declarator;
22509 tree asm_specification;
22510 int ctor_dtor_or_conv_p;
22512 /* Parse the declarator. */
22514 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22515 &ctor_dtor_or_conv_p,
22516 /*parenthesized_p=*/NULL,
22520 /* If something went wrong parsing the declarator, make sure
22521 that we at least consume some tokens. */
22522 if (declarator == cp_error_declarator)
22524 /* Skip to the end of the statement. */
22525 cp_parser_skip_to_end_of_statement (parser);
22526 /* If the next token is not a semicolon, that is
22527 probably because we just skipped over the body of
22528 a function. So, we consume a semicolon if
22529 present, but do not issue an error message if it
22531 if (cp_lexer_next_token_is (parser->lexer,
22533 cp_lexer_consume_token (parser->lexer);
22537 if (declares_class_or_enum & 2)
22538 cp_parser_check_for_definition_in_return_type
22539 (declarator, decl_specifiers.type,
22540 decl_specifiers.locations[ds_type_spec]);
22542 /* Look for an asm-specification. */
22543 asm_specification = cp_parser_asm_specification_opt (parser);
22544 /* Look for attributes that apply to the declaration. */
22545 attributes = cp_parser_attributes_opt (parser);
22546 /* Remember which attributes are prefix attributes and
22548 first_attribute = attributes;
22549 /* Combine the attributes. */
22550 attributes = chainon (prefix_attributes, attributes);
22552 /* If it's an `=', then we have a constant-initializer or a
22553 pure-specifier. It is not correct to parse the
22554 initializer before registering the member declaration
22555 since the member declaration should be in scope while
22556 its initializer is processed. However, the rest of the
22557 front end does not yet provide an interface that allows
22558 us to handle this correctly. */
22559 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22563 A pure-specifier shall be used only in the declaration of
22564 a virtual function.
22566 A member-declarator can contain a constant-initializer
22567 only if it declares a static member of integral or
22570 Therefore, if the DECLARATOR is for a function, we look
22571 for a pure-specifier; otherwise, we look for a
22572 constant-initializer. When we call `grokfield', it will
22573 perform more stringent semantics checks. */
22574 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22575 if (function_declarator_p (declarator)
22576 || (decl_specifiers.type
22577 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22578 && declarator->kind == cdk_id
22579 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22580 == FUNCTION_TYPE)))
22581 initializer = cp_parser_pure_specifier (parser);
22582 else if (decl_specifiers.storage_class != sc_static)
22583 initializer = cp_parser_save_nsdmi (parser);
22584 else if (cxx_dialect >= cxx11)
22587 /* Don't require a constant rvalue in C++11, since we
22588 might want a reference constant. We'll enforce
22589 constancy later. */
22590 cp_lexer_consume_token (parser->lexer);
22591 /* Parse the initializer. */
22592 initializer = cp_parser_initializer_clause (parser,
22596 /* Parse the initializer. */
22597 initializer = cp_parser_constant_initializer (parser);
22599 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22600 && !function_declarator_p (declarator))
22603 if (decl_specifiers.storage_class != sc_static)
22604 initializer = cp_parser_save_nsdmi (parser);
22606 initializer = cp_parser_initializer (parser, &x, &x);
22608 /* Otherwise, there is no initializer. */
22610 initializer = NULL_TREE;
22612 /* See if we are probably looking at a function
22613 definition. We are certainly not looking at a
22614 member-declarator. Calling `grokfield' has
22615 side-effects, so we must not do it unless we are sure
22616 that we are looking at a member-declarator. */
22617 if (cp_parser_token_starts_function_definition_p
22618 (cp_lexer_peek_token (parser->lexer)))
22620 /* The grammar does not allow a pure-specifier to be
22621 used when a member function is defined. (It is
22622 possible that this fact is an oversight in the
22623 standard, since a pure function may be defined
22624 outside of the class-specifier. */
22625 if (initializer && initializer_token_start)
22626 error_at (initializer_token_start->location,
22627 "pure-specifier on function-definition");
22628 decl = cp_parser_save_member_function_body (parser,
22632 if (parser->fully_implicit_function_template_p)
22633 decl = finish_fully_implicit_template (parser, decl);
22634 /* If the member was not a friend, declare it here. */
22636 finish_member_declaration (decl);
22637 /* Peek at the next token. */
22638 token = cp_lexer_peek_token (parser->lexer);
22639 /* If the next token is a semicolon, consume it. */
22640 if (token->type == CPP_SEMICOLON)
22641 cp_lexer_consume_token (parser->lexer);
22645 if (declarator->kind == cdk_function)
22646 declarator->id_loc = token->location;
22647 /* Create the declaration. */
22648 decl = grokfield (declarator, &decl_specifiers,
22649 initializer, /*init_const_expr_p=*/true,
22650 asm_specification, attributes);
22651 if (parser->fully_implicit_function_template_p)
22654 finish_fully_implicit_template (parser, 0);
22656 decl = finish_fully_implicit_template (parser, decl);
22660 cp_finalize_omp_declare_simd (parser, decl);
22661 cp_finalize_oacc_routine (parser, decl, false);
22663 /* Reset PREFIX_ATTRIBUTES. */
22664 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22665 attributes = TREE_CHAIN (attributes);
22667 TREE_CHAIN (attributes) = NULL_TREE;
22669 /* If there is any qualification still in effect, clear it
22670 now; we will be starting fresh with the next declarator. */
22671 parser->scope = NULL_TREE;
22672 parser->qualifying_scope = NULL_TREE;
22673 parser->object_scope = NULL_TREE;
22674 /* If it's a `,', then there are more declarators. */
22675 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22677 cp_lexer_consume_token (parser->lexer);
22678 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22680 cp_token *token = cp_lexer_previous_token (parser->lexer);
22681 error_at (token->location,
22682 "stray %<,%> at end of member declaration");
22685 /* If the next token isn't a `;', then we have a parse error. */
22686 else if (cp_lexer_next_token_is_not (parser->lexer,
22689 /* The next token might be a ways away from where the
22690 actual semicolon is missing. Find the previous token
22691 and use that for our error position. */
22692 cp_token *token = cp_lexer_previous_token (parser->lexer);
22693 error_at (token->location,
22694 "expected %<;%> at end of member declaration");
22696 /* Assume that the user meant to provide a semicolon. If
22697 we were to cp_parser_skip_to_end_of_statement, we might
22698 skip to a semicolon inside a member function definition
22699 and issue nonsensical error messages. */
22700 assume_semicolon = true;
22705 /* Add DECL to the list of members. */
22707 /* Explicitly include, eg, NSDMIs, for better error
22708 recovery (c++/58650). */
22709 || !DECL_DECLARES_FUNCTION_P (decl))
22710 finish_member_declaration (decl);
22712 if (TREE_CODE (decl) == FUNCTION_DECL)
22713 cp_parser_save_default_args (parser, decl);
22714 else if (TREE_CODE (decl) == FIELD_DECL
22715 && !DECL_C_BIT_FIELD (decl)
22716 && DECL_INITIAL (decl))
22717 /* Add DECL to the queue of NSDMI to be parsed later. */
22718 vec_safe_push (unparsed_nsdmis, decl);
22721 if (assume_semicolon)
22726 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22728 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22731 /* Parse a pure-specifier.
22736 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22737 Otherwise, ERROR_MARK_NODE is returned. */
22740 cp_parser_pure_specifier (cp_parser* parser)
22744 /* Look for the `=' token. */
22745 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22746 return error_mark_node;
22747 /* Look for the `0' token. */
22748 token = cp_lexer_peek_token (parser->lexer);
22750 if (token->type == CPP_EOF
22751 || token->type == CPP_PRAGMA_EOL)
22752 return error_mark_node;
22754 cp_lexer_consume_token (parser->lexer);
22756 /* Accept = default or = delete in c++0x mode. */
22757 if (token->keyword == RID_DEFAULT
22758 || token->keyword == RID_DELETE)
22760 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22761 return token->u.value;
22764 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22765 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22767 cp_parser_error (parser,
22768 "invalid pure specifier (only %<= 0%> is allowed)");
22769 cp_parser_skip_to_end_of_statement (parser);
22770 return error_mark_node;
22772 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22774 error_at (token->location, "templates may not be %<virtual%>");
22775 return error_mark_node;
22778 return integer_zero_node;
22781 /* Parse a constant-initializer.
22783 constant-initializer:
22784 = constant-expression
22786 Returns a representation of the constant-expression. */
22789 cp_parser_constant_initializer (cp_parser* parser)
22791 /* Look for the `=' token. */
22792 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22793 return error_mark_node;
22795 /* It is invalid to write:
22797 struct S { static const int i = { 7 }; };
22800 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22802 cp_parser_error (parser,
22803 "a brace-enclosed initializer is not allowed here");
22804 /* Consume the opening brace. */
22805 cp_lexer_consume_token (parser->lexer);
22806 /* Skip the initializer. */
22807 cp_parser_skip_to_closing_brace (parser);
22808 /* Look for the trailing `}'. */
22809 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22811 return error_mark_node;
22814 return cp_parser_constant_expression (parser);
22817 /* Derived classes [gram.class.derived] */
22819 /* Parse a base-clause.
22822 : base-specifier-list
22824 base-specifier-list:
22825 base-specifier ... [opt]
22826 base-specifier-list , base-specifier ... [opt]
22828 Returns a TREE_LIST representing the base-classes, in the order in
22829 which they were declared. The representation of each node is as
22830 described by cp_parser_base_specifier.
22832 In the case that no bases are specified, this function will return
22833 NULL_TREE, not ERROR_MARK_NODE. */
22836 cp_parser_base_clause (cp_parser* parser)
22838 tree bases = NULL_TREE;
22840 /* Look for the `:' that begins the list. */
22841 cp_parser_require (parser, CPP_COLON, RT_COLON);
22843 /* Scan the base-specifier-list. */
22848 bool pack_expansion_p = false;
22850 /* Look for the base-specifier. */
22851 base = cp_parser_base_specifier (parser);
22852 /* Look for the (optional) ellipsis. */
22853 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22855 /* Consume the `...'. */
22856 cp_lexer_consume_token (parser->lexer);
22858 pack_expansion_p = true;
22861 /* Add BASE to the front of the list. */
22862 if (base && base != error_mark_node)
22864 if (pack_expansion_p)
22865 /* Make this a pack expansion type. */
22866 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22868 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22870 TREE_CHAIN (base) = bases;
22874 /* Peek at the next token. */
22875 token = cp_lexer_peek_token (parser->lexer);
22876 /* If it's not a comma, then the list is complete. */
22877 if (token->type != CPP_COMMA)
22879 /* Consume the `,'. */
22880 cp_lexer_consume_token (parser->lexer);
22883 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22884 base class had a qualified name. However, the next name that
22885 appears is certainly not qualified. */
22886 parser->scope = NULL_TREE;
22887 parser->qualifying_scope = NULL_TREE;
22888 parser->object_scope = NULL_TREE;
22890 return nreverse (bases);
22893 /* Parse a base-specifier.
22896 :: [opt] nested-name-specifier [opt] class-name
22897 virtual access-specifier [opt] :: [opt] nested-name-specifier
22899 access-specifier virtual [opt] :: [opt] nested-name-specifier
22902 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22903 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22904 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22905 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22908 cp_parser_base_specifier (cp_parser* parser)
22912 bool virtual_p = false;
22913 bool duplicate_virtual_error_issued_p = false;
22914 bool duplicate_access_error_issued_p = false;
22915 bool class_scope_p, template_p;
22916 tree access = access_default_node;
22919 /* Process the optional `virtual' and `access-specifier'. */
22922 /* Peek at the next token. */
22923 token = cp_lexer_peek_token (parser->lexer);
22924 /* Process `virtual'. */
22925 switch (token->keyword)
22928 /* If `virtual' appears more than once, issue an error. */
22929 if (virtual_p && !duplicate_virtual_error_issued_p)
22931 cp_parser_error (parser,
22932 "%<virtual%> specified more than once in base-specified");
22933 duplicate_virtual_error_issued_p = true;
22938 /* Consume the `virtual' token. */
22939 cp_lexer_consume_token (parser->lexer);
22944 case RID_PROTECTED:
22946 /* If more than one access specifier appears, issue an
22948 if (access != access_default_node
22949 && !duplicate_access_error_issued_p)
22951 cp_parser_error (parser,
22952 "more than one access specifier in base-specified");
22953 duplicate_access_error_issued_p = true;
22956 access = ridpointers[(int) token->keyword];
22958 /* Consume the access-specifier. */
22959 cp_lexer_consume_token (parser->lexer);
22968 /* It is not uncommon to see programs mechanically, erroneously, use
22969 the 'typename' keyword to denote (dependent) qualified types
22970 as base classes. */
22971 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22973 token = cp_lexer_peek_token (parser->lexer);
22974 if (!processing_template_decl)
22975 error_at (token->location,
22976 "keyword %<typename%> not allowed outside of templates");
22978 error_at (token->location,
22979 "keyword %<typename%> not allowed in this context "
22980 "(the base class is implicitly a type)");
22981 cp_lexer_consume_token (parser->lexer);
22984 /* Look for the optional `::' operator. */
22985 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22986 /* Look for the nested-name-specifier. The simplest way to
22991 The keyword `typename' is not permitted in a base-specifier or
22992 mem-initializer; in these contexts a qualified name that
22993 depends on a template-parameter is implicitly assumed to be a
22996 is to pretend that we have seen the `typename' keyword at this
22998 cp_parser_nested_name_specifier_opt (parser,
22999 /*typename_keyword_p=*/true,
23000 /*check_dependency_p=*/true,
23002 /*is_declaration=*/true);
23003 /* If the base class is given by a qualified name, assume that names
23004 we see are type names or templates, as appropriate. */
23005 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23006 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23009 && cp_lexer_next_token_is_decltype (parser->lexer))
23010 /* DR 950 allows decltype as a base-specifier. */
23011 type = cp_parser_decltype (parser);
23014 /* Otherwise, look for the class-name. */
23015 type = cp_parser_class_name (parser,
23019 /*check_dependency_p=*/true,
23020 /*class_head_p=*/false,
23021 /*is_declaration=*/true);
23022 type = TREE_TYPE (type);
23025 if (type == error_mark_node)
23026 return error_mark_node;
23028 return finish_base_specifier (type, access, virtual_p);
23031 /* Exception handling [gram.exception] */
23033 /* Parse an (optional) noexcept-specification.
23035 noexcept-specification:
23036 noexcept ( constant-expression ) [opt]
23038 If no noexcept-specification is present, returns NULL_TREE.
23039 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23040 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23041 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23042 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23043 in which case a boolean condition is returned instead. */
23046 cp_parser_noexcept_specification_opt (cp_parser* parser,
23047 bool require_constexpr,
23048 bool* consumed_expr,
23052 const char *saved_message;
23054 /* Peek at the next token. */
23055 token = cp_lexer_peek_token (parser->lexer);
23057 /* Is it a noexcept-specification? */
23058 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23061 cp_lexer_consume_token (parser->lexer);
23063 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23065 cp_lexer_consume_token (parser->lexer);
23067 if (require_constexpr)
23069 /* Types may not be defined in an exception-specification. */
23070 saved_message = parser->type_definition_forbidden_message;
23071 parser->type_definition_forbidden_message
23072 = G_("types may not be defined in an exception-specification");
23074 expr = cp_parser_constant_expression (parser);
23076 /* Restore the saved message. */
23077 parser->type_definition_forbidden_message = saved_message;
23081 expr = cp_parser_expression (parser);
23082 *consumed_expr = true;
23085 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23089 expr = boolean_true_node;
23090 if (!require_constexpr)
23091 *consumed_expr = false;
23094 /* We cannot build a noexcept-spec right away because this will check
23095 that expr is a constexpr. */
23097 return build_noexcept_spec (expr, tf_warning_or_error);
23105 /* Parse an (optional) exception-specification.
23107 exception-specification:
23108 throw ( type-id-list [opt] )
23110 Returns a TREE_LIST representing the exception-specification. The
23111 TREE_VALUE of each node is a type. */
23114 cp_parser_exception_specification_opt (cp_parser* parser)
23118 const char *saved_message;
23120 /* Peek at the next token. */
23121 token = cp_lexer_peek_token (parser->lexer);
23123 /* Is it a noexcept-specification? */
23124 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23126 if (type_id_list != NULL_TREE)
23127 return type_id_list;
23129 /* If it's not `throw', then there's no exception-specification. */
23130 if (!cp_parser_is_keyword (token, RID_THROW))
23134 /* Enable this once a lot of code has transitioned to noexcept? */
23135 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23136 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23137 "deprecated in C++0x; use %<noexcept%> instead");
23140 /* Consume the `throw'. */
23141 cp_lexer_consume_token (parser->lexer);
23143 /* Look for the `('. */
23144 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23146 /* Peek at the next token. */
23147 token = cp_lexer_peek_token (parser->lexer);
23148 /* If it's not a `)', then there is a type-id-list. */
23149 if (token->type != CPP_CLOSE_PAREN)
23151 /* Types may not be defined in an exception-specification. */
23152 saved_message = parser->type_definition_forbidden_message;
23153 parser->type_definition_forbidden_message
23154 = G_("types may not be defined in an exception-specification");
23155 /* Parse the type-id-list. */
23156 type_id_list = cp_parser_type_id_list (parser);
23157 /* Restore the saved message. */
23158 parser->type_definition_forbidden_message = saved_message;
23161 type_id_list = empty_except_spec;
23163 /* Look for the `)'. */
23164 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23166 return type_id_list;
23169 /* Parse an (optional) type-id-list.
23173 type-id-list , type-id ... [opt]
23175 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23176 in the order that the types were presented. */
23179 cp_parser_type_id_list (cp_parser* parser)
23181 tree types = NULL_TREE;
23188 token = cp_lexer_peek_token (parser->lexer);
23190 /* Get the next type-id. */
23191 type = cp_parser_type_id (parser);
23192 /* Check for invalid 'auto'. */
23193 if (flag_concepts && type_uses_auto (type))
23195 error_at (token->location,
23196 "invalid use of %<auto%> in exception-specification");
23197 type = error_mark_node;
23199 /* Parse the optional ellipsis. */
23200 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23202 /* Consume the `...'. */
23203 cp_lexer_consume_token (parser->lexer);
23205 /* Turn the type into a pack expansion expression. */
23206 type = make_pack_expansion (type);
23208 /* Add it to the list. */
23209 types = add_exception_specifier (types, type, /*complain=*/1);
23210 /* Peek at the next token. */
23211 token = cp_lexer_peek_token (parser->lexer);
23212 /* If it is not a `,', we are done. */
23213 if (token->type != CPP_COMMA)
23215 /* Consume the `,'. */
23216 cp_lexer_consume_token (parser->lexer);
23219 return nreverse (types);
23222 /* Parse a try-block.
23225 try compound-statement handler-seq */
23228 cp_parser_try_block (cp_parser* parser)
23232 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23233 if (parser->in_function_body
23234 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23235 error ("%<try%> in %<constexpr%> function");
23237 try_block = begin_try_block ();
23238 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23239 finish_try_block (try_block);
23240 cp_parser_handler_seq (parser);
23241 finish_handler_sequence (try_block);
23246 /* Parse a function-try-block.
23248 function-try-block:
23249 try ctor-initializer [opt] function-body handler-seq */
23252 cp_parser_function_try_block (cp_parser* parser)
23254 tree compound_stmt;
23256 bool ctor_initializer_p;
23258 /* Look for the `try' keyword. */
23259 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23261 /* Let the rest of the front end know where we are. */
23262 try_block = begin_function_try_block (&compound_stmt);
23263 /* Parse the function-body. */
23264 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23265 (parser, /*in_function_try_block=*/true);
23266 /* We're done with the `try' part. */
23267 finish_function_try_block (try_block);
23268 /* Parse the handlers. */
23269 cp_parser_handler_seq (parser);
23270 /* We're done with the handlers. */
23271 finish_function_handler_sequence (try_block, compound_stmt);
23273 return ctor_initializer_p;
23276 /* Parse a handler-seq.
23279 handler handler-seq [opt] */
23282 cp_parser_handler_seq (cp_parser* parser)
23288 /* Parse the handler. */
23289 cp_parser_handler (parser);
23290 /* Peek at the next token. */
23291 token = cp_lexer_peek_token (parser->lexer);
23292 /* If it's not `catch' then there are no more handlers. */
23293 if (!cp_parser_is_keyword (token, RID_CATCH))
23298 /* Parse a handler.
23301 catch ( exception-declaration ) compound-statement */
23304 cp_parser_handler (cp_parser* parser)
23309 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23310 handler = begin_handler ();
23311 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23312 declaration = cp_parser_exception_declaration (parser);
23313 finish_handler_parms (declaration, handler);
23314 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23315 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23316 finish_handler (handler);
23319 /* Parse an exception-declaration.
23321 exception-declaration:
23322 type-specifier-seq declarator
23323 type-specifier-seq abstract-declarator
23327 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23328 ellipsis variant is used. */
23331 cp_parser_exception_declaration (cp_parser* parser)
23333 cp_decl_specifier_seq type_specifiers;
23334 cp_declarator *declarator;
23335 const char *saved_message;
23337 /* If it's an ellipsis, it's easy to handle. */
23338 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23340 /* Consume the `...' token. */
23341 cp_lexer_consume_token (parser->lexer);
23345 /* Types may not be defined in exception-declarations. */
23346 saved_message = parser->type_definition_forbidden_message;
23347 parser->type_definition_forbidden_message
23348 = G_("types may not be defined in exception-declarations");
23350 /* Parse the type-specifier-seq. */
23351 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23352 /*is_trailing_return=*/false,
23354 /* If it's a `)', then there is no declarator. */
23355 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23358 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23359 /*ctor_dtor_or_conv_p=*/NULL,
23360 /*parenthesized_p=*/NULL,
23361 /*member_p=*/false,
23362 /*friend_p=*/false);
23364 /* Restore the saved message. */
23365 parser->type_definition_forbidden_message = saved_message;
23367 if (!type_specifiers.any_specifiers_p)
23368 return error_mark_node;
23370 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23373 /* Parse a throw-expression.
23376 throw assignment-expression [opt]
23378 Returns a THROW_EXPR representing the throw-expression. */
23381 cp_parser_throw_expression (cp_parser* parser)
23386 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23387 token = cp_lexer_peek_token (parser->lexer);
23388 /* Figure out whether or not there is an assignment-expression
23389 following the "throw" keyword. */
23390 if (token->type == CPP_COMMA
23391 || token->type == CPP_SEMICOLON
23392 || token->type == CPP_CLOSE_PAREN
23393 || token->type == CPP_CLOSE_SQUARE
23394 || token->type == CPP_CLOSE_BRACE
23395 || token->type == CPP_COLON)
23396 expression = NULL_TREE;
23398 expression = cp_parser_assignment_expression (parser);
23400 return build_throw (expression);
23403 /* GNU Extensions */
23405 /* Parse an (optional) asm-specification.
23408 asm ( string-literal )
23410 If the asm-specification is present, returns a STRING_CST
23411 corresponding to the string-literal. Otherwise, returns
23415 cp_parser_asm_specification_opt (cp_parser* parser)
23418 tree asm_specification;
23420 /* Peek at the next token. */
23421 token = cp_lexer_peek_token (parser->lexer);
23422 /* If the next token isn't the `asm' keyword, then there's no
23423 asm-specification. */
23424 if (!cp_parser_is_keyword (token, RID_ASM))
23427 /* Consume the `asm' token. */
23428 cp_lexer_consume_token (parser->lexer);
23429 /* Look for the `('. */
23430 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23432 /* Look for the string-literal. */
23433 asm_specification = cp_parser_string_literal (parser, false, false);
23435 /* Look for the `)'. */
23436 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23438 return asm_specification;
23441 /* Parse an asm-operand-list.
23445 asm-operand-list , asm-operand
23448 string-literal ( expression )
23449 [ string-literal ] string-literal ( expression )
23451 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23452 each node is the expression. The TREE_PURPOSE is itself a
23453 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23454 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23455 is a STRING_CST for the string literal before the parenthesis. Returns
23456 ERROR_MARK_NODE if any of the operands are invalid. */
23459 cp_parser_asm_operand_list (cp_parser* parser)
23461 tree asm_operands = NULL_TREE;
23462 bool invalid_operands = false;
23466 tree string_literal;
23470 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23472 /* Consume the `[' token. */
23473 cp_lexer_consume_token (parser->lexer);
23474 /* Read the operand name. */
23475 name = cp_parser_identifier (parser);
23476 if (name != error_mark_node)
23477 name = build_string (IDENTIFIER_LENGTH (name),
23478 IDENTIFIER_POINTER (name));
23479 /* Look for the closing `]'. */
23480 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23484 /* Look for the string-literal. */
23485 string_literal = cp_parser_string_literal (parser, false, false);
23487 /* Look for the `('. */
23488 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23489 /* Parse the expression. */
23490 expression = cp_parser_expression (parser);
23491 /* Look for the `)'. */
23492 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23494 if (name == error_mark_node
23495 || string_literal == error_mark_node
23496 || expression == error_mark_node)
23497 invalid_operands = true;
23499 /* Add this operand to the list. */
23500 asm_operands = tree_cons (build_tree_list (name, string_literal),
23503 /* If the next token is not a `,', there are no more
23505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23507 /* Consume the `,'. */
23508 cp_lexer_consume_token (parser->lexer);
23511 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23514 /* Parse an asm-clobber-list.
23518 asm-clobber-list , string-literal
23520 Returns a TREE_LIST, indicating the clobbers in the order that they
23521 appeared. The TREE_VALUE of each node is a STRING_CST. */
23524 cp_parser_asm_clobber_list (cp_parser* parser)
23526 tree clobbers = NULL_TREE;
23530 tree string_literal;
23532 /* Look for the string literal. */
23533 string_literal = cp_parser_string_literal (parser, false, false);
23534 /* Add it to the list. */
23535 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23536 /* If the next token is not a `,', then the list is
23538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23540 /* Consume the `,' token. */
23541 cp_lexer_consume_token (parser->lexer);
23547 /* Parse an asm-label-list.
23551 asm-label-list , identifier
23553 Returns a TREE_LIST, indicating the labels in the order that they
23554 appeared. The TREE_VALUE of each node is a label. */
23557 cp_parser_asm_label_list (cp_parser* parser)
23559 tree labels = NULL_TREE;
23563 tree identifier, label, name;
23565 /* Look for the identifier. */
23566 identifier = cp_parser_identifier (parser);
23567 if (!error_operand_p (identifier))
23569 label = lookup_label (identifier);
23570 if (TREE_CODE (label) == LABEL_DECL)
23572 TREE_USED (label) = 1;
23573 check_goto (label);
23574 name = build_string (IDENTIFIER_LENGTH (identifier),
23575 IDENTIFIER_POINTER (identifier));
23576 labels = tree_cons (name, label, labels);
23579 /* If the next token is not a `,', then the list is
23581 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23583 /* Consume the `,' token. */
23584 cp_lexer_consume_token (parser->lexer);
23587 return nreverse (labels);
23590 /* Return TRUE iff the next tokens in the stream are possibly the
23591 beginning of a GNU extension attribute. */
23594 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23596 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23599 /* Return TRUE iff the next tokens in the stream are possibly the
23600 beginning of a standard C++-11 attribute specifier. */
23603 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23605 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23608 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23609 beginning of a standard C++-11 attribute specifier. */
23612 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23614 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23616 return (cxx_dialect >= cxx11
23617 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23618 || (token->type == CPP_OPEN_SQUARE
23619 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23620 && token->type == CPP_OPEN_SQUARE)));
23623 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23624 beginning of a GNU extension attribute. */
23627 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23629 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23631 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23634 /* Return true iff the next tokens can be the beginning of either a
23635 GNU attribute list, or a standard C++11 attribute sequence. */
23638 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23640 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23641 || cp_next_tokens_can_be_std_attribute_p (parser));
23644 /* Return true iff the next Nth tokens can be the beginning of either
23645 a GNU attribute list, or a standard C++11 attribute sequence. */
23648 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23650 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23651 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23654 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23655 of GNU attributes, or return NULL. */
23658 cp_parser_attributes_opt (cp_parser *parser)
23660 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23661 return cp_parser_gnu_attributes_opt (parser);
23662 return cp_parser_std_attribute_spec_seq (parser);
23665 #define CILK_SIMD_FN_CLAUSE_MASK \
23666 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23667 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23668 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23669 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23670 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23672 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23673 vector [(<clauses>)] */
23676 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23678 bool first_p = parser->cilk_simd_fn_info == NULL;
23679 cp_token *token = v_token;
23682 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23683 parser->cilk_simd_fn_info->error_seen = false;
23684 parser->cilk_simd_fn_info->fndecl_seen = false;
23685 parser->cilk_simd_fn_info->tokens = vNULL;
23687 int paren_scope = 0;
23688 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23690 cp_lexer_consume_token (parser->lexer);
23691 v_token = cp_lexer_peek_token (parser->lexer);
23694 while (paren_scope > 0)
23696 token = cp_lexer_peek_token (parser->lexer);
23697 if (token->type == CPP_OPEN_PAREN)
23699 else if (token->type == CPP_CLOSE_PAREN)
23701 /* Do not push the last ')' */
23702 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23703 cp_lexer_consume_token (parser->lexer);
23706 token->type = CPP_PRAGMA_EOL;
23707 parser->lexer->next_token = token;
23708 cp_lexer_consume_token (parser->lexer);
23710 struct cp_token_cache *cp
23711 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23712 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23715 /* Parse an (optional) series of attributes.
23718 attributes attribute
23721 __attribute__ (( attribute-list [opt] ))
23723 The return value is as for cp_parser_gnu_attribute_list. */
23726 cp_parser_gnu_attributes_opt (cp_parser* parser)
23728 tree attributes = NULL_TREE;
23733 tree attribute_list;
23736 /* Peek at the next token. */
23737 token = cp_lexer_peek_token (parser->lexer);
23738 /* If it's not `__attribute__', then we're done. */
23739 if (token->keyword != RID_ATTRIBUTE)
23742 /* Consume the `__attribute__' keyword. */
23743 cp_lexer_consume_token (parser->lexer);
23744 /* Look for the two `(' tokens. */
23745 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23746 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23748 /* Peek at the next token. */
23749 token = cp_lexer_peek_token (parser->lexer);
23750 if (token->type != CPP_CLOSE_PAREN)
23751 /* Parse the attribute-list. */
23752 attribute_list = cp_parser_gnu_attribute_list (parser);
23754 /* If the next token is a `)', then there is no attribute
23756 attribute_list = NULL;
23758 /* Look for the two `)' tokens. */
23759 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23761 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23764 cp_parser_skip_to_end_of_statement (parser);
23766 /* Add these new attributes to the list. */
23767 attributes = chainon (attributes, attribute_list);
23773 /* Parse a GNU attribute-list.
23777 attribute-list , attribute
23781 identifier ( identifier )
23782 identifier ( identifier , expression-list )
23783 identifier ( expression-list )
23785 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23786 to an attribute. The TREE_PURPOSE of each node is the identifier
23787 indicating which attribute is in use. The TREE_VALUE represents
23788 the arguments, if any. */
23791 cp_parser_gnu_attribute_list (cp_parser* parser)
23793 tree attribute_list = NULL_TREE;
23794 bool save_translate_strings_p = parser->translate_strings_p;
23796 parser->translate_strings_p = false;
23803 /* Look for the identifier. We also allow keywords here; for
23804 example `__attribute__ ((const))' is legal. */
23805 token = cp_lexer_peek_token (parser->lexer);
23806 if (token->type == CPP_NAME
23807 || token->type == CPP_KEYWORD)
23809 tree arguments = NULL_TREE;
23811 /* Consume the token, but save it since we need it for the
23812 SIMD enabled function parsing. */
23813 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23815 /* Save away the identifier that indicates which attribute
23817 identifier = (token->type == CPP_KEYWORD)
23818 /* For keywords, use the canonical spelling, not the
23819 parsed identifier. */
23820 ? ridpointers[(int) token->keyword]
23821 : id_token->u.value;
23823 attribute = build_tree_list (identifier, NULL_TREE);
23825 /* Peek at the next token. */
23826 token = cp_lexer_peek_token (parser->lexer);
23827 /* If it's an `(', then parse the attribute arguments. */
23828 if (token->type == CPP_OPEN_PAREN)
23830 vec<tree, va_gc> *vec;
23831 int attr_flag = (attribute_takes_identifier_p (identifier)
23832 ? id_attr : normal_attr);
23833 if (is_cilkplus_vector_p (identifier))
23835 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23839 vec = cp_parser_parenthesized_expression_list
23840 (parser, attr_flag, /*cast_p=*/false,
23841 /*allow_expansion_p=*/false,
23842 /*non_constant_p=*/NULL);
23844 arguments = error_mark_node;
23847 arguments = build_tree_list_vec (vec);
23848 release_tree_vector (vec);
23850 /* Save the arguments away. */
23851 TREE_VALUE (attribute) = arguments;
23853 else if (is_cilkplus_vector_p (identifier))
23855 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23859 if (arguments != error_mark_node)
23861 /* Add this attribute to the list. */
23862 TREE_CHAIN (attribute) = attribute_list;
23863 attribute_list = attribute;
23866 token = cp_lexer_peek_token (parser->lexer);
23868 /* Now, look for more attributes. If the next token isn't a
23869 `,', we're done. */
23870 if (token->type != CPP_COMMA)
23873 /* Consume the comma and keep going. */
23874 cp_lexer_consume_token (parser->lexer);
23876 parser->translate_strings_p = save_translate_strings_p;
23878 /* We built up the list in reverse order. */
23879 return nreverse (attribute_list);
23882 /* Parse a standard C++11 attribute.
23884 The returned representation is a TREE_LIST which TREE_PURPOSE is
23885 the scoped name of the attribute, and the TREE_VALUE is its
23888 Note that the scoped name of the attribute is itself a TREE_LIST
23889 which TREE_PURPOSE is the namespace of the attribute, and
23890 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23891 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23892 and which TREE_PURPOSE is directly the attribute name.
23894 Clients of the attribute code should use get_attribute_namespace
23895 and get_attribute_name to get the actual namespace and name of
23896 attributes, regardless of their being GNU or C++11 attributes.
23899 attribute-token attribute-argument-clause [opt]
23903 attribute-scoped-token
23905 attribute-scoped-token:
23906 attribute-namespace :: identifier
23908 attribute-namespace:
23911 attribute-argument-clause:
23912 ( balanced-token-seq )
23914 balanced-token-seq:
23915 balanced-token [opt]
23916 balanced-token-seq balanced-token
23919 ( balanced-token-seq )
23920 [ balanced-token-seq ]
23921 { balanced-token-seq }. */
23924 cp_parser_std_attribute (cp_parser *parser)
23926 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23929 /* First, parse name of the attribute, a.k.a attribute-token. */
23931 token = cp_lexer_peek_token (parser->lexer);
23932 if (token->type == CPP_NAME)
23933 attr_id = token->u.value;
23934 else if (token->type == CPP_KEYWORD)
23935 attr_id = ridpointers[(int) token->keyword];
23936 else if (token->flags & NAMED_OP)
23937 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23939 if (attr_id == NULL_TREE)
23942 cp_lexer_consume_token (parser->lexer);
23944 token = cp_lexer_peek_token (parser->lexer);
23945 if (token->type == CPP_SCOPE)
23947 /* We are seeing a scoped attribute token. */
23949 cp_lexer_consume_token (parser->lexer);
23952 token = cp_lexer_consume_token (parser->lexer);
23953 if (token->type == CPP_NAME)
23954 attr_id = token->u.value;
23955 else if (token->type == CPP_KEYWORD)
23956 attr_id = ridpointers[(int) token->keyword];
23959 error_at (token->location,
23960 "expected an identifier for the attribute name");
23961 return error_mark_node;
23963 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23965 token = cp_lexer_peek_token (parser->lexer);
23969 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23971 /* C++11 noreturn attribute is equivalent to GNU's. */
23972 if (is_attribute_p ("noreturn", attr_id))
23973 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23974 /* C++14 deprecated attribute is equivalent to GNU's. */
23975 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23977 if (cxx_dialect == cxx11)
23978 pedwarn (token->location, OPT_Wpedantic,
23979 "%<deprecated%> is a C++14 feature;"
23980 " use %<gnu::deprecated%>");
23981 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23983 /* Transactional Memory TS optimize_for_synchronized attribute is
23984 equivalent to GNU transaction_callable. */
23985 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23986 TREE_PURPOSE (attribute)
23987 = get_identifier ("transaction_callable");
23988 /* Transactional Memory attributes are GNU attributes. */
23989 else if (tm_attr_to_mask (attr_id))
23990 TREE_PURPOSE (attribute) = attr_id;
23993 /* Now parse the optional argument clause of the attribute. */
23995 if (token->type != CPP_OPEN_PAREN)
23999 vec<tree, va_gc> *vec;
24000 int attr_flag = normal_attr;
24002 if (attr_ns == get_identifier ("gnu")
24003 && attribute_takes_identifier_p (attr_id))
24004 /* A GNU attribute that takes an identifier in parameter. */
24005 attr_flag = id_attr;
24007 vec = cp_parser_parenthesized_expression_list
24008 (parser, attr_flag, /*cast_p=*/false,
24009 /*allow_expansion_p=*/true,
24010 /*non_constant_p=*/NULL);
24012 arguments = error_mark_node;
24015 arguments = build_tree_list_vec (vec);
24016 release_tree_vector (vec);
24019 if (arguments == error_mark_node)
24020 attribute = error_mark_node;
24022 TREE_VALUE (attribute) = arguments;
24028 /* Check that the attribute ATTRIBUTE appears at most once in the
24029 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24030 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24031 isn't implemented yet in GCC. */
24034 cp_parser_check_std_attribute (tree attributes, tree attribute)
24038 tree name = get_attribute_name (attribute);
24039 if (is_attribute_p ("noreturn", name)
24040 && lookup_attribute ("noreturn", attributes))
24041 error ("attribute noreturn can appear at most once "
24042 "in an attribute-list");
24043 else if (is_attribute_p ("deprecated", name)
24044 && lookup_attribute ("deprecated", attributes))
24045 error ("attribute deprecated can appear at most once "
24046 "in an attribute-list");
24050 /* Parse a list of standard C++-11 attributes.
24054 attribute-list , attribute[opt]
24056 attribute-list , attribute ...
24060 cp_parser_std_attribute_list (cp_parser *parser)
24062 tree attributes = NULL_TREE, attribute = NULL_TREE;
24063 cp_token *token = NULL;
24067 attribute = cp_parser_std_attribute (parser);
24068 if (attribute == error_mark_node)
24070 if (attribute != NULL_TREE)
24072 cp_parser_check_std_attribute (attributes, attribute);
24073 TREE_CHAIN (attribute) = attributes;
24074 attributes = attribute;
24076 token = cp_lexer_peek_token (parser->lexer);
24077 if (token->type == CPP_ELLIPSIS)
24079 cp_lexer_consume_token (parser->lexer);
24080 if (attribute == NULL_TREE)
24081 error_at (token->location,
24082 "expected attribute before %<...%>");
24084 TREE_VALUE (attribute)
24085 = make_pack_expansion (TREE_VALUE (attribute));
24086 token = cp_lexer_peek_token (parser->lexer);
24088 if (token->type != CPP_COMMA)
24090 cp_lexer_consume_token (parser->lexer);
24092 attributes = nreverse (attributes);
24096 /* Parse a standard C++-11 attribute specifier.
24098 attribute-specifier:
24099 [ [ attribute-list ] ]
24100 alignment-specifier
24102 alignment-specifier:
24103 alignas ( type-id ... [opt] )
24104 alignas ( alignment-expression ... [opt] ). */
24107 cp_parser_std_attribute_spec (cp_parser *parser)
24109 tree attributes = NULL_TREE;
24110 cp_token *token = cp_lexer_peek_token (parser->lexer);
24112 if (token->type == CPP_OPEN_SQUARE
24113 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24115 cp_lexer_consume_token (parser->lexer);
24116 cp_lexer_consume_token (parser->lexer);
24118 attributes = cp_parser_std_attribute_list (parser);
24120 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24121 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24122 cp_parser_skip_to_end_of_statement (parser);
24124 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24125 when we are sure that we have actually parsed them. */
24126 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24132 /* Look for an alignment-specifier. */
24134 token = cp_lexer_peek_token (parser->lexer);
24136 if (token->type != CPP_KEYWORD
24137 || token->keyword != RID_ALIGNAS)
24140 cp_lexer_consume_token (parser->lexer);
24141 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24143 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24145 cp_parser_error (parser, "expected %<(%>");
24146 return error_mark_node;
24149 cp_parser_parse_tentatively (parser);
24150 alignas_expr = cp_parser_type_id (parser);
24152 if (!cp_parser_parse_definitely (parser))
24154 alignas_expr = cp_parser_assignment_expression (parser);
24155 if (alignas_expr == error_mark_node)
24156 cp_parser_skip_to_end_of_statement (parser);
24157 if (alignas_expr == NULL_TREE
24158 || alignas_expr == error_mark_node)
24159 return alignas_expr;
24162 alignas_expr = cxx_alignas_expr (alignas_expr);
24163 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24165 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24167 cp_lexer_consume_token (parser->lexer);
24168 alignas_expr = make_pack_expansion (alignas_expr);
24171 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24173 cp_parser_error (parser, "expected %<)%>");
24174 return error_mark_node;
24177 /* Build the C++-11 representation of an 'aligned'
24180 build_tree_list (build_tree_list (get_identifier ("gnu"),
24181 get_identifier ("aligned")),
24188 /* Parse a standard C++-11 attribute-specifier-seq.
24190 attribute-specifier-seq:
24191 attribute-specifier-seq [opt] attribute-specifier
24195 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24197 tree attr_specs = NULL_TREE;
24198 tree attr_last = NULL_TREE;
24202 tree attr_spec = cp_parser_std_attribute_spec (parser);
24203 if (attr_spec == NULL_TREE)
24205 if (attr_spec == error_mark_node)
24206 return error_mark_node;
24209 TREE_CHAIN (attr_last) = attr_spec;
24211 attr_specs = attr_last = attr_spec;
24212 attr_last = tree_last (attr_last);
24218 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24219 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24220 current value of the PEDANTIC flag, regardless of whether or not
24221 the `__extension__' keyword is present. The caller is responsible
24222 for restoring the value of the PEDANTIC flag. */
24225 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24227 /* Save the old value of the PEDANTIC flag. */
24228 *saved_pedantic = pedantic;
24230 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24232 /* Consume the `__extension__' token. */
24233 cp_lexer_consume_token (parser->lexer);
24234 /* We're not being pedantic while the `__extension__' keyword is
24244 /* Parse a label declaration.
24247 __label__ label-declarator-seq ;
24249 label-declarator-seq:
24250 identifier , label-declarator-seq
24254 cp_parser_label_declaration (cp_parser* parser)
24256 /* Look for the `__label__' keyword. */
24257 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24263 /* Look for an identifier. */
24264 identifier = cp_parser_identifier (parser);
24265 /* If we failed, stop. */
24266 if (identifier == error_mark_node)
24268 /* Declare it as a label. */
24269 finish_label_decl (identifier);
24270 /* If the next token is a `;', stop. */
24271 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24273 /* Look for the `,' separating the label declarations. */
24274 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24277 /* Look for the final `;'. */
24278 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24281 // -------------------------------------------------------------------------- //
24284 // Parse a requires clause.
24286 // requires-clause:
24287 // 'requires' logical-or-expression
24289 // The required logical-or-expression must be a constant expression. Note
24290 // that we don't check that the expression is constepxr here. We defer until
24291 // we analyze constraints and then, we only check atomic constraints.
24293 cp_parser_requires_clause (cp_parser *parser)
24295 // Parse the requires clause so that it is not automatically folded.
24296 ++processing_template_decl;
24297 tree expr = cp_parser_binary_expression (parser, false, false,
24298 PREC_NOT_OPERATOR, NULL);
24299 if (check_for_bare_parameter_packs (expr))
24300 expr = error_mark_node;
24301 --processing_template_decl;
24305 // Optionally parse a requires clause:
24307 cp_parser_requires_clause_opt (cp_parser *parser)
24309 cp_token *tok = cp_lexer_peek_token (parser->lexer);
24310 if (tok->keyword != RID_REQUIRES)
24312 if (!flag_concepts && tok->type == CPP_NAME
24313 && tok->u.value == ridpointers[RID_REQUIRES])
24315 error_at (cp_lexer_peek_token (parser->lexer)->location,
24316 "%<requires%> only available with -fconcepts");
24317 /* Parse and discard the requires-clause. */
24318 cp_lexer_consume_token (parser->lexer);
24319 cp_parser_requires_clause (parser);
24323 cp_lexer_consume_token (parser->lexer);
24324 return cp_parser_requires_clause (parser);
24328 /*---------------------------------------------------------------------------
24329 Requires expressions
24330 ---------------------------------------------------------------------------*/
24332 /* Parse a requires expression
24334 requirement-expression:
24335 'requires' requirement-parameter-list [opt] requirement-body */
24337 cp_parser_requires_expression (cp_parser *parser)
24339 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24340 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24342 /* A requires-expression shall appear only within a concept
24343 definition or a requires-clause.
24345 TODO: Implement this diagnostic correctly. */
24346 if (!processing_template_decl)
24348 error_at (loc, "a requires expression cannot appear outside a template");
24349 cp_parser_skip_to_end_of_statement (parser);
24350 return error_mark_node;
24355 /* Local parameters are delared as variables within the scope
24356 of the expression. They are not visible past the end of
24357 the expression. Expressions within the requires-expression
24358 are unevaluated. */
24359 struct scope_sentinel
24363 ++cp_unevaluated_operand;
24364 begin_scope (sk_block, NULL_TREE);
24369 pop_bindings_and_leave_scope ();
24370 --cp_unevaluated_operand;
24374 /* Parse the optional parameter list. */
24375 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24377 parms = cp_parser_requirement_parameter_list (parser);
24378 if (parms == error_mark_node)
24379 return error_mark_node;
24384 /* Parse the requirement body. */
24385 reqs = cp_parser_requirement_body (parser);
24386 if (reqs == error_mark_node)
24387 return error_mark_node;
24390 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24392 grokparms (parms, &parms);
24393 return finish_requires_expr (parms, reqs);
24396 /* Parse a parameterized requirement.
24398 requirement-parameter-list:
24399 '(' parameter-declaration-clause ')' */
24401 cp_parser_requirement_parameter_list (cp_parser *parser)
24403 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24404 return error_mark_node;
24406 tree parms = cp_parser_parameter_declaration_clause (parser);
24408 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24409 return error_mark_node;
24414 /* Parse the body of a requirement.
24417 '{' requirement-list '}' */
24419 cp_parser_requirement_body (cp_parser *parser)
24421 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24422 return error_mark_node;
24424 tree reqs = cp_parser_requirement_list (parser);
24426 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24427 return error_mark_node;
24432 /* Parse a list of requirements.
24436 requirement-list ';' requirement[opt] */
24438 cp_parser_requirement_list (cp_parser *parser)
24440 tree result = NULL_TREE;
24443 tree req = cp_parser_requirement (parser);
24444 if (req == error_mark_node)
24445 return error_mark_node;
24447 result = tree_cons (NULL_TREE, req, result);
24449 /* If we see a semi-colon, consume it. */
24450 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24451 cp_lexer_consume_token (parser->lexer);
24453 /* Stop processing at the end of the list. */
24454 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24458 /* Reverse the order of requirements so they are analyzed in
24459 declaration order. */
24460 return nreverse (result);
24463 /* Parse a syntactic requirement or type requirement.
24467 compound-requirement
24469 nested-requirement */
24471 cp_parser_requirement (cp_parser *parser)
24473 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24474 return cp_parser_compound_requirement (parser);
24475 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24476 return cp_parser_type_requirement (parser);
24477 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24478 return cp_parser_nested_requirement (parser);
24480 return cp_parser_simple_requirement (parser);
24483 /* Parse a simple requirement.
24485 simple-requirement:
24488 cp_parser_simple_requirement (cp_parser *parser)
24490 tree expr = cp_parser_expression (parser, NULL, false, false);
24491 if (!expr || expr == error_mark_node)
24492 return error_mark_node;
24494 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24495 return error_mark_node;
24497 return finish_simple_requirement (expr);
24500 /* Parse a type requirement
24503 nested-name-specifier [opt] required-type-name ';'
24505 required-type-name:
24507 'template' [opt] simple-template-id */
24509 cp_parser_type_requirement (cp_parser *parser)
24511 cp_lexer_consume_token (parser->lexer);
24513 // Save the scope before parsing name specifiers.
24514 tree saved_scope = parser->scope;
24515 tree saved_object_scope = parser->object_scope;
24516 tree saved_qualifying_scope = parser->qualifying_scope;
24517 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24518 cp_parser_nested_name_specifier_opt (parser,
24519 /*typename_keyword_p=*/true,
24520 /*check_dependency_p=*/false,
24522 /*is_declaration=*/false);
24525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24527 cp_lexer_consume_token (parser->lexer);
24528 type = cp_parser_template_id (parser,
24529 /*template_keyword_p=*/true,
24530 /*check_dependency=*/false,
24531 /*tag_type=*/none_type,
24532 /*is_declaration=*/false);
24533 type = make_typename_type (parser->scope, type, typename_type,
24534 /*complain=*/tf_error);
24537 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24539 if (TREE_CODE (type) == TYPE_DECL)
24540 type = TREE_TYPE (type);
24542 parser->scope = saved_scope;
24543 parser->object_scope = saved_object_scope;
24544 parser->qualifying_scope = saved_qualifying_scope;
24546 if (type == error_mark_node)
24547 cp_parser_skip_to_end_of_statement (parser);
24549 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24550 return error_mark_node;
24551 if (type == error_mark_node)
24552 return error_mark_node;
24554 return finish_type_requirement (type);
24557 /* Parse a compound requirement
24559 compound-requirement:
24560 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24562 cp_parser_compound_requirement (cp_parser *parser)
24564 /* Parse an expression enclosed in '{ }'s. */
24565 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24566 return error_mark_node;
24568 tree expr = cp_parser_expression (parser, NULL, false, false);
24569 if (!expr || expr == error_mark_node)
24570 return error_mark_node;
24572 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24573 return error_mark_node;
24575 /* Parse the optional noexcept. */
24576 bool noexcept_p = false;
24577 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24579 cp_lexer_consume_token (parser->lexer);
24583 /* Parse the optional trailing return type. */
24584 tree type = NULL_TREE;
24585 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24587 cp_lexer_consume_token (parser->lexer);
24588 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24589 parser->in_result_type_constraint_p = true;
24590 type = cp_parser_trailing_type_id (parser);
24591 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24592 if (type == error_mark_node)
24593 return error_mark_node;
24596 return finish_compound_requirement (expr, type, noexcept_p);
24599 /* Parse a nested requirement. This is the same as a requires clause.
24601 nested-requirement:
24604 cp_parser_nested_requirement (cp_parser *parser)
24606 cp_lexer_consume_token (parser->lexer);
24607 tree req = cp_parser_requires_clause (parser);
24608 if (req == error_mark_node)
24609 return error_mark_node;
24610 return finish_nested_requirement (req);
24613 /* Support Functions */
24615 /* Return the appropriate prefer_type argument for lookup_name_real based on
24619 prefer_type_arg (tag_types tag_type)
24623 case none_type: return 0; // No preference.
24624 case scope_type: return 1; // Type or namespace.
24625 default: return 2; // Type only.
24629 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24630 NAME should have one of the representations used for an
24631 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24632 is returned. If PARSER->SCOPE is a dependent type, then a
24633 SCOPE_REF is returned.
24635 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24636 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24637 was formed. Abstractly, such entities should not be passed to this
24638 function, because they do not need to be looked up, but it is
24639 simpler to check for this special case here, rather than at the
24642 In cases not explicitly covered above, this function returns a
24643 DECL, OVERLOAD, or baselink representing the result of the lookup.
24644 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24647 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24648 (e.g., "struct") that was used. In that case bindings that do not
24649 refer to types are ignored.
24651 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24654 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24657 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24660 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24661 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24662 NULL_TREE otherwise. */
24665 cp_parser_lookup_name (cp_parser *parser, tree name,
24666 enum tag_types tag_type,
24669 bool check_dependency,
24670 tree *ambiguous_decls,
24671 location_t name_location)
24674 tree object_type = parser->context->object_type;
24676 /* Assume that the lookup will be unambiguous. */
24677 if (ambiguous_decls)
24678 *ambiguous_decls = NULL_TREE;
24680 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24681 no longer valid. Note that if we are parsing tentatively, and
24682 the parse fails, OBJECT_TYPE will be automatically restored. */
24683 parser->context->object_type = NULL_TREE;
24685 if (name == error_mark_node)
24686 return error_mark_node;
24688 /* A template-id has already been resolved; there is no lookup to
24690 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24692 if (BASELINK_P (name))
24694 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24695 == TEMPLATE_ID_EXPR);
24699 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24700 it should already have been checked to make sure that the name
24701 used matches the type being destroyed. */
24702 if (TREE_CODE (name) == BIT_NOT_EXPR)
24706 /* Figure out to which type this destructor applies. */
24708 type = parser->scope;
24709 else if (object_type)
24710 type = object_type;
24712 type = current_class_type;
24713 /* If that's not a class type, there is no destructor. */
24714 if (!type || !CLASS_TYPE_P (type))
24715 return error_mark_node;
24716 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24717 lazily_declare_fn (sfk_destructor, type);
24718 if (!CLASSTYPE_DESTRUCTORS (type))
24719 return error_mark_node;
24720 /* If it was a class type, return the destructor. */
24721 return CLASSTYPE_DESTRUCTORS (type);
24724 /* By this point, the NAME should be an ordinary identifier. If
24725 the id-expression was a qualified name, the qualifying scope is
24726 stored in PARSER->SCOPE at this point. */
24727 gcc_assert (identifier_p (name));
24729 /* Perform the lookup. */
24734 if (parser->scope == error_mark_node)
24735 return error_mark_node;
24737 /* If the SCOPE is dependent, the lookup must be deferred until
24738 the template is instantiated -- unless we are explicitly
24739 looking up names in uninstantiated templates. Even then, we
24740 cannot look up the name if the scope is not a class type; it
24741 might, for example, be a template type parameter. */
24742 dependent_p = (TYPE_P (parser->scope)
24743 && dependent_scope_p (parser->scope));
24744 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24746 /* Defer lookup. */
24747 decl = error_mark_node;
24750 tree pushed_scope = NULL_TREE;
24752 /* If PARSER->SCOPE is a dependent type, then it must be a
24753 class type, and we must not be checking dependencies;
24754 otherwise, we would have processed this lookup above. So
24755 that PARSER->SCOPE is not considered a dependent base by
24756 lookup_member, we must enter the scope here. */
24758 pushed_scope = push_scope (parser->scope);
24760 /* If the PARSER->SCOPE is a template specialization, it
24761 may be instantiated during name lookup. In that case,
24762 errors may be issued. Even if we rollback the current
24763 tentative parse, those errors are valid. */
24764 decl = lookup_qualified_name (parser->scope, name,
24765 prefer_type_arg (tag_type),
24766 /*complain=*/true);
24768 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24769 lookup result and the nested-name-specifier nominates a class C:
24770 * if the name specified after the nested-name-specifier, when
24771 looked up in C, is the injected-class-name of C (Clause 9), or
24772 * if the name specified after the nested-name-specifier is the
24773 same as the identifier or the simple-template-id's template-
24774 name in the last component of the nested-name-specifier,
24775 the name is instead considered to name the constructor of
24776 class C. [ Note: for example, the constructor is not an
24777 acceptable lookup result in an elaborated-type-specifier so
24778 the constructor would not be used in place of the
24779 injected-class-name. --end note ] Such a constructor name
24780 shall be used only in the declarator-id of a declaration that
24781 names a constructor or in a using-declaration. */
24782 if (tag_type == none_type
24783 && DECL_SELF_REFERENCE_P (decl)
24784 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24785 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24786 prefer_type_arg (tag_type),
24787 /*complain=*/true);
24789 /* If we have a single function from a using decl, pull it out. */
24790 if (TREE_CODE (decl) == OVERLOAD
24791 && !really_overloaded_fn (decl))
24792 decl = OVL_FUNCTION (decl);
24795 pop_scope (pushed_scope);
24798 /* If the scope is a dependent type and either we deferred lookup or
24799 we did lookup but didn't find the name, rememeber the name. */
24800 if (decl == error_mark_node && TYPE_P (parser->scope)
24801 && dependent_type_p (parser->scope))
24807 /* The resolution to Core Issue 180 says that `struct
24808 A::B' should be considered a type-name, even if `A'
24810 type = make_typename_type (parser->scope, name, tag_type,
24811 /*complain=*/tf_error);
24812 if (type != error_mark_node)
24813 decl = TYPE_NAME (type);
24815 else if (is_template
24816 && (cp_parser_next_token_ends_template_argument_p (parser)
24817 || cp_lexer_next_token_is (parser->lexer,
24819 decl = make_unbound_class_template (parser->scope,
24821 /*complain=*/tf_error);
24823 decl = build_qualified_name (/*type=*/NULL_TREE,
24824 parser->scope, name,
24827 parser->qualifying_scope = parser->scope;
24828 parser->object_scope = NULL_TREE;
24830 else if (object_type)
24832 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24833 OBJECT_TYPE is not a class. */
24834 if (CLASS_TYPE_P (object_type))
24835 /* If the OBJECT_TYPE is a template specialization, it may
24836 be instantiated during name lookup. In that case, errors
24837 may be issued. Even if we rollback the current tentative
24838 parse, those errors are valid. */
24839 decl = lookup_member (object_type,
24842 prefer_type_arg (tag_type),
24843 tf_warning_or_error);
24849 /* Look it up in the enclosing context. */
24850 decl = lookup_name_real (name, prefer_type_arg (tag_type),
24852 /*block_p=*/true, is_namespace, 0);
24853 /* DR 141 says when looking for a template-name after -> or ., only
24854 consider class templates. We need to fix our handling of
24855 dependent expressions to implement that properly, but for now
24856 let's ignore namespace-scope function templates. */
24857 if (decl && is_template && !DECL_TYPE_TEMPLATE_P (decl))
24860 if (is_overloaded_fn (d))
24861 d = get_first_fn (d);
24862 if (DECL_P (d) && !DECL_CLASS_SCOPE_P (d))
24866 if (object_type == unknown_type_node)
24867 /* The object is type-dependent, so we can't look anything up; we used
24868 this to get the DR 141 behavior. */
24869 object_type = NULL_TREE;
24870 parser->object_scope = object_type;
24871 parser->qualifying_scope = NULL_TREE;
24875 decl = lookup_name_real (name, prefer_type_arg (tag_type),
24877 /*block_p=*/true, is_namespace, 0);
24878 parser->qualifying_scope = NULL_TREE;
24879 parser->object_scope = NULL_TREE;
24882 /* If the lookup failed, let our caller know. */
24883 if (!decl || decl == error_mark_node)
24884 return error_mark_node;
24886 /* Pull out the template from an injected-class-name (or multiple). */
24888 decl = maybe_get_template_decl_from_type_decl (decl);
24890 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24891 if (TREE_CODE (decl) == TREE_LIST)
24893 if (ambiguous_decls)
24894 *ambiguous_decls = decl;
24895 /* The error message we have to print is too complicated for
24896 cp_parser_error, so we incorporate its actions directly. */
24897 if (!cp_parser_simulate_error (parser))
24899 error_at (name_location, "reference to %qD is ambiguous",
24901 print_candidates (decl);
24903 return error_mark_node;
24906 gcc_assert (DECL_P (decl)
24907 || TREE_CODE (decl) == OVERLOAD
24908 || TREE_CODE (decl) == SCOPE_REF
24909 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24910 || BASELINK_P (decl));
24912 /* If we have resolved the name of a member declaration, check to
24913 see if the declaration is accessible. When the name resolves to
24914 set of overloaded functions, accessibility is checked when
24915 overload resolution is done.
24917 During an explicit instantiation, access is not checked at all,
24918 as per [temp.explicit]. */
24920 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24922 maybe_record_typedef_use (decl);
24924 return cp_expr (decl, name_location);
24927 /* Like cp_parser_lookup_name, but for use in the typical case where
24928 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24929 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24932 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24934 return cp_parser_lookup_name (parser, name,
24936 /*is_template=*/false,
24937 /*is_namespace=*/false,
24938 /*check_dependency=*/true,
24939 /*ambiguous_decls=*/NULL,
24943 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24944 the current context, return the TYPE_DECL. If TAG_NAME_P is
24945 true, the DECL indicates the class being defined in a class-head,
24946 or declared in an elaborated-type-specifier.
24948 Otherwise, return DECL. */
24951 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24953 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24954 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24957 template <typename T> struct B;
24960 template <typename T> struct A::B {};
24962 Similarly, in an elaborated-type-specifier:
24964 namespace N { struct X{}; }
24967 template <typename T> friend struct N::X;
24970 However, if the DECL refers to a class type, and we are in
24971 the scope of the class, then the name lookup automatically
24972 finds the TYPE_DECL created by build_self_reference rather
24973 than a TEMPLATE_DECL. For example, in:
24975 template <class T> struct S {
24979 there is no need to handle such case. */
24981 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24982 return DECL_TEMPLATE_RESULT (decl);
24987 /* If too many, or too few, template-parameter lists apply to the
24988 declarator, issue an error message. Returns TRUE if all went well,
24989 and FALSE otherwise. */
24992 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24993 cp_declarator *declarator,
24994 location_t declarator_location)
24996 switch (declarator->kind)
25000 unsigned num_templates = 0;
25001 tree scope = declarator->u.id.qualifying_scope;
25004 num_templates = num_template_headers_for_class (scope);
25005 else if (TREE_CODE (declarator->u.id.unqualified_name)
25006 == TEMPLATE_ID_EXPR)
25007 /* If the DECLARATOR has the form `X<y>' then it uses one
25008 additional level of template parameters. */
25011 return cp_parser_check_template_parameters
25012 (parser, num_templates, declarator_location, declarator);
25018 case cdk_reference:
25020 return (cp_parser_check_declarator_template_parameters
25021 (parser, declarator->declarator, declarator_location));
25027 gcc_unreachable ();
25032 /* NUM_TEMPLATES were used in the current declaration. If that is
25033 invalid, return FALSE and issue an error messages. Otherwise,
25034 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25035 declarator and we can print more accurate diagnostics. */
25038 cp_parser_check_template_parameters (cp_parser* parser,
25039 unsigned num_templates,
25040 location_t location,
25041 cp_declarator *declarator)
25043 /* If there are the same number of template classes and parameter
25044 lists, that's OK. */
25045 if (parser->num_template_parameter_lists == num_templates)
25047 /* If there are more, but only one more, then we are referring to a
25048 member template. That's OK too. */
25049 if (parser->num_template_parameter_lists == num_templates + 1)
25051 /* If there are more template classes than parameter lists, we have
25054 template <class T> void S<T>::R<T>::f (); */
25055 if (parser->num_template_parameter_lists < num_templates)
25057 if (declarator && !current_function_decl)
25058 error_at (location, "specializing member %<%T::%E%> "
25059 "requires %<template<>%> syntax",
25060 declarator->u.id.qualifying_scope,
25061 declarator->u.id.unqualified_name);
25062 else if (declarator)
25063 error_at (location, "invalid declaration of %<%T::%E%>",
25064 declarator->u.id.qualifying_scope,
25065 declarator->u.id.unqualified_name);
25067 error_at (location, "too few template-parameter-lists");
25070 /* Otherwise, there are too many template parameter lists. We have
25073 template <class T> template <class U> void S::f(); */
25074 error_at (location, "too many template-parameter-lists");
25078 /* Parse an optional `::' token indicating that the following name is
25079 from the global namespace. If so, PARSER->SCOPE is set to the
25080 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25081 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25082 Returns the new value of PARSER->SCOPE, if the `::' token is
25083 present, and NULL_TREE otherwise. */
25086 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25090 /* Peek at the next token. */
25091 token = cp_lexer_peek_token (parser->lexer);
25092 /* If we're looking at a `::' token then we're starting from the
25093 global namespace, not our current location. */
25094 if (token->type == CPP_SCOPE)
25096 /* Consume the `::' token. */
25097 cp_lexer_consume_token (parser->lexer);
25098 /* Set the SCOPE so that we know where to start the lookup. */
25099 parser->scope = global_namespace;
25100 parser->qualifying_scope = global_namespace;
25101 parser->object_scope = NULL_TREE;
25103 return parser->scope;
25105 else if (!current_scope_valid_p)
25107 parser->scope = NULL_TREE;
25108 parser->qualifying_scope = NULL_TREE;
25109 parser->object_scope = NULL_TREE;
25115 /* Returns TRUE if the upcoming token sequence is the start of a
25116 constructor declarator. If FRIEND_P is true, the declarator is
25117 preceded by the `friend' specifier. */
25120 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25122 bool constructor_p;
25123 bool outside_class_specifier_p;
25124 tree nested_name_specifier;
25125 cp_token *next_token;
25127 /* The common case is that this is not a constructor declarator, so
25128 try to avoid doing lots of work if at all possible. It's not
25129 valid declare a constructor at function scope. */
25130 if (parser->in_function_body)
25132 /* And only certain tokens can begin a constructor declarator. */
25133 next_token = cp_lexer_peek_token (parser->lexer);
25134 if (next_token->type != CPP_NAME
25135 && next_token->type != CPP_SCOPE
25136 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25137 && next_token->type != CPP_TEMPLATE_ID)
25140 /* Parse tentatively; we are going to roll back all of the tokens
25142 cp_parser_parse_tentatively (parser);
25143 /* Assume that we are looking at a constructor declarator. */
25144 constructor_p = true;
25146 /* Look for the optional `::' operator. */
25147 cp_parser_global_scope_opt (parser,
25148 /*current_scope_valid_p=*/false);
25149 /* Look for the nested-name-specifier. */
25150 nested_name_specifier
25151 = (cp_parser_nested_name_specifier_opt (parser,
25152 /*typename_keyword_p=*/false,
25153 /*check_dependency_p=*/false,
25155 /*is_declaration=*/false));
25157 outside_class_specifier_p = (!at_class_scope_p ()
25158 || !TYPE_BEING_DEFINED (current_class_type)
25161 /* Outside of a class-specifier, there must be a
25162 nested-name-specifier. */
25163 if (!nested_name_specifier && outside_class_specifier_p)
25164 constructor_p = false;
25165 else if (nested_name_specifier == error_mark_node)
25166 constructor_p = false;
25168 /* If we have a class scope, this is easy; DR 147 says that S::S always
25169 names the constructor, and no other qualified name could. */
25170 if (constructor_p && nested_name_specifier
25171 && CLASS_TYPE_P (nested_name_specifier))
25173 tree id = cp_parser_unqualified_id (parser,
25174 /*template_keyword_p=*/false,
25175 /*check_dependency_p=*/false,
25176 /*declarator_p=*/true,
25177 /*optional_p=*/false);
25178 if (is_overloaded_fn (id))
25179 id = DECL_NAME (get_first_fn (id));
25180 if (!constructor_name_p (id, nested_name_specifier))
25181 constructor_p = false;
25183 /* If we still think that this might be a constructor-declarator,
25184 look for a class-name. */
25185 else if (constructor_p)
25189 template <typename T> struct S {
25193 we must recognize that the nested `S' names a class. */
25195 type_decl = cp_parser_class_name (parser,
25196 /*typename_keyword_p=*/false,
25197 /*template_keyword_p=*/false,
25199 /*check_dependency_p=*/false,
25200 /*class_head_p=*/false,
25201 /*is_declaration=*/false);
25202 /* If there was no class-name, then this is not a constructor.
25203 Otherwise, if we are in a class-specifier and we aren't
25204 handling a friend declaration, check that its type matches
25205 current_class_type (c++/38313). Note: error_mark_node
25206 is left alone for error recovery purposes. */
25207 constructor_p = (!cp_parser_error_occurred (parser)
25208 && (outside_class_specifier_p
25209 || type_decl == error_mark_node
25210 || same_type_p (current_class_type,
25211 TREE_TYPE (type_decl))));
25213 /* If we're still considering a constructor, we have to see a `(',
25214 to begin the parameter-declaration-clause, followed by either a
25215 `)', an `...', or a decl-specifier. We need to check for a
25216 type-specifier to avoid being fooled into thinking that:
25220 is a constructor. (It is actually a function named `f' that
25221 takes one parameter (of type `int') and returns a value of type
25224 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25225 constructor_p = false;
25228 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25229 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25230 /* A parameter declaration begins with a decl-specifier,
25231 which is either the "attribute" keyword, a storage class
25232 specifier, or (usually) a type-specifier. */
25233 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25236 tree pushed_scope = NULL_TREE;
25237 unsigned saved_num_template_parameter_lists;
25239 /* Names appearing in the type-specifier should be looked up
25240 in the scope of the class. */
25241 if (current_class_type)
25245 type = TREE_TYPE (type_decl);
25246 if (TREE_CODE (type) == TYPENAME_TYPE)
25248 type = resolve_typename_type (type,
25249 /*only_current_p=*/false);
25250 if (TREE_CODE (type) == TYPENAME_TYPE)
25252 cp_parser_abort_tentative_parse (parser);
25256 pushed_scope = push_scope (type);
25259 /* Inside the constructor parameter list, surrounding
25260 template-parameter-lists do not apply. */
25261 saved_num_template_parameter_lists
25262 = parser->num_template_parameter_lists;
25263 parser->num_template_parameter_lists = 0;
25265 /* Look for the type-specifier. */
25266 cp_parser_type_specifier (parser,
25267 CP_PARSER_FLAGS_NONE,
25268 /*decl_specs=*/NULL,
25269 /*is_declarator=*/true,
25270 /*declares_class_or_enum=*/NULL,
25271 /*is_cv_qualifier=*/NULL);
25273 parser->num_template_parameter_lists
25274 = saved_num_template_parameter_lists;
25276 /* Leave the scope of the class. */
25278 pop_scope (pushed_scope);
25280 constructor_p = !cp_parser_error_occurred (parser);
25284 /* We did not really want to consume any tokens. */
25285 cp_parser_abort_tentative_parse (parser);
25287 return constructor_p;
25290 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25291 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25292 they must be performed once we are in the scope of the function.
25294 Returns the function defined. */
25297 cp_parser_function_definition_from_specifiers_and_declarator
25298 (cp_parser* parser,
25299 cp_decl_specifier_seq *decl_specifiers,
25301 const cp_declarator *declarator)
25306 /* Begin the function-definition. */
25307 success_p = start_function (decl_specifiers, declarator, attributes);
25309 /* The things we're about to see are not directly qualified by any
25310 template headers we've seen thus far. */
25311 reset_specialization ();
25313 /* If there were names looked up in the decl-specifier-seq that we
25314 did not check, check them now. We must wait until we are in the
25315 scope of the function to perform the checks, since the function
25316 might be a friend. */
25317 perform_deferred_access_checks (tf_warning_or_error);
25321 cp_finalize_omp_declare_simd (parser, current_function_decl);
25322 parser->omp_declare_simd = NULL;
25323 cp_finalize_oacc_routine (parser, current_function_decl, true);
25324 parser->oacc_routine = NULL;
25329 /* Skip the entire function. */
25330 cp_parser_skip_to_end_of_block_or_statement (parser);
25331 fn = error_mark_node;
25333 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25335 /* Seen already, skip it. An error message has already been output. */
25336 cp_parser_skip_to_end_of_block_or_statement (parser);
25337 fn = current_function_decl;
25338 current_function_decl = NULL_TREE;
25339 /* If this is a function from a class, pop the nested class. */
25340 if (current_class_name)
25341 pop_nested_class ();
25346 if (DECL_DECLARED_INLINE_P (current_function_decl))
25347 tv = TV_PARSE_INLINE;
25349 tv = TV_PARSE_FUNC;
25351 fn = cp_parser_function_definition_after_declarator (parser,
25352 /*inline_p=*/false);
25359 /* Parse the part of a function-definition that follows the
25360 declarator. INLINE_P is TRUE iff this function is an inline
25361 function defined within a class-specifier.
25363 Returns the function defined. */
25366 cp_parser_function_definition_after_declarator (cp_parser* parser,
25370 bool ctor_initializer_p = false;
25371 bool saved_in_unbraced_linkage_specification_p;
25372 bool saved_in_function_body;
25373 unsigned saved_num_template_parameter_lists;
25375 bool fully_implicit_function_template_p
25376 = parser->fully_implicit_function_template_p;
25377 parser->fully_implicit_function_template_p = false;
25378 tree implicit_template_parms
25379 = parser->implicit_template_parms;
25380 parser->implicit_template_parms = 0;
25381 cp_binding_level* implicit_template_scope
25382 = parser->implicit_template_scope;
25383 parser->implicit_template_scope = 0;
25385 saved_in_function_body = parser->in_function_body;
25386 parser->in_function_body = true;
25387 /* If the next token is `return', then the code may be trying to
25388 make use of the "named return value" extension that G++ used to
25390 token = cp_lexer_peek_token (parser->lexer);
25391 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25393 /* Consume the `return' keyword. */
25394 cp_lexer_consume_token (parser->lexer);
25395 /* Look for the identifier that indicates what value is to be
25397 cp_parser_identifier (parser);
25398 /* Issue an error message. */
25399 error_at (token->location,
25400 "named return values are no longer supported");
25401 /* Skip tokens until we reach the start of the function body. */
25404 cp_token *token = cp_lexer_peek_token (parser->lexer);
25405 if (token->type == CPP_OPEN_BRACE
25406 || token->type == CPP_EOF
25407 || token->type == CPP_PRAGMA_EOL)
25409 cp_lexer_consume_token (parser->lexer);
25412 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25413 anything declared inside `f'. */
25414 saved_in_unbraced_linkage_specification_p
25415 = parser->in_unbraced_linkage_specification_p;
25416 parser->in_unbraced_linkage_specification_p = false;
25417 /* Inside the function, surrounding template-parameter-lists do not
25419 saved_num_template_parameter_lists
25420 = parser->num_template_parameter_lists;
25421 parser->num_template_parameter_lists = 0;
25423 start_lambda_scope (current_function_decl);
25425 /* If the next token is `try', `__transaction_atomic', or
25426 `__transaction_relaxed`, then we are looking at either function-try-block
25427 or function-transaction-block. Note that all of these include the
25429 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25430 ctor_initializer_p = cp_parser_function_transaction (parser,
25431 RID_TRANSACTION_ATOMIC);
25432 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25433 RID_TRANSACTION_RELAXED))
25434 ctor_initializer_p = cp_parser_function_transaction (parser,
25435 RID_TRANSACTION_RELAXED);
25436 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25437 ctor_initializer_p = cp_parser_function_try_block (parser);
25439 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25440 (parser, /*in_function_try_block=*/false);
25442 finish_lambda_scope ();
25444 /* Finish the function. */
25445 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25446 (inline_p ? 2 : 0));
25447 /* Generate code for it, if necessary. */
25448 expand_or_defer_fn (fn);
25449 /* Restore the saved values. */
25450 parser->in_unbraced_linkage_specification_p
25451 = saved_in_unbraced_linkage_specification_p;
25452 parser->num_template_parameter_lists
25453 = saved_num_template_parameter_lists;
25454 parser->in_function_body = saved_in_function_body;
25456 parser->fully_implicit_function_template_p
25457 = fully_implicit_function_template_p;
25458 parser->implicit_template_parms
25459 = implicit_template_parms;
25460 parser->implicit_template_scope
25461 = implicit_template_scope;
25463 if (parser->fully_implicit_function_template_p)
25464 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25469 /* Parse a template-declaration body (following argument list). */
25472 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25473 tree parameter_list,
25476 tree decl = NULL_TREE;
25477 bool friend_p = false;
25479 /* We just processed one more parameter list. */
25480 ++parser->num_template_parameter_lists;
25482 /* Get the deferred access checks from the parameter list. These
25483 will be checked once we know what is being declared, as for a
25484 member template the checks must be performed in the scope of the
25485 class containing the member. */
25486 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25488 /* Tentatively parse for a new template parameter list, which can either be
25489 the template keyword or a template introduction. */
25490 if (cp_parser_template_declaration_after_export (parser, member_p))
25492 else if (cxx_dialect >= cxx11
25493 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25494 decl = cp_parser_alias_declaration (parser);
25497 /* There are no access checks when parsing a template, as we do not
25498 know if a specialization will be a friend. */
25499 push_deferring_access_checks (dk_no_check);
25500 cp_token *token = cp_lexer_peek_token (parser->lexer);
25501 decl = cp_parser_single_declaration (parser,
25504 /*explicit_specialization_p=*/false,
25506 pop_deferring_access_checks ();
25508 /* If this is a member template declaration, let the front
25510 if (member_p && !friend_p && decl)
25512 if (TREE_CODE (decl) == TYPE_DECL)
25513 cp_parser_check_access_in_redeclaration (decl, token->location);
25515 decl = finish_member_template_decl (decl);
25517 else if (friend_p && decl
25518 && DECL_DECLARES_TYPE_P (decl))
25519 make_friend_class (current_class_type, TREE_TYPE (decl),
25520 /*complain=*/true);
25522 /* We are done with the current parameter list. */
25523 --parser->num_template_parameter_lists;
25525 pop_deferring_access_checks ();
25528 finish_template_decl (parameter_list);
25530 /* Check the template arguments for a literal operator template. */
25532 && DECL_DECLARES_FUNCTION_P (decl)
25533 && UDLIT_OPER_P (DECL_NAME (decl)))
25536 if (parameter_list == NULL_TREE)
25540 int num_parms = TREE_VEC_LENGTH (parameter_list);
25541 if (num_parms == 1)
25543 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25544 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25545 if (TREE_TYPE (parm) != char_type_node
25546 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25549 else if (num_parms == 2 && cxx_dialect >= cxx14)
25551 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25552 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25553 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25554 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25555 if (parm == error_mark_node
25556 || TREE_TYPE (parm) != TREE_TYPE (type)
25557 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25565 if (cxx_dialect >= cxx14)
25566 error ("literal operator template %qD has invalid parameter list."
25567 " Expected non-type template argument pack <char...>"
25568 " or <typename CharT, CharT...>",
25571 error ("literal operator template %qD has invalid parameter list."
25572 " Expected non-type template argument pack <char...>",
25577 /* Register member declarations. */
25578 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25579 finish_member_declaration (decl);
25580 /* If DECL is a function template, we must return to parse it later.
25581 (Even though there is no definition, there might be default
25582 arguments that need handling.) */
25583 if (member_p && decl
25584 && DECL_DECLARES_FUNCTION_P (decl))
25585 vec_safe_push (unparsed_funs_with_definitions, decl);
25588 /* Parse a template introduction header for a template-declaration. Returns
25589 false if tentative parse fails. */
25592 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25594 cp_parser_parse_tentatively (parser);
25596 tree saved_scope = parser->scope;
25597 tree saved_object_scope = parser->object_scope;
25598 tree saved_qualifying_scope = parser->qualifying_scope;
25600 /* Look for the optional `::' operator. */
25601 cp_parser_global_scope_opt (parser,
25602 /*current_scope_valid_p=*/false);
25603 /* Look for the nested-name-specifier. */
25604 cp_parser_nested_name_specifier_opt (parser,
25605 /*typename_keyword_p=*/false,
25606 /*check_dependency_p=*/true,
25608 /*is_declaration=*/false);
25610 cp_token *token = cp_lexer_peek_token (parser->lexer);
25611 tree concept_name = cp_parser_identifier (parser);
25613 /* Look up the concept for which we will be matching
25614 template parameters. */
25615 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25617 parser->scope = saved_scope;
25618 parser->object_scope = saved_object_scope;
25619 parser->qualifying_scope = saved_qualifying_scope;
25621 if (concept_name == error_mark_node)
25622 cp_parser_simulate_error (parser);
25624 /* Look for opening brace for introduction. */
25625 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25627 if (!cp_parser_parse_definitely (parser))
25630 push_deferring_access_checks (dk_deferred);
25632 /* Build vector of placeholder parameters and grab
25633 matching identifiers. */
25634 tree introduction_list = cp_parser_introduction_list (parser);
25636 /* The introduction-list shall not be empty. */
25637 int nargs = TREE_VEC_LENGTH (introduction_list);
25640 error ("empty introduction-list");
25644 /* Look for closing brace for introduction. */
25645 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25648 if (tmpl_decl == error_mark_node)
25650 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25655 /* Build and associate the constraint. */
25656 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25657 if (parms && parms != error_mark_node)
25659 cp_parser_template_declaration_after_parameters (parser, parms,
25664 error_at (token->location, "no matching concept for template-introduction");
25668 /* Parse a normal template-declaration following the template keyword. */
25671 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25673 tree parameter_list;
25674 bool need_lang_pop;
25675 location_t location = input_location;
25677 /* Look for the `<' token. */
25678 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25680 if (at_class_scope_p () && current_function_decl)
25682 /* 14.5.2.2 [temp.mem]
25684 A local class shall not have member templates. */
25685 error_at (location,
25686 "invalid declaration of member template in local class");
25687 cp_parser_skip_to_end_of_block_or_statement (parser);
25692 A template ... shall not have C linkage. */
25693 if (current_lang_name == lang_name_c)
25695 error_at (location, "template with C linkage");
25696 /* Give it C++ linkage to avoid confusing other parts of the
25698 push_lang_context (lang_name_cplusplus);
25699 need_lang_pop = true;
25702 need_lang_pop = false;
25704 /* We cannot perform access checks on the template parameter
25705 declarations until we know what is being declared, just as we
25706 cannot check the decl-specifier list. */
25707 push_deferring_access_checks (dk_deferred);
25709 /* If the next token is `>', then we have an invalid
25710 specialization. Rather than complain about an invalid template
25711 parameter, issue an error message here. */
25712 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25714 cp_parser_error (parser, "invalid explicit specialization");
25715 begin_specialization ();
25716 parameter_list = NULL_TREE;
25720 /* Parse the template parameters. */
25721 parameter_list = cp_parser_template_parameter_list (parser);
25724 /* Look for the `>'. */
25725 cp_parser_skip_to_end_of_template_parameter_list (parser);
25727 /* Manage template requirements */
25730 tree reqs = get_shorthand_constraints (current_template_parms);
25731 if (tree r = cp_parser_requires_clause_opt (parser))
25732 reqs = conjoin_constraints (reqs, normalize_expression (r));
25733 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25736 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25739 /* For the erroneous case of a template with C linkage, we pushed an
25740 implicit C++ linkage scope; exit that scope now. */
25742 pop_lang_context ();
25745 /* Parse a template-declaration, assuming that the `export' (and
25746 `extern') keywords, if present, has already been scanned. MEMBER_P
25747 is as for cp_parser_template_declaration. */
25750 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25752 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25754 cp_lexer_consume_token (parser->lexer);
25755 cp_parser_explicit_template_declaration (parser, member_p);
25758 else if (flag_concepts)
25759 return cp_parser_template_introduction (parser, member_p);
25764 /* Perform the deferred access checks from a template-parameter-list.
25765 CHECKS is a TREE_LIST of access checks, as returned by
25766 get_deferred_access_checks. */
25769 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25771 ++processing_template_parmlist;
25772 perform_access_checks (checks, tf_warning_or_error);
25773 --processing_template_parmlist;
25776 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25777 `function-definition' sequence that follows a template header.
25778 If MEMBER_P is true, this declaration appears in a class scope.
25780 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25781 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25784 cp_parser_single_declaration (cp_parser* parser,
25785 vec<deferred_access_check, va_gc> *checks,
25787 bool explicit_specialization_p,
25790 int declares_class_or_enum;
25791 tree decl = NULL_TREE;
25792 cp_decl_specifier_seq decl_specifiers;
25793 bool function_definition_p = false;
25794 cp_token *decl_spec_token_start;
25796 /* This function is only used when processing a template
25798 gcc_assert (innermost_scope_kind () == sk_template_parms
25799 || innermost_scope_kind () == sk_template_spec);
25801 /* Defer access checks until we know what is being declared. */
25802 push_deferring_access_checks (dk_deferred);
25804 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25806 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25807 cp_parser_decl_specifier_seq (parser,
25808 CP_PARSER_FLAGS_OPTIONAL,
25810 &declares_class_or_enum);
25812 *friend_p = cp_parser_friend_p (&decl_specifiers);
25814 /* There are no template typedefs. */
25815 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25817 error_at (decl_spec_token_start->location,
25818 "template declaration of %<typedef%>");
25819 decl = error_mark_node;
25822 /* Gather up the access checks that occurred the
25823 decl-specifier-seq. */
25824 stop_deferring_access_checks ();
25826 /* Check for the declaration of a template class. */
25827 if (declares_class_or_enum)
25829 if (cp_parser_declares_only_class_p (parser)
25830 || (declares_class_or_enum & 2))
25832 // If this is a declaration, but not a definition, associate
25833 // any constraints with the type declaration. Constraints
25834 // are associated with definitions in cp_parser_class_specifier.
25835 if (declares_class_or_enum == 1)
25836 associate_classtype_constraints (decl_specifiers.type);
25838 decl = shadow_tag (&decl_specifiers);
25843 friend template <typename T> struct A<T>::B;
25846 A<T>::B will be represented by a TYPENAME_TYPE, and
25847 therefore not recognized by shadow_tag. */
25848 if (friend_p && *friend_p
25850 && decl_specifiers.type
25851 && TYPE_P (decl_specifiers.type))
25852 decl = decl_specifiers.type;
25854 if (decl && decl != error_mark_node)
25855 decl = TYPE_NAME (decl);
25857 decl = error_mark_node;
25859 /* Perform access checks for template parameters. */
25860 cp_parser_perform_template_parameter_access_checks (checks);
25862 /* Give a helpful diagnostic for
25863 template <class T> struct A { } a;
25864 if we aren't already recovering from an error. */
25865 if (!cp_parser_declares_only_class_p (parser)
25868 error_at (cp_lexer_peek_token (parser->lexer)->location,
25869 "a class template declaration must not declare "
25871 cp_parser_skip_to_end_of_block_or_statement (parser);
25877 /* Complain about missing 'typename' or other invalid type names. */
25878 if (!decl_specifiers.any_type_specifiers_p
25879 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25881 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25882 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25883 the rest of this declaration. */
25884 decl = error_mark_node;
25888 /* If it's not a template class, try for a template function. If
25889 the next token is a `;', then this declaration does not declare
25890 anything. But, if there were errors in the decl-specifiers, then
25891 the error might well have come from an attempted class-specifier.
25892 In that case, there's no need to warn about a missing declarator. */
25894 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25895 || decl_specifiers.type != error_mark_node))
25897 decl = cp_parser_init_declarator (parser,
25900 /*function_definition_allowed_p=*/true,
25902 declares_class_or_enum,
25903 &function_definition_p,
25906 /* 7.1.1-1 [dcl.stc]
25908 A storage-class-specifier shall not be specified in an explicit
25909 specialization... */
25911 && explicit_specialization_p
25912 && decl_specifiers.storage_class != sc_none)
25914 error_at (decl_spec_token_start->location,
25915 "explicit template specialization cannot have a storage class");
25916 decl = error_mark_node;
25919 if (decl && VAR_P (decl))
25920 check_template_variable (decl);
25923 /* Look for a trailing `;' after the declaration. */
25924 if (!function_definition_p
25925 && (decl == error_mark_node
25926 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25927 cp_parser_skip_to_end_of_block_or_statement (parser);
25930 pop_deferring_access_checks ();
25932 /* Clear any current qualification; whatever comes next is the start
25933 of something new. */
25934 parser->scope = NULL_TREE;
25935 parser->qualifying_scope = NULL_TREE;
25936 parser->object_scope = NULL_TREE;
25941 /* Parse a cast-expression that is not the operand of a unary "&". */
25944 cp_parser_simple_cast_expression (cp_parser *parser)
25946 return cp_parser_cast_expression (parser, /*address_p=*/false,
25947 /*cast_p=*/false, /*decltype*/false, NULL);
25950 /* Parse a functional cast to TYPE. Returns an expression
25951 representing the cast. */
25954 cp_parser_functional_cast (cp_parser* parser, tree type)
25956 vec<tree, va_gc> *vec;
25957 tree expression_list;
25961 location_t start_loc = input_location;
25964 type = error_mark_node;
25966 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25968 cp_lexer_set_source_position (parser->lexer);
25969 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25970 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25971 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25972 if (TREE_CODE (type) == TYPE_DECL)
25973 type = TREE_TYPE (type);
25975 cast = finish_compound_literal (type, expression_list,
25976 tf_warning_or_error);
25977 /* Create a location of the form:
25980 with caret == start at the start of the type name,
25981 finishing at the closing brace. */
25982 location_t finish_loc
25983 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25984 location_t combined_loc = make_location (start_loc, start_loc,
25986 cast.set_location (combined_loc);
25991 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25993 /*allow_expansion_p=*/true,
25994 /*non_constant_p=*/NULL);
25996 expression_list = error_mark_node;
25999 expression_list = build_tree_list_vec (vec);
26000 release_tree_vector (vec);
26003 cast = build_functional_cast (type, expression_list,
26004 tf_warning_or_error);
26005 /* [expr.const]/1: In an integral constant expression "only type
26006 conversions to integral or enumeration type can be used". */
26007 if (TREE_CODE (type) == TYPE_DECL)
26008 type = TREE_TYPE (type);
26009 if (cast != error_mark_node
26010 && !cast_valid_in_integral_constant_expression_p (type)
26011 && cp_parser_non_integral_constant_expression (parser,
26013 return error_mark_node;
26015 /* Create a location of the form:
26018 with caret == start at the start of the type name,
26019 finishing at the closing paren. */
26020 location_t finish_loc
26021 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26022 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26023 cast.set_location (combined_loc);
26027 /* Save the tokens that make up the body of a member function defined
26028 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26029 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26030 specifiers applied to the declaration. Returns the FUNCTION_DECL
26031 for the member function. */
26034 cp_parser_save_member_function_body (cp_parser* parser,
26035 cp_decl_specifier_seq *decl_specifiers,
26036 cp_declarator *declarator,
26042 bool function_try_block = false;
26044 /* Create the FUNCTION_DECL. */
26045 fn = grokmethod (decl_specifiers, declarator, attributes);
26046 cp_finalize_omp_declare_simd (parser, fn);
26047 cp_finalize_oacc_routine (parser, fn, true);
26048 /* If something went badly wrong, bail out now. */
26049 if (fn == error_mark_node)
26051 /* If there's a function-body, skip it. */
26052 if (cp_parser_token_starts_function_definition_p
26053 (cp_lexer_peek_token (parser->lexer)))
26054 cp_parser_skip_to_end_of_block_or_statement (parser);
26055 return error_mark_node;
26058 /* Remember it, if there default args to post process. */
26059 cp_parser_save_default_args (parser, fn);
26061 /* Save away the tokens that make up the body of the
26063 first = parser->lexer->next_token;
26065 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26066 cp_lexer_consume_token (parser->lexer);
26067 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26068 RID_TRANSACTION_ATOMIC))
26070 cp_lexer_consume_token (parser->lexer);
26071 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26072 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26073 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26074 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26075 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26076 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26077 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26079 cp_lexer_consume_token (parser->lexer);
26080 cp_lexer_consume_token (parser->lexer);
26081 cp_lexer_consume_token (parser->lexer);
26082 cp_lexer_consume_token (parser->lexer);
26083 cp_lexer_consume_token (parser->lexer);
26086 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26087 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26089 cp_lexer_consume_token (parser->lexer);
26090 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26095 /* Handle function try blocks. */
26096 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26098 cp_lexer_consume_token (parser->lexer);
26099 function_try_block = true;
26101 /* We can have braced-init-list mem-initializers before the fn body. */
26102 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26104 cp_lexer_consume_token (parser->lexer);
26105 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26107 /* cache_group will stop after an un-nested { } pair, too. */
26108 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26111 /* variadic mem-inits have ... after the ')'. */
26112 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26113 cp_lexer_consume_token (parser->lexer);
26116 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26117 /* Handle function try blocks. */
26118 if (function_try_block)
26119 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26120 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26121 last = parser->lexer->next_token;
26123 /* Save away the inline definition; we will process it when the
26124 class is complete. */
26125 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26126 DECL_PENDING_INLINE_P (fn) = 1;
26128 /* We need to know that this was defined in the class, so that
26129 friend templates are handled correctly. */
26130 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26132 /* Add FN to the queue of functions to be parsed later. */
26133 vec_safe_push (unparsed_funs_with_definitions, fn);
26138 /* Save the tokens that make up the in-class initializer for a non-static
26139 data member. Returns a DEFAULT_ARG. */
26142 cp_parser_save_nsdmi (cp_parser* parser)
26144 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26147 /* Parse a template-argument-list, as well as the trailing ">" (but
26148 not the opening "<"). See cp_parser_template_argument_list for the
26152 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26156 tree saved_qualifying_scope;
26157 tree saved_object_scope;
26158 bool saved_greater_than_is_operator_p;
26159 int saved_unevaluated_operand;
26160 int saved_inhibit_evaluation_warnings;
26164 When parsing a template-id, the first non-nested `>' is taken as
26165 the end of the template-argument-list rather than a greater-than
26167 saved_greater_than_is_operator_p
26168 = parser->greater_than_is_operator_p;
26169 parser->greater_than_is_operator_p = false;
26170 /* Parsing the argument list may modify SCOPE, so we save it
26172 saved_scope = parser->scope;
26173 saved_qualifying_scope = parser->qualifying_scope;
26174 saved_object_scope = parser->object_scope;
26175 /* We need to evaluate the template arguments, even though this
26176 template-id may be nested within a "sizeof". */
26177 saved_unevaluated_operand = cp_unevaluated_operand;
26178 cp_unevaluated_operand = 0;
26179 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26180 c_inhibit_evaluation_warnings = 0;
26181 /* Parse the template-argument-list itself. */
26182 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26183 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26184 arguments = NULL_TREE;
26186 arguments = cp_parser_template_argument_list (parser);
26187 /* Look for the `>' that ends the template-argument-list. If we find
26188 a '>>' instead, it's probably just a typo. */
26189 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26191 if (cxx_dialect != cxx98)
26193 /* In C++0x, a `>>' in a template argument list or cast
26194 expression is considered to be two separate `>'
26195 tokens. So, change the current token to a `>', but don't
26196 consume it: it will be consumed later when the outer
26197 template argument list (or cast expression) is parsed.
26198 Note that this replacement of `>' for `>>' is necessary
26199 even if we are parsing tentatively: in the tentative
26200 case, after calling
26201 cp_parser_enclosed_template_argument_list we will always
26202 throw away all of the template arguments and the first
26203 closing `>', either because the template argument list
26204 was erroneous or because we are replacing those tokens
26205 with a CPP_TEMPLATE_ID token. The second `>' (which will
26206 not have been thrown away) is needed either to close an
26207 outer template argument list or to complete a new-style
26209 cp_token *token = cp_lexer_peek_token (parser->lexer);
26210 token->type = CPP_GREATER;
26212 else if (!saved_greater_than_is_operator_p)
26214 /* If we're in a nested template argument list, the '>>' has
26215 to be a typo for '> >'. We emit the error message, but we
26216 continue parsing and we push a '>' as next token, so that
26217 the argument list will be parsed correctly. Note that the
26218 global source location is still on the token before the
26219 '>>', so we need to say explicitly where we want it. */
26220 cp_token *token = cp_lexer_peek_token (parser->lexer);
26221 error_at (token->location, "%<>>%> should be %<> >%> "
26222 "within a nested template argument list");
26224 token->type = CPP_GREATER;
26228 /* If this is not a nested template argument list, the '>>'
26229 is a typo for '>'. Emit an error message and continue.
26230 Same deal about the token location, but here we can get it
26231 right by consuming the '>>' before issuing the diagnostic. */
26232 cp_token *token = cp_lexer_consume_token (parser->lexer);
26233 error_at (token->location,
26234 "spurious %<>>%>, use %<>%> to terminate "
26235 "a template argument list");
26239 cp_parser_skip_to_end_of_template_parameter_list (parser);
26240 /* The `>' token might be a greater-than operator again now. */
26241 parser->greater_than_is_operator_p
26242 = saved_greater_than_is_operator_p;
26243 /* Restore the SAVED_SCOPE. */
26244 parser->scope = saved_scope;
26245 parser->qualifying_scope = saved_qualifying_scope;
26246 parser->object_scope = saved_object_scope;
26247 cp_unevaluated_operand = saved_unevaluated_operand;
26248 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26253 /* MEMBER_FUNCTION is a member function, or a friend. If default
26254 arguments, or the body of the function have not yet been parsed,
26258 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26260 timevar_push (TV_PARSE_INMETH);
26261 /* If this member is a template, get the underlying
26263 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26264 member_function = DECL_TEMPLATE_RESULT (member_function);
26266 /* There should not be any class definitions in progress at this
26267 point; the bodies of members are only parsed outside of all class
26269 gcc_assert (parser->num_classes_being_defined == 0);
26270 /* While we're parsing the member functions we might encounter more
26271 classes. We want to handle them right away, but we don't want
26272 them getting mixed up with functions that are currently in the
26274 push_unparsed_function_queues (parser);
26276 /* Make sure that any template parameters are in scope. */
26277 maybe_begin_member_template_processing (member_function);
26279 /* If the body of the function has not yet been parsed, parse it
26281 if (DECL_PENDING_INLINE_P (member_function))
26283 tree function_scope;
26284 cp_token_cache *tokens;
26286 /* The function is no longer pending; we are processing it. */
26287 tokens = DECL_PENDING_INLINE_INFO (member_function);
26288 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26289 DECL_PENDING_INLINE_P (member_function) = 0;
26291 /* If this is a local class, enter the scope of the containing
26293 function_scope = current_function_decl;
26294 if (function_scope)
26295 push_function_context ();
26297 /* Push the body of the function onto the lexer stack. */
26298 cp_parser_push_lexer_for_tokens (parser, tokens);
26300 /* Let the front end know that we going to be defining this
26302 start_preparsed_function (member_function, NULL_TREE,
26303 SF_PRE_PARSED | SF_INCLASS_INLINE);
26305 /* Don't do access checking if it is a templated function. */
26306 if (processing_template_decl)
26307 push_deferring_access_checks (dk_no_check);
26309 /* #pragma omp declare reduction needs special parsing. */
26310 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26312 parser->lexer->in_pragma = true;
26313 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26314 finish_function (/*inline*/2);
26315 cp_check_omp_declare_reduction (member_function);
26318 /* Now, parse the body of the function. */
26319 cp_parser_function_definition_after_declarator (parser,
26320 /*inline_p=*/true);
26322 if (processing_template_decl)
26323 pop_deferring_access_checks ();
26325 /* Leave the scope of the containing function. */
26326 if (function_scope)
26327 pop_function_context ();
26328 cp_parser_pop_lexer (parser);
26331 /* Remove any template parameters from the symbol table. */
26332 maybe_end_member_template_processing ();
26334 /* Restore the queue. */
26335 pop_unparsed_function_queues (parser);
26336 timevar_pop (TV_PARSE_INMETH);
26339 /* If DECL contains any default args, remember it on the unparsed
26340 functions queue. */
26343 cp_parser_save_default_args (cp_parser* parser, tree decl)
26347 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26349 probe = TREE_CHAIN (probe))
26350 if (TREE_PURPOSE (probe))
26352 cp_default_arg_entry entry = {current_class_type, decl};
26353 vec_safe_push (unparsed_funs_with_default_args, entry);
26358 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26359 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26360 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26361 from the parameter-type-list. */
26364 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26365 tree default_arg, tree parmtype)
26367 cp_token_cache *tokens;
26371 if (default_arg == error_mark_node)
26372 return error_mark_node;
26374 /* Push the saved tokens for the default argument onto the parser's
26376 tokens = DEFARG_TOKENS (default_arg);
26377 cp_parser_push_lexer_for_tokens (parser, tokens);
26379 start_lambda_scope (decl);
26381 /* Parse the default argument. */
26382 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26383 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26384 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26386 finish_lambda_scope ();
26388 if (parsed_arg == error_mark_node)
26389 cp_parser_skip_to_end_of_statement (parser);
26391 if (!processing_template_decl)
26393 /* In a non-template class, check conversions now. In a template,
26394 we'll wait and instantiate these as needed. */
26395 if (TREE_CODE (decl) == PARM_DECL)
26396 parsed_arg = check_default_argument (parmtype, parsed_arg,
26397 tf_warning_or_error);
26399 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26402 /* If the token stream has not been completely used up, then
26403 there was extra junk after the end of the default
26405 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26407 if (TREE_CODE (decl) == PARM_DECL)
26408 cp_parser_error (parser, "expected %<,%>");
26410 cp_parser_error (parser, "expected %<;%>");
26413 /* Revert to the main lexer. */
26414 cp_parser_pop_lexer (parser);
26419 /* FIELD is a non-static data member with an initializer which we saved for
26420 later; parse it now. */
26423 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26427 maybe_begin_member_template_processing (field);
26429 push_unparsed_function_queues (parser);
26430 def = cp_parser_late_parse_one_default_arg (parser, field,
26431 DECL_INITIAL (field),
26433 pop_unparsed_function_queues (parser);
26435 maybe_end_member_template_processing ();
26437 DECL_INITIAL (field) = def;
26440 /* FN is a FUNCTION_DECL which may contains a parameter with an
26441 unparsed DEFAULT_ARG. Parse the default args now. This function
26442 assumes that the current scope is the scope in which the default
26443 argument should be processed. */
26446 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26448 bool saved_local_variables_forbidden_p;
26449 tree parm, parmdecl;
26451 /* While we're parsing the default args, we might (due to the
26452 statement expression extension) encounter more classes. We want
26453 to handle them right away, but we don't want them getting mixed
26454 up with default args that are currently in the queue. */
26455 push_unparsed_function_queues (parser);
26457 /* Local variable names (and the `this' keyword) may not appear
26458 in a default argument. */
26459 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26460 parser->local_variables_forbidden_p = true;
26462 push_defarg_context (fn);
26464 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26465 parmdecl = DECL_ARGUMENTS (fn);
26466 parm && parm != void_list_node;
26467 parm = TREE_CHAIN (parm),
26468 parmdecl = DECL_CHAIN (parmdecl))
26470 tree default_arg = TREE_PURPOSE (parm);
26472 vec<tree, va_gc> *insts;
26479 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26480 /* This can happen for a friend declaration for a function
26481 already declared with default arguments. */
26485 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26487 TREE_VALUE (parm));
26488 if (parsed_arg == error_mark_node)
26493 TREE_PURPOSE (parm) = parsed_arg;
26495 /* Update any instantiations we've already created. */
26496 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26497 vec_safe_iterate (insts, ix, ©); ix++)
26498 TREE_PURPOSE (copy) = parsed_arg;
26501 pop_defarg_context ();
26503 /* Make sure no default arg is missing. */
26504 check_default_args (fn);
26506 /* Restore the state of local_variables_forbidden_p. */
26507 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26509 /* Restore the queue. */
26510 pop_unparsed_function_queues (parser);
26513 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26515 sizeof ... ( identifier )
26517 where the 'sizeof' token has already been consumed. */
26520 cp_parser_sizeof_pack (cp_parser *parser)
26522 /* Consume the `...'. */
26523 cp_lexer_consume_token (parser->lexer);
26524 maybe_warn_variadic_templates ();
26526 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26528 cp_lexer_consume_token (parser->lexer);
26530 permerror (cp_lexer_peek_token (parser->lexer)->location,
26531 "%<sizeof...%> argument must be surrounded by parentheses");
26533 cp_token *token = cp_lexer_peek_token (parser->lexer);
26534 tree name = cp_parser_identifier (parser);
26535 if (name == error_mark_node)
26536 return error_mark_node;
26537 /* The name is not qualified. */
26538 parser->scope = NULL_TREE;
26539 parser->qualifying_scope = NULL_TREE;
26540 parser->object_scope = NULL_TREE;
26541 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26542 if (expr == error_mark_node)
26543 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26545 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26546 expr = TREE_TYPE (expr);
26547 else if (TREE_CODE (expr) == CONST_DECL)
26548 expr = DECL_INITIAL (expr);
26549 expr = make_pack_expansion (expr);
26550 PACK_EXPANSION_SIZEOF_P (expr) = true;
26553 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26558 /* Parse the operand of `sizeof' (or a similar operator). Returns
26559 either a TYPE or an expression, depending on the form of the
26560 input. The KEYWORD indicates which kind of expression we have
26564 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26566 tree expr = NULL_TREE;
26567 const char *saved_message;
26569 bool saved_integral_constant_expression_p;
26570 bool saved_non_integral_constant_expression_p;
26572 /* If it's a `...', then we are computing the length of a parameter
26574 if (keyword == RID_SIZEOF
26575 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26576 return cp_parser_sizeof_pack (parser);
26578 /* Types cannot be defined in a `sizeof' expression. Save away the
26580 saved_message = parser->type_definition_forbidden_message;
26581 /* And create the new one. */
26582 tmp = concat ("types may not be defined in %<",
26583 IDENTIFIER_POINTER (ridpointers[keyword]),
26584 "%> expressions", NULL);
26585 parser->type_definition_forbidden_message = tmp;
26587 /* The restrictions on constant-expressions do not apply inside
26588 sizeof expressions. */
26589 saved_integral_constant_expression_p
26590 = parser->integral_constant_expression_p;
26591 saved_non_integral_constant_expression_p
26592 = parser->non_integral_constant_expression_p;
26593 parser->integral_constant_expression_p = false;
26595 /* Do not actually evaluate the expression. */
26596 ++cp_unevaluated_operand;
26597 ++c_inhibit_evaluation_warnings;
26598 /* If it's a `(', then we might be looking at the type-id
26600 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26602 tree type = NULL_TREE;
26604 /* We can't be sure yet whether we're looking at a type-id or an
26606 cp_parser_parse_tentatively (parser);
26607 /* Note: as a GNU Extension, compound literals are considered
26608 postfix-expressions as they are in C99, so they are valid
26609 arguments to sizeof. See comment in cp_parser_cast_expression
26611 if (cp_parser_compound_literal_p (parser))
26612 cp_parser_simulate_error (parser);
26615 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26616 parser->in_type_id_in_expr_p = true;
26617 /* Look for the type-id. */
26618 type = cp_parser_type_id (parser);
26619 /* Look for the closing `)'. */
26620 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26621 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26624 /* If all went well, then we're done. */
26625 if (cp_parser_parse_definitely (parser))
26627 cp_decl_specifier_seq decl_specs;
26629 /* Build a trivial decl-specifier-seq. */
26630 clear_decl_specs (&decl_specs);
26631 decl_specs.type = type;
26633 /* Call grokdeclarator to figure out what type this is. */
26634 expr = grokdeclarator (NULL,
26638 /*attrlist=*/NULL);
26642 /* If the type-id production did not work out, then we must be
26643 looking at the unary-expression production. */
26645 expr = cp_parser_unary_expression (parser);
26647 /* Go back to evaluating expressions. */
26648 --cp_unevaluated_operand;
26649 --c_inhibit_evaluation_warnings;
26651 /* Free the message we created. */
26653 /* And restore the old one. */
26654 parser->type_definition_forbidden_message = saved_message;
26655 parser->integral_constant_expression_p
26656 = saved_integral_constant_expression_p;
26657 parser->non_integral_constant_expression_p
26658 = saved_non_integral_constant_expression_p;
26663 /* If the current declaration has no declarator, return true. */
26666 cp_parser_declares_only_class_p (cp_parser *parser)
26668 /* If the next token is a `;' or a `,' then there is no
26670 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26671 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26674 /* Update the DECL_SPECS to reflect the storage class indicated by
26678 cp_parser_set_storage_class (cp_parser *parser,
26679 cp_decl_specifier_seq *decl_specs,
26683 cp_storage_class storage_class;
26685 if (parser->in_unbraced_linkage_specification_p)
26687 error_at (token->location, "invalid use of %qD in linkage specification",
26688 ridpointers[keyword]);
26691 else if (decl_specs->storage_class != sc_none)
26693 decl_specs->conflicting_specifiers_p = true;
26697 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26698 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26699 && decl_specs->gnu_thread_keyword_p)
26701 pedwarn (decl_specs->locations[ds_thread], 0,
26702 "%<__thread%> before %qD", ridpointers[keyword]);
26708 storage_class = sc_auto;
26711 storage_class = sc_register;
26714 storage_class = sc_static;
26717 storage_class = sc_extern;
26720 storage_class = sc_mutable;
26723 gcc_unreachable ();
26725 decl_specs->storage_class = storage_class;
26726 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26728 /* A storage class specifier cannot be applied alongside a typedef
26729 specifier. If there is a typedef specifier present then set
26730 conflicting_specifiers_p which will trigger an error later
26731 on in grokdeclarator. */
26732 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26733 decl_specs->conflicting_specifiers_p = true;
26736 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26737 is true, the type is a class or enum definition. */
26740 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26743 bool type_definition_p)
26745 decl_specs->any_specifiers_p = true;
26747 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26748 (with, for example, in "typedef int wchar_t;") we remember that
26749 this is what happened. In system headers, we ignore these
26750 declarations so that G++ can work with system headers that are not
26752 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26753 && !type_definition_p
26754 && (type_spec == boolean_type_node
26755 || type_spec == char16_type_node
26756 || type_spec == char32_type_node
26757 || type_spec == wchar_type_node)
26758 && (decl_specs->type
26759 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26760 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26761 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26762 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26764 decl_specs->redefined_builtin_type = type_spec;
26765 set_and_check_decl_spec_loc (decl_specs,
26766 ds_redefined_builtin_type_spec,
26768 if (!decl_specs->type)
26770 decl_specs->type = type_spec;
26771 decl_specs->type_definition_p = false;
26772 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26775 else if (decl_specs->type)
26776 decl_specs->multiple_types_p = true;
26779 decl_specs->type = type_spec;
26780 decl_specs->type_definition_p = type_definition_p;
26781 decl_specs->redefined_builtin_type = NULL_TREE;
26782 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26786 /* True iff TOKEN is the GNU keyword __thread. */
26789 token_is__thread (cp_token *token)
26791 gcc_assert (token->keyword == RID_THREAD);
26792 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26795 /* Set the location for a declarator specifier and check if it is
26798 DECL_SPECS is the sequence of declarator specifiers onto which to
26801 DS is the single declarator specifier to set which location is to
26802 be set onto the existing sequence of declarators.
26804 LOCATION is the location for the declarator specifier to
26808 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26809 cp_decl_spec ds, cp_token *token)
26811 gcc_assert (ds < ds_last);
26813 if (decl_specs == NULL)
26816 source_location location = token->location;
26818 if (decl_specs->locations[ds] == 0)
26820 decl_specs->locations[ds] = location;
26821 if (ds == ds_thread)
26822 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26828 if (decl_specs->locations[ds_long_long] != 0)
26829 error_at (location,
26830 "%<long long long%> is too long for GCC");
26833 decl_specs->locations[ds_long_long] = location;
26834 pedwarn_cxx98 (location,
26836 "ISO C++ 1998 does not support %<long long%>");
26839 else if (ds == ds_thread)
26841 bool gnu = token_is__thread (token);
26842 if (gnu != decl_specs->gnu_thread_keyword_p)
26843 error_at (location,
26844 "both %<__thread%> and %<thread_local%> specified");
26846 error_at (location, "duplicate %qD", token->u.value);
26850 static const char *const decl_spec_names[] = {
26867 error_at (location,
26868 "duplicate %qs", decl_spec_names[ds]);
26873 /* Return true iff the declarator specifier DS is present in the
26874 sequence of declarator specifiers DECL_SPECS. */
26877 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26880 gcc_assert (ds < ds_last);
26882 if (decl_specs == NULL)
26885 return decl_specs->locations[ds] != 0;
26888 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26889 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26892 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26894 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26897 /* Issue an error message indicating that TOKEN_DESC was expected.
26898 If KEYWORD is true, it indicated this function is called by
26899 cp_parser_require_keword and the required token can only be
26900 a indicated keyword. */
26903 cp_parser_required_error (cp_parser *parser,
26904 required_token token_desc,
26907 switch (token_desc)
26910 cp_parser_error (parser, "expected %<new%>");
26913 cp_parser_error (parser, "expected %<delete%>");
26916 cp_parser_error (parser, "expected %<return%>");
26919 cp_parser_error (parser, "expected %<while%>");
26922 cp_parser_error (parser, "expected %<extern%>");
26924 case RT_STATIC_ASSERT:
26925 cp_parser_error (parser, "expected %<static_assert%>");
26928 cp_parser_error (parser, "expected %<decltype%>");
26931 cp_parser_error (parser, "expected %<operator%>");
26934 cp_parser_error (parser, "expected %<class%>");
26937 cp_parser_error (parser, "expected %<template%>");
26940 cp_parser_error (parser, "expected %<namespace%>");
26943 cp_parser_error (parser, "expected %<using%>");
26946 cp_parser_error (parser, "expected %<asm%>");
26949 cp_parser_error (parser, "expected %<try%>");
26952 cp_parser_error (parser, "expected %<catch%>");
26955 cp_parser_error (parser, "expected %<throw%>");
26958 cp_parser_error (parser, "expected %<__label__%>");
26961 cp_parser_error (parser, "expected %<@try%>");
26963 case RT_AT_SYNCHRONIZED:
26964 cp_parser_error (parser, "expected %<@synchronized%>");
26967 cp_parser_error (parser, "expected %<@throw%>");
26969 case RT_TRANSACTION_ATOMIC:
26970 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26972 case RT_TRANSACTION_RELAXED:
26973 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26980 switch (token_desc)
26983 cp_parser_error (parser, "expected %<;%>");
26985 case RT_OPEN_PAREN:
26986 cp_parser_error (parser, "expected %<(%>");
26988 case RT_CLOSE_BRACE:
26989 cp_parser_error (parser, "expected %<}%>");
26991 case RT_OPEN_BRACE:
26992 cp_parser_error (parser, "expected %<{%>");
26994 case RT_CLOSE_SQUARE:
26995 cp_parser_error (parser, "expected %<]%>");
26997 case RT_OPEN_SQUARE:
26998 cp_parser_error (parser, "expected %<[%>");
27001 cp_parser_error (parser, "expected %<,%>");
27004 cp_parser_error (parser, "expected %<::%>");
27007 cp_parser_error (parser, "expected %<<%>");
27010 cp_parser_error (parser, "expected %<>%>");
27013 cp_parser_error (parser, "expected %<=%>");
27016 cp_parser_error (parser, "expected %<...%>");
27019 cp_parser_error (parser, "expected %<*%>");
27022 cp_parser_error (parser, "expected %<~%>");
27025 cp_parser_error (parser, "expected %<:%>");
27027 case RT_COLON_SCOPE:
27028 cp_parser_error (parser, "expected %<:%> or %<::%>");
27030 case RT_CLOSE_PAREN:
27031 cp_parser_error (parser, "expected %<)%>");
27033 case RT_COMMA_CLOSE_PAREN:
27034 cp_parser_error (parser, "expected %<,%> or %<)%>");
27036 case RT_PRAGMA_EOL:
27037 cp_parser_error (parser, "expected end of line");
27040 cp_parser_error (parser, "expected identifier");
27043 cp_parser_error (parser, "expected selection-statement");
27045 case RT_INTERATION:
27046 cp_parser_error (parser, "expected iteration-statement");
27049 cp_parser_error (parser, "expected jump-statement");
27052 cp_parser_error (parser, "expected class-key");
27054 case RT_CLASS_TYPENAME_TEMPLATE:
27055 cp_parser_error (parser,
27056 "expected %<class%>, %<typename%>, or %<template%>");
27059 gcc_unreachable ();
27063 gcc_unreachable ();
27068 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27069 issue an error message indicating that TOKEN_DESC was expected.
27071 Returns the token consumed, if the token had the appropriate type.
27072 Otherwise, returns NULL. */
27075 cp_parser_require (cp_parser* parser,
27076 enum cpp_ttype type,
27077 required_token token_desc)
27079 if (cp_lexer_next_token_is (parser->lexer, type))
27080 return cp_lexer_consume_token (parser->lexer);
27083 /* Output the MESSAGE -- unless we're parsing tentatively. */
27084 if (!cp_parser_simulate_error (parser))
27085 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27090 /* An error message is produced if the next token is not '>'.
27091 All further tokens are skipped until the desired token is
27092 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27095 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27097 /* Current level of '< ... >'. */
27098 unsigned level = 0;
27099 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27100 unsigned nesting_depth = 0;
27102 /* Are we ready, yet? If not, issue error message. */
27103 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27106 /* Skip tokens until the desired token is found. */
27109 /* Peek at the next token. */
27110 switch (cp_lexer_peek_token (parser->lexer)->type)
27113 if (!nesting_depth)
27118 if (cxx_dialect == cxx98)
27119 /* C++0x views the `>>' operator as two `>' tokens, but
27122 else if (!nesting_depth && level-- == 0)
27124 /* We've hit a `>>' where the first `>' closes the
27125 template argument list, and the second `>' is
27126 spurious. Just consume the `>>' and stop; we've
27127 already produced at least one error. */
27128 cp_lexer_consume_token (parser->lexer);
27131 /* Fall through for C++0x, so we handle the second `>' in
27135 if (!nesting_depth && level-- == 0)
27137 /* We've reached the token we want, consume it and stop. */
27138 cp_lexer_consume_token (parser->lexer);
27143 case CPP_OPEN_PAREN:
27144 case CPP_OPEN_SQUARE:
27148 case CPP_CLOSE_PAREN:
27149 case CPP_CLOSE_SQUARE:
27150 if (nesting_depth-- == 0)
27155 case CPP_PRAGMA_EOL:
27156 case CPP_SEMICOLON:
27157 case CPP_OPEN_BRACE:
27158 case CPP_CLOSE_BRACE:
27159 /* The '>' was probably forgotten, don't look further. */
27166 /* Consume this token. */
27167 cp_lexer_consume_token (parser->lexer);
27171 /* If the next token is the indicated keyword, consume it. Otherwise,
27172 issue an error message indicating that TOKEN_DESC was expected.
27174 Returns the token consumed, if the token had the appropriate type.
27175 Otherwise, returns NULL. */
27178 cp_parser_require_keyword (cp_parser* parser,
27180 required_token token_desc)
27182 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27184 if (token && token->keyword != keyword)
27186 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27193 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27194 function-definition. */
27197 cp_parser_token_starts_function_definition_p (cp_token* token)
27199 return (/* An ordinary function-body begins with an `{'. */
27200 token->type == CPP_OPEN_BRACE
27201 /* A ctor-initializer begins with a `:'. */
27202 || token->type == CPP_COLON
27203 /* A function-try-block begins with `try'. */
27204 || token->keyword == RID_TRY
27205 /* A function-transaction-block begins with `__transaction_atomic'
27206 or `__transaction_relaxed'. */
27207 || token->keyword == RID_TRANSACTION_ATOMIC
27208 || token->keyword == RID_TRANSACTION_RELAXED
27209 /* The named return value extension begins with `return'. */
27210 || token->keyword == RID_RETURN);
27213 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27217 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27221 token = cp_lexer_peek_token (parser->lexer);
27222 return (token->type == CPP_OPEN_BRACE
27223 || (token->type == CPP_COLON
27224 && !parser->colon_doesnt_start_class_def_p));
27227 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27228 C++0x) ending a template-argument. */
27231 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27235 token = cp_lexer_peek_token (parser->lexer);
27236 return (token->type == CPP_COMMA
27237 || token->type == CPP_GREATER
27238 || token->type == CPP_ELLIPSIS
27239 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27242 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27243 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27246 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27251 token = cp_lexer_peek_nth_token (parser->lexer, n);
27252 if (token->type == CPP_LESS)
27254 /* Check for the sequence `<::' in the original code. It would be lexed as
27255 `[:', where `[' is a digraph, and there is no whitespace before
27257 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27260 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27261 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27267 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27268 or none_type otherwise. */
27270 static enum tag_types
27271 cp_parser_token_is_class_key (cp_token* token)
27273 switch (token->keyword)
27278 return record_type;
27287 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27288 or none_type otherwise or if the token is null. */
27290 static enum tag_types
27291 cp_parser_token_is_type_parameter_key (cp_token* token)
27296 switch (token->keyword)
27301 return typename_type;
27308 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27311 cp_parser_check_class_key (enum tag_types class_key, tree type)
27313 if (type == error_mark_node)
27315 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27317 if (permerror (input_location, "%qs tag used in naming %q#T",
27318 class_key == union_type ? "union"
27319 : class_key == record_type ? "struct" : "class",
27321 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27322 "%q#T was previously declared here", type);
27326 /* Issue an error message if DECL is redeclared with different
27327 access than its original declaration [class.access.spec/3].
27328 This applies to nested classes and nested class templates.
27332 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27334 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
27337 if ((TREE_PRIVATE (decl)
27338 != (current_access_specifier == access_private_node))
27339 || (TREE_PROTECTED (decl)
27340 != (current_access_specifier == access_protected_node)))
27341 error_at (location, "%qD redeclared with different access", decl);
27344 /* Look for the `template' keyword, as a syntactic disambiguator.
27345 Return TRUE iff it is present, in which case it will be
27349 cp_parser_optional_template_keyword (cp_parser *parser)
27351 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27353 /* In C++98 the `template' keyword can only be used within templates;
27354 outside templates the parser can always figure out what is a
27355 template and what is not. In C++11, per the resolution of DR 468,
27356 `template' is allowed in cases where it is not strictly necessary. */
27357 if (!processing_template_decl
27358 && pedantic && cxx_dialect == cxx98)
27360 cp_token *token = cp_lexer_peek_token (parser->lexer);
27361 pedwarn (token->location, OPT_Wpedantic,
27362 "in C++98 %<template%> (as a disambiguator) is only "
27363 "allowed within templates");
27364 /* If this part of the token stream is rescanned, the same
27365 error message would be generated. So, we purge the token
27366 from the stream. */
27367 cp_lexer_purge_token (parser->lexer);
27372 /* Consume the `template' keyword. */
27373 cp_lexer_consume_token (parser->lexer);
27380 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27381 set PARSER->SCOPE, and perform other related actions. */
27384 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27386 struct tree_check *check_value;
27388 /* Get the stored value. */
27389 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27390 /* Set the scope from the stored value. */
27391 parser->scope = saved_checks_value (check_value);
27392 parser->qualifying_scope = check_value->qualifying_scope;
27393 parser->object_scope = NULL_TREE;
27396 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27397 encounter the end of a block before what we were looking for. */
27400 cp_parser_cache_group (cp_parser *parser,
27401 enum cpp_ttype end,
27406 cp_token *token = cp_lexer_peek_token (parser->lexer);
27408 /* Abort a parenthesized expression if we encounter a semicolon. */
27409 if ((end == CPP_CLOSE_PAREN || depth == 0)
27410 && token->type == CPP_SEMICOLON)
27412 /* If we've reached the end of the file, stop. */
27413 if (token->type == CPP_EOF
27414 || (end != CPP_PRAGMA_EOL
27415 && token->type == CPP_PRAGMA_EOL))
27417 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27418 /* We've hit the end of an enclosing block, so there's been some
27419 kind of syntax error. */
27422 /* Consume the token. */
27423 cp_lexer_consume_token (parser->lexer);
27424 /* See if it starts a new group. */
27425 if (token->type == CPP_OPEN_BRACE)
27427 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27428 /* In theory this should probably check end == '}', but
27429 cp_parser_save_member_function_body needs it to exit
27430 after either '}' or ')' when called with ')'. */
27434 else if (token->type == CPP_OPEN_PAREN)
27436 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27437 if (depth == 0 && end == CPP_CLOSE_PAREN)
27440 else if (token->type == CPP_PRAGMA)
27441 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27442 else if (token->type == end)
27447 /* Like above, for caching a default argument or NSDMI. Both of these are
27448 terminated by a non-nested comma, but it can be unclear whether or not a
27449 comma is nested in a template argument list unless we do more parsing.
27450 In order to handle this ambiguity, when we encounter a ',' after a '<'
27451 we try to parse what follows as a parameter-declaration-list (in the
27452 case of a default argument) or a member-declarator (in the case of an
27453 NSDMI). If that succeeds, then we stop caching. */
27456 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27458 unsigned depth = 0;
27459 int maybe_template_id = 0;
27460 cp_token *first_token;
27462 tree default_argument;
27464 /* Add tokens until we have processed the entire default
27465 argument. We add the range [first_token, token). */
27466 first_token = cp_lexer_peek_token (parser->lexer);
27467 if (first_token->type == CPP_OPEN_BRACE)
27469 /* For list-initialization, this is straightforward. */
27470 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27471 token = cp_lexer_peek_token (parser->lexer);
27477 /* Peek at the next token. */
27478 token = cp_lexer_peek_token (parser->lexer);
27479 /* What we do depends on what token we have. */
27480 switch (token->type)
27482 /* In valid code, a default argument must be
27483 immediately followed by a `,' `)', or `...'. */
27485 if (depth == 0 && maybe_template_id)
27487 /* If we've seen a '<', we might be in a
27488 template-argument-list. Until Core issue 325 is
27489 resolved, we don't know how this situation ought
27490 to be handled, so try to DTRT. We check whether
27491 what comes after the comma is a valid parameter
27492 declaration list. If it is, then the comma ends
27493 the default argument; otherwise the default
27494 argument continues. */
27495 bool error = false;
27498 /* Set ITALP so cp_parser_parameter_declaration_list
27499 doesn't decide to commit to this parse. */
27500 bool saved_italp = parser->in_template_argument_list_p;
27501 parser->in_template_argument_list_p = true;
27503 cp_parser_parse_tentatively (parser);
27507 /* Parse declarators until we reach a non-comma or
27508 somthing that cannot be an initializer.
27509 Just checking whether we're looking at a single
27510 declarator is insufficient. Consider:
27511 int var = tuple<T,U>::x;
27512 The template parameter 'U' looks exactly like a
27516 int ctor_dtor_or_conv_p;
27517 cp_lexer_consume_token (parser->lexer);
27518 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27519 &ctor_dtor_or_conv_p,
27520 /*parenthesized_p=*/NULL,
27522 /*friend_p=*/false);
27523 peek = cp_lexer_peek_token (parser->lexer);
27524 if (cp_parser_error_occurred (parser))
27527 while (peek->type == CPP_COMMA);
27528 /* If we met an '=' or ';' then the original comma
27529 was the end of the NSDMI. Otherwise assume
27530 we're still in the NSDMI. */
27531 error = (peek->type != CPP_EQ
27532 && peek->type != CPP_SEMICOLON);
27536 cp_lexer_consume_token (parser->lexer);
27537 begin_scope (sk_function_parms, NULL_TREE);
27538 cp_parser_parameter_declaration_list (parser, &error);
27539 pop_bindings_and_leave_scope ();
27541 if (!cp_parser_error_occurred (parser) && !error)
27543 cp_parser_abort_tentative_parse (parser);
27545 parser->in_template_argument_list_p = saved_italp;
27548 case CPP_CLOSE_PAREN:
27550 /* If we run into a non-nested `;', `}', or `]',
27551 then the code is invalid -- but the default
27552 argument is certainly over. */
27553 case CPP_SEMICOLON:
27554 case CPP_CLOSE_BRACE:
27555 case CPP_CLOSE_SQUARE:
27557 /* Handle correctly int n = sizeof ... ( p ); */
27558 && token->type != CPP_ELLIPSIS)
27560 /* Update DEPTH, if necessary. */
27561 else if (token->type == CPP_CLOSE_PAREN
27562 || token->type == CPP_CLOSE_BRACE
27563 || token->type == CPP_CLOSE_SQUARE)
27567 case CPP_OPEN_PAREN:
27568 case CPP_OPEN_SQUARE:
27569 case CPP_OPEN_BRACE:
27575 /* This might be the comparison operator, or it might
27576 start a template argument list. */
27577 ++maybe_template_id;
27581 if (cxx_dialect == cxx98)
27583 /* Fall through for C++0x, which treats the `>>'
27584 operator like two `>' tokens in certain
27590 /* This might be an operator, or it might close a
27591 template argument list. But if a previous '<'
27592 started a template argument list, this will have
27593 closed it, so we can't be in one anymore. */
27594 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27595 if (maybe_template_id < 0)
27596 maybe_template_id = 0;
27600 /* If we run out of tokens, issue an error message. */
27602 case CPP_PRAGMA_EOL:
27603 error_at (token->location, "file ends in default argument");
27604 return error_mark_node;
27608 /* In these cases, we should look for template-ids.
27609 For example, if the default argument is
27610 `X<int, double>()', we need to do name lookup to
27611 figure out whether or not `X' is a template; if
27612 so, the `,' does not end the default argument.
27614 That is not yet done. */
27621 /* If we've reached the end, stop. */
27625 /* Add the token to the token block. */
27626 token = cp_lexer_consume_token (parser->lexer);
27629 /* Create a DEFAULT_ARG to represent the unparsed default
27631 default_argument = make_node (DEFAULT_ARG);
27632 DEFARG_TOKENS (default_argument)
27633 = cp_token_cache_new (first_token, token);
27634 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27636 return default_argument;
27639 /* Begin parsing tentatively. We always save tokens while parsing
27640 tentatively so that if the tentative parsing fails we can restore the
27644 cp_parser_parse_tentatively (cp_parser* parser)
27646 /* Enter a new parsing context. */
27647 parser->context = cp_parser_context_new (parser->context);
27648 /* Begin saving tokens. */
27649 cp_lexer_save_tokens (parser->lexer);
27650 /* In order to avoid repetitive access control error messages,
27651 access checks are queued up until we are no longer parsing
27653 push_deferring_access_checks (dk_deferred);
27656 /* Commit to the currently active tentative parse. */
27659 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27661 cp_parser_context *context;
27664 /* Mark all of the levels as committed. */
27665 lexer = parser->lexer;
27666 for (context = parser->context; context->next; context = context->next)
27668 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27670 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27671 while (!cp_lexer_saving_tokens (lexer))
27672 lexer = lexer->next;
27673 cp_lexer_commit_tokens (lexer);
27677 /* Commit to the topmost currently active tentative parse.
27679 Note that this function shouldn't be called when there are
27680 irreversible side-effects while in a tentative state. For
27681 example, we shouldn't create a permanent entry in the symbol
27682 table, or issue an error message that might not apply if the
27683 tentative parse is aborted. */
27686 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27688 cp_parser_context *context = parser->context;
27689 cp_lexer *lexer = parser->lexer;
27693 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27695 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27697 while (!cp_lexer_saving_tokens (lexer))
27698 lexer = lexer->next;
27699 cp_lexer_commit_tokens (lexer);
27703 /* Abort the currently active tentative parse. All consumed tokens
27704 will be rolled back, and no diagnostics will be issued. */
27707 cp_parser_abort_tentative_parse (cp_parser* parser)
27709 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27710 || errorcount > 0);
27711 cp_parser_simulate_error (parser);
27712 /* Now, pretend that we want to see if the construct was
27713 successfully parsed. */
27714 cp_parser_parse_definitely (parser);
27717 /* Stop parsing tentatively. If a parse error has occurred, restore the
27718 token stream. Otherwise, commit to the tokens we have consumed.
27719 Returns true if no error occurred; false otherwise. */
27722 cp_parser_parse_definitely (cp_parser* parser)
27724 bool error_occurred;
27725 cp_parser_context *context;
27727 /* Remember whether or not an error occurred, since we are about to
27728 destroy that information. */
27729 error_occurred = cp_parser_error_occurred (parser);
27730 /* Remove the topmost context from the stack. */
27731 context = parser->context;
27732 parser->context = context->next;
27733 /* If no parse errors occurred, commit to the tentative parse. */
27734 if (!error_occurred)
27736 /* Commit to the tokens read tentatively, unless that was
27738 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27739 cp_lexer_commit_tokens (parser->lexer);
27741 pop_to_parent_deferring_access_checks ();
27743 /* Otherwise, if errors occurred, roll back our state so that things
27744 are just as they were before we began the tentative parse. */
27747 cp_lexer_rollback_tokens (parser->lexer);
27748 pop_deferring_access_checks ();
27750 /* Add the context to the front of the free list. */
27751 context->next = cp_parser_context_free_list;
27752 cp_parser_context_free_list = context;
27754 return !error_occurred;
27757 /* Returns true if we are parsing tentatively and are not committed to
27758 this tentative parse. */
27761 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27763 return (cp_parser_parsing_tentatively (parser)
27764 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27767 /* Returns nonzero iff an error has occurred during the most recent
27768 tentative parse. */
27771 cp_parser_error_occurred (cp_parser* parser)
27773 return (cp_parser_parsing_tentatively (parser)
27774 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27777 /* Returns nonzero if GNU extensions are allowed. */
27780 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27782 return parser->allow_gnu_extensions_p;
27785 /* Objective-C++ Productions */
27788 /* Parse an Objective-C expression, which feeds into a primary-expression
27792 objc-message-expression
27793 objc-string-literal
27794 objc-encode-expression
27795 objc-protocol-expression
27796 objc-selector-expression
27798 Returns a tree representation of the expression. */
27801 cp_parser_objc_expression (cp_parser* parser)
27803 /* Try to figure out what kind of declaration is present. */
27804 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27808 case CPP_OPEN_SQUARE:
27809 return cp_parser_objc_message_expression (parser);
27811 case CPP_OBJC_STRING:
27812 kwd = cp_lexer_consume_token (parser->lexer);
27813 return objc_build_string_object (kwd->u.value);
27816 switch (kwd->keyword)
27818 case RID_AT_ENCODE:
27819 return cp_parser_objc_encode_expression (parser);
27821 case RID_AT_PROTOCOL:
27822 return cp_parser_objc_protocol_expression (parser);
27824 case RID_AT_SELECTOR:
27825 return cp_parser_objc_selector_expression (parser);
27831 error_at (kwd->location,
27832 "misplaced %<@%D%> Objective-C++ construct",
27834 cp_parser_skip_to_end_of_block_or_statement (parser);
27837 return error_mark_node;
27840 /* Parse an Objective-C message expression.
27842 objc-message-expression:
27843 [ objc-message-receiver objc-message-args ]
27845 Returns a representation of an Objective-C message. */
27848 cp_parser_objc_message_expression (cp_parser* parser)
27850 tree receiver, messageargs;
27852 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27853 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27854 receiver = cp_parser_objc_message_receiver (parser);
27855 messageargs = cp_parser_objc_message_args (parser);
27856 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27857 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27859 tree result = objc_build_message_expr (receiver, messageargs);
27861 /* Construct a location e.g.
27864 ranging from the '[' to the ']', with the caret at the start. */
27865 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27866 protected_set_expr_location (result, combined_loc);
27871 /* Parse an objc-message-receiver.
27873 objc-message-receiver:
27875 simple-type-specifier
27877 Returns a representation of the type or expression. */
27880 cp_parser_objc_message_receiver (cp_parser* parser)
27884 /* An Objective-C message receiver may be either (1) a type
27885 or (2) an expression. */
27886 cp_parser_parse_tentatively (parser);
27887 rcv = cp_parser_expression (parser);
27889 /* If that worked out, fine. */
27890 if (cp_parser_parse_definitely (parser))
27893 cp_parser_parse_tentatively (parser);
27894 rcv = cp_parser_simple_type_specifier (parser,
27895 /*decl_specs=*/NULL,
27896 CP_PARSER_FLAGS_NONE);
27898 if (cp_parser_parse_definitely (parser))
27899 return objc_get_class_reference (rcv);
27901 cp_parser_error (parser, "objective-c++ message receiver expected");
27902 return error_mark_node;
27905 /* Parse the arguments and selectors comprising an Objective-C message.
27910 objc-selector-args , objc-comma-args
27912 objc-selector-args:
27913 objc-selector [opt] : assignment-expression
27914 objc-selector-args objc-selector [opt] : assignment-expression
27917 assignment-expression
27918 objc-comma-args , assignment-expression
27920 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27921 selector arguments and TREE_VALUE containing a list of comma
27925 cp_parser_objc_message_args (cp_parser* parser)
27927 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27928 bool maybe_unary_selector_p = true;
27929 cp_token *token = cp_lexer_peek_token (parser->lexer);
27931 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27933 tree selector = NULL_TREE, arg;
27935 if (token->type != CPP_COLON)
27936 selector = cp_parser_objc_selector (parser);
27938 /* Detect if we have a unary selector. */
27939 if (maybe_unary_selector_p
27940 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27941 return build_tree_list (selector, NULL_TREE);
27943 maybe_unary_selector_p = false;
27944 cp_parser_require (parser, CPP_COLON, RT_COLON);
27945 arg = cp_parser_assignment_expression (parser);
27948 = chainon (sel_args,
27949 build_tree_list (selector, arg));
27951 token = cp_lexer_peek_token (parser->lexer);
27954 /* Handle non-selector arguments, if any. */
27955 while (token->type == CPP_COMMA)
27959 cp_lexer_consume_token (parser->lexer);
27960 arg = cp_parser_assignment_expression (parser);
27963 = chainon (addl_args,
27964 build_tree_list (NULL_TREE, arg));
27966 token = cp_lexer_peek_token (parser->lexer);
27969 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27971 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27972 return build_tree_list (error_mark_node, error_mark_node);
27975 return build_tree_list (sel_args, addl_args);
27978 /* Parse an Objective-C encode expression.
27980 objc-encode-expression:
27981 @encode objc-typename
27983 Returns an encoded representation of the type argument. */
27986 cp_parser_objc_encode_expression (cp_parser* parser)
27990 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27992 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27993 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27994 token = cp_lexer_peek_token (parser->lexer);
27995 type = complete_type (cp_parser_type_id (parser));
27996 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28000 error_at (token->location,
28001 "%<@encode%> must specify a type as an argument");
28002 return error_mark_node;
28005 /* This happens if we find @encode(T) (where T is a template
28006 typename or something dependent on a template typename) when
28007 parsing a template. In that case, we can't compile it
28008 immediately, but we rather create an AT_ENCODE_EXPR which will
28009 need to be instantiated when the template is used.
28011 if (dependent_type_p (type))
28013 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28014 TREE_READONLY (value) = 1;
28019 /* Build a location of the form:
28022 with caret==start at the @ token, finishing at the close paren. */
28023 location_t combined_loc
28024 = make_location (start_loc, start_loc,
28025 cp_lexer_previous_token (parser->lexer)->location);
28027 return cp_expr (objc_build_encode_expr (type), combined_loc);
28030 /* Parse an Objective-C @defs expression. */
28033 cp_parser_objc_defs_expression (cp_parser *parser)
28037 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28038 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28039 name = cp_parser_identifier (parser);
28040 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28042 return objc_get_class_ivars (name);
28045 /* Parse an Objective-C protocol expression.
28047 objc-protocol-expression:
28048 @protocol ( identifier )
28050 Returns a representation of the protocol expression. */
28053 cp_parser_objc_protocol_expression (cp_parser* parser)
28056 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28058 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28059 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28060 proto = cp_parser_identifier (parser);
28061 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28063 /* Build a location of the form:
28066 with caret==start at the @ token, finishing at the close paren. */
28067 location_t combined_loc
28068 = make_location (start_loc, start_loc,
28069 cp_lexer_previous_token (parser->lexer)->location);
28070 tree result = objc_build_protocol_expr (proto);
28071 protected_set_expr_location (result, combined_loc);
28075 /* Parse an Objective-C selector expression.
28077 objc-selector-expression:
28078 @selector ( objc-method-signature )
28080 objc-method-signature:
28086 objc-selector-seq objc-selector :
28088 Returns a representation of the method selector. */
28091 cp_parser_objc_selector_expression (cp_parser* parser)
28093 tree sel_seq = NULL_TREE;
28094 bool maybe_unary_selector_p = true;
28096 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28098 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28099 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28100 token = cp_lexer_peek_token (parser->lexer);
28102 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28103 || token->type == CPP_SCOPE)
28105 tree selector = NULL_TREE;
28107 if (token->type != CPP_COLON
28108 || token->type == CPP_SCOPE)
28109 selector = cp_parser_objc_selector (parser);
28111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28112 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28114 /* Detect if we have a unary selector. */
28115 if (maybe_unary_selector_p)
28117 sel_seq = selector;
28118 goto finish_selector;
28122 cp_parser_error (parser, "expected %<:%>");
28125 maybe_unary_selector_p = false;
28126 token = cp_lexer_consume_token (parser->lexer);
28128 if (token->type == CPP_SCOPE)
28131 = chainon (sel_seq,
28132 build_tree_list (selector, NULL_TREE));
28134 = chainon (sel_seq,
28135 build_tree_list (NULL_TREE, NULL_TREE));
28139 = chainon (sel_seq,
28140 build_tree_list (selector, NULL_TREE));
28142 token = cp_lexer_peek_token (parser->lexer);
28146 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28149 /* Build a location of the form:
28152 with caret==start at the @ token, finishing at the close paren. */
28153 location_t combined_loc
28154 = make_location (loc, loc,
28155 cp_lexer_previous_token (parser->lexer)->location);
28156 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28157 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28158 protected_set_expr_location (result, combined_loc);
28162 /* Parse a list of identifiers.
28164 objc-identifier-list:
28166 objc-identifier-list , identifier
28168 Returns a TREE_LIST of identifier nodes. */
28171 cp_parser_objc_identifier_list (cp_parser* parser)
28177 identifier = cp_parser_identifier (parser);
28178 if (identifier == error_mark_node)
28179 return error_mark_node;
28181 list = build_tree_list (NULL_TREE, identifier);
28182 sep = cp_lexer_peek_token (parser->lexer);
28184 while (sep->type == CPP_COMMA)
28186 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28187 identifier = cp_parser_identifier (parser);
28188 if (identifier == error_mark_node)
28191 list = chainon (list, build_tree_list (NULL_TREE,
28193 sep = cp_lexer_peek_token (parser->lexer);
28199 /* Parse an Objective-C alias declaration.
28201 objc-alias-declaration:
28202 @compatibility_alias identifier identifier ;
28204 This function registers the alias mapping with the Objective-C front end.
28205 It returns nothing. */
28208 cp_parser_objc_alias_declaration (cp_parser* parser)
28212 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28213 alias = cp_parser_identifier (parser);
28214 orig = cp_parser_identifier (parser);
28215 objc_declare_alias (alias, orig);
28216 cp_parser_consume_semicolon_at_end_of_statement (parser);
28219 /* Parse an Objective-C class forward-declaration.
28221 objc-class-declaration:
28222 @class objc-identifier-list ;
28224 The function registers the forward declarations with the Objective-C
28225 front end. It returns nothing. */
28228 cp_parser_objc_class_declaration (cp_parser* parser)
28230 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28235 id = cp_parser_identifier (parser);
28236 if (id == error_mark_node)
28239 objc_declare_class (id);
28241 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28242 cp_lexer_consume_token (parser->lexer);
28246 cp_parser_consume_semicolon_at_end_of_statement (parser);
28249 /* Parse a list of Objective-C protocol references.
28251 objc-protocol-refs-opt:
28252 objc-protocol-refs [opt]
28254 objc-protocol-refs:
28255 < objc-identifier-list >
28257 Returns a TREE_LIST of identifiers, if any. */
28260 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28262 tree protorefs = NULL_TREE;
28264 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28266 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28267 protorefs = cp_parser_objc_identifier_list (parser);
28268 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28274 /* Parse a Objective-C visibility specification. */
28277 cp_parser_objc_visibility_spec (cp_parser* parser)
28279 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28281 switch (vis->keyword)
28283 case RID_AT_PRIVATE:
28284 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28286 case RID_AT_PROTECTED:
28287 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28289 case RID_AT_PUBLIC:
28290 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28292 case RID_AT_PACKAGE:
28293 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28299 /* Eat '@private'/'@protected'/'@public'. */
28300 cp_lexer_consume_token (parser->lexer);
28303 /* Parse an Objective-C method type. Return 'true' if it is a class
28304 (+) method, and 'false' if it is an instance (-) method. */
28307 cp_parser_objc_method_type (cp_parser* parser)
28309 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28315 /* Parse an Objective-C protocol qualifier. */
28318 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28320 tree quals = NULL_TREE, node;
28321 cp_token *token = cp_lexer_peek_token (parser->lexer);
28323 node = token->u.value;
28325 while (node && identifier_p (node)
28326 && (node == ridpointers [(int) RID_IN]
28327 || node == ridpointers [(int) RID_OUT]
28328 || node == ridpointers [(int) RID_INOUT]
28329 || node == ridpointers [(int) RID_BYCOPY]
28330 || node == ridpointers [(int) RID_BYREF]
28331 || node == ridpointers [(int) RID_ONEWAY]))
28333 quals = tree_cons (NULL_TREE, node, quals);
28334 cp_lexer_consume_token (parser->lexer);
28335 token = cp_lexer_peek_token (parser->lexer);
28336 node = token->u.value;
28342 /* Parse an Objective-C typename. */
28345 cp_parser_objc_typename (cp_parser* parser)
28347 tree type_name = NULL_TREE;
28349 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28351 tree proto_quals, cp_type = NULL_TREE;
28353 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28354 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28356 /* An ObjC type name may consist of just protocol qualifiers, in which
28357 case the type shall default to 'id'. */
28358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28360 cp_type = cp_parser_type_id (parser);
28362 /* If the type could not be parsed, an error has already
28363 been produced. For error recovery, behave as if it had
28364 not been specified, which will use the default type
28366 if (cp_type == error_mark_node)
28368 cp_type = NULL_TREE;
28369 /* We need to skip to the closing parenthesis as
28370 cp_parser_type_id() does not seem to do it for
28372 cp_parser_skip_to_closing_parenthesis (parser,
28373 /*recovering=*/true,
28374 /*or_comma=*/false,
28375 /*consume_paren=*/false);
28379 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28380 type_name = build_tree_list (proto_quals, cp_type);
28386 /* Check to see if TYPE refers to an Objective-C selector name. */
28389 cp_parser_objc_selector_p (enum cpp_ttype type)
28391 return (type == CPP_NAME || type == CPP_KEYWORD
28392 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28393 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28394 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28395 || type == CPP_XOR || type == CPP_XOR_EQ);
28398 /* Parse an Objective-C selector. */
28401 cp_parser_objc_selector (cp_parser* parser)
28403 cp_token *token = cp_lexer_consume_token (parser->lexer);
28405 if (!cp_parser_objc_selector_p (token->type))
28407 error_at (token->location, "invalid Objective-C++ selector name");
28408 return error_mark_node;
28411 /* C++ operator names are allowed to appear in ObjC selectors. */
28412 switch (token->type)
28414 case CPP_AND_AND: return get_identifier ("and");
28415 case CPP_AND_EQ: return get_identifier ("and_eq");
28416 case CPP_AND: return get_identifier ("bitand");
28417 case CPP_OR: return get_identifier ("bitor");
28418 case CPP_COMPL: return get_identifier ("compl");
28419 case CPP_NOT: return get_identifier ("not");
28420 case CPP_NOT_EQ: return get_identifier ("not_eq");
28421 case CPP_OR_OR: return get_identifier ("or");
28422 case CPP_OR_EQ: return get_identifier ("or_eq");
28423 case CPP_XOR: return get_identifier ("xor");
28424 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28425 default: return token->u.value;
28429 /* Parse an Objective-C params list. */
28432 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28434 tree params = NULL_TREE;
28435 bool maybe_unary_selector_p = true;
28436 cp_token *token = cp_lexer_peek_token (parser->lexer);
28438 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28440 tree selector = NULL_TREE, type_name, identifier;
28441 tree parm_attr = NULL_TREE;
28443 if (token->keyword == RID_ATTRIBUTE)
28446 if (token->type != CPP_COLON)
28447 selector = cp_parser_objc_selector (parser);
28449 /* Detect if we have a unary selector. */
28450 if (maybe_unary_selector_p
28451 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28453 params = selector; /* Might be followed by attributes. */
28457 maybe_unary_selector_p = false;
28458 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28460 /* Something went quite wrong. There should be a colon
28461 here, but there is not. Stop parsing parameters. */
28464 type_name = cp_parser_objc_typename (parser);
28465 /* New ObjC allows attributes on parameters too. */
28466 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28467 parm_attr = cp_parser_attributes_opt (parser);
28468 identifier = cp_parser_identifier (parser);
28472 objc_build_keyword_decl (selector,
28477 token = cp_lexer_peek_token (parser->lexer);
28480 if (params == NULL_TREE)
28482 cp_parser_error (parser, "objective-c++ method declaration is expected");
28483 return error_mark_node;
28486 /* We allow tail attributes for the method. */
28487 if (token->keyword == RID_ATTRIBUTE)
28489 *attributes = cp_parser_attributes_opt (parser);
28490 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28491 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28493 cp_parser_error (parser,
28494 "method attributes must be specified at the end");
28495 return error_mark_node;
28498 if (params == NULL_TREE)
28500 cp_parser_error (parser, "objective-c++ method declaration is expected");
28501 return error_mark_node;
28506 /* Parse the non-keyword Objective-C params. */
28509 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28512 tree params = make_node (TREE_LIST);
28513 cp_token *token = cp_lexer_peek_token (parser->lexer);
28514 *ellipsisp = false; /* Initially, assume no ellipsis. */
28516 while (token->type == CPP_COMMA)
28518 cp_parameter_declarator *parmdecl;
28521 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28522 token = cp_lexer_peek_token (parser->lexer);
28524 if (token->type == CPP_ELLIPSIS)
28526 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28528 token = cp_lexer_peek_token (parser->lexer);
28532 /* TODO: parse attributes for tail parameters. */
28533 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28534 parm = grokdeclarator (parmdecl->declarator,
28535 &parmdecl->decl_specifiers,
28536 PARM, /*initialized=*/0,
28537 /*attrlist=*/NULL);
28539 chainon (params, build_tree_list (NULL_TREE, parm));
28540 token = cp_lexer_peek_token (parser->lexer);
28543 /* We allow tail attributes for the method. */
28544 if (token->keyword == RID_ATTRIBUTE)
28546 if (*attributes == NULL_TREE)
28548 *attributes = cp_parser_attributes_opt (parser);
28549 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28550 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28554 /* We have an error, but parse the attributes, so that we can
28556 *attributes = cp_parser_attributes_opt (parser);
28558 cp_parser_error (parser,
28559 "method attributes must be specified at the end");
28560 return error_mark_node;
28566 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28569 cp_parser_objc_interstitial_code (cp_parser* parser)
28571 cp_token *token = cp_lexer_peek_token (parser->lexer);
28573 /* If the next token is `extern' and the following token is a string
28574 literal, then we have a linkage specification. */
28575 if (token->keyword == RID_EXTERN
28576 && cp_parser_is_pure_string_literal
28577 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28578 cp_parser_linkage_specification (parser);
28579 /* Handle #pragma, if any. */
28580 else if (token->type == CPP_PRAGMA)
28581 cp_parser_pragma (parser, pragma_objc_icode, NULL);
28582 /* Allow stray semicolons. */
28583 else if (token->type == CPP_SEMICOLON)
28584 cp_lexer_consume_token (parser->lexer);
28585 /* Mark methods as optional or required, when building protocols. */
28586 else if (token->keyword == RID_AT_OPTIONAL)
28588 cp_lexer_consume_token (parser->lexer);
28589 objc_set_method_opt (true);
28591 else if (token->keyword == RID_AT_REQUIRED)
28593 cp_lexer_consume_token (parser->lexer);
28594 objc_set_method_opt (false);
28596 else if (token->keyword == RID_NAMESPACE)
28597 cp_parser_namespace_definition (parser);
28598 /* Other stray characters must generate errors. */
28599 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28601 cp_lexer_consume_token (parser->lexer);
28602 error ("stray %qs between Objective-C++ methods",
28603 token->type == CPP_OPEN_BRACE ? "{" : "}");
28605 /* Finally, try to parse a block-declaration, or a function-definition. */
28607 cp_parser_block_declaration (parser, /*statement_p=*/false);
28610 /* Parse a method signature. */
28613 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28615 tree rettype, kwdparms, optparms;
28616 bool ellipsis = false;
28617 bool is_class_method;
28619 is_class_method = cp_parser_objc_method_type (parser);
28620 rettype = cp_parser_objc_typename (parser);
28621 *attributes = NULL_TREE;
28622 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28623 if (kwdparms == error_mark_node)
28624 return error_mark_node;
28625 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28626 if (optparms == error_mark_node)
28627 return error_mark_node;
28629 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28633 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28636 cp_lexer_save_tokens (parser->lexer);
28637 tattr = cp_parser_attributes_opt (parser);
28638 gcc_assert (tattr) ;
28640 /* If the attributes are followed by a method introducer, this is not allowed.
28641 Dump the attributes and flag the situation. */
28642 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28643 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28646 /* Otherwise, the attributes introduce some interstitial code, possibly so
28647 rewind to allow that check. */
28648 cp_lexer_rollback_tokens (parser->lexer);
28652 /* Parse an Objective-C method prototype list. */
28655 cp_parser_objc_method_prototype_list (cp_parser* parser)
28657 cp_token *token = cp_lexer_peek_token (parser->lexer);
28659 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28661 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28663 tree attributes, sig;
28664 bool is_class_method;
28665 if (token->type == CPP_PLUS)
28666 is_class_method = true;
28668 is_class_method = false;
28669 sig = cp_parser_objc_method_signature (parser, &attributes);
28670 if (sig == error_mark_node)
28672 cp_parser_skip_to_end_of_block_or_statement (parser);
28673 token = cp_lexer_peek_token (parser->lexer);
28676 objc_add_method_declaration (is_class_method, sig, attributes);
28677 cp_parser_consume_semicolon_at_end_of_statement (parser);
28679 else if (token->keyword == RID_AT_PROPERTY)
28680 cp_parser_objc_at_property_declaration (parser);
28681 else if (token->keyword == RID_ATTRIBUTE
28682 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28683 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28685 "prefix attributes are ignored for methods");
28687 /* Allow for interspersed non-ObjC++ code. */
28688 cp_parser_objc_interstitial_code (parser);
28690 token = cp_lexer_peek_token (parser->lexer);
28693 if (token->type != CPP_EOF)
28694 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28696 cp_parser_error (parser, "expected %<@end%>");
28698 objc_finish_interface ();
28701 /* Parse an Objective-C method definition list. */
28704 cp_parser_objc_method_definition_list (cp_parser* parser)
28706 cp_token *token = cp_lexer_peek_token (parser->lexer);
28708 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28712 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28715 tree sig, attribute;
28716 bool is_class_method;
28717 if (token->type == CPP_PLUS)
28718 is_class_method = true;
28720 is_class_method = false;
28721 push_deferring_access_checks (dk_deferred);
28722 sig = cp_parser_objc_method_signature (parser, &attribute);
28723 if (sig == error_mark_node)
28725 cp_parser_skip_to_end_of_block_or_statement (parser);
28726 token = cp_lexer_peek_token (parser->lexer);
28729 objc_start_method_definition (is_class_method, sig, attribute,
28732 /* For historical reasons, we accept an optional semicolon. */
28733 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28734 cp_lexer_consume_token (parser->lexer);
28736 ptk = cp_lexer_peek_token (parser->lexer);
28737 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28738 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28740 perform_deferred_access_checks (tf_warning_or_error);
28741 stop_deferring_access_checks ();
28742 meth = cp_parser_function_definition_after_declarator (parser,
28744 pop_deferring_access_checks ();
28745 objc_finish_method_definition (meth);
28748 /* The following case will be removed once @synthesize is
28749 completely implemented. */
28750 else if (token->keyword == RID_AT_PROPERTY)
28751 cp_parser_objc_at_property_declaration (parser);
28752 else if (token->keyword == RID_AT_SYNTHESIZE)
28753 cp_parser_objc_at_synthesize_declaration (parser);
28754 else if (token->keyword == RID_AT_DYNAMIC)
28755 cp_parser_objc_at_dynamic_declaration (parser);
28756 else if (token->keyword == RID_ATTRIBUTE
28757 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28758 warning_at (token->location, OPT_Wattributes,
28759 "prefix attributes are ignored for methods");
28761 /* Allow for interspersed non-ObjC++ code. */
28762 cp_parser_objc_interstitial_code (parser);
28764 token = cp_lexer_peek_token (parser->lexer);
28767 if (token->type != CPP_EOF)
28768 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28770 cp_parser_error (parser, "expected %<@end%>");
28772 objc_finish_implementation ();
28775 /* Parse Objective-C ivars. */
28778 cp_parser_objc_class_ivars (cp_parser* parser)
28780 cp_token *token = cp_lexer_peek_token (parser->lexer);
28782 if (token->type != CPP_OPEN_BRACE)
28783 return; /* No ivars specified. */
28785 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28786 token = cp_lexer_peek_token (parser->lexer);
28788 while (token->type != CPP_CLOSE_BRACE
28789 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28791 cp_decl_specifier_seq declspecs;
28792 int decl_class_or_enum_p;
28793 tree prefix_attributes;
28795 cp_parser_objc_visibility_spec (parser);
28797 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28800 cp_parser_decl_specifier_seq (parser,
28801 CP_PARSER_FLAGS_OPTIONAL,
28803 &decl_class_or_enum_p);
28805 /* auto, register, static, extern, mutable. */
28806 if (declspecs.storage_class != sc_none)
28808 cp_parser_error (parser, "invalid type for instance variable");
28809 declspecs.storage_class = sc_none;
28812 /* thread_local. */
28813 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28815 cp_parser_error (parser, "invalid type for instance variable");
28816 declspecs.locations[ds_thread] = 0;
28820 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28822 cp_parser_error (parser, "invalid type for instance variable");
28823 declspecs.locations[ds_typedef] = 0;
28826 prefix_attributes = declspecs.attributes;
28827 declspecs.attributes = NULL_TREE;
28829 /* Keep going until we hit the `;' at the end of the
28831 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28833 tree width = NULL_TREE, attributes, first_attribute, decl;
28834 cp_declarator *declarator = NULL;
28835 int ctor_dtor_or_conv_p;
28837 /* Check for a (possibly unnamed) bitfield declaration. */
28838 token = cp_lexer_peek_token (parser->lexer);
28839 if (token->type == CPP_COLON)
28842 if (token->type == CPP_NAME
28843 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28846 /* Get the name of the bitfield. */
28847 declarator = make_id_declarator (NULL_TREE,
28848 cp_parser_identifier (parser),
28852 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28853 /* Get the width of the bitfield. */
28855 = cp_parser_constant_expression (parser);
28859 /* Parse the declarator. */
28861 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28862 &ctor_dtor_or_conv_p,
28863 /*parenthesized_p=*/NULL,
28864 /*member_p=*/false,
28865 /*friend_p=*/false);
28868 /* Look for attributes that apply to the ivar. */
28869 attributes = cp_parser_attributes_opt (parser);
28870 /* Remember which attributes are prefix attributes and
28872 first_attribute = attributes;
28873 /* Combine the attributes. */
28874 attributes = chainon (prefix_attributes, attributes);
28877 /* Create the bitfield declaration. */
28878 decl = grokbitfield (declarator, &declspecs,
28882 decl = grokfield (declarator, &declspecs,
28883 NULL_TREE, /*init_const_expr_p=*/false,
28884 NULL_TREE, attributes);
28886 /* Add the instance variable. */
28887 if (decl != error_mark_node && decl != NULL_TREE)
28888 objc_add_instance_variable (decl);
28890 /* Reset PREFIX_ATTRIBUTES. */
28891 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28892 attributes = TREE_CHAIN (attributes);
28894 TREE_CHAIN (attributes) = NULL_TREE;
28896 token = cp_lexer_peek_token (parser->lexer);
28898 if (token->type == CPP_COMMA)
28900 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28906 cp_parser_consume_semicolon_at_end_of_statement (parser);
28907 token = cp_lexer_peek_token (parser->lexer);
28910 if (token->keyword == RID_AT_END)
28911 cp_parser_error (parser, "expected %<}%>");
28913 /* Do not consume the RID_AT_END, so it will be read again as terminating
28914 the @interface of @implementation. */
28915 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28916 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28918 /* For historical reasons, we accept an optional semicolon. */
28919 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28920 cp_lexer_consume_token (parser->lexer);
28923 /* Parse an Objective-C protocol declaration. */
28926 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28928 tree proto, protorefs;
28931 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28934 tok = cp_lexer_peek_token (parser->lexer);
28935 error_at (tok->location, "identifier expected after %<@protocol%>");
28936 cp_parser_consume_semicolon_at_end_of_statement (parser);
28940 /* See if we have a forward declaration or a definition. */
28941 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28943 /* Try a forward declaration first. */
28944 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28950 id = cp_parser_identifier (parser);
28951 if (id == error_mark_node)
28954 objc_declare_protocol (id, attributes);
28956 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28957 cp_lexer_consume_token (parser->lexer);
28961 cp_parser_consume_semicolon_at_end_of_statement (parser);
28964 /* Ok, we got a full-fledged definition (or at least should). */
28967 proto = cp_parser_identifier (parser);
28968 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28969 objc_start_protocol (proto, protorefs, attributes);
28970 cp_parser_objc_method_prototype_list (parser);
28974 /* Parse an Objective-C superclass or category. */
28977 cp_parser_objc_superclass_or_category (cp_parser *parser,
28980 tree *categ, bool *is_class_extension)
28982 cp_token *next = cp_lexer_peek_token (parser->lexer);
28984 *super = *categ = NULL_TREE;
28985 *is_class_extension = false;
28986 if (next->type == CPP_COLON)
28988 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28989 *super = cp_parser_identifier (parser);
28991 else if (next->type == CPP_OPEN_PAREN)
28993 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28995 /* If there is no category name, and this is an @interface, we
28996 have a class extension. */
28997 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28999 *categ = NULL_TREE;
29000 *is_class_extension = true;
29003 *categ = cp_parser_identifier (parser);
29005 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29009 /* Parse an Objective-C class interface. */
29012 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29014 tree name, super, categ, protos;
29015 bool is_class_extension;
29017 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29018 name = cp_parser_identifier (parser);
29019 if (name == error_mark_node)
29021 /* It's hard to recover because even if valid @interface stuff
29022 is to follow, we can't compile it (or validate it) if we
29023 don't even know which class it refers to. Let's assume this
29024 was a stray '@interface' token in the stream and skip it.
29028 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29029 &is_class_extension);
29030 protos = cp_parser_objc_protocol_refs_opt (parser);
29032 /* We have either a class or a category on our hands. */
29033 if (categ || is_class_extension)
29034 objc_start_category_interface (name, categ, protos, attributes);
29037 objc_start_class_interface (name, super, protos, attributes);
29038 /* Handle instance variable declarations, if any. */
29039 cp_parser_objc_class_ivars (parser);
29040 objc_continue_interface ();
29043 cp_parser_objc_method_prototype_list (parser);
29046 /* Parse an Objective-C class implementation. */
29049 cp_parser_objc_class_implementation (cp_parser* parser)
29051 tree name, super, categ;
29052 bool is_class_extension;
29054 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29055 name = cp_parser_identifier (parser);
29056 if (name == error_mark_node)
29058 /* It's hard to recover because even if valid @implementation
29059 stuff is to follow, we can't compile it (or validate it) if
29060 we don't even know which class it refers to. Let's assume
29061 this was a stray '@implementation' token in the stream and
29066 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29067 &is_class_extension);
29069 /* We have either a class or a category on our hands. */
29071 objc_start_category_implementation (name, categ);
29074 objc_start_class_implementation (name, super);
29075 /* Handle instance variable declarations, if any. */
29076 cp_parser_objc_class_ivars (parser);
29077 objc_continue_implementation ();
29080 cp_parser_objc_method_definition_list (parser);
29083 /* Consume the @end token and finish off the implementation. */
29086 cp_parser_objc_end_implementation (cp_parser* parser)
29088 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29089 objc_finish_implementation ();
29092 /* Parse an Objective-C declaration. */
29095 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29097 /* Try to figure out what kind of declaration is present. */
29098 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29101 switch (kwd->keyword)
29106 error_at (kwd->location, "attributes may not be specified before"
29107 " the %<@%D%> Objective-C++ keyword",
29111 case RID_AT_IMPLEMENTATION:
29112 warning_at (kwd->location, OPT_Wattributes,
29113 "prefix attributes are ignored before %<@%D%>",
29120 switch (kwd->keyword)
29123 cp_parser_objc_alias_declaration (parser);
29126 cp_parser_objc_class_declaration (parser);
29128 case RID_AT_PROTOCOL:
29129 cp_parser_objc_protocol_declaration (parser, attributes);
29131 case RID_AT_INTERFACE:
29132 cp_parser_objc_class_interface (parser, attributes);
29134 case RID_AT_IMPLEMENTATION:
29135 cp_parser_objc_class_implementation (parser);
29138 cp_parser_objc_end_implementation (parser);
29141 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29143 cp_parser_skip_to_end_of_block_or_statement (parser);
29147 /* Parse an Objective-C try-catch-finally statement.
29149 objc-try-catch-finally-stmt:
29150 @try compound-statement objc-catch-clause-seq [opt]
29151 objc-finally-clause [opt]
29153 objc-catch-clause-seq:
29154 objc-catch-clause objc-catch-clause-seq [opt]
29157 @catch ( objc-exception-declaration ) compound-statement
29159 objc-finally-clause:
29160 @finally compound-statement
29162 objc-exception-declaration:
29163 parameter-declaration
29166 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29170 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29171 for C. Keep them in sync. */
29174 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29176 location_t location;
29179 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29180 location = cp_lexer_peek_token (parser->lexer)->location;
29181 objc_maybe_warn_exceptions (location);
29182 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29183 node, lest it get absorbed into the surrounding block. */
29184 stmt = push_stmt_list ();
29185 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29186 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29188 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29190 cp_parameter_declarator *parm;
29191 tree parameter_declaration = error_mark_node;
29192 bool seen_open_paren = false;
29194 cp_lexer_consume_token (parser->lexer);
29195 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29196 seen_open_paren = true;
29197 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29199 /* We have "@catch (...)" (where the '...' are literally
29200 what is in the code). Skip the '...'.
29201 parameter_declaration is set to NULL_TREE, and
29202 objc_being_catch_clauses() knows that that means
29204 cp_lexer_consume_token (parser->lexer);
29205 parameter_declaration = NULL_TREE;
29209 /* We have "@catch (NSException *exception)" or something
29210 like that. Parse the parameter declaration. */
29211 parm = cp_parser_parameter_declaration (parser, false, NULL);
29213 parameter_declaration = error_mark_node;
29215 parameter_declaration = grokdeclarator (parm->declarator,
29216 &parm->decl_specifiers,
29217 PARM, /*initialized=*/0,
29218 /*attrlist=*/NULL);
29220 if (seen_open_paren)
29221 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29224 /* If there was no open parenthesis, we are recovering from
29225 an error, and we are trying to figure out what mistake
29226 the user has made. */
29228 /* If there is an immediate closing parenthesis, the user
29229 probably forgot the opening one (ie, they typed "@catch
29230 NSException *e)". Parse the closing parenthesis and keep
29232 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29233 cp_lexer_consume_token (parser->lexer);
29235 /* If these is no immediate closing parenthesis, the user
29236 probably doesn't know that parenthesis are required at
29237 all (ie, they typed "@catch NSException *e"). So, just
29238 forget about the closing parenthesis and keep going. */
29240 objc_begin_catch_clause (parameter_declaration);
29241 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29242 objc_finish_catch_clause ();
29244 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29246 cp_lexer_consume_token (parser->lexer);
29247 location = cp_lexer_peek_token (parser->lexer)->location;
29248 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29249 node, lest it get absorbed into the surrounding block. */
29250 stmt = push_stmt_list ();
29251 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29252 objc_build_finally_clause (location, pop_stmt_list (stmt));
29255 return objc_finish_try_stmt ();
29258 /* Parse an Objective-C synchronized statement.
29260 objc-synchronized-stmt:
29261 @synchronized ( expression ) compound-statement
29263 Returns NULL_TREE. */
29266 cp_parser_objc_synchronized_statement (cp_parser *parser)
29268 location_t location;
29271 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29273 location = cp_lexer_peek_token (parser->lexer)->location;
29274 objc_maybe_warn_exceptions (location);
29275 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29276 lock = cp_parser_expression (parser);
29277 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29279 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29280 node, lest it get absorbed into the surrounding block. */
29281 stmt = push_stmt_list ();
29282 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29284 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29287 /* Parse an Objective-C throw statement.
29290 @throw assignment-expression [opt] ;
29292 Returns a constructed '@throw' statement. */
29295 cp_parser_objc_throw_statement (cp_parser *parser)
29297 tree expr = NULL_TREE;
29298 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29300 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29303 expr = cp_parser_expression (parser);
29305 cp_parser_consume_semicolon_at_end_of_statement (parser);
29307 return objc_build_throw_stmt (loc, expr);
29310 /* Parse an Objective-C statement. */
29313 cp_parser_objc_statement (cp_parser * parser)
29315 /* Try to figure out what kind of declaration is present. */
29316 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29318 switch (kwd->keyword)
29321 return cp_parser_objc_try_catch_finally_statement (parser);
29322 case RID_AT_SYNCHRONIZED:
29323 return cp_parser_objc_synchronized_statement (parser);
29325 return cp_parser_objc_throw_statement (parser);
29327 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29329 cp_parser_skip_to_end_of_block_or_statement (parser);
29332 return error_mark_node;
29335 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29336 look ahead to see if an objc keyword follows the attributes. This
29337 is to detect the use of prefix attributes on ObjC @interface and
29341 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29343 cp_lexer_save_tokens (parser->lexer);
29344 *attrib = cp_parser_attributes_opt (parser);
29345 gcc_assert (*attrib);
29346 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29348 cp_lexer_commit_tokens (parser->lexer);
29351 cp_lexer_rollback_tokens (parser->lexer);
29355 /* This routine is a minimal replacement for
29356 c_parser_struct_declaration () used when parsing the list of
29357 types/names or ObjC++ properties. For example, when parsing the
29360 @property (readonly) int a, b, c;
29362 this function is responsible for parsing "int a, int b, int c" and
29363 returning the declarations as CHAIN of DECLs.
29365 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29366 similar parsing. */
29368 cp_parser_objc_struct_declaration (cp_parser *parser)
29370 tree decls = NULL_TREE;
29371 cp_decl_specifier_seq declspecs;
29372 int decl_class_or_enum_p;
29373 tree prefix_attributes;
29375 cp_parser_decl_specifier_seq (parser,
29376 CP_PARSER_FLAGS_NONE,
29378 &decl_class_or_enum_p);
29380 if (declspecs.type == error_mark_node)
29381 return error_mark_node;
29383 /* auto, register, static, extern, mutable. */
29384 if (declspecs.storage_class != sc_none)
29386 cp_parser_error (parser, "invalid type for property");
29387 declspecs.storage_class = sc_none;
29390 /* thread_local. */
29391 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29393 cp_parser_error (parser, "invalid type for property");
29394 declspecs.locations[ds_thread] = 0;
29398 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29400 cp_parser_error (parser, "invalid type for property");
29401 declspecs.locations[ds_typedef] = 0;
29404 prefix_attributes = declspecs.attributes;
29405 declspecs.attributes = NULL_TREE;
29407 /* Keep going until we hit the `;' at the end of the declaration. */
29408 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29410 tree attributes, first_attribute, decl;
29411 cp_declarator *declarator;
29414 /* Parse the declarator. */
29415 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29416 NULL, NULL, false, false);
29418 /* Look for attributes that apply to the ivar. */
29419 attributes = cp_parser_attributes_opt (parser);
29420 /* Remember which attributes are prefix attributes and
29422 first_attribute = attributes;
29423 /* Combine the attributes. */
29424 attributes = chainon (prefix_attributes, attributes);
29426 decl = grokfield (declarator, &declspecs,
29427 NULL_TREE, /*init_const_expr_p=*/false,
29428 NULL_TREE, attributes);
29430 if (decl == error_mark_node || decl == NULL_TREE)
29431 return error_mark_node;
29433 /* Reset PREFIX_ATTRIBUTES. */
29434 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29435 attributes = TREE_CHAIN (attributes);
29437 TREE_CHAIN (attributes) = NULL_TREE;
29439 DECL_CHAIN (decl) = decls;
29442 token = cp_lexer_peek_token (parser->lexer);
29443 if (token->type == CPP_COMMA)
29445 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29454 /* Parse an Objective-C @property declaration. The syntax is:
29456 objc-property-declaration:
29457 '@property' objc-property-attributes[opt] struct-declaration ;
29459 objc-property-attributes:
29460 '(' objc-property-attribute-list ')'
29462 objc-property-attribute-list:
29463 objc-property-attribute
29464 objc-property-attribute-list, objc-property-attribute
29466 objc-property-attribute
29467 'getter' = identifier
29468 'setter' = identifier
29477 @property NSString *name;
29478 @property (readonly) id object;
29479 @property (retain, nonatomic, getter=getTheName) id name;
29480 @property int a, b, c;
29482 PS: This function is identical to
29483 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29485 cp_parser_objc_at_property_declaration (cp_parser *parser)
29487 /* The following variables hold the attributes of the properties as
29488 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29489 seen. When we see an attribute, we set them to 'true' (if they
29490 are boolean properties) or to the identifier (if they have an
29491 argument, ie, for getter and setter). Note that here we only
29492 parse the list of attributes, check the syntax and accumulate the
29493 attributes that we find. objc_add_property_declaration() will
29494 then process the information. */
29495 bool property_assign = false;
29496 bool property_copy = false;
29497 tree property_getter_ident = NULL_TREE;
29498 bool property_nonatomic = false;
29499 bool property_readonly = false;
29500 bool property_readwrite = false;
29501 bool property_retain = false;
29502 tree property_setter_ident = NULL_TREE;
29504 /* 'properties' is the list of properties that we read. Usually a
29505 single one, but maybe more (eg, in "@property int a, b, c;" there
29510 loc = cp_lexer_peek_token (parser->lexer)->location;
29512 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29514 /* Parse the optional attribute list... */
29515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29518 cp_lexer_consume_token (parser->lexer);
29522 bool syntax_error = false;
29523 cp_token *token = cp_lexer_peek_token (parser->lexer);
29526 if (token->type != CPP_NAME)
29528 cp_parser_error (parser, "expected identifier");
29531 keyword = C_RID_CODE (token->u.value);
29532 cp_lexer_consume_token (parser->lexer);
29535 case RID_ASSIGN: property_assign = true; break;
29536 case RID_COPY: property_copy = true; break;
29537 case RID_NONATOMIC: property_nonatomic = true; break;
29538 case RID_READONLY: property_readonly = true; break;
29539 case RID_READWRITE: property_readwrite = true; break;
29540 case RID_RETAIN: property_retain = true; break;
29544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29546 if (keyword == RID_GETTER)
29547 cp_parser_error (parser,
29548 "missing %<=%> (after %<getter%> attribute)");
29550 cp_parser_error (parser,
29551 "missing %<=%> (after %<setter%> attribute)");
29552 syntax_error = true;
29555 cp_lexer_consume_token (parser->lexer); /* eat the = */
29556 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29558 cp_parser_error (parser, "expected identifier");
29559 syntax_error = true;
29562 if (keyword == RID_SETTER)
29564 if (property_setter_ident != NULL_TREE)
29566 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29567 cp_lexer_consume_token (parser->lexer);
29570 property_setter_ident = cp_parser_objc_selector (parser);
29571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29572 cp_parser_error (parser, "setter name must terminate with %<:%>");
29574 cp_lexer_consume_token (parser->lexer);
29578 if (property_getter_ident != NULL_TREE)
29580 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29581 cp_lexer_consume_token (parser->lexer);
29584 property_getter_ident = cp_parser_objc_selector (parser);
29588 cp_parser_error (parser, "unknown property attribute");
29589 syntax_error = true;
29596 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29597 cp_lexer_consume_token (parser->lexer);
29602 /* FIXME: "@property (setter, assign);" will generate a spurious
29603 "error: expected ‘)’ before ‘,’ token". This is because
29604 cp_parser_require, unlike the C counterpart, will produce an
29605 error even if we are in error recovery. */
29606 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29608 cp_parser_skip_to_closing_parenthesis (parser,
29609 /*recovering=*/true,
29610 /*or_comma=*/false,
29611 /*consume_paren=*/true);
29615 /* ... and the property declaration(s). */
29616 properties = cp_parser_objc_struct_declaration (parser);
29618 if (properties == error_mark_node)
29620 cp_parser_skip_to_end_of_statement (parser);
29621 /* If the next token is now a `;', consume it. */
29622 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29623 cp_lexer_consume_token (parser->lexer);
29627 if (properties == NULL_TREE)
29628 cp_parser_error (parser, "expected identifier");
29631 /* Comma-separated properties are chained together in
29632 reverse order; add them one by one. */
29633 properties = nreverse (properties);
29635 for (; properties; properties = TREE_CHAIN (properties))
29636 objc_add_property_declaration (loc, copy_node (properties),
29637 property_readonly, property_readwrite,
29638 property_assign, property_retain,
29639 property_copy, property_nonatomic,
29640 property_getter_ident, property_setter_ident);
29643 cp_parser_consume_semicolon_at_end_of_statement (parser);
29646 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29648 objc-synthesize-declaration:
29649 @synthesize objc-synthesize-identifier-list ;
29651 objc-synthesize-identifier-list:
29652 objc-synthesize-identifier
29653 objc-synthesize-identifier-list, objc-synthesize-identifier
29655 objc-synthesize-identifier
29657 identifier = identifier
29660 @synthesize MyProperty;
29661 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29663 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29664 for C. Keep them in sync.
29667 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29669 tree list = NULL_TREE;
29671 loc = cp_lexer_peek_token (parser->lexer)->location;
29673 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29676 tree property, ivar;
29677 property = cp_parser_identifier (parser);
29678 if (property == error_mark_node)
29680 cp_parser_consume_semicolon_at_end_of_statement (parser);
29683 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29685 cp_lexer_consume_token (parser->lexer);
29686 ivar = cp_parser_identifier (parser);
29687 if (ivar == error_mark_node)
29689 cp_parser_consume_semicolon_at_end_of_statement (parser);
29695 list = chainon (list, build_tree_list (ivar, property));
29696 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29697 cp_lexer_consume_token (parser->lexer);
29701 cp_parser_consume_semicolon_at_end_of_statement (parser);
29702 objc_add_synthesize_declaration (loc, list);
29705 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29707 objc-dynamic-declaration:
29708 @dynamic identifier-list ;
29711 @dynamic MyProperty;
29712 @dynamic MyProperty, AnotherProperty;
29714 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29715 for C. Keep them in sync.
29718 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29720 tree list = NULL_TREE;
29722 loc = cp_lexer_peek_token (parser->lexer)->location;
29724 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29728 property = cp_parser_identifier (parser);
29729 if (property == error_mark_node)
29731 cp_parser_consume_semicolon_at_end_of_statement (parser);
29734 list = chainon (list, build_tree_list (NULL, property));
29735 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29736 cp_lexer_consume_token (parser->lexer);
29740 cp_parser_consume_semicolon_at_end_of_statement (parser);
29741 objc_add_dynamic_declaration (loc, list);
29745 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29747 /* Returns name of the next clause.
29748 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29749 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29750 returned and the token is consumed. */
29752 static pragma_omp_clause
29753 cp_parser_omp_clause_name (cp_parser *parser)
29755 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29757 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29758 result = PRAGMA_OACC_CLAUSE_AUTO;
29759 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29760 result = PRAGMA_OMP_CLAUSE_IF;
29761 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29762 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29763 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29764 result = PRAGMA_OACC_CLAUSE_DELETE;
29765 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29766 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29767 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29768 result = PRAGMA_OMP_CLAUSE_FOR;
29769 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29771 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29772 const char *p = IDENTIFIER_POINTER (id);
29777 if (!strcmp ("aligned", p))
29778 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29779 else if (!strcmp ("async", p))
29780 result = PRAGMA_OACC_CLAUSE_ASYNC;
29783 if (!strcmp ("collapse", p))
29784 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29785 else if (!strcmp ("copy", p))
29786 result = PRAGMA_OACC_CLAUSE_COPY;
29787 else if (!strcmp ("copyin", p))
29788 result = PRAGMA_OMP_CLAUSE_COPYIN;
29789 else if (!strcmp ("copyout", p))
29790 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29791 else if (!strcmp ("copyprivate", p))
29792 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29793 else if (!strcmp ("create", p))
29794 result = PRAGMA_OACC_CLAUSE_CREATE;
29797 if (!strcmp ("defaultmap", p))
29798 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29799 else if (!strcmp ("depend", p))
29800 result = PRAGMA_OMP_CLAUSE_DEPEND;
29801 else if (!strcmp ("device", p))
29802 result = PRAGMA_OMP_CLAUSE_DEVICE;
29803 else if (!strcmp ("deviceptr", p))
29804 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29805 else if (!strcmp ("device_resident", p))
29806 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29807 else if (!strcmp ("dist_schedule", p))
29808 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29811 if (!strcmp ("final", p))
29812 result = PRAGMA_OMP_CLAUSE_FINAL;
29813 else if (!strcmp ("firstprivate", p))
29814 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29815 else if (!strcmp ("from", p))
29816 result = PRAGMA_OMP_CLAUSE_FROM;
29819 if (!strcmp ("gang", p))
29820 result = PRAGMA_OACC_CLAUSE_GANG;
29821 else if (!strcmp ("grainsize", p))
29822 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29825 if (!strcmp ("hint", p))
29826 result = PRAGMA_OMP_CLAUSE_HINT;
29827 else if (!strcmp ("host", p))
29828 result = PRAGMA_OACC_CLAUSE_HOST;
29831 if (!strcmp ("inbranch", p))
29832 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29833 else if (!strcmp ("independent", p))
29834 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29835 else if (!strcmp ("is_device_ptr", p))
29836 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29839 if (!strcmp ("lastprivate", p))
29840 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29841 else if (!strcmp ("linear", p))
29842 result = PRAGMA_OMP_CLAUSE_LINEAR;
29843 else if (!strcmp ("link", p))
29844 result = PRAGMA_OMP_CLAUSE_LINK;
29847 if (!strcmp ("map", p))
29848 result = PRAGMA_OMP_CLAUSE_MAP;
29849 else if (!strcmp ("mergeable", p))
29850 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29851 else if (flag_cilkplus && !strcmp ("mask", p))
29852 result = PRAGMA_CILK_CLAUSE_MASK;
29855 if (!strcmp ("nogroup", p))
29856 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29857 else if (!strcmp ("notinbranch", p))
29858 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29859 else if (!strcmp ("nowait", p))
29860 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29861 else if (flag_cilkplus && !strcmp ("nomask", p))
29862 result = PRAGMA_CILK_CLAUSE_NOMASK;
29863 else if (!strcmp ("num_gangs", p))
29864 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29865 else if (!strcmp ("num_tasks", p))
29866 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29867 else if (!strcmp ("num_teams", p))
29868 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29869 else if (!strcmp ("num_threads", p))
29870 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29871 else if (!strcmp ("num_workers", p))
29872 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29875 if (!strcmp ("ordered", p))
29876 result = PRAGMA_OMP_CLAUSE_ORDERED;
29879 if (!strcmp ("parallel", p))
29880 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29881 else if (!strcmp ("present", p))
29882 result = PRAGMA_OACC_CLAUSE_PRESENT;
29883 else if (!strcmp ("present_or_copy", p)
29884 || !strcmp ("pcopy", p))
29885 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29886 else if (!strcmp ("present_or_copyin", p)
29887 || !strcmp ("pcopyin", p))
29888 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29889 else if (!strcmp ("present_or_copyout", p)
29890 || !strcmp ("pcopyout", p))
29891 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29892 else if (!strcmp ("present_or_create", p)
29893 || !strcmp ("pcreate", p))
29894 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29895 else if (!strcmp ("priority", p))
29896 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29897 else if (!strcmp ("proc_bind", p))
29898 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29901 if (!strcmp ("reduction", p))
29902 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29905 if (!strcmp ("safelen", p))
29906 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29907 else if (!strcmp ("schedule", p))
29908 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29909 else if (!strcmp ("sections", p))
29910 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29911 else if (!strcmp ("self", p))
29912 result = PRAGMA_OACC_CLAUSE_SELF;
29913 else if (!strcmp ("seq", p))
29914 result = PRAGMA_OACC_CLAUSE_SEQ;
29915 else if (!strcmp ("shared", p))
29916 result = PRAGMA_OMP_CLAUSE_SHARED;
29917 else if (!strcmp ("simd", p))
29918 result = PRAGMA_OMP_CLAUSE_SIMD;
29919 else if (!strcmp ("simdlen", p))
29920 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29923 if (!strcmp ("taskgroup", p))
29924 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29925 else if (!strcmp ("thread_limit", p))
29926 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29927 else if (!strcmp ("threads", p))
29928 result = PRAGMA_OMP_CLAUSE_THREADS;
29929 else if (!strcmp ("tile", p))
29930 result = PRAGMA_OACC_CLAUSE_TILE;
29931 else if (!strcmp ("to", p))
29932 result = PRAGMA_OMP_CLAUSE_TO;
29935 if (!strcmp ("uniform", p))
29936 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29937 else if (!strcmp ("untied", p))
29938 result = PRAGMA_OMP_CLAUSE_UNTIED;
29939 else if (!strcmp ("use_device", p))
29940 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29941 else if (!strcmp ("use_device_ptr", p))
29942 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29945 if (!strcmp ("vector", p))
29946 result = PRAGMA_OACC_CLAUSE_VECTOR;
29947 else if (!strcmp ("vector_length", p))
29948 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29949 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29950 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29953 if (!strcmp ("wait", p))
29954 result = PRAGMA_OACC_CLAUSE_WAIT;
29955 else if (!strcmp ("worker", p))
29956 result = PRAGMA_OACC_CLAUSE_WORKER;
29961 if (result != PRAGMA_OMP_CLAUSE_NONE)
29962 cp_lexer_consume_token (parser->lexer);
29967 /* Validate that a clause of the given type does not already exist. */
29970 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29971 const char *name, location_t location)
29975 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29976 if (OMP_CLAUSE_CODE (c) == code)
29978 error_at (location, "too many %qs clauses", name);
29986 variable-list , identifier
29988 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29989 colon). An opening parenthesis will have been consumed by the caller.
29991 If KIND is nonzero, create the appropriate node and install the decl
29992 in OMP_CLAUSE_DECL and add the node to the head of the list.
29994 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29995 return the list created.
29997 COLON can be NULL if only closing parenthesis should end the list,
29998 or pointer to bool which will receive false if the list is terminated
29999 by closing parenthesis or true if the list is terminated by colon. */
30002 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30003 tree list, bool *colon)
30006 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30009 parser->colon_corrects_to_scope_p = false;
30016 token = cp_lexer_peek_token (parser->lexer);
30018 && current_class_ptr
30019 && cp_parser_is_keyword (token, RID_THIS))
30021 decl = finish_this_expr ();
30022 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30023 || CONVERT_EXPR_P (decl))
30024 decl = TREE_OPERAND (decl, 0);
30025 cp_lexer_consume_token (parser->lexer);
30029 name = cp_parser_id_expression (parser, /*template_p=*/false,
30030 /*check_dependency_p=*/true,
30031 /*template_p=*/NULL,
30032 /*declarator_p=*/false,
30033 /*optional_p=*/false);
30034 if (name == error_mark_node)
30037 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30038 if (decl == error_mark_node)
30039 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30042 if (decl == error_mark_node)
30044 else if (kind != 0)
30048 case OMP_CLAUSE__CACHE_:
30049 /* The OpenACC cache directive explicitly only allows "array
30050 elements or subarrays". */
30051 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30053 error_at (token->location, "expected %<[%>");
30054 decl = error_mark_node;
30058 case OMP_CLAUSE_MAP:
30059 case OMP_CLAUSE_FROM:
30060 case OMP_CLAUSE_TO:
30061 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30064 = cp_lexer_peek_token (parser->lexer)->location;
30065 cp_id_kind idk = CP_ID_KIND_NONE;
30066 cp_lexer_consume_token (parser->lexer);
30067 decl = convert_from_reference (decl);
30069 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30074 case OMP_CLAUSE_DEPEND:
30075 case OMP_CLAUSE_REDUCTION:
30076 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30078 tree low_bound = NULL_TREE, length = NULL_TREE;
30080 parser->colon_corrects_to_scope_p = false;
30081 cp_lexer_consume_token (parser->lexer);
30082 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30083 low_bound = cp_parser_expression (parser);
30085 parser->colon_corrects_to_scope_p
30086 = saved_colon_corrects_to_scope_p;
30087 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30088 length = integer_one_node;
30091 /* Look for `:'. */
30092 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30094 if (!cp_lexer_next_token_is (parser->lexer,
30096 length = cp_parser_expression (parser);
30098 /* Look for the closing `]'. */
30099 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30103 decl = tree_cons (low_bound, length, decl);
30110 tree u = build_omp_clause (token->location, kind);
30111 OMP_CLAUSE_DECL (u) = decl;
30112 OMP_CLAUSE_CHAIN (u) = list;
30116 list = tree_cons (decl, NULL_TREE, list);
30119 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30121 cp_lexer_consume_token (parser->lexer);
30125 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30127 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30130 cp_parser_require (parser, CPP_COLON, RT_COLON);
30134 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30138 /* Try to resync to an unnested comma. Copied from
30139 cp_parser_parenthesized_expression_list. */
30142 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30143 ending = cp_parser_skip_to_closing_parenthesis (parser,
30144 /*recovering=*/true,
30146 /*consume_paren=*/true);
30154 /* Similarly, but expect leading and trailing parenthesis. This is a very
30155 common case for omp clauses. */
30158 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30160 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30161 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30166 copy ( variable-list )
30167 copyin ( variable-list )
30168 copyout ( variable-list )
30169 create ( variable-list )
30170 delete ( variable-list )
30171 present ( variable-list )
30172 present_or_copy ( variable-list )
30173 pcopy ( variable-list )
30174 present_or_copyin ( variable-list )
30175 pcopyin ( variable-list )
30176 present_or_copyout ( variable-list )
30177 pcopyout ( variable-list )
30178 present_or_create ( variable-list )
30179 pcreate ( variable-list ) */
30182 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30185 enum gomp_map_kind kind;
30188 case PRAGMA_OACC_CLAUSE_COPY:
30189 kind = GOMP_MAP_FORCE_TOFROM;
30191 case PRAGMA_OACC_CLAUSE_COPYIN:
30192 kind = GOMP_MAP_FORCE_TO;
30194 case PRAGMA_OACC_CLAUSE_COPYOUT:
30195 kind = GOMP_MAP_FORCE_FROM;
30197 case PRAGMA_OACC_CLAUSE_CREATE:
30198 kind = GOMP_MAP_FORCE_ALLOC;
30200 case PRAGMA_OACC_CLAUSE_DELETE:
30201 kind = GOMP_MAP_DELETE;
30203 case PRAGMA_OACC_CLAUSE_DEVICE:
30204 kind = GOMP_MAP_FORCE_TO;
30206 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30207 kind = GOMP_MAP_DEVICE_RESIDENT;
30209 case PRAGMA_OACC_CLAUSE_HOST:
30210 case PRAGMA_OACC_CLAUSE_SELF:
30211 kind = GOMP_MAP_FORCE_FROM;
30213 case PRAGMA_OACC_CLAUSE_LINK:
30214 kind = GOMP_MAP_LINK;
30216 case PRAGMA_OACC_CLAUSE_PRESENT:
30217 kind = GOMP_MAP_FORCE_PRESENT;
30219 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30220 kind = GOMP_MAP_TOFROM;
30222 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30223 kind = GOMP_MAP_TO;
30225 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30226 kind = GOMP_MAP_FROM;
30228 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30229 kind = GOMP_MAP_ALLOC;
30232 gcc_unreachable ();
30235 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30237 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30238 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30244 deviceptr ( variable-list ) */
30247 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30249 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30252 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30253 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30254 variable-list must only allow for pointer variables. */
30255 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30256 for (t = vars; t; t = TREE_CHAIN (t))
30258 tree v = TREE_PURPOSE (t);
30259 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30260 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30261 OMP_CLAUSE_DECL (u) = v;
30262 OMP_CLAUSE_CHAIN (u) = list;
30276 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30277 enum omp_clause_code code,
30278 tree list, location_t location)
30280 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30281 tree c = build_omp_clause (location, code);
30282 OMP_CLAUSE_CHAIN (c) = list;
30287 num_gangs ( expression )
30288 num_workers ( expression )
30289 vector_length ( expression ) */
30292 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30293 const char *str, tree list)
30295 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30297 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30300 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30302 if (t == error_mark_node
30303 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30305 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30306 /*or_comma=*/false,
30307 /*consume_paren=*/true);
30311 check_no_duplicate_clause (list, code, str, loc);
30313 tree c = build_omp_clause (loc, code);
30314 OMP_CLAUSE_OPERAND (c, 0) = t;
30315 OMP_CLAUSE_CHAIN (c) = list;
30321 gang [( gang-arg-list )]
30322 worker [( [num:] int-expr )]
30323 vector [( [length:] int-expr )]
30325 where gang-arg is one of:
30330 and size-expr may be:
30337 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30338 const char *str, tree list)
30340 const char *id = "num";
30341 cp_lexer *lexer = parser->lexer;
30342 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30343 location_t loc = cp_lexer_peek_token (lexer)->location;
30345 if (kind == OMP_CLAUSE_VECTOR)
30348 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30350 cp_lexer_consume_token (lexer);
30354 cp_token *next = cp_lexer_peek_token (lexer);
30357 /* Gang static argument. */
30358 if (kind == OMP_CLAUSE_GANG
30359 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30361 cp_lexer_consume_token (lexer);
30363 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30364 goto cleanup_error;
30367 if (ops[idx] != NULL)
30369 cp_parser_error (parser, "too many %<static%> arguments");
30370 goto cleanup_error;
30373 /* Check for the '*' argument. */
30374 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30375 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30376 || cp_lexer_nth_token_is (parser->lexer, 2,
30379 cp_lexer_consume_token (lexer);
30380 ops[idx] = integer_minus_one_node;
30382 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30384 cp_lexer_consume_token (lexer);
30390 /* Worker num: argument and vector length: arguments. */
30391 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30392 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30393 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30395 cp_lexer_consume_token (lexer); /* id */
30396 cp_lexer_consume_token (lexer); /* ':' */
30399 /* Now collect the actual argument. */
30400 if (ops[idx] != NULL_TREE)
30402 cp_parser_error (parser, "unexpected argument");
30403 goto cleanup_error;
30406 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30408 if (expr == error_mark_node)
30409 goto cleanup_error;
30411 mark_exp_read (expr);
30414 if (kind == OMP_CLAUSE_GANG
30415 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30417 cp_lexer_consume_token (lexer);
30424 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30425 goto cleanup_error;
30428 check_no_duplicate_clause (list, kind, str, loc);
30430 c = build_omp_clause (loc, kind);
30433 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30435 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30436 OMP_CLAUSE_CHAIN (c) = list;
30441 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30446 tile ( size-expr-list ) */
30449 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30451 tree c, expr = error_mark_node;
30452 tree tile = NULL_TREE;
30454 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30456 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30461 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30462 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30463 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30465 cp_lexer_consume_token (parser->lexer);
30466 expr = integer_minus_one_node;
30469 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30471 if (expr == error_mark_node)
30474 tile = tree_cons (NULL_TREE, expr, tile);
30476 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30477 cp_lexer_consume_token (parser->lexer);
30479 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30481 /* Consume the trailing ')'. */
30482 cp_lexer_consume_token (parser->lexer);
30484 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30485 tile = nreverse (tile);
30486 OMP_CLAUSE_TILE_LIST (c) = tile;
30487 OMP_CLAUSE_CHAIN (c) = list;
30492 Parse wait clause or directive parameters. */
30495 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30497 vec<tree, va_gc> *args;
30500 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30502 /*allow_expansion_p=*/true,
30503 /*non_constant_p=*/NULL);
30505 if (args == NULL || args->length () == 0)
30507 cp_parser_error (parser, "expected integer expression before ')'");
30509 release_tree_vector (args);
30513 args_tree = build_tree_list_vec (args);
30515 release_tree_vector (args);
30517 for (t = args_tree; t; t = TREE_CHAIN (t))
30519 tree targ = TREE_VALUE (t);
30521 if (targ != error_mark_node)
30523 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30524 error ("%<wait%> expression must be integral");
30527 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30529 mark_rvalue_use (targ);
30530 OMP_CLAUSE_DECL (c) = targ;
30531 OMP_CLAUSE_CHAIN (c) = list;
30541 wait ( int-expr-list ) */
30544 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30546 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30548 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30551 list = cp_parser_oacc_wait_list (parser, location, list);
30557 collapse ( constant-expression ) */
30560 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30566 loc = cp_lexer_peek_token (parser->lexer)->location;
30567 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30570 num = cp_parser_constant_expression (parser);
30572 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30573 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30574 /*or_comma=*/false,
30575 /*consume_paren=*/true);
30577 if (num == error_mark_node)
30579 num = fold_non_dependent_expr (num);
30580 if (!tree_fits_shwi_p (num)
30581 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30582 || (n = tree_to_shwi (num)) <= 0
30585 error_at (loc, "collapse argument needs positive constant integer expression");
30589 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30590 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30591 OMP_CLAUSE_CHAIN (c) = list;
30592 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30598 default ( shared | none )
30604 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30605 location_t location, bool is_oacc)
30607 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30610 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30612 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30614 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30615 const char *p = IDENTIFIER_POINTER (id);
30620 if (strcmp ("none", p) != 0)
30622 kind = OMP_CLAUSE_DEFAULT_NONE;
30626 if (strcmp ("shared", p) != 0 || is_oacc)
30628 kind = OMP_CLAUSE_DEFAULT_SHARED;
30635 cp_lexer_consume_token (parser->lexer);
30641 cp_parser_error (parser, "expected %<none%>");
30643 cp_parser_error (parser, "expected %<none%> or %<shared%>");
30646 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30647 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30648 /*or_comma=*/false,
30649 /*consume_paren=*/true);
30651 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30654 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30655 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30656 OMP_CLAUSE_CHAIN (c) = list;
30657 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30663 final ( expression ) */
30666 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30670 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30673 t = cp_parser_condition (parser);
30675 if (t == error_mark_node
30676 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30677 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30678 /*or_comma=*/false,
30679 /*consume_paren=*/true);
30681 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30683 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30684 OMP_CLAUSE_FINAL_EXPR (c) = t;
30685 OMP_CLAUSE_CHAIN (c) = list;
30694 if ( directive-name-modifier : expression )
30696 directive-name-modifier:
30697 parallel | task | taskloop | target data | target | target update
30698 | target enter data | target exit data */
30701 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30705 enum tree_code if_modifier = ERROR_MARK;
30707 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30710 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30712 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30713 const char *p = IDENTIFIER_POINTER (id);
30716 if (strcmp ("parallel", p) == 0)
30717 if_modifier = OMP_PARALLEL;
30718 else if (strcmp ("task", p) == 0)
30719 if_modifier = OMP_TASK;
30720 else if (strcmp ("taskloop", p) == 0)
30721 if_modifier = OMP_TASKLOOP;
30722 else if (strcmp ("target", p) == 0)
30724 if_modifier = OMP_TARGET;
30725 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30727 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30728 p = IDENTIFIER_POINTER (id);
30729 if (strcmp ("data", p) == 0)
30730 if_modifier = OMP_TARGET_DATA;
30731 else if (strcmp ("update", p) == 0)
30732 if_modifier = OMP_TARGET_UPDATE;
30733 else if (strcmp ("enter", p) == 0)
30734 if_modifier = OMP_TARGET_ENTER_DATA;
30735 else if (strcmp ("exit", p) == 0)
30736 if_modifier = OMP_TARGET_EXIT_DATA;
30737 if (if_modifier != OMP_TARGET)
30742 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30743 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30745 if_modifier = ERROR_MARK;
30747 if (if_modifier == OMP_TARGET_ENTER_DATA
30748 || if_modifier == OMP_TARGET_EXIT_DATA)
30750 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30752 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30753 p = IDENTIFIER_POINTER (id);
30754 if (strcmp ("data", p) == 0)
30760 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30761 error_at (loc, "expected %<data%>");
30762 if_modifier = ERROR_MARK;
30767 if (if_modifier != ERROR_MARK)
30769 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30772 cp_lexer_consume_token (parser->lexer);
30779 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30780 error_at (loc, "expected %<:%>");
30782 if_modifier = ERROR_MARK;
30787 t = cp_parser_condition (parser);
30789 if (t == error_mark_node
30790 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30791 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30792 /*or_comma=*/false,
30793 /*consume_paren=*/true);
30795 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30796 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30798 if (if_modifier != ERROR_MARK
30799 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30801 const char *p = NULL;
30802 switch (if_modifier)
30804 case OMP_PARALLEL: p = "parallel"; break;
30805 case OMP_TASK: p = "task"; break;
30806 case OMP_TASKLOOP: p = "taskloop"; break;
30807 case OMP_TARGET_DATA: p = "target data"; break;
30808 case OMP_TARGET: p = "target"; break;
30809 case OMP_TARGET_UPDATE: p = "target update"; break;
30810 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30811 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30812 default: gcc_unreachable ();
30814 error_at (location, "too many %<if%> clauses with %qs modifier",
30818 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30821 error_at (location, "too many %<if%> clauses");
30823 error_at (location, "too many %<if%> clauses without modifier");
30826 else if (if_modifier == ERROR_MARK
30827 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30829 error_at (location, "if any %<if%> clause has modifier, then all "
30830 "%<if%> clauses have to use modifier");
30835 c = build_omp_clause (location, OMP_CLAUSE_IF);
30836 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30837 OMP_CLAUSE_IF_EXPR (c) = t;
30838 OMP_CLAUSE_CHAIN (c) = list;
30847 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30848 tree list, location_t location)
30852 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30855 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30856 OMP_CLAUSE_CHAIN (c) = list;
30864 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30865 tree list, location_t location)
30869 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30871 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30872 OMP_CLAUSE_CHAIN (c) = list;
30877 num_threads ( expression ) */
30880 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30881 location_t location)
30885 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30888 t = cp_parser_expression (parser);
30890 if (t == error_mark_node
30891 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30892 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30893 /*or_comma=*/false,
30894 /*consume_paren=*/true);
30896 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30897 "num_threads", location);
30899 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30900 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30901 OMP_CLAUSE_CHAIN (c) = list;
30907 num_tasks ( expression ) */
30910 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30911 location_t location)
30915 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30918 t = cp_parser_expression (parser);
30920 if (t == error_mark_node
30921 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30922 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30923 /*or_comma=*/false,
30924 /*consume_paren=*/true);
30926 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30927 "num_tasks", location);
30929 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30930 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30931 OMP_CLAUSE_CHAIN (c) = list;
30937 grainsize ( expression ) */
30940 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30941 location_t location)
30945 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30948 t = cp_parser_expression (parser);
30950 if (t == error_mark_node
30951 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30952 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30953 /*or_comma=*/false,
30954 /*consume_paren=*/true);
30956 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30957 "grainsize", location);
30959 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30960 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30961 OMP_CLAUSE_CHAIN (c) = list;
30967 priority ( expression ) */
30970 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30971 location_t location)
30975 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30978 t = cp_parser_expression (parser);
30980 if (t == error_mark_node
30981 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30982 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30983 /*or_comma=*/false,
30984 /*consume_paren=*/true);
30986 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30987 "priority", location);
30989 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30990 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30991 OMP_CLAUSE_CHAIN (c) = list;
30997 hint ( expression ) */
31000 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31001 location_t location)
31005 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31008 t = cp_parser_expression (parser);
31010 if (t == error_mark_node
31011 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31012 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31013 /*or_comma=*/false,
31014 /*consume_paren=*/true);
31016 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31018 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31019 OMP_CLAUSE_HINT_EXPR (c) = t;
31020 OMP_CLAUSE_CHAIN (c) = list;
31026 defaultmap ( tofrom : scalar ) */
31029 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31030 location_t location)
31035 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31038 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31040 cp_parser_error (parser, "expected %<tofrom%>");
31043 id = cp_lexer_peek_token (parser->lexer)->u.value;
31044 p = IDENTIFIER_POINTER (id);
31045 if (strcmp (p, "tofrom") != 0)
31047 cp_parser_error (parser, "expected %<tofrom%>");
31050 cp_lexer_consume_token (parser->lexer);
31051 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31054 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31056 cp_parser_error (parser, "expected %<scalar%>");
31059 id = cp_lexer_peek_token (parser->lexer)->u.value;
31060 p = IDENTIFIER_POINTER (id);
31061 if (strcmp (p, "scalar") != 0)
31063 cp_parser_error (parser, "expected %<scalar%>");
31066 cp_lexer_consume_token (parser->lexer);
31067 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31070 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31073 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31074 OMP_CLAUSE_CHAIN (c) = list;
31078 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31079 /*or_comma=*/false,
31080 /*consume_paren=*/true);
31088 ordered ( constant-expression ) */
31091 cp_parser_omp_clause_ordered (cp_parser *parser,
31092 tree list, location_t location)
31094 tree c, num = NULL_TREE;
31097 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31098 "ordered", location);
31100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31102 cp_lexer_consume_token (parser->lexer);
31104 num = cp_parser_constant_expression (parser);
31106 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31107 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31108 /*or_comma=*/false,
31109 /*consume_paren=*/true);
31111 if (num == error_mark_node)
31113 num = fold_non_dependent_expr (num);
31114 if (!tree_fits_shwi_p (num)
31115 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31116 || (n = tree_to_shwi (num)) <= 0
31119 error_at (location,
31120 "ordered argument needs positive constant integer "
31126 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31127 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31128 OMP_CLAUSE_CHAIN (c) = list;
31133 reduction ( reduction-operator : variable-list )
31135 reduction-operator:
31136 One of: + * - & ^ | && ||
31140 reduction-operator:
31141 One of: + * - & ^ | && || min max
31145 reduction-operator:
31146 One of: + * - & ^ | && ||
31150 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31152 enum tree_code code = ERROR_MARK;
31153 tree nlist, c, id = NULL_TREE;
31155 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31158 switch (cp_lexer_peek_token (parser->lexer)->type)
31160 case CPP_PLUS: code = PLUS_EXPR; break;
31161 case CPP_MULT: code = MULT_EXPR; break;
31162 case CPP_MINUS: code = MINUS_EXPR; break;
31163 case CPP_AND: code = BIT_AND_EXPR; break;
31164 case CPP_XOR: code = BIT_XOR_EXPR; break;
31165 case CPP_OR: code = BIT_IOR_EXPR; break;
31166 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31167 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31171 if (code != ERROR_MARK)
31172 cp_lexer_consume_token (parser->lexer);
31175 bool saved_colon_corrects_to_scope_p;
31176 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31177 parser->colon_corrects_to_scope_p = false;
31178 id = cp_parser_id_expression (parser, /*template_p=*/false,
31179 /*check_dependency_p=*/true,
31180 /*template_p=*/NULL,
31181 /*declarator_p=*/false,
31182 /*optional_p=*/false);
31183 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31184 if (identifier_p (id))
31186 const char *p = IDENTIFIER_POINTER (id);
31188 if (strcmp (p, "min") == 0)
31190 else if (strcmp (p, "max") == 0)
31192 else if (id == ansi_opname (PLUS_EXPR))
31194 else if (id == ansi_opname (MULT_EXPR))
31196 else if (id == ansi_opname (MINUS_EXPR))
31198 else if (id == ansi_opname (BIT_AND_EXPR))
31199 code = BIT_AND_EXPR;
31200 else if (id == ansi_opname (BIT_IOR_EXPR))
31201 code = BIT_IOR_EXPR;
31202 else if (id == ansi_opname (BIT_XOR_EXPR))
31203 code = BIT_XOR_EXPR;
31204 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31205 code = TRUTH_ANDIF_EXPR;
31206 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31207 code = TRUTH_ORIF_EXPR;
31208 id = omp_reduction_id (code, id, NULL_TREE);
31209 tree scope = parser->scope;
31211 id = build_qualified_name (NULL_TREE, scope, id, false);
31212 parser->scope = NULL_TREE;
31213 parser->qualifying_scope = NULL_TREE;
31214 parser->object_scope = NULL_TREE;
31218 error ("invalid reduction-identifier");
31220 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31221 /*or_comma=*/false,
31222 /*consume_paren=*/true);
31227 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31230 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31232 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31234 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31235 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31242 schedule ( schedule-kind )
31243 schedule ( schedule-kind , expression )
31246 static | dynamic | guided | runtime | auto
31249 schedule ( schedule-modifier : schedule-kind )
31250 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31258 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31261 int modifiers = 0, nmodifiers = 0;
31263 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31266 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31268 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31270 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31271 const char *p = IDENTIFIER_POINTER (id);
31272 if (strcmp ("simd", p) == 0)
31273 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31274 else if (strcmp ("monotonic", p) == 0)
31275 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31276 else if (strcmp ("nonmonotonic", p) == 0)
31277 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31280 cp_lexer_consume_token (parser->lexer);
31281 if (nmodifiers++ == 0
31282 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31283 cp_lexer_consume_token (parser->lexer);
31286 cp_parser_require (parser, CPP_COLON, RT_COLON);
31291 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31293 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31294 const char *p = IDENTIFIER_POINTER (id);
31299 if (strcmp ("dynamic", p) != 0)
31301 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31305 if (strcmp ("guided", p) != 0)
31307 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31311 if (strcmp ("runtime", p) != 0)
31313 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31320 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31321 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31322 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31323 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31326 cp_lexer_consume_token (parser->lexer);
31328 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31329 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31330 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31331 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31333 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31338 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31341 cp_lexer_consume_token (parser->lexer);
31343 token = cp_lexer_peek_token (parser->lexer);
31344 t = cp_parser_assignment_expression (parser);
31346 if (t == error_mark_node)
31348 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31349 error_at (token->location, "schedule %<runtime%> does not take "
31350 "a %<chunk_size%> parameter");
31351 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31352 error_at (token->location, "schedule %<auto%> does not take "
31353 "a %<chunk_size%> parameter");
31355 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31357 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31360 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31363 OMP_CLAUSE_SCHEDULE_KIND (c)
31364 = (enum omp_clause_schedule_kind)
31365 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31367 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31368 OMP_CLAUSE_CHAIN (c) = list;
31372 cp_parser_error (parser, "invalid schedule kind");
31374 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31375 /*or_comma=*/false,
31376 /*consume_paren=*/true);
31384 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31385 tree list, location_t location)
31389 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31391 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31392 OMP_CLAUSE_CHAIN (c) = list;
31401 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31402 tree list, location_t location)
31404 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31405 tree c = build_omp_clause (location, code);
31406 OMP_CLAUSE_CHAIN (c) = list;
31417 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31418 enum omp_clause_code code,
31419 tree list, location_t location)
31421 tree c = build_omp_clause (location, code);
31422 OMP_CLAUSE_CHAIN (c) = list;
31430 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31431 tree list, location_t location)
31433 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31434 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31435 OMP_CLAUSE_CHAIN (c) = list;
31444 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31445 enum omp_clause_code code,
31446 tree list, location_t location)
31448 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31449 tree c = build_omp_clause (location, code);
31450 OMP_CLAUSE_CHAIN (c) = list;
31455 num_teams ( expression ) */
31458 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31459 location_t location)
31463 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31466 t = cp_parser_expression (parser);
31468 if (t == error_mark_node
31469 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31470 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31471 /*or_comma=*/false,
31472 /*consume_paren=*/true);
31474 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31475 "num_teams", location);
31477 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31478 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31479 OMP_CLAUSE_CHAIN (c) = list;
31485 thread_limit ( expression ) */
31488 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31489 location_t location)
31493 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31496 t = cp_parser_expression (parser);
31498 if (t == error_mark_node
31499 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31500 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31501 /*or_comma=*/false,
31502 /*consume_paren=*/true);
31504 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31505 "thread_limit", location);
31507 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31508 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31509 OMP_CLAUSE_CHAIN (c) = list;
31515 aligned ( variable-list )
31516 aligned ( variable-list : constant-expression ) */
31519 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31521 tree nlist, c, alignment = NULL_TREE;
31524 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31527 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31532 alignment = cp_parser_constant_expression (parser);
31534 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31535 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31536 /*or_comma=*/false,
31537 /*consume_paren=*/true);
31539 if (alignment == error_mark_node)
31540 alignment = NULL_TREE;
31543 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31544 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31550 linear ( variable-list )
31551 linear ( variable-list : expression )
31554 linear ( modifier ( variable-list ) )
31555 linear ( modifier ( variable-list ) : expression ) */
31558 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31559 bool is_cilk_simd_fn, bool declare_simd)
31561 tree nlist, c, step = integer_one_node;
31563 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31565 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31568 if (!is_cilk_simd_fn
31569 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31571 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31572 const char *p = IDENTIFIER_POINTER (id);
31574 if (strcmp ("ref", p) == 0)
31575 kind = OMP_CLAUSE_LINEAR_REF;
31576 else if (strcmp ("val", p) == 0)
31577 kind = OMP_CLAUSE_LINEAR_VAL;
31578 else if (strcmp ("uval", p) == 0)
31579 kind = OMP_CLAUSE_LINEAR_UVAL;
31580 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31581 cp_lexer_consume_token (parser->lexer);
31583 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31586 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31587 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31591 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31592 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31594 cp_parser_require (parser, CPP_COLON, RT_COLON);
31595 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31596 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31597 /*or_comma=*/false,
31598 /*consume_paren=*/true);
31605 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31606 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31608 cp_token *token = cp_lexer_peek_token (parser->lexer);
31609 cp_parser_parse_tentatively (parser);
31610 step = cp_parser_id_expression (parser, /*template_p=*/false,
31611 /*check_dependency_p=*/true,
31612 /*template_p=*/NULL,
31613 /*declarator_p=*/false,
31614 /*optional_p=*/false);
31615 if (step != error_mark_node)
31616 step = cp_parser_lookup_name_simple (parser, step, token->location);
31617 if (step == error_mark_node)
31620 cp_parser_abort_tentative_parse (parser);
31622 else if (!cp_parser_parse_definitely (parser))
31626 step = cp_parser_expression (parser);
31628 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31630 sorry ("using parameters for %<linear%> step is not supported yet");
31631 step = integer_one_node;
31633 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31634 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31635 /*or_comma=*/false,
31636 /*consume_paren=*/true);
31638 if (step == error_mark_node)
31642 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31644 OMP_CLAUSE_LINEAR_STEP (c) = step;
31645 OMP_CLAUSE_LINEAR_KIND (c) = kind;
31652 safelen ( constant-expression ) */
31655 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31656 location_t location)
31660 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31663 t = cp_parser_constant_expression (parser);
31665 if (t == error_mark_node
31666 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31667 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31668 /*or_comma=*/false,
31669 /*consume_paren=*/true);
31671 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31673 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31674 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31675 OMP_CLAUSE_CHAIN (c) = list;
31681 simdlen ( constant-expression ) */
31684 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31685 location_t location)
31689 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31692 t = cp_parser_constant_expression (parser);
31694 if (t == error_mark_node
31695 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31696 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31697 /*or_comma=*/false,
31698 /*consume_paren=*/true);
31700 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31702 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31703 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31704 OMP_CLAUSE_CHAIN (c) = list;
31711 identifier [+/- integer]
31712 vec , identifier [+/- integer]
31716 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31723 cp_parser_error (parser, "expected identifier");
31727 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31729 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31730 tree t, identifier = cp_parser_identifier (parser);
31731 tree addend = NULL;
31733 if (identifier == error_mark_node)
31734 t = error_mark_node;
31737 t = cp_parser_lookup_name_simple
31738 (parser, identifier,
31739 cp_lexer_peek_token (parser->lexer)->location);
31740 if (t == error_mark_node)
31741 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31746 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31748 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31750 addend = integer_zero_node;
31751 goto add_to_vector;
31753 cp_lexer_consume_token (parser->lexer);
31755 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31757 cp_parser_error (parser, "expected integer");
31761 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31762 if (TREE_CODE (addend) != INTEGER_CST)
31764 cp_parser_error (parser, "expected integer");
31767 cp_lexer_consume_token (parser->lexer);
31770 if (t != error_mark_node)
31772 vec = tree_cons (addend, t, vec);
31774 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31777 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31780 cp_lexer_consume_token (parser->lexer);
31783 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31785 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31786 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31787 OMP_CLAUSE_DECL (u) = nreverse (vec);
31788 OMP_CLAUSE_CHAIN (u) = list;
31795 depend ( depend-kind : variable-list )
31803 depend ( sink : vec ) */
31806 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31809 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31811 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31816 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31817 const char *p = IDENTIFIER_POINTER (id);
31819 if (strcmp ("in", p) == 0)
31820 kind = OMP_CLAUSE_DEPEND_IN;
31821 else if (strcmp ("inout", p) == 0)
31822 kind = OMP_CLAUSE_DEPEND_INOUT;
31823 else if (strcmp ("out", p) == 0)
31824 kind = OMP_CLAUSE_DEPEND_OUT;
31825 else if (strcmp ("source", p) == 0)
31826 kind = OMP_CLAUSE_DEPEND_SOURCE;
31827 else if (strcmp ("sink", p) == 0)
31828 kind = OMP_CLAUSE_DEPEND_SINK;
31835 cp_lexer_consume_token (parser->lexer);
31837 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31839 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31840 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31841 OMP_CLAUSE_DECL (c) = NULL_TREE;
31842 OMP_CLAUSE_CHAIN (c) = list;
31843 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31844 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31845 /*or_comma=*/false,
31846 /*consume_paren=*/true);
31850 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31853 if (kind == OMP_CLAUSE_DEPEND_SINK)
31854 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31857 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31860 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31861 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31866 cp_parser_error (parser, "invalid depend kind");
31868 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31869 /*or_comma=*/false,
31870 /*consume_paren=*/true);
31875 map ( map-kind : variable-list )
31876 map ( variable-list )
31879 alloc | to | from | tofrom
31883 alloc | to | from | tofrom | release | delete
31885 map ( always [,] map-kind: variable-list ) */
31888 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31891 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31892 bool always = false;
31894 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31897 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31899 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31900 const char *p = IDENTIFIER_POINTER (id);
31902 if (strcmp ("always", p) == 0)
31905 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31907 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31908 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31910 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31914 cp_lexer_consume_token (parser->lexer);
31916 cp_lexer_consume_token (parser->lexer);
31921 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31922 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31924 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31925 const char *p = IDENTIFIER_POINTER (id);
31927 if (strcmp ("alloc", p) == 0)
31928 kind = GOMP_MAP_ALLOC;
31929 else if (strcmp ("to", p) == 0)
31930 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31931 else if (strcmp ("from", p) == 0)
31932 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31933 else if (strcmp ("tofrom", p) == 0)
31934 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31935 else if (strcmp ("release", p) == 0)
31936 kind = GOMP_MAP_RELEASE;
31939 cp_parser_error (parser, "invalid map kind");
31940 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31941 /*or_comma=*/false,
31942 /*consume_paren=*/true);
31945 cp_lexer_consume_token (parser->lexer);
31946 cp_lexer_consume_token (parser->lexer);
31948 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31949 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31951 kind = GOMP_MAP_DELETE;
31952 cp_lexer_consume_token (parser->lexer);
31953 cp_lexer_consume_token (parser->lexer);
31956 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31959 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31960 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31966 device ( expression ) */
31969 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31970 location_t location)
31974 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31977 t = cp_parser_expression (parser);
31979 if (t == error_mark_node
31980 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31981 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31982 /*or_comma=*/false,
31983 /*consume_paren=*/true);
31985 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31986 "device", location);
31988 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31989 OMP_CLAUSE_DEVICE_ID (c) = t;
31990 OMP_CLAUSE_CHAIN (c) = list;
31996 dist_schedule ( static )
31997 dist_schedule ( static , expression ) */
32000 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32001 location_t location)
32005 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32008 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32010 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32012 cp_lexer_consume_token (parser->lexer);
32014 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32016 cp_lexer_consume_token (parser->lexer);
32018 t = cp_parser_assignment_expression (parser);
32020 if (t == error_mark_node)
32022 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32024 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32027 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32030 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32032 OMP_CLAUSE_CHAIN (c) = list;
32036 cp_parser_error (parser, "invalid dist_schedule kind");
32038 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32039 /*or_comma=*/false,
32040 /*consume_paren=*/true);
32045 proc_bind ( proc-bind-kind )
32048 master | close | spread */
32051 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32052 location_t location)
32055 enum omp_clause_proc_bind_kind kind;
32057 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32060 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32062 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32063 const char *p = IDENTIFIER_POINTER (id);
32065 if (strcmp ("master", p) == 0)
32066 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32067 else if (strcmp ("close", p) == 0)
32068 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32069 else if (strcmp ("spread", p) == 0)
32070 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32077 cp_lexer_consume_token (parser->lexer);
32078 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32081 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32082 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32084 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32085 OMP_CLAUSE_CHAIN (c) = list;
32089 cp_parser_error (parser, "invalid depend kind");
32091 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32092 /*or_comma=*/false,
32093 /*consume_paren=*/true);
32098 async [( int-expr )] */
32101 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32104 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32106 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32108 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32110 cp_lexer_consume_token (parser->lexer);
32112 t = cp_parser_expression (parser);
32113 if (t == error_mark_node
32114 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32115 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32116 /*or_comma=*/false,
32117 /*consume_paren=*/true);
32120 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32122 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32123 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32124 OMP_CLAUSE_CHAIN (c) = list;
32130 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32131 is a bitmask in MASK. Return the list of clauses found. */
32134 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32135 const char *where, cp_token *pragma_tok,
32136 bool finish_p = true)
32138 tree clauses = NULL;
32141 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32144 pragma_omp_clause c_kind;
32145 omp_clause_code code;
32146 const char *c_name;
32147 tree prev = clauses;
32149 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32150 cp_lexer_consume_token (parser->lexer);
32152 here = cp_lexer_peek_token (parser->lexer)->location;
32153 c_kind = cp_parser_omp_clause_name (parser);
32157 case PRAGMA_OACC_CLAUSE_ASYNC:
32158 clauses = cp_parser_oacc_clause_async (parser, clauses);
32161 case PRAGMA_OACC_CLAUSE_AUTO:
32162 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32166 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32167 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32168 c_name = "collapse";
32170 case PRAGMA_OACC_CLAUSE_COPY:
32171 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32174 case PRAGMA_OACC_CLAUSE_COPYIN:
32175 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32178 case PRAGMA_OACC_CLAUSE_COPYOUT:
32179 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32180 c_name = "copyout";
32182 case PRAGMA_OACC_CLAUSE_CREATE:
32183 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32186 case PRAGMA_OACC_CLAUSE_DELETE:
32187 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32190 case PRAGMA_OMP_CLAUSE_DEFAULT:
32191 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32192 c_name = "default";
32194 case PRAGMA_OACC_CLAUSE_DEVICE:
32195 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32198 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32199 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32200 c_name = "deviceptr";
32202 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32203 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32204 c_name = "device_resident";
32206 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32207 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32209 c_name = "firstprivate";
32211 case PRAGMA_OACC_CLAUSE_GANG:
32213 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32216 case PRAGMA_OACC_CLAUSE_HOST:
32217 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32220 case PRAGMA_OACC_CLAUSE_IF:
32221 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32224 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32225 clauses = cp_parser_oacc_simple_clause (parser,
32226 OMP_CLAUSE_INDEPENDENT,
32228 c_name = "independent";
32230 case PRAGMA_OACC_CLAUSE_LINK:
32231 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32234 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32235 code = OMP_CLAUSE_NUM_GANGS;
32236 c_name = "num_gangs";
32237 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32240 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32241 c_name = "num_workers";
32242 code = OMP_CLAUSE_NUM_WORKERS;
32243 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32246 case PRAGMA_OACC_CLAUSE_PRESENT:
32247 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32248 c_name = "present";
32250 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32251 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32252 c_name = "present_or_copy";
32254 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32255 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32256 c_name = "present_or_copyin";
32258 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32259 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32260 c_name = "present_or_copyout";
32262 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32263 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32264 c_name = "present_or_create";
32266 case PRAGMA_OACC_CLAUSE_PRIVATE:
32267 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32269 c_name = "private";
32271 case PRAGMA_OACC_CLAUSE_REDUCTION:
32272 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32273 c_name = "reduction";
32275 case PRAGMA_OACC_CLAUSE_SELF:
32276 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32279 case PRAGMA_OACC_CLAUSE_SEQ:
32280 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32284 case PRAGMA_OACC_CLAUSE_TILE:
32285 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32288 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32289 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32291 c_name = "use_device";
32293 case PRAGMA_OACC_CLAUSE_VECTOR:
32295 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32298 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32299 c_name = "vector_length";
32300 code = OMP_CLAUSE_VECTOR_LENGTH;
32301 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32304 case PRAGMA_OACC_CLAUSE_WAIT:
32305 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32308 case PRAGMA_OACC_CLAUSE_WORKER:
32310 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32314 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32320 if (((mask >> c_kind) & 1) == 0)
32322 /* Remove the invalid clause(s) from the list to avoid
32323 confusing the rest of the compiler. */
32325 error_at (here, "%qs is not valid for %qs", c_name, where);
32330 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32333 return finish_omp_clauses (clauses, false);
32338 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32339 is a bitmask in MASK. Return the list of clauses found; the result
32340 of clause default goes in *pdefault. */
32343 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32344 const char *where, cp_token *pragma_tok,
32345 bool finish_p = true)
32347 tree clauses = NULL;
32349 cp_token *token = NULL;
32351 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32353 pragma_omp_clause c_kind;
32354 const char *c_name;
32355 tree prev = clauses;
32357 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32358 cp_lexer_consume_token (parser->lexer);
32360 token = cp_lexer_peek_token (parser->lexer);
32361 c_kind = cp_parser_omp_clause_name (parser);
32365 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32366 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32368 c_name = "collapse";
32370 case PRAGMA_OMP_CLAUSE_COPYIN:
32371 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32374 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32375 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32377 c_name = "copyprivate";
32379 case PRAGMA_OMP_CLAUSE_DEFAULT:
32380 clauses = cp_parser_omp_clause_default (parser, clauses,
32381 token->location, false);
32382 c_name = "default";
32384 case PRAGMA_OMP_CLAUSE_FINAL:
32385 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32388 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32389 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32391 c_name = "firstprivate";
32393 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32394 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32396 c_name = "grainsize";
32398 case PRAGMA_OMP_CLAUSE_HINT:
32399 clauses = cp_parser_omp_clause_hint (parser, clauses,
32403 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32404 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32406 c_name = "defaultmap";
32408 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32409 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32411 c_name = "use_device_ptr";
32413 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32414 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32416 c_name = "is_device_ptr";
32418 case PRAGMA_OMP_CLAUSE_IF:
32419 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32423 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32424 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32426 c_name = "lastprivate";
32428 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32429 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32431 c_name = "mergeable";
32433 case PRAGMA_OMP_CLAUSE_NOWAIT:
32434 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32437 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32438 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32440 c_name = "num_tasks";
32442 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32443 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32445 c_name = "num_threads";
32447 case PRAGMA_OMP_CLAUSE_ORDERED:
32448 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32450 c_name = "ordered";
32452 case PRAGMA_OMP_CLAUSE_PRIORITY:
32453 clauses = cp_parser_omp_clause_priority (parser, clauses,
32455 c_name = "priority";
32457 case PRAGMA_OMP_CLAUSE_PRIVATE:
32458 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32460 c_name = "private";
32462 case PRAGMA_OMP_CLAUSE_REDUCTION:
32463 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32464 c_name = "reduction";
32466 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32467 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32469 c_name = "schedule";
32471 case PRAGMA_OMP_CLAUSE_SHARED:
32472 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32476 case PRAGMA_OMP_CLAUSE_UNTIED:
32477 clauses = cp_parser_omp_clause_untied (parser, clauses,
32481 case PRAGMA_OMP_CLAUSE_INBRANCH:
32482 case PRAGMA_CILK_CLAUSE_MASK:
32483 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32484 clauses, token->location);
32485 c_name = "inbranch";
32487 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32488 case PRAGMA_CILK_CLAUSE_NOMASK:
32489 clauses = cp_parser_omp_clause_branch (parser,
32490 OMP_CLAUSE_NOTINBRANCH,
32491 clauses, token->location);
32492 c_name = "notinbranch";
32494 case PRAGMA_OMP_CLAUSE_PARALLEL:
32495 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32496 clauses, token->location);
32497 c_name = "parallel";
32501 error_at (token->location, "%qs must be the first clause of %qs",
32506 case PRAGMA_OMP_CLAUSE_FOR:
32507 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32508 clauses, token->location);
32511 goto clause_not_first;
32513 case PRAGMA_OMP_CLAUSE_SECTIONS:
32514 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32515 clauses, token->location);
32516 c_name = "sections";
32518 goto clause_not_first;
32520 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32521 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32522 clauses, token->location);
32523 c_name = "taskgroup";
32525 goto clause_not_first;
32527 case PRAGMA_OMP_CLAUSE_LINK:
32528 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32531 case PRAGMA_OMP_CLAUSE_TO:
32532 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32533 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32536 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32539 case PRAGMA_OMP_CLAUSE_FROM:
32540 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32543 case PRAGMA_OMP_CLAUSE_UNIFORM:
32544 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32546 c_name = "uniform";
32548 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32549 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32551 c_name = "num_teams";
32553 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32554 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32556 c_name = "thread_limit";
32558 case PRAGMA_OMP_CLAUSE_ALIGNED:
32559 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32560 c_name = "aligned";
32562 case PRAGMA_OMP_CLAUSE_LINEAR:
32564 bool cilk_simd_fn = false, declare_simd = false;
32565 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32566 cilk_simd_fn = true;
32567 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32568 declare_simd = true;
32569 clauses = cp_parser_omp_clause_linear (parser, clauses,
32570 cilk_simd_fn, declare_simd);
32574 case PRAGMA_OMP_CLAUSE_DEPEND:
32575 clauses = cp_parser_omp_clause_depend (parser, clauses,
32579 case PRAGMA_OMP_CLAUSE_MAP:
32580 clauses = cp_parser_omp_clause_map (parser, clauses);
32583 case PRAGMA_OMP_CLAUSE_DEVICE:
32584 clauses = cp_parser_omp_clause_device (parser, clauses,
32588 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32589 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32591 c_name = "dist_schedule";
32593 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32594 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32596 c_name = "proc_bind";
32598 case PRAGMA_OMP_CLAUSE_SAFELEN:
32599 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32601 c_name = "safelen";
32603 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32604 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32606 c_name = "simdlen";
32608 case PRAGMA_OMP_CLAUSE_NOGROUP:
32609 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32611 c_name = "nogroup";
32613 case PRAGMA_OMP_CLAUSE_THREADS:
32615 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32616 clauses, token->location);
32617 c_name = "threads";
32619 case PRAGMA_OMP_CLAUSE_SIMD:
32621 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32622 clauses, token->location);
32625 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32626 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32627 c_name = "simdlen";
32630 cp_parser_error (parser, "expected %<#pragma omp%> clause");
32636 if (((mask >> c_kind) & 1) == 0)
32638 /* Remove the invalid clause(s) from the list to avoid
32639 confusing the rest of the compiler. */
32641 error_at (token->location, "%qs is not valid for %qs", c_name, where);
32645 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32646 no reason to skip to the end. */
32647 if (!(flag_cilkplus && pragma_tok == NULL))
32648 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32651 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32652 return finish_omp_clauses (clauses, false, true);
32654 return finish_omp_clauses (clauses, true);
32663 In practice, we're also interested in adding the statement to an
32664 outer node. So it is convenient if we work around the fact that
32665 cp_parser_statement calls add_stmt. */
32668 cp_parser_begin_omp_structured_block (cp_parser *parser)
32670 unsigned save = parser->in_statement;
32672 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32673 This preserves the "not within loop or switch" style error messages
32674 for nonsense cases like
32680 if (parser->in_statement)
32681 parser->in_statement = IN_OMP_BLOCK;
32687 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32689 parser->in_statement = save;
32693 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
32695 tree stmt = begin_omp_structured_block ();
32696 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32698 cp_parser_statement (parser, NULL_TREE, false, if_p);
32700 cp_parser_end_omp_structured_block (parser, save);
32701 return finish_omp_structured_block (stmt);
32705 # pragma omp atomic new-line
32709 x binop= expr | x++ | ++x | x-- | --x
32711 +, *, -, /, &, ^, |, <<, >>
32713 where x is an lvalue expression with scalar type.
32716 # pragma omp atomic new-line
32719 # pragma omp atomic read new-line
32722 # pragma omp atomic write new-line
32725 # pragma omp atomic update new-line
32728 # pragma omp atomic capture new-line
32731 # pragma omp atomic capture new-line
32739 expression-stmt | x = x binop expr
32741 v = expression-stmt
32743 { v = x; update-stmt; } | { update-stmt; v = x; }
32747 expression-stmt | x = x binop expr | x = expr binop x
32751 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32753 where x and v are lvalue expressions with scalar type. */
32756 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32758 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32759 tree rhs1 = NULL_TREE, orig_lhs;
32760 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32761 bool structured_block = false;
32762 bool seq_cst = false;
32764 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32766 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32767 const char *p = IDENTIFIER_POINTER (id);
32769 if (!strcmp (p, "seq_cst"))
32772 cp_lexer_consume_token (parser->lexer);
32773 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32774 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32775 cp_lexer_consume_token (parser->lexer);
32778 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32780 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32781 const char *p = IDENTIFIER_POINTER (id);
32783 if (!strcmp (p, "read"))
32784 code = OMP_ATOMIC_READ;
32785 else if (!strcmp (p, "write"))
32787 else if (!strcmp (p, "update"))
32789 else if (!strcmp (p, "capture"))
32790 code = OMP_ATOMIC_CAPTURE_NEW;
32794 cp_lexer_consume_token (parser->lexer);
32798 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32799 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32800 cp_lexer_consume_token (parser->lexer);
32802 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32804 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32805 const char *p = IDENTIFIER_POINTER (id);
32807 if (!strcmp (p, "seq_cst"))
32810 cp_lexer_consume_token (parser->lexer);
32814 cp_parser_require_pragma_eol (parser, pragma_tok);
32818 case OMP_ATOMIC_READ:
32819 case NOP_EXPR: /* atomic write */
32820 v = cp_parser_unary_expression (parser);
32821 if (v == error_mark_node)
32823 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32825 if (code == NOP_EXPR)
32826 lhs = cp_parser_expression (parser);
32828 lhs = cp_parser_unary_expression (parser);
32829 if (lhs == error_mark_node)
32831 if (code == NOP_EXPR)
32833 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32841 case OMP_ATOMIC_CAPTURE_NEW:
32842 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32844 cp_lexer_consume_token (parser->lexer);
32845 structured_block = true;
32849 v = cp_parser_unary_expression (parser);
32850 if (v == error_mark_node)
32852 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32860 lhs = cp_parser_unary_expression (parser);
32862 switch (TREE_CODE (lhs))
32867 case POSTINCREMENT_EXPR:
32868 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32869 code = OMP_ATOMIC_CAPTURE_OLD;
32871 case PREINCREMENT_EXPR:
32872 lhs = TREE_OPERAND (lhs, 0);
32873 opcode = PLUS_EXPR;
32874 rhs = integer_one_node;
32877 case POSTDECREMENT_EXPR:
32878 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32879 code = OMP_ATOMIC_CAPTURE_OLD;
32881 case PREDECREMENT_EXPR:
32882 lhs = TREE_OPERAND (lhs, 0);
32883 opcode = MINUS_EXPR;
32884 rhs = integer_one_node;
32887 case COMPOUND_EXPR:
32888 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32889 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32890 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32891 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32892 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32893 (TREE_OPERAND (lhs, 1), 0), 0)))
32895 /* Undo effects of boolean_increment for post {in,de}crement. */
32896 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32899 if (TREE_CODE (lhs) == MODIFY_EXPR
32900 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32902 /* Undo effects of boolean_increment. */
32903 if (integer_onep (TREE_OPERAND (lhs, 1)))
32905 /* This is pre or post increment. */
32906 rhs = TREE_OPERAND (lhs, 1);
32907 lhs = TREE_OPERAND (lhs, 0);
32909 if (code == OMP_ATOMIC_CAPTURE_NEW
32910 && !structured_block
32911 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32912 code = OMP_ATOMIC_CAPTURE_OLD;
32918 switch (cp_lexer_peek_token (parser->lexer)->type)
32921 opcode = MULT_EXPR;
32924 opcode = TRUNC_DIV_EXPR;
32927 opcode = PLUS_EXPR;
32930 opcode = MINUS_EXPR;
32932 case CPP_LSHIFT_EQ:
32933 opcode = LSHIFT_EXPR;
32935 case CPP_RSHIFT_EQ:
32936 opcode = RSHIFT_EXPR;
32939 opcode = BIT_AND_EXPR;
32942 opcode = BIT_IOR_EXPR;
32945 opcode = BIT_XOR_EXPR;
32948 enum cp_parser_prec oprec;
32950 cp_lexer_consume_token (parser->lexer);
32951 cp_parser_parse_tentatively (parser);
32952 rhs1 = cp_parser_simple_cast_expression (parser);
32953 if (rhs1 == error_mark_node)
32955 cp_parser_abort_tentative_parse (parser);
32956 cp_parser_simple_cast_expression (parser);
32959 token = cp_lexer_peek_token (parser->lexer);
32960 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32962 cp_parser_abort_tentative_parse (parser);
32963 cp_parser_parse_tentatively (parser);
32964 rhs = cp_parser_binary_expression (parser, false, true,
32965 PREC_NOT_OPERATOR, NULL);
32966 if (rhs == error_mark_node)
32968 cp_parser_abort_tentative_parse (parser);
32969 cp_parser_binary_expression (parser, false, true,
32970 PREC_NOT_OPERATOR, NULL);
32973 switch (TREE_CODE (rhs))
32976 case TRUNC_DIV_EXPR:
32985 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32987 if (cp_parser_parse_definitely (parser))
32989 opcode = TREE_CODE (rhs);
32990 rhs1 = TREE_OPERAND (rhs, 0);
32991 rhs = TREE_OPERAND (rhs, 1);
33001 cp_parser_abort_tentative_parse (parser);
33002 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33004 rhs = cp_parser_expression (parser);
33005 if (rhs == error_mark_node)
33011 cp_parser_error (parser,
33012 "invalid form of %<#pragma omp atomic%>");
33015 if (!cp_parser_parse_definitely (parser))
33017 switch (token->type)
33019 case CPP_SEMICOLON:
33020 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33022 code = OMP_ATOMIC_CAPTURE_OLD;
33027 cp_lexer_consume_token (parser->lexer);
33030 else if (structured_block)
33037 cp_parser_error (parser,
33038 "invalid form of %<#pragma omp atomic%>");
33041 opcode = MULT_EXPR;
33044 opcode = TRUNC_DIV_EXPR;
33047 opcode = PLUS_EXPR;
33050 opcode = MINUS_EXPR;
33053 opcode = LSHIFT_EXPR;
33056 opcode = RSHIFT_EXPR;
33059 opcode = BIT_AND_EXPR;
33062 opcode = BIT_IOR_EXPR;
33065 opcode = BIT_XOR_EXPR;
33068 cp_parser_error (parser,
33069 "invalid operator for %<#pragma omp atomic%>");
33072 oprec = TOKEN_PRECEDENCE (token);
33073 gcc_assert (oprec != PREC_NOT_OPERATOR);
33074 if (commutative_tree_code (opcode))
33075 oprec = (enum cp_parser_prec) (oprec - 1);
33076 cp_lexer_consume_token (parser->lexer);
33077 rhs = cp_parser_binary_expression (parser, false, false,
33079 if (rhs == error_mark_node)
33084 cp_parser_error (parser,
33085 "invalid operator for %<#pragma omp atomic%>");
33088 cp_lexer_consume_token (parser->lexer);
33090 rhs = cp_parser_expression (parser);
33091 if (rhs == error_mark_node)
33096 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33098 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33100 v = cp_parser_unary_expression (parser);
33101 if (v == error_mark_node)
33103 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33105 lhs1 = cp_parser_unary_expression (parser);
33106 if (lhs1 == error_mark_node)
33109 if (structured_block)
33111 cp_parser_consume_semicolon_at_end_of_statement (parser);
33112 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33115 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33116 if (!structured_block)
33117 cp_parser_consume_semicolon_at_end_of_statement (parser);
33121 cp_parser_skip_to_end_of_block_or_statement (parser);
33122 if (structured_block)
33124 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33125 cp_lexer_consume_token (parser->lexer);
33126 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33128 cp_parser_skip_to_end_of_block_or_statement (parser);
33129 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33130 cp_lexer_consume_token (parser->lexer);
33137 # pragma omp barrier new-line */
33140 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33142 cp_parser_require_pragma_eol (parser, pragma_tok);
33143 finish_omp_barrier ();
33147 # pragma omp critical [(name)] new-line
33151 # pragma omp critical [(name) [hint(expression)]] new-line
33152 structured-block */
33154 #define OMP_CRITICAL_CLAUSE_MASK \
33155 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33158 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33160 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33162 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33164 cp_lexer_consume_token (parser->lexer);
33166 name = cp_parser_identifier (parser);
33168 if (name == error_mark_node
33169 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33170 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33171 /*or_comma=*/false,
33172 /*consume_paren=*/true);
33173 if (name == error_mark_node)
33176 clauses = cp_parser_omp_all_clauses (parser,
33177 OMP_CRITICAL_CLAUSE_MASK,
33178 "#pragma omp critical", pragma_tok);
33181 cp_parser_require_pragma_eol (parser, pragma_tok);
33183 stmt = cp_parser_omp_structured_block (parser, if_p);
33184 return c_finish_omp_critical (input_location, stmt, name, clauses);
33188 # pragma omp flush flush-vars[opt] new-line
33191 ( variable-list ) */
33194 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33197 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33198 cp_parser_require_pragma_eol (parser, pragma_tok);
33200 finish_omp_flush ();
33203 /* Helper function, to parse omp for increment expression. */
33206 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33208 tree cond = cp_parser_binary_expression (parser, false, true,
33209 PREC_NOT_OPERATOR, NULL);
33210 if (cond == error_mark_node
33211 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33213 cp_parser_skip_to_end_of_statement (parser);
33214 return error_mark_node;
33217 switch (TREE_CODE (cond))
33225 if (code == CILK_SIMD || code == CILK_FOR)
33227 /* Fall through: OpenMP disallows NE_EXPR. */
33229 return error_mark_node;
33232 /* If decl is an iterator, preserve LHS and RHS of the relational
33233 expr until finish_omp_for. */
33235 && (type_dependent_expression_p (decl)
33236 || CLASS_TYPE_P (TREE_TYPE (decl))))
33239 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33241 TREE_OPERAND (cond, 0), ERROR_MARK,
33242 TREE_OPERAND (cond, 1), ERROR_MARK,
33243 /*overload=*/NULL, tf_warning_or_error);
33246 /* Helper function, to parse omp for increment expression. */
33249 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33251 cp_token *token = cp_lexer_peek_token (parser->lexer);
33257 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33259 op = (token->type == CPP_PLUS_PLUS
33260 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33261 cp_lexer_consume_token (parser->lexer);
33262 lhs = cp_parser_simple_cast_expression (parser);
33264 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33265 return error_mark_node;
33266 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33269 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33271 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33272 return error_mark_node;
33274 token = cp_lexer_peek_token (parser->lexer);
33275 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33277 op = (token->type == CPP_PLUS_PLUS
33278 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33279 cp_lexer_consume_token (parser->lexer);
33280 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33283 op = cp_parser_assignment_operator_opt (parser);
33284 if (op == ERROR_MARK)
33285 return error_mark_node;
33287 if (op != NOP_EXPR)
33289 rhs = cp_parser_assignment_expression (parser);
33290 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33291 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33294 lhs = cp_parser_binary_expression (parser, false, false,
33295 PREC_ADDITIVE_EXPRESSION, NULL);
33296 token = cp_lexer_peek_token (parser->lexer);
33297 decl_first = (lhs == decl
33298 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33301 if (token->type != CPP_PLUS
33302 && token->type != CPP_MINUS)
33303 return error_mark_node;
33307 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33308 cp_lexer_consume_token (parser->lexer);
33309 rhs = cp_parser_binary_expression (parser, false, false,
33310 PREC_ADDITIVE_EXPRESSION, NULL);
33311 token = cp_lexer_peek_token (parser->lexer);
33312 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33314 if (lhs == NULL_TREE)
33316 if (op == PLUS_EXPR)
33319 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33320 tf_warning_or_error);
33323 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33324 ERROR_MARK, NULL, tf_warning_or_error);
33327 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33332 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33333 || op == MINUS_EXPR)
33334 return error_mark_node;
33335 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33338 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33340 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33343 /* Parse the initialization statement of either an OpenMP for loop or
33344 a Cilk Plus for loop.
33346 Return true if the resulting construct should have an
33347 OMP_CLAUSE_PRIVATE added to it. */
33350 cp_parser_omp_for_loop_init (cp_parser *parser,
33351 enum tree_code code,
33352 tree &this_pre_body,
33353 vec<tree, va_gc> *for_block,
33359 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33362 tree add_private_clause = NULL_TREE;
33364 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33368 integer-type var = lb
33369 random-access-iterator-type var = lb
33370 pointer-type var = lb
33372 cp_decl_specifier_seq type_specifiers;
33374 /* First, try to parse as an initialized declaration. See
33375 cp_parser_condition, from whence the bulk of this is copied. */
33377 cp_parser_parse_tentatively (parser);
33378 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33379 /*is_trailing_return=*/false,
33381 if (cp_parser_parse_definitely (parser))
33383 /* If parsing a type specifier seq succeeded, then this
33384 MUST be a initialized declaration. */
33385 tree asm_specification, attributes;
33386 cp_declarator *declarator;
33388 declarator = cp_parser_declarator (parser,
33389 CP_PARSER_DECLARATOR_NAMED,
33390 /*ctor_dtor_or_conv_p=*/NULL,
33391 /*parenthesized_p=*/NULL,
33392 /*member_p=*/false,
33393 /*friend_p=*/false);
33394 attributes = cp_parser_attributes_opt (parser);
33395 asm_specification = cp_parser_asm_specification_opt (parser);
33397 if (declarator == cp_error_declarator)
33398 cp_parser_skip_to_end_of_statement (parser);
33402 tree pushed_scope, auto_node;
33404 decl = start_decl (declarator, &type_specifiers,
33405 SD_INITIALIZED, attributes,
33406 /*prefix_attributes=*/NULL_TREE,
33409 auto_node = type_uses_auto (TREE_TYPE (decl));
33410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33412 if (cp_lexer_next_token_is (parser->lexer,
33415 if (code != CILK_SIMD && code != CILK_FOR)
33416 error ("parenthesized initialization is not allowed in "
33417 "OpenMP %<for%> loop");
33419 error ("parenthesized initialization is "
33420 "not allowed in for-loop");
33423 /* Trigger an error. */
33424 cp_parser_require (parser, CPP_EQ, RT_EQ);
33426 init = error_mark_node;
33427 cp_parser_skip_to_end_of_statement (parser);
33429 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33430 || type_dependent_expression_p (decl)
33433 bool is_direct_init, is_non_constant_init;
33435 init = cp_parser_initializer (parser,
33437 &is_non_constant_init);
33442 = do_auto_deduction (TREE_TYPE (decl), init,
33445 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33446 && !type_dependent_expression_p (decl))
33450 cp_finish_decl (decl, init, !is_non_constant_init,
33452 LOOKUP_ONLYCONVERTING);
33454 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33456 vec_safe_push (for_block, this_pre_body);
33460 init = pop_stmt_list (this_pre_body);
33461 this_pre_body = NULL_TREE;
33466 cp_lexer_consume_token (parser->lexer);
33467 init = cp_parser_assignment_expression (parser);
33470 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33471 init = error_mark_node;
33473 cp_finish_decl (decl, NULL_TREE,
33474 /*init_const_expr_p=*/false,
33476 LOOKUP_ONLYCONVERTING);
33480 pop_scope (pushed_scope);
33486 /* If parsing a type specifier sequence failed, then
33487 this MUST be a simple expression. */
33488 if (code == CILK_FOR)
33489 error ("%<_Cilk_for%> allows expression instead of declaration only "
33490 "in C, not in C++");
33491 cp_parser_parse_tentatively (parser);
33492 decl = cp_parser_primary_expression (parser, false, false,
33494 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33495 if (!cp_parser_error_occurred (parser)
33497 && (TREE_CODE (decl) == COMPONENT_REF
33498 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33500 cp_parser_abort_tentative_parse (parser);
33501 cp_parser_parse_tentatively (parser);
33502 cp_token *token = cp_lexer_peek_token (parser->lexer);
33503 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33504 /*check_dependency_p=*/true,
33505 /*template_p=*/NULL,
33506 /*declarator_p=*/false,
33507 /*optional_p=*/false);
33508 if (name != error_mark_node
33509 && last_tok == cp_lexer_peek_token (parser->lexer))
33511 decl = cp_parser_lookup_name_simple (parser, name,
33513 if (TREE_CODE (decl) == FIELD_DECL)
33514 add_private_clause = omp_privatize_field (decl, false);
33516 cp_parser_abort_tentative_parse (parser);
33517 cp_parser_parse_tentatively (parser);
33518 decl = cp_parser_primary_expression (parser, false, false,
33521 if (!cp_parser_error_occurred (parser)
33524 && CLASS_TYPE_P (TREE_TYPE (decl)))
33528 cp_parser_parse_definitely (parser);
33529 cp_parser_require (parser, CPP_EQ, RT_EQ);
33530 rhs = cp_parser_assignment_expression (parser);
33532 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33535 tf_warning_or_error));
33536 if (!add_private_clause)
33537 add_private_clause = decl;
33542 cp_parser_abort_tentative_parse (parser);
33543 init = cp_parser_expression (parser);
33546 if (TREE_CODE (init) == MODIFY_EXPR
33547 || TREE_CODE (init) == MODOP_EXPR)
33548 real_decl = TREE_OPERAND (init, 0);
33552 return add_private_clause;
33555 /* Parse the restricted form of the for statement allowed by OpenMP. */
33558 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33559 tree *cclauses, bool *if_p)
33561 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33562 tree real_decl, initv, condv, incrv, declv;
33563 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33564 location_t loc_first;
33565 bool collapse_err = false;
33566 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33567 vec<tree, va_gc> *for_block = make_tree_vector ();
33568 auto_vec<tree, 4> orig_inits;
33570 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33571 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33572 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33573 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33574 && OMP_CLAUSE_ORDERED_EXPR (cl))
33577 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33580 if (ordered && ordered < collapse)
33582 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33583 "%<ordered%> clause parameter is less than %<collapse%>");
33584 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33585 = build_int_cst (NULL_TREE, collapse);
33586 ordered = collapse;
33590 for (tree *pc = &clauses; *pc; )
33591 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33593 error_at (OMP_CLAUSE_LOCATION (*pc),
33594 "%<linear%> clause may not be specified together "
33595 "with %<ordered%> clause with a parameter");
33596 *pc = OMP_CLAUSE_CHAIN (*pc);
33599 pc = &OMP_CLAUSE_CHAIN (*pc);
33602 gcc_assert (collapse >= 1 && ordered >= 0);
33603 count = ordered ? ordered : collapse;
33605 declv = make_tree_vec (count);
33606 initv = make_tree_vec (count);
33607 condv = make_tree_vec (count);
33608 incrv = make_tree_vec (count);
33610 loc_first = cp_lexer_peek_token (parser->lexer)->location;
33612 for (i = 0; i < count; i++)
33614 int bracecount = 0;
33615 tree add_private_clause = NULL_TREE;
33618 if (code != CILK_FOR
33619 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33621 cp_parser_error (parser, "for statement expected");
33624 if (code == CILK_FOR
33625 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33627 cp_parser_error (parser, "_Cilk_for statement expected");
33630 loc = cp_lexer_consume_token (parser->lexer)->location;
33632 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33635 init = orig_init = decl = real_decl = NULL;
33636 this_pre_body = push_stmt_list ();
33639 = cp_parser_omp_for_loop_init (parser, code,
33640 this_pre_body, for_block,
33641 init, orig_init, decl, real_decl);
33643 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33646 this_pre_body = pop_stmt_list (this_pre_body);
33650 pre_body = push_stmt_list ();
33652 add_stmt (this_pre_body);
33653 pre_body = pop_stmt_list (pre_body);
33656 pre_body = this_pre_body;
33661 if (cclauses != NULL
33662 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33663 && real_decl != NULL_TREE)
33666 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33667 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33668 && OMP_CLAUSE_DECL (*c) == real_decl)
33670 error_at (loc, "iteration variable %qD"
33671 " should not be firstprivate", real_decl);
33672 *c = OMP_CLAUSE_CHAIN (*c);
33674 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33675 && OMP_CLAUSE_DECL (*c) == real_decl)
33677 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33679 *c = OMP_CLAUSE_CHAIN (*c);
33680 if (code == OMP_SIMD)
33682 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33683 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33687 OMP_CLAUSE_CHAIN (l) = clauses;
33690 add_private_clause = NULL_TREE;
33694 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33695 && OMP_CLAUSE_DECL (*c) == real_decl)
33696 add_private_clause = NULL_TREE;
33697 c = &OMP_CLAUSE_CHAIN (*c);
33701 if (add_private_clause)
33704 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33706 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33707 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33708 && OMP_CLAUSE_DECL (c) == decl)
33710 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33711 && OMP_CLAUSE_DECL (c) == decl)
33712 error_at (loc, "iteration variable %qD "
33713 "should not be firstprivate",
33715 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33716 && OMP_CLAUSE_DECL (c) == decl)
33717 error_at (loc, "iteration variable %qD should not be reduction",
33722 if (code != OMP_SIMD)
33723 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33724 else if (collapse == 1)
33725 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33727 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33728 OMP_CLAUSE_DECL (c) = add_private_clause;
33729 c = finish_omp_clauses (c, true);
33732 OMP_CLAUSE_CHAIN (c) = clauses;
33734 /* For linear, signal that we need to fill up
33735 the so far unknown linear step. */
33736 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33737 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33743 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33744 cond = cp_parser_omp_for_cond (parser, decl, code);
33745 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33748 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33750 /* If decl is an iterator, preserve the operator on decl
33751 until finish_omp_for. */
33753 && ((processing_template_decl
33754 && (TREE_TYPE (real_decl) == NULL_TREE
33755 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33756 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33757 incr = cp_parser_omp_for_incr (parser, real_decl);
33759 incr = cp_parser_expression (parser);
33760 if (!EXPR_HAS_LOCATION (incr))
33761 protected_set_expr_location (incr, input_location);
33764 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33765 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33766 /*or_comma=*/false,
33767 /*consume_paren=*/true);
33769 TREE_VEC_ELT (declv, i) = decl;
33770 TREE_VEC_ELT (initv, i) = init;
33771 TREE_VEC_ELT (condv, i) = cond;
33772 TREE_VEC_ELT (incrv, i) = incr;
33775 orig_inits.safe_grow_cleared (i + 1);
33776 orig_inits[i] = orig_init;
33779 if (i == count - 1)
33782 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33783 in between the collapsed for loops to be still considered perfectly
33784 nested. Hopefully the final version clarifies this.
33785 For now handle (multiple) {'s and empty statements. */
33786 cp_parser_parse_tentatively (parser);
33789 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33791 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33793 cp_lexer_consume_token (parser->lexer);
33796 else if (bracecount
33797 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33798 cp_lexer_consume_token (parser->lexer);
33801 loc = cp_lexer_peek_token (parser->lexer)->location;
33802 error_at (loc, "not enough collapsed for loops");
33803 collapse_err = true;
33804 cp_parser_abort_tentative_parse (parser);
33813 cp_parser_parse_definitely (parser);
33814 nbraces += bracecount;
33821 /* Note that we saved the original contents of this flag when we entered
33822 the structured block, and so we don't need to re-save it here. */
33823 if (code == CILK_SIMD || code == CILK_FOR)
33824 parser->in_statement = IN_CILK_SIMD_FOR;
33826 parser->in_statement = IN_OMP_FOR;
33828 /* Note that the grammar doesn't call for a structured block here,
33829 though the loop as a whole is a structured block. */
33830 body = push_stmt_list ();
33831 cp_parser_statement (parser, NULL_TREE, false, if_p);
33832 body = pop_stmt_list (body);
33834 if (declv == NULL_TREE)
33837 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33838 body, pre_body, &orig_inits, clauses);
33842 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33844 cp_lexer_consume_token (parser->lexer);
33847 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33848 cp_lexer_consume_token (parser->lexer);
33853 error_at (cp_lexer_peek_token (parser->lexer)->location,
33854 "collapsed loops not perfectly nested");
33856 collapse_err = true;
33857 cp_parser_statement_seq_opt (parser, NULL);
33858 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33863 while (!for_block->is_empty ())
33864 add_stmt (pop_stmt_list (for_block->pop ()));
33865 release_tree_vector (for_block);
33870 /* Helper function for OpenMP parsing, split clauses and call
33871 finish_omp_clauses on each of the set of clauses afterwards. */
33874 cp_omp_split_clauses (location_t loc, enum tree_code code,
33875 omp_clause_mask mask, tree clauses, tree *cclauses)
33878 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33879 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33881 cclauses[i] = finish_omp_clauses (cclauses[i], true);
33885 #pragma omp simd simd-clause[optseq] new-line
33888 #define OMP_SIMD_CLAUSE_MASK \
33889 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33899 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33900 char *p_name, omp_clause_mask mask, tree *cclauses,
33903 tree clauses, sb, ret;
33905 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33907 strcat (p_name, " simd");
33908 mask |= OMP_SIMD_CLAUSE_MASK;
33910 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33914 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33915 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33916 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33917 OMP_CLAUSE_ORDERED);
33918 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33920 error_at (OMP_CLAUSE_LOCATION (c),
33921 "%<ordered%> clause with parameter may not be specified "
33922 "on %qs construct", p_name);
33923 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33927 sb = begin_omp_structured_block ();
33928 save = cp_parser_begin_omp_structured_block (parser);
33930 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
33932 cp_parser_end_omp_structured_block (parser, save);
33933 add_stmt (finish_omp_structured_block (sb));
33939 #pragma omp for for-clause[optseq] new-line
33943 #pragma omp for simd for-simd-clause[optseq] new-line
33946 #define OMP_FOR_CLAUSE_MASK \
33947 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33958 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33959 char *p_name, omp_clause_mask mask, tree *cclauses,
33962 tree clauses, sb, ret;
33964 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33966 strcat (p_name, " for");
33967 mask |= OMP_FOR_CLAUSE_MASK;
33968 /* parallel for{, simd} disallows nowait clause, but for
33969 target {teams distribute ,}parallel for{, simd} it should be accepted. */
33970 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
33971 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33972 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33973 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33974 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33976 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33978 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33979 const char *p = IDENTIFIER_POINTER (id);
33981 if (strcmp (p, "simd") == 0)
33983 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33984 if (cclauses == NULL)
33985 cclauses = cclauses_buf;
33987 cp_lexer_consume_token (parser->lexer);
33988 if (!flag_openmp) /* flag_openmp_simd */
33989 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33991 sb = begin_omp_structured_block ();
33992 save = cp_parser_begin_omp_structured_block (parser);
33993 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33995 cp_parser_end_omp_structured_block (parser, save);
33996 tree body = finish_omp_structured_block (sb);
33999 ret = make_node (OMP_FOR);
34000 TREE_TYPE (ret) = void_type_node;
34001 OMP_FOR_BODY (ret) = body;
34002 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34003 SET_EXPR_LOCATION (ret, loc);
34008 if (!flag_openmp) /* flag_openmp_simd */
34010 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34014 /* Composite distribute parallel for disallows linear clause. */
34015 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34016 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34018 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34022 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34023 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34026 sb = begin_omp_structured_block ();
34027 save = cp_parser_begin_omp_structured_block (parser);
34029 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34031 cp_parser_end_omp_structured_block (parser, save);
34032 add_stmt (finish_omp_structured_block (sb));
34038 # pragma omp master new-line
34039 structured-block */
34042 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34044 cp_parser_require_pragma_eol (parser, pragma_tok);
34045 return c_finish_omp_master (input_location,
34046 cp_parser_omp_structured_block (parser, if_p));
34050 # pragma omp ordered new-line
34054 # pragma omp ordered ordered-clauses new-line
34055 structured-block */
34057 #define OMP_ORDERED_CLAUSE_MASK \
34058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34061 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34062 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34065 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34066 enum pragma_context context, bool *if_p)
34068 location_t loc = pragma_tok->location;
34070 if (context != pragma_stmt && context != pragma_compound)
34072 cp_parser_error (parser, "expected declaration specifiers");
34073 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34077 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34079 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34080 const char *p = IDENTIFIER_POINTER (id);
34082 if (strcmp (p, "depend") == 0)
34084 if (context == pragma_stmt)
34086 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34087 "%<depend%> clause may only be used in compound "
34089 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34093 = cp_parser_omp_all_clauses (parser,
34094 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34095 "#pragma omp ordered", pragma_tok);
34096 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34102 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34103 "#pragma omp ordered", pragma_tok);
34104 c_finish_omp_ordered (loc, clauses,
34105 cp_parser_omp_structured_block (parser, if_p));
34112 { section-sequence }
34115 section-directive[opt] structured-block
34116 section-sequence section-directive structured-block */
34119 cp_parser_omp_sections_scope (cp_parser *parser)
34121 tree stmt, substmt;
34122 bool error_suppress = false;
34125 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34128 stmt = push_stmt_list ();
34130 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34131 != PRAGMA_OMP_SECTION)
34133 substmt = cp_parser_omp_structured_block (parser, NULL);
34134 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34135 add_stmt (substmt);
34140 tok = cp_lexer_peek_token (parser->lexer);
34141 if (tok->type == CPP_CLOSE_BRACE)
34143 if (tok->type == CPP_EOF)
34146 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34148 cp_lexer_consume_token (parser->lexer);
34149 cp_parser_require_pragma_eol (parser, tok);
34150 error_suppress = false;
34152 else if (!error_suppress)
34154 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34155 error_suppress = true;
34158 substmt = cp_parser_omp_structured_block (parser, NULL);
34159 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34160 add_stmt (substmt);
34162 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34164 substmt = pop_stmt_list (stmt);
34166 stmt = make_node (OMP_SECTIONS);
34167 TREE_TYPE (stmt) = void_type_node;
34168 OMP_SECTIONS_BODY (stmt) = substmt;
34175 # pragma omp sections sections-clause[optseq] newline
34178 #define OMP_SECTIONS_CLAUSE_MASK \
34179 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34186 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34187 char *p_name, omp_clause_mask mask, tree *cclauses)
34190 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34192 strcat (p_name, " sections");
34193 mask |= OMP_SECTIONS_CLAUSE_MASK;
34195 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34197 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34201 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34202 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34205 ret = cp_parser_omp_sections_scope (parser);
34207 OMP_SECTIONS_CLAUSES (ret) = clauses;
34213 # pragma omp parallel parallel-clause[optseq] new-line
34215 # pragma omp parallel for parallel-for-clause[optseq] new-line
34217 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34221 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34222 structured-block */
34224 #define OMP_PARALLEL_CLAUSE_MASK \
34225 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34236 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34237 char *p_name, omp_clause_mask mask, tree *cclauses,
34240 tree stmt, clauses, block;
34242 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34244 strcat (p_name, " parallel");
34245 mask |= OMP_PARALLEL_CLAUSE_MASK;
34246 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34247 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34248 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34249 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34251 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34253 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34254 if (cclauses == NULL)
34255 cclauses = cclauses_buf;
34257 cp_lexer_consume_token (parser->lexer);
34258 if (!flag_openmp) /* flag_openmp_simd */
34259 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34261 block = begin_omp_parallel ();
34262 save = cp_parser_begin_omp_structured_block (parser);
34263 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34265 cp_parser_end_omp_structured_block (parser, save);
34266 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34268 if (ret == NULL_TREE)
34270 OMP_PARALLEL_COMBINED (stmt) = 1;
34273 /* When combined with distribute, parallel has to be followed by for.
34274 #pragma omp target parallel is allowed though. */
34276 && (mask & (OMP_CLAUSE_MASK_1
34277 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34279 error_at (loc, "expected %<for%> after %qs", p_name);
34280 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34283 else if (!flag_openmp) /* flag_openmp_simd */
34285 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34288 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34290 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34291 const char *p = IDENTIFIER_POINTER (id);
34292 if (strcmp (p, "sections") == 0)
34294 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34295 cclauses = cclauses_buf;
34297 cp_lexer_consume_token (parser->lexer);
34298 block = begin_omp_parallel ();
34299 save = cp_parser_begin_omp_structured_block (parser);
34300 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34301 cp_parser_end_omp_structured_block (parser, save);
34302 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34304 OMP_PARALLEL_COMBINED (stmt) = 1;
34309 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34313 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34314 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34317 block = begin_omp_parallel ();
34318 save = cp_parser_begin_omp_structured_block (parser);
34319 cp_parser_statement (parser, NULL_TREE, false, if_p);
34320 cp_parser_end_omp_structured_block (parser, save);
34321 stmt = finish_omp_parallel (clauses, block);
34326 # pragma omp single single-clause[optseq] new-line
34327 structured-block */
34329 #define OMP_SINGLE_CLAUSE_MASK \
34330 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34336 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34338 tree stmt = make_node (OMP_SINGLE);
34339 TREE_TYPE (stmt) = void_type_node;
34341 OMP_SINGLE_CLAUSES (stmt)
34342 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34343 "#pragma omp single", pragma_tok);
34344 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34346 return add_stmt (stmt);
34350 # pragma omp task task-clause[optseq] new-line
34351 structured-block */
34353 #define OMP_TASK_CLAUSE_MASK \
34354 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34366 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34368 tree clauses, block;
34371 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34372 "#pragma omp task", pragma_tok);
34373 block = begin_omp_task ();
34374 save = cp_parser_begin_omp_structured_block (parser);
34375 cp_parser_statement (parser, NULL_TREE, false, if_p);
34376 cp_parser_end_omp_structured_block (parser, save);
34377 return finish_omp_task (clauses, block);
34381 # pragma omp taskwait new-line */
34384 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34386 cp_parser_require_pragma_eol (parser, pragma_tok);
34387 finish_omp_taskwait ();
34391 # pragma omp taskyield new-line */
34394 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34396 cp_parser_require_pragma_eol (parser, pragma_tok);
34397 finish_omp_taskyield ();
34401 # pragma omp taskgroup new-line
34402 structured-block */
34405 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34407 cp_parser_require_pragma_eol (parser, pragma_tok);
34408 return c_finish_omp_taskgroup (input_location,
34409 cp_parser_omp_structured_block (parser,
34415 # pragma omp threadprivate (variable-list) */
34418 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34422 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34423 cp_parser_require_pragma_eol (parser, pragma_tok);
34425 finish_omp_threadprivate (vars);
34429 # pragma omp cancel cancel-clause[optseq] new-line */
34431 #define OMP_CANCEL_CLAUSE_MASK \
34432 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34439 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34441 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34442 "#pragma omp cancel", pragma_tok);
34443 finish_omp_cancel (clauses);
34447 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34449 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34450 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34456 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
34459 bool point_seen = false;
34461 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34463 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34464 const char *p = IDENTIFIER_POINTER (id);
34466 if (strcmp (p, "point") == 0)
34468 cp_lexer_consume_token (parser->lexer);
34474 cp_parser_error (parser, "expected %<point%>");
34475 cp_parser_require_pragma_eol (parser, pragma_tok);
34479 clauses = cp_parser_omp_all_clauses (parser,
34480 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34481 "#pragma omp cancellation point",
34483 finish_omp_cancellation_point (clauses);
34487 #pragma omp distribute distribute-clause[optseq] new-line
34490 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34491 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34498 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34499 char *p_name, omp_clause_mask mask, tree *cclauses,
34502 tree clauses, sb, ret;
34504 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34506 strcat (p_name, " distribute");
34507 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34509 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34511 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34512 const char *p = IDENTIFIER_POINTER (id);
34514 bool parallel = false;
34516 if (strcmp (p, "simd") == 0)
34519 parallel = strcmp (p, "parallel") == 0;
34520 if (parallel || simd)
34522 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34523 if (cclauses == NULL)
34524 cclauses = cclauses_buf;
34525 cp_lexer_consume_token (parser->lexer);
34526 if (!flag_openmp) /* flag_openmp_simd */
34529 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34532 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34535 sb = begin_omp_structured_block ();
34536 save = cp_parser_begin_omp_structured_block (parser);
34538 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34541 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34543 cp_parser_end_omp_structured_block (parser, save);
34544 tree body = finish_omp_structured_block (sb);
34547 ret = make_node (OMP_DISTRIBUTE);
34548 TREE_TYPE (ret) = void_type_node;
34549 OMP_FOR_BODY (ret) = body;
34550 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34551 SET_EXPR_LOCATION (ret, loc);
34556 if (!flag_openmp) /* flag_openmp_simd */
34558 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34562 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34566 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34567 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34570 sb = begin_omp_structured_block ();
34571 save = cp_parser_begin_omp_structured_block (parser);
34573 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
34575 cp_parser_end_omp_structured_block (parser, save);
34576 add_stmt (finish_omp_structured_block (sb));
34582 # pragma omp teams teams-clause[optseq] new-line
34583 structured-block */
34585 #define OMP_TEAMS_CLAUSE_MASK \
34586 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34595 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34596 char *p_name, omp_clause_mask mask, tree *cclauses,
34599 tree clauses, sb, ret;
34601 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34603 strcat (p_name, " teams");
34604 mask |= OMP_TEAMS_CLAUSE_MASK;
34606 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34608 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34609 const char *p = IDENTIFIER_POINTER (id);
34610 if (strcmp (p, "distribute") == 0)
34612 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34613 if (cclauses == NULL)
34614 cclauses = cclauses_buf;
34616 cp_lexer_consume_token (parser->lexer);
34617 if (!flag_openmp) /* flag_openmp_simd */
34618 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34620 sb = begin_omp_structured_block ();
34621 save = cp_parser_begin_omp_structured_block (parser);
34622 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34624 cp_parser_end_omp_structured_block (parser, save);
34625 tree body = finish_omp_structured_block (sb);
34628 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34629 ret = make_node (OMP_TEAMS);
34630 TREE_TYPE (ret) = void_type_node;
34631 OMP_TEAMS_CLAUSES (ret) = clauses;
34632 OMP_TEAMS_BODY (ret) = body;
34633 OMP_TEAMS_COMBINED (ret) = 1;
34634 return add_stmt (ret);
34637 if (!flag_openmp) /* flag_openmp_simd */
34639 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34643 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34647 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34648 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34651 tree stmt = make_node (OMP_TEAMS);
34652 TREE_TYPE (stmt) = void_type_node;
34653 OMP_TEAMS_CLAUSES (stmt) = clauses;
34654 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34656 return add_stmt (stmt);
34660 # pragma omp target data target-data-clause[optseq] new-line
34661 structured-block */
34663 #define OMP_TARGET_DATA_CLAUSE_MASK \
34664 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34670 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34673 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34674 "#pragma omp target data", pragma_tok);
34676 for (tree *pc = &clauses; *pc;)
34678 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34679 switch (OMP_CLAUSE_MAP_KIND (*pc))
34682 case GOMP_MAP_ALWAYS_TO:
34683 case GOMP_MAP_FROM:
34684 case GOMP_MAP_ALWAYS_FROM:
34685 case GOMP_MAP_TOFROM:
34686 case GOMP_MAP_ALWAYS_TOFROM:
34687 case GOMP_MAP_ALLOC:
34690 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34691 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34692 case GOMP_MAP_ALWAYS_POINTER:
34696 error_at (OMP_CLAUSE_LOCATION (*pc),
34697 "%<#pragma omp target data%> with map-type other "
34698 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34699 "on %<map%> clause");
34700 *pc = OMP_CLAUSE_CHAIN (*pc);
34703 pc = &OMP_CLAUSE_CHAIN (*pc);
34709 error_at (pragma_tok->location,
34710 "%<#pragma omp target data%> must contain at least "
34711 "one %<map%> clause");
34715 tree stmt = make_node (OMP_TARGET_DATA);
34716 TREE_TYPE (stmt) = void_type_node;
34717 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34719 keep_next_level (true);
34720 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34722 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34723 return add_stmt (stmt);
34727 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34728 structured-block */
34730 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34731 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34738 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34739 enum pragma_context context)
34741 bool data_seen = false;
34742 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34744 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34745 const char *p = IDENTIFIER_POINTER (id);
34747 if (strcmp (p, "data") == 0)
34749 cp_lexer_consume_token (parser->lexer);
34755 cp_parser_error (parser, "expected %<data%>");
34756 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34760 if (context == pragma_stmt)
34762 error_at (pragma_tok->location,
34763 "%<#pragma omp target enter data%> may only be "
34764 "used in compound statements");
34765 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34770 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34771 "#pragma omp target enter data", pragma_tok);
34773 for (tree *pc = &clauses; *pc;)
34775 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34776 switch (OMP_CLAUSE_MAP_KIND (*pc))
34779 case GOMP_MAP_ALWAYS_TO:
34780 case GOMP_MAP_ALLOC:
34783 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34784 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34785 case GOMP_MAP_ALWAYS_POINTER:
34789 error_at (OMP_CLAUSE_LOCATION (*pc),
34790 "%<#pragma omp target enter data%> with map-type other "
34791 "than %<to%> or %<alloc%> on %<map%> clause");
34792 *pc = OMP_CLAUSE_CHAIN (*pc);
34795 pc = &OMP_CLAUSE_CHAIN (*pc);
34801 error_at (pragma_tok->location,
34802 "%<#pragma omp target enter data%> must contain at least "
34803 "one %<map%> clause");
34807 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34808 TREE_TYPE (stmt) = void_type_node;
34809 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34810 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34811 return add_stmt (stmt);
34815 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34816 structured-block */
34818 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34819 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34826 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34827 enum pragma_context context)
34829 bool data_seen = false;
34830 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34832 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34833 const char *p = IDENTIFIER_POINTER (id);
34835 if (strcmp (p, "data") == 0)
34837 cp_lexer_consume_token (parser->lexer);
34843 cp_parser_error (parser, "expected %<data%>");
34844 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34848 if (context == pragma_stmt)
34850 error_at (pragma_tok->location,
34851 "%<#pragma omp target exit data%> may only be "
34852 "used in compound statements");
34853 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34858 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34859 "#pragma omp target exit data", pragma_tok);
34861 for (tree *pc = &clauses; *pc;)
34863 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34864 switch (OMP_CLAUSE_MAP_KIND (*pc))
34866 case GOMP_MAP_FROM:
34867 case GOMP_MAP_ALWAYS_FROM:
34868 case GOMP_MAP_RELEASE:
34869 case GOMP_MAP_DELETE:
34872 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34873 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34874 case GOMP_MAP_ALWAYS_POINTER:
34878 error_at (OMP_CLAUSE_LOCATION (*pc),
34879 "%<#pragma omp target exit data%> with map-type other "
34880 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34882 *pc = OMP_CLAUSE_CHAIN (*pc);
34885 pc = &OMP_CLAUSE_CHAIN (*pc);
34891 error_at (pragma_tok->location,
34892 "%<#pragma omp target exit data%> must contain at least "
34893 "one %<map%> clause");
34897 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34898 TREE_TYPE (stmt) = void_type_node;
34899 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34900 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34901 return add_stmt (stmt);
34905 # pragma omp target update target-update-clause[optseq] new-line */
34907 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34908 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34916 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34917 enum pragma_context context)
34919 if (context == pragma_stmt)
34921 error_at (pragma_tok->location,
34922 "%<#pragma omp target update%> may only be "
34923 "used in compound statements");
34924 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34929 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34930 "#pragma omp target update", pragma_tok);
34931 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34932 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34934 error_at (pragma_tok->location,
34935 "%<#pragma omp target update%> must contain at least one "
34936 "%<from%> or %<to%> clauses");
34940 tree stmt = make_node (OMP_TARGET_UPDATE);
34941 TREE_TYPE (stmt) = void_type_node;
34942 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34943 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34949 # pragma omp target target-clause[optseq] new-line
34950 structured-block */
34952 #define OMP_TARGET_CLAUSE_MASK \
34953 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34964 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34965 enum pragma_context context, bool *if_p)
34967 tree *pc = NULL, stmt;
34969 if (context != pragma_stmt && context != pragma_compound)
34971 cp_parser_error (parser, "expected declaration specifiers");
34972 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34976 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34978 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34979 const char *p = IDENTIFIER_POINTER (id);
34980 enum tree_code ccode = ERROR_MARK;
34982 if (strcmp (p, "teams") == 0)
34984 else if (strcmp (p, "parallel") == 0)
34985 ccode = OMP_PARALLEL;
34986 else if (strcmp (p, "simd") == 0)
34988 if (ccode != ERROR_MARK)
34990 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34991 char p_name[sizeof ("#pragma omp target teams distribute "
34992 "parallel for simd")];
34994 cp_lexer_consume_token (parser->lexer);
34995 strcpy (p_name, "#pragma omp target");
34996 if (!flag_openmp) /* flag_openmp_simd */
35002 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35003 OMP_TARGET_CLAUSE_MASK,
35007 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35008 OMP_TARGET_CLAUSE_MASK,
35012 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35013 OMP_TARGET_CLAUSE_MASK,
35017 gcc_unreachable ();
35019 return stmt != NULL_TREE;
35021 keep_next_level (true);
35022 tree sb = begin_omp_structured_block (), ret;
35023 unsigned save = cp_parser_begin_omp_structured_block (parser);
35027 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35028 OMP_TARGET_CLAUSE_MASK, cclauses,
35032 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35033 OMP_TARGET_CLAUSE_MASK, cclauses,
35037 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35038 OMP_TARGET_CLAUSE_MASK, cclauses,
35042 gcc_unreachable ();
35044 cp_parser_end_omp_structured_block (parser, save);
35045 tree body = finish_omp_structured_block (sb);
35046 if (ret == NULL_TREE)
35048 if (ccode == OMP_TEAMS && !processing_template_decl)
35050 /* For combined target teams, ensure the num_teams and
35051 thread_limit clause expressions are evaluated on the host,
35052 before entering the target construct. */
35054 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35055 c; c = OMP_CLAUSE_CHAIN (c))
35056 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35057 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35058 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35060 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35061 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35062 if (expr == error_mark_node)
35064 tree tmp = TARGET_EXPR_SLOT (expr);
35066 OMP_CLAUSE_OPERAND (c, 0) = expr;
35067 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35068 OMP_CLAUSE_FIRSTPRIVATE);
35069 OMP_CLAUSE_DECL (tc) = tmp;
35070 OMP_CLAUSE_CHAIN (tc)
35071 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35072 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35075 tree stmt = make_node (OMP_TARGET);
35076 TREE_TYPE (stmt) = void_type_node;
35077 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35078 OMP_TARGET_BODY (stmt) = body;
35079 OMP_TARGET_COMBINED (stmt) = 1;
35081 pc = &OMP_TARGET_CLAUSES (stmt);
35082 goto check_clauses;
35084 else if (!flag_openmp) /* flag_openmp_simd */
35086 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35089 else if (strcmp (p, "data") == 0)
35091 cp_lexer_consume_token (parser->lexer);
35092 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35095 else if (strcmp (p, "enter") == 0)
35097 cp_lexer_consume_token (parser->lexer);
35098 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35101 else if (strcmp (p, "exit") == 0)
35103 cp_lexer_consume_token (parser->lexer);
35104 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35107 else if (strcmp (p, "update") == 0)
35109 cp_lexer_consume_token (parser->lexer);
35110 return cp_parser_omp_target_update (parser, pragma_tok, context);
35114 stmt = make_node (OMP_TARGET);
35115 TREE_TYPE (stmt) = void_type_node;
35117 OMP_TARGET_CLAUSES (stmt)
35118 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35119 "#pragma omp target", pragma_tok);
35120 pc = &OMP_TARGET_CLAUSES (stmt);
35121 keep_next_level (true);
35122 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35124 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35130 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35131 switch (OMP_CLAUSE_MAP_KIND (*pc))
35134 case GOMP_MAP_ALWAYS_TO:
35135 case GOMP_MAP_FROM:
35136 case GOMP_MAP_ALWAYS_FROM:
35137 case GOMP_MAP_TOFROM:
35138 case GOMP_MAP_ALWAYS_TOFROM:
35139 case GOMP_MAP_ALLOC:
35140 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35141 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35142 case GOMP_MAP_ALWAYS_POINTER:
35145 error_at (OMP_CLAUSE_LOCATION (*pc),
35146 "%<#pragma omp target%> with map-type other "
35147 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35148 "on %<map%> clause");
35149 *pc = OMP_CLAUSE_CHAIN (*pc);
35152 pc = &OMP_CLAUSE_CHAIN (*pc);
35158 # pragma acc cache (variable-list) new-line
35162 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35164 tree stmt, clauses;
35166 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35167 clauses = finish_omp_clauses (clauses, false);
35169 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35171 stmt = make_node (OACC_CACHE);
35172 TREE_TYPE (stmt) = void_type_node;
35173 OACC_CACHE_CLAUSES (stmt) = clauses;
35174 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35181 # pragma acc data oacc-data-clause[optseq] new-line
35182 structured-block */
35184 #define OACC_DATA_CLAUSE_MASK \
35185 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35198 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35200 tree stmt, clauses, block;
35203 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35204 "#pragma acc data", pragma_tok);
35206 block = begin_omp_parallel ();
35207 save = cp_parser_begin_omp_structured_block (parser);
35208 cp_parser_statement (parser, NULL_TREE, false, if_p);
35209 cp_parser_end_omp_structured_block (parser, save);
35210 stmt = finish_oacc_data (clauses, block);
35215 # pragma acc host_data <clauses> new-line
35216 structured-block */
35218 #define OACC_HOST_DATA_CLAUSE_MASK \
35219 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35222 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35224 tree stmt, clauses, block;
35227 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35228 "#pragma acc host_data", pragma_tok);
35230 block = begin_omp_parallel ();
35231 save = cp_parser_begin_omp_structured_block (parser);
35232 cp_parser_statement (parser, NULL_TREE, false, if_p);
35233 cp_parser_end_omp_structured_block (parser, save);
35234 stmt = finish_oacc_host_data (clauses, block);
35239 # pragma acc declare oacc-data-clause[optseq] new-line
35242 #define OACC_DECLARE_CLAUSE_MASK \
35243 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35257 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35259 tree clauses, stmt;
35260 bool error = false;
35262 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35263 "#pragma acc declare", pragma_tok, true);
35266 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35268 error_at (pragma_tok->location,
35269 "no valid clauses specified in %<#pragma acc declare%>");
35273 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35275 location_t loc = OMP_CLAUSE_LOCATION (t);
35276 tree decl = OMP_CLAUSE_DECL (t);
35277 if (!DECL_P (decl))
35279 error_at (loc, "array section in %<#pragma acc declare%>");
35283 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35284 switch (OMP_CLAUSE_MAP_KIND (t))
35286 case GOMP_MAP_FORCE_ALLOC:
35287 case GOMP_MAP_FORCE_TO:
35288 case GOMP_MAP_FORCE_DEVICEPTR:
35289 case GOMP_MAP_DEVICE_RESIDENT:
35292 case GOMP_MAP_POINTER:
35293 /* Generated by c_finish_omp_clauses from array sections;
35294 avoid spurious diagnostics. */
35297 case GOMP_MAP_LINK:
35298 if (!global_bindings_p ()
35299 && (TREE_STATIC (decl)
35300 || !DECL_EXTERNAL (decl)))
35303 "%qD must be a global variable in"
35304 "%<#pragma acc declare link%>",
35312 if (global_bindings_p ())
35314 error_at (loc, "invalid OpenACC clause at file scope");
35318 if (DECL_EXTERNAL (decl))
35321 "invalid use of %<extern%> variable %qD "
35322 "in %<#pragma acc declare%>", decl);
35326 else if (TREE_PUBLIC (decl))
35329 "invalid use of %<global%> variable %qD "
35330 "in %<#pragma acc declare%>", decl);
35337 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35338 || lookup_attribute ("omp declare target link",
35339 DECL_ATTRIBUTES (decl)))
35341 error_at (loc, "variable %qD used more than once with "
35342 "%<#pragma acc declare%>", decl);
35351 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35352 id = get_identifier ("omp declare target link");
35354 id = get_identifier ("omp declare target");
35356 DECL_ATTRIBUTES (decl)
35357 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35358 if (global_bindings_p ())
35360 symtab_node *node = symtab_node::get (decl);
35363 node->offloadable = 1;
35364 if (ENABLE_OFFLOADING)
35366 g->have_offload = true;
35367 if (is_a <varpool_node *> (node))
35368 vec_safe_push (offload_vars, decl);
35375 if (error || global_bindings_p ())
35378 stmt = make_node (OACC_DECLARE);
35379 TREE_TYPE (stmt) = void_type_node;
35380 OACC_DECLARE_CLAUSES (stmt) = clauses;
35381 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35389 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35393 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35395 LOC is the location of the #pragma token.
35398 #define OACC_ENTER_DATA_CLAUSE_MASK \
35399 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35407 #define OACC_EXIT_DATA_CLAUSE_MASK \
35408 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35415 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35418 tree stmt, clauses;
35420 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35421 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35423 cp_parser_error (parser, enter
35424 ? "expected %<data%> in %<#pragma acc enter data%>"
35425 : "expected %<data%> in %<#pragma acc exit data%>");
35426 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35431 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35432 if (strcmp (p, "data") != 0)
35434 cp_parser_error (parser, "invalid pragma");
35435 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35439 cp_lexer_consume_token (parser->lexer);
35442 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35443 "#pragma acc enter data", pragma_tok);
35445 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35446 "#pragma acc exit data", pragma_tok);
35448 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35450 error_at (pragma_tok->location,
35451 "%<#pragma acc enter data%> has no data movement clause");
35455 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35456 TREE_TYPE (stmt) = void_type_node;
35457 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35458 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35464 # pragma acc loop oacc-loop-clause[optseq] new-line
35465 structured-block */
35467 #define OACC_LOOP_CLAUSE_MASK \
35468 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35480 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35481 omp_clause_mask mask, tree *cclauses, bool *if_p)
35483 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35485 strcat (p_name, " loop");
35486 mask |= OACC_LOOP_CLAUSE_MASK;
35488 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35492 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35494 *cclauses = finish_omp_clauses (*cclauses, false);
35496 clauses = finish_omp_clauses (clauses, false);
35499 tree block = begin_omp_structured_block ();
35500 int save = cp_parser_begin_omp_structured_block (parser);
35501 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35502 cp_parser_end_omp_structured_block (parser, save);
35503 add_stmt (finish_omp_structured_block (block));
35509 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35514 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35518 #define OACC_KERNELS_CLAUSE_MASK \
35519 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35534 #define OACC_PARALLEL_CLAUSE_MASK \
35535 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35557 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35558 char *p_name, bool *if_p)
35560 omp_clause_mask mask;
35561 enum tree_code code;
35562 switch (cp_parser_pragma_kind (pragma_tok))
35564 case PRAGMA_OACC_KERNELS:
35565 strcat (p_name, " kernels");
35566 mask = OACC_KERNELS_CLAUSE_MASK;
35567 code = OACC_KERNELS;
35569 case PRAGMA_OACC_PARALLEL:
35570 strcat (p_name, " parallel");
35571 mask = OACC_PARALLEL_CLAUSE_MASK;
35572 code = OACC_PARALLEL;
35575 gcc_unreachable ();
35578 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35581 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35582 if (strcmp (p, "loop") == 0)
35584 cp_lexer_consume_token (parser->lexer);
35585 tree block = begin_omp_parallel ();
35587 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
35589 return finish_omp_construct (code, block, clauses);
35593 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35595 tree block = begin_omp_parallel ();
35596 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35597 cp_parser_statement (parser, NULL_TREE, false, if_p);
35598 cp_parser_end_omp_structured_block (parser, save);
35599 return finish_omp_construct (code, block, clauses);
35603 # pragma acc update oacc-update-clause[optseq] new-line
35606 #define OACC_UPDATE_CLAUSE_MASK \
35607 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35615 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35617 tree stmt, clauses;
35619 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35620 "#pragma acc update", pragma_tok);
35622 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35624 error_at (pragma_tok->location,
35625 "%<#pragma acc update%> must contain at least one "
35626 "%<device%> or %<host%> or %<self%> clause");
35630 stmt = make_node (OACC_UPDATE);
35631 TREE_TYPE (stmt) = void_type_node;
35632 OACC_UPDATE_CLAUSES (stmt) = clauses;
35633 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35639 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35641 LOC is the location of the #pragma token.
35644 #define OACC_WAIT_CLAUSE_MASK \
35645 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35648 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35650 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35651 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35653 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35654 list = cp_parser_oacc_wait_list (parser, loc, list);
35656 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35657 "#pragma acc wait", pragma_tok);
35659 stmt = c_finish_oacc_wait (loc, list, clauses);
35660 stmt = finish_expr_stmt (stmt);
35666 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35668 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35669 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35677 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35678 enum pragma_context context)
35680 bool first_p = parser->omp_declare_simd == NULL;
35681 cp_omp_declare_simd_data data;
35684 data.error_seen = false;
35685 data.fndecl_seen = false;
35686 data.tokens = vNULL;
35687 parser->omp_declare_simd = &data;
35689 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35690 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35691 cp_lexer_consume_token (parser->lexer);
35692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35693 parser->omp_declare_simd->error_seen = true;
35694 cp_parser_require_pragma_eol (parser, pragma_tok);
35695 struct cp_token_cache *cp
35696 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35697 parser->omp_declare_simd->tokens.safe_push (cp);
35700 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35701 cp_parser_pragma (parser, context, NULL);
35704 case pragma_external:
35705 cp_parser_declaration (parser);
35707 case pragma_member:
35708 cp_parser_member_declaration (parser);
35710 case pragma_objc_icode:
35711 cp_parser_block_declaration (parser, /*statement_p=*/false);
35714 cp_parser_declaration_statement (parser);
35717 if (parser->omp_declare_simd
35718 && !parser->omp_declare_simd->error_seen
35719 && !parser->omp_declare_simd->fndecl_seen)
35720 error_at (pragma_tok->location,
35721 "%<#pragma omp declare simd%> not immediately followed by "
35722 "function declaration or definition");
35723 data.tokens.release ();
35724 parser->omp_declare_simd = NULL;
35728 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35729 This function is modelled similar to the late parsing of omp declare
35733 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35735 struct cp_token_cache *ce;
35736 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35739 if (parser->omp_declare_simd != NULL
35740 || lookup_attribute ("simd", attrs))
35742 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35743 "used in the same function marked as a Cilk Plus SIMD-enabled "
35745 parser->cilk_simd_fn_info->tokens.release ();
35746 XDELETE (parser->cilk_simd_fn_info);
35747 parser->cilk_simd_fn_info = NULL;
35750 if (!info->error_seen && info->fndecl_seen)
35752 error ("vector attribute not immediately followed by a single function"
35753 " declaration or definition");
35754 info->error_seen = true;
35756 if (info->error_seen)
35759 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35763 cp_parser_push_lexer_for_tokens (parser, ce);
35764 parser->lexer->in_pragma = true;
35765 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35766 "SIMD-enabled functions attribute",
35768 cp_parser_pop_lexer (parser);
35770 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35772 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35773 TREE_CHAIN (c) = attrs;
35776 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35777 TREE_CHAIN (c) = attrs;
35778 if (processing_template_decl)
35779 ATTR_IS_DEPENDENT (c) = 1;
35782 info->fndecl_seen = true;
35783 parser->cilk_simd_fn_info->tokens.release ();
35784 XDELETE (parser->cilk_simd_fn_info);
35785 parser->cilk_simd_fn_info = NULL;
35789 /* Finalize #pragma omp declare simd clauses after direct declarator has
35790 been parsed, and put that into "omp declare simd" attribute. */
35793 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35795 struct cp_token_cache *ce;
35796 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35799 if (!data->error_seen && data->fndecl_seen)
35801 error ("%<#pragma omp declare simd%> not immediately followed by "
35802 "a single function declaration or definition");
35803 data->error_seen = true;
35806 if (data->error_seen)
35809 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35813 cp_parser_push_lexer_for_tokens (parser, ce);
35814 parser->lexer->in_pragma = true;
35815 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35816 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35817 cp_lexer_consume_token (parser->lexer);
35818 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35819 "#pragma omp declare simd", pragma_tok);
35820 cp_parser_pop_lexer (parser);
35822 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35823 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35824 TREE_CHAIN (c) = attrs;
35825 if (processing_template_decl)
35826 ATTR_IS_DEPENDENT (c) = 1;
35830 data->fndecl_seen = true;
35836 # pragma omp declare target new-line
35837 declarations and definitions
35838 # pragma omp end declare target new-line
35841 # pragma omp declare target ( extended-list ) new-line
35843 # pragma omp declare target declare-target-clauses[seq] new-line */
35845 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35846 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35850 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35852 tree clauses = NULL_TREE;
35853 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35855 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35856 "#pragma omp declare target", pragma_tok);
35857 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35859 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35861 clauses = finish_omp_clauses (clauses, true);
35862 cp_parser_require_pragma_eol (parser, pragma_tok);
35866 cp_parser_require_pragma_eol (parser, pragma_tok);
35867 scope_chain->omp_declare_target_attribute++;
35870 if (scope_chain->omp_declare_target_attribute)
35871 error_at (pragma_tok->location,
35872 "%<#pragma omp declare target%> with clauses in between "
35873 "%<#pragma omp declare target%> without clauses and "
35874 "%<#pragma omp end declare target%>");
35875 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35877 tree t = OMP_CLAUSE_DECL (c), id;
35878 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35879 tree at2 = lookup_attribute ("omp declare target link",
35880 DECL_ATTRIBUTES (t));
35881 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35883 id = get_identifier ("omp declare target link");
35884 std::swap (at1, at2);
35887 id = get_identifier ("omp declare target");
35890 error_at (OMP_CLAUSE_LOCATION (c),
35891 "%qD specified both in declare target %<link%> and %<to%>"
35897 symtab_node *node = symtab_node::get (t);
35898 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35901 node->offloadable = 1;
35902 if (ENABLE_OFFLOADING)
35904 g->have_offload = true;
35905 if (is_a <varpool_node *> (node))
35906 vec_safe_push (offload_vars, t);
35914 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35916 const char *p = "";
35917 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35919 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35920 p = IDENTIFIER_POINTER (id);
35922 if (strcmp (p, "declare") == 0)
35924 cp_lexer_consume_token (parser->lexer);
35926 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35928 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35929 p = IDENTIFIER_POINTER (id);
35931 if (strcmp (p, "target") == 0)
35932 cp_lexer_consume_token (parser->lexer);
35935 cp_parser_error (parser, "expected %<target%>");
35936 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35942 cp_parser_error (parser, "expected %<declare%>");
35943 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35946 cp_parser_require_pragma_eol (parser, pragma_tok);
35947 if (!scope_chain->omp_declare_target_attribute)
35948 error_at (pragma_tok->location,
35949 "%<#pragma omp end declare target%> without corresponding "
35950 "%<#pragma omp declare target%>");
35952 scope_chain->omp_declare_target_attribute--;
35955 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35956 expression and optional initializer clause of
35957 #pragma omp declare reduction. We store the expression(s) as
35958 either 3, 6 or 7 special statements inside of the artificial function's
35959 body. The first two statements are DECL_EXPRs for the artificial
35960 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35961 expression that uses those variables.
35962 If there was any INITIALIZER clause, this is followed by further statements,
35963 the fourth and fifth statements are DECL_EXPRs for the artificial
35964 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35965 constructor variant (first token after open paren is not omp_priv),
35966 then the sixth statement is a statement with the function call expression
35967 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35968 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35969 to initialize the OMP_PRIV artificial variable and there is seventh
35970 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35973 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35975 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35976 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35977 type = TREE_TYPE (type);
35978 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35979 DECL_ARTIFICIAL (omp_out) = 1;
35980 pushdecl (omp_out);
35981 add_decl_expr (omp_out);
35982 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35983 DECL_ARTIFICIAL (omp_in) = 1;
35985 add_decl_expr (omp_in);
35987 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35989 keep_next_level (true);
35990 tree block = begin_omp_structured_block ();
35991 combiner = cp_parser_expression (parser);
35992 finish_expr_stmt (combiner);
35993 block = finish_omp_structured_block (block);
35996 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35999 const char *p = "";
36000 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36002 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36003 p = IDENTIFIER_POINTER (id);
36006 if (strcmp (p, "initializer") == 0)
36008 cp_lexer_consume_token (parser->lexer);
36009 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36013 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36015 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36016 p = IDENTIFIER_POINTER (id);
36019 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36020 DECL_ARTIFICIAL (omp_priv) = 1;
36021 pushdecl (omp_priv);
36022 add_decl_expr (omp_priv);
36023 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36024 DECL_ARTIFICIAL (omp_orig) = 1;
36025 pushdecl (omp_orig);
36026 add_decl_expr (omp_orig);
36028 keep_next_level (true);
36029 block = begin_omp_structured_block ();
36032 if (strcmp (p, "omp_priv") == 0)
36034 bool is_direct_init, is_non_constant_init;
36036 cp_lexer_consume_token (parser->lexer);
36037 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36038 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36039 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36040 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36042 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36043 == CPP_CLOSE_PAREN))
36045 finish_omp_structured_block (block);
36046 error ("invalid initializer clause");
36049 initializer = cp_parser_initializer (parser, &is_direct_init,
36050 &is_non_constant_init);
36051 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36052 NULL_TREE, LOOKUP_ONLYCONVERTING);
36056 cp_parser_parse_tentatively (parser);
36057 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36058 /*check_dependency_p=*/true,
36059 /*template_p=*/NULL,
36060 /*declarator_p=*/false,
36061 /*optional_p=*/false);
36062 vec<tree, va_gc> *args;
36063 if (fn_name == error_mark_node
36064 || cp_parser_error_occurred (parser)
36065 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36066 || ((args = cp_parser_parenthesized_expression_list
36067 (parser, non_attr, /*cast_p=*/false,
36068 /*allow_expansion_p=*/true,
36069 /*non_constant_p=*/NULL)),
36070 cp_parser_error_occurred (parser)))
36072 finish_omp_structured_block (block);
36073 cp_parser_abort_tentative_parse (parser);
36074 cp_parser_error (parser, "expected id-expression (arguments)");
36079 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36080 if (arg == omp_priv
36081 || (TREE_CODE (arg) == ADDR_EXPR
36082 && TREE_OPERAND (arg, 0) == omp_priv))
36084 cp_parser_abort_tentative_parse (parser);
36085 if (arg == NULL_TREE)
36086 error ("one of the initializer call arguments should be %<omp_priv%>"
36087 " or %<&omp_priv%>");
36088 initializer = cp_parser_postfix_expression (parser, false, false, false,
36090 finish_expr_stmt (initializer);
36093 block = finish_omp_structured_block (block);
36094 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36098 add_decl_expr (omp_orig);
36100 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36104 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36105 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36111 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36112 initializer-clause[opt] new-line
36114 initializer-clause:
36115 initializer (omp_priv initializer)
36116 initializer (function-name (argument-list)) */
36119 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36120 enum pragma_context)
36122 auto_vec<tree> types;
36123 enum tree_code reduc_code = ERROR_MARK;
36124 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36126 cp_token *first_token;
36127 cp_token_cache *cp;
36131 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36132 p = obstack_alloc (&declarator_obstack, 0);
36134 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36137 switch (cp_lexer_peek_token (parser->lexer)->type)
36140 reduc_code = PLUS_EXPR;
36143 reduc_code = MULT_EXPR;
36146 reduc_code = MINUS_EXPR;
36149 reduc_code = BIT_AND_EXPR;
36152 reduc_code = BIT_XOR_EXPR;
36155 reduc_code = BIT_IOR_EXPR;
36158 reduc_code = TRUTH_ANDIF_EXPR;
36161 reduc_code = TRUTH_ORIF_EXPR;
36164 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36167 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36168 "%<|%>, %<&&%>, %<||%> or identifier");
36172 if (reduc_code != ERROR_MARK)
36173 cp_lexer_consume_token (parser->lexer);
36175 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36176 if (reduc_id == error_mark_node)
36179 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36182 /* Types may not be defined in declare reduction type list. */
36183 const char *saved_message;
36184 saved_message = parser->type_definition_forbidden_message;
36185 parser->type_definition_forbidden_message
36186 = G_("types may not be defined in declare reduction type list");
36187 bool saved_colon_corrects_to_scope_p;
36188 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36189 parser->colon_corrects_to_scope_p = false;
36190 bool saved_colon_doesnt_start_class_def_p;
36191 saved_colon_doesnt_start_class_def_p
36192 = parser->colon_doesnt_start_class_def_p;
36193 parser->colon_doesnt_start_class_def_p = true;
36197 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36198 type = cp_parser_type_id (parser);
36199 if (type == error_mark_node)
36201 else if (ARITHMETIC_TYPE_P (type)
36202 && (orig_reduc_id == NULL_TREE
36203 || (TREE_CODE (type) != COMPLEX_TYPE
36204 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36206 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36208 error_at (loc, "predeclared arithmetic type %qT in "
36209 "%<#pragma omp declare reduction%>", type);
36210 else if (TREE_CODE (type) == FUNCTION_TYPE
36211 || TREE_CODE (type) == METHOD_TYPE
36212 || TREE_CODE (type) == ARRAY_TYPE)
36213 error_at (loc, "function or array type %qT in "
36214 "%<#pragma omp declare reduction%>", type);
36215 else if (TREE_CODE (type) == REFERENCE_TYPE)
36216 error_at (loc, "reference type %qT in "
36217 "%<#pragma omp declare reduction%>", type);
36218 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36219 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36220 "%<#pragma omp declare reduction%>", type);
36222 types.safe_push (type);
36224 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36225 cp_lexer_consume_token (parser->lexer);
36230 /* Restore the saved message. */
36231 parser->type_definition_forbidden_message = saved_message;
36232 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36233 parser->colon_doesnt_start_class_def_p
36234 = saved_colon_doesnt_start_class_def_p;
36236 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36237 || types.is_empty ())
36240 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36244 first_token = cp_lexer_peek_token (parser->lexer);
36247 FOR_EACH_VEC_ELT (types, i, type)
36250 = build_function_type_list (void_type_node,
36251 cp_build_reference_type (type, false),
36253 tree this_reduc_id = reduc_id;
36254 if (!dependent_type_p (type))
36255 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36256 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36257 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36258 DECL_ARTIFICIAL (fndecl) = 1;
36259 DECL_EXTERNAL (fndecl) = 1;
36260 DECL_DECLARED_INLINE_P (fndecl) = 1;
36261 DECL_IGNORED_P (fndecl) = 1;
36262 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36263 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36264 DECL_ATTRIBUTES (fndecl)
36265 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36266 DECL_ATTRIBUTES (fndecl));
36267 if (processing_template_decl)
36268 fndecl = push_template_decl (fndecl);
36269 bool block_scope = false;
36270 tree block = NULL_TREE;
36271 if (current_function_decl)
36273 block_scope = true;
36274 DECL_CONTEXT (fndecl) = global_namespace;
36275 if (!processing_template_decl)
36278 else if (current_class_type)
36282 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36283 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36284 cp_lexer_consume_token (parser->lexer);
36285 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36287 cp = cp_token_cache_new (first_token,
36288 cp_lexer_peek_nth_token (parser->lexer,
36291 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36292 finish_member_declaration (fndecl);
36293 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36294 DECL_PENDING_INLINE_P (fndecl) = 1;
36295 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36300 DECL_CONTEXT (fndecl) = current_namespace;
36304 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36306 block = begin_omp_structured_block ();
36309 cp_parser_push_lexer_for_tokens (parser, cp);
36310 parser->lexer->in_pragma = true;
36312 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36315 finish_function (0);
36317 DECL_CONTEXT (fndecl) = current_function_decl;
36319 cp_parser_pop_lexer (parser);
36323 cp_parser_pop_lexer (parser);
36325 finish_function (0);
36328 DECL_CONTEXT (fndecl) = current_function_decl;
36329 block = finish_omp_structured_block (block);
36330 if (TREE_CODE (block) == BIND_EXPR)
36331 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36332 else if (TREE_CODE (block) == STATEMENT_LIST)
36333 DECL_SAVED_TREE (fndecl) = block;
36334 if (processing_template_decl)
36335 add_decl_expr (fndecl);
36337 cp_check_omp_declare_reduction (fndecl);
36338 if (cp == NULL && types.length () > 1)
36339 cp = cp_token_cache_new (first_token,
36340 cp_lexer_peek_nth_token (parser->lexer, 2));
36341 if (errs != errorcount)
36345 cp_parser_require_pragma_eol (parser, pragma_tok);
36348 /* Free any declarators allocated. */
36349 obstack_free (&declarator_obstack, p);
36353 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36354 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36355 initializer-clause[opt] new-line
36356 #pragma omp declare target new-line */
36359 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36360 enum pragma_context context)
36362 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36364 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36365 const char *p = IDENTIFIER_POINTER (id);
36367 if (strcmp (p, "simd") == 0)
36369 cp_lexer_consume_token (parser->lexer);
36370 cp_parser_omp_declare_simd (parser, pragma_tok,
36374 cp_ensure_no_omp_declare_simd (parser);
36375 if (strcmp (p, "reduction") == 0)
36377 cp_lexer_consume_token (parser->lexer);
36378 cp_parser_omp_declare_reduction (parser, pragma_tok,
36382 if (!flag_openmp) /* flag_openmp_simd */
36384 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36387 if (strcmp (p, "target") == 0)
36389 cp_lexer_consume_token (parser->lexer);
36390 cp_parser_omp_declare_target (parser, pragma_tok);
36394 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36396 cp_parser_require_pragma_eol (parser, pragma_tok);
36400 #pragma omp taskloop taskloop-clause[optseq] new-line
36403 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36406 #define OMP_TASKLOOP_CLAUSE_MASK \
36407 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36423 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36424 char *p_name, omp_clause_mask mask, tree *cclauses,
36427 tree clauses, sb, ret;
36429 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36431 strcat (p_name, " taskloop");
36432 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36434 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36436 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36437 const char *p = IDENTIFIER_POINTER (id);
36439 if (strcmp (p, "simd") == 0)
36441 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36442 if (cclauses == NULL)
36443 cclauses = cclauses_buf;
36445 cp_lexer_consume_token (parser->lexer);
36446 if (!flag_openmp) /* flag_openmp_simd */
36447 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36449 sb = begin_omp_structured_block ();
36450 save = cp_parser_begin_omp_structured_block (parser);
36451 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36453 cp_parser_end_omp_structured_block (parser, save);
36454 tree body = finish_omp_structured_block (sb);
36457 ret = make_node (OMP_TASKLOOP);
36458 TREE_TYPE (ret) = void_type_node;
36459 OMP_FOR_BODY (ret) = body;
36460 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36461 SET_EXPR_LOCATION (ret, loc);
36466 if (!flag_openmp) /* flag_openmp_simd */
36468 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36472 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36476 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36477 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36480 sb = begin_omp_structured_block ();
36481 save = cp_parser_begin_omp_structured_block (parser);
36483 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36486 cp_parser_end_omp_structured_block (parser, save);
36487 add_stmt (finish_omp_structured_block (sb));
36494 # pragma acc routine oacc-routine-clause[optseq] new-line
36495 function-definition
36497 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36500 #define OACC_ROUTINE_CLAUSE_MASK \
36501 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36507 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36508 component, which must resolve to a declared namespace-scope
36509 function. The clauses are either processed directly (for a named
36510 function), or defered until the immediatley following declaration
36514 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36515 enum pragma_context context)
36517 bool first_p = parser->oacc_routine == NULL;
36518 location_t loc = pragma_tok->location;
36519 cp_omp_declare_simd_data data;
36522 data.error_seen = false;
36523 data.fndecl_seen = false;
36524 data.tokens = vNULL;
36525 data.clauses = NULL_TREE;
36526 parser->oacc_routine = &data;
36529 tree decl = NULL_TREE;
36530 /* Create a dummy claue, to record location. */
36531 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36533 if (context != pragma_external)
36535 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36536 parser->oacc_routine->error_seen = true;
36537 parser->oacc_routine = NULL;
36541 /* Look for optional '( name )'. */
36542 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36546 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36547 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36548 cp_lexer_consume_token (parser->lexer);
36549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36550 parser->oacc_routine->error_seen = true;
36551 cp_parser_require_pragma_eol (parser, pragma_tok);
36553 error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36554 "%<#pragma acc routine%> not followed by a "
36555 "function declaration or definition");
36557 parser->oacc_routine->error_seen = true;
36561 cp_lexer_consume_token (parser->lexer);
36562 cp_token *token = cp_lexer_peek_token (parser->lexer);
36564 /* We parse the name as an id-expression. If it resolves to
36565 anything other than a non-overloaded function at namespace
36566 scope, it's an error. */
36567 tree id = cp_parser_id_expression (parser,
36568 /*template_keyword_p=*/false,
36569 /*check_dependency_p=*/false,
36570 /*template_p=*/NULL,
36571 /*declarator_p=*/false,
36572 /*optional_p=*/false);
36573 decl = cp_parser_lookup_name_simple (parser, id, token->location);
36574 if (id != error_mark_node && decl == error_mark_node)
36575 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36578 if (decl == error_mark_node
36579 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36581 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36582 parser->oacc_routine = NULL;
36586 /* Build a chain of clauses. */
36587 parser->lexer->in_pragma = true;
36588 tree clauses = NULL_TREE;
36589 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36590 "#pragma acc routine",
36591 cp_lexer_peek_token
36594 /* Force clauses to be non-null, by attaching context to it. */
36595 clauses = tree_cons (c_head, clauses, NULL_TREE);
36597 if (decl && is_overloaded_fn (decl)
36598 && (TREE_CODE (decl) != FUNCTION_DECL
36599 || DECL_FUNCTION_TEMPLATE_P (decl)))
36601 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36602 parser->oacc_routine = NULL;
36606 /* Perhaps we should use the same rule as declarations in different
36608 if (!DECL_NAMESPACE_SCOPE_P (decl))
36610 error_at (loc, "%<#pragma acc routine%> does not refer to a "
36611 "namespace scope function");
36612 parser->oacc_routine = NULL;
36616 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36619 "%<#pragma acc routine%> does not refer to a function");
36620 parser->oacc_routine = NULL;
36624 data.clauses = clauses;
36626 cp_finalize_oacc_routine (parser, decl, false);
36627 data.tokens.release ();
36628 parser->oacc_routine = NULL;
36632 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36633 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36634 cp_lexer_consume_token (parser->lexer);
36635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36636 parser->oacc_routine->error_seen = true;
36637 cp_parser_require_pragma_eol (parser, pragma_tok);
36639 struct cp_token_cache *cp
36640 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36641 parser->oacc_routine->tokens.safe_push (cp);
36644 parser->oacc_routine->clauses = c_head;
36646 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36647 cp_parser_pragma (parser, context, NULL);
36651 /* Create an empty list of clauses. */
36652 parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36654 cp_parser_declaration (parser);
36656 if (parser->oacc_routine
36657 && !parser->oacc_routine->error_seen
36658 && !parser->oacc_routine->fndecl_seen)
36659 error_at (loc, "%<#pragma acc routine%> not followed by a "
36660 "function declaration or definition");
36662 data.tokens.release ();
36663 parser->oacc_routine = NULL;
36668 /* Finalize #pragma acc routine clauses after direct declarator has
36669 been parsed, and put that into "oacc function" attribute. */
36672 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36674 struct cp_token_cache *ce;
36675 cp_omp_declare_simd_data *data = parser->oacc_routine;
36676 tree cl, clauses = parser->oacc_routine->clauses;
36679 loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36681 if ((!data->error_seen && data->fndecl_seen)
36682 || data->tokens.length () != 1)
36684 error_at (loc, "%<#pragma acc routine%> not followed by a "
36685 "function declaration or definition");
36686 data->error_seen = true;
36689 if (data->error_seen)
36692 ce = data->tokens[0];
36694 cp_parser_push_lexer_for_tokens (parser, ce);
36695 parser->lexer->in_pragma = true;
36696 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36698 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36699 cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36700 "#pragma acc routine", pragma_tok);
36701 cp_parser_pop_lexer (parser);
36703 tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36705 /* Force clauses to be non-null, by attaching context to it. */
36706 parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36708 data->fndecl_seen = true;
36712 /* Apply any saved OpenACC routine clauses to a just-parsed
36716 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36718 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36720 tree clauses = parser->oacc_routine->clauses;
36721 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36723 if (parser->oacc_routine->error_seen)
36726 if (fndecl == error_mark_node)
36728 parser->oacc_routine = NULL;
36732 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36734 cp_ensure_no_oacc_routine (parser);
36738 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36741 "%<#pragma acc routine%> not followed by a function "
36742 "declaration or definition");
36743 parser->oacc_routine = NULL;
36746 if (get_oacc_fn_attrib (fndecl))
36748 error_at (loc, "%<#pragma acc routine%> already applied to %D",
36750 parser->oacc_routine = NULL;
36753 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36755 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36756 TREE_USED (fndecl) ? "use" : "definition");
36757 parser->oacc_routine = NULL;
36760 /* Process for function attrib */
36761 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36762 replace_oacc_fn_attrib (fndecl, dims);
36764 /* Add an "omp target" attribute. */
36765 DECL_ATTRIBUTES (fndecl)
36766 = tree_cons (get_identifier ("omp declare target"),
36767 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36771 /* Main entry point to OpenMP statement pragmas. */
36774 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36777 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36778 omp_clause_mask mask (0);
36780 switch (cp_parser_pragma_kind (pragma_tok))
36782 case PRAGMA_OACC_ATOMIC:
36783 cp_parser_omp_atomic (parser, pragma_tok);
36785 case PRAGMA_OACC_CACHE:
36786 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36788 case PRAGMA_OACC_DATA:
36789 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
36791 case PRAGMA_OACC_ENTER_DATA:
36792 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36794 case PRAGMA_OACC_EXIT_DATA:
36795 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36797 case PRAGMA_OACC_HOST_DATA:
36798 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
36800 case PRAGMA_OACC_KERNELS:
36801 case PRAGMA_OACC_PARALLEL:
36802 strcpy (p_name, "#pragma acc");
36803 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
36806 case PRAGMA_OACC_LOOP:
36807 strcpy (p_name, "#pragma acc");
36808 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
36811 case PRAGMA_OACC_UPDATE:
36812 stmt = cp_parser_oacc_update (parser, pragma_tok);
36814 case PRAGMA_OACC_WAIT:
36815 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36817 case PRAGMA_OMP_ATOMIC:
36818 cp_parser_omp_atomic (parser, pragma_tok);
36820 case PRAGMA_OMP_CRITICAL:
36821 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
36823 case PRAGMA_OMP_DISTRIBUTE:
36824 strcpy (p_name, "#pragma omp");
36825 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
36828 case PRAGMA_OMP_FOR:
36829 strcpy (p_name, "#pragma omp");
36830 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
36833 case PRAGMA_OMP_MASTER:
36834 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
36836 case PRAGMA_OMP_PARALLEL:
36837 strcpy (p_name, "#pragma omp");
36838 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
36841 case PRAGMA_OMP_SECTIONS:
36842 strcpy (p_name, "#pragma omp");
36843 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36845 case PRAGMA_OMP_SIMD:
36846 strcpy (p_name, "#pragma omp");
36847 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
36850 case PRAGMA_OMP_SINGLE:
36851 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
36853 case PRAGMA_OMP_TASK:
36854 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
36856 case PRAGMA_OMP_TASKGROUP:
36857 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
36859 case PRAGMA_OMP_TASKLOOP:
36860 strcpy (p_name, "#pragma omp");
36861 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
36864 case PRAGMA_OMP_TEAMS:
36865 strcpy (p_name, "#pragma omp");
36866 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
36870 gcc_unreachable ();
36873 protected_set_expr_location (stmt, pragma_tok->location);
36876 /* Transactional Memory parsing routines. */
36878 /* Parse a transaction attribute.
36884 We use this instead of cp_parser_attributes_opt for transactions to avoid
36885 the pedwarn in C++98 mode. */
36888 cp_parser_txn_attribute_opt (cp_parser *parser)
36891 tree attr_name, attr = NULL;
36893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36894 return cp_parser_attributes_opt (parser);
36896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36898 cp_lexer_consume_token (parser->lexer);
36899 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36902 token = cp_lexer_peek_token (parser->lexer);
36903 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36905 token = cp_lexer_consume_token (parser->lexer);
36907 attr_name = (token->type == CPP_KEYWORD
36908 /* For keywords, use the canonical spelling,
36909 not the parsed identifier. */
36910 ? ridpointers[(int) token->keyword]
36912 attr = build_tree_list (attr_name, NULL_TREE);
36915 cp_parser_error (parser, "expected identifier");
36917 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36919 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36923 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36925 transaction-statement:
36926 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36928 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36932 cp_parser_transaction (cp_parser *parser, cp_token *token)
36934 unsigned char old_in = parser->in_transaction;
36935 unsigned char this_in = 1, new_in;
36936 enum rid keyword = token->keyword;
36937 tree stmt, attrs, noex;
36939 cp_lexer_consume_token (parser->lexer);
36941 if (keyword == RID_TRANSACTION_RELAXED
36942 || keyword == RID_SYNCHRONIZED)
36943 this_in |= TM_STMT_ATTR_RELAXED;
36946 attrs = cp_parser_txn_attribute_opt (parser);
36948 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36951 /* Parse a noexcept specification. */
36952 if (keyword == RID_ATOMIC_NOEXCEPT)
36953 noex = boolean_true_node;
36954 else if (keyword == RID_ATOMIC_CANCEL)
36956 /* cancel-and-throw is unimplemented. */
36957 sorry ("atomic_cancel");
36961 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36963 /* Keep track if we're in the lexical scope of an outer transaction. */
36964 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36966 stmt = begin_transaction_stmt (token->location, NULL, this_in);
36968 parser->in_transaction = new_in;
36969 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36970 parser->in_transaction = old_in;
36972 finish_transaction_stmt (stmt, NULL, this_in, noex);
36977 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36979 transaction-expression:
36980 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36981 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36985 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36987 unsigned char old_in = parser->in_transaction;
36988 unsigned char this_in = 1;
36992 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36994 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36995 || keyword == RID_TRANSACTION_RELAXED);
36999 keyword == RID_TRANSACTION_RELAXED
37000 ? G_("%<__transaction_relaxed%> without transactional memory "
37002 : G_("%<__transaction_atomic%> without transactional memory "
37003 "support enabled"));
37005 token = cp_parser_require_keyword (parser, keyword,
37006 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37007 : RT_TRANSACTION_RELAXED));
37008 gcc_assert (token != NULL);
37010 if (keyword == RID_TRANSACTION_RELAXED)
37011 this_in |= TM_STMT_ATTR_RELAXED;
37013 /* Set this early. This might mean that we allow transaction_cancel in
37014 an expression that we find out later actually has to be a constexpr.
37015 However, we expect that cxx_constant_value will be able to deal with
37016 this; also, if the noexcept has no constexpr, then what we parse next
37017 really is a transaction's body. */
37018 parser->in_transaction = this_in;
37020 /* Parse a noexcept specification. */
37021 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37024 if (!noex || !noex_expr
37025 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37027 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37029 expr = cp_parser_expression (parser);
37030 expr = finish_parenthesized_expr (expr);
37032 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37036 /* The only expression that is available got parsed for the noexcept
37037 already. noexcept is true then. */
37039 noex = boolean_true_node;
37042 expr = build_transaction_expr (token->location, expr, this_in, noex);
37043 parser->in_transaction = old_in;
37045 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37046 return error_mark_node;
37048 return (flag_tm ? expr : error_mark_node);
37051 /* Parse a function-transaction-block.
37053 function-transaction-block:
37054 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37056 __transaction_atomic txn-attribute[opt] function-try-block
37057 __transaction_relaxed ctor-initializer[opt] function-body
37058 __transaction_relaxed function-try-block
37062 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37064 unsigned char old_in = parser->in_transaction;
37065 unsigned char new_in = 1;
37066 tree compound_stmt, stmt, attrs;
37067 bool ctor_initializer_p;
37070 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37071 || keyword == RID_TRANSACTION_RELAXED);
37072 token = cp_parser_require_keyword (parser, keyword,
37073 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37074 : RT_TRANSACTION_RELAXED));
37075 gcc_assert (token != NULL);
37077 if (keyword == RID_TRANSACTION_RELAXED)
37078 new_in |= TM_STMT_ATTR_RELAXED;
37081 attrs = cp_parser_txn_attribute_opt (parser);
37083 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37086 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37088 parser->in_transaction = new_in;
37090 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37091 ctor_initializer_p = cp_parser_function_try_block (parser);
37093 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37094 (parser, /*in_function_try_block=*/false);
37096 parser->in_transaction = old_in;
37098 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37100 return ctor_initializer_p;
37103 /* Parse a __transaction_cancel statement.
37106 __transaction_cancel txn-attribute[opt] ;
37107 __transaction_cancel txn-attribute[opt] throw-expression ;
37109 ??? Cancel and throw is not yet implemented. */
37112 cp_parser_transaction_cancel (cp_parser *parser)
37115 bool is_outer = false;
37118 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37119 RT_TRANSACTION_CANCEL);
37120 gcc_assert (token != NULL);
37122 attrs = cp_parser_txn_attribute_opt (parser);
37124 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37126 /* ??? Parse cancel-and-throw here. */
37128 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37132 error_at (token->location, "%<__transaction_cancel%> without "
37133 "transactional memory support enabled");
37134 return error_mark_node;
37136 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37138 error_at (token->location, "%<__transaction_cancel%> within a "
37139 "%<__transaction_relaxed%>");
37140 return error_mark_node;
37144 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37145 && !is_tm_may_cancel_outer (current_function_decl))
37147 error_at (token->location, "outer %<__transaction_cancel%> not "
37148 "within outer %<__transaction_atomic%>");
37149 error_at (token->location,
37150 " or a %<transaction_may_cancel_outer%> function");
37151 return error_mark_node;
37154 else if (parser->in_transaction == 0)
37156 error_at (token->location, "%<__transaction_cancel%> not within "
37157 "%<__transaction_atomic%>");
37158 return error_mark_node;
37161 stmt = build_tm_abort_call (token->location, is_outer);
37169 static GTY (()) cp_parser *the_parser;
37172 /* Special handling for the first token or line in the file. The first
37173 thing in the file might be #pragma GCC pch_preprocess, which loads a
37174 PCH file, which is a GC collection point. So we need to handle this
37175 first pragma without benefit of an existing lexer structure.
37177 Always returns one token to the caller in *FIRST_TOKEN. This is
37178 either the true first token of the file, or the first token after
37179 the initial pragma. */
37182 cp_parser_initial_pragma (cp_token *first_token)
37186 cp_lexer_get_preprocessor_token (NULL, first_token);
37187 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37190 cp_lexer_get_preprocessor_token (NULL, first_token);
37191 if (first_token->type == CPP_STRING)
37193 name = first_token->u.value;
37195 cp_lexer_get_preprocessor_token (NULL, first_token);
37196 if (first_token->type != CPP_PRAGMA_EOL)
37197 error_at (first_token->location,
37198 "junk at end of %<#pragma GCC pch_preprocess%>");
37201 error_at (first_token->location, "expected string literal");
37203 /* Skip to the end of the pragma. */
37204 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37205 cp_lexer_get_preprocessor_token (NULL, first_token);
37207 /* Now actually load the PCH file. */
37209 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37211 /* Read one more token to return to our caller. We have to do this
37212 after reading the PCH file in, since its pointers have to be
37214 cp_lexer_get_preprocessor_token (NULL, first_token);
37217 /* Parses the grainsize pragma for the _Cilk_for statement.
37219 #pragma cilk grainsize = <VALUE>. */
37222 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37224 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37226 tree exp = cp_parser_binary_expression (parser, false, false,
37227 PREC_NOT_OPERATOR, NULL);
37228 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37229 if (!exp || exp == error_mark_node)
37231 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37235 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37237 cp_parser_cilk_for (parser, exp, if_p);
37239 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37240 "%<#pragma cilk grainsize%> is not followed by "
37244 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37247 /* Normal parsing of a pragma token. Here we can (and must) use the
37251 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37253 cp_token *pragma_tok;
37258 pragma_tok = cp_lexer_consume_token (parser->lexer);
37259 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37260 parser->lexer->in_pragma = true;
37262 id = cp_parser_pragma_kind (pragma_tok);
37263 if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
37264 cp_ensure_no_omp_declare_simd (parser);
37267 case PRAGMA_GCC_PCH_PREPROCESS:
37268 error_at (pragma_tok->location,
37269 "%<#pragma GCC pch_preprocess%> must be first");
37272 case PRAGMA_OMP_BARRIER:
37275 case pragma_compound:
37276 cp_parser_omp_barrier (parser, pragma_tok);
37279 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37280 "used in compound statements");
37287 case PRAGMA_OMP_FLUSH:
37290 case pragma_compound:
37291 cp_parser_omp_flush (parser, pragma_tok);
37294 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37295 "used in compound statements");
37302 case PRAGMA_OMP_TASKWAIT:
37305 case pragma_compound:
37306 cp_parser_omp_taskwait (parser, pragma_tok);
37309 error_at (pragma_tok->location,
37310 "%<#pragma omp taskwait%> may only be "
37311 "used in compound statements");
37318 case PRAGMA_OMP_TASKYIELD:
37321 case pragma_compound:
37322 cp_parser_omp_taskyield (parser, pragma_tok);
37325 error_at (pragma_tok->location,
37326 "%<#pragma omp taskyield%> may only be "
37327 "used in compound statements");
37334 case PRAGMA_OMP_CANCEL:
37337 case pragma_compound:
37338 cp_parser_omp_cancel (parser, pragma_tok);
37341 error_at (pragma_tok->location,
37342 "%<#pragma omp cancel%> may only be "
37343 "used in compound statements");
37350 case PRAGMA_OMP_CANCELLATION_POINT:
37353 case pragma_compound:
37354 cp_parser_omp_cancellation_point (parser, pragma_tok);
37357 error_at (pragma_tok->location,
37358 "%<#pragma omp cancellation point%> may only be "
37359 "used in compound statements");
37366 case PRAGMA_OMP_THREADPRIVATE:
37367 cp_parser_omp_threadprivate (parser, pragma_tok);
37370 case PRAGMA_OMP_DECLARE_REDUCTION:
37371 cp_parser_omp_declare (parser, pragma_tok, context);
37374 case PRAGMA_OACC_DECLARE:
37375 cp_parser_oacc_declare (parser, pragma_tok);
37378 case PRAGMA_OACC_ROUTINE:
37379 cp_parser_oacc_routine (parser, pragma_tok, context);
37382 case PRAGMA_OACC_ATOMIC:
37383 case PRAGMA_OACC_CACHE:
37384 case PRAGMA_OACC_DATA:
37385 case PRAGMA_OACC_ENTER_DATA:
37386 case PRAGMA_OACC_EXIT_DATA:
37387 case PRAGMA_OACC_HOST_DATA:
37388 case PRAGMA_OACC_KERNELS:
37389 case PRAGMA_OACC_PARALLEL:
37390 case PRAGMA_OACC_LOOP:
37391 case PRAGMA_OACC_UPDATE:
37392 case PRAGMA_OACC_WAIT:
37393 case PRAGMA_OMP_ATOMIC:
37394 case PRAGMA_OMP_CRITICAL:
37395 case PRAGMA_OMP_DISTRIBUTE:
37396 case PRAGMA_OMP_FOR:
37397 case PRAGMA_OMP_MASTER:
37398 case PRAGMA_OMP_PARALLEL:
37399 case PRAGMA_OMP_SECTIONS:
37400 case PRAGMA_OMP_SIMD:
37401 case PRAGMA_OMP_SINGLE:
37402 case PRAGMA_OMP_TASK:
37403 case PRAGMA_OMP_TASKGROUP:
37404 case PRAGMA_OMP_TASKLOOP:
37405 case PRAGMA_OMP_TEAMS:
37406 if (context != pragma_stmt && context != pragma_compound)
37408 stmt = push_omp_privatization_clauses (false);
37409 cp_parser_omp_construct (parser, pragma_tok, if_p);
37410 pop_omp_privatization_clauses (stmt);
37413 case PRAGMA_OMP_ORDERED:
37414 stmt = push_omp_privatization_clauses (false);
37415 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37416 pop_omp_privatization_clauses (stmt);
37419 case PRAGMA_OMP_TARGET:
37420 stmt = push_omp_privatization_clauses (false);
37421 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37422 pop_omp_privatization_clauses (stmt);
37425 case PRAGMA_OMP_END_DECLARE_TARGET:
37426 cp_parser_omp_end_declare_target (parser, pragma_tok);
37429 case PRAGMA_OMP_SECTION:
37430 error_at (pragma_tok->location,
37431 "%<#pragma omp section%> may only be used in "
37432 "%<#pragma omp sections%> construct");
37437 if (context == pragma_external)
37439 error_at (pragma_tok->location,
37440 "%<#pragma GCC ivdep%> must be inside a function");
37443 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37445 tok = cp_lexer_peek_token (the_parser->lexer);
37446 if (tok->type != CPP_KEYWORD
37447 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37448 && tok->keyword != RID_DO))
37450 cp_parser_error (parser, "for, while or do statement expected");
37453 cp_parser_iteration_statement (parser, if_p, true);
37457 case PRAGMA_CILK_SIMD:
37458 if (context == pragma_external)
37460 error_at (pragma_tok->location,
37461 "%<#pragma simd%> must be inside a function");
37464 stmt = push_omp_privatization_clauses (false);
37465 cp_parser_cilk_simd (parser, pragma_tok, if_p);
37466 pop_omp_privatization_clauses (stmt);
37469 case PRAGMA_CILK_GRAINSIZE:
37470 if (context == pragma_external)
37472 error_at (pragma_tok->location,
37473 "%<#pragma cilk grainsize%> must be inside a function");
37477 /* Ignore the pragma if Cilk Plus is not enabled. */
37480 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37485 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37486 "%<#pragma cilk grainsize%>");
37491 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37492 c_invoke_pragma_handler (id);
37496 cp_parser_error (parser, "expected declaration specifiers");
37500 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37504 /* The interface the pragma parsers have to the lexer. */
37507 pragma_lex (tree *value, location_t *loc)
37509 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37510 enum cpp_ttype ret = tok->type;
37512 *value = tok->u.value;
37514 *loc = tok->location;
37516 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37518 else if (ret == CPP_STRING)
37519 *value = cp_parser_string_literal (the_parser, false, false);
37522 if (ret == CPP_KEYWORD)
37524 cp_lexer_consume_token (the_parser->lexer);
37531 /* External interface. */
37533 /* Parse one entire translation unit. */
37536 c_parse_file (void)
37538 static bool already_called = false;
37540 if (already_called)
37541 fatal_error (input_location,
37542 "inter-module optimizations not implemented for C++");
37543 already_called = true;
37545 the_parser = cp_parser_new ();
37546 push_deferring_access_checks (flag_access_control
37547 ? dk_no_deferred : dk_no_check);
37548 cp_parser_translation_unit (the_parser);
37552 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37553 vectorlength clause:
37555 vectorlength ( constant-expression ) */
37558 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37561 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37563 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37564 safelen clause. Thus, vectorlength is represented as OMP 4.0
37565 safelen. For SIMD-enabled function it is represented by OMP 4.0
37568 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37571 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37574 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37575 return error_mark_node;
37577 expr = cp_parser_constant_expression (parser);
37578 expr = maybe_constant_value (expr);
37580 /* If expr == error_mark_node, then don't emit any errors nor
37581 create a clause. if any of the above functions returns
37582 error mark node then they would have emitted an error message. */
37583 if (expr == error_mark_node)
37585 else if (!TREE_TYPE (expr)
37586 || !TREE_CONSTANT (expr)
37587 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37588 error_at (loc, "vectorlength must be an integer constant");
37589 else if (TREE_CONSTANT (expr)
37590 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37591 error_at (loc, "vectorlength must be a power of 2");
37597 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37598 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37599 OMP_CLAUSE_CHAIN (c) = clauses;
37604 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37605 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37606 OMP_CLAUSE_CHAIN (c) = clauses;
37611 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37612 return error_mark_node;
37616 /* Handles the Cilk Plus #pragma simd linear clause.
37618 linear ( simd-linear-variable-list )
37620 simd-linear-variable-list:
37621 simd-linear-variable
37622 simd-linear-variable-list , simd-linear-variable
37624 simd-linear-variable:
37626 id-expression : simd-linear-step
37629 conditional-expression */
37632 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37634 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37636 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37640 cp_parser_error (parser, "expected identifier");
37641 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37642 return error_mark_node;
37645 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37646 parser->colon_corrects_to_scope_p = false;
37649 cp_token *token = cp_lexer_peek_token (parser->lexer);
37650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37652 cp_parser_error (parser, "expected variable-name");
37653 clauses = error_mark_node;
37657 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37659 tree decl = cp_parser_lookup_name_simple (parser, var_name,
37661 if (decl == error_mark_node)
37663 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37665 clauses = error_mark_node;
37669 tree e = NULL_TREE;
37670 tree step_size = integer_one_node;
37672 /* If present, parse the linear step. Otherwise, assume the default
37674 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37676 cp_lexer_consume_token (parser->lexer);
37678 e = cp_parser_assignment_expression (parser);
37679 e = maybe_constant_value (e);
37681 if (e == error_mark_node)
37683 /* If an error has occurred, then the whole pragma is
37684 considered ill-formed. Thus, no reason to keep
37686 clauses = error_mark_node;
37689 else if (type_dependent_expression_p (e)
37690 || value_dependent_expression_p (e)
37692 && INTEGRAL_TYPE_P (TREE_TYPE (e))
37693 && (TREE_CONSTANT (e)
37697 cp_parser_error (parser,
37698 "step size must be an integer constant "
37699 "expression or an integer variable");
37702 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37703 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37704 OMP_CLAUSE_DECL (l) = decl;
37705 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37706 OMP_CLAUSE_CHAIN (l) = clauses;
37709 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37710 cp_lexer_consume_token (parser->lexer);
37711 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37715 error_at (cp_lexer_peek_token (parser->lexer)->location,
37716 "expected %<,%> or %<)%> after %qE", decl);
37717 clauses = error_mark_node;
37721 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37722 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37726 /* Returns the name of the next clause. If the clause is not
37727 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37728 token is not consumed. Otherwise, the appropriate enum from the
37729 pragma_simd_clause is returned and the token is consumed. */
37731 static pragma_omp_clause
37732 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37734 pragma_omp_clause clause_type;
37735 cp_token *token = cp_lexer_peek_token (parser->lexer);
37737 if (token->keyword == RID_PRIVATE)
37738 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37739 else if (!token->u.value || token->type != CPP_NAME)
37740 return PRAGMA_CILK_CLAUSE_NONE;
37741 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37742 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37743 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37744 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37745 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37746 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37747 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37748 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37749 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37750 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37752 return PRAGMA_CILK_CLAUSE_NONE;
37754 cp_lexer_consume_token (parser->lexer);
37755 return clause_type;
37758 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37761 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37763 tree clauses = NULL_TREE;
37765 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37766 && clauses != error_mark_node)
37768 pragma_omp_clause c_kind;
37769 c_kind = cp_parser_cilk_simd_clause_name (parser);
37770 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37771 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37772 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37773 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37774 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37775 /* Use the OpenMP 4.0 equivalent function. */
37776 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37777 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37778 /* Use the OpenMP 4.0 equivalent function. */
37779 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37781 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37782 /* Use the OMP 4.0 equivalent function. */
37783 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37785 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37786 /* Use the OMP 4.0 equivalent function. */
37787 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37790 clauses = error_mark_node;
37791 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37796 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37798 if (clauses == error_mark_node)
37799 return error_mark_node;
37801 return c_finish_cilk_clauses (clauses);
37804 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37807 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
37809 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37811 if (clauses == error_mark_node)
37814 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37816 error_at (cp_lexer_peek_token (parser->lexer)->location,
37817 "for statement expected");
37821 tree sb = begin_omp_structured_block ();
37822 int save = cp_parser_begin_omp_structured_block (parser);
37823 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
37825 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37826 cp_parser_end_omp_structured_block (parser, save);
37827 add_stmt (finish_omp_structured_block (sb));
37830 /* Main entry-point for parsing Cilk Plus _Cilk_for
37831 loops. The return value is error_mark_node
37832 when errors happen and CILK_FOR tree on success. */
37835 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
37837 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37838 gcc_unreachable ();
37840 tree sb = begin_omp_structured_block ();
37841 int save = cp_parser_begin_omp_structured_block (parser);
37843 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37844 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37845 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37846 clauses = finish_omp_clauses (clauses, false);
37848 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
37850 cpp_validate_cilk_plus_loop (ret);
37852 ret = error_mark_node;
37854 cp_parser_end_omp_structured_block (parser, save);
37855 add_stmt (finish_omp_structured_block (sb));
37859 /* Create an identifier for a generic parameter type (a synthesized
37860 template parameter implied by `auto' or a concept identifier). */
37862 static GTY(()) int generic_parm_count;
37864 make_generic_type_name ()
37867 sprintf (buf, "auto:%d", ++generic_parm_count);
37868 return get_identifier (buf);
37871 /* Predicate that behaves as is_auto_or_concept but matches the parent
37872 node of the generic type rather than the generic type itself. This
37873 allows for type transformation in add_implicit_template_parms. */
37876 tree_type_is_auto_or_concept (const_tree t)
37878 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37881 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37882 (creating a new template parameter list if necessary). Returns the newly
37883 created template type parm. */
37886 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
37888 gcc_assert (current_binding_level->kind == sk_function_parms);
37890 /* Before committing to modifying any scope, if we're in an
37891 implicit template scope, and we're trying to synthesize a
37892 constrained parameter, try to find a previous parameter with
37893 the same name. This is the same-type rule for abbreviated
37894 function templates.
37896 NOTE: We can generate implicit parameters when tentatively
37897 parsing a nested name specifier, only to reject that parse
37898 later. However, matching the same template-id as part of a
37899 direct-declarator should generate an identical template
37900 parameter, so this rule will merge them. */
37901 if (parser->implicit_template_scope && constr)
37903 tree t = parser->implicit_template_parms;
37906 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37908 tree d = TREE_VALUE (t);
37909 if (TREE_CODE (d) == PARM_DECL)
37910 /* Return the TEMPLATE_PARM_INDEX. */
37911 d = DECL_INITIAL (d);
37914 t = TREE_CHAIN (t);
37918 /* We are either continuing a function template that already contains implicit
37919 template parameters, creating a new fully-implicit function template, or
37920 extending an existing explicit function template with implicit template
37923 cp_binding_level *const entry_scope = current_binding_level;
37925 bool become_template = false;
37926 cp_binding_level *parent_scope = 0;
37928 if (parser->implicit_template_scope)
37930 gcc_assert (parser->implicit_template_parms);
37932 current_binding_level = parser->implicit_template_scope;
37936 /* Roll back to the existing template parameter scope (in the case of
37937 extending an explicit function template) or introduce a new template
37938 parameter scope ahead of the function parameter scope (or class scope
37939 in the case of out-of-line member definitions). The function scope is
37940 added back after template parameter synthesis below. */
37942 cp_binding_level *scope = entry_scope;
37944 while (scope->kind == sk_function_parms)
37946 parent_scope = scope;
37947 scope = scope->level_chain;
37949 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37951 /* If not defining a class, then any class scope is a scope level in
37952 an out-of-line member definition. In this case simply wind back
37953 beyond the first such scope to inject the template parameter list.
37954 Otherwise wind back to the class being defined. The latter can
37955 occur in class member friend declarations such as:
37961 friend void A::foo (auto);
37964 The template parameter list synthesized for the friend declaration
37965 must be injected in the scope of 'B'. This can also occur in
37966 erroneous cases such as:
37972 void B::foo (auto) {}
37975 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37976 but, nevertheless, the template parameter list synthesized for the
37977 declarator should be injected into the scope of 'A' as if the
37978 ill-formed template was specified explicitly. */
37980 while (scope->kind == sk_class && !scope->defining_class_p)
37982 parent_scope = scope;
37983 scope = scope->level_chain;
37987 current_binding_level = scope;
37989 if (scope->kind != sk_template_parms
37990 || !function_being_declared_is_template_p (parser))
37992 /* Introduce a new template parameter list for implicit template
37995 become_template = true;
37997 parser->implicit_template_scope
37998 = begin_scope (sk_template_parms, NULL);
38000 ++processing_template_decl;
38002 parser->fully_implicit_function_template_p = true;
38003 ++parser->num_template_parameter_lists;
38007 /* Synthesize implicit template parameters at the end of the explicit
38008 template parameter list. */
38010 gcc_assert (current_template_parms);
38012 parser->implicit_template_scope = scope;
38014 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38015 parser->implicit_template_parms
38016 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38020 /* Synthesize a new template parameter and track the current template
38021 parameter chain with implicit_template_parms. */
38023 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38024 tree synth_id = make_generic_type_name ();
38025 tree synth_tmpl_parm;
38026 bool non_type = false;
38028 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38030 = finish_template_type_parm (class_type_node, synth_id);
38031 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38033 = finish_constrained_template_template_parm (proto, synth_id);
38036 synth_tmpl_parm = copy_decl (proto);
38037 DECL_NAME (synth_tmpl_parm) = synth_id;
38041 // Attach the constraint to the parm before processing.
38042 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38043 TREE_TYPE (node) = constr;
38045 = process_template_parm (parser->implicit_template_parms,
38048 /*non_type=*/non_type,
38049 /*param_pack=*/false);
38051 // Chain the new parameter to the list of implicit parameters.
38052 if (parser->implicit_template_parms)
38053 parser->implicit_template_parms
38054 = TREE_CHAIN (parser->implicit_template_parms);
38056 parser->implicit_template_parms = new_parm;
38058 tree new_decl = getdecls ();
38060 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38061 new_decl = DECL_INITIAL (new_decl);
38063 /* If creating a fully implicit function template, start the new implicit
38064 template parameter list with this synthesized type, otherwise grow the
38065 current template parameter list. */
38067 if (become_template)
38069 parent_scope->level_chain = current_binding_level;
38071 tree new_parms = make_tree_vec (1);
38072 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38073 current_template_parms = tree_cons (size_int (processing_template_decl),
38074 new_parms, current_template_parms);
38078 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38079 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38080 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38081 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38084 // If the new parameter was constrained, we need to add that to the
38085 // constraints in the template parameter list.
38086 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38088 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38089 reqs = conjoin_constraints (reqs, req);
38090 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38093 current_binding_level = entry_scope;
38098 /* Finish the declaration of a fully implicit function template. Such a
38099 template has no explicit template parameter list so has not been through the
38100 normal template head and tail processing. synthesize_implicit_template_parm
38101 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38102 provided if the declaration is a class member such that its template
38103 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38104 form is returned. Otherwise NULL_TREE is returned. */
38107 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38109 gcc_assert (parser->fully_implicit_function_template_p);
38111 if (member_decl_opt && member_decl_opt != error_mark_node
38112 && DECL_VIRTUAL_P (member_decl_opt))
38114 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38115 "implicit templates may not be %<virtual%>");
38116 DECL_VIRTUAL_P (member_decl_opt) = false;
38119 if (member_decl_opt)
38120 member_decl_opt = finish_member_template_decl (member_decl_opt);
38121 end_template_decl ();
38123 parser->fully_implicit_function_template_p = false;
38124 --parser->num_template_parameter_lists;
38126 return member_decl_opt;
38129 #include "gt-cp-parser.h"