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 /* Finish the compound-statement. */
10774 finish_compound_stmt (compound_stmt);
10775 /* Consume the `}'. */
10776 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10778 return compound_stmt;
10781 /* Parse an (optional) statement-seq.
10785 statement-seq [opt] statement */
10788 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10790 /* Scan statements until there aren't any more. */
10793 cp_token *token = cp_lexer_peek_token (parser->lexer);
10795 /* If we are looking at a `}', then we have run out of
10796 statements; the same is true if we have reached the end
10797 of file, or have stumbled upon a stray '@end'. */
10798 if (token->type == CPP_CLOSE_BRACE
10799 || token->type == CPP_EOF
10800 || token->type == CPP_PRAGMA_EOL
10801 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10804 /* If we are in a compound statement and find 'else' then
10805 something went wrong. */
10806 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10808 if (parser->in_statement & IN_IF_STMT)
10812 token = cp_lexer_consume_token (parser->lexer);
10813 error_at (token->location, "%<else%> without a previous %<if%>");
10817 /* Parse the statement. */
10818 cp_parser_statement (parser, in_statement_expr, true, NULL);
10822 /* Parse a selection-statement.
10824 selection-statement:
10825 if ( condition ) statement
10826 if ( condition ) statement else statement
10827 switch ( condition ) statement
10829 Returns the new IF_STMT or SWITCH_STMT.
10831 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10832 is a (possibly labeled) if statement which is not enclosed in
10833 braces and has an else clause. This is used to implement
10836 CHAIN is a vector of if-else-if conditions. This is used to implement
10837 -Wduplicated-cond. */
10840 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10845 token_indent_info guard_tinfo;
10850 /* Peek at the next token. */
10851 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10852 guard_tinfo = get_token_indent_info (token);
10854 /* See what kind of keyword it is. */
10855 keyword = token->keyword;
10864 /* Look for the `('. */
10865 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10867 cp_parser_skip_to_end_of_statement (parser);
10868 return error_mark_node;
10871 /* Begin the selection-statement. */
10872 if (keyword == RID_IF)
10873 statement = begin_if_stmt ();
10875 statement = begin_switch_stmt ();
10877 /* Parse the condition. */
10878 condition = cp_parser_condition (parser);
10879 /* Look for the `)'. */
10880 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10881 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10882 /*consume_paren=*/true);
10884 if (keyword == RID_IF)
10887 unsigned char in_statement;
10889 /* Add the condition. */
10890 finish_if_stmt_cond (condition, statement);
10892 if (warn_duplicated_cond)
10893 warn_duplicated_cond_add_or_warn (token->location, condition,
10896 /* Parse the then-clause. */
10897 in_statement = parser->in_statement;
10898 parser->in_statement |= IN_IF_STMT;
10899 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10901 parser->in_statement = in_statement;
10903 finish_then_clause (statement);
10905 /* If the next token is `else', parse the else-clause. */
10906 if (cp_lexer_next_token_is_keyword (parser->lexer,
10910 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10911 /* Consume the `else' keyword. */
10912 cp_lexer_consume_token (parser->lexer);
10913 if (warn_duplicated_cond)
10915 if (cp_lexer_next_token_is_keyword (parser->lexer,
10919 /* We've got "if (COND) else if (COND2)". Start
10920 the condition chain and add COND as the first
10922 chain = new vec<tree> ();
10923 if (!CONSTANT_CLASS_P (condition)
10924 && !TREE_SIDE_EFFECTS (condition))
10926 /* Wrap it in a NOP_EXPR so that we can set the
10927 location of the condition. */
10928 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10930 SET_EXPR_LOCATION (e, token->location);
10931 chain->safe_push (e);
10934 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10937 /* This is if-else without subsequent if. Zap the
10938 condition chain; we would have already warned at
10944 begin_else_clause (statement);
10945 /* Parse the else-clause. */
10946 cp_parser_implicitly_scoped_statement (parser, NULL,
10947 guard_tinfo, chain);
10949 finish_else_clause (statement);
10951 /* If we are currently parsing a then-clause, then
10952 IF_P will not be NULL. We set it to true to
10953 indicate that this if statement has an else clause.
10954 This may trigger the Wparentheses warning below
10955 when we get back up to the parent if statement. */
10961 /* This if statement does not have an else clause. If
10962 NESTED_IF is true, then the then-clause has an if
10963 statement which does have an else clause. We warn
10964 about the potential ambiguity. */
10966 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10967 "suggest explicit braces to avoid ambiguous"
10969 if (warn_duplicated_cond)
10971 /* We don't need the condition chain anymore. */
10977 /* Now we're all done with the if-statement. */
10978 finish_if_stmt (statement);
10982 bool in_switch_statement_p;
10983 unsigned char in_statement;
10985 /* Add the condition. */
10986 finish_switch_cond (condition, statement);
10988 /* Parse the body of the switch-statement. */
10989 in_switch_statement_p = parser->in_switch_statement_p;
10990 in_statement = parser->in_statement;
10991 parser->in_switch_statement_p = true;
10992 parser->in_statement |= IN_SWITCH_STMT;
10993 cp_parser_implicitly_scoped_statement (parser, NULL,
10995 parser->in_switch_statement_p = in_switch_statement_p;
10996 parser->in_statement = in_statement;
10998 /* Now we're all done with the switch-statement. */
10999 finish_switch_stmt (statement);
11007 cp_parser_error (parser, "expected selection-statement");
11008 return error_mark_node;
11012 /* Parse a condition.
11016 type-specifier-seq declarator = initializer-clause
11017 type-specifier-seq declarator braced-init-list
11022 type-specifier-seq declarator asm-specification [opt]
11023 attributes [opt] = assignment-expression
11025 Returns the expression that should be tested. */
11028 cp_parser_condition (cp_parser* parser)
11030 cp_decl_specifier_seq type_specifiers;
11031 const char *saved_message;
11032 int declares_class_or_enum;
11034 /* Try the declaration first. */
11035 cp_parser_parse_tentatively (parser);
11036 /* New types are not allowed in the type-specifier-seq for a
11038 saved_message = parser->type_definition_forbidden_message;
11039 parser->type_definition_forbidden_message
11040 = G_("types may not be defined in conditions");
11041 /* Parse the type-specifier-seq. */
11042 cp_parser_decl_specifier_seq (parser,
11043 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11045 &declares_class_or_enum);
11046 /* Restore the saved message. */
11047 parser->type_definition_forbidden_message = saved_message;
11048 /* If all is well, we might be looking at a declaration. */
11049 if (!cp_parser_error_occurred (parser))
11052 tree asm_specification;
11054 cp_declarator *declarator;
11055 tree initializer = NULL_TREE;
11057 /* Parse the declarator. */
11058 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11059 /*ctor_dtor_or_conv_p=*/NULL,
11060 /*parenthesized_p=*/NULL,
11061 /*member_p=*/false,
11062 /*friend_p=*/false);
11063 /* Parse the attributes. */
11064 attributes = cp_parser_attributes_opt (parser);
11065 /* Parse the asm-specification. */
11066 asm_specification = cp_parser_asm_specification_opt (parser);
11067 /* If the next token is not an `=' or '{', then we might still be
11068 looking at an expression. For example:
11072 looks like a decl-specifier-seq and a declarator -- but then
11073 there is no `=', so this is an expression. */
11074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11075 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11076 cp_parser_simulate_error (parser);
11078 /* If we did see an `=' or '{', then we are looking at a declaration
11080 if (cp_parser_parse_definitely (parser))
11083 bool non_constant_p;
11084 bool flags = LOOKUP_ONLYCONVERTING;
11086 /* Create the declaration. */
11087 decl = start_decl (declarator, &type_specifiers,
11088 /*initialized_p=*/true,
11089 attributes, /*prefix_attributes=*/NULL_TREE,
11092 /* Parse the initializer. */
11093 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11095 initializer = cp_parser_braced_list (parser, &non_constant_p);
11096 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11101 /* Consume the `='. */
11102 cp_parser_require (parser, CPP_EQ, RT_EQ);
11103 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11105 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11106 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11108 /* Process the initializer. */
11109 cp_finish_decl (decl,
11110 initializer, !non_constant_p,
11115 pop_scope (pushed_scope);
11117 return convert_from_reference (decl);
11120 /* If we didn't even get past the declarator successfully, we are
11121 definitely not looking at a declaration. */
11123 cp_parser_abort_tentative_parse (parser);
11125 /* Otherwise, we are looking at an expression. */
11126 return cp_parser_expression (parser);
11129 /* Parses a for-statement or range-for-statement until the closing ')',
11133 cp_parser_for (cp_parser *parser, bool ivdep)
11135 tree init, scope, decl;
11138 /* Begin the for-statement. */
11139 scope = begin_for_scope (&init);
11141 /* Parse the initialization. */
11142 is_range_for = cp_parser_for_init_statement (parser, &decl);
11145 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11147 return cp_parser_c_for (parser, scope, init, ivdep);
11151 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11153 /* Normal for loop */
11154 tree condition = NULL_TREE;
11155 tree expression = NULL_TREE;
11158 stmt = begin_for_stmt (scope, init);
11159 /* The for-init-statement has already been parsed in
11160 cp_parser_for_init_statement, so no work is needed here. */
11161 finish_for_init_stmt (stmt);
11163 /* If there's a condition, process it. */
11164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11165 condition = cp_parser_condition (parser);
11168 cp_parser_error (parser, "missing loop condition in loop with "
11169 "%<GCC ivdep%> pragma");
11170 condition = error_mark_node;
11172 finish_for_cond (condition, stmt, ivdep);
11173 /* Look for the `;'. */
11174 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11176 /* If there's an expression, process it. */
11177 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11178 expression = cp_parser_expression (parser);
11179 finish_for_expr (expression, stmt);
11184 /* Tries to parse a range-based for-statement:
11187 decl-specifier-seq declarator : expression
11189 The decl-specifier-seq declarator and the `:' are already parsed by
11190 cp_parser_for_init_statement. If processing_template_decl it returns a
11191 newly created RANGE_FOR_STMT; if not, it is converted to a
11192 regular FOR_STMT. */
11195 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11198 tree stmt, range_expr;
11199 cxx_binding *binding = NULL;
11200 tree name = NULL_TREE;
11202 /* Get the range declaration momentarily out of the way so that
11203 the range expression doesn't clash with it. */
11204 if (range_decl != error_mark_node)
11206 name = DECL_NAME (range_decl);
11207 binding = IDENTIFIER_BINDING (name);
11208 IDENTIFIER_BINDING (name) = binding->previous;
11211 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11213 bool expr_non_constant_p;
11214 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11217 range_expr = cp_parser_expression (parser);
11219 /* Put the range declaration back into scope. */
11220 if (range_decl != error_mark_node)
11222 binding->previous = IDENTIFIER_BINDING (name);
11223 IDENTIFIER_BINDING (name) = binding;
11226 /* If in template, STMT is converted to a normal for-statement
11227 at instantiation. If not, it is done just ahead. */
11228 if (processing_template_decl)
11230 if (check_for_bare_parameter_packs (range_expr))
11231 range_expr = error_mark_node;
11232 stmt = begin_range_for_stmt (scope, init);
11234 RANGE_FOR_IVDEP (stmt) = 1;
11235 finish_range_for_decl (stmt, range_decl, range_expr);
11236 if (!type_dependent_expression_p (range_expr)
11237 /* do_auto_deduction doesn't mess with template init-lists. */
11238 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11239 do_range_for_auto_deduction (range_decl, range_expr);
11243 stmt = begin_for_stmt (scope, init);
11244 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11249 /* Subroutine of cp_convert_range_for: given the initializer expression,
11250 builds up the range temporary. */
11253 build_range_temp (tree range_expr)
11255 tree range_type, range_temp;
11257 /* Find out the type deduced by the declaration
11258 `auto &&__range = range_expr'. */
11259 range_type = cp_build_reference_type (make_auto (), true);
11260 range_type = do_auto_deduction (range_type, range_expr,
11261 type_uses_auto (range_type));
11263 /* Create the __range variable. */
11264 range_temp = build_decl (input_location, VAR_DECL,
11265 get_identifier ("__for_range"), range_type);
11266 TREE_USED (range_temp) = 1;
11267 DECL_ARTIFICIAL (range_temp) = 1;
11272 /* Used by cp_parser_range_for in template context: we aren't going to
11273 do a full conversion yet, but we still need to resolve auto in the
11274 type of the for-range-declaration if present. This is basically
11275 a shortcut version of cp_convert_range_for. */
11278 do_range_for_auto_deduction (tree decl, tree range_expr)
11280 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11283 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11284 range_temp = convert_from_reference (build_range_temp (range_expr));
11285 iter_type = (cp_parser_perform_range_for_lookup
11286 (range_temp, &begin_dummy, &end_dummy));
11289 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11291 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11292 tf_warning_or_error);
11293 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11294 iter_decl, auto_node);
11299 /* Converts a range-based for-statement into a normal
11300 for-statement, as per the definition.
11302 for (RANGE_DECL : RANGE_EXPR)
11305 should be equivalent to:
11308 auto &&__range = RANGE_EXPR;
11309 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11313 RANGE_DECL = *__begin;
11318 If RANGE_EXPR is an array:
11319 BEGIN_EXPR = __range
11320 END_EXPR = __range + ARRAY_SIZE(__range)
11321 Else if RANGE_EXPR has a member 'begin' or 'end':
11322 BEGIN_EXPR = __range.begin()
11323 END_EXPR = __range.end()
11325 BEGIN_EXPR = begin(__range)
11326 END_EXPR = end(__range);
11328 If __range has a member 'begin' but not 'end', or vice versa, we must
11329 still use the second alternative (it will surely fail, however).
11330 When calling begin()/end() in the third alternative we must use
11331 argument dependent lookup, but always considering 'std' as an associated
11335 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11339 tree iter_type, begin_expr, end_expr;
11340 tree condition, expression;
11342 if (range_decl == error_mark_node || range_expr == error_mark_node)
11343 /* If an error happened previously do nothing or else a lot of
11344 unhelpful errors would be issued. */
11345 begin_expr = end_expr = iter_type = error_mark_node;
11350 if (VAR_P (range_expr)
11351 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11352 /* Can't bind a reference to an array of runtime bound. */
11353 range_temp = range_expr;
11356 range_temp = build_range_temp (range_expr);
11357 pushdecl (range_temp);
11358 cp_finish_decl (range_temp, range_expr,
11359 /*is_constant_init*/false, NULL_TREE,
11360 LOOKUP_ONLYCONVERTING);
11361 range_temp = convert_from_reference (range_temp);
11363 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11364 &begin_expr, &end_expr);
11367 /* The new for initialization statement. */
11368 begin = build_decl (input_location, VAR_DECL,
11369 get_identifier ("__for_begin"), iter_type);
11370 TREE_USED (begin) = 1;
11371 DECL_ARTIFICIAL (begin) = 1;
11373 cp_finish_decl (begin, begin_expr,
11374 /*is_constant_init*/false, NULL_TREE,
11375 LOOKUP_ONLYCONVERTING);
11377 if (cxx_dialect >= cxx1z)
11378 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11379 end = build_decl (input_location, VAR_DECL,
11380 get_identifier ("__for_end"), iter_type);
11381 TREE_USED (end) = 1;
11382 DECL_ARTIFICIAL (end) = 1;
11384 cp_finish_decl (end, end_expr,
11385 /*is_constant_init*/false, NULL_TREE,
11386 LOOKUP_ONLYCONVERTING);
11388 finish_for_init_stmt (statement);
11390 /* The new for condition. */
11391 condition = build_x_binary_op (input_location, NE_EXPR,
11394 NULL, tf_warning_or_error);
11395 finish_for_cond (condition, statement, ivdep);
11397 /* The new increment expression. */
11398 expression = finish_unary_op_expr (input_location,
11399 PREINCREMENT_EXPR, begin,
11400 tf_warning_or_error);
11401 finish_for_expr (expression, statement);
11403 /* The declaration is initialized with *__begin inside the loop body. */
11404 cp_finish_decl (range_decl,
11405 build_x_indirect_ref (input_location, begin, RO_NULL,
11406 tf_warning_or_error),
11407 /*is_constant_init*/false, NULL_TREE,
11408 LOOKUP_ONLYCONVERTING);
11413 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11414 We need to solve both at the same time because the method used
11415 depends on the existence of members begin or end.
11416 Returns the type deduced for the iterator expression. */
11419 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11421 if (error_operand_p (range))
11423 *begin = *end = error_mark_node;
11424 return error_mark_node;
11427 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11429 error ("range-based %<for%> expression of type %qT "
11430 "has incomplete type", TREE_TYPE (range));
11431 *begin = *end = error_mark_node;
11432 return error_mark_node;
11434 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11436 /* If RANGE is an array, we will use pointer arithmetic. */
11438 *end = build_binary_op (input_location, PLUS_EXPR,
11440 array_type_nelts_top (TREE_TYPE (range)),
11442 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
11446 /* If it is not an array, we must do a bit of magic. */
11447 tree id_begin, id_end;
11448 tree member_begin, member_end;
11450 *begin = *end = error_mark_node;
11452 id_begin = get_identifier ("begin");
11453 id_end = get_identifier ("end");
11454 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11455 /*protect=*/2, /*want_type=*/false,
11456 tf_warning_or_error);
11457 member_end = lookup_member (TREE_TYPE (range), id_end,
11458 /*protect=*/2, /*want_type=*/false,
11459 tf_warning_or_error);
11461 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11463 /* Use the member functions. */
11464 if (member_begin != NULL_TREE)
11465 *begin = cp_parser_range_for_member_function (range, id_begin);
11467 error ("range-based %<for%> expression of type %qT has an "
11468 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11470 if (member_end != NULL_TREE)
11471 *end = cp_parser_range_for_member_function (range, id_end);
11473 error ("range-based %<for%> expression of type %qT has a "
11474 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11478 /* Use global functions with ADL. */
11479 vec<tree, va_gc> *vec;
11480 vec = make_tree_vector ();
11482 vec_safe_push (vec, range);
11484 member_begin = perform_koenig_lookup (id_begin, vec,
11485 tf_warning_or_error);
11486 *begin = finish_call_expr (member_begin, &vec, false, true,
11487 tf_warning_or_error);
11488 member_end = perform_koenig_lookup (id_end, vec,
11489 tf_warning_or_error);
11490 *end = finish_call_expr (member_end, &vec, false, true,
11491 tf_warning_or_error);
11493 release_tree_vector (vec);
11496 /* Last common checks. */
11497 if (*begin == error_mark_node || *end == error_mark_node)
11499 /* If one of the expressions is an error do no more checks. */
11500 *begin = *end = error_mark_node;
11501 return error_mark_node;
11503 else if (type_dependent_expression_p (*begin)
11504 || type_dependent_expression_p (*end))
11505 /* Can happen, when, eg, in a template context, Koenig lookup
11506 can't resolve begin/end (c++/58503). */
11510 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11511 /* The unqualified type of the __begin and __end temporaries should
11512 be the same, as required by the multiple auto declaration. */
11513 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11515 if (cxx_dialect >= cxx1z
11516 && (build_x_binary_op (input_location, NE_EXPR,
11517 *begin, ERROR_MARK,
11520 != error_mark_node))
11521 /* P0184R0 allows __begin and __end to have different types,
11522 but make sure they are comparable so we can give a better
11525 error ("inconsistent begin/end types in range-based %<for%> "
11526 "statement: %qT and %qT",
11527 TREE_TYPE (*begin), TREE_TYPE (*end));
11534 /* Helper function for cp_parser_perform_range_for_lookup.
11535 Builds a tree for RANGE.IDENTIFIER(). */
11538 cp_parser_range_for_member_function (tree range, tree identifier)
11541 vec<tree, va_gc> *vec;
11543 member = finish_class_member_access_expr (range, identifier,
11544 false, tf_warning_or_error);
11545 if (member == error_mark_node)
11546 return error_mark_node;
11548 vec = make_tree_vector ();
11549 res = finish_call_expr (member, &vec,
11550 /*disallow_virtual=*/false,
11551 /*koenig_p=*/false,
11552 tf_warning_or_error);
11553 release_tree_vector (vec);
11557 /* Parse an iteration-statement.
11559 iteration-statement:
11560 while ( condition ) statement
11561 do statement while ( expression ) ;
11562 for ( for-init-statement condition [opt] ; expression [opt] )
11565 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11568 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11573 unsigned char in_statement;
11574 token_indent_info guard_tinfo;
11576 /* Peek at the next token. */
11577 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11579 return error_mark_node;
11581 guard_tinfo = get_token_indent_info (token);
11583 /* Remember whether or not we are already within an iteration
11585 in_statement = parser->in_statement;
11587 /* See what kind of keyword it is. */
11588 keyword = token->keyword;
11595 /* Begin the while-statement. */
11596 statement = begin_while_stmt ();
11597 /* Look for the `('. */
11598 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11599 /* Parse the condition. */
11600 condition = cp_parser_condition (parser);
11601 finish_while_stmt_cond (condition, statement, ivdep);
11602 /* Look for the `)'. */
11603 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11604 /* Parse the dependent statement. */
11605 parser->in_statement = IN_ITERATION_STMT;
11606 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11607 parser->in_statement = in_statement;
11608 /* We're done with the while-statement. */
11609 finish_while_stmt (statement);
11617 /* Begin the do-statement. */
11618 statement = begin_do_stmt ();
11619 /* Parse the body of the do-statement. */
11620 parser->in_statement = IN_ITERATION_STMT;
11621 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11622 parser->in_statement = in_statement;
11623 finish_do_body (statement);
11624 /* Look for the `while' keyword. */
11625 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11626 /* Look for the `('. */
11627 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11628 /* Parse the expression. */
11629 expression = cp_parser_expression (parser);
11630 /* We're done with the do-statement. */
11631 finish_do_stmt (expression, statement, ivdep);
11632 /* Look for the `)'. */
11633 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11634 /* Look for the `;'. */
11635 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11641 /* Look for the `('. */
11642 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11644 statement = cp_parser_for (parser, ivdep);
11646 /* Look for the `)'. */
11647 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11649 /* Parse the body of the for-statement. */
11650 parser->in_statement = IN_ITERATION_STMT;
11651 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11652 parser->in_statement = in_statement;
11654 /* We're done with the for-statement. */
11655 finish_for_stmt (statement);
11660 cp_parser_error (parser, "expected iteration-statement");
11661 statement = error_mark_node;
11668 /* Parse a for-init-statement or the declarator of a range-based-for.
11669 Returns true if a range-based-for declaration is seen.
11671 for-init-statement:
11672 expression-statement
11673 simple-declaration */
11676 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11678 /* If the next token is a `;', then we have an empty
11679 expression-statement. Grammatically, this is also a
11680 simple-declaration, but an invalid one, because it does not
11681 declare anything. Therefore, if we did not handle this case
11682 specially, we would issue an error message about an invalid
11684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11686 bool is_range_for = false;
11687 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11689 /* A colon is used in range-based for. */
11690 parser->colon_corrects_to_scope_p = false;
11692 /* We're going to speculatively look for a declaration, falling back
11693 to an expression, if necessary. */
11694 cp_parser_parse_tentatively (parser);
11695 /* Parse the declaration. */
11696 cp_parser_simple_declaration (parser,
11697 /*function_definition_allowed_p=*/false,
11699 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11700 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11702 /* It is a range-for, consume the ':' */
11703 cp_lexer_consume_token (parser->lexer);
11704 is_range_for = true;
11705 if (cxx_dialect < cxx11)
11707 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11708 "range-based %<for%> loops only available with "
11709 "-std=c++11 or -std=gnu++11");
11710 *decl = error_mark_node;
11714 /* The ';' is not consumed yet because we told
11715 cp_parser_simple_declaration not to. */
11716 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11718 if (cp_parser_parse_definitely (parser))
11719 return is_range_for;
11720 /* If the tentative parse failed, then we shall need to look for an
11721 expression-statement. */
11723 /* If we are here, it is an expression-statement. */
11724 cp_parser_expression_statement (parser, NULL_TREE);
11728 /* Parse a jump-statement.
11733 return expression [opt] ;
11734 return braced-init-list ;
11740 goto * expression ;
11742 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11745 cp_parser_jump_statement (cp_parser* parser)
11747 tree statement = error_mark_node;
11750 unsigned char in_statement;
11752 /* Peek at the next token. */
11753 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11755 return error_mark_node;
11757 /* See what kind of keyword it is. */
11758 keyword = token->keyword;
11762 in_statement = parser->in_statement & ~IN_IF_STMT;
11763 switch (in_statement)
11766 error_at (token->location, "break statement not within loop or switch");
11769 gcc_assert ((in_statement & IN_SWITCH_STMT)
11770 || in_statement == IN_ITERATION_STMT);
11771 statement = finish_break_stmt ();
11772 if (in_statement == IN_ITERATION_STMT)
11773 break_maybe_infinite_loop ();
11776 error_at (token->location, "invalid exit from OpenMP structured block");
11779 error_at (token->location, "break statement used with OpenMP for loop");
11781 case IN_CILK_SIMD_FOR:
11782 error_at (token->location, "break statement used with Cilk Plus for loop");
11785 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11789 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11792 error_at (token->location, "continue statement not within a loop");
11794 case IN_CILK_SIMD_FOR:
11795 error_at (token->location,
11796 "continue statement within %<#pragma simd%> loop body");
11797 /* Fall through. */
11798 case IN_ITERATION_STMT:
11800 statement = finish_continue_stmt ();
11803 error_at (token->location, "invalid exit from OpenMP structured block");
11806 gcc_unreachable ();
11808 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11814 bool expr_non_constant_p;
11816 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11818 cp_lexer_set_source_position (parser->lexer);
11819 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11820 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11822 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11823 expr = cp_parser_expression (parser);
11825 /* If the next token is a `;', then there is no
11828 /* Build the return-statement. */
11829 statement = finish_return_stmt (expr);
11830 /* Look for the final `;'. */
11831 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11836 if (parser->in_function_body
11837 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11839 error ("%<goto%> in %<constexpr%> function");
11840 cp_function_chain->invalid_constexpr = true;
11843 /* Create the goto-statement. */
11844 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11846 /* Issue a warning about this use of a GNU extension. */
11847 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11848 /* Consume the '*' token. */
11849 cp_lexer_consume_token (parser->lexer);
11850 /* Parse the dependent expression. */
11851 finish_goto_stmt (cp_parser_expression (parser));
11854 finish_goto_stmt (cp_parser_identifier (parser));
11855 /* Look for the final `;'. */
11856 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11860 cp_parser_error (parser, "expected jump-statement");
11867 /* Parse a declaration-statement.
11869 declaration-statement:
11870 block-declaration */
11873 cp_parser_declaration_statement (cp_parser* parser)
11877 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11878 p = obstack_alloc (&declarator_obstack, 0);
11880 /* Parse the block-declaration. */
11881 cp_parser_block_declaration (parser, /*statement_p=*/true);
11883 /* Free any declarators allocated. */
11884 obstack_free (&declarator_obstack, p);
11887 /* Some dependent statements (like `if (cond) statement'), are
11888 implicitly in their own scope. In other words, if the statement is
11889 a single statement (as opposed to a compound-statement), it is
11890 none-the-less treated as if it were enclosed in braces. Any
11891 declarations appearing in the dependent statement are out of scope
11892 after control passes that point. This function parses a statement,
11893 but ensures that is in its own scope, even if it is not a
11894 compound-statement.
11896 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11897 is a (possibly labeled) if statement which is not enclosed in
11898 braces and has an else clause. This is used to implement
11901 CHAIN is a vector of if-else-if conditions. This is used to implement
11904 Returns the new statement. */
11907 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11908 const token_indent_info &guard_tinfo,
11912 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11913 token_indent_info body_tinfo
11914 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11919 /* Mark if () ; with a special NOP_EXPR. */
11920 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11922 cp_lexer_consume_token (parser->lexer);
11923 statement = add_stmt (build_empty_stmt (body_loc));
11925 if (guard_tinfo.keyword == RID_IF
11926 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11927 warning_at (body_loc, OPT_Wempty_body,
11928 "suggest braces around empty body in an %<if%> statement");
11929 else if (guard_tinfo.keyword == RID_ELSE)
11930 warning_at (body_loc, OPT_Wempty_body,
11931 "suggest braces around empty body in an %<else%> statement");
11933 /* if a compound is opened, we simply parse the statement directly. */
11934 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11935 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11936 /* If the token is not a `{', then we must take special action. */
11939 /* Create a compound-statement. */
11940 statement = begin_compound_stmt (0);
11941 /* Parse the dependent-statement. */
11942 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11943 /* Finish the dummy compound-statement. */
11944 finish_compound_stmt (statement);
11947 token_indent_info next_tinfo
11948 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11949 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11951 /* Return the statement. */
11955 /* For some dependent statements (like `while (cond) statement'), we
11956 have already created a scope. Therefore, even if the dependent
11957 statement is a compound-statement, we do not want to create another
11961 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
11962 const token_indent_info &guard_tinfo)
11964 /* If the token is a `{', then we must take special action. */
11965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11967 token_indent_info body_tinfo
11968 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11970 cp_parser_statement (parser, NULL_TREE, false, if_p);
11971 token_indent_info next_tinfo
11972 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11973 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11977 /* Avoid calling cp_parser_compound_statement, so that we
11978 don't create a new scope. Do everything else by hand. */
11979 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11980 /* If the next keyword is `__label__' we have a label declaration. */
11981 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11982 cp_parser_label_declaration (parser);
11983 /* Parse an (optional) statement-seq. */
11984 cp_parser_statement_seq_opt (parser, NULL_TREE);
11985 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11989 /* Declarations [gram.dcl.dcl] */
11991 /* Parse an optional declaration-sequence.
11995 declaration-seq declaration */
11998 cp_parser_declaration_seq_opt (cp_parser* parser)
12004 token = cp_lexer_peek_token (parser->lexer);
12006 if (token->type == CPP_CLOSE_BRACE
12007 || token->type == CPP_EOF
12008 || token->type == CPP_PRAGMA_EOL)
12011 if (token->type == CPP_SEMICOLON)
12013 /* A declaration consisting of a single semicolon is
12014 invalid. Allow it unless we're being pedantic. */
12015 cp_lexer_consume_token (parser->lexer);
12016 if (!in_system_header_at (input_location))
12017 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12021 /* If we're entering or exiting a region that's implicitly
12022 extern "C", modify the lang context appropriately. */
12023 if (!parser->implicit_extern_c && token->implicit_extern_c)
12025 push_lang_context (lang_name_c);
12026 parser->implicit_extern_c = true;
12028 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12030 pop_lang_context ();
12031 parser->implicit_extern_c = false;
12034 if (token->type == CPP_PRAGMA)
12036 /* A top-level declaration can consist solely of a #pragma.
12037 A nested declaration cannot, so this is done here and not
12038 in cp_parser_declaration. (A #pragma at block scope is
12039 handled in cp_parser_statement.) */
12040 cp_parser_pragma (parser, pragma_external, NULL);
12044 /* Parse the declaration itself. */
12045 cp_parser_declaration (parser);
12049 /* Parse a declaration.
12053 function-definition
12054 template-declaration
12055 explicit-instantiation
12056 explicit-specialization
12057 linkage-specification
12058 namespace-definition
12063 __extension__ declaration */
12066 cp_parser_declaration (cp_parser* parser)
12070 int saved_pedantic;
12072 tree attributes = NULL_TREE;
12074 /* Check for the `__extension__' keyword. */
12075 if (cp_parser_extension_opt (parser, &saved_pedantic))
12077 /* Parse the qualified declaration. */
12078 cp_parser_declaration (parser);
12079 /* Restore the PEDANTIC flag. */
12080 pedantic = saved_pedantic;
12085 /* Try to figure out what kind of declaration is present. */
12086 token1 = *cp_lexer_peek_token (parser->lexer);
12088 if (token1.type != CPP_EOF)
12089 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12092 token2.type = CPP_EOF;
12093 token2.keyword = RID_MAX;
12096 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12097 p = obstack_alloc (&declarator_obstack, 0);
12099 /* If the next token is `extern' and the following token is a string
12100 literal, then we have a linkage specification. */
12101 if (token1.keyword == RID_EXTERN
12102 && cp_parser_is_pure_string_literal (&token2))
12103 cp_parser_linkage_specification (parser);
12104 /* If the next token is `template', then we have either a template
12105 declaration, an explicit instantiation, or an explicit
12107 else if (token1.keyword == RID_TEMPLATE)
12109 /* `template <>' indicates a template specialization. */
12110 if (token2.type == CPP_LESS
12111 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12112 cp_parser_explicit_specialization (parser);
12113 /* `template <' indicates a template declaration. */
12114 else if (token2.type == CPP_LESS)
12115 cp_parser_template_declaration (parser, /*member_p=*/false);
12116 /* Anything else must be an explicit instantiation. */
12118 cp_parser_explicit_instantiation (parser);
12120 /* If the next token is `export', then we have a template
12122 else if (token1.keyword == RID_EXPORT)
12123 cp_parser_template_declaration (parser, /*member_p=*/false);
12124 /* If the next token is `extern', 'static' or 'inline' and the one
12125 after that is `template', we have a GNU extended explicit
12126 instantiation directive. */
12127 else if (cp_parser_allow_gnu_extensions_p (parser)
12128 && (token1.keyword == RID_EXTERN
12129 || token1.keyword == RID_STATIC
12130 || token1.keyword == RID_INLINE)
12131 && token2.keyword == RID_TEMPLATE)
12132 cp_parser_explicit_instantiation (parser);
12133 /* If the next token is `namespace', check for a named or unnamed
12134 namespace definition. */
12135 else if (token1.keyword == RID_NAMESPACE
12136 && (/* A named namespace definition. */
12137 (token2.type == CPP_NAME
12138 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12140 || (token2.type == CPP_OPEN_SQUARE
12141 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12142 == CPP_OPEN_SQUARE)
12143 /* An unnamed namespace definition. */
12144 || token2.type == CPP_OPEN_BRACE
12145 || token2.keyword == RID_ATTRIBUTE))
12146 cp_parser_namespace_definition (parser);
12147 /* An inline (associated) namespace definition. */
12148 else if (token1.keyword == RID_INLINE
12149 && token2.keyword == RID_NAMESPACE)
12150 cp_parser_namespace_definition (parser);
12151 /* Objective-C++ declaration/definition. */
12152 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12153 cp_parser_objc_declaration (parser, NULL_TREE);
12154 else if (c_dialect_objc ()
12155 && token1.keyword == RID_ATTRIBUTE
12156 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12157 cp_parser_objc_declaration (parser, attributes);
12158 /* At this point we may have a template declared by a concept
12160 else if (flag_concepts
12161 && cp_parser_template_declaration_after_export (parser,
12162 /*member_p=*/false))
12165 /* Try to parse a block-declaration, or a function-definition. */
12166 cp_parser_block_declaration (parser, /*statement_p=*/false);
12168 /* Free any declarators allocated. */
12169 obstack_free (&declarator_obstack, p);
12172 /* Parse a block-declaration.
12177 namespace-alias-definition
12184 __extension__ block-declaration
12189 static_assert-declaration
12191 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12192 part of a declaration-statement. */
12195 cp_parser_block_declaration (cp_parser *parser,
12199 int saved_pedantic;
12201 /* Check for the `__extension__' keyword. */
12202 if (cp_parser_extension_opt (parser, &saved_pedantic))
12204 /* Parse the qualified declaration. */
12205 cp_parser_block_declaration (parser, statement_p);
12206 /* Restore the PEDANTIC flag. */
12207 pedantic = saved_pedantic;
12212 /* Peek at the next token to figure out which kind of declaration is
12214 token1 = cp_lexer_peek_token (parser->lexer);
12216 /* If the next keyword is `asm', we have an asm-definition. */
12217 if (token1->keyword == RID_ASM)
12220 cp_parser_commit_to_tentative_parse (parser);
12221 cp_parser_asm_definition (parser);
12223 /* If the next keyword is `namespace', we have a
12224 namespace-alias-definition. */
12225 else if (token1->keyword == RID_NAMESPACE)
12226 cp_parser_namespace_alias_definition (parser);
12227 /* If the next keyword is `using', we have a
12228 using-declaration, a using-directive, or an alias-declaration. */
12229 else if (token1->keyword == RID_USING)
12234 cp_parser_commit_to_tentative_parse (parser);
12235 /* If the token after `using' is `namespace', then we have a
12236 using-directive. */
12237 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12238 if (token2->keyword == RID_NAMESPACE)
12239 cp_parser_using_directive (parser);
12240 /* If the second token after 'using' is '=', then we have an
12241 alias-declaration. */
12242 else if (cxx_dialect >= cxx11
12243 && token2->type == CPP_NAME
12244 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12245 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12246 cp_parser_alias_declaration (parser);
12247 /* Otherwise, it's a using-declaration. */
12249 cp_parser_using_declaration (parser,
12250 /*access_declaration_p=*/false);
12252 /* If the next keyword is `__label__' we have a misplaced label
12254 else if (token1->keyword == RID_LABEL)
12256 cp_lexer_consume_token (parser->lexer);
12257 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12258 cp_parser_skip_to_end_of_statement (parser);
12259 /* If the next token is now a `;', consume it. */
12260 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12261 cp_lexer_consume_token (parser->lexer);
12263 /* If the next token is `static_assert' we have a static assertion. */
12264 else if (token1->keyword == RID_STATIC_ASSERT)
12265 cp_parser_static_assert (parser, /*member_p=*/false);
12266 /* Anything else must be a simple-declaration. */
12268 cp_parser_simple_declaration (parser, !statement_p,
12269 /*maybe_range_for_decl*/NULL);
12272 /* Parse a simple-declaration.
12274 simple-declaration:
12275 decl-specifier-seq [opt] init-declarator-list [opt] ;
12277 init-declarator-list:
12279 init-declarator-list , init-declarator
12281 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12282 function-definition as a simple-declaration.
12284 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12285 parsed declaration if it is an uninitialized single declarator not followed
12286 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12287 if present, will not be consumed. */
12290 cp_parser_simple_declaration (cp_parser* parser,
12291 bool function_definition_allowed_p,
12292 tree *maybe_range_for_decl)
12294 cp_decl_specifier_seq decl_specifiers;
12295 int declares_class_or_enum;
12296 bool saw_declarator;
12297 location_t comma_loc = UNKNOWN_LOCATION;
12298 location_t init_loc = UNKNOWN_LOCATION;
12300 if (maybe_range_for_decl)
12301 *maybe_range_for_decl = NULL_TREE;
12303 /* Defer access checks until we know what is being declared; the
12304 checks for names appearing in the decl-specifier-seq should be
12305 done as if we were in the scope of the thing being declared. */
12306 push_deferring_access_checks (dk_deferred);
12308 /* Parse the decl-specifier-seq. We have to keep track of whether
12309 or not the decl-specifier-seq declares a named class or
12310 enumeration type, since that is the only case in which the
12311 init-declarator-list is allowed to be empty.
12315 In a simple-declaration, the optional init-declarator-list can be
12316 omitted only when declaring a class or enumeration, that is when
12317 the decl-specifier-seq contains either a class-specifier, an
12318 elaborated-type-specifier, or an enum-specifier. */
12319 cp_parser_decl_specifier_seq (parser,
12320 CP_PARSER_FLAGS_OPTIONAL,
12322 &declares_class_or_enum);
12323 /* We no longer need to defer access checks. */
12324 stop_deferring_access_checks ();
12326 /* In a block scope, a valid declaration must always have a
12327 decl-specifier-seq. By not trying to parse declarators, we can
12328 resolve the declaration/expression ambiguity more quickly. */
12329 if (!function_definition_allowed_p
12330 && !decl_specifiers.any_specifiers_p)
12332 cp_parser_error (parser, "expected declaration");
12336 /* If the next two tokens are both identifiers, the code is
12337 erroneous. The usual cause of this situation is code like:
12341 where "T" should name a type -- but does not. */
12342 if (!decl_specifiers.any_type_specifiers_p
12343 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12345 /* If parsing tentatively, we should commit; we really are
12346 looking at a declaration. */
12347 cp_parser_commit_to_tentative_parse (parser);
12352 /* If we have seen at least one decl-specifier, and the next token
12353 is not a parenthesis, then we must be looking at a declaration.
12354 (After "int (" we might be looking at a functional cast.) */
12355 if (decl_specifiers.any_specifiers_p
12356 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12357 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12358 && !cp_parser_error_occurred (parser))
12359 cp_parser_commit_to_tentative_parse (parser);
12363 last_type = NULL_TREE;
12365 /* Keep going until we hit the `;' at the end of the simple
12367 saw_declarator = false;
12368 while (cp_lexer_next_token_is_not (parser->lexer,
12372 bool function_definition_p;
12374 tree auto_result = NULL_TREE;
12376 if (saw_declarator)
12378 /* If we are processing next declarator, comma is expected */
12379 token = cp_lexer_peek_token (parser->lexer);
12380 gcc_assert (token->type == CPP_COMMA);
12381 cp_lexer_consume_token (parser->lexer);
12382 if (maybe_range_for_decl)
12384 *maybe_range_for_decl = error_mark_node;
12385 if (comma_loc == UNKNOWN_LOCATION)
12386 comma_loc = token->location;
12390 saw_declarator = true;
12392 /* Parse the init-declarator. */
12393 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12395 function_definition_allowed_p,
12396 /*member_p=*/false,
12397 declares_class_or_enum,
12398 &function_definition_p,
12399 maybe_range_for_decl,
12402 /* If an error occurred while parsing tentatively, exit quickly.
12403 (That usually happens when in the body of a function; each
12404 statement is treated as a declaration-statement until proven
12406 if (cp_parser_error_occurred (parser))
12410 && (!processing_template_decl || !type_uses_auto (auto_result)))
12413 && last_type != error_mark_node
12414 && !same_type_p (auto_result, last_type))
12416 /* If the list of declarators contains more than one declarator,
12417 the type of each declared variable is determined as described
12418 above. If the type deduced for the template parameter U is not
12419 the same in each deduction, the program is ill-formed. */
12420 error_at (decl_specifiers.locations[ds_type_spec],
12421 "inconsistent deduction for %qT: %qT and then %qT",
12422 decl_specifiers.type, last_type, auto_result);
12423 last_type = error_mark_node;
12426 last_type = auto_result;
12429 /* Handle function definitions specially. */
12430 if (function_definition_p)
12432 /* If the next token is a `,', then we are probably
12433 processing something like:
12437 which is erroneous. */
12438 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12440 cp_token *token = cp_lexer_peek_token (parser->lexer);
12441 error_at (token->location,
12443 " declarations and function-definitions is forbidden");
12445 /* Otherwise, we're done with the list of declarators. */
12448 pop_deferring_access_checks ();
12452 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12453 *maybe_range_for_decl = decl;
12454 /* The next token should be either a `,' or a `;'. */
12455 token = cp_lexer_peek_token (parser->lexer);
12456 /* If it's a `,', there are more declarators to come. */
12457 if (token->type == CPP_COMMA)
12458 /* will be consumed next time around */;
12459 /* If it's a `;', we are done. */
12460 else if (token->type == CPP_SEMICOLON)
12462 else if (maybe_range_for_decl)
12464 if (declares_class_or_enum && token->type == CPP_COLON)
12465 pedwarn (decl_specifiers.locations[ds_type_spec], 0,
12466 "types may not be defined in a for-range-declaration");
12469 /* Anything else is an error. */
12472 /* If we have already issued an error message we don't need
12473 to issue another one. */
12474 if ((decl != error_mark_node
12475 && DECL_INITIAL (decl) != error_mark_node)
12476 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12477 cp_parser_error (parser, "expected %<,%> or %<;%>");
12478 /* Skip tokens until we reach the end of the statement. */
12479 cp_parser_skip_to_end_of_statement (parser);
12480 /* If the next token is now a `;', consume it. */
12481 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12482 cp_lexer_consume_token (parser->lexer);
12485 /* After the first time around, a function-definition is not
12486 allowed -- even if it was OK at first. For example:
12491 function_definition_allowed_p = false;
12494 /* Issue an error message if no declarators are present, and the
12495 decl-specifier-seq does not itself declare a class or
12496 enumeration: [dcl.dcl]/3. */
12497 if (!saw_declarator)
12499 if (cp_parser_declares_only_class_p (parser))
12501 if (!declares_class_or_enum
12502 && decl_specifiers.type
12503 && OVERLOAD_TYPE_P (decl_specifiers.type))
12504 /* Ensure an error is issued anyway when finish_decltype_type,
12505 called via cp_parser_decl_specifier_seq, returns a class or
12506 an enumeration (c++/51786). */
12507 decl_specifiers.type = NULL_TREE;
12508 shadow_tag (&decl_specifiers);
12510 /* Perform any deferred access checks. */
12511 perform_deferred_access_checks (tf_warning_or_error);
12514 /* Consume the `;'. */
12515 if (!maybe_range_for_decl)
12516 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12517 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12519 if (init_loc != UNKNOWN_LOCATION)
12520 error_at (init_loc, "initializer in range-based %<for%> loop");
12521 if (comma_loc != UNKNOWN_LOCATION)
12522 error_at (comma_loc,
12523 "multiple declarations in range-based %<for%> loop");
12527 pop_deferring_access_checks ();
12530 /* Parse a decl-specifier-seq.
12532 decl-specifier-seq:
12533 decl-specifier-seq [opt] decl-specifier
12534 decl-specifier attribute-specifier-seq [opt] (C++11)
12537 storage-class-specifier
12548 Concepts Extension:
12553 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12555 The parser flags FLAGS is used to control type-specifier parsing.
12557 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12560 1: one of the decl-specifiers is an elaborated-type-specifier
12561 (i.e., a type declaration)
12562 2: one of the decl-specifiers is an enum-specifier or a
12563 class-specifier (i.e., a type definition)
12568 cp_parser_decl_specifier_seq (cp_parser* parser,
12569 cp_parser_flags flags,
12570 cp_decl_specifier_seq *decl_specs,
12571 int* declares_class_or_enum)
12573 bool constructor_possible_p = !parser->in_declarator_p;
12574 bool found_decl_spec = false;
12575 cp_token *start_token = NULL;
12578 /* Clear DECL_SPECS. */
12579 clear_decl_specs (decl_specs);
12581 /* Assume no class or enumeration type is declared. */
12582 *declares_class_or_enum = 0;
12584 /* Keep reading specifiers until there are no more to read. */
12587 bool constructor_p;
12591 /* Peek at the next token. */
12592 token = cp_lexer_peek_token (parser->lexer);
12594 /* Save the first token of the decl spec list for error
12597 start_token = token;
12598 /* Handle attributes. */
12599 if (cp_next_tokens_can_be_attribute_p (parser))
12601 /* Parse the attributes. */
12602 tree attrs = cp_parser_attributes_opt (parser);
12604 /* In a sequence of declaration specifiers, c++11 attributes
12605 appertain to the type that precede them. In that case
12608 The attribute-specifier-seq affects the type only for
12609 the declaration it appears in, not other declarations
12610 involving the same type.
12612 But for now let's force the user to position the
12613 attribute either at the beginning of the declaration or
12614 after the declarator-id, which would clearly mean that it
12615 applies to the declarator. */
12616 if (cxx11_attribute_p (attrs))
12618 if (!found_decl_spec)
12619 /* The c++11 attribute is at the beginning of the
12620 declaration. It appertains to the entity being
12624 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12626 /* This is an attribute following a
12627 class-specifier. */
12628 if (decl_specs->type_definition_p)
12629 warn_misplaced_attr_for_class_type (token->location,
12635 decl_specs->std_attributes
12636 = chainon (decl_specs->std_attributes,
12638 if (decl_specs->locations[ds_std_attribute] == 0)
12639 decl_specs->locations[ds_std_attribute] = token->location;
12645 decl_specs->attributes
12646 = chainon (decl_specs->attributes,
12648 if (decl_specs->locations[ds_attribute] == 0)
12649 decl_specs->locations[ds_attribute] = token->location;
12652 /* Assume we will find a decl-specifier keyword. */
12653 found_decl_spec = true;
12654 /* If the next token is an appropriate keyword, we can simply
12655 add it to the list. */
12656 switch (token->keyword)
12662 if (!at_class_scope_p ())
12664 error_at (token->location, "%<friend%> used outside of class");
12665 cp_lexer_purge_token (parser->lexer);
12670 /* Consume the token. */
12671 cp_lexer_consume_token (parser->lexer);
12675 case RID_CONSTEXPR:
12677 cp_lexer_consume_token (parser->lexer);
12682 cp_lexer_consume_token (parser->lexer);
12685 /* function-specifier:
12692 cp_parser_function_specifier_opt (parser, decl_specs);
12699 /* Consume the token. */
12700 cp_lexer_consume_token (parser->lexer);
12701 /* A constructor declarator cannot appear in a typedef. */
12702 constructor_possible_p = false;
12703 /* The "typedef" keyword can only occur in a declaration; we
12704 may as well commit at this point. */
12705 cp_parser_commit_to_tentative_parse (parser);
12707 if (decl_specs->storage_class != sc_none)
12708 decl_specs->conflicting_specifiers_p = true;
12711 /* storage-class-specifier:
12721 if (cxx_dialect == cxx98)
12723 /* Consume the token. */
12724 cp_lexer_consume_token (parser->lexer);
12726 /* Complain about `auto' as a storage specifier, if
12727 we're complaining about C++0x compatibility. */
12728 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12729 " changes meaning in C++11; please remove it");
12731 /* Set the storage class anyway. */
12732 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12736 /* C++0x auto type-specifier. */
12737 found_decl_spec = false;
12744 /* Consume the token. */
12745 cp_lexer_consume_token (parser->lexer);
12746 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12750 /* Consume the token. */
12752 cp_lexer_consume_token (parser->lexer);
12756 /* We did not yet find a decl-specifier yet. */
12757 found_decl_spec = false;
12761 if (found_decl_spec
12762 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12763 && token->keyword != RID_CONSTEXPR)
12764 error ("decl-specifier invalid in condition");
12767 set_and_check_decl_spec_loc (decl_specs, ds, token);
12769 /* Constructors are a special case. The `S' in `S()' is not a
12770 decl-specifier; it is the beginning of the declarator. */
12772 = (!found_decl_spec
12773 && constructor_possible_p
12774 && (cp_parser_constructor_declarator_p
12775 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12777 /* If we don't have a DECL_SPEC yet, then we must be looking at
12778 a type-specifier. */
12779 if (!found_decl_spec && !constructor_p)
12781 int decl_spec_declares_class_or_enum;
12782 bool is_cv_qualifier;
12786 = cp_parser_type_specifier (parser, flags,
12788 /*is_declaration=*/true,
12789 &decl_spec_declares_class_or_enum,
12791 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12793 /* If this type-specifier referenced a user-defined type
12794 (a typedef, class-name, etc.), then we can't allow any
12795 more such type-specifiers henceforth.
12799 The longest sequence of decl-specifiers that could
12800 possibly be a type name is taken as the
12801 decl-specifier-seq of a declaration. The sequence shall
12802 be self-consistent as described below.
12806 As a general rule, at most one type-specifier is allowed
12807 in the complete decl-specifier-seq of a declaration. The
12808 only exceptions are the following:
12810 -- const or volatile can be combined with any other
12813 -- signed or unsigned can be combined with char, long,
12821 void g (const int Pc);
12823 Here, Pc is *not* part of the decl-specifier seq; it's
12824 the declarator. Therefore, once we see a type-specifier
12825 (other than a cv-qualifier), we forbid any additional
12826 user-defined types. We *do* still allow things like `int
12827 int' to be considered a decl-specifier-seq, and issue the
12828 error message later. */
12829 if (type_spec && !is_cv_qualifier)
12830 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12831 /* A constructor declarator cannot follow a type-specifier. */
12834 constructor_possible_p = false;
12835 found_decl_spec = true;
12836 if (!is_cv_qualifier)
12837 decl_specs->any_type_specifiers_p = true;
12841 /* If we still do not have a DECL_SPEC, then there are no more
12842 decl-specifiers. */
12843 if (!found_decl_spec)
12846 decl_specs->any_specifiers_p = true;
12847 /* After we see one decl-specifier, further decl-specifiers are
12848 always optional. */
12849 flags |= CP_PARSER_FLAGS_OPTIONAL;
12852 /* Don't allow a friend specifier with a class definition. */
12853 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12854 && (*declares_class_or_enum & 2))
12855 error_at (decl_specs->locations[ds_friend],
12856 "class definition may not be declared a friend");
12859 /* Parse an (optional) storage-class-specifier.
12861 storage-class-specifier:
12870 storage-class-specifier:
12873 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12876 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12878 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12881 if (cxx_dialect != cxx98)
12883 /* Fall through for C++98. */
12890 /* Consume the token. */
12891 return cp_lexer_consume_token (parser->lexer)->u.value;
12898 /* Parse an (optional) function-specifier.
12900 function-specifier:
12905 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12906 Updates DECL_SPECS, if it is non-NULL. */
12909 cp_parser_function_specifier_opt (cp_parser* parser,
12910 cp_decl_specifier_seq *decl_specs)
12912 cp_token *token = cp_lexer_peek_token (parser->lexer);
12913 switch (token->keyword)
12916 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12920 /* 14.5.2.3 [temp.mem]
12922 A member function template shall not be virtual. */
12923 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12924 error_at (token->location, "templates may not be %<virtual%>");
12926 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12930 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12937 /* Consume the token. */
12938 return cp_lexer_consume_token (parser->lexer)->u.value;
12941 /* Parse a linkage-specification.
12943 linkage-specification:
12944 extern string-literal { declaration-seq [opt] }
12945 extern string-literal declaration */
12948 cp_parser_linkage_specification (cp_parser* parser)
12952 /* Look for the `extern' keyword. */
12953 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12955 /* Look for the string-literal. */
12956 linkage = cp_parser_string_literal (parser, false, false);
12958 /* Transform the literal into an identifier. If the literal is a
12959 wide-character string, or contains embedded NULs, then we can't
12960 handle it as the user wants. */
12961 if (strlen (TREE_STRING_POINTER (linkage))
12962 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12964 cp_parser_error (parser, "invalid linkage-specification");
12965 /* Assume C++ linkage. */
12966 linkage = lang_name_cplusplus;
12969 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12971 /* We're now using the new linkage. */
12972 push_lang_context (linkage);
12974 /* If the next token is a `{', then we're using the first
12976 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12978 cp_ensure_no_omp_declare_simd (parser);
12979 cp_ensure_no_oacc_routine (parser);
12981 /* Consume the `{' token. */
12982 cp_lexer_consume_token (parser->lexer);
12983 /* Parse the declarations. */
12984 cp_parser_declaration_seq_opt (parser);
12985 /* Look for the closing `}'. */
12986 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12988 /* Otherwise, there's just one declaration. */
12991 bool saved_in_unbraced_linkage_specification_p;
12993 saved_in_unbraced_linkage_specification_p
12994 = parser->in_unbraced_linkage_specification_p;
12995 parser->in_unbraced_linkage_specification_p = true;
12996 cp_parser_declaration (parser);
12997 parser->in_unbraced_linkage_specification_p
12998 = saved_in_unbraced_linkage_specification_p;
13001 /* We're done with the linkage-specification. */
13002 pop_lang_context ();
13005 /* Parse a static_assert-declaration.
13007 static_assert-declaration:
13008 static_assert ( constant-expression , string-literal ) ;
13009 static_assert ( constant-expression ) ; (C++1Z)
13011 If MEMBER_P, this static_assert is a class member. */
13014 cp_parser_static_assert(cp_parser *parser, bool member_p)
13019 location_t saved_loc;
13022 /* Peek at the `static_assert' token so we can keep track of exactly
13023 where the static assertion started. */
13024 token = cp_lexer_peek_token (parser->lexer);
13025 saved_loc = token->location;
13027 /* Look for the `static_assert' keyword. */
13028 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13032 /* We know we are in a static assertion; commit to any tentative
13034 if (cp_parser_parsing_tentatively (parser))
13035 cp_parser_commit_to_tentative_parse (parser);
13037 /* Parse the `(' starting the static assertion condition. */
13038 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13040 /* Parse the constant-expression. Allow a non-constant expression
13041 here in order to give better diagnostics in finish_static_assert. */
13043 cp_parser_constant_expression (parser,
13044 /*allow_non_constant_p=*/true,
13045 /*non_constant_p=*/&dummy);
13047 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13049 if (cxx_dialect < cxx1z)
13050 pedwarn (input_location, OPT_Wpedantic,
13051 "static_assert without a message "
13052 "only available with -std=c++1z or -std=gnu++1z");
13054 cp_lexer_consume_token (parser->lexer);
13055 message = build_string (1, "");
13056 TREE_TYPE (message) = char_array_type_node;
13057 fix_string_type (message);
13061 /* Parse the separating `,'. */
13062 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13064 /* Parse the string-literal message. */
13065 message = cp_parser_string_literal (parser,
13066 /*translate=*/false,
13069 /* A `)' completes the static assertion. */
13070 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13071 cp_parser_skip_to_closing_parenthesis (parser,
13072 /*recovering=*/true,
13073 /*or_comma=*/false,
13074 /*consume_paren=*/true);
13077 /* A semicolon terminates the declaration. */
13078 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13080 /* Complete the static assertion, which may mean either processing
13081 the static assert now or saving it for template instantiation. */
13082 finish_static_assert (condition, message, saved_loc, member_p);
13085 /* Parse the expression in decltype ( expression ). */
13088 cp_parser_decltype_expr (cp_parser *parser,
13089 bool &id_expression_or_member_access_p)
13091 cp_token *id_expr_start_token;
13094 /* Since we're going to preserve any side-effects from this parse, set up a
13095 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13096 in the expression. */
13097 tentative_firewall firewall (parser);
13099 /* First, try parsing an id-expression. */
13100 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13101 cp_parser_parse_tentatively (parser);
13102 expr = cp_parser_id_expression (parser,
13103 /*template_keyword_p=*/false,
13104 /*check_dependency_p=*/true,
13105 /*template_p=*/NULL,
13106 /*declarator_p=*/false,
13107 /*optional_p=*/false);
13109 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13111 bool non_integral_constant_expression_p = false;
13112 tree id_expression = expr;
13114 const char *error_msg;
13116 if (identifier_p (expr))
13117 /* Lookup the name we got back from the id-expression. */
13118 expr = cp_parser_lookup_name_simple (parser, expr,
13119 id_expr_start_token->location);
13122 && expr != error_mark_node
13123 && TREE_CODE (expr) != TYPE_DECL
13124 && (TREE_CODE (expr) != BIT_NOT_EXPR
13125 || !TYPE_P (TREE_OPERAND (expr, 0)))
13126 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13128 /* Complete lookup of the id-expression. */
13129 expr = (finish_id_expression
13130 (id_expression, expr, parser->scope, &idk,
13131 /*integral_constant_expression_p=*/false,
13132 /*allow_non_integral_constant_expression_p=*/true,
13133 &non_integral_constant_expression_p,
13134 /*template_p=*/false,
13136 /*address_p=*/false,
13137 /*template_arg_p=*/false,
13139 id_expr_start_token->location));
13141 if (expr == error_mark_node)
13142 /* We found an id-expression, but it was something that we
13143 should not have found. This is an error, not something
13144 we can recover from, so note that we found an
13145 id-expression and we'll recover as gracefully as
13147 id_expression_or_member_access_p = true;
13151 && expr != error_mark_node
13152 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13153 /* We have an id-expression. */
13154 id_expression_or_member_access_p = true;
13157 if (!id_expression_or_member_access_p)
13159 /* Abort the id-expression parse. */
13160 cp_parser_abort_tentative_parse (parser);
13162 /* Parsing tentatively, again. */
13163 cp_parser_parse_tentatively (parser);
13165 /* Parse a class member access. */
13166 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13167 /*cast_p=*/false, /*decltype*/true,
13168 /*member_access_only_p=*/true, NULL);
13171 && expr != error_mark_node
13172 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13173 /* We have an id-expression. */
13174 id_expression_or_member_access_p = true;
13177 if (id_expression_or_member_access_p)
13178 /* We have parsed the complete id-expression or member access. */
13179 cp_parser_parse_definitely (parser);
13182 /* Abort our attempt to parse an id-expression or member access
13184 cp_parser_abort_tentative_parse (parser);
13186 /* Parse a full expression. */
13187 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13188 /*decltype_p=*/true);
13194 /* Parse a `decltype' type. Returns the type.
13196 simple-type-specifier:
13197 decltype ( expression )
13199 decltype ( auto ) */
13202 cp_parser_decltype (cp_parser *parser)
13205 bool id_expression_or_member_access_p = false;
13206 const char *saved_message;
13207 bool saved_integral_constant_expression_p;
13208 bool saved_non_integral_constant_expression_p;
13209 bool saved_greater_than_is_operator_p;
13210 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13212 if (start_token->type == CPP_DECLTYPE)
13214 /* Already parsed. */
13215 cp_lexer_consume_token (parser->lexer);
13216 return saved_checks_value (start_token->u.tree_check_value);
13219 /* Look for the `decltype' token. */
13220 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13221 return error_mark_node;
13223 /* Parse the opening `('. */
13224 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13225 return error_mark_node;
13227 /* decltype (auto) */
13228 if (cxx_dialect >= cxx14
13229 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13231 cp_lexer_consume_token (parser->lexer);
13232 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13233 return error_mark_node;
13234 expr = make_decltype_auto ();
13235 AUTO_IS_DECLTYPE (expr) = true;
13239 /* Types cannot be defined in a `decltype' expression. Save away the
13241 saved_message = parser->type_definition_forbidden_message;
13243 /* And create the new one. */
13244 parser->type_definition_forbidden_message
13245 = G_("types may not be defined in %<decltype%> expressions");
13247 /* The restrictions on constant-expressions do not apply inside
13248 decltype expressions. */
13249 saved_integral_constant_expression_p
13250 = parser->integral_constant_expression_p;
13251 saved_non_integral_constant_expression_p
13252 = parser->non_integral_constant_expression_p;
13253 parser->integral_constant_expression_p = false;
13255 /* Within a parenthesized expression, a `>' token is always
13256 the greater-than operator. */
13257 saved_greater_than_is_operator_p
13258 = parser->greater_than_is_operator_p;
13259 parser->greater_than_is_operator_p = true;
13261 /* Do not actually evaluate the expression. */
13262 ++cp_unevaluated_operand;
13264 /* Do not warn about problems with the expression. */
13265 ++c_inhibit_evaluation_warnings;
13267 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13269 /* Go back to evaluating expressions. */
13270 --cp_unevaluated_operand;
13271 --c_inhibit_evaluation_warnings;
13273 /* The `>' token might be the end of a template-id or
13274 template-parameter-list now. */
13275 parser->greater_than_is_operator_p
13276 = saved_greater_than_is_operator_p;
13278 /* Restore the old message and the integral constant expression
13280 parser->type_definition_forbidden_message = saved_message;
13281 parser->integral_constant_expression_p
13282 = saved_integral_constant_expression_p;
13283 parser->non_integral_constant_expression_p
13284 = saved_non_integral_constant_expression_p;
13286 /* Parse to the closing `)'. */
13287 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13289 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13290 /*consume_paren=*/true);
13291 return error_mark_node;
13294 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13295 tf_warning_or_error);
13298 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13300 start_token->type = CPP_DECLTYPE;
13301 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13302 start_token->u.tree_check_value->value = expr;
13303 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13304 start_token->keyword = RID_MAX;
13305 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13310 /* Special member functions [gram.special] */
13312 /* Parse a conversion-function-id.
13314 conversion-function-id:
13315 operator conversion-type-id
13317 Returns an IDENTIFIER_NODE representing the operator. */
13320 cp_parser_conversion_function_id (cp_parser* parser)
13324 tree saved_qualifying_scope;
13325 tree saved_object_scope;
13326 tree pushed_scope = NULL_TREE;
13328 /* Look for the `operator' token. */
13329 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13330 return error_mark_node;
13331 /* When we parse the conversion-type-id, the current scope will be
13332 reset. However, we need that information in able to look up the
13333 conversion function later, so we save it here. */
13334 saved_scope = parser->scope;
13335 saved_qualifying_scope = parser->qualifying_scope;
13336 saved_object_scope = parser->object_scope;
13337 /* We must enter the scope of the class so that the names of
13338 entities declared within the class are available in the
13339 conversion-type-id. For example, consider:
13346 S::operator I() { ... }
13348 In order to see that `I' is a type-name in the definition, we
13349 must be in the scope of `S'. */
13351 pushed_scope = push_scope (saved_scope);
13352 /* Parse the conversion-type-id. */
13353 type = cp_parser_conversion_type_id (parser);
13354 /* Leave the scope of the class, if any. */
13356 pop_scope (pushed_scope);
13357 /* Restore the saved scope. */
13358 parser->scope = saved_scope;
13359 parser->qualifying_scope = saved_qualifying_scope;
13360 parser->object_scope = saved_object_scope;
13361 /* If the TYPE is invalid, indicate failure. */
13362 if (type == error_mark_node)
13363 return error_mark_node;
13364 return mangle_conv_op_name_for_type (type);
13367 /* Parse a conversion-type-id:
13369 conversion-type-id:
13370 type-specifier-seq conversion-declarator [opt]
13372 Returns the TYPE specified. */
13375 cp_parser_conversion_type_id (cp_parser* parser)
13378 cp_decl_specifier_seq type_specifiers;
13379 cp_declarator *declarator;
13380 tree type_specified;
13381 const char *saved_message;
13383 /* Parse the attributes. */
13384 attributes = cp_parser_attributes_opt (parser);
13386 saved_message = parser->type_definition_forbidden_message;
13387 parser->type_definition_forbidden_message
13388 = G_("types may not be defined in a conversion-type-id");
13390 /* Parse the type-specifiers. */
13391 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13392 /*is_trailing_return=*/false,
13395 parser->type_definition_forbidden_message = saved_message;
13397 /* If that didn't work, stop. */
13398 if (type_specifiers.type == error_mark_node)
13399 return error_mark_node;
13400 /* Parse the conversion-declarator. */
13401 declarator = cp_parser_conversion_declarator_opt (parser);
13403 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13404 /*initialized=*/0, &attributes);
13406 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13408 /* Don't give this error when parsing tentatively. This happens to
13409 work because we always parse this definitively once. */
13410 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13411 && type_uses_auto (type_specified))
13413 if (cxx_dialect < cxx14)
13415 error ("invalid use of %<auto%> in conversion operator");
13416 return error_mark_node;
13418 else if (template_parm_scope_p ())
13419 warning (0, "use of %<auto%> in member template "
13420 "conversion operator can never be deduced");
13423 return type_specified;
13426 /* Parse an (optional) conversion-declarator.
13428 conversion-declarator:
13429 ptr-operator conversion-declarator [opt]
13433 static cp_declarator *
13434 cp_parser_conversion_declarator_opt (cp_parser* parser)
13436 enum tree_code code;
13437 tree class_type, std_attributes = NULL_TREE;
13438 cp_cv_quals cv_quals;
13440 /* We don't know if there's a ptr-operator next, or not. */
13441 cp_parser_parse_tentatively (parser);
13442 /* Try the ptr-operator. */
13443 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13445 /* If it worked, look for more conversion-declarators. */
13446 if (cp_parser_parse_definitely (parser))
13448 cp_declarator *declarator;
13450 /* Parse another optional declarator. */
13451 declarator = cp_parser_conversion_declarator_opt (parser);
13453 declarator = cp_parser_make_indirect_declarator
13454 (code, class_type, cv_quals, declarator, std_attributes);
13462 /* Parse an (optional) ctor-initializer.
13465 : mem-initializer-list
13467 Returns TRUE iff the ctor-initializer was actually present. */
13470 cp_parser_ctor_initializer_opt (cp_parser* parser)
13472 /* If the next token is not a `:', then there is no
13473 ctor-initializer. */
13474 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13476 /* Do default initialization of any bases and members. */
13477 if (DECL_CONSTRUCTOR_P (current_function_decl))
13478 finish_mem_initializers (NULL_TREE);
13483 /* Consume the `:' token. */
13484 cp_lexer_consume_token (parser->lexer);
13485 /* And the mem-initializer-list. */
13486 cp_parser_mem_initializer_list (parser);
13491 /* Parse a mem-initializer-list.
13493 mem-initializer-list:
13494 mem-initializer ... [opt]
13495 mem-initializer ... [opt] , mem-initializer-list */
13498 cp_parser_mem_initializer_list (cp_parser* parser)
13500 tree mem_initializer_list = NULL_TREE;
13501 tree target_ctor = error_mark_node;
13502 cp_token *token = cp_lexer_peek_token (parser->lexer);
13504 /* Let the semantic analysis code know that we are starting the
13505 mem-initializer-list. */
13506 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13507 error_at (token->location,
13508 "only constructors take member initializers");
13510 /* Loop through the list. */
13513 tree mem_initializer;
13515 token = cp_lexer_peek_token (parser->lexer);
13516 /* Parse the mem-initializer. */
13517 mem_initializer = cp_parser_mem_initializer (parser);
13518 /* If the next token is a `...', we're expanding member initializers. */
13519 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13521 /* Consume the `...'. */
13522 cp_lexer_consume_token (parser->lexer);
13524 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13525 can be expanded but members cannot. */
13526 if (mem_initializer != error_mark_node
13527 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13529 error_at (token->location,
13530 "cannot expand initializer for member %<%D%>",
13531 TREE_PURPOSE (mem_initializer));
13532 mem_initializer = error_mark_node;
13535 /* Construct the pack expansion type. */
13536 if (mem_initializer != error_mark_node)
13537 mem_initializer = make_pack_expansion (mem_initializer);
13539 if (target_ctor != error_mark_node
13540 && mem_initializer != error_mark_node)
13542 error ("mem-initializer for %qD follows constructor delegation",
13543 TREE_PURPOSE (mem_initializer));
13544 mem_initializer = error_mark_node;
13546 /* Look for a target constructor. */
13547 if (mem_initializer != error_mark_node
13548 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13549 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13551 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13552 if (mem_initializer_list)
13554 error ("constructor delegation follows mem-initializer for %qD",
13555 TREE_PURPOSE (mem_initializer_list));
13556 mem_initializer = error_mark_node;
13558 target_ctor = mem_initializer;
13560 /* Add it to the list, unless it was erroneous. */
13561 if (mem_initializer != error_mark_node)
13563 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13564 mem_initializer_list = mem_initializer;
13566 /* If the next token is not a `,', we're done. */
13567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13569 /* Consume the `,' token. */
13570 cp_lexer_consume_token (parser->lexer);
13573 /* Perform semantic analysis. */
13574 if (DECL_CONSTRUCTOR_P (current_function_decl))
13575 finish_mem_initializers (mem_initializer_list);
13578 /* Parse a mem-initializer.
13581 mem-initializer-id ( expression-list [opt] )
13582 mem-initializer-id braced-init-list
13587 ( expression-list [opt] )
13589 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13590 class) or FIELD_DECL (for a non-static data member) to initialize;
13591 the TREE_VALUE is the expression-list. An empty initialization
13592 list is represented by void_list_node. */
13595 cp_parser_mem_initializer (cp_parser* parser)
13597 tree mem_initializer_id;
13598 tree expression_list;
13600 cp_token *token = cp_lexer_peek_token (parser->lexer);
13602 /* Find out what is being initialized. */
13603 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13605 permerror (token->location,
13606 "anachronistic old-style base class initializer");
13607 mem_initializer_id = NULL_TREE;
13611 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13612 if (mem_initializer_id == error_mark_node)
13613 return mem_initializer_id;
13615 member = expand_member_init (mem_initializer_id);
13616 if (member && !DECL_P (member))
13617 in_base_initializer = 1;
13619 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13621 bool expr_non_constant_p;
13622 cp_lexer_set_source_position (parser->lexer);
13623 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13624 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13625 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13626 expression_list = build_tree_list (NULL_TREE, expression_list);
13630 vec<tree, va_gc> *vec;
13631 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13633 /*allow_expansion_p=*/true,
13634 /*non_constant_p=*/NULL);
13636 return error_mark_node;
13637 expression_list = build_tree_list_vec (vec);
13638 release_tree_vector (vec);
13641 if (expression_list == error_mark_node)
13642 return error_mark_node;
13643 if (!expression_list)
13644 expression_list = void_type_node;
13646 in_base_initializer = 0;
13648 return member ? build_tree_list (member, expression_list) : error_mark_node;
13651 /* Parse a mem-initializer-id.
13653 mem-initializer-id:
13654 :: [opt] nested-name-specifier [opt] class-name
13655 decltype-specifier (C++11)
13658 Returns a TYPE indicating the class to be initialized for the first
13659 production (and the second in C++11). Returns an IDENTIFIER_NODE
13660 indicating the data member to be initialized for the last production. */
13663 cp_parser_mem_initializer_id (cp_parser* parser)
13665 bool global_scope_p;
13666 bool nested_name_specifier_p;
13667 bool template_p = false;
13670 cp_token *token = cp_lexer_peek_token (parser->lexer);
13672 /* `typename' is not allowed in this context ([temp.res]). */
13673 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13675 error_at (token->location,
13676 "keyword %<typename%> not allowed in this context (a qualified "
13677 "member initializer is implicitly a type)");
13678 cp_lexer_consume_token (parser->lexer);
13680 /* Look for the optional `::' operator. */
13682 = (cp_parser_global_scope_opt (parser,
13683 /*current_scope_valid_p=*/false)
13685 /* Look for the optional nested-name-specifier. The simplest way to
13690 The keyword `typename' is not permitted in a base-specifier or
13691 mem-initializer; in these contexts a qualified name that
13692 depends on a template-parameter is implicitly assumed to be a
13695 is to assume that we have seen the `typename' keyword at this
13697 nested_name_specifier_p
13698 = (cp_parser_nested_name_specifier_opt (parser,
13699 /*typename_keyword_p=*/true,
13700 /*check_dependency_p=*/true,
13702 /*is_declaration=*/true)
13704 if (nested_name_specifier_p)
13705 template_p = cp_parser_optional_template_keyword (parser);
13706 /* If there is a `::' operator or a nested-name-specifier, then we
13707 are definitely looking for a class-name. */
13708 if (global_scope_p || nested_name_specifier_p)
13709 return cp_parser_class_name (parser,
13710 /*typename_keyword_p=*/true,
13711 /*template_keyword_p=*/template_p,
13713 /*check_dependency_p=*/true,
13714 /*class_head_p=*/false,
13715 /*is_declaration=*/true);
13716 /* Otherwise, we could also be looking for an ordinary identifier. */
13717 cp_parser_parse_tentatively (parser);
13718 if (cp_lexer_next_token_is_decltype (parser->lexer))
13719 /* Try a decltype-specifier. */
13720 id = cp_parser_decltype (parser);
13722 /* Otherwise, try a class-name. */
13723 id = cp_parser_class_name (parser,
13724 /*typename_keyword_p=*/true,
13725 /*template_keyword_p=*/false,
13727 /*check_dependency_p=*/true,
13728 /*class_head_p=*/false,
13729 /*is_declaration=*/true);
13730 /* If we found one, we're done. */
13731 if (cp_parser_parse_definitely (parser))
13733 /* Otherwise, look for an ordinary identifier. */
13734 return cp_parser_identifier (parser);
13737 /* Overloading [gram.over] */
13739 /* Parse an operator-function-id.
13741 operator-function-id:
13744 Returns an IDENTIFIER_NODE for the operator which is a
13745 human-readable spelling of the identifier, e.g., `operator +'. */
13748 cp_parser_operator_function_id (cp_parser* parser)
13750 /* Look for the `operator' keyword. */
13751 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13752 return error_mark_node;
13753 /* And then the name of the operator itself. */
13754 return cp_parser_operator (parser);
13757 /* Return an identifier node for a user-defined literal operator.
13758 The suffix identifier is chained to the operator name identifier. */
13761 cp_literal_operator_id (const char* name)
13764 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13765 + strlen (name) + 10);
13766 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13767 identifier = get_identifier (buffer);
13772 /* Parse an operator.
13775 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13776 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13777 || ++ -- , ->* -> () []
13784 Returns an IDENTIFIER_NODE for the operator which is a
13785 human-readable spelling of the identifier, e.g., `operator +'. */
13788 cp_parser_operator (cp_parser* parser)
13790 tree id = NULL_TREE;
13794 /* Peek at the next token. */
13795 token = cp_lexer_peek_token (parser->lexer);
13797 location_t start_loc = token->location;
13799 /* Figure out which operator we have. */
13800 switch (token->type)
13806 /* The keyword should be either `new' or `delete'. */
13807 if (token->keyword == RID_NEW)
13809 else if (token->keyword == RID_DELETE)
13814 /* Consume the `new' or `delete' token. */
13815 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13817 /* Peek at the next token. */
13818 token = cp_lexer_peek_token (parser->lexer);
13819 /* If it's a `[' token then this is the array variant of the
13821 if (token->type == CPP_OPEN_SQUARE)
13823 /* Consume the `[' token. */
13824 cp_lexer_consume_token (parser->lexer);
13825 /* Look for the `]' token. */
13826 if (cp_token *close_token
13827 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13828 end_loc = close_token->location;
13829 id = ansi_opname (op == NEW_EXPR
13830 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13832 /* Otherwise, we have the non-array variant. */
13834 id = ansi_opname (op);
13836 location_t loc = make_location (start_loc, start_loc, end_loc);
13838 return cp_expr (id, loc);
13842 id = ansi_opname (PLUS_EXPR);
13846 id = ansi_opname (MINUS_EXPR);
13850 id = ansi_opname (MULT_EXPR);
13854 id = ansi_opname (TRUNC_DIV_EXPR);
13858 id = ansi_opname (TRUNC_MOD_EXPR);
13862 id = ansi_opname (BIT_XOR_EXPR);
13866 id = ansi_opname (BIT_AND_EXPR);
13870 id = ansi_opname (BIT_IOR_EXPR);
13874 id = ansi_opname (BIT_NOT_EXPR);
13878 id = ansi_opname (TRUTH_NOT_EXPR);
13882 id = ansi_assopname (NOP_EXPR);
13886 id = ansi_opname (LT_EXPR);
13890 id = ansi_opname (GT_EXPR);
13894 id = ansi_assopname (PLUS_EXPR);
13898 id = ansi_assopname (MINUS_EXPR);
13902 id = ansi_assopname (MULT_EXPR);
13906 id = ansi_assopname (TRUNC_DIV_EXPR);
13910 id = ansi_assopname (TRUNC_MOD_EXPR);
13914 id = ansi_assopname (BIT_XOR_EXPR);
13918 id = ansi_assopname (BIT_AND_EXPR);
13922 id = ansi_assopname (BIT_IOR_EXPR);
13926 id = ansi_opname (LSHIFT_EXPR);
13930 id = ansi_opname (RSHIFT_EXPR);
13933 case CPP_LSHIFT_EQ:
13934 id = ansi_assopname (LSHIFT_EXPR);
13937 case CPP_RSHIFT_EQ:
13938 id = ansi_assopname (RSHIFT_EXPR);
13942 id = ansi_opname (EQ_EXPR);
13946 id = ansi_opname (NE_EXPR);
13950 id = ansi_opname (LE_EXPR);
13953 case CPP_GREATER_EQ:
13954 id = ansi_opname (GE_EXPR);
13958 id = ansi_opname (TRUTH_ANDIF_EXPR);
13962 id = ansi_opname (TRUTH_ORIF_EXPR);
13965 case CPP_PLUS_PLUS:
13966 id = ansi_opname (POSTINCREMENT_EXPR);
13969 case CPP_MINUS_MINUS:
13970 id = ansi_opname (PREDECREMENT_EXPR);
13974 id = ansi_opname (COMPOUND_EXPR);
13977 case CPP_DEREF_STAR:
13978 id = ansi_opname (MEMBER_REF);
13982 id = ansi_opname (COMPONENT_REF);
13985 case CPP_OPEN_PAREN:
13986 /* Consume the `('. */
13987 cp_lexer_consume_token (parser->lexer);
13988 /* Look for the matching `)'. */
13989 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13990 return ansi_opname (CALL_EXPR);
13992 case CPP_OPEN_SQUARE:
13993 /* Consume the `['. */
13994 cp_lexer_consume_token (parser->lexer);
13995 /* Look for the matching `]'. */
13996 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13997 return ansi_opname (ARRAY_REF);
13999 case CPP_UTF8STRING:
14000 case CPP_UTF8STRING_USERDEF:
14006 case CPP_STRING_USERDEF:
14007 case CPP_WSTRING_USERDEF:
14008 case CPP_STRING16_USERDEF:
14009 case CPP_STRING32_USERDEF:
14011 tree str, string_tree;
14014 if (cxx_dialect == cxx98)
14015 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14017 /* Consume the string. */
14018 str = cp_parser_string_literal (parser, /*translate=*/true,
14019 /*wide_ok=*/true, /*lookup_udlit=*/false);
14020 if (str == error_mark_node)
14021 return error_mark_node;
14022 else if (TREE_CODE (str) == USERDEF_LITERAL)
14024 string_tree = USERDEF_LITERAL_VALUE (str);
14025 id = USERDEF_LITERAL_SUFFIX_ID (str);
14030 /* Look for the suffix identifier. */
14031 token = cp_lexer_peek_token (parser->lexer);
14032 if (token->type == CPP_NAME)
14033 id = cp_parser_identifier (parser);
14034 else if (token->type == CPP_KEYWORD)
14036 error ("unexpected keyword;"
14037 " remove space between quotes and suffix identifier");
14038 return error_mark_node;
14042 error ("expected suffix identifier");
14043 return error_mark_node;
14046 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14047 (TREE_TYPE (TREE_TYPE (string_tree))));
14048 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14051 error ("expected empty string after %<operator%> keyword");
14052 return error_mark_node;
14054 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14057 error ("invalid encoding prefix in literal operator");
14058 return error_mark_node;
14060 if (id != error_mark_node)
14062 const char *name = IDENTIFIER_POINTER (id);
14063 id = cp_literal_operator_id (name);
14069 /* Anything else is an error. */
14073 /* If we have selected an identifier, we need to consume the
14076 cp_lexer_consume_token (parser->lexer);
14077 /* Otherwise, no valid operator name was present. */
14080 cp_parser_error (parser, "expected operator");
14081 id = error_mark_node;
14084 return cp_expr (id, start_loc);
14087 /* Parse a template-declaration.
14089 template-declaration:
14090 export [opt] template < template-parameter-list > declaration
14092 If MEMBER_P is TRUE, this template-declaration occurs within a
14095 The grammar rule given by the standard isn't correct. What
14096 is really meant is:
14098 template-declaration:
14099 export [opt] template-parameter-list-seq
14100 decl-specifier-seq [opt] init-declarator [opt] ;
14101 export [opt] template-parameter-list-seq
14102 function-definition
14104 template-parameter-list-seq:
14105 template-parameter-list-seq [opt]
14106 template < template-parameter-list >
14108 Concept Extensions:
14110 template-parameter-list-seq:
14111 template < template-parameter-list > requires-clause [opt]
14114 requires logical-or-expression */
14117 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14119 /* Check for `export'. */
14120 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14122 /* Consume the `export' token. */
14123 cp_lexer_consume_token (parser->lexer);
14124 /* Warn that we do not support `export'. */
14125 warning (0, "keyword %<export%> not implemented, and will be ignored");
14128 cp_parser_template_declaration_after_export (parser, member_p);
14131 /* Parse a template-parameter-list.
14133 template-parameter-list:
14135 template-parameter-list , template-parameter
14137 Returns a TREE_LIST. Each node represents a template parameter.
14138 The nodes are connected via their TREE_CHAINs. */
14141 cp_parser_template_parameter_list (cp_parser* parser)
14143 tree parameter_list = NULL_TREE;
14145 begin_template_parm_list ();
14147 /* The loop below parses the template parms. We first need to know
14148 the total number of template parms to be able to compute proper
14149 canonical types of each dependent type. So after the loop, when
14150 we know the total number of template parms,
14151 end_template_parm_list computes the proper canonical types and
14152 fixes up the dependent types accordingly. */
14157 bool is_parameter_pack;
14158 location_t parm_loc;
14160 /* Parse the template-parameter. */
14161 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14162 parameter = cp_parser_template_parameter (parser,
14164 &is_parameter_pack);
14165 /* Add it to the list. */
14166 if (parameter != error_mark_node)
14167 parameter_list = process_template_parm (parameter_list,
14171 is_parameter_pack);
14174 tree err_parm = build_tree_list (parameter, parameter);
14175 parameter_list = chainon (parameter_list, err_parm);
14178 /* If the next token is not a `,', we're done. */
14179 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14181 /* Otherwise, consume the `,' token. */
14182 cp_lexer_consume_token (parser->lexer);
14185 return end_template_parm_list (parameter_list);
14188 /* Parse a introduction-list.
14191 introduced-parameter
14192 introduction-list , introduced-parameter
14194 introduced-parameter:
14195 ...[opt] identifier
14197 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14198 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14199 WILDCARD_DECL will also have DECL_NAME set and token location in
14200 DECL_SOURCE_LOCATION. */
14203 cp_parser_introduction_list (cp_parser *parser)
14205 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14209 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14211 cp_lexer_consume_token (parser->lexer);
14213 /* Build placeholder. */
14214 tree parm = build_nt (WILDCARD_DECL);
14215 DECL_SOURCE_LOCATION (parm)
14216 = cp_lexer_peek_token (parser->lexer)->location;
14217 DECL_NAME (parm) = cp_parser_identifier (parser);
14218 WILDCARD_PACK_P (parm) = is_pack;
14219 vec_safe_push (introduction_vec, parm);
14221 /* If the next token is not a `,', we're done. */
14222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14224 /* Otherwise, consume the `,' token. */
14225 cp_lexer_consume_token (parser->lexer);
14228 /* Convert the vec into a TREE_VEC. */
14229 tree introduction_list = make_tree_vec (introduction_vec->length ());
14232 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14233 TREE_VEC_ELT (introduction_list, n) = parm;
14235 release_tree_vector (introduction_vec);
14236 return introduction_list;
14239 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14240 is an abstract declarator. */
14242 static inline cp_declarator*
14243 get_id_declarator (cp_declarator *declarator)
14245 cp_declarator *d = declarator;
14246 while (d && d->kind != cdk_id)
14251 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14252 is an abstract declarator. */
14255 get_unqualified_id (cp_declarator *declarator)
14257 declarator = get_id_declarator (declarator);
14259 return declarator->u.id.unqualified_name;
14264 /* Returns true if DECL represents a constrained-parameter. */
14267 is_constrained_parameter (tree decl)
14270 && TREE_CODE (decl) == TYPE_DECL
14271 && CONSTRAINED_PARM_CONCEPT (decl)
14272 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14275 /* Returns true if PARM declares a constrained-parameter. */
14278 is_constrained_parameter (cp_parameter_declarator *parm)
14280 return is_constrained_parameter (parm->decl_specifiers.type);
14283 /* Check that the type parameter is only a declarator-id, and that its
14284 type is not cv-qualified. */
14287 cp_parser_check_constrained_type_parm (cp_parser *parser,
14288 cp_parameter_declarator *parm)
14290 if (!parm->declarator)
14293 if (parm->declarator->kind != cdk_id)
14295 cp_parser_error (parser, "invalid constrained type parameter");
14299 /* Don't allow cv-qualified type parameters. */
14300 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14301 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14303 cp_parser_error (parser, "cv-qualified type parameter");
14310 /* Finish parsing/processing a template type parameter and checking
14311 various restrictions. */
14314 cp_parser_constrained_type_template_parm (cp_parser *parser,
14316 cp_parameter_declarator* parmdecl)
14318 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14319 return finish_template_type_parm (class_type_node, id);
14321 return error_mark_node;
14325 finish_constrained_template_template_parm (tree proto, tree id)
14327 /* FIXME: This should probably be copied, and we may need to adjust
14328 the template parameter depths. */
14329 tree saved_parms = current_template_parms;
14330 begin_template_parm_list ();
14331 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14332 end_template_parm_list ();
14334 tree parm = finish_template_template_parm (class_type_node, id);
14335 current_template_parms = saved_parms;
14340 /* Finish parsing/processing a template template parameter by borrowing
14341 the template parameter list from the prototype parameter. */
14344 cp_parser_constrained_template_template_parm (cp_parser *parser,
14347 cp_parameter_declarator *parmdecl)
14349 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14350 return error_mark_node;
14351 return finish_constrained_template_template_parm (proto, id);
14354 /* Create a new non-type template parameter from the given PARM
14358 constrained_non_type_template_parm (bool *is_non_type,
14359 cp_parameter_declarator *parm)
14361 *is_non_type = true;
14362 cp_declarator *decl = parm->declarator;
14363 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14364 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14365 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14368 /* Build a constrained template parameter based on the PARMDECL
14369 declarator. The type of PARMDECL is the constrained type, which
14370 refers to the prototype template parameter that ultimately
14371 specifies the type of the declared parameter. */
14374 finish_constrained_parameter (cp_parser *parser,
14375 cp_parameter_declarator *parmdecl,
14377 bool *is_parameter_pack)
14379 tree decl = parmdecl->decl_specifiers.type;
14380 tree id = get_unqualified_id (parmdecl->declarator);
14381 tree def = parmdecl->default_argument;
14382 tree proto = DECL_INITIAL (decl);
14384 /* A template parameter constrained by a variadic concept shall also
14385 be declared as a template parameter pack. */
14386 bool is_variadic = template_parameter_pack_p (proto);
14387 if (is_variadic && !*is_parameter_pack)
14388 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14390 /* Build the parameter. Return an error if the declarator was invalid. */
14392 if (TREE_CODE (proto) == TYPE_DECL)
14393 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14394 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14395 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14398 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14399 if (parm == error_mark_node)
14400 return error_mark_node;
14402 /* Finish the parameter decl and create a node attaching the
14403 default argument and constraint. */
14404 parm = build_tree_list (def, parm);
14405 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14410 /* Returns true if the parsed type actually represents the declaration
14411 of a type template-parameter. */
14414 declares_constrained_type_template_parameter (tree type)
14416 return (is_constrained_parameter (type)
14417 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14421 /* Returns true if the parsed type actually represents the declaration of
14422 a template template-parameter. */
14425 declares_constrained_template_template_parameter (tree type)
14427 return (is_constrained_parameter (type)
14428 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14431 /* Parse a default argument for a type template-parameter.
14432 Note that diagnostics are handled in cp_parser_template_parameter. */
14435 cp_parser_default_type_template_argument (cp_parser *parser)
14437 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14439 /* Consume the `=' token. */
14440 cp_lexer_consume_token (parser->lexer);
14442 cp_token *token = cp_lexer_peek_token (parser->lexer);
14444 /* Parse the default-argument. */
14445 push_deferring_access_checks (dk_no_deferred);
14446 tree default_argument = cp_parser_type_id (parser);
14447 pop_deferring_access_checks ();
14449 if (flag_concepts && type_uses_auto (default_argument))
14451 error_at (token->location,
14452 "invalid use of %<auto%> in default template argument");
14453 return error_mark_node;
14456 return default_argument;
14459 /* Parse a default argument for a template template-parameter. */
14462 cp_parser_default_template_template_argument (cp_parser *parser)
14464 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14468 /* Consume the `='. */
14469 cp_lexer_consume_token (parser->lexer);
14470 /* Parse the id-expression. */
14471 push_deferring_access_checks (dk_no_deferred);
14472 /* save token before parsing the id-expression, for error
14474 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14475 tree default_argument
14476 = cp_parser_id_expression (parser,
14477 /*template_keyword_p=*/false,
14478 /*check_dependency_p=*/true,
14479 /*template_p=*/&is_template,
14480 /*declarator_p=*/false,
14481 /*optional_p=*/false);
14482 if (TREE_CODE (default_argument) == TYPE_DECL)
14483 /* If the id-expression was a template-id that refers to
14484 a template-class, we already have the declaration here,
14485 so no further lookup is needed. */
14488 /* Look up the name. */
14490 = cp_parser_lookup_name (parser, default_argument,
14492 /*is_template=*/is_template,
14493 /*is_namespace=*/false,
14494 /*check_dependency=*/true,
14495 /*ambiguous_decls=*/NULL,
14497 /* See if the default argument is valid. */
14498 default_argument = check_template_template_default_arg (default_argument);
14499 pop_deferring_access_checks ();
14500 return default_argument;
14503 /* Parse a template-parameter.
14505 template-parameter:
14507 parameter-declaration
14509 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14510 the parameter. The TREE_PURPOSE is the default value, if any.
14511 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14512 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14513 set to true iff this parameter is a parameter pack. */
14516 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14517 bool *is_parameter_pack)
14520 cp_parameter_declarator *parameter_declarator;
14523 /* Assume it is a type parameter or a template parameter. */
14524 *is_non_type = false;
14525 /* Assume it not a parameter pack. */
14526 *is_parameter_pack = false;
14527 /* Peek at the next token. */
14528 token = cp_lexer_peek_token (parser->lexer);
14529 /* If it is `class' or `template', we have a type-parameter. */
14530 if (token->keyword == RID_TEMPLATE)
14531 return cp_parser_type_parameter (parser, is_parameter_pack);
14532 /* If it is `class' or `typename' we do not know yet whether it is a
14533 type parameter or a non-type parameter. Consider:
14535 template <typename T, typename T::X X> ...
14539 template <class C, class D*> ...
14541 Here, the first parameter is a type parameter, and the second is
14542 a non-type parameter. We can tell by looking at the token after
14543 the identifier -- if it is a `,', `=', or `>' then we have a type
14545 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14547 /* Peek at the token after `class' or `typename'. */
14548 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14549 /* If it's an ellipsis, we have a template type parameter
14551 if (token->type == CPP_ELLIPSIS)
14552 return cp_parser_type_parameter (parser, is_parameter_pack);
14553 /* If it's an identifier, skip it. */
14554 if (token->type == CPP_NAME)
14555 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14556 /* Now, see if the token looks like the end of a template
14558 if (token->type == CPP_COMMA
14559 || token->type == CPP_EQ
14560 || token->type == CPP_GREATER)
14561 return cp_parser_type_parameter (parser, is_parameter_pack);
14564 /* Otherwise, it is a non-type parameter or a constrained parameter.
14568 When parsing a default template-argument for a non-type
14569 template-parameter, the first non-nested `>' is taken as the end
14570 of the template parameter-list rather than a greater-than
14572 parameter_declarator
14573 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14574 /*parenthesized_p=*/NULL);
14576 if (!parameter_declarator)
14577 return error_mark_node;
14579 /* If the parameter declaration is marked as a parameter pack, set
14580 *IS_PARAMETER_PACK to notify the caller. */
14581 if (parameter_declarator->template_parameter_pack_p)
14582 *is_parameter_pack = true;
14584 if (parameter_declarator->default_argument)
14586 /* Can happen in some cases of erroneous input (c++/34892). */
14587 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14588 /* Consume the `...' for better error recovery. */
14589 cp_lexer_consume_token (parser->lexer);
14592 // The parameter may have been constrained.
14593 if (is_constrained_parameter (parameter_declarator))
14594 return finish_constrained_parameter (parser,
14595 parameter_declarator,
14597 is_parameter_pack);
14599 // Now we're sure that the parameter is a non-type parameter.
14600 *is_non_type = true;
14602 parm = grokdeclarator (parameter_declarator->declarator,
14603 ¶meter_declarator->decl_specifiers,
14604 TPARM, /*initialized=*/0,
14605 /*attrlist=*/NULL);
14606 if (parm == error_mark_node)
14607 return error_mark_node;
14609 return build_tree_list (parameter_declarator->default_argument, parm);
14612 /* Parse a type-parameter.
14615 class identifier [opt]
14616 class identifier [opt] = type-id
14617 typename identifier [opt]
14618 typename identifier [opt] = type-id
14619 template < template-parameter-list > class identifier [opt]
14620 template < template-parameter-list > class identifier [opt]
14623 GNU Extension (variadic templates):
14626 class ... identifier [opt]
14627 typename ... identifier [opt]
14629 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14630 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14631 the declaration of the parameter.
14633 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14636 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14641 /* Look for a keyword to tell us what kind of parameter this is. */
14642 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14644 return error_mark_node;
14646 switch (token->keyword)
14652 tree default_argument;
14654 /* If the next token is an ellipsis, we have a template
14656 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14658 /* Consume the `...' token. */
14659 cp_lexer_consume_token (parser->lexer);
14660 maybe_warn_variadic_templates ();
14662 *is_parameter_pack = true;
14665 /* If the next token is an identifier, then it names the
14667 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14668 identifier = cp_parser_identifier (parser);
14670 identifier = NULL_TREE;
14672 /* Create the parameter. */
14673 parameter = finish_template_type_parm (class_type_node, identifier);
14675 /* If the next token is an `=', we have a default argument. */
14676 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14679 = cp_parser_default_type_template_argument (parser);
14681 /* Template parameter packs cannot have default
14683 if (*is_parameter_pack)
14686 error_at (token->location,
14687 "template parameter pack %qD cannot have a "
14688 "default argument", identifier);
14690 error_at (token->location,
14691 "template parameter packs cannot have "
14692 "default arguments");
14693 default_argument = NULL_TREE;
14695 else if (check_for_bare_parameter_packs (default_argument))
14696 default_argument = error_mark_node;
14699 default_argument = NULL_TREE;
14701 /* Create the combined representation of the parameter and the
14702 default argument. */
14703 parameter = build_tree_list (default_argument, parameter);
14710 tree default_argument;
14712 /* Look for the `<'. */
14713 cp_parser_require (parser, CPP_LESS, RT_LESS);
14714 /* Parse the template-parameter-list. */
14715 cp_parser_template_parameter_list (parser);
14716 /* Look for the `>'. */
14717 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14719 // If template requirements are present, parse them.
14722 tree reqs = get_shorthand_constraints (current_template_parms);
14723 if (tree r = cp_parser_requires_clause_opt (parser))
14724 reqs = conjoin_constraints (reqs, normalize_expression (r));
14725 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14728 /* Look for the `class' or 'typename' keywords. */
14729 cp_parser_type_parameter_key (parser);
14730 /* If the next token is an ellipsis, we have a template
14732 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14734 /* Consume the `...' token. */
14735 cp_lexer_consume_token (parser->lexer);
14736 maybe_warn_variadic_templates ();
14738 *is_parameter_pack = true;
14740 /* If the next token is an `=', then there is a
14741 default-argument. If the next token is a `>', we are at
14742 the end of the parameter-list. If the next token is a `,',
14743 then we are at the end of this parameter. */
14744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14745 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14746 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14748 identifier = cp_parser_identifier (parser);
14749 /* Treat invalid names as if the parameter were nameless. */
14750 if (identifier == error_mark_node)
14751 identifier = NULL_TREE;
14754 identifier = NULL_TREE;
14756 /* Create the template parameter. */
14757 parameter = finish_template_template_parm (class_type_node,
14760 /* If the next token is an `=', then there is a
14761 default-argument. */
14762 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14765 = cp_parser_default_template_template_argument (parser);
14767 /* Template parameter packs cannot have default
14769 if (*is_parameter_pack)
14772 error_at (token->location,
14773 "template parameter pack %qD cannot "
14774 "have a default argument",
14777 error_at (token->location, "template parameter packs cannot "
14778 "have default arguments");
14779 default_argument = NULL_TREE;
14783 default_argument = NULL_TREE;
14785 /* Create the combined representation of the parameter and the
14786 default argument. */
14787 parameter = build_tree_list (default_argument, parameter);
14792 gcc_unreachable ();
14799 /* Parse a template-id.
14802 template-name < template-argument-list [opt] >
14804 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14805 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14806 returned. Otherwise, if the template-name names a function, or set
14807 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14808 names a class, returns a TYPE_DECL for the specialization.
14810 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14811 uninstantiated templates. */
14814 cp_parser_template_id (cp_parser *parser,
14815 bool template_keyword_p,
14816 bool check_dependency_p,
14817 enum tag_types tag_type,
14818 bool is_declaration)
14823 cp_token_position start_of_id = 0;
14824 cp_token *next_token = NULL, *next_token_2 = NULL;
14825 bool is_identifier;
14827 /* If the next token corresponds to a template-id, there is no need
14829 next_token = cp_lexer_peek_token (parser->lexer);
14830 if (next_token->type == CPP_TEMPLATE_ID)
14832 cp_lexer_consume_token (parser->lexer);
14833 return saved_checks_value (next_token->u.tree_check_value);
14836 /* Avoid performing name lookup if there is no possibility of
14837 finding a template-id. */
14838 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14839 || (next_token->type == CPP_NAME
14840 && !cp_parser_nth_token_starts_template_argument_list_p
14843 cp_parser_error (parser, "expected template-id");
14844 return error_mark_node;
14847 /* Remember where the template-id starts. */
14848 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14849 start_of_id = cp_lexer_token_position (parser->lexer, false);
14851 push_deferring_access_checks (dk_deferred);
14853 /* Parse the template-name. */
14854 is_identifier = false;
14855 templ = cp_parser_template_name (parser, template_keyword_p,
14856 check_dependency_p,
14860 if (templ == error_mark_node || is_identifier)
14862 pop_deferring_access_checks ();
14866 /* Since we're going to preserve any side-effects from this parse, set up a
14867 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14868 in the template arguments. */
14869 tentative_firewall firewall (parser);
14871 /* If we find the sequence `[:' after a template-name, it's probably
14872 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14873 parse correctly the argument list. */
14874 next_token = cp_lexer_peek_token (parser->lexer);
14875 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14876 if (next_token->type == CPP_OPEN_SQUARE
14877 && next_token->flags & DIGRAPH
14878 && next_token_2->type == CPP_COLON
14879 && !(next_token_2->flags & PREV_WHITE))
14881 cp_parser_parse_tentatively (parser);
14882 /* Change `:' into `::'. */
14883 next_token_2->type = CPP_SCOPE;
14884 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14886 cp_lexer_consume_token (parser->lexer);
14888 /* Parse the arguments. */
14889 arguments = cp_parser_enclosed_template_argument_list (parser);
14890 if (!cp_parser_parse_definitely (parser))
14892 /* If we couldn't parse an argument list, then we revert our changes
14893 and return simply an error. Maybe this is not a template-id
14895 next_token_2->type = CPP_COLON;
14896 cp_parser_error (parser, "expected %<<%>");
14897 pop_deferring_access_checks ();
14898 return error_mark_node;
14900 /* Otherwise, emit an error about the invalid digraph, but continue
14901 parsing because we got our argument list. */
14902 if (permerror (next_token->location,
14903 "%<<::%> cannot begin a template-argument list"))
14905 static bool hint = false;
14906 inform (next_token->location,
14907 "%<<:%> is an alternate spelling for %<[%>."
14908 " Insert whitespace between %<<%> and %<::%>");
14909 if (!hint && !flag_permissive)
14911 inform (next_token->location, "(if you use %<-fpermissive%> "
14912 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14913 "accept your code)");
14920 /* Look for the `<' that starts the template-argument-list. */
14921 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14923 pop_deferring_access_checks ();
14924 return error_mark_node;
14926 /* Parse the arguments. */
14927 arguments = cp_parser_enclosed_template_argument_list (parser);
14930 /* Build a representation of the specialization. */
14931 if (identifier_p (templ))
14932 template_id = build_min_nt_loc (next_token->location,
14935 else if (DECL_TYPE_TEMPLATE_P (templ)
14936 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14938 bool entering_scope;
14939 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14940 template (rather than some instantiation thereof) only if
14941 is not nested within some other construct. For example, in
14942 "template <typename T> void f(T) { A<T>::", A<T> is just an
14943 instantiation of A. */
14944 entering_scope = (template_parm_scope_p ()
14945 && cp_lexer_next_token_is (parser->lexer,
14948 = finish_template_type (templ, arguments, entering_scope);
14950 /* A template-like identifier may be a partial concept id. */
14951 else if (flag_concepts
14952 && (template_id = (cp_parser_maybe_partial_concept_id
14953 (parser, templ, arguments))))
14954 return template_id;
14955 else if (variable_template_p (templ))
14957 template_id = lookup_template_variable (templ, arguments);
14958 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14959 SET_EXPR_LOCATION (template_id, next_token->location);
14963 /* If it's not a class-template or a template-template, it should be
14964 a function-template. */
14965 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14966 || TREE_CODE (templ) == OVERLOAD
14967 || BASELINK_P (templ)));
14969 template_id = lookup_template_function (templ, arguments);
14970 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14971 SET_EXPR_LOCATION (template_id, next_token->location);
14974 /* If parsing tentatively, replace the sequence of tokens that makes
14975 up the template-id with a CPP_TEMPLATE_ID token. That way,
14976 should we re-parse the token stream, we will not have to repeat
14977 the effort required to do the parse, nor will we issue duplicate
14978 error messages about problems during instantiation of the
14981 /* Don't do this if we had a parse error in a declarator; re-parsing
14982 might succeed if a name changes meaning (60361). */
14983 && !(cp_parser_error_occurred (parser)
14984 && cp_parser_parsing_tentatively (parser)
14985 && parser->in_declarator_p))
14987 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14989 /* Reset the contents of the START_OF_ID token. */
14990 token->type = CPP_TEMPLATE_ID;
14992 /* Update the location to be of the form:
14993 template-name < template-argument-list [opt] >
14994 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14995 with caret == start at the start of the template-name,
14996 ranging until the closing '>'. */
14997 location_t finish_loc
14998 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
14999 location_t combined_loc
15000 = make_location (token->location, token->location, finish_loc);
15001 token->location = combined_loc;
15003 /* Retrieve any deferred checks. Do not pop this access checks yet
15004 so the memory will not be reclaimed during token replacing below. */
15005 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15006 token->u.tree_check_value->value = template_id;
15007 token->u.tree_check_value->checks = get_deferred_access_checks ();
15008 token->keyword = RID_MAX;
15010 /* Purge all subsequent tokens. */
15011 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15013 /* ??? Can we actually assume that, if template_id ==
15014 error_mark_node, we will have issued a diagnostic to the
15015 user, as opposed to simply marking the tentative parse as
15017 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15018 error_at (token->location, "parse error in template argument list");
15021 pop_to_parent_deferring_access_checks ();
15022 return template_id;
15025 /* Parse a template-name.
15030 The standard should actually say:
15034 operator-function-id
15036 A defect report has been filed about this issue.
15038 A conversion-function-id cannot be a template name because they cannot
15039 be part of a template-id. In fact, looking at this code:
15041 a.operator K<int>()
15043 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15044 It is impossible to call a templated conversion-function-id with an
15045 explicit argument list, since the only allowed template parameter is
15046 the type to which it is converting.
15048 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15049 `template' keyword, in a construction like:
15053 In that case `f' is taken to be a template-name, even though there
15054 is no way of knowing for sure.
15056 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15057 name refers to a set of overloaded functions, at least one of which
15058 is a template, or an IDENTIFIER_NODE with the name of the template,
15059 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15060 names are looked up inside uninstantiated templates. */
15063 cp_parser_template_name (cp_parser* parser,
15064 bool template_keyword_p,
15065 bool check_dependency_p,
15066 bool is_declaration,
15067 enum tag_types tag_type,
15068 bool *is_identifier)
15073 cp_token *token = cp_lexer_peek_token (parser->lexer);
15075 /* If the next token is `operator', then we have either an
15076 operator-function-id or a conversion-function-id. */
15077 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15079 /* We don't know whether we're looking at an
15080 operator-function-id or a conversion-function-id. */
15081 cp_parser_parse_tentatively (parser);
15082 /* Try an operator-function-id. */
15083 identifier = cp_parser_operator_function_id (parser);
15084 /* If that didn't work, try a conversion-function-id. */
15085 if (!cp_parser_parse_definitely (parser))
15087 cp_parser_error (parser, "expected template-name");
15088 return error_mark_node;
15091 /* Look for the identifier. */
15093 identifier = cp_parser_identifier (parser);
15095 /* If we didn't find an identifier, we don't have a template-id. */
15096 if (identifier == error_mark_node)
15097 return error_mark_node;
15099 /* If the name immediately followed the `template' keyword, then it
15100 is a template-name. However, if the next token is not `<', then
15101 we do not treat it as a template-name, since it is not being used
15102 as part of a template-id. This enables us to handle constructs
15105 template <typename T> struct S { S(); };
15106 template <typename T> S<T>::S();
15108 correctly. We would treat `S' as a template -- if it were `S<T>'
15109 -- but we do not if there is no `<'. */
15111 if (processing_template_decl
15112 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15114 /* In a declaration, in a dependent context, we pretend that the
15115 "template" keyword was present in order to improve error
15116 recovery. For example, given:
15118 template <typename T> void f(T::X<int>);
15120 we want to treat "X<int>" as a template-id. */
15122 && !template_keyword_p
15123 && parser->scope && TYPE_P (parser->scope)
15124 && check_dependency_p
15125 && dependent_scope_p (parser->scope)
15126 /* Do not do this for dtors (or ctors), since they never
15127 need the template keyword before their name. */
15128 && !constructor_name_p (identifier, parser->scope))
15130 cp_token_position start = 0;
15132 /* Explain what went wrong. */
15133 error_at (token->location, "non-template %qD used as template",
15135 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15136 parser->scope, identifier);
15137 /* If parsing tentatively, find the location of the "<" token. */
15138 if (cp_parser_simulate_error (parser))
15139 start = cp_lexer_token_position (parser->lexer, true);
15140 /* Parse the template arguments so that we can issue error
15141 messages about them. */
15142 cp_lexer_consume_token (parser->lexer);
15143 cp_parser_enclosed_template_argument_list (parser);
15144 /* Skip tokens until we find a good place from which to
15145 continue parsing. */
15146 cp_parser_skip_to_closing_parenthesis (parser,
15147 /*recovering=*/true,
15149 /*consume_paren=*/false);
15150 /* If parsing tentatively, permanently remove the
15151 template argument list. That will prevent duplicate
15152 error messages from being issued about the missing
15153 "template" keyword. */
15155 cp_lexer_purge_tokens_after (parser->lexer, start);
15157 *is_identifier = true;
15161 /* If the "template" keyword is present, then there is generally
15162 no point in doing name-lookup, so we just return IDENTIFIER.
15163 But, if the qualifying scope is non-dependent then we can
15164 (and must) do name-lookup normally. */
15165 if (template_keyword_p
15167 || (TYPE_P (parser->scope)
15168 && dependent_type_p (parser->scope))))
15172 /* Look up the name. */
15173 decl = cp_parser_lookup_name (parser, identifier,
15175 /*is_template=*/true,
15176 /*is_namespace=*/false,
15177 check_dependency_p,
15178 /*ambiguous_decls=*/NULL,
15181 decl = strip_using_decl (decl);
15183 /* If DECL is a template, then the name was a template-name. */
15184 if (TREE_CODE (decl) == TEMPLATE_DECL)
15186 if (TREE_DEPRECATED (decl)
15187 && deprecated_state != DEPRECATED_SUPPRESS)
15188 warn_deprecated_use (decl, NULL_TREE);
15192 tree fn = NULL_TREE;
15194 /* The standard does not explicitly indicate whether a name that
15195 names a set of overloaded declarations, some of which are
15196 templates, is a template-name. However, such a name should
15197 be a template-name; otherwise, there is no way to form a
15198 template-id for the overloaded templates. */
15199 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15200 if (TREE_CODE (fns) == OVERLOAD)
15201 for (fn = fns; fn; fn = OVL_NEXT (fn))
15202 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15207 /* The name does not name a template. */
15208 cp_parser_error (parser, "expected template-name");
15209 return error_mark_node;
15213 /* If DECL is dependent, and refers to a function, then just return
15214 its name; we will look it up again during template instantiation. */
15215 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15217 tree scope = ovl_scope (decl);
15218 if (TYPE_P (scope) && dependent_type_p (scope))
15225 /* Parse a template-argument-list.
15227 template-argument-list:
15228 template-argument ... [opt]
15229 template-argument-list , template-argument ... [opt]
15231 Returns a TREE_VEC containing the arguments. */
15234 cp_parser_template_argument_list (cp_parser* parser)
15236 tree fixed_args[10];
15237 unsigned n_args = 0;
15238 unsigned alloced = 10;
15239 tree *arg_ary = fixed_args;
15241 bool saved_in_template_argument_list_p;
15243 bool saved_non_ice_p;
15245 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15246 parser->in_template_argument_list_p = true;
15247 /* Even if the template-id appears in an integral
15248 constant-expression, the contents of the argument list do
15250 saved_ice_p = parser->integral_constant_expression_p;
15251 parser->integral_constant_expression_p = false;
15252 saved_non_ice_p = parser->non_integral_constant_expression_p;
15253 parser->non_integral_constant_expression_p = false;
15255 /* Parse the arguments. */
15261 /* Consume the comma. */
15262 cp_lexer_consume_token (parser->lexer);
15264 /* Parse the template-argument. */
15265 argument = cp_parser_template_argument (parser);
15267 /* If the next token is an ellipsis, we're expanding a template
15269 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15271 if (argument == error_mark_node)
15273 cp_token *token = cp_lexer_peek_token (parser->lexer);
15274 error_at (token->location,
15275 "expected parameter pack before %<...%>");
15277 /* Consume the `...' token. */
15278 cp_lexer_consume_token (parser->lexer);
15280 /* Make the argument into a TYPE_PACK_EXPANSION or
15281 EXPR_PACK_EXPANSION. */
15282 argument = make_pack_expansion (argument);
15285 if (n_args == alloced)
15289 if (arg_ary == fixed_args)
15291 arg_ary = XNEWVEC (tree, alloced);
15292 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15295 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15297 arg_ary[n_args++] = argument;
15299 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15301 vec = make_tree_vec (n_args);
15304 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15306 if (arg_ary != fixed_args)
15308 parser->non_integral_constant_expression_p = saved_non_ice_p;
15309 parser->integral_constant_expression_p = saved_ice_p;
15310 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15312 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15316 /* Parse a template-argument.
15319 assignment-expression
15323 The representation is that of an assignment-expression, type-id, or
15324 id-expression -- except that the qualified id-expression is
15325 evaluated, so that the value returned is either a DECL or an
15328 Although the standard says "assignment-expression", it forbids
15329 throw-expressions or assignments in the template argument.
15330 Therefore, we use "conditional-expression" instead. */
15333 cp_parser_template_argument (cp_parser* parser)
15338 bool maybe_type_id = false;
15339 cp_token *token = NULL, *argument_start_token = NULL;
15340 location_t loc = 0;
15343 /* There's really no way to know what we're looking at, so we just
15344 try each alternative in order.
15348 In a template-argument, an ambiguity between a type-id and an
15349 expression is resolved to a type-id, regardless of the form of
15350 the corresponding template-parameter.
15352 Therefore, we try a type-id first. */
15353 cp_parser_parse_tentatively (parser);
15354 argument = cp_parser_template_type_arg (parser);
15355 /* If there was no error parsing the type-id but the next token is a
15356 '>>', our behavior depends on which dialect of C++ we're
15357 parsing. In C++98, we probably found a typo for '> >'. But there
15358 are type-id which are also valid expressions. For instance:
15360 struct X { int operator >> (int); };
15361 template <int V> struct Foo {};
15364 Here 'X()' is a valid type-id of a function type, but the user just
15365 wanted to write the expression "X() >> 5". Thus, we remember that we
15366 found a valid type-id, but we still try to parse the argument as an
15367 expression to see what happens.
15369 In C++0x, the '>>' will be considered two separate '>'
15371 if (!cp_parser_error_occurred (parser)
15372 && cxx_dialect == cxx98
15373 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15375 maybe_type_id = true;
15376 cp_parser_abort_tentative_parse (parser);
15380 /* If the next token isn't a `,' or a `>', then this argument wasn't
15381 really finished. This means that the argument is not a valid
15383 if (!cp_parser_next_token_ends_template_argument_p (parser))
15384 cp_parser_error (parser, "expected template-argument");
15385 /* If that worked, we're done. */
15386 if (cp_parser_parse_definitely (parser))
15389 /* We're still not sure what the argument will be. */
15390 cp_parser_parse_tentatively (parser);
15391 /* Try a template. */
15392 argument_start_token = cp_lexer_peek_token (parser->lexer);
15393 argument = cp_parser_id_expression (parser,
15394 /*template_keyword_p=*/false,
15395 /*check_dependency_p=*/true,
15397 /*declarator_p=*/false,
15398 /*optional_p=*/false);
15399 /* If the next token isn't a `,' or a `>', then this argument wasn't
15400 really finished. */
15401 if (!cp_parser_next_token_ends_template_argument_p (parser))
15402 cp_parser_error (parser, "expected template-argument");
15403 if (!cp_parser_error_occurred (parser))
15405 /* Figure out what is being referred to. If the id-expression
15406 was for a class template specialization, then we will have a
15407 TYPE_DECL at this point. There is no need to do name lookup
15408 at this point in that case. */
15409 if (TREE_CODE (argument) != TYPE_DECL)
15410 argument = cp_parser_lookup_name (parser, argument,
15412 /*is_template=*/template_p,
15413 /*is_namespace=*/false,
15414 /*check_dependency=*/true,
15415 /*ambiguous_decls=*/NULL,
15416 argument_start_token->location);
15417 /* Handle a constrained-type-specifier for a non-type template
15419 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15421 else if (TREE_CODE (argument) != TEMPLATE_DECL
15422 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15423 cp_parser_error (parser, "expected template-name");
15425 if (cp_parser_parse_definitely (parser))
15427 if (TREE_DEPRECATED (argument))
15428 warn_deprecated_use (argument, NULL_TREE);
15431 /* It must be a non-type argument. In C++17 any constant-expression is
15433 if (cxx_dialect > cxx14)
15436 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15438 -- an integral constant-expression of integral or enumeration
15441 -- the name of a non-type template-parameter; or
15443 -- the name of an object or function with external linkage...
15445 -- the address of an object or function with external linkage...
15447 -- a pointer to member... */
15448 /* Look for a non-type template parameter. */
15449 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15451 cp_parser_parse_tentatively (parser);
15452 argument = cp_parser_primary_expression (parser,
15453 /*address_p=*/false,
15455 /*template_arg_p=*/true,
15457 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15458 || !cp_parser_next_token_ends_template_argument_p (parser))
15459 cp_parser_simulate_error (parser);
15460 if (cp_parser_parse_definitely (parser))
15464 /* If the next token is "&", the argument must be the address of an
15465 object or function with external linkage. */
15466 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15469 loc = cp_lexer_peek_token (parser->lexer)->location;
15470 cp_lexer_consume_token (parser->lexer);
15472 /* See if we might have an id-expression. */
15473 token = cp_lexer_peek_token (parser->lexer);
15474 if (token->type == CPP_NAME
15475 || token->keyword == RID_OPERATOR
15476 || token->type == CPP_SCOPE
15477 || token->type == CPP_TEMPLATE_ID
15478 || token->type == CPP_NESTED_NAME_SPECIFIER)
15480 cp_parser_parse_tentatively (parser);
15481 argument = cp_parser_primary_expression (parser,
15484 /*template_arg_p=*/true,
15486 if (cp_parser_error_occurred (parser)
15487 || !cp_parser_next_token_ends_template_argument_p (parser))
15488 cp_parser_abort_tentative_parse (parser);
15493 if (INDIRECT_REF_P (argument))
15495 /* Strip the dereference temporarily. */
15496 gcc_assert (REFERENCE_REF_P (argument));
15497 argument = TREE_OPERAND (argument, 0);
15500 /* If we're in a template, we represent a qualified-id referring
15501 to a static data member as a SCOPE_REF even if the scope isn't
15502 dependent so that we can check access control later. */
15504 if (TREE_CODE (probe) == SCOPE_REF)
15505 probe = TREE_OPERAND (probe, 1);
15508 /* A variable without external linkage might still be a
15509 valid constant-expression, so no error is issued here
15510 if the external-linkage check fails. */
15511 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15512 cp_parser_simulate_error (parser);
15514 else if (is_overloaded_fn (argument))
15515 /* All overloaded functions are allowed; if the external
15516 linkage test does not pass, an error will be issued
15520 && (TREE_CODE (argument) == OFFSET_REF
15521 || TREE_CODE (argument) == SCOPE_REF))
15522 /* A pointer-to-member. */
15524 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15527 cp_parser_simulate_error (parser);
15529 if (cp_parser_parse_definitely (parser))
15532 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15533 tf_warning_or_error);
15535 argument = convert_from_reference (argument);
15540 /* If the argument started with "&", there are no other valid
15541 alternatives at this point. */
15544 cp_parser_error (parser, "invalid non-type template argument");
15545 return error_mark_node;
15549 /* If the argument wasn't successfully parsed as a type-id followed
15550 by '>>', the argument can only be a constant expression now.
15551 Otherwise, we try parsing the constant-expression tentatively,
15552 because the argument could really be a type-id. */
15554 cp_parser_parse_tentatively (parser);
15556 if (cxx_dialect <= cxx14)
15557 argument = cp_parser_constant_expression (parser);
15560 /* With C++17 generalized non-type template arguments we need to handle
15561 lvalue constant expressions, too. */
15562 argument = cp_parser_assignment_expression (parser);
15563 require_potential_constant_expression (argument);
15566 if (!maybe_type_id)
15568 if (!cp_parser_next_token_ends_template_argument_p (parser))
15569 cp_parser_error (parser, "expected template-argument");
15570 if (cp_parser_parse_definitely (parser))
15572 /* We did our best to parse the argument as a non type-id, but that
15573 was the only alternative that matched (albeit with a '>' after
15574 it). We can assume it's just a typo from the user, and a
15575 diagnostic will then be issued. */
15576 return cp_parser_template_type_arg (parser);
15579 /* Parse an explicit-instantiation.
15581 explicit-instantiation:
15582 template declaration
15584 Although the standard says `declaration', what it really means is:
15586 explicit-instantiation:
15587 template decl-specifier-seq [opt] declarator [opt] ;
15589 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15590 supposed to be allowed. A defect report has been filed about this
15595 explicit-instantiation:
15596 storage-class-specifier template
15597 decl-specifier-seq [opt] declarator [opt] ;
15598 function-specifier template
15599 decl-specifier-seq [opt] declarator [opt] ; */
15602 cp_parser_explicit_instantiation (cp_parser* parser)
15604 int declares_class_or_enum;
15605 cp_decl_specifier_seq decl_specifiers;
15606 tree extension_specifier = NULL_TREE;
15608 timevar_push (TV_TEMPLATE_INST);
15610 /* Look for an (optional) storage-class-specifier or
15611 function-specifier. */
15612 if (cp_parser_allow_gnu_extensions_p (parser))
15614 extension_specifier
15615 = cp_parser_storage_class_specifier_opt (parser);
15616 if (!extension_specifier)
15617 extension_specifier
15618 = cp_parser_function_specifier_opt (parser,
15619 /*decl_specs=*/NULL);
15622 /* Look for the `template' keyword. */
15623 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15624 /* Let the front end know that we are processing an explicit
15626 begin_explicit_instantiation ();
15627 /* [temp.explicit] says that we are supposed to ignore access
15628 control while processing explicit instantiation directives. */
15629 push_deferring_access_checks (dk_no_check);
15630 /* Parse a decl-specifier-seq. */
15631 cp_parser_decl_specifier_seq (parser,
15632 CP_PARSER_FLAGS_OPTIONAL,
15634 &declares_class_or_enum);
15635 /* If there was exactly one decl-specifier, and it declared a class,
15636 and there's no declarator, then we have an explicit type
15638 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15642 type = check_tag_decl (&decl_specifiers,
15643 /*explicit_type_instantiation_p=*/true);
15644 /* Turn access control back on for names used during
15645 template instantiation. */
15646 pop_deferring_access_checks ();
15648 do_type_instantiation (type, extension_specifier,
15649 /*complain=*/tf_error);
15653 cp_declarator *declarator;
15656 /* Parse the declarator. */
15658 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15659 /*ctor_dtor_or_conv_p=*/NULL,
15660 /*parenthesized_p=*/NULL,
15661 /*member_p=*/false,
15662 /*friend_p=*/false);
15663 if (declares_class_or_enum & 2)
15664 cp_parser_check_for_definition_in_return_type (declarator,
15665 decl_specifiers.type,
15666 decl_specifiers.locations[ds_type_spec]);
15667 if (declarator != cp_error_declarator)
15669 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15670 permerror (decl_specifiers.locations[ds_inline],
15671 "explicit instantiation shall not use"
15672 " %<inline%> specifier");
15673 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15674 permerror (decl_specifiers.locations[ds_constexpr],
15675 "explicit instantiation shall not use"
15676 " %<constexpr%> specifier");
15678 decl = grokdeclarator (declarator, &decl_specifiers,
15679 NORMAL, 0, &decl_specifiers.attributes);
15680 /* Turn access control back on for names used during
15681 template instantiation. */
15682 pop_deferring_access_checks ();
15683 /* Do the explicit instantiation. */
15684 do_decl_instantiation (decl, extension_specifier);
15688 pop_deferring_access_checks ();
15689 /* Skip the body of the explicit instantiation. */
15690 cp_parser_skip_to_end_of_statement (parser);
15693 /* We're done with the instantiation. */
15694 end_explicit_instantiation ();
15696 cp_parser_consume_semicolon_at_end_of_statement (parser);
15698 timevar_pop (TV_TEMPLATE_INST);
15701 /* Parse an explicit-specialization.
15703 explicit-specialization:
15704 template < > declaration
15706 Although the standard says `declaration', what it really means is:
15708 explicit-specialization:
15709 template <> decl-specifier [opt] init-declarator [opt] ;
15710 template <> function-definition
15711 template <> explicit-specialization
15712 template <> template-declaration */
15715 cp_parser_explicit_specialization (cp_parser* parser)
15717 bool need_lang_pop;
15718 cp_token *token = cp_lexer_peek_token (parser->lexer);
15720 /* Look for the `template' keyword. */
15721 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15722 /* Look for the `<'. */
15723 cp_parser_require (parser, CPP_LESS, RT_LESS);
15724 /* Look for the `>'. */
15725 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15726 /* We have processed another parameter list. */
15727 ++parser->num_template_parameter_lists;
15730 A template ... explicit specialization ... shall not have C
15732 if (current_lang_name == lang_name_c)
15734 error_at (token->location, "template specialization with C linkage");
15735 /* Give it C++ linkage to avoid confusing other parts of the
15737 push_lang_context (lang_name_cplusplus);
15738 need_lang_pop = true;
15741 need_lang_pop = false;
15742 /* Let the front end know that we are beginning a specialization. */
15743 if (!begin_specialization ())
15745 end_specialization ();
15749 /* If the next keyword is `template', we need to figure out whether
15750 or not we're looking a template-declaration. */
15751 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15753 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15754 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15755 cp_parser_template_declaration_after_export (parser,
15756 /*member_p=*/false);
15758 cp_parser_explicit_specialization (parser);
15761 /* Parse the dependent declaration. */
15762 cp_parser_single_declaration (parser,
15764 /*member_p=*/false,
15765 /*explicit_specialization_p=*/true,
15766 /*friend_p=*/NULL);
15767 /* We're done with the specialization. */
15768 end_specialization ();
15769 /* For the erroneous case of a template with C linkage, we pushed an
15770 implicit C++ linkage scope; exit that scope now. */
15772 pop_lang_context ();
15773 /* We're done with this parameter list. */
15774 --parser->num_template_parameter_lists;
15777 /* Parse a type-specifier.
15780 simple-type-specifier
15783 elaborated-type-specifier
15791 Returns a representation of the type-specifier. For a
15792 class-specifier, enum-specifier, or elaborated-type-specifier, a
15793 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15795 The parser flags FLAGS is used to control type-specifier parsing.
15797 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15798 in a decl-specifier-seq.
15800 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15801 class-specifier, enum-specifier, or elaborated-type-specifier, then
15802 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15803 if a type is declared; 2 if it is defined. Otherwise, it is set to
15806 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15807 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15808 is set to FALSE. */
15811 cp_parser_type_specifier (cp_parser* parser,
15812 cp_parser_flags flags,
15813 cp_decl_specifier_seq *decl_specs,
15814 bool is_declaration,
15815 int* declares_class_or_enum,
15816 bool* is_cv_qualifier)
15818 tree type_spec = NULL_TREE;
15821 cp_decl_spec ds = ds_last;
15823 /* Assume this type-specifier does not declare a new type. */
15824 if (declares_class_or_enum)
15825 *declares_class_or_enum = 0;
15826 /* And that it does not specify a cv-qualifier. */
15827 if (is_cv_qualifier)
15828 *is_cv_qualifier = false;
15829 /* Peek at the next token. */
15830 token = cp_lexer_peek_token (parser->lexer);
15832 /* If we're looking at a keyword, we can use that to guide the
15833 production we choose. */
15834 keyword = token->keyword;
15838 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15839 goto elaborated_type_specifier;
15841 /* Look for the enum-specifier. */
15842 type_spec = cp_parser_enum_specifier (parser);
15843 /* If that worked, we're done. */
15846 if (declares_class_or_enum)
15847 *declares_class_or_enum = 2;
15849 cp_parser_set_decl_spec_type (decl_specs,
15852 /*type_definition_p=*/true);
15856 goto elaborated_type_specifier;
15858 /* Any of these indicate either a class-specifier, or an
15859 elaborated-type-specifier. */
15863 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15864 goto elaborated_type_specifier;
15866 /* Parse tentatively so that we can back up if we don't find a
15867 class-specifier. */
15868 cp_parser_parse_tentatively (parser);
15869 /* Look for the class-specifier. */
15870 type_spec = cp_parser_class_specifier (parser);
15871 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15872 /* If that worked, we're done. */
15873 if (cp_parser_parse_definitely (parser))
15875 if (declares_class_or_enum)
15876 *declares_class_or_enum = 2;
15878 cp_parser_set_decl_spec_type (decl_specs,
15881 /*type_definition_p=*/true);
15885 /* Fall through. */
15886 elaborated_type_specifier:
15887 /* We're declaring (not defining) a class or enum. */
15888 if (declares_class_or_enum)
15889 *declares_class_or_enum = 1;
15891 /* Fall through. */
15893 /* Look for an elaborated-type-specifier. */
15895 = (cp_parser_elaborated_type_specifier
15897 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15900 cp_parser_set_decl_spec_type (decl_specs,
15903 /*type_definition_p=*/false);
15908 if (is_cv_qualifier)
15909 *is_cv_qualifier = true;
15914 if (is_cv_qualifier)
15915 *is_cv_qualifier = true;
15920 if (is_cv_qualifier)
15921 *is_cv_qualifier = true;
15925 /* The `__complex__' keyword is a GNU extension. */
15933 /* Handle simple keywords. */
15938 set_and_check_decl_spec_loc (decl_specs, ds, token);
15939 decl_specs->any_specifiers_p = true;
15941 return cp_lexer_consume_token (parser->lexer)->u.value;
15944 /* If we do not already have a type-specifier, assume we are looking
15945 at a simple-type-specifier. */
15946 type_spec = cp_parser_simple_type_specifier (parser,
15950 /* If we didn't find a type-specifier, and a type-specifier was not
15951 optional in this context, issue an error message. */
15952 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15954 cp_parser_error (parser, "expected type specifier");
15955 return error_mark_node;
15961 /* Parse a simple-type-specifier.
15963 simple-type-specifier:
15964 :: [opt] nested-name-specifier [opt] type-name
15965 :: [opt] nested-name-specifier template template-id
15980 simple-type-specifier:
15982 decltype ( expression )
15985 __underlying_type ( type-id )
15989 simple-type-specifier:
15991 __typeof__ unary-expression
15992 __typeof__ ( type-id )
15993 __typeof__ ( type-id ) { initializer-list , [opt] }
15995 Concepts Extension:
15997 simple-type-specifier:
15998 constrained-type-specifier
16000 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16001 appropriately updated. */
16004 cp_parser_simple_type_specifier (cp_parser* parser,
16005 cp_decl_specifier_seq *decl_specs,
16006 cp_parser_flags flags)
16008 tree type = NULL_TREE;
16012 /* Peek at the next token. */
16013 token = cp_lexer_peek_token (parser->lexer);
16015 /* If we're looking at a keyword, things are easy. */
16016 switch (token->keyword)
16020 decl_specs->explicit_char_p = true;
16021 type = char_type_node;
16024 type = char16_type_node;
16027 type = char32_type_node;
16030 type = wchar_type_node;
16033 type = boolean_type_node;
16036 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16037 type = short_integer_type_node;
16041 decl_specs->explicit_int_p = true;
16042 type = integer_type_node;
16048 idx = token->keyword - RID_INT_N_0;
16049 if (! int_n_enabled_p [idx])
16053 decl_specs->explicit_intN_p = true;
16054 decl_specs->int_n_idx = idx;
16056 type = int_n_trees [idx].signed_type;
16060 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16061 type = long_integer_type_node;
16064 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16065 type = integer_type_node;
16068 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16069 type = unsigned_type_node;
16072 type = float_type_node;
16075 type = double_type_node;
16078 type = void_type_node;
16082 maybe_warn_cpp0x (CPP0X_AUTO);
16083 if (parser->auto_is_implicit_function_template_parm_p)
16085 /* The 'auto' might be the placeholder return type for a function decl
16086 with trailing return type. */
16087 bool have_trailing_return_fn_decl = false;
16089 cp_parser_parse_tentatively (parser);
16090 cp_lexer_consume_token (parser->lexer);
16091 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16092 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16093 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16094 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16096 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16098 cp_lexer_consume_token (parser->lexer);
16099 cp_parser_skip_to_closing_parenthesis (parser,
16100 /*recovering*/false,
16102 /*consume_paren*/true);
16106 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16108 have_trailing_return_fn_decl = true;
16112 cp_lexer_consume_token (parser->lexer);
16114 cp_parser_abort_tentative_parse (parser);
16116 if (have_trailing_return_fn_decl)
16118 type = make_auto ();
16122 if (cxx_dialect >= cxx14)
16124 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16125 type = TREE_TYPE (type);
16128 type = error_mark_node;
16130 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16132 if (cxx_dialect < cxx14)
16133 error_at (token->location,
16134 "use of %<auto%> in lambda parameter declaration "
16135 "only available with "
16136 "-std=c++14 or -std=gnu++14");
16138 else if (cxx_dialect < cxx14)
16139 error_at (token->location,
16140 "use of %<auto%> in parameter declaration "
16141 "only available with "
16142 "-std=c++14 or -std=gnu++14");
16143 else if (!flag_concepts)
16144 pedwarn (token->location, OPT_Wpedantic,
16145 "ISO C++ forbids use of %<auto%> in parameter "
16149 type = make_auto ();
16153 /* Since DR 743, decltype can either be a simple-type-specifier by
16154 itself or begin a nested-name-specifier. Parsing it will replace
16155 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16156 handling below decide what to do. */
16157 cp_parser_decltype (parser);
16158 cp_lexer_set_token_position (parser->lexer, token);
16162 /* Consume the `typeof' token. */
16163 cp_lexer_consume_token (parser->lexer);
16164 /* Parse the operand to `typeof'. */
16165 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16166 /* If it is not already a TYPE, take its type. */
16167 if (!TYPE_P (type))
16168 type = finish_typeof (type);
16171 cp_parser_set_decl_spec_type (decl_specs, type,
16173 /*type_definition_p=*/false);
16177 case RID_UNDERLYING_TYPE:
16178 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16180 cp_parser_set_decl_spec_type (decl_specs, type,
16182 /*type_definition_p=*/false);
16187 case RID_DIRECT_BASES:
16188 type = cp_parser_trait_expr (parser, token->keyword);
16190 cp_parser_set_decl_spec_type (decl_specs, type,
16192 /*type_definition_p=*/false);
16198 /* If token is an already-parsed decltype not followed by ::,
16199 it's a simple-type-specifier. */
16200 if (token->type == CPP_DECLTYPE
16201 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16203 type = saved_checks_value (token->u.tree_check_value);
16206 cp_parser_set_decl_spec_type (decl_specs, type,
16208 /*type_definition_p=*/false);
16209 /* Remember that we are handling a decltype in order to
16210 implement the resolution of DR 1510 when the argument
16211 isn't instantiation dependent. */
16212 decl_specs->decltype_p = true;
16214 cp_lexer_consume_token (parser->lexer);
16218 /* If the type-specifier was for a built-in type, we're done. */
16221 /* Record the type. */
16223 && (token->keyword != RID_SIGNED
16224 && token->keyword != RID_UNSIGNED
16225 && token->keyword != RID_SHORT
16226 && token->keyword != RID_LONG))
16227 cp_parser_set_decl_spec_type (decl_specs,
16230 /*type_definition_p=*/false);
16232 decl_specs->any_specifiers_p = true;
16234 /* Consume the token. */
16235 cp_lexer_consume_token (parser->lexer);
16237 if (type == error_mark_node)
16238 return error_mark_node;
16240 /* There is no valid C++ program where a non-template type is
16241 followed by a "<". That usually indicates that the user thought
16242 that the type was a template. */
16243 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16246 return TYPE_NAME (type);
16249 /* The type-specifier must be a user-defined type. */
16250 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16255 /* Don't gobble tokens or issue error messages if this is an
16256 optional type-specifier. */
16257 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16258 cp_parser_parse_tentatively (parser);
16260 /* Look for the optional `::' operator. */
16262 = (cp_parser_global_scope_opt (parser,
16263 /*current_scope_valid_p=*/false)
16265 /* Look for the nested-name specifier. */
16267 = (cp_parser_nested_name_specifier_opt (parser,
16268 /*typename_keyword_p=*/false,
16269 /*check_dependency_p=*/true,
16271 /*is_declaration=*/false)
16273 token = cp_lexer_peek_token (parser->lexer);
16274 /* If we have seen a nested-name-specifier, and the next token
16275 is `template', then we are using the template-id production. */
16277 && cp_parser_optional_template_keyword (parser))
16279 /* Look for the template-id. */
16280 type = cp_parser_template_id (parser,
16281 /*template_keyword_p=*/true,
16282 /*check_dependency_p=*/true,
16284 /*is_declaration=*/false);
16285 /* If the template-id did not name a type, we are out of
16287 if (TREE_CODE (type) != TYPE_DECL)
16289 cp_parser_error (parser, "expected template-id for type");
16293 /* Otherwise, look for a type-name. */
16295 type = cp_parser_type_name (parser);
16296 /* Keep track of all name-lookups performed in class scopes. */
16300 && TREE_CODE (type) == TYPE_DECL
16301 && identifier_p (DECL_NAME (type)))
16302 maybe_note_name_used_in_class (DECL_NAME (type), type);
16303 /* If it didn't work out, we don't have a TYPE. */
16304 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16305 && !cp_parser_parse_definitely (parser))
16307 if (type && decl_specs)
16308 cp_parser_set_decl_spec_type (decl_specs, type,
16310 /*type_definition_p=*/false);
16313 /* If we didn't get a type-name, issue an error message. */
16314 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16316 cp_parser_error (parser, "expected type-name");
16317 return error_mark_node;
16320 if (type && type != error_mark_node)
16322 /* See if TYPE is an Objective-C type, and if so, parse and
16323 accept any protocol references following it. Do this before
16324 the cp_parser_check_for_invalid_template_id() call, because
16325 Objective-C types can be followed by '<...>' which would
16326 enclose protocol names rather than template arguments, and so
16327 everything is fine. */
16328 if (c_dialect_objc () && !parser->scope
16329 && (objc_is_id (type) || objc_is_class_name (type)))
16331 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16332 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16334 /* Clobber the "unqualified" type previously entered into
16335 DECL_SPECS with the new, improved protocol-qualified version. */
16337 decl_specs->type = qual_type;
16342 /* There is no valid C++ program where a non-template type is
16343 followed by a "<". That usually indicates that the user
16344 thought that the type was a template. */
16345 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16353 /* Parse a type-name.
16359 simple-template-id [in c++0x]
16376 Returns a TYPE_DECL for the type. */
16379 cp_parser_type_name (cp_parser* parser)
16381 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16386 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16390 /* We can't know yet whether it is a class-name or not. */
16391 cp_parser_parse_tentatively (parser);
16392 /* Try a class-name. */
16393 type_decl = cp_parser_class_name (parser,
16394 typename_keyword_p,
16395 /*template_keyword_p=*/false,
16397 /*check_dependency_p=*/true,
16398 /*class_head_p=*/false,
16399 /*is_declaration=*/false);
16400 /* If it's not a class-name, keep looking. */
16401 if (!cp_parser_parse_definitely (parser))
16403 if (cxx_dialect < cxx11)
16404 /* It must be a typedef-name or an enum-name. */
16405 return cp_parser_nonclass_name (parser);
16407 cp_parser_parse_tentatively (parser);
16408 /* It is either a simple-template-id representing an
16409 instantiation of an alias template... */
16410 type_decl = cp_parser_template_id (parser,
16411 /*template_keyword_p=*/false,
16412 /*check_dependency_p=*/true,
16414 /*is_declaration=*/false);
16415 /* Note that this must be an instantiation of an alias template
16416 because [temp.names]/6 says:
16418 A template-id that names an alias template specialization
16421 Whereas [temp.names]/7 says:
16423 A simple-template-id that names a class template
16424 specialization is a class-name.
16426 With concepts, this could also be a partial-concept-id that
16427 declares a non-type template parameter. */
16428 if (type_decl != NULL_TREE
16429 && TREE_CODE (type_decl) == TYPE_DECL
16430 && TYPE_DECL_ALIAS_P (type_decl))
16431 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16432 else if (is_constrained_parameter (type_decl))
16433 /* Don't do anything. */ ;
16435 cp_parser_simulate_error (parser);
16437 if (!cp_parser_parse_definitely (parser))
16438 /* ... Or a typedef-name or an enum-name. */
16439 return cp_parser_nonclass_name (parser);
16445 /* Check if DECL and ARGS can form a constrained-type-specifier.
16446 If ARGS is non-null, we try to form a concept check of the
16447 form DECL<?, ARGS> where ? is a wildcard that matches any
16448 kind of template argument. If ARGS is NULL, then we try to
16449 form a concept check of the form DECL<?>. */
16452 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16453 tree decl, tree args)
16455 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16457 /* If we a constrained-type-specifier cannot be deduced. */
16458 if (parser->prevent_constrained_type_specifiers)
16461 /* A constrained type specifier can only be found in an
16462 overload set or as a reference to a template declaration.
16464 FIXME: This might be masking a bug. It's possible that
16465 that the deduction below is causing template specializations
16466 to be formed with the wildcard as an argument. */
16467 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16470 /* Try to build a call expression that evaluates the
16471 concept. This can fail if the overload set refers
16472 only to non-templates. */
16473 tree placeholder = build_nt (WILDCARD_DECL);
16474 tree check = build_concept_check (decl, placeholder, args);
16475 if (check == error_mark_node)
16478 /* Deduce the checked constraint and the prototype parameter.
16480 FIXME: In certain cases, failure to deduce should be a
16481 diagnosable error. */
16484 if (!deduce_constrained_parameter (check, conc, proto))
16487 /* In template parameter scope, this results in a constrained
16488 parameter. Return a descriptor of that parm. */
16489 if (processing_template_parmlist)
16490 return build_constrained_parameter (conc, proto, args);
16492 /* In a parameter-declaration-clause, constrained-type
16493 specifiers result in invented template parameters. */
16494 if (parser->auto_is_implicit_function_template_parm_p)
16496 tree x = build_constrained_parameter (conc, proto, args);
16497 return synthesize_implicit_template_parm (parser, x);
16501 /* Otherwise, we're in a context where the constrained
16502 type name is deduced and the constraint applies
16503 after deduction. */
16504 return make_constrained_auto (conc, args);
16510 /* If DECL refers to a concept, return a TYPE_DECL representing
16511 the result of using the constrained type specifier in the
16512 current context. DECL refers to a concept if
16514 - it is an overload set containing a function concept taking a single
16517 - it is a variable concept taking a single type argument. */
16520 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16523 && (TREE_CODE (decl) == OVERLOAD
16524 || BASELINK_P (decl)
16525 || variable_concept_p (decl)))
16526 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16531 /* Check if DECL and ARGS form a partial-concept-id. If so,
16532 assign ID to the resulting constrained placeholder.
16534 Returns true if the partial-concept-id designates a placeholder
16535 and false otherwise. Note that *id is set to NULL_TREE in
16539 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16541 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16544 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16556 Returns a TYPE_DECL for the type. */
16559 cp_parser_nonclass_name (cp_parser* parser)
16564 cp_token *token = cp_lexer_peek_token (parser->lexer);
16565 identifier = cp_parser_identifier (parser);
16566 if (identifier == error_mark_node)
16567 return error_mark_node;
16569 /* Look up the type-name. */
16570 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16572 type_decl = strip_using_decl (type_decl);
16574 /* If we found an overload set, then it may refer to a concept-name. */
16575 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16578 if (TREE_CODE (type_decl) != TYPE_DECL
16579 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16581 /* See if this is an Objective-C type. */
16582 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16583 tree type = objc_get_protocol_qualified_type (identifier, protos);
16585 type_decl = TYPE_NAME (type);
16588 /* Issue an error if we did not find a type-name. */
16589 if (TREE_CODE (type_decl) != TYPE_DECL
16590 /* In Objective-C, we have the complication that class names are
16591 normally type names and start declarations (eg, the
16592 "NSObject" in "NSObject *object;"), but can be used in an
16593 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16594 is an expression. So, a classname followed by a dot is not a
16595 valid type-name. */
16596 || (objc_is_class_name (TREE_TYPE (type_decl))
16597 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16599 if (!cp_parser_simulate_error (parser))
16600 cp_parser_name_lookup_error (parser, identifier, type_decl,
16601 NLE_TYPE, token->location);
16602 return error_mark_node;
16604 /* Remember that the name was used in the definition of the
16605 current class so that we can check later to see if the
16606 meaning would have been different after the class was
16607 entirely defined. */
16608 else if (type_decl != error_mark_node
16610 maybe_note_name_used_in_class (identifier, type_decl);
16615 /* Parse an elaborated-type-specifier. Note that the grammar given
16616 here incorporates the resolution to DR68.
16618 elaborated-type-specifier:
16619 class-key :: [opt] nested-name-specifier [opt] identifier
16620 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16621 enum-key :: [opt] nested-name-specifier [opt] identifier
16622 typename :: [opt] nested-name-specifier identifier
16623 typename :: [opt] nested-name-specifier template [opt]
16628 elaborated-type-specifier:
16629 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16630 class-key attributes :: [opt] nested-name-specifier [opt]
16631 template [opt] template-id
16632 enum attributes :: [opt] nested-name-specifier [opt] identifier
16634 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16635 declared `friend'. If IS_DECLARATION is TRUE, then this
16636 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16637 something is being declared.
16639 Returns the TYPE specified. */
16642 cp_parser_elaborated_type_specifier (cp_parser* parser,
16644 bool is_declaration)
16646 enum tag_types tag_type;
16648 tree type = NULL_TREE;
16649 tree attributes = NULL_TREE;
16651 cp_token *token = NULL;
16653 /* See if we're looking at the `enum' keyword. */
16654 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16656 /* Consume the `enum' token. */
16657 cp_lexer_consume_token (parser->lexer);
16658 /* Remember that it's an enumeration type. */
16659 tag_type = enum_type;
16660 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16661 enums) is used here. */
16662 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16663 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16665 pedwarn (input_location, 0, "elaborated-type-specifier "
16666 "for a scoped enum must not use the %<%D%> keyword",
16667 cp_lexer_peek_token (parser->lexer)->u.value);
16668 /* Consume the `struct' or `class' and parse it anyway. */
16669 cp_lexer_consume_token (parser->lexer);
16671 /* Parse the attributes. */
16672 attributes = cp_parser_attributes_opt (parser);
16674 /* Or, it might be `typename'. */
16675 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16678 /* Consume the `typename' token. */
16679 cp_lexer_consume_token (parser->lexer);
16680 /* Remember that it's a `typename' type. */
16681 tag_type = typename_type;
16683 /* Otherwise it must be a class-key. */
16686 tag_type = cp_parser_class_key (parser);
16687 if (tag_type == none_type)
16688 return error_mark_node;
16689 /* Parse the attributes. */
16690 attributes = cp_parser_attributes_opt (parser);
16693 /* Look for the `::' operator. */
16694 globalscope = cp_parser_global_scope_opt (parser,
16695 /*current_scope_valid_p=*/false);
16696 /* Look for the nested-name-specifier. */
16697 if (tag_type == typename_type && !globalscope)
16699 if (!cp_parser_nested_name_specifier (parser,
16700 /*typename_keyword_p=*/true,
16701 /*check_dependency_p=*/true,
16704 return error_mark_node;
16707 /* Even though `typename' is not present, the proposed resolution
16708 to Core Issue 180 says that in `class A<T>::B', `B' should be
16709 considered a type-name, even if `A<T>' is dependent. */
16710 cp_parser_nested_name_specifier_opt (parser,
16711 /*typename_keyword_p=*/true,
16712 /*check_dependency_p=*/true,
16715 /* For everything but enumeration types, consider a template-id.
16716 For an enumeration type, consider only a plain identifier. */
16717 if (tag_type != enum_type)
16719 bool template_p = false;
16722 /* Allow the `template' keyword. */
16723 template_p = cp_parser_optional_template_keyword (parser);
16724 /* If we didn't see `template', we don't know if there's a
16725 template-id or not. */
16727 cp_parser_parse_tentatively (parser);
16728 /* Parse the template-id. */
16729 token = cp_lexer_peek_token (parser->lexer);
16730 decl = cp_parser_template_id (parser, template_p,
16731 /*check_dependency_p=*/true,
16734 /* If we didn't find a template-id, look for an ordinary
16736 if (!template_p && !cp_parser_parse_definitely (parser))
16738 /* We can get here when cp_parser_template_id, called by
16739 cp_parser_class_name with tag_type == none_type, succeeds
16740 and caches a BASELINK. Then, when called again here,
16741 instead of failing and returning an error_mark_node
16742 returns it (see template/typename17.C in C++11).
16743 ??? Could we diagnose this earlier? */
16744 else if (tag_type == typename_type && BASELINK_P (decl))
16746 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16747 type = error_mark_node;
16749 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16750 in effect, then we must assume that, upon instantiation, the
16751 template will correspond to a class. */
16752 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16753 && tag_type == typename_type)
16754 type = make_typename_type (parser->scope, decl,
16756 /*complain=*/tf_error);
16757 /* If the `typename' keyword is in effect and DECL is not a type
16758 decl, then type is non existent. */
16759 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16761 else if (TREE_CODE (decl) == TYPE_DECL)
16762 type = check_elaborated_type_specifier (tag_type, decl,
16763 /*allow_template_p=*/true);
16764 else if (decl == error_mark_node)
16765 type = error_mark_node;
16770 token = cp_lexer_peek_token (parser->lexer);
16771 identifier = cp_parser_identifier (parser);
16773 if (identifier == error_mark_node)
16775 parser->scope = NULL_TREE;
16776 return error_mark_node;
16779 /* For a `typename', we needn't call xref_tag. */
16780 if (tag_type == typename_type
16781 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16782 return cp_parser_make_typename_type (parser, identifier,
16785 /* Template parameter lists apply only if we are not within a
16786 function parameter list. */
16787 bool template_parm_lists_apply
16788 = parser->num_template_parameter_lists;
16789 if (template_parm_lists_apply)
16790 for (cp_binding_level *s = current_binding_level;
16791 s && s->kind != sk_template_parms;
16792 s = s->level_chain)
16793 if (s->kind == sk_function_parms)
16794 template_parm_lists_apply = false;
16796 /* Look up a qualified name in the usual way. */
16800 tree ambiguous_decls;
16802 decl = cp_parser_lookup_name (parser, identifier,
16804 /*is_template=*/false,
16805 /*is_namespace=*/false,
16806 /*check_dependency=*/true,
16810 /* If the lookup was ambiguous, an error will already have been
16812 if (ambiguous_decls)
16813 return error_mark_node;
16815 /* If we are parsing friend declaration, DECL may be a
16816 TEMPLATE_DECL tree node here. However, we need to check
16817 whether this TEMPLATE_DECL results in valid code. Consider
16818 the following example:
16821 template <class T> class C {};
16824 template <class T> friend class N::C; // #1, valid code
16826 template <class T> class Y {
16827 friend class N::C; // #2, invalid code
16830 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16831 name lookup of `N::C'. We see that friend declaration must
16832 be template for the code to be valid. Note that
16833 processing_template_decl does not work here since it is
16834 always 1 for the above two cases. */
16836 decl = (cp_parser_maybe_treat_template_as_class
16837 (decl, /*tag_name_p=*/is_friend
16838 && template_parm_lists_apply));
16840 if (TREE_CODE (decl) != TYPE_DECL)
16842 cp_parser_diagnose_invalid_type_name (parser,
16845 return error_mark_node;
16848 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16850 bool allow_template = (template_parm_lists_apply
16851 || DECL_SELF_REFERENCE_P (decl));
16852 type = check_elaborated_type_specifier (tag_type, decl,
16855 if (type == error_mark_node)
16856 return error_mark_node;
16859 /* Forward declarations of nested types, such as
16864 are invalid unless all components preceding the final '::'
16865 are complete. If all enclosing types are complete, these
16866 declarations become merely pointless.
16868 Invalid forward declarations of nested types are errors
16869 caught elsewhere in parsing. Those that are pointless arrive
16872 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16873 && !is_friend && !processing_explicit_instantiation)
16874 warning (0, "declaration %qD does not declare anything", decl);
16876 type = TREE_TYPE (decl);
16880 /* An elaborated-type-specifier sometimes introduces a new type and
16881 sometimes names an existing type. Normally, the rule is that it
16882 introduces a new type only if there is not an existing type of
16883 the same name already in scope. For example, given:
16886 void f() { struct S s; }
16888 the `struct S' in the body of `f' is the same `struct S' as in
16889 the global scope; the existing definition is used. However, if
16890 there were no global declaration, this would introduce a new
16891 local class named `S'.
16893 An exception to this rule applies to the following code:
16895 namespace N { struct S; }
16897 Here, the elaborated-type-specifier names a new type
16898 unconditionally; even if there is already an `S' in the
16899 containing scope this declaration names a new type.
16900 This exception only applies if the elaborated-type-specifier
16901 forms the complete declaration:
16905 A declaration consisting solely of `class-key identifier ;' is
16906 either a redeclaration of the name in the current scope or a
16907 forward declaration of the identifier as a class name. It
16908 introduces the name into the current scope.
16910 We are in this situation precisely when the next token is a `;'.
16912 An exception to the exception is that a `friend' declaration does
16913 *not* name a new type; i.e., given:
16915 struct S { friend struct T; };
16917 `T' is not a new type in the scope of `S'.
16919 Also, `new struct S' or `sizeof (struct S)' never results in the
16920 definition of a new type; a new type can only be declared in a
16921 declaration context. */
16927 /* Friends have special name lookup rules. */
16928 ts = ts_within_enclosing_non_class;
16929 else if (is_declaration
16930 && cp_lexer_next_token_is (parser->lexer,
16932 /* This is a `class-key identifier ;' */
16938 (template_parm_lists_apply
16939 && (cp_parser_next_token_starts_class_definition_p (parser)
16940 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16941 /* An unqualified name was used to reference this type, so
16942 there were no qualifying templates. */
16943 if (template_parm_lists_apply
16944 && !cp_parser_check_template_parameters (parser,
16945 /*num_templates=*/0,
16947 /*declarator=*/NULL))
16948 return error_mark_node;
16949 type = xref_tag (tag_type, identifier, ts, template_p);
16953 if (type == error_mark_node)
16954 return error_mark_node;
16956 /* Allow attributes on forward declarations of classes. */
16959 if (TREE_CODE (type) == TYPENAME_TYPE)
16960 warning (OPT_Wattributes,
16961 "attributes ignored on uninstantiated type");
16962 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16963 && ! processing_explicit_instantiation)
16964 warning (OPT_Wattributes,
16965 "attributes ignored on template instantiation");
16966 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16967 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16969 warning (OPT_Wattributes,
16970 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16973 if (tag_type != enum_type)
16975 /* Indicate whether this class was declared as a `class' or as a
16977 if (CLASS_TYPE_P (type))
16978 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16979 cp_parser_check_class_key (tag_type, type);
16982 /* A "<" cannot follow an elaborated type specifier. If that
16983 happens, the user was probably trying to form a template-id. */
16984 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16990 /* Parse an enum-specifier.
16993 enum-head { enumerator-list [opt] }
16994 enum-head { enumerator-list , } [C++0x]
16997 enum-key identifier [opt] enum-base [opt]
16998 enum-key nested-name-specifier identifier enum-base [opt]
17003 enum struct [C++0x]
17006 : type-specifier-seq
17008 opaque-enum-specifier:
17009 enum-key identifier enum-base [opt] ;
17012 enum-key attributes[opt] identifier [opt] enum-base [opt]
17013 { enumerator-list [opt] }attributes[opt]
17014 enum-key attributes[opt] identifier [opt] enum-base [opt]
17015 { enumerator-list, }attributes[opt] [C++0x]
17017 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17018 if the token stream isn't an enum-specifier after all. */
17021 cp_parser_enum_specifier (cp_parser* parser)
17024 tree type = NULL_TREE;
17026 tree nested_name_specifier = NULL_TREE;
17028 bool scoped_enum_p = false;
17029 bool has_underlying_type = false;
17030 bool nested_being_defined = false;
17031 bool new_value_list = false;
17032 bool is_new_type = false;
17033 bool is_anonymous = false;
17034 tree underlying_type = NULL_TREE;
17035 cp_token *type_start_token = NULL;
17036 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17038 parser->colon_corrects_to_scope_p = false;
17040 /* Parse tentatively so that we can back up if we don't find a
17042 cp_parser_parse_tentatively (parser);
17044 /* Caller guarantees that the current token is 'enum', an identifier
17045 possibly follows, and the token after that is an opening brace.
17046 If we don't have an identifier, fabricate an anonymous name for
17047 the enumeration being defined. */
17048 cp_lexer_consume_token (parser->lexer);
17050 /* Parse the "class" or "struct", which indicates a scoped
17051 enumeration type in C++0x. */
17052 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17053 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17055 if (cxx_dialect < cxx11)
17056 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17058 /* Consume the `struct' or `class' token. */
17059 cp_lexer_consume_token (parser->lexer);
17061 scoped_enum_p = true;
17064 attributes = cp_parser_attributes_opt (parser);
17066 /* Clear the qualification. */
17067 parser->scope = NULL_TREE;
17068 parser->qualifying_scope = NULL_TREE;
17069 parser->object_scope = NULL_TREE;
17071 /* Figure out in what scope the declaration is being placed. */
17072 prev_scope = current_scope ();
17074 type_start_token = cp_lexer_peek_token (parser->lexer);
17076 push_deferring_access_checks (dk_no_check);
17077 nested_name_specifier
17078 = cp_parser_nested_name_specifier_opt (parser,
17079 /*typename_keyword_p=*/true,
17080 /*check_dependency_p=*/false,
17082 /*is_declaration=*/false);
17084 if (nested_name_specifier)
17088 identifier = cp_parser_identifier (parser);
17089 name = cp_parser_lookup_name (parser, identifier,
17091 /*is_template=*/false,
17092 /*is_namespace=*/false,
17093 /*check_dependency=*/true,
17094 /*ambiguous_decls=*/NULL,
17096 if (name && name != error_mark_node)
17098 type = TREE_TYPE (name);
17099 if (TREE_CODE (type) == TYPENAME_TYPE)
17101 /* Are template enums allowed in ISO? */
17102 if (template_parm_scope_p ())
17103 pedwarn (type_start_token->location, OPT_Wpedantic,
17104 "%qD is an enumeration template", name);
17105 /* ignore a typename reference, for it will be solved by name
17110 else if (nested_name_specifier == error_mark_node)
17111 /* We already issued an error. */;
17114 error_at (type_start_token->location,
17115 "%qD does not name an enumeration in %qT",
17116 identifier, nested_name_specifier);
17117 nested_name_specifier = error_mark_node;
17122 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17123 identifier = cp_parser_identifier (parser);
17126 identifier = make_anon_name ();
17127 is_anonymous = true;
17129 error_at (type_start_token->location,
17130 "anonymous scoped enum is not allowed");
17133 pop_deferring_access_checks ();
17135 /* Check for the `:' that denotes a specified underlying type in C++0x.
17136 Note that a ':' could also indicate a bitfield width, however. */
17137 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17139 cp_decl_specifier_seq type_specifiers;
17141 /* Consume the `:'. */
17142 cp_lexer_consume_token (parser->lexer);
17144 /* Parse the type-specifier-seq. */
17145 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17146 /*is_trailing_return=*/false,
17149 /* At this point this is surely not elaborated type specifier. */
17150 if (!cp_parser_parse_definitely (parser))
17153 if (cxx_dialect < cxx11)
17154 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17156 has_underlying_type = true;
17158 /* If that didn't work, stop. */
17159 if (type_specifiers.type != error_mark_node)
17161 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17162 /*initialized=*/0, NULL);
17163 if (underlying_type == error_mark_node
17164 || check_for_bare_parameter_packs (underlying_type))
17165 underlying_type = NULL_TREE;
17169 /* Look for the `{' but don't consume it yet. */
17170 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17172 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17174 cp_parser_error (parser, "expected %<{%>");
17175 if (has_underlying_type)
17181 /* An opaque-enum-specifier must have a ';' here. */
17182 if ((scoped_enum_p || underlying_type)
17183 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17185 cp_parser_error (parser, "expected %<;%> or %<{%>");
17186 if (has_underlying_type)
17194 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17197 if (nested_name_specifier)
17199 if (CLASS_TYPE_P (nested_name_specifier))
17201 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17202 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17203 push_scope (nested_name_specifier);
17205 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17207 push_nested_namespace (nested_name_specifier);
17211 /* Issue an error message if type-definitions are forbidden here. */
17212 if (!cp_parser_check_type_definition (parser))
17213 type = error_mark_node;
17215 /* Create the new type. We do this before consuming the opening
17216 brace so the enum will be recorded as being on the line of its
17217 tag (or the 'enum' keyword, if there is no tag). */
17218 type = start_enum (identifier, type, underlying_type,
17219 attributes, scoped_enum_p, &is_new_type);
17221 /* If the next token is not '{' it is an opaque-enum-specifier or an
17222 elaborated-type-specifier. */
17223 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17225 timevar_push (TV_PARSE_ENUM);
17226 if (nested_name_specifier
17227 && nested_name_specifier != error_mark_node)
17229 /* The following catches invalid code such as:
17230 enum class S<int>::E { A, B, C }; */
17231 if (!processing_specialization
17232 && CLASS_TYPE_P (nested_name_specifier)
17233 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17234 error_at (type_start_token->location, "cannot add an enumerator "
17235 "list to a template instantiation");
17237 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17239 error_at (type_start_token->location,
17240 "%<%T::%E%> has not been declared",
17241 TYPE_CONTEXT (nested_name_specifier),
17242 nested_name_specifier);
17243 type = error_mark_node;
17245 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17246 && !CLASS_TYPE_P (nested_name_specifier))
17248 error_at (type_start_token->location, "nested name specifier "
17249 "%qT for enum declaration does not name a class "
17250 "or namespace", nested_name_specifier);
17251 type = error_mark_node;
17253 /* If that scope does not contain the scope in which the
17254 class was originally declared, the program is invalid. */
17255 else if (prev_scope && !is_ancestor (prev_scope,
17256 nested_name_specifier))
17258 if (at_namespace_scope_p ())
17259 error_at (type_start_token->location,
17260 "declaration of %qD in namespace %qD which does not "
17262 type, prev_scope, nested_name_specifier);
17264 error_at (type_start_token->location,
17265 "declaration of %qD in %qD which does not "
17267 type, prev_scope, nested_name_specifier);
17268 type = error_mark_node;
17273 begin_scope (sk_scoped_enum, type);
17275 /* Consume the opening brace. */
17276 cp_lexer_consume_token (parser->lexer);
17278 if (type == error_mark_node)
17279 ; /* Nothing to add */
17280 else if (OPAQUE_ENUM_P (type)
17281 || (cxx_dialect > cxx98 && processing_specialization))
17283 new_value_list = true;
17284 SET_OPAQUE_ENUM_P (type, false);
17285 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17289 error_at (type_start_token->location,
17290 "multiple definition of %q#T", type);
17291 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17292 "previous definition here");
17293 type = error_mark_node;
17296 if (type == error_mark_node)
17297 cp_parser_skip_to_end_of_block_or_statement (parser);
17298 /* If the next token is not '}', then there are some enumerators. */
17299 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17301 if (is_anonymous && !scoped_enum_p)
17302 pedwarn (type_start_token->location, OPT_Wpedantic,
17303 "ISO C++ forbids empty anonymous enum");
17306 cp_parser_enumerator_list (parser, type);
17308 /* Consume the final '}'. */
17309 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17313 timevar_pop (TV_PARSE_ENUM);
17317 /* If a ';' follows, then it is an opaque-enum-specifier
17318 and additional restrictions apply. */
17319 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17322 error_at (type_start_token->location,
17323 "opaque-enum-specifier without name");
17324 else if (nested_name_specifier)
17325 error_at (type_start_token->location,
17326 "opaque-enum-specifier must use a simple identifier");
17330 /* Look for trailing attributes to apply to this enumeration, and
17331 apply them if appropriate. */
17332 if (cp_parser_allow_gnu_extensions_p (parser))
17334 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17335 cplus_decl_attributes (&type,
17337 (int) ATTR_FLAG_TYPE_IN_PLACE);
17340 /* Finish up the enumeration. */
17341 if (type != error_mark_node)
17343 if (new_value_list)
17344 finish_enum_value_list (type);
17346 finish_enum (type);
17349 if (nested_name_specifier)
17351 if (CLASS_TYPE_P (nested_name_specifier))
17353 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17354 pop_scope (nested_name_specifier);
17356 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17358 pop_nested_namespace (nested_name_specifier);
17362 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17366 /* Parse an enumerator-list. The enumerators all have the indicated
17370 enumerator-definition
17371 enumerator-list , enumerator-definition */
17374 cp_parser_enumerator_list (cp_parser* parser, tree type)
17378 /* Parse an enumerator-definition. */
17379 cp_parser_enumerator_definition (parser, type);
17381 /* If the next token is not a ',', we've reached the end of
17383 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17385 /* Otherwise, consume the `,' and keep going. */
17386 cp_lexer_consume_token (parser->lexer);
17387 /* If the next token is a `}', there is a trailing comma. */
17388 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17390 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17391 pedwarn (input_location, OPT_Wpedantic,
17392 "comma at end of enumerator list");
17398 /* Parse an enumerator-definition. The enumerator has the indicated
17401 enumerator-definition:
17403 enumerator = constant-expression
17410 enumerator-definition:
17411 enumerator attributes [opt]
17412 enumerator attributes [opt] = constant-expression */
17415 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17421 /* Save the input location because we are interested in the location
17422 of the identifier and not the location of the explicit value. */
17423 loc = cp_lexer_peek_token (parser->lexer)->location;
17425 /* Look for the identifier. */
17426 identifier = cp_parser_identifier (parser);
17427 if (identifier == error_mark_node)
17430 /* Parse any specified attributes. */
17431 tree attrs = cp_parser_attributes_opt (parser);
17433 /* If the next token is an '=', then there is an explicit value. */
17434 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17436 /* Consume the `=' token. */
17437 cp_lexer_consume_token (parser->lexer);
17438 /* Parse the value. */
17439 value = cp_parser_constant_expression (parser);
17444 /* If we are processing a template, make sure the initializer of the
17445 enumerator doesn't contain any bare template parameter pack. */
17446 if (check_for_bare_parameter_packs (value))
17447 value = error_mark_node;
17449 /* Create the enumerator. */
17450 build_enumerator (identifier, value, type, attrs, loc);
17453 /* Parse a namespace-name.
17456 original-namespace-name
17459 Returns the NAMESPACE_DECL for the namespace. */
17462 cp_parser_namespace_name (cp_parser* parser)
17465 tree namespace_decl;
17467 cp_token *token = cp_lexer_peek_token (parser->lexer);
17469 /* Get the name of the namespace. */
17470 identifier = cp_parser_identifier (parser);
17471 if (identifier == error_mark_node)
17472 return error_mark_node;
17474 /* Look up the identifier in the currently active scope. Look only
17475 for namespaces, due to:
17477 [basic.lookup.udir]
17479 When looking up a namespace-name in a using-directive or alias
17480 definition, only namespace names are considered.
17484 [basic.lookup.qual]
17486 During the lookup of a name preceding the :: scope resolution
17487 operator, object, function, and enumerator names are ignored.
17489 (Note that cp_parser_qualifying_entity only calls this
17490 function if the token after the name is the scope resolution
17492 namespace_decl = cp_parser_lookup_name (parser, identifier,
17494 /*is_template=*/false,
17495 /*is_namespace=*/true,
17496 /*check_dependency=*/true,
17497 /*ambiguous_decls=*/NULL,
17499 /* If it's not a namespace, issue an error. */
17500 if (namespace_decl == error_mark_node
17501 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17503 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17504 error_at (token->location, "%qD is not a namespace-name", identifier);
17505 cp_parser_error (parser, "expected namespace-name");
17506 namespace_decl = error_mark_node;
17509 return namespace_decl;
17512 /* Parse a namespace-definition.
17514 namespace-definition:
17515 named-namespace-definition
17516 unnamed-namespace-definition
17518 named-namespace-definition:
17519 original-namespace-definition
17520 extension-namespace-definition
17522 original-namespace-definition:
17523 namespace identifier { namespace-body }
17525 extension-namespace-definition:
17526 namespace original-namespace-name { namespace-body }
17528 unnamed-namespace-definition:
17529 namespace { namespace-body } */
17532 cp_parser_namespace_definition (cp_parser* parser)
17534 tree identifier, attribs;
17535 bool has_visibility;
17538 int nested_definition_count = 0;
17540 cp_ensure_no_omp_declare_simd (parser);
17541 cp_ensure_no_oacc_routine (parser);
17542 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17544 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17546 cp_lexer_consume_token (parser->lexer);
17551 /* Look for the `namespace' keyword. */
17552 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17554 /* Parse any specified attributes before the identifier. */
17555 attribs = cp_parser_attributes_opt (parser);
17557 /* Get the name of the namespace. We do not attempt to distinguish
17558 between an original-namespace-definition and an
17559 extension-namespace-definition at this point. The semantic
17560 analysis routines are responsible for that. */
17561 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17562 identifier = cp_parser_identifier (parser);
17564 identifier = NULL_TREE;
17566 /* Parse any specified attributes after the identifier. */
17567 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17568 if (post_ident_attribs)
17571 attribs = chainon (attribs, post_ident_attribs);
17573 attribs = post_ident_attribs;
17576 /* Start the namespace. */
17577 push_namespace (identifier);
17579 /* Parse any nested namespace definition. */
17580 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17583 error_at (token->location, "a nested namespace definition cannot have attributes");
17584 if (cxx_dialect < cxx1z)
17585 pedwarn (input_location, OPT_Wpedantic,
17586 "nested namespace definitions only available with "
17587 "-std=c++1z or -std=gnu++1z");
17589 error_at (token->location, "a nested namespace definition cannot be inline");
17590 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17592 cp_lexer_consume_token (parser->lexer);
17593 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17594 identifier = cp_parser_identifier (parser);
17597 cp_parser_error (parser, "nested identifier required");
17600 ++nested_definition_count;
17601 push_namespace (identifier);
17605 /* Look for the `{' to validate starting the namespace. */
17606 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17608 /* "inline namespace" is equivalent to a stub namespace definition
17609 followed by a strong using directive. */
17612 tree name_space = current_namespace;
17613 /* Set up namespace association. */
17614 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17615 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17616 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17617 /* Import the contents of the inline namespace. */
17619 do_using_directive (name_space);
17620 push_namespace (identifier);
17623 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17625 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17627 /* Parse the body of the namespace. */
17628 cp_parser_namespace_body (parser);
17630 if (has_visibility)
17631 pop_visibility (1);
17633 /* Finish the nested namespace definitions. */
17634 while (nested_definition_count--)
17637 /* Finish the namespace. */
17639 /* Look for the final `}'. */
17640 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17643 /* Parse a namespace-body.
17646 declaration-seq [opt] */
17649 cp_parser_namespace_body (cp_parser* parser)
17651 cp_parser_declaration_seq_opt (parser);
17654 /* Parse a namespace-alias-definition.
17656 namespace-alias-definition:
17657 namespace identifier = qualified-namespace-specifier ; */
17660 cp_parser_namespace_alias_definition (cp_parser* parser)
17663 tree namespace_specifier;
17665 cp_token *token = cp_lexer_peek_token (parser->lexer);
17667 /* Look for the `namespace' keyword. */
17668 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17669 /* Look for the identifier. */
17670 identifier = cp_parser_identifier (parser);
17671 if (identifier == error_mark_node)
17673 /* Look for the `=' token. */
17674 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17675 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17677 error_at (token->location, "%<namespace%> definition is not allowed here");
17678 /* Skip the definition. */
17679 cp_lexer_consume_token (parser->lexer);
17680 if (cp_parser_skip_to_closing_brace (parser))
17681 cp_lexer_consume_token (parser->lexer);
17684 cp_parser_require (parser, CPP_EQ, RT_EQ);
17685 /* Look for the qualified-namespace-specifier. */
17686 namespace_specifier
17687 = cp_parser_qualified_namespace_specifier (parser);
17688 /* Look for the `;' token. */
17689 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17691 /* Register the alias in the symbol table. */
17692 do_namespace_alias (identifier, namespace_specifier);
17695 /* Parse a qualified-namespace-specifier.
17697 qualified-namespace-specifier:
17698 :: [opt] nested-name-specifier [opt] namespace-name
17700 Returns a NAMESPACE_DECL corresponding to the specified
17704 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17706 /* Look for the optional `::'. */
17707 cp_parser_global_scope_opt (parser,
17708 /*current_scope_valid_p=*/false);
17710 /* Look for the optional nested-name-specifier. */
17711 cp_parser_nested_name_specifier_opt (parser,
17712 /*typename_keyword_p=*/false,
17713 /*check_dependency_p=*/true,
17715 /*is_declaration=*/true);
17717 return cp_parser_namespace_name (parser);
17720 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17721 access declaration.
17724 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17725 using :: unqualified-id ;
17727 access-declaration:
17733 cp_parser_using_declaration (cp_parser* parser,
17734 bool access_declaration_p)
17737 bool typename_p = false;
17738 bool global_scope_p;
17742 int oldcount = errorcount;
17743 cp_token *diag_token = NULL;
17745 if (access_declaration_p)
17747 diag_token = cp_lexer_peek_token (parser->lexer);
17748 cp_parser_parse_tentatively (parser);
17752 /* Look for the `using' keyword. */
17753 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17755 /* Peek at the next token. */
17756 token = cp_lexer_peek_token (parser->lexer);
17757 /* See if it's `typename'. */
17758 if (token->keyword == RID_TYPENAME)
17760 /* Remember that we've seen it. */
17762 /* Consume the `typename' token. */
17763 cp_lexer_consume_token (parser->lexer);
17767 /* Look for the optional global scope qualification. */
17769 = (cp_parser_global_scope_opt (parser,
17770 /*current_scope_valid_p=*/false)
17773 /* If we saw `typename', or didn't see `::', then there must be a
17774 nested-name-specifier present. */
17775 if (typename_p || !global_scope_p)
17777 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17778 /*check_dependency_p=*/true,
17780 /*is_declaration=*/true);
17781 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17783 cp_parser_skip_to_end_of_block_or_statement (parser);
17787 /* Otherwise, we could be in either of the two productions. In that
17788 case, treat the nested-name-specifier as optional. */
17790 qscope = cp_parser_nested_name_specifier_opt (parser,
17791 /*typename_keyword_p=*/false,
17792 /*check_dependency_p=*/true,
17794 /*is_declaration=*/true);
17796 qscope = global_namespace;
17797 else if (UNSCOPED_ENUM_P (qscope))
17798 qscope = CP_TYPE_CONTEXT (qscope);
17800 if (access_declaration_p && cp_parser_error_occurred (parser))
17801 /* Something has already gone wrong; there's no need to parse
17802 further. Since an error has occurred, the return value of
17803 cp_parser_parse_definitely will be false, as required. */
17804 return cp_parser_parse_definitely (parser);
17806 token = cp_lexer_peek_token (parser->lexer);
17807 /* Parse the unqualified-id. */
17808 identifier = cp_parser_unqualified_id (parser,
17809 /*template_keyword_p=*/false,
17810 /*check_dependency_p=*/true,
17811 /*declarator_p=*/true,
17812 /*optional_p=*/false);
17814 if (access_declaration_p)
17816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17817 cp_parser_simulate_error (parser);
17818 if (!cp_parser_parse_definitely (parser))
17822 /* The function we call to handle a using-declaration is different
17823 depending on what scope we are in. */
17824 if (qscope == error_mark_node || identifier == error_mark_node)
17826 else if (!identifier_p (identifier)
17827 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17828 /* [namespace.udecl]
17830 A using declaration shall not name a template-id. */
17831 error_at (token->location,
17832 "a template-id may not appear in a using-declaration");
17835 if (at_class_scope_p ())
17837 /* Create the USING_DECL. */
17838 decl = do_class_using_decl (parser->scope, identifier);
17840 if (decl && typename_p)
17841 USING_DECL_TYPENAME_P (decl) = 1;
17843 if (check_for_bare_parameter_packs (decl))
17845 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17849 /* Add it to the list of members in this class. */
17850 finish_member_declaration (decl);
17854 decl = cp_parser_lookup_name_simple (parser,
17857 if (decl == error_mark_node)
17858 cp_parser_name_lookup_error (parser, identifier,
17861 else if (check_for_bare_parameter_packs (decl))
17863 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17866 else if (!at_namespace_scope_p ())
17867 do_local_using_decl (decl, qscope, identifier);
17869 do_toplevel_using_decl (decl, qscope, identifier);
17873 /* Look for the final `;'. */
17874 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17876 if (access_declaration_p && errorcount == oldcount)
17877 warning_at (diag_token->location, OPT_Wdeprecated,
17878 "access declarations are deprecated "
17879 "in favour of using-declarations; "
17880 "suggestion: add the %<using%> keyword");
17885 /* Parse an alias-declaration.
17888 using identifier attribute-specifier-seq [opt] = type-id */
17891 cp_parser_alias_declaration (cp_parser* parser)
17893 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17894 location_t id_location;
17895 cp_declarator *declarator;
17896 cp_decl_specifier_seq decl_specs;
17898 const char *saved_message = NULL;
17900 /* Look for the `using' keyword. */
17901 cp_token *using_token
17902 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17903 if (using_token == NULL)
17904 return error_mark_node;
17906 id_location = cp_lexer_peek_token (parser->lexer)->location;
17907 id = cp_parser_identifier (parser);
17908 if (id == error_mark_node)
17909 return error_mark_node;
17911 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17912 attributes = cp_parser_attributes_opt (parser);
17913 if (attributes == error_mark_node)
17914 return error_mark_node;
17916 cp_parser_require (parser, CPP_EQ, RT_EQ);
17918 if (cp_parser_error_occurred (parser))
17919 return error_mark_node;
17921 cp_parser_commit_to_tentative_parse (parser);
17923 /* Now we are going to parse the type-id of the declaration. */
17928 "A type-specifier-seq shall not define a class or enumeration
17929 unless it appears in the type-id of an alias-declaration (7.1.3) that
17930 is not the declaration of a template-declaration."
17932 In other words, if we currently are in an alias template, the
17933 type-id should not define a type.
17935 So let's set parser->type_definition_forbidden_message in that
17936 case; cp_parser_check_type_definition (called by
17937 cp_parser_class_specifier) will then emit an error if a type is
17938 defined in the type-id. */
17939 if (parser->num_template_parameter_lists)
17941 saved_message = parser->type_definition_forbidden_message;
17942 parser->type_definition_forbidden_message =
17943 G_("types may not be defined in alias template declarations");
17946 type = cp_parser_type_id (parser);
17948 /* Restore the error message if need be. */
17949 if (parser->num_template_parameter_lists)
17950 parser->type_definition_forbidden_message = saved_message;
17952 if (type == error_mark_node
17953 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17955 cp_parser_skip_to_end_of_block_or_statement (parser);
17956 return error_mark_node;
17959 /* A typedef-name can also be introduced by an alias-declaration. The
17960 identifier following the using keyword becomes a typedef-name. It has
17961 the same semantics as if it were introduced by the typedef
17962 specifier. In particular, it does not define a new type and it shall
17963 not appear in the type-id. */
17965 clear_decl_specs (&decl_specs);
17966 decl_specs.type = type;
17967 if (attributes != NULL_TREE)
17969 decl_specs.attributes = attributes;
17970 set_and_check_decl_spec_loc (&decl_specs,
17974 set_and_check_decl_spec_loc (&decl_specs,
17977 set_and_check_decl_spec_loc (&decl_specs,
17981 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17982 declarator->id_loc = id_location;
17984 member_p = at_class_scope_p ();
17986 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17987 NULL_TREE, attributes);
17989 decl = start_decl (declarator, &decl_specs, 0,
17990 attributes, NULL_TREE, &pushed_scope);
17991 if (decl == error_mark_node)
17994 // Attach constraints to the alias declaration.
17995 if (flag_concepts && current_template_parms)
17997 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17998 tree constr = build_constraints (reqs, NULL_TREE);
17999 set_constraints (decl, constr);
18002 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18005 pop_scope (pushed_scope);
18007 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18008 added into the symbol table; otherwise, return the TYPE_DECL. */
18009 if (DECL_LANG_SPECIFIC (decl)
18010 && DECL_TEMPLATE_INFO (decl)
18011 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18013 decl = DECL_TI_TEMPLATE (decl);
18015 check_member_template (decl);
18021 /* Parse a using-directive.
18024 using namespace :: [opt] nested-name-specifier [opt]
18025 namespace-name ; */
18028 cp_parser_using_directive (cp_parser* parser)
18030 tree namespace_decl;
18033 /* Look for the `using' keyword. */
18034 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18035 /* And the `namespace' keyword. */
18036 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18037 /* Look for the optional `::' operator. */
18038 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18039 /* And the optional nested-name-specifier. */
18040 cp_parser_nested_name_specifier_opt (parser,
18041 /*typename_keyword_p=*/false,
18042 /*check_dependency_p=*/true,
18044 /*is_declaration=*/true);
18045 /* Get the namespace being used. */
18046 namespace_decl = cp_parser_namespace_name (parser);
18047 /* And any specified attributes. */
18048 attribs = cp_parser_attributes_opt (parser);
18049 /* Update the symbol table. */
18050 parse_using_directive (namespace_decl, attribs);
18051 /* Look for the final `;'. */
18052 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18055 /* Parse an asm-definition.
18058 asm ( string-literal ) ;
18063 asm volatile [opt] ( string-literal ) ;
18064 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18065 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18066 : asm-operand-list [opt] ) ;
18067 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18068 : asm-operand-list [opt]
18069 : asm-clobber-list [opt] ) ;
18070 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18071 : asm-clobber-list [opt]
18072 : asm-goto-list ) ; */
18075 cp_parser_asm_definition (cp_parser* parser)
18078 tree outputs = NULL_TREE;
18079 tree inputs = NULL_TREE;
18080 tree clobbers = NULL_TREE;
18081 tree labels = NULL_TREE;
18083 bool volatile_p = false;
18084 bool extended_p = false;
18085 bool invalid_inputs_p = false;
18086 bool invalid_outputs_p = false;
18087 bool goto_p = false;
18088 required_token missing = RT_NONE;
18090 /* Look for the `asm' keyword. */
18091 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18093 if (parser->in_function_body
18094 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18096 error ("%<asm%> in %<constexpr%> function");
18097 cp_function_chain->invalid_constexpr = true;
18100 /* See if the next token is `volatile'. */
18101 if (cp_parser_allow_gnu_extensions_p (parser)
18102 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18104 /* Remember that we saw the `volatile' keyword. */
18106 /* Consume the token. */
18107 cp_lexer_consume_token (parser->lexer);
18109 if (cp_parser_allow_gnu_extensions_p (parser)
18110 && parser->in_function_body
18111 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18113 /* Remember that we saw the `goto' keyword. */
18115 /* Consume the token. */
18116 cp_lexer_consume_token (parser->lexer);
18118 /* Look for the opening `('. */
18119 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18121 /* Look for the string. */
18122 string = cp_parser_string_literal (parser, false, false);
18123 if (string == error_mark_node)
18125 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18126 /*consume_paren=*/true);
18130 /* If we're allowing GNU extensions, check for the extended assembly
18131 syntax. Unfortunately, the `:' tokens need not be separated by
18132 a space in C, and so, for compatibility, we tolerate that here
18133 too. Doing that means that we have to treat the `::' operator as
18135 if (cp_parser_allow_gnu_extensions_p (parser)
18136 && parser->in_function_body
18137 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18138 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18140 bool inputs_p = false;
18141 bool clobbers_p = false;
18142 bool labels_p = false;
18144 /* The extended syntax was used. */
18147 /* Look for outputs. */
18148 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18150 /* Consume the `:'. */
18151 cp_lexer_consume_token (parser->lexer);
18152 /* Parse the output-operands. */
18153 if (cp_lexer_next_token_is_not (parser->lexer,
18155 && cp_lexer_next_token_is_not (parser->lexer,
18157 && cp_lexer_next_token_is_not (parser->lexer,
18161 outputs = cp_parser_asm_operand_list (parser);
18162 if (outputs == error_mark_node)
18163 invalid_outputs_p = true;
18166 /* If the next token is `::', there are no outputs, and the
18167 next token is the beginning of the inputs. */
18168 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18169 /* The inputs are coming next. */
18172 /* Look for inputs. */
18174 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18176 /* Consume the `:' or `::'. */
18177 cp_lexer_consume_token (parser->lexer);
18178 /* Parse the output-operands. */
18179 if (cp_lexer_next_token_is_not (parser->lexer,
18181 && cp_lexer_next_token_is_not (parser->lexer,
18183 && cp_lexer_next_token_is_not (parser->lexer,
18186 inputs = cp_parser_asm_operand_list (parser);
18187 if (inputs == error_mark_node)
18188 invalid_inputs_p = true;
18191 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18192 /* The clobbers are coming next. */
18195 /* Look for clobbers. */
18197 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18200 /* Consume the `:' or `::'. */
18201 cp_lexer_consume_token (parser->lexer);
18202 /* Parse the clobbers. */
18203 if (cp_lexer_next_token_is_not (parser->lexer,
18205 && cp_lexer_next_token_is_not (parser->lexer,
18207 clobbers = cp_parser_asm_clobber_list (parser);
18210 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18211 /* The labels are coming next. */
18214 /* Look for labels. */
18216 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18219 /* Consume the `:' or `::'. */
18220 cp_lexer_consume_token (parser->lexer);
18221 /* Parse the labels. */
18222 labels = cp_parser_asm_label_list (parser);
18225 if (goto_p && !labels_p)
18226 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18229 missing = RT_COLON_SCOPE;
18231 /* Look for the closing `)'. */
18232 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18233 missing ? missing : RT_CLOSE_PAREN))
18234 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18235 /*consume_paren=*/true);
18236 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18238 if (!invalid_inputs_p && !invalid_outputs_p)
18240 /* Create the ASM_EXPR. */
18241 if (parser->in_function_body)
18243 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18244 inputs, clobbers, labels);
18245 /* If the extended syntax was not used, mark the ASM_EXPR. */
18248 tree temp = asm_stmt;
18249 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18250 temp = TREE_OPERAND (temp, 0);
18252 ASM_INPUT_P (temp) = 1;
18256 symtab->finalize_toplevel_asm (string);
18260 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18261 type that comes from the decl-specifier-seq. */
18264 strip_declarator_types (tree type, cp_declarator *declarator)
18266 for (cp_declarator *d = declarator; d;)
18275 if (TYPE_PTRMEMFUNC_P (type))
18276 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18277 type = TREE_TYPE (type);
18285 /* Declarators [gram.dcl.decl] */
18287 /* Parse an init-declarator.
18290 declarator initializer [opt]
18295 declarator asm-specification [opt] attributes [opt] initializer [opt]
18297 function-definition:
18298 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18300 decl-specifier-seq [opt] declarator function-try-block
18304 function-definition:
18305 __extension__ function-definition
18309 function-definition:
18310 decl-specifier-seq [opt] declarator function-transaction-block
18312 The DECL_SPECIFIERS apply to this declarator. Returns a
18313 representation of the entity declared. If MEMBER_P is TRUE, then
18314 this declarator appears in a class scope. The new DECL created by
18315 this declarator is returned.
18317 The CHECKS are access checks that should be performed once we know
18318 what entity is being declared (and, therefore, what classes have
18321 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18322 for a function-definition here as well. If the declarator is a
18323 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18324 be TRUE upon return. By that point, the function-definition will
18325 have been completely parsed.
18327 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18330 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18331 parsed declaration if it is an uninitialized single declarator not followed
18332 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18333 if present, will not be consumed. If returned, this declarator will be
18334 created with SD_INITIALIZED but will not call cp_finish_decl.
18336 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18337 and there is an initializer, the pointed location_t is set to the
18338 location of the '=' or `(', or '{' in C++11 token introducing the
18342 cp_parser_init_declarator (cp_parser* parser,
18343 cp_decl_specifier_seq *decl_specifiers,
18344 vec<deferred_access_check, va_gc> *checks,
18345 bool function_definition_allowed_p,
18347 int declares_class_or_enum,
18348 bool* function_definition_p,
18349 tree* maybe_range_for_decl,
18350 location_t* init_loc,
18353 cp_token *token = NULL, *asm_spec_start_token = NULL,
18354 *attributes_start_token = NULL;
18355 cp_declarator *declarator;
18356 tree prefix_attributes;
18357 tree attributes = NULL;
18358 tree asm_specification;
18360 tree decl = NULL_TREE;
18362 int is_initialized;
18363 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18364 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18366 enum cpp_ttype initialization_kind;
18367 bool is_direct_init = false;
18368 bool is_non_constant_init;
18369 int ctor_dtor_or_conv_p;
18370 bool friend_p = cp_parser_friend_p (decl_specifiers);
18371 tree pushed_scope = NULL_TREE;
18372 bool range_for_decl_p = false;
18373 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18374 location_t tmp_init_loc = UNKNOWN_LOCATION;
18376 /* Gather the attributes that were provided with the
18377 decl-specifiers. */
18378 prefix_attributes = decl_specifiers->attributes;
18380 /* Assume that this is not the declarator for a function
18382 if (function_definition_p)
18383 *function_definition_p = false;
18385 /* Default arguments are only permitted for function parameters. */
18386 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18387 parser->default_arg_ok_p = false;
18389 /* Defer access checks while parsing the declarator; we cannot know
18390 what names are accessible until we know what is being
18392 resume_deferring_access_checks ();
18394 /* Parse the declarator. */
18395 token = cp_lexer_peek_token (parser->lexer);
18397 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18398 &ctor_dtor_or_conv_p,
18399 /*parenthesized_p=*/NULL,
18400 member_p, friend_p);
18401 /* Gather up the deferred checks. */
18402 stop_deferring_access_checks ();
18404 parser->default_arg_ok_p = saved_default_arg_ok_p;
18406 /* If the DECLARATOR was erroneous, there's no need to go
18408 if (declarator == cp_error_declarator)
18409 return error_mark_node;
18411 /* Check that the number of template-parameter-lists is OK. */
18412 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18414 return error_mark_node;
18416 if (declares_class_or_enum & 2)
18417 cp_parser_check_for_definition_in_return_type (declarator,
18418 decl_specifiers->type,
18419 decl_specifiers->locations[ds_type_spec]);
18421 /* Figure out what scope the entity declared by the DECLARATOR is
18422 located in. `grokdeclarator' sometimes changes the scope, so
18423 we compute it now. */
18424 scope = get_scope_of_declarator (declarator);
18426 /* Perform any lookups in the declared type which were thought to be
18427 dependent, but are not in the scope of the declarator. */
18428 decl_specifiers->type
18429 = maybe_update_decl_type (decl_specifiers->type, scope);
18431 /* If we're allowing GNU extensions, look for an
18432 asm-specification. */
18433 if (cp_parser_allow_gnu_extensions_p (parser))
18435 /* Look for an asm-specification. */
18436 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18437 asm_specification = cp_parser_asm_specification_opt (parser);
18440 asm_specification = NULL_TREE;
18442 /* Look for attributes. */
18443 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18444 attributes = cp_parser_attributes_opt (parser);
18446 /* Peek at the next token. */
18447 token = cp_lexer_peek_token (parser->lexer);
18449 bool bogus_implicit_tmpl = false;
18451 if (function_declarator_p (declarator))
18453 /* Check to see if the token indicates the start of a
18454 function-definition. */
18455 if (cp_parser_token_starts_function_definition_p (token))
18457 if (!function_definition_allowed_p)
18459 /* If a function-definition should not appear here, issue an
18461 cp_parser_error (parser,
18462 "a function-definition is not allowed here");
18463 return error_mark_node;
18466 location_t func_brace_location
18467 = cp_lexer_peek_token (parser->lexer)->location;
18469 /* Neither attributes nor an asm-specification are allowed
18470 on a function-definition. */
18471 if (asm_specification)
18472 error_at (asm_spec_start_token->location,
18473 "an asm-specification is not allowed "
18474 "on a function-definition");
18476 error_at (attributes_start_token->location,
18477 "attributes are not allowed "
18478 "on a function-definition");
18479 /* This is a function-definition. */
18480 *function_definition_p = true;
18482 /* Parse the function definition. */
18484 decl = cp_parser_save_member_function_body (parser,
18487 prefix_attributes);
18490 (cp_parser_function_definition_from_specifiers_and_declarator
18491 (parser, decl_specifiers, prefix_attributes, declarator));
18493 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18495 /* This is where the prologue starts... */
18496 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18497 = func_brace_location;
18503 else if (parser->fully_implicit_function_template_p)
18505 /* A non-template declaration involving a function parameter list
18506 containing an implicit template parameter will be made into a
18507 template. If the resulting declaration is not going to be an
18508 actual function then finish the template scope here to prevent it.
18509 An error message will be issued once we have a decl to talk about.
18511 FIXME probably we should do type deduction rather than create an
18512 implicit template, but the standard currently doesn't allow it. */
18513 bogus_implicit_tmpl = true;
18514 finish_fully_implicit_template (parser, NULL_TREE);
18519 Only in function declarations for constructors, destructors, and
18520 type conversions can the decl-specifier-seq be omitted.
18522 We explicitly postpone this check past the point where we handle
18523 function-definitions because we tolerate function-definitions
18524 that are missing their return types in some modes. */
18525 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18527 cp_parser_error (parser,
18528 "expected constructor, destructor, or type conversion");
18529 return error_mark_node;
18532 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18533 if (token->type == CPP_EQ
18534 || token->type == CPP_OPEN_PAREN
18535 || token->type == CPP_OPEN_BRACE)
18537 is_initialized = SD_INITIALIZED;
18538 initialization_kind = token->type;
18539 if (maybe_range_for_decl)
18540 *maybe_range_for_decl = error_mark_node;
18541 tmp_init_loc = token->location;
18542 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18543 *init_loc = tmp_init_loc;
18545 if (token->type == CPP_EQ
18546 && function_declarator_p (declarator))
18548 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18549 if (t2->keyword == RID_DEFAULT)
18550 is_initialized = SD_DEFAULTED;
18551 else if (t2->keyword == RID_DELETE)
18552 is_initialized = SD_DELETED;
18557 /* If the init-declarator isn't initialized and isn't followed by a
18558 `,' or `;', it's not a valid init-declarator. */
18559 if (token->type != CPP_COMMA
18560 && token->type != CPP_SEMICOLON)
18562 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18563 range_for_decl_p = true;
18566 if (!maybe_range_for_decl)
18567 cp_parser_error (parser, "expected initializer");
18568 return error_mark_node;
18571 is_initialized = SD_UNINITIALIZED;
18572 initialization_kind = CPP_EOF;
18575 /* Because start_decl has side-effects, we should only call it if we
18576 know we're going ahead. By this point, we know that we cannot
18577 possibly be looking at any other construct. */
18578 cp_parser_commit_to_tentative_parse (parser);
18580 /* Enter the newly declared entry in the symbol table. If we're
18581 processing a declaration in a class-specifier, we wait until
18582 after processing the initializer. */
18585 if (parser->in_unbraced_linkage_specification_p)
18586 decl_specifiers->storage_class = sc_extern;
18587 decl = start_decl (declarator, decl_specifiers,
18588 range_for_decl_p? SD_INITIALIZED : is_initialized,
18589 attributes, prefix_attributes, &pushed_scope);
18590 cp_finalize_omp_declare_simd (parser, decl);
18591 cp_finalize_oacc_routine (parser, decl, false);
18592 /* Adjust location of decl if declarator->id_loc is more appropriate:
18593 set, and decl wasn't merged with another decl, in which case its
18594 location would be different from input_location, and more accurate. */
18596 && declarator->id_loc != UNKNOWN_LOCATION
18597 && DECL_SOURCE_LOCATION (decl) == input_location)
18598 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18601 /* Enter the SCOPE. That way unqualified names appearing in the
18602 initializer will be looked up in SCOPE. */
18603 pushed_scope = push_scope (scope);
18605 /* Perform deferred access control checks, now that we know in which
18606 SCOPE the declared entity resides. */
18607 if (!member_p && decl)
18609 tree saved_current_function_decl = NULL_TREE;
18611 /* If the entity being declared is a function, pretend that we
18612 are in its scope. If it is a `friend', it may have access to
18613 things that would not otherwise be accessible. */
18614 if (TREE_CODE (decl) == FUNCTION_DECL)
18616 saved_current_function_decl = current_function_decl;
18617 current_function_decl = decl;
18620 /* Perform access checks for template parameters. */
18621 cp_parser_perform_template_parameter_access_checks (checks);
18623 /* Perform the access control checks for the declarator and the
18624 decl-specifiers. */
18625 perform_deferred_access_checks (tf_warning_or_error);
18627 /* Restore the saved value. */
18628 if (TREE_CODE (decl) == FUNCTION_DECL)
18629 current_function_decl = saved_current_function_decl;
18632 /* Parse the initializer. */
18633 initializer = NULL_TREE;
18634 is_direct_init = false;
18635 is_non_constant_init = true;
18636 if (is_initialized)
18638 if (function_declarator_p (declarator))
18640 if (initialization_kind == CPP_EQ)
18641 initializer = cp_parser_pure_specifier (parser);
18644 /* If the declaration was erroneous, we don't really
18645 know what the user intended, so just silently
18646 consume the initializer. */
18647 if (decl != error_mark_node)
18648 error_at (tmp_init_loc, "initializer provided for function");
18649 cp_parser_skip_to_closing_parenthesis (parser,
18650 /*recovering=*/true,
18651 /*or_comma=*/false,
18652 /*consume_paren=*/true);
18657 /* We want to record the extra mangling scope for in-class
18658 initializers of class members and initializers of static data
18659 member templates. The former involves deferring
18660 parsing of the initializer until end of class as with default
18661 arguments. So right here we only handle the latter. */
18662 if (!member_p && processing_template_decl)
18663 start_lambda_scope (decl);
18664 initializer = cp_parser_initializer (parser,
18666 &is_non_constant_init);
18667 if (!member_p && processing_template_decl)
18668 finish_lambda_scope ();
18669 if (initializer == error_mark_node)
18670 cp_parser_skip_to_end_of_statement (parser);
18674 /* The old parser allows attributes to appear after a parenthesized
18675 initializer. Mark Mitchell proposed removing this functionality
18676 on the GCC mailing lists on 2002-08-13. This parser accepts the
18677 attributes -- but ignores them. */
18678 if (cp_parser_allow_gnu_extensions_p (parser)
18679 && initialization_kind == CPP_OPEN_PAREN)
18680 if (cp_parser_attributes_opt (parser))
18681 warning (OPT_Wattributes,
18682 "attributes after parenthesized initializer ignored");
18684 /* And now complain about a non-function implicit template. */
18685 if (bogus_implicit_tmpl && decl != error_mark_node)
18686 error_at (DECL_SOURCE_LOCATION (decl),
18687 "non-function %qD declared as implicit template", decl);
18689 /* For an in-class declaration, use `grokfield' to create the
18695 pop_scope (pushed_scope);
18696 pushed_scope = NULL_TREE;
18698 decl = grokfield (declarator, decl_specifiers,
18699 initializer, !is_non_constant_init,
18700 /*asmspec=*/NULL_TREE,
18701 chainon (attributes, prefix_attributes));
18702 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18703 cp_parser_save_default_args (parser, decl);
18704 cp_finalize_omp_declare_simd (parser, decl);
18705 cp_finalize_oacc_routine (parser, decl, false);
18708 /* Finish processing the declaration. But, skip member
18710 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18712 cp_finish_decl (decl,
18713 initializer, !is_non_constant_init,
18715 /* If the initializer is in parentheses, then this is
18716 a direct-initialization, which means that an
18717 `explicit' constructor is OK. Otherwise, an
18718 `explicit' constructor cannot be used. */
18719 ((is_direct_init || !is_initialized)
18720 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18722 else if ((cxx_dialect != cxx98) && friend_p
18723 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18724 /* Core issue #226 (C++0x only): A default template-argument
18725 shall not be specified in a friend class template
18727 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18728 /*is_partial=*/false, /*is_friend_decl=*/1);
18730 if (!friend_p && pushed_scope)
18731 pop_scope (pushed_scope);
18733 if (function_declarator_p (declarator)
18734 && parser->fully_implicit_function_template_p)
18737 decl = finish_fully_implicit_template (parser, decl);
18739 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18742 if (auto_result && is_initialized && decl_specifiers->type
18743 && type_uses_auto (decl_specifiers->type))
18744 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
18749 /* Parse a declarator.
18753 ptr-operator declarator
18755 abstract-declarator:
18756 ptr-operator abstract-declarator [opt]
18757 direct-abstract-declarator
18762 attributes [opt] direct-declarator
18763 attributes [opt] ptr-operator declarator
18765 abstract-declarator:
18766 attributes [opt] ptr-operator abstract-declarator [opt]
18767 attributes [opt] direct-abstract-declarator
18769 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18770 detect constructor, destructor or conversion operators. It is set
18771 to -1 if the declarator is a name, and +1 if it is a
18772 function. Otherwise it is set to zero. Usually you just want to
18773 test for >0, but internally the negative value is used.
18775 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18776 a decl-specifier-seq unless it declares a constructor, destructor,
18777 or conversion. It might seem that we could check this condition in
18778 semantic analysis, rather than parsing, but that makes it difficult
18779 to handle something like `f()'. We want to notice that there are
18780 no decl-specifiers, and therefore realize that this is an
18781 expression, not a declaration.)
18783 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18784 the declarator is a direct-declarator of the form "(...)".
18786 MEMBER_P is true iff this declarator is a member-declarator.
18788 FRIEND_P is true iff this declarator is a friend. */
18790 static cp_declarator *
18791 cp_parser_declarator (cp_parser* parser,
18792 cp_parser_declarator_kind dcl_kind,
18793 int* ctor_dtor_or_conv_p,
18794 bool* parenthesized_p,
18795 bool member_p, bool friend_p)
18797 cp_declarator *declarator;
18798 enum tree_code code;
18799 cp_cv_quals cv_quals;
18801 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18803 /* Assume this is not a constructor, destructor, or type-conversion
18805 if (ctor_dtor_or_conv_p)
18806 *ctor_dtor_or_conv_p = 0;
18808 if (cp_parser_allow_gnu_extensions_p (parser))
18809 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18811 /* Check for the ptr-operator production. */
18812 cp_parser_parse_tentatively (parser);
18813 /* Parse the ptr-operator. */
18814 code = cp_parser_ptr_operator (parser,
18819 /* If that worked, then we have a ptr-operator. */
18820 if (cp_parser_parse_definitely (parser))
18822 /* If a ptr-operator was found, then this declarator was not
18824 if (parenthesized_p)
18825 *parenthesized_p = true;
18826 /* The dependent declarator is optional if we are parsing an
18827 abstract-declarator. */
18828 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18829 cp_parser_parse_tentatively (parser);
18831 /* Parse the dependent declarator. */
18832 declarator = cp_parser_declarator (parser, dcl_kind,
18833 /*ctor_dtor_or_conv_p=*/NULL,
18834 /*parenthesized_p=*/NULL,
18835 /*member_p=*/false,
18838 /* If we are parsing an abstract-declarator, we must handle the
18839 case where the dependent declarator is absent. */
18840 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18841 && !cp_parser_parse_definitely (parser))
18844 declarator = cp_parser_make_indirect_declarator
18845 (code, class_type, cv_quals, declarator, std_attributes);
18847 /* Everything else is a direct-declarator. */
18850 if (parenthesized_p)
18851 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18853 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18854 ctor_dtor_or_conv_p,
18855 member_p, friend_p);
18858 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18859 declarator->attributes = gnu_attributes;
18863 /* Parse a direct-declarator or direct-abstract-declarator.
18867 direct-declarator ( parameter-declaration-clause )
18868 cv-qualifier-seq [opt]
18869 ref-qualifier [opt]
18870 exception-specification [opt]
18871 direct-declarator [ constant-expression [opt] ]
18874 direct-abstract-declarator:
18875 direct-abstract-declarator [opt]
18876 ( parameter-declaration-clause )
18877 cv-qualifier-seq [opt]
18878 ref-qualifier [opt]
18879 exception-specification [opt]
18880 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18881 ( abstract-declarator )
18883 Returns a representation of the declarator. DCL_KIND is
18884 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18885 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18886 we are parsing a direct-declarator. It is
18887 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18888 of ambiguity we prefer an abstract declarator, as per
18889 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18890 as for cp_parser_declarator. */
18892 static cp_declarator *
18893 cp_parser_direct_declarator (cp_parser* parser,
18894 cp_parser_declarator_kind dcl_kind,
18895 int* ctor_dtor_or_conv_p,
18896 bool member_p, bool friend_p)
18899 cp_declarator *declarator = NULL;
18900 tree scope = NULL_TREE;
18901 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18902 bool saved_in_declarator_p = parser->in_declarator_p;
18904 tree pushed_scope = NULL_TREE;
18908 /* Peek at the next token. */
18909 token = cp_lexer_peek_token (parser->lexer);
18910 if (token->type == CPP_OPEN_PAREN)
18912 /* This is either a parameter-declaration-clause, or a
18913 parenthesized declarator. When we know we are parsing a
18914 named declarator, it must be a parenthesized declarator
18915 if FIRST is true. For instance, `(int)' is a
18916 parameter-declaration-clause, with an omitted
18917 direct-abstract-declarator. But `((*))', is a
18918 parenthesized abstract declarator. Finally, when T is a
18919 template parameter `(T)' is a
18920 parameter-declaration-clause, and not a parenthesized
18923 We first try and parse a parameter-declaration-clause,
18924 and then try a nested declarator (if FIRST is true).
18926 It is not an error for it not to be a
18927 parameter-declaration-clause, even when FIRST is
18933 The first is the declaration of a function while the
18934 second is the definition of a variable, including its
18937 Having seen only the parenthesis, we cannot know which of
18938 these two alternatives should be selected. Even more
18939 complex are examples like:
18944 The former is a function-declaration; the latter is a
18945 variable initialization.
18947 Thus again, we try a parameter-declaration-clause, and if
18948 that fails, we back out and return. */
18950 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18953 bool is_declarator = false;
18955 /* In a member-declarator, the only valid interpretation
18956 of a parenthesis is the start of a
18957 parameter-declaration-clause. (It is invalid to
18958 initialize a static data member with a parenthesized
18959 initializer; only the "=" form of initialization is
18962 cp_parser_parse_tentatively (parser);
18964 /* Consume the `('. */
18965 cp_lexer_consume_token (parser->lexer);
18968 /* If this is going to be an abstract declarator, we're
18969 in a declarator and we can't have default args. */
18970 parser->default_arg_ok_p = false;
18971 parser->in_declarator_p = true;
18974 begin_scope (sk_function_parms, NULL_TREE);
18976 /* Parse the parameter-declaration-clause. */
18977 params = cp_parser_parameter_declaration_clause (parser);
18979 /* Consume the `)'. */
18980 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18982 /* If all went well, parse the cv-qualifier-seq,
18983 ref-qualifier and the exception-specification. */
18984 if (member_p || cp_parser_parse_definitely (parser))
18986 cp_cv_quals cv_quals;
18987 cp_virt_specifiers virt_specifiers;
18988 cp_ref_qualifier ref_qual;
18989 tree exception_specification;
18992 bool memfn = (member_p || (pushed_scope
18993 && CLASS_TYPE_P (pushed_scope)));
18995 is_declarator = true;
18997 if (ctor_dtor_or_conv_p)
18998 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19001 /* Parse the cv-qualifier-seq. */
19002 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19003 /* Parse the ref-qualifier. */
19004 ref_qual = cp_parser_ref_qualifier_opt (parser);
19005 /* Parse the tx-qualifier. */
19006 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19007 /* And the exception-specification. */
19008 exception_specification
19009 = cp_parser_exception_specification_opt (parser);
19011 attrs = cp_parser_std_attribute_spec_seq (parser);
19013 /* In here, we handle cases where attribute is used after
19014 the function declaration. For example:
19015 void func (int x) __attribute__((vector(..))); */
19016 tree gnu_attrs = NULL_TREE;
19018 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19020 cp_parser_parse_tentatively (parser);
19021 tree attr = cp_parser_gnu_attributes_opt (parser);
19022 if (cp_lexer_next_token_is_not (parser->lexer,
19024 && cp_lexer_next_token_is_not (parser->lexer,
19026 cp_parser_abort_tentative_parse (parser);
19027 else if (!cp_parser_parse_definitely (parser))
19032 tree requires_clause = NULL_TREE;
19033 late_return = (cp_parser_late_return_type_opt
19034 (parser, declarator, requires_clause,
19035 memfn ? cv_quals : -1));
19037 /* Parse the virt-specifier-seq. */
19038 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19040 /* Create the function-declarator. */
19041 declarator = make_call_declarator (declarator,
19047 exception_specification,
19050 declarator->std_attributes = attrs;
19051 declarator->attributes = gnu_attrs;
19052 /* Any subsequent parameter lists are to do with
19053 return type, so are not those of the declared
19055 parser->default_arg_ok_p = false;
19058 /* Remove the function parms from scope. */
19059 pop_bindings_and_leave_scope ();
19062 /* Repeat the main loop. */
19066 /* If this is the first, we can try a parenthesized
19070 bool saved_in_type_id_in_expr_p;
19072 parser->default_arg_ok_p = saved_default_arg_ok_p;
19073 parser->in_declarator_p = saved_in_declarator_p;
19075 /* Consume the `('. */
19076 cp_lexer_consume_token (parser->lexer);
19077 /* Parse the nested declarator. */
19078 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19079 parser->in_type_id_in_expr_p = true;
19081 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19082 /*parenthesized_p=*/NULL,
19083 member_p, friend_p);
19084 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19086 /* Expect a `)'. */
19087 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19088 declarator = cp_error_declarator;
19089 if (declarator == cp_error_declarator)
19092 goto handle_declarator;
19094 /* Otherwise, we must be done. */
19098 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19099 && token->type == CPP_OPEN_SQUARE
19100 && !cp_next_tokens_can_be_attribute_p (parser))
19102 /* Parse an array-declarator. */
19103 tree bounds, attrs;
19105 if (ctor_dtor_or_conv_p)
19106 *ctor_dtor_or_conv_p = 0;
19109 parser->default_arg_ok_p = false;
19110 parser->in_declarator_p = true;
19111 /* Consume the `['. */
19112 cp_lexer_consume_token (parser->lexer);
19113 /* Peek at the next token. */
19114 token = cp_lexer_peek_token (parser->lexer);
19115 /* If the next token is `]', then there is no
19116 constant-expression. */
19117 if (token->type != CPP_CLOSE_SQUARE)
19119 bool non_constant_p;
19121 = cp_parser_constant_expression (parser,
19122 /*allow_non_constant=*/true,
19124 if (!non_constant_p)
19126 else if (error_operand_p (bounds))
19127 /* Already gave an error. */;
19128 else if (!parser->in_function_body
19129 || current_binding_level->kind == sk_function_parms)
19131 /* Normally, the array bound must be an integral constant
19132 expression. However, as an extension, we allow VLAs
19133 in function scopes as long as they aren't part of a
19134 parameter declaration. */
19135 cp_parser_error (parser,
19136 "array bound is not an integer constant");
19137 bounds = error_mark_node;
19139 else if (processing_template_decl
19140 && !type_dependent_expression_p (bounds))
19142 /* Remember this wasn't a constant-expression. */
19143 bounds = build_nop (TREE_TYPE (bounds), bounds);
19144 TREE_SIDE_EFFECTS (bounds) = 1;
19148 bounds = NULL_TREE;
19149 /* Look for the closing `]'. */
19150 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19152 declarator = cp_error_declarator;
19156 attrs = cp_parser_std_attribute_spec_seq (parser);
19157 declarator = make_array_declarator (declarator, bounds);
19158 declarator->std_attributes = attrs;
19160 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19163 tree qualifying_scope;
19164 tree unqualified_name;
19166 special_function_kind sfk;
19168 bool pack_expansion_p = false;
19169 cp_token *declarator_id_start_token;
19171 /* Parse a declarator-id */
19172 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19175 cp_parser_parse_tentatively (parser);
19177 /* If we see an ellipsis, we should be looking at a
19179 if (token->type == CPP_ELLIPSIS)
19181 /* Consume the `...' */
19182 cp_lexer_consume_token (parser->lexer);
19184 pack_expansion_p = true;
19188 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19190 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19191 qualifying_scope = parser->scope;
19196 if (!unqualified_name && pack_expansion_p)
19198 /* Check whether an error occurred. */
19199 okay = !cp_parser_error_occurred (parser);
19201 /* We already consumed the ellipsis to mark a
19202 parameter pack, but we have no way to report it,
19203 so abort the tentative parse. We will be exiting
19204 immediately anyway. */
19205 cp_parser_abort_tentative_parse (parser);
19208 okay = cp_parser_parse_definitely (parser);
19211 unqualified_name = error_mark_node;
19212 else if (unqualified_name
19213 && (qualifying_scope
19214 || (!identifier_p (unqualified_name))))
19216 cp_parser_error (parser, "expected unqualified-id");
19217 unqualified_name = error_mark_node;
19221 if (!unqualified_name)
19223 if (unqualified_name == error_mark_node)
19225 declarator = cp_error_declarator;
19226 pack_expansion_p = false;
19227 declarator->parameter_pack_p = false;
19231 attrs = cp_parser_std_attribute_spec_seq (parser);
19233 if (qualifying_scope && at_namespace_scope_p ()
19234 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19236 /* In the declaration of a member of a template class
19237 outside of the class itself, the SCOPE will sometimes
19238 be a TYPENAME_TYPE. For example, given:
19240 template <typename T>
19241 int S<T>::R::i = 3;
19243 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19244 this context, we must resolve S<T>::R to an ordinary
19245 type, rather than a typename type.
19247 The reason we normally avoid resolving TYPENAME_TYPEs
19248 is that a specialization of `S' might render
19249 `S<T>::R' not a type. However, if `S' is
19250 specialized, then this `i' will not be used, so there
19251 is no harm in resolving the types here. */
19254 /* Resolve the TYPENAME_TYPE. */
19255 type = resolve_typename_type (qualifying_scope,
19256 /*only_current_p=*/false);
19257 /* If that failed, the declarator is invalid. */
19258 if (TREE_CODE (type) == TYPENAME_TYPE)
19260 if (typedef_variant_p (type))
19261 error_at (declarator_id_start_token->location,
19262 "cannot define member of dependent typedef "
19265 error_at (declarator_id_start_token->location,
19266 "%<%T::%E%> is not a type",
19267 TYPE_CONTEXT (qualifying_scope),
19268 TYPE_IDENTIFIER (qualifying_scope));
19270 qualifying_scope = type;
19275 if (unqualified_name)
19279 if (qualifying_scope
19280 && CLASS_TYPE_P (qualifying_scope))
19281 class_type = qualifying_scope;
19283 class_type = current_class_type;
19285 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19287 tree name_type = TREE_TYPE (unqualified_name);
19288 if (class_type && same_type_p (name_type, class_type))
19290 if (qualifying_scope
19291 && CLASSTYPE_USE_TEMPLATE (name_type))
19293 error_at (declarator_id_start_token->location,
19294 "invalid use of constructor as a template");
19295 inform (declarator_id_start_token->location,
19296 "use %<%T::%D%> instead of %<%T::%D%> to "
19297 "name the constructor in a qualified name",
19299 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19300 class_type, name_type);
19301 declarator = cp_error_declarator;
19305 unqualified_name = constructor_name (class_type);
19309 /* We do not attempt to print the declarator
19310 here because we do not have enough
19311 information about its original syntactic
19313 cp_parser_error (parser, "invalid declarator");
19314 declarator = cp_error_declarator;
19321 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19322 sfk = sfk_destructor;
19323 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19324 sfk = sfk_conversion;
19325 else if (/* There's no way to declare a constructor
19326 for an anonymous type, even if the type
19327 got a name for linkage purposes. */
19328 !TYPE_WAS_ANONYMOUS (class_type)
19329 /* Handle correctly (c++/19200):
19343 friend void N::S();
19346 && class_type != qualifying_scope)
19347 && constructor_name_p (unqualified_name,
19350 unqualified_name = constructor_name (class_type);
19351 sfk = sfk_constructor;
19353 else if (is_overloaded_fn (unqualified_name)
19354 && DECL_CONSTRUCTOR_P (get_first_fn
19355 (unqualified_name)))
19356 sfk = sfk_constructor;
19358 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19359 *ctor_dtor_or_conv_p = -1;
19362 declarator = make_id_declarator (qualifying_scope,
19365 declarator->std_attributes = attrs;
19366 declarator->id_loc = token->location;
19367 declarator->parameter_pack_p = pack_expansion_p;
19369 if (pack_expansion_p)
19370 maybe_warn_variadic_templates ();
19373 handle_declarator:;
19374 scope = get_scope_of_declarator (declarator);
19377 /* Any names that appear after the declarator-id for a
19378 member are looked up in the containing scope. */
19379 if (at_function_scope_p ())
19381 /* But declarations with qualified-ids can't appear in a
19383 cp_parser_error (parser, "qualified-id in declaration");
19384 declarator = cp_error_declarator;
19387 pushed_scope = push_scope (scope);
19389 parser->in_declarator_p = true;
19390 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19391 || (declarator && declarator->kind == cdk_id))
19392 /* Default args are only allowed on function
19394 parser->default_arg_ok_p = saved_default_arg_ok_p;
19396 parser->default_arg_ok_p = false;
19405 /* For an abstract declarator, we might wind up with nothing at this
19406 point. That's an error; the declarator is not optional. */
19408 cp_parser_error (parser, "expected declarator");
19410 /* If we entered a scope, we must exit it now. */
19412 pop_scope (pushed_scope);
19414 parser->default_arg_ok_p = saved_default_arg_ok_p;
19415 parser->in_declarator_p = saved_in_declarator_p;
19420 /* Parse a ptr-operator.
19423 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19424 * cv-qualifier-seq [opt]
19426 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19427 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19432 & cv-qualifier-seq [opt]
19434 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19435 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19436 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19437 filled in with the TYPE containing the member. *CV_QUALS is
19438 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19439 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19440 Note that the tree codes returned by this function have nothing
19441 to do with the types of trees that will be eventually be created
19442 to represent the pointer or reference type being parsed. They are
19443 just constants with suggestive names. */
19444 static enum tree_code
19445 cp_parser_ptr_operator (cp_parser* parser,
19447 cp_cv_quals *cv_quals,
19450 enum tree_code code = ERROR_MARK;
19452 tree attrs = NULL_TREE;
19454 /* Assume that it's not a pointer-to-member. */
19456 /* And that there are no cv-qualifiers. */
19457 *cv_quals = TYPE_UNQUALIFIED;
19459 /* Peek at the next token. */
19460 token = cp_lexer_peek_token (parser->lexer);
19462 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19463 if (token->type == CPP_MULT)
19464 code = INDIRECT_REF;
19465 else if (token->type == CPP_AND)
19467 else if ((cxx_dialect != cxx98) &&
19468 token->type == CPP_AND_AND) /* C++0x only */
19469 code = NON_LVALUE_EXPR;
19471 if (code != ERROR_MARK)
19473 /* Consume the `*', `&' or `&&'. */
19474 cp_lexer_consume_token (parser->lexer);
19476 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19477 `&', if we are allowing GNU extensions. (The only qualifier
19478 that can legally appear after `&' is `restrict', but that is
19479 enforced during semantic analysis. */
19480 if (code == INDIRECT_REF
19481 || cp_parser_allow_gnu_extensions_p (parser))
19482 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19484 attrs = cp_parser_std_attribute_spec_seq (parser);
19485 if (attributes != NULL)
19486 *attributes = attrs;
19490 /* Try the pointer-to-member case. */
19491 cp_parser_parse_tentatively (parser);
19492 /* Look for the optional `::' operator. */
19493 cp_parser_global_scope_opt (parser,
19494 /*current_scope_valid_p=*/false);
19495 /* Look for the nested-name specifier. */
19496 token = cp_lexer_peek_token (parser->lexer);
19497 cp_parser_nested_name_specifier (parser,
19498 /*typename_keyword_p=*/false,
19499 /*check_dependency_p=*/true,
19501 /*is_declaration=*/false);
19502 /* If we found it, and the next token is a `*', then we are
19503 indeed looking at a pointer-to-member operator. */
19504 if (!cp_parser_error_occurred (parser)
19505 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19507 /* Indicate that the `*' operator was used. */
19508 code = INDIRECT_REF;
19510 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19511 error_at (token->location, "%qD is a namespace", parser->scope);
19512 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19513 error_at (token->location, "cannot form pointer to member of "
19514 "non-class %q#T", parser->scope);
19517 /* The type of which the member is a member is given by the
19519 *type = parser->scope;
19520 /* The next name will not be qualified. */
19521 parser->scope = NULL_TREE;
19522 parser->qualifying_scope = NULL_TREE;
19523 parser->object_scope = NULL_TREE;
19524 /* Look for optional c++11 attributes. */
19525 attrs = cp_parser_std_attribute_spec_seq (parser);
19526 if (attributes != NULL)
19527 *attributes = attrs;
19528 /* Look for the optional cv-qualifier-seq. */
19529 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19532 /* If that didn't work we don't have a ptr-operator. */
19533 if (!cp_parser_parse_definitely (parser))
19534 cp_parser_error (parser, "expected ptr-operator");
19540 /* Parse an (optional) cv-qualifier-seq.
19543 cv-qualifier cv-qualifier-seq [opt]
19554 Returns a bitmask representing the cv-qualifiers. */
19557 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19559 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19564 cp_cv_quals cv_qualifier;
19566 /* Peek at the next token. */
19567 token = cp_lexer_peek_token (parser->lexer);
19568 /* See if it's a cv-qualifier. */
19569 switch (token->keyword)
19572 cv_qualifier = TYPE_QUAL_CONST;
19576 cv_qualifier = TYPE_QUAL_VOLATILE;
19580 cv_qualifier = TYPE_QUAL_RESTRICT;
19584 cv_qualifier = TYPE_UNQUALIFIED;
19591 if (cv_quals & cv_qualifier)
19593 error_at (token->location, "duplicate cv-qualifier");
19594 cp_lexer_purge_token (parser->lexer);
19598 cp_lexer_consume_token (parser->lexer);
19599 cv_quals |= cv_qualifier;
19606 /* Parse an (optional) ref-qualifier
19612 Returns cp_ref_qualifier representing ref-qualifier. */
19614 static cp_ref_qualifier
19615 cp_parser_ref_qualifier_opt (cp_parser* parser)
19617 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19619 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19620 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19625 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19626 cp_token *token = cp_lexer_peek_token (parser->lexer);
19628 switch (token->type)
19631 curr_ref_qual = REF_QUAL_LVALUE;
19635 curr_ref_qual = REF_QUAL_RVALUE;
19639 curr_ref_qual = REF_QUAL_NONE;
19643 if (!curr_ref_qual)
19647 error_at (token->location, "multiple ref-qualifiers");
19648 cp_lexer_purge_token (parser->lexer);
19652 ref_qual = curr_ref_qual;
19653 cp_lexer_consume_token (parser->lexer);
19660 /* Parse an optional tx-qualifier.
19664 transaction_safe_dynamic */
19667 cp_parser_tx_qualifier_opt (cp_parser *parser)
19669 cp_token *token = cp_lexer_peek_token (parser->lexer);
19670 if (token->type == CPP_NAME)
19672 tree name = token->u.value;
19673 const char *p = IDENTIFIER_POINTER (name);
19674 const int len = strlen ("transaction_safe");
19675 if (!strncmp (p, "transaction_safe", len))
19679 || !strcmp (p, "_dynamic"))
19681 cp_lexer_consume_token (parser->lexer);
19684 error ("%E requires %<-fgnu-tm%>", name);
19695 /* Parse an (optional) virt-specifier-seq.
19697 virt-specifier-seq:
19698 virt-specifier virt-specifier-seq [opt]
19704 Returns a bitmask representing the virt-specifiers. */
19706 static cp_virt_specifiers
19707 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19709 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19714 cp_virt_specifiers virt_specifier;
19716 /* Peek at the next token. */
19717 token = cp_lexer_peek_token (parser->lexer);
19718 /* See if it's a virt-specifier-qualifier. */
19719 if (token->type != CPP_NAME)
19721 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19723 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19724 virt_specifier = VIRT_SPEC_OVERRIDE;
19726 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19728 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19729 virt_specifier = VIRT_SPEC_FINAL;
19731 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19733 virt_specifier = VIRT_SPEC_FINAL;
19738 if (virt_specifiers & virt_specifier)
19740 error_at (token->location, "duplicate virt-specifier");
19741 cp_lexer_purge_token (parser->lexer);
19745 cp_lexer_consume_token (parser->lexer);
19746 virt_specifiers |= virt_specifier;
19749 return virt_specifiers;
19752 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19753 is in scope even though it isn't real. */
19756 inject_this_parameter (tree ctype, cp_cv_quals quals)
19760 if (current_class_ptr)
19762 /* We don't clear this between NSDMIs. Is it already what we want? */
19763 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19764 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19765 && cp_type_quals (type) == quals)
19769 this_parm = build_this_parm (ctype, quals);
19770 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19771 current_class_ptr = NULL_TREE;
19773 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19774 current_class_ptr = this_parm;
19777 /* Return true iff our current scope is a non-static data member
19781 parsing_nsdmi (void)
19783 /* We recognize NSDMI context by the context-less 'this' pointer set up
19784 by the function above. */
19785 if (current_class_ptr
19786 && TREE_CODE (current_class_ptr) == PARM_DECL
19787 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19792 /* Parse a late-specified return type, if any. This is not a separate
19793 non-terminal, but part of a function declarator, which looks like
19795 -> trailing-type-specifier-seq abstract-declarator(opt)
19797 Returns the type indicated by the type-id.
19799 In addition to this, parse any queued up omp declare simd
19800 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19802 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19806 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19807 tree& requires_clause, cp_cv_quals quals)
19810 tree type = NULL_TREE;
19811 bool declare_simd_p = (parser->omp_declare_simd
19813 && declarator->kind == cdk_id);
19815 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19816 && declarator && declarator->kind == cdk_id);
19818 bool oacc_routine_p = (parser->oacc_routine
19820 && declarator->kind == cdk_id);
19822 /* Peek at the next token. */
19823 token = cp_lexer_peek_token (parser->lexer);
19824 /* A late-specified return type is indicated by an initial '->'. */
19825 if (token->type != CPP_DEREF
19826 && token->keyword != RID_REQUIRES
19827 && !(token->type == CPP_NAME
19828 && token->u.value == ridpointers[RID_REQUIRES])
19829 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19832 tree save_ccp = current_class_ptr;
19833 tree save_ccr = current_class_ref;
19836 /* DR 1207: 'this' is in scope in the trailing return type. */
19837 inject_this_parameter (current_class_type, quals);
19840 if (token->type == CPP_DEREF)
19842 /* Consume the ->. */
19843 cp_lexer_consume_token (parser->lexer);
19845 type = cp_parser_trailing_type_id (parser);
19848 /* Function declarations may be followed by a trailing
19849 requires-clause. */
19850 requires_clause = cp_parser_requires_clause_opt (parser);
19852 if (cilk_simd_fn_vector_p)
19853 declarator->attributes
19854 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19855 declarator->attributes);
19856 if (declare_simd_p)
19857 declarator->attributes
19858 = cp_parser_late_parsing_omp_declare_simd (parser,
19859 declarator->attributes);
19860 if (oacc_routine_p)
19861 declarator->attributes
19862 = cp_parser_late_parsing_oacc_routine (parser,
19863 declarator->attributes);
19867 current_class_ptr = save_ccp;
19868 current_class_ref = save_ccr;
19874 /* Parse a declarator-id.
19878 :: [opt] nested-name-specifier [opt] type-name
19880 In the `id-expression' case, the value returned is as for
19881 cp_parser_id_expression if the id-expression was an unqualified-id.
19882 If the id-expression was a qualified-id, then a SCOPE_REF is
19883 returned. The first operand is the scope (either a NAMESPACE_DECL
19884 or TREE_TYPE), but the second is still just a representation of an
19888 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19891 /* The expression must be an id-expression. Assume that qualified
19892 names are the names of types so that:
19895 int S<T>::R::i = 3;
19897 will work; we must treat `S<T>::R' as the name of a type.
19898 Similarly, assume that qualified names are templates, where
19902 int S<T>::R<T>::i = 3;
19905 id = cp_parser_id_expression (parser,
19906 /*template_keyword_p=*/false,
19907 /*check_dependency_p=*/false,
19908 /*template_p=*/NULL,
19909 /*declarator_p=*/true,
19911 if (id && BASELINK_P (id))
19912 id = BASELINK_FUNCTIONS (id);
19916 /* Parse a type-id.
19919 type-specifier-seq abstract-declarator [opt]
19921 Returns the TYPE specified. */
19924 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19925 bool is_trailing_return)
19927 cp_decl_specifier_seq type_specifier_seq;
19928 cp_declarator *abstract_declarator;
19930 /* Parse the type-specifier-seq. */
19931 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19932 is_trailing_return,
19933 &type_specifier_seq);
19934 if (type_specifier_seq.type == error_mark_node)
19935 return error_mark_node;
19937 /* There might or might not be an abstract declarator. */
19938 cp_parser_parse_tentatively (parser);
19939 /* Look for the declarator. */
19940 abstract_declarator
19941 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19942 /*parenthesized_p=*/NULL,
19943 /*member_p=*/false,
19944 /*friend_p=*/false);
19945 /* Check to see if there really was a declarator. */
19946 if (!cp_parser_parse_definitely (parser))
19947 abstract_declarator = NULL;
19949 if (type_specifier_seq.type
19950 /* The concepts TS allows 'auto' as a type-id. */
19951 && (!flag_concepts || parser->in_type_id_in_expr_p)
19952 /* None of the valid uses of 'auto' in C++14 involve the type-id
19953 nonterminal, but it is valid in a trailing-return-type. */
19954 && !(cxx_dialect >= cxx14 && is_trailing_return)
19955 && type_uses_auto (type_specifier_seq.type))
19957 /* A type-id with type 'auto' is only ok if the abstract declarator
19958 is a function declarator with a late-specified return type.
19960 A type-id with 'auto' is also valid in a trailing-return-type
19961 in a compound-requirement. */
19962 if (abstract_declarator
19963 && abstract_declarator->kind == cdk_function
19964 && abstract_declarator->u.function.late_return_type)
19966 else if (parser->in_result_type_constraint_p)
19970 error ("invalid use of %<auto%>");
19971 return error_mark_node;
19975 return groktypename (&type_specifier_seq, abstract_declarator,
19980 cp_parser_type_id (cp_parser *parser)
19982 return cp_parser_type_id_1 (parser, false, false);
19986 cp_parser_template_type_arg (cp_parser *parser)
19989 const char *saved_message = parser->type_definition_forbidden_message;
19990 parser->type_definition_forbidden_message
19991 = G_("types may not be defined in template arguments");
19992 r = cp_parser_type_id_1 (parser, true, false);
19993 parser->type_definition_forbidden_message = saved_message;
19994 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19996 error ("invalid use of %<auto%> in template argument");
19997 r = error_mark_node;
20003 cp_parser_trailing_type_id (cp_parser *parser)
20005 return cp_parser_type_id_1 (parser, false, true);
20008 /* Parse a type-specifier-seq.
20010 type-specifier-seq:
20011 type-specifier type-specifier-seq [opt]
20015 type-specifier-seq:
20016 attributes type-specifier-seq [opt]
20018 If IS_DECLARATION is true, we are at the start of a "condition" or
20019 exception-declaration, so we might be followed by a declarator-id.
20021 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20022 i.e. we've just seen "->".
20024 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20027 cp_parser_type_specifier_seq (cp_parser* parser,
20028 bool is_declaration,
20029 bool is_trailing_return,
20030 cp_decl_specifier_seq *type_specifier_seq)
20032 bool seen_type_specifier = false;
20033 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20034 cp_token *start_token = NULL;
20036 /* Clear the TYPE_SPECIFIER_SEQ. */
20037 clear_decl_specs (type_specifier_seq);
20039 /* In the context of a trailing return type, enum E { } is an
20040 elaborated-type-specifier followed by a function-body, not an
20042 if (is_trailing_return)
20043 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20045 /* Parse the type-specifiers and attributes. */
20048 tree type_specifier;
20049 bool is_cv_qualifier;
20051 /* Check for attributes first. */
20052 if (cp_next_tokens_can_be_attribute_p (parser))
20054 type_specifier_seq->attributes =
20055 chainon (type_specifier_seq->attributes,
20056 cp_parser_attributes_opt (parser));
20060 /* record the token of the beginning of the type specifier seq,
20061 for error reporting purposes*/
20063 start_token = cp_lexer_peek_token (parser->lexer);
20065 /* Look for the type-specifier. */
20066 type_specifier = cp_parser_type_specifier (parser,
20068 type_specifier_seq,
20069 /*is_declaration=*/false,
20072 if (!type_specifier)
20074 /* If the first type-specifier could not be found, this is not a
20075 type-specifier-seq at all. */
20076 if (!seen_type_specifier)
20078 /* Set in_declarator_p to avoid skipping to the semicolon. */
20079 int in_decl = parser->in_declarator_p;
20080 parser->in_declarator_p = true;
20082 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20083 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20084 cp_parser_error (parser, "expected type-specifier");
20086 parser->in_declarator_p = in_decl;
20088 type_specifier_seq->type = error_mark_node;
20091 /* If subsequent type-specifiers could not be found, the
20092 type-specifier-seq is complete. */
20096 seen_type_specifier = true;
20097 /* The standard says that a condition can be:
20099 type-specifier-seq declarator = assignment-expression
20106 we should treat the "S" as a declarator, not as a
20107 type-specifier. The standard doesn't say that explicitly for
20108 type-specifier-seq, but it does say that for
20109 decl-specifier-seq in an ordinary declaration. Perhaps it
20110 would be clearer just to allow a decl-specifier-seq here, and
20111 then add a semantic restriction that if any decl-specifiers
20112 that are not type-specifiers appear, the program is invalid. */
20113 if (is_declaration && !is_cv_qualifier)
20114 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20118 /* Return whether the function currently being declared has an associated
20119 template parameter list. */
20122 function_being_declared_is_template_p (cp_parser* parser)
20124 if (!current_template_parms || processing_template_parmlist)
20127 if (parser->implicit_template_scope)
20130 if (at_class_scope_p ()
20131 && TYPE_BEING_DEFINED (current_class_type))
20132 return parser->num_template_parameter_lists != 0;
20134 return ((int) parser->num_template_parameter_lists > template_class_depth
20135 (current_class_type));
20138 /* Parse a parameter-declaration-clause.
20140 parameter-declaration-clause:
20141 parameter-declaration-list [opt] ... [opt]
20142 parameter-declaration-list , ...
20144 Returns a representation for the parameter declarations. A return
20145 value of NULL indicates a parameter-declaration-clause consisting
20146 only of an ellipsis. */
20149 cp_parser_parameter_declaration_clause (cp_parser* parser)
20158 int auto_is_implicit_function_template_parm_p;
20160 parser->auto_is_implicit_function_template_parm_p
20161 = auto_is_implicit_function_template_parm_p;
20163 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20167 if (!processing_specialization
20168 && !processing_template_parmlist
20169 && !processing_explicit_instantiation)
20170 if (!current_function_decl
20171 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20172 parser->auto_is_implicit_function_template_parm_p = true;
20174 /* Peek at the next token. */
20175 token = cp_lexer_peek_token (parser->lexer);
20176 /* Check for trivial parameter-declaration-clauses. */
20177 if (token->type == CPP_ELLIPSIS)
20179 /* Consume the `...' token. */
20180 cp_lexer_consume_token (parser->lexer);
20183 else if (token->type == CPP_CLOSE_PAREN)
20184 /* There are no parameters. */
20186 #ifndef NO_IMPLICIT_EXTERN_C
20187 if (in_system_header_at (input_location)
20188 && current_class_type == NULL
20189 && current_lang_name == lang_name_c)
20193 return void_list_node;
20195 /* Check for `(void)', too, which is a special case. */
20196 else if (token->keyword == RID_VOID
20197 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20198 == CPP_CLOSE_PAREN))
20200 /* Consume the `void' token. */
20201 cp_lexer_consume_token (parser->lexer);
20202 /* There are no parameters. */
20203 return void_list_node;
20206 /* Parse the parameter-declaration-list. */
20207 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20208 /* If a parse error occurred while parsing the
20209 parameter-declaration-list, then the entire
20210 parameter-declaration-clause is erroneous. */
20214 /* Peek at the next token. */
20215 token = cp_lexer_peek_token (parser->lexer);
20216 /* If it's a `,', the clause should terminate with an ellipsis. */
20217 if (token->type == CPP_COMMA)
20219 /* Consume the `,'. */
20220 cp_lexer_consume_token (parser->lexer);
20221 /* Expect an ellipsis. */
20223 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20225 /* It might also be `...' if the optional trailing `,' was
20227 else if (token->type == CPP_ELLIPSIS)
20229 /* Consume the `...' token. */
20230 cp_lexer_consume_token (parser->lexer);
20231 /* And remember that we saw it. */
20235 ellipsis_p = false;
20237 /* Finish the parameter list. */
20239 parameters = chainon (parameters, void_list_node);
20244 /* Parse a parameter-declaration-list.
20246 parameter-declaration-list:
20247 parameter-declaration
20248 parameter-declaration-list , parameter-declaration
20250 Returns a representation of the parameter-declaration-list, as for
20251 cp_parser_parameter_declaration_clause. However, the
20252 `void_list_node' is never appended to the list. Upon return,
20253 *IS_ERROR will be true iff an error occurred. */
20256 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20258 tree parameters = NULL_TREE;
20259 tree *tail = ¶meters;
20260 bool saved_in_unbraced_linkage_specification_p;
20263 /* Assume all will go well. */
20265 /* The special considerations that apply to a function within an
20266 unbraced linkage specifications do not apply to the parameters
20267 to the function. */
20268 saved_in_unbraced_linkage_specification_p
20269 = parser->in_unbraced_linkage_specification_p;
20270 parser->in_unbraced_linkage_specification_p = false;
20272 /* Look for more parameters. */
20275 cp_parameter_declarator *parameter;
20276 tree decl = error_mark_node;
20277 bool parenthesized_p = false;
20278 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20279 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20280 (current_template_parms)) : 0);
20282 /* Parse the parameter. */
20284 = cp_parser_parameter_declaration (parser,
20285 /*template_parm_p=*/false,
20288 /* We don't know yet if the enclosing context is deprecated, so wait
20289 and warn in grokparms if appropriate. */
20290 deprecated_state = DEPRECATED_SUPPRESS;
20294 /* If a function parameter pack was specified and an implicit template
20295 parameter was introduced during cp_parser_parameter_declaration,
20296 change any implicit parameters introduced into packs. */
20297 if (parser->implicit_template_parms
20298 && parameter->declarator
20299 && parameter->declarator->parameter_pack_p)
20301 int latest_template_parm_idx = TREE_VEC_LENGTH
20302 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20304 if (latest_template_parm_idx != template_parm_idx)
20305 parameter->decl_specifiers.type = convert_generic_types_to_packs
20306 (parameter->decl_specifiers.type,
20307 template_parm_idx, latest_template_parm_idx);
20310 decl = grokdeclarator (parameter->declarator,
20311 ¶meter->decl_specifiers,
20313 parameter->default_argument != NULL_TREE,
20314 ¶meter->decl_specifiers.attributes);
20317 deprecated_state = DEPRECATED_NORMAL;
20319 /* If a parse error occurred parsing the parameter declaration,
20320 then the entire parameter-declaration-list is erroneous. */
20321 if (decl == error_mark_node)
20324 parameters = error_mark_node;
20328 if (parameter->decl_specifiers.attributes)
20329 cplus_decl_attributes (&decl,
20330 parameter->decl_specifiers.attributes,
20332 if (DECL_NAME (decl))
20333 decl = pushdecl (decl);
20335 if (decl != error_mark_node)
20337 retrofit_lang_decl (decl);
20338 DECL_PARM_INDEX (decl) = ++index;
20339 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20342 /* Add the new parameter to the list. */
20343 *tail = build_tree_list (parameter->default_argument, decl);
20344 tail = &TREE_CHAIN (*tail);
20346 /* Peek at the next token. */
20347 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20348 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20349 /* These are for Objective-C++ */
20350 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20351 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20352 /* The parameter-declaration-list is complete. */
20354 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20358 /* Peek at the next token. */
20359 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20360 /* If it's an ellipsis, then the list is complete. */
20361 if (token->type == CPP_ELLIPSIS)
20363 /* Otherwise, there must be more parameters. Consume the
20365 cp_lexer_consume_token (parser->lexer);
20366 /* When parsing something like:
20368 int i(float f, double d)
20370 we can tell after seeing the declaration for "f" that we
20371 are not looking at an initialization of a variable "i",
20372 but rather at the declaration of a function "i".
20374 Due to the fact that the parsing of template arguments
20375 (as specified to a template-id) requires backtracking we
20376 cannot use this technique when inside a template argument
20378 if (!parser->in_template_argument_list_p
20379 && !parser->in_type_id_in_expr_p
20380 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20381 /* However, a parameter-declaration of the form
20382 "float(f)" (which is a valid declaration of a
20383 parameter "f") can also be interpreted as an
20384 expression (the conversion of "f" to "float"). */
20385 && !parenthesized_p)
20386 cp_parser_commit_to_tentative_parse (parser);
20390 cp_parser_error (parser, "expected %<,%> or %<...%>");
20391 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20392 cp_parser_skip_to_closing_parenthesis (parser,
20393 /*recovering=*/true,
20394 /*or_comma=*/false,
20395 /*consume_paren=*/false);
20400 parser->in_unbraced_linkage_specification_p
20401 = saved_in_unbraced_linkage_specification_p;
20403 /* Reset implicit_template_scope if we are about to leave the function
20404 parameter list that introduced it. Note that for out-of-line member
20405 definitions, there will be one or more class scopes before we get to
20406 the template parameter scope. */
20408 if (cp_binding_level *its = parser->implicit_template_scope)
20409 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20411 while (maybe_its->kind == sk_class)
20412 maybe_its = maybe_its->level_chain;
20413 if (maybe_its == its)
20415 parser->implicit_template_parms = 0;
20416 parser->implicit_template_scope = 0;
20423 /* Parse a parameter declaration.
20425 parameter-declaration:
20426 decl-specifier-seq ... [opt] declarator
20427 decl-specifier-seq declarator = assignment-expression
20428 decl-specifier-seq ... [opt] abstract-declarator [opt]
20429 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20431 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20432 declares a template parameter. (In that case, a non-nested `>'
20433 token encountered during the parsing of the assignment-expression
20434 is not interpreted as a greater-than operator.)
20436 Returns a representation of the parameter, or NULL if an error
20437 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20438 true iff the declarator is of the form "(p)". */
20440 static cp_parameter_declarator *
20441 cp_parser_parameter_declaration (cp_parser *parser,
20442 bool template_parm_p,
20443 bool *parenthesized_p)
20445 int declares_class_or_enum;
20446 cp_decl_specifier_seq decl_specifiers;
20447 cp_declarator *declarator;
20448 tree default_argument;
20449 cp_token *token = NULL, *declarator_token_start = NULL;
20450 const char *saved_message;
20451 bool template_parameter_pack_p = false;
20453 /* In a template parameter, `>' is not an operator.
20457 When parsing a default template-argument for a non-type
20458 template-parameter, the first non-nested `>' is taken as the end
20459 of the template parameter-list rather than a greater-than
20462 /* Type definitions may not appear in parameter types. */
20463 saved_message = parser->type_definition_forbidden_message;
20464 parser->type_definition_forbidden_message
20465 = G_("types may not be defined in parameter types");
20467 /* Parse the declaration-specifiers. */
20468 cp_parser_decl_specifier_seq (parser,
20469 CP_PARSER_FLAGS_NONE,
20471 &declares_class_or_enum);
20473 /* Complain about missing 'typename' or other invalid type names. */
20474 if (!decl_specifiers.any_type_specifiers_p
20475 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20476 decl_specifiers.type = error_mark_node;
20478 /* If an error occurred, there's no reason to attempt to parse the
20479 rest of the declaration. */
20480 if (cp_parser_error_occurred (parser))
20482 parser->type_definition_forbidden_message = saved_message;
20486 /* Peek at the next token. */
20487 token = cp_lexer_peek_token (parser->lexer);
20489 /* If the next token is a `)', `,', `=', `>', or `...', then there
20490 is no declarator. However, when variadic templates are enabled,
20491 there may be a declarator following `...'. */
20492 if (token->type == CPP_CLOSE_PAREN
20493 || token->type == CPP_COMMA
20494 || token->type == CPP_EQ
20495 || token->type == CPP_GREATER)
20498 if (parenthesized_p)
20499 *parenthesized_p = false;
20501 /* Otherwise, there should be a declarator. */
20504 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20505 parser->default_arg_ok_p = false;
20507 /* After seeing a decl-specifier-seq, if the next token is not a
20508 "(", there is no possibility that the code is a valid
20509 expression. Therefore, if parsing tentatively, we commit at
20511 if (!parser->in_template_argument_list_p
20512 /* In an expression context, having seen:
20516 we cannot be sure whether we are looking at a
20517 function-type (taking a "char" as a parameter) or a cast
20518 of some object of type "char" to "int". */
20519 && !parser->in_type_id_in_expr_p
20520 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20521 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20522 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20523 cp_parser_commit_to_tentative_parse (parser);
20524 /* Parse the declarator. */
20525 declarator_token_start = token;
20526 declarator = cp_parser_declarator (parser,
20527 CP_PARSER_DECLARATOR_EITHER,
20528 /*ctor_dtor_or_conv_p=*/NULL,
20530 /*member_p=*/false,
20531 /*friend_p=*/false);
20532 parser->default_arg_ok_p = saved_default_arg_ok_p;
20533 /* After the declarator, allow more attributes. */
20534 decl_specifiers.attributes
20535 = chainon (decl_specifiers.attributes,
20536 cp_parser_attributes_opt (parser));
20538 /* If the declarator is a template parameter pack, remember that and
20539 clear the flag in the declarator itself so we don't get errors
20540 from grokdeclarator. */
20541 if (template_parm_p && declarator && declarator->parameter_pack_p)
20543 declarator->parameter_pack_p = false;
20544 template_parameter_pack_p = true;
20548 /* If the next token is an ellipsis, and we have not seen a declarator
20549 name, and if either the type of the declarator contains parameter
20550 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20551 for, eg, abbreviated integral type names), then we actually have a
20552 parameter pack expansion expression. Otherwise, leave the ellipsis
20553 for a C-style variadic function. */
20554 token = cp_lexer_peek_token (parser->lexer);
20555 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20557 tree type = decl_specifiers.type;
20559 if (type && DECL_P (type))
20560 type = TREE_TYPE (type);
20563 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20564 && (template_parm_p || uses_parameter_packs (type)))
20565 || (!type && template_parm_p))
20566 && declarator_can_be_parameter_pack (declarator))
20568 /* Consume the `...'. */
20569 cp_lexer_consume_token (parser->lexer);
20570 maybe_warn_variadic_templates ();
20572 /* Build a pack expansion type */
20573 if (template_parm_p)
20574 template_parameter_pack_p = true;
20575 else if (declarator)
20576 declarator->parameter_pack_p = true;
20578 decl_specifiers.type = make_pack_expansion (type);
20582 /* The restriction on defining new types applies only to the type
20583 of the parameter, not to the default argument. */
20584 parser->type_definition_forbidden_message = saved_message;
20586 /* If the next token is `=', then process a default argument. */
20587 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20589 tree type = decl_specifiers.type;
20590 token = cp_lexer_peek_token (parser->lexer);
20591 /* If we are defining a class, then the tokens that make up the
20592 default argument must be saved and processed later. */
20593 if (!template_parm_p && at_class_scope_p ()
20594 && TYPE_BEING_DEFINED (current_class_type)
20595 && !LAMBDA_TYPE_P (current_class_type))
20596 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20598 // A constrained-type-specifier may declare a type template-parameter.
20599 else if (declares_constrained_type_template_parameter (type))
20601 = cp_parser_default_type_template_argument (parser);
20603 // A constrained-type-specifier may declare a template-template-parameter.
20604 else if (declares_constrained_template_template_parameter (type))
20606 = cp_parser_default_template_template_argument (parser);
20608 /* Outside of a class definition, we can just parse the
20609 assignment-expression. */
20612 = cp_parser_default_argument (parser, template_parm_p);
20614 if (!parser->default_arg_ok_p)
20616 permerror (token->location,
20617 "default arguments are only "
20618 "permitted for function parameters");
20620 else if ((declarator && declarator->parameter_pack_p)
20621 || template_parameter_pack_p
20622 || (decl_specifiers.type
20623 && PACK_EXPANSION_P (decl_specifiers.type)))
20625 /* Find the name of the parameter pack. */
20626 cp_declarator *id_declarator = declarator;
20627 while (id_declarator && id_declarator->kind != cdk_id)
20628 id_declarator = id_declarator->declarator;
20630 if (id_declarator && id_declarator->kind == cdk_id)
20631 error_at (declarator_token_start->location,
20633 ? G_("template parameter pack %qD "
20634 "cannot have a default argument")
20635 : G_("parameter pack %qD cannot have "
20636 "a default argument"),
20637 id_declarator->u.id.unqualified_name);
20639 error_at (declarator_token_start->location,
20641 ? G_("template parameter pack cannot have "
20642 "a default argument")
20643 : G_("parameter pack cannot have a "
20644 "default argument"));
20646 default_argument = NULL_TREE;
20650 default_argument = NULL_TREE;
20652 return make_parameter_declarator (&decl_specifiers,
20655 template_parameter_pack_p);
20658 /* Parse a default argument and return it.
20660 TEMPLATE_PARM_P is true if this is a default argument for a
20661 non-type template parameter. */
20663 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20665 tree default_argument = NULL_TREE;
20666 bool saved_greater_than_is_operator_p;
20667 bool saved_local_variables_forbidden_p;
20668 bool non_constant_p, is_direct_init;
20670 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20672 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20673 parser->greater_than_is_operator_p = !template_parm_p;
20674 /* Local variable names (and the `this' keyword) may not
20675 appear in a default argument. */
20676 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20677 parser->local_variables_forbidden_p = true;
20678 /* Parse the assignment-expression. */
20679 if (template_parm_p)
20680 push_deferring_access_checks (dk_no_deferred);
20681 tree saved_class_ptr = NULL_TREE;
20682 tree saved_class_ref = NULL_TREE;
20683 /* The "this" pointer is not valid in a default argument. */
20686 saved_class_ptr = current_class_ptr;
20687 cp_function_chain->x_current_class_ptr = NULL_TREE;
20688 saved_class_ref = current_class_ref;
20689 cp_function_chain->x_current_class_ref = NULL_TREE;
20692 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20693 /* Restore the "this" pointer. */
20696 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20697 cp_function_chain->x_current_class_ref = saved_class_ref;
20699 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20700 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20701 if (template_parm_p)
20702 pop_deferring_access_checks ();
20703 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20704 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20706 return default_argument;
20709 /* Parse a function-body.
20712 compound_statement */
20715 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20717 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20718 ? BCS_TRY_BLOCK : BCS_NORMAL),
20722 /* Parse a ctor-initializer-opt followed by a function-body. Return
20723 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20724 is true we are parsing a function-try-block. */
20727 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20728 bool in_function_try_block)
20731 bool ctor_initializer_p;
20732 const bool check_body_p =
20733 DECL_CONSTRUCTOR_P (current_function_decl)
20734 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20737 /* Begin the function body. */
20738 body = begin_function_body ();
20739 /* Parse the optional ctor-initializer. */
20740 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20742 /* If we're parsing a constexpr constructor definition, we need
20743 to check that the constructor body is indeed empty. However,
20744 before we get to cp_parser_function_body lot of junk has been
20745 generated, so we can't just check that we have an empty block.
20746 Rather we take a snapshot of the outermost block, and check whether
20747 cp_parser_function_body changed its state. */
20750 list = cur_stmt_list;
20751 if (STATEMENT_LIST_TAIL (list))
20752 last = STATEMENT_LIST_TAIL (list)->stmt;
20754 /* Parse the function-body. */
20755 cp_parser_function_body (parser, in_function_try_block);
20757 check_constexpr_ctor_body (last, list, /*complain=*/true);
20758 /* Finish the function body. */
20759 finish_function_body (body);
20761 return ctor_initializer_p;
20764 /* Parse an initializer.
20767 = initializer-clause
20768 ( expression-list )
20770 Returns an expression representing the initializer. If no
20771 initializer is present, NULL_TREE is returned.
20773 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20774 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20775 set to TRUE if there is no initializer present. If there is an
20776 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20777 is set to true; otherwise it is set to false. */
20780 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20781 bool* non_constant_p)
20786 /* Peek at the next token. */
20787 token = cp_lexer_peek_token (parser->lexer);
20789 /* Let our caller know whether or not this initializer was
20791 *is_direct_init = (token->type != CPP_EQ);
20792 /* Assume that the initializer is constant. */
20793 *non_constant_p = false;
20795 if (token->type == CPP_EQ)
20797 /* Consume the `='. */
20798 cp_lexer_consume_token (parser->lexer);
20799 /* Parse the initializer-clause. */
20800 init = cp_parser_initializer_clause (parser, non_constant_p);
20802 else if (token->type == CPP_OPEN_PAREN)
20804 vec<tree, va_gc> *vec;
20805 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20807 /*allow_expansion_p=*/true,
20810 return error_mark_node;
20811 init = build_tree_list_vec (vec);
20812 release_tree_vector (vec);
20814 else if (token->type == CPP_OPEN_BRACE)
20816 cp_lexer_set_source_position (parser->lexer);
20817 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20818 init = cp_parser_braced_list (parser, non_constant_p);
20819 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20823 /* Anything else is an error. */
20824 cp_parser_error (parser, "expected initializer");
20825 init = error_mark_node;
20831 /* Parse an initializer-clause.
20833 initializer-clause:
20834 assignment-expression
20837 Returns an expression representing the initializer.
20839 If the `assignment-expression' production is used the value
20840 returned is simply a representation for the expression.
20842 Otherwise, calls cp_parser_braced_list. */
20845 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20847 cp_expr initializer;
20849 /* Assume the expression is constant. */
20850 *non_constant_p = false;
20852 /* If it is not a `{', then we are looking at an
20853 assignment-expression. */
20854 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20857 = cp_parser_constant_expression (parser,
20858 /*allow_non_constant_p=*/true,
20862 initializer = cp_parser_braced_list (parser, non_constant_p);
20864 return initializer;
20867 /* Parse a brace-enclosed initializer list.
20870 { initializer-list , [opt] }
20873 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20874 the elements of the initializer-list (or NULL, if the last
20875 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20876 NULL_TREE. There is no way to detect whether or not the optional
20877 trailing `,' was provided. NON_CONSTANT_P is as for
20878 cp_parser_initializer. */
20881 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20884 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20886 /* Consume the `{' token. */
20887 cp_lexer_consume_token (parser->lexer);
20888 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20889 initializer = make_node (CONSTRUCTOR);
20890 /* If it's not a `}', then there is a non-trivial initializer. */
20891 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20893 /* Parse the initializer list. */
20894 CONSTRUCTOR_ELTS (initializer)
20895 = cp_parser_initializer_list (parser, non_constant_p);
20896 /* A trailing `,' token is allowed. */
20897 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20898 cp_lexer_consume_token (parser->lexer);
20901 *non_constant_p = false;
20902 /* Now, there should be a trailing `}'. */
20903 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20904 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20905 TREE_TYPE (initializer) = init_list_type_node;
20907 cp_expr result (initializer);
20908 /* Build a location of the form:
20911 with caret==start at the open brace, finish at the close brace. */
20912 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20913 result.set_location (combined_loc);
20917 /* Consume tokens up to, and including, the next non-nested closing `]'.
20918 Returns true iff we found a closing `]'. */
20921 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20923 unsigned square_depth = 0;
20927 cp_token * token = cp_lexer_peek_token (parser->lexer);
20929 switch (token->type)
20932 case CPP_PRAGMA_EOL:
20933 /* If we've run out of tokens, then there is no closing `]'. */
20936 case CPP_OPEN_SQUARE:
20940 case CPP_CLOSE_SQUARE:
20941 if (!square_depth--)
20943 cp_lexer_consume_token (parser->lexer);
20952 /* Consume the token. */
20953 cp_lexer_consume_token (parser->lexer);
20957 /* Return true if we are looking at an array-designator, false otherwise. */
20960 cp_parser_array_designator_p (cp_parser *parser)
20962 /* Consume the `['. */
20963 cp_lexer_consume_token (parser->lexer);
20965 cp_lexer_save_tokens (parser->lexer);
20967 /* Skip tokens until the next token is a closing square bracket.
20968 If we find the closing `]', and the next token is a `=', then
20969 we are looking at an array designator. */
20970 bool array_designator_p
20971 = (cp_parser_skip_to_closing_square_bracket (parser)
20972 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20974 /* Roll back the tokens we skipped. */
20975 cp_lexer_rollback_tokens (parser->lexer);
20977 return array_designator_p;
20980 /* Parse an initializer-list.
20983 initializer-clause ... [opt]
20984 initializer-list , initializer-clause ... [opt]
20989 designation initializer-clause ...[opt]
20990 initializer-list , designation initializer-clause ...[opt]
20995 [ constant-expression ] =
20997 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20998 for the initializer. If the INDEX of the elt is non-NULL, it is the
20999 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21000 as for cp_parser_initializer. */
21002 static vec<constructor_elt, va_gc> *
21003 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21005 vec<constructor_elt, va_gc> *v = NULL;
21007 /* Assume all of the expressions are constant. */
21008 *non_constant_p = false;
21010 /* Parse the rest of the list. */
21016 bool clause_non_constant_p;
21018 /* If the next token is an identifier and the following one is a
21019 colon, we are looking at the GNU designated-initializer
21021 if (cp_parser_allow_gnu_extensions_p (parser)
21022 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21023 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21025 /* Warn the user that they are using an extension. */
21026 pedwarn (input_location, OPT_Wpedantic,
21027 "ISO C++ does not allow designated initializers");
21028 /* Consume the identifier. */
21029 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21030 /* Consume the `:'. */
21031 cp_lexer_consume_token (parser->lexer);
21033 /* Also handle the C99 syntax, '. id ='. */
21034 else if (cp_parser_allow_gnu_extensions_p (parser)
21035 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21036 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21037 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21039 /* Warn the user that they are using an extension. */
21040 pedwarn (input_location, OPT_Wpedantic,
21041 "ISO C++ does not allow C99 designated initializers");
21042 /* Consume the `.'. */
21043 cp_lexer_consume_token (parser->lexer);
21044 /* Consume the identifier. */
21045 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21046 /* Consume the `='. */
21047 cp_lexer_consume_token (parser->lexer);
21049 /* Also handle C99 array designators, '[ const ] ='. */
21050 else if (cp_parser_allow_gnu_extensions_p (parser)
21051 && !c_dialect_objc ()
21052 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21054 /* In C++11, [ could start a lambda-introducer. */
21055 bool non_const = false;
21057 cp_parser_parse_tentatively (parser);
21059 if (!cp_parser_array_designator_p (parser))
21061 cp_parser_simulate_error (parser);
21062 designator = NULL_TREE;
21066 designator = cp_parser_constant_expression (parser, true,
21068 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21069 cp_parser_require (parser, CPP_EQ, RT_EQ);
21072 if (!cp_parser_parse_definitely (parser))
21073 designator = NULL_TREE;
21074 else if (non_const)
21075 require_potential_rvalue_constant_expression (designator);
21078 designator = NULL_TREE;
21080 /* Parse the initializer. */
21081 initializer = cp_parser_initializer_clause (parser,
21082 &clause_non_constant_p);
21083 /* If any clause is non-constant, so is the entire initializer. */
21084 if (clause_non_constant_p)
21085 *non_constant_p = true;
21087 /* If we have an ellipsis, this is an initializer pack
21089 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21091 /* Consume the `...'. */
21092 cp_lexer_consume_token (parser->lexer);
21094 /* Turn the initializer into an initializer expansion. */
21095 initializer = make_pack_expansion (initializer);
21098 /* Add it to the vector. */
21099 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21101 /* If the next token is not a comma, we have reached the end of
21103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21106 /* Peek at the next token. */
21107 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21108 /* If the next token is a `}', then we're still done. An
21109 initializer-clause can have a trailing `,' after the
21110 initializer-list and before the closing `}'. */
21111 if (token->type == CPP_CLOSE_BRACE)
21114 /* Consume the `,' token. */
21115 cp_lexer_consume_token (parser->lexer);
21121 /* Classes [gram.class] */
21123 /* Parse a class-name.
21129 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21130 to indicate that names looked up in dependent types should be
21131 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21132 keyword has been used to indicate that the name that appears next
21133 is a template. TAG_TYPE indicates the explicit tag given before
21134 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21135 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21136 is the class being defined in a class-head. If ENUM_OK is TRUE,
21137 enum-names are also accepted.
21139 Returns the TYPE_DECL representing the class. */
21142 cp_parser_class_name (cp_parser *parser,
21143 bool typename_keyword_p,
21144 bool template_keyword_p,
21145 enum tag_types tag_type,
21146 bool check_dependency_p,
21148 bool is_declaration,
21155 tree identifier = NULL_TREE;
21157 /* All class-names start with an identifier. */
21158 token = cp_lexer_peek_token (parser->lexer);
21159 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21161 cp_parser_error (parser, "expected class-name");
21162 return error_mark_node;
21165 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21166 to a template-id, so we save it here. */
21167 scope = parser->scope;
21168 if (scope == error_mark_node)
21169 return error_mark_node;
21171 /* Any name names a type if we're following the `typename' keyword
21172 in a qualified name where the enclosing scope is type-dependent. */
21173 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21174 && dependent_type_p (scope));
21175 /* Handle the common case (an identifier, but not a template-id)
21177 if (token->type == CPP_NAME
21178 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21180 cp_token *identifier_token;
21183 /* Look for the identifier. */
21184 identifier_token = cp_lexer_peek_token (parser->lexer);
21185 ambiguous_p = identifier_token->error_reported;
21186 identifier = cp_parser_identifier (parser);
21187 /* If the next token isn't an identifier, we are certainly not
21188 looking at a class-name. */
21189 if (identifier == error_mark_node)
21190 decl = error_mark_node;
21191 /* If we know this is a type-name, there's no need to look it
21193 else if (typename_p)
21197 tree ambiguous_decls;
21198 /* If we already know that this lookup is ambiguous, then
21199 we've already issued an error message; there's no reason
21203 cp_parser_simulate_error (parser);
21204 return error_mark_node;
21206 /* If the next token is a `::', then the name must be a type
21209 [basic.lookup.qual]
21211 During the lookup for a name preceding the :: scope
21212 resolution operator, object, function, and enumerator
21213 names are ignored. */
21214 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21215 tag_type = scope_type;
21216 /* Look up the name. */
21217 decl = cp_parser_lookup_name (parser, identifier,
21219 /*is_template=*/false,
21220 /*is_namespace=*/false,
21221 check_dependency_p,
21223 identifier_token->location);
21224 if (ambiguous_decls)
21226 if (cp_parser_parsing_tentatively (parser))
21227 cp_parser_simulate_error (parser);
21228 return error_mark_node;
21234 /* Try a template-id. */
21235 decl = cp_parser_template_id (parser, template_keyword_p,
21236 check_dependency_p,
21239 if (decl == error_mark_node)
21240 return error_mark_node;
21243 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21245 /* If this is a typename, create a TYPENAME_TYPE. */
21246 if (typename_p && decl != error_mark_node)
21248 decl = make_typename_type (scope, decl, typename_type,
21249 /*complain=*/tf_error);
21250 if (decl != error_mark_node)
21251 decl = TYPE_NAME (decl);
21254 decl = strip_using_decl (decl);
21256 /* Check to see that it is really the name of a class. */
21257 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21258 && identifier_p (TREE_OPERAND (decl, 0))
21259 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21260 /* Situations like this:
21262 template <typename T> struct A {
21263 typename T::template X<int>::I i;
21266 are problematic. Is `T::template X<int>' a class-name? The
21267 standard does not seem to be definitive, but there is no other
21268 valid interpretation of the following `::'. Therefore, those
21269 names are considered class-names. */
21271 decl = make_typename_type (scope, decl, tag_type, tf_error);
21272 if (decl != error_mark_node)
21273 decl = TYPE_NAME (decl);
21275 else if (TREE_CODE (decl) != TYPE_DECL
21276 || TREE_TYPE (decl) == error_mark_node
21277 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21278 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21279 /* In Objective-C 2.0, a classname followed by '.' starts a
21280 dot-syntax expression, and it's not a type-name. */
21281 || (c_dialect_objc ()
21282 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21283 && objc_is_class_name (decl)))
21284 decl = error_mark_node;
21286 if (decl == error_mark_node)
21287 cp_parser_error (parser, "expected class-name");
21288 else if (identifier && !parser->scope)
21289 maybe_note_name_used_in_class (identifier, decl);
21294 /* Parse a class-specifier.
21297 class-head { member-specification [opt] }
21299 Returns the TREE_TYPE representing the class. */
21302 cp_parser_class_specifier_1 (cp_parser* parser)
21305 tree attributes = NULL_TREE;
21306 bool nested_name_specifier_p;
21307 unsigned saved_num_template_parameter_lists;
21308 bool saved_in_function_body;
21309 unsigned char in_statement;
21310 bool in_switch_statement_p;
21311 bool saved_in_unbraced_linkage_specification_p;
21312 tree old_scope = NULL_TREE;
21313 tree scope = NULL_TREE;
21314 cp_token *closing_brace;
21316 push_deferring_access_checks (dk_no_deferred);
21318 /* Parse the class-head. */
21319 type = cp_parser_class_head (parser,
21320 &nested_name_specifier_p);
21321 /* If the class-head was a semantic disaster, skip the entire body
21325 cp_parser_skip_to_end_of_block_or_statement (parser);
21326 pop_deferring_access_checks ();
21327 return error_mark_node;
21330 /* Look for the `{'. */
21331 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21333 pop_deferring_access_checks ();
21334 return error_mark_node;
21337 cp_ensure_no_omp_declare_simd (parser);
21338 cp_ensure_no_oacc_routine (parser);
21340 /* Issue an error message if type-definitions are forbidden here. */
21341 cp_parser_check_type_definition (parser);
21342 /* Remember that we are defining one more class. */
21343 ++parser->num_classes_being_defined;
21344 /* Inside the class, surrounding template-parameter-lists do not
21346 saved_num_template_parameter_lists
21347 = parser->num_template_parameter_lists;
21348 parser->num_template_parameter_lists = 0;
21349 /* We are not in a function body. */
21350 saved_in_function_body = parser->in_function_body;
21351 parser->in_function_body = false;
21352 /* Or in a loop. */
21353 in_statement = parser->in_statement;
21354 parser->in_statement = 0;
21355 /* Or in a switch. */
21356 in_switch_statement_p = parser->in_switch_statement_p;
21357 parser->in_switch_statement_p = false;
21358 /* We are not immediately inside an extern "lang" block. */
21359 saved_in_unbraced_linkage_specification_p
21360 = parser->in_unbraced_linkage_specification_p;
21361 parser->in_unbraced_linkage_specification_p = false;
21363 // Associate constraints with the type.
21365 type = associate_classtype_constraints (type);
21367 /* Start the class. */
21368 if (nested_name_specifier_p)
21370 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21371 old_scope = push_inner_scope (scope);
21373 type = begin_class_definition (type);
21375 if (type == error_mark_node)
21376 /* If the type is erroneous, skip the entire body of the class. */
21377 cp_parser_skip_to_closing_brace (parser);
21379 /* Parse the member-specification. */
21380 cp_parser_member_specification_opt (parser);
21382 /* Look for the trailing `}'. */
21383 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21384 /* Look for trailing attributes to apply to this class. */
21385 if (cp_parser_allow_gnu_extensions_p (parser))
21386 attributes = cp_parser_gnu_attributes_opt (parser);
21387 if (type != error_mark_node)
21388 type = finish_struct (type, attributes);
21389 if (nested_name_specifier_p)
21390 pop_inner_scope (old_scope, scope);
21392 /* We've finished a type definition. Check for the common syntax
21393 error of forgetting a semicolon after the definition. We need to
21394 be careful, as we can't just check for not-a-semicolon and be done
21395 with it; the user might have typed:
21397 class X { } c = ...;
21398 class X { } *p = ...;
21400 and so forth. Instead, enumerate all the possible tokens that
21401 might follow this production; if we don't see one of them, then
21402 complain and silently insert the semicolon. */
21404 cp_token *token = cp_lexer_peek_token (parser->lexer);
21405 bool want_semicolon = true;
21407 if (cp_next_tokens_can_be_std_attribute_p (parser))
21408 /* Don't try to parse c++11 attributes here. As per the
21409 grammar, that should be a task for
21410 cp_parser_decl_specifier_seq. */
21411 want_semicolon = false;
21413 switch (token->type)
21416 case CPP_SEMICOLON:
21419 case CPP_OPEN_PAREN:
21420 case CPP_CLOSE_PAREN:
21422 want_semicolon = false;
21425 /* While it's legal for type qualifiers and storage class
21426 specifiers to follow type definitions in the grammar, only
21427 compiler testsuites contain code like that. Assume that if
21428 we see such code, then what we're really seeing is a case
21432 const <type> var = ...;
21437 static <type> func (...) ...
21439 i.e. the qualifier or specifier applies to the next
21440 declaration. To do so, however, we need to look ahead one
21441 more token to see if *that* token is a type specifier.
21443 This code could be improved to handle:
21446 static const <type> var = ...; */
21448 if (keyword_is_decl_specifier (token->keyword))
21450 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21452 /* Handling user-defined types here would be nice, but very
21455 = (lookahead->type == CPP_KEYWORD
21456 && keyword_begins_type_specifier (lookahead->keyword));
21463 /* If we don't have a type, then something is very wrong and we
21464 shouldn't try to do anything clever. Likewise for not seeing the
21466 if (closing_brace && TYPE_P (type) && want_semicolon)
21468 cp_token_position prev
21469 = cp_lexer_previous_token_position (parser->lexer);
21470 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21471 location_t loc = prev_token->location;
21473 if (CLASSTYPE_DECLARED_CLASS (type))
21474 error_at (loc, "expected %<;%> after class definition");
21475 else if (TREE_CODE (type) == RECORD_TYPE)
21476 error_at (loc, "expected %<;%> after struct definition");
21477 else if (TREE_CODE (type) == UNION_TYPE)
21478 error_at (loc, "expected %<;%> after union definition");
21480 gcc_unreachable ();
21482 /* Unget one token and smash it to look as though we encountered
21483 a semicolon in the input stream. */
21484 cp_lexer_set_token_position (parser->lexer, prev);
21485 token = cp_lexer_peek_token (parser->lexer);
21486 token->type = CPP_SEMICOLON;
21487 token->keyword = RID_MAX;
21491 /* If this class is not itself within the scope of another class,
21492 then we need to parse the bodies of all of the queued function
21493 definitions. Note that the queued functions defined in a class
21494 are not always processed immediately following the
21495 class-specifier for that class. Consider:
21498 struct B { void f() { sizeof (A); } };
21501 If `f' were processed before the processing of `A' were
21502 completed, there would be no way to compute the size of `A'.
21503 Note that the nesting we are interested in here is lexical --
21504 not the semantic nesting given by TYPE_CONTEXT. In particular,
21507 struct A { struct B; };
21508 struct A::B { void f() { } };
21510 there is no need to delay the parsing of `A::B::f'. */
21511 if (--parser->num_classes_being_defined == 0)
21514 tree class_type = NULL_TREE;
21515 tree pushed_scope = NULL_TREE;
21517 cp_default_arg_entry *e;
21518 tree save_ccp, save_ccr;
21520 /* In a first pass, parse default arguments to the functions.
21521 Then, in a second pass, parse the bodies of the functions.
21522 This two-phased approach handles cases like:
21530 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21533 /* If there are default arguments that have not yet been processed,
21534 take care of them now. */
21535 if (class_type != e->class_type)
21538 pop_scope (pushed_scope);
21539 class_type = e->class_type;
21540 pushed_scope = push_scope (class_type);
21542 /* Make sure that any template parameters are in scope. */
21543 maybe_begin_member_template_processing (decl);
21544 /* Parse the default argument expressions. */
21545 cp_parser_late_parsing_default_args (parser, decl);
21546 /* Remove any template parameters from the symbol table. */
21547 maybe_end_member_template_processing ();
21549 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21550 /* Now parse any NSDMIs. */
21551 save_ccp = current_class_ptr;
21552 save_ccr = current_class_ref;
21553 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21555 if (class_type != DECL_CONTEXT (decl))
21558 pop_scope (pushed_scope);
21559 class_type = DECL_CONTEXT (decl);
21560 pushed_scope = push_scope (class_type);
21562 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21563 cp_parser_late_parsing_nsdmi (parser, decl);
21565 vec_safe_truncate (unparsed_nsdmis, 0);
21566 current_class_ptr = save_ccp;
21567 current_class_ref = save_ccr;
21569 pop_scope (pushed_scope);
21571 /* Now do some post-NSDMI bookkeeping. */
21572 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21573 after_nsdmi_defaulted_late_checks (class_type);
21574 vec_safe_truncate (unparsed_classes, 0);
21575 after_nsdmi_defaulted_late_checks (type);
21577 /* Now parse the body of the functions. */
21580 /* OpenMP UDRs need to be parsed before all other functions. */
21581 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21582 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21583 cp_parser_late_parsing_for_member (parser, decl);
21584 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21585 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21586 cp_parser_late_parsing_for_member (parser, decl);
21589 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21590 cp_parser_late_parsing_for_member (parser, decl);
21591 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21594 vec_safe_push (unparsed_classes, type);
21596 /* Put back any saved access checks. */
21597 pop_deferring_access_checks ();
21599 /* Restore saved state. */
21600 parser->in_switch_statement_p = in_switch_statement_p;
21601 parser->in_statement = in_statement;
21602 parser->in_function_body = saved_in_function_body;
21603 parser->num_template_parameter_lists
21604 = saved_num_template_parameter_lists;
21605 parser->in_unbraced_linkage_specification_p
21606 = saved_in_unbraced_linkage_specification_p;
21612 cp_parser_class_specifier (cp_parser* parser)
21615 timevar_push (TV_PARSE_STRUCT);
21616 ret = cp_parser_class_specifier_1 (parser);
21617 timevar_pop (TV_PARSE_STRUCT);
21621 /* Parse a class-head.
21624 class-key identifier [opt] base-clause [opt]
21625 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21626 class-key nested-name-specifier [opt] template-id
21629 class-virt-specifier:
21633 class-key attributes identifier [opt] base-clause [opt]
21634 class-key attributes nested-name-specifier identifier base-clause [opt]
21635 class-key attributes nested-name-specifier [opt] template-id
21638 Upon return BASES is initialized to the list of base classes (or
21639 NULL, if there are none) in the same form returned by
21640 cp_parser_base_clause.
21642 Returns the TYPE of the indicated class. Sets
21643 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21644 involving a nested-name-specifier was used, and FALSE otherwise.
21646 Returns error_mark_node if this is not a class-head.
21648 Returns NULL_TREE if the class-head is syntactically valid, but
21649 semantically invalid in a way that means we should skip the entire
21650 body of the class. */
21653 cp_parser_class_head (cp_parser* parser,
21654 bool* nested_name_specifier_p)
21656 tree nested_name_specifier;
21657 enum tag_types class_key;
21658 tree id = NULL_TREE;
21659 tree type = NULL_TREE;
21662 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21663 bool template_id_p = false;
21664 bool qualified_p = false;
21665 bool invalid_nested_name_p = false;
21666 bool invalid_explicit_specialization_p = false;
21667 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21668 tree pushed_scope = NULL_TREE;
21669 unsigned num_templates;
21670 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21671 /* Assume no nested-name-specifier will be present. */
21672 *nested_name_specifier_p = false;
21673 /* Assume no template parameter lists will be used in defining the
21676 parser->colon_corrects_to_scope_p = false;
21678 /* Look for the class-key. */
21679 class_key = cp_parser_class_key (parser);
21680 if (class_key == none_type)
21681 return error_mark_node;
21683 /* Parse the attributes. */
21684 attributes = cp_parser_attributes_opt (parser);
21686 /* If the next token is `::', that is invalid -- but sometimes
21687 people do try to write:
21691 Handle this gracefully by accepting the extra qualifier, and then
21692 issuing an error about it later if this really is a
21693 class-head. If it turns out just to be an elaborated type
21694 specifier, remain silent. */
21695 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21696 qualified_p = true;
21698 push_deferring_access_checks (dk_no_check);
21700 /* Determine the name of the class. Begin by looking for an
21701 optional nested-name-specifier. */
21702 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21703 nested_name_specifier
21704 = cp_parser_nested_name_specifier_opt (parser,
21705 /*typename_keyword_p=*/false,
21706 /*check_dependency_p=*/false,
21708 /*is_declaration=*/false);
21709 /* If there was a nested-name-specifier, then there *must* be an
21711 if (nested_name_specifier)
21713 type_start_token = cp_lexer_peek_token (parser->lexer);
21714 /* Although the grammar says `identifier', it really means
21715 `class-name' or `template-name'. You are only allowed to
21716 define a class that has already been declared with this
21719 The proposed resolution for Core Issue 180 says that wherever
21720 you see `class T::X' you should treat `X' as a type-name.
21722 It is OK to define an inaccessible class; for example:
21724 class A { class B; };
21727 We do not know if we will see a class-name, or a
21728 template-name. We look for a class-name first, in case the
21729 class-name is a template-id; if we looked for the
21730 template-name first we would stop after the template-name. */
21731 cp_parser_parse_tentatively (parser);
21732 type = cp_parser_class_name (parser,
21733 /*typename_keyword_p=*/false,
21734 /*template_keyword_p=*/false,
21736 /*check_dependency_p=*/false,
21737 /*class_head_p=*/true,
21738 /*is_declaration=*/false);
21739 /* If that didn't work, ignore the nested-name-specifier. */
21740 if (!cp_parser_parse_definitely (parser))
21742 invalid_nested_name_p = true;
21743 type_start_token = cp_lexer_peek_token (parser->lexer);
21744 id = cp_parser_identifier (parser);
21745 if (id == error_mark_node)
21748 /* If we could not find a corresponding TYPE, treat this
21749 declaration like an unqualified declaration. */
21750 if (type == error_mark_node)
21751 nested_name_specifier = NULL_TREE;
21752 /* Otherwise, count the number of templates used in TYPE and its
21753 containing scopes. */
21758 for (scope = TREE_TYPE (type);
21759 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21760 scope = get_containing_scope (scope))
21762 && CLASS_TYPE_P (scope)
21763 && CLASSTYPE_TEMPLATE_INFO (scope)
21764 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21765 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21766 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21770 /* Otherwise, the identifier is optional. */
21773 /* We don't know whether what comes next is a template-id,
21774 an identifier, or nothing at all. */
21775 cp_parser_parse_tentatively (parser);
21776 /* Check for a template-id. */
21777 type_start_token = cp_lexer_peek_token (parser->lexer);
21778 id = cp_parser_template_id (parser,
21779 /*template_keyword_p=*/false,
21780 /*check_dependency_p=*/true,
21782 /*is_declaration=*/true);
21783 /* If that didn't work, it could still be an identifier. */
21784 if (!cp_parser_parse_definitely (parser))
21786 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21788 type_start_token = cp_lexer_peek_token (parser->lexer);
21789 id = cp_parser_identifier (parser);
21796 template_id_p = true;
21801 pop_deferring_access_checks ();
21805 cp_parser_check_for_invalid_template_id (parser, id,
21807 type_start_token->location);
21809 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21811 /* If it's not a `:' or a `{' then we can't really be looking at a
21812 class-head, since a class-head only appears as part of a
21813 class-specifier. We have to detect this situation before calling
21814 xref_tag, since that has irreversible side-effects. */
21815 if (!cp_parser_next_token_starts_class_definition_p (parser))
21817 cp_parser_error (parser, "expected %<{%> or %<:%>");
21818 type = error_mark_node;
21822 /* At this point, we're going ahead with the class-specifier, even
21823 if some other problem occurs. */
21824 cp_parser_commit_to_tentative_parse (parser);
21825 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21827 cp_parser_error (parser,
21828 "cannot specify %<override%> for a class");
21829 type = error_mark_node;
21832 /* Issue the error about the overly-qualified name now. */
21835 cp_parser_error (parser,
21836 "global qualification of class name is invalid");
21837 type = error_mark_node;
21840 else if (invalid_nested_name_p)
21842 cp_parser_error (parser,
21843 "qualified name does not name a class");
21844 type = error_mark_node;
21847 else if (nested_name_specifier)
21851 /* Reject typedef-names in class heads. */
21852 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21854 error_at (type_start_token->location,
21855 "invalid class name in declaration of %qD",
21861 /* Figure out in what scope the declaration is being placed. */
21862 scope = current_scope ();
21863 /* If that scope does not contain the scope in which the
21864 class was originally declared, the program is invalid. */
21865 if (scope && !is_ancestor (scope, nested_name_specifier))
21867 if (at_namespace_scope_p ())
21868 error_at (type_start_token->location,
21869 "declaration of %qD in namespace %qD which does not "
21871 type, scope, nested_name_specifier);
21873 error_at (type_start_token->location,
21874 "declaration of %qD in %qD which does not enclose %qD",
21875 type, scope, nested_name_specifier);
21881 A declarator-id shall not be qualified except for the
21882 definition of a ... nested class outside of its class
21883 ... [or] the definition or explicit instantiation of a
21884 class member of a namespace outside of its namespace. */
21885 if (scope == nested_name_specifier)
21887 permerror (nested_name_specifier_token_start->location,
21888 "extra qualification not allowed");
21889 nested_name_specifier = NULL_TREE;
21893 /* An explicit-specialization must be preceded by "template <>". If
21894 it is not, try to recover gracefully. */
21895 if (at_namespace_scope_p ()
21896 && parser->num_template_parameter_lists == 0
21899 error_at (type_start_token->location,
21900 "an explicit specialization must be preceded by %<template <>%>");
21901 invalid_explicit_specialization_p = true;
21902 /* Take the same action that would have been taken by
21903 cp_parser_explicit_specialization. */
21904 ++parser->num_template_parameter_lists;
21905 begin_specialization ();
21907 /* There must be no "return" statements between this point and the
21908 end of this function; set "type "to the correct return value and
21909 use "goto done;" to return. */
21910 /* Make sure that the right number of template parameters were
21912 if (!cp_parser_check_template_parameters (parser, num_templates,
21913 type_start_token->location,
21914 /*declarator=*/NULL))
21916 /* If something went wrong, there is no point in even trying to
21917 process the class-definition. */
21922 /* Look up the type. */
21925 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21926 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21927 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21929 error_at (type_start_token->location,
21930 "function template %qD redeclared as a class template", id);
21931 type = error_mark_node;
21935 type = TREE_TYPE (id);
21936 type = maybe_process_partial_specialization (type);
21938 if (nested_name_specifier)
21939 pushed_scope = push_scope (nested_name_specifier);
21941 else if (nested_name_specifier)
21947 template <typename T> struct S { struct T };
21948 template <typename T> struct S<T>::T { };
21950 we will get a TYPENAME_TYPE when processing the definition of
21951 `S::T'. We need to resolve it to the actual type before we
21952 try to define it. */
21953 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21955 class_type = resolve_typename_type (TREE_TYPE (type),
21956 /*only_current_p=*/false);
21957 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21958 type = TYPE_NAME (class_type);
21961 cp_parser_error (parser, "could not resolve typename type");
21962 type = error_mark_node;
21966 if (maybe_process_partial_specialization (TREE_TYPE (type))
21967 == error_mark_node)
21973 class_type = current_class_type;
21974 /* Enter the scope indicated by the nested-name-specifier. */
21975 pushed_scope = push_scope (nested_name_specifier);
21976 /* Get the canonical version of this type. */
21977 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21978 /* Call push_template_decl if it seems like we should be defining a
21979 template either from the template headers or the type we're
21980 defining, so that we diagnose both extra and missing headers. */
21981 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21982 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21983 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21985 type = push_template_decl (type);
21986 if (type == error_mark_node)
21993 type = TREE_TYPE (type);
21994 *nested_name_specifier_p = true;
21996 else /* The name is not a nested name. */
21998 /* If the class was unnamed, create a dummy name. */
22000 id = make_anon_name ();
22001 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22002 parser->num_template_parameter_lists);
22005 /* Indicate whether this class was declared as a `class' or as a
22007 if (TREE_CODE (type) == RECORD_TYPE)
22008 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22009 cp_parser_check_class_key (class_key, type);
22011 /* If this type was already complete, and we see another definition,
22012 that's an error. */
22013 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22015 error_at (type_start_token->location, "redefinition of %q#T",
22017 error_at (type_start_token->location, "previous definition of %q+#T",
22022 else if (type == error_mark_node)
22027 /* Apply attributes now, before any use of the class as a template
22028 argument in its base list. */
22029 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22030 fixup_attribute_variants (type);
22033 /* We will have entered the scope containing the class; the names of
22034 base classes should be looked up in that context. For example:
22036 struct A { struct B {}; struct C; };
22037 struct A::C : B {};
22041 /* Get the list of base-classes, if there is one. */
22042 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22044 /* PR59482: enter the class scope so that base-specifiers are looked
22048 bases = cp_parser_base_clause (parser);
22049 /* PR59482: get out of the previously pushed class scope so that the
22050 subsequent pops pop the right thing. */
22057 /* If we're really defining a class, process the base classes.
22058 If they're invalid, fail. */
22059 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22060 && !xref_basetypes (type, bases))
22064 /* Leave the scope given by the nested-name-specifier. We will
22065 enter the class scope itself while processing the members. */
22067 pop_scope (pushed_scope);
22069 if (invalid_explicit_specialization_p)
22071 end_specialization ();
22072 --parser->num_template_parameter_lists;
22076 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22077 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22078 CLASSTYPE_FINAL (type) = 1;
22080 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22084 /* Parse a class-key.
22091 Returns the kind of class-key specified, or none_type to indicate
22094 static enum tag_types
22095 cp_parser_class_key (cp_parser* parser)
22098 enum tag_types tag_type;
22100 /* Look for the class-key. */
22101 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22105 /* Check to see if the TOKEN is a class-key. */
22106 tag_type = cp_parser_token_is_class_key (token);
22108 cp_parser_error (parser, "expected class-key");
22112 /* Parse a type-parameter-key.
22114 type-parameter-key:
22120 cp_parser_type_parameter_key (cp_parser* parser)
22122 /* Look for the type-parameter-key. */
22123 enum tag_types tag_type = none_type;
22124 cp_token *token = cp_lexer_peek_token (parser->lexer);
22125 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22127 cp_lexer_consume_token (parser->lexer);
22128 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22129 /* typename is not allowed in a template template parameter
22130 by the standard until C++1Z. */
22131 pedwarn (token->location, OPT_Wpedantic,
22132 "ISO C++ forbids typename key in template template parameter;"
22133 " use -std=c++1z or -std=gnu++1z");
22136 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22141 /* Parse an (optional) member-specification.
22143 member-specification:
22144 member-declaration member-specification [opt]
22145 access-specifier : member-specification [opt] */
22148 cp_parser_member_specification_opt (cp_parser* parser)
22155 /* Peek at the next token. */
22156 token = cp_lexer_peek_token (parser->lexer);
22157 /* If it's a `}', or EOF then we've seen all the members. */
22158 if (token->type == CPP_CLOSE_BRACE
22159 || token->type == CPP_EOF
22160 || token->type == CPP_PRAGMA_EOL)
22163 /* See if this token is a keyword. */
22164 keyword = token->keyword;
22168 case RID_PROTECTED:
22170 /* Consume the access-specifier. */
22171 cp_lexer_consume_token (parser->lexer);
22172 /* Remember which access-specifier is active. */
22173 current_access_specifier = token->u.value;
22174 /* Look for the `:'. */
22175 cp_parser_require (parser, CPP_COLON, RT_COLON);
22179 /* Accept #pragmas at class scope. */
22180 if (token->type == CPP_PRAGMA)
22182 cp_parser_pragma (parser, pragma_member, NULL);
22186 /* Otherwise, the next construction must be a
22187 member-declaration. */
22188 cp_parser_member_declaration (parser);
22193 /* Parse a member-declaration.
22195 member-declaration:
22196 decl-specifier-seq [opt] member-declarator-list [opt] ;
22197 function-definition ; [opt]
22198 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22200 template-declaration
22203 member-declarator-list:
22205 member-declarator-list , member-declarator
22208 declarator pure-specifier [opt]
22209 declarator constant-initializer [opt]
22210 identifier [opt] : constant-expression
22214 member-declaration:
22215 __extension__ member-declaration
22218 declarator attributes [opt] pure-specifier [opt]
22219 declarator attributes [opt] constant-initializer [opt]
22220 identifier [opt] attributes [opt] : constant-expression
22224 member-declaration:
22225 static_assert-declaration */
22228 cp_parser_member_declaration (cp_parser* parser)
22230 cp_decl_specifier_seq decl_specifiers;
22231 tree prefix_attributes;
22233 int declares_class_or_enum;
22235 cp_token *token = NULL;
22236 cp_token *decl_spec_token_start = NULL;
22237 cp_token *initializer_token_start = NULL;
22238 int saved_pedantic;
22239 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22241 /* Check for the `__extension__' keyword. */
22242 if (cp_parser_extension_opt (parser, &saved_pedantic))
22245 cp_parser_member_declaration (parser);
22246 /* Restore the old value of the PEDANTIC flag. */
22247 pedantic = saved_pedantic;
22252 /* Check for a template-declaration. */
22253 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22255 /* An explicit specialization here is an error condition, and we
22256 expect the specialization handler to detect and report this. */
22257 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22258 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22259 cp_parser_explicit_specialization (parser);
22261 cp_parser_template_declaration (parser, /*member_p=*/true);
22265 /* Check for a template introduction. */
22266 else if (cp_parser_template_declaration_after_export (parser, true))
22269 /* Check for a using-declaration. */
22270 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22272 if (cxx_dialect < cxx11)
22274 /* Parse the using-declaration. */
22275 cp_parser_using_declaration (parser,
22276 /*access_declaration_p=*/false);
22282 bool alias_decl_expected;
22283 cp_parser_parse_tentatively (parser);
22284 decl = cp_parser_alias_declaration (parser);
22285 /* Note that if we actually see the '=' token after the
22286 identifier, cp_parser_alias_declaration commits the
22287 tentative parse. In that case, we really expect an
22288 alias-declaration. Otherwise, we expect a using
22290 alias_decl_expected =
22291 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22292 cp_parser_parse_definitely (parser);
22294 if (alias_decl_expected)
22295 finish_member_declaration (decl);
22297 cp_parser_using_declaration (parser,
22298 /*access_declaration_p=*/false);
22303 /* Check for @defs. */
22304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22307 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22308 ivar = ivar_chains;
22312 ivar = TREE_CHAIN (member);
22313 TREE_CHAIN (member) = NULL_TREE;
22314 finish_member_declaration (member);
22319 /* If the next token is `static_assert' we have a static assertion. */
22320 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22322 cp_parser_static_assert (parser, /*member_p=*/true);
22326 parser->colon_corrects_to_scope_p = false;
22328 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22331 /* Parse the decl-specifier-seq. */
22332 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22333 cp_parser_decl_specifier_seq (parser,
22334 CP_PARSER_FLAGS_OPTIONAL,
22336 &declares_class_or_enum);
22337 /* Check for an invalid type-name. */
22338 if (!decl_specifiers.any_type_specifiers_p
22339 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22341 /* If there is no declarator, then the decl-specifier-seq should
22343 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22345 /* If there was no decl-specifier-seq, and the next token is a
22346 `;', then we have something like:
22352 Each member-declaration shall declare at least one member
22353 name of the class. */
22354 if (!decl_specifiers.any_specifiers_p)
22356 cp_token *token = cp_lexer_peek_token (parser->lexer);
22357 if (!in_system_header_at (token->location))
22358 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22364 /* See if this declaration is a friend. */
22365 friend_p = cp_parser_friend_p (&decl_specifiers);
22366 /* If there were decl-specifiers, check to see if there was
22367 a class-declaration. */
22368 type = check_tag_decl (&decl_specifiers,
22369 /*explicit_type_instantiation_p=*/false);
22370 /* Nested classes have already been added to the class, but
22371 a `friend' needs to be explicitly registered. */
22374 /* If the `friend' keyword was present, the friend must
22375 be introduced with a class-key. */
22376 if (!declares_class_or_enum && cxx_dialect < cxx11)
22377 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22378 "in C++03 a class-key must be used "
22379 "when declaring a friend");
22382 template <typename T> struct A {
22383 friend struct A<T>::B;
22386 A<T>::B will be represented by a TYPENAME_TYPE, and
22387 therefore not recognized by check_tag_decl. */
22390 type = decl_specifiers.type;
22391 if (type && TREE_CODE (type) == TYPE_DECL)
22392 type = TREE_TYPE (type);
22394 if (!type || !TYPE_P (type))
22395 error_at (decl_spec_token_start->location,
22396 "friend declaration does not name a class or "
22399 make_friend_class (current_class_type, type,
22400 /*complain=*/true);
22402 /* If there is no TYPE, an error message will already have
22404 else if (!type || type == error_mark_node)
22406 /* An anonymous aggregate has to be handled specially; such
22407 a declaration really declares a data member (with a
22408 particular type), as opposed to a nested class. */
22409 else if (ANON_AGGR_TYPE_P (type))
22412 if (decl_specifiers.storage_class != sc_none)
22413 error_at (decl_spec_token_start->location,
22414 "a storage class on an anonymous aggregate "
22415 "in class scope is not allowed");
22417 /* Remove constructors and such from TYPE, now that we
22418 know it is an anonymous aggregate. */
22419 fixup_anonymous_aggr (type);
22420 /* And make the corresponding data member. */
22421 decl = build_decl (decl_spec_token_start->location,
22422 FIELD_DECL, NULL_TREE, type);
22423 /* Add it to the class. */
22424 finish_member_declaration (decl);
22427 cp_parser_check_access_in_redeclaration
22429 decl_spec_token_start->location);
22434 bool assume_semicolon = false;
22436 /* Clear attributes from the decl_specifiers but keep them
22437 around as prefix attributes that apply them to the entity
22439 prefix_attributes = decl_specifiers.attributes;
22440 decl_specifiers.attributes = NULL_TREE;
22442 /* See if these declarations will be friends. */
22443 friend_p = cp_parser_friend_p (&decl_specifiers);
22445 /* Keep going until we hit the `;' at the end of the
22447 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22449 tree attributes = NULL_TREE;
22450 tree first_attribute;
22452 /* Peek at the next token. */
22453 token = cp_lexer_peek_token (parser->lexer);
22455 /* Check for a bitfield declaration. */
22456 if (token->type == CPP_COLON
22457 || (token->type == CPP_NAME
22458 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22464 /* Get the name of the bitfield. Note that we cannot just
22465 check TOKEN here because it may have been invalidated by
22466 the call to cp_lexer_peek_nth_token above. */
22467 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22468 identifier = cp_parser_identifier (parser);
22470 identifier = NULL_TREE;
22472 /* Consume the `:' token. */
22473 cp_lexer_consume_token (parser->lexer);
22474 /* Get the width of the bitfield. */
22476 = cp_parser_constant_expression (parser);
22478 /* Look for attributes that apply to the bitfield. */
22479 attributes = cp_parser_attributes_opt (parser);
22480 /* Remember which attributes are prefix attributes and
22482 first_attribute = attributes;
22483 /* Combine the attributes. */
22484 attributes = chainon (prefix_attributes, attributes);
22486 /* Create the bitfield declaration. */
22487 decl = grokbitfield (identifier
22488 ? make_id_declarator (NULL_TREE,
22498 cp_declarator *declarator;
22500 tree asm_specification;
22501 int ctor_dtor_or_conv_p;
22503 /* Parse the declarator. */
22505 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22506 &ctor_dtor_or_conv_p,
22507 /*parenthesized_p=*/NULL,
22511 /* If something went wrong parsing the declarator, make sure
22512 that we at least consume some tokens. */
22513 if (declarator == cp_error_declarator)
22515 /* Skip to the end of the statement. */
22516 cp_parser_skip_to_end_of_statement (parser);
22517 /* If the next token is not a semicolon, that is
22518 probably because we just skipped over the body of
22519 a function. So, we consume a semicolon if
22520 present, but do not issue an error message if it
22522 if (cp_lexer_next_token_is (parser->lexer,
22524 cp_lexer_consume_token (parser->lexer);
22528 if (declares_class_or_enum & 2)
22529 cp_parser_check_for_definition_in_return_type
22530 (declarator, decl_specifiers.type,
22531 decl_specifiers.locations[ds_type_spec]);
22533 /* Look for an asm-specification. */
22534 asm_specification = cp_parser_asm_specification_opt (parser);
22535 /* Look for attributes that apply to the declaration. */
22536 attributes = cp_parser_attributes_opt (parser);
22537 /* Remember which attributes are prefix attributes and
22539 first_attribute = attributes;
22540 /* Combine the attributes. */
22541 attributes = chainon (prefix_attributes, attributes);
22543 /* If it's an `=', then we have a constant-initializer or a
22544 pure-specifier. It is not correct to parse the
22545 initializer before registering the member declaration
22546 since the member declaration should be in scope while
22547 its initializer is processed. However, the rest of the
22548 front end does not yet provide an interface that allows
22549 us to handle this correctly. */
22550 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22554 A pure-specifier shall be used only in the declaration of
22555 a virtual function.
22557 A member-declarator can contain a constant-initializer
22558 only if it declares a static member of integral or
22561 Therefore, if the DECLARATOR is for a function, we look
22562 for a pure-specifier; otherwise, we look for a
22563 constant-initializer. When we call `grokfield', it will
22564 perform more stringent semantics checks. */
22565 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22566 if (function_declarator_p (declarator)
22567 || (decl_specifiers.type
22568 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22569 && declarator->kind == cdk_id
22570 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22571 == FUNCTION_TYPE)))
22572 initializer = cp_parser_pure_specifier (parser);
22573 else if (decl_specifiers.storage_class != sc_static)
22574 initializer = cp_parser_save_nsdmi (parser);
22575 else if (cxx_dialect >= cxx11)
22578 /* Don't require a constant rvalue in C++11, since we
22579 might want a reference constant. We'll enforce
22580 constancy later. */
22581 cp_lexer_consume_token (parser->lexer);
22582 /* Parse the initializer. */
22583 initializer = cp_parser_initializer_clause (parser,
22587 /* Parse the initializer. */
22588 initializer = cp_parser_constant_initializer (parser);
22590 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22591 && !function_declarator_p (declarator))
22594 if (decl_specifiers.storage_class != sc_static)
22595 initializer = cp_parser_save_nsdmi (parser);
22597 initializer = cp_parser_initializer (parser, &x, &x);
22599 /* Otherwise, there is no initializer. */
22601 initializer = NULL_TREE;
22603 /* See if we are probably looking at a function
22604 definition. We are certainly not looking at a
22605 member-declarator. Calling `grokfield' has
22606 side-effects, so we must not do it unless we are sure
22607 that we are looking at a member-declarator. */
22608 if (cp_parser_token_starts_function_definition_p
22609 (cp_lexer_peek_token (parser->lexer)))
22611 /* The grammar does not allow a pure-specifier to be
22612 used when a member function is defined. (It is
22613 possible that this fact is an oversight in the
22614 standard, since a pure function may be defined
22615 outside of the class-specifier. */
22616 if (initializer && initializer_token_start)
22617 error_at (initializer_token_start->location,
22618 "pure-specifier on function-definition");
22619 decl = cp_parser_save_member_function_body (parser,
22623 if (parser->fully_implicit_function_template_p)
22624 decl = finish_fully_implicit_template (parser, decl);
22625 /* If the member was not a friend, declare it here. */
22627 finish_member_declaration (decl);
22628 /* Peek at the next token. */
22629 token = cp_lexer_peek_token (parser->lexer);
22630 /* If the next token is a semicolon, consume it. */
22631 if (token->type == CPP_SEMICOLON)
22632 cp_lexer_consume_token (parser->lexer);
22636 if (declarator->kind == cdk_function)
22637 declarator->id_loc = token->location;
22638 /* Create the declaration. */
22639 decl = grokfield (declarator, &decl_specifiers,
22640 initializer, /*init_const_expr_p=*/true,
22641 asm_specification, attributes);
22642 if (parser->fully_implicit_function_template_p)
22645 finish_fully_implicit_template (parser, 0);
22647 decl = finish_fully_implicit_template (parser, decl);
22651 cp_finalize_omp_declare_simd (parser, decl);
22652 cp_finalize_oacc_routine (parser, decl, false);
22654 /* Reset PREFIX_ATTRIBUTES. */
22655 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22656 attributes = TREE_CHAIN (attributes);
22658 TREE_CHAIN (attributes) = NULL_TREE;
22660 /* If there is any qualification still in effect, clear it
22661 now; we will be starting fresh with the next declarator. */
22662 parser->scope = NULL_TREE;
22663 parser->qualifying_scope = NULL_TREE;
22664 parser->object_scope = NULL_TREE;
22665 /* If it's a `,', then there are more declarators. */
22666 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22668 cp_lexer_consume_token (parser->lexer);
22669 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22671 cp_token *token = cp_lexer_previous_token (parser->lexer);
22672 error_at (token->location,
22673 "stray %<,%> at end of member declaration");
22676 /* If the next token isn't a `;', then we have a parse error. */
22677 else if (cp_lexer_next_token_is_not (parser->lexer,
22680 /* The next token might be a ways away from where the
22681 actual semicolon is missing. Find the previous token
22682 and use that for our error position. */
22683 cp_token *token = cp_lexer_previous_token (parser->lexer);
22684 error_at (token->location,
22685 "expected %<;%> at end of member declaration");
22687 /* Assume that the user meant to provide a semicolon. If
22688 we were to cp_parser_skip_to_end_of_statement, we might
22689 skip to a semicolon inside a member function definition
22690 and issue nonsensical error messages. */
22691 assume_semicolon = true;
22696 /* Add DECL to the list of members. */
22698 /* Explicitly include, eg, NSDMIs, for better error
22699 recovery (c++/58650). */
22700 || !DECL_DECLARES_FUNCTION_P (decl))
22701 finish_member_declaration (decl);
22703 if (TREE_CODE (decl) == FUNCTION_DECL)
22704 cp_parser_save_default_args (parser, decl);
22705 else if (TREE_CODE (decl) == FIELD_DECL
22706 && !DECL_C_BIT_FIELD (decl)
22707 && DECL_INITIAL (decl))
22708 /* Add DECL to the queue of NSDMI to be parsed later. */
22709 vec_safe_push (unparsed_nsdmis, decl);
22712 if (assume_semicolon)
22717 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22719 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22722 /* Parse a pure-specifier.
22727 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22728 Otherwise, ERROR_MARK_NODE is returned. */
22731 cp_parser_pure_specifier (cp_parser* parser)
22735 /* Look for the `=' token. */
22736 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22737 return error_mark_node;
22738 /* Look for the `0' token. */
22739 token = cp_lexer_peek_token (parser->lexer);
22741 if (token->type == CPP_EOF
22742 || token->type == CPP_PRAGMA_EOL)
22743 return error_mark_node;
22745 cp_lexer_consume_token (parser->lexer);
22747 /* Accept = default or = delete in c++0x mode. */
22748 if (token->keyword == RID_DEFAULT
22749 || token->keyword == RID_DELETE)
22751 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22752 return token->u.value;
22755 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22756 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22758 cp_parser_error (parser,
22759 "invalid pure specifier (only %<= 0%> is allowed)");
22760 cp_parser_skip_to_end_of_statement (parser);
22761 return error_mark_node;
22763 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22765 error_at (token->location, "templates may not be %<virtual%>");
22766 return error_mark_node;
22769 return integer_zero_node;
22772 /* Parse a constant-initializer.
22774 constant-initializer:
22775 = constant-expression
22777 Returns a representation of the constant-expression. */
22780 cp_parser_constant_initializer (cp_parser* parser)
22782 /* Look for the `=' token. */
22783 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22784 return error_mark_node;
22786 /* It is invalid to write:
22788 struct S { static const int i = { 7 }; };
22791 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22793 cp_parser_error (parser,
22794 "a brace-enclosed initializer is not allowed here");
22795 /* Consume the opening brace. */
22796 cp_lexer_consume_token (parser->lexer);
22797 /* Skip the initializer. */
22798 cp_parser_skip_to_closing_brace (parser);
22799 /* Look for the trailing `}'. */
22800 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22802 return error_mark_node;
22805 return cp_parser_constant_expression (parser);
22808 /* Derived classes [gram.class.derived] */
22810 /* Parse a base-clause.
22813 : base-specifier-list
22815 base-specifier-list:
22816 base-specifier ... [opt]
22817 base-specifier-list , base-specifier ... [opt]
22819 Returns a TREE_LIST representing the base-classes, in the order in
22820 which they were declared. The representation of each node is as
22821 described by cp_parser_base_specifier.
22823 In the case that no bases are specified, this function will return
22824 NULL_TREE, not ERROR_MARK_NODE. */
22827 cp_parser_base_clause (cp_parser* parser)
22829 tree bases = NULL_TREE;
22831 /* Look for the `:' that begins the list. */
22832 cp_parser_require (parser, CPP_COLON, RT_COLON);
22834 /* Scan the base-specifier-list. */
22839 bool pack_expansion_p = false;
22841 /* Look for the base-specifier. */
22842 base = cp_parser_base_specifier (parser);
22843 /* Look for the (optional) ellipsis. */
22844 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22846 /* Consume the `...'. */
22847 cp_lexer_consume_token (parser->lexer);
22849 pack_expansion_p = true;
22852 /* Add BASE to the front of the list. */
22853 if (base && base != error_mark_node)
22855 if (pack_expansion_p)
22856 /* Make this a pack expansion type. */
22857 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22859 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22861 TREE_CHAIN (base) = bases;
22865 /* Peek at the next token. */
22866 token = cp_lexer_peek_token (parser->lexer);
22867 /* If it's not a comma, then the list is complete. */
22868 if (token->type != CPP_COMMA)
22870 /* Consume the `,'. */
22871 cp_lexer_consume_token (parser->lexer);
22874 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22875 base class had a qualified name. However, the next name that
22876 appears is certainly not qualified. */
22877 parser->scope = NULL_TREE;
22878 parser->qualifying_scope = NULL_TREE;
22879 parser->object_scope = NULL_TREE;
22881 return nreverse (bases);
22884 /* Parse a base-specifier.
22887 :: [opt] nested-name-specifier [opt] class-name
22888 virtual access-specifier [opt] :: [opt] nested-name-specifier
22890 access-specifier virtual [opt] :: [opt] nested-name-specifier
22893 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22894 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22895 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22896 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22899 cp_parser_base_specifier (cp_parser* parser)
22903 bool virtual_p = false;
22904 bool duplicate_virtual_error_issued_p = false;
22905 bool duplicate_access_error_issued_p = false;
22906 bool class_scope_p, template_p;
22907 tree access = access_default_node;
22910 /* Process the optional `virtual' and `access-specifier'. */
22913 /* Peek at the next token. */
22914 token = cp_lexer_peek_token (parser->lexer);
22915 /* Process `virtual'. */
22916 switch (token->keyword)
22919 /* If `virtual' appears more than once, issue an error. */
22920 if (virtual_p && !duplicate_virtual_error_issued_p)
22922 cp_parser_error (parser,
22923 "%<virtual%> specified more than once in base-specified");
22924 duplicate_virtual_error_issued_p = true;
22929 /* Consume the `virtual' token. */
22930 cp_lexer_consume_token (parser->lexer);
22935 case RID_PROTECTED:
22937 /* If more than one access specifier appears, issue an
22939 if (access != access_default_node
22940 && !duplicate_access_error_issued_p)
22942 cp_parser_error (parser,
22943 "more than one access specifier in base-specified");
22944 duplicate_access_error_issued_p = true;
22947 access = ridpointers[(int) token->keyword];
22949 /* Consume the access-specifier. */
22950 cp_lexer_consume_token (parser->lexer);
22959 /* It is not uncommon to see programs mechanically, erroneously, use
22960 the 'typename' keyword to denote (dependent) qualified types
22961 as base classes. */
22962 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22964 token = cp_lexer_peek_token (parser->lexer);
22965 if (!processing_template_decl)
22966 error_at (token->location,
22967 "keyword %<typename%> not allowed outside of templates");
22969 error_at (token->location,
22970 "keyword %<typename%> not allowed in this context "
22971 "(the base class is implicitly a type)");
22972 cp_lexer_consume_token (parser->lexer);
22975 /* Look for the optional `::' operator. */
22976 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22977 /* Look for the nested-name-specifier. The simplest way to
22982 The keyword `typename' is not permitted in a base-specifier or
22983 mem-initializer; in these contexts a qualified name that
22984 depends on a template-parameter is implicitly assumed to be a
22987 is to pretend that we have seen the `typename' keyword at this
22989 cp_parser_nested_name_specifier_opt (parser,
22990 /*typename_keyword_p=*/true,
22991 /*check_dependency_p=*/true,
22993 /*is_declaration=*/true);
22994 /* If the base class is given by a qualified name, assume that names
22995 we see are type names or templates, as appropriate. */
22996 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22997 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23000 && cp_lexer_next_token_is_decltype (parser->lexer))
23001 /* DR 950 allows decltype as a base-specifier. */
23002 type = cp_parser_decltype (parser);
23005 /* Otherwise, look for the class-name. */
23006 type = cp_parser_class_name (parser,
23010 /*check_dependency_p=*/true,
23011 /*class_head_p=*/false,
23012 /*is_declaration=*/true);
23013 type = TREE_TYPE (type);
23016 if (type == error_mark_node)
23017 return error_mark_node;
23019 return finish_base_specifier (type, access, virtual_p);
23022 /* Exception handling [gram.exception] */
23024 /* Parse an (optional) noexcept-specification.
23026 noexcept-specification:
23027 noexcept ( constant-expression ) [opt]
23029 If no noexcept-specification is present, returns NULL_TREE.
23030 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23031 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23032 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23033 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23034 in which case a boolean condition is returned instead. */
23037 cp_parser_noexcept_specification_opt (cp_parser* parser,
23038 bool require_constexpr,
23039 bool* consumed_expr,
23043 const char *saved_message;
23045 /* Peek at the next token. */
23046 token = cp_lexer_peek_token (parser->lexer);
23048 /* Is it a noexcept-specification? */
23049 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23052 cp_lexer_consume_token (parser->lexer);
23054 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23056 cp_lexer_consume_token (parser->lexer);
23058 if (require_constexpr)
23060 /* Types may not be defined in an exception-specification. */
23061 saved_message = parser->type_definition_forbidden_message;
23062 parser->type_definition_forbidden_message
23063 = G_("types may not be defined in an exception-specification");
23065 expr = cp_parser_constant_expression (parser);
23067 /* Restore the saved message. */
23068 parser->type_definition_forbidden_message = saved_message;
23072 expr = cp_parser_expression (parser);
23073 *consumed_expr = true;
23076 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23080 expr = boolean_true_node;
23081 if (!require_constexpr)
23082 *consumed_expr = false;
23085 /* We cannot build a noexcept-spec right away because this will check
23086 that expr is a constexpr. */
23088 return build_noexcept_spec (expr, tf_warning_or_error);
23096 /* Parse an (optional) exception-specification.
23098 exception-specification:
23099 throw ( type-id-list [opt] )
23101 Returns a TREE_LIST representing the exception-specification. The
23102 TREE_VALUE of each node is a type. */
23105 cp_parser_exception_specification_opt (cp_parser* parser)
23109 const char *saved_message;
23111 /* Peek at the next token. */
23112 token = cp_lexer_peek_token (parser->lexer);
23114 /* Is it a noexcept-specification? */
23115 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23117 if (type_id_list != NULL_TREE)
23118 return type_id_list;
23120 /* If it's not `throw', then there's no exception-specification. */
23121 if (!cp_parser_is_keyword (token, RID_THROW))
23125 /* Enable this once a lot of code has transitioned to noexcept? */
23126 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23127 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23128 "deprecated in C++0x; use %<noexcept%> instead");
23131 /* Consume the `throw'. */
23132 cp_lexer_consume_token (parser->lexer);
23134 /* Look for the `('. */
23135 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23137 /* Peek at the next token. */
23138 token = cp_lexer_peek_token (parser->lexer);
23139 /* If it's not a `)', then there is a type-id-list. */
23140 if (token->type != CPP_CLOSE_PAREN)
23142 /* Types may not be defined in an exception-specification. */
23143 saved_message = parser->type_definition_forbidden_message;
23144 parser->type_definition_forbidden_message
23145 = G_("types may not be defined in an exception-specification");
23146 /* Parse the type-id-list. */
23147 type_id_list = cp_parser_type_id_list (parser);
23148 /* Restore the saved message. */
23149 parser->type_definition_forbidden_message = saved_message;
23152 type_id_list = empty_except_spec;
23154 /* Look for the `)'. */
23155 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23157 return type_id_list;
23160 /* Parse an (optional) type-id-list.
23164 type-id-list , type-id ... [opt]
23166 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23167 in the order that the types were presented. */
23170 cp_parser_type_id_list (cp_parser* parser)
23172 tree types = NULL_TREE;
23179 token = cp_lexer_peek_token (parser->lexer);
23181 /* Get the next type-id. */
23182 type = cp_parser_type_id (parser);
23183 /* Check for invalid 'auto'. */
23184 if (flag_concepts && type_uses_auto (type))
23186 error_at (token->location,
23187 "invalid use of %<auto%> in exception-specification");
23188 type = error_mark_node;
23190 /* Parse the optional ellipsis. */
23191 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23193 /* Consume the `...'. */
23194 cp_lexer_consume_token (parser->lexer);
23196 /* Turn the type into a pack expansion expression. */
23197 type = make_pack_expansion (type);
23199 /* Add it to the list. */
23200 types = add_exception_specifier (types, type, /*complain=*/1);
23201 /* Peek at the next token. */
23202 token = cp_lexer_peek_token (parser->lexer);
23203 /* If it is not a `,', we are done. */
23204 if (token->type != CPP_COMMA)
23206 /* Consume the `,'. */
23207 cp_lexer_consume_token (parser->lexer);
23210 return nreverse (types);
23213 /* Parse a try-block.
23216 try compound-statement handler-seq */
23219 cp_parser_try_block (cp_parser* parser)
23223 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23224 if (parser->in_function_body
23225 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23226 error ("%<try%> in %<constexpr%> function");
23228 try_block = begin_try_block ();
23229 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23230 finish_try_block (try_block);
23231 cp_parser_handler_seq (parser);
23232 finish_handler_sequence (try_block);
23237 /* Parse a function-try-block.
23239 function-try-block:
23240 try ctor-initializer [opt] function-body handler-seq */
23243 cp_parser_function_try_block (cp_parser* parser)
23245 tree compound_stmt;
23247 bool ctor_initializer_p;
23249 /* Look for the `try' keyword. */
23250 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23252 /* Let the rest of the front end know where we are. */
23253 try_block = begin_function_try_block (&compound_stmt);
23254 /* Parse the function-body. */
23255 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23256 (parser, /*in_function_try_block=*/true);
23257 /* We're done with the `try' part. */
23258 finish_function_try_block (try_block);
23259 /* Parse the handlers. */
23260 cp_parser_handler_seq (parser);
23261 /* We're done with the handlers. */
23262 finish_function_handler_sequence (try_block, compound_stmt);
23264 return ctor_initializer_p;
23267 /* Parse a handler-seq.
23270 handler handler-seq [opt] */
23273 cp_parser_handler_seq (cp_parser* parser)
23279 /* Parse the handler. */
23280 cp_parser_handler (parser);
23281 /* Peek at the next token. */
23282 token = cp_lexer_peek_token (parser->lexer);
23283 /* If it's not `catch' then there are no more handlers. */
23284 if (!cp_parser_is_keyword (token, RID_CATCH))
23289 /* Parse a handler.
23292 catch ( exception-declaration ) compound-statement */
23295 cp_parser_handler (cp_parser* parser)
23300 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23301 handler = begin_handler ();
23302 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23303 declaration = cp_parser_exception_declaration (parser);
23304 finish_handler_parms (declaration, handler);
23305 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23306 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23307 finish_handler (handler);
23310 /* Parse an exception-declaration.
23312 exception-declaration:
23313 type-specifier-seq declarator
23314 type-specifier-seq abstract-declarator
23318 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23319 ellipsis variant is used. */
23322 cp_parser_exception_declaration (cp_parser* parser)
23324 cp_decl_specifier_seq type_specifiers;
23325 cp_declarator *declarator;
23326 const char *saved_message;
23328 /* If it's an ellipsis, it's easy to handle. */
23329 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23331 /* Consume the `...' token. */
23332 cp_lexer_consume_token (parser->lexer);
23336 /* Types may not be defined in exception-declarations. */
23337 saved_message = parser->type_definition_forbidden_message;
23338 parser->type_definition_forbidden_message
23339 = G_("types may not be defined in exception-declarations");
23341 /* Parse the type-specifier-seq. */
23342 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23343 /*is_trailing_return=*/false,
23345 /* If it's a `)', then there is no declarator. */
23346 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23349 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23350 /*ctor_dtor_or_conv_p=*/NULL,
23351 /*parenthesized_p=*/NULL,
23352 /*member_p=*/false,
23353 /*friend_p=*/false);
23355 /* Restore the saved message. */
23356 parser->type_definition_forbidden_message = saved_message;
23358 if (!type_specifiers.any_specifiers_p)
23359 return error_mark_node;
23361 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23364 /* Parse a throw-expression.
23367 throw assignment-expression [opt]
23369 Returns a THROW_EXPR representing the throw-expression. */
23372 cp_parser_throw_expression (cp_parser* parser)
23377 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23378 token = cp_lexer_peek_token (parser->lexer);
23379 /* Figure out whether or not there is an assignment-expression
23380 following the "throw" keyword. */
23381 if (token->type == CPP_COMMA
23382 || token->type == CPP_SEMICOLON
23383 || token->type == CPP_CLOSE_PAREN
23384 || token->type == CPP_CLOSE_SQUARE
23385 || token->type == CPP_CLOSE_BRACE
23386 || token->type == CPP_COLON)
23387 expression = NULL_TREE;
23389 expression = cp_parser_assignment_expression (parser);
23391 return build_throw (expression);
23394 /* GNU Extensions */
23396 /* Parse an (optional) asm-specification.
23399 asm ( string-literal )
23401 If the asm-specification is present, returns a STRING_CST
23402 corresponding to the string-literal. Otherwise, returns
23406 cp_parser_asm_specification_opt (cp_parser* parser)
23409 tree asm_specification;
23411 /* Peek at the next token. */
23412 token = cp_lexer_peek_token (parser->lexer);
23413 /* If the next token isn't the `asm' keyword, then there's no
23414 asm-specification. */
23415 if (!cp_parser_is_keyword (token, RID_ASM))
23418 /* Consume the `asm' token. */
23419 cp_lexer_consume_token (parser->lexer);
23420 /* Look for the `('. */
23421 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23423 /* Look for the string-literal. */
23424 asm_specification = cp_parser_string_literal (parser, false, false);
23426 /* Look for the `)'. */
23427 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23429 return asm_specification;
23432 /* Parse an asm-operand-list.
23436 asm-operand-list , asm-operand
23439 string-literal ( expression )
23440 [ string-literal ] string-literal ( expression )
23442 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23443 each node is the expression. The TREE_PURPOSE is itself a
23444 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23445 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23446 is a STRING_CST for the string literal before the parenthesis. Returns
23447 ERROR_MARK_NODE if any of the operands are invalid. */
23450 cp_parser_asm_operand_list (cp_parser* parser)
23452 tree asm_operands = NULL_TREE;
23453 bool invalid_operands = false;
23457 tree string_literal;
23461 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23463 /* Consume the `[' token. */
23464 cp_lexer_consume_token (parser->lexer);
23465 /* Read the operand name. */
23466 name = cp_parser_identifier (parser);
23467 if (name != error_mark_node)
23468 name = build_string (IDENTIFIER_LENGTH (name),
23469 IDENTIFIER_POINTER (name));
23470 /* Look for the closing `]'. */
23471 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23475 /* Look for the string-literal. */
23476 string_literal = cp_parser_string_literal (parser, false, false);
23478 /* Look for the `('. */
23479 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23480 /* Parse the expression. */
23481 expression = cp_parser_expression (parser);
23482 /* Look for the `)'. */
23483 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23485 if (name == error_mark_node
23486 || string_literal == error_mark_node
23487 || expression == error_mark_node)
23488 invalid_operands = true;
23490 /* Add this operand to the list. */
23491 asm_operands = tree_cons (build_tree_list (name, string_literal),
23494 /* If the next token is not a `,', there are no more
23496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23498 /* Consume the `,'. */
23499 cp_lexer_consume_token (parser->lexer);
23502 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23505 /* Parse an asm-clobber-list.
23509 asm-clobber-list , string-literal
23511 Returns a TREE_LIST, indicating the clobbers in the order that they
23512 appeared. The TREE_VALUE of each node is a STRING_CST. */
23515 cp_parser_asm_clobber_list (cp_parser* parser)
23517 tree clobbers = NULL_TREE;
23521 tree string_literal;
23523 /* Look for the string literal. */
23524 string_literal = cp_parser_string_literal (parser, false, false);
23525 /* Add it to the list. */
23526 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23527 /* If the next token is not a `,', then the list is
23529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23531 /* Consume the `,' token. */
23532 cp_lexer_consume_token (parser->lexer);
23538 /* Parse an asm-label-list.
23542 asm-label-list , identifier
23544 Returns a TREE_LIST, indicating the labels in the order that they
23545 appeared. The TREE_VALUE of each node is a label. */
23548 cp_parser_asm_label_list (cp_parser* parser)
23550 tree labels = NULL_TREE;
23554 tree identifier, label, name;
23556 /* Look for the identifier. */
23557 identifier = cp_parser_identifier (parser);
23558 if (!error_operand_p (identifier))
23560 label = lookup_label (identifier);
23561 if (TREE_CODE (label) == LABEL_DECL)
23563 TREE_USED (label) = 1;
23564 check_goto (label);
23565 name = build_string (IDENTIFIER_LENGTH (identifier),
23566 IDENTIFIER_POINTER (identifier));
23567 labels = tree_cons (name, label, labels);
23570 /* If the next token is not a `,', then the list is
23572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23574 /* Consume the `,' token. */
23575 cp_lexer_consume_token (parser->lexer);
23578 return nreverse (labels);
23581 /* Return TRUE iff the next tokens in the stream are possibly the
23582 beginning of a GNU extension attribute. */
23585 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23587 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23590 /* Return TRUE iff the next tokens in the stream are possibly the
23591 beginning of a standard C++-11 attribute specifier. */
23594 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23596 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23599 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23600 beginning of a standard C++-11 attribute specifier. */
23603 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23605 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23607 return (cxx_dialect >= cxx11
23608 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23609 || (token->type == CPP_OPEN_SQUARE
23610 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23611 && token->type == CPP_OPEN_SQUARE)));
23614 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23615 beginning of a GNU extension attribute. */
23618 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23620 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23622 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23625 /* Return true iff the next tokens can be the beginning of either a
23626 GNU attribute list, or a standard C++11 attribute sequence. */
23629 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23631 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23632 || cp_next_tokens_can_be_std_attribute_p (parser));
23635 /* Return true iff the next Nth tokens can be the beginning of either
23636 a GNU attribute list, or a standard C++11 attribute sequence. */
23639 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23641 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23642 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23645 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23646 of GNU attributes, or return NULL. */
23649 cp_parser_attributes_opt (cp_parser *parser)
23651 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23652 return cp_parser_gnu_attributes_opt (parser);
23653 return cp_parser_std_attribute_spec_seq (parser);
23656 #define CILK_SIMD_FN_CLAUSE_MASK \
23657 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23658 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23659 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23660 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23661 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23663 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23664 vector [(<clauses>)] */
23667 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23669 bool first_p = parser->cilk_simd_fn_info == NULL;
23670 cp_token *token = v_token;
23673 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23674 parser->cilk_simd_fn_info->error_seen = false;
23675 parser->cilk_simd_fn_info->fndecl_seen = false;
23676 parser->cilk_simd_fn_info->tokens = vNULL;
23678 int paren_scope = 0;
23679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23681 cp_lexer_consume_token (parser->lexer);
23682 v_token = cp_lexer_peek_token (parser->lexer);
23685 while (paren_scope > 0)
23687 token = cp_lexer_peek_token (parser->lexer);
23688 if (token->type == CPP_OPEN_PAREN)
23690 else if (token->type == CPP_CLOSE_PAREN)
23692 /* Do not push the last ')' */
23693 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23694 cp_lexer_consume_token (parser->lexer);
23697 token->type = CPP_PRAGMA_EOL;
23698 parser->lexer->next_token = token;
23699 cp_lexer_consume_token (parser->lexer);
23701 struct cp_token_cache *cp
23702 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23703 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23706 /* Parse an (optional) series of attributes.
23709 attributes attribute
23712 __attribute__ (( attribute-list [opt] ))
23714 The return value is as for cp_parser_gnu_attribute_list. */
23717 cp_parser_gnu_attributes_opt (cp_parser* parser)
23719 tree attributes = NULL_TREE;
23724 tree attribute_list;
23727 /* Peek at the next token. */
23728 token = cp_lexer_peek_token (parser->lexer);
23729 /* If it's not `__attribute__', then we're done. */
23730 if (token->keyword != RID_ATTRIBUTE)
23733 /* Consume the `__attribute__' keyword. */
23734 cp_lexer_consume_token (parser->lexer);
23735 /* Look for the two `(' tokens. */
23736 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23737 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23739 /* Peek at the next token. */
23740 token = cp_lexer_peek_token (parser->lexer);
23741 if (token->type != CPP_CLOSE_PAREN)
23742 /* Parse the attribute-list. */
23743 attribute_list = cp_parser_gnu_attribute_list (parser);
23745 /* If the next token is a `)', then there is no attribute
23747 attribute_list = NULL;
23749 /* Look for the two `)' tokens. */
23750 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23752 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23755 cp_parser_skip_to_end_of_statement (parser);
23757 /* Add these new attributes to the list. */
23758 attributes = chainon (attributes, attribute_list);
23764 /* Parse a GNU attribute-list.
23768 attribute-list , attribute
23772 identifier ( identifier )
23773 identifier ( identifier , expression-list )
23774 identifier ( expression-list )
23776 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23777 to an attribute. The TREE_PURPOSE of each node is the identifier
23778 indicating which attribute is in use. The TREE_VALUE represents
23779 the arguments, if any. */
23782 cp_parser_gnu_attribute_list (cp_parser* parser)
23784 tree attribute_list = NULL_TREE;
23785 bool save_translate_strings_p = parser->translate_strings_p;
23787 parser->translate_strings_p = false;
23794 /* Look for the identifier. We also allow keywords here; for
23795 example `__attribute__ ((const))' is legal. */
23796 token = cp_lexer_peek_token (parser->lexer);
23797 if (token->type == CPP_NAME
23798 || token->type == CPP_KEYWORD)
23800 tree arguments = NULL_TREE;
23802 /* Consume the token, but save it since we need it for the
23803 SIMD enabled function parsing. */
23804 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23806 /* Save away the identifier that indicates which attribute
23808 identifier = (token->type == CPP_KEYWORD)
23809 /* For keywords, use the canonical spelling, not the
23810 parsed identifier. */
23811 ? ridpointers[(int) token->keyword]
23812 : id_token->u.value;
23814 attribute = build_tree_list (identifier, NULL_TREE);
23816 /* Peek at the next token. */
23817 token = cp_lexer_peek_token (parser->lexer);
23818 /* If it's an `(', then parse the attribute arguments. */
23819 if (token->type == CPP_OPEN_PAREN)
23821 vec<tree, va_gc> *vec;
23822 int attr_flag = (attribute_takes_identifier_p (identifier)
23823 ? id_attr : normal_attr);
23824 if (is_cilkplus_vector_p (identifier))
23826 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23830 vec = cp_parser_parenthesized_expression_list
23831 (parser, attr_flag, /*cast_p=*/false,
23832 /*allow_expansion_p=*/false,
23833 /*non_constant_p=*/NULL);
23835 arguments = error_mark_node;
23838 arguments = build_tree_list_vec (vec);
23839 release_tree_vector (vec);
23841 /* Save the arguments away. */
23842 TREE_VALUE (attribute) = arguments;
23844 else if (is_cilkplus_vector_p (identifier))
23846 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23850 if (arguments != error_mark_node)
23852 /* Add this attribute to the list. */
23853 TREE_CHAIN (attribute) = attribute_list;
23854 attribute_list = attribute;
23857 token = cp_lexer_peek_token (parser->lexer);
23859 /* Now, look for more attributes. If the next token isn't a
23860 `,', we're done. */
23861 if (token->type != CPP_COMMA)
23864 /* Consume the comma and keep going. */
23865 cp_lexer_consume_token (parser->lexer);
23867 parser->translate_strings_p = save_translate_strings_p;
23869 /* We built up the list in reverse order. */
23870 return nreverse (attribute_list);
23873 /* Parse a standard C++11 attribute.
23875 The returned representation is a TREE_LIST which TREE_PURPOSE is
23876 the scoped name of the attribute, and the TREE_VALUE is its
23879 Note that the scoped name of the attribute is itself a TREE_LIST
23880 which TREE_PURPOSE is the namespace of the attribute, and
23881 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23882 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23883 and which TREE_PURPOSE is directly the attribute name.
23885 Clients of the attribute code should use get_attribute_namespace
23886 and get_attribute_name to get the actual namespace and name of
23887 attributes, regardless of their being GNU or C++11 attributes.
23890 attribute-token attribute-argument-clause [opt]
23894 attribute-scoped-token
23896 attribute-scoped-token:
23897 attribute-namespace :: identifier
23899 attribute-namespace:
23902 attribute-argument-clause:
23903 ( balanced-token-seq )
23905 balanced-token-seq:
23906 balanced-token [opt]
23907 balanced-token-seq balanced-token
23910 ( balanced-token-seq )
23911 [ balanced-token-seq ]
23912 { balanced-token-seq }. */
23915 cp_parser_std_attribute (cp_parser *parser)
23917 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23920 /* First, parse name of the attribute, a.k.a attribute-token. */
23922 token = cp_lexer_peek_token (parser->lexer);
23923 if (token->type == CPP_NAME)
23924 attr_id = token->u.value;
23925 else if (token->type == CPP_KEYWORD)
23926 attr_id = ridpointers[(int) token->keyword];
23927 else if (token->flags & NAMED_OP)
23928 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23930 if (attr_id == NULL_TREE)
23933 cp_lexer_consume_token (parser->lexer);
23935 token = cp_lexer_peek_token (parser->lexer);
23936 if (token->type == CPP_SCOPE)
23938 /* We are seeing a scoped attribute token. */
23940 cp_lexer_consume_token (parser->lexer);
23943 token = cp_lexer_consume_token (parser->lexer);
23944 if (token->type == CPP_NAME)
23945 attr_id = token->u.value;
23946 else if (token->type == CPP_KEYWORD)
23947 attr_id = ridpointers[(int) token->keyword];
23950 error_at (token->location,
23951 "expected an identifier for the attribute name");
23952 return error_mark_node;
23954 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23956 token = cp_lexer_peek_token (parser->lexer);
23960 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23962 /* C++11 noreturn attribute is equivalent to GNU's. */
23963 if (is_attribute_p ("noreturn", attr_id))
23964 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23965 /* C++14 deprecated attribute is equivalent to GNU's. */
23966 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23968 if (cxx_dialect == cxx11)
23969 pedwarn (token->location, OPT_Wpedantic,
23970 "%<deprecated%> is a C++14 feature;"
23971 " use %<gnu::deprecated%>");
23972 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23974 /* Transactional Memory TS optimize_for_synchronized attribute is
23975 equivalent to GNU transaction_callable. */
23976 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23977 TREE_PURPOSE (attribute)
23978 = get_identifier ("transaction_callable");
23979 /* Transactional Memory attributes are GNU attributes. */
23980 else if (tm_attr_to_mask (attr_id))
23981 TREE_PURPOSE (attribute) = attr_id;
23984 /* Now parse the optional argument clause of the attribute. */
23986 if (token->type != CPP_OPEN_PAREN)
23990 vec<tree, va_gc> *vec;
23991 int attr_flag = normal_attr;
23993 if (attr_ns == get_identifier ("gnu")
23994 && attribute_takes_identifier_p (attr_id))
23995 /* A GNU attribute that takes an identifier in parameter. */
23996 attr_flag = id_attr;
23998 vec = cp_parser_parenthesized_expression_list
23999 (parser, attr_flag, /*cast_p=*/false,
24000 /*allow_expansion_p=*/true,
24001 /*non_constant_p=*/NULL);
24003 arguments = error_mark_node;
24006 arguments = build_tree_list_vec (vec);
24007 release_tree_vector (vec);
24010 if (arguments == error_mark_node)
24011 attribute = error_mark_node;
24013 TREE_VALUE (attribute) = arguments;
24019 /* Check that the attribute ATTRIBUTE appears at most once in the
24020 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24021 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24022 isn't implemented yet in GCC. */
24025 cp_parser_check_std_attribute (tree attributes, tree attribute)
24029 tree name = get_attribute_name (attribute);
24030 if (is_attribute_p ("noreturn", name)
24031 && lookup_attribute ("noreturn", attributes))
24032 error ("attribute noreturn can appear at most once "
24033 "in an attribute-list");
24034 else if (is_attribute_p ("deprecated", name)
24035 && lookup_attribute ("deprecated", attributes))
24036 error ("attribute deprecated can appear at most once "
24037 "in an attribute-list");
24041 /* Parse a list of standard C++-11 attributes.
24045 attribute-list , attribute[opt]
24047 attribute-list , attribute ...
24051 cp_parser_std_attribute_list (cp_parser *parser)
24053 tree attributes = NULL_TREE, attribute = NULL_TREE;
24054 cp_token *token = NULL;
24058 attribute = cp_parser_std_attribute (parser);
24059 if (attribute == error_mark_node)
24061 if (attribute != NULL_TREE)
24063 cp_parser_check_std_attribute (attributes, attribute);
24064 TREE_CHAIN (attribute) = attributes;
24065 attributes = attribute;
24067 token = cp_lexer_peek_token (parser->lexer);
24068 if (token->type == CPP_ELLIPSIS)
24070 cp_lexer_consume_token (parser->lexer);
24071 if (attribute == NULL_TREE)
24072 error_at (token->location,
24073 "expected attribute before %<...%>");
24075 TREE_VALUE (attribute)
24076 = make_pack_expansion (TREE_VALUE (attribute));
24077 token = cp_lexer_peek_token (parser->lexer);
24079 if (token->type != CPP_COMMA)
24081 cp_lexer_consume_token (parser->lexer);
24083 attributes = nreverse (attributes);
24087 /* Parse a standard C++-11 attribute specifier.
24089 attribute-specifier:
24090 [ [ attribute-list ] ]
24091 alignment-specifier
24093 alignment-specifier:
24094 alignas ( type-id ... [opt] )
24095 alignas ( alignment-expression ... [opt] ). */
24098 cp_parser_std_attribute_spec (cp_parser *parser)
24100 tree attributes = NULL_TREE;
24101 cp_token *token = cp_lexer_peek_token (parser->lexer);
24103 if (token->type == CPP_OPEN_SQUARE
24104 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24106 cp_lexer_consume_token (parser->lexer);
24107 cp_lexer_consume_token (parser->lexer);
24109 attributes = cp_parser_std_attribute_list (parser);
24111 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24112 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24113 cp_parser_skip_to_end_of_statement (parser);
24115 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24116 when we are sure that we have actually parsed them. */
24117 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24123 /* Look for an alignment-specifier. */
24125 token = cp_lexer_peek_token (parser->lexer);
24127 if (token->type != CPP_KEYWORD
24128 || token->keyword != RID_ALIGNAS)
24131 cp_lexer_consume_token (parser->lexer);
24132 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24134 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24136 cp_parser_error (parser, "expected %<(%>");
24137 return error_mark_node;
24140 cp_parser_parse_tentatively (parser);
24141 alignas_expr = cp_parser_type_id (parser);
24143 if (!cp_parser_parse_definitely (parser))
24145 alignas_expr = cp_parser_assignment_expression (parser);
24146 if (alignas_expr == error_mark_node)
24147 cp_parser_skip_to_end_of_statement (parser);
24148 if (alignas_expr == NULL_TREE
24149 || alignas_expr == error_mark_node)
24150 return alignas_expr;
24153 alignas_expr = cxx_alignas_expr (alignas_expr);
24154 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24156 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24158 cp_lexer_consume_token (parser->lexer);
24159 alignas_expr = make_pack_expansion (alignas_expr);
24162 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24164 cp_parser_error (parser, "expected %<)%>");
24165 return error_mark_node;
24168 /* Build the C++-11 representation of an 'aligned'
24171 build_tree_list (build_tree_list (get_identifier ("gnu"),
24172 get_identifier ("aligned")),
24179 /* Parse a standard C++-11 attribute-specifier-seq.
24181 attribute-specifier-seq:
24182 attribute-specifier-seq [opt] attribute-specifier
24186 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24188 tree attr_specs = NULL_TREE;
24189 tree attr_last = NULL_TREE;
24193 tree attr_spec = cp_parser_std_attribute_spec (parser);
24194 if (attr_spec == NULL_TREE)
24196 if (attr_spec == error_mark_node)
24197 return error_mark_node;
24200 TREE_CHAIN (attr_last) = attr_spec;
24202 attr_specs = attr_last = attr_spec;
24203 attr_last = tree_last (attr_last);
24209 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24210 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24211 current value of the PEDANTIC flag, regardless of whether or not
24212 the `__extension__' keyword is present. The caller is responsible
24213 for restoring the value of the PEDANTIC flag. */
24216 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24218 /* Save the old value of the PEDANTIC flag. */
24219 *saved_pedantic = pedantic;
24221 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24223 /* Consume the `__extension__' token. */
24224 cp_lexer_consume_token (parser->lexer);
24225 /* We're not being pedantic while the `__extension__' keyword is
24235 /* Parse a label declaration.
24238 __label__ label-declarator-seq ;
24240 label-declarator-seq:
24241 identifier , label-declarator-seq
24245 cp_parser_label_declaration (cp_parser* parser)
24247 /* Look for the `__label__' keyword. */
24248 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24254 /* Look for an identifier. */
24255 identifier = cp_parser_identifier (parser);
24256 /* If we failed, stop. */
24257 if (identifier == error_mark_node)
24259 /* Declare it as a label. */
24260 finish_label_decl (identifier);
24261 /* If the next token is a `;', stop. */
24262 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24264 /* Look for the `,' separating the label declarations. */
24265 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24268 /* Look for the final `;'. */
24269 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24272 // -------------------------------------------------------------------------- //
24275 // Parse a requires clause.
24277 // requires-clause:
24278 // 'requires' logical-or-expression
24280 // The required logical-or-expression must be a constant expression. Note
24281 // that we don't check that the expression is constepxr here. We defer until
24282 // we analyze constraints and then, we only check atomic constraints.
24284 cp_parser_requires_clause (cp_parser *parser)
24286 // Parse the requires clause so that it is not automatically folded.
24287 ++processing_template_decl;
24288 tree expr = cp_parser_binary_expression (parser, false, false,
24289 PREC_NOT_OPERATOR, NULL);
24290 if (check_for_bare_parameter_packs (expr))
24291 expr = error_mark_node;
24292 --processing_template_decl;
24296 // Optionally parse a requires clause:
24298 cp_parser_requires_clause_opt (cp_parser *parser)
24300 cp_token *tok = cp_lexer_peek_token (parser->lexer);
24301 if (tok->keyword != RID_REQUIRES)
24303 if (!flag_concepts && tok->type == CPP_NAME
24304 && tok->u.value == ridpointers[RID_REQUIRES])
24306 error_at (cp_lexer_peek_token (parser->lexer)->location,
24307 "%<requires%> only available with -fconcepts");
24308 /* Parse and discard the requires-clause. */
24309 cp_lexer_consume_token (parser->lexer);
24310 cp_parser_requires_clause (parser);
24314 cp_lexer_consume_token (parser->lexer);
24315 return cp_parser_requires_clause (parser);
24319 /*---------------------------------------------------------------------------
24320 Requires expressions
24321 ---------------------------------------------------------------------------*/
24323 /* Parse a requires expression
24325 requirement-expression:
24326 'requires' requirement-parameter-list [opt] requirement-body */
24328 cp_parser_requires_expression (cp_parser *parser)
24330 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24331 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24333 /* A requires-expression shall appear only within a concept
24334 definition or a requires-clause.
24336 TODO: Implement this diagnostic correctly. */
24337 if (!processing_template_decl)
24339 error_at (loc, "a requires expression cannot appear outside a template");
24340 cp_parser_skip_to_end_of_statement (parser);
24341 return error_mark_node;
24346 /* Local parameters are delared as variables within the scope
24347 of the expression. They are not visible past the end of
24348 the expression. Expressions within the requires-expression
24349 are unevaluated. */
24350 struct scope_sentinel
24354 ++cp_unevaluated_operand;
24355 begin_scope (sk_block, NULL_TREE);
24360 pop_bindings_and_leave_scope ();
24361 --cp_unevaluated_operand;
24365 /* Parse the optional parameter list. */
24366 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24368 parms = cp_parser_requirement_parameter_list (parser);
24369 if (parms == error_mark_node)
24370 return error_mark_node;
24375 /* Parse the requirement body. */
24376 reqs = cp_parser_requirement_body (parser);
24377 if (reqs == error_mark_node)
24378 return error_mark_node;
24381 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24383 grokparms (parms, &parms);
24384 return finish_requires_expr (parms, reqs);
24387 /* Parse a parameterized requirement.
24389 requirement-parameter-list:
24390 '(' parameter-declaration-clause ')' */
24392 cp_parser_requirement_parameter_list (cp_parser *parser)
24394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24395 return error_mark_node;
24397 tree parms = cp_parser_parameter_declaration_clause (parser);
24399 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24400 return error_mark_node;
24405 /* Parse the body of a requirement.
24408 '{' requirement-list '}' */
24410 cp_parser_requirement_body (cp_parser *parser)
24412 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24413 return error_mark_node;
24415 tree reqs = cp_parser_requirement_list (parser);
24417 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24418 return error_mark_node;
24423 /* Parse a list of requirements.
24427 requirement-list ';' requirement[opt] */
24429 cp_parser_requirement_list (cp_parser *parser)
24431 tree result = NULL_TREE;
24434 tree req = cp_parser_requirement (parser);
24435 if (req == error_mark_node)
24436 return error_mark_node;
24438 result = tree_cons (NULL_TREE, req, result);
24440 /* If we see a semi-colon, consume it. */
24441 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24442 cp_lexer_consume_token (parser->lexer);
24444 /* Stop processing at the end of the list. */
24445 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24449 /* Reverse the order of requirements so they are analyzed in
24450 declaration order. */
24451 return nreverse (result);
24454 /* Parse a syntactic requirement or type requirement.
24458 compound-requirement
24460 nested-requirement */
24462 cp_parser_requirement (cp_parser *parser)
24464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24465 return cp_parser_compound_requirement (parser);
24466 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24467 return cp_parser_type_requirement (parser);
24468 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24469 return cp_parser_nested_requirement (parser);
24471 return cp_parser_simple_requirement (parser);
24474 /* Parse a simple requirement.
24476 simple-requirement:
24479 cp_parser_simple_requirement (cp_parser *parser)
24481 tree expr = cp_parser_expression (parser, NULL, false, false);
24482 if (!expr || expr == error_mark_node)
24483 return error_mark_node;
24485 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24486 return error_mark_node;
24488 return finish_simple_requirement (expr);
24491 /* Parse a type requirement
24494 nested-name-specifier [opt] required-type-name ';'
24496 required-type-name:
24498 'template' [opt] simple-template-id */
24500 cp_parser_type_requirement (cp_parser *parser)
24502 cp_lexer_consume_token (parser->lexer);
24504 // Save the scope before parsing name specifiers.
24505 tree saved_scope = parser->scope;
24506 tree saved_object_scope = parser->object_scope;
24507 tree saved_qualifying_scope = parser->qualifying_scope;
24508 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24509 cp_parser_nested_name_specifier_opt (parser,
24510 /*typename_keyword_p=*/true,
24511 /*check_dependency_p=*/false,
24513 /*is_declaration=*/false);
24516 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24518 cp_lexer_consume_token (parser->lexer);
24519 type = cp_parser_template_id (parser,
24520 /*template_keyword_p=*/true,
24521 /*check_dependency=*/false,
24522 /*tag_type=*/none_type,
24523 /*is_declaration=*/false);
24524 type = make_typename_type (parser->scope, type, typename_type,
24525 /*complain=*/tf_error);
24528 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24530 if (TREE_CODE (type) == TYPE_DECL)
24531 type = TREE_TYPE (type);
24533 parser->scope = saved_scope;
24534 parser->object_scope = saved_object_scope;
24535 parser->qualifying_scope = saved_qualifying_scope;
24537 if (type == error_mark_node)
24538 cp_parser_skip_to_end_of_statement (parser);
24540 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24541 return error_mark_node;
24542 if (type == error_mark_node)
24543 return error_mark_node;
24545 return finish_type_requirement (type);
24548 /* Parse a compound requirement
24550 compound-requirement:
24551 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24553 cp_parser_compound_requirement (cp_parser *parser)
24555 /* Parse an expression enclosed in '{ }'s. */
24556 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24557 return error_mark_node;
24559 tree expr = cp_parser_expression (parser, NULL, false, false);
24560 if (!expr || expr == error_mark_node)
24561 return error_mark_node;
24563 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24564 return error_mark_node;
24566 /* Parse the optional noexcept. */
24567 bool noexcept_p = false;
24568 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24570 cp_lexer_consume_token (parser->lexer);
24574 /* Parse the optional trailing return type. */
24575 tree type = NULL_TREE;
24576 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24578 cp_lexer_consume_token (parser->lexer);
24579 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24580 parser->in_result_type_constraint_p = true;
24581 type = cp_parser_trailing_type_id (parser);
24582 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24583 if (type == error_mark_node)
24584 return error_mark_node;
24587 return finish_compound_requirement (expr, type, noexcept_p);
24590 /* Parse a nested requirement. This is the same as a requires clause.
24592 nested-requirement:
24595 cp_parser_nested_requirement (cp_parser *parser)
24597 cp_lexer_consume_token (parser->lexer);
24598 tree req = cp_parser_requires_clause (parser);
24599 if (req == error_mark_node)
24600 return error_mark_node;
24601 return finish_nested_requirement (req);
24604 /* Support Functions */
24606 /* Return the appropriate prefer_type argument for lookup_name_real based on
24610 prefer_type_arg (tag_types tag_type)
24614 case none_type: return 0; // No preference.
24615 case scope_type: return 1; // Type or namespace.
24616 default: return 2; // Type only.
24620 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24621 NAME should have one of the representations used for an
24622 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24623 is returned. If PARSER->SCOPE is a dependent type, then a
24624 SCOPE_REF is returned.
24626 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24627 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24628 was formed. Abstractly, such entities should not be passed to this
24629 function, because they do not need to be looked up, but it is
24630 simpler to check for this special case here, rather than at the
24633 In cases not explicitly covered above, this function returns a
24634 DECL, OVERLOAD, or baselink representing the result of the lookup.
24635 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24638 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24639 (e.g., "struct") that was used. In that case bindings that do not
24640 refer to types are ignored.
24642 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24645 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24648 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24651 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24652 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24653 NULL_TREE otherwise. */
24656 cp_parser_lookup_name (cp_parser *parser, tree name,
24657 enum tag_types tag_type,
24660 bool check_dependency,
24661 tree *ambiguous_decls,
24662 location_t name_location)
24665 tree object_type = parser->context->object_type;
24667 /* Assume that the lookup will be unambiguous. */
24668 if (ambiguous_decls)
24669 *ambiguous_decls = NULL_TREE;
24671 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24672 no longer valid. Note that if we are parsing tentatively, and
24673 the parse fails, OBJECT_TYPE will be automatically restored. */
24674 parser->context->object_type = NULL_TREE;
24676 if (name == error_mark_node)
24677 return error_mark_node;
24679 /* A template-id has already been resolved; there is no lookup to
24681 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24683 if (BASELINK_P (name))
24685 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24686 == TEMPLATE_ID_EXPR);
24690 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24691 it should already have been checked to make sure that the name
24692 used matches the type being destroyed. */
24693 if (TREE_CODE (name) == BIT_NOT_EXPR)
24697 /* Figure out to which type this destructor applies. */
24699 type = parser->scope;
24700 else if (object_type)
24701 type = object_type;
24703 type = current_class_type;
24704 /* If that's not a class type, there is no destructor. */
24705 if (!type || !CLASS_TYPE_P (type))
24706 return error_mark_node;
24707 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24708 lazily_declare_fn (sfk_destructor, type);
24709 if (!CLASSTYPE_DESTRUCTORS (type))
24710 return error_mark_node;
24711 /* If it was a class type, return the destructor. */
24712 return CLASSTYPE_DESTRUCTORS (type);
24715 /* By this point, the NAME should be an ordinary identifier. If
24716 the id-expression was a qualified name, the qualifying scope is
24717 stored in PARSER->SCOPE at this point. */
24718 gcc_assert (identifier_p (name));
24720 /* Perform the lookup. */
24725 if (parser->scope == error_mark_node)
24726 return error_mark_node;
24728 /* If the SCOPE is dependent, the lookup must be deferred until
24729 the template is instantiated -- unless we are explicitly
24730 looking up names in uninstantiated templates. Even then, we
24731 cannot look up the name if the scope is not a class type; it
24732 might, for example, be a template type parameter. */
24733 dependent_p = (TYPE_P (parser->scope)
24734 && dependent_scope_p (parser->scope));
24735 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24737 /* Defer lookup. */
24738 decl = error_mark_node;
24741 tree pushed_scope = NULL_TREE;
24743 /* If PARSER->SCOPE is a dependent type, then it must be a
24744 class type, and we must not be checking dependencies;
24745 otherwise, we would have processed this lookup above. So
24746 that PARSER->SCOPE is not considered a dependent base by
24747 lookup_member, we must enter the scope here. */
24749 pushed_scope = push_scope (parser->scope);
24751 /* If the PARSER->SCOPE is a template specialization, it
24752 may be instantiated during name lookup. In that case,
24753 errors may be issued. Even if we rollback the current
24754 tentative parse, those errors are valid. */
24755 decl = lookup_qualified_name (parser->scope, name,
24756 prefer_type_arg (tag_type),
24757 /*complain=*/true);
24759 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24760 lookup result and the nested-name-specifier nominates a class C:
24761 * if the name specified after the nested-name-specifier, when
24762 looked up in C, is the injected-class-name of C (Clause 9), or
24763 * if the name specified after the nested-name-specifier is the
24764 same as the identifier or the simple-template-id's template-
24765 name in the last component of the nested-name-specifier,
24766 the name is instead considered to name the constructor of
24767 class C. [ Note: for example, the constructor is not an
24768 acceptable lookup result in an elaborated-type-specifier so
24769 the constructor would not be used in place of the
24770 injected-class-name. --end note ] Such a constructor name
24771 shall be used only in the declarator-id of a declaration that
24772 names a constructor or in a using-declaration. */
24773 if (tag_type == none_type
24774 && DECL_SELF_REFERENCE_P (decl)
24775 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24776 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24777 prefer_type_arg (tag_type),
24778 /*complain=*/true);
24780 /* If we have a single function from a using decl, pull it out. */
24781 if (TREE_CODE (decl) == OVERLOAD
24782 && !really_overloaded_fn (decl))
24783 decl = OVL_FUNCTION (decl);
24786 pop_scope (pushed_scope);
24789 /* If the scope is a dependent type and either we deferred lookup or
24790 we did lookup but didn't find the name, rememeber the name. */
24791 if (decl == error_mark_node && TYPE_P (parser->scope)
24792 && dependent_type_p (parser->scope))
24798 /* The resolution to Core Issue 180 says that `struct
24799 A::B' should be considered a type-name, even if `A'
24801 type = make_typename_type (parser->scope, name, tag_type,
24802 /*complain=*/tf_error);
24803 if (type != error_mark_node)
24804 decl = TYPE_NAME (type);
24806 else if (is_template
24807 && (cp_parser_next_token_ends_template_argument_p (parser)
24808 || cp_lexer_next_token_is (parser->lexer,
24810 decl = make_unbound_class_template (parser->scope,
24812 /*complain=*/tf_error);
24814 decl = build_qualified_name (/*type=*/NULL_TREE,
24815 parser->scope, name,
24818 parser->qualifying_scope = parser->scope;
24819 parser->object_scope = NULL_TREE;
24821 else if (object_type)
24823 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24824 OBJECT_TYPE is not a class. */
24825 if (CLASS_TYPE_P (object_type))
24826 /* If the OBJECT_TYPE is a template specialization, it may
24827 be instantiated during name lookup. In that case, errors
24828 may be issued. Even if we rollback the current tentative
24829 parse, those errors are valid. */
24830 decl = lookup_member (object_type,
24833 prefer_type_arg (tag_type),
24834 tf_warning_or_error);
24840 /* Look it up in the enclosing context. */
24841 decl = lookup_name_real (name, prefer_type_arg (tag_type),
24843 /*block_p=*/true, is_namespace, 0);
24844 /* DR 141 says when looking for a template-name after -> or ., only
24845 consider class templates. We need to fix our handling of
24846 dependent expressions to implement that properly, but for now
24847 let's ignore namespace-scope function templates. */
24848 if (decl && is_template && !DECL_TYPE_TEMPLATE_P (decl))
24851 if (is_overloaded_fn (d))
24852 d = get_first_fn (d);
24853 if (DECL_P (d) && !DECL_CLASS_SCOPE_P (d))
24857 if (object_type == unknown_type_node)
24858 /* The object is type-dependent, so we can't look anything up; we used
24859 this to get the DR 141 behavior. */
24860 object_type = NULL_TREE;
24861 parser->object_scope = object_type;
24862 parser->qualifying_scope = NULL_TREE;
24866 decl = lookup_name_real (name, prefer_type_arg (tag_type),
24868 /*block_p=*/true, is_namespace, 0);
24869 parser->qualifying_scope = NULL_TREE;
24870 parser->object_scope = NULL_TREE;
24873 /* If the lookup failed, let our caller know. */
24874 if (!decl || decl == error_mark_node)
24875 return error_mark_node;
24877 /* Pull out the template from an injected-class-name (or multiple). */
24879 decl = maybe_get_template_decl_from_type_decl (decl);
24881 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24882 if (TREE_CODE (decl) == TREE_LIST)
24884 if (ambiguous_decls)
24885 *ambiguous_decls = decl;
24886 /* The error message we have to print is too complicated for
24887 cp_parser_error, so we incorporate its actions directly. */
24888 if (!cp_parser_simulate_error (parser))
24890 error_at (name_location, "reference to %qD is ambiguous",
24892 print_candidates (decl);
24894 return error_mark_node;
24897 gcc_assert (DECL_P (decl)
24898 || TREE_CODE (decl) == OVERLOAD
24899 || TREE_CODE (decl) == SCOPE_REF
24900 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24901 || BASELINK_P (decl));
24903 /* If we have resolved the name of a member declaration, check to
24904 see if the declaration is accessible. When the name resolves to
24905 set of overloaded functions, accessibility is checked when
24906 overload resolution is done.
24908 During an explicit instantiation, access is not checked at all,
24909 as per [temp.explicit]. */
24911 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24913 maybe_record_typedef_use (decl);
24915 return cp_expr (decl, name_location);
24918 /* Like cp_parser_lookup_name, but for use in the typical case where
24919 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24920 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24923 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24925 return cp_parser_lookup_name (parser, name,
24927 /*is_template=*/false,
24928 /*is_namespace=*/false,
24929 /*check_dependency=*/true,
24930 /*ambiguous_decls=*/NULL,
24934 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24935 the current context, return the TYPE_DECL. If TAG_NAME_P is
24936 true, the DECL indicates the class being defined in a class-head,
24937 or declared in an elaborated-type-specifier.
24939 Otherwise, return DECL. */
24942 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24944 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24945 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24948 template <typename T> struct B;
24951 template <typename T> struct A::B {};
24953 Similarly, in an elaborated-type-specifier:
24955 namespace N { struct X{}; }
24958 template <typename T> friend struct N::X;
24961 However, if the DECL refers to a class type, and we are in
24962 the scope of the class, then the name lookup automatically
24963 finds the TYPE_DECL created by build_self_reference rather
24964 than a TEMPLATE_DECL. For example, in:
24966 template <class T> struct S {
24970 there is no need to handle such case. */
24972 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24973 return DECL_TEMPLATE_RESULT (decl);
24978 /* If too many, or too few, template-parameter lists apply to the
24979 declarator, issue an error message. Returns TRUE if all went well,
24980 and FALSE otherwise. */
24983 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24984 cp_declarator *declarator,
24985 location_t declarator_location)
24987 switch (declarator->kind)
24991 unsigned num_templates = 0;
24992 tree scope = declarator->u.id.qualifying_scope;
24995 num_templates = num_template_headers_for_class (scope);
24996 else if (TREE_CODE (declarator->u.id.unqualified_name)
24997 == TEMPLATE_ID_EXPR)
24998 /* If the DECLARATOR has the form `X<y>' then it uses one
24999 additional level of template parameters. */
25002 return cp_parser_check_template_parameters
25003 (parser, num_templates, declarator_location, declarator);
25009 case cdk_reference:
25011 return (cp_parser_check_declarator_template_parameters
25012 (parser, declarator->declarator, declarator_location));
25018 gcc_unreachable ();
25023 /* NUM_TEMPLATES were used in the current declaration. If that is
25024 invalid, return FALSE and issue an error messages. Otherwise,
25025 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25026 declarator and we can print more accurate diagnostics. */
25029 cp_parser_check_template_parameters (cp_parser* parser,
25030 unsigned num_templates,
25031 location_t location,
25032 cp_declarator *declarator)
25034 /* If there are the same number of template classes and parameter
25035 lists, that's OK. */
25036 if (parser->num_template_parameter_lists == num_templates)
25038 /* If there are more, but only one more, then we are referring to a
25039 member template. That's OK too. */
25040 if (parser->num_template_parameter_lists == num_templates + 1)
25042 /* If there are more template classes than parameter lists, we have
25045 template <class T> void S<T>::R<T>::f (); */
25046 if (parser->num_template_parameter_lists < num_templates)
25048 if (declarator && !current_function_decl)
25049 error_at (location, "specializing member %<%T::%E%> "
25050 "requires %<template<>%> syntax",
25051 declarator->u.id.qualifying_scope,
25052 declarator->u.id.unqualified_name);
25053 else if (declarator)
25054 error_at (location, "invalid declaration of %<%T::%E%>",
25055 declarator->u.id.qualifying_scope,
25056 declarator->u.id.unqualified_name);
25058 error_at (location, "too few template-parameter-lists");
25061 /* Otherwise, there are too many template parameter lists. We have
25064 template <class T> template <class U> void S::f(); */
25065 error_at (location, "too many template-parameter-lists");
25069 /* Parse an optional `::' token indicating that the following name is
25070 from the global namespace. If so, PARSER->SCOPE is set to the
25071 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25072 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25073 Returns the new value of PARSER->SCOPE, if the `::' token is
25074 present, and NULL_TREE otherwise. */
25077 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25081 /* Peek at the next token. */
25082 token = cp_lexer_peek_token (parser->lexer);
25083 /* If we're looking at a `::' token then we're starting from the
25084 global namespace, not our current location. */
25085 if (token->type == CPP_SCOPE)
25087 /* Consume the `::' token. */
25088 cp_lexer_consume_token (parser->lexer);
25089 /* Set the SCOPE so that we know where to start the lookup. */
25090 parser->scope = global_namespace;
25091 parser->qualifying_scope = global_namespace;
25092 parser->object_scope = NULL_TREE;
25094 return parser->scope;
25096 else if (!current_scope_valid_p)
25098 parser->scope = NULL_TREE;
25099 parser->qualifying_scope = NULL_TREE;
25100 parser->object_scope = NULL_TREE;
25106 /* Returns TRUE if the upcoming token sequence is the start of a
25107 constructor declarator. If FRIEND_P is true, the declarator is
25108 preceded by the `friend' specifier. */
25111 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25113 bool constructor_p;
25114 bool outside_class_specifier_p;
25115 tree nested_name_specifier;
25116 cp_token *next_token;
25118 /* The common case is that this is not a constructor declarator, so
25119 try to avoid doing lots of work if at all possible. It's not
25120 valid declare a constructor at function scope. */
25121 if (parser->in_function_body)
25123 /* And only certain tokens can begin a constructor declarator. */
25124 next_token = cp_lexer_peek_token (parser->lexer);
25125 if (next_token->type != CPP_NAME
25126 && next_token->type != CPP_SCOPE
25127 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25128 && next_token->type != CPP_TEMPLATE_ID)
25131 /* Parse tentatively; we are going to roll back all of the tokens
25133 cp_parser_parse_tentatively (parser);
25134 /* Assume that we are looking at a constructor declarator. */
25135 constructor_p = true;
25137 /* Look for the optional `::' operator. */
25138 cp_parser_global_scope_opt (parser,
25139 /*current_scope_valid_p=*/false);
25140 /* Look for the nested-name-specifier. */
25141 nested_name_specifier
25142 = (cp_parser_nested_name_specifier_opt (parser,
25143 /*typename_keyword_p=*/false,
25144 /*check_dependency_p=*/false,
25146 /*is_declaration=*/false));
25148 outside_class_specifier_p = (!at_class_scope_p ()
25149 || !TYPE_BEING_DEFINED (current_class_type)
25152 /* Outside of a class-specifier, there must be a
25153 nested-name-specifier. */
25154 if (!nested_name_specifier && outside_class_specifier_p)
25155 constructor_p = false;
25156 else if (nested_name_specifier == error_mark_node)
25157 constructor_p = false;
25159 /* If we have a class scope, this is easy; DR 147 says that S::S always
25160 names the constructor, and no other qualified name could. */
25161 if (constructor_p && nested_name_specifier
25162 && CLASS_TYPE_P (nested_name_specifier))
25164 tree id = cp_parser_unqualified_id (parser,
25165 /*template_keyword_p=*/false,
25166 /*check_dependency_p=*/false,
25167 /*declarator_p=*/true,
25168 /*optional_p=*/false);
25169 if (is_overloaded_fn (id))
25170 id = DECL_NAME (get_first_fn (id));
25171 if (!constructor_name_p (id, nested_name_specifier))
25172 constructor_p = false;
25174 /* If we still think that this might be a constructor-declarator,
25175 look for a class-name. */
25176 else if (constructor_p)
25180 template <typename T> struct S {
25184 we must recognize that the nested `S' names a class. */
25186 type_decl = cp_parser_class_name (parser,
25187 /*typename_keyword_p=*/false,
25188 /*template_keyword_p=*/false,
25190 /*check_dependency_p=*/false,
25191 /*class_head_p=*/false,
25192 /*is_declaration=*/false);
25193 /* If there was no class-name, then this is not a constructor.
25194 Otherwise, if we are in a class-specifier and we aren't
25195 handling a friend declaration, check that its type matches
25196 current_class_type (c++/38313). Note: error_mark_node
25197 is left alone for error recovery purposes. */
25198 constructor_p = (!cp_parser_error_occurred (parser)
25199 && (outside_class_specifier_p
25200 || type_decl == error_mark_node
25201 || same_type_p (current_class_type,
25202 TREE_TYPE (type_decl))));
25204 /* If we're still considering a constructor, we have to see a `(',
25205 to begin the parameter-declaration-clause, followed by either a
25206 `)', an `...', or a decl-specifier. We need to check for a
25207 type-specifier to avoid being fooled into thinking that:
25211 is a constructor. (It is actually a function named `f' that
25212 takes one parameter (of type `int') and returns a value of type
25215 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25216 constructor_p = false;
25219 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25220 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25221 /* A parameter declaration begins with a decl-specifier,
25222 which is either the "attribute" keyword, a storage class
25223 specifier, or (usually) a type-specifier. */
25224 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25227 tree pushed_scope = NULL_TREE;
25228 unsigned saved_num_template_parameter_lists;
25230 /* Names appearing in the type-specifier should be looked up
25231 in the scope of the class. */
25232 if (current_class_type)
25236 type = TREE_TYPE (type_decl);
25237 if (TREE_CODE (type) == TYPENAME_TYPE)
25239 type = resolve_typename_type (type,
25240 /*only_current_p=*/false);
25241 if (TREE_CODE (type) == TYPENAME_TYPE)
25243 cp_parser_abort_tentative_parse (parser);
25247 pushed_scope = push_scope (type);
25250 /* Inside the constructor parameter list, surrounding
25251 template-parameter-lists do not apply. */
25252 saved_num_template_parameter_lists
25253 = parser->num_template_parameter_lists;
25254 parser->num_template_parameter_lists = 0;
25256 /* Look for the type-specifier. */
25257 cp_parser_type_specifier (parser,
25258 CP_PARSER_FLAGS_NONE,
25259 /*decl_specs=*/NULL,
25260 /*is_declarator=*/true,
25261 /*declares_class_or_enum=*/NULL,
25262 /*is_cv_qualifier=*/NULL);
25264 parser->num_template_parameter_lists
25265 = saved_num_template_parameter_lists;
25267 /* Leave the scope of the class. */
25269 pop_scope (pushed_scope);
25271 constructor_p = !cp_parser_error_occurred (parser);
25275 /* We did not really want to consume any tokens. */
25276 cp_parser_abort_tentative_parse (parser);
25278 return constructor_p;
25281 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25282 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25283 they must be performed once we are in the scope of the function.
25285 Returns the function defined. */
25288 cp_parser_function_definition_from_specifiers_and_declarator
25289 (cp_parser* parser,
25290 cp_decl_specifier_seq *decl_specifiers,
25292 const cp_declarator *declarator)
25297 /* Begin the function-definition. */
25298 success_p = start_function (decl_specifiers, declarator, attributes);
25300 /* The things we're about to see are not directly qualified by any
25301 template headers we've seen thus far. */
25302 reset_specialization ();
25304 /* If there were names looked up in the decl-specifier-seq that we
25305 did not check, check them now. We must wait until we are in the
25306 scope of the function to perform the checks, since the function
25307 might be a friend. */
25308 perform_deferred_access_checks (tf_warning_or_error);
25312 cp_finalize_omp_declare_simd (parser, current_function_decl);
25313 parser->omp_declare_simd = NULL;
25314 cp_finalize_oacc_routine (parser, current_function_decl, true);
25315 parser->oacc_routine = NULL;
25320 /* Skip the entire function. */
25321 cp_parser_skip_to_end_of_block_or_statement (parser);
25322 fn = error_mark_node;
25324 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25326 /* Seen already, skip it. An error message has already been output. */
25327 cp_parser_skip_to_end_of_block_or_statement (parser);
25328 fn = current_function_decl;
25329 current_function_decl = NULL_TREE;
25330 /* If this is a function from a class, pop the nested class. */
25331 if (current_class_name)
25332 pop_nested_class ();
25337 if (DECL_DECLARED_INLINE_P (current_function_decl))
25338 tv = TV_PARSE_INLINE;
25340 tv = TV_PARSE_FUNC;
25342 fn = cp_parser_function_definition_after_declarator (parser,
25343 /*inline_p=*/false);
25350 /* Parse the part of a function-definition that follows the
25351 declarator. INLINE_P is TRUE iff this function is an inline
25352 function defined within a class-specifier.
25354 Returns the function defined. */
25357 cp_parser_function_definition_after_declarator (cp_parser* parser,
25361 bool ctor_initializer_p = false;
25362 bool saved_in_unbraced_linkage_specification_p;
25363 bool saved_in_function_body;
25364 unsigned saved_num_template_parameter_lists;
25366 bool fully_implicit_function_template_p
25367 = parser->fully_implicit_function_template_p;
25368 parser->fully_implicit_function_template_p = false;
25369 tree implicit_template_parms
25370 = parser->implicit_template_parms;
25371 parser->implicit_template_parms = 0;
25372 cp_binding_level* implicit_template_scope
25373 = parser->implicit_template_scope;
25374 parser->implicit_template_scope = 0;
25376 saved_in_function_body = parser->in_function_body;
25377 parser->in_function_body = true;
25378 /* If the next token is `return', then the code may be trying to
25379 make use of the "named return value" extension that G++ used to
25381 token = cp_lexer_peek_token (parser->lexer);
25382 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25384 /* Consume the `return' keyword. */
25385 cp_lexer_consume_token (parser->lexer);
25386 /* Look for the identifier that indicates what value is to be
25388 cp_parser_identifier (parser);
25389 /* Issue an error message. */
25390 error_at (token->location,
25391 "named return values are no longer supported");
25392 /* Skip tokens until we reach the start of the function body. */
25395 cp_token *token = cp_lexer_peek_token (parser->lexer);
25396 if (token->type == CPP_OPEN_BRACE
25397 || token->type == CPP_EOF
25398 || token->type == CPP_PRAGMA_EOL)
25400 cp_lexer_consume_token (parser->lexer);
25403 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25404 anything declared inside `f'. */
25405 saved_in_unbraced_linkage_specification_p
25406 = parser->in_unbraced_linkage_specification_p;
25407 parser->in_unbraced_linkage_specification_p = false;
25408 /* Inside the function, surrounding template-parameter-lists do not
25410 saved_num_template_parameter_lists
25411 = parser->num_template_parameter_lists;
25412 parser->num_template_parameter_lists = 0;
25414 start_lambda_scope (current_function_decl);
25416 /* If the next token is `try', `__transaction_atomic', or
25417 `__transaction_relaxed`, then we are looking at either function-try-block
25418 or function-transaction-block. Note that all of these include the
25420 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25421 ctor_initializer_p = cp_parser_function_transaction (parser,
25422 RID_TRANSACTION_ATOMIC);
25423 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25424 RID_TRANSACTION_RELAXED))
25425 ctor_initializer_p = cp_parser_function_transaction (parser,
25426 RID_TRANSACTION_RELAXED);
25427 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25428 ctor_initializer_p = cp_parser_function_try_block (parser);
25430 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25431 (parser, /*in_function_try_block=*/false);
25433 finish_lambda_scope ();
25435 /* Finish the function. */
25436 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25437 (inline_p ? 2 : 0));
25438 /* Generate code for it, if necessary. */
25439 expand_or_defer_fn (fn);
25440 /* Restore the saved values. */
25441 parser->in_unbraced_linkage_specification_p
25442 = saved_in_unbraced_linkage_specification_p;
25443 parser->num_template_parameter_lists
25444 = saved_num_template_parameter_lists;
25445 parser->in_function_body = saved_in_function_body;
25447 parser->fully_implicit_function_template_p
25448 = fully_implicit_function_template_p;
25449 parser->implicit_template_parms
25450 = implicit_template_parms;
25451 parser->implicit_template_scope
25452 = implicit_template_scope;
25454 if (parser->fully_implicit_function_template_p)
25455 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25460 /* Parse a template-declaration body (following argument list). */
25463 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25464 tree parameter_list,
25467 tree decl = NULL_TREE;
25468 bool friend_p = false;
25470 /* We just processed one more parameter list. */
25471 ++parser->num_template_parameter_lists;
25473 /* Get the deferred access checks from the parameter list. These
25474 will be checked once we know what is being declared, as for a
25475 member template the checks must be performed in the scope of the
25476 class containing the member. */
25477 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25479 /* Tentatively parse for a new template parameter list, which can either be
25480 the template keyword or a template introduction. */
25481 if (cp_parser_template_declaration_after_export (parser, member_p))
25483 else if (cxx_dialect >= cxx11
25484 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25485 decl = cp_parser_alias_declaration (parser);
25488 /* There are no access checks when parsing a template, as we do not
25489 know if a specialization will be a friend. */
25490 push_deferring_access_checks (dk_no_check);
25491 cp_token *token = cp_lexer_peek_token (parser->lexer);
25492 decl = cp_parser_single_declaration (parser,
25495 /*explicit_specialization_p=*/false,
25497 pop_deferring_access_checks ();
25499 /* If this is a member template declaration, let the front
25501 if (member_p && !friend_p && decl)
25503 if (TREE_CODE (decl) == TYPE_DECL)
25504 cp_parser_check_access_in_redeclaration (decl, token->location);
25506 decl = finish_member_template_decl (decl);
25508 else if (friend_p && decl
25509 && DECL_DECLARES_TYPE_P (decl))
25510 make_friend_class (current_class_type, TREE_TYPE (decl),
25511 /*complain=*/true);
25513 /* We are done with the current parameter list. */
25514 --parser->num_template_parameter_lists;
25516 pop_deferring_access_checks ();
25519 finish_template_decl (parameter_list);
25521 /* Check the template arguments for a literal operator template. */
25523 && DECL_DECLARES_FUNCTION_P (decl)
25524 && UDLIT_OPER_P (DECL_NAME (decl)))
25527 if (parameter_list == NULL_TREE)
25531 int num_parms = TREE_VEC_LENGTH (parameter_list);
25532 if (num_parms == 1)
25534 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25535 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25536 if (TREE_TYPE (parm) != char_type_node
25537 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25540 else if (num_parms == 2 && cxx_dialect >= cxx14)
25542 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25543 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25544 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25545 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25546 if (parm == error_mark_node
25547 || TREE_TYPE (parm) != TREE_TYPE (type)
25548 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25556 if (cxx_dialect >= cxx14)
25557 error ("literal operator template %qD has invalid parameter list."
25558 " Expected non-type template argument pack <char...>"
25559 " or <typename CharT, CharT...>",
25562 error ("literal operator template %qD has invalid parameter list."
25563 " Expected non-type template argument pack <char...>",
25568 /* Register member declarations. */
25569 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25570 finish_member_declaration (decl);
25571 /* If DECL is a function template, we must return to parse it later.
25572 (Even though there is no definition, there might be default
25573 arguments that need handling.) */
25574 if (member_p && decl
25575 && DECL_DECLARES_FUNCTION_P (decl))
25576 vec_safe_push (unparsed_funs_with_definitions, decl);
25579 /* Parse a template introduction header for a template-declaration. Returns
25580 false if tentative parse fails. */
25583 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25585 cp_parser_parse_tentatively (parser);
25587 tree saved_scope = parser->scope;
25588 tree saved_object_scope = parser->object_scope;
25589 tree saved_qualifying_scope = parser->qualifying_scope;
25591 /* Look for the optional `::' operator. */
25592 cp_parser_global_scope_opt (parser,
25593 /*current_scope_valid_p=*/false);
25594 /* Look for the nested-name-specifier. */
25595 cp_parser_nested_name_specifier_opt (parser,
25596 /*typename_keyword_p=*/false,
25597 /*check_dependency_p=*/true,
25599 /*is_declaration=*/false);
25601 cp_token *token = cp_lexer_peek_token (parser->lexer);
25602 tree concept_name = cp_parser_identifier (parser);
25604 /* Look up the concept for which we will be matching
25605 template parameters. */
25606 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25608 parser->scope = saved_scope;
25609 parser->object_scope = saved_object_scope;
25610 parser->qualifying_scope = saved_qualifying_scope;
25612 if (concept_name == error_mark_node)
25613 cp_parser_simulate_error (parser);
25615 /* Look for opening brace for introduction. */
25616 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25618 if (!cp_parser_parse_definitely (parser))
25621 push_deferring_access_checks (dk_deferred);
25623 /* Build vector of placeholder parameters and grab
25624 matching identifiers. */
25625 tree introduction_list = cp_parser_introduction_list (parser);
25627 /* The introduction-list shall not be empty. */
25628 int nargs = TREE_VEC_LENGTH (introduction_list);
25631 error ("empty introduction-list");
25635 /* Look for closing brace for introduction. */
25636 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25639 if (tmpl_decl == error_mark_node)
25641 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25646 /* Build and associate the constraint. */
25647 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25648 if (parms && parms != error_mark_node)
25650 cp_parser_template_declaration_after_parameters (parser, parms,
25655 error_at (token->location, "no matching concept for template-introduction");
25659 /* Parse a normal template-declaration following the template keyword. */
25662 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25664 tree parameter_list;
25665 bool need_lang_pop;
25666 location_t location = input_location;
25668 /* Look for the `<' token. */
25669 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25671 if (at_class_scope_p () && current_function_decl)
25673 /* 14.5.2.2 [temp.mem]
25675 A local class shall not have member templates. */
25676 error_at (location,
25677 "invalid declaration of member template in local class");
25678 cp_parser_skip_to_end_of_block_or_statement (parser);
25683 A template ... shall not have C linkage. */
25684 if (current_lang_name == lang_name_c)
25686 error_at (location, "template with C linkage");
25687 /* Give it C++ linkage to avoid confusing other parts of the
25689 push_lang_context (lang_name_cplusplus);
25690 need_lang_pop = true;
25693 need_lang_pop = false;
25695 /* We cannot perform access checks on the template parameter
25696 declarations until we know what is being declared, just as we
25697 cannot check the decl-specifier list. */
25698 push_deferring_access_checks (dk_deferred);
25700 /* If the next token is `>', then we have an invalid
25701 specialization. Rather than complain about an invalid template
25702 parameter, issue an error message here. */
25703 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25705 cp_parser_error (parser, "invalid explicit specialization");
25706 begin_specialization ();
25707 parameter_list = NULL_TREE;
25711 /* Parse the template parameters. */
25712 parameter_list = cp_parser_template_parameter_list (parser);
25715 /* Look for the `>'. */
25716 cp_parser_skip_to_end_of_template_parameter_list (parser);
25718 /* Manage template requirements */
25721 tree reqs = get_shorthand_constraints (current_template_parms);
25722 if (tree r = cp_parser_requires_clause_opt (parser))
25723 reqs = conjoin_constraints (reqs, normalize_expression (r));
25724 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25727 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25730 /* For the erroneous case of a template with C linkage, we pushed an
25731 implicit C++ linkage scope; exit that scope now. */
25733 pop_lang_context ();
25736 /* Parse a template-declaration, assuming that the `export' (and
25737 `extern') keywords, if present, has already been scanned. MEMBER_P
25738 is as for cp_parser_template_declaration. */
25741 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25745 cp_lexer_consume_token (parser->lexer);
25746 cp_parser_explicit_template_declaration (parser, member_p);
25749 else if (flag_concepts)
25750 return cp_parser_template_introduction (parser, member_p);
25755 /* Perform the deferred access checks from a template-parameter-list.
25756 CHECKS is a TREE_LIST of access checks, as returned by
25757 get_deferred_access_checks. */
25760 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25762 ++processing_template_parmlist;
25763 perform_access_checks (checks, tf_warning_or_error);
25764 --processing_template_parmlist;
25767 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25768 `function-definition' sequence that follows a template header.
25769 If MEMBER_P is true, this declaration appears in a class scope.
25771 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25772 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25775 cp_parser_single_declaration (cp_parser* parser,
25776 vec<deferred_access_check, va_gc> *checks,
25778 bool explicit_specialization_p,
25781 int declares_class_or_enum;
25782 tree decl = NULL_TREE;
25783 cp_decl_specifier_seq decl_specifiers;
25784 bool function_definition_p = false;
25785 cp_token *decl_spec_token_start;
25787 /* This function is only used when processing a template
25789 gcc_assert (innermost_scope_kind () == sk_template_parms
25790 || innermost_scope_kind () == sk_template_spec);
25792 /* Defer access checks until we know what is being declared. */
25793 push_deferring_access_checks (dk_deferred);
25795 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25797 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25798 cp_parser_decl_specifier_seq (parser,
25799 CP_PARSER_FLAGS_OPTIONAL,
25801 &declares_class_or_enum);
25803 *friend_p = cp_parser_friend_p (&decl_specifiers);
25805 /* There are no template typedefs. */
25806 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25808 error_at (decl_spec_token_start->location,
25809 "template declaration of %<typedef%>");
25810 decl = error_mark_node;
25813 /* Gather up the access checks that occurred the
25814 decl-specifier-seq. */
25815 stop_deferring_access_checks ();
25817 /* Check for the declaration of a template class. */
25818 if (declares_class_or_enum)
25820 if (cp_parser_declares_only_class_p (parser)
25821 || (declares_class_or_enum & 2))
25823 // If this is a declaration, but not a definition, associate
25824 // any constraints with the type declaration. Constraints
25825 // are associated with definitions in cp_parser_class_specifier.
25826 if (declares_class_or_enum == 1)
25827 associate_classtype_constraints (decl_specifiers.type);
25829 decl = shadow_tag (&decl_specifiers);
25834 friend template <typename T> struct A<T>::B;
25837 A<T>::B will be represented by a TYPENAME_TYPE, and
25838 therefore not recognized by shadow_tag. */
25839 if (friend_p && *friend_p
25841 && decl_specifiers.type
25842 && TYPE_P (decl_specifiers.type))
25843 decl = decl_specifiers.type;
25845 if (decl && decl != error_mark_node)
25846 decl = TYPE_NAME (decl);
25848 decl = error_mark_node;
25850 /* Perform access checks for template parameters. */
25851 cp_parser_perform_template_parameter_access_checks (checks);
25853 /* Give a helpful diagnostic for
25854 template <class T> struct A { } a;
25855 if we aren't already recovering from an error. */
25856 if (!cp_parser_declares_only_class_p (parser)
25859 error_at (cp_lexer_peek_token (parser->lexer)->location,
25860 "a class template declaration must not declare "
25862 cp_parser_skip_to_end_of_block_or_statement (parser);
25868 /* Complain about missing 'typename' or other invalid type names. */
25869 if (!decl_specifiers.any_type_specifiers_p
25870 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25872 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25873 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25874 the rest of this declaration. */
25875 decl = error_mark_node;
25879 /* If it's not a template class, try for a template function. If
25880 the next token is a `;', then this declaration does not declare
25881 anything. But, if there were errors in the decl-specifiers, then
25882 the error might well have come from an attempted class-specifier.
25883 In that case, there's no need to warn about a missing declarator. */
25885 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25886 || decl_specifiers.type != error_mark_node))
25888 decl = cp_parser_init_declarator (parser,
25891 /*function_definition_allowed_p=*/true,
25893 declares_class_or_enum,
25894 &function_definition_p,
25897 /* 7.1.1-1 [dcl.stc]
25899 A storage-class-specifier shall not be specified in an explicit
25900 specialization... */
25902 && explicit_specialization_p
25903 && decl_specifiers.storage_class != sc_none)
25905 error_at (decl_spec_token_start->location,
25906 "explicit template specialization cannot have a storage class");
25907 decl = error_mark_node;
25910 if (decl && VAR_P (decl))
25911 check_template_variable (decl);
25914 /* Look for a trailing `;' after the declaration. */
25915 if (!function_definition_p
25916 && (decl == error_mark_node
25917 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25918 cp_parser_skip_to_end_of_block_or_statement (parser);
25921 pop_deferring_access_checks ();
25923 /* Clear any current qualification; whatever comes next is the start
25924 of something new. */
25925 parser->scope = NULL_TREE;
25926 parser->qualifying_scope = NULL_TREE;
25927 parser->object_scope = NULL_TREE;
25932 /* Parse a cast-expression that is not the operand of a unary "&". */
25935 cp_parser_simple_cast_expression (cp_parser *parser)
25937 return cp_parser_cast_expression (parser, /*address_p=*/false,
25938 /*cast_p=*/false, /*decltype*/false, NULL);
25941 /* Parse a functional cast to TYPE. Returns an expression
25942 representing the cast. */
25945 cp_parser_functional_cast (cp_parser* parser, tree type)
25947 vec<tree, va_gc> *vec;
25948 tree expression_list;
25952 location_t start_loc = input_location;
25955 type = error_mark_node;
25957 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25959 cp_lexer_set_source_position (parser->lexer);
25960 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25961 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25962 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25963 if (TREE_CODE (type) == TYPE_DECL)
25964 type = TREE_TYPE (type);
25966 cast = finish_compound_literal (type, expression_list,
25967 tf_warning_or_error);
25968 /* Create a location of the form:
25971 with caret == start at the start of the type name,
25972 finishing at the closing brace. */
25973 location_t finish_loc
25974 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25975 location_t combined_loc = make_location (start_loc, start_loc,
25977 cast.set_location (combined_loc);
25982 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25984 /*allow_expansion_p=*/true,
25985 /*non_constant_p=*/NULL);
25987 expression_list = error_mark_node;
25990 expression_list = build_tree_list_vec (vec);
25991 release_tree_vector (vec);
25994 cast = build_functional_cast (type, expression_list,
25995 tf_warning_or_error);
25996 /* [expr.const]/1: In an integral constant expression "only type
25997 conversions to integral or enumeration type can be used". */
25998 if (TREE_CODE (type) == TYPE_DECL)
25999 type = TREE_TYPE (type);
26000 if (cast != error_mark_node
26001 && !cast_valid_in_integral_constant_expression_p (type)
26002 && cp_parser_non_integral_constant_expression (parser,
26004 return error_mark_node;
26006 /* Create a location of the form:
26009 with caret == start at the start of the type name,
26010 finishing at the closing paren. */
26011 location_t finish_loc
26012 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26013 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26014 cast.set_location (combined_loc);
26018 /* Save the tokens that make up the body of a member function defined
26019 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26020 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26021 specifiers applied to the declaration. Returns the FUNCTION_DECL
26022 for the member function. */
26025 cp_parser_save_member_function_body (cp_parser* parser,
26026 cp_decl_specifier_seq *decl_specifiers,
26027 cp_declarator *declarator,
26033 bool function_try_block = false;
26035 /* Create the FUNCTION_DECL. */
26036 fn = grokmethod (decl_specifiers, declarator, attributes);
26037 cp_finalize_omp_declare_simd (parser, fn);
26038 cp_finalize_oacc_routine (parser, fn, true);
26039 /* If something went badly wrong, bail out now. */
26040 if (fn == error_mark_node)
26042 /* If there's a function-body, skip it. */
26043 if (cp_parser_token_starts_function_definition_p
26044 (cp_lexer_peek_token (parser->lexer)))
26045 cp_parser_skip_to_end_of_block_or_statement (parser);
26046 return error_mark_node;
26049 /* Remember it, if there default args to post process. */
26050 cp_parser_save_default_args (parser, fn);
26052 /* Save away the tokens that make up the body of the
26054 first = parser->lexer->next_token;
26056 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26057 cp_lexer_consume_token (parser->lexer);
26058 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26059 RID_TRANSACTION_ATOMIC))
26061 cp_lexer_consume_token (parser->lexer);
26062 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26063 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26064 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26065 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26066 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26067 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26068 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26070 cp_lexer_consume_token (parser->lexer);
26071 cp_lexer_consume_token (parser->lexer);
26072 cp_lexer_consume_token (parser->lexer);
26073 cp_lexer_consume_token (parser->lexer);
26074 cp_lexer_consume_token (parser->lexer);
26077 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26078 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26080 cp_lexer_consume_token (parser->lexer);
26081 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26086 /* Handle function try blocks. */
26087 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26089 cp_lexer_consume_token (parser->lexer);
26090 function_try_block = true;
26092 /* We can have braced-init-list mem-initializers before the fn body. */
26093 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26095 cp_lexer_consume_token (parser->lexer);
26096 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26098 /* cache_group will stop after an un-nested { } pair, too. */
26099 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26102 /* variadic mem-inits have ... after the ')'. */
26103 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26104 cp_lexer_consume_token (parser->lexer);
26107 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26108 /* Handle function try blocks. */
26109 if (function_try_block)
26110 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26111 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26112 last = parser->lexer->next_token;
26114 /* Save away the inline definition; we will process it when the
26115 class is complete. */
26116 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26117 DECL_PENDING_INLINE_P (fn) = 1;
26119 /* We need to know that this was defined in the class, so that
26120 friend templates are handled correctly. */
26121 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26123 /* Add FN to the queue of functions to be parsed later. */
26124 vec_safe_push (unparsed_funs_with_definitions, fn);
26129 /* Save the tokens that make up the in-class initializer for a non-static
26130 data member. Returns a DEFAULT_ARG. */
26133 cp_parser_save_nsdmi (cp_parser* parser)
26135 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26138 /* Parse a template-argument-list, as well as the trailing ">" (but
26139 not the opening "<"). See cp_parser_template_argument_list for the
26143 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26147 tree saved_qualifying_scope;
26148 tree saved_object_scope;
26149 bool saved_greater_than_is_operator_p;
26150 int saved_unevaluated_operand;
26151 int saved_inhibit_evaluation_warnings;
26155 When parsing a template-id, the first non-nested `>' is taken as
26156 the end of the template-argument-list rather than a greater-than
26158 saved_greater_than_is_operator_p
26159 = parser->greater_than_is_operator_p;
26160 parser->greater_than_is_operator_p = false;
26161 /* Parsing the argument list may modify SCOPE, so we save it
26163 saved_scope = parser->scope;
26164 saved_qualifying_scope = parser->qualifying_scope;
26165 saved_object_scope = parser->object_scope;
26166 /* We need to evaluate the template arguments, even though this
26167 template-id may be nested within a "sizeof". */
26168 saved_unevaluated_operand = cp_unevaluated_operand;
26169 cp_unevaluated_operand = 0;
26170 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26171 c_inhibit_evaluation_warnings = 0;
26172 /* Parse the template-argument-list itself. */
26173 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26174 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26175 arguments = NULL_TREE;
26177 arguments = cp_parser_template_argument_list (parser);
26178 /* Look for the `>' that ends the template-argument-list. If we find
26179 a '>>' instead, it's probably just a typo. */
26180 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26182 if (cxx_dialect != cxx98)
26184 /* In C++0x, a `>>' in a template argument list or cast
26185 expression is considered to be two separate `>'
26186 tokens. So, change the current token to a `>', but don't
26187 consume it: it will be consumed later when the outer
26188 template argument list (or cast expression) is parsed.
26189 Note that this replacement of `>' for `>>' is necessary
26190 even if we are parsing tentatively: in the tentative
26191 case, after calling
26192 cp_parser_enclosed_template_argument_list we will always
26193 throw away all of the template arguments and the first
26194 closing `>', either because the template argument list
26195 was erroneous or because we are replacing those tokens
26196 with a CPP_TEMPLATE_ID token. The second `>' (which will
26197 not have been thrown away) is needed either to close an
26198 outer template argument list or to complete a new-style
26200 cp_token *token = cp_lexer_peek_token (parser->lexer);
26201 token->type = CPP_GREATER;
26203 else if (!saved_greater_than_is_operator_p)
26205 /* If we're in a nested template argument list, the '>>' has
26206 to be a typo for '> >'. We emit the error message, but we
26207 continue parsing and we push a '>' as next token, so that
26208 the argument list will be parsed correctly. Note that the
26209 global source location is still on the token before the
26210 '>>', so we need to say explicitly where we want it. */
26211 cp_token *token = cp_lexer_peek_token (parser->lexer);
26212 error_at (token->location, "%<>>%> should be %<> >%> "
26213 "within a nested template argument list");
26215 token->type = CPP_GREATER;
26219 /* If this is not a nested template argument list, the '>>'
26220 is a typo for '>'. Emit an error message and continue.
26221 Same deal about the token location, but here we can get it
26222 right by consuming the '>>' before issuing the diagnostic. */
26223 cp_token *token = cp_lexer_consume_token (parser->lexer);
26224 error_at (token->location,
26225 "spurious %<>>%>, use %<>%> to terminate "
26226 "a template argument list");
26230 cp_parser_skip_to_end_of_template_parameter_list (parser);
26231 /* The `>' token might be a greater-than operator again now. */
26232 parser->greater_than_is_operator_p
26233 = saved_greater_than_is_operator_p;
26234 /* Restore the SAVED_SCOPE. */
26235 parser->scope = saved_scope;
26236 parser->qualifying_scope = saved_qualifying_scope;
26237 parser->object_scope = saved_object_scope;
26238 cp_unevaluated_operand = saved_unevaluated_operand;
26239 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26244 /* MEMBER_FUNCTION is a member function, or a friend. If default
26245 arguments, or the body of the function have not yet been parsed,
26249 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26251 timevar_push (TV_PARSE_INMETH);
26252 /* If this member is a template, get the underlying
26254 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26255 member_function = DECL_TEMPLATE_RESULT (member_function);
26257 /* There should not be any class definitions in progress at this
26258 point; the bodies of members are only parsed outside of all class
26260 gcc_assert (parser->num_classes_being_defined == 0);
26261 /* While we're parsing the member functions we might encounter more
26262 classes. We want to handle them right away, but we don't want
26263 them getting mixed up with functions that are currently in the
26265 push_unparsed_function_queues (parser);
26267 /* Make sure that any template parameters are in scope. */
26268 maybe_begin_member_template_processing (member_function);
26270 /* If the body of the function has not yet been parsed, parse it
26272 if (DECL_PENDING_INLINE_P (member_function))
26274 tree function_scope;
26275 cp_token_cache *tokens;
26277 /* The function is no longer pending; we are processing it. */
26278 tokens = DECL_PENDING_INLINE_INFO (member_function);
26279 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26280 DECL_PENDING_INLINE_P (member_function) = 0;
26282 /* If this is a local class, enter the scope of the containing
26284 function_scope = current_function_decl;
26285 if (function_scope)
26286 push_function_context ();
26288 /* Push the body of the function onto the lexer stack. */
26289 cp_parser_push_lexer_for_tokens (parser, tokens);
26291 /* Let the front end know that we going to be defining this
26293 start_preparsed_function (member_function, NULL_TREE,
26294 SF_PRE_PARSED | SF_INCLASS_INLINE);
26296 /* Don't do access checking if it is a templated function. */
26297 if (processing_template_decl)
26298 push_deferring_access_checks (dk_no_check);
26300 /* #pragma omp declare reduction needs special parsing. */
26301 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26303 parser->lexer->in_pragma = true;
26304 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26305 finish_function (/*inline*/2);
26306 cp_check_omp_declare_reduction (member_function);
26309 /* Now, parse the body of the function. */
26310 cp_parser_function_definition_after_declarator (parser,
26311 /*inline_p=*/true);
26313 if (processing_template_decl)
26314 pop_deferring_access_checks ();
26316 /* Leave the scope of the containing function. */
26317 if (function_scope)
26318 pop_function_context ();
26319 cp_parser_pop_lexer (parser);
26322 /* Remove any template parameters from the symbol table. */
26323 maybe_end_member_template_processing ();
26325 /* Restore the queue. */
26326 pop_unparsed_function_queues (parser);
26327 timevar_pop (TV_PARSE_INMETH);
26330 /* If DECL contains any default args, remember it on the unparsed
26331 functions queue. */
26334 cp_parser_save_default_args (cp_parser* parser, tree decl)
26338 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26340 probe = TREE_CHAIN (probe))
26341 if (TREE_PURPOSE (probe))
26343 cp_default_arg_entry entry = {current_class_type, decl};
26344 vec_safe_push (unparsed_funs_with_default_args, entry);
26349 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26350 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26351 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26352 from the parameter-type-list. */
26355 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26356 tree default_arg, tree parmtype)
26358 cp_token_cache *tokens;
26362 if (default_arg == error_mark_node)
26363 return error_mark_node;
26365 /* Push the saved tokens for the default argument onto the parser's
26367 tokens = DEFARG_TOKENS (default_arg);
26368 cp_parser_push_lexer_for_tokens (parser, tokens);
26370 start_lambda_scope (decl);
26372 /* Parse the default argument. */
26373 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26374 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26375 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26377 finish_lambda_scope ();
26379 if (parsed_arg == error_mark_node)
26380 cp_parser_skip_to_end_of_statement (parser);
26382 if (!processing_template_decl)
26384 /* In a non-template class, check conversions now. In a template,
26385 we'll wait and instantiate these as needed. */
26386 if (TREE_CODE (decl) == PARM_DECL)
26387 parsed_arg = check_default_argument (parmtype, parsed_arg,
26388 tf_warning_or_error);
26390 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26393 /* If the token stream has not been completely used up, then
26394 there was extra junk after the end of the default
26396 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26398 if (TREE_CODE (decl) == PARM_DECL)
26399 cp_parser_error (parser, "expected %<,%>");
26401 cp_parser_error (parser, "expected %<;%>");
26404 /* Revert to the main lexer. */
26405 cp_parser_pop_lexer (parser);
26410 /* FIELD is a non-static data member with an initializer which we saved for
26411 later; parse it now. */
26414 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26418 maybe_begin_member_template_processing (field);
26420 push_unparsed_function_queues (parser);
26421 def = cp_parser_late_parse_one_default_arg (parser, field,
26422 DECL_INITIAL (field),
26424 pop_unparsed_function_queues (parser);
26426 maybe_end_member_template_processing ();
26428 DECL_INITIAL (field) = def;
26431 /* FN is a FUNCTION_DECL which may contains a parameter with an
26432 unparsed DEFAULT_ARG. Parse the default args now. This function
26433 assumes that the current scope is the scope in which the default
26434 argument should be processed. */
26437 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26439 bool saved_local_variables_forbidden_p;
26440 tree parm, parmdecl;
26442 /* While we're parsing the default args, we might (due to the
26443 statement expression extension) encounter more classes. We want
26444 to handle them right away, but we don't want them getting mixed
26445 up with default args that are currently in the queue. */
26446 push_unparsed_function_queues (parser);
26448 /* Local variable names (and the `this' keyword) may not appear
26449 in a default argument. */
26450 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26451 parser->local_variables_forbidden_p = true;
26453 push_defarg_context (fn);
26455 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26456 parmdecl = DECL_ARGUMENTS (fn);
26457 parm && parm != void_list_node;
26458 parm = TREE_CHAIN (parm),
26459 parmdecl = DECL_CHAIN (parmdecl))
26461 tree default_arg = TREE_PURPOSE (parm);
26463 vec<tree, va_gc> *insts;
26470 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26471 /* This can happen for a friend declaration for a function
26472 already declared with default arguments. */
26476 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26478 TREE_VALUE (parm));
26479 if (parsed_arg == error_mark_node)
26484 TREE_PURPOSE (parm) = parsed_arg;
26486 /* Update any instantiations we've already created. */
26487 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26488 vec_safe_iterate (insts, ix, ©); ix++)
26489 TREE_PURPOSE (copy) = parsed_arg;
26492 pop_defarg_context ();
26494 /* Make sure no default arg is missing. */
26495 check_default_args (fn);
26497 /* Restore the state of local_variables_forbidden_p. */
26498 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26500 /* Restore the queue. */
26501 pop_unparsed_function_queues (parser);
26504 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26506 sizeof ... ( identifier )
26508 where the 'sizeof' token has already been consumed. */
26511 cp_parser_sizeof_pack (cp_parser *parser)
26513 /* Consume the `...'. */
26514 cp_lexer_consume_token (parser->lexer);
26515 maybe_warn_variadic_templates ();
26517 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26519 cp_lexer_consume_token (parser->lexer);
26521 permerror (cp_lexer_peek_token (parser->lexer)->location,
26522 "%<sizeof...%> argument must be surrounded by parentheses");
26524 cp_token *token = cp_lexer_peek_token (parser->lexer);
26525 tree name = cp_parser_identifier (parser);
26526 if (name == error_mark_node)
26527 return error_mark_node;
26528 /* The name is not qualified. */
26529 parser->scope = NULL_TREE;
26530 parser->qualifying_scope = NULL_TREE;
26531 parser->object_scope = NULL_TREE;
26532 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26533 if (expr == error_mark_node)
26534 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26536 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26537 expr = TREE_TYPE (expr);
26538 else if (TREE_CODE (expr) == CONST_DECL)
26539 expr = DECL_INITIAL (expr);
26540 expr = make_pack_expansion (expr);
26541 PACK_EXPANSION_SIZEOF_P (expr) = true;
26544 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26549 /* Parse the operand of `sizeof' (or a similar operator). Returns
26550 either a TYPE or an expression, depending on the form of the
26551 input. The KEYWORD indicates which kind of expression we have
26555 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26557 tree expr = NULL_TREE;
26558 const char *saved_message;
26560 bool saved_integral_constant_expression_p;
26561 bool saved_non_integral_constant_expression_p;
26563 /* If it's a `...', then we are computing the length of a parameter
26565 if (keyword == RID_SIZEOF
26566 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26567 return cp_parser_sizeof_pack (parser);
26569 /* Types cannot be defined in a `sizeof' expression. Save away the
26571 saved_message = parser->type_definition_forbidden_message;
26572 /* And create the new one. */
26573 tmp = concat ("types may not be defined in %<",
26574 IDENTIFIER_POINTER (ridpointers[keyword]),
26575 "%> expressions", NULL);
26576 parser->type_definition_forbidden_message = tmp;
26578 /* The restrictions on constant-expressions do not apply inside
26579 sizeof expressions. */
26580 saved_integral_constant_expression_p
26581 = parser->integral_constant_expression_p;
26582 saved_non_integral_constant_expression_p
26583 = parser->non_integral_constant_expression_p;
26584 parser->integral_constant_expression_p = false;
26586 /* Do not actually evaluate the expression. */
26587 ++cp_unevaluated_operand;
26588 ++c_inhibit_evaluation_warnings;
26589 /* If it's a `(', then we might be looking at the type-id
26591 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26593 tree type = NULL_TREE;
26595 /* We can't be sure yet whether we're looking at a type-id or an
26597 cp_parser_parse_tentatively (parser);
26598 /* Note: as a GNU Extension, compound literals are considered
26599 postfix-expressions as they are in C99, so they are valid
26600 arguments to sizeof. See comment in cp_parser_cast_expression
26602 if (cp_parser_compound_literal_p (parser))
26603 cp_parser_simulate_error (parser);
26606 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26607 parser->in_type_id_in_expr_p = true;
26608 /* Look for the type-id. */
26609 type = cp_parser_type_id (parser);
26610 /* Look for the closing `)'. */
26611 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26612 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26615 /* If all went well, then we're done. */
26616 if (cp_parser_parse_definitely (parser))
26618 cp_decl_specifier_seq decl_specs;
26620 /* Build a trivial decl-specifier-seq. */
26621 clear_decl_specs (&decl_specs);
26622 decl_specs.type = type;
26624 /* Call grokdeclarator to figure out what type this is. */
26625 expr = grokdeclarator (NULL,
26629 /*attrlist=*/NULL);
26633 /* If the type-id production did not work out, then we must be
26634 looking at the unary-expression production. */
26636 expr = cp_parser_unary_expression (parser);
26638 /* Go back to evaluating expressions. */
26639 --cp_unevaluated_operand;
26640 --c_inhibit_evaluation_warnings;
26642 /* Free the message we created. */
26644 /* And restore the old one. */
26645 parser->type_definition_forbidden_message = saved_message;
26646 parser->integral_constant_expression_p
26647 = saved_integral_constant_expression_p;
26648 parser->non_integral_constant_expression_p
26649 = saved_non_integral_constant_expression_p;
26654 /* If the current declaration has no declarator, return true. */
26657 cp_parser_declares_only_class_p (cp_parser *parser)
26659 /* If the next token is a `;' or a `,' then there is no
26661 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26662 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26665 /* Update the DECL_SPECS to reflect the storage class indicated by
26669 cp_parser_set_storage_class (cp_parser *parser,
26670 cp_decl_specifier_seq *decl_specs,
26674 cp_storage_class storage_class;
26676 if (parser->in_unbraced_linkage_specification_p)
26678 error_at (token->location, "invalid use of %qD in linkage specification",
26679 ridpointers[keyword]);
26682 else if (decl_specs->storage_class != sc_none)
26684 decl_specs->conflicting_specifiers_p = true;
26688 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26689 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26690 && decl_specs->gnu_thread_keyword_p)
26692 pedwarn (decl_specs->locations[ds_thread], 0,
26693 "%<__thread%> before %qD", ridpointers[keyword]);
26699 storage_class = sc_auto;
26702 storage_class = sc_register;
26705 storage_class = sc_static;
26708 storage_class = sc_extern;
26711 storage_class = sc_mutable;
26714 gcc_unreachable ();
26716 decl_specs->storage_class = storage_class;
26717 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26719 /* A storage class specifier cannot be applied alongside a typedef
26720 specifier. If there is a typedef specifier present then set
26721 conflicting_specifiers_p which will trigger an error later
26722 on in grokdeclarator. */
26723 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26724 decl_specs->conflicting_specifiers_p = true;
26727 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26728 is true, the type is a class or enum definition. */
26731 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26734 bool type_definition_p)
26736 decl_specs->any_specifiers_p = true;
26738 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26739 (with, for example, in "typedef int wchar_t;") we remember that
26740 this is what happened. In system headers, we ignore these
26741 declarations so that G++ can work with system headers that are not
26743 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26744 && !type_definition_p
26745 && (type_spec == boolean_type_node
26746 || type_spec == char16_type_node
26747 || type_spec == char32_type_node
26748 || type_spec == wchar_type_node)
26749 && (decl_specs->type
26750 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26751 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26752 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26753 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26755 decl_specs->redefined_builtin_type = type_spec;
26756 set_and_check_decl_spec_loc (decl_specs,
26757 ds_redefined_builtin_type_spec,
26759 if (!decl_specs->type)
26761 decl_specs->type = type_spec;
26762 decl_specs->type_definition_p = false;
26763 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26766 else if (decl_specs->type)
26767 decl_specs->multiple_types_p = true;
26770 decl_specs->type = type_spec;
26771 decl_specs->type_definition_p = type_definition_p;
26772 decl_specs->redefined_builtin_type = NULL_TREE;
26773 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26777 /* True iff TOKEN is the GNU keyword __thread. */
26780 token_is__thread (cp_token *token)
26782 gcc_assert (token->keyword == RID_THREAD);
26783 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26786 /* Set the location for a declarator specifier and check if it is
26789 DECL_SPECS is the sequence of declarator specifiers onto which to
26792 DS is the single declarator specifier to set which location is to
26793 be set onto the existing sequence of declarators.
26795 LOCATION is the location for the declarator specifier to
26799 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26800 cp_decl_spec ds, cp_token *token)
26802 gcc_assert (ds < ds_last);
26804 if (decl_specs == NULL)
26807 source_location location = token->location;
26809 if (decl_specs->locations[ds] == 0)
26811 decl_specs->locations[ds] = location;
26812 if (ds == ds_thread)
26813 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26819 if (decl_specs->locations[ds_long_long] != 0)
26820 error_at (location,
26821 "%<long long long%> is too long for GCC");
26824 decl_specs->locations[ds_long_long] = location;
26825 pedwarn_cxx98 (location,
26827 "ISO C++ 1998 does not support %<long long%>");
26830 else if (ds == ds_thread)
26832 bool gnu = token_is__thread (token);
26833 if (gnu != decl_specs->gnu_thread_keyword_p)
26834 error_at (location,
26835 "both %<__thread%> and %<thread_local%> specified");
26837 error_at (location, "duplicate %qD", token->u.value);
26841 static const char *const decl_spec_names[] = {
26858 error_at (location,
26859 "duplicate %qs", decl_spec_names[ds]);
26864 /* Return true iff the declarator specifier DS is present in the
26865 sequence of declarator specifiers DECL_SPECS. */
26868 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26871 gcc_assert (ds < ds_last);
26873 if (decl_specs == NULL)
26876 return decl_specs->locations[ds] != 0;
26879 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26880 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26883 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26885 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26888 /* Issue an error message indicating that TOKEN_DESC was expected.
26889 If KEYWORD is true, it indicated this function is called by
26890 cp_parser_require_keword and the required token can only be
26891 a indicated keyword. */
26894 cp_parser_required_error (cp_parser *parser,
26895 required_token token_desc,
26898 switch (token_desc)
26901 cp_parser_error (parser, "expected %<new%>");
26904 cp_parser_error (parser, "expected %<delete%>");
26907 cp_parser_error (parser, "expected %<return%>");
26910 cp_parser_error (parser, "expected %<while%>");
26913 cp_parser_error (parser, "expected %<extern%>");
26915 case RT_STATIC_ASSERT:
26916 cp_parser_error (parser, "expected %<static_assert%>");
26919 cp_parser_error (parser, "expected %<decltype%>");
26922 cp_parser_error (parser, "expected %<operator%>");
26925 cp_parser_error (parser, "expected %<class%>");
26928 cp_parser_error (parser, "expected %<template%>");
26931 cp_parser_error (parser, "expected %<namespace%>");
26934 cp_parser_error (parser, "expected %<using%>");
26937 cp_parser_error (parser, "expected %<asm%>");
26940 cp_parser_error (parser, "expected %<try%>");
26943 cp_parser_error (parser, "expected %<catch%>");
26946 cp_parser_error (parser, "expected %<throw%>");
26949 cp_parser_error (parser, "expected %<__label__%>");
26952 cp_parser_error (parser, "expected %<@try%>");
26954 case RT_AT_SYNCHRONIZED:
26955 cp_parser_error (parser, "expected %<@synchronized%>");
26958 cp_parser_error (parser, "expected %<@throw%>");
26960 case RT_TRANSACTION_ATOMIC:
26961 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26963 case RT_TRANSACTION_RELAXED:
26964 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26971 switch (token_desc)
26974 cp_parser_error (parser, "expected %<;%>");
26976 case RT_OPEN_PAREN:
26977 cp_parser_error (parser, "expected %<(%>");
26979 case RT_CLOSE_BRACE:
26980 cp_parser_error (parser, "expected %<}%>");
26982 case RT_OPEN_BRACE:
26983 cp_parser_error (parser, "expected %<{%>");
26985 case RT_CLOSE_SQUARE:
26986 cp_parser_error (parser, "expected %<]%>");
26988 case RT_OPEN_SQUARE:
26989 cp_parser_error (parser, "expected %<[%>");
26992 cp_parser_error (parser, "expected %<,%>");
26995 cp_parser_error (parser, "expected %<::%>");
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 %<:%>");
27018 case RT_COLON_SCOPE:
27019 cp_parser_error (parser, "expected %<:%> or %<::%>");
27021 case RT_CLOSE_PAREN:
27022 cp_parser_error (parser, "expected %<)%>");
27024 case RT_COMMA_CLOSE_PAREN:
27025 cp_parser_error (parser, "expected %<,%> or %<)%>");
27027 case RT_PRAGMA_EOL:
27028 cp_parser_error (parser, "expected end of line");
27031 cp_parser_error (parser, "expected identifier");
27034 cp_parser_error (parser, "expected selection-statement");
27036 case RT_INTERATION:
27037 cp_parser_error (parser, "expected iteration-statement");
27040 cp_parser_error (parser, "expected jump-statement");
27043 cp_parser_error (parser, "expected class-key");
27045 case RT_CLASS_TYPENAME_TEMPLATE:
27046 cp_parser_error (parser,
27047 "expected %<class%>, %<typename%>, or %<template%>");
27050 gcc_unreachable ();
27054 gcc_unreachable ();
27059 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27060 issue an error message indicating that TOKEN_DESC was expected.
27062 Returns the token consumed, if the token had the appropriate type.
27063 Otherwise, returns NULL. */
27066 cp_parser_require (cp_parser* parser,
27067 enum cpp_ttype type,
27068 required_token token_desc)
27070 if (cp_lexer_next_token_is (parser->lexer, type))
27071 return cp_lexer_consume_token (parser->lexer);
27074 /* Output the MESSAGE -- unless we're parsing tentatively. */
27075 if (!cp_parser_simulate_error (parser))
27076 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27081 /* An error message is produced if the next token is not '>'.
27082 All further tokens are skipped until the desired token is
27083 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27086 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27088 /* Current level of '< ... >'. */
27089 unsigned level = 0;
27090 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27091 unsigned nesting_depth = 0;
27093 /* Are we ready, yet? If not, issue error message. */
27094 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27097 /* Skip tokens until the desired token is found. */
27100 /* Peek at the next token. */
27101 switch (cp_lexer_peek_token (parser->lexer)->type)
27104 if (!nesting_depth)
27109 if (cxx_dialect == cxx98)
27110 /* C++0x views the `>>' operator as two `>' tokens, but
27113 else if (!nesting_depth && level-- == 0)
27115 /* We've hit a `>>' where the first `>' closes the
27116 template argument list, and the second `>' is
27117 spurious. Just consume the `>>' and stop; we've
27118 already produced at least one error. */
27119 cp_lexer_consume_token (parser->lexer);
27122 /* Fall through for C++0x, so we handle the second `>' in
27126 if (!nesting_depth && level-- == 0)
27128 /* We've reached the token we want, consume it and stop. */
27129 cp_lexer_consume_token (parser->lexer);
27134 case CPP_OPEN_PAREN:
27135 case CPP_OPEN_SQUARE:
27139 case CPP_CLOSE_PAREN:
27140 case CPP_CLOSE_SQUARE:
27141 if (nesting_depth-- == 0)
27146 case CPP_PRAGMA_EOL:
27147 case CPP_SEMICOLON:
27148 case CPP_OPEN_BRACE:
27149 case CPP_CLOSE_BRACE:
27150 /* The '>' was probably forgotten, don't look further. */
27157 /* Consume this token. */
27158 cp_lexer_consume_token (parser->lexer);
27162 /* If the next token is the indicated keyword, consume it. Otherwise,
27163 issue an error message indicating that TOKEN_DESC was expected.
27165 Returns the token consumed, if the token had the appropriate type.
27166 Otherwise, returns NULL. */
27169 cp_parser_require_keyword (cp_parser* parser,
27171 required_token token_desc)
27173 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27175 if (token && token->keyword != keyword)
27177 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27184 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27185 function-definition. */
27188 cp_parser_token_starts_function_definition_p (cp_token* token)
27190 return (/* An ordinary function-body begins with an `{'. */
27191 token->type == CPP_OPEN_BRACE
27192 /* A ctor-initializer begins with a `:'. */
27193 || token->type == CPP_COLON
27194 /* A function-try-block begins with `try'. */
27195 || token->keyword == RID_TRY
27196 /* A function-transaction-block begins with `__transaction_atomic'
27197 or `__transaction_relaxed'. */
27198 || token->keyword == RID_TRANSACTION_ATOMIC
27199 || token->keyword == RID_TRANSACTION_RELAXED
27200 /* The named return value extension begins with `return'. */
27201 || token->keyword == RID_RETURN);
27204 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27208 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27212 token = cp_lexer_peek_token (parser->lexer);
27213 return (token->type == CPP_OPEN_BRACE
27214 || (token->type == CPP_COLON
27215 && !parser->colon_doesnt_start_class_def_p));
27218 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27219 C++0x) ending a template-argument. */
27222 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27226 token = cp_lexer_peek_token (parser->lexer);
27227 return (token->type == CPP_COMMA
27228 || token->type == CPP_GREATER
27229 || token->type == CPP_ELLIPSIS
27230 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27233 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27234 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27237 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27242 token = cp_lexer_peek_nth_token (parser->lexer, n);
27243 if (token->type == CPP_LESS)
27245 /* Check for the sequence `<::' in the original code. It would be lexed as
27246 `[:', where `[' is a digraph, and there is no whitespace before
27248 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27251 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27252 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27258 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27259 or none_type otherwise. */
27261 static enum tag_types
27262 cp_parser_token_is_class_key (cp_token* token)
27264 switch (token->keyword)
27269 return record_type;
27278 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27279 or none_type otherwise or if the token is null. */
27281 static enum tag_types
27282 cp_parser_token_is_type_parameter_key (cp_token* token)
27287 switch (token->keyword)
27292 return typename_type;
27299 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27302 cp_parser_check_class_key (enum tag_types class_key, tree type)
27304 if (type == error_mark_node)
27306 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27308 if (permerror (input_location, "%qs tag used in naming %q#T",
27309 class_key == union_type ? "union"
27310 : class_key == record_type ? "struct" : "class",
27312 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27313 "%q#T was previously declared here", type);
27317 /* Issue an error message if DECL is redeclared with different
27318 access than its original declaration [class.access.spec/3].
27319 This applies to nested classes and nested class templates.
27323 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27325 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
27328 if ((TREE_PRIVATE (decl)
27329 != (current_access_specifier == access_private_node))
27330 || (TREE_PROTECTED (decl)
27331 != (current_access_specifier == access_protected_node)))
27332 error_at (location, "%qD redeclared with different access", decl);
27335 /* Look for the `template' keyword, as a syntactic disambiguator.
27336 Return TRUE iff it is present, in which case it will be
27340 cp_parser_optional_template_keyword (cp_parser *parser)
27342 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27344 /* In C++98 the `template' keyword can only be used within templates;
27345 outside templates the parser can always figure out what is a
27346 template and what is not. In C++11, per the resolution of DR 468,
27347 `template' is allowed in cases where it is not strictly necessary. */
27348 if (!processing_template_decl
27349 && pedantic && cxx_dialect == cxx98)
27351 cp_token *token = cp_lexer_peek_token (parser->lexer);
27352 pedwarn (token->location, OPT_Wpedantic,
27353 "in C++98 %<template%> (as a disambiguator) is only "
27354 "allowed within templates");
27355 /* If this part of the token stream is rescanned, the same
27356 error message would be generated. So, we purge the token
27357 from the stream. */
27358 cp_lexer_purge_token (parser->lexer);
27363 /* Consume the `template' keyword. */
27364 cp_lexer_consume_token (parser->lexer);
27371 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27372 set PARSER->SCOPE, and perform other related actions. */
27375 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27377 struct tree_check *check_value;
27379 /* Get the stored value. */
27380 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27381 /* Set the scope from the stored value. */
27382 parser->scope = saved_checks_value (check_value);
27383 parser->qualifying_scope = check_value->qualifying_scope;
27384 parser->object_scope = NULL_TREE;
27387 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27388 encounter the end of a block before what we were looking for. */
27391 cp_parser_cache_group (cp_parser *parser,
27392 enum cpp_ttype end,
27397 cp_token *token = cp_lexer_peek_token (parser->lexer);
27399 /* Abort a parenthesized expression if we encounter a semicolon. */
27400 if ((end == CPP_CLOSE_PAREN || depth == 0)
27401 && token->type == CPP_SEMICOLON)
27403 /* If we've reached the end of the file, stop. */
27404 if (token->type == CPP_EOF
27405 || (end != CPP_PRAGMA_EOL
27406 && token->type == CPP_PRAGMA_EOL))
27408 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27409 /* We've hit the end of an enclosing block, so there's been some
27410 kind of syntax error. */
27413 /* Consume the token. */
27414 cp_lexer_consume_token (parser->lexer);
27415 /* See if it starts a new group. */
27416 if (token->type == CPP_OPEN_BRACE)
27418 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27419 /* In theory this should probably check end == '}', but
27420 cp_parser_save_member_function_body needs it to exit
27421 after either '}' or ')' when called with ')'. */
27425 else if (token->type == CPP_OPEN_PAREN)
27427 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27428 if (depth == 0 && end == CPP_CLOSE_PAREN)
27431 else if (token->type == CPP_PRAGMA)
27432 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27433 else if (token->type == end)
27438 /* Like above, for caching a default argument or NSDMI. Both of these are
27439 terminated by a non-nested comma, but it can be unclear whether or not a
27440 comma is nested in a template argument list unless we do more parsing.
27441 In order to handle this ambiguity, when we encounter a ',' after a '<'
27442 we try to parse what follows as a parameter-declaration-list (in the
27443 case of a default argument) or a member-declarator (in the case of an
27444 NSDMI). If that succeeds, then we stop caching. */
27447 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27449 unsigned depth = 0;
27450 int maybe_template_id = 0;
27451 cp_token *first_token;
27453 tree default_argument;
27455 /* Add tokens until we have processed the entire default
27456 argument. We add the range [first_token, token). */
27457 first_token = cp_lexer_peek_token (parser->lexer);
27458 if (first_token->type == CPP_OPEN_BRACE)
27460 /* For list-initialization, this is straightforward. */
27461 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27462 token = cp_lexer_peek_token (parser->lexer);
27468 /* Peek at the next token. */
27469 token = cp_lexer_peek_token (parser->lexer);
27470 /* What we do depends on what token we have. */
27471 switch (token->type)
27473 /* In valid code, a default argument must be
27474 immediately followed by a `,' `)', or `...'. */
27476 if (depth == 0 && maybe_template_id)
27478 /* If we've seen a '<', we might be in a
27479 template-argument-list. Until Core issue 325 is
27480 resolved, we don't know how this situation ought
27481 to be handled, so try to DTRT. We check whether
27482 what comes after the comma is a valid parameter
27483 declaration list. If it is, then the comma ends
27484 the default argument; otherwise the default
27485 argument continues. */
27486 bool error = false;
27489 /* Set ITALP so cp_parser_parameter_declaration_list
27490 doesn't decide to commit to this parse. */
27491 bool saved_italp = parser->in_template_argument_list_p;
27492 parser->in_template_argument_list_p = true;
27494 cp_parser_parse_tentatively (parser);
27498 /* Parse declarators until we reach a non-comma or
27499 somthing that cannot be an initializer.
27500 Just checking whether we're looking at a single
27501 declarator is insufficient. Consider:
27502 int var = tuple<T,U>::x;
27503 The template parameter 'U' looks exactly like a
27507 int ctor_dtor_or_conv_p;
27508 cp_lexer_consume_token (parser->lexer);
27509 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27510 &ctor_dtor_or_conv_p,
27511 /*parenthesized_p=*/NULL,
27513 /*friend_p=*/false);
27514 peek = cp_lexer_peek_token (parser->lexer);
27515 if (cp_parser_error_occurred (parser))
27518 while (peek->type == CPP_COMMA);
27519 /* If we met an '=' or ';' then the original comma
27520 was the end of the NSDMI. Otherwise assume
27521 we're still in the NSDMI. */
27522 error = (peek->type != CPP_EQ
27523 && peek->type != CPP_SEMICOLON);
27527 cp_lexer_consume_token (parser->lexer);
27528 begin_scope (sk_function_parms, NULL_TREE);
27529 cp_parser_parameter_declaration_list (parser, &error);
27530 pop_bindings_and_leave_scope ();
27532 if (!cp_parser_error_occurred (parser) && !error)
27534 cp_parser_abort_tentative_parse (parser);
27536 parser->in_template_argument_list_p = saved_italp;
27539 case CPP_CLOSE_PAREN:
27541 /* If we run into a non-nested `;', `}', or `]',
27542 then the code is invalid -- but the default
27543 argument is certainly over. */
27544 case CPP_SEMICOLON:
27545 case CPP_CLOSE_BRACE:
27546 case CPP_CLOSE_SQUARE:
27548 /* Handle correctly int n = sizeof ... ( p ); */
27549 && token->type != CPP_ELLIPSIS)
27551 /* Update DEPTH, if necessary. */
27552 else if (token->type == CPP_CLOSE_PAREN
27553 || token->type == CPP_CLOSE_BRACE
27554 || token->type == CPP_CLOSE_SQUARE)
27558 case CPP_OPEN_PAREN:
27559 case CPP_OPEN_SQUARE:
27560 case CPP_OPEN_BRACE:
27566 /* This might be the comparison operator, or it might
27567 start a template argument list. */
27568 ++maybe_template_id;
27572 if (cxx_dialect == cxx98)
27574 /* Fall through for C++0x, which treats the `>>'
27575 operator like two `>' tokens in certain
27581 /* This might be an operator, or it might close a
27582 template argument list. But if a previous '<'
27583 started a template argument list, this will have
27584 closed it, so we can't be in one anymore. */
27585 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27586 if (maybe_template_id < 0)
27587 maybe_template_id = 0;
27591 /* If we run out of tokens, issue an error message. */
27593 case CPP_PRAGMA_EOL:
27594 error_at (token->location, "file ends in default argument");
27595 return error_mark_node;
27599 /* In these cases, we should look for template-ids.
27600 For example, if the default argument is
27601 `X<int, double>()', we need to do name lookup to
27602 figure out whether or not `X' is a template; if
27603 so, the `,' does not end the default argument.
27605 That is not yet done. */
27612 /* If we've reached the end, stop. */
27616 /* Add the token to the token block. */
27617 token = cp_lexer_consume_token (parser->lexer);
27620 /* Create a DEFAULT_ARG to represent the unparsed default
27622 default_argument = make_node (DEFAULT_ARG);
27623 DEFARG_TOKENS (default_argument)
27624 = cp_token_cache_new (first_token, token);
27625 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27627 return default_argument;
27630 /* Begin parsing tentatively. We always save tokens while parsing
27631 tentatively so that if the tentative parsing fails we can restore the
27635 cp_parser_parse_tentatively (cp_parser* parser)
27637 /* Enter a new parsing context. */
27638 parser->context = cp_parser_context_new (parser->context);
27639 /* Begin saving tokens. */
27640 cp_lexer_save_tokens (parser->lexer);
27641 /* In order to avoid repetitive access control error messages,
27642 access checks are queued up until we are no longer parsing
27644 push_deferring_access_checks (dk_deferred);
27647 /* Commit to the currently active tentative parse. */
27650 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27652 cp_parser_context *context;
27655 /* Mark all of the levels as committed. */
27656 lexer = parser->lexer;
27657 for (context = parser->context; context->next; context = context->next)
27659 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27661 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27662 while (!cp_lexer_saving_tokens (lexer))
27663 lexer = lexer->next;
27664 cp_lexer_commit_tokens (lexer);
27668 /* Commit to the topmost currently active tentative parse.
27670 Note that this function shouldn't be called when there are
27671 irreversible side-effects while in a tentative state. For
27672 example, we shouldn't create a permanent entry in the symbol
27673 table, or issue an error message that might not apply if the
27674 tentative parse is aborted. */
27677 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27679 cp_parser_context *context = parser->context;
27680 cp_lexer *lexer = parser->lexer;
27684 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27686 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27688 while (!cp_lexer_saving_tokens (lexer))
27689 lexer = lexer->next;
27690 cp_lexer_commit_tokens (lexer);
27694 /* Abort the currently active tentative parse. All consumed tokens
27695 will be rolled back, and no diagnostics will be issued. */
27698 cp_parser_abort_tentative_parse (cp_parser* parser)
27700 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27701 || errorcount > 0);
27702 cp_parser_simulate_error (parser);
27703 /* Now, pretend that we want to see if the construct was
27704 successfully parsed. */
27705 cp_parser_parse_definitely (parser);
27708 /* Stop parsing tentatively. If a parse error has occurred, restore the
27709 token stream. Otherwise, commit to the tokens we have consumed.
27710 Returns true if no error occurred; false otherwise. */
27713 cp_parser_parse_definitely (cp_parser* parser)
27715 bool error_occurred;
27716 cp_parser_context *context;
27718 /* Remember whether or not an error occurred, since we are about to
27719 destroy that information. */
27720 error_occurred = cp_parser_error_occurred (parser);
27721 /* Remove the topmost context from the stack. */
27722 context = parser->context;
27723 parser->context = context->next;
27724 /* If no parse errors occurred, commit to the tentative parse. */
27725 if (!error_occurred)
27727 /* Commit to the tokens read tentatively, unless that was
27729 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27730 cp_lexer_commit_tokens (parser->lexer);
27732 pop_to_parent_deferring_access_checks ();
27734 /* Otherwise, if errors occurred, roll back our state so that things
27735 are just as they were before we began the tentative parse. */
27738 cp_lexer_rollback_tokens (parser->lexer);
27739 pop_deferring_access_checks ();
27741 /* Add the context to the front of the free list. */
27742 context->next = cp_parser_context_free_list;
27743 cp_parser_context_free_list = context;
27745 return !error_occurred;
27748 /* Returns true if we are parsing tentatively and are not committed to
27749 this tentative parse. */
27752 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27754 return (cp_parser_parsing_tentatively (parser)
27755 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27758 /* Returns nonzero iff an error has occurred during the most recent
27759 tentative parse. */
27762 cp_parser_error_occurred (cp_parser* parser)
27764 return (cp_parser_parsing_tentatively (parser)
27765 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27768 /* Returns nonzero if GNU extensions are allowed. */
27771 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27773 return parser->allow_gnu_extensions_p;
27776 /* Objective-C++ Productions */
27779 /* Parse an Objective-C expression, which feeds into a primary-expression
27783 objc-message-expression
27784 objc-string-literal
27785 objc-encode-expression
27786 objc-protocol-expression
27787 objc-selector-expression
27789 Returns a tree representation of the expression. */
27792 cp_parser_objc_expression (cp_parser* parser)
27794 /* Try to figure out what kind of declaration is present. */
27795 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27799 case CPP_OPEN_SQUARE:
27800 return cp_parser_objc_message_expression (parser);
27802 case CPP_OBJC_STRING:
27803 kwd = cp_lexer_consume_token (parser->lexer);
27804 return objc_build_string_object (kwd->u.value);
27807 switch (kwd->keyword)
27809 case RID_AT_ENCODE:
27810 return cp_parser_objc_encode_expression (parser);
27812 case RID_AT_PROTOCOL:
27813 return cp_parser_objc_protocol_expression (parser);
27815 case RID_AT_SELECTOR:
27816 return cp_parser_objc_selector_expression (parser);
27822 error_at (kwd->location,
27823 "misplaced %<@%D%> Objective-C++ construct",
27825 cp_parser_skip_to_end_of_block_or_statement (parser);
27828 return error_mark_node;
27831 /* Parse an Objective-C message expression.
27833 objc-message-expression:
27834 [ objc-message-receiver objc-message-args ]
27836 Returns a representation of an Objective-C message. */
27839 cp_parser_objc_message_expression (cp_parser* parser)
27841 tree receiver, messageargs;
27843 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27844 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27845 receiver = cp_parser_objc_message_receiver (parser);
27846 messageargs = cp_parser_objc_message_args (parser);
27847 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27848 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27850 tree result = objc_build_message_expr (receiver, messageargs);
27852 /* Construct a location e.g.
27855 ranging from the '[' to the ']', with the caret at the start. */
27856 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27857 protected_set_expr_location (result, combined_loc);
27862 /* Parse an objc-message-receiver.
27864 objc-message-receiver:
27866 simple-type-specifier
27868 Returns a representation of the type or expression. */
27871 cp_parser_objc_message_receiver (cp_parser* parser)
27875 /* An Objective-C message receiver may be either (1) a type
27876 or (2) an expression. */
27877 cp_parser_parse_tentatively (parser);
27878 rcv = cp_parser_expression (parser);
27880 /* If that worked out, fine. */
27881 if (cp_parser_parse_definitely (parser))
27884 cp_parser_parse_tentatively (parser);
27885 rcv = cp_parser_simple_type_specifier (parser,
27886 /*decl_specs=*/NULL,
27887 CP_PARSER_FLAGS_NONE);
27889 if (cp_parser_parse_definitely (parser))
27890 return objc_get_class_reference (rcv);
27892 cp_parser_error (parser, "objective-c++ message receiver expected");
27893 return error_mark_node;
27896 /* Parse the arguments and selectors comprising an Objective-C message.
27901 objc-selector-args , objc-comma-args
27903 objc-selector-args:
27904 objc-selector [opt] : assignment-expression
27905 objc-selector-args objc-selector [opt] : assignment-expression
27908 assignment-expression
27909 objc-comma-args , assignment-expression
27911 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27912 selector arguments and TREE_VALUE containing a list of comma
27916 cp_parser_objc_message_args (cp_parser* parser)
27918 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27919 bool maybe_unary_selector_p = true;
27920 cp_token *token = cp_lexer_peek_token (parser->lexer);
27922 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27924 tree selector = NULL_TREE, arg;
27926 if (token->type != CPP_COLON)
27927 selector = cp_parser_objc_selector (parser);
27929 /* Detect if we have a unary selector. */
27930 if (maybe_unary_selector_p
27931 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27932 return build_tree_list (selector, NULL_TREE);
27934 maybe_unary_selector_p = false;
27935 cp_parser_require (parser, CPP_COLON, RT_COLON);
27936 arg = cp_parser_assignment_expression (parser);
27939 = chainon (sel_args,
27940 build_tree_list (selector, arg));
27942 token = cp_lexer_peek_token (parser->lexer);
27945 /* Handle non-selector arguments, if any. */
27946 while (token->type == CPP_COMMA)
27950 cp_lexer_consume_token (parser->lexer);
27951 arg = cp_parser_assignment_expression (parser);
27954 = chainon (addl_args,
27955 build_tree_list (NULL_TREE, arg));
27957 token = cp_lexer_peek_token (parser->lexer);
27960 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27962 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27963 return build_tree_list (error_mark_node, error_mark_node);
27966 return build_tree_list (sel_args, addl_args);
27969 /* Parse an Objective-C encode expression.
27971 objc-encode-expression:
27972 @encode objc-typename
27974 Returns an encoded representation of the type argument. */
27977 cp_parser_objc_encode_expression (cp_parser* parser)
27981 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27983 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27984 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27985 token = cp_lexer_peek_token (parser->lexer);
27986 type = complete_type (cp_parser_type_id (parser));
27987 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27991 error_at (token->location,
27992 "%<@encode%> must specify a type as an argument");
27993 return error_mark_node;
27996 /* This happens if we find @encode(T) (where T is a template
27997 typename or something dependent on a template typename) when
27998 parsing a template. In that case, we can't compile it
27999 immediately, but we rather create an AT_ENCODE_EXPR which will
28000 need to be instantiated when the template is used.
28002 if (dependent_type_p (type))
28004 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28005 TREE_READONLY (value) = 1;
28010 /* Build a location of the form:
28013 with caret==start at the @ token, finishing at the close paren. */
28014 location_t combined_loc
28015 = make_location (start_loc, start_loc,
28016 cp_lexer_previous_token (parser->lexer)->location);
28018 return cp_expr (objc_build_encode_expr (type), combined_loc);
28021 /* Parse an Objective-C @defs expression. */
28024 cp_parser_objc_defs_expression (cp_parser *parser)
28028 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28029 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28030 name = cp_parser_identifier (parser);
28031 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28033 return objc_get_class_ivars (name);
28036 /* Parse an Objective-C protocol expression.
28038 objc-protocol-expression:
28039 @protocol ( identifier )
28041 Returns a representation of the protocol expression. */
28044 cp_parser_objc_protocol_expression (cp_parser* parser)
28047 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28049 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28050 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28051 proto = cp_parser_identifier (parser);
28052 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28054 /* Build a location of the form:
28057 with caret==start at the @ token, finishing at the close paren. */
28058 location_t combined_loc
28059 = make_location (start_loc, start_loc,
28060 cp_lexer_previous_token (parser->lexer)->location);
28061 tree result = objc_build_protocol_expr (proto);
28062 protected_set_expr_location (result, combined_loc);
28066 /* Parse an Objective-C selector expression.
28068 objc-selector-expression:
28069 @selector ( objc-method-signature )
28071 objc-method-signature:
28077 objc-selector-seq objc-selector :
28079 Returns a representation of the method selector. */
28082 cp_parser_objc_selector_expression (cp_parser* parser)
28084 tree sel_seq = NULL_TREE;
28085 bool maybe_unary_selector_p = true;
28087 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28089 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28090 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28091 token = cp_lexer_peek_token (parser->lexer);
28093 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28094 || token->type == CPP_SCOPE)
28096 tree selector = NULL_TREE;
28098 if (token->type != CPP_COLON
28099 || token->type == CPP_SCOPE)
28100 selector = cp_parser_objc_selector (parser);
28102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28103 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28105 /* Detect if we have a unary selector. */
28106 if (maybe_unary_selector_p)
28108 sel_seq = selector;
28109 goto finish_selector;
28113 cp_parser_error (parser, "expected %<:%>");
28116 maybe_unary_selector_p = false;
28117 token = cp_lexer_consume_token (parser->lexer);
28119 if (token->type == CPP_SCOPE)
28122 = chainon (sel_seq,
28123 build_tree_list (selector, NULL_TREE));
28125 = chainon (sel_seq,
28126 build_tree_list (NULL_TREE, NULL_TREE));
28130 = chainon (sel_seq,
28131 build_tree_list (selector, NULL_TREE));
28133 token = cp_lexer_peek_token (parser->lexer);
28137 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28140 /* Build a location of the form:
28143 with caret==start at the @ token, finishing at the close paren. */
28144 location_t combined_loc
28145 = make_location (loc, loc,
28146 cp_lexer_previous_token (parser->lexer)->location);
28147 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28148 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28149 protected_set_expr_location (result, combined_loc);
28153 /* Parse a list of identifiers.
28155 objc-identifier-list:
28157 objc-identifier-list , identifier
28159 Returns a TREE_LIST of identifier nodes. */
28162 cp_parser_objc_identifier_list (cp_parser* parser)
28168 identifier = cp_parser_identifier (parser);
28169 if (identifier == error_mark_node)
28170 return error_mark_node;
28172 list = build_tree_list (NULL_TREE, identifier);
28173 sep = cp_lexer_peek_token (parser->lexer);
28175 while (sep->type == CPP_COMMA)
28177 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28178 identifier = cp_parser_identifier (parser);
28179 if (identifier == error_mark_node)
28182 list = chainon (list, build_tree_list (NULL_TREE,
28184 sep = cp_lexer_peek_token (parser->lexer);
28190 /* Parse an Objective-C alias declaration.
28192 objc-alias-declaration:
28193 @compatibility_alias identifier identifier ;
28195 This function registers the alias mapping with the Objective-C front end.
28196 It returns nothing. */
28199 cp_parser_objc_alias_declaration (cp_parser* parser)
28203 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28204 alias = cp_parser_identifier (parser);
28205 orig = cp_parser_identifier (parser);
28206 objc_declare_alias (alias, orig);
28207 cp_parser_consume_semicolon_at_end_of_statement (parser);
28210 /* Parse an Objective-C class forward-declaration.
28212 objc-class-declaration:
28213 @class objc-identifier-list ;
28215 The function registers the forward declarations with the Objective-C
28216 front end. It returns nothing. */
28219 cp_parser_objc_class_declaration (cp_parser* parser)
28221 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28226 id = cp_parser_identifier (parser);
28227 if (id == error_mark_node)
28230 objc_declare_class (id);
28232 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28233 cp_lexer_consume_token (parser->lexer);
28237 cp_parser_consume_semicolon_at_end_of_statement (parser);
28240 /* Parse a list of Objective-C protocol references.
28242 objc-protocol-refs-opt:
28243 objc-protocol-refs [opt]
28245 objc-protocol-refs:
28246 < objc-identifier-list >
28248 Returns a TREE_LIST of identifiers, if any. */
28251 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28253 tree protorefs = NULL_TREE;
28255 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28257 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28258 protorefs = cp_parser_objc_identifier_list (parser);
28259 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28265 /* Parse a Objective-C visibility specification. */
28268 cp_parser_objc_visibility_spec (cp_parser* parser)
28270 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28272 switch (vis->keyword)
28274 case RID_AT_PRIVATE:
28275 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28277 case RID_AT_PROTECTED:
28278 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28280 case RID_AT_PUBLIC:
28281 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28283 case RID_AT_PACKAGE:
28284 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28290 /* Eat '@private'/'@protected'/'@public'. */
28291 cp_lexer_consume_token (parser->lexer);
28294 /* Parse an Objective-C method type. Return 'true' if it is a class
28295 (+) method, and 'false' if it is an instance (-) method. */
28298 cp_parser_objc_method_type (cp_parser* parser)
28300 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28306 /* Parse an Objective-C protocol qualifier. */
28309 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28311 tree quals = NULL_TREE, node;
28312 cp_token *token = cp_lexer_peek_token (parser->lexer);
28314 node = token->u.value;
28316 while (node && identifier_p (node)
28317 && (node == ridpointers [(int) RID_IN]
28318 || node == ridpointers [(int) RID_OUT]
28319 || node == ridpointers [(int) RID_INOUT]
28320 || node == ridpointers [(int) RID_BYCOPY]
28321 || node == ridpointers [(int) RID_BYREF]
28322 || node == ridpointers [(int) RID_ONEWAY]))
28324 quals = tree_cons (NULL_TREE, node, quals);
28325 cp_lexer_consume_token (parser->lexer);
28326 token = cp_lexer_peek_token (parser->lexer);
28327 node = token->u.value;
28333 /* Parse an Objective-C typename. */
28336 cp_parser_objc_typename (cp_parser* parser)
28338 tree type_name = NULL_TREE;
28340 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28342 tree proto_quals, cp_type = NULL_TREE;
28344 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28345 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28347 /* An ObjC type name may consist of just protocol qualifiers, in which
28348 case the type shall default to 'id'. */
28349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28351 cp_type = cp_parser_type_id (parser);
28353 /* If the type could not be parsed, an error has already
28354 been produced. For error recovery, behave as if it had
28355 not been specified, which will use the default type
28357 if (cp_type == error_mark_node)
28359 cp_type = NULL_TREE;
28360 /* We need to skip to the closing parenthesis as
28361 cp_parser_type_id() does not seem to do it for
28363 cp_parser_skip_to_closing_parenthesis (parser,
28364 /*recovering=*/true,
28365 /*or_comma=*/false,
28366 /*consume_paren=*/false);
28370 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28371 type_name = build_tree_list (proto_quals, cp_type);
28377 /* Check to see if TYPE refers to an Objective-C selector name. */
28380 cp_parser_objc_selector_p (enum cpp_ttype type)
28382 return (type == CPP_NAME || type == CPP_KEYWORD
28383 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28384 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28385 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28386 || type == CPP_XOR || type == CPP_XOR_EQ);
28389 /* Parse an Objective-C selector. */
28392 cp_parser_objc_selector (cp_parser* parser)
28394 cp_token *token = cp_lexer_consume_token (parser->lexer);
28396 if (!cp_parser_objc_selector_p (token->type))
28398 error_at (token->location, "invalid Objective-C++ selector name");
28399 return error_mark_node;
28402 /* C++ operator names are allowed to appear in ObjC selectors. */
28403 switch (token->type)
28405 case CPP_AND_AND: return get_identifier ("and");
28406 case CPP_AND_EQ: return get_identifier ("and_eq");
28407 case CPP_AND: return get_identifier ("bitand");
28408 case CPP_OR: return get_identifier ("bitor");
28409 case CPP_COMPL: return get_identifier ("compl");
28410 case CPP_NOT: return get_identifier ("not");
28411 case CPP_NOT_EQ: return get_identifier ("not_eq");
28412 case CPP_OR_OR: return get_identifier ("or");
28413 case CPP_OR_EQ: return get_identifier ("or_eq");
28414 case CPP_XOR: return get_identifier ("xor");
28415 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28416 default: return token->u.value;
28420 /* Parse an Objective-C params list. */
28423 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28425 tree params = NULL_TREE;
28426 bool maybe_unary_selector_p = true;
28427 cp_token *token = cp_lexer_peek_token (parser->lexer);
28429 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28431 tree selector = NULL_TREE, type_name, identifier;
28432 tree parm_attr = NULL_TREE;
28434 if (token->keyword == RID_ATTRIBUTE)
28437 if (token->type != CPP_COLON)
28438 selector = cp_parser_objc_selector (parser);
28440 /* Detect if we have a unary selector. */
28441 if (maybe_unary_selector_p
28442 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28444 params = selector; /* Might be followed by attributes. */
28448 maybe_unary_selector_p = false;
28449 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28451 /* Something went quite wrong. There should be a colon
28452 here, but there is not. Stop parsing parameters. */
28455 type_name = cp_parser_objc_typename (parser);
28456 /* New ObjC allows attributes on parameters too. */
28457 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28458 parm_attr = cp_parser_attributes_opt (parser);
28459 identifier = cp_parser_identifier (parser);
28463 objc_build_keyword_decl (selector,
28468 token = cp_lexer_peek_token (parser->lexer);
28471 if (params == NULL_TREE)
28473 cp_parser_error (parser, "objective-c++ method declaration is expected");
28474 return error_mark_node;
28477 /* We allow tail attributes for the method. */
28478 if (token->keyword == RID_ATTRIBUTE)
28480 *attributes = cp_parser_attributes_opt (parser);
28481 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28482 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28484 cp_parser_error (parser,
28485 "method attributes must be specified at the end");
28486 return error_mark_node;
28489 if (params == NULL_TREE)
28491 cp_parser_error (parser, "objective-c++ method declaration is expected");
28492 return error_mark_node;
28497 /* Parse the non-keyword Objective-C params. */
28500 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28503 tree params = make_node (TREE_LIST);
28504 cp_token *token = cp_lexer_peek_token (parser->lexer);
28505 *ellipsisp = false; /* Initially, assume no ellipsis. */
28507 while (token->type == CPP_COMMA)
28509 cp_parameter_declarator *parmdecl;
28512 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28513 token = cp_lexer_peek_token (parser->lexer);
28515 if (token->type == CPP_ELLIPSIS)
28517 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28519 token = cp_lexer_peek_token (parser->lexer);
28523 /* TODO: parse attributes for tail parameters. */
28524 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28525 parm = grokdeclarator (parmdecl->declarator,
28526 &parmdecl->decl_specifiers,
28527 PARM, /*initialized=*/0,
28528 /*attrlist=*/NULL);
28530 chainon (params, build_tree_list (NULL_TREE, parm));
28531 token = cp_lexer_peek_token (parser->lexer);
28534 /* We allow tail attributes for the method. */
28535 if (token->keyword == RID_ATTRIBUTE)
28537 if (*attributes == NULL_TREE)
28539 *attributes = cp_parser_attributes_opt (parser);
28540 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28541 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28545 /* We have an error, but parse the attributes, so that we can
28547 *attributes = cp_parser_attributes_opt (parser);
28549 cp_parser_error (parser,
28550 "method attributes must be specified at the end");
28551 return error_mark_node;
28557 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28560 cp_parser_objc_interstitial_code (cp_parser* parser)
28562 cp_token *token = cp_lexer_peek_token (parser->lexer);
28564 /* If the next token is `extern' and the following token is a string
28565 literal, then we have a linkage specification. */
28566 if (token->keyword == RID_EXTERN
28567 && cp_parser_is_pure_string_literal
28568 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28569 cp_parser_linkage_specification (parser);
28570 /* Handle #pragma, if any. */
28571 else if (token->type == CPP_PRAGMA)
28572 cp_parser_pragma (parser, pragma_objc_icode, NULL);
28573 /* Allow stray semicolons. */
28574 else if (token->type == CPP_SEMICOLON)
28575 cp_lexer_consume_token (parser->lexer);
28576 /* Mark methods as optional or required, when building protocols. */
28577 else if (token->keyword == RID_AT_OPTIONAL)
28579 cp_lexer_consume_token (parser->lexer);
28580 objc_set_method_opt (true);
28582 else if (token->keyword == RID_AT_REQUIRED)
28584 cp_lexer_consume_token (parser->lexer);
28585 objc_set_method_opt (false);
28587 else if (token->keyword == RID_NAMESPACE)
28588 cp_parser_namespace_definition (parser);
28589 /* Other stray characters must generate errors. */
28590 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28592 cp_lexer_consume_token (parser->lexer);
28593 error ("stray %qs between Objective-C++ methods",
28594 token->type == CPP_OPEN_BRACE ? "{" : "}");
28596 /* Finally, try to parse a block-declaration, or a function-definition. */
28598 cp_parser_block_declaration (parser, /*statement_p=*/false);
28601 /* Parse a method signature. */
28604 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28606 tree rettype, kwdparms, optparms;
28607 bool ellipsis = false;
28608 bool is_class_method;
28610 is_class_method = cp_parser_objc_method_type (parser);
28611 rettype = cp_parser_objc_typename (parser);
28612 *attributes = NULL_TREE;
28613 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28614 if (kwdparms == error_mark_node)
28615 return error_mark_node;
28616 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28617 if (optparms == error_mark_node)
28618 return error_mark_node;
28620 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28624 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28627 cp_lexer_save_tokens (parser->lexer);
28628 tattr = cp_parser_attributes_opt (parser);
28629 gcc_assert (tattr) ;
28631 /* If the attributes are followed by a method introducer, this is not allowed.
28632 Dump the attributes and flag the situation. */
28633 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28634 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28637 /* Otherwise, the attributes introduce some interstitial code, possibly so
28638 rewind to allow that check. */
28639 cp_lexer_rollback_tokens (parser->lexer);
28643 /* Parse an Objective-C method prototype list. */
28646 cp_parser_objc_method_prototype_list (cp_parser* parser)
28648 cp_token *token = cp_lexer_peek_token (parser->lexer);
28650 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28652 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28654 tree attributes, sig;
28655 bool is_class_method;
28656 if (token->type == CPP_PLUS)
28657 is_class_method = true;
28659 is_class_method = false;
28660 sig = cp_parser_objc_method_signature (parser, &attributes);
28661 if (sig == error_mark_node)
28663 cp_parser_skip_to_end_of_block_or_statement (parser);
28664 token = cp_lexer_peek_token (parser->lexer);
28667 objc_add_method_declaration (is_class_method, sig, attributes);
28668 cp_parser_consume_semicolon_at_end_of_statement (parser);
28670 else if (token->keyword == RID_AT_PROPERTY)
28671 cp_parser_objc_at_property_declaration (parser);
28672 else if (token->keyword == RID_ATTRIBUTE
28673 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28674 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28676 "prefix attributes are ignored for methods");
28678 /* Allow for interspersed non-ObjC++ code. */
28679 cp_parser_objc_interstitial_code (parser);
28681 token = cp_lexer_peek_token (parser->lexer);
28684 if (token->type != CPP_EOF)
28685 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28687 cp_parser_error (parser, "expected %<@end%>");
28689 objc_finish_interface ();
28692 /* Parse an Objective-C method definition list. */
28695 cp_parser_objc_method_definition_list (cp_parser* parser)
28697 cp_token *token = cp_lexer_peek_token (parser->lexer);
28699 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28703 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28706 tree sig, attribute;
28707 bool is_class_method;
28708 if (token->type == CPP_PLUS)
28709 is_class_method = true;
28711 is_class_method = false;
28712 push_deferring_access_checks (dk_deferred);
28713 sig = cp_parser_objc_method_signature (parser, &attribute);
28714 if (sig == error_mark_node)
28716 cp_parser_skip_to_end_of_block_or_statement (parser);
28717 token = cp_lexer_peek_token (parser->lexer);
28720 objc_start_method_definition (is_class_method, sig, attribute,
28723 /* For historical reasons, we accept an optional semicolon. */
28724 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28725 cp_lexer_consume_token (parser->lexer);
28727 ptk = cp_lexer_peek_token (parser->lexer);
28728 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28729 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28731 perform_deferred_access_checks (tf_warning_or_error);
28732 stop_deferring_access_checks ();
28733 meth = cp_parser_function_definition_after_declarator (parser,
28735 pop_deferring_access_checks ();
28736 objc_finish_method_definition (meth);
28739 /* The following case will be removed once @synthesize is
28740 completely implemented. */
28741 else if (token->keyword == RID_AT_PROPERTY)
28742 cp_parser_objc_at_property_declaration (parser);
28743 else if (token->keyword == RID_AT_SYNTHESIZE)
28744 cp_parser_objc_at_synthesize_declaration (parser);
28745 else if (token->keyword == RID_AT_DYNAMIC)
28746 cp_parser_objc_at_dynamic_declaration (parser);
28747 else if (token->keyword == RID_ATTRIBUTE
28748 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28749 warning_at (token->location, OPT_Wattributes,
28750 "prefix attributes are ignored for methods");
28752 /* Allow for interspersed non-ObjC++ code. */
28753 cp_parser_objc_interstitial_code (parser);
28755 token = cp_lexer_peek_token (parser->lexer);
28758 if (token->type != CPP_EOF)
28759 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28761 cp_parser_error (parser, "expected %<@end%>");
28763 objc_finish_implementation ();
28766 /* Parse Objective-C ivars. */
28769 cp_parser_objc_class_ivars (cp_parser* parser)
28771 cp_token *token = cp_lexer_peek_token (parser->lexer);
28773 if (token->type != CPP_OPEN_BRACE)
28774 return; /* No ivars specified. */
28776 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28777 token = cp_lexer_peek_token (parser->lexer);
28779 while (token->type != CPP_CLOSE_BRACE
28780 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28782 cp_decl_specifier_seq declspecs;
28783 int decl_class_or_enum_p;
28784 tree prefix_attributes;
28786 cp_parser_objc_visibility_spec (parser);
28788 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28791 cp_parser_decl_specifier_seq (parser,
28792 CP_PARSER_FLAGS_OPTIONAL,
28794 &decl_class_or_enum_p);
28796 /* auto, register, static, extern, mutable. */
28797 if (declspecs.storage_class != sc_none)
28799 cp_parser_error (parser, "invalid type for instance variable");
28800 declspecs.storage_class = sc_none;
28803 /* thread_local. */
28804 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28806 cp_parser_error (parser, "invalid type for instance variable");
28807 declspecs.locations[ds_thread] = 0;
28811 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28813 cp_parser_error (parser, "invalid type for instance variable");
28814 declspecs.locations[ds_typedef] = 0;
28817 prefix_attributes = declspecs.attributes;
28818 declspecs.attributes = NULL_TREE;
28820 /* Keep going until we hit the `;' at the end of the
28822 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28824 tree width = NULL_TREE, attributes, first_attribute, decl;
28825 cp_declarator *declarator = NULL;
28826 int ctor_dtor_or_conv_p;
28828 /* Check for a (possibly unnamed) bitfield declaration. */
28829 token = cp_lexer_peek_token (parser->lexer);
28830 if (token->type == CPP_COLON)
28833 if (token->type == CPP_NAME
28834 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28837 /* Get the name of the bitfield. */
28838 declarator = make_id_declarator (NULL_TREE,
28839 cp_parser_identifier (parser),
28843 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28844 /* Get the width of the bitfield. */
28846 = cp_parser_constant_expression (parser);
28850 /* Parse the declarator. */
28852 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28853 &ctor_dtor_or_conv_p,
28854 /*parenthesized_p=*/NULL,
28855 /*member_p=*/false,
28856 /*friend_p=*/false);
28859 /* Look for attributes that apply to the ivar. */
28860 attributes = cp_parser_attributes_opt (parser);
28861 /* Remember which attributes are prefix attributes and
28863 first_attribute = attributes;
28864 /* Combine the attributes. */
28865 attributes = chainon (prefix_attributes, attributes);
28868 /* Create the bitfield declaration. */
28869 decl = grokbitfield (declarator, &declspecs,
28873 decl = grokfield (declarator, &declspecs,
28874 NULL_TREE, /*init_const_expr_p=*/false,
28875 NULL_TREE, attributes);
28877 /* Add the instance variable. */
28878 if (decl != error_mark_node && decl != NULL_TREE)
28879 objc_add_instance_variable (decl);
28881 /* Reset PREFIX_ATTRIBUTES. */
28882 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28883 attributes = TREE_CHAIN (attributes);
28885 TREE_CHAIN (attributes) = NULL_TREE;
28887 token = cp_lexer_peek_token (parser->lexer);
28889 if (token->type == CPP_COMMA)
28891 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28897 cp_parser_consume_semicolon_at_end_of_statement (parser);
28898 token = cp_lexer_peek_token (parser->lexer);
28901 if (token->keyword == RID_AT_END)
28902 cp_parser_error (parser, "expected %<}%>");
28904 /* Do not consume the RID_AT_END, so it will be read again as terminating
28905 the @interface of @implementation. */
28906 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28907 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28909 /* For historical reasons, we accept an optional semicolon. */
28910 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28911 cp_lexer_consume_token (parser->lexer);
28914 /* Parse an Objective-C protocol declaration. */
28917 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28919 tree proto, protorefs;
28922 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28923 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28925 tok = cp_lexer_peek_token (parser->lexer);
28926 error_at (tok->location, "identifier expected after %<@protocol%>");
28927 cp_parser_consume_semicolon_at_end_of_statement (parser);
28931 /* See if we have a forward declaration or a definition. */
28932 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28934 /* Try a forward declaration first. */
28935 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28941 id = cp_parser_identifier (parser);
28942 if (id == error_mark_node)
28945 objc_declare_protocol (id, attributes);
28947 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28948 cp_lexer_consume_token (parser->lexer);
28952 cp_parser_consume_semicolon_at_end_of_statement (parser);
28955 /* Ok, we got a full-fledged definition (or at least should). */
28958 proto = cp_parser_identifier (parser);
28959 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28960 objc_start_protocol (proto, protorefs, attributes);
28961 cp_parser_objc_method_prototype_list (parser);
28965 /* Parse an Objective-C superclass or category. */
28968 cp_parser_objc_superclass_or_category (cp_parser *parser,
28971 tree *categ, bool *is_class_extension)
28973 cp_token *next = cp_lexer_peek_token (parser->lexer);
28975 *super = *categ = NULL_TREE;
28976 *is_class_extension = false;
28977 if (next->type == CPP_COLON)
28979 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28980 *super = cp_parser_identifier (parser);
28982 else if (next->type == CPP_OPEN_PAREN)
28984 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28986 /* If there is no category name, and this is an @interface, we
28987 have a class extension. */
28988 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28990 *categ = NULL_TREE;
28991 *is_class_extension = true;
28994 *categ = cp_parser_identifier (parser);
28996 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29000 /* Parse an Objective-C class interface. */
29003 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29005 tree name, super, categ, protos;
29006 bool is_class_extension;
29008 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29009 name = cp_parser_identifier (parser);
29010 if (name == error_mark_node)
29012 /* It's hard to recover because even if valid @interface stuff
29013 is to follow, we can't compile it (or validate it) if we
29014 don't even know which class it refers to. Let's assume this
29015 was a stray '@interface' token in the stream and skip it.
29019 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29020 &is_class_extension);
29021 protos = cp_parser_objc_protocol_refs_opt (parser);
29023 /* We have either a class or a category on our hands. */
29024 if (categ || is_class_extension)
29025 objc_start_category_interface (name, categ, protos, attributes);
29028 objc_start_class_interface (name, super, protos, attributes);
29029 /* Handle instance variable declarations, if any. */
29030 cp_parser_objc_class_ivars (parser);
29031 objc_continue_interface ();
29034 cp_parser_objc_method_prototype_list (parser);
29037 /* Parse an Objective-C class implementation. */
29040 cp_parser_objc_class_implementation (cp_parser* parser)
29042 tree name, super, categ;
29043 bool is_class_extension;
29045 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29046 name = cp_parser_identifier (parser);
29047 if (name == error_mark_node)
29049 /* It's hard to recover because even if valid @implementation
29050 stuff is to follow, we can't compile it (or validate it) if
29051 we don't even know which class it refers to. Let's assume
29052 this was a stray '@implementation' token in the stream and
29057 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29058 &is_class_extension);
29060 /* We have either a class or a category on our hands. */
29062 objc_start_category_implementation (name, categ);
29065 objc_start_class_implementation (name, super);
29066 /* Handle instance variable declarations, if any. */
29067 cp_parser_objc_class_ivars (parser);
29068 objc_continue_implementation ();
29071 cp_parser_objc_method_definition_list (parser);
29074 /* Consume the @end token and finish off the implementation. */
29077 cp_parser_objc_end_implementation (cp_parser* parser)
29079 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29080 objc_finish_implementation ();
29083 /* Parse an Objective-C declaration. */
29086 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29088 /* Try to figure out what kind of declaration is present. */
29089 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29092 switch (kwd->keyword)
29097 error_at (kwd->location, "attributes may not be specified before"
29098 " the %<@%D%> Objective-C++ keyword",
29102 case RID_AT_IMPLEMENTATION:
29103 warning_at (kwd->location, OPT_Wattributes,
29104 "prefix attributes are ignored before %<@%D%>",
29111 switch (kwd->keyword)
29114 cp_parser_objc_alias_declaration (parser);
29117 cp_parser_objc_class_declaration (parser);
29119 case RID_AT_PROTOCOL:
29120 cp_parser_objc_protocol_declaration (parser, attributes);
29122 case RID_AT_INTERFACE:
29123 cp_parser_objc_class_interface (parser, attributes);
29125 case RID_AT_IMPLEMENTATION:
29126 cp_parser_objc_class_implementation (parser);
29129 cp_parser_objc_end_implementation (parser);
29132 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29134 cp_parser_skip_to_end_of_block_or_statement (parser);
29138 /* Parse an Objective-C try-catch-finally statement.
29140 objc-try-catch-finally-stmt:
29141 @try compound-statement objc-catch-clause-seq [opt]
29142 objc-finally-clause [opt]
29144 objc-catch-clause-seq:
29145 objc-catch-clause objc-catch-clause-seq [opt]
29148 @catch ( objc-exception-declaration ) compound-statement
29150 objc-finally-clause:
29151 @finally compound-statement
29153 objc-exception-declaration:
29154 parameter-declaration
29157 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29161 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29162 for C. Keep them in sync. */
29165 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29167 location_t location;
29170 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29171 location = cp_lexer_peek_token (parser->lexer)->location;
29172 objc_maybe_warn_exceptions (location);
29173 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29174 node, lest it get absorbed into the surrounding block. */
29175 stmt = push_stmt_list ();
29176 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29177 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29179 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29181 cp_parameter_declarator *parm;
29182 tree parameter_declaration = error_mark_node;
29183 bool seen_open_paren = false;
29185 cp_lexer_consume_token (parser->lexer);
29186 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29187 seen_open_paren = true;
29188 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29190 /* We have "@catch (...)" (where the '...' are literally
29191 what is in the code). Skip the '...'.
29192 parameter_declaration is set to NULL_TREE, and
29193 objc_being_catch_clauses() knows that that means
29195 cp_lexer_consume_token (parser->lexer);
29196 parameter_declaration = NULL_TREE;
29200 /* We have "@catch (NSException *exception)" or something
29201 like that. Parse the parameter declaration. */
29202 parm = cp_parser_parameter_declaration (parser, false, NULL);
29204 parameter_declaration = error_mark_node;
29206 parameter_declaration = grokdeclarator (parm->declarator,
29207 &parm->decl_specifiers,
29208 PARM, /*initialized=*/0,
29209 /*attrlist=*/NULL);
29211 if (seen_open_paren)
29212 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29215 /* If there was no open parenthesis, we are recovering from
29216 an error, and we are trying to figure out what mistake
29217 the user has made. */
29219 /* If there is an immediate closing parenthesis, the user
29220 probably forgot the opening one (ie, they typed "@catch
29221 NSException *e)". Parse the closing parenthesis and keep
29223 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29224 cp_lexer_consume_token (parser->lexer);
29226 /* If these is no immediate closing parenthesis, the user
29227 probably doesn't know that parenthesis are required at
29228 all (ie, they typed "@catch NSException *e"). So, just
29229 forget about the closing parenthesis and keep going. */
29231 objc_begin_catch_clause (parameter_declaration);
29232 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29233 objc_finish_catch_clause ();
29235 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29237 cp_lexer_consume_token (parser->lexer);
29238 location = cp_lexer_peek_token (parser->lexer)->location;
29239 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29240 node, lest it get absorbed into the surrounding block. */
29241 stmt = push_stmt_list ();
29242 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29243 objc_build_finally_clause (location, pop_stmt_list (stmt));
29246 return objc_finish_try_stmt ();
29249 /* Parse an Objective-C synchronized statement.
29251 objc-synchronized-stmt:
29252 @synchronized ( expression ) compound-statement
29254 Returns NULL_TREE. */
29257 cp_parser_objc_synchronized_statement (cp_parser *parser)
29259 location_t location;
29262 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29264 location = cp_lexer_peek_token (parser->lexer)->location;
29265 objc_maybe_warn_exceptions (location);
29266 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29267 lock = cp_parser_expression (parser);
29268 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29270 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29271 node, lest it get absorbed into the surrounding block. */
29272 stmt = push_stmt_list ();
29273 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29275 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29278 /* Parse an Objective-C throw statement.
29281 @throw assignment-expression [opt] ;
29283 Returns a constructed '@throw' statement. */
29286 cp_parser_objc_throw_statement (cp_parser *parser)
29288 tree expr = NULL_TREE;
29289 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29291 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29294 expr = cp_parser_expression (parser);
29296 cp_parser_consume_semicolon_at_end_of_statement (parser);
29298 return objc_build_throw_stmt (loc, expr);
29301 /* Parse an Objective-C statement. */
29304 cp_parser_objc_statement (cp_parser * parser)
29306 /* Try to figure out what kind of declaration is present. */
29307 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29309 switch (kwd->keyword)
29312 return cp_parser_objc_try_catch_finally_statement (parser);
29313 case RID_AT_SYNCHRONIZED:
29314 return cp_parser_objc_synchronized_statement (parser);
29316 return cp_parser_objc_throw_statement (parser);
29318 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29320 cp_parser_skip_to_end_of_block_or_statement (parser);
29323 return error_mark_node;
29326 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29327 look ahead to see if an objc keyword follows the attributes. This
29328 is to detect the use of prefix attributes on ObjC @interface and
29332 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29334 cp_lexer_save_tokens (parser->lexer);
29335 *attrib = cp_parser_attributes_opt (parser);
29336 gcc_assert (*attrib);
29337 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29339 cp_lexer_commit_tokens (parser->lexer);
29342 cp_lexer_rollback_tokens (parser->lexer);
29346 /* This routine is a minimal replacement for
29347 c_parser_struct_declaration () used when parsing the list of
29348 types/names or ObjC++ properties. For example, when parsing the
29351 @property (readonly) int a, b, c;
29353 this function is responsible for parsing "int a, int b, int c" and
29354 returning the declarations as CHAIN of DECLs.
29356 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29357 similar parsing. */
29359 cp_parser_objc_struct_declaration (cp_parser *parser)
29361 tree decls = NULL_TREE;
29362 cp_decl_specifier_seq declspecs;
29363 int decl_class_or_enum_p;
29364 tree prefix_attributes;
29366 cp_parser_decl_specifier_seq (parser,
29367 CP_PARSER_FLAGS_NONE,
29369 &decl_class_or_enum_p);
29371 if (declspecs.type == error_mark_node)
29372 return error_mark_node;
29374 /* auto, register, static, extern, mutable. */
29375 if (declspecs.storage_class != sc_none)
29377 cp_parser_error (parser, "invalid type for property");
29378 declspecs.storage_class = sc_none;
29381 /* thread_local. */
29382 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29384 cp_parser_error (parser, "invalid type for property");
29385 declspecs.locations[ds_thread] = 0;
29389 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29391 cp_parser_error (parser, "invalid type for property");
29392 declspecs.locations[ds_typedef] = 0;
29395 prefix_attributes = declspecs.attributes;
29396 declspecs.attributes = NULL_TREE;
29398 /* Keep going until we hit the `;' at the end of the declaration. */
29399 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29401 tree attributes, first_attribute, decl;
29402 cp_declarator *declarator;
29405 /* Parse the declarator. */
29406 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29407 NULL, NULL, false, false);
29409 /* Look for attributes that apply to the ivar. */
29410 attributes = cp_parser_attributes_opt (parser);
29411 /* Remember which attributes are prefix attributes and
29413 first_attribute = attributes;
29414 /* Combine the attributes. */
29415 attributes = chainon (prefix_attributes, attributes);
29417 decl = grokfield (declarator, &declspecs,
29418 NULL_TREE, /*init_const_expr_p=*/false,
29419 NULL_TREE, attributes);
29421 if (decl == error_mark_node || decl == NULL_TREE)
29422 return error_mark_node;
29424 /* Reset PREFIX_ATTRIBUTES. */
29425 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29426 attributes = TREE_CHAIN (attributes);
29428 TREE_CHAIN (attributes) = NULL_TREE;
29430 DECL_CHAIN (decl) = decls;
29433 token = cp_lexer_peek_token (parser->lexer);
29434 if (token->type == CPP_COMMA)
29436 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29445 /* Parse an Objective-C @property declaration. The syntax is:
29447 objc-property-declaration:
29448 '@property' objc-property-attributes[opt] struct-declaration ;
29450 objc-property-attributes:
29451 '(' objc-property-attribute-list ')'
29453 objc-property-attribute-list:
29454 objc-property-attribute
29455 objc-property-attribute-list, objc-property-attribute
29457 objc-property-attribute
29458 'getter' = identifier
29459 'setter' = identifier
29468 @property NSString *name;
29469 @property (readonly) id object;
29470 @property (retain, nonatomic, getter=getTheName) id name;
29471 @property int a, b, c;
29473 PS: This function is identical to
29474 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29476 cp_parser_objc_at_property_declaration (cp_parser *parser)
29478 /* The following variables hold the attributes of the properties as
29479 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29480 seen. When we see an attribute, we set them to 'true' (if they
29481 are boolean properties) or to the identifier (if they have an
29482 argument, ie, for getter and setter). Note that here we only
29483 parse the list of attributes, check the syntax and accumulate the
29484 attributes that we find. objc_add_property_declaration() will
29485 then process the information. */
29486 bool property_assign = false;
29487 bool property_copy = false;
29488 tree property_getter_ident = NULL_TREE;
29489 bool property_nonatomic = false;
29490 bool property_readonly = false;
29491 bool property_readwrite = false;
29492 bool property_retain = false;
29493 tree property_setter_ident = NULL_TREE;
29495 /* 'properties' is the list of properties that we read. Usually a
29496 single one, but maybe more (eg, in "@property int a, b, c;" there
29501 loc = cp_lexer_peek_token (parser->lexer)->location;
29503 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29505 /* Parse the optional attribute list... */
29506 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29509 cp_lexer_consume_token (parser->lexer);
29513 bool syntax_error = false;
29514 cp_token *token = cp_lexer_peek_token (parser->lexer);
29517 if (token->type != CPP_NAME)
29519 cp_parser_error (parser, "expected identifier");
29522 keyword = C_RID_CODE (token->u.value);
29523 cp_lexer_consume_token (parser->lexer);
29526 case RID_ASSIGN: property_assign = true; break;
29527 case RID_COPY: property_copy = true; break;
29528 case RID_NONATOMIC: property_nonatomic = true; break;
29529 case RID_READONLY: property_readonly = true; break;
29530 case RID_READWRITE: property_readwrite = true; break;
29531 case RID_RETAIN: property_retain = true; break;
29535 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29537 if (keyword == RID_GETTER)
29538 cp_parser_error (parser,
29539 "missing %<=%> (after %<getter%> attribute)");
29541 cp_parser_error (parser,
29542 "missing %<=%> (after %<setter%> attribute)");
29543 syntax_error = true;
29546 cp_lexer_consume_token (parser->lexer); /* eat the = */
29547 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29549 cp_parser_error (parser, "expected identifier");
29550 syntax_error = true;
29553 if (keyword == RID_SETTER)
29555 if (property_setter_ident != NULL_TREE)
29557 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29558 cp_lexer_consume_token (parser->lexer);
29561 property_setter_ident = cp_parser_objc_selector (parser);
29562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29563 cp_parser_error (parser, "setter name must terminate with %<:%>");
29565 cp_lexer_consume_token (parser->lexer);
29569 if (property_getter_ident != NULL_TREE)
29571 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29572 cp_lexer_consume_token (parser->lexer);
29575 property_getter_ident = cp_parser_objc_selector (parser);
29579 cp_parser_error (parser, "unknown property attribute");
29580 syntax_error = true;
29587 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29588 cp_lexer_consume_token (parser->lexer);
29593 /* FIXME: "@property (setter, assign);" will generate a spurious
29594 "error: expected ‘)’ before ‘,’ token". This is because
29595 cp_parser_require, unlike the C counterpart, will produce an
29596 error even if we are in error recovery. */
29597 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29599 cp_parser_skip_to_closing_parenthesis (parser,
29600 /*recovering=*/true,
29601 /*or_comma=*/false,
29602 /*consume_paren=*/true);
29606 /* ... and the property declaration(s). */
29607 properties = cp_parser_objc_struct_declaration (parser);
29609 if (properties == error_mark_node)
29611 cp_parser_skip_to_end_of_statement (parser);
29612 /* If the next token is now a `;', consume it. */
29613 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29614 cp_lexer_consume_token (parser->lexer);
29618 if (properties == NULL_TREE)
29619 cp_parser_error (parser, "expected identifier");
29622 /* Comma-separated properties are chained together in
29623 reverse order; add them one by one. */
29624 properties = nreverse (properties);
29626 for (; properties; properties = TREE_CHAIN (properties))
29627 objc_add_property_declaration (loc, copy_node (properties),
29628 property_readonly, property_readwrite,
29629 property_assign, property_retain,
29630 property_copy, property_nonatomic,
29631 property_getter_ident, property_setter_ident);
29634 cp_parser_consume_semicolon_at_end_of_statement (parser);
29637 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29639 objc-synthesize-declaration:
29640 @synthesize objc-synthesize-identifier-list ;
29642 objc-synthesize-identifier-list:
29643 objc-synthesize-identifier
29644 objc-synthesize-identifier-list, objc-synthesize-identifier
29646 objc-synthesize-identifier
29648 identifier = identifier
29651 @synthesize MyProperty;
29652 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29654 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29655 for C. Keep them in sync.
29658 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29660 tree list = NULL_TREE;
29662 loc = cp_lexer_peek_token (parser->lexer)->location;
29664 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29667 tree property, ivar;
29668 property = cp_parser_identifier (parser);
29669 if (property == error_mark_node)
29671 cp_parser_consume_semicolon_at_end_of_statement (parser);
29674 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29676 cp_lexer_consume_token (parser->lexer);
29677 ivar = cp_parser_identifier (parser);
29678 if (ivar == error_mark_node)
29680 cp_parser_consume_semicolon_at_end_of_statement (parser);
29686 list = chainon (list, build_tree_list (ivar, property));
29687 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29688 cp_lexer_consume_token (parser->lexer);
29692 cp_parser_consume_semicolon_at_end_of_statement (parser);
29693 objc_add_synthesize_declaration (loc, list);
29696 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29698 objc-dynamic-declaration:
29699 @dynamic identifier-list ;
29702 @dynamic MyProperty;
29703 @dynamic MyProperty, AnotherProperty;
29705 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29706 for C. Keep them in sync.
29709 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29711 tree list = NULL_TREE;
29713 loc = cp_lexer_peek_token (parser->lexer)->location;
29715 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29719 property = cp_parser_identifier (parser);
29720 if (property == error_mark_node)
29722 cp_parser_consume_semicolon_at_end_of_statement (parser);
29725 list = chainon (list, build_tree_list (NULL, property));
29726 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29727 cp_lexer_consume_token (parser->lexer);
29731 cp_parser_consume_semicolon_at_end_of_statement (parser);
29732 objc_add_dynamic_declaration (loc, list);
29736 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29738 /* Returns name of the next clause.
29739 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29740 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29741 returned and the token is consumed. */
29743 static pragma_omp_clause
29744 cp_parser_omp_clause_name (cp_parser *parser)
29746 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29748 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29749 result = PRAGMA_OACC_CLAUSE_AUTO;
29750 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29751 result = PRAGMA_OMP_CLAUSE_IF;
29752 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29753 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29754 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29755 result = PRAGMA_OACC_CLAUSE_DELETE;
29756 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29757 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29758 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29759 result = PRAGMA_OMP_CLAUSE_FOR;
29760 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29762 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29763 const char *p = IDENTIFIER_POINTER (id);
29768 if (!strcmp ("aligned", p))
29769 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29770 else if (!strcmp ("async", p))
29771 result = PRAGMA_OACC_CLAUSE_ASYNC;
29774 if (!strcmp ("collapse", p))
29775 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29776 else if (!strcmp ("copy", p))
29777 result = PRAGMA_OACC_CLAUSE_COPY;
29778 else if (!strcmp ("copyin", p))
29779 result = PRAGMA_OMP_CLAUSE_COPYIN;
29780 else if (!strcmp ("copyout", p))
29781 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29782 else if (!strcmp ("copyprivate", p))
29783 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29784 else if (!strcmp ("create", p))
29785 result = PRAGMA_OACC_CLAUSE_CREATE;
29788 if (!strcmp ("defaultmap", p))
29789 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29790 else if (!strcmp ("depend", p))
29791 result = PRAGMA_OMP_CLAUSE_DEPEND;
29792 else if (!strcmp ("device", p))
29793 result = PRAGMA_OMP_CLAUSE_DEVICE;
29794 else if (!strcmp ("deviceptr", p))
29795 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29796 else if (!strcmp ("device_resident", p))
29797 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29798 else if (!strcmp ("dist_schedule", p))
29799 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29802 if (!strcmp ("final", p))
29803 result = PRAGMA_OMP_CLAUSE_FINAL;
29804 else if (!strcmp ("firstprivate", p))
29805 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29806 else if (!strcmp ("from", p))
29807 result = PRAGMA_OMP_CLAUSE_FROM;
29810 if (!strcmp ("gang", p))
29811 result = PRAGMA_OACC_CLAUSE_GANG;
29812 else if (!strcmp ("grainsize", p))
29813 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29816 if (!strcmp ("hint", p))
29817 result = PRAGMA_OMP_CLAUSE_HINT;
29818 else if (!strcmp ("host", p))
29819 result = PRAGMA_OACC_CLAUSE_HOST;
29822 if (!strcmp ("inbranch", p))
29823 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29824 else if (!strcmp ("independent", p))
29825 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29826 else if (!strcmp ("is_device_ptr", p))
29827 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29830 if (!strcmp ("lastprivate", p))
29831 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29832 else if (!strcmp ("linear", p))
29833 result = PRAGMA_OMP_CLAUSE_LINEAR;
29834 else if (!strcmp ("link", p))
29835 result = PRAGMA_OMP_CLAUSE_LINK;
29838 if (!strcmp ("map", p))
29839 result = PRAGMA_OMP_CLAUSE_MAP;
29840 else if (!strcmp ("mergeable", p))
29841 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29842 else if (flag_cilkplus && !strcmp ("mask", p))
29843 result = PRAGMA_CILK_CLAUSE_MASK;
29846 if (!strcmp ("nogroup", p))
29847 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29848 else if (!strcmp ("notinbranch", p))
29849 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29850 else if (!strcmp ("nowait", p))
29851 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29852 else if (flag_cilkplus && !strcmp ("nomask", p))
29853 result = PRAGMA_CILK_CLAUSE_NOMASK;
29854 else if (!strcmp ("num_gangs", p))
29855 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29856 else if (!strcmp ("num_tasks", p))
29857 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29858 else if (!strcmp ("num_teams", p))
29859 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29860 else if (!strcmp ("num_threads", p))
29861 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29862 else if (!strcmp ("num_workers", p))
29863 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29866 if (!strcmp ("ordered", p))
29867 result = PRAGMA_OMP_CLAUSE_ORDERED;
29870 if (!strcmp ("parallel", p))
29871 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29872 else if (!strcmp ("present", p))
29873 result = PRAGMA_OACC_CLAUSE_PRESENT;
29874 else if (!strcmp ("present_or_copy", p)
29875 || !strcmp ("pcopy", p))
29876 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29877 else if (!strcmp ("present_or_copyin", p)
29878 || !strcmp ("pcopyin", p))
29879 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29880 else if (!strcmp ("present_or_copyout", p)
29881 || !strcmp ("pcopyout", p))
29882 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29883 else if (!strcmp ("present_or_create", p)
29884 || !strcmp ("pcreate", p))
29885 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29886 else if (!strcmp ("priority", p))
29887 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29888 else if (!strcmp ("proc_bind", p))
29889 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29892 if (!strcmp ("reduction", p))
29893 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29896 if (!strcmp ("safelen", p))
29897 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29898 else if (!strcmp ("schedule", p))
29899 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29900 else if (!strcmp ("sections", p))
29901 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29902 else if (!strcmp ("self", p))
29903 result = PRAGMA_OACC_CLAUSE_SELF;
29904 else if (!strcmp ("seq", p))
29905 result = PRAGMA_OACC_CLAUSE_SEQ;
29906 else if (!strcmp ("shared", p))
29907 result = PRAGMA_OMP_CLAUSE_SHARED;
29908 else if (!strcmp ("simd", p))
29909 result = PRAGMA_OMP_CLAUSE_SIMD;
29910 else if (!strcmp ("simdlen", p))
29911 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29914 if (!strcmp ("taskgroup", p))
29915 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29916 else if (!strcmp ("thread_limit", p))
29917 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29918 else if (!strcmp ("threads", p))
29919 result = PRAGMA_OMP_CLAUSE_THREADS;
29920 else if (!strcmp ("tile", p))
29921 result = PRAGMA_OACC_CLAUSE_TILE;
29922 else if (!strcmp ("to", p))
29923 result = PRAGMA_OMP_CLAUSE_TO;
29926 if (!strcmp ("uniform", p))
29927 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29928 else if (!strcmp ("untied", p))
29929 result = PRAGMA_OMP_CLAUSE_UNTIED;
29930 else if (!strcmp ("use_device", p))
29931 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29932 else if (!strcmp ("use_device_ptr", p))
29933 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29936 if (!strcmp ("vector", p))
29937 result = PRAGMA_OACC_CLAUSE_VECTOR;
29938 else if (!strcmp ("vector_length", p))
29939 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29940 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29941 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29944 if (!strcmp ("wait", p))
29945 result = PRAGMA_OACC_CLAUSE_WAIT;
29946 else if (!strcmp ("worker", p))
29947 result = PRAGMA_OACC_CLAUSE_WORKER;
29952 if (result != PRAGMA_OMP_CLAUSE_NONE)
29953 cp_lexer_consume_token (parser->lexer);
29958 /* Validate that a clause of the given type does not already exist. */
29961 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29962 const char *name, location_t location)
29966 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29967 if (OMP_CLAUSE_CODE (c) == code)
29969 error_at (location, "too many %qs clauses", name);
29977 variable-list , identifier
29979 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29980 colon). An opening parenthesis will have been consumed by the caller.
29982 If KIND is nonzero, create the appropriate node and install the decl
29983 in OMP_CLAUSE_DECL and add the node to the head of the list.
29985 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29986 return the list created.
29988 COLON can be NULL if only closing parenthesis should end the list,
29989 or pointer to bool which will receive false if the list is terminated
29990 by closing parenthesis or true if the list is terminated by colon. */
29993 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29994 tree list, bool *colon)
29997 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30000 parser->colon_corrects_to_scope_p = false;
30007 token = cp_lexer_peek_token (parser->lexer);
30009 && current_class_ptr
30010 && cp_parser_is_keyword (token, RID_THIS))
30012 decl = finish_this_expr ();
30013 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30014 || CONVERT_EXPR_P (decl))
30015 decl = TREE_OPERAND (decl, 0);
30016 cp_lexer_consume_token (parser->lexer);
30020 name = cp_parser_id_expression (parser, /*template_p=*/false,
30021 /*check_dependency_p=*/true,
30022 /*template_p=*/NULL,
30023 /*declarator_p=*/false,
30024 /*optional_p=*/false);
30025 if (name == error_mark_node)
30028 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30029 if (decl == error_mark_node)
30030 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30033 if (decl == error_mark_node)
30035 else if (kind != 0)
30039 case OMP_CLAUSE__CACHE_:
30040 /* The OpenACC cache directive explicitly only allows "array
30041 elements or subarrays". */
30042 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30044 error_at (token->location, "expected %<[%>");
30045 decl = error_mark_node;
30049 case OMP_CLAUSE_MAP:
30050 case OMP_CLAUSE_FROM:
30051 case OMP_CLAUSE_TO:
30052 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30055 = cp_lexer_peek_token (parser->lexer)->location;
30056 cp_id_kind idk = CP_ID_KIND_NONE;
30057 cp_lexer_consume_token (parser->lexer);
30058 decl = convert_from_reference (decl);
30060 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30065 case OMP_CLAUSE_DEPEND:
30066 case OMP_CLAUSE_REDUCTION:
30067 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30069 tree low_bound = NULL_TREE, length = NULL_TREE;
30071 parser->colon_corrects_to_scope_p = false;
30072 cp_lexer_consume_token (parser->lexer);
30073 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30074 low_bound = cp_parser_expression (parser);
30076 parser->colon_corrects_to_scope_p
30077 = saved_colon_corrects_to_scope_p;
30078 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30079 length = integer_one_node;
30082 /* Look for `:'. */
30083 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30085 if (!cp_lexer_next_token_is (parser->lexer,
30087 length = cp_parser_expression (parser);
30089 /* Look for the closing `]'. */
30090 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30094 decl = tree_cons (low_bound, length, decl);
30101 tree u = build_omp_clause (token->location, kind);
30102 OMP_CLAUSE_DECL (u) = decl;
30103 OMP_CLAUSE_CHAIN (u) = list;
30107 list = tree_cons (decl, NULL_TREE, list);
30110 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30112 cp_lexer_consume_token (parser->lexer);
30116 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30118 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30121 cp_parser_require (parser, CPP_COLON, RT_COLON);
30125 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30129 /* Try to resync to an unnested comma. Copied from
30130 cp_parser_parenthesized_expression_list. */
30133 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30134 ending = cp_parser_skip_to_closing_parenthesis (parser,
30135 /*recovering=*/true,
30137 /*consume_paren=*/true);
30145 /* Similarly, but expect leading and trailing parenthesis. This is a very
30146 common case for omp clauses. */
30149 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30151 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30152 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30157 copy ( variable-list )
30158 copyin ( variable-list )
30159 copyout ( variable-list )
30160 create ( variable-list )
30161 delete ( variable-list )
30162 present ( variable-list )
30163 present_or_copy ( variable-list )
30164 pcopy ( variable-list )
30165 present_or_copyin ( variable-list )
30166 pcopyin ( variable-list )
30167 present_or_copyout ( variable-list )
30168 pcopyout ( variable-list )
30169 present_or_create ( variable-list )
30170 pcreate ( variable-list ) */
30173 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30176 enum gomp_map_kind kind;
30179 case PRAGMA_OACC_CLAUSE_COPY:
30180 kind = GOMP_MAP_FORCE_TOFROM;
30182 case PRAGMA_OACC_CLAUSE_COPYIN:
30183 kind = GOMP_MAP_FORCE_TO;
30185 case PRAGMA_OACC_CLAUSE_COPYOUT:
30186 kind = GOMP_MAP_FORCE_FROM;
30188 case PRAGMA_OACC_CLAUSE_CREATE:
30189 kind = GOMP_MAP_FORCE_ALLOC;
30191 case PRAGMA_OACC_CLAUSE_DELETE:
30192 kind = GOMP_MAP_DELETE;
30194 case PRAGMA_OACC_CLAUSE_DEVICE:
30195 kind = GOMP_MAP_FORCE_TO;
30197 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30198 kind = GOMP_MAP_DEVICE_RESIDENT;
30200 case PRAGMA_OACC_CLAUSE_HOST:
30201 case PRAGMA_OACC_CLAUSE_SELF:
30202 kind = GOMP_MAP_FORCE_FROM;
30204 case PRAGMA_OACC_CLAUSE_LINK:
30205 kind = GOMP_MAP_LINK;
30207 case PRAGMA_OACC_CLAUSE_PRESENT:
30208 kind = GOMP_MAP_FORCE_PRESENT;
30210 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30211 kind = GOMP_MAP_TOFROM;
30213 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30214 kind = GOMP_MAP_TO;
30216 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30217 kind = GOMP_MAP_FROM;
30219 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30220 kind = GOMP_MAP_ALLOC;
30223 gcc_unreachable ();
30226 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30228 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30229 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30235 deviceptr ( variable-list ) */
30238 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30243 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30244 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30245 variable-list must only allow for pointer variables. */
30246 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30247 for (t = vars; t; t = TREE_CHAIN (t))
30249 tree v = TREE_PURPOSE (t);
30250 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30251 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30252 OMP_CLAUSE_DECL (u) = v;
30253 OMP_CLAUSE_CHAIN (u) = list;
30267 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30268 enum omp_clause_code code,
30269 tree list, location_t location)
30271 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30272 tree c = build_omp_clause (location, code);
30273 OMP_CLAUSE_CHAIN (c) = list;
30278 num_gangs ( expression )
30279 num_workers ( expression )
30280 vector_length ( expression ) */
30283 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30284 const char *str, tree list)
30286 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30288 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30291 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30293 if (t == error_mark_node
30294 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30296 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30297 /*or_comma=*/false,
30298 /*consume_paren=*/true);
30302 check_no_duplicate_clause (list, code, str, loc);
30304 tree c = build_omp_clause (loc, code);
30305 OMP_CLAUSE_OPERAND (c, 0) = t;
30306 OMP_CLAUSE_CHAIN (c) = list;
30312 gang [( gang-arg-list )]
30313 worker [( [num:] int-expr )]
30314 vector [( [length:] int-expr )]
30316 where gang-arg is one of:
30321 and size-expr may be:
30328 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30329 const char *str, tree list)
30331 const char *id = "num";
30332 cp_lexer *lexer = parser->lexer;
30333 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30334 location_t loc = cp_lexer_peek_token (lexer)->location;
30336 if (kind == OMP_CLAUSE_VECTOR)
30339 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30341 cp_lexer_consume_token (lexer);
30345 cp_token *next = cp_lexer_peek_token (lexer);
30348 /* Gang static argument. */
30349 if (kind == OMP_CLAUSE_GANG
30350 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30352 cp_lexer_consume_token (lexer);
30354 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30355 goto cleanup_error;
30358 if (ops[idx] != NULL)
30360 cp_parser_error (parser, "too many %<static%> arguments");
30361 goto cleanup_error;
30364 /* Check for the '*' argument. */
30365 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30366 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30367 || cp_lexer_nth_token_is (parser->lexer, 2,
30370 cp_lexer_consume_token (lexer);
30371 ops[idx] = integer_minus_one_node;
30373 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30375 cp_lexer_consume_token (lexer);
30381 /* Worker num: argument and vector length: arguments. */
30382 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30383 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30384 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30386 cp_lexer_consume_token (lexer); /* id */
30387 cp_lexer_consume_token (lexer); /* ':' */
30390 /* Now collect the actual argument. */
30391 if (ops[idx] != NULL_TREE)
30393 cp_parser_error (parser, "unexpected argument");
30394 goto cleanup_error;
30397 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30399 if (expr == error_mark_node)
30400 goto cleanup_error;
30402 mark_exp_read (expr);
30405 if (kind == OMP_CLAUSE_GANG
30406 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30408 cp_lexer_consume_token (lexer);
30415 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30416 goto cleanup_error;
30419 check_no_duplicate_clause (list, kind, str, loc);
30421 c = build_omp_clause (loc, kind);
30424 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30426 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30427 OMP_CLAUSE_CHAIN (c) = list;
30432 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30437 tile ( size-expr-list ) */
30440 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30442 tree c, expr = error_mark_node;
30443 tree tile = NULL_TREE;
30445 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30447 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30452 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30453 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30454 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30456 cp_lexer_consume_token (parser->lexer);
30457 expr = integer_minus_one_node;
30460 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30462 if (expr == error_mark_node)
30465 tile = tree_cons (NULL_TREE, expr, tile);
30467 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30468 cp_lexer_consume_token (parser->lexer);
30470 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30472 /* Consume the trailing ')'. */
30473 cp_lexer_consume_token (parser->lexer);
30475 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30476 tile = nreverse (tile);
30477 OMP_CLAUSE_TILE_LIST (c) = tile;
30478 OMP_CLAUSE_CHAIN (c) = list;
30483 Parse wait clause or directive parameters. */
30486 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30488 vec<tree, va_gc> *args;
30491 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30493 /*allow_expansion_p=*/true,
30494 /*non_constant_p=*/NULL);
30496 if (args == NULL || args->length () == 0)
30498 cp_parser_error (parser, "expected integer expression before ')'");
30500 release_tree_vector (args);
30504 args_tree = build_tree_list_vec (args);
30506 release_tree_vector (args);
30508 for (t = args_tree; t; t = TREE_CHAIN (t))
30510 tree targ = TREE_VALUE (t);
30512 if (targ != error_mark_node)
30514 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30515 error ("%<wait%> expression must be integral");
30518 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30520 mark_rvalue_use (targ);
30521 OMP_CLAUSE_DECL (c) = targ;
30522 OMP_CLAUSE_CHAIN (c) = list;
30532 wait ( int-expr-list ) */
30535 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30537 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30539 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30542 list = cp_parser_oacc_wait_list (parser, location, list);
30548 collapse ( constant-expression ) */
30551 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30557 loc = cp_lexer_peek_token (parser->lexer)->location;
30558 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30561 num = cp_parser_constant_expression (parser);
30563 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30564 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30565 /*or_comma=*/false,
30566 /*consume_paren=*/true);
30568 if (num == error_mark_node)
30570 num = fold_non_dependent_expr (num);
30571 if (!tree_fits_shwi_p (num)
30572 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30573 || (n = tree_to_shwi (num)) <= 0
30576 error_at (loc, "collapse argument needs positive constant integer expression");
30580 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30581 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30582 OMP_CLAUSE_CHAIN (c) = list;
30583 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30589 default ( shared | none )
30595 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30596 location_t location, bool is_oacc)
30598 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30601 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30603 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30605 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30606 const char *p = IDENTIFIER_POINTER (id);
30611 if (strcmp ("none", p) != 0)
30613 kind = OMP_CLAUSE_DEFAULT_NONE;
30617 if (strcmp ("shared", p) != 0 || is_oacc)
30619 kind = OMP_CLAUSE_DEFAULT_SHARED;
30626 cp_lexer_consume_token (parser->lexer);
30632 cp_parser_error (parser, "expected %<none%>");
30634 cp_parser_error (parser, "expected %<none%> or %<shared%>");
30637 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30638 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30639 /*or_comma=*/false,
30640 /*consume_paren=*/true);
30642 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30645 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30646 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30647 OMP_CLAUSE_CHAIN (c) = list;
30648 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30654 final ( expression ) */
30657 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30661 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30664 t = cp_parser_condition (parser);
30666 if (t == error_mark_node
30667 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30668 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30669 /*or_comma=*/false,
30670 /*consume_paren=*/true);
30672 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30674 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30675 OMP_CLAUSE_FINAL_EXPR (c) = t;
30676 OMP_CLAUSE_CHAIN (c) = list;
30685 if ( directive-name-modifier : expression )
30687 directive-name-modifier:
30688 parallel | task | taskloop | target data | target | target update
30689 | target enter data | target exit data */
30692 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30696 enum tree_code if_modifier = ERROR_MARK;
30698 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30701 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30703 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30704 const char *p = IDENTIFIER_POINTER (id);
30707 if (strcmp ("parallel", p) == 0)
30708 if_modifier = OMP_PARALLEL;
30709 else if (strcmp ("task", p) == 0)
30710 if_modifier = OMP_TASK;
30711 else if (strcmp ("taskloop", p) == 0)
30712 if_modifier = OMP_TASKLOOP;
30713 else if (strcmp ("target", p) == 0)
30715 if_modifier = OMP_TARGET;
30716 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30718 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30719 p = IDENTIFIER_POINTER (id);
30720 if (strcmp ("data", p) == 0)
30721 if_modifier = OMP_TARGET_DATA;
30722 else if (strcmp ("update", p) == 0)
30723 if_modifier = OMP_TARGET_UPDATE;
30724 else if (strcmp ("enter", p) == 0)
30725 if_modifier = OMP_TARGET_ENTER_DATA;
30726 else if (strcmp ("exit", p) == 0)
30727 if_modifier = OMP_TARGET_EXIT_DATA;
30728 if (if_modifier != OMP_TARGET)
30733 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30734 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30736 if_modifier = ERROR_MARK;
30738 if (if_modifier == OMP_TARGET_ENTER_DATA
30739 || if_modifier == OMP_TARGET_EXIT_DATA)
30741 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30743 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30744 p = IDENTIFIER_POINTER (id);
30745 if (strcmp ("data", p) == 0)
30751 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30752 error_at (loc, "expected %<data%>");
30753 if_modifier = ERROR_MARK;
30758 if (if_modifier != ERROR_MARK)
30760 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30763 cp_lexer_consume_token (parser->lexer);
30770 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30771 error_at (loc, "expected %<:%>");
30773 if_modifier = ERROR_MARK;
30778 t = cp_parser_condition (parser);
30780 if (t == error_mark_node
30781 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30782 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30783 /*or_comma=*/false,
30784 /*consume_paren=*/true);
30786 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30787 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30789 if (if_modifier != ERROR_MARK
30790 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30792 const char *p = NULL;
30793 switch (if_modifier)
30795 case OMP_PARALLEL: p = "parallel"; break;
30796 case OMP_TASK: p = "task"; break;
30797 case OMP_TASKLOOP: p = "taskloop"; break;
30798 case OMP_TARGET_DATA: p = "target data"; break;
30799 case OMP_TARGET: p = "target"; break;
30800 case OMP_TARGET_UPDATE: p = "target update"; break;
30801 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30802 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30803 default: gcc_unreachable ();
30805 error_at (location, "too many %<if%> clauses with %qs modifier",
30809 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30812 error_at (location, "too many %<if%> clauses");
30814 error_at (location, "too many %<if%> clauses without modifier");
30817 else if (if_modifier == ERROR_MARK
30818 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30820 error_at (location, "if any %<if%> clause has modifier, then all "
30821 "%<if%> clauses have to use modifier");
30826 c = build_omp_clause (location, OMP_CLAUSE_IF);
30827 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30828 OMP_CLAUSE_IF_EXPR (c) = t;
30829 OMP_CLAUSE_CHAIN (c) = list;
30838 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30839 tree list, location_t location)
30843 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30846 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30847 OMP_CLAUSE_CHAIN (c) = list;
30855 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30856 tree list, location_t location)
30860 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30862 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30863 OMP_CLAUSE_CHAIN (c) = list;
30868 num_threads ( expression ) */
30871 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30872 location_t location)
30876 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30879 t = cp_parser_expression (parser);
30881 if (t == error_mark_node
30882 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30883 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30884 /*or_comma=*/false,
30885 /*consume_paren=*/true);
30887 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30888 "num_threads", location);
30890 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30891 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30892 OMP_CLAUSE_CHAIN (c) = list;
30898 num_tasks ( expression ) */
30901 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30902 location_t location)
30906 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30909 t = cp_parser_expression (parser);
30911 if (t == error_mark_node
30912 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30913 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30914 /*or_comma=*/false,
30915 /*consume_paren=*/true);
30917 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30918 "num_tasks", location);
30920 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30921 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30922 OMP_CLAUSE_CHAIN (c) = list;
30928 grainsize ( expression ) */
30931 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30932 location_t location)
30936 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30939 t = cp_parser_expression (parser);
30941 if (t == error_mark_node
30942 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30943 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30944 /*or_comma=*/false,
30945 /*consume_paren=*/true);
30947 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30948 "grainsize", location);
30950 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30951 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30952 OMP_CLAUSE_CHAIN (c) = list;
30958 priority ( expression ) */
30961 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30962 location_t location)
30966 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30969 t = cp_parser_expression (parser);
30971 if (t == error_mark_node
30972 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30973 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30974 /*or_comma=*/false,
30975 /*consume_paren=*/true);
30977 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30978 "priority", location);
30980 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30981 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30982 OMP_CLAUSE_CHAIN (c) = list;
30988 hint ( expression ) */
30991 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30992 location_t location)
30996 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30999 t = cp_parser_expression (parser);
31001 if (t == error_mark_node
31002 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31003 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31004 /*or_comma=*/false,
31005 /*consume_paren=*/true);
31007 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31009 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31010 OMP_CLAUSE_HINT_EXPR (c) = t;
31011 OMP_CLAUSE_CHAIN (c) = list;
31017 defaultmap ( tofrom : scalar ) */
31020 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31021 location_t location)
31026 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31029 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31031 cp_parser_error (parser, "expected %<tofrom%>");
31034 id = cp_lexer_peek_token (parser->lexer)->u.value;
31035 p = IDENTIFIER_POINTER (id);
31036 if (strcmp (p, "tofrom") != 0)
31038 cp_parser_error (parser, "expected %<tofrom%>");
31041 cp_lexer_consume_token (parser->lexer);
31042 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31045 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31047 cp_parser_error (parser, "expected %<scalar%>");
31050 id = cp_lexer_peek_token (parser->lexer)->u.value;
31051 p = IDENTIFIER_POINTER (id);
31052 if (strcmp (p, "scalar") != 0)
31054 cp_parser_error (parser, "expected %<scalar%>");
31057 cp_lexer_consume_token (parser->lexer);
31058 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31061 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31064 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31065 OMP_CLAUSE_CHAIN (c) = list;
31069 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31070 /*or_comma=*/false,
31071 /*consume_paren=*/true);
31079 ordered ( constant-expression ) */
31082 cp_parser_omp_clause_ordered (cp_parser *parser,
31083 tree list, location_t location)
31085 tree c, num = NULL_TREE;
31088 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31089 "ordered", location);
31091 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31093 cp_lexer_consume_token (parser->lexer);
31095 num = cp_parser_constant_expression (parser);
31097 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31098 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31099 /*or_comma=*/false,
31100 /*consume_paren=*/true);
31102 if (num == error_mark_node)
31104 num = fold_non_dependent_expr (num);
31105 if (!tree_fits_shwi_p (num)
31106 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31107 || (n = tree_to_shwi (num)) <= 0
31110 error_at (location,
31111 "ordered argument needs positive constant integer "
31117 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31118 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31119 OMP_CLAUSE_CHAIN (c) = list;
31124 reduction ( reduction-operator : variable-list )
31126 reduction-operator:
31127 One of: + * - & ^ | && ||
31131 reduction-operator:
31132 One of: + * - & ^ | && || min max
31136 reduction-operator:
31137 One of: + * - & ^ | && ||
31141 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31143 enum tree_code code = ERROR_MARK;
31144 tree nlist, c, id = NULL_TREE;
31146 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31149 switch (cp_lexer_peek_token (parser->lexer)->type)
31151 case CPP_PLUS: code = PLUS_EXPR; break;
31152 case CPP_MULT: code = MULT_EXPR; break;
31153 case CPP_MINUS: code = MINUS_EXPR; break;
31154 case CPP_AND: code = BIT_AND_EXPR; break;
31155 case CPP_XOR: code = BIT_XOR_EXPR; break;
31156 case CPP_OR: code = BIT_IOR_EXPR; break;
31157 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31158 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31162 if (code != ERROR_MARK)
31163 cp_lexer_consume_token (parser->lexer);
31166 bool saved_colon_corrects_to_scope_p;
31167 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31168 parser->colon_corrects_to_scope_p = false;
31169 id = cp_parser_id_expression (parser, /*template_p=*/false,
31170 /*check_dependency_p=*/true,
31171 /*template_p=*/NULL,
31172 /*declarator_p=*/false,
31173 /*optional_p=*/false);
31174 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31175 if (identifier_p (id))
31177 const char *p = IDENTIFIER_POINTER (id);
31179 if (strcmp (p, "min") == 0)
31181 else if (strcmp (p, "max") == 0)
31183 else if (id == ansi_opname (PLUS_EXPR))
31185 else if (id == ansi_opname (MULT_EXPR))
31187 else if (id == ansi_opname (MINUS_EXPR))
31189 else if (id == ansi_opname (BIT_AND_EXPR))
31190 code = BIT_AND_EXPR;
31191 else if (id == ansi_opname (BIT_IOR_EXPR))
31192 code = BIT_IOR_EXPR;
31193 else if (id == ansi_opname (BIT_XOR_EXPR))
31194 code = BIT_XOR_EXPR;
31195 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31196 code = TRUTH_ANDIF_EXPR;
31197 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31198 code = TRUTH_ORIF_EXPR;
31199 id = omp_reduction_id (code, id, NULL_TREE);
31200 tree scope = parser->scope;
31202 id = build_qualified_name (NULL_TREE, scope, id, false);
31203 parser->scope = NULL_TREE;
31204 parser->qualifying_scope = NULL_TREE;
31205 parser->object_scope = NULL_TREE;
31209 error ("invalid reduction-identifier");
31211 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31212 /*or_comma=*/false,
31213 /*consume_paren=*/true);
31218 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31221 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31223 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31225 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31226 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31233 schedule ( schedule-kind )
31234 schedule ( schedule-kind , expression )
31237 static | dynamic | guided | runtime | auto
31240 schedule ( schedule-modifier : schedule-kind )
31241 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31249 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31252 int modifiers = 0, nmodifiers = 0;
31254 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31257 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31259 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31261 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31262 const char *p = IDENTIFIER_POINTER (id);
31263 if (strcmp ("simd", p) == 0)
31264 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31265 else if (strcmp ("monotonic", p) == 0)
31266 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31267 else if (strcmp ("nonmonotonic", p) == 0)
31268 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31271 cp_lexer_consume_token (parser->lexer);
31272 if (nmodifiers++ == 0
31273 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31274 cp_lexer_consume_token (parser->lexer);
31277 cp_parser_require (parser, CPP_COLON, RT_COLON);
31282 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31284 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31285 const char *p = IDENTIFIER_POINTER (id);
31290 if (strcmp ("dynamic", p) != 0)
31292 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31296 if (strcmp ("guided", p) != 0)
31298 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31302 if (strcmp ("runtime", p) != 0)
31304 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31311 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31312 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31313 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31314 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31317 cp_lexer_consume_token (parser->lexer);
31319 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31320 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31321 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31322 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31324 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31329 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31332 cp_lexer_consume_token (parser->lexer);
31334 token = cp_lexer_peek_token (parser->lexer);
31335 t = cp_parser_assignment_expression (parser);
31337 if (t == error_mark_node)
31339 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31340 error_at (token->location, "schedule %<runtime%> does not take "
31341 "a %<chunk_size%> parameter");
31342 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31343 error_at (token->location, "schedule %<auto%> does not take "
31344 "a %<chunk_size%> parameter");
31346 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31348 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31351 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31354 OMP_CLAUSE_SCHEDULE_KIND (c)
31355 = (enum omp_clause_schedule_kind)
31356 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31358 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31359 OMP_CLAUSE_CHAIN (c) = list;
31363 cp_parser_error (parser, "invalid schedule kind");
31365 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31366 /*or_comma=*/false,
31367 /*consume_paren=*/true);
31375 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31376 tree list, location_t location)
31380 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31382 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31383 OMP_CLAUSE_CHAIN (c) = list;
31392 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31393 tree list, location_t location)
31395 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31396 tree c = build_omp_clause (location, code);
31397 OMP_CLAUSE_CHAIN (c) = list;
31408 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31409 enum omp_clause_code code,
31410 tree list, location_t location)
31412 tree c = build_omp_clause (location, code);
31413 OMP_CLAUSE_CHAIN (c) = list;
31421 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31422 tree list, location_t location)
31424 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31425 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31426 OMP_CLAUSE_CHAIN (c) = list;
31435 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31436 enum omp_clause_code code,
31437 tree list, location_t location)
31439 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31440 tree c = build_omp_clause (location, code);
31441 OMP_CLAUSE_CHAIN (c) = list;
31446 num_teams ( expression ) */
31449 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31450 location_t location)
31454 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31457 t = cp_parser_expression (parser);
31459 if (t == error_mark_node
31460 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31461 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31462 /*or_comma=*/false,
31463 /*consume_paren=*/true);
31465 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31466 "num_teams", location);
31468 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31469 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31470 OMP_CLAUSE_CHAIN (c) = list;
31476 thread_limit ( expression ) */
31479 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31480 location_t location)
31484 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31487 t = cp_parser_expression (parser);
31489 if (t == error_mark_node
31490 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31491 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31492 /*or_comma=*/false,
31493 /*consume_paren=*/true);
31495 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31496 "thread_limit", location);
31498 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31499 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31500 OMP_CLAUSE_CHAIN (c) = list;
31506 aligned ( variable-list )
31507 aligned ( variable-list : constant-expression ) */
31510 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31512 tree nlist, c, alignment = NULL_TREE;
31515 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31518 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31523 alignment = cp_parser_constant_expression (parser);
31525 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31526 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31527 /*or_comma=*/false,
31528 /*consume_paren=*/true);
31530 if (alignment == error_mark_node)
31531 alignment = NULL_TREE;
31534 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31535 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31541 linear ( variable-list )
31542 linear ( variable-list : expression )
31545 linear ( modifier ( variable-list ) )
31546 linear ( modifier ( variable-list ) : expression ) */
31549 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31550 bool is_cilk_simd_fn, bool declare_simd)
31552 tree nlist, c, step = integer_one_node;
31554 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31556 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31559 if (!is_cilk_simd_fn
31560 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31562 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31563 const char *p = IDENTIFIER_POINTER (id);
31565 if (strcmp ("ref", p) == 0)
31566 kind = OMP_CLAUSE_LINEAR_REF;
31567 else if (strcmp ("val", p) == 0)
31568 kind = OMP_CLAUSE_LINEAR_VAL;
31569 else if (strcmp ("uval", p) == 0)
31570 kind = OMP_CLAUSE_LINEAR_UVAL;
31571 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31572 cp_lexer_consume_token (parser->lexer);
31574 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31577 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31578 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31582 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31583 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31585 cp_parser_require (parser, CPP_COLON, RT_COLON);
31586 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31587 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31588 /*or_comma=*/false,
31589 /*consume_paren=*/true);
31596 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31597 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31599 cp_token *token = cp_lexer_peek_token (parser->lexer);
31600 cp_parser_parse_tentatively (parser);
31601 step = cp_parser_id_expression (parser, /*template_p=*/false,
31602 /*check_dependency_p=*/true,
31603 /*template_p=*/NULL,
31604 /*declarator_p=*/false,
31605 /*optional_p=*/false);
31606 if (step != error_mark_node)
31607 step = cp_parser_lookup_name_simple (parser, step, token->location);
31608 if (step == error_mark_node)
31611 cp_parser_abort_tentative_parse (parser);
31613 else if (!cp_parser_parse_definitely (parser))
31617 step = cp_parser_expression (parser);
31619 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31621 sorry ("using parameters for %<linear%> step is not supported yet");
31622 step = integer_one_node;
31624 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31625 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31626 /*or_comma=*/false,
31627 /*consume_paren=*/true);
31629 if (step == error_mark_node)
31633 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31635 OMP_CLAUSE_LINEAR_STEP (c) = step;
31636 OMP_CLAUSE_LINEAR_KIND (c) = kind;
31643 safelen ( constant-expression ) */
31646 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31647 location_t location)
31651 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31654 t = cp_parser_constant_expression (parser);
31656 if (t == error_mark_node
31657 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31658 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31659 /*or_comma=*/false,
31660 /*consume_paren=*/true);
31662 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31664 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31665 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31666 OMP_CLAUSE_CHAIN (c) = list;
31672 simdlen ( constant-expression ) */
31675 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31676 location_t location)
31680 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31683 t = cp_parser_constant_expression (parser);
31685 if (t == error_mark_node
31686 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31687 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31688 /*or_comma=*/false,
31689 /*consume_paren=*/true);
31691 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31693 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31694 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31695 OMP_CLAUSE_CHAIN (c) = list;
31702 identifier [+/- integer]
31703 vec , identifier [+/- integer]
31707 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31714 cp_parser_error (parser, "expected identifier");
31718 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31720 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31721 tree t, identifier = cp_parser_identifier (parser);
31722 tree addend = NULL;
31724 if (identifier == error_mark_node)
31725 t = error_mark_node;
31728 t = cp_parser_lookup_name_simple
31729 (parser, identifier,
31730 cp_lexer_peek_token (parser->lexer)->location);
31731 if (t == error_mark_node)
31732 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31737 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31739 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31741 addend = integer_zero_node;
31742 goto add_to_vector;
31744 cp_lexer_consume_token (parser->lexer);
31746 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31748 cp_parser_error (parser, "expected integer");
31752 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31753 if (TREE_CODE (addend) != INTEGER_CST)
31755 cp_parser_error (parser, "expected integer");
31758 cp_lexer_consume_token (parser->lexer);
31761 if (t != error_mark_node)
31763 vec = tree_cons (addend, t, vec);
31765 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31768 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31771 cp_lexer_consume_token (parser->lexer);
31774 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31776 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31777 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31778 OMP_CLAUSE_DECL (u) = nreverse (vec);
31779 OMP_CLAUSE_CHAIN (u) = list;
31786 depend ( depend-kind : variable-list )
31794 depend ( sink : vec ) */
31797 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31800 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31802 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31805 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31807 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31808 const char *p = IDENTIFIER_POINTER (id);
31810 if (strcmp ("in", p) == 0)
31811 kind = OMP_CLAUSE_DEPEND_IN;
31812 else if (strcmp ("inout", p) == 0)
31813 kind = OMP_CLAUSE_DEPEND_INOUT;
31814 else if (strcmp ("out", p) == 0)
31815 kind = OMP_CLAUSE_DEPEND_OUT;
31816 else if (strcmp ("source", p) == 0)
31817 kind = OMP_CLAUSE_DEPEND_SOURCE;
31818 else if (strcmp ("sink", p) == 0)
31819 kind = OMP_CLAUSE_DEPEND_SINK;
31826 cp_lexer_consume_token (parser->lexer);
31828 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31830 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31831 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31832 OMP_CLAUSE_DECL (c) = NULL_TREE;
31833 OMP_CLAUSE_CHAIN (c) = list;
31834 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31835 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31836 /*or_comma=*/false,
31837 /*consume_paren=*/true);
31841 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31844 if (kind == OMP_CLAUSE_DEPEND_SINK)
31845 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31848 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31851 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31852 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31857 cp_parser_error (parser, "invalid depend kind");
31859 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31860 /*or_comma=*/false,
31861 /*consume_paren=*/true);
31866 map ( map-kind : variable-list )
31867 map ( variable-list )
31870 alloc | to | from | tofrom
31874 alloc | to | from | tofrom | release | delete
31876 map ( always [,] map-kind: variable-list ) */
31879 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31882 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31883 bool always = false;
31885 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31890 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31891 const char *p = IDENTIFIER_POINTER (id);
31893 if (strcmp ("always", p) == 0)
31896 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31898 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31899 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31901 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31905 cp_lexer_consume_token (parser->lexer);
31907 cp_lexer_consume_token (parser->lexer);
31912 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31913 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31915 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31916 const char *p = IDENTIFIER_POINTER (id);
31918 if (strcmp ("alloc", p) == 0)
31919 kind = GOMP_MAP_ALLOC;
31920 else if (strcmp ("to", p) == 0)
31921 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31922 else if (strcmp ("from", p) == 0)
31923 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31924 else if (strcmp ("tofrom", p) == 0)
31925 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31926 else if (strcmp ("release", p) == 0)
31927 kind = GOMP_MAP_RELEASE;
31930 cp_parser_error (parser, "invalid map kind");
31931 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31932 /*or_comma=*/false,
31933 /*consume_paren=*/true);
31936 cp_lexer_consume_token (parser->lexer);
31937 cp_lexer_consume_token (parser->lexer);
31939 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31940 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31942 kind = GOMP_MAP_DELETE;
31943 cp_lexer_consume_token (parser->lexer);
31944 cp_lexer_consume_token (parser->lexer);
31947 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31950 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31951 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31957 device ( expression ) */
31960 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31961 location_t location)
31965 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31968 t = cp_parser_expression (parser);
31970 if (t == error_mark_node
31971 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31972 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31973 /*or_comma=*/false,
31974 /*consume_paren=*/true);
31976 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31977 "device", location);
31979 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31980 OMP_CLAUSE_DEVICE_ID (c) = t;
31981 OMP_CLAUSE_CHAIN (c) = list;
31987 dist_schedule ( static )
31988 dist_schedule ( static , expression ) */
31991 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31992 location_t location)
31996 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31999 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32001 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32003 cp_lexer_consume_token (parser->lexer);
32005 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32007 cp_lexer_consume_token (parser->lexer);
32009 t = cp_parser_assignment_expression (parser);
32011 if (t == error_mark_node)
32013 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32015 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32018 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32021 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32023 OMP_CLAUSE_CHAIN (c) = list;
32027 cp_parser_error (parser, "invalid dist_schedule kind");
32029 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32030 /*or_comma=*/false,
32031 /*consume_paren=*/true);
32036 proc_bind ( proc-bind-kind )
32039 master | close | spread */
32042 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32043 location_t location)
32046 enum omp_clause_proc_bind_kind kind;
32048 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32051 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32053 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32054 const char *p = IDENTIFIER_POINTER (id);
32056 if (strcmp ("master", p) == 0)
32057 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32058 else if (strcmp ("close", p) == 0)
32059 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32060 else if (strcmp ("spread", p) == 0)
32061 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32068 cp_lexer_consume_token (parser->lexer);
32069 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32072 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32073 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32075 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32076 OMP_CLAUSE_CHAIN (c) = list;
32080 cp_parser_error (parser, "invalid depend kind");
32082 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32083 /*or_comma=*/false,
32084 /*consume_paren=*/true);
32089 async [( int-expr )] */
32092 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32095 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32097 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32099 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32101 cp_lexer_consume_token (parser->lexer);
32103 t = cp_parser_expression (parser);
32104 if (t == error_mark_node
32105 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32106 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32107 /*or_comma=*/false,
32108 /*consume_paren=*/true);
32111 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32113 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32114 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32115 OMP_CLAUSE_CHAIN (c) = list;
32121 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32122 is a bitmask in MASK. Return the list of clauses found. */
32125 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32126 const char *where, cp_token *pragma_tok,
32127 bool finish_p = true)
32129 tree clauses = NULL;
32132 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32135 pragma_omp_clause c_kind;
32136 omp_clause_code code;
32137 const char *c_name;
32138 tree prev = clauses;
32140 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32141 cp_lexer_consume_token (parser->lexer);
32143 here = cp_lexer_peek_token (parser->lexer)->location;
32144 c_kind = cp_parser_omp_clause_name (parser);
32148 case PRAGMA_OACC_CLAUSE_ASYNC:
32149 clauses = cp_parser_oacc_clause_async (parser, clauses);
32152 case PRAGMA_OACC_CLAUSE_AUTO:
32153 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32157 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32158 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32159 c_name = "collapse";
32161 case PRAGMA_OACC_CLAUSE_COPY:
32162 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32165 case PRAGMA_OACC_CLAUSE_COPYIN:
32166 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32169 case PRAGMA_OACC_CLAUSE_COPYOUT:
32170 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32171 c_name = "copyout";
32173 case PRAGMA_OACC_CLAUSE_CREATE:
32174 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32177 case PRAGMA_OACC_CLAUSE_DELETE:
32178 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32181 case PRAGMA_OMP_CLAUSE_DEFAULT:
32182 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32183 c_name = "default";
32185 case PRAGMA_OACC_CLAUSE_DEVICE:
32186 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32189 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32190 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32191 c_name = "deviceptr";
32193 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32194 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32195 c_name = "device_resident";
32197 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32198 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32200 c_name = "firstprivate";
32202 case PRAGMA_OACC_CLAUSE_GANG:
32204 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32207 case PRAGMA_OACC_CLAUSE_HOST:
32208 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32211 case PRAGMA_OACC_CLAUSE_IF:
32212 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32215 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32216 clauses = cp_parser_oacc_simple_clause (parser,
32217 OMP_CLAUSE_INDEPENDENT,
32219 c_name = "independent";
32221 case PRAGMA_OACC_CLAUSE_LINK:
32222 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32225 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32226 code = OMP_CLAUSE_NUM_GANGS;
32227 c_name = "num_gangs";
32228 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32231 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32232 c_name = "num_workers";
32233 code = OMP_CLAUSE_NUM_WORKERS;
32234 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32237 case PRAGMA_OACC_CLAUSE_PRESENT:
32238 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32239 c_name = "present";
32241 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32242 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32243 c_name = "present_or_copy";
32245 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32246 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32247 c_name = "present_or_copyin";
32249 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32250 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32251 c_name = "present_or_copyout";
32253 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32254 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32255 c_name = "present_or_create";
32257 case PRAGMA_OACC_CLAUSE_PRIVATE:
32258 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32260 c_name = "private";
32262 case PRAGMA_OACC_CLAUSE_REDUCTION:
32263 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32264 c_name = "reduction";
32266 case PRAGMA_OACC_CLAUSE_SELF:
32267 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32270 case PRAGMA_OACC_CLAUSE_SEQ:
32271 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32275 case PRAGMA_OACC_CLAUSE_TILE:
32276 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32279 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32280 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32282 c_name = "use_device";
32284 case PRAGMA_OACC_CLAUSE_VECTOR:
32286 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32289 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32290 c_name = "vector_length";
32291 code = OMP_CLAUSE_VECTOR_LENGTH;
32292 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32295 case PRAGMA_OACC_CLAUSE_WAIT:
32296 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32299 case PRAGMA_OACC_CLAUSE_WORKER:
32301 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32305 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32311 if (((mask >> c_kind) & 1) == 0)
32313 /* Remove the invalid clause(s) from the list to avoid
32314 confusing the rest of the compiler. */
32316 error_at (here, "%qs is not valid for %qs", c_name, where);
32321 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32324 return finish_omp_clauses (clauses, false);
32329 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32330 is a bitmask in MASK. Return the list of clauses found; the result
32331 of clause default goes in *pdefault. */
32334 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32335 const char *where, cp_token *pragma_tok,
32336 bool finish_p = true)
32338 tree clauses = NULL;
32340 cp_token *token = NULL;
32342 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32344 pragma_omp_clause c_kind;
32345 const char *c_name;
32346 tree prev = clauses;
32348 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32349 cp_lexer_consume_token (parser->lexer);
32351 token = cp_lexer_peek_token (parser->lexer);
32352 c_kind = cp_parser_omp_clause_name (parser);
32356 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32357 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32359 c_name = "collapse";
32361 case PRAGMA_OMP_CLAUSE_COPYIN:
32362 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32365 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32366 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32368 c_name = "copyprivate";
32370 case PRAGMA_OMP_CLAUSE_DEFAULT:
32371 clauses = cp_parser_omp_clause_default (parser, clauses,
32372 token->location, false);
32373 c_name = "default";
32375 case PRAGMA_OMP_CLAUSE_FINAL:
32376 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32379 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32380 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32382 c_name = "firstprivate";
32384 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32385 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32387 c_name = "grainsize";
32389 case PRAGMA_OMP_CLAUSE_HINT:
32390 clauses = cp_parser_omp_clause_hint (parser, clauses,
32394 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32395 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32397 c_name = "defaultmap";
32399 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32400 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32402 c_name = "use_device_ptr";
32404 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32405 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32407 c_name = "is_device_ptr";
32409 case PRAGMA_OMP_CLAUSE_IF:
32410 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32414 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32415 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32417 c_name = "lastprivate";
32419 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32420 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32422 c_name = "mergeable";
32424 case PRAGMA_OMP_CLAUSE_NOWAIT:
32425 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32428 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32429 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32431 c_name = "num_tasks";
32433 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32434 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32436 c_name = "num_threads";
32438 case PRAGMA_OMP_CLAUSE_ORDERED:
32439 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32441 c_name = "ordered";
32443 case PRAGMA_OMP_CLAUSE_PRIORITY:
32444 clauses = cp_parser_omp_clause_priority (parser, clauses,
32446 c_name = "priority";
32448 case PRAGMA_OMP_CLAUSE_PRIVATE:
32449 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32451 c_name = "private";
32453 case PRAGMA_OMP_CLAUSE_REDUCTION:
32454 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32455 c_name = "reduction";
32457 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32458 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32460 c_name = "schedule";
32462 case PRAGMA_OMP_CLAUSE_SHARED:
32463 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32467 case PRAGMA_OMP_CLAUSE_UNTIED:
32468 clauses = cp_parser_omp_clause_untied (parser, clauses,
32472 case PRAGMA_OMP_CLAUSE_INBRANCH:
32473 case PRAGMA_CILK_CLAUSE_MASK:
32474 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32475 clauses, token->location);
32476 c_name = "inbranch";
32478 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32479 case PRAGMA_CILK_CLAUSE_NOMASK:
32480 clauses = cp_parser_omp_clause_branch (parser,
32481 OMP_CLAUSE_NOTINBRANCH,
32482 clauses, token->location);
32483 c_name = "notinbranch";
32485 case PRAGMA_OMP_CLAUSE_PARALLEL:
32486 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32487 clauses, token->location);
32488 c_name = "parallel";
32492 error_at (token->location, "%qs must be the first clause of %qs",
32497 case PRAGMA_OMP_CLAUSE_FOR:
32498 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32499 clauses, token->location);
32502 goto clause_not_first;
32504 case PRAGMA_OMP_CLAUSE_SECTIONS:
32505 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32506 clauses, token->location);
32507 c_name = "sections";
32509 goto clause_not_first;
32511 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32512 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32513 clauses, token->location);
32514 c_name = "taskgroup";
32516 goto clause_not_first;
32518 case PRAGMA_OMP_CLAUSE_LINK:
32519 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32522 case PRAGMA_OMP_CLAUSE_TO:
32523 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32524 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32527 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32530 case PRAGMA_OMP_CLAUSE_FROM:
32531 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32534 case PRAGMA_OMP_CLAUSE_UNIFORM:
32535 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32537 c_name = "uniform";
32539 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32540 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32542 c_name = "num_teams";
32544 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32545 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32547 c_name = "thread_limit";
32549 case PRAGMA_OMP_CLAUSE_ALIGNED:
32550 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32551 c_name = "aligned";
32553 case PRAGMA_OMP_CLAUSE_LINEAR:
32555 bool cilk_simd_fn = false, declare_simd = false;
32556 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32557 cilk_simd_fn = true;
32558 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32559 declare_simd = true;
32560 clauses = cp_parser_omp_clause_linear (parser, clauses,
32561 cilk_simd_fn, declare_simd);
32565 case PRAGMA_OMP_CLAUSE_DEPEND:
32566 clauses = cp_parser_omp_clause_depend (parser, clauses,
32570 case PRAGMA_OMP_CLAUSE_MAP:
32571 clauses = cp_parser_omp_clause_map (parser, clauses);
32574 case PRAGMA_OMP_CLAUSE_DEVICE:
32575 clauses = cp_parser_omp_clause_device (parser, clauses,
32579 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32580 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32582 c_name = "dist_schedule";
32584 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32585 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32587 c_name = "proc_bind";
32589 case PRAGMA_OMP_CLAUSE_SAFELEN:
32590 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32592 c_name = "safelen";
32594 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32595 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32597 c_name = "simdlen";
32599 case PRAGMA_OMP_CLAUSE_NOGROUP:
32600 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32602 c_name = "nogroup";
32604 case PRAGMA_OMP_CLAUSE_THREADS:
32606 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32607 clauses, token->location);
32608 c_name = "threads";
32610 case PRAGMA_OMP_CLAUSE_SIMD:
32612 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32613 clauses, token->location);
32616 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32617 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32618 c_name = "simdlen";
32621 cp_parser_error (parser, "expected %<#pragma omp%> clause");
32627 if (((mask >> c_kind) & 1) == 0)
32629 /* Remove the invalid clause(s) from the list to avoid
32630 confusing the rest of the compiler. */
32632 error_at (token->location, "%qs is not valid for %qs", c_name, where);
32636 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32637 no reason to skip to the end. */
32638 if (!(flag_cilkplus && pragma_tok == NULL))
32639 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32642 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32643 return finish_omp_clauses (clauses, false, true);
32645 return finish_omp_clauses (clauses, true);
32654 In practice, we're also interested in adding the statement to an
32655 outer node. So it is convenient if we work around the fact that
32656 cp_parser_statement calls add_stmt. */
32659 cp_parser_begin_omp_structured_block (cp_parser *parser)
32661 unsigned save = parser->in_statement;
32663 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32664 This preserves the "not within loop or switch" style error messages
32665 for nonsense cases like
32671 if (parser->in_statement)
32672 parser->in_statement = IN_OMP_BLOCK;
32678 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32680 parser->in_statement = save;
32684 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
32686 tree stmt = begin_omp_structured_block ();
32687 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32689 cp_parser_statement (parser, NULL_TREE, false, if_p);
32691 cp_parser_end_omp_structured_block (parser, save);
32692 return finish_omp_structured_block (stmt);
32696 # pragma omp atomic new-line
32700 x binop= expr | x++ | ++x | x-- | --x
32702 +, *, -, /, &, ^, |, <<, >>
32704 where x is an lvalue expression with scalar type.
32707 # pragma omp atomic new-line
32710 # pragma omp atomic read new-line
32713 # pragma omp atomic write new-line
32716 # pragma omp atomic update new-line
32719 # pragma omp atomic capture new-line
32722 # pragma omp atomic capture new-line
32730 expression-stmt | x = x binop expr
32732 v = expression-stmt
32734 { v = x; update-stmt; } | { update-stmt; v = x; }
32738 expression-stmt | x = x binop expr | x = expr binop x
32742 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32744 where x and v are lvalue expressions with scalar type. */
32747 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32749 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32750 tree rhs1 = NULL_TREE, orig_lhs;
32751 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32752 bool structured_block = false;
32753 bool seq_cst = false;
32755 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32757 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32758 const char *p = IDENTIFIER_POINTER (id);
32760 if (!strcmp (p, "seq_cst"))
32763 cp_lexer_consume_token (parser->lexer);
32764 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32765 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32766 cp_lexer_consume_token (parser->lexer);
32769 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32771 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32772 const char *p = IDENTIFIER_POINTER (id);
32774 if (!strcmp (p, "read"))
32775 code = OMP_ATOMIC_READ;
32776 else if (!strcmp (p, "write"))
32778 else if (!strcmp (p, "update"))
32780 else if (!strcmp (p, "capture"))
32781 code = OMP_ATOMIC_CAPTURE_NEW;
32785 cp_lexer_consume_token (parser->lexer);
32789 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32790 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32791 cp_lexer_consume_token (parser->lexer);
32793 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32795 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32796 const char *p = IDENTIFIER_POINTER (id);
32798 if (!strcmp (p, "seq_cst"))
32801 cp_lexer_consume_token (parser->lexer);
32805 cp_parser_require_pragma_eol (parser, pragma_tok);
32809 case OMP_ATOMIC_READ:
32810 case NOP_EXPR: /* atomic write */
32811 v = cp_parser_unary_expression (parser);
32812 if (v == error_mark_node)
32814 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32816 if (code == NOP_EXPR)
32817 lhs = cp_parser_expression (parser);
32819 lhs = cp_parser_unary_expression (parser);
32820 if (lhs == error_mark_node)
32822 if (code == NOP_EXPR)
32824 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32832 case OMP_ATOMIC_CAPTURE_NEW:
32833 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32835 cp_lexer_consume_token (parser->lexer);
32836 structured_block = true;
32840 v = cp_parser_unary_expression (parser);
32841 if (v == error_mark_node)
32843 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32851 lhs = cp_parser_unary_expression (parser);
32853 switch (TREE_CODE (lhs))
32858 case POSTINCREMENT_EXPR:
32859 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32860 code = OMP_ATOMIC_CAPTURE_OLD;
32862 case PREINCREMENT_EXPR:
32863 lhs = TREE_OPERAND (lhs, 0);
32864 opcode = PLUS_EXPR;
32865 rhs = integer_one_node;
32868 case POSTDECREMENT_EXPR:
32869 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32870 code = OMP_ATOMIC_CAPTURE_OLD;
32872 case PREDECREMENT_EXPR:
32873 lhs = TREE_OPERAND (lhs, 0);
32874 opcode = MINUS_EXPR;
32875 rhs = integer_one_node;
32878 case COMPOUND_EXPR:
32879 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32880 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32881 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32882 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32883 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32884 (TREE_OPERAND (lhs, 1), 0), 0)))
32886 /* Undo effects of boolean_increment for post {in,de}crement. */
32887 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32890 if (TREE_CODE (lhs) == MODIFY_EXPR
32891 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32893 /* Undo effects of boolean_increment. */
32894 if (integer_onep (TREE_OPERAND (lhs, 1)))
32896 /* This is pre or post increment. */
32897 rhs = TREE_OPERAND (lhs, 1);
32898 lhs = TREE_OPERAND (lhs, 0);
32900 if (code == OMP_ATOMIC_CAPTURE_NEW
32901 && !structured_block
32902 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32903 code = OMP_ATOMIC_CAPTURE_OLD;
32909 switch (cp_lexer_peek_token (parser->lexer)->type)
32912 opcode = MULT_EXPR;
32915 opcode = TRUNC_DIV_EXPR;
32918 opcode = PLUS_EXPR;
32921 opcode = MINUS_EXPR;
32923 case CPP_LSHIFT_EQ:
32924 opcode = LSHIFT_EXPR;
32926 case CPP_RSHIFT_EQ:
32927 opcode = RSHIFT_EXPR;
32930 opcode = BIT_AND_EXPR;
32933 opcode = BIT_IOR_EXPR;
32936 opcode = BIT_XOR_EXPR;
32939 enum cp_parser_prec oprec;
32941 cp_lexer_consume_token (parser->lexer);
32942 cp_parser_parse_tentatively (parser);
32943 rhs1 = cp_parser_simple_cast_expression (parser);
32944 if (rhs1 == error_mark_node)
32946 cp_parser_abort_tentative_parse (parser);
32947 cp_parser_simple_cast_expression (parser);
32950 token = cp_lexer_peek_token (parser->lexer);
32951 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32953 cp_parser_abort_tentative_parse (parser);
32954 cp_parser_parse_tentatively (parser);
32955 rhs = cp_parser_binary_expression (parser, false, true,
32956 PREC_NOT_OPERATOR, NULL);
32957 if (rhs == error_mark_node)
32959 cp_parser_abort_tentative_parse (parser);
32960 cp_parser_binary_expression (parser, false, true,
32961 PREC_NOT_OPERATOR, NULL);
32964 switch (TREE_CODE (rhs))
32967 case TRUNC_DIV_EXPR:
32976 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32978 if (cp_parser_parse_definitely (parser))
32980 opcode = TREE_CODE (rhs);
32981 rhs1 = TREE_OPERAND (rhs, 0);
32982 rhs = TREE_OPERAND (rhs, 1);
32992 cp_parser_abort_tentative_parse (parser);
32993 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32995 rhs = cp_parser_expression (parser);
32996 if (rhs == error_mark_node)
33002 cp_parser_error (parser,
33003 "invalid form of %<#pragma omp atomic%>");
33006 if (!cp_parser_parse_definitely (parser))
33008 switch (token->type)
33010 case CPP_SEMICOLON:
33011 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33013 code = OMP_ATOMIC_CAPTURE_OLD;
33018 cp_lexer_consume_token (parser->lexer);
33021 else if (structured_block)
33028 cp_parser_error (parser,
33029 "invalid form of %<#pragma omp atomic%>");
33032 opcode = MULT_EXPR;
33035 opcode = TRUNC_DIV_EXPR;
33038 opcode = PLUS_EXPR;
33041 opcode = MINUS_EXPR;
33044 opcode = LSHIFT_EXPR;
33047 opcode = RSHIFT_EXPR;
33050 opcode = BIT_AND_EXPR;
33053 opcode = BIT_IOR_EXPR;
33056 opcode = BIT_XOR_EXPR;
33059 cp_parser_error (parser,
33060 "invalid operator for %<#pragma omp atomic%>");
33063 oprec = TOKEN_PRECEDENCE (token);
33064 gcc_assert (oprec != PREC_NOT_OPERATOR);
33065 if (commutative_tree_code (opcode))
33066 oprec = (enum cp_parser_prec) (oprec - 1);
33067 cp_lexer_consume_token (parser->lexer);
33068 rhs = cp_parser_binary_expression (parser, false, false,
33070 if (rhs == error_mark_node)
33075 cp_parser_error (parser,
33076 "invalid operator for %<#pragma omp atomic%>");
33079 cp_lexer_consume_token (parser->lexer);
33081 rhs = cp_parser_expression (parser);
33082 if (rhs == error_mark_node)
33087 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33089 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33091 v = cp_parser_unary_expression (parser);
33092 if (v == error_mark_node)
33094 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33096 lhs1 = cp_parser_unary_expression (parser);
33097 if (lhs1 == error_mark_node)
33100 if (structured_block)
33102 cp_parser_consume_semicolon_at_end_of_statement (parser);
33103 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33106 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33107 if (!structured_block)
33108 cp_parser_consume_semicolon_at_end_of_statement (parser);
33112 cp_parser_skip_to_end_of_block_or_statement (parser);
33113 if (structured_block)
33115 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33116 cp_lexer_consume_token (parser->lexer);
33117 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33119 cp_parser_skip_to_end_of_block_or_statement (parser);
33120 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33121 cp_lexer_consume_token (parser->lexer);
33128 # pragma omp barrier new-line */
33131 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33133 cp_parser_require_pragma_eol (parser, pragma_tok);
33134 finish_omp_barrier ();
33138 # pragma omp critical [(name)] new-line
33142 # pragma omp critical [(name) [hint(expression)]] new-line
33143 structured-block */
33145 #define OMP_CRITICAL_CLAUSE_MASK \
33146 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33149 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33151 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33153 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33155 cp_lexer_consume_token (parser->lexer);
33157 name = cp_parser_identifier (parser);
33159 if (name == error_mark_node
33160 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33161 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33162 /*or_comma=*/false,
33163 /*consume_paren=*/true);
33164 if (name == error_mark_node)
33167 clauses = cp_parser_omp_all_clauses (parser,
33168 OMP_CRITICAL_CLAUSE_MASK,
33169 "#pragma omp critical", pragma_tok);
33172 cp_parser_require_pragma_eol (parser, pragma_tok);
33174 stmt = cp_parser_omp_structured_block (parser, if_p);
33175 return c_finish_omp_critical (input_location, stmt, name, clauses);
33179 # pragma omp flush flush-vars[opt] new-line
33182 ( variable-list ) */
33185 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33187 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33188 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33189 cp_parser_require_pragma_eol (parser, pragma_tok);
33191 finish_omp_flush ();
33194 /* Helper function, to parse omp for increment expression. */
33197 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33199 tree cond = cp_parser_binary_expression (parser, false, true,
33200 PREC_NOT_OPERATOR, NULL);
33201 if (cond == error_mark_node
33202 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33204 cp_parser_skip_to_end_of_statement (parser);
33205 return error_mark_node;
33208 switch (TREE_CODE (cond))
33216 if (code == CILK_SIMD || code == CILK_FOR)
33218 /* Fall through: OpenMP disallows NE_EXPR. */
33220 return error_mark_node;
33223 /* If decl is an iterator, preserve LHS and RHS of the relational
33224 expr until finish_omp_for. */
33226 && (type_dependent_expression_p (decl)
33227 || CLASS_TYPE_P (TREE_TYPE (decl))))
33230 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33232 TREE_OPERAND (cond, 0), ERROR_MARK,
33233 TREE_OPERAND (cond, 1), ERROR_MARK,
33234 /*overload=*/NULL, tf_warning_or_error);
33237 /* Helper function, to parse omp for increment expression. */
33240 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33242 cp_token *token = cp_lexer_peek_token (parser->lexer);
33248 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33250 op = (token->type == CPP_PLUS_PLUS
33251 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33252 cp_lexer_consume_token (parser->lexer);
33253 lhs = cp_parser_simple_cast_expression (parser);
33255 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33256 return error_mark_node;
33257 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33260 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33262 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33263 return error_mark_node;
33265 token = cp_lexer_peek_token (parser->lexer);
33266 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33268 op = (token->type == CPP_PLUS_PLUS
33269 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33270 cp_lexer_consume_token (parser->lexer);
33271 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33274 op = cp_parser_assignment_operator_opt (parser);
33275 if (op == ERROR_MARK)
33276 return error_mark_node;
33278 if (op != NOP_EXPR)
33280 rhs = cp_parser_assignment_expression (parser);
33281 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33282 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33285 lhs = cp_parser_binary_expression (parser, false, false,
33286 PREC_ADDITIVE_EXPRESSION, NULL);
33287 token = cp_lexer_peek_token (parser->lexer);
33288 decl_first = (lhs == decl
33289 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33292 if (token->type != CPP_PLUS
33293 && token->type != CPP_MINUS)
33294 return error_mark_node;
33298 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33299 cp_lexer_consume_token (parser->lexer);
33300 rhs = cp_parser_binary_expression (parser, false, false,
33301 PREC_ADDITIVE_EXPRESSION, NULL);
33302 token = cp_lexer_peek_token (parser->lexer);
33303 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33305 if (lhs == NULL_TREE)
33307 if (op == PLUS_EXPR)
33310 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33311 tf_warning_or_error);
33314 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33315 ERROR_MARK, NULL, tf_warning_or_error);
33318 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33323 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33324 || op == MINUS_EXPR)
33325 return error_mark_node;
33326 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33329 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33331 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33334 /* Parse the initialization statement of either an OpenMP for loop or
33335 a Cilk Plus for loop.
33337 Return true if the resulting construct should have an
33338 OMP_CLAUSE_PRIVATE added to it. */
33341 cp_parser_omp_for_loop_init (cp_parser *parser,
33342 enum tree_code code,
33343 tree &this_pre_body,
33344 vec<tree, va_gc> *for_block,
33350 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33353 tree add_private_clause = NULL_TREE;
33355 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33359 integer-type var = lb
33360 random-access-iterator-type var = lb
33361 pointer-type var = lb
33363 cp_decl_specifier_seq type_specifiers;
33365 /* First, try to parse as an initialized declaration. See
33366 cp_parser_condition, from whence the bulk of this is copied. */
33368 cp_parser_parse_tentatively (parser);
33369 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33370 /*is_trailing_return=*/false,
33372 if (cp_parser_parse_definitely (parser))
33374 /* If parsing a type specifier seq succeeded, then this
33375 MUST be a initialized declaration. */
33376 tree asm_specification, attributes;
33377 cp_declarator *declarator;
33379 declarator = cp_parser_declarator (parser,
33380 CP_PARSER_DECLARATOR_NAMED,
33381 /*ctor_dtor_or_conv_p=*/NULL,
33382 /*parenthesized_p=*/NULL,
33383 /*member_p=*/false,
33384 /*friend_p=*/false);
33385 attributes = cp_parser_attributes_opt (parser);
33386 asm_specification = cp_parser_asm_specification_opt (parser);
33388 if (declarator == cp_error_declarator)
33389 cp_parser_skip_to_end_of_statement (parser);
33393 tree pushed_scope, auto_node;
33395 decl = start_decl (declarator, &type_specifiers,
33396 SD_INITIALIZED, attributes,
33397 /*prefix_attributes=*/NULL_TREE,
33400 auto_node = type_uses_auto (TREE_TYPE (decl));
33401 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33403 if (cp_lexer_next_token_is (parser->lexer,
33406 if (code != CILK_SIMD && code != CILK_FOR)
33407 error ("parenthesized initialization is not allowed in "
33408 "OpenMP %<for%> loop");
33410 error ("parenthesized initialization is "
33411 "not allowed in for-loop");
33414 /* Trigger an error. */
33415 cp_parser_require (parser, CPP_EQ, RT_EQ);
33417 init = error_mark_node;
33418 cp_parser_skip_to_end_of_statement (parser);
33420 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33421 || type_dependent_expression_p (decl)
33424 bool is_direct_init, is_non_constant_init;
33426 init = cp_parser_initializer (parser,
33428 &is_non_constant_init);
33433 = do_auto_deduction (TREE_TYPE (decl), init,
33436 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33437 && !type_dependent_expression_p (decl))
33441 cp_finish_decl (decl, init, !is_non_constant_init,
33443 LOOKUP_ONLYCONVERTING);
33445 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33447 vec_safe_push (for_block, this_pre_body);
33451 init = pop_stmt_list (this_pre_body);
33452 this_pre_body = NULL_TREE;
33457 cp_lexer_consume_token (parser->lexer);
33458 init = cp_parser_assignment_expression (parser);
33461 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33462 init = error_mark_node;
33464 cp_finish_decl (decl, NULL_TREE,
33465 /*init_const_expr_p=*/false,
33467 LOOKUP_ONLYCONVERTING);
33471 pop_scope (pushed_scope);
33477 /* If parsing a type specifier sequence failed, then
33478 this MUST be a simple expression. */
33479 if (code == CILK_FOR)
33480 error ("%<_Cilk_for%> allows expression instead of declaration only "
33481 "in C, not in C++");
33482 cp_parser_parse_tentatively (parser);
33483 decl = cp_parser_primary_expression (parser, false, false,
33485 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33486 if (!cp_parser_error_occurred (parser)
33488 && (TREE_CODE (decl) == COMPONENT_REF
33489 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33491 cp_parser_abort_tentative_parse (parser);
33492 cp_parser_parse_tentatively (parser);
33493 cp_token *token = cp_lexer_peek_token (parser->lexer);
33494 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33495 /*check_dependency_p=*/true,
33496 /*template_p=*/NULL,
33497 /*declarator_p=*/false,
33498 /*optional_p=*/false);
33499 if (name != error_mark_node
33500 && last_tok == cp_lexer_peek_token (parser->lexer))
33502 decl = cp_parser_lookup_name_simple (parser, name,
33504 if (TREE_CODE (decl) == FIELD_DECL)
33505 add_private_clause = omp_privatize_field (decl, false);
33507 cp_parser_abort_tentative_parse (parser);
33508 cp_parser_parse_tentatively (parser);
33509 decl = cp_parser_primary_expression (parser, false, false,
33512 if (!cp_parser_error_occurred (parser)
33515 && CLASS_TYPE_P (TREE_TYPE (decl)))
33519 cp_parser_parse_definitely (parser);
33520 cp_parser_require (parser, CPP_EQ, RT_EQ);
33521 rhs = cp_parser_assignment_expression (parser);
33523 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33526 tf_warning_or_error));
33527 if (!add_private_clause)
33528 add_private_clause = decl;
33533 cp_parser_abort_tentative_parse (parser);
33534 init = cp_parser_expression (parser);
33537 if (TREE_CODE (init) == MODIFY_EXPR
33538 || TREE_CODE (init) == MODOP_EXPR)
33539 real_decl = TREE_OPERAND (init, 0);
33543 return add_private_clause;
33546 /* Parse the restricted form of the for statement allowed by OpenMP. */
33549 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33550 tree *cclauses, bool *if_p)
33552 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33553 tree real_decl, initv, condv, incrv, declv;
33554 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33555 location_t loc_first;
33556 bool collapse_err = false;
33557 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33558 vec<tree, va_gc> *for_block = make_tree_vector ();
33559 auto_vec<tree, 4> orig_inits;
33561 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33562 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33563 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33564 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33565 && OMP_CLAUSE_ORDERED_EXPR (cl))
33568 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33571 if (ordered && ordered < collapse)
33573 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33574 "%<ordered%> clause parameter is less than %<collapse%>");
33575 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33576 = build_int_cst (NULL_TREE, collapse);
33577 ordered = collapse;
33581 for (tree *pc = &clauses; *pc; )
33582 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33584 error_at (OMP_CLAUSE_LOCATION (*pc),
33585 "%<linear%> clause may not be specified together "
33586 "with %<ordered%> clause with a parameter");
33587 *pc = OMP_CLAUSE_CHAIN (*pc);
33590 pc = &OMP_CLAUSE_CHAIN (*pc);
33593 gcc_assert (collapse >= 1 && ordered >= 0);
33594 count = ordered ? ordered : collapse;
33596 declv = make_tree_vec (count);
33597 initv = make_tree_vec (count);
33598 condv = make_tree_vec (count);
33599 incrv = make_tree_vec (count);
33601 loc_first = cp_lexer_peek_token (parser->lexer)->location;
33603 for (i = 0; i < count; i++)
33605 int bracecount = 0;
33606 tree add_private_clause = NULL_TREE;
33609 if (code != CILK_FOR
33610 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33612 cp_parser_error (parser, "for statement expected");
33615 if (code == CILK_FOR
33616 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33618 cp_parser_error (parser, "_Cilk_for statement expected");
33621 loc = cp_lexer_consume_token (parser->lexer)->location;
33623 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33626 init = orig_init = decl = real_decl = NULL;
33627 this_pre_body = push_stmt_list ();
33630 = cp_parser_omp_for_loop_init (parser, code,
33631 this_pre_body, for_block,
33632 init, orig_init, decl, real_decl);
33634 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33637 this_pre_body = pop_stmt_list (this_pre_body);
33641 pre_body = push_stmt_list ();
33643 add_stmt (this_pre_body);
33644 pre_body = pop_stmt_list (pre_body);
33647 pre_body = this_pre_body;
33652 if (cclauses != NULL
33653 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33654 && real_decl != NULL_TREE)
33657 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33658 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33659 && OMP_CLAUSE_DECL (*c) == real_decl)
33661 error_at (loc, "iteration variable %qD"
33662 " should not be firstprivate", real_decl);
33663 *c = OMP_CLAUSE_CHAIN (*c);
33665 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33666 && OMP_CLAUSE_DECL (*c) == real_decl)
33668 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33670 *c = OMP_CLAUSE_CHAIN (*c);
33671 if (code == OMP_SIMD)
33673 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33674 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33678 OMP_CLAUSE_CHAIN (l) = clauses;
33681 add_private_clause = NULL_TREE;
33685 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33686 && OMP_CLAUSE_DECL (*c) == real_decl)
33687 add_private_clause = NULL_TREE;
33688 c = &OMP_CLAUSE_CHAIN (*c);
33692 if (add_private_clause)
33695 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33697 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33698 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33699 && OMP_CLAUSE_DECL (c) == decl)
33701 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33702 && OMP_CLAUSE_DECL (c) == decl)
33703 error_at (loc, "iteration variable %qD "
33704 "should not be firstprivate",
33706 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33707 && OMP_CLAUSE_DECL (c) == decl)
33708 error_at (loc, "iteration variable %qD should not be reduction",
33713 if (code != OMP_SIMD)
33714 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33715 else if (collapse == 1)
33716 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33718 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33719 OMP_CLAUSE_DECL (c) = add_private_clause;
33720 c = finish_omp_clauses (c, true);
33723 OMP_CLAUSE_CHAIN (c) = clauses;
33725 /* For linear, signal that we need to fill up
33726 the so far unknown linear step. */
33727 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33728 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33734 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33735 cond = cp_parser_omp_for_cond (parser, decl, code);
33736 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33741 /* If decl is an iterator, preserve the operator on decl
33742 until finish_omp_for. */
33744 && ((processing_template_decl
33745 && (TREE_TYPE (real_decl) == NULL_TREE
33746 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33747 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33748 incr = cp_parser_omp_for_incr (parser, real_decl);
33750 incr = cp_parser_expression (parser);
33751 if (!EXPR_HAS_LOCATION (incr))
33752 protected_set_expr_location (incr, input_location);
33755 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33756 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33757 /*or_comma=*/false,
33758 /*consume_paren=*/true);
33760 TREE_VEC_ELT (declv, i) = decl;
33761 TREE_VEC_ELT (initv, i) = init;
33762 TREE_VEC_ELT (condv, i) = cond;
33763 TREE_VEC_ELT (incrv, i) = incr;
33766 orig_inits.safe_grow_cleared (i + 1);
33767 orig_inits[i] = orig_init;
33770 if (i == count - 1)
33773 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33774 in between the collapsed for loops to be still considered perfectly
33775 nested. Hopefully the final version clarifies this.
33776 For now handle (multiple) {'s and empty statements. */
33777 cp_parser_parse_tentatively (parser);
33780 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33782 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33784 cp_lexer_consume_token (parser->lexer);
33787 else if (bracecount
33788 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33789 cp_lexer_consume_token (parser->lexer);
33792 loc = cp_lexer_peek_token (parser->lexer)->location;
33793 error_at (loc, "not enough collapsed for loops");
33794 collapse_err = true;
33795 cp_parser_abort_tentative_parse (parser);
33804 cp_parser_parse_definitely (parser);
33805 nbraces += bracecount;
33812 /* Note that we saved the original contents of this flag when we entered
33813 the structured block, and so we don't need to re-save it here. */
33814 if (code == CILK_SIMD || code == CILK_FOR)
33815 parser->in_statement = IN_CILK_SIMD_FOR;
33817 parser->in_statement = IN_OMP_FOR;
33819 /* Note that the grammar doesn't call for a structured block here,
33820 though the loop as a whole is a structured block. */
33821 body = push_stmt_list ();
33822 cp_parser_statement (parser, NULL_TREE, false, if_p);
33823 body = pop_stmt_list (body);
33825 if (declv == NULL_TREE)
33828 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33829 body, pre_body, &orig_inits, clauses);
33833 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33835 cp_lexer_consume_token (parser->lexer);
33838 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33839 cp_lexer_consume_token (parser->lexer);
33844 error_at (cp_lexer_peek_token (parser->lexer)->location,
33845 "collapsed loops not perfectly nested");
33847 collapse_err = true;
33848 cp_parser_statement_seq_opt (parser, NULL);
33849 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33854 while (!for_block->is_empty ())
33855 add_stmt (pop_stmt_list (for_block->pop ()));
33856 release_tree_vector (for_block);
33861 /* Helper function for OpenMP parsing, split clauses and call
33862 finish_omp_clauses on each of the set of clauses afterwards. */
33865 cp_omp_split_clauses (location_t loc, enum tree_code code,
33866 omp_clause_mask mask, tree clauses, tree *cclauses)
33869 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33870 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33872 cclauses[i] = finish_omp_clauses (cclauses[i], true);
33876 #pragma omp simd simd-clause[optseq] new-line
33879 #define OMP_SIMD_CLAUSE_MASK \
33880 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33890 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33891 char *p_name, omp_clause_mask mask, tree *cclauses,
33894 tree clauses, sb, ret;
33896 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33898 strcat (p_name, " simd");
33899 mask |= OMP_SIMD_CLAUSE_MASK;
33901 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33905 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33906 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33907 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33908 OMP_CLAUSE_ORDERED);
33909 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33911 error_at (OMP_CLAUSE_LOCATION (c),
33912 "%<ordered%> clause with parameter may not be specified "
33913 "on %qs construct", p_name);
33914 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33918 sb = begin_omp_structured_block ();
33919 save = cp_parser_begin_omp_structured_block (parser);
33921 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
33923 cp_parser_end_omp_structured_block (parser, save);
33924 add_stmt (finish_omp_structured_block (sb));
33930 #pragma omp for for-clause[optseq] new-line
33934 #pragma omp for simd for-simd-clause[optseq] new-line
33937 #define OMP_FOR_CLAUSE_MASK \
33938 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33949 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33950 char *p_name, omp_clause_mask mask, tree *cclauses,
33953 tree clauses, sb, ret;
33955 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33957 strcat (p_name, " for");
33958 mask |= OMP_FOR_CLAUSE_MASK;
33959 /* parallel for{, simd} disallows nowait clause, but for
33960 target {teams distribute ,}parallel for{, simd} it should be accepted. */
33961 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
33962 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33963 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33964 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33965 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33967 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33969 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33970 const char *p = IDENTIFIER_POINTER (id);
33972 if (strcmp (p, "simd") == 0)
33974 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33975 if (cclauses == NULL)
33976 cclauses = cclauses_buf;
33978 cp_lexer_consume_token (parser->lexer);
33979 if (!flag_openmp) /* flag_openmp_simd */
33980 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33982 sb = begin_omp_structured_block ();
33983 save = cp_parser_begin_omp_structured_block (parser);
33984 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33986 cp_parser_end_omp_structured_block (parser, save);
33987 tree body = finish_omp_structured_block (sb);
33990 ret = make_node (OMP_FOR);
33991 TREE_TYPE (ret) = void_type_node;
33992 OMP_FOR_BODY (ret) = body;
33993 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33994 SET_EXPR_LOCATION (ret, loc);
33999 if (!flag_openmp) /* flag_openmp_simd */
34001 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34005 /* Composite distribute parallel for disallows linear clause. */
34006 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34007 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34009 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34013 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34014 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34017 sb = begin_omp_structured_block ();
34018 save = cp_parser_begin_omp_structured_block (parser);
34020 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34022 cp_parser_end_omp_structured_block (parser, save);
34023 add_stmt (finish_omp_structured_block (sb));
34029 # pragma omp master new-line
34030 structured-block */
34033 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34035 cp_parser_require_pragma_eol (parser, pragma_tok);
34036 return c_finish_omp_master (input_location,
34037 cp_parser_omp_structured_block (parser, if_p));
34041 # pragma omp ordered new-line
34045 # pragma omp ordered ordered-clauses new-line
34046 structured-block */
34048 #define OMP_ORDERED_CLAUSE_MASK \
34049 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34052 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34053 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34056 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34057 enum pragma_context context, bool *if_p)
34059 location_t loc = pragma_tok->location;
34061 if (context != pragma_stmt && context != pragma_compound)
34063 cp_parser_error (parser, "expected declaration specifiers");
34064 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34068 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34070 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34071 const char *p = IDENTIFIER_POINTER (id);
34073 if (strcmp (p, "depend") == 0)
34075 if (context == pragma_stmt)
34077 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34078 "%<depend%> clause may only be used in compound "
34080 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34084 = cp_parser_omp_all_clauses (parser,
34085 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34086 "#pragma omp ordered", pragma_tok);
34087 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34093 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34094 "#pragma omp ordered", pragma_tok);
34095 c_finish_omp_ordered (loc, clauses,
34096 cp_parser_omp_structured_block (parser, if_p));
34103 { section-sequence }
34106 section-directive[opt] structured-block
34107 section-sequence section-directive structured-block */
34110 cp_parser_omp_sections_scope (cp_parser *parser)
34112 tree stmt, substmt;
34113 bool error_suppress = false;
34116 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34119 stmt = push_stmt_list ();
34121 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34122 != PRAGMA_OMP_SECTION)
34124 substmt = cp_parser_omp_structured_block (parser, NULL);
34125 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34126 add_stmt (substmt);
34131 tok = cp_lexer_peek_token (parser->lexer);
34132 if (tok->type == CPP_CLOSE_BRACE)
34134 if (tok->type == CPP_EOF)
34137 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34139 cp_lexer_consume_token (parser->lexer);
34140 cp_parser_require_pragma_eol (parser, tok);
34141 error_suppress = false;
34143 else if (!error_suppress)
34145 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34146 error_suppress = true;
34149 substmt = cp_parser_omp_structured_block (parser, NULL);
34150 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34151 add_stmt (substmt);
34153 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34155 substmt = pop_stmt_list (stmt);
34157 stmt = make_node (OMP_SECTIONS);
34158 TREE_TYPE (stmt) = void_type_node;
34159 OMP_SECTIONS_BODY (stmt) = substmt;
34166 # pragma omp sections sections-clause[optseq] newline
34169 #define OMP_SECTIONS_CLAUSE_MASK \
34170 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34177 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34178 char *p_name, omp_clause_mask mask, tree *cclauses)
34181 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34183 strcat (p_name, " sections");
34184 mask |= OMP_SECTIONS_CLAUSE_MASK;
34186 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34188 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34192 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34193 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34196 ret = cp_parser_omp_sections_scope (parser);
34198 OMP_SECTIONS_CLAUSES (ret) = clauses;
34204 # pragma omp parallel parallel-clause[optseq] new-line
34206 # pragma omp parallel for parallel-for-clause[optseq] new-line
34208 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34212 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34213 structured-block */
34215 #define OMP_PARALLEL_CLAUSE_MASK \
34216 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34227 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34228 char *p_name, omp_clause_mask mask, tree *cclauses,
34231 tree stmt, clauses, block;
34233 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34235 strcat (p_name, " parallel");
34236 mask |= OMP_PARALLEL_CLAUSE_MASK;
34237 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34238 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34239 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34240 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34244 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34245 if (cclauses == NULL)
34246 cclauses = cclauses_buf;
34248 cp_lexer_consume_token (parser->lexer);
34249 if (!flag_openmp) /* flag_openmp_simd */
34250 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34252 block = begin_omp_parallel ();
34253 save = cp_parser_begin_omp_structured_block (parser);
34254 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34256 cp_parser_end_omp_structured_block (parser, save);
34257 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34259 if (ret == NULL_TREE)
34261 OMP_PARALLEL_COMBINED (stmt) = 1;
34264 /* When combined with distribute, parallel has to be followed by for.
34265 #pragma omp target parallel is allowed though. */
34267 && (mask & (OMP_CLAUSE_MASK_1
34268 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34270 error_at (loc, "expected %<for%> after %qs", p_name);
34271 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34274 else if (!flag_openmp) /* flag_openmp_simd */
34276 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34279 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34281 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34282 const char *p = IDENTIFIER_POINTER (id);
34283 if (strcmp (p, "sections") == 0)
34285 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34286 cclauses = cclauses_buf;
34288 cp_lexer_consume_token (parser->lexer);
34289 block = begin_omp_parallel ();
34290 save = cp_parser_begin_omp_structured_block (parser);
34291 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34292 cp_parser_end_omp_structured_block (parser, save);
34293 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34295 OMP_PARALLEL_COMBINED (stmt) = 1;
34300 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34304 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34305 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34308 block = begin_omp_parallel ();
34309 save = cp_parser_begin_omp_structured_block (parser);
34310 cp_parser_statement (parser, NULL_TREE, false, if_p);
34311 cp_parser_end_omp_structured_block (parser, save);
34312 stmt = finish_omp_parallel (clauses, block);
34317 # pragma omp single single-clause[optseq] new-line
34318 structured-block */
34320 #define OMP_SINGLE_CLAUSE_MASK \
34321 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34327 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34329 tree stmt = make_node (OMP_SINGLE);
34330 TREE_TYPE (stmt) = void_type_node;
34332 OMP_SINGLE_CLAUSES (stmt)
34333 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34334 "#pragma omp single", pragma_tok);
34335 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34337 return add_stmt (stmt);
34341 # pragma omp task task-clause[optseq] new-line
34342 structured-block */
34344 #define OMP_TASK_CLAUSE_MASK \
34345 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34357 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34359 tree clauses, block;
34362 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34363 "#pragma omp task", pragma_tok);
34364 block = begin_omp_task ();
34365 save = cp_parser_begin_omp_structured_block (parser);
34366 cp_parser_statement (parser, NULL_TREE, false, if_p);
34367 cp_parser_end_omp_structured_block (parser, save);
34368 return finish_omp_task (clauses, block);
34372 # pragma omp taskwait new-line */
34375 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34377 cp_parser_require_pragma_eol (parser, pragma_tok);
34378 finish_omp_taskwait ();
34382 # pragma omp taskyield new-line */
34385 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34387 cp_parser_require_pragma_eol (parser, pragma_tok);
34388 finish_omp_taskyield ();
34392 # pragma omp taskgroup new-line
34393 structured-block */
34396 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34398 cp_parser_require_pragma_eol (parser, pragma_tok);
34399 return c_finish_omp_taskgroup (input_location,
34400 cp_parser_omp_structured_block (parser,
34406 # pragma omp threadprivate (variable-list) */
34409 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34413 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34414 cp_parser_require_pragma_eol (parser, pragma_tok);
34416 finish_omp_threadprivate (vars);
34420 # pragma omp cancel cancel-clause[optseq] new-line */
34422 #define OMP_CANCEL_CLAUSE_MASK \
34423 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34430 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34432 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34433 "#pragma omp cancel", pragma_tok);
34434 finish_omp_cancel (clauses);
34438 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34440 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34441 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34447 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
34450 bool point_seen = false;
34452 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34454 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34455 const char *p = IDENTIFIER_POINTER (id);
34457 if (strcmp (p, "point") == 0)
34459 cp_lexer_consume_token (parser->lexer);
34465 cp_parser_error (parser, "expected %<point%>");
34466 cp_parser_require_pragma_eol (parser, pragma_tok);
34470 clauses = cp_parser_omp_all_clauses (parser,
34471 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34472 "#pragma omp cancellation point",
34474 finish_omp_cancellation_point (clauses);
34478 #pragma omp distribute distribute-clause[optseq] new-line
34481 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34482 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34489 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34490 char *p_name, omp_clause_mask mask, tree *cclauses,
34493 tree clauses, sb, ret;
34495 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34497 strcat (p_name, " distribute");
34498 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34500 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34502 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34503 const char *p = IDENTIFIER_POINTER (id);
34505 bool parallel = false;
34507 if (strcmp (p, "simd") == 0)
34510 parallel = strcmp (p, "parallel") == 0;
34511 if (parallel || simd)
34513 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34514 if (cclauses == NULL)
34515 cclauses = cclauses_buf;
34516 cp_lexer_consume_token (parser->lexer);
34517 if (!flag_openmp) /* flag_openmp_simd */
34520 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34523 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34526 sb = begin_omp_structured_block ();
34527 save = cp_parser_begin_omp_structured_block (parser);
34529 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34532 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34534 cp_parser_end_omp_structured_block (parser, save);
34535 tree body = finish_omp_structured_block (sb);
34538 ret = make_node (OMP_DISTRIBUTE);
34539 TREE_TYPE (ret) = void_type_node;
34540 OMP_FOR_BODY (ret) = body;
34541 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34542 SET_EXPR_LOCATION (ret, loc);
34547 if (!flag_openmp) /* flag_openmp_simd */
34549 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34553 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34557 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34558 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34561 sb = begin_omp_structured_block ();
34562 save = cp_parser_begin_omp_structured_block (parser);
34564 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
34566 cp_parser_end_omp_structured_block (parser, save);
34567 add_stmt (finish_omp_structured_block (sb));
34573 # pragma omp teams teams-clause[optseq] new-line
34574 structured-block */
34576 #define OMP_TEAMS_CLAUSE_MASK \
34577 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34586 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34587 char *p_name, omp_clause_mask mask, tree *cclauses,
34590 tree clauses, sb, ret;
34592 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34594 strcat (p_name, " teams");
34595 mask |= OMP_TEAMS_CLAUSE_MASK;
34597 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34599 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34600 const char *p = IDENTIFIER_POINTER (id);
34601 if (strcmp (p, "distribute") == 0)
34603 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34604 if (cclauses == NULL)
34605 cclauses = cclauses_buf;
34607 cp_lexer_consume_token (parser->lexer);
34608 if (!flag_openmp) /* flag_openmp_simd */
34609 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34611 sb = begin_omp_structured_block ();
34612 save = cp_parser_begin_omp_structured_block (parser);
34613 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34615 cp_parser_end_omp_structured_block (parser, save);
34616 tree body = finish_omp_structured_block (sb);
34619 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34620 ret = make_node (OMP_TEAMS);
34621 TREE_TYPE (ret) = void_type_node;
34622 OMP_TEAMS_CLAUSES (ret) = clauses;
34623 OMP_TEAMS_BODY (ret) = body;
34624 OMP_TEAMS_COMBINED (ret) = 1;
34625 return add_stmt (ret);
34628 if (!flag_openmp) /* flag_openmp_simd */
34630 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34634 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34638 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34639 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34642 tree stmt = make_node (OMP_TEAMS);
34643 TREE_TYPE (stmt) = void_type_node;
34644 OMP_TEAMS_CLAUSES (stmt) = clauses;
34645 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34647 return add_stmt (stmt);
34651 # pragma omp target data target-data-clause[optseq] new-line
34652 structured-block */
34654 #define OMP_TARGET_DATA_CLAUSE_MASK \
34655 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34661 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34664 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34665 "#pragma omp target data", pragma_tok);
34667 for (tree *pc = &clauses; *pc;)
34669 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34670 switch (OMP_CLAUSE_MAP_KIND (*pc))
34673 case GOMP_MAP_ALWAYS_TO:
34674 case GOMP_MAP_FROM:
34675 case GOMP_MAP_ALWAYS_FROM:
34676 case GOMP_MAP_TOFROM:
34677 case GOMP_MAP_ALWAYS_TOFROM:
34678 case GOMP_MAP_ALLOC:
34681 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34682 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34683 case GOMP_MAP_ALWAYS_POINTER:
34687 error_at (OMP_CLAUSE_LOCATION (*pc),
34688 "%<#pragma omp target data%> with map-type other "
34689 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34690 "on %<map%> clause");
34691 *pc = OMP_CLAUSE_CHAIN (*pc);
34694 pc = &OMP_CLAUSE_CHAIN (*pc);
34700 error_at (pragma_tok->location,
34701 "%<#pragma omp target data%> must contain at least "
34702 "one %<map%> clause");
34706 tree stmt = make_node (OMP_TARGET_DATA);
34707 TREE_TYPE (stmt) = void_type_node;
34708 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34710 keep_next_level (true);
34711 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34713 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34714 return add_stmt (stmt);
34718 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34719 structured-block */
34721 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34722 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34729 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34730 enum pragma_context context)
34732 bool data_seen = false;
34733 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34735 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34736 const char *p = IDENTIFIER_POINTER (id);
34738 if (strcmp (p, "data") == 0)
34740 cp_lexer_consume_token (parser->lexer);
34746 cp_parser_error (parser, "expected %<data%>");
34747 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34751 if (context == pragma_stmt)
34753 error_at (pragma_tok->location,
34754 "%<#pragma omp target enter data%> may only be "
34755 "used in compound statements");
34756 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34761 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34762 "#pragma omp target enter data", pragma_tok);
34764 for (tree *pc = &clauses; *pc;)
34766 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34767 switch (OMP_CLAUSE_MAP_KIND (*pc))
34770 case GOMP_MAP_ALWAYS_TO:
34771 case GOMP_MAP_ALLOC:
34774 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34775 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34776 case GOMP_MAP_ALWAYS_POINTER:
34780 error_at (OMP_CLAUSE_LOCATION (*pc),
34781 "%<#pragma omp target enter data%> with map-type other "
34782 "than %<to%> or %<alloc%> on %<map%> clause");
34783 *pc = OMP_CLAUSE_CHAIN (*pc);
34786 pc = &OMP_CLAUSE_CHAIN (*pc);
34792 error_at (pragma_tok->location,
34793 "%<#pragma omp target enter data%> must contain at least "
34794 "one %<map%> clause");
34798 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34799 TREE_TYPE (stmt) = void_type_node;
34800 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34801 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34802 return add_stmt (stmt);
34806 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34807 structured-block */
34809 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34810 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34817 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34818 enum pragma_context context)
34820 bool data_seen = false;
34821 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34823 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34824 const char *p = IDENTIFIER_POINTER (id);
34826 if (strcmp (p, "data") == 0)
34828 cp_lexer_consume_token (parser->lexer);
34834 cp_parser_error (parser, "expected %<data%>");
34835 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34839 if (context == pragma_stmt)
34841 error_at (pragma_tok->location,
34842 "%<#pragma omp target exit data%> may only be "
34843 "used in compound statements");
34844 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34849 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34850 "#pragma omp target exit data", pragma_tok);
34852 for (tree *pc = &clauses; *pc;)
34854 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34855 switch (OMP_CLAUSE_MAP_KIND (*pc))
34857 case GOMP_MAP_FROM:
34858 case GOMP_MAP_ALWAYS_FROM:
34859 case GOMP_MAP_RELEASE:
34860 case GOMP_MAP_DELETE:
34863 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34864 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34865 case GOMP_MAP_ALWAYS_POINTER:
34869 error_at (OMP_CLAUSE_LOCATION (*pc),
34870 "%<#pragma omp target exit data%> with map-type other "
34871 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34873 *pc = OMP_CLAUSE_CHAIN (*pc);
34876 pc = &OMP_CLAUSE_CHAIN (*pc);
34882 error_at (pragma_tok->location,
34883 "%<#pragma omp target exit data%> must contain at least "
34884 "one %<map%> clause");
34888 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34889 TREE_TYPE (stmt) = void_type_node;
34890 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34891 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34892 return add_stmt (stmt);
34896 # pragma omp target update target-update-clause[optseq] new-line */
34898 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34899 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34907 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34908 enum pragma_context context)
34910 if (context == pragma_stmt)
34912 error_at (pragma_tok->location,
34913 "%<#pragma omp target update%> may only be "
34914 "used in compound statements");
34915 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34920 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34921 "#pragma omp target update", pragma_tok);
34922 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34923 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34925 error_at (pragma_tok->location,
34926 "%<#pragma omp target update%> must contain at least one "
34927 "%<from%> or %<to%> clauses");
34931 tree stmt = make_node (OMP_TARGET_UPDATE);
34932 TREE_TYPE (stmt) = void_type_node;
34933 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34934 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34940 # pragma omp target target-clause[optseq] new-line
34941 structured-block */
34943 #define OMP_TARGET_CLAUSE_MASK \
34944 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34955 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34956 enum pragma_context context, bool *if_p)
34958 tree *pc = NULL, stmt;
34960 if (context != pragma_stmt && context != pragma_compound)
34962 cp_parser_error (parser, "expected declaration specifiers");
34963 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34967 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34969 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34970 const char *p = IDENTIFIER_POINTER (id);
34971 enum tree_code ccode = ERROR_MARK;
34973 if (strcmp (p, "teams") == 0)
34975 else if (strcmp (p, "parallel") == 0)
34976 ccode = OMP_PARALLEL;
34977 else if (strcmp (p, "simd") == 0)
34979 if (ccode != ERROR_MARK)
34981 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34982 char p_name[sizeof ("#pragma omp target teams distribute "
34983 "parallel for simd")];
34985 cp_lexer_consume_token (parser->lexer);
34986 strcpy (p_name, "#pragma omp target");
34987 if (!flag_openmp) /* flag_openmp_simd */
34993 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34994 OMP_TARGET_CLAUSE_MASK,
34998 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34999 OMP_TARGET_CLAUSE_MASK,
35003 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35004 OMP_TARGET_CLAUSE_MASK,
35008 gcc_unreachable ();
35010 return stmt != NULL_TREE;
35012 keep_next_level (true);
35013 tree sb = begin_omp_structured_block (), ret;
35014 unsigned save = cp_parser_begin_omp_structured_block (parser);
35018 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35019 OMP_TARGET_CLAUSE_MASK, cclauses,
35023 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35024 OMP_TARGET_CLAUSE_MASK, cclauses,
35028 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35029 OMP_TARGET_CLAUSE_MASK, cclauses,
35033 gcc_unreachable ();
35035 cp_parser_end_omp_structured_block (parser, save);
35036 tree body = finish_omp_structured_block (sb);
35037 if (ret == NULL_TREE)
35039 if (ccode == OMP_TEAMS && !processing_template_decl)
35041 /* For combined target teams, ensure the num_teams and
35042 thread_limit clause expressions are evaluated on the host,
35043 before entering the target construct. */
35045 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35046 c; c = OMP_CLAUSE_CHAIN (c))
35047 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35048 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35049 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35051 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35052 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35053 if (expr == error_mark_node)
35055 tree tmp = TARGET_EXPR_SLOT (expr);
35057 OMP_CLAUSE_OPERAND (c, 0) = expr;
35058 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35059 OMP_CLAUSE_FIRSTPRIVATE);
35060 OMP_CLAUSE_DECL (tc) = tmp;
35061 OMP_CLAUSE_CHAIN (tc)
35062 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35063 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35066 tree stmt = make_node (OMP_TARGET);
35067 TREE_TYPE (stmt) = void_type_node;
35068 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35069 OMP_TARGET_BODY (stmt) = body;
35070 OMP_TARGET_COMBINED (stmt) = 1;
35072 pc = &OMP_TARGET_CLAUSES (stmt);
35073 goto check_clauses;
35075 else if (!flag_openmp) /* flag_openmp_simd */
35077 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35080 else if (strcmp (p, "data") == 0)
35082 cp_lexer_consume_token (parser->lexer);
35083 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35086 else if (strcmp (p, "enter") == 0)
35088 cp_lexer_consume_token (parser->lexer);
35089 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35092 else if (strcmp (p, "exit") == 0)
35094 cp_lexer_consume_token (parser->lexer);
35095 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35098 else if (strcmp (p, "update") == 0)
35100 cp_lexer_consume_token (parser->lexer);
35101 return cp_parser_omp_target_update (parser, pragma_tok, context);
35105 stmt = make_node (OMP_TARGET);
35106 TREE_TYPE (stmt) = void_type_node;
35108 OMP_TARGET_CLAUSES (stmt)
35109 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35110 "#pragma omp target", pragma_tok);
35111 pc = &OMP_TARGET_CLAUSES (stmt);
35112 keep_next_level (true);
35113 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35115 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35121 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35122 switch (OMP_CLAUSE_MAP_KIND (*pc))
35125 case GOMP_MAP_ALWAYS_TO:
35126 case GOMP_MAP_FROM:
35127 case GOMP_MAP_ALWAYS_FROM:
35128 case GOMP_MAP_TOFROM:
35129 case GOMP_MAP_ALWAYS_TOFROM:
35130 case GOMP_MAP_ALLOC:
35131 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35132 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35133 case GOMP_MAP_ALWAYS_POINTER:
35136 error_at (OMP_CLAUSE_LOCATION (*pc),
35137 "%<#pragma omp target%> with map-type other "
35138 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35139 "on %<map%> clause");
35140 *pc = OMP_CLAUSE_CHAIN (*pc);
35143 pc = &OMP_CLAUSE_CHAIN (*pc);
35149 # pragma acc cache (variable-list) new-line
35153 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35155 tree stmt, clauses;
35157 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35158 clauses = finish_omp_clauses (clauses, false);
35160 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35162 stmt = make_node (OACC_CACHE);
35163 TREE_TYPE (stmt) = void_type_node;
35164 OACC_CACHE_CLAUSES (stmt) = clauses;
35165 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35172 # pragma acc data oacc-data-clause[optseq] new-line
35173 structured-block */
35175 #define OACC_DATA_CLAUSE_MASK \
35176 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35189 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35191 tree stmt, clauses, block;
35194 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35195 "#pragma acc data", pragma_tok);
35197 block = begin_omp_parallel ();
35198 save = cp_parser_begin_omp_structured_block (parser);
35199 cp_parser_statement (parser, NULL_TREE, false, if_p);
35200 cp_parser_end_omp_structured_block (parser, save);
35201 stmt = finish_oacc_data (clauses, block);
35206 # pragma acc host_data <clauses> new-line
35207 structured-block */
35209 #define OACC_HOST_DATA_CLAUSE_MASK \
35210 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35213 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35215 tree stmt, clauses, block;
35218 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35219 "#pragma acc host_data", pragma_tok);
35221 block = begin_omp_parallel ();
35222 save = cp_parser_begin_omp_structured_block (parser);
35223 cp_parser_statement (parser, NULL_TREE, false, if_p);
35224 cp_parser_end_omp_structured_block (parser, save);
35225 stmt = finish_oacc_host_data (clauses, block);
35230 # pragma acc declare oacc-data-clause[optseq] new-line
35233 #define OACC_DECLARE_CLAUSE_MASK \
35234 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35248 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35250 tree clauses, stmt;
35251 bool error = false;
35253 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35254 "#pragma acc declare", pragma_tok, true);
35257 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35259 error_at (pragma_tok->location,
35260 "no valid clauses specified in %<#pragma acc declare%>");
35264 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35266 location_t loc = OMP_CLAUSE_LOCATION (t);
35267 tree decl = OMP_CLAUSE_DECL (t);
35268 if (!DECL_P (decl))
35270 error_at (loc, "array section in %<#pragma acc declare%>");
35274 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35275 switch (OMP_CLAUSE_MAP_KIND (t))
35277 case GOMP_MAP_FORCE_ALLOC:
35278 case GOMP_MAP_FORCE_TO:
35279 case GOMP_MAP_FORCE_DEVICEPTR:
35280 case GOMP_MAP_DEVICE_RESIDENT:
35283 case GOMP_MAP_POINTER:
35284 /* Generated by c_finish_omp_clauses from array sections;
35285 avoid spurious diagnostics. */
35288 case GOMP_MAP_LINK:
35289 if (!global_bindings_p ()
35290 && (TREE_STATIC (decl)
35291 || !DECL_EXTERNAL (decl)))
35294 "%qD must be a global variable in"
35295 "%<#pragma acc declare link%>",
35303 if (global_bindings_p ())
35305 error_at (loc, "invalid OpenACC clause at file scope");
35309 if (DECL_EXTERNAL (decl))
35312 "invalid use of %<extern%> variable %qD "
35313 "in %<#pragma acc declare%>", decl);
35317 else if (TREE_PUBLIC (decl))
35320 "invalid use of %<global%> variable %qD "
35321 "in %<#pragma acc declare%>", decl);
35328 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35329 || lookup_attribute ("omp declare target link",
35330 DECL_ATTRIBUTES (decl)))
35332 error_at (loc, "variable %qD used more than once with "
35333 "%<#pragma acc declare%>", decl);
35342 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35343 id = get_identifier ("omp declare target link");
35345 id = get_identifier ("omp declare target");
35347 DECL_ATTRIBUTES (decl)
35348 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35349 if (global_bindings_p ())
35351 symtab_node *node = symtab_node::get (decl);
35354 node->offloadable = 1;
35355 if (ENABLE_OFFLOADING)
35357 g->have_offload = true;
35358 if (is_a <varpool_node *> (node))
35359 vec_safe_push (offload_vars, decl);
35366 if (error || global_bindings_p ())
35369 stmt = make_node (OACC_DECLARE);
35370 TREE_TYPE (stmt) = void_type_node;
35371 OACC_DECLARE_CLAUSES (stmt) = clauses;
35372 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35380 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35384 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35386 LOC is the location of the #pragma token.
35389 #define OACC_ENTER_DATA_CLAUSE_MASK \
35390 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35398 #define OACC_EXIT_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_COPYOUT) \
35402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35406 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35409 tree stmt, clauses;
35411 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35412 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35414 cp_parser_error (parser, enter
35415 ? "expected %<data%> in %<#pragma acc enter data%>"
35416 : "expected %<data%> in %<#pragma acc exit data%>");
35417 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35422 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35423 if (strcmp (p, "data") != 0)
35425 cp_parser_error (parser, "invalid pragma");
35426 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35430 cp_lexer_consume_token (parser->lexer);
35433 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35434 "#pragma acc enter data", pragma_tok);
35436 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35437 "#pragma acc exit data", pragma_tok);
35439 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35441 error_at (pragma_tok->location,
35442 "%<#pragma acc enter data%> has no data movement clause");
35446 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35447 TREE_TYPE (stmt) = void_type_node;
35448 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35449 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35455 # pragma acc loop oacc-loop-clause[optseq] new-line
35456 structured-block */
35458 #define OACC_LOOP_CLAUSE_MASK \
35459 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35471 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35472 omp_clause_mask mask, tree *cclauses, bool *if_p)
35474 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35476 strcat (p_name, " loop");
35477 mask |= OACC_LOOP_CLAUSE_MASK;
35479 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35483 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35485 *cclauses = finish_omp_clauses (*cclauses, false);
35487 clauses = finish_omp_clauses (clauses, false);
35490 tree block = begin_omp_structured_block ();
35491 int save = cp_parser_begin_omp_structured_block (parser);
35492 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35493 cp_parser_end_omp_structured_block (parser, save);
35494 add_stmt (finish_omp_structured_block (block));
35500 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35505 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35509 #define OACC_KERNELS_CLAUSE_MASK \
35510 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35525 #define OACC_PARALLEL_CLAUSE_MASK \
35526 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35548 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35549 char *p_name, bool *if_p)
35551 omp_clause_mask mask;
35552 enum tree_code code;
35553 switch (cp_parser_pragma_kind (pragma_tok))
35555 case PRAGMA_OACC_KERNELS:
35556 strcat (p_name, " kernels");
35557 mask = OACC_KERNELS_CLAUSE_MASK;
35558 code = OACC_KERNELS;
35560 case PRAGMA_OACC_PARALLEL:
35561 strcat (p_name, " parallel");
35562 mask = OACC_PARALLEL_CLAUSE_MASK;
35563 code = OACC_PARALLEL;
35566 gcc_unreachable ();
35569 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35572 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35573 if (strcmp (p, "loop") == 0)
35575 cp_lexer_consume_token (parser->lexer);
35576 tree block = begin_omp_parallel ();
35578 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
35580 return finish_omp_construct (code, block, clauses);
35584 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35586 tree block = begin_omp_parallel ();
35587 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35588 cp_parser_statement (parser, NULL_TREE, false, if_p);
35589 cp_parser_end_omp_structured_block (parser, save);
35590 return finish_omp_construct (code, block, clauses);
35594 # pragma acc update oacc-update-clause[optseq] new-line
35597 #define OACC_UPDATE_CLAUSE_MASK \
35598 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35606 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35608 tree stmt, clauses;
35610 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35611 "#pragma acc update", pragma_tok);
35613 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35615 error_at (pragma_tok->location,
35616 "%<#pragma acc update%> must contain at least one "
35617 "%<device%> or %<host%> or %<self%> clause");
35621 stmt = make_node (OACC_UPDATE);
35622 TREE_TYPE (stmt) = void_type_node;
35623 OACC_UPDATE_CLAUSES (stmt) = clauses;
35624 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35630 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35632 LOC is the location of the #pragma token.
35635 #define OACC_WAIT_CLAUSE_MASK \
35636 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35639 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35641 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35642 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35644 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35645 list = cp_parser_oacc_wait_list (parser, loc, list);
35647 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35648 "#pragma acc wait", pragma_tok);
35650 stmt = c_finish_oacc_wait (loc, list, clauses);
35651 stmt = finish_expr_stmt (stmt);
35657 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35659 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35660 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35668 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35669 enum pragma_context context)
35671 bool first_p = parser->omp_declare_simd == NULL;
35672 cp_omp_declare_simd_data data;
35675 data.error_seen = false;
35676 data.fndecl_seen = false;
35677 data.tokens = vNULL;
35678 parser->omp_declare_simd = &data;
35680 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35681 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35682 cp_lexer_consume_token (parser->lexer);
35683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35684 parser->omp_declare_simd->error_seen = true;
35685 cp_parser_require_pragma_eol (parser, pragma_tok);
35686 struct cp_token_cache *cp
35687 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35688 parser->omp_declare_simd->tokens.safe_push (cp);
35691 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35692 cp_parser_pragma (parser, context, NULL);
35695 case pragma_external:
35696 cp_parser_declaration (parser);
35698 case pragma_member:
35699 cp_parser_member_declaration (parser);
35701 case pragma_objc_icode:
35702 cp_parser_block_declaration (parser, /*statement_p=*/false);
35705 cp_parser_declaration_statement (parser);
35708 if (parser->omp_declare_simd
35709 && !parser->omp_declare_simd->error_seen
35710 && !parser->omp_declare_simd->fndecl_seen)
35711 error_at (pragma_tok->location,
35712 "%<#pragma omp declare simd%> not immediately followed by "
35713 "function declaration or definition");
35714 data.tokens.release ();
35715 parser->omp_declare_simd = NULL;
35719 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35720 This function is modelled similar to the late parsing of omp declare
35724 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35726 struct cp_token_cache *ce;
35727 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35730 if (parser->omp_declare_simd != NULL
35731 || lookup_attribute ("simd", attrs))
35733 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35734 "used in the same function marked as a Cilk Plus SIMD-enabled "
35736 parser->cilk_simd_fn_info->tokens.release ();
35737 XDELETE (parser->cilk_simd_fn_info);
35738 parser->cilk_simd_fn_info = NULL;
35741 if (!info->error_seen && info->fndecl_seen)
35743 error ("vector attribute not immediately followed by a single function"
35744 " declaration or definition");
35745 info->error_seen = true;
35747 if (info->error_seen)
35750 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35754 cp_parser_push_lexer_for_tokens (parser, ce);
35755 parser->lexer->in_pragma = true;
35756 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35757 "SIMD-enabled functions attribute",
35759 cp_parser_pop_lexer (parser);
35761 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35763 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35764 TREE_CHAIN (c) = attrs;
35767 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35768 TREE_CHAIN (c) = attrs;
35769 if (processing_template_decl)
35770 ATTR_IS_DEPENDENT (c) = 1;
35773 info->fndecl_seen = true;
35774 parser->cilk_simd_fn_info->tokens.release ();
35775 XDELETE (parser->cilk_simd_fn_info);
35776 parser->cilk_simd_fn_info = NULL;
35780 /* Finalize #pragma omp declare simd clauses after direct declarator has
35781 been parsed, and put that into "omp declare simd" attribute. */
35784 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35786 struct cp_token_cache *ce;
35787 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35790 if (!data->error_seen && data->fndecl_seen)
35792 error ("%<#pragma omp declare simd%> not immediately followed by "
35793 "a single function declaration or definition");
35794 data->error_seen = true;
35797 if (data->error_seen)
35800 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35804 cp_parser_push_lexer_for_tokens (parser, ce);
35805 parser->lexer->in_pragma = true;
35806 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35807 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35808 cp_lexer_consume_token (parser->lexer);
35809 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35810 "#pragma omp declare simd", pragma_tok);
35811 cp_parser_pop_lexer (parser);
35813 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35814 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35815 TREE_CHAIN (c) = attrs;
35816 if (processing_template_decl)
35817 ATTR_IS_DEPENDENT (c) = 1;
35821 data->fndecl_seen = true;
35827 # pragma omp declare target new-line
35828 declarations and definitions
35829 # pragma omp end declare target new-line
35832 # pragma omp declare target ( extended-list ) new-line
35834 # pragma omp declare target declare-target-clauses[seq] new-line */
35836 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35837 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35841 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35843 tree clauses = NULL_TREE;
35844 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35846 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35847 "#pragma omp declare target", pragma_tok);
35848 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35850 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35852 clauses = finish_omp_clauses (clauses, true);
35853 cp_parser_require_pragma_eol (parser, pragma_tok);
35857 cp_parser_require_pragma_eol (parser, pragma_tok);
35858 scope_chain->omp_declare_target_attribute++;
35861 if (scope_chain->omp_declare_target_attribute)
35862 error_at (pragma_tok->location,
35863 "%<#pragma omp declare target%> with clauses in between "
35864 "%<#pragma omp declare target%> without clauses and "
35865 "%<#pragma omp end declare target%>");
35866 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35868 tree t = OMP_CLAUSE_DECL (c), id;
35869 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35870 tree at2 = lookup_attribute ("omp declare target link",
35871 DECL_ATTRIBUTES (t));
35872 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35874 id = get_identifier ("omp declare target link");
35875 std::swap (at1, at2);
35878 id = get_identifier ("omp declare target");
35881 error_at (OMP_CLAUSE_LOCATION (c),
35882 "%qD specified both in declare target %<link%> and %<to%>"
35888 symtab_node *node = symtab_node::get (t);
35889 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35892 node->offloadable = 1;
35893 if (ENABLE_OFFLOADING)
35895 g->have_offload = true;
35896 if (is_a <varpool_node *> (node))
35897 vec_safe_push (offload_vars, t);
35905 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35907 const char *p = "";
35908 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35910 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35911 p = IDENTIFIER_POINTER (id);
35913 if (strcmp (p, "declare") == 0)
35915 cp_lexer_consume_token (parser->lexer);
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, "target") == 0)
35923 cp_lexer_consume_token (parser->lexer);
35926 cp_parser_error (parser, "expected %<target%>");
35927 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35933 cp_parser_error (parser, "expected %<declare%>");
35934 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35937 cp_parser_require_pragma_eol (parser, pragma_tok);
35938 if (!scope_chain->omp_declare_target_attribute)
35939 error_at (pragma_tok->location,
35940 "%<#pragma omp end declare target%> without corresponding "
35941 "%<#pragma omp declare target%>");
35943 scope_chain->omp_declare_target_attribute--;
35946 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35947 expression and optional initializer clause of
35948 #pragma omp declare reduction. We store the expression(s) as
35949 either 3, 6 or 7 special statements inside of the artificial function's
35950 body. The first two statements are DECL_EXPRs for the artificial
35951 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35952 expression that uses those variables.
35953 If there was any INITIALIZER clause, this is followed by further statements,
35954 the fourth and fifth statements are DECL_EXPRs for the artificial
35955 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35956 constructor variant (first token after open paren is not omp_priv),
35957 then the sixth statement is a statement with the function call expression
35958 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35959 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35960 to initialize the OMP_PRIV artificial variable and there is seventh
35961 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35964 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35966 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35967 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35968 type = TREE_TYPE (type);
35969 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35970 DECL_ARTIFICIAL (omp_out) = 1;
35971 pushdecl (omp_out);
35972 add_decl_expr (omp_out);
35973 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35974 DECL_ARTIFICIAL (omp_in) = 1;
35976 add_decl_expr (omp_in);
35978 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35980 keep_next_level (true);
35981 tree block = begin_omp_structured_block ();
35982 combiner = cp_parser_expression (parser);
35983 finish_expr_stmt (combiner);
35984 block = finish_omp_structured_block (block);
35987 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35990 const char *p = "";
35991 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35993 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35994 p = IDENTIFIER_POINTER (id);
35997 if (strcmp (p, "initializer") == 0)
35999 cp_lexer_consume_token (parser->lexer);
36000 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36004 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36006 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36007 p = IDENTIFIER_POINTER (id);
36010 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36011 DECL_ARTIFICIAL (omp_priv) = 1;
36012 pushdecl (omp_priv);
36013 add_decl_expr (omp_priv);
36014 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36015 DECL_ARTIFICIAL (omp_orig) = 1;
36016 pushdecl (omp_orig);
36017 add_decl_expr (omp_orig);
36019 keep_next_level (true);
36020 block = begin_omp_structured_block ();
36023 if (strcmp (p, "omp_priv") == 0)
36025 bool is_direct_init, is_non_constant_init;
36027 cp_lexer_consume_token (parser->lexer);
36028 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36029 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36030 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36031 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36033 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36034 == CPP_CLOSE_PAREN))
36036 finish_omp_structured_block (block);
36037 error ("invalid initializer clause");
36040 initializer = cp_parser_initializer (parser, &is_direct_init,
36041 &is_non_constant_init);
36042 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36043 NULL_TREE, LOOKUP_ONLYCONVERTING);
36047 cp_parser_parse_tentatively (parser);
36048 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36049 /*check_dependency_p=*/true,
36050 /*template_p=*/NULL,
36051 /*declarator_p=*/false,
36052 /*optional_p=*/false);
36053 vec<tree, va_gc> *args;
36054 if (fn_name == error_mark_node
36055 || cp_parser_error_occurred (parser)
36056 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36057 || ((args = cp_parser_parenthesized_expression_list
36058 (parser, non_attr, /*cast_p=*/false,
36059 /*allow_expansion_p=*/true,
36060 /*non_constant_p=*/NULL)),
36061 cp_parser_error_occurred (parser)))
36063 finish_omp_structured_block (block);
36064 cp_parser_abort_tentative_parse (parser);
36065 cp_parser_error (parser, "expected id-expression (arguments)");
36070 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36071 if (arg == omp_priv
36072 || (TREE_CODE (arg) == ADDR_EXPR
36073 && TREE_OPERAND (arg, 0) == omp_priv))
36075 cp_parser_abort_tentative_parse (parser);
36076 if (arg == NULL_TREE)
36077 error ("one of the initializer call arguments should be %<omp_priv%>"
36078 " or %<&omp_priv%>");
36079 initializer = cp_parser_postfix_expression (parser, false, false, false,
36081 finish_expr_stmt (initializer);
36084 block = finish_omp_structured_block (block);
36085 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36089 add_decl_expr (omp_orig);
36091 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36095 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36096 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36102 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36103 initializer-clause[opt] new-line
36105 initializer-clause:
36106 initializer (omp_priv initializer)
36107 initializer (function-name (argument-list)) */
36110 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36111 enum pragma_context)
36113 auto_vec<tree> types;
36114 enum tree_code reduc_code = ERROR_MARK;
36115 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36117 cp_token *first_token;
36118 cp_token_cache *cp;
36122 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36123 p = obstack_alloc (&declarator_obstack, 0);
36125 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36128 switch (cp_lexer_peek_token (parser->lexer)->type)
36131 reduc_code = PLUS_EXPR;
36134 reduc_code = MULT_EXPR;
36137 reduc_code = MINUS_EXPR;
36140 reduc_code = BIT_AND_EXPR;
36143 reduc_code = BIT_XOR_EXPR;
36146 reduc_code = BIT_IOR_EXPR;
36149 reduc_code = TRUTH_ANDIF_EXPR;
36152 reduc_code = TRUTH_ORIF_EXPR;
36155 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36158 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36159 "%<|%>, %<&&%>, %<||%> or identifier");
36163 if (reduc_code != ERROR_MARK)
36164 cp_lexer_consume_token (parser->lexer);
36166 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36167 if (reduc_id == error_mark_node)
36170 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36173 /* Types may not be defined in declare reduction type list. */
36174 const char *saved_message;
36175 saved_message = parser->type_definition_forbidden_message;
36176 parser->type_definition_forbidden_message
36177 = G_("types may not be defined in declare reduction type list");
36178 bool saved_colon_corrects_to_scope_p;
36179 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36180 parser->colon_corrects_to_scope_p = false;
36181 bool saved_colon_doesnt_start_class_def_p;
36182 saved_colon_doesnt_start_class_def_p
36183 = parser->colon_doesnt_start_class_def_p;
36184 parser->colon_doesnt_start_class_def_p = true;
36188 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36189 type = cp_parser_type_id (parser);
36190 if (type == error_mark_node)
36192 else if (ARITHMETIC_TYPE_P (type)
36193 && (orig_reduc_id == NULL_TREE
36194 || (TREE_CODE (type) != COMPLEX_TYPE
36195 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36197 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36199 error_at (loc, "predeclared arithmetic type %qT in "
36200 "%<#pragma omp declare reduction%>", type);
36201 else if (TREE_CODE (type) == FUNCTION_TYPE
36202 || TREE_CODE (type) == METHOD_TYPE
36203 || TREE_CODE (type) == ARRAY_TYPE)
36204 error_at (loc, "function or array type %qT in "
36205 "%<#pragma omp declare reduction%>", type);
36206 else if (TREE_CODE (type) == REFERENCE_TYPE)
36207 error_at (loc, "reference type %qT in "
36208 "%<#pragma omp declare reduction%>", type);
36209 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36210 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36211 "%<#pragma omp declare reduction%>", type);
36213 types.safe_push (type);
36215 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36216 cp_lexer_consume_token (parser->lexer);
36221 /* Restore the saved message. */
36222 parser->type_definition_forbidden_message = saved_message;
36223 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36224 parser->colon_doesnt_start_class_def_p
36225 = saved_colon_doesnt_start_class_def_p;
36227 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36228 || types.is_empty ())
36231 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36235 first_token = cp_lexer_peek_token (parser->lexer);
36238 FOR_EACH_VEC_ELT (types, i, type)
36241 = build_function_type_list (void_type_node,
36242 cp_build_reference_type (type, false),
36244 tree this_reduc_id = reduc_id;
36245 if (!dependent_type_p (type))
36246 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36247 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36248 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36249 DECL_ARTIFICIAL (fndecl) = 1;
36250 DECL_EXTERNAL (fndecl) = 1;
36251 DECL_DECLARED_INLINE_P (fndecl) = 1;
36252 DECL_IGNORED_P (fndecl) = 1;
36253 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36254 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36255 DECL_ATTRIBUTES (fndecl)
36256 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36257 DECL_ATTRIBUTES (fndecl));
36258 if (processing_template_decl)
36259 fndecl = push_template_decl (fndecl);
36260 bool block_scope = false;
36261 tree block = NULL_TREE;
36262 if (current_function_decl)
36264 block_scope = true;
36265 DECL_CONTEXT (fndecl) = global_namespace;
36266 if (!processing_template_decl)
36269 else if (current_class_type)
36273 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36274 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36275 cp_lexer_consume_token (parser->lexer);
36276 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36278 cp = cp_token_cache_new (first_token,
36279 cp_lexer_peek_nth_token (parser->lexer,
36282 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36283 finish_member_declaration (fndecl);
36284 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36285 DECL_PENDING_INLINE_P (fndecl) = 1;
36286 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36291 DECL_CONTEXT (fndecl) = current_namespace;
36295 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36297 block = begin_omp_structured_block ();
36300 cp_parser_push_lexer_for_tokens (parser, cp);
36301 parser->lexer->in_pragma = true;
36303 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36306 finish_function (0);
36308 DECL_CONTEXT (fndecl) = current_function_decl;
36310 cp_parser_pop_lexer (parser);
36314 cp_parser_pop_lexer (parser);
36316 finish_function (0);
36319 DECL_CONTEXT (fndecl) = current_function_decl;
36320 block = finish_omp_structured_block (block);
36321 if (TREE_CODE (block) == BIND_EXPR)
36322 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36323 else if (TREE_CODE (block) == STATEMENT_LIST)
36324 DECL_SAVED_TREE (fndecl) = block;
36325 if (processing_template_decl)
36326 add_decl_expr (fndecl);
36328 cp_check_omp_declare_reduction (fndecl);
36329 if (cp == NULL && types.length () > 1)
36330 cp = cp_token_cache_new (first_token,
36331 cp_lexer_peek_nth_token (parser->lexer, 2));
36332 if (errs != errorcount)
36336 cp_parser_require_pragma_eol (parser, pragma_tok);
36339 /* Free any declarators allocated. */
36340 obstack_free (&declarator_obstack, p);
36344 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36345 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36346 initializer-clause[opt] new-line
36347 #pragma omp declare target new-line */
36350 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36351 enum pragma_context context)
36353 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36355 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36356 const char *p = IDENTIFIER_POINTER (id);
36358 if (strcmp (p, "simd") == 0)
36360 cp_lexer_consume_token (parser->lexer);
36361 cp_parser_omp_declare_simd (parser, pragma_tok,
36365 cp_ensure_no_omp_declare_simd (parser);
36366 if (strcmp (p, "reduction") == 0)
36368 cp_lexer_consume_token (parser->lexer);
36369 cp_parser_omp_declare_reduction (parser, pragma_tok,
36373 if (!flag_openmp) /* flag_openmp_simd */
36375 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36378 if (strcmp (p, "target") == 0)
36380 cp_lexer_consume_token (parser->lexer);
36381 cp_parser_omp_declare_target (parser, pragma_tok);
36385 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36387 cp_parser_require_pragma_eol (parser, pragma_tok);
36391 #pragma omp taskloop taskloop-clause[optseq] new-line
36394 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36397 #define OMP_TASKLOOP_CLAUSE_MASK \
36398 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36414 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36415 char *p_name, omp_clause_mask mask, tree *cclauses,
36418 tree clauses, sb, ret;
36420 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36422 strcat (p_name, " taskloop");
36423 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36425 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36427 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36428 const char *p = IDENTIFIER_POINTER (id);
36430 if (strcmp (p, "simd") == 0)
36432 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36433 if (cclauses == NULL)
36434 cclauses = cclauses_buf;
36436 cp_lexer_consume_token (parser->lexer);
36437 if (!flag_openmp) /* flag_openmp_simd */
36438 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36440 sb = begin_omp_structured_block ();
36441 save = cp_parser_begin_omp_structured_block (parser);
36442 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36444 cp_parser_end_omp_structured_block (parser, save);
36445 tree body = finish_omp_structured_block (sb);
36448 ret = make_node (OMP_TASKLOOP);
36449 TREE_TYPE (ret) = void_type_node;
36450 OMP_FOR_BODY (ret) = body;
36451 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36452 SET_EXPR_LOCATION (ret, loc);
36457 if (!flag_openmp) /* flag_openmp_simd */
36459 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36463 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36467 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36468 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36471 sb = begin_omp_structured_block ();
36472 save = cp_parser_begin_omp_structured_block (parser);
36474 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36477 cp_parser_end_omp_structured_block (parser, save);
36478 add_stmt (finish_omp_structured_block (sb));
36485 # pragma acc routine oacc-routine-clause[optseq] new-line
36486 function-definition
36488 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36491 #define OACC_ROUTINE_CLAUSE_MASK \
36492 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36498 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36499 component, which must resolve to a declared namespace-scope
36500 function. The clauses are either processed directly (for a named
36501 function), or defered until the immediatley following declaration
36505 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36506 enum pragma_context context)
36508 bool first_p = parser->oacc_routine == NULL;
36509 location_t loc = pragma_tok->location;
36510 cp_omp_declare_simd_data data;
36513 data.error_seen = false;
36514 data.fndecl_seen = false;
36515 data.tokens = vNULL;
36516 data.clauses = NULL_TREE;
36517 parser->oacc_routine = &data;
36520 tree decl = NULL_TREE;
36521 /* Create a dummy claue, to record location. */
36522 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36524 if (context != pragma_external)
36526 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36527 parser->oacc_routine->error_seen = true;
36528 parser->oacc_routine = NULL;
36532 /* Look for optional '( name )'. */
36533 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36537 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36538 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36539 cp_lexer_consume_token (parser->lexer);
36540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36541 parser->oacc_routine->error_seen = true;
36542 cp_parser_require_pragma_eol (parser, pragma_tok);
36544 error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36545 "%<#pragma acc routine%> not followed by a "
36546 "function declaration or definition");
36548 parser->oacc_routine->error_seen = true;
36552 cp_lexer_consume_token (parser->lexer);
36553 cp_token *token = cp_lexer_peek_token (parser->lexer);
36555 /* We parse the name as an id-expression. If it resolves to
36556 anything other than a non-overloaded function at namespace
36557 scope, it's an error. */
36558 tree id = cp_parser_id_expression (parser,
36559 /*template_keyword_p=*/false,
36560 /*check_dependency_p=*/false,
36561 /*template_p=*/NULL,
36562 /*declarator_p=*/false,
36563 /*optional_p=*/false);
36564 decl = cp_parser_lookup_name_simple (parser, id, token->location);
36565 if (id != error_mark_node && decl == error_mark_node)
36566 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36569 if (decl == error_mark_node
36570 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36572 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36573 parser->oacc_routine = NULL;
36577 /* Build a chain of clauses. */
36578 parser->lexer->in_pragma = true;
36579 tree clauses = NULL_TREE;
36580 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36581 "#pragma acc routine",
36582 cp_lexer_peek_token
36585 /* Force clauses to be non-null, by attaching context to it. */
36586 clauses = tree_cons (c_head, clauses, NULL_TREE);
36588 if (decl && is_overloaded_fn (decl)
36589 && (TREE_CODE (decl) != FUNCTION_DECL
36590 || DECL_FUNCTION_TEMPLATE_P (decl)))
36592 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36593 parser->oacc_routine = NULL;
36597 /* Perhaps we should use the same rule as declarations in different
36599 if (!DECL_NAMESPACE_SCOPE_P (decl))
36601 error_at (loc, "%<#pragma acc routine%> does not refer to a "
36602 "namespace scope function");
36603 parser->oacc_routine = NULL;
36607 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36610 "%<#pragma acc routine%> does not refer to a function");
36611 parser->oacc_routine = NULL;
36615 data.clauses = clauses;
36617 cp_finalize_oacc_routine (parser, decl, false);
36618 data.tokens.release ();
36619 parser->oacc_routine = NULL;
36623 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36624 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36625 cp_lexer_consume_token (parser->lexer);
36626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36627 parser->oacc_routine->error_seen = true;
36628 cp_parser_require_pragma_eol (parser, pragma_tok);
36630 struct cp_token_cache *cp
36631 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36632 parser->oacc_routine->tokens.safe_push (cp);
36635 parser->oacc_routine->clauses = c_head;
36637 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36638 cp_parser_pragma (parser, context, NULL);
36642 /* Create an empty list of clauses. */
36643 parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36645 cp_parser_declaration (parser);
36647 if (parser->oacc_routine
36648 && !parser->oacc_routine->error_seen
36649 && !parser->oacc_routine->fndecl_seen)
36650 error_at (loc, "%<#pragma acc routine%> not followed by a "
36651 "function declaration or definition");
36653 data.tokens.release ();
36654 parser->oacc_routine = NULL;
36659 /* Finalize #pragma acc routine clauses after direct declarator has
36660 been parsed, and put that into "oacc function" attribute. */
36663 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36665 struct cp_token_cache *ce;
36666 cp_omp_declare_simd_data *data = parser->oacc_routine;
36667 tree cl, clauses = parser->oacc_routine->clauses;
36670 loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36672 if ((!data->error_seen && data->fndecl_seen)
36673 || data->tokens.length () != 1)
36675 error_at (loc, "%<#pragma acc routine%> not followed by a "
36676 "function declaration or definition");
36677 data->error_seen = true;
36680 if (data->error_seen)
36683 ce = data->tokens[0];
36685 cp_parser_push_lexer_for_tokens (parser, ce);
36686 parser->lexer->in_pragma = true;
36687 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36689 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36690 cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36691 "#pragma acc routine", pragma_tok);
36692 cp_parser_pop_lexer (parser);
36694 tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36696 /* Force clauses to be non-null, by attaching context to it. */
36697 parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36699 data->fndecl_seen = true;
36703 /* Apply any saved OpenACC routine clauses to a just-parsed
36707 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36709 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36711 tree clauses = parser->oacc_routine->clauses;
36712 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36714 if (parser->oacc_routine->error_seen)
36717 if (fndecl == error_mark_node)
36719 parser->oacc_routine = NULL;
36723 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36725 cp_ensure_no_oacc_routine (parser);
36729 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36732 "%<#pragma acc routine%> not followed by a function "
36733 "declaration or definition");
36734 parser->oacc_routine = NULL;
36737 if (get_oacc_fn_attrib (fndecl))
36739 error_at (loc, "%<#pragma acc routine%> already applied to %D",
36741 parser->oacc_routine = NULL;
36744 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36746 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36747 TREE_USED (fndecl) ? "use" : "definition");
36748 parser->oacc_routine = NULL;
36751 /* Process for function attrib */
36752 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36753 replace_oacc_fn_attrib (fndecl, dims);
36755 /* Add an "omp target" attribute. */
36756 DECL_ATTRIBUTES (fndecl)
36757 = tree_cons (get_identifier ("omp declare target"),
36758 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36762 /* Main entry point to OpenMP statement pragmas. */
36765 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36768 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36769 omp_clause_mask mask (0);
36771 switch (cp_parser_pragma_kind (pragma_tok))
36773 case PRAGMA_OACC_ATOMIC:
36774 cp_parser_omp_atomic (parser, pragma_tok);
36776 case PRAGMA_OACC_CACHE:
36777 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36779 case PRAGMA_OACC_DATA:
36780 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
36782 case PRAGMA_OACC_ENTER_DATA:
36783 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36785 case PRAGMA_OACC_EXIT_DATA:
36786 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36788 case PRAGMA_OACC_HOST_DATA:
36789 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
36791 case PRAGMA_OACC_KERNELS:
36792 case PRAGMA_OACC_PARALLEL:
36793 strcpy (p_name, "#pragma acc");
36794 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
36797 case PRAGMA_OACC_LOOP:
36798 strcpy (p_name, "#pragma acc");
36799 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
36802 case PRAGMA_OACC_UPDATE:
36803 stmt = cp_parser_oacc_update (parser, pragma_tok);
36805 case PRAGMA_OACC_WAIT:
36806 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36808 case PRAGMA_OMP_ATOMIC:
36809 cp_parser_omp_atomic (parser, pragma_tok);
36811 case PRAGMA_OMP_CRITICAL:
36812 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
36814 case PRAGMA_OMP_DISTRIBUTE:
36815 strcpy (p_name, "#pragma omp");
36816 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
36819 case PRAGMA_OMP_FOR:
36820 strcpy (p_name, "#pragma omp");
36821 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
36824 case PRAGMA_OMP_MASTER:
36825 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
36827 case PRAGMA_OMP_PARALLEL:
36828 strcpy (p_name, "#pragma omp");
36829 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
36832 case PRAGMA_OMP_SECTIONS:
36833 strcpy (p_name, "#pragma omp");
36834 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36836 case PRAGMA_OMP_SIMD:
36837 strcpy (p_name, "#pragma omp");
36838 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
36841 case PRAGMA_OMP_SINGLE:
36842 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
36844 case PRAGMA_OMP_TASK:
36845 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
36847 case PRAGMA_OMP_TASKGROUP:
36848 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
36850 case PRAGMA_OMP_TASKLOOP:
36851 strcpy (p_name, "#pragma omp");
36852 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
36855 case PRAGMA_OMP_TEAMS:
36856 strcpy (p_name, "#pragma omp");
36857 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
36861 gcc_unreachable ();
36864 protected_set_expr_location (stmt, pragma_tok->location);
36867 /* Transactional Memory parsing routines. */
36869 /* Parse a transaction attribute.
36875 We use this instead of cp_parser_attributes_opt for transactions to avoid
36876 the pedwarn in C++98 mode. */
36879 cp_parser_txn_attribute_opt (cp_parser *parser)
36882 tree attr_name, attr = NULL;
36884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36885 return cp_parser_attributes_opt (parser);
36887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36889 cp_lexer_consume_token (parser->lexer);
36890 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36893 token = cp_lexer_peek_token (parser->lexer);
36894 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36896 token = cp_lexer_consume_token (parser->lexer);
36898 attr_name = (token->type == CPP_KEYWORD
36899 /* For keywords, use the canonical spelling,
36900 not the parsed identifier. */
36901 ? ridpointers[(int) token->keyword]
36903 attr = build_tree_list (attr_name, NULL_TREE);
36906 cp_parser_error (parser, "expected identifier");
36908 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36910 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36914 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36916 transaction-statement:
36917 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36919 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36923 cp_parser_transaction (cp_parser *parser, cp_token *token)
36925 unsigned char old_in = parser->in_transaction;
36926 unsigned char this_in = 1, new_in;
36927 enum rid keyword = token->keyword;
36928 tree stmt, attrs, noex;
36930 cp_lexer_consume_token (parser->lexer);
36932 if (keyword == RID_TRANSACTION_RELAXED
36933 || keyword == RID_SYNCHRONIZED)
36934 this_in |= TM_STMT_ATTR_RELAXED;
36937 attrs = cp_parser_txn_attribute_opt (parser);
36939 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36942 /* Parse a noexcept specification. */
36943 if (keyword == RID_ATOMIC_NOEXCEPT)
36944 noex = boolean_true_node;
36945 else if (keyword == RID_ATOMIC_CANCEL)
36947 /* cancel-and-throw is unimplemented. */
36948 sorry ("atomic_cancel");
36952 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36954 /* Keep track if we're in the lexical scope of an outer transaction. */
36955 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36957 stmt = begin_transaction_stmt (token->location, NULL, this_in);
36959 parser->in_transaction = new_in;
36960 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36961 parser->in_transaction = old_in;
36963 finish_transaction_stmt (stmt, NULL, this_in, noex);
36968 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36970 transaction-expression:
36971 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36972 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36976 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36978 unsigned char old_in = parser->in_transaction;
36979 unsigned char this_in = 1;
36983 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36985 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36986 || keyword == RID_TRANSACTION_RELAXED);
36990 keyword == RID_TRANSACTION_RELAXED
36991 ? G_("%<__transaction_relaxed%> without transactional memory "
36993 : G_("%<__transaction_atomic%> without transactional memory "
36994 "support enabled"));
36996 token = cp_parser_require_keyword (parser, keyword,
36997 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36998 : RT_TRANSACTION_RELAXED));
36999 gcc_assert (token != NULL);
37001 if (keyword == RID_TRANSACTION_RELAXED)
37002 this_in |= TM_STMT_ATTR_RELAXED;
37004 /* Set this early. This might mean that we allow transaction_cancel in
37005 an expression that we find out later actually has to be a constexpr.
37006 However, we expect that cxx_constant_value will be able to deal with
37007 this; also, if the noexcept has no constexpr, then what we parse next
37008 really is a transaction's body. */
37009 parser->in_transaction = this_in;
37011 /* Parse a noexcept specification. */
37012 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37015 if (!noex || !noex_expr
37016 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37018 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37020 expr = cp_parser_expression (parser);
37021 expr = finish_parenthesized_expr (expr);
37023 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37027 /* The only expression that is available got parsed for the noexcept
37028 already. noexcept is true then. */
37030 noex = boolean_true_node;
37033 expr = build_transaction_expr (token->location, expr, this_in, noex);
37034 parser->in_transaction = old_in;
37036 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37037 return error_mark_node;
37039 return (flag_tm ? expr : error_mark_node);
37042 /* Parse a function-transaction-block.
37044 function-transaction-block:
37045 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37047 __transaction_atomic txn-attribute[opt] function-try-block
37048 __transaction_relaxed ctor-initializer[opt] function-body
37049 __transaction_relaxed function-try-block
37053 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37055 unsigned char old_in = parser->in_transaction;
37056 unsigned char new_in = 1;
37057 tree compound_stmt, stmt, attrs;
37058 bool ctor_initializer_p;
37061 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37062 || keyword == RID_TRANSACTION_RELAXED);
37063 token = cp_parser_require_keyword (parser, keyword,
37064 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37065 : RT_TRANSACTION_RELAXED));
37066 gcc_assert (token != NULL);
37068 if (keyword == RID_TRANSACTION_RELAXED)
37069 new_in |= TM_STMT_ATTR_RELAXED;
37072 attrs = cp_parser_txn_attribute_opt (parser);
37074 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37077 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37079 parser->in_transaction = new_in;
37081 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37082 ctor_initializer_p = cp_parser_function_try_block (parser);
37084 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37085 (parser, /*in_function_try_block=*/false);
37087 parser->in_transaction = old_in;
37089 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37091 return ctor_initializer_p;
37094 /* Parse a __transaction_cancel statement.
37097 __transaction_cancel txn-attribute[opt] ;
37098 __transaction_cancel txn-attribute[opt] throw-expression ;
37100 ??? Cancel and throw is not yet implemented. */
37103 cp_parser_transaction_cancel (cp_parser *parser)
37106 bool is_outer = false;
37109 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37110 RT_TRANSACTION_CANCEL);
37111 gcc_assert (token != NULL);
37113 attrs = cp_parser_txn_attribute_opt (parser);
37115 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37117 /* ??? Parse cancel-and-throw here. */
37119 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37123 error_at (token->location, "%<__transaction_cancel%> without "
37124 "transactional memory support enabled");
37125 return error_mark_node;
37127 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37129 error_at (token->location, "%<__transaction_cancel%> within a "
37130 "%<__transaction_relaxed%>");
37131 return error_mark_node;
37135 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37136 && !is_tm_may_cancel_outer (current_function_decl))
37138 error_at (token->location, "outer %<__transaction_cancel%> not "
37139 "within outer %<__transaction_atomic%>");
37140 error_at (token->location,
37141 " or a %<transaction_may_cancel_outer%> function");
37142 return error_mark_node;
37145 else if (parser->in_transaction == 0)
37147 error_at (token->location, "%<__transaction_cancel%> not within "
37148 "%<__transaction_atomic%>");
37149 return error_mark_node;
37152 stmt = build_tm_abort_call (token->location, is_outer);
37160 static GTY (()) cp_parser *the_parser;
37163 /* Special handling for the first token or line in the file. The first
37164 thing in the file might be #pragma GCC pch_preprocess, which loads a
37165 PCH file, which is a GC collection point. So we need to handle this
37166 first pragma without benefit of an existing lexer structure.
37168 Always returns one token to the caller in *FIRST_TOKEN. This is
37169 either the true first token of the file, or the first token after
37170 the initial pragma. */
37173 cp_parser_initial_pragma (cp_token *first_token)
37177 cp_lexer_get_preprocessor_token (NULL, first_token);
37178 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37181 cp_lexer_get_preprocessor_token (NULL, first_token);
37182 if (first_token->type == CPP_STRING)
37184 name = first_token->u.value;
37186 cp_lexer_get_preprocessor_token (NULL, first_token);
37187 if (first_token->type != CPP_PRAGMA_EOL)
37188 error_at (first_token->location,
37189 "junk at end of %<#pragma GCC pch_preprocess%>");
37192 error_at (first_token->location, "expected string literal");
37194 /* Skip to the end of the pragma. */
37195 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37196 cp_lexer_get_preprocessor_token (NULL, first_token);
37198 /* Now actually load the PCH file. */
37200 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37202 /* Read one more token to return to our caller. We have to do this
37203 after reading the PCH file in, since its pointers have to be
37205 cp_lexer_get_preprocessor_token (NULL, first_token);
37208 /* Parses the grainsize pragma for the _Cilk_for statement.
37210 #pragma cilk grainsize = <VALUE>. */
37213 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37215 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37217 tree exp = cp_parser_binary_expression (parser, false, false,
37218 PREC_NOT_OPERATOR, NULL);
37219 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37220 if (!exp || exp == error_mark_node)
37222 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37226 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37227 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37228 cp_parser_cilk_for (parser, exp, if_p);
37230 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37231 "%<#pragma cilk grainsize%> is not followed by "
37235 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37238 /* Normal parsing of a pragma token. Here we can (and must) use the
37242 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37244 cp_token *pragma_tok;
37249 pragma_tok = cp_lexer_consume_token (parser->lexer);
37250 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37251 parser->lexer->in_pragma = true;
37253 id = cp_parser_pragma_kind (pragma_tok);
37254 if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
37255 cp_ensure_no_omp_declare_simd (parser);
37258 case PRAGMA_GCC_PCH_PREPROCESS:
37259 error_at (pragma_tok->location,
37260 "%<#pragma GCC pch_preprocess%> must be first");
37263 case PRAGMA_OMP_BARRIER:
37266 case pragma_compound:
37267 cp_parser_omp_barrier (parser, pragma_tok);
37270 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37271 "used in compound statements");
37278 case PRAGMA_OMP_FLUSH:
37281 case pragma_compound:
37282 cp_parser_omp_flush (parser, pragma_tok);
37285 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37286 "used in compound statements");
37293 case PRAGMA_OMP_TASKWAIT:
37296 case pragma_compound:
37297 cp_parser_omp_taskwait (parser, pragma_tok);
37300 error_at (pragma_tok->location,
37301 "%<#pragma omp taskwait%> may only be "
37302 "used in compound statements");
37309 case PRAGMA_OMP_TASKYIELD:
37312 case pragma_compound:
37313 cp_parser_omp_taskyield (parser, pragma_tok);
37316 error_at (pragma_tok->location,
37317 "%<#pragma omp taskyield%> may only be "
37318 "used in compound statements");
37325 case PRAGMA_OMP_CANCEL:
37328 case pragma_compound:
37329 cp_parser_omp_cancel (parser, pragma_tok);
37332 error_at (pragma_tok->location,
37333 "%<#pragma omp cancel%> may only be "
37334 "used in compound statements");
37341 case PRAGMA_OMP_CANCELLATION_POINT:
37344 case pragma_compound:
37345 cp_parser_omp_cancellation_point (parser, pragma_tok);
37348 error_at (pragma_tok->location,
37349 "%<#pragma omp cancellation point%> may only be "
37350 "used in compound statements");
37357 case PRAGMA_OMP_THREADPRIVATE:
37358 cp_parser_omp_threadprivate (parser, pragma_tok);
37361 case PRAGMA_OMP_DECLARE_REDUCTION:
37362 cp_parser_omp_declare (parser, pragma_tok, context);
37365 case PRAGMA_OACC_DECLARE:
37366 cp_parser_oacc_declare (parser, pragma_tok);
37369 case PRAGMA_OACC_ROUTINE:
37370 cp_parser_oacc_routine (parser, pragma_tok, context);
37373 case PRAGMA_OACC_ATOMIC:
37374 case PRAGMA_OACC_CACHE:
37375 case PRAGMA_OACC_DATA:
37376 case PRAGMA_OACC_ENTER_DATA:
37377 case PRAGMA_OACC_EXIT_DATA:
37378 case PRAGMA_OACC_HOST_DATA:
37379 case PRAGMA_OACC_KERNELS:
37380 case PRAGMA_OACC_PARALLEL:
37381 case PRAGMA_OACC_LOOP:
37382 case PRAGMA_OACC_UPDATE:
37383 case PRAGMA_OACC_WAIT:
37384 case PRAGMA_OMP_ATOMIC:
37385 case PRAGMA_OMP_CRITICAL:
37386 case PRAGMA_OMP_DISTRIBUTE:
37387 case PRAGMA_OMP_FOR:
37388 case PRAGMA_OMP_MASTER:
37389 case PRAGMA_OMP_PARALLEL:
37390 case PRAGMA_OMP_SECTIONS:
37391 case PRAGMA_OMP_SIMD:
37392 case PRAGMA_OMP_SINGLE:
37393 case PRAGMA_OMP_TASK:
37394 case PRAGMA_OMP_TASKGROUP:
37395 case PRAGMA_OMP_TASKLOOP:
37396 case PRAGMA_OMP_TEAMS:
37397 if (context != pragma_stmt && context != pragma_compound)
37399 stmt = push_omp_privatization_clauses (false);
37400 cp_parser_omp_construct (parser, pragma_tok, if_p);
37401 pop_omp_privatization_clauses (stmt);
37404 case PRAGMA_OMP_ORDERED:
37405 stmt = push_omp_privatization_clauses (false);
37406 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37407 pop_omp_privatization_clauses (stmt);
37410 case PRAGMA_OMP_TARGET:
37411 stmt = push_omp_privatization_clauses (false);
37412 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37413 pop_omp_privatization_clauses (stmt);
37416 case PRAGMA_OMP_END_DECLARE_TARGET:
37417 cp_parser_omp_end_declare_target (parser, pragma_tok);
37420 case PRAGMA_OMP_SECTION:
37421 error_at (pragma_tok->location,
37422 "%<#pragma omp section%> may only be used in "
37423 "%<#pragma omp sections%> construct");
37428 if (context == pragma_external)
37430 error_at (pragma_tok->location,
37431 "%<#pragma GCC ivdep%> must be inside a function");
37434 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37436 tok = cp_lexer_peek_token (the_parser->lexer);
37437 if (tok->type != CPP_KEYWORD
37438 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37439 && tok->keyword != RID_DO))
37441 cp_parser_error (parser, "for, while or do statement expected");
37444 cp_parser_iteration_statement (parser, if_p, true);
37448 case PRAGMA_CILK_SIMD:
37449 if (context == pragma_external)
37451 error_at (pragma_tok->location,
37452 "%<#pragma simd%> must be inside a function");
37455 stmt = push_omp_privatization_clauses (false);
37456 cp_parser_cilk_simd (parser, pragma_tok, if_p);
37457 pop_omp_privatization_clauses (stmt);
37460 case PRAGMA_CILK_GRAINSIZE:
37461 if (context == pragma_external)
37463 error_at (pragma_tok->location,
37464 "%<#pragma cilk grainsize%> must be inside a function");
37468 /* Ignore the pragma if Cilk Plus is not enabled. */
37471 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37476 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37477 "%<#pragma cilk grainsize%>");
37482 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37483 c_invoke_pragma_handler (id);
37487 cp_parser_error (parser, "expected declaration specifiers");
37491 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37495 /* The interface the pragma parsers have to the lexer. */
37498 pragma_lex (tree *value, location_t *loc)
37500 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37501 enum cpp_ttype ret = tok->type;
37503 *value = tok->u.value;
37505 *loc = tok->location;
37507 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37509 else if (ret == CPP_STRING)
37510 *value = cp_parser_string_literal (the_parser, false, false);
37513 if (ret == CPP_KEYWORD)
37515 cp_lexer_consume_token (the_parser->lexer);
37522 /* External interface. */
37524 /* Parse one entire translation unit. */
37527 c_parse_file (void)
37529 static bool already_called = false;
37531 if (already_called)
37532 fatal_error (input_location,
37533 "inter-module optimizations not implemented for C++");
37534 already_called = true;
37536 the_parser = cp_parser_new ();
37537 push_deferring_access_checks (flag_access_control
37538 ? dk_no_deferred : dk_no_check);
37539 cp_parser_translation_unit (the_parser);
37543 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37544 vectorlength clause:
37546 vectorlength ( constant-expression ) */
37549 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37552 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37554 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37555 safelen clause. Thus, vectorlength is represented as OMP 4.0
37556 safelen. For SIMD-enabled function it is represented by OMP 4.0
37559 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37562 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37565 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37566 return error_mark_node;
37568 expr = cp_parser_constant_expression (parser);
37569 expr = maybe_constant_value (expr);
37571 /* If expr == error_mark_node, then don't emit any errors nor
37572 create a clause. if any of the above functions returns
37573 error mark node then they would have emitted an error message. */
37574 if (expr == error_mark_node)
37576 else if (!TREE_TYPE (expr)
37577 || !TREE_CONSTANT (expr)
37578 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37579 error_at (loc, "vectorlength must be an integer constant");
37580 else if (TREE_CONSTANT (expr)
37581 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37582 error_at (loc, "vectorlength must be a power of 2");
37588 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37589 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37590 OMP_CLAUSE_CHAIN (c) = clauses;
37595 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37596 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37597 OMP_CLAUSE_CHAIN (c) = clauses;
37602 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37603 return error_mark_node;
37607 /* Handles the Cilk Plus #pragma simd linear clause.
37609 linear ( simd-linear-variable-list )
37611 simd-linear-variable-list:
37612 simd-linear-variable
37613 simd-linear-variable-list , simd-linear-variable
37615 simd-linear-variable:
37617 id-expression : simd-linear-step
37620 conditional-expression */
37623 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37625 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37627 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37631 cp_parser_error (parser, "expected identifier");
37632 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37633 return error_mark_node;
37636 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37637 parser->colon_corrects_to_scope_p = false;
37640 cp_token *token = cp_lexer_peek_token (parser->lexer);
37641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37643 cp_parser_error (parser, "expected variable-name");
37644 clauses = error_mark_node;
37648 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37650 tree decl = cp_parser_lookup_name_simple (parser, var_name,
37652 if (decl == error_mark_node)
37654 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37656 clauses = error_mark_node;
37660 tree e = NULL_TREE;
37661 tree step_size = integer_one_node;
37663 /* If present, parse the linear step. Otherwise, assume the default
37665 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37667 cp_lexer_consume_token (parser->lexer);
37669 e = cp_parser_assignment_expression (parser);
37670 e = maybe_constant_value (e);
37672 if (e == error_mark_node)
37674 /* If an error has occurred, then the whole pragma is
37675 considered ill-formed. Thus, no reason to keep
37677 clauses = error_mark_node;
37680 else if (type_dependent_expression_p (e)
37681 || value_dependent_expression_p (e)
37683 && INTEGRAL_TYPE_P (TREE_TYPE (e))
37684 && (TREE_CONSTANT (e)
37688 cp_parser_error (parser,
37689 "step size must be an integer constant "
37690 "expression or an integer variable");
37693 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37694 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37695 OMP_CLAUSE_DECL (l) = decl;
37696 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37697 OMP_CLAUSE_CHAIN (l) = clauses;
37700 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37701 cp_lexer_consume_token (parser->lexer);
37702 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37706 error_at (cp_lexer_peek_token (parser->lexer)->location,
37707 "expected %<,%> or %<)%> after %qE", decl);
37708 clauses = error_mark_node;
37712 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37713 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37717 /* Returns the name of the next clause. If the clause is not
37718 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37719 token is not consumed. Otherwise, the appropriate enum from the
37720 pragma_simd_clause is returned and the token is consumed. */
37722 static pragma_omp_clause
37723 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37725 pragma_omp_clause clause_type;
37726 cp_token *token = cp_lexer_peek_token (parser->lexer);
37728 if (token->keyword == RID_PRIVATE)
37729 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37730 else if (!token->u.value || token->type != CPP_NAME)
37731 return PRAGMA_CILK_CLAUSE_NONE;
37732 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37733 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37734 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37735 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37736 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37737 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37738 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37739 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37740 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37741 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37743 return PRAGMA_CILK_CLAUSE_NONE;
37745 cp_lexer_consume_token (parser->lexer);
37746 return clause_type;
37749 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37752 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37754 tree clauses = NULL_TREE;
37756 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37757 && clauses != error_mark_node)
37759 pragma_omp_clause c_kind;
37760 c_kind = cp_parser_cilk_simd_clause_name (parser);
37761 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37762 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37763 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37764 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37765 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37766 /* Use the OpenMP 4.0 equivalent function. */
37767 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37768 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37769 /* Use the OpenMP 4.0 equivalent function. */
37770 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37772 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37773 /* Use the OMP 4.0 equivalent function. */
37774 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37776 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37777 /* Use the OMP 4.0 equivalent function. */
37778 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37781 clauses = error_mark_node;
37782 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37787 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37789 if (clauses == error_mark_node)
37790 return error_mark_node;
37792 return c_finish_cilk_clauses (clauses);
37795 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37798 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
37800 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37802 if (clauses == error_mark_node)
37805 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37807 error_at (cp_lexer_peek_token (parser->lexer)->location,
37808 "for statement expected");
37812 tree sb = begin_omp_structured_block ();
37813 int save = cp_parser_begin_omp_structured_block (parser);
37814 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
37816 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37817 cp_parser_end_omp_structured_block (parser, save);
37818 add_stmt (finish_omp_structured_block (sb));
37821 /* Main entry-point for parsing Cilk Plus _Cilk_for
37822 loops. The return value is error_mark_node
37823 when errors happen and CILK_FOR tree on success. */
37826 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
37828 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37829 gcc_unreachable ();
37831 tree sb = begin_omp_structured_block ();
37832 int save = cp_parser_begin_omp_structured_block (parser);
37834 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37835 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37836 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37837 clauses = finish_omp_clauses (clauses, false);
37839 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
37841 cpp_validate_cilk_plus_loop (ret);
37843 ret = error_mark_node;
37845 cp_parser_end_omp_structured_block (parser, save);
37846 add_stmt (finish_omp_structured_block (sb));
37850 /* Create an identifier for a generic parameter type (a synthesized
37851 template parameter implied by `auto' or a concept identifier). */
37853 static GTY(()) int generic_parm_count;
37855 make_generic_type_name ()
37858 sprintf (buf, "auto:%d", ++generic_parm_count);
37859 return get_identifier (buf);
37862 /* Predicate that behaves as is_auto_or_concept but matches the parent
37863 node of the generic type rather than the generic type itself. This
37864 allows for type transformation in add_implicit_template_parms. */
37867 tree_type_is_auto_or_concept (const_tree t)
37869 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37872 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37873 (creating a new template parameter list if necessary). Returns the newly
37874 created template type parm. */
37877 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
37879 gcc_assert (current_binding_level->kind == sk_function_parms);
37881 /* Before committing to modifying any scope, if we're in an
37882 implicit template scope, and we're trying to synthesize a
37883 constrained parameter, try to find a previous parameter with
37884 the same name. This is the same-type rule for abbreviated
37885 function templates.
37887 NOTE: We can generate implicit parameters when tentatively
37888 parsing a nested name specifier, only to reject that parse
37889 later. However, matching the same template-id as part of a
37890 direct-declarator should generate an identical template
37891 parameter, so this rule will merge them. */
37892 if (parser->implicit_template_scope && constr)
37894 tree t = parser->implicit_template_parms;
37897 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37899 tree d = TREE_VALUE (t);
37900 if (TREE_CODE (d) == PARM_DECL)
37901 /* Return the TEMPLATE_PARM_INDEX. */
37902 d = DECL_INITIAL (d);
37905 t = TREE_CHAIN (t);
37909 /* We are either continuing a function template that already contains implicit
37910 template parameters, creating a new fully-implicit function template, or
37911 extending an existing explicit function template with implicit template
37914 cp_binding_level *const entry_scope = current_binding_level;
37916 bool become_template = false;
37917 cp_binding_level *parent_scope = 0;
37919 if (parser->implicit_template_scope)
37921 gcc_assert (parser->implicit_template_parms);
37923 current_binding_level = parser->implicit_template_scope;
37927 /* Roll back to the existing template parameter scope (in the case of
37928 extending an explicit function template) or introduce a new template
37929 parameter scope ahead of the function parameter scope (or class scope
37930 in the case of out-of-line member definitions). The function scope is
37931 added back after template parameter synthesis below. */
37933 cp_binding_level *scope = entry_scope;
37935 while (scope->kind == sk_function_parms)
37937 parent_scope = scope;
37938 scope = scope->level_chain;
37940 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37942 /* If not defining a class, then any class scope is a scope level in
37943 an out-of-line member definition. In this case simply wind back
37944 beyond the first such scope to inject the template parameter list.
37945 Otherwise wind back to the class being defined. The latter can
37946 occur in class member friend declarations such as:
37952 friend void A::foo (auto);
37955 The template parameter list synthesized for the friend declaration
37956 must be injected in the scope of 'B'. This can also occur in
37957 erroneous cases such as:
37963 void B::foo (auto) {}
37966 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37967 but, nevertheless, the template parameter list synthesized for the
37968 declarator should be injected into the scope of 'A' as if the
37969 ill-formed template was specified explicitly. */
37971 while (scope->kind == sk_class && !scope->defining_class_p)
37973 parent_scope = scope;
37974 scope = scope->level_chain;
37978 current_binding_level = scope;
37980 if (scope->kind != sk_template_parms
37981 || !function_being_declared_is_template_p (parser))
37983 /* Introduce a new template parameter list for implicit template
37986 become_template = true;
37988 parser->implicit_template_scope
37989 = begin_scope (sk_template_parms, NULL);
37991 ++processing_template_decl;
37993 parser->fully_implicit_function_template_p = true;
37994 ++parser->num_template_parameter_lists;
37998 /* Synthesize implicit template parameters at the end of the explicit
37999 template parameter list. */
38001 gcc_assert (current_template_parms);
38003 parser->implicit_template_scope = scope;
38005 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38006 parser->implicit_template_parms
38007 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38011 /* Synthesize a new template parameter and track the current template
38012 parameter chain with implicit_template_parms. */
38014 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38015 tree synth_id = make_generic_type_name ();
38016 tree synth_tmpl_parm;
38017 bool non_type = false;
38019 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38021 = finish_template_type_parm (class_type_node, synth_id);
38022 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38024 = finish_constrained_template_template_parm (proto, synth_id);
38027 synth_tmpl_parm = copy_decl (proto);
38028 DECL_NAME (synth_tmpl_parm) = synth_id;
38032 // Attach the constraint to the parm before processing.
38033 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38034 TREE_TYPE (node) = constr;
38036 = process_template_parm (parser->implicit_template_parms,
38039 /*non_type=*/non_type,
38040 /*param_pack=*/false);
38042 // Chain the new parameter to the list of implicit parameters.
38043 if (parser->implicit_template_parms)
38044 parser->implicit_template_parms
38045 = TREE_CHAIN (parser->implicit_template_parms);
38047 parser->implicit_template_parms = new_parm;
38049 tree new_decl = getdecls ();
38051 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38052 new_decl = DECL_INITIAL (new_decl);
38054 /* If creating a fully implicit function template, start the new implicit
38055 template parameter list with this synthesized type, otherwise grow the
38056 current template parameter list. */
38058 if (become_template)
38060 parent_scope->level_chain = current_binding_level;
38062 tree new_parms = make_tree_vec (1);
38063 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38064 current_template_parms = tree_cons (size_int (processing_template_decl),
38065 new_parms, current_template_parms);
38069 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38070 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38071 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38072 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38075 // If the new parameter was constrained, we need to add that to the
38076 // constraints in the template parameter list.
38077 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38079 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38080 reqs = conjoin_constraints (reqs, req);
38081 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38084 current_binding_level = entry_scope;
38089 /* Finish the declaration of a fully implicit function template. Such a
38090 template has no explicit template parameter list so has not been through the
38091 normal template head and tail processing. synthesize_implicit_template_parm
38092 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38093 provided if the declaration is a class member such that its template
38094 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38095 form is returned. Otherwise NULL_TREE is returned. */
38098 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38100 gcc_assert (parser->fully_implicit_function_template_p);
38102 if (member_decl_opt && member_decl_opt != error_mark_node
38103 && DECL_VIRTUAL_P (member_decl_opt))
38105 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38106 "implicit templates may not be %<virtual%>");
38107 DECL_VIRTUAL_P (member_decl_opt) = false;
38110 if (member_decl_opt)
38111 member_decl_opt = finish_member_template_decl (member_decl_opt);
38112 end_template_decl ();
38114 parser->fully_implicit_function_template_p = false;
38115 --parser->num_template_parameter_lists;
38117 return member_decl_opt;
38120 #include "gt-cp-parser.h"